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 | |
Jakub Kicinski | aef2fed | 2021-12-15 18:55:37 -0800 | [diff] [blame] | 10 | #include <linux/bpf-cgroup.h> |
Steven Rostedt (VMware) | 17911ff | 2019-10-11 17:22:50 -0400 | [diff] [blame] | 11 | #include <linux/security.h> |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 12 | #include <linux/module.h> |
| 13 | #include <linux/uaccess.h> |
Ingo Molnar | b2d0910 | 2017-02-04 01:27:20 +0100 | [diff] [blame] | 14 | #include <linux/rculist.h> |
Masami Hiramatsu | 540adea | 2018-01-13 02:55:03 +0900 | [diff] [blame] | 15 | #include <linux/error-injection.h> |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 16 | |
Masami Hiramatsu | 970988e | 2019-05-22 17:32:35 +0900 | [diff] [blame] | 17 | #include <asm/setup.h> /* for COMMAND_LINE_SIZE */ |
| 18 | |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 19 | #include "trace_dynevent.h" |
Francis Deslauriers | d899926 | 2018-07-30 19:20:42 +0900 | [diff] [blame] | 20 | #include "trace_kprobe_selftest.h" |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 21 | #include "trace_probe.h" |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 22 | #include "trace_probe_tmpl.h" |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 23 | |
Masami Hiramatsu | f52487e | 2009-09-10 19:53:53 -0400 | [diff] [blame] | 24 | #define KPROBE_EVENT_SYSTEM "kprobes" |
Alban Crequy | 696ced4 | 2017-04-03 12:36:22 +0200 | [diff] [blame] | 25 | #define KRETPROBE_MAXACTIVE_MAX 4096 |
Masami Hiramatsu | 970988e | 2019-05-22 17:32:35 +0900 | [diff] [blame] | 26 | |
| 27 | /* Kprobe early definition from command line */ |
| 28 | static char kprobe_boot_events_buf[COMMAND_LINE_SIZE] __initdata; |
| 29 | |
| 30 | static int __init set_kprobe_boot_events(char *str) |
| 31 | { |
| 32 | strlcpy(kprobe_boot_events_buf, str, COMMAND_LINE_SIZE); |
Masami Hiramatsu | 60efe21 | 2020-12-08 17:54:09 +0900 | [diff] [blame] | 33 | disable_tracing_selftest("running kprobe events"); |
| 34 | |
Masami Hiramatsu | 970988e | 2019-05-22 17:32:35 +0900 | [diff] [blame] | 35 | return 0; |
| 36 | } |
| 37 | __setup("kprobe_event=", set_kprobe_boot_events); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 38 | |
Masami Hiramatsu | d262271 | 2021-02-01 13:48:11 -0600 | [diff] [blame] | 39 | static int trace_kprobe_create(const char *raw_command); |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 40 | static int trace_kprobe_show(struct seq_file *m, struct dyn_event *ev); |
| 41 | static int trace_kprobe_release(struct dyn_event *ev); |
| 42 | static bool trace_kprobe_is_busy(struct dyn_event *ev); |
| 43 | static bool trace_kprobe_match(const char *system, const char *event, |
Masami Hiramatsu | 3019913 | 2019-06-20 00:07:39 +0900 | [diff] [blame] | 44 | int argc, const char **argv, struct dyn_event *ev); |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 45 | |
| 46 | static struct dyn_event_operations trace_kprobe_ops = { |
| 47 | .create = trace_kprobe_create, |
| 48 | .show = trace_kprobe_show, |
| 49 | .is_busy = trace_kprobe_is_busy, |
| 50 | .free = trace_kprobe_release, |
| 51 | .match = trace_kprobe_match, |
| 52 | }; |
| 53 | |
Valdis Klētnieks | cede666 | 2019-03-12 04:58:32 -0400 | [diff] [blame] | 54 | /* |
Masami Hiramatsu | 77b44d1 | 2009-11-03 19:12:47 -0500 | [diff] [blame] | 55 | * Kprobe event core functions |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 56 | */ |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 57 | struct trace_kprobe { |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 58 | struct dyn_event devent; |
Masami Hiramatsu | 4a846b4 | 2009-09-11 05:31:21 +0200 | [diff] [blame] | 59 | struct kretprobe rp; /* Use rp.kp for kprobe use */ |
Martin KaFai Lau | a7636d9 | 2016-02-03 12:28:28 -0800 | [diff] [blame] | 60 | unsigned long __percpu *nhit; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 61 | const char *symbol; /* symbol name */ |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 62 | struct trace_probe tp; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 63 | }; |
| 64 | |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 65 | static bool is_trace_kprobe(struct dyn_event *ev) |
| 66 | { |
| 67 | return ev->ops == &trace_kprobe_ops; |
| 68 | } |
| 69 | |
| 70 | static struct trace_kprobe *to_trace_kprobe(struct dyn_event *ev) |
| 71 | { |
| 72 | return container_of(ev, struct trace_kprobe, devent); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * for_each_trace_kprobe - iterate over the trace_kprobe list |
| 77 | * @pos: the struct trace_kprobe * for each entry |
| 78 | * @dpos: the struct dyn_event * to use as a loop cursor |
| 79 | */ |
| 80 | #define for_each_trace_kprobe(pos, dpos) \ |
| 81 | for_each_dyn_event(dpos) \ |
| 82 | if (is_trace_kprobe(dpos) && (pos = to_trace_kprobe(dpos))) |
| 83 | |
Masami Hiramatsu | 3da0f18 | 2014-04-17 17:18:28 +0900 | [diff] [blame] | 84 | static nokprobe_inline bool trace_kprobe_is_return(struct trace_kprobe *tk) |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 85 | { |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 86 | return tk->rp.handler != NULL; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 87 | } |
| 88 | |
Masami Hiramatsu | 3da0f18 | 2014-04-17 17:18:28 +0900 | [diff] [blame] | 89 | static nokprobe_inline const char *trace_kprobe_symbol(struct trace_kprobe *tk) |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 90 | { |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 91 | return tk->symbol ? tk->symbol : "unknown"; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 92 | } |
| 93 | |
Masami Hiramatsu | 3da0f18 | 2014-04-17 17:18:28 +0900 | [diff] [blame] | 94 | static nokprobe_inline unsigned long trace_kprobe_offset(struct trace_kprobe *tk) |
Masami Hiramatsu | 6142431 | 2011-06-27 16:26:56 +0900 | [diff] [blame] | 95 | { |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 96 | return tk->rp.kp.offset; |
Masami Hiramatsu | 6142431 | 2011-06-27 16:26:56 +0900 | [diff] [blame] | 97 | } |
| 98 | |
Masami Hiramatsu | 3da0f18 | 2014-04-17 17:18:28 +0900 | [diff] [blame] | 99 | static nokprobe_inline bool trace_kprobe_has_gone(struct trace_kprobe *tk) |
Masami Hiramatsu | 6142431 | 2011-06-27 16:26:56 +0900 | [diff] [blame] | 100 | { |
Masami Hiramatsu | 29e8077a | 2021-09-14 23:40:16 +0900 | [diff] [blame] | 101 | return kprobe_gone(&tk->rp.kp); |
Masami Hiramatsu | 6142431 | 2011-06-27 16:26:56 +0900 | [diff] [blame] | 102 | } |
| 103 | |
Masami Hiramatsu | 3da0f18 | 2014-04-17 17:18:28 +0900 | [diff] [blame] | 104 | static nokprobe_inline bool trace_kprobe_within_module(struct trace_kprobe *tk, |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 105 | struct module *mod) |
Masami Hiramatsu | 6142431 | 2011-06-27 16:26:56 +0900 | [diff] [blame] | 106 | { |
Jarkko Sakkinen | f3d3642 | 2020-08-18 08:08:57 +0300 | [diff] [blame] | 107 | int len = strlen(module_name(mod)); |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 108 | const char *name = trace_kprobe_symbol(tk); |
Jarkko Sakkinen | f3d3642 | 2020-08-18 08:08:57 +0300 | [diff] [blame] | 109 | |
| 110 | return strncmp(module_name(mod), name, len) == 0 && name[len] == ':'; |
Masami Hiramatsu | 6142431 | 2011-06-27 16:26:56 +0900 | [diff] [blame] | 111 | } |
| 112 | |
Masami Hiramatsu | 59158ec | 2018-08-29 01:18:15 +0900 | [diff] [blame] | 113 | static nokprobe_inline bool trace_kprobe_module_exist(struct trace_kprobe *tk) |
Masami Hiramatsu | 6142431 | 2011-06-27 16:26:56 +0900 | [diff] [blame] | 114 | { |
Masami Hiramatsu | 59158ec | 2018-08-29 01:18:15 +0900 | [diff] [blame] | 115 | char *p; |
| 116 | bool ret; |
| 117 | |
| 118 | if (!tk->symbol) |
| 119 | return false; |
| 120 | p = strchr(tk->symbol, ':'); |
| 121 | if (!p) |
| 122 | return true; |
| 123 | *p = '\0'; |
Christoph Hellwig | a006050 | 2021-02-02 13:13:25 +0100 | [diff] [blame] | 124 | rcu_read_lock_sched(); |
Masami Hiramatsu | 59158ec | 2018-08-29 01:18:15 +0900 | [diff] [blame] | 125 | ret = !!find_module(tk->symbol); |
Christoph Hellwig | a006050 | 2021-02-02 13:13:25 +0100 | [diff] [blame] | 126 | rcu_read_unlock_sched(); |
Masami Hiramatsu | 59158ec | 2018-08-29 01:18:15 +0900 | [diff] [blame] | 127 | *p = ':'; |
| 128 | |
| 129 | return ret; |
Masami Hiramatsu | 6142431 | 2011-06-27 16:26:56 +0900 | [diff] [blame] | 130 | } |
| 131 | |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 132 | static bool trace_kprobe_is_busy(struct dyn_event *ev) |
| 133 | { |
| 134 | struct trace_kprobe *tk = to_trace_kprobe(ev); |
| 135 | |
| 136 | return trace_probe_is_enabled(&tk->tp); |
| 137 | } |
| 138 | |
Masami Hiramatsu | eb5bf81 | 2019-06-20 00:08:08 +0900 | [diff] [blame] | 139 | static bool trace_kprobe_match_command_head(struct trace_kprobe *tk, |
| 140 | int argc, const char **argv) |
| 141 | { |
| 142 | char buf[MAX_ARGSTR_LEN + 1]; |
| 143 | |
| 144 | if (!argc) |
| 145 | return true; |
| 146 | |
| 147 | if (!tk->symbol) |
| 148 | snprintf(buf, sizeof(buf), "0x%p", tk->rp.kp.addr); |
| 149 | else if (tk->rp.kp.offset) |
| 150 | snprintf(buf, sizeof(buf), "%s+%u", |
| 151 | trace_kprobe_symbol(tk), tk->rp.kp.offset); |
| 152 | else |
| 153 | snprintf(buf, sizeof(buf), "%s", trace_kprobe_symbol(tk)); |
| 154 | if (strcmp(buf, argv[0])) |
| 155 | return false; |
| 156 | argc--; argv++; |
| 157 | |
| 158 | return trace_probe_match_command_args(&tk->tp, argc, argv); |
| 159 | } |
| 160 | |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 161 | static bool trace_kprobe_match(const char *system, const char *event, |
Masami Hiramatsu | 3019913 | 2019-06-20 00:07:39 +0900 | [diff] [blame] | 162 | int argc, const char **argv, struct dyn_event *ev) |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 163 | { |
| 164 | struct trace_kprobe *tk = to_trace_kprobe(ev); |
| 165 | |
Masami Hiramatsu | b55ce203 | 2019-06-01 00:17:47 +0900 | [diff] [blame] | 166 | return strcmp(trace_probe_name(&tk->tp), event) == 0 && |
Masami Hiramatsu | eb5bf81 | 2019-06-20 00:08:08 +0900 | [diff] [blame] | 167 | (!system || strcmp(trace_probe_group_name(&tk->tp), system) == 0) && |
| 168 | trace_kprobe_match_command_head(tk, argc, argv); |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 169 | } |
| 170 | |
Marcin Nowakowski | f18f97a | 2016-12-09 15:19:37 +0100 | [diff] [blame] | 171 | static nokprobe_inline unsigned long trace_kprobe_nhit(struct trace_kprobe *tk) |
| 172 | { |
| 173 | unsigned long nhit = 0; |
| 174 | int cpu; |
| 175 | |
| 176 | for_each_possible_cpu(cpu) |
| 177 | nhit += *per_cpu_ptr(tk->nhit, cpu); |
| 178 | |
| 179 | return nhit; |
| 180 | } |
| 181 | |
Masami Hiramatsu | 715fa2f | 2019-06-01 00:18:07 +0900 | [diff] [blame] | 182 | static nokprobe_inline bool trace_kprobe_is_registered(struct trace_kprobe *tk) |
| 183 | { |
| 184 | return !(list_empty(&tk->rp.kp.list) && |
| 185 | hlist_unhashed(&tk->rp.kp.hlist)); |
| 186 | } |
| 187 | |
Masami Hiramatsu | 6bc6c77 | 2018-08-02 16:50:48 +0900 | [diff] [blame] | 188 | /* Return 0 if it fails to find the symbol address */ |
Masami Hiramatsu | 45408c4 | 2018-07-30 19:20:14 +0900 | [diff] [blame] | 189 | static nokprobe_inline |
| 190 | unsigned long trace_kprobe_address(struct trace_kprobe *tk) |
| 191 | { |
| 192 | unsigned long addr; |
| 193 | |
| 194 | if (tk->symbol) { |
| 195 | addr = (unsigned long) |
| 196 | kallsyms_lookup_name(trace_kprobe_symbol(tk)); |
Masami Hiramatsu | 6bc6c77 | 2018-08-02 16:50:48 +0900 | [diff] [blame] | 197 | if (addr) |
| 198 | addr += tk->rp.kp.offset; |
Masami Hiramatsu | 45408c4 | 2018-07-30 19:20:14 +0900 | [diff] [blame] | 199 | } else { |
| 200 | addr = (unsigned long)tk->rp.kp.addr; |
| 201 | } |
| 202 | return addr; |
| 203 | } |
| 204 | |
Masami Hiramatsu | 60d53e2 | 2019-06-20 00:07:20 +0900 | [diff] [blame] | 205 | static nokprobe_inline struct trace_kprobe * |
| 206 | trace_kprobe_primary_from_call(struct trace_event_call *call) |
| 207 | { |
| 208 | struct trace_probe *tp; |
| 209 | |
| 210 | tp = trace_probe_primary_from_call(call); |
| 211 | if (WARN_ON_ONCE(!tp)) |
| 212 | return NULL; |
| 213 | |
| 214 | return container_of(tp, struct trace_kprobe, tp); |
| 215 | } |
| 216 | |
Masami Hiramatsu | b4da334 | 2018-01-13 02:54:04 +0900 | [diff] [blame] | 217 | bool trace_kprobe_on_func_entry(struct trace_event_call *call) |
Josef Bacik | 9802d86 | 2017-12-11 11:36:48 -0500 | [diff] [blame] | 218 | { |
Masami Hiramatsu | 60d53e2 | 2019-06-20 00:07:20 +0900 | [diff] [blame] | 219 | struct trace_kprobe *tk = trace_kprobe_primary_from_call(call); |
Masami Hiramatsu | b4da334 | 2018-01-13 02:54:04 +0900 | [diff] [blame] | 220 | |
Masami Hiramatsu | 97c753e | 2021-01-28 00:37:51 +0900 | [diff] [blame] | 221 | return tk ? (kprobe_on_func_entry(tk->rp.kp.addr, |
Masami Hiramatsu | b4da334 | 2018-01-13 02:54:04 +0900 | [diff] [blame] | 222 | tk->rp.kp.addr ? NULL : tk->rp.kp.symbol_name, |
Masami Hiramatsu | 97c753e | 2021-01-28 00:37:51 +0900 | [diff] [blame] | 223 | tk->rp.kp.addr ? 0 : tk->rp.kp.offset) == 0) : false; |
Josef Bacik | 9802d86 | 2017-12-11 11:36:48 -0500 | [diff] [blame] | 224 | } |
| 225 | |
Masami Hiramatsu | b4da334 | 2018-01-13 02:54:04 +0900 | [diff] [blame] | 226 | bool trace_kprobe_error_injectable(struct trace_event_call *call) |
Josef Bacik | 9802d86 | 2017-12-11 11:36:48 -0500 | [diff] [blame] | 227 | { |
Masami Hiramatsu | 60d53e2 | 2019-06-20 00:07:20 +0900 | [diff] [blame] | 228 | struct trace_kprobe *tk = trace_kprobe_primary_from_call(call); |
Josef Bacik | 9802d86 | 2017-12-11 11:36:48 -0500 | [diff] [blame] | 229 | |
Masami Hiramatsu | 60d53e2 | 2019-06-20 00:07:20 +0900 | [diff] [blame] | 230 | return tk ? within_error_injection_list(trace_kprobe_address(tk)) : |
| 231 | false; |
Josef Bacik | 9802d86 | 2017-12-11 11:36:48 -0500 | [diff] [blame] | 232 | } |
| 233 | |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 234 | static int register_kprobe_event(struct trace_kprobe *tk); |
| 235 | static int unregister_kprobe_event(struct trace_kprobe *tk); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 236 | |
Masami Hiramatsu | 50d7805 | 2009-09-14 16:49:20 -0400 | [diff] [blame] | 237 | static int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs); |
| 238 | static int kretprobe_dispatcher(struct kretprobe_instance *ri, |
| 239 | struct pt_regs *regs); |
| 240 | |
Masami Hiramatsu | 455b289 | 2019-06-01 00:17:06 +0900 | [diff] [blame] | 241 | static void free_trace_kprobe(struct trace_kprobe *tk) |
| 242 | { |
| 243 | if (tk) { |
| 244 | trace_probe_cleanup(&tk->tp); |
| 245 | kfree(tk->symbol); |
| 246 | free_percpu(tk->nhit); |
| 247 | kfree(tk); |
| 248 | } |
| 249 | } |
| 250 | |
Masami Hiramatsu | 4a846b4 | 2009-09-11 05:31:21 +0200 | [diff] [blame] | 251 | /* |
| 252 | * Allocate new trace_probe and initialize it (including kprobes). |
| 253 | */ |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 254 | static struct trace_kprobe *alloc_trace_kprobe(const char *group, |
Masami Hiramatsu | f52487e | 2009-09-10 19:53:53 -0400 | [diff] [blame] | 255 | const char *event, |
Masami Hiramatsu | 4a846b4 | 2009-09-11 05:31:21 +0200 | [diff] [blame] | 256 | void *addr, |
| 257 | const char *symbol, |
| 258 | unsigned long offs, |
Alban Crequy | 696ced4 | 2017-04-03 12:36:22 +0200 | [diff] [blame] | 259 | int maxactive, |
Srikar Dronamraju | 3a6b766 | 2012-04-09 14:41:33 +0530 | [diff] [blame] | 260 | int nargs, bool is_return) |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 261 | { |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 262 | struct trace_kprobe *tk; |
Masami Hiramatsu | 6f3cf44 | 2009-12-16 17:24:08 -0500 | [diff] [blame] | 263 | int ret = -ENOMEM; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 264 | |
Steven Rostedt (VMware) | 845cbf3 | 2021-08-16 23:43:00 -0400 | [diff] [blame] | 265 | tk = kzalloc(struct_size(tk, tp.args, nargs), GFP_KERNEL); |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 266 | if (!tk) |
Masami Hiramatsu | 6f3cf44 | 2009-12-16 17:24:08 -0500 | [diff] [blame] | 267 | return ERR_PTR(ret); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 268 | |
Martin KaFai Lau | a7636d9 | 2016-02-03 12:28:28 -0800 | [diff] [blame] | 269 | tk->nhit = alloc_percpu(unsigned long); |
| 270 | if (!tk->nhit) |
| 271 | goto error; |
| 272 | |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 273 | if (symbol) { |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 274 | tk->symbol = kstrdup(symbol, GFP_KERNEL); |
| 275 | if (!tk->symbol) |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 276 | goto error; |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 277 | tk->rp.kp.symbol_name = tk->symbol; |
| 278 | tk->rp.kp.offset = offs; |
Masami Hiramatsu | 4a846b4 | 2009-09-11 05:31:21 +0200 | [diff] [blame] | 279 | } else |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 280 | tk->rp.kp.addr = addr; |
Masami Hiramatsu | 4a846b4 | 2009-09-11 05:31:21 +0200 | [diff] [blame] | 281 | |
| 282 | if (is_return) |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 283 | tk->rp.handler = kretprobe_dispatcher; |
Masami Hiramatsu | 4a846b4 | 2009-09-11 05:31:21 +0200 | [diff] [blame] | 284 | else |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 285 | tk->rp.kp.pre_handler = kprobe_dispatcher; |
Masami Hiramatsu | 4a846b4 | 2009-09-11 05:31:21 +0200 | [diff] [blame] | 286 | |
Alban Crequy | 696ced4 | 2017-04-03 12:36:22 +0200 | [diff] [blame] | 287 | tk->rp.maxactive = maxactive; |
Masami Hiramatsu | 715fa2f | 2019-06-01 00:18:07 +0900 | [diff] [blame] | 288 | INIT_HLIST_NODE(&tk->rp.kp.hlist); |
| 289 | INIT_LIST_HEAD(&tk->rp.kp.list); |
Alban Crequy | 696ced4 | 2017-04-03 12:36:22 +0200 | [diff] [blame] | 290 | |
Masami Hiramatsu | b61387c | 2020-01-22 12:23:25 +0900 | [diff] [blame] | 291 | ret = trace_probe_init(&tk->tp, event, group, false); |
Masami Hiramatsu | 455b289 | 2019-06-01 00:17:06 +0900 | [diff] [blame] | 292 | if (ret < 0) |
Masami Hiramatsu | f52487e | 2009-09-10 19:53:53 -0400 | [diff] [blame] | 293 | goto error; |
| 294 | |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 295 | dyn_event_init(&tk->devent, &trace_kprobe_ops); |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 296 | return tk; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 297 | error: |
Masami Hiramatsu | 455b289 | 2019-06-01 00:17:06 +0900 | [diff] [blame] | 298 | free_trace_kprobe(tk); |
Masami Hiramatsu | 6f3cf44 | 2009-12-16 17:24:08 -0500 | [diff] [blame] | 299 | return ERR_PTR(ret); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 300 | } |
| 301 | |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 302 | static struct trace_kprobe *find_trace_kprobe(const char *event, |
| 303 | const char *group) |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 304 | { |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 305 | struct dyn_event *pos; |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 306 | struct trace_kprobe *tk; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 307 | |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 308 | for_each_trace_kprobe(tk, pos) |
Masami Hiramatsu | b55ce203 | 2019-06-01 00:17:47 +0900 | [diff] [blame] | 309 | if (strcmp(trace_probe_name(&tk->tp), event) == 0 && |
| 310 | strcmp(trace_probe_group_name(&tk->tp), group) == 0) |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 311 | return tk; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 312 | return NULL; |
| 313 | } |
| 314 | |
Steven Rostedt (VMware) | 87107a2 | 2018-07-26 12:07:32 -0400 | [diff] [blame] | 315 | static inline int __enable_trace_kprobe(struct trace_kprobe *tk) |
| 316 | { |
| 317 | int ret = 0; |
| 318 | |
Masami Hiramatsu | 715fa2f | 2019-06-01 00:18:07 +0900 | [diff] [blame] | 319 | if (trace_kprobe_is_registered(tk) && !trace_kprobe_has_gone(tk)) { |
Steven Rostedt (VMware) | 87107a2 | 2018-07-26 12:07:32 -0400 | [diff] [blame] | 320 | if (trace_kprobe_is_return(tk)) |
| 321 | ret = enable_kretprobe(&tk->rp); |
| 322 | else |
| 323 | ret = enable_kprobe(&tk->rp.kp); |
| 324 | } |
| 325 | |
| 326 | return ret; |
| 327 | } |
| 328 | |
Masami Hiramatsu | 60d53e2 | 2019-06-20 00:07:20 +0900 | [diff] [blame] | 329 | static void __disable_trace_kprobe(struct trace_probe *tp) |
| 330 | { |
Masami Hiramatsu | 60d53e2 | 2019-06-20 00:07:20 +0900 | [diff] [blame] | 331 | struct trace_kprobe *tk; |
| 332 | |
Jiri Olsa | e161c6b | 2021-11-25 21:28:52 +0100 | [diff] [blame] | 333 | list_for_each_entry(tk, trace_probe_probe_list(tp), tp.list) { |
Masami Hiramatsu | 60d53e2 | 2019-06-20 00:07:20 +0900 | [diff] [blame] | 334 | if (!trace_kprobe_is_registered(tk)) |
| 335 | continue; |
| 336 | if (trace_kprobe_is_return(tk)) |
| 337 | disable_kretprobe(&tk->rp); |
| 338 | else |
| 339 | disable_kprobe(&tk->rp.kp); |
| 340 | } |
| 341 | } |
| 342 | |
Oleg Nesterov | 3fe3d61 | 2013-06-20 19:38:09 +0200 | [diff] [blame] | 343 | /* |
Masami Hiramatsu | 41a7dd4 | 2013-05-09 14:44:49 +0900 | [diff] [blame] | 344 | * Enable trace_probe |
| 345 | * if the file is NULL, enable "perf" handler, or enable "trace" handler. |
| 346 | */ |
Masami Hiramatsu | 60d53e2 | 2019-06-20 00:07:20 +0900 | [diff] [blame] | 347 | static int enable_trace_kprobe(struct trace_event_call *call, |
| 348 | struct trace_event_file *file) |
Masami Hiramatsu | 1538f88 | 2011-06-27 16:26:44 +0900 | [diff] [blame] | 349 | { |
Jiri Olsa | e161c6b | 2021-11-25 21:28:52 +0100 | [diff] [blame] | 350 | struct trace_probe *tp; |
Masami Hiramatsu | 60d53e2 | 2019-06-20 00:07:20 +0900 | [diff] [blame] | 351 | struct trace_kprobe *tk; |
| 352 | bool enabled; |
Masami Hiramatsu | 1538f88 | 2011-06-27 16:26:44 +0900 | [diff] [blame] | 353 | int ret = 0; |
| 354 | |
Masami Hiramatsu | 60d53e2 | 2019-06-20 00:07:20 +0900 | [diff] [blame] | 355 | tp = trace_probe_primary_from_call(call); |
| 356 | if (WARN_ON_ONCE(!tp)) |
| 357 | return -ENODEV; |
| 358 | enabled = trace_probe_is_enabled(tp); |
| 359 | |
| 360 | /* This also changes "enabled" state */ |
Masami Hiramatsu | 41a7dd4 | 2013-05-09 14:44:49 +0900 | [diff] [blame] | 361 | if (file) { |
Masami Hiramatsu | 60d53e2 | 2019-06-20 00:07:20 +0900 | [diff] [blame] | 362 | ret = trace_probe_add_file(tp, file); |
Steven Rostedt (VMware) | 87107a2 | 2018-07-26 12:07:32 -0400 | [diff] [blame] | 363 | if (ret) |
Masami Hiramatsu | b5f935e | 2019-06-01 00:17:26 +0900 | [diff] [blame] | 364 | return ret; |
| 365 | } else |
Masami Hiramatsu | 60d53e2 | 2019-06-20 00:07:20 +0900 | [diff] [blame] | 366 | trace_probe_set_flag(tp, TP_FLAG_PROFILE); |
Masami Hiramatsu | b5f935e | 2019-06-01 00:17:26 +0900 | [diff] [blame] | 367 | |
| 368 | if (enabled) |
| 369 | return 0; |
| 370 | |
Jiri Olsa | e161c6b | 2021-11-25 21:28:52 +0100 | [diff] [blame] | 371 | list_for_each_entry(tk, trace_probe_probe_list(tp), tp.list) { |
Masami Hiramatsu | 60d53e2 | 2019-06-20 00:07:20 +0900 | [diff] [blame] | 372 | if (trace_kprobe_has_gone(tk)) |
| 373 | continue; |
| 374 | ret = __enable_trace_kprobe(tk); |
Masami Hiramatsu | 44d00dc | 2019-09-18 17:55:37 +0900 | [diff] [blame] | 375 | if (ret) |
Masami Hiramatsu | 60d53e2 | 2019-06-20 00:07:20 +0900 | [diff] [blame] | 376 | break; |
Masami Hiramatsu | 60d53e2 | 2019-06-20 00:07:20 +0900 | [diff] [blame] | 377 | enabled = true; |
| 378 | } |
| 379 | |
Masami Hiramatsu | 44d00dc | 2019-09-18 17:55:37 +0900 | [diff] [blame] | 380 | if (ret) { |
| 381 | /* Failed to enable one of them. Roll back all */ |
| 382 | if (enabled) |
| 383 | __disable_trace_kprobe(tp); |
Masami Hiramatsu | b5f935e | 2019-06-01 00:17:26 +0900 | [diff] [blame] | 384 | if (file) |
Masami Hiramatsu | 60d53e2 | 2019-06-20 00:07:20 +0900 | [diff] [blame] | 385 | trace_probe_remove_file(tp, file); |
Masami Hiramatsu | b5f935e | 2019-06-01 00:17:26 +0900 | [diff] [blame] | 386 | else |
Masami Hiramatsu | 60d53e2 | 2019-06-20 00:07:20 +0900 | [diff] [blame] | 387 | trace_probe_clear_flag(tp, TP_FLAG_PROFILE); |
Artem Savkov | 57ea2a3 | 2018-07-25 16:20:38 +0200 | [diff] [blame] | 388 | } |
Masami Hiramatsu | b5f935e | 2019-06-01 00:17:26 +0900 | [diff] [blame] | 389 | |
Masami Hiramatsu | 1538f88 | 2011-06-27 16:26:44 +0900 | [diff] [blame] | 390 | return ret; |
| 391 | } |
| 392 | |
Masami Hiramatsu | 41a7dd4 | 2013-05-09 14:44:49 +0900 | [diff] [blame] | 393 | /* |
| 394 | * Disable trace_probe |
| 395 | * if the file is NULL, disable "perf" handler, or disable "trace" handler. |
| 396 | */ |
Masami Hiramatsu | 60d53e2 | 2019-06-20 00:07:20 +0900 | [diff] [blame] | 397 | static int disable_trace_kprobe(struct trace_event_call *call, |
| 398 | struct trace_event_file *file) |
Masami Hiramatsu | 41a7dd4 | 2013-05-09 14:44:49 +0900 | [diff] [blame] | 399 | { |
Masami Hiramatsu | 60d53e2 | 2019-06-20 00:07:20 +0900 | [diff] [blame] | 400 | struct trace_probe *tp; |
| 401 | |
| 402 | tp = trace_probe_primary_from_call(call); |
| 403 | if (WARN_ON_ONCE(!tp)) |
| 404 | return -ENODEV; |
Masami Hiramatsu | 41a7dd4 | 2013-05-09 14:44:49 +0900 | [diff] [blame] | 405 | |
Masami Hiramatsu | 41a7dd4 | 2013-05-09 14:44:49 +0900 | [diff] [blame] | 406 | if (file) { |
Masami Hiramatsu | b5f935e | 2019-06-01 00:17:26 +0900 | [diff] [blame] | 407 | if (!trace_probe_get_file_link(tp, file)) |
| 408 | return -ENOENT; |
| 409 | if (!trace_probe_has_single_file(tp)) |
Oleg Nesterov | 3fe3d61 | 2013-06-20 19:38:09 +0200 | [diff] [blame] | 410 | goto out; |
Masami Hiramatsu | 747774d | 2019-06-01 00:17:37 +0900 | [diff] [blame] | 411 | trace_probe_clear_flag(tp, TP_FLAG_TRACE); |
Masami Hiramatsu | 41a7dd4 | 2013-05-09 14:44:49 +0900 | [diff] [blame] | 412 | } else |
Masami Hiramatsu | 747774d | 2019-06-01 00:17:37 +0900 | [diff] [blame] | 413 | trace_probe_clear_flag(tp, TP_FLAG_PROFILE); |
Masami Hiramatsu | 41a7dd4 | 2013-05-09 14:44:49 +0900 | [diff] [blame] | 414 | |
Masami Hiramatsu | 60d53e2 | 2019-06-20 00:07:20 +0900 | [diff] [blame] | 415 | if (!trace_probe_is_enabled(tp)) |
| 416 | __disable_trace_kprobe(tp); |
Song Liu | e12f03d | 2017-12-06 14:45:15 -0800 | [diff] [blame] | 417 | |
Oleg Nesterov | 3fe3d61 | 2013-06-20 19:38:09 +0200 | [diff] [blame] | 418 | out: |
Masami Hiramatsu | b5f935e | 2019-06-01 00:17:26 +0900 | [diff] [blame] | 419 | if (file) |
Masami Hiramatsu | a232e27 | 2013-07-09 18:35:26 +0900 | [diff] [blame] | 420 | /* |
Masami Hiramatsu | b5f935e | 2019-06-01 00:17:26 +0900 | [diff] [blame] | 421 | * Synchronization is done in below function. For perf event, |
| 422 | * file == NULL and perf_trace_event_unreg() calls |
| 423 | * tracepoint_synchronize_unregister() to ensure synchronize |
| 424 | * event. We don't need to care about it. |
Masami Hiramatsu | a232e27 | 2013-07-09 18:35:26 +0900 | [diff] [blame] | 425 | */ |
Masami Hiramatsu | b5f935e | 2019-06-01 00:17:26 +0900 | [diff] [blame] | 426 | trace_probe_remove_file(tp, file); |
Masami Hiramatsu | a232e27 | 2013-07-09 18:35:26 +0900 | [diff] [blame] | 427 | |
Masami Hiramatsu | 60d53e2 | 2019-06-20 00:07:20 +0900 | [diff] [blame] | 428 | return 0; |
Masami Hiramatsu | 1538f88 | 2011-06-27 16:26:44 +0900 | [diff] [blame] | 429 | } |
| 430 | |
Masami Hiramatsu | 7bb83f6f | 2021-01-08 13:19:38 +0900 | [diff] [blame] | 431 | #if defined(CONFIG_DYNAMIC_FTRACE) && \ |
Masami Hiramatsu | 45408c4 | 2018-07-30 19:20:14 +0900 | [diff] [blame] | 432 | !defined(CONFIG_KPROBE_EVENTS_ON_NOTRACE) |
Masami Hiramatsu | c7411a1 | 2019-10-29 17:31:44 +0900 | [diff] [blame] | 433 | static bool __within_notrace_func(unsigned long addr) |
Masami Hiramatsu | 45408c4 | 2018-07-30 19:20:14 +0900 | [diff] [blame] | 434 | { |
Masami Hiramatsu | c7411a1 | 2019-10-29 17:31:44 +0900 | [diff] [blame] | 435 | unsigned long offset, size; |
Masami Hiramatsu | 45408c4 | 2018-07-30 19:20:14 +0900 | [diff] [blame] | 436 | |
Masami Hiramatsu | 6bc6c77 | 2018-08-02 16:50:48 +0900 | [diff] [blame] | 437 | if (!addr || !kallsyms_lookup_size_offset(addr, &size, &offset)) |
| 438 | return false; |
Masami Hiramatsu | 45408c4 | 2018-07-30 19:20:14 +0900 | [diff] [blame] | 439 | |
Masami Hiramatsu | 9161a86 | 2018-08-21 22:04:57 +0900 | [diff] [blame] | 440 | /* Get the entry address of the target function */ |
| 441 | addr -= offset; |
| 442 | |
| 443 | /* |
| 444 | * Since ftrace_location_range() does inclusive range check, we need |
| 445 | * to subtract 1 byte from the end address. |
| 446 | */ |
| 447 | return !ftrace_location_range(addr, addr + size - 1); |
Masami Hiramatsu | 45408c4 | 2018-07-30 19:20:14 +0900 | [diff] [blame] | 448 | } |
Masami Hiramatsu | c7411a1 | 2019-10-29 17:31:44 +0900 | [diff] [blame] | 449 | |
| 450 | static bool within_notrace_func(struct trace_kprobe *tk) |
| 451 | { |
Masami Hiramatsu | dcbd21c | 2020-04-25 14:49:09 +0900 | [diff] [blame] | 452 | unsigned long addr = trace_kprobe_address(tk); |
Masami Hiramatsu | c7411a1 | 2019-10-29 17:31:44 +0900 | [diff] [blame] | 453 | char symname[KSYM_NAME_LEN], *p; |
| 454 | |
| 455 | if (!__within_notrace_func(addr)) |
| 456 | return false; |
| 457 | |
| 458 | /* Check if the address is on a suffixed-symbol */ |
| 459 | if (!lookup_symbol_name(addr, symname)) { |
| 460 | p = strchr(symname, '.'); |
| 461 | if (!p) |
| 462 | return true; |
| 463 | *p = '\0'; |
| 464 | addr = (unsigned long)kprobe_lookup_name(symname, 0); |
| 465 | if (addr) |
| 466 | return __within_notrace_func(addr); |
| 467 | } |
| 468 | |
| 469 | return true; |
| 470 | } |
Masami Hiramatsu | 45408c4 | 2018-07-30 19:20:14 +0900 | [diff] [blame] | 471 | #else |
| 472 | #define within_notrace_func(tk) (false) |
| 473 | #endif |
| 474 | |
Masami Hiramatsu | 6142431 | 2011-06-27 16:26:56 +0900 | [diff] [blame] | 475 | /* Internal register function - just handle k*probes and flags */ |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 476 | static int __register_trace_kprobe(struct trace_kprobe *tk) |
Masami Hiramatsu | 6142431 | 2011-06-27 16:26:56 +0900 | [diff] [blame] | 477 | { |
Masami Hiramatsu | a668281 | 2018-08-29 01:18:43 +0900 | [diff] [blame] | 478 | int i, ret; |
Masami Hiramatsu | 6142431 | 2011-06-27 16:26:56 +0900 | [diff] [blame] | 479 | |
David Howells | a94549d | 2019-08-19 17:17:58 -0700 | [diff] [blame] | 480 | ret = security_locked_down(LOCKDOWN_KPROBES); |
| 481 | if (ret) |
| 482 | return ret; |
| 483 | |
Masami Hiramatsu | 715fa2f | 2019-06-01 00:18:07 +0900 | [diff] [blame] | 484 | if (trace_kprobe_is_registered(tk)) |
Masami Hiramatsu | 6142431 | 2011-06-27 16:26:56 +0900 | [diff] [blame] | 485 | return -EINVAL; |
| 486 | |
Masami Hiramatsu | 45408c4 | 2018-07-30 19:20:14 +0900 | [diff] [blame] | 487 | if (within_notrace_func(tk)) { |
| 488 | pr_warn("Could not probe notrace function %s\n", |
| 489 | trace_kprobe_symbol(tk)); |
| 490 | return -EINVAL; |
| 491 | } |
| 492 | |
Masami Hiramatsu | a668281 | 2018-08-29 01:18:43 +0900 | [diff] [blame] | 493 | for (i = 0; i < tk->tp.nr_args; i++) { |
| 494 | ret = traceprobe_update_arg(&tk->tp.args[i]); |
| 495 | if (ret) |
| 496 | return ret; |
| 497 | } |
| 498 | |
Masami Hiramatsu | 6142431 | 2011-06-27 16:26:56 +0900 | [diff] [blame] | 499 | /* Set/clear disabled flag according to tp->flag */ |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 500 | if (trace_probe_is_enabled(&tk->tp)) |
| 501 | tk->rp.kp.flags &= ~KPROBE_FLAG_DISABLED; |
Masami Hiramatsu | 6142431 | 2011-06-27 16:26:56 +0900 | [diff] [blame] | 502 | else |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 503 | tk->rp.kp.flags |= KPROBE_FLAG_DISABLED; |
Masami Hiramatsu | 6142431 | 2011-06-27 16:26:56 +0900 | [diff] [blame] | 504 | |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 505 | if (trace_kprobe_is_return(tk)) |
| 506 | ret = register_kretprobe(&tk->rp); |
Masami Hiramatsu | 6142431 | 2011-06-27 16:26:56 +0900 | [diff] [blame] | 507 | else |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 508 | ret = register_kprobe(&tk->rp.kp); |
Masami Hiramatsu | 6142431 | 2011-06-27 16:26:56 +0900 | [diff] [blame] | 509 | |
Masami Hiramatsu | 6142431 | 2011-06-27 16:26:56 +0900 | [diff] [blame] | 510 | return ret; |
| 511 | } |
| 512 | |
| 513 | /* Internal unregister function - just handle k*probes and flags */ |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 514 | static void __unregister_trace_kprobe(struct trace_kprobe *tk) |
Masami Hiramatsu | 6142431 | 2011-06-27 16:26:56 +0900 | [diff] [blame] | 515 | { |
Masami Hiramatsu | 715fa2f | 2019-06-01 00:18:07 +0900 | [diff] [blame] | 516 | if (trace_kprobe_is_registered(tk)) { |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 517 | if (trace_kprobe_is_return(tk)) |
| 518 | unregister_kretprobe(&tk->rp); |
Masami Hiramatsu | 6142431 | 2011-06-27 16:26:56 +0900 | [diff] [blame] | 519 | else |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 520 | unregister_kprobe(&tk->rp.kp); |
Masami Hiramatsu | 715fa2f | 2019-06-01 00:18:07 +0900 | [diff] [blame] | 521 | /* Cleanup kprobe for reuse and mark it unregistered */ |
| 522 | INIT_HLIST_NODE(&tk->rp.kp.hlist); |
| 523 | INIT_LIST_HEAD(&tk->rp.kp.list); |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 524 | if (tk->rp.kp.symbol_name) |
| 525 | tk->rp.kp.addr = NULL; |
Masami Hiramatsu | 6142431 | 2011-06-27 16:26:56 +0900 | [diff] [blame] | 526 | } |
| 527 | } |
| 528 | |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 529 | /* Unregister a trace_probe and probe_event */ |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 530 | static int unregister_trace_kprobe(struct trace_kprobe *tk) |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 531 | { |
Masami Hiramatsu | ca89bc0 | 2019-06-20 00:07:49 +0900 | [diff] [blame] | 532 | /* If other probes are on the event, just unregister kprobe */ |
| 533 | if (trace_probe_has_sibling(&tk->tp)) |
| 534 | goto unreg; |
| 535 | |
Masami Hiramatsu | 02ca152 | 2011-10-04 19:44:38 +0900 | [diff] [blame] | 536 | /* Enabled event can not be unregistered */ |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 537 | if (trace_probe_is_enabled(&tk->tp)) |
Masami Hiramatsu | 02ca152 | 2011-10-04 19:44:38 +0900 | [diff] [blame] | 538 | return -EBUSY; |
| 539 | |
Steven Rostedt (VMware) | 1d18538 | 2021-08-16 23:42:57 -0400 | [diff] [blame] | 540 | /* If there's a reference to the dynamic event */ |
| 541 | if (trace_event_dyn_busy(trace_probe_event_call(&tk->tp))) |
| 542 | return -EBUSY; |
| 543 | |
Steven Rostedt (Red Hat) | 40c3259 | 2013-07-03 23:33:50 -0400 | [diff] [blame] | 544 | /* Will fail if probe is being used by ftrace or perf */ |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 545 | if (unregister_kprobe_event(tk)) |
Steven Rostedt (Red Hat) | 40c3259 | 2013-07-03 23:33:50 -0400 | [diff] [blame] | 546 | return -EBUSY; |
| 547 | |
Masami Hiramatsu | ca89bc0 | 2019-06-20 00:07:49 +0900 | [diff] [blame] | 548 | unreg: |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 549 | __unregister_trace_kprobe(tk); |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 550 | dyn_event_remove(&tk->devent); |
Masami Hiramatsu | ca89bc0 | 2019-06-20 00:07:49 +0900 | [diff] [blame] | 551 | trace_probe_unlink(&tk->tp); |
Masami Hiramatsu | 02ca152 | 2011-10-04 19:44:38 +0900 | [diff] [blame] | 552 | |
| 553 | return 0; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 554 | } |
| 555 | |
Masami Hiramatsu | fe60b0c | 2019-09-18 17:55:46 +0900 | [diff] [blame] | 556 | static bool trace_kprobe_has_same_kprobe(struct trace_kprobe *orig, |
| 557 | struct trace_kprobe *comp) |
| 558 | { |
| 559 | struct trace_probe_event *tpe = orig->tp.event; |
Masami Hiramatsu | fe60b0c | 2019-09-18 17:55:46 +0900 | [diff] [blame] | 560 | int i; |
| 561 | |
Jiri Olsa | e161c6b | 2021-11-25 21:28:52 +0100 | [diff] [blame] | 562 | list_for_each_entry(orig, &tpe->probes, tp.list) { |
Masami Hiramatsu | fe60b0c | 2019-09-18 17:55:46 +0900 | [diff] [blame] | 563 | if (strcmp(trace_kprobe_symbol(orig), |
| 564 | trace_kprobe_symbol(comp)) || |
| 565 | trace_kprobe_offset(orig) != trace_kprobe_offset(comp)) |
| 566 | continue; |
| 567 | |
| 568 | /* |
| 569 | * trace_probe_compare_arg_type() ensured that nr_args and |
| 570 | * each argument name and type are same. Let's compare comm. |
| 571 | */ |
| 572 | for (i = 0; i < orig->tp.nr_args; i++) { |
| 573 | if (strcmp(orig->tp.args[i].comm, |
| 574 | comp->tp.args[i].comm)) |
Srikar Dronamraju | f8d7ab2 | 2019-09-24 17:19:06 +0530 | [diff] [blame] | 575 | break; |
Masami Hiramatsu | fe60b0c | 2019-09-18 17:55:46 +0900 | [diff] [blame] | 576 | } |
| 577 | |
Srikar Dronamraju | f8d7ab2 | 2019-09-24 17:19:06 +0530 | [diff] [blame] | 578 | if (i == orig->tp.nr_args) |
| 579 | return true; |
Masami Hiramatsu | fe60b0c | 2019-09-18 17:55:46 +0900 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | return false; |
| 583 | } |
| 584 | |
Masami Hiramatsu | ca89bc0 | 2019-06-20 00:07:49 +0900 | [diff] [blame] | 585 | static int append_trace_kprobe(struct trace_kprobe *tk, struct trace_kprobe *to) |
| 586 | { |
| 587 | int ret; |
| 588 | |
Masami Hiramatsu | fe60b0c | 2019-09-18 17:55:46 +0900 | [diff] [blame] | 589 | ret = trace_probe_compare_arg_type(&tk->tp, &to->tp); |
| 590 | if (ret) { |
| 591 | /* Note that argument starts index = 2 */ |
| 592 | trace_probe_log_set_index(ret + 1); |
| 593 | trace_probe_log_err(0, DIFF_ARG_TYPE); |
| 594 | return -EEXIST; |
| 595 | } |
| 596 | if (trace_kprobe_has_same_kprobe(to, tk)) { |
| 597 | trace_probe_log_set_index(0); |
| 598 | trace_probe_log_err(0, SAME_PROBE); |
| 599 | return -EEXIST; |
| 600 | } |
| 601 | |
Masami Hiramatsu | ca89bc0 | 2019-06-20 00:07:49 +0900 | [diff] [blame] | 602 | /* Append to existing event */ |
| 603 | ret = trace_probe_append(&tk->tp, &to->tp); |
| 604 | if (ret) |
| 605 | return ret; |
| 606 | |
| 607 | /* Register k*probe */ |
| 608 | ret = __register_trace_kprobe(tk); |
| 609 | if (ret == -ENOENT && !trace_kprobe_module_exist(tk)) { |
| 610 | pr_warn("This probe might be able to register after target module is loaded. Continue.\n"); |
| 611 | ret = 0; |
| 612 | } |
| 613 | |
| 614 | if (ret) |
| 615 | trace_probe_unlink(&tk->tp); |
| 616 | else |
Steven Rostedt (VMware) | 8b0e6c7 | 2021-08-16 23:42:56 -0400 | [diff] [blame] | 617 | dyn_event_add(&tk->devent, trace_probe_event_call(&tk->tp)); |
Masami Hiramatsu | ca89bc0 | 2019-06-20 00:07:49 +0900 | [diff] [blame] | 618 | |
| 619 | return ret; |
| 620 | } |
| 621 | |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 622 | /* Register a trace_probe and probe_event */ |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 623 | static int register_trace_kprobe(struct trace_kprobe *tk) |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 624 | { |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 625 | struct trace_kprobe *old_tk; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 626 | int ret; |
| 627 | |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 628 | mutex_lock(&event_mutex); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 629 | |
Masami Hiramatsu | b55ce203 | 2019-06-01 00:17:47 +0900 | [diff] [blame] | 630 | old_tk = find_trace_kprobe(trace_probe_name(&tk->tp), |
| 631 | trace_probe_group_name(&tk->tp)); |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 632 | if (old_tk) { |
Masami Hiramatsu | ca89bc0 | 2019-06-20 00:07:49 +0900 | [diff] [blame] | 633 | if (trace_kprobe_is_return(tk) != trace_kprobe_is_return(old_tk)) { |
| 634 | trace_probe_log_set_index(0); |
| 635 | trace_probe_log_err(0, DIFF_PROBE_TYPE); |
| 636 | ret = -EEXIST; |
| 637 | } else { |
Masami Hiramatsu | fe60b0c | 2019-09-18 17:55:46 +0900 | [diff] [blame] | 638 | ret = append_trace_kprobe(tk, old_tk); |
Masami Hiramatsu | ca89bc0 | 2019-06-20 00:07:49 +0900 | [diff] [blame] | 639 | } |
| 640 | goto end; |
Masami Hiramatsu | 2d5e067 | 2009-09-14 16:48:56 -0400 | [diff] [blame] | 641 | } |
Masami Hiramatsu | 6142431 | 2011-06-27 16:26:56 +0900 | [diff] [blame] | 642 | |
| 643 | /* Register new event */ |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 644 | ret = register_kprobe_event(tk); |
Masami Hiramatsu | 2d5e067 | 2009-09-14 16:48:56 -0400 | [diff] [blame] | 645 | if (ret) { |
Masami Hiramatsu | 8e24206 | 2021-08-19 19:26:02 +0900 | [diff] [blame] | 646 | if (ret == -EEXIST) { |
| 647 | trace_probe_log_set_index(0); |
| 648 | trace_probe_log_err(0, EVENT_EXIST); |
| 649 | } else |
| 650 | pr_warn("Failed to register probe event(%d)\n", ret); |
Masami Hiramatsu | 2d5e067 | 2009-09-14 16:48:56 -0400 | [diff] [blame] | 651 | goto end; |
| 652 | } |
| 653 | |
Masami Hiramatsu | 6142431 | 2011-06-27 16:26:56 +0900 | [diff] [blame] | 654 | /* Register k*probe */ |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 655 | ret = __register_trace_kprobe(tk); |
Masami Hiramatsu | 59158ec | 2018-08-29 01:18:15 +0900 | [diff] [blame] | 656 | if (ret == -ENOENT && !trace_kprobe_module_exist(tk)) { |
| 657 | pr_warn("This probe might be able to register after target module is loaded. Continue.\n"); |
| 658 | ret = 0; |
| 659 | } |
| 660 | |
Masami Hiramatsu | 6142431 | 2011-06-27 16:26:56 +0900 | [diff] [blame] | 661 | if (ret < 0) |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 662 | unregister_kprobe_event(tk); |
Masami Hiramatsu | 6142431 | 2011-06-27 16:26:56 +0900 | [diff] [blame] | 663 | else |
Steven Rostedt (VMware) | 8b0e6c7 | 2021-08-16 23:42:56 -0400 | [diff] [blame] | 664 | dyn_event_add(&tk->devent, trace_probe_event_call(&tk->tp)); |
Masami Hiramatsu | 6142431 | 2011-06-27 16:26:56 +0900 | [diff] [blame] | 665 | |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 666 | end: |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 667 | mutex_unlock(&event_mutex); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 668 | return ret; |
| 669 | } |
| 670 | |
Masami Hiramatsu | 6142431 | 2011-06-27 16:26:56 +0900 | [diff] [blame] | 671 | /* Module notifier call back, checking event on the module */ |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 672 | static int trace_kprobe_module_callback(struct notifier_block *nb, |
Masami Hiramatsu | 6142431 | 2011-06-27 16:26:56 +0900 | [diff] [blame] | 673 | unsigned long val, void *data) |
| 674 | { |
| 675 | struct module *mod = data; |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 676 | struct dyn_event *pos; |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 677 | struct trace_kprobe *tk; |
Masami Hiramatsu | 6142431 | 2011-06-27 16:26:56 +0900 | [diff] [blame] | 678 | int ret; |
| 679 | |
| 680 | if (val != MODULE_STATE_COMING) |
| 681 | return NOTIFY_DONE; |
| 682 | |
| 683 | /* Update probes on coming module */ |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 684 | mutex_lock(&event_mutex); |
| 685 | for_each_trace_kprobe(tk, pos) { |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 686 | if (trace_kprobe_within_module(tk, mod)) { |
Masami Hiramatsu | 02ca152 | 2011-10-04 19:44:38 +0900 | [diff] [blame] | 687 | /* Don't need to check busy - this should have gone. */ |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 688 | __unregister_trace_kprobe(tk); |
| 689 | ret = __register_trace_kprobe(tk); |
Masami Hiramatsu | 6142431 | 2011-06-27 16:26:56 +0900 | [diff] [blame] | 690 | if (ret) |
Joe Perches | a395d6a | 2016-03-22 14:28:09 -0700 | [diff] [blame] | 691 | pr_warn("Failed to re-register probe %s on %s: %d\n", |
Masami Hiramatsu | b55ce203 | 2019-06-01 00:17:47 +0900 | [diff] [blame] | 692 | trace_probe_name(&tk->tp), |
Jarkko Sakkinen | f3d3642 | 2020-08-18 08:08:57 +0300 | [diff] [blame] | 693 | module_name(mod), ret); |
Masami Hiramatsu | 6142431 | 2011-06-27 16:26:56 +0900 | [diff] [blame] | 694 | } |
| 695 | } |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 696 | mutex_unlock(&event_mutex); |
Masami Hiramatsu | 6142431 | 2011-06-27 16:26:56 +0900 | [diff] [blame] | 697 | |
| 698 | return NOTIFY_DONE; |
| 699 | } |
| 700 | |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 701 | static struct notifier_block trace_kprobe_module_nb = { |
| 702 | .notifier_call = trace_kprobe_module_callback, |
Masami Hiramatsu | 6142431 | 2011-06-27 16:26:56 +0900 | [diff] [blame] | 703 | .priority = 1 /* Invoked after kprobe module callback */ |
| 704 | }; |
| 705 | |
Masami Hiramatsu | d262271 | 2021-02-01 13:48:11 -0600 | [diff] [blame] | 706 | static int __trace_kprobe_create(int argc, const char *argv[]) |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 707 | { |
| 708 | /* |
| 709 | * Argument syntax: |
Alban Crequy | 696ced4 | 2017-04-03 12:36:22 +0200 | [diff] [blame] | 710 | * - Add kprobe: |
| 711 | * p[:[GRP/]EVENT] [MOD:]KSYM[+OFFS]|KADDR [FETCHARGS] |
| 712 | * - Add kretprobe: |
| 713 | * r[MAXACTIVE][:[GRP/]EVENT] [MOD:]KSYM[+0] [FETCHARGS] |
Masami Hiramatsu | 4725cd8 | 2020-09-10 17:55:35 +0900 | [diff] [blame] | 714 | * Or |
| 715 | * p:[GRP/]EVENT] [MOD:]KSYM[+0]%return [FETCHARGS] |
| 716 | * |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 717 | * Fetch args: |
Masami Hiramatsu | 2e06ff6 | 2009-10-07 18:27:59 -0400 | [diff] [blame] | 718 | * $retval : fetch return value |
| 719 | * $stack : fetch stack address |
| 720 | * $stackN : fetch Nth of stack (N:0-) |
Omar Sandoval | 35abb67 | 2016-06-08 18:38:02 -0700 | [diff] [blame] | 721 | * $comm : fetch current task comm |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 722 | * @ADDR : fetch memory at ADDR (ADDR should be in kernel) |
| 723 | * @SYM[+|-offs] : fetch memory at SYM +|- offs (SYM is a data symbol) |
| 724 | * %REG : fetch register REG |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 725 | * Dereferencing memory fetch: |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 726 | * +|-offs(ARG) : fetch memory at ARG +|- offs address. |
Masami Hiramatsu | eca0d91 | 2009-09-10 19:53:38 -0400 | [diff] [blame] | 727 | * Alias name of args: |
| 728 | * NAME=FETCHARG : set NAME as alias of FETCHARG. |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 729 | * Type of args: |
| 730 | * FETCHARG:TYPE : use TYPE instead of unsigned long. |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 731 | */ |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 732 | struct trace_kprobe *tk = NULL; |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 733 | int i, len, ret = 0; |
| 734 | bool is_return = false; |
| 735 | char *symbol = NULL, *tmp = NULL; |
| 736 | const char *event = NULL, *group = KPROBE_EVENT_SYSTEM; |
Steven Rostedt (VMware) | 007517a | 2021-08-19 00:13:27 -0400 | [diff] [blame] | 737 | enum probe_print_type ptype; |
Alban Crequy | 696ced4 | 2017-04-03 12:36:22 +0200 | [diff] [blame] | 738 | int maxactive = 0; |
Masami Hiramatsu | c5d343b | 2018-03-17 21:38:10 +0900 | [diff] [blame] | 739 | long offset = 0; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 740 | void *addr = NULL; |
Masami Hiramatsu | 4a846b4 | 2009-09-11 05:31:21 +0200 | [diff] [blame] | 741 | char buf[MAX_EVENT_NAME_LEN]; |
Masami Hiramatsu | a1303af | 2018-04-25 21:21:26 +0900 | [diff] [blame] | 742 | unsigned int flags = TPARG_FL_KERNEL; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 743 | |
Andrea Righi | 8b05a3a | 2019-01-11 07:01:13 +0100 | [diff] [blame] | 744 | switch (argv[0][0]) { |
| 745 | case 'r': |
Srikar Dronamraju | 3a6b766 | 2012-04-09 14:41:33 +0530 | [diff] [blame] | 746 | is_return = true; |
Andrea Righi | 8b05a3a | 2019-01-11 07:01:13 +0100 | [diff] [blame] | 747 | break; |
| 748 | case 'p': |
| 749 | break; |
| 750 | default: |
| 751 | return -ECANCELED; |
| 752 | } |
| 753 | if (argc < 2) |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 754 | return -ECANCELED; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 755 | |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 756 | trace_probe_log_init("trace_kprobe", argc, argv); |
| 757 | |
Alban Crequy | 696ced4 | 2017-04-03 12:36:22 +0200 | [diff] [blame] | 758 | event = strchr(&argv[0][1], ':'); |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 759 | if (event) |
Alban Crequy | 696ced4 | 2017-04-03 12:36:22 +0200 | [diff] [blame] | 760 | event++; |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 761 | |
Masami Hiramatsu | 287c038 | 2019-03-14 13:30:09 +0900 | [diff] [blame] | 762 | if (isdigit(argv[0][1])) { |
| 763 | if (!is_return) { |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 764 | trace_probe_log_err(1, MAXACT_NO_KPROBE); |
| 765 | goto parse_error; |
Masami Hiramatsu | 287c038 | 2019-03-14 13:30:09 +0900 | [diff] [blame] | 766 | } |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 767 | if (event) |
| 768 | len = event - &argv[0][1] - 1; |
| 769 | else |
| 770 | len = strlen(&argv[0][1]); |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 771 | if (len > MAX_EVENT_NAME_LEN - 1) { |
| 772 | trace_probe_log_err(1, BAD_MAXACT); |
| 773 | goto parse_error; |
| 774 | } |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 775 | memcpy(buf, &argv[0][1], len); |
| 776 | buf[len] = '\0'; |
| 777 | ret = kstrtouint(buf, 0, &maxactive); |
Masami Hiramatsu | 287c038 | 2019-03-14 13:30:09 +0900 | [diff] [blame] | 778 | if (ret || !maxactive) { |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 779 | trace_probe_log_err(1, BAD_MAXACT); |
| 780 | goto parse_error; |
Alban Crequy | 696ced4 | 2017-04-03 12:36:22 +0200 | [diff] [blame] | 781 | } |
| 782 | /* kretprobes instances are iterated over via a list. The |
| 783 | * maximum should stay reasonable. |
| 784 | */ |
| 785 | if (maxactive > KRETPROBE_MAXACTIVE_MAX) { |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 786 | trace_probe_log_err(1, MAXACT_TOO_BIG); |
| 787 | goto parse_error; |
Alban Crequy | 696ced4 | 2017-04-03 12:36:22 +0200 | [diff] [blame] | 788 | } |
| 789 | } |
| 790 | |
Sabrina Dubroca | 9e52b32 | 2017-06-22 11:24:42 +0200 | [diff] [blame] | 791 | /* try to parse an address. if that fails, try to read the |
| 792 | * input as a symbol. */ |
| 793 | if (kstrtoul(argv[1], 0, (unsigned long *)&addr)) { |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 794 | trace_probe_log_set_index(1); |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 795 | /* Check whether uprobe event specified */ |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 796 | if (strchr(argv[1], '/') && strchr(argv[1], ':')) { |
| 797 | ret = -ECANCELED; |
| 798 | goto error; |
| 799 | } |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 800 | /* a symbol specified */ |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 801 | symbol = kstrdup(argv[1], GFP_KERNEL); |
| 802 | if (!symbol) |
| 803 | return -ENOMEM; |
Masami Hiramatsu | 4725cd8 | 2020-09-10 17:55:35 +0900 | [diff] [blame] | 804 | |
| 805 | tmp = strchr(symbol, '%'); |
| 806 | if (tmp) { |
| 807 | if (!strcmp(tmp, "%return")) { |
| 808 | *tmp = '\0'; |
| 809 | is_return = true; |
| 810 | } else { |
| 811 | trace_probe_log_err(tmp - symbol, BAD_ADDR_SUFFIX); |
| 812 | goto parse_error; |
| 813 | } |
| 814 | } |
| 815 | |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 816 | /* TODO: support .init module functions */ |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 817 | ret = traceprobe_split_symbol_offset(symbol, &offset); |
Masami Hiramatsu | c5d343b | 2018-03-17 21:38:10 +0900 | [diff] [blame] | 818 | if (ret || offset < 0 || offset > UINT_MAX) { |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 819 | trace_probe_log_err(0, BAD_PROBE_ADDR); |
| 820 | goto parse_error; |
Masami Hiramatsu | e63cc23 | 2009-10-16 20:07:28 -0400 | [diff] [blame] | 821 | } |
Masami Hiramatsu | 4725cd8 | 2020-09-10 17:55:35 +0900 | [diff] [blame] | 822 | if (is_return) |
| 823 | flags |= TPARG_FL_RETURN; |
Masami Hiramatsu | 97c753e | 2021-01-28 00:37:51 +0900 | [diff] [blame] | 824 | ret = kprobe_on_func_entry(NULL, symbol, offset); |
| 825 | if (ret == 0) |
Masami Hiramatsu | a1303af | 2018-04-25 21:21:26 +0900 | [diff] [blame] | 826 | flags |= TPARG_FL_FENTRY; |
Masami Hiramatsu | 97c753e | 2021-01-28 00:37:51 +0900 | [diff] [blame] | 827 | /* Defer the ENOENT case until register kprobe */ |
| 828 | if (ret == -EINVAL && is_return) { |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 829 | trace_probe_log_err(0, BAD_RETPROBE); |
| 830 | goto parse_error; |
Masami Hiramatsu | e63cc23 | 2009-10-16 20:07:28 -0400 | [diff] [blame] | 831 | } |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 832 | } |
| 833 | |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 834 | trace_probe_log_set_index(0); |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 835 | if (event) { |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 836 | ret = traceprobe_parse_event_name(&event, &group, buf, |
| 837 | event - argv[0]); |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 838 | if (ret) |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 839 | goto parse_error; |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 840 | } else { |
Masami Hiramatsu | 4263565 | 2009-08-13 16:35:26 -0400 | [diff] [blame] | 841 | /* Make a new event name */ |
Masami Hiramatsu | 4263565 | 2009-08-13 16:35:26 -0400 | [diff] [blame] | 842 | if (symbol) |
Masami Hiramatsu | 6f3cf44 | 2009-12-16 17:24:08 -0500 | [diff] [blame] | 843 | snprintf(buf, MAX_EVENT_NAME_LEN, "%c_%s_%ld", |
Masami Hiramatsu | 4263565 | 2009-08-13 16:35:26 -0400 | [diff] [blame] | 844 | is_return ? 'r' : 'p', symbol, offset); |
| 845 | else |
Masami Hiramatsu | 6f3cf44 | 2009-12-16 17:24:08 -0500 | [diff] [blame] | 846 | snprintf(buf, MAX_EVENT_NAME_LEN, "%c_0x%p", |
Masami Hiramatsu | 4263565 | 2009-08-13 16:35:26 -0400 | [diff] [blame] | 847 | is_return ? 'r' : 'p', addr); |
Naveen N. Rao | fca18a4 | 2017-07-08 00:27:30 +0530 | [diff] [blame] | 848 | sanitize_event_name(buf); |
Masami Hiramatsu | 4a846b4 | 2009-09-11 05:31:21 +0200 | [diff] [blame] | 849 | event = buf; |
| 850 | } |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 851 | |
| 852 | /* setup a probe */ |
Alban Crequy | 696ced4 | 2017-04-03 12:36:22 +0200 | [diff] [blame] | 853 | tk = alloc_trace_kprobe(group, event, addr, symbol, offset, maxactive, |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 854 | argc - 2, is_return); |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 855 | if (IS_ERR(tk)) { |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 856 | ret = PTR_ERR(tk); |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 857 | /* This must return -ENOMEM, else there is a bug */ |
Masami Hiramatsu | a039480 | 2019-03-14 13:30:50 +0900 | [diff] [blame] | 858 | WARN_ON_ONCE(ret != -ENOMEM); |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 859 | goto out; /* We know tk is not allocated */ |
Masami Hiramatsu | e63cc23 | 2009-10-16 20:07:28 -0400 | [diff] [blame] | 860 | } |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 861 | argc -= 2; argv += 2; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 862 | |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 863 | /* parse arguments */ |
Masami Hiramatsu | a82378d | 2009-08-13 16:35:18 -0400 | [diff] [blame] | 864 | for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) { |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 865 | trace_probe_log_set_index(i + 2); |
Steven Rostedt (VMware) | fcd9db5 | 2021-08-16 23:42:58 -0400 | [diff] [blame] | 866 | ret = traceprobe_parse_probe_arg(&tk->tp, i, argv[i], flags); |
Masami Hiramatsu | d00bbea9 | 2018-11-05 18:01:40 +0900 | [diff] [blame] | 867 | if (ret) |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 868 | goto error; /* This can be -ENOMEM */ |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 869 | } |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 870 | |
Steven Rostedt (VMware) | 007517a | 2021-08-19 00:13:27 -0400 | [diff] [blame] | 871 | ptype = is_return ? PROBE_PRINT_RETURN : PROBE_PRINT_NORMAL; |
| 872 | ret = traceprobe_set_print_fmt(&tk->tp, ptype); |
Masami Hiramatsu | f730e0f | 2019-06-01 00:16:46 +0900 | [diff] [blame] | 873 | if (ret < 0) |
| 874 | goto error; |
| 875 | |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 876 | ret = register_trace_kprobe(tk); |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 877 | if (ret) { |
| 878 | trace_probe_log_set_index(1); |
| 879 | if (ret == -EILSEQ) |
| 880 | trace_probe_log_err(0, BAD_INSN_BNDRY); |
| 881 | else if (ret == -ENOENT) |
| 882 | trace_probe_log_err(0, BAD_PROBE_ADDR); |
Masami Hiramatsu | ca89bc0 | 2019-06-20 00:07:49 +0900 | [diff] [blame] | 883 | else if (ret != -ENOMEM && ret != -EEXIST) |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 884 | trace_probe_log_err(0, FAIL_REG_PROBE); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 885 | goto error; |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 886 | } |
| 887 | |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 888 | out: |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 889 | trace_probe_log_clear(); |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 890 | kfree(symbol); |
| 891 | return ret; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 892 | |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 893 | parse_error: |
| 894 | ret = -EINVAL; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 895 | error: |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 896 | free_trace_kprobe(tk); |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 897 | goto out; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 898 | } |
| 899 | |
Masami Hiramatsu | d262271 | 2021-02-01 13:48:11 -0600 | [diff] [blame] | 900 | static int trace_kprobe_create(const char *raw_command) |
| 901 | { |
| 902 | return trace_probe_create(raw_command, __trace_kprobe_create); |
| 903 | } |
| 904 | |
| 905 | static int create_or_delete_trace_kprobe(const char *raw_command) |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 906 | { |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 907 | int ret; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 908 | |
Masami Hiramatsu | d262271 | 2021-02-01 13:48:11 -0600 | [diff] [blame] | 909 | if (raw_command[0] == '-') |
| 910 | return dyn_event_release(raw_command, &trace_kprobe_ops); |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 911 | |
Masami Hiramatsu | d262271 | 2021-02-01 13:48:11 -0600 | [diff] [blame] | 912 | ret = trace_kprobe_create(raw_command); |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 913 | return ret == -ECANCELED ? -EINVAL : ret; |
| 914 | } |
| 915 | |
Tom Zanussi | 29a1548 | 2020-01-29 12:59:30 -0600 | [diff] [blame] | 916 | static int trace_kprobe_run_command(struct dynevent_cmd *cmd) |
Tom Zanussi | 2a588dd | 2020-01-29 12:59:29 -0600 | [diff] [blame] | 917 | { |
Masami Hiramatsu | d262271 | 2021-02-01 13:48:11 -0600 | [diff] [blame] | 918 | return create_or_delete_trace_kprobe(cmd->seq.buffer); |
Tom Zanussi | 2a588dd | 2020-01-29 12:59:29 -0600 | [diff] [blame] | 919 | } |
| 920 | |
| 921 | /** |
| 922 | * kprobe_event_cmd_init - Initialize a kprobe event command object |
| 923 | * @cmd: A pointer to the dynevent_cmd struct representing the new event |
| 924 | * @buf: A pointer to the buffer used to build the command |
| 925 | * @maxlen: The length of the buffer passed in @buf |
| 926 | * |
| 927 | * Initialize a synthetic event command object. Use this before |
| 928 | * calling any of the other kprobe_event functions. |
| 929 | */ |
| 930 | void kprobe_event_cmd_init(struct dynevent_cmd *cmd, char *buf, int maxlen) |
| 931 | { |
| 932 | dynevent_cmd_init(cmd, buf, maxlen, DYNEVENT_TYPE_KPROBE, |
Tom Zanussi | 29a1548 | 2020-01-29 12:59:30 -0600 | [diff] [blame] | 933 | trace_kprobe_run_command); |
Tom Zanussi | 2a588dd | 2020-01-29 12:59:29 -0600 | [diff] [blame] | 934 | } |
| 935 | EXPORT_SYMBOL_GPL(kprobe_event_cmd_init); |
| 936 | |
| 937 | /** |
| 938 | * __kprobe_event_gen_cmd_start - Generate a kprobe event command from arg list |
| 939 | * @cmd: A pointer to the dynevent_cmd struct representing the new event |
| 940 | * @name: The name of the kprobe event |
| 941 | * @loc: The location of the kprobe event |
| 942 | * @kretprobe: Is this a return probe? |
| 943 | * @args: Variable number of arg (pairs), one pair for each field |
| 944 | * |
| 945 | * NOTE: Users normally won't want to call this function directly, but |
| 946 | * rather use the kprobe_event_gen_cmd_start() wrapper, which automatically |
| 947 | * adds a NULL to the end of the arg list. If this function is used |
| 948 | * directly, make sure the last arg in the variable arg list is NULL. |
| 949 | * |
| 950 | * Generate a kprobe event command to be executed by |
| 951 | * kprobe_event_gen_cmd_end(). This function can be used to generate the |
| 952 | * complete command or only the first part of it; in the latter case, |
| 953 | * kprobe_event_add_fields() can be used to add more fields following this. |
| 954 | * |
Masami Hiramatsu | 5b4dcd2 | 2020-04-25 14:49:26 +0900 | [diff] [blame] | 955 | * Unlikely the synth_event_gen_cmd_start(), @loc must be specified. This |
| 956 | * returns -EINVAL if @loc == NULL. |
| 957 | * |
Tom Zanussi | 2a588dd | 2020-01-29 12:59:29 -0600 | [diff] [blame] | 958 | * Return: 0 if successful, error otherwise. |
| 959 | */ |
| 960 | int __kprobe_event_gen_cmd_start(struct dynevent_cmd *cmd, bool kretprobe, |
| 961 | const char *name, const char *loc, ...) |
| 962 | { |
| 963 | char buf[MAX_EVENT_NAME_LEN]; |
| 964 | struct dynevent_arg arg; |
| 965 | va_list args; |
| 966 | int ret; |
| 967 | |
| 968 | if (cmd->type != DYNEVENT_TYPE_KPROBE) |
| 969 | return -EINVAL; |
| 970 | |
Masami Hiramatsu | 5b4dcd2 | 2020-04-25 14:49:26 +0900 | [diff] [blame] | 971 | if (!loc) |
| 972 | return -EINVAL; |
| 973 | |
Tom Zanussi | 2a588dd | 2020-01-29 12:59:29 -0600 | [diff] [blame] | 974 | if (kretprobe) |
| 975 | snprintf(buf, MAX_EVENT_NAME_LEN, "r:kprobes/%s", name); |
| 976 | else |
| 977 | snprintf(buf, MAX_EVENT_NAME_LEN, "p:kprobes/%s", name); |
| 978 | |
| 979 | ret = dynevent_str_add(cmd, buf); |
| 980 | if (ret) |
| 981 | return ret; |
| 982 | |
Tom Zanussi | 74403b6 | 2020-01-31 15:55:32 -0600 | [diff] [blame] | 983 | dynevent_arg_init(&arg, 0); |
Tom Zanussi | 2a588dd | 2020-01-29 12:59:29 -0600 | [diff] [blame] | 984 | arg.str = loc; |
Tom Zanussi | 74403b6 | 2020-01-31 15:55:32 -0600 | [diff] [blame] | 985 | ret = dynevent_arg_add(cmd, &arg, NULL); |
Tom Zanussi | 2a588dd | 2020-01-29 12:59:29 -0600 | [diff] [blame] | 986 | if (ret) |
| 987 | return ret; |
| 988 | |
| 989 | va_start(args, loc); |
| 990 | for (;;) { |
| 991 | const char *field; |
| 992 | |
| 993 | field = va_arg(args, const char *); |
| 994 | if (!field) |
| 995 | break; |
| 996 | |
| 997 | if (++cmd->n_fields > MAX_TRACE_ARGS) { |
| 998 | ret = -EINVAL; |
| 999 | break; |
| 1000 | } |
| 1001 | |
| 1002 | arg.str = field; |
Tom Zanussi | 74403b6 | 2020-01-31 15:55:32 -0600 | [diff] [blame] | 1003 | ret = dynevent_arg_add(cmd, &arg, NULL); |
Tom Zanussi | 2a588dd | 2020-01-29 12:59:29 -0600 | [diff] [blame] | 1004 | if (ret) |
| 1005 | break; |
| 1006 | } |
| 1007 | va_end(args); |
| 1008 | |
| 1009 | return ret; |
| 1010 | } |
| 1011 | EXPORT_SYMBOL_GPL(__kprobe_event_gen_cmd_start); |
| 1012 | |
| 1013 | /** |
| 1014 | * __kprobe_event_add_fields - Add probe fields to a kprobe command from arg list |
| 1015 | * @cmd: A pointer to the dynevent_cmd struct representing the new event |
| 1016 | * @args: Variable number of arg (pairs), one pair for each field |
| 1017 | * |
| 1018 | * NOTE: Users normally won't want to call this function directly, but |
| 1019 | * rather use the kprobe_event_add_fields() wrapper, which |
| 1020 | * automatically adds a NULL to the end of the arg list. If this |
| 1021 | * function is used directly, make sure the last arg in the variable |
| 1022 | * arg list is NULL. |
| 1023 | * |
| 1024 | * Add probe fields to an existing kprobe command using a variable |
| 1025 | * list of args. Fields are added in the same order they're listed. |
| 1026 | * |
| 1027 | * Return: 0 if successful, error otherwise. |
| 1028 | */ |
| 1029 | int __kprobe_event_add_fields(struct dynevent_cmd *cmd, ...) |
| 1030 | { |
| 1031 | struct dynevent_arg arg; |
| 1032 | va_list args; |
Gustavo A. R. Silva | 10f129c | 2020-02-05 16:34:04 -0600 | [diff] [blame] | 1033 | int ret = 0; |
Tom Zanussi | 2a588dd | 2020-01-29 12:59:29 -0600 | [diff] [blame] | 1034 | |
| 1035 | if (cmd->type != DYNEVENT_TYPE_KPROBE) |
| 1036 | return -EINVAL; |
| 1037 | |
Tom Zanussi | 74403b6 | 2020-01-31 15:55:32 -0600 | [diff] [blame] | 1038 | dynevent_arg_init(&arg, 0); |
Tom Zanussi | 2a588dd | 2020-01-29 12:59:29 -0600 | [diff] [blame] | 1039 | |
| 1040 | va_start(args, cmd); |
| 1041 | for (;;) { |
| 1042 | const char *field; |
| 1043 | |
| 1044 | field = va_arg(args, const char *); |
| 1045 | if (!field) |
| 1046 | break; |
| 1047 | |
| 1048 | if (++cmd->n_fields > MAX_TRACE_ARGS) { |
| 1049 | ret = -EINVAL; |
| 1050 | break; |
| 1051 | } |
| 1052 | |
| 1053 | arg.str = field; |
Tom Zanussi | 74403b6 | 2020-01-31 15:55:32 -0600 | [diff] [blame] | 1054 | ret = dynevent_arg_add(cmd, &arg, NULL); |
Tom Zanussi | 2a588dd | 2020-01-29 12:59:29 -0600 | [diff] [blame] | 1055 | if (ret) |
| 1056 | break; |
| 1057 | } |
| 1058 | va_end(args); |
| 1059 | |
| 1060 | return ret; |
| 1061 | } |
| 1062 | EXPORT_SYMBOL_GPL(__kprobe_event_add_fields); |
| 1063 | |
| 1064 | /** |
| 1065 | * kprobe_event_delete - Delete a kprobe event |
| 1066 | * @name: The name of the kprobe event to delete |
| 1067 | * |
| 1068 | * Delete a kprobe event with the give @name from kernel code rather |
| 1069 | * than directly from the command line. |
| 1070 | * |
| 1071 | * Return: 0 if successful, error otherwise. |
| 1072 | */ |
| 1073 | int kprobe_event_delete(const char *name) |
| 1074 | { |
| 1075 | char buf[MAX_EVENT_NAME_LEN]; |
| 1076 | |
| 1077 | snprintf(buf, MAX_EVENT_NAME_LEN, "-:%s", name); |
| 1078 | |
Masami Hiramatsu | d262271 | 2021-02-01 13:48:11 -0600 | [diff] [blame] | 1079 | return create_or_delete_trace_kprobe(buf); |
Tom Zanussi | 2a588dd | 2020-01-29 12:59:29 -0600 | [diff] [blame] | 1080 | } |
| 1081 | EXPORT_SYMBOL_GPL(kprobe_event_delete); |
| 1082 | |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 1083 | static int trace_kprobe_release(struct dyn_event *ev) |
| 1084 | { |
| 1085 | struct trace_kprobe *tk = to_trace_kprobe(ev); |
| 1086 | int ret = unregister_trace_kprobe(tk); |
| 1087 | |
| 1088 | if (!ret) |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 1089 | free_trace_kprobe(tk); |
Masami Hiramatsu | 02ca152 | 2011-10-04 19:44:38 +0900 | [diff] [blame] | 1090 | return ret; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1091 | } |
| 1092 | |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 1093 | 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] | 1094 | { |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 1095 | struct trace_kprobe *tk = to_trace_kprobe(ev); |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 1096 | int i; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1097 | |
Rasmus Villemoes | fa6f0cc | 2014-11-08 21:42:10 +0100 | [diff] [blame] | 1098 | seq_putc(m, trace_kprobe_is_return(tk) ? 'r' : 'p'); |
Masami Hiramatsu | 6a13a0d | 2020-03-24 16:34:48 +0900 | [diff] [blame] | 1099 | if (trace_kprobe_is_return(tk) && tk->rp.maxactive) |
| 1100 | seq_printf(m, "%d", tk->rp.maxactive); |
Masami Hiramatsu | b55ce203 | 2019-06-01 00:17:47 +0900 | [diff] [blame] | 1101 | seq_printf(m, ":%s/%s", trace_probe_group_name(&tk->tp), |
| 1102 | trace_probe_name(&tk->tp)); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1103 | |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 1104 | if (!tk->symbol) |
| 1105 | seq_printf(m, " 0x%p", tk->rp.kp.addr); |
| 1106 | else if (tk->rp.kp.offset) |
| 1107 | seq_printf(m, " %s+%u", trace_kprobe_symbol(tk), |
| 1108 | tk->rp.kp.offset); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1109 | else |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 1110 | seq_printf(m, " %s", trace_kprobe_symbol(tk)); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1111 | |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 1112 | for (i = 0; i < tk->tp.nr_args; i++) |
| 1113 | 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] | 1114 | seq_putc(m, '\n'); |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 1115 | |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1116 | return 0; |
| 1117 | } |
| 1118 | |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 1119 | static int probes_seq_show(struct seq_file *m, void *v) |
| 1120 | { |
| 1121 | struct dyn_event *ev = v; |
| 1122 | |
| 1123 | if (!is_trace_kprobe(ev)) |
| 1124 | return 0; |
| 1125 | |
| 1126 | return trace_kprobe_show(m, ev); |
| 1127 | } |
| 1128 | |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1129 | static const struct seq_operations probes_seq_op = { |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 1130 | .start = dyn_event_seq_start, |
| 1131 | .next = dyn_event_seq_next, |
| 1132 | .stop = dyn_event_seq_stop, |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1133 | .show = probes_seq_show |
| 1134 | }; |
| 1135 | |
| 1136 | static int probes_open(struct inode *inode, struct file *file) |
| 1137 | { |
Masami Hiramatsu | 02ca152 | 2011-10-04 19:44:38 +0900 | [diff] [blame] | 1138 | int ret; |
| 1139 | |
Steven Rostedt (VMware) | 17911ff | 2019-10-11 17:22:50 -0400 | [diff] [blame] | 1140 | ret = security_locked_down(LOCKDOWN_TRACEFS); |
| 1141 | if (ret) |
| 1142 | return ret; |
| 1143 | |
Masami Hiramatsu | 02ca152 | 2011-10-04 19:44:38 +0900 | [diff] [blame] | 1144 | if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) { |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 1145 | ret = dyn_events_release_all(&trace_kprobe_ops); |
Masami Hiramatsu | 02ca152 | 2011-10-04 19:44:38 +0900 | [diff] [blame] | 1146 | if (ret < 0) |
| 1147 | return ret; |
| 1148 | } |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1149 | |
| 1150 | return seq_open(file, &probes_seq_op); |
| 1151 | } |
| 1152 | |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1153 | static ssize_t probes_write(struct file *file, const char __user *buffer, |
| 1154 | size_t count, loff_t *ppos) |
| 1155 | { |
Tom Zanussi | 7e465ba | 2017-09-22 14:58:20 -0500 | [diff] [blame] | 1156 | return trace_parse_run_command(file, buffer, count, ppos, |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 1157 | create_or_delete_trace_kprobe); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1158 | } |
| 1159 | |
| 1160 | static const struct file_operations kprobe_events_ops = { |
| 1161 | .owner = THIS_MODULE, |
| 1162 | .open = probes_open, |
| 1163 | .read = seq_read, |
| 1164 | .llseek = seq_lseek, |
| 1165 | .release = seq_release, |
| 1166 | .write = probes_write, |
| 1167 | }; |
| 1168 | |
Masami Hiramatsu | cd7e7bd | 2009-08-13 16:35:42 -0400 | [diff] [blame] | 1169 | /* Probes profiling interfaces */ |
| 1170 | static int probes_profile_seq_show(struct seq_file *m, void *v) |
| 1171 | { |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 1172 | struct dyn_event *ev = v; |
| 1173 | struct trace_kprobe *tk; |
Xiangyang Zhang | dfea08a | 2022-01-07 23:02:42 +0800 | [diff] [blame] | 1174 | unsigned long nmissed; |
Masami Hiramatsu | cd7e7bd | 2009-08-13 16:35:42 -0400 | [diff] [blame] | 1175 | |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 1176 | if (!is_trace_kprobe(ev)) |
| 1177 | return 0; |
| 1178 | |
| 1179 | tk = to_trace_kprobe(ev); |
Xiangyang Zhang | dfea08a | 2022-01-07 23:02:42 +0800 | [diff] [blame] | 1180 | nmissed = trace_kprobe_is_return(tk) ? |
| 1181 | tk->rp.kp.nmissed + tk->rp.nmissed : tk->rp.kp.nmissed; |
Mathieu Desnoyers | de7b297 | 2014-04-08 17:26:21 -0400 | [diff] [blame] | 1182 | seq_printf(m, " %-44s %15lu %15lu\n", |
Masami Hiramatsu | b55ce203 | 2019-06-01 00:17:47 +0900 | [diff] [blame] | 1183 | trace_probe_name(&tk->tp), |
Marcin Nowakowski | f18f97a | 2016-12-09 15:19:37 +0100 | [diff] [blame] | 1184 | trace_kprobe_nhit(tk), |
Xiangyang Zhang | dfea08a | 2022-01-07 23:02:42 +0800 | [diff] [blame] | 1185 | nmissed); |
Masami Hiramatsu | cd7e7bd | 2009-08-13 16:35:42 -0400 | [diff] [blame] | 1186 | |
| 1187 | return 0; |
| 1188 | } |
| 1189 | |
| 1190 | static const struct seq_operations profile_seq_op = { |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 1191 | .start = dyn_event_seq_start, |
| 1192 | .next = dyn_event_seq_next, |
| 1193 | .stop = dyn_event_seq_stop, |
Masami Hiramatsu | cd7e7bd | 2009-08-13 16:35:42 -0400 | [diff] [blame] | 1194 | .show = probes_profile_seq_show |
| 1195 | }; |
| 1196 | |
| 1197 | static int profile_open(struct inode *inode, struct file *file) |
| 1198 | { |
Steven Rostedt (VMware) | 17911ff | 2019-10-11 17:22:50 -0400 | [diff] [blame] | 1199 | int ret; |
| 1200 | |
| 1201 | ret = security_locked_down(LOCKDOWN_TRACEFS); |
| 1202 | if (ret) |
| 1203 | return ret; |
| 1204 | |
Masami Hiramatsu | cd7e7bd | 2009-08-13 16:35:42 -0400 | [diff] [blame] | 1205 | return seq_open(file, &profile_seq_op); |
| 1206 | } |
| 1207 | |
| 1208 | static const struct file_operations kprobe_profile_ops = { |
| 1209 | .owner = THIS_MODULE, |
| 1210 | .open = profile_open, |
| 1211 | .read = seq_read, |
| 1212 | .llseek = seq_lseek, |
| 1213 | .release = seq_release, |
| 1214 | }; |
| 1215 | |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 1216 | /* Kprobe specific fetch functions */ |
| 1217 | |
| 1218 | /* Return the length of string -- including null terminal byte */ |
Masami Hiramatsu | 9178412 | 2018-04-25 21:19:01 +0900 | [diff] [blame] | 1219 | static nokprobe_inline int |
Masami Hiramatsu | 88903c4 | 2019-05-15 14:38:30 +0900 | [diff] [blame] | 1220 | fetch_store_strlen_user(unsigned long addr) |
| 1221 | { |
| 1222 | const void __user *uaddr = (__force const void __user *)addr; |
| 1223 | |
Christoph Hellwig | 02dddb1 | 2020-06-08 21:34:20 -0700 | [diff] [blame] | 1224 | return strnlen_user_nofault(uaddr, MAX_STRING_SIZE); |
Masami Hiramatsu | 88903c4 | 2019-05-15 14:38:30 +0900 | [diff] [blame] | 1225 | } |
| 1226 | |
Christoph Hellwig | 9de1fec | 2020-06-08 21:34:44 -0700 | [diff] [blame] | 1227 | /* Return the length of string -- including null terminal byte */ |
Masami Hiramatsu | 9178412 | 2018-04-25 21:19:01 +0900 | [diff] [blame] | 1228 | static nokprobe_inline int |
Christoph Hellwig | 9de1fec | 2020-06-08 21:34:44 -0700 | [diff] [blame] | 1229 | fetch_store_strlen(unsigned long addr) |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 1230 | { |
Christoph Hellwig | 9de1fec | 2020-06-08 21:34:44 -0700 | [diff] [blame] | 1231 | int ret, len = 0; |
| 1232 | u8 c; |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 1233 | |
Christoph Hellwig | 9de1fec | 2020-06-08 21:34:44 -0700 | [diff] [blame] | 1234 | #ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE |
| 1235 | if (addr < TASK_SIZE) |
| 1236 | return fetch_store_strlen_user(addr); |
| 1237 | #endif |
Masami Hiramatsu | 88903c4 | 2019-05-15 14:38:30 +0900 | [diff] [blame] | 1238 | |
Christoph Hellwig | 9de1fec | 2020-06-08 21:34:44 -0700 | [diff] [blame] | 1239 | do { |
Christoph Hellwig | fe55731 | 2020-06-17 09:37:53 +0200 | [diff] [blame] | 1240 | ret = copy_from_kernel_nofault(&c, (u8 *)addr + len, 1); |
Christoph Hellwig | 9de1fec | 2020-06-08 21:34:44 -0700 | [diff] [blame] | 1241 | len++; |
| 1242 | } while (c && ret == 0 && len < MAX_STRING_SIZE); |
Masami Hiramatsu | 88903c4 | 2019-05-15 14:38:30 +0900 | [diff] [blame] | 1243 | |
Christoph Hellwig | 9de1fec | 2020-06-08 21:34:44 -0700 | [diff] [blame] | 1244 | return (ret < 0) ? ret : len; |
Masami Hiramatsu | 88903c4 | 2019-05-15 14:38:30 +0900 | [diff] [blame] | 1245 | } |
| 1246 | |
| 1247 | /* |
| 1248 | * Fetch a null-terminated string from user. Caller MUST set *(u32 *)buf |
| 1249 | * with max length and relative data location. |
| 1250 | */ |
| 1251 | static nokprobe_inline int |
| 1252 | fetch_store_string_user(unsigned long addr, void *dest, void *base) |
| 1253 | { |
| 1254 | const void __user *uaddr = (__force const void __user *)addr; |
| 1255 | int maxlen = get_loc_len(*(u32 *)dest); |
| 1256 | void *__dest; |
| 1257 | long ret; |
| 1258 | |
| 1259 | if (unlikely(!maxlen)) |
| 1260 | return -ENOMEM; |
| 1261 | |
| 1262 | __dest = get_loc_data(dest, base); |
| 1263 | |
Christoph Hellwig | bd88bb5 | 2020-06-08 21:34:14 -0700 | [diff] [blame] | 1264 | ret = strncpy_from_user_nofault(__dest, uaddr, maxlen); |
Masami Hiramatsu | 88903c4 | 2019-05-15 14:38:30 +0900 | [diff] [blame] | 1265 | if (ret >= 0) |
| 1266 | *(u32 *)dest = make_data_loc(ret, __dest - base); |
| 1267 | |
Masami Hiramatsu | 9178412 | 2018-04-25 21:19:01 +0900 | [diff] [blame] | 1268 | return ret; |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 1269 | } |
| 1270 | |
Christoph Hellwig | 9de1fec | 2020-06-08 21:34:44 -0700 | [diff] [blame] | 1271 | /* |
| 1272 | * Fetch a null-terminated string. Caller MUST set *(u32 *)buf with max |
| 1273 | * length and relative data location. |
| 1274 | */ |
Masami Hiramatsu | 9b960a3 | 2018-04-25 21:19:59 +0900 | [diff] [blame] | 1275 | static nokprobe_inline int |
Christoph Hellwig | 9de1fec | 2020-06-08 21:34:44 -0700 | [diff] [blame] | 1276 | fetch_store_string(unsigned long addr, void *dest, void *base) |
Masami Hiramatsu | 9b960a3 | 2018-04-25 21:19:59 +0900 | [diff] [blame] | 1277 | { |
Christoph Hellwig | 9de1fec | 2020-06-08 21:34:44 -0700 | [diff] [blame] | 1278 | int maxlen = get_loc_len(*(u32 *)dest); |
| 1279 | void *__dest; |
| 1280 | long ret; |
| 1281 | |
| 1282 | #ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE |
| 1283 | if ((unsigned long)addr < TASK_SIZE) |
| 1284 | return fetch_store_string_user(addr, dest, base); |
| 1285 | #endif |
| 1286 | |
| 1287 | if (unlikely(!maxlen)) |
| 1288 | return -ENOMEM; |
| 1289 | |
| 1290 | __dest = get_loc_data(dest, base); |
| 1291 | |
| 1292 | /* |
| 1293 | * Try to get string again, since the string can be changed while |
| 1294 | * probing. |
| 1295 | */ |
| 1296 | ret = strncpy_from_kernel_nofault(__dest, (void *)addr, maxlen); |
| 1297 | if (ret >= 0) |
| 1298 | *(u32 *)dest = make_data_loc(ret, __dest - base); |
| 1299 | |
| 1300 | return ret; |
Masami Hiramatsu | 9b960a3 | 2018-04-25 21:19:59 +0900 | [diff] [blame] | 1301 | } |
| 1302 | |
Masami Hiramatsu | e65f7ae | 2019-05-15 14:38:42 +0900 | [diff] [blame] | 1303 | static nokprobe_inline int |
| 1304 | probe_mem_read_user(void *dest, void *src, size_t size) |
| 1305 | { |
Masami Hiramatsu | 539b75b | 2019-05-22 17:27:52 +0900 | [diff] [blame] | 1306 | const void __user *uaddr = (__force const void __user *)src; |
| 1307 | |
Christoph Hellwig | c0ee37e | 2020-06-17 09:37:54 +0200 | [diff] [blame] | 1308 | return copy_from_user_nofault(dest, uaddr, size); |
Masami Hiramatsu | e65f7ae | 2019-05-15 14:38:42 +0900 | [diff] [blame] | 1309 | } |
| 1310 | |
Christoph Hellwig | 9de1fec | 2020-06-08 21:34:44 -0700 | [diff] [blame] | 1311 | static nokprobe_inline int |
| 1312 | probe_mem_read(void *dest, void *src, size_t size) |
| 1313 | { |
| 1314 | #ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE |
| 1315 | if ((unsigned long)src < TASK_SIZE) |
| 1316 | return probe_mem_read_user(dest, src, size); |
| 1317 | #endif |
Christoph Hellwig | fe55731 | 2020-06-17 09:37:53 +0200 | [diff] [blame] | 1318 | return copy_from_kernel_nofault(dest, src, size); |
Christoph Hellwig | 9de1fec | 2020-06-08 21:34:44 -0700 | [diff] [blame] | 1319 | } |
| 1320 | |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 1321 | /* Note that we don't verify it, since the code does not come from user space */ |
| 1322 | static int |
Steven Rostedt (VMware) | 8565a45 | 2021-08-19 00:13:28 -0400 | [diff] [blame] | 1323 | process_fetch_insn(struct fetch_insn *code, void *rec, void *dest, |
Masami Hiramatsu | 9178412 | 2018-04-25 21:19:01 +0900 | [diff] [blame] | 1324 | void *base) |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 1325 | { |
Steven Rostedt (VMware) | 8565a45 | 2021-08-19 00:13:28 -0400 | [diff] [blame] | 1326 | struct pt_regs *regs = rec; |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 1327 | unsigned long val; |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 1328 | |
Masami Hiramatsu | a668281 | 2018-08-29 01:18:43 +0900 | [diff] [blame] | 1329 | retry: |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 1330 | /* 1st stage: get value from context */ |
| 1331 | switch (code->op) { |
| 1332 | case FETCH_OP_REG: |
| 1333 | val = regs_get_register(regs, code->param); |
| 1334 | break; |
| 1335 | case FETCH_OP_STACK: |
| 1336 | val = regs_get_kernel_stack_nth(regs, code->param); |
| 1337 | break; |
| 1338 | case FETCH_OP_STACKP: |
| 1339 | val = kernel_stack_pointer(regs); |
| 1340 | break; |
| 1341 | case FETCH_OP_RETVAL: |
| 1342 | val = regs_return_value(regs); |
| 1343 | break; |
| 1344 | case FETCH_OP_IMM: |
| 1345 | val = code->immediate; |
| 1346 | break; |
| 1347 | case FETCH_OP_COMM: |
| 1348 | val = (unsigned long)current->comm; |
| 1349 | break; |
Masami Hiramatsu | a42e3c4 | 2019-06-20 00:08:37 +0900 | [diff] [blame] | 1350 | case FETCH_OP_DATA: |
| 1351 | val = (unsigned long)code->data; |
| 1352 | break; |
Masami Hiramatsu | a1303af | 2018-04-25 21:21:26 +0900 | [diff] [blame] | 1353 | #ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API |
| 1354 | case FETCH_OP_ARG: |
| 1355 | val = regs_get_kernel_argument(regs, code->param); |
| 1356 | break; |
| 1357 | #endif |
Masami Hiramatsu | a668281 | 2018-08-29 01:18:43 +0900 | [diff] [blame] | 1358 | case FETCH_NOP_SYMBOL: /* Ignore a place holder */ |
| 1359 | code++; |
| 1360 | goto retry; |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 1361 | default: |
| 1362 | return -EILSEQ; |
| 1363 | } |
| 1364 | code++; |
| 1365 | |
Masami Hiramatsu | 9b960a3 | 2018-04-25 21:19:59 +0900 | [diff] [blame] | 1366 | return process_fetch_insn_bottom(code, val, dest, base); |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 1367 | } |
| 1368 | NOKPROBE_SYMBOL(process_fetch_insn) |
| 1369 | |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1370 | /* Kprobe handler */ |
Masami Hiramatsu | 3da0f18 | 2014-04-17 17:18:28 +0900 | [diff] [blame] | 1371 | static nokprobe_inline void |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 1372 | __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] | 1373 | struct trace_event_file *trace_file) |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1374 | { |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 1375 | struct kprobe_trace_entry_head *entry; |
Masami Hiramatsu | e3dc9f8 | 2019-06-01 00:17:57 +0900 | [diff] [blame] | 1376 | struct trace_event_call *call = trace_probe_event_call(&tk->tp); |
Masami Hiramatsu | 8cfcf15 | 2020-01-11 01:05:31 +0900 | [diff] [blame] | 1377 | struct trace_event_buffer fbuffer; |
| 1378 | int dsize; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1379 | |
Steven Rostedt (Red Hat) | 7f1d2f8 | 2015-05-05 10:09:53 -0400 | [diff] [blame] | 1380 | WARN_ON(call != trace_file->event_call); |
Masami Hiramatsu | 41a7dd4 | 2013-05-09 14:44:49 +0900 | [diff] [blame] | 1381 | |
Steven Rostedt (Red Hat) | 09a5059 | 2015-05-13 15:21:25 -0400 | [diff] [blame] | 1382 | if (trace_trigger_soft_disabled(trace_file)) |
Steven Rostedt (Red Hat) | 13a1e4a | 2014-01-06 21:32:10 -0500 | [diff] [blame] | 1383 | return; |
Masami Hiramatsu | b882008 | 2013-05-09 14:44:54 +0900 | [diff] [blame] | 1384 | |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 1385 | dsize = __get_data_size(&tk->tp, regs); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1386 | |
Steven Rostedt (VMware) | 5e6cd84 | 2021-11-29 21:39:49 -0500 | [diff] [blame] | 1387 | entry = trace_event_buffer_reserve(&fbuffer, trace_file, |
| 1388 | sizeof(*entry) + tk->tp.size + dsize); |
| 1389 | if (!entry) |
Xiao Guangrong | 1e12a4a | 2010-01-28 09:34:27 +0800 | [diff] [blame] | 1390 | return; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1391 | |
Masami Hiramatsu | 8cfcf15 | 2020-01-11 01:05:31 +0900 | [diff] [blame] | 1392 | fbuffer.regs = regs; |
| 1393 | entry = fbuffer.entry = ring_buffer_event_data(fbuffer.event); |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 1394 | entry->ip = (unsigned long)tk->rp.kp.addr; |
Masami Hiramatsu | 9178412 | 2018-04-25 21:19:01 +0900 | [diff] [blame] | 1395 | store_trace_args(&entry[1], &tk->tp, regs, sizeof(*entry), dsize); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1396 | |
Masami Hiramatsu | 8cfcf15 | 2020-01-11 01:05:31 +0900 | [diff] [blame] | 1397 | trace_event_buffer_commit(&fbuffer); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1398 | } |
| 1399 | |
Masami Hiramatsu | 3da0f18 | 2014-04-17 17:18:28 +0900 | [diff] [blame] | 1400 | static void |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 1401 | kprobe_trace_func(struct trace_kprobe *tk, struct pt_regs *regs) |
Masami Hiramatsu | 41a7dd4 | 2013-05-09 14:44:49 +0900 | [diff] [blame] | 1402 | { |
Oleg Nesterov | b04d52e | 2013-06-20 19:38:14 +0200 | [diff] [blame] | 1403 | struct event_file_link *link; |
Masami Hiramatsu | 41a7dd4 | 2013-05-09 14:44:49 +0900 | [diff] [blame] | 1404 | |
Masami Hiramatsu | b5f935e | 2019-06-01 00:17:26 +0900 | [diff] [blame] | 1405 | trace_probe_for_each_link_rcu(link, &tk->tp) |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 1406 | __kprobe_trace_func(tk, regs, link->file); |
Masami Hiramatsu | 41a7dd4 | 2013-05-09 14:44:49 +0900 | [diff] [blame] | 1407 | } |
Masami Hiramatsu | 3da0f18 | 2014-04-17 17:18:28 +0900 | [diff] [blame] | 1408 | NOKPROBE_SYMBOL(kprobe_trace_func); |
Masami Hiramatsu | 41a7dd4 | 2013-05-09 14:44:49 +0900 | [diff] [blame] | 1409 | |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1410 | /* Kretprobe handler */ |
Masami Hiramatsu | 3da0f18 | 2014-04-17 17:18:28 +0900 | [diff] [blame] | 1411 | static nokprobe_inline void |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 1412 | __kretprobe_trace_func(struct trace_kprobe *tk, struct kretprobe_instance *ri, |
Masami Hiramatsu | 41a7dd4 | 2013-05-09 14:44:49 +0900 | [diff] [blame] | 1413 | struct pt_regs *regs, |
Steven Rostedt (Red Hat) | 7f1d2f8 | 2015-05-05 10:09:53 -0400 | [diff] [blame] | 1414 | struct trace_event_file *trace_file) |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1415 | { |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 1416 | struct kretprobe_trace_entry_head *entry; |
Masami Hiramatsu | 8cfcf15 | 2020-01-11 01:05:31 +0900 | [diff] [blame] | 1417 | struct trace_event_buffer fbuffer; |
Masami Hiramatsu | e3dc9f8 | 2019-06-01 00:17:57 +0900 | [diff] [blame] | 1418 | struct trace_event_call *call = trace_probe_event_call(&tk->tp); |
Masami Hiramatsu | 8cfcf15 | 2020-01-11 01:05:31 +0900 | [diff] [blame] | 1419 | int dsize; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1420 | |
Steven Rostedt (Red Hat) | 7f1d2f8 | 2015-05-05 10:09:53 -0400 | [diff] [blame] | 1421 | WARN_ON(call != trace_file->event_call); |
Masami Hiramatsu | 41a7dd4 | 2013-05-09 14:44:49 +0900 | [diff] [blame] | 1422 | |
Steven Rostedt (Red Hat) | 09a5059 | 2015-05-13 15:21:25 -0400 | [diff] [blame] | 1423 | if (trace_trigger_soft_disabled(trace_file)) |
Steven Rostedt (Red Hat) | 13a1e4a | 2014-01-06 21:32:10 -0500 | [diff] [blame] | 1424 | return; |
Masami Hiramatsu | b882008 | 2013-05-09 14:44:54 +0900 | [diff] [blame] | 1425 | |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 1426 | dsize = __get_data_size(&tk->tp, regs); |
Steven Rostedt (VMware) | 5e6cd84 | 2021-11-29 21:39:49 -0500 | [diff] [blame] | 1427 | |
| 1428 | entry = trace_event_buffer_reserve(&fbuffer, trace_file, |
| 1429 | sizeof(*entry) + tk->tp.size + dsize); |
| 1430 | if (!entry) |
Xiao Guangrong | 1e12a4a | 2010-01-28 09:34:27 +0800 | [diff] [blame] | 1431 | return; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1432 | |
Masami Hiramatsu | 8cfcf15 | 2020-01-11 01:05:31 +0900 | [diff] [blame] | 1433 | fbuffer.regs = regs; |
| 1434 | entry = fbuffer.entry = ring_buffer_event_data(fbuffer.event); |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 1435 | entry->func = (unsigned long)tk->rp.kp.addr; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1436 | entry->ret_ip = (unsigned long)ri->ret_addr; |
Masami Hiramatsu | 9178412 | 2018-04-25 21:19:01 +0900 | [diff] [blame] | 1437 | store_trace_args(&entry[1], &tk->tp, regs, sizeof(*entry), dsize); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1438 | |
Masami Hiramatsu | 8cfcf15 | 2020-01-11 01:05:31 +0900 | [diff] [blame] | 1439 | trace_event_buffer_commit(&fbuffer); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1440 | } |
| 1441 | |
Masami Hiramatsu | 3da0f18 | 2014-04-17 17:18:28 +0900 | [diff] [blame] | 1442 | static void |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 1443 | kretprobe_trace_func(struct trace_kprobe *tk, struct kretprobe_instance *ri, |
Masami Hiramatsu | 41a7dd4 | 2013-05-09 14:44:49 +0900 | [diff] [blame] | 1444 | struct pt_regs *regs) |
| 1445 | { |
Oleg Nesterov | b04d52e | 2013-06-20 19:38:14 +0200 | [diff] [blame] | 1446 | struct event_file_link *link; |
Masami Hiramatsu | 41a7dd4 | 2013-05-09 14:44:49 +0900 | [diff] [blame] | 1447 | |
Masami Hiramatsu | b5f935e | 2019-06-01 00:17:26 +0900 | [diff] [blame] | 1448 | trace_probe_for_each_link_rcu(link, &tk->tp) |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 1449 | __kretprobe_trace_func(tk, ri, regs, link->file); |
Masami Hiramatsu | 41a7dd4 | 2013-05-09 14:44:49 +0900 | [diff] [blame] | 1450 | } |
Masami Hiramatsu | 3da0f18 | 2014-04-17 17:18:28 +0900 | [diff] [blame] | 1451 | NOKPROBE_SYMBOL(kretprobe_trace_func); |
Masami Hiramatsu | 41a7dd4 | 2013-05-09 14:44:49 +0900 | [diff] [blame] | 1452 | |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1453 | /* Event entry printers */ |
Masami Hiramatsu | b62fdd9 | 2013-05-13 20:58:39 +0900 | [diff] [blame] | 1454 | static enum print_line_t |
Steven Rostedt | a9a5776 | 2010-04-22 18:46:14 -0400 | [diff] [blame] | 1455 | print_kprobe_event(struct trace_iterator *iter, int flags, |
| 1456 | struct trace_event *event) |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1457 | { |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 1458 | struct kprobe_trace_entry_head *field; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1459 | struct trace_seq *s = &iter->seq; |
Masami Hiramatsu | eca0d91 | 2009-09-10 19:53:38 -0400 | [diff] [blame] | 1460 | struct trace_probe *tp; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1461 | |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 1462 | field = (struct kprobe_trace_entry_head *)iter->ent; |
Masami Hiramatsu | 60d53e2 | 2019-06-20 00:07:20 +0900 | [diff] [blame] | 1463 | tp = trace_probe_primary_from_call( |
| 1464 | container_of(event, struct trace_event_call, event)); |
| 1465 | if (WARN_ON_ONCE(!tp)) |
| 1466 | goto out; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1467 | |
Masami Hiramatsu | b55ce203 | 2019-06-01 00:17:47 +0900 | [diff] [blame] | 1468 | trace_seq_printf(s, "%s: (", trace_probe_name(tp)); |
Masami Hiramatsu | 6e9f23d | 2009-09-10 19:53:45 -0400 | [diff] [blame] | 1469 | |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1470 | 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] | 1471 | goto out; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1472 | |
Steven Rostedt (Red Hat) | 85224da | 2014-11-12 15:18:16 -0500 | [diff] [blame] | 1473 | trace_seq_putc(s, ')'); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1474 | |
Masami Hiramatsu | 56de763 | 2018-04-25 21:16:36 +0900 | [diff] [blame] | 1475 | if (print_probe_args(s, tp->args, tp->nr_args, |
| 1476 | (u8 *)&field[1], field) < 0) |
| 1477 | goto out; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1478 | |
Steven Rostedt (Red Hat) | 85224da | 2014-11-12 15:18:16 -0500 | [diff] [blame] | 1479 | trace_seq_putc(s, '\n'); |
| 1480 | out: |
| 1481 | return trace_handle_return(s); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1482 | } |
| 1483 | |
Masami Hiramatsu | b62fdd9 | 2013-05-13 20:58:39 +0900 | [diff] [blame] | 1484 | static enum print_line_t |
Steven Rostedt | a9a5776 | 2010-04-22 18:46:14 -0400 | [diff] [blame] | 1485 | print_kretprobe_event(struct trace_iterator *iter, int flags, |
| 1486 | struct trace_event *event) |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1487 | { |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 1488 | struct kretprobe_trace_entry_head *field; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1489 | struct trace_seq *s = &iter->seq; |
Masami Hiramatsu | eca0d91 | 2009-09-10 19:53:38 -0400 | [diff] [blame] | 1490 | struct trace_probe *tp; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1491 | |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 1492 | field = (struct kretprobe_trace_entry_head *)iter->ent; |
Masami Hiramatsu | 60d53e2 | 2019-06-20 00:07:20 +0900 | [diff] [blame] | 1493 | tp = trace_probe_primary_from_call( |
| 1494 | container_of(event, struct trace_event_call, event)); |
| 1495 | if (WARN_ON_ONCE(!tp)) |
| 1496 | goto out; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1497 | |
Masami Hiramatsu | b55ce203 | 2019-06-01 00:17:47 +0900 | [diff] [blame] | 1498 | trace_seq_printf(s, "%s: (", trace_probe_name(tp)); |
Masami Hiramatsu | 6e9f23d | 2009-09-10 19:53:45 -0400 | [diff] [blame] | 1499 | |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1500 | 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] | 1501 | goto out; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1502 | |
Steven Rostedt (Red Hat) | 85224da | 2014-11-12 15:18:16 -0500 | [diff] [blame] | 1503 | trace_seq_puts(s, " <- "); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1504 | |
| 1505 | 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] | 1506 | goto out; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1507 | |
Steven Rostedt (Red Hat) | 85224da | 2014-11-12 15:18:16 -0500 | [diff] [blame] | 1508 | trace_seq_putc(s, ')'); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1509 | |
Masami Hiramatsu | 56de763 | 2018-04-25 21:16:36 +0900 | [diff] [blame] | 1510 | if (print_probe_args(s, tp->args, tp->nr_args, |
| 1511 | (u8 *)&field[1], field) < 0) |
| 1512 | goto out; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1513 | |
Steven Rostedt (Red Hat) | 85224da | 2014-11-12 15:18:16 -0500 | [diff] [blame] | 1514 | trace_seq_putc(s, '\n'); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1515 | |
Steven Rostedt (Red Hat) | 85224da | 2014-11-12 15:18:16 -0500 | [diff] [blame] | 1516 | out: |
| 1517 | return trace_handle_return(s); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1518 | } |
| 1519 | |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1520 | |
Steven Rostedt (Red Hat) | 2425bcb | 2015-05-05 11:45:27 -0400 | [diff] [blame] | 1521 | static int kprobe_event_define_fields(struct trace_event_call *event_call) |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1522 | { |
Masami Hiramatsu | eeb07b0 | 2018-04-25 21:17:05 +0900 | [diff] [blame] | 1523 | int ret; |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 1524 | struct kprobe_trace_entry_head field; |
Masami Hiramatsu | 60d53e2 | 2019-06-20 00:07:20 +0900 | [diff] [blame] | 1525 | struct trace_probe *tp; |
| 1526 | |
| 1527 | tp = trace_probe_primary_from_call(event_call); |
| 1528 | if (WARN_ON_ONCE(!tp)) |
| 1529 | return -ENOENT; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1530 | |
Masami Hiramatsu | a703d94 | 2009-10-07 18:28:07 -0400 | [diff] [blame] | 1531 | DEFINE_FIELD(unsigned long, ip, FIELD_STRING_IP, 0); |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 1532 | |
Masami Hiramatsu | 60d53e2 | 2019-06-20 00:07:20 +0900 | [diff] [blame] | 1533 | return traceprobe_define_arg_fields(event_call, sizeof(field), tp); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1534 | } |
| 1535 | |
Steven Rostedt (Red Hat) | 2425bcb | 2015-05-05 11:45:27 -0400 | [diff] [blame] | 1536 | static int kretprobe_event_define_fields(struct trace_event_call *event_call) |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1537 | { |
Masami Hiramatsu | eeb07b0 | 2018-04-25 21:17:05 +0900 | [diff] [blame] | 1538 | int ret; |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 1539 | struct kretprobe_trace_entry_head field; |
Masami Hiramatsu | 60d53e2 | 2019-06-20 00:07:20 +0900 | [diff] [blame] | 1540 | struct trace_probe *tp; |
| 1541 | |
| 1542 | tp = trace_probe_primary_from_call(event_call); |
| 1543 | if (WARN_ON_ONCE(!tp)) |
| 1544 | return -ENOENT; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1545 | |
Masami Hiramatsu | a703d94 | 2009-10-07 18:28:07 -0400 | [diff] [blame] | 1546 | DEFINE_FIELD(unsigned long, func, FIELD_STRING_FUNC, 0); |
| 1547 | DEFINE_FIELD(unsigned long, ret_ip, FIELD_STRING_RETIP, 0); |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 1548 | |
Masami Hiramatsu | 60d53e2 | 2019-06-20 00:07:20 +0900 | [diff] [blame] | 1549 | return traceprobe_define_arg_fields(event_call, sizeof(field), tp); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1550 | } |
| 1551 | |
Li Zefan | 07b139c | 2009-12-21 14:27:35 +0800 | [diff] [blame] | 1552 | #ifdef CONFIG_PERF_EVENTS |
Masami Hiramatsu | e08d1c6 | 2009-09-10 19:53:30 -0400 | [diff] [blame] | 1553 | |
| 1554 | /* Kprobe profile handler */ |
Josef Bacik | 9802d86 | 2017-12-11 11:36:48 -0500 | [diff] [blame] | 1555 | static int |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 1556 | kprobe_perf_func(struct trace_kprobe *tk, struct pt_regs *regs) |
Masami Hiramatsu | e08d1c6 | 2009-09-10 19:53:30 -0400 | [diff] [blame] | 1557 | { |
Masami Hiramatsu | e3dc9f8 | 2019-06-01 00:17:57 +0900 | [diff] [blame] | 1558 | struct trace_event_call *call = trace_probe_event_call(&tk->tp); |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 1559 | struct kprobe_trace_entry_head *entry; |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 1560 | struct hlist_head *head; |
Masami Hiramatsu | e09c861 | 2010-07-05 15:54:45 -0300 | [diff] [blame] | 1561 | int size, __size, dsize; |
Peter Zijlstra | 4ed7c92 | 2009-11-23 11:37:29 +0100 | [diff] [blame] | 1562 | int rctx; |
Masami Hiramatsu | e08d1c6 | 2009-09-10 19:53:30 -0400 | [diff] [blame] | 1563 | |
Josef Bacik | 9802d86 | 2017-12-11 11:36:48 -0500 | [diff] [blame] | 1564 | if (bpf_prog_array_valid(call)) { |
Masami Hiramatsu | 66665ad | 2018-01-13 02:54:33 +0900 | [diff] [blame] | 1565 | unsigned long orig_ip = instruction_pointer(regs); |
Josef Bacik | 9802d86 | 2017-12-11 11:36:48 -0500 | [diff] [blame] | 1566 | int ret; |
| 1567 | |
| 1568 | ret = trace_call_bpf(call, regs); |
| 1569 | |
| 1570 | /* |
| 1571 | * 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] | 1572 | * pt_regs, and if so return 1 so that we don't do the |
| 1573 | * single stepping. |
Josef Bacik | 9802d86 | 2017-12-11 11:36:48 -0500 | [diff] [blame] | 1574 | */ |
Masami Hiramatsu | cce188b | 2018-06-20 01:15:45 +0900 | [diff] [blame] | 1575 | if (orig_ip != instruction_pointer(regs)) |
Josef Bacik | 9802d86 | 2017-12-11 11:36:48 -0500 | [diff] [blame] | 1576 | return 1; |
Josef Bacik | 9802d86 | 2017-12-11 11:36:48 -0500 | [diff] [blame] | 1577 | if (!ret) |
| 1578 | return 0; |
| 1579 | } |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 1580 | |
Oleg Nesterov | 288e984 | 2013-06-20 19:38:06 +0200 | [diff] [blame] | 1581 | head = this_cpu_ptr(call->perf_events); |
| 1582 | if (hlist_empty(head)) |
Josef Bacik | 9802d86 | 2017-12-11 11:36:48 -0500 | [diff] [blame] | 1583 | return 0; |
Oleg Nesterov | 288e984 | 2013-06-20 19:38:06 +0200 | [diff] [blame] | 1584 | |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 1585 | dsize = __get_data_size(&tk->tp, regs); |
| 1586 | __size = sizeof(*entry) + tk->tp.size + dsize; |
Masami Hiramatsu | 74ebb63 | 2009-09-14 16:49:28 -0400 | [diff] [blame] | 1587 | size = ALIGN(__size + sizeof(u32), sizeof(u64)); |
| 1588 | size -= sizeof(u32); |
Masami Hiramatsu | e08d1c6 | 2009-09-10 19:53:30 -0400 | [diff] [blame] | 1589 | |
Alexei Starovoitov | 1e1dcd9 | 2016-04-06 18:43:24 -0700 | [diff] [blame] | 1590 | entry = perf_trace_buf_alloc(size, NULL, &rctx); |
Xiao Guangrong | 430ad5a | 2010-01-28 09:32:29 +0800 | [diff] [blame] | 1591 | if (!entry) |
Josef Bacik | 9802d86 | 2017-12-11 11:36:48 -0500 | [diff] [blame] | 1592 | return 0; |
Frederic Weisbecker | ce71b9d | 2009-11-22 05:26:55 +0100 | [diff] [blame] | 1593 | |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 1594 | entry->ip = (unsigned long)tk->rp.kp.addr; |
Masami Hiramatsu | e09c861 | 2010-07-05 15:54:45 -0300 | [diff] [blame] | 1595 | memset(&entry[1], 0, dsize); |
Masami Hiramatsu | 9178412 | 2018-04-25 21:19:01 +0900 | [diff] [blame] | 1596 | store_trace_args(&entry[1], &tk->tp, regs, sizeof(*entry), dsize); |
Alexei Starovoitov | 1e1dcd9 | 2016-04-06 18:43:24 -0700 | [diff] [blame] | 1597 | perf_trace_buf_submit(entry, size, rctx, call->event.type, 1, regs, |
Peter Zijlstra | 8fd0fbb | 2017-10-11 09:45:29 +0200 | [diff] [blame] | 1598 | head, NULL); |
Josef Bacik | 9802d86 | 2017-12-11 11:36:48 -0500 | [diff] [blame] | 1599 | return 0; |
Masami Hiramatsu | e08d1c6 | 2009-09-10 19:53:30 -0400 | [diff] [blame] | 1600 | } |
Masami Hiramatsu | 3da0f18 | 2014-04-17 17:18:28 +0900 | [diff] [blame] | 1601 | NOKPROBE_SYMBOL(kprobe_perf_func); |
Masami Hiramatsu | e08d1c6 | 2009-09-10 19:53:30 -0400 | [diff] [blame] | 1602 | |
| 1603 | /* Kretprobe profile handler */ |
Masami Hiramatsu | 3da0f18 | 2014-04-17 17:18:28 +0900 | [diff] [blame] | 1604 | static void |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 1605 | kretprobe_perf_func(struct trace_kprobe *tk, struct kretprobe_instance *ri, |
Masami Hiramatsu | 2b106aa | 2013-05-09 14:44:41 +0900 | [diff] [blame] | 1606 | struct pt_regs *regs) |
Masami Hiramatsu | e08d1c6 | 2009-09-10 19:53:30 -0400 | [diff] [blame] | 1607 | { |
Masami Hiramatsu | e3dc9f8 | 2019-06-01 00:17:57 +0900 | [diff] [blame] | 1608 | struct trace_event_call *call = trace_probe_event_call(&tk->tp); |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 1609 | struct kretprobe_trace_entry_head *entry; |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 1610 | struct hlist_head *head; |
Masami Hiramatsu | e09c861 | 2010-07-05 15:54:45 -0300 | [diff] [blame] | 1611 | int size, __size, dsize; |
Peter Zijlstra | 4ed7c92 | 2009-11-23 11:37:29 +0100 | [diff] [blame] | 1612 | int rctx; |
Masami Hiramatsu | e08d1c6 | 2009-09-10 19:53:30 -0400 | [diff] [blame] | 1613 | |
Yonghong Song | e87c6bc | 2017-10-23 23:53:08 -0700 | [diff] [blame] | 1614 | if (bpf_prog_array_valid(call) && !trace_call_bpf(call, regs)) |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 1615 | return; |
| 1616 | |
Oleg Nesterov | 288e984 | 2013-06-20 19:38:06 +0200 | [diff] [blame] | 1617 | head = this_cpu_ptr(call->perf_events); |
| 1618 | if (hlist_empty(head)) |
| 1619 | return; |
| 1620 | |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 1621 | dsize = __get_data_size(&tk->tp, regs); |
| 1622 | __size = sizeof(*entry) + tk->tp.size + dsize; |
Masami Hiramatsu | 74ebb63 | 2009-09-14 16:49:28 -0400 | [diff] [blame] | 1623 | size = ALIGN(__size + sizeof(u32), sizeof(u64)); |
| 1624 | size -= sizeof(u32); |
Masami Hiramatsu | e08d1c6 | 2009-09-10 19:53:30 -0400 | [diff] [blame] | 1625 | |
Alexei Starovoitov | 1e1dcd9 | 2016-04-06 18:43:24 -0700 | [diff] [blame] | 1626 | entry = perf_trace_buf_alloc(size, NULL, &rctx); |
Xiao Guangrong | 430ad5a | 2010-01-28 09:32:29 +0800 | [diff] [blame] | 1627 | if (!entry) |
Xiao Guangrong | 1e12a4a | 2010-01-28 09:34:27 +0800 | [diff] [blame] | 1628 | return; |
Frederic Weisbecker | ce71b9d | 2009-11-22 05:26:55 +0100 | [diff] [blame] | 1629 | |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 1630 | entry->func = (unsigned long)tk->rp.kp.addr; |
Masami Hiramatsu | a1a138d | 2009-09-25 11:20:12 -0700 | [diff] [blame] | 1631 | entry->ret_ip = (unsigned long)ri->ret_addr; |
Masami Hiramatsu | 9178412 | 2018-04-25 21:19:01 +0900 | [diff] [blame] | 1632 | store_trace_args(&entry[1], &tk->tp, regs, sizeof(*entry), dsize); |
Alexei Starovoitov | 1e1dcd9 | 2016-04-06 18:43:24 -0700 | [diff] [blame] | 1633 | perf_trace_buf_submit(entry, size, rctx, call->event.type, 1, regs, |
Peter Zijlstra | 8fd0fbb | 2017-10-11 09:45:29 +0200 | [diff] [blame] | 1634 | head, NULL); |
Masami Hiramatsu | e08d1c6 | 2009-09-10 19:53:30 -0400 | [diff] [blame] | 1635 | } |
Masami Hiramatsu | 3da0f18 | 2014-04-17 17:18:28 +0900 | [diff] [blame] | 1636 | NOKPROBE_SYMBOL(kretprobe_perf_func); |
Yonghong Song | 41bdc4b | 2018-05-24 11:21:09 -0700 | [diff] [blame] | 1637 | |
| 1638 | int bpf_get_kprobe_info(const struct perf_event *event, u32 *fd_type, |
| 1639 | const char **symbol, u64 *probe_offset, |
| 1640 | u64 *probe_addr, bool perf_type_tracepoint) |
| 1641 | { |
| 1642 | const char *pevent = trace_event_name(event->tp_event); |
| 1643 | const char *group = event->tp_event->class->system; |
| 1644 | struct trace_kprobe *tk; |
| 1645 | |
| 1646 | if (perf_type_tracepoint) |
| 1647 | tk = find_trace_kprobe(pevent, group); |
| 1648 | else |
Jean-Philippe Brucker | 22d5bd6 | 2020-06-08 14:45:32 +0200 | [diff] [blame] | 1649 | tk = trace_kprobe_primary_from_call(event->tp_event); |
Yonghong Song | 41bdc4b | 2018-05-24 11:21:09 -0700 | [diff] [blame] | 1650 | if (!tk) |
| 1651 | return -EINVAL; |
| 1652 | |
| 1653 | *fd_type = trace_kprobe_is_return(tk) ? BPF_FD_TYPE_KRETPROBE |
| 1654 | : BPF_FD_TYPE_KPROBE; |
| 1655 | if (tk->symbol) { |
| 1656 | *symbol = tk->symbol; |
| 1657 | *probe_offset = tk->rp.kp.offset; |
| 1658 | *probe_addr = 0; |
| 1659 | } else { |
| 1660 | *symbol = NULL; |
| 1661 | *probe_offset = 0; |
| 1662 | *probe_addr = (unsigned long)tk->rp.kp.addr; |
| 1663 | } |
| 1664 | return 0; |
| 1665 | } |
Li Zefan | 07b139c | 2009-12-21 14:27:35 +0800 | [diff] [blame] | 1666 | #endif /* CONFIG_PERF_EVENTS */ |
Masami Hiramatsu | 50d7805 | 2009-09-14 16:49:20 -0400 | [diff] [blame] | 1667 | |
Oleg Nesterov | 3fe3d61 | 2013-06-20 19:38:09 +0200 | [diff] [blame] | 1668 | /* |
| 1669 | * called by perf_trace_init() or __ftrace_set_clr_event() under event_mutex. |
| 1670 | * |
| 1671 | * kprobe_trace_self_tests_init() does enable_trace_probe/disable_trace_probe |
| 1672 | * lockless, but we can't race with this __init function. |
| 1673 | */ |
Steven Rostedt (Red Hat) | 2425bcb | 2015-05-05 11:45:27 -0400 | [diff] [blame] | 1674 | static int kprobe_register(struct trace_event_call *event, |
Masami Hiramatsu | fbc1963 | 2014-04-17 17:18:00 +0900 | [diff] [blame] | 1675 | enum trace_reg type, void *data) |
Steven Rostedt | 2239291 | 2010-04-21 12:27:06 -0400 | [diff] [blame] | 1676 | { |
Steven Rostedt (Red Hat) | 7f1d2f8 | 2015-05-05 10:09:53 -0400 | [diff] [blame] | 1677 | struct trace_event_file *file = data; |
Masami Hiramatsu | 1538f88 | 2011-06-27 16:26:44 +0900 | [diff] [blame] | 1678 | |
Steven Rostedt | 2239291 | 2010-04-21 12:27:06 -0400 | [diff] [blame] | 1679 | switch (type) { |
| 1680 | case TRACE_REG_REGISTER: |
Masami Hiramatsu | 60d53e2 | 2019-06-20 00:07:20 +0900 | [diff] [blame] | 1681 | return enable_trace_kprobe(event, file); |
Steven Rostedt | 2239291 | 2010-04-21 12:27:06 -0400 | [diff] [blame] | 1682 | case TRACE_REG_UNREGISTER: |
Masami Hiramatsu | 60d53e2 | 2019-06-20 00:07:20 +0900 | [diff] [blame] | 1683 | return disable_trace_kprobe(event, file); |
Steven Rostedt | 2239291 | 2010-04-21 12:27:06 -0400 | [diff] [blame] | 1684 | |
| 1685 | #ifdef CONFIG_PERF_EVENTS |
| 1686 | case TRACE_REG_PERF_REGISTER: |
Masami Hiramatsu | 60d53e2 | 2019-06-20 00:07:20 +0900 | [diff] [blame] | 1687 | return enable_trace_kprobe(event, NULL); |
Steven Rostedt | 2239291 | 2010-04-21 12:27:06 -0400 | [diff] [blame] | 1688 | case TRACE_REG_PERF_UNREGISTER: |
Masami Hiramatsu | 60d53e2 | 2019-06-20 00:07:20 +0900 | [diff] [blame] | 1689 | return disable_trace_kprobe(event, NULL); |
Jiri Olsa | ceec0b6 | 2012-02-15 15:51:49 +0100 | [diff] [blame] | 1690 | case TRACE_REG_PERF_OPEN: |
| 1691 | case TRACE_REG_PERF_CLOSE: |
Jiri Olsa | 489c75c | 2012-02-15 15:51:50 +0100 | [diff] [blame] | 1692 | case TRACE_REG_PERF_ADD: |
| 1693 | case TRACE_REG_PERF_DEL: |
Jiri Olsa | ceec0b6 | 2012-02-15 15:51:49 +0100 | [diff] [blame] | 1694 | return 0; |
Steven Rostedt | 2239291 | 2010-04-21 12:27:06 -0400 | [diff] [blame] | 1695 | #endif |
| 1696 | } |
| 1697 | return 0; |
| 1698 | } |
Masami Hiramatsu | 50d7805 | 2009-09-14 16:49:20 -0400 | [diff] [blame] | 1699 | |
Masami Hiramatsu | 3da0f18 | 2014-04-17 17:18:28 +0900 | [diff] [blame] | 1700 | static int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs) |
Masami Hiramatsu | 50d7805 | 2009-09-14 16:49:20 -0400 | [diff] [blame] | 1701 | { |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 1702 | struct trace_kprobe *tk = container_of(kp, struct trace_kprobe, rp.kp); |
Josef Bacik | 9802d86 | 2017-12-11 11:36:48 -0500 | [diff] [blame] | 1703 | int ret = 0; |
Masami Hiramatsu | 50d7805 | 2009-09-14 16:49:20 -0400 | [diff] [blame] | 1704 | |
Martin KaFai Lau | a7636d9 | 2016-02-03 12:28:28 -0800 | [diff] [blame] | 1705 | raw_cpu_inc(*tk->nhit); |
Masami Hiramatsu | 48182bd | 2013-05-09 14:44:36 +0900 | [diff] [blame] | 1706 | |
Masami Hiramatsu | 747774d | 2019-06-01 00:17:37 +0900 | [diff] [blame] | 1707 | if (trace_probe_test_flag(&tk->tp, TP_FLAG_TRACE)) |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 1708 | kprobe_trace_func(tk, regs); |
Li Zefan | 07b139c | 2009-12-21 14:27:35 +0800 | [diff] [blame] | 1709 | #ifdef CONFIG_PERF_EVENTS |
Masami Hiramatsu | 747774d | 2019-06-01 00:17:37 +0900 | [diff] [blame] | 1710 | if (trace_probe_test_flag(&tk->tp, TP_FLAG_PROFILE)) |
Josef Bacik | 9802d86 | 2017-12-11 11:36:48 -0500 | [diff] [blame] | 1711 | ret = kprobe_perf_func(tk, regs); |
Li Zefan | 07b139c | 2009-12-21 14:27:35 +0800 | [diff] [blame] | 1712 | #endif |
Josef Bacik | 9802d86 | 2017-12-11 11:36:48 -0500 | [diff] [blame] | 1713 | return ret; |
Masami Hiramatsu | e08d1c6 | 2009-09-10 19:53:30 -0400 | [diff] [blame] | 1714 | } |
Masami Hiramatsu | 3da0f18 | 2014-04-17 17:18:28 +0900 | [diff] [blame] | 1715 | NOKPROBE_SYMBOL(kprobe_dispatcher); |
Masami Hiramatsu | e08d1c6 | 2009-09-10 19:53:30 -0400 | [diff] [blame] | 1716 | |
Masami Hiramatsu | 3da0f18 | 2014-04-17 17:18:28 +0900 | [diff] [blame] | 1717 | static int |
| 1718 | kretprobe_dispatcher(struct kretprobe_instance *ri, struct pt_regs *regs) |
Masami Hiramatsu | 50d7805 | 2009-09-14 16:49:20 -0400 | [diff] [blame] | 1719 | { |
Peter Zijlstra | d741bf4 | 2020-08-29 22:03:24 +0900 | [diff] [blame] | 1720 | struct kretprobe *rp = get_kretprobe(ri); |
| 1721 | struct trace_kprobe *tk = container_of(rp, struct trace_kprobe, rp); |
Masami Hiramatsu | 50d7805 | 2009-09-14 16:49:20 -0400 | [diff] [blame] | 1722 | |
Martin KaFai Lau | a7636d9 | 2016-02-03 12:28:28 -0800 | [diff] [blame] | 1723 | raw_cpu_inc(*tk->nhit); |
Masami Hiramatsu | 48182bd | 2013-05-09 14:44:36 +0900 | [diff] [blame] | 1724 | |
Masami Hiramatsu | 747774d | 2019-06-01 00:17:37 +0900 | [diff] [blame] | 1725 | if (trace_probe_test_flag(&tk->tp, TP_FLAG_TRACE)) |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 1726 | kretprobe_trace_func(tk, ri, regs); |
Li Zefan | 07b139c | 2009-12-21 14:27:35 +0800 | [diff] [blame] | 1727 | #ifdef CONFIG_PERF_EVENTS |
Masami Hiramatsu | 747774d | 2019-06-01 00:17:37 +0900 | [diff] [blame] | 1728 | if (trace_probe_test_flag(&tk->tp, TP_FLAG_PROFILE)) |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 1729 | kretprobe_perf_func(tk, ri, regs); |
Li Zefan | 07b139c | 2009-12-21 14:27:35 +0800 | [diff] [blame] | 1730 | #endif |
Ingo Molnar | f2cc020 | 2021-03-23 18:49:35 +0100 | [diff] [blame] | 1731 | return 0; /* We don't tweak kernel, so just return 0 */ |
Masami Hiramatsu | 50d7805 | 2009-09-14 16:49:20 -0400 | [diff] [blame] | 1732 | } |
Masami Hiramatsu | 3da0f18 | 2014-04-17 17:18:28 +0900 | [diff] [blame] | 1733 | NOKPROBE_SYMBOL(kretprobe_dispatcher); |
Masami Hiramatsu | e08d1c6 | 2009-09-10 19:53:30 -0400 | [diff] [blame] | 1734 | |
Steven Rostedt | a9a5776 | 2010-04-22 18:46:14 -0400 | [diff] [blame] | 1735 | static struct trace_event_functions kretprobe_funcs = { |
| 1736 | .trace = print_kretprobe_event |
| 1737 | }; |
| 1738 | |
| 1739 | static struct trace_event_functions kprobe_funcs = { |
| 1740 | .trace = print_kprobe_event |
| 1741 | }; |
| 1742 | |
Peter Zijlstra | 04ae87a | 2019-10-24 22:26:59 +0200 | [diff] [blame] | 1743 | static struct trace_event_fields kretprobe_fields_array[] = { |
| 1744 | { .type = TRACE_FUNCTION_TYPE, |
| 1745 | .define_fields = kretprobe_event_define_fields }, |
| 1746 | {} |
| 1747 | }; |
| 1748 | |
| 1749 | static struct trace_event_fields kprobe_fields_array[] = { |
| 1750 | { .type = TRACE_FUNCTION_TYPE, |
| 1751 | .define_fields = kprobe_event_define_fields }, |
| 1752 | {} |
| 1753 | }; |
| 1754 | |
Masami Hiramatsu | e3dc9f8 | 2019-06-01 00:17:57 +0900 | [diff] [blame] | 1755 | static inline void init_trace_event_call(struct trace_kprobe *tk) |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1756 | { |
Masami Hiramatsu | e3dc9f8 | 2019-06-01 00:17:57 +0900 | [diff] [blame] | 1757 | struct trace_event_call *call = trace_probe_event_call(&tk->tp); |
| 1758 | |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 1759 | if (trace_kprobe_is_return(tk)) { |
Steven Rostedt | 80decc7 | 2010-04-23 10:00:22 -0400 | [diff] [blame] | 1760 | call->event.funcs = &kretprobe_funcs; |
Peter Zijlstra | 04ae87a | 2019-10-24 22:26:59 +0200 | [diff] [blame] | 1761 | call->class->fields_array = kretprobe_fields_array; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1762 | } else { |
Steven Rostedt | 80decc7 | 2010-04-23 10:00:22 -0400 | [diff] [blame] | 1763 | call->event.funcs = &kprobe_funcs; |
Peter Zijlstra | 04ae87a | 2019-10-24 22:26:59 +0200 | [diff] [blame] | 1764 | call->class->fields_array = kprobe_fields_array; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1765 | } |
Song Liu | e12f03d | 2017-12-06 14:45:15 -0800 | [diff] [blame] | 1766 | |
| 1767 | call->flags = TRACE_EVENT_FL_KPROBE; |
| 1768 | call->class->reg = kprobe_register; |
Song Liu | e12f03d | 2017-12-06 14:45:15 -0800 | [diff] [blame] | 1769 | } |
| 1770 | |
| 1771 | static int register_kprobe_event(struct trace_kprobe *tk) |
| 1772 | { |
Masami Hiramatsu | e3dc9f8 | 2019-06-01 00:17:57 +0900 | [diff] [blame] | 1773 | init_trace_event_call(tk); |
Song Liu | e12f03d | 2017-12-06 14:45:15 -0800 | [diff] [blame] | 1774 | |
Masami Hiramatsu | 46e5376 | 2019-06-01 00:17:16 +0900 | [diff] [blame] | 1775 | return trace_probe_register_event_call(&tk->tp); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1776 | } |
| 1777 | |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 1778 | static int unregister_kprobe_event(struct trace_kprobe *tk) |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1779 | { |
Masami Hiramatsu | 46e5376 | 2019-06-01 00:17:16 +0900 | [diff] [blame] | 1780 | return trace_probe_unregister_event_call(&tk->tp); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1781 | } |
| 1782 | |
Song Liu | e12f03d | 2017-12-06 14:45:15 -0800 | [diff] [blame] | 1783 | #ifdef CONFIG_PERF_EVENTS |
| 1784 | /* create a trace_kprobe, but don't add it to global lists */ |
| 1785 | struct trace_event_call * |
| 1786 | create_local_trace_kprobe(char *func, void *addr, unsigned long offs, |
| 1787 | bool is_return) |
| 1788 | { |
Steven Rostedt (VMware) | 007517a | 2021-08-19 00:13:27 -0400 | [diff] [blame] | 1789 | enum probe_print_type ptype; |
Song Liu | e12f03d | 2017-12-06 14:45:15 -0800 | [diff] [blame] | 1790 | struct trace_kprobe *tk; |
| 1791 | int ret; |
| 1792 | char *event; |
| 1793 | |
| 1794 | /* |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 1795 | * 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] | 1796 | * searched in find_trace_kprobe(). Therefore, there is no concern of |
| 1797 | * duplicated name here. |
| 1798 | */ |
| 1799 | event = func ? func : "DUMMY_EVENT"; |
| 1800 | |
| 1801 | tk = alloc_trace_kprobe(KPROBE_EVENT_SYSTEM, event, (void *)addr, func, |
| 1802 | offs, 0 /* maxactive */, 0 /* nargs */, |
| 1803 | is_return); |
| 1804 | |
| 1805 | if (IS_ERR(tk)) { |
| 1806 | pr_info("Failed to allocate trace_probe.(%d)\n", |
| 1807 | (int)PTR_ERR(tk)); |
| 1808 | return ERR_CAST(tk); |
| 1809 | } |
| 1810 | |
Masami Hiramatsu | e3dc9f8 | 2019-06-01 00:17:57 +0900 | [diff] [blame] | 1811 | init_trace_event_call(tk); |
Song Liu | e12f03d | 2017-12-06 14:45:15 -0800 | [diff] [blame] | 1812 | |
Steven Rostedt (VMware) | 007517a | 2021-08-19 00:13:27 -0400 | [diff] [blame] | 1813 | ptype = trace_kprobe_is_return(tk) ? |
| 1814 | PROBE_PRINT_RETURN : PROBE_PRINT_NORMAL; |
| 1815 | if (traceprobe_set_print_fmt(&tk->tp, ptype) < 0) { |
Song Liu | e12f03d | 2017-12-06 14:45:15 -0800 | [diff] [blame] | 1816 | ret = -ENOMEM; |
| 1817 | goto error; |
| 1818 | } |
| 1819 | |
| 1820 | ret = __register_trace_kprobe(tk); |
Masami Hiramatsu | f730e0f | 2019-06-01 00:16:46 +0900 | [diff] [blame] | 1821 | if (ret < 0) |
Song Liu | e12f03d | 2017-12-06 14:45:15 -0800 | [diff] [blame] | 1822 | goto error; |
| 1823 | |
Masami Hiramatsu | e3dc9f8 | 2019-06-01 00:17:57 +0900 | [diff] [blame] | 1824 | return trace_probe_event_call(&tk->tp); |
Song Liu | e12f03d | 2017-12-06 14:45:15 -0800 | [diff] [blame] | 1825 | error: |
| 1826 | free_trace_kprobe(tk); |
| 1827 | return ERR_PTR(ret); |
| 1828 | } |
| 1829 | |
| 1830 | void destroy_local_trace_kprobe(struct trace_event_call *event_call) |
| 1831 | { |
| 1832 | struct trace_kprobe *tk; |
| 1833 | |
Masami Hiramatsu | 60d53e2 | 2019-06-20 00:07:20 +0900 | [diff] [blame] | 1834 | tk = trace_kprobe_primary_from_call(event_call); |
| 1835 | if (unlikely(!tk)) |
| 1836 | return; |
Song Liu | e12f03d | 2017-12-06 14:45:15 -0800 | [diff] [blame] | 1837 | |
| 1838 | if (trace_probe_is_enabled(&tk->tp)) { |
| 1839 | WARN_ON(1); |
| 1840 | return; |
| 1841 | } |
| 1842 | |
| 1843 | __unregister_trace_kprobe(tk); |
Jiri Olsa | 0fc8c35 | 2018-07-09 16:19:06 +0200 | [diff] [blame] | 1844 | |
Song Liu | e12f03d | 2017-12-06 14:45:15 -0800 | [diff] [blame] | 1845 | free_trace_kprobe(tk); |
| 1846 | } |
| 1847 | #endif /* CONFIG_PERF_EVENTS */ |
| 1848 | |
Masami Hiramatsu | 970988e | 2019-05-22 17:32:35 +0900 | [diff] [blame] | 1849 | static __init void enable_boot_kprobe_events(void) |
| 1850 | { |
| 1851 | struct trace_array *tr = top_trace_array(); |
| 1852 | struct trace_event_file *file; |
| 1853 | struct trace_kprobe *tk; |
| 1854 | struct dyn_event *pos; |
| 1855 | |
| 1856 | mutex_lock(&event_mutex); |
| 1857 | for_each_trace_kprobe(tk, pos) { |
| 1858 | list_for_each_entry(file, &tr->events, list) |
Masami Hiramatsu | e3dc9f8 | 2019-06-01 00:17:57 +0900 | [diff] [blame] | 1859 | if (file->event_call == trace_probe_event_call(&tk->tp)) |
Masami Hiramatsu | 970988e | 2019-05-22 17:32:35 +0900 | [diff] [blame] | 1860 | trace_event_enable_disable(file, 1, 0); |
| 1861 | } |
| 1862 | mutex_unlock(&event_mutex); |
| 1863 | } |
| 1864 | |
| 1865 | static __init void setup_boot_kprobe_events(void) |
| 1866 | { |
| 1867 | char *p, *cmd = kprobe_boot_events_buf; |
| 1868 | int ret; |
| 1869 | |
| 1870 | strreplace(kprobe_boot_events_buf, ',', ' '); |
| 1871 | |
| 1872 | while (cmd && *cmd != '\0') { |
| 1873 | p = strchr(cmd, ';'); |
| 1874 | if (p) |
| 1875 | *p++ = '\0'; |
| 1876 | |
Masami Hiramatsu | d262271 | 2021-02-01 13:48:11 -0600 | [diff] [blame] | 1877 | ret = create_or_delete_trace_kprobe(cmd); |
Masami Hiramatsu | 970988e | 2019-05-22 17:32:35 +0900 | [diff] [blame] | 1878 | if (ret) |
| 1879 | pr_warn("Failed to add event(%d): %s\n", ret, cmd); |
| 1880 | |
| 1881 | cmd = p; |
| 1882 | } |
| 1883 | |
| 1884 | enable_boot_kprobe_events(); |
| 1885 | } |
| 1886 | |
Masami Hiramatsu | d8d4c6d | 2020-01-11 01:05:42 +0900 | [diff] [blame] | 1887 | /* |
Masami Hiramatsu | ba0fbfb | 2020-09-10 21:39:17 +0900 | [diff] [blame] | 1888 | * Register dynevent at core_initcall. This allows kernel to setup kprobe |
| 1889 | * events in postcore_initcall without tracefs. |
Masami Hiramatsu | d8d4c6d | 2020-01-11 01:05:42 +0900 | [diff] [blame] | 1890 | */ |
| 1891 | static __init int init_kprobe_trace_early(void) |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1892 | { |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 1893 | int ret; |
| 1894 | |
| 1895 | ret = dyn_event_register(&trace_kprobe_ops); |
| 1896 | if (ret) |
| 1897 | return ret; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1898 | |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 1899 | if (register_module_notifier(&trace_kprobe_module_nb)) |
Masami Hiramatsu | 6142431 | 2011-06-27 16:26:56 +0900 | [diff] [blame] | 1900 | return -EINVAL; |
| 1901 | |
Masami Hiramatsu | d8d4c6d | 2020-01-11 01:05:42 +0900 | [diff] [blame] | 1902 | return 0; |
| 1903 | } |
Masami Hiramatsu | ba0fbfb | 2020-09-10 21:39:17 +0900 | [diff] [blame] | 1904 | core_initcall(init_kprobe_trace_early); |
Masami Hiramatsu | d8d4c6d | 2020-01-11 01:05:42 +0900 | [diff] [blame] | 1905 | |
| 1906 | /* Make a tracefs interface for controlling probe points */ |
| 1907 | static __init int init_kprobe_trace(void) |
| 1908 | { |
Wei Yang | 22c36b1 | 2020-07-12 09:10:36 +0800 | [diff] [blame] | 1909 | int ret; |
Masami Hiramatsu | d8d4c6d | 2020-01-11 01:05:42 +0900 | [diff] [blame] | 1910 | struct dentry *entry; |
| 1911 | |
Wei Yang | 22c36b1 | 2020-07-12 09:10:36 +0800 | [diff] [blame] | 1912 | ret = tracing_init_dentry(); |
| 1913 | if (ret) |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1914 | return 0; |
| 1915 | |
Steven Rostedt (VMware) | 21ccc9c | 2021-08-18 11:24:51 -0400 | [diff] [blame] | 1916 | entry = tracefs_create_file("kprobe_events", TRACE_MODE_WRITE, |
| 1917 | NULL, NULL, &kprobe_events_ops); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1918 | |
Masami Hiramatsu | cd7e7bd | 2009-08-13 16:35:42 -0400 | [diff] [blame] | 1919 | /* Event list interface */ |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1920 | if (!entry) |
Joe Perches | a395d6a | 2016-03-22 14:28:09 -0700 | [diff] [blame] | 1921 | pr_warn("Could not create tracefs 'kprobe_events' entry\n"); |
Masami Hiramatsu | cd7e7bd | 2009-08-13 16:35:42 -0400 | [diff] [blame] | 1922 | |
| 1923 | /* Profile interface */ |
Steven Rostedt (VMware) | 21ccc9c | 2021-08-18 11:24:51 -0400 | [diff] [blame] | 1924 | entry = tracefs_create_file("kprobe_profile", TRACE_MODE_READ, |
| 1925 | NULL, NULL, &kprobe_profile_ops); |
Masami Hiramatsu | cd7e7bd | 2009-08-13 16:35:42 -0400 | [diff] [blame] | 1926 | |
| 1927 | if (!entry) |
Joe Perches | a395d6a | 2016-03-22 14:28:09 -0700 | [diff] [blame] | 1928 | pr_warn("Could not create tracefs 'kprobe_profile' entry\n"); |
Masami Hiramatsu | 970988e | 2019-05-22 17:32:35 +0900 | [diff] [blame] | 1929 | |
| 1930 | setup_boot_kprobe_events(); |
| 1931 | |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1932 | return 0; |
| 1933 | } |
| 1934 | fs_initcall(init_kprobe_trace); |
| 1935 | |
| 1936 | |
| 1937 | #ifdef CONFIG_FTRACE_STARTUP_TEST |
Arnd Bergmann | 26a346f | 2017-02-01 17:57:56 +0100 | [diff] [blame] | 1938 | static __init struct trace_event_file * |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 1939 | find_trace_probe_file(struct trace_kprobe *tk, struct trace_array *tr) |
Masami Hiramatsu | 41a7dd4 | 2013-05-09 14:44:49 +0900 | [diff] [blame] | 1940 | { |
Steven Rostedt (Red Hat) | 7f1d2f8 | 2015-05-05 10:09:53 -0400 | [diff] [blame] | 1941 | struct trace_event_file *file; |
Masami Hiramatsu | 41a7dd4 | 2013-05-09 14:44:49 +0900 | [diff] [blame] | 1942 | |
| 1943 | list_for_each_entry(file, &tr->events, list) |
Masami Hiramatsu | e3dc9f8 | 2019-06-01 00:17:57 +0900 | [diff] [blame] | 1944 | if (file->event_call == trace_probe_event_call(&tk->tp)) |
Masami Hiramatsu | 41a7dd4 | 2013-05-09 14:44:49 +0900 | [diff] [blame] | 1945 | return file; |
| 1946 | |
| 1947 | return NULL; |
| 1948 | } |
| 1949 | |
Oleg Nesterov | 3fe3d61 | 2013-06-20 19:38:09 +0200 | [diff] [blame] | 1950 | /* |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 1951 | * 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] | 1952 | * stage, we can do this lockless. |
| 1953 | */ |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1954 | static __init int kprobe_trace_self_tests_init(void) |
| 1955 | { |
Masami Hiramatsu | 231e36f | 2010-01-14 00:12:12 -0500 | [diff] [blame] | 1956 | int ret, warn = 0; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1957 | int (*target)(int, int, int, int, int, int); |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 1958 | struct trace_kprobe *tk; |
Steven Rostedt (Red Hat) | 7f1d2f8 | 2015-05-05 10:09:53 -0400 | [diff] [blame] | 1959 | struct trace_event_file *file; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1960 | |
Yoshihiro YUNOMAE | 748ec3a | 2014-06-06 07:35:20 +0900 | [diff] [blame] | 1961 | if (tracing_is_disabled()) |
| 1962 | return -ENODEV; |
| 1963 | |
Masami Hiramatsu | 60efe21 | 2020-12-08 17:54:09 +0900 | [diff] [blame] | 1964 | if (tracing_selftest_disabled) |
Steven Rostedt (VMware) | b6399cc | 2019-05-23 19:50:34 -0400 | [diff] [blame] | 1965 | return 0; |
Steven Rostedt (VMware) | b6399cc | 2019-05-23 19:50:34 -0400 | [diff] [blame] | 1966 | |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1967 | target = kprobe_trace_selftest_target; |
| 1968 | |
| 1969 | pr_info("Testing kprobe tracing: "); |
| 1970 | |
Masami Hiramatsu | d262271 | 2021-02-01 13:48:11 -0600 | [diff] [blame] | 1971 | ret = create_or_delete_trace_kprobe("p:testprobe kprobe_trace_selftest_target $stack $stack0 +0($stack)"); |
Masami Hiramatsu | 231e36f | 2010-01-14 00:12:12 -0500 | [diff] [blame] | 1972 | if (WARN_ON_ONCE(ret)) { |
Masami Hiramatsu | 41a7dd4 | 2013-05-09 14:44:49 +0900 | [diff] [blame] | 1973 | pr_warn("error on probing function entry.\n"); |
Masami Hiramatsu | 231e36f | 2010-01-14 00:12:12 -0500 | [diff] [blame] | 1974 | warn++; |
| 1975 | } else { |
| 1976 | /* Enable trace point */ |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 1977 | tk = find_trace_kprobe("testprobe", KPROBE_EVENT_SYSTEM); |
| 1978 | if (WARN_ON_ONCE(tk == NULL)) { |
Masami Hiramatsu | 41a7dd4 | 2013-05-09 14:44:49 +0900 | [diff] [blame] | 1979 | pr_warn("error on getting new probe.\n"); |
Masami Hiramatsu | 231e36f | 2010-01-14 00:12:12 -0500 | [diff] [blame] | 1980 | warn++; |
Masami Hiramatsu | 41a7dd4 | 2013-05-09 14:44:49 +0900 | [diff] [blame] | 1981 | } else { |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 1982 | file = find_trace_probe_file(tk, top_trace_array()); |
Masami Hiramatsu | 41a7dd4 | 2013-05-09 14:44:49 +0900 | [diff] [blame] | 1983 | if (WARN_ON_ONCE(file == NULL)) { |
| 1984 | pr_warn("error on getting probe file.\n"); |
| 1985 | warn++; |
| 1986 | } else |
Masami Hiramatsu | 60d53e2 | 2019-06-20 00:07:20 +0900 | [diff] [blame] | 1987 | enable_trace_kprobe( |
| 1988 | trace_probe_event_call(&tk->tp), file); |
Masami Hiramatsu | 41a7dd4 | 2013-05-09 14:44:49 +0900 | [diff] [blame] | 1989 | } |
Masami Hiramatsu | 231e36f | 2010-01-14 00:12:12 -0500 | [diff] [blame] | 1990 | } |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1991 | |
Masami Hiramatsu | d262271 | 2021-02-01 13:48:11 -0600 | [diff] [blame] | 1992 | ret = create_or_delete_trace_kprobe("r:testprobe2 kprobe_trace_selftest_target $retval"); |
Masami Hiramatsu | 231e36f | 2010-01-14 00:12:12 -0500 | [diff] [blame] | 1993 | if (WARN_ON_ONCE(ret)) { |
Masami Hiramatsu | 41a7dd4 | 2013-05-09 14:44:49 +0900 | [diff] [blame] | 1994 | pr_warn("error on probing function return.\n"); |
Masami Hiramatsu | 231e36f | 2010-01-14 00:12:12 -0500 | [diff] [blame] | 1995 | warn++; |
| 1996 | } else { |
| 1997 | /* Enable trace point */ |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 1998 | tk = find_trace_kprobe("testprobe2", KPROBE_EVENT_SYSTEM); |
| 1999 | if (WARN_ON_ONCE(tk == NULL)) { |
Masami Hiramatsu | 41a7dd4 | 2013-05-09 14:44:49 +0900 | [diff] [blame] | 2000 | pr_warn("error on getting 2nd new probe.\n"); |
Masami Hiramatsu | 231e36f | 2010-01-14 00:12:12 -0500 | [diff] [blame] | 2001 | warn++; |
Masami Hiramatsu | 41a7dd4 | 2013-05-09 14:44:49 +0900 | [diff] [blame] | 2002 | } else { |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 2003 | file = find_trace_probe_file(tk, top_trace_array()); |
Masami Hiramatsu | 41a7dd4 | 2013-05-09 14:44:49 +0900 | [diff] [blame] | 2004 | if (WARN_ON_ONCE(file == NULL)) { |
| 2005 | pr_warn("error on getting probe file.\n"); |
| 2006 | warn++; |
| 2007 | } else |
Masami Hiramatsu | 60d53e2 | 2019-06-20 00:07:20 +0900 | [diff] [blame] | 2008 | enable_trace_kprobe( |
| 2009 | trace_probe_event_call(&tk->tp), file); |
Masami Hiramatsu | 41a7dd4 | 2013-05-09 14:44:49 +0900 | [diff] [blame] | 2010 | } |
Masami Hiramatsu | 231e36f | 2010-01-14 00:12:12 -0500 | [diff] [blame] | 2011 | } |
| 2012 | |
| 2013 | if (warn) |
| 2014 | goto end; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 2015 | |
| 2016 | ret = target(1, 2, 3, 4, 5, 6); |
| 2017 | |
Marcin Nowakowski | d4d7ccc | 2016-12-09 15:19:38 +0100 | [diff] [blame] | 2018 | /* |
| 2019 | * Not expecting an error here, the check is only to prevent the |
| 2020 | * optimizer from removing the call to target() as otherwise there |
| 2021 | * are no side-effects and the call is never performed. |
| 2022 | */ |
| 2023 | if (ret != 21) |
| 2024 | warn++; |
| 2025 | |
Masami Hiramatsu | 02ca152 | 2011-10-04 19:44:38 +0900 | [diff] [blame] | 2026 | /* Disable trace points before removing it */ |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 2027 | tk = find_trace_kprobe("testprobe", KPROBE_EVENT_SYSTEM); |
| 2028 | if (WARN_ON_ONCE(tk == NULL)) { |
Masami Hiramatsu | 41a7dd4 | 2013-05-09 14:44:49 +0900 | [diff] [blame] | 2029 | pr_warn("error on getting test probe.\n"); |
Masami Hiramatsu | 02ca152 | 2011-10-04 19:44:38 +0900 | [diff] [blame] | 2030 | warn++; |
Masami Hiramatsu | 41a7dd4 | 2013-05-09 14:44:49 +0900 | [diff] [blame] | 2031 | } else { |
Marcin Nowakowski | d4d7ccc | 2016-12-09 15:19:38 +0100 | [diff] [blame] | 2032 | if (trace_kprobe_nhit(tk) != 1) { |
| 2033 | pr_warn("incorrect number of testprobe hits\n"); |
| 2034 | warn++; |
| 2035 | } |
| 2036 | |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 2037 | file = find_trace_probe_file(tk, top_trace_array()); |
Masami Hiramatsu | 41a7dd4 | 2013-05-09 14:44:49 +0900 | [diff] [blame] | 2038 | if (WARN_ON_ONCE(file == NULL)) { |
| 2039 | pr_warn("error on getting probe file.\n"); |
| 2040 | warn++; |
| 2041 | } else |
Masami Hiramatsu | 60d53e2 | 2019-06-20 00:07:20 +0900 | [diff] [blame] | 2042 | disable_trace_kprobe( |
| 2043 | trace_probe_event_call(&tk->tp), file); |
Masami Hiramatsu | 41a7dd4 | 2013-05-09 14:44:49 +0900 | [diff] [blame] | 2044 | } |
Masami Hiramatsu | 02ca152 | 2011-10-04 19:44:38 +0900 | [diff] [blame] | 2045 | |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 2046 | tk = find_trace_kprobe("testprobe2", KPROBE_EVENT_SYSTEM); |
| 2047 | if (WARN_ON_ONCE(tk == NULL)) { |
Masami Hiramatsu | 41a7dd4 | 2013-05-09 14:44:49 +0900 | [diff] [blame] | 2048 | pr_warn("error on getting 2nd test probe.\n"); |
Masami Hiramatsu | 02ca152 | 2011-10-04 19:44:38 +0900 | [diff] [blame] | 2049 | warn++; |
Masami Hiramatsu | 41a7dd4 | 2013-05-09 14:44:49 +0900 | [diff] [blame] | 2050 | } else { |
Marcin Nowakowski | d4d7ccc | 2016-12-09 15:19:38 +0100 | [diff] [blame] | 2051 | if (trace_kprobe_nhit(tk) != 1) { |
| 2052 | pr_warn("incorrect number of testprobe2 hits\n"); |
| 2053 | warn++; |
| 2054 | } |
| 2055 | |
Namhyung Kim | c31ffb3 | 2013-07-03 13:50:51 +0900 | [diff] [blame] | 2056 | file = find_trace_probe_file(tk, top_trace_array()); |
Masami Hiramatsu | 41a7dd4 | 2013-05-09 14:44:49 +0900 | [diff] [blame] | 2057 | if (WARN_ON_ONCE(file == NULL)) { |
| 2058 | pr_warn("error on getting probe file.\n"); |
| 2059 | warn++; |
| 2060 | } else |
Masami Hiramatsu | 60d53e2 | 2019-06-20 00:07:20 +0900 | [diff] [blame] | 2061 | disable_trace_kprobe( |
| 2062 | trace_probe_event_call(&tk->tp), file); |
Masami Hiramatsu | 41a7dd4 | 2013-05-09 14:44:49 +0900 | [diff] [blame] | 2063 | } |
Masami Hiramatsu | 02ca152 | 2011-10-04 19:44:38 +0900 | [diff] [blame] | 2064 | |
Masami Hiramatsu | d262271 | 2021-02-01 13:48:11 -0600 | [diff] [blame] | 2065 | ret = create_or_delete_trace_kprobe("-:testprobe"); |
Masami Hiramatsu | 231e36f | 2010-01-14 00:12:12 -0500 | [diff] [blame] | 2066 | if (WARN_ON_ONCE(ret)) { |
Masami Hiramatsu | 41a7dd4 | 2013-05-09 14:44:49 +0900 | [diff] [blame] | 2067 | pr_warn("error on deleting a probe.\n"); |
Masami Hiramatsu | 231e36f | 2010-01-14 00:12:12 -0500 | [diff] [blame] | 2068 | warn++; |
| 2069 | } |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 2070 | |
Masami Hiramatsu | d262271 | 2021-02-01 13:48:11 -0600 | [diff] [blame] | 2071 | ret = create_or_delete_trace_kprobe("-:testprobe2"); |
Masami Hiramatsu | 231e36f | 2010-01-14 00:12:12 -0500 | [diff] [blame] | 2072 | if (WARN_ON_ONCE(ret)) { |
Masami Hiramatsu | 41a7dd4 | 2013-05-09 14:44:49 +0900 | [diff] [blame] | 2073 | pr_warn("error on deleting a probe.\n"); |
Masami Hiramatsu | 231e36f | 2010-01-14 00:12:12 -0500 | [diff] [blame] | 2074 | warn++; |
| 2075 | } |
| 2076 | |
| 2077 | end: |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 2078 | ret = dyn_events_release_all(&trace_kprobe_ops); |
| 2079 | if (WARN_ON_ONCE(ret)) { |
| 2080 | pr_warn("error on cleaning up probes.\n"); |
| 2081 | warn++; |
| 2082 | } |
Thomas Gleixner | 30e7d894 | 2017-05-17 10:19:49 +0200 | [diff] [blame] | 2083 | /* |
| 2084 | * Wait for the optimizer work to finish. Otherwise it might fiddle |
| 2085 | * with probes in already freed __init text. |
| 2086 | */ |
| 2087 | wait_for_kprobe_optimizer(); |
Masami Hiramatsu | 231e36f | 2010-01-14 00:12:12 -0500 | [diff] [blame] | 2088 | if (warn) |
| 2089 | pr_cont("NG: Some tests are failed. Please check them.\n"); |
| 2090 | else |
| 2091 | pr_cont("OK\n"); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 2092 | return 0; |
| 2093 | } |
| 2094 | |
| 2095 | late_initcall(kprobe_trace_self_tests_init); |
| 2096 | |
| 2097 | #endif |