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