1 /* Core dump and executable file functions below target vector, for GDB.
3 Copyright (C) 1986-2022 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"
60 /* The core file target. */
62 static const target_info core_target_info
= {
64 N_("Local core dump file"),
65 N_("Use a core file as a target.\n\
66 Specify the filename of the core file.")
69 class core_target final
: public process_stratum_target
74 const target_info
&info () const override
75 { return core_target_info
; }
77 void close () override
;
78 void detach (inferior
*, int) override
;
79 void fetch_registers (struct regcache
*, int) override
;
81 enum target_xfer_status
xfer_partial (enum target_object object
,
84 const gdb_byte
*writebuf
,
85 ULONGEST offset
, ULONGEST len
,
86 ULONGEST
*xfered_len
) override
;
87 void files_info () override
;
89 bool thread_alive (ptid_t ptid
) override
;
90 const struct target_desc
*read_description () override
;
92 std::string
pid_to_str (ptid_t
) override
;
94 const char *thread_name (struct thread_info
*) override
;
96 bool has_all_memory () override
{ return true; }
97 bool has_memory () override
;
98 bool has_stack () override
;
99 bool has_registers () override
;
100 bool has_execution (inferior
*inf
) override
{ return false; }
102 bool info_proc (const char *, enum info_proc_what
) override
;
106 /* Getter, see variable definition. */
107 struct gdbarch
*core_gdbarch ()
109 return m_core_gdbarch
;
112 /* See definition. */
113 void get_core_register_section (struct regcache
*regcache
,
114 const struct regset
*regset
,
116 int section_min_size
,
117 const char *human_name
,
120 /* See definition. */
121 void info_proc_mappings (struct gdbarch
*gdbarch
);
123 private: /* per-core data */
125 /* The core's section table. Note that these target sections are
126 *not* mapped in the current address spaces' set of target
127 sections --- those should come only from pure executable or
128 shared library bfds. The core bfd sections are an implementation
129 detail of the core target, just like ptrace is for unix child
131 target_section_table m_core_section_table
;
133 /* File-backed address space mappings: some core files include
134 information about memory mapped files. */
135 target_section_table m_core_file_mappings
;
137 /* Unavailable mappings. These correspond to pathnames which either
138 weren't found or could not be opened. Knowing these addresses can
140 std::vector
<mem_range
> m_core_unavailable_mappings
;
142 /* Build m_core_file_mappings. Called from the constructor. */
143 void build_file_mappings ();
145 /* Helper method for xfer_partial. */
146 enum target_xfer_status
xfer_memory_via_mappings (gdb_byte
*readbuf
,
147 const gdb_byte
*writebuf
,
150 ULONGEST
*xfered_len
);
152 /* FIXME: kettenis/20031023: Eventually this field should
154 struct gdbarch
*m_core_gdbarch
= NULL
;
157 core_target::core_target ()
159 /* Find a first arch based on the BFD. We need the initial gdbarch so
160 we can setup the hooks to find a target description. */
161 m_core_gdbarch
= gdbarch_from_bfd (core_bfd
);
163 /* If the arch is able to read a target description from the core, it
164 could yield a more specific gdbarch. */
165 const struct target_desc
*tdesc
= read_description ();
167 if (tdesc
!= nullptr)
169 struct gdbarch_info info
;
170 info
.abfd
= core_bfd
;
171 info
.target_desc
= tdesc
;
172 m_core_gdbarch
= gdbarch_find_by_info (info
);
176 || !gdbarch_iterate_over_regset_sections_p (m_core_gdbarch
))
177 error (_("\"%s\": Core file format not supported"),
178 bfd_get_filename (core_bfd
));
180 /* Find the data section */
181 m_core_section_table
= build_section_table (core_bfd
);
183 build_file_mappings ();
186 /* Construct the target_section_table for file-backed mappings if
189 For each unique path in the note, we'll open a BFD with a bfd
190 target of "binary". This is an unstructured bfd target upon which
191 we'll impose a structure from the mappings in the architecture-specific
192 mappings note. A BFD section is allocated and initialized for each
195 We take care to not share already open bfds with other parts of
196 GDB; in particular, we don't want to add new sections to existing
197 BFDs. We do, however, ensure that the BFDs that we allocate here
198 will go away (be deallocated) when the core target is detached. */
201 core_target::build_file_mappings ()
203 std::unordered_map
<std::string
, struct bfd
*> bfd_map
;
204 std::unordered_set
<std::string
> unavailable_paths
;
206 /* See linux_read_core_file_mappings() in linux-tdep.c for an example
207 read_core_file_mappings method. */
208 gdbarch_read_core_file_mappings (m_core_gdbarch
, core_bfd
,
210 /* After determining the number of mappings, read_core_file_mappings
211 will invoke this lambda. */
216 /* read_core_file_mappings will invoke this lambda for each mapping
218 [&] (int num
, ULONGEST start
, ULONGEST end
, ULONGEST file_ofs
,
219 const char *filename
, const bfd_build_id
*build_id
)
221 /* Architecture-specific read_core_mapping methods are expected to
222 weed out non-file-backed mappings. */
223 gdb_assert (filename
!= nullptr);
225 struct bfd
*bfd
= bfd_map
[filename
];
228 /* Use exec_file_find() to do sysroot expansion. It'll
229 also strip the potential sysroot "target:" prefix. If
230 there is no sysroot, an equivalent (possibly more
231 canonical) pathname will be provided. */
232 gdb::unique_xmalloc_ptr
<char> expanded_fname
233 = exec_file_find (filename
, NULL
);
235 if (expanded_fname
== nullptr && build_id
!= nullptr)
236 debuginfod_exec_query (build_id
->data
, build_id
->size
,
237 filename
, &expanded_fname
);
239 if (expanded_fname
== nullptr)
241 m_core_unavailable_mappings
.emplace_back (start
, end
- start
);
242 /* Print just one warning per path. */
243 if (unavailable_paths
.insert (filename
).second
)
244 warning (_("Can't open file %s during file-backed mapping "
250 bfd
= bfd_map
[filename
] = bfd_openr (expanded_fname
.get (),
253 if (bfd
== nullptr || !bfd_check_format (bfd
, bfd_object
))
255 m_core_unavailable_mappings
.emplace_back (start
, end
- start
);
256 /* If we get here, there's a good chance that it's due to
257 an internal error. We issue a warning instead of an
258 internal error because of the possibility that the
259 file was removed in between checking for its
260 existence during the expansion in exec_file_find()
261 and the calls to bfd_openr() / bfd_check_format().
262 Output both the path from the core file note along
263 with its expansion to make debugging this problem
265 warning (_("Can't open file %s which was expanded to %s "
266 "during file-backed mapping note processing"),
267 filename
, expanded_fname
.get ());
272 /* Ensure that the bfd will be closed when core_bfd is closed.
273 This can be checked before/after a core file detach via
274 "maint info bfds". */
275 gdb_bfd_record_inclusion (core_bfd
, bfd
);
278 /* Make new BFD section. All sections have the same name,
279 which is permitted by bfd_make_section_anyway(). */
280 asection
*sec
= bfd_make_section_anyway (bfd
, "load");
282 error (_("Can't make section"));
283 sec
->filepos
= file_ofs
;
284 bfd_set_section_flags (sec
, SEC_READONLY
| SEC_HAS_CONTENTS
);
285 bfd_set_section_size (sec
, end
- start
);
286 bfd_set_section_vma (sec
, start
);
287 bfd_set_section_lma (sec
, start
);
288 bfd_set_section_alignment (sec
, 2);
290 /* Set target_section fields. */
291 m_core_file_mappings
.emplace_back (start
, end
, sec
);
293 /* If this is a bfd of a shared library, record its soname
295 if (build_id
!= nullptr)
297 gdb::unique_xmalloc_ptr
<char> soname
298 = gdb_bfd_read_elf_soname (bfd
->filename
);
299 if (soname
!= nullptr)
300 set_cbfd_soname_build_id (current_program_space
->cbfd
,
301 soname
.get (), build_id
);
305 normalize_mem_ranges (&m_core_unavailable_mappings
);
308 /* An arbitrary identifier for the core inferior. */
309 #define CORELOW_PID 1
311 /* Close the core target. */
314 core_target::close ()
318 switch_to_no_thread (); /* Avoid confusion from thread
320 exit_inferior_silent (current_inferior ());
322 /* Clear out solib state while the bfd is still open. See
323 comments in clear_solib in solib.c. */
326 current_program_space
->cbfd
.reset (nullptr);
329 /* Core targets are heap-allocated (see core_target_open), so here
330 we delete ourselves. */
334 /* Look for sections whose names start with `.reg/' so that we can
335 extract the list of threads in a core file. */
338 add_to_thread_list (asection
*asect
, asection
*reg_sect
)
342 bool fake_pid_p
= false;
343 struct inferior
*inf
;
345 if (!startswith (bfd_section_name (asect
), ".reg/"))
348 core_tid
= atoi (bfd_section_name (asect
) + 5);
350 pid
= bfd_core_file_pid (core_bfd
);
359 inf
= current_inferior ();
362 inferior_appeared (inf
, pid
);
363 inf
->fake_pid_p
= fake_pid_p
;
366 ptid_t
ptid (pid
, lwpid
);
368 thread_info
*thr
= add_thread (inf
->process_target (), ptid
);
370 /* Warning, Will Robinson, looking at BFD private data! */
373 && asect
->filepos
== reg_sect
->filepos
) /* Did we find .reg? */
374 switch_to_thread (thr
); /* Yes, make it current. */
377 /* Issue a message saying we have no core to debug, if FROM_TTY. */
380 maybe_say_no_core_file_now (int from_tty
)
383 gdb_printf (_("No core file now.\n"));
386 /* Backward compatibility with old way of specifying core files. */
389 core_file_command (const char *filename
, int from_tty
)
391 dont_repeat (); /* Either way, seems bogus. */
393 if (filename
== NULL
)
395 if (core_bfd
!= NULL
)
397 target_detach (current_inferior (), from_tty
);
398 gdb_assert (core_bfd
== NULL
);
401 maybe_say_no_core_file_now (from_tty
);
404 core_target_open (filename
, from_tty
);
407 /* Locate (and load) an executable file (and symbols) given the core file
411 locate_exec_from_corefile_build_id (bfd
*abfd
, int from_tty
)
413 const bfd_build_id
*build_id
= build_id_bfd_get (abfd
);
414 if (build_id
== nullptr)
417 gdb_bfd_ref_ptr execbfd
418 = build_id_to_exec_bfd (build_id
->size
, build_id
->data
);
420 if (execbfd
== nullptr)
422 /* Attempt to query debuginfod for the executable. */
423 gdb::unique_xmalloc_ptr
<char> execpath
;
424 scoped_fd fd
= debuginfod_exec_query (build_id
->data
, build_id
->size
,
425 abfd
->filename
, &execpath
);
429 execbfd
= gdb_bfd_open (execpath
.get (), gnutarget
);
431 if (execbfd
== nullptr)
432 warning (_("\"%s\" from debuginfod cannot be opened as bfd: %s"),
434 gdb_bfd_errmsg (bfd_get_error (), nullptr).c_str ());
435 else if (!build_id_verify (execbfd
.get (), build_id
->size
,
437 execbfd
.reset (nullptr);
441 if (execbfd
!= nullptr)
443 exec_file_attach (bfd_get_filename (execbfd
.get ()), from_tty
);
444 symbol_file_add_main (bfd_get_filename (execbfd
.get ()),
445 symfile_add_flag (from_tty
? SYMFILE_VERBOSE
: 0));
452 core_target_open (const char *arg
, int from_tty
)
459 target_preopen (from_tty
);
463 error (_("No core file specified. (Use `detach' "
464 "to stop debugging a core file.)"));
466 error (_("No core file specified."));
469 gdb::unique_xmalloc_ptr
<char> filename (tilde_expand (arg
));
470 if (strlen (filename
.get ()) != 0
471 && !IS_ABSOLUTE_PATH (filename
.get ()))
472 filename
= make_unique_xstrdup (gdb_abspath (filename
.get ()).c_str ());
474 flags
= O_BINARY
| O_LARGEFILE
;
479 scratch_chan
= gdb_open_cloexec (filename
.get (), flags
, 0).release ();
480 if (scratch_chan
< 0)
481 perror_with_name (filename
.get ());
483 gdb_bfd_ref_ptr
temp_bfd (gdb_bfd_fopen (filename
.get (), gnutarget
,
484 write_files
? FOPEN_RUB
: FOPEN_RB
,
486 if (temp_bfd
== NULL
)
487 perror_with_name (filename
.get ());
489 if (!bfd_check_format (temp_bfd
.get (), bfd_core
))
491 /* Do it after the err msg */
492 /* FIXME: should be checking for errors from bfd_close (for one
493 thing, on error it does not free all the storage associated
495 error (_("\"%s\" is not a core dump: %s"),
496 filename
.get (), bfd_errmsg (bfd_get_error ()));
499 current_program_space
->cbfd
= std::move (temp_bfd
);
501 core_target
*target
= new core_target ();
503 /* Own the target until it is successfully pushed. */
504 target_ops_up
target_holder (target
);
508 /* If we have no exec file, try to set the architecture from the
509 core file. We don't do this unconditionally since an exec file
510 typically contains more information that helps us determine the
511 architecture than a core file. */
512 if (!current_program_space
->exec_bfd ())
513 set_gdbarch_from_file (core_bfd
);
515 current_inferior ()->push_target (std::move (target_holder
));
517 switch_to_no_thread ();
519 /* Need to flush the register cache (and the frame cache) from a
520 previous debug session. If inferior_ptid ends up the same as the
521 last debug session --- e.g., b foo; run; gcore core1; step; gcore
522 core2; core core1; core core2 --- then there's potential for
523 get_current_regcache to return the cached regcache of the
524 previous session, and the frame cache being stale. */
525 registers_changed ();
527 /* Build up thread list from BFD sections, and possibly set the
528 current thread to the .reg/NN section matching the .reg
530 asection
*reg_sect
= bfd_get_section_by_name (core_bfd
, ".reg");
531 for (asection
*sect
: gdb_bfd_sections (core_bfd
))
532 add_to_thread_list (sect
, reg_sect
);
534 if (inferior_ptid
== null_ptid
)
536 /* Either we found no .reg/NN section, and hence we have a
537 non-threaded core (single-threaded, from gdb's perspective),
538 or for some reason add_to_thread_list couldn't determine
539 which was the "main" thread. The latter case shouldn't
540 usually happen, but we're dealing with input here, which can
541 always be broken in different ways. */
542 thread_info
*thread
= first_thread_of_inferior (current_inferior ());
546 inferior_appeared (current_inferior (), CORELOW_PID
);
547 thread
= add_thread_silent (target
, ptid_t (CORELOW_PID
));
550 switch_to_thread (thread
);
553 if (current_program_space
->exec_bfd () == nullptr)
554 locate_exec_from_corefile_build_id (core_bfd
, from_tty
);
556 post_create_inferior (from_tty
);
558 /* Now go through the target stack looking for threads since there
559 may be a thread_stratum target loaded on top of target core by
560 now. The layer above should claim threads found in the BFD
564 target_update_thread_list ();
567 catch (const gdb_exception_error
&except
)
569 exception_print (gdb_stderr
, except
);
572 p
= bfd_core_file_failing_command (core_bfd
);
574 gdb_printf (_("Core was generated by `%s'.\n"), p
);
576 /* Clearing any previous state of convenience variables. */
577 clear_exit_convenience_vars ();
579 siggy
= bfd_core_file_failing_signal (core_bfd
);
582 gdbarch
*core_gdbarch
= target
->core_gdbarch ();
584 /* If we don't have a CORE_GDBARCH to work with, assume a native
585 core (map gdb_signal from host signals). If we do have
586 CORE_GDBARCH to work with, but no gdb_signal_from_target
587 implementation for that gdbarch, as a fallback measure,
588 assume the host signal mapping. It'll be correct for native
589 cores, but most likely incorrect for cross-cores. */
590 enum gdb_signal sig
= (core_gdbarch
!= NULL
591 && gdbarch_gdb_signal_from_target_p (core_gdbarch
)
592 ? gdbarch_gdb_signal_from_target (core_gdbarch
,
594 : gdb_signal_from_host (siggy
));
596 gdb_printf (_("Program terminated with signal %s, %s"),
597 gdb_signal_to_name (sig
), gdb_signal_to_string (sig
));
598 if (gdbarch_report_signal_info_p (core_gdbarch
))
599 gdbarch_report_signal_info (core_gdbarch
, current_uiout
, sig
);
600 gdb_printf (_(".\n"));
602 /* Set the value of the internal variable $_exitsignal,
603 which holds the signal uncaught by the inferior. */
604 set_internalvar_integer (lookup_internalvar ("_exitsignal"),
608 /* Fetch all registers from core file. */
609 target_fetch_registers (get_current_regcache (), -1);
611 /* Now, set up the frame cache, and print the top of stack. */
612 reinit_frame_cache ();
613 print_stack_frame (get_selected_frame (NULL
), 1, SRC_AND_LOC
, 1);
615 /* Current thread should be NUM 1 but the user does not know that.
616 If a program is single threaded gdb in general does not mention
617 anything about threads. That is why the test is >= 2. */
618 if (thread_count (target
) >= 2)
622 thread_command (NULL
, from_tty
);
624 catch (const gdb_exception_error
&except
)
626 exception_print (gdb_stderr
, except
);
632 core_target::detach (inferior
*inf
, int from_tty
)
634 /* Note that 'this' is dangling after this call. unpush_target
635 closes the target, and our close implementation deletes
637 inf
->unpush_target (this);
639 /* Clear the register cache and the frame cache. */
640 registers_changed ();
641 reinit_frame_cache ();
642 maybe_say_no_core_file_now (from_tty
);
645 /* Try to retrieve registers from a section in core_bfd, and supply
648 If ptid's lwp member is zero, do the single-threaded
649 thing: look for a section named NAME. If ptid's lwp
650 member is non-zero, do the multi-threaded thing: look for a section
651 named "NAME/LWP", where LWP is the shortest ASCII decimal
652 representation of ptid's lwp member.
654 HUMAN_NAME is a human-readable name for the kind of registers the
655 NAME section contains, for use in error messages.
657 If REQUIRED is true, print an error if the core file doesn't have a
658 section by the appropriate name. Otherwise, just do nothing. */
661 core_target::get_core_register_section (struct regcache
*regcache
,
662 const struct regset
*regset
,
664 int section_min_size
,
665 const char *human_name
,
668 gdb_assert (regset
!= nullptr);
670 struct bfd_section
*section
;
672 bool variable_size_section
= (regset
->flags
& REGSET_VARIABLE_SIZE
);
674 thread_section_name
section_name (name
, regcache
->ptid ());
676 section
= bfd_get_section_by_name (core_bfd
, section_name
.c_str ());
680 warning (_("Couldn't find %s registers in core file."),
685 size
= bfd_section_size (section
);
686 if (size
< section_min_size
)
688 warning (_("Section `%s' in core file too small."),
689 section_name
.c_str ());
692 if (size
!= section_min_size
&& !variable_size_section
)
694 warning (_("Unexpected size of section `%s' in core file."),
695 section_name
.c_str ());
698 gdb::byte_vector
contents (size
);
699 if (!bfd_get_section_contents (core_bfd
, section
, contents
.data (),
702 warning (_("Couldn't read %s registers from `%s' section in core file."),
703 human_name
, section_name
.c_str ());
707 regset
->supply_regset (regset
, regcache
, -1, contents
.data (), size
);
710 /* Data passed to gdbarch_iterate_over_regset_sections's callback. */
711 struct get_core_registers_cb_data
714 struct regcache
*regcache
;
717 /* Callback for get_core_registers that handles a single core file
718 register note section. */
721 get_core_registers_cb (const char *sect_name
, int supply_size
, int collect_size
,
722 const struct regset
*regset
,
723 const char *human_name
, void *cb_data
)
725 gdb_assert (regset
!= nullptr);
727 auto *data
= (get_core_registers_cb_data
*) cb_data
;
728 bool required
= false;
729 bool variable_size_section
= (regset
->flags
& REGSET_VARIABLE_SIZE
);
731 if (!variable_size_section
)
732 gdb_assert (supply_size
== collect_size
);
734 if (strcmp (sect_name
, ".reg") == 0)
737 if (human_name
== NULL
)
738 human_name
= "general-purpose";
740 else if (strcmp (sect_name
, ".reg2") == 0)
742 if (human_name
== NULL
)
743 human_name
= "floating-point";
746 data
->target
->get_core_register_section (data
->regcache
, regset
, sect_name
,
747 supply_size
, human_name
, required
);
750 /* Get the registers out of a core file. This is the machine-
751 independent part. Fetch_core_registers is the machine-dependent
752 part, typically implemented in the xm-file for each
755 /* We just get all the registers, so we don't use regno. */
758 core_target::fetch_registers (struct regcache
*regcache
, int regno
)
760 if (!(m_core_gdbarch
!= nullptr
761 && gdbarch_iterate_over_regset_sections_p (m_core_gdbarch
)))
763 gdb_printf (gdb_stderr
,
764 "Can't fetch registers from this type of core file\n");
768 struct gdbarch
*gdbarch
= regcache
->arch ();
769 get_core_registers_cb_data data
= { this, regcache
};
770 gdbarch_iterate_over_regset_sections (gdbarch
,
771 get_core_registers_cb
,
772 (void *) &data
, NULL
);
774 /* Mark all registers not found in the core as unavailable. */
775 for (int i
= 0; i
< gdbarch_num_regs (regcache
->arch ()); i
++)
776 if (regcache
->get_register_status (i
) == REG_UNKNOWN
)
777 regcache
->raw_supply (i
, NULL
);
781 core_target::files_info ()
783 print_section_info (&m_core_section_table
, core_bfd
);
786 /* Helper method for core_target::xfer_partial. */
788 enum target_xfer_status
789 core_target::xfer_memory_via_mappings (gdb_byte
*readbuf
,
790 const gdb_byte
*writebuf
,
791 ULONGEST offset
, ULONGEST len
,
792 ULONGEST
*xfered_len
)
794 enum target_xfer_status xfer_status
;
796 xfer_status
= (section_table_xfer_memory_partial
798 offset
, len
, xfered_len
,
799 m_core_file_mappings
));
801 if (xfer_status
== TARGET_XFER_OK
|| m_core_unavailable_mappings
.empty ())
804 /* There are instances - e.g. when debugging within a docker
805 container using the AUFS storage driver - where the pathnames
806 obtained from the note section are incorrect. Despite the path
807 being wrong, just knowing the start and end addresses of the
808 mappings is still useful; we can attempt an access of the file
809 stratum constrained to the address ranges corresponding to the
810 unavailable mappings. */
812 ULONGEST memaddr
= offset
;
813 ULONGEST memend
= offset
+ len
;
815 for (const auto &mr
: m_core_unavailable_mappings
)
817 if (address_in_mem_range (memaddr
, &mr
))
819 if (!address_in_mem_range (memend
, &mr
))
820 len
= mr
.start
+ mr
.length
- memaddr
;
822 xfer_status
= this->beneath ()->xfer_partial (TARGET_OBJECT_MEMORY
,
836 enum target_xfer_status
837 core_target::xfer_partial (enum target_object object
, const char *annex
,
838 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
839 ULONGEST offset
, ULONGEST len
, ULONGEST
*xfered_len
)
843 case TARGET_OBJECT_MEMORY
:
845 enum target_xfer_status xfer_status
;
847 /* Try accessing memory contents from core file data,
848 restricting consideration to those sections for which
849 the BFD section flag SEC_HAS_CONTENTS is set. */
850 auto has_contents_cb
= [] (const struct target_section
*s
)
852 return ((s
->the_bfd_section
->flags
& SEC_HAS_CONTENTS
) != 0);
854 xfer_status
= section_table_xfer_memory_partial
856 offset
, len
, xfered_len
,
857 m_core_section_table
,
859 if (xfer_status
== TARGET_XFER_OK
)
860 return TARGET_XFER_OK
;
862 /* Check file backed mappings. If they're available, use
863 core file provided mappings (e.g. from .note.linuxcore.file
864 or the like) as this should provide a more accurate
865 result. If not, check the stratum beneath us, which should
868 We also check unavailable mappings due to Docker/AUFS driver
870 if (!m_core_file_mappings
.empty ()
871 || !m_core_unavailable_mappings
.empty ())
873 xfer_status
= xfer_memory_via_mappings (readbuf
, writebuf
, offset
,
877 xfer_status
= this->beneath ()->xfer_partial (object
, annex
, readbuf
,
878 writebuf
, offset
, len
,
880 if (xfer_status
== TARGET_XFER_OK
)
881 return TARGET_XFER_OK
;
883 /* Finally, attempt to access data in core file sections with
884 no contents. These will typically read as all zero. */
885 auto no_contents_cb
= [&] (const struct target_section
*s
)
887 return !has_contents_cb (s
);
889 xfer_status
= section_table_xfer_memory_partial
891 offset
, len
, xfered_len
,
892 m_core_section_table
,
897 case TARGET_OBJECT_AUXV
:
900 /* When the aux vector is stored in core file, BFD
901 represents this with a fake section called ".auxv". */
903 struct bfd_section
*section
;
906 section
= bfd_get_section_by_name (core_bfd
, ".auxv");
908 return TARGET_XFER_E_IO
;
910 size
= bfd_section_size (section
);
912 return TARGET_XFER_EOF
;
918 return TARGET_XFER_EOF
;
919 if (!bfd_get_section_contents (core_bfd
, section
, readbuf
,
920 (file_ptr
) offset
, size
))
922 warning (_("Couldn't read NT_AUXV note in core file."));
923 return TARGET_XFER_E_IO
;
926 *xfered_len
= (ULONGEST
) size
;
927 return TARGET_XFER_OK
;
929 return TARGET_XFER_E_IO
;
931 case TARGET_OBJECT_WCOOKIE
:
934 /* When the StackGhost cookie is stored in core file, BFD
935 represents this with a fake section called
938 struct bfd_section
*section
;
941 section
= bfd_get_section_by_name (core_bfd
, ".wcookie");
943 return TARGET_XFER_E_IO
;
945 size
= bfd_section_size (section
);
947 return TARGET_XFER_EOF
;
953 return TARGET_XFER_EOF
;
954 if (!bfd_get_section_contents (core_bfd
, section
, readbuf
,
955 (file_ptr
) offset
, size
))
957 warning (_("Couldn't read StackGhost cookie in core file."));
958 return TARGET_XFER_E_IO
;
961 *xfered_len
= (ULONGEST
) size
;
962 return TARGET_XFER_OK
;
965 return TARGET_XFER_E_IO
;
967 case TARGET_OBJECT_LIBRARIES
:
968 if (m_core_gdbarch
!= nullptr
969 && gdbarch_core_xfer_shared_libraries_p (m_core_gdbarch
))
972 return TARGET_XFER_E_IO
;
975 *xfered_len
= gdbarch_core_xfer_shared_libraries (m_core_gdbarch
,
979 if (*xfered_len
== 0)
980 return TARGET_XFER_EOF
;
982 return TARGET_XFER_OK
;
985 return TARGET_XFER_E_IO
;
987 case TARGET_OBJECT_LIBRARIES_AIX
:
988 if (m_core_gdbarch
!= nullptr
989 && gdbarch_core_xfer_shared_libraries_aix_p (m_core_gdbarch
))
992 return TARGET_XFER_E_IO
;
996 = gdbarch_core_xfer_shared_libraries_aix (m_core_gdbarch
,
1000 if (*xfered_len
== 0)
1001 return TARGET_XFER_EOF
;
1003 return TARGET_XFER_OK
;
1006 return TARGET_XFER_E_IO
;
1008 case TARGET_OBJECT_SIGNAL_INFO
:
1011 if (m_core_gdbarch
!= nullptr
1012 && gdbarch_core_xfer_siginfo_p (m_core_gdbarch
))
1014 LONGEST l
= gdbarch_core_xfer_siginfo (m_core_gdbarch
, readbuf
,
1021 return TARGET_XFER_EOF
;
1023 return TARGET_XFER_OK
;
1027 return TARGET_XFER_E_IO
;
1030 return this->beneath ()->xfer_partial (object
, annex
, readbuf
,
1031 writebuf
, offset
, len
,
1038 /* Okay, let's be honest: threads gleaned from a core file aren't
1039 exactly lively, are they? On the other hand, if we don't claim
1040 that each & every one is alive, then we don't get any of them
1041 to appear in an "info thread" command, which is quite a useful
1045 core_target::thread_alive (ptid_t ptid
)
1050 /* Ask the current architecture what it knows about this core file.
1051 That will be used, in turn, to pick a better architecture. This
1052 wrapper could be avoided if targets got a chance to specialize
1055 const struct target_desc
*
1056 core_target::read_description ()
1058 /* If the core file contains a target description note then we will use
1059 that in preference to anything else. */
1060 bfd_size_type tdesc_note_size
= 0;
1061 struct bfd_section
*tdesc_note_section
1062 = bfd_get_section_by_name (core_bfd
, ".gdb-tdesc");
1063 if (tdesc_note_section
!= nullptr)
1064 tdesc_note_size
= bfd_section_size (tdesc_note_section
);
1065 if (tdesc_note_size
> 0)
1067 gdb::char_vector
contents (tdesc_note_size
+ 1);
1068 if (bfd_get_section_contents (core_bfd
, tdesc_note_section
,
1069 contents
.data (), (file_ptr
) 0,
1072 /* Ensure we have a null terminator. */
1073 contents
[tdesc_note_size
] = '\0';
1074 const struct target_desc
*result
1075 = string_read_description_xml (contents
.data ());
1076 if (result
!= nullptr)
1081 if (m_core_gdbarch
&& gdbarch_core_read_description_p (m_core_gdbarch
))
1083 const struct target_desc
*result
;
1085 result
= gdbarch_core_read_description (m_core_gdbarch
, this, core_bfd
);
1090 return this->beneath ()->read_description ();
1094 core_target::pid_to_str (ptid_t ptid
)
1096 struct inferior
*inf
;
1099 /* The preferred way is to have a gdbarch/OS specific
1101 if (m_core_gdbarch
!= nullptr
1102 && gdbarch_core_pid_to_str_p (m_core_gdbarch
))
1103 return gdbarch_core_pid_to_str (m_core_gdbarch
, ptid
);
1105 /* Otherwise, if we don't have one, we'll just fallback to
1106 "process", with normal_pid_to_str. */
1108 /* Try the LWPID field first. */
1111 return normal_pid_to_str (ptid_t (pid
));
1113 /* Otherwise, this isn't a "threaded" core -- use the PID field, but
1114 only if it isn't a fake PID. */
1115 inf
= find_inferior_ptid (this, ptid
);
1116 if (inf
!= NULL
&& !inf
->fake_pid_p
)
1117 return normal_pid_to_str (ptid
);
1119 /* No luck. We simply don't have a valid PID to print. */
1120 return "<main task>";
1124 core_target::thread_name (struct thread_info
*thr
)
1126 if (m_core_gdbarch
!= nullptr
1127 && gdbarch_core_thread_name_p (m_core_gdbarch
))
1128 return gdbarch_core_thread_name (m_core_gdbarch
, thr
);
1133 core_target::has_memory ()
1135 return (core_bfd
!= NULL
);
1139 core_target::has_stack ()
1141 return (core_bfd
!= NULL
);
1145 core_target::has_registers ()
1147 return (core_bfd
!= NULL
);
1150 /* Implement the to_info_proc method. */
1153 core_target::info_proc (const char *args
, enum info_proc_what request
)
1155 struct gdbarch
*gdbarch
= get_current_arch ();
1157 /* Since this is the core file target, call the 'core_info_proc'
1158 method on gdbarch, not 'info_proc'. */
1159 if (gdbarch_core_info_proc_p (gdbarch
))
1160 gdbarch_core_info_proc (gdbarch
, args
, request
);
1165 /* Get a pointer to the current core target. If not connected to a
1166 core target, return NULL. */
1168 static core_target
*
1169 get_current_core_target ()
1171 target_ops
*proc_target
= current_inferior ()->process_target ();
1172 return dynamic_cast<core_target
*> (proc_target
);
1175 /* Display file backed mappings from core file. */
1178 core_target::info_proc_mappings (struct gdbarch
*gdbarch
)
1180 if (!m_core_file_mappings
.empty ())
1182 gdb_printf (_("Mapped address spaces:\n\n"));
1183 if (gdbarch_addr_bit (gdbarch
) == 32)
1185 gdb_printf ("\t%10s %10s %10s %10s %s\n",
1188 " Size", " Offset", "objfile");
1192 gdb_printf (" %18s %18s %10s %10s %s\n",
1195 " Size", " Offset", "objfile");
1199 for (const target_section
&tsp
: m_core_file_mappings
)
1201 ULONGEST start
= tsp
.addr
;
1202 ULONGEST end
= tsp
.endaddr
;
1203 ULONGEST file_ofs
= tsp
.the_bfd_section
->filepos
;
1204 const char *filename
= bfd_get_filename (tsp
.the_bfd_section
->owner
);
1206 if (gdbarch_addr_bit (gdbarch
) == 32)
1207 gdb_printf ("\t%10s %10s %10s %10s %s\n",
1208 paddress (gdbarch
, start
),
1209 paddress (gdbarch
, end
),
1210 hex_string (end
- start
),
1211 hex_string (file_ofs
),
1214 gdb_printf (" %18s %18s %10s %10s %s\n",
1215 paddress (gdbarch
, start
),
1216 paddress (gdbarch
, end
),
1217 hex_string (end
- start
),
1218 hex_string (file_ofs
),
1223 /* Implement "maintenance print core-file-backed-mappings" command.
1225 If mappings are loaded, the results should be similar to the
1226 mappings shown by "info proc mappings". This command is mainly a
1227 debugging tool for GDB developers to make sure that the expected
1228 mappings are present after loading a core file. For Linux, the
1229 output provided by this command will be very similar (if not
1230 identical) to that provided by "info proc mappings". This is not
1231 necessarily the case for other OSes which might provide
1232 more/different information in the "info proc mappings" output. */
1235 maintenance_print_core_file_backed_mappings (const char *args
, int from_tty
)
1237 core_target
*targ
= get_current_core_target ();
1238 if (targ
!= nullptr)
1239 targ
->info_proc_mappings (targ
->core_gdbarch ());
1242 void _initialize_corelow ();
1244 _initialize_corelow ()
1246 add_target (core_target_info
, core_target_open
, filename_completer
);
1247 add_cmd ("core-file-backed-mappings", class_maintenance
,
1248 maintenance_print_core_file_backed_mappings
,
1249 _("Print core file's file-backed mappings."),
1250 &maintenanceprintlist
);