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 TRACE_KPROBE_ARGS 6
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 unsigned int nr_args;
188 struct fetch_func args[TRACE_KPROBE_ARGS];
189 struct ftrace_event_call call;
192 static int kprobe_trace_func(struct kprobe *kp, struct pt_regs *regs);
193 static int kretprobe_trace_func(struct kretprobe_instance *ri,
194 struct pt_regs *regs);
196 static __kprobes int probe_is_return(struct trace_probe *tp)
198 return (tp->rp.handler == kretprobe_trace_func);
201 static __kprobes const char *probe_symbol(struct trace_probe *tp)
203 return tp->symbol ? tp->symbol : "unknown";
206 static __kprobes long probe_offset(struct trace_probe *tp)
208 return (probe_is_return(tp)) ? tp->rp.kp.offset : tp->kp.offset;
211 static __kprobes void *probe_address(struct trace_probe *tp)
213 return (probe_is_return(tp)) ? tp->rp.kp.addr : tp->kp.addr;
216 static int trace_arg_string(char *buf, size_t n, struct fetch_func *ff)
220 if (ff->func == fetch_argument)
221 ret = snprintf(buf, n, "a%lu", (unsigned long)ff->data);
222 else if (ff->func == fetch_register) {
224 name = regs_query_register_name((unsigned int)((long)ff->data));
225 ret = snprintf(buf, n, "%%%s", name);
226 } else if (ff->func == fetch_stack)
227 ret = snprintf(buf, n, "s%lu", (unsigned long)ff->data);
228 else if (ff->func == fetch_memory)
229 ret = snprintf(buf, n, "@0x%p", ff->data);
230 else if (ff->func == fetch_symbol) {
231 struct symbol_cache *sc = ff->data;
232 ret = snprintf(buf, n, "@%s%+ld", sc->symbol, sc->offset);
233 } else if (ff->func == fetch_retvalue)
234 ret = snprintf(buf, n, "rv");
235 else if (ff->func == fetch_ip)
236 ret = snprintf(buf, n, "ra");
237 else if (ff->func == fetch_stack_address)
238 ret = snprintf(buf, n, "sa");
239 else if (ff->func == fetch_indirect) {
240 struct indirect_fetch_data *id = ff->data;
242 ret = snprintf(buf, n, "%+ld(", id->offset);
246 ret = trace_arg_string(buf + l, n - l, &id->orig);
250 ret = snprintf(buf + l, n - l, ")");
259 static int register_probe_event(struct trace_probe *tp);
260 static void unregister_probe_event(struct trace_probe *tp);
262 static DEFINE_MUTEX(probe_lock);
263 static LIST_HEAD(probe_list);
265 static struct trace_probe *alloc_trace_probe(const char *symbol,
268 struct trace_probe *tp;
270 tp = kzalloc(sizeof(struct trace_probe), GFP_KERNEL);
272 return ERR_PTR(-ENOMEM);
275 tp->symbol = kstrdup(symbol, GFP_KERNEL);
280 tp->call.name = kstrdup(event, GFP_KERNEL);
285 INIT_LIST_HEAD(&tp->list);
290 return ERR_PTR(-ENOMEM);
293 static void free_trace_probe(struct trace_probe *tp)
297 for (i = 0; i < tp->nr_args; i++)
298 if (tp->args[i].func == fetch_symbol)
299 free_symbol_cache(tp->args[i].data);
300 else if (tp->args[i].func == fetch_indirect)
301 free_indirect_fetch_data(tp->args[i].data);
303 kfree(tp->call.name);
308 static struct trace_probe *find_probe_event(const char *event)
310 struct trace_probe *tp;
312 list_for_each_entry(tp, &probe_list, list)
313 if (tp->call.name && !strcmp(tp->call.name, event))
318 static void __unregister_trace_probe(struct trace_probe *tp)
320 if (probe_is_return(tp))
321 unregister_kretprobe(&tp->rp);
323 unregister_kprobe(&tp->kp);
326 /* Unregister a trace_probe and probe_event: call with locking probe_lock */
327 static void unregister_trace_probe(struct trace_probe *tp)
330 unregister_probe_event(tp);
331 __unregister_trace_probe(tp);
335 /* Register a trace_probe and probe_event */
336 static int register_trace_probe(struct trace_probe *tp)
338 struct trace_probe *old_tp;
341 mutex_lock(&probe_lock);
343 if (probe_is_return(tp))
344 ret = register_kretprobe(&tp->rp);
346 ret = register_kprobe(&tp->kp);
349 pr_warning("Could not insert probe(%d)\n", ret);
350 if (ret == -EILSEQ) {
351 pr_warning("Probing address(0x%p) is not an "
352 "instruction boundary.\n",
358 /* register as an event */
360 old_tp = find_probe_event(tp->call.name);
362 /* delete old event */
363 unregister_trace_probe(old_tp);
364 free_trace_probe(old_tp);
366 ret = register_probe_event(tp);
368 pr_warning("Faild to register probe event(%d)\n", ret);
369 __unregister_trace_probe(tp);
372 list_add_tail(&tp->list, &probe_list);
374 mutex_unlock(&probe_lock);
378 /* Split symbol and offset. */
379 static int split_symbol_offset(char *symbol, long *offset)
387 tmp = strchr(symbol, '+');
389 tmp = strchr(symbol, '-');
392 /* skip sign because strict_strtol doesn't accept '+' */
393 ret = strict_strtol(tmp + 1, 0, offset);
397 *offset = -(*offset);
404 #define PARAM_MAX_ARGS 16
405 #define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long))
407 static int parse_trace_arg(char *arg, struct fetch_func *ff, int is_return)
415 case 'a': /* argument */
416 ret = strict_strtoul(arg + 1, 10, ¶m);
417 if (ret || param > PARAM_MAX_ARGS)
420 ff->func = fetch_argument;
421 ff->data = (void *)param;
424 case 'r': /* retval or retaddr */
425 if (is_return && arg[1] == 'v') {
426 ff->func = fetch_retvalue;
428 } else if (is_return && arg[1] == 'a') {
434 case '%': /* named register */
435 ret = regs_query_register_offset(arg + 1);
437 ff->func = fetch_register;
438 ff->data = (void *)(unsigned long)ret;
442 case 's': /* stack */
444 ff->func = fetch_stack_address;
447 ret = strict_strtoul(arg + 1, 10, ¶m);
448 if (ret || param > PARAM_MAX_STACK)
451 ff->func = fetch_stack;
452 ff->data = (void *)param;
456 case '@': /* memory or symbol */
457 if (isdigit(arg[1])) {
458 ret = strict_strtoul(arg + 1, 0, ¶m);
461 ff->func = fetch_memory;
462 ff->data = (void *)param;
464 ret = split_symbol_offset(arg + 1, &offset);
467 ff->data = alloc_symbol_cache(arg + 1,
470 ff->func = fetch_symbol;
475 case '+': /* indirect memory */
477 tmp = strchr(arg, '(');
483 ret = strict_strtol(arg + 1, 0, &offset);
489 tmp = strrchr(arg, ')');
491 struct indirect_fetch_data *id;
493 id = kzalloc(sizeof(struct indirect_fetch_data),
498 ret = parse_trace_arg(arg, &id->orig, is_return);
502 ff->func = fetch_indirect;
503 ff->data = (void *)id;
509 /* TODO: support custom handler */
515 static int create_trace_probe(int argc, char **argv)
519 * - Add kprobe: p[:EVENT] SYMBOL[+OFFS|-OFFS]|ADDRESS [FETCHARGS]
520 * - Add kretprobe: r[:EVENT] SYMBOL[+0] [FETCHARGS]
522 * aN : fetch Nth of function argument. (N:0-)
523 * rv : fetch return value
524 * ra : fetch return address
525 * sa : fetch stack address
526 * sN : fetch Nth of stack (N:0-)
527 * @ADDR : fetch memory at ADDR (ADDR should be in kernel)
528 * @SYM[+|-offs] : fetch memory at SYM +|- offs (SYM is a data symbol)
529 * %REG : fetch register REG
530 * Indirect memory fetch:
531 * +|-offs(ARG) : fetch memory at ARG +|- offs address.
533 struct trace_probe *tp;
537 char *symbol = NULL, *event = NULL;
544 if (argv[0][0] == 'p')
546 else if (argv[0][0] == 'r')
551 if (argv[0][1] == ':') {
553 if (strlen(event) == 0) {
554 pr_info("Event name is not specifiled\n");
559 if (isdigit(argv[1][0])) {
562 /* an address specified */
563 ret = strict_strtoul(&argv[0][2], 0, (unsigned long *)&addr);
567 /* a symbol specified */
569 /* TODO: support .init module functions */
570 ret = split_symbol_offset(symbol, &offset);
573 if (offset && is_return)
578 tp = alloc_trace_probe(symbol, event);
584 tp->rp.handler = kretprobe_trace_func;
587 tp->kp.pre_handler = kprobe_trace_func;
591 kp->symbol_name = tp->symbol;
596 /* parse arguments */
597 argc -= 2; argv += 2; ret = 0;
598 for (i = 0; i < argc && i < TRACE_KPROBE_ARGS; i++) {
599 if (strlen(argv[i]) > MAX_ARGSTR_LEN) {
600 pr_info("Argument%d(%s) is too long.\n", i, argv[i]);
604 ret = parse_trace_arg(argv[i], &tp->args[i], is_return);
610 ret = register_trace_probe(tp);
616 free_trace_probe(tp);
620 static void cleanup_all_probes(void)
622 struct trace_probe *tp;
624 mutex_lock(&probe_lock);
625 /* TODO: Use batch unregistration */
626 while (!list_empty(&probe_list)) {
627 tp = list_entry(probe_list.next, struct trace_probe, list);
628 unregister_trace_probe(tp);
629 free_trace_probe(tp);
631 mutex_unlock(&probe_lock);
635 /* Probes listing interfaces */
636 static void *probes_seq_start(struct seq_file *m, loff_t *pos)
638 mutex_lock(&probe_lock);
639 return seq_list_start(&probe_list, *pos);
642 static void *probes_seq_next(struct seq_file *m, void *v, loff_t *pos)
644 return seq_list_next(v, &probe_list, pos);
647 static void probes_seq_stop(struct seq_file *m, void *v)
649 mutex_unlock(&probe_lock);
652 static int probes_seq_show(struct seq_file *m, void *v)
654 struct trace_probe *tp = v;
656 char buf[MAX_ARGSTR_LEN + 1];
658 seq_printf(m, "%c", probe_is_return(tp) ? 'r' : 'p');
660 seq_printf(m, ":%s", tp->call.name);
663 seq_printf(m, " %s%+ld", probe_symbol(tp), probe_offset(tp));
665 seq_printf(m, " 0x%p", probe_address(tp));
667 for (i = 0; i < tp->nr_args; i++) {
668 ret = trace_arg_string(buf, MAX_ARGSTR_LEN, &tp->args[i]);
670 pr_warning("Argument%d decoding error(%d).\n", i, ret);
673 seq_printf(m, " %s", buf);
679 static const struct seq_operations probes_seq_op = {
680 .start = probes_seq_start,
681 .next = probes_seq_next,
682 .stop = probes_seq_stop,
683 .show = probes_seq_show
686 static int probes_open(struct inode *inode, struct file *file)
688 if ((file->f_mode & FMODE_WRITE) &&
689 (file->f_flags & O_TRUNC))
690 cleanup_all_probes();
692 return seq_open(file, &probes_seq_op);
695 static int command_trace_probe(const char *buf)
698 int argc = 0, ret = 0;
700 argv = argv_split(GFP_KERNEL, buf, &argc);
705 ret = create_trace_probe(argc, argv);
711 #define WRITE_BUFSIZE 128
713 static ssize_t probes_write(struct file *file, const char __user *buffer,
714 size_t count, loff_t *ppos)
721 kbuf = kmalloc(WRITE_BUFSIZE, GFP_KERNEL);
726 while (done < count) {
728 if (size >= WRITE_BUFSIZE)
729 size = WRITE_BUFSIZE - 1;
730 if (copy_from_user(kbuf, buffer + done, size)) {
735 tmp = strchr(kbuf, '\n');
738 size = tmp - kbuf + 1;
739 } else if (done + size < count) {
740 pr_warning("Line length is too long: "
741 "Should be less than %d.", WRITE_BUFSIZE);
746 /* Remove comments */
747 tmp = strchr(kbuf, '#');
751 ret = command_trace_probe(kbuf);
761 static const struct file_operations kprobe_events_ops = {
762 .owner = THIS_MODULE,
766 .release = seq_release,
767 .write = probes_write,
771 static __kprobes int kprobe_trace_func(struct kprobe *kp, struct pt_regs *regs)
773 struct trace_probe *tp = container_of(kp, struct trace_probe, kp);
774 struct kprobe_trace_entry *entry;
775 struct ring_buffer_event *event;
777 unsigned long irq_flags;
778 struct ftrace_event_call *call = &event_kprobe;
783 local_save_flags(irq_flags);
784 pc = preempt_count();
786 size = SIZEOF_KPROBE_TRACE_ENTRY(tp->nr_args);
788 event = trace_current_buffer_lock_reserve(TRACE_KPROBE, size,
793 entry = ring_buffer_event_data(event);
794 entry->nargs = tp->nr_args;
795 entry->ip = (unsigned long)kp->addr;
796 for (i = 0; i < tp->nr_args; i++)
797 entry->args[i] = call_fetch(&tp->args[i], regs);
799 if (!filter_current_check_discard(call, entry, event))
800 trace_nowake_buffer_unlock_commit(event, irq_flags, pc);
804 /* Kretprobe handler */
805 static __kprobes int kretprobe_trace_func(struct kretprobe_instance *ri,
806 struct pt_regs *regs)
808 struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
809 struct kretprobe_trace_entry *entry;
810 struct ring_buffer_event *event;
812 unsigned long irq_flags;
813 struct ftrace_event_call *call = &event_kretprobe;
818 local_save_flags(irq_flags);
819 pc = preempt_count();
821 size = SIZEOF_KRETPROBE_TRACE_ENTRY(tp->nr_args);
823 event = trace_current_buffer_lock_reserve(TRACE_KRETPROBE, size,
828 entry = ring_buffer_event_data(event);
829 entry->nargs = tp->nr_args;
830 entry->func = (unsigned long)probe_address(tp);
831 entry->ret_ip = (unsigned long)ri->ret_addr;
832 for (i = 0; i < tp->nr_args; i++)
833 entry->args[i] = call_fetch(&tp->args[i], regs);
835 if (!filter_current_check_discard(call, entry, event))
836 trace_nowake_buffer_unlock_commit(event, irq_flags, pc);
841 /* Event entry printers */
843 print_kprobe_event(struct trace_iterator *iter, int flags)
845 struct kprobe_trace_entry *field;
846 struct trace_seq *s = &iter->seq;
849 trace_assign_type(field, iter->ent);
851 if (!seq_print_ip_sym(s, field->ip, flags | TRACE_ITER_SYM_OFFSET))
854 if (!trace_seq_puts(s, ":"))
857 for (i = 0; i < field->nargs; i++)
858 if (!trace_seq_printf(s, " 0x%lx", field->args[i]))
861 if (!trace_seq_puts(s, "\n"))
864 return TRACE_TYPE_HANDLED;
866 return TRACE_TYPE_PARTIAL_LINE;
870 print_kretprobe_event(struct trace_iterator *iter, int flags)
872 struct kretprobe_trace_entry *field;
873 struct trace_seq *s = &iter->seq;
876 trace_assign_type(field, iter->ent);
878 if (!seq_print_ip_sym(s, field->ret_ip, flags | TRACE_ITER_SYM_OFFSET))
881 if (!trace_seq_puts(s, " <- "))
884 if (!seq_print_ip_sym(s, field->func, flags & ~TRACE_ITER_SYM_OFFSET))
887 if (!trace_seq_puts(s, ":"))
890 for (i = 0; i < field->nargs; i++)
891 if (!trace_seq_printf(s, " 0x%lx", field->args[i]))
894 if (!trace_seq_puts(s, "\n"))
897 return TRACE_TYPE_HANDLED;
899 return TRACE_TYPE_PARTIAL_LINE;
902 static struct trace_event kprobe_trace_event = {
903 .type = TRACE_KPROBE,
904 .trace = print_kprobe_event,
907 static struct trace_event kretprobe_trace_event = {
908 .type = TRACE_KRETPROBE,
909 .trace = print_kretprobe_event,
912 static int probe_event_enable(struct ftrace_event_call *call)
914 struct trace_probe *tp = (struct trace_probe *)call->data;
916 if (probe_is_return(tp))
917 return enable_kretprobe(&tp->rp);
919 return enable_kprobe(&tp->kp);
922 static void probe_event_disable(struct ftrace_event_call *call)
924 struct trace_probe *tp = (struct trace_probe *)call->data;
926 if (probe_is_return(tp))
927 disable_kretprobe(&tp->rp);
929 disable_kprobe(&tp->kp);
932 static int probe_event_raw_init(struct ftrace_event_call *event_call)
934 INIT_LIST_HEAD(&event_call->fields);
935 init_preds(event_call);
940 #define DEFINE_FIELD(type, item, name, is_signed) \
942 ret = trace_define_field(event_call, #type, name, \
943 offsetof(typeof(field), item), \
944 sizeof(field.item), is_signed, \
950 static int kprobe_event_define_fields(struct ftrace_event_call *event_call)
953 struct kprobe_trace_entry field;
954 char buf[MAX_ARGSTR_LEN + 1];
955 struct trace_probe *tp = (struct trace_probe *)event_call->data;
957 ret = trace_define_common_fields(event_call);
961 DEFINE_FIELD(unsigned long, ip, "ip", 0);
962 DEFINE_FIELD(int, nargs, "nargs", 1);
963 for (i = 0; i < tp->nr_args; i++) {
964 /* Set argN as a field */
965 sprintf(buf, "arg%d", i);
966 DEFINE_FIELD(unsigned long, args[i], buf, 0);
967 /* Set argument string as an alias field */
968 ret = trace_arg_string(buf, MAX_ARGSTR_LEN, &tp->args[i]);
971 DEFINE_FIELD(unsigned long, args[i], buf, 0);
976 static int kretprobe_event_define_fields(struct ftrace_event_call *event_call)
979 struct kretprobe_trace_entry field;
980 char buf[MAX_ARGSTR_LEN + 1];
981 struct trace_probe *tp = (struct trace_probe *)event_call->data;
983 ret = trace_define_common_fields(event_call);
987 DEFINE_FIELD(unsigned long, func, "func", 0);
988 DEFINE_FIELD(unsigned long, ret_ip, "ret_ip", 0);
989 DEFINE_FIELD(int, nargs, "nargs", 1);
990 for (i = 0; i < tp->nr_args; i++) {
991 /* Set argN as a field */
992 sprintf(buf, "arg%d", i);
993 DEFINE_FIELD(unsigned long, args[i], buf, 0);
994 /* Set argument string as an alias field */
995 ret = trace_arg_string(buf, MAX_ARGSTR_LEN, &tp->args[i]);
998 DEFINE_FIELD(unsigned long, args[i], buf, 0);
1003 static int __probe_event_show_format(struct trace_seq *s,
1004 struct trace_probe *tp, const char *fmt,
1008 char buf[MAX_ARGSTR_LEN + 1];
1011 for (i = 0; i < tp->nr_args; i++) {
1012 ret = trace_arg_string(buf, MAX_ARGSTR_LEN, &tp->args[i]);
1015 if (!trace_seq_printf(s, "\talias: %s;\toriginal: arg%d;\n",
1020 if (!trace_seq_printf(s, "\nprint fmt: \"%s", fmt))
1023 for (i = 0; i < tp->nr_args; i++)
1024 if (!trace_seq_puts(s, " 0x%lx"))
1027 if (!trace_seq_printf(s, "\", %s", arg))
1030 for (i = 0; i < tp->nr_args; i++)
1031 if (!trace_seq_printf(s, ", arg%d", i))
1034 return trace_seq_puts(s, "\n");
1038 #define SHOW_FIELD(type, item, name) \
1040 ret = trace_seq_printf(s, "\tfield: " #type " %s;\t" \
1041 "offset:%u;tsize:%u;\n", name, \
1042 (unsigned int)offsetof(typeof(field), item),\
1043 (unsigned int)sizeof(type)); \
1048 static int kprobe_event_show_format(struct ftrace_event_call *call,
1049 struct trace_seq *s)
1051 struct kprobe_trace_entry field __attribute__((unused));
1054 struct trace_probe *tp = (struct trace_probe *)call->data;
1056 SHOW_FIELD(unsigned long, ip, "ip");
1057 SHOW_FIELD(int, nargs, "nargs");
1060 for (i = 0; i < tp->nr_args; i++) {
1061 sprintf(buf, "arg%d", i);
1062 SHOW_FIELD(unsigned long, args[i], buf);
1064 trace_seq_puts(s, "\n");
1066 return __probe_event_show_format(s, tp, "%lx:", "ip");
1069 static int kretprobe_event_show_format(struct ftrace_event_call *call,
1070 struct trace_seq *s)
1072 struct kretprobe_trace_entry field __attribute__((unused));
1075 struct trace_probe *tp = (struct trace_probe *)call->data;
1077 SHOW_FIELD(unsigned long, func, "func");
1078 SHOW_FIELD(unsigned long, ret_ip, "ret_ip");
1079 SHOW_FIELD(int, nargs, "nargs");
1082 for (i = 0; i < tp->nr_args; i++) {
1083 sprintf(buf, "arg%d", i);
1084 SHOW_FIELD(unsigned long, args[i], buf);
1086 trace_seq_puts(s, "\n");
1088 return __probe_event_show_format(s, tp, "%lx <- %lx:",
1092 static int register_probe_event(struct trace_probe *tp)
1094 struct ftrace_event_call *call = &tp->call;
1097 /* Initialize ftrace_event_call */
1098 call->system = "kprobes";
1099 if (probe_is_return(tp)) {
1100 call->event = &kretprobe_trace_event;
1101 call->id = TRACE_KRETPROBE;
1102 call->raw_init = probe_event_raw_init;
1103 call->show_format = kretprobe_event_show_format;
1104 call->define_fields = kretprobe_event_define_fields;
1106 call->event = &kprobe_trace_event;
1107 call->id = TRACE_KPROBE;
1108 call->raw_init = probe_event_raw_init;
1109 call->show_format = kprobe_event_show_format;
1110 call->define_fields = kprobe_event_define_fields;
1113 call->regfunc = probe_event_enable;
1114 call->unregfunc = probe_event_disable;
1116 ret = trace_add_event_call(call);
1118 pr_info("Failed to register kprobe event: %s\n", call->name);
1122 static void unregister_probe_event(struct trace_probe *tp)
1125 * Prevent to unregister event itself because the event is shared
1126 * among other probes.
1128 tp->call.event = NULL;
1129 trace_remove_event_call(&tp->call);
1132 /* Make a debugfs interface for controling probe points */
1133 static __init int init_kprobe_trace(void)
1135 struct dentry *d_tracer;
1136 struct dentry *entry;
1139 ret = register_ftrace_event(&kprobe_trace_event);
1141 pr_warning("Could not register kprobe_trace_event type.\n");
1144 ret = register_ftrace_event(&kretprobe_trace_event);
1146 pr_warning("Could not register kretprobe_trace_event type.\n");
1150 d_tracer = tracing_init_dentry();
1154 entry = debugfs_create_file("kprobe_events", 0644, d_tracer,
1155 NULL, &kprobe_events_ops);
1158 pr_warning("Could not create debugfs "
1159 "'kprobe_events' entry\n");
1162 fs_initcall(init_kprobe_trace);
1165 #ifdef CONFIG_FTRACE_STARTUP_TEST
1167 static int kprobe_trace_selftest_target(int a1, int a2, int a3,
1168 int a4, int a5, int a6)
1170 return a1 + a2 + a3 + a4 + a5 + a6;
1173 static __init int kprobe_trace_self_tests_init(void)
1176 int (*target)(int, int, int, int, int, int);
1178 target = kprobe_trace_selftest_target;
1180 pr_info("Testing kprobe tracing: ");
1182 ret = command_trace_probe("p:testprobe kprobe_trace_selftest_target "
1183 "a1 a2 a3 a4 a5 a6");
1184 if (WARN_ON_ONCE(ret))
1185 pr_warning("error enabling function entry\n");
1187 ret = command_trace_probe("r:testprobe2 kprobe_trace_selftest_target "
1189 if (WARN_ON_ONCE(ret))
1190 pr_warning("error enabling function return\n");
1192 ret = target(1, 2, 3, 4, 5, 6);
1194 cleanup_all_probes();
1200 late_initcall(kprobe_trace_self_tests_init);