[PATCH 02/12] fbsd-nat: Add helper routines for register sets using PT_[G]SETREGSET.
Luis Machado
luis.machado@arm.com
Thu Mar 24 08:51:08 GMT 2022
On 3/23/22 21:00, John Baldwin wrote:
> FreeBSD's kernel has recently added PT_GETREGSET and PT_SETREGSET
> operations to fetch a register set named by an ELF note type. These
> helper routines provide helpers to check for a register set's
> existence, fetch registers for a register set, and store registers to
> a register set.
> ---
> gdb/fbsd-nat.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++
> gdb/fbsd-nat.h | 42 +++++++++++++++++++++++++++++
> 2 files changed, 114 insertions(+)
>
> diff --git a/gdb/fbsd-nat.c b/gdb/fbsd-nat.c
> index 934fdbad6ef..84abdd9a322 100644
> --- a/gdb/fbsd-nat.c
> +++ b/gdb/fbsd-nat.c
> @@ -1772,6 +1772,78 @@ fbsd_nat_target::store_register_set (struct regcache *regcache, int regnum,
> return false;
> }
>
> +#ifdef PT_GETREGSET
Instead of ifdef-ing the entire block of code, wouldn't it be better to
conditionally define the constant and have the code do checks at runtime?
If GDB is built on a machine without the proper headers and copied onto
a machine with support for PT_GETREGSET, it won't be able to use this
improvement. On the other hand, if we build GDB on a machine with the
headers, but copy it into a machine without support for PT_GETREGSET,
things will break, no?
> +/* See fbsd-nat.h. */
> +
> +bool
> +fbsd_nat_target::have_regset (ptid_t ptid, int note)
> +{
> + pid_t pid = get_ptrace_pid (ptid);
> + struct iovec iov;
> +
> + iov.iov_base = nullptr;
> + iov.iov_len = 0;
> + if (ptrace (PT_GETREGSET, pid, (PTRACE_TYPE_ARG3) &iov, note) == -1)
> + return 0;
> + return iov.iov_len;
> +}
> +
> +/* See fbsd-nat.h. */
> +
> +bool
> +fbsd_nat_target::fetch_regset (struct regcache *regcache, int regnum, int note,
> + const struct regset *regset, 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))
> + {
> + struct iovec iov;
> +
> + iov.iov_base = regs;
> + iov.iov_len = size;
> + if (ptrace (PT_GETREGSET, pid, (PTRACE_TYPE_ARG3) &iov, note) == -1)
> + perror_with_name (_("Couldn't get registers"));
> +
> + regcache->supply_regset (regset, regnum, regs, size);
> + return true;
> + }
> + return false;
> +}
> +
> +bool
> +fbsd_nat_target::store_regset (struct regcache *regcache, int regnum, int note,
> + const struct regset *regset, 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))
> + {
> + struct iovec iov;
> +
> + iov.iov_base = regs;
> + iov.iov_len = size;
> + if (ptrace (PT_GETREGSET, pid, (PTRACE_TYPE_ARG3) &iov, note) == -1)
> + perror_with_name (_("Couldn't get registers"));
> +
> + regcache->collect_regset (regset, regnum, regs, size);
> +
> + if (ptrace (PT_SETREGSET, pid, (PTRACE_TYPE_ARG3) &iov, note) == -1)
> + perror_with_name (_("Couldn't write registers"));
> + return true;
> + }
> + return false;
> +}
> +#endif
> +
> /* See fbsd-nat.h. */
>
> bool
> diff --git a/gdb/fbsd-nat.h b/gdb/fbsd-nat.h
> index 82f7ee47949..6a4003627e4 100644
> --- a/gdb/fbsd-nat.h
> +++ b/gdb/fbsd-nat.h
> @@ -151,6 +151,19 @@ class fbsd_nat_target : public inf_ptrace_target
> bool store_register_set (struct regcache *regcache, int regnum, int fetch_op,
> int store_op, const struct regset *regset,
> void *regs, size_t size);
> +
> +#ifdef PT_GETREGSET
> + /* 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);
> +
> + bool store_regset (struct regcache *regcache, int regnum, int note,
> + const struct regset *regset, void *regs, size_t size);
> +#endif
> +
> protected:
> /* Wrapper versions of the above helpers which accept a register set
> type such as 'struct reg' or 'struct fpreg'. */
> @@ -172,6 +185,35 @@ class fbsd_nat_target : public inf_ptrace_target
> return store_register_set (regcache, regnum, fetch_op, store_op, regset,
> ®s, sizeof (regs));
> }
> +
> +#ifdef PT_GETREGSET
> + /* 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
> + the size of the register set is returned. If the register set is
> + not present, zero is returned. */
> +
> + bool have_regset (ptid_t ptid, int note);
> +
> + /* Wrapper versions of the PT_GETREGSET and PT_REGSET helpers which
> + accept a register set type. */
> +
> + template <class Regset>
> + bool fetch_regset (struct regcache *regcache, int regnum, int note,
> + const struct regset *regset)
> + {
> + Regset regs;
> + return fetch_regset (regcache, regnum, note, regset, ®s, sizeof (regs));
> + }
> +
> + template <class Regset>
> + bool store_regset (struct regcache *regcache, int regnum, int note,
> + const struct regset *regset)
> + {
> + Regset regs;
> + return store_regset (regcache, regnum, note, regset, ®s, sizeof (regs));
> + }
> +#endif
> };
>
> /* Fetch the signal information for PTID and store it in *SIGINFO.
More information about the Binutils
mailing list