Skip to content

Commit a1fb68b

Browse files
frailltMindaugas Vinkelis
authored and
Mindaugas Vinkelis
committed
make it easy to resolve symbols by frame
1 parent f3af31c commit a1fb68b

File tree

1 file changed

+31
-21
lines changed

1 file changed

+31
-21
lines changed

src/capture.rs

+31-21
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,27 @@ impl Frame {
8686
} => module_base_address.map(|addr| addr as *mut c_void),
8787
}
8888
}
89+
90+
/// Resolve all addresses in the frame to their symbolic names.
91+
fn resolve_symbols(&self) -> Vec<BacktraceSymbol> {
92+
let mut symbols = Vec::new();
93+
let sym = |symbol: &Symbol| {
94+
symbols.push(BacktraceSymbol {
95+
name: symbol.name().map(|m| m.as_bytes().to_vec()),
96+
addr: symbol.addr().map(|a| a as usize),
97+
filename: symbol.filename().map(|m| m.to_owned()),
98+
lineno: symbol.lineno(),
99+
colno: symbol.colno(),
100+
});
101+
};
102+
match *self {
103+
Frame::Raw(ref f) => resolve_frame(f, sym),
104+
Frame::Deserialized { ip, .. } => {
105+
resolve(ip as *mut c_void, sym);
106+
}
107+
}
108+
symbols
109+
}
89110
}
90111

91112
/// Captured version of a symbol in a backtrace.
@@ -216,27 +237,7 @@ impl Backtrace {
216237
/// This function requires the `std` feature of the `backtrace` crate to be
217238
/// enabled, and the `std` feature is enabled by default.
218239
pub fn resolve(&mut self) {
219-
for frame in self.frames.iter_mut().filter(|f| f.symbols.is_none()) {
220-
let mut symbols = Vec::new();
221-
{
222-
let sym = |symbol: &Symbol| {
223-
symbols.push(BacktraceSymbol {
224-
name: symbol.name().map(|m| m.as_bytes().to_vec()),
225-
addr: symbol.addr().map(|a| a as usize),
226-
filename: symbol.filename().map(|m| m.to_owned()),
227-
lineno: symbol.lineno(),
228-
colno: symbol.colno(),
229-
});
230-
};
231-
match frame.frame {
232-
Frame::Raw(ref f) => resolve_frame(f, sym),
233-
Frame::Deserialized { ip, .. } => {
234-
resolve(ip as *mut c_void, sym);
235-
}
236-
}
237-
}
238-
frame.symbols = Some(symbols);
239-
}
240+
self.frames.iter_mut().for_each(BacktraceFrame::resolve);
240241
}
241242
}
242243

@@ -314,6 +315,15 @@ impl BacktraceFrame {
314315
pub fn symbols(&self) -> &[BacktraceSymbol] {
315316
self.symbols.as_ref().map(|s| &s[..]).unwrap_or(&[])
316317
}
318+
319+
/// Resolve all addresses in the frame to their symbolic names.
320+
///
321+
/// If this frame has been previously resolved, this function does nothing.
322+
pub fn resolve(&mut self) {
323+
if self.symbols.is_none() {
324+
self.symbols = Some(self.frame.resolve_symbols());
325+
}
326+
}
317327
}
318328

319329
impl BacktraceSymbol {

0 commit comments

Comments
 (0)