2 * kprobe based kernel tracer
4 * Created by Masami Hiramatsu <mhiramat@redhat.com>
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.
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.
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
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>
33 #include "trace_output.h"
35 #define MAX_TRACE_ARGS 128
36 #define MAX_ARGSTR_LEN 63
38 /* currently, trace_kprobe only supports X86. */
41 unsigned long (*func)(struct pt_regs *, void *);
45 static __kprobes unsigned long call_fetch(struct fetch_func *f,
48 return f->func(regs, f->data);
52 static __kprobes unsigned long fetch_register(struct pt_regs *regs,
55 return regs_get_register(regs, (unsigned int)((unsigned long)offset));
58 static __kprobes unsigned long fetch_stack(struct pt_regs *regs,
61 return regs_get_kernel_stack_nth(regs,
62 (unsigned int)((unsigned long)num));
65 static __kprobes unsigned long fetch_memory(struct pt_regs *regs, void *addr)
69 if (probe_kernel_address(addr, retval))
74 static __kprobes unsigned long fetch_argument(struct pt_regs *regs, void *num)
76 return regs_get_argument_nth(regs, (unsigned int)((unsigned long)num));
79 static __kprobes unsigned long fetch_retvalue(struct pt_regs *regs,
82 return regs_return_value(regs);
85 static __kprobes unsigned long fetch_ip(struct pt_regs *regs, void *dummy)
87 return instruction_pointer(regs);
90 static __kprobes unsigned long fetch_stack_address(struct pt_regs *regs,
93 return kernel_stack_pointer(regs);
96 /* Memory fetching by symbol */
103 static unsigned long update_symbol_cache(struct symbol_cache *sc)
105 sc->addr = (unsigned long)kallsyms_lookup_name(sc->symbol);
107 sc->addr += sc->offset;
111 static void free_symbol_cache(struct symbol_cache *sc)
117 static struct symbol_cache *alloc_symbol_cache(const char *sym, long offset)
119 struct symbol_cache *sc;
121 if (!sym || strlen(sym) == 0)
123 sc = kzalloc(sizeof(struct symbol_cache), GFP_KERNEL);
127 sc->symbol = kstrdup(sym, GFP_KERNEL);
134 update_symbol_cache(sc);
138 static __kprobes unsigned long fetch_symbol(struct pt_regs *regs, void *data)
140 struct symbol_cache *sc = data;
143 return fetch_memory(regs, (void *)sc->addr);
148 /* Special indirect memory access interface */
149 struct indirect_fetch_data {
150 struct fetch_func orig;
154 static __kprobes unsigned long fetch_indirect(struct pt_regs *regs, void *data)
156 struct indirect_fetch_data *ind = data;
159 addr = call_fetch(&ind->orig, regs);
162 return fetch_memory(regs, (void *)addr);
167 static __kprobes void free_indirect_fetch_data(struct indirect_fetch_data *data)
169 if (data->orig.func == fetch_indirect)
170 free_indirect_fetch_data(data->orig.data);
171 else if (data->orig.func == fetch_symbol)
172 free_symbol_cache(data->orig.data);
181 struct list_head list;
186 const char *symbol; /* symbol name */
187 struct ftrace_event_call call;
188 unsigned int nr_args;
189 struct fetch_func args[];
192 #define SIZEOF_TRACE_PROBE(n) \
193 (offsetof(struct trace_probe, args) + \
194 (sizeof(struct fetch_func) * (n)))
196 static int kprobe_trace_func(struct kprobe *kp, struct pt_regs *regs);
197 static int kretprobe_trace_func(struct kretprobe_instance *ri,
198 struct pt_regs *regs);
200 static __kprobes int probe_is_return(struct trace_probe *tp)
202 return (tp->rp.handler == kretprobe_trace_func);
205 static __kprobes const char *probe_symbol(struct trace_probe *tp)
207 return tp->symbol ? tp->symbol : "unknown";
210 static __kprobes long probe_offset(struct trace_probe *tp)
212 return (probe_is_return(tp)) ? tp->rp.kp.offset : tp->kp.offset;
215 static __kprobes void *probe_address(struct trace_probe *tp)
217 return (probe_is_return(tp)) ? tp->rp.kp.addr : tp->kp.addr;
220 static int trace_arg_string(char *buf, size_t n, struct fetch_func *ff)
224 if (ff->func == fetch_argument)
225 ret = snprintf(buf, n, "a%lu", (unsigned long)ff->data);
226 else if (ff->func == fetch_register) {
228 name = regs_query_register_name((unsigned int)((long)ff->data));
229 ret = snprintf(buf, n, "%%%s", name);
230 } else if (ff->func == fetch_stack)
231 ret = snprintf(buf, n, "s%lu", (unsigned long)ff->data);
232 else if (ff->func == fetch_memory)
233 ret = snprintf(buf, n, "@0x%p", ff->data);
234 else if (ff->func == fetch_symbol) {
235 struct symbol_cache *sc = ff->data;
236 ret = snprintf(buf, n, "@%s%+ld", sc->symbol, sc->offset);
237 } else if (ff->func == fetch_retvalue)
238 ret = snprintf(buf, n, "rv");
239 else if (ff->func == fetch_ip)
240 ret = snprintf(buf, n, "ra");
241 else if (ff->func == fetch_stack_address)
242 ret = snprintf(buf, n, "sa");
243 else if (ff->func == fetch_indirect) {
244 struct indirect_fetch_data *id = ff->data;
246 ret = snprintf(buf, n, "%+ld(", id->offset);
250 ret = trace_arg_string(buf + l, n - l, &id->orig);
254 ret = snprintf(buf + l, n - l, ")");
263 static int register_probe_event(struct trace_probe *tp);
264 static void unregister_probe_event(struct trace_probe *tp);
266 static DEFINE_MUTEX(probe_lock);
267 static LIST_HEAD(probe_list);
269 static struct trace_probe *alloc_trace_probe(const char *symbol,
270 const char *event, int nargs)
272 struct trace_probe *tp;
274 tp = kzalloc(SIZEOF_TRACE_PROBE(nargs), GFP_KERNEL);
276 return ERR_PTR(-ENOMEM);
279 tp->symbol = kstrdup(symbol, GFP_KERNEL);
284 tp->call.name = kstrdup(event, GFP_KERNEL);
289 INIT_LIST_HEAD(&tp->list);
294 return ERR_PTR(-ENOMEM);
297 static void free_trace_probe(struct trace_probe *tp)
301 for (i = 0; i < tp->nr_args; i++)
302 if (tp->args[i].func == fetch_symbol)
303 free_symbol_cache(tp->args[i].data);
304 else if (tp->args[i].func == fetch_indirect)
305 free_indirect_fetch_data(tp->args[i].data);
307 kfree(tp->call.name);
312 static struct trace_probe *find_probe_event(const char *event)
314 struct trace_probe *tp;
316 list_for_each_entry(tp, &probe_list, list)
317 if (tp->call.name && !strcmp(tp->call.name, event))
322 static void __unregister_trace_probe(struct trace_probe *tp)
324 if (probe_is_return(tp))
325 unregister_kretprobe(&tp->rp);
327 unregister_kprobe(&tp->kp);
330 /* Unregister a trace_probe and probe_event: call with locking probe_lock */
331 static void unregister_trace_probe(struct trace_probe *tp)
334 unregister_probe_event(tp);
335 __unregister_trace_probe(tp);
339 /* Register a trace_probe and probe_event */
340 static int register_trace_probe(struct trace_probe *tp)
342 struct trace_probe *old_tp;
345 mutex_lock(&probe_lock);
347 if (probe_is_return(tp))
348 ret = register_kretprobe(&tp->rp);
350 ret = register_kprobe(&tp->kp);
353 pr_warning("Could not insert probe(%d)\n", ret);
354 if (ret == -EILSEQ) {
355 pr_warning("Probing address(0x%p) is not an "
356 "instruction boundary.\n",
362 /* register as an event */
364 old_tp = find_probe_event(tp->call.name);
366 /* delete old event */
367 unregister_trace_probe(old_tp);
368 free_trace_probe(old_tp);
370 ret = register_probe_event(tp);
372 pr_warning("Faild to register probe event(%d)\n", ret);
373 __unregister_trace_probe(tp);
376 list_add_tail(&tp->list, &probe_list);
378 mutex_unlock(&probe_lock);
382 /* Split symbol and offset. */
383 static int split_symbol_offset(char *symbol, long *offset)
391 tmp = strchr(symbol, '+');
393 tmp = strchr(symbol, '-');
396 /* skip sign because strict_strtol doesn't accept '+' */
397 ret = strict_strtol(tmp + 1, 0, offset);
401 *offset = -(*offset);
408 #define PARAM_MAX_ARGS 16
409 #define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long))
411 static int parse_trace_arg(char *arg, struct fetch_func *ff, int is_return)
419 case 'a': /* argument */
420 ret = strict_strtoul(arg + 1, 10, ¶m);
421 if (ret || param > PARAM_MAX_ARGS)
424 ff->func = fetch_argument;
425 ff->data = (void *)param;
428 case 'r': /* retval or retaddr */
429 if (is_return && arg[1] == 'v') {
430 ff->func = fetch_retvalue;
432 } else if (is_return && arg[1] == 'a') {
438 case '%': /* named register */
439 ret = regs_query_register_offset(arg + 1);
441 ff->func = fetch_register;
442 ff->data = (void *)(unsigned long)ret;
446 case 's': /* stack */
448 ff->func = fetch_stack_address;
451 ret = strict_strtoul(arg + 1, 10, ¶m);
452 if (ret || param > PARAM_MAX_STACK)
455 ff->func = fetch_stack;
456 ff->data = (void *)param;
460 case '@': /* memory or symbol */
461 if (isdigit(arg[1])) {
462 ret = strict_strtoul(arg + 1, 0, ¶m);
465 ff->func = fetch_memory;
466 ff->data = (void *)param;
468 ret = split_symbol_offset(arg + 1, &offset);
471 ff->data = alloc_symbol_cache(arg + 1,
474 ff->func = fetch_symbol;
479 case '+': /* indirect memory */
481 tmp = strchr(arg, '(');
487 ret = strict_strtol(arg + 1, 0, &offset);
493 tmp = strrchr(arg, ')');
495 struct indirect_fetch_data *id;
497 id = kzalloc(sizeof(struct indirect_fetch_data),
502 ret = parse_trace_arg(arg, &id->orig, is_return);
506 ff->func = fetch_indirect;
507 ff->data = (void *)id;
513 /* TODO: support custom handler */
519 static int create_trace_probe(int argc, char **argv)
523 * - Add kprobe: p[:EVENT] SYMBOL[+OFFS|-OFFS]|ADDRESS [FETCHARGS]
524 * - Add kretprobe: r[:EVENT] SYMBOL[+0] [FETCHARGS]
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.
537 struct trace_probe *tp;
541 char *symbol = NULL, *event = NULL;
548 if (argv[0][0] == 'p')
550 else if (argv[0][0] == 'r')
555 if (argv[0][1] == ':') {
557 if (strlen(event) == 0) {
558 pr_info("Event name is not specifiled\n");
563 if (isdigit(argv[1][0])) {
566 /* an address specified */
567 ret = strict_strtoul(&argv[0][2], 0, (unsigned long *)&addr);
571 /* a symbol specified */
573 /* TODO: support .init module functions */
574 ret = split_symbol_offset(symbol, &offset);
577 if (offset && is_return)
580 argc -= 2; argv += 2;
583 tp = alloc_trace_probe(symbol, event, argc);
589 tp->rp.handler = kretprobe_trace_func;
592 tp->kp.pre_handler = kprobe_trace_func;
596 kp->symbol_name = tp->symbol;
601 /* parse arguments */
603 for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
604 if (strlen(argv[i]) > MAX_ARGSTR_LEN) {
605 pr_info("Argument%d(%s) is too long.\n", i, argv[i]);
609 ret = parse_trace_arg(argv[i], &tp->args[i], is_return);
615 ret = register_trace_probe(tp);
621 free_trace_probe(tp);
625 static void cleanup_all_probes(void)
627 struct trace_probe *tp;
629 mutex_lock(&probe_lock);
630 /* TODO: Use batch unregistration */
631 while (!list_empty(&probe_list)) {
632 tp = list_entry(probe_list.next, struct trace_probe, list);
633 unregister_trace_probe(tp);
634 free_trace_probe(tp);
636 mutex_unlock(&probe_lock);
640 /* Probes listing interfaces */
641 static void *probes_seq_start(struct seq_file *m, loff_t *pos)
643 mutex_lock(&probe_lock);
644 return seq_list_start(&probe_list, *pos);
647 static void *probes_seq_next(struct seq_file *m, void *v, loff_t *pos)
649 return seq_list_next(v, &probe_list, pos);
652 static void probes_seq_stop(struct seq_file *m, void *v)
654 mutex_unlock(&probe_lock);
657 static int probes_seq_show(struct seq_file *m, void *v)
659 struct trace_probe *tp = v;
661 char buf[MAX_ARGSTR_LEN + 1];
663 seq_printf(m, "%c", probe_is_return(tp) ? 'r' : 'p');
665 seq_printf(m, ":%s", tp->call.name);
668 seq_printf(m, " %s%+ld", probe_symbol(tp), probe_offset(tp));
670 seq_printf(m, " 0x%p", probe_address(tp));
672 for (i = 0; i < tp->nr_args; i++) {
673 ret = trace_arg_string(buf, MAX_ARGSTR_LEN, &tp->args[i]);
675 pr_warning("Argument%d decoding error(%d).\n", i, ret);
678 seq_printf(m, " %s", buf);
684 static const struct seq_operations probes_seq_op = {
685 .start = probes_seq_start,
686 .next = probes_seq_next,
687 .stop = probes_seq_stop,
688 .show = probes_seq_show
691 static int probes_open(struct inode *inode, struct file *file)
693 if ((file->f_mode & FMODE_WRITE) &&
694 (file->f_flags & O_TRUNC))
695 cleanup_all_probes();
697 return seq_open(file, &probes_seq_op);
700 static int command_trace_probe(const char *buf)
703 int argc = 0, ret = 0;
705 argv = argv_split(GFP_KERNEL, buf, &argc);
710 ret = create_trace_probe(argc, argv);
716 #define WRITE_BUFSIZE 128
718 static ssize_t probes_write(struct file *file, const char __user *buffer,
719 size_t count, loff_t *ppos)
726 kbuf = kmalloc(WRITE_BUFSIZE, GFP_KERNEL);
731 while (done < count) {
733 if (size >= WRITE_BUFSIZE)
734 size = WRITE_BUFSIZE - 1;
735 if (copy_from_user(kbuf, buffer + done, size)) {
740 tmp = strchr(kbuf, '\n');
743 size = tmp - kbuf + 1;
744 } else if (done + size < count) {
745 pr_warning("Line length is too long: "
746 "Should be less than %d.", WRITE_BUFSIZE);
751 /* Remove comments */
752 tmp = strchr(kbuf, '#');
756 ret = command_trace_probe(kbuf);
766 static const struct file_operations kprobe_events_ops = {
767 .owner = THIS_MODULE,
771 .release = seq_release,
772 .write = probes_write,
776 static __kprobes int kprobe_trace_func(struct kprobe *kp, struct pt_regs *regs)
778 struct trace_probe *tp = container_of(kp, struct trace_probe, kp);
779 struct kprobe_trace_entry *entry;
780 struct ring_buffer_event *event;
782 unsigned long irq_flags;
783 struct ftrace_event_call *call = &event_kprobe;
788 local_save_flags(irq_flags);
789 pc = preempt_count();
791 size = SIZEOF_KPROBE_TRACE_ENTRY(tp->nr_args);
793 event = trace_current_buffer_lock_reserve(TRACE_KPROBE, size,
798 entry = ring_buffer_event_data(event);
799 entry->nargs = tp->nr_args;
800 entry->ip = (unsigned long)kp->addr;
801 for (i = 0; i < tp->nr_args; i++)
802 entry->args[i] = call_fetch(&tp->args[i], regs);
804 if (!filter_current_check_discard(call, entry, event))
805 trace_nowake_buffer_unlock_commit(event, irq_flags, pc);
809 /* Kretprobe handler */
810 static __kprobes int kretprobe_trace_func(struct kretprobe_instance *ri,
811 struct pt_regs *regs)
813 struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
814 struct kretprobe_trace_entry *entry;
815 struct ring_buffer_event *event;
817 unsigned long irq_flags;
818 struct ftrace_event_call *call = &event_kretprobe;
823 local_save_flags(irq_flags);
824 pc = preempt_count();
826 size = SIZEOF_KRETPROBE_TRACE_ENTRY(tp->nr_args);
828 event = trace_current_buffer_lock_reserve(TRACE_KRETPROBE, size,
833 entry = ring_buffer_event_data(event);
834 entry->nargs = tp->nr_args;
835 entry->func = (unsigned long)probe_address(tp);
836 entry->ret_ip = (unsigned long)ri->ret_addr;
837 for (i = 0; i < tp->nr_args; i++)
838 entry->args[i] = call_fetch(&tp->args[i], regs);
840 if (!filter_current_check_discard(call, entry, event))
841 trace_nowake_buffer_unlock_commit(event, irq_flags, pc);
846 /* Event entry printers */
848 print_kprobe_event(struct trace_iterator *iter, int flags)
850 struct kprobe_trace_entry *field;
851 struct trace_seq *s = &iter->seq;
854 trace_assign_type(field, iter->ent);
856 if (!seq_print_ip_sym(s, field->ip, flags | TRACE_ITER_SYM_OFFSET))
859 if (!trace_seq_puts(s, ":"))
862 for (i = 0; i < field->nargs; i++)
863 if (!trace_seq_printf(s, " 0x%lx", field->args[i]))
866 if (!trace_seq_puts(s, "\n"))
869 return TRACE_TYPE_HANDLED;
871 return TRACE_TYPE_PARTIAL_LINE;
875 print_kretprobe_event(struct trace_iterator *iter, int flags)
877 struct kretprobe_trace_entry *field;
878 struct trace_seq *s = &iter->seq;
881 trace_assign_type(field, iter->ent);
883 if (!seq_print_ip_sym(s, field->ret_ip, flags | TRACE_ITER_SYM_OFFSET))
886 if (!trace_seq_puts(s, " <- "))
889 if (!seq_print_ip_sym(s, field->func, flags & ~TRACE_ITER_SYM_OFFSET))
892 if (!trace_seq_puts(s, ":"))
895 for (i = 0; i < field->nargs; i++)
896 if (!trace_seq_printf(s, " 0x%lx", field->args[i]))
899 if (!trace_seq_puts(s, "\n"))
902 return TRACE_TYPE_HANDLED;
904 return TRACE_TYPE_PARTIAL_LINE;
907 static struct trace_event kprobe_trace_event = {
908 .type = TRACE_KPROBE,
909 .trace = print_kprobe_event,
912 static struct trace_event kretprobe_trace_event = {
913 .type = TRACE_KRETPROBE,
914 .trace = print_kretprobe_event,
917 static int probe_event_enable(struct ftrace_event_call *call)
919 struct trace_probe *tp = (struct trace_probe *)call->data;
921 if (probe_is_return(tp))
922 return enable_kretprobe(&tp->rp);
924 return enable_kprobe(&tp->kp);
927 static void probe_event_disable(struct ftrace_event_call *call)
929 struct trace_probe *tp = (struct trace_probe *)call->data;
931 if (probe_is_return(tp))
932 disable_kretprobe(&tp->rp);
934 disable_kprobe(&tp->kp);
937 static int probe_event_raw_init(struct ftrace_event_call *event_call)
939 INIT_LIST_HEAD(&event_call->fields);
940 init_preds(event_call);
945 #define DEFINE_FIELD(type, item, name, is_signed) \
947 ret = trace_define_field(event_call, #type, name, \
948 offsetof(typeof(field), item), \
949 sizeof(field.item), is_signed, \
955 static int kprobe_event_define_fields(struct ftrace_event_call *event_call)
958 struct kprobe_trace_entry field;
959 char buf[MAX_ARGSTR_LEN + 1];
960 struct trace_probe *tp = (struct trace_probe *)event_call->data;
962 ret = trace_define_common_fields(event_call);
966 DEFINE_FIELD(unsigned long, ip, "ip", 0);
967 DEFINE_FIELD(int, nargs, "nargs", 1);
968 for (i = 0; i < tp->nr_args; i++) {
969 /* Set argN as a field */
970 sprintf(buf, "arg%d", i);
971 DEFINE_FIELD(unsigned long, args[i], buf, 0);
972 /* Set argument string as an alias field */
973 ret = trace_arg_string(buf, MAX_ARGSTR_LEN, &tp->args[i]);
976 DEFINE_FIELD(unsigned long, args[i], buf, 0);
981 static int kretprobe_event_define_fields(struct ftrace_event_call *event_call)
984 struct kretprobe_trace_entry field;
985 char buf[MAX_ARGSTR_LEN + 1];
986 struct trace_probe *tp = (struct trace_probe *)event_call->data;
988 ret = trace_define_common_fields(event_call);
992 DEFINE_FIELD(unsigned long, func, "func", 0);
993 DEFINE_FIELD(unsigned long, ret_ip, "ret_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]);
1003 DEFINE_FIELD(unsigned long, args[i], buf, 0);
1008 static int __probe_event_show_format(struct trace_seq *s,
1009 struct trace_probe *tp, const char *fmt,
1013 char buf[MAX_ARGSTR_LEN + 1];
1016 for (i = 0; i < tp->nr_args; i++) {
1017 ret = trace_arg_string(buf, MAX_ARGSTR_LEN, &tp->args[i]);
1020 if (!trace_seq_printf(s, "\talias: %s;\toriginal: arg%d;\n",
1025 if (!trace_seq_printf(s, "\nprint fmt: \"%s", fmt))
1028 for (i = 0; i < tp->nr_args; i++)
1029 if (!trace_seq_puts(s, " 0x%lx"))
1032 if (!trace_seq_printf(s, "\", %s", arg))
1035 for (i = 0; i < tp->nr_args; i++)
1036 if (!trace_seq_printf(s, ", arg%d", i))
1039 return trace_seq_puts(s, "\n");
1043 #define SHOW_FIELD(type, item, name) \
1045 ret = trace_seq_printf(s, "\tfield: " #type " %s;\t" \
1046 "offset:%u;tsize:%u;\n", name, \
1047 (unsigned int)offsetof(typeof(field), item),\
1048 (unsigned int)sizeof(type)); \
1053 static int kprobe_event_show_format(struct ftrace_event_call *call,
1054 struct trace_seq *s)
1056 struct kprobe_trace_entry field __attribute__((unused));
1059 struct trace_probe *tp = (struct trace_probe *)call->data;
1061 SHOW_FIELD(unsigned long, ip, "ip");
1062 SHOW_FIELD(int, nargs, "nargs");
1065 for (i = 0; i < tp->nr_args; i++) {
1066 sprintf(buf, "arg%d", i);
1067 SHOW_FIELD(unsigned long, args[i], buf);
1069 trace_seq_puts(s, "\n");
1071 return __probe_event_show_format(s, tp, "%lx:", "ip");
1074 static int kretprobe_event_show_format(struct ftrace_event_call *call,
1075 struct trace_seq *s)
1077 struct kretprobe_trace_entry field __attribute__((unused));
1080 struct trace_probe *tp = (struct trace_probe *)call->data;
1082 SHOW_FIELD(unsigned long, func, "func");
1083 SHOW_FIELD(unsigned long, ret_ip, "ret_ip");
1084 SHOW_FIELD(int, nargs, "nargs");
1087 for (i = 0; i < tp->nr_args; i++) {
1088 sprintf(buf, "arg%d", i);
1089 SHOW_FIELD(unsigned long, args[i], buf);
1091 trace_seq_puts(s, "\n");
1093 return __probe_event_show_format(s, tp, "%lx <- %lx:",
1097 static int register_probe_event(struct trace_probe *tp)
1099 struct ftrace_event_call *call = &tp->call;
1102 /* Initialize ftrace_event_call */
1103 call->system = "kprobes";
1104 if (probe_is_return(tp)) {
1105 call->event = &kretprobe_trace_event;
1106 call->id = TRACE_KRETPROBE;
1107 call->raw_init = probe_event_raw_init;
1108 call->show_format = kretprobe_event_show_format;
1109 call->define_fields = kretprobe_event_define_fields;
1111 call->event = &kprobe_trace_event;
1112 call->id = TRACE_KPROBE;
1113 call->raw_init = probe_event_raw_init;
1114 call->show_format = kprobe_event_show_format;
1115 call->define_fields = kprobe_event_define_fields;
1118 call->regfunc = probe_event_enable;
1119 call->unregfunc = probe_event_disable;
1121 ret = trace_add_event_call(call);
1123 pr_info("Failed to register kprobe event: %s\n", call->name);
1127 static void unregister_probe_event(struct trace_probe *tp)
1130 * Prevent to unregister event itself because the event is shared
1131 * among other probes.
1133 tp->call.event = NULL;
1134 trace_remove_event_call(&tp->call);
1137 /* Make a debugfs interface for controling probe points */
1138 static __init int init_kprobe_trace(void)
1140 struct dentry *d_tracer;
1141 struct dentry *entry;
1144 ret = register_ftrace_event(&kprobe_trace_event);
1146 pr_warning("Could not register kprobe_trace_event type.\n");
1149 ret = register_ftrace_event(&kretprobe_trace_event);
1151 pr_warning("Could not register kretprobe_trace_event type.\n");
1155 d_tracer = tracing_init_dentry();
1159 entry = debugfs_create_file("kprobe_events", 0644, d_tracer,
1160 NULL, &kprobe_events_ops);
1163 pr_warning("Could not create debugfs "
1164 "'kprobe_events' entry\n");
1167 fs_initcall(init_kprobe_trace);
1170 #ifdef CONFIG_FTRACE_STARTUP_TEST
1172 static int kprobe_trace_selftest_target(int a1, int a2, int a3,
1173 int a4, int a5, int a6)
1175 return a1 + a2 + a3 + a4 + a5 + a6;
1178 static __init int kprobe_trace_self_tests_init(void)
1181 int (*target)(int, int, int, int, int, int);
1183 target = kprobe_trace_selftest_target;
1185 pr_info("Testing kprobe tracing: ");
1187 ret = command_trace_probe("p:testprobe kprobe_trace_selftest_target "
1188 "a1 a2 a3 a4 a5 a6");
1189 if (WARN_ON_ONCE(ret))
1190 pr_warning("error enabling function entry\n");
1192 ret = command_trace_probe("r:testprobe2 kprobe_trace_selftest_target "
1194 if (WARN_ON_ONCE(ret))
1195 pr_warning("error enabling function return\n");
1197 ret = target(1, 2, 3, 4, 5, 6);
1199 cleanup_all_probes();
1205 late_initcall(kprobe_trace_self_tests_init);