2010-07-02 Ulrich Weigand <uweigand@de.ibm.com>
[binutils-gdb.git] / gdb / gcore.c
index 6d6ca47d146ed530c8acfecc6e0330af42836937..a38a5a357c804fb01e1551688989749f5b7bfb07 100644 (file)
@@ -1,6 +1,6 @@
 /* Generate a core file for the inferior process.
 
-   Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
+   Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
    Free Software Foundation, Inc.
 
    This file is part of GDB.
@@ -52,6 +52,7 @@ bfd *
 create_gcore_bfd (char *filename)
 {
   bfd *obfd = bfd_openw (filename, default_gcore_target ());
+
   if (!obfd)
     error (_("Failed to open '%s' for output."), filename);
   bfd_set_format (obfd, bfd_core);
@@ -102,6 +103,16 @@ write_gcore_file (bfd *obfd)
     }
 }
 
+static void
+do_bfd_delete_cleanup (void *arg)
+{
+  bfd *obfd = arg;
+  const char *filename = obfd->filename;
+
+  bfd_close (arg);
+  unlink (filename);
+}
+
 /* gcore_command -- implements the 'gcore' command.
    Generate a core file from the inferior process.  */
 
@@ -132,8 +143,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 +152,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);
+  bfd_close (obfd);
 }
 
 static unsigned long
@@ -291,7 +301,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 +414,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
@@ -467,14 +477,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.  */
@@ -521,9 +530,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)