From b20fa4672dcacacbe765a1a590975e9c534bc166 Mon Sep 17 00:00:00 2001 From: Elnath Date: Sat, 17 May 2025 17:12:20 +0200 Subject: [PATCH] More specific error types for functions --- src/debug_target.rs | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/src/debug_target.rs b/src/debug_target.rs index 5976781..4e7685a 100644 --- a/src/debug_target.rs +++ b/src/debug_target.rs @@ -8,12 +8,16 @@ use nix::unistd::Pid; pub struct PTraceError(#[from] nix::errno::Errno); #[derive(thiserror::Error, Debug)] -pub enum DebugError { - #[error("Error when calling ptrace: {0}")] - PTraceError(nix::errno::Errno), // TODO: do not repeat above implem +#[error("Error when waiting on child process: {0}")] +pub struct WaitError(#[from] nix::errno::Errno); - #[error("Error when waiting on child process: {0}")] - WaitError(nix::errno::Errno), +#[derive(thiserror::Error, Debug)] +pub enum DebugError { + #[error(transparent)] + PTraceError(#[from] PTraceError), + + #[error(transparent)] + WaitError(#[from] WaitError), #[error("Child stopped with status {0:?}, but was not expecting to catch this one")] UnexpectedWaitStatus(nix::sys::wait::WaitStatus), @@ -37,20 +41,19 @@ impl DebugState for Stopped { } impl DebugTarget { - pub fn new(pid: Pid) -> Result { - waitid(Id::Pid(pid), WaitPidFlag::WSTOPPED).map_err(DebugError::WaitError)?; + pub fn new(pid: Pid) -> Result { + waitid(Id::Pid(pid), WaitPidFlag::WSTOPPED).map_err(WaitError)?; Ok(DebugTarget { state: Stopped { pid } }) } - pub fn cont(self) -> Result, DebugError> { - nix::sys::ptrace::cont(self.state.pid, None).map_err(DebugError::PTraceError)?; + pub fn cont(self) -> Result, PTraceError> { + nix::sys::ptrace::cont(self.state.pid, None).map_err(PTraceError)?; Ok(DebugTarget { state: Running { pid: self.state.pid } }) } pub fn stepi(self) -> Result { - 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)? { + nix::sys::ptrace::step(self.state.pid, None).map_err(PTraceError)?; + match waitid(Id::Pid(self.state.pid), WaitPidFlag::WSTOPPED).map_err(WaitError)? { WaitStatus::PtraceEvent(_pid, Signal::SIGTRAP, _c_event) => Ok(self), status => Err(DebugError::UnexpectedWaitStatus(status)), } @@ -72,7 +75,7 @@ impl DebugState for Running { impl DebugTarget { pub fn wait_for_exit(self) -> Result { - match waitid(Id::Pid(self.state.pid), WaitPidFlag::WEXITED).map_err(DebugError::WaitError)? { + match waitid(Id::Pid(self.state.pid), WaitPidFlag::WEXITED).map_err(WaitError)? { WaitStatus::Exited(_pid, exit_code) => Ok(exit_code), status => Err(DebugError::UnexpectedWaitStatus(status)) }