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 <unordered_map>
52 #include <unordered_set>
53 #include "cli/cli-cmds.h"
54 #include "xml-tdesc.h"
56 #include "cli/cli-style.h"
62 /* A mem_range and the build-id associated with the file mapped into the
65 struct mem_range_and_build_id
67 mem_range_and_build_id (mem_range
&&r
, const bfd_build_id
*id
)
72 /* A range of memory addresses. */
75 /* The build-id of the file mapped into RANGE. */
76 const bfd_build_id
*build_id
;
79 /* An instance of this class is created within the core_target and is used
80 to hold all the information that relating to mapped files, their address
81 ranges, and their corresponding build-ids. */
83 struct mapped_file_info
85 /* See comment on function definition. */
87 void add (const char *soname
, const char *expected_filename
,
88 const char *actual_filename
, std::vector
<mem_range
> &&ranges
,
89 const bfd_build_id
*build_id
);
91 /* See comment on function definition. */
93 std::optional
<core_target_mapped_file_info
>
94 lookup (const char *filename
, const std::optional
<CORE_ADDR
> &addr
);
98 /* Helper for ::lookup. BUILD_ID is a build-id that was found in
99 one of the data structures within this class. Lookup the
100 corresponding filename in m_build_id_to_filename_map and return a pair
101 containing the build-id and filename.
103 If no corresponding filename is found in m_build_id_to_filename_map
104 then the returned pair contains BUILD_ID and an empty string.
106 If BUILD_ID is nullptr then the returned pair contains nullptr and an
109 struct core_target_mapped_file_info
110 make_result (const bfd_build_id
*build_id
)
112 if (build_id
!= nullptr)
114 auto it
= m_build_id_to_filename_map
.find (build_id
);
115 if (it
!= m_build_id_to_filename_map
.end ())
116 return { build_id
, it
->second
};
119 return { build_id
, {} };
122 /* A type that maps a string to a build-id. */
123 using string_to_build_id_map
124 = std::unordered_map
<std::string
, const bfd_build_id
*>;
126 /* A type that maps a build-id to a string. */
127 using build_id_to_string_map
128 = std::unordered_map
<const bfd_build_id
*, std::string
>;
130 /* When loading a core file, the build-ids are extracted based on the
131 file backed mappings. This map associates the name of a file that was
132 mapped into the core file with the corresponding build-id. The
133 build-id pointers in this map will never be nullptr as we only record
134 files if they have a build-id. */
136 string_to_build_id_map m_filename_to_build_id_map
;
138 /* Map a build-id pointer back to the name of the file that was mapped
139 into the inferior's address space. If we lookup a matching build-id
140 using either a soname or an address then this map allows us to also
141 provide a full path to a file with a matching build-id. */
143 build_id_to_string_map m_build_id_to_filename_map
;
145 /* If the file that was mapped into the core file was a shared library
146 then it might have a DT_SONAME tag in its .dynamic section, this tag
147 contains the name of a shared object. When opening a shared library,
148 if it's basename appears in this map then we can use the corresponding
151 In the rare case that two different files have the same DT_SONAME
152 value then the build-id pointer in this map will be nullptr, this
153 indicates that it's not possible to find a build-id based on the given
156 string_to_build_id_map m_soname_to_build_id_map
;
158 /* This vector maps memory ranges onto an associated build-id. The
159 ranges are those of the files mapped into the core file.
161 Entries in this vector must not overlap, and are sorted be increasing
162 memory address. Within each entry the build-id pointer will not be
165 While building this vector the entries are not sorted, they are
166 sorted once after the table has finished being built. */
168 std::vector
<mem_range_and_build_id
> m_address_to_build_id_list
;
170 /* False if address_to_build_id_list is unsorted, otherwise true. */
172 bool m_address_to_build_id_list_sorted
= false;
175 /* The core file target. */
177 static const target_info core_target_info
= {
179 N_("Local core dump file"),
180 N_("Use a core file as a target.\n\
181 Specify the filename of the core file.")
184 class core_target final
: public process_stratum_target
189 const target_info
&info () const override
190 { return core_target_info
; }
192 void close () override
;
193 void detach (inferior
*, int) override
;
194 void fetch_registers (struct regcache
*, int) override
;
196 enum target_xfer_status
xfer_partial (enum target_object object
,
199 const gdb_byte
*writebuf
,
200 ULONGEST offset
, ULONGEST len
,
201 ULONGEST
*xfered_len
) override
;
202 void files_info () override
;
204 bool thread_alive (ptid_t ptid
) override
;
205 const struct target_desc
*read_description () override
;
207 std::string
pid_to_str (ptid_t
) override
;
209 const char *thread_name (struct thread_info
*) override
;
211 bool has_all_memory () override
{ return true; }
212 bool has_memory () override
;
213 bool has_stack () override
;
214 bool has_registers () override
;
215 bool has_execution (inferior
*inf
) override
{ return false; }
217 bool info_proc (const char *, enum info_proc_what
) override
;
219 bool supports_memory_tagging () override
;
221 /* Core file implementation of fetch_memtags. Fetch the memory tags from
223 bool fetch_memtags (CORE_ADDR address
, size_t len
,
224 gdb::byte_vector
&tags
, int type
) override
;
226 /* If the architecture supports it, check if ADDRESS is within a memory range
227 mapped with tags. For example, MTE tags for AArch64. */
228 bool is_address_tagged (gdbarch
*gdbarch
, CORE_ADDR address
) override
;
230 x86_xsave_layout
fetch_x86_xsave_layout () override
;
234 /* Getter, see variable definition. */
235 struct gdbarch
*core_gdbarch ()
237 return m_core_gdbarch
;
240 /* See definition. */
241 void get_core_register_section (struct regcache
*regcache
,
242 const struct regset
*regset
,
244 int section_min_size
,
245 const char *human_name
,
248 /* See definition. */
249 void info_proc_mappings (struct gdbarch
*gdbarch
);
251 std::optional
<core_target_mapped_file_info
>
252 lookup_mapped_file_info (const char *filename
,
253 const std::optional
<CORE_ADDR
> &addr
)
255 return m_mapped_file_info
.lookup (filename
, addr
);
258 private: /* per-core data */
260 /* Get rid of the core inferior. */
263 /* The core's section table. Note that these target sections are
264 *not* mapped in the current address spaces' set of target
265 sections --- those should come only from pure executable or
266 shared library bfds. The core bfd sections are an implementation
267 detail of the core target, just like ptrace is for unix child
269 std::vector
<target_section
> m_core_section_table
;
271 /* File-backed address space mappings: some core files include
272 information about memory mapped files. */
273 std::vector
<target_section
> m_core_file_mappings
;
275 /* Unavailable mappings. These correspond to pathnames which either
276 weren't found or could not be opened. Knowing these addresses can
278 std::vector
<mem_range
> m_core_unavailable_mappings
;
280 /* Data structure that holds information mapping filenames and address
281 ranges to the corresponding build-ids as well as the reverse build-id
282 to filename mapping. */
283 mapped_file_info m_mapped_file_info
;
285 /* Build m_core_file_mappings and m_mapped_file_info. Called from the
287 void build_file_mappings ();
289 /* FIXME: kettenis/20031023: Eventually this field should
291 struct gdbarch
*m_core_gdbarch
= NULL
;
294 core_target::core_target ()
296 /* Find a first arch based on the BFD. We need the initial gdbarch so
297 we can setup the hooks to find a target description. */
298 m_core_gdbarch
= gdbarch_from_bfd (current_program_space
->core_bfd ());
300 /* If the arch is able to read a target description from the core, it
301 could yield a more specific gdbarch. */
302 const struct target_desc
*tdesc
= read_description ();
304 if (tdesc
!= nullptr)
306 struct gdbarch_info info
;
307 info
.abfd
= current_program_space
->core_bfd ();
308 info
.target_desc
= tdesc
;
309 m_core_gdbarch
= gdbarch_find_by_info (info
);
313 || !gdbarch_iterate_over_regset_sections_p (m_core_gdbarch
))
314 error (_("\"%s\": Core file format not supported"),
315 bfd_get_filename (current_program_space
->core_bfd ()));
317 /* Find the data section */
318 m_core_section_table
= build_section_table (current_program_space
->core_bfd ());
320 build_file_mappings ();
323 /* Construct the table for file-backed mappings if they exist.
325 For each unique path in the note, we'll open a BFD with a bfd
326 target of "binary". This is an unstructured bfd target upon which
327 we'll impose a structure from the mappings in the architecture-specific
328 mappings note. A BFD section is allocated and initialized for each
331 We take care to not share already open bfds with other parts of
332 GDB; in particular, we don't want to add new sections to existing
333 BFDs. We do, however, ensure that the BFDs that we allocate here
334 will go away (be deallocated) when the core target is detached. */
337 core_target::build_file_mappings ()
339 /* Type holding information about a single file mapped into the inferior
340 at the point when the core file was created. Associates a build-id
341 with the list of regions the file is mapped into. */
344 /* Type for a region of a file that was mapped into the inferior when
345 the core file was generated. */
348 /* Constructor. See member variables for argument descriptions. */
349 region (CORE_ADDR start_
, CORE_ADDR end_
, CORE_ADDR file_ofs_
)
355 /* The inferior address for the start of the mapped region. */
358 /* The inferior address immediately after the mapped region. */
361 /* The offset within the mapped file for this content. */
365 /* If not nullptr, then this is the build-id associated with this
367 const bfd_build_id
*build_id
= nullptr;
369 /* If true then we have seen multiple different build-ids associated
370 with the same filename. The build_id field will have been set back
371 to nullptr, and we should not set build_id in future. */
372 bool ignore_build_id_p
= false;
374 /* All the mapped regions of this file. */
375 std::vector
<region
> regions
;
378 std::unordered_map
<std::string
, struct bfd
*> bfd_map
;
379 std::unordered_set
<std::string
> unavailable_paths
;
381 /* All files mapped into the core file. The key is the filename. */
382 std::unordered_map
<std::string
, mapped_file
> mapped_files
;
384 /* See linux_read_core_file_mappings() in linux-tdep.c for an example
385 read_core_file_mappings method. */
386 gdbarch_read_core_file_mappings (m_core_gdbarch
,
387 current_program_space
->core_bfd (),
389 /* After determining the number of mappings, read_core_file_mappings
390 will invoke this lambda. */
395 /* read_core_file_mappings will invoke this lambda for each mapping
397 [&] (int num
, ULONGEST start
, ULONGEST end
, ULONGEST file_ofs
,
398 const char *filename
, const bfd_build_id
*build_id
)
400 /* Architecture-specific read_core_mapping methods are expected to
401 weed out non-file-backed mappings. */
402 gdb_assert (filename
!= nullptr);
404 /* Add this mapped region to the data for FILENAME. */
405 mapped_file
&file_data
= mapped_files
[filename
];
406 file_data
.regions
.emplace_back (start
, end
, file_ofs
);
407 if (build_id
!= nullptr && !file_data
.ignore_build_id_p
)
409 if (file_data
.build_id
== nullptr)
410 file_data
.build_id
= build_id
;
411 else if (!build_id_equal (build_id
, file_data
.build_id
))
413 warning (_("Multiple build-ids found for %ps"),
414 styled_string (file_name_style
.style (), filename
));
415 file_data
.build_id
= nullptr;
416 file_data
.ignore_build_id_p
= true;
421 for (const auto &iter
: mapped_files
)
423 const std::string
&filename
= iter
.first
;
424 const mapped_file
&file_data
= iter
.second
;
426 /* Use exec_file_find() to do sysroot expansion. It'll
427 also strip the potential sysroot "target:" prefix. If
428 there is no sysroot, an equivalent (possibly more
429 canonical) pathname will be provided. */
430 gdb::unique_xmalloc_ptr
<char> expanded_fname
431 = exec_file_find (filename
.c_str (), nullptr);
433 bool build_id_mismatch
= false;
434 if (expanded_fname
!= nullptr && file_data
.build_id
!= nullptr)
436 /* We temporarily open the bfd as a structured target, this
437 allows us to read the build-id from the bfd if there is one.
438 For this task it's OK if we reuse an already open bfd object,
439 so we make this call through GDB's bfd cache. Once we've
440 checked the build-id (if there is one) we'll drop this
441 reference and re-open the bfd using the "binary" target. */
442 gdb_bfd_ref_ptr tmp_bfd
443 = gdb_bfd_open (expanded_fname
.get (), gnutarget
);
445 if (tmp_bfd
!= nullptr
446 && bfd_check_format (tmp_bfd
.get (), bfd_object
)
447 && build_id_bfd_get (tmp_bfd
.get ()) != nullptr)
449 /* The newly opened TMP_BFD has a build-id, and this mapped
450 file has a build-id extracted from the core-file. Check
451 the build-id's match, and if not, reject TMP_BFD. */
452 const struct bfd_build_id
*found
453 = build_id_bfd_get (tmp_bfd
.get ());
454 if (!build_id_equal (found
, file_data
.build_id
))
455 build_id_mismatch
= true;
459 gdb_bfd_ref_ptr abfd
;
460 if (expanded_fname
!= nullptr && !build_id_mismatch
)
462 struct bfd
*b
= bfd_openr (expanded_fname
.get (), "binary");
463 abfd
= gdb_bfd_ref_ptr::new_reference (b
);
466 if ((expanded_fname
== nullptr
468 || !bfd_check_format (abfd
.get (), bfd_object
))
469 && file_data
.build_id
!= nullptr)
471 abfd
= find_objfile_by_build_id (file_data
.build_id
,
476 /* The find_objfile_by_build_id will have opened ABFD using
477 the GNUTARGET global bfd type, however, we need the bfd
478 opened as the binary type (see the function's header
479 comment), so now we reopen ABFD with the desired binary
482 = make_unique_xstrdup (bfd_get_filename (abfd
.get ()));
483 struct bfd
*b
= bfd_openr (expanded_fname
.get (), "binary");
484 gdb_assert (b
!= nullptr);
485 abfd
= gdb_bfd_ref_ptr::new_reference (b
);
489 std::vector
<mem_range
> ranges
;
490 for (const mapped_file::region
®ion
: file_data
.regions
)
491 ranges
.emplace_back (region
.start
, region
.end
- region
.start
);
493 if (expanded_fname
== nullptr
495 || !bfd_check_format (abfd
.get (), bfd_object
))
497 /* If ABFD was opened, but the wrong format, close it now. */
500 /* Record all regions for this file as unavailable. */
501 for (const mapped_file::region
®ion
: file_data
.regions
)
502 m_core_unavailable_mappings
.emplace_back (region
.start
,
506 /* And give the user an appropriate warning. */
507 if (build_id_mismatch
)
509 if (expanded_fname
== nullptr
510 || filename
== expanded_fname
.get ())
511 warning (_("File %ps doesn't match build-id from core-file "
512 "during file-backed mapping processing"),
513 styled_string (file_name_style
.style (),
516 warning (_("File %ps which was expanded to %ps, doesn't match "
517 "build-id from core-file during file-backed "
518 "mapping processing"),
519 styled_string (file_name_style
.style (),
521 styled_string (file_name_style
.style (),
522 expanded_fname
.get ()));
526 if (expanded_fname
== nullptr
527 || filename
== expanded_fname
.get ())
528 warning (_("Can't open file %ps during file-backed mapping "
530 styled_string (file_name_style
.style (),
533 warning (_("Can't open file %ps which was expanded to %ps "
534 "during file-backed mapping note processing"),
535 styled_string (file_name_style
.style (),
537 styled_string (file_name_style
.style (),
538 expanded_fname
.get ()));
543 /* Ensure that the bfd will be closed when core_bfd is closed.
544 This can be checked before/after a core file detach via "maint
546 gdb_bfd_record_inclusion (current_program_space
->core_bfd (),
549 /* Create sections for each mapped region. */
550 for (const mapped_file::region
®ion
: file_data
.regions
)
552 /* Make new BFD section. All sections have the same name,
553 which is permitted by bfd_make_section_anyway(). */
554 asection
*sec
= bfd_make_section_anyway (abfd
.get (), "load");
556 error (_("Can't make section"));
557 sec
->filepos
= region
.file_ofs
;
558 bfd_set_section_flags (sec
, SEC_READONLY
| SEC_HAS_CONTENTS
);
559 bfd_set_section_size (sec
, region
.end
- region
.start
);
560 bfd_set_section_vma (sec
, region
.start
);
561 bfd_set_section_lma (sec
, region
.start
);
562 bfd_set_section_alignment (sec
, 2);
564 /* Set target_section fields. */
565 m_core_file_mappings
.emplace_back (region
.start
, region
.end
, sec
);
569 /* If this is a bfd with a build-id then record the filename,
570 optional soname (DT_SONAME .dynamic attribute), and the range of
571 addresses at which this bfd is mapped. This information can be
572 used to perform build-id checking when loading the shared
574 if (file_data
.build_id
!= nullptr)
576 normalize_mem_ranges (&ranges
);
578 const char *actual_filename
= nullptr;
579 gdb::unique_xmalloc_ptr
<char> soname
;
582 actual_filename
= bfd_get_filename (abfd
.get ());
583 soname
= gdb_bfd_read_elf_soname (actual_filename
);
586 m_mapped_file_info
.add (soname
.get (), filename
.c_str (),
587 actual_filename
, std::move (ranges
),
592 normalize_mem_ranges (&m_core_unavailable_mappings
);
595 /* An arbitrary identifier for the core inferior. */
596 #define CORELOW_PID 1
599 core_target::clear_core ()
601 if (current_program_space
->core_bfd () != nullptr)
603 switch_to_no_thread (); /* Avoid confusion from thread
605 exit_inferior (current_inferior ());
607 /* Clear out solib state while the bfd is still open. See
608 comments in clear_solib in solib.c. */
609 clear_solib (current_program_space
);
611 current_program_space
->cbfd
.reset (nullptr);
615 /* Close the core target. */
618 core_target::close ()
622 /* Core targets are heap-allocated (see core_target_open), so here
623 we delete ourselves. */
627 /* Look for sections whose names start with `.reg/' so that we can
628 extract the list of threads in a core file. */
630 /* If ASECT is a section whose name begins with '.reg/' then extract the
631 lwpid after the '/' and create a new thread in INF.
633 If REG_SECT is not nullptr, and the both ASECT and REG_SECT point at the
634 same position in the parent bfd object then switch to the newly created
635 thread, otherwise, the selected thread is left unchanged. */
638 add_to_thread_list (asection
*asect
, asection
*reg_sect
, inferior
*inf
)
640 if (!startswith (bfd_section_name (asect
), ".reg/"))
643 int lwpid
= atoi (bfd_section_name (asect
) + 5);
644 ptid_t
ptid (inf
->pid
, lwpid
);
645 thread_info
*thr
= add_thread (inf
->process_target (), ptid
);
647 /* Warning, Will Robinson, looking at BFD private data! */
650 && asect
->filepos
== reg_sect
->filepos
) /* Did we find .reg? */
651 switch_to_thread (thr
); /* Yes, make it current. */
654 /* Issue a message saying we have no core to debug, if FROM_TTY. */
657 maybe_say_no_core_file_now (int from_tty
)
660 gdb_printf (_("No core file now.\n"));
663 /* Backward compatibility with old way of specifying core files. */
666 core_file_command (const char *filename
, int from_tty
)
668 dont_repeat (); /* Either way, seems bogus. */
670 if (filename
== NULL
)
672 if (current_program_space
->core_bfd () != nullptr)
674 target_detach (current_inferior (), from_tty
);
675 gdb_assert (current_program_space
->core_bfd () == nullptr);
678 maybe_say_no_core_file_now (from_tty
);
681 core_target_open (filename
, from_tty
);
684 /* A vmcore file is a core file created by the Linux kernel at the point of
685 a crash. Each thread in the core file represents a real CPU core, and
686 the lwpid for each thread is the pid of the process that was running on
687 that core at the moment of the crash.
689 However, not every CPU core will have been running a process, some cores
690 will be idle. For these idle cores the CPU writes an lwpid of 0. And
691 of course, multiple cores might be idle, so there could be multiple
692 threads with an lwpid of 0.
694 The problem is GDB doesn't really like threads with an lwpid of 0; GDB
695 presents such a thread as a process rather than a thread. And GDB
696 certainly doesn't like multiple threads having the same lwpid, each time
697 a new thread is seen with the same lwpid the earlier thread (with the
698 same lwpid) will be deleted.
700 This function addresses both of these problems by assigning a fake lwpid
701 to any thread with an lwpid of 0.
703 GDB finds the lwpid information by looking at the bfd section names
704 which include the lwpid, e.g. .reg/NN where NN is the lwpid. This
705 function looks though all the section names looking for sections named
706 .reg/NN. If any sections are found where NN == 0, then we assign a new
707 unique value of NN. Then, in a second pass, any sections ending /0 are
708 assigned their new number.
710 Remember, a core file may contain multiple register sections for
711 different register sets, but the sets are always grouped by thread, so
712 we can figure out which registers should be assigned the same new
713 lwpid. For example, consider a core file containing:
715 .reg/0, .reg2/0, .reg/0, .reg2/0
717 This represents two threads, each thread contains a .reg and .reg2
718 register set. The .reg represents the start of each thread. After
719 renaming the sections will now look like this:
721 .reg/1, .reg2/1, .reg/2, .reg2/2
723 After calling this function the rest of the core file handling code can
724 treat this core file just like any other core file. */
727 rename_vmcore_idle_reg_sections (bfd
*abfd
, inferior
*inf
)
729 /* Map from the bfd section to its lwpid (the /NN number). */
730 std::vector
<std::pair
<asection
*, int>> sections_and_lwpids
;
732 /* The set of all /NN numbers found. Needed so we can easily find unused
733 numbers in the case that we need to rename some sections. */
734 std::unordered_set
<int> all_lwpids
;
736 /* A count of how many sections called .reg/0 we have found. */
737 unsigned zero_lwpid_count
= 0;
739 /* Look for all the .reg sections. Record the section object and the
740 lwpid which is extracted from the section name. Spot if any have an
742 for (asection
*sect
: gdb_bfd_sections (current_program_space
->core_bfd ()))
744 if (startswith (bfd_section_name (sect
), ".reg/"))
746 int lwpid
= atoi (bfd_section_name (sect
) + 5);
747 sections_and_lwpids
.emplace_back (sect
, lwpid
);
748 all_lwpids
.insert (lwpid
);
754 /* If every ".reg/NN" section has a non-zero lwpid then we don't need to
756 if (zero_lwpid_count
== 0)
759 /* Assign a new number to any .reg sections with an lwpid of 0. */
761 for (auto §_and_lwpid
: sections_and_lwpids
)
762 if (sect_and_lwpid
.second
== 0)
764 while (all_lwpids
.find (new_lwpid
) != all_lwpids
.end ())
766 sect_and_lwpid
.second
= new_lwpid
;
770 /* Now update the names of any sections with an lwpid of 0. This is
771 more than just the .reg sections we originally found. */
772 std::string replacement_lwpid_str
;
773 auto iter
= sections_and_lwpids
.begin ();
774 int replacement_lwpid
= 0;
775 for (asection
*sect
: gdb_bfd_sections (current_program_space
->core_bfd ()))
777 if (iter
!= sections_and_lwpids
.end () && sect
== iter
->first
)
779 gdb_assert (startswith (bfd_section_name (sect
), ".reg/"));
781 int lwpid
= atoi (bfd_section_name (sect
) + 5);
782 if (lwpid
== iter
->second
)
784 /* This section was not given a new number. */
785 gdb_assert (lwpid
!= 0);
786 replacement_lwpid
= 0;
790 replacement_lwpid
= iter
->second
;
791 ptid_t
ptid (inf
->pid
, replacement_lwpid
);
792 if (!replacement_lwpid_str
.empty ())
793 replacement_lwpid_str
+= ", ";
794 replacement_lwpid_str
+= target_pid_to_str (ptid
);
800 if (replacement_lwpid
!= 0)
802 const char *name
= bfd_section_name (sect
);
803 size_t len
= strlen (name
);
805 if (strncmp (name
+ len
- 2, "/0", 2) == 0)
807 /* This section needs a new name. */
809 = string_printf ("%.*s/%d",
810 static_cast<int> (len
- 2),
811 name
, replacement_lwpid
);
813 = static_cast<char *> (bfd_alloc (abfd
, name_str
.size () + 1));
814 if (name_buf
== nullptr)
815 error (_("failed to allocate space for section name '%s'"),
817 memcpy (name_buf
, name_str
.c_str(), name_str
.size () + 1);
818 bfd_rename_section (sect
, name_buf
);
823 if (zero_lwpid_count
== 1)
824 warning (_("found thread with pid 0, assigned replacement Target Id: %s"),
825 replacement_lwpid_str
.c_str ());
827 warning (_("found threads with pid 0, assigned replacement Target Ids: %s"),
828 replacement_lwpid_str
.c_str ());
831 /* Locate (and load) an executable file (and symbols) given the core file
835 locate_exec_from_corefile_build_id (bfd
*abfd
, int from_tty
)
837 const bfd_build_id
*build_id
= build_id_bfd_get (abfd
);
838 if (build_id
== nullptr)
841 gdb_bfd_ref_ptr execbfd
842 = find_objfile_by_build_id (build_id
, abfd
->filename
);
844 if (execbfd
!= nullptr)
846 exec_file_attach (bfd_get_filename (execbfd
.get ()), from_tty
);
847 symbol_file_add_main (bfd_get_filename (execbfd
.get ()),
848 symfile_add_flag (from_tty
? SYMFILE_VERBOSE
: 0));
855 core_target_open (const char *arg
, int from_tty
)
862 target_preopen (from_tty
);
864 std::string filename
= extract_single_filename_arg (arg
);
866 if (filename
.empty ())
868 if (current_program_space
->core_bfd ())
869 error (_("No core file specified. (Use `detach' "
870 "to stop debugging a core file.)"));
872 error (_("No core file specified."));
875 if (!IS_ABSOLUTE_PATH (filename
.c_str ()))
876 filename
= gdb_abspath (filename
);
878 flags
= O_BINARY
| O_LARGEFILE
;
883 scratch_chan
= gdb_open_cloexec (filename
.c_str (), flags
, 0).release ();
884 if (scratch_chan
< 0)
885 perror_with_name (filename
.c_str ());
887 gdb_bfd_ref_ptr
temp_bfd (gdb_bfd_fopen (filename
.c_str (), gnutarget
,
888 write_files
? FOPEN_RUB
: FOPEN_RB
,
890 if (temp_bfd
== NULL
)
891 perror_with_name (filename
.c_str ());
893 if (!bfd_check_format (temp_bfd
.get (), bfd_core
))
895 /* Do it after the err msg */
896 /* FIXME: should be checking for errors from bfd_close (for one
897 thing, on error it does not free all the storage associated
899 error (_("\"%s\" is not a core dump: %s"),
900 filename
.c_str (), bfd_errmsg (bfd_get_error ()));
903 current_program_space
->cbfd
= std::move (temp_bfd
);
905 core_target
*target
= new core_target ();
907 /* Own the target until it is successfully pushed. */
908 target_ops_up
target_holder (target
);
912 /* If we have no exec file, try to set the architecture from the
913 core file. We don't do this unconditionally since an exec file
914 typically contains more information that helps us determine the
915 architecture than a core file. */
916 if (!current_program_space
->exec_bfd ())
917 set_gdbarch_from_file (current_program_space
->core_bfd ());
919 current_inferior ()->push_target (std::move (target_holder
));
921 switch_to_no_thread ();
923 /* Need to flush the register cache (and the frame cache) from a
924 previous debug session. If inferior_ptid ends up the same as the
925 last debug session --- e.g., b foo; run; gcore core1; step; gcore
926 core2; core core1; core core2 --- then there's potential for
927 get_current_regcache to return the cached regcache of the
928 previous session, and the frame cache being stale. */
929 registers_changed ();
931 /* Find (or fake) the pid for the process in this core file, and
932 initialise the current inferior with that pid. */
933 bool fake_pid_p
= false;
934 int pid
= bfd_core_file_pid (current_program_space
->core_bfd ());
941 inferior
*inf
= current_inferior ();
942 gdb_assert (inf
->pid
== 0);
943 inferior_appeared (inf
, pid
);
944 inf
->fake_pid_p
= fake_pid_p
;
946 /* Rename any .reg/0 sections, giving them each a fake lwpid. */
947 rename_vmcore_idle_reg_sections (current_program_space
->core_bfd (), inf
);
949 /* Build up thread list from BFD sections, and possibly set the
950 current thread to the .reg/NN section matching the .reg
953 = bfd_get_section_by_name (current_program_space
->core_bfd (), ".reg");
954 for (asection
*sect
: gdb_bfd_sections (current_program_space
->core_bfd ()))
955 add_to_thread_list (sect
, reg_sect
, inf
);
957 if (inferior_ptid
== null_ptid
)
959 /* Either we found no .reg/NN section, and hence we have a
960 non-threaded core (single-threaded, from gdb's perspective),
961 or for some reason add_to_thread_list couldn't determine
962 which was the "main" thread. The latter case shouldn't
963 usually happen, but we're dealing with input here, which can
964 always be broken in different ways. */
965 thread_info
*thread
= first_thread_of_inferior (inf
);
968 thread
= add_thread_silent (target
, ptid_t (CORELOW_PID
));
970 switch_to_thread (thread
);
973 if (current_program_space
->exec_bfd () == nullptr)
974 locate_exec_from_corefile_build_id (current_program_space
->core_bfd (),
977 post_create_inferior (from_tty
);
979 /* Now go through the target stack looking for threads since there
980 may be a thread_stratum target loaded on top of target core by
981 now. The layer above should claim threads found in the BFD
985 target_update_thread_list ();
988 catch (const gdb_exception_error
&except
)
990 exception_print (gdb_stderr
, except
);
993 p
= bfd_core_file_failing_command (current_program_space
->core_bfd ());
995 gdb_printf (_("Core was generated by `%s'.\n"), p
);
997 /* Clearing any previous state of convenience variables. */
998 clear_exit_convenience_vars ();
1000 siggy
= bfd_core_file_failing_signal (current_program_space
->core_bfd ());
1003 gdbarch
*core_gdbarch
= target
->core_gdbarch ();
1005 /* If we don't have a CORE_GDBARCH to work with, assume a native
1006 core (map gdb_signal from host signals). If we do have
1007 CORE_GDBARCH to work with, but no gdb_signal_from_target
1008 implementation for that gdbarch, as a fallback measure,
1009 assume the host signal mapping. It'll be correct for native
1010 cores, but most likely incorrect for cross-cores. */
1011 enum gdb_signal sig
= (core_gdbarch
!= NULL
1012 && gdbarch_gdb_signal_from_target_p (core_gdbarch
)
1013 ? gdbarch_gdb_signal_from_target (core_gdbarch
,
1015 : gdb_signal_from_host (siggy
));
1017 gdb_printf (_("Program terminated with signal %s, %s"),
1018 gdb_signal_to_name (sig
), gdb_signal_to_string (sig
));
1019 if (gdbarch_report_signal_info_p (core_gdbarch
))
1020 gdbarch_report_signal_info (core_gdbarch
, current_uiout
, sig
);
1021 gdb_printf (_(".\n"));
1023 /* Set the value of the internal variable $_exitsignal,
1024 which holds the signal uncaught by the inferior. */
1025 set_internalvar_integer (lookup_internalvar ("_exitsignal"),
1029 /* Fetch all registers from core file. */
1030 target_fetch_registers (get_thread_regcache (inferior_thread ()), -1);
1032 /* Now, set up the frame cache, and print the top of stack. */
1033 reinit_frame_cache ();
1034 print_stack_frame (get_selected_frame (NULL
), 1, SRC_AND_LOC
, 1);
1036 /* Current thread should be NUM 1 but the user does not know that.
1037 If a program is single threaded gdb in general does not mention
1038 anything about threads. That is why the test is >= 2. */
1039 if (thread_count (target
) >= 2)
1043 thread_command (NULL
, from_tty
);
1045 catch (const gdb_exception_error
&except
)
1047 exception_print (gdb_stderr
, except
);
1053 core_target::detach (inferior
*inf
, int from_tty
)
1055 /* Get rid of the core. Don't rely on core_target::close doing it,
1056 because target_detach may be called with core_target's refcount > 1,
1057 meaning core_target::close may not be called yet by the
1058 unpush_target call below. */
1061 /* Note that 'this' may be dangling after this call. unpush_target
1062 closes the target if the refcount reaches 0, and our close
1063 implementation deletes 'this'. */
1064 inf
->unpush_target (this);
1066 /* Clear the register cache and the frame cache. */
1067 registers_changed ();
1068 reinit_frame_cache ();
1069 maybe_say_no_core_file_now (from_tty
);
1072 /* Try to retrieve registers from a section in core_bfd, and supply
1075 If ptid's lwp member is zero, do the single-threaded
1076 thing: look for a section named NAME. If ptid's lwp
1077 member is non-zero, do the multi-threaded thing: look for a section
1078 named "NAME/LWP", where LWP is the shortest ASCII decimal
1079 representation of ptid's lwp member.
1081 HUMAN_NAME is a human-readable name for the kind of registers the
1082 NAME section contains, for use in error messages.
1084 If REQUIRED is true, print an error if the core file doesn't have a
1085 section by the appropriate name. Otherwise, just do nothing. */
1088 core_target::get_core_register_section (struct regcache
*regcache
,
1089 const struct regset
*regset
,
1091 int section_min_size
,
1092 const char *human_name
,
1095 gdb_assert (regset
!= nullptr);
1097 struct bfd_section
*section
;
1099 bool variable_size_section
= (regset
->flags
& REGSET_VARIABLE_SIZE
);
1101 thread_section_name
section_name (name
, regcache
->ptid ());
1103 section
= bfd_get_section_by_name (current_program_space
->core_bfd (),
1104 section_name
.c_str ());
1108 warning (_("Couldn't find %s registers in core file."),
1113 size
= bfd_section_size (section
);
1114 if (size
< section_min_size
)
1116 warning (_("Section `%s' in core file too small."),
1117 section_name
.c_str ());
1120 if (size
!= section_min_size
&& !variable_size_section
)
1122 warning (_("Unexpected size of section `%s' in core file."),
1123 section_name
.c_str ());
1126 gdb::byte_vector
contents (size
);
1127 if (!bfd_get_section_contents (current_program_space
->core_bfd (), section
,
1128 contents
.data (), (file_ptr
) 0, size
))
1130 warning (_("Couldn't read %s registers from `%s' section in core file."),
1131 human_name
, section_name
.c_str ());
1135 regset
->supply_regset (regset
, regcache
, -1, contents
.data (), size
);
1138 /* Data passed to gdbarch_iterate_over_regset_sections's callback. */
1139 struct get_core_registers_cb_data
1141 core_target
*target
;
1142 struct regcache
*regcache
;
1145 /* Callback for get_core_registers that handles a single core file
1146 register note section. */
1149 get_core_registers_cb (const char *sect_name
, int supply_size
, int collect_size
,
1150 const struct regset
*regset
,
1151 const char *human_name
, void *cb_data
)
1153 gdb_assert (regset
!= nullptr);
1155 auto *data
= (get_core_registers_cb_data
*) cb_data
;
1156 bool required
= false;
1157 bool variable_size_section
= (regset
->flags
& REGSET_VARIABLE_SIZE
);
1159 if (!variable_size_section
)
1160 gdb_assert (supply_size
== collect_size
);
1162 if (strcmp (sect_name
, ".reg") == 0)
1165 if (human_name
== NULL
)
1166 human_name
= "general-purpose";
1168 else if (strcmp (sect_name
, ".reg2") == 0)
1170 if (human_name
== NULL
)
1171 human_name
= "floating-point";
1174 data
->target
->get_core_register_section (data
->regcache
, regset
, sect_name
,
1175 supply_size
, human_name
, required
);
1178 /* Get the registers out of a core file. This is the machine-
1179 independent part. Fetch_core_registers is the machine-dependent
1180 part, typically implemented in the xm-file for each
1183 /* We just get all the registers, so we don't use regno. */
1186 core_target::fetch_registers (struct regcache
*regcache
, int regno
)
1188 if (!(m_core_gdbarch
!= nullptr
1189 && gdbarch_iterate_over_regset_sections_p (m_core_gdbarch
)))
1191 gdb_printf (gdb_stderr
,
1192 "Can't fetch registers from this type of core file\n");
1196 struct gdbarch
*gdbarch
= regcache
->arch ();
1197 get_core_registers_cb_data data
= { this, regcache
};
1198 gdbarch_iterate_over_regset_sections (gdbarch
,
1199 get_core_registers_cb
,
1200 (void *) &data
, NULL
);
1202 /* Mark all registers not found in the core as unavailable. */
1203 for (int i
= 0; i
< gdbarch_num_regs (regcache
->arch ()); i
++)
1204 if (regcache
->get_register_status (i
) == REG_UNKNOWN
)
1205 regcache
->raw_supply (i
, NULL
);
1209 core_target::files_info ()
1211 print_section_info (&m_core_section_table
, current_program_space
->core_bfd ());
1215 enum target_xfer_status
1216 core_target::xfer_partial (enum target_object object
, const char *annex
,
1217 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1218 ULONGEST offset
, ULONGEST len
, ULONGEST
*xfered_len
)
1222 case TARGET_OBJECT_MEMORY
:
1224 enum target_xfer_status xfer_status
;
1226 /* Try accessing memory contents from core file data,
1227 restricting consideration to those sections for which
1228 the BFD section flag SEC_HAS_CONTENTS is set. */
1229 auto has_contents_cb
= [] (const struct target_section
*s
)
1231 return ((s
->the_bfd_section
->flags
& SEC_HAS_CONTENTS
) != 0);
1233 xfer_status
= section_table_xfer_memory_partial
1235 offset
, len
, xfered_len
,
1236 m_core_section_table
,
1238 if (xfer_status
== TARGET_XFER_OK
)
1239 return TARGET_XFER_OK
;
1241 /* Check file backed mappings. If they're available, use core file
1242 provided mappings (e.g. from .note.linuxcore.file or the like)
1243 as this should provide a more accurate result. */
1244 if (!m_core_file_mappings
.empty ())
1246 xfer_status
= section_table_xfer_memory_partial
1247 (readbuf
, writebuf
, offset
, len
, xfered_len
,
1248 m_core_file_mappings
);
1249 if (xfer_status
== TARGET_XFER_OK
)
1253 /* If the access is within an unavailable file mapping then we try
1254 to check in the stratum below (the executable stratum). The
1255 thinking here is that if the mapping was read/write then the
1256 contents would have been written into the core file and the
1257 access would have been satisfied by m_core_section_table.
1259 But if the access has not yet been resolved then we can assume
1260 the access is read-only. If the executable was not found
1261 during the mapped file check then we'll have an unavailable
1262 mapping entry, however, if the user has provided the executable
1263 (maybe in a different location) then we might be able to
1264 resolve the access from there.
1266 If that fails, but the access is within an unavailable region,
1267 then the access itself should fail. */
1268 for (const auto &mr
: m_core_unavailable_mappings
)
1270 if (mr
.contains (offset
))
1272 if (!mr
.contains (offset
+ len
))
1273 len
= mr
.start
+ mr
.length
- offset
;
1276 = this->beneath ()->xfer_partial (TARGET_OBJECT_MEMORY
,
1280 if (xfer_status
== TARGET_XFER_OK
)
1281 return TARGET_XFER_OK
;
1283 return TARGET_XFER_E_IO
;
1287 /* The following is acting as a fallback in case we encounter a
1288 situation where the core file is lacking and mapped file
1289 information. Here we query the exec file stratum to see if it
1290 can resolve the access. Doing this when we are missing mapped
1291 file information might be the best we can do, but there are
1292 certainly cases this will get wrong, e.g. if an inferior created
1293 a zero initialised mapping over the top of some data that exists
1294 within the executable then this will return the executable data
1295 rather than the zero data. Maybe we should just drop this
1297 if (m_core_file_mappings
.empty ()
1298 && m_core_unavailable_mappings
.empty ())
1301 = this->beneath ()->xfer_partial (object
, annex
, readbuf
,
1302 writebuf
, offset
, len
,
1304 if (xfer_status
== TARGET_XFER_OK
)
1305 return TARGET_XFER_OK
;
1308 /* Finally, attempt to access data in core file sections with
1309 no contents. These will typically read as all zero. */
1310 auto no_contents_cb
= [&] (const struct target_section
*s
)
1312 return !has_contents_cb (s
);
1314 xfer_status
= section_table_xfer_memory_partial
1316 offset
, len
, xfered_len
,
1317 m_core_section_table
,
1322 case TARGET_OBJECT_AUXV
:
1325 /* When the aux vector is stored in core file, BFD
1326 represents this with a fake section called ".auxv". */
1328 struct bfd_section
*section
;
1331 section
= bfd_get_section_by_name (current_program_space
->core_bfd (),
1333 if (section
== NULL
)
1334 return TARGET_XFER_E_IO
;
1336 size
= bfd_section_size (section
);
1338 return TARGET_XFER_EOF
;
1344 return TARGET_XFER_EOF
;
1345 if (!bfd_get_section_contents (current_program_space
->core_bfd (),
1346 section
, readbuf
, (file_ptr
) offset
,
1349 warning (_("Couldn't read NT_AUXV note in core file."));
1350 return TARGET_XFER_E_IO
;
1353 *xfered_len
= (ULONGEST
) size
;
1354 return TARGET_XFER_OK
;
1356 return TARGET_XFER_E_IO
;
1358 case TARGET_OBJECT_WCOOKIE
:
1361 /* When the StackGhost cookie is stored in core file, BFD
1362 represents this with a fake section called
1365 struct bfd_section
*section
;
1368 section
= bfd_get_section_by_name (current_program_space
->core_bfd (),
1370 if (section
== NULL
)
1371 return TARGET_XFER_E_IO
;
1373 size
= bfd_section_size (section
);
1375 return TARGET_XFER_EOF
;
1381 return TARGET_XFER_EOF
;
1382 if (!bfd_get_section_contents (current_program_space
->core_bfd (),
1383 section
, readbuf
, (file_ptr
) offset
,
1386 warning (_("Couldn't read StackGhost cookie in core file."));
1387 return TARGET_XFER_E_IO
;
1390 *xfered_len
= (ULONGEST
) size
;
1391 return TARGET_XFER_OK
;
1394 return TARGET_XFER_E_IO
;
1396 case TARGET_OBJECT_LIBRARIES
:
1397 if (m_core_gdbarch
!= nullptr
1398 && gdbarch_core_xfer_shared_libraries_p (m_core_gdbarch
))
1401 return TARGET_XFER_E_IO
;
1404 *xfered_len
= gdbarch_core_xfer_shared_libraries (m_core_gdbarch
,
1408 if (*xfered_len
== 0)
1409 return TARGET_XFER_EOF
;
1411 return TARGET_XFER_OK
;
1414 return TARGET_XFER_E_IO
;
1416 case TARGET_OBJECT_LIBRARIES_AIX
:
1417 if (m_core_gdbarch
!= nullptr
1418 && gdbarch_core_xfer_shared_libraries_aix_p (m_core_gdbarch
))
1421 return TARGET_XFER_E_IO
;
1425 = gdbarch_core_xfer_shared_libraries_aix (m_core_gdbarch
,
1429 if (*xfered_len
== 0)
1430 return TARGET_XFER_EOF
;
1432 return TARGET_XFER_OK
;
1435 return TARGET_XFER_E_IO
;
1437 case TARGET_OBJECT_SIGNAL_INFO
:
1440 if (m_core_gdbarch
!= nullptr
1441 && gdbarch_core_xfer_siginfo_p (m_core_gdbarch
))
1443 LONGEST l
= gdbarch_core_xfer_siginfo (m_core_gdbarch
, readbuf
,
1450 return TARGET_XFER_EOF
;
1452 return TARGET_XFER_OK
;
1456 return TARGET_XFER_E_IO
;
1459 return this->beneath ()->xfer_partial (object
, annex
, readbuf
,
1460 writebuf
, offset
, len
,
1467 /* Okay, let's be honest: threads gleaned from a core file aren't
1468 exactly lively, are they? On the other hand, if we don't claim
1469 that each & every one is alive, then we don't get any of them
1470 to appear in an "info thread" command, which is quite a useful
1474 core_target::thread_alive (ptid_t ptid
)
1479 /* Ask the current architecture what it knows about this core file.
1480 That will be used, in turn, to pick a better architecture. This
1481 wrapper could be avoided if targets got a chance to specialize
1484 const struct target_desc
*
1485 core_target::read_description ()
1487 /* First check whether the target wants us to use the corefile target
1488 description notes. */
1489 if (gdbarch_use_target_description_from_corefile_notes
1490 (m_core_gdbarch
, current_program_space
->core_bfd ()))
1492 /* If the core file contains a target description note then go ahead and
1494 bfd_size_type tdesc_note_size
= 0;
1495 struct bfd_section
*tdesc_note_section
1496 = bfd_get_section_by_name (current_program_space
->core_bfd (), ".gdb-tdesc");
1497 if (tdesc_note_section
!= nullptr)
1498 tdesc_note_size
= bfd_section_size (tdesc_note_section
);
1499 if (tdesc_note_size
> 0)
1501 gdb::char_vector
contents (tdesc_note_size
+ 1);
1502 if (bfd_get_section_contents (current_program_space
->core_bfd (),
1503 tdesc_note_section
, contents
.data (),
1504 (file_ptr
) 0, tdesc_note_size
))
1506 /* Ensure we have a null terminator. */
1507 contents
[tdesc_note_size
] = '\0';
1508 const struct target_desc
*result
1509 = string_read_description_xml (contents
.data ());
1510 if (result
!= nullptr)
1516 /* If the architecture provides a corefile target description hook, use
1517 it now. Even if the core file contains a target description in a note
1518 section, it is not useful for targets that can potentially have distinct
1519 descriptions for each thread. One example is AArch64's SVE/SME
1520 extensions that allow per-thread vector length changes, resulting in
1521 registers with different sizes. */
1522 if (m_core_gdbarch
&& gdbarch_core_read_description_p (m_core_gdbarch
))
1524 const struct target_desc
*result
;
1526 result
= gdbarch_core_read_description
1527 (m_core_gdbarch
, this, current_program_space
->core_bfd ());
1528 if (result
!= nullptr)
1532 return this->beneath ()->read_description ();
1536 core_target::pid_to_str (ptid_t ptid
)
1538 struct inferior
*inf
;
1541 /* The preferred way is to have a gdbarch/OS specific
1543 if (m_core_gdbarch
!= nullptr
1544 && gdbarch_core_pid_to_str_p (m_core_gdbarch
))
1545 return gdbarch_core_pid_to_str (m_core_gdbarch
, ptid
);
1547 /* Otherwise, if we don't have one, we'll just fallback to
1548 "process", with normal_pid_to_str. */
1550 /* Try the LWPID field first. */
1553 return normal_pid_to_str (ptid_t (pid
));
1555 /* Otherwise, this isn't a "threaded" core -- use the PID field, but
1556 only if it isn't a fake PID. */
1557 inf
= find_inferior_ptid (this, ptid
);
1558 if (inf
!= NULL
&& !inf
->fake_pid_p
)
1559 return normal_pid_to_str (ptid
);
1561 /* No luck. We simply don't have a valid PID to print. */
1562 return "<main task>";
1566 core_target::thread_name (struct thread_info
*thr
)
1568 if (m_core_gdbarch
!= nullptr
1569 && gdbarch_core_thread_name_p (m_core_gdbarch
))
1570 return gdbarch_core_thread_name (m_core_gdbarch
, thr
);
1575 core_target::has_memory ()
1577 return current_program_space
->core_bfd () != nullptr;
1581 core_target::has_stack ()
1583 return current_program_space
->core_bfd () != nullptr;
1587 core_target::has_registers ()
1589 return current_program_space
->core_bfd () != nullptr;
1592 /* Implement the to_info_proc method. */
1595 core_target::info_proc (const char *args
, enum info_proc_what request
)
1597 struct gdbarch
*gdbarch
= get_current_arch ();
1599 /* Since this is the core file target, call the 'core_info_proc'
1600 method on gdbarch, not 'info_proc'. */
1601 if (gdbarch_core_info_proc_p (gdbarch
))
1602 gdbarch_core_info_proc (gdbarch
, args
, request
);
1607 /* Implementation of the "supports_memory_tagging" target_ops method. */
1610 core_target::supports_memory_tagging ()
1612 /* Look for memory tag sections. If they exist, that means this core file
1613 supports memory tagging. */
1615 return (bfd_get_section_by_name (current_program_space
->core_bfd (), "memtag")
1619 /* Implementation of the "fetch_memtags" target_ops method. */
1622 core_target::fetch_memtags (CORE_ADDR address
, size_t len
,
1623 gdb::byte_vector
&tags
, int type
)
1625 gdbarch
*gdbarch
= current_inferior ()->arch ();
1627 /* Make sure we have a way to decode the memory tag notes. */
1628 if (!gdbarch_decode_memtag_section_p (gdbarch
))
1629 error (_("gdbarch_decode_memtag_section not implemented for this "
1632 memtag_section_info info
;
1633 info
.memtag_section
= nullptr;
1635 while (get_next_core_memtag_section (current_program_space
->core_bfd (),
1636 info
.memtag_section
, address
, info
))
1638 size_t adjusted_length
1639 = (address
+ len
< info
.end_address
) ? len
: (info
.end_address
- address
);
1641 /* Decode the memory tag note and return the tags. */
1642 gdb::byte_vector tags_read
1643 = gdbarch_decode_memtag_section (gdbarch
, info
.memtag_section
, type
,
1644 address
, adjusted_length
);
1646 /* Transfer over the tags that have been read. */
1647 tags
.insert (tags
.end (), tags_read
.begin (), tags_read
.end ());
1649 /* ADDRESS + LEN may cross the boundaries of a particular memory tag
1650 segment. Check if we need to fetch tags from a different section. */
1651 if (!tags_read
.empty () && (address
+ len
) < info
.end_address
)
1654 /* There are more tags to fetch. Update ADDRESS and LEN. */
1655 len
-= (info
.end_address
- address
);
1656 address
= info
.end_address
;
1663 core_target::is_address_tagged (gdbarch
*gdbarch
, CORE_ADDR address
)
1665 return gdbarch_tagged_address_p (gdbarch
, address
);
1668 /* Implementation of the "fetch_x86_xsave_layout" target_ops method. */
1671 core_target::fetch_x86_xsave_layout ()
1673 if (m_core_gdbarch
!= nullptr &&
1674 gdbarch_core_read_x86_xsave_layout_p (m_core_gdbarch
))
1676 x86_xsave_layout layout
;
1677 if (!gdbarch_core_read_x86_xsave_layout (m_core_gdbarch
, layout
))
1686 /* Get a pointer to the current core target. If not connected to a
1687 core target, return NULL. */
1689 static core_target
*
1690 get_current_core_target ()
1692 target_ops
*proc_target
= current_inferior ()->process_target ();
1693 return dynamic_cast<core_target
*> (proc_target
);
1696 /* Display file backed mappings from core file. */
1699 core_target::info_proc_mappings (struct gdbarch
*gdbarch
)
1701 if (m_core_file_mappings
.empty ())
1704 gdb_printf (_("Mapped address spaces:\n\n"));
1705 ui_out_emit_table
emitter (current_uiout
, 5, -1, "ProcMappings");
1707 int width
= gdbarch_addr_bit (gdbarch
) == 32 ? 10 : 18;
1708 current_uiout
->table_header (width
, ui_left
, "start", "Start Addr");
1709 current_uiout
->table_header (width
, ui_left
, "end", "End Addr");
1710 current_uiout
->table_header (width
, ui_left
, "size", "Size");
1711 current_uiout
->table_header (width
, ui_left
, "offset", "Offset");
1712 current_uiout
->table_header (0, ui_left
, "objfile", "File");
1713 current_uiout
->table_body ();
1715 for (const target_section
&tsp
: m_core_file_mappings
)
1717 ULONGEST start
= tsp
.addr
;
1718 ULONGEST end
= tsp
.endaddr
;
1719 ULONGEST file_ofs
= tsp
.the_bfd_section
->filepos
;
1720 const char *filename
= bfd_get_filename (tsp
.the_bfd_section
->owner
);
1722 ui_out_emit_tuple
tuple_emitter (current_uiout
, nullptr);
1723 current_uiout
->field_core_addr ("start", gdbarch
, start
);
1724 current_uiout
->field_core_addr ("end", gdbarch
, end
);
1725 /* These next two aren't really addresses and so shouldn't be
1727 current_uiout
->field_string ("size", paddress (gdbarch
, end
- start
));
1728 current_uiout
->field_string ("offset", paddress (gdbarch
, file_ofs
));
1729 current_uiout
->field_string ("objfile", filename
,
1730 file_name_style
.style ());
1731 current_uiout
->text ("\n");
1735 /* Implement "maintenance print core-file-backed-mappings" command.
1737 If mappings are loaded, the results should be similar to the
1738 mappings shown by "info proc mappings". This command is mainly a
1739 debugging tool for GDB developers to make sure that the expected
1740 mappings are present after loading a core file. For Linux, the
1741 output provided by this command will be very similar (if not
1742 identical) to that provided by "info proc mappings". This is not
1743 necessarily the case for other OSes which might provide
1744 more/different information in the "info proc mappings" output. */
1747 maintenance_print_core_file_backed_mappings (const char *args
, int from_tty
)
1749 core_target
*targ
= get_current_core_target ();
1750 if (targ
!= nullptr)
1751 targ
->info_proc_mappings (targ
->core_gdbarch ());
1754 /* Add more details discovered while processing the core-file's mapped file
1755 information, we're building maps between filenames and the corresponding
1756 build-ids, between address ranges and the corresponding build-ids, and
1757 also a reverse map between build-id and the corresponding filename.
1759 SONAME is the DT_SONAME attribute extracted from the .dynamic section of
1760 a shared library that was mapped into the core file. This can be
1761 nullptr if the mapped files was not a shared library, or didn't have a
1762 DT_SONAME attribute.
1764 EXPECTED_FILENAME is the name of the file that was mapped into the
1765 inferior as extracted from the core file, this should never be nullptr.
1767 ACTUAL_FILENAME is the name of the actual file GDB found to provide the
1768 mapped file information, this can be nullptr if GDB failed to find a
1769 suitable file. This might be different to EXPECTED_FILENAME, e.g. GDB
1770 might have downloaded the file from debuginfod and so ACTUAL_FILENAME
1771 will be a file in the debuginfod client cache.
1773 RANGES is the list of memory ranges at which this file was mapped into
1776 BUILD_ID is the build-id for this mapped file, this will never be
1777 nullptr. Not every mapped file will have a build-id, but there's no
1778 point calling this function if we failed to find a build-id; this
1779 structure only exists so we can lookup files based on their build-id. */
1782 mapped_file_info::add (const char *soname
,
1783 const char *expected_filename
,
1784 const char *actual_filename
,
1785 std::vector
<mem_range
> &&ranges
,
1786 const bfd_build_id
*build_id
)
1788 gdb_assert (build_id
!= nullptr);
1789 gdb_assert (expected_filename
!= nullptr);
1791 if (soname
!= nullptr)
1793 /* If we already have an entry with this SONAME then this indicates
1794 that the inferior has two files mapped into memory with different
1795 file names (and most likely different build-ids), but with the
1796 same DT_SONAME attribute. In this case we can't use the
1797 DT_SONAME to figure out the expected build-id of a shared
1798 library, so poison the entry for this SONAME by setting the entry
1800 auto it
= m_soname_to_build_id_map
.find (soname
);
1801 if (it
!= m_soname_to_build_id_map
.end ()
1802 && it
->second
!= nullptr
1803 && !build_id_equal (it
->second
, build_id
))
1804 m_soname_to_build_id_map
[soname
] = nullptr;
1806 m_soname_to_build_id_map
[soname
] = build_id
;
1809 /* When the core file is initially opened and the mapped files are
1810 parsed, we group the build-id information based on the file name. As
1811 a consequence, we should see each EXPECTED_FILENAME value exactly
1812 once. This means that each insertion should always succeed. */
1814 = m_filename_to_build_id_map
.emplace (expected_filename
, build_id
).second
;
1815 gdb_assert (inserted
);
1817 /* Setup the reverse build-id to file name map. */
1818 if (actual_filename
!= nullptr)
1819 m_build_id_to_filename_map
.emplace (build_id
, actual_filename
);
1821 /* Setup the list of memory range to build-id objects. */
1822 for (mem_range
&r
: ranges
)
1823 m_address_to_build_id_list
.emplace_back (std::move (r
), build_id
);
1825 /* At this point the m_address_to_build_id_list is unsorted (we just
1826 added some entries to the end of the list). All entries should be
1827 added before any look-ups are performed, and the list is only sorted
1828 when the first look-up is performed. */
1829 gdb_assert (!m_address_to_build_id_list_sorted
);
1832 /* FILENAME is the name of a file GDB is trying to load, and ADDR is
1833 (optionally) an address within the file in the inferior's address space.
1835 Search through the information gathered from the core-file's mapped file
1836 information looking for a file named FILENAME, or for a file that covers
1837 ADDR. If a match is found then return the build-id for the file along
1838 with the location where GDB found the mapped file.
1840 The location of the mapped file might be the empty string if GDB was
1841 unable to find the mapped file.
1843 If no build-id can be found for FILENAME then GDB will return a pair
1844 containing nullptr (for the build-id) and an empty string for the file
1847 std::optional
<core_target_mapped_file_info
>
1848 mapped_file_info::lookup (const char *filename
,
1849 const std::optional
<CORE_ADDR
> &addr
)
1851 if (filename
!= nullptr)
1853 /* If there's a matching entry in m_filename_to_build_id_map then the
1854 associated build-id will not be nullptr, and can be used to
1855 validate that FILENAME is correct. */
1856 auto it
= m_filename_to_build_id_map
.find (filename
);
1857 if (it
!= m_filename_to_build_id_map
.end ())
1858 return make_result (it
->second
);
1861 if (addr
.has_value ())
1863 /* On the first lookup, sort the address_to_build_id_list. */
1864 if (!m_address_to_build_id_list_sorted
)
1866 std::sort (m_address_to_build_id_list
.begin (),
1867 m_address_to_build_id_list
.end (),
1868 [] (const mem_range_and_build_id
&a
,
1869 const mem_range_and_build_id
&b
) {
1870 return a
.range
< b
.range
;
1872 m_address_to_build_id_list_sorted
= true;
1875 /* Look for the first entry whose range's start address is not less
1876 than, or equal too, the address ADDR. If we find such an entry,
1877 then the previous entry's range might contain ADDR. If it does
1878 then that previous entry's build-id can be used. */
1879 auto it
= std::lower_bound
1880 (m_address_to_build_id_list
.begin (),
1881 m_address_to_build_id_list
.end (),
1883 [] (const mem_range_and_build_id
&a
,
1884 const CORE_ADDR
&b
) {
1885 return a
.range
.start
<= b
;
1888 if (it
!= m_address_to_build_id_list
.begin ())
1892 if (it
->range
.contains (*addr
))
1893 return make_result (it
->build_id
);
1897 if (filename
!= nullptr)
1899 /* If the basename of FILENAME appears in m_soname_to_build_id_map
1900 then when the mapped files were processed, we saw a file with a
1901 DT_SONAME attribute corresponding to FILENAME, use that build-id
1902 to validate FILENAME.
1904 However, the build-id in this map might be nullptr if we saw
1905 multiple mapped files with the same DT_SONAME attribute (though
1906 this should be pretty rare). */
1908 = m_soname_to_build_id_map
.find (lbasename (filename
));
1909 if (it
!= m_soname_to_build_id_map
.end ()
1910 && it
->second
!= nullptr)
1911 return make_result (it
->second
);
1917 /* See gdbcore.h. */
1919 std::optional
<core_target_mapped_file_info
>
1920 core_target_find_mapped_file (const char *filename
,
1921 std::optional
<CORE_ADDR
> addr
)
1923 core_target
*targ
= get_current_core_target ();
1924 if (targ
== nullptr || current_program_space
->cbfd
.get () == nullptr)
1927 return targ
->lookup_mapped_file_info (filename
, addr
);
1930 void _initialize_corelow ();
1932 _initialize_corelow ()
1934 add_target (core_target_info
, core_target_open
,
1935 filename_maybe_quoted_completer
);
1936 add_cmd ("core-file-backed-mappings", class_maintenance
,
1937 maintenance_print_core_file_backed_mappings
,
1938 _("Print core file's file-backed mappings."),
1939 &maintenanceprintlist
);