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