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
;
25 static u32 (*target
)(u32 value
);
26 static u32 (*target2
)(u32 value
);
28 static noinline u32
kprobe_target(u32 value
)
30 return (value
/ div_factor
);
33 static int kp_pre_handler(struct kprobe
*p
, struct pt_regs
*regs
)
35 preh_val
= (rand1
/ div_factor
);
39 static void kp_post_handler(struct kprobe
*p
, struct pt_regs
*regs
,
42 if (preh_val
!= (rand1
/ div_factor
)) {
44 printk(KERN_ERR
"Kprobe smoke test failed: "
45 "incorrect value in post_handler\n");
47 posth_val
= preh_val
+ div_factor
;
50 static struct kprobe kp
= {
51 .symbol_name
= "kprobe_target",
52 .pre_handler
= kp_pre_handler
,
53 .post_handler
= kp_post_handler
56 static int test_kprobe(void)
60 ret
= register_kprobe(&kp
);
62 printk(KERN_ERR
"Kprobe smoke test failed: "
63 "register_kprobe returned %d\n", ret
);
68 unregister_kprobe(&kp
);
71 printk(KERN_ERR
"Kprobe smoke test failed: "
72 "kprobe pre_handler not called\n");
77 printk(KERN_ERR
"Kprobe smoke test failed: "
78 "kprobe post_handler not called\n");
85 static noinline u32
kprobe_target2(u32 value
)
87 return (value
/ div_factor
) + 1;
90 static int kp_pre_handler2(struct kprobe
*p
, struct pt_regs
*regs
)
92 preh_val
= (rand1
/ div_factor
) + 1;
96 static void kp_post_handler2(struct kprobe
*p
, struct pt_regs
*regs
,
99 if (preh_val
!= (rand1
/ div_factor
) + 1) {
101 printk(KERN_ERR
"Kprobe smoke test failed: "
102 "incorrect value in post_handler2\n");
104 posth_val
= preh_val
+ div_factor
;
107 static struct kprobe kp2
= {
108 .symbol_name
= "kprobe_target2",
109 .pre_handler
= kp_pre_handler2
,
110 .post_handler
= kp_post_handler2
113 static int test_kprobes(void)
116 struct kprobe
*kps
[2] = {&kp
, &kp2
};
118 kp
.addr
= 0; /* addr should be cleard for reusing kprobe. */
119 ret
= register_kprobes(kps
, 2);
121 printk(KERN_ERR
"Kprobe smoke test failed: "
122 "register_kprobes returned %d\n", ret
);
131 printk(KERN_ERR
"Kprobe smoke test failed: "
132 "kprobe pre_handler not called\n");
136 if (posth_val
== 0) {
137 printk(KERN_ERR
"Kprobe smoke test failed: "
138 "kprobe post_handler not called\n");
144 ret
= target2(rand1
);
147 printk(KERN_ERR
"Kprobe smoke test failed: "
148 "kprobe pre_handler2 not called\n");
152 if (posth_val
== 0) {
153 printk(KERN_ERR
"Kprobe smoke test failed: "
154 "kprobe post_handler2 not called\n");
158 unregister_kprobes(kps
, 2);
163 static u32
j_kprobe_target(u32 value
)
165 if (value
!= rand1
) {
167 printk(KERN_ERR
"Kprobe smoke test failed: "
168 "incorrect value in jprobe handler\n");
176 static struct jprobe jp
= {
177 .entry
= j_kprobe_target
,
178 .kp
.symbol_name
= "kprobe_target"
181 static int test_jprobe(void)
185 ret
= register_jprobe(&jp
);
187 printk(KERN_ERR
"Kprobe smoke test failed: "
188 "register_jprobe returned %d\n", ret
);
193 unregister_jprobe(&jp
);
195 printk(KERN_ERR
"Kprobe smoke test failed: "
196 "jprobe handler not called\n");
203 static struct jprobe jp2
= {
204 .entry
= j_kprobe_target
,
205 .kp
.symbol_name
= "kprobe_target2"
208 static int test_jprobes(void)
211 struct jprobe
*jps
[2] = {&jp
, &jp2
};
213 jp
.kp
.addr
= 0; /* addr should be cleard for reusing kprobe. */
214 ret
= register_jprobes(jps
, 2);
216 printk(KERN_ERR
"Kprobe smoke test failed: "
217 "register_jprobes returned %d\n", ret
);
224 printk(KERN_ERR
"Kprobe smoke test failed: "
225 "jprobe handler not called\n");
230 ret
= target2(rand1
);
232 printk(KERN_ERR
"Kprobe smoke test failed: "
233 "jprobe handler2 not called\n");
236 unregister_jprobes(jps
, 2);
240 #ifdef CONFIG_KRETPROBES
243 static int entry_handler(struct kretprobe_instance
*ri
, struct pt_regs
*regs
)
245 krph_val
= (rand1
/ div_factor
);
249 static int return_handler(struct kretprobe_instance
*ri
, struct pt_regs
*regs
)
251 unsigned long ret
= regs_return_value(regs
);
253 if (ret
!= (rand1
/ div_factor
)) {
255 printk(KERN_ERR
"Kprobe smoke test failed: "
256 "incorrect value in kretprobe handler\n");
260 printk(KERN_ERR
"Kprobe smoke test failed: "
261 "call to kretprobe entry handler failed\n");
268 static struct kretprobe rp
= {
269 .handler
= return_handler
,
270 .entry_handler
= entry_handler
,
271 .kp
.symbol_name
= "kprobe_target"
274 static int test_kretprobe(void)
278 ret
= register_kretprobe(&rp
);
280 printk(KERN_ERR
"Kprobe smoke test failed: "
281 "register_kretprobe returned %d\n", ret
);
286 unregister_kretprobe(&rp
);
287 if (krph_val
!= rand1
) {
288 printk(KERN_ERR
"Kprobe smoke test failed: "
289 "kretprobe handler not called\n");
296 static int return_handler2(struct kretprobe_instance
*ri
, struct pt_regs
*regs
)
298 unsigned long ret
= regs_return_value(regs
);
300 if (ret
!= (rand1
/ div_factor
) + 1) {
302 printk(KERN_ERR
"Kprobe smoke test failed: "
303 "incorrect value in kretprobe handler2\n");
307 printk(KERN_ERR
"Kprobe smoke test failed: "
308 "call to kretprobe entry handler failed\n");
315 static struct kretprobe rp2
= {
316 .handler
= return_handler2
,
317 .entry_handler
= entry_handler
,
318 .kp
.symbol_name
= "kprobe_target2"
321 static int test_kretprobes(void)
324 struct kretprobe
*rps
[2] = {&rp
, &rp2
};
326 rp
.kp
.addr
= 0; /* addr should be cleard for reusing kprobe. */
327 ret
= register_kretprobes(rps
, 2);
329 printk(KERN_ERR
"Kprobe smoke test failed: "
330 "register_kretprobe returned %d\n", ret
);
336 if (krph_val
!= rand1
) {
337 printk(KERN_ERR
"Kprobe smoke test failed: "
338 "kretprobe handler not called\n");
343 ret
= target2(rand1
);
344 if (krph_val
!= rand1
) {
345 printk(KERN_ERR
"Kprobe smoke test failed: "
346 "kretprobe handler2 not called\n");
349 unregister_kretprobes(rps
, 2);
352 #endif /* CONFIG_KRETPROBES */
354 int init_test_probes(void)
358 target
= kprobe_target
;
359 target2
= kprobe_target2
;
363 } while (rand1
<= div_factor
);
365 printk(KERN_INFO
"Kprobe smoke test started\n");
372 ret
= test_kprobes();
382 ret
= test_jprobes();
386 #ifdef CONFIG_KRETPROBES
388 ret
= test_kretprobe();
393 ret
= test_kretprobes();
396 #endif /* CONFIG_KRETPROBES */
399 printk(KERN_ERR
"BUG: Kprobe smoke test: %d out of "
400 "%d tests failed\n", errors
, num_tests
);
401 else if (handler_errors
)
402 printk(KERN_ERR
"BUG: Kprobe smoke test: %d error(s) "
403 "running handlers\n", handler_errors
);
405 printk(KERN_INFO
"Kprobe smoke test passed successfully\n");
This page took 0.038459 seconds and 5 git commands to generate.