2 * uprobes-based tracing events
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 * Copyright (C) IBM Corporation, 2010-2012
18 * Author: Srikar Dronamraju <srikar@linux.vnet.ibm.com>
21 #include <linux/module.h>
22 #include <linux/uaccess.h>
23 #include <linux/uprobes.h>
24 #include <linux/namei.h>
25 #include <linux/string.h>
27 #include "trace_probe.h"
29 #define UPROBE_EVENT_SYSTEM "uprobes"
31 struct trace_uprobe_filter {
34 struct list_head perf_events;
38 * uprobe event core functions
41 struct list_head list;
42 struct ftrace_event_class class;
43 struct ftrace_event_call call;
44 struct trace_uprobe_filter filter;
45 struct uprobe_consumer consumer;
50 unsigned int flags; /* For TP_FLAG_* */
51 ssize_t size; /* trace entry size */
53 struct probe_arg args[];
56 #define SIZEOF_TRACE_UPROBE(n) \
57 (offsetof(struct trace_uprobe, args) + \
58 (sizeof(struct probe_arg) * (n)))
60 static int register_uprobe_event(struct trace_uprobe *tu);
61 static void unregister_uprobe_event(struct trace_uprobe *tu);
63 static DEFINE_MUTEX(uprobe_lock);
64 static LIST_HEAD(uprobe_list);
66 static int uprobe_dispatcher(struct uprobe_consumer *con, struct pt_regs *regs);
68 static inline void init_trace_uprobe_filter(struct trace_uprobe_filter *filter)
70 rwlock_init(&filter->rwlock);
71 filter->nr_systemwide = 0;
72 INIT_LIST_HEAD(&filter->perf_events);
75 static inline bool uprobe_filter_is_empty(struct trace_uprobe_filter *filter)
77 return !filter->nr_systemwide && list_empty(&filter->perf_events);
81 * Allocate new trace_uprobe and initialize it (including uprobes).
83 static struct trace_uprobe *
84 alloc_trace_uprobe(const char *group, const char *event, int nargs)
86 struct trace_uprobe *tu;
88 if (!event || !is_good_name(event))
89 return ERR_PTR(-EINVAL);
91 if (!group || !is_good_name(group))
92 return ERR_PTR(-EINVAL);
94 tu = kzalloc(SIZEOF_TRACE_UPROBE(nargs), GFP_KERNEL);
96 return ERR_PTR(-ENOMEM);
98 tu->call.class = &tu->class;
99 tu->call.name = kstrdup(event, GFP_KERNEL);
103 tu->class.system = kstrdup(group, GFP_KERNEL);
104 if (!tu->class.system)
107 INIT_LIST_HEAD(&tu->list);
108 tu->consumer.handler = uprobe_dispatcher;
109 init_trace_uprobe_filter(&tu->filter);
113 kfree(tu->call.name);
116 return ERR_PTR(-ENOMEM);
119 static void free_trace_uprobe(struct trace_uprobe *tu)
123 for (i = 0; i < tu->nr_args; i++)
124 traceprobe_free_probe_arg(&tu->args[i]);
127 kfree(tu->call.class->system);
128 kfree(tu->call.name);
133 static struct trace_uprobe *find_probe_event(const char *event, const char *group)
135 struct trace_uprobe *tu;
137 list_for_each_entry(tu, &uprobe_list, list)
138 if (strcmp(tu->call.name, event) == 0 &&
139 strcmp(tu->call.class->system, group) == 0)
145 /* Unregister a trace_uprobe and probe_event: call with locking uprobe_lock */
146 static void unregister_trace_uprobe(struct trace_uprobe *tu)
149 unregister_uprobe_event(tu);
150 free_trace_uprobe(tu);
153 /* Register a trace_uprobe and probe_event */
154 static int register_trace_uprobe(struct trace_uprobe *tu)
156 struct trace_uprobe *old_tp;
159 mutex_lock(&uprobe_lock);
161 /* register as an event */
162 old_tp = find_probe_event(tu->call.name, tu->call.class->system);
164 /* delete old event */
165 unregister_trace_uprobe(old_tp);
167 ret = register_uprobe_event(tu);
169 pr_warning("Failed to register probe event(%d)\n", ret);
173 list_add_tail(&tu->list, &uprobe_list);
176 mutex_unlock(&uprobe_lock);
183 * - Add uprobe: p[:[GRP/]EVENT] PATH:SYMBOL[+offs] [FETCHARGS]
185 * - Remove uprobe: -:[GRP/]EVENT
187 static int create_trace_uprobe(int argc, char **argv)
189 struct trace_uprobe *tu;
191 char *arg, *event, *group, *filename;
192 char buf[MAX_EVENT_NAME_LEN];
194 unsigned long offset;
204 /* argc must be >= 1 */
205 if (argv[0][0] == '-')
207 else if (argv[0][0] != 'p') {
208 pr_info("Probe definition must be started with 'p' or '-'.\n");
212 if (argv[0][1] == ':') {
214 arg = strchr(event, '/');
221 if (strlen(group) == 0) {
222 pr_info("Group name is not specified\n");
226 if (strlen(event) == 0) {
227 pr_info("Event name is not specified\n");
232 group = UPROBE_EVENT_SYSTEM;
236 pr_info("Delete command needs an event name.\n");
239 mutex_lock(&uprobe_lock);
240 tu = find_probe_event(event, group);
243 mutex_unlock(&uprobe_lock);
244 pr_info("Event %s/%s doesn't exist.\n", group, event);
247 /* delete an event */
248 unregister_trace_uprobe(tu);
249 mutex_unlock(&uprobe_lock);
254 pr_info("Probe point is not specified.\n");
257 if (isdigit(argv[1][0])) {
258 pr_info("probe point must be have a filename.\n");
261 arg = strchr(argv[1], ':');
263 goto fail_address_parse;
267 ret = kern_path(filename, LOOKUP_FOLLOW, &path);
269 goto fail_address_parse;
271 inode = igrab(path.dentry->d_inode);
274 if (!inode || !S_ISREG(inode->i_mode)) {
276 goto fail_address_parse;
279 ret = kstrtoul(arg, 0, &offset);
281 goto fail_address_parse;
291 tail = kstrdup(kbasename(filename), GFP_KERNEL);
294 goto fail_address_parse;
297 ptr = strpbrk(tail, ".-_");
301 snprintf(buf, MAX_EVENT_NAME_LEN, "%c_%s_0x%lx", 'p', tail, offset);
306 tu = alloc_trace_uprobe(group, event, argc);
308 pr_info("Failed to allocate trace_uprobe.(%d)\n", (int)PTR_ERR(tu));
310 goto fail_address_parse;
314 tu->filename = kstrdup(filename, GFP_KERNEL);
317 pr_info("Failed to allocate filename.\n");
322 /* parse arguments */
324 for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
325 /* Increment count for freeing args in error case */
328 /* Parse argument name */
329 arg = strchr(argv[i], '=');
332 tu->args[i].name = kstrdup(argv[i], GFP_KERNEL);
335 /* If argument name is omitted, set "argN" */
336 snprintf(buf, MAX_EVENT_NAME_LEN, "arg%d", i + 1);
337 tu->args[i].name = kstrdup(buf, GFP_KERNEL);
340 if (!tu->args[i].name) {
341 pr_info("Failed to allocate argument[%d] name.\n", i);
346 if (!is_good_name(tu->args[i].name)) {
347 pr_info("Invalid argument[%d] name: %s\n", i, tu->args[i].name);
352 if (traceprobe_conflict_field_name(tu->args[i].name, tu->args, i)) {
353 pr_info("Argument[%d] name '%s' conflicts with "
354 "another field.\n", i, argv[i]);
359 /* Parse fetch argument */
360 ret = traceprobe_parse_probe_arg(arg, &tu->size, &tu->args[i], false, false);
362 pr_info("Parse error at argument[%d]. (%d)\n", i, ret);
367 ret = register_trace_uprobe(tu);
373 free_trace_uprobe(tu);
380 pr_info("Failed to parse address or file.\n");
385 static void cleanup_all_probes(void)
387 struct trace_uprobe *tu;
389 mutex_lock(&uprobe_lock);
390 while (!list_empty(&uprobe_list)) {
391 tu = list_entry(uprobe_list.next, struct trace_uprobe, list);
392 unregister_trace_uprobe(tu);
394 mutex_unlock(&uprobe_lock);
397 /* Probes listing interfaces */
398 static void *probes_seq_start(struct seq_file *m, loff_t *pos)
400 mutex_lock(&uprobe_lock);
401 return seq_list_start(&uprobe_list, *pos);
404 static void *probes_seq_next(struct seq_file *m, void *v, loff_t *pos)
406 return seq_list_next(v, &uprobe_list, pos);
409 static void probes_seq_stop(struct seq_file *m, void *v)
411 mutex_unlock(&uprobe_lock);
414 static int probes_seq_show(struct seq_file *m, void *v)
416 struct trace_uprobe *tu = v;
419 seq_printf(m, "p:%s/%s", tu->call.class->system, tu->call.name);
420 seq_printf(m, " %s:0x%p", tu->filename, (void *)tu->offset);
422 for (i = 0; i < tu->nr_args; i++)
423 seq_printf(m, " %s=%s", tu->args[i].name, tu->args[i].comm);
429 static const struct seq_operations probes_seq_op = {
430 .start = probes_seq_start,
431 .next = probes_seq_next,
432 .stop = probes_seq_stop,
433 .show = probes_seq_show
436 static int probes_open(struct inode *inode, struct file *file)
438 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC))
439 cleanup_all_probes();
441 return seq_open(file, &probes_seq_op);
444 static ssize_t probes_write(struct file *file, const char __user *buffer,
445 size_t count, loff_t *ppos)
447 return traceprobe_probes_write(file, buffer, count, ppos, create_trace_uprobe);
450 static const struct file_operations uprobe_events_ops = {
451 .owner = THIS_MODULE,
455 .release = seq_release,
456 .write = probes_write,
459 /* Probes profiling interfaces */
460 static int probes_profile_seq_show(struct seq_file *m, void *v)
462 struct trace_uprobe *tu = v;
464 seq_printf(m, " %s %-44s %15lu\n", tu->filename, tu->call.name, tu->nhit);
468 static const struct seq_operations profile_seq_op = {
469 .start = probes_seq_start,
470 .next = probes_seq_next,
471 .stop = probes_seq_stop,
472 .show = probes_profile_seq_show
475 static int profile_open(struct inode *inode, struct file *file)
477 return seq_open(file, &profile_seq_op);
480 static const struct file_operations uprobe_profile_ops = {
481 .owner = THIS_MODULE,
482 .open = profile_open,
485 .release = seq_release,
489 static int uprobe_trace_func(struct trace_uprobe *tu, struct pt_regs *regs)
491 struct uprobe_trace_entry_head *entry;
492 struct ring_buffer_event *event;
493 struct ring_buffer *buffer;
496 unsigned long irq_flags;
497 struct ftrace_event_call *call = &tu->call;
499 local_save_flags(irq_flags);
500 pc = preempt_count();
502 size = sizeof(*entry) + tu->size;
504 event = trace_current_buffer_lock_reserve(&buffer, call->event.type,
505 size, irq_flags, pc);
509 entry = ring_buffer_event_data(event);
510 entry->ip = instruction_pointer(task_pt_regs(current));
511 data = (u8 *)&entry[1];
512 for (i = 0; i < tu->nr_args; i++)
513 call_fetch(&tu->args[i].fetch, regs, data + tu->args[i].offset);
515 if (!filter_current_check_discard(buffer, call, entry, event))
516 trace_buffer_unlock_commit(buffer, event, irq_flags, pc);
521 /* Event entry printers */
522 static enum print_line_t
523 print_uprobe_event(struct trace_iterator *iter, int flags, struct trace_event *event)
525 struct uprobe_trace_entry_head *field;
526 struct trace_seq *s = &iter->seq;
527 struct trace_uprobe *tu;
531 field = (struct uprobe_trace_entry_head *)iter->ent;
532 tu = container_of(event, struct trace_uprobe, call.event);
534 if (!trace_seq_printf(s, "%s: (", tu->call.name))
537 if (!seq_print_ip_sym(s, field->ip, flags | TRACE_ITER_SYM_OFFSET))
540 if (!trace_seq_puts(s, ")"))
543 data = (u8 *)&field[1];
544 for (i = 0; i < tu->nr_args; i++) {
545 if (!tu->args[i].type->print(s, tu->args[i].name,
546 data + tu->args[i].offset, field))
550 if (trace_seq_puts(s, "\n"))
551 return TRACE_TYPE_HANDLED;
554 return TRACE_TYPE_PARTIAL_LINE;
557 static inline bool is_trace_uprobe_enabled(struct trace_uprobe *tu)
559 return tu->flags & (TP_FLAG_TRACE | TP_FLAG_PROFILE);
562 typedef bool (*filter_func_t)(struct uprobe_consumer *self,
563 enum uprobe_filter_ctx ctx,
564 struct mm_struct *mm);
567 probe_event_enable(struct trace_uprobe *tu, int flag, filter_func_t filter)
571 if (is_trace_uprobe_enabled(tu))
574 WARN_ON(!uprobe_filter_is_empty(&tu->filter));
577 tu->consumer.filter = filter;
578 ret = uprobe_register(tu->inode, tu->offset, &tu->consumer);
585 static void probe_event_disable(struct trace_uprobe *tu, int flag)
587 if (!is_trace_uprobe_enabled(tu))
590 WARN_ON(!uprobe_filter_is_empty(&tu->filter));
592 uprobe_unregister(tu->inode, tu->offset, &tu->consumer);
596 static int uprobe_event_define_fields(struct ftrace_event_call *event_call)
599 struct uprobe_trace_entry_head field;
600 struct trace_uprobe *tu = (struct trace_uprobe *)event_call->data;
602 DEFINE_FIELD(unsigned long, ip, FIELD_STRING_IP, 0);
603 /* Set argument names as fields */
604 for (i = 0; i < tu->nr_args; i++) {
605 ret = trace_define_field(event_call, tu->args[i].type->fmttype,
607 sizeof(field) + tu->args[i].offset,
608 tu->args[i].type->size,
609 tu->args[i].type->is_signed,
618 #define LEN_OR_ZERO (len ? len - pos : 0)
619 static int __set_print_fmt(struct trace_uprobe *tu, char *buf, int len)
621 const char *fmt, *arg;
626 arg = "REC->" FIELD_STRING_IP;
628 /* When len=0, we just calculate the needed length */
630 pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", fmt);
632 for (i = 0; i < tu->nr_args; i++) {
633 pos += snprintf(buf + pos, LEN_OR_ZERO, " %s=%s",
634 tu->args[i].name, tu->args[i].type->fmt);
637 pos += snprintf(buf + pos, LEN_OR_ZERO, "\", %s", arg);
639 for (i = 0; i < tu->nr_args; i++) {
640 pos += snprintf(buf + pos, LEN_OR_ZERO, ", REC->%s",
644 return pos; /* return the length of print_fmt */
648 static int set_print_fmt(struct trace_uprobe *tu)
653 /* First: called with 0 length to calculate the needed length */
654 len = __set_print_fmt(tu, NULL, 0);
655 print_fmt = kmalloc(len + 1, GFP_KERNEL);
659 /* Second: actually write the @print_fmt */
660 __set_print_fmt(tu, print_fmt, len + 1);
661 tu->call.print_fmt = print_fmt;
666 #ifdef CONFIG_PERF_EVENTS
668 __uprobe_perf_filter(struct trace_uprobe_filter *filter, struct mm_struct *mm)
670 struct perf_event *event;
672 if (filter->nr_systemwide)
675 list_for_each_entry(event, &filter->perf_events, hw.tp_list) {
676 if (event->hw.tp_target->mm == mm)
684 uprobe_filter_event(struct trace_uprobe *tu, struct perf_event *event)
686 return __uprobe_perf_filter(&tu->filter, event->hw.tp_target->mm);
689 static int uprobe_perf_open(struct trace_uprobe *tu, struct perf_event *event)
693 write_lock(&tu->filter.rwlock);
694 if (event->hw.tp_target) {
696 * event->parent != NULL means copy_process(), we can avoid
697 * uprobe_apply(). current->mm must be probed and we can rely
698 * on dup_mmap() which preserves the already installed bp's.
700 * attr.enable_on_exec means that exec/mmap will install the
701 * breakpoints we need.
703 done = tu->filter.nr_systemwide ||
704 event->parent || event->attr.enable_on_exec ||
705 uprobe_filter_event(tu, event);
706 list_add(&event->hw.tp_list, &tu->filter.perf_events);
708 done = tu->filter.nr_systemwide;
709 tu->filter.nr_systemwide++;
711 write_unlock(&tu->filter.rwlock);
714 uprobe_apply(tu->inode, tu->offset, &tu->consumer, true);
719 static int uprobe_perf_close(struct trace_uprobe *tu, struct perf_event *event)
723 write_lock(&tu->filter.rwlock);
724 if (event->hw.tp_target) {
725 list_del(&event->hw.tp_list);
726 done = tu->filter.nr_systemwide ||
727 (event->hw.tp_target->flags & PF_EXITING) ||
728 uprobe_filter_event(tu, event);
730 tu->filter.nr_systemwide--;
731 done = tu->filter.nr_systemwide;
733 write_unlock(&tu->filter.rwlock);
736 uprobe_apply(tu->inode, tu->offset, &tu->consumer, false);
741 static bool uprobe_perf_filter(struct uprobe_consumer *uc,
742 enum uprobe_filter_ctx ctx, struct mm_struct *mm)
744 struct trace_uprobe *tu;
747 tu = container_of(uc, struct trace_uprobe, consumer);
748 read_lock(&tu->filter.rwlock);
749 ret = __uprobe_perf_filter(&tu->filter, mm);
750 read_unlock(&tu->filter.rwlock);
755 /* uprobe profile handler */
756 static int uprobe_perf_func(struct trace_uprobe *tu, struct pt_regs *regs)
758 struct ftrace_event_call *call = &tu->call;
759 struct uprobe_trace_entry_head *entry;
760 struct hlist_head *head;
765 if (!uprobe_perf_filter(&tu->consumer, 0, current->mm))
766 return UPROBE_HANDLER_REMOVE;
768 __size = sizeof(*entry) + tu->size;
769 size = ALIGN(__size + sizeof(u32), sizeof(u64));
771 if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE, "profile buffer not large enough"))
776 entry = perf_trace_buf_prepare(size, call->event.type, regs, &rctx);
780 entry->ip = instruction_pointer(task_pt_regs(current));
781 data = (u8 *)&entry[1];
782 for (i = 0; i < tu->nr_args; i++)
783 call_fetch(&tu->args[i].fetch, regs, data + tu->args[i].offset);
785 head = this_cpu_ptr(call->perf_events);
786 perf_trace_buf_submit(entry, size, rctx, entry->ip, 1, regs, head, NULL);
792 #endif /* CONFIG_PERF_EVENTS */
795 int trace_uprobe_register(struct ftrace_event_call *event, enum trace_reg type, void *data)
797 struct trace_uprobe *tu = (struct trace_uprobe *)event->data;
800 case TRACE_REG_REGISTER:
801 return probe_event_enable(tu, TP_FLAG_TRACE, NULL);
803 case TRACE_REG_UNREGISTER:
804 probe_event_disable(tu, TP_FLAG_TRACE);
807 #ifdef CONFIG_PERF_EVENTS
808 case TRACE_REG_PERF_REGISTER:
809 return probe_event_enable(tu, TP_FLAG_PROFILE, uprobe_perf_filter);
811 case TRACE_REG_PERF_UNREGISTER:
812 probe_event_disable(tu, TP_FLAG_PROFILE);
815 case TRACE_REG_PERF_OPEN:
816 return uprobe_perf_open(tu, data);
818 case TRACE_REG_PERF_CLOSE:
819 return uprobe_perf_close(tu, data);
828 static int uprobe_dispatcher(struct uprobe_consumer *con, struct pt_regs *regs)
830 struct trace_uprobe *tu;
833 tu = container_of(con, struct trace_uprobe, consumer);
836 if (tu->flags & TP_FLAG_TRACE)
837 ret |= uprobe_trace_func(tu, regs);
839 #ifdef CONFIG_PERF_EVENTS
840 if (tu->flags & TP_FLAG_PROFILE)
841 ret |= uprobe_perf_func(tu, regs);
846 static struct trace_event_functions uprobe_funcs = {
847 .trace = print_uprobe_event
850 static int register_uprobe_event(struct trace_uprobe *tu)
852 struct ftrace_event_call *call = &tu->call;
855 /* Initialize ftrace_event_call */
856 INIT_LIST_HEAD(&call->class->fields);
857 call->event.funcs = &uprobe_funcs;
858 call->class->define_fields = uprobe_event_define_fields;
860 if (set_print_fmt(tu) < 0)
863 ret = register_ftrace_event(&call->event);
865 kfree(call->print_fmt);
869 call->class->reg = trace_uprobe_register;
871 ret = trace_add_event_call(call);
874 pr_info("Failed to register uprobe event: %s\n", call->name);
875 kfree(call->print_fmt);
876 unregister_ftrace_event(&call->event);
882 static void unregister_uprobe_event(struct trace_uprobe *tu)
884 /* tu->event is unregistered in trace_remove_event_call() */
885 trace_remove_event_call(&tu->call);
886 kfree(tu->call.print_fmt);
887 tu->call.print_fmt = NULL;
890 /* Make a trace interface for controling probe points */
891 static __init int init_uprobe_trace(void)
893 struct dentry *d_tracer;
895 d_tracer = tracing_init_dentry();
899 trace_create_file("uprobe_events", 0644, d_tracer,
900 NULL, &uprobe_events_ops);
901 /* Profile interface */
902 trace_create_file("uprobe_profile", 0444, d_tracer,
903 NULL, &uprobe_profile_ops);
907 fs_initcall(init_uprobe_trace);