xref: /linux/samples/kprobes/kprobe_example.c (revision 09c434b8a0047c69e48499de0107de312901e798)
1*09c434b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2804defeaSAnanth N Mavinakayanahalli /*
3804defeaSAnanth N Mavinakayanahalli  * NOTE: This example is works on x86 and powerpc.
4804defeaSAnanth N Mavinakayanahalli  * Here's a sample kernel module showing the use of kprobes to dump a
554aea454SPetr Mladek  * stack trace and selected registers when _do_fork() is called.
6804defeaSAnanth N Mavinakayanahalli  *
7804defeaSAnanth N Mavinakayanahalli  * For more information on theory of operation of kprobes, see
8804defeaSAnanth N Mavinakayanahalli  * Documentation/kprobes.txt
9804defeaSAnanth N Mavinakayanahalli  *
10804defeaSAnanth N Mavinakayanahalli  * You will see the trace data in /var/log/messages and on the console
1154aea454SPetr Mladek  * whenever _do_fork() is invoked to create a new process.
12804defeaSAnanth N Mavinakayanahalli  */
13804defeaSAnanth N Mavinakayanahalli 
14804defeaSAnanth N Mavinakayanahalli #include <linux/kernel.h>
15804defeaSAnanth N Mavinakayanahalli #include <linux/module.h>
16804defeaSAnanth N Mavinakayanahalli #include <linux/kprobes.h>
17804defeaSAnanth N Mavinakayanahalli 
18d04659acSHuang Shijie #define MAX_SYMBOL_LEN	64
19d04659acSHuang Shijie static char symbol[MAX_SYMBOL_LEN] = "_do_fork";
20d04659acSHuang Shijie module_param_string(symbol, symbol, sizeof(symbol), 0644);
21d04659acSHuang Shijie 
22804defeaSAnanth N Mavinakayanahalli /* For each probe you need to allocate a kprobe structure */
23804defeaSAnanth N Mavinakayanahalli static struct kprobe kp = {
24d04659acSHuang Shijie 	.symbol_name	= symbol,
25804defeaSAnanth N Mavinakayanahalli };
26804defeaSAnanth N Mavinakayanahalli 
27804defeaSAnanth N Mavinakayanahalli /* kprobe pre_handler: called just before the probed instruction is executed */
28804defeaSAnanth N Mavinakayanahalli static int handler_pre(struct kprobe *p, struct pt_regs *regs)
29804defeaSAnanth N Mavinakayanahalli {
30804defeaSAnanth N Mavinakayanahalli #ifdef CONFIG_X86
31e708c148SHuang Shijie 	pr_info("<%s> pre_handler: p->addr = 0x%p, ip = %lx, 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
35e708c148SHuang Shijie 	pr_info("<%s> pre_handler: p->addr = 0x%p, nip = 0x%lx, msr = 0x%lx\n",
36ea9b5013SHuang Shijie 		p->symbol_name, p->addr, regs->nip, regs->msr);
37804defeaSAnanth N Mavinakayanahalli #endif
388a149237SDavid Daney #ifdef CONFIG_MIPS
39e708c148SHuang Shijie 	pr_info("<%s> pre_handler: p->addr = 0x%p, epc = 0x%lx, status = 0x%lx\n",
40ea9b5013SHuang Shijie 		p->symbol_name, p->addr, regs->cp0_epc, regs->cp0_status);
418a149237SDavid Daney #endif
42af78cedeSSandeepa Prabhu #ifdef CONFIG_ARM64
43af78cedeSSandeepa Prabhu 	pr_info("<%s> pre_handler: p->addr = 0x%p, pc = 0x%lx,"
44af78cedeSSandeepa Prabhu 			" pstate = 0x%lx\n",
45af78cedeSSandeepa Prabhu 		p->symbol_name, p->addr, (long)regs->pc, (long)regs->pstate);
46af78cedeSSandeepa Prabhu #endif
47e16c5dd5SJohannes Thumshirn #ifdef CONFIG_S390
48e16c5dd5SJohannes Thumshirn 	pr_info("<%s> pre_handler: p->addr, 0x%p, ip = 0x%lx, flags = 0x%lx\n",
49e16c5dd5SJohannes Thumshirn 		p->symbol_name, p->addr, regs->psw.addr, regs->flags);
50e16c5dd5SJohannes Thumshirn #endif
51804defeaSAnanth N Mavinakayanahalli 
52804defeaSAnanth N Mavinakayanahalli 	/* A dump_stack() here will give a stack backtrace */
53804defeaSAnanth N Mavinakayanahalli 	return 0;
54804defeaSAnanth N Mavinakayanahalli }
55804defeaSAnanth N Mavinakayanahalli 
56804defeaSAnanth N Mavinakayanahalli /* kprobe post_handler: called after the probed instruction is executed */
57804defeaSAnanth N Mavinakayanahalli static void handler_post(struct kprobe *p, struct pt_regs *regs,
58804defeaSAnanth N Mavinakayanahalli 				unsigned long flags)
59804defeaSAnanth N Mavinakayanahalli {
60804defeaSAnanth N Mavinakayanahalli #ifdef CONFIG_X86
61e708c148SHuang Shijie 	pr_info("<%s> post_handler: p->addr = 0x%p, flags = 0x%lx\n",
62ea9b5013SHuang Shijie 		p->symbol_name, p->addr, regs->flags);
63804defeaSAnanth N Mavinakayanahalli #endif
64804defeaSAnanth N Mavinakayanahalli #ifdef CONFIG_PPC
65e708c148SHuang Shijie 	pr_info("<%s> post_handler: p->addr = 0x%p, msr = 0x%lx\n",
66ea9b5013SHuang Shijie 		p->symbol_name, p->addr, regs->msr);
67804defeaSAnanth N Mavinakayanahalli #endif
688a149237SDavid Daney #ifdef CONFIG_MIPS
69e708c148SHuang Shijie 	pr_info("<%s> post_handler: p->addr = 0x%p, status = 0x%lx\n",
70ea9b5013SHuang Shijie 		p->symbol_name, p->addr, regs->cp0_status);
718a149237SDavid Daney #endif
72af78cedeSSandeepa Prabhu #ifdef CONFIG_ARM64
73af78cedeSSandeepa Prabhu 	pr_info("<%s> post_handler: p->addr = 0x%p, pstate = 0x%lx\n",
74af78cedeSSandeepa Prabhu 		p->symbol_name, p->addr, (long)regs->pstate);
75af78cedeSSandeepa Prabhu #endif
76e16c5dd5SJohannes Thumshirn #ifdef CONFIG_S390
77e16c5dd5SJohannes Thumshirn 	pr_info("<%s> pre_handler: p->addr, 0x%p, flags = 0x%lx\n",
78e16c5dd5SJohannes Thumshirn 		p->symbol_name, p->addr, regs->flags);
79e16c5dd5SJohannes Thumshirn #endif
80804defeaSAnanth N Mavinakayanahalli }
81804defeaSAnanth N Mavinakayanahalli 
82804defeaSAnanth N Mavinakayanahalli /*
83804defeaSAnanth N Mavinakayanahalli  * fault_handler: this is called if an exception is generated for any
84804defeaSAnanth N Mavinakayanahalli  * instruction within the pre- or post-handler, or when Kprobes
85804defeaSAnanth N Mavinakayanahalli  * single-steps the probed instruction.
86804defeaSAnanth N Mavinakayanahalli  */
87804defeaSAnanth N Mavinakayanahalli static int handler_fault(struct kprobe *p, struct pt_regs *regs, int trapnr)
88804defeaSAnanth N Mavinakayanahalli {
89e708c148SHuang Shijie 	pr_info("fault_handler: p->addr = 0x%p, trap #%dn", p->addr, trapnr);
90804defeaSAnanth N Mavinakayanahalli 	/* Return 0 because we don't handle the fault. */
91804defeaSAnanth N Mavinakayanahalli 	return 0;
92804defeaSAnanth N Mavinakayanahalli }
93804defeaSAnanth N Mavinakayanahalli 
94804defeaSAnanth N Mavinakayanahalli static int __init kprobe_init(void)
95804defeaSAnanth N Mavinakayanahalli {
96804defeaSAnanth N Mavinakayanahalli 	int ret;
97804defeaSAnanth N Mavinakayanahalli 	kp.pre_handler = handler_pre;
98804defeaSAnanth N Mavinakayanahalli 	kp.post_handler = handler_post;
99804defeaSAnanth N Mavinakayanahalli 	kp.fault_handler = handler_fault;
100804defeaSAnanth N Mavinakayanahalli 
101804defeaSAnanth N Mavinakayanahalli 	ret = register_kprobe(&kp);
102804defeaSAnanth N Mavinakayanahalli 	if (ret < 0) {
103e708c148SHuang Shijie 		pr_err("register_kprobe failed, returned %d\n", ret);
104804defeaSAnanth N Mavinakayanahalli 		return ret;
105804defeaSAnanth N Mavinakayanahalli 	}
106e708c148SHuang Shijie 	pr_info("Planted kprobe at %p\n", kp.addr);
107804defeaSAnanth N Mavinakayanahalli 	return 0;
108804defeaSAnanth N Mavinakayanahalli }
109804defeaSAnanth N Mavinakayanahalli 
110804defeaSAnanth N Mavinakayanahalli static void __exit kprobe_exit(void)
111804defeaSAnanth N Mavinakayanahalli {
112804defeaSAnanth N Mavinakayanahalli 	unregister_kprobe(&kp);
113e708c148SHuang Shijie 	pr_info("kprobe at %p unregistered\n", kp.addr);
114804defeaSAnanth N Mavinakayanahalli }
115804defeaSAnanth N Mavinakayanahalli 
116804defeaSAnanth N Mavinakayanahalli module_init(kprobe_init)
117804defeaSAnanth N Mavinakayanahalli module_exit(kprobe_exit)
118804defeaSAnanth N Mavinakayanahalli MODULE_LICENSE("GPL");
119