1 /* Core dump and executable file functions below target vector, for GDB.
3 Copyright (C) 1986-2024 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/>. */
20 #include "arch-utils.h"
23 #include "exceptions.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>
54 #include "cli/cli-cmds.h"
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 /* If the architecture supports it, check if ADDRESS is within a memory range
114 mapped with tags. For example, MTE tags for AArch64. */
115 bool is_address_tagged (gdbarch
*gdbarch
, CORE_ADDR address
) override
;
117 x86_xsave_layout
fetch_x86_xsave_layout () override
;
121 /* Getter, see variable definition. */
122 struct gdbarch
*core_gdbarch ()
124 return m_core_gdbarch
;
127 /* See definition. */
128 void get_core_register_section (struct regcache
*regcache
,
129 const struct regset
*regset
,
131 int section_min_size
,
132 const char *human_name
,
135 /* See definition. */
136 void info_proc_mappings (struct gdbarch
*gdbarch
);
138 private: /* per-core data */
140 /* Get rid of the core inferior. */
143 /* The core's section table. Note that these target sections are
144 *not* mapped in the current address spaces' set of target
145 sections --- those should come only from pure executable or
146 shared library bfds. The core bfd sections are an implementation
147 detail of the core target, just like ptrace is for unix child
149 std::vector
<target_section
> m_core_section_table
;
151 /* File-backed address space mappings: some core files include
152 information about memory mapped files. */
153 std::vector
<target_section
> m_core_file_mappings
;
155 /* Unavailable mappings. These correspond to pathnames which either
156 weren't found or could not be opened. Knowing these addresses can
158 std::vector
<mem_range
> m_core_unavailable_mappings
;
160 /* Build m_core_file_mappings. Called from the constructor. */
161 void build_file_mappings ();
163 /* Helper method for xfer_partial. */
164 enum target_xfer_status
xfer_memory_via_mappings (gdb_byte
*readbuf
,
165 const gdb_byte
*writebuf
,
168 ULONGEST
*xfered_len
);
170 /* FIXME: kettenis/20031023: Eventually this field should
172 struct gdbarch
*m_core_gdbarch
= NULL
;
175 core_target::core_target ()
177 /* Find a first arch based on the BFD. We need the initial gdbarch so
178 we can setup the hooks to find a target description. */
179 m_core_gdbarch
= gdbarch_from_bfd (current_program_space
->core_bfd ());
181 /* If the arch is able to read a target description from the core, it
182 could yield a more specific gdbarch. */
183 const struct target_desc
*tdesc
= read_description ();
185 if (tdesc
!= nullptr)
187 struct gdbarch_info info
;
188 info
.abfd
= current_program_space
->core_bfd ();
189 info
.target_desc
= tdesc
;
190 m_core_gdbarch
= gdbarch_find_by_info (info
);
194 || !gdbarch_iterate_over_regset_sections_p (m_core_gdbarch
))
195 error (_("\"%s\": Core file format not supported"),
196 bfd_get_filename (current_program_space
->core_bfd ()));
198 /* Find the data section */
199 m_core_section_table
= build_section_table (current_program_space
->core_bfd ());
201 build_file_mappings ();
204 /* Construct the table for file-backed mappings if they exist.
206 For each unique path in the note, we'll open a BFD with a bfd
207 target of "binary". This is an unstructured bfd target upon which
208 we'll impose a structure from the mappings in the architecture-specific
209 mappings note. A BFD section is allocated and initialized for each
212 We take care to not share already open bfds with other parts of
213 GDB; in particular, we don't want to add new sections to existing
214 BFDs. We do, however, ensure that the BFDs that we allocate here
215 will go away (be deallocated) when the core target is detached. */
218 core_target::build_file_mappings ()
220 std::unordered_map
<std::string
, struct bfd
*> bfd_map
;
221 std::unordered_set
<std::string
> unavailable_paths
;
223 /* See linux_read_core_file_mappings() in linux-tdep.c for an example
224 read_core_file_mappings method. */
225 gdbarch_read_core_file_mappings (m_core_gdbarch
,
226 current_program_space
->core_bfd (),
228 /* After determining the number of mappings, read_core_file_mappings
229 will invoke this lambda. */
234 /* read_core_file_mappings will invoke this lambda for each mapping
236 [&] (int num
, ULONGEST start
, ULONGEST end
, ULONGEST file_ofs
,
237 const char *filename
, const bfd_build_id
*build_id
)
239 /* Architecture-specific read_core_mapping methods are expected to
240 weed out non-file-backed mappings. */
241 gdb_assert (filename
!= nullptr);
243 if (unavailable_paths
.find (filename
) != unavailable_paths
.end ())
245 /* We have already seen some mapping for FILENAME but failed to
246 find/open the file. There is no point in trying the same
247 thing again so just record that the range [start, end) is
249 m_core_unavailable_mappings
.emplace_back (start
, end
- start
);
253 struct bfd
*bfd
= bfd_map
[filename
];
256 /* Use exec_file_find() to do sysroot expansion. It'll
257 also strip the potential sysroot "target:" prefix. If
258 there is no sysroot, an equivalent (possibly more
259 canonical) pathname will be provided. */
260 gdb::unique_xmalloc_ptr
<char> expanded_fname
261 = exec_file_find (filename
, NULL
);
263 if (expanded_fname
== nullptr && build_id
!= nullptr)
264 debuginfod_exec_query (build_id
->data
, build_id
->size
,
265 filename
, &expanded_fname
);
267 if (expanded_fname
== nullptr)
269 m_core_unavailable_mappings
.emplace_back (start
, end
- start
);
270 unavailable_paths
.insert (filename
);
271 warning (_("Can't open file %s during file-backed mapping "
277 bfd
= bfd_openr (expanded_fname
.get (), "binary");
279 if (bfd
== nullptr || !bfd_check_format (bfd
, bfd_object
))
281 m_core_unavailable_mappings
.emplace_back (start
, end
- start
);
282 unavailable_paths
.insert (filename
);
283 warning (_("Can't open file %s which was expanded to %s "
284 "during file-backed mapping note processing"),
285 filename
, expanded_fname
.get ());
291 /* Ensure that the bfd will be closed when core_bfd is closed.
292 This can be checked before/after a core file detach via
293 "maint info bfds". */
294 gdb_bfd_record_inclusion (current_program_space
->core_bfd (), bfd
);
295 bfd_map
[filename
] = bfd
;
298 /* Make new BFD section. All sections have the same name,
299 which is permitted by bfd_make_section_anyway(). */
300 asection
*sec
= bfd_make_section_anyway (bfd
, "load");
302 error (_("Can't make section"));
303 sec
->filepos
= file_ofs
;
304 bfd_set_section_flags (sec
, SEC_READONLY
| SEC_HAS_CONTENTS
);
305 bfd_set_section_size (sec
, end
- start
);
306 bfd_set_section_vma (sec
, start
);
307 bfd_set_section_lma (sec
, start
);
308 bfd_set_section_alignment (sec
, 2);
310 /* Set target_section fields. */
311 m_core_file_mappings
.emplace_back (start
, end
, sec
);
313 /* If this is a bfd of a shared library, record its soname
315 if (build_id
!= nullptr)
317 gdb::unique_xmalloc_ptr
<char> soname
318 = gdb_bfd_read_elf_soname (bfd
->filename
);
319 if (soname
!= nullptr)
320 set_cbfd_soname_build_id (current_program_space
->cbfd
,
321 soname
.get (), build_id
);
325 normalize_mem_ranges (&m_core_unavailable_mappings
);
328 /* An arbitrary identifier for the core inferior. */
329 #define CORELOW_PID 1
332 core_target::clear_core ()
334 if (current_program_space
->core_bfd () != nullptr)
336 switch_to_no_thread (); /* Avoid confusion from thread
338 exit_inferior (current_inferior ());
340 /* Clear out solib state while the bfd is still open. See
341 comments in clear_solib in solib.c. */
342 clear_solib (current_program_space
);
344 current_program_space
->cbfd
.reset (nullptr);
348 /* Close the core target. */
351 core_target::close ()
355 /* Core targets are heap-allocated (see core_target_open), so here
356 we delete ourselves. */
360 /* Look for sections whose names start with `.reg/' so that we can
361 extract the list of threads in a core file. */
363 /* If ASECT is a section whose name begins with '.reg/' then extract the
364 lwpid after the '/' and create a new thread in INF.
366 If REG_SECT is not nullptr, and the both ASECT and REG_SECT point at the
367 same position in the parent bfd object then switch to the newly created
368 thread, otherwise, the selected thread is left unchanged. */
371 add_to_thread_list (asection
*asect
, asection
*reg_sect
, inferior
*inf
)
373 if (!startswith (bfd_section_name (asect
), ".reg/"))
376 int lwpid
= atoi (bfd_section_name (asect
) + 5);
377 ptid_t
ptid (inf
->pid
, lwpid
);
378 thread_info
*thr
= add_thread (inf
->process_target (), ptid
);
380 /* Warning, Will Robinson, looking at BFD private data! */
383 && asect
->filepos
== reg_sect
->filepos
) /* Did we find .reg? */
384 switch_to_thread (thr
); /* Yes, make it current. */
387 /* Issue a message saying we have no core to debug, if FROM_TTY. */
390 maybe_say_no_core_file_now (int from_tty
)
393 gdb_printf (_("No core file now.\n"));
396 /* Backward compatibility with old way of specifying core files. */
399 core_file_command (const char *filename
, int from_tty
)
401 dont_repeat (); /* Either way, seems bogus. */
403 if (filename
== NULL
)
405 if (current_program_space
->core_bfd () != nullptr)
407 target_detach (current_inferior (), from_tty
);
408 gdb_assert (current_program_space
->core_bfd () == nullptr);
411 maybe_say_no_core_file_now (from_tty
);
414 core_target_open (filename
, from_tty
);
417 /* A vmcore file is a core file created by the Linux kernel at the point of
418 a crash. Each thread in the core file represents a real CPU core, and
419 the lwpid for each thread is the pid of the process that was running on
420 that core at the moment of the crash.
422 However, not every CPU core will have been running a process, some cores
423 will be idle. For these idle cores the CPU writes an lwpid of 0. And
424 of course, multiple cores might be idle, so there could be multiple
425 threads with an lwpid of 0.
427 The problem is GDB doesn't really like threads with an lwpid of 0; GDB
428 presents such a thread as a process rather than a thread. And GDB
429 certainly doesn't like multiple threads having the same lwpid, each time
430 a new thread is seen with the same lwpid the earlier thread (with the
431 same lwpid) will be deleted.
433 This function addresses both of these problems by assigning a fake lwpid
434 to any thread with an lwpid of 0.
436 GDB finds the lwpid information by looking at the bfd section names
437 which include the lwpid, e.g. .reg/NN where NN is the lwpid. This
438 function looks though all the section names looking for sections named
439 .reg/NN. If any sections are found where NN == 0, then we assign a new
440 unique value of NN. Then, in a second pass, any sections ending /0 are
441 assigned their new number.
443 Remember, a core file may contain multiple register sections for
444 different register sets, but the sets are always grouped by thread, so
445 we can figure out which registers should be assigned the same new
446 lwpid. For example, consider a core file containing:
448 .reg/0, .reg2/0, .reg/0, .reg2/0
450 This represents two threads, each thread contains a .reg and .reg2
451 register set. The .reg represents the start of each thread. After
452 renaming the sections will now look like this:
454 .reg/1, .reg2/1, .reg/2, .reg2/2
456 After calling this function the rest of the core file handling code can
457 treat this core file just like any other core file. */
460 rename_vmcore_idle_reg_sections (bfd
*abfd
, inferior
*inf
)
462 /* Map from the bfd section to its lwpid (the /NN number). */
463 std::vector
<std::pair
<asection
*, int>> sections_and_lwpids
;
465 /* The set of all /NN numbers found. Needed so we can easily find unused
466 numbers in the case that we need to rename some sections. */
467 std::unordered_set
<int> all_lwpids
;
469 /* A count of how many sections called .reg/0 we have found. */
470 unsigned zero_lwpid_count
= 0;
472 /* Look for all the .reg sections. Record the section object and the
473 lwpid which is extracted from the section name. Spot if any have an
475 for (asection
*sect
: gdb_bfd_sections (current_program_space
->core_bfd ()))
477 if (startswith (bfd_section_name (sect
), ".reg/"))
479 int lwpid
= atoi (bfd_section_name (sect
) + 5);
480 sections_and_lwpids
.emplace_back (sect
, lwpid
);
481 all_lwpids
.insert (lwpid
);
487 /* If every ".reg/NN" section has a non-zero lwpid then we don't need to
489 if (zero_lwpid_count
== 0)
492 /* Assign a new number to any .reg sections with an lwpid of 0. */
494 for (auto §_and_lwpid
: sections_and_lwpids
)
495 if (sect_and_lwpid
.second
== 0)
497 while (all_lwpids
.find (new_lwpid
) != all_lwpids
.end ())
499 sect_and_lwpid
.second
= new_lwpid
;
503 /* Now update the names of any sections with an lwpid of 0. This is
504 more than just the .reg sections we originally found. */
505 std::string replacement_lwpid_str
;
506 auto iter
= sections_and_lwpids
.begin ();
507 int replacement_lwpid
= 0;
508 for (asection
*sect
: gdb_bfd_sections (current_program_space
->core_bfd ()))
510 if (iter
!= sections_and_lwpids
.end () && sect
== iter
->first
)
512 gdb_assert (startswith (bfd_section_name (sect
), ".reg/"));
514 int lwpid
= atoi (bfd_section_name (sect
) + 5);
515 if (lwpid
== iter
->second
)
517 /* This section was not given a new number. */
518 gdb_assert (lwpid
!= 0);
519 replacement_lwpid
= 0;
523 replacement_lwpid
= iter
->second
;
524 ptid_t
ptid (inf
->pid
, replacement_lwpid
);
525 if (!replacement_lwpid_str
.empty ())
526 replacement_lwpid_str
+= ", ";
527 replacement_lwpid_str
+= target_pid_to_str (ptid
);
533 if (replacement_lwpid
!= 0)
535 const char *name
= bfd_section_name (sect
);
536 size_t len
= strlen (name
);
538 if (strncmp (name
+ len
- 2, "/0", 2) == 0)
540 /* This section needs a new name. */
542 = string_printf ("%.*s/%d",
543 static_cast<int> (len
- 2),
544 name
, replacement_lwpid
);
546 = static_cast<char *> (bfd_alloc (abfd
, name_str
.size () + 1));
547 if (name_buf
== nullptr)
548 error (_("failed to allocate space for section name '%s'"),
550 memcpy (name_buf
, name_str
.c_str(), name_str
.size () + 1);
551 bfd_rename_section (sect
, name_buf
);
556 if (zero_lwpid_count
== 1)
557 warning (_("found thread with pid 0, assigned replacement Target Id: %s"),
558 replacement_lwpid_str
.c_str ());
560 warning (_("found threads with pid 0, assigned replacement Target Ids: %s"),
561 replacement_lwpid_str
.c_str ());
564 /* Locate (and load) an executable file (and symbols) given the core file
568 locate_exec_from_corefile_build_id (bfd
*abfd
, int from_tty
)
570 const bfd_build_id
*build_id
= build_id_bfd_get (abfd
);
571 if (build_id
== nullptr)
574 gdb_bfd_ref_ptr execbfd
575 = build_id_to_exec_bfd (build_id
->size
, build_id
->data
);
577 if (execbfd
== nullptr)
579 /* Attempt to query debuginfod for the executable. */
580 gdb::unique_xmalloc_ptr
<char> execpath
;
581 scoped_fd fd
= debuginfod_exec_query (build_id
->data
, build_id
->size
,
582 abfd
->filename
, &execpath
);
586 execbfd
= gdb_bfd_open (execpath
.get (), gnutarget
);
588 if (execbfd
== nullptr)
589 warning (_("\"%s\" from debuginfod cannot be opened as bfd: %s"),
591 gdb_bfd_errmsg (bfd_get_error (), nullptr).c_str ());
592 else if (!build_id_verify (execbfd
.get (), build_id
->size
,
594 execbfd
.reset (nullptr);
598 if (execbfd
!= nullptr)
600 exec_file_attach (bfd_get_filename (execbfd
.get ()), from_tty
);
601 symbol_file_add_main (bfd_get_filename (execbfd
.get ()),
602 symfile_add_flag (from_tty
? SYMFILE_VERBOSE
: 0));
609 core_target_open (const char *arg
, int from_tty
)
616 target_preopen (from_tty
);
619 if (current_program_space
->core_bfd ())
620 error (_("No core file specified. (Use `detach' "
621 "to stop debugging a core file.)"));
623 error (_("No core file specified."));
626 gdb::unique_xmalloc_ptr
<char> filename (tilde_expand (arg
));
627 if (strlen (filename
.get ()) != 0
628 && !IS_ABSOLUTE_PATH (filename
.get ()))
629 filename
= make_unique_xstrdup (gdb_abspath (filename
).c_str ());
631 flags
= O_BINARY
| O_LARGEFILE
;
636 scratch_chan
= gdb_open_cloexec (filename
.get (), flags
, 0).release ();
637 if (scratch_chan
< 0)
638 perror_with_name (filename
.get ());
640 gdb_bfd_ref_ptr
temp_bfd (gdb_bfd_fopen (filename
.get (), gnutarget
,
641 write_files
? FOPEN_RUB
: FOPEN_RB
,
643 if (temp_bfd
== NULL
)
644 perror_with_name (filename
.get ());
646 if (!bfd_check_format (temp_bfd
.get (), bfd_core
))
648 /* Do it after the err msg */
649 /* FIXME: should be checking for errors from bfd_close (for one
650 thing, on error it does not free all the storage associated
652 error (_("\"%s\" is not a core dump: %s"),
653 filename
.get (), bfd_errmsg (bfd_get_error ()));
656 current_program_space
->cbfd
= std::move (temp_bfd
);
658 core_target
*target
= new core_target ();
660 /* Own the target until it is successfully pushed. */
661 target_ops_up
target_holder (target
);
665 /* If we have no exec file, try to set the architecture from the
666 core file. We don't do this unconditionally since an exec file
667 typically contains more information that helps us determine the
668 architecture than a core file. */
669 if (!current_program_space
->exec_bfd ())
670 set_gdbarch_from_file (current_program_space
->core_bfd ());
672 current_inferior ()->push_target (std::move (target_holder
));
674 switch_to_no_thread ();
676 /* Need to flush the register cache (and the frame cache) from a
677 previous debug session. If inferior_ptid ends up the same as the
678 last debug session --- e.g., b foo; run; gcore core1; step; gcore
679 core2; core core1; core core2 --- then there's potential for
680 get_current_regcache to return the cached regcache of the
681 previous session, and the frame cache being stale. */
682 registers_changed ();
684 /* Find (or fake) the pid for the process in this core file, and
685 initialise the current inferior with that pid. */
686 bool fake_pid_p
= false;
687 int pid
= bfd_core_file_pid (current_program_space
->core_bfd ());
694 inferior
*inf
= current_inferior ();
695 gdb_assert (inf
->pid
== 0);
696 inferior_appeared (inf
, pid
);
697 inf
->fake_pid_p
= fake_pid_p
;
699 /* Rename any .reg/0 sections, giving them each a fake lwpid. */
700 rename_vmcore_idle_reg_sections (current_program_space
->core_bfd (), inf
);
702 /* Build up thread list from BFD sections, and possibly set the
703 current thread to the .reg/NN section matching the .reg
706 = bfd_get_section_by_name (current_program_space
->core_bfd (), ".reg");
707 for (asection
*sect
: gdb_bfd_sections (current_program_space
->core_bfd ()))
708 add_to_thread_list (sect
, reg_sect
, inf
);
710 if (inferior_ptid
== null_ptid
)
712 /* Either we found no .reg/NN section, and hence we have a
713 non-threaded core (single-threaded, from gdb's perspective),
714 or for some reason add_to_thread_list couldn't determine
715 which was the "main" thread. The latter case shouldn't
716 usually happen, but we're dealing with input here, which can
717 always be broken in different ways. */
718 thread_info
*thread
= first_thread_of_inferior (inf
);
721 thread
= add_thread_silent (target
, ptid_t (CORELOW_PID
));
723 switch_to_thread (thread
);
726 if (current_program_space
->exec_bfd () == nullptr)
727 locate_exec_from_corefile_build_id (current_program_space
->core_bfd (),
730 post_create_inferior (from_tty
);
732 /* Now go through the target stack looking for threads since there
733 may be a thread_stratum target loaded on top of target core by
734 now. The layer above should claim threads found in the BFD
738 target_update_thread_list ();
741 catch (const gdb_exception_error
&except
)
743 exception_print (gdb_stderr
, except
);
746 p
= bfd_core_file_failing_command (current_program_space
->core_bfd ());
748 gdb_printf (_("Core was generated by `%s'.\n"), p
);
750 /* Clearing any previous state of convenience variables. */
751 clear_exit_convenience_vars ();
753 siggy
= bfd_core_file_failing_signal (current_program_space
->core_bfd ());
756 gdbarch
*core_gdbarch
= target
->core_gdbarch ();
758 /* If we don't have a CORE_GDBARCH to work with, assume a native
759 core (map gdb_signal from host signals). If we do have
760 CORE_GDBARCH to work with, but no gdb_signal_from_target
761 implementation for that gdbarch, as a fallback measure,
762 assume the host signal mapping. It'll be correct for native
763 cores, but most likely incorrect for cross-cores. */
764 enum gdb_signal sig
= (core_gdbarch
!= NULL
765 && gdbarch_gdb_signal_from_target_p (core_gdbarch
)
766 ? gdbarch_gdb_signal_from_target (core_gdbarch
,
768 : gdb_signal_from_host (siggy
));
770 gdb_printf (_("Program terminated with signal %s, %s"),
771 gdb_signal_to_name (sig
), gdb_signal_to_string (sig
));
772 if (gdbarch_report_signal_info_p (core_gdbarch
))
773 gdbarch_report_signal_info (core_gdbarch
, current_uiout
, sig
);
774 gdb_printf (_(".\n"));
776 /* Set the value of the internal variable $_exitsignal,
777 which holds the signal uncaught by the inferior. */
778 set_internalvar_integer (lookup_internalvar ("_exitsignal"),
782 /* Fetch all registers from core file. */
783 target_fetch_registers (get_thread_regcache (inferior_thread ()), -1);
785 /* Now, set up the frame cache, and print the top of stack. */
786 reinit_frame_cache ();
787 print_stack_frame (get_selected_frame (NULL
), 1, SRC_AND_LOC
, 1);
789 /* Current thread should be NUM 1 but the user does not know that.
790 If a program is single threaded gdb in general does not mention
791 anything about threads. That is why the test is >= 2. */
792 if (thread_count (target
) >= 2)
796 thread_command (NULL
, from_tty
);
798 catch (const gdb_exception_error
&except
)
800 exception_print (gdb_stderr
, except
);
806 core_target::detach (inferior
*inf
, int from_tty
)
808 /* Get rid of the core. Don't rely on core_target::close doing it,
809 because target_detach may be called with core_target's refcount > 1,
810 meaning core_target::close may not be called yet by the
811 unpush_target call below. */
814 /* Note that 'this' may be dangling after this call. unpush_target
815 closes the target if the refcount reaches 0, and our close
816 implementation deletes 'this'. */
817 inf
->unpush_target (this);
819 /* Clear the register cache and the frame cache. */
820 registers_changed ();
821 reinit_frame_cache ();
822 maybe_say_no_core_file_now (from_tty
);
825 /* Try to retrieve registers from a section in core_bfd, and supply
828 If ptid's lwp member is zero, do the single-threaded
829 thing: look for a section named NAME. If ptid's lwp
830 member is non-zero, do the multi-threaded thing: look for a section
831 named "NAME/LWP", where LWP is the shortest ASCII decimal
832 representation of ptid's lwp member.
834 HUMAN_NAME is a human-readable name for the kind of registers the
835 NAME section contains, for use in error messages.
837 If REQUIRED is true, print an error if the core file doesn't have a
838 section by the appropriate name. Otherwise, just do nothing. */
841 core_target::get_core_register_section (struct regcache
*regcache
,
842 const struct regset
*regset
,
844 int section_min_size
,
845 const char *human_name
,
848 gdb_assert (regset
!= nullptr);
850 struct bfd_section
*section
;
852 bool variable_size_section
= (regset
->flags
& REGSET_VARIABLE_SIZE
);
854 thread_section_name
section_name (name
, regcache
->ptid ());
856 section
= bfd_get_section_by_name (current_program_space
->core_bfd (),
857 section_name
.c_str ());
861 warning (_("Couldn't find %s registers in core file."),
866 size
= bfd_section_size (section
);
867 if (size
< section_min_size
)
869 warning (_("Section `%s' in core file too small."),
870 section_name
.c_str ());
873 if (size
!= section_min_size
&& !variable_size_section
)
875 warning (_("Unexpected size of section `%s' in core file."),
876 section_name
.c_str ());
879 gdb::byte_vector
contents (size
);
880 if (!bfd_get_section_contents (current_program_space
->core_bfd (), section
,
881 contents
.data (), (file_ptr
) 0, size
))
883 warning (_("Couldn't read %s registers from `%s' section in core file."),
884 human_name
, section_name
.c_str ());
888 regset
->supply_regset (regset
, regcache
, -1, contents
.data (), size
);
891 /* Data passed to gdbarch_iterate_over_regset_sections's callback. */
892 struct get_core_registers_cb_data
895 struct regcache
*regcache
;
898 /* Callback for get_core_registers that handles a single core file
899 register note section. */
902 get_core_registers_cb (const char *sect_name
, int supply_size
, int collect_size
,
903 const struct regset
*regset
,
904 const char *human_name
, void *cb_data
)
906 gdb_assert (regset
!= nullptr);
908 auto *data
= (get_core_registers_cb_data
*) cb_data
;
909 bool required
= false;
910 bool variable_size_section
= (regset
->flags
& REGSET_VARIABLE_SIZE
);
912 if (!variable_size_section
)
913 gdb_assert (supply_size
== collect_size
);
915 if (strcmp (sect_name
, ".reg") == 0)
918 if (human_name
== NULL
)
919 human_name
= "general-purpose";
921 else if (strcmp (sect_name
, ".reg2") == 0)
923 if (human_name
== NULL
)
924 human_name
= "floating-point";
927 data
->target
->get_core_register_section (data
->regcache
, regset
, sect_name
,
928 supply_size
, human_name
, required
);
931 /* Get the registers out of a core file. This is the machine-
932 independent part. Fetch_core_registers is the machine-dependent
933 part, typically implemented in the xm-file for each
936 /* We just get all the registers, so we don't use regno. */
939 core_target::fetch_registers (struct regcache
*regcache
, int regno
)
941 if (!(m_core_gdbarch
!= nullptr
942 && gdbarch_iterate_over_regset_sections_p (m_core_gdbarch
)))
944 gdb_printf (gdb_stderr
,
945 "Can't fetch registers from this type of core file\n");
949 struct gdbarch
*gdbarch
= regcache
->arch ();
950 get_core_registers_cb_data data
= { this, regcache
};
951 gdbarch_iterate_over_regset_sections (gdbarch
,
952 get_core_registers_cb
,
953 (void *) &data
, NULL
);
955 /* Mark all registers not found in the core as unavailable. */
956 for (int i
= 0; i
< gdbarch_num_regs (regcache
->arch ()); i
++)
957 if (regcache
->get_register_status (i
) == REG_UNKNOWN
)
958 regcache
->raw_supply (i
, NULL
);
962 core_target::files_info ()
964 print_section_info (&m_core_section_table
, current_program_space
->core_bfd ());
967 /* Helper method for core_target::xfer_partial. */
969 enum target_xfer_status
970 core_target::xfer_memory_via_mappings (gdb_byte
*readbuf
,
971 const gdb_byte
*writebuf
,
972 ULONGEST offset
, ULONGEST len
,
973 ULONGEST
*xfered_len
)
975 enum target_xfer_status xfer_status
;
977 xfer_status
= (section_table_xfer_memory_partial
979 offset
, len
, xfered_len
,
980 m_core_file_mappings
));
982 if (xfer_status
== TARGET_XFER_OK
|| m_core_unavailable_mappings
.empty ())
985 /* There are instances - e.g. when debugging within a docker
986 container using the AUFS storage driver - where the pathnames
987 obtained from the note section are incorrect. Despite the path
988 being wrong, just knowing the start and end addresses of the
989 mappings is still useful; we can attempt an access of the file
990 stratum constrained to the address ranges corresponding to the
991 unavailable mappings. */
993 ULONGEST memaddr
= offset
;
994 ULONGEST memend
= offset
+ len
;
996 for (const auto &mr
: m_core_unavailable_mappings
)
998 if (mr
.contains (memaddr
))
1000 if (!mr
.contains (memend
))
1001 len
= mr
.start
+ mr
.length
- memaddr
;
1003 xfer_status
= this->beneath ()->xfer_partial (TARGET_OBJECT_MEMORY
,
1017 enum target_xfer_status
1018 core_target::xfer_partial (enum target_object object
, const char *annex
,
1019 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1020 ULONGEST offset
, ULONGEST len
, ULONGEST
*xfered_len
)
1024 case TARGET_OBJECT_MEMORY
:
1026 enum target_xfer_status xfer_status
;
1028 /* Try accessing memory contents from core file data,
1029 restricting consideration to those sections for which
1030 the BFD section flag SEC_HAS_CONTENTS is set. */
1031 auto has_contents_cb
= [] (const struct target_section
*s
)
1033 return ((s
->the_bfd_section
->flags
& SEC_HAS_CONTENTS
) != 0);
1035 xfer_status
= section_table_xfer_memory_partial
1037 offset
, len
, xfered_len
,
1038 m_core_section_table
,
1040 if (xfer_status
== TARGET_XFER_OK
)
1041 return TARGET_XFER_OK
;
1043 /* Check file backed mappings. If they're available, use
1044 core file provided mappings (e.g. from .note.linuxcore.file
1045 or the like) as this should provide a more accurate
1046 result. If not, check the stratum beneath us, which should
1047 be the file stratum.
1049 We also check unavailable mappings due to Docker/AUFS driver
1051 if (!m_core_file_mappings
.empty ()
1052 || !m_core_unavailable_mappings
.empty ())
1054 xfer_status
= xfer_memory_via_mappings (readbuf
, writebuf
, offset
,
1058 xfer_status
= this->beneath ()->xfer_partial (object
, annex
, readbuf
,
1059 writebuf
, offset
, len
,
1061 if (xfer_status
== TARGET_XFER_OK
)
1062 return TARGET_XFER_OK
;
1064 /* Finally, attempt to access data in core file sections with
1065 no contents. These will typically read as all zero. */
1066 auto no_contents_cb
= [&] (const struct target_section
*s
)
1068 return !has_contents_cb (s
);
1070 xfer_status
= section_table_xfer_memory_partial
1072 offset
, len
, xfered_len
,
1073 m_core_section_table
,
1078 case TARGET_OBJECT_AUXV
:
1081 /* When the aux vector is stored in core file, BFD
1082 represents this with a fake section called ".auxv". */
1084 struct bfd_section
*section
;
1087 section
= bfd_get_section_by_name (current_program_space
->core_bfd (),
1089 if (section
== NULL
)
1090 return TARGET_XFER_E_IO
;
1092 size
= bfd_section_size (section
);
1094 return TARGET_XFER_EOF
;
1100 return TARGET_XFER_EOF
;
1101 if (!bfd_get_section_contents (current_program_space
->core_bfd (),
1102 section
, readbuf
, (file_ptr
) offset
,
1105 warning (_("Couldn't read NT_AUXV note in core file."));
1106 return TARGET_XFER_E_IO
;
1109 *xfered_len
= (ULONGEST
) size
;
1110 return TARGET_XFER_OK
;
1112 return TARGET_XFER_E_IO
;
1114 case TARGET_OBJECT_WCOOKIE
:
1117 /* When the StackGhost cookie is stored in core file, BFD
1118 represents this with a fake section called
1121 struct bfd_section
*section
;
1124 section
= bfd_get_section_by_name (current_program_space
->core_bfd (),
1126 if (section
== NULL
)
1127 return TARGET_XFER_E_IO
;
1129 size
= bfd_section_size (section
);
1131 return TARGET_XFER_EOF
;
1137 return TARGET_XFER_EOF
;
1138 if (!bfd_get_section_contents (current_program_space
->core_bfd (),
1139 section
, readbuf
, (file_ptr
) offset
,
1142 warning (_("Couldn't read StackGhost cookie in core file."));
1143 return TARGET_XFER_E_IO
;
1146 *xfered_len
= (ULONGEST
) size
;
1147 return TARGET_XFER_OK
;
1150 return TARGET_XFER_E_IO
;
1152 case TARGET_OBJECT_LIBRARIES
:
1153 if (m_core_gdbarch
!= nullptr
1154 && gdbarch_core_xfer_shared_libraries_p (m_core_gdbarch
))
1157 return TARGET_XFER_E_IO
;
1160 *xfered_len
= gdbarch_core_xfer_shared_libraries (m_core_gdbarch
,
1164 if (*xfered_len
== 0)
1165 return TARGET_XFER_EOF
;
1167 return TARGET_XFER_OK
;
1170 return TARGET_XFER_E_IO
;
1172 case TARGET_OBJECT_LIBRARIES_AIX
:
1173 if (m_core_gdbarch
!= nullptr
1174 && gdbarch_core_xfer_shared_libraries_aix_p (m_core_gdbarch
))
1177 return TARGET_XFER_E_IO
;
1181 = gdbarch_core_xfer_shared_libraries_aix (m_core_gdbarch
,
1185 if (*xfered_len
== 0)
1186 return TARGET_XFER_EOF
;
1188 return TARGET_XFER_OK
;
1191 return TARGET_XFER_E_IO
;
1193 case TARGET_OBJECT_SIGNAL_INFO
:
1196 if (m_core_gdbarch
!= nullptr
1197 && gdbarch_core_xfer_siginfo_p (m_core_gdbarch
))
1199 LONGEST l
= gdbarch_core_xfer_siginfo (m_core_gdbarch
, readbuf
,
1206 return TARGET_XFER_EOF
;
1208 return TARGET_XFER_OK
;
1212 return TARGET_XFER_E_IO
;
1215 return this->beneath ()->xfer_partial (object
, annex
, readbuf
,
1216 writebuf
, offset
, len
,
1223 /* Okay, let's be honest: threads gleaned from a core file aren't
1224 exactly lively, are they? On the other hand, if we don't claim
1225 that each & every one is alive, then we don't get any of them
1226 to appear in an "info thread" command, which is quite a useful
1230 core_target::thread_alive (ptid_t ptid
)
1235 /* Ask the current architecture what it knows about this core file.
1236 That will be used, in turn, to pick a better architecture. This
1237 wrapper could be avoided if targets got a chance to specialize
1240 const struct target_desc
*
1241 core_target::read_description ()
1243 /* First check whether the target wants us to use the corefile target
1244 description notes. */
1245 if (gdbarch_use_target_description_from_corefile_notes
1246 (m_core_gdbarch
, current_program_space
->core_bfd ()))
1248 /* If the core file contains a target description note then go ahead and
1250 bfd_size_type tdesc_note_size
= 0;
1251 struct bfd_section
*tdesc_note_section
1252 = bfd_get_section_by_name (current_program_space
->core_bfd (), ".gdb-tdesc");
1253 if (tdesc_note_section
!= nullptr)
1254 tdesc_note_size
= bfd_section_size (tdesc_note_section
);
1255 if (tdesc_note_size
> 0)
1257 gdb::char_vector
contents (tdesc_note_size
+ 1);
1258 if (bfd_get_section_contents (current_program_space
->core_bfd (),
1259 tdesc_note_section
, contents
.data (),
1260 (file_ptr
) 0, tdesc_note_size
))
1262 /* Ensure we have a null terminator. */
1263 contents
[tdesc_note_size
] = '\0';
1264 const struct target_desc
*result
1265 = string_read_description_xml (contents
.data ());
1266 if (result
!= nullptr)
1272 /* If the architecture provides a corefile target description hook, use
1273 it now. Even if the core file contains a target description in a note
1274 section, it is not useful for targets that can potentially have distinct
1275 descriptions for each thread. One example is AArch64's SVE/SME
1276 extensions that allow per-thread vector length changes, resulting in
1277 registers with different sizes. */
1278 if (m_core_gdbarch
&& gdbarch_core_read_description_p (m_core_gdbarch
))
1280 const struct target_desc
*result
;
1282 result
= gdbarch_core_read_description
1283 (m_core_gdbarch
, this, current_program_space
->core_bfd ());
1284 if (result
!= nullptr)
1288 return this->beneath ()->read_description ();
1292 core_target::pid_to_str (ptid_t ptid
)
1294 struct inferior
*inf
;
1297 /* The preferred way is to have a gdbarch/OS specific
1299 if (m_core_gdbarch
!= nullptr
1300 && gdbarch_core_pid_to_str_p (m_core_gdbarch
))
1301 return gdbarch_core_pid_to_str (m_core_gdbarch
, ptid
);
1303 /* Otherwise, if we don't have one, we'll just fallback to
1304 "process", with normal_pid_to_str. */
1306 /* Try the LWPID field first. */
1309 return normal_pid_to_str (ptid_t (pid
));
1311 /* Otherwise, this isn't a "threaded" core -- use the PID field, but
1312 only if it isn't a fake PID. */
1313 inf
= find_inferior_ptid (this, ptid
);
1314 if (inf
!= NULL
&& !inf
->fake_pid_p
)
1315 return normal_pid_to_str (ptid
);
1317 /* No luck. We simply don't have a valid PID to print. */
1318 return "<main task>";
1322 core_target::thread_name (struct thread_info
*thr
)
1324 if (m_core_gdbarch
!= nullptr
1325 && gdbarch_core_thread_name_p (m_core_gdbarch
))
1326 return gdbarch_core_thread_name (m_core_gdbarch
, thr
);
1331 core_target::has_memory ()
1333 return current_program_space
->core_bfd () != nullptr;
1337 core_target::has_stack ()
1339 return current_program_space
->core_bfd () != nullptr;
1343 core_target::has_registers ()
1345 return current_program_space
->core_bfd () != nullptr;
1348 /* Implement the to_info_proc method. */
1351 core_target::info_proc (const char *args
, enum info_proc_what request
)
1353 struct gdbarch
*gdbarch
= get_current_arch ();
1355 /* Since this is the core file target, call the 'core_info_proc'
1356 method on gdbarch, not 'info_proc'. */
1357 if (gdbarch_core_info_proc_p (gdbarch
))
1358 gdbarch_core_info_proc (gdbarch
, args
, request
);
1363 /* Implementation of the "supports_memory_tagging" target_ops method. */
1366 core_target::supports_memory_tagging ()
1368 /* Look for memory tag sections. If they exist, that means this core file
1369 supports memory tagging. */
1371 return (bfd_get_section_by_name (current_program_space
->core_bfd (), "memtag")
1375 /* Implementation of the "fetch_memtags" target_ops method. */
1378 core_target::fetch_memtags (CORE_ADDR address
, size_t len
,
1379 gdb::byte_vector
&tags
, int type
)
1381 gdbarch
*gdbarch
= current_inferior ()->arch ();
1383 /* Make sure we have a way to decode the memory tag notes. */
1384 if (!gdbarch_decode_memtag_section_p (gdbarch
))
1385 error (_("gdbarch_decode_memtag_section not implemented for this "
1388 memtag_section_info info
;
1389 info
.memtag_section
= nullptr;
1391 while (get_next_core_memtag_section (current_program_space
->core_bfd (),
1392 info
.memtag_section
, address
, info
))
1394 size_t adjusted_length
1395 = (address
+ len
< info
.end_address
) ? len
: (info
.end_address
- address
);
1397 /* Decode the memory tag note and return the tags. */
1398 gdb::byte_vector tags_read
1399 = gdbarch_decode_memtag_section (gdbarch
, info
.memtag_section
, type
,
1400 address
, adjusted_length
);
1402 /* Transfer over the tags that have been read. */
1403 tags
.insert (tags
.end (), tags_read
.begin (), tags_read
.end ());
1405 /* ADDRESS + LEN may cross the boundaries of a particular memory tag
1406 segment. Check if we need to fetch tags from a different section. */
1407 if (!tags_read
.empty () && (address
+ len
) < info
.end_address
)
1410 /* There are more tags to fetch. Update ADDRESS and LEN. */
1411 len
-= (info
.end_address
- address
);
1412 address
= info
.end_address
;
1419 core_target::is_address_tagged (gdbarch
*gdbarch
, CORE_ADDR address
)
1421 return gdbarch_tagged_address_p (gdbarch
, address
);
1424 /* Implementation of the "fetch_x86_xsave_layout" target_ops method. */
1427 core_target::fetch_x86_xsave_layout ()
1429 if (m_core_gdbarch
!= nullptr &&
1430 gdbarch_core_read_x86_xsave_layout_p (m_core_gdbarch
))
1432 x86_xsave_layout layout
;
1433 if (!gdbarch_core_read_x86_xsave_layout (m_core_gdbarch
, layout
))
1442 /* Get a pointer to the current core target. If not connected to a
1443 core target, return NULL. */
1445 static core_target
*
1446 get_current_core_target ()
1448 target_ops
*proc_target
= current_inferior ()->process_target ();
1449 return dynamic_cast<core_target
*> (proc_target
);
1452 /* Display file backed mappings from core file. */
1455 core_target::info_proc_mappings (struct gdbarch
*gdbarch
)
1457 if (!m_core_file_mappings
.empty ())
1459 gdb_printf (_("Mapped address spaces:\n\n"));
1460 if (gdbarch_addr_bit (gdbarch
) == 32)
1462 gdb_printf ("\t%10s %10s %10s %10s %s\n",
1465 " Size", " Offset", "objfile");
1469 gdb_printf (" %18s %18s %10s %10s %s\n",
1472 " Size", " Offset", "objfile");
1476 for (const target_section
&tsp
: m_core_file_mappings
)
1478 ULONGEST start
= tsp
.addr
;
1479 ULONGEST end
= tsp
.endaddr
;
1480 ULONGEST file_ofs
= tsp
.the_bfd_section
->filepos
;
1481 const char *filename
= bfd_get_filename (tsp
.the_bfd_section
->owner
);
1483 if (gdbarch_addr_bit (gdbarch
) == 32)
1484 gdb_printf ("\t%10s %10s %10s %10s %s\n",
1485 paddress (gdbarch
, start
),
1486 paddress (gdbarch
, end
),
1487 hex_string (end
- start
),
1488 hex_string (file_ofs
),
1491 gdb_printf (" %18s %18s %10s %10s %s\n",
1492 paddress (gdbarch
, start
),
1493 paddress (gdbarch
, end
),
1494 hex_string (end
- start
),
1495 hex_string (file_ofs
),
1500 /* Implement "maintenance print core-file-backed-mappings" command.
1502 If mappings are loaded, the results should be similar to the
1503 mappings shown by "info proc mappings". This command is mainly a
1504 debugging tool for GDB developers to make sure that the expected
1505 mappings are present after loading a core file. For Linux, the
1506 output provided by this command will be very similar (if not
1507 identical) to that provided by "info proc mappings". This is not
1508 necessarily the case for other OSes which might provide
1509 more/different information in the "info proc mappings" output. */
1512 maintenance_print_core_file_backed_mappings (const char *args
, int from_tty
)
1514 core_target
*targ
= get_current_core_target ();
1515 if (targ
!= nullptr)
1516 targ
->info_proc_mappings (targ
->core_gdbarch ());
1519 void _initialize_corelow ();
1521 _initialize_corelow ()
1523 add_target (core_target_info
, core_target_open
, filename_completer
);
1524 add_cmd ("core-file-backed-mappings", class_maintenance
,
1525 maintenance_print_core_file_backed_mappings
,
1526 _("Print core file's file-backed mappings."),
1527 &maintenanceprintlist
);