1 /* Core dump and executable file functions below target vector, for GDB.
3 Copyright (C) 1986-2020 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 <unordered_map>
50 #include <unordered_set>
57 /* The core file target. */
59 static const target_info core_target_info
= {
61 N_("Local core dump file"),
62 N_("Use a core file as a target.\n\
63 Specify the filename of the core file.")
66 class core_target final
: public process_stratum_target
70 ~core_target () override
;
72 const target_info
&info () const override
73 { return core_target_info
; }
75 void close () override
;
76 void detach (inferior
*, int) override
;
77 void fetch_registers (struct regcache
*, int) override
;
79 enum target_xfer_status
xfer_partial (enum target_object object
,
82 const gdb_byte
*writebuf
,
83 ULONGEST offset
, ULONGEST len
,
84 ULONGEST
*xfered_len
) override
;
85 void files_info () override
;
87 bool thread_alive (ptid_t ptid
) override
;
88 const struct target_desc
*read_description () override
;
90 std::string
pid_to_str (ptid_t
) override
;
92 const char *thread_name (struct thread_info
*) override
;
94 bool has_all_memory () override
{ return true; }
95 bool has_memory () override
;
96 bool has_stack () override
;
97 bool has_registers () override
;
98 bool has_execution (inferior
*inf
) override
{ return false; }
100 bool info_proc (const char *, enum info_proc_what
) override
;
104 /* Getter, see variable definition. */
105 struct gdbarch
*core_gdbarch ()
107 return m_core_gdbarch
;
110 /* See definition. */
111 void get_core_register_section (struct regcache
*regcache
,
112 const struct regset
*regset
,
114 int section_min_size
,
115 const char *human_name
,
118 /* See definition. */
119 void info_proc_mappings (struct gdbarch
*gdbarch
);
121 private: /* per-core data */
123 /* The core's section table. Note that these target sections are
124 *not* mapped in the current address spaces' set of target
125 sections --- those should come only from pure executable or
126 shared library bfds. The core bfd sections are an implementation
127 detail of the core target, just like ptrace is for unix child
129 target_section_table m_core_section_table
{};
131 /* File-backed address space mappings: some core files include
132 information about memory mapped files. */
133 target_section_table m_core_file_mappings
{};
135 /* Unavailable mappings. These correspond to pathnames which either
136 weren't found or could not be opened. Knowing these addresses can
138 std::vector
<mem_range
> m_core_unavailable_mappings
;
140 /* Build m_core_file_mappings. Called from the constructor. */
141 void build_file_mappings ();
143 /* Helper method for xfer_partial. */
144 enum target_xfer_status
xfer_memory_via_mappings (gdb_byte
*readbuf
,
145 const gdb_byte
*writebuf
,
148 ULONGEST
*xfered_len
);
150 /* FIXME: kettenis/20031023: Eventually this field should
152 struct gdbarch
*m_core_gdbarch
= NULL
;
155 core_target::core_target ()
157 m_core_gdbarch
= gdbarch_from_bfd (core_bfd
);
160 || !gdbarch_iterate_over_regset_sections_p (m_core_gdbarch
))
161 error (_("\"%s\": Core file format not supported"),
162 bfd_get_filename (core_bfd
));
164 /* Find the data section */
165 if (build_section_table (core_bfd
,
166 &m_core_section_table
.sections
,
167 &m_core_section_table
.sections_end
))
168 error (_("\"%s\": Can't find sections: %s"),
169 bfd_get_filename (core_bfd
), bfd_errmsg (bfd_get_error ()));
171 build_file_mappings ();
174 core_target::~core_target ()
176 xfree (m_core_section_table
.sections
);
177 xfree (m_core_file_mappings
.sections
);
180 /* Construct the target_section_table for file-backed mappings if
183 For each unique path in the note, we'll open a BFD with a bfd
184 target of "binary". This is an unstructured bfd target upon which
185 we'll impose a structure from the mappings in the architecture-specific
186 mappings note. A BFD section is allocated and initialized for each
189 We take care to not share already open bfds with other parts of
190 GDB; in particular, we don't want to add new sections to existing
191 BFDs. We do, however, ensure that the BFDs that we allocate here
192 will go away (be deallocated) when the core target is detached. */
195 core_target::build_file_mappings ()
197 std::unordered_map
<std::string
, struct bfd
*> bfd_map
;
198 std::unordered_set
<std::string
> unavailable_paths
;
200 /* See linux_read_core_file_mappings() in linux-tdep.c for an example
201 read_core_file_mappings method. */
202 gdbarch_read_core_file_mappings (m_core_gdbarch
, core_bfd
,
204 /* After determining the number of mappings, read_core_file_mappings
205 will invoke this lambda which allocates target_section storage for
209 m_core_file_mappings
.sections
= XNEWVEC (struct target_section
, count
);
210 m_core_file_mappings
.sections_end
= m_core_file_mappings
.sections
;
213 /* read_core_file_mappings will invoke this lambda for each mapping
215 [&] (int num
, ULONGEST start
, ULONGEST end
, ULONGEST file_ofs
,
216 const char *filename
, const void *other
)
218 /* Architecture-specific read_core_mapping methods are expected to
219 weed out non-file-backed mappings. */
220 gdb_assert (filename
!= nullptr);
222 struct bfd
*bfd
= bfd_map
[filename
];
225 /* Use exec_file_find() to do sysroot expansion. It'll
226 also strip the potential sysroot "target:" prefix. If
227 there is no sysroot, an equivalent (possibly more
228 canonical) pathname will be provided. */
229 gdb::unique_xmalloc_ptr
<char> expanded_fname
230 = exec_file_find (filename
, NULL
);
231 if (expanded_fname
== nullptr)
233 m_core_unavailable_mappings
.emplace_back (start
, end
- start
);
234 /* Print just one warning per path. */
235 if (unavailable_paths
.insert (filename
).second
)
236 warning (_("Can't open file %s during file-backed mapping "
242 bfd
= bfd_map
[filename
] = bfd_openr (expanded_fname
.get (),
245 if (bfd
== nullptr || !bfd_check_format (bfd
, bfd_object
))
247 m_core_unavailable_mappings
.emplace_back (start
, end
- start
);
248 /* If we get here, there's a good chance that it's due to
249 an internal error. We issue a warning instead of an
250 internal error because of the possibility that the
251 file was removed in between checking for its
252 existence during the expansion in exec_file_find()
253 and the calls to bfd_openr() / bfd_check_format().
254 Output both the path from the core file note along
255 with its expansion to make debugging this problem
257 warning (_("Can't open file %s which was expanded to %s "
258 "during file-backed mapping note processing"),
259 filename
, expanded_fname
.get ());
264 /* Ensure that the bfd will be closed when core_bfd is closed.
265 This can be checked before/after a core file detach via
266 "maint info bfds". */
267 gdb_bfd_record_inclusion (core_bfd
, bfd
);
270 /* Make new BFD section. All sections have the same name,
271 which is permitted by bfd_make_section_anyway(). */
272 asection
*sec
= bfd_make_section_anyway (bfd
, "load");
274 error (_("Can't make section"));
275 sec
->filepos
= file_ofs
;
276 bfd_set_section_flags (sec
, SEC_READONLY
| SEC_HAS_CONTENTS
);
277 bfd_set_section_size (sec
, end
- start
);
278 bfd_set_section_vma (sec
, start
);
279 bfd_set_section_lma (sec
, start
);
280 bfd_set_section_alignment (sec
, 2);
282 /* Set target_section fields. */
283 struct target_section
*ts
= m_core_file_mappings
.sections_end
++;
287 ts
->the_bfd_section
= sec
;
290 normalize_mem_ranges (&m_core_unavailable_mappings
);
293 /* An arbitrary identifier for the core inferior. */
294 #define CORELOW_PID 1
296 /* Close the core target. */
299 core_target::close ()
303 switch_to_no_thread (); /* Avoid confusion from thread
305 exit_inferior_silent (current_inferior ());
307 /* Clear out solib state while the bfd is still open. See
308 comments in clear_solib in solib.c. */
311 current_program_space
->cbfd
.reset (nullptr);
314 /* Core targets are heap-allocated (see core_target_open), so here
315 we delete ourselves. */
319 /* Look for sections whose names start with `.reg/' so that we can
320 extract the list of threads in a core file. */
323 add_to_thread_list (asection
*asect
, asection
*reg_sect
)
327 bool fake_pid_p
= false;
328 struct inferior
*inf
;
330 if (!startswith (bfd_section_name (asect
), ".reg/"))
333 core_tid
= atoi (bfd_section_name (asect
) + 5);
335 pid
= bfd_core_file_pid (core_bfd
);
344 inf
= current_inferior ();
347 inferior_appeared (inf
, pid
);
348 inf
->fake_pid_p
= fake_pid_p
;
351 ptid_t
ptid (pid
, lwpid
);
353 thread_info
*thr
= add_thread (inf
->process_target (), ptid
);
355 /* Warning, Will Robinson, looking at BFD private data! */
358 && asect
->filepos
== reg_sect
->filepos
) /* Did we find .reg? */
359 switch_to_thread (thr
); /* Yes, make it current. */
362 /* Issue a message saying we have no core to debug, if FROM_TTY. */
365 maybe_say_no_core_file_now (int from_tty
)
368 printf_filtered (_("No core file now.\n"));
371 /* Backward compatibility with old way of specifying core files. */
374 core_file_command (const char *filename
, int from_tty
)
376 dont_repeat (); /* Either way, seems bogus. */
378 if (filename
== NULL
)
380 if (core_bfd
!= NULL
)
382 target_detach (current_inferior (), from_tty
);
383 gdb_assert (core_bfd
== NULL
);
386 maybe_say_no_core_file_now (from_tty
);
389 core_target_open (filename
, from_tty
);
392 /* Locate (and load) an executable file (and symbols) given the core file
396 locate_exec_from_corefile_build_id (bfd
*abfd
, int from_tty
)
398 const bfd_build_id
*build_id
= build_id_bfd_get (abfd
);
399 if (build_id
== nullptr)
402 gdb_bfd_ref_ptr execbfd
403 = build_id_to_exec_bfd (build_id
->size
, build_id
->data
);
405 if (execbfd
!= nullptr)
407 exec_file_attach (bfd_get_filename (execbfd
.get ()), from_tty
);
408 symbol_file_add_main (bfd_get_filename (execbfd
.get ()),
409 symfile_add_flag (from_tty
? SYMFILE_VERBOSE
: 0));
416 core_target_open (const char *arg
, int from_tty
)
423 target_preopen (from_tty
);
427 error (_("No core file specified. (Use `detach' "
428 "to stop debugging a core file.)"));
430 error (_("No core file specified."));
433 gdb::unique_xmalloc_ptr
<char> filename (tilde_expand (arg
));
434 if (!IS_ABSOLUTE_PATH (filename
.get ()))
435 filename
= gdb_abspath (filename
.get ());
437 flags
= O_BINARY
| O_LARGEFILE
;
442 scratch_chan
= gdb_open_cloexec (filename
.get (), flags
, 0);
443 if (scratch_chan
< 0)
444 perror_with_name (filename
.get ());
446 gdb_bfd_ref_ptr
temp_bfd (gdb_bfd_fopen (filename
.get (), gnutarget
,
447 write_files
? FOPEN_RUB
: FOPEN_RB
,
449 if (temp_bfd
== NULL
)
450 perror_with_name (filename
.get ());
452 if (!bfd_check_format (temp_bfd
.get (), bfd_core
))
454 /* Do it after the err msg */
455 /* FIXME: should be checking for errors from bfd_close (for one
456 thing, on error it does not free all the storage associated
458 error (_("\"%s\" is not a core dump: %s"),
459 filename
.get (), bfd_errmsg (bfd_get_error ()));
462 current_program_space
->cbfd
= std::move (temp_bfd
);
464 core_target
*target
= new core_target ();
466 /* Own the target until it is successfully pushed. */
467 target_ops_up
target_holder (target
);
471 /* If we have no exec file, try to set the architecture from the
472 core file. We don't do this unconditionally since an exec file
473 typically contains more information that helps us determine the
474 architecture than a core file. */
476 set_gdbarch_from_file (core_bfd
);
478 push_target (std::move (target_holder
));
480 switch_to_no_thread ();
482 /* Need to flush the register cache (and the frame cache) from a
483 previous debug session. If inferior_ptid ends up the same as the
484 last debug session --- e.g., b foo; run; gcore core1; step; gcore
485 core2; core core1; core core2 --- then there's potential for
486 get_current_regcache to return the cached regcache of the
487 previous session, and the frame cache being stale. */
488 registers_changed ();
490 /* Build up thread list from BFD sections, and possibly set the
491 current thread to the .reg/NN section matching the .reg
493 asection
*reg_sect
= bfd_get_section_by_name (core_bfd
, ".reg");
494 for (asection
*sect
: gdb_bfd_sections (core_bfd
))
495 add_to_thread_list (sect
, reg_sect
);
497 if (inferior_ptid
== null_ptid
)
499 /* Either we found no .reg/NN section, and hence we have a
500 non-threaded core (single-threaded, from gdb's perspective),
501 or for some reason add_to_thread_list couldn't determine
502 which was the "main" thread. The latter case shouldn't
503 usually happen, but we're dealing with input here, which can
504 always be broken in different ways. */
505 thread_info
*thread
= first_thread_of_inferior (current_inferior ());
509 inferior_appeared (current_inferior (), CORELOW_PID
);
510 thread
= add_thread_silent (target
, ptid_t (CORELOW_PID
));
513 switch_to_thread (thread
);
516 if (exec_bfd
== nullptr)
517 locate_exec_from_corefile_build_id (core_bfd
, from_tty
);
519 post_create_inferior (from_tty
);
521 /* Now go through the target stack looking for threads since there
522 may be a thread_stratum target loaded on top of target core by
523 now. The layer above should claim threads found in the BFD
527 target_update_thread_list ();
530 catch (const gdb_exception_error
&except
)
532 exception_print (gdb_stderr
, except
);
535 p
= bfd_core_file_failing_command (core_bfd
);
537 printf_filtered (_("Core was generated by `%s'.\n"), p
);
539 /* Clearing any previous state of convenience variables. */
540 clear_exit_convenience_vars ();
542 siggy
= bfd_core_file_failing_signal (core_bfd
);
545 gdbarch
*core_gdbarch
= target
->core_gdbarch ();
547 /* If we don't have a CORE_GDBARCH to work with, assume a native
548 core (map gdb_signal from host signals). If we do have
549 CORE_GDBARCH to work with, but no gdb_signal_from_target
550 implementation for that gdbarch, as a fallback measure,
551 assume the host signal mapping. It'll be correct for native
552 cores, but most likely incorrect for cross-cores. */
553 enum gdb_signal sig
= (core_gdbarch
!= NULL
554 && gdbarch_gdb_signal_from_target_p (core_gdbarch
)
555 ? gdbarch_gdb_signal_from_target (core_gdbarch
,
557 : gdb_signal_from_host (siggy
));
559 printf_filtered (_("Program terminated with signal %s, %s"),
560 gdb_signal_to_name (sig
), gdb_signal_to_string (sig
));
561 if (gdbarch_report_signal_info_p (core_gdbarch
))
562 gdbarch_report_signal_info (core_gdbarch
, current_uiout
, sig
);
563 printf_filtered (_(".\n"));
565 /* Set the value of the internal variable $_exitsignal,
566 which holds the signal uncaught by the inferior. */
567 set_internalvar_integer (lookup_internalvar ("_exitsignal"),
571 /* Fetch all registers from core file. */
572 target_fetch_registers (get_current_regcache (), -1);
574 /* Now, set up the frame cache, and print the top of stack. */
575 reinit_frame_cache ();
576 print_stack_frame (get_selected_frame (NULL
), 1, SRC_AND_LOC
, 1);
578 /* Current thread should be NUM 1 but the user does not know that.
579 If a program is single threaded gdb in general does not mention
580 anything about threads. That is why the test is >= 2. */
581 if (thread_count (target
) >= 2)
585 thread_command (NULL
, from_tty
);
587 catch (const gdb_exception_error
&except
)
589 exception_print (gdb_stderr
, except
);
595 core_target::detach (inferior
*inf
, int from_tty
)
597 /* Note that 'this' is dangling after this call. unpush_target
598 closes the target, and our close implementation deletes
600 unpush_target (this);
602 /* Clear the register cache and the frame cache. */
603 registers_changed ();
604 reinit_frame_cache ();
605 maybe_say_no_core_file_now (from_tty
);
608 /* Try to retrieve registers from a section in core_bfd, and supply
611 If ptid's lwp member is zero, do the single-threaded
612 thing: look for a section named NAME. If ptid's lwp
613 member is non-zero, do the multi-threaded thing: look for a section
614 named "NAME/LWP", where LWP is the shortest ASCII decimal
615 representation of ptid's lwp member.
617 HUMAN_NAME is a human-readable name for the kind of registers the
618 NAME section contains, for use in error messages.
620 If REQUIRED is true, print an error if the core file doesn't have a
621 section by the appropriate name. Otherwise, just do nothing. */
624 core_target::get_core_register_section (struct regcache
*regcache
,
625 const struct regset
*regset
,
627 int section_min_size
,
628 const char *human_name
,
631 gdb_assert (regset
!= nullptr);
633 struct bfd_section
*section
;
635 bool variable_size_section
= (regset
->flags
& REGSET_VARIABLE_SIZE
);
637 thread_section_name
section_name (name
, regcache
->ptid ());
639 section
= bfd_get_section_by_name (core_bfd
, section_name
.c_str ());
643 warning (_("Couldn't find %s registers in core file."),
648 size
= bfd_section_size (section
);
649 if (size
< section_min_size
)
651 warning (_("Section `%s' in core file too small."),
652 section_name
.c_str ());
655 if (size
!= section_min_size
&& !variable_size_section
)
657 warning (_("Unexpected size of section `%s' in core file."),
658 section_name
.c_str ());
661 gdb::byte_vector
contents (size
);
662 if (!bfd_get_section_contents (core_bfd
, section
, contents
.data (),
665 warning (_("Couldn't read %s registers from `%s' section in core file."),
666 human_name
, section_name
.c_str ());
670 regset
->supply_regset (regset
, regcache
, -1, contents
.data (), size
);
673 /* Data passed to gdbarch_iterate_over_regset_sections's callback. */
674 struct get_core_registers_cb_data
677 struct regcache
*regcache
;
680 /* Callback for get_core_registers that handles a single core file
681 register note section. */
684 get_core_registers_cb (const char *sect_name
, int supply_size
, int collect_size
,
685 const struct regset
*regset
,
686 const char *human_name
, void *cb_data
)
688 gdb_assert (regset
!= nullptr);
690 auto *data
= (get_core_registers_cb_data
*) cb_data
;
691 bool required
= false;
692 bool variable_size_section
= (regset
->flags
& REGSET_VARIABLE_SIZE
);
694 if (!variable_size_section
)
695 gdb_assert (supply_size
== collect_size
);
697 if (strcmp (sect_name
, ".reg") == 0)
700 if (human_name
== NULL
)
701 human_name
= "general-purpose";
703 else if (strcmp (sect_name
, ".reg2") == 0)
705 if (human_name
== NULL
)
706 human_name
= "floating-point";
709 data
->target
->get_core_register_section (data
->regcache
, regset
, sect_name
,
710 supply_size
, human_name
, required
);
713 /* Get the registers out of a core file. This is the machine-
714 independent part. Fetch_core_registers is the machine-dependent
715 part, typically implemented in the xm-file for each
718 /* We just get all the registers, so we don't use regno. */
721 core_target::fetch_registers (struct regcache
*regcache
, int regno
)
723 if (!(m_core_gdbarch
!= nullptr
724 && gdbarch_iterate_over_regset_sections_p (m_core_gdbarch
)))
726 fprintf_filtered (gdb_stderr
,
727 "Can't fetch registers from this type of core file\n");
731 struct gdbarch
*gdbarch
= regcache
->arch ();
732 get_core_registers_cb_data data
= { this, regcache
};
733 gdbarch_iterate_over_regset_sections (gdbarch
,
734 get_core_registers_cb
,
735 (void *) &data
, NULL
);
737 /* Mark all registers not found in the core as unavailable. */
738 for (int i
= 0; i
< gdbarch_num_regs (regcache
->arch ()); i
++)
739 if (regcache
->get_register_status (i
) == REG_UNKNOWN
)
740 regcache
->raw_supply (i
, NULL
);
744 core_target::files_info ()
746 print_section_info (&m_core_section_table
, core_bfd
);
749 /* Helper method for core_target::xfer_partial. */
751 enum target_xfer_status
752 core_target::xfer_memory_via_mappings (gdb_byte
*readbuf
,
753 const gdb_byte
*writebuf
,
754 ULONGEST offset
, ULONGEST len
,
755 ULONGEST
*xfered_len
)
757 enum target_xfer_status xfer_status
;
759 xfer_status
= (section_table_xfer_memory_partial
761 offset
, len
, xfered_len
,
762 m_core_file_mappings
.sections
,
763 m_core_file_mappings
.sections_end
));
765 if (xfer_status
== TARGET_XFER_OK
|| m_core_unavailable_mappings
.empty ())
768 /* There are instances - e.g. when debugging within a docker
769 container using the AUFS storage driver - where the pathnames
770 obtained from the note section are incorrect. Despite the path
771 being wrong, just knowing the start and end addresses of the
772 mappings is still useful; we can attempt an access of the file
773 stratum constrained to the address ranges corresponding to the
774 unavailable mappings. */
776 ULONGEST memaddr
= offset
;
777 ULONGEST memend
= offset
+ len
;
779 for (const auto &mr
: m_core_unavailable_mappings
)
781 if (address_in_mem_range (memaddr
, &mr
))
783 if (!address_in_mem_range (memend
, &mr
))
784 len
= mr
.start
+ mr
.length
- memaddr
;
786 xfer_status
= this->beneath ()->xfer_partial (TARGET_OBJECT_MEMORY
,
800 enum target_xfer_status
801 core_target::xfer_partial (enum target_object object
, const char *annex
,
802 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
803 ULONGEST offset
, ULONGEST len
, ULONGEST
*xfered_len
)
807 case TARGET_OBJECT_MEMORY
:
809 enum target_xfer_status xfer_status
;
811 /* Try accessing memory contents from core file data,
812 restricting consideration to those sections for which
813 the BFD section flag SEC_HAS_CONTENTS is set. */
814 auto has_contents_cb
= [] (const struct target_section
*s
)
816 return ((s
->the_bfd_section
->flags
& SEC_HAS_CONTENTS
) != 0);
818 xfer_status
= section_table_xfer_memory_partial
820 offset
, len
, xfered_len
,
821 m_core_section_table
.sections
,
822 m_core_section_table
.sections_end
,
824 if (xfer_status
== TARGET_XFER_OK
)
825 return TARGET_XFER_OK
;
827 /* Check file backed mappings. If they're available, use
828 core file provided mappings (e.g. from .note.linuxcore.file
829 or the like) as this should provide a more accurate
830 result. If not, check the stratum beneath us, which should
831 be the file stratum. */
832 if (m_core_file_mappings
.sections
!= nullptr)
833 xfer_status
= xfer_memory_via_mappings (readbuf
, writebuf
, offset
,
836 xfer_status
= this->beneath ()->xfer_partial (object
, annex
, readbuf
,
837 writebuf
, offset
, len
,
839 if (xfer_status
== TARGET_XFER_OK
)
840 return TARGET_XFER_OK
;
842 /* Finally, attempt to access data in core file sections with
843 no contents. These will typically read as all zero. */
844 auto no_contents_cb
= [&] (const struct target_section
*s
)
846 return !has_contents_cb (s
);
848 xfer_status
= section_table_xfer_memory_partial
850 offset
, len
, xfered_len
,
851 m_core_section_table
.sections
,
852 m_core_section_table
.sections_end
,
857 case TARGET_OBJECT_AUXV
:
860 /* When the aux vector is stored in core file, BFD
861 represents this with a fake section called ".auxv". */
863 struct bfd_section
*section
;
866 section
= bfd_get_section_by_name (core_bfd
, ".auxv");
868 return TARGET_XFER_E_IO
;
870 size
= bfd_section_size (section
);
872 return TARGET_XFER_EOF
;
878 return TARGET_XFER_EOF
;
879 if (!bfd_get_section_contents (core_bfd
, section
, readbuf
,
880 (file_ptr
) offset
, size
))
882 warning (_("Couldn't read NT_AUXV note in core file."));
883 return TARGET_XFER_E_IO
;
886 *xfered_len
= (ULONGEST
) size
;
887 return TARGET_XFER_OK
;
889 return TARGET_XFER_E_IO
;
891 case TARGET_OBJECT_WCOOKIE
:
894 /* When the StackGhost cookie is stored in core file, BFD
895 represents this with a fake section called
898 struct bfd_section
*section
;
901 section
= bfd_get_section_by_name (core_bfd
, ".wcookie");
903 return TARGET_XFER_E_IO
;
905 size
= bfd_section_size (section
);
907 return TARGET_XFER_EOF
;
913 return TARGET_XFER_EOF
;
914 if (!bfd_get_section_contents (core_bfd
, section
, readbuf
,
915 (file_ptr
) offset
, size
))
917 warning (_("Couldn't read StackGhost cookie in core file."));
918 return TARGET_XFER_E_IO
;
921 *xfered_len
= (ULONGEST
) size
;
922 return TARGET_XFER_OK
;
925 return TARGET_XFER_E_IO
;
927 case TARGET_OBJECT_LIBRARIES
:
928 if (m_core_gdbarch
!= nullptr
929 && gdbarch_core_xfer_shared_libraries_p (m_core_gdbarch
))
932 return TARGET_XFER_E_IO
;
935 *xfered_len
= gdbarch_core_xfer_shared_libraries (m_core_gdbarch
,
939 if (*xfered_len
== 0)
940 return TARGET_XFER_EOF
;
942 return TARGET_XFER_OK
;
947 case TARGET_OBJECT_LIBRARIES_AIX
:
948 if (m_core_gdbarch
!= nullptr
949 && gdbarch_core_xfer_shared_libraries_aix_p (m_core_gdbarch
))
952 return TARGET_XFER_E_IO
;
956 = gdbarch_core_xfer_shared_libraries_aix (m_core_gdbarch
,
960 if (*xfered_len
== 0)
961 return TARGET_XFER_EOF
;
963 return TARGET_XFER_OK
;
968 case TARGET_OBJECT_SIGNAL_INFO
:
971 if (m_core_gdbarch
!= nullptr
972 && gdbarch_core_xfer_siginfo_p (m_core_gdbarch
))
974 LONGEST l
= gdbarch_core_xfer_siginfo (m_core_gdbarch
, readbuf
,
981 return TARGET_XFER_EOF
;
983 return TARGET_XFER_OK
;
987 return TARGET_XFER_E_IO
;
990 return this->beneath ()->xfer_partial (object
, annex
, readbuf
,
991 writebuf
, offset
, len
,
998 /* Okay, let's be honest: threads gleaned from a core file aren't
999 exactly lively, are they? On the other hand, if we don't claim
1000 that each & every one is alive, then we don't get any of them
1001 to appear in an "info thread" command, which is quite a useful
1005 core_target::thread_alive (ptid_t ptid
)
1010 /* Ask the current architecture what it knows about this core file.
1011 That will be used, in turn, to pick a better architecture. This
1012 wrapper could be avoided if targets got a chance to specialize
1015 const struct target_desc
*
1016 core_target::read_description ()
1018 if (m_core_gdbarch
&& gdbarch_core_read_description_p (m_core_gdbarch
))
1020 const struct target_desc
*result
;
1022 result
= gdbarch_core_read_description (m_core_gdbarch
, this, core_bfd
);
1027 return this->beneath ()->read_description ();
1031 core_target::pid_to_str (ptid_t ptid
)
1033 struct inferior
*inf
;
1036 /* The preferred way is to have a gdbarch/OS specific
1038 if (m_core_gdbarch
!= nullptr
1039 && gdbarch_core_pid_to_str_p (m_core_gdbarch
))
1040 return gdbarch_core_pid_to_str (m_core_gdbarch
, ptid
);
1042 /* Otherwise, if we don't have one, we'll just fallback to
1043 "process", with normal_pid_to_str. */
1045 /* Try the LWPID field first. */
1048 return normal_pid_to_str (ptid_t (pid
));
1050 /* Otherwise, this isn't a "threaded" core -- use the PID field, but
1051 only if it isn't a fake PID. */
1052 inf
= find_inferior_ptid (this, ptid
);
1053 if (inf
!= NULL
&& !inf
->fake_pid_p
)
1054 return normal_pid_to_str (ptid
);
1056 /* No luck. We simply don't have a valid PID to print. */
1057 return "<main task>";
1061 core_target::thread_name (struct thread_info
*thr
)
1063 if (m_core_gdbarch
!= nullptr
1064 && gdbarch_core_thread_name_p (m_core_gdbarch
))
1065 return gdbarch_core_thread_name (m_core_gdbarch
, thr
);
1070 core_target::has_memory ()
1072 return (core_bfd
!= NULL
);
1076 core_target::has_stack ()
1078 return (core_bfd
!= NULL
);
1082 core_target::has_registers ()
1084 return (core_bfd
!= NULL
);
1087 /* Implement the to_info_proc method. */
1090 core_target::info_proc (const char *args
, enum info_proc_what request
)
1092 struct gdbarch
*gdbarch
= get_current_arch ();
1094 /* Since this is the core file target, call the 'core_info_proc'
1095 method on gdbarch, not 'info_proc'. */
1096 if (gdbarch_core_info_proc_p (gdbarch
))
1097 gdbarch_core_info_proc (gdbarch
, args
, request
);
1102 /* Get a pointer to the current core target. If not connected to a
1103 core target, return NULL. */
1105 static core_target
*
1106 get_current_core_target ()
1108 target_ops
*proc_target
= current_inferior ()->process_target ();
1109 return dynamic_cast<core_target
*> (proc_target
);
1112 /* Display file backed mappings from core file. */
1115 core_target::info_proc_mappings (struct gdbarch
*gdbarch
)
1117 if (m_core_file_mappings
.sections
!= m_core_file_mappings
.sections_end
)
1119 printf_filtered (_("Mapped address spaces:\n\n"));
1120 if (gdbarch_addr_bit (gdbarch
) == 32)
1122 printf_filtered ("\t%10s %10s %10s %10s %s\n",
1125 " Size", " Offset", "objfile");
1129 printf_filtered (" %18s %18s %10s %10s %s\n",
1132 " Size", " Offset", "objfile");
1136 for (const struct target_section
*tsp
= m_core_file_mappings
.sections
;
1137 tsp
< m_core_file_mappings
.sections_end
;
1140 ULONGEST start
= tsp
->addr
;
1141 ULONGEST end
= tsp
->endaddr
;
1142 ULONGEST file_ofs
= tsp
->the_bfd_section
->filepos
;
1143 const char *filename
= bfd_get_filename (tsp
->the_bfd_section
->owner
);
1145 if (gdbarch_addr_bit (gdbarch
) == 32)
1146 printf_filtered ("\t%10s %10s %10s %10s %s\n",
1147 paddress (gdbarch
, start
),
1148 paddress (gdbarch
, end
),
1149 hex_string (end
- start
),
1150 hex_string (file_ofs
),
1153 printf_filtered (" %18s %18s %10s %10s %s\n",
1154 paddress (gdbarch
, start
),
1155 paddress (gdbarch
, end
),
1156 hex_string (end
- start
),
1157 hex_string (file_ofs
),
1162 /* Implement "maintenance print core-file-backed-mappings" command.
1164 If mappings are loaded, the results should be similar to the
1165 mappings shown by "info proc mappings". This command is mainly a
1166 debugging tool for GDB developers to make sure that the expected
1167 mappings are present after loading a core file. For Linux, the
1168 output provided by this command will be very similar (if not
1169 identical) to that provided by "info proc mappings". This is not
1170 necessarily the case for other OSes which might provide
1171 more/different information in the "info proc mappings" output. */
1174 maintenance_print_core_file_backed_mappings (const char *args
, int from_tty
)
1176 core_target
*targ
= get_current_core_target ();
1177 if (targ
!= nullptr)
1178 targ
->info_proc_mappings (targ
->core_gdbarch ());
1181 void _initialize_corelow ();
1183 _initialize_corelow ()
1185 add_target (core_target_info
, core_target_open
, filename_completer
);
1186 add_cmd ("core-file-backed-mappings", class_maintenance
,
1187 maintenance_print_core_file_backed_mappings
,
1188 _("Print core file's file-backed mappings."),
1189 &maintenanceprintlist
);