blob: d693c23a85e8a0f21df776d48463950ab13cedaa [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
Petr Mladek54aea452015-10-01 15:37:11 -07005 * stack trace and selected registers when _do_fork() is called.
Ananth N Mavinakayanahalli804defe2008-03-04 14:28:38 -08006 *
7 * For more information on theory of operation of kprobes, see
8 * Documentation/kprobes.txt
9 *
10 * You will see the trace data in /var/log/messages and on the console
Petr Mladek54aea452015-10-01 15:37:11 -070011 * whenever _do_fork() 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
19static char symbol[MAX_SYMBOL_LEN] = "_do_fork";
20module_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 */
28static int handler_pre(struct kprobe *p, struct pt_regs *regs)
29{
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 */
57static void handler_post(struct kprobe *p, struct pt_regs *regs,
58 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}
93
94static int __init kprobe_init(void)
95{
96 int ret;
97 kp.pre_handler = handler_pre;
98 kp.post_handler = handler_post;
99 kp.fault_handler = handler_fault;
100
101 ret = register_kprobe(&kp);
102 if (ret < 0) {
Huang Shijiee708c142016-08-03 13:46:03 -0700103 pr_err("register_kprobe failed, returned %d\n", ret);
Ananth N Mavinakayanahalli804defe2008-03-04 14:28:38 -0800104 return ret;
105 }
Huang Shijiee708c142016-08-03 13:46:03 -0700106 pr_info("Planted kprobe at %p\n", kp.addr);
Ananth N Mavinakayanahalli804defe2008-03-04 14:28:38 -0800107 return 0;
108}
109
110static void __exit kprobe_exit(void)
111{
112 unregister_kprobe(&kp);
Huang Shijiee708c142016-08-03 13:46:03 -0700113 pr_info("kprobe at %p unregistered\n", kp.addr);
Ananth N Mavinakayanahalli804defe2008-03-04 14:28:38 -0800114}
115
116module_init(kprobe_init)
117module_exit(kprobe_exit)
118MODULE_LICENSE("GPL");