Repository navigation
Expand file tree
/
Copy pathmain.rs
More file actions
502 lines (461 loc) · 16.6 KB
/
Copy pathmain.rs
File metadata and controls
502 lines (461 loc) · 16.6 KB
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release
use clap::Parser;
use eframe::egui;
use egui::{Color32, Ui, Vec2};
use egui_extras::{Size, TableBuilder};
use logger::MapLogger;
use memmap2::Mmap;
use minidump::{format::MINIDUMP_STREAM_TYPE, system_info::PointerWidth, Minidump, Module};
use minidump_common::utils::basename;
use minidump_processor::ProcessState;
use minidump_unwind::{CallStack, StackFrame};
use processor::{
MaybeMinidump, MaybeProcessed, MinidumpAnalysis, ProcessDump, ProcessingStatus, ProcessorTask,
};
use std::{
cmp::Ordering,
path::PathBuf,
sync::{Arc, Condvar, Mutex},
};
use tracing_subscriber::prelude::*;
use ui_logs::LogUiState;
use ui_processed::ProcessedUiState;
use ui_raw_dump::RawDumpUiState;
pub mod logger;
pub mod processor;
mod ui_logs;
mod ui_processed;
mod ui_raw_dump;
mod ui_settings;
struct MyApp {
logger: MapLogger,
settings: Settings,
tab: Tab,
raw_dump_ui_state: RawDumpUiState,
processed_ui_state: ProcessedUiState,
log_ui_state: LogUiState,
cur_status: ProcessingStatus,
last_status: ProcessingStatus,
minidump: MaybeMinidump,
processed: MaybeProcessed,
pointer_width: PointerWidth,
task_sender: Arc<(Mutex<Option<ProcessorTask>>, Condvar)>,
analysis_state: Arc<MinidumpAnalysis>,
}
struct Settings {
available_paths: Vec<PathBuf>,
picked_path: Option<String>,
symbol_paths: Vec<(String, bool)>,
symbol_urls: Vec<(String, bool)>,
symbol_cache: (String, bool),
http_timeout_secs: String,
raw_dump_brief: bool,
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
enum Tab {
Settings,
Processed,
RawDump,
Logs,
}
#[derive(Parser)]
struct Cli {
#[clap(action, long)]
symbols_url: Vec<String>,
#[clap(action, long)]
symbols_path: Vec<String>,
#[clap(action)]
minidumps: Vec<PathBuf>,
}
const DEFAULT_HTTP_TIMEOUT_SECS: u64 = 1000;
fn main() {
let cli = Cli::parse();
let available_paths = cli.minidumps;
let symbol_paths = if cli.symbols_path.is_empty() {
vec![(String::new(), true)]
} else {
cli.symbols_path.into_iter().map(|p| (p, true)).collect()
};
let symbol_urls = if cli.symbols_url.is_empty() {
vec![
("https://symbols.mozilla.org/".to_string(), true),
(
"https://msdl.microsoft.com/download/symbols/".to_string(),
true,
),
(String::new(), true),
]
} else {
cli.symbols_url.into_iter().map(|p| (p, true)).collect()
};
let logger = MapLogger::new();
tracing_subscriber::registry().with(logger.clone()).init();
let options = eframe::NativeOptions {
drag_and_drop_support: true,
initial_window_size: Some(Vec2::new(1000.0, 800.0)),
..Default::default()
};
let task_sender = Arc::new((Mutex::new(None::<ProcessorTask>), Condvar::new()));
let task_receiver = task_sender.clone();
let analysis_receiver = Arc::new(MinidumpAnalysis::default());
let analysis_sender = analysis_receiver.clone();
let logger_handle = logger.clone();
// Start the processor background thread
let _handle = std::thread::spawn(move || {
processor::run_processor(task_receiver, analysis_sender, logger_handle);
});
// Launch the app
eframe::run_native(
"rust-minidump debugger",
options,
Box::new(|_cc| {
Box::new(MyApp {
logger,
tab: Tab::Settings,
settings: Settings {
available_paths,
picked_path: None,
raw_dump_brief: true,
symbol_urls,
symbol_paths,
symbol_cache: (
std::env::temp_dir()
.join("minidump-cache")
.to_string_lossy()
.into_owned(),
true,
),
http_timeout_secs: DEFAULT_HTTP_TIMEOUT_SECS.to_string(),
},
raw_dump_ui_state: RawDumpUiState { cur_stream: 0 },
processed_ui_state: ProcessedUiState {
cur_thread: 0,
cur_frame: 0,
},
log_ui_state: LogUiState {
cur_thread: None,
cur_frame: None,
},
cur_status: ProcessingStatus::NoDump,
last_status: ProcessingStatus::NoDump,
minidump: None,
processed: None,
pointer_width: PointerWidth::Unknown,
task_sender,
analysis_state: analysis_receiver,
})
}),
);
}
// The main even loop
impl eframe::App for MyApp {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
self.poll_processor_state();
self.update_ui(ctx);
self.last_status = self.cur_status;
}
}
// Core State Updating
impl MyApp {
fn poll_processor_state(&mut self) {
// Fetch updates from processing thread
let new_minidump = self.analysis_state.minidump.lock().unwrap().take();
if let Some(dump) = new_minidump {
if let Ok(dump) = &dump {
self.process_dump(dump.clone());
}
self.minidump = Some(dump);
}
if self.cur_status < ProcessingStatus::Done {
let stats = self.analysis_state.stats.lock().unwrap();
let partial = stats.processor_stats.take_unwalked_result();
if let Some(state) = partial {
self.pointer_width = state.system_info.cpu.pointer_width();
if self.tab == Tab::Settings && self.cur_status <= ProcessingStatus::RawProcessing {
self.tab = Tab::Processed;
}
self.cur_status = ProcessingStatus::Symbolicating;
if let Some(crashed_thread) = state.requesting_thread {
self.processed_ui_state.cur_thread = crashed_thread;
}
self.processed = Some(Ok(Arc::new(state)));
}
if let Some(partial) = self.processed.as_mut().and_then(|p| p.as_mut().ok()) {
let partial = Arc::make_mut(partial);
stats.processor_stats.drain_new_frames(|frame| {
let thread = &mut partial.threads[frame.thread_idx];
match thread.frames.len().cmp(&frame.frame_idx) {
Ordering::Greater => {
// Allows us to overwrite the old context frame
thread.frames[frame.frame_idx] = frame.frame;
}
Ordering::Equal => {
thread.frames.push(frame.frame);
}
Ordering::Less => {
unreachable!("stack frames arrived in wrong order??");
}
}
});
}
}
let new_processed = self.analysis_state.processed.lock().unwrap().take();
if let Some(processed) = new_processed {
if self.tab == Tab::Settings && self.cur_status <= ProcessingStatus::RawProcessing {
self.tab = Tab::Processed;
}
self.cur_status = ProcessingStatus::Done;
if let Ok(state) = &processed {
self.pointer_width = state.system_info.cpu.pointer_width();
if let Some(crashed_thread) = state.requesting_thread {
self.processed_ui_state.cur_thread = crashed_thread;
}
}
self.processed = Some(processed);
}
}
fn set_path(&mut self, idx: usize) {
let path = self.settings.available_paths[idx].clone();
self.cur_status = ProcessingStatus::ReadingDump;
self.settings.picked_path = Some(path.display().to_string());
let (lock, condvar) = &*self.task_sender;
let mut new_task = lock.lock().unwrap();
*new_task = Some(ProcessorTask::ReadDump(path));
self.minidump = None;
self.processed = None;
self.tab = Tab::Settings;
condvar.notify_one();
}
fn process_dump(&mut self, dump: Arc<Minidump<'static, Mmap>>) {
let (lock, condvar) = &*self.task_sender;
let mut new_task = lock.lock().unwrap();
self.cur_status = ProcessingStatus::RawProcessing;
let symbol_paths = self
.settings
.symbol_paths
.iter()
.filter(|(path, enabled)| *enabled && !path.trim().is_empty())
.map(|(path, _enabled)| PathBuf::from(path))
.collect();
let symbol_urls = self
.settings
.symbol_urls
.iter()
.filter(|(url, enabled)| *enabled && !url.trim().is_empty())
.map(|(url, _enabled)| url.to_owned())
.collect();
let (raw_cache, cache_enabled) = &self.settings.symbol_cache;
let clear_cache = !cache_enabled;
let symbol_cache = PathBuf::from(raw_cache);
let http_timeout_secs = self
.settings
.http_timeout_secs
.parse::<u64>()
.unwrap_or(DEFAULT_HTTP_TIMEOUT_SECS);
*new_task = Some(ProcessorTask::ProcessDump(ProcessDump {
dump,
symbol_paths,
symbol_urls,
symbol_cache,
clear_cache,
http_timeout_secs,
}));
condvar.notify_one();
}
fn cancel_processing(&mut self) {
let (lock, condvar) = &*self.task_sender;
let mut new_task = lock.lock().unwrap();
*new_task = Some(ProcessorTask::Cancel);
condvar.notify_one();
}
}
// Main UI: sets up tabs and then shells out to the current view
//
// All the different views have been split off into different files
// because they don't care about eachother and things were getting way
// out of control with all these unrelated UIs together!
impl MyApp {
fn update_ui(&mut self, ctx: &egui::Context) {
egui::TopBottomPanel::top("tab bar")
.resizable(false)
.show(ctx, |ui| {
ui.horizontal(|ui| {
ui.selectable_value(&mut self.tab, Tab::Settings, "settings");
if self.cur_status >= ProcessingStatus::RawProcessing {
ui.selectable_value(&mut self.tab, Tab::RawDump, "raw dump");
}
if self.cur_status >= ProcessingStatus::Symbolicating {
ui.selectable_value(&mut self.tab, Tab::Processed, "processed");
}
if self.cur_status >= ProcessingStatus::RawProcessing {
ui.selectable_value(&mut self.tab, Tab::Logs, "logs");
}
});
});
egui::CentralPanel::default().show(ctx, |ui| match self.tab {
Tab::Settings => self.ui_settings(ui, ctx),
Tab::RawDump => self.ui_raw_dump(ui, ctx),
Tab::Processed => self.ui_processed(ui, ctx),
Tab::Logs => self.ui_logs(ui, ctx),
});
}
fn format_addr(&self, addr: u64) -> String {
match self.pointer_width {
minidump::system_info::PointerWidth::Bits32 => format!("0x{addr:08x}"),
minidump::system_info::PointerWidth::Bits64 => format!("0x{addr:016x}"),
minidump::system_info::PointerWidth::Unknown => format!("0x{addr:08x}"),
}
}
}
fn listing(
ui: &mut Ui,
ctx: &egui::Context,
id: u64,
items: impl IntoIterator<Item = (String, String)>,
) {
ui.push_id(id, |ui| {
let mono_font = egui::style::TextStyle::Monospace.resolve(ui.style());
let body_font = egui::style::TextStyle::Body.resolve(ui.style());
TableBuilder::new(ui)
.striped(true)
.cell_layout(egui::Layout::left_to_right().with_cross_align(egui::Align::Center))
.column(Size::initial(120.0).at_least(40.0))
.column(Size::remainder().at_least(60.0))
.clip(false)
.resizable(true)
.scroll(false)
.body(|mut body| {
let widths = body.widths();
let col1_width = widths[0];
let col2_width = widths[1];
for (lhs, rhs) in items {
let (col1, col2, row_height) = {
let fonts = ctx.fonts();
let col1 = fonts.layout(lhs, body_font.clone(), Color32::BLACK, col1_width);
let col2 = fonts.layout(rhs, mono_font.clone(), Color32::BLACK, col2_width);
let row_height = col1.rect.height().max(col2.rect.height()) + 6.0;
(col1, col2, row_height)
};
body.row(row_height, |mut row| {
row.col(|ui| {
ui.label(col1);
});
row.col(|ui| {
ui.label(col2);
});
});
}
});
});
}
fn threadname(stack: &CallStack) -> String {
if let Some(name) = &stack.thread_name {
format!("{} ({})", name, stack.thread_id)
} else {
format!("({})", stack.thread_id)
}
}
fn sourcename(file: &str) -> &str {
let base = basename(file);
match base.rsplit_once(':') {
Some((lhs, _rhs)) => lhs,
None => base,
}
}
fn stream_vendor(stream_type: u32) -> &'static str {
if stream_type <= MINIDUMP_STREAM_TYPE::LastReservedStream as u32 {
"Official"
} else {
match stream_type & 0xFFFF0000 {
0x4767_0000 => "Google",
0x4d7a_0000 => "Mozilla",
_ => "Unknown",
}
}
}
fn frame_source(f: &mut impl std::fmt::Write, frame: &StackFrame) -> Result<(), std::fmt::Error> {
let addr = frame.instruction;
if let Some(ref module) = frame.module {
if let (Some(source_file), Some(source_line), Some(_source_line_base)) = (
&frame.source_file_name,
&frame.source_line,
&frame.source_line_base,
) {
write!(f, "{}: {}", sourcename(source_file), source_line,)?;
} else if let Some(function_base) = frame.function_base {
write!(
f,
"{} + {:#x}",
basename(&module.code_file()),
addr - function_base
)?;
}
}
Ok(())
}
fn frame_signature_from_indices(
state: &ProcessState,
thread_idx: Option<usize>,
frame_idx: Option<usize>,
) -> String {
use std::fmt::Write;
fn frame_signature_from_indices_inner(
buf: &mut String,
state: &ProcessState,
thread_idx: Option<usize>,
frame_idx: Option<usize>,
) -> Option<()> {
let thread_idx = thread_idx?;
let frame_idx = frame_idx?;
let thread = state.threads.get(thread_idx)?;
let frame = thread.frames.get(frame_idx)?;
frame_signature(buf, frame).ok()?;
Some(())
}
if frame_idx.is_none() {
return "<no frame>".to_owned();
}
let mut buf = String::new();
write!(&mut buf, "{}: ", frame_idx.unwrap()).unwrap();
let _ = frame_signature_from_indices_inner(&mut buf, state, thread_idx, frame_idx);
buf
}
fn frame_signature(
f: &mut impl std::fmt::Write,
frame: &StackFrame,
) -> Result<(), std::fmt::Error> {
let addr = frame.instruction;
if let Some(ref module) = frame.module {
if let (Some(function), Some(_function_base)) = (&frame.function_name, &frame.function_base)
{
write!(f, "{function}")?;
} else {
write!(
f,
"{} + {:#x}",
basename(&module.code_file()),
addr - module.base_address()
)?;
}
} else {
write!(f, "{addr:#x}")?;
// List off overlapping unloaded modules.
// First we need to collect them up by name so that we can print
// all the overlaps from one module together and dedupe them.
for (name, offsets) in &frame.unloaded_modules {
write!(f, " (unloaded {name}@")?;
let mut first = true;
for offset in offsets {
if first {
write!(f, "{offset:#x}")?;
} else {
// `|` is our separator for multiple entries
write!(f, "|{offset:#x}")?;
}
first = false;
}
write!(f, ")")?;
}
}
Ok(())
}