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;
187 const char *symbol; /* symbol name */
188 struct ftrace_event_call call;
189 unsigned int nr_args;
190 struct fetch_func args[];
193 #define SIZEOF_TRACE_PROBE(n) \
194 (offsetof(struct trace_probe, args) + \
195 (sizeof(struct fetch_func) * (n)))
197 static int kprobe_trace_func(struct kprobe *kp, struct pt_regs *regs);
198 static int kretprobe_trace_func(struct kretprobe_instance *ri,
199 struct pt_regs *regs);
201 static __kprobes int probe_is_return(struct trace_probe *tp)
203 return (tp->rp.handler == kretprobe_trace_func);
206 static __kprobes const char *probe_symbol(struct trace_probe *tp)
208 return tp->symbol ? tp->symbol : "unknown";
211 static __kprobes long probe_offset(struct trace_probe *tp)
213 return (probe_is_return(tp)) ? tp->rp.kp.offset : tp->kp.offset;
216 static __kprobes void *probe_address(struct trace_probe *tp)
218 return (probe_is_return(tp)) ? tp->rp.kp.addr : tp->kp.addr;
221 static int trace_arg_string(char *buf, size_t n, struct fetch_func *ff)
225 if (ff->func == fetch_argument)
226 ret = snprintf(buf, n, "a%lu", (unsigned long)ff->data);
227 else if (ff->func == fetch_register) {
229 name = regs_query_register_name((unsigned int)((long)ff->data));
230 ret = snprintf(buf, n, "%%%s", name);
231 } else if (ff->func == fetch_stack)
232 ret = snprintf(buf, n, "s%lu", (unsigned long)ff->data);
233 else if (ff->func == fetch_memory)
234 ret = snprintf(buf, n, "@0x%p", ff->data);
235 else if (ff->func == fetch_symbol) {
236 struct symbol_cache *sc = ff->data;
237 ret = snprintf(buf, n, "@%s%+ld", sc->symbol, sc->offset);
238 } else if (ff->func == fetch_retvalue)
239 ret = snprintf(buf, n, "rv");
240 else if (ff->func == fetch_ip)
241 ret = snprintf(buf, n, "ra");
242 else if (ff->func == fetch_stack_address)
243 ret = snprintf(buf, n, "sa");
244 else if (ff->func == fetch_indirect) {
245 struct indirect_fetch_data *id = ff->data;
247 ret = snprintf(buf, n, "%+ld(", id->offset);
251 ret = trace_arg_string(buf + l, n - l, &id->orig);
255 ret = snprintf(buf + l, n - l, ")");
264 static int register_probe_event(struct trace_probe *tp);
265 static void unregister_probe_event(struct trace_probe *tp);
267 static DEFINE_MUTEX(probe_lock);
268 static LIST_HEAD(probe_list);
270 static struct trace_probe *alloc_trace_probe(const char *symbol,
271 const char *event, int nargs)
273 struct trace_probe *tp;
275 tp = kzalloc(SIZEOF_TRACE_PROBE(nargs), GFP_KERNEL);
277 return ERR_PTR(-ENOMEM);
280 tp->symbol = kstrdup(symbol, GFP_KERNEL);
286 tp->call.name = kstrdup(event, GFP_KERNEL);
290 INIT_LIST_HEAD(&tp->list);
295 return ERR_PTR(-ENOMEM);
298 static void free_trace_probe(struct trace_probe *tp)
302 for (i = 0; i < tp->nr_args; i++)
303 if (tp->args[i].func == fetch_symbol)
304 free_symbol_cache(tp->args[i].data);
305 else if (tp->args[i].func == fetch_indirect)
306 free_indirect_fetch_data(tp->args[i].data);
308 kfree(tp->call.name);
313 static struct trace_probe *find_probe_event(const char *event)
315 struct trace_probe *tp;
317 list_for_each_entry(tp, &probe_list, list)
318 if (!strcmp(tp->call.name, event))
323 static void __unregister_trace_probe(struct trace_probe *tp)
325 if (probe_is_return(tp))
326 unregister_kretprobe(&tp->rp);
328 unregister_kprobe(&tp->kp);
331 /* Unregister a trace_probe and probe_event: call with locking probe_lock */
332 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 */
363 old_tp = find_probe_event(tp->call.name);
365 /* delete old event */
366 unregister_trace_probe(old_tp);
367 free_trace_probe(old_tp);
369 ret = register_probe_event(tp);
371 pr_warning("Faild to register probe event(%d)\n", ret);
372 __unregister_trace_probe(tp);
374 list_add_tail(&tp->list, &probe_list);
376 mutex_unlock(&probe_lock);
380 /* Split symbol and offset. */
381 static int split_symbol_offset(char *symbol, long *offset)
389 tmp = strchr(symbol, '+');
391 tmp = strchr(symbol, '-');
394 /* skip sign because strict_strtol doesn't accept '+' */
395 ret = strict_strtol(tmp + 1, 0, offset);
399 *offset = -(*offset);
406 #define PARAM_MAX_ARGS 16
407 #define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long))
409 static int parse_trace_arg(char *arg, struct fetch_func *ff, int is_return)
417 case 'a': /* argument */
418 ret = strict_strtoul(arg + 1, 10, ¶m);
419 if (ret || param > PARAM_MAX_ARGS)
422 ff->func = fetch_argument;
423 ff->data = (void *)param;
426 case 'r': /* retval or retaddr */
427 if (is_return && arg[1] == 'v') {
428 ff->func = fetch_retvalue;
430 } else if (is_return && arg[1] == 'a') {
436 case '%': /* named register */
437 ret = regs_query_register_offset(arg + 1);
439 ff->func = fetch_register;
440 ff->data = (void *)(unsigned long)ret;
444 case 's': /* stack */
446 ff->func = fetch_stack_address;
449 ret = strict_strtoul(arg + 1, 10, ¶m);
450 if (ret || param > PARAM_MAX_STACK)
453 ff->func = fetch_stack;
454 ff->data = (void *)param;
458 case '@': /* memory or symbol */
459 if (isdigit(arg[1])) {
460 ret = strict_strtoul(arg + 1, 0, ¶m);
463 ff->func = fetch_memory;
464 ff->data = (void *)param;
466 ret = split_symbol_offset(arg + 1, &offset);
469 ff->data = alloc_symbol_cache(arg + 1,
472 ff->func = fetch_symbol;
477 case '+': /* indirect memory */
479 tmp = strchr(arg, '(');
485 ret = strict_strtol(arg + 1, 0, &offset);
491 tmp = strrchr(arg, ')');
493 struct indirect_fetch_data *id;
495 id = kzalloc(sizeof(struct indirect_fetch_data),
500 ret = parse_trace_arg(arg, &id->orig, is_return);
504 ff->func = fetch_indirect;
505 ff->data = (void *)id;
511 /* TODO: support custom handler */
517 static int create_trace_probe(int argc, char **argv)
521 * - Add kprobe: p[:EVENT] SYMBOL[+OFFS|-OFFS]|ADDRESS [FETCHARGS]
522 * - Add kretprobe: r[:EVENT] SYMBOL[+0] [FETCHARGS]
524 * aN : fetch Nth of function argument. (N:0-)
525 * rv : fetch return value
526 * ra : fetch return address
527 * sa : fetch stack address
528 * sN : fetch Nth of stack (N:0-)
529 * @ADDR : fetch memory at ADDR (ADDR should be in kernel)
530 * @SYM[+|-offs] : fetch memory at SYM +|- offs (SYM is a data symbol)
531 * %REG : fetch register REG
532 * Indirect memory fetch:
533 * +|-offs(ARG) : fetch memory at ARG +|- offs address.
535 struct trace_probe *tp;
539 char *symbol = NULL, *event = NULL;
546 if (argv[0][0] == 'p')
548 else if (argv[0][0] == 'r')
553 if (argv[0][1] == ':') {
555 if (strlen(event) == 0) {
556 pr_info("Event name is not specifiled\n");
561 if (isdigit(argv[1][0])) {
564 /* an address specified */
565 ret = strict_strtoul(&argv[0][2], 0, (unsigned long *)&addr);
569 /* a symbol specified */
571 /* TODO: support .init module functions */
572 ret = split_symbol_offset(symbol, &offset);
575 if (offset && is_return)
578 argc -= 2; argv += 2;
582 /* Make a new event name */
583 char buf[MAX_EVENT_NAME_LEN];
585 snprintf(buf, MAX_EVENT_NAME_LEN, "%c@%s%+ld",
586 is_return ? 'r' : 'p', symbol, offset);
588 snprintf(buf, MAX_EVENT_NAME_LEN, "%c@0x%p",
589 is_return ? 'r' : 'p', addr);
590 tp = alloc_trace_probe(symbol, buf, argc);
592 tp = alloc_trace_probe(symbol, event, argc);
598 tp->rp.handler = kretprobe_trace_func;
601 tp->kp.pre_handler = kprobe_trace_func;
605 kp->symbol_name = tp->symbol;
610 /* parse arguments */
612 for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
613 if (strlen(argv[i]) > MAX_ARGSTR_LEN) {
614 pr_info("Argument%d(%s) is too long.\n", i, argv[i]);
618 ret = parse_trace_arg(argv[i], &tp->args[i], is_return);
624 ret = register_trace_probe(tp);
630 free_trace_probe(tp);
634 static void cleanup_all_probes(void)
636 struct trace_probe *tp;
638 mutex_lock(&probe_lock);
639 /* TODO: Use batch unregistration */
640 while (!list_empty(&probe_list)) {
641 tp = list_entry(probe_list.next, struct trace_probe, list);
642 unregister_trace_probe(tp);
643 free_trace_probe(tp);
645 mutex_unlock(&probe_lock);
649 /* Probes listing interfaces */
650 static void *probes_seq_start(struct seq_file *m, loff_t *pos)
652 mutex_lock(&probe_lock);
653 return seq_list_start(&probe_list, *pos);
656 static void *probes_seq_next(struct seq_file *m, void *v, loff_t *pos)
658 return seq_list_next(v, &probe_list, pos);
661 static void probes_seq_stop(struct seq_file *m, void *v)
663 mutex_unlock(&probe_lock);
666 static int probes_seq_show(struct seq_file *m, void *v)
668 struct trace_probe *tp = v;
670 char buf[MAX_ARGSTR_LEN + 1];
672 seq_printf(m, "%c", probe_is_return(tp) ? 'r' : 'p');
673 seq_printf(m, ":%s", tp->call.name);
676 seq_printf(m, " %s%+ld", probe_symbol(tp), probe_offset(tp));
678 seq_printf(m, " 0x%p", probe_address(tp));
680 for (i = 0; i < tp->nr_args; i++) {
681 ret = trace_arg_string(buf, MAX_ARGSTR_LEN, &tp->args[i]);
683 pr_warning("Argument%d decoding error(%d).\n", i, ret);
686 seq_printf(m, " %s", buf);
692 static const struct seq_operations probes_seq_op = {
693 .start = probes_seq_start,
694 .next = probes_seq_next,
695 .stop = probes_seq_stop,
696 .show = probes_seq_show
699 static int probes_open(struct inode *inode, struct file *file)
701 if ((file->f_mode & FMODE_WRITE) &&
702 (file->f_flags & O_TRUNC))
703 cleanup_all_probes();
705 return seq_open(file, &probes_seq_op);
708 static int command_trace_probe(const char *buf)
711 int argc = 0, ret = 0;
713 argv = argv_split(GFP_KERNEL, buf, &argc);
718 ret = create_trace_probe(argc, argv);
724 #define WRITE_BUFSIZE 128
726 static ssize_t probes_write(struct file *file, const char __user *buffer,
727 size_t count, loff_t *ppos)
734 kbuf = kmalloc(WRITE_BUFSIZE, GFP_KERNEL);
739 while (done < count) {
741 if (size >= WRITE_BUFSIZE)
742 size = WRITE_BUFSIZE - 1;
743 if (copy_from_user(kbuf, buffer + done, size)) {
748 tmp = strchr(kbuf, '\n');
751 size = tmp - kbuf + 1;
752 } else if (done + size < count) {
753 pr_warning("Line length is too long: "
754 "Should be less than %d.", WRITE_BUFSIZE);
759 /* Remove comments */
760 tmp = strchr(kbuf, '#');
764 ret = command_trace_probe(kbuf);
774 static const struct file_operations kprobe_events_ops = {
775 .owner = THIS_MODULE,
779 .release = seq_release,
780 .write = probes_write,
784 static __kprobes int kprobe_trace_func(struct kprobe *kp, struct pt_regs *regs)
786 struct trace_probe *tp = container_of(kp, struct trace_probe, kp);
787 struct kprobe_trace_entry *entry;
788 struct ring_buffer_event *event;
790 unsigned long irq_flags;
791 struct ftrace_event_call *call = &tp->call;
793 local_save_flags(irq_flags);
794 pc = preempt_count();
796 size = SIZEOF_KPROBE_TRACE_ENTRY(tp->nr_args);
798 event = trace_current_buffer_lock_reserve(TRACE_KPROBE, size,
803 entry = ring_buffer_event_data(event);
804 entry->nargs = tp->nr_args;
805 entry->ip = (unsigned long)kp->addr;
806 for (i = 0; i < tp->nr_args; i++)
807 entry->args[i] = call_fetch(&tp->args[i], regs);
809 if (!filter_current_check_discard(call, entry, event))
810 trace_nowake_buffer_unlock_commit(event, irq_flags, pc);
814 /* Kretprobe handler */
815 static __kprobes int kretprobe_trace_func(struct kretprobe_instance *ri,
816 struct pt_regs *regs)
818 struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
819 struct kretprobe_trace_entry *entry;
820 struct ring_buffer_event *event;
822 unsigned long irq_flags;
823 struct ftrace_event_call *call = &tp->call;
825 local_save_flags(irq_flags);
826 pc = preempt_count();
828 size = SIZEOF_KRETPROBE_TRACE_ENTRY(tp->nr_args);
830 event = trace_current_buffer_lock_reserve(TRACE_KRETPROBE, size,
835 entry = ring_buffer_event_data(event);
836 entry->nargs = tp->nr_args;
837 entry->func = (unsigned long)probe_address(tp);
838 entry->ret_ip = (unsigned long)ri->ret_addr;
839 for (i = 0; i < tp->nr_args; i++)
840 entry->args[i] = call_fetch(&tp->args[i], regs);
842 if (!filter_current_check_discard(call, entry, event))
843 trace_nowake_buffer_unlock_commit(event, irq_flags, pc);
848 /* Event entry printers */
850 print_kprobe_event(struct trace_iterator *iter, int flags)
852 struct kprobe_trace_entry *field;
853 struct trace_seq *s = &iter->seq;
856 trace_assign_type(field, iter->ent);
858 if (!seq_print_ip_sym(s, field->ip, flags | TRACE_ITER_SYM_OFFSET))
861 if (!trace_seq_puts(s, ":"))
864 for (i = 0; i < field->nargs; i++)
865 if (!trace_seq_printf(s, " 0x%lx", field->args[i]))
868 if (!trace_seq_puts(s, "\n"))
871 return TRACE_TYPE_HANDLED;
873 return TRACE_TYPE_PARTIAL_LINE;
877 print_kretprobe_event(struct trace_iterator *iter, int flags)
879 struct kretprobe_trace_entry *field;
880 struct trace_seq *s = &iter->seq;
883 trace_assign_type(field, iter->ent);
885 if (!seq_print_ip_sym(s, field->ret_ip, flags | TRACE_ITER_SYM_OFFSET))
888 if (!trace_seq_puts(s, " <- "))
891 if (!seq_print_ip_sym(s, field->func, flags & ~TRACE_ITER_SYM_OFFSET))
894 if (!trace_seq_puts(s, ":"))
897 for (i = 0; i < field->nargs; i++)
898 if (!trace_seq_printf(s, " 0x%lx", field->args[i]))
901 if (!trace_seq_puts(s, "\n"))
904 return TRACE_TYPE_HANDLED;
906 return TRACE_TYPE_PARTIAL_LINE;
909 static struct trace_event kprobe_trace_event = {
910 .type = TRACE_KPROBE,
911 .trace = print_kprobe_event,
914 static struct trace_event kretprobe_trace_event = {
915 .type = TRACE_KRETPROBE,
916 .trace = print_kretprobe_event,
919 static int probe_event_enable(struct ftrace_event_call *call)
921 struct trace_probe *tp = (struct trace_probe *)call->data;
923 if (probe_is_return(tp))
924 return enable_kretprobe(&tp->rp);
926 return enable_kprobe(&tp->kp);
929 static void probe_event_disable(struct ftrace_event_call *call)
931 struct trace_probe *tp = (struct trace_probe *)call->data;
933 if (probe_is_return(tp))
934 disable_kretprobe(&tp->rp);
936 disable_kprobe(&tp->kp);
939 static int probe_event_raw_init(struct ftrace_event_call *event_call)
941 INIT_LIST_HEAD(&event_call->fields);
942 init_preds(event_call);
947 #define DEFINE_FIELD(type, item, name, is_signed) \
949 ret = trace_define_field(event_call, #type, name, \
950 offsetof(typeof(field), item), \
951 sizeof(field.item), is_signed, \
957 static int kprobe_event_define_fields(struct ftrace_event_call *event_call)
960 struct kprobe_trace_entry field;
961 char buf[MAX_ARGSTR_LEN + 1];
962 struct trace_probe *tp = (struct trace_probe *)event_call->data;
964 ret = trace_define_common_fields(event_call);
968 DEFINE_FIELD(unsigned long, ip, "ip", 0);
969 DEFINE_FIELD(int, nargs, "nargs", 1);
970 for (i = 0; i < tp->nr_args; i++) {
971 /* Set argN as a field */
972 sprintf(buf, "arg%d", i);
973 DEFINE_FIELD(unsigned long, args[i], buf, 0);
974 /* Set argument string as an alias field */
975 ret = trace_arg_string(buf, MAX_ARGSTR_LEN, &tp->args[i]);
978 DEFINE_FIELD(unsigned long, args[i], buf, 0);
983 static int kretprobe_event_define_fields(struct ftrace_event_call *event_call)
986 struct kretprobe_trace_entry field;
987 char buf[MAX_ARGSTR_LEN + 1];
988 struct trace_probe *tp = (struct trace_probe *)event_call->data;
990 ret = trace_define_common_fields(event_call);
994 DEFINE_FIELD(unsigned long, func, "func", 0);
995 DEFINE_FIELD(unsigned long, ret_ip, "ret_ip", 0);
996 DEFINE_FIELD(int, nargs, "nargs", 1);
997 for (i = 0; i < tp->nr_args; i++) {
998 /* Set argN as a field */
999 sprintf(buf, "arg%d", i);
1000 DEFINE_FIELD(unsigned long, args[i], buf, 0);
1001 /* Set argument string as an alias field */
1002 ret = trace_arg_string(buf, MAX_ARGSTR_LEN, &tp->args[i]);
1005 DEFINE_FIELD(unsigned long, args[i], buf, 0);
1010 static int __probe_event_show_format(struct trace_seq *s,
1011 struct trace_probe *tp, const char *fmt,
1015 char buf[MAX_ARGSTR_LEN + 1];
1018 for (i = 0; i < tp->nr_args; i++) {
1019 ret = trace_arg_string(buf, MAX_ARGSTR_LEN, &tp->args[i]);
1022 if (!trace_seq_printf(s, "\talias: %s;\toriginal: arg%d;\n",
1027 if (!trace_seq_printf(s, "\nprint fmt: \"%s", fmt))
1030 for (i = 0; i < tp->nr_args; i++)
1031 if (!trace_seq_puts(s, " 0x%lx"))
1034 if (!trace_seq_printf(s, "\", %s", arg))
1037 for (i = 0; i < tp->nr_args; i++)
1038 if (!trace_seq_printf(s, ", arg%d", i))
1041 return trace_seq_puts(s, "\n");
1045 #define SHOW_FIELD(type, item, name) \
1047 ret = trace_seq_printf(s, "\tfield: " #type " %s;\t" \
1048 "offset:%u;tsize:%u;\n", name, \
1049 (unsigned int)offsetof(typeof(field), item),\
1050 (unsigned int)sizeof(type)); \
1055 static int kprobe_event_show_format(struct ftrace_event_call *call,
1056 struct trace_seq *s)
1058 struct kprobe_trace_entry field __attribute__((unused));
1061 struct trace_probe *tp = (struct trace_probe *)call->data;
1063 SHOW_FIELD(unsigned long, ip, "ip");
1064 SHOW_FIELD(int, nargs, "nargs");
1067 for (i = 0; i < tp->nr_args; i++) {
1068 sprintf(buf, "arg%d", i);
1069 SHOW_FIELD(unsigned long, args[i], buf);
1071 trace_seq_puts(s, "\n");
1073 return __probe_event_show_format(s, tp, "%lx:", "ip");
1076 static int kretprobe_event_show_format(struct ftrace_event_call *call,
1077 struct trace_seq *s)
1079 struct kretprobe_trace_entry field __attribute__((unused));
1082 struct trace_probe *tp = (struct trace_probe *)call->data;
1084 SHOW_FIELD(unsigned long, func, "func");
1085 SHOW_FIELD(unsigned long, ret_ip, "ret_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 <- %lx:",
1099 static int register_probe_event(struct trace_probe *tp)
1101 struct ftrace_event_call *call = &tp->call;
1104 /* Initialize ftrace_event_call */
1105 call->system = "kprobes";
1106 if (probe_is_return(tp)) {
1107 call->event = &kretprobe_trace_event;
1108 call->id = TRACE_KRETPROBE;
1109 call->raw_init = probe_event_raw_init;
1110 call->show_format = kretprobe_event_show_format;
1111 call->define_fields = kretprobe_event_define_fields;
1113 call->event = &kprobe_trace_event;
1114 call->id = TRACE_KPROBE;
1115 call->raw_init = probe_event_raw_init;
1116 call->show_format = kprobe_event_show_format;
1117 call->define_fields = kprobe_event_define_fields;
1120 call->regfunc = probe_event_enable;
1121 call->unregfunc = probe_event_disable;
1123 ret = trace_add_event_call(call);
1125 pr_info("Failed to register kprobe event: %s\n", call->name);
1129 static void unregister_probe_event(struct trace_probe *tp)
1132 * Prevent to unregister event itself because the event is shared
1133 * among other probes.
1135 tp->call.event = NULL;
1136 trace_remove_event_call(&tp->call);
1139 /* Make a debugfs interface for controling probe points */
1140 static __init int init_kprobe_trace(void)
1142 struct dentry *d_tracer;
1143 struct dentry *entry;
1146 ret = register_ftrace_event(&kprobe_trace_event);
1148 pr_warning("Could not register kprobe_trace_event type.\n");
1151 ret = register_ftrace_event(&kretprobe_trace_event);
1153 pr_warning("Could not register kretprobe_trace_event type.\n");
1157 d_tracer = tracing_init_dentry();
1161 entry = debugfs_create_file("kprobe_events", 0644, d_tracer,
1162 NULL, &kprobe_events_ops);
1165 pr_warning("Could not create debugfs "
1166 "'kprobe_events' entry\n");
1169 fs_initcall(init_kprobe_trace);
1172 #ifdef CONFIG_FTRACE_STARTUP_TEST
1174 static int kprobe_trace_selftest_target(int a1, int a2, int a3,
1175 int a4, int a5, int a6)
1177 return a1 + a2 + a3 + a4 + a5 + a6;
1180 static __init int kprobe_trace_self_tests_init(void)
1183 int (*target)(int, int, int, int, int, int);
1185 target = kprobe_trace_selftest_target;
1187 pr_info("Testing kprobe tracing: ");
1189 ret = command_trace_probe("p:testprobe kprobe_trace_selftest_target "
1190 "a1 a2 a3 a4 a5 a6");
1191 if (WARN_ON_ONCE(ret))
1192 pr_warning("error enabling function entry\n");
1194 ret = command_trace_probe("r:testprobe2 kprobe_trace_selftest_target "
1196 if (WARN_ON_ONCE(ret))
1197 pr_warning("error enabling function return\n");
1199 ret = target(1, 2, 3, 4, 5, 6);
1201 cleanup_all_probes();
1207 late_initcall(kprobe_trace_self_tests_init);