38#include "readline/tilde.h"
46#include "gdbsupport/filestuff.h"
48#include "gdbsupport/pathstuff.h"
49#include "gdbsupport/scoped_fd.h"
51#include <unordered_map>
52#include <unordered_set>
65 N_(
"Local core dump file"),
66 N_(
"Use a core file as a target.\n\
67Specify the filename of the core file.")
78 void close ()
override;
85 const gdb_byte *writebuf,
86 ULONGEST offset, ULONGEST len,
87 ULONGEST *xfered_len)
override;
110 gdb::byte_vector &tags,
int type)
override;
124 int section_min_size,
125 const char *human_name,
158 const gdb_byte *writebuf,
161 ULONGEST *xfered_len);
178 if (tdesc !=
nullptr)
182 info.target_desc = tdesc;
188 error (_(
"\"%s\": Core file format not supported"),
214 std::unordered_map<std::string, struct bfd *> bfd_map;
215 std::unordered_set<std::string> unavailable_paths;
229 [&] (
int num, ULONGEST start, ULONGEST end, ULONGEST file_ofs,
230 const char *filename,
const bfd_build_id *build_id)
234 gdb_assert (filename !=
nullptr);
236 struct bfd *bfd = bfd_map[filename];
243 gdb::unique_xmalloc_ptr<char> expanded_fname
246 if (expanded_fname ==
nullptr && build_id !=
nullptr)
248 filename, &expanded_fname);
250 if (expanded_fname ==
nullptr)
254 if (unavailable_paths.insert (filename).second)
255 warning (_(
"Can't open file %s during file-backed mapping "
261 bfd = bfd_map[filename] = bfd_openr (expanded_fname.get (),
264 if (bfd ==
nullptr || !bfd_check_format (bfd, bfd_object))
276 warning (_(
"Can't open file %s which was expanded to %s "
277 "during file-backed mapping note processing"),
278 filename, expanded_fname.get ());
291 asection *sec = bfd_make_section_anyway (bfd,
"load");
293 error (_(
"Can't make section"));
294 sec->filepos = file_ofs;
295 bfd_set_section_flags (sec, SEC_READONLY | SEC_HAS_CONTENTS);
296 bfd_set_section_size (sec, end - start);
297 bfd_set_section_vma (sec, start);
298 bfd_set_section_lma (sec, start);
299 bfd_set_section_alignment (sec, 2);
306 if (build_id !=
nullptr)
308 gdb::unique_xmalloc_ptr<char> soname
310 if (soname !=
nullptr)
312 soname.get (), build_id);
359 bool fake_pid_p =
false;
362 if (!startswith (bfd_section_name (asect),
".reg/"))
365 core_tid = atoi (bfd_section_name (asect) + 5);
383 ptid_t ptid (
pid, lwpid);
390 && asect->filepos == reg_sect->filepos)
410 if (filename == NULL)
431 if (build_id ==
nullptr)
437 if (execbfd ==
nullptr)
440 gdb::unique_xmalloc_ptr<char> execpath;
442 abfd->filename, &execpath);
448 if (execbfd ==
nullptr)
449 warning (_(
"\"%s\" from debuginfod cannot be opened as bfd: %s"),
454 execbfd.reset (
nullptr);
458 if (execbfd !=
nullptr)
480 error (_(
"No core file specified. (Use `detach' "
481 "to stop debugging a core file.)"));
483 error (_(
"No core file specified."));
486 gdb::unique_xmalloc_ptr<char> filename (tilde_expand (arg));
487 if (strlen (filename.get ()) != 0
488 && !IS_ABSOLUTE_PATH (filename.get ()))
489 filename = make_unique_xstrdup (gdb_abspath (filename.get ()).c_str ());
496 scratch_chan = gdb_open_cloexec (filename.get (),
flags, 0).release ();
497 if (scratch_chan < 0)
503 if (temp_bfd == NULL)
506 if (!bfd_check_format (temp_bfd.get (), bfd_core))
512 error (_(
"\"%s\" is not a core dump: %s"),
513 filename.get (), bfd_errmsg (bfd_get_error ()));
547 asection *reg_sect = bfd_get_section_by_name (
core_bfd,
".reg");
584 catch (
const gdb_exception_error &except)
589 p = bfd_core_file_failing_command (
core_bfd);
591 gdb_printf (_(
"Core was generated by `%s'.\n"), p);
596 siggy = bfd_core_file_failing_signal (
core_bfd);
607 enum gdb_signal sig = (core_gdbarch != NULL
611 : gdb_signal_from_host (siggy));
613 gdb_printf (_(
"Program terminated with signal %s, %s"),
614 gdb_signal_to_name (sig), gdb_signal_to_string (sig));
641 catch (
const gdb_exception_error &except)
660 inf->unpush_target (
this);
687 int section_min_size,
688 const char *human_name,
691 gdb_assert (
regset !=
nullptr);
693 struct bfd_section *section;
699 section = bfd_get_section_by_name (
core_bfd, section_name.
c_str ());
703 warning (_(
"Couldn't find %s registers in core file."),
708 size = bfd_section_size (section);
709 if (
size < section_min_size)
711 warning (_(
"Section `%s' in core file too small."),
712 section_name.
c_str ());
715 if (
size != section_min_size && !variable_size_section)
717 warning (_(
"Unexpected size of section `%s' in core file."),
718 section_name.
c_str ());
721 gdb::byte_vector contents (
size);
722 if (!bfd_get_section_contents (
core_bfd, section, contents.data (),
725 warning (_(
"Couldn't read %s registers from `%s' section in core file."),
726 human_name, section_name.
c_str ());
746 const char *human_name,
void *cb_data)
748 gdb_assert (
regset !=
nullptr);
751 bool required =
false;
754 if (!variable_size_section)
755 gdb_assert (supply_size == collect_size);
757 if (strcmp (sect_name,
".reg") == 0)
760 if (human_name == NULL)
761 human_name =
"general-purpose";
763 else if (strcmp (sect_name,
".reg2") == 0)
765 if (human_name == NULL)
766 human_name =
"floating-point";
769 data->target->get_core_register_section (data->regcache,
regset, sect_name,
770 supply_size, human_name, required);
787 "Can't fetch registers from this type of core file\n");
795 (
void *) &data, NULL);
813 const gdb_byte *writebuf,
814 ULONGEST offset, ULONGEST len,
815 ULONGEST *xfered_len)
821 offset, len, xfered_len,
835 ULONGEST memaddr = offset;
836 ULONGEST memend = offset + len;
843 len = mr.start + mr.length - memaddr;
861 gdb_byte *readbuf,
const gdb_byte *writebuf,
862 ULONGEST offset, ULONGEST len, ULONGEST *xfered_len)
875 return ((s->the_bfd_section->flags & SEC_HAS_CONTENTS) != 0);
879 offset, len, xfered_len,
901 writebuf, offset, len,
910 return !has_contents_cb (s);
914 offset, len, xfered_len,
926 struct bfd_section *section;
929 section = bfd_get_section_by_name (
core_bfd,
".auxv");
933 size = bfd_section_size (section);
942 if (!bfd_get_section_contents (
core_bfd, section, readbuf,
943 (file_ptr) offset,
size))
945 warning (_(
"Couldn't read NT_AUXV note in core file."));
949 *xfered_len = (ULONGEST)
size;
961 struct bfd_section *section;
964 section = bfd_get_section_by_name (
core_bfd,
".wcookie");
968 size = bfd_section_size (section);
977 if (!bfd_get_section_contents (
core_bfd, section, readbuf,
978 (file_ptr) offset,
size))
980 warning (_(
"Couldn't read StackGhost cookie in core file."));
984 *xfered_len = (ULONGEST)
size;
1002 if (*xfered_len == 0)
1023 if (*xfered_len == 0)
1054 writebuf, offset, len,
1083 bfd_size_type tdesc_note_size = 0;
1084 struct bfd_section *tdesc_note_section
1085 = bfd_get_section_by_name (
core_bfd,
".gdb-tdesc");
1086 if (tdesc_note_section !=
nullptr)
1087 tdesc_note_size = bfd_section_size (tdesc_note_section);
1088 if (tdesc_note_size > 0)
1090 gdb::char_vector contents (tdesc_note_size + 1);
1091 if (bfd_get_section_contents (
core_bfd, tdesc_note_section,
1092 contents.data (), (file_ptr) 0,
1096 contents[tdesc_note_size] =
'\0';
1099 if (result !=
nullptr)
1139 if (
inf != NULL && !
inf->fake_pid_p)
1143 return "<main task>";
1196 return (bfd_get_section_by_name (
core_bfd,
"memtag") !=
nullptr);
1203 gdb::byte_vector &tags,
int type)
1209 error (_(
"gdbarch_decode_memtag_section not implemented for this "
1213 info.memtag_section =
nullptr;
1218 size_t adjusted_length
1219 = (address + len <
info.end_address) ? len : (
info.end_address - address);
1222 gdb::byte_vector tags_read
1224 address, adjusted_length);
1227 tags.insert (tags.end (), tags_read.begin (), tags_read.end ());
1231 if (!tags_read.empty () && (address + len) <
info.end_address)
1235 len -= (
info.end_address - address);
1236 address =
info.end_address;
1259 gdb_printf (_(
"Mapped address spaces:\n\n"));
1265 " Size",
" Offset",
"objfile");
1272 " Size",
" Offset",
"objfile");
1278 ULONGEST start = tsp.addr;
1279 ULONGEST end = tsp.endaddr;
1280 ULONGEST file_ofs = tsp.the_bfd_section->filepos;
1281 const char *filename = bfd_get_filename (tsp.the_bfd_section->owner);
1287 hex_string (end - start),
1288 hex_string (file_ofs),
1294 hex_string (end - start),
1295 hex_string (file_ofs),
1315 if (targ !=
nullptr)
1326 _(
"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
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)
static void add_to_thread_list(asection *asect, asection *reg_sect)
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 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 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)
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_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_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_silent(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 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
void perror_with_name(const char *string)
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)