/* Generate a core file for the inferior process.
- Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
- 2011 Free Software Foundation, Inc.
+ Copyright (C) 2001-2012 Free Software Foundation, Inc.
This file is part of GDB.
#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
bfd *
create_gcore_bfd (char *filename)
{
- bfd *obfd = bfd_openw (filename, default_gcore_target ());
+ bfd *obfd = gdb_bfd_ref (bfd_openw (filename, default_gcore_target ()));
if (!obfd)
error (_("Failed to open '%s' for output."), filename);
+ gdb_bfd_stash_filename (obfd);
bfd_set_format (obfd, bfd_core);
bfd_set_arch_mach (obfd, default_gcore_arch (), default_gcore_mach ());
return obfd;
asection *note_sec = NULL;
/* An external target method must build the notes section. */
- note_data = target_make_corefile_notes (obfd, ¬e_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, ¬e_size);
+ else
+ note_data = gdbarch_make_corefile_notes (target_gdbarch, obfd, ¬e_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
bfd *obfd = arg;
const char *filename = obfd->filename;
- bfd_close (arg);
+ gdb_bfd_unref (arg);
unlink (filename);
}
fprintf_filtered (gdb_stdout, "Saved corefile %s\n", corefilename);
discard_cleanups (old_chain);
- bfd_close (obfd);
+ gdb_bfd_unref (obfd);
}
static unsigned long
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);