Skip to content

Commit d7b9e39

Browse files
committed
Implement enum Register and three high level wrappers
1 parent 07e6c2f commit d7b9e39

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

src/sys/ptrace.rs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@ use {Errno, Error, Result};
33
use libc::{c_void, c_long, siginfo_t};
44
use ::unistd::Pid;
55

6+
//------------------ First part: a low-level wrapper for ptrace -----------------//
7+
8+
#[cfg(all(target_os = "linux",
9+
any(target_arch = "x86",
10+
target_arch = "x86_64",
11+
target_arch = "arm")),
12+
)]
613
pub mod ptrace {
714
use libc::c_int;
815

@@ -140,3 +147,60 @@ pub fn ptrace_setsiginfo(pid: Pid, sig: &siginfo_t) -> Result<()> {
140147
Err(e) => Err(e),
141148
}
142149
}
150+
151+
//-------------------------- Second part: a low-level wrapper for ptrace ----------------------//
152+
153+
#[cfg(target_arch = "x86_64")]
154+
// We're going to export it anyway
155+
#[allow(dead_code)]
156+
#[allow(non_camel_case_types)]
157+
pub enum Register {
158+
R15 = 0 * 8,
159+
R14 = 1 * 8,
160+
R13 = 2 * 8,
161+
R12 = 3 * 8,
162+
RBP = 4 * 8,
163+
RBX = 5 * 8,
164+
R11 = 6 * 8,
165+
R10 = 7 * 8,
166+
R9 = 8 * 8,
167+
R8 = 9 * 8,
168+
RAX = 10 * 8,
169+
RCX = 11 * 8,
170+
RDX = 12 * 8,
171+
RSI = 13 * 8,
172+
RDI = 14 * 8,
173+
ORIG_RAX = 15 * 8,
174+
RIP = 16 * 8,
175+
CS = 17 * 8,
176+
EFLAGS = 18 * 8,
177+
RSP = 19 * 8,
178+
SS = 20 * 8,
179+
FS_BASE = 21 * 8,
180+
GS_BASE = 22 * 8,
181+
DS = 23 * 8,
182+
ES = 24 * 8,
183+
FS = 25 * 8,
184+
GS = 26 * 8,
185+
}
186+
187+
/// Makes the `PTRACE_SYSCALL` request to ptrace
188+
pub fn syscall(pid: Pid) -> Result<()> {
189+
ptrace(ptrace::PTRACE_SYSCALL, pid, ptr::null_mut(), ptr::null_mut()).map(|_| ()) // ignore the useless return value
190+
}
191+
192+
/// Makes the `PTRACE_PEEKUSER` request to ptrace
193+
pub fn peekuser(pid: Pid, reg: Register) -> Result<c_long> {
194+
let reg_arg = (reg as i32) as *mut c_void;
195+
ptrace(ptrace::PTRACE_PEEKUSER, pid, reg_arg, ptr::null_mut())
196+
}
197+
198+
/// Sets the process as traceable with `PTRACE_TRACEME`
199+
pub fn traceme() -> Result<()> {
200+
ptrace(
201+
ptrace::PTRACE_TRACEME,
202+
Pid::from_raw(0),
203+
ptr::null_mut(),
204+
ptr::null_mut(),
205+
).map(|_| ()) // ignore the useless return value
206+
}

0 commit comments

Comments
 (0)