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