Removed old PTraceError variant and renamed CustomPtraceError to PTraceError

This commit is contained in:
Elnath 2025-05-19 21:59:15 +02:00
parent e61f62487b
commit 123ec6e7d7
2 changed files with 14 additions and 21 deletions

View File

@ -8,11 +8,7 @@ pub use stopped_target::*;
#[derive(thiserror::Error, Debug)]
#[error("Error when calling ptrace: {0}")]
pub struct PTraceError(#[from] nix::errno::Errno);
#[derive(thiserror::Error, Debug)]
#[error("Error when calling ptrace: {0}")]
pub struct CustomPTraceError(#[from] clibwrap::ErrnoError);
pub struct PTraceError(#[from] clibwrap::ErrnoError);
#[derive(thiserror::Error, Debug)]
#[error("Error when waiting on child process: {0}")]
@ -23,9 +19,6 @@ pub enum DebugError {
#[error(transparent)]
PTraceError(#[from] PTraceError),
#[error(transparent)]
CustomPTraceError(#[from] CustomPTraceError),
#[error(transparent)]
WaitError(#[from] WaitError),

View File

@ -1,5 +1,5 @@
use crate::clibwrap;
use crate::debug_target::{CustomPTraceError, DebugError, RunningTarget, WaitError};
use crate::debug_target::{DebugError, PTraceError, RunningTarget, WaitError};
use crate::syscall_info::{syscall_info, SyscallInfo, SyscallInfoError};
use libc::c_long;
use nix::sys::wait::{waitid, Id, WaitPidFlag};
@ -21,11 +21,11 @@ impl StoppedTarget {
pub fn new(pid: Pid) -> Result<Self, DebugError> {
waitid(Id::Pid(pid), WaitPidFlag::WSTOPPED).map_err(WaitError)?;
// Needed for waiting on syscalls and apparently also for getting syscall info (can not get it to work without this)
clibwrap::ptrace::set_options(pid.as_raw(), clibwrap::ptrace::PTraceOptions::PTRACE_O_TRACESYSGOOD).map_err(CustomPTraceError)?;
clibwrap::ptrace::set_options(pid.as_raw(), clibwrap::ptrace::PTraceOptions::PTRACE_O_TRACESYSGOOD).map_err(PTraceError)?;
Ok(Self { pid, breakpoints: HashMap::new() })
}
pub fn on_breakpoint(&self) -> Result<bool, CustomPTraceError> {
pub fn on_breakpoint(&self) -> Result<bool, PTraceError> {
let rip = self.get_registers()?.rip;
Ok(self.breakpoints.contains_key(&(rip - 1)))
}
@ -34,7 +34,7 @@ impl StoppedTarget {
let mut registers = self.get_registers()?;
self.remove_breakpoint(registers.rip - 1)?;
registers.rip -= 1;
clibwrap::ptrace::set_regs(self.pid.as_raw(), registers).map_err(CustomPTraceError)?;
clibwrap::ptrace::set_regs(self.pid.as_raw(), registers).map_err(PTraceError)?;
Ok(())
}
@ -42,7 +42,7 @@ impl StoppedTarget {
if self.on_breakpoint()? {
self.breakpoint_remove_and_rewind()?;
}
clibwrap::ptrace::cont(self.pid.as_raw()).map_err(CustomPTraceError)?;
clibwrap::ptrace::cont(self.pid.as_raw()).map_err(PTraceError)?;
Ok(self.to_running())
}
@ -50,7 +50,7 @@ impl StoppedTarget {
if self.on_breakpoint()? {
self.breakpoint_remove_and_rewind()?;
}
clibwrap::ptrace::single_step(self.pid.as_raw()).map_err(CustomPTraceError)?;
clibwrap::ptrace::single_step(self.pid.as_raw()).map_err(PTraceError)?;
Ok(self.to_running())
}
@ -58,12 +58,12 @@ impl StoppedTarget {
if self.on_breakpoint()? {
self.breakpoint_remove_and_rewind()?;
}
clibwrap::ptrace::syscall(self.pid.as_raw()).map_err(CustomPTraceError)?;
clibwrap::ptrace::syscall(self.pid.as_raw()).map_err(PTraceError)?;
Ok(self.to_running())
}
pub fn get_registers(&self) -> Result<clibwrap::ptrace::registers, CustomPTraceError> {
clibwrap::ptrace::get_regs(self.pid.as_raw()).map_err(CustomPTraceError)
pub fn get_registers(&self) -> Result<clibwrap::ptrace::registers, PTraceError> {
clibwrap::ptrace::get_regs(self.pid.as_raw()).map_err(PTraceError)
}
pub fn get_syscall_info(&self) -> Result<SyscallInfo, SyscallInfoError> {
@ -75,10 +75,10 @@ impl StoppedTarget {
if self.breakpoints.contains_key(&address) {
return Err(DebugError::DuplicateBreakpoint { address });
}
let orig_bytes = clibwrap::ptrace::peek_data(self.pid.as_raw(), address).map_err(CustomPTraceError)?;
let orig_bytes = clibwrap::ptrace::peek_data(self.pid.as_raw(), address).map_err(PTraceError)?;
let target_byte: u8 = (orig_bytes & 0xFF as c_long) as u8;
let new_content = (orig_bytes & (!0xff as c_long)) | (0xCC as c_long);
clibwrap::ptrace::poke_data(self.pid.as_raw(), address, new_content).map_err(CustomPTraceError)?;
clibwrap::ptrace::poke_data(self.pid.as_raw(), address, new_content).map_err(PTraceError)?;
self.breakpoints.insert(address, target_byte);
Ok(())
@ -89,9 +89,9 @@ impl StoppedTarget {
match self.breakpoints.remove(&address) {
None => Err(DebugError::NonExistingBreakpoint { address }),
Some(original_byte) => {
let content = clibwrap::ptrace::peek_data(self.pid.as_raw(), address).map_err(CustomPTraceError)?;
let content = clibwrap::ptrace::peek_data(self.pid.as_raw(), address).map_err(PTraceError)?;
let new_content = (content & (!0xff as c_long)) | (original_byte as c_long);
clibwrap::ptrace::poke_data(self.pid.as_raw(), address, new_content).map_err(CustomPTraceError)?;
clibwrap::ptrace::poke_data(self.pid.as_raw(), address, new_content).map_err(PTraceError)?;
Ok(())
}
}