1use crate::sys::{Result, check};
4
5pub const DEFAULT_OPTIONS: i64 = (libc::PTRACE_O_TRACESYSGOOD
7 | libc::PTRACE_O_TRACEFORK
8 | libc::PTRACE_O_TRACEVFORK
9 | libc::PTRACE_O_TRACECLONE
10 | libc::PTRACE_O_TRACEEXEC
11 | libc::PTRACE_O_TRACESECCOMP) as i64;
12
13pub const OPTION_EXITKILL: i64 = libc::PTRACE_O_EXITKILL as i64;
15
16pub fn seize(pid: i32, options: i64) -> Result<()> {
26 check(unsafe { libc::ptrace(libc::PTRACE_SEIZE, pid, 0, options) }).map(|_| ())
27}
28
29pub fn listen(pid: i32) -> Result<()> {
31 check(unsafe { libc::ptrace(libc::PTRACE_LISTEN, pid, 0, 0) }).map(|_| ())
32}
33
34#[allow(dead_code)]
35pub fn setoptions(pid: i32, options: i64) -> Result<()> {
41 check(unsafe { libc::ptrace(libc::PTRACE_SETOPTIONS, pid, 0, options) }).map(|_| ())
42}
43
44pub fn cont(pid: i32, sig: i32) -> Result<()> {
46 check(unsafe { libc::ptrace(libc::PTRACE_CONT, pid, 0, sig as i64) }).map(|_| ())
47}
48
49pub fn getsiginfo(pid: i32) -> Result<libc::siginfo_t> {
51 let mut si: libc::siginfo_t = unsafe { std::mem::zeroed() };
52 check(unsafe { libc::ptrace(libc::PTRACE_GETSIGINFO, pid, 0, &mut si as *mut _) })?;
53 Ok(si)
54}
55
56pub fn geteventmsg(pid: i32) -> Result<u64> {
58 let mut msg: u64 = 0;
59 check(unsafe { libc::ptrace(libc::PTRACE_GETEVENTMSG, pid, 0, &mut msg as *mut u64) })?;
60 Ok(msg)
61}
62
63#[cfg(target_arch = "x86_64")]
65pub fn getregs(pid: i32) -> Result<libc::user_regs_struct> {
66 let mut regs: libc::user_regs_struct = unsafe { std::mem::zeroed() };
67 check(unsafe { libc::ptrace(libc::PTRACE_GETREGS, pid, 0, &mut regs as *mut _) })?;
68 Ok(regs)
69}
70
71#[cfg(target_arch = "x86_64")]
73pub fn setregs(pid: i32, regs: &libc::user_regs_struct) -> Result<()> {
74 check(unsafe { libc::ptrace(libc::PTRACE_SETREGS, pid, 0, regs as *const _) }).map(|_| ())
75}
76
77#[allow(dead_code)]
88pub fn getregset(pid: i32, nt: u32, buf: *mut u8, len: usize) -> Result<usize> {
89 let mut iov = libc::iovec {
90 iov_base: buf as *mut _,
91 iov_len: len,
92 };
93 check(unsafe { libc::ptrace(libc::PTRACE_GETREGSET, pid, nt as usize, &mut iov as *mut _) })?;
94 Ok(iov.iov_len)
95}
96
97#[allow(dead_code)]
104pub fn setregset(pid: i32, nt: u32, buf: *mut u8, len: usize) -> Result<()> {
105 let mut iov = libc::iovec {
106 iov_base: buf as *mut _,
107 iov_len: len,
108 };
109 check(unsafe { libc::ptrace(libc::PTRACE_SETREGSET, pid, nt as usize, &mut iov as *mut _) }).map(|_| ())
110}