2 * test_kprobes.c - simple sanity test for *probes
4 * Copyright IBM Corp. 2008
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it would be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
14 * the GNU General Public License for more details.
17 #include <linux/kernel.h>
18 #include <linux/kprobes.h>
19 #include <linux/random.h>
23 static u32 rand1
, preh_val
, posth_val
, jph_val
;
24 static int errors
, handler_errors
, num_tests
;
26 static noinline u32
kprobe_target(u32 value
)
29 * gcc ignores noinline on some architectures unless we stuff
30 * sufficient lard into the function. The get_kprobe() here is
33 * NOTE: We aren't concerned about the correctness of get_kprobe()
34 * here; hence, this call is neither under !preempt nor with the
35 * kprobe_mutex held. This is fine(tm)
37 if (get_kprobe((void *)0xdeadbeef))
38 printk(KERN_INFO
"Kprobe smoke test: probe on 0xdeadbeef!\n");
40 return (value
/ div_factor
);
43 static int kp_pre_handler(struct kprobe
*p
, struct pt_regs
*regs
)
45 preh_val
= (rand1
/ div_factor
);
49 static void kp_post_handler(struct kprobe
*p
, struct pt_regs
*regs
,
52 if (preh_val
!= (rand1
/ div_factor
)) {
54 printk(KERN_ERR
"Kprobe smoke test failed: "
55 "incorrect value in post_handler\n");
57 posth_val
= preh_val
+ div_factor
;
60 static struct kprobe kp
= {
61 .symbol_name
= "kprobe_target",
62 .pre_handler
= kp_pre_handler
,
63 .post_handler
= kp_post_handler
66 static int test_kprobe(void)
70 ret
= register_kprobe(&kp
);
72 printk(KERN_ERR
"Kprobe smoke test failed: "
73 "register_kprobe returned %d\n", ret
);
77 ret
= kprobe_target(rand1
);
78 unregister_kprobe(&kp
);
81 printk(KERN_ERR
"Kprobe smoke test failed: "
82 "kprobe pre_handler not called\n");
87 printk(KERN_ERR
"Kprobe smoke test failed: "
88 "kprobe post_handler not called\n");
95 static u32
j_kprobe_target(u32 value
)
99 printk(KERN_ERR
"Kprobe smoke test failed: "
100 "incorrect value in jprobe handler\n");
108 static struct jprobe jp
= {
109 .entry
= j_kprobe_target
,
110 .kp
.symbol_name
= "kprobe_target"
113 static int test_jprobe(void)
117 ret
= register_jprobe(&jp
);
119 printk(KERN_ERR
"Kprobe smoke test failed: "
120 "register_jprobe returned %d\n", ret
);
124 ret
= kprobe_target(rand1
);
125 unregister_jprobe(&jp
);
127 printk(KERN_ERR
"Kprobe smoke test failed: "
128 "jprobe handler not called\n");
135 #ifdef CONFIG_KRETPROBES
138 static int return_handler(struct kretprobe_instance
*ri
, struct pt_regs
*regs
)
140 unsigned long ret
= regs_return_value(regs
);
142 if (ret
!= (rand1
/ div_factor
)) {
144 printk(KERN_ERR
"Kprobe smoke test failed: "
145 "incorrect value in kretprobe handler\n");
148 krph_val
= (rand1
/ div_factor
);
152 static struct kretprobe rp
= {
153 .handler
= return_handler
,
154 .kp
.symbol_name
= "kprobe_target"
157 static int test_kretprobe(void)
161 ret
= register_kretprobe(&rp
);
163 printk(KERN_ERR
"Kprobe smoke test failed: "
164 "register_kretprobe returned %d\n", ret
);
168 ret
= kprobe_target(rand1
);
169 unregister_kretprobe(&rp
);
171 printk(KERN_ERR
"Kprobe smoke test failed: "
172 "kretprobe handler not called\n");
178 #endif /* CONFIG_KRETPROBES */
180 int init_test_probes(void)
186 } while (rand1
<= div_factor
);
188 printk(KERN_INFO
"Kprobe smoke test started\n");
199 #ifdef CONFIG_KRETPROBES
201 ret
= test_kretprobe();
204 #endif /* CONFIG_KRETPROBES */
207 printk(KERN_ERR
"BUG: Kprobe smoke test: %d out of "
208 "%d tests failed\n", errors
, num_tests
);
209 else if (handler_errors
)
210 printk(KERN_ERR
"BUG: Kprobe smoke test: %d error(s) "
211 "running handlers\n", handler_errors
);
213 printk(KERN_INFO
"Kprobe smoke test passed successfully\n");