1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
extern crate proc_maps;
extern crate goblin;
extern crate benfred_read_process_memory as read_process_memory;
extern crate memmap;
extern crate libc;
#[macro_use]
extern crate log;
#[cfg(target_os="linux")]
#[macro_use]
extern crate lazy_static;
#[cfg(target_os="linux")]
extern crate regex;
#[cfg(target_os="linux")]
extern crate nix;
#[cfg(target_os="linux")]
extern crate object;
#[cfg(target_os="linux")]
extern crate addr2line;
#[cfg(target_os="macos")]
extern crate mach_o_sys;
#[cfg(target_os="macos")]
extern crate mach;
#[cfg(target_os="macos")]
extern crate libproc;
#[cfg(windows)]
extern crate winapi;
#[cfg(target_os="macos")]
mod osx;
#[cfg(target_os="macos")]
pub use osx::*;
#[cfg(target_os="linux")]
mod linux;
#[cfg(target_os="linux")]
pub use linux::*;
#[cfg(target_os="freebsd")]
mod freebsd;
#[cfg(target_os="freebsd")]
pub use freebsd::*;
#[cfg(target_os="windows")]
mod windows;
#[cfg(target_os="windows")]
pub use windows::*;
#[derive(Debug)]
pub enum Error {
NoBinaryForAddress(u64),
GoblinError(::goblin::error::Error),
IOError(std::io::Error),
Other(String),
#[cfg(use_libunwind)]
LibunwindError(linux::libunwind::Error),
#[cfg(target_os="linux")]
NixError(nix::Error),
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match *self {
Error::NoBinaryForAddress(addr) => {
write!(f, "No binary found for address 0x{:016x}. Try reloading.", addr)
},
Error::GoblinError(ref e) => e.fmt(f),
Error::IOError(ref e) => e.fmt(f),
Error::Other(ref e) => write!(f, "{}", e),
#[cfg(use_libunwind)]
Error::LibunwindError(ref e) => e.fmt(f),
#[cfg(target_os="linux")]
Error::NixError(ref e) => e.fmt(f),
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match *self {
Error::GoblinError(ref e) => Some(e),
Error::IOError(ref e) => Some(e),
#[cfg(use_libunwind)]
Error::LibunwindError(ref e) => Some(e),
#[cfg(target_os="linux")]
Error::NixError(ref e) => Some(e),
_ => None,
}
}
}
impl From<goblin::error::Error> for Error {
fn from(err: goblin::error::Error) -> Error {
Error::GoblinError(err)
}
}
impl From<std::io::Error> for Error {
fn from(err: std::io::Error) -> Error {
Error::IOError(err)
}
}
#[cfg(target_os="linux")]
impl From<nix::Error> for Error {
fn from(err: nix::Error) -> Error {
Error::NixError(err)
}
}
#[cfg(use_libunwind)]
impl From<linux::libunwind::Error> for Error {
fn from(err: linux::libunwind::Error) -> Error {
Error::LibunwindError(err)
}
}
#[derive(Debug, Clone)]
pub struct StackFrame {
pub line: Option<u64>,
pub filename: Option<String>,
pub function: Option<String>,
pub module: String,
pub addr: u64
}
impl std::fmt::Display for StackFrame {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let function = self.function.as_ref().map(String::as_str).unwrap_or("?");
if let Some(filename) = self.filename.as_ref() {
write!(f, "0x{:016x} {} ({}:{})", self.addr, function, filename, self.line.unwrap_or(0))
} else {
write!(f, "0x{:016x} {} ({})", self.addr, function, self.module)
}
}
}
pub trait ProcessMemory {
fn read(&self, addr: usize, buf: &mut [u8]) -> Result<(), Error>;
fn copy(&self, addr: usize, length: usize) -> Result<Vec<u8>, Error> {
let mut data = vec![0; length];
self.read(addr, &mut data)?;
Ok(data)
}
fn copy_struct<T>(&self, addr: usize) -> Result<T, Error> {
let mut data = vec![0; std::mem::size_of::<T>()];
self.read(addr, &mut data)?;
Ok(unsafe { std::ptr::read(data.as_ptr() as *const _) })
}
fn copy_pointer<T>(&self, ptr: *const T) -> Result<T, Error> {
self.copy_struct(ptr as usize)
}
fn copy_vec<T>(&self, addr: usize, length: usize)
-> Result<Vec<T>, Error>
{
let mut vec = self.copy(addr, length * std::mem::size_of::<T>())?;
let capacity = vec.capacity() as usize / std::mem::size_of::<T>() as usize;
let ptr = vec.as_mut_ptr() as *mut T;
std::mem::forget(vec);
unsafe { Ok(Vec::from_raw_parts(ptr, capacity, capacity)) }
}
}
#[doc(hidden)]
pub struct LocalProcess;
impl ProcessMemory for LocalProcess {
fn read(&self, addr: usize, buf: &mut [u8]) -> Result<(), Error> {
unsafe {
std::ptr::copy_nonoverlapping(addr as *mut u8, buf.as_mut_ptr(), buf.len());
}
Ok(())
}
}
#[cfg(any(target_os="linux", target_os="windows", target_os="freebsd"))]
#[doc(hidden)]
fn filter_child_pids(target_pid: Pid, processes: &std::collections::HashMap<Pid, Pid>) -> Vec<(Pid, Pid)> {
let mut ret = Vec::new();
for (child, parent) in processes.iter() {
let mut current = *parent;
loop {
if current == target_pid {
ret.push((*child, *parent));
break;
}
current = match processes.get(¤t) {
Some(pid) => {
if current == *pid {
break;
}
*pid
}
None => break
};
}
}
ret
}
#[cfg(test)]
pub mod tests {
use super::*;
struct Point { x: i32, y: i64 }
#[test]
fn test_copy_pointer() {
let original = Point{x:15, y:25};
let copy = LocalProcess.copy_pointer(&original).unwrap();
assert_eq!(original.x, copy.x);
assert_eq!(original.y, copy.y);
}
#[test]
fn test_copy_struct() {
let original = Point{x:10, y:20};
let copy: Point = LocalProcess.copy_struct(&original as *const Point as usize).unwrap();
assert_eq!(original.x, copy.x);
assert_eq!(original.y, copy.y);
}
}