0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #define pr_fmt(fmt) "%s: " fmt, __func__
0014
0015 #include <linux/kernel.h>
0016 #include <linux/module.h>
0017 #include <linux/fprobe.h>
0018 #include <linux/sched/debug.h>
0019 #include <linux/slab.h>
0020
0021 #define BACKTRACE_DEPTH 16
0022 #define MAX_SYMBOL_LEN 4096
0023 static struct fprobe sample_probe;
0024 static unsigned long nhit;
0025
0026 static char symbol[MAX_SYMBOL_LEN] = "kernel_clone";
0027 module_param_string(symbol, symbol, sizeof(symbol), 0644);
0028 MODULE_PARM_DESC(symbol, "Probed symbol(s), given by comma separated symbols or a wildcard pattern.");
0029
0030 static char nosymbol[MAX_SYMBOL_LEN] = "";
0031 module_param_string(nosymbol, nosymbol, sizeof(nosymbol), 0644);
0032 MODULE_PARM_DESC(nosymbol, "Not-probed symbols, given by a wildcard pattern.");
0033
0034 static bool stackdump = true;
0035 module_param(stackdump, bool, 0644);
0036 MODULE_PARM_DESC(stackdump, "Enable stackdump.");
0037
0038 static bool use_trace = false;
0039 module_param(use_trace, bool, 0644);
0040 MODULE_PARM_DESC(use_trace, "Use trace_printk instead of printk. This is only for debugging.");
0041
0042 static void show_backtrace(void)
0043 {
0044 unsigned long stacks[BACKTRACE_DEPTH];
0045 unsigned int len;
0046
0047 len = stack_trace_save(stacks, BACKTRACE_DEPTH, 2);
0048 stack_trace_print(stacks, len, 24);
0049 }
0050
0051 static void sample_entry_handler(struct fprobe *fp, unsigned long ip, struct pt_regs *regs)
0052 {
0053 if (use_trace)
0054
0055
0056
0057
0058 trace_printk("Enter <%pS> ip = 0x%p\n", (void *)ip, (void *)ip);
0059 else
0060 pr_info("Enter <%pS> ip = 0x%p\n", (void *)ip, (void *)ip);
0061 nhit++;
0062 if (stackdump)
0063 show_backtrace();
0064 }
0065
0066 static void sample_exit_handler(struct fprobe *fp, unsigned long ip, struct pt_regs *regs)
0067 {
0068 unsigned long rip = instruction_pointer(regs);
0069
0070 if (use_trace)
0071
0072
0073
0074
0075 trace_printk("Return from <%pS> ip = 0x%p to rip = 0x%p (%pS)\n",
0076 (void *)ip, (void *)ip, (void *)rip, (void *)rip);
0077 else
0078 pr_info("Return from <%pS> ip = 0x%p to rip = 0x%p (%pS)\n",
0079 (void *)ip, (void *)ip, (void *)rip, (void *)rip);
0080 nhit++;
0081 if (stackdump)
0082 show_backtrace();
0083 }
0084
0085 static int __init fprobe_init(void)
0086 {
0087 char *p, *symbuf = NULL;
0088 const char **syms;
0089 int ret, count, i;
0090
0091 sample_probe.entry_handler = sample_entry_handler;
0092 sample_probe.exit_handler = sample_exit_handler;
0093
0094 if (strchr(symbol, '*')) {
0095
0096 ret = register_fprobe(&sample_probe, symbol,
0097 nosymbol[0] == '\0' ? NULL : nosymbol);
0098 goto out;
0099 } else if (!strchr(symbol, ',')) {
0100 symbuf = symbol;
0101 ret = register_fprobe_syms(&sample_probe, (const char **)&symbuf, 1);
0102 goto out;
0103 }
0104
0105
0106 symbuf = kstrdup(symbol, GFP_KERNEL);
0107 if (!symbuf)
0108 return -ENOMEM;
0109 p = symbuf;
0110 count = 1;
0111 while ((p = strchr(++p, ',')) != NULL)
0112 count++;
0113
0114 pr_info("%d symbols found\n", count);
0115
0116 syms = kcalloc(count, sizeof(char *), GFP_KERNEL);
0117 if (!syms) {
0118 kfree(symbuf);
0119 return -ENOMEM;
0120 }
0121
0122 p = symbuf;
0123 for (i = 0; i < count; i++)
0124 syms[i] = strsep(&p, ",");
0125
0126 ret = register_fprobe_syms(&sample_probe, syms, count);
0127 kfree(syms);
0128 kfree(symbuf);
0129 out:
0130 if (ret < 0)
0131 pr_err("register_fprobe failed, returned %d\n", ret);
0132 else
0133 pr_info("Planted fprobe at %s\n", symbol);
0134
0135 return ret;
0136 }
0137
0138 static void __exit fprobe_exit(void)
0139 {
0140 unregister_fprobe(&sample_probe);
0141
0142 pr_info("fprobe at %s unregistered. %ld times hit, %ld times missed\n",
0143 symbol, nhit, sample_probe.nmissed);
0144 }
0145
0146 module_init(fprobe_init)
0147 module_exit(fprobe_exit)
0148 MODULE_LICENSE("GPL");