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
use std::path::Path;
use std::process::Command;
use regex::Regex;
pub mod error;
#[derive(Debug, Clone)]
pub enum ExecType<'a> {
Local(&'a [&'a str]),
Remote(&'a str),
Core { target: &'a str, core: &'a str },
}
#[derive(Debug)]
pub struct GdbCommand<'a> {
exec_type: ExecType<'a>,
args: Vec<&'a str>,
}
impl<'a> GdbCommand<'a> {
pub fn new(exec_type: &'a ExecType) -> GdbCommand<'a> {
GdbCommand {
exec_type: exec_type.clone(),
args: Vec::new(),
}
}
pub fn ex(&mut self, cmd: &'a str) -> &'a mut GdbCommand {
self.args.push("-ex");
self.args.push("p \"gdb-command\"");
self.args.push("-ex");
self.args.push(cmd);
self
}
pub fn raw(&self) -> error::Result<Vec<u8>> {
let mut gdb = Command::new("gdb");
let mut gdb_args = Vec::new();
gdb_args.push("--batch");
gdb_args.push("-ex");
gdb_args.push("set backtrace limit 2000");
gdb_args.push("-ex");
gdb_args.push("set disassembly-flavor intel");
match &self.exec_type {
ExecType::Local(args) => {
if !Path::new(args[0]).exists() {
return Err(error::Error::NoFile(args[0].to_string()));
}
gdb_args.push("-ex");
gdb_args.push("r");
gdb_args.append(&mut self.args.clone());
gdb_args.push("--args");
gdb_args.extend_from_slice(args);
}
ExecType::Remote(pid) => {
gdb_args.push("-p");
gdb_args.push(pid);
gdb_args.append(&mut self.args.clone());
}
ExecType::Core { target, core } => {
if !Path::new(target).exists() {
return Err(error::Error::NoFile(target.to_string()));
}
if !Path::new(core).exists() {
return Err(error::Error::NoFile(core.to_string()));
}
gdb_args.append(&mut self.args.clone());
gdb_args.push(&target);
gdb_args.push(&core);
}
}
let output = gdb.args(&gdb_args).output()?;
if output.status.success() {
Ok(output.stdout.clone())
} else {
Err(error::Error::ExitCode(output.status.code().unwrap()))
}
}
pub fn bt(&mut self) -> &'a mut GdbCommand {
self.ex("bt")
}
pub fn disassembly(&mut self) -> &'a mut GdbCommand {
self.ex("x/16i $pc")
}
pub fn regs(&mut self) -> &'a mut GdbCommand {
self.ex("i r")
}
pub fn mappings(&mut self) -> &'a mut GdbCommand {
self.ex("info proc mappings")
}
pub fn run(&self) -> error::Result<Vec<String>> {
let stdout = self.raw()?;
let output = String::from_utf8(stdout).unwrap();
let re = Regex::new(r#"(?m)^\$\d+\s*=\s*"gdb-command"$"#).unwrap();
let mut result = re
.split(&output)
.map(|s| s.to_string())
.collect::<Vec<String>>();
result.remove(0);
Ok(result)
}
}