blob: a02f53836ee1b064976e25cd55ccc4b7a76f0455 [file] [log] [blame]
Thomas Gleixner09c434b2019-05-19 13:08:20 +01001// SPDX-License-Identifier: GPL-2.0-only
Ananth N Mavinakayanahalli804defe2008-03-04 14:28:38 -08002/*
3 * NOTE: This example is works on x86 and powerpc.
4 * Here's a sample kernel module showing the use of kprobes to dump a
Christian Brauner25239fd2020-08-19 12:46:52 +02005 * stack trace and selected registers when kernel_clone() is called.
Ananth N Mavinakayanahalli804defe2008-03-04 14:28:38 -08006 *
7 * For more information on theory of operation of kprobes, see
Mauro Carvalho Chehab8e2a46a2020-06-15 08:50:25 +02008 * Documentation/staging/kprobes.rst
Ananth N Mavinakayanahalli804defe2008-03-04 14:28:38 -08009 *
10 * You will see the trace data in /var/log/messages and on the console
Christian Brauner25239fd2020-08-19 12:46:52 +020011 * whenever kernel_clone() is invoked to create a new process.
Ananth N Mavinakayanahalli804defe2008-03-04 14:28:38 -080012 */
13
14#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/kprobes.h>
17
Huang Shijied04659a2016-05-20 17:04:33 -070018#define MAX_SYMBOL_LEN 64
Christian Brauner25239fd2020-08-19 12:46:52 +020019static char symbol[MAX_SYMBOL_LEN] = "kernel_clone";
Huang Shijied04659a2016-05-20 17:04:33 -070020module_param_string(symbol, symbol, sizeof(symbol), 0644);
21
Ananth N Mavinakayanahalli804defe2008-03-04 14:28:38 -080022/* For each probe you need to allocate a kprobe structure */
23static struct kprobe kp = {
Huang Shijied04659a2016-05-20 17:04:33 -070024 .symbol_name = symbol,
Ananth N Mavinakayanahalli804defe2008-03-04 14:28:38 -080025};
26
27/* kprobe pre_handler: called just before the probed instruction is executed */
Masami Hiramatsud85eaa92020-03-26 23:50:11 +090028static int __kprobes handler_pre(struct kprobe *p, struct pt_regs *regs)
Ananth N Mavinakayanahalli804defe2008-03-04 14:28:38 -080029{
30#ifdef CONFIG_X86
Huang Shijiee708c142016-08-03 13:46:03 -070031 pr_info("<%s> pre_handler: p->addr = 0x%p, ip = %lx, flags = 0x%lx\n",
Huang Shijieea9b5012016-05-20 17:04:36 -070032 p->symbol_name, p->addr, regs->ip, regs->flags);
Ananth N Mavinakayanahalli804defe2008-03-04 14:28:38 -080033#endif
34#ifdef CONFIG_PPC
Huang Shijiee708c142016-08-03 13:46:03 -070035 pr_info("<%s> pre_handler: p->addr = 0x%p, nip = 0x%lx, msr = 0x%lx\n",
Huang Shijieea9b5012016-05-20 17:04:36 -070036 p->symbol_name, p->addr, regs->nip, regs->msr);
Ananth N Mavinakayanahalli804defe2008-03-04 14:28:38 -080037#endif
David Daney8a149232010-08-03 11:22:21 -070038#ifdef CONFIG_MIPS
Huang Shijiee708c142016-08-03 13:46:03 -070039 pr_info("<%s> pre_handler: p->addr = 0x%p, epc = 0x%lx, status = 0x%lx\n",
Huang Shijieea9b5012016-05-20 17:04:36 -070040 p->symbol_name, p->addr, regs->cp0_epc, regs->cp0_status);
David Daney8a149232010-08-03 11:22:21 -070041#endif
Sandeepa Prabhuaf78ced2016-07-08 12:35:54 -040042#ifdef CONFIG_ARM64
43 pr_info("<%s> pre_handler: p->addr = 0x%p, pc = 0x%lx,"
44 " pstate = 0x%lx\n",
45 p->symbol_name, p->addr, (long)regs->pc, (long)regs->pstate);
46#endif
Johannes Thumshirne16c5dd2017-09-14 14:11:15 +020047#ifdef CONFIG_S390
48 pr_info("<%s> pre_handler: p->addr, 0x%p, ip = 0x%lx, flags = 0x%lx\n",
49 p->symbol_name, p->addr, regs->psw.addr, regs->flags);
50#endif
Ananth N Mavinakayanahalli804defe2008-03-04 14:28:38 -080051
52 /* A dump_stack() here will give a stack backtrace */
53 return 0;
54}
55
56/* kprobe post_handler: called after the probed instruction is executed */
Masami Hiramatsud85eaa92020-03-26 23:50:11 +090057static void __kprobes handler_post(struct kprobe *p, struct pt_regs *regs,
Ananth N Mavinakayanahalli804defe2008-03-04 14:28:38 -080058 unsigned long flags)
59{
60#ifdef CONFIG_X86
Huang Shijiee708c142016-08-03 13:46:03 -070061 pr_info("<%s> post_handler: p->addr = 0x%p, flags = 0x%lx\n",
Huang Shijieea9b5012016-05-20 17:04:36 -070062 p->symbol_name, p->addr, regs->flags);
Ananth N Mavinakayanahalli804defe2008-03-04 14:28:38 -080063#endif
64#ifdef CONFIG_PPC
Huang Shijiee708c142016-08-03 13:46:03 -070065 pr_info("<%s> post_handler: p->addr = 0x%p, msr = 0x%lx\n",
Huang Shijieea9b5012016-05-20 17:04:36 -070066 p->symbol_name, p->addr, regs->msr);
Ananth N Mavinakayanahalli804defe2008-03-04 14:28:38 -080067#endif
David Daney8a149232010-08-03 11:22:21 -070068#ifdef CONFIG_MIPS
Huang Shijiee708c142016-08-03 13:46:03 -070069 pr_info("<%s> post_handler: p->addr = 0x%p, status = 0x%lx\n",
Huang Shijieea9b5012016-05-20 17:04:36 -070070 p->symbol_name, p->addr, regs->cp0_status);
David Daney8a149232010-08-03 11:22:21 -070071#endif
Sandeepa Prabhuaf78ced2016-07-08 12:35:54 -040072#ifdef CONFIG_ARM64
73 pr_info("<%s> post_handler: p->addr = 0x%p, pstate = 0x%lx\n",
74 p->symbol_name, p->addr, (long)regs->pstate);
75#endif
Johannes Thumshirne16c5dd2017-09-14 14:11:15 +020076#ifdef CONFIG_S390
77 pr_info("<%s> pre_handler: p->addr, 0x%p, flags = 0x%lx\n",
78 p->symbol_name, p->addr, regs->flags);
79#endif
Ananth N Mavinakayanahalli804defe2008-03-04 14:28:38 -080080}
81
82/*
83 * fault_handler: this is called if an exception is generated for any
84 * instruction within the pre- or post-handler, or when Kprobes
85 * single-steps the probed instruction.
86 */
87static int handler_fault(struct kprobe *p, struct pt_regs *regs, int trapnr)
88{
Huang Shijiee708c142016-08-03 13:46:03 -070089 pr_info("fault_handler: p->addr = 0x%p, trap #%dn", p->addr, trapnr);
Ananth N Mavinakayanahalli804defe2008-03-04 14:28:38 -080090 /* Return 0 because we don't handle the fault. */
91 return 0;
92}
Masami Hiramatsud85eaa92020-03-26 23:50:11 +090093/* NOKPROBE_SYMBOL() is also available */
94NOKPROBE_SYMBOL(handler_fault);
Ananth N Mavinakayanahalli804defe2008-03-04 14:28:38 -080095
96static int __init kprobe_init(void)
97{
98 int ret;
99 kp.pre_handler = handler_pre;
100 kp.post_handler = handler_post;
101 kp.fault_handler = handler_fault;
102
103 ret = register_kprobe(&kp);
104 if (ret < 0) {
Huang Shijiee708c142016-08-03 13:46:03 -0700105 pr_err("register_kprobe failed, returned %d\n", ret);
Ananth N Mavinakayanahalli804defe2008-03-04 14:28:38 -0800106 return ret;
107 }
Huang Shijiee708c142016-08-03 13:46:03 -0700108 pr_info("Planted kprobe at %p\n", kp.addr);
Ananth N Mavinakayanahalli804defe2008-03-04 14:28:38 -0800109 return 0;
110}
111
112static void __exit kprobe_exit(void)
113{
114 unregister_kprobe(&kp);
Huang Shijiee708c142016-08-03 13:46:03 -0700115 pr_info("kprobe at %p unregistered\n", kp.addr);
Ananth N Mavinakayanahalli804defe2008-03-04 14:28:38 -0800116}
117
118module_init(kprobe_init)
119module_exit(kprobe_exit)
120MODULE_LICENSE("GPL");