xref: /DragonOS/kernel/src/debug/kprobe/test.rs (revision 55e6f0b65f91b32638fd56581f711a816eccdcd1)
1 use crate::arch::interrupt::TrapFrame;
2 use crate::debug::kprobe::{register_kprobe, unregister_kprobe, KprobeInfo};
3 use alloc::string::ToString;
4 use kprobe::ProbeArgs;
5 use log::info;
6 
7 #[inline(never)]
8 fn detect_func(x: usize, y: usize) -> usize {
9     let hart = 0;
10     info!("detect_func: hart_id: {}, x: {}, y:{}", hart, x, y);
11     hart
12 }
13 
14 fn pre_handler(regs: &dyn ProbeArgs) {
15     let pt_regs = regs.as_any().downcast_ref::<TrapFrame>().unwrap();
16     info!(
17         "call pre_handler, the sp is {:#x}",
18         pt_regs as *const _ as usize
19     );
20 }
21 
22 fn post_handler(regs: &dyn ProbeArgs) {
23     let pt_regs = regs.as_any().downcast_ref::<TrapFrame>().unwrap();
24     info!(
25         "call post_handler, the sp is {:#x}",
26         pt_regs as *const _ as usize
27     );
28 }
29 
30 fn fault_handler(regs: &dyn ProbeArgs) {
31     let pt_regs = regs.as_any().downcast_ref::<TrapFrame>().unwrap();
32     info!(
33         "call fault_handler, the sp is {:#x}",
34         pt_regs as *const _ as usize
35     );
36 }
37 
38 pub fn kprobe_test() {
39     info!("kprobe test for [detect_func]: {:#x}", detect_func as usize);
40     let kprobe_info = KprobeInfo {
41         pre_handler,
42         post_handler,
43         fault_handler: Some(fault_handler),
44         event_callback: None,
45         symbol: None,
46         addr: Some(detect_func as usize),
47         offset: 0,
48         enable: true,
49     };
50     let kprobe = register_kprobe(kprobe_info).unwrap();
51 
52     let new_pre_handler = |regs: &dyn ProbeArgs| {
53         let pt_regs = regs.as_any().downcast_ref::<TrapFrame>().unwrap();
54         info!(
55             "call new pre_handler, the sp is {:#x}",
56             pt_regs as *const _ as usize
57         );
58     };
59 
60     let kprobe_info = KprobeInfo {
61         pre_handler: new_pre_handler,
62         post_handler,
63         fault_handler: Some(fault_handler),
64         event_callback: None,
65         symbol: Some("dragonos_kernel::debug::kprobe::test::detect_func".to_string()),
66         addr: None,
67         offset: 0,
68         enable: true,
69     };
70     let kprobe2 = register_kprobe(kprobe_info).unwrap();
71     info!(
72         "install 2 kprobes at [detect_func]: {:#x}",
73         detect_func as usize
74     );
75     detect_func(1, 2);
76     unregister_kprobe(kprobe);
77     unregister_kprobe(kprobe2);
78     info!(
79         "uninstall 2 kprobes at [detect_func]: {:#x}",
80         detect_func as usize
81     );
82     detect_func(1, 2);
83     info!("kprobe test end");
84 }
85