#[cfg(linux_android)] // FIXME: qemu-user doesn't implement ptrace on most arches #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] mod ptrace { usecrate::*; use libc::_exit; use nix::sys::ptrace::{self, Event, Options}; use nix::sys::signal::*; use nix::sys::wait::*; use nix::unistd::ForkResult::*; use nix::unistd::*;
fn ptrace_child() -> ! {
ptrace::traceme().unwrap(); // As recommended by ptrace(2), raise SIGTRAP to pause the child // until the parent is ready to continue
raise(SIGTRAP).unwrap(); unsafe { _exit(0) }
}
fn ptrace_wait_parent(child: Pid) { // Wait for the raised SIGTRAP
assert_eq!(
waitpid(child, None),
Ok(WaitStatus::Stopped(child, SIGTRAP))
); // We want to test a syscall stop and a PTRACE_EVENT stop
ptrace::setoptions(
child,
Options::PTRACE_O_TRACESYSGOOD | Options::PTRACE_O_TRACEEXIT,
)
.expect("setoptions failed");
// First, stop on the next system call, which will be exit()
ptrace::syscall(child, None).expect("syscall failed");
assert_eq!(waitpid(child, None), Ok(WaitStatus::PtraceSyscall(child))); // Then get the ptrace event for the process exiting
ptrace::cont(child, None).expect("cont failed");
assert_eq!(
waitpid(child, None),
Ok(WaitStatus::PtraceEvent(
child,
SIGTRAP,
Event::PTRACE_EVENT_EXIT as i32
))
); // Finally get the normal wait() result, now that the process has exited
ptrace::cont(child, None).expect("cont failed");
assert_eq!(waitpid(child, None), Ok(WaitStatus::Exited(child, 0)));
}
#[cfg(not(target_env = "uclibc"))] fn ptrace_waitid_parent(child: Pid) { // Wait for the raised SIGTRAP // // Unlike waitpid(), waitid() can distinguish trap events from regular // stop events, so unlike ptrace_wait_parent(), we get a PtraceEvent here
assert_eq!(
waitid(Id::Pid(child), WaitPidFlag::WEXITED),
Ok(WaitStatus::PtraceEvent(child, SIGTRAP, 0)),
); // We want to test a syscall stop and a PTRACE_EVENT stop
ptrace::setoptions(
child,
Options::PTRACE_O_TRACESYSGOOD | Options::PTRACE_O_TRACEEXIT,
)
.expect("setopts failed");
// First, stop on the next system call, which will be exit()
ptrace::syscall(child, None).expect("syscall failed");
assert_eq!(
waitid(Id::Pid(child), WaitPidFlag::WEXITED),
Ok(WaitStatus::PtraceSyscall(child)),
); // Then get the ptrace event for the process exiting
ptrace::cont(child, None).expect("cont failed");
assert_eq!(
waitid(Id::Pid(child), WaitPidFlag::WEXITED),
Ok(WaitStatus::PtraceEvent(
child,
SIGTRAP,
Event::PTRACE_EVENT_EXIT as i32
)),
); // Finally get the normal wait() result, now that the process has exited
ptrace::cont(child, None).expect("cont failed");
assert_eq!(
waitid(Id::Pid(child), WaitPidFlag::WEXITED),
Ok(WaitStatus::Exited(child, 0)),
);
}
#[test] fn test_wait_ptrace() {
require_capability!("test_wait_ptrace", CAP_SYS_PTRACE); let _m = crate::FORK_MTX.lock();
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.