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 unsigned int 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, unsigned long *offset)
391 tmp = strchr(symbol, '+');
393 /* skip sign because strict_strtol doesn't accept '+' */
394 ret = strict_strtoul(tmp + 1, 0, offset);
403 #define PARAM_MAX_ARGS 16
404 #define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long))
406 static int parse_probe_arg(char *arg, struct fetch_func *ff, int is_return)
414 case 'a': /* argument */
415 ret = strict_strtoul(arg + 1, 10, ¶m);
416 if (ret || param > PARAM_MAX_ARGS)
419 ff->func = fetch_argument;
420 ff->data = (void *)param;
423 case 'r': /* retval or retaddr */
424 if (is_return && arg[1] == 'v') {
425 ff->func = fetch_retvalue;
427 } else if (is_return && arg[1] == 'a') {
433 case '%': /* named register */
434 ret = regs_query_register_offset(arg + 1);
436 ff->func = fetch_register;
437 ff->data = (void *)(unsigned long)ret;
441 case 's': /* stack */
443 ff->func = fetch_stack_address;
446 ret = strict_strtoul(arg + 1, 10, ¶m);
447 if (ret || param > PARAM_MAX_STACK)
450 ff->func = fetch_stack;
451 ff->data = (void *)param;
455 case '@': /* memory or symbol */
456 if (isdigit(arg[1])) {
457 ret = strict_strtoul(arg + 1, 0, ¶m);
460 ff->func = fetch_memory;
461 ff->data = (void *)param;
463 ret = split_symbol_offset(arg + 1, &offset);
466 ff->data = alloc_symbol_cache(arg + 1,
469 ff->func = fetch_symbol;
474 case '+': /* indirect memory */
476 tmp = strchr(arg, '(');
482 ret = strict_strtol(arg + 1, 0, &offset);
488 tmp = strrchr(arg, ')');
490 struct indirect_fetch_data *id;
492 id = kzalloc(sizeof(struct indirect_fetch_data),
497 ret = parse_probe_arg(arg, &id->orig, is_return);
501 ff->func = fetch_indirect;
502 ff->data = (void *)id;
508 /* TODO: support custom handler */
514 static int create_trace_probe(int argc, char **argv)
518 * - Add kprobe: p[:EVENT] SYMBOL[+OFFS]|ADDRESS [FETCHARGS]
519 * - Add kretprobe: r[:EVENT] SYMBOL[+0] [FETCHARGS]
521 * aN : fetch Nth of function argument. (N:0-)
522 * rv : fetch return value
523 * ra : fetch return address
524 * sa : fetch stack address
525 * sN : fetch Nth of stack (N:0-)
526 * @ADDR : fetch memory at ADDR (ADDR should be in kernel)
527 * @SYM[+|-offs] : fetch memory at SYM +|- offs (SYM is a data symbol)
528 * %REG : fetch register REG
529 * Indirect memory fetch:
530 * +|-offs(ARG) : fetch memory at ARG +|- offs address.
532 struct trace_probe *tp;
536 char *symbol = NULL, *event = NULL;
537 unsigned long offset = 0;
543 if (argv[0][0] == 'p')
545 else if (argv[0][0] == 'r')
550 if (argv[0][1] == ':') {
552 if (strlen(event) == 0) {
553 pr_info("Event name is not specifiled\n");
558 if (isdigit(argv[1][0])) {
561 /* an address specified */
562 ret = strict_strtoul(&argv[0][2], 0, (unsigned long *)&addr);
566 /* a symbol specified */
568 /* TODO: support .init module functions */
569 ret = split_symbol_offset(symbol, &offset);
572 if (offset && is_return)
575 argc -= 2; argv += 2;
579 /* Make a new event name */
580 char buf[MAX_EVENT_NAME_LEN];
582 snprintf(buf, MAX_EVENT_NAME_LEN, "%c@%s%+ld",
583 is_return ? 'r' : 'p', symbol, offset);
585 snprintf(buf, MAX_EVENT_NAME_LEN, "%c@0x%p",
586 is_return ? 'r' : 'p', addr);
587 tp = alloc_trace_probe(symbol, buf, argc);
589 tp = alloc_trace_probe(symbol, event, argc);
595 tp->rp.handler = kretprobe_trace_func;
598 tp->kp.pre_handler = kprobe_trace_func;
602 kp->symbol_name = tp->symbol;
603 kp->offset = (unsigned int)offset;
607 /* parse arguments */
609 for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
610 if (strlen(argv[i]) > MAX_ARGSTR_LEN) {
611 pr_info("Argument%d(%s) is too long.\n", i, argv[i]);
615 ret = parse_probe_arg(argv[i], &tp->args[i], is_return);
621 ret = register_trace_probe(tp);
627 free_trace_probe(tp);
631 static void cleanup_all_probes(void)
633 struct trace_probe *tp;
635 mutex_lock(&probe_lock);
636 /* TODO: Use batch unregistration */
637 while (!list_empty(&probe_list)) {
638 tp = list_entry(probe_list.next, struct trace_probe, list);
639 unregister_trace_probe(tp);
640 free_trace_probe(tp);
642 mutex_unlock(&probe_lock);
646 /* Probes listing interfaces */
647 static void *probes_seq_start(struct seq_file *m, loff_t *pos)
649 mutex_lock(&probe_lock);
650 return seq_list_start(&probe_list, *pos);
653 static void *probes_seq_next(struct seq_file *m, void *v, loff_t *pos)
655 return seq_list_next(v, &probe_list, pos);
658 static void probes_seq_stop(struct seq_file *m, void *v)
660 mutex_unlock(&probe_lock);
663 static int probes_seq_show(struct seq_file *m, void *v)
665 struct trace_probe *tp = v;
667 char buf[MAX_ARGSTR_LEN + 1];
669 seq_printf(m, "%c", probe_is_return(tp) ? 'r' : 'p');
670 seq_printf(m, ":%s", tp->call.name);
673 seq_printf(m, " %s+%u", probe_symbol(tp), probe_offset(tp));
675 seq_printf(m, " 0x%p", probe_address(tp));
677 for (i = 0; i < tp->nr_args; i++) {
678 ret = probe_arg_string(buf, MAX_ARGSTR_LEN, &tp->args[i]);
680 pr_warning("Argument%d decoding error(%d).\n", i, ret);
683 seq_printf(m, " %s", buf);
689 static const struct seq_operations probes_seq_op = {
690 .start = probes_seq_start,
691 .next = probes_seq_next,
692 .stop = probes_seq_stop,
693 .show = probes_seq_show
696 static int probes_open(struct inode *inode, struct file *file)
698 if ((file->f_mode & FMODE_WRITE) &&
699 (file->f_flags & O_TRUNC))
700 cleanup_all_probes();
702 return seq_open(file, &probes_seq_op);
705 static int command_trace_probe(const char *buf)
708 int argc = 0, ret = 0;
710 argv = argv_split(GFP_KERNEL, buf, &argc);
715 ret = create_trace_probe(argc, argv);
721 #define WRITE_BUFSIZE 128
723 static ssize_t probes_write(struct file *file, const char __user *buffer,
724 size_t count, loff_t *ppos)
731 kbuf = kmalloc(WRITE_BUFSIZE, GFP_KERNEL);
736 while (done < count) {
738 if (size >= WRITE_BUFSIZE)
739 size = WRITE_BUFSIZE - 1;
740 if (copy_from_user(kbuf, buffer + done, size)) {
745 tmp = strchr(kbuf, '\n');
748 size = tmp - kbuf + 1;
749 } else if (done + size < count) {
750 pr_warning("Line length is too long: "
751 "Should be less than %d.", WRITE_BUFSIZE);
756 /* Remove comments */
757 tmp = strchr(kbuf, '#');
761 ret = command_trace_probe(kbuf);
771 static const struct file_operations kprobe_events_ops = {
772 .owner = THIS_MODULE,
776 .release = seq_release,
777 .write = probes_write,
780 /* Probes profiling interfaces */
781 static int probes_profile_seq_show(struct seq_file *m, void *v)
783 struct trace_probe *tp = v;
785 seq_printf(m, " %-44s %15lu %15lu\n", tp->call.name, tp->nhit,
786 probe_is_return(tp) ? tp->rp.kp.nmissed : tp->kp.nmissed);
791 static const struct seq_operations profile_seq_op = {
792 .start = probes_seq_start,
793 .next = probes_seq_next,
794 .stop = probes_seq_stop,
795 .show = probes_profile_seq_show
798 static int profile_open(struct inode *inode, struct file *file)
800 return seq_open(file, &profile_seq_op);
803 static const struct file_operations kprobe_profile_ops = {
804 .owner = THIS_MODULE,
805 .open = profile_open,
808 .release = seq_release,
812 static __kprobes int kprobe_trace_func(struct kprobe *kp, struct pt_regs *regs)
814 struct trace_probe *tp = container_of(kp, struct trace_probe, kp);
815 struct kprobe_trace_entry *entry;
816 struct ring_buffer_event *event;
817 struct ring_buffer *buffer;
819 unsigned long irq_flags;
820 struct ftrace_event_call *call = &tp->call;
824 local_save_flags(irq_flags);
825 pc = preempt_count();
827 size = SIZEOF_KPROBE_TRACE_ENTRY(tp->nr_args);
829 event = trace_current_buffer_lock_reserve(&buffer, call->id, size,
834 entry = ring_buffer_event_data(event);
835 entry->nargs = tp->nr_args;
836 entry->ip = (unsigned long)kp->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(buffer, call, entry, event))
841 trace_nowake_buffer_unlock_commit(buffer, event, irq_flags, pc);
845 /* Kretprobe handler */
846 static __kprobes int kretprobe_trace_func(struct kretprobe_instance *ri,
847 struct pt_regs *regs)
849 struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
850 struct kretprobe_trace_entry *entry;
851 struct ring_buffer_event *event;
852 struct ring_buffer *buffer;
854 unsigned long irq_flags;
855 struct ftrace_event_call *call = &tp->call;
857 local_save_flags(irq_flags);
858 pc = preempt_count();
860 size = SIZEOF_KRETPROBE_TRACE_ENTRY(tp->nr_args);
862 event = trace_current_buffer_lock_reserve(&buffer, call->id, size,
867 entry = ring_buffer_event_data(event);
868 entry->nargs = tp->nr_args;
869 entry->func = (unsigned long)probe_address(tp);
870 entry->ret_ip = (unsigned long)ri->ret_addr;
871 for (i = 0; i < tp->nr_args; i++)
872 entry->args[i] = call_fetch(&tp->args[i], regs);
874 if (!filter_current_check_discard(buffer, call, entry, event))
875 trace_nowake_buffer_unlock_commit(buffer, event, irq_flags, pc);
880 /* Event entry printers */
882 print_kprobe_event(struct trace_iterator *iter, int flags)
884 struct kprobe_trace_entry *field;
885 struct trace_seq *s = &iter->seq;
888 field = (struct kprobe_trace_entry *)iter->ent;
890 if (!seq_print_ip_sym(s, field->ip, flags | TRACE_ITER_SYM_OFFSET))
893 if (!trace_seq_puts(s, ":"))
896 for (i = 0; i < field->nargs; i++)
897 if (!trace_seq_printf(s, " 0x%lx", field->args[i]))
900 if (!trace_seq_puts(s, "\n"))
903 return TRACE_TYPE_HANDLED;
905 return TRACE_TYPE_PARTIAL_LINE;
909 print_kretprobe_event(struct trace_iterator *iter, int flags)
911 struct kretprobe_trace_entry *field;
912 struct trace_seq *s = &iter->seq;
915 field = (struct kretprobe_trace_entry *)iter->ent;
917 if (!seq_print_ip_sym(s, field->ret_ip, flags | TRACE_ITER_SYM_OFFSET))
920 if (!trace_seq_puts(s, " <- "))
923 if (!seq_print_ip_sym(s, field->func, flags & ~TRACE_ITER_SYM_OFFSET))
926 if (!trace_seq_puts(s, ":"))
929 for (i = 0; i < field->nargs; i++)
930 if (!trace_seq_printf(s, " 0x%lx", field->args[i]))
933 if (!trace_seq_puts(s, "\n"))
936 return TRACE_TYPE_HANDLED;
938 return TRACE_TYPE_PARTIAL_LINE;
941 static int probe_event_enable(struct ftrace_event_call *call)
943 struct trace_probe *tp = (struct trace_probe *)call->data;
945 if (probe_is_return(tp))
946 return enable_kretprobe(&tp->rp);
948 return enable_kprobe(&tp->kp);
951 static void probe_event_disable(struct ftrace_event_call *call)
953 struct trace_probe *tp = (struct trace_probe *)call->data;
955 if (probe_is_return(tp))
956 disable_kretprobe(&tp->rp);
958 disable_kprobe(&tp->kp);
961 static int probe_event_raw_init(struct ftrace_event_call *event_call)
963 INIT_LIST_HEAD(&event_call->fields);
969 #define DEFINE_FIELD(type, item, name, is_signed) \
971 ret = trace_define_field(event_call, #type, name, \
972 offsetof(typeof(field), item), \
973 sizeof(field.item), is_signed, \
979 static int kprobe_event_define_fields(struct ftrace_event_call *event_call)
982 struct kprobe_trace_entry field;
983 char buf[MAX_ARGSTR_LEN + 1];
984 struct trace_probe *tp = (struct trace_probe *)event_call->data;
986 ret = trace_define_common_fields(event_call);
990 DEFINE_FIELD(unsigned long, ip, "ip", 0);
991 DEFINE_FIELD(int, nargs, "nargs", 1);
992 for (i = 0; i < tp->nr_args; i++) {
993 /* Set argN as a field */
994 sprintf(buf, "arg%d", i);
995 DEFINE_FIELD(unsigned long, args[i], buf, 0);
996 /* Set argument string as an alias field */
997 ret = probe_arg_string(buf, MAX_ARGSTR_LEN, &tp->args[i]);
1000 DEFINE_FIELD(unsigned long, args[i], buf, 0);
1005 static int kretprobe_event_define_fields(struct ftrace_event_call *event_call)
1008 struct kretprobe_trace_entry field;
1009 char buf[MAX_ARGSTR_LEN + 1];
1010 struct trace_probe *tp = (struct trace_probe *)event_call->data;
1012 ret = trace_define_common_fields(event_call);
1016 DEFINE_FIELD(unsigned long, func, "func", 0);
1017 DEFINE_FIELD(unsigned long, ret_ip, "ret_ip", 0);
1018 DEFINE_FIELD(int, nargs, "nargs", 1);
1019 for (i = 0; i < tp->nr_args; i++) {
1020 /* Set argN as a field */
1021 sprintf(buf, "arg%d", i);
1022 DEFINE_FIELD(unsigned long, args[i], buf, 0);
1023 /* Set argument string as an alias field */
1024 ret = probe_arg_string(buf, MAX_ARGSTR_LEN, &tp->args[i]);
1027 DEFINE_FIELD(unsigned long, args[i], buf, 0);
1032 static int __probe_event_show_format(struct trace_seq *s,
1033 struct trace_probe *tp, const char *fmt,
1037 char buf[MAX_ARGSTR_LEN + 1];
1040 for (i = 0; i < tp->nr_args; i++) {
1041 ret = probe_arg_string(buf, MAX_ARGSTR_LEN, &tp->args[i]);
1044 if (!trace_seq_printf(s, "\talias: %s;\toriginal: arg%d;\n",
1049 if (!trace_seq_printf(s, "\nprint fmt: \"%s", fmt))
1052 for (i = 0; i < tp->nr_args; i++)
1053 if (!trace_seq_puts(s, " 0x%lx"))
1056 if (!trace_seq_printf(s, "\", %s", arg))
1059 for (i = 0; i < tp->nr_args; i++)
1060 if (!trace_seq_printf(s, ", arg%d", i))
1063 return trace_seq_puts(s, "\n");
1067 #define SHOW_FIELD(type, item, name) \
1069 ret = trace_seq_printf(s, "\tfield: " #type " %s;\t" \
1070 "offset:%u;\tsize:%u;\n", name, \
1071 (unsigned int)offsetof(typeof(field), item),\
1072 (unsigned int)sizeof(type)); \
1077 static int kprobe_event_show_format(struct ftrace_event_call *call,
1078 struct trace_seq *s)
1080 struct kprobe_trace_entry field __attribute__((unused));
1083 struct trace_probe *tp = (struct trace_probe *)call->data;
1085 SHOW_FIELD(unsigned long, ip, "ip");
1086 SHOW_FIELD(int, nargs, "nargs");
1089 for (i = 0; i < tp->nr_args; i++) {
1090 sprintf(buf, "arg%d", i);
1091 SHOW_FIELD(unsigned long, args[i], buf);
1093 trace_seq_puts(s, "\n");
1095 return __probe_event_show_format(s, tp, "%lx:", "ip");
1098 static int kretprobe_event_show_format(struct ftrace_event_call *call,
1099 struct trace_seq *s)
1101 struct kretprobe_trace_entry field __attribute__((unused));
1104 struct trace_probe *tp = (struct trace_probe *)call->data;
1106 SHOW_FIELD(unsigned long, func, "func");
1107 SHOW_FIELD(unsigned long, ret_ip, "ret_ip");
1108 SHOW_FIELD(int, nargs, "nargs");
1111 for (i = 0; i < tp->nr_args; i++) {
1112 sprintf(buf, "arg%d", i);
1113 SHOW_FIELD(unsigned long, args[i], buf);
1115 trace_seq_puts(s, "\n");
1117 return __probe_event_show_format(s, tp, "%lx <- %lx:",
1121 static int register_probe_event(struct trace_probe *tp)
1123 struct ftrace_event_call *call = &tp->call;
1126 /* Initialize ftrace_event_call */
1127 call->system = "kprobes";
1128 if (probe_is_return(tp)) {
1129 tp->event.trace = print_kretprobe_event;
1130 call->raw_init = probe_event_raw_init;
1131 call->show_format = kretprobe_event_show_format;
1132 call->define_fields = kretprobe_event_define_fields;
1134 tp->event.trace = print_kprobe_event;
1135 call->raw_init = probe_event_raw_init;
1136 call->show_format = kprobe_event_show_format;
1137 call->define_fields = kprobe_event_define_fields;
1139 call->event = &tp->event;
1140 call->id = register_ftrace_event(&tp->event);
1144 call->regfunc = probe_event_enable;
1145 call->unregfunc = probe_event_disable;
1147 ret = trace_add_event_call(call);
1149 pr_info("Failed to register kprobe event: %s\n", call->name);
1150 unregister_ftrace_event(&tp->event);
1155 static void unregister_probe_event(struct trace_probe *tp)
1157 /* tp->event is unregistered in trace_remove_event_call() */
1158 trace_remove_event_call(&tp->call);
1161 /* Make a debugfs interface for controling probe points */
1162 static __init int init_kprobe_trace(void)
1164 struct dentry *d_tracer;
1165 struct dentry *entry;
1167 d_tracer = tracing_init_dentry();
1171 entry = debugfs_create_file("kprobe_events", 0644, d_tracer,
1172 NULL, &kprobe_events_ops);
1174 /* Event list interface */
1176 pr_warning("Could not create debugfs "
1177 "'kprobe_events' entry\n");
1179 /* Profile interface */
1180 entry = debugfs_create_file("kprobe_profile", 0444, d_tracer,
1181 NULL, &kprobe_profile_ops);
1184 pr_warning("Could not create debugfs "
1185 "'kprobe_profile' entry\n");
1188 fs_initcall(init_kprobe_trace);
1191 #ifdef CONFIG_FTRACE_STARTUP_TEST
1193 static int kprobe_trace_selftest_target(int a1, int a2, int a3,
1194 int a4, int a5, int a6)
1196 return a1 + a2 + a3 + a4 + a5 + a6;
1199 static __init int kprobe_trace_self_tests_init(void)
1202 int (*target)(int, int, int, int, int, int);
1204 target = kprobe_trace_selftest_target;
1206 pr_info("Testing kprobe tracing: ");
1208 ret = command_trace_probe("p:testprobe kprobe_trace_selftest_target "
1209 "a1 a2 a3 a4 a5 a6");
1210 if (WARN_ON_ONCE(ret))
1211 pr_warning("error enabling function entry\n");
1213 ret = command_trace_probe("r:testprobe2 kprobe_trace_selftest_target "
1215 if (WARN_ON_ONCE(ret))
1216 pr_warning("error enabling function return\n");
1218 ret = target(1, 2, 3, 4, 5, 6);
1220 cleanup_all_probes();
1226 late_initcall(kprobe_trace_self_tests_init);