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 13*db1ea668STiezhu Yang #define pr_fmt(fmt) "%s: " fmt, __func__ 14*db1ea668STiezhu Yang 15804defeaSAnanth N Mavinakayanahalli #include <linux/kernel.h> 16804defeaSAnanth N Mavinakayanahalli #include <linux/module.h> 17804defeaSAnanth N Mavinakayanahalli #include <linux/kprobes.h> 18804defeaSAnanth N Mavinakayanahalli 19d04659acSHuang Shijie #define MAX_SYMBOL_LEN 64 2025239fd3SChristian Brauner static char symbol[MAX_SYMBOL_LEN] = "kernel_clone"; 21d04659acSHuang Shijie module_param_string(symbol, symbol, sizeof(symbol), 0644); 22d04659acSHuang Shijie 23804defeaSAnanth N Mavinakayanahalli /* For each probe you need to allocate a kprobe structure */ 24804defeaSAnanth N Mavinakayanahalli static struct kprobe kp = { 25d04659acSHuang Shijie .symbol_name = symbol, 26804defeaSAnanth N Mavinakayanahalli }; 27804defeaSAnanth N Mavinakayanahalli 28804defeaSAnanth N Mavinakayanahalli /* kprobe pre_handler: called just before the probed instruction is executed */ 29d85eaa94SMasami Hiramatsu static int __kprobes handler_pre(struct kprobe *p, struct pt_regs *regs) 30804defeaSAnanth N Mavinakayanahalli { 31804defeaSAnanth N Mavinakayanahalli #ifdef CONFIG_X86 32*db1ea668STiezhu Yang pr_info("<%s> p->addr = 0x%p, ip = %lx, flags = 0x%lx\n", 33ea9b5013SHuang Shijie p->symbol_name, p->addr, regs->ip, regs->flags); 34804defeaSAnanth N Mavinakayanahalli #endif 35804defeaSAnanth N Mavinakayanahalli #ifdef CONFIG_PPC 36*db1ea668STiezhu Yang pr_info("<%s> p->addr = 0x%p, nip = 0x%lx, msr = 0x%lx\n", 37ea9b5013SHuang Shijie p->symbol_name, p->addr, regs->nip, regs->msr); 38804defeaSAnanth N Mavinakayanahalli #endif 398a149237SDavid Daney #ifdef CONFIG_MIPS 40*db1ea668STiezhu Yang pr_info("<%s> p->addr = 0x%p, epc = 0x%lx, status = 0x%lx\n", 41ea9b5013SHuang Shijie p->symbol_name, p->addr, regs->cp0_epc, regs->cp0_status); 428a149237SDavid Daney #endif 43af78cedeSSandeepa Prabhu #ifdef CONFIG_ARM64 44*db1ea668STiezhu Yang pr_info("<%s> p->addr = 0x%p, pc = 0x%lx, pstate = 0x%lx\n", 45af78cedeSSandeepa Prabhu p->symbol_name, p->addr, (long)regs->pc, (long)regs->pstate); 46af78cedeSSandeepa Prabhu #endif 476970613bSMarc Koderer #ifdef CONFIG_ARM 48*db1ea668STiezhu Yang pr_info("<%s> p->addr = 0x%p, pc = 0x%lx, cpsr = 0x%lx\n", 496970613bSMarc Koderer p->symbol_name, p->addr, (long)regs->ARM_pc, (long)regs->ARM_cpsr); 506970613bSMarc Koderer #endif 518a07ac39SJisheng Zhang #ifdef CONFIG_RISCV 52*db1ea668STiezhu Yang pr_info("<%s> p->addr = 0x%p, pc = 0x%lx, status = 0x%lx\n", 538a07ac39SJisheng Zhang p->symbol_name, p->addr, regs->epc, regs->status); 548a07ac39SJisheng Zhang #endif 55e16c5dd5SJohannes Thumshirn #ifdef CONFIG_S390 56*db1ea668STiezhu Yang pr_info("<%s> p->addr, 0x%p, ip = 0x%lx, flags = 0x%lx\n", 57e16c5dd5SJohannes Thumshirn p->symbol_name, p->addr, regs->psw.addr, regs->flags); 58e16c5dd5SJohannes Thumshirn #endif 59804defeaSAnanth N Mavinakayanahalli 60804defeaSAnanth N Mavinakayanahalli /* A dump_stack() here will give a stack backtrace */ 61804defeaSAnanth N Mavinakayanahalli return 0; 62804defeaSAnanth N Mavinakayanahalli } 63804defeaSAnanth N Mavinakayanahalli 64804defeaSAnanth N Mavinakayanahalli /* kprobe post_handler: called after the probed instruction is executed */ 65d85eaa94SMasami Hiramatsu static void __kprobes handler_post(struct kprobe *p, struct pt_regs *regs, 66804defeaSAnanth N Mavinakayanahalli unsigned long flags) 67804defeaSAnanth N Mavinakayanahalli { 68804defeaSAnanth N Mavinakayanahalli #ifdef CONFIG_X86 69*db1ea668STiezhu Yang pr_info("<%s> p->addr = 0x%p, flags = 0x%lx\n", 70ea9b5013SHuang Shijie p->symbol_name, p->addr, regs->flags); 71804defeaSAnanth N Mavinakayanahalli #endif 72804defeaSAnanth N Mavinakayanahalli #ifdef CONFIG_PPC 73*db1ea668STiezhu Yang pr_info("<%s> p->addr = 0x%p, msr = 0x%lx\n", 74ea9b5013SHuang Shijie p->symbol_name, p->addr, regs->msr); 75804defeaSAnanth N Mavinakayanahalli #endif 768a149237SDavid Daney #ifdef CONFIG_MIPS 77*db1ea668STiezhu Yang pr_info("<%s> p->addr = 0x%p, status = 0x%lx\n", 78ea9b5013SHuang Shijie p->symbol_name, p->addr, regs->cp0_status); 798a149237SDavid Daney #endif 80af78cedeSSandeepa Prabhu #ifdef CONFIG_ARM64 81*db1ea668STiezhu Yang pr_info("<%s> p->addr = 0x%p, pstate = 0x%lx\n", 82af78cedeSSandeepa Prabhu p->symbol_name, p->addr, (long)regs->pstate); 83af78cedeSSandeepa Prabhu #endif 846970613bSMarc Koderer #ifdef CONFIG_ARM 85*db1ea668STiezhu Yang pr_info("<%s> p->addr = 0x%p, cpsr = 0x%lx\n", 866970613bSMarc Koderer p->symbol_name, p->addr, (long)regs->ARM_cpsr); 876970613bSMarc Koderer #endif 888a07ac39SJisheng Zhang #ifdef CONFIG_RISCV 89*db1ea668STiezhu Yang pr_info("<%s> p->addr = 0x%p, status = 0x%lx\n", 908a07ac39SJisheng Zhang p->symbol_name, p->addr, regs->status); 918a07ac39SJisheng Zhang #endif 92e16c5dd5SJohannes Thumshirn #ifdef CONFIG_S390 93*db1ea668STiezhu Yang pr_info("<%s> p->addr, 0x%p, flags = 0x%lx\n", 94e16c5dd5SJohannes Thumshirn p->symbol_name, p->addr, regs->flags); 95e16c5dd5SJohannes Thumshirn #endif 96804defeaSAnanth N Mavinakayanahalli } 97804defeaSAnanth N Mavinakayanahalli 98804defeaSAnanth N Mavinakayanahalli /* 99804defeaSAnanth N Mavinakayanahalli * fault_handler: this is called if an exception is generated for any 100804defeaSAnanth N Mavinakayanahalli * instruction within the pre- or post-handler, or when Kprobes 101804defeaSAnanth N Mavinakayanahalli * single-steps the probed instruction. 102804defeaSAnanth N Mavinakayanahalli */ 103804defeaSAnanth N Mavinakayanahalli static int handler_fault(struct kprobe *p, struct pt_regs *regs, int trapnr) 104804defeaSAnanth N Mavinakayanahalli { 105*db1ea668STiezhu Yang pr_info("p->addr = 0x%p, trap #%d\n", p->addr, trapnr); 106804defeaSAnanth N Mavinakayanahalli /* Return 0 because we don't handle the fault. */ 107804defeaSAnanth N Mavinakayanahalli return 0; 108804defeaSAnanth N Mavinakayanahalli } 109d85eaa94SMasami Hiramatsu /* NOKPROBE_SYMBOL() is also available */ 110d85eaa94SMasami Hiramatsu NOKPROBE_SYMBOL(handler_fault); 111804defeaSAnanth N Mavinakayanahalli 112804defeaSAnanth N Mavinakayanahalli static int __init kprobe_init(void) 113804defeaSAnanth N Mavinakayanahalli { 114804defeaSAnanth N Mavinakayanahalli int ret; 115804defeaSAnanth N Mavinakayanahalli kp.pre_handler = handler_pre; 116804defeaSAnanth N Mavinakayanahalli kp.post_handler = handler_post; 117804defeaSAnanth N Mavinakayanahalli kp.fault_handler = handler_fault; 118804defeaSAnanth N Mavinakayanahalli 119804defeaSAnanth N Mavinakayanahalli ret = register_kprobe(&kp); 120804defeaSAnanth N Mavinakayanahalli if (ret < 0) { 121e708c148SHuang Shijie pr_err("register_kprobe failed, returned %d\n", ret); 122804defeaSAnanth N Mavinakayanahalli return ret; 123804defeaSAnanth N Mavinakayanahalli } 124e708c148SHuang Shijie pr_info("Planted kprobe at %p\n", kp.addr); 125804defeaSAnanth N Mavinakayanahalli return 0; 126804defeaSAnanth N Mavinakayanahalli } 127804defeaSAnanth N Mavinakayanahalli 128804defeaSAnanth N Mavinakayanahalli static void __exit kprobe_exit(void) 129804defeaSAnanth N Mavinakayanahalli { 130804defeaSAnanth N Mavinakayanahalli unregister_kprobe(&kp); 131e708c148SHuang Shijie pr_info("kprobe at %p unregistered\n", kp.addr); 132804defeaSAnanth N Mavinakayanahalli } 133804defeaSAnanth N Mavinakayanahalli 134804defeaSAnanth N Mavinakayanahalli module_init(kprobe_init) 135804defeaSAnanth N Mavinakayanahalli module_exit(kprobe_exit) 136804defeaSAnanth N Mavinakayanahalli MODULE_LICENSE("GPL"); 137