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 #define pr_fmt(fmt) "Kprobe smoke test: " fmt
19 #include <linux/kernel.h>
20 #include <linux/kprobes.h>
21 #include <linux/random.h>
25 static u32 rand1, preh_val, posth_val, jph_val;
26 static int errors, handler_errors, num_tests;
27 static u32 (*target)(u32 value);
28 static u32 (*target2)(u32 value);
30 static noinline u32 kprobe_target(u32 value)
32 return (value / div_factor);
35 static int kp_pre_handler(struct kprobe *p, struct pt_regs *regs)
39 pr_err("pre-handler is preemptible\n");
41 preh_val = (rand1 / div_factor);
45 static void kp_post_handler(struct kprobe *p, struct pt_regs *regs,
50 pr_err("post-handler is preemptible\n");
52 if (preh_val != (rand1 / div_factor)) {
54 pr_err("incorrect value in post_handler\n");
56 posth_val = preh_val + div_factor;
59 static struct kprobe kp = {
60 .symbol_name = "kprobe_target",
61 .pre_handler = kp_pre_handler,
62 .post_handler = kp_post_handler
65 static int test_kprobe(void)
69 ret = register_kprobe(&kp);
71 pr_err("register_kprobe returned %d\n", ret);
76 unregister_kprobe(&kp);
79 pr_err("kprobe pre_handler not called\n");
84 pr_err("kprobe post_handler not called\n");
91 static noinline u32 kprobe_target2(u32 value)
93 return (value / div_factor) + 1;
96 static int kp_pre_handler2(struct kprobe *p, struct pt_regs *regs)
98 preh_val = (rand1 / div_factor) + 1;
102 static void kp_post_handler2(struct kprobe *p, struct pt_regs *regs,
105 if (preh_val != (rand1 / div_factor) + 1) {
107 pr_err("incorrect value in post_handler2\n");
109 posth_val = preh_val + div_factor;
112 static struct kprobe kp2 = {
113 .symbol_name = "kprobe_target2",
114 .pre_handler = kp_pre_handler2,
115 .post_handler = kp_post_handler2
118 static int test_kprobes(void)
121 struct kprobe *kps[2] = {&kp, &kp2};
123 /* addr and flags should be cleard for reusing kprobe. */
126 ret = register_kprobes(kps, 2);
128 pr_err("register_kprobes returned %d\n", ret);
137 pr_err("kprobe pre_handler not called\n");
141 if (posth_val == 0) {
142 pr_err("kprobe post_handler not called\n");
148 ret = target2(rand1);
151 pr_err("kprobe pre_handler2 not called\n");
155 if (posth_val == 0) {
156 pr_err("kprobe post_handler2 not called\n");
160 unregister_kprobes(kps, 2);
165 static u32 j_kprobe_target(u32 value)
169 pr_err("jprobe-handler is preemptible\n");
171 if (value != rand1) {
173 pr_err("incorrect value in jprobe handler\n");
181 static struct jprobe jp = {
182 .entry = j_kprobe_target,
183 .kp.symbol_name = "kprobe_target"
186 static int test_jprobe(void)
190 ret = register_jprobe(&jp);
192 pr_err("register_jprobe returned %d\n", ret);
197 unregister_jprobe(&jp);
199 pr_err("jprobe handler not called\n");
206 static struct jprobe jp2 = {
207 .entry = j_kprobe_target,
208 .kp.symbol_name = "kprobe_target2"
211 static int test_jprobes(void)
214 struct jprobe *jps[2] = {&jp, &jp2};
216 /* addr and flags should be cleard for reusing kprobe. */
219 ret = register_jprobes(jps, 2);
221 pr_err("register_jprobes returned %d\n", ret);
228 pr_err("jprobe handler not called\n");
233 ret = target2(rand1);
235 pr_err("jprobe handler2 not called\n");
238 unregister_jprobes(jps, 2);
242 #ifdef CONFIG_KRETPROBES
245 static int entry_handler(struct kretprobe_instance *ri, struct pt_regs *regs)
249 pr_err("kretprobe entry handler is preemptible\n");
251 krph_val = (rand1 / div_factor);
255 static int return_handler(struct kretprobe_instance *ri, struct pt_regs *regs)
257 unsigned long ret = regs_return_value(regs);
261 pr_err("kretprobe return handler is preemptible\n");
263 if (ret != (rand1 / div_factor)) {
265 pr_err("incorrect value in kretprobe handler\n");
269 pr_err("call to kretprobe entry handler failed\n");
276 static struct kretprobe rp = {
277 .handler = return_handler,
278 .entry_handler = entry_handler,
279 .kp.symbol_name = "kprobe_target"
282 static int test_kretprobe(void)
286 ret = register_kretprobe(&rp);
288 pr_err("register_kretprobe returned %d\n", ret);
293 unregister_kretprobe(&rp);
294 if (krph_val != rand1) {
295 pr_err("kretprobe handler not called\n");
302 static int return_handler2(struct kretprobe_instance *ri, struct pt_regs *regs)
304 unsigned long ret = regs_return_value(regs);
306 if (ret != (rand1 / div_factor) + 1) {
308 pr_err("incorrect value in kretprobe handler2\n");
312 pr_err("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 pr_err("register_kretprobe returned %d\n", ret);
341 if (krph_val != rand1) {
342 pr_err("kretprobe handler not called\n");
347 ret = target2(rand1);
348 if (krph_val != rand1) {
349 pr_err("kretprobe handler2 not called\n");
352 unregister_kretprobes(rps, 2);
355 #endif /* CONFIG_KRETPROBES */
357 int init_test_probes(void)
361 target = kprobe_target;
362 target2 = kprobe_target2;
365 rand1 = prandom_u32();
366 } while (rand1 <= div_factor);
368 pr_info("started\n");
375 ret = test_kprobes();
385 ret = test_jprobes();
389 #ifdef CONFIG_KRETPROBES
391 ret = test_kretprobe();
396 ret = test_kretprobes();
399 #endif /* CONFIG_KRETPROBES */
402 pr_err("BUG: %d out of %d tests failed\n", errors, num_tests);
403 else if (handler_errors)
404 pr_err("BUG: %d error(s) running handlers\n", handler_errors);
406 pr_info("passed successfully\n");