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_event_container
*container
= event
->container
;
42 if (unlikely(!LTTNG_READ_ONCE(container
->session
->active
)))
44 if (unlikely(!LTTNG_READ_ONCE(container
->enabled
)))
46 if (unlikely(!LTTNG_READ_ONCE(event
->enabled
)))
49 switch (container
->type
) {
50 case LTTNG_EVENT_CONTAINER_CHANNEL
:
52 struct lttng_channel
*chan
= lttng_event_container_get_channel(container
);
53 struct lib_ring_buffer_ctx ctx
;
55 lib_ring_buffer_ctx_init(&ctx
, chan
->chan
, <tng_probe_ctx
,
56 sizeof(payload
), lttng_alignof(payload
), -1);
58 ret
= chan
->ops
->event_reserve(&ctx
, event
->id
);
63 payload
.ip
= (unsigned long)instruction_pointer(regs
);
65 lib_ring_buffer_align_ctx(&ctx
, lttng_alignof(payload
));
66 chan
->ops
->event_write(&ctx
, &payload
, sizeof(payload
));
67 chan
->ops
->event_commit(&ctx
);
70 case LTTNG_EVENT_CONTAINER_COUNTER
:
72 struct lttng_counter
*counter
= lttng_event_container_get_counter(container
);
73 size_t index
= event
->id
;
75 (void) counter
->ops
->counter_add(counter
->counter
, &index
, 1);
83 int lttng_uprobes_event_notifier_handler_pre(struct uprobe_consumer
*uc
, struct pt_regs
*regs
)
85 struct lttng_uprobe_handler
*uprobe_handler
=
86 container_of(uc
, struct lttng_uprobe_handler
, up_consumer
);
87 struct lttng_event_notifier
*event_notifier
= uprobe_handler
->u
.event_notifier
;
88 struct lttng_kernel_notifier_ctx notif_ctx
;
90 if (unlikely(!READ_ONCE(event_notifier
->enabled
)))
93 notif_ctx
.eval_capture
= LTTNG_READ_ONCE(event_notifier
->eval_capture
);
94 event_notifier
->send_notification(event_notifier
, NULL
, NULL
, ¬if_ctx
);
99 * Create event description.
102 int lttng_create_uprobe_event(const char *name
, struct lttng_event
*event
)
104 struct lttng_event_desc
*desc
;
105 struct lttng_event_field
*fields
;
108 desc
= kzalloc(sizeof(*event
->desc
), GFP_KERNEL
);
111 desc
->name
= kstrdup(name
, GFP_KERNEL
);
118 desc
->fields
= fields
=
119 kzalloc(1 * sizeof(struct lttng_event_field
), GFP_KERNEL
);
125 fields
[0].name
= "ip";
126 fields
[0].type
.atype
= atype_integer
;
127 fields
[0].type
.u
.integer
.size
= sizeof(unsigned long) * CHAR_BIT
;
128 fields
[0].type
.u
.integer
.alignment
= lttng_alignof(unsigned long) * CHAR_BIT
;
129 fields
[0].type
.u
.integer
.signedness
= lttng_is_signed_type(unsigned long);
130 fields
[0].type
.u
.integer
.reverse_byte_order
= 0;
131 fields
[0].type
.u
.integer
.base
= 16;
132 fields
[0].type
.u
.integer
.encoding
= lttng_encode_none
;
134 desc
->owner
= THIS_MODULE
;
147 * Create event_notifier description.
150 int lttng_create_uprobe_event_notifier(const char *name
, struct lttng_event_notifier
*event_notifier
)
152 struct lttng_event_desc
*desc
;
155 desc
= kzalloc(sizeof(*event_notifier
->desc
), GFP_KERNEL
);
158 desc
->name
= kstrdup(name
, GFP_KERNEL
);
166 desc
->owner
= THIS_MODULE
;
167 event_notifier
->desc
= desc
;
177 * Returns the inode struct from the current task and an fd. The inode is
178 * grabbed by this function and must be put once we are done with it using
181 static struct inode
*get_inode_from_fd(int fd
)
188 * Returns the file backing the given fd. Needs to be done inside an RCU
191 file
= lttng_lookup_fd_rcu(fd
);
193 printk(KERN_WARNING
"LTTng: Cannot access file backing the fd(%d)\n", fd
);
198 /* Grab a reference on the inode. */
199 inode
= igrab(file
->f_path
.dentry
->d_inode
);
201 printk(KERN_WARNING
"LTTng: Cannot grab a reference on the inode.\n");
209 int lttng_uprobes_add_callsite(struct lttng_uprobe
*uprobe
,
210 struct lttng_kernel_event_callsite __user
*callsite
,
211 int (*handler
)(struct uprobe_consumer
*self
, struct pt_regs
*regs
),
215 struct lttng_uprobe_handler
*uprobe_handler
;
222 uprobe_handler
= kzalloc(sizeof(struct lttng_uprobe_handler
), GFP_KERNEL
);
223 if (!uprobe_handler
) {
224 printk(KERN_WARNING
"LTTng: Error allocating uprobe_handler");
229 /* Ensure the memory we just allocated don't event_notifier page faults. */
230 wrapper_vmalloc_sync_mappings();
232 uprobe_handler
->u
.event
= priv_data
;
233 uprobe_handler
->up_consumer
.handler
= handler
;
235 ret
= copy_from_user(&uprobe_handler
->offset
, &callsite
->u
.uprobe
.offset
, sizeof(uint64_t));
240 ret
= wrapper_uprobe_register(uprobe
->inode
,
241 uprobe_handler
->offset
, &uprobe_handler
->up_consumer
);
243 printk(KERN_WARNING
"LTTng: Error registering probe on inode %lu "
244 "and offset 0x%llx\n", uprobe
->inode
->i_ino
,
245 uprobe_handler
->offset
);
250 list_add(&uprobe_handler
->node
, &uprobe
->head
);
255 kfree(uprobe_handler
);
260 int lttng_uprobes_event_add_callsite(struct lttng_event
*event
,
261 struct lttng_kernel_event_callsite __user
*callsite
)
263 return lttng_uprobes_add_callsite(&event
->u
.uprobe
, callsite
,
264 lttng_uprobes_event_handler_pre
, event
);
266 EXPORT_SYMBOL_GPL(lttng_uprobes_event_add_callsite
);
268 int lttng_uprobes_event_notifier_add_callsite(struct lttng_event_notifier
*event_notifier
,
269 struct lttng_kernel_event_callsite __user
*callsite
)
271 return lttng_uprobes_add_callsite(&event_notifier
->u
.uprobe
, callsite
,
272 lttng_uprobes_event_notifier_handler_pre
, event_notifier
);
274 EXPORT_SYMBOL_GPL(lttng_uprobes_event_notifier_add_callsite
);
277 int lttng_uprobes_register(struct lttng_uprobe
*uprobe
, int fd
)
282 inode
= get_inode_from_fd(fd
);
284 printk(KERN_WARNING
"LTTng: Cannot get inode from fd\n");
288 uprobe
->inode
= inode
;
289 INIT_LIST_HEAD(&uprobe
->head
);
295 int lttng_uprobes_register_event(const char *name
, int fd
, struct lttng_event
*event
)
299 ret
= lttng_create_uprobe_event(name
, event
);
303 ret
= lttng_uprobes_register(&event
->u
.uprobe
, fd
);
310 kfree(event
->desc
->name
);
315 EXPORT_SYMBOL_GPL(lttng_uprobes_register_event
);
317 int lttng_uprobes_register_event_notifier(const char *name
, int fd
,
318 struct lttng_event_notifier
*event_notifier
)
322 ret
= lttng_create_uprobe_event_notifier(name
, event_notifier
);
326 ret
= lttng_uprobes_register(&event_notifier
->u
.uprobe
, fd
);
333 kfree(event_notifier
->desc
->name
);
334 kfree(event_notifier
->desc
);
338 EXPORT_SYMBOL_GPL(lttng_uprobes_register_event_notifier
);
341 void lttng_uprobes_unregister(struct inode
*inode
, struct list_head
*head
)
343 struct lttng_uprobe_handler
*iter
, *tmp
;
346 * Iterate over the list of handler, remove each handler from the list
347 * and free the struct.
349 list_for_each_entry_safe(iter
, tmp
, head
, node
) {
350 wrapper_uprobe_unregister(inode
, iter
->offset
, &iter
->up_consumer
);
351 list_del(&iter
->node
);
357 void lttng_uprobes_unregister_event(struct lttng_event
*event
)
359 lttng_uprobes_unregister(event
->u
.uprobe
.inode
, &event
->u
.uprobe
.head
);
361 EXPORT_SYMBOL_GPL(lttng_uprobes_unregister_event
);
363 void lttng_uprobes_unregister_event_notifier(struct lttng_event_notifier
*event_notifier
)
365 lttng_uprobes_unregister(event_notifier
->u
.uprobe
.inode
, &event_notifier
->u
.uprobe
.head
);
367 EXPORT_SYMBOL_GPL(lttng_uprobes_unregister_event_notifier
);
369 void lttng_uprobes_destroy_event_private(struct lttng_event
*event
)
371 iput(event
->u
.uprobe
.inode
);
372 kfree(event
->desc
->name
);
375 EXPORT_SYMBOL_GPL(lttng_uprobes_destroy_event_private
);
377 void lttng_uprobes_destroy_event_notifier_private(struct lttng_event_notifier
*event_notifier
)
379 iput(event_notifier
->u
.uprobe
.inode
);
380 kfree(event_notifier
->desc
->name
);
381 kfree(event_notifier
->desc
);
383 EXPORT_SYMBOL_GPL(lttng_uprobes_destroy_event_notifier_private
);
385 MODULE_LICENSE("GPL and additional rights");
386 MODULE_AUTHOR("Yannick Brosseau");
387 MODULE_DESCRIPTION("Linux Trace Toolkit Uprobes Support");