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 /* addr and flags should be cleard for reusing kprobe. */
121 ret
= register_kprobes(kps
, 2);
123 printk(KERN_ERR
"Kprobe smoke test failed: "
124 "register_kprobes returned %d\n", ret
);
133 printk(KERN_ERR
"Kprobe smoke test failed: "
134 "kprobe pre_handler not called\n");
138 if (posth_val
== 0) {
139 printk(KERN_ERR
"Kprobe smoke test failed: "
140 "kprobe post_handler not called\n");
146 ret
= target2(rand1
);
149 printk(KERN_ERR
"Kprobe smoke test failed: "
150 "kprobe pre_handler2 not called\n");
154 if (posth_val
== 0) {
155 printk(KERN_ERR
"Kprobe smoke test failed: "
156 "kprobe post_handler2 not called\n");
160 unregister_kprobes(kps
, 2);
165 static u32
j_kprobe_target(u32 value
)
167 if (value
!= rand1
) {
169 printk(KERN_ERR
"Kprobe smoke test failed: "
170 "incorrect value in jprobe handler\n");
178 static struct jprobe jp
= {
179 .entry
= j_kprobe_target
,
180 .kp
.symbol_name
= "kprobe_target"
183 static int test_jprobe(void)
187 ret
= register_jprobe(&jp
);
189 printk(KERN_ERR
"Kprobe smoke test failed: "
190 "register_jprobe returned %d\n", ret
);
195 unregister_jprobe(&jp
);
197 printk(KERN_ERR
"Kprobe smoke test failed: "
198 "jprobe handler not called\n");
205 static struct jprobe jp2
= {
206 .entry
= j_kprobe_target
,
207 .kp
.symbol_name
= "kprobe_target2"
210 static int test_jprobes(void)
213 struct jprobe
*jps
[2] = {&jp
, &jp2
};
215 /* addr and flags should be cleard for reusing kprobe. */
218 ret
= register_jprobes(jps
, 2);
220 printk(KERN_ERR
"Kprobe smoke test failed: "
221 "register_jprobes returned %d\n", ret
);
228 printk(KERN_ERR
"Kprobe smoke test failed: "
229 "jprobe handler not called\n");
234 ret
= target2(rand1
);
236 printk(KERN_ERR
"Kprobe smoke test failed: "
237 "jprobe handler2 not called\n");
240 unregister_jprobes(jps
, 2);
244 #ifdef CONFIG_KRETPROBES
247 static int entry_handler(struct kretprobe_instance
*ri
, struct pt_regs
*regs
)
249 krph_val
= (rand1
/ div_factor
);
253 static int return_handler(struct kretprobe_instance
*ri
, struct pt_regs
*regs
)
255 unsigned long ret
= regs_return_value(regs
);
257 if (ret
!= (rand1
/ div_factor
)) {
259 printk(KERN_ERR
"Kprobe smoke test failed: "
260 "incorrect value in kretprobe handler\n");
264 printk(KERN_ERR
"Kprobe smoke test failed: "
265 "call to kretprobe entry handler failed\n");
272 static struct kretprobe rp
= {
273 .handler
= return_handler
,
274 .entry_handler
= entry_handler
,
275 .kp
.symbol_name
= "kprobe_target"
278 static int test_kretprobe(void)
282 ret
= register_kretprobe(&rp
);
284 printk(KERN_ERR
"Kprobe smoke test failed: "
285 "register_kretprobe returned %d\n", ret
);
290 unregister_kretprobe(&rp
);
291 if (krph_val
!= rand1
) {
292 printk(KERN_ERR
"Kprobe smoke test failed: "
293 "kretprobe handler not called\n");
300 static int return_handler2(struct kretprobe_instance
*ri
, struct pt_regs
*regs
)
302 unsigned long ret
= regs_return_value(regs
);
304 if (ret
!= (rand1
/ div_factor
) + 1) {
306 printk(KERN_ERR
"Kprobe smoke test failed: "
307 "incorrect value in kretprobe handler2\n");
311 printk(KERN_ERR
"Kprobe smoke test failed: "
312 "call to kretprobe entry handler failed\n");
319 static struct kretprobe rp2
= {
320 .handler
= return_handler2
,
321 .entry_handler
= entry_handler
,
322 .kp
.symbol_name
= "kprobe_target2"
325 static int test_kretprobes(void)
328 struct kretprobe
*rps
[2] = {&rp
, &rp2
};
330 /* addr and flags should be cleard for reusing kprobe. */
333 ret
= register_kretprobes(rps
, 2);
335 printk(KERN_ERR
"Kprobe smoke test failed: "
336 "register_kretprobe returned %d\n", ret
);
342 if (krph_val
!= rand1
) {
343 printk(KERN_ERR
"Kprobe smoke test failed: "
344 "kretprobe handler not called\n");
349 ret
= target2(rand1
);
350 if (krph_val
!= rand1
) {
351 printk(KERN_ERR
"Kprobe smoke test failed: "
352 "kretprobe handler2 not called\n");
355 unregister_kretprobes(rps
, 2);
358 #endif /* CONFIG_KRETPROBES */
360 int init_test_probes(void)
364 target
= kprobe_target
;
365 target2
= kprobe_target2
;
368 rand1
= prandom_u32();
369 } while (rand1
<= div_factor
);
371 printk(KERN_INFO
"Kprobe smoke test started\n");
378 ret
= test_kprobes();
388 ret
= test_jprobes();
392 #ifdef CONFIG_KRETPROBES
394 ret
= test_kretprobe();
399 ret
= test_kretprobes();
402 #endif /* CONFIG_KRETPROBES */
405 printk(KERN_ERR
"BUG: Kprobe smoke test: %d out of "
406 "%d tests failed\n", errors
, num_tests
);
407 else if (handler_errors
)
408 printk(KERN_ERR
"BUG: Kprobe smoke test: %d error(s) "
409 "running handlers\n", handler_errors
);
411 printk(KERN_INFO
"Kprobe smoke test passed successfully\n");
This page took 0.037595 seconds and 5 git commands to generate.