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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
use std::collections::{BTreeMap, BTreeSet};
use std::ops::Deref;
use std::path::Path;
use std::time::{Duration, SystemTime};
use minidump::{self, *};
use crate::arg_recovery;
use crate::evil;
use crate::process_state::{CallStack, CallStackInfo, LinuxStandardBase, ProcessState};
use crate::stackwalker;
use crate::symbols::*;
use crate::system_info::SystemInfo;
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct ProcessorOptions<'a> {
pub evil_json: Option<&'a Path>,
pub recover_function_args: bool,
}
impl ProcessorOptions<'_> {
pub fn stable_basic() -> Self {
ProcessorOptions {
evil_json: None,
recover_function_args: false,
}
}
pub fn stable_all() -> Self {
ProcessorOptions {
evil_json: None,
recover_function_args: false,
}
}
pub fn unstable_all() -> Self {
ProcessorOptions {
evil_json: None,
recover_function_args: true,
}
}
fn check_deprecated_and_disabled(&self) {
}
}
impl Default for ProcessorOptions<'_> {
fn default() -> Self {
Self::stable_basic()
}
}
#[derive(Debug, thiserror::Error)]
pub enum ProcessError {
#[error("Failed to read minidump")]
MinidumpReadError(#[from] minidump::Error),
#[error("An unknown error occurred")]
UnknownError,
#[error("The system information stream was not found")]
MissingSystemInfo,
#[error("The thread list stream was not found")]
MissingThreadList,
}
impl ProcessError {
pub fn name(&self) -> &'static str {
match self {
ProcessError::MinidumpReadError(_) => "MinidumpReadError",
ProcessError::UnknownError => "UnknownError",
ProcessError::MissingSystemInfo => "MissingSystemInfo",
ProcessError::MissingThreadList => "MissingThreadList",
}
}
}
pub async fn process_minidump<'a, T, P>(
dump: &Minidump<'a, T>,
symbol_provider: &P,
) -> Result<ProcessState, ProcessError>
where
T: Deref<Target = [u8]> + 'a,
P: SymbolProvider + Sync,
{
process_minidump_with_options(dump, symbol_provider, ProcessorOptions::default()).await
}
pub async fn process_minidump_with_options<'a, T, P>(
dump: &Minidump<'a, T>,
symbol_provider: &P,
options: ProcessorOptions<'_>,
) -> Result<ProcessState, ProcessError>
where
T: Deref<Target = [u8]> + 'a,
P: SymbolProvider + Sync,
{
options.check_deprecated_and_disabled();
let thread_list = dump
.get_stream::<MinidumpThreadList>()
.or(Err(ProcessError::MissingThreadList))?;
let thread_names = dump
.get_stream::<MinidumpThreadNames>()
.unwrap_or_else(|_| MinidumpThreadNames::default());
let dump_system_info = dump
.get_stream::<MinidumpSystemInfo>()
.or(Err(ProcessError::MissingSystemInfo))?;
let (os_version, os_build) = dump_system_info.os_parts();
let linux_standard_base = dump.get_stream::<MinidumpLinuxLsbRelease>().ok();
let linux_cpu_info = dump
.get_stream::<MinidumpLinuxCpuInfo>()
.unwrap_or_default();
let _linux_environ = dump.get_stream::<MinidumpLinuxEnviron>().ok();
let _linux_proc_status = dump.get_stream::<MinidumpLinuxProcStatus>().ok();
let mut cpu_microcode_version = None;
for (key, val) in linux_cpu_info.iter() {
if key.as_bytes() == b"microcode" {
cpu_microcode_version = val
.to_str()
.ok()
.and_then(|val| val.strip_prefix("0x"))
.and_then(|val| u64::from_str_radix(val, 16).ok());
break;
}
}
let linux_standard_base = linux_standard_base.map(|linux_standard_base| {
let mut lsb = LinuxStandardBase::default();
for (key, val) in linux_standard_base.iter() {
match key.as_bytes() {
b"DISTRIB_ID" | b"ID" => lsb.id = val.to_string_lossy().into_owned(),
b"DISTRIB_RELEASE" | b"VERSION_ID" => {
lsb.release = val.to_string_lossy().into_owned()
}
b"DISTRIB_CODENAME" | b"VERSION_CODENAME" => {
lsb.codename = val.to_string_lossy().into_owned()
}
b"DISTRIB_DESCRIPTION" | b"PRETTY_NAME" => {
lsb.description = val.to_string_lossy().into_owned()
}
_ => {}
}
}
lsb
});
let cpu_info = dump_system_info
.cpu_info()
.map(|string| string.into_owned());
let system_info = SystemInfo {
os: dump_system_info.os,
os_version: Some(os_version),
os_build,
cpu: dump_system_info.cpu,
cpu_info,
cpu_microcode_version,
cpu_count: dump_system_info.raw.number_of_processors as usize,
};
let mac_crash_info = dump
.get_stream::<MinidumpMacCrashInfo>()
.ok()
.map(|info| info.raw);
let misc_info = dump.get_stream::<MinidumpMiscInfo>().ok();
let (process_id, process_create_time) = if let Some(misc_info) = misc_info.as_ref() {
(
misc_info.raw.process_id().cloned(),
misc_info.process_create_time(),
)
} else {
(None, None)
};
let breakpad_info = dump.get_stream::<MinidumpBreakpadInfo>();
let (dump_thread_id, requesting_thread_id) = if let Ok(info) = breakpad_info {
(info.dump_thread_id, info.requesting_thread_id)
} else {
(None, None)
};
let exception_stream = dump.get_stream::<MinidumpException>().ok();
let exception_ref = exception_stream.as_ref();
let (crash_reason, crash_address, crashing_thread_id) = if let Some(exception) = exception_ref {
(
Some(exception.get_crash_reason(system_info.os, system_info.cpu)),
Some(exception.get_crash_address(system_info.os, system_info.cpu)),
Some(exception.get_crashing_thread_id()),
)
} else {
(None, None, None)
};
let exception_context =
exception_ref.and_then(|e| e.context(&dump_system_info, misc_info.as_ref()));
let assertion = None;
let modules = match dump.get_stream::<MinidumpModuleList>() {
Ok(module_list) => module_list,
Err(_) => MinidumpModuleList::new(),
};
let unloaded_modules = match dump.get_stream::<MinidumpUnloadedModuleList>() {
Ok(module_list) => module_list,
Err(_) => MinidumpUnloadedModuleList::new(),
};
let memory_list = dump.get_stream::<MinidumpMemoryList>().unwrap_or_default();
let memory_info_list = dump.get_stream::<MinidumpMemoryInfoList>().ok();
let linux_maps = dump.get_stream::<MinidumpLinuxMaps>().ok();
let _memory_info = UnifiedMemoryInfoList::new(memory_info_list, linux_maps).unwrap_or_default();
let evil = options
.evil_json
.and_then(evil::handle_evil)
.unwrap_or_default();
let mut threads = vec![];
let mut requesting_thread = None;
for (i, thread) in thread_list.threads.iter().enumerate() {
let id = thread.raw.thread_id;
if dump_thread_id.is_some() && dump_thread_id.unwrap() == id {
threads.push(CallStack::with_info(id, CallStackInfo::DumpThreadSkipped));
continue;
}
let thread_context = thread.context(&dump_system_info, misc_info.as_ref());
let context = if crashing_thread_id
.or(requesting_thread_id)
.map(|id| id == thread.raw.thread_id)
.unwrap_or(false)
{
requesting_thread = Some(i);
exception_context.as_deref().or(thread_context.as_deref())
} else {
thread_context.as_deref()
};
let stack_memory = thread.stack_memory(&memory_list);
let mut stack = stackwalker::walk_stack(
&context,
stack_memory.as_deref(),
&modules,
&system_info,
symbol_provider,
)
.await;
stack.thread_id = id;
for frame in &mut stack.frames {
if frame.module.is_none() {
let mut offsets = BTreeMap::new();
for unloaded in unloaded_modules.modules_at_address(frame.instruction) {
let offset = frame.instruction - unloaded.raw.base_of_image;
offsets
.entry(unloaded.name.clone())
.or_insert_with(BTreeSet::new)
.insert(offset);
}
frame.unloaded_modules = offsets;
}
}
let name = thread_names
.get_name(thread.raw.thread_id)
.map(|cow| cow.into_owned())
.or_else(|| evil.thread_names.get(&thread.raw.thread_id).cloned());
stack.thread_name = name;
stack.last_error_value = thread.last_error(system_info.cpu, &memory_list);
if options.recover_function_args {
arg_recovery::fill_arguments(&mut stack, stack_memory.as_deref());
}
threads.push(stack);
}
let unknown_streams = dump.unknown_streams().collect();
let unimplemented_streams = dump.unimplemented_streams().collect();
let symbol_stats = symbol_provider.stats();
Ok(ProcessState {
process_id,
time: SystemTime::UNIX_EPOCH + Duration::from_secs(dump.header.time_date_stamp as u64),
process_create_time,
cert_info: evil.certs,
crash_reason,
crash_address,
assertion,
requesting_thread,
system_info,
linux_standard_base,
mac_crash_info,
threads,
modules,
unloaded_modules,
unknown_streams,
unimplemented_streams,
symbol_stats,
})
}