Skip to content

Commit 4311307

Browse files
committed
Build against nix HEAD
Now that process_vm_readv has been merged, I no longer need a local fork of nix, so hopefully this compiles for other people now.
1 parent 8fbcd9b commit 4311307

File tree

3 files changed

+25
-26
lines changed

3 files changed

+25
-26
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ version = "0.1.0"
44
authors = ["Geoffrey Thomas <geofft@ldpreload.com>"]
55

66
[dependencies]
7-
nix = { path = "../nix-rust" } # "0.8"
7+
nix = { git = "https://github.com/nix-rust/nix" }
88
libc = "0.2"
99
serde = "0.9"
1010
serde_derive = "0.9"

src/bin/gtrace.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ fn main() {
1616
}
1717
cmd.before_exec(gtrace::traceme);
1818
let mut child = cmd.spawn().expect("child process failed");
19-
let pid: libc::pid_t = child.id() as libc::pid_t;
19+
let pid = nix::unistd::Pid::from_raw(child.id() as libc::pid_t);
2020
let mut tracee = gtrace::Tracee::new(pid);
2121
loop {
2222
match tracee.step(waitpid(pid, None).unwrap()) {

src/lib.rs

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,31 @@ extern crate serde_derive;
66
#[macro_use]
77
extern crate gtrace_derive;
88

9-
//extern crate libc;
9+
extern crate libc;
1010
extern crate nix;
1111

12-
use nix::libc;
13-
14-
use libc::pid_t;
12+
use nix::unistd::Pid;
1513
use nix::sys::wait::WaitStatus;
16-
use nix::sys::ptrace::*;
17-
use nix::sys::ptrace::ptrace::*;
14+
use nix::sys::ptrace;
1815
use nix::Result;
1916

2017
pub mod arch;
2118
pub mod decode;
2219
pub mod syscall;
2320

2421
pub fn traceme() -> std::io::Result<()> {
25-
match ptrace(PTRACE_TRACEME, 0, 0 as *mut _, 0 as *mut _) {
26-
Ok(_) => Ok(()),
27-
Err(e) => Err(std::io::Error::from(e))
22+
match ptrace::traceme() {
23+
Ok(()) => Ok(()),
24+
Err(::nix::Error::Sys(errno)) => Err(std::io::Error::from_raw_os_error(errno as i32)),
25+
Err(e) => Err(std::io::Error::new(std::io::ErrorKind::Other, e))
2826
}
2927
}
3028

3129
pub enum TraceEvent {
3230
SysEnter,
3331
SysExit,
3432
Signal(u8),
35-
Exit(i8),
33+
Exit(i32),
3634
}
3735

3836
enum State {
@@ -41,13 +39,14 @@ enum State {
4139
}
4240

4341
pub struct Tracee {
44-
pid: pid_t,
42+
pid: Pid,
4543
state: State,
4644
}
4745

4846
impl Tracee {
49-
pub fn new(pid: pid_t) -> Tracee {
50-
ptrace_setoptions(pid, PTRACE_O_TRACESYSGOOD | PTRACE_O_TRACEEXEC).unwrap();
47+
pub fn new(pid: Pid) -> Tracee {
48+
ptrace::setoptions(pid, ptrace::PTRACE_O_TRACESYSGOOD |
49+
ptrace::PTRACE_O_TRACEEXEC).unwrap();
5150
Tracee {
5251
pid: pid,
5352
state: State::Userspace,
@@ -75,11 +74,12 @@ impl Tracee {
7574
}
7675

7776
pub fn run(&mut self) -> Result<()> {
78-
ptrace(PTRACE_SYSCALL, self.pid, 0 as *mut _, 0 as *mut _).map(drop)
77+
ptrace::syscall(self.pid)
7978
}
8079

8180
pub fn get_syscall(&mut self) -> Result<u64> {
82-
ptrace(PTRACE_PEEKUSER, self.pid, arch::x86_64::ORIG_RAX as *mut _, 0 as *mut _).map(|x| x as u64)
81+
unsafe { ptrace::ptrace(ptrace::Request::PTRACE_PEEKUSER, self.pid,
82+
arch::x86_64::ORIG_RAX as *mut _, 0 as *mut _).map(|x| x as u64) }
8383
}
8484

8585
pub fn get_arg(&mut self, reg: u8) -> Result<u64> {
@@ -92,11 +92,13 @@ impl Tracee {
9292
5 => arch::x86_64::R9,
9393
_ => panic!("there aren't that many registers")
9494
};
95-
ptrace(PTRACE_PEEKUSER, self.pid, offset as *mut _, 0 as *mut _).map(|x| x as u64)
95+
unsafe { ptrace::ptrace(ptrace::Request::PTRACE_PEEKUSER, self.pid,
96+
offset as *mut _, 0 as *mut _).map(|x| x as u64) }
9697
}
9798

9899
pub fn get_return(&mut self) -> Result<i64> {
99-
ptrace(PTRACE_PEEKUSER, self.pid, arch::x86_64::RAX as *mut _, 0 as *mut _)
100+
unsafe { ptrace::ptrace(ptrace::Request::PTRACE_PEEKUSER, self.pid,
101+
arch::x86_64::RAX as *mut _, 0 as *mut _) }
100102
}
101103

102104
/// Read len bytes from addr. May return fewer than len bytes if
@@ -109,13 +111,12 @@ impl Tracee {
109111
unsafe {
110112
res.set_len(len);
111113
let target: Vec<_> = PageIter::new(addr, len, arch::x86_64::PAGE_SIZE)
112-
.map(|(a, l)| IoVec::from_slice(std::slice::from_raw_parts(a as *const _, l)))
114+
.map(|(a, l)| RemoteIoVec { base: a, len: l })
113115
.collect();
114116
let n = {
115117
try!(process_vm_readv(self.pid,
116118
&mut [IoVec::from_mut_slice(&mut res)],
117-
&target[..],
118-
CmaFlags::empty()))
119+
&target[..]))
119120
};
120121
res.set_len(n);
121122
}
@@ -145,8 +146,7 @@ impl Tracee {
145146
let n = {
146147
try!(process_vm_readv(self.pid,
147148
&mut [IoVec::from_mut_slice(&mut res)],
148-
&[IoVec::from_slice(std::slice::from_raw_parts(chunkaddr as *const u8, chunklen))],
149-
CmaFlags::empty()))
149+
&[RemoteIoVec { base: chunkaddr, len: chunklen }]))
150150
};
151151
res.set_len(n);
152152
}
@@ -171,8 +171,7 @@ impl Tracee {
171171
res.set_len(oldlen + chunklen);
172172
match { process_vm_readv(self.pid,
173173
&mut [IoVec::from_mut_slice(&mut res[oldlen..])],
174-
&[IoVec::from_slice(std::slice::from_raw_parts(chunkaddr as *const u8, chunklen))],
175-
CmaFlags::empty()) } {
174+
&[RemoteIoVec { base: chunkaddr, len: chunklen }]) } {
176175
Ok(n) => { res.set_len(n); }
177176
Err(Sys(EFAULT)) => { return Ok((res, false)); }
178177
Err(e) => { return Err(e); }

0 commit comments

Comments
 (0)