Skip to content

Commit 56354b9

Browse files
committed
sys::ptrace: adding ::syscall_info() for linux/glibc.
to retrieve the very system call which stopped the process. Can be found the type of calls and their related informations. e.g. on exit we get the return value of the call.
1 parent a397b08 commit 56354b9

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

src/sys/ptrace/linux.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ libc_enum! {
146146
#[cfg(all(target_os = "linux", target_env = "gnu",
147147
any(target_arch = "x86", target_arch = "x86_64")))]
148148
PTRACE_SYSEMU_SINGLESTEP,
149+
#[cfg(all(target_os = "linux", target_env = "gnu"))]
150+
PTRACE_GET_SYSCALL_INFO,
149151
}
150152
}
151153

@@ -567,6 +569,12 @@ pub fn setsiginfo(pid: Pid, sig: &siginfo_t) -> Result<()> {
567569
}
568570
}
569571

572+
/// Get the informations of the syscall that caused the stop.
573+
#[cfg(all(target_os = "linux", target_env = "gnu"))]
574+
pub fn syscall_info(pid: Pid) -> Result<libc::ptrace_syscall_info> {
575+
ptrace_get_data::<libc::ptrace_syscall_info>(Request::PTRACE_GET_SYSCALL_INFO, pid)
576+
}
577+
570578
/// Sets the process as traceable, as with `ptrace(PTRACE_TRACEME, ...)`
571579
///
572580
/// Indicates that this process is to be traced by its parent.

test/sys/test_ptrace.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,3 +381,29 @@ fn test_ptrace_regsets() {
381381
}
382382
}
383383
}
384+
385+
#[cfg(all(target_os = "linux", target_env = "gnu"))]
386+
#[test]
387+
fn test_ptrace_syscall_info() {
388+
use nix::sys::ptrace;
389+
use nix::sys::wait::{waitpid, WaitStatus};
390+
use nix::unistd::fork;
391+
use nix::unistd::ForkResult::*;
392+
393+
require_capability!("test_ptrace_interrupt", CAP_SYS_PTRACE);
394+
395+
let _m = crate::FORK_MTX.lock();
396+
match unsafe { fork() }.expect("Error: Fork Failed") {
397+
Child => {
398+
ptrace::traceme().unwrap();
399+
unsafe {
400+
::libc::_exit(0);
401+
}
402+
}
403+
Parent { child } => {
404+
assert_eq!(waitpid(child, None), Ok(WaitStatus::Exited(child, 0)));
405+
let si = ptrace::syscall_info(child).unwrap();
406+
assert!(si.op >= libc::PTRACE_SYSCALL_INFO_ENTRY);
407+
}
408+
}
409+
}

0 commit comments

Comments
 (0)