Commit | Line | Data |
---|---|---|
413d37d1 | 1 | /* |
77b44d1b | 2 | * Kprobes-based tracing events |
413d37d1 MH |
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 | */ | |
19 | ||
20 | #include <linux/module.h> | |
21 | #include <linux/uaccess.h> | |
22 | #include <linux/kprobes.h> | |
23 | #include <linux/seq_file.h> | |
24 | #include <linux/slab.h> | |
25 | #include <linux/smp.h> | |
26 | #include <linux/debugfs.h> | |
27 | #include <linux/types.h> | |
28 | #include <linux/string.h> | |
29 | #include <linux/ctype.h> | |
30 | #include <linux/ptrace.h> | |
d7a4b414 | 31 | #include <linux/perf_event.h> |
413d37d1 MH |
32 | |
33 | #include "trace.h" | |
34 | #include "trace_output.h" | |
35 | ||
a82378d8 | 36 | #define MAX_TRACE_ARGS 128 |
413d37d1 | 37 | #define MAX_ARGSTR_LEN 63 |
4263565d | 38 | #define MAX_EVENT_NAME_LEN 64 |
f52487e9 | 39 | #define KPROBE_EVENT_SYSTEM "kprobes" |
413d37d1 | 40 | |
a703d946 | 41 | /* Reserved field names */ |
e93f4d85 MH |
42 | #define FIELD_STRING_IP "__probe_ip" |
43 | #define FIELD_STRING_NARGS "__probe_nargs" | |
44 | #define FIELD_STRING_RETIP "__probe_ret_ip" | |
45 | #define FIELD_STRING_FUNC "__probe_func" | |
a703d946 MH |
46 | |
47 | const char *reserved_field_names[] = { | |
48 | "common_type", | |
49 | "common_flags", | |
50 | "common_preempt_count", | |
51 | "common_pid", | |
52 | "common_tgid", | |
53 | "common_lock_depth", | |
54 | FIELD_STRING_IP, | |
55 | FIELD_STRING_NARGS, | |
56 | FIELD_STRING_RETIP, | |
57 | FIELD_STRING_FUNC, | |
58 | }; | |
59 | ||
413d37d1 MH |
60 | struct fetch_func { |
61 | unsigned long (*func)(struct pt_regs *, void *); | |
62 | void *data; | |
63 | }; | |
64 | ||
65 | static __kprobes unsigned long call_fetch(struct fetch_func *f, | |
66 | struct pt_regs *regs) | |
67 | { | |
68 | return f->func(regs, f->data); | |
69 | } | |
70 | ||
71 | /* fetch handlers */ | |
72 | static __kprobes unsigned long fetch_register(struct pt_regs *regs, | |
73 | void *offset) | |
74 | { | |
75 | return regs_get_register(regs, (unsigned int)((unsigned long)offset)); | |
76 | } | |
77 | ||
78 | static __kprobes unsigned long fetch_stack(struct pt_regs *regs, | |
79 | void *num) | |
80 | { | |
81 | return regs_get_kernel_stack_nth(regs, | |
82 | (unsigned int)((unsigned long)num)); | |
83 | } | |
84 | ||
85 | static __kprobes unsigned long fetch_memory(struct pt_regs *regs, void *addr) | |
86 | { | |
87 | unsigned long retval; | |
88 | ||
89 | if (probe_kernel_address(addr, retval)) | |
90 | return 0; | |
91 | return retval; | |
92 | } | |
93 | ||
94 | static __kprobes unsigned long fetch_argument(struct pt_regs *regs, void *num) | |
95 | { | |
96 | return regs_get_argument_nth(regs, (unsigned int)((unsigned long)num)); | |
97 | } | |
98 | ||
99 | static __kprobes unsigned long fetch_retvalue(struct pt_regs *regs, | |
100 | void *dummy) | |
101 | { | |
102 | return regs_return_value(regs); | |
103 | } | |
104 | ||
413d37d1 MH |
105 | static __kprobes unsigned long fetch_stack_address(struct pt_regs *regs, |
106 | void *dummy) | |
107 | { | |
108 | return kernel_stack_pointer(regs); | |
109 | } | |
110 | ||
111 | /* Memory fetching by symbol */ | |
112 | struct symbol_cache { | |
113 | char *symbol; | |
114 | long offset; | |
115 | unsigned long addr; | |
116 | }; | |
117 | ||
118 | static unsigned long update_symbol_cache(struct symbol_cache *sc) | |
119 | { | |
120 | sc->addr = (unsigned long)kallsyms_lookup_name(sc->symbol); | |
121 | if (sc->addr) | |
122 | sc->addr += sc->offset; | |
123 | return sc->addr; | |
124 | } | |
125 | ||
126 | static void free_symbol_cache(struct symbol_cache *sc) | |
127 | { | |
128 | kfree(sc->symbol); | |
129 | kfree(sc); | |
130 | } | |
131 | ||
132 | static struct symbol_cache *alloc_symbol_cache(const char *sym, long offset) | |
133 | { | |
134 | struct symbol_cache *sc; | |
135 | ||
136 | if (!sym || strlen(sym) == 0) | |
137 | return NULL; | |
138 | sc = kzalloc(sizeof(struct symbol_cache), GFP_KERNEL); | |
139 | if (!sc) | |
140 | return NULL; | |
141 | ||
142 | sc->symbol = kstrdup(sym, GFP_KERNEL); | |
143 | if (!sc->symbol) { | |
144 | kfree(sc); | |
145 | return NULL; | |
146 | } | |
147 | sc->offset = offset; | |
148 | ||
149 | update_symbol_cache(sc); | |
150 | return sc; | |
151 | } | |
152 | ||
153 | static __kprobes unsigned long fetch_symbol(struct pt_regs *regs, void *data) | |
154 | { | |
155 | struct symbol_cache *sc = data; | |
156 | ||
157 | if (sc->addr) | |
158 | return fetch_memory(regs, (void *)sc->addr); | |
159 | else | |
160 | return 0; | |
161 | } | |
162 | ||
163 | /* Special indirect memory access interface */ | |
164 | struct indirect_fetch_data { | |
165 | struct fetch_func orig; | |
166 | long offset; | |
167 | }; | |
168 | ||
169 | static __kprobes unsigned long fetch_indirect(struct pt_regs *regs, void *data) | |
170 | { | |
171 | struct indirect_fetch_data *ind = data; | |
172 | unsigned long addr; | |
173 | ||
174 | addr = call_fetch(&ind->orig, regs); | |
175 | if (addr) { | |
176 | addr += ind->offset; | |
177 | return fetch_memory(regs, (void *)addr); | |
178 | } else | |
179 | return 0; | |
180 | } | |
181 | ||
182 | static __kprobes void free_indirect_fetch_data(struct indirect_fetch_data *data) | |
183 | { | |
184 | if (data->orig.func == fetch_indirect) | |
185 | free_indirect_fetch_data(data->orig.data); | |
186 | else if (data->orig.func == fetch_symbol) | |
187 | free_symbol_cache(data->orig.data); | |
188 | kfree(data); | |
189 | } | |
190 | ||
191 | /** | |
77b44d1b | 192 | * Kprobe event core functions |
413d37d1 MH |
193 | */ |
194 | ||
eca0d916 MH |
195 | struct probe_arg { |
196 | struct fetch_func fetch; | |
197 | const char *name; | |
198 | }; | |
199 | ||
50d78056 MH |
200 | /* Flags for trace_probe */ |
201 | #define TP_FLAG_TRACE 1 | |
202 | #define TP_FLAG_PROFILE 2 | |
203 | ||
413d37d1 MH |
204 | struct trace_probe { |
205 | struct list_head list; | |
4a846b44 | 206 | struct kretprobe rp; /* Use rp.kp for kprobe use */ |
cd7e7bd5 | 207 | unsigned long nhit; |
50d78056 | 208 | unsigned int flags; /* For TP_FLAG_* */ |
413d37d1 | 209 | const char *symbol; /* symbol name */ |
413d37d1 | 210 | struct ftrace_event_call call; |
ff50d991 | 211 | struct trace_event event; |
a82378d8 | 212 | unsigned int nr_args; |
eca0d916 | 213 | struct probe_arg args[]; |
413d37d1 MH |
214 | }; |
215 | ||
a82378d8 MH |
216 | #define SIZEOF_TRACE_PROBE(n) \ |
217 | (offsetof(struct trace_probe, args) + \ | |
eca0d916 | 218 | (sizeof(struct probe_arg) * (n))) |
a82378d8 | 219 | |
413d37d1 MH |
220 | static __kprobes int probe_is_return(struct trace_probe *tp) |
221 | { | |
4a846b44 | 222 | return tp->rp.handler != NULL; |
413d37d1 MH |
223 | } |
224 | ||
225 | static __kprobes const char *probe_symbol(struct trace_probe *tp) | |
226 | { | |
227 | return tp->symbol ? tp->symbol : "unknown"; | |
228 | } | |
229 | ||
30a7e073 | 230 | static int probe_arg_string(char *buf, size_t n, struct fetch_func *ff) |
413d37d1 MH |
231 | { |
232 | int ret = -EINVAL; | |
233 | ||
234 | if (ff->func == fetch_argument) | |
2e06ff63 | 235 | ret = snprintf(buf, n, "$arg%lu", (unsigned long)ff->data); |
413d37d1 MH |
236 | else if (ff->func == fetch_register) { |
237 | const char *name; | |
238 | name = regs_query_register_name((unsigned int)((long)ff->data)); | |
239 | ret = snprintf(buf, n, "%%%s", name); | |
240 | } else if (ff->func == fetch_stack) | |
2e06ff63 | 241 | ret = snprintf(buf, n, "$stack%lu", (unsigned long)ff->data); |
413d37d1 MH |
242 | else if (ff->func == fetch_memory) |
243 | ret = snprintf(buf, n, "@0x%p", ff->data); | |
244 | else if (ff->func == fetch_symbol) { | |
245 | struct symbol_cache *sc = ff->data; | |
52a11f35 LJ |
246 | if (sc->offset) |
247 | ret = snprintf(buf, n, "@%s%+ld", sc->symbol, | |
248 | sc->offset); | |
249 | else | |
250 | ret = snprintf(buf, n, "@%s", sc->symbol); | |
413d37d1 | 251 | } else if (ff->func == fetch_retvalue) |
2e06ff63 | 252 | ret = snprintf(buf, n, "$retval"); |
413d37d1 | 253 | else if (ff->func == fetch_stack_address) |
2e06ff63 | 254 | ret = snprintf(buf, n, "$stack"); |
413d37d1 MH |
255 | else if (ff->func == fetch_indirect) { |
256 | struct indirect_fetch_data *id = ff->data; | |
257 | size_t l = 0; | |
258 | ret = snprintf(buf, n, "%+ld(", id->offset); | |
259 | if (ret >= n) | |
260 | goto end; | |
261 | l += ret; | |
30a7e073 | 262 | ret = probe_arg_string(buf + l, n - l, &id->orig); |
413d37d1 MH |
263 | if (ret < 0) |
264 | goto end; | |
265 | l += ret; | |
266 | ret = snprintf(buf + l, n - l, ")"); | |
267 | ret += l; | |
268 | } | |
269 | end: | |
270 | if (ret >= n) | |
271 | return -ENOSPC; | |
272 | return ret; | |
273 | } | |
274 | ||
275 | static int register_probe_event(struct trace_probe *tp); | |
276 | static void unregister_probe_event(struct trace_probe *tp); | |
277 | ||
278 | static DEFINE_MUTEX(probe_lock); | |
279 | static LIST_HEAD(probe_list); | |
280 | ||
50d78056 MH |
281 | static int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs); |
282 | static int kretprobe_dispatcher(struct kretprobe_instance *ri, | |
283 | struct pt_regs *regs); | |
284 | ||
6f3cf440 MH |
285 | /* Check the name is good for event/group */ |
286 | static int check_event_name(const char *name) | |
287 | { | |
288 | if (!isalpha(*name) && *name != '_') | |
289 | return 0; | |
290 | while (*++name != '\0') { | |
291 | if (!isalpha(*name) && !isdigit(*name) && *name != '_') | |
292 | return 0; | |
293 | } | |
294 | return 1; | |
295 | } | |
296 | ||
4a846b44 MH |
297 | /* |
298 | * Allocate new trace_probe and initialize it (including kprobes). | |
299 | */ | |
f52487e9 MH |
300 | static struct trace_probe *alloc_trace_probe(const char *group, |
301 | const char *event, | |
4a846b44 MH |
302 | void *addr, |
303 | const char *symbol, | |
304 | unsigned long offs, | |
305 | int nargs, int is_return) | |
413d37d1 MH |
306 | { |
307 | struct trace_probe *tp; | |
6f3cf440 | 308 | int ret = -ENOMEM; |
413d37d1 | 309 | |
a82378d8 | 310 | tp = kzalloc(SIZEOF_TRACE_PROBE(nargs), GFP_KERNEL); |
413d37d1 | 311 | if (!tp) |
6f3cf440 | 312 | return ERR_PTR(ret); |
413d37d1 MH |
313 | |
314 | if (symbol) { | |
315 | tp->symbol = kstrdup(symbol, GFP_KERNEL); | |
316 | if (!tp->symbol) | |
317 | goto error; | |
4a846b44 MH |
318 | tp->rp.kp.symbol_name = tp->symbol; |
319 | tp->rp.kp.offset = offs; | |
320 | } else | |
321 | tp->rp.kp.addr = addr; | |
322 | ||
323 | if (is_return) | |
50d78056 | 324 | tp->rp.handler = kretprobe_dispatcher; |
4a846b44 | 325 | else |
50d78056 | 326 | tp->rp.kp.pre_handler = kprobe_dispatcher; |
4a846b44 | 327 | |
6f3cf440 MH |
328 | if (!event || !check_event_name(event)) { |
329 | ret = -EINVAL; | |
4263565d | 330 | goto error; |
6f3cf440 MH |
331 | } |
332 | ||
4263565d MH |
333 | tp->call.name = kstrdup(event, GFP_KERNEL); |
334 | if (!tp->call.name) | |
335 | goto error; | |
413d37d1 | 336 | |
6f3cf440 MH |
337 | if (!group || !check_event_name(group)) { |
338 | ret = -EINVAL; | |
f52487e9 | 339 | goto error; |
6f3cf440 MH |
340 | } |
341 | ||
f52487e9 MH |
342 | tp->call.system = kstrdup(group, GFP_KERNEL); |
343 | if (!tp->call.system) | |
344 | goto error; | |
345 | ||
413d37d1 MH |
346 | INIT_LIST_HEAD(&tp->list); |
347 | return tp; | |
348 | error: | |
f52487e9 | 349 | kfree(tp->call.name); |
413d37d1 MH |
350 | kfree(tp->symbol); |
351 | kfree(tp); | |
6f3cf440 | 352 | return ERR_PTR(ret); |
413d37d1 MH |
353 | } |
354 | ||
eca0d916 MH |
355 | static void free_probe_arg(struct probe_arg *arg) |
356 | { | |
357 | if (arg->fetch.func == fetch_symbol) | |
358 | free_symbol_cache(arg->fetch.data); | |
359 | else if (arg->fetch.func == fetch_indirect) | |
360 | free_indirect_fetch_data(arg->fetch.data); | |
361 | kfree(arg->name); | |
362 | } | |
363 | ||
413d37d1 MH |
364 | static void free_trace_probe(struct trace_probe *tp) |
365 | { | |
366 | int i; | |
367 | ||
368 | for (i = 0; i < tp->nr_args; i++) | |
eca0d916 | 369 | free_probe_arg(&tp->args[i]); |
413d37d1 | 370 | |
f52487e9 | 371 | kfree(tp->call.system); |
413d37d1 MH |
372 | kfree(tp->call.name); |
373 | kfree(tp->symbol); | |
374 | kfree(tp); | |
375 | } | |
376 | ||
dd004c47 MH |
377 | static struct trace_probe *find_probe_event(const char *event, |
378 | const char *group) | |
413d37d1 MH |
379 | { |
380 | struct trace_probe *tp; | |
381 | ||
382 | list_for_each_entry(tp, &probe_list, list) | |
dd004c47 MH |
383 | if (strcmp(tp->call.name, event) == 0 && |
384 | strcmp(tp->call.system, group) == 0) | |
413d37d1 MH |
385 | return tp; |
386 | return NULL; | |
387 | } | |
388 | ||
2d5e067e MH |
389 | /* Unregister a trace_probe and probe_event: call with locking probe_lock */ |
390 | static void unregister_trace_probe(struct trace_probe *tp) | |
413d37d1 MH |
391 | { |
392 | if (probe_is_return(tp)) | |
393 | unregister_kretprobe(&tp->rp); | |
394 | else | |
4a846b44 | 395 | unregister_kprobe(&tp->rp.kp); |
413d37d1 | 396 | list_del(&tp->list); |
2d5e067e | 397 | unregister_probe_event(tp); |
413d37d1 MH |
398 | } |
399 | ||
400 | /* Register a trace_probe and probe_event */ | |
401 | static int register_trace_probe(struct trace_probe *tp) | |
402 | { | |
403 | struct trace_probe *old_tp; | |
404 | int ret; | |
405 | ||
406 | mutex_lock(&probe_lock); | |
407 | ||
2d5e067e | 408 | /* register as an event */ |
dd004c47 | 409 | old_tp = find_probe_event(tp->call.name, tp->call.system); |
2d5e067e MH |
410 | if (old_tp) { |
411 | /* delete old event */ | |
412 | unregister_trace_probe(old_tp); | |
413 | free_trace_probe(old_tp); | |
414 | } | |
415 | ret = register_probe_event(tp); | |
416 | if (ret) { | |
417 | pr_warning("Faild to register probe event(%d)\n", ret); | |
418 | goto end; | |
419 | } | |
420 | ||
5a0d9050 | 421 | tp->rp.kp.flags |= KPROBE_FLAG_DISABLED; |
413d37d1 MH |
422 | if (probe_is_return(tp)) |
423 | ret = register_kretprobe(&tp->rp); | |
424 | else | |
4a846b44 | 425 | ret = register_kprobe(&tp->rp.kp); |
413d37d1 MH |
426 | |
427 | if (ret) { | |
428 | pr_warning("Could not insert probe(%d)\n", ret); | |
429 | if (ret == -EILSEQ) { | |
430 | pr_warning("Probing address(0x%p) is not an " | |
431 | "instruction boundary.\n", | |
4a846b44 | 432 | tp->rp.kp.addr); |
413d37d1 MH |
433 | ret = -EINVAL; |
434 | } | |
2d5e067e MH |
435 | unregister_probe_event(tp); |
436 | } else | |
437 | list_add_tail(&tp->list, &probe_list); | |
413d37d1 MH |
438 | end: |
439 | mutex_unlock(&probe_lock); | |
440 | return ret; | |
441 | } | |
442 | ||
443 | /* Split symbol and offset. */ | |
2fba0c88 | 444 | static int split_symbol_offset(char *symbol, unsigned long *offset) |
413d37d1 MH |
445 | { |
446 | char *tmp; | |
447 | int ret; | |
448 | ||
449 | if (!offset) | |
450 | return -EINVAL; | |
451 | ||
452 | tmp = strchr(symbol, '+'); | |
413d37d1 MH |
453 | if (tmp) { |
454 | /* skip sign because strict_strtol doesn't accept '+' */ | |
2fba0c88 | 455 | ret = strict_strtoul(tmp + 1, 0, offset); |
413d37d1 MH |
456 | if (ret) |
457 | return ret; | |
413d37d1 MH |
458 | *tmp = '\0'; |
459 | } else | |
460 | *offset = 0; | |
461 | return 0; | |
462 | } | |
463 | ||
464 | #define PARAM_MAX_ARGS 16 | |
465 | #define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long)) | |
466 | ||
405b2651 | 467 | static int parse_probe_vars(char *arg, struct fetch_func *ff, int is_return) |
413d37d1 MH |
468 | { |
469 | int ret = 0; | |
470 | unsigned long param; | |
413d37d1 | 471 | |
2e06ff63 MH |
472 | if (strcmp(arg, "retval") == 0) { |
473 | if (is_return) { | |
413d37d1 MH |
474 | ff->func = fetch_retvalue; |
475 | ff->data = NULL; | |
413d37d1 MH |
476 | } else |
477 | ret = -EINVAL; | |
2e06ff63 MH |
478 | } else if (strncmp(arg, "stack", 5) == 0) { |
479 | if (arg[5] == '\0') { | |
413d37d1 MH |
480 | ff->func = fetch_stack_address; |
481 | ff->data = NULL; | |
2e06ff63 MH |
482 | } else if (isdigit(arg[5])) { |
483 | ret = strict_strtoul(arg + 5, 10, ¶m); | |
413d37d1 MH |
484 | if (ret || param > PARAM_MAX_STACK) |
485 | ret = -EINVAL; | |
486 | else { | |
487 | ff->func = fetch_stack; | |
488 | ff->data = (void *)param; | |
489 | } | |
2e06ff63 MH |
490 | } else |
491 | ret = -EINVAL; | |
492 | } else if (strncmp(arg, "arg", 3) == 0 && isdigit(arg[3])) { | |
493 | ret = strict_strtoul(arg + 3, 10, ¶m); | |
494 | if (ret || param > PARAM_MAX_ARGS) | |
495 | ret = -EINVAL; | |
496 | else { | |
497 | ff->func = fetch_argument; | |
498 | ff->data = (void *)param; | |
413d37d1 | 499 | } |
2e06ff63 | 500 | } else |
405b2651 | 501 | ret = -EINVAL; |
405b2651 MH |
502 | return ret; |
503 | } | |
504 | ||
ba8665d7 MH |
505 | /* Recursive argument parser */ |
506 | static int __parse_probe_arg(char *arg, struct fetch_func *ff, int is_return) | |
405b2651 MH |
507 | { |
508 | int ret = 0; | |
509 | unsigned long param; | |
510 | long offset; | |
511 | char *tmp; | |
512 | ||
513 | switch (arg[0]) { | |
514 | case '$': | |
515 | ret = parse_probe_vars(arg + 1, ff, is_return); | |
516 | break; | |
517 | case '%': /* named register */ | |
518 | ret = regs_query_register_offset(arg + 1); | |
519 | if (ret >= 0) { | |
520 | ff->func = fetch_register; | |
521 | ff->data = (void *)(unsigned long)ret; | |
522 | ret = 0; | |
523 | } | |
524 | break; | |
413d37d1 MH |
525 | case '@': /* memory or symbol */ |
526 | if (isdigit(arg[1])) { | |
527 | ret = strict_strtoul(arg + 1, 0, ¶m); | |
528 | if (ret) | |
529 | break; | |
530 | ff->func = fetch_memory; | |
531 | ff->data = (void *)param; | |
532 | } else { | |
533 | ret = split_symbol_offset(arg + 1, &offset); | |
534 | if (ret) | |
535 | break; | |
405b2651 | 536 | ff->data = alloc_symbol_cache(arg + 1, offset); |
413d37d1 MH |
537 | if (ff->data) |
538 | ff->func = fetch_symbol; | |
539 | else | |
540 | ret = -EINVAL; | |
541 | } | |
542 | break; | |
543 | case '+': /* indirect memory */ | |
544 | case '-': | |
545 | tmp = strchr(arg, '('); | |
546 | if (!tmp) { | |
547 | ret = -EINVAL; | |
548 | break; | |
549 | } | |
550 | *tmp = '\0'; | |
551 | ret = strict_strtol(arg + 1, 0, &offset); | |
552 | if (ret) | |
553 | break; | |
554 | if (arg[0] == '-') | |
555 | offset = -offset; | |
556 | arg = tmp + 1; | |
557 | tmp = strrchr(arg, ')'); | |
558 | if (tmp) { | |
559 | struct indirect_fetch_data *id; | |
560 | *tmp = '\0'; | |
561 | id = kzalloc(sizeof(struct indirect_fetch_data), | |
562 | GFP_KERNEL); | |
563 | if (!id) | |
564 | return -ENOMEM; | |
565 | id->offset = offset; | |
ba8665d7 | 566 | ret = __parse_probe_arg(arg, &id->orig, is_return); |
413d37d1 MH |
567 | if (ret) |
568 | kfree(id); | |
569 | else { | |
570 | ff->func = fetch_indirect; | |
571 | ff->data = (void *)id; | |
572 | } | |
573 | } else | |
574 | ret = -EINVAL; | |
575 | break; | |
576 | default: | |
577 | /* TODO: support custom handler */ | |
578 | ret = -EINVAL; | |
579 | } | |
580 | return ret; | |
581 | } | |
582 | ||
ba8665d7 MH |
583 | /* String length checking wrapper */ |
584 | static int parse_probe_arg(char *arg, struct fetch_func *ff, int is_return) | |
585 | { | |
586 | if (strlen(arg) > MAX_ARGSTR_LEN) { | |
587 | pr_info("Argument is too long.: %s\n", arg); | |
588 | return -ENOSPC; | |
589 | } | |
590 | return __parse_probe_arg(arg, ff, is_return); | |
591 | } | |
592 | ||
a703d946 MH |
593 | /* Return 1 if name is reserved or already used by another argument */ |
594 | static int conflict_field_name(const char *name, | |
595 | struct probe_arg *args, int narg) | |
596 | { | |
597 | int i; | |
598 | for (i = 0; i < ARRAY_SIZE(reserved_field_names); i++) | |
599 | if (strcmp(reserved_field_names[i], name) == 0) | |
600 | return 1; | |
601 | for (i = 0; i < narg; i++) | |
602 | if (strcmp(args[i].name, name) == 0) | |
603 | return 1; | |
604 | return 0; | |
605 | } | |
606 | ||
413d37d1 MH |
607 | static int create_trace_probe(int argc, char **argv) |
608 | { | |
609 | /* | |
610 | * Argument syntax: | |
f52487e9 MH |
611 | * - Add kprobe: p[:[GRP/]EVENT] KSYM[+OFFS]|KADDR [FETCHARGS] |
612 | * - Add kretprobe: r[:[GRP/]EVENT] KSYM[+0] [FETCHARGS] | |
413d37d1 | 613 | * Fetch args: |
2e06ff63 MH |
614 | * $argN : fetch Nth of function argument. (N:0-) |
615 | * $retval : fetch return value | |
616 | * $stack : fetch stack address | |
617 | * $stackN : fetch Nth of stack (N:0-) | |
413d37d1 MH |
618 | * @ADDR : fetch memory at ADDR (ADDR should be in kernel) |
619 | * @SYM[+|-offs] : fetch memory at SYM +|- offs (SYM is a data symbol) | |
620 | * %REG : fetch register REG | |
621 | * Indirect memory fetch: | |
622 | * +|-offs(ARG) : fetch memory at ARG +|- offs address. | |
eca0d916 MH |
623 | * Alias name of args: |
624 | * NAME=FETCHARG : set NAME as alias of FETCHARG. | |
413d37d1 MH |
625 | */ |
626 | struct trace_probe *tp; | |
413d37d1 | 627 | int i, ret = 0; |
a7c312be | 628 | int is_return = 0, is_delete = 0; |
f52487e9 | 629 | char *symbol = NULL, *event = NULL, *arg = NULL, *group = NULL; |
2fba0c88 | 630 | unsigned long offset = 0; |
413d37d1 | 631 | void *addr = NULL; |
4a846b44 | 632 | char buf[MAX_EVENT_NAME_LEN]; |
413d37d1 | 633 | |
a7c312be | 634 | /* argc must be >= 1 */ |
413d37d1 MH |
635 | if (argv[0][0] == 'p') |
636 | is_return = 0; | |
637 | else if (argv[0][0] == 'r') | |
638 | is_return = 1; | |
a7c312be MH |
639 | else if (argv[0][0] == '-') |
640 | is_delete = 1; | |
e63cc239 | 641 | else { |
a7c312be MH |
642 | pr_info("Probe definition must be started with 'p', 'r' or" |
643 | " '-'.\n"); | |
413d37d1 | 644 | return -EINVAL; |
e63cc239 | 645 | } |
413d37d1 MH |
646 | |
647 | if (argv[0][1] == ':') { | |
648 | event = &argv[0][2]; | |
f52487e9 MH |
649 | if (strchr(event, '/')) { |
650 | group = event; | |
651 | event = strchr(group, '/') + 1; | |
652 | event[-1] = '\0'; | |
653 | if (strlen(group) == 0) { | |
a5efd925 | 654 | pr_info("Group name is not specified\n"); |
f52487e9 MH |
655 | return -EINVAL; |
656 | } | |
657 | } | |
413d37d1 | 658 | if (strlen(event) == 0) { |
a5efd925 | 659 | pr_info("Event name is not specified\n"); |
413d37d1 MH |
660 | return -EINVAL; |
661 | } | |
662 | } | |
a7c312be MH |
663 | if (!group) |
664 | group = KPROBE_EVENT_SYSTEM; | |
413d37d1 | 665 | |
a7c312be MH |
666 | if (is_delete) { |
667 | if (!event) { | |
668 | pr_info("Delete command needs an event name.\n"); | |
669 | return -EINVAL; | |
670 | } | |
671 | tp = find_probe_event(event, group); | |
672 | if (!tp) { | |
673 | pr_info("Event %s/%s doesn't exist.\n", group, event); | |
674 | return -ENOENT; | |
675 | } | |
676 | /* delete an event */ | |
677 | unregister_trace_probe(tp); | |
678 | free_trace_probe(tp); | |
679 | return 0; | |
680 | } | |
681 | ||
682 | if (argc < 2) { | |
683 | pr_info("Probe point is not specified.\n"); | |
684 | return -EINVAL; | |
685 | } | |
413d37d1 | 686 | if (isdigit(argv[1][0])) { |
e63cc239 MH |
687 | if (is_return) { |
688 | pr_info("Return probe point must be a symbol.\n"); | |
413d37d1 | 689 | return -EINVAL; |
e63cc239 | 690 | } |
413d37d1 | 691 | /* an address specified */ |
a9bb18f3 | 692 | ret = strict_strtoul(&argv[1][0], 0, (unsigned long *)&addr); |
e63cc239 MH |
693 | if (ret) { |
694 | pr_info("Failed to parse address.\n"); | |
413d37d1 | 695 | return ret; |
e63cc239 | 696 | } |
413d37d1 MH |
697 | } else { |
698 | /* a symbol specified */ | |
699 | symbol = argv[1]; | |
700 | /* TODO: support .init module functions */ | |
701 | ret = split_symbol_offset(symbol, &offset); | |
e63cc239 MH |
702 | if (ret) { |
703 | pr_info("Failed to parse symbol.\n"); | |
413d37d1 | 704 | return ret; |
e63cc239 MH |
705 | } |
706 | if (offset && is_return) { | |
707 | pr_info("Return probe must be used without offset.\n"); | |
413d37d1 | 708 | return -EINVAL; |
e63cc239 | 709 | } |
413d37d1 | 710 | } |
a82378d8 | 711 | argc -= 2; argv += 2; |
413d37d1 MH |
712 | |
713 | /* setup a probe */ | |
4263565d MH |
714 | if (!event) { |
715 | /* Make a new event name */ | |
4263565d | 716 | if (symbol) |
6f3cf440 | 717 | snprintf(buf, MAX_EVENT_NAME_LEN, "%c_%s_%ld", |
4263565d MH |
718 | is_return ? 'r' : 'p', symbol, offset); |
719 | else | |
6f3cf440 | 720 | snprintf(buf, MAX_EVENT_NAME_LEN, "%c_0x%p", |
4263565d | 721 | is_return ? 'r' : 'p', addr); |
4a846b44 MH |
722 | event = buf; |
723 | } | |
f52487e9 MH |
724 | tp = alloc_trace_probe(group, event, addr, symbol, offset, argc, |
725 | is_return); | |
e63cc239 MH |
726 | if (IS_ERR(tp)) { |
727 | pr_info("Failed to allocate trace_probe.(%d)\n", | |
728 | (int)PTR_ERR(tp)); | |
413d37d1 | 729 | return PTR_ERR(tp); |
e63cc239 | 730 | } |
413d37d1 | 731 | |
413d37d1 | 732 | /* parse arguments */ |
a82378d8 MH |
733 | ret = 0; |
734 | for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) { | |
eca0d916 MH |
735 | /* Parse argument name */ |
736 | arg = strchr(argv[i], '='); | |
737 | if (arg) | |
738 | *arg++ = '\0'; | |
739 | else | |
740 | arg = argv[i]; | |
a703d946 MH |
741 | |
742 | if (conflict_field_name(argv[i], tp->args, i)) { | |
e63cc239 MH |
743 | pr_info("Argument%d name '%s' conflicts with " |
744 | "another field.\n", i, argv[i]); | |
a703d946 MH |
745 | ret = -EINVAL; |
746 | goto error; | |
747 | } | |
748 | ||
eca0d916 | 749 | tp->args[i].name = kstrdup(argv[i], GFP_KERNEL); |
ba8665d7 MH |
750 | if (!tp->args[i].name) { |
751 | pr_info("Failed to allocate argument%d name '%s'.\n", | |
752 | i, argv[i]); | |
753 | ret = -ENOMEM; | |
413d37d1 MH |
754 | goto error; |
755 | } | |
ba8665d7 MH |
756 | |
757 | /* Parse fetch argument */ | |
eca0d916 | 758 | ret = parse_probe_arg(arg, &tp->args[i].fetch, is_return); |
e63cc239 MH |
759 | if (ret) { |
760 | pr_info("Parse error at argument%d. (%d)\n", i, ret); | |
abab9d37 | 761 | kfree(tp->args[i].name); |
413d37d1 | 762 | goto error; |
e63cc239 | 763 | } |
abab9d37 LJ |
764 | |
765 | tp->nr_args++; | |
413d37d1 | 766 | } |
413d37d1 MH |
767 | |
768 | ret = register_trace_probe(tp); | |
769 | if (ret) | |
770 | goto error; | |
771 | return 0; | |
772 | ||
773 | error: | |
774 | free_trace_probe(tp); | |
775 | return ret; | |
776 | } | |
777 | ||
778 | static void cleanup_all_probes(void) | |
779 | { | |
780 | struct trace_probe *tp; | |
781 | ||
782 | mutex_lock(&probe_lock); | |
783 | /* TODO: Use batch unregistration */ | |
784 | while (!list_empty(&probe_list)) { | |
785 | tp = list_entry(probe_list.next, struct trace_probe, list); | |
786 | unregister_trace_probe(tp); | |
787 | free_trace_probe(tp); | |
788 | } | |
789 | mutex_unlock(&probe_lock); | |
790 | } | |
791 | ||
792 | ||
793 | /* Probes listing interfaces */ | |
794 | static void *probes_seq_start(struct seq_file *m, loff_t *pos) | |
795 | { | |
796 | mutex_lock(&probe_lock); | |
797 | return seq_list_start(&probe_list, *pos); | |
798 | } | |
799 | ||
800 | static void *probes_seq_next(struct seq_file *m, void *v, loff_t *pos) | |
801 | { | |
802 | return seq_list_next(v, &probe_list, pos); | |
803 | } | |
804 | ||
805 | static void probes_seq_stop(struct seq_file *m, void *v) | |
806 | { | |
807 | mutex_unlock(&probe_lock); | |
808 | } | |
809 | ||
810 | static int probes_seq_show(struct seq_file *m, void *v) | |
811 | { | |
812 | struct trace_probe *tp = v; | |
813 | int i, ret; | |
814 | char buf[MAX_ARGSTR_LEN + 1]; | |
815 | ||
816 | seq_printf(m, "%c", probe_is_return(tp) ? 'r' : 'p'); | |
3d9b2e1d | 817 | seq_printf(m, ":%s/%s", tp->call.system, tp->call.name); |
413d37d1 | 818 | |
52a11f35 LJ |
819 | if (!tp->symbol) |
820 | seq_printf(m, " 0x%p", tp->rp.kp.addr); | |
821 | else if (tp->rp.kp.offset) | |
4a846b44 | 822 | seq_printf(m, " %s+%u", probe_symbol(tp), tp->rp.kp.offset); |
413d37d1 | 823 | else |
52a11f35 | 824 | seq_printf(m, " %s", probe_symbol(tp)); |
413d37d1 MH |
825 | |
826 | for (i = 0; i < tp->nr_args; i++) { | |
eca0d916 | 827 | ret = probe_arg_string(buf, MAX_ARGSTR_LEN, &tp->args[i].fetch); |
413d37d1 MH |
828 | if (ret < 0) { |
829 | pr_warning("Argument%d decoding error(%d).\n", i, ret); | |
830 | return ret; | |
831 | } | |
eca0d916 | 832 | seq_printf(m, " %s=%s", tp->args[i].name, buf); |
413d37d1 MH |
833 | } |
834 | seq_printf(m, "\n"); | |
835 | return 0; | |
836 | } | |
837 | ||
838 | static const struct seq_operations probes_seq_op = { | |
839 | .start = probes_seq_start, | |
840 | .next = probes_seq_next, | |
841 | .stop = probes_seq_stop, | |
842 | .show = probes_seq_show | |
843 | }; | |
844 | ||
845 | static int probes_open(struct inode *inode, struct file *file) | |
846 | { | |
847 | if ((file->f_mode & FMODE_WRITE) && | |
848 | (file->f_flags & O_TRUNC)) | |
849 | cleanup_all_probes(); | |
850 | ||
851 | return seq_open(file, &probes_seq_op); | |
852 | } | |
853 | ||
854 | static int command_trace_probe(const char *buf) | |
855 | { | |
856 | char **argv; | |
857 | int argc = 0, ret = 0; | |
858 | ||
859 | argv = argv_split(GFP_KERNEL, buf, &argc); | |
860 | if (!argv) | |
861 | return -ENOMEM; | |
862 | ||
863 | if (argc) | |
864 | ret = create_trace_probe(argc, argv); | |
865 | ||
866 | argv_free(argv); | |
867 | return ret; | |
868 | } | |
869 | ||
870 | #define WRITE_BUFSIZE 128 | |
871 | ||
872 | static ssize_t probes_write(struct file *file, const char __user *buffer, | |
873 | size_t count, loff_t *ppos) | |
874 | { | |
875 | char *kbuf, *tmp; | |
876 | int ret; | |
877 | size_t done; | |
878 | size_t size; | |
879 | ||
880 | kbuf = kmalloc(WRITE_BUFSIZE, GFP_KERNEL); | |
881 | if (!kbuf) | |
882 | return -ENOMEM; | |
883 | ||
884 | ret = done = 0; | |
885 | while (done < count) { | |
886 | size = count - done; | |
887 | if (size >= WRITE_BUFSIZE) | |
888 | size = WRITE_BUFSIZE - 1; | |
889 | if (copy_from_user(kbuf, buffer + done, size)) { | |
890 | ret = -EFAULT; | |
891 | goto out; | |
892 | } | |
893 | kbuf[size] = '\0'; | |
894 | tmp = strchr(kbuf, '\n'); | |
895 | if (tmp) { | |
896 | *tmp = '\0'; | |
897 | size = tmp - kbuf + 1; | |
898 | } else if (done + size < count) { | |
899 | pr_warning("Line length is too long: " | |
900 | "Should be less than %d.", WRITE_BUFSIZE); | |
901 | ret = -EINVAL; | |
902 | goto out; | |
903 | } | |
904 | done += size; | |
905 | /* Remove comments */ | |
906 | tmp = strchr(kbuf, '#'); | |
907 | if (tmp) | |
908 | *tmp = '\0'; | |
909 | ||
910 | ret = command_trace_probe(kbuf); | |
911 | if (ret) | |
912 | goto out; | |
913 | } | |
914 | ret = done; | |
915 | out: | |
916 | kfree(kbuf); | |
917 | return ret; | |
918 | } | |
919 | ||
920 | static const struct file_operations kprobe_events_ops = { | |
921 | .owner = THIS_MODULE, | |
922 | .open = probes_open, | |
923 | .read = seq_read, | |
924 | .llseek = seq_lseek, | |
925 | .release = seq_release, | |
926 | .write = probes_write, | |
927 | }; | |
928 | ||
cd7e7bd5 MH |
929 | /* Probes profiling interfaces */ |
930 | static int probes_profile_seq_show(struct seq_file *m, void *v) | |
931 | { | |
932 | struct trace_probe *tp = v; | |
933 | ||
934 | seq_printf(m, " %-44s %15lu %15lu\n", tp->call.name, tp->nhit, | |
4a846b44 | 935 | tp->rp.kp.nmissed); |
cd7e7bd5 MH |
936 | |
937 | return 0; | |
938 | } | |
939 | ||
940 | static const struct seq_operations profile_seq_op = { | |
941 | .start = probes_seq_start, | |
942 | .next = probes_seq_next, | |
943 | .stop = probes_seq_stop, | |
944 | .show = probes_profile_seq_show | |
945 | }; | |
946 | ||
947 | static int profile_open(struct inode *inode, struct file *file) | |
948 | { | |
949 | return seq_open(file, &profile_seq_op); | |
950 | } | |
951 | ||
952 | static const struct file_operations kprobe_profile_ops = { | |
953 | .owner = THIS_MODULE, | |
954 | .open = profile_open, | |
955 | .read = seq_read, | |
956 | .llseek = seq_lseek, | |
957 | .release = seq_release, | |
958 | }; | |
959 | ||
413d37d1 MH |
960 | /* Kprobe handler */ |
961 | static __kprobes int kprobe_trace_func(struct kprobe *kp, struct pt_regs *regs) | |
962 | { | |
4a846b44 | 963 | struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp); |
413d37d1 MH |
964 | struct kprobe_trace_entry *entry; |
965 | struct ring_buffer_event *event; | |
8f8ffe24 | 966 | struct ring_buffer *buffer; |
413d37d1 MH |
967 | int size, i, pc; |
968 | unsigned long irq_flags; | |
4263565d | 969 | struct ftrace_event_call *call = &tp->call; |
413d37d1 | 970 | |
cd7e7bd5 MH |
971 | tp->nhit++; |
972 | ||
413d37d1 MH |
973 | local_save_flags(irq_flags); |
974 | pc = preempt_count(); | |
975 | ||
976 | size = SIZEOF_KPROBE_TRACE_ENTRY(tp->nr_args); | |
977 | ||
8f8ffe24 | 978 | event = trace_current_buffer_lock_reserve(&buffer, call->id, size, |
413d37d1 MH |
979 | irq_flags, pc); |
980 | if (!event) | |
981 | return 0; | |
982 | ||
983 | entry = ring_buffer_event_data(event); | |
984 | entry->nargs = tp->nr_args; | |
985 | entry->ip = (unsigned long)kp->addr; | |
986 | for (i = 0; i < tp->nr_args; i++) | |
eca0d916 | 987 | entry->args[i] = call_fetch(&tp->args[i].fetch, regs); |
413d37d1 | 988 | |
8f8ffe24 FW |
989 | if (!filter_current_check_discard(buffer, call, entry, event)) |
990 | trace_nowake_buffer_unlock_commit(buffer, event, irq_flags, pc); | |
413d37d1 MH |
991 | return 0; |
992 | } | |
993 | ||
994 | /* Kretprobe handler */ | |
995 | static __kprobes int kretprobe_trace_func(struct kretprobe_instance *ri, | |
996 | struct pt_regs *regs) | |
997 | { | |
998 | struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp); | |
999 | struct kretprobe_trace_entry *entry; | |
1000 | struct ring_buffer_event *event; | |
8f8ffe24 | 1001 | struct ring_buffer *buffer; |
413d37d1 MH |
1002 | int size, i, pc; |
1003 | unsigned long irq_flags; | |
4263565d | 1004 | struct ftrace_event_call *call = &tp->call; |
413d37d1 MH |
1005 | |
1006 | local_save_flags(irq_flags); | |
1007 | pc = preempt_count(); | |
1008 | ||
1009 | size = SIZEOF_KRETPROBE_TRACE_ENTRY(tp->nr_args); | |
1010 | ||
8f8ffe24 | 1011 | event = trace_current_buffer_lock_reserve(&buffer, call->id, size, |
413d37d1 MH |
1012 | irq_flags, pc); |
1013 | if (!event) | |
1014 | return 0; | |
1015 | ||
1016 | entry = ring_buffer_event_data(event); | |
1017 | entry->nargs = tp->nr_args; | |
4a846b44 | 1018 | entry->func = (unsigned long)tp->rp.kp.addr; |
413d37d1 MH |
1019 | entry->ret_ip = (unsigned long)ri->ret_addr; |
1020 | for (i = 0; i < tp->nr_args; i++) | |
eca0d916 | 1021 | entry->args[i] = call_fetch(&tp->args[i].fetch, regs); |
413d37d1 | 1022 | |
8f8ffe24 FW |
1023 | if (!filter_current_check_discard(buffer, call, entry, event)) |
1024 | trace_nowake_buffer_unlock_commit(buffer, event, irq_flags, pc); | |
413d37d1 MH |
1025 | |
1026 | return 0; | |
1027 | } | |
1028 | ||
1029 | /* Event entry printers */ | |
1030 | enum print_line_t | |
1031 | print_kprobe_event(struct trace_iterator *iter, int flags) | |
1032 | { | |
1033 | struct kprobe_trace_entry *field; | |
1034 | struct trace_seq *s = &iter->seq; | |
eca0d916 MH |
1035 | struct trace_event *event; |
1036 | struct trace_probe *tp; | |
413d37d1 MH |
1037 | int i; |
1038 | ||
ff50d991 | 1039 | field = (struct kprobe_trace_entry *)iter->ent; |
eca0d916 MH |
1040 | event = ftrace_find_event(field->ent.type); |
1041 | tp = container_of(event, struct trace_probe, event); | |
413d37d1 | 1042 | |
6e9f23d1 MH |
1043 | if (!trace_seq_printf(s, "%s: (", tp->call.name)) |
1044 | goto partial; | |
1045 | ||
413d37d1 MH |
1046 | if (!seq_print_ip_sym(s, field->ip, flags | TRACE_ITER_SYM_OFFSET)) |
1047 | goto partial; | |
1048 | ||
6e9f23d1 | 1049 | if (!trace_seq_puts(s, ")")) |
413d37d1 MH |
1050 | goto partial; |
1051 | ||
1052 | for (i = 0; i < field->nargs; i++) | |
eca0d916 MH |
1053 | if (!trace_seq_printf(s, " %s=%lx", |
1054 | tp->args[i].name, field->args[i])) | |
413d37d1 MH |
1055 | goto partial; |
1056 | ||
1057 | if (!trace_seq_puts(s, "\n")) | |
1058 | goto partial; | |
1059 | ||
1060 | return TRACE_TYPE_HANDLED; | |
1061 | partial: | |
1062 | return TRACE_TYPE_PARTIAL_LINE; | |
1063 | } | |
1064 | ||
1065 | enum print_line_t | |
1066 | print_kretprobe_event(struct trace_iterator *iter, int flags) | |
1067 | { | |
1068 | struct kretprobe_trace_entry *field; | |
1069 | struct trace_seq *s = &iter->seq; | |
eca0d916 MH |
1070 | struct trace_event *event; |
1071 | struct trace_probe *tp; | |
413d37d1 MH |
1072 | int i; |
1073 | ||
ff50d991 | 1074 | field = (struct kretprobe_trace_entry *)iter->ent; |
eca0d916 MH |
1075 | event = ftrace_find_event(field->ent.type); |
1076 | tp = container_of(event, struct trace_probe, event); | |
413d37d1 | 1077 | |
6e9f23d1 MH |
1078 | if (!trace_seq_printf(s, "%s: (", tp->call.name)) |
1079 | goto partial; | |
1080 | ||
413d37d1 MH |
1081 | if (!seq_print_ip_sym(s, field->ret_ip, flags | TRACE_ITER_SYM_OFFSET)) |
1082 | goto partial; | |
1083 | ||
1084 | if (!trace_seq_puts(s, " <- ")) | |
1085 | goto partial; | |
1086 | ||
1087 | if (!seq_print_ip_sym(s, field->func, flags & ~TRACE_ITER_SYM_OFFSET)) | |
1088 | goto partial; | |
1089 | ||
6e9f23d1 | 1090 | if (!trace_seq_puts(s, ")")) |
413d37d1 MH |
1091 | goto partial; |
1092 | ||
1093 | for (i = 0; i < field->nargs; i++) | |
eca0d916 MH |
1094 | if (!trace_seq_printf(s, " %s=%lx", |
1095 | tp->args[i].name, field->args[i])) | |
413d37d1 MH |
1096 | goto partial; |
1097 | ||
1098 | if (!trace_seq_puts(s, "\n")) | |
1099 | goto partial; | |
1100 | ||
1101 | return TRACE_TYPE_HANDLED; | |
1102 | partial: | |
1103 | return TRACE_TYPE_PARTIAL_LINE; | |
1104 | } | |
1105 | ||
413d37d1 MH |
1106 | static int probe_event_enable(struct ftrace_event_call *call) |
1107 | { | |
1108 | struct trace_probe *tp = (struct trace_probe *)call->data; | |
1109 | ||
50d78056 MH |
1110 | tp->flags |= TP_FLAG_TRACE; |
1111 | if (probe_is_return(tp)) | |
413d37d1 | 1112 | return enable_kretprobe(&tp->rp); |
50d78056 | 1113 | else |
4a846b44 | 1114 | return enable_kprobe(&tp->rp.kp); |
413d37d1 MH |
1115 | } |
1116 | ||
1117 | static void probe_event_disable(struct ftrace_event_call *call) | |
1118 | { | |
1119 | struct trace_probe *tp = (struct trace_probe *)call->data; | |
1120 | ||
50d78056 MH |
1121 | tp->flags &= ~TP_FLAG_TRACE; |
1122 | if (!(tp->flags & (TP_FLAG_TRACE | TP_FLAG_PROFILE))) { | |
1123 | if (probe_is_return(tp)) | |
1124 | disable_kretprobe(&tp->rp); | |
1125 | else | |
1126 | disable_kprobe(&tp->rp.kp); | |
1127 | } | |
413d37d1 MH |
1128 | } |
1129 | ||
1130 | static int probe_event_raw_init(struct ftrace_event_call *event_call) | |
1131 | { | |
1132 | INIT_LIST_HEAD(&event_call->fields); | |
8f8ffe24 | 1133 | |
413d37d1 MH |
1134 | return 0; |
1135 | } | |
1136 | ||
1137 | #undef DEFINE_FIELD | |
1138 | #define DEFINE_FIELD(type, item, name, is_signed) \ | |
1139 | do { \ | |
1140 | ret = trace_define_field(event_call, #type, name, \ | |
1141 | offsetof(typeof(field), item), \ | |
1142 | sizeof(field.item), is_signed, \ | |
1143 | FILTER_OTHER); \ | |
1144 | if (ret) \ | |
1145 | return ret; \ | |
1146 | } while (0) | |
1147 | ||
1148 | static int kprobe_event_define_fields(struct ftrace_event_call *event_call) | |
1149 | { | |
1150 | int ret, i; | |
1151 | struct kprobe_trace_entry field; | |
413d37d1 MH |
1152 | struct trace_probe *tp = (struct trace_probe *)event_call->data; |
1153 | ||
a703d946 MH |
1154 | DEFINE_FIELD(unsigned long, ip, FIELD_STRING_IP, 0); |
1155 | DEFINE_FIELD(int, nargs, FIELD_STRING_NARGS, 1); | |
eca0d916 MH |
1156 | /* Set argument names as fields */ |
1157 | for (i = 0; i < tp->nr_args; i++) | |
1158 | DEFINE_FIELD(unsigned long, args[i], tp->args[i].name, 0); | |
413d37d1 MH |
1159 | return 0; |
1160 | } | |
1161 | ||
1162 | static int kretprobe_event_define_fields(struct ftrace_event_call *event_call) | |
1163 | { | |
1164 | int ret, i; | |
1165 | struct kretprobe_trace_entry field; | |
413d37d1 MH |
1166 | struct trace_probe *tp = (struct trace_probe *)event_call->data; |
1167 | ||
a703d946 MH |
1168 | DEFINE_FIELD(unsigned long, func, FIELD_STRING_FUNC, 0); |
1169 | DEFINE_FIELD(unsigned long, ret_ip, FIELD_STRING_RETIP, 0); | |
1170 | DEFINE_FIELD(int, nargs, FIELD_STRING_NARGS, 1); | |
eca0d916 MH |
1171 | /* Set argument names as fields */ |
1172 | for (i = 0; i < tp->nr_args; i++) | |
1173 | DEFINE_FIELD(unsigned long, args[i], tp->args[i].name, 0); | |
413d37d1 MH |
1174 | return 0; |
1175 | } | |
1176 | ||
a342a028 LJ |
1177 | static int __set_print_fmt(struct trace_probe *tp, char *buf, int len) |
1178 | { | |
1179 | int i; | |
1180 | int pos = 0; | |
1181 | ||
1182 | const char *fmt, *arg; | |
1183 | ||
1184 | if (!probe_is_return(tp)) { | |
1185 | fmt = "(%lx)"; | |
1186 | arg = "REC->" FIELD_STRING_IP; | |
1187 | } else { | |
1188 | fmt = "(%lx <- %lx)"; | |
1189 | arg = "REC->" FIELD_STRING_FUNC ", REC->" FIELD_STRING_RETIP; | |
1190 | } | |
1191 | ||
1192 | /* When len=0, we just calculate the needed length */ | |
1193 | #define LEN_OR_ZERO (len ? len - pos : 0) | |
1194 | ||
1195 | pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", fmt); | |
1196 | ||
1197 | for (i = 0; i < tp->nr_args; i++) { | |
1198 | pos += snprintf(buf + pos, LEN_OR_ZERO, " %s=%%lx", | |
1199 | tp->args[i].name); | |
1200 | } | |
1201 | ||
1202 | pos += snprintf(buf + pos, LEN_OR_ZERO, "\", %s", arg); | |
1203 | ||
1204 | for (i = 0; i < tp->nr_args; i++) { | |
1205 | pos += snprintf(buf + pos, LEN_OR_ZERO, ", REC->%s", | |
1206 | tp->args[i].name); | |
1207 | } | |
1208 | ||
1209 | #undef LEN_OR_ZERO | |
1210 | ||
1211 | /* return the length of print_fmt */ | |
1212 | return pos; | |
1213 | } | |
1214 | ||
1215 | static int set_print_fmt(struct trace_probe *tp) | |
1216 | { | |
1217 | int len; | |
1218 | char *print_fmt; | |
1219 | ||
1220 | /* First: called with 0 length to calculate the needed length */ | |
1221 | len = __set_print_fmt(tp, NULL, 0); | |
1222 | print_fmt = kmalloc(len + 1, GFP_KERNEL); | |
1223 | if (!print_fmt) | |
1224 | return -ENOMEM; | |
1225 | ||
1226 | /* Second: actually write the @print_fmt */ | |
1227 | __set_print_fmt(tp, print_fmt, len + 1); | |
1228 | tp->call.print_fmt = print_fmt; | |
1229 | ||
1230 | return 0; | |
1231 | } | |
1232 | ||
e08d1c65 MH |
1233 | #ifdef CONFIG_EVENT_PROFILE |
1234 | ||
1235 | /* Kprobe profile handler */ | |
1236 | static __kprobes int kprobe_profile_func(struct kprobe *kp, | |
1237 | struct pt_regs *regs) | |
1238 | { | |
1239 | struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp); | |
1240 | struct ftrace_event_call *call = &tp->call; | |
1241 | struct kprobe_trace_entry *entry; | |
a1a138d0 MH |
1242 | struct trace_entry *ent; |
1243 | int size, __size, i, pc, __cpu; | |
e08d1c65 | 1244 | unsigned long irq_flags; |
ce71b9df | 1245 | char *trace_buf; |
a1a138d0 | 1246 | char *raw_data; |
4ed7c92d | 1247 | int rctx; |
e08d1c65 | 1248 | |
e08d1c65 | 1249 | pc = preempt_count(); |
74ebb63e MH |
1250 | __size = SIZEOF_KPROBE_TRACE_ENTRY(tp->nr_args); |
1251 | size = ALIGN(__size + sizeof(u32), sizeof(u64)); | |
1252 | size -= sizeof(u32); | |
a1a138d0 MH |
1253 | if (WARN_ONCE(size > FTRACE_MAX_PROFILE_SIZE, |
1254 | "profile buffer not large enough")) | |
1255 | return 0; | |
e08d1c65 | 1256 | |
a1a138d0 MH |
1257 | /* |
1258 | * Protect the non nmi buffer | |
1259 | * This also protects the rcu read side | |
1260 | */ | |
1261 | local_irq_save(irq_flags); | |
ce71b9df | 1262 | |
4ed7c92d PZ |
1263 | rctx = perf_swevent_get_recursion_context(); |
1264 | if (rctx < 0) | |
ce71b9df FW |
1265 | goto end_recursion; |
1266 | ||
a1a138d0 MH |
1267 | __cpu = smp_processor_id(); |
1268 | ||
1269 | if (in_nmi()) | |
444a2a3b | 1270 | trace_buf = rcu_dereference(perf_trace_buf_nmi); |
a1a138d0 | 1271 | else |
444a2a3b | 1272 | trace_buf = rcu_dereference(perf_trace_buf); |
a1a138d0 | 1273 | |
444a2a3b | 1274 | if (!trace_buf) |
a1a138d0 MH |
1275 | goto end; |
1276 | ||
ce71b9df | 1277 | raw_data = per_cpu_ptr(trace_buf, __cpu); |
444a2a3b | 1278 | |
a1a138d0 MH |
1279 | /* Zero dead bytes from alignment to avoid buffer leak to userspace */ |
1280 | *(u64 *)(&raw_data[size - sizeof(u64)]) = 0ULL; | |
1281 | entry = (struct kprobe_trace_entry *)raw_data; | |
1282 | ent = &entry->ent; | |
1283 | ||
1284 | tracing_generic_entry_update(ent, irq_flags, pc); | |
1285 | ent->type = call->id; | |
1286 | entry->nargs = tp->nr_args; | |
1287 | entry->ip = (unsigned long)kp->addr; | |
1288 | for (i = 0; i < tp->nr_args; i++) | |
1289 | entry->args[i] = call_fetch(&tp->args[i].fetch, regs); | |
1290 | perf_tp_event(call->id, entry->ip, 1, entry, size); | |
444a2a3b | 1291 | |
a1a138d0 | 1292 | end: |
4ed7c92d | 1293 | perf_swevent_put_recursion_context(rctx); |
ce71b9df | 1294 | end_recursion: |
a1a138d0 | 1295 | local_irq_restore(irq_flags); |
444a2a3b | 1296 | |
e08d1c65 MH |
1297 | return 0; |
1298 | } | |
1299 | ||
1300 | /* Kretprobe profile handler */ | |
1301 | static __kprobes int kretprobe_profile_func(struct kretprobe_instance *ri, | |
1302 | struct pt_regs *regs) | |
1303 | { | |
1304 | struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp); | |
1305 | struct ftrace_event_call *call = &tp->call; | |
1306 | struct kretprobe_trace_entry *entry; | |
a1a138d0 MH |
1307 | struct trace_entry *ent; |
1308 | int size, __size, i, pc, __cpu; | |
e08d1c65 | 1309 | unsigned long irq_flags; |
ce71b9df | 1310 | char *trace_buf; |
a1a138d0 | 1311 | char *raw_data; |
4ed7c92d | 1312 | int rctx; |
e08d1c65 | 1313 | |
e08d1c65 | 1314 | pc = preempt_count(); |
74ebb63e MH |
1315 | __size = SIZEOF_KRETPROBE_TRACE_ENTRY(tp->nr_args); |
1316 | size = ALIGN(__size + sizeof(u32), sizeof(u64)); | |
1317 | size -= sizeof(u32); | |
a1a138d0 MH |
1318 | if (WARN_ONCE(size > FTRACE_MAX_PROFILE_SIZE, |
1319 | "profile buffer not large enough")) | |
1320 | return 0; | |
1321 | ||
1322 | /* | |
1323 | * Protect the non nmi buffer | |
1324 | * This also protects the rcu read side | |
1325 | */ | |
1326 | local_irq_save(irq_flags); | |
ce71b9df | 1327 | |
4ed7c92d PZ |
1328 | rctx = perf_swevent_get_recursion_context(); |
1329 | if (rctx < 0) | |
ce71b9df FW |
1330 | goto end_recursion; |
1331 | ||
a1a138d0 MH |
1332 | __cpu = smp_processor_id(); |
1333 | ||
1334 | if (in_nmi()) | |
444a2a3b | 1335 | trace_buf = rcu_dereference(perf_trace_buf_nmi); |
a1a138d0 | 1336 | else |
444a2a3b | 1337 | trace_buf = rcu_dereference(perf_trace_buf); |
a1a138d0 | 1338 | |
444a2a3b | 1339 | if (!trace_buf) |
a1a138d0 MH |
1340 | goto end; |
1341 | ||
ce71b9df | 1342 | raw_data = per_cpu_ptr(trace_buf, __cpu); |
444a2a3b | 1343 | |
a1a138d0 MH |
1344 | /* Zero dead bytes from alignment to avoid buffer leak to userspace */ |
1345 | *(u64 *)(&raw_data[size - sizeof(u64)]) = 0ULL; | |
1346 | entry = (struct kretprobe_trace_entry *)raw_data; | |
1347 | ent = &entry->ent; | |
e08d1c65 | 1348 | |
a1a138d0 MH |
1349 | tracing_generic_entry_update(ent, irq_flags, pc); |
1350 | ent->type = call->id; | |
1351 | entry->nargs = tp->nr_args; | |
1352 | entry->func = (unsigned long)tp->rp.kp.addr; | |
1353 | entry->ret_ip = (unsigned long)ri->ret_addr; | |
1354 | for (i = 0; i < tp->nr_args; i++) | |
1355 | entry->args[i] = call_fetch(&tp->args[i].fetch, regs); | |
1356 | perf_tp_event(call->id, entry->ret_ip, 1, entry, size); | |
444a2a3b | 1357 | |
a1a138d0 | 1358 | end: |
4ed7c92d | 1359 | perf_swevent_put_recursion_context(rctx); |
ce71b9df | 1360 | end_recursion: |
a1a138d0 | 1361 | local_irq_restore(irq_flags); |
444a2a3b | 1362 | |
e08d1c65 MH |
1363 | return 0; |
1364 | } | |
1365 | ||
1366 | static int probe_profile_enable(struct ftrace_event_call *call) | |
1367 | { | |
1368 | struct trace_probe *tp = (struct trace_probe *)call->data; | |
1369 | ||
50d78056 | 1370 | tp->flags |= TP_FLAG_PROFILE; |
d7a4b414 | 1371 | |
50d78056 | 1372 | if (probe_is_return(tp)) |
e08d1c65 | 1373 | return enable_kretprobe(&tp->rp); |
50d78056 | 1374 | else |
e08d1c65 | 1375 | return enable_kprobe(&tp->rp.kp); |
e08d1c65 MH |
1376 | } |
1377 | ||
1378 | static void probe_profile_disable(struct ftrace_event_call *call) | |
1379 | { | |
50d78056 MH |
1380 | struct trace_probe *tp = (struct trace_probe *)call->data; |
1381 | ||
d7a4b414 | 1382 | tp->flags &= ~TP_FLAG_PROFILE; |
50d78056 | 1383 | |
d7a4b414 | 1384 | if (!(tp->flags & TP_FLAG_TRACE)) { |
50d78056 MH |
1385 | if (probe_is_return(tp)) |
1386 | disable_kretprobe(&tp->rp); | |
1387 | else | |
1388 | disable_kprobe(&tp->rp.kp); | |
1389 | } | |
e08d1c65 | 1390 | } |
50d78056 MH |
1391 | #endif /* CONFIG_EVENT_PROFILE */ |
1392 | ||
1393 | ||
1394 | static __kprobes | |
1395 | int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs) | |
1396 | { | |
1397 | struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp); | |
e08d1c65 | 1398 | |
50d78056 MH |
1399 | if (tp->flags & TP_FLAG_TRACE) |
1400 | kprobe_trace_func(kp, regs); | |
1401 | #ifdef CONFIG_EVENT_PROFILE | |
1402 | if (tp->flags & TP_FLAG_PROFILE) | |
1403 | kprobe_profile_func(kp, regs); | |
e08d1c65 | 1404 | #endif /* CONFIG_EVENT_PROFILE */ |
50d78056 MH |
1405 | return 0; /* We don't tweek kernel, so just return 0 */ |
1406 | } | |
1407 | ||
1408 | static __kprobes | |
1409 | int kretprobe_dispatcher(struct kretprobe_instance *ri, struct pt_regs *regs) | |
1410 | { | |
1411 | struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp); | |
1412 | ||
1413 | if (tp->flags & TP_FLAG_TRACE) | |
1414 | kretprobe_trace_func(ri, regs); | |
1415 | #ifdef CONFIG_EVENT_PROFILE | |
1416 | if (tp->flags & TP_FLAG_PROFILE) | |
1417 | kretprobe_profile_func(ri, regs); | |
1418 | #endif /* CONFIG_EVENT_PROFILE */ | |
1419 | return 0; /* We don't tweek kernel, so just return 0 */ | |
1420 | } | |
e08d1c65 | 1421 | |
413d37d1 MH |
1422 | static int register_probe_event(struct trace_probe *tp) |
1423 | { | |
1424 | struct ftrace_event_call *call = &tp->call; | |
1425 | int ret; | |
1426 | ||
1427 | /* Initialize ftrace_event_call */ | |
413d37d1 | 1428 | if (probe_is_return(tp)) { |
ff50d991 | 1429 | tp->event.trace = print_kretprobe_event; |
413d37d1 | 1430 | call->raw_init = probe_event_raw_init; |
413d37d1 MH |
1431 | call->define_fields = kretprobe_event_define_fields; |
1432 | } else { | |
ff50d991 | 1433 | tp->event.trace = print_kprobe_event; |
413d37d1 | 1434 | call->raw_init = probe_event_raw_init; |
413d37d1 MH |
1435 | call->define_fields = kprobe_event_define_fields; |
1436 | } | |
a342a028 LJ |
1437 | if (set_print_fmt(tp) < 0) |
1438 | return -ENOMEM; | |
ff50d991 MH |
1439 | call->event = &tp->event; |
1440 | call->id = register_ftrace_event(&tp->event); | |
a342a028 LJ |
1441 | if (!call->id) { |
1442 | kfree(call->print_fmt); | |
ff50d991 | 1443 | return -ENODEV; |
a342a028 | 1444 | } |
5a0d9050 | 1445 | call->enabled = 0; |
413d37d1 MH |
1446 | call->regfunc = probe_event_enable; |
1447 | call->unregfunc = probe_event_disable; | |
e08d1c65 MH |
1448 | |
1449 | #ifdef CONFIG_EVENT_PROFILE | |
e08d1c65 MH |
1450 | call->profile_enable = probe_profile_enable; |
1451 | call->profile_disable = probe_profile_disable; | |
1452 | #endif | |
413d37d1 MH |
1453 | call->data = tp; |
1454 | ret = trace_add_event_call(call); | |
ff50d991 | 1455 | if (ret) { |
413d37d1 | 1456 | pr_info("Failed to register kprobe event: %s\n", call->name); |
a342a028 | 1457 | kfree(call->print_fmt); |
ff50d991 MH |
1458 | unregister_ftrace_event(&tp->event); |
1459 | } | |
413d37d1 MH |
1460 | return ret; |
1461 | } | |
1462 | ||
1463 | static void unregister_probe_event(struct trace_probe *tp) | |
1464 | { | |
ff50d991 | 1465 | /* tp->event is unregistered in trace_remove_event_call() */ |
413d37d1 | 1466 | trace_remove_event_call(&tp->call); |
a342a028 | 1467 | kfree(tp->call.print_fmt); |
413d37d1 MH |
1468 | } |
1469 | ||
1470 | /* Make a debugfs interface for controling probe points */ | |
1471 | static __init int init_kprobe_trace(void) | |
1472 | { | |
1473 | struct dentry *d_tracer; | |
1474 | struct dentry *entry; | |
413d37d1 MH |
1475 | |
1476 | d_tracer = tracing_init_dentry(); | |
1477 | if (!d_tracer) | |
1478 | return 0; | |
1479 | ||
1480 | entry = debugfs_create_file("kprobe_events", 0644, d_tracer, | |
1481 | NULL, &kprobe_events_ops); | |
1482 | ||
cd7e7bd5 | 1483 | /* Event list interface */ |
413d37d1 MH |
1484 | if (!entry) |
1485 | pr_warning("Could not create debugfs " | |
1486 | "'kprobe_events' entry\n"); | |
cd7e7bd5 MH |
1487 | |
1488 | /* Profile interface */ | |
1489 | entry = debugfs_create_file("kprobe_profile", 0444, d_tracer, | |
1490 | NULL, &kprobe_profile_ops); | |
1491 | ||
1492 | if (!entry) | |
1493 | pr_warning("Could not create debugfs " | |
1494 | "'kprobe_profile' entry\n"); | |
413d37d1 MH |
1495 | return 0; |
1496 | } | |
1497 | fs_initcall(init_kprobe_trace); | |
1498 | ||
1499 | ||
1500 | #ifdef CONFIG_FTRACE_STARTUP_TEST | |
1501 | ||
1502 | static int kprobe_trace_selftest_target(int a1, int a2, int a3, | |
1503 | int a4, int a5, int a6) | |
1504 | { | |
1505 | return a1 + a2 + a3 + a4 + a5 + a6; | |
1506 | } | |
1507 | ||
1508 | static __init int kprobe_trace_self_tests_init(void) | |
1509 | { | |
1510 | int ret; | |
1511 | int (*target)(int, int, int, int, int, int); | |
1512 | ||
1513 | target = kprobe_trace_selftest_target; | |
1514 | ||
1515 | pr_info("Testing kprobe tracing: "); | |
1516 | ||
1517 | ret = command_trace_probe("p:testprobe kprobe_trace_selftest_target " | |
f397af06 | 1518 | "$arg1 $arg2 $arg3 $arg4 $stack $stack0"); |
413d37d1 MH |
1519 | if (WARN_ON_ONCE(ret)) |
1520 | pr_warning("error enabling function entry\n"); | |
1521 | ||
1522 | ret = command_trace_probe("r:testprobe2 kprobe_trace_selftest_target " | |
f397af06 | 1523 | "$retval"); |
413d37d1 MH |
1524 | if (WARN_ON_ONCE(ret)) |
1525 | pr_warning("error enabling function return\n"); | |
1526 | ||
1527 | ret = target(1, 2, 3, 4, 5, 6); | |
1528 | ||
1529 | cleanup_all_probes(); | |
1530 | ||
1531 | pr_cont("OK\n"); | |
1532 | return 0; | |
1533 | } | |
1534 | ||
1535 | late_initcall(kprobe_trace_self_tests_init); | |
1536 | ||
1537 | #endif |