38#include "readline/tilde.h"
46#include "gdbsupport/filestuff.h"
48#include "gdbsupport/pathstuff.h"
49#include "gdbsupport/scoped_fd.h"
50#include "gdbsupport/x86-xstate.h"
52#include <unordered_map>
53#include <unordered_set>
66 N_(
"Local core dump file"),
67 N_(
"Use a core file as a target.\n\
68Specify the filename of the core file.")
79 void close ()
override;
86 const gdb_byte *writebuf,
87 ULONGEST offset, ULONGEST len,
88 ULONGEST *xfered_len)
override;
111 gdb::byte_vector &tags,
int type)
override;
127 int section_min_size,
128 const char *human_name,
161 const gdb_byte *writebuf,
164 ULONGEST *xfered_len);
181 if (tdesc !=
nullptr)
185 info.target_desc = tdesc;
191 error (_(
"\"%s\": Core file format not supported"),
217 std::unordered_map<std::string, struct bfd *> bfd_map;
218 std::unordered_set<std::string> unavailable_paths;
232 [&] (
int num, ULONGEST start, ULONGEST end, ULONGEST file_ofs,
233 const char *filename,
const bfd_build_id *build_id)
237 gdb_assert (filename !=
nullptr);
239 if (unavailable_paths.find (filename) != unavailable_paths.end ())
249 struct bfd *bfd = bfd_map[filename];
256 gdb::unique_xmalloc_ptr<char> expanded_fname
259 if (expanded_fname ==
nullptr && build_id !=
nullptr)
261 filename, &expanded_fname);
263 if (expanded_fname ==
nullptr)
266 unavailable_paths.insert (filename);
267 warning (_(
"Can't open file %s during file-backed mapping "
273 bfd = bfd_openr (expanded_fname.get (),
"binary");
275 if (bfd ==
nullptr || !bfd_check_format (bfd, bfd_object))
278 unavailable_paths.insert (filename);
279 warning (_(
"Can't open file %s which was expanded to %s "
280 "during file-backed mapping note processing"),
281 filename, expanded_fname.get ());
291 bfd_map[filename] = bfd;
296 asection *sec = bfd_make_section_anyway (bfd,
"load");
298 error (_(
"Can't make section"));
299 sec->filepos = file_ofs;
300 bfd_set_section_flags (sec, SEC_READONLY | SEC_HAS_CONTENTS);
301 bfd_set_section_size (sec, end - start);
302 bfd_set_section_vma (sec, start);
303 bfd_set_section_lma (sec, start);
304 bfd_set_section_alignment (sec, 2);
311 if (build_id !=
nullptr)
313 gdb::unique_xmalloc_ptr<char> soname
315 if (soname !=
nullptr)
317 soname.get (), build_id);
369 if (!startswith (bfd_section_name (asect),
".reg/"))
372 int lwpid = atoi (bfd_section_name (asect) + 5);
373 ptid_t ptid (
inf->
pid, lwpid);
379 && asect->filepos == reg_sect->filepos)
399 if (filename == NULL)
459 std::vector<std::pair<asection *, int>> sections_and_lwpids;
463 std::unordered_set<int> all_lwpids;
466 unsigned zero_lwpid_count = 0;
473 if (startswith (bfd_section_name (sect),
".reg/"))
475 int lwpid = atoi (bfd_section_name (sect) + 5);
476 sections_and_lwpids.emplace_back (sect, lwpid);
477 all_lwpids.insert (lwpid);
485 if (zero_lwpid_count == 0)
490 for (
auto §_and_lwpid : sections_and_lwpids)
491 if (sect_and_lwpid.second == 0)
493 while (all_lwpids.find (new_lwpid) != all_lwpids.end ())
495 sect_and_lwpid.second = new_lwpid;
501 std::string replacement_lwpid_str;
502 auto iter = sections_and_lwpids.begin ();
503 int replacement_lwpid = 0;
506 if (iter != sections_and_lwpids.end () && sect == iter->first)
508 gdb_assert (startswith (bfd_section_name (sect),
".reg/"));
510 int lwpid = atoi (bfd_section_name (sect) + 5);
511 if (lwpid == iter->second)
514 gdb_assert (lwpid != 0);
515 replacement_lwpid = 0;
519 replacement_lwpid = iter->second;
520 ptid_t ptid (
inf->
pid, replacement_lwpid);
521 if (!replacement_lwpid_str.empty ())
522 replacement_lwpid_str +=
", ";
529 if (replacement_lwpid != 0)
531 const char *
name = bfd_section_name (sect);
532 size_t len = strlen (
name);
534 if (strncmp (
name + len - 2,
"/0", 2) == 0)
538 = string_printf (
"%.*s/%d",
539 static_cast<int> (len - 2),
540 name, replacement_lwpid);
542 =
static_cast<char *
> (bfd_alloc (abfd, name_str.size () + 1));
543 if (name_buf ==
nullptr)
544 error (_(
"failed to allocate space for section name '%s'"),
546 memcpy (name_buf, name_str.c_str(), name_str.size () + 1);
547 bfd_rename_section (sect, name_buf);
552 if (zero_lwpid_count == 1)
553 warning (_(
"found thread with pid 0, assigned replacement Target Id: %s"),
554 replacement_lwpid_str.c_str ());
556 warning (_(
"found threads with pid 0, assigned replacement Target Ids: %s"),
557 replacement_lwpid_str.c_str ());
567 if (build_id ==
nullptr)
573 if (execbfd ==
nullptr)
576 gdb::unique_xmalloc_ptr<char> execpath;
578 abfd->filename, &execpath);
584 if (execbfd ==
nullptr)
585 warning (_(
"\"%s\" from debuginfod cannot be opened as bfd: %s"),
590 execbfd.reset (
nullptr);
594 if (execbfd !=
nullptr)
616 error (_(
"No core file specified. (Use `detach' "
617 "to stop debugging a core file.)"));
619 error (_(
"No core file specified."));
622 gdb::unique_xmalloc_ptr<char> filename (tilde_expand (arg));
623 if (strlen (filename.get ()) != 0
624 && !IS_ABSOLUTE_PATH (filename.get ()))
625 filename = make_unique_xstrdup (gdb_abspath (filename.get ()).c_str ());
632 scratch_chan = gdb_open_cloexec (filename.get (),
flags, 0).release ();
633 if (scratch_chan < 0)
634 perror_with_name (filename.get ());
639 if (temp_bfd == NULL)
640 perror_with_name (filename.get ());
642 if (!bfd_check_format (temp_bfd.get (), bfd_core))
648 error (_(
"\"%s\" is not a core dump: %s"),
649 filename.get (), bfd_errmsg (bfd_get_error ()));
682 bool fake_pid_p =
false;
691 gdb_assert (
inf->
pid == 0);
693 inf->fake_pid_p = fake_pid_p;
701 asection *reg_sect = bfd_get_section_by_name (
core_bfd,
".reg");
735 catch (
const gdb_exception_error &except)
740 p = bfd_core_file_failing_command (
core_bfd);
742 gdb_printf (_(
"Core was generated by `%s'.\n"), p);
747 siggy = bfd_core_file_failing_signal (
core_bfd);
758 enum gdb_signal sig = (core_gdbarch != NULL
762 : gdb_signal_from_host (siggy));
764 gdb_printf (_(
"Program terminated with signal %s, %s"),
765 gdb_signal_to_name (sig), gdb_signal_to_string (sig));
792 catch (
const gdb_exception_error &except)
811 inf->unpush_target (
this);
838 int section_min_size,
839 const char *human_name,
842 gdb_assert (
regset !=
nullptr);
844 struct bfd_section *section;
850 section = bfd_get_section_by_name (
core_bfd, section_name.
c_str ());
854 warning (_(
"Couldn't find %s registers in core file."),
859 size = bfd_section_size (section);
860 if (
size < section_min_size)
862 warning (_(
"Section `%s' in core file too small."),
863 section_name.
c_str ());
866 if (
size != section_min_size && !variable_size_section)
868 warning (_(
"Unexpected size of section `%s' in core file."),
869 section_name.
c_str ());
872 gdb::byte_vector contents (
size);
873 if (!bfd_get_section_contents (
core_bfd, section, contents.data (),
876 warning (_(
"Couldn't read %s registers from `%s' section in core file."),
877 human_name, section_name.
c_str ());
897 const char *human_name,
void *cb_data)
899 gdb_assert (
regset !=
nullptr);
902 bool required =
false;
905 if (!variable_size_section)
906 gdb_assert (supply_size == collect_size);
908 if (strcmp (sect_name,
".reg") == 0)
911 if (human_name == NULL)
912 human_name =
"general-purpose";
914 else if (strcmp (sect_name,
".reg2") == 0)
916 if (human_name == NULL)
917 human_name =
"floating-point";
920 data->target->get_core_register_section (data->regcache,
regset, sect_name,
921 supply_size, human_name, required);
938 "Can't fetch registers from this type of core file\n");
946 (
void *) &data, NULL);
964 const gdb_byte *writebuf,
965 ULONGEST offset, ULONGEST len,
966 ULONGEST *xfered_len)
972 offset, len, xfered_len,
986 ULONGEST memaddr = offset;
987 ULONGEST memend = offset + len;
994 len = mr.start + mr.length - memaddr;
1012 gdb_byte *readbuf,
const gdb_byte *writebuf,
1013 ULONGEST offset, ULONGEST len, ULONGEST *xfered_len)
1026 return ((s->the_bfd_section->flags & SEC_HAS_CONTENTS) != 0);
1030 offset, len, xfered_len,
1052 writebuf, offset, len,
1061 return !has_contents_cb (s);
1065 offset, len, xfered_len,
1077 struct bfd_section *section;
1080 section = bfd_get_section_by_name (
core_bfd,
".auxv");
1081 if (section == NULL)
1084 size = bfd_section_size (section);
1093 if (!bfd_get_section_contents (
core_bfd, section, readbuf,
1094 (file_ptr) offset,
size))
1096 warning (_(
"Couldn't read NT_AUXV note in core file."));
1100 *xfered_len = (ULONGEST)
size;
1112 struct bfd_section *section;
1115 section = bfd_get_section_by_name (
core_bfd,
".wcookie");
1116 if (section == NULL)
1119 size = bfd_section_size (section);
1128 if (!bfd_get_section_contents (
core_bfd, section, readbuf,
1129 (file_ptr) offset,
size))
1131 warning (_(
"Couldn't read StackGhost cookie in core file."));
1135 *xfered_len = (ULONGEST)
size;
1153 if (*xfered_len == 0)
1174 if (*xfered_len == 0)
1205 writebuf, offset, len,
1239 bfd_size_type tdesc_note_size = 0;
1240 struct bfd_section *tdesc_note_section
1241 = bfd_get_section_by_name (
core_bfd,
".gdb-tdesc");
1242 if (tdesc_note_section !=
nullptr)
1243 tdesc_note_size = bfd_section_size (tdesc_note_section);
1244 if (tdesc_note_size > 0)
1246 gdb::char_vector contents (tdesc_note_size + 1);
1247 if (bfd_get_section_contents (
core_bfd, tdesc_note_section,
1248 contents.data (), (file_ptr) 0,
1252 contents[tdesc_note_size] =
'\0';
1255 if (result !=
nullptr)
1272 if (result !=
nullptr)
1302 if (
inf != NULL && !
inf->fake_pid_p)
1306 return "<main task>";
1359 return (bfd_get_section_by_name (
core_bfd,
"memtag") !=
nullptr);
1366 gdb::byte_vector &tags,
int type)
1372 error (_(
"gdbarch_decode_memtag_section not implemented for this "
1376 info.memtag_section =
nullptr;
1381 size_t adjusted_length
1382 = (address + len <
info.end_address) ? len : (
info.end_address - address);
1385 gdb::byte_vector tags_read
1387 address, adjusted_length);
1390 tags.insert (tags.end (), tags_read.begin (), tags_read.end ());
1394 if (!tags_read.empty () && (address + len) <
info.end_address)
1398 len -= (
info.end_address - address);
1399 address =
info.end_address;
1413 x86_xsave_layout layout;
1440 gdb_printf (_(
"Mapped address spaces:\n\n"));
1446 " Size",
" Offset",
"objfile");
1453 " Size",
" Offset",
"objfile");
1459 ULONGEST start = tsp.addr;
1460 ULONGEST end = tsp.endaddr;
1461 ULONGEST file_ofs = tsp.the_bfd_section->filepos;
1462 const char *filename = bfd_get_filename (tsp.the_bfd_section->owner);
1468 hex_string (end - start),
1469 hex_string (file_ofs),
1475 hex_string (end - start),
1476 hex_string (file_ofs),
1496 if (targ !=
nullptr)
1507 _(
"Print core file's file-backed mappings."),
struct gdbarch * gdbarch_from_bfd(bfd *abfd)
struct gdbarch * get_current_arch(void)
void set_gdbarch_from_file(bfd *abfd)
struct gdbarch * target_gdbarch(void)
struct gdbarch * gdbarch_find_by_info(struct gdbarch_info info)
int build_id_verify(bfd *abfd, size_t check_len, const bfd_byte *check)
gdb_bfd_ref_ptr build_id_to_exec_bfd(size_t build_id_len, const bfd_byte *build_id)
const struct bfd_build_id * build_id_bfd_get(bfd *abfd)
std::string pid_to_str(ptid_t) override
bool has_all_memory() override
bool supports_memory_tagging() override
void build_file_mappings()
void fetch_registers(struct regcache *, int) override
enum target_xfer_status xfer_partial(enum target_object object, const char *annex, gdb_byte *readbuf, const gdb_byte *writebuf, ULONGEST offset, ULONGEST len, ULONGEST *xfered_len) override
void get_core_register_section(struct regcache *regcache, const struct regset *regset, const char *name, int section_min_size, const char *human_name, bool required)
bool has_stack() override
void detach(inferior *, int) override
bool thread_alive(ptid_t ptid) override
bool has_execution(inferior *inf) override
const struct target_desc * read_description() override
enum target_xfer_status xfer_memory_via_mappings(gdb_byte *readbuf, const gdb_byte *writebuf, ULONGEST offset, ULONGEST len, ULONGEST *xfered_len)
target_section_table m_core_file_mappings
const target_info & info() const override
bool fetch_memtags(CORE_ADDR address, size_t len, gdb::byte_vector &tags, int type) override
void files_info() override
x86_xsave_layout fetch_x86_xsave_layout() override
const char * thread_name(struct thread_info *) override
bool has_registers() override
void info_proc_mappings(struct gdbarch *gdbarch)
target_section_table m_core_section_table
std::vector< mem_range > m_core_unavailable_mappings
bool info_proc(const char *, enum info_proc_what) override
struct gdbarch * m_core_gdbarch
struct gdbarch * core_gdbarch()
bool has_memory() override
void push_target(struct target_ops *t)
struct process_stratum_target * process_target()
const std::string & args() const
void raw_supply(int regnum, const void *buf) override
enum register_status get_register_status(int regnum) const override
const char * c_str() const
struct cmd_list_element * maintenanceprintlist
struct cmd_list_element * add_cmd(const char *name, enum command_class theclass, const char *doc, struct cmd_list_element **list)
void filename_completer(struct cmd_list_element *ignore, completion_tracker &tracker, const char *text, const char *word)
void validate_files(void)
void _initialize_corelow()
static void get_core_registers_cb(const char *sect_name, int supply_size, int collect_size, const struct regset *regset, const char *human_name, void *cb_data)
static void rename_vmcore_idle_reg_sections(bfd *abfd, inferior *inf)
static const target_info core_target_info
static void maybe_say_no_core_file_now(int from_tty)
void core_file_command(const char *filename, int from_tty)
void core_target_open(const char *arg, int from_tty)
static void locate_exec_from_corefile_build_id(bfd *abfd, int from_tty)
static core_target * get_current_core_target()
static void add_to_thread_list(asection *asect, asection *reg_sect, inferior *inf)
static void maintenance_print_core_file_backed_mappings(const char *args, int from_tty)
scoped_fd debuginfod_exec_query(const unsigned char *build_id, int build_id_len, const char *filename, gdb::unique_xmalloc_ptr< char > *destname)
void exception_print(struct ui_file *file, const struct gdb_exception &e)
enum target_xfer_status section_table_xfer_memory_partial(gdb_byte *readbuf, const gdb_byte *writebuf, ULONGEST offset, ULONGEST len, ULONGEST *xfered_len, const target_section_table §ions, gdb::function_view< bool(const struct target_section *)> match_cb)
target_section_table build_section_table(struct bfd *some_bfd)
void exec_file_attach(const char *filename, int from_tty)
void print_section_info(const target_section_table *t, bfd *abfd)
void reinit_frame_cache(void)
frame_info_ptr get_selected_frame(const char *message)
void print_stack_frame(frame_info_ptr, int print_level, enum print_what print_what, int set_current_sal)
gdb_bfd_ref_ptr gdb_bfd_fopen(const char *filename, const char *target, const char *mode, int fd)
std::string gdb_bfd_errmsg(bfd_error_type error_tag, char **matching)
void gdb_bfd_record_inclusion(bfd *includer, bfd *includee)
gdb_bfd_ref_ptr gdb_bfd_open(const char *name, const char *target, int fd, bool warn_if_slow)
gdb::ref_ptr< struct bfd, gdb_bfd_ref_policy > gdb_bfd_ref_ptr
static gdb_bfd_section_range gdb_bfd_sections(bfd *abfd)
LONGEST gdbarch_core_xfer_siginfo(struct gdbarch *gdbarch, gdb_byte *readbuf, ULONGEST offset, ULONGEST len)
bool gdbarch_core_info_proc_p(struct gdbarch *gdbarch)
ULONGEST gdbarch_core_xfer_shared_libraries_aix(struct gdbarch *gdbarch, gdb_byte *readbuf, ULONGEST offset, ULONGEST len)
void gdbarch_iterate_over_regset_sections(struct gdbarch *gdbarch, iterate_over_regset_sections_cb *cb, void *cb_data, const struct regcache *regcache)
bool gdbarch_decode_memtag_section_p(struct gdbarch *gdbarch)
const char * gdbarch_core_thread_name(struct gdbarch *gdbarch, struct thread_info *thr)
int gdbarch_addr_bit(struct gdbarch *gdbarch)
const struct target_desc * gdbarch_core_read_description(struct gdbarch *gdbarch, struct target_ops *target, bfd *abfd)
bool gdbarch_core_read_x86_xsave_layout_p(struct gdbarch *gdbarch)
ULONGEST gdbarch_core_xfer_shared_libraries(struct gdbarch *gdbarch, gdb_byte *readbuf, ULONGEST offset, ULONGEST len)
int gdbarch_num_regs(struct gdbarch *gdbarch)
bool gdbarch_core_thread_name_p(struct gdbarch *gdbarch)
bool gdbarch_use_target_description_from_corefile_notes(struct gdbarch *gdbarch, struct bfd *corefile_bfd)
bool gdbarch_core_read_description_p(struct gdbarch *gdbarch)
void gdbarch_report_signal_info(struct gdbarch *gdbarch, struct ui_out *uiout, enum gdb_signal siggnal)
bool gdbarch_report_signal_info_p(struct gdbarch *gdbarch)
bool gdbarch_gdb_signal_from_target_p(struct gdbarch *gdbarch)
bool gdbarch_core_xfer_siginfo_p(struct gdbarch *gdbarch)
enum gdb_signal gdbarch_gdb_signal_from_target(struct gdbarch *gdbarch, int signo)
bool gdbarch_core_xfer_shared_libraries_p(struct gdbarch *gdbarch)
bool gdbarch_iterate_over_regset_sections_p(struct gdbarch *gdbarch)
void gdbarch_core_info_proc(struct gdbarch *gdbarch, const char *args, enum info_proc_what what)
bool gdbarch_core_read_x86_xsave_layout(struct gdbarch *gdbarch, x86_xsave_layout &xsave_layout)
bool gdbarch_core_xfer_shared_libraries_aix_p(struct gdbarch *gdbarch)
bool gdbarch_core_pid_to_str_p(struct gdbarch *gdbarch)
void gdbarch_read_core_file_mappings(struct gdbarch *gdbarch, struct bfd *cbfd, read_core_file_mappings_pre_loop_ftype pre_loop_cb, read_core_file_mappings_loop_ftype loop_cb)
gdb::byte_vector gdbarch_decode_memtag_section(struct gdbarch *gdbarch, bfd_section *section, int type, CORE_ADDR address, size_t length)
std::string gdbarch_core_pid_to_str(struct gdbarch *gdbarch, ptid_t ptid)
struct thread_info * add_thread(process_stratum_target *targ, ptid_t ptid)
int thread_count(process_stratum_target *proc_target)
struct thread_info * add_thread_silent(process_stratum_target *targ, ptid_t ptid)
struct thread_info * first_thread_of_inferior(inferior *inf)
void switch_to_thread(struct thread_info *thr)
void thread_command(const char *tidstr, int from_tty)
void switch_to_no_thread()
mach_port_t kern_return_t mach_port_t mach_msg_type_name_t msgportsPoly mach_port_t kern_return_t pid_t pid mach_port_t kern_return_t mach_port_t task mach_port_t kern_return_t int flags
mach_port_t mach_port_t name mach_port_t mach_port_t name kern_return_t int int rusage_t pid_t pid
void post_create_inferior(int from_tty)
void inferior_appeared(struct inferior *inf, int pid)
struct inferior * find_inferior_ptid(process_stratum_target *targ, ptid_t ptid)
struct inferior * current_inferior(void)
void exit_inferior(struct inferior *inf)
void clear_exit_convenience_vars(void)
int address_in_mem_range(CORE_ADDR address, const struct mem_range *r)
void normalize_mem_ranges(std::vector< mem_range > *memory)
bool get_next_core_memtag_section(bfd *abfd, asection *section, CORE_ADDR address, memtag_section_info &info)
struct program_space * current_program_space
struct regcache * get_current_regcache(void)
void registers_changed(void)
#define REGSET_VARIABLE_SIZE
void set_cbfd_soname_build_id(gdb_bfd_ref_ptr abfd, const char *soname, const bfd_build_id *build_id)
gdb::unique_xmalloc_ptr< char > exec_file_find(const char *in_pathname, int *fd)
gdb::unique_xmalloc_ptr< char > gdb_bfd_read_elf_soname(const char *filename)
struct regcache * regcache
supply_regset_ftype * supply_regset
target_ops * beneath() const
virtual enum target_xfer_status xfer_partial(enum target_object object, const char *annex, gdb_byte *readbuf, const gdb_byte *writebuf, ULONGEST offset, ULONGEST len, ULONGEST *xfered_len) TARGET_DEFAULT_RETURN(TARGET_XFER_E_IO)
virtual const struct target_desc * read_description() TARGET_DEFAULT_RETURN(NULL)
void symbol_file_add_main(const char *args, symfile_add_flags add_flags)
std::vector< target_section > target_section_table
void target_detach(inferior *inf, int from_tty)
void target_fetch_registers(struct regcache *regcache, int regno)
void target_update_thread_list(void)
void add_target(const target_info &t, target_open_ftype *func, completer_ftype *completer)
std::string target_pid_to_str(ptid_t ptid)
std::string normal_pid_to_str(ptid_t ptid)
void target_preopen(int from_tty)
@ TARGET_OBJECT_LIBRARIES_AIX
@ TARGET_OBJECT_SIGNAL_INFO
@ TARGET_OBJECT_LIBRARIES
std::unique_ptr< target_ops, target_ops_deleter > target_ops_up
const char * paddress(struct gdbarch *gdbarch, CORE_ADDR addr)
void gdb_printf(struct ui_file *stream, const char *format,...)
struct internalvar * lookup_internalvar(const char *name)
void set_internalvar_integer(struct internalvar *var, LONGEST l)
const struct target_desc * string_read_description_xml(const char *xml)