aboutsummaryrefslogtreecommitdiff
path: root/gdb/fbsd-tdep.c
diff options
context:
space:
mode:
authorAndrew Burgess <andrew.burgess@embecosm.com>2021-01-18 16:00:38 +0000
committerAndrew Burgess <andrew.burgess@embecosm.com>2021-03-05 17:21:40 +0000
commitf3a5df7bd65bea1840d00bd65d5eee7e339ca8da (patch)
tree54ab67d8645052ef01c0f03b4cc4105b69a9c5b5 /gdb/fbsd-tdep.c
parent2017f38777038db926239b9bbea738d270597b0e (diff)
downloadbinutils-f3a5df7bd65bea1840d00bd65d5eee7e339ca8da.zip
binutils-f3a5df7bd65bea1840d00bd65d5eee7e339ca8da.tar.gz
binutils-f3a5df7bd65bea1840d00bd65d5eee7e339ca8da.tar.bz2
gdb: unify parts of the Linux and FreeBSD core dumping code
While reviewing the Linux and FreeBSD core dumping code within GDB for another patch series, I noticed that the code that collects the registers for each thread and writes these into ELF note format is basically identical between Linux and FreeBSD. This commit merges this code and moves it into a new file gcore-elf.c. The function find_signalled_thread is moved from linux-tdep.c to gcore.c despite not being shared. A later commit will make use of this function. I did merge, and then revert a previous version of this patch (commit 82a1fd3a4935 for the original patch and 03642b7189bc for the revert). The problem with the original patch is that it introduced a unconditional dependency between GDB and some ELF specific functions in the BFD library, e.g. elfcore_write_prstatus and elfcore_write_register_note. It was pointed out in this mailing list post: https://sourceware.org/pipermail/gdb-patches/2021-February/175750.html that this change was breaking any build of GDB for non-ELF targets. To confirm this breakage, and to test this new version of GDB I configured and built for the target x86_64-apple-darwin20.3.0. Where the previous version of this patch placed all of the common code into gcore.c, which is included in all builds of GDB, this new patch only places non-ELF specific generic code (i.e. find_signalled_thread) into gcore.c, the ELF specific code is put into the new gcore-elf.c file, which is only included in GDB if BFD has ELF support. The contents of gcore-elf.c are referenced unconditionally from linux-tdep.c and fbsd-tdep.c, this is fine, we previously always assumed that these two targets required ELF support, and we continue to make that assumption after this patch; nothing has changed there. With my previous version of this patch the darwin target mentioned above failed to build, but with the new version, the target builds fine. There are a couple of minor changes to the FreeBSD target after this commit, but I believe that these are changes for the better: (1) For FreeBSD we always used to record the thread-id in the core file by using ptid_t.lwp (). In contrast the Linux code did this: /* For remote targets the LWP may not be available, so use the TID. */ long lwp = ptid.lwp (); if (lwp == 0) lwp = ptid.tid (); Both target now do this: /* The LWP is often not available for bare metal target, in which case use the tid instead. */ if (ptid.lwp_p ()) lwp = ptid.lwp (); else lwp = ptid.tid (); Which is equivalent for Linux, but is a change for FreeBSD. I think that all this means is that in some cases where GDB might have previously recorded a thread-id of 0 for each thread, we might now get something more useful. (2) When collecting the registers for Linux we collected into a zero initialised buffer. By contrast on FreeBSD the buffer is left uninitialised. In the new code the buffer is always zero initialised. I suspect once the registers are copied into the buffer there's probably no gaps left so this makes no difference, but if it does then using zeros rather than random bits of GDB's memory is probably a good thing. Otherwise, there should be no other user visible changes after this commit. Tested this on x86-64/GNU-Linux and x86-64/FreeBSD-12.2 with no regressions. gdb/ChangeLog: * Makefile.in (SFILES): Add gcore-elf.c. (HFILES_NO_SRCDIR): Add gcore-elf.h * configure: Regenerate. * configure.ac: Add gcore-elf.o to CONFIG_OBS if we have ELF support. * fbsd-tdep.c: Add 'gcore-elf.h' include. (struct fbsd_collect_regset_section_cb_data): Delete. (fbsd_collect_regset_section_cb): Delete. (fbsd_collect_thread_registers): Delete. (struct fbsd_corefile_thread_data): Delete. (fbsd_corefile_thread): Delete. (fbsd_make_corefile_notes): Call gcore_elf_build_thread_register_notes instead of the now deleted FreeBSD code. * gcore-elf.c: New file, the content was moved here from linux-tdep.c, functions were renamed and given minor cleanup. * gcore-elf.h: New file. * gcore.c (gcore_find_signalled_thread): Moved here from linux-tdep.c and given a new name. Minor cleanups. * gcore.h (gcore_find_signalled_thread): Declare. * linux-tdep.c: Add 'gcore.h' and 'gcore-elf.h' includes. (struct linux_collect_regset_section_cb_data): Delete. (linux_collect_regset_section_cb): Delete. (linux_collect_thread_registers): Delete. (linux_corefile_thread): Call gcore_elf_build_thread_register_notes. (find_signalled_thread): Delete. (linux_make_corefile_notes): Call gcore_find_signalled_thread.
Diffstat (limited to 'gdb/fbsd-tdep.c')
-rw-r--r--gdb/fbsd-tdep.c134
1 files changed, 6 insertions, 128 deletions
diff --git a/gdb/fbsd-tdep.c b/gdb/fbsd-tdep.c
index cc51e92..dc4278c 100644
--- a/gdb/fbsd-tdep.c
+++ b/gdb/fbsd-tdep.c
@@ -32,6 +32,7 @@
#include "elf-bfd.h"
#include "fbsd-tdep.h"
+#include "gcore-elf.h"
/* This enum is derived from FreeBSD's <sys/signal.h>. */
@@ -583,129 +584,6 @@ find_signalled_thread (struct thread_info *info, void *data)
return 0;
}
-/* Structure for passing information from
- fbsd_collect_thread_registers via an iterator to
- fbsd_collect_regset_section_cb. */
-
-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;
- gdb::unique_xmalloc_ptr<char> &note_data;
- int *note_size;
- unsigned long lwp;
- enum gdb_signal stop_signal;
- bool abort_iteration = false;
-};
-
-static void
-fbsd_collect_regset_section_cb (const char *sect_name, int supply_size,
- int collect_size, const struct regset *regset,
- const char *human_name, void *cb_data)
-{
- char *buf;
- struct fbsd_collect_regset_section_cb_data *data
- = (struct fbsd_collect_regset_section_cb_data *) cb_data;
-
- if (data->abort_iteration)
- return;
-
- gdb_assert (regset->collect_regset);
-
- buf = (char *) xmalloc (collect_size);
- regset->collect_regset (regset, data->regcache, -1, buf, collect_size);
-
- /* PRSTATUS still needs to be treated specially. */
- if (strcmp (sect_name, ".reg") == 0)
- 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.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 = true;
-}
-
-/* Records the thread's register state for the corefile note
- section. */
-
-static void
-fbsd_collect_thread_registers (const struct regcache *regcache,
- ptid_t ptid, bfd *obfd,
- gdb::unique_xmalloc_ptr<char> &note_data,
- int *note_size,
- enum gdb_signal stop_signal)
-{
- 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);
-}
-
-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;
- gdb::unique_xmalloc_ptr<char> &note_data;
- int *note_size;
- enum gdb_signal stop_signal;
-};
-
-/* Records the thread's register state for the corefile note
- section. */
-
-static void
-fbsd_corefile_thread (struct thread_info *info,
- struct fbsd_corefile_thread_data *args)
-{
- struct regcache *regcache;
-
- regcache = get_thread_arch_regcache (info->inf->process_target (),
- info->ptid, args->gdbarch);
-
- target_fetch_registers (regcache, -1);
-
- 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
for the target object of type OBJECT. If STRUCTSIZE is non-zero,
the data is prefixed with a 32-bit integer size to match the format
@@ -782,16 +660,16 @@ fbsd_make_corefile_notes (struct gdbarch *gdbarch, bfd *obfd, int *note_size)
signalled_thr = curr_thr;
}
- fbsd_corefile_thread_data thread_args (gdbarch, obfd, note_data, note_size,
- signalled_thr->suspend.stop_signal);
-
- fbsd_corefile_thread (signalled_thr, &thread_args);
+ enum gdb_signal stop_signal = signalled_thr->suspend.stop_signal;
+ gcore_elf_build_thread_register_notes (gdbarch, signalled_thr, stop_signal,
+ obfd, &note_data, note_size);
for (thread_info *thr : current_inferior ()->non_exited_threads ())
{
if (thr == signalled_thr)
continue;
- fbsd_corefile_thread (thr, &thread_args);
+ gcore_elf_build_thread_register_notes (gdbarch, thr, stop_signal,
+ obfd, &note_data, note_size);
}
/* Auxiliary vector. */