Skip to content

Commit 525c74d

Browse files
authored
sys::ptrace: adding ::syscall_info() for linux/glibc. (#2627)
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 525c74d

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

changelog/2627.added.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add `ptrace::syscall_info` for linux/glibc

src/sys/ptrace/linux.rs

+9
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,13 @@ pub fn setsiginfo(pid: Pid, sig: &siginfo_t) -> Result<()> {
567569
}
568570
}
569571

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

test/sys/test_ptrace.rs

+29
Original file line numberDiff line numberDiff line change
@@ -381,3 +381,32 @@ 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_syscall_info", 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+
std::thread::sleep(std::time::Duration::from_millis(1000));
400+
unsafe {
401+
::libc::_exit(0);
402+
}
403+
}
404+
Parent { child } => loop {
405+
if let Ok(WaitStatus::Exited(_, 0)) = waitpid(child, None) {
406+
break;
407+
}
408+
let si = ptrace::syscall_info(child).unwrap();
409+
assert!(si.op >= libc::PTRACE_SYSCALL_INFO_ENTRY);
410+
},
411+
}
412+
}

0 commit comments

Comments
 (0)