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