tracing: Add kprobes event profiling interface
[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>
31
32#include "trace.h"
33#include "trace_output.h"
34
a82378d8 35#define MAX_TRACE_ARGS 128
413d37d1 36#define MAX_ARGSTR_LEN 63
4263565d 37#define MAX_EVENT_NAME_LEN 64
413d37d1
MH
38
39/* currently, trace_kprobe only supports X86. */
40
41struct fetch_func {
42 unsigned long (*func)(struct pt_regs *, void *);
43 void *data;
44};
45
46static __kprobes unsigned long call_fetch(struct fetch_func *f,
47 struct pt_regs *regs)
48{
49 return f->func(regs, f->data);
50}
51
52/* fetch handlers */
53static __kprobes unsigned long fetch_register(struct pt_regs *regs,
54 void *offset)
55{
56 return regs_get_register(regs, (unsigned int)((unsigned long)offset));
57}
58
59static __kprobes unsigned long fetch_stack(struct pt_regs *regs,
60 void *num)
61{
62 return regs_get_kernel_stack_nth(regs,
63 (unsigned int)((unsigned long)num));
64}
65
66static __kprobes unsigned long fetch_memory(struct pt_regs *regs, void *addr)
67{
68 unsigned long retval;
69
70 if (probe_kernel_address(addr, retval))
71 return 0;
72 return retval;
73}
74
75static __kprobes unsigned long fetch_argument(struct pt_regs *regs, void *num)
76{
77 return regs_get_argument_nth(regs, (unsigned int)((unsigned long)num));
78}
79
80static __kprobes unsigned long fetch_retvalue(struct pt_regs *regs,
81 void *dummy)
82{
83 return regs_return_value(regs);
84}
85
86static __kprobes unsigned long fetch_ip(struct pt_regs *regs, void *dummy)
87{
88 return instruction_pointer(regs);
89}
90
91static __kprobes unsigned long fetch_stack_address(struct pt_regs *regs,
92 void *dummy)
93{
94 return kernel_stack_pointer(regs);
95}
96
97/* Memory fetching by symbol */
98struct symbol_cache {
99 char *symbol;
100 long offset;
101 unsigned long addr;
102};
103
104static unsigned long update_symbol_cache(struct symbol_cache *sc)
105{
106 sc->addr = (unsigned long)kallsyms_lookup_name(sc->symbol);
107 if (sc->addr)
108 sc->addr += sc->offset;
109 return sc->addr;
110}
111
112static void free_symbol_cache(struct symbol_cache *sc)
113{
114 kfree(sc->symbol);
115 kfree(sc);
116}
117
118static struct symbol_cache *alloc_symbol_cache(const char *sym, long offset)
119{
120 struct symbol_cache *sc;
121
122 if (!sym || strlen(sym) == 0)
123 return NULL;
124 sc = kzalloc(sizeof(struct symbol_cache), GFP_KERNEL);
125 if (!sc)
126 return NULL;
127
128 sc->symbol = kstrdup(sym, GFP_KERNEL);
129 if (!sc->symbol) {
130 kfree(sc);
131 return NULL;
132 }
133 sc->offset = offset;
134
135 update_symbol_cache(sc);
136 return sc;
137}
138
139static __kprobes unsigned long fetch_symbol(struct pt_regs *regs, void *data)
140{
141 struct symbol_cache *sc = data;
142
143 if (sc->addr)
144 return fetch_memory(regs, (void *)sc->addr);
145 else
146 return 0;
147}
148
149/* Special indirect memory access interface */
150struct indirect_fetch_data {
151 struct fetch_func orig;
152 long offset;
153};
154
155static __kprobes unsigned long fetch_indirect(struct pt_regs *regs, void *data)
156{
157 struct indirect_fetch_data *ind = data;
158 unsigned long addr;
159
160 addr = call_fetch(&ind->orig, regs);
161 if (addr) {
162 addr += ind->offset;
163 return fetch_memory(regs, (void *)addr);
164 } else
165 return 0;
166}
167
168static __kprobes void free_indirect_fetch_data(struct indirect_fetch_data *data)
169{
170 if (data->orig.func == fetch_indirect)
171 free_indirect_fetch_data(data->orig.data);
172 else if (data->orig.func == fetch_symbol)
173 free_symbol_cache(data->orig.data);
174 kfree(data);
175}
176
177/**
178 * kprobe_trace_core
179 */
180
181struct trace_probe {
182 struct list_head list;
183 union {
184 struct kprobe kp;
185 struct kretprobe rp;
186 };
cd7e7bd5 187 unsigned long nhit;
413d37d1 188 const char *symbol; /* symbol name */
413d37d1 189 struct ftrace_event_call call;
ff50d991 190 struct trace_event event;
a82378d8
MH
191 unsigned int nr_args;
192 struct fetch_func args[];
413d37d1
MH
193};
194
a82378d8
MH
195#define SIZEOF_TRACE_PROBE(n) \
196 (offsetof(struct trace_probe, args) + \
197 (sizeof(struct fetch_func) * (n)))
198
413d37d1
MH
199static int kprobe_trace_func(struct kprobe *kp, struct pt_regs *regs);
200static int kretprobe_trace_func(struct kretprobe_instance *ri,
201 struct pt_regs *regs);
202
203static __kprobes int probe_is_return(struct trace_probe *tp)
204{
205 return (tp->rp.handler == kretprobe_trace_func);
206}
207
208static __kprobes const char *probe_symbol(struct trace_probe *tp)
209{
210 return tp->symbol ? tp->symbol : "unknown";
211}
212
213static __kprobes long probe_offset(struct trace_probe *tp)
214{
215 return (probe_is_return(tp)) ? tp->rp.kp.offset : tp->kp.offset;
216}
217
218static __kprobes void *probe_address(struct trace_probe *tp)
219{
220 return (probe_is_return(tp)) ? tp->rp.kp.addr : tp->kp.addr;
221}
222
223static int trace_arg_string(char *buf, size_t n, struct fetch_func *ff)
224{
225 int ret = -EINVAL;
226
227 if (ff->func == fetch_argument)
228 ret = snprintf(buf, n, "a%lu", (unsigned long)ff->data);
229 else if (ff->func == fetch_register) {
230 const char *name;
231 name = regs_query_register_name((unsigned int)((long)ff->data));
232 ret = snprintf(buf, n, "%%%s", name);
233 } else if (ff->func == fetch_stack)
234 ret = snprintf(buf, n, "s%lu", (unsigned long)ff->data);
235 else if (ff->func == fetch_memory)
236 ret = snprintf(buf, n, "@0x%p", ff->data);
237 else if (ff->func == fetch_symbol) {
238 struct symbol_cache *sc = ff->data;
239 ret = snprintf(buf, n, "@%s%+ld", sc->symbol, sc->offset);
240 } else if (ff->func == fetch_retvalue)
241 ret = snprintf(buf, n, "rv");
242 else if (ff->func == fetch_ip)
243 ret = snprintf(buf, n, "ra");
244 else if (ff->func == fetch_stack_address)
245 ret = snprintf(buf, n, "sa");
246 else if (ff->func == fetch_indirect) {
247 struct indirect_fetch_data *id = ff->data;
248 size_t l = 0;
249 ret = snprintf(buf, n, "%+ld(", id->offset);
250 if (ret >= n)
251 goto end;
252 l += ret;
253 ret = trace_arg_string(buf + l, n - l, &id->orig);
254 if (ret < 0)
255 goto end;
256 l += ret;
257 ret = snprintf(buf + l, n - l, ")");
258 ret += l;
259 }
260end:
261 if (ret >= n)
262 return -ENOSPC;
263 return ret;
264}
265
266static int register_probe_event(struct trace_probe *tp);
267static void unregister_probe_event(struct trace_probe *tp);
268
269static DEFINE_MUTEX(probe_lock);
270static LIST_HEAD(probe_list);
271
272static struct trace_probe *alloc_trace_probe(const char *symbol,
a82378d8 273 const char *event, int nargs)
413d37d1
MH
274{
275 struct trace_probe *tp;
276
a82378d8 277 tp = kzalloc(SIZEOF_TRACE_PROBE(nargs), GFP_KERNEL);
413d37d1
MH
278 if (!tp)
279 return ERR_PTR(-ENOMEM);
280
281 if (symbol) {
282 tp->symbol = kstrdup(symbol, GFP_KERNEL);
283 if (!tp->symbol)
284 goto error;
285 }
4263565d
MH
286 if (!event)
287 goto error;
288 tp->call.name = kstrdup(event, GFP_KERNEL);
289 if (!tp->call.name)
290 goto error;
413d37d1
MH
291
292 INIT_LIST_HEAD(&tp->list);
293 return tp;
294error:
295 kfree(tp->symbol);
296 kfree(tp);
297 return ERR_PTR(-ENOMEM);
298}
299
300static void free_trace_probe(struct trace_probe *tp)
301{
302 int i;
303
304 for (i = 0; i < tp->nr_args; i++)
305 if (tp->args[i].func == fetch_symbol)
306 free_symbol_cache(tp->args[i].data);
307 else if (tp->args[i].func == fetch_indirect)
308 free_indirect_fetch_data(tp->args[i].data);
309
310 kfree(tp->call.name);
311 kfree(tp->symbol);
312 kfree(tp);
313}
314
315static struct trace_probe *find_probe_event(const char *event)
316{
317 struct trace_probe *tp;
318
319 list_for_each_entry(tp, &probe_list, list)
4263565d 320 if (!strcmp(tp->call.name, event))
413d37d1
MH
321 return tp;
322 return NULL;
323}
324
325static void __unregister_trace_probe(struct trace_probe *tp)
326{
327 if (probe_is_return(tp))
328 unregister_kretprobe(&tp->rp);
329 else
330 unregister_kprobe(&tp->kp);
331}
332
333/* Unregister a trace_probe and probe_event: call with locking probe_lock */
334static void unregister_trace_probe(struct trace_probe *tp)
335{
4263565d 336 unregister_probe_event(tp);
413d37d1
MH
337 __unregister_trace_probe(tp);
338 list_del(&tp->list);
339}
340
341/* Register a trace_probe and probe_event */
342static int register_trace_probe(struct trace_probe *tp)
343{
344 struct trace_probe *old_tp;
345 int ret;
346
347 mutex_lock(&probe_lock);
348
349 if (probe_is_return(tp))
350 ret = register_kretprobe(&tp->rp);
351 else
352 ret = register_kprobe(&tp->kp);
353
354 if (ret) {
355 pr_warning("Could not insert probe(%d)\n", ret);
356 if (ret == -EILSEQ) {
357 pr_warning("Probing address(0x%p) is not an "
358 "instruction boundary.\n",
359 probe_address(tp));
360 ret = -EINVAL;
361 }
362 goto end;
363 }
364 /* register as an event */
4263565d
MH
365 old_tp = find_probe_event(tp->call.name);
366 if (old_tp) {
367 /* delete old event */
368 unregister_trace_probe(old_tp);
369 free_trace_probe(old_tp);
370 }
371 ret = register_probe_event(tp);
372 if (ret) {
373 pr_warning("Faild to register probe event(%d)\n", ret);
374 __unregister_trace_probe(tp);
413d37d1
MH
375 }
376 list_add_tail(&tp->list, &probe_list);
377end:
378 mutex_unlock(&probe_lock);
379 return ret;
380}
381
382/* Split symbol and offset. */
383static int split_symbol_offset(char *symbol, long *offset)
384{
385 char *tmp;
386 int ret;
387
388 if (!offset)
389 return -EINVAL;
390
391 tmp = strchr(symbol, '+');
392 if (!tmp)
393 tmp = strchr(symbol, '-');
394
395 if (tmp) {
396 /* skip sign because strict_strtol doesn't accept '+' */
397 ret = strict_strtol(tmp + 1, 0, offset);
398 if (ret)
399 return ret;
400 if (*tmp == '-')
401 *offset = -(*offset);
402 *tmp = '\0';
403 } else
404 *offset = 0;
405 return 0;
406}
407
408#define PARAM_MAX_ARGS 16
409#define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long))
410
411static int parse_trace_arg(char *arg, struct fetch_func *ff, int is_return)
412{
413 int ret = 0;
414 unsigned long param;
415 long offset;
416 char *tmp;
417
418 switch (arg[0]) {
419 case 'a': /* argument */
420 ret = strict_strtoul(arg + 1, 10, &param);
421 if (ret || param > PARAM_MAX_ARGS)
422 ret = -EINVAL;
423 else {
424 ff->func = fetch_argument;
425 ff->data = (void *)param;
426 }
427 break;
428 case 'r': /* retval or retaddr */
429 if (is_return && arg[1] == 'v') {
430 ff->func = fetch_retvalue;
431 ff->data = NULL;
432 } else if (is_return && arg[1] == 'a') {
433 ff->func = fetch_ip;
434 ff->data = NULL;
435 } else
436 ret = -EINVAL;
437 break;
438 case '%': /* named register */
439 ret = regs_query_register_offset(arg + 1);
440 if (ret >= 0) {
441 ff->func = fetch_register;
442 ff->data = (void *)(unsigned long)ret;
443 ret = 0;
444 }
445 break;
446 case 's': /* stack */
447 if (arg[1] == 'a') {
448 ff->func = fetch_stack_address;
449 ff->data = NULL;
450 } else {
451 ret = strict_strtoul(arg + 1, 10, &param);
452 if (ret || param > PARAM_MAX_STACK)
453 ret = -EINVAL;
454 else {
455 ff->func = fetch_stack;
456 ff->data = (void *)param;
457 }
458 }
459 break;
460 case '@': /* memory or symbol */
461 if (isdigit(arg[1])) {
462 ret = strict_strtoul(arg + 1, 0, &param);
463 if (ret)
464 break;
465 ff->func = fetch_memory;
466 ff->data = (void *)param;
467 } else {
468 ret = split_symbol_offset(arg + 1, &offset);
469 if (ret)
470 break;
471 ff->data = alloc_symbol_cache(arg + 1,
472 offset);
473 if (ff->data)
474 ff->func = fetch_symbol;
475 else
476 ret = -EINVAL;
477 }
478 break;
479 case '+': /* indirect memory */
480 case '-':
481 tmp = strchr(arg, '(');
482 if (!tmp) {
483 ret = -EINVAL;
484 break;
485 }
486 *tmp = '\0';
487 ret = strict_strtol(arg + 1, 0, &offset);
488 if (ret)
489 break;
490 if (arg[0] == '-')
491 offset = -offset;
492 arg = tmp + 1;
493 tmp = strrchr(arg, ')');
494 if (tmp) {
495 struct indirect_fetch_data *id;
496 *tmp = '\0';
497 id = kzalloc(sizeof(struct indirect_fetch_data),
498 GFP_KERNEL);
499 if (!id)
500 return -ENOMEM;
501 id->offset = offset;
502 ret = parse_trace_arg(arg, &id->orig, is_return);
503 if (ret)
504 kfree(id);
505 else {
506 ff->func = fetch_indirect;
507 ff->data = (void *)id;
508 }
509 } else
510 ret = -EINVAL;
511 break;
512 default:
513 /* TODO: support custom handler */
514 ret = -EINVAL;
515 }
516 return ret;
517}
518
519static int create_trace_probe(int argc, char **argv)
520{
521 /*
522 * Argument syntax:
523 * - Add kprobe: p[:EVENT] SYMBOL[+OFFS|-OFFS]|ADDRESS [FETCHARGS]
524 * - Add kretprobe: r[:EVENT] SYMBOL[+0] [FETCHARGS]
525 * Fetch args:
526 * aN : fetch Nth of function argument. (N:0-)
527 * rv : fetch return value
528 * ra : fetch return address
529 * sa : fetch stack address
530 * sN : fetch Nth of stack (N:0-)
531 * @ADDR : fetch memory at ADDR (ADDR should be in kernel)
532 * @SYM[+|-offs] : fetch memory at SYM +|- offs (SYM is a data symbol)
533 * %REG : fetch register REG
534 * Indirect memory fetch:
535 * +|-offs(ARG) : fetch memory at ARG +|- offs address.
536 */
537 struct trace_probe *tp;
538 struct kprobe *kp;
539 int i, ret = 0;
540 int is_return = 0;
541 char *symbol = NULL, *event = NULL;
542 long offset = 0;
543 void *addr = NULL;
544
545 if (argc < 2)
546 return -EINVAL;
547
548 if (argv[0][0] == 'p')
549 is_return = 0;
550 else if (argv[0][0] == 'r')
551 is_return = 1;
552 else
553 return -EINVAL;
554
555 if (argv[0][1] == ':') {
556 event = &argv[0][2];
557 if (strlen(event) == 0) {
558 pr_info("Event name is not specifiled\n");
559 return -EINVAL;
560 }
561 }
562
563 if (isdigit(argv[1][0])) {
564 if (is_return)
565 return -EINVAL;
566 /* an address specified */
567 ret = strict_strtoul(&argv[0][2], 0, (unsigned long *)&addr);
568 if (ret)
569 return ret;
570 } else {
571 /* a symbol specified */
572 symbol = argv[1];
573 /* TODO: support .init module functions */
574 ret = split_symbol_offset(symbol, &offset);
575 if (ret)
576 return ret;
577 if (offset && is_return)
578 return -EINVAL;
579 }
a82378d8 580 argc -= 2; argv += 2;
413d37d1
MH
581
582 /* setup a probe */
4263565d
MH
583 if (!event) {
584 /* Make a new event name */
585 char buf[MAX_EVENT_NAME_LEN];
586 if (symbol)
587 snprintf(buf, MAX_EVENT_NAME_LEN, "%c@%s%+ld",
588 is_return ? 'r' : 'p', symbol, offset);
589 else
590 snprintf(buf, MAX_EVENT_NAME_LEN, "%c@0x%p",
591 is_return ? 'r' : 'p', addr);
592 tp = alloc_trace_probe(symbol, buf, argc);
593 } else
594 tp = alloc_trace_probe(symbol, event, argc);
413d37d1
MH
595 if (IS_ERR(tp))
596 return PTR_ERR(tp);
597
598 if (is_return) {
599 kp = &tp->rp.kp;
600 tp->rp.handler = kretprobe_trace_func;
601 } else {
602 kp = &tp->kp;
603 tp->kp.pre_handler = kprobe_trace_func;
604 }
605
606 if (tp->symbol) {
607 kp->symbol_name = tp->symbol;
608 kp->offset = offset;
609 } else
610 kp->addr = addr;
611
612 /* parse arguments */
a82378d8
MH
613 ret = 0;
614 for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
413d37d1
MH
615 if (strlen(argv[i]) > MAX_ARGSTR_LEN) {
616 pr_info("Argument%d(%s) is too long.\n", i, argv[i]);
617 ret = -ENOSPC;
618 goto error;
619 }
620 ret = parse_trace_arg(argv[i], &tp->args[i], is_return);
621 if (ret)
622 goto error;
623 }
624 tp->nr_args = i;
625
626 ret = register_trace_probe(tp);
627 if (ret)
628 goto error;
629 return 0;
630
631error:
632 free_trace_probe(tp);
633 return ret;
634}
635
636static void cleanup_all_probes(void)
637{
638 struct trace_probe *tp;
639
640 mutex_lock(&probe_lock);
641 /* TODO: Use batch unregistration */
642 while (!list_empty(&probe_list)) {
643 tp = list_entry(probe_list.next, struct trace_probe, list);
644 unregister_trace_probe(tp);
645 free_trace_probe(tp);
646 }
647 mutex_unlock(&probe_lock);
648}
649
650
651/* Probes listing interfaces */
652static void *probes_seq_start(struct seq_file *m, loff_t *pos)
653{
654 mutex_lock(&probe_lock);
655 return seq_list_start(&probe_list, *pos);
656}
657
658static void *probes_seq_next(struct seq_file *m, void *v, loff_t *pos)
659{
660 return seq_list_next(v, &probe_list, pos);
661}
662
663static void probes_seq_stop(struct seq_file *m, void *v)
664{
665 mutex_unlock(&probe_lock);
666}
667
668static int probes_seq_show(struct seq_file *m, void *v)
669{
670 struct trace_probe *tp = v;
671 int i, ret;
672 char buf[MAX_ARGSTR_LEN + 1];
673
674 seq_printf(m, "%c", probe_is_return(tp) ? 'r' : 'p');
4263565d 675 seq_printf(m, ":%s", tp->call.name);
413d37d1
MH
676
677 if (tp->symbol)
678 seq_printf(m, " %s%+ld", probe_symbol(tp), probe_offset(tp));
679 else
680 seq_printf(m, " 0x%p", probe_address(tp));
681
682 for (i = 0; i < tp->nr_args; i++) {
683 ret = trace_arg_string(buf, MAX_ARGSTR_LEN, &tp->args[i]);
684 if (ret < 0) {
685 pr_warning("Argument%d decoding error(%d).\n", i, ret);
686 return ret;
687 }
688 seq_printf(m, " %s", buf);
689 }
690 seq_printf(m, "\n");
691 return 0;
692}
693
694static const struct seq_operations probes_seq_op = {
695 .start = probes_seq_start,
696 .next = probes_seq_next,
697 .stop = probes_seq_stop,
698 .show = probes_seq_show
699};
700
701static int probes_open(struct inode *inode, struct file *file)
702{
703 if ((file->f_mode & FMODE_WRITE) &&
704 (file->f_flags & O_TRUNC))
705 cleanup_all_probes();
706
707 return seq_open(file, &probes_seq_op);
708}
709
710static int command_trace_probe(const char *buf)
711{
712 char **argv;
713 int argc = 0, ret = 0;
714
715 argv = argv_split(GFP_KERNEL, buf, &argc);
716 if (!argv)
717 return -ENOMEM;
718
719 if (argc)
720 ret = create_trace_probe(argc, argv);
721
722 argv_free(argv);
723 return ret;
724}
725
726#define WRITE_BUFSIZE 128
727
728static ssize_t probes_write(struct file *file, const char __user *buffer,
729 size_t count, loff_t *ppos)
730{
731 char *kbuf, *tmp;
732 int ret;
733 size_t done;
734 size_t size;
735
736 kbuf = kmalloc(WRITE_BUFSIZE, GFP_KERNEL);
737 if (!kbuf)
738 return -ENOMEM;
739
740 ret = done = 0;
741 while (done < count) {
742 size = count - done;
743 if (size >= WRITE_BUFSIZE)
744 size = WRITE_BUFSIZE - 1;
745 if (copy_from_user(kbuf, buffer + done, size)) {
746 ret = -EFAULT;
747 goto out;
748 }
749 kbuf[size] = '\0';
750 tmp = strchr(kbuf, '\n');
751 if (tmp) {
752 *tmp = '\0';
753 size = tmp - kbuf + 1;
754 } else if (done + size < count) {
755 pr_warning("Line length is too long: "
756 "Should be less than %d.", WRITE_BUFSIZE);
757 ret = -EINVAL;
758 goto out;
759 }
760 done += size;
761 /* Remove comments */
762 tmp = strchr(kbuf, '#');
763 if (tmp)
764 *tmp = '\0';
765
766 ret = command_trace_probe(kbuf);
767 if (ret)
768 goto out;
769 }
770 ret = done;
771out:
772 kfree(kbuf);
773 return ret;
774}
775
776static const struct file_operations kprobe_events_ops = {
777 .owner = THIS_MODULE,
778 .open = probes_open,
779 .read = seq_read,
780 .llseek = seq_lseek,
781 .release = seq_release,
782 .write = probes_write,
783};
784
cd7e7bd5
MH
785/* Probes profiling interfaces */
786static int probes_profile_seq_show(struct seq_file *m, void *v)
787{
788 struct trace_probe *tp = v;
789
790 seq_printf(m, " %-44s %15lu %15lu\n", tp->call.name, tp->nhit,
791 probe_is_return(tp) ? tp->rp.kp.nmissed : tp->kp.nmissed);
792
793 return 0;
794}
795
796static const struct seq_operations profile_seq_op = {
797 .start = probes_seq_start,
798 .next = probes_seq_next,
799 .stop = probes_seq_stop,
800 .show = probes_profile_seq_show
801};
802
803static int profile_open(struct inode *inode, struct file *file)
804{
805 return seq_open(file, &profile_seq_op);
806}
807
808static const struct file_operations kprobe_profile_ops = {
809 .owner = THIS_MODULE,
810 .open = profile_open,
811 .read = seq_read,
812 .llseek = seq_lseek,
813 .release = seq_release,
814};
815
413d37d1
MH
816/* Kprobe handler */
817static __kprobes int kprobe_trace_func(struct kprobe *kp, struct pt_regs *regs)
818{
819 struct trace_probe *tp = container_of(kp, struct trace_probe, kp);
820 struct kprobe_trace_entry *entry;
821 struct ring_buffer_event *event;
822 int size, i, pc;
823 unsigned long irq_flags;
4263565d 824 struct ftrace_event_call *call = &tp->call;
413d37d1 825
cd7e7bd5
MH
826 tp->nhit++;
827
413d37d1
MH
828 local_save_flags(irq_flags);
829 pc = preempt_count();
830
831 size = SIZEOF_KPROBE_TRACE_ENTRY(tp->nr_args);
832
ff50d991 833 event = trace_current_buffer_lock_reserve(call->id, size,
413d37d1
MH
834 irq_flags, pc);
835 if (!event)
836 return 0;
837
838 entry = ring_buffer_event_data(event);
839 entry->nargs = tp->nr_args;
840 entry->ip = (unsigned long)kp->addr;
841 for (i = 0; i < tp->nr_args; i++)
842 entry->args[i] = call_fetch(&tp->args[i], regs);
843
844 if (!filter_current_check_discard(call, entry, event))
845 trace_nowake_buffer_unlock_commit(event, irq_flags, pc);
846 return 0;
847}
848
849/* Kretprobe handler */
850static __kprobes int kretprobe_trace_func(struct kretprobe_instance *ri,
851 struct pt_regs *regs)
852{
853 struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
854 struct kretprobe_trace_entry *entry;
855 struct ring_buffer_event *event;
856 int size, i, pc;
857 unsigned long irq_flags;
4263565d 858 struct ftrace_event_call *call = &tp->call;
413d37d1
MH
859
860 local_save_flags(irq_flags);
861 pc = preempt_count();
862
863 size = SIZEOF_KRETPROBE_TRACE_ENTRY(tp->nr_args);
864
ff50d991 865 event = trace_current_buffer_lock_reserve(call->id, size,
413d37d1
MH
866 irq_flags, pc);
867 if (!event)
868 return 0;
869
870 entry = ring_buffer_event_data(event);
871 entry->nargs = tp->nr_args;
872 entry->func = (unsigned long)probe_address(tp);
873 entry->ret_ip = (unsigned long)ri->ret_addr;
874 for (i = 0; i < tp->nr_args; i++)
875 entry->args[i] = call_fetch(&tp->args[i], regs);
876
877 if (!filter_current_check_discard(call, entry, event))
878 trace_nowake_buffer_unlock_commit(event, irq_flags, pc);
879
880 return 0;
881}
882
883/* Event entry printers */
884enum print_line_t
885print_kprobe_event(struct trace_iterator *iter, int flags)
886{
887 struct kprobe_trace_entry *field;
888 struct trace_seq *s = &iter->seq;
889 int i;
890
ff50d991 891 field = (struct kprobe_trace_entry *)iter->ent;
413d37d1
MH
892
893 if (!seq_print_ip_sym(s, field->ip, flags | TRACE_ITER_SYM_OFFSET))
894 goto partial;
895
896 if (!trace_seq_puts(s, ":"))
897 goto partial;
898
899 for (i = 0; i < field->nargs; i++)
900 if (!trace_seq_printf(s, " 0x%lx", field->args[i]))
901 goto partial;
902
903 if (!trace_seq_puts(s, "\n"))
904 goto partial;
905
906 return TRACE_TYPE_HANDLED;
907partial:
908 return TRACE_TYPE_PARTIAL_LINE;
909}
910
911enum print_line_t
912print_kretprobe_event(struct trace_iterator *iter, int flags)
913{
914 struct kretprobe_trace_entry *field;
915 struct trace_seq *s = &iter->seq;
916 int i;
917
ff50d991 918 field = (struct kretprobe_trace_entry *)iter->ent;
413d37d1
MH
919
920 if (!seq_print_ip_sym(s, field->ret_ip, flags | TRACE_ITER_SYM_OFFSET))
921 goto partial;
922
923 if (!trace_seq_puts(s, " <- "))
924 goto partial;
925
926 if (!seq_print_ip_sym(s, field->func, flags & ~TRACE_ITER_SYM_OFFSET))
927 goto partial;
928
929 if (!trace_seq_puts(s, ":"))
930 goto partial;
931
932 for (i = 0; i < field->nargs; i++)
933 if (!trace_seq_printf(s, " 0x%lx", field->args[i]))
934 goto partial;
935
936 if (!trace_seq_puts(s, "\n"))
937 goto partial;
938
939 return TRACE_TYPE_HANDLED;
940partial:
941 return TRACE_TYPE_PARTIAL_LINE;
942}
943
413d37d1
MH
944static int probe_event_enable(struct ftrace_event_call *call)
945{
946 struct trace_probe *tp = (struct trace_probe *)call->data;
947
948 if (probe_is_return(tp))
949 return enable_kretprobe(&tp->rp);
950 else
951 return enable_kprobe(&tp->kp);
952}
953
954static void probe_event_disable(struct ftrace_event_call *call)
955{
956 struct trace_probe *tp = (struct trace_probe *)call->data;
957
958 if (probe_is_return(tp))
959 disable_kretprobe(&tp->rp);
960 else
961 disable_kprobe(&tp->kp);
962}
963
964static int probe_event_raw_init(struct ftrace_event_call *event_call)
965{
966 INIT_LIST_HEAD(&event_call->fields);
967 init_preds(event_call);
968 return 0;
969}
970
971#undef DEFINE_FIELD
972#define DEFINE_FIELD(type, item, name, is_signed) \
973 do { \
974 ret = trace_define_field(event_call, #type, name, \
975 offsetof(typeof(field), item), \
976 sizeof(field.item), is_signed, \
977 FILTER_OTHER); \
978 if (ret) \
979 return ret; \
980 } while (0)
981
982static int kprobe_event_define_fields(struct ftrace_event_call *event_call)
983{
984 int ret, i;
985 struct kprobe_trace_entry field;
986 char buf[MAX_ARGSTR_LEN + 1];
987 struct trace_probe *tp = (struct trace_probe *)event_call->data;
988
989 ret = trace_define_common_fields(event_call);
990 if (!ret)
991 return ret;
992
993 DEFINE_FIELD(unsigned long, ip, "ip", 0);
994 DEFINE_FIELD(int, nargs, "nargs", 1);
995 for (i = 0; i < tp->nr_args; i++) {
996 /* Set argN as a field */
997 sprintf(buf, "arg%d", i);
998 DEFINE_FIELD(unsigned long, args[i], buf, 0);
999 /* Set argument string as an alias field */
1000 ret = trace_arg_string(buf, MAX_ARGSTR_LEN, &tp->args[i]);
1001 if (ret < 0)
1002 return ret;
1003 DEFINE_FIELD(unsigned long, args[i], buf, 0);
1004 }
1005 return 0;
1006}
1007
1008static int kretprobe_event_define_fields(struct ftrace_event_call *event_call)
1009{
1010 int ret, i;
1011 struct kretprobe_trace_entry field;
1012 char buf[MAX_ARGSTR_LEN + 1];
1013 struct trace_probe *tp = (struct trace_probe *)event_call->data;
1014
1015 ret = trace_define_common_fields(event_call);
1016 if (!ret)
1017 return ret;
1018
1019 DEFINE_FIELD(unsigned long, func, "func", 0);
1020 DEFINE_FIELD(unsigned long, ret_ip, "ret_ip", 0);
1021 DEFINE_FIELD(int, nargs, "nargs", 1);
1022 for (i = 0; i < tp->nr_args; i++) {
1023 /* Set argN as a field */
1024 sprintf(buf, "arg%d", i);
1025 DEFINE_FIELD(unsigned long, args[i], buf, 0);
1026 /* Set argument string as an alias field */
1027 ret = trace_arg_string(buf, MAX_ARGSTR_LEN, &tp->args[i]);
1028 if (ret < 0)
1029 return ret;
1030 DEFINE_FIELD(unsigned long, args[i], buf, 0);
1031 }
1032 return 0;
1033}
1034
1035static int __probe_event_show_format(struct trace_seq *s,
1036 struct trace_probe *tp, const char *fmt,
1037 const char *arg)
1038{
1039 int i, ret;
1040 char buf[MAX_ARGSTR_LEN + 1];
1041
1042 /* Show aliases */
1043 for (i = 0; i < tp->nr_args; i++) {
1044 ret = trace_arg_string(buf, MAX_ARGSTR_LEN, &tp->args[i]);
1045 if (ret < 0)
1046 return ret;
1047 if (!trace_seq_printf(s, "\talias: %s;\toriginal: arg%d;\n",
1048 buf, i))
1049 return 0;
1050 }
1051 /* Show format */
1052 if (!trace_seq_printf(s, "\nprint fmt: \"%s", fmt))
1053 return 0;
1054
1055 for (i = 0; i < tp->nr_args; i++)
1056 if (!trace_seq_puts(s, " 0x%lx"))
1057 return 0;
1058
1059 if (!trace_seq_printf(s, "\", %s", arg))
1060 return 0;
1061
1062 for (i = 0; i < tp->nr_args; i++)
1063 if (!trace_seq_printf(s, ", arg%d", i))
1064 return 0;
1065
1066 return trace_seq_puts(s, "\n");
1067}
1068
1069#undef SHOW_FIELD
1070#define SHOW_FIELD(type, item, name) \
1071 do { \
1072 ret = trace_seq_printf(s, "\tfield: " #type " %s;\t" \
1073 "offset:%u;tsize:%u;\n", name, \
1074 (unsigned int)offsetof(typeof(field), item),\
1075 (unsigned int)sizeof(type)); \
1076 if (!ret) \
1077 return 0; \
1078 } while (0)
1079
1080static int kprobe_event_show_format(struct ftrace_event_call *call,
1081 struct trace_seq *s)
1082{
1083 struct kprobe_trace_entry field __attribute__((unused));
1084 int ret, i;
1085 char buf[8];
1086 struct trace_probe *tp = (struct trace_probe *)call->data;
1087
1088 SHOW_FIELD(unsigned long, ip, "ip");
1089 SHOW_FIELD(int, nargs, "nargs");
1090
1091 /* Show fields */
1092 for (i = 0; i < tp->nr_args; i++) {
1093 sprintf(buf, "arg%d", i);
1094 SHOW_FIELD(unsigned long, args[i], buf);
1095 }
1096 trace_seq_puts(s, "\n");
1097
1098 return __probe_event_show_format(s, tp, "%lx:", "ip");
1099}
1100
1101static int kretprobe_event_show_format(struct ftrace_event_call *call,
1102 struct trace_seq *s)
1103{
1104 struct kretprobe_trace_entry field __attribute__((unused));
1105 int ret, i;
1106 char buf[8];
1107 struct trace_probe *tp = (struct trace_probe *)call->data;
1108
1109 SHOW_FIELD(unsigned long, func, "func");
1110 SHOW_FIELD(unsigned long, ret_ip, "ret_ip");
1111 SHOW_FIELD(int, nargs, "nargs");
1112
1113 /* Show fields */
1114 for (i = 0; i < tp->nr_args; i++) {
1115 sprintf(buf, "arg%d", i);
1116 SHOW_FIELD(unsigned long, args[i], buf);
1117 }
1118 trace_seq_puts(s, "\n");
1119
1120 return __probe_event_show_format(s, tp, "%lx <- %lx:",
1121 "func, ret_ip");
1122}
1123
1124static int register_probe_event(struct trace_probe *tp)
1125{
1126 struct ftrace_event_call *call = &tp->call;
1127 int ret;
1128
1129 /* Initialize ftrace_event_call */
1130 call->system = "kprobes";
1131 if (probe_is_return(tp)) {
ff50d991 1132 tp->event.trace = print_kretprobe_event;
413d37d1
MH
1133 call->raw_init = probe_event_raw_init;
1134 call->show_format = kretprobe_event_show_format;
1135 call->define_fields = kretprobe_event_define_fields;
1136 } else {
ff50d991 1137 tp->event.trace = print_kprobe_event;
413d37d1
MH
1138 call->raw_init = probe_event_raw_init;
1139 call->show_format = kprobe_event_show_format;
1140 call->define_fields = kprobe_event_define_fields;
1141 }
ff50d991
MH
1142 call->event = &tp->event;
1143 call->id = register_ftrace_event(&tp->event);
1144 if (!call->id)
1145 return -ENODEV;
413d37d1
MH
1146 call->enabled = 1;
1147 call->regfunc = probe_event_enable;
1148 call->unregfunc = probe_event_disable;
1149 call->data = tp;
1150 ret = trace_add_event_call(call);
ff50d991 1151 if (ret) {
413d37d1 1152 pr_info("Failed to register kprobe event: %s\n", call->name);
ff50d991
MH
1153 unregister_ftrace_event(&tp->event);
1154 }
413d37d1
MH
1155 return ret;
1156}
1157
1158static void unregister_probe_event(struct trace_probe *tp)
1159{
ff50d991 1160 /* tp->event is unregistered in trace_remove_event_call() */
413d37d1
MH
1161 trace_remove_event_call(&tp->call);
1162}
1163
1164/* Make a debugfs interface for controling probe points */
1165static __init int init_kprobe_trace(void)
1166{
1167 struct dentry *d_tracer;
1168 struct dentry *entry;
413d37d1
MH
1169
1170 d_tracer = tracing_init_dentry();
1171 if (!d_tracer)
1172 return 0;
1173
1174 entry = debugfs_create_file("kprobe_events", 0644, d_tracer,
1175 NULL, &kprobe_events_ops);
1176
cd7e7bd5 1177 /* Event list interface */
413d37d1
MH
1178 if (!entry)
1179 pr_warning("Could not create debugfs "
1180 "'kprobe_events' entry\n");
cd7e7bd5
MH
1181
1182 /* Profile interface */
1183 entry = debugfs_create_file("kprobe_profile", 0444, d_tracer,
1184 NULL, &kprobe_profile_ops);
1185
1186 if (!entry)
1187 pr_warning("Could not create debugfs "
1188 "'kprobe_profile' entry\n");
413d37d1
MH
1189 return 0;
1190}
1191fs_initcall(init_kprobe_trace);
1192
1193
1194#ifdef CONFIG_FTRACE_STARTUP_TEST
1195
1196static int kprobe_trace_selftest_target(int a1, int a2, int a3,
1197 int a4, int a5, int a6)
1198{
1199 return a1 + a2 + a3 + a4 + a5 + a6;
1200}
1201
1202static __init int kprobe_trace_self_tests_init(void)
1203{
1204 int ret;
1205 int (*target)(int, int, int, int, int, int);
1206
1207 target = kprobe_trace_selftest_target;
1208
1209 pr_info("Testing kprobe tracing: ");
1210
1211 ret = command_trace_probe("p:testprobe kprobe_trace_selftest_target "
1212 "a1 a2 a3 a4 a5 a6");
1213 if (WARN_ON_ONCE(ret))
1214 pr_warning("error enabling function entry\n");
1215
1216 ret = command_trace_probe("r:testprobe2 kprobe_trace_selftest_target "
1217 "ra rv");
1218 if (WARN_ON_ONCE(ret))
1219 pr_warning("error enabling function return\n");
1220
1221 ret = target(1, 2, 3, 4, 5, 6);
1222
1223 cleanup_all_probes();
1224
1225 pr_cont("OK\n");
1226 return 0;
1227}
1228
1229late_initcall(kprobe_trace_self_tests_init);
1230
1231#endif
This page took 0.068539 seconds and 5 git commands to generate.