1 // SPDX-License-Identifier: GPL-2.0
3 * uprobes-based tracing events
5 * Copyright (C) IBM Corporation, 2010-2012
6 * Author: Srikar Dronamraju <srikar@linux.vnet.ibm.com>
8 #define pr_fmt(fmt) "trace_uprobe: " fmt
10 #include <linux/ctype.h>
11 #include <linux/module.h>
12 #include <linux/uaccess.h>
13 #include <linux/uprobes.h>
14 #include <linux/namei.h>
15 #include <linux/string.h>
16 #include <linux/rculist.h>
18 #include "trace_dynevent.h"
19 #include "trace_probe.h"
20 #include "trace_probe_tmpl.h"
22 #define UPROBE_EVENT_SYSTEM "uprobes"
24 struct uprobe_trace_entry_head {
25 struct trace_entry ent;
26 unsigned long vaddr[];
29 #define SIZEOF_TRACE_ENTRY(is_return) \
30 (sizeof(struct uprobe_trace_entry_head) + \
31 sizeof(unsigned long) * (is_return ? 2 : 1))
33 #define DATAOF_TRACE_ENTRY(entry, is_return) \
34 ((void*)(entry) + SIZEOF_TRACE_ENTRY(is_return))
36 struct trace_uprobe_filter {
39 struct list_head perf_events;
42 static int trace_uprobe_create(int argc, const char **argv);
43 static int trace_uprobe_show(struct seq_file *m, struct dyn_event *ev);
44 static int trace_uprobe_release(struct dyn_event *ev);
45 static bool trace_uprobe_is_busy(struct dyn_event *ev);
46 static bool trace_uprobe_match(const char *system, const char *event,
47 struct dyn_event *ev);
49 static struct dyn_event_operations trace_uprobe_ops = {
50 .create = trace_uprobe_create,
51 .show = trace_uprobe_show,
52 .is_busy = trace_uprobe_is_busy,
53 .free = trace_uprobe_release,
54 .match = trace_uprobe_match,
58 * uprobe event core functions
61 struct dyn_event devent;
62 struct trace_uprobe_filter filter;
63 struct uprobe_consumer consumer;
68 unsigned long ref_ctr_offset;
70 struct trace_probe tp;
73 static bool is_trace_uprobe(struct dyn_event *ev)
75 return ev->ops == &trace_uprobe_ops;
78 static struct trace_uprobe *to_trace_uprobe(struct dyn_event *ev)
80 return container_of(ev, struct trace_uprobe, devent);
84 * for_each_trace_uprobe - iterate over the trace_uprobe list
85 * @pos: the struct trace_uprobe * for each entry
86 * @dpos: the struct dyn_event * to use as a loop cursor
88 #define for_each_trace_uprobe(pos, dpos) \
89 for_each_dyn_event(dpos) \
90 if (is_trace_uprobe(dpos) && (pos = to_trace_uprobe(dpos)))
92 #define SIZEOF_TRACE_UPROBE(n) \
93 (offsetof(struct trace_uprobe, tp.args) + \
94 (sizeof(struct probe_arg) * (n)))
96 static int register_uprobe_event(struct trace_uprobe *tu);
97 static int unregister_uprobe_event(struct trace_uprobe *tu);
99 struct uprobe_dispatch_data {
100 struct trace_uprobe *tu;
101 unsigned long bp_addr;
104 static int uprobe_dispatcher(struct uprobe_consumer *con, struct pt_regs *regs);
105 static int uretprobe_dispatcher(struct uprobe_consumer *con,
106 unsigned long func, struct pt_regs *regs);
108 #ifdef CONFIG_STACK_GROWSUP
109 static unsigned long adjust_stack_addr(unsigned long addr, unsigned int n)
111 return addr - (n * sizeof(long));
114 static unsigned long adjust_stack_addr(unsigned long addr, unsigned int n)
116 return addr + (n * sizeof(long));
120 static unsigned long get_user_stack_nth(struct pt_regs *regs, unsigned int n)
123 unsigned long addr = user_stack_pointer(regs);
125 addr = adjust_stack_addr(addr, n);
127 if (copy_from_user(&ret, (void __force __user *) addr, sizeof(ret)))
134 * Uprobes-specific fetch functions
136 static nokprobe_inline int
137 probe_mem_read(void *dest, void *src, size_t size)
139 void __user *vaddr = (void __force __user *)src;
141 return copy_from_user(dest, vaddr, size) ? -EFAULT : 0;
144 static nokprobe_inline int
145 probe_mem_read_user(void *dest, void *src, size_t size)
147 return probe_mem_read(dest, src, size);
151 * Fetch a null-terminated string. Caller MUST set *(u32 *)dest with max
152 * length and relative data location.
154 static nokprobe_inline int
155 fetch_store_string(unsigned long addr, void *dest, void *base)
158 u32 loc = *(u32 *)dest;
159 int maxlen = get_loc_len(loc);
160 u8 *dst = get_loc_data(dest, base);
161 void __user *src = (void __force __user *) addr;
163 if (unlikely(!maxlen))
166 if (addr == FETCH_TOKEN_COMM)
167 ret = strlcpy(dst, current->comm, maxlen);
169 ret = strncpy_from_user(dst, src, maxlen);
175 * Include the terminating null byte. In this case it
176 * was copied by strncpy_from_user but not accounted
180 *(u32 *)dest = make_data_loc(ret, (void *)dst - base);
186 static nokprobe_inline int
187 fetch_store_string_user(unsigned long addr, void *dest, void *base)
189 return fetch_store_string(addr, dest, base);
192 /* Return the length of string -- including null terminal byte */
193 static nokprobe_inline int
194 fetch_store_strlen(unsigned long addr)
197 void __user *vaddr = (void __force __user *) addr;
199 if (addr == FETCH_TOKEN_COMM)
200 len = strlen(current->comm) + 1;
202 len = strnlen_user(vaddr, MAX_STRING_SIZE);
204 return (len > MAX_STRING_SIZE) ? 0 : len;
207 static nokprobe_inline int
208 fetch_store_strlen_user(unsigned long addr)
210 return fetch_store_strlen(addr);
213 static unsigned long translate_user_vaddr(unsigned long file_offset)
215 unsigned long base_addr;
216 struct uprobe_dispatch_data *udd;
218 udd = (void *) current->utask->vaddr;
220 base_addr = udd->bp_addr - udd->tu->offset;
221 return base_addr + file_offset;
224 /* Note that we don't verify it, since the code does not come from user space */
226 process_fetch_insn(struct fetch_insn *code, struct pt_regs *regs, void *dest,
231 /* 1st stage: get value from context */
234 val = regs_get_register(regs, code->param);
237 val = get_user_stack_nth(regs, code->param);
239 case FETCH_OP_STACKP:
240 val = user_stack_pointer(regs);
242 case FETCH_OP_RETVAL:
243 val = regs_return_value(regs);
246 val = code->immediate;
249 val = FETCH_TOKEN_COMM;
252 val = translate_user_vaddr(code->immediate);
259 return process_fetch_insn_bottom(code, val, dest, base);
261 NOKPROBE_SYMBOL(process_fetch_insn)
263 static inline void init_trace_uprobe_filter(struct trace_uprobe_filter *filter)
265 rwlock_init(&filter->rwlock);
266 filter->nr_systemwide = 0;
267 INIT_LIST_HEAD(&filter->perf_events);
270 static inline bool uprobe_filter_is_empty(struct trace_uprobe_filter *filter)
272 return !filter->nr_systemwide && list_empty(&filter->perf_events);
275 static inline bool is_ret_probe(struct trace_uprobe *tu)
277 return tu->consumer.ret_handler != NULL;
280 static bool trace_uprobe_is_busy(struct dyn_event *ev)
282 struct trace_uprobe *tu = to_trace_uprobe(ev);
284 return trace_probe_is_enabled(&tu->tp);
287 static bool trace_uprobe_match(const char *system, const char *event,
288 struct dyn_event *ev)
290 struct trace_uprobe *tu = to_trace_uprobe(ev);
292 return strcmp(trace_event_name(&tu->tp.call), event) == 0 &&
293 (!system || strcmp(tu->tp.call.class->system, system) == 0);
297 * Allocate new trace_uprobe and initialize it (including uprobes).
299 static struct trace_uprobe *
300 alloc_trace_uprobe(const char *group, const char *event, int nargs, bool is_ret)
302 struct trace_uprobe *tu;
304 if (!event || !group)
305 return ERR_PTR(-EINVAL);
307 tu = kzalloc(SIZEOF_TRACE_UPROBE(nargs), GFP_KERNEL);
309 return ERR_PTR(-ENOMEM);
311 tu->tp.call.class = &tu->tp.class;
312 tu->tp.call.name = kstrdup(event, GFP_KERNEL);
313 if (!tu->tp.call.name)
316 tu->tp.class.system = kstrdup(group, GFP_KERNEL);
317 if (!tu->tp.class.system)
320 dyn_event_init(&tu->devent, &trace_uprobe_ops);
321 INIT_LIST_HEAD(&tu->tp.files);
322 tu->consumer.handler = uprobe_dispatcher;
324 tu->consumer.ret_handler = uretprobe_dispatcher;
325 init_trace_uprobe_filter(&tu->filter);
329 kfree(tu->tp.call.name);
332 return ERR_PTR(-ENOMEM);
335 static void free_trace_uprobe(struct trace_uprobe *tu)
342 for (i = 0; i < tu->tp.nr_args; i++)
343 traceprobe_free_probe_arg(&tu->tp.args[i]);
346 kfree(tu->tp.call.class->system);
347 kfree(tu->tp.call.name);
348 kfree(tu->tp.call.print_fmt);
353 static struct trace_uprobe *find_probe_event(const char *event, const char *group)
355 struct dyn_event *pos;
356 struct trace_uprobe *tu;
358 for_each_trace_uprobe(tu, pos)
359 if (strcmp(trace_event_name(&tu->tp.call), event) == 0 &&
360 strcmp(tu->tp.call.class->system, group) == 0)
366 /* Unregister a trace_uprobe and probe_event */
367 static int unregister_trace_uprobe(struct trace_uprobe *tu)
371 ret = unregister_uprobe_event(tu);
375 dyn_event_remove(&tu->devent);
376 free_trace_uprobe(tu);
381 * Uprobe with multiple reference counter is not allowed. i.e.
382 * If inode and offset matches, reference counter offset *must*
383 * match as well. Though, there is one exception: If user is
384 * replacing old trace_uprobe with new one(same group/event),
385 * then we allow same uprobe with new reference counter as far
386 * as the new one does not conflict with any other existing
389 static struct trace_uprobe *find_old_trace_uprobe(struct trace_uprobe *new)
391 struct dyn_event *pos;
392 struct trace_uprobe *tmp, *old = NULL;
393 struct inode *new_inode = d_real_inode(new->path.dentry);
395 old = find_probe_event(trace_event_name(&new->tp.call),
396 new->tp.call.class->system);
398 for_each_trace_uprobe(tmp, pos) {
399 if ((old ? old != tmp : true) &&
400 new_inode == d_real_inode(tmp->path.dentry) &&
401 new->offset == tmp->offset &&
402 new->ref_ctr_offset != tmp->ref_ctr_offset) {
403 pr_warn("Reference counter offset mismatch.");
404 return ERR_PTR(-EINVAL);
410 /* Register a trace_uprobe and probe_event */
411 static int register_trace_uprobe(struct trace_uprobe *tu)
413 struct trace_uprobe *old_tu;
416 mutex_lock(&event_mutex);
418 /* register as an event */
419 old_tu = find_old_trace_uprobe(tu);
420 if (IS_ERR(old_tu)) {
421 ret = PTR_ERR(old_tu);
426 /* delete old event */
427 ret = unregister_trace_uprobe(old_tu);
432 ret = register_uprobe_event(tu);
434 pr_warn("Failed to register probe event(%d)\n", ret);
438 dyn_event_add(&tu->devent);
441 mutex_unlock(&event_mutex);
448 * - Add uprobe: p|r[:[GRP/]EVENT] PATH:OFFSET [FETCHARGS]
450 * - Remove uprobe: -:[GRP/]EVENT
452 static int trace_uprobe_create(int argc, const char **argv)
454 struct trace_uprobe *tu;
455 const char *event = NULL, *group = UPROBE_EVENT_SYSTEM;
456 char *arg, *filename, *rctr, *rctr_end, *tmp;
457 char buf[MAX_EVENT_NAME_LEN];
459 unsigned long offset, ref_ctr_offset;
460 bool is_return = false;
466 /* argc must be >= 1 */
467 if (argv[0][0] == 'r')
469 else if (argv[0][0] != 'p' || argc < 2)
472 if (argv[0][1] == ':')
475 if (!strchr(argv[1], '/'))
478 filename = kstrdup(argv[1], GFP_KERNEL);
482 /* Find the last occurrence, in case the path contains ':' too. */
483 arg = strrchr(filename, ':');
484 if (!arg || !isdigit(arg[1])) {
489 trace_probe_log_init("trace_uprobe", argc, argv);
490 trace_probe_log_set_index(1); /* filename is the 2nd argument */
493 ret = kern_path(filename, LOOKUP_FOLLOW, &path);
495 trace_probe_log_err(0, FILE_NOT_FOUND);
497 trace_probe_log_clear();
500 if (!d_is_reg(path.dentry)) {
501 trace_probe_log_err(0, NO_REGULAR_FILE);
503 goto fail_address_parse;
506 /* Parse reference counter offset if specified. */
507 rctr = strchr(arg, '(');
509 rctr_end = strchr(rctr, ')');
512 rctr_end = rctr + strlen(rctr);
513 trace_probe_log_err(rctr_end - filename,
515 goto fail_address_parse;
516 } else if (rctr_end[1] != '\0') {
518 trace_probe_log_err(rctr_end + 1 - filename,
520 goto fail_address_parse;
525 ret = kstrtoul(rctr, 0, &ref_ctr_offset);
527 trace_probe_log_err(rctr - filename, BAD_REFCNT);
528 goto fail_address_parse;
532 /* Parse uprobe offset. */
533 ret = kstrtoul(arg, 0, &offset);
535 trace_probe_log_err(arg - filename, BAD_UPROBE_OFFS);
536 goto fail_address_parse;
540 trace_probe_log_set_index(0);
542 ret = traceprobe_parse_event_name(&event, &group, buf,
545 goto fail_address_parse;
550 tail = kstrdup(kbasename(filename), GFP_KERNEL);
553 goto fail_address_parse;
556 ptr = strpbrk(tail, ".-_");
560 snprintf(buf, MAX_EVENT_NAME_LEN, "%c_%s_0x%lx", 'p', tail, offset);
568 tu = alloc_trace_uprobe(group, event, argc, is_return);
571 /* This must return -ENOMEM otherwise there is a bug */
572 WARN_ON_ONCE(ret != -ENOMEM);
573 goto fail_address_parse;
576 tu->ref_ctr_offset = ref_ctr_offset;
578 tu->filename = filename;
580 /* parse arguments */
581 for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
582 tmp = kstrdup(argv[i], GFP_KERNEL);
588 trace_probe_log_set_index(i + 2);
589 ret = traceprobe_parse_probe_arg(&tu->tp, i, tmp,
590 is_return ? TPARG_FL_RETURN : 0);
596 ret = traceprobe_set_print_fmt(&tu->tp, is_ret_probe(tu));
600 ret = register_trace_uprobe(tu);
605 free_trace_uprobe(tu);
607 trace_probe_log_clear();
611 trace_probe_log_clear();
618 static int create_or_delete_trace_uprobe(int argc, char **argv)
622 if (argv[0][0] == '-')
623 return dyn_event_release(argc, argv, &trace_uprobe_ops);
625 ret = trace_uprobe_create(argc, (const char **)argv);
626 return ret == -ECANCELED ? -EINVAL : ret;
629 static int trace_uprobe_release(struct dyn_event *ev)
631 struct trace_uprobe *tu = to_trace_uprobe(ev);
633 return unregister_trace_uprobe(tu);
636 /* Probes listing interfaces */
637 static int trace_uprobe_show(struct seq_file *m, struct dyn_event *ev)
639 struct trace_uprobe *tu = to_trace_uprobe(ev);
640 char c = is_ret_probe(tu) ? 'r' : 'p';
643 seq_printf(m, "%c:%s/%s %s:0x%0*lx", c, tu->tp.call.class->system,
644 trace_event_name(&tu->tp.call), tu->filename,
645 (int)(sizeof(void *) * 2), tu->offset);
647 if (tu->ref_ctr_offset)
648 seq_printf(m, "(0x%lx)", tu->ref_ctr_offset);
650 for (i = 0; i < tu->tp.nr_args; i++)
651 seq_printf(m, " %s=%s", tu->tp.args[i].name, tu->tp.args[i].comm);
657 static int probes_seq_show(struct seq_file *m, void *v)
659 struct dyn_event *ev = v;
661 if (!is_trace_uprobe(ev))
664 return trace_uprobe_show(m, ev);
667 static const struct seq_operations probes_seq_op = {
668 .start = dyn_event_seq_start,
669 .next = dyn_event_seq_next,
670 .stop = dyn_event_seq_stop,
671 .show = probes_seq_show
674 static int probes_open(struct inode *inode, struct file *file)
678 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
679 ret = dyn_events_release_all(&trace_uprobe_ops);
684 return seq_open(file, &probes_seq_op);
687 static ssize_t probes_write(struct file *file, const char __user *buffer,
688 size_t count, loff_t *ppos)
690 return trace_parse_run_command(file, buffer, count, ppos,
691 create_or_delete_trace_uprobe);
694 static const struct file_operations uprobe_events_ops = {
695 .owner = THIS_MODULE,
699 .release = seq_release,
700 .write = probes_write,
703 /* Probes profiling interfaces */
704 static int probes_profile_seq_show(struct seq_file *m, void *v)
706 struct dyn_event *ev = v;
707 struct trace_uprobe *tu;
709 if (!is_trace_uprobe(ev))
712 tu = to_trace_uprobe(ev);
713 seq_printf(m, " %s %-44s %15lu\n", tu->filename,
714 trace_event_name(&tu->tp.call), tu->nhit);
718 static const struct seq_operations profile_seq_op = {
719 .start = dyn_event_seq_start,
720 .next = dyn_event_seq_next,
721 .stop = dyn_event_seq_stop,
722 .show = probes_profile_seq_show
725 static int profile_open(struct inode *inode, struct file *file)
727 return seq_open(file, &profile_seq_op);
730 static const struct file_operations uprobe_profile_ops = {
731 .owner = THIS_MODULE,
732 .open = profile_open,
735 .release = seq_release,
738 struct uprobe_cpu_buffer {
742 static struct uprobe_cpu_buffer __percpu *uprobe_cpu_buffer;
743 static int uprobe_buffer_refcnt;
745 static int uprobe_buffer_init(void)
749 uprobe_cpu_buffer = alloc_percpu(struct uprobe_cpu_buffer);
750 if (uprobe_cpu_buffer == NULL)
753 for_each_possible_cpu(cpu) {
754 struct page *p = alloc_pages_node(cpu_to_node(cpu),
760 per_cpu_ptr(uprobe_cpu_buffer, cpu)->buf = page_address(p);
761 mutex_init(&per_cpu_ptr(uprobe_cpu_buffer, cpu)->mutex);
767 for_each_possible_cpu(cpu) {
770 free_page((unsigned long)per_cpu_ptr(uprobe_cpu_buffer, cpu)->buf);
773 free_percpu(uprobe_cpu_buffer);
777 static int uprobe_buffer_enable(void)
781 BUG_ON(!mutex_is_locked(&event_mutex));
783 if (uprobe_buffer_refcnt++ == 0) {
784 ret = uprobe_buffer_init();
786 uprobe_buffer_refcnt--;
792 static void uprobe_buffer_disable(void)
796 BUG_ON(!mutex_is_locked(&event_mutex));
798 if (--uprobe_buffer_refcnt == 0) {
799 for_each_possible_cpu(cpu)
800 free_page((unsigned long)per_cpu_ptr(uprobe_cpu_buffer,
803 free_percpu(uprobe_cpu_buffer);
804 uprobe_cpu_buffer = NULL;
808 static struct uprobe_cpu_buffer *uprobe_buffer_get(void)
810 struct uprobe_cpu_buffer *ucb;
813 cpu = raw_smp_processor_id();
814 ucb = per_cpu_ptr(uprobe_cpu_buffer, cpu);
817 * Use per-cpu buffers for fastest access, but we might migrate
818 * so the mutex makes sure we have sole access to it.
820 mutex_lock(&ucb->mutex);
825 static void uprobe_buffer_put(struct uprobe_cpu_buffer *ucb)
827 mutex_unlock(&ucb->mutex);
830 static void __uprobe_trace_func(struct trace_uprobe *tu,
831 unsigned long func, struct pt_regs *regs,
832 struct uprobe_cpu_buffer *ucb, int dsize,
833 struct trace_event_file *trace_file)
835 struct uprobe_trace_entry_head *entry;
836 struct ring_buffer_event *event;
837 struct ring_buffer *buffer;
840 struct trace_event_call *call = &tu->tp.call;
842 WARN_ON(call != trace_file->event_call);
844 if (WARN_ON_ONCE(tu->tp.size + dsize > PAGE_SIZE))
847 if (trace_trigger_soft_disabled(trace_file))
850 esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
851 size = esize + tu->tp.size + dsize;
852 event = trace_event_buffer_lock_reserve(&buffer, trace_file,
853 call->event.type, size, 0, 0);
857 entry = ring_buffer_event_data(event);
858 if (is_ret_probe(tu)) {
859 entry->vaddr[0] = func;
860 entry->vaddr[1] = instruction_pointer(regs);
861 data = DATAOF_TRACE_ENTRY(entry, true);
863 entry->vaddr[0] = instruction_pointer(regs);
864 data = DATAOF_TRACE_ENTRY(entry, false);
867 memcpy(data, ucb->buf, tu->tp.size + dsize);
869 event_trigger_unlock_commit(trace_file, buffer, event, entry, 0, 0);
873 static int uprobe_trace_func(struct trace_uprobe *tu, struct pt_regs *regs,
874 struct uprobe_cpu_buffer *ucb, int dsize)
876 struct event_file_link *link;
878 if (is_ret_probe(tu))
882 list_for_each_entry_rcu(link, &tu->tp.files, list)
883 __uprobe_trace_func(tu, 0, regs, ucb, dsize, link->file);
889 static void uretprobe_trace_func(struct trace_uprobe *tu, unsigned long func,
890 struct pt_regs *regs,
891 struct uprobe_cpu_buffer *ucb, int dsize)
893 struct event_file_link *link;
896 list_for_each_entry_rcu(link, &tu->tp.files, list)
897 __uprobe_trace_func(tu, func, regs, ucb, dsize, link->file);
901 /* Event entry printers */
902 static enum print_line_t
903 print_uprobe_event(struct trace_iterator *iter, int flags, struct trace_event *event)
905 struct uprobe_trace_entry_head *entry;
906 struct trace_seq *s = &iter->seq;
907 struct trace_uprobe *tu;
910 entry = (struct uprobe_trace_entry_head *)iter->ent;
911 tu = container_of(event, struct trace_uprobe, tp.call.event);
913 if (is_ret_probe(tu)) {
914 trace_seq_printf(s, "%s: (0x%lx <- 0x%lx)",
915 trace_event_name(&tu->tp.call),
916 entry->vaddr[1], entry->vaddr[0]);
917 data = DATAOF_TRACE_ENTRY(entry, true);
919 trace_seq_printf(s, "%s: (0x%lx)",
920 trace_event_name(&tu->tp.call),
922 data = DATAOF_TRACE_ENTRY(entry, false);
925 if (print_probe_args(s, tu->tp.args, tu->tp.nr_args, data, entry) < 0)
928 trace_seq_putc(s, '\n');
931 return trace_handle_return(s);
934 typedef bool (*filter_func_t)(struct uprobe_consumer *self,
935 enum uprobe_filter_ctx ctx,
936 struct mm_struct *mm);
939 probe_event_enable(struct trace_uprobe *tu, struct trace_event_file *file,
940 filter_func_t filter)
942 bool enabled = trace_probe_is_enabled(&tu->tp);
943 struct event_file_link *link = NULL;
947 if (tu->tp.flags & TP_FLAG_PROFILE)
950 link = kmalloc(sizeof(*link), GFP_KERNEL);
955 list_add_tail_rcu(&link->list, &tu->tp.files);
957 tu->tp.flags |= TP_FLAG_TRACE;
959 if (tu->tp.flags & TP_FLAG_TRACE)
962 tu->tp.flags |= TP_FLAG_PROFILE;
965 WARN_ON(!uprobe_filter_is_empty(&tu->filter));
970 ret = uprobe_buffer_enable();
974 tu->consumer.filter = filter;
975 tu->inode = d_real_inode(tu->path.dentry);
976 if (tu->ref_ctr_offset) {
977 ret = uprobe_register_refctr(tu->inode, tu->offset,
978 tu->ref_ctr_offset, &tu->consumer);
980 ret = uprobe_register(tu->inode, tu->offset, &tu->consumer);
989 uprobe_buffer_disable();
993 list_del(&link->list);
995 tu->tp.flags &= ~TP_FLAG_TRACE;
997 tu->tp.flags &= ~TP_FLAG_PROFILE;
1003 probe_event_disable(struct trace_uprobe *tu, struct trace_event_file *file)
1005 if (!trace_probe_is_enabled(&tu->tp))
1009 struct event_file_link *link;
1011 link = find_event_file_link(&tu->tp, file);
1015 list_del_rcu(&link->list);
1016 /* synchronize with u{,ret}probe_trace_func */
1020 if (!list_empty(&tu->tp.files))
1024 WARN_ON(!uprobe_filter_is_empty(&tu->filter));
1026 uprobe_unregister(tu->inode, tu->offset, &tu->consumer);
1028 tu->tp.flags &= file ? ~TP_FLAG_TRACE : ~TP_FLAG_PROFILE;
1030 uprobe_buffer_disable();
1033 static int uprobe_event_define_fields(struct trace_event_call *event_call)
1036 struct uprobe_trace_entry_head field;
1037 struct trace_uprobe *tu = event_call->data;
1039 if (is_ret_probe(tu)) {
1040 DEFINE_FIELD(unsigned long, vaddr[0], FIELD_STRING_FUNC, 0);
1041 DEFINE_FIELD(unsigned long, vaddr[1], FIELD_STRING_RETIP, 0);
1042 size = SIZEOF_TRACE_ENTRY(true);
1044 DEFINE_FIELD(unsigned long, vaddr[0], FIELD_STRING_IP, 0);
1045 size = SIZEOF_TRACE_ENTRY(false);
1048 return traceprobe_define_arg_fields(event_call, size, &tu->tp);
1051 #ifdef CONFIG_PERF_EVENTS
1053 __uprobe_perf_filter(struct trace_uprobe_filter *filter, struct mm_struct *mm)
1055 struct perf_event *event;
1057 if (filter->nr_systemwide)
1060 list_for_each_entry(event, &filter->perf_events, hw.tp_list) {
1061 if (event->hw.target->mm == mm)
1069 uprobe_filter_event(struct trace_uprobe *tu, struct perf_event *event)
1071 return __uprobe_perf_filter(&tu->filter, event->hw.target->mm);
1074 static int uprobe_perf_close(struct trace_uprobe *tu, struct perf_event *event)
1078 write_lock(&tu->filter.rwlock);
1079 if (event->hw.target) {
1080 list_del(&event->hw.tp_list);
1081 done = tu->filter.nr_systemwide ||
1082 (event->hw.target->flags & PF_EXITING) ||
1083 uprobe_filter_event(tu, event);
1085 tu->filter.nr_systemwide--;
1086 done = tu->filter.nr_systemwide;
1088 write_unlock(&tu->filter.rwlock);
1091 return uprobe_apply(tu->inode, tu->offset, &tu->consumer, false);
1096 static int uprobe_perf_open(struct trace_uprobe *tu, struct perf_event *event)
1101 write_lock(&tu->filter.rwlock);
1102 if (event->hw.target) {
1104 * event->parent != NULL means copy_process(), we can avoid
1105 * uprobe_apply(). current->mm must be probed and we can rely
1106 * on dup_mmap() which preserves the already installed bp's.
1108 * attr.enable_on_exec means that exec/mmap will install the
1109 * breakpoints we need.
1111 done = tu->filter.nr_systemwide ||
1112 event->parent || event->attr.enable_on_exec ||
1113 uprobe_filter_event(tu, event);
1114 list_add(&event->hw.tp_list, &tu->filter.perf_events);
1116 done = tu->filter.nr_systemwide;
1117 tu->filter.nr_systemwide++;
1119 write_unlock(&tu->filter.rwlock);
1123 err = uprobe_apply(tu->inode, tu->offset, &tu->consumer, true);
1125 uprobe_perf_close(tu, event);
1130 static bool uprobe_perf_filter(struct uprobe_consumer *uc,
1131 enum uprobe_filter_ctx ctx, struct mm_struct *mm)
1133 struct trace_uprobe *tu;
1136 tu = container_of(uc, struct trace_uprobe, consumer);
1137 read_lock(&tu->filter.rwlock);
1138 ret = __uprobe_perf_filter(&tu->filter, mm);
1139 read_unlock(&tu->filter.rwlock);
1144 static void __uprobe_perf_func(struct trace_uprobe *tu,
1145 unsigned long func, struct pt_regs *regs,
1146 struct uprobe_cpu_buffer *ucb, int dsize)
1148 struct trace_event_call *call = &tu->tp.call;
1149 struct uprobe_trace_entry_head *entry;
1150 struct hlist_head *head;
1155 if (bpf_prog_array_valid(call) && !trace_call_bpf(call, regs))
1158 esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
1160 size = esize + tu->tp.size + dsize;
1161 size = ALIGN(size + sizeof(u32), sizeof(u64)) - sizeof(u32);
1162 if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE, "profile buffer not large enough"))
1166 head = this_cpu_ptr(call->perf_events);
1167 if (hlist_empty(head))
1170 entry = perf_trace_buf_alloc(size, NULL, &rctx);
1174 if (is_ret_probe(tu)) {
1175 entry->vaddr[0] = func;
1176 entry->vaddr[1] = instruction_pointer(regs);
1177 data = DATAOF_TRACE_ENTRY(entry, true);
1179 entry->vaddr[0] = instruction_pointer(regs);
1180 data = DATAOF_TRACE_ENTRY(entry, false);
1183 memcpy(data, ucb->buf, tu->tp.size + dsize);
1185 if (size - esize > tu->tp.size + dsize) {
1186 int len = tu->tp.size + dsize;
1188 memset(data + len, 0, size - esize - len);
1191 perf_trace_buf_submit(entry, size, rctx, call->event.type, 1, regs,
1197 /* uprobe profile handler */
1198 static int uprobe_perf_func(struct trace_uprobe *tu, struct pt_regs *regs,
1199 struct uprobe_cpu_buffer *ucb, int dsize)
1201 if (!uprobe_perf_filter(&tu->consumer, 0, current->mm))
1202 return UPROBE_HANDLER_REMOVE;
1204 if (!is_ret_probe(tu))
1205 __uprobe_perf_func(tu, 0, regs, ucb, dsize);
1209 static void uretprobe_perf_func(struct trace_uprobe *tu, unsigned long func,
1210 struct pt_regs *regs,
1211 struct uprobe_cpu_buffer *ucb, int dsize)
1213 __uprobe_perf_func(tu, func, regs, ucb, dsize);
1216 int bpf_get_uprobe_info(const struct perf_event *event, u32 *fd_type,
1217 const char **filename, u64 *probe_offset,
1218 bool perf_type_tracepoint)
1220 const char *pevent = trace_event_name(event->tp_event);
1221 const char *group = event->tp_event->class->system;
1222 struct trace_uprobe *tu;
1224 if (perf_type_tracepoint)
1225 tu = find_probe_event(pevent, group);
1227 tu = event->tp_event->data;
1231 *fd_type = is_ret_probe(tu) ? BPF_FD_TYPE_URETPROBE
1232 : BPF_FD_TYPE_UPROBE;
1233 *filename = tu->filename;
1234 *probe_offset = tu->offset;
1237 #endif /* CONFIG_PERF_EVENTS */
1240 trace_uprobe_register(struct trace_event_call *event, enum trace_reg type,
1243 struct trace_uprobe *tu = event->data;
1244 struct trace_event_file *file = data;
1247 case TRACE_REG_REGISTER:
1248 return probe_event_enable(tu, file, NULL);
1250 case TRACE_REG_UNREGISTER:
1251 probe_event_disable(tu, file);
1254 #ifdef CONFIG_PERF_EVENTS
1255 case TRACE_REG_PERF_REGISTER:
1256 return probe_event_enable(tu, NULL, uprobe_perf_filter);
1258 case TRACE_REG_PERF_UNREGISTER:
1259 probe_event_disable(tu, NULL);
1262 case TRACE_REG_PERF_OPEN:
1263 return uprobe_perf_open(tu, data);
1265 case TRACE_REG_PERF_CLOSE:
1266 return uprobe_perf_close(tu, data);
1275 static int uprobe_dispatcher(struct uprobe_consumer *con, struct pt_regs *regs)
1277 struct trace_uprobe *tu;
1278 struct uprobe_dispatch_data udd;
1279 struct uprobe_cpu_buffer *ucb;
1284 tu = container_of(con, struct trace_uprobe, consumer);
1288 udd.bp_addr = instruction_pointer(regs);
1290 current->utask->vaddr = (unsigned long) &udd;
1292 if (WARN_ON_ONCE(!uprobe_cpu_buffer))
1295 dsize = __get_data_size(&tu->tp, regs);
1296 esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
1298 ucb = uprobe_buffer_get();
1299 store_trace_args(ucb->buf, &tu->tp, regs, esize, dsize);
1301 if (tu->tp.flags & TP_FLAG_TRACE)
1302 ret |= uprobe_trace_func(tu, regs, ucb, dsize);
1304 #ifdef CONFIG_PERF_EVENTS
1305 if (tu->tp.flags & TP_FLAG_PROFILE)
1306 ret |= uprobe_perf_func(tu, regs, ucb, dsize);
1308 uprobe_buffer_put(ucb);
1312 static int uretprobe_dispatcher(struct uprobe_consumer *con,
1313 unsigned long func, struct pt_regs *regs)
1315 struct trace_uprobe *tu;
1316 struct uprobe_dispatch_data udd;
1317 struct uprobe_cpu_buffer *ucb;
1320 tu = container_of(con, struct trace_uprobe, consumer);
1325 current->utask->vaddr = (unsigned long) &udd;
1327 if (WARN_ON_ONCE(!uprobe_cpu_buffer))
1330 dsize = __get_data_size(&tu->tp, regs);
1331 esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
1333 ucb = uprobe_buffer_get();
1334 store_trace_args(ucb->buf, &tu->tp, regs, esize, dsize);
1336 if (tu->tp.flags & TP_FLAG_TRACE)
1337 uretprobe_trace_func(tu, func, regs, ucb, dsize);
1339 #ifdef CONFIG_PERF_EVENTS
1340 if (tu->tp.flags & TP_FLAG_PROFILE)
1341 uretprobe_perf_func(tu, func, regs, ucb, dsize);
1343 uprobe_buffer_put(ucb);
1347 static struct trace_event_functions uprobe_funcs = {
1348 .trace = print_uprobe_event
1351 static inline void init_trace_event_call(struct trace_uprobe *tu,
1352 struct trace_event_call *call)
1354 INIT_LIST_HEAD(&call->class->fields);
1355 call->event.funcs = &uprobe_funcs;
1356 call->class->define_fields = uprobe_event_define_fields;
1358 call->flags = TRACE_EVENT_FL_UPROBE;
1359 call->class->reg = trace_uprobe_register;
1363 static int register_uprobe_event(struct trace_uprobe *tu)
1365 struct trace_event_call *call = &tu->tp.call;
1368 init_trace_event_call(tu, call);
1370 ret = register_trace_event(&call->event);
1374 ret = trace_add_event_call(call);
1377 pr_info("Failed to register uprobe event: %s\n",
1378 trace_event_name(call));
1379 unregister_trace_event(&call->event);
1385 static int unregister_uprobe_event(struct trace_uprobe *tu)
1387 /* tu->event is unregistered in trace_remove_event_call() */
1388 return trace_remove_event_call(&tu->tp.call);
1391 #ifdef CONFIG_PERF_EVENTS
1392 struct trace_event_call *
1393 create_local_trace_uprobe(char *name, unsigned long offs,
1394 unsigned long ref_ctr_offset, bool is_return)
1396 struct trace_uprobe *tu;
1400 ret = kern_path(name, LOOKUP_FOLLOW, &path);
1402 return ERR_PTR(ret);
1404 if (!d_is_reg(path.dentry)) {
1406 return ERR_PTR(-EINVAL);
1410 * local trace_kprobes are not added to dyn_event, so they are never
1411 * searched in find_trace_kprobe(). Therefore, there is no concern of
1412 * duplicated name "DUMMY_EVENT" here.
1414 tu = alloc_trace_uprobe(UPROBE_EVENT_SYSTEM, "DUMMY_EVENT", 0,
1418 pr_info("Failed to allocate trace_uprobe.(%d)\n",
1421 return ERR_CAST(tu);
1426 tu->ref_ctr_offset = ref_ctr_offset;
1427 tu->filename = kstrdup(name, GFP_KERNEL);
1428 init_trace_event_call(tu, &tu->tp.call);
1430 if (traceprobe_set_print_fmt(&tu->tp, is_ret_probe(tu)) < 0) {
1435 return &tu->tp.call;
1437 free_trace_uprobe(tu);
1438 return ERR_PTR(ret);
1441 void destroy_local_trace_uprobe(struct trace_event_call *event_call)
1443 struct trace_uprobe *tu;
1445 tu = container_of(event_call, struct trace_uprobe, tp.call);
1447 free_trace_uprobe(tu);
1449 #endif /* CONFIG_PERF_EVENTS */
1451 /* Make a trace interface for controling probe points */
1452 static __init int init_uprobe_trace(void)
1454 struct dentry *d_tracer;
1457 ret = dyn_event_register(&trace_uprobe_ops);
1461 d_tracer = tracing_init_dentry();
1462 if (IS_ERR(d_tracer))
1465 trace_create_file("uprobe_events", 0644, d_tracer,
1466 NULL, &uprobe_events_ops);
1467 /* Profile interface */
1468 trace_create_file("uprobe_profile", 0444, d_tracer,
1469 NULL, &uprobe_profile_ops);
1473 fs_initcall(init_uprobe_trace);