binutils-gdb/gdb/elf-none-tdep.c
Andrew Burgess fb8f3fc0c3 gdb/riscv: introduce bare metal core dump support
This commit adds the ability for bare metal RISC-V target to generate
core files from within GDB.

The intended use case is that a user will connect to a remote bare
metal target, debug up to some error condition, then generate a core
file in the normal way using:

  (gdb) generate-core-file

This core file can then be used to revisit the state of the remote
target without having to reconnect to the remote target.

The core file creation code is split between two new files.  In
elf-none-tdep.c is code for any architecture with the none
ABI (i.e. bare metal) when the BFD library is built with ELF support.

In riscv-none-tdep.c are the RISC-V specific parts.  This is where the
regset and regcache_map_entry structures are defined that control how
registers are laid out in the core file.  As this file could (in
theory at least) be used for a non-ELF bare metal RISC-V target, the
calls into elf-none-tdep.c are guarded with '#ifdef HAVE_ELF'.

Currently for RISC-V only the x-regs and f-regs (if present) are
written out.  In future commits I plan to add support for writing out
the RISC-V CSRs.

The core dump format is based around generating an ELF containing
sections for the writable regions of memory that a user could be
using.  Which regions are dumped rely on GDB's existing common core
dumping code, GDB will attempt to figure out the stack and heap as
well as copying out writable data sections as identified by the
original ELF.

Register information is added to the core dump using notes, just as it
is for Linux of FreeBSD core dumps.  The note types used consist of
the 3 basic types you would expect in a OS based core dump,
NT_PRPSINFO, NT_PRSTATUS, NT_FPREGSET.

The layout of these notes differs slightly (due to field sizes)
between RV32 and RV64.  Below I describe the data layout for each
note.  In all cases, all padding fields should be set to zero.

Note NT_PRPSINFO is optional.  Its data layout is:

  struct prpsinfo32_t		/* For RV32.  */
  {
    uint8_t padding[32];
    char fname[16];
    char psargs[80];
  }

  struct prpsinfo64_t		/* For RV64.  */
  {
    uint8_t padding[40];
    char fname[16];
    char psargs[80];
  }

Field 'fname' - null terminated string consisting of the basename of
    (up to the fist 15 characters of) the executable.  Any additional
    space should be set to zero.  If there's no executable name then
    this field can be set to all zero.

Field 'psargs' - a null terminated string up to 80 characters in
    length.  Any additional space should be filled with zero.  This
    field contains the full executable path and any arguments passed
    to the executable.  If there's nothing sensible to write in this
    field then fill it with zero.

Note NT_PRSTATUS is required, its data layout is:

  struct prstatus32_t		/* For RV32.  */
  {
    uint8_t padding_1[12];
    uint16_t sig;
    uint8_t padding_2[10];
    uint32_t thread_id;
    uint8_t padding_3[44];
    uint32_t x_regs[32];
    uint8_t padding_4[4];
  }

  struct prstatus64_t		/* For RV64.  */
  {
    uint8_t padding_1[12];
    uint16_t sig;
    uint8_t padding_2[18];
    uint32_t thread_id;
    uint8_t padding_3[76];
    uint64_t x_regs[32];
    uint8_t padding_4[4];
  }

Field 'sig' - the signal that stopped this thread.  It's implementation
    defined what this field actually means.  Within GDB this will be
    the signal number that the remote target reports as the stop
    reason for this thread.

Field 'thread_is' - the thread id for this thread.  It's implementation
    defined what this field actually means.  Within GDB this will be
    thread thread-id that is assigned to each remote thread.

Field 'x_regs' - at index 0 we store the program counter, and at
    indices 1 to 31 we store x-registers 1 to 31.  x-register 0 is not
    stored, its value is always zero anyway.

Note NT_FPREGSET is optional, its data layout is:

  fpregset32_t			/* For targets with 'F' extension.  */
  {
    uint32_t f_regs[32];
    uint32_t fcsr;
  }

  fpregset64_t			/* For targets with 'D' extension .  */
  {
    uint64_t f_regs[32];
    uint32_t fcsr;
  }

Field 'f_regs' - stores f-registers 0 to 31.

Field 'fcsr' - stores the fcsr CSR register, and is always 4-bytes.

The rules for ordering the notes is the same as for Linux.  The
NT_PRSTATUS note must come before any other notes about additional
register sets.  And for multi-threaded targets all registers for a
single thread should be grouped together.  This is because only
NT_PRSTATUS includes a thread-id, all additional register notes after
a NT_PRSTATUS are assumed to belong to the same thread until a
different NT_PRSTATUS is seen.

gdb/ChangeLog:

	* Makefile.in (ALL_TARGET_OBS): Add riscv-none-tdep.o.
	(ALLDEPFILES): Add riscv-none-tdep.c.
	* configure: Regenerate.
	* configure.ac (CONFIG_OBS): Add elf-none-tdep.o when BFD has ELF
	support.
	* configure.tgt (riscv*-*-*): Include riscv-none-tdep.c.
	* elf-none-tdep.c: New file.
	* elf-none-tdep.h: New file.
	* riscv-none-tdep.c: New file.
2021-03-05 17:21:41 +00:00

127 lines
3.9 KiB
C

/* Common code for targets with the none ABI (bare-metal), but where the
BFD library is build with ELF support.
Copyright (C) 2020-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 "elf-none-tdep.h"
#include "regset.h"
#include "elf-bfd.h" /* for elfcore_write_* */
#include "inferior.h"
#include "regcache.h"
#include "gdbarch.h"
#include "gcore.h"
#include "gcore-elf.h"
/* Build the note section for a corefile, and return it in a malloc
buffer. Currently this just dumps all available registers for each
thread. */
static gdb::unique_xmalloc_ptr<char>
elf_none_make_corefile_notes (struct gdbarch *gdbarch, bfd *obfd,
int *note_size)
{
gdb::unique_xmalloc_ptr<char> note_data;
/* Add note information about the executable and its arguments. */
std::string fname;
std::string psargs;
static const size_t fname_len = 16;
static const size_t psargs_len = 80;
if (get_exec_file (0))
{
const char *exe = get_exec_file (0);
fname = lbasename (exe);
psargs = std::string (exe);
const char *infargs = get_inferior_args ();
if (infargs != nullptr)
psargs += " " + std::string (infargs);
/* All existing targets that handle writing out prpsinfo expect the
fname and psargs strings to be at least 16 and 80 characters long
respectively, including a null terminator at the end. Resize to
the expected length minus one to ensure there is a null within the
required length. */
fname.resize (fname_len - 1);
psargs.resize (psargs_len - 1);
}
/* Resize the buffers up to their required lengths. This will fill any
remaining space with the null character. */
fname.resize (fname_len);
psargs.resize (psargs_len);
/* Now write out the prpsinfo structure. */
note_data.reset (elfcore_write_prpsinfo (obfd, note_data.release (),
note_size, fname.c_str (),
psargs.c_str ()));
if (note_data == nullptr)
return nullptr;
/* Thread register information. */
try
{
update_thread_list ();
}
catch (const gdb_exception_error &e)
{
exception_print (gdb_stderr, e);
}
/* Like the Linux kernel, prefer dumping the signalled thread first.
"First thread" is what tools use to infer the signalled thread. */
thread_info *signalled_thr = gcore_find_signalled_thread ();
/* All threads are reported as having been stopped by the same signal
that stopped SIGNALLED_THR. */
gdb_signal stop_signal;
if (signalled_thr != nullptr)
stop_signal = signalled_thr->suspend.stop_signal;
else
stop_signal = GDB_SIGNAL_0;
if (signalled_thr != nullptr)
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;
gcore_elf_build_thread_register_notes (gdbarch, thr, stop_signal, obfd,
&note_data, note_size);
}
/* Target description. */
gcore_elf_make_tdesc_note (obfd, &note_data, note_size);
return note_data;
}
/* See none-tdep.h. */
void
elf_none_init_abi (struct gdbarch *gdbarch)
{
/* Default core file support. */
set_gdbarch_make_corefile_notes (gdbarch, elf_none_make_corefile_notes);
}