]> bbs.cooldavid.org Git - net-next-2.6.git/blame - kernel/trace/trace_kprobe.c
trace_kprobes: Fix memory leak
[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);
abab9d37 707 kfree(tp->args[i].name);
413d37d1 708 goto error;
e63cc239 709 }
abab9d37
LJ
710
711 tp->nr_args++;
413d37d1 712 }
413d37d1
MH
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;
ce71b9df 1216 char *trace_buf;
a1a138d0 1217 char *raw_data;
4ed7c92d 1218 int rctx;
e08d1c65 1219
e08d1c65 1220 pc = preempt_count();
74ebb63e
MH
1221 __size = SIZEOF_KPROBE_TRACE_ENTRY(tp->nr_args);
1222 size = ALIGN(__size + sizeof(u32), sizeof(u64));
1223 size -= sizeof(u32);
a1a138d0
MH
1224 if (WARN_ONCE(size > FTRACE_MAX_PROFILE_SIZE,
1225 "profile buffer not large enough"))
1226 return 0;
e08d1c65 1227
a1a138d0
MH
1228 /*
1229 * Protect the non nmi buffer
1230 * This also protects the rcu read side
1231 */
1232 local_irq_save(irq_flags);
ce71b9df 1233
4ed7c92d
PZ
1234 rctx = perf_swevent_get_recursion_context();
1235 if (rctx < 0)
ce71b9df
FW
1236 goto end_recursion;
1237
a1a138d0
MH
1238 __cpu = smp_processor_id();
1239
1240 if (in_nmi())
444a2a3b 1241 trace_buf = rcu_dereference(perf_trace_buf_nmi);
a1a138d0 1242 else
444a2a3b 1243 trace_buf = rcu_dereference(perf_trace_buf);
a1a138d0 1244
444a2a3b 1245 if (!trace_buf)
a1a138d0
MH
1246 goto end;
1247
ce71b9df 1248 raw_data = per_cpu_ptr(trace_buf, __cpu);
444a2a3b 1249
a1a138d0
MH
1250 /* Zero dead bytes from alignment to avoid buffer leak to userspace */
1251 *(u64 *)(&raw_data[size - sizeof(u64)]) = 0ULL;
1252 entry = (struct kprobe_trace_entry *)raw_data;
1253 ent = &entry->ent;
1254
1255 tracing_generic_entry_update(ent, irq_flags, pc);
1256 ent->type = call->id;
1257 entry->nargs = tp->nr_args;
1258 entry->ip = (unsigned long)kp->addr;
1259 for (i = 0; i < tp->nr_args; i++)
1260 entry->args[i] = call_fetch(&tp->args[i].fetch, regs);
1261 perf_tp_event(call->id, entry->ip, 1, entry, size);
444a2a3b 1262
a1a138d0 1263end:
4ed7c92d 1264 perf_swevent_put_recursion_context(rctx);
ce71b9df 1265end_recursion:
a1a138d0 1266 local_irq_restore(irq_flags);
444a2a3b 1267
e08d1c65
MH
1268 return 0;
1269}
1270
1271/* Kretprobe profile handler */
1272static __kprobes int kretprobe_profile_func(struct kretprobe_instance *ri,
1273 struct pt_regs *regs)
1274{
1275 struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
1276 struct ftrace_event_call *call = &tp->call;
1277 struct kretprobe_trace_entry *entry;
a1a138d0
MH
1278 struct trace_entry *ent;
1279 int size, __size, i, pc, __cpu;
e08d1c65 1280 unsigned long irq_flags;
ce71b9df 1281 char *trace_buf;
a1a138d0 1282 char *raw_data;
4ed7c92d 1283 int rctx;
e08d1c65 1284
e08d1c65 1285 pc = preempt_count();
74ebb63e
MH
1286 __size = SIZEOF_KRETPROBE_TRACE_ENTRY(tp->nr_args);
1287 size = ALIGN(__size + sizeof(u32), sizeof(u64));
1288 size -= sizeof(u32);
a1a138d0
MH
1289 if (WARN_ONCE(size > FTRACE_MAX_PROFILE_SIZE,
1290 "profile buffer not large enough"))
1291 return 0;
1292
1293 /*
1294 * Protect the non nmi buffer
1295 * This also protects the rcu read side
1296 */
1297 local_irq_save(irq_flags);
ce71b9df 1298
4ed7c92d
PZ
1299 rctx = perf_swevent_get_recursion_context();
1300 if (rctx < 0)
ce71b9df
FW
1301 goto end_recursion;
1302
a1a138d0
MH
1303 __cpu = smp_processor_id();
1304
1305 if (in_nmi())
444a2a3b 1306 trace_buf = rcu_dereference(perf_trace_buf_nmi);
a1a138d0 1307 else
444a2a3b 1308 trace_buf = rcu_dereference(perf_trace_buf);
a1a138d0 1309
444a2a3b 1310 if (!trace_buf)
a1a138d0
MH
1311 goto end;
1312
ce71b9df 1313 raw_data = per_cpu_ptr(trace_buf, __cpu);
444a2a3b 1314
a1a138d0
MH
1315 /* Zero dead bytes from alignment to avoid buffer leak to userspace */
1316 *(u64 *)(&raw_data[size - sizeof(u64)]) = 0ULL;
1317 entry = (struct kretprobe_trace_entry *)raw_data;
1318 ent = &entry->ent;
e08d1c65 1319
a1a138d0
MH
1320 tracing_generic_entry_update(ent, irq_flags, pc);
1321 ent->type = call->id;
1322 entry->nargs = tp->nr_args;
1323 entry->func = (unsigned long)tp->rp.kp.addr;
1324 entry->ret_ip = (unsigned long)ri->ret_addr;
1325 for (i = 0; i < tp->nr_args; i++)
1326 entry->args[i] = call_fetch(&tp->args[i].fetch, regs);
1327 perf_tp_event(call->id, entry->ret_ip, 1, entry, size);
444a2a3b 1328
a1a138d0 1329end:
4ed7c92d 1330 perf_swevent_put_recursion_context(rctx);
ce71b9df 1331end_recursion:
a1a138d0 1332 local_irq_restore(irq_flags);
444a2a3b 1333
e08d1c65
MH
1334 return 0;
1335}
1336
1337static int probe_profile_enable(struct ftrace_event_call *call)
1338{
1339 struct trace_probe *tp = (struct trace_probe *)call->data;
1340
50d78056 1341 tp->flags |= TP_FLAG_PROFILE;
d7a4b414 1342
50d78056 1343 if (probe_is_return(tp))
e08d1c65 1344 return enable_kretprobe(&tp->rp);
50d78056 1345 else
e08d1c65 1346 return enable_kprobe(&tp->rp.kp);
e08d1c65
MH
1347}
1348
1349static void probe_profile_disable(struct ftrace_event_call *call)
1350{
50d78056
MH
1351 struct trace_probe *tp = (struct trace_probe *)call->data;
1352
d7a4b414 1353 tp->flags &= ~TP_FLAG_PROFILE;
50d78056 1354
d7a4b414 1355 if (!(tp->flags & TP_FLAG_TRACE)) {
50d78056
MH
1356 if (probe_is_return(tp))
1357 disable_kretprobe(&tp->rp);
1358 else
1359 disable_kprobe(&tp->rp.kp);
1360 }
e08d1c65 1361}
50d78056
MH
1362#endif /* CONFIG_EVENT_PROFILE */
1363
1364
1365static __kprobes
1366int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs)
1367{
1368 struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
e08d1c65 1369
50d78056
MH
1370 if (tp->flags & TP_FLAG_TRACE)
1371 kprobe_trace_func(kp, regs);
1372#ifdef CONFIG_EVENT_PROFILE
1373 if (tp->flags & TP_FLAG_PROFILE)
1374 kprobe_profile_func(kp, regs);
e08d1c65 1375#endif /* CONFIG_EVENT_PROFILE */
50d78056
MH
1376 return 0; /* We don't tweek kernel, so just return 0 */
1377}
1378
1379static __kprobes
1380int kretprobe_dispatcher(struct kretprobe_instance *ri, struct pt_regs *regs)
1381{
1382 struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
1383
1384 if (tp->flags & TP_FLAG_TRACE)
1385 kretprobe_trace_func(ri, regs);
1386#ifdef CONFIG_EVENT_PROFILE
1387 if (tp->flags & TP_FLAG_PROFILE)
1388 kretprobe_profile_func(ri, regs);
1389#endif /* CONFIG_EVENT_PROFILE */
1390 return 0; /* We don't tweek kernel, so just return 0 */
1391}
e08d1c65 1392
413d37d1
MH
1393static int register_probe_event(struct trace_probe *tp)
1394{
1395 struct ftrace_event_call *call = &tp->call;
1396 int ret;
1397
1398 /* Initialize ftrace_event_call */
413d37d1 1399 if (probe_is_return(tp)) {
ff50d991 1400 tp->event.trace = print_kretprobe_event;
413d37d1
MH
1401 call->raw_init = probe_event_raw_init;
1402 call->show_format = kretprobe_event_show_format;
1403 call->define_fields = kretprobe_event_define_fields;
1404 } else {
ff50d991 1405 tp->event.trace = print_kprobe_event;
413d37d1
MH
1406 call->raw_init = probe_event_raw_init;
1407 call->show_format = kprobe_event_show_format;
1408 call->define_fields = kprobe_event_define_fields;
1409 }
ff50d991
MH
1410 call->event = &tp->event;
1411 call->id = register_ftrace_event(&tp->event);
1412 if (!call->id)
1413 return -ENODEV;
5a0d9050 1414 call->enabled = 0;
413d37d1
MH
1415 call->regfunc = probe_event_enable;
1416 call->unregfunc = probe_event_disable;
e08d1c65
MH
1417
1418#ifdef CONFIG_EVENT_PROFILE
1419 atomic_set(&call->profile_count, -1);
1420 call->profile_enable = probe_profile_enable;
1421 call->profile_disable = probe_profile_disable;
1422#endif
413d37d1
MH
1423 call->data = tp;
1424 ret = trace_add_event_call(call);
ff50d991 1425 if (ret) {
413d37d1 1426 pr_info("Failed to register kprobe event: %s\n", call->name);
ff50d991
MH
1427 unregister_ftrace_event(&tp->event);
1428 }
413d37d1
MH
1429 return ret;
1430}
1431
1432static void unregister_probe_event(struct trace_probe *tp)
1433{
ff50d991 1434 /* tp->event is unregistered in trace_remove_event_call() */
413d37d1
MH
1435 trace_remove_event_call(&tp->call);
1436}
1437
1438/* Make a debugfs interface for controling probe points */
1439static __init int init_kprobe_trace(void)
1440{
1441 struct dentry *d_tracer;
1442 struct dentry *entry;
413d37d1
MH
1443
1444 d_tracer = tracing_init_dentry();
1445 if (!d_tracer)
1446 return 0;
1447
1448 entry = debugfs_create_file("kprobe_events", 0644, d_tracer,
1449 NULL, &kprobe_events_ops);
1450
cd7e7bd5 1451 /* Event list interface */
413d37d1
MH
1452 if (!entry)
1453 pr_warning("Could not create debugfs "
1454 "'kprobe_events' entry\n");
cd7e7bd5
MH
1455
1456 /* Profile interface */
1457 entry = debugfs_create_file("kprobe_profile", 0444, d_tracer,
1458 NULL, &kprobe_profile_ops);
1459
1460 if (!entry)
1461 pr_warning("Could not create debugfs "
1462 "'kprobe_profile' entry\n");
413d37d1
MH
1463 return 0;
1464}
1465fs_initcall(init_kprobe_trace);
1466
1467
1468#ifdef CONFIG_FTRACE_STARTUP_TEST
1469
1470static int kprobe_trace_selftest_target(int a1, int a2, int a3,
1471 int a4, int a5, int a6)
1472{
1473 return a1 + a2 + a3 + a4 + a5 + a6;
1474}
1475
1476static __init int kprobe_trace_self_tests_init(void)
1477{
1478 int ret;
1479 int (*target)(int, int, int, int, int, int);
1480
1481 target = kprobe_trace_selftest_target;
1482
1483 pr_info("Testing kprobe tracing: ");
1484
1485 ret = command_trace_probe("p:testprobe kprobe_trace_selftest_target "
f397af06 1486 "$arg1 $arg2 $arg3 $arg4 $stack $stack0");
413d37d1
MH
1487 if (WARN_ON_ONCE(ret))
1488 pr_warning("error enabling function entry\n");
1489
1490 ret = command_trace_probe("r:testprobe2 kprobe_trace_selftest_target "
f397af06 1491 "$retval");
413d37d1
MH
1492 if (WARN_ON_ONCE(ret))
1493 pr_warning("error enabling function return\n");
1494
1495 ret = target(1, 2, 3, 4, 5, 6);
1496
1497 cleanup_all_probes();
1498
1499 pr_cont("OK\n");
1500 return 0;
1501}
1502
1503late_initcall(kprobe_trace_self_tests_init);
1504
1505#endif