]> bbs.cooldavid.org Git - net-next-2.6.git/blame - kernel/trace/trace_kprobe.c
tracing/kprobes: Avoid field name confliction
[net-next-2.6.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
MH
41/* Reserved field names */
42#define FIELD_STRING_IP "ip"
43#define FIELD_STRING_NARGS "nargs"
44#define FIELD_STRING_RETIP "ret_ip"
45#define FIELD_STRING_FUNC "func"
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
356static struct trace_probe *find_probe_event(const char *event)
357{
358 struct trace_probe *tp;
359
360 list_for_each_entry(tp, &probe_list, list)
4263565d 361 if (!strcmp(tp->call.name, event))
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
MH
385 /* register as an event */
386 old_tp = find_probe_event(tp->call.name);
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
MH
599
600 if (argc < 2)
601 return -EINVAL;
602
603 if (argv[0][0] == 'p')
604 is_return = 0;
605 else if (argv[0][0] == 'r')
606 is_return = 1;
607 else
608 return -EINVAL;
609
610 if (argv[0][1] == ':') {
611 event = &argv[0][2];
f52487e9
MH
612 if (strchr(event, '/')) {
613 group = event;
614 event = strchr(group, '/') + 1;
615 event[-1] = '\0';
616 if (strlen(group) == 0) {
617 pr_info("Group name is not specifiled\n");
618 return -EINVAL;
619 }
620 }
413d37d1
MH
621 if (strlen(event) == 0) {
622 pr_info("Event name is not specifiled\n");
623 return -EINVAL;
624 }
625 }
626
627 if (isdigit(argv[1][0])) {
628 if (is_return)
629 return -EINVAL;
630 /* an address specified */
631 ret = strict_strtoul(&argv[0][2], 0, (unsigned long *)&addr);
632 if (ret)
633 return ret;
634 } else {
635 /* a symbol specified */
636 symbol = argv[1];
637 /* TODO: support .init module functions */
638 ret = split_symbol_offset(symbol, &offset);
639 if (ret)
640 return ret;
641 if (offset && is_return)
642 return -EINVAL;
643 }
a82378d8 644 argc -= 2; argv += 2;
413d37d1
MH
645
646 /* setup a probe */
f52487e9
MH
647 if (!group)
648 group = KPROBE_EVENT_SYSTEM;
4263565d
MH
649 if (!event) {
650 /* Make a new event name */
4263565d
MH
651 if (symbol)
652 snprintf(buf, MAX_EVENT_NAME_LEN, "%c@%s%+ld",
653 is_return ? 'r' : 'p', symbol, offset);
654 else
655 snprintf(buf, MAX_EVENT_NAME_LEN, "%c@0x%p",
656 is_return ? 'r' : 'p', addr);
4a846b44
MH
657 event = buf;
658 }
f52487e9
MH
659 tp = alloc_trace_probe(group, event, addr, symbol, offset, argc,
660 is_return);
413d37d1
MH
661 if (IS_ERR(tp))
662 return PTR_ERR(tp);
663
413d37d1 664 /* parse arguments */
a82378d8
MH
665 ret = 0;
666 for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
eca0d916
MH
667 /* Parse argument name */
668 arg = strchr(argv[i], '=');
669 if (arg)
670 *arg++ = '\0';
671 else
672 arg = argv[i];
a703d946
MH
673
674 if (conflict_field_name(argv[i], tp->args, i)) {
675 ret = -EINVAL;
676 goto error;
677 }
678
eca0d916
MH
679 tp->args[i].name = kstrdup(argv[i], GFP_KERNEL);
680
681 /* Parse fetch argument */
682 if (strlen(arg) > MAX_ARGSTR_LEN) {
683 pr_info("Argument%d(%s) is too long.\n", i, arg);
413d37d1
MH
684 ret = -ENOSPC;
685 goto error;
686 }
eca0d916 687 ret = parse_probe_arg(arg, &tp->args[i].fetch, is_return);
413d37d1
MH
688 if (ret)
689 goto error;
690 }
691 tp->nr_args = i;
692
693 ret = register_trace_probe(tp);
694 if (ret)
695 goto error;
696 return 0;
697
698error:
699 free_trace_probe(tp);
700 return ret;
701}
702
703static void cleanup_all_probes(void)
704{
705 struct trace_probe *tp;
706
707 mutex_lock(&probe_lock);
708 /* TODO: Use batch unregistration */
709 while (!list_empty(&probe_list)) {
710 tp = list_entry(probe_list.next, struct trace_probe, list);
711 unregister_trace_probe(tp);
712 free_trace_probe(tp);
713 }
714 mutex_unlock(&probe_lock);
715}
716
717
718/* Probes listing interfaces */
719static void *probes_seq_start(struct seq_file *m, loff_t *pos)
720{
721 mutex_lock(&probe_lock);
722 return seq_list_start(&probe_list, *pos);
723}
724
725static void *probes_seq_next(struct seq_file *m, void *v, loff_t *pos)
726{
727 return seq_list_next(v, &probe_list, pos);
728}
729
730static void probes_seq_stop(struct seq_file *m, void *v)
731{
732 mutex_unlock(&probe_lock);
733}
734
735static int probes_seq_show(struct seq_file *m, void *v)
736{
737 struct trace_probe *tp = v;
738 int i, ret;
739 char buf[MAX_ARGSTR_LEN + 1];
740
741 seq_printf(m, "%c", probe_is_return(tp) ? 'r' : 'p');
4263565d 742 seq_printf(m, ":%s", tp->call.name);
413d37d1
MH
743
744 if (tp->symbol)
4a846b44 745 seq_printf(m, " %s+%u", probe_symbol(tp), tp->rp.kp.offset);
413d37d1 746 else
4a846b44 747 seq_printf(m, " 0x%p", tp->rp.kp.addr);
413d37d1
MH
748
749 for (i = 0; i < tp->nr_args; i++) {
eca0d916 750 ret = probe_arg_string(buf, MAX_ARGSTR_LEN, &tp->args[i].fetch);
413d37d1
MH
751 if (ret < 0) {
752 pr_warning("Argument%d decoding error(%d).\n", i, ret);
753 return ret;
754 }
eca0d916 755 seq_printf(m, " %s=%s", tp->args[i].name, buf);
413d37d1
MH
756 }
757 seq_printf(m, "\n");
758 return 0;
759}
760
761static const struct seq_operations probes_seq_op = {
762 .start = probes_seq_start,
763 .next = probes_seq_next,
764 .stop = probes_seq_stop,
765 .show = probes_seq_show
766};
767
768static int probes_open(struct inode *inode, struct file *file)
769{
770 if ((file->f_mode & FMODE_WRITE) &&
771 (file->f_flags & O_TRUNC))
772 cleanup_all_probes();
773
774 return seq_open(file, &probes_seq_op);
775}
776
777static int command_trace_probe(const char *buf)
778{
779 char **argv;
780 int argc = 0, ret = 0;
781
782 argv = argv_split(GFP_KERNEL, buf, &argc);
783 if (!argv)
784 return -ENOMEM;
785
786 if (argc)
787 ret = create_trace_probe(argc, argv);
788
789 argv_free(argv);
790 return ret;
791}
792
793#define WRITE_BUFSIZE 128
794
795static ssize_t probes_write(struct file *file, const char __user *buffer,
796 size_t count, loff_t *ppos)
797{
798 char *kbuf, *tmp;
799 int ret;
800 size_t done;
801 size_t size;
802
803 kbuf = kmalloc(WRITE_BUFSIZE, GFP_KERNEL);
804 if (!kbuf)
805 return -ENOMEM;
806
807 ret = done = 0;
808 while (done < count) {
809 size = count - done;
810 if (size >= WRITE_BUFSIZE)
811 size = WRITE_BUFSIZE - 1;
812 if (copy_from_user(kbuf, buffer + done, size)) {
813 ret = -EFAULT;
814 goto out;
815 }
816 kbuf[size] = '\0';
817 tmp = strchr(kbuf, '\n');
818 if (tmp) {
819 *tmp = '\0';
820 size = tmp - kbuf + 1;
821 } else if (done + size < count) {
822 pr_warning("Line length is too long: "
823 "Should be less than %d.", WRITE_BUFSIZE);
824 ret = -EINVAL;
825 goto out;
826 }
827 done += size;
828 /* Remove comments */
829 tmp = strchr(kbuf, '#');
830 if (tmp)
831 *tmp = '\0';
832
833 ret = command_trace_probe(kbuf);
834 if (ret)
835 goto out;
836 }
837 ret = done;
838out:
839 kfree(kbuf);
840 return ret;
841}
842
843static const struct file_operations kprobe_events_ops = {
844 .owner = THIS_MODULE,
845 .open = probes_open,
846 .read = seq_read,
847 .llseek = seq_lseek,
848 .release = seq_release,
849 .write = probes_write,
850};
851
cd7e7bd5
MH
852/* Probes profiling interfaces */
853static int probes_profile_seq_show(struct seq_file *m, void *v)
854{
855 struct trace_probe *tp = v;
856
857 seq_printf(m, " %-44s %15lu %15lu\n", tp->call.name, tp->nhit,
4a846b44 858 tp->rp.kp.nmissed);
cd7e7bd5
MH
859
860 return 0;
861}
862
863static const struct seq_operations profile_seq_op = {
864 .start = probes_seq_start,
865 .next = probes_seq_next,
866 .stop = probes_seq_stop,
867 .show = probes_profile_seq_show
868};
869
870static int profile_open(struct inode *inode, struct file *file)
871{
872 return seq_open(file, &profile_seq_op);
873}
874
875static const struct file_operations kprobe_profile_ops = {
876 .owner = THIS_MODULE,
877 .open = profile_open,
878 .read = seq_read,
879 .llseek = seq_lseek,
880 .release = seq_release,
881};
882
413d37d1
MH
883/* Kprobe handler */
884static __kprobes int kprobe_trace_func(struct kprobe *kp, struct pt_regs *regs)
885{
4a846b44 886 struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
413d37d1
MH
887 struct kprobe_trace_entry *entry;
888 struct ring_buffer_event *event;
8f8ffe24 889 struct ring_buffer *buffer;
413d37d1
MH
890 int size, i, pc;
891 unsigned long irq_flags;
4263565d 892 struct ftrace_event_call *call = &tp->call;
413d37d1 893
cd7e7bd5
MH
894 tp->nhit++;
895
413d37d1
MH
896 local_save_flags(irq_flags);
897 pc = preempt_count();
898
899 size = SIZEOF_KPROBE_TRACE_ENTRY(tp->nr_args);
900
8f8ffe24 901 event = trace_current_buffer_lock_reserve(&buffer, call->id, size,
413d37d1
MH
902 irq_flags, pc);
903 if (!event)
904 return 0;
905
906 entry = ring_buffer_event_data(event);
907 entry->nargs = tp->nr_args;
908 entry->ip = (unsigned long)kp->addr;
909 for (i = 0; i < tp->nr_args; i++)
eca0d916 910 entry->args[i] = call_fetch(&tp->args[i].fetch, regs);
413d37d1 911
8f8ffe24
FW
912 if (!filter_current_check_discard(buffer, call, entry, event))
913 trace_nowake_buffer_unlock_commit(buffer, event, irq_flags, pc);
413d37d1
MH
914 return 0;
915}
916
917/* Kretprobe handler */
918static __kprobes int kretprobe_trace_func(struct kretprobe_instance *ri,
919 struct pt_regs *regs)
920{
921 struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
922 struct kretprobe_trace_entry *entry;
923 struct ring_buffer_event *event;
8f8ffe24 924 struct ring_buffer *buffer;
413d37d1
MH
925 int size, i, pc;
926 unsigned long irq_flags;
4263565d 927 struct ftrace_event_call *call = &tp->call;
413d37d1
MH
928
929 local_save_flags(irq_flags);
930 pc = preempt_count();
931
932 size = SIZEOF_KRETPROBE_TRACE_ENTRY(tp->nr_args);
933
8f8ffe24 934 event = trace_current_buffer_lock_reserve(&buffer, call->id, size,
413d37d1
MH
935 irq_flags, pc);
936 if (!event)
937 return 0;
938
939 entry = ring_buffer_event_data(event);
940 entry->nargs = tp->nr_args;
4a846b44 941 entry->func = (unsigned long)tp->rp.kp.addr;
413d37d1
MH
942 entry->ret_ip = (unsigned long)ri->ret_addr;
943 for (i = 0; i < tp->nr_args; i++)
eca0d916 944 entry->args[i] = call_fetch(&tp->args[i].fetch, regs);
413d37d1 945
8f8ffe24
FW
946 if (!filter_current_check_discard(buffer, call, entry, event))
947 trace_nowake_buffer_unlock_commit(buffer, event, irq_flags, pc);
413d37d1
MH
948
949 return 0;
950}
951
952/* Event entry printers */
953enum print_line_t
954print_kprobe_event(struct trace_iterator *iter, int flags)
955{
956 struct kprobe_trace_entry *field;
957 struct trace_seq *s = &iter->seq;
eca0d916
MH
958 struct trace_event *event;
959 struct trace_probe *tp;
413d37d1
MH
960 int i;
961
ff50d991 962 field = (struct kprobe_trace_entry *)iter->ent;
eca0d916
MH
963 event = ftrace_find_event(field->ent.type);
964 tp = container_of(event, struct trace_probe, event);
413d37d1 965
6e9f23d1
MH
966 if (!trace_seq_printf(s, "%s: (", tp->call.name))
967 goto partial;
968
413d37d1
MH
969 if (!seq_print_ip_sym(s, field->ip, flags | TRACE_ITER_SYM_OFFSET))
970 goto partial;
971
6e9f23d1 972 if (!trace_seq_puts(s, ")"))
413d37d1
MH
973 goto partial;
974
975 for (i = 0; i < field->nargs; i++)
eca0d916
MH
976 if (!trace_seq_printf(s, " %s=%lx",
977 tp->args[i].name, field->args[i]))
413d37d1
MH
978 goto partial;
979
980 if (!trace_seq_puts(s, "\n"))
981 goto partial;
982
983 return TRACE_TYPE_HANDLED;
984partial:
985 return TRACE_TYPE_PARTIAL_LINE;
986}
987
988enum print_line_t
989print_kretprobe_event(struct trace_iterator *iter, int flags)
990{
991 struct kretprobe_trace_entry *field;
992 struct trace_seq *s = &iter->seq;
eca0d916
MH
993 struct trace_event *event;
994 struct trace_probe *tp;
413d37d1
MH
995 int i;
996
ff50d991 997 field = (struct kretprobe_trace_entry *)iter->ent;
eca0d916
MH
998 event = ftrace_find_event(field->ent.type);
999 tp = container_of(event, struct trace_probe, event);
413d37d1 1000
6e9f23d1
MH
1001 if (!trace_seq_printf(s, "%s: (", tp->call.name))
1002 goto partial;
1003
413d37d1
MH
1004 if (!seq_print_ip_sym(s, field->ret_ip, flags | TRACE_ITER_SYM_OFFSET))
1005 goto partial;
1006
1007 if (!trace_seq_puts(s, " <- "))
1008 goto partial;
1009
1010 if (!seq_print_ip_sym(s, field->func, flags & ~TRACE_ITER_SYM_OFFSET))
1011 goto partial;
1012
6e9f23d1 1013 if (!trace_seq_puts(s, ")"))
413d37d1
MH
1014 goto partial;
1015
1016 for (i = 0; i < field->nargs; i++)
eca0d916
MH
1017 if (!trace_seq_printf(s, " %s=%lx",
1018 tp->args[i].name, field->args[i]))
413d37d1
MH
1019 goto partial;
1020
1021 if (!trace_seq_puts(s, "\n"))
1022 goto partial;
1023
1024 return TRACE_TYPE_HANDLED;
1025partial:
1026 return TRACE_TYPE_PARTIAL_LINE;
1027}
1028
413d37d1
MH
1029static int probe_event_enable(struct ftrace_event_call *call)
1030{
1031 struct trace_probe *tp = (struct trace_probe *)call->data;
1032
50d78056
MH
1033 tp->flags |= TP_FLAG_TRACE;
1034 if (probe_is_return(tp))
413d37d1 1035 return enable_kretprobe(&tp->rp);
50d78056 1036 else
4a846b44 1037 return enable_kprobe(&tp->rp.kp);
413d37d1
MH
1038}
1039
1040static void probe_event_disable(struct ftrace_event_call *call)
1041{
1042 struct trace_probe *tp = (struct trace_probe *)call->data;
1043
50d78056
MH
1044 tp->flags &= ~TP_FLAG_TRACE;
1045 if (!(tp->flags & (TP_FLAG_TRACE | TP_FLAG_PROFILE))) {
1046 if (probe_is_return(tp))
1047 disable_kretprobe(&tp->rp);
1048 else
1049 disable_kprobe(&tp->rp.kp);
1050 }
413d37d1
MH
1051}
1052
1053static int probe_event_raw_init(struct ftrace_event_call *event_call)
1054{
1055 INIT_LIST_HEAD(&event_call->fields);
8f8ffe24 1056
413d37d1
MH
1057 return 0;
1058}
1059
1060#undef DEFINE_FIELD
1061#define DEFINE_FIELD(type, item, name, is_signed) \
1062 do { \
1063 ret = trace_define_field(event_call, #type, name, \
1064 offsetof(typeof(field), item), \
1065 sizeof(field.item), is_signed, \
1066 FILTER_OTHER); \
1067 if (ret) \
1068 return ret; \
1069 } while (0)
1070
1071static int kprobe_event_define_fields(struct ftrace_event_call *event_call)
1072{
1073 int ret, i;
1074 struct kprobe_trace_entry field;
413d37d1
MH
1075 struct trace_probe *tp = (struct trace_probe *)event_call->data;
1076
1077 ret = trace_define_common_fields(event_call);
1078 if (!ret)
1079 return ret;
1080
a703d946
MH
1081 DEFINE_FIELD(unsigned long, ip, FIELD_STRING_IP, 0);
1082 DEFINE_FIELD(int, nargs, FIELD_STRING_NARGS, 1);
eca0d916
MH
1083 /* Set argument names as fields */
1084 for (i = 0; i < tp->nr_args; i++)
1085 DEFINE_FIELD(unsigned long, args[i], tp->args[i].name, 0);
413d37d1
MH
1086 return 0;
1087}
1088
1089static int kretprobe_event_define_fields(struct ftrace_event_call *event_call)
1090{
1091 int ret, i;
1092 struct kretprobe_trace_entry field;
413d37d1
MH
1093 struct trace_probe *tp = (struct trace_probe *)event_call->data;
1094
1095 ret = trace_define_common_fields(event_call);
1096 if (!ret)
1097 return ret;
1098
a703d946
MH
1099 DEFINE_FIELD(unsigned long, func, FIELD_STRING_FUNC, 0);
1100 DEFINE_FIELD(unsigned long, ret_ip, FIELD_STRING_RETIP, 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 __probe_event_show_format(struct trace_seq *s,
1109 struct trace_probe *tp, const char *fmt,
1110 const char *arg)
1111{
eca0d916 1112 int i;
413d37d1 1113
413d37d1
MH
1114 /* Show format */
1115 if (!trace_seq_printf(s, "\nprint fmt: \"%s", fmt))
1116 return 0;
1117
1118 for (i = 0; i < tp->nr_args; i++)
eca0d916 1119 if (!trace_seq_printf(s, " %s=%%lx", tp->args[i].name))
413d37d1
MH
1120 return 0;
1121
1122 if (!trace_seq_printf(s, "\", %s", arg))
1123 return 0;
1124
1125 for (i = 0; i < tp->nr_args; i++)
eca0d916 1126 if (!trace_seq_printf(s, ", REC->%s", tp->args[i].name))
413d37d1
MH
1127 return 0;
1128
1129 return trace_seq_puts(s, "\n");
1130}
1131
1132#undef SHOW_FIELD
1133#define SHOW_FIELD(type, item, name) \
1134 do { \
1135 ret = trace_seq_printf(s, "\tfield: " #type " %s;\t" \
38a47497 1136 "offset:%u;\tsize:%u;\n", name, \
413d37d1
MH
1137 (unsigned int)offsetof(typeof(field), item),\
1138 (unsigned int)sizeof(type)); \
1139 if (!ret) \
1140 return 0; \
1141 } while (0)
1142
1143static int kprobe_event_show_format(struct ftrace_event_call *call,
1144 struct trace_seq *s)
1145{
1146 struct kprobe_trace_entry field __attribute__((unused));
1147 int ret, i;
413d37d1
MH
1148 struct trace_probe *tp = (struct trace_probe *)call->data;
1149
a703d946
MH
1150 SHOW_FIELD(unsigned long, ip, FIELD_STRING_IP);
1151 SHOW_FIELD(int, nargs, FIELD_STRING_NARGS);
413d37d1
MH
1152
1153 /* Show fields */
eca0d916
MH
1154 for (i = 0; i < tp->nr_args; i++)
1155 SHOW_FIELD(unsigned long, args[i], tp->args[i].name);
413d37d1
MH
1156 trace_seq_puts(s, "\n");
1157
a703d946
MH
1158 return __probe_event_show_format(s, tp, "(%lx)",
1159 "REC->" FIELD_STRING_IP);
413d37d1
MH
1160}
1161
1162static int kretprobe_event_show_format(struct ftrace_event_call *call,
1163 struct trace_seq *s)
1164{
1165 struct kretprobe_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, func, FIELD_STRING_FUNC);
1170 SHOW_FIELD(unsigned long, ret_ip, FIELD_STRING_RETIP);
1171 SHOW_FIELD(int, nargs, FIELD_STRING_NARGS);
413d37d1
MH
1172
1173 /* Show fields */
eca0d916
MH
1174 for (i = 0; i < tp->nr_args; i++)
1175 SHOW_FIELD(unsigned long, args[i], tp->args[i].name);
413d37d1
MH
1176 trace_seq_puts(s, "\n");
1177
6e9f23d1 1178 return __probe_event_show_format(s, tp, "(%lx <- %lx)",
a703d946
MH
1179 "REC->" FIELD_STRING_FUNC
1180 ", REC->" FIELD_STRING_RETIP);
413d37d1
MH
1181}
1182
e08d1c65
MH
1183#ifdef CONFIG_EVENT_PROFILE
1184
1185/* Kprobe profile handler */
1186static __kprobes int kprobe_profile_func(struct kprobe *kp,
1187 struct pt_regs *regs)
1188{
1189 struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
1190 struct ftrace_event_call *call = &tp->call;
1191 struct kprobe_trace_entry *entry;
a1a138d0
MH
1192 struct trace_entry *ent;
1193 int size, __size, i, pc, __cpu;
e08d1c65 1194 unsigned long irq_flags;
a1a138d0 1195 char *raw_data;
e08d1c65 1196
e08d1c65 1197 pc = preempt_count();
74ebb63e
MH
1198 __size = SIZEOF_KPROBE_TRACE_ENTRY(tp->nr_args);
1199 size = ALIGN(__size + sizeof(u32), sizeof(u64));
1200 size -= sizeof(u32);
a1a138d0
MH
1201 if (WARN_ONCE(size > FTRACE_MAX_PROFILE_SIZE,
1202 "profile buffer not large enough"))
1203 return 0;
e08d1c65 1204
a1a138d0
MH
1205 /*
1206 * Protect the non nmi buffer
1207 * This also protects the rcu read side
1208 */
1209 local_irq_save(irq_flags);
1210 __cpu = smp_processor_id();
1211
1212 if (in_nmi())
1213 raw_data = rcu_dereference(trace_profile_buf_nmi);
1214 else
1215 raw_data = rcu_dereference(trace_profile_buf);
1216
1217 if (!raw_data)
1218 goto end;
1219
1220 raw_data = per_cpu_ptr(raw_data, __cpu);
1221 /* Zero dead bytes from alignment to avoid buffer leak to userspace */
1222 *(u64 *)(&raw_data[size - sizeof(u64)]) = 0ULL;
1223 entry = (struct kprobe_trace_entry *)raw_data;
1224 ent = &entry->ent;
1225
1226 tracing_generic_entry_update(ent, irq_flags, pc);
1227 ent->type = call->id;
1228 entry->nargs = tp->nr_args;
1229 entry->ip = (unsigned long)kp->addr;
1230 for (i = 0; i < tp->nr_args; i++)
1231 entry->args[i] = call_fetch(&tp->args[i].fetch, regs);
1232 perf_tp_event(call->id, entry->ip, 1, entry, size);
1233end:
1234 local_irq_restore(irq_flags);
e08d1c65
MH
1235 return 0;
1236}
1237
1238/* Kretprobe profile handler */
1239static __kprobes int kretprobe_profile_func(struct kretprobe_instance *ri,
1240 struct pt_regs *regs)
1241{
1242 struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
1243 struct ftrace_event_call *call = &tp->call;
1244 struct kretprobe_trace_entry *entry;
a1a138d0
MH
1245 struct trace_entry *ent;
1246 int size, __size, i, pc, __cpu;
e08d1c65 1247 unsigned long irq_flags;
a1a138d0 1248 char *raw_data;
e08d1c65 1249
e08d1c65 1250 pc = preempt_count();
74ebb63e
MH
1251 __size = SIZEOF_KRETPROBE_TRACE_ENTRY(tp->nr_args);
1252 size = ALIGN(__size + sizeof(u32), sizeof(u64));
1253 size -= sizeof(u32);
a1a138d0
MH
1254 if (WARN_ONCE(size > FTRACE_MAX_PROFILE_SIZE,
1255 "profile buffer not large enough"))
1256 return 0;
1257
1258 /*
1259 * Protect the non nmi buffer
1260 * This also protects the rcu read side
1261 */
1262 local_irq_save(irq_flags);
1263 __cpu = smp_processor_id();
1264
1265 if (in_nmi())
1266 raw_data = rcu_dereference(trace_profile_buf_nmi);
1267 else
1268 raw_data = rcu_dereference(trace_profile_buf);
1269
1270 if (!raw_data)
1271 goto end;
1272
1273 raw_data = per_cpu_ptr(raw_data, __cpu);
1274 /* Zero dead bytes from alignment to avoid buffer leak to userspace */
1275 *(u64 *)(&raw_data[size - sizeof(u64)]) = 0ULL;
1276 entry = (struct kretprobe_trace_entry *)raw_data;
1277 ent = &entry->ent;
e08d1c65 1278
a1a138d0
MH
1279 tracing_generic_entry_update(ent, irq_flags, pc);
1280 ent->type = call->id;
1281 entry->nargs = tp->nr_args;
1282 entry->func = (unsigned long)tp->rp.kp.addr;
1283 entry->ret_ip = (unsigned long)ri->ret_addr;
1284 for (i = 0; i < tp->nr_args; i++)
1285 entry->args[i] = call_fetch(&tp->args[i].fetch, regs);
1286 perf_tp_event(call->id, entry->ret_ip, 1, entry, size);
1287end:
1288 local_irq_restore(irq_flags);
e08d1c65
MH
1289 return 0;
1290}
1291
1292static int probe_profile_enable(struct ftrace_event_call *call)
1293{
1294 struct trace_probe *tp = (struct trace_probe *)call->data;
1295
50d78056 1296 tp->flags |= TP_FLAG_PROFILE;
d7a4b414 1297
50d78056 1298 if (probe_is_return(tp))
e08d1c65 1299 return enable_kretprobe(&tp->rp);
50d78056 1300 else
e08d1c65 1301 return enable_kprobe(&tp->rp.kp);
e08d1c65
MH
1302}
1303
1304static void probe_profile_disable(struct ftrace_event_call *call)
1305{
50d78056
MH
1306 struct trace_probe *tp = (struct trace_probe *)call->data;
1307
d7a4b414 1308 tp->flags &= ~TP_FLAG_PROFILE;
50d78056 1309
d7a4b414 1310 if (!(tp->flags & TP_FLAG_TRACE)) {
50d78056
MH
1311 if (probe_is_return(tp))
1312 disable_kretprobe(&tp->rp);
1313 else
1314 disable_kprobe(&tp->rp.kp);
1315 }
e08d1c65 1316}
50d78056
MH
1317#endif /* CONFIG_EVENT_PROFILE */
1318
1319
1320static __kprobes
1321int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs)
1322{
1323 struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
e08d1c65 1324
50d78056
MH
1325 if (tp->flags & TP_FLAG_TRACE)
1326 kprobe_trace_func(kp, regs);
1327#ifdef CONFIG_EVENT_PROFILE
1328 if (tp->flags & TP_FLAG_PROFILE)
1329 kprobe_profile_func(kp, regs);
e08d1c65 1330#endif /* CONFIG_EVENT_PROFILE */
50d78056
MH
1331 return 0; /* We don't tweek kernel, so just return 0 */
1332}
1333
1334static __kprobes
1335int kretprobe_dispatcher(struct kretprobe_instance *ri, struct pt_regs *regs)
1336{
1337 struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
1338
1339 if (tp->flags & TP_FLAG_TRACE)
1340 kretprobe_trace_func(ri, regs);
1341#ifdef CONFIG_EVENT_PROFILE
1342 if (tp->flags & TP_FLAG_PROFILE)
1343 kretprobe_profile_func(ri, regs);
1344#endif /* CONFIG_EVENT_PROFILE */
1345 return 0; /* We don't tweek kernel, so just return 0 */
1346}
e08d1c65 1347
413d37d1
MH
1348static int register_probe_event(struct trace_probe *tp)
1349{
1350 struct ftrace_event_call *call = &tp->call;
1351 int ret;
1352
1353 /* Initialize ftrace_event_call */
413d37d1 1354 if (probe_is_return(tp)) {
ff50d991 1355 tp->event.trace = print_kretprobe_event;
413d37d1
MH
1356 call->raw_init = probe_event_raw_init;
1357 call->show_format = kretprobe_event_show_format;
1358 call->define_fields = kretprobe_event_define_fields;
1359 } else {
ff50d991 1360 tp->event.trace = print_kprobe_event;
413d37d1
MH
1361 call->raw_init = probe_event_raw_init;
1362 call->show_format = kprobe_event_show_format;
1363 call->define_fields = kprobe_event_define_fields;
1364 }
ff50d991
MH
1365 call->event = &tp->event;
1366 call->id = register_ftrace_event(&tp->event);
1367 if (!call->id)
1368 return -ENODEV;
5a0d9050 1369 call->enabled = 0;
413d37d1
MH
1370 call->regfunc = probe_event_enable;
1371 call->unregfunc = probe_event_disable;
e08d1c65
MH
1372
1373#ifdef CONFIG_EVENT_PROFILE
1374 atomic_set(&call->profile_count, -1);
1375 call->profile_enable = probe_profile_enable;
1376 call->profile_disable = probe_profile_disable;
1377#endif
413d37d1
MH
1378 call->data = tp;
1379 ret = trace_add_event_call(call);
ff50d991 1380 if (ret) {
413d37d1 1381 pr_info("Failed to register kprobe event: %s\n", call->name);
ff50d991
MH
1382 unregister_ftrace_event(&tp->event);
1383 }
413d37d1
MH
1384 return ret;
1385}
1386
1387static void unregister_probe_event(struct trace_probe *tp)
1388{
ff50d991 1389 /* tp->event is unregistered in trace_remove_event_call() */
413d37d1
MH
1390 trace_remove_event_call(&tp->call);
1391}
1392
1393/* Make a debugfs interface for controling probe points */
1394static __init int init_kprobe_trace(void)
1395{
1396 struct dentry *d_tracer;
1397 struct dentry *entry;
413d37d1
MH
1398
1399 d_tracer = tracing_init_dentry();
1400 if (!d_tracer)
1401 return 0;
1402
1403 entry = debugfs_create_file("kprobe_events", 0644, d_tracer,
1404 NULL, &kprobe_events_ops);
1405
cd7e7bd5 1406 /* Event list interface */
413d37d1
MH
1407 if (!entry)
1408 pr_warning("Could not create debugfs "
1409 "'kprobe_events' entry\n");
cd7e7bd5
MH
1410
1411 /* Profile interface */
1412 entry = debugfs_create_file("kprobe_profile", 0444, d_tracer,
1413 NULL, &kprobe_profile_ops);
1414
1415 if (!entry)
1416 pr_warning("Could not create debugfs "
1417 "'kprobe_profile' entry\n");
413d37d1
MH
1418 return 0;
1419}
1420fs_initcall(init_kprobe_trace);
1421
1422
1423#ifdef CONFIG_FTRACE_STARTUP_TEST
1424
1425static int kprobe_trace_selftest_target(int a1, int a2, int a3,
1426 int a4, int a5, int a6)
1427{
1428 return a1 + a2 + a3 + a4 + a5 + a6;
1429}
1430
1431static __init int kprobe_trace_self_tests_init(void)
1432{
1433 int ret;
1434 int (*target)(int, int, int, int, int, int);
1435
1436 target = kprobe_trace_selftest_target;
1437
1438 pr_info("Testing kprobe tracing: ");
1439
1440 ret = command_trace_probe("p:testprobe kprobe_trace_selftest_target "
1441 "a1 a2 a3 a4 a5 a6");
1442 if (WARN_ON_ONCE(ret))
1443 pr_warning("error enabling function entry\n");
1444
1445 ret = command_trace_probe("r:testprobe2 kprobe_trace_selftest_target "
1446 "ra rv");
1447 if (WARN_ON_ONCE(ret))
1448 pr_warning("error enabling function return\n");
1449
1450 ret = target(1, 2, 3, 4, 5, 6);
1451
1452 cleanup_all_probes();
1453
1454 pr_cont("OK\n");
1455 return 0;
1456}
1457
1458late_initcall(kprobe_trace_self_tests_init);
1459
1460#endif