36#include "readline/tilde.h"
38#include "gdbsupport/gdb_unlinker.h"
39#include "gdbsupport/byte-vector.h"
40#include "gdbsupport/scope-exit.h"
45#define MAX_COPY_BYTES (1024 * 1024)
60 error (_(
"Failed to open '%s' for output."), filename);
61 bfd_set_format (
obfd.get (), bfd_core);
71 gdb::unique_xmalloc_ptr<char> note_data;
73 asection *note_sec = NULL;
86 error (_(
"Target does not support core file generation."));
89 note_sec = bfd_make_section_anyway_with_flags (
obfd,
"note0",
94 error (_(
"Failed to create 'note' section for corefile: %s"),
95 bfd_errmsg (bfd_get_error ()));
97 bfd_set_section_vma (note_sec, 0);
98 bfd_set_section_alignment (note_sec, 0);
99 bfd_set_section_size (note_sec,
note_size);
103 error (_(
"gcore: failed to get corefile memory sections from target."));
106 if (!bfd_set_section_contents (
obfd, note_sec, note_data.get (), 0,
108 warning (_(
"writing note section (%s)"), bfd_errmsg (bfd_get_error ()));
128 gdb::unique_xmalloc_ptr<char> corefilename;
135 corefilename.reset (tilde_expand (args));
143 gdb_printf (
"Opening corefile '%s' for output.\n",
144 corefilename.get ());
154 gdb::unlinker unlink_file (corefilename.get ());
163 gdb_printf (
"Saved corefile %s\n", corefilename.get ());
166static enum bfd_architecture
172 return bfdarch->arch;
174 error (_(
"Can't find bfd architecture for corefile (need execfile)."));
251 struct value *target_sbrk_arg;
252 struct value *sbrk_fn, *ret;
273 gdb_assert (target_sbrk_arg);
279 if ((LONGEST) tmp <= 0 || (LONGEST) tmp == 0xffffffff)
293 bfd_vma top_of_data_memory = 0;
294 bfd_vma top_of_heap = 0;
295 bfd_size_type sec_size;
318 for (sec = abfd->sections; sec; sec = sec->next)
320 if (bfd_section_flags (sec) & SEC_DATA
321 || strcmp (
".bss", bfd_section_name (sec)) == 0)
323 sec_vaddr = bfd_section_vma (sec);
324 sec_size = bfd_section_size (sec);
325 if (sec_vaddr + sec_size > top_of_data_memory)
326 top_of_data_memory = sec_vaddr + sec_size;
331 if (top_of_heap == (bfd_vma) 0)
335 if (top_of_heap > top_of_data_memory)
337 *bottom = top_of_data_memory;
355 if (startswith (bfd_section_name (osec),
"memtag"))
359 if (startswith (bfd_section_name (osec),
"load"))
361 else if (startswith (bfd_section_name (osec),
"note"))
367 if (!(bfd_section_flags (osec) & SEC_READONLY))
369 if (bfd_section_flags (osec) & SEC_CODE)
372 bfd_record_phdr (
obfd, p_type, 1, p_flags, 0, 0, 0, 0, 1, &osec);
384 int write,
int exec,
int modified,
bool memory_tagged,
387 bfd *
obfd = (bfd *) data;
389 flagword
flags = SEC_ALLOC | SEC_HAS_CONTENTS | SEC_LOAD;
394 if (
read == 0 && write == 0 && exec == 0 && modified == 0)
398 gdb_printf (
"Ignore segment, %s bytes at %s\n",
414 asection *asec = objsec->the_bfd_section;
415 bfd_vma align = (bfd_vma) 1 << bfd_section_alignment (asec);
416 bfd_vma start = objsec->addr () & -align;
417 bfd_vma end = (objsec->endaddr () + align - 1) & -align;
427 && ((vaddr >= start && vaddr +
size <= end)
428 || (start >= vaddr && end <= vaddr +
size))
429 && !(bfd_get_file_flags (abfd) & BFD_IN_MEMORY))
431 flags &= ~(SEC_LOAD | SEC_HAS_CONTENTS);
440 flags |= SEC_READONLY;
447 osec = bfd_make_section_anyway_with_flags (
obfd,
"load",
flags);
450 warning (_(
"Couldn't make gcore segment: %s"),
451 bfd_errmsg (bfd_get_error ()));
461 bfd_set_section_size (osec,
size);
462 bfd_set_section_vma (osec, vaddr);
463 bfd_set_section_lma (osec, 0);
476 int read,
int write,
int exec,
477 int modified,
bool memory_tagged,
484 bfd *
obfd = (bfd *) data;
489 asection *memtag_section
492 if (memtag_section ==
nullptr)
494 warning (_(
"Couldn't make gcore memory tag segment: %s"),
495 bfd_errmsg (bfd_get_error ()));
503 plongest (bfd_section_size (memtag_section)),
515 bfd_vma temp_bottom, temp_top;
521 asection *isec = objsec->the_bfd_section;
522 flagword
flags = bfd_section_flags (isec);
528 if ((
flags & SEC_ALLOC) || (
flags & SEC_LOAD))
530 int size = bfd_section_size (isec);
533 ret = (*func) (objsec->addr (),
size,
535 (
flags & SEC_READONLY) == 0,
536 (
flags & SEC_CODE) != 0,
547 (*
func) (temp_bottom, temp_top - temp_bottom,
558 (*func) (temp_bottom, temp_top - temp_bottom,
572 bfd_size_type
size, total_size = bfd_section_size (osec);
576 if ((bfd_section_flags (osec) & SEC_LOAD) == 0)
580 if (!startswith (bfd_section_name (osec),
"load"))
584 gdb::byte_vector memhunk (
size);
586 while (total_size > 0)
588 if (
size > total_size)
592 memhunk.data (),
size) != 0)
594 warning (_(
"Memory read failed for corefile "
595 "section, %s bytes at %s."),
600 if (!bfd_set_section_contents (
obfd, osec, memhunk.data (),
603 warning (_(
"Failed to write corefile contents (%s)."),
604 bfd_errmsg (bfd_get_error ()));
619 if (!startswith (bfd_section_name (osec),
"memtag"))
624 error (_(
"Failed to fill memory tag section for core file."));
675 if (thr->stop_signal () != GDB_SIGNAL_0)
691Save a core file with the current state of the debugged process.\n\
692Usage: generate-core-file [FILENAME]\n\
693Argument is optional filename. Default filename is 'core.PROCESS_ID'."));
struct gdbarch * target_gdbarch(void)
inf_non_exited_threads_range non_exited_threads()
gdb_signal stop_signal() const
cmd_list_element * add_com_alias(const char *name, cmd_list_element *target, command_class theclass, int abbrev_flag)
struct cmd_list_element * add_com(const char *name, enum command_class theclass, cmd_simple_func_ftype *fun, const char *doc)
int(* find_memory_region_ftype)(CORE_ADDR addr, unsigned long size, int read, int write, int exec, int modified, bool memory_tagged, void *data)
ssize_t read(int fd, void *buf, size_t count)
CORE_ADDR get_frame_sp(frame_info_ptr this_frame)
struct gdbarch * get_frame_arch(frame_info_ptr this_frame)
frame_info_ptr get_current_frame(void)
frame_info_ptr get_prev_frame(frame_info_ptr this_frame)
CORE_ADDR get_frame_base(frame_info_ptr fi)
thread_info * gcore_find_signalled_thread()
static void gcore_copy_callback(bfd *obfd, asection *osec)
int objfile_find_memory_regions(struct target_ops *self, find_memory_region_ftype func, void *obfd)
static void make_output_phdrs(bfd *obfd, asection *osec)
static int gcore_memory_sections(bfd *)
static int gcore_create_callback(CORE_ADDR vaddr, unsigned long size, int read, int write, int exec, int modified, bool memory_tagged, void *data)
static int gcore_create_memtag_section_callback(CORE_ADDR vaddr, unsigned long size, int read, int write, int exec, int modified, bool memory_tagged, void *data)
static void gcore_command(const char *args, int from_tty)
static bfd_vma call_target_sbrk(int sbrk_arg)
gdb_bfd_ref_ptr create_gcore_bfd(const char *filename)
static int derive_heap_segment(bfd *abfd, bfd_vma *bottom, bfd_vma *top)
static enum bfd_architecture default_gcore_arch(void)
static int derive_stack_segment(bfd_vma *bottom, bfd_vma *top)
static const char * default_gcore_target(void)
void write_gcore_file(bfd *obfd)
static void write_gcore_file_1(bfd *obfd)
static void gcore_copy_memtag_section_callback(bfd *obfd, asection *osec)
gdb_bfd_ref_ptr gdb_bfd_openw(const char *filename, const char *target)
gdb::ref_ptr< struct bfd, gdb_bfd_ref_policy > gdb_bfd_ref_ptr
static gdb_bfd_section_range gdb_bfd_sections(bfd *abfd)
bool gdbarch_fill_memtag_section(struct gdbarch *gdbarch, asection *osec)
bool gdbarch_make_corefile_notes_p(struct gdbarch *gdbarch)
int gdbarch_find_memory_regions(struct gdbarch *gdbarch, find_memory_region_ftype func, void *data)
gdb::unique_xmalloc_ptr< char > gdbarch_make_corefile_notes(struct gdbarch *gdbarch, bfd *obfd, int *note_size)
bool gdbarch_gcore_bfd_target_p(struct gdbarch *gdbarch)
asection * gdbarch_create_memtag_section(struct gdbarch *gdbarch, bfd *obfd, CORE_ADDR address, size_t size)
const char * gdbarch_gcore_bfd_target(struct gdbarch *gdbarch)
int gdbarch_inner_than(struct gdbarch *gdbarch, CORE_ADDR lhs, CORE_ADDR rhs)
const struct bfd_arch_info * gdbarch_bfd_arch_info(struct gdbarch *gdbarch)
bool gdbarch_find_memory_regions_p(struct gdbarch *gdbarch)
struct thread_info * inferior_thread(void)
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
struct value * call_function_by_hand(struct value *function, type *default_return_type, gdb::array_view< value * > args)
struct inferior * current_inferior(void)
struct bound_minimal_symbol lookup_minimal_symbol(const char *name, const char *sfile, struct objfile *objf)
struct program_space * current_program_space
void(* func)(remote_target *remote, char *)
bool solib_keep_data_in_core(CORE_ADDR vaddr, unsigned long size)
iterator_range< section_iterator > sections()
struct objfile * separate_debug_objfile_backlink
struct gdbarch * arch() const
objfiles_range objfiles()
bool target_supports_dumpcore()
void target_done_generating_core(void)
gdb::unique_xmalloc_ptr< char > target_make_corefile_notes(bfd *bfd, int *size_p)
void target_dumpcore(const char *filename)
bool target_has_execution(inferior *inf)
int target_has_registers()
void target_prepare_to_generate_core(void)
int target_read_memory(CORE_ADDR memaddr, gdb_byte *myaddr, ssize_t len)
int target_find_memory_regions(find_memory_region_ftype func, void *data)
const char * paddress(struct gdbarch *gdbarch, CORE_ADDR addr)
void gdb_printf(struct ui_file *stream, const char *format,...)
struct value * find_function_in_inferior(const char *name, struct objfile **objf_p)
struct value * value_from_longest(struct type *type, LONGEST num)
LONGEST value_as_long(struct value *val)