Skip to content

Commit 7f2ac63

Browse files
bors[bot]wpovell
andcommitted
Merge #1010
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 <[email protected]>
2 parents c2923ea + 2a437bf commit 7f2ac63

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
1313
([#972](https://github.com/nix-rust/nix/pull/972))
1414
- Added `symlinkat` wrapper.
1515
([#997](https://github.com/nix-rust/nix/pull/997))
16+
- Added `ptrace::{getregs, setregs}`.
17+
([#1010](https://github.com/nix-rust/nix/pull/1010))
1618

1719
### Changed
1820
### Fixed
@@ -46,7 +48,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
4648
- Added a `fchownat` wrapper.
4749
([#955](https://github.com/nix-rust/nix/pull/955))
4850
- Added support for `ptrace` on BSD operating systems ([#949](https://github.com/nix-rust/nix/pull/949))
49-
- Added `ptrace` functions for reads and writes to tracee memory and ptrace kill
51+
- Added `ptrace` functions for reads and writes to tracee memory and ptrace kill
5052
([#949](https://github.com/nix-rust/nix/pull/949)) ([#958](https://github.com/nix-rust/nix/pull/958))
5153
- Added a `acct` wrapper module for enabling and disabling process accounting
5254
([#952](https://github.com/nix-rust/nix/pull/952))

src/sys/ptrace/linux.rs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ use sys::signal::Signal;
99

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

12+
#[cfg(all(target_os = "linux",
13+
any(target_arch = "x86_64",
14+
target_arch = "x86"),
15+
target_env = "gnu"))]
16+
use libc::user_regs_struct;
17+
1218
cfg_if! {
1319
if #[cfg(any(all(target_os = "linux", target_arch = "s390x"),
1420
all(target_os = "linux", target_env = "gnu")))] {
@@ -192,6 +198,30 @@ fn ptrace_peek(request: Request, pid: Pid, addr: AddressType, data: *mut c_void)
192198
}
193199
}
194200

201+
/// Get user registers, as with `ptrace(PTRACE_GETREGS, ...)`
202+
#[cfg(all(target_os = "linux",
203+
any(target_arch = "x86_64",
204+
target_arch = "x86"),
205+
target_env = "gnu"))]
206+
pub fn getregs(pid: Pid) -> Result<user_regs_struct> {
207+
ptrace_get_data::<user_regs_struct>(Request::PTRACE_GETREGS, pid)
208+
}
209+
210+
/// Set user registers, as with `ptrace(PTRACE_SETREGS, ...)`
211+
#[cfg(all(target_os = "linux",
212+
any(target_arch = "x86_64",
213+
target_arch = "x86"),
214+
target_env = "gnu"))]
215+
pub fn setregs(pid: Pid, regs: user_regs_struct) -> Result<()> {
216+
let res = unsafe {
217+
libc::ptrace(Request::PTRACE_SETREGS as RequestType,
218+
libc::pid_t::from(pid),
219+
ptr::null_mut::<c_void>(),
220+
&regs as *const _ as *const c_void)
221+
};
222+
Errno::result(res).map(drop)
223+
}
224+
195225
/// Function for ptrace requests that return values from the data field.
196226
/// Some ptrace get requests populate structs or larger elements than `c_long`
197227
/// and therefore use the data field to return values. This function handles these
@@ -215,8 +245,6 @@ unsafe fn ptrace_other(request: Request, pid: Pid, addr: AddressType, data: *mut
215245

216246
/// Set options, as with `ptrace(PTRACE_SETOPTIONS,...)`.
217247
pub fn setoptions(pid: Pid, options: Options) -> Result<()> {
218-
use std::ptr;
219-
220248
let res = unsafe {
221249
libc::ptrace(Request::PTRACE_SETOPTIONS as RequestType,
222250
libc::pid_t::from(pid),

0 commit comments

Comments
 (0)