1804defeaSAnanth N Mavinakayanahalli /* 2804defeaSAnanth N Mavinakayanahalli * NOTE: This example is works on x86 and powerpc. 3804defeaSAnanth N Mavinakayanahalli * Here's a sample kernel module showing the use of kprobes to dump a 4804defeaSAnanth N Mavinakayanahalli * stack trace and selected registers when do_fork() is called. 5804defeaSAnanth N Mavinakayanahalli * 6804defeaSAnanth N Mavinakayanahalli * For more information on theory of operation of kprobes, see 7804defeaSAnanth N Mavinakayanahalli * Documentation/kprobes.txt 8804defeaSAnanth N Mavinakayanahalli * 9804defeaSAnanth N Mavinakayanahalli * You will see the trace data in /var/log/messages and on the console 10804defeaSAnanth N Mavinakayanahalli * whenever do_fork() is invoked to create a new process. 11804defeaSAnanth N Mavinakayanahalli */ 12804defeaSAnanth N Mavinakayanahalli 13804defeaSAnanth N Mavinakayanahalli #include <linux/kernel.h> 14804defeaSAnanth N Mavinakayanahalli #include <linux/module.h> 15804defeaSAnanth N Mavinakayanahalli #include <linux/kprobes.h> 16804defeaSAnanth N Mavinakayanahalli 17804defeaSAnanth N Mavinakayanahalli /* For each probe you need to allocate a kprobe structure */ 18804defeaSAnanth N Mavinakayanahalli static struct kprobe kp = { 19804defeaSAnanth N Mavinakayanahalli .symbol_name = "do_fork", 20804defeaSAnanth N Mavinakayanahalli }; 21804defeaSAnanth N Mavinakayanahalli 22804defeaSAnanth N Mavinakayanahalli /* kprobe pre_handler: called just before the probed instruction is executed */ 23804defeaSAnanth N Mavinakayanahalli static int handler_pre(struct kprobe *p, struct pt_regs *regs) 24804defeaSAnanth N Mavinakayanahalli { 25804defeaSAnanth N Mavinakayanahalli #ifdef CONFIG_X86 26804defeaSAnanth N Mavinakayanahalli printk(KERN_INFO "pre_handler: p->addr = 0x%p, ip = %lx," 27804defeaSAnanth N Mavinakayanahalli " flags = 0x%lx\n", 28804defeaSAnanth N Mavinakayanahalli p->addr, regs->ip, regs->flags); 29804defeaSAnanth N Mavinakayanahalli #endif 30804defeaSAnanth N Mavinakayanahalli #ifdef CONFIG_PPC 31804defeaSAnanth N Mavinakayanahalli printk(KERN_INFO "pre_handler: p->addr = 0x%p, nip = 0x%lx," 32804defeaSAnanth N Mavinakayanahalli " msr = 0x%lx\n", 33804defeaSAnanth N Mavinakayanahalli p->addr, regs->nip, regs->msr); 34804defeaSAnanth N Mavinakayanahalli #endif 35*8a149237SDavid Daney #ifdef CONFIG_MIPS 36*8a149237SDavid Daney printk(KERN_INFO "pre_handler: p->addr = 0x%p, epc = 0x%lx," 37*8a149237SDavid Daney " status = 0x%lx\n", 38*8a149237SDavid Daney p->addr, regs->cp0_epc, regs->cp0_status); 39*8a149237SDavid Daney #endif 40804defeaSAnanth N Mavinakayanahalli 41804defeaSAnanth N Mavinakayanahalli /* A dump_stack() here will give a stack backtrace */ 42804defeaSAnanth N Mavinakayanahalli return 0; 43804defeaSAnanth N Mavinakayanahalli } 44804defeaSAnanth N Mavinakayanahalli 45804defeaSAnanth N Mavinakayanahalli /* kprobe post_handler: called after the probed instruction is executed */ 46804defeaSAnanth N Mavinakayanahalli static void handler_post(struct kprobe *p, struct pt_regs *regs, 47804defeaSAnanth N Mavinakayanahalli unsigned long flags) 48804defeaSAnanth N Mavinakayanahalli { 49804defeaSAnanth N Mavinakayanahalli #ifdef CONFIG_X86 50804defeaSAnanth N Mavinakayanahalli printk(KERN_INFO "post_handler: p->addr = 0x%p, flags = 0x%lx\n", 51804defeaSAnanth N Mavinakayanahalli p->addr, regs->flags); 52804defeaSAnanth N Mavinakayanahalli #endif 53804defeaSAnanth N Mavinakayanahalli #ifdef CONFIG_PPC 54804defeaSAnanth N Mavinakayanahalli printk(KERN_INFO "post_handler: p->addr = 0x%p, msr = 0x%lx\n", 55804defeaSAnanth N Mavinakayanahalli p->addr, regs->msr); 56804defeaSAnanth N Mavinakayanahalli #endif 57*8a149237SDavid Daney #ifdef CONFIG_MIPS 58*8a149237SDavid Daney printk(KERN_INFO "post_handler: p->addr = 0x%p, status = 0x%lx\n", 59*8a149237SDavid Daney p->addr, regs->cp0_status); 60*8a149237SDavid Daney #endif 61804defeaSAnanth N Mavinakayanahalli } 62804defeaSAnanth N Mavinakayanahalli 63804defeaSAnanth N Mavinakayanahalli /* 64804defeaSAnanth N Mavinakayanahalli * fault_handler: this is called if an exception is generated for any 65804defeaSAnanth N Mavinakayanahalli * instruction within the pre- or post-handler, or when Kprobes 66804defeaSAnanth N Mavinakayanahalli * single-steps the probed instruction. 67804defeaSAnanth N Mavinakayanahalli */ 68804defeaSAnanth N Mavinakayanahalli static int handler_fault(struct kprobe *p, struct pt_regs *regs, int trapnr) 69804defeaSAnanth N Mavinakayanahalli { 70804defeaSAnanth N Mavinakayanahalli printk(KERN_INFO "fault_handler: p->addr = 0x%p, trap #%dn", 71804defeaSAnanth N Mavinakayanahalli p->addr, trapnr); 72804defeaSAnanth N Mavinakayanahalli /* Return 0 because we don't handle the fault. */ 73804defeaSAnanth N Mavinakayanahalli return 0; 74804defeaSAnanth N Mavinakayanahalli } 75804defeaSAnanth N Mavinakayanahalli 76804defeaSAnanth N Mavinakayanahalli static int __init kprobe_init(void) 77804defeaSAnanth N Mavinakayanahalli { 78804defeaSAnanth N Mavinakayanahalli int ret; 79804defeaSAnanth N Mavinakayanahalli kp.pre_handler = handler_pre; 80804defeaSAnanth N Mavinakayanahalli kp.post_handler = handler_post; 81804defeaSAnanth N Mavinakayanahalli kp.fault_handler = handler_fault; 82804defeaSAnanth N Mavinakayanahalli 83804defeaSAnanth N Mavinakayanahalli ret = register_kprobe(&kp); 84804defeaSAnanth N Mavinakayanahalli if (ret < 0) { 85804defeaSAnanth N Mavinakayanahalli printk(KERN_INFO "register_kprobe failed, returned %d\n", ret); 86804defeaSAnanth N Mavinakayanahalli return ret; 87804defeaSAnanth N Mavinakayanahalli } 88804defeaSAnanth N Mavinakayanahalli printk(KERN_INFO "Planted kprobe at %p\n", kp.addr); 89804defeaSAnanth N Mavinakayanahalli return 0; 90804defeaSAnanth N Mavinakayanahalli } 91804defeaSAnanth N Mavinakayanahalli 92804defeaSAnanth N Mavinakayanahalli static void __exit kprobe_exit(void) 93804defeaSAnanth N Mavinakayanahalli { 94804defeaSAnanth N Mavinakayanahalli unregister_kprobe(&kp); 95804defeaSAnanth N Mavinakayanahalli printk(KERN_INFO "kprobe at %p unregistered\n", kp.addr); 96804defeaSAnanth N Mavinakayanahalli } 97804defeaSAnanth N Mavinakayanahalli 98804defeaSAnanth N Mavinakayanahalli module_init(kprobe_init) 99804defeaSAnanth N Mavinakayanahalli module_exit(kprobe_exit) 100804defeaSAnanth N Mavinakayanahalli MODULE_LICENSE("GPL"); 101