1 // SPDX-License-Identifier: GPL-2.0-only
3 * Here's a sample kernel module showing the use of kprobes to dump a
4 * stack trace and selected registers when kernel_clone() is called.
6 * For more information on theory of operation of kprobes, see
7 * Documentation/trace/kprobes.rst
9 * You will see the trace data in /var/log/messages and on the console
10 * whenever kernel_clone() is invoked to create a new process.
13 #define pr_fmt(fmt) "%s: " fmt, __func__
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/kprobes.h>
19 static char symbol[KSYM_NAME_LEN] = "kernel_clone";
20 module_param_string(symbol, symbol, KSYM_NAME_LEN, 0644);
22 /* For each probe you need to allocate a kprobe structure */
23 static struct kprobe kp = {
24 .symbol_name = symbol,
27 /* kprobe pre_handler: called just before the probed instruction is executed */
28 static int __kprobes handler_pre(struct kprobe *p, struct pt_regs *regs)
31 pr_info("<%s> p->addr = 0x%p, ip = %lx, flags = 0x%lx\n",
32 p->symbol_name, p->addr, regs->ip, regs->flags);
35 pr_info("<%s> p->addr = 0x%p, nip = 0x%lx, msr = 0x%lx\n",
36 p->symbol_name, p->addr, regs->nip, regs->msr);
39 pr_info("<%s> p->addr = 0x%p, epc = 0x%lx, status = 0x%lx\n",
40 p->symbol_name, p->addr, regs->cp0_epc, regs->cp0_status);
43 pr_info("<%s> p->addr = 0x%p, pc = 0x%lx, pstate = 0x%lx\n",
44 p->symbol_name, p->addr, (long)regs->pc, (long)regs->pstate);
47 pr_info("<%s> p->addr = 0x%p, pc = 0x%lx, cpsr = 0x%lx\n",
48 p->symbol_name, p->addr, (long)regs->ARM_pc, (long)regs->ARM_cpsr);
51 pr_info("<%s> p->addr = 0x%p, pc = 0x%lx, status = 0x%lx\n",
52 p->symbol_name, p->addr, regs->epc, regs->status);
55 pr_info("<%s> p->addr, 0x%p, ip = 0x%lx, flags = 0x%lx\n",
56 p->symbol_name, p->addr, regs->psw.addr, regs->flags);
58 #ifdef CONFIG_LOONGARCH
59 pr_info("<%s> p->addr = 0x%p, era = 0x%lx, estat = 0x%lx\n",
60 p->symbol_name, p->addr, regs->csr_era, regs->csr_estat);
63 /* A dump_stack() here will give a stack backtrace */
67 /* kprobe post_handler: called after the probed instruction is executed */
68 static void __kprobes handler_post(struct kprobe *p, struct pt_regs *regs,
72 pr_info("<%s> p->addr = 0x%p, flags = 0x%lx\n",
73 p->symbol_name, p->addr, regs->flags);
76 pr_info("<%s> p->addr = 0x%p, msr = 0x%lx\n",
77 p->symbol_name, p->addr, regs->msr);
80 pr_info("<%s> p->addr = 0x%p, status = 0x%lx\n",
81 p->symbol_name, p->addr, regs->cp0_status);
84 pr_info("<%s> p->addr = 0x%p, pstate = 0x%lx\n",
85 p->symbol_name, p->addr, (long)regs->pstate);
88 pr_info("<%s> p->addr = 0x%p, cpsr = 0x%lx\n",
89 p->symbol_name, p->addr, (long)regs->ARM_cpsr);
92 pr_info("<%s> p->addr = 0x%p, status = 0x%lx\n",
93 p->symbol_name, p->addr, regs->status);
96 pr_info("<%s> p->addr, 0x%p, flags = 0x%lx\n",
97 p->symbol_name, p->addr, regs->flags);
99 #ifdef CONFIG_LOONGARCH
100 pr_info("<%s> p->addr = 0x%p, estat = 0x%lx\n",
101 p->symbol_name, p->addr, regs->csr_estat);
105 static int __init kprobe_init(void)
108 kp.pre_handler = handler_pre;
109 kp.post_handler = handler_post;
111 ret = register_kprobe(&kp);
113 pr_err("register_kprobe failed, returned %d\n", ret);
116 pr_info("Planted kprobe at %p\n", kp.addr);
120 static void __exit kprobe_exit(void)
122 unregister_kprobe(&kp);
123 pr_info("kprobe at %p unregistered\n", kp.addr);
126 module_init(kprobe_init)
127 module_exit(kprobe_exit)
128 MODULE_LICENSE("GPL");