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_stack_address(struct pt_regs *regs,
91 return kernel_stack_pointer(regs);
94 /* Memory fetching by symbol */
101 static unsigned long update_symbol_cache(struct symbol_cache *sc)
103 sc->addr = (unsigned long)kallsyms_lookup_name(sc->symbol);
105 sc->addr += sc->offset;
109 static void free_symbol_cache(struct symbol_cache *sc)
115 static struct symbol_cache *alloc_symbol_cache(const char *sym, long offset)
117 struct symbol_cache *sc;
119 if (!sym || strlen(sym) == 0)
121 sc = kzalloc(sizeof(struct symbol_cache), GFP_KERNEL);
125 sc->symbol = kstrdup(sym, GFP_KERNEL);
132 update_symbol_cache(sc);
136 static __kprobes unsigned long fetch_symbol(struct pt_regs *regs, void *data)
138 struct symbol_cache *sc = data;
141 return fetch_memory(regs, (void *)sc->addr);
146 /* Special indirect memory access interface */
147 struct indirect_fetch_data {
148 struct fetch_func orig;
152 static __kprobes unsigned long fetch_indirect(struct pt_regs *regs, void *data)
154 struct indirect_fetch_data *ind = data;
157 addr = call_fetch(&ind->orig, regs);
160 return fetch_memory(regs, (void *)addr);
165 static __kprobes void free_indirect_fetch_data(struct indirect_fetch_data *data)
167 if (data->orig.func == fetch_indirect)
168 free_indirect_fetch_data(data->orig.data);
169 else if (data->orig.func == fetch_symbol)
170 free_symbol_cache(data->orig.data);
175 * Kprobe tracer core functions
179 struct fetch_func fetch;
183 /* Flags for trace_probe */
184 #define TP_FLAG_TRACE 1
185 #define TP_FLAG_PROFILE 2
188 struct list_head list;
189 struct kretprobe rp; /* Use rp.kp for kprobe use */
191 unsigned int flags; /* For TP_FLAG_* */
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 __kprobes int probe_is_return(struct trace_probe *tp)
205 return tp->rp.handler != NULL;
208 static __kprobes const char *probe_symbol(struct trace_probe *tp)
210 return tp->symbol ? tp->symbol : "unknown";
213 static int probe_arg_string(char *buf, size_t n, struct fetch_func *ff)
217 if (ff->func == fetch_argument)
218 ret = snprintf(buf, n, "$arg%lu", (unsigned long)ff->data);
219 else if (ff->func == fetch_register) {
221 name = regs_query_register_name((unsigned int)((long)ff->data));
222 ret = snprintf(buf, n, "%%%s", name);
223 } else if (ff->func == fetch_stack)
224 ret = snprintf(buf, n, "$stack%lu", (unsigned long)ff->data);
225 else if (ff->func == fetch_memory)
226 ret = snprintf(buf, n, "@0x%p", ff->data);
227 else if (ff->func == fetch_symbol) {
228 struct symbol_cache *sc = ff->data;
229 ret = snprintf(buf, n, "@%s%+ld", sc->symbol, sc->offset);
230 } else if (ff->func == fetch_retvalue)
231 ret = snprintf(buf, n, "$retval");
232 else if (ff->func == fetch_stack_address)
233 ret = snprintf(buf, n, "$stack");
234 else if (ff->func == fetch_indirect) {
235 struct indirect_fetch_data *id = ff->data;
237 ret = snprintf(buf, n, "%+ld(", id->offset);
241 ret = probe_arg_string(buf + l, n - l, &id->orig);
245 ret = snprintf(buf + l, n - l, ")");
254 static int register_probe_event(struct trace_probe *tp);
255 static void unregister_probe_event(struct trace_probe *tp);
257 static DEFINE_MUTEX(probe_lock);
258 static LIST_HEAD(probe_list);
260 static int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs);
261 static int kretprobe_dispatcher(struct kretprobe_instance *ri,
262 struct pt_regs *regs);
265 * Allocate new trace_probe and initialize it (including kprobes).
267 static struct trace_probe *alloc_trace_probe(const char *group,
272 int nargs, int is_return)
274 struct trace_probe *tp;
276 tp = kzalloc(SIZEOF_TRACE_PROBE(nargs), GFP_KERNEL);
278 return ERR_PTR(-ENOMEM);
281 tp->symbol = kstrdup(symbol, GFP_KERNEL);
284 tp->rp.kp.symbol_name = tp->symbol;
285 tp->rp.kp.offset = offs;
287 tp->rp.kp.addr = addr;
290 tp->rp.handler = kretprobe_dispatcher;
292 tp->rp.kp.pre_handler = kprobe_dispatcher;
296 tp->call.name = kstrdup(event, GFP_KERNEL);
302 tp->call.system = kstrdup(group, GFP_KERNEL);
303 if (!tp->call.system)
306 INIT_LIST_HEAD(&tp->list);
309 kfree(tp->call.name);
312 return ERR_PTR(-ENOMEM);
315 static void free_probe_arg(struct probe_arg *arg)
317 if (arg->fetch.func == fetch_symbol)
318 free_symbol_cache(arg->fetch.data);
319 else if (arg->fetch.func == fetch_indirect)
320 free_indirect_fetch_data(arg->fetch.data);
324 static void free_trace_probe(struct trace_probe *tp)
328 for (i = 0; i < tp->nr_args; i++)
329 free_probe_arg(&tp->args[i]);
331 kfree(tp->call.system);
332 kfree(tp->call.name);
337 static struct trace_probe *find_probe_event(const char *event)
339 struct trace_probe *tp;
341 list_for_each_entry(tp, &probe_list, list)
342 if (!strcmp(tp->call.name, event))
347 /* Unregister a trace_probe and probe_event: call with locking probe_lock */
348 static void unregister_trace_probe(struct trace_probe *tp)
350 if (probe_is_return(tp))
351 unregister_kretprobe(&tp->rp);
353 unregister_kprobe(&tp->rp.kp);
355 unregister_probe_event(tp);
358 /* Register a trace_probe and probe_event */
359 static int register_trace_probe(struct trace_probe *tp)
361 struct trace_probe *old_tp;
364 mutex_lock(&probe_lock);
366 /* register as an event */
367 old_tp = find_probe_event(tp->call.name);
369 /* delete old event */
370 unregister_trace_probe(old_tp);
371 free_trace_probe(old_tp);
373 ret = register_probe_event(tp);
375 pr_warning("Faild to register probe event(%d)\n", ret);
379 tp->rp.kp.flags |= KPROBE_FLAG_DISABLED;
380 if (probe_is_return(tp))
381 ret = register_kretprobe(&tp->rp);
383 ret = register_kprobe(&tp->rp.kp);
386 pr_warning("Could not insert probe(%d)\n", ret);
387 if (ret == -EILSEQ) {
388 pr_warning("Probing address(0x%p) is not an "
389 "instruction boundary.\n",
393 unregister_probe_event(tp);
395 list_add_tail(&tp->list, &probe_list);
397 mutex_unlock(&probe_lock);
401 /* Split symbol and offset. */
402 static int split_symbol_offset(char *symbol, unsigned long *offset)
410 tmp = strchr(symbol, '+');
412 /* skip sign because strict_strtol doesn't accept '+' */
413 ret = strict_strtoul(tmp + 1, 0, offset);
422 #define PARAM_MAX_ARGS 16
423 #define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long))
425 static int parse_probe_vars(char *arg, struct fetch_func *ff, int is_return)
430 if (strcmp(arg, "retval") == 0) {
432 ff->func = fetch_retvalue;
436 } else if (strncmp(arg, "stack", 5) == 0) {
437 if (arg[5] == '\0') {
438 ff->func = fetch_stack_address;
440 } else if (isdigit(arg[5])) {
441 ret = strict_strtoul(arg + 5, 10, ¶m);
442 if (ret || param > PARAM_MAX_STACK)
445 ff->func = fetch_stack;
446 ff->data = (void *)param;
450 } else if (strncmp(arg, "arg", 3) == 0 && isdigit(arg[3])) {
451 ret = strict_strtoul(arg + 3, 10, ¶m);
452 if (ret || param > PARAM_MAX_ARGS)
455 ff->func = fetch_argument;
456 ff->data = (void *)param;
463 static int parse_probe_arg(char *arg, struct fetch_func *ff, int is_return)
472 ret = parse_probe_vars(arg + 1, ff, is_return);
474 case '%': /* named register */
475 ret = regs_query_register_offset(arg + 1);
477 ff->func = fetch_register;
478 ff->data = (void *)(unsigned long)ret;
482 case '@': /* memory or symbol */
483 if (isdigit(arg[1])) {
484 ret = strict_strtoul(arg + 1, 0, ¶m);
487 ff->func = fetch_memory;
488 ff->data = (void *)param;
490 ret = split_symbol_offset(arg + 1, &offset);
493 ff->data = alloc_symbol_cache(arg + 1, offset);
495 ff->func = fetch_symbol;
500 case '+': /* indirect memory */
502 tmp = strchr(arg, '(');
508 ret = strict_strtol(arg + 1, 0, &offset);
514 tmp = strrchr(arg, ')');
516 struct indirect_fetch_data *id;
518 id = kzalloc(sizeof(struct indirect_fetch_data),
523 ret = parse_probe_arg(arg, &id->orig, is_return);
527 ff->func = fetch_indirect;
528 ff->data = (void *)id;
534 /* TODO: support custom handler */
540 static int create_trace_probe(int argc, char **argv)
544 * - Add kprobe: p[:[GRP/]EVENT] KSYM[+OFFS]|KADDR [FETCHARGS]
545 * - Add kretprobe: r[:[GRP/]EVENT] KSYM[+0] [FETCHARGS]
547 * $argN : fetch Nth of function argument. (N:0-)
548 * $retval : fetch return value
549 * $stack : fetch stack address
550 * $stackN : 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 tp->flags |= TP_FLAG_TRACE;
995 if (probe_is_return(tp))
996 return enable_kretprobe(&tp->rp);
998 return enable_kprobe(&tp->rp.kp);
1001 static void probe_event_disable(struct ftrace_event_call *call)
1003 struct trace_probe *tp = (struct trace_probe *)call->data;
1005 tp->flags &= ~TP_FLAG_TRACE;
1006 if (!(tp->flags & (TP_FLAG_TRACE | TP_FLAG_PROFILE))) {
1007 if (probe_is_return(tp))
1008 disable_kretprobe(&tp->rp);
1010 disable_kprobe(&tp->rp.kp);
1014 static int probe_event_raw_init(struct ftrace_event_call *event_call)
1016 INIT_LIST_HEAD(&event_call->fields);
1022 #define DEFINE_FIELD(type, item, name, is_signed) \
1024 ret = trace_define_field(event_call, #type, name, \
1025 offsetof(typeof(field), item), \
1026 sizeof(field.item), is_signed, \
1032 static int kprobe_event_define_fields(struct ftrace_event_call *event_call)
1035 struct kprobe_trace_entry field;
1036 struct trace_probe *tp = (struct trace_probe *)event_call->data;
1038 ret = trace_define_common_fields(event_call);
1042 DEFINE_FIELD(unsigned long, ip, "ip", 0);
1043 DEFINE_FIELD(int, nargs, "nargs", 1);
1044 /* Set argument names as fields */
1045 for (i = 0; i < tp->nr_args; i++)
1046 DEFINE_FIELD(unsigned long, args[i], tp->args[i].name, 0);
1050 static int kretprobe_event_define_fields(struct ftrace_event_call *event_call)
1053 struct kretprobe_trace_entry field;
1054 struct trace_probe *tp = (struct trace_probe *)event_call->data;
1056 ret = trace_define_common_fields(event_call);
1060 DEFINE_FIELD(unsigned long, func, "func", 0);
1061 DEFINE_FIELD(unsigned long, ret_ip, "ret_ip", 0);
1062 DEFINE_FIELD(int, nargs, "nargs", 1);
1063 /* Set argument names as fields */
1064 for (i = 0; i < tp->nr_args; i++)
1065 DEFINE_FIELD(unsigned long, args[i], tp->args[i].name, 0);
1069 static int __probe_event_show_format(struct trace_seq *s,
1070 struct trace_probe *tp, const char *fmt,
1076 if (!trace_seq_printf(s, "\nprint fmt: \"%s", fmt))
1079 for (i = 0; i < tp->nr_args; i++)
1080 if (!trace_seq_printf(s, " %s=%%lx", tp->args[i].name))
1083 if (!trace_seq_printf(s, "\", %s", arg))
1086 for (i = 0; i < tp->nr_args; i++)
1087 if (!trace_seq_printf(s, ", REC->%s", tp->args[i].name))
1090 return trace_seq_puts(s, "\n");
1094 #define SHOW_FIELD(type, item, name) \
1096 ret = trace_seq_printf(s, "\tfield: " #type " %s;\t" \
1097 "offset:%u;\tsize:%u;\n", name, \
1098 (unsigned int)offsetof(typeof(field), item),\
1099 (unsigned int)sizeof(type)); \
1104 static int kprobe_event_show_format(struct ftrace_event_call *call,
1105 struct trace_seq *s)
1107 struct kprobe_trace_entry field __attribute__((unused));
1109 struct trace_probe *tp = (struct trace_probe *)call->data;
1111 SHOW_FIELD(unsigned long, ip, "ip");
1112 SHOW_FIELD(int, nargs, "nargs");
1115 for (i = 0; i < tp->nr_args; i++)
1116 SHOW_FIELD(unsigned long, args[i], tp->args[i].name);
1117 trace_seq_puts(s, "\n");
1119 return __probe_event_show_format(s, tp, "(%lx)", "REC->ip");
1122 static int kretprobe_event_show_format(struct ftrace_event_call *call,
1123 struct trace_seq *s)
1125 struct kretprobe_trace_entry field __attribute__((unused));
1127 struct trace_probe *tp = (struct trace_probe *)call->data;
1129 SHOW_FIELD(unsigned long, func, "func");
1130 SHOW_FIELD(unsigned long, ret_ip, "ret_ip");
1131 SHOW_FIELD(int, nargs, "nargs");
1134 for (i = 0; i < tp->nr_args; i++)
1135 SHOW_FIELD(unsigned long, args[i], tp->args[i].name);
1136 trace_seq_puts(s, "\n");
1138 return __probe_event_show_format(s, tp, "(%lx <- %lx)",
1139 "REC->func, REC->ret_ip");
1142 #ifdef CONFIG_EVENT_PROFILE
1144 /* Kprobe profile handler */
1145 static __kprobes int kprobe_profile_func(struct kprobe *kp,
1146 struct pt_regs *regs)
1148 struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
1149 struct ftrace_event_call *call = &tp->call;
1150 struct kprobe_trace_entry *entry;
1151 struct trace_entry *ent;
1152 int size, __size, i, pc, __cpu;
1153 unsigned long irq_flags;
1156 pc = preempt_count();
1157 __size = SIZEOF_KPROBE_TRACE_ENTRY(tp->nr_args);
1158 size = ALIGN(__size + sizeof(u32), sizeof(u64));
1159 size -= sizeof(u32);
1160 if (WARN_ONCE(size > FTRACE_MAX_PROFILE_SIZE,
1161 "profile buffer not large enough"))
1165 * Protect the non nmi buffer
1166 * This also protects the rcu read side
1168 local_irq_save(irq_flags);
1169 __cpu = smp_processor_id();
1172 raw_data = rcu_dereference(trace_profile_buf_nmi);
1174 raw_data = rcu_dereference(trace_profile_buf);
1179 raw_data = per_cpu_ptr(raw_data, __cpu);
1180 /* Zero dead bytes from alignment to avoid buffer leak to userspace */
1181 *(u64 *)(&raw_data[size - sizeof(u64)]) = 0ULL;
1182 entry = (struct kprobe_trace_entry *)raw_data;
1185 tracing_generic_entry_update(ent, irq_flags, pc);
1186 ent->type = call->id;
1187 entry->nargs = tp->nr_args;
1188 entry->ip = (unsigned long)kp->addr;
1189 for (i = 0; i < tp->nr_args; i++)
1190 entry->args[i] = call_fetch(&tp->args[i].fetch, regs);
1191 perf_tp_event(call->id, entry->ip, 1, entry, size);
1193 local_irq_restore(irq_flags);
1197 /* Kretprobe profile handler */
1198 static __kprobes int kretprobe_profile_func(struct kretprobe_instance *ri,
1199 struct pt_regs *regs)
1201 struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
1202 struct ftrace_event_call *call = &tp->call;
1203 struct kretprobe_trace_entry *entry;
1204 struct trace_entry *ent;
1205 int size, __size, i, pc, __cpu;
1206 unsigned long irq_flags;
1209 pc = preempt_count();
1210 __size = SIZEOF_KRETPROBE_TRACE_ENTRY(tp->nr_args);
1211 size = ALIGN(__size + sizeof(u32), sizeof(u64));
1212 size -= sizeof(u32);
1213 if (WARN_ONCE(size > FTRACE_MAX_PROFILE_SIZE,
1214 "profile buffer not large enough"))
1218 * Protect the non nmi buffer
1219 * This also protects the rcu read side
1221 local_irq_save(irq_flags);
1222 __cpu = smp_processor_id();
1225 raw_data = rcu_dereference(trace_profile_buf_nmi);
1227 raw_data = rcu_dereference(trace_profile_buf);
1232 raw_data = per_cpu_ptr(raw_data, __cpu);
1233 /* Zero dead bytes from alignment to avoid buffer leak to userspace */
1234 *(u64 *)(&raw_data[size - sizeof(u64)]) = 0ULL;
1235 entry = (struct kretprobe_trace_entry *)raw_data;
1238 tracing_generic_entry_update(ent, irq_flags, pc);
1239 ent->type = call->id;
1240 entry->nargs = tp->nr_args;
1241 entry->func = (unsigned long)tp->rp.kp.addr;
1242 entry->ret_ip = (unsigned long)ri->ret_addr;
1243 for (i = 0; i < tp->nr_args; i++)
1244 entry->args[i] = call_fetch(&tp->args[i].fetch, regs);
1245 perf_tp_event(call->id, entry->ret_ip, 1, entry, size);
1247 local_irq_restore(irq_flags);
1251 static int probe_profile_enable(struct ftrace_event_call *call)
1253 struct trace_probe *tp = (struct trace_probe *)call->data;
1255 tp->flags |= TP_FLAG_PROFILE;
1257 if (probe_is_return(tp))
1258 return enable_kretprobe(&tp->rp);
1260 return enable_kprobe(&tp->rp.kp);
1263 static void probe_profile_disable(struct ftrace_event_call *call)
1265 struct trace_probe *tp = (struct trace_probe *)call->data;
1267 tp->flags &= ~TP_FLAG_PROFILE;
1269 if (!(tp->flags & TP_FLAG_TRACE)) {
1270 if (probe_is_return(tp))
1271 disable_kretprobe(&tp->rp);
1273 disable_kprobe(&tp->rp.kp);
1276 #endif /* CONFIG_EVENT_PROFILE */
1280 int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs)
1282 struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
1284 if (tp->flags & TP_FLAG_TRACE)
1285 kprobe_trace_func(kp, regs);
1286 #ifdef CONFIG_EVENT_PROFILE
1287 if (tp->flags & TP_FLAG_PROFILE)
1288 kprobe_profile_func(kp, regs);
1289 #endif /* CONFIG_EVENT_PROFILE */
1290 return 0; /* We don't tweek kernel, so just return 0 */
1294 int kretprobe_dispatcher(struct kretprobe_instance *ri, struct pt_regs *regs)
1296 struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
1298 if (tp->flags & TP_FLAG_TRACE)
1299 kretprobe_trace_func(ri, regs);
1300 #ifdef CONFIG_EVENT_PROFILE
1301 if (tp->flags & TP_FLAG_PROFILE)
1302 kretprobe_profile_func(ri, regs);
1303 #endif /* CONFIG_EVENT_PROFILE */
1304 return 0; /* We don't tweek kernel, so just return 0 */
1307 static int register_probe_event(struct trace_probe *tp)
1309 struct ftrace_event_call *call = &tp->call;
1312 /* Initialize ftrace_event_call */
1313 if (probe_is_return(tp)) {
1314 tp->event.trace = print_kretprobe_event;
1315 call->raw_init = probe_event_raw_init;
1316 call->show_format = kretprobe_event_show_format;
1317 call->define_fields = kretprobe_event_define_fields;
1319 tp->event.trace = print_kprobe_event;
1320 call->raw_init = probe_event_raw_init;
1321 call->show_format = kprobe_event_show_format;
1322 call->define_fields = kprobe_event_define_fields;
1324 call->event = &tp->event;
1325 call->id = register_ftrace_event(&tp->event);
1329 call->regfunc = probe_event_enable;
1330 call->unregfunc = probe_event_disable;
1332 #ifdef CONFIG_EVENT_PROFILE
1333 atomic_set(&call->profile_count, -1);
1334 call->profile_enable = probe_profile_enable;
1335 call->profile_disable = probe_profile_disable;
1338 ret = trace_add_event_call(call);
1340 pr_info("Failed to register kprobe event: %s\n", call->name);
1341 unregister_ftrace_event(&tp->event);
1346 static void unregister_probe_event(struct trace_probe *tp)
1348 /* tp->event is unregistered in trace_remove_event_call() */
1349 trace_remove_event_call(&tp->call);
1352 /* Make a debugfs interface for controling probe points */
1353 static __init int init_kprobe_trace(void)
1355 struct dentry *d_tracer;
1356 struct dentry *entry;
1358 d_tracer = tracing_init_dentry();
1362 entry = debugfs_create_file("kprobe_events", 0644, d_tracer,
1363 NULL, &kprobe_events_ops);
1365 /* Event list interface */
1367 pr_warning("Could not create debugfs "
1368 "'kprobe_events' entry\n");
1370 /* Profile interface */
1371 entry = debugfs_create_file("kprobe_profile", 0444, d_tracer,
1372 NULL, &kprobe_profile_ops);
1375 pr_warning("Could not create debugfs "
1376 "'kprobe_profile' entry\n");
1379 fs_initcall(init_kprobe_trace);
1382 #ifdef CONFIG_FTRACE_STARTUP_TEST
1384 static int kprobe_trace_selftest_target(int a1, int a2, int a3,
1385 int a4, int a5, int a6)
1387 return a1 + a2 + a3 + a4 + a5 + a6;
1390 static __init int kprobe_trace_self_tests_init(void)
1393 int (*target)(int, int, int, int, int, int);
1395 target = kprobe_trace_selftest_target;
1397 pr_info("Testing kprobe tracing: ");
1399 ret = command_trace_probe("p:testprobe kprobe_trace_selftest_target "
1400 "a1 a2 a3 a4 a5 a6");
1401 if (WARN_ON_ONCE(ret))
1402 pr_warning("error enabling function entry\n");
1404 ret = command_trace_probe("r:testprobe2 kprobe_trace_selftest_target "
1406 if (WARN_ON_ONCE(ret))
1407 pr_warning("error enabling function return\n");
1409 ret = target(1, 2, 3, 4, 5, 6);
1411 cleanup_all_probes();
1417 late_initcall(kprobe_trace_self_tests_init);