From: John Baldwin Date: Tue, 3 May 2022 23:05:10 +0000 (-0700) Subject: fbsd-nat: Add helper routines for register sets using PT_[G]SETREGSET. X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=40c23d880386d6e8202567eaa2a6b041feb1a652;p=binutils-gdb.git fbsd-nat: Add helper routines for register sets using PT_[G]SETREGSET. 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. --- diff --git a/gdb/fbsd-nat.c b/gdb/fbsd-nat.c index 8e5107c26f8..a501d9212c0 100644 --- a/gdb/fbsd-nat.c +++ b/gdb/fbsd-nat.c @@ -49,6 +49,11 @@ #include +#ifndef PT_GETREGSET +#define PT_GETREGSET 42 /* Get a target register set */ +#define PT_SETREGSET 43 /* Set a target register set */ +#endif + /* Return the name of a file that can be opened to get the symbols for the child process identified by PID. */ @@ -1774,6 +1779,76 @@ fbsd_nat_target::store_register_set (struct regcache *regcache, int regnum, /* 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; +} + +/* See fbsd-nat.h. */ + bool fbsd_nat_get_siginfo (ptid_t ptid, siginfo_t *siginfo) { diff --git a/gdb/fbsd-nat.h b/gdb/fbsd-nat.h index 82f7ee47949..ba359c62314 100644 --- a/gdb/fbsd-nat.h +++ b/gdb/fbsd-nat.h @@ -151,6 +151,17 @@ private: bool store_register_set (struct regcache *regcache, int regnum, int fetch_op, int store_op, const struct regset *regset, 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); + + bool store_regset (struct regcache *regcache, int regnum, int note, + const struct regset *regset, void *regs, size_t size); + protected: /* Wrapper versions of the above helpers which accept a register set type such as 'struct reg' or 'struct fpreg'. */ @@ -172,6 +183,33 @@ protected: return store_register_set (regcache, regnum, fetch_op, store_op, regset, ®s, 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 + 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 + 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 + 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)); + } }; /* Fetch the signal information for PTID and store it in *SIGINFO.