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
);
27 static noinline u32
kprobe_target(u32 value
)
29 return (value
/ div_factor
);
32 static int kp_pre_handler(struct kprobe
*p
, struct pt_regs
*regs
)
34 preh_val
= (rand1
/ div_factor
);
38 static void kp_post_handler(struct kprobe
*p
, struct pt_regs
*regs
,
41 if (preh_val
!= (rand1
/ div_factor
)) {
43 printk(KERN_ERR
"Kprobe smoke test failed: "
44 "incorrect value in post_handler\n");
46 posth_val
= preh_val
+ div_factor
;
49 static struct kprobe kp
= {
50 .symbol_name
= "kprobe_target",
51 .pre_handler
= kp_pre_handler
,
52 .post_handler
= kp_post_handler
55 static int test_kprobe(void)
59 ret
= register_kprobe(&kp
);
61 printk(KERN_ERR
"Kprobe smoke test failed: "
62 "register_kprobe returned %d\n", ret
);
67 unregister_kprobe(&kp
);
70 printk(KERN_ERR
"Kprobe smoke test failed: "
71 "kprobe pre_handler not called\n");
76 printk(KERN_ERR
"Kprobe smoke test failed: "
77 "kprobe post_handler not called\n");
84 static u32
j_kprobe_target(u32 value
)
88 printk(KERN_ERR
"Kprobe smoke test failed: "
89 "incorrect value in jprobe handler\n");
97 static struct jprobe jp
= {
98 .entry
= j_kprobe_target
,
99 .kp
.symbol_name
= "kprobe_target"
102 static int test_jprobe(void)
106 ret
= register_jprobe(&jp
);
108 printk(KERN_ERR
"Kprobe smoke test failed: "
109 "register_jprobe returned %d\n", ret
);
114 unregister_jprobe(&jp
);
116 printk(KERN_ERR
"Kprobe smoke test failed: "
117 "jprobe handler not called\n");
124 #ifdef CONFIG_KRETPROBES
127 static int entry_handler(struct kretprobe_instance
*ri
, struct pt_regs
*regs
)
129 krph_val
= (rand1
/ div_factor
);
133 static int return_handler(struct kretprobe_instance
*ri
, struct pt_regs
*regs
)
135 unsigned long ret
= regs_return_value(regs
);
137 if (ret
!= (rand1
/ div_factor
)) {
139 printk(KERN_ERR
"Kprobe smoke test failed: "
140 "incorrect value in kretprobe handler\n");
144 printk(KERN_ERR
"Kprobe smoke test failed: "
145 "call to kretprobe entry handler failed\n");
152 static struct kretprobe rp
= {
153 .handler
= return_handler
,
154 .entry_handler
= entry_handler
,
155 .kp
.symbol_name
= "kprobe_target"
158 static int test_kretprobe(void)
162 ret
= register_kretprobe(&rp
);
164 printk(KERN_ERR
"Kprobe smoke test failed: "
165 "register_kretprobe returned %d\n", ret
);
170 unregister_kretprobe(&rp
);
171 if (krph_val
!= rand1
) {
172 printk(KERN_ERR
"Kprobe smoke test failed: "
173 "kretprobe handler not called\n");
179 #endif /* CONFIG_KRETPROBES */
181 int init_test_probes(void)
185 target
= kprobe_target
;
189 } while (rand1
<= div_factor
);
191 printk(KERN_INFO
"Kprobe smoke test started\n");
202 #ifdef CONFIG_KRETPROBES
204 ret
= test_kretprobe();
207 #endif /* CONFIG_KRETPROBES */
210 printk(KERN_ERR
"BUG: Kprobe smoke test: %d out of "
211 "%d tests failed\n", errors
, num_tests
);
212 else if (handler_errors
)
213 printk(KERN_ERR
"BUG: Kprobe smoke test: %d error(s) "
214 "running handlers\n", handler_errors
);
216 printk(KERN_INFO
"Kprobe smoke test passed successfully\n");