From 22391a4fe93237c9a26266b4cdf14486159871d6 Mon Sep 17 00:00:00 2001 From: Elnath Date: Sat, 17 May 2025 16:55:39 +0200 Subject: [PATCH] Differentiate errno from wait or ptrace call: no blanket From impl --- src/debug_target.rs | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/debug_target.rs b/src/debug_target.rs index 2ec1ce9..5976781 100644 --- a/src/debug_target.rs +++ b/src/debug_target.rs @@ -10,7 +10,10 @@ pub struct PTraceError(#[from] nix::errno::Errno); #[derive(thiserror::Error, Debug)] pub enum DebugError { #[error("Error when calling ptrace: {0}")] - PTraceError(#[from] nix::errno::Errno), // TODO: do not repeat above implem + PTraceError(nix::errno::Errno), // TODO: do not repeat above implem + + #[error("Error when waiting on child process: {0}")] + WaitError(nix::errno::Errno), #[error("Child stopped with status {0:?}, but was not expecting to catch this one")] UnexpectedWaitStatus(nix::sys::wait::WaitStatus), @@ -35,18 +38,19 @@ impl DebugState for Stopped { impl DebugTarget { pub fn new(pid: Pid) -> Result { - waitid(Id::Pid(pid), WaitPidFlag::WSTOPPED)?; + waitid(Id::Pid(pid), WaitPidFlag::WSTOPPED).map_err(DebugError::WaitError)?; Ok(DebugTarget { state: Stopped { pid } }) } pub fn cont(self) -> Result, DebugError> { - nix::sys::ptrace::cont(self.state.pid, None)?; + nix::sys::ptrace::cont(self.state.pid, None).map_err(DebugError::PTraceError)?; Ok(DebugTarget { state: Running { pid: self.state.pid } }) } pub fn stepi(self) -> Result { - nix::sys::ptrace::step(self.state.pid, None)?; - match waitid(Id::Pid(self.state.pid), WaitPidFlag::WSTOPPED | WaitPidFlag::WEXITED)? { + nix::sys::ptrace::step(self.state.pid, None).map_err(DebugError::PTraceError)?; + println!("ptrace actually fine"); + match waitid(Id::Pid(self.state.pid), WaitPidFlag::WSTOPPED).map_err(DebugError::WaitError)? { WaitStatus::PtraceEvent(_pid, Signal::SIGTRAP, _c_event) => Ok(self), status => Err(DebugError::UnexpectedWaitStatus(status)), } @@ -68,7 +72,7 @@ impl DebugState for Running { impl DebugTarget { pub fn wait_for_exit(self) -> Result { - match waitid(Id::Pid(self.state.pid), WaitPidFlag::WEXITED)? { + match waitid(Id::Pid(self.state.pid), WaitPidFlag::WEXITED).map_err(DebugError::WaitError)? { WaitStatus::Exited(_pid, exit_code) => Ok(exit_code), status => Err(DebugError::UnexpectedWaitStatus(status)) }