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