Skip to content

Commit

Permalink
Merge #1010
Browse files Browse the repository at this point in the history
1010: Added `ptrace::{getregs, setregs}` r=asomers a=wpovell

I needed `ptrace`'s `getregs` and `setregs` for a project I was working on, so I added them! First time contributor, so let me know if I should change / fix anything. Things work fine in my project which uses both functions, let me know if further testing is needed.

Co-authored-by: Will Povell <william_povell@brown.edu>
  • Loading branch information
bors[bot] and wpovell committed Jan 10, 2019
2 parents c2923ea + 2a437bf commit 7f2ac63
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
([#972](https://github.com/nix-rust/nix/pull/972))
- Added `symlinkat` wrapper.
([#997](https://github.com/nix-rust/nix/pull/997))
- Added `ptrace::{getregs, setregs}`.
([#1010](https://github.com/nix-rust/nix/pull/1010))

### Changed
### Fixed
Expand Down Expand Up @@ -46,7 +48,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
- Added a `fchownat` wrapper.
([#955](https://github.com/nix-rust/nix/pull/955))
- Added support for `ptrace` on BSD operating systems ([#949](https://github.com/nix-rust/nix/pull/949))
- Added `ptrace` functions for reads and writes to tracee memory and ptrace kill
- Added `ptrace` functions for reads and writes to tracee memory and ptrace kill
([#949](https://github.com/nix-rust/nix/pull/949)) ([#958](https://github.com/nix-rust/nix/pull/958))
- Added a `acct` wrapper module for enabling and disabling process accounting
([#952](https://github.com/nix-rust/nix/pull/952))
Expand Down
32 changes: 30 additions & 2 deletions src/sys/ptrace/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ use sys::signal::Signal;

pub type AddressType = *mut ::libc::c_void;

#[cfg(all(target_os = "linux",
any(target_arch = "x86_64",
target_arch = "x86"),
target_env = "gnu"))]
use libc::user_regs_struct;

cfg_if! {
if #[cfg(any(all(target_os = "linux", target_arch = "s390x"),
all(target_os = "linux", target_env = "gnu")))] {
Expand Down Expand Up @@ -192,6 +198,30 @@ fn ptrace_peek(request: Request, pid: Pid, addr: AddressType, data: *mut c_void)
}
}

/// Get user registers, as with `ptrace(PTRACE_GETREGS, ...)`
#[cfg(all(target_os = "linux",
any(target_arch = "x86_64",
target_arch = "x86"),
target_env = "gnu"))]
pub fn getregs(pid: Pid) -> Result<user_regs_struct> {
ptrace_get_data::<user_regs_struct>(Request::PTRACE_GETREGS, pid)
}

/// Set user registers, as with `ptrace(PTRACE_SETREGS, ...)`
#[cfg(all(target_os = "linux",
any(target_arch = "x86_64",
target_arch = "x86"),
target_env = "gnu"))]
pub fn setregs(pid: Pid, regs: user_regs_struct) -> Result<()> {
let res = unsafe {
libc::ptrace(Request::PTRACE_SETREGS as RequestType,
libc::pid_t::from(pid),
ptr::null_mut::<c_void>(),
&regs as *const _ as *const c_void)
};
Errno::result(res).map(drop)
}

/// Function for ptrace requests that return values from the data field.
/// Some ptrace get requests populate structs or larger elements than `c_long`
/// and therefore use the data field to return values. This function handles these
Expand All @@ -215,8 +245,6 @@ unsafe fn ptrace_other(request: Request, pid: Pid, addr: AddressType, data: *mut

/// Set options, as with `ptrace(PTRACE_SETOPTIONS,...)`.
pub fn setoptions(pid: Pid, options: Options) -> Result<()> {
use std::ptr;

let res = unsafe {
libc::ptrace(Request::PTRACE_SETOPTIONS as RequestType,
libc::pid_t::from(pid),
Expand Down

0 comments on commit 7f2ac63

Please sign in to comment.