* layout.cc (Layout::add_target_dynamic_tags): If
[deliverable/binutils-gdb.git] / gdb / gcore.c
index 6d6ca47d146ed530c8acfecc6e0330af42836937..a45e78773919a177922d21113976ca91fff89f7a 100644 (file)
@@ -1,7 +1,6 @@
 /* Generate a core file for the inferior process.
 
-   Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
-   Free Software Foundation, Inc.
+   Copyright (C) 2001-2012 Free Software Foundation, Inc.
 
    This file is part of GDB.
 
@@ -34,6 +33,7 @@
 #include <fcntl.h>
 #include "regcache.h"
 #include "regset.h"
+#include "gdb_bfd.h"
 
 /* The largest amount of memory to read from the target at once.  We
    must throttle it to limit the amount of memory used by GDB during
@@ -51,7 +51,8 @@ static int gcore_memory_sections (bfd *);
 bfd *
 create_gcore_bfd (char *filename)
 {
-  bfd *obfd = bfd_openw (filename, default_gcore_target ());
+  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);
@@ -71,35 +72,47 @@ write_gcore_file (bfd *obfd)
   asection *note_sec = NULL;
 
   /* An external target method must build the notes section.  */
-  note_data = target_make_corefile_notes (obfd, &note_size);
+  /* FIXME: uweigand/2011-10-06: All architectures that support core file
+     generation should be converted to gdbarch_make_corefile_notes; at that
+     point, the target vector method can be removed.  */
+  if (!gdbarch_make_corefile_notes_p (target_gdbarch))
+    note_data = target_make_corefile_notes (obfd, &note_size);
+  else
+    note_data = gdbarch_make_corefile_notes (target_gdbarch, obfd, &note_size);
 
-  /* Create the note section.  */
-  if (note_data != NULL && note_size != 0)
-    {
-      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 ()));
+  if (note_data == NULL || note_size == 0)
+    error (_("Target does not support core file generation."));
 
-      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);
-    }
+  /* Create the note section.  */
+  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);
 
   /* 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 ()));
-    }
+  if (!bfd_set_section_contents (obfd, note_sec, note_data, 0, note_size))
+    warning (_("writing note section (%s)"), bfd_errmsg (bfd_get_error ()));
+}
+
+static void
+do_bfd_delete_cleanup (void *arg)
+{
+  bfd *obfd = arg;
+  const char *filename = obfd->filename;
+
+  gdb_bfd_unref (arg);
+  unlink (filename);
 }
 
 /* gcore_command -- implements the 'gcore' command.
@@ -132,8 +145,8 @@ gcore_command (char *args, int from_tty)
   /* Open the output file.  */
   obfd = create_gcore_bfd (corefilename);
 
-  /* Need a cleanup that will close the file (FIXME: delete it?).  */
-  old_chain = make_cleanup_bfd_close (obfd);
+  /* Need a cleanup that will close and delete the file.  */
+  old_chain = make_cleanup (do_bfd_delete_cleanup, obfd);
 
   /* Call worker function.  */
   write_gcore_file (obfd);
@@ -141,9 +154,8 @@ gcore_command (char *args, int from_tty)
   /* 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;
+  discard_cleanups (old_chain);
+  gdb_bfd_unref (obfd);
 }
 
 static unsigned long
@@ -291,7 +303,6 @@ call_target_sbrk (int sbrk_arg)
 static int
 derive_heap_segment (bfd *abfd, bfd_vma *bottom, bfd_vma *top)
 {
-  struct gdbarch *gdbarch;
   bfd_vma top_of_data_memory = 0;
   bfd_vma top_of_heap = 0;
   bfd_size_type sec_size;
@@ -405,6 +416,7 @@ gcore_create_callback (CORE_ADDR vaddr, unsigned long size,
                                                                    asec);
          bfd_vma start = obj_section_addr (objsec) & -align;
          bfd_vma end = (obj_section_endaddr (objsec) + 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
@@ -416,9 +428,8 @@ gcore_create_callback (CORE_ADDR vaddr, unsigned long size,
               || (start >= vaddr && end <= vaddr + size))
              && !(bfd_get_file_flags (abfd) & BFD_IN_MEMORY))
            {
-             flags &= ~SEC_LOAD;
-             flags |= SEC_NEVER_LOAD;
-             goto keep;        /* break out of two nested for loops */
+             flags &= ~(SEC_LOAD | SEC_HAS_CONTENTS);
+             goto keep;        /* Break out of two nested for loops.  */
            }
        }
 
@@ -452,9 +463,7 @@ gcore_create_callback (CORE_ADDR vaddr, unsigned long size,
 }
 
 static int
-objfile_find_memory_regions (int (*func) (CORE_ADDR, unsigned long,
-                                         int, int, int, void *),
-                            void *obfd)
+objfile_find_memory_regions (find_memory_region_ftype func, void *obfd)
 {
   /* Use objfile data to create memory sections.  */
   struct objfile *objfile;
@@ -467,14 +476,13 @@ objfile_find_memory_regions (int (*func) (CORE_ADDR, unsigned long,
       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) (obj_section_addr (objsec), bfd_section_size (ibfd, isec),
+         ret = (*func) (obj_section_addr (objsec), size, 
                         1, /* All sections will be readable.  */
                         (flags & SEC_READONLY) == 0, /* Writable.  */
                         (flags & SEC_CODE) != 0, /* Executable.  */
@@ -492,7 +500,7 @@ objfile_find_memory_regions (int (*func) (CORE_ADDR, unsigned long,
             0, /* Stack section will not be executable.  */
             obfd);
 
-  /* Make a heap segment. */
+  /* 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.  */
@@ -521,9 +529,6 @@ gcore_copy_callback (bfd *obfd, asection *osec, void *ignored)
 
   size = min (total_size, MAX_COPY_BYTES);
   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);
 
   while (total_size > 0)
@@ -534,7 +539,8 @@ gcore_copy_callback (bfd *obfd, asection *osec, void *ignored)
       if (target_read_memory (bfd_section_vma (obfd, osec) + offset,
                              memhunk, size) != 0)
        {
-         warning (_("Memory read failed for corefile section, %s bytes at %s."),
+         warning (_("Memory read failed for corefile "
+                    "section, %s bytes at %s."),
                   plongest (size),
                   paddress (target_gdbarch, bfd_section_vma (obfd, osec)));
          break;
@@ -556,8 +562,14 @@ gcore_copy_callback (bfd *obfd, asection *osec, void *ignored)
 static int
 gcore_memory_sections (bfd *obfd)
 {
-  if (target_find_memory_regions (gcore_create_callback, obfd) != 0)
-    return 0;                  /* FIXME: error return/msg?  */
+  /* Try gdbarch method first, then fall back to target method.  */
+  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: error return/msg?  */
+    }
 
   /* Record phdrs for section-to-segment mapping.  */
   bfd_map_over_sections (obfd, make_output_phdrs, NULL);
This page took 0.027567 seconds and 4 git commands to generate.