1 /* SPDX-License-Identifier: (GPL-2.0-only OR LGPL-2.1-only)
3 * probes/lttng-uprobes.c
5 * LTTng uprobes integration module.
7 * Copyright (C) 2013 Yannick Brosseau <yannick.brosseau@gmail.com>
8 * Copyright (C) 2009-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
12 #include <wrapper/fdtable.h>
13 #include <linux/file.h>
14 #include <linux/list.h>
15 #include <linux/module.h>
16 #include <linux/namei.h>
17 #include <linux/slab.h>
18 #include <linux/uaccess.h>
19 #include <linux/uprobes.h>
21 #include <lttng/events.h>
22 #include <lttng/events-internal.h>
23 #include <lttng/tracer.h>
24 #include <wrapper/irqflags.h>
25 #include <ringbuffer/frontend_types.h>
26 #include <wrapper/vmalloc.h>
29 int lttng_uprobes_event_handler_pre(struct uprobe_consumer
*uc
, struct pt_regs
*regs
)
31 struct lttng_uprobe_handler
*uprobe_handler
=
32 container_of(uc
, struct lttng_uprobe_handler
, up_consumer
);
33 struct lttng_kernel_event_common
*event
= uprobe_handler
->event
;
34 struct lttng_kernel_probe_ctx lttng_probe_ctx
= {
36 .interruptible
= !lttng_regs_irqs_disabled(regs
),
42 switch (event
->type
) {
43 case LTTNG_KERNEL_EVENT_TYPE_RECORDER
:
45 struct lttng_kernel_event_recorder
*event_recorder
=
46 container_of(event
, struct lttng_kernel_event_recorder
, parent
);
47 struct lttng_kernel_channel_buffer
*chan
= event_recorder
->chan
;
49 if (unlikely(!LTTNG_READ_ONCE(chan
->parent
.session
->active
)))
51 if (unlikely(!LTTNG_READ_ONCE(chan
->parent
.enabled
)))
55 case LTTNG_KERNEL_EVENT_TYPE_NOTIFIER
:
61 if (unlikely(!LTTNG_READ_ONCE(event
->enabled
)))
64 switch (event
->type
) {
65 case LTTNG_KERNEL_EVENT_TYPE_RECORDER
:
67 struct lttng_kernel_event_recorder
*event_recorder
=
68 container_of(event
, struct lttng_kernel_event_recorder
, parent
);
69 struct lttng_kernel_channel_buffer
*chan
= event_recorder
->chan
;
70 struct lttng_kernel_ring_buffer_ctx ctx
;
73 lib_ring_buffer_ctx_init(&ctx
, event_recorder
,
74 sizeof(payload
), lttng_alignof(payload
), <tng_probe_ctx
);
76 ret
= chan
->ops
->event_reserve(&ctx
);
81 payload
.ip
= (unsigned long)instruction_pointer(regs
);
83 chan
->ops
->event_write(&ctx
, &payload
, sizeof(payload
), lttng_alignof(payload
));
84 chan
->ops
->event_commit(&ctx
);
87 case LTTNG_KERNEL_EVENT_TYPE_NOTIFIER
:
89 struct lttng_kernel_event_notifier
*event_notifier
=
90 container_of(event
, struct lttng_kernel_event_notifier
, parent
);
91 struct lttng_kernel_notification_ctx notif_ctx
;
93 notif_ctx
.eval_capture
= LTTNG_READ_ONCE(event_notifier
->eval_capture
);
94 event_notifier
->notification_send(event_notifier
, NULL
, NULL
, ¬if_ctx
);
103 static const struct lttng_kernel_event_field
*event_fields
[] = {
104 lttng_kernel_static_event_field("ip",
105 lttng_kernel_static_type_integer_from_type(unsigned long, __BYTE_ORDER
, 16),
109 static const struct lttng_kernel_tracepoint_class tp_class
= {
110 .nr_fields
= ARRAY_SIZE(event_fields
),
111 .fields
= event_fields
,
115 * Create event description.
118 int lttng_create_uprobe_event(const char *name
, struct lttng_kernel_event_common
*event
)
120 struct lttng_kernel_event_desc
*desc
;
123 desc
= kzalloc(sizeof(*desc
), GFP_KERNEL
);
126 desc
->tp_class
= &tp_class
;
127 desc
->event_name
= kstrdup(name
, GFP_KERNEL
);
128 if (!desc
->event_name
) {
132 desc
->owner
= THIS_MODULE
;
133 event
->priv
->desc
= desc
;
143 * Returns the inode struct from the current task and an fd. The inode is
144 * grabbed by this function and must be put once we are done with it using
147 static struct inode
*get_inode_from_fd(int fd
)
154 * Returns the file backing the given fd. Needs to be done inside an RCU
157 file
= lttng_lookup_fdget_rcu(fd
);
159 printk(KERN_WARNING
"LTTng: Cannot access file backing the fd(%d)\n", fd
);
164 /* Grab a reference on the inode. */
165 inode
= igrab(file
->f_path
.dentry
->d_inode
);
167 printk(KERN_WARNING
"LTTng: Cannot grab a reference on the inode.\n");
178 int lttng_uprobes_add_callsite(struct lttng_uprobe
*uprobe
,
179 struct lttng_kernel_abi_event_callsite __user
*callsite
,
180 int (*handler
)(struct uprobe_consumer
*self
, struct pt_regs
*regs
),
184 struct lttng_uprobe_handler
*uprobe_handler
;
191 uprobe_handler
= kzalloc(sizeof(struct lttng_uprobe_handler
), GFP_KERNEL
);
192 if (!uprobe_handler
) {
193 printk(KERN_WARNING
"LTTng: Error allocating uprobe_handler");
198 /* Ensure the memory we just allocated don't notify page faults. */
199 wrapper_vmalloc_sync_mappings();
201 uprobe_handler
->event
= priv_data
;
202 uprobe_handler
->up_consumer
.handler
= handler
;
204 ret
= copy_from_user(&uprobe_handler
->offset
, &callsite
->u
.uprobe
.offset
, sizeof(uint64_t));
209 ret
= uprobe_register(uprobe
->inode
,
210 uprobe_handler
->offset
, &uprobe_handler
->up_consumer
);
212 printk(KERN_WARNING
"LTTng: Error registering probe on inode %lu "
213 "and offset 0x%llx\n", uprobe
->inode
->i_ino
,
214 uprobe_handler
->offset
);
219 list_add(&uprobe_handler
->node
, &uprobe
->head
);
224 kfree(uprobe_handler
);
229 int lttng_uprobes_event_add_callsite(struct lttng_kernel_event_common
*event
,
230 struct lttng_kernel_abi_event_callsite __user
*callsite
)
232 return lttng_uprobes_add_callsite(&event
->priv
->u
.uprobe
, callsite
,
233 lttng_uprobes_event_handler_pre
, event
);
235 EXPORT_SYMBOL_GPL(lttng_uprobes_event_add_callsite
);
238 int lttng_uprobes_register(struct lttng_uprobe
*uprobe
, int fd
)
243 inode
= get_inode_from_fd(fd
);
245 printk(KERN_WARNING
"LTTng: Cannot get inode from fd\n");
249 uprobe
->inode
= inode
;
250 INIT_LIST_HEAD(&uprobe
->head
);
256 int lttng_uprobes_register_event(const char *name
, int fd
, struct lttng_kernel_event_common
*event
)
260 ret
= lttng_create_uprobe_event(name
, event
);
264 ret
= lttng_uprobes_register(&event
->priv
->u
.uprobe
, fd
);
271 kfree(event
->priv
->desc
->event_name
);
272 kfree(event
->priv
->desc
);
276 EXPORT_SYMBOL_GPL(lttng_uprobes_register_event
);
279 void lttng_uprobes_unregister(struct inode
*inode
, struct list_head
*head
)
281 struct lttng_uprobe_handler
*iter
, *tmp
;
284 * Iterate over the list of handler, remove each handler from the list
285 * and free the struct.
287 list_for_each_entry_safe(iter
, tmp
, head
, node
) {
288 uprobe_unregister(inode
, iter
->offset
, &iter
->up_consumer
);
289 list_del(&iter
->node
);
294 void lttng_uprobes_unregister_event(struct lttng_kernel_event_common
*event
)
296 lttng_uprobes_unregister(event
->priv
->u
.uprobe
.inode
, &event
->priv
->u
.uprobe
.head
);
298 EXPORT_SYMBOL_GPL(lttng_uprobes_unregister_event
);
300 void lttng_uprobes_destroy_event_private(struct lttng_kernel_event_common
*event
)
302 iput(event
->priv
->u
.uprobe
.inode
);
303 kfree(event
->priv
->desc
->event_name
);
304 kfree(event
->priv
->desc
);
306 EXPORT_SYMBOL_GPL(lttng_uprobes_destroy_event_private
);
308 MODULE_LICENSE("GPL and additional rights");
309 MODULE_AUTHOR("Yannick Brosseau");
310 MODULE_DESCRIPTION("Linux Trace Toolkit Uprobes Support");