[PATCH 3/5] fbsd-nat: Pass an optional register base to the register set helpers.

John Baldwin
2022-07-08T01:03:55Z

This is needed to permit using the helpers for register sets with a
variable base. In particular regnum needs to be converted into a
relative register number before passed to regcache_map_supplies.
---
gdb/fbsd-nat.c | 34 +++++++++++++++++++---------------
gdb/fbsd-nat.h | 49 ++++++++++++++++++++++++++++---------------------
2 files changed, 47 insertions(+), 36 deletions(-)

diff --git a/gdb/fbsd-nat.c b/gdb/fbsd-nat.c
index 9d7383ac0ab..edcd3fc3ed1 100644
--- a/gdb/fbsd-nat.c
+++ b/gdb/fbsd-nat.c
@@ -1732,14 +1732,15 @@ fbsd_nat_target::supports_disable_randomization ()
bool
fbsd_nat_target::fetch_register_set (struct regcache *regcache, int regnum,
int fetch_op, const struct regset *regset,
- void *regs, size_t size)
+ int regbase, void *regs, size_t size)
{
const struct regcache_map_entry *map
= (const struct regcache_map_entry *) regset->regmap;
pid_t pid = get_ptrace_pid (regcache->ptid ());

- if (regnum == -1 || regcache_map_supplies (map, regnum, regcache->arch(),
- size))
+ if (regnum == -1
+ || (regnum >= regbase && regcache_map_supplies (map, regnum - regbase,
+ regcache->arch(), size)))
{
if (ptrace (fetch_op, pid, (PTRACE_TYPE_ARG3) regs, 0) == -1)
perror_with_name (_("Couldn't get registers"));
@@ -1755,15 +1756,16 @@ fbsd_nat_target::fetch_register_set (struct regcache *regcache, int regnum,
bool
fbsd_nat_target::store_register_set (struct regcache *regcache, int regnum,
int fetch_op, int store_op,
- const struct regset *regset, void *regs,
- size_t size)
+ const struct regset *regset, int regbase,
+ void *regs, size_t size)
{
const struct regcache_map_entry *map
= (const struct regcache_map_entry *) regset->regmap;
pid_t pid = get_ptrace_pid (regcache->ptid ());

- if (regnum == -1 || regcache_map_supplies (map, regnum, regcache->arch(),
- size))
+ if (regnum == -1
+ || (regnum >= regbase && regcache_map_supplies (map, regnum - regbase,
+ regcache->arch(), size)))
{
if (ptrace (fetch_op, pid, (PTRACE_TYPE_ARG3) regs, 0) == -1)
perror_with_name (_("Couldn't get registers"));
@@ -1796,15 +1798,16 @@ fbsd_nat_target::have_regset (ptid_t ptid, int note)

bool
fbsd_nat_target::fetch_regset (struct regcache *regcache, int regnum, int note,
- const struct regset *regset, void *regs,
- size_t size)
+ const struct regset *regset, int regbase,
+ void *regs, size_t size)
{
const struct regcache_map_entry *map
= (const struct regcache_map_entry *) regset->regmap;
pid_t pid = get_ptrace_pid (regcache->ptid ());

- if (regnum == -1 || regcache_map_supplies (map, regnum, regcache->arch(),
- size))
+ if (regnum == -1
+ || (regnum >= regbase && regcache_map_supplies (map, regnum - regbase,
+ regcache->arch(), size)))
{
struct iovec iov;

@@ -1821,15 +1824,16 @@ fbsd_nat_target::fetch_regset (struct regcache *regcache, int regnum, int note,

bool
fbsd_nat_target::store_regset (struct regcache *regcache, int regnum, int note,
- const struct regset *regset, void *regs,
- size_t size)
+ const struct regset *regset, int regbase,
+ void *regs, size_t size)
{
const struct regcache_map_entry *map
= (const struct regcache_map_entry *) regset->regmap;
pid_t pid = get_ptrace_pid (regcache->ptid ());

- if (regnum == -1 || regcache_map_supplies (map, regnum, regcache->arch(),
- size))
+ if (regnum == -1
+ || (regnum >= regbase && regcache_map_supplies (map, regnum - regbase,
+ regcache->arch(), size)))
{
struct iovec iov;

diff --git a/gdb/fbsd-nat.h b/gdb/fbsd-nat.h
index 3a13bc8711f..22e47dcca0d 100644
--- a/gdb/fbsd-nat.h
+++ b/gdb/fbsd-nat.h
@@ -133,7 +133,8 @@ class fbsd_nat_target : public inf_ptrace_target
/* Helper routines for use in fetch_registers and store_registers in
subclasses. These routines fetch and store a single set of
registers described by REGSET. The REGSET's 'regmap' field must
- point to an array of 'struct regcache_map_entry'.
+ point to an array of 'struct regcache_map_entry'. The valid
+ register numbers in the register map are relative to REGBASE.

FETCH_OP is a ptrace operation to fetch the set of registers from
a native thread. STORE_OP is a ptrace operation to store the set
@@ -143,24 +144,27 @@ class fbsd_nat_target : public inf_ptrace_target
and SIZE is the size of the storage.

Returns true if the register set was transferred due to a
- matching REGNUM.*/
+ matching REGNUM. */

bool fetch_register_set (struct regcache *regcache, int regnum, int fetch_op,
- const struct regset *regset, void *regs, size_t size);
+ const struct regset *regset, int regbase, void *regs,
+ size_t size);

bool store_register_set (struct regcache *regcache, int regnum, int fetch_op,
int store_op, const struct regset *regset,
- void *regs, size_t size);
+ int regbase, void *regs, size_t size);

/* Helper routines which use PT_GETREGSET and PT_SETREGSET for the
specified NOTE instead of regset-specific fetch and store
ops. */

bool fetch_regset (struct regcache *regcache, int regnum, int note,
- const struct regset *regset, void *regs, size_t size);
+ const struct regset *regset, int regbase, void *regs,
+ size_t size);

bool store_regset (struct regcache *regcache, int regnum, int note,
- const struct regset *regset, void *regs, size_t size);
+ const struct regset *regset, int regbase, void *regs,
+ size_t size);

protected:
/* Wrapper versions of the above helpers which accept a register set
@@ -168,22 +172,23 @@ class fbsd_nat_target : public inf_ptrace_target

template <class Regset>
bool fetch_register_set (struct regcache *regcache, int regnum, int fetch_op,
- const struct regset *regset)
+ const struct regset *regset, int regbase = 0)
{
Regset regs;
- return fetch_register_set (regcache, regnum, fetch_op, regset, &regs,
- sizeof (regs));
- }
-
- template <class Regset>
- bool store_register_set (struct regcache *regcache, int regnum, int fetch_op,
- int store_op, const struct regset *regset)
- {
- Regset regs;
- return store_register_set (regcache, regnum, fetch_op, store_op, regset,
+ return fetch_register_set (regcache, regnum, fetch_op, regset, regbase,
&regs, sizeof (regs));
}

+ template <class Regset>
+ bool store_register_set (struct regcache *regcache, int regnum, int fetch_op,
+ int store_op, const struct regset *regset,
+ int regbase = 0)
+ {
+ Regset regs;
+ return store_register_set (regcache, regnum, fetch_op, store_op, regset,
+ regbase, &regs, sizeof (regs));
+ }
+
/* Helper routine for use in read_description in subclasses. This
routine checks if the register set for the specified NOTE is
present for a given PTID. If the register set is present, the
@@ -197,18 +202,20 @@ class fbsd_nat_target : public inf_ptrace_target

template <class Regset>
bool fetch_regset (struct regcache *regcache, int regnum, int note,
- const struct regset *regset)
+ const struct regset *regset, int regbase = 0)
{
Regset regs;
- return fetch_regset (regcache, regnum, note, regset, &regs, sizeof (regs));
+ return fetch_regset (regcache, regnum, note, regset, regbase, &regs,
+ sizeof (regs));
}

template <class Regset>
bool store_regset (struct regcache *regcache, int regnum, int note,
- const struct regset *regset)
+ const struct regset *regset, int regbase = 0)
{
Regset regs;
- return store_regset (regcache, regnum, note, regset, &regs, sizeof (regs));
+ return store_regset (regcache, regnum, note, regset, regbase, &regs,
+ sizeof (regs));
}
};

--
2.36.1