gdb/gcore.c - gdb
Functions defined
Macros defined
Source code
- #include "defs.h"
- #include "elf-bfd.h"
- #include "infcall.h"
- #include "inferior.h"
- #include "gdbcore.h"
- #include "objfiles.h"
- #include "solib.h"
- #include "symfile.h"
- #include "arch-utils.h"
- #include "completer.h"
- #include "gcore.h"
- #include "cli/cli-decode.h"
- #include <fcntl.h>
- #include "regcache.h"
- #include "regset.h"
- #include "gdb_bfd.h"
- #include "readline/tilde.h"
- #define MAX_COPY_BYTES (1024 * 1024)
- static const char *default_gcore_target (void);
- static enum bfd_architecture default_gcore_arch (void);
- static unsigned long default_gcore_mach (void);
- static int gcore_memory_sections (bfd *);
- bfd *
- create_gcore_bfd (const char *filename)
- {
- bfd *obfd = gdb_bfd_openw (filename, default_gcore_target ());
- if (!obfd)
- error (_("Failed to open '%s' for output."), filename);
- bfd_set_format (obfd, bfd_core);
- bfd_set_arch_mach (obfd, default_gcore_arch (), default_gcore_mach ());
- return obfd;
- }
- static void
- write_gcore_file_1 (bfd *obfd)
- {
- struct cleanup *cleanup;
- void *note_data = NULL;
- int note_size = 0;
- asection *note_sec = NULL;
-
- FIXME
- if (!gdbarch_make_corefile_notes_p (target_gdbarch ()))
- note_data = target_make_corefile_notes (obfd, ¬e_size);
- else
- note_data = gdbarch_make_corefile_notes (target_gdbarch (), obfd, ¬e_size);
- cleanup = make_cleanup (xfree, note_data);
- if (note_data == NULL || note_size == 0)
- error (_("Target does not support core file generation."));
-
- note_sec = bfd_make_section_anyway_with_flags (obfd, "note0",
- SEC_HAS_CONTENTS
- | SEC_READONLY
- | SEC_ALLOC);
- if (note_sec == NULL)
- error (_("Failed to create 'note' section for corefile: %s"),
- bfd_errmsg (bfd_get_error ()));
- bfd_set_section_vma (obfd, note_sec, 0);
- bfd_set_section_alignment (obfd, note_sec, 0);
- bfd_set_section_size (obfd, note_sec, note_size);
-
- if (gcore_memory_sections (obfd) == 0)
- error (_("gcore: failed to get corefile memory sections from target."));
-
- if (!bfd_set_section_contents (obfd, note_sec, note_data, 0, note_size))
- warning (_("writing note section (%s)"), bfd_errmsg (bfd_get_error ()));
- do_cleanups (cleanup);
- }
- void
- write_gcore_file (bfd *obfd)
- {
- volatile struct gdb_exception except;
- target_prepare_to_generate_core ();
- TRY_CATCH (except, RETURN_MASK_ALL)
- write_gcore_file_1 (obfd);
- target_done_generating_core ();
- if (except.reason < 0)
- throw_exception (except);
- }
- static void
- do_bfd_delete_cleanup (void *arg)
- {
- bfd *obfd = arg;
- const char *filename = obfd->filename;
- gdb_bfd_unref (arg);
- unlink (filename);
- }
- static void
- gcore_command (char *args, int from_tty)
- {
- struct cleanup *filename_chain;
- struct cleanup *bfd_chain;
- char *corefilename;
- bfd *obfd;
-
- if (!target_has_execution)
- noprocess ();
- if (args && *args)
- corefilename = tilde_expand (args);
- else
- {
-
- corefilename = xstrprintf ("core.%d", ptid_get_pid (inferior_ptid));
- }
- filename_chain = make_cleanup (xfree, corefilename);
- if (info_verbose)
- fprintf_filtered (gdb_stdout,
- "Opening corefile '%s' for output.\n", corefilename);
-
- obfd = create_gcore_bfd (corefilename);
-
- bfd_chain = make_cleanup (do_bfd_delete_cleanup, obfd);
-
- write_gcore_file (obfd);
-
- discard_cleanups (bfd_chain);
- gdb_bfd_unref (obfd);
- fprintf_filtered (gdb_stdout, "Saved corefile %s\n", corefilename);
- do_cleanups (filename_chain);
- }
- static unsigned long
- default_gcore_mach (void)
- {
- #if 1
- return 0;
- #else
- #endif
- }
- static enum bfd_architecture
- default_gcore_arch (void)
- {
- const struct bfd_arch_info *bfdarch = gdbarch_bfd_arch_info (target_gdbarch ());
- if (bfdarch != NULL)
- return bfdarch->arch;
- if (exec_bfd == NULL)
- error (_("Can't find bfd architecture for corefile (need execfile)."));
- return bfd_get_arch (exec_bfd);
- }
- static const char *
- default_gcore_target (void)
- {
-
- if (gdbarch_gcore_bfd_target_p (target_gdbarch ()))
- return gdbarch_gcore_bfd_target (target_gdbarch ());
-
- if (exec_bfd == NULL)
- return NULL;
- else
- return bfd_get_target (exec_bfd);
- }
- static int
- derive_stack_segment (bfd_vma *bottom, bfd_vma *top)
- {
- struct frame_info *fi, *tmp_fi;
- gdb_assert (bottom);
- gdb_assert (top);
-
- if (!target_has_stack || !target_has_registers)
- return 0;
-
- fi = get_current_frame ();
- if (fi == NULL)
- return 0;
-
- *top = get_frame_base (fi);
-
- if (gdbarch_inner_than (get_frame_arch (fi), get_frame_sp (fi), *top))
- *top = get_frame_sp (fi);
-
- while ((tmp_fi = get_prev_frame (fi)) != NULL)
- fi = tmp_fi;
-
- *bottom = get_frame_base (fi);
-
- if (*bottom > *top)
- {
- bfd_vma tmp_vma;
- tmp_vma = *top;
- *top = *bottom;
- *bottom = tmp_vma;
- }
- return 1;
- }
- static bfd_vma
- call_target_sbrk (int sbrk_arg)
- {
- struct objfile *sbrk_objf;
- struct gdbarch *gdbarch;
- bfd_vma top_of_heap;
- struct value *target_sbrk_arg;
- struct value *sbrk_fn, *ret;
- bfd_vma tmp;
- if (lookup_minimal_symbol ("sbrk", NULL, NULL).minsym != NULL)
- {
- sbrk_fn = find_function_in_inferior ("sbrk", &sbrk_objf);
- if (sbrk_fn == NULL)
- return (bfd_vma) 0;
- }
- else if (lookup_minimal_symbol ("_sbrk", NULL, NULL).minsym != NULL)
- {
- sbrk_fn = find_function_in_inferior ("_sbrk", &sbrk_objf);
- if (sbrk_fn == NULL)
- return (bfd_vma) 0;
- }
- else
- return (bfd_vma) 0;
- gdbarch = get_objfile_arch (sbrk_objf);
- target_sbrk_arg = value_from_longest (builtin_type (gdbarch)->builtin_int,
- sbrk_arg);
- gdb_assert (target_sbrk_arg);
- ret = call_function_by_hand (sbrk_fn, 1, &target_sbrk_arg);
- if (ret == NULL)
- return (bfd_vma) 0;
- tmp = value_as_long (ret);
- if ((LONGEST) tmp <= 0 || (LONGEST) tmp == 0xffffffff)
- return (bfd_vma) 0;
- top_of_heap = tmp;
- return top_of_heap;
- }
- static int
- derive_heap_segment (bfd *abfd, bfd_vma *bottom, bfd_vma *top)
- {
- bfd_vma top_of_data_memory = 0;
- bfd_vma top_of_heap = 0;
- bfd_size_type sec_size;
- bfd_vma sec_vaddr;
- asection *sec;
- gdb_assert (bottom);
- gdb_assert (top);
-
- if (!target_has_execution)
- return 0;
-
- for (sec = abfd->sections; sec; sec = sec->next)
- {
- if (bfd_get_section_flags (abfd, sec) & SEC_DATA
- || strcmp (".bss", bfd_section_name (abfd, sec)) == 0)
- {
- sec_vaddr = bfd_get_section_vma (abfd, sec);
- sec_size = bfd_get_section_size (sec);
- if (sec_vaddr + sec_size > top_of_data_memory)
- top_of_data_memory = sec_vaddr + sec_size;
- }
- }
- top_of_heap = call_target_sbrk (0);
- if (top_of_heap == (bfd_vma) 0)
- return 0;
-
- if (top_of_heap > top_of_data_memory)
- {
- *bottom = top_of_data_memory;
- *top = top_of_heap;
- return 1;
- }
-
- return 0;
- }
- static void
- make_output_phdrs (bfd *obfd, asection *osec, void *ignored)
- {
- int p_flags = 0;
- int p_type = 0;
- FIXME
- if (strncmp (bfd_section_name (obfd, osec), "load", 4) == 0)
- p_type = PT_LOAD;
- else if (strncmp (bfd_section_name (obfd, osec), "note", 4) == 0)
- p_type = PT_NOTE;
- else
- p_type = PT_NULL;
- p_flags |= PF_R;
- if (!(bfd_get_section_flags (obfd, osec) & SEC_READONLY))
- p_flags |= PF_W;
- if (bfd_get_section_flags (obfd, osec) & SEC_CODE)
- p_flags |= PF_X;
- bfd_record_phdr (obfd, p_type, 1, p_flags, 0, 0, 0, 0, 1, &osec);
- }
- static int
- gcore_create_callback (CORE_ADDR vaddr, unsigned long size, int read,
- int write, int exec, int modified, void *data)
- {
- bfd *obfd = data;
- asection *osec;
- flagword flags = SEC_ALLOC | SEC_HAS_CONTENTS | SEC_LOAD;
-
- if (read == 0 && write == 0 && exec == 0 && modified == 0)
- {
- if (info_verbose)
- {
- fprintf_filtered (gdb_stdout, "Ignore segment, %s bytes at %s\n",
- plongest (size), paddress (target_gdbarch (), vaddr));
- }
- return 0;
- }
- if (write == 0 && modified == 0 && !solib_keep_data_in_core (vaddr, size))
- {
-
- struct objfile *objfile;
- struct obj_section *objsec;
- ALL_OBJSECTIONS (objfile, objsec)
- {
- bfd *abfd = objfile->obfd;
- asection *asec = objsec->the_bfd_section;
- bfd_vma align = (bfd_vma) 1 << bfd_get_section_alignment (abfd,
- asec);
- bfd_vma start = obj_section_addr (objsec) & -align;
- bfd_vma end = (obj_section_endaddr (objsec) + align - 1) & -align;
-
- if (objfile->separate_debug_objfile_backlink == NULL
- && ((vaddr >= start && vaddr + size <= end)
- || (start >= vaddr && end <= vaddr + size))
- && !(bfd_get_file_flags (abfd) & BFD_IN_MEMORY))
- {
- flags &= ~(SEC_LOAD | SEC_HAS_CONTENTS);
- goto keep;
- }
- }
- keep:;
- }
- if (write == 0)
- flags |= SEC_READONLY;
- if (exec)
- flags |= SEC_CODE;
- else
- flags |= SEC_DATA;
- osec = bfd_make_section_anyway_with_flags (obfd, "load", flags);
- if (osec == NULL)
- {
- warning (_("Couldn't make gcore segment: %s"),
- bfd_errmsg (bfd_get_error ()));
- return 1;
- }
- if (info_verbose)
- {
- fprintf_filtered (gdb_stdout, "Save segment, %s bytes at %s\n",
- plongest (size), paddress (target_gdbarch (), vaddr));
- }
- bfd_set_section_size (obfd, osec, size);
- bfd_set_section_vma (obfd, osec, vaddr);
- bfd_section_lma (obfd, osec) = 0;
- return 0;
- }
- int
- objfile_find_memory_regions (struct target_ops *self,
- find_memory_region_ftype func, void *obfd)
- {
-
- struct objfile *objfile;
- struct obj_section *objsec;
- bfd_vma temp_bottom, temp_top;
-
- ALL_OBJSECTIONS (objfile, objsec)
- {
- bfd *ibfd = objfile->obfd;
- asection *isec = objsec->the_bfd_section;
- flagword flags = bfd_get_section_flags (ibfd, isec);
-
- if (objfile->separate_debug_objfile_backlink != NULL)
- continue;
- if ((flags & SEC_ALLOC) || (flags & SEC_LOAD))
- {
- int size = bfd_section_size (ibfd, isec);
- int ret;
- ret = (*func) (obj_section_addr (objsec), size,
- 1,
- (flags & SEC_READONLY) == 0,
- (flags & SEC_CODE) != 0,
- 1,
- obfd);
- if (ret != 0)
- return ret;
- }
- }
-
- if (derive_stack_segment (&temp_bottom, &temp_top))
- (*func) (temp_bottom, temp_top - temp_bottom,
- 1,
- 1,
- 0,
- 1,
- obfd);
-
- if (derive_heap_segment (exec_bfd, &temp_bottom, &temp_top))
- (*func) (temp_bottom, temp_top - temp_bottom,
- 1,
- 1,
- 0,
- 1,
- obfd);
- return 0;
- }
- static void
- gcore_copy_callback (bfd *obfd, asection *osec, void *ignored)
- {
- bfd_size_type size, total_size = bfd_section_size (obfd, osec);
- file_ptr offset = 0;
- struct cleanup *old_chain = NULL;
- void *memhunk;
-
- if ((bfd_get_section_flags (obfd, osec) & SEC_LOAD) == 0)
- return;
-
- if (strncmp ("load", bfd_section_name (obfd, osec), 4) != 0)
- return;
- size = min (total_size, MAX_COPY_BYTES);
- memhunk = xmalloc (size);
- old_chain = make_cleanup (xfree, memhunk);
- while (total_size > 0)
- {
- if (size > total_size)
- size = total_size;
- if (target_read_memory (bfd_section_vma (obfd, osec) + offset,
- memhunk, size) != 0)
- {
- warning (_("Memory read failed for corefile "
- "section, %s bytes at %s."),
- plongest (size),
- paddress (target_gdbarch (), bfd_section_vma (obfd, osec)));
- break;
- }
- if (!bfd_set_section_contents (obfd, osec, memhunk, offset, size))
- {
- warning (_("Failed to write corefile contents (%s)."),
- bfd_errmsg (bfd_get_error ()));
- break;
- }
- total_size -= size;
- offset += size;
- }
- do_cleanups (old_chain);
- }
- static int
- gcore_memory_sections (bfd *obfd)
- {
-
- if (!gdbarch_find_memory_regions_p (target_gdbarch ())
- || gdbarch_find_memory_regions (target_gdbarch (),
- gcore_create_callback, obfd) != 0)
- {
- if (target_find_memory_regions (gcore_create_callback, obfd) != 0)
- return 0; FIXME
- }
-
- bfd_map_over_sections (obfd, make_output_phdrs, NULL);
-
- bfd_map_over_sections (obfd, gcore_copy_callback, NULL);
- return 1;
- }
- extern initialize_file_ftype _initialize_gcore;
- void
- _initialize_gcore (void)
- {
- add_com ("generate-core-file", class_files, gcore_command, _("\
- Save a core file with the current state of the debugged process.\n\
- Argument is optional filename. Default filename is 'core.<process_id>'."));
- add_com_alias ("gcore", "generate-core-file", class_files, 1);
- }