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/list.h>
14 #include <linux/module.h>
15 #include <linux/namei.h>
16 #include <linux/slab.h>
17 #include <linux/uaccess.h>
18 #include <lttng/events.h>
19 #include <lttng/tracer.h>
20 #include <wrapper/irqflags.h>
21 #include <ringbuffer/frontend_types.h>
22 #include <wrapper/uprobes.h>
23 #include <wrapper/vmalloc.h>
26 int lttng_uprobes_event_handler_pre(struct uprobe_consumer
*uc
, struct pt_regs
*regs
)
28 struct lttng_uprobe_handler
*uprobe_handler
=
29 container_of(uc
, struct lttng_uprobe_handler
, up_consumer
);
30 struct lttng_event
*event
= uprobe_handler
->u
.event
;
31 struct lttng_probe_ctx lttng_probe_ctx
= {
33 .interruptible
= !lttng_regs_irqs_disabled(regs
),
35 struct lttng_channel
*chan
= event
->chan
;
36 struct lib_ring_buffer_ctx ctx
;
43 if (unlikely(!LTTNG_READ_ONCE(chan
->session
->active
)))
45 if (unlikely(!LTTNG_READ_ONCE(chan
->enabled
)))
47 if (unlikely(!LTTNG_READ_ONCE(event
->enabled
)))
50 lib_ring_buffer_ctx_init(&ctx
, chan
->chan
, <tng_probe_ctx
,
51 sizeof(payload
), lttng_alignof(payload
), -1);
53 ret
= chan
->ops
->event_reserve(&ctx
, event
->id
);
58 payload
.ip
= (unsigned long)instruction_pointer(regs
);
60 lib_ring_buffer_align_ctx(&ctx
, lttng_alignof(payload
));
61 chan
->ops
->event_write(&ctx
, &payload
, sizeof(payload
));
62 chan
->ops
->event_commit(&ctx
);
67 int lttng_uprobes_event_notifier_handler_pre(struct uprobe_consumer
*uc
, struct pt_regs
*regs
)
69 struct lttng_uprobe_handler
*uprobe_handler
=
70 container_of(uc
, struct lttng_uprobe_handler
, up_consumer
);
71 struct lttng_event_notifier
*event_notifier
= uprobe_handler
->u
.event_notifier
;
73 if (unlikely(!READ_ONCE(event_notifier
->enabled
)))
76 event_notifier
->send_notification(event_notifier
, NULL
, NULL
);
81 * Create event description.
84 int lttng_create_uprobe_event(const char *name
, struct lttng_event
*event
)
86 struct lttng_event_desc
*desc
;
87 struct lttng_event_field
*fields
;
90 desc
= kzalloc(sizeof(*event
->desc
), GFP_KERNEL
);
93 desc
->name
= kstrdup(name
, GFP_KERNEL
);
100 desc
->fields
= fields
=
101 kzalloc(1 * sizeof(struct lttng_event_field
), GFP_KERNEL
);
107 fields
[0].name
= "ip";
108 fields
[0].type
.atype
= atype_integer
;
109 fields
[0].type
.u
.integer
.size
= sizeof(unsigned long) * CHAR_BIT
;
110 fields
[0].type
.u
.integer
.alignment
= lttng_alignof(unsigned long) * CHAR_BIT
;
111 fields
[0].type
.u
.integer
.signedness
= lttng_is_signed_type(unsigned long);
112 fields
[0].type
.u
.integer
.reverse_byte_order
= 0;
113 fields
[0].type
.u
.integer
.base
= 16;
114 fields
[0].type
.u
.integer
.encoding
= lttng_encode_none
;
116 desc
->owner
= THIS_MODULE
;
129 * Create event_notifier description.
132 int lttng_create_uprobe_event_notifier(const char *name
, struct lttng_event_notifier
*event_notifier
)
134 struct lttng_event_desc
*desc
;
137 desc
= kzalloc(sizeof(*event_notifier
->desc
), GFP_KERNEL
);
140 desc
->name
= kstrdup(name
, GFP_KERNEL
);
148 desc
->owner
= THIS_MODULE
;
149 event_notifier
->desc
= desc
;
159 * Returns the inode struct from the current task and an fd. The inode is
160 * grabbed by this function and must be put once we are done with it using
163 static struct inode
*get_inode_from_fd(int fd
)
170 * Returns the file backing the given fd. Needs to be done inside an RCU
173 file
= lttng_lookup_fd_rcu(fd
);
175 printk(KERN_WARNING
"LTTng: Cannot access file backing the fd(%d)\n", fd
);
180 /* Grab a reference on the inode. */
181 inode
= igrab(file
->f_path
.dentry
->d_inode
);
183 printk(KERN_WARNING
"LTTng: Cannot grab a reference on the inode.\n");
191 int lttng_uprobes_add_callsite(struct lttng_uprobe
*uprobe
,
192 struct lttng_kernel_event_callsite __user
*callsite
,
193 int (*handler
)(struct uprobe_consumer
*self
, struct pt_regs
*regs
),
197 struct lttng_uprobe_handler
*uprobe_handler
;
204 uprobe_handler
= kzalloc(sizeof(struct lttng_uprobe_handler
), GFP_KERNEL
);
205 if (!uprobe_handler
) {
206 printk(KERN_WARNING
"LTTng: Error allocating uprobe_handler");
211 /* Ensure the memory we just allocated don't event_notifier page faults. */
212 wrapper_vmalloc_sync_mappings();
214 uprobe_handler
->u
.event
= priv_data
;
215 uprobe_handler
->up_consumer
.handler
= handler
;
217 ret
= copy_from_user(&uprobe_handler
->offset
, &callsite
->u
.uprobe
.offset
, sizeof(uint64_t));
222 ret
= wrapper_uprobe_register(uprobe
->inode
,
223 uprobe_handler
->offset
, &uprobe_handler
->up_consumer
);
225 printk(KERN_WARNING
"LTTng: Error registering probe on inode %lu "
226 "and offset 0x%llx\n", uprobe
->inode
->i_ino
,
227 uprobe_handler
->offset
);
232 list_add(&uprobe_handler
->node
, &uprobe
->head
);
237 kfree(uprobe_handler
);
242 int lttng_uprobes_event_add_callsite(struct lttng_event
*event
,
243 struct lttng_kernel_event_callsite __user
*callsite
)
245 return lttng_uprobes_add_callsite(&event
->u
.uprobe
, callsite
,
246 lttng_uprobes_event_handler_pre
, event
);
248 EXPORT_SYMBOL_GPL(lttng_uprobes_event_add_callsite
);
250 int lttng_uprobes_event_notifier_add_callsite(struct lttng_event_notifier
*event_notifier
,
251 struct lttng_kernel_event_callsite __user
*callsite
)
253 return lttng_uprobes_add_callsite(&event_notifier
->u
.uprobe
, callsite
,
254 lttng_uprobes_event_notifier_handler_pre
, event_notifier
);
256 EXPORT_SYMBOL_GPL(lttng_uprobes_event_notifier_add_callsite
);
259 int lttng_uprobes_register(struct lttng_uprobe
*uprobe
, int fd
)
264 inode
= get_inode_from_fd(fd
);
266 printk(KERN_WARNING
"LTTng: Cannot get inode from fd\n");
270 uprobe
->inode
= inode
;
271 INIT_LIST_HEAD(&uprobe
->head
);
277 int lttng_uprobes_register_event(const char *name
, int fd
, struct lttng_event
*event
)
281 ret
= lttng_create_uprobe_event(name
, event
);
285 ret
= lttng_uprobes_register(&event
->u
.uprobe
, fd
);
292 kfree(event
->desc
->name
);
297 EXPORT_SYMBOL_GPL(lttng_uprobes_register_event
);
299 int lttng_uprobes_register_event_notifier(const char *name
, int fd
,
300 struct lttng_event_notifier
*event_notifier
)
304 ret
= lttng_create_uprobe_event_notifier(name
, event_notifier
);
308 ret
= lttng_uprobes_register(&event_notifier
->u
.uprobe
, fd
);
315 kfree(event_notifier
->desc
->name
);
316 kfree(event_notifier
->desc
);
320 EXPORT_SYMBOL_GPL(lttng_uprobes_register_event_notifier
);
323 void lttng_uprobes_unregister(struct inode
*inode
, struct list_head
*head
)
325 struct lttng_uprobe_handler
*iter
, *tmp
;
328 * Iterate over the list of handler, remove each handler from the list
329 * and free the struct.
331 list_for_each_entry_safe(iter
, tmp
, head
, node
) {
332 wrapper_uprobe_unregister(inode
, iter
->offset
, &iter
->up_consumer
);
333 list_del(&iter
->node
);
339 void lttng_uprobes_unregister_event(struct lttng_event
*event
)
341 lttng_uprobes_unregister(event
->u
.uprobe
.inode
, &event
->u
.uprobe
.head
);
343 EXPORT_SYMBOL_GPL(lttng_uprobes_unregister_event
);
345 void lttng_uprobes_unregister_event_notifier(struct lttng_event_notifier
*event_notifier
)
347 lttng_uprobes_unregister(event_notifier
->u
.uprobe
.inode
, &event_notifier
->u
.uprobe
.head
);
349 EXPORT_SYMBOL_GPL(lttng_uprobes_unregister_event_notifier
);
351 void lttng_uprobes_destroy_event_private(struct lttng_event
*event
)
353 iput(event
->u
.uprobe
.inode
);
354 kfree(event
->desc
->name
);
357 EXPORT_SYMBOL_GPL(lttng_uprobes_destroy_event_private
);
359 void lttng_uprobes_destroy_event_notifier_private(struct lttng_event_notifier
*event_notifier
)
361 iput(event_notifier
->u
.uprobe
.inode
);
362 kfree(event_notifier
->desc
->name
);
363 kfree(event_notifier
->desc
);
365 EXPORT_SYMBOL_GPL(lttng_uprobes_destroy_event_notifier_private
);
367 MODULE_LICENSE("GPL and additional rights");
368 MODULE_AUTHOR("Yannick Brosseau");
369 MODULE_DESCRIPTION("Linux Trace Toolkit Uprobes Support");