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 int argc, const char **argv, 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 = (unsigned long)code->data;
255 val = translate_user_vaddr(code->immediate);
262 return process_fetch_insn_bottom(code, val, dest, base);
264 NOKPROBE_SYMBOL(process_fetch_insn)
266 static inline void init_trace_uprobe_filter(struct trace_uprobe_filter *filter)
268 rwlock_init(&filter->rwlock);
269 filter->nr_systemwide = 0;
270 INIT_LIST_HEAD(&filter->perf_events);
273 static inline bool uprobe_filter_is_empty(struct trace_uprobe_filter *filter)
275 return !filter->nr_systemwide && list_empty(&filter->perf_events);
278 static inline bool is_ret_probe(struct trace_uprobe *tu)
280 return tu->consumer.ret_handler != NULL;
283 static bool trace_uprobe_is_busy(struct dyn_event *ev)
285 struct trace_uprobe *tu = to_trace_uprobe(ev);
287 return trace_probe_is_enabled(&tu->tp);
290 static bool trace_uprobe_match_command_head(struct trace_uprobe *tu,
291 int argc, const char **argv)
293 char buf[MAX_ARGSTR_LEN + 1];
299 len = strlen(tu->filename);
300 if (strncmp(tu->filename, argv[0], len) || argv[0][len] != ':')
303 if (tu->ref_ctr_offset == 0)
304 snprintf(buf, sizeof(buf), "0x%0*lx",
305 (int)(sizeof(void *) * 2), tu->offset);
307 snprintf(buf, sizeof(buf), "0x%0*lx(0x%lx)",
308 (int)(sizeof(void *) * 2), tu->offset,
310 if (strcmp(buf, &argv[0][len + 1]))
315 return trace_probe_match_command_args(&tu->tp, argc, argv);
318 static bool trace_uprobe_match(const char *system, const char *event,
319 int argc, const char **argv, struct dyn_event *ev)
321 struct trace_uprobe *tu = to_trace_uprobe(ev);
323 return strcmp(trace_probe_name(&tu->tp), event) == 0 &&
324 (!system || strcmp(trace_probe_group_name(&tu->tp), system) == 0) &&
325 trace_uprobe_match_command_head(tu, argc, argv);
328 static nokprobe_inline struct trace_uprobe *
329 trace_uprobe_primary_from_call(struct trace_event_call *call)
331 struct trace_probe *tp;
333 tp = trace_probe_primary_from_call(call);
334 if (WARN_ON_ONCE(!tp))
337 return container_of(tp, struct trace_uprobe, tp);
341 * Allocate new trace_uprobe and initialize it (including uprobes).
343 static struct trace_uprobe *
344 alloc_trace_uprobe(const char *group, const char *event, int nargs, bool is_ret)
346 struct trace_uprobe *tu;
349 tu = kzalloc(SIZEOF_TRACE_UPROBE(nargs), GFP_KERNEL);
351 return ERR_PTR(-ENOMEM);
353 ret = trace_probe_init(&tu->tp, event, group);
357 dyn_event_init(&tu->devent, &trace_uprobe_ops);
358 tu->consumer.handler = uprobe_dispatcher;
360 tu->consumer.ret_handler = uretprobe_dispatcher;
361 init_trace_uprobe_filter(&tu->filter);
370 static void free_trace_uprobe(struct trace_uprobe *tu)
376 trace_probe_cleanup(&tu->tp);
381 static struct trace_uprobe *find_probe_event(const char *event, const char *group)
383 struct dyn_event *pos;
384 struct trace_uprobe *tu;
386 for_each_trace_uprobe(tu, pos)
387 if (strcmp(trace_probe_name(&tu->tp), event) == 0 &&
388 strcmp(trace_probe_group_name(&tu->tp), group) == 0)
394 /* Unregister a trace_uprobe and probe_event */
395 static int unregister_trace_uprobe(struct trace_uprobe *tu)
399 if (trace_probe_has_sibling(&tu->tp))
402 ret = unregister_uprobe_event(tu);
407 dyn_event_remove(&tu->devent);
408 trace_probe_unlink(&tu->tp);
409 free_trace_uprobe(tu);
413 static bool trace_uprobe_has_same_uprobe(struct trace_uprobe *orig,
414 struct trace_uprobe *comp)
416 struct trace_probe_event *tpe = orig->tp.event;
417 struct trace_probe *pos;
418 struct inode *comp_inode = d_real_inode(comp->path.dentry);
421 list_for_each_entry(pos, &tpe->probes, list) {
422 orig = container_of(pos, struct trace_uprobe, tp);
423 if (comp_inode != d_real_inode(orig->path.dentry) ||
424 comp->offset != orig->offset)
428 * trace_probe_compare_arg_type() ensured that nr_args and
429 * each argument name and type are same. Let's compare comm.
431 for (i = 0; i < orig->tp.nr_args; i++) {
432 if (strcmp(orig->tp.args[i].comm,
433 comp->tp.args[i].comm))
437 if (i == orig->tp.nr_args)
444 static int append_trace_uprobe(struct trace_uprobe *tu, struct trace_uprobe *to)
448 ret = trace_probe_compare_arg_type(&tu->tp, &to->tp);
450 /* Note that argument starts index = 2 */
451 trace_probe_log_set_index(ret + 1);
452 trace_probe_log_err(0, DIFF_ARG_TYPE);
455 if (trace_uprobe_has_same_uprobe(to, tu)) {
456 trace_probe_log_set_index(0);
457 trace_probe_log_err(0, SAME_PROBE);
461 /* Append to existing event */
462 ret = trace_probe_append(&tu->tp, &to->tp);
464 dyn_event_add(&tu->devent);
470 * Uprobe with multiple reference counter is not allowed. i.e.
471 * If inode and offset matches, reference counter offset *must*
472 * match as well. Though, there is one exception: If user is
473 * replacing old trace_uprobe with new one(same group/event),
474 * then we allow same uprobe with new reference counter as far
475 * as the new one does not conflict with any other existing
478 static int validate_ref_ctr_offset(struct trace_uprobe *new)
480 struct dyn_event *pos;
481 struct trace_uprobe *tmp;
482 struct inode *new_inode = d_real_inode(new->path.dentry);
484 for_each_trace_uprobe(tmp, pos) {
485 if (new_inode == d_real_inode(tmp->path.dentry) &&
486 new->offset == tmp->offset &&
487 new->ref_ctr_offset != tmp->ref_ctr_offset) {
488 pr_warn("Reference counter offset mismatch.");
495 /* Register a trace_uprobe and probe_event */
496 static int register_trace_uprobe(struct trace_uprobe *tu)
498 struct trace_uprobe *old_tu;
501 mutex_lock(&event_mutex);
503 ret = validate_ref_ctr_offset(tu);
507 /* register as an event */
508 old_tu = find_probe_event(trace_probe_name(&tu->tp),
509 trace_probe_group_name(&tu->tp));
511 if (is_ret_probe(tu) != is_ret_probe(old_tu)) {
512 trace_probe_log_set_index(0);
513 trace_probe_log_err(0, DIFF_PROBE_TYPE);
516 ret = append_trace_uprobe(tu, old_tu);
521 ret = register_uprobe_event(tu);
523 pr_warn("Failed to register probe event(%d)\n", ret);
527 dyn_event_add(&tu->devent);
530 mutex_unlock(&event_mutex);
537 * - Add uprobe: p|r[:[GRP/]EVENT] PATH:OFFSET [FETCHARGS]
539 static int trace_uprobe_create(int argc, const char **argv)
541 struct trace_uprobe *tu;
542 const char *event = NULL, *group = UPROBE_EVENT_SYSTEM;
543 char *arg, *filename, *rctr, *rctr_end, *tmp;
544 char buf[MAX_EVENT_NAME_LEN];
546 unsigned long offset, ref_ctr_offset;
547 bool is_return = false;
553 switch (argv[0][0]) {
566 if (argv[0][1] == ':')
569 if (!strchr(argv[1], '/'))
572 filename = kstrdup(argv[1], GFP_KERNEL);
576 /* Find the last occurrence, in case the path contains ':' too. */
577 arg = strrchr(filename, ':');
578 if (!arg || !isdigit(arg[1])) {
583 trace_probe_log_init("trace_uprobe", argc, argv);
584 trace_probe_log_set_index(1); /* filename is the 2nd argument */
587 ret = kern_path(filename, LOOKUP_FOLLOW, &path);
589 trace_probe_log_err(0, FILE_NOT_FOUND);
591 trace_probe_log_clear();
594 if (!d_is_reg(path.dentry)) {
595 trace_probe_log_err(0, NO_REGULAR_FILE);
597 goto fail_address_parse;
600 /* Parse reference counter offset if specified. */
601 rctr = strchr(arg, '(');
603 rctr_end = strchr(rctr, ')');
606 rctr_end = rctr + strlen(rctr);
607 trace_probe_log_err(rctr_end - filename,
609 goto fail_address_parse;
610 } else if (rctr_end[1] != '\0') {
612 trace_probe_log_err(rctr_end + 1 - filename,
614 goto fail_address_parse;
619 ret = kstrtoul(rctr, 0, &ref_ctr_offset);
621 trace_probe_log_err(rctr - filename, BAD_REFCNT);
622 goto fail_address_parse;
626 /* Parse uprobe offset. */
627 ret = kstrtoul(arg, 0, &offset);
629 trace_probe_log_err(arg - filename, BAD_UPROBE_OFFS);
630 goto fail_address_parse;
634 trace_probe_log_set_index(0);
636 ret = traceprobe_parse_event_name(&event, &group, buf,
639 goto fail_address_parse;
644 tail = kstrdup(kbasename(filename), GFP_KERNEL);
647 goto fail_address_parse;
650 ptr = strpbrk(tail, ".-_");
654 snprintf(buf, MAX_EVENT_NAME_LEN, "%c_%s_0x%lx", 'p', tail, offset);
662 tu = alloc_trace_uprobe(group, event, argc, is_return);
665 /* This must return -ENOMEM otherwise there is a bug */
666 WARN_ON_ONCE(ret != -ENOMEM);
667 goto fail_address_parse;
670 tu->ref_ctr_offset = ref_ctr_offset;
672 tu->filename = filename;
674 /* parse arguments */
675 for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
676 tmp = kstrdup(argv[i], GFP_KERNEL);
682 trace_probe_log_set_index(i + 2);
683 ret = traceprobe_parse_probe_arg(&tu->tp, i, tmp,
684 is_return ? TPARG_FL_RETURN : 0);
690 ret = traceprobe_set_print_fmt(&tu->tp, is_ret_probe(tu));
694 ret = register_trace_uprobe(tu);
699 free_trace_uprobe(tu);
701 trace_probe_log_clear();
705 trace_probe_log_clear();
712 static int create_or_delete_trace_uprobe(int argc, char **argv)
716 if (argv[0][0] == '-')
717 return dyn_event_release(argc, argv, &trace_uprobe_ops);
719 ret = trace_uprobe_create(argc, (const char **)argv);
720 return ret == -ECANCELED ? -EINVAL : ret;
723 static int trace_uprobe_release(struct dyn_event *ev)
725 struct trace_uprobe *tu = to_trace_uprobe(ev);
727 return unregister_trace_uprobe(tu);
730 /* Probes listing interfaces */
731 static int trace_uprobe_show(struct seq_file *m, struct dyn_event *ev)
733 struct trace_uprobe *tu = to_trace_uprobe(ev);
734 char c = is_ret_probe(tu) ? 'r' : 'p';
737 seq_printf(m, "%c:%s/%s %s:0x%0*lx", c, trace_probe_group_name(&tu->tp),
738 trace_probe_name(&tu->tp), tu->filename,
739 (int)(sizeof(void *) * 2), tu->offset);
741 if (tu->ref_ctr_offset)
742 seq_printf(m, "(0x%lx)", tu->ref_ctr_offset);
744 for (i = 0; i < tu->tp.nr_args; i++)
745 seq_printf(m, " %s=%s", tu->tp.args[i].name, tu->tp.args[i].comm);
751 static int probes_seq_show(struct seq_file *m, void *v)
753 struct dyn_event *ev = v;
755 if (!is_trace_uprobe(ev))
758 return trace_uprobe_show(m, ev);
761 static const struct seq_operations probes_seq_op = {
762 .start = dyn_event_seq_start,
763 .next = dyn_event_seq_next,
764 .stop = dyn_event_seq_stop,
765 .show = probes_seq_show
768 static int probes_open(struct inode *inode, struct file *file)
772 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
773 ret = dyn_events_release_all(&trace_uprobe_ops);
778 return seq_open(file, &probes_seq_op);
781 static ssize_t probes_write(struct file *file, const char __user *buffer,
782 size_t count, loff_t *ppos)
784 return trace_parse_run_command(file, buffer, count, ppos,
785 create_or_delete_trace_uprobe);
788 static const struct file_operations uprobe_events_ops = {
789 .owner = THIS_MODULE,
793 .release = seq_release,
794 .write = probes_write,
797 /* Probes profiling interfaces */
798 static int probes_profile_seq_show(struct seq_file *m, void *v)
800 struct dyn_event *ev = v;
801 struct trace_uprobe *tu;
803 if (!is_trace_uprobe(ev))
806 tu = to_trace_uprobe(ev);
807 seq_printf(m, " %s %-44s %15lu\n", tu->filename,
808 trace_probe_name(&tu->tp), tu->nhit);
812 static const struct seq_operations profile_seq_op = {
813 .start = dyn_event_seq_start,
814 .next = dyn_event_seq_next,
815 .stop = dyn_event_seq_stop,
816 .show = probes_profile_seq_show
819 static int profile_open(struct inode *inode, struct file *file)
821 return seq_open(file, &profile_seq_op);
824 static const struct file_operations uprobe_profile_ops = {
825 .owner = THIS_MODULE,
826 .open = profile_open,
829 .release = seq_release,
832 struct uprobe_cpu_buffer {
836 static struct uprobe_cpu_buffer __percpu *uprobe_cpu_buffer;
837 static int uprobe_buffer_refcnt;
839 static int uprobe_buffer_init(void)
843 uprobe_cpu_buffer = alloc_percpu(struct uprobe_cpu_buffer);
844 if (uprobe_cpu_buffer == NULL)
847 for_each_possible_cpu(cpu) {
848 struct page *p = alloc_pages_node(cpu_to_node(cpu),
854 per_cpu_ptr(uprobe_cpu_buffer, cpu)->buf = page_address(p);
855 mutex_init(&per_cpu_ptr(uprobe_cpu_buffer, cpu)->mutex);
861 for_each_possible_cpu(cpu) {
864 free_page((unsigned long)per_cpu_ptr(uprobe_cpu_buffer, cpu)->buf);
867 free_percpu(uprobe_cpu_buffer);
871 static int uprobe_buffer_enable(void)
875 BUG_ON(!mutex_is_locked(&event_mutex));
877 if (uprobe_buffer_refcnt++ == 0) {
878 ret = uprobe_buffer_init();
880 uprobe_buffer_refcnt--;
886 static void uprobe_buffer_disable(void)
890 BUG_ON(!mutex_is_locked(&event_mutex));
892 if (--uprobe_buffer_refcnt == 0) {
893 for_each_possible_cpu(cpu)
894 free_page((unsigned long)per_cpu_ptr(uprobe_cpu_buffer,
897 free_percpu(uprobe_cpu_buffer);
898 uprobe_cpu_buffer = NULL;
902 static struct uprobe_cpu_buffer *uprobe_buffer_get(void)
904 struct uprobe_cpu_buffer *ucb;
907 cpu = raw_smp_processor_id();
908 ucb = per_cpu_ptr(uprobe_cpu_buffer, cpu);
911 * Use per-cpu buffers for fastest access, but we might migrate
912 * so the mutex makes sure we have sole access to it.
914 mutex_lock(&ucb->mutex);
919 static void uprobe_buffer_put(struct uprobe_cpu_buffer *ucb)
921 mutex_unlock(&ucb->mutex);
924 static void __uprobe_trace_func(struct trace_uprobe *tu,
925 unsigned long func, struct pt_regs *regs,
926 struct uprobe_cpu_buffer *ucb, int dsize,
927 struct trace_event_file *trace_file)
929 struct uprobe_trace_entry_head *entry;
930 struct ring_buffer_event *event;
931 struct ring_buffer *buffer;
934 struct trace_event_call *call = trace_probe_event_call(&tu->tp);
936 WARN_ON(call != trace_file->event_call);
938 if (WARN_ON_ONCE(tu->tp.size + dsize > PAGE_SIZE))
941 if (trace_trigger_soft_disabled(trace_file))
944 esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
945 size = esize + tu->tp.size + dsize;
946 event = trace_event_buffer_lock_reserve(&buffer, trace_file,
947 call->event.type, size, 0, 0);
951 entry = ring_buffer_event_data(event);
952 if (is_ret_probe(tu)) {
953 entry->vaddr[0] = func;
954 entry->vaddr[1] = instruction_pointer(regs);
955 data = DATAOF_TRACE_ENTRY(entry, true);
957 entry->vaddr[0] = instruction_pointer(regs);
958 data = DATAOF_TRACE_ENTRY(entry, false);
961 memcpy(data, ucb->buf, tu->tp.size + dsize);
963 event_trigger_unlock_commit(trace_file, buffer, event, entry, 0, 0);
967 static int uprobe_trace_func(struct trace_uprobe *tu, struct pt_regs *regs,
968 struct uprobe_cpu_buffer *ucb, int dsize)
970 struct event_file_link *link;
972 if (is_ret_probe(tu))
976 trace_probe_for_each_link_rcu(link, &tu->tp)
977 __uprobe_trace_func(tu, 0, regs, ucb, dsize, link->file);
983 static void uretprobe_trace_func(struct trace_uprobe *tu, unsigned long func,
984 struct pt_regs *regs,
985 struct uprobe_cpu_buffer *ucb, int dsize)
987 struct event_file_link *link;
990 trace_probe_for_each_link_rcu(link, &tu->tp)
991 __uprobe_trace_func(tu, func, regs, ucb, dsize, link->file);
995 /* Event entry printers */
996 static enum print_line_t
997 print_uprobe_event(struct trace_iterator *iter, int flags, struct trace_event *event)
999 struct uprobe_trace_entry_head *entry;
1000 struct trace_seq *s = &iter->seq;
1001 struct trace_uprobe *tu;
1004 entry = (struct uprobe_trace_entry_head *)iter->ent;
1005 tu = trace_uprobe_primary_from_call(
1006 container_of(event, struct trace_event_call, event));
1010 if (is_ret_probe(tu)) {
1011 trace_seq_printf(s, "%s: (0x%lx <- 0x%lx)",
1012 trace_probe_name(&tu->tp),
1013 entry->vaddr[1], entry->vaddr[0]);
1014 data = DATAOF_TRACE_ENTRY(entry, true);
1016 trace_seq_printf(s, "%s: (0x%lx)",
1017 trace_probe_name(&tu->tp),
1019 data = DATAOF_TRACE_ENTRY(entry, false);
1022 if (print_probe_args(s, tu->tp.args, tu->tp.nr_args, data, entry) < 0)
1025 trace_seq_putc(s, '\n');
1028 return trace_handle_return(s);
1031 typedef bool (*filter_func_t)(struct uprobe_consumer *self,
1032 enum uprobe_filter_ctx ctx,
1033 struct mm_struct *mm);
1035 static int trace_uprobe_enable(struct trace_uprobe *tu, filter_func_t filter)
1039 tu->consumer.filter = filter;
1040 tu->inode = d_real_inode(tu->path.dentry);
1042 if (tu->ref_ctr_offset)
1043 ret = uprobe_register_refctr(tu->inode, tu->offset,
1044 tu->ref_ctr_offset, &tu->consumer);
1046 ret = uprobe_register(tu->inode, tu->offset, &tu->consumer);
1054 static void __probe_event_disable(struct trace_probe *tp)
1056 struct trace_probe *pos;
1057 struct trace_uprobe *tu;
1059 list_for_each_entry(pos, trace_probe_probe_list(tp), list) {
1060 tu = container_of(pos, struct trace_uprobe, tp);
1064 WARN_ON(!uprobe_filter_is_empty(&tu->filter));
1066 uprobe_unregister(tu->inode, tu->offset, &tu->consumer);
1071 static int probe_event_enable(struct trace_event_call *call,
1072 struct trace_event_file *file, filter_func_t filter)
1074 struct trace_probe *pos, *tp;
1075 struct trace_uprobe *tu;
1079 tp = trace_probe_primary_from_call(call);
1080 if (WARN_ON_ONCE(!tp))
1082 enabled = trace_probe_is_enabled(tp);
1084 /* This may also change "enabled" state */
1086 if (trace_probe_test_flag(tp, TP_FLAG_PROFILE))
1089 ret = trace_probe_add_file(tp, file);
1093 if (trace_probe_test_flag(tp, TP_FLAG_TRACE))
1096 trace_probe_set_flag(tp, TP_FLAG_PROFILE);
1099 tu = container_of(tp, struct trace_uprobe, tp);
1100 WARN_ON(!uprobe_filter_is_empty(&tu->filter));
1105 ret = uprobe_buffer_enable();
1109 list_for_each_entry(pos, trace_probe_probe_list(tp), list) {
1110 tu = container_of(pos, struct trace_uprobe, tp);
1111 ret = trace_uprobe_enable(tu, filter);
1113 __probe_event_disable(tp);
1121 uprobe_buffer_disable();
1125 trace_probe_remove_file(tp, file);
1127 trace_probe_clear_flag(tp, TP_FLAG_PROFILE);
1132 static void probe_event_disable(struct trace_event_call *call,
1133 struct trace_event_file *file)
1135 struct trace_probe *tp;
1137 tp = trace_probe_primary_from_call(call);
1138 if (WARN_ON_ONCE(!tp))
1141 if (!trace_probe_is_enabled(tp))
1145 if (trace_probe_remove_file(tp, file) < 0)
1148 if (trace_probe_is_enabled(tp))
1151 trace_probe_clear_flag(tp, TP_FLAG_PROFILE);
1153 __probe_event_disable(tp);
1154 uprobe_buffer_disable();
1157 static int uprobe_event_define_fields(struct trace_event_call *event_call)
1160 struct uprobe_trace_entry_head field;
1161 struct trace_uprobe *tu;
1163 tu = trace_uprobe_primary_from_call(event_call);
1167 if (is_ret_probe(tu)) {
1168 DEFINE_FIELD(unsigned long, vaddr[0], FIELD_STRING_FUNC, 0);
1169 DEFINE_FIELD(unsigned long, vaddr[1], FIELD_STRING_RETIP, 0);
1170 size = SIZEOF_TRACE_ENTRY(true);
1172 DEFINE_FIELD(unsigned long, vaddr[0], FIELD_STRING_IP, 0);
1173 size = SIZEOF_TRACE_ENTRY(false);
1176 return traceprobe_define_arg_fields(event_call, size, &tu->tp);
1179 #ifdef CONFIG_PERF_EVENTS
1181 __uprobe_perf_filter(struct trace_uprobe_filter *filter, struct mm_struct *mm)
1183 struct perf_event *event;
1185 if (filter->nr_systemwide)
1188 list_for_each_entry(event, &filter->perf_events, hw.tp_list) {
1189 if (event->hw.target->mm == mm)
1197 uprobe_filter_event(struct trace_uprobe *tu, struct perf_event *event)
1199 return __uprobe_perf_filter(&tu->filter, event->hw.target->mm);
1202 static int uprobe_perf_close(struct trace_uprobe *tu, struct perf_event *event)
1206 write_lock(&tu->filter.rwlock);
1207 if (event->hw.target) {
1208 list_del(&event->hw.tp_list);
1209 done = tu->filter.nr_systemwide ||
1210 (event->hw.target->flags & PF_EXITING) ||
1211 uprobe_filter_event(tu, event);
1213 tu->filter.nr_systemwide--;
1214 done = tu->filter.nr_systemwide;
1216 write_unlock(&tu->filter.rwlock);
1219 return uprobe_apply(tu->inode, tu->offset, &tu->consumer, false);
1224 static int uprobe_perf_open(struct trace_uprobe *tu, struct perf_event *event)
1229 write_lock(&tu->filter.rwlock);
1230 if (event->hw.target) {
1232 * event->parent != NULL means copy_process(), we can avoid
1233 * uprobe_apply(). current->mm must be probed and we can rely
1234 * on dup_mmap() which preserves the already installed bp's.
1236 * attr.enable_on_exec means that exec/mmap will install the
1237 * breakpoints we need.
1239 done = tu->filter.nr_systemwide ||
1240 event->parent || event->attr.enable_on_exec ||
1241 uprobe_filter_event(tu, event);
1242 list_add(&event->hw.tp_list, &tu->filter.perf_events);
1244 done = tu->filter.nr_systemwide;
1245 tu->filter.nr_systemwide++;
1247 write_unlock(&tu->filter.rwlock);
1251 err = uprobe_apply(tu->inode, tu->offset, &tu->consumer, true);
1253 uprobe_perf_close(tu, event);
1258 static int uprobe_perf_multi_call(struct trace_event_call *call,
1259 struct perf_event *event,
1260 int (*op)(struct trace_uprobe *tu, struct perf_event *event))
1262 struct trace_probe *pos, *tp;
1263 struct trace_uprobe *tu;
1266 tp = trace_probe_primary_from_call(call);
1267 if (WARN_ON_ONCE(!tp))
1270 list_for_each_entry(pos, trace_probe_probe_list(tp), list) {
1271 tu = container_of(pos, struct trace_uprobe, tp);
1272 ret = op(tu, event);
1279 static bool uprobe_perf_filter(struct uprobe_consumer *uc,
1280 enum uprobe_filter_ctx ctx, struct mm_struct *mm)
1282 struct trace_uprobe *tu;
1285 tu = container_of(uc, struct trace_uprobe, consumer);
1286 read_lock(&tu->filter.rwlock);
1287 ret = __uprobe_perf_filter(&tu->filter, mm);
1288 read_unlock(&tu->filter.rwlock);
1293 static void __uprobe_perf_func(struct trace_uprobe *tu,
1294 unsigned long func, struct pt_regs *regs,
1295 struct uprobe_cpu_buffer *ucb, int dsize)
1297 struct trace_event_call *call = trace_probe_event_call(&tu->tp);
1298 struct uprobe_trace_entry_head *entry;
1299 struct hlist_head *head;
1304 if (bpf_prog_array_valid(call) && !trace_call_bpf(call, regs))
1307 esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
1309 size = esize + tu->tp.size + dsize;
1310 size = ALIGN(size + sizeof(u32), sizeof(u64)) - sizeof(u32);
1311 if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE, "profile buffer not large enough"))
1315 head = this_cpu_ptr(call->perf_events);
1316 if (hlist_empty(head))
1319 entry = perf_trace_buf_alloc(size, NULL, &rctx);
1323 if (is_ret_probe(tu)) {
1324 entry->vaddr[0] = func;
1325 entry->vaddr[1] = instruction_pointer(regs);
1326 data = DATAOF_TRACE_ENTRY(entry, true);
1328 entry->vaddr[0] = instruction_pointer(regs);
1329 data = DATAOF_TRACE_ENTRY(entry, false);
1332 memcpy(data, ucb->buf, tu->tp.size + dsize);
1334 if (size - esize > tu->tp.size + dsize) {
1335 int len = tu->tp.size + dsize;
1337 memset(data + len, 0, size - esize - len);
1340 perf_trace_buf_submit(entry, size, rctx, call->event.type, 1, regs,
1346 /* uprobe profile handler */
1347 static int uprobe_perf_func(struct trace_uprobe *tu, struct pt_regs *regs,
1348 struct uprobe_cpu_buffer *ucb, int dsize)
1350 if (!uprobe_perf_filter(&tu->consumer, 0, current->mm))
1351 return UPROBE_HANDLER_REMOVE;
1353 if (!is_ret_probe(tu))
1354 __uprobe_perf_func(tu, 0, regs, ucb, dsize);
1358 static void uretprobe_perf_func(struct trace_uprobe *tu, unsigned long func,
1359 struct pt_regs *regs,
1360 struct uprobe_cpu_buffer *ucb, int dsize)
1362 __uprobe_perf_func(tu, func, regs, ucb, dsize);
1365 int bpf_get_uprobe_info(const struct perf_event *event, u32 *fd_type,
1366 const char **filename, u64 *probe_offset,
1367 bool perf_type_tracepoint)
1369 const char *pevent = trace_event_name(event->tp_event);
1370 const char *group = event->tp_event->class->system;
1371 struct trace_uprobe *tu;
1373 if (perf_type_tracepoint)
1374 tu = find_probe_event(pevent, group);
1376 tu = event->tp_event->data;
1380 *fd_type = is_ret_probe(tu) ? BPF_FD_TYPE_URETPROBE
1381 : BPF_FD_TYPE_UPROBE;
1382 *filename = tu->filename;
1383 *probe_offset = tu->offset;
1386 #endif /* CONFIG_PERF_EVENTS */
1389 trace_uprobe_register(struct trace_event_call *event, enum trace_reg type,
1392 struct trace_event_file *file = data;
1395 case TRACE_REG_REGISTER:
1396 return probe_event_enable(event, file, NULL);
1398 case TRACE_REG_UNREGISTER:
1399 probe_event_disable(event, file);
1402 #ifdef CONFIG_PERF_EVENTS
1403 case TRACE_REG_PERF_REGISTER:
1404 return probe_event_enable(event, NULL, uprobe_perf_filter);
1406 case TRACE_REG_PERF_UNREGISTER:
1407 probe_event_disable(event, NULL);
1410 case TRACE_REG_PERF_OPEN:
1411 return uprobe_perf_multi_call(event, data, uprobe_perf_open);
1413 case TRACE_REG_PERF_CLOSE:
1414 return uprobe_perf_multi_call(event, data, uprobe_perf_close);
1423 static int uprobe_dispatcher(struct uprobe_consumer *con, struct pt_regs *regs)
1425 struct trace_uprobe *tu;
1426 struct uprobe_dispatch_data udd;
1427 struct uprobe_cpu_buffer *ucb;
1432 tu = container_of(con, struct trace_uprobe, consumer);
1436 udd.bp_addr = instruction_pointer(regs);
1438 current->utask->vaddr = (unsigned long) &udd;
1440 if (WARN_ON_ONCE(!uprobe_cpu_buffer))
1443 dsize = __get_data_size(&tu->tp, regs);
1444 esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
1446 ucb = uprobe_buffer_get();
1447 store_trace_args(ucb->buf, &tu->tp, regs, esize, dsize);
1449 if (trace_probe_test_flag(&tu->tp, TP_FLAG_TRACE))
1450 ret |= uprobe_trace_func(tu, regs, ucb, dsize);
1452 #ifdef CONFIG_PERF_EVENTS
1453 if (trace_probe_test_flag(&tu->tp, TP_FLAG_PROFILE))
1454 ret |= uprobe_perf_func(tu, regs, ucb, dsize);
1456 uprobe_buffer_put(ucb);
1460 static int uretprobe_dispatcher(struct uprobe_consumer *con,
1461 unsigned long func, struct pt_regs *regs)
1463 struct trace_uprobe *tu;
1464 struct uprobe_dispatch_data udd;
1465 struct uprobe_cpu_buffer *ucb;
1468 tu = container_of(con, struct trace_uprobe, consumer);
1473 current->utask->vaddr = (unsigned long) &udd;
1475 if (WARN_ON_ONCE(!uprobe_cpu_buffer))
1478 dsize = __get_data_size(&tu->tp, regs);
1479 esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
1481 ucb = uprobe_buffer_get();
1482 store_trace_args(ucb->buf, &tu->tp, regs, esize, dsize);
1484 if (trace_probe_test_flag(&tu->tp, TP_FLAG_TRACE))
1485 uretprobe_trace_func(tu, func, regs, ucb, dsize);
1487 #ifdef CONFIG_PERF_EVENTS
1488 if (trace_probe_test_flag(&tu->tp, TP_FLAG_PROFILE))
1489 uretprobe_perf_func(tu, func, regs, ucb, dsize);
1491 uprobe_buffer_put(ucb);
1495 static struct trace_event_functions uprobe_funcs = {
1496 .trace = print_uprobe_event
1499 static inline void init_trace_event_call(struct trace_uprobe *tu)
1501 struct trace_event_call *call = trace_probe_event_call(&tu->tp);
1503 call->event.funcs = &uprobe_funcs;
1504 call->class->define_fields = uprobe_event_define_fields;
1506 call->flags = TRACE_EVENT_FL_UPROBE | TRACE_EVENT_FL_CAP_ANY;
1507 call->class->reg = trace_uprobe_register;
1510 static int register_uprobe_event(struct trace_uprobe *tu)
1512 init_trace_event_call(tu);
1514 return trace_probe_register_event_call(&tu->tp);
1517 static int unregister_uprobe_event(struct trace_uprobe *tu)
1519 return trace_probe_unregister_event_call(&tu->tp);
1522 #ifdef CONFIG_PERF_EVENTS
1523 struct trace_event_call *
1524 create_local_trace_uprobe(char *name, unsigned long offs,
1525 unsigned long ref_ctr_offset, bool is_return)
1527 struct trace_uprobe *tu;
1531 ret = kern_path(name, LOOKUP_FOLLOW, &path);
1533 return ERR_PTR(ret);
1535 if (!d_is_reg(path.dentry)) {
1537 return ERR_PTR(-EINVAL);
1541 * local trace_kprobes are not added to dyn_event, so they are never
1542 * searched in find_trace_kprobe(). Therefore, there is no concern of
1543 * duplicated name "DUMMY_EVENT" here.
1545 tu = alloc_trace_uprobe(UPROBE_EVENT_SYSTEM, "DUMMY_EVENT", 0,
1549 pr_info("Failed to allocate trace_uprobe.(%d)\n",
1552 return ERR_CAST(tu);
1557 tu->ref_ctr_offset = ref_ctr_offset;
1558 tu->filename = kstrdup(name, GFP_KERNEL);
1559 init_trace_event_call(tu);
1561 if (traceprobe_set_print_fmt(&tu->tp, is_ret_probe(tu)) < 0) {
1566 return trace_probe_event_call(&tu->tp);
1568 free_trace_uprobe(tu);
1569 return ERR_PTR(ret);
1572 void destroy_local_trace_uprobe(struct trace_event_call *event_call)
1574 struct trace_uprobe *tu;
1576 tu = trace_uprobe_primary_from_call(event_call);
1578 free_trace_uprobe(tu);
1580 #endif /* CONFIG_PERF_EVENTS */
1582 /* Make a trace interface for controling probe points */
1583 static __init int init_uprobe_trace(void)
1585 struct dentry *d_tracer;
1588 ret = dyn_event_register(&trace_uprobe_ops);
1592 d_tracer = tracing_init_dentry();
1593 if (IS_ERR(d_tracer))
1596 trace_create_file("uprobe_events", 0644, d_tracer,
1597 NULL, &uprobe_events_ops);
1598 /* Profile interface */
1599 trace_create_file("uprobe_profile", 0444, d_tracer,
1600 NULL, &uprobe_profile_ops);
1604 fs_initcall(init_uprobe_trace);