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 static void add_to_thread_list (bfd
*, asection
*, void *);
295 /* An arbitrary identifier for the core inferior. */
296 #define CORELOW_PID 1
298 /* Close the core target. */
301 core_target::close ()
305 switch_to_no_thread (); /* Avoid confusion from thread
307 exit_inferior_silent (current_inferior ());
309 /* Clear out solib state while the bfd is still open. See
310 comments in clear_solib in solib.c. */
313 current_program_space
->cbfd
.reset (nullptr);
316 /* Core targets are heap-allocated (see core_target_open), so here
317 we delete ourselves. */
321 /* Look for sections whose names start with `.reg/' so that we can
322 extract the list of threads in a core file. */
325 add_to_thread_list (bfd
*abfd
, asection
*asect
, void *reg_sect_arg
)
329 asection
*reg_sect
= (asection
*) reg_sect_arg
;
330 bool fake_pid_p
= false;
331 struct inferior
*inf
;
333 if (!startswith (bfd_section_name (asect
), ".reg/"))
336 core_tid
= atoi (bfd_section_name (asect
) + 5);
338 pid
= bfd_core_file_pid (core_bfd
);
347 inf
= current_inferior ();
350 inferior_appeared (inf
, pid
);
351 inf
->fake_pid_p
= fake_pid_p
;
354 ptid_t
ptid (pid
, lwpid
);
356 thread_info
*thr
= add_thread (inf
->process_target (), ptid
);
358 /* Warning, Will Robinson, looking at BFD private data! */
361 && asect
->filepos
== reg_sect
->filepos
) /* Did we find .reg? */
362 switch_to_thread (thr
); /* Yes, make it current. */
365 /* Issue a message saying we have no core to debug, if FROM_TTY. */
368 maybe_say_no_core_file_now (int from_tty
)
371 printf_filtered (_("No core file now.\n"));
374 /* Backward compatibility with old way of specifying core files. */
377 core_file_command (const char *filename
, int from_tty
)
379 dont_repeat (); /* Either way, seems bogus. */
381 if (filename
== NULL
)
383 if (core_bfd
!= NULL
)
385 target_detach (current_inferior (), from_tty
);
386 gdb_assert (core_bfd
== NULL
);
389 maybe_say_no_core_file_now (from_tty
);
392 core_target_open (filename
, from_tty
);
395 /* Locate (and load) an executable file (and symbols) given the core file
399 locate_exec_from_corefile_build_id (bfd
*abfd
, int from_tty
)
401 const bfd_build_id
*build_id
= build_id_bfd_get (abfd
);
402 if (build_id
== nullptr)
405 gdb_bfd_ref_ptr execbfd
406 = build_id_to_exec_bfd (build_id
->size
, build_id
->data
);
408 if (execbfd
!= nullptr)
410 exec_file_attach (bfd_get_filename (execbfd
.get ()), from_tty
);
411 symbol_file_add_main (bfd_get_filename (execbfd
.get ()),
412 symfile_add_flag (from_tty
? SYMFILE_VERBOSE
: 0));
419 core_target_open (const char *arg
, int from_tty
)
426 target_preopen (from_tty
);
430 error (_("No core file specified. (Use `detach' "
431 "to stop debugging a core file.)"));
433 error (_("No core file specified."));
436 gdb::unique_xmalloc_ptr
<char> filename (tilde_expand (arg
));
437 if (!IS_ABSOLUTE_PATH (filename
.get ()))
438 filename
= gdb_abspath (filename
.get ());
440 flags
= O_BINARY
| O_LARGEFILE
;
445 scratch_chan
= gdb_open_cloexec (filename
.get (), flags
, 0);
446 if (scratch_chan
< 0)
447 perror_with_name (filename
.get ());
449 gdb_bfd_ref_ptr
temp_bfd (gdb_bfd_fopen (filename
.get (), gnutarget
,
450 write_files
? FOPEN_RUB
: FOPEN_RB
,
452 if (temp_bfd
== NULL
)
453 perror_with_name (filename
.get ());
455 if (!bfd_check_format (temp_bfd
.get (), bfd_core
))
457 /* Do it after the err msg */
458 /* FIXME: should be checking for errors from bfd_close (for one
459 thing, on error it does not free all the storage associated
461 error (_("\"%s\" is not a core dump: %s"),
462 filename
.get (), bfd_errmsg (bfd_get_error ()));
465 current_program_space
->cbfd
= std::move (temp_bfd
);
467 core_target
*target
= new core_target ();
469 /* Own the target until it is successfully pushed. */
470 target_ops_up
target_holder (target
);
474 /* If we have no exec file, try to set the architecture from the
475 core file. We don't do this unconditionally since an exec file
476 typically contains more information that helps us determine the
477 architecture than a core file. */
479 set_gdbarch_from_file (core_bfd
);
481 push_target (std::move (target_holder
));
483 switch_to_no_thread ();
485 /* Need to flush the register cache (and the frame cache) from a
486 previous debug session. If inferior_ptid ends up the same as the
487 last debug session --- e.g., b foo; run; gcore core1; step; gcore
488 core2; core core1; core core2 --- then there's potential for
489 get_current_regcache to return the cached regcache of the
490 previous session, and the frame cache being stale. */
491 registers_changed ();
493 /* Build up thread list from BFD sections, and possibly set the
494 current thread to the .reg/NN section matching the .reg
496 bfd_map_over_sections (core_bfd
, add_to_thread_list
,
497 bfd_get_section_by_name (core_bfd
, ".reg"));
499 if (inferior_ptid
== null_ptid
)
501 /* Either we found no .reg/NN section, and hence we have a
502 non-threaded core (single-threaded, from gdb's perspective),
503 or for some reason add_to_thread_list couldn't determine
504 which was the "main" thread. The latter case shouldn't
505 usually happen, but we're dealing with input here, which can
506 always be broken in different ways. */
507 thread_info
*thread
= first_thread_of_inferior (current_inferior ());
511 inferior_appeared (current_inferior (), CORELOW_PID
);
512 thread
= add_thread_silent (target
, ptid_t (CORELOW_PID
));
515 switch_to_thread (thread
);
518 if (exec_bfd
== nullptr)
519 locate_exec_from_corefile_build_id (core_bfd
, from_tty
);
521 post_create_inferior (target
, from_tty
);
523 /* Now go through the target stack looking for threads since there
524 may be a thread_stratum target loaded on top of target core by
525 now. The layer above should claim threads found in the BFD
529 target_update_thread_list ();
532 catch (const gdb_exception_error
&except
)
534 exception_print (gdb_stderr
, except
);
537 p
= bfd_core_file_failing_command (core_bfd
);
539 printf_filtered (_("Core was generated by `%s'.\n"), p
);
541 /* Clearing any previous state of convenience variables. */
542 clear_exit_convenience_vars ();
544 siggy
= bfd_core_file_failing_signal (core_bfd
);
547 gdbarch
*core_gdbarch
= target
->core_gdbarch ();
549 /* If we don't have a CORE_GDBARCH to work with, assume a native
550 core (map gdb_signal from host signals). If we do have
551 CORE_GDBARCH to work with, but no gdb_signal_from_target
552 implementation for that gdbarch, as a fallback measure,
553 assume the host signal mapping. It'll be correct for native
554 cores, but most likely incorrect for cross-cores. */
555 enum gdb_signal sig
= (core_gdbarch
!= NULL
556 && gdbarch_gdb_signal_from_target_p (core_gdbarch
)
557 ? gdbarch_gdb_signal_from_target (core_gdbarch
,
559 : gdb_signal_from_host (siggy
));
561 printf_filtered (_("Program terminated with signal %s, %s"),
562 gdb_signal_to_name (sig
), gdb_signal_to_string (sig
));
563 if (gdbarch_report_signal_info_p (core_gdbarch
))
564 gdbarch_report_signal_info (core_gdbarch
, current_uiout
, sig
);
565 printf_filtered (_(".\n"));
567 /* Set the value of the internal variable $_exitsignal,
568 which holds the signal uncaught by the inferior. */
569 set_internalvar_integer (lookup_internalvar ("_exitsignal"),
573 /* Fetch all registers from core file. */
574 target_fetch_registers (get_current_regcache (), -1);
576 /* Now, set up the frame cache, and print the top of stack. */
577 reinit_frame_cache ();
578 print_stack_frame (get_selected_frame (NULL
), 1, SRC_AND_LOC
, 1);
580 /* Current thread should be NUM 1 but the user does not know that.
581 If a program is single threaded gdb in general does not mention
582 anything about threads. That is why the test is >= 2. */
583 if (thread_count (target
) >= 2)
587 thread_command (NULL
, from_tty
);
589 catch (const gdb_exception_error
&except
)
591 exception_print (gdb_stderr
, except
);
597 core_target::detach (inferior
*inf
, int from_tty
)
599 /* Note that 'this' is dangling after this call. unpush_target
600 closes the target, and our close implementation deletes
602 unpush_target (this);
604 /* Clear the register cache and the frame cache. */
605 registers_changed ();
606 reinit_frame_cache ();
607 maybe_say_no_core_file_now (from_tty
);
610 /* Try to retrieve registers from a section in core_bfd, and supply
613 If ptid's lwp member is zero, do the single-threaded
614 thing: look for a section named NAME. If ptid's lwp
615 member is non-zero, do the multi-threaded thing: look for a section
616 named "NAME/LWP", where LWP is the shortest ASCII decimal
617 representation of ptid's lwp member.
619 HUMAN_NAME is a human-readable name for the kind of registers the
620 NAME section contains, for use in error messages.
622 If REQUIRED is true, print an error if the core file doesn't have a
623 section by the appropriate name. Otherwise, just do nothing. */
626 core_target::get_core_register_section (struct regcache
*regcache
,
627 const struct regset
*regset
,
629 int section_min_size
,
630 const char *human_name
,
633 gdb_assert (regset
!= nullptr);
635 struct bfd_section
*section
;
637 bool variable_size_section
= (regset
->flags
& REGSET_VARIABLE_SIZE
);
639 thread_section_name
section_name (name
, regcache
->ptid ());
641 section
= bfd_get_section_by_name (core_bfd
, section_name
.c_str ());
645 warning (_("Couldn't find %s registers in core file."),
650 size
= bfd_section_size (section
);
651 if (size
< section_min_size
)
653 warning (_("Section `%s' in core file too small."),
654 section_name
.c_str ());
657 if (size
!= section_min_size
&& !variable_size_section
)
659 warning (_("Unexpected size of section `%s' in core file."),
660 section_name
.c_str ());
663 gdb::byte_vector
contents (size
);
664 if (!bfd_get_section_contents (core_bfd
, section
, contents
.data (),
667 warning (_("Couldn't read %s registers from `%s' section in core file."),
668 human_name
, section_name
.c_str ());
672 regset
->supply_regset (regset
, regcache
, -1, contents
.data (), size
);
675 /* Data passed to gdbarch_iterate_over_regset_sections's callback. */
676 struct get_core_registers_cb_data
679 struct regcache
*regcache
;
682 /* Callback for get_core_registers that handles a single core file
683 register note section. */
686 get_core_registers_cb (const char *sect_name
, int supply_size
, int collect_size
,
687 const struct regset
*regset
,
688 const char *human_name
, void *cb_data
)
690 gdb_assert (regset
!= nullptr);
692 auto *data
= (get_core_registers_cb_data
*) cb_data
;
693 bool required
= false;
694 bool variable_size_section
= (regset
->flags
& REGSET_VARIABLE_SIZE
);
696 if (!variable_size_section
)
697 gdb_assert (supply_size
== collect_size
);
699 if (strcmp (sect_name
, ".reg") == 0)
702 if (human_name
== NULL
)
703 human_name
= "general-purpose";
705 else if (strcmp (sect_name
, ".reg2") == 0)
707 if (human_name
== NULL
)
708 human_name
= "floating-point";
711 data
->target
->get_core_register_section (data
->regcache
, regset
, sect_name
,
712 supply_size
, human_name
, required
);
715 /* Get the registers out of a core file. This is the machine-
716 independent part. Fetch_core_registers is the machine-dependent
717 part, typically implemented in the xm-file for each
720 /* We just get all the registers, so we don't use regno. */
723 core_target::fetch_registers (struct regcache
*regcache
, int regno
)
725 if (!(m_core_gdbarch
!= nullptr
726 && gdbarch_iterate_over_regset_sections_p (m_core_gdbarch
)))
728 fprintf_filtered (gdb_stderr
,
729 "Can't fetch registers from this type of core file\n");
733 struct gdbarch
*gdbarch
= regcache
->arch ();
734 get_core_registers_cb_data data
= { this, regcache
};
735 gdbarch_iterate_over_regset_sections (gdbarch
,
736 get_core_registers_cb
,
737 (void *) &data
, NULL
);
739 /* Mark all registers not found in the core as unavailable. */
740 for (int i
= 0; i
< gdbarch_num_regs (regcache
->arch ()); i
++)
741 if (regcache
->get_register_status (i
) == REG_UNKNOWN
)
742 regcache
->raw_supply (i
, NULL
);
746 core_target::files_info ()
748 print_section_info (&m_core_section_table
, core_bfd
);
751 /* Helper method for core_target::xfer_partial. */
753 enum target_xfer_status
754 core_target::xfer_memory_via_mappings (gdb_byte
*readbuf
,
755 const gdb_byte
*writebuf
,
756 ULONGEST offset
, ULONGEST len
,
757 ULONGEST
*xfered_len
)
759 enum target_xfer_status xfer_status
;
761 xfer_status
= (section_table_xfer_memory_partial
763 offset
, len
, xfered_len
,
764 m_core_file_mappings
.sections
,
765 m_core_file_mappings
.sections_end
));
767 if (xfer_status
== TARGET_XFER_OK
|| m_core_unavailable_mappings
.empty ())
770 /* There are instances - e.g. when debugging within a docker
771 container using the AUFS storage driver - where the pathnames
772 obtained from the note section are incorrect. Despite the path
773 being wrong, just knowing the start and end addresses of the
774 mappings is still useful; we can attempt an access of the file
775 stratum constrained to the address ranges corresponding to the
776 unavailable mappings. */
778 ULONGEST memaddr
= offset
;
779 ULONGEST memend
= offset
+ len
;
781 for (const auto &mr
: m_core_unavailable_mappings
)
783 if (address_in_mem_range (memaddr
, &mr
))
785 if (!address_in_mem_range (memend
, &mr
))
786 len
= mr
.start
+ mr
.length
- memaddr
;
788 xfer_status
= this->beneath ()->xfer_partial (TARGET_OBJECT_MEMORY
,
802 enum target_xfer_status
803 core_target::xfer_partial (enum target_object object
, const char *annex
,
804 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
805 ULONGEST offset
, ULONGEST len
, ULONGEST
*xfered_len
)
809 case TARGET_OBJECT_MEMORY
:
811 enum target_xfer_status xfer_status
;
813 /* Try accessing memory contents from core file data,
814 restricting consideration to those sections for which
815 the BFD section flag SEC_HAS_CONTENTS is set. */
816 auto has_contents_cb
= [] (const struct target_section
*s
)
818 return ((s
->the_bfd_section
->flags
& SEC_HAS_CONTENTS
) != 0);
820 xfer_status
= section_table_xfer_memory_partial
822 offset
, len
, xfered_len
,
823 m_core_section_table
.sections
,
824 m_core_section_table
.sections_end
,
826 if (xfer_status
== TARGET_XFER_OK
)
827 return TARGET_XFER_OK
;
829 /* Check file backed mappings. If they're available, use
830 core file provided mappings (e.g. from .note.linuxcore.file
831 or the like) as this should provide a more accurate
832 result. If not, check the stratum beneath us, which should
833 be the file stratum. */
834 if (m_core_file_mappings
.sections
!= nullptr)
835 xfer_status
= xfer_memory_via_mappings (readbuf
, writebuf
, offset
,
838 xfer_status
= this->beneath ()->xfer_partial (object
, annex
, readbuf
,
839 writebuf
, offset
, len
,
841 if (xfer_status
== TARGET_XFER_OK
)
842 return TARGET_XFER_OK
;
844 /* Finally, attempt to access data in core file sections with
845 no contents. These will typically read as all zero. */
846 auto no_contents_cb
= [&] (const struct target_section
*s
)
848 return !has_contents_cb (s
);
850 xfer_status
= section_table_xfer_memory_partial
852 offset
, len
, xfered_len
,
853 m_core_section_table
.sections
,
854 m_core_section_table
.sections_end
,
859 case TARGET_OBJECT_AUXV
:
862 /* When the aux vector is stored in core file, BFD
863 represents this with a fake section called ".auxv". */
865 struct bfd_section
*section
;
868 section
= bfd_get_section_by_name (core_bfd
, ".auxv");
870 return TARGET_XFER_E_IO
;
872 size
= bfd_section_size (section
);
874 return TARGET_XFER_EOF
;
880 return TARGET_XFER_EOF
;
881 if (!bfd_get_section_contents (core_bfd
, section
, readbuf
,
882 (file_ptr
) offset
, size
))
884 warning (_("Couldn't read NT_AUXV note in core file."));
885 return TARGET_XFER_E_IO
;
888 *xfered_len
= (ULONGEST
) size
;
889 return TARGET_XFER_OK
;
891 return TARGET_XFER_E_IO
;
893 case TARGET_OBJECT_WCOOKIE
:
896 /* When the StackGhost cookie is stored in core file, BFD
897 represents this with a fake section called
900 struct bfd_section
*section
;
903 section
= bfd_get_section_by_name (core_bfd
, ".wcookie");
905 return TARGET_XFER_E_IO
;
907 size
= bfd_section_size (section
);
909 return TARGET_XFER_EOF
;
915 return TARGET_XFER_EOF
;
916 if (!bfd_get_section_contents (core_bfd
, section
, readbuf
,
917 (file_ptr
) offset
, size
))
919 warning (_("Couldn't read StackGhost cookie in core file."));
920 return TARGET_XFER_E_IO
;
923 *xfered_len
= (ULONGEST
) size
;
924 return TARGET_XFER_OK
;
927 return TARGET_XFER_E_IO
;
929 case TARGET_OBJECT_LIBRARIES
:
930 if (m_core_gdbarch
!= nullptr
931 && gdbarch_core_xfer_shared_libraries_p (m_core_gdbarch
))
934 return TARGET_XFER_E_IO
;
937 *xfered_len
= gdbarch_core_xfer_shared_libraries (m_core_gdbarch
,
941 if (*xfered_len
== 0)
942 return TARGET_XFER_EOF
;
944 return TARGET_XFER_OK
;
949 case TARGET_OBJECT_LIBRARIES_AIX
:
950 if (m_core_gdbarch
!= nullptr
951 && gdbarch_core_xfer_shared_libraries_aix_p (m_core_gdbarch
))
954 return TARGET_XFER_E_IO
;
958 = gdbarch_core_xfer_shared_libraries_aix (m_core_gdbarch
,
962 if (*xfered_len
== 0)
963 return TARGET_XFER_EOF
;
965 return TARGET_XFER_OK
;
970 case TARGET_OBJECT_SIGNAL_INFO
:
973 if (m_core_gdbarch
!= nullptr
974 && gdbarch_core_xfer_siginfo_p (m_core_gdbarch
))
976 LONGEST l
= gdbarch_core_xfer_siginfo (m_core_gdbarch
, readbuf
,
983 return TARGET_XFER_EOF
;
985 return TARGET_XFER_OK
;
989 return TARGET_XFER_E_IO
;
992 return this->beneath ()->xfer_partial (object
, annex
, readbuf
,
993 writebuf
, offset
, len
,
1000 /* Okay, let's be honest: threads gleaned from a core file aren't
1001 exactly lively, are they? On the other hand, if we don't claim
1002 that each & every one is alive, then we don't get any of them
1003 to appear in an "info thread" command, which is quite a useful
1007 core_target::thread_alive (ptid_t ptid
)
1012 /* Ask the current architecture what it knows about this core file.
1013 That will be used, in turn, to pick a better architecture. This
1014 wrapper could be avoided if targets got a chance to specialize
1017 const struct target_desc
*
1018 core_target::read_description ()
1020 if (m_core_gdbarch
&& gdbarch_core_read_description_p (m_core_gdbarch
))
1022 const struct target_desc
*result
;
1024 result
= gdbarch_core_read_description (m_core_gdbarch
, this, core_bfd
);
1029 return this->beneath ()->read_description ();
1033 core_target::pid_to_str (ptid_t ptid
)
1035 struct inferior
*inf
;
1038 /* The preferred way is to have a gdbarch/OS specific
1040 if (m_core_gdbarch
!= nullptr
1041 && gdbarch_core_pid_to_str_p (m_core_gdbarch
))
1042 return gdbarch_core_pid_to_str (m_core_gdbarch
, ptid
);
1044 /* Otherwise, if we don't have one, we'll just fallback to
1045 "process", with normal_pid_to_str. */
1047 /* Try the LWPID field first. */
1050 return normal_pid_to_str (ptid_t (pid
));
1052 /* Otherwise, this isn't a "threaded" core -- use the PID field, but
1053 only if it isn't a fake PID. */
1054 inf
= find_inferior_ptid (this, ptid
);
1055 if (inf
!= NULL
&& !inf
->fake_pid_p
)
1056 return normal_pid_to_str (ptid
);
1058 /* No luck. We simply don't have a valid PID to print. */
1059 return "<main task>";
1063 core_target::thread_name (struct thread_info
*thr
)
1065 if (m_core_gdbarch
!= nullptr
1066 && gdbarch_core_thread_name_p (m_core_gdbarch
))
1067 return gdbarch_core_thread_name (m_core_gdbarch
, thr
);
1072 core_target::has_memory ()
1074 return (core_bfd
!= NULL
);
1078 core_target::has_stack ()
1080 return (core_bfd
!= NULL
);
1084 core_target::has_registers ()
1086 return (core_bfd
!= NULL
);
1089 /* Implement the to_info_proc method. */
1092 core_target::info_proc (const char *args
, enum info_proc_what request
)
1094 struct gdbarch
*gdbarch
= get_current_arch ();
1096 /* Since this is the core file target, call the 'core_info_proc'
1097 method on gdbarch, not 'info_proc'. */
1098 if (gdbarch_core_info_proc_p (gdbarch
))
1099 gdbarch_core_info_proc (gdbarch
, args
, request
);
1104 /* Get a pointer to the current core target. If not connected to a
1105 core target, return NULL. */
1107 static core_target
*
1108 get_current_core_target ()
1110 target_ops
*proc_target
= current_inferior ()->process_target ();
1111 return dynamic_cast<core_target
*> (proc_target
);
1114 /* Display file backed mappings from core file. */
1117 core_target::info_proc_mappings (struct gdbarch
*gdbarch
)
1119 if (m_core_file_mappings
.sections
!= m_core_file_mappings
.sections_end
)
1121 printf_filtered (_("Mapped address spaces:\n\n"));
1122 if (gdbarch_addr_bit (gdbarch
) == 32)
1124 printf_filtered ("\t%10s %10s %10s %10s %s\n",
1127 " Size", " Offset", "objfile");
1131 printf_filtered (" %18s %18s %10s %10s %s\n",
1134 " Size", " Offset", "objfile");
1138 for (const struct target_section
*tsp
= m_core_file_mappings
.sections
;
1139 tsp
< m_core_file_mappings
.sections_end
;
1142 ULONGEST start
= tsp
->addr
;
1143 ULONGEST end
= tsp
->endaddr
;
1144 ULONGEST file_ofs
= tsp
->the_bfd_section
->filepos
;
1145 const char *filename
= bfd_get_filename (tsp
->the_bfd_section
->owner
);
1147 if (gdbarch_addr_bit (gdbarch
) == 32)
1148 printf_filtered ("\t%10s %10s %10s %10s %s\n",
1149 paddress (gdbarch
, start
),
1150 paddress (gdbarch
, end
),
1151 hex_string (end
- start
),
1152 hex_string (file_ofs
),
1155 printf_filtered (" %18s %18s %10s %10s %s\n",
1156 paddress (gdbarch
, start
),
1157 paddress (gdbarch
, end
),
1158 hex_string (end
- start
),
1159 hex_string (file_ofs
),
1164 /* Implement "maintenance print core-file-backed-mappings" command.
1166 If mappings are loaded, the results should be similar to the
1167 mappings shown by "info proc mappings". This command is mainly a
1168 debugging tool for GDB developers to make sure that the expected
1169 mappings are present after loading a core file. For Linux, the
1170 output provided by this command will be very similar (if not
1171 identical) to that provided by "info proc mappings". This is not
1172 necessarily the case for other OSes which might provide
1173 more/different information in the "info proc mappings" output. */
1176 maintenance_print_core_file_backed_mappings (const char *args
, int from_tty
)
1178 core_target
*targ
= get_current_core_target ();
1179 if (targ
!= nullptr)
1180 targ
->info_proc_mappings (targ
->core_gdbarch ());
1183 void _initialize_corelow ();
1185 _initialize_corelow ()
1187 add_target (core_target_info
, core_target_open
, filename_completer
);
1188 add_cmd ("core-file-backed-mappings", class_maintenance
,
1189 maintenance_print_core_file_backed_mappings
,
1190 _("Print core file's file-backed mappings."),
1191 &maintenanceprintlist
);