backtrace.rs 711 B

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