[RFA] implement gcore on hp/ux

Joel Brobecker brobecker@adacore.com
Tue Jan 10 17:18:00 GMT 2006


Hello,

The attached patch aims at implementing the gcore command on hp/ux.
Contrary to the current gcore implementation where we write the core
file ourselves, the implementation on hp/ux relies on the TT_PROC_CORE
ttrace command.

In order to make the gcore command a bit more generic, I added a new
method in the target vector which, when set, is responsible for writing
the core file.

The core of gcore.c that used to write the core file has been moved
to gcore-elf.c, and the exec_ops target vector is updated there.
Since there is now a second method to update (the one we just added),
I added a new function in exec.c with a corresponding declaration in
defs.h (besides the one we already have).

gcore.o is now always part of the build. When the target does not
support core files, the debugger now prints that this command is
not supported on this platform.

All builds that were including gcore.o are now including gcore-elf.o.
Including this file causes us to update the exec target ops as we used
to, and hence hooks up the "elf" implementation.

For the ttrace target, it's straighforward...

2005-01-10  Joel Brobecker  <brobecker@adacore.com>

        * target.h (target_ops): New method to_write_core_file.
        (target_write_core_file_p): New macro.
        (target_write_core_file): New macro.
        * target.c (update_current_target): Add heritance of to_write_core_file.
        * exec.c (exec_set_write_core_file): New function.
        * defs.h (exec_set_write_core_file): Add declaration.
        * gcore.c: Extract out the code writing the core file and move it
        to gcore-elf.c.
        * gcore-elf.c: New file, mostly extracted from gcore.o.
        * Makefile.in (COMMON_OBS): Add gcore.o.
        (gcore.o): Update dependencies.
        (gcore-elf.o): Add rule.
        * inf-ttrace (inf_ttrace_write_core_file): New function.
        (inf_ttrace_target): set the to_write_core_file method.
        * config/alpha/fbsd.mh (NATDEPFILES): Likewise.
        * config/arm/linux.mh (NATDEPFILES): Likewise.
        * config/i386/fbsd.mh (NATDEPFILES): Likewise.
        * config/i386/fbsd64.mh (NATDEPFILES): Likewise.
        * config/i386/i386sol2.mh (NATDEPFILES): Likewise.
        * config/i386/i386v42mp.mh (NATDEPFILES): Likewise.
        * config/i386/linux.mh (NATDEPFILES): Likewise.
        * config/i386/linux64.mh (NATDEPFILES): Likewise.
        * config/i386/sol2-64.mh (NATDEPFILES): Likewise.
        * config/ia64/linux.mh (NATDEPFILES): Likewise.
        * config/m32r/linux.mh (NATDEPFILES): Likewise.
        * config/m68k/linux.mh (NATDEPFILES): Likewise.
        * config/mips/linux.mh (NATDEPFILES): Likewise.
        * config/pa/linux.mh (NATDEPFILES): Likewise.
        * config/powerpc/linux.mh (NATDEPFILES): Likewise.
        * config/powerpc/ppc64-linux.mh (NATDEPFILES): Likewise.
        * config/s390/s390.mh (NATDEPFILES): Likewise.
        * config/sparc/fbsd.mh (NATDEPFILES): Likewise.
        * config/sparc/linux.mh (NATDEPFILES): Likewise.
        * config/sparc/linux64.mh (NATDEPFILES): Likewise.
        * config/sparc/sol2.mh (NATDEPFILES): Likewise.

Tested on x86-linux and pa32-hpux. No regression on the linux
side. On the hp/ux side, we get improvments from auxv.exp and
gcore.exp now passes.

Comments? Ok to apply?

Thanks,
-- 
Joel
-------------- next part --------------
Index: target.h
===================================================================
RCS file: /cvs/src/src/gdb/target.h,v
retrieving revision 1.77
diff -u -p -r1.77 target.h
--- target.h	17 Dec 2005 22:34:03 -0000	1.77
+++ target.h	10 Jan 2006 16:03:28 -0000
@@ -398,6 +398,7 @@ struct target_ops
     void (*to_async) (void (*cb) (enum inferior_event_type, void *context),
 		      void *context);
     int to_async_mask_value;
+    void (*to_write_core_file) (int pid, const char *filename);
     int (*to_find_memory_regions) (int (*) (CORE_ADDR,
 					    unsigned long,
 					    int, int, int,
@@ -968,6 +969,13 @@ extern void (*deprecated_target_new_objf
 #define target_pid_to_exec_file(pid) \
      (current_target.to_pid_to_exec_file) (pid)
 
+/* Dump the inferior memory in a core file.  */
+
+#define target_write_core_file_p \
+     (current_target.to_write_core_file != NULL)
+
+#define target_write_core_file(pid, filename) \
+     (current_target.to_write_core_file (pid, filename))
 /*
  * Iterator function for target memory regions.
  * Calls a callback function once for each memory region 'mapped'
Index: target.c
===================================================================
RCS file: /cvs/src/src/gdb/target.c,v
retrieving revision 1.113
diff -u -p -r1.113 target.c
--- target.c	17 Dec 2005 22:34:03 -0000	1.113
+++ target.c	10 Jan 2006 16:03:27 -0000
@@ -453,6 +453,7 @@ update_current_target (void)
       INHERIT (to_is_async_p, t);
       INHERIT (to_async, t);
       INHERIT (to_async_mask_value, t);
+      INHERIT (to_write_core_file, t);
       INHERIT (to_find_memory_regions, t);
       INHERIT (to_make_corefile_notes, t);
       INHERIT (to_get_thread_local_address, t);
Index: exec.c
===================================================================
RCS file: /cvs/src/src/gdb/exec.c,v
retrieving revision 1.59
diff -u -p -r1.59 exec.c
--- exec.c	17 Dec 2005 22:33:59 -0000	1.59
+++ exec.c	10 Jan 2006 16:02:27 -0000
@@ -699,6 +699,14 @@ ignore (CORE_ADDR addr, bfd_byte *conten
   return 0;
 }
 
+/* Set the write_core_file method in the exec target vector. */
+
+void
+exec_set_write_core_file (void (*func) (int, const char *))
+{
+  exec_ops.to_write_core_file = func;
+}
+
 /* Find mapped memory. */
 
 extern void
Index: defs.h
===================================================================
RCS file: /cvs/src/src/gdb/defs.h,v
retrieving revision 1.190
diff -u -p -r1.190 defs.h
--- defs.h	17 Dec 2005 22:33:59 -0000	1.190
+++ defs.h	10 Jan 2006 16:02:19 -0000
@@ -623,6 +623,8 @@ extern void exec_set_section_offsets (bf
 				      bfd_signed_vma data_off,
 				      bfd_signed_vma bss_off);
 
+extern void exec_set_write_core_file (void (*func) (int, const char *));
+
 /* Take over the 'find_mapped_memory' vector from exec.c. */
 extern void exec_set_find_memory_regions (int (*) (int (*) (CORE_ADDR, 
 							    unsigned long, 
Index: gcore.c
===================================================================
RCS file: /cvs/src/src/gdb/gcore.c,v
retrieving revision 1.18
diff -u -p -r1.18 gcore.c
--- gcore.c	17 Dec 2005 22:33:59 -0000	1.18
+++ gcore.c	10 Jan 2006 16:02:30 -0000
@@ -20,38 +20,24 @@
    Boston, MA 02110-1301, USA.  */
 
 #include "defs.h"
-#include "elf-bfd.h"
-#include "infcall.h"
 #include "inferior.h"
-#include "gdbcore.h"
-#include "objfiles.h"
-#include "symfile.h"
-
-#include "cli/cli-decode.h"
-
-#include "gdb_assert.h"
-
-static 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 *);
+#include "target.h"
+#include "command.h"
 
 /* Generate a core file from the inferior process.  */
 
 static void
 gcore_command (char *args, int from_tty)
 {
-  struct cleanup *old_chain;
   char *corefilename, corefilename_buffer[40];
-  asection *note_sec = NULL;
-  bfd *obfd;
-  void *note_data = NULL;
-  int note_size = 0;
 
   /* No use generating a corefile without a target process.  */
   if (!target_has_execution)
     noprocess ();
 
+  if (!target_write_core_file_p)
+    error ("Command not supported on this platform");
+
   if (args && *args)
     corefilename = args;
   else
@@ -61,431 +47,7 @@ gcore_command (char *args, int from_tty)
       corefilename = corefilename_buffer;
     }
 
-  if (info_verbose)
-    fprintf_filtered (gdb_stdout,
-		      "Opening corefile '%s' for output.\n", corefilename);
-
-  /* Open the output file.  */
-  obfd = bfd_openw (corefilename, default_gcore_target ());
-  if (!obfd)
-    error (_("Failed to open '%s' for output."), corefilename);
-
-  /* Need a cleanup that will close the file (FIXME: delete it?).  */
-  old_chain = make_cleanup_bfd_close (obfd);
-
-  bfd_set_format (obfd, bfd_core);
-  bfd_set_arch_mach (obfd, default_gcore_arch (), default_gcore_mach ());
-
-  /* An external target method must build the notes section.  */
-  note_data = target_make_corefile_notes (obfd, &note_size);
-
-  /* Create the note section.  */
-  if (note_data != NULL && note_size != 0)
-    {
-      note_sec = bfd_make_section_anyway (obfd, "note0");
-      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_flags (obfd, note_sec,
-			     SEC_HAS_CONTENTS | SEC_READONLY | SEC_ALLOC);
-      bfd_set_section_alignment (obfd, note_sec, 0);
-      bfd_set_section_size (obfd, note_sec, note_size);
-    }
-
-  /* Now create the memory/load sections.  */
-  if (gcore_memory_sections (obfd) == 0)
-    error (_("gcore: failed to get corefile memory sections from target."));
-
-  /* Write out the contents of the note section.  */
-  if (note_data != NULL && note_size != 0)
-    {
-      if (!bfd_set_section_contents (obfd, note_sec, note_data, 0, note_size))
-	warning (_("writing note section (%s)"), bfd_errmsg (bfd_get_error ()));
-    }
-
-  /* Succeeded.  */
-  fprintf_filtered (gdb_stdout, "Saved corefile %s\n", corefilename);
-
-  /* Clean-ups will close the output file and free malloc memory.  */
-  do_cleanups (old_chain);
-  return;
-}
-
-static unsigned long
-default_gcore_mach (void)
-{
-#if 1	/* See if this even matters...  */
-  return 0;
-#else
-#ifdef TARGET_ARCHITECTURE
-  const struct bfd_arch_info *bfdarch = TARGET_ARCHITECTURE;
-
-  if (bfdarch != NULL)
-    return bfdarch->mach;
-#endif /* TARGET_ARCHITECTURE */
-  if (exec_bfd == NULL)
-    error (_("Can't find default bfd machine type (need execfile)."));
-
-  return bfd_get_mach (exec_bfd);
-#endif /* 1 */
-}
-
-static enum bfd_architecture
-default_gcore_arch (void)
-{
-#ifdef TARGET_ARCHITECTURE
-  const struct bfd_arch_info * bfdarch = TARGET_ARCHITECTURE;
-
-  if (bfdarch != NULL)
-    return bfdarch->arch;
-#endif
-  if (exec_bfd == NULL)
-    error (_("Can't find bfd architecture for corefile (need execfile)."));
-
-  return bfd_get_arch (exec_bfd);
-}
-
-static char *
-default_gcore_target (void)
-{
-  /* FIXME: This may only work for ELF targets.  */
-  if (exec_bfd == NULL)
-    return NULL;
-  else
-    return bfd_get_target (exec_bfd);
-}
-
-/* Derive a reasonable stack segment by unwinding the target stack,
-   and store its limits in *BOTTOM and *TOP.  Return non-zero if
-   successful.  */
-
-static int
-derive_stack_segment (bfd_vma *bottom, bfd_vma *top)
-{
-  struct frame_info *fi, *tmp_fi;
-
-  gdb_assert (bottom);
-  gdb_assert (top);
-
-  /* Can't succeed without stack and registers.  */
-  if (!target_has_stack || !target_has_registers)
-    return 0;
-
-  /* Can't succeed without current frame.  */
-  fi = get_current_frame ();
-  if (fi == NULL)
-    return 0;
-
-  /* Save frame pointer of TOS frame.  */
-  *top = get_frame_base (fi);
-  /* If current stack pointer is more "inner", use that instead.  */
-  if (INNER_THAN (read_sp (), *top))
-    *top = read_sp ();
-
-  /* Find prev-most frame.  */
-  while ((tmp_fi = get_prev_frame (fi)) != NULL)
-    fi = tmp_fi;
-
-  /* Save frame pointer of prev-most frame.  */
-  *bottom = get_frame_base (fi);
-
-  /* Now canonicalize their order, so that BOTTOM is a lower address
-     (as opposed to a lower stack frame).  */
-  if (*bottom > *top)
-    {
-      bfd_vma tmp_vma;
-
-      tmp_vma = *top;
-      *top = *bottom;
-      *bottom = tmp_vma;
-    }
-
-  return 1;
-}
-
-/* Derive a reasonable heap segment for ABFD by looking at sbrk and
-   the static data sections.  Store its limits in *BOTTOM and *TOP.
-   Return non-zero if successful.  */
-
-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;
-  struct value *zero, *sbrk;
-  bfd_vma sec_vaddr;
-  asection *sec;
-
-  gdb_assert (bottom);
-  gdb_assert (top);
-
-  /* This function depends on being able to call a function in the
-     inferior.  */
-  if (!target_has_execution)
-    return 0;
-
-  /* The following code assumes that the link map is arranged as
-     follows (low to high addresses):
-
-     ---------------------------------
-     | text sections                 |
-     ---------------------------------
-     | data sections (including bss) |
-     ---------------------------------
-     | heap                          |
-     --------------------------------- */
-
-  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;
-	}
-    }
-
-  /* Now get the top-of-heap by calling sbrk in the inferior.  */
-  if (lookup_minimal_symbol ("sbrk", NULL, NULL) != NULL)
-    {
-      sbrk = find_function_in_inferior ("sbrk");
-      if (sbrk == NULL)
-	return 0;
-    }
-  else if (lookup_minimal_symbol ("_sbrk", NULL, NULL) != NULL)
-    {
-      sbrk = find_function_in_inferior ("_sbrk");
-      if (sbrk == NULL)
-	return 0;
-    }
-  else
-    return 0;
-
-  zero = value_from_longest (builtin_type_int, 0);
-  gdb_assert (zero);
-  sbrk = call_function_by_hand (sbrk, 1, &zero);
-  if (sbrk == NULL)
-    return 0;
-  top_of_heap = value_as_long (sbrk);
-
-  /* Return results.  */
-  if (top_of_heap > top_of_data_memory)
-    {
-      *bottom = top_of_data_memory;
-      *top = top_of_heap;
-      return 1;
-    }
-
-  /* No additional heap space needs to be saved.  */
-  return 0;
-}
-
-static void
-make_output_phdrs (bfd *obfd, asection *osec, void *ignored)
-{
-  int p_flags = 0;
-  int p_type;
-
-  /* FIXME: these constants may only be applicable for ELF.  */
-  if (strncmp (bfd_section_name (obfd, osec), "load", 4) == 0)
-    p_type = PT_LOAD;
-  else
-    p_type = PT_NOTE;
-
-  p_flags |= PF_R;	/* Segment is readable.  */
-  if (!(bfd_get_section_flags (obfd, osec) & SEC_READONLY))
-    p_flags |= PF_W;	/* Segment is writable.  */
-  if (bfd_get_section_flags (obfd, osec) & SEC_CODE)
-    p_flags |= PF_X;	/* Segment is executable.  */
-
-  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, void *data)
-{
-  bfd *obfd = data;
-  asection *osec;
-  flagword flags = SEC_ALLOC | SEC_HAS_CONTENTS | SEC_LOAD;
-
-  /* If the memory segment has no permissions set, ignore it, otherwise
-     when we later try to access it for read/write, we'll get an error
-     or jam the kernel.  */
-  if (read == 0 && write == 0 && exec == 0)
-    {
-      if (info_verbose)
-        {
-          fprintf_filtered (gdb_stdout, "Ignore segment, %s bytes at 0x%s\n",
-                           paddr_d (size), paddr_nz (vaddr));
-        }
-
-      return 0;
-    }
-
-  if (write == 0)
-    {
-      /* See if this region of memory lies inside a known file on disk.
-	 If so, we can avoid copying its contents by clearing SEC_LOAD.  */
-      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 = objsec->addr & -align;
-	  bfd_vma end = (objsec->endaddr + align - 1) & -align;
-	  /* Match if either the entire memory region lies inside the
-	     section (i.e. a mapping covering some pages of a large
-	     segment) or the entire section lies inside the memory region
-	     (i.e. a mapping covering multiple small sections).
-
-	     This BFD was synthesized from reading target memory,
-	     we don't want to omit that.  */
-	  if (((vaddr >= start && vaddr + size <= end)
-	       || (start >= vaddr && end <= vaddr + size))
-	      && !(bfd_get_file_flags (abfd) & BFD_IN_MEMORY))
-	    {
-	      flags &= ~SEC_LOAD;
-	      goto keep;	/* break out of two nested for loops */
-	    }
-	}
-
-    keep:
-      flags |= SEC_READONLY;
-    }
-
-  if (exec)
-    flags |= SEC_CODE;
-  else
-    flags |= SEC_DATA;
-
-  osec = bfd_make_section_anyway (obfd, "load");
-  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 0x%s\n",
-			paddr_d (size), paddr_nz (vaddr));
-    }
-
-  bfd_set_section_size (obfd, osec, size);
-  bfd_set_section_vma (obfd, osec, vaddr);
-  bfd_section_lma (obfd, osec) = 0; /* ??? bfd_set_section_lma?  */
-  bfd_set_section_flags (obfd, osec, flags);
-  return 0;
-}
-
-static int
-objfile_find_memory_regions (int (*func) (CORE_ADDR, unsigned long,
-					  int, int, int, void *),
-			     void *obfd)
-{
-  /* Use objfile data to create memory sections.  */
-  struct objfile *objfile;
-  struct obj_section *objsec;
-  bfd_vma temp_bottom, temp_top;
-
-  /* Call callback function for each objfile section.  */
-  ALL_OBJSECTIONS (objfile, objsec)
-    {
-      bfd *ibfd = objfile->obfd;
-      asection *isec = objsec->the_bfd_section;
-      flagword flags = bfd_get_section_flags (ibfd, isec);
-      int ret;
-
-      if ((flags & SEC_ALLOC) || (flags & SEC_LOAD))
-	{
-	  int size = bfd_section_size (ibfd, isec);
-	  int ret;
-
-	  ret = (*func) (objsec->addr, bfd_section_size (ibfd, isec),
-			 1, /* All sections will be readable.  */
-			 (flags & SEC_READONLY) == 0, /* Writable.  */
-			 (flags & SEC_CODE) != 0, /* Executable.  */
-			 obfd);
-	  if (ret != 0)
-	    return ret;
-	}
-    }
-
-  /* Make a stack segment.  */
-  if (derive_stack_segment (&temp_bottom, &temp_top))
-    (*func) (temp_bottom, temp_top - temp_bottom,
-	     1, /* Stack section will be readable.  */
-	     1, /* Stack section will be writable.  */
-	     0, /* Stack section will not be executable.  */
-	     obfd);
-
-  /* Make a heap segment. */
-  if (derive_heap_segment (exec_bfd, &temp_bottom, &temp_top))
-    (*func) (temp_bottom, temp_top - temp_bottom,
-	     1, /* Heap section will be readable.  */
-	     1, /* Heap section will be writable.  */
-	     0, /* Heap section will not be executable.  */
-	     obfd);
-
-  return 0;
-}
-
-static void
-gcore_copy_callback (bfd *obfd, asection *osec, void *ignored)
-{
-  bfd_size_type size = bfd_section_size (obfd, osec);
-  struct cleanup *old_chain = NULL;
-  void *memhunk;
-
-  /* Read-only sections are marked; we don't have to copy their contents.  */
-  if ((bfd_get_section_flags (obfd, osec) & SEC_LOAD) == 0)
-    return;
-
-  /* Only interested in "load" sections.  */
-  if (strncmp ("load", bfd_section_name (obfd, osec), 4) != 0)
-    return;
-
-  memhunk = xmalloc (size);
-  /* ??? This is crap since xmalloc should never return NULL.  */
-  if (memhunk == NULL)
-    error (_("Not enough memory to create corefile."));
-  old_chain = make_cleanup (xfree, memhunk);
-
-  if (target_read_memory (bfd_section_vma (obfd, osec),
-			  memhunk, size) != 0)
-    warning (_("Memory read failed for corefile section, %s bytes at 0x%s."),
-	     paddr_d (size), paddr (bfd_section_vma (obfd, osec)));
-  if (!bfd_set_section_contents (obfd, osec, memhunk, 0, size))
-    warning (_("Failed to write corefile contents (%s)."),
-	     bfd_errmsg (bfd_get_error ()));
-
-  do_cleanups (old_chain);	/* Frees MEMHUNK.  */
-}
-
-static int
-gcore_memory_sections (bfd *obfd)
-{
-  if (target_find_memory_regions (gcore_create_callback, obfd) != 0)
-    return 0;			/* FIXME: error return/msg?  */
-
-  /* Record phdrs for section-to-segment mapping.  */
-  bfd_map_over_sections (obfd, make_output_phdrs, NULL);
-
-  /* Copy memory region contents.  */
-  bfd_map_over_sections (obfd, gcore_copy_callback, NULL);
-
-  return 1;
+  target_write_core_file (ptid_get_pid (inferior_ptid), corefilename);
 }
 
 void
@@ -496,5 +58,4 @@ Save a core file with the current state 
 Argument is optional filename.  Default filename is 'core.<process_id>'."));
 
   add_com_alias ("gcore", "generate-core-file", class_files, 1);
-  exec_set_find_memory_regions (objfile_find_memory_regions);
 }
Index: Makefile.in
===================================================================
RCS file: /cvs/src/src/gdb/Makefile.in,v
retrieving revision 1.772
diff -u -p -r1.772 Makefile.in
--- Makefile.in	4 Jan 2006 19:34:58 -0000	1.772
+++ Makefile.in	10 Jan 2006 16:01:52 -0000
@@ -940,7 +940,7 @@ COMMON_OBS = $(DEPFILES) $(CONFIG_OBS) $
 	reggroups.o regset.o \
 	trad-frame.o \
 	tramp-frame.o \
-	solib.o solib-null.o
+	solib.o solib-null.o gcore.o
 
 TSOBS = inflow.o
 
@@ -1971,9 +1971,9 @@ f-typeprint.o: f-typeprint.c $(defs_h) $
 f-valprint.o: f-valprint.c $(defs_h) $(gdb_string_h) $(symtab_h) \
 	$(gdbtypes_h) $(expression_h) $(value_h) $(valprint_h) $(language_h) \
 	$(f_lang_h) $(frame_h) $(gdbcore_h) $(command_h) $(block_h)
-gcore.o: gcore.c $(defs_h) $(elf_bfd_h) $(infcall_h) $(inferior_h) \
-	$(gdbcore_h) $(objfiles_h) $(symfile_h) $(cli_decode_h) \
-	$(gdb_assert_h)
+gcore.o: gcore.c $(defs_h) $(inferior_h) $(target_h) $(command_h)
+gcore-elf.o: gcore-elf.c $(defs_h) $(elf_bfd_h) $(infcall_h) \
+        $(inferior_h) $(gdbcore_h) $(gdb_assert_h)
 gdbarch.o: gdbarch.c $(defs_h) $(arch_utils_h) $(gdbcmd_h) $(inferior_h) \
 	$(symcat_h) $(floatformat_h) $(gdb_assert_h) $(gdb_string_h) \
 	$(gdb_events_h) $(reggroups_h) $(osabi_h) $(gdb_obstack_h)
Index: inf-ttrace.c
===================================================================
RCS file: /cvs/src/src/gdb/inf-ttrace.c,v
retrieving revision 1.18
diff -u -p -r1.18 inf-ttrace.c
--- inf-ttrace.c	23 Dec 2005 20:51:35 -0000	1.18
+++ inf-ttrace.c	10 Jan 2006 16:02:46 -0000
@@ -1116,6 +1116,31 @@ inf_ttrace_pid_to_str (ptid_t ptid)
 
   return normal_pid_to_str (ptid);
 }
+
+static void
+inf_ttrace_write_core_file (int pid, const char *filename)
+{
+  int status;
+  char default_filename[64];
+
+  /* Certain versions of HP/UX do not support the specification of the core
+     file name.  To work around this, we simply do not specify it, let
+     the kernel dump the core file in the default file name, and we then
+     rename it.  */
+  status = ttrace (TT_PROC_CORE, pid, 0, 0, 0, 0);
+  
+  if (status == -1)
+    perror_with_name ("ttrace");
+
+  sprintf (default_filename, "core.%d", pid); 
+  status = rename (default_filename, filename);
+
+  if (status != 0)
+    perror_with_name ("Unable to save core file");
+  else
+    fprintf_filtered (gdb_stdout, "Saved corefile %s\n", filename);
+} 
+
 
 
 struct target_ops *
@@ -1140,6 +1165,7 @@ inf_ttrace_target (void)
   t->to_mourn_inferior = inf_ttrace_mourn_inferior;
   t->to_thread_alive = inf_ttrace_thread_alive;
   t->to_pid_to_str = inf_ttrace_pid_to_str;
+  t->to_write_core_file = inf_ttrace_write_core_file;
   t->to_xfer_partial = inf_ttrace_xfer_partial;
 
   ttrace_ops_hack = t;
Index: config/alpha/alpha-linux.mh
===================================================================
RCS file: /cvs/src/src/gdb/config/alpha/alpha-linux.mh,v
retrieving revision 1.19
diff -u -p -r1.19 alpha-linux.mh
--- config/alpha/alpha-linux.mh	4 Jan 2006 19:33:09 -0000	1.19
+++ config/alpha/alpha-linux.mh	10 Jan 2006 16:03:38 -0000
@@ -1,7 +1,7 @@
 # Host: Little-endian Alpha running Linux
 NAT_FILE= nm-linux.h
 NATDEPFILES= inf-ptrace.o corelow.o alpha-nat.o alpha-linux-nat.o \
-	fork-child.o proc-service.o linux-thread-db.o gcore.o \
+	fork-child.o proc-service.o linux-thread-db.o gcore-elf.o \
 	linux-nat.o linux-fork.o
 
 # The dynamically loaded libthread_db needs access to symbols in the
Index: config/alpha/fbsd.mh
===================================================================
RCS file: /cvs/src/src/gdb/config/alpha/fbsd.mh,v
retrieving revision 1.7
diff -u -p -r1.7 fbsd.mh
--- config/alpha/fbsd.mh	5 Mar 2005 14:36:10 -0000	1.7
+++ config/alpha/fbsd.mh	10 Jan 2006 16:03:38 -0000
@@ -1,6 +1,6 @@
 # Host: FreeBSD/alpha
 NATDEPFILES= fork-child.o inf-ptrace.o \
 	fbsd-nat.o alphabsd-nat.o \
-	gcore.o solib.o solib-svr4.o solib-legacy.o \
+	gcore-elf.o solib.o solib-svr4.o solib-legacy.o \
 	corelow.o core-regset.o
 NAT_FILE= nm-fbsd.h
Index: config/arm/linux.mh
===================================================================
RCS file: /cvs/src/src/gdb/config/arm/linux.mh,v
retrieving revision 1.17
diff -u -p -r1.17 linux.mh
--- config/arm/linux.mh	4 Jan 2006 19:33:09 -0000	1.17
+++ config/arm/linux.mh	10 Jan 2006 16:03:38 -0000
@@ -2,7 +2,7 @@
 
 NAT_FILE= nm-linux.h
 NATDEPFILES= inf-ptrace.o fork-child.o corelow.o	\
-	core-regset.o arm-linux-nat.o gcore.o	\
+	core-regset.o arm-linux-nat.o gcore-elf.o	\
 	proc-service.o linux-thread-db.o linux-nat.o linux-fork.o
 
 LOADLIBES= -ldl -rdynamic
Index: config/i386/fbsd.mh
===================================================================
RCS file: /cvs/src/src/gdb/config/i386/fbsd.mh,v
retrieving revision 1.19
diff -u -p -r1.19 fbsd.mh
--- config/i386/fbsd.mh	1 Oct 2004 17:26:12 -0000	1.19
+++ config/i386/fbsd.mh	10 Jan 2006 16:03:38 -0000
@@ -1,7 +1,7 @@
 # Host: FreeBSD/i386
 NATDEPFILES= fork-child.o inf-ptrace.o \
 	fbsd-nat.o i386-nat.o i386bsd-nat.o i386fbsd-nat.o \
-	gcore.o bsd-kvm.o
+	gcore-elf.o bsd-kvm.o
 NAT_FILE= nm-fbsd.h
 
 LOADLIBES= -lkvm
Index: config/i386/fbsd64.mh
===================================================================
RCS file: /cvs/src/src/gdb/config/i386/fbsd64.mh,v
retrieving revision 1.11
diff -u -p -r1.11 fbsd64.mh
--- config/i386/fbsd64.mh	1 Oct 2004 17:26:13 -0000	1.11
+++ config/i386/fbsd64.mh	10 Jan 2006 16:03:38 -0000
@@ -1,6 +1,6 @@
 # Host: FreeBSD/amd64
 NATDEPFILES= fork-child.o inf-ptrace.o \
 	fbsd-nat.o amd64-nat.o amd64bsd-nat.o amd64fbsd-nat.o \
-	gcore.o bsd-kvm.o
+	gcore-elf.o bsd-kvm.o
 
 LOADLIBES= -lkvm
Index: config/i386/i386sol2.mh
===================================================================
RCS file: /cvs/src/src/gdb/config/i386/i386sol2.mh,v
retrieving revision 1.11
diff -u -p -r1.11 i386sol2.mh
--- config/i386/i386sol2.mh	31 Oct 2004 15:35:08 -0000	1.11
+++ config/i386/i386sol2.mh	10 Jan 2006 16:03:38 -0000
@@ -1,4 +1,4 @@
 # Host: Solaris x86
 NATDEPFILES= fork-child.o i386v4-nat.o i386-sol2-nat.o \
-	procfs.o proc-api.o proc-events.o proc-flags.o proc-why.o gcore.o
+	procfs.o proc-api.o proc-events.o proc-flags.o proc-why.o gcore-elf.o
 NAT_FILE= nm-i386sol2.h
Index: config/i386/i386v42mp.mh
===================================================================
RCS file: /cvs/src/src/gdb/config/i386/i386v42mp.mh,v
retrieving revision 1.9
diff -u -p -r1.9 i386v42mp.mh
--- config/i386/i386v42mp.mh	5 Aug 2004 15:23:53 -0000	1.9
+++ config/i386/i386v42mp.mh	10 Jan 2006 16:03:38 -0000
@@ -11,6 +11,6 @@ NAT_FILE= nm-i386v42mp.h
 # consequence, make considers subsequent tab-indented lines to be
 # some sort of error.
 NATDEPFILES= corelow.o core-regset.o fork-child.o i386v4-nat.o \
-	gcore.o solib.o solib-svr4.o solib-legacy.o procfs.o proc-api.o \
+	gcore-elf.o solib.o solib-svr4.o solib-legacy.o procfs.o proc-api.o \
 	proc-events.o proc-flags.o proc-why.o uw-thread.o
 
Index: config/i386/linux.mh
===================================================================
RCS file: /cvs/src/src/gdb/config/i386/linux.mh,v
retrieving revision 1.19
diff -u -p -r1.19 linux.mh
--- config/i386/linux.mh	4 Jan 2006 19:33:11 -0000	1.19
+++ config/i386/linux.mh	10 Jan 2006 16:03:38 -0000
@@ -3,7 +3,7 @@
 NAT_FILE= nm-linux.h
 NATDEPFILES= inf-ptrace.o fork-child.o corelow.o \
 	core-aout.o i386-nat.o i386-linux-nat.o \
-	proc-service.o linux-thread-db.o gcore.o \
+	proc-service.o linux-thread-db.o gcore-elf.o \
 	linux-nat.o linux-fork.o
 
 # The dynamically loaded libthread_db needs access to symbols in the
Index: config/i386/linux64.mh
===================================================================
RCS file: /cvs/src/src/gdb/config/i386/linux64.mh,v
retrieving revision 1.7
diff -u -p -r1.7 linux64.mh
--- config/i386/linux64.mh	4 Jan 2006 19:33:11 -0000	1.7
+++ config/i386/linux64.mh	10 Jan 2006 16:03:38 -0000
@@ -1,7 +1,7 @@
 # Host: GNU/Linux x86-64
 NATDEPFILES= inf-ptrace.o fork-child.o \
 	i386-nat.o amd64-nat.o amd64-linux-nat.o linux-nat.o \
-	proc-service.o linux-thread-db.o gcore.o  linux-fork.o
+	proc-service.o linux-thread-db.o gcore-elf.o  linux-fork.o
 NAT_FILE= nm-linux64.h
 
 # The dynamically loaded libthread_db needs access to symbols in the
Index: config/i386/sol2-64.mh
===================================================================
RCS file: /cvs/src/src/gdb/config/i386/sol2-64.mh,v
retrieving revision 1.1
diff -u -p -r1.1 sol2-64.mh
--- config/i386/sol2-64.mh	1 Nov 2004 21:05:56 -0000	1.1
+++ config/i386/sol2-64.mh	10 Jan 2006 16:03:39 -0000
@@ -1,4 +1,4 @@
 # Host: Solaris x86_64
 NATDEPFILES= fork-child.o amd64-nat.o i386v4-nat.o i386-sol2-nat.o \
-	procfs.o proc-api.o proc-events.o proc-flags.o proc-why.o gcore.o
+	procfs.o proc-api.o proc-events.o proc-flags.o proc-why.o gcore-elf.o
 NAT_FILE= nm-i386sol2.h
Index: config/ia64/linux.mh
===================================================================
RCS file: /cvs/src/src/gdb/config/ia64/linux.mh,v
retrieving revision 1.20
diff -u -p -r1.20 linux.mh
--- config/ia64/linux.mh	4 Jan 2006 19:33:11 -0000	1.20
+++ config/ia64/linux.mh	10 Jan 2006 16:03:39 -0000
@@ -1,7 +1,7 @@
 # Host: Intel IA-64 running GNU/Linux
 
 NAT_FILE= nm-linux.h
-NATDEPFILES= inf-ptrace.o fork-child.o corelow.o gcore.o \
+NATDEPFILES= inf-ptrace.o fork-child.o corelow.o gcore-elf.o \
 	core-aout.o core-regset.o ia64-linux-nat.o \
 	proc-service.o linux-thread-db.o linux-nat.o linux-fork.o
 
Index: config/m32r/linux.mh
===================================================================
RCS file: /cvs/src/src/gdb/config/m32r/linux.mh,v
retrieving revision 1.4
diff -u -p -r1.4 linux.mh
--- config/m32r/linux.mh	4 Jan 2006 19:33:11 -0000	1.4
+++ config/m32r/linux.mh	10 Jan 2006 16:03:39 -0000
@@ -2,7 +2,7 @@
 
 NAT_FILE= nm-linux.h
 NATDEPFILES= inf-ptrace.o fork-child.o corelow.o	\
-	m32r-linux-nat.o gcore.o proc-service.o linux-thread-db.o	\
+	m32r-linux-nat.o gcore-elf.o proc-service.o linux-thread-db.o	\
 	linux-nat.o linux-fork.o
 
 LOADLIBES= -ldl -rdynamic
Index: config/m68k/linux.mh
===================================================================
RCS file: /cvs/src/src/gdb/config/m68k/linux.mh,v
retrieving revision 1.18
diff -u -p -r1.18 linux.mh
--- config/m68k/linux.mh	4 Jan 2006 19:33:11 -0000	1.18
+++ config/m68k/linux.mh	10 Jan 2006 16:03:39 -0000
@@ -2,7 +2,7 @@
 
 NAT_FILE= nm-linux.h
 NATDEPFILES= inf-ptrace.o fork-child.o \
-	corelow.o core-aout.o m68klinux-nat.o gcore.o \
+	corelow.o core-aout.o m68klinux-nat.o gcore-elf.o \
 	proc-service.o linux-thread-db.o linux-nat.o linux-fork.o
 
 # The dynamically loaded libthread_db needs access to symbols in the
Index: config/mips/linux.mh
===================================================================
RCS file: /cvs/src/src/gdb/config/mips/linux.mh,v
retrieving revision 1.11
diff -u -p -r1.11 linux.mh
--- config/mips/linux.mh	4 Jan 2006 19:33:12 -0000	1.11
+++ config/mips/linux.mh	10 Jan 2006 16:03:39 -0000
@@ -1,7 +1,7 @@
 # Host: Linux/MIPS
 NAT_FILE= nm-linux.h
 NATDEPFILES= inf-ptrace.o fork-child.o mips-linux-nat.o \
-	linux-thread-db.o proc-service.o gcore.o \
+	linux-thread-db.o proc-service.o gcore-elf.o \
 	linux-nat.o linux-fork.o
 
 LOADLIBES = -ldl -rdynamic
Index: config/pa/linux.mh
===================================================================
RCS file: /cvs/src/src/gdb/config/pa/linux.mh,v
retrieving revision 1.7
diff -u -p -r1.7 linux.mh
--- config/pa/linux.mh	4 Jan 2006 19:33:12 -0000	1.7
+++ config/pa/linux.mh	10 Jan 2006 16:03:39 -0000
@@ -1,7 +1,7 @@
 # Host: Hewlett-Packard PA-RISC machine, running Linux
 XDEPFILES=
 NAT_FILE= nm-linux.h
-NATDEPFILES= inf-ptrace.o fork-child.o corelow.o gcore.o \
+NATDEPFILES= inf-ptrace.o fork-child.o corelow.o gcore-elf.o \
 	core-regset.o hppa-linux-nat.o \
 	proc-service.o linux-thread-db.o linux-nat.o linux-fork.o
 
Index: config/powerpc/linux.mh
===================================================================
RCS file: /cvs/src/src/gdb/config/powerpc/linux.mh,v
retrieving revision 1.20
diff -u -p -r1.20 linux.mh
--- config/powerpc/linux.mh	4 Jan 2006 19:33:12 -0000	1.20
+++ config/powerpc/linux.mh	10 Jan 2006 16:03:39 -0000
@@ -5,6 +5,6 @@ XM_CLIBS=
 NAT_FILE= nm-linux.h
 NATDEPFILES= inf-ptrace.o fork-child.o \
 	ppc-linux-nat.o proc-service.o linux-thread-db.o \
-	gcore.o linux-nat.o linux-fork.o
+	gcore-elf.o linux-nat.o linux-fork.o
 
 LOADLIBES = -ldl -rdynamic
Index: config/powerpc/ppc64-linux.mh
===================================================================
RCS file: /cvs/src/src/gdb/config/powerpc/ppc64-linux.mh,v
retrieving revision 1.9
diff -u -p -r1.9 ppc64-linux.mh
--- config/powerpc/ppc64-linux.mh	4 Jan 2006 19:33:12 -0000	1.9
+++ config/powerpc/ppc64-linux.mh	10 Jan 2006 16:03:39 -0000
@@ -5,7 +5,7 @@ XM_CLIBS=
 NAT_FILE= nm-ppc64-linux.h
 NATDEPFILES= inf-ptrace.o fork-child.o \
 	ppc-linux-nat.o proc-service.o linux-thread-db.o \
-	gcore.o linux-nat.o linux-fork.o
+	gcore-elf.o linux-nat.o linux-fork.o
 
 # The PowerPC has severe limitations on TOC size, and uses them even
 # for non-PIC code.  GDB overflows those tables when compiling with
Index: config/s390/s390.mh
===================================================================
RCS file: /cvs/src/src/gdb/config/s390/s390.mh,v
retrieving revision 1.13
diff -u -p -r1.13 s390.mh
--- config/s390/s390.mh	10 Sep 2005 18:11:13 -0000	1.13
+++ config/s390/s390.mh	10 Jan 2006 16:03:39 -0000
@@ -1,5 +1,5 @@
 # Host: S390, running Linux
 NAT_FILE= nm-linux.h
 NATDEPFILES= inf-ptrace.o fork-child.o corelow.o s390-nat.o \
-	gcore.o linux-thread-db.o proc-service.o linux-nat.o
+	gcore-elf.o linux-thread-db.o proc-service.o linux-nat.o
 LOADLIBES = -ldl -rdynamic
Index: config/sparc/fbsd.mh
===================================================================
RCS file: /cvs/src/src/gdb/config/sparc/fbsd.mh,v
retrieving revision 1.9
diff -u -p -r1.9 fbsd.mh
--- config/sparc/fbsd.mh	15 Jul 2005 20:10:19 -0000	1.9
+++ config/sparc/fbsd.mh	10 Jan 2006 16:03:39 -0000
@@ -1,6 +1,6 @@
 # Host: FreeBSD/sparc64
 NATDEPFILES= fork-child.o inf-ptrace.o \
 	fbsd-nat.o sparc-nat.o sparc64-nat.o sparc64fbsd-nat.o \
-	gcore.o bsd-kvm.o
+	gcore-elf.o bsd-kvm.o
 
 LOADLIBES= -lkvm
Index: config/sparc/linux.mh
===================================================================
RCS file: /cvs/src/src/gdb/config/sparc/linux.mh,v
retrieving revision 1.17
diff -u -p -r1.17 linux.mh
--- config/sparc/linux.mh	4 Jan 2006 19:33:12 -0000	1.17
+++ config/sparc/linux.mh	10 Jan 2006 16:03:39 -0000
@@ -3,7 +3,7 @@ NAT_FILE= nm-linux.h
 NATDEPFILES= sparc-nat.o sparc-sol2-nat.o sparc-linux-nat.o \
 	corelow.o core-regset.o fork-child.o inf-ptrace.o \
 	proc-service.o linux-thread-db.o \
-	gcore.o linux-nat.o linux-fork.o
+	gcore-elf.o linux-nat.o linux-fork.o
 
 # The dynamically loaded libthread_db needs access to symbols in the
 # gdb executable.
Index: config/sparc/linux64.mh
===================================================================
RCS file: /cvs/src/src/gdb/config/sparc/linux64.mh,v
retrieving revision 1.8
diff -u -p -r1.8 linux64.mh
--- config/sparc/linux64.mh	4 Jan 2006 19:33:13 -0000	1.8
+++ config/sparc/linux64.mh	10 Jan 2006 16:03:39 -0000
@@ -4,7 +4,7 @@ NATDEPFILES= sparc-nat.o sparc64-nat.o s
 	corelow.o core-regset.o \
 	fork-child.o inf-ptrace.o \
 	proc-service.o linux-thread-db.o \
-	gcore.o linux-nat.o linux-fork.o
+	gcore-elf.o linux-nat.o linux-fork.o
 
 # The dynamically loaded libthread_db needs access to symbols in the
 # gdb executable.
Index: config/sparc/sol2.mh
===================================================================
RCS file: /cvs/src/src/gdb/config/sparc/sol2.mh,v
retrieving revision 1.3
diff -u -p -r1.3 sol2.mh
--- config/sparc/sol2.mh	7 Apr 2004 00:15:12 -0000	1.3
+++ config/sparc/sol2.mh	10 Jan 2006 16:03:39 -0000
@@ -1,5 +1,5 @@
 # Host: Solaris SPARC & UltraSPARC
 NAT_FILE= nm-sol2.h
 NATDEPFILES= sparc-sol2-nat.o \
-	corelow.o core-regset.o fork-child.o gcore.o \
+	corelow.o core-regset.o fork-child.o gcore-elf.o \
 	procfs.o proc-api.o proc-events.o proc-flags.o proc-why.o
-------------- next part --------------
/* Generate a core file for the inferior process.

   Copyright (C) 2006 Free Software Foundation, Inc.

   This file is part of GDB.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.  */

#include "defs.h"
#include "elf-bfd.h"
#include "infcall.h"
#include "inferior.h"
#include "gdbcore.h"
#include "objfiles.h"
#include "gdb_assert.h"

static 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 *);
static void gcore_elf_write_core_file (int pid, const char *corefilename);

/* Generate a core file from the inferior process.  */

static void
gcore_elf_write_core_file (int pid, const char *corefilename)
{
  struct cleanup *old_chain;
  asection *note_sec = NULL;
  bfd *obfd;
  void *note_data = NULL;
  int note_size = 0;

  if (info_verbose)
    fprintf_filtered (gdb_stdout,
		      "Opening corefile '%s' for output.\n", corefilename);

  /* Open the output file.  */
  obfd = bfd_openw (corefilename, default_gcore_target ());
  if (!obfd)
    error (_("Failed to open '%s' for output."), corefilename);

  /* Need a cleanup that will close the file (FIXME: delete it?).  */
  old_chain = make_cleanup_bfd_close (obfd);

  bfd_set_format (obfd, bfd_core);
  bfd_set_arch_mach (obfd, default_gcore_arch (), default_gcore_mach ());

  /* An external target method must build the notes section.  */
  note_data = target_make_corefile_notes (obfd, &note_size);

  /* Create the note section.  */
  if (note_data != NULL && note_size != 0)
    {
      note_sec = bfd_make_section_anyway (obfd, "note0");
      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_flags (obfd, note_sec,
			     SEC_HAS_CONTENTS | SEC_READONLY | SEC_ALLOC);
      bfd_set_section_alignment (obfd, note_sec, 0);
      bfd_set_section_size (obfd, note_sec, note_size);
    }

  /* Now create the memory/load sections.  */
  if (gcore_memory_sections (obfd) == 0)
    error (_("gcore: failed to get corefile memory sections from target."));

  /* Write out the contents of the note section.  */
  if (note_data != NULL && note_size != 0)
    {
      if (!bfd_set_section_contents (obfd, note_sec, note_data, 0, note_size))
	warning (_("writing note section (%s)"), bfd_errmsg (bfd_get_error ()));
    }

  /* Succeeded.  */
  fprintf_filtered (gdb_stdout, "Saved corefile %s\n", corefilename);

  /* Clean-ups will close the output file and free malloc memory.  */
  do_cleanups (old_chain);
  return;
}

static unsigned long
default_gcore_mach (void)
{
#if 1	/* See if this even matters...  */
  return 0;
#else
#ifdef TARGET_ARCHITECTURE
  const struct bfd_arch_info *bfdarch = TARGET_ARCHITECTURE;

  if (bfdarch != NULL)
    return bfdarch->mach;
#endif /* TARGET_ARCHITECTURE */
  if (exec_bfd == NULL)
    error (_("Can't find default bfd machine type (need execfile)."));

  return bfd_get_mach (exec_bfd);
#endif /* 1 */
}

static enum bfd_architecture
default_gcore_arch (void)
{
#ifdef TARGET_ARCHITECTURE
  const struct bfd_arch_info * bfdarch = TARGET_ARCHITECTURE;

  if (bfdarch != NULL)
    return bfdarch->arch;
#endif
  if (exec_bfd == NULL)
    error (_("Can't find bfd architecture for corefile (need execfile)."));

  return bfd_get_arch (exec_bfd);
}

static char *
default_gcore_target (void)
{
  /* FIXME: This may only work for ELF targets.  */
  if (exec_bfd == NULL)
    return NULL;
  else
    return bfd_get_target (exec_bfd);
}

/* Derive a reasonable stack segment by unwinding the target stack,
   and store its limits in *BOTTOM and *TOP.  Return non-zero if
   successful.  */

static int
derive_stack_segment (bfd_vma *bottom, bfd_vma *top)
{
  struct frame_info *fi, *tmp_fi;

  gdb_assert (bottom);
  gdb_assert (top);

  /* Can't succeed without stack and registers.  */
  if (!target_has_stack || !target_has_registers)
    return 0;

  /* Can't succeed without current frame.  */
  fi = get_current_frame ();
  if (fi == NULL)
    return 0;

  /* Save frame pointer of TOS frame.  */
  *top = get_frame_base (fi);
  /* If current stack pointer is more "inner", use that instead.  */
  if (INNER_THAN (read_sp (), *top))
    *top = read_sp ();

  /* Find prev-most frame.  */
  while ((tmp_fi = get_prev_frame (fi)) != NULL)
    fi = tmp_fi;

  /* Save frame pointer of prev-most frame.  */
  *bottom = get_frame_base (fi);

  /* Now canonicalize their order, so that BOTTOM is a lower address
     (as opposed to a lower stack frame).  */
  if (*bottom > *top)
    {
      bfd_vma tmp_vma;

      tmp_vma = *top;
      *top = *bottom;
      *bottom = tmp_vma;
    }

  return 1;
}

/* Derive a reasonable heap segment for ABFD by looking at sbrk and
   the static data sections.  Store its limits in *BOTTOM and *TOP.
   Return non-zero if successful.  */

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;
  struct value *zero, *sbrk;
  bfd_vma sec_vaddr;
  asection *sec;

  gdb_assert (bottom);
  gdb_assert (top);

  /* This function depends on being able to call a function in the
     inferior.  */
  if (!target_has_execution)
    return 0;

  /* The following code assumes that the link map is arranged as
     follows (low to high addresses):

     ---------------------------------
     | text sections                 |
     ---------------------------------
     | data sections (including bss) |
     ---------------------------------
     | heap                          |
     --------------------------------- */

  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;
	}
    }

  /* Now get the top-of-heap by calling sbrk in the inferior.  */
  if (lookup_minimal_symbol ("sbrk", NULL, NULL) != NULL)
    {
      sbrk = find_function_in_inferior ("sbrk");
      if (sbrk == NULL)
	return 0;
    }
  else if (lookup_minimal_symbol ("_sbrk", NULL, NULL) != NULL)
    {
      sbrk = find_function_in_inferior ("_sbrk");
      if (sbrk == NULL)
	return 0;
    }
  else
    return 0;

  zero = value_from_longest (builtin_type_int, 0);
  gdb_assert (zero);
  sbrk = call_function_by_hand (sbrk, 1, &zero);
  if (sbrk == NULL)
    return 0;
  top_of_heap = value_as_long (sbrk);

  /* Return results.  */
  if (top_of_heap > top_of_data_memory)
    {
      *bottom = top_of_data_memory;
      *top = top_of_heap;
      return 1;
    }

  /* No additional heap space needs to be saved.  */
  return 0;
}

static void
make_output_phdrs (bfd *obfd, asection *osec, void *ignored)
{
  int p_flags = 0;
  int p_type;

  /* FIXME: these constants may only be applicable for ELF.  */
  if (strncmp (bfd_section_name (obfd, osec), "load", 4) == 0)
    p_type = PT_LOAD;
  else
    p_type = PT_NOTE;

  p_flags |= PF_R;	/* Segment is readable.  */
  if (!(bfd_get_section_flags (obfd, osec) & SEC_READONLY))
    p_flags |= PF_W;	/* Segment is writable.  */
  if (bfd_get_section_flags (obfd, osec) & SEC_CODE)
    p_flags |= PF_X;	/* Segment is executable.  */

  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, void *data)
{
  bfd *obfd = data;
  asection *osec;
  flagword flags = SEC_ALLOC | SEC_HAS_CONTENTS | SEC_LOAD;

  /* If the memory segment has no permissions set, ignore it, otherwise
     when we later try to access it for read/write, we'll get an error
     or jam the kernel.  */
  if (read == 0 && write == 0 && exec == 0)
    {
      if (info_verbose)
        {
          fprintf_filtered (gdb_stdout, "Ignore segment, %s bytes at 0x%s\n",
                           paddr_d (size), paddr_nz (vaddr));
        }

      return 0;
    }

  if (write == 0)
    {
      /* See if this region of memory lies inside a known file on disk.
	 If so, we can avoid copying its contents by clearing SEC_LOAD.  */
      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 = objsec->addr & -align;
	  bfd_vma end = (objsec->endaddr + align - 1) & -align;
	  /* Match if either the entire memory region lies inside the
	     section (i.e. a mapping covering some pages of a large
	     segment) or the entire section lies inside the memory region
	     (i.e. a mapping covering multiple small sections).

	     This BFD was synthesized from reading target memory,
	     we don't want to omit that.  */
	  if (((vaddr >= start && vaddr + size <= end)
	       || (start >= vaddr && end <= vaddr + size))
	      && !(bfd_get_file_flags (abfd) & BFD_IN_MEMORY))
	    {
	      flags &= ~SEC_LOAD;
	      goto keep;	/* break out of two nested for loops */
	    }
	}

    keep:
      flags |= SEC_READONLY;
    }

  if (exec)
    flags |= SEC_CODE;
  else
    flags |= SEC_DATA;

  osec = bfd_make_section_anyway (obfd, "load");
  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 0x%s\n",
			paddr_d (size), paddr_nz (vaddr));
    }

  bfd_set_section_size (obfd, osec, size);
  bfd_set_section_vma (obfd, osec, vaddr);
  bfd_section_lma (obfd, osec) = 0; /* ??? bfd_set_section_lma?  */
  bfd_set_section_flags (obfd, osec, flags);
  return 0;
}

static int
objfile_find_memory_regions (int (*func) (CORE_ADDR, unsigned long,
					  int, int, int, void *),
			     void *obfd)
{
  /* Use objfile data to create memory sections.  */
  struct objfile *objfile;
  struct obj_section *objsec;
  bfd_vma temp_bottom, temp_top;

  /* Call callback function for each objfile section.  */
  ALL_OBJSECTIONS (objfile, objsec)
    {
      bfd *ibfd = objfile->obfd;
      asection *isec = objsec->the_bfd_section;
      flagword flags = bfd_get_section_flags (ibfd, isec);
      int ret;

      if ((flags & SEC_ALLOC) || (flags & SEC_LOAD))
	{
	  int size = bfd_section_size (ibfd, isec);
	  int ret;

	  ret = (*func) (objsec->addr, bfd_section_size (ibfd, isec),
			 1, /* All sections will be readable.  */
			 (flags & SEC_READONLY) == 0, /* Writable.  */
			 (flags & SEC_CODE) != 0, /* Executable.  */
			 obfd);
	  if (ret != 0)
	    return ret;
	}
    }

  /* Make a stack segment.  */
  if (derive_stack_segment (&temp_bottom, &temp_top))
    (*func) (temp_bottom, temp_top - temp_bottom,
	     1, /* Stack section will be readable.  */
	     1, /* Stack section will be writable.  */
	     0, /* Stack section will not be executable.  */
	     obfd);

  /* Make a heap segment. */
  if (derive_heap_segment (exec_bfd, &temp_bottom, &temp_top))
    (*func) (temp_bottom, temp_top - temp_bottom,
	     1, /* Heap section will be readable.  */
	     1, /* Heap section will be writable.  */
	     0, /* Heap section will not be executable.  */
	     obfd);

  return 0;
}

static void
gcore_copy_callback (bfd *obfd, asection *osec, void *ignored)
{
  bfd_size_type size = bfd_section_size (obfd, osec);
  struct cleanup *old_chain = NULL;
  void *memhunk;

  /* Read-only sections are marked; we don't have to copy their contents.  */
  if ((bfd_get_section_flags (obfd, osec) & SEC_LOAD) == 0)
    return;

  /* Only interested in "load" sections.  */
  if (strncmp ("load", bfd_section_name (obfd, osec), 4) != 0)
    return;

  memhunk = xmalloc (size);
  /* ??? This is crap since xmalloc should never return NULL.  */
  if (memhunk == NULL)
    error (_("Not enough memory to create corefile."));
  old_chain = make_cleanup (xfree, memhunk);

  if (target_read_memory (bfd_section_vma (obfd, osec),
			  memhunk, size) != 0)
    warning (_("Memory read failed for corefile section, %s bytes at 0x%s."),
	     paddr_d (size), paddr (bfd_section_vma (obfd, osec)));
  if (!bfd_set_section_contents (obfd, osec, memhunk, 0, size))
    warning (_("Failed to write corefile contents (%s)."),
	     bfd_errmsg (bfd_get_error ()));

  do_cleanups (old_chain);	/* Frees MEMHUNK.  */
}

static int
gcore_memory_sections (bfd *obfd)
{
  if (target_find_memory_regions (gcore_create_callback, obfd) != 0)
    return 0;			/* FIXME: error return/msg?  */

  /* Record phdrs for section-to-segment mapping.  */
  bfd_map_over_sections (obfd, make_output_phdrs, NULL);

  /* Copy memory region contents.  */
  bfd_map_over_sections (obfd, gcore_copy_callback, NULL);

  return 1;
}

void
_initialize_gcore_elf (void)
{
  exec_set_write_core_file (gcore_elf_write_core_file);
  exec_set_find_memory_regions (objfile_find_memory_regions);
}


More information about the Gdb-patches mailing list