|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | +/* |
| 3 | + * fprobe - Simple ftrace probe wrapper for function entry. |
| 4 | + */ |
| 5 | +#define pr_fmt(fmt) "fprobe: " fmt |
| 6 | + |
| 7 | +#include <linux/err.h> |
| 8 | +#include <linux/fprobe.h> |
| 9 | +#include <linux/kallsyms.h> |
| 10 | +#include <linux/kprobes.h> |
| 11 | +#include <linux/slab.h> |
| 12 | +#include <linux/sort.h> |
| 13 | + |
| 14 | +static void fprobe_handler(unsigned long ip, unsigned long parent_ip, |
| 15 | + struct ftrace_ops *ops, struct ftrace_regs *fregs) |
| 16 | +{ |
| 17 | + struct fprobe *fp; |
| 18 | + int bit; |
| 19 | + |
| 20 | + fp = container_of(ops, struct fprobe, ops); |
| 21 | + if (fprobe_disabled(fp)) |
| 22 | + return; |
| 23 | + |
| 24 | + bit = ftrace_test_recursion_trylock(ip, parent_ip); |
| 25 | + if (bit < 0) { |
| 26 | + fp->nmissed++; |
| 27 | + return; |
| 28 | + } |
| 29 | + |
| 30 | + if (fp->entry_handler) |
| 31 | + fp->entry_handler(fp, ip, ftrace_get_regs(fregs)); |
| 32 | + |
| 33 | + ftrace_test_recursion_unlock(bit); |
| 34 | +} |
| 35 | +NOKPROBE_SYMBOL(fprobe_handler); |
| 36 | + |
| 37 | +/* Convert ftrace location address from symbols */ |
| 38 | +static unsigned long *get_ftrace_locations(const char **syms, int num) |
| 39 | +{ |
| 40 | + unsigned long addr, size; |
| 41 | + unsigned long *addrs; |
| 42 | + int i; |
| 43 | + |
| 44 | + /* Convert symbols to symbol address */ |
| 45 | + addrs = kcalloc(num, sizeof(*addrs), GFP_KERNEL); |
| 46 | + if (!addrs) |
| 47 | + return ERR_PTR(-ENOMEM); |
| 48 | + |
| 49 | + for (i = 0; i < num; i++) { |
| 50 | + addr = kallsyms_lookup_name(syms[i]); |
| 51 | + if (!addr) /* Maybe wrong symbol */ |
| 52 | + goto error; |
| 53 | + |
| 54 | + /* Convert symbol address to ftrace location. */ |
| 55 | + if (!kallsyms_lookup_size_offset(addr, &size, NULL) || !size) |
| 56 | + goto error; |
| 57 | + |
| 58 | + addr = ftrace_location_range(addr, addr + size - 1); |
| 59 | + if (!addr) /* No dynamic ftrace there. */ |
| 60 | + goto error; |
| 61 | + |
| 62 | + addrs[i] = addr; |
| 63 | + } |
| 64 | + |
| 65 | + return addrs; |
| 66 | + |
| 67 | +error: |
| 68 | + kfree(addrs); |
| 69 | + |
| 70 | + return ERR_PTR(-ENOENT); |
| 71 | +} |
| 72 | + |
| 73 | +static void fprobe_init(struct fprobe *fp) |
| 74 | +{ |
| 75 | + fp->nmissed = 0; |
| 76 | + fp->ops.func = fprobe_handler; |
| 77 | + fp->ops.flags |= FTRACE_OPS_FL_SAVE_REGS; |
| 78 | +} |
| 79 | + |
| 80 | +/** |
| 81 | + * register_fprobe() - Register fprobe to ftrace by pattern. |
| 82 | + * @fp: A fprobe data structure to be registered. |
| 83 | + * @filter: A wildcard pattern of probed symbols. |
| 84 | + * @notfilter: A wildcard pattern of NOT probed symbols. |
| 85 | + * |
| 86 | + * Register @fp to ftrace for enabling the probe on the symbols matched to @filter. |
| 87 | + * If @notfilter is not NULL, the symbols matched the @notfilter are not probed. |
| 88 | + * |
| 89 | + * Return 0 if @fp is registered successfully, -errno if not. |
| 90 | + */ |
| 91 | +int register_fprobe(struct fprobe *fp, const char *filter, const char *notfilter) |
| 92 | +{ |
| 93 | + unsigned char *str; |
| 94 | + int ret, len; |
| 95 | + |
| 96 | + if (!fp || !filter) |
| 97 | + return -EINVAL; |
| 98 | + |
| 99 | + fprobe_init(fp); |
| 100 | + |
| 101 | + len = strlen(filter); |
| 102 | + str = kstrdup(filter, GFP_KERNEL); |
| 103 | + ret = ftrace_set_filter(&fp->ops, str, len, 0); |
| 104 | + kfree(str); |
| 105 | + if (ret) |
| 106 | + return ret; |
| 107 | + |
| 108 | + if (notfilter) { |
| 109 | + len = strlen(notfilter); |
| 110 | + str = kstrdup(notfilter, GFP_KERNEL); |
| 111 | + ret = ftrace_set_notrace(&fp->ops, str, len, 0); |
| 112 | + kfree(str); |
| 113 | + if (ret) |
| 114 | + goto out; |
| 115 | + } |
| 116 | + |
| 117 | + ret = register_ftrace_function(&fp->ops); |
| 118 | +out: |
| 119 | + if (ret) |
| 120 | + ftrace_free_filter(&fp->ops); |
| 121 | + return ret; |
| 122 | +} |
| 123 | +EXPORT_SYMBOL_GPL(register_fprobe); |
| 124 | + |
| 125 | +/** |
| 126 | + * register_fprobe_ips() - Register fprobe to ftrace by address. |
| 127 | + * @fp: A fprobe data structure to be registered. |
| 128 | + * @addrs: An array of target ftrace location addresses. |
| 129 | + * @num: The number of entries of @addrs. |
| 130 | + * |
| 131 | + * Register @fp to ftrace for enabling the probe on the address given by @addrs. |
| 132 | + * The @addrs must be the addresses of ftrace location address, which may be |
| 133 | + * the symbol address + arch-dependent offset. |
| 134 | + * If you unsure what this mean, please use other registration functions. |
| 135 | + * |
| 136 | + * Return 0 if @fp is registered successfully, -errno if not. |
| 137 | + */ |
| 138 | +int register_fprobe_ips(struct fprobe *fp, unsigned long *addrs, int num) |
| 139 | +{ |
| 140 | + int ret; |
| 141 | + |
| 142 | + if (!fp || !addrs || num <= 0) |
| 143 | + return -EINVAL; |
| 144 | + |
| 145 | + fprobe_init(fp); |
| 146 | + |
| 147 | + ret = ftrace_set_filter_ips(&fp->ops, addrs, num, 0, 0); |
| 148 | + if (!ret) |
| 149 | + ret = register_ftrace_function(&fp->ops); |
| 150 | + |
| 151 | + if (ret) |
| 152 | + ftrace_free_filter(&fp->ops); |
| 153 | + |
| 154 | + return ret; |
| 155 | +} |
| 156 | +EXPORT_SYMBOL_GPL(register_fprobe_ips); |
| 157 | + |
| 158 | +/** |
| 159 | + * register_fprobe_syms() - Register fprobe to ftrace by symbols. |
| 160 | + * @fp: A fprobe data structure to be registered. |
| 161 | + * @syms: An array of target symbols. |
| 162 | + * @num: The number of entries of @syms. |
| 163 | + * |
| 164 | + * Register @fp to the symbols given by @syms array. This will be useful if |
| 165 | + * you are sure the symbols exist in the kernel. |
| 166 | + * |
| 167 | + * Return 0 if @fp is registered successfully, -errno if not. |
| 168 | + */ |
| 169 | +int register_fprobe_syms(struct fprobe *fp, const char **syms, int num) |
| 170 | +{ |
| 171 | + unsigned long *addrs; |
| 172 | + int ret; |
| 173 | + |
| 174 | + if (!fp || !syms || num <= 0) |
| 175 | + return -EINVAL; |
| 176 | + |
| 177 | + addrs = get_ftrace_locations(syms, num); |
| 178 | + if (IS_ERR(addrs)) |
| 179 | + return PTR_ERR(addrs); |
| 180 | + |
| 181 | + ret = register_fprobe_ips(fp, addrs, num); |
| 182 | + |
| 183 | + kfree(addrs); |
| 184 | + |
| 185 | + return ret; |
| 186 | +} |
| 187 | +EXPORT_SYMBOL_GPL(register_fprobe_syms); |
| 188 | + |
| 189 | +/** |
| 190 | + * unregister_fprobe() - Unregister fprobe from ftrace |
| 191 | + * @fp: A fprobe data structure to be unregistered. |
| 192 | + * |
| 193 | + * Unregister fprobe (and remove ftrace hooks from the function entries). |
| 194 | + * |
| 195 | + * Return 0 if @fp is unregistered successfully, -errno if not. |
| 196 | + */ |
| 197 | +int unregister_fprobe(struct fprobe *fp) |
| 198 | +{ |
| 199 | + int ret; |
| 200 | + |
| 201 | + if (!fp || fp->ops.func != fprobe_handler) |
| 202 | + return -EINVAL; |
| 203 | + |
| 204 | + ret = unregister_ftrace_function(&fp->ops); |
| 205 | + |
| 206 | + if (!ret) |
| 207 | + ftrace_free_filter(&fp->ops); |
| 208 | + |
| 209 | + return ret; |
| 210 | +} |
| 211 | +EXPORT_SYMBOL_GPL(unregister_fprobe); |
0 commit comments