1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
use core::fmt;
use libc::user_regs_struct;
use nix::{sys::ptrace, unistd::Pid};
use crate::diag::Result;
/// Helpers for reading and writing register state of the traced process.
///
/// `Registers` stores a snapshot of the platform `user_regs_struct` for a
/// given `Pid` and provides convenience accessors. Currently this module
/// assumes `x86_64` register layout.
pub struct Registers {
pid: Pid,
regs: user_regs_struct,
}
impl Registers {
/// Read the current register state for `pid`.
///
/// # Arguments
///
/// * `pid` - The process id of the traced process to read registers from.
///
/// # Errors
///
/// Returns an error if the underlying `ptrace::getregs` call fails.
///
/// # Returns
///
/// A `Result` containing the `Registers` snapshot on success.
pub fn read(pid: Pid) -> Result<Self> {
Ok(Self {
pid,
regs: ptrace::getregs(pid)?,
})
}
/// Write the locally-modified register state back to the tracee.
///
/// # Errors
///
/// Returns an error if `ptrace::setregs` fails.
///
/// # Returns
///
/// Returns `Ok(())` on success.
pub fn write(&self) -> Result<()> {
ptrace::setregs(self.pid, self.regs)?;
Ok(())
}
#[must_use]
/// Instruction pointer (RIP).
///
/// # Returns
///
/// The current value of the instruction pointer (RIP) for the stored
/// register snapshot.
pub fn rip(&self) -> u64 {
self.regs.rip
}
/// Set the instruction pointer.
///
/// # Arguments
///
/// * `value` - The new instruction pointer (RIP) value to store in the snapshot.
pub fn set_rip(&mut self, value: u64) {
self.regs.rip = value;
}
#[must_use]
/// Stack pointer (RSP).
///
/// # Returns
///
/// The current value of the stack pointer (RSP).
pub fn rsp(&self) -> u64 {
self.regs.rsp
}
#[must_use]
/// Return the RAX register value.
///
/// # Returns
///
/// The value of the `rax` register in the snapshot.
pub fn rax(&self) -> u64 {
self.regs.rax
}
#[must_use]
/// Return the original RAX value saved by the kernel (useful for syscalls).
///
/// # Returns
///
/// The `orig_rax` register value recorded by the kernel.
pub fn orig_rax(&self) -> u64 {
self.regs.orig_rax
}
#[must_use]
/// Return the 6 register parameters passed in registers for `x86_64`
/// (rdi, rsi, rdx, r10, r8, r9).
///
/// # Returns
///
/// An array with the six parameter registers as u64 in calling
/// convention order.
pub fn function_params(&self) -> [u64; 6] {
[
self.regs.rdi,
self.regs.rsi,
self.regs.rdx,
self.regs.r10,
self.regs.r8,
self.regs.r9,
]
}
}
impl fmt::Display for Registers {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
writeln!(f, "rip: {:#x}", self.regs.rip)?;
writeln!(f, "rsp: {:#x}", self.regs.rsp)?;
writeln!(f, "rbp: {:#x}", self.regs.rbp)?;
writeln!(f, "eflags: {:#x}", self.regs.eflags)?;
writeln!(f, "orig_rax: {:#x}", self.regs.orig_rax)?;
writeln!(f, "rax: {:#x}", self.regs.rax)?;
writeln!(f, "rbx: {:#x}", self.regs.rbx)?;
writeln!(f, "rcx: {:#x}", self.regs.rcx)?;
writeln!(f, "rdx: {:#x}", self.regs.rdx)?;
writeln!(f, "rdi: {:#x}", self.regs.rdi)?;
writeln!(f, "rsi: {:#x}", self.regs.rsi)?;
writeln!(f, "r8: {:#x}", self.regs.r8)?;
writeln!(f, "r9: {:#x}", self.regs.r9)?;
writeln!(f, "r10: {:#x}", self.regs.r10)?;
writeln!(f, "r11: {:#x}", self.regs.r11)?;
writeln!(f, "r12: {:#x}", self.regs.r12)?;
writeln!(f, "r13: {:#x}", self.regs.r13)?;
writeln!(f, "r14: {:#x}", self.regs.r14)?;
writeln!(f, "r15: {:#x}", self.regs.r15)?;
writeln!(f, "cs: {:#x}", self.regs.cs)?;
writeln!(f, "ds: {:#x}", self.regs.ds)?;
writeln!(f, "es: {:#x}", self.regs.es)?;
writeln!(f, "fs: {:#x}", self.regs.fs)?;
writeln!(f, "gs: {:#x}", self.regs.gs)?;
writeln!(f, "ss: {:#x}", self.regs.ss)?;
writeln!(f, "fs_base: {:#x}", self.regs.fs_base)?;
write!(f, "gs_base: {:#x}", self.regs.gs_base)
}
}
#[cfg(test)]
mod tests {
use super::*;
use libc::user_regs_struct;
use nix::unistd::Pid;
fn make_regs() -> user_regs_struct {
user_regs_struct {
r15: 0,
r14: 0,
r13: 0,
r12: 0,
rbp: 0x2000,
rbx: 0x3000,
r11: 0,
r10: 0,
r9: 0,
r8: 0,
rax: 1,
rcx: 2,
rdx: 3,
rsi: 4,
rdi: 5,
orig_rax: 0,
rip: 0x1000,
cs: 0,
eflags: 0,
rsp: 0x4000,
ss: 0,
fs_base: 0,
gs_base: 0,
ds: 0,
es: 0,
fs: 0,
gs: 0,
}
}
#[test]
fn test_registers_accessors_and_display() {
let regs = make_regs();
let r = Registers {
pid: Pid::from_raw(1),
regs,
};
assert_eq!(r.rip(), 0x1000);
assert_eq!(r.rsp(), 0x4000);
assert_eq!(r.rax(), 1);
assert_eq!(r.function_params()[0], 5);
let s = format!("{}", r);
assert!(s.contains("rip:"));
assert!(s.contains("rsp:"));
}
}