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 || !group)
277 return ERR_PTR(-EINVAL);
279 tu = kzalloc(SIZEOF_TRACE_UPROBE(nargs), GFP_KERNEL);
281 return ERR_PTR(-ENOMEM);
283 tu->tp.call.class = &tu->tp.class;
284 tu->tp.call.name = kstrdup(event, GFP_KERNEL);
285 if (!tu->tp.call.name)
288 tu->tp.class.system = kstrdup(group, GFP_KERNEL);
289 if (!tu->tp.class.system)
292 dyn_event_init(&tu->devent, &trace_uprobe_ops);
293 INIT_LIST_HEAD(&tu->tp.files);
294 tu->consumer.handler = uprobe_dispatcher;
296 tu->consumer.ret_handler = uretprobe_dispatcher;
297 init_trace_uprobe_filter(&tu->filter);
301 kfree(tu->tp.call.name);
304 return ERR_PTR(-ENOMEM);
307 static void free_trace_uprobe(struct trace_uprobe *tu)
314 for (i = 0; i < tu->tp.nr_args; i++)
315 traceprobe_free_probe_arg(&tu->tp.args[i]);
318 kfree(tu->tp.call.class->system);
319 kfree(tu->tp.call.name);
324 static struct trace_uprobe *find_probe_event(const char *event, const char *group)
326 struct dyn_event *pos;
327 struct trace_uprobe *tu;
329 for_each_trace_uprobe(tu, pos)
330 if (strcmp(trace_event_name(&tu->tp.call), event) == 0 &&
331 strcmp(tu->tp.call.class->system, group) == 0)
337 /* Unregister a trace_uprobe and probe_event */
338 static int unregister_trace_uprobe(struct trace_uprobe *tu)
342 ret = unregister_uprobe_event(tu);
346 dyn_event_remove(&tu->devent);
347 free_trace_uprobe(tu);
352 * Uprobe with multiple reference counter is not allowed. i.e.
353 * If inode and offset matches, reference counter offset *must*
354 * match as well. Though, there is one exception: If user is
355 * replacing old trace_uprobe with new one(same group/event),
356 * then we allow same uprobe with new reference counter as far
357 * as the new one does not conflict with any other existing
360 static struct trace_uprobe *find_old_trace_uprobe(struct trace_uprobe *new)
362 struct dyn_event *pos;
363 struct trace_uprobe *tmp, *old = NULL;
364 struct inode *new_inode = d_real_inode(new->path.dentry);
366 old = find_probe_event(trace_event_name(&new->tp.call),
367 new->tp.call.class->system);
369 for_each_trace_uprobe(tmp, pos) {
370 if ((old ? old != tmp : true) &&
371 new_inode == d_real_inode(tmp->path.dentry) &&
372 new->offset == tmp->offset &&
373 new->ref_ctr_offset != tmp->ref_ctr_offset) {
374 pr_warn("Reference counter offset mismatch.");
375 return ERR_PTR(-EINVAL);
381 /* Register a trace_uprobe and probe_event */
382 static int register_trace_uprobe(struct trace_uprobe *tu)
384 struct trace_uprobe *old_tu;
387 mutex_lock(&event_mutex);
389 /* register as an event */
390 old_tu = find_old_trace_uprobe(tu);
391 if (IS_ERR(old_tu)) {
392 ret = PTR_ERR(old_tu);
397 /* delete old event */
398 ret = unregister_trace_uprobe(old_tu);
403 ret = register_uprobe_event(tu);
405 pr_warn("Failed to register probe event(%d)\n", ret);
409 dyn_event_add(&tu->devent);
412 mutex_unlock(&event_mutex);
419 * - Add uprobe: p|r[:[GRP/]EVENT] PATH:OFFSET [FETCHARGS]
421 * - Remove uprobe: -:[GRP/]EVENT
423 static int trace_uprobe_create(int argc, const char **argv)
425 struct trace_uprobe *tu;
426 const char *event = NULL, *group = UPROBE_EVENT_SYSTEM;
427 char *arg, *filename, *rctr, *rctr_end, *tmp;
428 char buf[MAX_EVENT_NAME_LEN];
430 unsigned long offset, ref_ctr_offset;
431 bool is_return = false;
437 /* argc must be >= 1 */
438 if (argv[0][0] == 'r')
440 else if (argv[0][0] != 'p' || argc < 2)
443 if (argv[0][1] == ':')
446 if (!strchr(argv[1], '/'))
449 filename = kstrdup(argv[1], GFP_KERNEL);
453 /* Find the last occurrence, in case the path contains ':' too. */
454 arg = strrchr(filename, ':');
455 if (!arg || !isdigit(arg[1])) {
461 ret = kern_path(filename, LOOKUP_FOLLOW, &path);
466 if (!d_is_reg(path.dentry)) {
468 goto fail_address_parse;
471 /* Parse reference counter offset if specified. */
472 rctr = strchr(arg, '(');
474 rctr_end = strchr(rctr, ')');
475 if (rctr > rctr_end || *(rctr_end + 1) != 0) {
477 pr_info("Invalid reference counter offset.\n");
478 goto fail_address_parse;
483 ret = kstrtoul(rctr, 0, &ref_ctr_offset);
485 pr_info("Invalid reference counter offset.\n");
486 goto fail_address_parse;
490 /* Parse uprobe offset. */
491 ret = kstrtoul(arg, 0, &offset);
493 goto fail_address_parse;
500 ret = traceprobe_parse_event_name(&event, &group, buf);
502 goto fail_address_parse;
507 tail = kstrdup(kbasename(filename), GFP_KERNEL);
510 goto fail_address_parse;
513 ptr = strpbrk(tail, ".-_");
517 snprintf(buf, MAX_EVENT_NAME_LEN, "%c_%s_0x%lx", 'p', tail, offset);
522 tu = alloc_trace_uprobe(group, event, argc, is_return);
525 /* This must return -ENOMEM otherwise there is a bug */
526 WARN_ON_ONCE(ret != -ENOMEM);
527 goto fail_address_parse;
530 tu->ref_ctr_offset = ref_ctr_offset;
532 tu->filename = filename;
534 /* parse arguments */
535 for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
536 tmp = kstrdup(argv[i], GFP_KERNEL);
542 ret = traceprobe_parse_probe_arg(&tu->tp, i, tmp,
543 is_return ? TPARG_FL_RETURN : 0);
549 ret = register_trace_uprobe(tu);
555 free_trace_uprobe(tu);
562 pr_info("Failed to parse address or file.\n");
567 static int create_or_delete_trace_uprobe(int argc, char **argv)
571 if (argv[0][0] == '-')
572 return dyn_event_release(argc, argv, &trace_uprobe_ops);
574 ret = trace_uprobe_create(argc, (const char **)argv);
575 return ret == -ECANCELED ? -EINVAL : ret;
578 static int trace_uprobe_release(struct dyn_event *ev)
580 struct trace_uprobe *tu = to_trace_uprobe(ev);
582 return unregister_trace_uprobe(tu);
585 /* Probes listing interfaces */
586 static int trace_uprobe_show(struct seq_file *m, struct dyn_event *ev)
588 struct trace_uprobe *tu = to_trace_uprobe(ev);
589 char c = is_ret_probe(tu) ? 'r' : 'p';
592 seq_printf(m, "%c:%s/%s %s:0x%0*lx", c, tu->tp.call.class->system,
593 trace_event_name(&tu->tp.call), tu->filename,
594 (int)(sizeof(void *) * 2), tu->offset);
596 if (tu->ref_ctr_offset)
597 seq_printf(m, "(0x%lx)", tu->ref_ctr_offset);
599 for (i = 0; i < tu->tp.nr_args; i++)
600 seq_printf(m, " %s=%s", tu->tp.args[i].name, tu->tp.args[i].comm);
606 static int probes_seq_show(struct seq_file *m, void *v)
608 struct dyn_event *ev = v;
610 if (!is_trace_uprobe(ev))
613 return trace_uprobe_show(m, ev);
616 static const struct seq_operations probes_seq_op = {
617 .start = dyn_event_seq_start,
618 .next = dyn_event_seq_next,
619 .stop = dyn_event_seq_stop,
620 .show = probes_seq_show
623 static int probes_open(struct inode *inode, struct file *file)
627 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
628 ret = dyn_events_release_all(&trace_uprobe_ops);
633 return seq_open(file, &probes_seq_op);
636 static ssize_t probes_write(struct file *file, const char __user *buffer,
637 size_t count, loff_t *ppos)
639 return trace_parse_run_command(file, buffer, count, ppos,
640 create_or_delete_trace_uprobe);
643 static const struct file_operations uprobe_events_ops = {
644 .owner = THIS_MODULE,
648 .release = seq_release,
649 .write = probes_write,
652 /* Probes profiling interfaces */
653 static int probes_profile_seq_show(struct seq_file *m, void *v)
655 struct dyn_event *ev = v;
656 struct trace_uprobe *tu;
658 if (!is_trace_uprobe(ev))
661 tu = to_trace_uprobe(ev);
662 seq_printf(m, " %s %-44s %15lu\n", tu->filename,
663 trace_event_name(&tu->tp.call), tu->nhit);
667 static const struct seq_operations profile_seq_op = {
668 .start = dyn_event_seq_start,
669 .next = dyn_event_seq_next,
670 .stop = dyn_event_seq_stop,
671 .show = probes_profile_seq_show
674 static int profile_open(struct inode *inode, struct file *file)
676 return seq_open(file, &profile_seq_op);
679 static const struct file_operations uprobe_profile_ops = {
680 .owner = THIS_MODULE,
681 .open = profile_open,
684 .release = seq_release,
687 struct uprobe_cpu_buffer {
691 static struct uprobe_cpu_buffer __percpu *uprobe_cpu_buffer;
692 static int uprobe_buffer_refcnt;
694 static int uprobe_buffer_init(void)
698 uprobe_cpu_buffer = alloc_percpu(struct uprobe_cpu_buffer);
699 if (uprobe_cpu_buffer == NULL)
702 for_each_possible_cpu(cpu) {
703 struct page *p = alloc_pages_node(cpu_to_node(cpu),
709 per_cpu_ptr(uprobe_cpu_buffer, cpu)->buf = page_address(p);
710 mutex_init(&per_cpu_ptr(uprobe_cpu_buffer, cpu)->mutex);
716 for_each_possible_cpu(cpu) {
719 free_page((unsigned long)per_cpu_ptr(uprobe_cpu_buffer, cpu)->buf);
722 free_percpu(uprobe_cpu_buffer);
726 static int uprobe_buffer_enable(void)
730 BUG_ON(!mutex_is_locked(&event_mutex));
732 if (uprobe_buffer_refcnt++ == 0) {
733 ret = uprobe_buffer_init();
735 uprobe_buffer_refcnt--;
741 static void uprobe_buffer_disable(void)
745 BUG_ON(!mutex_is_locked(&event_mutex));
747 if (--uprobe_buffer_refcnt == 0) {
748 for_each_possible_cpu(cpu)
749 free_page((unsigned long)per_cpu_ptr(uprobe_cpu_buffer,
752 free_percpu(uprobe_cpu_buffer);
753 uprobe_cpu_buffer = NULL;
757 static struct uprobe_cpu_buffer *uprobe_buffer_get(void)
759 struct uprobe_cpu_buffer *ucb;
762 cpu = raw_smp_processor_id();
763 ucb = per_cpu_ptr(uprobe_cpu_buffer, cpu);
766 * Use per-cpu buffers for fastest access, but we might migrate
767 * so the mutex makes sure we have sole access to it.
769 mutex_lock(&ucb->mutex);
774 static void uprobe_buffer_put(struct uprobe_cpu_buffer *ucb)
776 mutex_unlock(&ucb->mutex);
779 static void __uprobe_trace_func(struct trace_uprobe *tu,
780 unsigned long func, struct pt_regs *regs,
781 struct uprobe_cpu_buffer *ucb, int dsize,
782 struct trace_event_file *trace_file)
784 struct uprobe_trace_entry_head *entry;
785 struct ring_buffer_event *event;
786 struct ring_buffer *buffer;
789 struct trace_event_call *call = &tu->tp.call;
791 WARN_ON(call != trace_file->event_call);
793 if (WARN_ON_ONCE(tu->tp.size + dsize > PAGE_SIZE))
796 if (trace_trigger_soft_disabled(trace_file))
799 esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
800 size = esize + tu->tp.size + dsize;
801 event = trace_event_buffer_lock_reserve(&buffer, trace_file,
802 call->event.type, size, 0, 0);
806 entry = ring_buffer_event_data(event);
807 if (is_ret_probe(tu)) {
808 entry->vaddr[0] = func;
809 entry->vaddr[1] = instruction_pointer(regs);
810 data = DATAOF_TRACE_ENTRY(entry, true);
812 entry->vaddr[0] = instruction_pointer(regs);
813 data = DATAOF_TRACE_ENTRY(entry, false);
816 memcpy(data, ucb->buf, tu->tp.size + dsize);
818 event_trigger_unlock_commit(trace_file, buffer, event, entry, 0, 0);
822 static int uprobe_trace_func(struct trace_uprobe *tu, struct pt_regs *regs,
823 struct uprobe_cpu_buffer *ucb, int dsize)
825 struct event_file_link *link;
827 if (is_ret_probe(tu))
831 list_for_each_entry_rcu(link, &tu->tp.files, list)
832 __uprobe_trace_func(tu, 0, regs, ucb, dsize, link->file);
838 static void uretprobe_trace_func(struct trace_uprobe *tu, unsigned long func,
839 struct pt_regs *regs,
840 struct uprobe_cpu_buffer *ucb, int dsize)
842 struct event_file_link *link;
845 list_for_each_entry_rcu(link, &tu->tp.files, list)
846 __uprobe_trace_func(tu, func, regs, ucb, dsize, link->file);
850 /* Event entry printers */
851 static enum print_line_t
852 print_uprobe_event(struct trace_iterator *iter, int flags, struct trace_event *event)
854 struct uprobe_trace_entry_head *entry;
855 struct trace_seq *s = &iter->seq;
856 struct trace_uprobe *tu;
859 entry = (struct uprobe_trace_entry_head *)iter->ent;
860 tu = container_of(event, struct trace_uprobe, tp.call.event);
862 if (is_ret_probe(tu)) {
863 trace_seq_printf(s, "%s: (0x%lx <- 0x%lx)",
864 trace_event_name(&tu->tp.call),
865 entry->vaddr[1], entry->vaddr[0]);
866 data = DATAOF_TRACE_ENTRY(entry, true);
868 trace_seq_printf(s, "%s: (0x%lx)",
869 trace_event_name(&tu->tp.call),
871 data = DATAOF_TRACE_ENTRY(entry, false);
874 if (print_probe_args(s, tu->tp.args, tu->tp.nr_args, data, entry) < 0)
877 trace_seq_putc(s, '\n');
880 return trace_handle_return(s);
883 typedef bool (*filter_func_t)(struct uprobe_consumer *self,
884 enum uprobe_filter_ctx ctx,
885 struct mm_struct *mm);
888 probe_event_enable(struct trace_uprobe *tu, struct trace_event_file *file,
889 filter_func_t filter)
891 bool enabled = trace_probe_is_enabled(&tu->tp);
892 struct event_file_link *link = NULL;
896 if (tu->tp.flags & TP_FLAG_PROFILE)
899 link = kmalloc(sizeof(*link), GFP_KERNEL);
904 list_add_tail_rcu(&link->list, &tu->tp.files);
906 tu->tp.flags |= TP_FLAG_TRACE;
908 if (tu->tp.flags & TP_FLAG_TRACE)
911 tu->tp.flags |= TP_FLAG_PROFILE;
914 WARN_ON(!uprobe_filter_is_empty(&tu->filter));
919 ret = uprobe_buffer_enable();
923 tu->consumer.filter = filter;
924 tu->inode = d_real_inode(tu->path.dentry);
925 if (tu->ref_ctr_offset) {
926 ret = uprobe_register_refctr(tu->inode, tu->offset,
927 tu->ref_ctr_offset, &tu->consumer);
929 ret = uprobe_register(tu->inode, tu->offset, &tu->consumer);
938 uprobe_buffer_disable();
942 list_del(&link->list);
944 tu->tp.flags &= ~TP_FLAG_TRACE;
946 tu->tp.flags &= ~TP_FLAG_PROFILE;
952 probe_event_disable(struct trace_uprobe *tu, struct trace_event_file *file)
954 if (!trace_probe_is_enabled(&tu->tp))
958 struct event_file_link *link;
960 link = find_event_file_link(&tu->tp, file);
964 list_del_rcu(&link->list);
965 /* synchronize with u{,ret}probe_trace_func */
969 if (!list_empty(&tu->tp.files))
973 WARN_ON(!uprobe_filter_is_empty(&tu->filter));
975 uprobe_unregister(tu->inode, tu->offset, &tu->consumer);
977 tu->tp.flags &= file ? ~TP_FLAG_TRACE : ~TP_FLAG_PROFILE;
979 uprobe_buffer_disable();
982 static int uprobe_event_define_fields(struct trace_event_call *event_call)
985 struct uprobe_trace_entry_head field;
986 struct trace_uprobe *tu = event_call->data;
988 if (is_ret_probe(tu)) {
989 DEFINE_FIELD(unsigned long, vaddr[0], FIELD_STRING_FUNC, 0);
990 DEFINE_FIELD(unsigned long, vaddr[1], FIELD_STRING_RETIP, 0);
991 size = SIZEOF_TRACE_ENTRY(true);
993 DEFINE_FIELD(unsigned long, vaddr[0], FIELD_STRING_IP, 0);
994 size = SIZEOF_TRACE_ENTRY(false);
997 return traceprobe_define_arg_fields(event_call, size, &tu->tp);
1000 #ifdef CONFIG_PERF_EVENTS
1002 __uprobe_perf_filter(struct trace_uprobe_filter *filter, struct mm_struct *mm)
1004 struct perf_event *event;
1006 if (filter->nr_systemwide)
1009 list_for_each_entry(event, &filter->perf_events, hw.tp_list) {
1010 if (event->hw.target->mm == mm)
1018 uprobe_filter_event(struct trace_uprobe *tu, struct perf_event *event)
1020 return __uprobe_perf_filter(&tu->filter, event->hw.target->mm);
1023 static int uprobe_perf_close(struct trace_uprobe *tu, struct perf_event *event)
1027 write_lock(&tu->filter.rwlock);
1028 if (event->hw.target) {
1029 list_del(&event->hw.tp_list);
1030 done = tu->filter.nr_systemwide ||
1031 (event->hw.target->flags & PF_EXITING) ||
1032 uprobe_filter_event(tu, event);
1034 tu->filter.nr_systemwide--;
1035 done = tu->filter.nr_systemwide;
1037 write_unlock(&tu->filter.rwlock);
1040 return uprobe_apply(tu->inode, tu->offset, &tu->consumer, false);
1045 static int uprobe_perf_open(struct trace_uprobe *tu, struct perf_event *event)
1050 write_lock(&tu->filter.rwlock);
1051 if (event->hw.target) {
1053 * event->parent != NULL means copy_process(), we can avoid
1054 * uprobe_apply(). current->mm must be probed and we can rely
1055 * on dup_mmap() which preserves the already installed bp's.
1057 * attr.enable_on_exec means that exec/mmap will install the
1058 * breakpoints we need.
1060 done = tu->filter.nr_systemwide ||
1061 event->parent || event->attr.enable_on_exec ||
1062 uprobe_filter_event(tu, event);
1063 list_add(&event->hw.tp_list, &tu->filter.perf_events);
1065 done = tu->filter.nr_systemwide;
1066 tu->filter.nr_systemwide++;
1068 write_unlock(&tu->filter.rwlock);
1072 err = uprobe_apply(tu->inode, tu->offset, &tu->consumer, true);
1074 uprobe_perf_close(tu, event);
1079 static bool uprobe_perf_filter(struct uprobe_consumer *uc,
1080 enum uprobe_filter_ctx ctx, struct mm_struct *mm)
1082 struct trace_uprobe *tu;
1085 tu = container_of(uc, struct trace_uprobe, consumer);
1086 read_lock(&tu->filter.rwlock);
1087 ret = __uprobe_perf_filter(&tu->filter, mm);
1088 read_unlock(&tu->filter.rwlock);
1093 static void __uprobe_perf_func(struct trace_uprobe *tu,
1094 unsigned long func, struct pt_regs *regs,
1095 struct uprobe_cpu_buffer *ucb, int dsize)
1097 struct trace_event_call *call = &tu->tp.call;
1098 struct uprobe_trace_entry_head *entry;
1099 struct hlist_head *head;
1104 if (bpf_prog_array_valid(call) && !trace_call_bpf(call, regs))
1107 esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
1109 size = esize + tu->tp.size + dsize;
1110 size = ALIGN(size + sizeof(u32), sizeof(u64)) - sizeof(u32);
1111 if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE, "profile buffer not large enough"))
1115 head = this_cpu_ptr(call->perf_events);
1116 if (hlist_empty(head))
1119 entry = perf_trace_buf_alloc(size, NULL, &rctx);
1123 if (is_ret_probe(tu)) {
1124 entry->vaddr[0] = func;
1125 entry->vaddr[1] = instruction_pointer(regs);
1126 data = DATAOF_TRACE_ENTRY(entry, true);
1128 entry->vaddr[0] = instruction_pointer(regs);
1129 data = DATAOF_TRACE_ENTRY(entry, false);
1132 memcpy(data, ucb->buf, tu->tp.size + dsize);
1134 if (size - esize > tu->tp.size + dsize) {
1135 int len = tu->tp.size + dsize;
1137 memset(data + len, 0, size - esize - len);
1140 perf_trace_buf_submit(entry, size, rctx, call->event.type, 1, regs,
1146 /* uprobe profile handler */
1147 static int uprobe_perf_func(struct trace_uprobe *tu, struct pt_regs *regs,
1148 struct uprobe_cpu_buffer *ucb, int dsize)
1150 if (!uprobe_perf_filter(&tu->consumer, 0, current->mm))
1151 return UPROBE_HANDLER_REMOVE;
1153 if (!is_ret_probe(tu))
1154 __uprobe_perf_func(tu, 0, regs, ucb, dsize);
1158 static void uretprobe_perf_func(struct trace_uprobe *tu, unsigned long func,
1159 struct pt_regs *regs,
1160 struct uprobe_cpu_buffer *ucb, int dsize)
1162 __uprobe_perf_func(tu, func, regs, ucb, dsize);
1165 int bpf_get_uprobe_info(const struct perf_event *event, u32 *fd_type,
1166 const char **filename, u64 *probe_offset,
1167 bool perf_type_tracepoint)
1169 const char *pevent = trace_event_name(event->tp_event);
1170 const char *group = event->tp_event->class->system;
1171 struct trace_uprobe *tu;
1173 if (perf_type_tracepoint)
1174 tu = find_probe_event(pevent, group);
1176 tu = event->tp_event->data;
1180 *fd_type = is_ret_probe(tu) ? BPF_FD_TYPE_URETPROBE
1181 : BPF_FD_TYPE_UPROBE;
1182 *filename = tu->filename;
1183 *probe_offset = tu->offset;
1186 #endif /* CONFIG_PERF_EVENTS */
1189 trace_uprobe_register(struct trace_event_call *event, enum trace_reg type,
1192 struct trace_uprobe *tu = event->data;
1193 struct trace_event_file *file = data;
1196 case TRACE_REG_REGISTER:
1197 return probe_event_enable(tu, file, NULL);
1199 case TRACE_REG_UNREGISTER:
1200 probe_event_disable(tu, file);
1203 #ifdef CONFIG_PERF_EVENTS
1204 case TRACE_REG_PERF_REGISTER:
1205 return probe_event_enable(tu, NULL, uprobe_perf_filter);
1207 case TRACE_REG_PERF_UNREGISTER:
1208 probe_event_disable(tu, NULL);
1211 case TRACE_REG_PERF_OPEN:
1212 return uprobe_perf_open(tu, data);
1214 case TRACE_REG_PERF_CLOSE:
1215 return uprobe_perf_close(tu, data);
1224 static int uprobe_dispatcher(struct uprobe_consumer *con, struct pt_regs *regs)
1226 struct trace_uprobe *tu;
1227 struct uprobe_dispatch_data udd;
1228 struct uprobe_cpu_buffer *ucb;
1233 tu = container_of(con, struct trace_uprobe, consumer);
1237 udd.bp_addr = instruction_pointer(regs);
1239 current->utask->vaddr = (unsigned long) &udd;
1241 if (WARN_ON_ONCE(!uprobe_cpu_buffer))
1244 dsize = __get_data_size(&tu->tp, regs);
1245 esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
1247 ucb = uprobe_buffer_get();
1248 store_trace_args(ucb->buf, &tu->tp, regs, esize, dsize);
1250 if (tu->tp.flags & TP_FLAG_TRACE)
1251 ret |= uprobe_trace_func(tu, regs, ucb, dsize);
1253 #ifdef CONFIG_PERF_EVENTS
1254 if (tu->tp.flags & TP_FLAG_PROFILE)
1255 ret |= uprobe_perf_func(tu, regs, ucb, dsize);
1257 uprobe_buffer_put(ucb);
1261 static int uretprobe_dispatcher(struct uprobe_consumer *con,
1262 unsigned long func, struct pt_regs *regs)
1264 struct trace_uprobe *tu;
1265 struct uprobe_dispatch_data udd;
1266 struct uprobe_cpu_buffer *ucb;
1269 tu = container_of(con, struct trace_uprobe, consumer);
1274 current->utask->vaddr = (unsigned long) &udd;
1276 if (WARN_ON_ONCE(!uprobe_cpu_buffer))
1279 dsize = __get_data_size(&tu->tp, regs);
1280 esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
1282 ucb = uprobe_buffer_get();
1283 store_trace_args(ucb->buf, &tu->tp, regs, esize, dsize);
1285 if (tu->tp.flags & TP_FLAG_TRACE)
1286 uretprobe_trace_func(tu, func, regs, ucb, dsize);
1288 #ifdef CONFIG_PERF_EVENTS
1289 if (tu->tp.flags & TP_FLAG_PROFILE)
1290 uretprobe_perf_func(tu, func, regs, ucb, dsize);
1292 uprobe_buffer_put(ucb);
1296 static struct trace_event_functions uprobe_funcs = {
1297 .trace = print_uprobe_event
1300 static inline void init_trace_event_call(struct trace_uprobe *tu,
1301 struct trace_event_call *call)
1303 INIT_LIST_HEAD(&call->class->fields);
1304 call->event.funcs = &uprobe_funcs;
1305 call->class->define_fields = uprobe_event_define_fields;
1307 call->flags = TRACE_EVENT_FL_UPROBE;
1308 call->class->reg = trace_uprobe_register;
1312 static int register_uprobe_event(struct trace_uprobe *tu)
1314 struct trace_event_call *call = &tu->tp.call;
1317 init_trace_event_call(tu, call);
1319 if (traceprobe_set_print_fmt(&tu->tp, is_ret_probe(tu)) < 0)
1322 ret = register_trace_event(&call->event);
1324 kfree(call->print_fmt);
1328 ret = trace_add_event_call(call);
1331 pr_info("Failed to register uprobe event: %s\n",
1332 trace_event_name(call));
1333 kfree(call->print_fmt);
1334 unregister_trace_event(&call->event);
1340 static int unregister_uprobe_event(struct trace_uprobe *tu)
1344 /* tu->event is unregistered in trace_remove_event_call() */
1345 ret = trace_remove_event_call(&tu->tp.call);
1348 kfree(tu->tp.call.print_fmt);
1349 tu->tp.call.print_fmt = NULL;
1353 #ifdef CONFIG_PERF_EVENTS
1354 struct trace_event_call *
1355 create_local_trace_uprobe(char *name, unsigned long offs,
1356 unsigned long ref_ctr_offset, bool is_return)
1358 struct trace_uprobe *tu;
1362 ret = kern_path(name, LOOKUP_FOLLOW, &path);
1364 return ERR_PTR(ret);
1366 if (!d_is_reg(path.dentry)) {
1368 return ERR_PTR(-EINVAL);
1372 * local trace_kprobes are not added to dyn_event, so they are never
1373 * searched in find_trace_kprobe(). Therefore, there is no concern of
1374 * duplicated name "DUMMY_EVENT" here.
1376 tu = alloc_trace_uprobe(UPROBE_EVENT_SYSTEM, "DUMMY_EVENT", 0,
1380 pr_info("Failed to allocate trace_uprobe.(%d)\n",
1383 return ERR_CAST(tu);
1388 tu->ref_ctr_offset = ref_ctr_offset;
1389 tu->filename = kstrdup(name, GFP_KERNEL);
1390 init_trace_event_call(tu, &tu->tp.call);
1392 if (traceprobe_set_print_fmt(&tu->tp, is_ret_probe(tu)) < 0) {
1397 return &tu->tp.call;
1399 free_trace_uprobe(tu);
1400 return ERR_PTR(ret);
1403 void destroy_local_trace_uprobe(struct trace_event_call *event_call)
1405 struct trace_uprobe *tu;
1407 tu = container_of(event_call, struct trace_uprobe, tp.call);
1409 kfree(tu->tp.call.print_fmt);
1410 tu->tp.call.print_fmt = NULL;
1412 free_trace_uprobe(tu);
1414 #endif /* CONFIG_PERF_EVENTS */
1416 /* Make a trace interface for controling probe points */
1417 static __init int init_uprobe_trace(void)
1419 struct dentry *d_tracer;
1422 ret = dyn_event_register(&trace_uprobe_ops);
1426 d_tracer = tracing_init_dentry();
1427 if (IS_ERR(d_tracer))
1430 trace_create_file("uprobe_events", 0644, d_tracer,
1431 NULL, &uprobe_events_ops);
1432 /* Profile interface */
1433 trace_create_file("uprobe_profile", 0444, d_tracer,
1434 NULL, &uprobe_profile_ops);
1438 fs_initcall(init_uprobe_trace);