Steven Rostedt (VMware) | bcea3f9 | 2018-08-16 11:23:53 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 2 | /* |
Masami Hiramatsu | 77b44d1 | 2009-11-03 19:12:47 -0500 | [diff] [blame] | 3 | * Kprobes-based tracing events |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 4 | * |
| 5 | * Created by Masami Hiramatsu <mhiramat@redhat.com> |
| 6 | * |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 7 | */ |
Masami Hiramatsu | 7257634 | 2017-02-07 20:21:28 +0900 | [diff] [blame] | 8 | #define pr_fmt(fmt) "trace_kprobe: " fmt |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 9 | |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/uaccess.h> |
Ingo Molnar | b2d0910 | 2017-02-04 01:27:20 +0100 | [diff] [blame] | 12 | #include <linux/rculist.h> |
Masami Hiramatsu | 540adea | 2018-01-13 02:55:03 +0900 | [diff] [blame] | 13 | #include <linux/error-injection.h> |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 14 | |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 15 | #include "trace_dynevent.h" |
Francis Deslauriers | d899926 | 2018-07-30 19:20:42 +0900 | [diff] [blame] | 16 | #include "trace_kprobe_selftest.h" |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 17 | #include "trace_probe.h" |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 18 | #include "trace_probe_tmpl.h" |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 19 | |
Masami Hiramatsu | f52487e | 2009-09-10 19:53:53 -0400 | [diff] [blame] | 20 | #define KPROBE_EVENT_SYSTEM "kprobes" |
Alban Crequy | 696ced4 | 2017-04-03 12:36:22 +0200 | [diff] [blame] | 21 | #define KRETPROBE_MAXACTIVE_MAX 4096 |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 22 | |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 23 | static int trace_kprobe_create(int argc, const char **argv); |
| 24 | static int trace_kprobe_show(struct seq_file *m, struct dyn_event *ev); |
| 25 | static int trace_kprobe_release(struct dyn_event *ev); |
| 26 | static bool trace_kprobe_is_busy(struct dyn_event *ev); |
| 27 | static bool trace_kprobe_match(const char *system, const char *event, |
| 28 | struct dyn_event *ev); |
| 29 | |
| 30 | static struct dyn_event_operations trace_kprobe_ops = { |
| 31 | .create = trace_kprobe_create, |
| 32 | .show = trace_kprobe_show, |
| 33 | .is_busy = trace_kprobe_is_busy, |
| 34 | .free = trace_kprobe_release, |
| 35 | .match = trace_kprobe_match, |
| 36 | }; |
| 37 | |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 38 | /** |
Masami Hiramatsu | 77b44d1 | 2009-11-03 19:12:47 -0500 | [diff] [blame] | 39 | * Kprobe event core functions |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 40 | */ |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 41 | struct trace_kprobe { |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 42 | struct dyn_event devent; |
Masami Hiramatsu | 4a846b4 | 2009-09-11 05:31:21 +0200 | [diff] [blame] | 43 | struct kretprobe rp; /* Use rp.kp for kprobe use */ |
Martin KaFai Lau | a7636d9 | 2016-02-03 12:28:28 -0800 | [diff] [blame] | 44 | unsigned long __percpu *nhit; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 45 | const char *symbol; /* symbol name */ |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 46 | struct trace_probe tp; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 47 | }; |
| 48 | |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 49 | static bool is_trace_kprobe(struct dyn_event *ev) |
| 50 | { |
| 51 | return ev->ops == &trace_kprobe_ops; |
| 52 | } |
| 53 | |
| 54 | static struct trace_kprobe *to_trace_kprobe(struct dyn_event *ev) |
| 55 | { |
| 56 | return container_of(ev, struct trace_kprobe, devent); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * for_each_trace_kprobe - iterate over the trace_kprobe list |
| 61 | * @pos: the struct trace_kprobe * for each entry |
| 62 | * @dpos: the struct dyn_event * to use as a loop cursor |
| 63 | */ |
| 64 | #define for_each_trace_kprobe(pos, dpos) \ |
| 65 | for_each_dyn_event(dpos) \ |
| 66 | if (is_trace_kprobe(dpos) && (pos = to_trace_kprobe(dpos))) |
| 67 | |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 68 | #define SIZEOF_TRACE_KPROBE(n) \ |
| 69 | (offsetof(struct trace_kprobe, tp.args) + \ |
Masami Hiramatsu | eca0d91 | 2009-09-10 19:53:38 -0400 | [diff] [blame] | 70 | (sizeof(struct probe_arg) * (n))) |
Masami Hiramatsu | a82378d | 2009-08-13 16:35:18 -0400 | [diff] [blame] | 71 | |
Masami Hiramatsu | 3da0f18 | 2014-04-17 17:18:28 +0900 | [diff] [blame] | 72 | static nokprobe_inline bool trace_kprobe_is_return(struct trace_kprobe *tk) |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 73 | { |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 74 | return tk->rp.handler != NULL; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 75 | } |
| 76 | |
Masami Hiramatsu | 3da0f18 | 2014-04-17 17:18:28 +0900 | [diff] [blame] | 77 | static nokprobe_inline const char *trace_kprobe_symbol(struct trace_kprobe *tk) |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 78 | { |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 79 | return tk->symbol ? tk->symbol : "unknown"; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 80 | } |
| 81 | |
Masami Hiramatsu | 3da0f18 | 2014-04-17 17:18:28 +0900 | [diff] [blame] | 82 | static nokprobe_inline unsigned long trace_kprobe_offset(struct trace_kprobe *tk) |
Masami Hiramatsu | 6142431 | 2011-06-27 16:26:56 +0900 | [diff] [blame] | 83 | { |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 84 | return tk->rp.kp.offset; |
Masami Hiramatsu | 6142431 | 2011-06-27 16:26:56 +0900 | [diff] [blame] | 85 | } |
| 86 | |
Masami Hiramatsu | 3da0f18 | 2014-04-17 17:18:28 +0900 | [diff] [blame] | 87 | static nokprobe_inline bool trace_kprobe_has_gone(struct trace_kprobe *tk) |
Masami Hiramatsu | 6142431 | 2011-06-27 16:26:56 +0900 | [diff] [blame] | 88 | { |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 89 | return !!(kprobe_gone(&tk->rp.kp)); |
Masami Hiramatsu | 6142431 | 2011-06-27 16:26:56 +0900 | [diff] [blame] | 90 | } |
| 91 | |
Masami Hiramatsu | 3da0f18 | 2014-04-17 17:18:28 +0900 | [diff] [blame] | 92 | static nokprobe_inline bool trace_kprobe_within_module(struct trace_kprobe *tk, |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 93 | struct module *mod) |
Masami Hiramatsu | 6142431 | 2011-06-27 16:26:56 +0900 | [diff] [blame] | 94 | { |
| 95 | int len = strlen(mod->name); |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 96 | const char *name = trace_kprobe_symbol(tk); |
Masami Hiramatsu | 6142431 | 2011-06-27 16:26:56 +0900 | [diff] [blame] | 97 | return strncmp(mod->name, name, len) == 0 && name[len] == ':'; |
| 98 | } |
| 99 | |
Masami Hiramatsu | 59158ec | 2018-08-29 01:18:15 +0900 | [diff] [blame] | 100 | static nokprobe_inline bool trace_kprobe_module_exist(struct trace_kprobe *tk) |
Masami Hiramatsu | 6142431 | 2011-06-27 16:26:56 +0900 | [diff] [blame] | 101 | { |
Masami Hiramatsu | 59158ec | 2018-08-29 01:18:15 +0900 | [diff] [blame] | 102 | char *p; |
| 103 | bool ret; |
| 104 | |
| 105 | if (!tk->symbol) |
| 106 | return false; |
| 107 | p = strchr(tk->symbol, ':'); |
| 108 | if (!p) |
| 109 | return true; |
| 110 | *p = '\0'; |
| 111 | mutex_lock(&module_mutex); |
| 112 | ret = !!find_module(tk->symbol); |
| 113 | mutex_unlock(&module_mutex); |
| 114 | *p = ':'; |
| 115 | |
| 116 | return ret; |
Masami Hiramatsu | 6142431 | 2011-06-27 16:26:56 +0900 | [diff] [blame] | 117 | } |
| 118 | |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 119 | static bool trace_kprobe_is_busy(struct dyn_event *ev) |
| 120 | { |
| 121 | struct trace_kprobe *tk = to_trace_kprobe(ev); |
| 122 | |
| 123 | return trace_probe_is_enabled(&tk->tp); |
| 124 | } |
| 125 | |
| 126 | static bool trace_kprobe_match(const char *system, const char *event, |
| 127 | struct dyn_event *ev) |
| 128 | { |
| 129 | struct trace_kprobe *tk = to_trace_kprobe(ev); |
| 130 | |
| 131 | return strcmp(trace_event_name(&tk->tp.call), event) == 0 && |
| 132 | (!system || strcmp(tk->tp.call.class->system, system) == 0); |
| 133 | } |
| 134 | |
Marcin Nowakowski | f18f97a | 2016-12-09 15:19:37 +0100 | [diff] [blame] | 135 | static nokprobe_inline unsigned long trace_kprobe_nhit(struct trace_kprobe *tk) |
| 136 | { |
| 137 | unsigned long nhit = 0; |
| 138 | int cpu; |
| 139 | |
| 140 | for_each_possible_cpu(cpu) |
| 141 | nhit += *per_cpu_ptr(tk->nhit, cpu); |
| 142 | |
| 143 | return nhit; |
| 144 | } |
| 145 | |
Masami Hiramatsu | 6bc6c77 | 2018-08-02 16:50:48 +0900 | [diff] [blame] | 146 | /* Return 0 if it fails to find the symbol address */ |
Masami Hiramatsu | 45408c4 | 2018-07-30 19:20:14 +0900 | [diff] [blame] | 147 | static nokprobe_inline |
| 148 | unsigned long trace_kprobe_address(struct trace_kprobe *tk) |
| 149 | { |
| 150 | unsigned long addr; |
| 151 | |
| 152 | if (tk->symbol) { |
| 153 | addr = (unsigned long) |
| 154 | kallsyms_lookup_name(trace_kprobe_symbol(tk)); |
Masami Hiramatsu | 6bc6c77 | 2018-08-02 16:50:48 +0900 | [diff] [blame] | 155 | if (addr) |
| 156 | addr += tk->rp.kp.offset; |
Masami Hiramatsu | 45408c4 | 2018-07-30 19:20:14 +0900 | [diff] [blame] | 157 | } else { |
| 158 | addr = (unsigned long)tk->rp.kp.addr; |
| 159 | } |
| 160 | return addr; |
| 161 | } |
| 162 | |
Masami Hiramatsu | b4da334 | 2018-01-13 02:54:04 +0900 | [diff] [blame] | 163 | bool trace_kprobe_on_func_entry(struct trace_event_call *call) |
Josef Bacik | 9802d86 | 2017-12-11 11:36:48 -0500 | [diff] [blame] | 164 | { |
| 165 | struct trace_kprobe *tk = (struct trace_kprobe *)call->data; |
Masami Hiramatsu | b4da334 | 2018-01-13 02:54:04 +0900 | [diff] [blame] | 166 | |
| 167 | return kprobe_on_func_entry(tk->rp.kp.addr, |
| 168 | tk->rp.kp.addr ? NULL : tk->rp.kp.symbol_name, |
| 169 | tk->rp.kp.addr ? 0 : tk->rp.kp.offset); |
Josef Bacik | 9802d86 | 2017-12-11 11:36:48 -0500 | [diff] [blame] | 170 | } |
| 171 | |
Masami Hiramatsu | b4da334 | 2018-01-13 02:54:04 +0900 | [diff] [blame] | 172 | bool trace_kprobe_error_injectable(struct trace_event_call *call) |
Josef Bacik | 9802d86 | 2017-12-11 11:36:48 -0500 | [diff] [blame] | 173 | { |
| 174 | struct trace_kprobe *tk = (struct trace_kprobe *)call->data; |
Josef Bacik | 9802d86 | 2017-12-11 11:36:48 -0500 | [diff] [blame] | 175 | |
Masami Hiramatsu | 45408c4 | 2018-07-30 19:20:14 +0900 | [diff] [blame] | 176 | return within_error_injection_list(trace_kprobe_address(tk)); |
Josef Bacik | 9802d86 | 2017-12-11 11:36:48 -0500 | [diff] [blame] | 177 | } |
| 178 | |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 179 | static int register_kprobe_event(struct trace_kprobe *tk); |
| 180 | static int unregister_kprobe_event(struct trace_kprobe *tk); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 181 | |
Masami Hiramatsu | 50d7805 | 2009-09-14 16:49:20 -0400 | [diff] [blame] | 182 | static int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs); |
| 183 | static int kretprobe_dispatcher(struct kretprobe_instance *ri, |
| 184 | struct pt_regs *regs); |
| 185 | |
Masami Hiramatsu | 4a846b4 | 2009-09-11 05:31:21 +0200 | [diff] [blame] | 186 | /* |
| 187 | * Allocate new trace_probe and initialize it (including kprobes). |
| 188 | */ |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 189 | static struct trace_kprobe *alloc_trace_kprobe(const char *group, |
Masami Hiramatsu | f52487e | 2009-09-10 19:53:53 -0400 | [diff] [blame] | 190 | const char *event, |
Masami Hiramatsu | 4a846b4 | 2009-09-11 05:31:21 +0200 | [diff] [blame] | 191 | void *addr, |
| 192 | const char *symbol, |
| 193 | unsigned long offs, |
Alban Crequy | 696ced4 | 2017-04-03 12:36:22 +0200 | [diff] [blame] | 194 | int maxactive, |
Srikar Dronamraju | 3a6b766 | 2012-04-09 14:41:33 +0530 | [diff] [blame] | 195 | int nargs, bool is_return) |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 196 | { |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 197 | struct trace_kprobe *tk; |
Masami Hiramatsu | 6f3cf44 | 2009-12-16 17:24:08 -0500 | [diff] [blame] | 198 | int ret = -ENOMEM; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 199 | |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 200 | tk = kzalloc(SIZEOF_TRACE_KPROBE(nargs), GFP_KERNEL); |
| 201 | if (!tk) |
Masami Hiramatsu | 6f3cf44 | 2009-12-16 17:24:08 -0500 | [diff] [blame] | 202 | return ERR_PTR(ret); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 203 | |
Martin KaFai Lau | a7636d9 | 2016-02-03 12:28:28 -0800 | [diff] [blame] | 204 | tk->nhit = alloc_percpu(unsigned long); |
| 205 | if (!tk->nhit) |
| 206 | goto error; |
| 207 | |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 208 | if (symbol) { |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 209 | tk->symbol = kstrdup(symbol, GFP_KERNEL); |
| 210 | if (!tk->symbol) |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 211 | goto error; |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 212 | tk->rp.kp.symbol_name = tk->symbol; |
| 213 | tk->rp.kp.offset = offs; |
Masami Hiramatsu | 4a846b4 | 2009-09-11 05:31:21 +0200 | [diff] [blame] | 214 | } else |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 215 | tk->rp.kp.addr = addr; |
Masami Hiramatsu | 4a846b4 | 2009-09-11 05:31:21 +0200 | [diff] [blame] | 216 | |
| 217 | if (is_return) |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 218 | tk->rp.handler = kretprobe_dispatcher; |
Masami Hiramatsu | 4a846b4 | 2009-09-11 05:31:21 +0200 | [diff] [blame] | 219 | else |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 220 | tk->rp.kp.pre_handler = kprobe_dispatcher; |
Masami Hiramatsu | 4a846b4 | 2009-09-11 05:31:21 +0200 | [diff] [blame] | 221 | |
Alban Crequy | 696ced4 | 2017-04-03 12:36:22 +0200 | [diff] [blame] | 222 | tk->rp.maxactive = maxactive; |
| 223 | |
Masami Hiramatsu | da34634 | 2010-08-27 20:39:12 +0900 | [diff] [blame] | 224 | if (!event || !is_good_name(event)) { |
Masami Hiramatsu | 6f3cf44 | 2009-12-16 17:24:08 -0500 | [diff] [blame] | 225 | ret = -EINVAL; |
Masami Hiramatsu | 4263565 | 2009-08-13 16:35:26 -0400 | [diff] [blame] | 226 | goto error; |
Masami Hiramatsu | 6f3cf44 | 2009-12-16 17:24:08 -0500 | [diff] [blame] | 227 | } |
| 228 | |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 229 | tk->tp.call.class = &tk->tp.class; |
| 230 | tk->tp.call.name = kstrdup(event, GFP_KERNEL); |
| 231 | if (!tk->tp.call.name) |
Masami Hiramatsu | 4263565 | 2009-08-13 16:35:26 -0400 | [diff] [blame] | 232 | goto error; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 233 | |
Masami Hiramatsu | da34634 | 2010-08-27 20:39:12 +0900 | [diff] [blame] | 234 | if (!group || !is_good_name(group)) { |
Masami Hiramatsu | 6f3cf44 | 2009-12-16 17:24:08 -0500 | [diff] [blame] | 235 | ret = -EINVAL; |
Masami Hiramatsu | f52487e | 2009-09-10 19:53:53 -0400 | [diff] [blame] | 236 | goto error; |
Masami Hiramatsu | 6f3cf44 | 2009-12-16 17:24:08 -0500 | [diff] [blame] | 237 | } |
| 238 | |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 239 | tk->tp.class.system = kstrdup(group, GFP_KERNEL); |
| 240 | if (!tk->tp.class.system) |
Masami Hiramatsu | f52487e | 2009-09-10 19:53:53 -0400 | [diff] [blame] | 241 | goto error; |
| 242 | |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 243 | dyn_event_init(&tk->devent, &trace_kprobe_ops); |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 244 | INIT_LIST_HEAD(&tk->tp.files); |
| 245 | return tk; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 246 | error: |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 247 | kfree(tk->tp.call.name); |
| 248 | kfree(tk->symbol); |
Martin KaFai Lau | a7636d9 | 2016-02-03 12:28:28 -0800 | [diff] [blame] | 249 | free_percpu(tk->nhit); |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 250 | kfree(tk); |
Masami Hiramatsu | 6f3cf44 | 2009-12-16 17:24:08 -0500 | [diff] [blame] | 251 | return ERR_PTR(ret); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 252 | } |
| 253 | |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 254 | static void free_trace_kprobe(struct trace_kprobe *tk) |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 255 | { |
| 256 | int i; |
| 257 | |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 258 | if (!tk) |
| 259 | return; |
| 260 | |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 261 | for (i = 0; i < tk->tp.nr_args; i++) |
| 262 | traceprobe_free_probe_arg(&tk->tp.args[i]); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 263 | |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 264 | kfree(tk->tp.call.class->system); |
| 265 | kfree(tk->tp.call.name); |
| 266 | kfree(tk->symbol); |
Martin KaFai Lau | a7636d9 | 2016-02-03 12:28:28 -0800 | [diff] [blame] | 267 | free_percpu(tk->nhit); |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 268 | kfree(tk); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 269 | } |
| 270 | |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 271 | static struct trace_kprobe *find_trace_kprobe(const char *event, |
| 272 | const char *group) |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 273 | { |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 274 | struct dyn_event *pos; |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 275 | struct trace_kprobe *tk; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 276 | |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 277 | for_each_trace_kprobe(tk, pos) |
Steven Rostedt (Red Hat) | 687fcc4 | 2015-05-13 14:20:14 -0400 | [diff] [blame] | 278 | if (strcmp(trace_event_name(&tk->tp.call), event) == 0 && |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 279 | strcmp(tk->tp.call.class->system, group) == 0) |
| 280 | return tk; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 281 | return NULL; |
| 282 | } |
| 283 | |
Steven Rostedt (VMware) | 87107a2 | 2018-07-26 12:07:32 -0400 | [diff] [blame] | 284 | static inline int __enable_trace_kprobe(struct trace_kprobe *tk) |
| 285 | { |
| 286 | int ret = 0; |
| 287 | |
| 288 | if (trace_probe_is_registered(&tk->tp) && !trace_kprobe_has_gone(tk)) { |
| 289 | if (trace_kprobe_is_return(tk)) |
| 290 | ret = enable_kretprobe(&tk->rp); |
| 291 | else |
| 292 | ret = enable_kprobe(&tk->rp.kp); |
| 293 | } |
| 294 | |
| 295 | return ret; |
| 296 | } |
| 297 | |
Oleg Nesterov | 3fe3d61 | 2013-06-20 19:38:09 +0200 | [diff] [blame] | 298 | /* |
Masami Hiramatsu | 41a7dd4 | 2013-05-09 14:44:49 +0900 | [diff] [blame] | 299 | * Enable trace_probe |
| 300 | * if the file is NULL, enable "perf" handler, or enable "trace" handler. |
| 301 | */ |
| 302 | static int |
Steven Rostedt (Red Hat) | 7f1d2f8 | 2015-05-05 10:09:53 -0400 | [diff] [blame] | 303 | enable_trace_kprobe(struct trace_kprobe *tk, struct trace_event_file *file) |
Masami Hiramatsu | 1538f88 | 2011-06-27 16:26:44 +0900 | [diff] [blame] | 304 | { |
Steven Rostedt (VMware) | 87107a2 | 2018-07-26 12:07:32 -0400 | [diff] [blame] | 305 | struct event_file_link *link; |
Masami Hiramatsu | 1538f88 | 2011-06-27 16:26:44 +0900 | [diff] [blame] | 306 | int ret = 0; |
| 307 | |
Masami Hiramatsu | 41a7dd4 | 2013-05-09 14:44:49 +0900 | [diff] [blame] | 308 | if (file) { |
Oleg Nesterov | b04d52e | 2013-06-20 19:38:14 +0200 | [diff] [blame] | 309 | link = kmalloc(sizeof(*link), GFP_KERNEL); |
| 310 | if (!link) { |
Masami Hiramatsu | 41a7dd4 | 2013-05-09 14:44:49 +0900 | [diff] [blame] | 311 | ret = -ENOMEM; |
Oleg Nesterov | 3fe3d61 | 2013-06-20 19:38:09 +0200 | [diff] [blame] | 312 | goto out; |
Masami Hiramatsu | 41a7dd4 | 2013-05-09 14:44:49 +0900 | [diff] [blame] | 313 | } |
Masami Hiramatsu | 41a7dd4 | 2013-05-09 14:44:49 +0900 | [diff] [blame] | 314 | |
Oleg Nesterov | b04d52e | 2013-06-20 19:38:14 +0200 | [diff] [blame] | 315 | link->file = file; |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 316 | list_add_tail_rcu(&link->list, &tk->tp.files); |
Oleg Nesterov | b04d52e | 2013-06-20 19:38:14 +0200 | [diff] [blame] | 317 | |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 318 | tk->tp.flags |= TP_FLAG_TRACE; |
Steven Rostedt (VMware) | 87107a2 | 2018-07-26 12:07:32 -0400 | [diff] [blame] | 319 | ret = __enable_trace_kprobe(tk); |
| 320 | if (ret) { |
| 321 | list_del_rcu(&link->list); |
Artem Savkov | 57ea2a3 | 2018-07-25 16:20:38 +0200 | [diff] [blame] | 322 | kfree(link); |
| 323 | tk->tp.flags &= ~TP_FLAG_TRACE; |
Artem Savkov | 57ea2a3 | 2018-07-25 16:20:38 +0200 | [diff] [blame] | 324 | } |
Steven Rostedt (VMware) | 87107a2 | 2018-07-26 12:07:32 -0400 | [diff] [blame] | 325 | |
| 326 | } else { |
| 327 | tk->tp.flags |= TP_FLAG_PROFILE; |
| 328 | ret = __enable_trace_kprobe(tk); |
| 329 | if (ret) |
| 330 | tk->tp.flags &= ~TP_FLAG_PROFILE; |
Artem Savkov | 57ea2a3 | 2018-07-25 16:20:38 +0200 | [diff] [blame] | 331 | } |
Oleg Nesterov | 3fe3d61 | 2013-06-20 19:38:09 +0200 | [diff] [blame] | 332 | out: |
Masami Hiramatsu | 1538f88 | 2011-06-27 16:26:44 +0900 | [diff] [blame] | 333 | return ret; |
| 334 | } |
| 335 | |
Masami Hiramatsu | 41a7dd4 | 2013-05-09 14:44:49 +0900 | [diff] [blame] | 336 | /* |
| 337 | * Disable trace_probe |
| 338 | * if the file is NULL, disable "perf" handler, or disable "trace" handler. |
| 339 | */ |
| 340 | static int |
Steven Rostedt (Red Hat) | 7f1d2f8 | 2015-05-05 10:09:53 -0400 | [diff] [blame] | 341 | disable_trace_kprobe(struct trace_kprobe *tk, struct trace_event_file *file) |
Masami Hiramatsu | 41a7dd4 | 2013-05-09 14:44:49 +0900 | [diff] [blame] | 342 | { |
Masami Hiramatsu | a232e27 | 2013-07-09 18:35:26 +0900 | [diff] [blame] | 343 | struct event_file_link *link = NULL; |
| 344 | int wait = 0; |
Masami Hiramatsu | 41a7dd4 | 2013-05-09 14:44:49 +0900 | [diff] [blame] | 345 | int ret = 0; |
| 346 | |
Masami Hiramatsu | 41a7dd4 | 2013-05-09 14:44:49 +0900 | [diff] [blame] | 347 | if (file) { |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 348 | link = find_event_file_link(&tk->tp, file); |
Oleg Nesterov | b04d52e | 2013-06-20 19:38:14 +0200 | [diff] [blame] | 349 | if (!link) { |
Masami Hiramatsu | 41a7dd4 | 2013-05-09 14:44:49 +0900 | [diff] [blame] | 350 | ret = -EINVAL; |
Oleg Nesterov | 3fe3d61 | 2013-06-20 19:38:09 +0200 | [diff] [blame] | 351 | goto out; |
Masami Hiramatsu | 41a7dd4 | 2013-05-09 14:44:49 +0900 | [diff] [blame] | 352 | } |
| 353 | |
Oleg Nesterov | b04d52e | 2013-06-20 19:38:14 +0200 | [diff] [blame] | 354 | list_del_rcu(&link->list); |
Masami Hiramatsu | a232e27 | 2013-07-09 18:35:26 +0900 | [diff] [blame] | 355 | wait = 1; |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 356 | if (!list_empty(&tk->tp.files)) |
Oleg Nesterov | b04d52e | 2013-06-20 19:38:14 +0200 | [diff] [blame] | 357 | goto out; |
| 358 | |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 359 | tk->tp.flags &= ~TP_FLAG_TRACE; |
Masami Hiramatsu | 41a7dd4 | 2013-05-09 14:44:49 +0900 | [diff] [blame] | 360 | } else |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 361 | tk->tp.flags &= ~TP_FLAG_PROFILE; |
Masami Hiramatsu | 41a7dd4 | 2013-05-09 14:44:49 +0900 | [diff] [blame] | 362 | |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 363 | if (!trace_probe_is_enabled(&tk->tp) && trace_probe_is_registered(&tk->tp)) { |
| 364 | if (trace_kprobe_is_return(tk)) |
| 365 | disable_kretprobe(&tk->rp); |
Masami Hiramatsu | 1538f88 | 2011-06-27 16:26:44 +0900 | [diff] [blame] | 366 | else |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 367 | disable_kprobe(&tk->rp.kp); |
Masami Hiramatsu | a232e27 | 2013-07-09 18:35:26 +0900 | [diff] [blame] | 368 | wait = 1; |
Masami Hiramatsu | 1538f88 | 2011-06-27 16:26:44 +0900 | [diff] [blame] | 369 | } |
Song Liu | e12f03d | 2017-12-06 14:45:15 -0800 | [diff] [blame] | 370 | |
| 371 | /* |
| 372 | * if tk is not added to any list, it must be a local trace_kprobe |
| 373 | * created with perf_event_open. We don't need to wait for these |
| 374 | * trace_kprobes |
| 375 | */ |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 376 | if (list_empty(&tk->devent.list)) |
Song Liu | e12f03d | 2017-12-06 14:45:15 -0800 | [diff] [blame] | 377 | wait = 0; |
Oleg Nesterov | 3fe3d61 | 2013-06-20 19:38:09 +0200 | [diff] [blame] | 378 | out: |
Masami Hiramatsu | a232e27 | 2013-07-09 18:35:26 +0900 | [diff] [blame] | 379 | if (wait) { |
| 380 | /* |
| 381 | * Synchronize with kprobe_trace_func/kretprobe_trace_func |
| 382 | * to ensure disabled (all running handlers are finished). |
| 383 | * This is not only for kfree(), but also the caller, |
| 384 | * trace_remove_event_call() supposes it for releasing |
| 385 | * event_call related objects, which will be accessed in |
| 386 | * the kprobe_trace_func/kretprobe_trace_func. |
| 387 | */ |
Paul E. McKenney | 7440172 | 2018-11-06 18:44:52 -0800 | [diff] [blame] | 388 | synchronize_rcu(); |
Masami Hiramatsu | a232e27 | 2013-07-09 18:35:26 +0900 | [diff] [blame] | 389 | kfree(link); /* Ignored if link == NULL */ |
| 390 | } |
| 391 | |
Masami Hiramatsu | 41a7dd4 | 2013-05-09 14:44:49 +0900 | [diff] [blame] | 392 | return ret; |
Masami Hiramatsu | 1538f88 | 2011-06-27 16:26:44 +0900 | [diff] [blame] | 393 | } |
| 394 | |
Masami Hiramatsu | 45408c4 | 2018-07-30 19:20:14 +0900 | [diff] [blame] | 395 | #if defined(CONFIG_KPROBES_ON_FTRACE) && \ |
| 396 | !defined(CONFIG_KPROBE_EVENTS_ON_NOTRACE) |
| 397 | static bool within_notrace_func(struct trace_kprobe *tk) |
| 398 | { |
| 399 | unsigned long offset, size, addr; |
| 400 | |
| 401 | addr = trace_kprobe_address(tk); |
Masami Hiramatsu | 6bc6c77 | 2018-08-02 16:50:48 +0900 | [diff] [blame] | 402 | if (!addr || !kallsyms_lookup_size_offset(addr, &size, &offset)) |
| 403 | return false; |
Masami Hiramatsu | 45408c4 | 2018-07-30 19:20:14 +0900 | [diff] [blame] | 404 | |
Masami Hiramatsu | 9161a86 | 2018-08-21 22:04:57 +0900 | [diff] [blame] | 405 | /* Get the entry address of the target function */ |
| 406 | addr -= offset; |
| 407 | |
| 408 | /* |
| 409 | * Since ftrace_location_range() does inclusive range check, we need |
| 410 | * to subtract 1 byte from the end address. |
| 411 | */ |
| 412 | return !ftrace_location_range(addr, addr + size - 1); |
Masami Hiramatsu | 45408c4 | 2018-07-30 19:20:14 +0900 | [diff] [blame] | 413 | } |
| 414 | #else |
| 415 | #define within_notrace_func(tk) (false) |
| 416 | #endif |
| 417 | |
Masami Hiramatsu | 6142431 | 2011-06-27 16:26:56 +0900 | [diff] [blame] | 418 | /* Internal register function - just handle k*probes and flags */ |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 419 | static int __register_trace_kprobe(struct trace_kprobe *tk) |
Masami Hiramatsu | 6142431 | 2011-06-27 16:26:56 +0900 | [diff] [blame] | 420 | { |
Masami Hiramatsu | a668281 | 2018-08-29 01:18:43 +0900 | [diff] [blame] | 421 | int i, ret; |
Masami Hiramatsu | 6142431 | 2011-06-27 16:26:56 +0900 | [diff] [blame] | 422 | |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 423 | if (trace_probe_is_registered(&tk->tp)) |
Masami Hiramatsu | 6142431 | 2011-06-27 16:26:56 +0900 | [diff] [blame] | 424 | return -EINVAL; |
| 425 | |
Masami Hiramatsu | 45408c4 | 2018-07-30 19:20:14 +0900 | [diff] [blame] | 426 | if (within_notrace_func(tk)) { |
| 427 | pr_warn("Could not probe notrace function %s\n", |
| 428 | trace_kprobe_symbol(tk)); |
| 429 | return -EINVAL; |
| 430 | } |
| 431 | |
Masami Hiramatsu | a668281 | 2018-08-29 01:18:43 +0900 | [diff] [blame] | 432 | for (i = 0; i < tk->tp.nr_args; i++) { |
| 433 | ret = traceprobe_update_arg(&tk->tp.args[i]); |
| 434 | if (ret) |
| 435 | return ret; |
| 436 | } |
| 437 | |
Masami Hiramatsu | 6142431 | 2011-06-27 16:26:56 +0900 | [diff] [blame] | 438 | /* Set/clear disabled flag according to tp->flag */ |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 439 | if (trace_probe_is_enabled(&tk->tp)) |
| 440 | tk->rp.kp.flags &= ~KPROBE_FLAG_DISABLED; |
Masami Hiramatsu | 6142431 | 2011-06-27 16:26:56 +0900 | [diff] [blame] | 441 | else |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 442 | tk->rp.kp.flags |= KPROBE_FLAG_DISABLED; |
Masami Hiramatsu | 6142431 | 2011-06-27 16:26:56 +0900 | [diff] [blame] | 443 | |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 444 | if (trace_kprobe_is_return(tk)) |
| 445 | ret = register_kretprobe(&tk->rp); |
Masami Hiramatsu | 6142431 | 2011-06-27 16:26:56 +0900 | [diff] [blame] | 446 | else |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 447 | ret = register_kprobe(&tk->rp.kp); |
Masami Hiramatsu | 6142431 | 2011-06-27 16:26:56 +0900 | [diff] [blame] | 448 | |
Masami Hiramatsu | 59158ec | 2018-08-29 01:18:15 +0900 | [diff] [blame] | 449 | if (ret == 0) { |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 450 | tk->tp.flags |= TP_FLAG_REGISTERED; |
Masami Hiramatsu | 59158ec | 2018-08-29 01:18:15 +0900 | [diff] [blame] | 451 | } else if (ret == -EILSEQ) { |
| 452 | pr_warn("Probing address(0x%p) is not an instruction boundary.\n", |
| 453 | tk->rp.kp.addr); |
| 454 | ret = -EINVAL; |
Masami Hiramatsu | 6142431 | 2011-06-27 16:26:56 +0900 | [diff] [blame] | 455 | } |
Masami Hiramatsu | 6142431 | 2011-06-27 16:26:56 +0900 | [diff] [blame] | 456 | return ret; |
| 457 | } |
| 458 | |
| 459 | /* Internal unregister function - just handle k*probes and flags */ |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 460 | static void __unregister_trace_kprobe(struct trace_kprobe *tk) |
Masami Hiramatsu | 6142431 | 2011-06-27 16:26:56 +0900 | [diff] [blame] | 461 | { |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 462 | if (trace_probe_is_registered(&tk->tp)) { |
| 463 | if (trace_kprobe_is_return(tk)) |
| 464 | unregister_kretprobe(&tk->rp); |
Masami Hiramatsu | 6142431 | 2011-06-27 16:26:56 +0900 | [diff] [blame] | 465 | else |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 466 | unregister_kprobe(&tk->rp.kp); |
| 467 | tk->tp.flags &= ~TP_FLAG_REGISTERED; |
Masami Hiramatsu | 6142431 | 2011-06-27 16:26:56 +0900 | [diff] [blame] | 468 | /* Cleanup kprobe for reuse */ |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 469 | if (tk->rp.kp.symbol_name) |
| 470 | tk->rp.kp.addr = NULL; |
Masami Hiramatsu | 6142431 | 2011-06-27 16:26:56 +0900 | [diff] [blame] | 471 | } |
| 472 | } |
| 473 | |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 474 | /* Unregister a trace_probe and probe_event */ |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 475 | static int unregister_trace_kprobe(struct trace_kprobe *tk) |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 476 | { |
Masami Hiramatsu | 02ca152 | 2011-10-04 19:44:38 +0900 | [diff] [blame] | 477 | /* Enabled event can not be unregistered */ |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 478 | if (trace_probe_is_enabled(&tk->tp)) |
Masami Hiramatsu | 02ca152 | 2011-10-04 19:44:38 +0900 | [diff] [blame] | 479 | return -EBUSY; |
| 480 | |
Steven Rostedt (Red Hat) | 40c3259 | 2013-07-03 23:33:50 -0400 | [diff] [blame] | 481 | /* Will fail if probe is being used by ftrace or perf */ |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 482 | if (unregister_kprobe_event(tk)) |
Steven Rostedt (Red Hat) | 40c3259 | 2013-07-03 23:33:50 -0400 | [diff] [blame] | 483 | return -EBUSY; |
| 484 | |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 485 | __unregister_trace_kprobe(tk); |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 486 | dyn_event_remove(&tk->devent); |
Masami Hiramatsu | 02ca152 | 2011-10-04 19:44:38 +0900 | [diff] [blame] | 487 | |
| 488 | return 0; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | /* Register a trace_probe and probe_event */ |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 492 | static int register_trace_kprobe(struct trace_kprobe *tk) |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 493 | { |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 494 | struct trace_kprobe *old_tk; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 495 | int ret; |
| 496 | |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 497 | mutex_lock(&event_mutex); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 498 | |
Masami Hiramatsu | 6142431 | 2011-06-27 16:26:56 +0900 | [diff] [blame] | 499 | /* Delete old (same name) event if exist */ |
Steven Rostedt (Red Hat) | 687fcc4 | 2015-05-13 14:20:14 -0400 | [diff] [blame] | 500 | old_tk = find_trace_kprobe(trace_event_name(&tk->tp.call), |
Mathieu Desnoyers | de7b297 | 2014-04-08 17:26:21 -0400 | [diff] [blame] | 501 | tk->tp.call.class->system); |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 502 | if (old_tk) { |
| 503 | ret = unregister_trace_kprobe(old_tk); |
Masami Hiramatsu | 02ca152 | 2011-10-04 19:44:38 +0900 | [diff] [blame] | 504 | if (ret < 0) |
| 505 | goto end; |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 506 | free_trace_kprobe(old_tk); |
Masami Hiramatsu | 2d5e067 | 2009-09-14 16:48:56 -0400 | [diff] [blame] | 507 | } |
Masami Hiramatsu | 6142431 | 2011-06-27 16:26:56 +0900 | [diff] [blame] | 508 | |
| 509 | /* Register new event */ |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 510 | ret = register_kprobe_event(tk); |
Masami Hiramatsu | 2d5e067 | 2009-09-14 16:48:56 -0400 | [diff] [blame] | 511 | if (ret) { |
Joe Perches | a395d6a | 2016-03-22 14:28:09 -0700 | [diff] [blame] | 512 | pr_warn("Failed to register probe event(%d)\n", ret); |
Masami Hiramatsu | 2d5e067 | 2009-09-14 16:48:56 -0400 | [diff] [blame] | 513 | goto end; |
| 514 | } |
| 515 | |
Masami Hiramatsu | 6142431 | 2011-06-27 16:26:56 +0900 | [diff] [blame] | 516 | /* Register k*probe */ |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 517 | ret = __register_trace_kprobe(tk); |
Masami Hiramatsu | 59158ec | 2018-08-29 01:18:15 +0900 | [diff] [blame] | 518 | if (ret == -ENOENT && !trace_kprobe_module_exist(tk)) { |
| 519 | pr_warn("This probe might be able to register after target module is loaded. Continue.\n"); |
| 520 | ret = 0; |
| 521 | } |
| 522 | |
Masami Hiramatsu | 6142431 | 2011-06-27 16:26:56 +0900 | [diff] [blame] | 523 | if (ret < 0) |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 524 | unregister_kprobe_event(tk); |
Masami Hiramatsu | 6142431 | 2011-06-27 16:26:56 +0900 | [diff] [blame] | 525 | else |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 526 | dyn_event_add(&tk->devent); |
Masami Hiramatsu | 6142431 | 2011-06-27 16:26:56 +0900 | [diff] [blame] | 527 | |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 528 | end: |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 529 | mutex_unlock(&event_mutex); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 530 | return ret; |
| 531 | } |
| 532 | |
Masami Hiramatsu | 6142431 | 2011-06-27 16:26:56 +0900 | [diff] [blame] | 533 | /* Module notifier call back, checking event on the module */ |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 534 | static int trace_kprobe_module_callback(struct notifier_block *nb, |
Masami Hiramatsu | 6142431 | 2011-06-27 16:26:56 +0900 | [diff] [blame] | 535 | unsigned long val, void *data) |
| 536 | { |
| 537 | struct module *mod = data; |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 538 | struct dyn_event *pos; |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 539 | struct trace_kprobe *tk; |
Masami Hiramatsu | 6142431 | 2011-06-27 16:26:56 +0900 | [diff] [blame] | 540 | int ret; |
| 541 | |
| 542 | if (val != MODULE_STATE_COMING) |
| 543 | return NOTIFY_DONE; |
| 544 | |
| 545 | /* Update probes on coming module */ |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 546 | mutex_lock(&event_mutex); |
| 547 | for_each_trace_kprobe(tk, pos) { |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 548 | if (trace_kprobe_within_module(tk, mod)) { |
Masami Hiramatsu | 02ca152 | 2011-10-04 19:44:38 +0900 | [diff] [blame] | 549 | /* Don't need to check busy - this should have gone. */ |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 550 | __unregister_trace_kprobe(tk); |
| 551 | ret = __register_trace_kprobe(tk); |
Masami Hiramatsu | 6142431 | 2011-06-27 16:26:56 +0900 | [diff] [blame] | 552 | if (ret) |
Joe Perches | a395d6a | 2016-03-22 14:28:09 -0700 | [diff] [blame] | 553 | pr_warn("Failed to re-register probe %s on %s: %d\n", |
| 554 | trace_event_name(&tk->tp.call), |
| 555 | mod->name, ret); |
Masami Hiramatsu | 6142431 | 2011-06-27 16:26:56 +0900 | [diff] [blame] | 556 | } |
| 557 | } |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 558 | mutex_unlock(&event_mutex); |
Masami Hiramatsu | 6142431 | 2011-06-27 16:26:56 +0900 | [diff] [blame] | 559 | |
| 560 | return NOTIFY_DONE; |
| 561 | } |
| 562 | |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 563 | static struct notifier_block trace_kprobe_module_nb = { |
| 564 | .notifier_call = trace_kprobe_module_callback, |
Masami Hiramatsu | 6142431 | 2011-06-27 16:26:56 +0900 | [diff] [blame] | 565 | .priority = 1 /* Invoked after kprobe module callback */ |
| 566 | }; |
| 567 | |
Naveen N. Rao | fca18a4 | 2017-07-08 00:27:30 +0530 | [diff] [blame] | 568 | /* Convert certain expected symbols into '_' when generating event names */ |
| 569 | static inline void sanitize_event_name(char *name) |
| 570 | { |
| 571 | while (*name++ != '\0') |
| 572 | if (*name == ':' || *name == '.') |
| 573 | *name = '_'; |
| 574 | } |
| 575 | |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 576 | static int trace_kprobe_create(int argc, const char *argv[]) |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 577 | { |
| 578 | /* |
| 579 | * Argument syntax: |
Alban Crequy | 696ced4 | 2017-04-03 12:36:22 +0200 | [diff] [blame] | 580 | * - Add kprobe: |
| 581 | * p[:[GRP/]EVENT] [MOD:]KSYM[+OFFS]|KADDR [FETCHARGS] |
| 582 | * - Add kretprobe: |
| 583 | * r[MAXACTIVE][:[GRP/]EVENT] [MOD:]KSYM[+0] [FETCHARGS] |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 584 | * Fetch args: |
Masami Hiramatsu | 2e06ff6 | 2009-10-07 18:27:59 -0400 | [diff] [blame] | 585 | * $retval : fetch return value |
| 586 | * $stack : fetch stack address |
| 587 | * $stackN : fetch Nth of stack (N:0-) |
Omar Sandoval | 35abb67 | 2016-06-08 18:38:02 -0700 | [diff] [blame] | 588 | * $comm : fetch current task comm |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 589 | * @ADDR : fetch memory at ADDR (ADDR should be in kernel) |
| 590 | * @SYM[+|-offs] : fetch memory at SYM +|- offs (SYM is a data symbol) |
| 591 | * %REG : fetch register REG |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 592 | * Dereferencing memory fetch: |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 593 | * +|-offs(ARG) : fetch memory at ARG +|- offs address. |
Masami Hiramatsu | eca0d91 | 2009-09-10 19:53:38 -0400 | [diff] [blame] | 594 | * Alias name of args: |
| 595 | * NAME=FETCHARG : set NAME as alias of FETCHARG. |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 596 | * Type of args: |
| 597 | * FETCHARG:TYPE : use TYPE instead of unsigned long. |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 598 | */ |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 599 | struct trace_kprobe *tk; |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 600 | int i, len, ret = 0; |
| 601 | bool is_return = false; |
| 602 | char *symbol = NULL, *tmp = NULL; |
| 603 | const char *event = NULL, *group = KPROBE_EVENT_SYSTEM; |
Alban Crequy | 696ced4 | 2017-04-03 12:36:22 +0200 | [diff] [blame] | 604 | int maxactive = 0; |
Masami Hiramatsu | c5d343b | 2018-03-17 21:38:10 +0900 | [diff] [blame] | 605 | long offset = 0; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 606 | void *addr = NULL; |
Masami Hiramatsu | 4a846b4 | 2009-09-11 05:31:21 +0200 | [diff] [blame] | 607 | char buf[MAX_EVENT_NAME_LEN]; |
Masami Hiramatsu | a1303af | 2018-04-25 21:21:26 +0900 | [diff] [blame] | 608 | unsigned int flags = TPARG_FL_KERNEL; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 609 | |
Andrea Righi | 8b05a3a | 2019-01-11 07:01:13 +0100 | [diff] [blame] | 610 | switch (argv[0][0]) { |
| 611 | case 'r': |
Srikar Dronamraju | 3a6b766 | 2012-04-09 14:41:33 +0530 | [diff] [blame] | 612 | is_return = true; |
Masami Hiramatsu | a1303af | 2018-04-25 21:21:26 +0900 | [diff] [blame] | 613 | flags |= TPARG_FL_RETURN; |
Andrea Righi | 8b05a3a | 2019-01-11 07:01:13 +0100 | [diff] [blame] | 614 | break; |
| 615 | case 'p': |
| 616 | break; |
| 617 | default: |
| 618 | return -ECANCELED; |
| 619 | } |
| 620 | if (argc < 2) |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 621 | return -ECANCELED; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 622 | |
Alban Crequy | 696ced4 | 2017-04-03 12:36:22 +0200 | [diff] [blame] | 623 | event = strchr(&argv[0][1], ':'); |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 624 | if (event) |
Alban Crequy | 696ced4 | 2017-04-03 12:36:22 +0200 | [diff] [blame] | 625 | event++; |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 626 | |
Alban Crequy | 696ced4 | 2017-04-03 12:36:22 +0200 | [diff] [blame] | 627 | if (is_return && isdigit(argv[0][1])) { |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 628 | if (event) |
| 629 | len = event - &argv[0][1] - 1; |
| 630 | else |
| 631 | len = strlen(&argv[0][1]); |
| 632 | if (len > MAX_EVENT_NAME_LEN - 1) |
| 633 | return -E2BIG; |
| 634 | memcpy(buf, &argv[0][1], len); |
| 635 | buf[len] = '\0'; |
| 636 | ret = kstrtouint(buf, 0, &maxactive); |
Alban Crequy | 696ced4 | 2017-04-03 12:36:22 +0200 | [diff] [blame] | 637 | if (ret) { |
| 638 | pr_info("Failed to parse maxactive.\n"); |
| 639 | return ret; |
| 640 | } |
| 641 | /* kretprobes instances are iterated over via a list. The |
| 642 | * maximum should stay reasonable. |
| 643 | */ |
| 644 | if (maxactive > KRETPROBE_MAXACTIVE_MAX) { |
| 645 | pr_info("Maxactive is too big (%d > %d).\n", |
| 646 | maxactive, KRETPROBE_MAXACTIVE_MAX); |
| 647 | return -E2BIG; |
| 648 | } |
| 649 | } |
| 650 | |
Sabrina Dubroca | 9e52b32 | 2017-06-22 11:24:42 +0200 | [diff] [blame] | 651 | /* try to parse an address. if that fails, try to read the |
| 652 | * input as a symbol. */ |
| 653 | if (kstrtoul(argv[1], 0, (unsigned long *)&addr)) { |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 654 | /* Check whether uprobe event specified */ |
| 655 | if (strchr(argv[1], '/') && strchr(argv[1], ':')) |
| 656 | return -ECANCELED; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 657 | /* a symbol specified */ |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 658 | symbol = kstrdup(argv[1], GFP_KERNEL); |
| 659 | if (!symbol) |
| 660 | return -ENOMEM; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 661 | /* TODO: support .init module functions */ |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 662 | ret = traceprobe_split_symbol_offset(symbol, &offset); |
Masami Hiramatsu | c5d343b | 2018-03-17 21:38:10 +0900 | [diff] [blame] | 663 | if (ret || offset < 0 || offset > UINT_MAX) { |
Sabrina Dubroca | 9e52b32 | 2017-06-22 11:24:42 +0200 | [diff] [blame] | 664 | pr_info("Failed to parse either an address or a symbol.\n"); |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 665 | goto out; |
Masami Hiramatsu | e63cc23 | 2009-10-16 20:07:28 -0400 | [diff] [blame] | 666 | } |
Masami Hiramatsu | a1303af | 2018-04-25 21:21:26 +0900 | [diff] [blame] | 667 | if (kprobe_on_func_entry(NULL, symbol, offset)) |
| 668 | flags |= TPARG_FL_FENTRY; |
| 669 | if (offset && is_return && !(flags & TPARG_FL_FENTRY)) { |
Steven Rostedt (VMware) | d0e0257 | 2017-02-27 11:52:04 -0500 | [diff] [blame] | 670 | pr_info("Given offset is not valid for return probe.\n"); |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 671 | ret = -EINVAL; |
| 672 | goto out; |
Masami Hiramatsu | e63cc23 | 2009-10-16 20:07:28 -0400 | [diff] [blame] | 673 | } |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 674 | } |
Masami Hiramatsu | a82378d | 2009-08-13 16:35:18 -0400 | [diff] [blame] | 675 | argc -= 2; argv += 2; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 676 | |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 677 | if (event) { |
| 678 | ret = traceprobe_parse_event_name(&event, &group, buf); |
| 679 | if (ret) |
| 680 | goto out; |
| 681 | } else { |
Masami Hiramatsu | 4263565 | 2009-08-13 16:35:26 -0400 | [diff] [blame] | 682 | /* Make a new event name */ |
Masami Hiramatsu | 4263565 | 2009-08-13 16:35:26 -0400 | [diff] [blame] | 683 | if (symbol) |
Masami Hiramatsu | 6f3cf44 | 2009-12-16 17:24:08 -0500 | [diff] [blame] | 684 | snprintf(buf, MAX_EVENT_NAME_LEN, "%c_%s_%ld", |
Masami Hiramatsu | 4263565 | 2009-08-13 16:35:26 -0400 | [diff] [blame] | 685 | is_return ? 'r' : 'p', symbol, offset); |
| 686 | else |
Masami Hiramatsu | 6f3cf44 | 2009-12-16 17:24:08 -0500 | [diff] [blame] | 687 | snprintf(buf, MAX_EVENT_NAME_LEN, "%c_0x%p", |
Masami Hiramatsu | 4263565 | 2009-08-13 16:35:26 -0400 | [diff] [blame] | 688 | is_return ? 'r' : 'p', addr); |
Naveen N. Rao | fca18a4 | 2017-07-08 00:27:30 +0530 | [diff] [blame] | 689 | sanitize_event_name(buf); |
Masami Hiramatsu | 4a846b4 | 2009-09-11 05:31:21 +0200 | [diff] [blame] | 690 | event = buf; |
| 691 | } |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 692 | |
| 693 | /* setup a probe */ |
Alban Crequy | 696ced4 | 2017-04-03 12:36:22 +0200 | [diff] [blame] | 694 | tk = alloc_trace_kprobe(group, event, addr, symbol, offset, maxactive, |
| 695 | argc, is_return); |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 696 | if (IS_ERR(tk)) { |
Masami Hiramatsu | e63cc23 | 2009-10-16 20:07:28 -0400 | [diff] [blame] | 697 | pr_info("Failed to allocate trace_probe.(%d)\n", |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 698 | (int)PTR_ERR(tk)); |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 699 | ret = PTR_ERR(tk); |
| 700 | goto out; |
Masami Hiramatsu | e63cc23 | 2009-10-16 20:07:28 -0400 | [diff] [blame] | 701 | } |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 702 | |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 703 | /* parse arguments */ |
Masami Hiramatsu | a82378d | 2009-08-13 16:35:18 -0400 | [diff] [blame] | 704 | for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) { |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 705 | tmp = kstrdup(argv[i], GFP_KERNEL); |
| 706 | if (!tmp) { |
Masami Hiramatsu | ba8665d | 2009-11-30 19:19:20 -0500 | [diff] [blame] | 707 | ret = -ENOMEM; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 708 | goto error; |
| 709 | } |
Masami Hiramatsu | da34634 | 2010-08-27 20:39:12 +0900 | [diff] [blame] | 710 | |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 711 | ret = traceprobe_parse_probe_arg(&tk->tp, i, tmp, flags); |
| 712 | kfree(tmp); |
Masami Hiramatsu | d00bbea9 | 2018-11-05 18:01:40 +0900 | [diff] [blame] | 713 | if (ret) |
Masami Hiramatsu | da34634 | 2010-08-27 20:39:12 +0900 | [diff] [blame] | 714 | goto error; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 715 | } |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 716 | |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 717 | ret = register_trace_kprobe(tk); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 718 | if (ret) |
| 719 | goto error; |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 720 | out: |
| 721 | kfree(symbol); |
| 722 | return ret; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 723 | |
| 724 | error: |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 725 | free_trace_kprobe(tk); |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 726 | goto out; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 727 | } |
| 728 | |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 729 | static int create_or_delete_trace_kprobe(int argc, char **argv) |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 730 | { |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 731 | int ret; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 732 | |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 733 | if (argv[0][0] == '-') |
| 734 | return dyn_event_release(argc, argv, &trace_kprobe_ops); |
| 735 | |
| 736 | ret = trace_kprobe_create(argc, (const char **)argv); |
| 737 | return ret == -ECANCELED ? -EINVAL : ret; |
| 738 | } |
| 739 | |
| 740 | static int trace_kprobe_release(struct dyn_event *ev) |
| 741 | { |
| 742 | struct trace_kprobe *tk = to_trace_kprobe(ev); |
| 743 | int ret = unregister_trace_kprobe(tk); |
| 744 | |
| 745 | if (!ret) |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 746 | free_trace_kprobe(tk); |
Masami Hiramatsu | 02ca152 | 2011-10-04 19:44:38 +0900 | [diff] [blame] | 747 | return ret; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 748 | } |
| 749 | |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 750 | static int trace_kprobe_show(struct seq_file *m, struct dyn_event *ev) |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 751 | { |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 752 | struct trace_kprobe *tk = to_trace_kprobe(ev); |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 753 | int i; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 754 | |
Rasmus Villemoes | fa6f0cc | 2014-11-08 21:42:10 +0100 | [diff] [blame] | 755 | seq_putc(m, trace_kprobe_is_return(tk) ? 'r' : 'p'); |
Mathieu Desnoyers | de7b297 | 2014-04-08 17:26:21 -0400 | [diff] [blame] | 756 | seq_printf(m, ":%s/%s", tk->tp.call.class->system, |
Steven Rostedt (Red Hat) | 687fcc4 | 2015-05-13 14:20:14 -0400 | [diff] [blame] | 757 | trace_event_name(&tk->tp.call)); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 758 | |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 759 | if (!tk->symbol) |
| 760 | seq_printf(m, " 0x%p", tk->rp.kp.addr); |
| 761 | else if (tk->rp.kp.offset) |
| 762 | seq_printf(m, " %s+%u", trace_kprobe_symbol(tk), |
| 763 | tk->rp.kp.offset); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 764 | else |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 765 | seq_printf(m, " %s", trace_kprobe_symbol(tk)); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 766 | |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 767 | for (i = 0; i < tk->tp.nr_args; i++) |
| 768 | seq_printf(m, " %s=%s", tk->tp.args[i].name, tk->tp.args[i].comm); |
Rasmus Villemoes | fa6f0cc | 2014-11-08 21:42:10 +0100 | [diff] [blame] | 769 | seq_putc(m, '\n'); |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 770 | |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 771 | return 0; |
| 772 | } |
| 773 | |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 774 | static int probes_seq_show(struct seq_file *m, void *v) |
| 775 | { |
| 776 | struct dyn_event *ev = v; |
| 777 | |
| 778 | if (!is_trace_kprobe(ev)) |
| 779 | return 0; |
| 780 | |
| 781 | return trace_kprobe_show(m, ev); |
| 782 | } |
| 783 | |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 784 | static const struct seq_operations probes_seq_op = { |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 785 | .start = dyn_event_seq_start, |
| 786 | .next = dyn_event_seq_next, |
| 787 | .stop = dyn_event_seq_stop, |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 788 | .show = probes_seq_show |
| 789 | }; |
| 790 | |
| 791 | static int probes_open(struct inode *inode, struct file *file) |
| 792 | { |
Masami Hiramatsu | 02ca152 | 2011-10-04 19:44:38 +0900 | [diff] [blame] | 793 | int ret; |
| 794 | |
| 795 | if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) { |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 796 | ret = dyn_events_release_all(&trace_kprobe_ops); |
Masami Hiramatsu | 02ca152 | 2011-10-04 19:44:38 +0900 | [diff] [blame] | 797 | if (ret < 0) |
| 798 | return ret; |
| 799 | } |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 800 | |
| 801 | return seq_open(file, &probes_seq_op); |
| 802 | } |
| 803 | |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 804 | static ssize_t probes_write(struct file *file, const char __user *buffer, |
| 805 | size_t count, loff_t *ppos) |
| 806 | { |
Tom Zanussi | 7e465ba | 2017-09-22 14:58:20 -0500 | [diff] [blame] | 807 | return trace_parse_run_command(file, buffer, count, ppos, |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 808 | create_or_delete_trace_kprobe); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 809 | } |
| 810 | |
| 811 | static const struct file_operations kprobe_events_ops = { |
| 812 | .owner = THIS_MODULE, |
| 813 | .open = probes_open, |
| 814 | .read = seq_read, |
| 815 | .llseek = seq_lseek, |
| 816 | .release = seq_release, |
| 817 | .write = probes_write, |
| 818 | }; |
| 819 | |
Masami Hiramatsu | cd7e7bd | 2009-08-13 16:35:42 -0400 | [diff] [blame] | 820 | /* Probes profiling interfaces */ |
| 821 | static int probes_profile_seq_show(struct seq_file *m, void *v) |
| 822 | { |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 823 | struct dyn_event *ev = v; |
| 824 | struct trace_kprobe *tk; |
Masami Hiramatsu | cd7e7bd | 2009-08-13 16:35:42 -0400 | [diff] [blame] | 825 | |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 826 | if (!is_trace_kprobe(ev)) |
| 827 | return 0; |
| 828 | |
| 829 | tk = to_trace_kprobe(ev); |
Mathieu Desnoyers | de7b297 | 2014-04-08 17:26:21 -0400 | [diff] [blame] | 830 | seq_printf(m, " %-44s %15lu %15lu\n", |
Marcin Nowakowski | f18f97a | 2016-12-09 15:19:37 +0100 | [diff] [blame] | 831 | trace_event_name(&tk->tp.call), |
| 832 | trace_kprobe_nhit(tk), |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 833 | tk->rp.kp.nmissed); |
Masami Hiramatsu | cd7e7bd | 2009-08-13 16:35:42 -0400 | [diff] [blame] | 834 | |
| 835 | return 0; |
| 836 | } |
| 837 | |
| 838 | static const struct seq_operations profile_seq_op = { |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 839 | .start = dyn_event_seq_start, |
| 840 | .next = dyn_event_seq_next, |
| 841 | .stop = dyn_event_seq_stop, |
Masami Hiramatsu | cd7e7bd | 2009-08-13 16:35:42 -0400 | [diff] [blame] | 842 | .show = probes_profile_seq_show |
| 843 | }; |
| 844 | |
| 845 | static int profile_open(struct inode *inode, struct file *file) |
| 846 | { |
| 847 | return seq_open(file, &profile_seq_op); |
| 848 | } |
| 849 | |
| 850 | static const struct file_operations kprobe_profile_ops = { |
| 851 | .owner = THIS_MODULE, |
| 852 | .open = profile_open, |
| 853 | .read = seq_read, |
| 854 | .llseek = seq_lseek, |
| 855 | .release = seq_release, |
| 856 | }; |
| 857 | |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 858 | /* Kprobe specific fetch functions */ |
| 859 | |
| 860 | /* Return the length of string -- including null terminal byte */ |
Masami Hiramatsu | 9178412 | 2018-04-25 21:19:01 +0900 | [diff] [blame] | 861 | static nokprobe_inline int |
| 862 | fetch_store_strlen(unsigned long addr) |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 863 | { |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 864 | int ret, len = 0; |
| 865 | u8 c; |
| 866 | |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 867 | do { |
Masami Hiramatsu | 49ef5f4 | 2019-02-22 01:16:43 +0900 | [diff] [blame^] | 868 | ret = probe_kernel_read(&c, (u8 *)addr + len, 1); |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 869 | len++; |
| 870 | } while (c && ret == 0 && len < MAX_STRING_SIZE); |
| 871 | |
Masami Hiramatsu | 9178412 | 2018-04-25 21:19:01 +0900 | [diff] [blame] | 872 | return (ret < 0) ? ret : len; |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 873 | } |
| 874 | |
| 875 | /* |
| 876 | * Fetch a null-terminated string. Caller MUST set *(u32 *)buf with max |
| 877 | * length and relative data location. |
| 878 | */ |
Masami Hiramatsu | 9178412 | 2018-04-25 21:19:01 +0900 | [diff] [blame] | 879 | static nokprobe_inline int |
| 880 | fetch_store_string(unsigned long addr, void *dest, void *base) |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 881 | { |
Masami Hiramatsu | 9178412 | 2018-04-25 21:19:01 +0900 | [diff] [blame] | 882 | int maxlen = get_loc_len(*(u32 *)dest); |
| 883 | u8 *dst = get_loc_data(dest, base); |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 884 | long ret; |
| 885 | |
Masami Hiramatsu | 9178412 | 2018-04-25 21:19:01 +0900 | [diff] [blame] | 886 | if (unlikely(!maxlen)) |
| 887 | return -ENOMEM; |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 888 | /* |
| 889 | * Try to get string again, since the string can be changed while |
| 890 | * probing. |
| 891 | */ |
| 892 | ret = strncpy_from_unsafe(dst, (void *)addr, maxlen); |
| 893 | |
Masami Hiramatsu | 9178412 | 2018-04-25 21:19:01 +0900 | [diff] [blame] | 894 | if (ret >= 0) |
| 895 | *(u32 *)dest = make_data_loc(ret, (void *)dst - base); |
| 896 | return ret; |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 897 | } |
| 898 | |
Masami Hiramatsu | 9b960a3 | 2018-04-25 21:19:59 +0900 | [diff] [blame] | 899 | static nokprobe_inline int |
| 900 | probe_mem_read(void *dest, void *src, size_t size) |
| 901 | { |
| 902 | return probe_kernel_read(dest, src, size); |
| 903 | } |
| 904 | |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 905 | /* Note that we don't verify it, since the code does not come from user space */ |
| 906 | static int |
| 907 | process_fetch_insn(struct fetch_insn *code, struct pt_regs *regs, void *dest, |
Masami Hiramatsu | 9178412 | 2018-04-25 21:19:01 +0900 | [diff] [blame] | 908 | void *base) |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 909 | { |
| 910 | unsigned long val; |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 911 | |
Masami Hiramatsu | a668281 | 2018-08-29 01:18:43 +0900 | [diff] [blame] | 912 | retry: |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 913 | /* 1st stage: get value from context */ |
| 914 | switch (code->op) { |
| 915 | case FETCH_OP_REG: |
| 916 | val = regs_get_register(regs, code->param); |
| 917 | break; |
| 918 | case FETCH_OP_STACK: |
| 919 | val = regs_get_kernel_stack_nth(regs, code->param); |
| 920 | break; |
| 921 | case FETCH_OP_STACKP: |
| 922 | val = kernel_stack_pointer(regs); |
| 923 | break; |
| 924 | case FETCH_OP_RETVAL: |
| 925 | val = regs_return_value(regs); |
| 926 | break; |
| 927 | case FETCH_OP_IMM: |
| 928 | val = code->immediate; |
| 929 | break; |
| 930 | case FETCH_OP_COMM: |
| 931 | val = (unsigned long)current->comm; |
| 932 | break; |
Masami Hiramatsu | a1303af | 2018-04-25 21:21:26 +0900 | [diff] [blame] | 933 | #ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API |
| 934 | case FETCH_OP_ARG: |
| 935 | val = regs_get_kernel_argument(regs, code->param); |
| 936 | break; |
| 937 | #endif |
Masami Hiramatsu | a668281 | 2018-08-29 01:18:43 +0900 | [diff] [blame] | 938 | case FETCH_NOP_SYMBOL: /* Ignore a place holder */ |
| 939 | code++; |
| 940 | goto retry; |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 941 | default: |
| 942 | return -EILSEQ; |
| 943 | } |
| 944 | code++; |
| 945 | |
Masami Hiramatsu | 9b960a3 | 2018-04-25 21:19:59 +0900 | [diff] [blame] | 946 | return process_fetch_insn_bottom(code, val, dest, base); |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 947 | } |
| 948 | NOKPROBE_SYMBOL(process_fetch_insn) |
| 949 | |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 950 | /* Kprobe handler */ |
Masami Hiramatsu | 3da0f18 | 2014-04-17 17:18:28 +0900 | [diff] [blame] | 951 | static nokprobe_inline void |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 952 | __kprobe_trace_func(struct trace_kprobe *tk, struct pt_regs *regs, |
Steven Rostedt (Red Hat) | 7f1d2f8 | 2015-05-05 10:09:53 -0400 | [diff] [blame] | 953 | struct trace_event_file *trace_file) |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 954 | { |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 955 | struct kprobe_trace_entry_head *entry; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 956 | struct ring_buffer_event *event; |
Frederic Weisbecker | 8f8ffe2 | 2009-09-11 01:09:23 +0200 | [diff] [blame] | 957 | struct ring_buffer *buffer; |
Masami Hiramatsu | e09c861 | 2010-07-05 15:54:45 -0300 | [diff] [blame] | 958 | int size, dsize, pc; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 959 | unsigned long irq_flags; |
Steven Rostedt (Red Hat) | 2425bcb | 2015-05-05 11:45:27 -0400 | [diff] [blame] | 960 | struct trace_event_call *call = &tk->tp.call; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 961 | |
Steven Rostedt (Red Hat) | 7f1d2f8 | 2015-05-05 10:09:53 -0400 | [diff] [blame] | 962 | WARN_ON(call != trace_file->event_call); |
Masami Hiramatsu | 41a7dd4 | 2013-05-09 14:44:49 +0900 | [diff] [blame] | 963 | |
Steven Rostedt (Red Hat) | 09a5059 | 2015-05-13 15:21:25 -0400 | [diff] [blame] | 964 | if (trace_trigger_soft_disabled(trace_file)) |
Steven Rostedt (Red Hat) | 13a1e4a | 2014-01-06 21:32:10 -0500 | [diff] [blame] | 965 | return; |
Masami Hiramatsu | b882008 | 2013-05-09 14:44:54 +0900 | [diff] [blame] | 966 | |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 967 | local_save_flags(irq_flags); |
| 968 | pc = preempt_count(); |
| 969 | |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 970 | dsize = __get_data_size(&tk->tp, regs); |
| 971 | size = sizeof(*entry) + tk->tp.size + dsize; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 972 | |
Steven Rostedt (Red Hat) | 7f1d2f8 | 2015-05-05 10:09:53 -0400 | [diff] [blame] | 973 | event = trace_event_buffer_lock_reserve(&buffer, trace_file, |
Masami Hiramatsu | 41a7dd4 | 2013-05-09 14:44:49 +0900 | [diff] [blame] | 974 | call->event.type, |
| 975 | size, irq_flags, pc); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 976 | if (!event) |
Xiao Guangrong | 1e12a4a | 2010-01-28 09:34:27 +0800 | [diff] [blame] | 977 | return; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 978 | |
| 979 | entry = ring_buffer_event_data(event); |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 980 | entry->ip = (unsigned long)tk->rp.kp.addr; |
Masami Hiramatsu | 9178412 | 2018-04-25 21:19:01 +0900 | [diff] [blame] | 981 | store_trace_args(&entry[1], &tk->tp, regs, sizeof(*entry), dsize); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 982 | |
Steven Rostedt (Red Hat) | 7f1d2f8 | 2015-05-05 10:09:53 -0400 | [diff] [blame] | 983 | event_trigger_unlock_commit_regs(trace_file, buffer, event, |
Steven Rostedt (Red Hat) | 13a1e4a | 2014-01-06 21:32:10 -0500 | [diff] [blame] | 984 | entry, irq_flags, pc, regs); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 985 | } |
| 986 | |
Masami Hiramatsu | 3da0f18 | 2014-04-17 17:18:28 +0900 | [diff] [blame] | 987 | static void |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 988 | kprobe_trace_func(struct trace_kprobe *tk, struct pt_regs *regs) |
Masami Hiramatsu | 41a7dd4 | 2013-05-09 14:44:49 +0900 | [diff] [blame] | 989 | { |
Oleg Nesterov | b04d52e | 2013-06-20 19:38:14 +0200 | [diff] [blame] | 990 | struct event_file_link *link; |
Masami Hiramatsu | 41a7dd4 | 2013-05-09 14:44:49 +0900 | [diff] [blame] | 991 | |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 992 | list_for_each_entry_rcu(link, &tk->tp.files, list) |
| 993 | __kprobe_trace_func(tk, regs, link->file); |
Masami Hiramatsu | 41a7dd4 | 2013-05-09 14:44:49 +0900 | [diff] [blame] | 994 | } |
Masami Hiramatsu | 3da0f18 | 2014-04-17 17:18:28 +0900 | [diff] [blame] | 995 | NOKPROBE_SYMBOL(kprobe_trace_func); |
Masami Hiramatsu | 41a7dd4 | 2013-05-09 14:44:49 +0900 | [diff] [blame] | 996 | |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 997 | /* Kretprobe handler */ |
Masami Hiramatsu | 3da0f18 | 2014-04-17 17:18:28 +0900 | [diff] [blame] | 998 | static nokprobe_inline void |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 999 | __kretprobe_trace_func(struct trace_kprobe *tk, struct kretprobe_instance *ri, |
Masami Hiramatsu | 41a7dd4 | 2013-05-09 14:44:49 +0900 | [diff] [blame] | 1000 | struct pt_regs *regs, |
Steven Rostedt (Red Hat) | 7f1d2f8 | 2015-05-05 10:09:53 -0400 | [diff] [blame] | 1001 | struct trace_event_file *trace_file) |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1002 | { |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 1003 | struct kretprobe_trace_entry_head *entry; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1004 | struct ring_buffer_event *event; |
Frederic Weisbecker | 8f8ffe2 | 2009-09-11 01:09:23 +0200 | [diff] [blame] | 1005 | struct ring_buffer *buffer; |
Masami Hiramatsu | e09c861 | 2010-07-05 15:54:45 -0300 | [diff] [blame] | 1006 | int size, pc, dsize; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1007 | unsigned long irq_flags; |
Steven Rostedt (Red Hat) | 2425bcb | 2015-05-05 11:45:27 -0400 | [diff] [blame] | 1008 | struct trace_event_call *call = &tk->tp.call; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1009 | |
Steven Rostedt (Red Hat) | 7f1d2f8 | 2015-05-05 10:09:53 -0400 | [diff] [blame] | 1010 | WARN_ON(call != trace_file->event_call); |
Masami Hiramatsu | 41a7dd4 | 2013-05-09 14:44:49 +0900 | [diff] [blame] | 1011 | |
Steven Rostedt (Red Hat) | 09a5059 | 2015-05-13 15:21:25 -0400 | [diff] [blame] | 1012 | if (trace_trigger_soft_disabled(trace_file)) |
Steven Rostedt (Red Hat) | 13a1e4a | 2014-01-06 21:32:10 -0500 | [diff] [blame] | 1013 | return; |
Masami Hiramatsu | b882008 | 2013-05-09 14:44:54 +0900 | [diff] [blame] | 1014 | |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1015 | local_save_flags(irq_flags); |
| 1016 | pc = preempt_count(); |
| 1017 | |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 1018 | dsize = __get_data_size(&tk->tp, regs); |
| 1019 | size = sizeof(*entry) + tk->tp.size + dsize; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1020 | |
Steven Rostedt (Red Hat) | 7f1d2f8 | 2015-05-05 10:09:53 -0400 | [diff] [blame] | 1021 | event = trace_event_buffer_lock_reserve(&buffer, trace_file, |
Masami Hiramatsu | 41a7dd4 | 2013-05-09 14:44:49 +0900 | [diff] [blame] | 1022 | call->event.type, |
| 1023 | size, irq_flags, pc); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1024 | if (!event) |
Xiao Guangrong | 1e12a4a | 2010-01-28 09:34:27 +0800 | [diff] [blame] | 1025 | return; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1026 | |
| 1027 | entry = ring_buffer_event_data(event); |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 1028 | entry->func = (unsigned long)tk->rp.kp.addr; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1029 | entry->ret_ip = (unsigned long)ri->ret_addr; |
Masami Hiramatsu | 9178412 | 2018-04-25 21:19:01 +0900 | [diff] [blame] | 1030 | store_trace_args(&entry[1], &tk->tp, regs, sizeof(*entry), dsize); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1031 | |
Steven Rostedt (Red Hat) | 7f1d2f8 | 2015-05-05 10:09:53 -0400 | [diff] [blame] | 1032 | event_trigger_unlock_commit_regs(trace_file, buffer, event, |
Steven Rostedt (Red Hat) | 13a1e4a | 2014-01-06 21:32:10 -0500 | [diff] [blame] | 1033 | entry, irq_flags, pc, regs); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1034 | } |
| 1035 | |
Masami Hiramatsu | 3da0f18 | 2014-04-17 17:18:28 +0900 | [diff] [blame] | 1036 | static void |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 1037 | kretprobe_trace_func(struct trace_kprobe *tk, struct kretprobe_instance *ri, |
Masami Hiramatsu | 41a7dd4 | 2013-05-09 14:44:49 +0900 | [diff] [blame] | 1038 | struct pt_regs *regs) |
| 1039 | { |
Oleg Nesterov | b04d52e | 2013-06-20 19:38:14 +0200 | [diff] [blame] | 1040 | struct event_file_link *link; |
Masami Hiramatsu | 41a7dd4 | 2013-05-09 14:44:49 +0900 | [diff] [blame] | 1041 | |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 1042 | list_for_each_entry_rcu(link, &tk->tp.files, list) |
| 1043 | __kretprobe_trace_func(tk, ri, regs, link->file); |
Masami Hiramatsu | 41a7dd4 | 2013-05-09 14:44:49 +0900 | [diff] [blame] | 1044 | } |
Masami Hiramatsu | 3da0f18 | 2014-04-17 17:18:28 +0900 | [diff] [blame] | 1045 | NOKPROBE_SYMBOL(kretprobe_trace_func); |
Masami Hiramatsu | 41a7dd4 | 2013-05-09 14:44:49 +0900 | [diff] [blame] | 1046 | |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1047 | /* Event entry printers */ |
Masami Hiramatsu | b62fdd9 | 2013-05-13 20:58:39 +0900 | [diff] [blame] | 1048 | static enum print_line_t |
Steven Rostedt | a9a5776 | 2010-04-22 18:46:14 -0400 | [diff] [blame] | 1049 | print_kprobe_event(struct trace_iterator *iter, int flags, |
| 1050 | struct trace_event *event) |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1051 | { |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 1052 | struct kprobe_trace_entry_head *field; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1053 | struct trace_seq *s = &iter->seq; |
Masami Hiramatsu | eca0d91 | 2009-09-10 19:53:38 -0400 | [diff] [blame] | 1054 | struct trace_probe *tp; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1055 | |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 1056 | field = (struct kprobe_trace_entry_head *)iter->ent; |
Steven Rostedt | 80decc7 | 2010-04-23 10:00:22 -0400 | [diff] [blame] | 1057 | tp = container_of(event, struct trace_probe, call.event); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1058 | |
Steven Rostedt (Red Hat) | 687fcc4 | 2015-05-13 14:20:14 -0400 | [diff] [blame] | 1059 | trace_seq_printf(s, "%s: (", trace_event_name(&tp->call)); |
Masami Hiramatsu | 6e9f23d | 2009-09-10 19:53:45 -0400 | [diff] [blame] | 1060 | |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1061 | if (!seq_print_ip_sym(s, field->ip, flags | TRACE_ITER_SYM_OFFSET)) |
Steven Rostedt (Red Hat) | 85224da | 2014-11-12 15:18:16 -0500 | [diff] [blame] | 1062 | goto out; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1063 | |
Steven Rostedt (Red Hat) | 85224da | 2014-11-12 15:18:16 -0500 | [diff] [blame] | 1064 | trace_seq_putc(s, ')'); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1065 | |
Masami Hiramatsu | 56de763 | 2018-04-25 21:16:36 +0900 | [diff] [blame] | 1066 | if (print_probe_args(s, tp->args, tp->nr_args, |
| 1067 | (u8 *)&field[1], field) < 0) |
| 1068 | goto out; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1069 | |
Steven Rostedt (Red Hat) | 85224da | 2014-11-12 15:18:16 -0500 | [diff] [blame] | 1070 | trace_seq_putc(s, '\n'); |
| 1071 | out: |
| 1072 | return trace_handle_return(s); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1073 | } |
| 1074 | |
Masami Hiramatsu | b62fdd9 | 2013-05-13 20:58:39 +0900 | [diff] [blame] | 1075 | static enum print_line_t |
Steven Rostedt | a9a5776 | 2010-04-22 18:46:14 -0400 | [diff] [blame] | 1076 | print_kretprobe_event(struct trace_iterator *iter, int flags, |
| 1077 | struct trace_event *event) |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1078 | { |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 1079 | struct kretprobe_trace_entry_head *field; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1080 | struct trace_seq *s = &iter->seq; |
Masami Hiramatsu | eca0d91 | 2009-09-10 19:53:38 -0400 | [diff] [blame] | 1081 | struct trace_probe *tp; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1082 | |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 1083 | field = (struct kretprobe_trace_entry_head *)iter->ent; |
Steven Rostedt | 80decc7 | 2010-04-23 10:00:22 -0400 | [diff] [blame] | 1084 | tp = container_of(event, struct trace_probe, call.event); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1085 | |
Steven Rostedt (Red Hat) | 687fcc4 | 2015-05-13 14:20:14 -0400 | [diff] [blame] | 1086 | trace_seq_printf(s, "%s: (", trace_event_name(&tp->call)); |
Masami Hiramatsu | 6e9f23d | 2009-09-10 19:53:45 -0400 | [diff] [blame] | 1087 | |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1088 | if (!seq_print_ip_sym(s, field->ret_ip, flags | TRACE_ITER_SYM_OFFSET)) |
Steven Rostedt (Red Hat) | 85224da | 2014-11-12 15:18:16 -0500 | [diff] [blame] | 1089 | goto out; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1090 | |
Steven Rostedt (Red Hat) | 85224da | 2014-11-12 15:18:16 -0500 | [diff] [blame] | 1091 | trace_seq_puts(s, " <- "); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1092 | |
| 1093 | if (!seq_print_ip_sym(s, field->func, flags & ~TRACE_ITER_SYM_OFFSET)) |
Steven Rostedt (Red Hat) | 85224da | 2014-11-12 15:18:16 -0500 | [diff] [blame] | 1094 | goto out; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1095 | |
Steven Rostedt (Red Hat) | 85224da | 2014-11-12 15:18:16 -0500 | [diff] [blame] | 1096 | trace_seq_putc(s, ')'); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1097 | |
Masami Hiramatsu | 56de763 | 2018-04-25 21:16:36 +0900 | [diff] [blame] | 1098 | if (print_probe_args(s, tp->args, tp->nr_args, |
| 1099 | (u8 *)&field[1], field) < 0) |
| 1100 | goto out; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1101 | |
Steven Rostedt (Red Hat) | 85224da | 2014-11-12 15:18:16 -0500 | [diff] [blame] | 1102 | trace_seq_putc(s, '\n'); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1103 | |
Steven Rostedt (Red Hat) | 85224da | 2014-11-12 15:18:16 -0500 | [diff] [blame] | 1104 | out: |
| 1105 | return trace_handle_return(s); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1106 | } |
| 1107 | |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1108 | |
Steven Rostedt (Red Hat) | 2425bcb | 2015-05-05 11:45:27 -0400 | [diff] [blame] | 1109 | static int kprobe_event_define_fields(struct trace_event_call *event_call) |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1110 | { |
Masami Hiramatsu | eeb07b0 | 2018-04-25 21:17:05 +0900 | [diff] [blame] | 1111 | int ret; |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 1112 | struct kprobe_trace_entry_head field; |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 1113 | struct trace_kprobe *tk = (struct trace_kprobe *)event_call->data; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1114 | |
Masami Hiramatsu | a703d94 | 2009-10-07 18:28:07 -0400 | [diff] [blame] | 1115 | DEFINE_FIELD(unsigned long, ip, FIELD_STRING_IP, 0); |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 1116 | |
Masami Hiramatsu | eeb07b0 | 2018-04-25 21:17:05 +0900 | [diff] [blame] | 1117 | return traceprobe_define_arg_fields(event_call, sizeof(field), &tk->tp); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1118 | } |
| 1119 | |
Steven Rostedt (Red Hat) | 2425bcb | 2015-05-05 11:45:27 -0400 | [diff] [blame] | 1120 | static int kretprobe_event_define_fields(struct trace_event_call *event_call) |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1121 | { |
Masami Hiramatsu | eeb07b0 | 2018-04-25 21:17:05 +0900 | [diff] [blame] | 1122 | int ret; |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 1123 | struct kretprobe_trace_entry_head field; |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 1124 | struct trace_kprobe *tk = (struct trace_kprobe *)event_call->data; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1125 | |
Masami Hiramatsu | a703d94 | 2009-10-07 18:28:07 -0400 | [diff] [blame] | 1126 | DEFINE_FIELD(unsigned long, func, FIELD_STRING_FUNC, 0); |
| 1127 | DEFINE_FIELD(unsigned long, ret_ip, FIELD_STRING_RETIP, 0); |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 1128 | |
Masami Hiramatsu | eeb07b0 | 2018-04-25 21:17:05 +0900 | [diff] [blame] | 1129 | return traceprobe_define_arg_fields(event_call, sizeof(field), &tk->tp); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1130 | } |
| 1131 | |
Li Zefan | 07b139c | 2009-12-21 14:27:35 +0800 | [diff] [blame] | 1132 | #ifdef CONFIG_PERF_EVENTS |
Masami Hiramatsu | e08d1c6 | 2009-09-10 19:53:30 -0400 | [diff] [blame] | 1133 | |
| 1134 | /* Kprobe profile handler */ |
Josef Bacik | 9802d86 | 2017-12-11 11:36:48 -0500 | [diff] [blame] | 1135 | static int |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 1136 | kprobe_perf_func(struct trace_kprobe *tk, struct pt_regs *regs) |
Masami Hiramatsu | e08d1c6 | 2009-09-10 19:53:30 -0400 | [diff] [blame] | 1137 | { |
Steven Rostedt (Red Hat) | 2425bcb | 2015-05-05 11:45:27 -0400 | [diff] [blame] | 1138 | struct trace_event_call *call = &tk->tp.call; |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 1139 | struct kprobe_trace_entry_head *entry; |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 1140 | struct hlist_head *head; |
Masami Hiramatsu | e09c861 | 2010-07-05 15:54:45 -0300 | [diff] [blame] | 1141 | int size, __size, dsize; |
Peter Zijlstra | 4ed7c92 | 2009-11-23 11:37:29 +0100 | [diff] [blame] | 1142 | int rctx; |
Masami Hiramatsu | e08d1c6 | 2009-09-10 19:53:30 -0400 | [diff] [blame] | 1143 | |
Josef Bacik | 9802d86 | 2017-12-11 11:36:48 -0500 | [diff] [blame] | 1144 | if (bpf_prog_array_valid(call)) { |
Masami Hiramatsu | 66665ad | 2018-01-13 02:54:33 +0900 | [diff] [blame] | 1145 | unsigned long orig_ip = instruction_pointer(regs); |
Josef Bacik | 9802d86 | 2017-12-11 11:36:48 -0500 | [diff] [blame] | 1146 | int ret; |
| 1147 | |
| 1148 | ret = trace_call_bpf(call, regs); |
| 1149 | |
| 1150 | /* |
| 1151 | * We need to check and see if we modified the pc of the |
Masami Hiramatsu | cce188b | 2018-06-20 01:15:45 +0900 | [diff] [blame] | 1152 | * pt_regs, and if so return 1 so that we don't do the |
| 1153 | * single stepping. |
Josef Bacik | 9802d86 | 2017-12-11 11:36:48 -0500 | [diff] [blame] | 1154 | */ |
Masami Hiramatsu | cce188b | 2018-06-20 01:15:45 +0900 | [diff] [blame] | 1155 | if (orig_ip != instruction_pointer(regs)) |
Josef Bacik | 9802d86 | 2017-12-11 11:36:48 -0500 | [diff] [blame] | 1156 | return 1; |
Josef Bacik | 9802d86 | 2017-12-11 11:36:48 -0500 | [diff] [blame] | 1157 | if (!ret) |
| 1158 | return 0; |
| 1159 | } |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 1160 | |
Oleg Nesterov | 288e984 | 2013-06-20 19:38:06 +0200 | [diff] [blame] | 1161 | head = this_cpu_ptr(call->perf_events); |
| 1162 | if (hlist_empty(head)) |
Josef Bacik | 9802d86 | 2017-12-11 11:36:48 -0500 | [diff] [blame] | 1163 | return 0; |
Oleg Nesterov | 288e984 | 2013-06-20 19:38:06 +0200 | [diff] [blame] | 1164 | |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 1165 | dsize = __get_data_size(&tk->tp, regs); |
| 1166 | __size = sizeof(*entry) + tk->tp.size + dsize; |
Masami Hiramatsu | 74ebb63 | 2009-09-14 16:49:28 -0400 | [diff] [blame] | 1167 | size = ALIGN(__size + sizeof(u32), sizeof(u64)); |
| 1168 | size -= sizeof(u32); |
Masami Hiramatsu | e08d1c6 | 2009-09-10 19:53:30 -0400 | [diff] [blame] | 1169 | |
Alexei Starovoitov | 1e1dcd9 | 2016-04-06 18:43:24 -0700 | [diff] [blame] | 1170 | entry = perf_trace_buf_alloc(size, NULL, &rctx); |
Xiao Guangrong | 430ad5a | 2010-01-28 09:32:29 +0800 | [diff] [blame] | 1171 | if (!entry) |
Josef Bacik | 9802d86 | 2017-12-11 11:36:48 -0500 | [diff] [blame] | 1172 | return 0; |
Frederic Weisbecker | ce71b9d | 2009-11-22 05:26:55 +0100 | [diff] [blame] | 1173 | |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 1174 | entry->ip = (unsigned long)tk->rp.kp.addr; |
Masami Hiramatsu | e09c861 | 2010-07-05 15:54:45 -0300 | [diff] [blame] | 1175 | memset(&entry[1], 0, dsize); |
Masami Hiramatsu | 9178412 | 2018-04-25 21:19:01 +0900 | [diff] [blame] | 1176 | store_trace_args(&entry[1], &tk->tp, regs, sizeof(*entry), dsize); |
Alexei Starovoitov | 1e1dcd9 | 2016-04-06 18:43:24 -0700 | [diff] [blame] | 1177 | perf_trace_buf_submit(entry, size, rctx, call->event.type, 1, regs, |
Peter Zijlstra | 8fd0fbb | 2017-10-11 09:45:29 +0200 | [diff] [blame] | 1178 | head, NULL); |
Josef Bacik | 9802d86 | 2017-12-11 11:36:48 -0500 | [diff] [blame] | 1179 | return 0; |
Masami Hiramatsu | e08d1c6 | 2009-09-10 19:53:30 -0400 | [diff] [blame] | 1180 | } |
Masami Hiramatsu | 3da0f18 | 2014-04-17 17:18:28 +0900 | [diff] [blame] | 1181 | NOKPROBE_SYMBOL(kprobe_perf_func); |
Masami Hiramatsu | e08d1c6 | 2009-09-10 19:53:30 -0400 | [diff] [blame] | 1182 | |
| 1183 | /* Kretprobe profile handler */ |
Masami Hiramatsu | 3da0f18 | 2014-04-17 17:18:28 +0900 | [diff] [blame] | 1184 | static void |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 1185 | kretprobe_perf_func(struct trace_kprobe *tk, struct kretprobe_instance *ri, |
Masami Hiramatsu | 2b106aa | 2013-05-09 14:44:41 +0900 | [diff] [blame] | 1186 | struct pt_regs *regs) |
Masami Hiramatsu | e08d1c6 | 2009-09-10 19:53:30 -0400 | [diff] [blame] | 1187 | { |
Steven Rostedt (Red Hat) | 2425bcb | 2015-05-05 11:45:27 -0400 | [diff] [blame] | 1188 | struct trace_event_call *call = &tk->tp.call; |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 1189 | struct kretprobe_trace_entry_head *entry; |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 1190 | struct hlist_head *head; |
Masami Hiramatsu | e09c861 | 2010-07-05 15:54:45 -0300 | [diff] [blame] | 1191 | int size, __size, dsize; |
Peter Zijlstra | 4ed7c92 | 2009-11-23 11:37:29 +0100 | [diff] [blame] | 1192 | int rctx; |
Masami Hiramatsu | e08d1c6 | 2009-09-10 19:53:30 -0400 | [diff] [blame] | 1193 | |
Yonghong Song | e87c6bc | 2017-10-23 23:53:08 -0700 | [diff] [blame] | 1194 | if (bpf_prog_array_valid(call) && !trace_call_bpf(call, regs)) |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 1195 | return; |
| 1196 | |
Oleg Nesterov | 288e984 | 2013-06-20 19:38:06 +0200 | [diff] [blame] | 1197 | head = this_cpu_ptr(call->perf_events); |
| 1198 | if (hlist_empty(head)) |
| 1199 | return; |
| 1200 | |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 1201 | dsize = __get_data_size(&tk->tp, regs); |
| 1202 | __size = sizeof(*entry) + tk->tp.size + dsize; |
Masami Hiramatsu | 74ebb63 | 2009-09-14 16:49:28 -0400 | [diff] [blame] | 1203 | size = ALIGN(__size + sizeof(u32), sizeof(u64)); |
| 1204 | size -= sizeof(u32); |
Masami Hiramatsu | e08d1c6 | 2009-09-10 19:53:30 -0400 | [diff] [blame] | 1205 | |
Alexei Starovoitov | 1e1dcd9 | 2016-04-06 18:43:24 -0700 | [diff] [blame] | 1206 | entry = perf_trace_buf_alloc(size, NULL, &rctx); |
Xiao Guangrong | 430ad5a | 2010-01-28 09:32:29 +0800 | [diff] [blame] | 1207 | if (!entry) |
Xiao Guangrong | 1e12a4a | 2010-01-28 09:34:27 +0800 | [diff] [blame] | 1208 | return; |
Frederic Weisbecker | ce71b9d | 2009-11-22 05:26:55 +0100 | [diff] [blame] | 1209 | |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 1210 | entry->func = (unsigned long)tk->rp.kp.addr; |
Masami Hiramatsu | a1a138d | 2009-09-25 11:20:12 -0700 | [diff] [blame] | 1211 | entry->ret_ip = (unsigned long)ri->ret_addr; |
Masami Hiramatsu | 9178412 | 2018-04-25 21:19:01 +0900 | [diff] [blame] | 1212 | store_trace_args(&entry[1], &tk->tp, regs, sizeof(*entry), dsize); |
Alexei Starovoitov | 1e1dcd9 | 2016-04-06 18:43:24 -0700 | [diff] [blame] | 1213 | perf_trace_buf_submit(entry, size, rctx, call->event.type, 1, regs, |
Peter Zijlstra | 8fd0fbb | 2017-10-11 09:45:29 +0200 | [diff] [blame] | 1214 | head, NULL); |
Masami Hiramatsu | e08d1c6 | 2009-09-10 19:53:30 -0400 | [diff] [blame] | 1215 | } |
Masami Hiramatsu | 3da0f18 | 2014-04-17 17:18:28 +0900 | [diff] [blame] | 1216 | NOKPROBE_SYMBOL(kretprobe_perf_func); |
Yonghong Song | 41bdc4b | 2018-05-24 11:21:09 -0700 | [diff] [blame] | 1217 | |
| 1218 | int bpf_get_kprobe_info(const struct perf_event *event, u32 *fd_type, |
| 1219 | const char **symbol, u64 *probe_offset, |
| 1220 | u64 *probe_addr, bool perf_type_tracepoint) |
| 1221 | { |
| 1222 | const char *pevent = trace_event_name(event->tp_event); |
| 1223 | const char *group = event->tp_event->class->system; |
| 1224 | struct trace_kprobe *tk; |
| 1225 | |
| 1226 | if (perf_type_tracepoint) |
| 1227 | tk = find_trace_kprobe(pevent, group); |
| 1228 | else |
| 1229 | tk = event->tp_event->data; |
| 1230 | if (!tk) |
| 1231 | return -EINVAL; |
| 1232 | |
| 1233 | *fd_type = trace_kprobe_is_return(tk) ? BPF_FD_TYPE_KRETPROBE |
| 1234 | : BPF_FD_TYPE_KPROBE; |
| 1235 | if (tk->symbol) { |
| 1236 | *symbol = tk->symbol; |
| 1237 | *probe_offset = tk->rp.kp.offset; |
| 1238 | *probe_addr = 0; |
| 1239 | } else { |
| 1240 | *symbol = NULL; |
| 1241 | *probe_offset = 0; |
| 1242 | *probe_addr = (unsigned long)tk->rp.kp.addr; |
| 1243 | } |
| 1244 | return 0; |
| 1245 | } |
Li Zefan | 07b139c | 2009-12-21 14:27:35 +0800 | [diff] [blame] | 1246 | #endif /* CONFIG_PERF_EVENTS */ |
Masami Hiramatsu | 50d7805 | 2009-09-14 16:49:20 -0400 | [diff] [blame] | 1247 | |
Oleg Nesterov | 3fe3d61 | 2013-06-20 19:38:09 +0200 | [diff] [blame] | 1248 | /* |
| 1249 | * called by perf_trace_init() or __ftrace_set_clr_event() under event_mutex. |
| 1250 | * |
| 1251 | * kprobe_trace_self_tests_init() does enable_trace_probe/disable_trace_probe |
| 1252 | * lockless, but we can't race with this __init function. |
| 1253 | */ |
Steven Rostedt (Red Hat) | 2425bcb | 2015-05-05 11:45:27 -0400 | [diff] [blame] | 1254 | static int kprobe_register(struct trace_event_call *event, |
Masami Hiramatsu | fbc1963 | 2014-04-17 17:18:00 +0900 | [diff] [blame] | 1255 | enum trace_reg type, void *data) |
Steven Rostedt | 2239291 | 2010-04-21 12:27:06 -0400 | [diff] [blame] | 1256 | { |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 1257 | struct trace_kprobe *tk = (struct trace_kprobe *)event->data; |
Steven Rostedt (Red Hat) | 7f1d2f8 | 2015-05-05 10:09:53 -0400 | [diff] [blame] | 1258 | struct trace_event_file *file = data; |
Masami Hiramatsu | 1538f88 | 2011-06-27 16:26:44 +0900 | [diff] [blame] | 1259 | |
Steven Rostedt | 2239291 | 2010-04-21 12:27:06 -0400 | [diff] [blame] | 1260 | switch (type) { |
| 1261 | case TRACE_REG_REGISTER: |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 1262 | return enable_trace_kprobe(tk, file); |
Steven Rostedt | 2239291 | 2010-04-21 12:27:06 -0400 | [diff] [blame] | 1263 | case TRACE_REG_UNREGISTER: |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 1264 | return disable_trace_kprobe(tk, file); |
Steven Rostedt | 2239291 | 2010-04-21 12:27:06 -0400 | [diff] [blame] | 1265 | |
| 1266 | #ifdef CONFIG_PERF_EVENTS |
| 1267 | case TRACE_REG_PERF_REGISTER: |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 1268 | return enable_trace_kprobe(tk, NULL); |
Steven Rostedt | 2239291 | 2010-04-21 12:27:06 -0400 | [diff] [blame] | 1269 | case TRACE_REG_PERF_UNREGISTER: |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 1270 | return disable_trace_kprobe(tk, NULL); |
Jiri Olsa | ceec0b6 | 2012-02-15 15:51:49 +0100 | [diff] [blame] | 1271 | case TRACE_REG_PERF_OPEN: |
| 1272 | case TRACE_REG_PERF_CLOSE: |
Jiri Olsa | 489c75c | 2012-02-15 15:51:50 +0100 | [diff] [blame] | 1273 | case TRACE_REG_PERF_ADD: |
| 1274 | case TRACE_REG_PERF_DEL: |
Jiri Olsa | ceec0b6 | 2012-02-15 15:51:49 +0100 | [diff] [blame] | 1275 | return 0; |
Steven Rostedt | 2239291 | 2010-04-21 12:27:06 -0400 | [diff] [blame] | 1276 | #endif |
| 1277 | } |
| 1278 | return 0; |
| 1279 | } |
Masami Hiramatsu | 50d7805 | 2009-09-14 16:49:20 -0400 | [diff] [blame] | 1280 | |
Masami Hiramatsu | 3da0f18 | 2014-04-17 17:18:28 +0900 | [diff] [blame] | 1281 | static int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs) |
Masami Hiramatsu | 50d7805 | 2009-09-14 16:49:20 -0400 | [diff] [blame] | 1282 | { |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 1283 | struct trace_kprobe *tk = container_of(kp, struct trace_kprobe, rp.kp); |
Josef Bacik | 9802d86 | 2017-12-11 11:36:48 -0500 | [diff] [blame] | 1284 | int ret = 0; |
Masami Hiramatsu | 50d7805 | 2009-09-14 16:49:20 -0400 | [diff] [blame] | 1285 | |
Martin KaFai Lau | a7636d9 | 2016-02-03 12:28:28 -0800 | [diff] [blame] | 1286 | raw_cpu_inc(*tk->nhit); |
Masami Hiramatsu | 48182bd | 2013-05-09 14:44:36 +0900 | [diff] [blame] | 1287 | |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 1288 | if (tk->tp.flags & TP_FLAG_TRACE) |
| 1289 | kprobe_trace_func(tk, regs); |
Li Zefan | 07b139c | 2009-12-21 14:27:35 +0800 | [diff] [blame] | 1290 | #ifdef CONFIG_PERF_EVENTS |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 1291 | if (tk->tp.flags & TP_FLAG_PROFILE) |
Josef Bacik | 9802d86 | 2017-12-11 11:36:48 -0500 | [diff] [blame] | 1292 | ret = kprobe_perf_func(tk, regs); |
Li Zefan | 07b139c | 2009-12-21 14:27:35 +0800 | [diff] [blame] | 1293 | #endif |
Josef Bacik | 9802d86 | 2017-12-11 11:36:48 -0500 | [diff] [blame] | 1294 | return ret; |
Masami Hiramatsu | e08d1c6 | 2009-09-10 19:53:30 -0400 | [diff] [blame] | 1295 | } |
Masami Hiramatsu | 3da0f18 | 2014-04-17 17:18:28 +0900 | [diff] [blame] | 1296 | NOKPROBE_SYMBOL(kprobe_dispatcher); |
Masami Hiramatsu | e08d1c6 | 2009-09-10 19:53:30 -0400 | [diff] [blame] | 1297 | |
Masami Hiramatsu | 3da0f18 | 2014-04-17 17:18:28 +0900 | [diff] [blame] | 1298 | static int |
| 1299 | kretprobe_dispatcher(struct kretprobe_instance *ri, struct pt_regs *regs) |
Masami Hiramatsu | 50d7805 | 2009-09-14 16:49:20 -0400 | [diff] [blame] | 1300 | { |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 1301 | struct trace_kprobe *tk = container_of(ri->rp, struct trace_kprobe, rp); |
Masami Hiramatsu | 50d7805 | 2009-09-14 16:49:20 -0400 | [diff] [blame] | 1302 | |
Martin KaFai Lau | a7636d9 | 2016-02-03 12:28:28 -0800 | [diff] [blame] | 1303 | raw_cpu_inc(*tk->nhit); |
Masami Hiramatsu | 48182bd | 2013-05-09 14:44:36 +0900 | [diff] [blame] | 1304 | |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 1305 | if (tk->tp.flags & TP_FLAG_TRACE) |
| 1306 | kretprobe_trace_func(tk, ri, regs); |
Li Zefan | 07b139c | 2009-12-21 14:27:35 +0800 | [diff] [blame] | 1307 | #ifdef CONFIG_PERF_EVENTS |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 1308 | if (tk->tp.flags & TP_FLAG_PROFILE) |
| 1309 | kretprobe_perf_func(tk, ri, regs); |
Li Zefan | 07b139c | 2009-12-21 14:27:35 +0800 | [diff] [blame] | 1310 | #endif |
Masami Hiramatsu | 50d7805 | 2009-09-14 16:49:20 -0400 | [diff] [blame] | 1311 | return 0; /* We don't tweek kernel, so just return 0 */ |
| 1312 | } |
Masami Hiramatsu | 3da0f18 | 2014-04-17 17:18:28 +0900 | [diff] [blame] | 1313 | NOKPROBE_SYMBOL(kretprobe_dispatcher); |
Masami Hiramatsu | e08d1c6 | 2009-09-10 19:53:30 -0400 | [diff] [blame] | 1314 | |
Steven Rostedt | a9a5776 | 2010-04-22 18:46:14 -0400 | [diff] [blame] | 1315 | static struct trace_event_functions kretprobe_funcs = { |
| 1316 | .trace = print_kretprobe_event |
| 1317 | }; |
| 1318 | |
| 1319 | static struct trace_event_functions kprobe_funcs = { |
| 1320 | .trace = print_kprobe_event |
| 1321 | }; |
| 1322 | |
Song Liu | e12f03d | 2017-12-06 14:45:15 -0800 | [diff] [blame] | 1323 | static inline void init_trace_event_call(struct trace_kprobe *tk, |
| 1324 | struct trace_event_call *call) |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1325 | { |
Li Zefan | ffb9f99 | 2010-05-24 16:24:52 +0800 | [diff] [blame] | 1326 | INIT_LIST_HEAD(&call->class->fields); |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 1327 | if (trace_kprobe_is_return(tk)) { |
Steven Rostedt | 80decc7 | 2010-04-23 10:00:22 -0400 | [diff] [blame] | 1328 | call->event.funcs = &kretprobe_funcs; |
Steven Rostedt | 2e33af0 | 2010-04-22 10:35:55 -0400 | [diff] [blame] | 1329 | call->class->define_fields = kretprobe_event_define_fields; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1330 | } else { |
Steven Rostedt | 80decc7 | 2010-04-23 10:00:22 -0400 | [diff] [blame] | 1331 | call->event.funcs = &kprobe_funcs; |
Steven Rostedt | 2e33af0 | 2010-04-22 10:35:55 -0400 | [diff] [blame] | 1332 | call->class->define_fields = kprobe_event_define_fields; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1333 | } |
Song Liu | e12f03d | 2017-12-06 14:45:15 -0800 | [diff] [blame] | 1334 | |
| 1335 | call->flags = TRACE_EVENT_FL_KPROBE; |
| 1336 | call->class->reg = kprobe_register; |
| 1337 | call->data = tk; |
| 1338 | } |
| 1339 | |
| 1340 | static int register_kprobe_event(struct trace_kprobe *tk) |
| 1341 | { |
| 1342 | struct trace_event_call *call = &tk->tp.call; |
| 1343 | int ret = 0; |
| 1344 | |
| 1345 | init_trace_event_call(tk, call); |
| 1346 | |
Masami Hiramatsu | 0a46c85 | 2018-04-25 21:19:30 +0900 | [diff] [blame] | 1347 | if (traceprobe_set_print_fmt(&tk->tp, trace_kprobe_is_return(tk)) < 0) |
Lai Jiangshan | a342a028 | 2009-12-15 15:39:49 +0800 | [diff] [blame] | 1348 | return -ENOMEM; |
Steven Rostedt (Red Hat) | 9023c93 | 2015-05-05 09:39:12 -0400 | [diff] [blame] | 1349 | ret = register_trace_event(&call->event); |
Steven Rostedt | 32c0eda | 2010-04-23 10:38:03 -0400 | [diff] [blame] | 1350 | if (!ret) { |
Lai Jiangshan | a342a028 | 2009-12-15 15:39:49 +0800 | [diff] [blame] | 1351 | kfree(call->print_fmt); |
Masami Hiramatsu | ff50d99 | 2009-08-13 16:35:34 -0400 | [diff] [blame] | 1352 | return -ENODEV; |
Lai Jiangshan | a342a028 | 2009-12-15 15:39:49 +0800 | [diff] [blame] | 1353 | } |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1354 | ret = trace_add_event_call(call); |
Masami Hiramatsu | ff50d99 | 2009-08-13 16:35:34 -0400 | [diff] [blame] | 1355 | if (ret) { |
Mathieu Desnoyers | de7b297 | 2014-04-08 17:26:21 -0400 | [diff] [blame] | 1356 | pr_info("Failed to register kprobe event: %s\n", |
Steven Rostedt (Red Hat) | 687fcc4 | 2015-05-13 14:20:14 -0400 | [diff] [blame] | 1357 | trace_event_name(call)); |
Lai Jiangshan | a342a028 | 2009-12-15 15:39:49 +0800 | [diff] [blame] | 1358 | kfree(call->print_fmt); |
Steven Rostedt (Red Hat) | 9023c93 | 2015-05-05 09:39:12 -0400 | [diff] [blame] | 1359 | unregister_trace_event(&call->event); |
Masami Hiramatsu | ff50d99 | 2009-08-13 16:35:34 -0400 | [diff] [blame] | 1360 | } |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1361 | return ret; |
| 1362 | } |
| 1363 | |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 1364 | static int unregister_kprobe_event(struct trace_kprobe *tk) |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1365 | { |
Steven Rostedt (Red Hat) | 40c3259 | 2013-07-03 23:33:50 -0400 | [diff] [blame] | 1366 | int ret; |
| 1367 | |
Masami Hiramatsu | ff50d99 | 2009-08-13 16:35:34 -0400 | [diff] [blame] | 1368 | /* tp->event is unregistered in trace_remove_event_call() */ |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 1369 | ret = trace_remove_event_call(&tk->tp.call); |
Steven Rostedt (Red Hat) | 40c3259 | 2013-07-03 23:33:50 -0400 | [diff] [blame] | 1370 | if (!ret) |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 1371 | kfree(tk->tp.call.print_fmt); |
Steven Rostedt (Red Hat) | 40c3259 | 2013-07-03 23:33:50 -0400 | [diff] [blame] | 1372 | return ret; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1373 | } |
| 1374 | |
Song Liu | e12f03d | 2017-12-06 14:45:15 -0800 | [diff] [blame] | 1375 | #ifdef CONFIG_PERF_EVENTS |
| 1376 | /* create a trace_kprobe, but don't add it to global lists */ |
| 1377 | struct trace_event_call * |
| 1378 | create_local_trace_kprobe(char *func, void *addr, unsigned long offs, |
| 1379 | bool is_return) |
| 1380 | { |
| 1381 | struct trace_kprobe *tk; |
| 1382 | int ret; |
| 1383 | char *event; |
| 1384 | |
| 1385 | /* |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 1386 | * local trace_kprobes are not added to dyn_event, so they are never |
Song Liu | e12f03d | 2017-12-06 14:45:15 -0800 | [diff] [blame] | 1387 | * searched in find_trace_kprobe(). Therefore, there is no concern of |
| 1388 | * duplicated name here. |
| 1389 | */ |
| 1390 | event = func ? func : "DUMMY_EVENT"; |
| 1391 | |
| 1392 | tk = alloc_trace_kprobe(KPROBE_EVENT_SYSTEM, event, (void *)addr, func, |
| 1393 | offs, 0 /* maxactive */, 0 /* nargs */, |
| 1394 | is_return); |
| 1395 | |
| 1396 | if (IS_ERR(tk)) { |
| 1397 | pr_info("Failed to allocate trace_probe.(%d)\n", |
| 1398 | (int)PTR_ERR(tk)); |
| 1399 | return ERR_CAST(tk); |
| 1400 | } |
| 1401 | |
| 1402 | init_trace_event_call(tk, &tk->tp.call); |
| 1403 | |
Masami Hiramatsu | 0a46c85 | 2018-04-25 21:19:30 +0900 | [diff] [blame] | 1404 | if (traceprobe_set_print_fmt(&tk->tp, trace_kprobe_is_return(tk)) < 0) { |
Song Liu | e12f03d | 2017-12-06 14:45:15 -0800 | [diff] [blame] | 1405 | ret = -ENOMEM; |
| 1406 | goto error; |
| 1407 | } |
| 1408 | |
| 1409 | ret = __register_trace_kprobe(tk); |
Jiri Olsa | 0fc8c35 | 2018-07-09 16:19:06 +0200 | [diff] [blame] | 1410 | if (ret < 0) { |
| 1411 | kfree(tk->tp.call.print_fmt); |
Song Liu | e12f03d | 2017-12-06 14:45:15 -0800 | [diff] [blame] | 1412 | goto error; |
Jiri Olsa | 0fc8c35 | 2018-07-09 16:19:06 +0200 | [diff] [blame] | 1413 | } |
Song Liu | e12f03d | 2017-12-06 14:45:15 -0800 | [diff] [blame] | 1414 | |
| 1415 | return &tk->tp.call; |
| 1416 | error: |
| 1417 | free_trace_kprobe(tk); |
| 1418 | return ERR_PTR(ret); |
| 1419 | } |
| 1420 | |
| 1421 | void destroy_local_trace_kprobe(struct trace_event_call *event_call) |
| 1422 | { |
| 1423 | struct trace_kprobe *tk; |
| 1424 | |
| 1425 | tk = container_of(event_call, struct trace_kprobe, tp.call); |
| 1426 | |
| 1427 | if (trace_probe_is_enabled(&tk->tp)) { |
| 1428 | WARN_ON(1); |
| 1429 | return; |
| 1430 | } |
| 1431 | |
| 1432 | __unregister_trace_kprobe(tk); |
Jiri Olsa | 0fc8c35 | 2018-07-09 16:19:06 +0200 | [diff] [blame] | 1433 | |
| 1434 | kfree(tk->tp.call.print_fmt); |
Song Liu | e12f03d | 2017-12-06 14:45:15 -0800 | [diff] [blame] | 1435 | free_trace_kprobe(tk); |
| 1436 | } |
| 1437 | #endif /* CONFIG_PERF_EVENTS */ |
| 1438 | |
Steven Rostedt (Red Hat) | 8434dc9 | 2015-01-20 12:13:40 -0500 | [diff] [blame] | 1439 | /* Make a tracefs interface for controlling probe points */ |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1440 | static __init int init_kprobe_trace(void) |
| 1441 | { |
| 1442 | struct dentry *d_tracer; |
| 1443 | struct dentry *entry; |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 1444 | int ret; |
| 1445 | |
| 1446 | ret = dyn_event_register(&trace_kprobe_ops); |
| 1447 | if (ret) |
| 1448 | return ret; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1449 | |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 1450 | if (register_module_notifier(&trace_kprobe_module_nb)) |
Masami Hiramatsu | 6142431 | 2011-06-27 16:26:56 +0900 | [diff] [blame] | 1451 | return -EINVAL; |
| 1452 | |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1453 | d_tracer = tracing_init_dentry(); |
Steven Rostedt (Red Hat) | 14a5ae4 | 2015-01-20 11:14:16 -0500 | [diff] [blame] | 1454 | if (IS_ERR(d_tracer)) |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1455 | return 0; |
| 1456 | |
Steven Rostedt (Red Hat) | 8434dc9 | 2015-01-20 12:13:40 -0500 | [diff] [blame] | 1457 | entry = tracefs_create_file("kprobe_events", 0644, d_tracer, |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1458 | NULL, &kprobe_events_ops); |
| 1459 | |
Masami Hiramatsu | cd7e7bd | 2009-08-13 16:35:42 -0400 | [diff] [blame] | 1460 | /* Event list interface */ |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1461 | if (!entry) |
Joe Perches | a395d6a | 2016-03-22 14:28:09 -0700 | [diff] [blame] | 1462 | pr_warn("Could not create tracefs 'kprobe_events' entry\n"); |
Masami Hiramatsu | cd7e7bd | 2009-08-13 16:35:42 -0400 | [diff] [blame] | 1463 | |
| 1464 | /* Profile interface */ |
Steven Rostedt (Red Hat) | 8434dc9 | 2015-01-20 12:13:40 -0500 | [diff] [blame] | 1465 | entry = tracefs_create_file("kprobe_profile", 0444, d_tracer, |
Masami Hiramatsu | cd7e7bd | 2009-08-13 16:35:42 -0400 | [diff] [blame] | 1466 | NULL, &kprobe_profile_ops); |
| 1467 | |
| 1468 | if (!entry) |
Joe Perches | a395d6a | 2016-03-22 14:28:09 -0700 | [diff] [blame] | 1469 | pr_warn("Could not create tracefs 'kprobe_profile' entry\n"); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1470 | return 0; |
| 1471 | } |
| 1472 | fs_initcall(init_kprobe_trace); |
| 1473 | |
| 1474 | |
| 1475 | #ifdef CONFIG_FTRACE_STARTUP_TEST |
Arnd Bergmann | 26a346f | 2017-02-01 17:57:56 +0100 | [diff] [blame] | 1476 | static __init struct trace_event_file * |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 1477 | find_trace_probe_file(struct trace_kprobe *tk, struct trace_array *tr) |
Masami Hiramatsu | 41a7dd4 | 2013-05-09 14:44:49 +0900 | [diff] [blame] | 1478 | { |
Steven Rostedt (Red Hat) | 7f1d2f8 | 2015-05-05 10:09:53 -0400 | [diff] [blame] | 1479 | struct trace_event_file *file; |
Masami Hiramatsu | 41a7dd4 | 2013-05-09 14:44:49 +0900 | [diff] [blame] | 1480 | |
| 1481 | list_for_each_entry(file, &tr->events, list) |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 1482 | if (file->event_call == &tk->tp.call) |
Masami Hiramatsu | 41a7dd4 | 2013-05-09 14:44:49 +0900 | [diff] [blame] | 1483 | return file; |
| 1484 | |
| 1485 | return NULL; |
| 1486 | } |
| 1487 | |
Oleg Nesterov | 3fe3d61 | 2013-06-20 19:38:09 +0200 | [diff] [blame] | 1488 | /* |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 1489 | * Nobody but us can call enable_trace_kprobe/disable_trace_kprobe at this |
Oleg Nesterov | 3fe3d61 | 2013-06-20 19:38:09 +0200 | [diff] [blame] | 1490 | * stage, we can do this lockless. |
| 1491 | */ |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1492 | static __init int kprobe_trace_self_tests_init(void) |
| 1493 | { |
Masami Hiramatsu | 231e36f | 2010-01-14 00:12:12 -0500 | [diff] [blame] | 1494 | int ret, warn = 0; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1495 | int (*target)(int, int, int, int, int, int); |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 1496 | struct trace_kprobe *tk; |
Steven Rostedt (Red Hat) | 7f1d2f8 | 2015-05-05 10:09:53 -0400 | [diff] [blame] | 1497 | struct trace_event_file *file; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1498 | |
Yoshihiro YUNOMAE | 748ec3a | 2014-06-06 07:35:20 +0900 | [diff] [blame] | 1499 | if (tracing_is_disabled()) |
| 1500 | return -ENODEV; |
| 1501 | |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1502 | target = kprobe_trace_selftest_target; |
| 1503 | |
| 1504 | pr_info("Testing kprobe tracing: "); |
| 1505 | |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 1506 | ret = trace_run_command("p:testprobe kprobe_trace_selftest_target $stack $stack0 +0($stack)", |
| 1507 | create_or_delete_trace_kprobe); |
Masami Hiramatsu | 231e36f | 2010-01-14 00:12:12 -0500 | [diff] [blame] | 1508 | if (WARN_ON_ONCE(ret)) { |
Masami Hiramatsu | 41a7dd4 | 2013-05-09 14:44:49 +0900 | [diff] [blame] | 1509 | pr_warn("error on probing function entry.\n"); |
Masami Hiramatsu | 231e36f | 2010-01-14 00:12:12 -0500 | [diff] [blame] | 1510 | warn++; |
| 1511 | } else { |
| 1512 | /* Enable trace point */ |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 1513 | tk = find_trace_kprobe("testprobe", KPROBE_EVENT_SYSTEM); |
| 1514 | if (WARN_ON_ONCE(tk == NULL)) { |
Masami Hiramatsu | 41a7dd4 | 2013-05-09 14:44:49 +0900 | [diff] [blame] | 1515 | pr_warn("error on getting new probe.\n"); |
Masami Hiramatsu | 231e36f | 2010-01-14 00:12:12 -0500 | [diff] [blame] | 1516 | warn++; |
Masami Hiramatsu | 41a7dd4 | 2013-05-09 14:44:49 +0900 | [diff] [blame] | 1517 | } else { |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 1518 | file = find_trace_probe_file(tk, top_trace_array()); |
Masami Hiramatsu | 41a7dd4 | 2013-05-09 14:44:49 +0900 | [diff] [blame] | 1519 | if (WARN_ON_ONCE(file == NULL)) { |
| 1520 | pr_warn("error on getting probe file.\n"); |
| 1521 | warn++; |
| 1522 | } else |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 1523 | enable_trace_kprobe(tk, file); |
Masami Hiramatsu | 41a7dd4 | 2013-05-09 14:44:49 +0900 | [diff] [blame] | 1524 | } |
Masami Hiramatsu | 231e36f | 2010-01-14 00:12:12 -0500 | [diff] [blame] | 1525 | } |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1526 | |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 1527 | ret = trace_run_command("r:testprobe2 kprobe_trace_selftest_target $retval", |
| 1528 | create_or_delete_trace_kprobe); |
Masami Hiramatsu | 231e36f | 2010-01-14 00:12:12 -0500 | [diff] [blame] | 1529 | if (WARN_ON_ONCE(ret)) { |
Masami Hiramatsu | 41a7dd4 | 2013-05-09 14:44:49 +0900 | [diff] [blame] | 1530 | pr_warn("error on probing function return.\n"); |
Masami Hiramatsu | 231e36f | 2010-01-14 00:12:12 -0500 | [diff] [blame] | 1531 | warn++; |
| 1532 | } else { |
| 1533 | /* Enable trace point */ |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 1534 | tk = find_trace_kprobe("testprobe2", KPROBE_EVENT_SYSTEM); |
| 1535 | if (WARN_ON_ONCE(tk == NULL)) { |
Masami Hiramatsu | 41a7dd4 | 2013-05-09 14:44:49 +0900 | [diff] [blame] | 1536 | pr_warn("error on getting 2nd new probe.\n"); |
Masami Hiramatsu | 231e36f | 2010-01-14 00:12:12 -0500 | [diff] [blame] | 1537 | warn++; |
Masami Hiramatsu | 41a7dd4 | 2013-05-09 14:44:49 +0900 | [diff] [blame] | 1538 | } else { |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 1539 | file = find_trace_probe_file(tk, top_trace_array()); |
Masami Hiramatsu | 41a7dd4 | 2013-05-09 14:44:49 +0900 | [diff] [blame] | 1540 | if (WARN_ON_ONCE(file == NULL)) { |
| 1541 | pr_warn("error on getting probe file.\n"); |
| 1542 | warn++; |
| 1543 | } else |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 1544 | enable_trace_kprobe(tk, file); |
Masami Hiramatsu | 41a7dd4 | 2013-05-09 14:44:49 +0900 | [diff] [blame] | 1545 | } |
Masami Hiramatsu | 231e36f | 2010-01-14 00:12:12 -0500 | [diff] [blame] | 1546 | } |
| 1547 | |
| 1548 | if (warn) |
| 1549 | goto end; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1550 | |
| 1551 | ret = target(1, 2, 3, 4, 5, 6); |
| 1552 | |
Marcin Nowakowski | d4d7ccc | 2016-12-09 15:19:38 +0100 | [diff] [blame] | 1553 | /* |
| 1554 | * Not expecting an error here, the check is only to prevent the |
| 1555 | * optimizer from removing the call to target() as otherwise there |
| 1556 | * are no side-effects and the call is never performed. |
| 1557 | */ |
| 1558 | if (ret != 21) |
| 1559 | warn++; |
| 1560 | |
Masami Hiramatsu | 02ca152 | 2011-10-04 19:44:38 +0900 | [diff] [blame] | 1561 | /* Disable trace points before removing it */ |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 1562 | tk = find_trace_kprobe("testprobe", KPROBE_EVENT_SYSTEM); |
| 1563 | if (WARN_ON_ONCE(tk == NULL)) { |
Masami Hiramatsu | 41a7dd4 | 2013-05-09 14:44:49 +0900 | [diff] [blame] | 1564 | pr_warn("error on getting test probe.\n"); |
Masami Hiramatsu | 02ca152 | 2011-10-04 19:44:38 +0900 | [diff] [blame] | 1565 | warn++; |
Masami Hiramatsu | 41a7dd4 | 2013-05-09 14:44:49 +0900 | [diff] [blame] | 1566 | } else { |
Marcin Nowakowski | d4d7ccc | 2016-12-09 15:19:38 +0100 | [diff] [blame] | 1567 | if (trace_kprobe_nhit(tk) != 1) { |
| 1568 | pr_warn("incorrect number of testprobe hits\n"); |
| 1569 | warn++; |
| 1570 | } |
| 1571 | |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 1572 | file = find_trace_probe_file(tk, top_trace_array()); |
Masami Hiramatsu | 41a7dd4 | 2013-05-09 14:44:49 +0900 | [diff] [blame] | 1573 | if (WARN_ON_ONCE(file == NULL)) { |
| 1574 | pr_warn("error on getting probe file.\n"); |
| 1575 | warn++; |
| 1576 | } else |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 1577 | disable_trace_kprobe(tk, file); |
Masami Hiramatsu | 41a7dd4 | 2013-05-09 14:44:49 +0900 | [diff] [blame] | 1578 | } |
Masami Hiramatsu | 02ca152 | 2011-10-04 19:44:38 +0900 | [diff] [blame] | 1579 | |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 1580 | tk = find_trace_kprobe("testprobe2", KPROBE_EVENT_SYSTEM); |
| 1581 | if (WARN_ON_ONCE(tk == NULL)) { |
Masami Hiramatsu | 41a7dd4 | 2013-05-09 14:44:49 +0900 | [diff] [blame] | 1582 | pr_warn("error on getting 2nd test probe.\n"); |
Masami Hiramatsu | 02ca152 | 2011-10-04 19:44:38 +0900 | [diff] [blame] | 1583 | warn++; |
Masami Hiramatsu | 41a7dd4 | 2013-05-09 14:44:49 +0900 | [diff] [blame] | 1584 | } else { |
Marcin Nowakowski | d4d7ccc | 2016-12-09 15:19:38 +0100 | [diff] [blame] | 1585 | if (trace_kprobe_nhit(tk) != 1) { |
| 1586 | pr_warn("incorrect number of testprobe2 hits\n"); |
| 1587 | warn++; |
| 1588 | } |
| 1589 | |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 1590 | file = find_trace_probe_file(tk, top_trace_array()); |
Masami Hiramatsu | 41a7dd4 | 2013-05-09 14:44:49 +0900 | [diff] [blame] | 1591 | if (WARN_ON_ONCE(file == NULL)) { |
| 1592 | pr_warn("error on getting probe file.\n"); |
| 1593 | warn++; |
| 1594 | } else |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 1595 | disable_trace_kprobe(tk, file); |
Masami Hiramatsu | 41a7dd4 | 2013-05-09 14:44:49 +0900 | [diff] [blame] | 1596 | } |
Masami Hiramatsu | 02ca152 | 2011-10-04 19:44:38 +0900 | [diff] [blame] | 1597 | |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 1598 | ret = trace_run_command("-:testprobe", create_or_delete_trace_kprobe); |
Masami Hiramatsu | 231e36f | 2010-01-14 00:12:12 -0500 | [diff] [blame] | 1599 | if (WARN_ON_ONCE(ret)) { |
Masami Hiramatsu | 41a7dd4 | 2013-05-09 14:44:49 +0900 | [diff] [blame] | 1600 | pr_warn("error on deleting a probe.\n"); |
Masami Hiramatsu | 231e36f | 2010-01-14 00:12:12 -0500 | [diff] [blame] | 1601 | warn++; |
| 1602 | } |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1603 | |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 1604 | ret = trace_run_command("-:testprobe2", create_or_delete_trace_kprobe); |
Masami Hiramatsu | 231e36f | 2010-01-14 00:12:12 -0500 | [diff] [blame] | 1605 | if (WARN_ON_ONCE(ret)) { |
Masami Hiramatsu | 41a7dd4 | 2013-05-09 14:44:49 +0900 | [diff] [blame] | 1606 | pr_warn("error on deleting a probe.\n"); |
Masami Hiramatsu | 231e36f | 2010-01-14 00:12:12 -0500 | [diff] [blame] | 1607 | warn++; |
| 1608 | } |
| 1609 | |
| 1610 | end: |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 1611 | ret = dyn_events_release_all(&trace_kprobe_ops); |
| 1612 | if (WARN_ON_ONCE(ret)) { |
| 1613 | pr_warn("error on cleaning up probes.\n"); |
| 1614 | warn++; |
| 1615 | } |
Thomas Gleixner | 30e7d894 | 2017-05-17 10:19:49 +0200 | [diff] [blame] | 1616 | /* |
| 1617 | * Wait for the optimizer work to finish. Otherwise it might fiddle |
| 1618 | * with probes in already freed __init text. |
| 1619 | */ |
| 1620 | wait_for_kprobe_optimizer(); |
Masami Hiramatsu | 231e36f | 2010-01-14 00:12:12 -0500 | [diff] [blame] | 1621 | if (warn) |
| 1622 | pr_cont("NG: Some tests are failed. Please check them.\n"); |
| 1623 | else |
| 1624 | pr_cont("OK\n"); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1625 | return 0; |
| 1626 | } |
| 1627 | |
| 1628 | late_initcall(kprobe_trace_self_tests_init); |
| 1629 | |
| 1630 | #endif |