^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * NOTE: This example is works on x86 and powerpc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Here's a sample kernel module showing the use of kprobes to dump a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * stack trace and selected registers when kernel_clone() is called.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * For more information on theory of operation of kprobes, see
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Documentation/trace/kprobes.rst
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * You will see the trace data in /var/log/messages and on the console
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * whenever kernel_clone() is invoked to create a new process.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/kprobes.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #define MAX_SYMBOL_LEN 64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) static char symbol[MAX_SYMBOL_LEN] = "kernel_clone";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) module_param_string(symbol, symbol, sizeof(symbol), 0644);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) /* For each probe you need to allocate a kprobe structure */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) static struct kprobe kp = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) .symbol_name = symbol,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) /* kprobe pre_handler: called just before the probed instruction is executed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) static int __kprobes handler_pre(struct kprobe *p, struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #ifdef CONFIG_X86
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) pr_info("<%s> pre_handler: p->addr = 0x%p, ip = %lx, flags = 0x%lx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) p->symbol_name, p->addr, regs->ip, regs->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #ifdef CONFIG_PPC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) pr_info("<%s> pre_handler: p->addr = 0x%p, nip = 0x%lx, msr = 0x%lx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) p->symbol_name, p->addr, regs->nip, regs->msr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) #ifdef CONFIG_MIPS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) pr_info("<%s> pre_handler: p->addr = 0x%p, epc = 0x%lx, status = 0x%lx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) p->symbol_name, p->addr, regs->cp0_epc, regs->cp0_status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) #ifdef CONFIG_ARM64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) pr_info("<%s> pre_handler: p->addr = 0x%p, pc = 0x%lx,"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) " pstate = 0x%lx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) p->symbol_name, p->addr, (long)regs->pc, (long)regs->pstate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) #ifdef CONFIG_S390
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) pr_info("<%s> pre_handler: p->addr, 0x%p, ip = 0x%lx, flags = 0x%lx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) p->symbol_name, p->addr, regs->psw.addr, regs->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) /* A dump_stack() here will give a stack backtrace */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) /* kprobe post_handler: called after the probed instruction is executed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) static void __kprobes handler_post(struct kprobe *p, struct pt_regs *regs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) unsigned long flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) #ifdef CONFIG_X86
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) pr_info("<%s> post_handler: p->addr = 0x%p, flags = 0x%lx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) p->symbol_name, p->addr, regs->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) #ifdef CONFIG_PPC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) pr_info("<%s> post_handler: p->addr = 0x%p, msr = 0x%lx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) p->symbol_name, p->addr, regs->msr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) #ifdef CONFIG_MIPS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) pr_info("<%s> post_handler: p->addr = 0x%p, status = 0x%lx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) p->symbol_name, p->addr, regs->cp0_status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) #ifdef CONFIG_ARM64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) pr_info("<%s> post_handler: p->addr = 0x%p, pstate = 0x%lx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) p->symbol_name, p->addr, (long)regs->pstate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) #ifdef CONFIG_S390
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) pr_info("<%s> pre_handler: p->addr, 0x%p, flags = 0x%lx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) p->symbol_name, p->addr, regs->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) * fault_handler: this is called if an exception is generated for any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) * instruction within the pre- or post-handler, or when Kprobes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) * single-steps the probed instruction.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) static int handler_fault(struct kprobe *p, struct pt_regs *regs, int trapnr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) pr_info("fault_handler: p->addr = 0x%p, trap #%dn", p->addr, trapnr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) /* Return 0 because we don't handle the fault. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) /* NOKPROBE_SYMBOL() is also available */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) NOKPROBE_SYMBOL(handler_fault);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) static int __init kprobe_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) kp.pre_handler = handler_pre;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) kp.post_handler = handler_post;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) kp.fault_handler = handler_fault;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) ret = register_kprobe(&kp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) pr_err("register_kprobe failed, returned %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) pr_info("Planted kprobe at %p\n", kp.addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) static void __exit kprobe_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) unregister_kprobe(&kp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) pr_info("kprobe at %p unregistered\n", kp.addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) module_init(kprobe_init)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) module_exit(kprobe_exit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) MODULE_LICENSE("GPL");