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 * Fetch a null-terminated string. Caller MUST set *(u32 *)dest with max
145 * length and relative data location.
147 static nokprobe_inline int
148 fetch_store_string(unsigned long addr, void *dest, void *base)
151 u32 loc = *(u32 *)dest;
152 int maxlen = get_loc_len(loc);
153 u8 *dst = get_loc_data(dest, base);
154 void __user *src = (void __force __user *) addr;
156 if (unlikely(!maxlen))
159 ret = strncpy_from_user(dst, src, maxlen);
165 * Include the terminating null byte. In this case it
166 * was copied by strncpy_from_user but not accounted
170 *(u32 *)dest = make_data_loc(ret, (void *)dst - base);
176 /* Return the length of string -- including null terminal byte */
177 static nokprobe_inline int
178 fetch_store_strlen(unsigned long addr)
181 void __user *vaddr = (void __force __user *) addr;
183 len = strnlen_user(vaddr, MAX_STRING_SIZE);
185 return (len > MAX_STRING_SIZE) ? 0 : len;
188 static unsigned long translate_user_vaddr(unsigned long file_offset)
190 unsigned long base_addr;
191 struct uprobe_dispatch_data *udd;
193 udd = (void *) current->utask->vaddr;
195 base_addr = udd->bp_addr - udd->tu->offset;
196 return base_addr + file_offset;
199 /* Note that we don't verify it, since the code does not come from user space */
201 process_fetch_insn(struct fetch_insn *code, struct pt_regs *regs, void *dest,
206 /* 1st stage: get value from context */
209 val = regs_get_register(regs, code->param);
212 val = get_user_stack_nth(regs, code->param);
214 case FETCH_OP_STACKP:
215 val = user_stack_pointer(regs);
217 case FETCH_OP_RETVAL:
218 val = regs_return_value(regs);
221 val = code->immediate;
224 val = translate_user_vaddr(code->immediate);
231 return process_fetch_insn_bottom(code, val, dest, base);
233 NOKPROBE_SYMBOL(process_fetch_insn)
235 static inline void init_trace_uprobe_filter(struct trace_uprobe_filter *filter)
237 rwlock_init(&filter->rwlock);
238 filter->nr_systemwide = 0;
239 INIT_LIST_HEAD(&filter->perf_events);
242 static inline bool uprobe_filter_is_empty(struct trace_uprobe_filter *filter)
244 return !filter->nr_systemwide && list_empty(&filter->perf_events);
247 static inline bool is_ret_probe(struct trace_uprobe *tu)
249 return tu->consumer.ret_handler != NULL;
252 static bool trace_uprobe_is_busy(struct dyn_event *ev)
254 struct trace_uprobe *tu = to_trace_uprobe(ev);
256 return trace_probe_is_enabled(&tu->tp);
259 static bool trace_uprobe_match(const char *system, const char *event,
260 struct dyn_event *ev)
262 struct trace_uprobe *tu = to_trace_uprobe(ev);
264 return strcmp(trace_event_name(&tu->tp.call), event) == 0 &&
265 (!system || strcmp(tu->tp.call.class->system, system) == 0);
269 * Allocate new trace_uprobe and initialize it (including uprobes).
271 static struct trace_uprobe *
272 alloc_trace_uprobe(const char *group, const char *event, int nargs, bool is_ret)
274 struct trace_uprobe *tu;
276 if (!event || !is_good_name(event))
277 return ERR_PTR(-EINVAL);
279 if (!group || !is_good_name(group))
280 return ERR_PTR(-EINVAL);
282 tu = kzalloc(SIZEOF_TRACE_UPROBE(nargs), GFP_KERNEL);
284 return ERR_PTR(-ENOMEM);
286 tu->tp.call.class = &tu->tp.class;
287 tu->tp.call.name = kstrdup(event, GFP_KERNEL);
288 if (!tu->tp.call.name)
291 tu->tp.class.system = kstrdup(group, GFP_KERNEL);
292 if (!tu->tp.class.system)
295 dyn_event_init(&tu->devent, &trace_uprobe_ops);
296 INIT_LIST_HEAD(&tu->tp.files);
297 tu->consumer.handler = uprobe_dispatcher;
299 tu->consumer.ret_handler = uretprobe_dispatcher;
300 init_trace_uprobe_filter(&tu->filter);
304 kfree(tu->tp.call.name);
307 return ERR_PTR(-ENOMEM);
310 static void free_trace_uprobe(struct trace_uprobe *tu)
317 for (i = 0; i < tu->tp.nr_args; i++)
318 traceprobe_free_probe_arg(&tu->tp.args[i]);
321 kfree(tu->tp.call.class->system);
322 kfree(tu->tp.call.name);
327 static struct trace_uprobe *find_probe_event(const char *event, const char *group)
329 struct dyn_event *pos;
330 struct trace_uprobe *tu;
332 for_each_trace_uprobe(tu, pos)
333 if (strcmp(trace_event_name(&tu->tp.call), event) == 0 &&
334 strcmp(tu->tp.call.class->system, group) == 0)
340 /* Unregister a trace_uprobe and probe_event */
341 static int unregister_trace_uprobe(struct trace_uprobe *tu)
345 ret = unregister_uprobe_event(tu);
349 dyn_event_remove(&tu->devent);
350 free_trace_uprobe(tu);
355 * Uprobe with multiple reference counter is not allowed. i.e.
356 * If inode and offset matches, reference counter offset *must*
357 * match as well. Though, there is one exception: If user is
358 * replacing old trace_uprobe with new one(same group/event),
359 * then we allow same uprobe with new reference counter as far
360 * as the new one does not conflict with any other existing
363 static struct trace_uprobe *find_old_trace_uprobe(struct trace_uprobe *new)
365 struct dyn_event *pos;
366 struct trace_uprobe *tmp, *old = NULL;
367 struct inode *new_inode = d_real_inode(new->path.dentry);
369 old = find_probe_event(trace_event_name(&new->tp.call),
370 new->tp.call.class->system);
372 for_each_trace_uprobe(tmp, pos) {
373 if ((old ? old != tmp : true) &&
374 new_inode == d_real_inode(tmp->path.dentry) &&
375 new->offset == tmp->offset &&
376 new->ref_ctr_offset != tmp->ref_ctr_offset) {
377 pr_warn("Reference counter offset mismatch.");
378 return ERR_PTR(-EINVAL);
384 /* Register a trace_uprobe and probe_event */
385 static int register_trace_uprobe(struct trace_uprobe *tu)
387 struct trace_uprobe *old_tu;
390 mutex_lock(&event_mutex);
392 /* register as an event */
393 old_tu = find_old_trace_uprobe(tu);
394 if (IS_ERR(old_tu)) {
395 ret = PTR_ERR(old_tu);
400 /* delete old event */
401 ret = unregister_trace_uprobe(old_tu);
406 ret = register_uprobe_event(tu);
408 pr_warn("Failed to register probe event(%d)\n", ret);
412 dyn_event_add(&tu->devent);
415 mutex_unlock(&event_mutex);
422 * - Add uprobe: p|r[:[GRP/]EVENT] PATH:OFFSET [FETCHARGS]
424 * - Remove uprobe: -:[GRP/]EVENT
426 static int trace_uprobe_create(int argc, const char **argv)
428 struct trace_uprobe *tu;
429 const char *event = NULL, *group = UPROBE_EVENT_SYSTEM;
430 char *arg, *filename, *rctr, *rctr_end, *tmp;
431 char buf[MAX_EVENT_NAME_LEN];
433 unsigned long offset, ref_ctr_offset;
434 bool is_return = false;
440 /* argc must be >= 1 */
441 if (argv[0][0] == 'r')
443 else if (argv[0][0] != 'p' || argc < 2)
446 if (argv[0][1] == ':')
449 if (!strchr(argv[1], '/'))
452 filename = kstrdup(argv[1], GFP_KERNEL);
456 /* Find the last occurrence, in case the path contains ':' too. */
457 arg = strrchr(filename, ':');
458 if (!arg || !isdigit(arg[1])) {
464 ret = kern_path(filename, LOOKUP_FOLLOW, &path);
469 if (!d_is_reg(path.dentry)) {
471 goto fail_address_parse;
474 /* Parse reference counter offset if specified. */
475 rctr = strchr(arg, '(');
477 rctr_end = strchr(rctr, ')');
478 if (rctr > rctr_end || *(rctr_end + 1) != 0) {
480 pr_info("Invalid reference counter offset.\n");
481 goto fail_address_parse;
486 ret = kstrtoul(rctr, 0, &ref_ctr_offset);
488 pr_info("Invalid reference counter offset.\n");
489 goto fail_address_parse;
493 /* Parse uprobe offset. */
494 ret = kstrtoul(arg, 0, &offset);
496 goto fail_address_parse;
503 ret = traceprobe_parse_event_name(&event, &group, buf);
505 goto fail_address_parse;
510 tail = kstrdup(kbasename(filename), GFP_KERNEL);
513 goto fail_address_parse;
516 ptr = strpbrk(tail, ".-_");
520 snprintf(buf, MAX_EVENT_NAME_LEN, "%c_%s_0x%lx", 'p', tail, offset);
525 tu = alloc_trace_uprobe(group, event, argc, is_return);
527 pr_info("Failed to allocate trace_uprobe.(%d)\n", (int)PTR_ERR(tu));
529 goto fail_address_parse;
532 tu->ref_ctr_offset = ref_ctr_offset;
534 tu->filename = filename;
536 /* parse arguments */
537 for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
538 tmp = kstrdup(argv[i], GFP_KERNEL);
544 ret = traceprobe_parse_probe_arg(&tu->tp, i, tmp,
545 is_return ? TPARG_FL_RETURN : 0);
551 ret = register_trace_uprobe(tu);
557 free_trace_uprobe(tu);
564 pr_info("Failed to parse address or file.\n");
569 static int create_or_delete_trace_uprobe(int argc, char **argv)
573 if (argv[0][0] == '-')
574 return dyn_event_release(argc, argv, &trace_uprobe_ops);
576 ret = trace_uprobe_create(argc, (const char **)argv);
577 return ret == -ECANCELED ? -EINVAL : ret;
580 static int trace_uprobe_release(struct dyn_event *ev)
582 struct trace_uprobe *tu = to_trace_uprobe(ev);
584 return unregister_trace_uprobe(tu);
587 /* Probes listing interfaces */
588 static int trace_uprobe_show(struct seq_file *m, struct dyn_event *ev)
590 struct trace_uprobe *tu = to_trace_uprobe(ev);
591 char c = is_ret_probe(tu) ? 'r' : 'p';
594 seq_printf(m, "%c:%s/%s %s:0x%0*lx", c, tu->tp.call.class->system,
595 trace_event_name(&tu->tp.call), tu->filename,
596 (int)(sizeof(void *) * 2), tu->offset);
598 if (tu->ref_ctr_offset)
599 seq_printf(m, "(0x%lx)", tu->ref_ctr_offset);
601 for (i = 0; i < tu->tp.nr_args; i++)
602 seq_printf(m, " %s=%s", tu->tp.args[i].name, tu->tp.args[i].comm);
608 static int probes_seq_show(struct seq_file *m, void *v)
610 struct dyn_event *ev = v;
612 if (!is_trace_uprobe(ev))
615 return trace_uprobe_show(m, ev);
618 static const struct seq_operations probes_seq_op = {
619 .start = dyn_event_seq_start,
620 .next = dyn_event_seq_next,
621 .stop = dyn_event_seq_stop,
622 .show = probes_seq_show
625 static int probes_open(struct inode *inode, struct file *file)
629 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
630 ret = dyn_events_release_all(&trace_uprobe_ops);
635 return seq_open(file, &probes_seq_op);
638 static ssize_t probes_write(struct file *file, const char __user *buffer,
639 size_t count, loff_t *ppos)
641 return trace_parse_run_command(file, buffer, count, ppos,
642 create_or_delete_trace_uprobe);
645 static const struct file_operations uprobe_events_ops = {
646 .owner = THIS_MODULE,
650 .release = seq_release,
651 .write = probes_write,
654 /* Probes profiling interfaces */
655 static int probes_profile_seq_show(struct seq_file *m, void *v)
657 struct dyn_event *ev = v;
658 struct trace_uprobe *tu;
660 if (!is_trace_uprobe(ev))
663 tu = to_trace_uprobe(ev);
664 seq_printf(m, " %s %-44s %15lu\n", tu->filename,
665 trace_event_name(&tu->tp.call), tu->nhit);
669 static const struct seq_operations profile_seq_op = {
670 .start = dyn_event_seq_start,
671 .next = dyn_event_seq_next,
672 .stop = dyn_event_seq_stop,
673 .show = probes_profile_seq_show
676 static int profile_open(struct inode *inode, struct file *file)
678 return seq_open(file, &profile_seq_op);
681 static const struct file_operations uprobe_profile_ops = {
682 .owner = THIS_MODULE,
683 .open = profile_open,
686 .release = seq_release,
689 struct uprobe_cpu_buffer {
693 static struct uprobe_cpu_buffer __percpu *uprobe_cpu_buffer;
694 static int uprobe_buffer_refcnt;
696 static int uprobe_buffer_init(void)
700 uprobe_cpu_buffer = alloc_percpu(struct uprobe_cpu_buffer);
701 if (uprobe_cpu_buffer == NULL)
704 for_each_possible_cpu(cpu) {
705 struct page *p = alloc_pages_node(cpu_to_node(cpu),
711 per_cpu_ptr(uprobe_cpu_buffer, cpu)->buf = page_address(p);
712 mutex_init(&per_cpu_ptr(uprobe_cpu_buffer, cpu)->mutex);
718 for_each_possible_cpu(cpu) {
721 free_page((unsigned long)per_cpu_ptr(uprobe_cpu_buffer, cpu)->buf);
724 free_percpu(uprobe_cpu_buffer);
728 static int uprobe_buffer_enable(void)
732 BUG_ON(!mutex_is_locked(&event_mutex));
734 if (uprobe_buffer_refcnt++ == 0) {
735 ret = uprobe_buffer_init();
737 uprobe_buffer_refcnt--;
743 static void uprobe_buffer_disable(void)
747 BUG_ON(!mutex_is_locked(&event_mutex));
749 if (--uprobe_buffer_refcnt == 0) {
750 for_each_possible_cpu(cpu)
751 free_page((unsigned long)per_cpu_ptr(uprobe_cpu_buffer,
754 free_percpu(uprobe_cpu_buffer);
755 uprobe_cpu_buffer = NULL;
759 static struct uprobe_cpu_buffer *uprobe_buffer_get(void)
761 struct uprobe_cpu_buffer *ucb;
764 cpu = raw_smp_processor_id();
765 ucb = per_cpu_ptr(uprobe_cpu_buffer, cpu);
768 * Use per-cpu buffers for fastest access, but we might migrate
769 * so the mutex makes sure we have sole access to it.
771 mutex_lock(&ucb->mutex);
776 static void uprobe_buffer_put(struct uprobe_cpu_buffer *ucb)
778 mutex_unlock(&ucb->mutex);
781 static void __uprobe_trace_func(struct trace_uprobe *tu,
782 unsigned long func, struct pt_regs *regs,
783 struct uprobe_cpu_buffer *ucb, int dsize,
784 struct trace_event_file *trace_file)
786 struct uprobe_trace_entry_head *entry;
787 struct ring_buffer_event *event;
788 struct ring_buffer *buffer;
791 struct trace_event_call *call = &tu->tp.call;
793 WARN_ON(call != trace_file->event_call);
795 if (WARN_ON_ONCE(tu->tp.size + dsize > PAGE_SIZE))
798 if (trace_trigger_soft_disabled(trace_file))
801 esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
802 size = esize + tu->tp.size + dsize;
803 event = trace_event_buffer_lock_reserve(&buffer, trace_file,
804 call->event.type, size, 0, 0);
808 entry = ring_buffer_event_data(event);
809 if (is_ret_probe(tu)) {
810 entry->vaddr[0] = func;
811 entry->vaddr[1] = instruction_pointer(regs);
812 data = DATAOF_TRACE_ENTRY(entry, true);
814 entry->vaddr[0] = instruction_pointer(regs);
815 data = DATAOF_TRACE_ENTRY(entry, false);
818 memcpy(data, ucb->buf, tu->tp.size + dsize);
820 event_trigger_unlock_commit(trace_file, buffer, event, entry, 0, 0);
824 static int uprobe_trace_func(struct trace_uprobe *tu, struct pt_regs *regs,
825 struct uprobe_cpu_buffer *ucb, int dsize)
827 struct event_file_link *link;
829 if (is_ret_probe(tu))
833 list_for_each_entry_rcu(link, &tu->tp.files, list)
834 __uprobe_trace_func(tu, 0, regs, ucb, dsize, link->file);
840 static void uretprobe_trace_func(struct trace_uprobe *tu, unsigned long func,
841 struct pt_regs *regs,
842 struct uprobe_cpu_buffer *ucb, int dsize)
844 struct event_file_link *link;
847 list_for_each_entry_rcu(link, &tu->tp.files, list)
848 __uprobe_trace_func(tu, func, regs, ucb, dsize, link->file);
852 /* Event entry printers */
853 static enum print_line_t
854 print_uprobe_event(struct trace_iterator *iter, int flags, struct trace_event *event)
856 struct uprobe_trace_entry_head *entry;
857 struct trace_seq *s = &iter->seq;
858 struct trace_uprobe *tu;
861 entry = (struct uprobe_trace_entry_head *)iter->ent;
862 tu = container_of(event, struct trace_uprobe, tp.call.event);
864 if (is_ret_probe(tu)) {
865 trace_seq_printf(s, "%s: (0x%lx <- 0x%lx)",
866 trace_event_name(&tu->tp.call),
867 entry->vaddr[1], entry->vaddr[0]);
868 data = DATAOF_TRACE_ENTRY(entry, true);
870 trace_seq_printf(s, "%s: (0x%lx)",
871 trace_event_name(&tu->tp.call),
873 data = DATAOF_TRACE_ENTRY(entry, false);
876 if (print_probe_args(s, tu->tp.args, tu->tp.nr_args, data, entry) < 0)
879 trace_seq_putc(s, '\n');
882 return trace_handle_return(s);
885 typedef bool (*filter_func_t)(struct uprobe_consumer *self,
886 enum uprobe_filter_ctx ctx,
887 struct mm_struct *mm);
890 probe_event_enable(struct trace_uprobe *tu, struct trace_event_file *file,
891 filter_func_t filter)
893 bool enabled = trace_probe_is_enabled(&tu->tp);
894 struct event_file_link *link = NULL;
898 if (tu->tp.flags & TP_FLAG_PROFILE)
901 link = kmalloc(sizeof(*link), GFP_KERNEL);
906 list_add_tail_rcu(&link->list, &tu->tp.files);
908 tu->tp.flags |= TP_FLAG_TRACE;
910 if (tu->tp.flags & TP_FLAG_TRACE)
913 tu->tp.flags |= TP_FLAG_PROFILE;
916 WARN_ON(!uprobe_filter_is_empty(&tu->filter));
921 ret = uprobe_buffer_enable();
925 tu->consumer.filter = filter;
926 tu->inode = d_real_inode(tu->path.dentry);
927 if (tu->ref_ctr_offset) {
928 ret = uprobe_register_refctr(tu->inode, tu->offset,
929 tu->ref_ctr_offset, &tu->consumer);
931 ret = uprobe_register(tu->inode, tu->offset, &tu->consumer);
940 uprobe_buffer_disable();
944 list_del(&link->list);
946 tu->tp.flags &= ~TP_FLAG_TRACE;
948 tu->tp.flags &= ~TP_FLAG_PROFILE;
954 probe_event_disable(struct trace_uprobe *tu, struct trace_event_file *file)
956 if (!trace_probe_is_enabled(&tu->tp))
960 struct event_file_link *link;
962 link = find_event_file_link(&tu->tp, file);
966 list_del_rcu(&link->list);
967 /* synchronize with u{,ret}probe_trace_func */
971 if (!list_empty(&tu->tp.files))
975 WARN_ON(!uprobe_filter_is_empty(&tu->filter));
977 uprobe_unregister(tu->inode, tu->offset, &tu->consumer);
979 tu->tp.flags &= file ? ~TP_FLAG_TRACE : ~TP_FLAG_PROFILE;
981 uprobe_buffer_disable();
984 static int uprobe_event_define_fields(struct trace_event_call *event_call)
987 struct uprobe_trace_entry_head field;
988 struct trace_uprobe *tu = event_call->data;
990 if (is_ret_probe(tu)) {
991 DEFINE_FIELD(unsigned long, vaddr[0], FIELD_STRING_FUNC, 0);
992 DEFINE_FIELD(unsigned long, vaddr[1], FIELD_STRING_RETIP, 0);
993 size = SIZEOF_TRACE_ENTRY(true);
995 DEFINE_FIELD(unsigned long, vaddr[0], FIELD_STRING_IP, 0);
996 size = SIZEOF_TRACE_ENTRY(false);
999 return traceprobe_define_arg_fields(event_call, size, &tu->tp);
1002 #ifdef CONFIG_PERF_EVENTS
1004 __uprobe_perf_filter(struct trace_uprobe_filter *filter, struct mm_struct *mm)
1006 struct perf_event *event;
1008 if (filter->nr_systemwide)
1011 list_for_each_entry(event, &filter->perf_events, hw.tp_list) {
1012 if (event->hw.target->mm == mm)
1020 uprobe_filter_event(struct trace_uprobe *tu, struct perf_event *event)
1022 return __uprobe_perf_filter(&tu->filter, event->hw.target->mm);
1025 static int uprobe_perf_close(struct trace_uprobe *tu, struct perf_event *event)
1029 write_lock(&tu->filter.rwlock);
1030 if (event->hw.target) {
1031 list_del(&event->hw.tp_list);
1032 done = tu->filter.nr_systemwide ||
1033 (event->hw.target->flags & PF_EXITING) ||
1034 uprobe_filter_event(tu, event);
1036 tu->filter.nr_systemwide--;
1037 done = tu->filter.nr_systemwide;
1039 write_unlock(&tu->filter.rwlock);
1042 return uprobe_apply(tu->inode, tu->offset, &tu->consumer, false);
1047 static int uprobe_perf_open(struct trace_uprobe *tu, struct perf_event *event)
1052 write_lock(&tu->filter.rwlock);
1053 if (event->hw.target) {
1055 * event->parent != NULL means copy_process(), we can avoid
1056 * uprobe_apply(). current->mm must be probed and we can rely
1057 * on dup_mmap() which preserves the already installed bp's.
1059 * attr.enable_on_exec means that exec/mmap will install the
1060 * breakpoints we need.
1062 done = tu->filter.nr_systemwide ||
1063 event->parent || event->attr.enable_on_exec ||
1064 uprobe_filter_event(tu, event);
1065 list_add(&event->hw.tp_list, &tu->filter.perf_events);
1067 done = tu->filter.nr_systemwide;
1068 tu->filter.nr_systemwide++;
1070 write_unlock(&tu->filter.rwlock);
1074 err = uprobe_apply(tu->inode, tu->offset, &tu->consumer, true);
1076 uprobe_perf_close(tu, event);
1081 static bool uprobe_perf_filter(struct uprobe_consumer *uc,
1082 enum uprobe_filter_ctx ctx, struct mm_struct *mm)
1084 struct trace_uprobe *tu;
1087 tu = container_of(uc, struct trace_uprobe, consumer);
1088 read_lock(&tu->filter.rwlock);
1089 ret = __uprobe_perf_filter(&tu->filter, mm);
1090 read_unlock(&tu->filter.rwlock);
1095 static void __uprobe_perf_func(struct trace_uprobe *tu,
1096 unsigned long func, struct pt_regs *regs,
1097 struct uprobe_cpu_buffer *ucb, int dsize)
1099 struct trace_event_call *call = &tu->tp.call;
1100 struct uprobe_trace_entry_head *entry;
1101 struct hlist_head *head;
1106 if (bpf_prog_array_valid(call) && !trace_call_bpf(call, regs))
1109 esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
1111 size = esize + tu->tp.size + dsize;
1112 size = ALIGN(size + sizeof(u32), sizeof(u64)) - sizeof(u32);
1113 if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE, "profile buffer not large enough"))
1117 head = this_cpu_ptr(call->perf_events);
1118 if (hlist_empty(head))
1121 entry = perf_trace_buf_alloc(size, NULL, &rctx);
1125 if (is_ret_probe(tu)) {
1126 entry->vaddr[0] = func;
1127 entry->vaddr[1] = instruction_pointer(regs);
1128 data = DATAOF_TRACE_ENTRY(entry, true);
1130 entry->vaddr[0] = instruction_pointer(regs);
1131 data = DATAOF_TRACE_ENTRY(entry, false);
1134 memcpy(data, ucb->buf, tu->tp.size + dsize);
1136 if (size - esize > tu->tp.size + dsize) {
1137 int len = tu->tp.size + dsize;
1139 memset(data + len, 0, size - esize - len);
1142 perf_trace_buf_submit(entry, size, rctx, call->event.type, 1, regs,
1148 /* uprobe profile handler */
1149 static int uprobe_perf_func(struct trace_uprobe *tu, struct pt_regs *regs,
1150 struct uprobe_cpu_buffer *ucb, int dsize)
1152 if (!uprobe_perf_filter(&tu->consumer, 0, current->mm))
1153 return UPROBE_HANDLER_REMOVE;
1155 if (!is_ret_probe(tu))
1156 __uprobe_perf_func(tu, 0, regs, ucb, dsize);
1160 static void uretprobe_perf_func(struct trace_uprobe *tu, unsigned long func,
1161 struct pt_regs *regs,
1162 struct uprobe_cpu_buffer *ucb, int dsize)
1164 __uprobe_perf_func(tu, func, regs, ucb, dsize);
1167 int bpf_get_uprobe_info(const struct perf_event *event, u32 *fd_type,
1168 const char **filename, u64 *probe_offset,
1169 bool perf_type_tracepoint)
1171 const char *pevent = trace_event_name(event->tp_event);
1172 const char *group = event->tp_event->class->system;
1173 struct trace_uprobe *tu;
1175 if (perf_type_tracepoint)
1176 tu = find_probe_event(pevent, group);
1178 tu = event->tp_event->data;
1182 *fd_type = is_ret_probe(tu) ? BPF_FD_TYPE_URETPROBE
1183 : BPF_FD_TYPE_UPROBE;
1184 *filename = tu->filename;
1185 *probe_offset = tu->offset;
1188 #endif /* CONFIG_PERF_EVENTS */
1191 trace_uprobe_register(struct trace_event_call *event, enum trace_reg type,
1194 struct trace_uprobe *tu = event->data;
1195 struct trace_event_file *file = data;
1198 case TRACE_REG_REGISTER:
1199 return probe_event_enable(tu, file, NULL);
1201 case TRACE_REG_UNREGISTER:
1202 probe_event_disable(tu, file);
1205 #ifdef CONFIG_PERF_EVENTS
1206 case TRACE_REG_PERF_REGISTER:
1207 return probe_event_enable(tu, NULL, uprobe_perf_filter);
1209 case TRACE_REG_PERF_UNREGISTER:
1210 probe_event_disable(tu, NULL);
1213 case TRACE_REG_PERF_OPEN:
1214 return uprobe_perf_open(tu, data);
1216 case TRACE_REG_PERF_CLOSE:
1217 return uprobe_perf_close(tu, data);
1226 static int uprobe_dispatcher(struct uprobe_consumer *con, struct pt_regs *regs)
1228 struct trace_uprobe *tu;
1229 struct uprobe_dispatch_data udd;
1230 struct uprobe_cpu_buffer *ucb;
1235 tu = container_of(con, struct trace_uprobe, consumer);
1239 udd.bp_addr = instruction_pointer(regs);
1241 current->utask->vaddr = (unsigned long) &udd;
1243 if (WARN_ON_ONCE(!uprobe_cpu_buffer))
1246 dsize = __get_data_size(&tu->tp, regs);
1247 esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
1249 ucb = uprobe_buffer_get();
1250 store_trace_args(ucb->buf, &tu->tp, regs, esize, dsize);
1252 if (tu->tp.flags & TP_FLAG_TRACE)
1253 ret |= uprobe_trace_func(tu, regs, ucb, dsize);
1255 #ifdef CONFIG_PERF_EVENTS
1256 if (tu->tp.flags & TP_FLAG_PROFILE)
1257 ret |= uprobe_perf_func(tu, regs, ucb, dsize);
1259 uprobe_buffer_put(ucb);
1263 static int uretprobe_dispatcher(struct uprobe_consumer *con,
1264 unsigned long func, struct pt_regs *regs)
1266 struct trace_uprobe *tu;
1267 struct uprobe_dispatch_data udd;
1268 struct uprobe_cpu_buffer *ucb;
1271 tu = container_of(con, struct trace_uprobe, consumer);
1276 current->utask->vaddr = (unsigned long) &udd;
1278 if (WARN_ON_ONCE(!uprobe_cpu_buffer))
1281 dsize = __get_data_size(&tu->tp, regs);
1282 esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
1284 ucb = uprobe_buffer_get();
1285 store_trace_args(ucb->buf, &tu->tp, regs, esize, dsize);
1287 if (tu->tp.flags & TP_FLAG_TRACE)
1288 uretprobe_trace_func(tu, func, regs, ucb, dsize);
1290 #ifdef CONFIG_PERF_EVENTS
1291 if (tu->tp.flags & TP_FLAG_PROFILE)
1292 uretprobe_perf_func(tu, func, regs, ucb, dsize);
1294 uprobe_buffer_put(ucb);
1298 static struct trace_event_functions uprobe_funcs = {
1299 .trace = print_uprobe_event
1302 static inline void init_trace_event_call(struct trace_uprobe *tu,
1303 struct trace_event_call *call)
1305 INIT_LIST_HEAD(&call->class->fields);
1306 call->event.funcs = &uprobe_funcs;
1307 call->class->define_fields = uprobe_event_define_fields;
1309 call->flags = TRACE_EVENT_FL_UPROBE;
1310 call->class->reg = trace_uprobe_register;
1314 static int register_uprobe_event(struct trace_uprobe *tu)
1316 struct trace_event_call *call = &tu->tp.call;
1319 init_trace_event_call(tu, call);
1321 if (traceprobe_set_print_fmt(&tu->tp, is_ret_probe(tu)) < 0)
1324 ret = register_trace_event(&call->event);
1326 kfree(call->print_fmt);
1330 ret = trace_add_event_call(call);
1333 pr_info("Failed to register uprobe event: %s\n",
1334 trace_event_name(call));
1335 kfree(call->print_fmt);
1336 unregister_trace_event(&call->event);
1342 static int unregister_uprobe_event(struct trace_uprobe *tu)
1346 /* tu->event is unregistered in trace_remove_event_call() */
1347 ret = trace_remove_event_call(&tu->tp.call);
1350 kfree(tu->tp.call.print_fmt);
1351 tu->tp.call.print_fmt = NULL;
1355 #ifdef CONFIG_PERF_EVENTS
1356 struct trace_event_call *
1357 create_local_trace_uprobe(char *name, unsigned long offs,
1358 unsigned long ref_ctr_offset, bool is_return)
1360 struct trace_uprobe *tu;
1364 ret = kern_path(name, LOOKUP_FOLLOW, &path);
1366 return ERR_PTR(ret);
1368 if (!d_is_reg(path.dentry)) {
1370 return ERR_PTR(-EINVAL);
1374 * local trace_kprobes are not added to dyn_event, so they are never
1375 * searched in find_trace_kprobe(). Therefore, there is no concern of
1376 * duplicated name "DUMMY_EVENT" here.
1378 tu = alloc_trace_uprobe(UPROBE_EVENT_SYSTEM, "DUMMY_EVENT", 0,
1382 pr_info("Failed to allocate trace_uprobe.(%d)\n",
1385 return ERR_CAST(tu);
1390 tu->ref_ctr_offset = ref_ctr_offset;
1391 tu->filename = kstrdup(name, GFP_KERNEL);
1392 init_trace_event_call(tu, &tu->tp.call);
1394 if (traceprobe_set_print_fmt(&tu->tp, is_ret_probe(tu)) < 0) {
1399 return &tu->tp.call;
1401 free_trace_uprobe(tu);
1402 return ERR_PTR(ret);
1405 void destroy_local_trace_uprobe(struct trace_event_call *event_call)
1407 struct trace_uprobe *tu;
1409 tu = container_of(event_call, struct trace_uprobe, tp.call);
1411 kfree(tu->tp.call.print_fmt);
1412 tu->tp.call.print_fmt = NULL;
1414 free_trace_uprobe(tu);
1416 #endif /* CONFIG_PERF_EVENTS */
1418 /* Make a trace interface for controling probe points */
1419 static __init int init_uprobe_trace(void)
1421 struct dentry *d_tracer;
1424 ret = dyn_event_register(&trace_uprobe_ops);
1428 d_tracer = tracing_init_dentry();
1429 if (IS_ERR(d_tracer))
1432 trace_create_file("uprobe_events", 0644, d_tracer,
1433 NULL, &uprobe_events_ops);
1434 /* Profile interface */
1435 trace_create_file("uprobe_profile", 0444, d_tracer,
1436 NULL, &uprobe_profile_ops);
1440 fs_initcall(init_uprobe_trace);