aboutsummaryrefslogtreecommitdiff
path: root/gdb/fbsd-tdep.c
diff options
context:
space:
mode:
authorSimon Marchi <simon.marchi@polymtl.ca>2020-10-21 10:43:48 -0400
committerSimon Marchi <simon.marchi@efficios.com>2020-10-21 10:43:48 -0400
commitc21f37a889dbf57077981970a1b6167a0dcb39af (patch)
tree9fb1572976cf1664ed89cb7749d7b706c5edf6d7 /gdb/fbsd-tdep.c
parent4dbe16c8115cd9820cadba5300cbfe04f518269f (diff)
downloadgdb-c21f37a889dbf57077981970a1b6167a0dcb39af.zip
gdb-c21f37a889dbf57077981970a1b6167a0dcb39af.tar.gz
gdb-c21f37a889dbf57077981970a1b6167a0dcb39af.tar.bz2
gdb: make gdbarch_make_corefile_notes return a unique ptr
This patch starts by making the gdbarch_make_corefile_notes function return a gdb::unique_xmalloc_ptr<char> and takes care of the fallouts, mostly in linux-tdep.c and fbsd-tdep.c. The difficulty in these files is that they use the BFD API for writing core files, where you pass in a pointer to a malloc-ed buffer (or NULL in the beginning), it re-allocs it if needed, and returns you the possibly updated pointer. I therefore used this pattern everywhere: note_data.reset (elfcore_write_note (obfd, note_data.release (), ...) This hands over the ownership of note_data to the BFD function for the duration of the call, and then puts its back in note_data right after the call. gdb/ChangeLog: * gdbarch.sh (make_corefile_notes): Return unique pointer. * gdbarch.c: Re-generate. * gdbarch.h: Re-generate. * gcore.c (write_gcore_file_1): Adjust. * fbsd-tdep.c (struct fbsd_collect_regset_section_cb_data): Add constructor. <note_data>: Change type to unique pointer. <abort_iteration>: Change type to bool. (fbsd_collect_regset_section_cb): Adjust to unique pointer. (fbsd_collect_thread_registers): Return void, adjust. (struct fbsd_corefile_thread_data): Add construtor. <note_data>: Change type to unique pointer. (fbsd_corefile_thread): Adjust. (fbsd_make_corefile_notes): Return unique pointer, adjust. * linux-tdep.c (linux_make_mappings_corefile_notes): Change type to unique pointer, adjust. (struct linux_collect_regset_section_cb_data): Add constructor. <note_data>: Change type to unique pointer. <abort_iteration>: Change type to bool. (linux_collect_thread_registers): Return void, adjust. (struct linux_corefile_thread_data): Add constructor. <note_data>: Change type to unique pointer. (linux_corefile_thread): Adjust. (linux_make_corefile_notes): Return unique pointer, adjust. Change-Id: I1e03476bb47b87c6acb3e12204d193f38cc4e02b
Diffstat (limited to 'gdb/fbsd-tdep.c')
-rw-r--r--gdb/fbsd-tdep.c121
1 files changed, 72 insertions, 49 deletions
diff --git a/gdb/fbsd-tdep.c b/gdb/fbsd-tdep.c
index a462e4d..f7ee35b 100644
--- a/gdb/fbsd-tdep.c
+++ b/gdb/fbsd-tdep.c
@@ -589,13 +589,27 @@ find_signalled_thread (struct thread_info *info, void *data)
struct fbsd_collect_regset_section_cb_data
{
+ fbsd_collect_regset_section_cb_data (const struct regcache *regcache,
+ bfd *obfd,
+ gdb::unique_xmalloc_ptr<char> &note_data,
+ int *note_size,
+ unsigned long lwp,
+ gdb_signal stop_signal)
+ : regcache (regcache),
+ obfd (obfd),
+ note_data (note_data),
+ note_size (note_size),
+ lwp (lwp),
+ stop_signal (stop_signal)
+ {}
+
const struct regcache *regcache;
bfd *obfd;
- char *note_data;
+ gdb::unique_xmalloc_ptr<char> &note_data;
int *note_size;
unsigned long lwp;
enum gdb_signal stop_signal;
- int abort_iteration;
+ bool abort_iteration = false;
};
static void
@@ -617,50 +631,58 @@ fbsd_collect_regset_section_cb (const char *sect_name, int supply_size,
/* PRSTATUS still needs to be treated specially. */
if (strcmp (sect_name, ".reg") == 0)
- data->note_data = (char *) elfcore_write_prstatus
- (data->obfd, data->note_data, data->note_size, data->lwp,
- gdb_signal_to_host (data->stop_signal), buf);
+ data->note_data.reset (elfcore_write_prstatus
+ (data->obfd, data->note_data.release (),
+ data->note_size, data->lwp,
+ gdb_signal_to_host (data->stop_signal),
+ buf));
else
- data->note_data = (char *) elfcore_write_register_note
- (data->obfd, data->note_data, data->note_size,
- sect_name, buf, collect_size);
+ data->note_data.reset (elfcore_write_register_note
+ (data->obfd, data->note_data.release (),
+ data->note_size, sect_name, buf,
+ collect_size));
xfree (buf);
if (data->note_data == NULL)
- data->abort_iteration = 1;
+ data->abort_iteration = true;
}
/* Records the thread's register state for the corefile note
section. */
-static char *
+static void
fbsd_collect_thread_registers (const struct regcache *regcache,
ptid_t ptid, bfd *obfd,
- char *note_data, int *note_size,
+ gdb::unique_xmalloc_ptr<char> &note_data,
+ int *note_size,
enum gdb_signal stop_signal)
{
- struct gdbarch *gdbarch = regcache->arch ();
- struct fbsd_collect_regset_section_cb_data data;
-
- data.regcache = regcache;
- data.obfd = obfd;
- data.note_data = note_data;
- data.note_size = note_size;
- data.stop_signal = stop_signal;
- data.abort_iteration = 0;
- data.lwp = ptid.lwp ();
-
- gdbarch_iterate_over_regset_sections (gdbarch,
+ fbsd_collect_regset_section_cb_data data (regcache, obfd, note_data,
+ note_size, ptid.lwp (),
+ stop_signal);
+
+ gdbarch_iterate_over_regset_sections (regcache->arch (),
fbsd_collect_regset_section_cb,
&data, regcache);
- return data.note_data;
}
struct fbsd_corefile_thread_data
{
+ fbsd_corefile_thread_data (struct gdbarch *gdbarch,
+ bfd *obfd,
+ gdb::unique_xmalloc_ptr<char> &note_data,
+ int *note_size,
+ gdb_signal stop_signal)
+ : gdbarch (gdbarch),
+ obfd (obfd),
+ note_data (note_data),
+ note_size (note_size),
+ stop_signal (stop_signal)
+ {}
+
struct gdbarch *gdbarch;
bfd *obfd;
- char *note_data;
+ gdb::unique_xmalloc_ptr<char> &note_data;
int *note_size;
enum gdb_signal stop_signal;
};
@@ -679,9 +701,9 @@ fbsd_corefile_thread (struct thread_info *info,
target_fetch_registers (regcache, -1);
- args->note_data = fbsd_collect_thread_registers
- (regcache, info->ptid, args->obfd, args->note_data,
- args->note_size, args->stop_signal);
+ fbsd_collect_thread_registers (regcache, info->ptid, args->obfd,
+ args->note_data, args->note_size,
+ args->stop_signal);
}
/* Return a byte_vector containing the contents of a core dump note
@@ -709,11 +731,10 @@ fbsd_make_note_desc (enum target_object object, uint32_t structsize)
/* Create appropriate note sections for a corefile, returning them in
allocated memory. */
-static char *
+static gdb::unique_xmalloc_ptr<char>
fbsd_make_corefile_notes (struct gdbarch *gdbarch, bfd *obfd, int *note_size)
{
- struct fbsd_corefile_thread_data thread_args;
- char *note_data = NULL;
+ gdb::unique_xmalloc_ptr<char> note_data;
Elf_Internal_Ehdr *i_ehdrp;
struct thread_info *curr_thr, *signalled_thr;
@@ -732,8 +753,9 @@ fbsd_make_corefile_notes (struct gdbarch *gdbarch, bfd *obfd, int *note_size)
if (infargs != NULL)
psargs = psargs + " " + infargs;
- note_data = elfcore_write_prpsinfo (obfd, note_data, note_size,
- fname, psargs.c_str ());
+ note_data.reset (elfcore_write_prpsinfo (obfd, note_data.release (),
+ note_size, fname,
+ psargs.c_str ()));
}
/* Thread register information. */
@@ -760,11 +782,8 @@ fbsd_make_corefile_notes (struct gdbarch *gdbarch, bfd *obfd, int *note_size)
signalled_thr = curr_thr;
}
- thread_args.gdbarch = gdbarch;
- thread_args.obfd = obfd;
- thread_args.note_data = note_data;
- thread_args.note_size = note_size;
- thread_args.stop_signal = signalled_thr->suspend.stop_signal;
+ fbsd_corefile_thread_data thread_args (gdbarch, obfd, note_data, note_size,
+ signalled_thr->suspend.stop_signal);
fbsd_corefile_thread (signalled_thr, &thread_args);
for (thread_info *thr : current_inferior ()->non_exited_threads ())
@@ -775,17 +794,17 @@ fbsd_make_corefile_notes (struct gdbarch *gdbarch, bfd *obfd, int *note_size)
fbsd_corefile_thread (thr, &thread_args);
}
- note_data = thread_args.note_data;
-
/* Auxiliary vector. */
uint32_t structsize = gdbarch_ptr_bit (gdbarch) / 4; /* Elf_Auxinfo */
gdb::optional<gdb::byte_vector> note_desc =
fbsd_make_note_desc (TARGET_OBJECT_AUXV, structsize);
if (note_desc && !note_desc->empty ())
{
- note_data = elfcore_write_note (obfd, note_data, note_size, "FreeBSD",
- NT_FREEBSD_PROCSTAT_AUXV,
- note_desc->data (), note_desc->size ());
+ note_data.reset (elfcore_write_note (obfd, note_data.release (),
+ note_size, "FreeBSD",
+ NT_FREEBSD_PROCSTAT_AUXV,
+ note_desc->data (),
+ note_desc->size ()));
if (!note_data)
return NULL;
}
@@ -794,9 +813,11 @@ fbsd_make_corefile_notes (struct gdbarch *gdbarch, bfd *obfd, int *note_size)
note_desc = fbsd_make_note_desc (TARGET_OBJECT_FREEBSD_VMMAP, 0);
if (note_desc && !note_desc->empty ())
{
- note_data = elfcore_write_note (obfd, note_data, note_size, "FreeBSD",
- NT_FREEBSD_PROCSTAT_VMMAP,
- note_desc->data (), note_desc->size ());
+ note_data.reset (elfcore_write_note (obfd, note_data.release (),
+ note_size, "FreeBSD",
+ NT_FREEBSD_PROCSTAT_VMMAP,
+ note_desc->data (),
+ note_desc->size ()));
if (!note_data)
return NULL;
}
@@ -804,9 +825,11 @@ fbsd_make_corefile_notes (struct gdbarch *gdbarch, bfd *obfd, int *note_size)
note_desc = fbsd_make_note_desc (TARGET_OBJECT_FREEBSD_PS_STRINGS, 0);
if (note_desc && !note_desc->empty ())
{
- note_data = elfcore_write_note (obfd, note_data, note_size, "FreeBSD",
- NT_FREEBSD_PROCSTAT_PSSTRINGS,
- note_desc->data (), note_desc->size ());
+ note_data.reset (elfcore_write_note (obfd, note_data.release (),
+ note_size, "FreeBSD",
+ NT_FREEBSD_PROCSTAT_PSSTRINGS,
+ note_desc->data (),
+ note_desc->size ()));
if (!note_data)
return NULL;
}