gdb/x86: Fix write out of mxcsr register for xsave targets
[deliverable/binutils-gdb.git] / gdb / corelow.c
CommitLineData
c906108c 1/* Core dump and executable file functions below target vector, for GDB.
4646aa9d 2
e2882c85 3 Copyright (C) 1986-2018 Free Software Foundation, Inc.
c906108c 4
c5aa993b 5 This file is part of GDB.
c906108c 6
c5aa993b
JM
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
c5aa993b 10 (at your option) any later version.
c906108c 11
c5aa993b
JM
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
c906108c 16
c5aa993b 17 You should have received a copy of the GNU General Public License
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
19
20#include "defs.h"
0e24ac5d 21#include "arch-utils.h"
c906108c
SS
22#include <signal.h>
23#include <fcntl.h>
fc24370e
MS
24#ifdef HAVE_SYS_FILE_H
25#include <sys/file.h> /* needed for F_OK and friends */
26#endif
c5aa993b 27#include "frame.h" /* required by inferior.h */
c906108c 28#include "inferior.h"
45741a9c 29#include "infrun.h"
c906108c
SS
30#include "symtab.h"
31#include "command.h"
32#include "bfd.h"
33#include "target.h"
34#include "gdbcore.h"
35#include "gdbthread.h"
4e052eda 36#include "regcache.h"
0e24ac5d 37#include "regset.h"
990f9fe3 38#include "symfile.h"
4646aa9d 39#include "exec.h"
dbda9972 40#include "readline/readline.h"
a77053c2 41#include "solib.h"
f90c07ac 42#include "filenames.h"
6c95b8df 43#include "progspace.h"
516ba659 44#include "objfiles.h"
cbb099e8 45#include "gdb_bfd.h"
9852c492 46#include "completer.h"
614c279d 47#include "filestuff.h"
8e860359 48
ee28ca0f
AC
49#ifndef O_LARGEFILE
50#define O_LARGEFILE 0
51#endif
52
15244507
PA
53static core_fns *sniff_core_bfd (gdbarch *core_gdbarch,
54 bfd *abfd);
55
f6ac5f3d
PA
56/* The core file target. */
57
d9f719f1
PA
58static const target_info core_target_info = {
59 "core",
60 N_("Local core dump file"),
61 N_("Use a core file as a target. Specify the filename of the core file.")
62};
63
f6ac5f3d
PA
64class core_target final : public target_ops
65{
66public:
15244507
PA
67 core_target ();
68 ~core_target () override;
f6ac5f3d 69
d9f719f1
PA
70 const target_info &info () const override
71 { return core_target_info; }
f6ac5f3d 72
f6ac5f3d
PA
73 void close () override;
74 void detach (inferior *, int) override;
75 void fetch_registers (struct regcache *, int) override;
76
77 enum target_xfer_status xfer_partial (enum target_object object,
78 const char *annex,
79 gdb_byte *readbuf,
80 const gdb_byte *writebuf,
81 ULONGEST offset, ULONGEST len,
82 ULONGEST *xfered_len) override;
83 void files_info () override;
84
57810aa7 85 bool thread_alive (ptid_t ptid) override;
f6ac5f3d
PA
86 const struct target_desc *read_description () override;
87
88 const char *pid_to_str (ptid_t) override;
89
90 const char *thread_name (struct thread_info *) override;
91
57810aa7
PA
92 bool has_memory () override;
93 bool has_stack () override;
94 bool has_registers () override;
f6ac5f3d 95 bool info_proc (const char *, enum info_proc_what) override;
f6ac5f3d 96
15244507
PA
97 /* A few helpers. */
98
99 /* Getter, see variable definition. */
100 struct gdbarch *core_gdbarch ()
101 {
102 return m_core_gdbarch;
103 }
104
105 /* See definition. */
106 void get_core_register_section (struct regcache *regcache,
107 const struct regset *regset,
108 const char *name,
109 int min_size,
110 int which,
111 const char *human_name,
112 bool required);
113
114private: /* per-core data */
115
116 /* The core's section table. Note that these target sections are
117 *not* mapped in the current address spaces' set of target
118 sections --- those should come only from pure executable or
119 shared library bfds. The core bfd sections are an implementation
120 detail of the core target, just like ptrace is for unix child
121 targets. */
122 target_section_table m_core_section_table {};
123
124 /* The core_fns for a core file handler that is prepared to read the
125 core file currently open on core_bfd. */
126 core_fns *m_core_vec = NULL;
127
128 /* FIXME: kettenis/20031023: Eventually this field should
129 disappear. */
130 struct gdbarch *m_core_gdbarch = NULL;
131};
c906108c 132
15244507
PA
133core_target::core_target ()
134{
135 to_stratum = process_stratum;
c906108c 136
15244507 137 m_core_gdbarch = gdbarch_from_bfd (core_bfd);
2acceee2 138
15244507
PA
139 /* Find a suitable core file handler to munch on core_bfd */
140 m_core_vec = sniff_core_bfd (m_core_gdbarch, core_bfd);
2acceee2 141
15244507
PA
142 /* Find the data section */
143 if (build_section_table (core_bfd,
144 &m_core_section_table.sections,
145 &m_core_section_table.sections_end))
146 error (_("\"%s\": Can't find sections: %s"),
147 bfd_get_filename (core_bfd), bfd_errmsg (bfd_get_error ()));
148}
0e24ac5d 149
15244507
PA
150core_target::~core_target ()
151{
152 xfree (m_core_section_table.sections);
153}
0e24ac5d 154
15244507
PA
155/* List of all available core_fns. On gdb startup, each core file
156 register reader calls deprecated_add_core_fns() to register
157 information on each core format it is prepared to read. */
07b82ea5 158
15244507 159static struct core_fns *core_file_fns = NULL;
2acceee2 160
020cc13c 161static int gdb_check_format (bfd *);
2acceee2 162
4efb68b1 163static void add_to_thread_list (bfd *, asection *, void *);
c906108c 164
7f9f62ba
PA
165/* An arbitrary identifier for the core inferior. */
166#define CORELOW_PID 1
167
aff410f1
MS
168/* Link a new core_fns into the global core_file_fns list. Called on
169 gdb startup by the _initialize routine in each core file register
b021a221 170 reader, to register information about each format the reader is
aff410f1 171 prepared to handle. */
c906108c
SS
172
173void
00e32a35 174deprecated_add_core_fns (struct core_fns *cf)
c906108c 175{
c5aa993b 176 cf->next = core_file_fns;
c906108c
SS
177 core_file_fns = cf;
178}
179
2acceee2
JM
180/* The default function that core file handlers can use to examine a
181 core file BFD and decide whether or not to accept the job of
aff410f1 182 reading the core file. */
2acceee2
JM
183
184int
fba45db2 185default_core_sniffer (struct core_fns *our_fns, bfd *abfd)
2acceee2
JM
186{
187 int result;
188
189 result = (bfd_get_flavour (abfd) == our_fns -> core_flavour);
190 return (result);
191}
192
193/* Walk through the list of core functions to find a set that can
06b9f45f 194 handle the core file open on ABFD. Returns pointer to set that is
aff410f1 195 selected. */
2acceee2
JM
196
197static struct core_fns *
15244507 198sniff_core_bfd (struct gdbarch *core_gdbarch, bfd *abfd)
2acceee2
JM
199{
200 struct core_fns *cf;
201 struct core_fns *yummy = NULL;
45eba0ab 202 int matches = 0;
2acceee2 203
aff410f1
MS
204 /* Don't sniff if we have support for register sets in
205 CORE_GDBARCH. */
29082443 206 if (core_gdbarch && gdbarch_iterate_over_regset_sections_p (core_gdbarch))
0e24ac5d
MK
207 return NULL;
208
2acceee2
JM
209 for (cf = core_file_fns; cf != NULL; cf = cf->next)
210 {
211 if (cf->core_sniffer (cf, abfd))
212 {
213 yummy = cf;
214 matches++;
215 }
216 }
217 if (matches > 1)
218 {
8a3fe4f8 219 warning (_("\"%s\": ambiguous core format, %d handlers match"),
2acceee2
JM
220 bfd_get_filename (abfd), matches);
221 }
222 else if (matches == 0)
06b9f45f
JK
223 error (_("\"%s\": no core file handler recognizes format"),
224 bfd_get_filename (abfd));
225
2acceee2
JM
226 return (yummy);
227}
228
229/* The default is to reject every core file format we see. Either
230 BFD has to recognize it, or we have to provide a function in the
aff410f1 231 core file handler that recognizes it. */
2acceee2
JM
232
233int
fba45db2 234default_check_format (bfd *abfd)
2acceee2
JM
235{
236 return (0);
237}
238
aff410f1 239/* Attempt to recognize core file formats that BFD rejects. */
2acceee2 240
020cc13c 241static int
fba45db2 242gdb_check_format (bfd *abfd)
2acceee2
JM
243{
244 struct core_fns *cf;
245
246 for (cf = core_file_fns; cf != NULL; cf = cf->next)
247 {
248 if (cf->check_format (abfd))
249 {
81a9a963 250 return (1);
2acceee2
JM
251 }
252 }
81a9a963 253 return (0);
2acceee2 254}
c906108c 255
15244507 256/* Close the core target. */
c906108c 257
15244507
PA
258void
259core_target::close ()
c906108c 260{
c906108c
SS
261 if (core_bfd)
262 {
959b8724 263 int pid = ptid_get_pid (inferior_ptid);
aff410f1
MS
264 inferior_ptid = null_ptid; /* Avoid confusion from thread
265 stuff. */
06b9f45f
JK
266 if (pid != 0)
267 exit_inferior_silent (pid);
c906108c 268
aff410f1
MS
269 /* Clear out solib state while the bfd is still open. See
270 comments in clear_solib in solib.c. */
a77053c2 271 clear_solib ();
7a292a7a 272
cbb099e8 273 gdb_bfd_unref (core_bfd);
c906108c 274 core_bfd = NULL;
c906108c 275 }
c906108c 276
15244507
PA
277 /* Core targets are heap-allocated (see core_target_open), so here
278 we delete ourselves. */
279 delete this;
74b7792f
AC
280}
281
aff410f1
MS
282/* Look for sections whose names start with `.reg/' so that we can
283 extract the list of threads in a core file. */
c906108c
SS
284
285static void
4efb68b1 286add_to_thread_list (bfd *abfd, asection *asect, void *reg_sect_arg)
c906108c 287{
0de3b513 288 ptid_t ptid;
3cdd9356
PA
289 int core_tid;
290 int pid, lwpid;
c906108c 291 asection *reg_sect = (asection *) reg_sect_arg;
88f38a04
PA
292 int fake_pid_p = 0;
293 struct inferior *inf;
c906108c 294
61012eef 295 if (!startswith (bfd_section_name (abfd, asect), ".reg/"))
c906108c
SS
296 return;
297
3cdd9356 298 core_tid = atoi (bfd_section_name (abfd, asect) + 5);
c906108c 299
261b8d08
PA
300 pid = bfd_core_file_pid (core_bfd);
301 if (pid == 0)
3cdd9356 302 {
88f38a04 303 fake_pid_p = 1;
3cdd9356 304 pid = CORELOW_PID;
3cdd9356 305 }
0de3b513 306
261b8d08
PA
307 lwpid = core_tid;
308
88f38a04
PA
309 inf = current_inferior ();
310 if (inf->pid == 0)
311 {
312 inferior_appeared (inf, pid);
313 inf->fake_pid_p = fake_pid_p;
314 }
3cdd9356
PA
315
316 ptid = ptid_build (pid, lwpid, 0);
317
318 add_thread (ptid);
c906108c
SS
319
320/* Warning, Will Robinson, looking at BFD private data! */
321
322 if (reg_sect != NULL
aff410f1
MS
323 && asect->filepos == reg_sect->filepos) /* Did we find .reg? */
324 inferior_ptid = ptid; /* Yes, make it current. */
c906108c
SS
325}
326
451953fa
PA
327/* Issue a message saying we have no core to debug, if FROM_TTY. */
328
329static void
330maybe_say_no_core_file_now (int from_tty)
331{
332 if (from_tty)
333 printf_filtered (_("No core file now.\n"));
334}
335
336/* Backward compatability with old way of specifying core files. */
337
338void
339core_file_command (const char *filename, int from_tty)
340{
341 dont_repeat (); /* Either way, seems bogus. */
342
343 if (filename == NULL)
344 {
345 if (core_bfd != NULL)
346 {
347 target_detach (current_inferior (), from_tty);
348 gdb_assert (core_bfd == NULL);
349 }
350 else
351 maybe_say_no_core_file_now (from_tty);
352 }
353 else
354 core_target_open (filename, from_tty);
355}
356
d9f719f1 357/* See gdbcore.h. */
c906108c 358
f6ac5f3d 359void
d9f719f1 360core_target_open (const char *arg, int from_tty)
c906108c
SS
361{
362 const char *p;
363 int siggy;
364 struct cleanup *old_chain;
c906108c 365 int scratch_chan;
ee28ca0f 366 int flags;
c906108c
SS
367
368 target_preopen (from_tty);
014f9477 369 if (!arg)
c906108c 370 {
8a3fe4f8 371 if (core_bfd)
3e43a32a
MS
372 error (_("No core file specified. (Use `detach' "
373 "to stop debugging a core file.)"));
8a3fe4f8
AC
374 else
375 error (_("No core file specified."));
c906108c
SS
376 }
377
ee0c3293
TT
378 gdb::unique_xmalloc_ptr<char> filename (tilde_expand (arg));
379 if (!IS_ABSOLUTE_PATH (filename.get ()))
380 filename.reset (concat (current_directory, "/",
381 filename.get (), (char *) NULL));
c906108c 382
ee28ca0f
AC
383 flags = O_BINARY | O_LARGEFILE;
384 if (write_files)
385 flags |= O_RDWR;
386 else
387 flags |= O_RDONLY;
ee0c3293 388 scratch_chan = gdb_open_cloexec (filename.get (), flags, 0);
c906108c 389 if (scratch_chan < 0)
ee0c3293 390 perror_with_name (filename.get ());
c906108c 391
ee0c3293 392 gdb_bfd_ref_ptr temp_bfd (gdb_bfd_fopen (filename.get (), gnutarget,
192b62ce
TT
393 write_files ? FOPEN_RUB : FOPEN_RB,
394 scratch_chan));
c906108c 395 if (temp_bfd == NULL)
ee0c3293 396 perror_with_name (filename.get ());
c906108c 397
192b62ce
TT
398 if (!bfd_check_format (temp_bfd.get (), bfd_core)
399 && !gdb_check_format (temp_bfd.get ()))
c906108c
SS
400 {
401 /* Do it after the err msg */
aff410f1
MS
402 /* FIXME: should be checking for errors from bfd_close (for one
403 thing, on error it does not free all the storage associated
404 with the bfd). */
8a3fe4f8 405 error (_("\"%s\" is not a core dump: %s"),
ee0c3293 406 filename.get (), bfd_errmsg (bfd_get_error ()));
c906108c
SS
407 }
408
192b62ce 409 core_bfd = temp_bfd.release ();
c906108c 410
15244507 411 core_target *target = new core_target ();
0e24ac5d 412
15244507
PA
413 /* Own the target until it is successfully pushed. */
414 target_ops_up target_holder (target);
2acceee2 415
c906108c
SS
416 validate_files ();
417
2f1b5984
MK
418 /* If we have no exec file, try to set the architecture from the
419 core file. We don't do this unconditionally since an exec file
420 typically contains more information that helps us determine the
421 architecture than a core file. */
422 if (!exec_bfd)
423 set_gdbarch_from_file (core_bfd);
cbda0a99 424
15244507
PA
425 push_target (target);
426 target_holder.release ();
c906108c 427
0de3b513
PA
428 /* Do this before acknowledging the inferior, so if
429 post_create_inferior throws (can happen easilly if you're loading
430 a core file with the wrong exec), we aren't left with threads
431 from the previous inferior. */
432 init_thread_list ();
433
3cdd9356 434 inferior_ptid = null_ptid;
0de3b513 435
739fc47a
PA
436 /* Need to flush the register cache (and the frame cache) from a
437 previous debug session. If inferior_ptid ends up the same as the
438 last debug session --- e.g., b foo; run; gcore core1; step; gcore
439 core2; core core1; core core2 --- then there's potential for
440 get_current_regcache to return the cached regcache of the
441 previous session, and the frame cache being stale. */
442 registers_changed ();
443
0de3b513
PA
444 /* Build up thread list from BFD sections, and possibly set the
445 current thread to the .reg/NN section matching the .reg
aff410f1 446 section. */
0de3b513
PA
447 bfd_map_over_sections (core_bfd, add_to_thread_list,
448 bfd_get_section_by_name (core_bfd, ".reg"));
449
3cdd9356
PA
450 if (ptid_equal (inferior_ptid, null_ptid))
451 {
452 /* Either we found no .reg/NN section, and hence we have a
453 non-threaded core (single-threaded, from gdb's perspective),
454 or for some reason add_to_thread_list couldn't determine
455 which was the "main" thread. The latter case shouldn't
456 usually happen, but we're dealing with input here, which can
457 always be broken in different ways. */
458 struct thread_info *thread = first_thread_of_process (-1);
c5504eaf 459
3cdd9356
PA
460 if (thread == NULL)
461 {
c45ceae0 462 inferior_appeared (current_inferior (), CORELOW_PID);
3cdd9356
PA
463 inferior_ptid = pid_to_ptid (CORELOW_PID);
464 add_thread_silent (inferior_ptid);
465 }
466 else
467 switch_to_thread (thread->ptid);
468 }
469
15244507 470 post_create_inferior (target, from_tty);
959b8724 471
0de3b513
PA
472 /* Now go through the target stack looking for threads since there
473 may be a thread_stratum target loaded on top of target core by
474 now. The layer above should claim threads found in the BFD
475 sections. */
492d29ea 476 TRY
8e7b59a5 477 {
e8032dde 478 target_update_thread_list ();
8e7b59a5
KS
479 }
480
492d29ea
PA
481 CATCH (except, RETURN_MASK_ERROR)
482 {
483 exception_print (gdb_stderr, except);
484 }
485 END_CATCH
0de3b513 486
c906108c
SS
487 p = bfd_core_file_failing_command (core_bfd);
488 if (p)
a3f17187 489 printf_filtered (_("Core was generated by `%s'.\n"), p);
c906108c 490
0c557179
SDJ
491 /* Clearing any previous state of convenience variables. */
492 clear_exit_convenience_vars ();
493
c906108c
SS
494 siggy = bfd_core_file_failing_signal (core_bfd);
495 if (siggy > 0)
423ec54c 496 {
15244507
PA
497 gdbarch *core_gdbarch = target->core_gdbarch ();
498
22203bbf 499 /* If we don't have a CORE_GDBARCH to work with, assume a native
1f8cf220
PA
500 core (map gdb_signal from host signals). If we do have
501 CORE_GDBARCH to work with, but no gdb_signal_from_target
502 implementation for that gdbarch, as a fallback measure,
503 assume the host signal mapping. It'll be correct for native
504 cores, but most likely incorrect for cross-cores. */
2ea28649 505 enum gdb_signal sig = (core_gdbarch != NULL
1f8cf220
PA
506 && gdbarch_gdb_signal_from_target_p (core_gdbarch)
507 ? gdbarch_gdb_signal_from_target (core_gdbarch,
508 siggy)
509 : gdb_signal_from_host (siggy));
423ec54c 510
2d503272
PM
511 printf_filtered (_("Program terminated with signal %s, %s.\n"),
512 gdb_signal_to_name (sig), gdb_signal_to_string (sig));
0c557179
SDJ
513
514 /* Set the value of the internal variable $_exitsignal,
515 which holds the signal uncaught by the inferior. */
516 set_internalvar_integer (lookup_internalvar ("_exitsignal"),
517 siggy);
423ec54c 518 }
c906108c 519
87ab71f0
PA
520 /* Fetch all registers from core file. */
521 target_fetch_registers (get_current_regcache (), -1);
c906108c 522
87ab71f0
PA
523 /* Now, set up the frame cache, and print the top of stack. */
524 reinit_frame_cache ();
08d72866 525 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
f0e8c4c5
JK
526
527 /* Current thread should be NUM 1 but the user does not know that.
528 If a program is single threaded gdb in general does not mention
529 anything about threads. That is why the test is >= 2. */
530 if (thread_count () >= 2)
531 {
492d29ea 532 TRY
f0e8c4c5
JK
533 {
534 thread_command (NULL, from_tty);
535 }
492d29ea
PA
536 CATCH (except, RETURN_MASK_ERROR)
537 {
538 exception_print (gdb_stderr, except);
539 }
540 END_CATCH
f0e8c4c5 541 }
c906108c
SS
542}
543
f6ac5f3d
PA
544void
545core_target::detach (inferior *inf, int from_tty)
c906108c 546{
15244507
PA
547 /* Note that 'this' is dangling after this call. unpush_target
548 closes the target, and our close implementation deletes
549 'this'. */
f6ac5f3d 550 unpush_target (this);
15244507 551
c906108c 552 reinit_frame_cache ();
451953fa 553 maybe_say_no_core_file_now (from_tty);
c906108c
SS
554}
555
de57eccd 556/* Try to retrieve registers from a section in core_bfd, and supply
15244507
PA
557 them to m_core_vec->core_read_registers, as the register set
558 numbered WHICH.
de57eccd 559
11a33714
SM
560 If ptid's lwp member is zero, do the single-threaded
561 thing: look for a section named NAME. If ptid's lwp
0de3b513
PA
562 member is non-zero, do the multi-threaded thing: look for a section
563 named "NAME/LWP", where LWP is the shortest ASCII decimal
11a33714 564 representation of ptid's lwp member.
de57eccd
JM
565
566 HUMAN_NAME is a human-readable name for the kind of registers the
567 NAME section contains, for use in error messages.
568
15244507
PA
569 If REQUIRED is true, print an error if the core file doesn't have a
570 section by the appropriate name. Otherwise, just do nothing. */
de57eccd 571
15244507
PA
572void
573core_target::get_core_register_section (struct regcache *regcache,
574 const struct regset *regset,
575 const char *name,
576 int min_size,
577 int which,
578 const char *human_name,
579 bool required)
de57eccd 580{
7be0c536 581 struct bfd_section *section;
de57eccd
JM
582 bfd_size_type size;
583 char *contents;
874a1c8c
AT
584 bool variable_size_section = (regset != NULL
585 && regset->flags & REGSET_VARIABLE_SIZE);
de57eccd 586
3c3ae77e 587 thread_section_name section_name (name, regcache->ptid ());
de57eccd 588
3c3ae77e 589 section = bfd_get_section_by_name (core_bfd, section_name.c_str ());
de57eccd
JM
590 if (! section)
591 {
592 if (required)
aff410f1
MS
593 warning (_("Couldn't find %s registers in core file."),
594 human_name);
de57eccd
JM
595 return;
596 }
597
598 size = bfd_section_size (core_bfd, section);
8f0435f7
AA
599 if (size < min_size)
600 {
3c3ae77e
PA
601 warning (_("Section `%s' in core file too small."),
602 section_name.c_str ());
8f0435f7
AA
603 return;
604 }
874a1c8c 605 if (size != min_size && !variable_size_section)
f962539a
AA
606 {
607 warning (_("Unexpected size of section `%s' in core file."),
3c3ae77e 608 section_name.c_str ());
f962539a 609 }
8f0435f7 610
224c3ddb 611 contents = (char *) alloca (size);
de57eccd
JM
612 if (! bfd_get_section_contents (core_bfd, section, contents,
613 (file_ptr) 0, size))
614 {
8a3fe4f8 615 warning (_("Couldn't read %s registers from `%s' section in core file."),
3c3ae77e 616 human_name, section_name.c_str ());
de57eccd
JM
617 return;
618 }
619
8f0435f7
AA
620 if (regset != NULL)
621 {
9eefc95f 622 regset->supply_regset (regset, regcache, -1, contents, size);
0e24ac5d
MK
623 return;
624 }
625
15244507
PA
626 gdb_assert (m_core_vec != nullptr);
627 m_core_vec->core_read_registers (regcache, contents, size, which,
628 ((CORE_ADDR)
629 bfd_section_vma (core_bfd, section)));
de57eccd
JM
630}
631
15244507
PA
632/* Data passed to gdbarch_iterate_over_regset_sections's callback. */
633struct get_core_registers_cb_data
634{
635 core_target *target;
636 struct regcache *regcache;
637};
638
5aa82d05
AA
639/* Callback for get_core_registers that handles a single core file
640 register note section. */
641
642static void
643get_core_registers_cb (const char *sect_name, int size,
8f0435f7 644 const struct regset *regset,
5aa82d05
AA
645 const char *human_name, void *cb_data)
646{
15244507
PA
647 auto *data = (get_core_registers_cb_data *) cb_data;
648 bool required = false;
5aa82d05
AA
649
650 if (strcmp (sect_name, ".reg") == 0)
8f0435f7 651 {
15244507 652 required = true;
8f0435f7
AA
653 if (human_name == NULL)
654 human_name = "general-purpose";
655 }
5aa82d05 656 else if (strcmp (sect_name, ".reg2") == 0)
8f0435f7
AA
657 {
658 if (human_name == NULL)
659 human_name = "floating-point";
660 }
661
662 /* The 'which' parameter is only used when no regset is provided.
663 Thus we just set it to -1. */
15244507
PA
664 data->target->get_core_register_section (data->regcache, regset, sect_name,
665 size, -1, human_name, required);
5aa82d05 666}
de57eccd 667
c906108c
SS
668/* Get the registers out of a core file. This is the machine-
669 independent part. Fetch_core_registers is the machine-dependent
aff410f1
MS
670 part, typically implemented in the xm-file for each
671 architecture. */
c906108c
SS
672
673/* We just get all the registers, so we don't use regno. */
674
f6ac5f3d
PA
675void
676core_target::fetch_registers (struct regcache *regcache, int regno)
c906108c 677{
9c5ea4d9 678 int i;
5aa82d05 679 struct gdbarch *gdbarch;
c906108c 680
15244507
PA
681 if (!(m_core_gdbarch != nullptr
682 && gdbarch_iterate_over_regset_sections_p (m_core_gdbarch))
683 && (m_core_vec == NULL || m_core_vec->core_read_registers == NULL))
c906108c
SS
684 {
685 fprintf_filtered (gdb_stderr,
c5aa993b 686 "Can't fetch registers from this type of core file\n");
c906108c
SS
687 return;
688 }
689
ac7936df 690 gdbarch = regcache->arch ();
5aa82d05 691 if (gdbarch_iterate_over_regset_sections_p (gdbarch))
15244507
PA
692 {
693 get_core_registers_cb_data data = { this, regcache };
694 gdbarch_iterate_over_regset_sections (gdbarch,
695 get_core_registers_cb,
696 (void *) &data, NULL);
697 }
1b1818e4
UW
698 else
699 {
8f0435f7
AA
700 get_core_register_section (regcache, NULL,
701 ".reg", 0, 0, "general-purpose", 1);
702 get_core_register_section (regcache, NULL,
703 ".reg2", 0, 2, "floating-point", 0);
1b1818e4 704 }
c906108c 705
ee99023e 706 /* Mark all registers not found in the core as unavailable. */
ac7936df 707 for (i = 0; i < gdbarch_num_regs (regcache->arch ()); i++)
ee99023e 708 if (regcache_register_status (regcache, i) == REG_UNKNOWN)
9c5ea4d9 709 regcache_raw_supply (regcache, i, NULL);
c906108c
SS
710}
711
f6ac5f3d
PA
712void
713core_target::files_info ()
c906108c 714{
15244507 715 print_section_info (&m_core_section_table, core_bfd);
c906108c 716}
e2544d02 717\f
efcbbd14
UW
718struct spuid_list
719{
720 gdb_byte *buf;
721 ULONGEST offset;
722 LONGEST len;
723 ULONGEST pos;
724 ULONGEST written;
725};
726
727static void
728add_to_spuid_list (bfd *abfd, asection *asect, void *list_p)
729{
9a3c8263 730 struct spuid_list *list = (struct spuid_list *) list_p;
efcbbd14 731 enum bfd_endian byte_order
aff410f1 732 = bfd_big_endian (abfd) ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
efcbbd14
UW
733 int fd, pos = 0;
734
735 sscanf (bfd_section_name (abfd, asect), "SPU/%d/regs%n", &fd, &pos);
736 if (pos == 0)
737 return;
738
739 if (list->pos >= list->offset && list->pos + 4 <= list->offset + list->len)
740 {
741 store_unsigned_integer (list->buf + list->pos - list->offset,
742 4, byte_order, fd);
743 list->written += 4;
744 }
745 list->pos += 4;
746}
747
f6ac5f3d
PA
748enum target_xfer_status
749core_target::xfer_partial (enum target_object object, const char *annex,
750 gdb_byte *readbuf, const gdb_byte *writebuf,
751 ULONGEST offset, ULONGEST len, ULONGEST *xfered_len)
e2544d02
RM
752{
753 switch (object)
754 {
755 case TARGET_OBJECT_MEMORY:
15244507
PA
756 return (section_table_xfer_memory_partial
757 (readbuf, writebuf,
758 offset, len, xfered_len,
759 m_core_section_table.sections,
760 m_core_section_table.sections_end,
761 NULL));
e2544d02
RM
762
763 case TARGET_OBJECT_AUXV:
764 if (readbuf)
765 {
766 /* When the aux vector is stored in core file, BFD
767 represents this with a fake section called ".auxv". */
768
c4c5b7ba 769 struct bfd_section *section;
e2544d02 770 bfd_size_type size;
e2544d02
RM
771
772 section = bfd_get_section_by_name (core_bfd, ".auxv");
773 if (section == NULL)
2ed4b548 774 return TARGET_XFER_E_IO;
e2544d02
RM
775
776 size = bfd_section_size (core_bfd, section);
777 if (offset >= size)
9b409511 778 return TARGET_XFER_EOF;
e2544d02
RM
779 size -= offset;
780 if (size > len)
781 size = len;
9b409511
YQ
782
783 if (size == 0)
784 return TARGET_XFER_EOF;
785 if (!bfd_get_section_contents (core_bfd, section, readbuf,
786 (file_ptr) offset, size))
e2544d02 787 {
8a3fe4f8 788 warning (_("Couldn't read NT_AUXV note in core file."));
2ed4b548 789 return TARGET_XFER_E_IO;
e2544d02
RM
790 }
791
9b409511
YQ
792 *xfered_len = (ULONGEST) size;
793 return TARGET_XFER_OK;
e2544d02 794 }
2ed4b548 795 return TARGET_XFER_E_IO;
e2544d02 796
403e1656
MK
797 case TARGET_OBJECT_WCOOKIE:
798 if (readbuf)
799 {
800 /* When the StackGhost cookie is stored in core file, BFD
aff410f1
MS
801 represents this with a fake section called
802 ".wcookie". */
403e1656
MK
803
804 struct bfd_section *section;
805 bfd_size_type size;
403e1656
MK
806
807 section = bfd_get_section_by_name (core_bfd, ".wcookie");
808 if (section == NULL)
2ed4b548 809 return TARGET_XFER_E_IO;
403e1656
MK
810
811 size = bfd_section_size (core_bfd, section);
812 if (offset >= size)
96c4f946 813 return TARGET_XFER_EOF;
403e1656
MK
814 size -= offset;
815 if (size > len)
816 size = len;
9b409511
YQ
817
818 if (size == 0)
819 return TARGET_XFER_EOF;
820 if (!bfd_get_section_contents (core_bfd, section, readbuf,
821 (file_ptr) offset, size))
403e1656 822 {
8a3fe4f8 823 warning (_("Couldn't read StackGhost cookie in core file."));
2ed4b548 824 return TARGET_XFER_E_IO;
403e1656
MK
825 }
826
9b409511
YQ
827 *xfered_len = (ULONGEST) size;
828 return TARGET_XFER_OK;
829
403e1656 830 }
2ed4b548 831 return TARGET_XFER_E_IO;
403e1656 832
de584861 833 case TARGET_OBJECT_LIBRARIES:
15244507
PA
834 if (m_core_gdbarch != nullptr
835 && gdbarch_core_xfer_shared_libraries_p (m_core_gdbarch))
de584861
PA
836 {
837 if (writebuf)
2ed4b548 838 return TARGET_XFER_E_IO;
9b409511
YQ
839 else
840 {
15244507 841 *xfered_len = gdbarch_core_xfer_shared_libraries (m_core_gdbarch,
9b409511
YQ
842 readbuf,
843 offset, len);
844
845 if (*xfered_len == 0)
846 return TARGET_XFER_EOF;
847 else
848 return TARGET_XFER_OK;
849 }
de584861
PA
850 }
851 /* FALL THROUGH */
852
356a5233 853 case TARGET_OBJECT_LIBRARIES_AIX:
15244507
PA
854 if (m_core_gdbarch != nullptr
855 && gdbarch_core_xfer_shared_libraries_aix_p (m_core_gdbarch))
356a5233
JB
856 {
857 if (writebuf)
2ed4b548 858 return TARGET_XFER_E_IO;
9b409511
YQ
859 else
860 {
861 *xfered_len
15244507 862 = gdbarch_core_xfer_shared_libraries_aix (m_core_gdbarch,
9b409511
YQ
863 readbuf, offset,
864 len);
865
866 if (*xfered_len == 0)
867 return TARGET_XFER_EOF;
868 else
869 return TARGET_XFER_OK;
870 }
356a5233
JB
871 }
872 /* FALL THROUGH */
873
efcbbd14
UW
874 case TARGET_OBJECT_SPU:
875 if (readbuf && annex)
876 {
877 /* When the SPU contexts are stored in a core file, BFD
aff410f1
MS
878 represents this with a fake section called
879 "SPU/<annex>". */
efcbbd14
UW
880
881 struct bfd_section *section;
882 bfd_size_type size;
efcbbd14 883 char sectionstr[100];
c5504eaf 884
efcbbd14
UW
885 xsnprintf (sectionstr, sizeof sectionstr, "SPU/%s", annex);
886
887 section = bfd_get_section_by_name (core_bfd, sectionstr);
888 if (section == NULL)
2ed4b548 889 return TARGET_XFER_E_IO;
efcbbd14
UW
890
891 size = bfd_section_size (core_bfd, section);
892 if (offset >= size)
9b409511 893 return TARGET_XFER_EOF;
efcbbd14
UW
894 size -= offset;
895 if (size > len)
896 size = len;
9b409511
YQ
897
898 if (size == 0)
899 return TARGET_XFER_EOF;
900 if (!bfd_get_section_contents (core_bfd, section, readbuf,
901 (file_ptr) offset, size))
efcbbd14
UW
902 {
903 warning (_("Couldn't read SPU section in core file."));
2ed4b548 904 return TARGET_XFER_E_IO;
efcbbd14
UW
905 }
906
9b409511
YQ
907 *xfered_len = (ULONGEST) size;
908 return TARGET_XFER_OK;
efcbbd14
UW
909 }
910 else if (readbuf)
911 {
912 /* NULL annex requests list of all present spuids. */
913 struct spuid_list list;
c5504eaf 914
efcbbd14
UW
915 list.buf = readbuf;
916 list.offset = offset;
917 list.len = len;
918 list.pos = 0;
919 list.written = 0;
920 bfd_map_over_sections (core_bfd, add_to_spuid_list, &list);
9b409511
YQ
921
922 if (list.written == 0)
923 return TARGET_XFER_EOF;
924 else
925 {
926 *xfered_len = (ULONGEST) list.written;
927 return TARGET_XFER_OK;
928 }
efcbbd14 929 }
2ed4b548 930 return TARGET_XFER_E_IO;
efcbbd14 931
9015683b
TT
932 case TARGET_OBJECT_SIGNAL_INFO:
933 if (readbuf)
9b409511 934 {
15244507
PA
935 if (m_core_gdbarch != nullptr
936 && gdbarch_core_xfer_siginfo_p (m_core_gdbarch))
9b409511 937 {
15244507 938 LONGEST l = gdbarch_core_xfer_siginfo (m_core_gdbarch, readbuf,
382b69bb
JB
939 offset, len);
940
941 if (l >= 0)
942 {
943 *xfered_len = l;
944 if (l == 0)
945 return TARGET_XFER_EOF;
946 else
947 return TARGET_XFER_OK;
948 }
9b409511
YQ
949 }
950 }
2ed4b548 951 return TARGET_XFER_E_IO;
9015683b 952
e2544d02 953 default:
f6ac5f3d
PA
954 return this->beneath->xfer_partial (object, annex, readbuf,
955 writebuf, offset, len,
956 xfered_len);
e2544d02
RM
957 }
958}
959
c906108c 960\f
c906108c
SS
961
962/* Okay, let's be honest: threads gleaned from a core file aren't
963 exactly lively, are they? On the other hand, if we don't claim
964 that each & every one is alive, then we don't get any of them
965 to appear in an "info thread" command, which is quite a useful
966 behaviour.
c5aa993b 967 */
57810aa7 968bool
f6ac5f3d 969core_target::thread_alive (ptid_t ptid)
c906108c 970{
57810aa7 971 return true;
c906108c
SS
972}
973
4eb0ad19
DJ
974/* Ask the current architecture what it knows about this core file.
975 That will be used, in turn, to pick a better architecture. This
976 wrapper could be avoided if targets got a chance to specialize
15244507 977 core_target. */
4eb0ad19 978
f6ac5f3d
PA
979const struct target_desc *
980core_target::read_description ()
4eb0ad19 981{
15244507 982 if (m_core_gdbarch && gdbarch_core_read_description_p (m_core_gdbarch))
2117c711
TT
983 {
984 const struct target_desc *result;
985
15244507 986 result = gdbarch_core_read_description (m_core_gdbarch, this, core_bfd);
2117c711
TT
987 if (result != NULL)
988 return result;
989 }
4eb0ad19 990
f6ac5f3d 991 return this->beneath->read_description ();
4eb0ad19
DJ
992}
993
f6ac5f3d
PA
994const char *
995core_target::pid_to_str (ptid_t ptid)
0de3b513
PA
996{
997 static char buf[64];
88f38a04 998 struct inferior *inf;
a5ee0f0c 999 int pid;
0de3b513 1000
a5ee0f0c
PA
1001 /* The preferred way is to have a gdbarch/OS specific
1002 implementation. */
15244507
PA
1003 if (m_core_gdbarch != nullptr
1004 && gdbarch_core_pid_to_str_p (m_core_gdbarch))
1005 return gdbarch_core_pid_to_str (m_core_gdbarch, ptid);
c5504eaf 1006
a5ee0f0c
PA
1007 /* Otherwise, if we don't have one, we'll just fallback to
1008 "process", with normal_pid_to_str. */
28439f5e 1009
a5ee0f0c
PA
1010 /* Try the LWPID field first. */
1011 pid = ptid_get_lwp (ptid);
1012 if (pid != 0)
1013 return normal_pid_to_str (pid_to_ptid (pid));
1014
1015 /* Otherwise, this isn't a "threaded" core -- use the PID field, but
1016 only if it isn't a fake PID. */
c9657e70 1017 inf = find_inferior_ptid (ptid);
88f38a04 1018 if (inf != NULL && !inf->fake_pid_p)
a5ee0f0c 1019 return normal_pid_to_str (ptid);
0de3b513 1020
a5ee0f0c
PA
1021 /* No luck. We simply don't have a valid PID to print. */
1022 xsnprintf (buf, sizeof buf, "<main task>");
0de3b513
PA
1023 return buf;
1024}
1025
f6ac5f3d
PA
1026const char *
1027core_target::thread_name (struct thread_info *thr)
4dfc5dbc 1028{
15244507
PA
1029 if (m_core_gdbarch != nullptr
1030 && gdbarch_core_thread_name_p (m_core_gdbarch))
1031 return gdbarch_core_thread_name (m_core_gdbarch, thr);
4dfc5dbc
JB
1032 return NULL;
1033}
1034
57810aa7 1035bool
f6ac5f3d 1036core_target::has_memory ()
c35b1492
PA
1037{
1038 return (core_bfd != NULL);
1039}
1040
57810aa7 1041bool
f6ac5f3d 1042core_target::has_stack ()
c35b1492
PA
1043{
1044 return (core_bfd != NULL);
1045}
1046
57810aa7 1047bool
f6ac5f3d 1048core_target::has_registers ()
c35b1492
PA
1049{
1050 return (core_bfd != NULL);
1051}
1052
451b7c33
TT
1053/* Implement the to_info_proc method. */
1054
f6ac5f3d
PA
1055bool
1056core_target::info_proc (const char *args, enum info_proc_what request)
451b7c33
TT
1057{
1058 struct gdbarch *gdbarch = get_current_arch ();
1059
1060 /* Since this is the core file target, call the 'core_info_proc'
1061 method on gdbarch, not 'info_proc'. */
1062 if (gdbarch_core_info_proc_p (gdbarch))
1063 gdbarch_core_info_proc (gdbarch, args, request);
c906108c 1064
f6ac5f3d 1065 return true;
c906108c
SS
1066}
1067
c906108c 1068void
fba45db2 1069_initialize_corelow (void)
c906108c 1070{
d9f719f1 1071 add_target (core_target_info, core_target_open, filename_completer);
c906108c 1072}
This page took 1.320821 seconds and 4 git commands to generate.