Differentiate errno from wait or ptrace call: no blanket From<Errno> impl

This commit is contained in:
Elnath 2025-05-17 16:55:39 +02:00
parent 3e4520e055
commit 22391a4fe9
1 changed files with 10 additions and 6 deletions

View File

@ -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<Stopped> {
pub fn new(pid: Pid) -> Result<Self, DebugError> {
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<DebugTarget<Running>, 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<Self, DebugError> {
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<Running> {
pub fn wait_for_exit(self) -> Result<i32, DebugError> {
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))
}