Skip to content

Commit 00364df

Browse files
committed
Implement enum Register and three high level wrappers
1 parent d0628e1 commit 00364df

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

src/sys/ptrace.rs

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

0 commit comments

Comments
 (0)