2 * Kprobes-based tracing events
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 /* Reserved field names */
42 #define FIELD_STRING_IP "__probe_ip"
43 #define FIELD_STRING_NARGS "__probe_nargs"
44 #define FIELD_STRING_RETIP "__probe_ret_ip"
45 #define FIELD_STRING_FUNC "__probe_func"
47 const char *reserved_field_names[] = {
50 "common_preempt_count",
61 unsigned long (*func)(struct pt_regs *, void *);
65 static __kprobes unsigned long call_fetch(struct fetch_func *f,
68 return f->func(regs, f->data);
72 static __kprobes unsigned long fetch_register(struct pt_regs *regs,
75 return regs_get_register(regs, (unsigned int)((unsigned long)offset));
78 static __kprobes unsigned long fetch_stack(struct pt_regs *regs,
81 return regs_get_kernel_stack_nth(regs,
82 (unsigned int)((unsigned long)num));
85 static __kprobes unsigned long fetch_memory(struct pt_regs *regs, void *addr)
89 if (probe_kernel_address(addr, retval))
94 static __kprobes unsigned long fetch_argument(struct pt_regs *regs, void *num)
96 return regs_get_argument_nth(regs, (unsigned int)((unsigned long)num));
99 static __kprobes unsigned long fetch_retvalue(struct pt_regs *regs,
102 return regs_return_value(regs);
105 static __kprobes unsigned long fetch_stack_address(struct pt_regs *regs,
108 return kernel_stack_pointer(regs);
111 /* Memory fetching by symbol */
112 struct symbol_cache {
118 static unsigned long update_symbol_cache(struct symbol_cache *sc)
120 sc->addr = (unsigned long)kallsyms_lookup_name(sc->symbol);
122 sc->addr += sc->offset;
126 static void free_symbol_cache(struct symbol_cache *sc)
132 static struct symbol_cache *alloc_symbol_cache(const char *sym, long offset)
134 struct symbol_cache *sc;
136 if (!sym || strlen(sym) == 0)
138 sc = kzalloc(sizeof(struct symbol_cache), GFP_KERNEL);
142 sc->symbol = kstrdup(sym, GFP_KERNEL);
149 update_symbol_cache(sc);
153 static __kprobes unsigned long fetch_symbol(struct pt_regs *regs, void *data)
155 struct symbol_cache *sc = data;
158 return fetch_memory(regs, (void *)sc->addr);
163 /* Special indirect memory access interface */
164 struct indirect_fetch_data {
165 struct fetch_func orig;
169 static __kprobes unsigned long fetch_indirect(struct pt_regs *regs, void *data)
171 struct indirect_fetch_data *ind = data;
174 addr = call_fetch(&ind->orig, regs);
177 return fetch_memory(regs, (void *)addr);
182 static __kprobes void free_indirect_fetch_data(struct indirect_fetch_data *data)
184 if (data->orig.func == fetch_indirect)
185 free_indirect_fetch_data(data->orig.data);
186 else if (data->orig.func == fetch_symbol)
187 free_symbol_cache(data->orig.data);
192 * Kprobe event core functions
196 struct fetch_func fetch;
200 /* Flags for trace_probe */
201 #define TP_FLAG_TRACE 1
202 #define TP_FLAG_PROFILE 2
205 struct list_head list;
206 struct kretprobe rp; /* Use rp.kp for kprobe use */
208 unsigned int flags; /* For TP_FLAG_* */
209 const char *symbol; /* symbol name */
210 struct ftrace_event_call call;
211 struct trace_event event;
212 unsigned int nr_args;
213 struct probe_arg args[];
216 #define SIZEOF_TRACE_PROBE(n) \
217 (offsetof(struct trace_probe, args) + \
218 (sizeof(struct probe_arg) * (n)))
220 static __kprobes int probe_is_return(struct trace_probe *tp)
222 return tp->rp.handler != NULL;
225 static __kprobes const char *probe_symbol(struct trace_probe *tp)
227 return tp->symbol ? tp->symbol : "unknown";
230 static int probe_arg_string(char *buf, size_t n, struct fetch_func *ff)
234 if (ff->func == fetch_argument)
235 ret = snprintf(buf, n, "$arg%lu", (unsigned long)ff->data);
236 else if (ff->func == fetch_register) {
238 name = regs_query_register_name((unsigned int)((long)ff->data));
239 ret = snprintf(buf, n, "%%%s", name);
240 } else if (ff->func == fetch_stack)
241 ret = snprintf(buf, n, "$stack%lu", (unsigned long)ff->data);
242 else if (ff->func == fetch_memory)
243 ret = snprintf(buf, n, "@0x%p", ff->data);
244 else if (ff->func == fetch_symbol) {
245 struct symbol_cache *sc = ff->data;
246 ret = snprintf(buf, n, "@%s%+ld", sc->symbol, sc->offset);
247 } else if (ff->func == fetch_retvalue)
248 ret = snprintf(buf, n, "$retval");
249 else if (ff->func == fetch_stack_address)
250 ret = snprintf(buf, n, "$stack");
251 else if (ff->func == fetch_indirect) {
252 struct indirect_fetch_data *id = ff->data;
254 ret = snprintf(buf, n, "%+ld(", id->offset);
258 ret = probe_arg_string(buf + l, n - l, &id->orig);
262 ret = snprintf(buf + l, n - l, ")");
271 static int register_probe_event(struct trace_probe *tp);
272 static void unregister_probe_event(struct trace_probe *tp);
274 static DEFINE_MUTEX(probe_lock);
275 static LIST_HEAD(probe_list);
277 static int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs);
278 static int kretprobe_dispatcher(struct kretprobe_instance *ri,
279 struct pt_regs *regs);
282 * Allocate new trace_probe and initialize it (including kprobes).
284 static struct trace_probe *alloc_trace_probe(const char *group,
289 int nargs, int is_return)
291 struct trace_probe *tp;
293 tp = kzalloc(SIZEOF_TRACE_PROBE(nargs), GFP_KERNEL);
295 return ERR_PTR(-ENOMEM);
298 tp->symbol = kstrdup(symbol, GFP_KERNEL);
301 tp->rp.kp.symbol_name = tp->symbol;
302 tp->rp.kp.offset = offs;
304 tp->rp.kp.addr = addr;
307 tp->rp.handler = kretprobe_dispatcher;
309 tp->rp.kp.pre_handler = kprobe_dispatcher;
313 tp->call.name = kstrdup(event, GFP_KERNEL);
319 tp->call.system = kstrdup(group, GFP_KERNEL);
320 if (!tp->call.system)
323 INIT_LIST_HEAD(&tp->list);
326 kfree(tp->call.name);
329 return ERR_PTR(-ENOMEM);
332 static void free_probe_arg(struct probe_arg *arg)
334 if (arg->fetch.func == fetch_symbol)
335 free_symbol_cache(arg->fetch.data);
336 else if (arg->fetch.func == fetch_indirect)
337 free_indirect_fetch_data(arg->fetch.data);
341 static void free_trace_probe(struct trace_probe *tp)
345 for (i = 0; i < tp->nr_args; i++)
346 free_probe_arg(&tp->args[i]);
348 kfree(tp->call.system);
349 kfree(tp->call.name);
354 static struct trace_probe *find_probe_event(const char *event,
357 struct trace_probe *tp;
359 list_for_each_entry(tp, &probe_list, list)
360 if (strcmp(tp->call.name, event) == 0 &&
361 strcmp(tp->call.system, group) == 0)
366 /* Unregister a trace_probe and probe_event: call with locking probe_lock */
367 static void unregister_trace_probe(struct trace_probe *tp)
369 if (probe_is_return(tp))
370 unregister_kretprobe(&tp->rp);
372 unregister_kprobe(&tp->rp.kp);
374 unregister_probe_event(tp);
377 /* Register a trace_probe and probe_event */
378 static int register_trace_probe(struct trace_probe *tp)
380 struct trace_probe *old_tp;
383 mutex_lock(&probe_lock);
385 /* register as an event */
386 old_tp = find_probe_event(tp->call.name, tp->call.system);
388 /* delete old event */
389 unregister_trace_probe(old_tp);
390 free_trace_probe(old_tp);
392 ret = register_probe_event(tp);
394 pr_warning("Faild to register probe event(%d)\n", ret);
398 tp->rp.kp.flags |= KPROBE_FLAG_DISABLED;
399 if (probe_is_return(tp))
400 ret = register_kretprobe(&tp->rp);
402 ret = register_kprobe(&tp->rp.kp);
405 pr_warning("Could not insert probe(%d)\n", ret);
406 if (ret == -EILSEQ) {
407 pr_warning("Probing address(0x%p) is not an "
408 "instruction boundary.\n",
412 unregister_probe_event(tp);
414 list_add_tail(&tp->list, &probe_list);
416 mutex_unlock(&probe_lock);
420 /* Split symbol and offset. */
421 static int split_symbol_offset(char *symbol, unsigned long *offset)
429 tmp = strchr(symbol, '+');
431 /* skip sign because strict_strtol doesn't accept '+' */
432 ret = strict_strtoul(tmp + 1, 0, offset);
441 #define PARAM_MAX_ARGS 16
442 #define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long))
444 static int parse_probe_vars(char *arg, struct fetch_func *ff, int is_return)
449 if (strcmp(arg, "retval") == 0) {
451 ff->func = fetch_retvalue;
455 } else if (strncmp(arg, "stack", 5) == 0) {
456 if (arg[5] == '\0') {
457 ff->func = fetch_stack_address;
459 } else if (isdigit(arg[5])) {
460 ret = strict_strtoul(arg + 5, 10, ¶m);
461 if (ret || param > PARAM_MAX_STACK)
464 ff->func = fetch_stack;
465 ff->data = (void *)param;
469 } else if (strncmp(arg, "arg", 3) == 0 && isdigit(arg[3])) {
470 ret = strict_strtoul(arg + 3, 10, ¶m);
471 if (ret || param > PARAM_MAX_ARGS)
474 ff->func = fetch_argument;
475 ff->data = (void *)param;
482 static int parse_probe_arg(char *arg, struct fetch_func *ff, int is_return)
491 ret = parse_probe_vars(arg + 1, ff, is_return);
493 case '%': /* named register */
494 ret = regs_query_register_offset(arg + 1);
496 ff->func = fetch_register;
497 ff->data = (void *)(unsigned long)ret;
501 case '@': /* memory or symbol */
502 if (isdigit(arg[1])) {
503 ret = strict_strtoul(arg + 1, 0, ¶m);
506 ff->func = fetch_memory;
507 ff->data = (void *)param;
509 ret = split_symbol_offset(arg + 1, &offset);
512 ff->data = alloc_symbol_cache(arg + 1, offset);
514 ff->func = fetch_symbol;
519 case '+': /* indirect memory */
521 tmp = strchr(arg, '(');
527 ret = strict_strtol(arg + 1, 0, &offset);
533 tmp = strrchr(arg, ')');
535 struct indirect_fetch_data *id;
537 id = kzalloc(sizeof(struct indirect_fetch_data),
542 ret = parse_probe_arg(arg, &id->orig, is_return);
546 ff->func = fetch_indirect;
547 ff->data = (void *)id;
553 /* TODO: support custom handler */
559 /* Return 1 if name is reserved or already used by another argument */
560 static int conflict_field_name(const char *name,
561 struct probe_arg *args, int narg)
564 for (i = 0; i < ARRAY_SIZE(reserved_field_names); i++)
565 if (strcmp(reserved_field_names[i], name) == 0)
567 for (i = 0; i < narg; i++)
568 if (strcmp(args[i].name, name) == 0)
573 static int create_trace_probe(int argc, char **argv)
577 * - Add kprobe: p[:[GRP/]EVENT] KSYM[+OFFS]|KADDR [FETCHARGS]
578 * - Add kretprobe: r[:[GRP/]EVENT] KSYM[+0] [FETCHARGS]
580 * $argN : fetch Nth of function argument. (N:0-)
581 * $retval : fetch return value
582 * $stack : fetch stack address
583 * $stackN : fetch Nth of stack (N:0-)
584 * @ADDR : fetch memory at ADDR (ADDR should be in kernel)
585 * @SYM[+|-offs] : fetch memory at SYM +|- offs (SYM is a data symbol)
586 * %REG : fetch register REG
587 * Indirect memory fetch:
588 * +|-offs(ARG) : fetch memory at ARG +|- offs address.
589 * Alias name of args:
590 * NAME=FETCHARG : set NAME as alias of FETCHARG.
592 struct trace_probe *tp;
595 char *symbol = NULL, *event = NULL, *arg = NULL, *group = NULL;
596 unsigned long offset = 0;
598 char buf[MAX_EVENT_NAME_LEN];
601 pr_info("Probe point is not specified.\n");
605 if (argv[0][0] == 'p')
607 else if (argv[0][0] == 'r')
610 pr_info("Probe definition must be started with 'p' or 'r'.\n");
614 if (argv[0][1] == ':') {
616 if (strchr(event, '/')) {
618 event = strchr(group, '/') + 1;
620 if (strlen(group) == 0) {
621 pr_info("Group name is not specifiled\n");
625 if (strlen(event) == 0) {
626 pr_info("Event name is not specifiled\n");
631 if (isdigit(argv[1][0])) {
633 pr_info("Return probe point must be a symbol.\n");
636 /* an address specified */
637 ret = strict_strtoul(&argv[0][2], 0, (unsigned long *)&addr);
639 pr_info("Failed to parse address.\n");
643 /* a symbol specified */
645 /* TODO: support .init module functions */
646 ret = split_symbol_offset(symbol, &offset);
648 pr_info("Failed to parse symbol.\n");
651 if (offset && is_return) {
652 pr_info("Return probe must be used without offset.\n");
656 argc -= 2; argv += 2;
660 group = KPROBE_EVENT_SYSTEM;
662 /* Make a new event name */
664 snprintf(buf, MAX_EVENT_NAME_LEN, "%c@%s%+ld",
665 is_return ? 'r' : 'p', symbol, offset);
667 snprintf(buf, MAX_EVENT_NAME_LEN, "%c@0x%p",
668 is_return ? 'r' : 'p', addr);
671 tp = alloc_trace_probe(group, event, addr, symbol, offset, argc,
674 pr_info("Failed to allocate trace_probe.(%d)\n",
679 /* parse arguments */
681 for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
682 /* Parse argument name */
683 arg = strchr(argv[i], '=');
689 if (conflict_field_name(argv[i], tp->args, i)) {
690 pr_info("Argument%d name '%s' conflicts with "
691 "another field.\n", i, argv[i]);
696 tp->args[i].name = kstrdup(argv[i], GFP_KERNEL);
698 /* Parse fetch argument */
699 if (strlen(arg) > MAX_ARGSTR_LEN) {
700 pr_info("Argument%d(%s) is too long.\n", i, arg);
704 ret = parse_probe_arg(arg, &tp->args[i].fetch, is_return);
706 pr_info("Parse error at argument%d. (%d)\n", i, ret);
712 ret = register_trace_probe(tp);
718 free_trace_probe(tp);
722 static void cleanup_all_probes(void)
724 struct trace_probe *tp;
726 mutex_lock(&probe_lock);
727 /* TODO: Use batch unregistration */
728 while (!list_empty(&probe_list)) {
729 tp = list_entry(probe_list.next, struct trace_probe, list);
730 unregister_trace_probe(tp);
731 free_trace_probe(tp);
733 mutex_unlock(&probe_lock);
737 /* Probes listing interfaces */
738 static void *probes_seq_start(struct seq_file *m, loff_t *pos)
740 mutex_lock(&probe_lock);
741 return seq_list_start(&probe_list, *pos);
744 static void *probes_seq_next(struct seq_file *m, void *v, loff_t *pos)
746 return seq_list_next(v, &probe_list, pos);
749 static void probes_seq_stop(struct seq_file *m, void *v)
751 mutex_unlock(&probe_lock);
754 static int probes_seq_show(struct seq_file *m, void *v)
756 struct trace_probe *tp = v;
758 char buf[MAX_ARGSTR_LEN + 1];
760 seq_printf(m, "%c", probe_is_return(tp) ? 'r' : 'p');
761 seq_printf(m, ":%s", tp->call.name);
764 seq_printf(m, " %s+%u", probe_symbol(tp), tp->rp.kp.offset);
766 seq_printf(m, " 0x%p", tp->rp.kp.addr);
768 for (i = 0; i < tp->nr_args; i++) {
769 ret = probe_arg_string(buf, MAX_ARGSTR_LEN, &tp->args[i].fetch);
771 pr_warning("Argument%d decoding error(%d).\n", i, ret);
774 seq_printf(m, " %s=%s", tp->args[i].name, buf);
780 static const struct seq_operations probes_seq_op = {
781 .start = probes_seq_start,
782 .next = probes_seq_next,
783 .stop = probes_seq_stop,
784 .show = probes_seq_show
787 static int probes_open(struct inode *inode, struct file *file)
789 if ((file->f_mode & FMODE_WRITE) &&
790 (file->f_flags & O_TRUNC))
791 cleanup_all_probes();
793 return seq_open(file, &probes_seq_op);
796 static int command_trace_probe(const char *buf)
799 int argc = 0, ret = 0;
801 argv = argv_split(GFP_KERNEL, buf, &argc);
806 ret = create_trace_probe(argc, argv);
812 #define WRITE_BUFSIZE 128
814 static ssize_t probes_write(struct file *file, const char __user *buffer,
815 size_t count, loff_t *ppos)
822 kbuf = kmalloc(WRITE_BUFSIZE, GFP_KERNEL);
827 while (done < count) {
829 if (size >= WRITE_BUFSIZE)
830 size = WRITE_BUFSIZE - 1;
831 if (copy_from_user(kbuf, buffer + done, size)) {
836 tmp = strchr(kbuf, '\n');
839 size = tmp - kbuf + 1;
840 } else if (done + size < count) {
841 pr_warning("Line length is too long: "
842 "Should be less than %d.", WRITE_BUFSIZE);
847 /* Remove comments */
848 tmp = strchr(kbuf, '#');
852 ret = command_trace_probe(kbuf);
862 static const struct file_operations kprobe_events_ops = {
863 .owner = THIS_MODULE,
867 .release = seq_release,
868 .write = probes_write,
871 /* Probes profiling interfaces */
872 static int probes_profile_seq_show(struct seq_file *m, void *v)
874 struct trace_probe *tp = v;
876 seq_printf(m, " %-44s %15lu %15lu\n", tp->call.name, tp->nhit,
882 static const struct seq_operations profile_seq_op = {
883 .start = probes_seq_start,
884 .next = probes_seq_next,
885 .stop = probes_seq_stop,
886 .show = probes_profile_seq_show
889 static int profile_open(struct inode *inode, struct file *file)
891 return seq_open(file, &profile_seq_op);
894 static const struct file_operations kprobe_profile_ops = {
895 .owner = THIS_MODULE,
896 .open = profile_open,
899 .release = seq_release,
903 static __kprobes int kprobe_trace_func(struct kprobe *kp, struct pt_regs *regs)
905 struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
906 struct kprobe_trace_entry *entry;
907 struct ring_buffer_event *event;
908 struct ring_buffer *buffer;
910 unsigned long irq_flags;
911 struct ftrace_event_call *call = &tp->call;
915 local_save_flags(irq_flags);
916 pc = preempt_count();
918 size = SIZEOF_KPROBE_TRACE_ENTRY(tp->nr_args);
920 event = trace_current_buffer_lock_reserve(&buffer, call->id, size,
925 entry = ring_buffer_event_data(event);
926 entry->nargs = tp->nr_args;
927 entry->ip = (unsigned long)kp->addr;
928 for (i = 0; i < tp->nr_args; i++)
929 entry->args[i] = call_fetch(&tp->args[i].fetch, regs);
931 if (!filter_current_check_discard(buffer, call, entry, event))
932 trace_nowake_buffer_unlock_commit(buffer, event, irq_flags, pc);
936 /* Kretprobe handler */
937 static __kprobes int kretprobe_trace_func(struct kretprobe_instance *ri,
938 struct pt_regs *regs)
940 struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
941 struct kretprobe_trace_entry *entry;
942 struct ring_buffer_event *event;
943 struct ring_buffer *buffer;
945 unsigned long irq_flags;
946 struct ftrace_event_call *call = &tp->call;
948 local_save_flags(irq_flags);
949 pc = preempt_count();
951 size = SIZEOF_KRETPROBE_TRACE_ENTRY(tp->nr_args);
953 event = trace_current_buffer_lock_reserve(&buffer, call->id, size,
958 entry = ring_buffer_event_data(event);
959 entry->nargs = tp->nr_args;
960 entry->func = (unsigned long)tp->rp.kp.addr;
961 entry->ret_ip = (unsigned long)ri->ret_addr;
962 for (i = 0; i < tp->nr_args; i++)
963 entry->args[i] = call_fetch(&tp->args[i].fetch, regs);
965 if (!filter_current_check_discard(buffer, call, entry, event))
966 trace_nowake_buffer_unlock_commit(buffer, event, irq_flags, pc);
971 /* Event entry printers */
973 print_kprobe_event(struct trace_iterator *iter, int flags)
975 struct kprobe_trace_entry *field;
976 struct trace_seq *s = &iter->seq;
977 struct trace_event *event;
978 struct trace_probe *tp;
981 field = (struct kprobe_trace_entry *)iter->ent;
982 event = ftrace_find_event(field->ent.type);
983 tp = container_of(event, struct trace_probe, event);
985 if (!trace_seq_printf(s, "%s: (", tp->call.name))
988 if (!seq_print_ip_sym(s, field->ip, flags | TRACE_ITER_SYM_OFFSET))
991 if (!trace_seq_puts(s, ")"))
994 for (i = 0; i < field->nargs; i++)
995 if (!trace_seq_printf(s, " %s=%lx",
996 tp->args[i].name, field->args[i]))
999 if (!trace_seq_puts(s, "\n"))
1002 return TRACE_TYPE_HANDLED;
1004 return TRACE_TYPE_PARTIAL_LINE;
1008 print_kretprobe_event(struct trace_iterator *iter, int flags)
1010 struct kretprobe_trace_entry *field;
1011 struct trace_seq *s = &iter->seq;
1012 struct trace_event *event;
1013 struct trace_probe *tp;
1016 field = (struct kretprobe_trace_entry *)iter->ent;
1017 event = ftrace_find_event(field->ent.type);
1018 tp = container_of(event, struct trace_probe, event);
1020 if (!trace_seq_printf(s, "%s: (", tp->call.name))
1023 if (!seq_print_ip_sym(s, field->ret_ip, flags | TRACE_ITER_SYM_OFFSET))
1026 if (!trace_seq_puts(s, " <- "))
1029 if (!seq_print_ip_sym(s, field->func, flags & ~TRACE_ITER_SYM_OFFSET))
1032 if (!trace_seq_puts(s, ")"))
1035 for (i = 0; i < field->nargs; i++)
1036 if (!trace_seq_printf(s, " %s=%lx",
1037 tp->args[i].name, field->args[i]))
1040 if (!trace_seq_puts(s, "\n"))
1043 return TRACE_TYPE_HANDLED;
1045 return TRACE_TYPE_PARTIAL_LINE;
1048 static int probe_event_enable(struct ftrace_event_call *call)
1050 struct trace_probe *tp = (struct trace_probe *)call->data;
1052 tp->flags |= TP_FLAG_TRACE;
1053 if (probe_is_return(tp))
1054 return enable_kretprobe(&tp->rp);
1056 return enable_kprobe(&tp->rp.kp);
1059 static void probe_event_disable(struct ftrace_event_call *call)
1061 struct trace_probe *tp = (struct trace_probe *)call->data;
1063 tp->flags &= ~TP_FLAG_TRACE;
1064 if (!(tp->flags & (TP_FLAG_TRACE | TP_FLAG_PROFILE))) {
1065 if (probe_is_return(tp))
1066 disable_kretprobe(&tp->rp);
1068 disable_kprobe(&tp->rp.kp);
1072 static int probe_event_raw_init(struct ftrace_event_call *event_call)
1074 INIT_LIST_HEAD(&event_call->fields);
1080 #define DEFINE_FIELD(type, item, name, is_signed) \
1082 ret = trace_define_field(event_call, #type, name, \
1083 offsetof(typeof(field), item), \
1084 sizeof(field.item), is_signed, \
1090 static int kprobe_event_define_fields(struct ftrace_event_call *event_call)
1093 struct kprobe_trace_entry field;
1094 struct trace_probe *tp = (struct trace_probe *)event_call->data;
1096 ret = trace_define_common_fields(event_call);
1100 DEFINE_FIELD(unsigned long, ip, FIELD_STRING_IP, 0);
1101 DEFINE_FIELD(int, nargs, FIELD_STRING_NARGS, 1);
1102 /* Set argument names as fields */
1103 for (i = 0; i < tp->nr_args; i++)
1104 DEFINE_FIELD(unsigned long, args[i], tp->args[i].name, 0);
1108 static int kretprobe_event_define_fields(struct ftrace_event_call *event_call)
1111 struct kretprobe_trace_entry field;
1112 struct trace_probe *tp = (struct trace_probe *)event_call->data;
1114 ret = trace_define_common_fields(event_call);
1118 DEFINE_FIELD(unsigned long, func, FIELD_STRING_FUNC, 0);
1119 DEFINE_FIELD(unsigned long, ret_ip, FIELD_STRING_RETIP, 0);
1120 DEFINE_FIELD(int, nargs, FIELD_STRING_NARGS, 1);
1121 /* Set argument names as fields */
1122 for (i = 0; i < tp->nr_args; i++)
1123 DEFINE_FIELD(unsigned long, args[i], tp->args[i].name, 0);
1127 static int __probe_event_show_format(struct trace_seq *s,
1128 struct trace_probe *tp, const char *fmt,
1134 if (!trace_seq_printf(s, "\nprint fmt: \"%s", fmt))
1137 for (i = 0; i < tp->nr_args; i++)
1138 if (!trace_seq_printf(s, " %s=%%lx", tp->args[i].name))
1141 if (!trace_seq_printf(s, "\", %s", arg))
1144 for (i = 0; i < tp->nr_args; i++)
1145 if (!trace_seq_printf(s, ", REC->%s", tp->args[i].name))
1148 return trace_seq_puts(s, "\n");
1152 #define SHOW_FIELD(type, item, name) \
1154 ret = trace_seq_printf(s, "\tfield: " #type " %s;\t" \
1155 "offset:%u;\tsize:%u;\n", name, \
1156 (unsigned int)offsetof(typeof(field), item),\
1157 (unsigned int)sizeof(type)); \
1162 static int kprobe_event_show_format(struct ftrace_event_call *call,
1163 struct trace_seq *s)
1165 struct kprobe_trace_entry field __attribute__((unused));
1167 struct trace_probe *tp = (struct trace_probe *)call->data;
1169 SHOW_FIELD(unsigned long, ip, FIELD_STRING_IP);
1170 SHOW_FIELD(int, nargs, FIELD_STRING_NARGS);
1173 for (i = 0; i < tp->nr_args; i++)
1174 SHOW_FIELD(unsigned long, args[i], tp->args[i].name);
1175 trace_seq_puts(s, "\n");
1177 return __probe_event_show_format(s, tp, "(%lx)",
1178 "REC->" FIELD_STRING_IP);
1181 static int kretprobe_event_show_format(struct ftrace_event_call *call,
1182 struct trace_seq *s)
1184 struct kretprobe_trace_entry field __attribute__((unused));
1186 struct trace_probe *tp = (struct trace_probe *)call->data;
1188 SHOW_FIELD(unsigned long, func, FIELD_STRING_FUNC);
1189 SHOW_FIELD(unsigned long, ret_ip, FIELD_STRING_RETIP);
1190 SHOW_FIELD(int, nargs, FIELD_STRING_NARGS);
1193 for (i = 0; i < tp->nr_args; i++)
1194 SHOW_FIELD(unsigned long, args[i], tp->args[i].name);
1195 trace_seq_puts(s, "\n");
1197 return __probe_event_show_format(s, tp, "(%lx <- %lx)",
1198 "REC->" FIELD_STRING_FUNC
1199 ", REC->" FIELD_STRING_RETIP);
1202 #ifdef CONFIG_EVENT_PROFILE
1204 /* Kprobe profile handler */
1205 static __kprobes int kprobe_profile_func(struct kprobe *kp,
1206 struct pt_regs *regs)
1208 struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
1209 struct ftrace_event_call *call = &tp->call;
1210 struct kprobe_trace_entry *entry;
1211 struct trace_entry *ent;
1212 int size, __size, i, pc, __cpu;
1213 unsigned long irq_flags;
1218 pc = preempt_count();
1219 __size = SIZEOF_KPROBE_TRACE_ENTRY(tp->nr_args);
1220 size = ALIGN(__size + sizeof(u32), sizeof(u64));
1221 size -= sizeof(u32);
1222 if (WARN_ONCE(size > FTRACE_MAX_PROFILE_SIZE,
1223 "profile buffer not large enough"))
1227 * Protect the non nmi buffer
1228 * This also protects the rcu read side
1230 local_irq_save(irq_flags);
1232 if (perf_swevent_get_recursion_context(&recursion))
1235 __cpu = smp_processor_id();
1238 trace_buf = rcu_dereference(perf_trace_buf_nmi);
1240 trace_buf = rcu_dereference(perf_trace_buf);
1245 raw_data = per_cpu_ptr(trace_buf, __cpu);
1247 /* Zero dead bytes from alignment to avoid buffer leak to userspace */
1248 *(u64 *)(&raw_data[size - sizeof(u64)]) = 0ULL;
1249 entry = (struct kprobe_trace_entry *)raw_data;
1252 tracing_generic_entry_update(ent, irq_flags, pc);
1253 ent->type = call->id;
1254 entry->nargs = tp->nr_args;
1255 entry->ip = (unsigned long)kp->addr;
1256 for (i = 0; i < tp->nr_args; i++)
1257 entry->args[i] = call_fetch(&tp->args[i].fetch, regs);
1258 perf_tp_event(call->id, entry->ip, 1, entry, size);
1261 perf_swevent_put_recursion_context(recursion);
1263 local_irq_restore(irq_flags);
1268 /* Kretprobe profile handler */
1269 static __kprobes int kretprobe_profile_func(struct kretprobe_instance *ri,
1270 struct pt_regs *regs)
1272 struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
1273 struct ftrace_event_call *call = &tp->call;
1274 struct kretprobe_trace_entry *entry;
1275 struct trace_entry *ent;
1276 int size, __size, i, pc, __cpu;
1277 unsigned long irq_flags;
1282 pc = preempt_count();
1283 __size = SIZEOF_KRETPROBE_TRACE_ENTRY(tp->nr_args);
1284 size = ALIGN(__size + sizeof(u32), sizeof(u64));
1285 size -= sizeof(u32);
1286 if (WARN_ONCE(size > FTRACE_MAX_PROFILE_SIZE,
1287 "profile buffer not large enough"))
1291 * Protect the non nmi buffer
1292 * This also protects the rcu read side
1294 local_irq_save(irq_flags);
1296 if (perf_swevent_get_recursion_context(&recursion))
1299 __cpu = smp_processor_id();
1302 trace_buf = rcu_dereference(perf_trace_buf_nmi);
1304 trace_buf = rcu_dereference(perf_trace_buf);
1309 raw_data = per_cpu_ptr(trace_buf, __cpu);
1311 /* Zero dead bytes from alignment to avoid buffer leak to userspace */
1312 *(u64 *)(&raw_data[size - sizeof(u64)]) = 0ULL;
1313 entry = (struct kretprobe_trace_entry *)raw_data;
1316 tracing_generic_entry_update(ent, irq_flags, pc);
1317 ent->type = call->id;
1318 entry->nargs = tp->nr_args;
1319 entry->func = (unsigned long)tp->rp.kp.addr;
1320 entry->ret_ip = (unsigned long)ri->ret_addr;
1321 for (i = 0; i < tp->nr_args; i++)
1322 entry->args[i] = call_fetch(&tp->args[i].fetch, regs);
1323 perf_tp_event(call->id, entry->ret_ip, 1, entry, size);
1326 perf_swevent_put_recursion_context(recursion);
1328 local_irq_restore(irq_flags);
1333 static int probe_profile_enable(struct ftrace_event_call *call)
1335 struct trace_probe *tp = (struct trace_probe *)call->data;
1337 tp->flags |= TP_FLAG_PROFILE;
1339 if (probe_is_return(tp))
1340 return enable_kretprobe(&tp->rp);
1342 return enable_kprobe(&tp->rp.kp);
1345 static void probe_profile_disable(struct ftrace_event_call *call)
1347 struct trace_probe *tp = (struct trace_probe *)call->data;
1349 tp->flags &= ~TP_FLAG_PROFILE;
1351 if (!(tp->flags & TP_FLAG_TRACE)) {
1352 if (probe_is_return(tp))
1353 disable_kretprobe(&tp->rp);
1355 disable_kprobe(&tp->rp.kp);
1358 #endif /* CONFIG_EVENT_PROFILE */
1362 int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs)
1364 struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
1366 if (tp->flags & TP_FLAG_TRACE)
1367 kprobe_trace_func(kp, regs);
1368 #ifdef CONFIG_EVENT_PROFILE
1369 if (tp->flags & TP_FLAG_PROFILE)
1370 kprobe_profile_func(kp, regs);
1371 #endif /* CONFIG_EVENT_PROFILE */
1372 return 0; /* We don't tweek kernel, so just return 0 */
1376 int kretprobe_dispatcher(struct kretprobe_instance *ri, struct pt_regs *regs)
1378 struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
1380 if (tp->flags & TP_FLAG_TRACE)
1381 kretprobe_trace_func(ri, regs);
1382 #ifdef CONFIG_EVENT_PROFILE
1383 if (tp->flags & TP_FLAG_PROFILE)
1384 kretprobe_profile_func(ri, regs);
1385 #endif /* CONFIG_EVENT_PROFILE */
1386 return 0; /* We don't tweek kernel, so just return 0 */
1389 static int register_probe_event(struct trace_probe *tp)
1391 struct ftrace_event_call *call = &tp->call;
1394 /* Initialize ftrace_event_call */
1395 if (probe_is_return(tp)) {
1396 tp->event.trace = print_kretprobe_event;
1397 call->raw_init = probe_event_raw_init;
1398 call->show_format = kretprobe_event_show_format;
1399 call->define_fields = kretprobe_event_define_fields;
1401 tp->event.trace = print_kprobe_event;
1402 call->raw_init = probe_event_raw_init;
1403 call->show_format = kprobe_event_show_format;
1404 call->define_fields = kprobe_event_define_fields;
1406 call->event = &tp->event;
1407 call->id = register_ftrace_event(&tp->event);
1411 call->regfunc = probe_event_enable;
1412 call->unregfunc = probe_event_disable;
1414 #ifdef CONFIG_EVENT_PROFILE
1415 atomic_set(&call->profile_count, -1);
1416 call->profile_enable = probe_profile_enable;
1417 call->profile_disable = probe_profile_disable;
1420 ret = trace_add_event_call(call);
1422 pr_info("Failed to register kprobe event: %s\n", call->name);
1423 unregister_ftrace_event(&tp->event);
1428 static void unregister_probe_event(struct trace_probe *tp)
1430 /* tp->event is unregistered in trace_remove_event_call() */
1431 trace_remove_event_call(&tp->call);
1434 /* Make a debugfs interface for controling probe points */
1435 static __init int init_kprobe_trace(void)
1437 struct dentry *d_tracer;
1438 struct dentry *entry;
1440 d_tracer = tracing_init_dentry();
1444 entry = debugfs_create_file("kprobe_events", 0644, d_tracer,
1445 NULL, &kprobe_events_ops);
1447 /* Event list interface */
1449 pr_warning("Could not create debugfs "
1450 "'kprobe_events' entry\n");
1452 /* Profile interface */
1453 entry = debugfs_create_file("kprobe_profile", 0444, d_tracer,
1454 NULL, &kprobe_profile_ops);
1457 pr_warning("Could not create debugfs "
1458 "'kprobe_profile' entry\n");
1461 fs_initcall(init_kprobe_trace);
1464 #ifdef CONFIG_FTRACE_STARTUP_TEST
1466 static int kprobe_trace_selftest_target(int a1, int a2, int a3,
1467 int a4, int a5, int a6)
1469 return a1 + a2 + a3 + a4 + a5 + a6;
1472 static __init int kprobe_trace_self_tests_init(void)
1475 int (*target)(int, int, int, int, int, int);
1477 target = kprobe_trace_selftest_target;
1479 pr_info("Testing kprobe tracing: ");
1481 ret = command_trace_probe("p:testprobe kprobe_trace_selftest_target "
1482 "$arg1 $arg2 $arg3 $arg4 $stack $stack0");
1483 if (WARN_ON_ONCE(ret))
1484 pr_warning("error enabling function entry\n");
1486 ret = command_trace_probe("r:testprobe2 kprobe_trace_selftest_target "
1488 if (WARN_ON_ONCE(ret))
1489 pr_warning("error enabling function return\n");
1491 ret = target(1, 2, 3, 4, 5, 6);
1493 cleanup_all_probes();
1499 late_initcall(kprobe_trace_self_tests_init);