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
37 #define MAX_EVENT_NAME_LEN 64
39 /* currently, trace_kprobe only supports X86. */
42 unsigned long (*func)(struct pt_regs *, void *);
46 static __kprobes unsigned long call_fetch(struct fetch_func *f,
49 return f->func(regs, f->data);
53 static __kprobes unsigned long fetch_register(struct pt_regs *regs,
56 return regs_get_register(regs, (unsigned int)((unsigned long)offset));
59 static __kprobes unsigned long fetch_stack(struct pt_regs *regs,
62 return regs_get_kernel_stack_nth(regs,
63 (unsigned int)((unsigned long)num));
66 static __kprobes unsigned long fetch_memory(struct pt_regs *regs, void *addr)
70 if (probe_kernel_address(addr, retval))
75 static __kprobes unsigned long fetch_argument(struct pt_regs *regs, void *num)
77 return regs_get_argument_nth(regs, (unsigned int)((unsigned long)num));
80 static __kprobes unsigned long fetch_retvalue(struct pt_regs *regs,
83 return regs_return_value(regs);
86 static __kprobes unsigned long fetch_ip(struct pt_regs *regs, void *dummy)
88 return instruction_pointer(regs);
91 static __kprobes unsigned long fetch_stack_address(struct pt_regs *regs,
94 return kernel_stack_pointer(regs);
97 /* Memory fetching by symbol */
104 static unsigned long update_symbol_cache(struct symbol_cache *sc)
106 sc->addr = (unsigned long)kallsyms_lookup_name(sc->symbol);
108 sc->addr += sc->offset;
112 static void free_symbol_cache(struct symbol_cache *sc)
118 static struct symbol_cache *alloc_symbol_cache(const char *sym, long offset)
120 struct symbol_cache *sc;
122 if (!sym || strlen(sym) == 0)
124 sc = kzalloc(sizeof(struct symbol_cache), GFP_KERNEL);
128 sc->symbol = kstrdup(sym, GFP_KERNEL);
135 update_symbol_cache(sc);
139 static __kprobes unsigned long fetch_symbol(struct pt_regs *regs, void *data)
141 struct symbol_cache *sc = data;
144 return fetch_memory(regs, (void *)sc->addr);
149 /* Special indirect memory access interface */
150 struct indirect_fetch_data {
151 struct fetch_func orig;
155 static __kprobes unsigned long fetch_indirect(struct pt_regs *regs, void *data)
157 struct indirect_fetch_data *ind = data;
160 addr = call_fetch(&ind->orig, regs);
163 return fetch_memory(regs, (void *)addr);
168 static __kprobes void free_indirect_fetch_data(struct indirect_fetch_data *data)
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);
182 struct list_head list;
188 const char *symbol; /* symbol name */
189 struct ftrace_event_call call;
190 struct trace_event event;
191 unsigned int nr_args;
192 struct fetch_func args[];
195 #define SIZEOF_TRACE_PROBE(n) \
196 (offsetof(struct trace_probe, args) + \
197 (sizeof(struct fetch_func) * (n)))
199 static int kprobe_trace_func(struct kprobe *kp, struct pt_regs *regs);
200 static int kretprobe_trace_func(struct kretprobe_instance *ri,
201 struct pt_regs *regs);
203 static __kprobes int probe_is_return(struct trace_probe *tp)
205 return (tp->rp.handler == kretprobe_trace_func);
208 static __kprobes const char *probe_symbol(struct trace_probe *tp)
210 return tp->symbol ? tp->symbol : "unknown";
213 static __kprobes long probe_offset(struct trace_probe *tp)
215 return (probe_is_return(tp)) ? tp->rp.kp.offset : tp->kp.offset;
218 static __kprobes void *probe_address(struct trace_probe *tp)
220 return (probe_is_return(tp)) ? tp->rp.kp.addr : tp->kp.addr;
223 static int probe_arg_string(char *buf, size_t n, struct fetch_func *ff)
227 if (ff->func == fetch_argument)
228 ret = snprintf(buf, n, "a%lu", (unsigned long)ff->data);
229 else if (ff->func == fetch_register) {
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;
249 ret = snprintf(buf, n, "%+ld(", id->offset);
253 ret = probe_arg_string(buf + l, n - l, &id->orig);
257 ret = snprintf(buf + l, n - l, ")");
266 static int register_probe_event(struct trace_probe *tp);
267 static void unregister_probe_event(struct trace_probe *tp);
269 static DEFINE_MUTEX(probe_lock);
270 static LIST_HEAD(probe_list);
272 static struct trace_probe *alloc_trace_probe(const char *symbol,
273 const char *event, int nargs)
275 struct trace_probe *tp;
277 tp = kzalloc(SIZEOF_TRACE_PROBE(nargs), GFP_KERNEL);
279 return ERR_PTR(-ENOMEM);
282 tp->symbol = kstrdup(symbol, GFP_KERNEL);
288 tp->call.name = kstrdup(event, GFP_KERNEL);
292 INIT_LIST_HEAD(&tp->list);
297 return ERR_PTR(-ENOMEM);
300 static void free_trace_probe(struct trace_probe *tp)
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);
310 kfree(tp->call.name);
315 static struct trace_probe *find_probe_event(const char *event)
317 struct trace_probe *tp;
319 list_for_each_entry(tp, &probe_list, list)
320 if (!strcmp(tp->call.name, event))
325 static void __unregister_trace_probe(struct trace_probe *tp)
327 if (probe_is_return(tp))
328 unregister_kretprobe(&tp->rp);
330 unregister_kprobe(&tp->kp);
333 /* Unregister a trace_probe and probe_event: call with locking probe_lock */
334 static void unregister_trace_probe(struct trace_probe *tp)
336 unregister_probe_event(tp);
337 __unregister_trace_probe(tp);
341 /* Register a trace_probe and probe_event */
342 static int register_trace_probe(struct trace_probe *tp)
344 struct trace_probe *old_tp;
347 mutex_lock(&probe_lock);
349 if (probe_is_return(tp))
350 ret = register_kretprobe(&tp->rp);
352 ret = register_kprobe(&tp->kp);
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",
364 /* register as an event */
365 old_tp = find_probe_event(tp->call.name);
367 /* delete old event */
368 unregister_trace_probe(old_tp);
369 free_trace_probe(old_tp);
371 ret = register_probe_event(tp);
373 pr_warning("Faild to register probe event(%d)\n", ret);
374 __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_probe_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_probe_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;
584 /* Make a new event name */
585 char buf[MAX_EVENT_NAME_LEN];
587 snprintf(buf, MAX_EVENT_NAME_LEN, "%c@%s%+ld",
588 is_return ? 'r' : 'p', symbol, offset);
590 snprintf(buf, MAX_EVENT_NAME_LEN, "%c@0x%p",
591 is_return ? 'r' : 'p', addr);
592 tp = alloc_trace_probe(symbol, buf, argc);
594 tp = alloc_trace_probe(symbol, event, argc);
600 tp->rp.handler = kretprobe_trace_func;
603 tp->kp.pre_handler = kprobe_trace_func;
607 kp->symbol_name = tp->symbol;
612 /* parse arguments */
614 for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
615 if (strlen(argv[i]) > MAX_ARGSTR_LEN) {
616 pr_info("Argument%d(%s) is too long.\n", i, argv[i]);
620 ret = parse_probe_arg(argv[i], &tp->args[i], is_return);
626 ret = register_trace_probe(tp);
632 free_trace_probe(tp);
636 static void cleanup_all_probes(void)
638 struct trace_probe *tp;
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);
647 mutex_unlock(&probe_lock);
651 /* Probes listing interfaces */
652 static void *probes_seq_start(struct seq_file *m, loff_t *pos)
654 mutex_lock(&probe_lock);
655 return seq_list_start(&probe_list, *pos);
658 static void *probes_seq_next(struct seq_file *m, void *v, loff_t *pos)
660 return seq_list_next(v, &probe_list, pos);
663 static void probes_seq_stop(struct seq_file *m, void *v)
665 mutex_unlock(&probe_lock);
668 static int probes_seq_show(struct seq_file *m, void *v)
670 struct trace_probe *tp = v;
672 char buf[MAX_ARGSTR_LEN + 1];
674 seq_printf(m, "%c", probe_is_return(tp) ? 'r' : 'p');
675 seq_printf(m, ":%s", tp->call.name);
678 seq_printf(m, " %s%+ld", probe_symbol(tp), probe_offset(tp));
680 seq_printf(m, " 0x%p", probe_address(tp));
682 for (i = 0; i < tp->nr_args; i++) {
683 ret = probe_arg_string(buf, MAX_ARGSTR_LEN, &tp->args[i]);
685 pr_warning("Argument%d decoding error(%d).\n", i, ret);
688 seq_printf(m, " %s", buf);
694 static 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
701 static int probes_open(struct inode *inode, struct file *file)
703 if ((file->f_mode & FMODE_WRITE) &&
704 (file->f_flags & O_TRUNC))
705 cleanup_all_probes();
707 return seq_open(file, &probes_seq_op);
710 static int command_trace_probe(const char *buf)
713 int argc = 0, ret = 0;
715 argv = argv_split(GFP_KERNEL, buf, &argc);
720 ret = create_trace_probe(argc, argv);
726 #define WRITE_BUFSIZE 128
728 static ssize_t probes_write(struct file *file, const char __user *buffer,
729 size_t count, loff_t *ppos)
736 kbuf = kmalloc(WRITE_BUFSIZE, GFP_KERNEL);
741 while (done < count) {
743 if (size >= WRITE_BUFSIZE)
744 size = WRITE_BUFSIZE - 1;
745 if (copy_from_user(kbuf, buffer + done, size)) {
750 tmp = strchr(kbuf, '\n');
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);
761 /* Remove comments */
762 tmp = strchr(kbuf, '#');
766 ret = command_trace_probe(kbuf);
776 static const struct file_operations kprobe_events_ops = {
777 .owner = THIS_MODULE,
781 .release = seq_release,
782 .write = probes_write,
785 /* Probes profiling interfaces */
786 static int probes_profile_seq_show(struct seq_file *m, void *v)
788 struct trace_probe *tp = v;
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);
796 static 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
803 static int profile_open(struct inode *inode, struct file *file)
805 return seq_open(file, &profile_seq_op);
808 static const struct file_operations kprobe_profile_ops = {
809 .owner = THIS_MODULE,
810 .open = profile_open,
813 .release = seq_release,
817 static __kprobes int kprobe_trace_func(struct kprobe *kp, struct pt_regs *regs)
819 struct trace_probe *tp = container_of(kp, struct trace_probe, kp);
820 struct kprobe_trace_entry *entry;
821 struct ring_buffer_event *event;
823 unsigned long irq_flags;
824 struct ftrace_event_call *call = &tp->call;
828 local_save_flags(irq_flags);
829 pc = preempt_count();
831 size = SIZEOF_KPROBE_TRACE_ENTRY(tp->nr_args);
833 event = trace_current_buffer_lock_reserve(call->id, size,
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);
844 if (!filter_current_check_discard(call, entry, event))
845 trace_nowake_buffer_unlock_commit(event, irq_flags, pc);
849 /* Kretprobe handler */
850 static __kprobes int kretprobe_trace_func(struct kretprobe_instance *ri,
851 struct pt_regs *regs)
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;
857 unsigned long irq_flags;
858 struct ftrace_event_call *call = &tp->call;
860 local_save_flags(irq_flags);
861 pc = preempt_count();
863 size = SIZEOF_KRETPROBE_TRACE_ENTRY(tp->nr_args);
865 event = trace_current_buffer_lock_reserve(call->id, size,
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);
877 if (!filter_current_check_discard(call, entry, event))
878 trace_nowake_buffer_unlock_commit(event, irq_flags, pc);
883 /* Event entry printers */
885 print_kprobe_event(struct trace_iterator *iter, int flags)
887 struct kprobe_trace_entry *field;
888 struct trace_seq *s = &iter->seq;
891 field = (struct kprobe_trace_entry *)iter->ent;
893 if (!seq_print_ip_sym(s, field->ip, flags | TRACE_ITER_SYM_OFFSET))
896 if (!trace_seq_puts(s, ":"))
899 for (i = 0; i < field->nargs; i++)
900 if (!trace_seq_printf(s, " 0x%lx", field->args[i]))
903 if (!trace_seq_puts(s, "\n"))
906 return TRACE_TYPE_HANDLED;
908 return TRACE_TYPE_PARTIAL_LINE;
912 print_kretprobe_event(struct trace_iterator *iter, int flags)
914 struct kretprobe_trace_entry *field;
915 struct trace_seq *s = &iter->seq;
918 field = (struct kretprobe_trace_entry *)iter->ent;
920 if (!seq_print_ip_sym(s, field->ret_ip, flags | TRACE_ITER_SYM_OFFSET))
923 if (!trace_seq_puts(s, " <- "))
926 if (!seq_print_ip_sym(s, field->func, flags & ~TRACE_ITER_SYM_OFFSET))
929 if (!trace_seq_puts(s, ":"))
932 for (i = 0; i < field->nargs; i++)
933 if (!trace_seq_printf(s, " 0x%lx", field->args[i]))
936 if (!trace_seq_puts(s, "\n"))
939 return TRACE_TYPE_HANDLED;
941 return TRACE_TYPE_PARTIAL_LINE;
944 static int probe_event_enable(struct ftrace_event_call *call)
946 struct trace_probe *tp = (struct trace_probe *)call->data;
948 if (probe_is_return(tp))
949 return enable_kretprobe(&tp->rp);
951 return enable_kprobe(&tp->kp);
954 static void probe_event_disable(struct ftrace_event_call *call)
956 struct trace_probe *tp = (struct trace_probe *)call->data;
958 if (probe_is_return(tp))
959 disable_kretprobe(&tp->rp);
961 disable_kprobe(&tp->kp);
964 static int probe_event_raw_init(struct ftrace_event_call *event_call)
966 INIT_LIST_HEAD(&event_call->fields);
967 init_preds(event_call);
972 #define DEFINE_FIELD(type, item, name, is_signed) \
974 ret = trace_define_field(event_call, #type, name, \
975 offsetof(typeof(field), item), \
976 sizeof(field.item), is_signed, \
982 static int kprobe_event_define_fields(struct ftrace_event_call *event_call)
985 struct kprobe_trace_entry field;
986 char buf[MAX_ARGSTR_LEN + 1];
987 struct trace_probe *tp = (struct trace_probe *)event_call->data;
989 ret = trace_define_common_fields(event_call);
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 = probe_arg_string(buf, MAX_ARGSTR_LEN, &tp->args[i]);
1003 DEFINE_FIELD(unsigned long, args[i], buf, 0);
1008 static int kretprobe_event_define_fields(struct ftrace_event_call *event_call)
1011 struct kretprobe_trace_entry field;
1012 char buf[MAX_ARGSTR_LEN + 1];
1013 struct trace_probe *tp = (struct trace_probe *)event_call->data;
1015 ret = trace_define_common_fields(event_call);
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 = probe_arg_string(buf, MAX_ARGSTR_LEN, &tp->args[i]);
1030 DEFINE_FIELD(unsigned long, args[i], buf, 0);
1035 static int __probe_event_show_format(struct trace_seq *s,
1036 struct trace_probe *tp, const char *fmt,
1040 char buf[MAX_ARGSTR_LEN + 1];
1043 for (i = 0; i < tp->nr_args; i++) {
1044 ret = probe_arg_string(buf, MAX_ARGSTR_LEN, &tp->args[i]);
1047 if (!trace_seq_printf(s, "\talias: %s;\toriginal: arg%d;\n",
1052 if (!trace_seq_printf(s, "\nprint fmt: \"%s", fmt))
1055 for (i = 0; i < tp->nr_args; i++)
1056 if (!trace_seq_puts(s, " 0x%lx"))
1059 if (!trace_seq_printf(s, "\", %s", arg))
1062 for (i = 0; i < tp->nr_args; i++)
1063 if (!trace_seq_printf(s, ", arg%d", i))
1066 return trace_seq_puts(s, "\n");
1070 #define SHOW_FIELD(type, item, name) \
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)); \
1080 static int kprobe_event_show_format(struct ftrace_event_call *call,
1081 struct trace_seq *s)
1083 struct kprobe_trace_entry field __attribute__((unused));
1086 struct trace_probe *tp = (struct trace_probe *)call->data;
1088 SHOW_FIELD(unsigned long, ip, "ip");
1089 SHOW_FIELD(int, nargs, "nargs");
1092 for (i = 0; i < tp->nr_args; i++) {
1093 sprintf(buf, "arg%d", i);
1094 SHOW_FIELD(unsigned long, args[i], buf);
1096 trace_seq_puts(s, "\n");
1098 return __probe_event_show_format(s, tp, "%lx:", "ip");
1101 static int kretprobe_event_show_format(struct ftrace_event_call *call,
1102 struct trace_seq *s)
1104 struct kretprobe_trace_entry field __attribute__((unused));
1107 struct trace_probe *tp = (struct trace_probe *)call->data;
1109 SHOW_FIELD(unsigned long, func, "func");
1110 SHOW_FIELD(unsigned long, ret_ip, "ret_ip");
1111 SHOW_FIELD(int, nargs, "nargs");
1114 for (i = 0; i < tp->nr_args; i++) {
1115 sprintf(buf, "arg%d", i);
1116 SHOW_FIELD(unsigned long, args[i], buf);
1118 trace_seq_puts(s, "\n");
1120 return __probe_event_show_format(s, tp, "%lx <- %lx:",
1124 static int register_probe_event(struct trace_probe *tp)
1126 struct ftrace_event_call *call = &tp->call;
1129 /* Initialize ftrace_event_call */
1130 call->system = "kprobes";
1131 if (probe_is_return(tp)) {
1132 tp->event.trace = print_kretprobe_event;
1133 call->raw_init = probe_event_raw_init;
1134 call->show_format = kretprobe_event_show_format;
1135 call->define_fields = kretprobe_event_define_fields;
1137 tp->event.trace = print_kprobe_event;
1138 call->raw_init = probe_event_raw_init;
1139 call->show_format = kprobe_event_show_format;
1140 call->define_fields = kprobe_event_define_fields;
1142 call->event = &tp->event;
1143 call->id = register_ftrace_event(&tp->event);
1147 call->regfunc = probe_event_enable;
1148 call->unregfunc = probe_event_disable;
1150 ret = trace_add_event_call(call);
1152 pr_info("Failed to register kprobe event: %s\n", call->name);
1153 unregister_ftrace_event(&tp->event);
1158 static void unregister_probe_event(struct trace_probe *tp)
1160 /* tp->event is unregistered in trace_remove_event_call() */
1161 trace_remove_event_call(&tp->call);
1164 /* Make a debugfs interface for controling probe points */
1165 static __init int init_kprobe_trace(void)
1167 struct dentry *d_tracer;
1168 struct dentry *entry;
1170 d_tracer = tracing_init_dentry();
1174 entry = debugfs_create_file("kprobe_events", 0644, d_tracer,
1175 NULL, &kprobe_events_ops);
1177 /* Event list interface */
1179 pr_warning("Could not create debugfs "
1180 "'kprobe_events' entry\n");
1182 /* Profile interface */
1183 entry = debugfs_create_file("kprobe_profile", 0444, d_tracer,
1184 NULL, &kprobe_profile_ops);
1187 pr_warning("Could not create debugfs "
1188 "'kprobe_profile' entry\n");
1191 fs_initcall(init_kprobe_trace);
1194 #ifdef CONFIG_FTRACE_STARTUP_TEST
1196 static int kprobe_trace_selftest_target(int a1, int a2, int a3,
1197 int a4, int a5, int a6)
1199 return a1 + a2 + a3 + a4 + a5 + a6;
1202 static __init int kprobe_trace_self_tests_init(void)
1205 int (*target)(int, int, int, int, int, int);
1207 target = kprobe_trace_selftest_target;
1209 pr_info("Testing kprobe tracing: ");
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");
1216 ret = command_trace_probe("r:testprobe2 kprobe_trace_selftest_target "
1218 if (WARN_ON_ONCE(ret))
1219 pr_warning("error enabling function return\n");
1221 ret = target(1, 2, 3, 4, 5, 6);
1223 cleanup_all_probes();
1229 late_initcall(kprobe_trace_self_tests_init);