1 /* Core dump and executable file functions below target vector, for GDB.
3 Copyright (C) 1986-2021 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>
52 #include "xml-tdesc.h"
58 /* The core file target. */
60 static const target_info core_target_info = {
62 N_("Local core dump file"),
63 N_("Use a core file as a target.\n\
64 Specify the filename of the core file.")
67 class core_target final : public process_stratum_target
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 m_core_section_table = build_section_table (core_bfd);
167 build_file_mappings ();
170 /* Construct the target_section_table for file-backed mappings if
173 For each unique path in the note, we'll open a BFD with a bfd
174 target of "binary". This is an unstructured bfd target upon which
175 we'll impose a structure from the mappings in the architecture-specific
176 mappings note. A BFD section is allocated and initialized for each
179 We take care to not share already open bfds with other parts of
180 GDB; in particular, we don't want to add new sections to existing
181 BFDs. We do, however, ensure that the BFDs that we allocate here
182 will go away (be deallocated) when the core target is detached. */
185 core_target::build_file_mappings ()
187 std::unordered_map<std::string, struct bfd *> bfd_map;
188 std::unordered_set<std::string> unavailable_paths;
190 /* See linux_read_core_file_mappings() in linux-tdep.c for an example
191 read_core_file_mappings method. */
192 gdbarch_read_core_file_mappings (m_core_gdbarch, core_bfd,
194 /* After determining the number of mappings, read_core_file_mappings
195 will invoke this lambda. */
200 /* read_core_file_mappings will invoke this lambda for each mapping
202 [&] (int num, ULONGEST start, ULONGEST end, ULONGEST file_ofs,
203 const char *filename)
205 /* Architecture-specific read_core_mapping methods are expected to
206 weed out non-file-backed mappings. */
207 gdb_assert (filename != nullptr);
209 struct bfd *bfd = bfd_map[filename];
212 /* Use exec_file_find() to do sysroot expansion. It'll
213 also strip the potential sysroot "target:" prefix. If
214 there is no sysroot, an equivalent (possibly more
215 canonical) pathname will be provided. */
216 gdb::unique_xmalloc_ptr<char> expanded_fname
217 = exec_file_find (filename, NULL);
218 if (expanded_fname == nullptr)
220 m_core_unavailable_mappings.emplace_back (start, end - start);
221 /* Print just one warning per path. */
222 if (unavailable_paths.insert (filename).second)
223 warning (_("Can't open file %s during file-backed mapping "
229 bfd = bfd_map[filename] = bfd_openr (expanded_fname.get (),
232 if (bfd == nullptr || !bfd_check_format (bfd, bfd_object))
234 m_core_unavailable_mappings.emplace_back (start, end - start);
235 /* If we get here, there's a good chance that it's due to
236 an internal error. We issue a warning instead of an
237 internal error because of the possibility that the
238 file was removed in between checking for its
239 existence during the expansion in exec_file_find()
240 and the calls to bfd_openr() / bfd_check_format().
241 Output both the path from the core file note along
242 with its expansion to make debugging this problem
244 warning (_("Can't open file %s which was expanded to %s "
245 "during file-backed mapping note processing"),
246 filename, expanded_fname.get ());
251 /* Ensure that the bfd will be closed when core_bfd is closed.
252 This can be checked before/after a core file detach via
253 "maint info bfds". */
254 gdb_bfd_record_inclusion (core_bfd, bfd);
257 /* Make new BFD section. All sections have the same name,
258 which is permitted by bfd_make_section_anyway(). */
259 asection *sec = bfd_make_section_anyway (bfd, "load");
261 error (_("Can't make section"));
262 sec->filepos = file_ofs;
263 bfd_set_section_flags (sec, SEC_READONLY | SEC_HAS_CONTENTS);
264 bfd_set_section_size (sec, end - start);
265 bfd_set_section_vma (sec, start);
266 bfd_set_section_lma (sec, start);
267 bfd_set_section_alignment (sec, 2);
269 /* Set target_section fields. */
270 m_core_file_mappings.emplace_back (start, end, sec);
273 normalize_mem_ranges (&m_core_unavailable_mappings);
276 /* An arbitrary identifier for the core inferior. */
277 #define CORELOW_PID 1
279 /* Close the core target. */
282 core_target::close ()
286 switch_to_no_thread (); /* Avoid confusion from thread
288 exit_inferior_silent (current_inferior ());
290 /* Clear out solib state while the bfd is still open. See
291 comments in clear_solib in solib.c. */
294 current_program_space->cbfd.reset (nullptr);
297 /* Core targets are heap-allocated (see core_target_open), so here
298 we delete ourselves. */
302 /* Look for sections whose names start with `.reg/' so that we can
303 extract the list of threads in a core file. */
306 add_to_thread_list (asection *asect, asection *reg_sect)
310 bool fake_pid_p = false;
311 struct inferior *inf;
313 if (!startswith (bfd_section_name (asect), ".reg/"))
316 core_tid = atoi (bfd_section_name (asect) + 5);
318 pid = bfd_core_file_pid (core_bfd);
327 inf = current_inferior ();
330 inferior_appeared (inf, pid);
331 inf->fake_pid_p = fake_pid_p;
334 ptid_t ptid (pid, lwpid);
336 thread_info *thr = add_thread (inf->process_target (), ptid);
338 /* Warning, Will Robinson, looking at BFD private data! */
341 && asect->filepos == reg_sect->filepos) /* Did we find .reg? */
342 switch_to_thread (thr); /* Yes, make it current. */
345 /* Issue a message saying we have no core to debug, if FROM_TTY. */
348 maybe_say_no_core_file_now (int from_tty)
351 printf_filtered (_("No core file now.\n"));
354 /* Backward compatibility with old way of specifying core files. */
357 core_file_command (const char *filename, int from_tty)
359 dont_repeat (); /* Either way, seems bogus. */
361 if (filename == NULL)
363 if (core_bfd != NULL)
365 target_detach (current_inferior (), from_tty);
366 gdb_assert (core_bfd == NULL);
369 maybe_say_no_core_file_now (from_tty);
372 core_target_open (filename, from_tty);
375 /* Locate (and load) an executable file (and symbols) given the core file
379 locate_exec_from_corefile_build_id (bfd *abfd, int from_tty)
381 const bfd_build_id *build_id = build_id_bfd_get (abfd);
382 if (build_id == nullptr)
385 gdb_bfd_ref_ptr execbfd
386 = build_id_to_exec_bfd (build_id->size, build_id->data);
388 if (execbfd != nullptr)
390 exec_file_attach (bfd_get_filename (execbfd.get ()), from_tty);
391 symbol_file_add_main (bfd_get_filename (execbfd.get ()),
392 symfile_add_flag (from_tty ? SYMFILE_VERBOSE : 0));
399 core_target_open (const char *arg, int from_tty)
406 target_preopen (from_tty);
410 error (_("No core file specified. (Use `detach' "
411 "to stop debugging a core file.)"));
413 error (_("No core file specified."));
416 gdb::unique_xmalloc_ptr<char> filename (tilde_expand (arg));
417 if (!IS_ABSOLUTE_PATH (filename.get ()))
418 filename = gdb_abspath (filename.get ());
420 flags = O_BINARY | O_LARGEFILE;
425 scratch_chan = gdb_open_cloexec (filename.get (), flags, 0);
426 if (scratch_chan < 0)
427 perror_with_name (filename.get ());
429 gdb_bfd_ref_ptr temp_bfd (gdb_bfd_fopen (filename.get (), gnutarget,
430 write_files ? FOPEN_RUB : FOPEN_RB,
432 if (temp_bfd == NULL)
433 perror_with_name (filename.get ());
435 if (!bfd_check_format (temp_bfd.get (), bfd_core))
437 /* Do it after the err msg */
438 /* FIXME: should be checking for errors from bfd_close (for one
439 thing, on error it does not free all the storage associated
441 error (_("\"%s\" is not a core dump: %s"),
442 filename.get (), bfd_errmsg (bfd_get_error ()));
445 current_program_space->cbfd = std::move (temp_bfd);
447 core_target *target = new core_target ();
449 /* Own the target until it is successfully pushed. */
450 target_ops_up target_holder (target);
454 /* If we have no exec file, try to set the architecture from the
455 core file. We don't do this unconditionally since an exec file
456 typically contains more information that helps us determine the
457 architecture than a core file. */
458 if (!current_program_space->exec_bfd ())
459 set_gdbarch_from_file (core_bfd);
461 current_inferior ()->push_target (std::move (target_holder));
463 switch_to_no_thread ();
465 /* Need to flush the register cache (and the frame cache) from a
466 previous debug session. If inferior_ptid ends up the same as the
467 last debug session --- e.g., b foo; run; gcore core1; step; gcore
468 core2; core core1; core core2 --- then there's potential for
469 get_current_regcache to return the cached regcache of the
470 previous session, and the frame cache being stale. */
471 registers_changed ();
473 /* Build up thread list from BFD sections, and possibly set the
474 current thread to the .reg/NN section matching the .reg
476 asection *reg_sect = bfd_get_section_by_name (core_bfd, ".reg");
477 for (asection *sect : gdb_bfd_sections (core_bfd))
478 add_to_thread_list (sect, reg_sect);
480 if (inferior_ptid == null_ptid)
482 /* Either we found no .reg/NN section, and hence we have a
483 non-threaded core (single-threaded, from gdb's perspective),
484 or for some reason add_to_thread_list couldn't determine
485 which was the "main" thread. The latter case shouldn't
486 usually happen, but we're dealing with input here, which can
487 always be broken in different ways. */
488 thread_info *thread = first_thread_of_inferior (current_inferior ());
492 inferior_appeared (current_inferior (), CORELOW_PID);
493 thread = add_thread_silent (target, ptid_t (CORELOW_PID));
496 switch_to_thread (thread);
499 if (current_program_space->exec_bfd () == nullptr)
500 locate_exec_from_corefile_build_id (core_bfd, from_tty);
502 post_create_inferior (from_tty);
504 /* Now go through the target stack looking for threads since there
505 may be a thread_stratum target loaded on top of target core by
506 now. The layer above should claim threads found in the BFD
510 target_update_thread_list ();
513 catch (const gdb_exception_error &except)
515 exception_print (gdb_stderr, except);
518 p = bfd_core_file_failing_command (core_bfd);
520 printf_filtered (_("Core was generated by `%s'.\n"), p);
522 /* Clearing any previous state of convenience variables. */
523 clear_exit_convenience_vars ();
525 siggy = bfd_core_file_failing_signal (core_bfd);
528 gdbarch *core_gdbarch = target->core_gdbarch ();
530 /* If we don't have a CORE_GDBARCH to work with, assume a native
531 core (map gdb_signal from host signals). If we do have
532 CORE_GDBARCH to work with, but no gdb_signal_from_target
533 implementation for that gdbarch, as a fallback measure,
534 assume the host signal mapping. It'll be correct for native
535 cores, but most likely incorrect for cross-cores. */
536 enum gdb_signal sig = (core_gdbarch != NULL
537 && gdbarch_gdb_signal_from_target_p (core_gdbarch)
538 ? gdbarch_gdb_signal_from_target (core_gdbarch,
540 : gdb_signal_from_host (siggy));
542 printf_filtered (_("Program terminated with signal %s, %s"),
543 gdb_signal_to_name (sig), gdb_signal_to_string (sig));
544 if (gdbarch_report_signal_info_p (core_gdbarch))
545 gdbarch_report_signal_info (core_gdbarch, current_uiout, sig);
546 printf_filtered (_(".\n"));
548 /* Set the value of the internal variable $_exitsignal,
549 which holds the signal uncaught by the inferior. */
550 set_internalvar_integer (lookup_internalvar ("_exitsignal"),
554 /* Fetch all registers from core file. */
555 target_fetch_registers (get_current_regcache (), -1);
557 /* Now, set up the frame cache, and print the top of stack. */
558 reinit_frame_cache ();
559 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
561 /* Current thread should be NUM 1 but the user does not know that.
562 If a program is single threaded gdb in general does not mention
563 anything about threads. That is why the test is >= 2. */
564 if (thread_count (target) >= 2)
568 thread_command (NULL, from_tty);
570 catch (const gdb_exception_error &except)
572 exception_print (gdb_stderr, except);
578 core_target::detach (inferior *inf, int from_tty)
580 /* Note that 'this' is dangling after this call. unpush_target
581 closes the target, and our close implementation deletes
583 inf->unpush_target (this);
585 /* Clear the register cache and the frame cache. */
586 registers_changed ();
587 reinit_frame_cache ();
588 maybe_say_no_core_file_now (from_tty);
591 /* Try to retrieve registers from a section in core_bfd, and supply
594 If ptid's lwp member is zero, do the single-threaded
595 thing: look for a section named NAME. If ptid's lwp
596 member is non-zero, do the multi-threaded thing: look for a section
597 named "NAME/LWP", where LWP is the shortest ASCII decimal
598 representation of ptid's lwp member.
600 HUMAN_NAME is a human-readable name for the kind of registers the
601 NAME section contains, for use in error messages.
603 If REQUIRED is true, print an error if the core file doesn't have a
604 section by the appropriate name. Otherwise, just do nothing. */
607 core_target::get_core_register_section (struct regcache *regcache,
608 const struct regset *regset,
610 int section_min_size,
611 const char *human_name,
614 gdb_assert (regset != nullptr);
616 struct bfd_section *section;
618 bool variable_size_section = (regset->flags & REGSET_VARIABLE_SIZE);
620 thread_section_name section_name (name, regcache->ptid ());
622 section = bfd_get_section_by_name (core_bfd, section_name.c_str ());
626 warning (_("Couldn't find %s registers in core file."),
631 size = bfd_section_size (section);
632 if (size < section_min_size)
634 warning (_("Section `%s' in core file too small."),
635 section_name.c_str ());
638 if (size != section_min_size && !variable_size_section)
640 warning (_("Unexpected size of section `%s' in core file."),
641 section_name.c_str ());
644 gdb::byte_vector contents (size);
645 if (!bfd_get_section_contents (core_bfd, section, contents.data (),
648 warning (_("Couldn't read %s registers from `%s' section in core file."),
649 human_name, section_name.c_str ());
653 regset->supply_regset (regset, regcache, -1, contents.data (), size);
656 /* Data passed to gdbarch_iterate_over_regset_sections's callback. */
657 struct get_core_registers_cb_data
660 struct regcache *regcache;
663 /* Callback for get_core_registers that handles a single core file
664 register note section. */
667 get_core_registers_cb (const char *sect_name, int supply_size, int collect_size,
668 const struct regset *regset,
669 const char *human_name, void *cb_data)
671 gdb_assert (regset != nullptr);
673 auto *data = (get_core_registers_cb_data *) cb_data;
674 bool required = false;
675 bool variable_size_section = (regset->flags & REGSET_VARIABLE_SIZE);
677 if (!variable_size_section)
678 gdb_assert (supply_size == collect_size);
680 if (strcmp (sect_name, ".reg") == 0)
683 if (human_name == NULL)
684 human_name = "general-purpose";
686 else if (strcmp (sect_name, ".reg2") == 0)
688 if (human_name == NULL)
689 human_name = "floating-point";
692 data->target->get_core_register_section (data->regcache, regset, sect_name,
693 supply_size, human_name, required);
696 /* Get the registers out of a core file. This is the machine-
697 independent part. Fetch_core_registers is the machine-dependent
698 part, typically implemented in the xm-file for each
701 /* We just get all the registers, so we don't use regno. */
704 core_target::fetch_registers (struct regcache *regcache, int regno)
706 if (!(m_core_gdbarch != nullptr
707 && gdbarch_iterate_over_regset_sections_p (m_core_gdbarch)))
709 fprintf_filtered (gdb_stderr,
710 "Can't fetch registers from this type of core file\n");
714 struct gdbarch *gdbarch = regcache->arch ();
715 get_core_registers_cb_data data = { this, regcache };
716 gdbarch_iterate_over_regset_sections (gdbarch,
717 get_core_registers_cb,
718 (void *) &data, NULL);
720 /* Mark all registers not found in the core as unavailable. */
721 for (int i = 0; i < gdbarch_num_regs (regcache->arch ()); i++)
722 if (regcache->get_register_status (i) == REG_UNKNOWN)
723 regcache->raw_supply (i, NULL);
727 core_target::files_info ()
729 print_section_info (&m_core_section_table, core_bfd);
732 /* Helper method for core_target::xfer_partial. */
734 enum target_xfer_status
735 core_target::xfer_memory_via_mappings (gdb_byte *readbuf,
736 const gdb_byte *writebuf,
737 ULONGEST offset, ULONGEST len,
738 ULONGEST *xfered_len)
740 enum target_xfer_status xfer_status;
742 xfer_status = (section_table_xfer_memory_partial
744 offset, len, xfered_len,
745 m_core_file_mappings));
747 if (xfer_status == TARGET_XFER_OK || m_core_unavailable_mappings.empty ())
750 /* There are instances - e.g. when debugging within a docker
751 container using the AUFS storage driver - where the pathnames
752 obtained from the note section are incorrect. Despite the path
753 being wrong, just knowing the start and end addresses of the
754 mappings is still useful; we can attempt an access of the file
755 stratum constrained to the address ranges corresponding to the
756 unavailable mappings. */
758 ULONGEST memaddr = offset;
759 ULONGEST memend = offset + len;
761 for (const auto &mr : m_core_unavailable_mappings)
763 if (address_in_mem_range (memaddr, &mr))
765 if (!address_in_mem_range (memend, &mr))
766 len = mr.start + mr.length - memaddr;
768 xfer_status = this->beneath ()->xfer_partial (TARGET_OBJECT_MEMORY,
782 enum target_xfer_status
783 core_target::xfer_partial (enum target_object object, const char *annex,
784 gdb_byte *readbuf, const gdb_byte *writebuf,
785 ULONGEST offset, ULONGEST len, ULONGEST *xfered_len)
789 case TARGET_OBJECT_MEMORY:
791 enum target_xfer_status xfer_status;
793 /* Try accessing memory contents from core file data,
794 restricting consideration to those sections for which
795 the BFD section flag SEC_HAS_CONTENTS is set. */
796 auto has_contents_cb = [] (const struct target_section *s)
798 return ((s->the_bfd_section->flags & SEC_HAS_CONTENTS) != 0);
800 xfer_status = section_table_xfer_memory_partial
802 offset, len, xfered_len,
803 m_core_section_table,
805 if (xfer_status == TARGET_XFER_OK)
806 return TARGET_XFER_OK;
808 /* Check file backed mappings. If they're available, use
809 core file provided mappings (e.g. from .note.linuxcore.file
810 or the like) as this should provide a more accurate
811 result. If not, check the stratum beneath us, which should
814 We also check unavailable mappings due to Docker/AUFS driver
816 if (!m_core_file_mappings.empty ()
817 || !m_core_unavailable_mappings.empty ())
819 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 the core file contains a target description note then we will use
1005 that in preference to anything else. */
1006 bfd_size_type tdesc_note_size = 0;
1007 struct bfd_section *tdesc_note_section
1008 = bfd_get_section_by_name (core_bfd, ".gdb-tdesc");
1009 if (tdesc_note_section != nullptr)
1010 tdesc_note_size = bfd_section_size (tdesc_note_section);
1011 if (tdesc_note_size > 0)
1013 gdb::char_vector contents (tdesc_note_size + 1);
1014 if (bfd_get_section_contents (core_bfd, tdesc_note_section,
1015 contents.data (), (file_ptr) 0,
1018 /* Ensure we have a null terminator. */
1019 contents[tdesc_note_size] = '\0';
1020 const struct target_desc *result
1021 = string_read_description_xml (contents.data ());
1022 if (result != nullptr)
1027 if (m_core_gdbarch && gdbarch_core_read_description_p (m_core_gdbarch))
1029 const struct target_desc *result;
1031 result = gdbarch_core_read_description (m_core_gdbarch, this, core_bfd);
1036 return this->beneath ()->read_description ();
1040 core_target::pid_to_str (ptid_t ptid)
1042 struct inferior *inf;
1045 /* The preferred way is to have a gdbarch/OS specific
1047 if (m_core_gdbarch != nullptr
1048 && gdbarch_core_pid_to_str_p (m_core_gdbarch))
1049 return gdbarch_core_pid_to_str (m_core_gdbarch, ptid);
1051 /* Otherwise, if we don't have one, we'll just fallback to
1052 "process", with normal_pid_to_str. */
1054 /* Try the LWPID field first. */
1057 return normal_pid_to_str (ptid_t (pid));
1059 /* Otherwise, this isn't a "threaded" core -- use the PID field, but
1060 only if it isn't a fake PID. */
1061 inf = find_inferior_ptid (this, ptid);
1062 if (inf != NULL && !inf->fake_pid_p)
1063 return normal_pid_to_str (ptid);
1065 /* No luck. We simply don't have a valid PID to print. */
1066 return "<main task>";
1070 core_target::thread_name (struct thread_info *thr)
1072 if (m_core_gdbarch != nullptr
1073 && gdbarch_core_thread_name_p (m_core_gdbarch))
1074 return gdbarch_core_thread_name (m_core_gdbarch, thr);
1079 core_target::has_memory ()
1081 return (core_bfd != NULL);
1085 core_target::has_stack ()
1087 return (core_bfd != NULL);
1091 core_target::has_registers ()
1093 return (core_bfd != NULL);
1096 /* Implement the to_info_proc method. */
1099 core_target::info_proc (const char *args, enum info_proc_what request)
1101 struct gdbarch *gdbarch = get_current_arch ();
1103 /* Since this is the core file target, call the 'core_info_proc'
1104 method on gdbarch, not 'info_proc'. */
1105 if (gdbarch_core_info_proc_p (gdbarch))
1106 gdbarch_core_info_proc (gdbarch, args, request);
1111 /* Get a pointer to the current core target. If not connected to a
1112 core target, return NULL. */
1114 static core_target *
1115 get_current_core_target ()
1117 target_ops *proc_target = current_inferior ()->process_target ();
1118 return dynamic_cast<core_target *> (proc_target);
1121 /* Display file backed mappings from core file. */
1124 core_target::info_proc_mappings (struct gdbarch *gdbarch)
1126 if (!m_core_file_mappings.empty ())
1128 printf_filtered (_("Mapped address spaces:\n\n"));
1129 if (gdbarch_addr_bit (gdbarch) == 32)
1131 printf_filtered ("\t%10s %10s %10s %10s %s\n",
1134 " Size", " Offset", "objfile");
1138 printf_filtered (" %18s %18s %10s %10s %s\n",
1141 " Size", " Offset", "objfile");
1145 for (const target_section &tsp : m_core_file_mappings)
1147 ULONGEST start = tsp.addr;
1148 ULONGEST end = tsp.endaddr;
1149 ULONGEST file_ofs = tsp.the_bfd_section->filepos;
1150 const char *filename = bfd_get_filename (tsp.the_bfd_section->owner);
1152 if (gdbarch_addr_bit (gdbarch) == 32)
1153 printf_filtered ("\t%10s %10s %10s %10s %s\n",
1154 paddress (gdbarch, start),
1155 paddress (gdbarch, end),
1156 hex_string (end - start),
1157 hex_string (file_ofs),
1160 printf_filtered (" %18s %18s %10s %10s %s\n",
1161 paddress (gdbarch, start),
1162 paddress (gdbarch, end),
1163 hex_string (end - start),
1164 hex_string (file_ofs),
1169 /* Implement "maintenance print core-file-backed-mappings" command.
1171 If mappings are loaded, the results should be similar to the
1172 mappings shown by "info proc mappings". This command is mainly a
1173 debugging tool for GDB developers to make sure that the expected
1174 mappings are present after loading a core file. For Linux, the
1175 output provided by this command will be very similar (if not
1176 identical) to that provided by "info proc mappings". This is not
1177 necessarily the case for other OSes which might provide
1178 more/different information in the "info proc mappings" output. */
1181 maintenance_print_core_file_backed_mappings (const char *args, int from_tty)
1183 core_target *targ = get_current_core_target ();
1184 if (targ != nullptr)
1185 targ->info_proc_mappings (targ->core_gdbarch ());
1188 void _initialize_corelow ();
1190 _initialize_corelow ()
1192 add_target (core_target_info, core_target_open, filename_completer);
1193 add_cmd ("core-file-backed-mappings", class_maintenance,
1194 maintenance_print_core_file_backed_mappings,
1195 _("Print core file's file-backed mappings."),
1196 &maintenanceprintlist);