2023-02-22 11:02:43 +08:00
|
|
|
use libc::{c_int, c_void};
|
|
|
|
|
2020-06-30 17:19:09 +08:00
|
|
|
use crate::libunwind as uw;
|
|
|
|
|
|
|
|
const UW_REG_SP: c_int = 13;
|
|
|
|
|
|
|
|
pub fn backtrace<F>(f: F) -> Result<(), uw::_Unwind_Reason_Code>
|
2023-02-22 11:02:43 +08:00
|
|
|
where F: FnMut(usize) -> () {
|
2020-06-30 17:19:09 +08:00
|
|
|
struct TraceContext<F> {
|
|
|
|
step_fn: F,
|
2023-02-22 11:02:43 +08:00
|
|
|
prev_sp: uw::_Unwind_Word,
|
2020-06-30 17:19:09 +08:00
|
|
|
}
|
|
|
|
|
2023-02-22 11:02:43 +08:00
|
|
|
extern "C" fn trace<F>(context: *mut uw::_Unwind_Context, arg: *mut c_void) -> uw::_Unwind_Reason_Code
|
|
|
|
where F: FnMut(usize) -> () {
|
2020-06-30 17:19:09 +08:00
|
|
|
unsafe {
|
|
|
|
let trace_context = &mut *(arg as *mut TraceContext<F>);
|
|
|
|
|
|
|
|
// Detect the root of a libfringe thread
|
|
|
|
let cur_sp = uw::_Unwind_GetGR(context, UW_REG_SP);
|
|
|
|
if cur_sp == trace_context.prev_sp {
|
2023-02-22 11:02:43 +08:00
|
|
|
return uw::_URC_END_OF_STACK;
|
2020-06-30 17:19:09 +08:00
|
|
|
} else {
|
|
|
|
trace_context.prev_sp = cur_sp;
|
|
|
|
}
|
|
|
|
|
|
|
|
(trace_context.step_fn)(uw::_Unwind_GetIP(context));
|
|
|
|
uw::_URC_NO_REASON
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
let mut trace_context = TraceContext { step_fn: f, prev_sp: 0 };
|
|
|
|
match uw::_Unwind_Backtrace(trace::<F>, &mut trace_context as *mut _ as *mut c_void) {
|
|
|
|
uw::_URC_NO_REASON => Ok(()),
|
2023-02-22 11:02:43 +08:00
|
|
|
err => Err(err),
|
2020-06-30 17:19:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|