1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Here's a sample kernel module showing the use of fprobe to dump a 4 * stack trace and function entry/exit when kernel_clone() is called. 5 * 6 * For more information on theory of operation of fprobe, see 7 * Documentation/trace/fprobe.rst 8 * 9 * You will see the trace data in /sys/kernel/tracing/trace 10 * whenever kernel_clone() is invoked to create a new process. 11 */ 12 13 #define pr_fmt(fmt) "%s: " fmt, __func__ 14 15 #include <linux/kernel.h> 16 #include <linux/module.h> 17 #include <linux/fprobe.h> 18 #include <linux/slab.h> 19 #include <linux/trace_printk.h> 20 21 #define MAX_SYMBOL_LEN 4096 22 static struct fprobe sample_probe; 23 static unsigned long nhit; 24 25 static char symbol[MAX_SYMBOL_LEN] = "kernel_clone"; 26 module_param_string(symbol, symbol, sizeof(symbol), 0644); 27 MODULE_PARM_DESC(symbol, "Probed symbol(s), given by comma separated symbols or a wildcard pattern."); 28 29 static char nosymbol[MAX_SYMBOL_LEN] = ""; 30 module_param_string(nosymbol, nosymbol, sizeof(nosymbol), 0644); 31 MODULE_PARM_DESC(nosymbol, "Not-probed symbols, given by a wildcard pattern."); 32 33 static bool stackdump = true; 34 module_param(stackdump, bool, 0644); 35 MODULE_PARM_DESC(stackdump, "Enable stackdump."); 36 37 static int sample_entry_handler(struct fprobe *fp, unsigned long ip, 38 unsigned long ret_ip, 39 struct ftrace_regs *fregs, void *data) 40 { 41 trace_printk("Enter <%pS> ip = 0x%p\n", (void *)ip, (void *)ip); 42 nhit++; 43 if (stackdump) 44 trace_dump_stack(0); 45 return 0; 46 } 47 48 static void sample_exit_handler(struct fprobe *fp, unsigned long ip, 49 unsigned long ret_ip, struct ftrace_regs *regs, 50 void *data) 51 { 52 unsigned long rip = ret_ip; 53 54 trace_printk("Return from <%pS> ip = 0x%p to rip = 0x%p (%pS)\n", 55 (void *)ip, (void *)ip, (void *)rip, (void *)rip); 56 nhit++; 57 if (stackdump) 58 trace_dump_stack(0); 59 } 60 61 static int __init fprobe_init(void) 62 { 63 char *p, *symbuf = NULL; 64 const char **syms; 65 int ret, count, i; 66 67 sample_probe.entry_handler = sample_entry_handler; 68 sample_probe.exit_handler = sample_exit_handler; 69 70 if (strchr(symbol, '*')) { 71 /* filter based fprobe */ 72 ret = register_fprobe(&sample_probe, symbol, 73 nosymbol[0] == '\0' ? NULL : nosymbol); 74 goto out; 75 } else if (!strchr(symbol, ',')) { 76 symbuf = symbol; 77 ret = register_fprobe_syms(&sample_probe, (const char **)&symbuf, 1); 78 goto out; 79 } 80 81 /* Comma separated symbols */ 82 symbuf = kstrdup(symbol, GFP_KERNEL); 83 if (!symbuf) 84 return -ENOMEM; 85 p = symbuf; 86 count = 1; 87 while ((p = strchr(++p, ',')) != NULL) 88 count++; 89 90 pr_info("%d symbols found\n", count); 91 92 syms = kcalloc(count, sizeof(char *), GFP_KERNEL); 93 if (!syms) { 94 kfree(symbuf); 95 return -ENOMEM; 96 } 97 98 p = symbuf; 99 for (i = 0; i < count; i++) 100 syms[i] = strsep(&p, ","); 101 102 ret = register_fprobe_syms(&sample_probe, syms, count); 103 kfree(syms); 104 kfree(symbuf); 105 out: 106 if (ret < 0) 107 pr_err("register_fprobe failed, returned %d\n", ret); 108 else 109 pr_info("Planted fprobe at %s\n", symbol); 110 111 return ret; 112 } 113 114 static void __exit fprobe_exit(void) 115 { 116 unregister_fprobe(&sample_probe); 117 118 pr_info("fprobe at %s unregistered. %ld times hit, %ld times missed\n", 119 symbol, nhit, sample_probe.nmissed); 120 } 121 122 module_init(fprobe_init) 123 module_exit(fprobe_exit) 124 MODULE_DESCRIPTION("sample kernel module showing the use of fprobe"); 125 MODULE_LICENSE("GPL"); 126