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_event.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;
188 /* Flags for trace_probe */
189 #define TP_FLAG_TRACE 1
190 #define TP_FLAG_PROFILE 2
193 struct list_head list;
194 struct kretprobe rp; /* Use rp.kp for kprobe use */
196 unsigned int flags; /* For TP_FLAG_* */
197 const char *symbol; /* symbol name */
198 struct ftrace_event_call call;
199 struct trace_event event;
200 unsigned int nr_args;
201 struct probe_arg args[];
204 #define SIZEOF_TRACE_PROBE(n) \
205 (offsetof(struct trace_probe, args) + \
206 (sizeof(struct probe_arg) * (n)))
208 static __kprobes int probe_is_return(struct trace_probe *tp)
210 return tp->rp.handler != NULL;
213 static __kprobes const char *probe_symbol(struct trace_probe *tp)
215 return tp->symbol ? tp->symbol : "unknown";
218 static int probe_arg_string(char *buf, size_t n, struct fetch_func *ff)
222 if (ff->func == fetch_argument)
223 ret = snprintf(buf, n, "$a%lu", (unsigned long)ff->data);
224 else if (ff->func == fetch_register) {
226 name = regs_query_register_name((unsigned int)((long)ff->data));
227 ret = snprintf(buf, n, "%%%s", name);
228 } else if (ff->func == fetch_stack)
229 ret = snprintf(buf, n, "$s%lu", (unsigned long)ff->data);
230 else if (ff->func == fetch_memory)
231 ret = snprintf(buf, n, "@0x%p", ff->data);
232 else if (ff->func == fetch_symbol) {
233 struct symbol_cache *sc = ff->data;
234 ret = snprintf(buf, n, "@%s%+ld", sc->symbol, sc->offset);
235 } else if (ff->func == fetch_retvalue)
236 ret = snprintf(buf, n, "$rv");
237 else if (ff->func == fetch_ip)
238 ret = snprintf(buf, n, "$ra");
239 else if (ff->func == fetch_stack_address)
240 ret = snprintf(buf, n, "$sa");
241 else if (ff->func == fetch_indirect) {
242 struct indirect_fetch_data *id = ff->data;
244 ret = snprintf(buf, n, "%+ld(", id->offset);
248 ret = probe_arg_string(buf + l, n - l, &id->orig);
252 ret = snprintf(buf + l, n - l, ")");
261 static int register_probe_event(struct trace_probe *tp);
262 static void unregister_probe_event(struct trace_probe *tp);
264 static DEFINE_MUTEX(probe_lock);
265 static LIST_HEAD(probe_list);
267 static int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs);
268 static int kretprobe_dispatcher(struct kretprobe_instance *ri,
269 struct pt_regs *regs);
272 * Allocate new trace_probe and initialize it (including kprobes).
274 static struct trace_probe *alloc_trace_probe(const char *group,
279 int nargs, int is_return)
281 struct trace_probe *tp;
283 tp = kzalloc(SIZEOF_TRACE_PROBE(nargs), GFP_KERNEL);
285 return ERR_PTR(-ENOMEM);
288 tp->symbol = kstrdup(symbol, GFP_KERNEL);
291 tp->rp.kp.symbol_name = tp->symbol;
292 tp->rp.kp.offset = offs;
294 tp->rp.kp.addr = addr;
297 tp->rp.handler = kretprobe_dispatcher;
299 tp->rp.kp.pre_handler = kprobe_dispatcher;
303 tp->call.name = kstrdup(event, GFP_KERNEL);
309 tp->call.system = kstrdup(group, GFP_KERNEL);
310 if (!tp->call.system)
313 INIT_LIST_HEAD(&tp->list);
316 kfree(tp->call.name);
319 return ERR_PTR(-ENOMEM);
322 static void free_probe_arg(struct probe_arg *arg)
324 if (arg->fetch.func == fetch_symbol)
325 free_symbol_cache(arg->fetch.data);
326 else if (arg->fetch.func == fetch_indirect)
327 free_indirect_fetch_data(arg->fetch.data);
331 static void free_trace_probe(struct trace_probe *tp)
335 for (i = 0; i < tp->nr_args; i++)
336 free_probe_arg(&tp->args[i]);
338 kfree(tp->call.system);
339 kfree(tp->call.name);
344 static struct trace_probe *find_probe_event(const char *event)
346 struct trace_probe *tp;
348 list_for_each_entry(tp, &probe_list, list)
349 if (!strcmp(tp->call.name, event))
354 /* Unregister a trace_probe and probe_event: call with locking probe_lock */
355 static void unregister_trace_probe(struct trace_probe *tp)
357 if (probe_is_return(tp))
358 unregister_kretprobe(&tp->rp);
360 unregister_kprobe(&tp->rp.kp);
362 unregister_probe_event(tp);
365 /* Register a trace_probe and probe_event */
366 static int register_trace_probe(struct trace_probe *tp)
368 struct trace_probe *old_tp;
371 mutex_lock(&probe_lock);
373 /* register as an event */
374 old_tp = find_probe_event(tp->call.name);
376 /* delete old event */
377 unregister_trace_probe(old_tp);
378 free_trace_probe(old_tp);
380 ret = register_probe_event(tp);
382 pr_warning("Faild to register probe event(%d)\n", ret);
386 tp->rp.kp.flags |= KPROBE_FLAG_DISABLED;
387 if (probe_is_return(tp))
388 ret = register_kretprobe(&tp->rp);
390 ret = register_kprobe(&tp->rp.kp);
393 pr_warning("Could not insert probe(%d)\n", ret);
394 if (ret == -EILSEQ) {
395 pr_warning("Probing address(0x%p) is not an "
396 "instruction boundary.\n",
400 unregister_probe_event(tp);
402 list_add_tail(&tp->list, &probe_list);
404 mutex_unlock(&probe_lock);
408 /* Split symbol and offset. */
409 static int split_symbol_offset(char *symbol, unsigned long *offset)
417 tmp = strchr(symbol, '+');
419 /* skip sign because strict_strtol doesn't accept '+' */
420 ret = strict_strtoul(tmp + 1, 0, offset);
429 #define PARAM_MAX_ARGS 16
430 #define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long))
432 static int parse_probe_vars(char *arg, struct fetch_func *ff, int is_return)
438 case 'a': /* argument */
439 ret = strict_strtoul(arg + 1, 10, ¶m);
440 if (ret || param > PARAM_MAX_ARGS)
443 ff->func = fetch_argument;
444 ff->data = (void *)param;
447 case 'r': /* retval or retaddr */
448 if (is_return && arg[1] == 'v') {
449 ff->func = fetch_retvalue;
451 } else if (is_return && arg[1] == 'a') {
457 case 's': /* stack */
459 ff->func = fetch_stack_address;
462 ret = strict_strtoul(arg + 1, 10, ¶m);
463 if (ret || param > PARAM_MAX_STACK)
466 ff->func = fetch_stack;
467 ff->data = (void *)param;
477 static int parse_probe_arg(char *arg, struct fetch_func *ff, int is_return)
486 ret = parse_probe_vars(arg + 1, ff, is_return);
488 case '%': /* named register */
489 ret = regs_query_register_offset(arg + 1);
491 ff->func = fetch_register;
492 ff->data = (void *)(unsigned long)ret;
496 case '@': /* memory or symbol */
497 if (isdigit(arg[1])) {
498 ret = strict_strtoul(arg + 1, 0, ¶m);
501 ff->func = fetch_memory;
502 ff->data = (void *)param;
504 ret = split_symbol_offset(arg + 1, &offset);
507 ff->data = alloc_symbol_cache(arg + 1, offset);
509 ff->func = fetch_symbol;
514 case '+': /* indirect memory */
516 tmp = strchr(arg, '(');
522 ret = strict_strtol(arg + 1, 0, &offset);
528 tmp = strrchr(arg, ')');
530 struct indirect_fetch_data *id;
532 id = kzalloc(sizeof(struct indirect_fetch_data),
537 ret = parse_probe_arg(arg, &id->orig, is_return);
541 ff->func = fetch_indirect;
542 ff->data = (void *)id;
548 /* TODO: support custom handler */
554 static int create_trace_probe(int argc, char **argv)
558 * - Add kprobe: p[:[GRP/]EVENT] KSYM[+OFFS]|KADDR [FETCHARGS]
559 * - Add kretprobe: r[:[GRP/]EVENT] KSYM[+0] [FETCHARGS]
561 * $aN : fetch Nth of function argument. (N:0-)
562 * $rv : fetch return value
563 * $ra : fetch return address
564 * $sa : fetch stack address
565 * $sN : fetch Nth of stack (N:0-)
566 * @ADDR : fetch memory at ADDR (ADDR should be in kernel)
567 * @SYM[+|-offs] : fetch memory at SYM +|- offs (SYM is a data symbol)
568 * %REG : fetch register REG
569 * Indirect memory fetch:
570 * +|-offs(ARG) : fetch memory at ARG +|- offs address.
571 * Alias name of args:
572 * NAME=FETCHARG : set NAME as alias of FETCHARG.
574 struct trace_probe *tp;
577 char *symbol = NULL, *event = NULL, *arg = NULL, *group = NULL;
578 unsigned long offset = 0;
580 char buf[MAX_EVENT_NAME_LEN];
585 if (argv[0][0] == 'p')
587 else if (argv[0][0] == 'r')
592 if (argv[0][1] == ':') {
594 if (strchr(event, '/')) {
596 event = strchr(group, '/') + 1;
598 if (strlen(group) == 0) {
599 pr_info("Group name is not specifiled\n");
603 if (strlen(event) == 0) {
604 pr_info("Event name is not specifiled\n");
609 if (isdigit(argv[1][0])) {
612 /* an address specified */
613 ret = strict_strtoul(&argv[0][2], 0, (unsigned long *)&addr);
617 /* a symbol specified */
619 /* TODO: support .init module functions */
620 ret = split_symbol_offset(symbol, &offset);
623 if (offset && is_return)
626 argc -= 2; argv += 2;
630 group = KPROBE_EVENT_SYSTEM;
632 /* Make a new event name */
634 snprintf(buf, MAX_EVENT_NAME_LEN, "%c@%s%+ld",
635 is_return ? 'r' : 'p', symbol, offset);
637 snprintf(buf, MAX_EVENT_NAME_LEN, "%c@0x%p",
638 is_return ? 'r' : 'p', addr);
641 tp = alloc_trace_probe(group, event, addr, symbol, offset, argc,
646 /* parse arguments */
648 for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
649 /* Parse argument name */
650 arg = strchr(argv[i], '=');
655 tp->args[i].name = kstrdup(argv[i], GFP_KERNEL);
657 /* Parse fetch argument */
658 if (strlen(arg) > MAX_ARGSTR_LEN) {
659 pr_info("Argument%d(%s) is too long.\n", i, arg);
663 ret = parse_probe_arg(arg, &tp->args[i].fetch, is_return);
669 ret = register_trace_probe(tp);
675 free_trace_probe(tp);
679 static void cleanup_all_probes(void)
681 struct trace_probe *tp;
683 mutex_lock(&probe_lock);
684 /* TODO: Use batch unregistration */
685 while (!list_empty(&probe_list)) {
686 tp = list_entry(probe_list.next, struct trace_probe, list);
687 unregister_trace_probe(tp);
688 free_trace_probe(tp);
690 mutex_unlock(&probe_lock);
694 /* Probes listing interfaces */
695 static void *probes_seq_start(struct seq_file *m, loff_t *pos)
697 mutex_lock(&probe_lock);
698 return seq_list_start(&probe_list, *pos);
701 static void *probes_seq_next(struct seq_file *m, void *v, loff_t *pos)
703 return seq_list_next(v, &probe_list, pos);
706 static void probes_seq_stop(struct seq_file *m, void *v)
708 mutex_unlock(&probe_lock);
711 static int probes_seq_show(struct seq_file *m, void *v)
713 struct trace_probe *tp = v;
715 char buf[MAX_ARGSTR_LEN + 1];
717 seq_printf(m, "%c", probe_is_return(tp) ? 'r' : 'p');
718 seq_printf(m, ":%s", tp->call.name);
721 seq_printf(m, " %s+%u", probe_symbol(tp), tp->rp.kp.offset);
723 seq_printf(m, " 0x%p", tp->rp.kp.addr);
725 for (i = 0; i < tp->nr_args; i++) {
726 ret = probe_arg_string(buf, MAX_ARGSTR_LEN, &tp->args[i].fetch);
728 pr_warning("Argument%d decoding error(%d).\n", i, ret);
731 seq_printf(m, " %s=%s", tp->args[i].name, buf);
737 static const struct seq_operations probes_seq_op = {
738 .start = probes_seq_start,
739 .next = probes_seq_next,
740 .stop = probes_seq_stop,
741 .show = probes_seq_show
744 static int probes_open(struct inode *inode, struct file *file)
746 if ((file->f_mode & FMODE_WRITE) &&
747 (file->f_flags & O_TRUNC))
748 cleanup_all_probes();
750 return seq_open(file, &probes_seq_op);
753 static int command_trace_probe(const char *buf)
756 int argc = 0, ret = 0;
758 argv = argv_split(GFP_KERNEL, buf, &argc);
763 ret = create_trace_probe(argc, argv);
769 #define WRITE_BUFSIZE 128
771 static ssize_t probes_write(struct file *file, const char __user *buffer,
772 size_t count, loff_t *ppos)
779 kbuf = kmalloc(WRITE_BUFSIZE, GFP_KERNEL);
784 while (done < count) {
786 if (size >= WRITE_BUFSIZE)
787 size = WRITE_BUFSIZE - 1;
788 if (copy_from_user(kbuf, buffer + done, size)) {
793 tmp = strchr(kbuf, '\n');
796 size = tmp - kbuf + 1;
797 } else if (done + size < count) {
798 pr_warning("Line length is too long: "
799 "Should be less than %d.", WRITE_BUFSIZE);
804 /* Remove comments */
805 tmp = strchr(kbuf, '#');
809 ret = command_trace_probe(kbuf);
819 static const struct file_operations kprobe_events_ops = {
820 .owner = THIS_MODULE,
824 .release = seq_release,
825 .write = probes_write,
828 /* Probes profiling interfaces */
829 static int probes_profile_seq_show(struct seq_file *m, void *v)
831 struct trace_probe *tp = v;
833 seq_printf(m, " %-44s %15lu %15lu\n", tp->call.name, tp->nhit,
839 static const struct seq_operations profile_seq_op = {
840 .start = probes_seq_start,
841 .next = probes_seq_next,
842 .stop = probes_seq_stop,
843 .show = probes_profile_seq_show
846 static int profile_open(struct inode *inode, struct file *file)
848 return seq_open(file, &profile_seq_op);
851 static const struct file_operations kprobe_profile_ops = {
852 .owner = THIS_MODULE,
853 .open = profile_open,
856 .release = seq_release,
860 static __kprobes int kprobe_trace_func(struct kprobe *kp, struct pt_regs *regs)
862 struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
863 struct kprobe_trace_entry *entry;
864 struct ring_buffer_event *event;
865 struct ring_buffer *buffer;
867 unsigned long irq_flags;
868 struct ftrace_event_call *call = &tp->call;
872 local_save_flags(irq_flags);
873 pc = preempt_count();
875 size = SIZEOF_KPROBE_TRACE_ENTRY(tp->nr_args);
877 event = trace_current_buffer_lock_reserve(&buffer, call->id, size,
882 entry = ring_buffer_event_data(event);
883 entry->nargs = tp->nr_args;
884 entry->ip = (unsigned long)kp->addr;
885 for (i = 0; i < tp->nr_args; i++)
886 entry->args[i] = call_fetch(&tp->args[i].fetch, regs);
888 if (!filter_current_check_discard(buffer, call, entry, event))
889 trace_nowake_buffer_unlock_commit(buffer, event, irq_flags, pc);
893 /* Kretprobe handler */
894 static __kprobes int kretprobe_trace_func(struct kretprobe_instance *ri,
895 struct pt_regs *regs)
897 struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
898 struct kretprobe_trace_entry *entry;
899 struct ring_buffer_event *event;
900 struct ring_buffer *buffer;
902 unsigned long irq_flags;
903 struct ftrace_event_call *call = &tp->call;
905 local_save_flags(irq_flags);
906 pc = preempt_count();
908 size = SIZEOF_KRETPROBE_TRACE_ENTRY(tp->nr_args);
910 event = trace_current_buffer_lock_reserve(&buffer, call->id, size,
915 entry = ring_buffer_event_data(event);
916 entry->nargs = tp->nr_args;
917 entry->func = (unsigned long)tp->rp.kp.addr;
918 entry->ret_ip = (unsigned long)ri->ret_addr;
919 for (i = 0; i < tp->nr_args; i++)
920 entry->args[i] = call_fetch(&tp->args[i].fetch, regs);
922 if (!filter_current_check_discard(buffer, call, entry, event))
923 trace_nowake_buffer_unlock_commit(buffer, event, irq_flags, pc);
928 /* Event entry printers */
930 print_kprobe_event(struct trace_iterator *iter, int flags)
932 struct kprobe_trace_entry *field;
933 struct trace_seq *s = &iter->seq;
934 struct trace_event *event;
935 struct trace_probe *tp;
938 field = (struct kprobe_trace_entry *)iter->ent;
939 event = ftrace_find_event(field->ent.type);
940 tp = container_of(event, struct trace_probe, event);
942 if (!trace_seq_printf(s, "%s: (", tp->call.name))
945 if (!seq_print_ip_sym(s, field->ip, flags | TRACE_ITER_SYM_OFFSET))
948 if (!trace_seq_puts(s, ")"))
951 for (i = 0; i < field->nargs; i++)
952 if (!trace_seq_printf(s, " %s=%lx",
953 tp->args[i].name, field->args[i]))
956 if (!trace_seq_puts(s, "\n"))
959 return TRACE_TYPE_HANDLED;
961 return TRACE_TYPE_PARTIAL_LINE;
965 print_kretprobe_event(struct trace_iterator *iter, int flags)
967 struct kretprobe_trace_entry *field;
968 struct trace_seq *s = &iter->seq;
969 struct trace_event *event;
970 struct trace_probe *tp;
973 field = (struct kretprobe_trace_entry *)iter->ent;
974 event = ftrace_find_event(field->ent.type);
975 tp = container_of(event, struct trace_probe, event);
977 if (!trace_seq_printf(s, "%s: (", tp->call.name))
980 if (!seq_print_ip_sym(s, field->ret_ip, flags | TRACE_ITER_SYM_OFFSET))
983 if (!trace_seq_puts(s, " <- "))
986 if (!seq_print_ip_sym(s, field->func, flags & ~TRACE_ITER_SYM_OFFSET))
989 if (!trace_seq_puts(s, ")"))
992 for (i = 0; i < field->nargs; i++)
993 if (!trace_seq_printf(s, " %s=%lx",
994 tp->args[i].name, field->args[i]))
997 if (!trace_seq_puts(s, "\n"))
1000 return TRACE_TYPE_HANDLED;
1002 return TRACE_TYPE_PARTIAL_LINE;
1005 static int probe_event_enable(struct ftrace_event_call *call)
1007 struct trace_probe *tp = (struct trace_probe *)call->data;
1009 tp->flags |= TP_FLAG_TRACE;
1010 if (probe_is_return(tp))
1011 return enable_kretprobe(&tp->rp);
1013 return enable_kprobe(&tp->rp.kp);
1016 static void probe_event_disable(struct ftrace_event_call *call)
1018 struct trace_probe *tp = (struct trace_probe *)call->data;
1020 tp->flags &= ~TP_FLAG_TRACE;
1021 if (!(tp->flags & (TP_FLAG_TRACE | TP_FLAG_PROFILE))) {
1022 if (probe_is_return(tp))
1023 disable_kretprobe(&tp->rp);
1025 disable_kprobe(&tp->rp.kp);
1029 static int probe_event_raw_init(struct ftrace_event_call *event_call)
1031 INIT_LIST_HEAD(&event_call->fields);
1037 #define DEFINE_FIELD(type, item, name, is_signed) \
1039 ret = trace_define_field(event_call, #type, name, \
1040 offsetof(typeof(field), item), \
1041 sizeof(field.item), is_signed, \
1047 static int kprobe_event_define_fields(struct ftrace_event_call *event_call)
1050 struct kprobe_trace_entry field;
1051 struct trace_probe *tp = (struct trace_probe *)event_call->data;
1053 ret = trace_define_common_fields(event_call);
1057 DEFINE_FIELD(unsigned long, ip, "ip", 0);
1058 DEFINE_FIELD(int, nargs, "nargs", 1);
1059 /* Set argument names as fields */
1060 for (i = 0; i < tp->nr_args; i++)
1061 DEFINE_FIELD(unsigned long, args[i], tp->args[i].name, 0);
1065 static int kretprobe_event_define_fields(struct ftrace_event_call *event_call)
1068 struct kretprobe_trace_entry field;
1069 struct trace_probe *tp = (struct trace_probe *)event_call->data;
1071 ret = trace_define_common_fields(event_call);
1075 DEFINE_FIELD(unsigned long, func, "func", 0);
1076 DEFINE_FIELD(unsigned long, ret_ip, "ret_ip", 0);
1077 DEFINE_FIELD(int, nargs, "nargs", 1);
1078 /* Set argument names as fields */
1079 for (i = 0; i < tp->nr_args; i++)
1080 DEFINE_FIELD(unsigned long, args[i], tp->args[i].name, 0);
1084 static int __probe_event_show_format(struct trace_seq *s,
1085 struct trace_probe *tp, const char *fmt,
1091 if (!trace_seq_printf(s, "\nprint fmt: \"%s", fmt))
1094 for (i = 0; i < tp->nr_args; i++)
1095 if (!trace_seq_printf(s, " %s=%%lx", tp->args[i].name))
1098 if (!trace_seq_printf(s, "\", %s", arg))
1101 for (i = 0; i < tp->nr_args; i++)
1102 if (!trace_seq_printf(s, ", REC->%s", tp->args[i].name))
1105 return trace_seq_puts(s, "\n");
1109 #define SHOW_FIELD(type, item, name) \
1111 ret = trace_seq_printf(s, "\tfield: " #type " %s;\t" \
1112 "offset:%u;\tsize:%u;\n", name, \
1113 (unsigned int)offsetof(typeof(field), item),\
1114 (unsigned int)sizeof(type)); \
1119 static int kprobe_event_show_format(struct ftrace_event_call *call,
1120 struct trace_seq *s)
1122 struct kprobe_trace_entry field __attribute__((unused));
1124 struct trace_probe *tp = (struct trace_probe *)call->data;
1126 SHOW_FIELD(unsigned long, ip, "ip");
1127 SHOW_FIELD(int, nargs, "nargs");
1130 for (i = 0; i < tp->nr_args; i++)
1131 SHOW_FIELD(unsigned long, args[i], tp->args[i].name);
1132 trace_seq_puts(s, "\n");
1134 return __probe_event_show_format(s, tp, "(%lx)", "REC->ip");
1137 static int kretprobe_event_show_format(struct ftrace_event_call *call,
1138 struct trace_seq *s)
1140 struct kretprobe_trace_entry field __attribute__((unused));
1142 struct trace_probe *tp = (struct trace_probe *)call->data;
1144 SHOW_FIELD(unsigned long, func, "func");
1145 SHOW_FIELD(unsigned long, ret_ip, "ret_ip");
1146 SHOW_FIELD(int, nargs, "nargs");
1149 for (i = 0; i < tp->nr_args; i++)
1150 SHOW_FIELD(unsigned long, args[i], tp->args[i].name);
1151 trace_seq_puts(s, "\n");
1153 return __probe_event_show_format(s, tp, "(%lx <- %lx)",
1154 "REC->func, REC->ret_ip");
1157 #ifdef CONFIG_EVENT_PROFILE
1159 /* Kprobe profile handler */
1160 static __kprobes int kprobe_profile_func(struct kprobe *kp,
1161 struct pt_regs *regs)
1163 struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
1164 struct ftrace_event_call *call = &tp->call;
1165 struct kprobe_trace_entry *entry;
1166 struct trace_entry *ent;
1167 int size, __size, i, pc, __cpu;
1168 unsigned long irq_flags;
1171 pc = preempt_count();
1172 __size = SIZEOF_KPROBE_TRACE_ENTRY(tp->nr_args);
1173 size = ALIGN(__size + sizeof(u32), sizeof(u64));
1174 size -= sizeof(u32);
1175 if (WARN_ONCE(size > FTRACE_MAX_PROFILE_SIZE,
1176 "profile buffer not large enough"))
1180 * Protect the non nmi buffer
1181 * This also protects the rcu read side
1183 local_irq_save(irq_flags);
1184 __cpu = smp_processor_id();
1187 raw_data = rcu_dereference(trace_profile_buf_nmi);
1189 raw_data = rcu_dereference(trace_profile_buf);
1194 raw_data = per_cpu_ptr(raw_data, __cpu);
1195 /* Zero dead bytes from alignment to avoid buffer leak to userspace */
1196 *(u64 *)(&raw_data[size - sizeof(u64)]) = 0ULL;
1197 entry = (struct kprobe_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->ip = (unsigned long)kp->addr;
1204 for (i = 0; i < tp->nr_args; i++)
1205 entry->args[i] = call_fetch(&tp->args[i].fetch, regs);
1206 perf_tp_event(call->id, entry->ip, 1, entry, size);
1208 local_irq_restore(irq_flags);
1212 /* Kretprobe profile handler */
1213 static __kprobes int kretprobe_profile_func(struct kretprobe_instance *ri,
1214 struct pt_regs *regs)
1216 struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
1217 struct ftrace_event_call *call = &tp->call;
1218 struct kretprobe_trace_entry *entry;
1219 struct trace_entry *ent;
1220 int size, __size, i, pc, __cpu;
1221 unsigned long irq_flags;
1224 pc = preempt_count();
1225 __size = SIZEOF_KRETPROBE_TRACE_ENTRY(tp->nr_args);
1226 size = ALIGN(__size + sizeof(u32), sizeof(u64));
1227 size -= sizeof(u32);
1228 if (WARN_ONCE(size > FTRACE_MAX_PROFILE_SIZE,
1229 "profile buffer not large enough"))
1233 * Protect the non nmi buffer
1234 * This also protects the rcu read side
1236 local_irq_save(irq_flags);
1237 __cpu = smp_processor_id();
1240 raw_data = rcu_dereference(trace_profile_buf_nmi);
1242 raw_data = rcu_dereference(trace_profile_buf);
1247 raw_data = per_cpu_ptr(raw_data, __cpu);
1248 /* Zero dead bytes from alignment to avoid buffer leak to userspace */
1249 *(u64 *)(&raw_data[size - sizeof(u64)]) = 0ULL;
1250 entry = (struct kretprobe_trace_entry *)raw_data;
1253 tracing_generic_entry_update(ent, irq_flags, pc);
1254 ent->type = call->id;
1255 entry->nargs = tp->nr_args;
1256 entry->func = (unsigned long)tp->rp.kp.addr;
1257 entry->ret_ip = (unsigned long)ri->ret_addr;
1258 for (i = 0; i < tp->nr_args; i++)
1259 entry->args[i] = call_fetch(&tp->args[i].fetch, regs);
1260 perf_tp_event(call->id, entry->ret_ip, 1, entry, size);
1262 local_irq_restore(irq_flags);
1266 static int probe_profile_enable(struct ftrace_event_call *call)
1268 struct trace_probe *tp = (struct trace_probe *)call->data;
1270 tp->flags |= TP_FLAG_PROFILE;
1272 if (probe_is_return(tp))
1273 return enable_kretprobe(&tp->rp);
1275 return enable_kprobe(&tp->rp.kp);
1278 static void probe_profile_disable(struct ftrace_event_call *call)
1280 struct trace_probe *tp = (struct trace_probe *)call->data;
1282 tp->flags &= ~TP_FLAG_PROFILE;
1284 if (!(tp->flags & TP_FLAG_TRACE)) {
1285 if (probe_is_return(tp))
1286 disable_kretprobe(&tp->rp);
1288 disable_kprobe(&tp->rp.kp);
1291 #endif /* CONFIG_EVENT_PROFILE */
1295 int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs)
1297 struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
1299 if (tp->flags & TP_FLAG_TRACE)
1300 kprobe_trace_func(kp, regs);
1301 #ifdef CONFIG_EVENT_PROFILE
1302 if (tp->flags & TP_FLAG_PROFILE)
1303 kprobe_profile_func(kp, regs);
1304 #endif /* CONFIG_EVENT_PROFILE */
1305 return 0; /* We don't tweek kernel, so just return 0 */
1309 int kretprobe_dispatcher(struct kretprobe_instance *ri, struct pt_regs *regs)
1311 struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
1313 if (tp->flags & TP_FLAG_TRACE)
1314 kretprobe_trace_func(ri, regs);
1315 #ifdef CONFIG_EVENT_PROFILE
1316 if (tp->flags & TP_FLAG_PROFILE)
1317 kretprobe_profile_func(ri, regs);
1318 #endif /* CONFIG_EVENT_PROFILE */
1319 return 0; /* We don't tweek kernel, so just return 0 */
1322 static int register_probe_event(struct trace_probe *tp)
1324 struct ftrace_event_call *call = &tp->call;
1327 /* Initialize ftrace_event_call */
1328 if (probe_is_return(tp)) {
1329 tp->event.trace = print_kretprobe_event;
1330 call->raw_init = probe_event_raw_init;
1331 call->show_format = kretprobe_event_show_format;
1332 call->define_fields = kretprobe_event_define_fields;
1334 tp->event.trace = print_kprobe_event;
1335 call->raw_init = probe_event_raw_init;
1336 call->show_format = kprobe_event_show_format;
1337 call->define_fields = kprobe_event_define_fields;
1339 call->event = &tp->event;
1340 call->id = register_ftrace_event(&tp->event);
1344 call->regfunc = probe_event_enable;
1345 call->unregfunc = probe_event_disable;
1347 #ifdef CONFIG_EVENT_PROFILE
1348 atomic_set(&call->profile_count, -1);
1349 call->profile_enable = probe_profile_enable;
1350 call->profile_disable = probe_profile_disable;
1353 ret = trace_add_event_call(call);
1355 pr_info("Failed to register kprobe event: %s\n", call->name);
1356 unregister_ftrace_event(&tp->event);
1361 static void unregister_probe_event(struct trace_probe *tp)
1363 /* tp->event is unregistered in trace_remove_event_call() */
1364 trace_remove_event_call(&tp->call);
1367 /* Make a debugfs interface for controling probe points */
1368 static __init int init_kprobe_trace(void)
1370 struct dentry *d_tracer;
1371 struct dentry *entry;
1373 d_tracer = tracing_init_dentry();
1377 entry = debugfs_create_file("kprobe_events", 0644, d_tracer,
1378 NULL, &kprobe_events_ops);
1380 /* Event list interface */
1382 pr_warning("Could not create debugfs "
1383 "'kprobe_events' entry\n");
1385 /* Profile interface */
1386 entry = debugfs_create_file("kprobe_profile", 0444, d_tracer,
1387 NULL, &kprobe_profile_ops);
1390 pr_warning("Could not create debugfs "
1391 "'kprobe_profile' entry\n");
1394 fs_initcall(init_kprobe_trace);
1397 #ifdef CONFIG_FTRACE_STARTUP_TEST
1399 static int kprobe_trace_selftest_target(int a1, int a2, int a3,
1400 int a4, int a5, int a6)
1402 return a1 + a2 + a3 + a4 + a5 + a6;
1405 static __init int kprobe_trace_self_tests_init(void)
1408 int (*target)(int, int, int, int, int, int);
1410 target = kprobe_trace_selftest_target;
1412 pr_info("Testing kprobe tracing: ");
1414 ret = command_trace_probe("p:testprobe kprobe_trace_selftest_target "
1415 "a1 a2 a3 a4 a5 a6");
1416 if (WARN_ON_ONCE(ret))
1417 pr_warning("error enabling function entry\n");
1419 ret = command_trace_probe("r:testprobe2 kprobe_trace_selftest_target "
1421 if (WARN_ON_ONCE(ret))
1422 pr_warning("error enabling function return\n");
1424 ret = target(1, 2, 3, 4, 5, 6);
1426 cleanup_all_probes();
1432 late_initcall(kprobe_trace_self_tests_init);