Skip to content

Commit e79df13

Browse files
committed
Factor out common backtrace frame truncating code
1 parent 853c734 commit e79df13

File tree

3 files changed

+13
-23
lines changed

3 files changed

+13
-23
lines changed

src/capture.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,13 @@ cfg_if::cfg_if! {
385385

386386
impl fmt::Debug for Backtrace {
387387
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
388-
fmt_backtrace(self, fmt)
388+
fmt.write_str("stack backtrace:\n")?;
389+
let frames = if fmt.alternate() {
390+
&self.frames[..]
391+
} else {
392+
&self.frames[self.actual_start_index..]
393+
};
394+
fmt_backtrace(frames, fmt)
389395
}
390396
}
391397

src/capture/default_backtrace_fmt.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,9 @@
1-
use crate::Backtrace;
1+
use super::BacktraceFrame;
22
use std::ffi::c_void;
33
use std::fmt;
44

5-
pub(super) fn fmt_backtrace(bt: &Backtrace, fmt: &mut fmt::Formatter) -> fmt::Result {
6-
write!(fmt, "stack backtrace:")?;
7-
8-
let iter = if fmt.alternate() {
9-
bt.frames.iter()
10-
} else {
11-
bt.frames[bt.actual_start_index..].iter()
12-
};
13-
14-
for (idx, frame) in iter.enumerate() {
5+
pub(super) fn fmt_backtrace(frames: &[BacktraceFrame], fmt: &mut fmt::Formatter) -> fmt::Result {
6+
for (idx, frame) in frames.iter().enumerate() {
157
// To reduce TCB size in Sgx enclave, we do not want to implement symbol resolution functionality.
168
// Rather, we can print the offset of the address here, which could be later mapped to
179
// correct function.

src/capture/fuchsia_backtrace_fmt.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,17 @@
1-
use crate::Backtrace;
1+
use super::BacktraceFrame;
22
use libc::c_void;
33
use std::fmt;
44

5-
pub(super) fn fmt_backtrace(bt: &Backtrace, fmt: &mut fmt::Formatter) -> fmt::Result {
6-
fmt.write_str("stack backtrace:\n")?;
7-
5+
pub(super) fn fmt_backtrace(frames: &[BacktraceFrame], fmt: &mut fmt::Formatter) -> fmt::Result {
86
// Print the DSO context to tell the logger where our libs are loaded into memory.
97
let mut dso_cx = vec![];
108
fuchsia_backtrace::print_dso_context(&mut dso_cx).map_err(|_| fmt::Error)?;
119
if let Ok(s) = std::string::String::from_utf8(dso_cx) {
1210
fmt.write_str(&s)?;
1311
}
1412

15-
let iter = if fmt.alternate() {
16-
bt.frames.iter()
17-
} else {
18-
bt.frames[bt.actual_start_index..].iter()
19-
};
20-
2113
// Print the addresses of the backtrace frames
22-
for (idx, frame) in iter.enumerate() {
14+
for (idx, frame) in frames.iter().enumerate() {
2315
let ip: *mut c_void = frame.ip();
2416
fmt.write_str("{{{bt:")?;
2517
write!(fmt, "{}:{:?}", idx, ip)?;

0 commit comments

Comments
 (0)