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>
31 #include <linux/perf_counter.h>
34 #include "trace_output.h"
36 #define MAX_TRACE_ARGS 128
37 #define MAX_ARGSTR_LEN 63
38 #define MAX_EVENT_NAME_LEN 64
39 #define KPROBE_EVENT_SYSTEM "kprobes"
41 /* currently, trace_kprobe only supports X86. */
44 unsigned long (*func)(struct pt_regs *, void *);
48 static __kprobes unsigned long call_fetch(struct fetch_func *f,
51 return f->func(regs, f->data);
55 static __kprobes unsigned long fetch_register(struct pt_regs *regs,
58 return regs_get_register(regs, (unsigned int)((unsigned long)offset));
61 static __kprobes unsigned long fetch_stack(struct pt_regs *regs,
64 return regs_get_kernel_stack_nth(regs,
65 (unsigned int)((unsigned long)num));
68 static __kprobes unsigned long fetch_memory(struct pt_regs *regs, void *addr)
72 if (probe_kernel_address(addr, retval))
77 static __kprobes unsigned long fetch_argument(struct pt_regs *regs, void *num)
79 return regs_get_argument_nth(regs, (unsigned int)((unsigned long)num));
82 static __kprobes unsigned long fetch_retvalue(struct pt_regs *regs,
85 return regs_return_value(regs);
88 static __kprobes unsigned long fetch_ip(struct pt_regs *regs, void *dummy)
90 return instruction_pointer(regs);
93 static __kprobes unsigned long fetch_stack_address(struct pt_regs *regs,
96 return kernel_stack_pointer(regs);
99 /* Memory fetching by symbol */
100 struct symbol_cache {
106 static unsigned long update_symbol_cache(struct symbol_cache *sc)
108 sc->addr = (unsigned long)kallsyms_lookup_name(sc->symbol);
110 sc->addr += sc->offset;
114 static void free_symbol_cache(struct symbol_cache *sc)
120 static struct symbol_cache *alloc_symbol_cache(const char *sym, long offset)
122 struct symbol_cache *sc;
124 if (!sym || strlen(sym) == 0)
126 sc = kzalloc(sizeof(struct symbol_cache), GFP_KERNEL);
130 sc->symbol = kstrdup(sym, GFP_KERNEL);
137 update_symbol_cache(sc);
141 static __kprobes unsigned long fetch_symbol(struct pt_regs *regs, void *data)
143 struct symbol_cache *sc = data;
146 return fetch_memory(regs, (void *)sc->addr);
151 /* Special indirect memory access interface */
152 struct indirect_fetch_data {
153 struct fetch_func orig;
157 static __kprobes unsigned long fetch_indirect(struct pt_regs *regs, void *data)
159 struct indirect_fetch_data *ind = data;
162 addr = call_fetch(&ind->orig, regs);
165 return fetch_memory(regs, (void *)addr);
170 static __kprobes void free_indirect_fetch_data(struct indirect_fetch_data *data)
172 if (data->orig.func == fetch_indirect)
173 free_indirect_fetch_data(data->orig.data);
174 else if (data->orig.func == fetch_symbol)
175 free_symbol_cache(data->orig.data);
180 * Kprobe tracer core functions
184 struct fetch_func fetch;
189 struct list_head list;
190 struct kretprobe rp; /* Use rp.kp for kprobe use */
192 const char *symbol; /* symbol name */
193 struct ftrace_event_call call;
194 struct trace_event event;
195 unsigned int nr_args;
196 struct probe_arg args[];
199 #define SIZEOF_TRACE_PROBE(n) \
200 (offsetof(struct trace_probe, args) + \
201 (sizeof(struct probe_arg) * (n)))
203 static int kprobe_trace_func(struct kprobe *kp, struct pt_regs *regs);
204 static int kretprobe_trace_func(struct kretprobe_instance *ri,
205 struct pt_regs *regs);
207 static __kprobes int probe_is_return(struct trace_probe *tp)
209 return tp->rp.handler != NULL;
212 static __kprobes const char *probe_symbol(struct trace_probe *tp)
214 return tp->symbol ? tp->symbol : "unknown";
217 static int probe_arg_string(char *buf, size_t n, struct fetch_func *ff)
221 if (ff->func == fetch_argument)
222 ret = snprintf(buf, n, "a%lu", (unsigned long)ff->data);
223 else if (ff->func == fetch_register) {
225 name = regs_query_register_name((unsigned int)((long)ff->data));
226 ret = snprintf(buf, n, "%%%s", name);
227 } else if (ff->func == fetch_stack)
228 ret = snprintf(buf, n, "s%lu", (unsigned long)ff->data);
229 else if (ff->func == fetch_memory)
230 ret = snprintf(buf, n, "@0x%p", ff->data);
231 else if (ff->func == fetch_symbol) {
232 struct symbol_cache *sc = ff->data;
233 ret = snprintf(buf, n, "@%s%+ld", sc->symbol, sc->offset);
234 } else if (ff->func == fetch_retvalue)
235 ret = snprintf(buf, n, "rv");
236 else if (ff->func == fetch_ip)
237 ret = snprintf(buf, n, "ra");
238 else if (ff->func == fetch_stack_address)
239 ret = snprintf(buf, n, "sa");
240 else if (ff->func == fetch_indirect) {
241 struct indirect_fetch_data *id = ff->data;
243 ret = snprintf(buf, n, "%+ld(", id->offset);
247 ret = probe_arg_string(buf + l, n - l, &id->orig);
251 ret = snprintf(buf + l, n - l, ")");
260 static int register_probe_event(struct trace_probe *tp);
261 static void unregister_probe_event(struct trace_probe *tp);
263 static DEFINE_MUTEX(probe_lock);
264 static LIST_HEAD(probe_list);
267 * Allocate new trace_probe and initialize it (including kprobes).
269 static struct trace_probe *alloc_trace_probe(const char *group,
274 int nargs, int is_return)
276 struct trace_probe *tp;
278 tp = kzalloc(SIZEOF_TRACE_PROBE(nargs), GFP_KERNEL);
280 return ERR_PTR(-ENOMEM);
283 tp->symbol = kstrdup(symbol, GFP_KERNEL);
286 tp->rp.kp.symbol_name = tp->symbol;
287 tp->rp.kp.offset = offs;
289 tp->rp.kp.addr = addr;
291 /* Set handler here for checking whether this probe is return or not. */
293 tp->rp.handler = kretprobe_trace_func;
295 tp->rp.kp.pre_handler = kprobe_trace_func;
299 tp->call.name = kstrdup(event, GFP_KERNEL);
305 tp->call.system = kstrdup(group, GFP_KERNEL);
306 if (!tp->call.system)
309 INIT_LIST_HEAD(&tp->list);
312 kfree(tp->call.name);
315 return ERR_PTR(-ENOMEM);
318 static void free_probe_arg(struct probe_arg *arg)
320 if (arg->fetch.func == fetch_symbol)
321 free_symbol_cache(arg->fetch.data);
322 else if (arg->fetch.func == fetch_indirect)
323 free_indirect_fetch_data(arg->fetch.data);
327 static void free_trace_probe(struct trace_probe *tp)
331 for (i = 0; i < tp->nr_args; i++)
332 free_probe_arg(&tp->args[i]);
334 kfree(tp->call.system);
335 kfree(tp->call.name);
340 static struct trace_probe *find_probe_event(const char *event)
342 struct trace_probe *tp;
344 list_for_each_entry(tp, &probe_list, list)
345 if (!strcmp(tp->call.name, event))
350 static void __unregister_trace_probe(struct trace_probe *tp)
352 if (probe_is_return(tp))
353 unregister_kretprobe(&tp->rp);
355 unregister_kprobe(&tp->rp.kp);
358 /* Unregister a trace_probe and probe_event: call with locking probe_lock */
359 static void unregister_trace_probe(struct trace_probe *tp)
361 unregister_probe_event(tp);
362 __unregister_trace_probe(tp);
366 /* Register a trace_probe and probe_event */
367 static int register_trace_probe(struct trace_probe *tp)
369 struct trace_probe *old_tp;
372 mutex_lock(&probe_lock);
374 if (probe_is_return(tp))
375 ret = register_kretprobe(&tp->rp);
377 ret = register_kprobe(&tp->rp.kp);
380 pr_warning("Could not insert probe(%d)\n", ret);
381 if (ret == -EILSEQ) {
382 pr_warning("Probing address(0x%p) is not an "
383 "instruction boundary.\n",
389 /* register as an event */
390 old_tp = find_probe_event(tp->call.name);
392 /* delete old event */
393 unregister_trace_probe(old_tp);
394 free_trace_probe(old_tp);
396 ret = register_probe_event(tp);
398 pr_warning("Faild to register probe event(%d)\n", ret);
399 __unregister_trace_probe(tp);
401 list_add_tail(&tp->list, &probe_list);
403 mutex_unlock(&probe_lock);
407 /* Split symbol and offset. */
408 static int split_symbol_offset(char *symbol, unsigned long *offset)
416 tmp = strchr(symbol, '+');
418 /* skip sign because strict_strtol doesn't accept '+' */
419 ret = strict_strtoul(tmp + 1, 0, offset);
428 #define PARAM_MAX_ARGS 16
429 #define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long))
431 static int parse_probe_arg(char *arg, struct fetch_func *ff, int is_return)
439 case 'a': /* argument */
440 ret = strict_strtoul(arg + 1, 10, ¶m);
441 if (ret || param > PARAM_MAX_ARGS)
444 ff->func = fetch_argument;
445 ff->data = (void *)param;
448 case 'r': /* retval or retaddr */
449 if (is_return && arg[1] == 'v') {
450 ff->func = fetch_retvalue;
452 } else if (is_return && arg[1] == 'a') {
458 case '%': /* named register */
459 ret = regs_query_register_offset(arg + 1);
461 ff->func = fetch_register;
462 ff->data = (void *)(unsigned long)ret;
466 case 's': /* stack */
468 ff->func = fetch_stack_address;
471 ret = strict_strtoul(arg + 1, 10, ¶m);
472 if (ret || param > PARAM_MAX_STACK)
475 ff->func = fetch_stack;
476 ff->data = (void *)param;
480 case '@': /* memory or symbol */
481 if (isdigit(arg[1])) {
482 ret = strict_strtoul(arg + 1, 0, ¶m);
485 ff->func = fetch_memory;
486 ff->data = (void *)param;
488 ret = split_symbol_offset(arg + 1, &offset);
491 ff->data = alloc_symbol_cache(arg + 1,
494 ff->func = fetch_symbol;
499 case '+': /* indirect memory */
501 tmp = strchr(arg, '(');
507 ret = strict_strtol(arg + 1, 0, &offset);
513 tmp = strrchr(arg, ')');
515 struct indirect_fetch_data *id;
517 id = kzalloc(sizeof(struct indirect_fetch_data),
522 ret = parse_probe_arg(arg, &id->orig, is_return);
526 ff->func = fetch_indirect;
527 ff->data = (void *)id;
533 /* TODO: support custom handler */
539 static int create_trace_probe(int argc, char **argv)
543 * - Add kprobe: p[:[GRP/]EVENT] KSYM[+OFFS]|KADDR [FETCHARGS]
544 * - Add kretprobe: r[:[GRP/]EVENT] KSYM[+0] [FETCHARGS]
546 * aN : fetch Nth of function argument. (N:0-)
547 * rv : fetch return value
548 * ra : fetch return address
549 * sa : fetch stack address
550 * sN : fetch Nth of stack (N:0-)
551 * @ADDR : fetch memory at ADDR (ADDR should be in kernel)
552 * @SYM[+|-offs] : fetch memory at SYM +|- offs (SYM is a data symbol)
553 * %REG : fetch register REG
554 * Indirect memory fetch:
555 * +|-offs(ARG) : fetch memory at ARG +|- offs address.
556 * Alias name of args:
557 * NAME=FETCHARG : set NAME as alias of FETCHARG.
559 struct trace_probe *tp;
562 char *symbol = NULL, *event = NULL, *arg = NULL, *group = NULL;
563 unsigned long offset = 0;
565 char buf[MAX_EVENT_NAME_LEN];
570 if (argv[0][0] == 'p')
572 else if (argv[0][0] == 'r')
577 if (argv[0][1] == ':') {
579 if (strchr(event, '/')) {
581 event = strchr(group, '/') + 1;
583 if (strlen(group) == 0) {
584 pr_info("Group name is not specifiled\n");
588 if (strlen(event) == 0) {
589 pr_info("Event name is not specifiled\n");
594 if (isdigit(argv[1][0])) {
597 /* an address specified */
598 ret = strict_strtoul(&argv[0][2], 0, (unsigned long *)&addr);
602 /* a symbol specified */
604 /* TODO: support .init module functions */
605 ret = split_symbol_offset(symbol, &offset);
608 if (offset && is_return)
611 argc -= 2; argv += 2;
615 group = KPROBE_EVENT_SYSTEM;
617 /* Make a new event name */
619 snprintf(buf, MAX_EVENT_NAME_LEN, "%c@%s%+ld",
620 is_return ? 'r' : 'p', symbol, offset);
622 snprintf(buf, MAX_EVENT_NAME_LEN, "%c@0x%p",
623 is_return ? 'r' : 'p', addr);
626 tp = alloc_trace_probe(group, event, addr, symbol, offset, argc,
631 /* parse arguments */
633 for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
634 /* Parse argument name */
635 arg = strchr(argv[i], '=');
640 tp->args[i].name = kstrdup(argv[i], GFP_KERNEL);
642 /* Parse fetch argument */
643 if (strlen(arg) > MAX_ARGSTR_LEN) {
644 pr_info("Argument%d(%s) is too long.\n", i, arg);
648 ret = parse_probe_arg(arg, &tp->args[i].fetch, is_return);
654 ret = register_trace_probe(tp);
660 free_trace_probe(tp);
664 static void cleanup_all_probes(void)
666 struct trace_probe *tp;
668 mutex_lock(&probe_lock);
669 /* TODO: Use batch unregistration */
670 while (!list_empty(&probe_list)) {
671 tp = list_entry(probe_list.next, struct trace_probe, list);
672 unregister_trace_probe(tp);
673 free_trace_probe(tp);
675 mutex_unlock(&probe_lock);
679 /* Probes listing interfaces */
680 static void *probes_seq_start(struct seq_file *m, loff_t *pos)
682 mutex_lock(&probe_lock);
683 return seq_list_start(&probe_list, *pos);
686 static void *probes_seq_next(struct seq_file *m, void *v, loff_t *pos)
688 return seq_list_next(v, &probe_list, pos);
691 static void probes_seq_stop(struct seq_file *m, void *v)
693 mutex_unlock(&probe_lock);
696 static int probes_seq_show(struct seq_file *m, void *v)
698 struct trace_probe *tp = v;
700 char buf[MAX_ARGSTR_LEN + 1];
702 seq_printf(m, "%c", probe_is_return(tp) ? 'r' : 'p');
703 seq_printf(m, ":%s", tp->call.name);
706 seq_printf(m, " %s+%u", probe_symbol(tp), tp->rp.kp.offset);
708 seq_printf(m, " 0x%p", tp->rp.kp.addr);
710 for (i = 0; i < tp->nr_args; i++) {
711 ret = probe_arg_string(buf, MAX_ARGSTR_LEN, &tp->args[i].fetch);
713 pr_warning("Argument%d decoding error(%d).\n", i, ret);
716 seq_printf(m, " %s=%s", tp->args[i].name, buf);
722 static const struct seq_operations probes_seq_op = {
723 .start = probes_seq_start,
724 .next = probes_seq_next,
725 .stop = probes_seq_stop,
726 .show = probes_seq_show
729 static int probes_open(struct inode *inode, struct file *file)
731 if ((file->f_mode & FMODE_WRITE) &&
732 (file->f_flags & O_TRUNC))
733 cleanup_all_probes();
735 return seq_open(file, &probes_seq_op);
738 static int command_trace_probe(const char *buf)
741 int argc = 0, ret = 0;
743 argv = argv_split(GFP_KERNEL, buf, &argc);
748 ret = create_trace_probe(argc, argv);
754 #define WRITE_BUFSIZE 128
756 static ssize_t probes_write(struct file *file, const char __user *buffer,
757 size_t count, loff_t *ppos)
764 kbuf = kmalloc(WRITE_BUFSIZE, GFP_KERNEL);
769 while (done < count) {
771 if (size >= WRITE_BUFSIZE)
772 size = WRITE_BUFSIZE - 1;
773 if (copy_from_user(kbuf, buffer + done, size)) {
778 tmp = strchr(kbuf, '\n');
781 size = tmp - kbuf + 1;
782 } else if (done + size < count) {
783 pr_warning("Line length is too long: "
784 "Should be less than %d.", WRITE_BUFSIZE);
789 /* Remove comments */
790 tmp = strchr(kbuf, '#');
794 ret = command_trace_probe(kbuf);
804 static const struct file_operations kprobe_events_ops = {
805 .owner = THIS_MODULE,
809 .release = seq_release,
810 .write = probes_write,
813 /* Probes profiling interfaces */
814 static int probes_profile_seq_show(struct seq_file *m, void *v)
816 struct trace_probe *tp = v;
818 seq_printf(m, " %-44s %15lu %15lu\n", tp->call.name, tp->nhit,
824 static const struct seq_operations profile_seq_op = {
825 .start = probes_seq_start,
826 .next = probes_seq_next,
827 .stop = probes_seq_stop,
828 .show = probes_profile_seq_show
831 static int profile_open(struct inode *inode, struct file *file)
833 return seq_open(file, &profile_seq_op);
836 static const struct file_operations kprobe_profile_ops = {
837 .owner = THIS_MODULE,
838 .open = profile_open,
841 .release = seq_release,
845 static __kprobes int kprobe_trace_func(struct kprobe *kp, struct pt_regs *regs)
847 struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
848 struct kprobe_trace_entry *entry;
849 struct ring_buffer_event *event;
850 struct ring_buffer *buffer;
852 unsigned long irq_flags;
853 struct ftrace_event_call *call = &tp->call;
857 local_save_flags(irq_flags);
858 pc = preempt_count();
860 size = SIZEOF_KPROBE_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->ip = (unsigned long)kp->addr;
870 for (i = 0; i < tp->nr_args; i++)
871 entry->args[i] = call_fetch(&tp->args[i].fetch, regs);
873 if (!filter_current_check_discard(buffer, call, entry, event))
874 trace_nowake_buffer_unlock_commit(buffer, event, irq_flags, pc);
878 /* Kretprobe handler */
879 static __kprobes int kretprobe_trace_func(struct kretprobe_instance *ri,
880 struct pt_regs *regs)
882 struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
883 struct kretprobe_trace_entry *entry;
884 struct ring_buffer_event *event;
885 struct ring_buffer *buffer;
887 unsigned long irq_flags;
888 struct ftrace_event_call *call = &tp->call;
890 local_save_flags(irq_flags);
891 pc = preempt_count();
893 size = SIZEOF_KRETPROBE_TRACE_ENTRY(tp->nr_args);
895 event = trace_current_buffer_lock_reserve(&buffer, call->id, size,
900 entry = ring_buffer_event_data(event);
901 entry->nargs = tp->nr_args;
902 entry->func = (unsigned long)tp->rp.kp.addr;
903 entry->ret_ip = (unsigned long)ri->ret_addr;
904 for (i = 0; i < tp->nr_args; i++)
905 entry->args[i] = call_fetch(&tp->args[i].fetch, regs);
907 if (!filter_current_check_discard(buffer, call, entry, event))
908 trace_nowake_buffer_unlock_commit(buffer, event, irq_flags, pc);
913 /* Event entry printers */
915 print_kprobe_event(struct trace_iterator *iter, int flags)
917 struct kprobe_trace_entry *field;
918 struct trace_seq *s = &iter->seq;
919 struct trace_event *event;
920 struct trace_probe *tp;
923 field = (struct kprobe_trace_entry *)iter->ent;
924 event = ftrace_find_event(field->ent.type);
925 tp = container_of(event, struct trace_probe, event);
927 if (!trace_seq_printf(s, "%s: (", tp->call.name))
930 if (!seq_print_ip_sym(s, field->ip, flags | TRACE_ITER_SYM_OFFSET))
933 if (!trace_seq_puts(s, ")"))
936 for (i = 0; i < field->nargs; i++)
937 if (!trace_seq_printf(s, " %s=%lx",
938 tp->args[i].name, field->args[i]))
941 if (!trace_seq_puts(s, "\n"))
944 return TRACE_TYPE_HANDLED;
946 return TRACE_TYPE_PARTIAL_LINE;
950 print_kretprobe_event(struct trace_iterator *iter, int flags)
952 struct kretprobe_trace_entry *field;
953 struct trace_seq *s = &iter->seq;
954 struct trace_event *event;
955 struct trace_probe *tp;
958 field = (struct kretprobe_trace_entry *)iter->ent;
959 event = ftrace_find_event(field->ent.type);
960 tp = container_of(event, struct trace_probe, event);
962 if (!trace_seq_printf(s, "%s: (", tp->call.name))
965 if (!seq_print_ip_sym(s, field->ret_ip, flags | TRACE_ITER_SYM_OFFSET))
968 if (!trace_seq_puts(s, " <- "))
971 if (!seq_print_ip_sym(s, field->func, flags & ~TRACE_ITER_SYM_OFFSET))
974 if (!trace_seq_puts(s, ")"))
977 for (i = 0; i < field->nargs; i++)
978 if (!trace_seq_printf(s, " %s=%lx",
979 tp->args[i].name, field->args[i]))
982 if (!trace_seq_puts(s, "\n"))
985 return TRACE_TYPE_HANDLED;
987 return TRACE_TYPE_PARTIAL_LINE;
990 static int probe_event_enable(struct ftrace_event_call *call)
992 struct trace_probe *tp = (struct trace_probe *)call->data;
994 if (probe_is_return(tp)) {
995 tp->rp.handler = kretprobe_trace_func;
996 return enable_kretprobe(&tp->rp);
998 tp->rp.kp.pre_handler = kprobe_trace_func;
999 return enable_kprobe(&tp->rp.kp);
1003 static void probe_event_disable(struct ftrace_event_call *call)
1005 struct trace_probe *tp = (struct trace_probe *)call->data;
1007 if (probe_is_return(tp))
1008 disable_kretprobe(&tp->rp);
1010 disable_kprobe(&tp->rp.kp);
1013 static int probe_event_raw_init(struct ftrace_event_call *event_call)
1015 INIT_LIST_HEAD(&event_call->fields);
1021 #define DEFINE_FIELD(type, item, name, is_signed) \
1023 ret = trace_define_field(event_call, #type, name, \
1024 offsetof(typeof(field), item), \
1025 sizeof(field.item), is_signed, \
1031 static int kprobe_event_define_fields(struct ftrace_event_call *event_call)
1034 struct kprobe_trace_entry field;
1035 struct trace_probe *tp = (struct trace_probe *)event_call->data;
1037 ret = trace_define_common_fields(event_call);
1041 DEFINE_FIELD(unsigned long, ip, "ip", 0);
1042 DEFINE_FIELD(int, nargs, "nargs", 1);
1043 /* Set argument names as fields */
1044 for (i = 0; i < tp->nr_args; i++)
1045 DEFINE_FIELD(unsigned long, args[i], tp->args[i].name, 0);
1049 static int kretprobe_event_define_fields(struct ftrace_event_call *event_call)
1052 struct kretprobe_trace_entry field;
1053 struct trace_probe *tp = (struct trace_probe *)event_call->data;
1055 ret = trace_define_common_fields(event_call);
1059 DEFINE_FIELD(unsigned long, func, "func", 0);
1060 DEFINE_FIELD(unsigned long, ret_ip, "ret_ip", 0);
1061 DEFINE_FIELD(int, nargs, "nargs", 1);
1062 /* Set argument names as fields */
1063 for (i = 0; i < tp->nr_args; i++)
1064 DEFINE_FIELD(unsigned long, args[i], tp->args[i].name, 0);
1068 static int __probe_event_show_format(struct trace_seq *s,
1069 struct trace_probe *tp, const char *fmt,
1075 if (!trace_seq_printf(s, "\nprint fmt: \"%s", fmt))
1078 for (i = 0; i < tp->nr_args; i++)
1079 if (!trace_seq_printf(s, " %s=%%lx", tp->args[i].name))
1082 if (!trace_seq_printf(s, "\", %s", arg))
1085 for (i = 0; i < tp->nr_args; i++)
1086 if (!trace_seq_printf(s, ", REC->%s", tp->args[i].name))
1089 return trace_seq_puts(s, "\n");
1093 #define SHOW_FIELD(type, item, name) \
1095 ret = trace_seq_printf(s, "\tfield: " #type " %s;\t" \
1096 "offset:%u;\tsize:%u;\n", name, \
1097 (unsigned int)offsetof(typeof(field), item),\
1098 (unsigned int)sizeof(type)); \
1103 static int kprobe_event_show_format(struct ftrace_event_call *call,
1104 struct trace_seq *s)
1106 struct kprobe_trace_entry field __attribute__((unused));
1108 struct trace_probe *tp = (struct trace_probe *)call->data;
1110 SHOW_FIELD(unsigned long, ip, "ip");
1111 SHOW_FIELD(int, nargs, "nargs");
1114 for (i = 0; i < tp->nr_args; i++)
1115 SHOW_FIELD(unsigned long, args[i], tp->args[i].name);
1116 trace_seq_puts(s, "\n");
1118 return __probe_event_show_format(s, tp, "(%lx)", "REC->ip");
1121 static int kretprobe_event_show_format(struct ftrace_event_call *call,
1122 struct trace_seq *s)
1124 struct kretprobe_trace_entry field __attribute__((unused));
1126 struct trace_probe *tp = (struct trace_probe *)call->data;
1128 SHOW_FIELD(unsigned long, func, "func");
1129 SHOW_FIELD(unsigned long, ret_ip, "ret_ip");
1130 SHOW_FIELD(int, nargs, "nargs");
1133 for (i = 0; i < tp->nr_args; i++)
1134 SHOW_FIELD(unsigned long, args[i], tp->args[i].name);
1135 trace_seq_puts(s, "\n");
1137 return __probe_event_show_format(s, tp, "(%lx <- %lx)",
1138 "REC->func, REC->ret_ip");
1141 #ifdef CONFIG_EVENT_PROFILE
1143 /* Kprobe profile handler */
1144 static __kprobes int kprobe_profile_func(struct kprobe *kp,
1145 struct pt_regs *regs)
1147 struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
1148 struct ftrace_event_call *call = &tp->call;
1149 struct kprobe_trace_entry *entry;
1151 unsigned long irq_flags;
1153 local_save_flags(irq_flags);
1154 pc = preempt_count();
1156 size = SIZEOF_KPROBE_TRACE_ENTRY(tp->nr_args);
1159 char raw_data[size];
1160 struct trace_entry *ent;
1162 *(u64 *)(&raw_data[size - sizeof(u64)]) = 0ULL;
1163 entry = (struct kprobe_trace_entry *)raw_data;
1166 tracing_generic_entry_update(ent, irq_flags, pc);
1167 ent->type = call->id;
1168 entry->nargs = tp->nr_args;
1169 entry->ip = (unsigned long)kp->addr;
1170 for (i = 0; i < tp->nr_args; i++)
1171 entry->args[i] = call_fetch(&tp->args[i].fetch, regs);
1172 perf_tpcounter_event(call->id, entry->ip, 1, entry, size);
1177 /* Kretprobe profile handler */
1178 static __kprobes int kretprobe_profile_func(struct kretprobe_instance *ri,
1179 struct pt_regs *regs)
1181 struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
1182 struct ftrace_event_call *call = &tp->call;
1183 struct kretprobe_trace_entry *entry;
1185 unsigned long irq_flags;
1187 local_save_flags(irq_flags);
1188 pc = preempt_count();
1190 size = SIZEOF_KRETPROBE_TRACE_ENTRY(tp->nr_args);
1193 char raw_data[size];
1194 struct trace_entry *ent;
1196 *(u64 *)(&raw_data[size - sizeof(u64)]) = 0ULL;
1197 entry = (struct kretprobe_trace_entry *)raw_data;
1200 tracing_generic_entry_update(ent, irq_flags, pc);
1201 ent->type = call->id;
1202 entry->nargs = tp->nr_args;
1203 entry->func = (unsigned long)tp->rp.kp.addr;
1204 entry->ret_ip = (unsigned long)ri->ret_addr;
1205 for (i = 0; i < tp->nr_args; i++)
1206 entry->args[i] = call_fetch(&tp->args[i].fetch, regs);
1207 perf_tpcounter_event(call->id, entry->ret_ip, 1, entry, size);
1212 static int probe_profile_enable(struct ftrace_event_call *call)
1214 struct trace_probe *tp = (struct trace_probe *)call->data;
1216 if (atomic_inc_return(&call->profile_count))
1219 if (probe_is_return(tp)) {
1220 tp->rp.handler = kretprobe_profile_func;
1221 return enable_kretprobe(&tp->rp);
1223 tp->rp.kp.pre_handler = kprobe_profile_func;
1224 return enable_kprobe(&tp->rp.kp);
1228 static void probe_profile_disable(struct ftrace_event_call *call)
1230 if (atomic_add_negative(-1, &call->profile_count))
1231 probe_event_disable(call);
1234 #endif /* CONFIG_EVENT_PROFILE */
1236 static int register_probe_event(struct trace_probe *tp)
1238 struct ftrace_event_call *call = &tp->call;
1241 /* Initialize ftrace_event_call */
1242 if (probe_is_return(tp)) {
1243 tp->event.trace = print_kretprobe_event;
1244 call->raw_init = probe_event_raw_init;
1245 call->show_format = kretprobe_event_show_format;
1246 call->define_fields = kretprobe_event_define_fields;
1248 tp->event.trace = print_kprobe_event;
1249 call->raw_init = probe_event_raw_init;
1250 call->show_format = kprobe_event_show_format;
1251 call->define_fields = kprobe_event_define_fields;
1253 call->event = &tp->event;
1254 call->id = register_ftrace_event(&tp->event);
1258 call->regfunc = probe_event_enable;
1259 call->unregfunc = probe_event_disable;
1261 #ifdef CONFIG_EVENT_PROFILE
1262 atomic_set(&call->profile_count, -1);
1263 call->profile_enable = probe_profile_enable;
1264 call->profile_disable = probe_profile_disable;
1267 ret = trace_add_event_call(call);
1269 pr_info("Failed to register kprobe event: %s\n", call->name);
1270 unregister_ftrace_event(&tp->event);
1275 static void unregister_probe_event(struct trace_probe *tp)
1277 /* tp->event is unregistered in trace_remove_event_call() */
1278 trace_remove_event_call(&tp->call);
1281 /* Make a debugfs interface for controling probe points */
1282 static __init int init_kprobe_trace(void)
1284 struct dentry *d_tracer;
1285 struct dentry *entry;
1287 d_tracer = tracing_init_dentry();
1291 entry = debugfs_create_file("kprobe_events", 0644, d_tracer,
1292 NULL, &kprobe_events_ops);
1294 /* Event list interface */
1296 pr_warning("Could not create debugfs "
1297 "'kprobe_events' entry\n");
1299 /* Profile interface */
1300 entry = debugfs_create_file("kprobe_profile", 0444, d_tracer,
1301 NULL, &kprobe_profile_ops);
1304 pr_warning("Could not create debugfs "
1305 "'kprobe_profile' entry\n");
1308 fs_initcall(init_kprobe_trace);
1311 #ifdef CONFIG_FTRACE_STARTUP_TEST
1313 static int kprobe_trace_selftest_target(int a1, int a2, int a3,
1314 int a4, int a5, int a6)
1316 return a1 + a2 + a3 + a4 + a5 + a6;
1319 static __init int kprobe_trace_self_tests_init(void)
1322 int (*target)(int, int, int, int, int, int);
1324 target = kprobe_trace_selftest_target;
1326 pr_info("Testing kprobe tracing: ");
1328 ret = command_trace_probe("p:testprobe kprobe_trace_selftest_target "
1329 "a1 a2 a3 a4 a5 a6");
1330 if (WARN_ON_ONCE(ret))
1331 pr_warning("error enabling function entry\n");
1333 ret = command_trace_probe("r:testprobe2 kprobe_trace_selftest_target "
1335 if (WARN_ON_ONCE(ret))
1336 pr_warning("error enabling function return\n");
1338 ret = target(1, 2, 3, 4, 5, 6);
1340 cleanup_all_probes();
1346 late_initcall(kprobe_trace_self_tests_init);