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
use std::fmt;
use crate::error;
#[derive(Clone, Default, Debug)]
pub struct File {
pub start: u64,
pub end: u64,
pub offset: u64,
pub name: String,
}
impl File {
pub fn new(start: u64, end: u64, offset: u64, fname: &str) -> Self {
File {
start,
end,
offset,
name: String::from(fname),
}
}
}
impl fmt::Display for File {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"File {{ Start: 0x{:x}, End: 0x{:x}, offset: 0x{:x}, path: {} }}",
self.start, self.end, self.offset, self.name
)
}
}
pub type MappedFiles = Vec<File>;
pub trait MappedFilesExt {
fn from_gdb<T: AsRef<str>>(mapping: T) -> error::Result<MappedFiles>;
fn find(&self, addr: u64) -> Option<File>;
}
impl MappedFilesExt for MappedFiles {
fn from_gdb<T: AsRef<str>>(mapping: T) -> error::Result<MappedFiles> {
let mut hlp = mapping
.as_ref()
.lines()
.map(|s| s.trim().to_string())
.collect::<Vec<String>>();
if let Some(pos) = hlp.iter().position(|x| x.contains("Start Addr")) {
hlp.drain(0..pos + 1);
}
let mut info = Vec::new();
let mut name_idx = 0;
for x in hlp.iter() {
let filevec = x
.split_whitespace()
.map(|s| s.trim().to_string())
.filter(|s| !s.is_empty())
.collect::<Vec<String>>();
if filevec.len() < 4 {
return Err(error::Error::MappedFilesParse(format!(
"Expected at least 4 columns in {}",
x.to_string()
)));
}
name_idx = name_idx.max(filevec.len() - 1);
info.push(filevec);
}
let mut files = MappedFiles::new();
for x in info.iter() {
let f = File {
start: u64::from_str_radix(x[0].get(2..).unwrap_or(&x[0]), 16)?,
end: u64::from_str_radix(x[1].get(2..).unwrap_or(&x[1]), 16)?,
offset: u64::from_str_radix(x[3].get(2..).unwrap_or(&x[3]), 16)?,
name: x.get(name_idx).unwrap_or(&String::new()).clone(),
};
files.push(f);
}
Ok(files)
}
fn find(&self, addr: u64) -> Option<File> {
self.iter()
.find(|&x| (x.start <= addr as u64) && (x.end > addr as u64))
.cloned()
}
}