/* * NOTE: This example is designed to check that the kprobe reentry work. * * For more information on theory of operation of kprobes, see * Documentation/kprobes.txt * */ #include #include #include /* For each probe you need to allocate a kprobe structure */ static struct kprobe kp = { .symbol_name = "do_fork", }; static struct kprobe kp_re = { .symbol_name = "int_sqrt", }; static unsigned long y=0; static unsigned long z=0; /* kprobe pre_handler: called just before the probed instruction is executed */ static int handler_pre(struct kprobe *p, struct pt_regs *regs) { /* call another function that is instrumented with a kprobe to ensure that reentry works */ unsigned long x=1764; y = int_sqrt(x); return 0; } /* kprobe post_handler: called after the probed instruction is executed */ static void handler_post(struct kprobe *p, struct pt_regs *regs, unsigned long flags) { return; } /* kprobe pre_handler: called just before the probed instruction is executed */ static int handler_pre_re(struct kprobe *p, struct pt_regs *regs) { /* if reentry is working as expected this code may not be executed */ z = 0xdeadbeef; return 0; } /* kprobe post_handler: called after the probed instruction is executed */ static void handler_post_re(struct kprobe *p, struct pt_regs *regs, unsigned long flags) { return; } /* * fault_handler: this is called if an exception is generated for any * instruction within the pre- or post-handler, or when Kprobes * single-steps the probed instruction. */ static int handler_fault(struct kprobe *p, struct pt_regs *regs, int trapnr) { printk(KERN_INFO "fault_handler: p->addr = 0x%p, trap #%dn", p->addr, trapnr); /* Return 0 because we don't handle the fault. */ return 0; } static int __init kprobe_init(void) { int ret; kp.pre_handler = handler_pre; kp.post_handler = handler_post; kp.fault_handler = handler_fault; ret = register_kprobe(&kp); if (ret < 0) { printk(KERN_INFO "register_kprobe failed, returned %d\n", ret); return ret; } printk(KERN_INFO "Planted kprobe at %p\n", kp.addr); kp_re.pre_handler = handler_pre_re; kp_re.post_handler = handler_post_re; kp_re.fault_handler = handler_fault; ret = register_kprobe(&kp_re); if (ret < 0) { printk(KERN_INFO "register_kprobe failed, returned %d\n", ret); return ret; } printk(KERN_INFO "Planted kprobe at %p\n", kp_re.addr); return 0; } static void __exit kprobe_exit(void) { unregister_kprobe(&kp); printk(KERN_INFO "kprobe at %p unregistered\n", kp.addr); unregister_kprobe(&kp_re); printk(KERN_INFO "kprobe at %p unregistered\n", kp_re.addr); printk(KERN_INFO "y = %ld\n", y); printk(KERN_INFO "z = %lx\n", z); } module_init(kprobe_init) module_exit(kprobe_exit) MODULE_LICENSE("GPL");