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.
2021-01-19 00:00:38 +08:00
|
|
|
/* Copyright (C) 2021 Free Software Foundation, Inc.
|
|
|
|
|
|
|
|
This file is part of GDB.
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
|
|
|
|
|
|
|
#include "defs.h"
|
|
|
|
#include "gcore-elf.h"
|
|
|
|
#include "elf-bfd.h"
|
|
|
|
#include "target.h"
|
|
|
|
#include "regcache.h"
|
|
|
|
#include "gdbarch.h"
|
|
|
|
#include "gdbthread.h"
|
|
|
|
#include "inferior.h"
|
|
|
|
#include "regset.h"
|
gdb: write target description into core file
When a core file is created from within GDB add the target description
into a note within the core file.
When loading a core file, if the target description note is present
then load the target description from the core file.
The benefit of this is that we can be sure that, when analysing the
core file within GDB, that we are using the exact same target
description as was in use at the time the core file was created.
GDB already supports a mechanism for figuring out the target
description from a given corefile; gdbarch_core_read_description.
This new mechanism (GDB adding the target description) is not going to
replace the old mechanism. Core files generated outside of GDB will
not include a target description, and so GDB still needs to be able to
figure out a target description for these files.
My primary motivation for adding this feature is that, in a future
commit, I will be adding support for bare metal core dumps on some
targets. For RISC-V specifically, I want to be able to dump all the
available control status registers. As different targets will present
different sets of register in their target description, including
registers that are possibly not otherwise known to GDB I wanted a way
to capture these registers in the core dump.
I therefore need a mechanism to write out an arbitrary set of
registers, and to then derive a target description from this arbitrary
set when later loading the core file. The obvious approach (I think)
is to just reuse the target description.
Once I'd decided to add support for writing out the target description
I could either choose to make this RISC-V only, or make it generic. I
figure that having the target description in the core file doesn't
hurt, and _might_ be helpful. So that's how I got here, general
support for including the target description in GDB generated core
files.
In previous versions of this patch I added the target description from
generic code (in gcore.c). However, doing this creates a dependency
between GDB's common code and bfd ELF support. As ELF support in gdb
is optional (for example the target x86_64-apple-darwin20.3.0 does not
include ELF support) then having gcore.c require ELF support would
break the GDB build in some cases.
Instead, in this version of the patch, writing the target description
note is done from each specific targets make notes function. Each of
these now calls a common function in gcore-elf.c (which is only linked
in when bfd has ELF support). And so only targets that are ELF based
will call the new function and we can therefore avoid an unconditional
dependency on ELF support.
gdb/ChangeLog:
* corelow.c: Add 'xml-tdesc.h' include.
(core_target::read_description): Load the target description from
the core file when possible.
* fbsd-tdep.c (fbsd_make_corefile_notes): Add target description
note.
* gcore-elf.c: Add 'gdbsupport/tdesc.h' include.
(gcore_elf_make_tdesc_note): New function.
* gcore-elf.h (gcore_elf_make_tdesc_note): Declare.
* linux-tdep.c (linux_make_corefile_notes): Add target description
note.
2020-11-27 23:41:52 +08:00
|
|
|
#include "gdbsupport/tdesc.h"
|
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.
2021-01-19 00:00:38 +08:00
|
|
|
|
|
|
|
/* Structure for passing information from GCORE_COLLECT_THREAD_REGISTERS
|
|
|
|
via an iterator to GCORE_COLLECT_REGSET_SECTION_CB. */
|
|
|
|
|
|
|
|
struct gcore_elf_collect_regset_section_cb_data
|
|
|
|
{
|
|
|
|
gcore_elf_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_elf_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_elf_collect_regset_section_cb_data *data
|
|
|
|
= (struct gcore_elf_collect_regset_section_cb_data *) cb_data;
|
|
|
|
bool variable_size_section = (regset != nullptr
|
|
|
|
&& 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_elf_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_elf_collect_regset_section_cb_data data (gdbarch, regcache, obfd,
|
|
|
|
ptid, stop_signal,
|
|
|
|
note_data, note_size);
|
|
|
|
gdbarch_iterate_over_regset_sections
|
|
|
|
(gdbarch, gcore_elf_collect_regset_section_cb, &data, regcache);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* See gcore-elf.h. */
|
|
|
|
|
|
|
|
void
|
|
|
|
gcore_elf_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_elf_collect_thread_registers (regcache, info->ptid, obfd,
|
|
|
|
note_data, note_size, stop_signal);
|
|
|
|
}
|
gdb: write target description into core file
When a core file is created from within GDB add the target description
into a note within the core file.
When loading a core file, if the target description note is present
then load the target description from the core file.
The benefit of this is that we can be sure that, when analysing the
core file within GDB, that we are using the exact same target
description as was in use at the time the core file was created.
GDB already supports a mechanism for figuring out the target
description from a given corefile; gdbarch_core_read_description.
This new mechanism (GDB adding the target description) is not going to
replace the old mechanism. Core files generated outside of GDB will
not include a target description, and so GDB still needs to be able to
figure out a target description for these files.
My primary motivation for adding this feature is that, in a future
commit, I will be adding support for bare metal core dumps on some
targets. For RISC-V specifically, I want to be able to dump all the
available control status registers. As different targets will present
different sets of register in their target description, including
registers that are possibly not otherwise known to GDB I wanted a way
to capture these registers in the core dump.
I therefore need a mechanism to write out an arbitrary set of
registers, and to then derive a target description from this arbitrary
set when later loading the core file. The obvious approach (I think)
is to just reuse the target description.
Once I'd decided to add support for writing out the target description
I could either choose to make this RISC-V only, or make it generic. I
figure that having the target description in the core file doesn't
hurt, and _might_ be helpful. So that's how I got here, general
support for including the target description in GDB generated core
files.
In previous versions of this patch I added the target description from
generic code (in gcore.c). However, doing this creates a dependency
between GDB's common code and bfd ELF support. As ELF support in gdb
is optional (for example the target x86_64-apple-darwin20.3.0 does not
include ELF support) then having gcore.c require ELF support would
break the GDB build in some cases.
Instead, in this version of the patch, writing the target description
note is done from each specific targets make notes function. Each of
these now calls a common function in gcore-elf.c (which is only linked
in when bfd has ELF support). And so only targets that are ELF based
will call the new function and we can therefore avoid an unconditional
dependency on ELF support.
gdb/ChangeLog:
* corelow.c: Add 'xml-tdesc.h' include.
(core_target::read_description): Load the target description from
the core file when possible.
* fbsd-tdep.c (fbsd_make_corefile_notes): Add target description
note.
* gcore-elf.c: Add 'gdbsupport/tdesc.h' include.
(gcore_elf_make_tdesc_note): New function.
* gcore-elf.h (gcore_elf_make_tdesc_note): Declare.
* linux-tdep.c (linux_make_corefile_notes): Add target description
note.
2020-11-27 23:41:52 +08:00
|
|
|
|
|
|
|
/* See gcore-elf.h. */
|
|
|
|
|
|
|
|
void
|
|
|
|
gcore_elf_make_tdesc_note (bfd *obfd,
|
|
|
|
gdb::unique_xmalloc_ptr<char> *note_data,
|
|
|
|
int *note_size)
|
|
|
|
{
|
|
|
|
/* Append the target description to the core file. */
|
|
|
|
const struct target_desc *tdesc = gdbarch_target_desc (target_gdbarch ());
|
|
|
|
const char *tdesc_xml
|
|
|
|
= tdesc == nullptr ? nullptr : tdesc_get_features_xml (tdesc);
|
|
|
|
if (tdesc_xml != nullptr && *tdesc_xml != '\0')
|
|
|
|
{
|
|
|
|
/* Skip the leading '@'. */
|
|
|
|
if (*tdesc_xml == '@')
|
|
|
|
++tdesc_xml;
|
|
|
|
|
|
|
|
/* Include the null terminator in the length. */
|
|
|
|
size_t tdesc_len = strlen (tdesc_xml) + 1;
|
|
|
|
|
|
|
|
/* Now add the target description into the core file. */
|
|
|
|
note_data->reset (elfcore_write_register_note (obfd,
|
|
|
|
note_data->release (),
|
|
|
|
note_size,
|
|
|
|
".gdb-tdesc", tdesc_xml,
|
|
|
|
tdesc_len));
|
|
|
|
}
|
|
|
|
}
|