libclues
Linux C++ Tracing Library
Loading...
Searching...
No Matches
RegisterSet.hxx
1#pragma once
2
3// C++
4#include <iosfwd>
5#include <variant>
6
7// Linux
8#include <sys/uio.h> // struct iov
9
10// cosmos
11#include <cosmos/error/RuntimeError.hxx>
12#include <cosmos/error/UsageError.hxx>
13#include <cosmos/io/iovector.hxx>
14#include <cosmos/proc/ptrace.hxx>
15#include <cosmos/types.hxx>
16#include <cosmos/utils.hxx>
17
18// clues
19#include <clues/regs/traits.hxx>
20#include <clues/sysnrs/fwd.hxx>
21#include <clues/types.hxx>
22
23namespace clues {
24
26
30template <ABI abi>
31class RegisterSet {
32public: // types
33
36 static constexpr auto ABI = abi;
37 using register_t = ABIRegisterData::register_t;
38
39public: // functions
40
41 explicit RegisterSet(const cosmos::no_init_t &) {
42 }
43
44 RegisterSet() {
45 m_regs.clear();
46 }
47
49 void fillIov(cosmos::InputMemoryRegion &iov) {
50 // This makes sure the RegisterData wrapper is not bigger than
51 // the expected amount of register data.
52 static_assert(sizeof(m_regs) == sizeof(typename ABIRegisterData::register_t) * ABIRegisterData::NUM_REGS);
53 iov.setBase(&m_regs);
54 iov.setLength(sizeof(m_regs));
55 }
56
58 void iovFilled(const cosmos::InputMemoryRegion &iov) {
59 if (iov.getLength() < sizeof(m_regs)) {
60 throw cosmos::RuntimeError{"received incomplete register set"};
61 }
62 }
63
65 static constexpr cosmos::ptrace::RegisterType registerType() {
66 return cosmos::ptrace::RegisterType::GENERAL_PURPOSE;
67 }
68
70 auto abiSyscallNr() const { return m_regs.syscallNr(); }
71
74 return to_generic(abiSyscallNr());
75 }
76
78 register_t syscallRes() const { return m_regs.syscallRes(); }
79
81
87 register_t syscallParameter(const size_t number) const {
88 auto pars = m_regs.syscallPars();
89 if (number >= pars.size()) {
90 throw cosmos::UsageError{"invalid system call parameter nr."};
91 }
92
93 return pars[number];
94 }
95
97 auto& raw() const {
98 return m_regs;
99 }
100
101protected: // data
102
105};
106
108using AnyRegisterSet = std::variant<
113
114} // end ns
115
116template <clues::ABI abi>
117CLUES_API std::ostream& operator<<(std::ostream &o, const clues::RegisterSet<abi> &rs);
Holds a set of registers for the given ABI.
void fillIov(cosmos::InputMemoryRegion &iov)
Prepares iov for doing a ptrace system call of type ptrace::Request::GETREGSET.
register_t syscallRes() const
Returns the system call result on exit from a syscall.
SystemCallNr syscallNr() const
Returns the generic SystemCallNr on entry to a syscall.
register_t syscallParameter(const size_t number) const
Returns the content of the given system call parameter register.
auto & raw() const
Provides access to the raw RegisterData based data structure.
void iovFilled(const cosmos::InputMemoryRegion &iov)
Verify data received from a ptrace system call of type ptrace::Request::GETREGSET.
RegisterDataTraits< abi >::type ABIRegisterData
This is the concrete type holding the raw register data for this ABI.
auto abiSyscallNr() const
Returns the ABI-specific system call number on entry to a syscall.
ABIRegisterData m_regs
The raw data structure holding the ABI specific register data.
static constexpr cosmos::ptrace::RegisterType registerType()
The type to pass to ptrace::Request::GETREGSET for obtaining the general purpose registers.
std::variant< RegisterSet< ABI::I386 >, RegisterSet< ABI::X86_64 >, RegisterSet< ABI::X32 >, RegisterSet< ABI::AARCH64 > > AnyRegisterSet
A variant to hold any of the ABI-specific RegisterSet types.
SystemCallNr
Abstract system call number usable across architectures and ABIs.
Definition generic.hxx:29
clues::SystemCallNr to_generic(const SystemCallNrAARCH64 nr)
Convert the native system call nr. into its generic representation.
Definition aarch64.cxx:21
std::nullptr_t type
the proper RegisterData type for this ABI.
Definition traits.hxx:19