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