Remove ptid_match
[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 {
aff410f1
MS
263 inferior_ptid = null_ptid; /* Avoid confusion from thread
264 stuff. */
00431a78 265 exit_inferior_silent (current_inferior ());
c906108c 266
aff410f1
MS
267 /* Clear out solib state while the bfd is still open. See
268 comments in clear_solib in solib.c. */
a77053c2 269 clear_solib ();
7a292a7a 270
06333fea 271 current_program_space->cbfd.reset (nullptr);
c906108c 272 }
c906108c 273
15244507
PA
274 /* Core targets are heap-allocated (see core_target_open), so here
275 we delete ourselves. */
276 delete this;
74b7792f
AC
277}
278
aff410f1
MS
279/* Look for sections whose names start with `.reg/' so that we can
280 extract the list of threads in a core file. */
c906108c
SS
281
282static void
4efb68b1 283add_to_thread_list (bfd *abfd, asection *asect, void *reg_sect_arg)
c906108c 284{
0de3b513 285 ptid_t ptid;
3cdd9356
PA
286 int core_tid;
287 int pid, lwpid;
c906108c 288 asection *reg_sect = (asection *) reg_sect_arg;
88f38a04
PA
289 int fake_pid_p = 0;
290 struct inferior *inf;
c906108c 291
61012eef 292 if (!startswith (bfd_section_name (abfd, asect), ".reg/"))
c906108c
SS
293 return;
294
3cdd9356 295 core_tid = atoi (bfd_section_name (abfd, asect) + 5);
c906108c 296
261b8d08
PA
297 pid = bfd_core_file_pid (core_bfd);
298 if (pid == 0)
3cdd9356 299 {
88f38a04 300 fake_pid_p = 1;
3cdd9356 301 pid = CORELOW_PID;
3cdd9356 302 }
0de3b513 303
261b8d08
PA
304 lwpid = core_tid;
305
88f38a04
PA
306 inf = current_inferior ();
307 if (inf->pid == 0)
308 {
309 inferior_appeared (inf, pid);
310 inf->fake_pid_p = fake_pid_p;
311 }
3cdd9356 312
fd79271b 313 ptid = ptid_t (pid, lwpid, 0);
3cdd9356
PA
314
315 add_thread (ptid);
c906108c
SS
316
317/* Warning, Will Robinson, looking at BFD private data! */
318
319 if (reg_sect != NULL
aff410f1
MS
320 && asect->filepos == reg_sect->filepos) /* Did we find .reg? */
321 inferior_ptid = ptid; /* Yes, make it current. */
c906108c
SS
322}
323
451953fa
PA
324/* Issue a message saying we have no core to debug, if FROM_TTY. */
325
326static void
327maybe_say_no_core_file_now (int from_tty)
328{
329 if (from_tty)
330 printf_filtered (_("No core file now.\n"));
331}
332
333/* Backward compatability with old way of specifying core files. */
334
335void
336core_file_command (const char *filename, int from_tty)
337{
338 dont_repeat (); /* Either way, seems bogus. */
339
340 if (filename == NULL)
341 {
342 if (core_bfd != NULL)
343 {
344 target_detach (current_inferior (), from_tty);
345 gdb_assert (core_bfd == NULL);
346 }
347 else
348 maybe_say_no_core_file_now (from_tty);
349 }
350 else
351 core_target_open (filename, from_tty);
352}
353
d9f719f1 354/* See gdbcore.h. */
c906108c 355
f6ac5f3d 356void
d9f719f1 357core_target_open (const char *arg, int from_tty)
c906108c
SS
358{
359 const char *p;
360 int siggy;
361 struct cleanup *old_chain;
c906108c 362 int scratch_chan;
ee28ca0f 363 int flags;
c906108c
SS
364
365 target_preopen (from_tty);
014f9477 366 if (!arg)
c906108c 367 {
8a3fe4f8 368 if (core_bfd)
3e43a32a
MS
369 error (_("No core file specified. (Use `detach' "
370 "to stop debugging a core file.)"));
8a3fe4f8
AC
371 else
372 error (_("No core file specified."));
c906108c
SS
373 }
374
ee0c3293
TT
375 gdb::unique_xmalloc_ptr<char> filename (tilde_expand (arg));
376 if (!IS_ABSOLUTE_PATH (filename.get ()))
377 filename.reset (concat (current_directory, "/",
378 filename.get (), (char *) NULL));
c906108c 379
ee28ca0f
AC
380 flags = O_BINARY | O_LARGEFILE;
381 if (write_files)
382 flags |= O_RDWR;
383 else
384 flags |= O_RDONLY;
ee0c3293 385 scratch_chan = gdb_open_cloexec (filename.get (), flags, 0);
c906108c 386 if (scratch_chan < 0)
ee0c3293 387 perror_with_name (filename.get ());
c906108c 388
ee0c3293 389 gdb_bfd_ref_ptr temp_bfd (gdb_bfd_fopen (filename.get (), gnutarget,
192b62ce
TT
390 write_files ? FOPEN_RUB : FOPEN_RB,
391 scratch_chan));
c906108c 392 if (temp_bfd == NULL)
ee0c3293 393 perror_with_name (filename.get ());
c906108c 394
192b62ce
TT
395 if (!bfd_check_format (temp_bfd.get (), bfd_core)
396 && !gdb_check_format (temp_bfd.get ()))
c906108c
SS
397 {
398 /* Do it after the err msg */
aff410f1
MS
399 /* FIXME: should be checking for errors from bfd_close (for one
400 thing, on error it does not free all the storage associated
401 with the bfd). */
8a3fe4f8 402 error (_("\"%s\" is not a core dump: %s"),
ee0c3293 403 filename.get (), bfd_errmsg (bfd_get_error ()));
c906108c
SS
404 }
405
06333fea 406 current_program_space->cbfd = std::move (temp_bfd);
c906108c 407
15244507 408 core_target *target = new core_target ();
0e24ac5d 409
15244507
PA
410 /* Own the target until it is successfully pushed. */
411 target_ops_up target_holder (target);
2acceee2 412
c906108c
SS
413 validate_files ();
414
2f1b5984
MK
415 /* If we have no exec file, try to set the architecture from the
416 core file. We don't do this unconditionally since an exec file
417 typically contains more information that helps us determine the
418 architecture than a core file. */
419 if (!exec_bfd)
420 set_gdbarch_from_file (core_bfd);
cbda0a99 421
15244507
PA
422 push_target (target);
423 target_holder.release ();
c906108c 424
0de3b513
PA
425 /* Do this before acknowledging the inferior, so if
426 post_create_inferior throws (can happen easilly if you're loading
427 a core file with the wrong exec), we aren't left with threads
428 from the previous inferior. */
429 init_thread_list ();
430
3cdd9356 431 inferior_ptid = null_ptid;
0de3b513 432
739fc47a
PA
433 /* Need to flush the register cache (and the frame cache) from a
434 previous debug session. If inferior_ptid ends up the same as the
435 last debug session --- e.g., b foo; run; gcore core1; step; gcore
436 core2; core core1; core core2 --- then there's potential for
437 get_current_regcache to return the cached regcache of the
438 previous session, and the frame cache being stale. */
439 registers_changed ();
440
0de3b513
PA
441 /* Build up thread list from BFD sections, and possibly set the
442 current thread to the .reg/NN section matching the .reg
aff410f1 443 section. */
0de3b513
PA
444 bfd_map_over_sections (core_bfd, add_to_thread_list,
445 bfd_get_section_by_name (core_bfd, ".reg"));
446
3cdd9356
PA
447 if (ptid_equal (inferior_ptid, null_ptid))
448 {
449 /* Either we found no .reg/NN section, and hence we have a
450 non-threaded core (single-threaded, from gdb's perspective),
451 or for some reason add_to_thread_list couldn't determine
452 which was the "main" thread. The latter case shouldn't
453 usually happen, but we're dealing with input here, which can
454 always be broken in different ways. */
00431a78 455 thread_info *thread = first_thread_of_inferior (current_inferior ());
c5504eaf 456
3cdd9356
PA
457 if (thread == NULL)
458 {
c45ceae0 459 inferior_appeared (current_inferior (), CORELOW_PID);
f2907e49 460 inferior_ptid = ptid_t (CORELOW_PID);
3cdd9356
PA
461 add_thread_silent (inferior_ptid);
462 }
463 else
00431a78 464 switch_to_thread (thread);
3cdd9356
PA
465 }
466
15244507 467 post_create_inferior (target, from_tty);
959b8724 468
0de3b513
PA
469 /* Now go through the target stack looking for threads since there
470 may be a thread_stratum target loaded on top of target core by
471 now. The layer above should claim threads found in the BFD
472 sections. */
492d29ea 473 TRY
8e7b59a5 474 {
e8032dde 475 target_update_thread_list ();
8e7b59a5
KS
476 }
477
492d29ea
PA
478 CATCH (except, RETURN_MASK_ERROR)
479 {
480 exception_print (gdb_stderr, except);
481 }
482 END_CATCH
0de3b513 483
c906108c
SS
484 p = bfd_core_file_failing_command (core_bfd);
485 if (p)
a3f17187 486 printf_filtered (_("Core was generated by `%s'.\n"), p);
c906108c 487
0c557179
SDJ
488 /* Clearing any previous state of convenience variables. */
489 clear_exit_convenience_vars ();
490
c906108c
SS
491 siggy = bfd_core_file_failing_signal (core_bfd);
492 if (siggy > 0)
423ec54c 493 {
15244507
PA
494 gdbarch *core_gdbarch = target->core_gdbarch ();
495
22203bbf 496 /* If we don't have a CORE_GDBARCH to work with, assume a native
1f8cf220
PA
497 core (map gdb_signal from host signals). If we do have
498 CORE_GDBARCH to work with, but no gdb_signal_from_target
499 implementation for that gdbarch, as a fallback measure,
500 assume the host signal mapping. It'll be correct for native
501 cores, but most likely incorrect for cross-cores. */
2ea28649 502 enum gdb_signal sig = (core_gdbarch != NULL
1f8cf220
PA
503 && gdbarch_gdb_signal_from_target_p (core_gdbarch)
504 ? gdbarch_gdb_signal_from_target (core_gdbarch,
505 siggy)
506 : gdb_signal_from_host (siggy));
423ec54c 507
2d503272
PM
508 printf_filtered (_("Program terminated with signal %s, %s.\n"),
509 gdb_signal_to_name (sig), gdb_signal_to_string (sig));
0c557179
SDJ
510
511 /* Set the value of the internal variable $_exitsignal,
512 which holds the signal uncaught by the inferior. */
513 set_internalvar_integer (lookup_internalvar ("_exitsignal"),
514 siggy);
423ec54c 515 }
c906108c 516
87ab71f0
PA
517 /* Fetch all registers from core file. */
518 target_fetch_registers (get_current_regcache (), -1);
c906108c 519
87ab71f0
PA
520 /* Now, set up the frame cache, and print the top of stack. */
521 reinit_frame_cache ();
08d72866 522 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
f0e8c4c5
JK
523
524 /* Current thread should be NUM 1 but the user does not know that.
525 If a program is single threaded gdb in general does not mention
526 anything about threads. That is why the test is >= 2. */
527 if (thread_count () >= 2)
528 {
492d29ea 529 TRY
f0e8c4c5
JK
530 {
531 thread_command (NULL, from_tty);
532 }
492d29ea
PA
533 CATCH (except, RETURN_MASK_ERROR)
534 {
535 exception_print (gdb_stderr, except);
536 }
537 END_CATCH
f0e8c4c5 538 }
c906108c
SS
539}
540
f6ac5f3d
PA
541void
542core_target::detach (inferior *inf, int from_tty)
c906108c 543{
15244507
PA
544 /* Note that 'this' is dangling after this call. unpush_target
545 closes the target, and our close implementation deletes
546 'this'. */
f6ac5f3d 547 unpush_target (this);
15244507 548
c906108c 549 reinit_frame_cache ();
451953fa 550 maybe_say_no_core_file_now (from_tty);
c906108c
SS
551}
552
de57eccd 553/* Try to retrieve registers from a section in core_bfd, and supply
15244507
PA
554 them to m_core_vec->core_read_registers, as the register set
555 numbered WHICH.
de57eccd 556
11a33714
SM
557 If ptid's lwp member is zero, do the single-threaded
558 thing: look for a section named NAME. If ptid's lwp
0de3b513
PA
559 member is non-zero, do the multi-threaded thing: look for a section
560 named "NAME/LWP", where LWP is the shortest ASCII decimal
11a33714 561 representation of ptid's lwp member.
de57eccd
JM
562
563 HUMAN_NAME is a human-readable name for the kind of registers the
564 NAME section contains, for use in error messages.
565
15244507
PA
566 If REQUIRED is true, print an error if the core file doesn't have a
567 section by the appropriate name. Otherwise, just do nothing. */
de57eccd 568
15244507
PA
569void
570core_target::get_core_register_section (struct regcache *regcache,
571 const struct regset *regset,
572 const char *name,
573 int min_size,
574 int which,
575 const char *human_name,
576 bool required)
de57eccd 577{
7be0c536 578 struct bfd_section *section;
de57eccd
JM
579 bfd_size_type size;
580 char *contents;
874a1c8c
AT
581 bool variable_size_section = (regset != NULL
582 && regset->flags & REGSET_VARIABLE_SIZE);
de57eccd 583
3c3ae77e 584 thread_section_name section_name (name, regcache->ptid ());
de57eccd 585
3c3ae77e 586 section = bfd_get_section_by_name (core_bfd, section_name.c_str ());
de57eccd
JM
587 if (! section)
588 {
589 if (required)
aff410f1
MS
590 warning (_("Couldn't find %s registers in core file."),
591 human_name);
de57eccd
JM
592 return;
593 }
594
595 size = bfd_section_size (core_bfd, section);
8f0435f7
AA
596 if (size < min_size)
597 {
3c3ae77e
PA
598 warning (_("Section `%s' in core file too small."),
599 section_name.c_str ());
8f0435f7
AA
600 return;
601 }
874a1c8c 602 if (size != min_size && !variable_size_section)
f962539a
AA
603 {
604 warning (_("Unexpected size of section `%s' in core file."),
3c3ae77e 605 section_name.c_str ());
f962539a 606 }
8f0435f7 607
224c3ddb 608 contents = (char *) alloca (size);
de57eccd
JM
609 if (! bfd_get_section_contents (core_bfd, section, contents,
610 (file_ptr) 0, size))
611 {
8a3fe4f8 612 warning (_("Couldn't read %s registers from `%s' section in core file."),
3c3ae77e 613 human_name, section_name.c_str ());
de57eccd
JM
614 return;
615 }
616
8f0435f7
AA
617 if (regset != NULL)
618 {
9eefc95f 619 regset->supply_regset (regset, regcache, -1, contents, size);
0e24ac5d
MK
620 return;
621 }
622
15244507
PA
623 gdb_assert (m_core_vec != nullptr);
624 m_core_vec->core_read_registers (regcache, contents, size, which,
625 ((CORE_ADDR)
626 bfd_section_vma (core_bfd, section)));
de57eccd
JM
627}
628
15244507
PA
629/* Data passed to gdbarch_iterate_over_regset_sections's callback. */
630struct get_core_registers_cb_data
631{
632 core_target *target;
633 struct regcache *regcache;
634};
635
5aa82d05
AA
636/* Callback for get_core_registers that handles a single core file
637 register note section. */
638
639static void
640get_core_registers_cb (const char *sect_name, int size,
8f0435f7 641 const struct regset *regset,
5aa82d05
AA
642 const char *human_name, void *cb_data)
643{
15244507
PA
644 auto *data = (get_core_registers_cb_data *) cb_data;
645 bool required = false;
5aa82d05
AA
646
647 if (strcmp (sect_name, ".reg") == 0)
8f0435f7 648 {
15244507 649 required = true;
8f0435f7
AA
650 if (human_name == NULL)
651 human_name = "general-purpose";
652 }
5aa82d05 653 else if (strcmp (sect_name, ".reg2") == 0)
8f0435f7
AA
654 {
655 if (human_name == NULL)
656 human_name = "floating-point";
657 }
658
659 /* The 'which' parameter is only used when no regset is provided.
660 Thus we just set it to -1. */
15244507
PA
661 data->target->get_core_register_section (data->regcache, regset, sect_name,
662 size, -1, human_name, required);
5aa82d05 663}
de57eccd 664
c906108c
SS
665/* Get the registers out of a core file. This is the machine-
666 independent part. Fetch_core_registers is the machine-dependent
aff410f1
MS
667 part, typically implemented in the xm-file for each
668 architecture. */
c906108c
SS
669
670/* We just get all the registers, so we don't use regno. */
671
f6ac5f3d
PA
672void
673core_target::fetch_registers (struct regcache *regcache, int regno)
c906108c 674{
9c5ea4d9 675 int i;
5aa82d05 676 struct gdbarch *gdbarch;
c906108c 677
15244507
PA
678 if (!(m_core_gdbarch != nullptr
679 && gdbarch_iterate_over_regset_sections_p (m_core_gdbarch))
680 && (m_core_vec == NULL || m_core_vec->core_read_registers == NULL))
c906108c
SS
681 {
682 fprintf_filtered (gdb_stderr,
c5aa993b 683 "Can't fetch registers from this type of core file\n");
c906108c
SS
684 return;
685 }
686
ac7936df 687 gdbarch = regcache->arch ();
5aa82d05 688 if (gdbarch_iterate_over_regset_sections_p (gdbarch))
15244507
PA
689 {
690 get_core_registers_cb_data data = { this, regcache };
691 gdbarch_iterate_over_regset_sections (gdbarch,
692 get_core_registers_cb,
693 (void *) &data, NULL);
694 }
1b1818e4
UW
695 else
696 {
8f0435f7
AA
697 get_core_register_section (regcache, NULL,
698 ".reg", 0, 0, "general-purpose", 1);
699 get_core_register_section (regcache, NULL,
700 ".reg2", 0, 2, "floating-point", 0);
1b1818e4 701 }
c906108c 702
ee99023e 703 /* Mark all registers not found in the core as unavailable. */
ac7936df 704 for (i = 0; i < gdbarch_num_regs (regcache->arch ()); i++)
0ec9f114 705 if (regcache->get_register_status (i) == REG_UNKNOWN)
73e1c03f 706 regcache->raw_supply (i, NULL);
c906108c
SS
707}
708
f6ac5f3d
PA
709void
710core_target::files_info ()
c906108c 711{
15244507 712 print_section_info (&m_core_section_table, core_bfd);
c906108c 713}
e2544d02 714\f
efcbbd14
UW
715struct spuid_list
716{
717 gdb_byte *buf;
718 ULONGEST offset;
719 LONGEST len;
720 ULONGEST pos;
721 ULONGEST written;
722};
723
724static void
725add_to_spuid_list (bfd *abfd, asection *asect, void *list_p)
726{
9a3c8263 727 struct spuid_list *list = (struct spuid_list *) list_p;
efcbbd14 728 enum bfd_endian byte_order
aff410f1 729 = bfd_big_endian (abfd) ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
efcbbd14
UW
730 int fd, pos = 0;
731
732 sscanf (bfd_section_name (abfd, asect), "SPU/%d/regs%n", &fd, &pos);
733 if (pos == 0)
734 return;
735
736 if (list->pos >= list->offset && list->pos + 4 <= list->offset + list->len)
737 {
738 store_unsigned_integer (list->buf + list->pos - list->offset,
739 4, byte_order, fd);
740 list->written += 4;
741 }
742 list->pos += 4;
743}
744
f6ac5f3d
PA
745enum target_xfer_status
746core_target::xfer_partial (enum target_object object, const char *annex,
747 gdb_byte *readbuf, const gdb_byte *writebuf,
748 ULONGEST offset, ULONGEST len, ULONGEST *xfered_len)
e2544d02
RM
749{
750 switch (object)
751 {
752 case TARGET_OBJECT_MEMORY:
15244507
PA
753 return (section_table_xfer_memory_partial
754 (readbuf, writebuf,
755 offset, len, xfered_len,
756 m_core_section_table.sections,
757 m_core_section_table.sections_end,
758 NULL));
e2544d02
RM
759
760 case TARGET_OBJECT_AUXV:
761 if (readbuf)
762 {
763 /* When the aux vector is stored in core file, BFD
764 represents this with a fake section called ".auxv". */
765
c4c5b7ba 766 struct bfd_section *section;
e2544d02 767 bfd_size_type size;
e2544d02
RM
768
769 section = bfd_get_section_by_name (core_bfd, ".auxv");
770 if (section == NULL)
2ed4b548 771 return TARGET_XFER_E_IO;
e2544d02
RM
772
773 size = bfd_section_size (core_bfd, section);
774 if (offset >= size)
9b409511 775 return TARGET_XFER_EOF;
e2544d02
RM
776 size -= offset;
777 if (size > len)
778 size = len;
9b409511
YQ
779
780 if (size == 0)
781 return TARGET_XFER_EOF;
782 if (!bfd_get_section_contents (core_bfd, section, readbuf,
783 (file_ptr) offset, size))
e2544d02 784 {
8a3fe4f8 785 warning (_("Couldn't read NT_AUXV note in core file."));
2ed4b548 786 return TARGET_XFER_E_IO;
e2544d02
RM
787 }
788
9b409511
YQ
789 *xfered_len = (ULONGEST) size;
790 return TARGET_XFER_OK;
e2544d02 791 }
2ed4b548 792 return TARGET_XFER_E_IO;
e2544d02 793
403e1656
MK
794 case TARGET_OBJECT_WCOOKIE:
795 if (readbuf)
796 {
797 /* When the StackGhost cookie is stored in core file, BFD
aff410f1
MS
798 represents this with a fake section called
799 ".wcookie". */
403e1656
MK
800
801 struct bfd_section *section;
802 bfd_size_type size;
403e1656
MK
803
804 section = bfd_get_section_by_name (core_bfd, ".wcookie");
805 if (section == NULL)
2ed4b548 806 return TARGET_XFER_E_IO;
403e1656
MK
807
808 size = bfd_section_size (core_bfd, section);
809 if (offset >= size)
96c4f946 810 return TARGET_XFER_EOF;
403e1656
MK
811 size -= offset;
812 if (size > len)
813 size = len;
9b409511
YQ
814
815 if (size == 0)
816 return TARGET_XFER_EOF;
817 if (!bfd_get_section_contents (core_bfd, section, readbuf,
818 (file_ptr) offset, size))
403e1656 819 {
8a3fe4f8 820 warning (_("Couldn't read StackGhost cookie in core file."));
2ed4b548 821 return TARGET_XFER_E_IO;
403e1656
MK
822 }
823
9b409511
YQ
824 *xfered_len = (ULONGEST) size;
825 return TARGET_XFER_OK;
826
403e1656 827 }
2ed4b548 828 return TARGET_XFER_E_IO;
403e1656 829
de584861 830 case TARGET_OBJECT_LIBRARIES:
15244507
PA
831 if (m_core_gdbarch != nullptr
832 && gdbarch_core_xfer_shared_libraries_p (m_core_gdbarch))
de584861
PA
833 {
834 if (writebuf)
2ed4b548 835 return TARGET_XFER_E_IO;
9b409511
YQ
836 else
837 {
15244507 838 *xfered_len = gdbarch_core_xfer_shared_libraries (m_core_gdbarch,
9b409511
YQ
839 readbuf,
840 offset, len);
841
842 if (*xfered_len == 0)
843 return TARGET_XFER_EOF;
844 else
845 return TARGET_XFER_OK;
846 }
de584861
PA
847 }
848 /* FALL THROUGH */
849
356a5233 850 case TARGET_OBJECT_LIBRARIES_AIX:
15244507
PA
851 if (m_core_gdbarch != nullptr
852 && gdbarch_core_xfer_shared_libraries_aix_p (m_core_gdbarch))
356a5233
JB
853 {
854 if (writebuf)
2ed4b548 855 return TARGET_XFER_E_IO;
9b409511
YQ
856 else
857 {
858 *xfered_len
15244507 859 = gdbarch_core_xfer_shared_libraries_aix (m_core_gdbarch,
9b409511
YQ
860 readbuf, offset,
861 len);
862
863 if (*xfered_len == 0)
864 return TARGET_XFER_EOF;
865 else
866 return TARGET_XFER_OK;
867 }
356a5233
JB
868 }
869 /* FALL THROUGH */
870
efcbbd14
UW
871 case TARGET_OBJECT_SPU:
872 if (readbuf && annex)
873 {
874 /* When the SPU contexts are stored in a core file, BFD
aff410f1
MS
875 represents this with a fake section called
876 "SPU/<annex>". */
efcbbd14
UW
877
878 struct bfd_section *section;
879 bfd_size_type size;
efcbbd14 880 char sectionstr[100];
c5504eaf 881
efcbbd14
UW
882 xsnprintf (sectionstr, sizeof sectionstr, "SPU/%s", annex);
883
884 section = bfd_get_section_by_name (core_bfd, sectionstr);
885 if (section == NULL)
2ed4b548 886 return TARGET_XFER_E_IO;
efcbbd14
UW
887
888 size = bfd_section_size (core_bfd, section);
889 if (offset >= size)
9b409511 890 return TARGET_XFER_EOF;
efcbbd14
UW
891 size -= offset;
892 if (size > len)
893 size = len;
9b409511
YQ
894
895 if (size == 0)
896 return TARGET_XFER_EOF;
897 if (!bfd_get_section_contents (core_bfd, section, readbuf,
898 (file_ptr) offset, size))
efcbbd14
UW
899 {
900 warning (_("Couldn't read SPU section in core file."));
2ed4b548 901 return TARGET_XFER_E_IO;
efcbbd14
UW
902 }
903
9b409511
YQ
904 *xfered_len = (ULONGEST) size;
905 return TARGET_XFER_OK;
efcbbd14
UW
906 }
907 else if (readbuf)
908 {
909 /* NULL annex requests list of all present spuids. */
910 struct spuid_list list;
c5504eaf 911
efcbbd14
UW
912 list.buf = readbuf;
913 list.offset = offset;
914 list.len = len;
915 list.pos = 0;
916 list.written = 0;
917 bfd_map_over_sections (core_bfd, add_to_spuid_list, &list);
9b409511
YQ
918
919 if (list.written == 0)
920 return TARGET_XFER_EOF;
921 else
922 {
923 *xfered_len = (ULONGEST) list.written;
924 return TARGET_XFER_OK;
925 }
efcbbd14 926 }
2ed4b548 927 return TARGET_XFER_E_IO;
efcbbd14 928
9015683b
TT
929 case TARGET_OBJECT_SIGNAL_INFO:
930 if (readbuf)
9b409511 931 {
15244507
PA
932 if (m_core_gdbarch != nullptr
933 && gdbarch_core_xfer_siginfo_p (m_core_gdbarch))
9b409511 934 {
15244507 935 LONGEST l = gdbarch_core_xfer_siginfo (m_core_gdbarch, readbuf,
382b69bb
JB
936 offset, len);
937
938 if (l >= 0)
939 {
940 *xfered_len = l;
941 if (l == 0)
942 return TARGET_XFER_EOF;
943 else
944 return TARGET_XFER_OK;
945 }
9b409511
YQ
946 }
947 }
2ed4b548 948 return TARGET_XFER_E_IO;
9015683b 949
e2544d02 950 default:
b6a8c27b
PA
951 return this->beneath ()->xfer_partial (object, annex, readbuf,
952 writebuf, offset, len,
953 xfered_len);
e2544d02
RM
954 }
955}
956
c906108c 957\f
c906108c
SS
958
959/* Okay, let's be honest: threads gleaned from a core file aren't
960 exactly lively, are they? On the other hand, if we don't claim
961 that each & every one is alive, then we don't get any of them
962 to appear in an "info thread" command, which is quite a useful
963 behaviour.
c5aa993b 964 */
57810aa7 965bool
f6ac5f3d 966core_target::thread_alive (ptid_t ptid)
c906108c 967{
57810aa7 968 return true;
c906108c
SS
969}
970
4eb0ad19
DJ
971/* Ask the current architecture what it knows about this core file.
972 That will be used, in turn, to pick a better architecture. This
973 wrapper could be avoided if targets got a chance to specialize
15244507 974 core_target. */
4eb0ad19 975
f6ac5f3d
PA
976const struct target_desc *
977core_target::read_description ()
4eb0ad19 978{
15244507 979 if (m_core_gdbarch && gdbarch_core_read_description_p (m_core_gdbarch))
2117c711
TT
980 {
981 const struct target_desc *result;
982
15244507 983 result = gdbarch_core_read_description (m_core_gdbarch, this, core_bfd);
2117c711
TT
984 if (result != NULL)
985 return result;
986 }
4eb0ad19 987
b6a8c27b 988 return this->beneath ()->read_description ();
4eb0ad19
DJ
989}
990
f6ac5f3d
PA
991const char *
992core_target::pid_to_str (ptid_t ptid)
0de3b513
PA
993{
994 static char buf[64];
88f38a04 995 struct inferior *inf;
a5ee0f0c 996 int pid;
0de3b513 997
a5ee0f0c
PA
998 /* The preferred way is to have a gdbarch/OS specific
999 implementation. */
15244507
PA
1000 if (m_core_gdbarch != nullptr
1001 && gdbarch_core_pid_to_str_p (m_core_gdbarch))
1002 return gdbarch_core_pid_to_str (m_core_gdbarch, ptid);
c5504eaf 1003
a5ee0f0c
PA
1004 /* Otherwise, if we don't have one, we'll just fallback to
1005 "process", with normal_pid_to_str. */
28439f5e 1006
a5ee0f0c 1007 /* Try the LWPID field first. */
e38504b3 1008 pid = ptid.lwp ();
a5ee0f0c 1009 if (pid != 0)
f2907e49 1010 return normal_pid_to_str (ptid_t (pid));
a5ee0f0c
PA
1011
1012 /* Otherwise, this isn't a "threaded" core -- use the PID field, but
1013 only if it isn't a fake PID. */
c9657e70 1014 inf = find_inferior_ptid (ptid);
88f38a04 1015 if (inf != NULL && !inf->fake_pid_p)
a5ee0f0c 1016 return normal_pid_to_str (ptid);
0de3b513 1017
a5ee0f0c
PA
1018 /* No luck. We simply don't have a valid PID to print. */
1019 xsnprintf (buf, sizeof buf, "<main task>");
0de3b513
PA
1020 return buf;
1021}
1022
f6ac5f3d
PA
1023const char *
1024core_target::thread_name (struct thread_info *thr)
4dfc5dbc 1025{
15244507
PA
1026 if (m_core_gdbarch != nullptr
1027 && gdbarch_core_thread_name_p (m_core_gdbarch))
1028 return gdbarch_core_thread_name (m_core_gdbarch, thr);
4dfc5dbc
JB
1029 return NULL;
1030}
1031
57810aa7 1032bool
f6ac5f3d 1033core_target::has_memory ()
c35b1492
PA
1034{
1035 return (core_bfd != NULL);
1036}
1037
57810aa7 1038bool
f6ac5f3d 1039core_target::has_stack ()
c35b1492
PA
1040{
1041 return (core_bfd != NULL);
1042}
1043
57810aa7 1044bool
f6ac5f3d 1045core_target::has_registers ()
c35b1492
PA
1046{
1047 return (core_bfd != NULL);
1048}
1049
451b7c33
TT
1050/* Implement the to_info_proc method. */
1051
f6ac5f3d
PA
1052bool
1053core_target::info_proc (const char *args, enum info_proc_what request)
451b7c33
TT
1054{
1055 struct gdbarch *gdbarch = get_current_arch ();
1056
1057 /* Since this is the core file target, call the 'core_info_proc'
1058 method on gdbarch, not 'info_proc'. */
1059 if (gdbarch_core_info_proc_p (gdbarch))
1060 gdbarch_core_info_proc (gdbarch, args, request);
c906108c 1061
f6ac5f3d 1062 return true;
c906108c
SS
1063}
1064
c906108c 1065void
fba45db2 1066_initialize_corelow (void)
c906108c 1067{
d9f719f1 1068 add_target (core_target_info, core_target_open, filename_completer);
c906108c 1069}
This page took 1.585683 seconds and 4 git commands to generate.