backtrace/
backtrace.rs

1use mini_backtrace::Backtrace;
2
3fn main() {
4    let bt = Backtrace::<16>::capture();
5    println!("Backtrace:");
6    for frame in bt.frames {
7        println!("  {:#x}", adjust_for_pic(frame));
8    }
9    if bt.frames_omitted {
10        println!(" ... <frames omitted>");
11    }
12}
13
14// For position-independent code, convert the addresses to be relative to the
15// executable base address.
16//
17// This should *only* be done for position-independent binaries, not statically
18// linked ones.
19fn adjust_for_pic(ip: usize) -> usize {
20    extern "C" {
21        // Symbol defined by the linker
22        static __executable_start: [u8; 0];
23    }
24    let base = unsafe { __executable_start.as_ptr() as usize };
25    ip - base
26}