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>
23 #include "trace_probe.h"
25 #define KPROBE_EVENT_SYSTEM "kprobes"
28 * Kprobe event core functions
31 struct list_head list;
32 struct kretprobe rp; /* Use rp.kp for kprobe use */
34 unsigned int flags; /* For TP_FLAG_* */
35 const char *symbol; /* symbol name */
36 struct ftrace_event_class class;
37 struct ftrace_event_call call;
38 struct ftrace_event_file * __rcu *files;
39 ssize_t size; /* trace entry size */
41 struct probe_arg args[];
44 #define SIZEOF_TRACE_PROBE(n) \
45 (offsetof(struct trace_probe, args) + \
46 (sizeof(struct probe_arg) * (n)))
49 static __kprobes bool trace_probe_is_return(struct trace_probe *tp)
51 return tp->rp.handler != NULL;
54 static __kprobes const char *trace_probe_symbol(struct trace_probe *tp)
56 return tp->symbol ? tp->symbol : "unknown";
59 static __kprobes unsigned long trace_probe_offset(struct trace_probe *tp)
61 return tp->rp.kp.offset;
64 static __kprobes bool trace_probe_is_enabled(struct trace_probe *tp)
66 return !!(tp->flags & (TP_FLAG_TRACE | TP_FLAG_PROFILE));
69 static __kprobes bool trace_probe_is_registered(struct trace_probe *tp)
71 return !!(tp->flags & TP_FLAG_REGISTERED);
74 static __kprobes bool trace_probe_has_gone(struct trace_probe *tp)
76 return !!(kprobe_gone(&tp->rp.kp));
79 static __kprobes bool trace_probe_within_module(struct trace_probe *tp,
82 int len = strlen(mod->name);
83 const char *name = trace_probe_symbol(tp);
84 return strncmp(mod->name, name, len) == 0 && name[len] == ':';
87 static __kprobes bool trace_probe_is_on_module(struct trace_probe *tp)
89 return !!strchr(trace_probe_symbol(tp), ':');
92 static int register_probe_event(struct trace_probe *tp);
93 static int unregister_probe_event(struct trace_probe *tp);
95 static DEFINE_MUTEX(probe_lock);
96 static LIST_HEAD(probe_list);
98 static int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs);
99 static int kretprobe_dispatcher(struct kretprobe_instance *ri,
100 struct pt_regs *regs);
103 * Allocate new trace_probe and initialize it (including kprobes).
105 static struct trace_probe *alloc_trace_probe(const char *group,
110 int nargs, bool is_return)
112 struct trace_probe *tp;
115 tp = kzalloc(SIZEOF_TRACE_PROBE(nargs), GFP_KERNEL);
120 tp->symbol = kstrdup(symbol, GFP_KERNEL);
123 tp->rp.kp.symbol_name = tp->symbol;
124 tp->rp.kp.offset = offs;
126 tp->rp.kp.addr = addr;
129 tp->rp.handler = kretprobe_dispatcher;
131 tp->rp.kp.pre_handler = kprobe_dispatcher;
133 if (!event || !is_good_name(event)) {
138 tp->call.class = &tp->class;
139 tp->call.name = kstrdup(event, GFP_KERNEL);
143 if (!group || !is_good_name(group)) {
148 tp->class.system = kstrdup(group, GFP_KERNEL);
149 if (!tp->class.system)
152 INIT_LIST_HEAD(&tp->list);
155 kfree(tp->call.name);
161 static void free_trace_probe(struct trace_probe *tp)
165 for (i = 0; i < tp->nr_args; i++)
166 traceprobe_free_probe_arg(&tp->args[i]);
168 kfree(tp->call.class->system);
169 kfree(tp->call.name);
174 static struct trace_probe *find_trace_probe(const char *event,
177 struct trace_probe *tp;
179 list_for_each_entry(tp, &probe_list, list)
180 if (strcmp(tp->call.name, event) == 0 &&
181 strcmp(tp->call.class->system, group) == 0)
186 static int trace_probe_nr_files(struct trace_probe *tp)
188 struct ftrace_event_file **file;
192 * Since all tp->files updater is protected by probe_enable_lock,
193 * we don't need to lock an rcu_read_lock.
195 file = rcu_dereference_raw(tp->files);
203 static DEFINE_MUTEX(probe_enable_lock);
207 * if the file is NULL, enable "perf" handler, or enable "trace" handler.
210 enable_trace_probe(struct trace_probe *tp, struct ftrace_event_file *file)
214 mutex_lock(&probe_enable_lock);
217 struct ftrace_event_file **new, **old;
218 int n = trace_probe_nr_files(tp);
220 old = rcu_dereference_raw(tp->files);
221 /* 1 is for new one and 1 is for stopper */
222 new = kzalloc((n + 2) * sizeof(struct ftrace_event_file *),
228 memcpy(new, old, n * sizeof(struct ftrace_event_file *));
230 /* The last one keeps a NULL */
232 rcu_assign_pointer(tp->files, new);
233 tp->flags |= TP_FLAG_TRACE;
236 /* Make sure the probe is done with old files */
241 tp->flags |= TP_FLAG_PROFILE;
243 if (trace_probe_is_enabled(tp) && trace_probe_is_registered(tp) &&
244 !trace_probe_has_gone(tp)) {
245 if (trace_probe_is_return(tp))
246 ret = enable_kretprobe(&tp->rp);
248 ret = enable_kprobe(&tp->rp.kp);
252 mutex_unlock(&probe_enable_lock);
258 trace_probe_file_index(struct trace_probe *tp, struct ftrace_event_file *file)
260 struct ftrace_event_file **files;
264 * Since all tp->files updater is protected by probe_enable_lock,
265 * we don't need to lock an rcu_read_lock.
267 files = rcu_dereference_raw(tp->files);
269 for (i = 0; files[i]; i++)
270 if (files[i] == file)
278 * Disable trace_probe
279 * if the file is NULL, disable "perf" handler, or disable "trace" handler.
282 disable_trace_probe(struct trace_probe *tp, struct ftrace_event_file *file)
284 struct ftrace_event_file **old = NULL;
288 mutex_lock(&probe_enable_lock);
291 struct ftrace_event_file **new, **old;
292 int n = trace_probe_nr_files(tp);
295 old = rcu_dereference_raw(tp->files);
296 if (n == 0 || trace_probe_file_index(tp, file) < 0) {
301 if (n == 1) { /* Remove the last file */
302 tp->flags &= ~TP_FLAG_TRACE;
305 new = kzalloc(n * sizeof(struct ftrace_event_file *),
312 /* This copy & check loop copies the NULL stopper too */
313 for (i = 0, j = 0; j < n && i < n + 1; i++)
318 rcu_assign_pointer(tp->files, new);
321 tp->flags &= ~TP_FLAG_PROFILE;
323 if (!trace_probe_is_enabled(tp) && trace_probe_is_registered(tp)) {
324 if (trace_probe_is_return(tp))
325 disable_kretprobe(&tp->rp);
327 disable_kprobe(&tp->rp.kp);
332 mutex_unlock(&probe_enable_lock);
336 * Synchronize with kprobe_trace_func/kretprobe_trace_func
337 * to ensure disabled (all running handlers are finished).
338 * This is not only for kfree(), but also the caller,
339 * trace_remove_event_call() supposes it for releasing
340 * event_call related objects, which will be accessed in
341 * the kprobe_trace_func/kretprobe_trace_func.
344 kfree(old); /* Ignored if link == NULL */
350 /* Internal register function - just handle k*probes and flags */
351 static int __register_trace_probe(struct trace_probe *tp)
355 if (trace_probe_is_registered(tp))
358 for (i = 0; i < tp->nr_args; i++)
359 traceprobe_update_arg(&tp->args[i]);
361 /* Set/clear disabled flag according to tp->flag */
362 if (trace_probe_is_enabled(tp))
363 tp->rp.kp.flags &= ~KPROBE_FLAG_DISABLED;
365 tp->rp.kp.flags |= KPROBE_FLAG_DISABLED;
367 if (trace_probe_is_return(tp))
368 ret = register_kretprobe(&tp->rp);
370 ret = register_kprobe(&tp->rp.kp);
373 tp->flags |= TP_FLAG_REGISTERED;
375 pr_warning("Could not insert probe at %s+%lu: %d\n",
376 trace_probe_symbol(tp), trace_probe_offset(tp), ret);
377 if (ret == -ENOENT && trace_probe_is_on_module(tp)) {
378 pr_warning("This probe might be able to register after"
379 "target module is loaded. Continue.\n");
381 } else if (ret == -EILSEQ) {
382 pr_warning("Probing address(0x%p) is not an "
383 "instruction boundary.\n",
392 /* Internal unregister function - just handle k*probes and flags */
393 static void __unregister_trace_probe(struct trace_probe *tp)
395 if (trace_probe_is_registered(tp)) {
396 if (trace_probe_is_return(tp))
397 unregister_kretprobe(&tp->rp);
399 unregister_kprobe(&tp->rp.kp);
400 tp->flags &= ~TP_FLAG_REGISTERED;
401 /* Cleanup kprobe for reuse */
402 if (tp->rp.kp.symbol_name)
403 tp->rp.kp.addr = NULL;
407 /* Unregister a trace_probe and probe_event: call with locking probe_lock */
408 static int unregister_trace_probe(struct trace_probe *tp)
410 /* Enabled event can not be unregistered */
411 if (trace_probe_is_enabled(tp))
414 /* Will fail if probe is being used by ftrace or perf */
415 if (unregister_probe_event(tp))
418 __unregister_trace_probe(tp);
424 /* Register a trace_probe and probe_event */
425 static int register_trace_probe(struct trace_probe *tp)
427 struct trace_probe *old_tp;
430 mutex_lock(&probe_lock);
432 /* Delete old (same name) event if exist */
433 old_tp = find_trace_probe(tp->call.name, tp->call.class->system);
435 ret = unregister_trace_probe(old_tp);
438 free_trace_probe(old_tp);
441 /* Register new event */
442 ret = register_probe_event(tp);
444 pr_warning("Failed to register probe event(%d)\n", ret);
448 /* Register k*probe */
449 ret = __register_trace_probe(tp);
451 unregister_probe_event(tp);
453 list_add_tail(&tp->list, &probe_list);
456 mutex_unlock(&probe_lock);
460 /* Module notifier call back, checking event on the module */
461 static int trace_probe_module_callback(struct notifier_block *nb,
462 unsigned long val, void *data)
464 struct module *mod = data;
465 struct trace_probe *tp;
468 if (val != MODULE_STATE_COMING)
471 /* Update probes on coming module */
472 mutex_lock(&probe_lock);
473 list_for_each_entry(tp, &probe_list, list) {
474 if (trace_probe_within_module(tp, mod)) {
475 /* Don't need to check busy - this should have gone. */
476 __unregister_trace_probe(tp);
477 ret = __register_trace_probe(tp);
479 pr_warning("Failed to re-register probe %s on"
481 tp->call.name, mod->name, ret);
484 mutex_unlock(&probe_lock);
489 static struct notifier_block trace_probe_module_nb = {
490 .notifier_call = trace_probe_module_callback,
491 .priority = 1 /* Invoked after kprobe module callback */
494 static int create_trace_probe(int argc, char **argv)
498 * - Add kprobe: p[:[GRP/]EVENT] [MOD:]KSYM[+OFFS]|KADDR [FETCHARGS]
499 * - Add kretprobe: r[:[GRP/]EVENT] [MOD:]KSYM[+0] [FETCHARGS]
501 * $retval : fetch return value
502 * $stack : fetch stack address
503 * $stackN : fetch Nth of stack (N:0-)
504 * @ADDR : fetch memory at ADDR (ADDR should be in kernel)
505 * @SYM[+|-offs] : fetch memory at SYM +|- offs (SYM is a data symbol)
506 * %REG : fetch register REG
507 * Dereferencing memory fetch:
508 * +|-offs(ARG) : fetch memory at ARG +|- offs address.
509 * Alias name of args:
510 * NAME=FETCHARG : set NAME as alias of FETCHARG.
512 * FETCHARG:TYPE : use TYPE instead of unsigned long.
514 struct trace_probe *tp;
516 bool is_return = false, is_delete = false;
517 char *symbol = NULL, *event = NULL, *group = NULL;
519 unsigned long offset = 0;
521 char buf[MAX_EVENT_NAME_LEN];
523 /* argc must be >= 1 */
524 if (argv[0][0] == 'p')
526 else if (argv[0][0] == 'r')
528 else if (argv[0][0] == '-')
531 pr_info("Probe definition must be started with 'p', 'r' or"
536 if (argv[0][1] == ':') {
538 if (strchr(event, '/')) {
540 event = strchr(group, '/') + 1;
542 if (strlen(group) == 0) {
543 pr_info("Group name is not specified\n");
547 if (strlen(event) == 0) {
548 pr_info("Event name is not specified\n");
553 group = KPROBE_EVENT_SYSTEM;
557 pr_info("Delete command needs an event name.\n");
560 mutex_lock(&probe_lock);
561 tp = find_trace_probe(event, group);
563 mutex_unlock(&probe_lock);
564 pr_info("Event %s/%s doesn't exist.\n", group, event);
567 /* delete an event */
568 ret = unregister_trace_probe(tp);
570 free_trace_probe(tp);
571 mutex_unlock(&probe_lock);
576 pr_info("Probe point is not specified.\n");
579 if (isdigit(argv[1][0])) {
581 pr_info("Return probe point must be a symbol.\n");
584 /* an address specified */
585 ret = kstrtoul(&argv[1][0], 0, (unsigned long *)&addr);
587 pr_info("Failed to parse address.\n");
591 /* a symbol specified */
593 /* TODO: support .init module functions */
594 ret = traceprobe_split_symbol_offset(symbol, &offset);
596 pr_info("Failed to parse symbol.\n");
599 if (offset && is_return) {
600 pr_info("Return probe must be used without offset.\n");
604 argc -= 2; argv += 2;
608 /* Make a new event name */
610 snprintf(buf, MAX_EVENT_NAME_LEN, "%c_%s_%ld",
611 is_return ? 'r' : 'p', symbol, offset);
613 snprintf(buf, MAX_EVENT_NAME_LEN, "%c_0x%p",
614 is_return ? 'r' : 'p', addr);
617 tp = alloc_trace_probe(group, event, addr, symbol, offset, argc,
620 pr_info("Failed to allocate trace_probe.(%d)\n",
625 /* parse arguments */
627 for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
628 /* Increment count for freeing args in error case */
631 /* Parse argument name */
632 arg = strchr(argv[i], '=');
635 tp->args[i].name = kstrdup(argv[i], GFP_KERNEL);
638 /* If argument name is omitted, set "argN" */
639 snprintf(buf, MAX_EVENT_NAME_LEN, "arg%d", i + 1);
640 tp->args[i].name = kstrdup(buf, GFP_KERNEL);
643 if (!tp->args[i].name) {
644 pr_info("Failed to allocate argument[%d] name.\n", i);
649 if (!is_good_name(tp->args[i].name)) {
650 pr_info("Invalid argument[%d] name: %s\n",
651 i, tp->args[i].name);
656 if (traceprobe_conflict_field_name(tp->args[i].name,
658 pr_info("Argument[%d] name '%s' conflicts with "
659 "another field.\n", i, argv[i]);
664 /* Parse fetch argument */
665 ret = traceprobe_parse_probe_arg(arg, &tp->size, &tp->args[i],
668 pr_info("Parse error at argument[%d]. (%d)\n", i, ret);
673 ret = register_trace_probe(tp);
679 free_trace_probe(tp);
683 static int release_all_trace_probes(void)
685 struct trace_probe *tp;
688 mutex_lock(&probe_lock);
689 /* Ensure no probe is in use. */
690 list_for_each_entry(tp, &probe_list, list)
691 if (trace_probe_is_enabled(tp)) {
695 /* TODO: Use batch unregistration */
696 while (!list_empty(&probe_list)) {
697 tp = list_entry(probe_list.next, struct trace_probe, list);
698 ret = unregister_trace_probe(tp);
701 free_trace_probe(tp);
705 mutex_unlock(&probe_lock);
710 /* Probes listing interfaces */
711 static void *probes_seq_start(struct seq_file *m, loff_t *pos)
713 mutex_lock(&probe_lock);
714 return seq_list_start(&probe_list, *pos);
717 static void *probes_seq_next(struct seq_file *m, void *v, loff_t *pos)
719 return seq_list_next(v, &probe_list, pos);
722 static void probes_seq_stop(struct seq_file *m, void *v)
724 mutex_unlock(&probe_lock);
727 static int probes_seq_show(struct seq_file *m, void *v)
729 struct trace_probe *tp = v;
732 seq_printf(m, "%c", trace_probe_is_return(tp) ? 'r' : 'p');
733 seq_printf(m, ":%s/%s", tp->call.class->system, tp->call.name);
736 seq_printf(m, " 0x%p", tp->rp.kp.addr);
737 else if (tp->rp.kp.offset)
738 seq_printf(m, " %s+%u", trace_probe_symbol(tp),
741 seq_printf(m, " %s", trace_probe_symbol(tp));
743 for (i = 0; i < tp->nr_args; i++)
744 seq_printf(m, " %s=%s", tp->args[i].name, tp->args[i].comm);
750 static const struct seq_operations probes_seq_op = {
751 .start = probes_seq_start,
752 .next = probes_seq_next,
753 .stop = probes_seq_stop,
754 .show = probes_seq_show
757 static int probes_open(struct inode *inode, struct file *file)
761 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
762 ret = release_all_trace_probes();
767 return seq_open(file, &probes_seq_op);
770 static ssize_t probes_write(struct file *file, const char __user *buffer,
771 size_t count, loff_t *ppos)
773 return traceprobe_probes_write(file, buffer, count, ppos,
777 static const struct file_operations kprobe_events_ops = {
778 .owner = THIS_MODULE,
782 .release = seq_release,
783 .write = probes_write,
786 /* Probes profiling interfaces */
787 static int probes_profile_seq_show(struct seq_file *m, void *v)
789 struct trace_probe *tp = v;
791 seq_printf(m, " %-44s %15lu %15lu\n", tp->call.name, tp->nhit,
797 static const struct seq_operations profile_seq_op = {
798 .start = probes_seq_start,
799 .next = probes_seq_next,
800 .stop = probes_seq_stop,
801 .show = probes_profile_seq_show
804 static int profile_open(struct inode *inode, struct file *file)
806 return seq_open(file, &profile_seq_op);
809 static const struct file_operations kprobe_profile_ops = {
810 .owner = THIS_MODULE,
811 .open = profile_open,
814 .release = seq_release,
817 /* Sum up total data length for dynamic arraies (strings) */
818 static __kprobes int __get_data_size(struct trace_probe *tp,
819 struct pt_regs *regs)
824 for (i = 0; i < tp->nr_args; i++)
825 if (unlikely(tp->args[i].fetch_size.fn)) {
826 call_fetch(&tp->args[i].fetch_size, regs, &len);
833 /* Store the value of each argument */
834 static __kprobes void store_trace_args(int ent_size, struct trace_probe *tp,
835 struct pt_regs *regs,
836 u8 *data, int maxlen)
840 u32 *dl; /* Data (relative) location */
842 for (i = 0; i < tp->nr_args; i++) {
843 if (unlikely(tp->args[i].fetch_size.fn)) {
845 * First, we set the relative location and
846 * maximum data length to *dl
848 dl = (u32 *)(data + tp->args[i].offset);
849 *dl = make_data_rloc(maxlen, end - tp->args[i].offset);
850 /* Then try to fetch string or dynamic array data */
851 call_fetch(&tp->args[i].fetch, regs, dl);
852 /* Reduce maximum length */
853 end += get_rloc_len(*dl);
854 maxlen -= get_rloc_len(*dl);
855 /* Trick here, convert data_rloc to data_loc */
856 *dl = convert_rloc_to_loc(*dl,
857 ent_size + tp->args[i].offset);
859 /* Just fetching data normally */
860 call_fetch(&tp->args[i].fetch, regs,
861 data + tp->args[i].offset);
866 static __kprobes void
867 __kprobe_trace_func(struct trace_probe *tp, struct pt_regs *regs,
868 struct ftrace_event_file *ftrace_file)
870 struct kprobe_trace_entry_head *entry;
871 struct ring_buffer_event *event;
872 struct ring_buffer *buffer;
874 unsigned long irq_flags;
875 struct ftrace_event_call *call = &tp->call;
877 WARN_ON(call != ftrace_file->event_call);
879 if (test_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &ftrace_file->flags))
882 local_save_flags(irq_flags);
883 pc = preempt_count();
885 dsize = __get_data_size(tp, regs);
886 size = sizeof(*entry) + tp->size + dsize;
888 event = trace_event_buffer_lock_reserve(&buffer, ftrace_file,
890 size, irq_flags, pc);
894 entry = ring_buffer_event_data(event);
895 entry->ip = (unsigned long)tp->rp.kp.addr;
896 store_trace_args(sizeof(*entry), tp, regs, (u8 *)&entry[1], dsize);
898 if (!filter_current_check_discard(buffer, call, entry, event))
899 trace_buffer_unlock_commit_regs(buffer, event,
900 irq_flags, pc, regs);
903 static __kprobes void
904 kprobe_trace_func(struct trace_probe *tp, struct pt_regs *regs)
907 * Note: preempt is already disabled around the kprobe handler.
908 * However, we still need an smp_read_barrier_depends() corresponding
909 * to smp_wmb() in rcu_assign_pointer() to access the pointer.
911 struct ftrace_event_file **file = rcu_dereference_raw(tp->files);
917 __kprobe_trace_func(tp, regs, *file);
922 /* Kretprobe handler */
923 static __kprobes void
924 __kretprobe_trace_func(struct trace_probe *tp, struct kretprobe_instance *ri,
925 struct pt_regs *regs,
926 struct ftrace_event_file *ftrace_file)
928 struct kretprobe_trace_entry_head *entry;
929 struct ring_buffer_event *event;
930 struct ring_buffer *buffer;
932 unsigned long irq_flags;
933 struct ftrace_event_call *call = &tp->call;
935 WARN_ON(call != ftrace_file->event_call);
937 if (test_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &ftrace_file->flags))
940 local_save_flags(irq_flags);
941 pc = preempt_count();
943 dsize = __get_data_size(tp, regs);
944 size = sizeof(*entry) + tp->size + dsize;
946 event = trace_event_buffer_lock_reserve(&buffer, ftrace_file,
948 size, irq_flags, pc);
952 entry = ring_buffer_event_data(event);
953 entry->func = (unsigned long)tp->rp.kp.addr;
954 entry->ret_ip = (unsigned long)ri->ret_addr;
955 store_trace_args(sizeof(*entry), tp, regs, (u8 *)&entry[1], dsize);
957 if (!filter_current_check_discard(buffer, call, entry, event))
958 trace_buffer_unlock_commit_regs(buffer, event,
959 irq_flags, pc, regs);
962 static __kprobes void
963 kretprobe_trace_func(struct trace_probe *tp, struct kretprobe_instance *ri,
964 struct pt_regs *regs)
967 * Note: preempt is already disabled around the kprobe handler.
968 * However, we still need an smp_read_barrier_depends() corresponding
969 * to smp_wmb() in rcu_assign_pointer() to access the pointer.
971 struct ftrace_event_file **file = rcu_dereference_raw(tp->files);
977 __kretprobe_trace_func(tp, ri, regs, *file);
982 /* Event entry printers */
983 static enum print_line_t
984 print_kprobe_event(struct trace_iterator *iter, int flags,
985 struct trace_event *event)
987 struct kprobe_trace_entry_head *field;
988 struct trace_seq *s = &iter->seq;
989 struct trace_probe *tp;
993 field = (struct kprobe_trace_entry_head *)iter->ent;
994 tp = container_of(event, struct trace_probe, call.event);
996 if (!trace_seq_printf(s, "%s: (", tp->call.name))
999 if (!seq_print_ip_sym(s, field->ip, flags | TRACE_ITER_SYM_OFFSET))
1002 if (!trace_seq_puts(s, ")"))
1005 data = (u8 *)&field[1];
1006 for (i = 0; i < tp->nr_args; i++)
1007 if (!tp->args[i].type->print(s, tp->args[i].name,
1008 data + tp->args[i].offset, field))
1011 if (!trace_seq_puts(s, "\n"))
1014 return TRACE_TYPE_HANDLED;
1016 return TRACE_TYPE_PARTIAL_LINE;
1019 static enum print_line_t
1020 print_kretprobe_event(struct trace_iterator *iter, int flags,
1021 struct trace_event *event)
1023 struct kretprobe_trace_entry_head *field;
1024 struct trace_seq *s = &iter->seq;
1025 struct trace_probe *tp;
1029 field = (struct kretprobe_trace_entry_head *)iter->ent;
1030 tp = container_of(event, struct trace_probe, call.event);
1032 if (!trace_seq_printf(s, "%s: (", tp->call.name))
1035 if (!seq_print_ip_sym(s, field->ret_ip, flags | TRACE_ITER_SYM_OFFSET))
1038 if (!trace_seq_puts(s, " <- "))
1041 if (!seq_print_ip_sym(s, field->func, flags & ~TRACE_ITER_SYM_OFFSET))
1044 if (!trace_seq_puts(s, ")"))
1047 data = (u8 *)&field[1];
1048 for (i = 0; i < tp->nr_args; i++)
1049 if (!tp->args[i].type->print(s, tp->args[i].name,
1050 data + tp->args[i].offset, field))
1053 if (!trace_seq_puts(s, "\n"))
1056 return TRACE_TYPE_HANDLED;
1058 return TRACE_TYPE_PARTIAL_LINE;
1062 static int kprobe_event_define_fields(struct ftrace_event_call *event_call)
1065 struct kprobe_trace_entry_head field;
1066 struct trace_probe *tp = (struct trace_probe *)event_call->data;
1068 DEFINE_FIELD(unsigned long, ip, FIELD_STRING_IP, 0);
1069 /* Set argument names as fields */
1070 for (i = 0; i < tp->nr_args; i++) {
1071 ret = trace_define_field(event_call, tp->args[i].type->fmttype,
1073 sizeof(field) + tp->args[i].offset,
1074 tp->args[i].type->size,
1075 tp->args[i].type->is_signed,
1083 static int kretprobe_event_define_fields(struct ftrace_event_call *event_call)
1086 struct kretprobe_trace_entry_head field;
1087 struct trace_probe *tp = (struct trace_probe *)event_call->data;
1089 DEFINE_FIELD(unsigned long, func, FIELD_STRING_FUNC, 0);
1090 DEFINE_FIELD(unsigned long, ret_ip, FIELD_STRING_RETIP, 0);
1091 /* Set argument names as fields */
1092 for (i = 0; i < tp->nr_args; i++) {
1093 ret = trace_define_field(event_call, tp->args[i].type->fmttype,
1095 sizeof(field) + tp->args[i].offset,
1096 tp->args[i].type->size,
1097 tp->args[i].type->is_signed,
1105 static int __set_print_fmt(struct trace_probe *tp, char *buf, int len)
1110 const char *fmt, *arg;
1112 if (!trace_probe_is_return(tp)) {
1114 arg = "REC->" FIELD_STRING_IP;
1116 fmt = "(%lx <- %lx)";
1117 arg = "REC->" FIELD_STRING_FUNC ", REC->" FIELD_STRING_RETIP;
1120 /* When len=0, we just calculate the needed length */
1121 #define LEN_OR_ZERO (len ? len - pos : 0)
1123 pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", fmt);
1125 for (i = 0; i < tp->nr_args; i++) {
1126 pos += snprintf(buf + pos, LEN_OR_ZERO, " %s=%s",
1127 tp->args[i].name, tp->args[i].type->fmt);
1130 pos += snprintf(buf + pos, LEN_OR_ZERO, "\", %s", arg);
1132 for (i = 0; i < tp->nr_args; i++) {
1133 if (strcmp(tp->args[i].type->name, "string") == 0)
1134 pos += snprintf(buf + pos, LEN_OR_ZERO,
1138 pos += snprintf(buf + pos, LEN_OR_ZERO, ", REC->%s",
1144 /* return the length of print_fmt */
1148 static int set_print_fmt(struct trace_probe *tp)
1153 /* First: called with 0 length to calculate the needed length */
1154 len = __set_print_fmt(tp, NULL, 0);
1155 print_fmt = kmalloc(len + 1, GFP_KERNEL);
1159 /* Second: actually write the @print_fmt */
1160 __set_print_fmt(tp, print_fmt, len + 1);
1161 tp->call.print_fmt = print_fmt;
1166 #ifdef CONFIG_PERF_EVENTS
1168 /* Kprobe profile handler */
1169 static __kprobes void
1170 kprobe_perf_func(struct trace_probe *tp, struct pt_regs *regs)
1172 struct ftrace_event_call *call = &tp->call;
1173 struct kprobe_trace_entry_head *entry;
1174 struct hlist_head *head;
1175 int size, __size, dsize;
1178 dsize = __get_data_size(tp, regs);
1179 __size = sizeof(*entry) + tp->size + dsize;
1180 size = ALIGN(__size + sizeof(u32), sizeof(u64));
1181 size -= sizeof(u32);
1182 if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE,
1183 "profile buffer not large enough"))
1186 entry = perf_trace_buf_prepare(size, call->event.type, regs, &rctx);
1190 entry->ip = (unsigned long)tp->rp.kp.addr;
1191 memset(&entry[1], 0, dsize);
1192 store_trace_args(sizeof(*entry), tp, regs, (u8 *)&entry[1], dsize);
1194 head = this_cpu_ptr(call->perf_events);
1195 perf_trace_buf_submit(entry, size, rctx,
1196 entry->ip, 1, regs, head, NULL);
1199 /* Kretprobe profile handler */
1200 static __kprobes void
1201 kretprobe_perf_func(struct trace_probe *tp, struct kretprobe_instance *ri,
1202 struct pt_regs *regs)
1204 struct ftrace_event_call *call = &tp->call;
1205 struct kretprobe_trace_entry_head *entry;
1206 struct hlist_head *head;
1207 int size, __size, dsize;
1210 dsize = __get_data_size(tp, regs);
1211 __size = sizeof(*entry) + tp->size + dsize;
1212 size = ALIGN(__size + sizeof(u32), sizeof(u64));
1213 size -= sizeof(u32);
1214 if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE,
1215 "profile buffer not large enough"))
1218 entry = perf_trace_buf_prepare(size, call->event.type, regs, &rctx);
1222 entry->func = (unsigned long)tp->rp.kp.addr;
1223 entry->ret_ip = (unsigned long)ri->ret_addr;
1224 store_trace_args(sizeof(*entry), tp, regs, (u8 *)&entry[1], dsize);
1226 head = this_cpu_ptr(call->perf_events);
1227 perf_trace_buf_submit(entry, size, rctx,
1228 entry->ret_ip, 1, regs, head, NULL);
1230 #endif /* CONFIG_PERF_EVENTS */
1233 int kprobe_register(struct ftrace_event_call *event,
1234 enum trace_reg type, void *data)
1236 struct trace_probe *tp = (struct trace_probe *)event->data;
1237 struct ftrace_event_file *file = data;
1240 case TRACE_REG_REGISTER:
1241 return enable_trace_probe(tp, file);
1242 case TRACE_REG_UNREGISTER:
1243 return disable_trace_probe(tp, file);
1245 #ifdef CONFIG_PERF_EVENTS
1246 case TRACE_REG_PERF_REGISTER:
1247 return enable_trace_probe(tp, NULL);
1248 case TRACE_REG_PERF_UNREGISTER:
1249 return disable_trace_probe(tp, NULL);
1250 case TRACE_REG_PERF_OPEN:
1251 case TRACE_REG_PERF_CLOSE:
1252 case TRACE_REG_PERF_ADD:
1253 case TRACE_REG_PERF_DEL:
1261 int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs)
1263 struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
1267 if (tp->flags & TP_FLAG_TRACE)
1268 kprobe_trace_func(tp, regs);
1269 #ifdef CONFIG_PERF_EVENTS
1270 if (tp->flags & TP_FLAG_PROFILE)
1271 kprobe_perf_func(tp, regs);
1273 return 0; /* We don't tweek kernel, so just return 0 */
1277 int kretprobe_dispatcher(struct kretprobe_instance *ri, struct pt_regs *regs)
1279 struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
1283 if (tp->flags & TP_FLAG_TRACE)
1284 kretprobe_trace_func(tp, ri, regs);
1285 #ifdef CONFIG_PERF_EVENTS
1286 if (tp->flags & TP_FLAG_PROFILE)
1287 kretprobe_perf_func(tp, ri, regs);
1289 return 0; /* We don't tweek kernel, so just return 0 */
1292 static struct trace_event_functions kretprobe_funcs = {
1293 .trace = print_kretprobe_event
1296 static struct trace_event_functions kprobe_funcs = {
1297 .trace = print_kprobe_event
1300 static int register_probe_event(struct trace_probe *tp)
1302 struct ftrace_event_call *call = &tp->call;
1305 /* Initialize ftrace_event_call */
1306 INIT_LIST_HEAD(&call->class->fields);
1307 if (trace_probe_is_return(tp)) {
1308 call->event.funcs = &kretprobe_funcs;
1309 call->class->define_fields = kretprobe_event_define_fields;
1311 call->event.funcs = &kprobe_funcs;
1312 call->class->define_fields = kprobe_event_define_fields;
1314 if (set_print_fmt(tp) < 0)
1316 ret = register_ftrace_event(&call->event);
1318 kfree(call->print_fmt);
1322 call->class->reg = kprobe_register;
1324 ret = trace_add_event_call(call);
1326 pr_info("Failed to register kprobe event: %s\n", call->name);
1327 kfree(call->print_fmt);
1328 unregister_ftrace_event(&call->event);
1333 static int unregister_probe_event(struct trace_probe *tp)
1337 /* tp->event is unregistered in trace_remove_event_call() */
1338 ret = trace_remove_event_call(&tp->call);
1340 kfree(tp->call.print_fmt);
1344 /* Make a debugfs interface for controlling probe points */
1345 static __init int init_kprobe_trace(void)
1347 struct dentry *d_tracer;
1348 struct dentry *entry;
1350 if (register_module_notifier(&trace_probe_module_nb))
1353 d_tracer = tracing_init_dentry();
1357 entry = debugfs_create_file("kprobe_events", 0644, d_tracer,
1358 NULL, &kprobe_events_ops);
1360 /* Event list interface */
1362 pr_warning("Could not create debugfs "
1363 "'kprobe_events' entry\n");
1365 /* Profile interface */
1366 entry = debugfs_create_file("kprobe_profile", 0444, d_tracer,
1367 NULL, &kprobe_profile_ops);
1370 pr_warning("Could not create debugfs "
1371 "'kprobe_profile' entry\n");
1374 fs_initcall(init_kprobe_trace);
1377 #ifdef CONFIG_FTRACE_STARTUP_TEST
1380 * The "__used" keeps gcc from removing the function symbol
1381 * from the kallsyms table.
1383 static __used int kprobe_trace_selftest_target(int a1, int a2, int a3,
1384 int a4, int a5, int a6)
1386 return a1 + a2 + a3 + a4 + a5 + a6;
1389 static struct ftrace_event_file *
1390 find_trace_probe_file(struct trace_probe *tp, struct trace_array *tr)
1392 struct ftrace_event_file *file;
1394 list_for_each_entry(file, &tr->events, list)
1395 if (file->event_call == &tp->call)
1401 static __init int kprobe_trace_self_tests_init(void)
1404 int (*target)(int, int, int, int, int, int);
1405 struct trace_probe *tp;
1406 struct ftrace_event_file *file;
1408 target = kprobe_trace_selftest_target;
1410 pr_info("Testing kprobe tracing: ");
1412 ret = traceprobe_command("p:testprobe kprobe_trace_selftest_target "
1413 "$stack $stack0 +0($stack)",
1414 create_trace_probe);
1415 if (WARN_ON_ONCE(ret)) {
1416 pr_warn("error on probing function entry.\n");
1419 /* Enable trace point */
1420 tp = find_trace_probe("testprobe", KPROBE_EVENT_SYSTEM);
1421 if (WARN_ON_ONCE(tp == NULL)) {
1422 pr_warn("error on getting new probe.\n");
1425 file = find_trace_probe_file(tp, top_trace_array());
1426 if (WARN_ON_ONCE(file == NULL)) {
1427 pr_warn("error on getting probe file.\n");
1430 enable_trace_probe(tp, file);
1434 ret = traceprobe_command("r:testprobe2 kprobe_trace_selftest_target "
1435 "$retval", create_trace_probe);
1436 if (WARN_ON_ONCE(ret)) {
1437 pr_warn("error on probing function return.\n");
1440 /* Enable trace point */
1441 tp = find_trace_probe("testprobe2", KPROBE_EVENT_SYSTEM);
1442 if (WARN_ON_ONCE(tp == NULL)) {
1443 pr_warn("error on getting 2nd new probe.\n");
1446 file = find_trace_probe_file(tp, top_trace_array());
1447 if (WARN_ON_ONCE(file == NULL)) {
1448 pr_warn("error on getting probe file.\n");
1451 enable_trace_probe(tp, file);
1458 ret = target(1, 2, 3, 4, 5, 6);
1460 /* Disable trace points before removing it */
1461 tp = find_trace_probe("testprobe", KPROBE_EVENT_SYSTEM);
1462 if (WARN_ON_ONCE(tp == NULL)) {
1463 pr_warn("error on getting test probe.\n");
1466 file = find_trace_probe_file(tp, top_trace_array());
1467 if (WARN_ON_ONCE(file == NULL)) {
1468 pr_warn("error on getting probe file.\n");
1471 disable_trace_probe(tp, file);
1474 tp = find_trace_probe("testprobe2", KPROBE_EVENT_SYSTEM);
1475 if (WARN_ON_ONCE(tp == NULL)) {
1476 pr_warn("error on getting 2nd test probe.\n");
1479 file = find_trace_probe_file(tp, top_trace_array());
1480 if (WARN_ON_ONCE(file == NULL)) {
1481 pr_warn("error on getting probe file.\n");
1484 disable_trace_probe(tp, file);
1487 ret = traceprobe_command("-:testprobe", create_trace_probe);
1488 if (WARN_ON_ONCE(ret)) {
1489 pr_warn("error on deleting a probe.\n");
1493 ret = traceprobe_command("-:testprobe2", create_trace_probe);
1494 if (WARN_ON_ONCE(ret)) {
1495 pr_warn("error on deleting a probe.\n");
1500 release_all_trace_probes();
1502 pr_cont("NG: Some tests are failed. Please check them.\n");
1508 late_initcall(kprobe_trace_self_tests_init);