2 * uprobes-based tracing events
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 * Copyright (C) IBM Corporation, 2010-2012
18 * Author: Srikar Dronamraju <srikar@linux.vnet.ibm.com>
20 #define pr_fmt(fmt) "trace_kprobe: " fmt
22 #include <linux/module.h>
23 #include <linux/uaccess.h>
24 #include <linux/uprobes.h>
25 #include <linux/namei.h>
26 #include <linux/string.h>
27 #include <linux/rculist.h>
29 #include "trace_probe.h"
31 #define UPROBE_EVENT_SYSTEM "uprobes"
33 struct uprobe_trace_entry_head
{
34 struct trace_entry ent
;
35 unsigned long vaddr
[];
38 #define SIZEOF_TRACE_ENTRY(is_return) \
39 (sizeof(struct uprobe_trace_entry_head) + \
40 sizeof(unsigned long) * (is_return ? 2 : 1))
42 #define DATAOF_TRACE_ENTRY(entry, is_return) \
43 ((void*)(entry) + SIZEOF_TRACE_ENTRY(is_return))
45 struct trace_uprobe_filter
{
48 struct list_head perf_events
;
52 * uprobe event core functions
55 struct list_head list
;
56 struct trace_uprobe_filter filter
;
57 struct uprobe_consumer consumer
;
62 struct trace_probe tp
;
65 #define SIZEOF_TRACE_UPROBE(n) \
66 (offsetof(struct trace_uprobe, tp.args) + \
67 (sizeof(struct probe_arg) * (n)))
69 static int register_uprobe_event(struct trace_uprobe
*tu
);
70 static int unregister_uprobe_event(struct trace_uprobe
*tu
);
72 static DEFINE_MUTEX(uprobe_lock
);
73 static LIST_HEAD(uprobe_list
);
75 struct uprobe_dispatch_data
{
76 struct trace_uprobe
*tu
;
77 unsigned long bp_addr
;
80 static int uprobe_dispatcher(struct uprobe_consumer
*con
, struct pt_regs
*regs
);
81 static int uretprobe_dispatcher(struct uprobe_consumer
*con
,
82 unsigned long func
, struct pt_regs
*regs
);
84 #ifdef CONFIG_STACK_GROWSUP
85 static unsigned long adjust_stack_addr(unsigned long addr
, unsigned int n
)
87 return addr
- (n
* sizeof(long));
90 static unsigned long adjust_stack_addr(unsigned long addr
, unsigned int n
)
92 return addr
+ (n
* sizeof(long));
96 static unsigned long get_user_stack_nth(struct pt_regs
*regs
, unsigned int n
)
99 unsigned long addr
= user_stack_pointer(regs
);
101 addr
= adjust_stack_addr(addr
, n
);
103 if (copy_from_user(&ret
, (void __force __user
*) addr
, sizeof(ret
)))
110 * Uprobes-specific fetch functions
112 #define DEFINE_FETCH_stack(type) \
113 static void FETCH_FUNC_NAME(stack, type)(struct pt_regs *regs, \
114 void *offset, void *dest) \
116 *(type *)dest = (type)get_user_stack_nth(regs, \
117 ((unsigned long)offset)); \
119 DEFINE_BASIC_FETCH_FUNCS(stack
)
120 /* No string on the stack entry */
121 #define fetch_stack_string NULL
122 #define fetch_stack_string_size NULL
124 #define DEFINE_FETCH_memory(type) \
125 static void FETCH_FUNC_NAME(memory, type)(struct pt_regs *regs, \
126 void *addr, void *dest) \
129 void __user *vaddr = (void __force __user *) addr; \
131 if (copy_from_user(&retval, vaddr, sizeof(type))) \
134 *(type *) dest = retval; \
136 DEFINE_BASIC_FETCH_FUNCS(memory
)
138 * Fetch a null-terminated string. Caller MUST set *(u32 *)dest with max
139 * length and relative data location.
141 static void FETCH_FUNC_NAME(memory
, string
)(struct pt_regs
*regs
,
142 void *addr
, void *dest
)
145 u32 rloc
= *(u32
*)dest
;
146 int maxlen
= get_rloc_len(rloc
);
147 u8
*dst
= get_rloc_data(dest
);
148 void __user
*src
= (void __force __user
*) addr
;
153 ret
= strncpy_from_user(dst
, src
, maxlen
);
155 if (ret
< 0) { /* Failed to fetch string */
156 ((u8
*)get_rloc_data(dest
))[0] = '\0';
157 *(u32
*)dest
= make_data_rloc(0, get_rloc_offs(rloc
));
159 *(u32
*)dest
= make_data_rloc(ret
, get_rloc_offs(rloc
));
163 static void FETCH_FUNC_NAME(memory
, string_size
)(struct pt_regs
*regs
,
164 void *addr
, void *dest
)
167 void __user
*vaddr
= (void __force __user
*) addr
;
169 len
= strnlen_user(vaddr
, MAX_STRING_SIZE
);
171 if (len
== 0 || len
> MAX_STRING_SIZE
) /* Failed to check length */
177 static unsigned long translate_user_vaddr(void *file_offset
)
179 unsigned long base_addr
;
180 struct uprobe_dispatch_data
*udd
;
182 udd
= (void *) current
->utask
->vaddr
;
184 base_addr
= udd
->bp_addr
- udd
->tu
->offset
;
185 return base_addr
+ (unsigned long)file_offset
;
188 #define DEFINE_FETCH_file_offset(type) \
189 static void FETCH_FUNC_NAME(file_offset, type)(struct pt_regs *regs, \
190 void *offset, void *dest)\
192 void *vaddr = (void *)translate_user_vaddr(offset); \
194 FETCH_FUNC_NAME(memory, type)(regs, vaddr, dest); \
196 DEFINE_BASIC_FETCH_FUNCS(file_offset
)
197 DEFINE_FETCH_file_offset(string
)
198 DEFINE_FETCH_file_offset(string_size
)
200 /* Fetch type information table */
201 static const struct fetch_type uprobes_fetch_type_table
[] = {
203 [FETCH_TYPE_STRING
] = __ASSIGN_FETCH_TYPE("string", string
, string
,
204 sizeof(u32
), 1, "__data_loc char[]"),
205 [FETCH_TYPE_STRSIZE
] = __ASSIGN_FETCH_TYPE("string_size", u32
,
206 string_size
, sizeof(u32
), 0, "u32"),
208 ASSIGN_FETCH_TYPE(u8
, u8
, 0),
209 ASSIGN_FETCH_TYPE(u16
, u16
, 0),
210 ASSIGN_FETCH_TYPE(u32
, u32
, 0),
211 ASSIGN_FETCH_TYPE(u64
, u64
, 0),
212 ASSIGN_FETCH_TYPE(s8
, u8
, 1),
213 ASSIGN_FETCH_TYPE(s16
, u16
, 1),
214 ASSIGN_FETCH_TYPE(s32
, u32
, 1),
215 ASSIGN_FETCH_TYPE(s64
, u64
, 1),
216 ASSIGN_FETCH_TYPE_ALIAS(x8
, u8
, u8
, 0),
217 ASSIGN_FETCH_TYPE_ALIAS(x16
, u16
, u16
, 0),
218 ASSIGN_FETCH_TYPE_ALIAS(x32
, u32
, u32
, 0),
219 ASSIGN_FETCH_TYPE_ALIAS(x64
, u64
, u64
, 0),
221 ASSIGN_FETCH_TYPE_END
224 static inline void init_trace_uprobe_filter(struct trace_uprobe_filter
*filter
)
226 rwlock_init(&filter
->rwlock
);
227 filter
->nr_systemwide
= 0;
228 INIT_LIST_HEAD(&filter
->perf_events
);
231 static inline bool uprobe_filter_is_empty(struct trace_uprobe_filter
*filter
)
233 return !filter
->nr_systemwide
&& list_empty(&filter
->perf_events
);
236 static inline bool is_ret_probe(struct trace_uprobe
*tu
)
238 return tu
->consumer
.ret_handler
!= NULL
;
242 * Allocate new trace_uprobe and initialize it (including uprobes).
244 static struct trace_uprobe
*
245 alloc_trace_uprobe(const char *group
, const char *event
, int nargs
, bool is_ret
)
247 struct trace_uprobe
*tu
;
249 if (!event
|| !is_good_name(event
))
250 return ERR_PTR(-EINVAL
);
252 if (!group
|| !is_good_name(group
))
253 return ERR_PTR(-EINVAL
);
255 tu
= kzalloc(SIZEOF_TRACE_UPROBE(nargs
), GFP_KERNEL
);
257 return ERR_PTR(-ENOMEM
);
259 tu
->tp
.call
.class = &tu
->tp
.class;
260 tu
->tp
.call
.name
= kstrdup(event
, GFP_KERNEL
);
261 if (!tu
->tp
.call
.name
)
264 tu
->tp
.class.system
= kstrdup(group
, GFP_KERNEL
);
265 if (!tu
->tp
.class.system
)
268 INIT_LIST_HEAD(&tu
->list
);
269 INIT_LIST_HEAD(&tu
->tp
.files
);
270 tu
->consumer
.handler
= uprobe_dispatcher
;
272 tu
->consumer
.ret_handler
= uretprobe_dispatcher
;
273 init_trace_uprobe_filter(&tu
->filter
);
277 kfree(tu
->tp
.call
.name
);
280 return ERR_PTR(-ENOMEM
);
283 static void free_trace_uprobe(struct trace_uprobe
*tu
)
287 for (i
= 0; i
< tu
->tp
.nr_args
; i
++)
288 traceprobe_free_probe_arg(&tu
->tp
.args
[i
]);
291 kfree(tu
->tp
.call
.class->system
);
292 kfree(tu
->tp
.call
.name
);
297 static struct trace_uprobe
*find_probe_event(const char *event
, const char *group
)
299 struct trace_uprobe
*tu
;
301 list_for_each_entry(tu
, &uprobe_list
, list
)
302 if (strcmp(trace_event_name(&tu
->tp
.call
), event
) == 0 &&
303 strcmp(tu
->tp
.call
.class->system
, group
) == 0)
309 /* Unregister a trace_uprobe and probe_event: call with locking uprobe_lock */
310 static int unregister_trace_uprobe(struct trace_uprobe
*tu
)
314 ret
= unregister_uprobe_event(tu
);
319 free_trace_uprobe(tu
);
323 /* Register a trace_uprobe and probe_event */
324 static int register_trace_uprobe(struct trace_uprobe
*tu
)
326 struct trace_uprobe
*old_tu
;
329 mutex_lock(&uprobe_lock
);
331 /* register as an event */
332 old_tu
= find_probe_event(trace_event_name(&tu
->tp
.call
),
333 tu
->tp
.call
.class->system
);
335 /* delete old event */
336 ret
= unregister_trace_uprobe(old_tu
);
341 ret
= register_uprobe_event(tu
);
343 pr_warn("Failed to register probe event(%d)\n", ret
);
347 list_add_tail(&tu
->list
, &uprobe_list
);
350 mutex_unlock(&uprobe_lock
);
357 * - Add uprobe: p|r[:[GRP/]EVENT] PATH:OFFSET [FETCHARGS]
359 * - Remove uprobe: -:[GRP/]EVENT
361 static int create_trace_uprobe(int argc
, char **argv
)
363 struct trace_uprobe
*tu
;
365 char *arg
, *event
, *group
, *filename
;
366 char buf
[MAX_EVENT_NAME_LEN
];
368 unsigned long offset
;
369 bool is_delete
, is_return
;
379 /* argc must be >= 1 */
380 if (argv
[0][0] == '-')
382 else if (argv
[0][0] == 'r')
384 else if (argv
[0][0] != 'p') {
385 pr_info("Probe definition must be started with 'p', 'r' or '-'.\n");
389 if (argv
[0][1] == ':') {
391 arg
= strchr(event
, '/');
398 if (strlen(group
) == 0) {
399 pr_info("Group name is not specified\n");
403 if (strlen(event
) == 0) {
404 pr_info("Event name is not specified\n");
409 group
= UPROBE_EVENT_SYSTEM
;
415 pr_info("Delete command needs an event name.\n");
418 mutex_lock(&uprobe_lock
);
419 tu
= find_probe_event(event
, group
);
422 mutex_unlock(&uprobe_lock
);
423 pr_info("Event %s/%s doesn't exist.\n", group
, event
);
426 /* delete an event */
427 ret
= unregister_trace_uprobe(tu
);
428 mutex_unlock(&uprobe_lock
);
433 pr_info("Probe point is not specified.\n");
436 /* Find the last occurrence, in case the path contains ':' too. */
437 arg
= strrchr(argv
[1], ':');
440 goto fail_address_parse
;
445 ret
= kern_path(filename
, LOOKUP_FOLLOW
, &path
);
447 goto fail_address_parse
;
449 inode
= igrab(d_inode(path
.dentry
));
452 if (!inode
|| !S_ISREG(inode
->i_mode
)) {
454 goto fail_address_parse
;
457 ret
= kstrtoul(arg
, 0, &offset
);
459 goto fail_address_parse
;
469 tail
= kstrdup(kbasename(filename
), GFP_KERNEL
);
472 goto fail_address_parse
;
475 ptr
= strpbrk(tail
, ".-_");
479 snprintf(buf
, MAX_EVENT_NAME_LEN
, "%c_%s_0x%lx", 'p', tail
, offset
);
484 tu
= alloc_trace_uprobe(group
, event
, argc
, is_return
);
486 pr_info("Failed to allocate trace_uprobe.(%d)\n", (int)PTR_ERR(tu
));
488 goto fail_address_parse
;
492 tu
->filename
= kstrdup(filename
, GFP_KERNEL
);
495 pr_info("Failed to allocate filename.\n");
500 /* parse arguments */
502 for (i
= 0; i
< argc
&& i
< MAX_TRACE_ARGS
; i
++) {
503 struct probe_arg
*parg
= &tu
->tp
.args
[i
];
505 /* Increment count for freeing args in error case */
508 /* Parse argument name */
509 arg
= strchr(argv
[i
], '=');
512 parg
->name
= kstrdup(argv
[i
], GFP_KERNEL
);
515 /* If argument name is omitted, set "argN" */
516 snprintf(buf
, MAX_EVENT_NAME_LEN
, "arg%d", i
+ 1);
517 parg
->name
= kstrdup(buf
, GFP_KERNEL
);
521 pr_info("Failed to allocate argument[%d] name.\n", i
);
526 if (!is_good_name(parg
->name
)) {
527 pr_info("Invalid argument[%d] name: %s\n", i
, parg
->name
);
532 if (traceprobe_conflict_field_name(parg
->name
, tu
->tp
.args
, i
)) {
533 pr_info("Argument[%d] name '%s' conflicts with "
534 "another field.\n", i
, argv
[i
]);
539 /* Parse fetch argument */
540 ret
= traceprobe_parse_probe_arg(arg
, &tu
->tp
.size
, parg
,
542 uprobes_fetch_type_table
);
544 pr_info("Parse error at argument[%d]. (%d)\n", i
, ret
);
549 ret
= register_trace_uprobe(tu
);
555 free_trace_uprobe(tu
);
561 pr_info("Failed to parse address or file.\n");
566 static int cleanup_all_probes(void)
568 struct trace_uprobe
*tu
;
571 mutex_lock(&uprobe_lock
);
572 while (!list_empty(&uprobe_list
)) {
573 tu
= list_entry(uprobe_list
.next
, struct trace_uprobe
, list
);
574 ret
= unregister_trace_uprobe(tu
);
578 mutex_unlock(&uprobe_lock
);
582 /* Probes listing interfaces */
583 static void *probes_seq_start(struct seq_file
*m
, loff_t
*pos
)
585 mutex_lock(&uprobe_lock
);
586 return seq_list_start(&uprobe_list
, *pos
);
589 static void *probes_seq_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
591 return seq_list_next(v
, &uprobe_list
, pos
);
594 static void probes_seq_stop(struct seq_file
*m
, void *v
)
596 mutex_unlock(&uprobe_lock
);
599 static int probes_seq_show(struct seq_file
*m
, void *v
)
601 struct trace_uprobe
*tu
= v
;
602 char c
= is_ret_probe(tu
) ? 'r' : 'p';
605 seq_printf(m
, "%c:%s/%s", c
, tu
->tp
.call
.class->system
,
606 trace_event_name(&tu
->tp
.call
));
607 seq_printf(m
, " %s:", tu
->filename
);
609 /* Don't print "0x (null)" when offset is 0 */
611 seq_printf(m
, "0x%p", (void *)tu
->offset
);
613 switch (sizeof(void *)) {
615 seq_printf(m
, "0x00000000");
619 seq_printf(m
, "0x0000000000000000");
624 for (i
= 0; i
< tu
->tp
.nr_args
; i
++)
625 seq_printf(m
, " %s=%s", tu
->tp
.args
[i
].name
, tu
->tp
.args
[i
].comm
);
631 static const struct seq_operations probes_seq_op
= {
632 .start
= probes_seq_start
,
633 .next
= probes_seq_next
,
634 .stop
= probes_seq_stop
,
635 .show
= probes_seq_show
638 static int probes_open(struct inode
*inode
, struct file
*file
)
642 if ((file
->f_mode
& FMODE_WRITE
) && (file
->f_flags
& O_TRUNC
)) {
643 ret
= cleanup_all_probes();
648 return seq_open(file
, &probes_seq_op
);
651 static ssize_t
probes_write(struct file
*file
, const char __user
*buffer
,
652 size_t count
, loff_t
*ppos
)
654 return traceprobe_probes_write(file
, buffer
, count
, ppos
, create_trace_uprobe
);
657 static const struct file_operations uprobe_events_ops
= {
658 .owner
= THIS_MODULE
,
662 .release
= seq_release
,
663 .write
= probes_write
,
666 /* Probes profiling interfaces */
667 static int probes_profile_seq_show(struct seq_file
*m
, void *v
)
669 struct trace_uprobe
*tu
= v
;
671 seq_printf(m
, " %s %-44s %15lu\n", tu
->filename
,
672 trace_event_name(&tu
->tp
.call
), tu
->nhit
);
676 static const struct seq_operations profile_seq_op
= {
677 .start
= probes_seq_start
,
678 .next
= probes_seq_next
,
679 .stop
= probes_seq_stop
,
680 .show
= probes_profile_seq_show
683 static int profile_open(struct inode
*inode
, struct file
*file
)
685 return seq_open(file
, &profile_seq_op
);
688 static const struct file_operations uprobe_profile_ops
= {
689 .owner
= THIS_MODULE
,
690 .open
= profile_open
,
693 .release
= seq_release
,
696 struct uprobe_cpu_buffer
{
700 static struct uprobe_cpu_buffer __percpu
*uprobe_cpu_buffer
;
701 static int uprobe_buffer_refcnt
;
703 static int uprobe_buffer_init(void)
707 uprobe_cpu_buffer
= alloc_percpu(struct uprobe_cpu_buffer
);
708 if (uprobe_cpu_buffer
== NULL
)
711 for_each_possible_cpu(cpu
) {
712 struct page
*p
= alloc_pages_node(cpu_to_node(cpu
),
718 per_cpu_ptr(uprobe_cpu_buffer
, cpu
)->buf
= page_address(p
);
719 mutex_init(&per_cpu_ptr(uprobe_cpu_buffer
, cpu
)->mutex
);
725 for_each_possible_cpu(cpu
) {
728 free_page((unsigned long)per_cpu_ptr(uprobe_cpu_buffer
, cpu
)->buf
);
731 free_percpu(uprobe_cpu_buffer
);
735 static int uprobe_buffer_enable(void)
739 BUG_ON(!mutex_is_locked(&event_mutex
));
741 if (uprobe_buffer_refcnt
++ == 0) {
742 ret
= uprobe_buffer_init();
744 uprobe_buffer_refcnt
--;
750 static void uprobe_buffer_disable(void)
754 BUG_ON(!mutex_is_locked(&event_mutex
));
756 if (--uprobe_buffer_refcnt
== 0) {
757 for_each_possible_cpu(cpu
)
758 free_page((unsigned long)per_cpu_ptr(uprobe_cpu_buffer
,
761 free_percpu(uprobe_cpu_buffer
);
762 uprobe_cpu_buffer
= NULL
;
766 static struct uprobe_cpu_buffer
*uprobe_buffer_get(void)
768 struct uprobe_cpu_buffer
*ucb
;
771 cpu
= raw_smp_processor_id();
772 ucb
= per_cpu_ptr(uprobe_cpu_buffer
, cpu
);
775 * Use per-cpu buffers for fastest access, but we might migrate
776 * so the mutex makes sure we have sole access to it.
778 mutex_lock(&ucb
->mutex
);
783 static void uprobe_buffer_put(struct uprobe_cpu_buffer
*ucb
)
785 mutex_unlock(&ucb
->mutex
);
788 static void __uprobe_trace_func(struct trace_uprobe
*tu
,
789 unsigned long func
, struct pt_regs
*regs
,
790 struct uprobe_cpu_buffer
*ucb
, int dsize
,
791 struct trace_event_file
*trace_file
)
793 struct uprobe_trace_entry_head
*entry
;
794 struct ring_buffer_event
*event
;
795 struct ring_buffer
*buffer
;
798 struct trace_event_call
*call
= &tu
->tp
.call
;
800 WARN_ON(call
!= trace_file
->event_call
);
802 if (WARN_ON_ONCE(tu
->tp
.size
+ dsize
> PAGE_SIZE
))
805 if (trace_trigger_soft_disabled(trace_file
))
808 esize
= SIZEOF_TRACE_ENTRY(is_ret_probe(tu
));
809 size
= esize
+ tu
->tp
.size
+ dsize
;
810 event
= trace_event_buffer_lock_reserve(&buffer
, trace_file
,
811 call
->event
.type
, size
, 0, 0);
815 entry
= ring_buffer_event_data(event
);
816 if (is_ret_probe(tu
)) {
817 entry
->vaddr
[0] = func
;
818 entry
->vaddr
[1] = instruction_pointer(regs
);
819 data
= DATAOF_TRACE_ENTRY(entry
, true);
821 entry
->vaddr
[0] = instruction_pointer(regs
);
822 data
= DATAOF_TRACE_ENTRY(entry
, false);
825 memcpy(data
, ucb
->buf
, tu
->tp
.size
+ dsize
);
827 event_trigger_unlock_commit(trace_file
, buffer
, event
, entry
, 0, 0);
831 static int uprobe_trace_func(struct trace_uprobe
*tu
, struct pt_regs
*regs
,
832 struct uprobe_cpu_buffer
*ucb
, int dsize
)
834 struct event_file_link
*link
;
836 if (is_ret_probe(tu
))
840 list_for_each_entry_rcu(link
, &tu
->tp
.files
, list
)
841 __uprobe_trace_func(tu
, 0, regs
, ucb
, dsize
, link
->file
);
847 static void uretprobe_trace_func(struct trace_uprobe
*tu
, unsigned long func
,
848 struct pt_regs
*regs
,
849 struct uprobe_cpu_buffer
*ucb
, int dsize
)
851 struct event_file_link
*link
;
854 list_for_each_entry_rcu(link
, &tu
->tp
.files
, list
)
855 __uprobe_trace_func(tu
, func
, regs
, ucb
, dsize
, link
->file
);
859 /* Event entry printers */
860 static enum print_line_t
861 print_uprobe_event(struct trace_iterator
*iter
, int flags
, struct trace_event
*event
)
863 struct uprobe_trace_entry_head
*entry
;
864 struct trace_seq
*s
= &iter
->seq
;
865 struct trace_uprobe
*tu
;
869 entry
= (struct uprobe_trace_entry_head
*)iter
->ent
;
870 tu
= container_of(event
, struct trace_uprobe
, tp
.call
.event
);
872 if (is_ret_probe(tu
)) {
873 trace_seq_printf(s
, "%s: (0x%lx <- 0x%lx)",
874 trace_event_name(&tu
->tp
.call
),
875 entry
->vaddr
[1], entry
->vaddr
[0]);
876 data
= DATAOF_TRACE_ENTRY(entry
, true);
878 trace_seq_printf(s
, "%s: (0x%lx)",
879 trace_event_name(&tu
->tp
.call
),
881 data
= DATAOF_TRACE_ENTRY(entry
, false);
884 for (i
= 0; i
< tu
->tp
.nr_args
; i
++) {
885 struct probe_arg
*parg
= &tu
->tp
.args
[i
];
887 if (!parg
->type
->print(s
, parg
->name
, data
+ parg
->offset
, entry
))
891 trace_seq_putc(s
, '\n');
894 return trace_handle_return(s
);
897 typedef bool (*filter_func_t
)(struct uprobe_consumer
*self
,
898 enum uprobe_filter_ctx ctx
,
899 struct mm_struct
*mm
);
902 probe_event_enable(struct trace_uprobe
*tu
, struct trace_event_file
*file
,
903 filter_func_t filter
)
905 bool enabled
= trace_probe_is_enabled(&tu
->tp
);
906 struct event_file_link
*link
= NULL
;
910 if (tu
->tp
.flags
& TP_FLAG_PROFILE
)
913 link
= kmalloc(sizeof(*link
), GFP_KERNEL
);
918 list_add_tail_rcu(&link
->list
, &tu
->tp
.files
);
920 tu
->tp
.flags
|= TP_FLAG_TRACE
;
922 if (tu
->tp
.flags
& TP_FLAG_TRACE
)
925 tu
->tp
.flags
|= TP_FLAG_PROFILE
;
928 WARN_ON(!uprobe_filter_is_empty(&tu
->filter
));
933 ret
= uprobe_buffer_enable();
937 tu
->consumer
.filter
= filter
;
938 ret
= uprobe_register(tu
->inode
, tu
->offset
, &tu
->consumer
);
945 uprobe_buffer_disable();
949 list_del(&link
->list
);
951 tu
->tp
.flags
&= ~TP_FLAG_TRACE
;
953 tu
->tp
.flags
&= ~TP_FLAG_PROFILE
;
959 probe_event_disable(struct trace_uprobe
*tu
, struct trace_event_file
*file
)
961 if (!trace_probe_is_enabled(&tu
->tp
))
965 struct event_file_link
*link
;
967 link
= find_event_file_link(&tu
->tp
, file
);
971 list_del_rcu(&link
->list
);
972 /* synchronize with u{,ret}probe_trace_func */
976 if (!list_empty(&tu
->tp
.files
))
980 WARN_ON(!uprobe_filter_is_empty(&tu
->filter
));
982 uprobe_unregister(tu
->inode
, tu
->offset
, &tu
->consumer
);
983 tu
->tp
.flags
&= file
? ~TP_FLAG_TRACE
: ~TP_FLAG_PROFILE
;
985 uprobe_buffer_disable();
988 static int uprobe_event_define_fields(struct trace_event_call
*event_call
)
991 struct uprobe_trace_entry_head field
;
992 struct trace_uprobe
*tu
= event_call
->data
;
994 if (is_ret_probe(tu
)) {
995 DEFINE_FIELD(unsigned long, vaddr
[0], FIELD_STRING_FUNC
, 0);
996 DEFINE_FIELD(unsigned long, vaddr
[1], FIELD_STRING_RETIP
, 0);
997 size
= SIZEOF_TRACE_ENTRY(true);
999 DEFINE_FIELD(unsigned long, vaddr
[0], FIELD_STRING_IP
, 0);
1000 size
= SIZEOF_TRACE_ENTRY(false);
1002 /* Set argument names as fields */
1003 for (i
= 0; i
< tu
->tp
.nr_args
; i
++) {
1004 struct probe_arg
*parg
= &tu
->tp
.args
[i
];
1006 ret
= trace_define_field(event_call
, parg
->type
->fmttype
,
1007 parg
->name
, size
+ parg
->offset
,
1008 parg
->type
->size
, parg
->type
->is_signed
,
1017 #ifdef CONFIG_PERF_EVENTS
1019 __uprobe_perf_filter(struct trace_uprobe_filter
*filter
, struct mm_struct
*mm
)
1021 struct perf_event
*event
;
1023 if (filter
->nr_systemwide
)
1026 list_for_each_entry(event
, &filter
->perf_events
, hw
.tp_list
) {
1027 if (event
->hw
.target
->mm
== mm
)
1035 uprobe_filter_event(struct trace_uprobe
*tu
, struct perf_event
*event
)
1037 return __uprobe_perf_filter(&tu
->filter
, event
->hw
.target
->mm
);
1040 static int uprobe_perf_close(struct trace_uprobe
*tu
, struct perf_event
*event
)
1044 write_lock(&tu
->filter
.rwlock
);
1045 if (event
->hw
.target
) {
1046 list_del(&event
->hw
.tp_list
);
1047 done
= tu
->filter
.nr_systemwide
||
1048 (event
->hw
.target
->flags
& PF_EXITING
) ||
1049 uprobe_filter_event(tu
, event
);
1051 tu
->filter
.nr_systemwide
--;
1052 done
= tu
->filter
.nr_systemwide
;
1054 write_unlock(&tu
->filter
.rwlock
);
1057 return uprobe_apply(tu
->inode
, tu
->offset
, &tu
->consumer
, false);
1062 static int uprobe_perf_open(struct trace_uprobe
*tu
, struct perf_event
*event
)
1067 write_lock(&tu
->filter
.rwlock
);
1068 if (event
->hw
.target
) {
1070 * event->parent != NULL means copy_process(), we can avoid
1071 * uprobe_apply(). current->mm must be probed and we can rely
1072 * on dup_mmap() which preserves the already installed bp's.
1074 * attr.enable_on_exec means that exec/mmap will install the
1075 * breakpoints we need.
1077 done
= tu
->filter
.nr_systemwide
||
1078 event
->parent
|| event
->attr
.enable_on_exec
||
1079 uprobe_filter_event(tu
, event
);
1080 list_add(&event
->hw
.tp_list
, &tu
->filter
.perf_events
);
1082 done
= tu
->filter
.nr_systemwide
;
1083 tu
->filter
.nr_systemwide
++;
1085 write_unlock(&tu
->filter
.rwlock
);
1089 err
= uprobe_apply(tu
->inode
, tu
->offset
, &tu
->consumer
, true);
1091 uprobe_perf_close(tu
, event
);
1096 static bool uprobe_perf_filter(struct uprobe_consumer
*uc
,
1097 enum uprobe_filter_ctx ctx
, struct mm_struct
*mm
)
1099 struct trace_uprobe
*tu
;
1102 tu
= container_of(uc
, struct trace_uprobe
, consumer
);
1103 read_lock(&tu
->filter
.rwlock
);
1104 ret
= __uprobe_perf_filter(&tu
->filter
, mm
);
1105 read_unlock(&tu
->filter
.rwlock
);
1110 static void __uprobe_perf_func(struct trace_uprobe
*tu
,
1111 unsigned long func
, struct pt_regs
*regs
,
1112 struct uprobe_cpu_buffer
*ucb
, int dsize
)
1114 struct trace_event_call
*call
= &tu
->tp
.call
;
1115 struct uprobe_trace_entry_head
*entry
;
1116 struct bpf_prog
*prog
= call
->prog
;
1117 struct hlist_head
*head
;
1122 if (prog
&& !trace_call_bpf(prog
, regs
))
1125 esize
= SIZEOF_TRACE_ENTRY(is_ret_probe(tu
));
1127 size
= esize
+ tu
->tp
.size
+ dsize
;
1128 size
= ALIGN(size
+ sizeof(u32
), sizeof(u64
)) - sizeof(u32
);
1129 if (WARN_ONCE(size
> PERF_MAX_TRACE_SIZE
, "profile buffer not large enough"))
1133 head
= this_cpu_ptr(call
->perf_events
);
1134 if (hlist_empty(head
))
1137 entry
= perf_trace_buf_alloc(size
, NULL
, &rctx
);
1141 if (is_ret_probe(tu
)) {
1142 entry
->vaddr
[0] = func
;
1143 entry
->vaddr
[1] = instruction_pointer(regs
);
1144 data
= DATAOF_TRACE_ENTRY(entry
, true);
1146 entry
->vaddr
[0] = instruction_pointer(regs
);
1147 data
= DATAOF_TRACE_ENTRY(entry
, false);
1150 memcpy(data
, ucb
->buf
, tu
->tp
.size
+ dsize
);
1152 if (size
- esize
> tu
->tp
.size
+ dsize
) {
1153 int len
= tu
->tp
.size
+ dsize
;
1155 memset(data
+ len
, 0, size
- esize
- len
);
1158 perf_trace_buf_submit(entry
, size
, rctx
, call
->event
.type
, 1, regs
,
1164 /* uprobe profile handler */
1165 static int uprobe_perf_func(struct trace_uprobe
*tu
, struct pt_regs
*regs
,
1166 struct uprobe_cpu_buffer
*ucb
, int dsize
)
1168 if (!uprobe_perf_filter(&tu
->consumer
, 0, current
->mm
))
1169 return UPROBE_HANDLER_REMOVE
;
1171 if (!is_ret_probe(tu
))
1172 __uprobe_perf_func(tu
, 0, regs
, ucb
, dsize
);
1176 static void uretprobe_perf_func(struct trace_uprobe
*tu
, unsigned long func
,
1177 struct pt_regs
*regs
,
1178 struct uprobe_cpu_buffer
*ucb
, int dsize
)
1180 __uprobe_perf_func(tu
, func
, regs
, ucb
, dsize
);
1182 #endif /* CONFIG_PERF_EVENTS */
1185 trace_uprobe_register(struct trace_event_call
*event
, enum trace_reg type
,
1188 struct trace_uprobe
*tu
= event
->data
;
1189 struct trace_event_file
*file
= data
;
1192 case TRACE_REG_REGISTER
:
1193 return probe_event_enable(tu
, file
, NULL
);
1195 case TRACE_REG_UNREGISTER
:
1196 probe_event_disable(tu
, file
);
1199 #ifdef CONFIG_PERF_EVENTS
1200 case TRACE_REG_PERF_REGISTER
:
1201 return probe_event_enable(tu
, NULL
, uprobe_perf_filter
);
1203 case TRACE_REG_PERF_UNREGISTER
:
1204 probe_event_disable(tu
, NULL
);
1207 case TRACE_REG_PERF_OPEN
:
1208 return uprobe_perf_open(tu
, data
);
1210 case TRACE_REG_PERF_CLOSE
:
1211 return uprobe_perf_close(tu
, data
);
1220 static int uprobe_dispatcher(struct uprobe_consumer
*con
, struct pt_regs
*regs
)
1222 struct trace_uprobe
*tu
;
1223 struct uprobe_dispatch_data udd
;
1224 struct uprobe_cpu_buffer
*ucb
;
1229 tu
= container_of(con
, struct trace_uprobe
, consumer
);
1233 udd
.bp_addr
= instruction_pointer(regs
);
1235 current
->utask
->vaddr
= (unsigned long) &udd
;
1237 if (WARN_ON_ONCE(!uprobe_cpu_buffer
))
1240 dsize
= __get_data_size(&tu
->tp
, regs
);
1241 esize
= SIZEOF_TRACE_ENTRY(is_ret_probe(tu
));
1243 ucb
= uprobe_buffer_get();
1244 store_trace_args(esize
, &tu
->tp
, regs
, ucb
->buf
, dsize
);
1246 if (tu
->tp
.flags
& TP_FLAG_TRACE
)
1247 ret
|= uprobe_trace_func(tu
, regs
, ucb
, dsize
);
1249 #ifdef CONFIG_PERF_EVENTS
1250 if (tu
->tp
.flags
& TP_FLAG_PROFILE
)
1251 ret
|= uprobe_perf_func(tu
, regs
, ucb
, dsize
);
1253 uprobe_buffer_put(ucb
);
1257 static int uretprobe_dispatcher(struct uprobe_consumer
*con
,
1258 unsigned long func
, struct pt_regs
*regs
)
1260 struct trace_uprobe
*tu
;
1261 struct uprobe_dispatch_data udd
;
1262 struct uprobe_cpu_buffer
*ucb
;
1265 tu
= container_of(con
, struct trace_uprobe
, consumer
);
1270 current
->utask
->vaddr
= (unsigned long) &udd
;
1272 if (WARN_ON_ONCE(!uprobe_cpu_buffer
))
1275 dsize
= __get_data_size(&tu
->tp
, regs
);
1276 esize
= SIZEOF_TRACE_ENTRY(is_ret_probe(tu
));
1278 ucb
= uprobe_buffer_get();
1279 store_trace_args(esize
, &tu
->tp
, regs
, ucb
->buf
, dsize
);
1281 if (tu
->tp
.flags
& TP_FLAG_TRACE
)
1282 uretprobe_trace_func(tu
, func
, regs
, ucb
, dsize
);
1284 #ifdef CONFIG_PERF_EVENTS
1285 if (tu
->tp
.flags
& TP_FLAG_PROFILE
)
1286 uretprobe_perf_func(tu
, func
, regs
, ucb
, dsize
);
1288 uprobe_buffer_put(ucb
);
1292 static struct trace_event_functions uprobe_funcs
= {
1293 .trace
= print_uprobe_event
1296 static int register_uprobe_event(struct trace_uprobe
*tu
)
1298 struct trace_event_call
*call
= &tu
->tp
.call
;
1301 /* Initialize trace_event_call */
1302 INIT_LIST_HEAD(&call
->class->fields
);
1303 call
->event
.funcs
= &uprobe_funcs
;
1304 call
->class->define_fields
= uprobe_event_define_fields
;
1306 if (set_print_fmt(&tu
->tp
, is_ret_probe(tu
)) < 0)
1309 ret
= register_trace_event(&call
->event
);
1311 kfree(call
->print_fmt
);
1315 call
->flags
= TRACE_EVENT_FL_UPROBE
;
1316 call
->class->reg
= trace_uprobe_register
;
1318 ret
= trace_add_event_call(call
);
1321 pr_info("Failed to register uprobe event: %s\n",
1322 trace_event_name(call
));
1323 kfree(call
->print_fmt
);
1324 unregister_trace_event(&call
->event
);
1330 static int unregister_uprobe_event(struct trace_uprobe
*tu
)
1334 /* tu->event is unregistered in trace_remove_event_call() */
1335 ret
= trace_remove_event_call(&tu
->tp
.call
);
1338 kfree(tu
->tp
.call
.print_fmt
);
1339 tu
->tp
.call
.print_fmt
= NULL
;
1343 /* Make a trace interface for controling probe points */
1344 static __init
int init_uprobe_trace(void)
1346 struct dentry
*d_tracer
;
1348 d_tracer
= tracing_init_dentry();
1349 if (IS_ERR(d_tracer
))
1352 trace_create_file("uprobe_events", 0644, d_tracer
,
1353 NULL
, &uprobe_events_ops
);
1354 /* Profile interface */
1355 trace_create_file("uprobe_profile", 0444, d_tracer
,
1356 NULL
, &uprobe_profile_ops
);
1360 fs_initcall(init_uprobe_trace
);