/* Generate a core file for the inferior process.
- Copyright (C) 2001-2017 Free Software Foundation, Inc.
+ Copyright (C) 2001-2019 Free Software Foundation, Inc.
This file is part of GDB.
#include "readline/tilde.h"
#include <algorithm>
#include "common/gdb_unlinker.h"
-#include "byte-vector.h"
+#include "common/byte-vector.h"
+#include "common/scope-exit.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
static void
write_gcore_file_1 (bfd *obfd)
{
- struct cleanup *cleanup;
- void *note_data = NULL;
+ gdb::unique_xmalloc_ptr<char> note_data;
int note_size = 0;
asection *note_sec = NULL;
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);
+ note_data.reset (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);
+ note_data.reset (gdbarch_make_corefile_notes (target_gdbarch (), obfd,
+ ¬e_size));
if (note_data == NULL || note_size == 0)
error (_("Target does not support core file generation."));
error (_("gcore: failed to get corefile memory sections from target."));
/* Write out the contents of the note section. */
- if (!bfd_set_section_contents (obfd, note_sec, note_data, 0, note_size))
+ if (!bfd_set_section_contents (obfd, note_sec, note_data.get (), 0,
+ note_size))
warning (_("writing note section (%s)"), bfd_errmsg (bfd_get_error ()));
-
- do_cleanups (cleanup);
}
/* write_gcore_file -- helper for gcore_command (exported).
void
write_gcore_file (bfd *obfd)
{
- struct gdb_exception except = exception_none;
-
target_prepare_to_generate_core ();
-
- TRY
- {
- write_gcore_file_1 (obfd);
- }
- CATCH (e, RETURN_MASK_ALL)
- {
- except = e;
- }
- END_CATCH
-
- target_done_generating_core ();
-
- if (except.reason < 0)
- throw_exception (except);
+ SCOPE_EXIT { target_done_generating_core (); };
+ write_gcore_file_1 (obfd);
}
/* gcore_command -- implements the 'gcore' command.
else
{
/* Default corefile name is "core.PID". */
- corefilename.reset (xstrprintf ("core.%d", ptid_get_pid (inferior_ptid)));
+ corefilename.reset (xstrprintf ("core.%d", inferior_ptid.pid ()));
}
if (info_verbose)
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, NULL, 1, &target_sbrk_arg);
+ ret = call_function_by_hand (sbrk_fn, NULL, target_sbrk_arg);
if (ret == NULL)
return (bfd_vma) 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 = 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
- (i.e. a mapping covering multiple small sections).
-
- This BFD was synthesized from reading target memory,
- we don't want to omit that. */
- 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; /* Break out of two nested for loops. */
- }
- }
+ for (objfile *objfile : current_program_space->objfiles ())
+ ALL_OBJFILE_OSECTIONS (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;
+
+ /* 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 (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; /* Break out of two nested for loops. */
+ }
+ }
keep:;
}
find_memory_region_ftype func, 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);
-
- /* Separate debug info files are irrelevant for gcore. */
- 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, /* All sections will be readable. */
- (flags & SEC_READONLY) == 0, /* Writable. */
- (flags & SEC_CODE) != 0, /* Executable. */
- 1, /* MODIFIED is unknown, pass it as true. */
- obfd);
- if (ret != 0)
- return ret;
- }
- }
+ for (objfile *objfile : current_program_space->objfiles ())
+ ALL_OBJFILE_OSECTIONS (objfile, objsec)
+ {
+ bfd *ibfd = objfile->obfd;
+ asection *isec = objsec->the_bfd_section;
+ flagword flags = bfd_get_section_flags (ibfd, isec);
+
+ /* Separate debug info files are irrelevant for gcore. */
+ 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, /* All sections will be readable. */
+ (flags & SEC_READONLY) == 0, /* Writable. */
+ (flags & SEC_CODE) != 0, /* Executable. */
+ 1, /* MODIFIED is unknown, pass it as true. */
+ obfd);
+ if (ret != 0)
+ return ret;
+ }
+ }
/* Make a stack segment. */
if (derive_stack_segment (&temp_bottom, &temp_top))
{
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>'."));
+Usage: generate-core-file [FILENAME]\n\
+Argument is optional filename. Default filename is 'core.PROCESS_ID'."));
add_com_alias ("gcore", "generate-core-file", class_files, 1);
}