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