Skip to main content

relibc/header/sys_ptrace/
mod.rs

1//! `sys/ptrace.h` implementation.
2//!
3//! Non-POSIX, see <https://www.man7.org/linux/man-pages/man2/ptrace.2.html>.
4
5use crate::{
6    error::ResultExt,
7    platform::{PalPtrace, Sys, types::c_int},
8};
9
10/// Indicate that this process is to be traced by its parent.
11/// Only used by the tracee.
12pub const PTRACE_TRACEME: c_int = 0;
13/// Equivalent to `PTRACE_PEEKDATA`.
14pub const PTRACE_PEEKTEXT: c_int = 1;
15/// Read a word at the address `addr` in the tracee's memory, returning the
16/// word as a result of the `ptrace()` call.
17pub const PTRACE_PEEKDATA: c_int = 2;
18/// Equivalent to `PTRACE_POKEDATA`.
19pub const PTRACE_POKETEXT: c_int = 4;
20/// Copy the word `data` to the address `addr` in the tracee's memory.
21pub const PTRACE_POKEDATA: c_int = 5;
22/// Restart the stopped tracee process.
23pub const PTRACE_CONT: c_int = 7;
24/// Send the tracee a `SIGKILL` to terminate it.
25/// Considered deprecated. Should just send `SIGKILL` directly instead.
26pub const PTRACE_KILL: c_int = 8;
27/// Restart the stopped tracee as for `PTRACE_CONT`, but arrange for the
28/// tracee to be stopped after execution of a single instruction.
29pub const PTRACE_SINGLESTEP: c_int = 9;
30/// Copy the tracee's general-purpose registers to the address `data` in
31/// the tracer.
32pub const PTRACE_GETREGS: c_int = 12;
33/// Modify the tracee's general-purpose registers from the address `data`
34/// in the tracer.
35pub const PTRACE_SETREGS: c_int = 13;
36/// Copy the tracee's floating-point registers to the address `data` in
37/// the tracer.
38pub const PTRACE_GETFPREGS: c_int = 14;
39/// Modify the tracee's floating-point registers from the address `data`
40/// in the tracer.
41pub const PTRACE_SETFPREGS: c_int = 15;
42/// Attach to the process specified in `pid`, making it a tracee of the
43/// calling process.
44pub const PTRACE_ATTACH: c_int = 16;
45/// Restart the stopped tracee as for `PTRACE_CONT`, but first detach from it.
46pub const PTRACE_DETACH: c_int = 17;
47/// Restart the stopped tracee as for `PTRACE_CONT`, but arrange for the
48/// tracee to be stopped at the next entry to or exit from a system call.
49pub const PTRACE_SYSCALL: c_int = 24;
50/// Continue and stop on entry to the next system call, which will not be
51/// executed.
52pub const PTRACE_SYSEMU: c_int = 31;
53/// Same as `PTRACE_SYSEMU` but also singlestep if not a system call.
54pub const PTRACE_SYSEMU_SINGLESTEP: c_int = 32;
55
56/// Non-POSIX, see <https://www.man7.org/linux/man-pages/man2/ptrace.2.html>.
57///
58/// Provides a means by which one process (the "tracer") may observe and
59/// control the execution of another process (the "tracee"), and examine
60/// and change the tracee's memory and registers.
61///
62/// The `__valist` argument represents 3 arguments in this order:
63/// - pid
64/// - *addr
65/// - *data
66///
67/// Currently only implemented on x86_64.
68#[unsafe(no_mangle)]
69pub unsafe extern "C" fn ptrace(request: c_int, mut __valist: ...) -> c_int {
70    // Musl also just grabs the arguments from the varargs...
71    unsafe {
72        Sys::ptrace(
73            request,
74            __valist.next_arg(),
75            __valist.next_arg(),
76            __valist.next_arg(),
77        )
78    }
79    .or_minus_one_errno()
80}