xref: /linux/samples/kprobes/kprobe_example.c (revision 8a07ac39f87d6c762006398029762c40e4d9d075)
109c434b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2804defeaSAnanth N Mavinakayanahalli /*
3804defeaSAnanth N Mavinakayanahalli  * Here's a sample kernel module showing the use of kprobes to dump a
425239fd3SChristian Brauner  * stack trace and selected registers when kernel_clone() is called.
5804defeaSAnanth N Mavinakayanahalli  *
6804defeaSAnanth N Mavinakayanahalli  * For more information on theory of operation of kprobes, see
77f9a2357SMauro Carvalho Chehab  * Documentation/trace/kprobes.rst
8804defeaSAnanth N Mavinakayanahalli  *
9804defeaSAnanth N Mavinakayanahalli  * You will see the trace data in /var/log/messages and on the console
1025239fd3SChristian Brauner  * whenever kernel_clone() 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
1825239fd3SChristian Brauner static char symbol[MAX_SYMBOL_LEN] = "kernel_clone";
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 */
27d85eaa94SMasami Hiramatsu static int __kprobes handler_pre(struct kprobe *p, struct pt_regs *regs)
28804defeaSAnanth N Mavinakayanahalli {
29804defeaSAnanth N Mavinakayanahalli #ifdef CONFIG_X86
30e708c148SHuang Shijie 	pr_info("<%s> pre_handler: p->addr = 0x%p, ip = %lx, flags = 0x%lx\n",
31ea9b5013SHuang Shijie 		p->symbol_name, p->addr, regs->ip, regs->flags);
32804defeaSAnanth N Mavinakayanahalli #endif
33804defeaSAnanth N Mavinakayanahalli #ifdef CONFIG_PPC
34e708c148SHuang Shijie 	pr_info("<%s> pre_handler: p->addr = 0x%p, nip = 0x%lx, msr = 0x%lx\n",
35ea9b5013SHuang Shijie 		p->symbol_name, p->addr, regs->nip, regs->msr);
36804defeaSAnanth N Mavinakayanahalli #endif
378a149237SDavid Daney #ifdef CONFIG_MIPS
38e708c148SHuang Shijie 	pr_info("<%s> pre_handler: p->addr = 0x%p, epc = 0x%lx, status = 0x%lx\n",
39ea9b5013SHuang Shijie 		p->symbol_name, p->addr, regs->cp0_epc, regs->cp0_status);
408a149237SDavid Daney #endif
41af78cedeSSandeepa Prabhu #ifdef CONFIG_ARM64
42af78cedeSSandeepa Prabhu 	pr_info("<%s> pre_handler: p->addr = 0x%p, pc = 0x%lx,"
43af78cedeSSandeepa Prabhu 			" pstate = 0x%lx\n",
44af78cedeSSandeepa Prabhu 		p->symbol_name, p->addr, (long)regs->pc, (long)regs->pstate);
45af78cedeSSandeepa Prabhu #endif
466970613bSMarc Koderer #ifdef CONFIG_ARM
476970613bSMarc Koderer 	pr_info("<%s> pre_handler: p->addr = 0x%p, pc = 0x%lx, cpsr = 0x%lx\n",
486970613bSMarc Koderer 		p->symbol_name, p->addr, (long)regs->ARM_pc, (long)regs->ARM_cpsr);
496970613bSMarc Koderer #endif
50*8a07ac39SJisheng Zhang #ifdef CONFIG_RISCV
51*8a07ac39SJisheng Zhang 	pr_info("<%s> pre_handler: p->addr = 0x%p, pc = 0x%lx, status = 0x%lx\n",
52*8a07ac39SJisheng Zhang 		p->symbol_name, p->addr, regs->epc, regs->status);
53*8a07ac39SJisheng Zhang #endif
54e16c5dd5SJohannes Thumshirn #ifdef CONFIG_S390
55e16c5dd5SJohannes Thumshirn 	pr_info("<%s> pre_handler: p->addr, 0x%p, ip = 0x%lx, flags = 0x%lx\n",
56e16c5dd5SJohannes Thumshirn 		p->symbol_name, p->addr, regs->psw.addr, regs->flags);
57e16c5dd5SJohannes Thumshirn #endif
58804defeaSAnanth N Mavinakayanahalli 
59804defeaSAnanth N Mavinakayanahalli 	/* A dump_stack() here will give a stack backtrace */
60804defeaSAnanth N Mavinakayanahalli 	return 0;
61804defeaSAnanth N Mavinakayanahalli }
62804defeaSAnanth N Mavinakayanahalli 
63804defeaSAnanth N Mavinakayanahalli /* kprobe post_handler: called after the probed instruction is executed */
64d85eaa94SMasami Hiramatsu static void __kprobes handler_post(struct kprobe *p, struct pt_regs *regs,
65804defeaSAnanth N Mavinakayanahalli 				unsigned long flags)
66804defeaSAnanth N Mavinakayanahalli {
67804defeaSAnanth N Mavinakayanahalli #ifdef CONFIG_X86
68e708c148SHuang Shijie 	pr_info("<%s> post_handler: p->addr = 0x%p, flags = 0x%lx\n",
69ea9b5013SHuang Shijie 		p->symbol_name, p->addr, regs->flags);
70804defeaSAnanth N Mavinakayanahalli #endif
71804defeaSAnanth N Mavinakayanahalli #ifdef CONFIG_PPC
72e708c148SHuang Shijie 	pr_info("<%s> post_handler: p->addr = 0x%p, msr = 0x%lx\n",
73ea9b5013SHuang Shijie 		p->symbol_name, p->addr, regs->msr);
74804defeaSAnanth N Mavinakayanahalli #endif
758a149237SDavid Daney #ifdef CONFIG_MIPS
76e708c148SHuang Shijie 	pr_info("<%s> post_handler: p->addr = 0x%p, status = 0x%lx\n",
77ea9b5013SHuang Shijie 		p->symbol_name, p->addr, regs->cp0_status);
788a149237SDavid Daney #endif
79af78cedeSSandeepa Prabhu #ifdef CONFIG_ARM64
80af78cedeSSandeepa Prabhu 	pr_info("<%s> post_handler: p->addr = 0x%p, pstate = 0x%lx\n",
81af78cedeSSandeepa Prabhu 		p->symbol_name, p->addr, (long)regs->pstate);
82af78cedeSSandeepa Prabhu #endif
836970613bSMarc Koderer #ifdef CONFIG_ARM
846970613bSMarc Koderer 	pr_info("<%s> post_handler: p->addr = 0x%p, cpsr = 0x%lx\n",
856970613bSMarc Koderer 		p->symbol_name, p->addr, (long)regs->ARM_cpsr);
866970613bSMarc Koderer #endif
87*8a07ac39SJisheng Zhang #ifdef CONFIG_RISCV
88*8a07ac39SJisheng Zhang 	pr_info("<%s> post_handler: p->addr = 0x%p, status = 0x%lx\n",
89*8a07ac39SJisheng Zhang 		p->symbol_name, p->addr, regs->status);
90*8a07ac39SJisheng Zhang #endif
91e16c5dd5SJohannes Thumshirn #ifdef CONFIG_S390
92e16c5dd5SJohannes Thumshirn 	pr_info("<%s> pre_handler: p->addr, 0x%p, flags = 0x%lx\n",
93e16c5dd5SJohannes Thumshirn 		p->symbol_name, p->addr, regs->flags);
94e16c5dd5SJohannes Thumshirn #endif
95804defeaSAnanth N Mavinakayanahalli }
96804defeaSAnanth N Mavinakayanahalli 
97804defeaSAnanth N Mavinakayanahalli /*
98804defeaSAnanth N Mavinakayanahalli  * fault_handler: this is called if an exception is generated for any
99804defeaSAnanth N Mavinakayanahalli  * instruction within the pre- or post-handler, or when Kprobes
100804defeaSAnanth N Mavinakayanahalli  * single-steps the probed instruction.
101804defeaSAnanth N Mavinakayanahalli  */
102804defeaSAnanth N Mavinakayanahalli static int handler_fault(struct kprobe *p, struct pt_regs *regs, int trapnr)
103804defeaSAnanth N Mavinakayanahalli {
104e708c148SHuang Shijie 	pr_info("fault_handler: p->addr = 0x%p, trap #%dn", p->addr, trapnr);
105804defeaSAnanth N Mavinakayanahalli 	/* Return 0 because we don't handle the fault. */
106804defeaSAnanth N Mavinakayanahalli 	return 0;
107804defeaSAnanth N Mavinakayanahalli }
108d85eaa94SMasami Hiramatsu /* NOKPROBE_SYMBOL() is also available */
109d85eaa94SMasami Hiramatsu NOKPROBE_SYMBOL(handler_fault);
110804defeaSAnanth N Mavinakayanahalli 
111804defeaSAnanth N Mavinakayanahalli static int __init kprobe_init(void)
112804defeaSAnanth N Mavinakayanahalli {
113804defeaSAnanth N Mavinakayanahalli 	int ret;
114804defeaSAnanth N Mavinakayanahalli 	kp.pre_handler = handler_pre;
115804defeaSAnanth N Mavinakayanahalli 	kp.post_handler = handler_post;
116804defeaSAnanth N Mavinakayanahalli 	kp.fault_handler = handler_fault;
117804defeaSAnanth N Mavinakayanahalli 
118804defeaSAnanth N Mavinakayanahalli 	ret = register_kprobe(&kp);
119804defeaSAnanth N Mavinakayanahalli 	if (ret < 0) {
120e708c148SHuang Shijie 		pr_err("register_kprobe failed, returned %d\n", ret);
121804defeaSAnanth N Mavinakayanahalli 		return ret;
122804defeaSAnanth N Mavinakayanahalli 	}
123e708c148SHuang Shijie 	pr_info("Planted kprobe at %p\n", kp.addr);
124804defeaSAnanth N Mavinakayanahalli 	return 0;
125804defeaSAnanth N Mavinakayanahalli }
126804defeaSAnanth N Mavinakayanahalli 
127804defeaSAnanth N Mavinakayanahalli static void __exit kprobe_exit(void)
128804defeaSAnanth N Mavinakayanahalli {
129804defeaSAnanth N Mavinakayanahalli 	unregister_kprobe(&kp);
130e708c148SHuang Shijie 	pr_info("kprobe at %p unregistered\n", kp.addr);
131804defeaSAnanth N Mavinakayanahalli }
132804defeaSAnanth N Mavinakayanahalli 
133804defeaSAnanth N Mavinakayanahalli module_init(kprobe_init)
134804defeaSAnanth N Mavinakayanahalli module_exit(kprobe_exit)
135804defeaSAnanth N Mavinakayanahalli MODULE_LICENSE("GPL");
136