diff options
author | Andrew Burgess <andrew.burgess@embecosm.com> | 2021-01-18 16:00:38 +0000 |
---|---|---|
committer | Andrew Burgess <andrew.burgess@embecosm.com> | 2021-02-01 10:35:18 +0000 |
commit | 82a1fd3a4935fe665cf08bc6820942c4a091184c (patch) | |
tree | e3b60b40dfa329387c96a226dc6c8a585e71ecb7 /gdb/gcore.c | |
parent | a5f92c675662a7ed5353fcd0b23b69fdbd6ecf43 (diff) | |
download | gdb-82a1fd3a4935fe665cf08bc6820942c4a091184c.zip gdb-82a1fd3a4935fe665cf08bc6820942c4a091184c.tar.gz gdb-82a1fd3a4935fe665cf08bc6820942c4a091184c.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 the gcore.c file,
which seemed like the right place for generic writing a core file
code.
The function find_signalled_thread is moved from linux-tdep.c despite
not being shared. A later commit will make use of this function.
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 (HFILES_NO_SRCDIR): Add corefile.h.
* gcore.c (struct gcore_collect_regset_section_cb_data): Moved
here from linux-tdep.c and given a new name. Minor cleanups.
(gcore_collect_regset_section_cb): Likewise.
(gcore_collect_thread_registers): Likewise.
(gcore_build_thread_register_notes): Likewise.
(gcore_find_signalled_thread): Likewise.
* gcore.h (gcore_build_thread_register_notes): Declare.
(gcore_find_signalled_thread): Declare.
* fbsd-tdep.c: Add 'gcore.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_build_thread_register_notes instead of the now deleted
FreeBSD code.
* linux-tdep.c: Add 'gcore.h' include.
(struct linux_collect_regset_section_cb_data): Delete.
(linux_collect_regset_section_cb): Delete.
(linux_collect_thread_registers): Delete.
(linux_corefile_thread): Call
gcore_build_thread_register_notes.
(find_signalled_thread): Delete.
(linux_make_corefile_notes): Call gcore_find_signalled_thread.
Diffstat (limited to 'gdb/gcore.c')
-rw-r--r-- | gdb/gcore.c | 136 |
1 files changed, 136 insertions, 0 deletions
diff --git a/gdb/gcore.c b/gdb/gcore.c index 73ac6b0..d62aa3a 100644 --- a/gdb/gcore.c +++ b/gdb/gcore.c @@ -579,6 +579,142 @@ gcore_memory_sections (bfd *obfd) return 1; } +/* Structure for passing information from GCORE_COLLECT_THREAD_REGISTERS + via an iterator to GCORE_COLLECT_REGSET_SECTION_CB. */ + +struct gcore_collect_regset_section_cb_data +{ + gcore_collect_regset_section_cb_data (struct gdbarch *gdbarch, + const struct regcache *regcache, + bfd *obfd, ptid_t ptid, + gdb_signal stop_signal, + gdb::unique_xmalloc_ptr<char> *note_data, + int *note_size) + + : gdbarch (gdbarch), regcache (regcache), obfd (obfd), + note_data (note_data), note_size (note_size), + stop_signal (stop_signal) + { + /* 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 (); + } + + struct gdbarch *gdbarch; + 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; +}; + +/* Callback for ITERATE_OVER_REGSET_SECTIONS that records a single + regset in the core file note section. */ + +static void +gcore_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) +{ + struct gcore_collect_regset_section_cb_data *data + = (struct gcore_collect_regset_section_cb_data *) cb_data; + bool variable_size_section = (regset != NULL + && regset->flags & REGSET_VARIABLE_SIZE); + + gdb_assert (variable_size_section || supply_size == collect_size); + + if (data->abort_iteration) + return; + + gdb_assert (regset != nullptr && regset->collect_regset != nullptr); + + /* This is intentionally zero-initialized by using std::vector, so + that any padding bytes in the core file will show as 0. */ + std::vector<gdb_byte> buf (collect_size); + + regset->collect_regset (regset, data->regcache, -1, buf.data (), + 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.data ())); + else + data->note_data->reset (elfcore_write_register_note + (data->obfd, data->note_data->release (), + data->note_size, sect_name, buf.data (), + collect_size)); + + if (data->note_data == nullptr) + data->abort_iteration = true; +} + +/* Records the register state of thread PTID out of REGCACHE into the note + buffer represented by *NOTE_DATA and NOTE_SIZE. OBFD is the bfd into + which the core file is being created, and STOP_SIGNAL is the signal that + cause thread PTID to stop. */ + +static void +gcore_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) +{ + struct gdbarch *gdbarch = regcache->arch (); + gcore_collect_regset_section_cb_data data (gdbarch, regcache, obfd, ptid, + stop_signal, note_data, + note_size); + gdbarch_iterate_over_regset_sections (gdbarch, + gcore_collect_regset_section_cb, + &data, regcache); +} + +/* See gcore.h. */ + +void +gcore_build_thread_register_notes + (struct gdbarch *gdbarch, struct thread_info *info, gdb_signal stop_signal, + bfd *obfd, gdb::unique_xmalloc_ptr<char> *note_data, int *note_size) +{ + struct regcache *regcache + = get_thread_arch_regcache (info->inf->process_target (), + info->ptid, gdbarch); + target_fetch_registers (regcache, -1); + gcore_collect_thread_registers (regcache, info->ptid, obfd, note_data, + note_size, stop_signal); +} + +/* See gcore.h. */ + +thread_info * +gcore_find_signalled_thread () +{ + thread_info *curr_thr = inferior_thread (); + if (curr_thr->state != THREAD_EXITED + && curr_thr->suspend.stop_signal != GDB_SIGNAL_0) + return curr_thr; + + for (thread_info *thr : current_inferior ()->non_exited_threads ()) + if (thr->suspend.stop_signal != GDB_SIGNAL_0) + return thr; + + /* Default to the current thread, unless it has exited. */ + if (curr_thr->state != THREAD_EXITED) + return curr_thr; + + return nullptr; +} + void _initialize_gcore (); void _initialize_gcore () |