1 /* Core dump and executable file functions below target vector, for GDB.
3 Copyright (C) 1986-2023 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
21 #include "arch-utils.h"
24 #include "frame.h" /* required by inferior.h */
31 #include "process-stratum-target.h"
33 #include "gdbthread.h"
38 #include "readline/tilde.h"
41 #include "filenames.h"
42 #include "progspace.h"
45 #include "completer.h"
46 #include "gdbsupport/filestuff.h"
48 #include "gdbsupport/pathstuff.h"
49 #include "gdbsupport/scoped_fd.h"
50 #include "gdbsupport/x86-xstate.h"
51 #include "debuginfod-support.h"
52 #include <unordered_map>
53 #include <unordered_set>
55 #include "xml-tdesc.h"
62 /* The core file target. */
64 static const target_info core_target_info
= {
66 N_("Local core dump file"),
67 N_("Use a core file as a target.\n\
68 Specify the filename of the core file.")
71 class core_target final
: public process_stratum_target
76 const target_info
&info () const override
77 { return core_target_info
; }
79 void close () override
;
80 void detach (inferior
*, int) override
;
81 void fetch_registers (struct regcache
*, int) override
;
83 enum target_xfer_status
xfer_partial (enum target_object object
,
86 const gdb_byte
*writebuf
,
87 ULONGEST offset
, ULONGEST len
,
88 ULONGEST
*xfered_len
) override
;
89 void files_info () override
;
91 bool thread_alive (ptid_t ptid
) override
;
92 const struct target_desc
*read_description () override
;
94 std::string
pid_to_str (ptid_t
) override
;
96 const char *thread_name (struct thread_info
*) override
;
98 bool has_all_memory () override
{ return true; }
99 bool has_memory () override
;
100 bool has_stack () override
;
101 bool has_registers () override
;
102 bool has_execution (inferior
*inf
) override
{ return false; }
104 bool info_proc (const char *, enum info_proc_what
) override
;
106 bool supports_memory_tagging () override
;
108 /* Core file implementation of fetch_memtags. Fetch the memory tags from
110 bool fetch_memtags (CORE_ADDR address
, size_t len
,
111 gdb::byte_vector
&tags
, int type
) override
;
113 x86_xsave_layout
fetch_x86_xsave_layout () override
;
117 /* Getter, see variable definition. */
118 struct gdbarch
*core_gdbarch ()
120 return m_core_gdbarch
;
123 /* See definition. */
124 void get_core_register_section (struct regcache
*regcache
,
125 const struct regset
*regset
,
127 int section_min_size
,
128 const char *human_name
,
131 /* See definition. */
132 void info_proc_mappings (struct gdbarch
*gdbarch
);
134 private: /* per-core data */
136 /* Get rid of the core inferior. */
139 /* The core's section table. Note that these target sections are
140 *not* mapped in the current address spaces' set of target
141 sections --- those should come only from pure executable or
142 shared library bfds. The core bfd sections are an implementation
143 detail of the core target, just like ptrace is for unix child
145 target_section_table m_core_section_table
;
147 /* File-backed address space mappings: some core files include
148 information about memory mapped files. */
149 target_section_table m_core_file_mappings
;
151 /* Unavailable mappings. These correspond to pathnames which either
152 weren't found or could not be opened. Knowing these addresses can
154 std::vector
<mem_range
> m_core_unavailable_mappings
;
156 /* Build m_core_file_mappings. Called from the constructor. */
157 void build_file_mappings ();
159 /* Helper method for xfer_partial. */
160 enum target_xfer_status
xfer_memory_via_mappings (gdb_byte
*readbuf
,
161 const gdb_byte
*writebuf
,
164 ULONGEST
*xfered_len
);
166 /* FIXME: kettenis/20031023: Eventually this field should
168 struct gdbarch
*m_core_gdbarch
= NULL
;
171 core_target::core_target ()
173 /* Find a first arch based on the BFD. We need the initial gdbarch so
174 we can setup the hooks to find a target description. */
175 m_core_gdbarch
= gdbarch_from_bfd (core_bfd
);
177 /* If the arch is able to read a target description from the core, it
178 could yield a more specific gdbarch. */
179 const struct target_desc
*tdesc
= read_description ();
181 if (tdesc
!= nullptr)
183 struct gdbarch_info info
;
184 info
.abfd
= core_bfd
;
185 info
.target_desc
= tdesc
;
186 m_core_gdbarch
= gdbarch_find_by_info (info
);
190 || !gdbarch_iterate_over_regset_sections_p (m_core_gdbarch
))
191 error (_("\"%s\": Core file format not supported"),
192 bfd_get_filename (core_bfd
));
194 /* Find the data section */
195 m_core_section_table
= build_section_table (core_bfd
);
197 build_file_mappings ();
200 /* Construct the target_section_table for file-backed mappings if
203 For each unique path in the note, we'll open a BFD with a bfd
204 target of "binary". This is an unstructured bfd target upon which
205 we'll impose a structure from the mappings in the architecture-specific
206 mappings note. A BFD section is allocated and initialized for each
209 We take care to not share already open bfds with other parts of
210 GDB; in particular, we don't want to add new sections to existing
211 BFDs. We do, however, ensure that the BFDs that we allocate here
212 will go away (be deallocated) when the core target is detached. */
215 core_target::build_file_mappings ()
217 std::unordered_map
<std::string
, struct bfd
*> bfd_map
;
218 std::unordered_set
<std::string
> unavailable_paths
;
220 /* See linux_read_core_file_mappings() in linux-tdep.c for an example
221 read_core_file_mappings method. */
222 gdbarch_read_core_file_mappings (m_core_gdbarch
, core_bfd
,
224 /* After determining the number of mappings, read_core_file_mappings
225 will invoke this lambda. */
230 /* read_core_file_mappings will invoke this lambda for each mapping
232 [&] (int num
, ULONGEST start
, ULONGEST end
, ULONGEST file_ofs
,
233 const char *filename
, const bfd_build_id
*build_id
)
235 /* Architecture-specific read_core_mapping methods are expected to
236 weed out non-file-backed mappings. */
237 gdb_assert (filename
!= nullptr);
239 if (unavailable_paths
.find (filename
) != unavailable_paths
.end ())
241 /* We have already seen some mapping for FILENAME but failed to
242 find/open the file. There is no point in trying the same
243 thing again so just record that the range [start, end) is
245 m_core_unavailable_mappings
.emplace_back (start
, end
- start
);
249 struct bfd
*bfd
= bfd_map
[filename
];
252 /* Use exec_file_find() to do sysroot expansion. It'll
253 also strip the potential sysroot "target:" prefix. If
254 there is no sysroot, an equivalent (possibly more
255 canonical) pathname will be provided. */
256 gdb::unique_xmalloc_ptr
<char> expanded_fname
257 = exec_file_find (filename
, NULL
);
259 if (expanded_fname
== nullptr && build_id
!= nullptr)
260 debuginfod_exec_query (build_id
->data
, build_id
->size
,
261 filename
, &expanded_fname
);
263 if (expanded_fname
== nullptr)
265 m_core_unavailable_mappings
.emplace_back (start
, end
- start
);
266 unavailable_paths
.insert (filename
);
267 warning (_("Can't open file %s during file-backed mapping "
273 bfd
= bfd_openr (expanded_fname
.get (), "binary");
275 if (bfd
== nullptr || !bfd_check_format (bfd
, bfd_object
))
277 m_core_unavailable_mappings
.emplace_back (start
, end
- start
);
278 unavailable_paths
.insert (filename
);
279 warning (_("Can't open file %s which was expanded to %s "
280 "during file-backed mapping note processing"),
281 filename
, expanded_fname
.get ());
287 /* Ensure that the bfd will be closed when core_bfd is closed.
288 This can be checked before/after a core file detach via
289 "maint info bfds". */
290 gdb_bfd_record_inclusion (core_bfd
, bfd
);
291 bfd_map
[filename
] = bfd
;
294 /* Make new BFD section. All sections have the same name,
295 which is permitted by bfd_make_section_anyway(). */
296 asection
*sec
= bfd_make_section_anyway (bfd
, "load");
298 error (_("Can't make section"));
299 sec
->filepos
= file_ofs
;
300 bfd_set_section_flags (sec
, SEC_READONLY
| SEC_HAS_CONTENTS
);
301 bfd_set_section_size (sec
, end
- start
);
302 bfd_set_section_vma (sec
, start
);
303 bfd_set_section_lma (sec
, start
);
304 bfd_set_section_alignment (sec
, 2);
306 /* Set target_section fields. */
307 m_core_file_mappings
.emplace_back (start
, end
, sec
);
309 /* If this is a bfd of a shared library, record its soname
311 if (build_id
!= nullptr)
313 gdb::unique_xmalloc_ptr
<char> soname
314 = gdb_bfd_read_elf_soname (bfd
->filename
);
315 if (soname
!= nullptr)
316 set_cbfd_soname_build_id (current_program_space
->cbfd
,
317 soname
.get (), build_id
);
321 normalize_mem_ranges (&m_core_unavailable_mappings
);
324 /* An arbitrary identifier for the core inferior. */
325 #define CORELOW_PID 1
328 core_target::clear_core ()
332 switch_to_no_thread (); /* Avoid confusion from thread
334 exit_inferior (current_inferior ());
336 /* Clear out solib state while the bfd is still open. See
337 comments in clear_solib in solib.c. */
340 current_program_space
->cbfd
.reset (nullptr);
344 /* Close the core target. */
347 core_target::close ()
351 /* Core targets are heap-allocated (see core_target_open), so here
352 we delete ourselves. */
356 /* Look for sections whose names start with `.reg/' so that we can
357 extract the list of threads in a core file. */
359 /* If ASECT is a section whose name begins with '.reg/' then extract the
360 lwpid after the '/' and create a new thread in INF.
362 If REG_SECT is not nullptr, and the both ASECT and REG_SECT point at the
363 same position in the parent bfd object then switch to the newly created
364 thread, otherwise, the selected thread is left unchanged. */
367 add_to_thread_list (asection
*asect
, asection
*reg_sect
, inferior
*inf
)
369 if (!startswith (bfd_section_name (asect
), ".reg/"))
372 int lwpid
= atoi (bfd_section_name (asect
) + 5);
373 ptid_t
ptid (inf
->pid
, lwpid
);
374 thread_info
*thr
= add_thread (inf
->process_target (), ptid
);
376 /* Warning, Will Robinson, looking at BFD private data! */
379 && asect
->filepos
== reg_sect
->filepos
) /* Did we find .reg? */
380 switch_to_thread (thr
); /* Yes, make it current. */
383 /* Issue a message saying we have no core to debug, if FROM_TTY. */
386 maybe_say_no_core_file_now (int from_tty
)
389 gdb_printf (_("No core file now.\n"));
392 /* Backward compatibility with old way of specifying core files. */
395 core_file_command (const char *filename
, int from_tty
)
397 dont_repeat (); /* Either way, seems bogus. */
399 if (filename
== NULL
)
401 if (core_bfd
!= NULL
)
403 target_detach (current_inferior (), from_tty
);
404 gdb_assert (core_bfd
== NULL
);
407 maybe_say_no_core_file_now (from_tty
);
410 core_target_open (filename
, from_tty
);
413 /* A vmcore file is a core file created by the Linux kernel at the point of
414 a crash. Each thread in the core file represents a real CPU core, and
415 the lwpid for each thread is the pid of the process that was running on
416 that core at the moment of the crash.
418 However, not every CPU core will have been running a process, some cores
419 will be idle. For these idle cores the CPU writes an lwpid of 0. And
420 of course, multiple cores might be idle, so there could be multiple
421 threads with an lwpid of 0.
423 The problem is GDB doesn't really like threads with an lwpid of 0; GDB
424 presents such a thread as a process rather than a thread. And GDB
425 certainly doesn't like multiple threads having the same lwpid, each time
426 a new thread is seen with the same lwpid the earlier thread (with the
427 same lwpid) will be deleted.
429 This function addresses both of these problems by assigning a fake lwpid
430 to any thread with an lwpid of 0.
432 GDB finds the lwpid information by looking at the bfd section names
433 which include the lwpid, e.g. .reg/NN where NN is the lwpid. This
434 function looks though all the section names looking for sections named
435 .reg/NN. If any sections are found where NN == 0, then we assign a new
436 unique value of NN. Then, in a second pass, any sections ending /0 are
437 assigned their new number.
439 Remember, a core file may contain multiple register sections for
440 different register sets, but the sets are always grouped by thread, so
441 we can figure out which registers should be assigned the same new
442 lwpid. For example, consider a core file containing:
444 .reg/0, .reg2/0, .reg/0, .reg2/0
446 This represents two threads, each thread contains a .reg and .reg2
447 register set. The .reg represents the start of each thread. After
448 renaming the sections will now look like this:
450 .reg/1, .reg2/1, .reg/2, .reg2/2
452 After calling this function the rest of the core file handling code can
453 treat this core file just like any other core file. */
456 rename_vmcore_idle_reg_sections (bfd
*abfd
, inferior
*inf
)
458 /* Map from the bfd section to its lwpid (the /NN number). */
459 std::vector
<std::pair
<asection
*, int>> sections_and_lwpids
;
461 /* The set of all /NN numbers found. Needed so we can easily find unused
462 numbers in the case that we need to rename some sections. */
463 std::unordered_set
<int> all_lwpids
;
465 /* A count of how many sections called .reg/0 we have found. */
466 unsigned zero_lwpid_count
= 0;
468 /* Look for all the .reg sections. Record the section object and the
469 lwpid which is extracted from the section name. Spot if any have an
471 for (asection
*sect
: gdb_bfd_sections (core_bfd
))
473 if (startswith (bfd_section_name (sect
), ".reg/"))
475 int lwpid
= atoi (bfd_section_name (sect
) + 5);
476 sections_and_lwpids
.emplace_back (sect
, lwpid
);
477 all_lwpids
.insert (lwpid
);
483 /* If every ".reg/NN" section has a non-zero lwpid then we don't need to
485 if (zero_lwpid_count
== 0)
488 /* Assign a new number to any .reg sections with an lwpid of 0. */
490 for (auto §_and_lwpid
: sections_and_lwpids
)
491 if (sect_and_lwpid
.second
== 0)
493 while (all_lwpids
.find (new_lwpid
) != all_lwpids
.end ())
495 sect_and_lwpid
.second
= new_lwpid
;
499 /* Now update the names of any sections with an lwpid of 0. This is
500 more than just the .reg sections we originally found. */
501 std::string replacement_lwpid_str
;
502 auto iter
= sections_and_lwpids
.begin ();
503 int replacement_lwpid
= 0;
504 for (asection
*sect
: gdb_bfd_sections (core_bfd
))
506 if (iter
!= sections_and_lwpids
.end () && sect
== iter
->first
)
508 gdb_assert (startswith (bfd_section_name (sect
), ".reg/"));
510 int lwpid
= atoi (bfd_section_name (sect
) + 5);
511 if (lwpid
== iter
->second
)
513 /* This section was not given a new number. */
514 gdb_assert (lwpid
!= 0);
515 replacement_lwpid
= 0;
519 replacement_lwpid
= iter
->second
;
520 ptid_t
ptid (inf
->pid
, replacement_lwpid
);
521 if (!replacement_lwpid_str
.empty ())
522 replacement_lwpid_str
+= ", ";
523 replacement_lwpid_str
+= target_pid_to_str (ptid
);
529 if (replacement_lwpid
!= 0)
531 const char *name
= bfd_section_name (sect
);
532 size_t len
= strlen (name
);
534 if (strncmp (name
+ len
- 2, "/0", 2) == 0)
536 /* This section needs a new name. */
538 = string_printf ("%.*s/%d",
539 static_cast<int> (len
- 2),
540 name
, replacement_lwpid
);
542 = static_cast<char *> (bfd_alloc (abfd
, name_str
.size () + 1));
543 if (name_buf
== nullptr)
544 error (_("failed to allocate space for section name '%s'"),
546 memcpy (name_buf
, name_str
.c_str(), name_str
.size () + 1);
547 bfd_rename_section (sect
, name_buf
);
552 if (zero_lwpid_count
== 1)
553 warning (_("found thread with pid 0, assigned replacement Target Id: %s"),
554 replacement_lwpid_str
.c_str ());
556 warning (_("found threads with pid 0, assigned replacement Target Ids: %s"),
557 replacement_lwpid_str
.c_str ());
560 /* Locate (and load) an executable file (and symbols) given the core file
564 locate_exec_from_corefile_build_id (bfd
*abfd
, int from_tty
)
566 const bfd_build_id
*build_id
= build_id_bfd_get (abfd
);
567 if (build_id
== nullptr)
570 gdb_bfd_ref_ptr execbfd
571 = build_id_to_exec_bfd (build_id
->size
, build_id
->data
);
573 if (execbfd
== nullptr)
575 /* Attempt to query debuginfod for the executable. */
576 gdb::unique_xmalloc_ptr
<char> execpath
;
577 scoped_fd fd
= debuginfod_exec_query (build_id
->data
, build_id
->size
,
578 abfd
->filename
, &execpath
);
582 execbfd
= gdb_bfd_open (execpath
.get (), gnutarget
);
584 if (execbfd
== nullptr)
585 warning (_("\"%s\" from debuginfod cannot be opened as bfd: %s"),
587 gdb_bfd_errmsg (bfd_get_error (), nullptr).c_str ());
588 else if (!build_id_verify (execbfd
.get (), build_id
->size
,
590 execbfd
.reset (nullptr);
594 if (execbfd
!= nullptr)
596 exec_file_attach (bfd_get_filename (execbfd
.get ()), from_tty
);
597 symbol_file_add_main (bfd_get_filename (execbfd
.get ()),
598 symfile_add_flag (from_tty
? SYMFILE_VERBOSE
: 0));
605 core_target_open (const char *arg
, int from_tty
)
612 target_preopen (from_tty
);
616 error (_("No core file specified. (Use `detach' "
617 "to stop debugging a core file.)"));
619 error (_("No core file specified."));
622 gdb::unique_xmalloc_ptr
<char> filename (tilde_expand (arg
));
623 if (strlen (filename
.get ()) != 0
624 && !IS_ABSOLUTE_PATH (filename
.get ()))
625 filename
= make_unique_xstrdup (gdb_abspath (filename
.get ()).c_str ());
627 flags
= O_BINARY
| O_LARGEFILE
;
632 scratch_chan
= gdb_open_cloexec (filename
.get (), flags
, 0).release ();
633 if (scratch_chan
< 0)
634 perror_with_name (filename
.get ());
636 gdb_bfd_ref_ptr
temp_bfd (gdb_bfd_fopen (filename
.get (), gnutarget
,
637 write_files
? FOPEN_RUB
: FOPEN_RB
,
639 if (temp_bfd
== NULL
)
640 perror_with_name (filename
.get ());
642 if (!bfd_check_format (temp_bfd
.get (), bfd_core
))
644 /* Do it after the err msg */
645 /* FIXME: should be checking for errors from bfd_close (for one
646 thing, on error it does not free all the storage associated
648 error (_("\"%s\" is not a core dump: %s"),
649 filename
.get (), bfd_errmsg (bfd_get_error ()));
652 current_program_space
->cbfd
= std::move (temp_bfd
);
654 core_target
*target
= new core_target ();
656 /* Own the target until it is successfully pushed. */
657 target_ops_up
target_holder (target
);
661 /* If we have no exec file, try to set the architecture from the
662 core file. We don't do this unconditionally since an exec file
663 typically contains more information that helps us determine the
664 architecture than a core file. */
665 if (!current_program_space
->exec_bfd ())
666 set_gdbarch_from_file (core_bfd
);
668 current_inferior ()->push_target (std::move (target_holder
));
670 switch_to_no_thread ();
672 /* Need to flush the register cache (and the frame cache) from a
673 previous debug session. If inferior_ptid ends up the same as the
674 last debug session --- e.g., b foo; run; gcore core1; step; gcore
675 core2; core core1; core core2 --- then there's potential for
676 get_current_regcache to return the cached regcache of the
677 previous session, and the frame cache being stale. */
678 registers_changed ();
680 /* Find (or fake) the pid for the process in this core file, and
681 initialise the current inferior with that pid. */
682 bool fake_pid_p
= false;
683 int pid
= bfd_core_file_pid (core_bfd
);
690 inferior
*inf
= current_inferior ();
691 gdb_assert (inf
->pid
== 0);
692 inferior_appeared (inf
, pid
);
693 inf
->fake_pid_p
= fake_pid_p
;
695 /* Rename any .reg/0 sections, giving them each a fake lwpid. */
696 rename_vmcore_idle_reg_sections (core_bfd
, inf
);
698 /* Build up thread list from BFD sections, and possibly set the
699 current thread to the .reg/NN section matching the .reg
701 asection
*reg_sect
= bfd_get_section_by_name (core_bfd
, ".reg");
702 for (asection
*sect
: gdb_bfd_sections (core_bfd
))
703 add_to_thread_list (sect
, reg_sect
, inf
);
705 if (inferior_ptid
== null_ptid
)
707 /* Either we found no .reg/NN section, and hence we have a
708 non-threaded core (single-threaded, from gdb's perspective),
709 or for some reason add_to_thread_list couldn't determine
710 which was the "main" thread. The latter case shouldn't
711 usually happen, but we're dealing with input here, which can
712 always be broken in different ways. */
713 thread_info
*thread
= first_thread_of_inferior (inf
);
716 thread
= add_thread_silent (target
, ptid_t (CORELOW_PID
));
718 switch_to_thread (thread
);
721 if (current_program_space
->exec_bfd () == nullptr)
722 locate_exec_from_corefile_build_id (core_bfd
, from_tty
);
724 post_create_inferior (from_tty
);
726 /* Now go through the target stack looking for threads since there
727 may be a thread_stratum target loaded on top of target core by
728 now. The layer above should claim threads found in the BFD
732 target_update_thread_list ();
735 catch (const gdb_exception_error
&except
)
737 exception_print (gdb_stderr
, except
);
740 p
= bfd_core_file_failing_command (core_bfd
);
742 gdb_printf (_("Core was generated by `%s'.\n"), p
);
744 /* Clearing any previous state of convenience variables. */
745 clear_exit_convenience_vars ();
747 siggy
= bfd_core_file_failing_signal (core_bfd
);
750 gdbarch
*core_gdbarch
= target
->core_gdbarch ();
752 /* If we don't have a CORE_GDBARCH to work with, assume a native
753 core (map gdb_signal from host signals). If we do have
754 CORE_GDBARCH to work with, but no gdb_signal_from_target
755 implementation for that gdbarch, as a fallback measure,
756 assume the host signal mapping. It'll be correct for native
757 cores, but most likely incorrect for cross-cores. */
758 enum gdb_signal sig
= (core_gdbarch
!= NULL
759 && gdbarch_gdb_signal_from_target_p (core_gdbarch
)
760 ? gdbarch_gdb_signal_from_target (core_gdbarch
,
762 : gdb_signal_from_host (siggy
));
764 gdb_printf (_("Program terminated with signal %s, %s"),
765 gdb_signal_to_name (sig
), gdb_signal_to_string (sig
));
766 if (gdbarch_report_signal_info_p (core_gdbarch
))
767 gdbarch_report_signal_info (core_gdbarch
, current_uiout
, sig
);
768 gdb_printf (_(".\n"));
770 /* Set the value of the internal variable $_exitsignal,
771 which holds the signal uncaught by the inferior. */
772 set_internalvar_integer (lookup_internalvar ("_exitsignal"),
776 /* Fetch all registers from core file. */
777 target_fetch_registers (get_current_regcache (), -1);
779 /* Now, set up the frame cache, and print the top of stack. */
780 reinit_frame_cache ();
781 print_stack_frame (get_selected_frame (NULL
), 1, SRC_AND_LOC
, 1);
783 /* Current thread should be NUM 1 but the user does not know that.
784 If a program is single threaded gdb in general does not mention
785 anything about threads. That is why the test is >= 2. */
786 if (thread_count (target
) >= 2)
790 thread_command (NULL
, from_tty
);
792 catch (const gdb_exception_error
&except
)
794 exception_print (gdb_stderr
, except
);
800 core_target::detach (inferior
*inf
, int from_tty
)
802 /* Get rid of the core. Don't rely on core_target::close doing it,
803 because target_detach may be called with core_target's refcount > 1,
804 meaning core_target::close may not be called yet by the
805 unpush_target call below. */
808 /* Note that 'this' may be dangling after this call. unpush_target
809 closes the target if the refcount reaches 0, and our close
810 implementation deletes 'this'. */
811 inf
->unpush_target (this);
813 /* Clear the register cache and the frame cache. */
814 registers_changed ();
815 reinit_frame_cache ();
816 maybe_say_no_core_file_now (from_tty
);
819 /* Try to retrieve registers from a section in core_bfd, and supply
822 If ptid's lwp member is zero, do the single-threaded
823 thing: look for a section named NAME. If ptid's lwp
824 member is non-zero, do the multi-threaded thing: look for a section
825 named "NAME/LWP", where LWP is the shortest ASCII decimal
826 representation of ptid's lwp member.
828 HUMAN_NAME is a human-readable name for the kind of registers the
829 NAME section contains, for use in error messages.
831 If REQUIRED is true, print an error if the core file doesn't have a
832 section by the appropriate name. Otherwise, just do nothing. */
835 core_target::get_core_register_section (struct regcache
*regcache
,
836 const struct regset
*regset
,
838 int section_min_size
,
839 const char *human_name
,
842 gdb_assert (regset
!= nullptr);
844 struct bfd_section
*section
;
846 bool variable_size_section
= (regset
->flags
& REGSET_VARIABLE_SIZE
);
848 thread_section_name
section_name (name
, regcache
->ptid ());
850 section
= bfd_get_section_by_name (core_bfd
, section_name
.c_str ());
854 warning (_("Couldn't find %s registers in core file."),
859 size
= bfd_section_size (section
);
860 if (size
< section_min_size
)
862 warning (_("Section `%s' in core file too small."),
863 section_name
.c_str ());
866 if (size
!= section_min_size
&& !variable_size_section
)
868 warning (_("Unexpected size of section `%s' in core file."),
869 section_name
.c_str ());
872 gdb::byte_vector
contents (size
);
873 if (!bfd_get_section_contents (core_bfd
, section
, contents
.data (),
876 warning (_("Couldn't read %s registers from `%s' section in core file."),
877 human_name
, section_name
.c_str ());
881 regset
->supply_regset (regset
, regcache
, -1, contents
.data (), size
);
884 /* Data passed to gdbarch_iterate_over_regset_sections's callback. */
885 struct get_core_registers_cb_data
888 struct regcache
*regcache
;
891 /* Callback for get_core_registers that handles a single core file
892 register note section. */
895 get_core_registers_cb (const char *sect_name
, int supply_size
, int collect_size
,
896 const struct regset
*regset
,
897 const char *human_name
, void *cb_data
)
899 gdb_assert (regset
!= nullptr);
901 auto *data
= (get_core_registers_cb_data
*) cb_data
;
902 bool required
= false;
903 bool variable_size_section
= (regset
->flags
& REGSET_VARIABLE_SIZE
);
905 if (!variable_size_section
)
906 gdb_assert (supply_size
== collect_size
);
908 if (strcmp (sect_name
, ".reg") == 0)
911 if (human_name
== NULL
)
912 human_name
= "general-purpose";
914 else if (strcmp (sect_name
, ".reg2") == 0)
916 if (human_name
== NULL
)
917 human_name
= "floating-point";
920 data
->target
->get_core_register_section (data
->regcache
, regset
, sect_name
,
921 supply_size
, human_name
, required
);
924 /* Get the registers out of a core file. This is the machine-
925 independent part. Fetch_core_registers is the machine-dependent
926 part, typically implemented in the xm-file for each
929 /* We just get all the registers, so we don't use regno. */
932 core_target::fetch_registers (struct regcache
*regcache
, int regno
)
934 if (!(m_core_gdbarch
!= nullptr
935 && gdbarch_iterate_over_regset_sections_p (m_core_gdbarch
)))
937 gdb_printf (gdb_stderr
,
938 "Can't fetch registers from this type of core file\n");
942 struct gdbarch
*gdbarch
= regcache
->arch ();
943 get_core_registers_cb_data data
= { this, regcache
};
944 gdbarch_iterate_over_regset_sections (gdbarch
,
945 get_core_registers_cb
,
946 (void *) &data
, NULL
);
948 /* Mark all registers not found in the core as unavailable. */
949 for (int i
= 0; i
< gdbarch_num_regs (regcache
->arch ()); i
++)
950 if (regcache
->get_register_status (i
) == REG_UNKNOWN
)
951 regcache
->raw_supply (i
, NULL
);
955 core_target::files_info ()
957 print_section_info (&m_core_section_table
, core_bfd
);
960 /* Helper method for core_target::xfer_partial. */
962 enum target_xfer_status
963 core_target::xfer_memory_via_mappings (gdb_byte
*readbuf
,
964 const gdb_byte
*writebuf
,
965 ULONGEST offset
, ULONGEST len
,
966 ULONGEST
*xfered_len
)
968 enum target_xfer_status xfer_status
;
970 xfer_status
= (section_table_xfer_memory_partial
972 offset
, len
, xfered_len
,
973 m_core_file_mappings
));
975 if (xfer_status
== TARGET_XFER_OK
|| m_core_unavailable_mappings
.empty ())
978 /* There are instances - e.g. when debugging within a docker
979 container using the AUFS storage driver - where the pathnames
980 obtained from the note section are incorrect. Despite the path
981 being wrong, just knowing the start and end addresses of the
982 mappings is still useful; we can attempt an access of the file
983 stratum constrained to the address ranges corresponding to the
984 unavailable mappings. */
986 ULONGEST memaddr
= offset
;
987 ULONGEST memend
= offset
+ len
;
989 for (const auto &mr
: m_core_unavailable_mappings
)
991 if (address_in_mem_range (memaddr
, &mr
))
993 if (!address_in_mem_range (memend
, &mr
))
994 len
= mr
.start
+ mr
.length
- memaddr
;
996 xfer_status
= this->beneath ()->xfer_partial (TARGET_OBJECT_MEMORY
,
1010 enum target_xfer_status
1011 core_target::xfer_partial (enum target_object object
, const char *annex
,
1012 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1013 ULONGEST offset
, ULONGEST len
, ULONGEST
*xfered_len
)
1017 case TARGET_OBJECT_MEMORY
:
1019 enum target_xfer_status xfer_status
;
1021 /* Try accessing memory contents from core file data,
1022 restricting consideration to those sections for which
1023 the BFD section flag SEC_HAS_CONTENTS is set. */
1024 auto has_contents_cb
= [] (const struct target_section
*s
)
1026 return ((s
->the_bfd_section
->flags
& SEC_HAS_CONTENTS
) != 0);
1028 xfer_status
= section_table_xfer_memory_partial
1030 offset
, len
, xfered_len
,
1031 m_core_section_table
,
1033 if (xfer_status
== TARGET_XFER_OK
)
1034 return TARGET_XFER_OK
;
1036 /* Check file backed mappings. If they're available, use
1037 core file provided mappings (e.g. from .note.linuxcore.file
1038 or the like) as this should provide a more accurate
1039 result. If not, check the stratum beneath us, which should
1040 be the file stratum.
1042 We also check unavailable mappings due to Docker/AUFS driver
1044 if (!m_core_file_mappings
.empty ()
1045 || !m_core_unavailable_mappings
.empty ())
1047 xfer_status
= xfer_memory_via_mappings (readbuf
, writebuf
, offset
,
1051 xfer_status
= this->beneath ()->xfer_partial (object
, annex
, readbuf
,
1052 writebuf
, offset
, len
,
1054 if (xfer_status
== TARGET_XFER_OK
)
1055 return TARGET_XFER_OK
;
1057 /* Finally, attempt to access data in core file sections with
1058 no contents. These will typically read as all zero. */
1059 auto no_contents_cb
= [&] (const struct target_section
*s
)
1061 return !has_contents_cb (s
);
1063 xfer_status
= section_table_xfer_memory_partial
1065 offset
, len
, xfered_len
,
1066 m_core_section_table
,
1071 case TARGET_OBJECT_AUXV
:
1074 /* When the aux vector is stored in core file, BFD
1075 represents this with a fake section called ".auxv". */
1077 struct bfd_section
*section
;
1080 section
= bfd_get_section_by_name (core_bfd
, ".auxv");
1081 if (section
== NULL
)
1082 return TARGET_XFER_E_IO
;
1084 size
= bfd_section_size (section
);
1086 return TARGET_XFER_EOF
;
1092 return TARGET_XFER_EOF
;
1093 if (!bfd_get_section_contents (core_bfd
, section
, readbuf
,
1094 (file_ptr
) offset
, size
))
1096 warning (_("Couldn't read NT_AUXV note in core file."));
1097 return TARGET_XFER_E_IO
;
1100 *xfered_len
= (ULONGEST
) size
;
1101 return TARGET_XFER_OK
;
1103 return TARGET_XFER_E_IO
;
1105 case TARGET_OBJECT_WCOOKIE
:
1108 /* When the StackGhost cookie is stored in core file, BFD
1109 represents this with a fake section called
1112 struct bfd_section
*section
;
1115 section
= bfd_get_section_by_name (core_bfd
, ".wcookie");
1116 if (section
== NULL
)
1117 return TARGET_XFER_E_IO
;
1119 size
= bfd_section_size (section
);
1121 return TARGET_XFER_EOF
;
1127 return TARGET_XFER_EOF
;
1128 if (!bfd_get_section_contents (core_bfd
, section
, readbuf
,
1129 (file_ptr
) offset
, size
))
1131 warning (_("Couldn't read StackGhost cookie in core file."));
1132 return TARGET_XFER_E_IO
;
1135 *xfered_len
= (ULONGEST
) size
;
1136 return TARGET_XFER_OK
;
1139 return TARGET_XFER_E_IO
;
1141 case TARGET_OBJECT_LIBRARIES
:
1142 if (m_core_gdbarch
!= nullptr
1143 && gdbarch_core_xfer_shared_libraries_p (m_core_gdbarch
))
1146 return TARGET_XFER_E_IO
;
1149 *xfered_len
= gdbarch_core_xfer_shared_libraries (m_core_gdbarch
,
1153 if (*xfered_len
== 0)
1154 return TARGET_XFER_EOF
;
1156 return TARGET_XFER_OK
;
1159 return TARGET_XFER_E_IO
;
1161 case TARGET_OBJECT_LIBRARIES_AIX
:
1162 if (m_core_gdbarch
!= nullptr
1163 && gdbarch_core_xfer_shared_libraries_aix_p (m_core_gdbarch
))
1166 return TARGET_XFER_E_IO
;
1170 = gdbarch_core_xfer_shared_libraries_aix (m_core_gdbarch
,
1174 if (*xfered_len
== 0)
1175 return TARGET_XFER_EOF
;
1177 return TARGET_XFER_OK
;
1180 return TARGET_XFER_E_IO
;
1182 case TARGET_OBJECT_SIGNAL_INFO
:
1185 if (m_core_gdbarch
!= nullptr
1186 && gdbarch_core_xfer_siginfo_p (m_core_gdbarch
))
1188 LONGEST l
= gdbarch_core_xfer_siginfo (m_core_gdbarch
, readbuf
,
1195 return TARGET_XFER_EOF
;
1197 return TARGET_XFER_OK
;
1201 return TARGET_XFER_E_IO
;
1204 return this->beneath ()->xfer_partial (object
, annex
, readbuf
,
1205 writebuf
, offset
, len
,
1212 /* Okay, let's be honest: threads gleaned from a core file aren't
1213 exactly lively, are they? On the other hand, if we don't claim
1214 that each & every one is alive, then we don't get any of them
1215 to appear in an "info thread" command, which is quite a useful
1219 core_target::thread_alive (ptid_t ptid
)
1224 /* Ask the current architecture what it knows about this core file.
1225 That will be used, in turn, to pick a better architecture. This
1226 wrapper could be avoided if targets got a chance to specialize
1229 const struct target_desc
*
1230 core_target::read_description ()
1232 /* If the core file contains a target description note then we will use
1233 that in preference to anything else. */
1234 bfd_size_type tdesc_note_size
= 0;
1235 struct bfd_section
*tdesc_note_section
1236 = bfd_get_section_by_name (core_bfd
, ".gdb-tdesc");
1237 if (tdesc_note_section
!= nullptr)
1238 tdesc_note_size
= bfd_section_size (tdesc_note_section
);
1239 if (tdesc_note_size
> 0)
1241 gdb::char_vector
contents (tdesc_note_size
+ 1);
1242 if (bfd_get_section_contents (core_bfd
, tdesc_note_section
,
1243 contents
.data (), (file_ptr
) 0,
1246 /* Ensure we have a null terminator. */
1247 contents
[tdesc_note_size
] = '\0';
1248 const struct target_desc
*result
1249 = string_read_description_xml (contents
.data ());
1250 if (result
!= nullptr)
1255 if (m_core_gdbarch
&& gdbarch_core_read_description_p (m_core_gdbarch
))
1257 const struct target_desc
*result
;
1259 result
= gdbarch_core_read_description (m_core_gdbarch
, this, core_bfd
);
1264 return this->beneath ()->read_description ();
1268 core_target::pid_to_str (ptid_t ptid
)
1270 struct inferior
*inf
;
1273 /* The preferred way is to have a gdbarch/OS specific
1275 if (m_core_gdbarch
!= nullptr
1276 && gdbarch_core_pid_to_str_p (m_core_gdbarch
))
1277 return gdbarch_core_pid_to_str (m_core_gdbarch
, ptid
);
1279 /* Otherwise, if we don't have one, we'll just fallback to
1280 "process", with normal_pid_to_str. */
1282 /* Try the LWPID field first. */
1285 return normal_pid_to_str (ptid_t (pid
));
1287 /* Otherwise, this isn't a "threaded" core -- use the PID field, but
1288 only if it isn't a fake PID. */
1289 inf
= find_inferior_ptid (this, ptid
);
1290 if (inf
!= NULL
&& !inf
->fake_pid_p
)
1291 return normal_pid_to_str (ptid
);
1293 /* No luck. We simply don't have a valid PID to print. */
1294 return "<main task>";
1298 core_target::thread_name (struct thread_info
*thr
)
1300 if (m_core_gdbarch
!= nullptr
1301 && gdbarch_core_thread_name_p (m_core_gdbarch
))
1302 return gdbarch_core_thread_name (m_core_gdbarch
, thr
);
1307 core_target::has_memory ()
1309 return (core_bfd
!= NULL
);
1313 core_target::has_stack ()
1315 return (core_bfd
!= NULL
);
1319 core_target::has_registers ()
1321 return (core_bfd
!= NULL
);
1324 /* Implement the to_info_proc method. */
1327 core_target::info_proc (const char *args
, enum info_proc_what request
)
1329 struct gdbarch
*gdbarch
= get_current_arch ();
1331 /* Since this is the core file target, call the 'core_info_proc'
1332 method on gdbarch, not 'info_proc'. */
1333 if (gdbarch_core_info_proc_p (gdbarch
))
1334 gdbarch_core_info_proc (gdbarch
, args
, request
);
1339 /* Implementation of the "supports_memory_tagging" target_ops method. */
1342 core_target::supports_memory_tagging ()
1344 /* Look for memory tag sections. If they exist, that means this core file
1345 supports memory tagging. */
1347 return (bfd_get_section_by_name (core_bfd
, "memtag") != nullptr);
1350 /* Implementation of the "fetch_memtags" target_ops method. */
1353 core_target::fetch_memtags (CORE_ADDR address
, size_t len
,
1354 gdb::byte_vector
&tags
, int type
)
1356 struct gdbarch
*gdbarch
= target_gdbarch ();
1358 /* Make sure we have a way to decode the memory tag notes. */
1359 if (!gdbarch_decode_memtag_section_p (gdbarch
))
1360 error (_("gdbarch_decode_memtag_section not implemented for this "
1363 memtag_section_info info
;
1364 info
.memtag_section
= nullptr;
1366 while (get_next_core_memtag_section (core_bfd
, info
.memtag_section
,
1369 size_t adjusted_length
1370 = (address
+ len
< info
.end_address
) ? len
: (info
.end_address
- address
);
1372 /* Decode the memory tag note and return the tags. */
1373 gdb::byte_vector tags_read
1374 = gdbarch_decode_memtag_section (gdbarch
, info
.memtag_section
, type
,
1375 address
, adjusted_length
);
1377 /* Transfer over the tags that have been read. */
1378 tags
.insert (tags
.end (), tags_read
.begin (), tags_read
.end ());
1380 /* ADDRESS + LEN may cross the boundaries of a particular memory tag
1381 segment. Check if we need to fetch tags from a different section. */
1382 if (!tags_read
.empty () && (address
+ len
) < info
.end_address
)
1385 /* There are more tags to fetch. Update ADDRESS and LEN. */
1386 len
-= (info
.end_address
- address
);
1387 address
= info
.end_address
;
1393 /* Implementation of the "fetch_x86_xsave_layout" target_ops method. */
1396 core_target::fetch_x86_xsave_layout ()
1398 if (m_core_gdbarch
!= nullptr &&
1399 gdbarch_core_read_x86_xsave_layout_p (m_core_gdbarch
))
1401 x86_xsave_layout layout
;
1402 if (!gdbarch_core_read_x86_xsave_layout (m_core_gdbarch
, layout
))
1411 /* Get a pointer to the current core target. If not connected to a
1412 core target, return NULL. */
1414 static core_target
*
1415 get_current_core_target ()
1417 target_ops
*proc_target
= current_inferior ()->process_target ();
1418 return dynamic_cast<core_target
*> (proc_target
);
1421 /* Display file backed mappings from core file. */
1424 core_target::info_proc_mappings (struct gdbarch
*gdbarch
)
1426 if (!m_core_file_mappings
.empty ())
1428 gdb_printf (_("Mapped address spaces:\n\n"));
1429 if (gdbarch_addr_bit (gdbarch
) == 32)
1431 gdb_printf ("\t%10s %10s %10s %10s %s\n",
1434 " Size", " Offset", "objfile");
1438 gdb_printf (" %18s %18s %10s %10s %s\n",
1441 " Size", " Offset", "objfile");
1445 for (const target_section
&tsp
: m_core_file_mappings
)
1447 ULONGEST start
= tsp
.addr
;
1448 ULONGEST end
= tsp
.endaddr
;
1449 ULONGEST file_ofs
= tsp
.the_bfd_section
->filepos
;
1450 const char *filename
= bfd_get_filename (tsp
.the_bfd_section
->owner
);
1452 if (gdbarch_addr_bit (gdbarch
) == 32)
1453 gdb_printf ("\t%10s %10s %10s %10s %s\n",
1454 paddress (gdbarch
, start
),
1455 paddress (gdbarch
, end
),
1456 hex_string (end
- start
),
1457 hex_string (file_ofs
),
1460 gdb_printf (" %18s %18s %10s %10s %s\n",
1461 paddress (gdbarch
, start
),
1462 paddress (gdbarch
, end
),
1463 hex_string (end
- start
),
1464 hex_string (file_ofs
),
1469 /* Implement "maintenance print core-file-backed-mappings" command.
1471 If mappings are loaded, the results should be similar to the
1472 mappings shown by "info proc mappings". This command is mainly a
1473 debugging tool for GDB developers to make sure that the expected
1474 mappings are present after loading a core file. For Linux, the
1475 output provided by this command will be very similar (if not
1476 identical) to that provided by "info proc mappings". This is not
1477 necessarily the case for other OSes which might provide
1478 more/different information in the "info proc mappings" output. */
1481 maintenance_print_core_file_backed_mappings (const char *args
, int from_tty
)
1483 core_target
*targ
= get_current_core_target ();
1484 if (targ
!= nullptr)
1485 targ
->info_proc_mappings (targ
->core_gdbarch ());
1488 void _initialize_corelow ();
1490 _initialize_corelow ()
1492 add_target (core_target_info
, core_target_open
, filename_completer
);
1493 add_cmd ("core-file-backed-mappings", class_maintenance
,
1494 maintenance_print_core_file_backed_mappings
,
1495 _("Print core file's file-backed mappings."),
1496 &maintenanceprintlist
);