gdb: Add maint set ignore-prologue-end-flag
[binutils-gdb.git] / gdb / corelow.c
1 /* Core dump and executable file functions below target vector, for GDB.
2
3 Copyright (C) 1986-2022 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
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
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
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.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include "defs.h"
21 #include "arch-utils.h"
22 #include <signal.h>
23 #include <fcntl.h>
24 #include "frame.h" /* required by inferior.h */
25 #include "inferior.h"
26 #include "infrun.h"
27 #include "symtab.h"
28 #include "command.h"
29 #include "bfd.h"
30 #include "target.h"
31 #include "process-stratum-target.h"
32 #include "gdbcore.h"
33 #include "gdbthread.h"
34 #include "regcache.h"
35 #include "regset.h"
36 #include "symfile.h"
37 #include "exec.h"
38 #include "readline/tilde.h"
39 #include "solib.h"
40 #include "solist.h"
41 #include "filenames.h"
42 #include "progspace.h"
43 #include "objfiles.h"
44 #include "gdb_bfd.h"
45 #include "completer.h"
46 #include "gdbsupport/filestuff.h"
47 #include "build-id.h"
48 #include "gdbsupport/pathstuff.h"
49 #include "gdbsupport/scoped_fd.h"
50 #include "debuginfod-support.h"
51 #include <unordered_map>
52 #include <unordered_set>
53 #include "gdbcmd.h"
54 #include "xml-tdesc.h"
55
56 #ifndef O_LARGEFILE
57 #define O_LARGEFILE 0
58 #endif
59
60 /* The core file target. */
61
62 static const target_info core_target_info = {
63 "core",
64 N_("Local core dump file"),
65 N_("Use a core file as a target.\n\
66 Specify the filename of the core file.")
67 };
68
69 class core_target final : public process_stratum_target
70 {
71 public:
72 core_target ();
73
74 const target_info &info () const override
75 { return core_target_info; }
76
77 void close () override;
78 void detach (inferior *, int) override;
79 void fetch_registers (struct regcache *, int) override;
80
81 enum target_xfer_status xfer_partial (enum target_object object,
82 const char *annex,
83 gdb_byte *readbuf,
84 const gdb_byte *writebuf,
85 ULONGEST offset, ULONGEST len,
86 ULONGEST *xfered_len) override;
87 void files_info () override;
88
89 bool thread_alive (ptid_t ptid) override;
90 const struct target_desc *read_description () override;
91
92 std::string pid_to_str (ptid_t) override;
93
94 const char *thread_name (struct thread_info *) override;
95
96 bool has_all_memory () override { return true; }
97 bool has_memory () override;
98 bool has_stack () override;
99 bool has_registers () override;
100 bool has_execution (inferior *inf) override { return false; }
101
102 bool info_proc (const char *, enum info_proc_what) override;
103
104 /* A few helpers. */
105
106 /* Getter, see variable definition. */
107 struct gdbarch *core_gdbarch ()
108 {
109 return m_core_gdbarch;
110 }
111
112 /* See definition. */
113 void get_core_register_section (struct regcache *regcache,
114 const struct regset *regset,
115 const char *name,
116 int section_min_size,
117 const char *human_name,
118 bool required);
119
120 /* See definition. */
121 void info_proc_mappings (struct gdbarch *gdbarch);
122
123 private: /* per-core data */
124
125 /* The core's section table. Note that these target sections are
126 *not* mapped in the current address spaces' set of target
127 sections --- those should come only from pure executable or
128 shared library bfds. The core bfd sections are an implementation
129 detail of the core target, just like ptrace is for unix child
130 targets. */
131 target_section_table m_core_section_table;
132
133 /* File-backed address space mappings: some core files include
134 information about memory mapped files. */
135 target_section_table m_core_file_mappings;
136
137 /* Unavailable mappings. These correspond to pathnames which either
138 weren't found or could not be opened. Knowing these addresses can
139 still be useful. */
140 std::vector<mem_range> m_core_unavailable_mappings;
141
142 /* Build m_core_file_mappings. Called from the constructor. */
143 void build_file_mappings ();
144
145 /* Helper method for xfer_partial. */
146 enum target_xfer_status xfer_memory_via_mappings (gdb_byte *readbuf,
147 const gdb_byte *writebuf,
148 ULONGEST offset,
149 ULONGEST len,
150 ULONGEST *xfered_len);
151
152 /* FIXME: kettenis/20031023: Eventually this field should
153 disappear. */
154 struct gdbarch *m_core_gdbarch = NULL;
155 };
156
157 core_target::core_target ()
158 {
159 /* Find a first arch based on the BFD. We need the initial gdbarch so
160 we can setup the hooks to find a target description. */
161 m_core_gdbarch = gdbarch_from_bfd (core_bfd);
162
163 /* If the arch is able to read a target description from the core, it
164 could yield a more specific gdbarch. */
165 const struct target_desc *tdesc = read_description ();
166
167 if (tdesc != nullptr)
168 {
169 struct gdbarch_info info;
170 info.abfd = core_bfd;
171 info.target_desc = tdesc;
172 m_core_gdbarch = gdbarch_find_by_info (info);
173 }
174
175 if (!m_core_gdbarch
176 || !gdbarch_iterate_over_regset_sections_p (m_core_gdbarch))
177 error (_("\"%s\": Core file format not supported"),
178 bfd_get_filename (core_bfd));
179
180 /* Find the data section */
181 m_core_section_table = build_section_table (core_bfd);
182
183 build_file_mappings ();
184 }
185
186 /* Construct the target_section_table for file-backed mappings if
187 they exist.
188
189 For each unique path in the note, we'll open a BFD with a bfd
190 target of "binary". This is an unstructured bfd target upon which
191 we'll impose a structure from the mappings in the architecture-specific
192 mappings note. A BFD section is allocated and initialized for each
193 file-backed mapping.
194
195 We take care to not share already open bfds with other parts of
196 GDB; in particular, we don't want to add new sections to existing
197 BFDs. We do, however, ensure that the BFDs that we allocate here
198 will go away (be deallocated) when the core target is detached. */
199
200 void
201 core_target::build_file_mappings ()
202 {
203 std::unordered_map<std::string, struct bfd *> bfd_map;
204 std::unordered_set<std::string> unavailable_paths;
205
206 /* See linux_read_core_file_mappings() in linux-tdep.c for an example
207 read_core_file_mappings method. */
208 gdbarch_read_core_file_mappings (m_core_gdbarch, core_bfd,
209
210 /* After determining the number of mappings, read_core_file_mappings
211 will invoke this lambda. */
212 [&] (ULONGEST)
213 {
214 },
215
216 /* read_core_file_mappings will invoke this lambda for each mapping
217 that it finds. */
218 [&] (int num, ULONGEST start, ULONGEST end, ULONGEST file_ofs,
219 const char *filename, const bfd_build_id *build_id)
220 {
221 /* Architecture-specific read_core_mapping methods are expected to
222 weed out non-file-backed mappings. */
223 gdb_assert (filename != nullptr);
224
225 struct bfd *bfd = bfd_map[filename];
226 if (bfd == nullptr)
227 {
228 /* Use exec_file_find() to do sysroot expansion. It'll
229 also strip the potential sysroot "target:" prefix. If
230 there is no sysroot, an equivalent (possibly more
231 canonical) pathname will be provided. */
232 gdb::unique_xmalloc_ptr<char> expanded_fname
233 = exec_file_find (filename, NULL);
234
235 if (expanded_fname == nullptr && build_id != nullptr)
236 debuginfod_exec_query (build_id->data, build_id->size,
237 filename, &expanded_fname);
238
239 if (expanded_fname == nullptr)
240 {
241 m_core_unavailable_mappings.emplace_back (start, end - start);
242 /* Print just one warning per path. */
243 if (unavailable_paths.insert (filename).second)
244 warning (_("Can't open file %s during file-backed mapping "
245 "note processing"),
246 filename);
247 return;
248 }
249
250 bfd = bfd_map[filename] = bfd_openr (expanded_fname.get (),
251 "binary");
252
253 if (bfd == nullptr || !bfd_check_format (bfd, bfd_object))
254 {
255 m_core_unavailable_mappings.emplace_back (start, end - start);
256 /* If we get here, there's a good chance that it's due to
257 an internal error. We issue a warning instead of an
258 internal error because of the possibility that the
259 file was removed in between checking for its
260 existence during the expansion in exec_file_find()
261 and the calls to bfd_openr() / bfd_check_format().
262 Output both the path from the core file note along
263 with its expansion to make debugging this problem
264 easier. */
265 warning (_("Can't open file %s which was expanded to %s "
266 "during file-backed mapping note processing"),
267 filename, expanded_fname.get ());
268 if (bfd != nullptr)
269 bfd_close (bfd);
270 return;
271 }
272 /* Ensure that the bfd will be closed when core_bfd is closed.
273 This can be checked before/after a core file detach via
274 "maint info bfds". */
275 gdb_bfd_record_inclusion (core_bfd, bfd);
276 }
277
278 /* Make new BFD section. All sections have the same name,
279 which is permitted by bfd_make_section_anyway(). */
280 asection *sec = bfd_make_section_anyway (bfd, "load");
281 if (sec == nullptr)
282 error (_("Can't make section"));
283 sec->filepos = file_ofs;
284 bfd_set_section_flags (sec, SEC_READONLY | SEC_HAS_CONTENTS);
285 bfd_set_section_size (sec, end - start);
286 bfd_set_section_vma (sec, start);
287 bfd_set_section_lma (sec, start);
288 bfd_set_section_alignment (sec, 2);
289
290 /* Set target_section fields. */
291 m_core_file_mappings.emplace_back (start, end, sec);
292
293 /* If this is a bfd of a shared library, record its soname
294 and build id. */
295 if (build_id != nullptr)
296 {
297 gdb::unique_xmalloc_ptr<char> soname
298 = gdb_bfd_read_elf_soname (bfd->filename);
299 if (soname != nullptr)
300 set_cbfd_soname_build_id (current_program_space->cbfd,
301 soname.get (), build_id);
302 }
303 });
304
305 normalize_mem_ranges (&m_core_unavailable_mappings);
306 }
307
308 /* An arbitrary identifier for the core inferior. */
309 #define CORELOW_PID 1
310
311 /* Close the core target. */
312
313 void
314 core_target::close ()
315 {
316 if (core_bfd)
317 {
318 switch_to_no_thread (); /* Avoid confusion from thread
319 stuff. */
320 exit_inferior_silent (current_inferior ());
321
322 /* Clear out solib state while the bfd is still open. See
323 comments in clear_solib in solib.c. */
324 clear_solib ();
325
326 current_program_space->cbfd.reset (nullptr);
327 }
328
329 /* Core targets are heap-allocated (see core_target_open), so here
330 we delete ourselves. */
331 delete this;
332 }
333
334 /* Look for sections whose names start with `.reg/' so that we can
335 extract the list of threads in a core file. */
336
337 static void
338 add_to_thread_list (asection *asect, asection *reg_sect)
339 {
340 int core_tid;
341 int pid, lwpid;
342 bool fake_pid_p = false;
343 struct inferior *inf;
344
345 if (!startswith (bfd_section_name (asect), ".reg/"))
346 return;
347
348 core_tid = atoi (bfd_section_name (asect) + 5);
349
350 pid = bfd_core_file_pid (core_bfd);
351 if (pid == 0)
352 {
353 fake_pid_p = true;
354 pid = CORELOW_PID;
355 }
356
357 lwpid = core_tid;
358
359 inf = current_inferior ();
360 if (inf->pid == 0)
361 {
362 inferior_appeared (inf, pid);
363 inf->fake_pid_p = fake_pid_p;
364 }
365
366 ptid_t ptid (pid, lwpid);
367
368 thread_info *thr = add_thread (inf->process_target (), ptid);
369
370 /* Warning, Will Robinson, looking at BFD private data! */
371
372 if (reg_sect != NULL
373 && asect->filepos == reg_sect->filepos) /* Did we find .reg? */
374 switch_to_thread (thr); /* Yes, make it current. */
375 }
376
377 /* Issue a message saying we have no core to debug, if FROM_TTY. */
378
379 static void
380 maybe_say_no_core_file_now (int from_tty)
381 {
382 if (from_tty)
383 gdb_printf (_("No core file now.\n"));
384 }
385
386 /* Backward compatibility with old way of specifying core files. */
387
388 void
389 core_file_command (const char *filename, int from_tty)
390 {
391 dont_repeat (); /* Either way, seems bogus. */
392
393 if (filename == NULL)
394 {
395 if (core_bfd != NULL)
396 {
397 target_detach (current_inferior (), from_tty);
398 gdb_assert (core_bfd == NULL);
399 }
400 else
401 maybe_say_no_core_file_now (from_tty);
402 }
403 else
404 core_target_open (filename, from_tty);
405 }
406
407 /* Locate (and load) an executable file (and symbols) given the core file
408 BFD ABFD. */
409
410 static void
411 locate_exec_from_corefile_build_id (bfd *abfd, int from_tty)
412 {
413 const bfd_build_id *build_id = build_id_bfd_get (abfd);
414 if (build_id == nullptr)
415 return;
416
417 gdb_bfd_ref_ptr execbfd
418 = build_id_to_exec_bfd (build_id->size, build_id->data);
419
420 if (execbfd == nullptr)
421 {
422 /* Attempt to query debuginfod for the executable. */
423 gdb::unique_xmalloc_ptr<char> execpath;
424 scoped_fd fd = debuginfod_exec_query (build_id->data, build_id->size,
425 abfd->filename, &execpath);
426
427 if (fd.get () >= 0)
428 {
429 execbfd = gdb_bfd_open (execpath.get (), gnutarget);
430
431 if (execbfd == nullptr)
432 warning (_("\"%s\" from debuginfod cannot be opened as bfd: %s"),
433 execpath.get (),
434 gdb_bfd_errmsg (bfd_get_error (), nullptr).c_str ());
435 else if (!build_id_verify (execbfd.get (), build_id->size,
436 build_id->data))
437 execbfd.reset (nullptr);
438 }
439 }
440
441 if (execbfd != nullptr)
442 {
443 exec_file_attach (bfd_get_filename (execbfd.get ()), from_tty);
444 symbol_file_add_main (bfd_get_filename (execbfd.get ()),
445 symfile_add_flag (from_tty ? SYMFILE_VERBOSE : 0));
446 }
447 }
448
449 /* See gdbcore.h. */
450
451 void
452 core_target_open (const char *arg, int from_tty)
453 {
454 const char *p;
455 int siggy;
456 int scratch_chan;
457 int flags;
458
459 target_preopen (from_tty);
460 if (!arg)
461 {
462 if (core_bfd)
463 error (_("No core file specified. (Use `detach' "
464 "to stop debugging a core file.)"));
465 else
466 error (_("No core file specified."));
467 }
468
469 gdb::unique_xmalloc_ptr<char> filename (tilde_expand (arg));
470 if (strlen (filename.get ()) != 0
471 && !IS_ABSOLUTE_PATH (filename.get ()))
472 filename = gdb_abspath (filename.get ());
473
474 flags = O_BINARY | O_LARGEFILE;
475 if (write_files)
476 flags |= O_RDWR;
477 else
478 flags |= O_RDONLY;
479 scratch_chan = gdb_open_cloexec (filename.get (), flags, 0).release ();
480 if (scratch_chan < 0)
481 perror_with_name (filename.get ());
482
483 gdb_bfd_ref_ptr temp_bfd (gdb_bfd_fopen (filename.get (), gnutarget,
484 write_files ? FOPEN_RUB : FOPEN_RB,
485 scratch_chan));
486 if (temp_bfd == NULL)
487 perror_with_name (filename.get ());
488
489 if (!bfd_check_format (temp_bfd.get (), bfd_core))
490 {
491 /* Do it after the err msg */
492 /* FIXME: should be checking for errors from bfd_close (for one
493 thing, on error it does not free all the storage associated
494 with the bfd). */
495 error (_("\"%s\" is not a core dump: %s"),
496 filename.get (), bfd_errmsg (bfd_get_error ()));
497 }
498
499 current_program_space->cbfd = std::move (temp_bfd);
500
501 core_target *target = new core_target ();
502
503 /* Own the target until it is successfully pushed. */
504 target_ops_up target_holder (target);
505
506 validate_files ();
507
508 /* If we have no exec file, try to set the architecture from the
509 core file. We don't do this unconditionally since an exec file
510 typically contains more information that helps us determine the
511 architecture than a core file. */
512 if (!current_program_space->exec_bfd ())
513 set_gdbarch_from_file (core_bfd);
514
515 current_inferior ()->push_target (std::move (target_holder));
516
517 switch_to_no_thread ();
518
519 /* Need to flush the register cache (and the frame cache) from a
520 previous debug session. If inferior_ptid ends up the same as the
521 last debug session --- e.g., b foo; run; gcore core1; step; gcore
522 core2; core core1; core core2 --- then there's potential for
523 get_current_regcache to return the cached regcache of the
524 previous session, and the frame cache being stale. */
525 registers_changed ();
526
527 /* Build up thread list from BFD sections, and possibly set the
528 current thread to the .reg/NN section matching the .reg
529 section. */
530 asection *reg_sect = bfd_get_section_by_name (core_bfd, ".reg");
531 for (asection *sect : gdb_bfd_sections (core_bfd))
532 add_to_thread_list (sect, reg_sect);
533
534 if (inferior_ptid == null_ptid)
535 {
536 /* Either we found no .reg/NN section, and hence we have a
537 non-threaded core (single-threaded, from gdb's perspective),
538 or for some reason add_to_thread_list couldn't determine
539 which was the "main" thread. The latter case shouldn't
540 usually happen, but we're dealing with input here, which can
541 always be broken in different ways. */
542 thread_info *thread = first_thread_of_inferior (current_inferior ());
543
544 if (thread == NULL)
545 {
546 inferior_appeared (current_inferior (), CORELOW_PID);
547 thread = add_thread_silent (target, ptid_t (CORELOW_PID));
548 }
549
550 switch_to_thread (thread);
551 }
552
553 if (current_program_space->exec_bfd () == nullptr)
554 locate_exec_from_corefile_build_id (core_bfd, from_tty);
555
556 post_create_inferior (from_tty);
557
558 /* Now go through the target stack looking for threads since there
559 may be a thread_stratum target loaded on top of target core by
560 now. The layer above should claim threads found in the BFD
561 sections. */
562 try
563 {
564 target_update_thread_list ();
565 }
566
567 catch (const gdb_exception_error &except)
568 {
569 exception_print (gdb_stderr, except);
570 }
571
572 p = bfd_core_file_failing_command (core_bfd);
573 if (p)
574 gdb_printf (_("Core was generated by `%s'.\n"), p);
575
576 /* Clearing any previous state of convenience variables. */
577 clear_exit_convenience_vars ();
578
579 siggy = bfd_core_file_failing_signal (core_bfd);
580 if (siggy > 0)
581 {
582 gdbarch *core_gdbarch = target->core_gdbarch ();
583
584 /* If we don't have a CORE_GDBARCH to work with, assume a native
585 core (map gdb_signal from host signals). If we do have
586 CORE_GDBARCH to work with, but no gdb_signal_from_target
587 implementation for that gdbarch, as a fallback measure,
588 assume the host signal mapping. It'll be correct for native
589 cores, but most likely incorrect for cross-cores. */
590 enum gdb_signal sig = (core_gdbarch != NULL
591 && gdbarch_gdb_signal_from_target_p (core_gdbarch)
592 ? gdbarch_gdb_signal_from_target (core_gdbarch,
593 siggy)
594 : gdb_signal_from_host (siggy));
595
596 gdb_printf (_("Program terminated with signal %s, %s"),
597 gdb_signal_to_name (sig), gdb_signal_to_string (sig));
598 if (gdbarch_report_signal_info_p (core_gdbarch))
599 gdbarch_report_signal_info (core_gdbarch, current_uiout, sig);
600 gdb_printf (_(".\n"));
601
602 /* Set the value of the internal variable $_exitsignal,
603 which holds the signal uncaught by the inferior. */
604 set_internalvar_integer (lookup_internalvar ("_exitsignal"),
605 siggy);
606 }
607
608 /* Fetch all registers from core file. */
609 target_fetch_registers (get_current_regcache (), -1);
610
611 /* Now, set up the frame cache, and print the top of stack. */
612 reinit_frame_cache ();
613 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
614
615 /* Current thread should be NUM 1 but the user does not know that.
616 If a program is single threaded gdb in general does not mention
617 anything about threads. That is why the test is >= 2. */
618 if (thread_count (target) >= 2)
619 {
620 try
621 {
622 thread_command (NULL, from_tty);
623 }
624 catch (const gdb_exception_error &except)
625 {
626 exception_print (gdb_stderr, except);
627 }
628 }
629 }
630
631 void
632 core_target::detach (inferior *inf, int from_tty)
633 {
634 /* Note that 'this' is dangling after this call. unpush_target
635 closes the target, and our close implementation deletes
636 'this'. */
637 inf->unpush_target (this);
638
639 /* Clear the register cache and the frame cache. */
640 registers_changed ();
641 reinit_frame_cache ();
642 maybe_say_no_core_file_now (from_tty);
643 }
644
645 /* Try to retrieve registers from a section in core_bfd, and supply
646 them to REGSET.
647
648 If ptid's lwp member is zero, do the single-threaded
649 thing: look for a section named NAME. If ptid's lwp
650 member is non-zero, do the multi-threaded thing: look for a section
651 named "NAME/LWP", where LWP is the shortest ASCII decimal
652 representation of ptid's lwp member.
653
654 HUMAN_NAME is a human-readable name for the kind of registers the
655 NAME section contains, for use in error messages.
656
657 If REQUIRED is true, print an error if the core file doesn't have a
658 section by the appropriate name. Otherwise, just do nothing. */
659
660 void
661 core_target::get_core_register_section (struct regcache *regcache,
662 const struct regset *regset,
663 const char *name,
664 int section_min_size,
665 const char *human_name,
666 bool required)
667 {
668 gdb_assert (regset != nullptr);
669
670 struct bfd_section *section;
671 bfd_size_type size;
672 bool variable_size_section = (regset->flags & REGSET_VARIABLE_SIZE);
673
674 thread_section_name section_name (name, regcache->ptid ());
675
676 section = bfd_get_section_by_name (core_bfd, section_name.c_str ());
677 if (! section)
678 {
679 if (required)
680 warning (_("Couldn't find %s registers in core file."),
681 human_name);
682 return;
683 }
684
685 size = bfd_section_size (section);
686 if (size < section_min_size)
687 {
688 warning (_("Section `%s' in core file too small."),
689 section_name.c_str ());
690 return;
691 }
692 if (size != section_min_size && !variable_size_section)
693 {
694 warning (_("Unexpected size of section `%s' in core file."),
695 section_name.c_str ());
696 }
697
698 gdb::byte_vector contents (size);
699 if (!bfd_get_section_contents (core_bfd, section, contents.data (),
700 (file_ptr) 0, size))
701 {
702 warning (_("Couldn't read %s registers from `%s' section in core file."),
703 human_name, section_name.c_str ());
704 return;
705 }
706
707 regset->supply_regset (regset, regcache, -1, contents.data (), size);
708 }
709
710 /* Data passed to gdbarch_iterate_over_regset_sections's callback. */
711 struct get_core_registers_cb_data
712 {
713 core_target *target;
714 struct regcache *regcache;
715 };
716
717 /* Callback for get_core_registers that handles a single core file
718 register note section. */
719
720 static void
721 get_core_registers_cb (const char *sect_name, int supply_size, int collect_size,
722 const struct regset *regset,
723 const char *human_name, void *cb_data)
724 {
725 gdb_assert (regset != nullptr);
726
727 auto *data = (get_core_registers_cb_data *) cb_data;
728 bool required = false;
729 bool variable_size_section = (regset->flags & REGSET_VARIABLE_SIZE);
730
731 if (!variable_size_section)
732 gdb_assert (supply_size == collect_size);
733
734 if (strcmp (sect_name, ".reg") == 0)
735 {
736 required = true;
737 if (human_name == NULL)
738 human_name = "general-purpose";
739 }
740 else if (strcmp (sect_name, ".reg2") == 0)
741 {
742 if (human_name == NULL)
743 human_name = "floating-point";
744 }
745
746 data->target->get_core_register_section (data->regcache, regset, sect_name,
747 supply_size, human_name, required);
748 }
749
750 /* Get the registers out of a core file. This is the machine-
751 independent part. Fetch_core_registers is the machine-dependent
752 part, typically implemented in the xm-file for each
753 architecture. */
754
755 /* We just get all the registers, so we don't use regno. */
756
757 void
758 core_target::fetch_registers (struct regcache *regcache, int regno)
759 {
760 if (!(m_core_gdbarch != nullptr
761 && gdbarch_iterate_over_regset_sections_p (m_core_gdbarch)))
762 {
763 gdb_printf (gdb_stderr,
764 "Can't fetch registers from this type of core file\n");
765 return;
766 }
767
768 struct gdbarch *gdbarch = regcache->arch ();
769 get_core_registers_cb_data data = { this, regcache };
770 gdbarch_iterate_over_regset_sections (gdbarch,
771 get_core_registers_cb,
772 (void *) &data, NULL);
773
774 /* Mark all registers not found in the core as unavailable. */
775 for (int i = 0; i < gdbarch_num_regs (regcache->arch ()); i++)
776 if (regcache->get_register_status (i) == REG_UNKNOWN)
777 regcache->raw_supply (i, NULL);
778 }
779
780 void
781 core_target::files_info ()
782 {
783 print_section_info (&m_core_section_table, core_bfd);
784 }
785 \f
786 /* Helper method for core_target::xfer_partial. */
787
788 enum target_xfer_status
789 core_target::xfer_memory_via_mappings (gdb_byte *readbuf,
790 const gdb_byte *writebuf,
791 ULONGEST offset, ULONGEST len,
792 ULONGEST *xfered_len)
793 {
794 enum target_xfer_status xfer_status;
795
796 xfer_status = (section_table_xfer_memory_partial
797 (readbuf, writebuf,
798 offset, len, xfered_len,
799 m_core_file_mappings));
800
801 if (xfer_status == TARGET_XFER_OK || m_core_unavailable_mappings.empty ())
802 return xfer_status;
803
804 /* There are instances - e.g. when debugging within a docker
805 container using the AUFS storage driver - where the pathnames
806 obtained from the note section are incorrect. Despite the path
807 being wrong, just knowing the start and end addresses of the
808 mappings is still useful; we can attempt an access of the file
809 stratum constrained to the address ranges corresponding to the
810 unavailable mappings. */
811
812 ULONGEST memaddr = offset;
813 ULONGEST memend = offset + len;
814
815 for (const auto &mr : m_core_unavailable_mappings)
816 {
817 if (address_in_mem_range (memaddr, &mr))
818 {
819 if (!address_in_mem_range (memend, &mr))
820 len = mr.start + mr.length - memaddr;
821
822 xfer_status = this->beneath ()->xfer_partial (TARGET_OBJECT_MEMORY,
823 NULL,
824 readbuf,
825 writebuf,
826 offset,
827 len,
828 xfered_len);
829 break;
830 }
831 }
832
833 return xfer_status;
834 }
835
836 enum target_xfer_status
837 core_target::xfer_partial (enum target_object object, const char *annex,
838 gdb_byte *readbuf, const gdb_byte *writebuf,
839 ULONGEST offset, ULONGEST len, ULONGEST *xfered_len)
840 {
841 switch (object)
842 {
843 case TARGET_OBJECT_MEMORY:
844 {
845 enum target_xfer_status xfer_status;
846
847 /* Try accessing memory contents from core file data,
848 restricting consideration to those sections for which
849 the BFD section flag SEC_HAS_CONTENTS is set. */
850 auto has_contents_cb = [] (const struct target_section *s)
851 {
852 return ((s->the_bfd_section->flags & SEC_HAS_CONTENTS) != 0);
853 };
854 xfer_status = section_table_xfer_memory_partial
855 (readbuf, writebuf,
856 offset, len, xfered_len,
857 m_core_section_table,
858 has_contents_cb);
859 if (xfer_status == TARGET_XFER_OK)
860 return TARGET_XFER_OK;
861
862 /* Check file backed mappings. If they're available, use
863 core file provided mappings (e.g. from .note.linuxcore.file
864 or the like) as this should provide a more accurate
865 result. If not, check the stratum beneath us, which should
866 be the file stratum.
867
868 We also check unavailable mappings due to Docker/AUFS driver
869 issues. */
870 if (!m_core_file_mappings.empty ()
871 || !m_core_unavailable_mappings.empty ())
872 {
873 xfer_status = xfer_memory_via_mappings (readbuf, writebuf, offset,
874 len, xfered_len);
875 }
876 else
877 xfer_status = this->beneath ()->xfer_partial (object, annex, readbuf,
878 writebuf, offset, len,
879 xfered_len);
880 if (xfer_status == TARGET_XFER_OK)
881 return TARGET_XFER_OK;
882
883 /* Finally, attempt to access data in core file sections with
884 no contents. These will typically read as all zero. */
885 auto no_contents_cb = [&] (const struct target_section *s)
886 {
887 return !has_contents_cb (s);
888 };
889 xfer_status = section_table_xfer_memory_partial
890 (readbuf, writebuf,
891 offset, len, xfered_len,
892 m_core_section_table,
893 no_contents_cb);
894
895 return xfer_status;
896 }
897 case TARGET_OBJECT_AUXV:
898 if (readbuf)
899 {
900 /* When the aux vector is stored in core file, BFD
901 represents this with a fake section called ".auxv". */
902
903 struct bfd_section *section;
904 bfd_size_type size;
905
906 section = bfd_get_section_by_name (core_bfd, ".auxv");
907 if (section == NULL)
908 return TARGET_XFER_E_IO;
909
910 size = bfd_section_size (section);
911 if (offset >= size)
912 return TARGET_XFER_EOF;
913 size -= offset;
914 if (size > len)
915 size = len;
916
917 if (size == 0)
918 return TARGET_XFER_EOF;
919 if (!bfd_get_section_contents (core_bfd, section, readbuf,
920 (file_ptr) offset, size))
921 {
922 warning (_("Couldn't read NT_AUXV note in core file."));
923 return TARGET_XFER_E_IO;
924 }
925
926 *xfered_len = (ULONGEST) size;
927 return TARGET_XFER_OK;
928 }
929 return TARGET_XFER_E_IO;
930
931 case TARGET_OBJECT_WCOOKIE:
932 if (readbuf)
933 {
934 /* When the StackGhost cookie is stored in core file, BFD
935 represents this with a fake section called
936 ".wcookie". */
937
938 struct bfd_section *section;
939 bfd_size_type size;
940
941 section = bfd_get_section_by_name (core_bfd, ".wcookie");
942 if (section == NULL)
943 return TARGET_XFER_E_IO;
944
945 size = bfd_section_size (section);
946 if (offset >= size)
947 return TARGET_XFER_EOF;
948 size -= offset;
949 if (size > len)
950 size = len;
951
952 if (size == 0)
953 return TARGET_XFER_EOF;
954 if (!bfd_get_section_contents (core_bfd, section, readbuf,
955 (file_ptr) offset, size))
956 {
957 warning (_("Couldn't read StackGhost cookie in core file."));
958 return TARGET_XFER_E_IO;
959 }
960
961 *xfered_len = (ULONGEST) size;
962 return TARGET_XFER_OK;
963
964 }
965 return TARGET_XFER_E_IO;
966
967 case TARGET_OBJECT_LIBRARIES:
968 if (m_core_gdbarch != nullptr
969 && gdbarch_core_xfer_shared_libraries_p (m_core_gdbarch))
970 {
971 if (writebuf)
972 return TARGET_XFER_E_IO;
973 else
974 {
975 *xfered_len = gdbarch_core_xfer_shared_libraries (m_core_gdbarch,
976 readbuf,
977 offset, len);
978
979 if (*xfered_len == 0)
980 return TARGET_XFER_EOF;
981 else
982 return TARGET_XFER_OK;
983 }
984 }
985 return TARGET_XFER_E_IO;
986
987 case TARGET_OBJECT_LIBRARIES_AIX:
988 if (m_core_gdbarch != nullptr
989 && gdbarch_core_xfer_shared_libraries_aix_p (m_core_gdbarch))
990 {
991 if (writebuf)
992 return TARGET_XFER_E_IO;
993 else
994 {
995 *xfered_len
996 = gdbarch_core_xfer_shared_libraries_aix (m_core_gdbarch,
997 readbuf, offset,
998 len);
999
1000 if (*xfered_len == 0)
1001 return TARGET_XFER_EOF;
1002 else
1003 return TARGET_XFER_OK;
1004 }
1005 }
1006 return TARGET_XFER_E_IO;
1007
1008 case TARGET_OBJECT_SIGNAL_INFO:
1009 if (readbuf)
1010 {
1011 if (m_core_gdbarch != nullptr
1012 && gdbarch_core_xfer_siginfo_p (m_core_gdbarch))
1013 {
1014 LONGEST l = gdbarch_core_xfer_siginfo (m_core_gdbarch, readbuf,
1015 offset, len);
1016
1017 if (l >= 0)
1018 {
1019 *xfered_len = l;
1020 if (l == 0)
1021 return TARGET_XFER_EOF;
1022 else
1023 return TARGET_XFER_OK;
1024 }
1025 }
1026 }
1027 return TARGET_XFER_E_IO;
1028
1029 default:
1030 return this->beneath ()->xfer_partial (object, annex, readbuf,
1031 writebuf, offset, len,
1032 xfered_len);
1033 }
1034 }
1035
1036 \f
1037
1038 /* Okay, let's be honest: threads gleaned from a core file aren't
1039 exactly lively, are they? On the other hand, if we don't claim
1040 that each & every one is alive, then we don't get any of them
1041 to appear in an "info thread" command, which is quite a useful
1042 behaviour.
1043 */
1044 bool
1045 core_target::thread_alive (ptid_t ptid)
1046 {
1047 return true;
1048 }
1049
1050 /* Ask the current architecture what it knows about this core file.
1051 That will be used, in turn, to pick a better architecture. This
1052 wrapper could be avoided if targets got a chance to specialize
1053 core_target. */
1054
1055 const struct target_desc *
1056 core_target::read_description ()
1057 {
1058 /* If the core file contains a target description note then we will use
1059 that in preference to anything else. */
1060 bfd_size_type tdesc_note_size = 0;
1061 struct bfd_section *tdesc_note_section
1062 = bfd_get_section_by_name (core_bfd, ".gdb-tdesc");
1063 if (tdesc_note_section != nullptr)
1064 tdesc_note_size = bfd_section_size (tdesc_note_section);
1065 if (tdesc_note_size > 0)
1066 {
1067 gdb::char_vector contents (tdesc_note_size + 1);
1068 if (bfd_get_section_contents (core_bfd, tdesc_note_section,
1069 contents.data (), (file_ptr) 0,
1070 tdesc_note_size))
1071 {
1072 /* Ensure we have a null terminator. */
1073 contents[tdesc_note_size] = '\0';
1074 const struct target_desc *result
1075 = string_read_description_xml (contents.data ());
1076 if (result != nullptr)
1077 return result;
1078 }
1079 }
1080
1081 if (m_core_gdbarch && gdbarch_core_read_description_p (m_core_gdbarch))
1082 {
1083 const struct target_desc *result;
1084
1085 result = gdbarch_core_read_description (m_core_gdbarch, this, core_bfd);
1086 if (result != NULL)
1087 return result;
1088 }
1089
1090 return this->beneath ()->read_description ();
1091 }
1092
1093 std::string
1094 core_target::pid_to_str (ptid_t ptid)
1095 {
1096 struct inferior *inf;
1097 int pid;
1098
1099 /* The preferred way is to have a gdbarch/OS specific
1100 implementation. */
1101 if (m_core_gdbarch != nullptr
1102 && gdbarch_core_pid_to_str_p (m_core_gdbarch))
1103 return gdbarch_core_pid_to_str (m_core_gdbarch, ptid);
1104
1105 /* Otherwise, if we don't have one, we'll just fallback to
1106 "process", with normal_pid_to_str. */
1107
1108 /* Try the LWPID field first. */
1109 pid = ptid.lwp ();
1110 if (pid != 0)
1111 return normal_pid_to_str (ptid_t (pid));
1112
1113 /* Otherwise, this isn't a "threaded" core -- use the PID field, but
1114 only if it isn't a fake PID. */
1115 inf = find_inferior_ptid (this, ptid);
1116 if (inf != NULL && !inf->fake_pid_p)
1117 return normal_pid_to_str (ptid);
1118
1119 /* No luck. We simply don't have a valid PID to print. */
1120 return "<main task>";
1121 }
1122
1123 const char *
1124 core_target::thread_name (struct thread_info *thr)
1125 {
1126 if (m_core_gdbarch != nullptr
1127 && gdbarch_core_thread_name_p (m_core_gdbarch))
1128 return gdbarch_core_thread_name (m_core_gdbarch, thr);
1129 return NULL;
1130 }
1131
1132 bool
1133 core_target::has_memory ()
1134 {
1135 return (core_bfd != NULL);
1136 }
1137
1138 bool
1139 core_target::has_stack ()
1140 {
1141 return (core_bfd != NULL);
1142 }
1143
1144 bool
1145 core_target::has_registers ()
1146 {
1147 return (core_bfd != NULL);
1148 }
1149
1150 /* Implement the to_info_proc method. */
1151
1152 bool
1153 core_target::info_proc (const char *args, enum info_proc_what request)
1154 {
1155 struct gdbarch *gdbarch = get_current_arch ();
1156
1157 /* Since this is the core file target, call the 'core_info_proc'
1158 method on gdbarch, not 'info_proc'. */
1159 if (gdbarch_core_info_proc_p (gdbarch))
1160 gdbarch_core_info_proc (gdbarch, args, request);
1161
1162 return true;
1163 }
1164
1165 /* Get a pointer to the current core target. If not connected to a
1166 core target, return NULL. */
1167
1168 static core_target *
1169 get_current_core_target ()
1170 {
1171 target_ops *proc_target = current_inferior ()->process_target ();
1172 return dynamic_cast<core_target *> (proc_target);
1173 }
1174
1175 /* Display file backed mappings from core file. */
1176
1177 void
1178 core_target::info_proc_mappings (struct gdbarch *gdbarch)
1179 {
1180 if (!m_core_file_mappings.empty ())
1181 {
1182 gdb_printf (_("Mapped address spaces:\n\n"));
1183 if (gdbarch_addr_bit (gdbarch) == 32)
1184 {
1185 gdb_printf ("\t%10s %10s %10s %10s %s\n",
1186 "Start Addr",
1187 " End Addr",
1188 " Size", " Offset", "objfile");
1189 }
1190 else
1191 {
1192 gdb_printf (" %18s %18s %10s %10s %s\n",
1193 "Start Addr",
1194 " End Addr",
1195 " Size", " Offset", "objfile");
1196 }
1197 }
1198
1199 for (const target_section &tsp : m_core_file_mappings)
1200 {
1201 ULONGEST start = tsp.addr;
1202 ULONGEST end = tsp.endaddr;
1203 ULONGEST file_ofs = tsp.the_bfd_section->filepos;
1204 const char *filename = bfd_get_filename (tsp.the_bfd_section->owner);
1205
1206 if (gdbarch_addr_bit (gdbarch) == 32)
1207 gdb_printf ("\t%10s %10s %10s %10s %s\n",
1208 paddress (gdbarch, start),
1209 paddress (gdbarch, end),
1210 hex_string (end - start),
1211 hex_string (file_ofs),
1212 filename);
1213 else
1214 gdb_printf (" %18s %18s %10s %10s %s\n",
1215 paddress (gdbarch, start),
1216 paddress (gdbarch, end),
1217 hex_string (end - start),
1218 hex_string (file_ofs),
1219 filename);
1220 }
1221 }
1222
1223 /* Implement "maintenance print core-file-backed-mappings" command.
1224
1225 If mappings are loaded, the results should be similar to the
1226 mappings shown by "info proc mappings". This command is mainly a
1227 debugging tool for GDB developers to make sure that the expected
1228 mappings are present after loading a core file. For Linux, the
1229 output provided by this command will be very similar (if not
1230 identical) to that provided by "info proc mappings". This is not
1231 necessarily the case for other OSes which might provide
1232 more/different information in the "info proc mappings" output. */
1233
1234 static void
1235 maintenance_print_core_file_backed_mappings (const char *args, int from_tty)
1236 {
1237 core_target *targ = get_current_core_target ();
1238 if (targ != nullptr)
1239 targ->info_proc_mappings (targ->core_gdbarch ());
1240 }
1241
1242 void _initialize_corelow ();
1243 void
1244 _initialize_corelow ()
1245 {
1246 add_target (core_target_info, core_target_open, filename_completer);
1247 add_cmd ("core-file-backed-mappings", class_maintenance,
1248 maintenance_print_core_file_backed_mappings,
1249 _("Print core file's file-backed mappings."),
1250 &maintenanceprintlist);
1251 }