hook.rs 764 B

123456789101112131415161718192021222324252627
  1. use unwinding::abi::{UnwindContext, UnwindReasonCode, _Unwind_GetIP};
  2. use unwinding::panic::UserUnwindTrace;
  3. extern "C" {
  4. fn lookup_kallsyms(addr: u64, level: i32) -> i32;
  5. }
  6. /// User hook for unwinding
  7. ///
  8. /// During stack backtrace, the user can print the function location of the current stack frame.
  9. pub struct Tracer;
  10. pub struct CallbackData {
  11. pub counter: usize,
  12. }
  13. impl UserUnwindTrace for Tracer {
  14. type Arg = CallbackData;
  15. fn trace(ctx: &UnwindContext<'_>, arg: *mut Self::Arg) -> UnwindReasonCode {
  16. let data = unsafe { &mut *(arg) };
  17. data.counter += 1;
  18. let pc = _Unwind_GetIP(ctx);
  19. unsafe {
  20. lookup_kallsyms(pc as u64, data.counter as i32);
  21. }
  22. UnwindReasonCode::NO_REASON
  23. }
  24. }