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_kprobe: " 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);
163 *(u32 *)dest = make_data_loc(ret, (void *)dst - base);
169 /* Return the length of string -- including null terminal byte */
170 static nokprobe_inline int
171 fetch_store_strlen(unsigned long addr)
174 void __user *vaddr = (void __force __user *) addr;
176 len = strnlen_user(vaddr, MAX_STRING_SIZE);
178 return (len > MAX_STRING_SIZE) ? 0 : len;
181 static unsigned long translate_user_vaddr(unsigned long file_offset)
183 unsigned long base_addr;
184 struct uprobe_dispatch_data *udd;
186 udd = (void *) current->utask->vaddr;
188 base_addr = udd->bp_addr - udd->tu->offset;
189 return base_addr + file_offset;
192 /* Note that we don't verify it, since the code does not come from user space */
194 process_fetch_insn(struct fetch_insn *code, struct pt_regs *regs, void *dest,
199 /* 1st stage: get value from context */
202 val = regs_get_register(regs, code->param);
205 val = get_user_stack_nth(regs, code->param);
207 case FETCH_OP_STACKP:
208 val = user_stack_pointer(regs);
210 case FETCH_OP_RETVAL:
211 val = regs_return_value(regs);
214 val = code->immediate;
217 val = translate_user_vaddr(code->immediate);
224 return process_fetch_insn_bottom(code, val, dest, base);
226 NOKPROBE_SYMBOL(process_fetch_insn)
228 static inline void init_trace_uprobe_filter(struct trace_uprobe_filter *filter)
230 rwlock_init(&filter->rwlock);
231 filter->nr_systemwide = 0;
232 INIT_LIST_HEAD(&filter->perf_events);
235 static inline bool uprobe_filter_is_empty(struct trace_uprobe_filter *filter)
237 return !filter->nr_systemwide && list_empty(&filter->perf_events);
240 static inline bool is_ret_probe(struct trace_uprobe *tu)
242 return tu->consumer.ret_handler != NULL;
245 static bool trace_uprobe_is_busy(struct dyn_event *ev)
247 struct trace_uprobe *tu = to_trace_uprobe(ev);
249 return trace_probe_is_enabled(&tu->tp);
252 static bool trace_uprobe_match(const char *system, const char *event,
253 struct dyn_event *ev)
255 struct trace_uprobe *tu = to_trace_uprobe(ev);
257 return strcmp(trace_event_name(&tu->tp.call), event) == 0 &&
258 (!system || strcmp(tu->tp.call.class->system, system) == 0);
262 * Allocate new trace_uprobe and initialize it (including uprobes).
264 static struct trace_uprobe *
265 alloc_trace_uprobe(const char *group, const char *event, int nargs, bool is_ret)
267 struct trace_uprobe *tu;
269 if (!event || !group)
270 return ERR_PTR(-EINVAL);
272 tu = kzalloc(SIZEOF_TRACE_UPROBE(nargs), GFP_KERNEL);
274 return ERR_PTR(-ENOMEM);
276 tu->tp.call.class = &tu->tp.class;
277 tu->tp.call.name = kstrdup(event, GFP_KERNEL);
278 if (!tu->tp.call.name)
281 tu->tp.class.system = kstrdup(group, GFP_KERNEL);
282 if (!tu->tp.class.system)
285 dyn_event_init(&tu->devent, &trace_uprobe_ops);
286 INIT_LIST_HEAD(&tu->tp.files);
287 tu->consumer.handler = uprobe_dispatcher;
289 tu->consumer.ret_handler = uretprobe_dispatcher;
290 init_trace_uprobe_filter(&tu->filter);
294 kfree(tu->tp.call.name);
297 return ERR_PTR(-ENOMEM);
300 static void free_trace_uprobe(struct trace_uprobe *tu)
307 for (i = 0; i < tu->tp.nr_args; i++)
308 traceprobe_free_probe_arg(&tu->tp.args[i]);
311 kfree(tu->tp.call.class->system);
312 kfree(tu->tp.call.name);
317 static struct trace_uprobe *find_probe_event(const char *event, const char *group)
319 struct dyn_event *pos;
320 struct trace_uprobe *tu;
322 for_each_trace_uprobe(tu, pos)
323 if (strcmp(trace_event_name(&tu->tp.call), event) == 0 &&
324 strcmp(tu->tp.call.class->system, group) == 0)
330 /* Unregister a trace_uprobe and probe_event */
331 static int unregister_trace_uprobe(struct trace_uprobe *tu)
335 ret = unregister_uprobe_event(tu);
339 dyn_event_remove(&tu->devent);
340 free_trace_uprobe(tu);
345 * Uprobe with multiple reference counter is not allowed. i.e.
346 * If inode and offset matches, reference counter offset *must*
347 * match as well. Though, there is one exception: If user is
348 * replacing old trace_uprobe with new one(same group/event),
349 * then we allow same uprobe with new reference counter as far
350 * as the new one does not conflict with any other existing
353 static struct trace_uprobe *find_old_trace_uprobe(struct trace_uprobe *new)
355 struct dyn_event *pos;
356 struct trace_uprobe *tmp, *old = NULL;
357 struct inode *new_inode = d_real_inode(new->path.dentry);
359 old = find_probe_event(trace_event_name(&new->tp.call),
360 new->tp.call.class->system);
362 for_each_trace_uprobe(tmp, pos) {
363 if ((old ? old != tmp : true) &&
364 new_inode == d_real_inode(tmp->path.dentry) &&
365 new->offset == tmp->offset &&
366 new->ref_ctr_offset != tmp->ref_ctr_offset) {
367 pr_warn("Reference counter offset mismatch.");
368 return ERR_PTR(-EINVAL);
374 /* Register a trace_uprobe and probe_event */
375 static int register_trace_uprobe(struct trace_uprobe *tu)
377 struct trace_uprobe *old_tu;
380 mutex_lock(&event_mutex);
382 /* register as an event */
383 old_tu = find_old_trace_uprobe(tu);
384 if (IS_ERR(old_tu)) {
385 ret = PTR_ERR(old_tu);
390 /* delete old event */
391 ret = unregister_trace_uprobe(old_tu);
396 ret = register_uprobe_event(tu);
398 pr_warn("Failed to register probe event(%d)\n", ret);
402 dyn_event_add(&tu->devent);
405 mutex_unlock(&event_mutex);
412 * - Add uprobe: p|r[:[GRP/]EVENT] PATH:OFFSET [FETCHARGS]
414 * - Remove uprobe: -:[GRP/]EVENT
416 static int trace_uprobe_create(int argc, const char **argv)
418 struct trace_uprobe *tu;
419 const char *event = NULL, *group = UPROBE_EVENT_SYSTEM;
420 char *arg, *filename, *rctr, *rctr_end, *tmp;
421 char buf[MAX_EVENT_NAME_LEN];
423 unsigned long offset, ref_ctr_offset;
424 bool is_return = false;
430 /* argc must be >= 1 */
431 if (argv[0][0] == 'r')
433 else if (argv[0][0] != 'p' || argc < 2)
436 if (argv[0][1] == ':')
439 if (!strchr(argv[1], '/'))
442 filename = kstrdup(argv[1], GFP_KERNEL);
446 /* Find the last occurrence, in case the path contains ':' too. */
447 arg = strrchr(filename, ':');
448 if (!arg || !isdigit(arg[1])) {
454 ret = kern_path(filename, LOOKUP_FOLLOW, &path);
459 if (!d_is_reg(path.dentry)) {
461 goto fail_address_parse;
464 /* Parse reference counter offset if specified. */
465 rctr = strchr(arg, '(');
467 rctr_end = strchr(rctr, ')');
468 if (rctr > rctr_end || *(rctr_end + 1) != 0) {
470 pr_info("Invalid reference counter offset.\n");
471 goto fail_address_parse;
476 ret = kstrtoul(rctr, 0, &ref_ctr_offset);
478 pr_info("Invalid reference counter offset.\n");
479 goto fail_address_parse;
483 /* Parse uprobe offset. */
484 ret = kstrtoul(arg, 0, &offset);
486 goto fail_address_parse;
493 ret = traceprobe_parse_event_name(&event, &group, buf);
495 goto fail_address_parse;
500 tail = kstrdup(kbasename(filename), GFP_KERNEL);
503 goto fail_address_parse;
506 ptr = strpbrk(tail, ".-_");
510 snprintf(buf, MAX_EVENT_NAME_LEN, "%c_%s_0x%lx", 'p', tail, offset);
515 tu = alloc_trace_uprobe(group, event, argc, is_return);
517 pr_info("Failed to allocate trace_uprobe.(%d)\n", (int)PTR_ERR(tu));
519 goto fail_address_parse;
522 tu->ref_ctr_offset = ref_ctr_offset;
524 tu->filename = filename;
526 /* parse arguments */
527 for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
528 tmp = kstrdup(argv[i], GFP_KERNEL);
534 ret = traceprobe_parse_probe_arg(&tu->tp, i, tmp,
535 is_return ? TPARG_FL_RETURN : 0);
541 ret = register_trace_uprobe(tu);
547 free_trace_uprobe(tu);
554 pr_info("Failed to parse address or file.\n");
559 static int create_or_delete_trace_uprobe(int argc, char **argv)
563 if (argv[0][0] == '-')
564 return dyn_event_release(argc, argv, &trace_uprobe_ops);
566 ret = trace_uprobe_create(argc, (const char **)argv);
567 return ret == -ECANCELED ? -EINVAL : ret;
570 static int trace_uprobe_release(struct dyn_event *ev)
572 struct trace_uprobe *tu = to_trace_uprobe(ev);
574 return unregister_trace_uprobe(tu);
577 /* Probes listing interfaces */
578 static int trace_uprobe_show(struct seq_file *m, struct dyn_event *ev)
580 struct trace_uprobe *tu = to_trace_uprobe(ev);
581 char c = is_ret_probe(tu) ? 'r' : 'p';
584 seq_printf(m, "%c:%s/%s %s:0x%0*lx", c, tu->tp.call.class->system,
585 trace_event_name(&tu->tp.call), tu->filename,
586 (int)(sizeof(void *) * 2), tu->offset);
588 if (tu->ref_ctr_offset)
589 seq_printf(m, "(0x%lx)", tu->ref_ctr_offset);
591 for (i = 0; i < tu->tp.nr_args; i++)
592 seq_printf(m, " %s=%s", tu->tp.args[i].name, tu->tp.args[i].comm);
598 static int probes_seq_show(struct seq_file *m, void *v)
600 struct dyn_event *ev = v;
602 if (!is_trace_uprobe(ev))
605 return trace_uprobe_show(m, ev);
608 static const struct seq_operations probes_seq_op = {
609 .start = dyn_event_seq_start,
610 .next = dyn_event_seq_next,
611 .stop = dyn_event_seq_stop,
612 .show = probes_seq_show
615 static int probes_open(struct inode *inode, struct file *file)
619 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
620 ret = dyn_events_release_all(&trace_uprobe_ops);
625 return seq_open(file, &probes_seq_op);
628 static ssize_t probes_write(struct file *file, const char __user *buffer,
629 size_t count, loff_t *ppos)
631 return trace_parse_run_command(file, buffer, count, ppos,
632 create_or_delete_trace_uprobe);
635 static const struct file_operations uprobe_events_ops = {
636 .owner = THIS_MODULE,
640 .release = seq_release,
641 .write = probes_write,
644 /* Probes profiling interfaces */
645 static int probes_profile_seq_show(struct seq_file *m, void *v)
647 struct dyn_event *ev = v;
648 struct trace_uprobe *tu;
650 if (!is_trace_uprobe(ev))
653 tu = to_trace_uprobe(ev);
654 seq_printf(m, " %s %-44s %15lu\n", tu->filename,
655 trace_event_name(&tu->tp.call), tu->nhit);
659 static const struct seq_operations profile_seq_op = {
660 .start = dyn_event_seq_start,
661 .next = dyn_event_seq_next,
662 .stop = dyn_event_seq_stop,
663 .show = probes_profile_seq_show
666 static int profile_open(struct inode *inode, struct file *file)
668 return seq_open(file, &profile_seq_op);
671 static const struct file_operations uprobe_profile_ops = {
672 .owner = THIS_MODULE,
673 .open = profile_open,
676 .release = seq_release,
679 struct uprobe_cpu_buffer {
683 static struct uprobe_cpu_buffer __percpu *uprobe_cpu_buffer;
684 static int uprobe_buffer_refcnt;
686 static int uprobe_buffer_init(void)
690 uprobe_cpu_buffer = alloc_percpu(struct uprobe_cpu_buffer);
691 if (uprobe_cpu_buffer == NULL)
694 for_each_possible_cpu(cpu) {
695 struct page *p = alloc_pages_node(cpu_to_node(cpu),
701 per_cpu_ptr(uprobe_cpu_buffer, cpu)->buf = page_address(p);
702 mutex_init(&per_cpu_ptr(uprobe_cpu_buffer, cpu)->mutex);
708 for_each_possible_cpu(cpu) {
711 free_page((unsigned long)per_cpu_ptr(uprobe_cpu_buffer, cpu)->buf);
714 free_percpu(uprobe_cpu_buffer);
718 static int uprobe_buffer_enable(void)
722 BUG_ON(!mutex_is_locked(&event_mutex));
724 if (uprobe_buffer_refcnt++ == 0) {
725 ret = uprobe_buffer_init();
727 uprobe_buffer_refcnt--;
733 static void uprobe_buffer_disable(void)
737 BUG_ON(!mutex_is_locked(&event_mutex));
739 if (--uprobe_buffer_refcnt == 0) {
740 for_each_possible_cpu(cpu)
741 free_page((unsigned long)per_cpu_ptr(uprobe_cpu_buffer,
744 free_percpu(uprobe_cpu_buffer);
745 uprobe_cpu_buffer = NULL;
749 static struct uprobe_cpu_buffer *uprobe_buffer_get(void)
751 struct uprobe_cpu_buffer *ucb;
754 cpu = raw_smp_processor_id();
755 ucb = per_cpu_ptr(uprobe_cpu_buffer, cpu);
758 * Use per-cpu buffers for fastest access, but we might migrate
759 * so the mutex makes sure we have sole access to it.
761 mutex_lock(&ucb->mutex);
766 static void uprobe_buffer_put(struct uprobe_cpu_buffer *ucb)
768 mutex_unlock(&ucb->mutex);
771 static void __uprobe_trace_func(struct trace_uprobe *tu,
772 unsigned long func, struct pt_regs *regs,
773 struct uprobe_cpu_buffer *ucb, int dsize,
774 struct trace_event_file *trace_file)
776 struct uprobe_trace_entry_head *entry;
777 struct ring_buffer_event *event;
778 struct ring_buffer *buffer;
781 struct trace_event_call *call = &tu->tp.call;
783 WARN_ON(call != trace_file->event_call);
785 if (WARN_ON_ONCE(tu->tp.size + dsize > PAGE_SIZE))
788 if (trace_trigger_soft_disabled(trace_file))
791 esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
792 size = esize + tu->tp.size + dsize;
793 event = trace_event_buffer_lock_reserve(&buffer, trace_file,
794 call->event.type, size, 0, 0);
798 entry = ring_buffer_event_data(event);
799 if (is_ret_probe(tu)) {
800 entry->vaddr[0] = func;
801 entry->vaddr[1] = instruction_pointer(regs);
802 data = DATAOF_TRACE_ENTRY(entry, true);
804 entry->vaddr[0] = instruction_pointer(regs);
805 data = DATAOF_TRACE_ENTRY(entry, false);
808 memcpy(data, ucb->buf, tu->tp.size + dsize);
810 event_trigger_unlock_commit(trace_file, buffer, event, entry, 0, 0);
814 static int uprobe_trace_func(struct trace_uprobe *tu, struct pt_regs *regs,
815 struct uprobe_cpu_buffer *ucb, int dsize)
817 struct event_file_link *link;
819 if (is_ret_probe(tu))
823 list_for_each_entry_rcu(link, &tu->tp.files, list)
824 __uprobe_trace_func(tu, 0, regs, ucb, dsize, link->file);
830 static void uretprobe_trace_func(struct trace_uprobe *tu, unsigned long func,
831 struct pt_regs *regs,
832 struct uprobe_cpu_buffer *ucb, int dsize)
834 struct event_file_link *link;
837 list_for_each_entry_rcu(link, &tu->tp.files, list)
838 __uprobe_trace_func(tu, func, regs, ucb, dsize, link->file);
842 /* Event entry printers */
843 static enum print_line_t
844 print_uprobe_event(struct trace_iterator *iter, int flags, struct trace_event *event)
846 struct uprobe_trace_entry_head *entry;
847 struct trace_seq *s = &iter->seq;
848 struct trace_uprobe *tu;
851 entry = (struct uprobe_trace_entry_head *)iter->ent;
852 tu = container_of(event, struct trace_uprobe, tp.call.event);
854 if (is_ret_probe(tu)) {
855 trace_seq_printf(s, "%s: (0x%lx <- 0x%lx)",
856 trace_event_name(&tu->tp.call),
857 entry->vaddr[1], entry->vaddr[0]);
858 data = DATAOF_TRACE_ENTRY(entry, true);
860 trace_seq_printf(s, "%s: (0x%lx)",
861 trace_event_name(&tu->tp.call),
863 data = DATAOF_TRACE_ENTRY(entry, false);
866 if (print_probe_args(s, tu->tp.args, tu->tp.nr_args, data, entry) < 0)
869 trace_seq_putc(s, '\n');
872 return trace_handle_return(s);
875 typedef bool (*filter_func_t)(struct uprobe_consumer *self,
876 enum uprobe_filter_ctx ctx,
877 struct mm_struct *mm);
880 probe_event_enable(struct trace_uprobe *tu, struct trace_event_file *file,
881 filter_func_t filter)
883 bool enabled = trace_probe_is_enabled(&tu->tp);
884 struct event_file_link *link = NULL;
888 if (tu->tp.flags & TP_FLAG_PROFILE)
891 link = kmalloc(sizeof(*link), GFP_KERNEL);
896 list_add_tail_rcu(&link->list, &tu->tp.files);
898 tu->tp.flags |= TP_FLAG_TRACE;
900 if (tu->tp.flags & TP_FLAG_TRACE)
903 tu->tp.flags |= TP_FLAG_PROFILE;
906 WARN_ON(!uprobe_filter_is_empty(&tu->filter));
911 ret = uprobe_buffer_enable();
915 tu->consumer.filter = filter;
916 tu->inode = d_real_inode(tu->path.dentry);
917 if (tu->ref_ctr_offset) {
918 ret = uprobe_register_refctr(tu->inode, tu->offset,
919 tu->ref_ctr_offset, &tu->consumer);
921 ret = uprobe_register(tu->inode, tu->offset, &tu->consumer);
930 uprobe_buffer_disable();
934 list_del(&link->list);
936 tu->tp.flags &= ~TP_FLAG_TRACE;
938 tu->tp.flags &= ~TP_FLAG_PROFILE;
944 probe_event_disable(struct trace_uprobe *tu, struct trace_event_file *file)
946 if (!trace_probe_is_enabled(&tu->tp))
950 struct event_file_link *link;
952 link = find_event_file_link(&tu->tp, file);
956 list_del_rcu(&link->list);
957 /* synchronize with u{,ret}probe_trace_func */
961 if (!list_empty(&tu->tp.files))
965 WARN_ON(!uprobe_filter_is_empty(&tu->filter));
967 uprobe_unregister(tu->inode, tu->offset, &tu->consumer);
969 tu->tp.flags &= file ? ~TP_FLAG_TRACE : ~TP_FLAG_PROFILE;
971 uprobe_buffer_disable();
974 static int uprobe_event_define_fields(struct trace_event_call *event_call)
977 struct uprobe_trace_entry_head field;
978 struct trace_uprobe *tu = event_call->data;
980 if (is_ret_probe(tu)) {
981 DEFINE_FIELD(unsigned long, vaddr[0], FIELD_STRING_FUNC, 0);
982 DEFINE_FIELD(unsigned long, vaddr[1], FIELD_STRING_RETIP, 0);
983 size = SIZEOF_TRACE_ENTRY(true);
985 DEFINE_FIELD(unsigned long, vaddr[0], FIELD_STRING_IP, 0);
986 size = SIZEOF_TRACE_ENTRY(false);
989 return traceprobe_define_arg_fields(event_call, size, &tu->tp);
992 #ifdef CONFIG_PERF_EVENTS
994 __uprobe_perf_filter(struct trace_uprobe_filter *filter, struct mm_struct *mm)
996 struct perf_event *event;
998 if (filter->nr_systemwide)
1001 list_for_each_entry(event, &filter->perf_events, hw.tp_list) {
1002 if (event->hw.target->mm == mm)
1010 uprobe_filter_event(struct trace_uprobe *tu, struct perf_event *event)
1012 return __uprobe_perf_filter(&tu->filter, event->hw.target->mm);
1015 static int uprobe_perf_close(struct trace_uprobe *tu, struct perf_event *event)
1019 write_lock(&tu->filter.rwlock);
1020 if (event->hw.target) {
1021 list_del(&event->hw.tp_list);
1022 done = tu->filter.nr_systemwide ||
1023 (event->hw.target->flags & PF_EXITING) ||
1024 uprobe_filter_event(tu, event);
1026 tu->filter.nr_systemwide--;
1027 done = tu->filter.nr_systemwide;
1029 write_unlock(&tu->filter.rwlock);
1032 return uprobe_apply(tu->inode, tu->offset, &tu->consumer, false);
1037 static int uprobe_perf_open(struct trace_uprobe *tu, struct perf_event *event)
1042 write_lock(&tu->filter.rwlock);
1043 if (event->hw.target) {
1045 * event->parent != NULL means copy_process(), we can avoid
1046 * uprobe_apply(). current->mm must be probed and we can rely
1047 * on dup_mmap() which preserves the already installed bp's.
1049 * attr.enable_on_exec means that exec/mmap will install the
1050 * breakpoints we need.
1052 done = tu->filter.nr_systemwide ||
1053 event->parent || event->attr.enable_on_exec ||
1054 uprobe_filter_event(tu, event);
1055 list_add(&event->hw.tp_list, &tu->filter.perf_events);
1057 done = tu->filter.nr_systemwide;
1058 tu->filter.nr_systemwide++;
1060 write_unlock(&tu->filter.rwlock);
1064 err = uprobe_apply(tu->inode, tu->offset, &tu->consumer, true);
1066 uprobe_perf_close(tu, event);
1071 static bool uprobe_perf_filter(struct uprobe_consumer *uc,
1072 enum uprobe_filter_ctx ctx, struct mm_struct *mm)
1074 struct trace_uprobe *tu;
1077 tu = container_of(uc, struct trace_uprobe, consumer);
1078 read_lock(&tu->filter.rwlock);
1079 ret = __uprobe_perf_filter(&tu->filter, mm);
1080 read_unlock(&tu->filter.rwlock);
1085 static void __uprobe_perf_func(struct trace_uprobe *tu,
1086 unsigned long func, struct pt_regs *regs,
1087 struct uprobe_cpu_buffer *ucb, int dsize)
1089 struct trace_event_call *call = &tu->tp.call;
1090 struct uprobe_trace_entry_head *entry;
1091 struct hlist_head *head;
1096 if (bpf_prog_array_valid(call) && !trace_call_bpf(call, regs))
1099 esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
1101 size = esize + tu->tp.size + dsize;
1102 size = ALIGN(size + sizeof(u32), sizeof(u64)) - sizeof(u32);
1103 if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE, "profile buffer not large enough"))
1107 head = this_cpu_ptr(call->perf_events);
1108 if (hlist_empty(head))
1111 entry = perf_trace_buf_alloc(size, NULL, &rctx);
1115 if (is_ret_probe(tu)) {
1116 entry->vaddr[0] = func;
1117 entry->vaddr[1] = instruction_pointer(regs);
1118 data = DATAOF_TRACE_ENTRY(entry, true);
1120 entry->vaddr[0] = instruction_pointer(regs);
1121 data = DATAOF_TRACE_ENTRY(entry, false);
1124 memcpy(data, ucb->buf, tu->tp.size + dsize);
1126 if (size - esize > tu->tp.size + dsize) {
1127 int len = tu->tp.size + dsize;
1129 memset(data + len, 0, size - esize - len);
1132 perf_trace_buf_submit(entry, size, rctx, call->event.type, 1, regs,
1138 /* uprobe profile handler */
1139 static int uprobe_perf_func(struct trace_uprobe *tu, struct pt_regs *regs,
1140 struct uprobe_cpu_buffer *ucb, int dsize)
1142 if (!uprobe_perf_filter(&tu->consumer, 0, current->mm))
1143 return UPROBE_HANDLER_REMOVE;
1145 if (!is_ret_probe(tu))
1146 __uprobe_perf_func(tu, 0, regs, ucb, dsize);
1150 static void uretprobe_perf_func(struct trace_uprobe *tu, unsigned long func,
1151 struct pt_regs *regs,
1152 struct uprobe_cpu_buffer *ucb, int dsize)
1154 __uprobe_perf_func(tu, func, regs, ucb, dsize);
1157 int bpf_get_uprobe_info(const struct perf_event *event, u32 *fd_type,
1158 const char **filename, u64 *probe_offset,
1159 bool perf_type_tracepoint)
1161 const char *pevent = trace_event_name(event->tp_event);
1162 const char *group = event->tp_event->class->system;
1163 struct trace_uprobe *tu;
1165 if (perf_type_tracepoint)
1166 tu = find_probe_event(pevent, group);
1168 tu = event->tp_event->data;
1172 *fd_type = is_ret_probe(tu) ? BPF_FD_TYPE_URETPROBE
1173 : BPF_FD_TYPE_UPROBE;
1174 *filename = tu->filename;
1175 *probe_offset = tu->offset;
1178 #endif /* CONFIG_PERF_EVENTS */
1181 trace_uprobe_register(struct trace_event_call *event, enum trace_reg type,
1184 struct trace_uprobe *tu = event->data;
1185 struct trace_event_file *file = data;
1188 case TRACE_REG_REGISTER:
1189 return probe_event_enable(tu, file, NULL);
1191 case TRACE_REG_UNREGISTER:
1192 probe_event_disable(tu, file);
1195 #ifdef CONFIG_PERF_EVENTS
1196 case TRACE_REG_PERF_REGISTER:
1197 return probe_event_enable(tu, NULL, uprobe_perf_filter);
1199 case TRACE_REG_PERF_UNREGISTER:
1200 probe_event_disable(tu, NULL);
1203 case TRACE_REG_PERF_OPEN:
1204 return uprobe_perf_open(tu, data);
1206 case TRACE_REG_PERF_CLOSE:
1207 return uprobe_perf_close(tu, data);
1216 static int uprobe_dispatcher(struct uprobe_consumer *con, struct pt_regs *regs)
1218 struct trace_uprobe *tu;
1219 struct uprobe_dispatch_data udd;
1220 struct uprobe_cpu_buffer *ucb;
1225 tu = container_of(con, struct trace_uprobe, consumer);
1229 udd.bp_addr = instruction_pointer(regs);
1231 current->utask->vaddr = (unsigned long) &udd;
1233 if (WARN_ON_ONCE(!uprobe_cpu_buffer))
1236 dsize = __get_data_size(&tu->tp, regs);
1237 esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
1239 ucb = uprobe_buffer_get();
1240 store_trace_args(ucb->buf, &tu->tp, regs, esize, dsize);
1242 if (tu->tp.flags & TP_FLAG_TRACE)
1243 ret |= uprobe_trace_func(tu, regs, ucb, dsize);
1245 #ifdef CONFIG_PERF_EVENTS
1246 if (tu->tp.flags & TP_FLAG_PROFILE)
1247 ret |= uprobe_perf_func(tu, regs, ucb, dsize);
1249 uprobe_buffer_put(ucb);
1253 static int uretprobe_dispatcher(struct uprobe_consumer *con,
1254 unsigned long func, struct pt_regs *regs)
1256 struct trace_uprobe *tu;
1257 struct uprobe_dispatch_data udd;
1258 struct uprobe_cpu_buffer *ucb;
1261 tu = container_of(con, struct trace_uprobe, consumer);
1266 current->utask->vaddr = (unsigned long) &udd;
1268 if (WARN_ON_ONCE(!uprobe_cpu_buffer))
1271 dsize = __get_data_size(&tu->tp, regs);
1272 esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
1274 ucb = uprobe_buffer_get();
1275 store_trace_args(ucb->buf, &tu->tp, regs, esize, dsize);
1277 if (tu->tp.flags & TP_FLAG_TRACE)
1278 uretprobe_trace_func(tu, func, regs, ucb, dsize);
1280 #ifdef CONFIG_PERF_EVENTS
1281 if (tu->tp.flags & TP_FLAG_PROFILE)
1282 uretprobe_perf_func(tu, func, regs, ucb, dsize);
1284 uprobe_buffer_put(ucb);
1288 static struct trace_event_functions uprobe_funcs = {
1289 .trace = print_uprobe_event
1292 static inline void init_trace_event_call(struct trace_uprobe *tu,
1293 struct trace_event_call *call)
1295 INIT_LIST_HEAD(&call->class->fields);
1296 call->event.funcs = &uprobe_funcs;
1297 call->class->define_fields = uprobe_event_define_fields;
1299 call->flags = TRACE_EVENT_FL_UPROBE;
1300 call->class->reg = trace_uprobe_register;
1304 static int register_uprobe_event(struct trace_uprobe *tu)
1306 struct trace_event_call *call = &tu->tp.call;
1309 init_trace_event_call(tu, call);
1311 if (traceprobe_set_print_fmt(&tu->tp, is_ret_probe(tu)) < 0)
1314 ret = register_trace_event(&call->event);
1316 kfree(call->print_fmt);
1320 ret = trace_add_event_call(call);
1323 pr_info("Failed to register uprobe event: %s\n",
1324 trace_event_name(call));
1325 kfree(call->print_fmt);
1326 unregister_trace_event(&call->event);
1332 static int unregister_uprobe_event(struct trace_uprobe *tu)
1336 /* tu->event is unregistered in trace_remove_event_call() */
1337 ret = trace_remove_event_call(&tu->tp.call);
1340 kfree(tu->tp.call.print_fmt);
1341 tu->tp.call.print_fmt = NULL;
1345 #ifdef CONFIG_PERF_EVENTS
1346 struct trace_event_call *
1347 create_local_trace_uprobe(char *name, unsigned long offs,
1348 unsigned long ref_ctr_offset, bool is_return)
1350 struct trace_uprobe *tu;
1354 ret = kern_path(name, LOOKUP_FOLLOW, &path);
1356 return ERR_PTR(ret);
1358 if (!d_is_reg(path.dentry)) {
1360 return ERR_PTR(-EINVAL);
1364 * local trace_kprobes are not added to dyn_event, so they are never
1365 * searched in find_trace_kprobe(). Therefore, there is no concern of
1366 * duplicated name "DUMMY_EVENT" here.
1368 tu = alloc_trace_uprobe(UPROBE_EVENT_SYSTEM, "DUMMY_EVENT", 0,
1372 pr_info("Failed to allocate trace_uprobe.(%d)\n",
1375 return ERR_CAST(tu);
1380 tu->ref_ctr_offset = ref_ctr_offset;
1381 tu->filename = kstrdup(name, GFP_KERNEL);
1382 init_trace_event_call(tu, &tu->tp.call);
1384 if (traceprobe_set_print_fmt(&tu->tp, is_ret_probe(tu)) < 0) {
1389 return &tu->tp.call;
1391 free_trace_uprobe(tu);
1392 return ERR_PTR(ret);
1395 void destroy_local_trace_uprobe(struct trace_event_call *event_call)
1397 struct trace_uprobe *tu;
1399 tu = container_of(event_call, struct trace_uprobe, tp.call);
1401 kfree(tu->tp.call.print_fmt);
1402 tu->tp.call.print_fmt = NULL;
1404 free_trace_uprobe(tu);
1406 #endif /* CONFIG_PERF_EVENTS */
1408 /* Make a trace interface for controling probe points */
1409 static __init int init_uprobe_trace(void)
1411 struct dentry *d_tracer;
1414 ret = dyn_event_register(&trace_uprobe_ops);
1418 d_tracer = tracing_init_dentry();
1419 if (IS_ERR(d_tracer))
1422 trace_create_file("uprobe_events", 0644, d_tracer,
1423 NULL, &uprobe_events_ops);
1424 /* Profile interface */
1425 trace_create_file("uprobe_profile", 0444, d_tracer,
1426 NULL, &uprobe_profile_ops);
1430 fs_initcall(init_uprobe_trace);