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 /* 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",
60 /* currently, trace_kprobe only supports X86. */
63 unsigned long (*func)(struct pt_regs *, void *);
67 static __kprobes unsigned long call_fetch(struct fetch_func *f,
70 return f->func(regs, f->data);
74 static __kprobes unsigned long fetch_register(struct pt_regs *regs,
77 return regs_get_register(regs, (unsigned int)((unsigned long)offset));
80 static __kprobes unsigned long fetch_stack(struct pt_regs *regs,
83 return regs_get_kernel_stack_nth(regs,
84 (unsigned int)((unsigned long)num));
87 static __kprobes unsigned long fetch_memory(struct pt_regs *regs, void *addr)
91 if (probe_kernel_address(addr, retval))
96 static __kprobes unsigned long fetch_argument(struct pt_regs *regs, void *num)
98 return regs_get_argument_nth(regs, (unsigned int)((unsigned long)num));
101 static __kprobes unsigned long fetch_retvalue(struct pt_regs *regs,
104 return regs_return_value(regs);
107 static __kprobes unsigned long fetch_stack_address(struct pt_regs *regs,
110 return kernel_stack_pointer(regs);
113 /* Memory fetching by symbol */
114 struct symbol_cache {
120 static unsigned long update_symbol_cache(struct symbol_cache *sc)
122 sc->addr = (unsigned long)kallsyms_lookup_name(sc->symbol);
124 sc->addr += sc->offset;
128 static void free_symbol_cache(struct symbol_cache *sc)
134 static struct symbol_cache *alloc_symbol_cache(const char *sym, long offset)
136 struct symbol_cache *sc;
138 if (!sym || strlen(sym) == 0)
140 sc = kzalloc(sizeof(struct symbol_cache), GFP_KERNEL);
144 sc->symbol = kstrdup(sym, GFP_KERNEL);
151 update_symbol_cache(sc);
155 static __kprobes unsigned long fetch_symbol(struct pt_regs *regs, void *data)
157 struct symbol_cache *sc = data;
160 return fetch_memory(regs, (void *)sc->addr);
165 /* Special indirect memory access interface */
166 struct indirect_fetch_data {
167 struct fetch_func orig;
171 static __kprobes unsigned long fetch_indirect(struct pt_regs *regs, void *data)
173 struct indirect_fetch_data *ind = data;
176 addr = call_fetch(&ind->orig, regs);
179 return fetch_memory(regs, (void *)addr);
184 static __kprobes void free_indirect_fetch_data(struct indirect_fetch_data *data)
186 if (data->orig.func == fetch_indirect)
187 free_indirect_fetch_data(data->orig.data);
188 else if (data->orig.func == fetch_symbol)
189 free_symbol_cache(data->orig.data);
194 * Kprobe tracer core functions
198 struct fetch_func fetch;
202 /* Flags for trace_probe */
203 #define TP_FLAG_TRACE 1
204 #define TP_FLAG_PROFILE 2
207 struct list_head list;
208 struct kretprobe rp; /* Use rp.kp for kprobe use */
210 unsigned int flags; /* For TP_FLAG_* */
211 const char *symbol; /* symbol name */
212 struct ftrace_event_call call;
213 struct trace_event event;
214 unsigned int nr_args;
215 struct probe_arg args[];
218 #define SIZEOF_TRACE_PROBE(n) \
219 (offsetof(struct trace_probe, args) + \
220 (sizeof(struct probe_arg) * (n)))
222 static __kprobes int probe_is_return(struct trace_probe *tp)
224 return tp->rp.handler != NULL;
227 static __kprobes const char *probe_symbol(struct trace_probe *tp)
229 return tp->symbol ? tp->symbol : "unknown";
232 static int probe_arg_string(char *buf, size_t n, struct fetch_func *ff)
236 if (ff->func == fetch_argument)
237 ret = snprintf(buf, n, "$arg%lu", (unsigned long)ff->data);
238 else if (ff->func == fetch_register) {
240 name = regs_query_register_name((unsigned int)((long)ff->data));
241 ret = snprintf(buf, n, "%%%s", name);
242 } else if (ff->func == fetch_stack)
243 ret = snprintf(buf, n, "$stack%lu", (unsigned long)ff->data);
244 else if (ff->func == fetch_memory)
245 ret = snprintf(buf, n, "@0x%p", ff->data);
246 else if (ff->func == fetch_symbol) {
247 struct symbol_cache *sc = ff->data;
248 ret = snprintf(buf, n, "@%s%+ld", sc->symbol, sc->offset);
249 } else if (ff->func == fetch_retvalue)
250 ret = snprintf(buf, n, "$retval");
251 else if (ff->func == fetch_stack_address)
252 ret = snprintf(buf, n, "$stack");
253 else if (ff->func == fetch_indirect) {
254 struct indirect_fetch_data *id = ff->data;
256 ret = snprintf(buf, n, "%+ld(", id->offset);
260 ret = probe_arg_string(buf + l, n - l, &id->orig);
264 ret = snprintf(buf + l, n - l, ")");
273 static int register_probe_event(struct trace_probe *tp);
274 static void unregister_probe_event(struct trace_probe *tp);
276 static DEFINE_MUTEX(probe_lock);
277 static LIST_HEAD(probe_list);
279 static int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs);
280 static int kretprobe_dispatcher(struct kretprobe_instance *ri,
281 struct pt_regs *regs);
284 * Allocate new trace_probe and initialize it (including kprobes).
286 static struct trace_probe *alloc_trace_probe(const char *group,
291 int nargs, int is_return)
293 struct trace_probe *tp;
295 tp = kzalloc(SIZEOF_TRACE_PROBE(nargs), GFP_KERNEL);
297 return ERR_PTR(-ENOMEM);
300 tp->symbol = kstrdup(symbol, GFP_KERNEL);
303 tp->rp.kp.symbol_name = tp->symbol;
304 tp->rp.kp.offset = offs;
306 tp->rp.kp.addr = addr;
309 tp->rp.handler = kretprobe_dispatcher;
311 tp->rp.kp.pre_handler = kprobe_dispatcher;
315 tp->call.name = kstrdup(event, GFP_KERNEL);
321 tp->call.system = kstrdup(group, GFP_KERNEL);
322 if (!tp->call.system)
325 INIT_LIST_HEAD(&tp->list);
328 kfree(tp->call.name);
331 return ERR_PTR(-ENOMEM);
334 static void free_probe_arg(struct probe_arg *arg)
336 if (arg->fetch.func == fetch_symbol)
337 free_symbol_cache(arg->fetch.data);
338 else if (arg->fetch.func == fetch_indirect)
339 free_indirect_fetch_data(arg->fetch.data);
343 static void free_trace_probe(struct trace_probe *tp)
347 for (i = 0; i < tp->nr_args; i++)
348 free_probe_arg(&tp->args[i]);
350 kfree(tp->call.system);
351 kfree(tp->call.name);
356 static struct trace_probe *find_probe_event(const char *event)
358 struct trace_probe *tp;
360 list_for_each_entry(tp, &probe_list, list)
361 if (!strcmp(tp->call.name, event))
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);
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];
603 if (argv[0][0] == 'p')
605 else if (argv[0][0] == 'r')
610 if (argv[0][1] == ':') {
612 if (strchr(event, '/')) {
614 event = strchr(group, '/') + 1;
616 if (strlen(group) == 0) {
617 pr_info("Group name is not specifiled\n");
621 if (strlen(event) == 0) {
622 pr_info("Event name is not specifiled\n");
627 if (isdigit(argv[1][0])) {
630 /* an address specified */
631 ret = strict_strtoul(&argv[0][2], 0, (unsigned long *)&addr);
635 /* a symbol specified */
637 /* TODO: support .init module functions */
638 ret = split_symbol_offset(symbol, &offset);
641 if (offset && is_return)
644 argc -= 2; argv += 2;
648 group = KPROBE_EVENT_SYSTEM;
650 /* Make a new event name */
652 snprintf(buf, MAX_EVENT_NAME_LEN, "%c@%s%+ld",
653 is_return ? 'r' : 'p', symbol, offset);
655 snprintf(buf, MAX_EVENT_NAME_LEN, "%c@0x%p",
656 is_return ? 'r' : 'p', addr);
659 tp = alloc_trace_probe(group, event, addr, symbol, offset, argc,
664 /* parse arguments */
666 for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
667 /* Parse argument name */
668 arg = strchr(argv[i], '=');
674 if (conflict_field_name(argv[i], tp->args, i)) {
679 tp->args[i].name = kstrdup(argv[i], GFP_KERNEL);
681 /* Parse fetch argument */
682 if (strlen(arg) > MAX_ARGSTR_LEN) {
683 pr_info("Argument%d(%s) is too long.\n", i, arg);
687 ret = parse_probe_arg(arg, &tp->args[i].fetch, is_return);
693 ret = register_trace_probe(tp);
699 free_trace_probe(tp);
703 static void cleanup_all_probes(void)
705 struct trace_probe *tp;
707 mutex_lock(&probe_lock);
708 /* TODO: Use batch unregistration */
709 while (!list_empty(&probe_list)) {
710 tp = list_entry(probe_list.next, struct trace_probe, list);
711 unregister_trace_probe(tp);
712 free_trace_probe(tp);
714 mutex_unlock(&probe_lock);
718 /* Probes listing interfaces */
719 static void *probes_seq_start(struct seq_file *m, loff_t *pos)
721 mutex_lock(&probe_lock);
722 return seq_list_start(&probe_list, *pos);
725 static void *probes_seq_next(struct seq_file *m, void *v, loff_t *pos)
727 return seq_list_next(v, &probe_list, pos);
730 static void probes_seq_stop(struct seq_file *m, void *v)
732 mutex_unlock(&probe_lock);
735 static int probes_seq_show(struct seq_file *m, void *v)
737 struct trace_probe *tp = v;
739 char buf[MAX_ARGSTR_LEN + 1];
741 seq_printf(m, "%c", probe_is_return(tp) ? 'r' : 'p');
742 seq_printf(m, ":%s", tp->call.name);
745 seq_printf(m, " %s+%u", probe_symbol(tp), tp->rp.kp.offset);
747 seq_printf(m, " 0x%p", tp->rp.kp.addr);
749 for (i = 0; i < tp->nr_args; i++) {
750 ret = probe_arg_string(buf, MAX_ARGSTR_LEN, &tp->args[i].fetch);
752 pr_warning("Argument%d decoding error(%d).\n", i, ret);
755 seq_printf(m, " %s=%s", tp->args[i].name, buf);
761 static const struct seq_operations probes_seq_op = {
762 .start = probes_seq_start,
763 .next = probes_seq_next,
764 .stop = probes_seq_stop,
765 .show = probes_seq_show
768 static int probes_open(struct inode *inode, struct file *file)
770 if ((file->f_mode & FMODE_WRITE) &&
771 (file->f_flags & O_TRUNC))
772 cleanup_all_probes();
774 return seq_open(file, &probes_seq_op);
777 static int command_trace_probe(const char *buf)
780 int argc = 0, ret = 0;
782 argv = argv_split(GFP_KERNEL, buf, &argc);
787 ret = create_trace_probe(argc, argv);
793 #define WRITE_BUFSIZE 128
795 static ssize_t probes_write(struct file *file, const char __user *buffer,
796 size_t count, loff_t *ppos)
803 kbuf = kmalloc(WRITE_BUFSIZE, GFP_KERNEL);
808 while (done < count) {
810 if (size >= WRITE_BUFSIZE)
811 size = WRITE_BUFSIZE - 1;
812 if (copy_from_user(kbuf, buffer + done, size)) {
817 tmp = strchr(kbuf, '\n');
820 size = tmp - kbuf + 1;
821 } else if (done + size < count) {
822 pr_warning("Line length is too long: "
823 "Should be less than %d.", WRITE_BUFSIZE);
828 /* Remove comments */
829 tmp = strchr(kbuf, '#');
833 ret = command_trace_probe(kbuf);
843 static const struct file_operations kprobe_events_ops = {
844 .owner = THIS_MODULE,
848 .release = seq_release,
849 .write = probes_write,
852 /* Probes profiling interfaces */
853 static int probes_profile_seq_show(struct seq_file *m, void *v)
855 struct trace_probe *tp = v;
857 seq_printf(m, " %-44s %15lu %15lu\n", tp->call.name, tp->nhit,
863 static const struct seq_operations profile_seq_op = {
864 .start = probes_seq_start,
865 .next = probes_seq_next,
866 .stop = probes_seq_stop,
867 .show = probes_profile_seq_show
870 static int profile_open(struct inode *inode, struct file *file)
872 return seq_open(file, &profile_seq_op);
875 static const struct file_operations kprobe_profile_ops = {
876 .owner = THIS_MODULE,
877 .open = profile_open,
880 .release = seq_release,
884 static __kprobes int kprobe_trace_func(struct kprobe *kp, struct pt_regs *regs)
886 struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
887 struct kprobe_trace_entry *entry;
888 struct ring_buffer_event *event;
889 struct ring_buffer *buffer;
891 unsigned long irq_flags;
892 struct ftrace_event_call *call = &tp->call;
896 local_save_flags(irq_flags);
897 pc = preempt_count();
899 size = SIZEOF_KPROBE_TRACE_ENTRY(tp->nr_args);
901 event = trace_current_buffer_lock_reserve(&buffer, call->id, size,
906 entry = ring_buffer_event_data(event);
907 entry->nargs = tp->nr_args;
908 entry->ip = (unsigned long)kp->addr;
909 for (i = 0; i < tp->nr_args; i++)
910 entry->args[i] = call_fetch(&tp->args[i].fetch, regs);
912 if (!filter_current_check_discard(buffer, call, entry, event))
913 trace_nowake_buffer_unlock_commit(buffer, event, irq_flags, pc);
917 /* Kretprobe handler */
918 static __kprobes int kretprobe_trace_func(struct kretprobe_instance *ri,
919 struct pt_regs *regs)
921 struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
922 struct kretprobe_trace_entry *entry;
923 struct ring_buffer_event *event;
924 struct ring_buffer *buffer;
926 unsigned long irq_flags;
927 struct ftrace_event_call *call = &tp->call;
929 local_save_flags(irq_flags);
930 pc = preempt_count();
932 size = SIZEOF_KRETPROBE_TRACE_ENTRY(tp->nr_args);
934 event = trace_current_buffer_lock_reserve(&buffer, call->id, size,
939 entry = ring_buffer_event_data(event);
940 entry->nargs = tp->nr_args;
941 entry->func = (unsigned long)tp->rp.kp.addr;
942 entry->ret_ip = (unsigned long)ri->ret_addr;
943 for (i = 0; i < tp->nr_args; i++)
944 entry->args[i] = call_fetch(&tp->args[i].fetch, regs);
946 if (!filter_current_check_discard(buffer, call, entry, event))
947 trace_nowake_buffer_unlock_commit(buffer, event, irq_flags, pc);
952 /* Event entry printers */
954 print_kprobe_event(struct trace_iterator *iter, int flags)
956 struct kprobe_trace_entry *field;
957 struct trace_seq *s = &iter->seq;
958 struct trace_event *event;
959 struct trace_probe *tp;
962 field = (struct kprobe_trace_entry *)iter->ent;
963 event = ftrace_find_event(field->ent.type);
964 tp = container_of(event, struct trace_probe, event);
966 if (!trace_seq_printf(s, "%s: (", tp->call.name))
969 if (!seq_print_ip_sym(s, field->ip, flags | TRACE_ITER_SYM_OFFSET))
972 if (!trace_seq_puts(s, ")"))
975 for (i = 0; i < field->nargs; i++)
976 if (!trace_seq_printf(s, " %s=%lx",
977 tp->args[i].name, field->args[i]))
980 if (!trace_seq_puts(s, "\n"))
983 return TRACE_TYPE_HANDLED;
985 return TRACE_TYPE_PARTIAL_LINE;
989 print_kretprobe_event(struct trace_iterator *iter, int flags)
991 struct kretprobe_trace_entry *field;
992 struct trace_seq *s = &iter->seq;
993 struct trace_event *event;
994 struct trace_probe *tp;
997 field = (struct kretprobe_trace_entry *)iter->ent;
998 event = ftrace_find_event(field->ent.type);
999 tp = container_of(event, struct trace_probe, event);
1001 if (!trace_seq_printf(s, "%s: (", tp->call.name))
1004 if (!seq_print_ip_sym(s, field->ret_ip, flags | TRACE_ITER_SYM_OFFSET))
1007 if (!trace_seq_puts(s, " <- "))
1010 if (!seq_print_ip_sym(s, field->func, flags & ~TRACE_ITER_SYM_OFFSET))
1013 if (!trace_seq_puts(s, ")"))
1016 for (i = 0; i < field->nargs; i++)
1017 if (!trace_seq_printf(s, " %s=%lx",
1018 tp->args[i].name, field->args[i]))
1021 if (!trace_seq_puts(s, "\n"))
1024 return TRACE_TYPE_HANDLED;
1026 return TRACE_TYPE_PARTIAL_LINE;
1029 static int probe_event_enable(struct ftrace_event_call *call)
1031 struct trace_probe *tp = (struct trace_probe *)call->data;
1033 tp->flags |= TP_FLAG_TRACE;
1034 if (probe_is_return(tp))
1035 return enable_kretprobe(&tp->rp);
1037 return enable_kprobe(&tp->rp.kp);
1040 static void probe_event_disable(struct ftrace_event_call *call)
1042 struct trace_probe *tp = (struct trace_probe *)call->data;
1044 tp->flags &= ~TP_FLAG_TRACE;
1045 if (!(tp->flags & (TP_FLAG_TRACE | TP_FLAG_PROFILE))) {
1046 if (probe_is_return(tp))
1047 disable_kretprobe(&tp->rp);
1049 disable_kprobe(&tp->rp.kp);
1053 static int probe_event_raw_init(struct ftrace_event_call *event_call)
1055 INIT_LIST_HEAD(&event_call->fields);
1061 #define DEFINE_FIELD(type, item, name, is_signed) \
1063 ret = trace_define_field(event_call, #type, name, \
1064 offsetof(typeof(field), item), \
1065 sizeof(field.item), is_signed, \
1071 static int kprobe_event_define_fields(struct ftrace_event_call *event_call)
1074 struct kprobe_trace_entry field;
1075 struct trace_probe *tp = (struct trace_probe *)event_call->data;
1077 ret = trace_define_common_fields(event_call);
1081 DEFINE_FIELD(unsigned long, ip, FIELD_STRING_IP, 0);
1082 DEFINE_FIELD(int, nargs, FIELD_STRING_NARGS, 1);
1083 /* Set argument names as fields */
1084 for (i = 0; i < tp->nr_args; i++)
1085 DEFINE_FIELD(unsigned long, args[i], tp->args[i].name, 0);
1089 static int kretprobe_event_define_fields(struct ftrace_event_call *event_call)
1092 struct kretprobe_trace_entry field;
1093 struct trace_probe *tp = (struct trace_probe *)event_call->data;
1095 ret = trace_define_common_fields(event_call);
1099 DEFINE_FIELD(unsigned long, func, FIELD_STRING_FUNC, 0);
1100 DEFINE_FIELD(unsigned long, ret_ip, FIELD_STRING_RETIP, 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 __probe_event_show_format(struct trace_seq *s,
1109 struct trace_probe *tp, const char *fmt,
1115 if (!trace_seq_printf(s, "\nprint fmt: \"%s", fmt))
1118 for (i = 0; i < tp->nr_args; i++)
1119 if (!trace_seq_printf(s, " %s=%%lx", tp->args[i].name))
1122 if (!trace_seq_printf(s, "\", %s", arg))
1125 for (i = 0; i < tp->nr_args; i++)
1126 if (!trace_seq_printf(s, ", REC->%s", tp->args[i].name))
1129 return trace_seq_puts(s, "\n");
1133 #define SHOW_FIELD(type, item, name) \
1135 ret = trace_seq_printf(s, "\tfield: " #type " %s;\t" \
1136 "offset:%u;\tsize:%u;\n", name, \
1137 (unsigned int)offsetof(typeof(field), item),\
1138 (unsigned int)sizeof(type)); \
1143 static int kprobe_event_show_format(struct ftrace_event_call *call,
1144 struct trace_seq *s)
1146 struct kprobe_trace_entry field __attribute__((unused));
1148 struct trace_probe *tp = (struct trace_probe *)call->data;
1150 SHOW_FIELD(unsigned long, ip, FIELD_STRING_IP);
1151 SHOW_FIELD(int, nargs, FIELD_STRING_NARGS);
1154 for (i = 0; i < tp->nr_args; i++)
1155 SHOW_FIELD(unsigned long, args[i], tp->args[i].name);
1156 trace_seq_puts(s, "\n");
1158 return __probe_event_show_format(s, tp, "(%lx)",
1159 "REC->" FIELD_STRING_IP);
1162 static int kretprobe_event_show_format(struct ftrace_event_call *call,
1163 struct trace_seq *s)
1165 struct kretprobe_trace_entry field __attribute__((unused));
1167 struct trace_probe *tp = (struct trace_probe *)call->data;
1169 SHOW_FIELD(unsigned long, func, FIELD_STRING_FUNC);
1170 SHOW_FIELD(unsigned long, ret_ip, FIELD_STRING_RETIP);
1171 SHOW_FIELD(int, nargs, FIELD_STRING_NARGS);
1174 for (i = 0; i < tp->nr_args; i++)
1175 SHOW_FIELD(unsigned long, args[i], tp->args[i].name);
1176 trace_seq_puts(s, "\n");
1178 return __probe_event_show_format(s, tp, "(%lx <- %lx)",
1179 "REC->" FIELD_STRING_FUNC
1180 ", REC->" FIELD_STRING_RETIP);
1183 #ifdef CONFIG_EVENT_PROFILE
1185 /* Kprobe profile handler */
1186 static __kprobes int kprobe_profile_func(struct kprobe *kp,
1187 struct pt_regs *regs)
1189 struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
1190 struct ftrace_event_call *call = &tp->call;
1191 struct kprobe_trace_entry *entry;
1192 struct trace_entry *ent;
1193 int size, __size, i, pc, __cpu;
1194 unsigned long irq_flags;
1197 pc = preempt_count();
1198 __size = SIZEOF_KPROBE_TRACE_ENTRY(tp->nr_args);
1199 size = ALIGN(__size + sizeof(u32), sizeof(u64));
1200 size -= sizeof(u32);
1201 if (WARN_ONCE(size > FTRACE_MAX_PROFILE_SIZE,
1202 "profile buffer not large enough"))
1206 * Protect the non nmi buffer
1207 * This also protects the rcu read side
1209 local_irq_save(irq_flags);
1210 __cpu = smp_processor_id();
1213 raw_data = rcu_dereference(trace_profile_buf_nmi);
1215 raw_data = rcu_dereference(trace_profile_buf);
1220 raw_data = per_cpu_ptr(raw_data, __cpu);
1221 /* Zero dead bytes from alignment to avoid buffer leak to userspace */
1222 *(u64 *)(&raw_data[size - sizeof(u64)]) = 0ULL;
1223 entry = (struct kprobe_trace_entry *)raw_data;
1226 tracing_generic_entry_update(ent, irq_flags, pc);
1227 ent->type = call->id;
1228 entry->nargs = tp->nr_args;
1229 entry->ip = (unsigned long)kp->addr;
1230 for (i = 0; i < tp->nr_args; i++)
1231 entry->args[i] = call_fetch(&tp->args[i].fetch, regs);
1232 perf_tp_event(call->id, entry->ip, 1, entry, size);
1234 local_irq_restore(irq_flags);
1238 /* Kretprobe profile handler */
1239 static __kprobes int kretprobe_profile_func(struct kretprobe_instance *ri,
1240 struct pt_regs *regs)
1242 struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
1243 struct ftrace_event_call *call = &tp->call;
1244 struct kretprobe_trace_entry *entry;
1245 struct trace_entry *ent;
1246 int size, __size, i, pc, __cpu;
1247 unsigned long irq_flags;
1250 pc = preempt_count();
1251 __size = SIZEOF_KRETPROBE_TRACE_ENTRY(tp->nr_args);
1252 size = ALIGN(__size + sizeof(u32), sizeof(u64));
1253 size -= sizeof(u32);
1254 if (WARN_ONCE(size > FTRACE_MAX_PROFILE_SIZE,
1255 "profile buffer not large enough"))
1259 * Protect the non nmi buffer
1260 * This also protects the rcu read side
1262 local_irq_save(irq_flags);
1263 __cpu = smp_processor_id();
1266 raw_data = rcu_dereference(trace_profile_buf_nmi);
1268 raw_data = rcu_dereference(trace_profile_buf);
1273 raw_data = per_cpu_ptr(raw_data, __cpu);
1274 /* Zero dead bytes from alignment to avoid buffer leak to userspace */
1275 *(u64 *)(&raw_data[size - sizeof(u64)]) = 0ULL;
1276 entry = (struct kretprobe_trace_entry *)raw_data;
1279 tracing_generic_entry_update(ent, irq_flags, pc);
1280 ent->type = call->id;
1281 entry->nargs = tp->nr_args;
1282 entry->func = (unsigned long)tp->rp.kp.addr;
1283 entry->ret_ip = (unsigned long)ri->ret_addr;
1284 for (i = 0; i < tp->nr_args; i++)
1285 entry->args[i] = call_fetch(&tp->args[i].fetch, regs);
1286 perf_tp_event(call->id, entry->ret_ip, 1, entry, size);
1288 local_irq_restore(irq_flags);
1292 static int probe_profile_enable(struct ftrace_event_call *call)
1294 struct trace_probe *tp = (struct trace_probe *)call->data;
1296 tp->flags |= TP_FLAG_PROFILE;
1298 if (probe_is_return(tp))
1299 return enable_kretprobe(&tp->rp);
1301 return enable_kprobe(&tp->rp.kp);
1304 static void probe_profile_disable(struct ftrace_event_call *call)
1306 struct trace_probe *tp = (struct trace_probe *)call->data;
1308 tp->flags &= ~TP_FLAG_PROFILE;
1310 if (!(tp->flags & TP_FLAG_TRACE)) {
1311 if (probe_is_return(tp))
1312 disable_kretprobe(&tp->rp);
1314 disable_kprobe(&tp->rp.kp);
1317 #endif /* CONFIG_EVENT_PROFILE */
1321 int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs)
1323 struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
1325 if (tp->flags & TP_FLAG_TRACE)
1326 kprobe_trace_func(kp, regs);
1327 #ifdef CONFIG_EVENT_PROFILE
1328 if (tp->flags & TP_FLAG_PROFILE)
1329 kprobe_profile_func(kp, regs);
1330 #endif /* CONFIG_EVENT_PROFILE */
1331 return 0; /* We don't tweek kernel, so just return 0 */
1335 int kretprobe_dispatcher(struct kretprobe_instance *ri, struct pt_regs *regs)
1337 struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
1339 if (tp->flags & TP_FLAG_TRACE)
1340 kretprobe_trace_func(ri, regs);
1341 #ifdef CONFIG_EVENT_PROFILE
1342 if (tp->flags & TP_FLAG_PROFILE)
1343 kretprobe_profile_func(ri, regs);
1344 #endif /* CONFIG_EVENT_PROFILE */
1345 return 0; /* We don't tweek kernel, so just return 0 */
1348 static int register_probe_event(struct trace_probe *tp)
1350 struct ftrace_event_call *call = &tp->call;
1353 /* Initialize ftrace_event_call */
1354 if (probe_is_return(tp)) {
1355 tp->event.trace = print_kretprobe_event;
1356 call->raw_init = probe_event_raw_init;
1357 call->show_format = kretprobe_event_show_format;
1358 call->define_fields = kretprobe_event_define_fields;
1360 tp->event.trace = print_kprobe_event;
1361 call->raw_init = probe_event_raw_init;
1362 call->show_format = kprobe_event_show_format;
1363 call->define_fields = kprobe_event_define_fields;
1365 call->event = &tp->event;
1366 call->id = register_ftrace_event(&tp->event);
1370 call->regfunc = probe_event_enable;
1371 call->unregfunc = probe_event_disable;
1373 #ifdef CONFIG_EVENT_PROFILE
1374 atomic_set(&call->profile_count, -1);
1375 call->profile_enable = probe_profile_enable;
1376 call->profile_disable = probe_profile_disable;
1379 ret = trace_add_event_call(call);
1381 pr_info("Failed to register kprobe event: %s\n", call->name);
1382 unregister_ftrace_event(&tp->event);
1387 static void unregister_probe_event(struct trace_probe *tp)
1389 /* tp->event is unregistered in trace_remove_event_call() */
1390 trace_remove_event_call(&tp->call);
1393 /* Make a debugfs interface for controling probe points */
1394 static __init int init_kprobe_trace(void)
1396 struct dentry *d_tracer;
1397 struct dentry *entry;
1399 d_tracer = tracing_init_dentry();
1403 entry = debugfs_create_file("kprobe_events", 0644, d_tracer,
1404 NULL, &kprobe_events_ops);
1406 /* Event list interface */
1408 pr_warning("Could not create debugfs "
1409 "'kprobe_events' entry\n");
1411 /* Profile interface */
1412 entry = debugfs_create_file("kprobe_profile", 0444, d_tracer,
1413 NULL, &kprobe_profile_ops);
1416 pr_warning("Could not create debugfs "
1417 "'kprobe_profile' entry\n");
1420 fs_initcall(init_kprobe_trace);
1423 #ifdef CONFIG_FTRACE_STARTUP_TEST
1425 static int kprobe_trace_selftest_target(int a1, int a2, int a3,
1426 int a4, int a5, int a6)
1428 return a1 + a2 + a3 + a4 + a5 + a6;
1431 static __init int kprobe_trace_self_tests_init(void)
1434 int (*target)(int, int, int, int, int, int);
1436 target = kprobe_trace_selftest_target;
1438 pr_info("Testing kprobe tracing: ");
1440 ret = command_trace_probe("p:testprobe kprobe_trace_selftest_target "
1441 "$arg1 $arg2 $arg3 $arg4 $stack $stack0");
1442 if (WARN_ON_ONCE(ret))
1443 pr_warning("error enabling function entry\n");
1445 ret = command_trace_probe("r:testprobe2 kprobe_trace_selftest_target "
1447 if (WARN_ON_ONCE(ret))
1448 pr_warning("error enabling function return\n");
1450 ret = target(1, 2, 3, 4, 5, 6);
1452 cleanup_all_probes();
1458 late_initcall(kprobe_trace_self_tests_init);