@@ -6,33 +6,31 @@ extern crate serde_derive;
6
6
#[ macro_use]
7
7
extern crate gtrace_derive;
8
8
9
- // extern crate libc;
9
+ extern crate libc;
10
10
extern crate nix;
11
11
12
- use nix:: libc;
13
-
14
- use libc:: pid_t;
12
+ use nix:: unistd:: Pid ;
15
13
use nix:: sys:: wait:: WaitStatus ;
16
- use nix:: sys:: ptrace:: * ;
17
- use nix:: sys:: ptrace:: ptrace:: * ;
14
+ use nix:: sys:: ptrace;
18
15
use nix:: Result ;
19
16
20
17
pub mod arch;
21
18
pub mod decode;
22
19
pub mod syscall;
23
20
24
21
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) )
28
26
}
29
27
}
30
28
31
29
pub enum TraceEvent {
32
30
SysEnter ,
33
31
SysExit ,
34
32
Signal ( u8 ) ,
35
- Exit ( i8 ) ,
33
+ Exit ( i32 ) ,
36
34
}
37
35
38
36
enum State {
@@ -41,13 +39,14 @@ enum State {
41
39
}
42
40
43
41
pub struct Tracee {
44
- pid : pid_t ,
42
+ pid : Pid ,
45
43
state : State ,
46
44
}
47
45
48
46
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 ( ) ;
51
50
Tracee {
52
51
pid : pid,
53
52
state : State :: Userspace ,
@@ -75,11 +74,12 @@ impl Tracee {
75
74
}
76
75
77
76
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 )
79
78
}
80
79
81
80
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 ) }
83
83
}
84
84
85
85
pub fn get_arg ( & mut self , reg : u8 ) -> Result < u64 > {
@@ -92,11 +92,13 @@ impl Tracee {
92
92
5 => arch:: x86_64:: R9 ,
93
93
_ => panic ! ( "there aren't that many registers" )
94
94
} ;
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 ) }
96
97
}
97
98
98
99
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 _ ) }
100
102
}
101
103
102
104
/// Read len bytes from addr. May return fewer than len bytes if
@@ -109,13 +111,12 @@ impl Tracee {
109
111
unsafe {
110
112
res. set_len ( len) ;
111
113
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 } )
113
115
. collect ( ) ;
114
116
let n = {
115
117
try!( process_vm_readv ( self . pid ,
116
118
& mut [ IoVec :: from_mut_slice ( & mut res) ] ,
117
- & target[ ..] ,
118
- CmaFlags :: empty ( ) ) )
119
+ & target[ ..] ) )
119
120
} ;
120
121
res. set_len ( n) ;
121
122
}
@@ -145,8 +146,7 @@ impl Tracee {
145
146
let n = {
146
147
try!( process_vm_readv ( self . pid ,
147
148
& 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 } ] ) )
150
150
} ;
151
151
res. set_len ( n) ;
152
152
}
@@ -171,8 +171,7 @@ impl Tracee {
171
171
res. set_len ( oldlen + chunklen) ;
172
172
match { process_vm_readv ( self . pid ,
173
173
& 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 } ] ) } {
176
175
Ok ( n) => { res. set_len ( n) ; }
177
176
Err ( Sys ( EFAULT ) ) => { return Ok ( ( res, false ) ) ; }
178
177
Err ( e) => { return Err ( e) ; }
0 commit comments