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_counter.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
40 /* currently, trace_kprobe only supports X86. */
43 unsigned long (*func)(struct pt_regs *, void *);
47 static __kprobes unsigned long call_fetch(struct fetch_func *f,
50 return f->func(regs, f->data);
54 static __kprobes unsigned long fetch_register(struct pt_regs *regs,
57 return regs_get_register(regs, (unsigned int)((unsigned long)offset));
60 static __kprobes unsigned long fetch_stack(struct pt_regs *regs,
63 return regs_get_kernel_stack_nth(regs,
64 (unsigned int)((unsigned long)num));
67 static __kprobes unsigned long fetch_memory(struct pt_regs *regs, void *addr)
71 if (probe_kernel_address(addr, retval))
76 static __kprobes unsigned long fetch_argument(struct pt_regs *regs, void *num)
78 return regs_get_argument_nth(regs, (unsigned int)((unsigned long)num));
81 static __kprobes unsigned long fetch_retvalue(struct pt_regs *regs,
84 return regs_return_value(regs);
87 static __kprobes unsigned long fetch_ip(struct pt_regs *regs, void *dummy)
89 return instruction_pointer(regs);
92 static __kprobes unsigned long fetch_stack_address(struct pt_regs *regs,
95 return kernel_stack_pointer(regs);
98 /* Memory fetching by symbol */
105 static unsigned long update_symbol_cache(struct symbol_cache *sc)
107 sc->addr = (unsigned long)kallsyms_lookup_name(sc->symbol);
109 sc->addr += sc->offset;
113 static void free_symbol_cache(struct symbol_cache *sc)
119 static struct symbol_cache *alloc_symbol_cache(const char *sym, long offset)
121 struct symbol_cache *sc;
123 if (!sym || strlen(sym) == 0)
125 sc = kzalloc(sizeof(struct symbol_cache), GFP_KERNEL);
129 sc->symbol = kstrdup(sym, GFP_KERNEL);
136 update_symbol_cache(sc);
140 static __kprobes unsigned long fetch_symbol(struct pt_regs *regs, void *data)
142 struct symbol_cache *sc = data;
145 return fetch_memory(regs, (void *)sc->addr);
150 /* Special indirect memory access interface */
151 struct indirect_fetch_data {
152 struct fetch_func orig;
156 static __kprobes unsigned long fetch_indirect(struct pt_regs *regs, void *data)
158 struct indirect_fetch_data *ind = data;
161 addr = call_fetch(&ind->orig, regs);
164 return fetch_memory(regs, (void *)addr);
169 static __kprobes void free_indirect_fetch_data(struct indirect_fetch_data *data)
171 if (data->orig.func == fetch_indirect)
172 free_indirect_fetch_data(data->orig.data);
173 else if (data->orig.func == fetch_symbol)
174 free_symbol_cache(data->orig.data);
183 struct list_head list;
184 struct kretprobe rp; /* Use rp.kp for kprobe use */
186 const char *symbol; /* symbol name */
187 struct ftrace_event_call call;
188 struct trace_event event;
189 unsigned int nr_args;
190 struct fetch_func args[];
193 #define SIZEOF_TRACE_PROBE(n) \
194 (offsetof(struct trace_probe, args) + \
195 (sizeof(struct fetch_func) * (n)))
197 static int kprobe_trace_func(struct kprobe *kp, struct pt_regs *regs);
198 static int kretprobe_trace_func(struct kretprobe_instance *ri,
199 struct pt_regs *regs);
201 static __kprobes int probe_is_return(struct trace_probe *tp)
203 return tp->rp.handler != NULL;
206 static __kprobes const char *probe_symbol(struct trace_probe *tp)
208 return tp->symbol ? tp->symbol : "unknown";
211 static int probe_arg_string(char *buf, size_t n, struct fetch_func *ff)
215 if (ff->func == fetch_argument)
216 ret = snprintf(buf, n, "a%lu", (unsigned long)ff->data);
217 else if (ff->func == fetch_register) {
219 name = regs_query_register_name((unsigned int)((long)ff->data));
220 ret = snprintf(buf, n, "%%%s", name);
221 } else if (ff->func == fetch_stack)
222 ret = snprintf(buf, n, "s%lu", (unsigned long)ff->data);
223 else if (ff->func == fetch_memory)
224 ret = snprintf(buf, n, "@0x%p", ff->data);
225 else if (ff->func == fetch_symbol) {
226 struct symbol_cache *sc = ff->data;
227 ret = snprintf(buf, n, "@%s%+ld", sc->symbol, sc->offset);
228 } else if (ff->func == fetch_retvalue)
229 ret = snprintf(buf, n, "rv");
230 else if (ff->func == fetch_ip)
231 ret = snprintf(buf, n, "ra");
232 else if (ff->func == fetch_stack_address)
233 ret = snprintf(buf, n, "sa");
234 else if (ff->func == fetch_indirect) {
235 struct indirect_fetch_data *id = ff->data;
237 ret = snprintf(buf, n, "%+ld(", id->offset);
241 ret = probe_arg_string(buf + l, n - l, &id->orig);
245 ret = snprintf(buf + l, n - l, ")");
254 static int register_probe_event(struct trace_probe *tp);
255 static void unregister_probe_event(struct trace_probe *tp);
257 static DEFINE_MUTEX(probe_lock);
258 static LIST_HEAD(probe_list);
261 * Allocate new trace_probe and initialize it (including kprobes).
263 static struct trace_probe *alloc_trace_probe(const char *event,
267 int nargs, int is_return)
269 struct trace_probe *tp;
271 tp = kzalloc(SIZEOF_TRACE_PROBE(nargs), GFP_KERNEL);
273 return ERR_PTR(-ENOMEM);
276 tp->symbol = kstrdup(symbol, GFP_KERNEL);
279 tp->rp.kp.symbol_name = tp->symbol;
280 tp->rp.kp.offset = offs;
282 tp->rp.kp.addr = addr;
284 /* Set handler here for checking whether this probe is return or not. */
286 tp->rp.handler = kretprobe_trace_func;
288 tp->rp.kp.pre_handler = kprobe_trace_func;
292 tp->call.name = kstrdup(event, GFP_KERNEL);
296 INIT_LIST_HEAD(&tp->list);
301 return ERR_PTR(-ENOMEM);
304 static void free_trace_probe(struct trace_probe *tp)
308 for (i = 0; i < tp->nr_args; i++)
309 if (tp->args[i].func == fetch_symbol)
310 free_symbol_cache(tp->args[i].data);
311 else if (tp->args[i].func == fetch_indirect)
312 free_indirect_fetch_data(tp->args[i].data);
314 kfree(tp->call.name);
319 static struct trace_probe *find_probe_event(const char *event)
321 struct trace_probe *tp;
323 list_for_each_entry(tp, &probe_list, list)
324 if (!strcmp(tp->call.name, event))
329 static void __unregister_trace_probe(struct trace_probe *tp)
331 if (probe_is_return(tp))
332 unregister_kretprobe(&tp->rp);
334 unregister_kprobe(&tp->rp.kp);
337 /* Unregister a trace_probe and probe_event: call with locking probe_lock */
338 static void unregister_trace_probe(struct trace_probe *tp)
340 unregister_probe_event(tp);
341 __unregister_trace_probe(tp);
345 /* Register a trace_probe and probe_event */
346 static int register_trace_probe(struct trace_probe *tp)
348 struct trace_probe *old_tp;
351 mutex_lock(&probe_lock);
353 if (probe_is_return(tp))
354 ret = register_kretprobe(&tp->rp);
356 ret = register_kprobe(&tp->rp.kp);
359 pr_warning("Could not insert probe(%d)\n", ret);
360 if (ret == -EILSEQ) {
361 pr_warning("Probing address(0x%p) is not an "
362 "instruction boundary.\n",
368 /* register as an event */
369 old_tp = find_probe_event(tp->call.name);
371 /* delete old event */
372 unregister_trace_probe(old_tp);
373 free_trace_probe(old_tp);
375 ret = register_probe_event(tp);
377 pr_warning("Faild to register probe event(%d)\n", ret);
378 __unregister_trace_probe(tp);
380 list_add_tail(&tp->list, &probe_list);
382 mutex_unlock(&probe_lock);
386 /* Split symbol and offset. */
387 static int split_symbol_offset(char *symbol, unsigned long *offset)
395 tmp = strchr(symbol, '+');
397 /* skip sign because strict_strtol doesn't accept '+' */
398 ret = strict_strtoul(tmp + 1, 0, offset);
407 #define PARAM_MAX_ARGS 16
408 #define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long))
410 static int parse_probe_arg(char *arg, struct fetch_func *ff, int is_return)
418 case 'a': /* argument */
419 ret = strict_strtoul(arg + 1, 10, ¶m);
420 if (ret || param > PARAM_MAX_ARGS)
423 ff->func = fetch_argument;
424 ff->data = (void *)param;
427 case 'r': /* retval or retaddr */
428 if (is_return && arg[1] == 'v') {
429 ff->func = fetch_retvalue;
431 } else if (is_return && arg[1] == 'a') {
437 case '%': /* named register */
438 ret = regs_query_register_offset(arg + 1);
440 ff->func = fetch_register;
441 ff->data = (void *)(unsigned long)ret;
445 case 's': /* stack */
447 ff->func = fetch_stack_address;
450 ret = strict_strtoul(arg + 1, 10, ¶m);
451 if (ret || param > PARAM_MAX_STACK)
454 ff->func = fetch_stack;
455 ff->data = (void *)param;
459 case '@': /* memory or symbol */
460 if (isdigit(arg[1])) {
461 ret = strict_strtoul(arg + 1, 0, ¶m);
464 ff->func = fetch_memory;
465 ff->data = (void *)param;
467 ret = split_symbol_offset(arg + 1, &offset);
470 ff->data = alloc_symbol_cache(arg + 1,
473 ff->func = fetch_symbol;
478 case '+': /* indirect memory */
480 tmp = strchr(arg, '(');
486 ret = strict_strtol(arg + 1, 0, &offset);
492 tmp = strrchr(arg, ')');
494 struct indirect_fetch_data *id;
496 id = kzalloc(sizeof(struct indirect_fetch_data),
501 ret = parse_probe_arg(arg, &id->orig, is_return);
505 ff->func = fetch_indirect;
506 ff->data = (void *)id;
512 /* TODO: support custom handler */
518 static int create_trace_probe(int argc, char **argv)
522 * - Add kprobe: p[:EVENT] SYMBOL[+OFFS]|ADDRESS [FETCHARGS]
523 * - Add kretprobe: r[:EVENT] SYMBOL[+0] [FETCHARGS]
525 * aN : fetch Nth of function argument. (N:0-)
526 * rv : fetch return value
527 * ra : fetch return address
528 * sa : fetch stack address
529 * sN : fetch Nth of stack (N:0-)
530 * @ADDR : fetch memory at ADDR (ADDR should be in kernel)
531 * @SYM[+|-offs] : fetch memory at SYM +|- offs (SYM is a data symbol)
532 * %REG : fetch register REG
533 * Indirect memory fetch:
534 * +|-offs(ARG) : fetch memory at ARG +|- offs address.
536 struct trace_probe *tp;
539 char *symbol = NULL, *event = NULL;
540 unsigned long offset = 0;
542 char buf[MAX_EVENT_NAME_LEN];
547 if (argv[0][0] == 'p')
549 else if (argv[0][0] == 'r')
554 if (argv[0][1] == ':') {
556 if (strlen(event) == 0) {
557 pr_info("Event name is not specifiled\n");
562 if (isdigit(argv[1][0])) {
565 /* an address specified */
566 ret = strict_strtoul(&argv[0][2], 0, (unsigned long *)&addr);
570 /* a symbol specified */
572 /* TODO: support .init module functions */
573 ret = split_symbol_offset(symbol, &offset);
576 if (offset && is_return)
579 argc -= 2; argv += 2;
583 /* Make a new event name */
585 snprintf(buf, MAX_EVENT_NAME_LEN, "%c@%s%+ld",
586 is_return ? 'r' : 'p', symbol, offset);
588 snprintf(buf, MAX_EVENT_NAME_LEN, "%c@0x%p",
589 is_return ? 'r' : 'p', addr);
592 tp = alloc_trace_probe(event, addr, symbol, offset, argc, is_return);
596 /* parse arguments */
598 for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
599 if (strlen(argv[i]) > MAX_ARGSTR_LEN) {
600 pr_info("Argument%d(%s) is too long.\n", i, argv[i]);
604 ret = parse_probe_arg(argv[i], &tp->args[i], is_return);
610 ret = register_trace_probe(tp);
616 free_trace_probe(tp);
620 static void cleanup_all_probes(void)
622 struct trace_probe *tp;
624 mutex_lock(&probe_lock);
625 /* TODO: Use batch unregistration */
626 while (!list_empty(&probe_list)) {
627 tp = list_entry(probe_list.next, struct trace_probe, list);
628 unregister_trace_probe(tp);
629 free_trace_probe(tp);
631 mutex_unlock(&probe_lock);
635 /* Probes listing interfaces */
636 static void *probes_seq_start(struct seq_file *m, loff_t *pos)
638 mutex_lock(&probe_lock);
639 return seq_list_start(&probe_list, *pos);
642 static void *probes_seq_next(struct seq_file *m, void *v, loff_t *pos)
644 return seq_list_next(v, &probe_list, pos);
647 static void probes_seq_stop(struct seq_file *m, void *v)
649 mutex_unlock(&probe_lock);
652 static int probes_seq_show(struct seq_file *m, void *v)
654 struct trace_probe *tp = v;
656 char buf[MAX_ARGSTR_LEN + 1];
658 seq_printf(m, "%c", probe_is_return(tp) ? 'r' : 'p');
659 seq_printf(m, ":%s", tp->call.name);
662 seq_printf(m, " %s+%u", probe_symbol(tp), tp->rp.kp.offset);
664 seq_printf(m, " 0x%p", tp->rp.kp.addr);
666 for (i = 0; i < tp->nr_args; i++) {
667 ret = probe_arg_string(buf, MAX_ARGSTR_LEN, &tp->args[i]);
669 pr_warning("Argument%d decoding error(%d).\n", i, ret);
672 seq_printf(m, " %s", buf);
678 static const struct seq_operations probes_seq_op = {
679 .start = probes_seq_start,
680 .next = probes_seq_next,
681 .stop = probes_seq_stop,
682 .show = probes_seq_show
685 static int probes_open(struct inode *inode, struct file *file)
687 if ((file->f_mode & FMODE_WRITE) &&
688 (file->f_flags & O_TRUNC))
689 cleanup_all_probes();
691 return seq_open(file, &probes_seq_op);
694 static int command_trace_probe(const char *buf)
697 int argc = 0, ret = 0;
699 argv = argv_split(GFP_KERNEL, buf, &argc);
704 ret = create_trace_probe(argc, argv);
710 #define WRITE_BUFSIZE 128
712 static ssize_t probes_write(struct file *file, const char __user *buffer,
713 size_t count, loff_t *ppos)
720 kbuf = kmalloc(WRITE_BUFSIZE, GFP_KERNEL);
725 while (done < count) {
727 if (size >= WRITE_BUFSIZE)
728 size = WRITE_BUFSIZE - 1;
729 if (copy_from_user(kbuf, buffer + done, size)) {
734 tmp = strchr(kbuf, '\n');
737 size = tmp - kbuf + 1;
738 } else if (done + size < count) {
739 pr_warning("Line length is too long: "
740 "Should be less than %d.", WRITE_BUFSIZE);
745 /* Remove comments */
746 tmp = strchr(kbuf, '#');
750 ret = command_trace_probe(kbuf);
760 static const struct file_operations kprobe_events_ops = {
761 .owner = THIS_MODULE,
765 .release = seq_release,
766 .write = probes_write,
769 /* Probes profiling interfaces */
770 static int probes_profile_seq_show(struct seq_file *m, void *v)
772 struct trace_probe *tp = v;
774 seq_printf(m, " %-44s %15lu %15lu\n", tp->call.name, tp->nhit,
780 static const struct seq_operations profile_seq_op = {
781 .start = probes_seq_start,
782 .next = probes_seq_next,
783 .stop = probes_seq_stop,
784 .show = probes_profile_seq_show
787 static int profile_open(struct inode *inode, struct file *file)
789 return seq_open(file, &profile_seq_op);
792 static const struct file_operations kprobe_profile_ops = {
793 .owner = THIS_MODULE,
794 .open = profile_open,
797 .release = seq_release,
801 static __kprobes int kprobe_trace_func(struct kprobe *kp, struct pt_regs *regs)
803 struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
804 struct kprobe_trace_entry *entry;
805 struct ring_buffer_event *event;
806 struct ring_buffer *buffer;
808 unsigned long irq_flags;
809 struct ftrace_event_call *call = &tp->call;
813 local_save_flags(irq_flags);
814 pc = preempt_count();
816 size = SIZEOF_KPROBE_TRACE_ENTRY(tp->nr_args);
818 event = trace_current_buffer_lock_reserve(&buffer, call->id, size,
823 entry = ring_buffer_event_data(event);
824 entry->nargs = tp->nr_args;
825 entry->ip = (unsigned long)kp->addr;
826 for (i = 0; i < tp->nr_args; i++)
827 entry->args[i] = call_fetch(&tp->args[i], regs);
829 if (!filter_current_check_discard(buffer, call, entry, event))
830 trace_nowake_buffer_unlock_commit(buffer, event, irq_flags, pc);
834 /* Kretprobe handler */
835 static __kprobes int kretprobe_trace_func(struct kretprobe_instance *ri,
836 struct pt_regs *regs)
838 struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
839 struct kretprobe_trace_entry *entry;
840 struct ring_buffer_event *event;
841 struct ring_buffer *buffer;
843 unsigned long irq_flags;
844 struct ftrace_event_call *call = &tp->call;
846 local_save_flags(irq_flags);
847 pc = preempt_count();
849 size = SIZEOF_KRETPROBE_TRACE_ENTRY(tp->nr_args);
851 event = trace_current_buffer_lock_reserve(&buffer, call->id, size,
856 entry = ring_buffer_event_data(event);
857 entry->nargs = tp->nr_args;
858 entry->func = (unsigned long)tp->rp.kp.addr;
859 entry->ret_ip = (unsigned long)ri->ret_addr;
860 for (i = 0; i < tp->nr_args; i++)
861 entry->args[i] = call_fetch(&tp->args[i], regs);
863 if (!filter_current_check_discard(buffer, call, entry, event))
864 trace_nowake_buffer_unlock_commit(buffer, event, irq_flags, pc);
869 /* Event entry printers */
871 print_kprobe_event(struct trace_iterator *iter, int flags)
873 struct kprobe_trace_entry *field;
874 struct trace_seq *s = &iter->seq;
877 field = (struct kprobe_trace_entry *)iter->ent;
879 if (!seq_print_ip_sym(s, field->ip, flags | TRACE_ITER_SYM_OFFSET))
882 if (!trace_seq_puts(s, ":"))
885 for (i = 0; i < field->nargs; i++)
886 if (!trace_seq_printf(s, " 0x%lx", field->args[i]))
889 if (!trace_seq_puts(s, "\n"))
892 return TRACE_TYPE_HANDLED;
894 return TRACE_TYPE_PARTIAL_LINE;
898 print_kretprobe_event(struct trace_iterator *iter, int flags)
900 struct kretprobe_trace_entry *field;
901 struct trace_seq *s = &iter->seq;
904 field = (struct kretprobe_trace_entry *)iter->ent;
906 if (!seq_print_ip_sym(s, field->ret_ip, flags | TRACE_ITER_SYM_OFFSET))
909 if (!trace_seq_puts(s, " <- "))
912 if (!seq_print_ip_sym(s, field->func, flags & ~TRACE_ITER_SYM_OFFSET))
915 if (!trace_seq_puts(s, ":"))
918 for (i = 0; i < field->nargs; i++)
919 if (!trace_seq_printf(s, " 0x%lx", field->args[i]))
922 if (!trace_seq_puts(s, "\n"))
925 return TRACE_TYPE_HANDLED;
927 return TRACE_TYPE_PARTIAL_LINE;
930 static int probe_event_enable(struct ftrace_event_call *call)
932 struct trace_probe *tp = (struct trace_probe *)call->data;
934 if (probe_is_return(tp)) {
935 tp->rp.handler = kretprobe_trace_func;
936 return enable_kretprobe(&tp->rp);
938 tp->rp.kp.pre_handler = kprobe_trace_func;
939 return enable_kprobe(&tp->rp.kp);
943 static void probe_event_disable(struct ftrace_event_call *call)
945 struct trace_probe *tp = (struct trace_probe *)call->data;
947 if (probe_is_return(tp))
948 disable_kretprobe(&tp->rp);
950 disable_kprobe(&tp->rp.kp);
953 static int probe_event_raw_init(struct ftrace_event_call *event_call)
955 INIT_LIST_HEAD(&event_call->fields);
961 #define DEFINE_FIELD(type, item, name, is_signed) \
963 ret = trace_define_field(event_call, #type, name, \
964 offsetof(typeof(field), item), \
965 sizeof(field.item), is_signed, \
971 static int kprobe_event_define_fields(struct ftrace_event_call *event_call)
974 struct kprobe_trace_entry field;
975 char buf[MAX_ARGSTR_LEN + 1];
976 struct trace_probe *tp = (struct trace_probe *)event_call->data;
978 ret = trace_define_common_fields(event_call);
982 DEFINE_FIELD(unsigned long, ip, "ip", 0);
983 DEFINE_FIELD(int, nargs, "nargs", 1);
984 for (i = 0; i < tp->nr_args; i++) {
985 /* Set argN as a field */
986 sprintf(buf, "arg%d", i);
987 DEFINE_FIELD(unsigned long, args[i], buf, 0);
988 /* Set argument string as an alias field */
989 ret = probe_arg_string(buf, MAX_ARGSTR_LEN, &tp->args[i]);
992 DEFINE_FIELD(unsigned long, args[i], buf, 0);
997 static int kretprobe_event_define_fields(struct ftrace_event_call *event_call)
1000 struct kretprobe_trace_entry field;
1001 char buf[MAX_ARGSTR_LEN + 1];
1002 struct trace_probe *tp = (struct trace_probe *)event_call->data;
1004 ret = trace_define_common_fields(event_call);
1008 DEFINE_FIELD(unsigned long, func, "func", 0);
1009 DEFINE_FIELD(unsigned long, ret_ip, "ret_ip", 0);
1010 DEFINE_FIELD(int, nargs, "nargs", 1);
1011 for (i = 0; i < tp->nr_args; i++) {
1012 /* Set argN as a field */
1013 sprintf(buf, "arg%d", i);
1014 DEFINE_FIELD(unsigned long, args[i], buf, 0);
1015 /* Set argument string as an alias field */
1016 ret = probe_arg_string(buf, MAX_ARGSTR_LEN, &tp->args[i]);
1019 DEFINE_FIELD(unsigned long, args[i], buf, 0);
1024 static int __probe_event_show_format(struct trace_seq *s,
1025 struct trace_probe *tp, const char *fmt,
1029 char buf[MAX_ARGSTR_LEN + 1];
1032 for (i = 0; i < tp->nr_args; i++) {
1033 ret = probe_arg_string(buf, MAX_ARGSTR_LEN, &tp->args[i]);
1036 if (!trace_seq_printf(s, "\talias: %s;\toriginal: arg%d;\n",
1041 if (!trace_seq_printf(s, "\nprint fmt: \"%s", fmt))
1044 for (i = 0; i < tp->nr_args; i++)
1045 if (!trace_seq_puts(s, " 0x%lx"))
1048 if (!trace_seq_printf(s, "\", %s", arg))
1051 for (i = 0; i < tp->nr_args; i++)
1052 if (!trace_seq_printf(s, ", arg%d", i))
1055 return trace_seq_puts(s, "\n");
1059 #define SHOW_FIELD(type, item, name) \
1061 ret = trace_seq_printf(s, "\tfield: " #type " %s;\t" \
1062 "offset:%u;\tsize:%u;\n", name, \
1063 (unsigned int)offsetof(typeof(field), item),\
1064 (unsigned int)sizeof(type)); \
1069 static int kprobe_event_show_format(struct ftrace_event_call *call,
1070 struct trace_seq *s)
1072 struct kprobe_trace_entry field __attribute__((unused));
1075 struct trace_probe *tp = (struct trace_probe *)call->data;
1077 SHOW_FIELD(unsigned long, ip, "ip");
1078 SHOW_FIELD(int, nargs, "nargs");
1081 for (i = 0; i < tp->nr_args; i++) {
1082 sprintf(buf, "arg%d", i);
1083 SHOW_FIELD(unsigned long, args[i], buf);
1085 trace_seq_puts(s, "\n");
1087 return __probe_event_show_format(s, tp, "%lx:", "ip");
1090 static int kretprobe_event_show_format(struct ftrace_event_call *call,
1091 struct trace_seq *s)
1093 struct kretprobe_trace_entry field __attribute__((unused));
1096 struct trace_probe *tp = (struct trace_probe *)call->data;
1098 SHOW_FIELD(unsigned long, func, "func");
1099 SHOW_FIELD(unsigned long, ret_ip, "ret_ip");
1100 SHOW_FIELD(int, nargs, "nargs");
1103 for (i = 0; i < tp->nr_args; i++) {
1104 sprintf(buf, "arg%d", i);
1105 SHOW_FIELD(unsigned long, args[i], buf);
1107 trace_seq_puts(s, "\n");
1109 return __probe_event_show_format(s, tp, "%lx <- %lx:",
1113 #ifdef CONFIG_EVENT_PROFILE
1115 /* Kprobe profile handler */
1116 static __kprobes int kprobe_profile_func(struct kprobe *kp,
1117 struct pt_regs *regs)
1119 struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
1120 struct ftrace_event_call *call = &tp->call;
1121 struct kprobe_trace_entry *entry;
1123 unsigned long irq_flags;
1125 local_save_flags(irq_flags);
1126 pc = preempt_count();
1128 size = SIZEOF_KPROBE_TRACE_ENTRY(tp->nr_args);
1131 char raw_data[size];
1132 struct trace_entry *ent;
1134 *(u64 *)(&raw_data[size - sizeof(u64)]) = 0ULL;
1135 entry = (struct kprobe_trace_entry *)raw_data;
1138 tracing_generic_entry_update(ent, irq_flags, pc);
1139 ent->type = call->id;
1140 entry->nargs = tp->nr_args;
1141 entry->ip = (unsigned long)kp->addr;
1142 for (i = 0; i < tp->nr_args; i++)
1143 entry->args[i] = call_fetch(&tp->args[i], regs);
1144 perf_tpcounter_event(call->id, entry->ip, 1, entry, size);
1149 /* Kretprobe profile handler */
1150 static __kprobes int kretprobe_profile_func(struct kretprobe_instance *ri,
1151 struct pt_regs *regs)
1153 struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
1154 struct ftrace_event_call *call = &tp->call;
1155 struct kretprobe_trace_entry *entry;
1157 unsigned long irq_flags;
1159 local_save_flags(irq_flags);
1160 pc = preempt_count();
1162 size = SIZEOF_KRETPROBE_TRACE_ENTRY(tp->nr_args);
1165 char raw_data[size];
1166 struct trace_entry *ent;
1168 *(u64 *)(&raw_data[size - sizeof(u64)]) = 0ULL;
1169 entry = (struct kretprobe_trace_entry *)raw_data;
1172 tracing_generic_entry_update(ent, irq_flags, pc);
1173 ent->type = call->id;
1174 entry->nargs = tp->nr_args;
1175 entry->func = (unsigned long)tp->rp.kp.addr;
1176 entry->ret_ip = (unsigned long)ri->ret_addr;
1177 for (i = 0; i < tp->nr_args; i++)
1178 entry->args[i] = call_fetch(&tp->args[i], regs);
1179 perf_tpcounter_event(call->id, entry->ret_ip, 1, entry, size);
1184 static int probe_profile_enable(struct ftrace_event_call *call)
1186 struct trace_probe *tp = (struct trace_probe *)call->data;
1188 if (atomic_inc_return(&call->profile_count))
1191 if (probe_is_return(tp)) {
1192 tp->rp.handler = kretprobe_profile_func;
1193 return enable_kretprobe(&tp->rp);
1195 tp->rp.kp.pre_handler = kprobe_profile_func;
1196 return enable_kprobe(&tp->rp.kp);
1200 static void probe_profile_disable(struct ftrace_event_call *call)
1202 if (atomic_add_negative(-1, &call->profile_count))
1203 probe_event_disable(call);
1206 #endif /* CONFIG_EVENT_PROFILE */
1208 static int register_probe_event(struct trace_probe *tp)
1210 struct ftrace_event_call *call = &tp->call;
1213 /* Initialize ftrace_event_call */
1214 call->system = "kprobes";
1215 if (probe_is_return(tp)) {
1216 tp->event.trace = print_kretprobe_event;
1217 call->raw_init = probe_event_raw_init;
1218 call->show_format = kretprobe_event_show_format;
1219 call->define_fields = kretprobe_event_define_fields;
1221 tp->event.trace = print_kprobe_event;
1222 call->raw_init = probe_event_raw_init;
1223 call->show_format = kprobe_event_show_format;
1224 call->define_fields = kprobe_event_define_fields;
1226 call->event = &tp->event;
1227 call->id = register_ftrace_event(&tp->event);
1231 call->regfunc = probe_event_enable;
1232 call->unregfunc = probe_event_disable;
1234 #ifdef CONFIG_EVENT_PROFILE
1235 atomic_set(&call->profile_count, -1);
1236 call->profile_enable = probe_profile_enable;
1237 call->profile_disable = probe_profile_disable;
1240 ret = trace_add_event_call(call);
1242 pr_info("Failed to register kprobe event: %s\n", call->name);
1243 unregister_ftrace_event(&tp->event);
1248 static void unregister_probe_event(struct trace_probe *tp)
1250 /* tp->event is unregistered in trace_remove_event_call() */
1251 trace_remove_event_call(&tp->call);
1254 /* Make a debugfs interface for controling probe points */
1255 static __init int init_kprobe_trace(void)
1257 struct dentry *d_tracer;
1258 struct dentry *entry;
1260 d_tracer = tracing_init_dentry();
1264 entry = debugfs_create_file("kprobe_events", 0644, d_tracer,
1265 NULL, &kprobe_events_ops);
1267 /* Event list interface */
1269 pr_warning("Could not create debugfs "
1270 "'kprobe_events' entry\n");
1272 /* Profile interface */
1273 entry = debugfs_create_file("kprobe_profile", 0444, d_tracer,
1274 NULL, &kprobe_profile_ops);
1277 pr_warning("Could not create debugfs "
1278 "'kprobe_profile' entry\n");
1281 fs_initcall(init_kprobe_trace);
1284 #ifdef CONFIG_FTRACE_STARTUP_TEST
1286 static int kprobe_trace_selftest_target(int a1, int a2, int a3,
1287 int a4, int a5, int a6)
1289 return a1 + a2 + a3 + a4 + a5 + a6;
1292 static __init int kprobe_trace_self_tests_init(void)
1295 int (*target)(int, int, int, int, int, int);
1297 target = kprobe_trace_selftest_target;
1299 pr_info("Testing kprobe tracing: ");
1301 ret = command_trace_probe("p:testprobe kprobe_trace_selftest_target "
1302 "a1 a2 a3 a4 a5 a6");
1303 if (WARN_ON_ONCE(ret))
1304 pr_warning("error enabling function entry\n");
1306 ret = command_trace_probe("r:testprobe2 kprobe_trace_selftest_target "
1308 if (WARN_ON_ONCE(ret))
1309 pr_warning("error enabling function return\n");
1311 ret = target(1, 2, 3, 4, 5, 6);
1313 cleanup_all_probes();
1319 late_initcall(kprobe_trace_self_tests_init);