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/module.h>
11 #include <linux/uaccess.h>
12 #include <linux/uprobes.h>
13 #include <linux/namei.h>
14 #include <linux/string.h>
15 #include <linux/rculist.h>
17 #include "trace_probe.h"
19 #define UPROBE_EVENT_SYSTEM "uprobes"
21 struct uprobe_trace_entry_head {
22 struct trace_entry ent;
23 unsigned long vaddr[];
26 #define SIZEOF_TRACE_ENTRY(is_return) \
27 (sizeof(struct uprobe_trace_entry_head) + \
28 sizeof(unsigned long) * (is_return ? 2 : 1))
30 #define DATAOF_TRACE_ENTRY(entry, is_return) \
31 ((void*)(entry) + SIZEOF_TRACE_ENTRY(is_return))
33 struct trace_uprobe_filter {
36 struct list_head perf_events;
40 * uprobe event core functions
43 struct list_head list;
44 struct trace_uprobe_filter filter;
45 struct uprobe_consumer consumer;
51 struct trace_probe tp;
54 #define SIZEOF_TRACE_UPROBE(n) \
55 (offsetof(struct trace_uprobe, tp.args) + \
56 (sizeof(struct probe_arg) * (n)))
58 static int register_uprobe_event(struct trace_uprobe *tu);
59 static int unregister_uprobe_event(struct trace_uprobe *tu);
61 static DEFINE_MUTEX(uprobe_lock);
62 static LIST_HEAD(uprobe_list);
64 struct uprobe_dispatch_data {
65 struct trace_uprobe *tu;
66 unsigned long bp_addr;
69 static int uprobe_dispatcher(struct uprobe_consumer *con, struct pt_regs *regs);
70 static int uretprobe_dispatcher(struct uprobe_consumer *con,
71 unsigned long func, struct pt_regs *regs);
73 #ifdef CONFIG_STACK_GROWSUP
74 static unsigned long adjust_stack_addr(unsigned long addr, unsigned int n)
76 return addr - (n * sizeof(long));
79 static unsigned long adjust_stack_addr(unsigned long addr, unsigned int n)
81 return addr + (n * sizeof(long));
85 static unsigned long get_user_stack_nth(struct pt_regs *regs, unsigned int n)
88 unsigned long addr = user_stack_pointer(regs);
90 addr = adjust_stack_addr(addr, n);
92 if (copy_from_user(&ret, (void __force __user *) addr, sizeof(ret)))
99 * Uprobes-specific fetch functions
101 #define DEFINE_FETCH_stack(type) \
102 static void FETCH_FUNC_NAME(stack, type)(struct pt_regs *regs, \
103 void *offset, void *dest) \
105 *(type *)dest = (type)get_user_stack_nth(regs, \
106 ((unsigned long)offset)); \
108 DEFINE_BASIC_FETCH_FUNCS(stack)
109 /* No string on the stack entry */
110 #define fetch_stack_string NULL
111 #define fetch_stack_string_size NULL
113 #define DEFINE_FETCH_memory(type) \
114 static void FETCH_FUNC_NAME(memory, type)(struct pt_regs *regs, \
115 void *addr, void *dest) \
118 void __user *vaddr = (void __force __user *) addr; \
120 if (copy_from_user(&retval, vaddr, sizeof(type))) \
123 *(type *) dest = retval; \
125 DEFINE_BASIC_FETCH_FUNCS(memory)
127 * Fetch a null-terminated string. Caller MUST set *(u32 *)dest with max
128 * length and relative data location.
130 static void FETCH_FUNC_NAME(memory, string)(struct pt_regs *regs,
131 void *addr, void *dest)
134 u32 rloc = *(u32 *)dest;
135 int maxlen = get_rloc_len(rloc);
136 u8 *dst = get_rloc_data(dest);
137 void __user *src = (void __force __user *) addr;
142 ret = strncpy_from_user(dst, src, maxlen);
147 * Include the terminating null byte. In this case it
148 * was copied by strncpy_from_user but not accounted
153 if (ret < 0) { /* Failed to fetch string */
154 ((u8 *)get_rloc_data(dest))[0] = '\0';
155 *(u32 *)dest = make_data_rloc(0, get_rloc_offs(rloc));
157 *(u32 *)dest = make_data_rloc(ret, get_rloc_offs(rloc));
161 static void FETCH_FUNC_NAME(memory, string_size)(struct pt_regs *regs,
162 void *addr, void *dest)
165 void __user *vaddr = (void __force __user *) addr;
167 len = strnlen_user(vaddr, MAX_STRING_SIZE);
169 if (len == 0 || len > MAX_STRING_SIZE) /* Failed to check length */
175 static unsigned long translate_user_vaddr(void *file_offset)
177 unsigned long base_addr;
178 struct uprobe_dispatch_data *udd;
180 udd = (void *) current->utask->vaddr;
182 base_addr = udd->bp_addr - udd->tu->offset;
183 return base_addr + (unsigned long)file_offset;
186 #define DEFINE_FETCH_file_offset(type) \
187 static void FETCH_FUNC_NAME(file_offset, type)(struct pt_regs *regs, \
188 void *offset, void *dest)\
190 void *vaddr = (void *)translate_user_vaddr(offset); \
192 FETCH_FUNC_NAME(memory, type)(regs, vaddr, dest); \
194 DEFINE_BASIC_FETCH_FUNCS(file_offset)
195 DEFINE_FETCH_file_offset(string)
196 DEFINE_FETCH_file_offset(string_size)
198 /* Fetch type information table */
199 static const struct fetch_type uprobes_fetch_type_table[] = {
201 [FETCH_TYPE_STRING] = __ASSIGN_FETCH_TYPE("string", string, string,
202 sizeof(u32), 1, "__data_loc char[]"),
203 [FETCH_TYPE_STRSIZE] = __ASSIGN_FETCH_TYPE("string_size", u32,
204 string_size, sizeof(u32), 0, "u32"),
206 ASSIGN_FETCH_TYPE(u8, u8, 0),
207 ASSIGN_FETCH_TYPE(u16, u16, 0),
208 ASSIGN_FETCH_TYPE(u32, u32, 0),
209 ASSIGN_FETCH_TYPE(u64, u64, 0),
210 ASSIGN_FETCH_TYPE(s8, u8, 1),
211 ASSIGN_FETCH_TYPE(s16, u16, 1),
212 ASSIGN_FETCH_TYPE(s32, u32, 1),
213 ASSIGN_FETCH_TYPE(s64, u64, 1),
214 ASSIGN_FETCH_TYPE_ALIAS(x8, u8, u8, 0),
215 ASSIGN_FETCH_TYPE_ALIAS(x16, u16, u16, 0),
216 ASSIGN_FETCH_TYPE_ALIAS(x32, u32, u32, 0),
217 ASSIGN_FETCH_TYPE_ALIAS(x64, u64, u64, 0),
219 ASSIGN_FETCH_TYPE_END
222 static inline void init_trace_uprobe_filter(struct trace_uprobe_filter *filter)
224 rwlock_init(&filter->rwlock);
225 filter->nr_systemwide = 0;
226 INIT_LIST_HEAD(&filter->perf_events);
229 static inline bool uprobe_filter_is_empty(struct trace_uprobe_filter *filter)
231 return !filter->nr_systemwide && list_empty(&filter->perf_events);
234 static inline bool is_ret_probe(struct trace_uprobe *tu)
236 return tu->consumer.ret_handler != NULL;
240 * Allocate new trace_uprobe and initialize it (including uprobes).
242 static struct trace_uprobe *
243 alloc_trace_uprobe(const char *group, const char *event, int nargs, bool is_ret)
245 struct trace_uprobe *tu;
247 if (!event || !is_good_name(event))
248 return ERR_PTR(-EINVAL);
250 if (!group || !is_good_name(group))
251 return ERR_PTR(-EINVAL);
253 tu = kzalloc(SIZEOF_TRACE_UPROBE(nargs), GFP_KERNEL);
255 return ERR_PTR(-ENOMEM);
257 tu->tp.call.class = &tu->tp.class;
258 tu->tp.call.name = kstrdup(event, GFP_KERNEL);
259 if (!tu->tp.call.name)
262 tu->tp.class.system = kstrdup(group, GFP_KERNEL);
263 if (!tu->tp.class.system)
266 INIT_LIST_HEAD(&tu->list);
267 INIT_LIST_HEAD(&tu->tp.files);
268 tu->consumer.handler = uprobe_dispatcher;
270 tu->consumer.ret_handler = uretprobe_dispatcher;
271 init_trace_uprobe_filter(&tu->filter);
275 kfree(tu->tp.call.name);
278 return ERR_PTR(-ENOMEM);
281 static void free_trace_uprobe(struct trace_uprobe *tu)
285 for (i = 0; i < tu->tp.nr_args; i++)
286 traceprobe_free_probe_arg(&tu->tp.args[i]);
289 kfree(tu->tp.call.class->system);
290 kfree(tu->tp.call.name);
295 static struct trace_uprobe *find_probe_event(const char *event, const char *group)
297 struct trace_uprobe *tu;
299 list_for_each_entry(tu, &uprobe_list, list)
300 if (strcmp(trace_event_name(&tu->tp.call), event) == 0 &&
301 strcmp(tu->tp.call.class->system, group) == 0)
307 /* Unregister a trace_uprobe and probe_event: call with locking uprobe_lock */
308 static int unregister_trace_uprobe(struct trace_uprobe *tu)
312 ret = unregister_uprobe_event(tu);
317 free_trace_uprobe(tu);
321 /* Register a trace_uprobe and probe_event */
322 static int register_trace_uprobe(struct trace_uprobe *tu)
324 struct trace_uprobe *old_tu;
327 mutex_lock(&uprobe_lock);
329 /* register as an event */
330 old_tu = find_probe_event(trace_event_name(&tu->tp.call),
331 tu->tp.call.class->system);
333 /* delete old event */
334 ret = unregister_trace_uprobe(old_tu);
339 ret = register_uprobe_event(tu);
341 pr_warn("Failed to register probe event(%d)\n", ret);
345 list_add_tail(&tu->list, &uprobe_list);
348 mutex_unlock(&uprobe_lock);
355 * - Add uprobe: p|r[:[GRP/]EVENT] PATH:OFFSET [FETCHARGS]
357 * - Remove uprobe: -:[GRP/]EVENT
359 static int create_trace_uprobe(int argc, char **argv)
361 struct trace_uprobe *tu;
362 char *arg, *event, *group, *filename;
363 char buf[MAX_EVENT_NAME_LEN];
365 unsigned long offset;
366 bool is_delete, is_return;
375 /* argc must be >= 1 */
376 if (argv[0][0] == '-')
378 else if (argv[0][0] == 'r')
380 else if (argv[0][0] != 'p') {
381 pr_info("Probe definition must be started with 'p', 'r' or '-'.\n");
385 if (argv[0][1] == ':') {
387 arg = strchr(event, '/');
394 if (strlen(group) == 0) {
395 pr_info("Group name is not specified\n");
399 if (strlen(event) == 0) {
400 pr_info("Event name is not specified\n");
405 group = UPROBE_EVENT_SYSTEM;
411 pr_info("Delete command needs an event name.\n");
414 mutex_lock(&uprobe_lock);
415 tu = find_probe_event(event, group);
418 mutex_unlock(&uprobe_lock);
419 pr_info("Event %s/%s doesn't exist.\n", group, event);
422 /* delete an event */
423 ret = unregister_trace_uprobe(tu);
424 mutex_unlock(&uprobe_lock);
429 pr_info("Probe point is not specified.\n");
432 /* Find the last occurrence, in case the path contains ':' too. */
433 arg = strrchr(argv[1], ':');
439 ret = kern_path(filename, LOOKUP_FOLLOW, &path);
443 if (!d_is_reg(path.dentry)) {
445 goto fail_address_parse;
448 ret = kstrtoul(arg, 0, &offset);
450 goto fail_address_parse;
460 tail = kstrdup(kbasename(filename), GFP_KERNEL);
463 goto fail_address_parse;
466 ptr = strpbrk(tail, ".-_");
470 snprintf(buf, MAX_EVENT_NAME_LEN, "%c_%s_0x%lx", 'p', tail, offset);
475 tu = alloc_trace_uprobe(group, event, argc, is_return);
477 pr_info("Failed to allocate trace_uprobe.(%d)\n", (int)PTR_ERR(tu));
479 goto fail_address_parse;
483 tu->filename = kstrdup(filename, GFP_KERNEL);
486 pr_info("Failed to allocate filename.\n");
491 /* parse arguments */
493 for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
494 struct probe_arg *parg = &tu->tp.args[i];
496 /* Increment count for freeing args in error case */
499 /* Parse argument name */
500 arg = strchr(argv[i], '=');
503 parg->name = kstrdup(argv[i], GFP_KERNEL);
506 /* If argument name is omitted, set "argN" */
507 snprintf(buf, MAX_EVENT_NAME_LEN, "arg%d", i + 1);
508 parg->name = kstrdup(buf, GFP_KERNEL);
512 pr_info("Failed to allocate argument[%d] name.\n", i);
517 if (!is_good_name(parg->name)) {
518 pr_info("Invalid argument[%d] name: %s\n", i, parg->name);
523 if (traceprobe_conflict_field_name(parg->name, tu->tp.args, i)) {
524 pr_info("Argument[%d] name '%s' conflicts with "
525 "another field.\n", i, argv[i]);
530 /* Parse fetch argument */
531 ret = traceprobe_parse_probe_arg(arg, &tu->tp.size, parg,
533 uprobes_fetch_type_table);
535 pr_info("Parse error at argument[%d]. (%d)\n", i, ret);
540 ret = register_trace_uprobe(tu);
546 free_trace_uprobe(tu);
552 pr_info("Failed to parse address or file.\n");
557 static int cleanup_all_probes(void)
559 struct trace_uprobe *tu;
562 mutex_lock(&uprobe_lock);
563 while (!list_empty(&uprobe_list)) {
564 tu = list_entry(uprobe_list.next, struct trace_uprobe, list);
565 ret = unregister_trace_uprobe(tu);
569 mutex_unlock(&uprobe_lock);
573 /* Probes listing interfaces */
574 static void *probes_seq_start(struct seq_file *m, loff_t *pos)
576 mutex_lock(&uprobe_lock);
577 return seq_list_start(&uprobe_list, *pos);
580 static void *probes_seq_next(struct seq_file *m, void *v, loff_t *pos)
582 return seq_list_next(v, &uprobe_list, pos);
585 static void probes_seq_stop(struct seq_file *m, void *v)
587 mutex_unlock(&uprobe_lock);
590 static int probes_seq_show(struct seq_file *m, void *v)
592 struct trace_uprobe *tu = v;
593 char c = is_ret_probe(tu) ? 'r' : 'p';
596 seq_printf(m, "%c:%s/%s %s:0x%0*lx", c, tu->tp.call.class->system,
597 trace_event_name(&tu->tp.call), tu->filename,
598 (int)(sizeof(void *) * 2), tu->offset);
600 for (i = 0; i < tu->tp.nr_args; i++)
601 seq_printf(m, " %s=%s", tu->tp.args[i].name, tu->tp.args[i].comm);
607 static const struct seq_operations probes_seq_op = {
608 .start = probes_seq_start,
609 .next = probes_seq_next,
610 .stop = probes_seq_stop,
611 .show = probes_seq_show
614 static int probes_open(struct inode *inode, struct file *file)
618 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
619 ret = cleanup_all_probes();
624 return seq_open(file, &probes_seq_op);
627 static ssize_t probes_write(struct file *file, const char __user *buffer,
628 size_t count, loff_t *ppos)
630 return trace_parse_run_command(file, buffer, count, ppos, create_trace_uprobe);
633 static const struct file_operations uprobe_events_ops = {
634 .owner = THIS_MODULE,
638 .release = seq_release,
639 .write = probes_write,
642 /* Probes profiling interfaces */
643 static int probes_profile_seq_show(struct seq_file *m, void *v)
645 struct trace_uprobe *tu = v;
647 seq_printf(m, " %s %-44s %15lu\n", tu->filename,
648 trace_event_name(&tu->tp.call), tu->nhit);
652 static const struct seq_operations profile_seq_op = {
653 .start = probes_seq_start,
654 .next = probes_seq_next,
655 .stop = probes_seq_stop,
656 .show = probes_profile_seq_show
659 static int profile_open(struct inode *inode, struct file *file)
661 return seq_open(file, &profile_seq_op);
664 static const struct file_operations uprobe_profile_ops = {
665 .owner = THIS_MODULE,
666 .open = profile_open,
669 .release = seq_release,
672 struct uprobe_cpu_buffer {
676 static struct uprobe_cpu_buffer __percpu *uprobe_cpu_buffer;
677 static int uprobe_buffer_refcnt;
679 static int uprobe_buffer_init(void)
683 uprobe_cpu_buffer = alloc_percpu(struct uprobe_cpu_buffer);
684 if (uprobe_cpu_buffer == NULL)
687 for_each_possible_cpu(cpu) {
688 struct page *p = alloc_pages_node(cpu_to_node(cpu),
694 per_cpu_ptr(uprobe_cpu_buffer, cpu)->buf = page_address(p);
695 mutex_init(&per_cpu_ptr(uprobe_cpu_buffer, cpu)->mutex);
701 for_each_possible_cpu(cpu) {
704 free_page((unsigned long)per_cpu_ptr(uprobe_cpu_buffer, cpu)->buf);
707 free_percpu(uprobe_cpu_buffer);
711 static int uprobe_buffer_enable(void)
715 BUG_ON(!mutex_is_locked(&event_mutex));
717 if (uprobe_buffer_refcnt++ == 0) {
718 ret = uprobe_buffer_init();
720 uprobe_buffer_refcnt--;
726 static void uprobe_buffer_disable(void)
730 BUG_ON(!mutex_is_locked(&event_mutex));
732 if (--uprobe_buffer_refcnt == 0) {
733 for_each_possible_cpu(cpu)
734 free_page((unsigned long)per_cpu_ptr(uprobe_cpu_buffer,
737 free_percpu(uprobe_cpu_buffer);
738 uprobe_cpu_buffer = NULL;
742 static struct uprobe_cpu_buffer *uprobe_buffer_get(void)
744 struct uprobe_cpu_buffer *ucb;
747 cpu = raw_smp_processor_id();
748 ucb = per_cpu_ptr(uprobe_cpu_buffer, cpu);
751 * Use per-cpu buffers for fastest access, but we might migrate
752 * so the mutex makes sure we have sole access to it.
754 mutex_lock(&ucb->mutex);
759 static void uprobe_buffer_put(struct uprobe_cpu_buffer *ucb)
761 mutex_unlock(&ucb->mutex);
764 static void __uprobe_trace_func(struct trace_uprobe *tu,
765 unsigned long func, struct pt_regs *regs,
766 struct uprobe_cpu_buffer *ucb, int dsize,
767 struct trace_event_file *trace_file)
769 struct uprobe_trace_entry_head *entry;
770 struct ring_buffer_event *event;
771 struct ring_buffer *buffer;
774 struct trace_event_call *call = &tu->tp.call;
776 WARN_ON(call != trace_file->event_call);
778 if (WARN_ON_ONCE(tu->tp.size + dsize > PAGE_SIZE))
781 if (trace_trigger_soft_disabled(trace_file))
784 esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
785 size = esize + tu->tp.size + dsize;
786 event = trace_event_buffer_lock_reserve(&buffer, trace_file,
787 call->event.type, size, 0, 0);
791 entry = ring_buffer_event_data(event);
792 if (is_ret_probe(tu)) {
793 entry->vaddr[0] = func;
794 entry->vaddr[1] = instruction_pointer(regs);
795 data = DATAOF_TRACE_ENTRY(entry, true);
797 entry->vaddr[0] = instruction_pointer(regs);
798 data = DATAOF_TRACE_ENTRY(entry, false);
801 memcpy(data, ucb->buf, tu->tp.size + dsize);
803 event_trigger_unlock_commit(trace_file, buffer, event, entry, 0, 0);
807 static int uprobe_trace_func(struct trace_uprobe *tu, struct pt_regs *regs,
808 struct uprobe_cpu_buffer *ucb, int dsize)
810 struct event_file_link *link;
812 if (is_ret_probe(tu))
816 list_for_each_entry_rcu(link, &tu->tp.files, list)
817 __uprobe_trace_func(tu, 0, regs, ucb, dsize, link->file);
823 static void uretprobe_trace_func(struct trace_uprobe *tu, unsigned long func,
824 struct pt_regs *regs,
825 struct uprobe_cpu_buffer *ucb, int dsize)
827 struct event_file_link *link;
830 list_for_each_entry_rcu(link, &tu->tp.files, list)
831 __uprobe_trace_func(tu, func, regs, ucb, dsize, link->file);
835 /* Event entry printers */
836 static enum print_line_t
837 print_uprobe_event(struct trace_iterator *iter, int flags, struct trace_event *event)
839 struct uprobe_trace_entry_head *entry;
840 struct trace_seq *s = &iter->seq;
841 struct trace_uprobe *tu;
845 entry = (struct uprobe_trace_entry_head *)iter->ent;
846 tu = container_of(event, struct trace_uprobe, tp.call.event);
848 if (is_ret_probe(tu)) {
849 trace_seq_printf(s, "%s: (0x%lx <- 0x%lx)",
850 trace_event_name(&tu->tp.call),
851 entry->vaddr[1], entry->vaddr[0]);
852 data = DATAOF_TRACE_ENTRY(entry, true);
854 trace_seq_printf(s, "%s: (0x%lx)",
855 trace_event_name(&tu->tp.call),
857 data = DATAOF_TRACE_ENTRY(entry, false);
860 for (i = 0; i < tu->tp.nr_args; i++) {
861 struct probe_arg *parg = &tu->tp.args[i];
863 if (!parg->type->print(s, parg->name, data + parg->offset, entry))
867 trace_seq_putc(s, '\n');
870 return trace_handle_return(s);
873 typedef bool (*filter_func_t)(struct uprobe_consumer *self,
874 enum uprobe_filter_ctx ctx,
875 struct mm_struct *mm);
878 probe_event_enable(struct trace_uprobe *tu, struct trace_event_file *file,
879 filter_func_t filter)
881 bool enabled = trace_probe_is_enabled(&tu->tp);
882 struct event_file_link *link = NULL;
886 if (tu->tp.flags & TP_FLAG_PROFILE)
889 link = kmalloc(sizeof(*link), GFP_KERNEL);
894 list_add_tail_rcu(&link->list, &tu->tp.files);
896 tu->tp.flags |= TP_FLAG_TRACE;
898 if (tu->tp.flags & TP_FLAG_TRACE)
901 tu->tp.flags |= TP_FLAG_PROFILE;
904 WARN_ON(!uprobe_filter_is_empty(&tu->filter));
909 ret = uprobe_buffer_enable();
913 tu->consumer.filter = filter;
914 tu->inode = d_real_inode(tu->path.dentry);
915 ret = uprobe_register(tu->inode, tu->offset, &tu->consumer);
922 uprobe_buffer_disable();
926 list_del(&link->list);
928 tu->tp.flags &= ~TP_FLAG_TRACE;
930 tu->tp.flags &= ~TP_FLAG_PROFILE;
936 probe_event_disable(struct trace_uprobe *tu, struct trace_event_file *file)
938 if (!trace_probe_is_enabled(&tu->tp))
942 struct event_file_link *link;
944 link = find_event_file_link(&tu->tp, file);
948 list_del_rcu(&link->list);
949 /* synchronize with u{,ret}probe_trace_func */
953 if (!list_empty(&tu->tp.files))
957 WARN_ON(!uprobe_filter_is_empty(&tu->filter));
959 uprobe_unregister(tu->inode, tu->offset, &tu->consumer);
961 tu->tp.flags &= file ? ~TP_FLAG_TRACE : ~TP_FLAG_PROFILE;
963 uprobe_buffer_disable();
966 static int uprobe_event_define_fields(struct trace_event_call *event_call)
969 struct uprobe_trace_entry_head field;
970 struct trace_uprobe *tu = event_call->data;
972 if (is_ret_probe(tu)) {
973 DEFINE_FIELD(unsigned long, vaddr[0], FIELD_STRING_FUNC, 0);
974 DEFINE_FIELD(unsigned long, vaddr[1], FIELD_STRING_RETIP, 0);
975 size = SIZEOF_TRACE_ENTRY(true);
977 DEFINE_FIELD(unsigned long, vaddr[0], FIELD_STRING_IP, 0);
978 size = SIZEOF_TRACE_ENTRY(false);
980 /* Set argument names as fields */
981 for (i = 0; i < tu->tp.nr_args; i++) {
982 struct probe_arg *parg = &tu->tp.args[i];
984 ret = trace_define_field(event_call, parg->type->fmttype,
985 parg->name, size + parg->offset,
986 parg->type->size, parg->type->is_signed,
995 #ifdef CONFIG_PERF_EVENTS
997 __uprobe_perf_filter(struct trace_uprobe_filter *filter, struct mm_struct *mm)
999 struct perf_event *event;
1001 if (filter->nr_systemwide)
1004 list_for_each_entry(event, &filter->perf_events, hw.tp_list) {
1005 if (event->hw.target->mm == mm)
1013 uprobe_filter_event(struct trace_uprobe *tu, struct perf_event *event)
1015 return __uprobe_perf_filter(&tu->filter, event->hw.target->mm);
1018 static int uprobe_perf_close(struct trace_uprobe *tu, struct perf_event *event)
1022 write_lock(&tu->filter.rwlock);
1023 if (event->hw.target) {
1024 list_del(&event->hw.tp_list);
1025 done = tu->filter.nr_systemwide ||
1026 (event->hw.target->flags & PF_EXITING) ||
1027 uprobe_filter_event(tu, event);
1029 tu->filter.nr_systemwide--;
1030 done = tu->filter.nr_systemwide;
1032 write_unlock(&tu->filter.rwlock);
1035 return uprobe_apply(tu->inode, tu->offset, &tu->consumer, false);
1040 static int uprobe_perf_open(struct trace_uprobe *tu, struct perf_event *event)
1045 write_lock(&tu->filter.rwlock);
1046 if (event->hw.target) {
1048 * event->parent != NULL means copy_process(), we can avoid
1049 * uprobe_apply(). current->mm must be probed and we can rely
1050 * on dup_mmap() which preserves the already installed bp's.
1052 * attr.enable_on_exec means that exec/mmap will install the
1053 * breakpoints we need.
1055 done = tu->filter.nr_systemwide ||
1056 event->parent || event->attr.enable_on_exec ||
1057 uprobe_filter_event(tu, event);
1058 list_add(&event->hw.tp_list, &tu->filter.perf_events);
1060 done = tu->filter.nr_systemwide;
1061 tu->filter.nr_systemwide++;
1063 write_unlock(&tu->filter.rwlock);
1067 err = uprobe_apply(tu->inode, tu->offset, &tu->consumer, true);
1069 uprobe_perf_close(tu, event);
1074 static bool uprobe_perf_filter(struct uprobe_consumer *uc,
1075 enum uprobe_filter_ctx ctx, struct mm_struct *mm)
1077 struct trace_uprobe *tu;
1080 tu = container_of(uc, struct trace_uprobe, consumer);
1081 read_lock(&tu->filter.rwlock);
1082 ret = __uprobe_perf_filter(&tu->filter, mm);
1083 read_unlock(&tu->filter.rwlock);
1088 static void __uprobe_perf_func(struct trace_uprobe *tu,
1089 unsigned long func, struct pt_regs *regs,
1090 struct uprobe_cpu_buffer *ucb, int dsize)
1092 struct trace_event_call *call = &tu->tp.call;
1093 struct uprobe_trace_entry_head *entry;
1094 struct hlist_head *head;
1099 if (bpf_prog_array_valid(call) && !trace_call_bpf(call, regs))
1102 esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
1104 size = esize + tu->tp.size + dsize;
1105 size = ALIGN(size + sizeof(u32), sizeof(u64)) - sizeof(u32);
1106 if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE, "profile buffer not large enough"))
1110 head = this_cpu_ptr(call->perf_events);
1111 if (hlist_empty(head))
1114 entry = perf_trace_buf_alloc(size, NULL, &rctx);
1118 if (is_ret_probe(tu)) {
1119 entry->vaddr[0] = func;
1120 entry->vaddr[1] = instruction_pointer(regs);
1121 data = DATAOF_TRACE_ENTRY(entry, true);
1123 entry->vaddr[0] = instruction_pointer(regs);
1124 data = DATAOF_TRACE_ENTRY(entry, false);
1127 memcpy(data, ucb->buf, tu->tp.size + dsize);
1129 if (size - esize > tu->tp.size + dsize) {
1130 int len = tu->tp.size + dsize;
1132 memset(data + len, 0, size - esize - len);
1135 perf_trace_buf_submit(entry, size, rctx, call->event.type, 1, regs,
1141 /* uprobe profile handler */
1142 static int uprobe_perf_func(struct trace_uprobe *tu, struct pt_regs *regs,
1143 struct uprobe_cpu_buffer *ucb, int dsize)
1145 if (!uprobe_perf_filter(&tu->consumer, 0, current->mm))
1146 return UPROBE_HANDLER_REMOVE;
1148 if (!is_ret_probe(tu))
1149 __uprobe_perf_func(tu, 0, regs, ucb, dsize);
1153 static void uretprobe_perf_func(struct trace_uprobe *tu, unsigned long func,
1154 struct pt_regs *regs,
1155 struct uprobe_cpu_buffer *ucb, int dsize)
1157 __uprobe_perf_func(tu, func, regs, ucb, dsize);
1160 int bpf_get_uprobe_info(const struct perf_event *event, u32 *fd_type,
1161 const char **filename, u64 *probe_offset,
1162 bool perf_type_tracepoint)
1164 const char *pevent = trace_event_name(event->tp_event);
1165 const char *group = event->tp_event->class->system;
1166 struct trace_uprobe *tu;
1168 if (perf_type_tracepoint)
1169 tu = find_probe_event(pevent, group);
1171 tu = event->tp_event->data;
1175 *fd_type = is_ret_probe(tu) ? BPF_FD_TYPE_URETPROBE
1176 : BPF_FD_TYPE_UPROBE;
1177 *filename = tu->filename;
1178 *probe_offset = tu->offset;
1181 #endif /* CONFIG_PERF_EVENTS */
1184 trace_uprobe_register(struct trace_event_call *event, enum trace_reg type,
1187 struct trace_uprobe *tu = event->data;
1188 struct trace_event_file *file = data;
1191 case TRACE_REG_REGISTER:
1192 return probe_event_enable(tu, file, NULL);
1194 case TRACE_REG_UNREGISTER:
1195 probe_event_disable(tu, file);
1198 #ifdef CONFIG_PERF_EVENTS
1199 case TRACE_REG_PERF_REGISTER:
1200 return probe_event_enable(tu, NULL, uprobe_perf_filter);
1202 case TRACE_REG_PERF_UNREGISTER:
1203 probe_event_disable(tu, NULL);
1206 case TRACE_REG_PERF_OPEN:
1207 return uprobe_perf_open(tu, data);
1209 case TRACE_REG_PERF_CLOSE:
1210 return uprobe_perf_close(tu, data);
1219 static int uprobe_dispatcher(struct uprobe_consumer *con, struct pt_regs *regs)
1221 struct trace_uprobe *tu;
1222 struct uprobe_dispatch_data udd;
1223 struct uprobe_cpu_buffer *ucb;
1228 tu = container_of(con, struct trace_uprobe, consumer);
1232 udd.bp_addr = instruction_pointer(regs);
1234 current->utask->vaddr = (unsigned long) &udd;
1236 if (WARN_ON_ONCE(!uprobe_cpu_buffer))
1239 dsize = __get_data_size(&tu->tp, regs);
1240 esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
1242 ucb = uprobe_buffer_get();
1243 store_trace_args(esize, &tu->tp, regs, ucb->buf, dsize);
1245 if (tu->tp.flags & TP_FLAG_TRACE)
1246 ret |= uprobe_trace_func(tu, regs, ucb, dsize);
1248 #ifdef CONFIG_PERF_EVENTS
1249 if (tu->tp.flags & TP_FLAG_PROFILE)
1250 ret |= uprobe_perf_func(tu, regs, ucb, dsize);
1252 uprobe_buffer_put(ucb);
1256 static int uretprobe_dispatcher(struct uprobe_consumer *con,
1257 unsigned long func, struct pt_regs *regs)
1259 struct trace_uprobe *tu;
1260 struct uprobe_dispatch_data udd;
1261 struct uprobe_cpu_buffer *ucb;
1264 tu = container_of(con, struct trace_uprobe, consumer);
1269 current->utask->vaddr = (unsigned long) &udd;
1271 if (WARN_ON_ONCE(!uprobe_cpu_buffer))
1274 dsize = __get_data_size(&tu->tp, regs);
1275 esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
1277 ucb = uprobe_buffer_get();
1278 store_trace_args(esize, &tu->tp, regs, ucb->buf, dsize);
1280 if (tu->tp.flags & TP_FLAG_TRACE)
1281 uretprobe_trace_func(tu, func, regs, ucb, dsize);
1283 #ifdef CONFIG_PERF_EVENTS
1284 if (tu->tp.flags & TP_FLAG_PROFILE)
1285 uretprobe_perf_func(tu, func, regs, ucb, dsize);
1287 uprobe_buffer_put(ucb);
1291 static struct trace_event_functions uprobe_funcs = {
1292 .trace = print_uprobe_event
1295 static inline void init_trace_event_call(struct trace_uprobe *tu,
1296 struct trace_event_call *call)
1298 INIT_LIST_HEAD(&call->class->fields);
1299 call->event.funcs = &uprobe_funcs;
1300 call->class->define_fields = uprobe_event_define_fields;
1302 call->flags = TRACE_EVENT_FL_UPROBE;
1303 call->class->reg = trace_uprobe_register;
1307 static int register_uprobe_event(struct trace_uprobe *tu)
1309 struct trace_event_call *call = &tu->tp.call;
1312 init_trace_event_call(tu, call);
1314 if (set_print_fmt(&tu->tp, is_ret_probe(tu)) < 0)
1317 ret = register_trace_event(&call->event);
1319 kfree(call->print_fmt);
1323 ret = trace_add_event_call(call);
1326 pr_info("Failed to register uprobe event: %s\n",
1327 trace_event_name(call));
1328 kfree(call->print_fmt);
1329 unregister_trace_event(&call->event);
1335 static int unregister_uprobe_event(struct trace_uprobe *tu)
1339 /* tu->event is unregistered in trace_remove_event_call() */
1340 ret = trace_remove_event_call(&tu->tp.call);
1343 kfree(tu->tp.call.print_fmt);
1344 tu->tp.call.print_fmt = NULL;
1348 #ifdef CONFIG_PERF_EVENTS
1349 struct trace_event_call *
1350 create_local_trace_uprobe(char *name, unsigned long offs, bool is_return)
1352 struct trace_uprobe *tu;
1356 ret = kern_path(name, LOOKUP_FOLLOW, &path);
1358 return ERR_PTR(ret);
1360 if (!d_is_reg(path.dentry)) {
1362 return ERR_PTR(-EINVAL);
1366 * local trace_kprobes are not added to probe_list, so they are never
1367 * searched in find_trace_kprobe(). Therefore, there is no concern of
1368 * duplicated name "DUMMY_EVENT" here.
1370 tu = alloc_trace_uprobe(UPROBE_EVENT_SYSTEM, "DUMMY_EVENT", 0,
1374 pr_info("Failed to allocate trace_uprobe.(%d)\n",
1377 return ERR_CAST(tu);
1382 tu->filename = kstrdup(name, GFP_KERNEL);
1383 init_trace_event_call(tu, &tu->tp.call);
1385 if (set_print_fmt(&tu->tp, is_ret_probe(tu)) < 0) {
1390 return &tu->tp.call;
1392 free_trace_uprobe(tu);
1393 return ERR_PTR(ret);
1396 void destroy_local_trace_uprobe(struct trace_event_call *event_call)
1398 struct trace_uprobe *tu;
1400 tu = container_of(event_call, struct trace_uprobe, tp.call);
1402 kfree(tu->tp.call.print_fmt);
1403 tu->tp.call.print_fmt = NULL;
1405 free_trace_uprobe(tu);
1407 #endif /* CONFIG_PERF_EVENTS */
1409 /* Make a trace interface for controling probe points */
1410 static __init int init_uprobe_trace(void)
1412 struct dentry *d_tracer;
1414 d_tracer = tracing_init_dentry();
1415 if (IS_ERR(d_tracer))
1418 trace_create_file("uprobe_events", 0644, d_tracer,
1419 NULL, &uprobe_events_ops);
1420 /* Profile interface */
1421 trace_create_file("uprobe_profile", 0444, d_tracer,
1422 NULL, &uprobe_profile_ops);
1426 fs_initcall(init_uprobe_trace);