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 "debuginfod-support.h"
51 #include <unordered_map>
52 #include <unordered_set>
54 #include "xml-tdesc.h"
61 /* The core file target. */
63 static const target_info core_target_info
= {
65 N_("Local core dump file"),
66 N_("Use a core file as a target.\n\
67 Specify the filename of the core file.")
70 class core_target final
: public process_stratum_target
75 const target_info
&info () const override
76 { return core_target_info
; }
78 void close () override
;
79 void detach (inferior
*, int) override
;
80 void fetch_registers (struct regcache
*, int) override
;
82 enum target_xfer_status
xfer_partial (enum target_object object
,
85 const gdb_byte
*writebuf
,
86 ULONGEST offset
, ULONGEST len
,
87 ULONGEST
*xfered_len
) override
;
88 void files_info () override
;
90 bool thread_alive (ptid_t ptid
) override
;
91 const struct target_desc
*read_description () override
;
93 std::string
pid_to_str (ptid_t
) override
;
95 const char *thread_name (struct thread_info
*) override
;
97 bool has_all_memory () override
{ return true; }
98 bool has_memory () override
;
99 bool has_stack () override
;
100 bool has_registers () override
;
101 bool has_execution (inferior
*inf
) override
{ return false; }
103 bool info_proc (const char *, enum info_proc_what
) override
;
105 bool supports_memory_tagging () override
;
107 /* Core file implementation of fetch_memtags. Fetch the memory tags from
109 bool fetch_memtags (CORE_ADDR address
, size_t len
,
110 gdb::byte_vector
&tags
, int type
) override
;
114 /* Getter, see variable definition. */
115 struct gdbarch
*core_gdbarch ()
117 return m_core_gdbarch
;
120 /* See definition. */
121 void get_core_register_section (struct regcache
*regcache
,
122 const struct regset
*regset
,
124 int section_min_size
,
125 const char *human_name
,
128 /* See definition. */
129 void info_proc_mappings (struct gdbarch
*gdbarch
);
131 private: /* per-core data */
133 /* Get rid of the core inferior. */
136 /* The core's section table. Note that these target sections are
137 *not* mapped in the current address spaces' set of target
138 sections --- those should come only from pure executable or
139 shared library bfds. The core bfd sections are an implementation
140 detail of the core target, just like ptrace is for unix child
142 target_section_table m_core_section_table
;
144 /* File-backed address space mappings: some core files include
145 information about memory mapped files. */
146 target_section_table m_core_file_mappings
;
148 /* Unavailable mappings. These correspond to pathnames which either
149 weren't found or could not be opened. Knowing these addresses can
151 std::vector
<mem_range
> m_core_unavailable_mappings
;
153 /* Build m_core_file_mappings. Called from the constructor. */
154 void build_file_mappings ();
156 /* Helper method for xfer_partial. */
157 enum target_xfer_status
xfer_memory_via_mappings (gdb_byte
*readbuf
,
158 const gdb_byte
*writebuf
,
161 ULONGEST
*xfered_len
);
163 /* FIXME: kettenis/20031023: Eventually this field should
165 struct gdbarch
*m_core_gdbarch
= NULL
;
168 core_target::core_target ()
170 /* Find a first arch based on the BFD. We need the initial gdbarch so
171 we can setup the hooks to find a target description. */
172 m_core_gdbarch
= gdbarch_from_bfd (core_bfd
);
174 /* If the arch is able to read a target description from the core, it
175 could yield a more specific gdbarch. */
176 const struct target_desc
*tdesc
= read_description ();
178 if (tdesc
!= nullptr)
180 struct gdbarch_info info
;
181 info
.abfd
= core_bfd
;
182 info
.target_desc
= tdesc
;
183 m_core_gdbarch
= gdbarch_find_by_info (info
);
187 || !gdbarch_iterate_over_regset_sections_p (m_core_gdbarch
))
188 error (_("\"%s\": Core file format not supported"),
189 bfd_get_filename (core_bfd
));
191 /* Find the data section */
192 m_core_section_table
= build_section_table (core_bfd
);
194 build_file_mappings ();
197 /* Construct the target_section_table for file-backed mappings if
200 For each unique path in the note, we'll open a BFD with a bfd
201 target of "binary". This is an unstructured bfd target upon which
202 we'll impose a structure from the mappings in the architecture-specific
203 mappings note. A BFD section is allocated and initialized for each
206 We take care to not share already open bfds with other parts of
207 GDB; in particular, we don't want to add new sections to existing
208 BFDs. We do, however, ensure that the BFDs that we allocate here
209 will go away (be deallocated) when the core target is detached. */
212 core_target::build_file_mappings ()
214 std::unordered_map
<std::string
, struct bfd
*> bfd_map
;
215 std::unordered_set
<std::string
> unavailable_paths
;
217 /* See linux_read_core_file_mappings() in linux-tdep.c for an example
218 read_core_file_mappings method. */
219 gdbarch_read_core_file_mappings (m_core_gdbarch
, core_bfd
,
221 /* After determining the number of mappings, read_core_file_mappings
222 will invoke this lambda. */
227 /* read_core_file_mappings will invoke this lambda for each mapping
229 [&] (int num
, ULONGEST start
, ULONGEST end
, ULONGEST file_ofs
,
230 const char *filename
, const bfd_build_id
*build_id
)
232 /* Architecture-specific read_core_mapping methods are expected to
233 weed out non-file-backed mappings. */
234 gdb_assert (filename
!= nullptr);
236 if (unavailable_paths
.find (filename
) != unavailable_paths
.end ())
238 /* We have already seen some mapping for FILENAME but failed to
239 find/open the file. There is no point in trying the same
240 thing again so just record that the range [start, end) is
242 m_core_unavailable_mappings
.emplace_back (start
, end
- start
);
246 struct bfd
*bfd
= bfd_map
[filename
];
249 /* Use exec_file_find() to do sysroot expansion. It'll
250 also strip the potential sysroot "target:" prefix. If
251 there is no sysroot, an equivalent (possibly more
252 canonical) pathname will be provided. */
253 gdb::unique_xmalloc_ptr
<char> expanded_fname
254 = exec_file_find (filename
, NULL
);
256 if (expanded_fname
== nullptr && build_id
!= nullptr)
257 debuginfod_exec_query (build_id
->data
, build_id
->size
,
258 filename
, &expanded_fname
);
260 if (expanded_fname
== nullptr)
262 m_core_unavailable_mappings
.emplace_back (start
, end
- start
);
263 unavailable_paths
.insert (filename
);
264 warning (_("Can't open file %s during file-backed mapping "
270 bfd
= bfd_openr (expanded_fname
.get (), "binary");
272 if (bfd
== nullptr || !bfd_check_format (bfd
, bfd_object
))
274 m_core_unavailable_mappings
.emplace_back (start
, end
- start
);
275 unavailable_paths
.insert (filename
);
276 warning (_("Can't open file %s which was expanded to %s "
277 "during file-backed mapping note processing"),
278 filename
, expanded_fname
.get ());
284 /* Ensure that the bfd will be closed when core_bfd is closed.
285 This can be checked before/after a core file detach via
286 "maint info bfds". */
287 gdb_bfd_record_inclusion (core_bfd
, bfd
);
288 bfd_map
[filename
] = bfd
;
291 /* Make new BFD section. All sections have the same name,
292 which is permitted by bfd_make_section_anyway(). */
293 asection
*sec
= bfd_make_section_anyway (bfd
, "load");
295 error (_("Can't make section"));
296 sec
->filepos
= file_ofs
;
297 bfd_set_section_flags (sec
, SEC_READONLY
| SEC_HAS_CONTENTS
);
298 bfd_set_section_size (sec
, end
- start
);
299 bfd_set_section_vma (sec
, start
);
300 bfd_set_section_lma (sec
, start
);
301 bfd_set_section_alignment (sec
, 2);
303 /* Set target_section fields. */
304 m_core_file_mappings
.emplace_back (start
, end
, sec
);
306 /* If this is a bfd of a shared library, record its soname
308 if (build_id
!= nullptr)
310 gdb::unique_xmalloc_ptr
<char> soname
311 = gdb_bfd_read_elf_soname (bfd
->filename
);
312 if (soname
!= nullptr)
313 set_cbfd_soname_build_id (current_program_space
->cbfd
,
314 soname
.get (), build_id
);
318 normalize_mem_ranges (&m_core_unavailable_mappings
);
321 /* An arbitrary identifier for the core inferior. */
322 #define CORELOW_PID 1
325 core_target::clear_core ()
329 switch_to_no_thread (); /* Avoid confusion from thread
331 exit_inferior (current_inferior ());
333 /* Clear out solib state while the bfd is still open. See
334 comments in clear_solib in solib.c. */
337 current_program_space
->cbfd
.reset (nullptr);
341 /* Close the core target. */
344 core_target::close ()
348 /* Core targets are heap-allocated (see core_target_open), so here
349 we delete ourselves. */
353 /* Look for sections whose names start with `.reg/' so that we can
354 extract the list of threads in a core file. */
356 /* If ASECT is a section whose name begins with '.reg/' then extract the
357 lwpid after the '/' and create a new thread in INF.
359 If REG_SECT is not nullptr, and the both ASECT and REG_SECT point at the
360 same position in the parent bfd object then switch to the newly created
361 thread, otherwise, the selected thread is left unchanged. */
364 add_to_thread_list (asection
*asect
, asection
*reg_sect
, inferior
*inf
)
366 if (!startswith (bfd_section_name (asect
), ".reg/"))
369 int lwpid
= atoi (bfd_section_name (asect
) + 5);
370 ptid_t
ptid (inf
->pid
, lwpid
);
371 thread_info
*thr
= add_thread (inf
->process_target (), ptid
);
373 /* Warning, Will Robinson, looking at BFD private data! */
376 && asect
->filepos
== reg_sect
->filepos
) /* Did we find .reg? */
377 switch_to_thread (thr
); /* Yes, make it current. */
380 /* Issue a message saying we have no core to debug, if FROM_TTY. */
383 maybe_say_no_core_file_now (int from_tty
)
386 gdb_printf (_("No core file now.\n"));
389 /* Backward compatibility with old way of specifying core files. */
392 core_file_command (const char *filename
, int from_tty
)
394 dont_repeat (); /* Either way, seems bogus. */
396 if (filename
== NULL
)
398 if (core_bfd
!= NULL
)
400 target_detach (current_inferior (), from_tty
);
401 gdb_assert (core_bfd
== NULL
);
404 maybe_say_no_core_file_now (from_tty
);
407 core_target_open (filename
, from_tty
);
410 /* A vmcore file is a core file created by the Linux kernel at the point of
411 a crash. Each thread in the core file represents a real CPU core, and
412 the lwpid for each thread is the pid of the process that was running on
413 that core at the moment of the crash.
415 However, not every CPU core will have been running a process, some cores
416 will be idle. For these idle cores the CPU writes an lwpid of 0. And
417 of course, multiple cores might be idle, so there could be multiple
418 threads with an lwpid of 0.
420 The problem is GDB doesn't really like threads with an lwpid of 0; GDB
421 presents such a thread as a process rather than a thread. And GDB
422 certainly doesn't like multiple threads having the same lwpid, each time
423 a new thread is seen with the same lwpid the earlier thread (with the
424 same lwpid) will be deleted.
426 This function addresses both of these problems by assigning a fake lwpid
427 to any thread with an lwpid of 0.
429 GDB finds the lwpid information by looking at the bfd section names
430 which include the lwpid, e.g. .reg/NN where NN is the lwpid. This
431 function looks though all the section names looking for sections named
432 .reg/NN. If any sections are found where NN == 0, then we assign a new
433 unique value of NN. Then, in a second pass, any sections ending /0 are
434 assigned their new number.
436 Remember, a core file may contain multiple register sections for
437 different register sets, but the sets are always grouped by thread, so
438 we can figure out which registers should be assigned the same new
439 lwpid. For example, consider a core file containing:
441 .reg/0, .reg2/0, .reg/0, .reg2/0
443 This represents two threads, each thread contains a .reg and .reg2
444 register set. The .reg represents the start of each thread. After
445 renaming the sections will now look like this:
447 .reg/1, .reg2/1, .reg/2, .reg2/2
449 After calling this function the rest of the core file handling code can
450 treat this core file just like any other core file. */
453 rename_vmcore_idle_reg_sections (bfd
*abfd
, inferior
*inf
)
455 /* Map from the bfd section to its lwpid (the /NN number). */
456 std::vector
<std::pair
<asection
*, int>> sections_and_lwpids
;
458 /* The set of all /NN numbers found. Needed so we can easily find unused
459 numbers in the case that we need to rename some sections. */
460 std::unordered_set
<int> all_lwpids
;
462 /* A count of how many sections called .reg/0 we have found. */
463 unsigned zero_lwpid_count
= 0;
465 /* Look for all the .reg sections. Record the section object and the
466 lwpid which is extracted from the section name. Spot if any have an
468 for (asection
*sect
: gdb_bfd_sections (core_bfd
))
470 if (startswith (bfd_section_name (sect
), ".reg/"))
472 int lwpid
= atoi (bfd_section_name (sect
) + 5);
473 sections_and_lwpids
.emplace_back (sect
, lwpid
);
474 all_lwpids
.insert (lwpid
);
480 /* If every ".reg/NN" section has a non-zero lwpid then we don't need to
482 if (zero_lwpid_count
== 0)
485 /* Assign a new number to any .reg sections with an lwpid of 0. */
487 for (auto §_and_lwpid
: sections_and_lwpids
)
488 if (sect_and_lwpid
.second
== 0)
490 while (all_lwpids
.find (new_lwpid
) != all_lwpids
.end ())
492 sect_and_lwpid
.second
= new_lwpid
;
496 /* Now update the names of any sections with an lwpid of 0. This is
497 more than just the .reg sections we originally found. */
498 std::string replacement_lwpid_str
;
499 auto iter
= sections_and_lwpids
.begin ();
500 int replacement_lwpid
= 0;
501 for (asection
*sect
: gdb_bfd_sections (core_bfd
))
503 if (iter
!= sections_and_lwpids
.end () && sect
== iter
->first
)
505 gdb_assert (startswith (bfd_section_name (sect
), ".reg/"));
507 int lwpid
= atoi (bfd_section_name (sect
) + 5);
508 if (lwpid
== iter
->second
)
510 /* This section was not given a new number. */
511 gdb_assert (lwpid
!= 0);
512 replacement_lwpid
= 0;
516 replacement_lwpid
= iter
->second
;
517 ptid_t
ptid (inf
->pid
, replacement_lwpid
);
518 if (!replacement_lwpid_str
.empty ())
519 replacement_lwpid_str
+= ", ";
520 replacement_lwpid_str
+= target_pid_to_str (ptid
);
526 if (replacement_lwpid
!= 0)
528 const char *name
= bfd_section_name (sect
);
529 size_t len
= strlen (name
);
531 if (strncmp (name
+ len
- 2, "/0", 2) == 0)
533 /* This section needs a new name. */
535 = string_printf ("%.*s/%d",
536 static_cast<int> (len
- 2),
537 name
, replacement_lwpid
);
539 = static_cast<char *> (bfd_alloc (abfd
, name_str
.size () + 1));
540 if (name_buf
== nullptr)
541 error (_("failed to allocate space for section name '%s'"),
543 memcpy (name_buf
, name_str
.c_str(), name_str
.size () + 1);
544 bfd_rename_section (sect
, name_buf
);
549 if (zero_lwpid_count
== 1)
550 warning (_("found thread with pid 0, assigned replacement Target Id: %s"),
551 replacement_lwpid_str
.c_str ());
553 warning (_("found threads with pid 0, assigned replacement Target Ids: %s"),
554 replacement_lwpid_str
.c_str ());
557 /* Locate (and load) an executable file (and symbols) given the core file
561 locate_exec_from_corefile_build_id (bfd
*abfd
, int from_tty
)
563 const bfd_build_id
*build_id
= build_id_bfd_get (abfd
);
564 if (build_id
== nullptr)
567 gdb_bfd_ref_ptr execbfd
568 = build_id_to_exec_bfd (build_id
->size
, build_id
->data
);
570 if (execbfd
== nullptr)
572 /* Attempt to query debuginfod for the executable. */
573 gdb::unique_xmalloc_ptr
<char> execpath
;
574 scoped_fd fd
= debuginfod_exec_query (build_id
->data
, build_id
->size
,
575 abfd
->filename
, &execpath
);
579 execbfd
= gdb_bfd_open (execpath
.get (), gnutarget
);
581 if (execbfd
== nullptr)
582 warning (_("\"%s\" from debuginfod cannot be opened as bfd: %s"),
584 gdb_bfd_errmsg (bfd_get_error (), nullptr).c_str ());
585 else if (!build_id_verify (execbfd
.get (), build_id
->size
,
587 execbfd
.reset (nullptr);
591 if (execbfd
!= nullptr)
593 exec_file_attach (bfd_get_filename (execbfd
.get ()), from_tty
);
594 symbol_file_add_main (bfd_get_filename (execbfd
.get ()),
595 symfile_add_flag (from_tty
? SYMFILE_VERBOSE
: 0));
602 core_target_open (const char *arg
, int from_tty
)
609 target_preopen (from_tty
);
613 error (_("No core file specified. (Use `detach' "
614 "to stop debugging a core file.)"));
616 error (_("No core file specified."));
619 gdb::unique_xmalloc_ptr
<char> filename (tilde_expand (arg
));
620 if (strlen (filename
.get ()) != 0
621 && !IS_ABSOLUTE_PATH (filename
.get ()))
622 filename
= make_unique_xstrdup (gdb_abspath (filename
.get ()).c_str ());
624 flags
= O_BINARY
| O_LARGEFILE
;
629 scratch_chan
= gdb_open_cloexec (filename
.get (), flags
, 0).release ();
630 if (scratch_chan
< 0)
631 perror_with_name (filename
.get ());
633 gdb_bfd_ref_ptr
temp_bfd (gdb_bfd_fopen (filename
.get (), gnutarget
,
634 write_files
? FOPEN_RUB
: FOPEN_RB
,
636 if (temp_bfd
== NULL
)
637 perror_with_name (filename
.get ());
639 if (!bfd_check_format (temp_bfd
.get (), bfd_core
))
641 /* Do it after the err msg */
642 /* FIXME: should be checking for errors from bfd_close (for one
643 thing, on error it does not free all the storage associated
645 error (_("\"%s\" is not a core dump: %s"),
646 filename
.get (), bfd_errmsg (bfd_get_error ()));
649 current_program_space
->cbfd
= std::move (temp_bfd
);
651 core_target
*target
= new core_target ();
653 /* Own the target until it is successfully pushed. */
654 target_ops_up
target_holder (target
);
658 /* If we have no exec file, try to set the architecture from the
659 core file. We don't do this unconditionally since an exec file
660 typically contains more information that helps us determine the
661 architecture than a core file. */
662 if (!current_program_space
->exec_bfd ())
663 set_gdbarch_from_file (core_bfd
);
665 current_inferior ()->push_target (std::move (target_holder
));
667 switch_to_no_thread ();
669 /* Need to flush the register cache (and the frame cache) from a
670 previous debug session. If inferior_ptid ends up the same as the
671 last debug session --- e.g., b foo; run; gcore core1; step; gcore
672 core2; core core1; core core2 --- then there's potential for
673 get_current_regcache to return the cached regcache of the
674 previous session, and the frame cache being stale. */
675 registers_changed ();
677 /* Find (or fake) the pid for the process in this core file, and
678 initialise the current inferior with that pid. */
679 bool fake_pid_p
= false;
680 int pid
= bfd_core_file_pid (core_bfd
);
687 inferior
*inf
= current_inferior ();
688 gdb_assert (inf
->pid
== 0);
689 inferior_appeared (inf
, pid
);
690 inf
->fake_pid_p
= fake_pid_p
;
692 /* Rename any .reg/0 sections, giving them each a fake lwpid. */
693 rename_vmcore_idle_reg_sections (core_bfd
, inf
);
695 /* Build up thread list from BFD sections, and possibly set the
696 current thread to the .reg/NN section matching the .reg
698 asection
*reg_sect
= bfd_get_section_by_name (core_bfd
, ".reg");
699 for (asection
*sect
: gdb_bfd_sections (core_bfd
))
700 add_to_thread_list (sect
, reg_sect
, inf
);
702 if (inferior_ptid
== null_ptid
)
704 /* Either we found no .reg/NN section, and hence we have a
705 non-threaded core (single-threaded, from gdb's perspective),
706 or for some reason add_to_thread_list couldn't determine
707 which was the "main" thread. The latter case shouldn't
708 usually happen, but we're dealing with input here, which can
709 always be broken in different ways. */
710 thread_info
*thread
= first_thread_of_inferior (inf
);
713 thread
= add_thread_silent (target
, ptid_t (CORELOW_PID
));
715 switch_to_thread (thread
);
718 if (current_program_space
->exec_bfd () == nullptr)
719 locate_exec_from_corefile_build_id (core_bfd
, from_tty
);
721 post_create_inferior (from_tty
);
723 /* Now go through the target stack looking for threads since there
724 may be a thread_stratum target loaded on top of target core by
725 now. The layer above should claim threads found in the BFD
729 target_update_thread_list ();
732 catch (const gdb_exception_error
&except
)
734 exception_print (gdb_stderr
, except
);
737 p
= bfd_core_file_failing_command (core_bfd
);
739 gdb_printf (_("Core was generated by `%s'.\n"), p
);
741 /* Clearing any previous state of convenience variables. */
742 clear_exit_convenience_vars ();
744 siggy
= bfd_core_file_failing_signal (core_bfd
);
747 gdbarch
*core_gdbarch
= target
->core_gdbarch ();
749 /* If we don't have a CORE_GDBARCH to work with, assume a native
750 core (map gdb_signal from host signals). If we do have
751 CORE_GDBARCH to work with, but no gdb_signal_from_target
752 implementation for that gdbarch, as a fallback measure,
753 assume the host signal mapping. It'll be correct for native
754 cores, but most likely incorrect for cross-cores. */
755 enum gdb_signal sig
= (core_gdbarch
!= NULL
756 && gdbarch_gdb_signal_from_target_p (core_gdbarch
)
757 ? gdbarch_gdb_signal_from_target (core_gdbarch
,
759 : gdb_signal_from_host (siggy
));
761 gdb_printf (_("Program terminated with signal %s, %s"),
762 gdb_signal_to_name (sig
), gdb_signal_to_string (sig
));
763 if (gdbarch_report_signal_info_p (core_gdbarch
))
764 gdbarch_report_signal_info (core_gdbarch
, current_uiout
, sig
);
765 gdb_printf (_(".\n"));
767 /* Set the value of the internal variable $_exitsignal,
768 which holds the signal uncaught by the inferior. */
769 set_internalvar_integer (lookup_internalvar ("_exitsignal"),
773 /* Fetch all registers from core file. */
774 target_fetch_registers (get_current_regcache (), -1);
776 /* Now, set up the frame cache, and print the top of stack. */
777 reinit_frame_cache ();
778 print_stack_frame (get_selected_frame (NULL
), 1, SRC_AND_LOC
, 1);
780 /* Current thread should be NUM 1 but the user does not know that.
781 If a program is single threaded gdb in general does not mention
782 anything about threads. That is why the test is >= 2. */
783 if (thread_count (target
) >= 2)
787 thread_command (NULL
, from_tty
);
789 catch (const gdb_exception_error
&except
)
791 exception_print (gdb_stderr
, except
);
797 core_target::detach (inferior
*inf
, int from_tty
)
799 /* Get rid of the core. Don't rely on core_target::close doing it,
800 because target_detach may be called with core_target's refcount > 1,
801 meaning core_target::close may not be called yet by the
802 unpush_target call below. */
805 /* Note that 'this' may be dangling after this call. unpush_target
806 closes the target if the refcount reaches 0, and our close
807 implementation deletes 'this'. */
808 inf
->unpush_target (this);
810 /* Clear the register cache and the frame cache. */
811 registers_changed ();
812 reinit_frame_cache ();
813 maybe_say_no_core_file_now (from_tty
);
816 /* Try to retrieve registers from a section in core_bfd, and supply
819 If ptid's lwp member is zero, do the single-threaded
820 thing: look for a section named NAME. If ptid's lwp
821 member is non-zero, do the multi-threaded thing: look for a section
822 named "NAME/LWP", where LWP is the shortest ASCII decimal
823 representation of ptid's lwp member.
825 HUMAN_NAME is a human-readable name for the kind of registers the
826 NAME section contains, for use in error messages.
828 If REQUIRED is true, print an error if the core file doesn't have a
829 section by the appropriate name. Otherwise, just do nothing. */
832 core_target::get_core_register_section (struct regcache
*regcache
,
833 const struct regset
*regset
,
835 int section_min_size
,
836 const char *human_name
,
839 gdb_assert (regset
!= nullptr);
841 struct bfd_section
*section
;
843 bool variable_size_section
= (regset
->flags
& REGSET_VARIABLE_SIZE
);
845 thread_section_name
section_name (name
, regcache
->ptid ());
847 section
= bfd_get_section_by_name (core_bfd
, section_name
.c_str ());
851 warning (_("Couldn't find %s registers in core file."),
856 size
= bfd_section_size (section
);
857 if (size
< section_min_size
)
859 warning (_("Section `%s' in core file too small."),
860 section_name
.c_str ());
863 if (size
!= section_min_size
&& !variable_size_section
)
865 warning (_("Unexpected size of section `%s' in core file."),
866 section_name
.c_str ());
869 gdb::byte_vector
contents (size
);
870 if (!bfd_get_section_contents (core_bfd
, section
, contents
.data (),
873 warning (_("Couldn't read %s registers from `%s' section in core file."),
874 human_name
, section_name
.c_str ());
878 regset
->supply_regset (regset
, regcache
, -1, contents
.data (), size
);
881 /* Data passed to gdbarch_iterate_over_regset_sections's callback. */
882 struct get_core_registers_cb_data
885 struct regcache
*regcache
;
888 /* Callback for get_core_registers that handles a single core file
889 register note section. */
892 get_core_registers_cb (const char *sect_name
, int supply_size
, int collect_size
,
893 const struct regset
*regset
,
894 const char *human_name
, void *cb_data
)
896 gdb_assert (regset
!= nullptr);
898 auto *data
= (get_core_registers_cb_data
*) cb_data
;
899 bool required
= false;
900 bool variable_size_section
= (regset
->flags
& REGSET_VARIABLE_SIZE
);
902 if (!variable_size_section
)
903 gdb_assert (supply_size
== collect_size
);
905 if (strcmp (sect_name
, ".reg") == 0)
908 if (human_name
== NULL
)
909 human_name
= "general-purpose";
911 else if (strcmp (sect_name
, ".reg2") == 0)
913 if (human_name
== NULL
)
914 human_name
= "floating-point";
917 data
->target
->get_core_register_section (data
->regcache
, regset
, sect_name
,
918 supply_size
, human_name
, required
);
921 /* Get the registers out of a core file. This is the machine-
922 independent part. Fetch_core_registers is the machine-dependent
923 part, typically implemented in the xm-file for each
926 /* We just get all the registers, so we don't use regno. */
929 core_target::fetch_registers (struct regcache
*regcache
, int regno
)
931 if (!(m_core_gdbarch
!= nullptr
932 && gdbarch_iterate_over_regset_sections_p (m_core_gdbarch
)))
934 gdb_printf (gdb_stderr
,
935 "Can't fetch registers from this type of core file\n");
939 struct gdbarch
*gdbarch
= regcache
->arch ();
940 get_core_registers_cb_data data
= { this, regcache
};
941 gdbarch_iterate_over_regset_sections (gdbarch
,
942 get_core_registers_cb
,
943 (void *) &data
, NULL
);
945 /* Mark all registers not found in the core as unavailable. */
946 for (int i
= 0; i
< gdbarch_num_regs (regcache
->arch ()); i
++)
947 if (regcache
->get_register_status (i
) == REG_UNKNOWN
)
948 regcache
->raw_supply (i
, NULL
);
952 core_target::files_info ()
954 print_section_info (&m_core_section_table
, core_bfd
);
957 /* Helper method for core_target::xfer_partial. */
959 enum target_xfer_status
960 core_target::xfer_memory_via_mappings (gdb_byte
*readbuf
,
961 const gdb_byte
*writebuf
,
962 ULONGEST offset
, ULONGEST len
,
963 ULONGEST
*xfered_len
)
965 enum target_xfer_status xfer_status
;
967 xfer_status
= (section_table_xfer_memory_partial
969 offset
, len
, xfered_len
,
970 m_core_file_mappings
));
972 if (xfer_status
== TARGET_XFER_OK
|| m_core_unavailable_mappings
.empty ())
975 /* There are instances - e.g. when debugging within a docker
976 container using the AUFS storage driver - where the pathnames
977 obtained from the note section are incorrect. Despite the path
978 being wrong, just knowing the start and end addresses of the
979 mappings is still useful; we can attempt an access of the file
980 stratum constrained to the address ranges corresponding to the
981 unavailable mappings. */
983 ULONGEST memaddr
= offset
;
984 ULONGEST memend
= offset
+ len
;
986 for (const auto &mr
: m_core_unavailable_mappings
)
988 if (address_in_mem_range (memaddr
, &mr
))
990 if (!address_in_mem_range (memend
, &mr
))
991 len
= mr
.start
+ mr
.length
- memaddr
;
993 xfer_status
= this->beneath ()->xfer_partial (TARGET_OBJECT_MEMORY
,
1007 enum target_xfer_status
1008 core_target::xfer_partial (enum target_object object
, const char *annex
,
1009 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1010 ULONGEST offset
, ULONGEST len
, ULONGEST
*xfered_len
)
1014 case TARGET_OBJECT_MEMORY
:
1016 enum target_xfer_status xfer_status
;
1018 /* Try accessing memory contents from core file data,
1019 restricting consideration to those sections for which
1020 the BFD section flag SEC_HAS_CONTENTS is set. */
1021 auto has_contents_cb
= [] (const struct target_section
*s
)
1023 return ((s
->the_bfd_section
->flags
& SEC_HAS_CONTENTS
) != 0);
1025 xfer_status
= section_table_xfer_memory_partial
1027 offset
, len
, xfered_len
,
1028 m_core_section_table
,
1030 if (xfer_status
== TARGET_XFER_OK
)
1031 return TARGET_XFER_OK
;
1033 /* Check file backed mappings. If they're available, use
1034 core file provided mappings (e.g. from .note.linuxcore.file
1035 or the like) as this should provide a more accurate
1036 result. If not, check the stratum beneath us, which should
1037 be the file stratum.
1039 We also check unavailable mappings due to Docker/AUFS driver
1041 if (!m_core_file_mappings
.empty ()
1042 || !m_core_unavailable_mappings
.empty ())
1044 xfer_status
= xfer_memory_via_mappings (readbuf
, writebuf
, offset
,
1048 xfer_status
= this->beneath ()->xfer_partial (object
, annex
, readbuf
,
1049 writebuf
, offset
, len
,
1051 if (xfer_status
== TARGET_XFER_OK
)
1052 return TARGET_XFER_OK
;
1054 /* Finally, attempt to access data in core file sections with
1055 no contents. These will typically read as all zero. */
1056 auto no_contents_cb
= [&] (const struct target_section
*s
)
1058 return !has_contents_cb (s
);
1060 xfer_status
= section_table_xfer_memory_partial
1062 offset
, len
, xfered_len
,
1063 m_core_section_table
,
1068 case TARGET_OBJECT_AUXV
:
1071 /* When the aux vector is stored in core file, BFD
1072 represents this with a fake section called ".auxv". */
1074 struct bfd_section
*section
;
1077 section
= bfd_get_section_by_name (core_bfd
, ".auxv");
1078 if (section
== NULL
)
1079 return TARGET_XFER_E_IO
;
1081 size
= bfd_section_size (section
);
1083 return TARGET_XFER_EOF
;
1089 return TARGET_XFER_EOF
;
1090 if (!bfd_get_section_contents (core_bfd
, section
, readbuf
,
1091 (file_ptr
) offset
, size
))
1093 warning (_("Couldn't read NT_AUXV note in core file."));
1094 return TARGET_XFER_E_IO
;
1097 *xfered_len
= (ULONGEST
) size
;
1098 return TARGET_XFER_OK
;
1100 return TARGET_XFER_E_IO
;
1102 case TARGET_OBJECT_WCOOKIE
:
1105 /* When the StackGhost cookie is stored in core file, BFD
1106 represents this with a fake section called
1109 struct bfd_section
*section
;
1112 section
= bfd_get_section_by_name (core_bfd
, ".wcookie");
1113 if (section
== NULL
)
1114 return TARGET_XFER_E_IO
;
1116 size
= bfd_section_size (section
);
1118 return TARGET_XFER_EOF
;
1124 return TARGET_XFER_EOF
;
1125 if (!bfd_get_section_contents (core_bfd
, section
, readbuf
,
1126 (file_ptr
) offset
, size
))
1128 warning (_("Couldn't read StackGhost cookie in core file."));
1129 return TARGET_XFER_E_IO
;
1132 *xfered_len
= (ULONGEST
) size
;
1133 return TARGET_XFER_OK
;
1136 return TARGET_XFER_E_IO
;
1138 case TARGET_OBJECT_LIBRARIES
:
1139 if (m_core_gdbarch
!= nullptr
1140 && gdbarch_core_xfer_shared_libraries_p (m_core_gdbarch
))
1143 return TARGET_XFER_E_IO
;
1146 *xfered_len
= gdbarch_core_xfer_shared_libraries (m_core_gdbarch
,
1150 if (*xfered_len
== 0)
1151 return TARGET_XFER_EOF
;
1153 return TARGET_XFER_OK
;
1156 return TARGET_XFER_E_IO
;
1158 case TARGET_OBJECT_LIBRARIES_AIX
:
1159 if (m_core_gdbarch
!= nullptr
1160 && gdbarch_core_xfer_shared_libraries_aix_p (m_core_gdbarch
))
1163 return TARGET_XFER_E_IO
;
1167 = gdbarch_core_xfer_shared_libraries_aix (m_core_gdbarch
,
1171 if (*xfered_len
== 0)
1172 return TARGET_XFER_EOF
;
1174 return TARGET_XFER_OK
;
1177 return TARGET_XFER_E_IO
;
1179 case TARGET_OBJECT_SIGNAL_INFO
:
1182 if (m_core_gdbarch
!= nullptr
1183 && gdbarch_core_xfer_siginfo_p (m_core_gdbarch
))
1185 LONGEST l
= gdbarch_core_xfer_siginfo (m_core_gdbarch
, readbuf
,
1192 return TARGET_XFER_EOF
;
1194 return TARGET_XFER_OK
;
1198 return TARGET_XFER_E_IO
;
1201 return this->beneath ()->xfer_partial (object
, annex
, readbuf
,
1202 writebuf
, offset
, len
,
1209 /* Okay, let's be honest: threads gleaned from a core file aren't
1210 exactly lively, are they? On the other hand, if we don't claim
1211 that each & every one is alive, then we don't get any of them
1212 to appear in an "info thread" command, which is quite a useful
1216 core_target::thread_alive (ptid_t ptid
)
1221 /* Ask the current architecture what it knows about this core file.
1222 That will be used, in turn, to pick a better architecture. This
1223 wrapper could be avoided if targets got a chance to specialize
1226 const struct target_desc
*
1227 core_target::read_description ()
1229 /* If the core file contains a target description note then we will use
1230 that in preference to anything else. */
1231 bfd_size_type tdesc_note_size
= 0;
1232 struct bfd_section
*tdesc_note_section
1233 = bfd_get_section_by_name (core_bfd
, ".gdb-tdesc");
1234 if (tdesc_note_section
!= nullptr)
1235 tdesc_note_size
= bfd_section_size (tdesc_note_section
);
1236 if (tdesc_note_size
> 0)
1238 gdb::char_vector
contents (tdesc_note_size
+ 1);
1239 if (bfd_get_section_contents (core_bfd
, tdesc_note_section
,
1240 contents
.data (), (file_ptr
) 0,
1243 /* Ensure we have a null terminator. */
1244 contents
[tdesc_note_size
] = '\0';
1245 const struct target_desc
*result
1246 = string_read_description_xml (contents
.data ());
1247 if (result
!= nullptr)
1252 if (m_core_gdbarch
&& gdbarch_core_read_description_p (m_core_gdbarch
))
1254 const struct target_desc
*result
;
1256 result
= gdbarch_core_read_description (m_core_gdbarch
, this, core_bfd
);
1261 return this->beneath ()->read_description ();
1265 core_target::pid_to_str (ptid_t ptid
)
1267 struct inferior
*inf
;
1270 /* The preferred way is to have a gdbarch/OS specific
1272 if (m_core_gdbarch
!= nullptr
1273 && gdbarch_core_pid_to_str_p (m_core_gdbarch
))
1274 return gdbarch_core_pid_to_str (m_core_gdbarch
, ptid
);
1276 /* Otherwise, if we don't have one, we'll just fallback to
1277 "process", with normal_pid_to_str. */
1279 /* Try the LWPID field first. */
1282 return normal_pid_to_str (ptid_t (pid
));
1284 /* Otherwise, this isn't a "threaded" core -- use the PID field, but
1285 only if it isn't a fake PID. */
1286 inf
= find_inferior_ptid (this, ptid
);
1287 if (inf
!= NULL
&& !inf
->fake_pid_p
)
1288 return normal_pid_to_str (ptid
);
1290 /* No luck. We simply don't have a valid PID to print. */
1291 return "<main task>";
1295 core_target::thread_name (struct thread_info
*thr
)
1297 if (m_core_gdbarch
!= nullptr
1298 && gdbarch_core_thread_name_p (m_core_gdbarch
))
1299 return gdbarch_core_thread_name (m_core_gdbarch
, thr
);
1304 core_target::has_memory ()
1306 return (core_bfd
!= NULL
);
1310 core_target::has_stack ()
1312 return (core_bfd
!= NULL
);
1316 core_target::has_registers ()
1318 return (core_bfd
!= NULL
);
1321 /* Implement the to_info_proc method. */
1324 core_target::info_proc (const char *args
, enum info_proc_what request
)
1326 struct gdbarch
*gdbarch
= get_current_arch ();
1328 /* Since this is the core file target, call the 'core_info_proc'
1329 method on gdbarch, not 'info_proc'. */
1330 if (gdbarch_core_info_proc_p (gdbarch
))
1331 gdbarch_core_info_proc (gdbarch
, args
, request
);
1336 /* Implementation of the "supports_memory_tagging" target_ops method. */
1339 core_target::supports_memory_tagging ()
1341 /* Look for memory tag sections. If they exist, that means this core file
1342 supports memory tagging. */
1344 return (bfd_get_section_by_name (core_bfd
, "memtag") != nullptr);
1347 /* Implementation of the "fetch_memtags" target_ops method. */
1350 core_target::fetch_memtags (CORE_ADDR address
, size_t len
,
1351 gdb::byte_vector
&tags
, int type
)
1353 struct gdbarch
*gdbarch
= target_gdbarch ();
1355 /* Make sure we have a way to decode the memory tag notes. */
1356 if (!gdbarch_decode_memtag_section_p (gdbarch
))
1357 error (_("gdbarch_decode_memtag_section not implemented for this "
1360 memtag_section_info info
;
1361 info
.memtag_section
= nullptr;
1363 while (get_next_core_memtag_section (core_bfd
, info
.memtag_section
,
1366 size_t adjusted_length
1367 = (address
+ len
< info
.end_address
) ? len
: (info
.end_address
- address
);
1369 /* Decode the memory tag note and return the tags. */
1370 gdb::byte_vector tags_read
1371 = gdbarch_decode_memtag_section (gdbarch
, info
.memtag_section
, type
,
1372 address
, adjusted_length
);
1374 /* Transfer over the tags that have been read. */
1375 tags
.insert (tags
.end (), tags_read
.begin (), tags_read
.end ());
1377 /* ADDRESS + LEN may cross the boundaries of a particular memory tag
1378 segment. Check if we need to fetch tags from a different section. */
1379 if (!tags_read
.empty () && (address
+ len
) < info
.end_address
)
1382 /* There are more tags to fetch. Update ADDRESS and LEN. */
1383 len
-= (info
.end_address
- address
);
1384 address
= info
.end_address
;
1390 /* Get a pointer to the current core target. If not connected to a
1391 core target, return NULL. */
1393 static core_target
*
1394 get_current_core_target ()
1396 target_ops
*proc_target
= current_inferior ()->process_target ();
1397 return dynamic_cast<core_target
*> (proc_target
);
1400 /* Display file backed mappings from core file. */
1403 core_target::info_proc_mappings (struct gdbarch
*gdbarch
)
1405 if (!m_core_file_mappings
.empty ())
1407 gdb_printf (_("Mapped address spaces:\n\n"));
1408 if (gdbarch_addr_bit (gdbarch
) == 32)
1410 gdb_printf ("\t%10s %10s %10s %10s %s\n",
1413 " Size", " Offset", "objfile");
1417 gdb_printf (" %18s %18s %10s %10s %s\n",
1420 " Size", " Offset", "objfile");
1424 for (const target_section
&tsp
: m_core_file_mappings
)
1426 ULONGEST start
= tsp
.addr
;
1427 ULONGEST end
= tsp
.endaddr
;
1428 ULONGEST file_ofs
= tsp
.the_bfd_section
->filepos
;
1429 const char *filename
= bfd_get_filename (tsp
.the_bfd_section
->owner
);
1431 if (gdbarch_addr_bit (gdbarch
) == 32)
1432 gdb_printf ("\t%10s %10s %10s %10s %s\n",
1433 paddress (gdbarch
, start
),
1434 paddress (gdbarch
, end
),
1435 hex_string (end
- start
),
1436 hex_string (file_ofs
),
1439 gdb_printf (" %18s %18s %10s %10s %s\n",
1440 paddress (gdbarch
, start
),
1441 paddress (gdbarch
, end
),
1442 hex_string (end
- start
),
1443 hex_string (file_ofs
),
1448 /* Implement "maintenance print core-file-backed-mappings" command.
1450 If mappings are loaded, the results should be similar to the
1451 mappings shown by "info proc mappings". This command is mainly a
1452 debugging tool for GDB developers to make sure that the expected
1453 mappings are present after loading a core file. For Linux, the
1454 output provided by this command will be very similar (if not
1455 identical) to that provided by "info proc mappings". This is not
1456 necessarily the case for other OSes which might provide
1457 more/different information in the "info proc mappings" output. */
1460 maintenance_print_core_file_backed_mappings (const char *args
, int from_tty
)
1462 core_target
*targ
= get_current_core_target ();
1463 if (targ
!= nullptr)
1464 targ
->info_proc_mappings (targ
->core_gdbarch ());
1467 void _initialize_corelow ();
1469 _initialize_corelow ()
1471 add_target (core_target_info
, core_target_open
, filename_completer
);
1472 add_cmd ("core-file-backed-mappings", class_maintenance
,
1473 maintenance_print_core_file_backed_mappings
,
1474 _("Print core file's file-backed mappings."),
1475 &maintenanceprintlist
);