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
71 const target_info
&info () const override
72 { return core_target_info
; }
74 void close () override
;
75 void detach (inferior
*, int) override
;
76 void fetch_registers (struct regcache
*, int) override
;
78 enum target_xfer_status
xfer_partial (enum target_object object
,
81 const gdb_byte
*writebuf
,
82 ULONGEST offset
, ULONGEST len
,
83 ULONGEST
*xfered_len
) override
;
84 void files_info () override
;
86 bool thread_alive (ptid_t ptid
) override
;
87 const struct target_desc
*read_description () override
;
89 std::string
pid_to_str (ptid_t
) override
;
91 const char *thread_name (struct thread_info
*) override
;
93 bool has_all_memory () override
{ return true; }
94 bool has_memory () override
;
95 bool has_stack () override
;
96 bool has_registers () override
;
97 bool has_execution (inferior
*inf
) override
{ return false; }
99 bool info_proc (const char *, enum info_proc_what
) override
;
103 /* Getter, see variable definition. */
104 struct gdbarch
*core_gdbarch ()
106 return m_core_gdbarch
;
109 /* See definition. */
110 void get_core_register_section (struct regcache
*regcache
,
111 const struct regset
*regset
,
113 int section_min_size
,
114 const char *human_name
,
117 /* See definition. */
118 void info_proc_mappings (struct gdbarch
*gdbarch
);
120 gdb::byte_vector
read_capability (CORE_ADDR addr
) override
;
122 private: /* per-core data */
124 /* The core's section table. Note that these target sections are
125 *not* mapped in the current address spaces' set of target
126 sections --- those should come only from pure executable or
127 shared library bfds. The core bfd sections are an implementation
128 detail of the core target, just like ptrace is for unix child
130 target_section_table m_core_section_table
;
132 /* File-backed address space mappings: some core files include
133 information about memory mapped files. */
134 target_section_table m_core_file_mappings
;
136 /* Unavailable mappings. These correspond to pathnames which either
137 weren't found or could not be opened. Knowing these addresses can
139 std::vector
<mem_range
> m_core_unavailable_mappings
;
141 /* Build m_core_file_mappings. Called from the constructor. */
142 void build_file_mappings ();
144 /* Helper method for xfer_partial. */
145 enum target_xfer_status
xfer_memory_via_mappings (gdb_byte
*readbuf
,
146 const gdb_byte
*writebuf
,
149 ULONGEST
*xfered_len
);
151 /* FIXME: kettenis/20031023: Eventually this field should
153 struct gdbarch
*m_core_gdbarch
= NULL
;
156 core_target::core_target ()
158 m_core_gdbarch
= gdbarch_from_bfd (core_bfd
);
161 || !gdbarch_iterate_over_regset_sections_p (m_core_gdbarch
))
162 error (_("\"%s\": Core file format not supported"),
163 bfd_get_filename (core_bfd
));
165 /* Find the data section */
166 m_core_section_table
= build_section_table (core_bfd
);
168 build_file_mappings ();
171 /* Construct the target_section_table for file-backed mappings if
174 For each unique path in the note, we'll open a BFD with a bfd
175 target of "binary". This is an unstructured bfd target upon which
176 we'll impose a structure from the mappings in the architecture-specific
177 mappings note. A BFD section is allocated and initialized for each
180 We take care to not share already open bfds with other parts of
181 GDB; in particular, we don't want to add new sections to existing
182 BFDs. We do, however, ensure that the BFDs that we allocate here
183 will go away (be deallocated) when the core target is detached. */
186 core_target::build_file_mappings ()
188 std::unordered_map
<std::string
, struct bfd
*> bfd_map
;
189 std::unordered_set
<std::string
> unavailable_paths
;
191 /* See linux_read_core_file_mappings() in linux-tdep.c for an example
192 read_core_file_mappings method. */
193 gdbarch_read_core_file_mappings (m_core_gdbarch
, core_bfd
,
195 /* After determining the number of mappings, read_core_file_mappings
196 will invoke this lambda. */
201 /* read_core_file_mappings will invoke this lambda for each mapping
203 [&] (int num
, ULONGEST start
, ULONGEST end
, ULONGEST file_ofs
,
204 const char *filename
, const void *other
)
206 /* Architecture-specific read_core_mapping methods are expected to
207 weed out non-file-backed mappings. */
208 gdb_assert (filename
!= nullptr);
210 struct bfd
*bfd
= bfd_map
[filename
];
213 /* Use exec_file_find() to do sysroot expansion. It'll
214 also strip the potential sysroot "target:" prefix. If
215 there is no sysroot, an equivalent (possibly more
216 canonical) pathname will be provided. */
217 gdb::unique_xmalloc_ptr
<char> expanded_fname
218 = exec_file_find (filename
, NULL
);
219 if (expanded_fname
== nullptr)
221 m_core_unavailable_mappings
.emplace_back (start
, end
- start
);
222 /* Print just one warning per path. */
223 if (unavailable_paths
.insert (filename
).second
)
224 warning (_("Can't open file %s during file-backed mapping "
230 bfd
= bfd_map
[filename
] = bfd_openr (expanded_fname
.get (),
233 if (bfd
== nullptr || !bfd_check_format (bfd
, bfd_object
))
235 m_core_unavailable_mappings
.emplace_back (start
, end
- start
);
236 /* If we get here, there's a good chance that it's due to
237 an internal error. We issue a warning instead of an
238 internal error because of the possibility that the
239 file was removed in between checking for its
240 existence during the expansion in exec_file_find()
241 and the calls to bfd_openr() / bfd_check_format().
242 Output both the path from the core file note along
243 with its expansion to make debugging this problem
245 warning (_("Can't open file %s which was expanded to %s "
246 "during file-backed mapping note processing"),
247 filename
, expanded_fname
.get ());
252 /* Ensure that the bfd will be closed when core_bfd is closed.
253 This can be checked before/after a core file detach via
254 "maint info bfds". */
255 gdb_bfd_record_inclusion (core_bfd
, bfd
);
258 /* Make new BFD section. All sections have the same name,
259 which is permitted by bfd_make_section_anyway(). */
260 asection
*sec
= bfd_make_section_anyway (bfd
, "load");
262 error (_("Can't make section"));
263 sec
->filepos
= file_ofs
;
264 bfd_set_section_flags (sec
, SEC_READONLY
| SEC_HAS_CONTENTS
);
265 bfd_set_section_size (sec
, end
- start
);
266 bfd_set_section_vma (sec
, start
);
267 bfd_set_section_lma (sec
, start
);
268 bfd_set_section_alignment (sec
, 2);
270 /* Set target_section fields. */
271 m_core_file_mappings
.emplace_back ();
272 target_section
&ts
= m_core_file_mappings
.back ();
276 ts
.the_bfd_section
= sec
;
279 normalize_mem_ranges (&m_core_unavailable_mappings
);
282 /* An arbitrary identifier for the core inferior. */
283 #define CORELOW_PID 1
285 /* Close the core target. */
288 core_target::close ()
292 switch_to_no_thread (); /* Avoid confusion from thread
294 exit_inferior_silent (current_inferior ());
296 /* Clear out solib state while the bfd is still open. See
297 comments in clear_solib in solib.c. */
300 current_program_space
->cbfd
.reset (nullptr);
303 /* Core targets are heap-allocated (see core_target_open), so here
304 we delete ourselves. */
308 /* Look for sections whose names start with `.reg/' so that we can
309 extract the list of threads in a core file. */
312 add_to_thread_list (asection
*asect
, asection
*reg_sect
)
316 bool fake_pid_p
= false;
317 struct inferior
*inf
;
319 if (!startswith (bfd_section_name (asect
), ".reg/"))
322 core_tid
= atoi (bfd_section_name (asect
) + 5);
324 pid
= bfd_core_file_pid (core_bfd
);
333 inf
= current_inferior ();
336 inferior_appeared (inf
, pid
);
337 inf
->fake_pid_p
= fake_pid_p
;
340 ptid_t
ptid (pid
, lwpid
);
342 thread_info
*thr
= add_thread (inf
->process_target (), ptid
);
344 /* Warning, Will Robinson, looking at BFD private data! */
347 && asect
->filepos
== reg_sect
->filepos
) /* Did we find .reg? */
348 switch_to_thread (thr
); /* Yes, make it current. */
351 /* Issue a message saying we have no core to debug, if FROM_TTY. */
354 maybe_say_no_core_file_now (int from_tty
)
357 printf_filtered (_("No core file now.\n"));
360 /* Backward compatibility with old way of specifying core files. */
363 core_file_command (const char *filename
, int from_tty
)
365 dont_repeat (); /* Either way, seems bogus. */
367 if (filename
== NULL
)
369 if (core_bfd
!= NULL
)
371 target_detach (current_inferior (), from_tty
);
372 gdb_assert (core_bfd
== NULL
);
375 maybe_say_no_core_file_now (from_tty
);
378 core_target_open (filename
, from_tty
);
381 /* Locate (and load) an executable file (and symbols) given the core file
385 locate_exec_from_corefile_build_id (bfd
*abfd
, int from_tty
)
387 const bfd_build_id
*build_id
= build_id_bfd_get (abfd
);
388 if (build_id
== nullptr)
391 gdb_bfd_ref_ptr execbfd
392 = build_id_to_exec_bfd (build_id
->size
, build_id
->data
);
394 if (execbfd
!= nullptr)
396 exec_file_attach (bfd_get_filename (execbfd
.get ()), from_tty
);
397 symbol_file_add_main (bfd_get_filename (execbfd
.get ()),
398 symfile_add_flag (from_tty
? SYMFILE_VERBOSE
: 0));
405 core_target_open (const char *arg
, int from_tty
)
412 target_preopen (from_tty
);
416 error (_("No core file specified. (Use `detach' "
417 "to stop debugging a core file.)"));
419 error (_("No core file specified."));
422 gdb::unique_xmalloc_ptr
<char> filename (tilde_expand (arg
));
423 if (!IS_ABSOLUTE_PATH (filename
.get ()))
424 filename
= gdb_abspath (filename
.get ());
426 flags
= O_BINARY
| O_LARGEFILE
;
431 scratch_chan
= gdb_open_cloexec (filename
.get (), flags
, 0);
432 if (scratch_chan
< 0)
433 perror_with_name (filename
.get ());
435 gdb_bfd_ref_ptr
temp_bfd (gdb_bfd_fopen (filename
.get (), gnutarget
,
436 write_files
? FOPEN_RUB
: FOPEN_RB
,
438 if (temp_bfd
== NULL
)
439 perror_with_name (filename
.get ());
441 if (!bfd_check_format (temp_bfd
.get (), bfd_core
))
443 /* Do it after the err msg */
444 /* FIXME: should be checking for errors from bfd_close (for one
445 thing, on error it does not free all the storage associated
447 error (_("\"%s\" is not a core dump: %s"),
448 filename
.get (), bfd_errmsg (bfd_get_error ()));
451 current_program_space
->cbfd
= std::move (temp_bfd
);
453 core_target
*target
= new core_target ();
455 /* Own the target until it is successfully pushed. */
456 target_ops_up
target_holder (target
);
460 /* If we have no exec file, try to set the architecture from the
461 core file. We don't do this unconditionally since an exec file
462 typically contains more information that helps us determine the
463 architecture than a core file. */
465 set_gdbarch_from_file (core_bfd
);
467 push_target (std::move (target_holder
));
469 switch_to_no_thread ();
471 /* Need to flush the register cache (and the frame cache) from a
472 previous debug session. If inferior_ptid ends up the same as the
473 last debug session --- e.g., b foo; run; gcore core1; step; gcore
474 core2; core core1; core core2 --- then there's potential for
475 get_current_regcache to return the cached regcache of the
476 previous session, and the frame cache being stale. */
477 registers_changed ();
479 /* Build up thread list from BFD sections, and possibly set the
480 current thread to the .reg/NN section matching the .reg
482 asection
*reg_sect
= bfd_get_section_by_name (core_bfd
, ".reg");
483 for (asection
*sect
: gdb_bfd_sections (core_bfd
))
484 add_to_thread_list (sect
, reg_sect
);
486 if (inferior_ptid
== null_ptid
)
488 /* Either we found no .reg/NN section, and hence we have a
489 non-threaded core (single-threaded, from gdb's perspective),
490 or for some reason add_to_thread_list couldn't determine
491 which was the "main" thread. The latter case shouldn't
492 usually happen, but we're dealing with input here, which can
493 always be broken in different ways. */
494 thread_info
*thread
= first_thread_of_inferior (current_inferior ());
498 inferior_appeared (current_inferior (), CORELOW_PID
);
499 thread
= add_thread_silent (target
, ptid_t (CORELOW_PID
));
502 switch_to_thread (thread
);
505 if (exec_bfd
== nullptr)
506 locate_exec_from_corefile_build_id (core_bfd
, from_tty
);
508 post_create_inferior (from_tty
);
510 /* Now go through the target stack looking for threads since there
511 may be a thread_stratum target loaded on top of target core by
512 now. The layer above should claim threads found in the BFD
516 target_update_thread_list ();
519 catch (const gdb_exception_error
&except
)
521 exception_print (gdb_stderr
, except
);
524 p
= bfd_core_file_failing_command (core_bfd
);
526 printf_filtered (_("Core was generated by `%s'.\n"), p
);
528 /* Clearing any previous state of convenience variables. */
529 clear_exit_convenience_vars ();
531 siggy
= bfd_core_file_failing_signal (core_bfd
);
534 gdbarch
*core_gdbarch
= target
->core_gdbarch ();
536 /* If we don't have a CORE_GDBARCH to work with, assume a native
537 core (map gdb_signal from host signals). If we do have
538 CORE_GDBARCH to work with, but no gdb_signal_from_target
539 implementation for that gdbarch, as a fallback measure,
540 assume the host signal mapping. It'll be correct for native
541 cores, but most likely incorrect for cross-cores. */
542 enum gdb_signal sig
= (core_gdbarch
!= NULL
543 && gdbarch_gdb_signal_from_target_p (core_gdbarch
)
544 ? gdbarch_gdb_signal_from_target (core_gdbarch
,
546 : gdb_signal_from_host (siggy
));
548 printf_filtered (_("Program terminated with signal %s, %s"),
549 gdb_signal_to_name (sig
), gdb_signal_to_string (sig
));
550 if (gdbarch_report_signal_info_p (core_gdbarch
))
551 gdbarch_report_signal_info (core_gdbarch
, current_uiout
, sig
);
552 printf_filtered (_(".\n"));
554 /* Set the value of the internal variable $_exitsignal,
555 which holds the signal uncaught by the inferior. */
556 set_internalvar_integer (lookup_internalvar ("_exitsignal"),
560 /* Fetch all registers from core file. */
561 target_fetch_registers (get_current_regcache (), -1);
563 /* Now, set up the frame cache, and print the top of stack. */
564 reinit_frame_cache ();
565 print_stack_frame (get_selected_frame (NULL
), 1, SRC_AND_LOC
, 1);
567 /* Current thread should be NUM 1 but the user does not know that.
568 If a program is single threaded gdb in general does not mention
569 anything about threads. That is why the test is >= 2. */
570 if (thread_count (target
) >= 2)
574 thread_command (NULL
, from_tty
);
576 catch (const gdb_exception_error
&except
)
578 exception_print (gdb_stderr
, except
);
584 core_target::detach (inferior
*inf
, int from_tty
)
586 /* Note that 'this' is dangling after this call. unpush_target
587 closes the target, and our close implementation deletes
589 unpush_target (this);
591 /* Clear the register cache and the frame cache. */
592 registers_changed ();
593 reinit_frame_cache ();
594 maybe_say_no_core_file_now (from_tty
);
597 /* Try to retrieve registers from a section in core_bfd, and supply
600 If ptid's lwp member is zero, do the single-threaded
601 thing: look for a section named NAME. If ptid's lwp
602 member is non-zero, do the multi-threaded thing: look for a section
603 named "NAME/LWP", where LWP is the shortest ASCII decimal
604 representation of ptid's lwp member.
606 HUMAN_NAME is a human-readable name for the kind of registers the
607 NAME section contains, for use in error messages.
609 If REQUIRED is true, print an error if the core file doesn't have a
610 section by the appropriate name. Otherwise, just do nothing. */
613 core_target::get_core_register_section (struct regcache
*regcache
,
614 const struct regset
*regset
,
616 int section_min_size
,
617 const char *human_name
,
620 gdb_assert (regset
!= nullptr);
622 struct bfd_section
*section
;
624 bool variable_size_section
= (regset
->flags
& REGSET_VARIABLE_SIZE
);
626 thread_section_name
section_name (name
, regcache
->ptid ());
628 section
= bfd_get_section_by_name (core_bfd
, section_name
.c_str ());
632 warning (_("Couldn't find %s registers in core file."),
637 size
= bfd_section_size (section
);
638 if (size
< section_min_size
)
640 warning (_("Section `%s' in core file too small."),
641 section_name
.c_str ());
644 if (size
!= section_min_size
&& !variable_size_section
)
646 warning (_("Unexpected size of section `%s' in core file."),
647 section_name
.c_str ());
650 gdb::byte_vector
contents (size
);
651 if (!bfd_get_section_contents (core_bfd
, section
, contents
.data (),
654 warning (_("Couldn't read %s registers from `%s' section in core file."),
655 human_name
, section_name
.c_str ());
659 regset
->supply_regset (regset
, regcache
, -1, contents
.data (), size
);
662 /* Data passed to gdbarch_iterate_over_regset_sections's callback. */
663 struct get_core_registers_cb_data
666 struct regcache
*regcache
;
669 /* Callback for get_core_registers that handles a single core file
670 register note section. */
673 get_core_registers_cb (const char *sect_name
, int supply_size
, int collect_size
,
674 const struct regset
*regset
,
675 const char *human_name
, void *cb_data
)
677 gdb_assert (regset
!= nullptr);
679 auto *data
= (get_core_registers_cb_data
*) cb_data
;
680 bool required
= false;
681 bool variable_size_section
= (regset
->flags
& REGSET_VARIABLE_SIZE
);
683 if (!variable_size_section
)
684 gdb_assert (supply_size
== collect_size
);
686 if (strcmp (sect_name
, ".reg") == 0)
689 if (human_name
== NULL
)
690 human_name
= "general-purpose";
692 else if (strcmp (sect_name
, ".reg2") == 0)
694 if (human_name
== NULL
)
695 human_name
= "floating-point";
698 data
->target
->get_core_register_section (data
->regcache
, regset
, sect_name
,
699 supply_size
, human_name
, required
);
702 /* Get the registers out of a core file. This is the machine-
703 independent part. Fetch_core_registers is the machine-dependent
704 part, typically implemented in the xm-file for each
707 /* We just get all the registers, so we don't use regno. */
710 core_target::fetch_registers (struct regcache
*regcache
, int regno
)
712 if (!(m_core_gdbarch
!= nullptr
713 && gdbarch_iterate_over_regset_sections_p (m_core_gdbarch
)))
715 fprintf_filtered (gdb_stderr
,
716 "Can't fetch registers from this type of core file\n");
720 struct gdbarch
*gdbarch
= regcache
->arch ();
721 get_core_registers_cb_data data
= { this, regcache
};
722 gdbarch_iterate_over_regset_sections (gdbarch
,
723 get_core_registers_cb
,
724 (void *) &data
, NULL
);
726 /* Mark all registers not found in the core as unavailable. */
727 for (int i
= 0; i
< gdbarch_num_regs (regcache
->arch ()); i
++)
728 if (regcache
->get_register_status (i
) == REG_UNKNOWN
)
729 regcache
->raw_supply (i
, NULL
);
733 core_target::files_info ()
735 print_section_info (&m_core_section_table
, core_bfd
);
738 /* Helper method for core_target::xfer_partial. */
740 enum target_xfer_status
741 core_target::xfer_memory_via_mappings (gdb_byte
*readbuf
,
742 const gdb_byte
*writebuf
,
743 ULONGEST offset
, ULONGEST len
,
744 ULONGEST
*xfered_len
)
746 enum target_xfer_status xfer_status
;
748 xfer_status
= (section_table_xfer_memory_partial
750 offset
, len
, xfered_len
,
751 m_core_file_mappings
));
753 if (xfer_status
== TARGET_XFER_OK
|| m_core_unavailable_mappings
.empty ())
756 /* There are instances - e.g. when debugging within a docker
757 container using the AUFS storage driver - where the pathnames
758 obtained from the note section are incorrect. Despite the path
759 being wrong, just knowing the start and end addresses of the
760 mappings is still useful; we can attempt an access of the file
761 stratum constrained to the address ranges corresponding to the
762 unavailable mappings. */
764 ULONGEST memaddr
= offset
;
765 ULONGEST memend
= offset
+ len
;
767 for (const auto &mr
: m_core_unavailable_mappings
)
769 if (address_in_mem_range (memaddr
, &mr
))
771 if (!address_in_mem_range (memend
, &mr
))
772 len
= mr
.start
+ mr
.length
- memaddr
;
774 xfer_status
= this->beneath ()->xfer_partial (TARGET_OBJECT_MEMORY
,
788 enum target_xfer_status
789 core_target::xfer_partial (enum target_object object
, const char *annex
,
790 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
791 ULONGEST offset
, ULONGEST len
, ULONGEST
*xfered_len
)
795 case TARGET_OBJECT_MEMORY
:
797 enum target_xfer_status xfer_status
;
799 /* Try accessing memory contents from core file data,
800 restricting consideration to those sections for which
801 the BFD section flag SEC_HAS_CONTENTS is set. */
802 auto has_contents_cb
= [] (const struct target_section
*s
)
804 return ((s
->the_bfd_section
->flags
& SEC_HAS_CONTENTS
) != 0);
806 xfer_status
= section_table_xfer_memory_partial
808 offset
, len
, xfered_len
,
809 m_core_section_table
,
811 if (xfer_status
== TARGET_XFER_OK
)
812 return TARGET_XFER_OK
;
814 /* Check file backed mappings. If they're available, use
815 core file provided mappings (e.g. from .note.linuxcore.file
816 or the like) as this should provide a more accurate
817 result. If not, check the stratum beneath us, which should
818 be the file stratum. */
819 if (!m_core_file_mappings
.empty ())
820 xfer_status
= xfer_memory_via_mappings (readbuf
, writebuf
, offset
,
823 xfer_status
= this->beneath ()->xfer_partial (object
, annex
, readbuf
,
824 writebuf
, offset
, len
,
826 if (xfer_status
== TARGET_XFER_OK
)
827 return TARGET_XFER_OK
;
829 /* Finally, attempt to access data in core file sections with
830 no contents. These will typically read as all zero. */
831 auto no_contents_cb
= [&] (const struct target_section
*s
)
833 return !has_contents_cb (s
);
835 xfer_status
= section_table_xfer_memory_partial
837 offset
, len
, xfered_len
,
838 m_core_section_table
,
843 case TARGET_OBJECT_AUXV
:
846 /* When the aux vector is stored in core file, BFD
847 represents this with a fake section called ".auxv". */
849 struct bfd_section
*section
;
852 section
= bfd_get_section_by_name (core_bfd
, ".auxv");
854 return TARGET_XFER_E_IO
;
856 size
= bfd_section_size (section
);
858 return TARGET_XFER_EOF
;
864 return TARGET_XFER_EOF
;
865 if (!bfd_get_section_contents (core_bfd
, section
, readbuf
,
866 (file_ptr
) offset
, size
))
868 warning (_("Couldn't read NT_AUXV note in core file."));
869 return TARGET_XFER_E_IO
;
872 *xfered_len
= (ULONGEST
) size
;
873 return TARGET_XFER_OK
;
875 return TARGET_XFER_E_IO
;
877 case TARGET_OBJECT_WCOOKIE
:
880 /* When the StackGhost cookie is stored in core file, BFD
881 represents this with a fake section called
884 struct bfd_section
*section
;
887 section
= bfd_get_section_by_name (core_bfd
, ".wcookie");
889 return TARGET_XFER_E_IO
;
891 size
= bfd_section_size (section
);
893 return TARGET_XFER_EOF
;
899 return TARGET_XFER_EOF
;
900 if (!bfd_get_section_contents (core_bfd
, section
, readbuf
,
901 (file_ptr
) offset
, size
))
903 warning (_("Couldn't read StackGhost cookie in core file."));
904 return TARGET_XFER_E_IO
;
907 *xfered_len
= (ULONGEST
) size
;
908 return TARGET_XFER_OK
;
911 return TARGET_XFER_E_IO
;
913 case TARGET_OBJECT_LIBRARIES
:
914 if (m_core_gdbarch
!= nullptr
915 && gdbarch_core_xfer_shared_libraries_p (m_core_gdbarch
))
918 return TARGET_XFER_E_IO
;
921 *xfered_len
= gdbarch_core_xfer_shared_libraries (m_core_gdbarch
,
925 if (*xfered_len
== 0)
926 return TARGET_XFER_EOF
;
928 return TARGET_XFER_OK
;
933 case TARGET_OBJECT_LIBRARIES_AIX
:
934 if (m_core_gdbarch
!= nullptr
935 && gdbarch_core_xfer_shared_libraries_aix_p (m_core_gdbarch
))
938 return TARGET_XFER_E_IO
;
942 = gdbarch_core_xfer_shared_libraries_aix (m_core_gdbarch
,
946 if (*xfered_len
== 0)
947 return TARGET_XFER_EOF
;
949 return TARGET_XFER_OK
;
954 case TARGET_OBJECT_SIGNAL_INFO
:
957 if (m_core_gdbarch
!= nullptr
958 && gdbarch_core_xfer_siginfo_p (m_core_gdbarch
))
960 LONGEST l
= gdbarch_core_xfer_siginfo (m_core_gdbarch
, readbuf
,
967 return TARGET_XFER_EOF
;
969 return TARGET_XFER_OK
;
973 return TARGET_XFER_E_IO
;
976 return this->beneath ()->xfer_partial (object
, annex
, readbuf
,
977 writebuf
, offset
, len
,
984 /* Okay, let's be honest: threads gleaned from a core file aren't
985 exactly lively, are they? On the other hand, if we don't claim
986 that each & every one is alive, then we don't get any of them
987 to appear in an "info thread" command, which is quite a useful
991 core_target::thread_alive (ptid_t ptid
)
996 /* Ask the current architecture what it knows about this core file.
997 That will be used, in turn, to pick a better architecture. This
998 wrapper could be avoided if targets got a chance to specialize
1001 const struct target_desc
*
1002 core_target::read_description ()
1004 if (m_core_gdbarch
&& gdbarch_core_read_description_p (m_core_gdbarch
))
1006 const struct target_desc
*result
;
1008 result
= gdbarch_core_read_description (m_core_gdbarch
, this, core_bfd
);
1013 return this->beneath ()->read_description ();
1017 core_target::pid_to_str (ptid_t ptid
)
1019 struct inferior
*inf
;
1022 /* The preferred way is to have a gdbarch/OS specific
1024 if (m_core_gdbarch
!= nullptr
1025 && gdbarch_core_pid_to_str_p (m_core_gdbarch
))
1026 return gdbarch_core_pid_to_str (m_core_gdbarch
, ptid
);
1028 /* Otherwise, if we don't have one, we'll just fallback to
1029 "process", with normal_pid_to_str. */
1031 /* Try the LWPID field first. */
1034 return normal_pid_to_str (ptid_t (pid
));
1036 /* Otherwise, this isn't a "threaded" core -- use the PID field, but
1037 only if it isn't a fake PID. */
1038 inf
= find_inferior_ptid (this, ptid
);
1039 if (inf
!= NULL
&& !inf
->fake_pid_p
)
1040 return normal_pid_to_str (ptid
);
1042 /* No luck. We simply don't have a valid PID to print. */
1043 return "<main task>";
1047 core_target::thread_name (struct thread_info
*thr
)
1049 if (m_core_gdbarch
!= nullptr
1050 && gdbarch_core_thread_name_p (m_core_gdbarch
))
1051 return gdbarch_core_thread_name (m_core_gdbarch
, thr
);
1056 core_target::has_memory ()
1058 return (core_bfd
!= NULL
);
1062 core_target::has_stack ()
1064 return (core_bfd
!= NULL
);
1068 core_target::has_registers ()
1070 return (core_bfd
!= NULL
);
1073 /* Implement the to_info_proc method. */
1076 core_target::info_proc (const char *args
, enum info_proc_what request
)
1078 struct gdbarch
*gdbarch
= get_current_arch ();
1080 /* Since this is the core file target, call the 'core_info_proc'
1081 method on gdbarch, not 'info_proc'. */
1082 if (gdbarch_core_info_proc_p (gdbarch
))
1083 gdbarch_core_info_proc (gdbarch
, args
, request
);
1088 /* Get a pointer to the current core target. If not connected to a
1089 core target, return NULL. */
1091 static core_target
*
1092 get_current_core_target ()
1094 target_ops
*proc_target
= current_inferior ()->process_target ();
1095 return dynamic_cast<core_target
*> (proc_target
);
1098 /* Display file backed mappings from core file. */
1101 core_target::info_proc_mappings (struct gdbarch
*gdbarch
)
1103 if (!m_core_file_mappings
.empty ())
1105 printf_filtered (_("Mapped address spaces:\n\n"));
1106 if (gdbarch_addr_bit (gdbarch
) == 32)
1108 printf_filtered ("\t%10s %10s %10s %10s %s\n",
1111 " Size", " Offset", "objfile");
1115 printf_filtered (" %18s %18s %10s %10s %s\n",
1118 " Size", " Offset", "objfile");
1122 for (const target_section
&tsp
: m_core_file_mappings
)
1124 ULONGEST start
= tsp
.addr
;
1125 ULONGEST end
= tsp
.endaddr
;
1126 ULONGEST file_ofs
= tsp
.the_bfd_section
->filepos
;
1127 const char *filename
= bfd_get_filename (tsp
.the_bfd_section
->owner
);
1129 if (gdbarch_addr_bit (gdbarch
) == 32)
1130 printf_filtered ("\t%10s %10s %10s %10s %s\n",
1131 paddress (gdbarch
, start
),
1132 paddress (gdbarch
, end
),
1133 hex_string (end
- start
),
1134 hex_string (file_ofs
),
1137 printf_filtered (" %18s %18s %10s %10s %s\n",
1138 paddress (gdbarch
, start
),
1139 paddress (gdbarch
, end
),
1140 hex_string (end
- start
),
1141 hex_string (file_ofs
),
1146 /* Implement the read_capability target method. */
1149 core_target::read_capability (CORE_ADDR addr
)
1151 struct gdbarch
*gdbarch
= target_gdbarch ();
1153 gdb::byte_vector
cap(0);
1155 /* Make sure we have a way to decode the memory tag notes. */
1156 if (!gdbarch_decode_memtag_note_p (gdbarch
))
1157 warning (_("gdbarch_decode_memtag_note not implemented for this "
1161 = bfd_get_section_by_name (core_bfd
, ".memtag");
1163 /* Go through all the memtag sections and figure out if ADDR
1164 falls within one of the memory ranges that contain tags. */
1165 while (section
!= nullptr)
1167 size_t note_size
= bfd_section_size (section
);
1169 if (note_size
< 2 * sizeof (uint64_t) + sizeof (uint16_t))
1171 warning (_("malformed core note - too short for header"));
1175 gdb::byte_vector
note (note_size
);
1177 if (!bfd_get_section_contents (core_bfd
, section
,
1178 note
.data (), 0, note_size
))
1180 warning (_("could not get core note contents."));
1184 uint64_t start_address
;
1185 memcpy (&start_address
, note
.data () + sizeof (uint16_t),
1187 uint64_t end_address
;
1188 memcpy (&end_address
,
1189 note
.data () + sizeof (uint16_t) + sizeof (uint64_t),
1192 /* Is the address within [start_address, end_address)? */
1193 if (addr
>= start_address
1194 && addr
< end_address
)
1196 /* Hardcoded to hold Morello capabilities for now. */
1198 /* Decode the memory tag note and return the tags. */
1199 cap
[0] = gdbarch_decode_memtag_note (gdbarch
, note
, addr
);
1201 /* Read the capability. */
1202 if (target_read_memory (addr
, cap
.data () + 1, 16) == 0)
1205 error (_("Could not read capability from core file.\n"));
1208 /* Keep looking. Get the next session. */
1209 section
= bfd_get_next_section_by_name (core_bfd
, section
);
1215 /* Implement "maintenance print core-file-backed-mappings" command.
1217 If mappings are loaded, the results should be similar to the
1218 mappings shown by "info proc mappings". This command is mainly a
1219 debugging tool for GDB developers to make sure that the expected
1220 mappings are present after loading a core file. For Linux, the
1221 output provided by this command will be very similar (if not
1222 identical) to that provided by "info proc mappings". This is not
1223 necessarily the case for other OSes which might provide
1224 more/different information in the "info proc mappings" output. */
1227 maintenance_print_core_file_backed_mappings (const char *args
, int from_tty
)
1229 core_target
*targ
= get_current_core_target ();
1230 if (targ
!= nullptr)
1231 targ
->info_proc_mappings (targ
->core_gdbarch ());
1234 void _initialize_corelow ();
1236 _initialize_corelow ()
1238 add_target (core_target_info
, core_target_open
, filename_completer
);
1239 add_cmd ("core-file-backed-mappings", class_maintenance
,
1240 maintenance_print_core_file_backed_mappings
,
1241 _("Print core file's file-backed mappings."),
1242 &maintenanceprintlist
);