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_retvalue(struct pt_regs *regs,
97 return regs_return_value(regs);
100 static __kprobes unsigned long fetch_stack_address(struct pt_regs *regs,
103 return kernel_stack_pointer(regs);
106 /* Memory fetching by symbol */
107 struct symbol_cache {
113 static unsigned long update_symbol_cache(struct symbol_cache *sc)
115 sc->addr = (unsigned long)kallsyms_lookup_name(sc->symbol);
117 sc->addr += sc->offset;
121 static void free_symbol_cache(struct symbol_cache *sc)
127 static struct symbol_cache *alloc_symbol_cache(const char *sym, long offset)
129 struct symbol_cache *sc;
131 if (!sym || strlen(sym) == 0)
133 sc = kzalloc(sizeof(struct symbol_cache), GFP_KERNEL);
137 sc->symbol = kstrdup(sym, GFP_KERNEL);
144 update_symbol_cache(sc);
148 static __kprobes unsigned long fetch_symbol(struct pt_regs *regs, void *data)
150 struct symbol_cache *sc = data;
153 return fetch_memory(regs, (void *)sc->addr);
158 /* Special indirect memory access interface */
159 struct indirect_fetch_data {
160 struct fetch_func orig;
164 static __kprobes unsigned long fetch_indirect(struct pt_regs *regs, void *data)
166 struct indirect_fetch_data *ind = data;
169 addr = call_fetch(&ind->orig, regs);
172 return fetch_memory(regs, (void *)addr);
177 static __kprobes void free_indirect_fetch_data(struct indirect_fetch_data *data)
179 if (data->orig.func == fetch_indirect)
180 free_indirect_fetch_data(data->orig.data);
181 else if (data->orig.func == fetch_symbol)
182 free_symbol_cache(data->orig.data);
187 * Kprobe event core functions
191 struct fetch_func fetch;
195 /* Flags for trace_probe */
196 #define TP_FLAG_TRACE 1
197 #define TP_FLAG_PROFILE 2
200 struct list_head list;
201 struct kretprobe rp; /* Use rp.kp for kprobe use */
203 unsigned int flags; /* For TP_FLAG_* */
204 const char *symbol; /* symbol name */
205 struct ftrace_event_class class;
206 struct ftrace_event_call call;
207 struct trace_event event;
208 unsigned int nr_args;
209 struct probe_arg args[];
212 #define SIZEOF_TRACE_PROBE(n) \
213 (offsetof(struct trace_probe, args) + \
214 (sizeof(struct probe_arg) * (n)))
216 static __kprobes int probe_is_return(struct trace_probe *tp)
218 return tp->rp.handler != NULL;
221 static __kprobes const char *probe_symbol(struct trace_probe *tp)
223 return tp->symbol ? tp->symbol : "unknown";
226 static int probe_arg_string(char *buf, size_t n, struct fetch_func *ff)
230 if (ff->func == fetch_register) {
232 name = regs_query_register_name((unsigned int)((long)ff->data));
233 ret = snprintf(buf, n, "%%%s", name);
234 } else if (ff->func == fetch_stack)
235 ret = snprintf(buf, n, "$stack%lu", (unsigned long)ff->data);
236 else if (ff->func == fetch_memory)
237 ret = snprintf(buf, n, "@0x%p", ff->data);
238 else if (ff->func == fetch_symbol) {
239 struct symbol_cache *sc = ff->data;
241 ret = snprintf(buf, n, "@%s%+ld", sc->symbol,
244 ret = snprintf(buf, n, "@%s", sc->symbol);
245 } else if (ff->func == fetch_retvalue)
246 ret = snprintf(buf, n, "$retval");
247 else if (ff->func == fetch_stack_address)
248 ret = snprintf(buf, n, "$stack");
249 else if (ff->func == fetch_indirect) {
250 struct indirect_fetch_data *id = ff->data;
252 ret = snprintf(buf, n, "%+ld(", id->offset);
256 ret = probe_arg_string(buf + l, n - l, &id->orig);
260 ret = snprintf(buf + l, n - l, ")");
269 static int register_probe_event(struct trace_probe *tp);
270 static void unregister_probe_event(struct trace_probe *tp);
272 static DEFINE_MUTEX(probe_lock);
273 static LIST_HEAD(probe_list);
275 static int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs);
276 static int kretprobe_dispatcher(struct kretprobe_instance *ri,
277 struct pt_regs *regs);
279 /* Check the name is good for event/group */
280 static int check_event_name(const char *name)
282 if (!isalpha(*name) && *name != '_')
284 while (*++name != '\0') {
285 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
292 * Allocate new trace_probe and initialize it (including kprobes).
294 static struct trace_probe *alloc_trace_probe(const char *group,
299 int nargs, int is_return)
301 struct trace_probe *tp;
304 tp = kzalloc(SIZEOF_TRACE_PROBE(nargs), GFP_KERNEL);
309 tp->symbol = kstrdup(symbol, GFP_KERNEL);
312 tp->rp.kp.symbol_name = tp->symbol;
313 tp->rp.kp.offset = offs;
315 tp->rp.kp.addr = addr;
318 tp->rp.handler = kretprobe_dispatcher;
320 tp->rp.kp.pre_handler = kprobe_dispatcher;
322 if (!event || !check_event_name(event)) {
327 tp->call.class = &tp->class;
328 tp->call.name = kstrdup(event, GFP_KERNEL);
332 if (!group || !check_event_name(group)) {
337 tp->class.system = kstrdup(group, GFP_KERNEL);
338 if (!tp->class.system)
341 INIT_LIST_HEAD(&tp->list);
344 kfree(tp->call.name);
350 static void free_probe_arg(struct probe_arg *arg)
352 if (arg->fetch.func == fetch_symbol)
353 free_symbol_cache(arg->fetch.data);
354 else if (arg->fetch.func == fetch_indirect)
355 free_indirect_fetch_data(arg->fetch.data);
359 static void free_trace_probe(struct trace_probe *tp)
363 for (i = 0; i < tp->nr_args; i++)
364 free_probe_arg(&tp->args[i]);
366 kfree(tp->call.class->system);
367 kfree(tp->call.name);
372 static struct trace_probe *find_probe_event(const char *event,
375 struct trace_probe *tp;
377 list_for_each_entry(tp, &probe_list, list)
378 if (strcmp(tp->call.name, event) == 0 &&
379 strcmp(tp->call.class->system, group) == 0)
384 /* Unregister a trace_probe and probe_event: call with locking probe_lock */
385 static void unregister_trace_probe(struct trace_probe *tp)
387 if (probe_is_return(tp))
388 unregister_kretprobe(&tp->rp);
390 unregister_kprobe(&tp->rp.kp);
392 unregister_probe_event(tp);
395 /* Register a trace_probe and probe_event */
396 static int register_trace_probe(struct trace_probe *tp)
398 struct trace_probe *old_tp;
401 mutex_lock(&probe_lock);
403 /* register as an event */
404 old_tp = find_probe_event(tp->call.name, tp->call.class->system);
406 /* delete old event */
407 unregister_trace_probe(old_tp);
408 free_trace_probe(old_tp);
410 ret = register_probe_event(tp);
412 pr_warning("Faild to register probe event(%d)\n", ret);
416 tp->rp.kp.flags |= KPROBE_FLAG_DISABLED;
417 if (probe_is_return(tp))
418 ret = register_kretprobe(&tp->rp);
420 ret = register_kprobe(&tp->rp.kp);
423 pr_warning("Could not insert probe(%d)\n", ret);
424 if (ret == -EILSEQ) {
425 pr_warning("Probing address(0x%p) is not an "
426 "instruction boundary.\n",
430 unregister_probe_event(tp);
432 list_add_tail(&tp->list, &probe_list);
434 mutex_unlock(&probe_lock);
438 /* Split symbol and offset. */
439 static int split_symbol_offset(char *symbol, unsigned long *offset)
447 tmp = strchr(symbol, '+');
449 /* skip sign because strict_strtol doesn't accept '+' */
450 ret = strict_strtoul(tmp + 1, 0, offset);
459 #define PARAM_MAX_ARGS 16
460 #define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long))
462 static int parse_probe_vars(char *arg, struct fetch_func *ff, int is_return)
467 if (strcmp(arg, "retval") == 0) {
469 ff->func = fetch_retvalue;
473 } else if (strncmp(arg, "stack", 5) == 0) {
474 if (arg[5] == '\0') {
475 ff->func = fetch_stack_address;
477 } else if (isdigit(arg[5])) {
478 ret = strict_strtoul(arg + 5, 10, ¶m);
479 if (ret || param > PARAM_MAX_STACK)
482 ff->func = fetch_stack;
483 ff->data = (void *)param;
492 /* Recursive argument parser */
493 static int __parse_probe_arg(char *arg, struct fetch_func *ff, int is_return)
502 ret = parse_probe_vars(arg + 1, ff, is_return);
504 case '%': /* named register */
505 ret = regs_query_register_offset(arg + 1);
507 ff->func = fetch_register;
508 ff->data = (void *)(unsigned long)ret;
512 case '@': /* memory or symbol */
513 if (isdigit(arg[1])) {
514 ret = strict_strtoul(arg + 1, 0, ¶m);
517 ff->func = fetch_memory;
518 ff->data = (void *)param;
520 ret = split_symbol_offset(arg + 1, &offset);
523 ff->data = alloc_symbol_cache(arg + 1, offset);
525 ff->func = fetch_symbol;
530 case '+': /* indirect memory */
532 tmp = strchr(arg, '(');
538 ret = strict_strtol(arg + 1, 0, &offset);
544 tmp = strrchr(arg, ')');
546 struct indirect_fetch_data *id;
548 id = kzalloc(sizeof(struct indirect_fetch_data),
553 ret = __parse_probe_arg(arg, &id->orig, is_return);
557 ff->func = fetch_indirect;
558 ff->data = (void *)id;
564 /* TODO: support custom handler */
570 /* String length checking wrapper */
571 static int parse_probe_arg(char *arg, struct fetch_func *ff, int is_return)
573 if (strlen(arg) > MAX_ARGSTR_LEN) {
574 pr_info("Argument is too long.: %s\n", arg);
577 return __parse_probe_arg(arg, ff, is_return);
580 /* Return 1 if name is reserved or already used by another argument */
581 static int conflict_field_name(const char *name,
582 struct probe_arg *args, int narg)
585 for (i = 0; i < ARRAY_SIZE(reserved_field_names); i++)
586 if (strcmp(reserved_field_names[i], name) == 0)
588 for (i = 0; i < narg; i++)
589 if (strcmp(args[i].name, name) == 0)
594 static int create_trace_probe(int argc, char **argv)
598 * - Add kprobe: p[:[GRP/]EVENT] KSYM[+OFFS]|KADDR [FETCHARGS]
599 * - Add kretprobe: r[:[GRP/]EVENT] KSYM[+0] [FETCHARGS]
601 * $retval : fetch return value
602 * $stack : fetch stack address
603 * $stackN : fetch Nth of stack (N:0-)
604 * @ADDR : fetch memory at ADDR (ADDR should be in kernel)
605 * @SYM[+|-offs] : fetch memory at SYM +|- offs (SYM is a data symbol)
606 * %REG : fetch register REG
607 * Indirect memory fetch:
608 * +|-offs(ARG) : fetch memory at ARG +|- offs address.
609 * Alias name of args:
610 * NAME=FETCHARG : set NAME as alias of FETCHARG.
612 struct trace_probe *tp;
614 int is_return = 0, is_delete = 0;
615 char *symbol = NULL, *event = NULL, *arg = NULL, *group = NULL;
616 unsigned long offset = 0;
618 char buf[MAX_EVENT_NAME_LEN];
620 /* argc must be >= 1 */
621 if (argv[0][0] == 'p')
623 else if (argv[0][0] == 'r')
625 else if (argv[0][0] == '-')
628 pr_info("Probe definition must be started with 'p', 'r' or"
633 if (argv[0][1] == ':') {
635 if (strchr(event, '/')) {
637 event = strchr(group, '/') + 1;
639 if (strlen(group) == 0) {
640 pr_info("Group name is not specified\n");
644 if (strlen(event) == 0) {
645 pr_info("Event name is not specified\n");
650 group = KPROBE_EVENT_SYSTEM;
654 pr_info("Delete command needs an event name.\n");
657 tp = find_probe_event(event, group);
659 pr_info("Event %s/%s doesn't exist.\n", group, event);
662 /* delete an event */
663 unregister_trace_probe(tp);
664 free_trace_probe(tp);
669 pr_info("Probe point is not specified.\n");
672 if (isdigit(argv[1][0])) {
674 pr_info("Return probe point must be a symbol.\n");
677 /* an address specified */
678 ret = strict_strtoul(&argv[1][0], 0, (unsigned long *)&addr);
680 pr_info("Failed to parse address.\n");
684 /* a symbol specified */
686 /* TODO: support .init module functions */
687 ret = split_symbol_offset(symbol, &offset);
689 pr_info("Failed to parse symbol.\n");
692 if (offset && is_return) {
693 pr_info("Return probe must be used without offset.\n");
697 argc -= 2; argv += 2;
701 /* Make a new event name */
703 snprintf(buf, MAX_EVENT_NAME_LEN, "%c_%s_%ld",
704 is_return ? 'r' : 'p', symbol, offset);
706 snprintf(buf, MAX_EVENT_NAME_LEN, "%c_0x%p",
707 is_return ? 'r' : 'p', addr);
710 tp = alloc_trace_probe(group, event, addr, symbol, offset, argc,
713 pr_info("Failed to allocate trace_probe.(%d)\n",
718 /* parse arguments */
720 for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
721 /* Parse argument name */
722 arg = strchr(argv[i], '=');
728 if (conflict_field_name(argv[i], tp->args, i)) {
729 pr_info("Argument%d name '%s' conflicts with "
730 "another field.\n", i, argv[i]);
735 tp->args[i].name = kstrdup(argv[i], GFP_KERNEL);
736 if (!tp->args[i].name) {
737 pr_info("Failed to allocate argument%d name '%s'.\n",
743 /* Parse fetch argument */
744 ret = parse_probe_arg(arg, &tp->args[i].fetch, is_return);
746 pr_info("Parse error at argument%d. (%d)\n", i, ret);
747 kfree(tp->args[i].name);
754 ret = register_trace_probe(tp);
760 free_trace_probe(tp);
764 static void cleanup_all_probes(void)
766 struct trace_probe *tp;
768 mutex_lock(&probe_lock);
769 /* TODO: Use batch unregistration */
770 while (!list_empty(&probe_list)) {
771 tp = list_entry(probe_list.next, struct trace_probe, list);
772 unregister_trace_probe(tp);
773 free_trace_probe(tp);
775 mutex_unlock(&probe_lock);
779 /* Probes listing interfaces */
780 static void *probes_seq_start(struct seq_file *m, loff_t *pos)
782 mutex_lock(&probe_lock);
783 return seq_list_start(&probe_list, *pos);
786 static void *probes_seq_next(struct seq_file *m, void *v, loff_t *pos)
788 return seq_list_next(v, &probe_list, pos);
791 static void probes_seq_stop(struct seq_file *m, void *v)
793 mutex_unlock(&probe_lock);
796 static int probes_seq_show(struct seq_file *m, void *v)
798 struct trace_probe *tp = v;
800 char buf[MAX_ARGSTR_LEN + 1];
802 seq_printf(m, "%c", probe_is_return(tp) ? 'r' : 'p');
803 seq_printf(m, ":%s/%s", tp->call.class->system, tp->call.name);
806 seq_printf(m, " 0x%p", tp->rp.kp.addr);
807 else if (tp->rp.kp.offset)
808 seq_printf(m, " %s+%u", probe_symbol(tp), tp->rp.kp.offset);
810 seq_printf(m, " %s", probe_symbol(tp));
812 for (i = 0; i < tp->nr_args; i++) {
813 ret = probe_arg_string(buf, MAX_ARGSTR_LEN, &tp->args[i].fetch);
815 pr_warning("Argument%d decoding error(%d).\n", i, ret);
818 seq_printf(m, " %s=%s", tp->args[i].name, buf);
824 static const struct seq_operations probes_seq_op = {
825 .start = probes_seq_start,
826 .next = probes_seq_next,
827 .stop = probes_seq_stop,
828 .show = probes_seq_show
831 static int probes_open(struct inode *inode, struct file *file)
833 if ((file->f_mode & FMODE_WRITE) &&
834 (file->f_flags & O_TRUNC))
835 cleanup_all_probes();
837 return seq_open(file, &probes_seq_op);
840 static int command_trace_probe(const char *buf)
843 int argc = 0, ret = 0;
845 argv = argv_split(GFP_KERNEL, buf, &argc);
850 ret = create_trace_probe(argc, argv);
856 #define WRITE_BUFSIZE 128
858 static ssize_t probes_write(struct file *file, const char __user *buffer,
859 size_t count, loff_t *ppos)
866 kbuf = kmalloc(WRITE_BUFSIZE, GFP_KERNEL);
871 while (done < count) {
873 if (size >= WRITE_BUFSIZE)
874 size = WRITE_BUFSIZE - 1;
875 if (copy_from_user(kbuf, buffer + done, size)) {
880 tmp = strchr(kbuf, '\n');
883 size = tmp - kbuf + 1;
884 } else if (done + size < count) {
885 pr_warning("Line length is too long: "
886 "Should be less than %d.", WRITE_BUFSIZE);
891 /* Remove comments */
892 tmp = strchr(kbuf, '#');
896 ret = command_trace_probe(kbuf);
906 static const struct file_operations kprobe_events_ops = {
907 .owner = THIS_MODULE,
911 .release = seq_release,
912 .write = probes_write,
915 /* Probes profiling interfaces */
916 static int probes_profile_seq_show(struct seq_file *m, void *v)
918 struct trace_probe *tp = v;
920 seq_printf(m, " %-44s %15lu %15lu\n", tp->call.name, tp->nhit,
926 static const struct seq_operations profile_seq_op = {
927 .start = probes_seq_start,
928 .next = probes_seq_next,
929 .stop = probes_seq_stop,
930 .show = probes_profile_seq_show
933 static int profile_open(struct inode *inode, struct file *file)
935 return seq_open(file, &profile_seq_op);
938 static const struct file_operations kprobe_profile_ops = {
939 .owner = THIS_MODULE,
940 .open = profile_open,
943 .release = seq_release,
947 static __kprobes void kprobe_trace_func(struct kprobe *kp, struct pt_regs *regs)
949 struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
950 struct kprobe_trace_entry *entry;
951 struct ring_buffer_event *event;
952 struct ring_buffer *buffer;
954 unsigned long irq_flags;
955 struct ftrace_event_call *call = &tp->call;
959 local_save_flags(irq_flags);
960 pc = preempt_count();
962 size = SIZEOF_KPROBE_TRACE_ENTRY(tp->nr_args);
964 event = trace_current_buffer_lock_reserve(&buffer, call->id, size,
969 entry = ring_buffer_event_data(event);
970 entry->nargs = tp->nr_args;
971 entry->ip = (unsigned long)kp->addr;
972 for (i = 0; i < tp->nr_args; i++)
973 entry->args[i] = call_fetch(&tp->args[i].fetch, regs);
975 if (!filter_current_check_discard(buffer, call, entry, event))
976 trace_nowake_buffer_unlock_commit(buffer, event, irq_flags, pc);
979 /* Kretprobe handler */
980 static __kprobes void kretprobe_trace_func(struct kretprobe_instance *ri,
981 struct pt_regs *regs)
983 struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
984 struct kretprobe_trace_entry *entry;
985 struct ring_buffer_event *event;
986 struct ring_buffer *buffer;
988 unsigned long irq_flags;
989 struct ftrace_event_call *call = &tp->call;
991 local_save_flags(irq_flags);
992 pc = preempt_count();
994 size = SIZEOF_KRETPROBE_TRACE_ENTRY(tp->nr_args);
996 event = trace_current_buffer_lock_reserve(&buffer, call->id, size,
1001 entry = ring_buffer_event_data(event);
1002 entry->nargs = tp->nr_args;
1003 entry->func = (unsigned long)tp->rp.kp.addr;
1004 entry->ret_ip = (unsigned long)ri->ret_addr;
1005 for (i = 0; i < tp->nr_args; i++)
1006 entry->args[i] = call_fetch(&tp->args[i].fetch, regs);
1008 if (!filter_current_check_discard(buffer, call, entry, event))
1009 trace_nowake_buffer_unlock_commit(buffer, event, irq_flags, pc);
1012 /* Event entry printers */
1014 print_kprobe_event(struct trace_iterator *iter, int flags)
1016 struct kprobe_trace_entry *field;
1017 struct trace_seq *s = &iter->seq;
1018 struct trace_event *event;
1019 struct trace_probe *tp;
1022 field = (struct kprobe_trace_entry *)iter->ent;
1023 event = ftrace_find_event(field->ent.type);
1024 tp = container_of(event, struct trace_probe, event);
1026 if (!trace_seq_printf(s, "%s: (", tp->call.name))
1029 if (!seq_print_ip_sym(s, field->ip, 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;
1049 print_kretprobe_event(struct trace_iterator *iter, int flags)
1051 struct kretprobe_trace_entry *field;
1052 struct trace_seq *s = &iter->seq;
1053 struct trace_event *event;
1054 struct trace_probe *tp;
1057 field = (struct kretprobe_trace_entry *)iter->ent;
1058 event = ftrace_find_event(field->ent.type);
1059 tp = container_of(event, struct trace_probe, event);
1061 if (!trace_seq_printf(s, "%s: (", tp->call.name))
1064 if (!seq_print_ip_sym(s, field->ret_ip, flags | TRACE_ITER_SYM_OFFSET))
1067 if (!trace_seq_puts(s, " <- "))
1070 if (!seq_print_ip_sym(s, field->func, flags & ~TRACE_ITER_SYM_OFFSET))
1073 if (!trace_seq_puts(s, ")"))
1076 for (i = 0; i < field->nargs; i++)
1077 if (!trace_seq_printf(s, " %s=%lx",
1078 tp->args[i].name, field->args[i]))
1081 if (!trace_seq_puts(s, "\n"))
1084 return TRACE_TYPE_HANDLED;
1086 return TRACE_TYPE_PARTIAL_LINE;
1089 static int probe_event_enable(struct ftrace_event_call *call)
1091 struct trace_probe *tp = (struct trace_probe *)call->data;
1093 tp->flags |= TP_FLAG_TRACE;
1094 if (probe_is_return(tp))
1095 return enable_kretprobe(&tp->rp);
1097 return enable_kprobe(&tp->rp.kp);
1100 static void probe_event_disable(struct ftrace_event_call *call)
1102 struct trace_probe *tp = (struct trace_probe *)call->data;
1104 tp->flags &= ~TP_FLAG_TRACE;
1105 if (!(tp->flags & (TP_FLAG_TRACE | TP_FLAG_PROFILE))) {
1106 if (probe_is_return(tp))
1107 disable_kretprobe(&tp->rp);
1109 disable_kprobe(&tp->rp.kp);
1113 static int probe_event_raw_init(struct ftrace_event_call *event_call)
1119 #define DEFINE_FIELD(type, item, name, is_signed) \
1121 ret = trace_define_field(event_call, #type, name, \
1122 offsetof(typeof(field), item), \
1123 sizeof(field.item), is_signed, \
1129 static int kprobe_event_define_fields(struct ftrace_event_call *event_call)
1132 struct kprobe_trace_entry field;
1133 struct trace_probe *tp = (struct trace_probe *)event_call->data;
1135 DEFINE_FIELD(unsigned long, ip, FIELD_STRING_IP, 0);
1136 DEFINE_FIELD(int, nargs, FIELD_STRING_NARGS, 1);
1137 /* Set argument names as fields */
1138 for (i = 0; i < tp->nr_args; i++)
1139 DEFINE_FIELD(unsigned long, args[i], tp->args[i].name, 0);
1143 static int kretprobe_event_define_fields(struct ftrace_event_call *event_call)
1146 struct kretprobe_trace_entry field;
1147 struct trace_probe *tp = (struct trace_probe *)event_call->data;
1149 DEFINE_FIELD(unsigned long, func, FIELD_STRING_FUNC, 0);
1150 DEFINE_FIELD(unsigned long, ret_ip, FIELD_STRING_RETIP, 0);
1151 DEFINE_FIELD(int, nargs, FIELD_STRING_NARGS, 1);
1152 /* Set argument names as fields */
1153 for (i = 0; i < tp->nr_args; i++)
1154 DEFINE_FIELD(unsigned long, args[i], tp->args[i].name, 0);
1158 static int __set_print_fmt(struct trace_probe *tp, char *buf, int len)
1163 const char *fmt, *arg;
1165 if (!probe_is_return(tp)) {
1167 arg = "REC->" FIELD_STRING_IP;
1169 fmt = "(%lx <- %lx)";
1170 arg = "REC->" FIELD_STRING_FUNC ", REC->" FIELD_STRING_RETIP;
1173 /* When len=0, we just calculate the needed length */
1174 #define LEN_OR_ZERO (len ? len - pos : 0)
1176 pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", fmt);
1178 for (i = 0; i < tp->nr_args; i++) {
1179 pos += snprintf(buf + pos, LEN_OR_ZERO, " %s=%%lx",
1183 pos += snprintf(buf + pos, LEN_OR_ZERO, "\", %s", arg);
1185 for (i = 0; i < tp->nr_args; i++) {
1186 pos += snprintf(buf + pos, LEN_OR_ZERO, ", REC->%s",
1192 /* return the length of print_fmt */
1196 static int set_print_fmt(struct trace_probe *tp)
1201 /* First: called with 0 length to calculate the needed length */
1202 len = __set_print_fmt(tp, NULL, 0);
1203 print_fmt = kmalloc(len + 1, GFP_KERNEL);
1207 /* Second: actually write the @print_fmt */
1208 __set_print_fmt(tp, print_fmt, len + 1);
1209 tp->call.print_fmt = print_fmt;
1214 #ifdef CONFIG_PERF_EVENTS
1216 /* Kprobe profile handler */
1217 static __kprobes void kprobe_perf_func(struct kprobe *kp,
1218 struct pt_regs *regs)
1220 struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
1221 struct ftrace_event_call *call = &tp->call;
1222 struct kprobe_trace_entry *entry;
1223 int size, __size, i;
1224 unsigned long irq_flags;
1227 __size = SIZEOF_KPROBE_TRACE_ENTRY(tp->nr_args);
1228 size = ALIGN(__size + sizeof(u32), sizeof(u64));
1229 size -= sizeof(u32);
1230 if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE,
1231 "profile buffer not large enough"))
1234 entry = perf_trace_buf_prepare(size, call->id, &rctx, &irq_flags);
1238 entry->nargs = tp->nr_args;
1239 entry->ip = (unsigned long)kp->addr;
1240 for (i = 0; i < tp->nr_args; i++)
1241 entry->args[i] = call_fetch(&tp->args[i].fetch, regs);
1243 perf_trace_buf_submit(entry, size, rctx, entry->ip, 1, irq_flags, regs);
1246 /* Kretprobe profile handler */
1247 static __kprobes void kretprobe_perf_func(struct kretprobe_instance *ri,
1248 struct pt_regs *regs)
1250 struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
1251 struct ftrace_event_call *call = &tp->call;
1252 struct kretprobe_trace_entry *entry;
1253 int size, __size, i;
1254 unsigned long irq_flags;
1257 __size = SIZEOF_KRETPROBE_TRACE_ENTRY(tp->nr_args);
1258 size = ALIGN(__size + sizeof(u32), sizeof(u64));
1259 size -= sizeof(u32);
1260 if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE,
1261 "profile buffer not large enough"))
1264 entry = perf_trace_buf_prepare(size, call->id, &rctx, &irq_flags);
1268 entry->nargs = tp->nr_args;
1269 entry->func = (unsigned long)tp->rp.kp.addr;
1270 entry->ret_ip = (unsigned long)ri->ret_addr;
1271 for (i = 0; i < tp->nr_args; i++)
1272 entry->args[i] = call_fetch(&tp->args[i].fetch, regs);
1274 perf_trace_buf_submit(entry, size, rctx, entry->ret_ip, 1,
1278 static int probe_perf_enable(struct ftrace_event_call *call)
1280 struct trace_probe *tp = (struct trace_probe *)call->data;
1282 tp->flags |= TP_FLAG_PROFILE;
1284 if (probe_is_return(tp))
1285 return enable_kretprobe(&tp->rp);
1287 return enable_kprobe(&tp->rp.kp);
1290 static void probe_perf_disable(struct ftrace_event_call *call)
1292 struct trace_probe *tp = (struct trace_probe *)call->data;
1294 tp->flags &= ~TP_FLAG_PROFILE;
1296 if (!(tp->flags & TP_FLAG_TRACE)) {
1297 if (probe_is_return(tp))
1298 disable_kretprobe(&tp->rp);
1300 disable_kprobe(&tp->rp.kp);
1303 #endif /* CONFIG_PERF_EVENTS */
1306 int kprobe_register(struct ftrace_event_call *event, enum trace_reg type)
1309 case TRACE_REG_REGISTER:
1310 return probe_event_enable(event);
1311 case TRACE_REG_UNREGISTER:
1312 probe_event_disable(event);
1315 #ifdef CONFIG_PERF_EVENTS
1316 case TRACE_REG_PERF_REGISTER:
1317 return probe_perf_enable(event);
1318 case TRACE_REG_PERF_UNREGISTER:
1319 probe_perf_disable(event);
1327 int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs)
1329 struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
1331 if (tp->flags & TP_FLAG_TRACE)
1332 kprobe_trace_func(kp, regs);
1333 #ifdef CONFIG_PERF_EVENTS
1334 if (tp->flags & TP_FLAG_PROFILE)
1335 kprobe_perf_func(kp, regs);
1337 return 0; /* We don't tweek kernel, so just return 0 */
1341 int kretprobe_dispatcher(struct kretprobe_instance *ri, struct pt_regs *regs)
1343 struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
1345 if (tp->flags & TP_FLAG_TRACE)
1346 kretprobe_trace_func(ri, regs);
1347 #ifdef CONFIG_PERF_EVENTS
1348 if (tp->flags & TP_FLAG_PROFILE)
1349 kretprobe_perf_func(ri, regs);
1351 return 0; /* We don't tweek kernel, so just return 0 */
1354 static int register_probe_event(struct trace_probe *tp)
1356 struct ftrace_event_call *call = &tp->call;
1359 /* Initialize ftrace_event_call */
1360 if (probe_is_return(tp)) {
1361 tp->event.trace = print_kretprobe_event;
1362 INIT_LIST_HEAD(&call->class->fields);
1363 call->class->raw_init = probe_event_raw_init;
1364 call->class->define_fields = kretprobe_event_define_fields;
1366 INIT_LIST_HEAD(&call->class->fields);
1367 tp->event.trace = print_kprobe_event;
1368 call->class->raw_init = probe_event_raw_init;
1369 call->class->define_fields = kprobe_event_define_fields;
1371 if (set_print_fmt(tp) < 0)
1373 call->event = &tp->event;
1374 call->id = register_ftrace_event(&tp->event);
1376 kfree(call->print_fmt);
1380 call->class->reg = kprobe_register;
1382 ret = trace_add_event_call(call);
1384 pr_info("Failed to register kprobe event: %s\n", call->name);
1385 kfree(call->print_fmt);
1386 unregister_ftrace_event(&tp->event);
1391 static void unregister_probe_event(struct trace_probe *tp)
1393 /* tp->event is unregistered in trace_remove_event_call() */
1394 trace_remove_event_call(&tp->call);
1395 kfree(tp->call.print_fmt);
1398 /* Make a debugfs interface for controling probe points */
1399 static __init int init_kprobe_trace(void)
1401 struct dentry *d_tracer;
1402 struct dentry *entry;
1404 d_tracer = tracing_init_dentry();
1408 entry = debugfs_create_file("kprobe_events", 0644, d_tracer,
1409 NULL, &kprobe_events_ops);
1411 /* Event list interface */
1413 pr_warning("Could not create debugfs "
1414 "'kprobe_events' entry\n");
1416 /* Profile interface */
1417 entry = debugfs_create_file("kprobe_profile", 0444, d_tracer,
1418 NULL, &kprobe_profile_ops);
1421 pr_warning("Could not create debugfs "
1422 "'kprobe_profile' entry\n");
1425 fs_initcall(init_kprobe_trace);
1428 #ifdef CONFIG_FTRACE_STARTUP_TEST
1430 static int kprobe_trace_selftest_target(int a1, int a2, int a3,
1431 int a4, int a5, int a6)
1433 return a1 + a2 + a3 + a4 + a5 + a6;
1436 static __init int kprobe_trace_self_tests_init(void)
1439 int (*target)(int, int, int, int, int, int);
1440 struct trace_probe *tp;
1442 target = kprobe_trace_selftest_target;
1444 pr_info("Testing kprobe tracing: ");
1446 ret = command_trace_probe("p:testprobe kprobe_trace_selftest_target "
1447 "$stack $stack0 +0($stack)");
1448 if (WARN_ON_ONCE(ret)) {
1449 pr_warning("error on probing function entry.\n");
1452 /* Enable trace point */
1453 tp = find_probe_event("testprobe", KPROBE_EVENT_SYSTEM);
1454 if (WARN_ON_ONCE(tp == NULL)) {
1455 pr_warning("error on getting new probe.\n");
1458 probe_event_enable(&tp->call);
1461 ret = command_trace_probe("r:testprobe2 kprobe_trace_selftest_target "
1463 if (WARN_ON_ONCE(ret)) {
1464 pr_warning("error on probing function return.\n");
1467 /* Enable trace point */
1468 tp = find_probe_event("testprobe2", KPROBE_EVENT_SYSTEM);
1469 if (WARN_ON_ONCE(tp == NULL)) {
1470 pr_warning("error on getting new probe.\n");
1473 probe_event_enable(&tp->call);
1479 ret = target(1, 2, 3, 4, 5, 6);
1481 ret = command_trace_probe("-:testprobe");
1482 if (WARN_ON_ONCE(ret)) {
1483 pr_warning("error on deleting a probe.\n");
1487 ret = command_trace_probe("-:testprobe2");
1488 if (WARN_ON_ONCE(ret)) {
1489 pr_warning("error on deleting a probe.\n");
1494 cleanup_all_probes();
1496 pr_cont("NG: Some tests are failed. Please check them.\n");
1502 late_initcall(kprobe_trace_self_tests_init);