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