binutils-gdb/gdbserver/linux-arc-low.cc
Andrew Burgess 1048062a3f gdbserver: pass osabi to GDB in more target descriptions
Problem Description
-------------------

On a Windows machine I built gdbserver, configured for the target
'x86_64-w64-mingw32', then on a GNU/Linux machine I built GDB with
support for all target (--enable-targets=all).

On the Windows machine I start gdbserver with a small test binary:

  $ gdbserver 192.168.129.25:54321 C:\some\directory\executable.exe

On the GNU/Linux machine I start GDB without the test binary, and
connect to gdbserver.

As I have not given GDB the test binary, my expectation is that GDB
would connect to gdbserver and then download the file over the remote
protocol, but instead I was presented with this message:

  (gdb) target remote 192.168.129.25:54321
  Remote debugging using 192.168.129.25:54321
  warning: C:\some\directory\executable.exe: No such file or directory.
  0x00007ffa3e1e1741 in ?? ()
  (gdb)

What I found is that if I told GDB where to find the binary, like
this:

  (gdb) file target:C:/some/directory/executable.exe
  A program is being debugged already.
  Are you sure you want to change the file? (y or n) y
  Reading C:/some/directory/executable.exe from remote target...
  warning: File transfers from remote targets can be slow. Use "set sysroot" to access files locally instead.
  Reading C:/some/directory/executable.exe from remote target...
  Reading symbols from target:C:/some/directory/executable.exe...
  (gdb)

then GDB would download the executable.

The Actual Issue
----------------

I tracked the problem down to exec_file_find (solib.c).  The remote
target was passing an absolute Windows filename (beginning with "C:/"
in this case), but in exec_file_find GDB was failing the
IS_TARGET_ABSOLUTE_PATH call, and so was treating the filename as
relative.

The IS_TARGET_ABSOLUTE_PATH call was failing because GDB thought that
the file system kind was "unix", and as the filename didn't start with
a "/" it assumed the filename was not absolute.

But I'm connecting to a Windows target and 'target-file-system-kind'
was set to "auto", so GDB should be figuring out that the target
file-system is "dos-based".

Looking in effective_target_file_system_kind (filesystem.c), we find
that the logic of "auto" is delegated to the current gdbarch.  However
in windows-tdep.c we see:

  set_gdbarch_has_dos_based_file_system (gdbarch, 1);

So if we are using a Windows gdbarch we should have "dos-based"
filesystems.  What this means is that after connecting to the remote
target GDB has selected the wrong gdbarch.

What's happening is that the target description sent back by the
remote target only includes the x86-64 registers.  There's no
information about which OS we're on.  As a consequence, GDB picks the
first x86-64 gdbarch which can handle the provided register set, which
happens to be a GNU/Linux gdbarch.

And indeed, there doesn't appear to be anywhere in gdbserver that sets
the osabi on the target descriptions. Some target descriptions do have
their osabi set when the description is created, e.g. in:

  gdb/arch/amd64.c	- Sets GNU/Linux osabi when appropriate.
  gdb/arch/i386.c	- Likewise.
  gdb/arch/tic6x.c	- Always set GNU/Linux osabi.

There are also some cases in gdb/features/*.c where the tdesc is set,
but these locations are only called from GDB, not from gdbserver.

This means that many target descriptions are created without an osabi,
gdbserver does nothing to fix this, and the description is returned to
GDB without an osabi included.  This leaves GDB having to guess what
the target osabi is, and in some cases, GDB can get this wrong.

Proposed Solution
-----------------

I propose to change init_target_desc so that it requires an gdb_osabi
to be passed in, this will then be used to set the target_desc osabi
field.

I believe that within gdbserver init_target_desc is called for every
target_desc, so this should mean that every target_desc has an
opportunity to set the osabi to something sane.

I did consider passing the osabi into the code which creates the
target_desc objects, but that would require updating far more code, as
each target has its own code for creating target descriptions.
The approach taken here requires minimal changes and forces every
user of init_target_desc to think about what the correct osabi is.

In some cases, e.g. amd64, where the osabi is already set when the
target_desc is created, the init_target_desc call will override the
current value, however, we should always be replacing it with the same
actual value.  i.e. if the target_desc is created with the osabi set
to GNU/Linux, then this should only happen when gdbserver is built for
GNU/Linux, in which case the init_target_desc should also be setting
the osabi to GNU/Linux.

The Tricky Bits
---------------

Some targets, like amd64, use a features based approach for creating
target_desc objects, there's a function in arch/amd64.c which creates
a target_desc, adds features too it, and returns the new target_desc.
This target_desc is then passed to an init_target_desc call within
gdbserver.  This is the easy case to handle.

Then there are other targets which instead have a fixed set of xml
files, each of which is converted into a .dat file, which is then used
to generate a .cc file, which is compiled into gdbserver.  The
generated .cc file creates the target_desc object and calls
init_target_desc on it.  In this case though the target description
that is sent to GDB isn't generated from the target_desc object, but
is instead the contents of the fixed xml file.  For this case the
osabi which we pass to init_target_desc should match the osabi that
exists in the fixed xml file.

Luckily, in the previous commit I copied the osabi information from
the fixed xml files into the .dat files.  So in this commit I have
extended regdat.sh to read the osabi from the .dat file and use it in
the generated init_target_desc call.

The problem with some of these .dat base targets is that their fixed
xml files don't currently contain any osabi information, and the file
names don't indicate that they are Linux only (despite them currently
only being used from gdbserver for Linux targets), so I don't
currently feel confident adding any osabi information to these files.
An example would be features/rs6000/powerpc-64.xml.  For now I've just
ignored these cases.  The init_target_desc will use GDB_OSABI_UNKNOWN
which is the default.  This means that for these targets nothing
changes from the current behaviour.  But many other targets do now
pass the osabi back.  Targets that do pass the osabi back are
improved with this commit.

Conclusion
----------

Now when I connect to the Windows remote the target description
returned includes the osabi name.  With this extra information GDB
selects the correct gdbarch object, which means that GDB understands
the target has a "dos-based" file-system.  With that correct GDB
understands that the filename it was given is absolute, and so fetches
the file from the remote as we'd like.

Reviewed-By: Kevin Buettner <kevinb@redhat.com>
2024-11-12 12:51:36 +00:00

418 lines
14 KiB
C++

/* Target dependent code for the remote server for GNU/Linux ARC.
Copyright 2020-2024 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 "regdef.h"
#include "linux-low.h"
#include "tdesc.h"
#include "arch/arc.h"
#include <linux/elf.h>
#include <arpa/inet.h>
/* Linux starting with 4.12 supports NT_ARC_V2 note type, which adds R30,
R58 and R59 registers. */
#ifdef NT_ARC_V2
#define ARC_HAS_V2_REGSET
#endif
/* The encoding of the instruction "TRAP_S 1" (endianness agnostic). */
#define TRAP_S_1_OPCODE 0x783e
#define TRAP_S_1_SIZE 2
/* Using a mere "uint16_t arc_linux_traps_s = TRAP_S_1_OPCODE" would
work as well, because the endianness will end up correctly when
the code is compiled for the same endianness as the target (see
the notes for "low_breakpoint_at" in this file). However, this
illustrates how the __BIG_ENDIAN__ macro can be used to make
easy-to-understand codes. */
#if defined(__BIG_ENDIAN__)
/* 0x78, 0x3e. */
static gdb_byte arc_linux_trap_s[TRAP_S_1_SIZE]
= {TRAP_S_1_OPCODE >> 8, TRAP_S_1_OPCODE & 0xFF};
#else
/* 0x3e, 0x78. */
static gdb_byte arc_linux_trap_s[TRAP_S_1_SIZE]
= {TRAP_S_1_OPCODE && 0xFF, TRAP_S_1_OPCODE >> 8};
#endif
/* Linux target op definitions for the ARC architecture.
Note for future: in case of adding the protected method low_get_next_pcs(),
the public method supports_software_single_step() should be added to return
"true". */
class arc_target : public linux_process_target
{
public:
const regs_info *get_regs_info () override;
const gdb_byte *sw_breakpoint_from_kind (int kind, int *size) override;
protected:
void low_arch_setup () override;
bool low_cannot_fetch_register (int regno) override;
bool low_cannot_store_register (int regno) override;
bool low_supports_breakpoints () override;
CORE_ADDR low_get_pc (regcache *regcache) override;
void low_set_pc (regcache *regcache, CORE_ADDR newpc) override;
bool low_breakpoint_at (CORE_ADDR where) override;
};
/* The singleton target ops object. */
static arc_target the_arc_target;
bool
arc_target::low_supports_breakpoints ()
{
return true;
}
CORE_ADDR
arc_target::low_get_pc (regcache *regcache)
{
return linux_get_pc_32bit (regcache);
}
void
arc_target::low_set_pc (regcache *regcache, CORE_ADDR pc)
{
linux_set_pc_32bit (regcache, pc);
}
static const struct target_desc *
arc_linux_read_description (void)
{
#ifdef __ARC700__
arc_arch_features features (4, ARC_ISA_ARCV1);
#else
arc_arch_features features (4, ARC_ISA_ARCV2);
#endif
target_desc_up tdesc = arc_create_target_description (features);
static const char *expedite_regs[] = { "sp", "status32", nullptr };
init_target_desc (tdesc.get (), expedite_regs, GDB_OSABI_LINUX);
return tdesc.release ();
}
void
arc_target::low_arch_setup ()
{
current_process ()->tdesc = arc_linux_read_description ();
}
bool
arc_target::low_cannot_fetch_register (int regno)
{
return (regno >= current_process ()->tdesc->reg_defs.size ());
}
bool
arc_target::low_cannot_store_register (int regno)
{
return (regno >= current_process ()->tdesc->reg_defs.size ());
}
/* This works for both endianness. Below you see an illustration of how
the "trap_s 1" instruction encoded for both endianness in the memory
will end up as the TRAP_S_1_OPCODE constant:
BE: 0x78 0x3e --> at INSN addr: 0x78 0x3e --> INSN = 0x783e
LE: 0x3e 0x78 --> at INSN addr: 0x3e 0x78 --> INSN = 0x783e
One can employ "memcmp()" for comparing the arrays too. */
bool
arc_target::low_breakpoint_at (CORE_ADDR where)
{
uint16_t insn;
/* "the_target" global variable is the current object at hand. */
this->read_memory (where, (gdb_byte *) &insn, TRAP_S_1_SIZE);
return (insn == TRAP_S_1_OPCODE);
}
/* PTRACE_GETREGSET/NT_PRSTATUS and PTRACE_SETREGSET/NT_PRSTATUS work with
regsets in a struct, "user_regs_struct", defined in the
linux/arch/arc/include/uapi/asm/ptrace.h header. This code supports
ARC Linux ABI v3 and v4. */
/* Populate a ptrace NT_PRSTATUS regset from a regcache.
This appears to be a unique approach to populating the buffer, but
being name, rather than offset based, it is robust to future API
changes, as there is no need to create a regmap of registers in the
user_regs_struct. */
static void
arc_fill_gregset (struct regcache *regcache, void *buf)
{
struct user_regs_struct *regbuf = (struct user_regs_struct *) buf;
/* Core registers. */
collect_register_by_name (regcache, "r0", &(regbuf->scratch.r0));
collect_register_by_name (regcache, "r1", &(regbuf->scratch.r1));
collect_register_by_name (regcache, "r2", &(regbuf->scratch.r2));
collect_register_by_name (regcache, "r3", &(regbuf->scratch.r3));
collect_register_by_name (regcache, "r4", &(regbuf->scratch.r4));
collect_register_by_name (regcache, "r5", &(regbuf->scratch.r5));
collect_register_by_name (regcache, "r6", &(regbuf->scratch.r6));
collect_register_by_name (regcache, "r7", &(regbuf->scratch.r7));
collect_register_by_name (regcache, "r8", &(regbuf->scratch.r8));
collect_register_by_name (regcache, "r9", &(regbuf->scratch.r9));
collect_register_by_name (regcache, "r10", &(regbuf->scratch.r10));
collect_register_by_name (regcache, "r11", &(regbuf->scratch.r11));
collect_register_by_name (regcache, "r12", &(regbuf->scratch.r12));
collect_register_by_name (regcache, "r13", &(regbuf->callee.r13));
collect_register_by_name (regcache, "r14", &(regbuf->callee.r14));
collect_register_by_name (regcache, "r15", &(regbuf->callee.r15));
collect_register_by_name (regcache, "r16", &(regbuf->callee.r16));
collect_register_by_name (regcache, "r17", &(regbuf->callee.r17));
collect_register_by_name (regcache, "r18", &(regbuf->callee.r18));
collect_register_by_name (regcache, "r19", &(regbuf->callee.r19));
collect_register_by_name (regcache, "r20", &(regbuf->callee.r20));
collect_register_by_name (regcache, "r21", &(regbuf->callee.r21));
collect_register_by_name (regcache, "r22", &(regbuf->callee.r22));
collect_register_by_name (regcache, "r23", &(regbuf->callee.r23));
collect_register_by_name (regcache, "r24", &(regbuf->callee.r24));
collect_register_by_name (regcache, "r25", &(regbuf->callee.r25));
collect_register_by_name (regcache, "gp", &(regbuf->scratch.gp));
collect_register_by_name (regcache, "fp", &(regbuf->scratch.fp));
collect_register_by_name (regcache, "sp", &(regbuf->scratch.sp));
collect_register_by_name (regcache, "blink", &(regbuf->scratch.blink));
/* Loop registers. */
collect_register_by_name (regcache, "lp_count", &(regbuf->scratch.lp_count));
collect_register_by_name (regcache, "lp_start", &(regbuf->scratch.lp_start));
collect_register_by_name (regcache, "lp_end", &(regbuf->scratch.lp_end));
/* The current "pc" value must be written to "eret" (exception return
address) register, because that is the address that the kernel code
will jump back to after a breakpoint exception has been raised.
The "pc_stop" value is ignored by the genregs_set() in
linux/arch/arc/kernel/ptrace.c. */
collect_register_by_name (regcache, "pc", &(regbuf->scratch.ret));
/* Currently ARC Linux ptrace doesn't allow writes to status32 because
some of its bits are kernel mode-only and shoudn't be writable from
user-space. Writing status32 from debugger could be useful, though,
so ability to write non-privileged bits will be added to kernel
sooner or later. */
/* BTA. */
collect_register_by_name (regcache, "bta", &(regbuf->scratch.bta));
}
/* Populate a regcache from a ptrace NT_PRSTATUS regset. */
static void
arc_store_gregset (struct regcache *regcache, const void *buf)
{
const struct user_regs_struct *regbuf = (const struct user_regs_struct *) buf;
/* Core registers. */
supply_register_by_name (regcache, "r0", &(regbuf->scratch.r0));
supply_register_by_name (regcache, "r1", &(regbuf->scratch.r1));
supply_register_by_name (regcache, "r2", &(regbuf->scratch.r2));
supply_register_by_name (regcache, "r3", &(regbuf->scratch.r3));
supply_register_by_name (regcache, "r4", &(regbuf->scratch.r4));
supply_register_by_name (regcache, "r5", &(regbuf->scratch.r5));
supply_register_by_name (regcache, "r6", &(regbuf->scratch.r6));
supply_register_by_name (regcache, "r7", &(regbuf->scratch.r7));
supply_register_by_name (regcache, "r8", &(regbuf->scratch.r8));
supply_register_by_name (regcache, "r9", &(regbuf->scratch.r9));
supply_register_by_name (regcache, "r10", &(regbuf->scratch.r10));
supply_register_by_name (regcache, "r11", &(regbuf->scratch.r11));
supply_register_by_name (regcache, "r12", &(regbuf->scratch.r12));
supply_register_by_name (regcache, "r13", &(regbuf->callee.r13));
supply_register_by_name (regcache, "r14", &(regbuf->callee.r14));
supply_register_by_name (regcache, "r15", &(regbuf->callee.r15));
supply_register_by_name (regcache, "r16", &(regbuf->callee.r16));
supply_register_by_name (regcache, "r17", &(regbuf->callee.r17));
supply_register_by_name (regcache, "r18", &(regbuf->callee.r18));
supply_register_by_name (regcache, "r19", &(regbuf->callee.r19));
supply_register_by_name (regcache, "r20", &(regbuf->callee.r20));
supply_register_by_name (regcache, "r21", &(regbuf->callee.r21));
supply_register_by_name (regcache, "r22", &(regbuf->callee.r22));
supply_register_by_name (regcache, "r23", &(regbuf->callee.r23));
supply_register_by_name (regcache, "r24", &(regbuf->callee.r24));
supply_register_by_name (regcache, "r25", &(regbuf->callee.r25));
supply_register_by_name (regcache, "gp", &(regbuf->scratch.gp));
supply_register_by_name (regcache, "fp", &(regbuf->scratch.fp));
supply_register_by_name (regcache, "sp", &(regbuf->scratch.sp));
supply_register_by_name (regcache, "blink", &(regbuf->scratch.blink));
/* Loop registers. */
supply_register_by_name (regcache, "lp_count", &(regbuf->scratch.lp_count));
supply_register_by_name (regcache, "lp_start", &(regbuf->scratch.lp_start));
supply_register_by_name (regcache, "lp_end", &(regbuf->scratch.lp_end));
/* The genregs_get() in linux/arch/arc/kernel/ptrace.c populates the
pseudo register "stop_pc" with the "efa" (exception fault address)
register. This was deemed necessary, because the breakpoint
instruction, "trap_s 1", is a committing one; i.e. the "eret"
(exception return address) register will be pointing to the next
instruction, while "efa" points to the address that raised the
breakpoint. */
supply_register_by_name (regcache, "pc", &(regbuf->stop_pc));
unsigned long pcl = regbuf->stop_pc & ~3L;
supply_register_by_name (regcache, "pcl", &pcl);
/* Other auxiliary registers. */
supply_register_by_name (regcache, "status32", &(regbuf->scratch.status32));
/* BTA. */
supply_register_by_name (regcache, "bta", &(regbuf->scratch.bta));
}
#ifdef ARC_HAS_V2_REGSET
/* Look through a regcache's TDESC for a register named NAME.
If found, return true; false, otherwise. */
static bool
is_reg_name_available_p (const struct target_desc *tdesc,
const char *name)
{
for (const gdb::reg &reg : tdesc->reg_defs)
if (strcmp (name, reg.name) == 0)
return true;
return false;
}
/* Copy registers from regcache to user_regs_arcv2. */
static void
arc_fill_v2_regset (struct regcache *regcache, void *buf)
{
struct user_regs_arcv2 *regbuf = (struct user_regs_arcv2 *) buf;
if (is_reg_name_available_p (regcache->tdesc, "r30"))
collect_register_by_name (regcache, "r30", &(regbuf->r30));
if (is_reg_name_available_p (regcache->tdesc, "r58"))
collect_register_by_name (regcache, "r58", &(regbuf->r58));
if (is_reg_name_available_p (regcache->tdesc, "r59"))
collect_register_by_name (regcache, "r59", &(regbuf->r59));
}
/* Copy registers from user_regs_arcv2 to regcache. */
static void
arc_store_v2_regset (struct regcache *regcache, const void *buf)
{
struct user_regs_arcv2 *regbuf = (struct user_regs_arcv2 *) buf;
if (is_reg_name_available_p (regcache->tdesc, "r30"))
supply_register_by_name (regcache, "r30", &(regbuf->r30));
if (is_reg_name_available_p (regcache->tdesc, "r58"))
supply_register_by_name (regcache, "r58", &(regbuf->r58));
if (is_reg_name_available_p (regcache->tdesc, "r59"))
supply_register_by_name (regcache, "r59", &(regbuf->r59));
}
#endif
/* Fetch the thread-local storage pointer for libthread_db. Note that
this function is not called from GDB, but is called from libthread_db.
This is the same function as for other architectures, for example in
linux-arm-low.c. */
ps_err_e
ps_get_thread_area (struct ps_prochandle *ph, lwpid_t lwpid,
int idx, void **base)
{
if (ptrace (PTRACE_GET_THREAD_AREA, lwpid, nullptr, base) != 0)
return PS_ERR;
/* IDX is the bias from the thread pointer to the beginning of the
thread descriptor. It has to be subtracted due to implementation
quirks in libthread_db. */
*base = (void *) ((char *) *base - idx);
return PS_OK;
}
static struct regset_info arc_regsets[] =
{
{ PTRACE_GETREGSET, PTRACE_SETREGSET, NT_PRSTATUS,
sizeof (struct user_regs_struct), GENERAL_REGS,
arc_fill_gregset, arc_store_gregset
},
#ifdef ARC_HAS_V2_REGSET
{ PTRACE_GETREGSET, PTRACE_SETREGSET, NT_ARC_V2,
sizeof (struct user_regs_arcv2), GENERAL_REGS,
arc_fill_v2_regset, arc_store_v2_regset
},
#endif
NULL_REGSET
};
static struct regsets_info arc_regsets_info =
{
arc_regsets, /* regsets */
0, /* num_regsets */
nullptr, /* disabled regsets */
};
static struct regs_info arc_regs_info =
{
nullptr, /* regset_bitmap */
nullptr, /* usrregs */
&arc_regsets_info
};
const regs_info *
arc_target::get_regs_info ()
{
return &arc_regs_info;
}
/* One of the methods necessary for Z0 packet support. */
const gdb_byte *
arc_target::sw_breakpoint_from_kind (int kind, int *size)
{
gdb_assert (kind == TRAP_S_1_SIZE);
*size = kind;
return arc_linux_trap_s;
}
/* The linux target ops object. */
linux_process_target *the_linux_target = &the_arc_target;
void
initialize_low_arch (void)
{
initialize_regsets_info (&arc_regsets_info);
}