Starting to write custom clib wrapper

This commit is contained in:
Elnath 2025-05-18 23:06:11 +02:00
parent 9822c95c37
commit c7bf8c341c
5 changed files with 50 additions and 2 deletions

11
src/clibwrap/mod.rs Normal file
View File

@ -0,0 +1,11 @@
use std::ffi::c_long;
pub mod ptrace;
type Pid = i32;
#[derive(Debug, thiserror::Error)]
#[error("Some kind of error when calling into C code: return code {error_code}")]
pub struct CError {
error_code: c_long,
}

31
src/clibwrap/ptrace.rs Normal file
View File

@ -0,0 +1,31 @@
use crate::clibwrap::{CError, Pid};
use std::ffi::{c_long, c_void};
#[allow(non_camel_case_types)]
#[repr(u32)]
enum PTraceRequest {
PTRACE_CONT = 7,
PTRACE_SINGLESTEP = 9,
}
unsafe extern "C" {
fn ptrace(request: PTraceRequest, pid: Pid, addr: *mut c_void, data: *mut c_void) -> c_long;
}
pub fn cont(pid: Pid) -> Result<(), CError> {
let return_code = unsafe { ptrace(PTraceRequest::PTRACE_CONT, pid, 0 as _, 0 as _) };
if return_code == 0 {
Ok(())
} else {
Err(CError { error_code: return_code })
}
}
pub fn single_step(pid: Pid) -> Result<(), CError> {
let return_code = unsafe { ptrace(PTraceRequest::PTRACE_SINGLESTEP, pid, 0 as _, 0 as _) };
if return_code == 0 {
Ok(())
} else {
Err(CError { error_code: return_code })
}
}

View File

@ -1,6 +1,7 @@
mod stopped_target; mod stopped_target;
mod running_target; mod running_target;
use crate::clibwrap;
use nix::unistd::Pid; use nix::unistd::Pid;
pub use running_target::*; pub use running_target::*;
pub use stopped_target::*; pub use stopped_target::*;
@ -18,6 +19,9 @@ pub enum DebugError {
#[error(transparent)] #[error(transparent)]
PTraceError(#[from] PTraceError), PTraceError(#[from] PTraceError),
#[error(transparent)]
CustomPTraceError(#[from] clibwrap::CError),
#[error(transparent)] #[error(transparent)]
WaitError(#[from] WaitError), WaitError(#[from] WaitError),

View File

@ -1,3 +1,4 @@
use crate::clibwrap;
use crate::debug_target::{DebugError, PTraceError, RunningTarget, WaitError}; use crate::debug_target::{DebugError, PTraceError, RunningTarget, WaitError};
use crate::syscall_info::{syscall_info, SyscallInfo, SyscallInfoError}; use crate::syscall_info::{syscall_info, SyscallInfo, SyscallInfoError};
use libc::{c_long, user_regs_struct}; use libc::{c_long, user_regs_struct};
@ -42,7 +43,7 @@ impl StoppedTarget {
if self.on_breakpoint()? { if self.on_breakpoint()? {
self.breakpoint_remove_and_rewind()?; self.breakpoint_remove_and_rewind()?;
} }
nix::sys::ptrace::cont(self.pid, None).map_err(PTraceError)?; clibwrap::ptrace::cont(self.pid.as_raw())?;
Ok(self.to_running()) Ok(self.to_running())
} }
@ -50,7 +51,7 @@ impl StoppedTarget {
if self.on_breakpoint()? { if self.on_breakpoint()? {
self.breakpoint_remove_and_rewind()?; self.breakpoint_remove_and_rewind()?;
} }
nix::sys::ptrace::step(self.pid, None).map_err(PTraceError)?; clibwrap::ptrace::single_step(self.pid.as_raw())?;
Ok(self.to_running()) Ok(self.to_running())
} }

View File

@ -1,5 +1,6 @@
mod debug_target; mod debug_target;
mod syscall_info; mod syscall_info;
mod clibwrap;
use crate::debug_target::{ExitedTarget, StoppedTarget}; use crate::debug_target::{ExitedTarget, StoppedTarget};
use color_eyre::eyre::eyre; use color_eyre::eyre::eyre;