xref: /linux/samples/kprobes/kprobe_example.c (revision af78cede8bfc772baf424fc03f7cd3c8f9437733)
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
454aea454SPetr Mladek  * 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
1054aea454SPetr Mladek  * 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 
17d04659acSHuang Shijie #define MAX_SYMBOL_LEN	64
18d04659acSHuang Shijie static char symbol[MAX_SYMBOL_LEN] = "_do_fork";
19d04659acSHuang Shijie module_param_string(symbol, symbol, sizeof(symbol), 0644);
20d04659acSHuang Shijie 
21804defeaSAnanth N Mavinakayanahalli /* For each probe you need to allocate a kprobe structure */
22804defeaSAnanth N Mavinakayanahalli static struct kprobe kp = {
23d04659acSHuang Shijie 	.symbol_name	= symbol,
24804defeaSAnanth N Mavinakayanahalli };
25804defeaSAnanth N Mavinakayanahalli 
26804defeaSAnanth N Mavinakayanahalli /* kprobe pre_handler: called just before the probed instruction is executed */
27804defeaSAnanth N Mavinakayanahalli static int handler_pre(struct kprobe *p, struct pt_regs *regs)
28804defeaSAnanth N Mavinakayanahalli {
29804defeaSAnanth N Mavinakayanahalli #ifdef CONFIG_X86
30ea9b5013SHuang Shijie 	printk(KERN_INFO "<%s> pre_handler: p->addr = 0x%p, ip = %lx,"
31804defeaSAnanth N Mavinakayanahalli 			" flags = 0x%lx\n",
32ea9b5013SHuang Shijie 		p->symbol_name, p->addr, regs->ip, regs->flags);
33804defeaSAnanth N Mavinakayanahalli #endif
34804defeaSAnanth N Mavinakayanahalli #ifdef CONFIG_PPC
35ea9b5013SHuang Shijie 	printk(KERN_INFO "<%s> pre_handler: p->addr = 0x%p, nip = 0x%lx,"
36804defeaSAnanth N Mavinakayanahalli 			" msr = 0x%lx\n",
37ea9b5013SHuang Shijie 		p->symbol_name, p->addr, regs->nip, regs->msr);
38804defeaSAnanth N Mavinakayanahalli #endif
398a149237SDavid Daney #ifdef CONFIG_MIPS
40ea9b5013SHuang Shijie 	printk(KERN_INFO "<%s> pre_handler: p->addr = 0x%p, epc = 0x%lx,"
418a149237SDavid Daney 			" status = 0x%lx\n",
42ea9b5013SHuang Shijie 		p->symbol_name, p->addr, regs->cp0_epc, regs->cp0_status);
438a149237SDavid Daney #endif
443fa17c39STony Lu #ifdef CONFIG_TILEGX
45ea9b5013SHuang Shijie 	printk(KERN_INFO "<%s> pre_handler: p->addr = 0x%p, pc = 0x%lx,"
463fa17c39STony Lu 			" ex1 = 0x%lx\n",
47ea9b5013SHuang Shijie 		p->symbol_name, p->addr, regs->pc, regs->ex1);
483fa17c39STony Lu #endif
49*af78cedeSSandeepa Prabhu #ifdef CONFIG_ARM64
50*af78cedeSSandeepa Prabhu 	pr_info("<%s> pre_handler: p->addr = 0x%p, pc = 0x%lx,"
51*af78cedeSSandeepa Prabhu 			" pstate = 0x%lx\n",
52*af78cedeSSandeepa Prabhu 		p->symbol_name, p->addr, (long)regs->pc, (long)regs->pstate);
53*af78cedeSSandeepa Prabhu #endif
54804defeaSAnanth N Mavinakayanahalli 
55804defeaSAnanth N Mavinakayanahalli 	/* A dump_stack() here will give a stack backtrace */
56804defeaSAnanth N Mavinakayanahalli 	return 0;
57804defeaSAnanth N Mavinakayanahalli }
58804defeaSAnanth N Mavinakayanahalli 
59804defeaSAnanth N Mavinakayanahalli /* kprobe post_handler: called after the probed instruction is executed */
60804defeaSAnanth N Mavinakayanahalli static void handler_post(struct kprobe *p, struct pt_regs *regs,
61804defeaSAnanth N Mavinakayanahalli 				unsigned long flags)
62804defeaSAnanth N Mavinakayanahalli {
63804defeaSAnanth N Mavinakayanahalli #ifdef CONFIG_X86
64ea9b5013SHuang Shijie 	printk(KERN_INFO "<%s> post_handler: p->addr = 0x%p, flags = 0x%lx\n",
65ea9b5013SHuang Shijie 		p->symbol_name, p->addr, regs->flags);
66804defeaSAnanth N Mavinakayanahalli #endif
67804defeaSAnanth N Mavinakayanahalli #ifdef CONFIG_PPC
68ea9b5013SHuang Shijie 	printk(KERN_INFO "<%s> post_handler: p->addr = 0x%p, msr = 0x%lx\n",
69ea9b5013SHuang Shijie 		p->symbol_name, p->addr, regs->msr);
70804defeaSAnanth N Mavinakayanahalli #endif
718a149237SDavid Daney #ifdef CONFIG_MIPS
72ea9b5013SHuang Shijie 	printk(KERN_INFO "<%s> post_handler: p->addr = 0x%p, status = 0x%lx\n",
73ea9b5013SHuang Shijie 		p->symbol_name, p->addr, regs->cp0_status);
748a149237SDavid Daney #endif
753fa17c39STony Lu #ifdef CONFIG_TILEGX
76ea9b5013SHuang Shijie 	printk(KERN_INFO "<%s> post_handler: p->addr = 0x%p, ex1 = 0x%lx\n",
77ea9b5013SHuang Shijie 		p->symbol_name, p->addr, regs->ex1);
783fa17c39STony Lu #endif
79*af78cedeSSandeepa Prabhu #ifdef CONFIG_ARM64
80*af78cedeSSandeepa Prabhu 	pr_info("<%s> post_handler: p->addr = 0x%p, pstate = 0x%lx\n",
81*af78cedeSSandeepa Prabhu 		p->symbol_name, p->addr, (long)regs->pstate);
82*af78cedeSSandeepa Prabhu #endif
83804defeaSAnanth N Mavinakayanahalli }
84804defeaSAnanth N Mavinakayanahalli 
85804defeaSAnanth N Mavinakayanahalli /*
86804defeaSAnanth N Mavinakayanahalli  * fault_handler: this is called if an exception is generated for any
87804defeaSAnanth N Mavinakayanahalli  * instruction within the pre- or post-handler, or when Kprobes
88804defeaSAnanth N Mavinakayanahalli  * single-steps the probed instruction.
89804defeaSAnanth N Mavinakayanahalli  */
90804defeaSAnanth N Mavinakayanahalli static int handler_fault(struct kprobe *p, struct pt_regs *regs, int trapnr)
91804defeaSAnanth N Mavinakayanahalli {
92804defeaSAnanth N Mavinakayanahalli 	printk(KERN_INFO "fault_handler: p->addr = 0x%p, trap #%dn",
93804defeaSAnanth N Mavinakayanahalli 		p->addr, trapnr);
94804defeaSAnanth N Mavinakayanahalli 	/* Return 0 because we don't handle the fault. */
95804defeaSAnanth N Mavinakayanahalli 	return 0;
96804defeaSAnanth N Mavinakayanahalli }
97804defeaSAnanth N Mavinakayanahalli 
98804defeaSAnanth N Mavinakayanahalli static int __init kprobe_init(void)
99804defeaSAnanth N Mavinakayanahalli {
100804defeaSAnanth N Mavinakayanahalli 	int ret;
101804defeaSAnanth N Mavinakayanahalli 	kp.pre_handler = handler_pre;
102804defeaSAnanth N Mavinakayanahalli 	kp.post_handler = handler_post;
103804defeaSAnanth N Mavinakayanahalli 	kp.fault_handler = handler_fault;
104804defeaSAnanth N Mavinakayanahalli 
105804defeaSAnanth N Mavinakayanahalli 	ret = register_kprobe(&kp);
106804defeaSAnanth N Mavinakayanahalli 	if (ret < 0) {
107804defeaSAnanth N Mavinakayanahalli 		printk(KERN_INFO "register_kprobe failed, returned %d\n", ret);
108804defeaSAnanth N Mavinakayanahalli 		return ret;
109804defeaSAnanth N Mavinakayanahalli 	}
110804defeaSAnanth N Mavinakayanahalli 	printk(KERN_INFO "Planted kprobe at %p\n", kp.addr);
111804defeaSAnanth N Mavinakayanahalli 	return 0;
112804defeaSAnanth N Mavinakayanahalli }
113804defeaSAnanth N Mavinakayanahalli 
114804defeaSAnanth N Mavinakayanahalli static void __exit kprobe_exit(void)
115804defeaSAnanth N Mavinakayanahalli {
116804defeaSAnanth N Mavinakayanahalli 	unregister_kprobe(&kp);
117804defeaSAnanth N Mavinakayanahalli 	printk(KERN_INFO "kprobe at %p unregistered\n", kp.addr);
118804defeaSAnanth N Mavinakayanahalli }
119804defeaSAnanth N Mavinakayanahalli 
120804defeaSAnanth N Mavinakayanahalli module_init(kprobe_init)
121804defeaSAnanth N Mavinakayanahalli module_exit(kprobe_exit)
122804defeaSAnanth N Mavinakayanahalli MODULE_LICENSE("GPL");
123