1 /* SPDX-License-Identifier: (GPL-2.0 OR LGPL-2.1)
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 <wrapper/ringbuffer/frontend_types.h>
22 #include <wrapper/uprobes.h>
23 #include <wrapper/vmalloc.h>
26 int lttng_uprobes_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
->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 * Create event description.
70 int lttng_create_uprobe_event(const char *name
, struct lttng_event
*event
)
72 struct lttng_event_desc
*desc
;
73 struct lttng_event_field
*fields
;
76 desc
= kzalloc(sizeof(*event
->desc
), GFP_KERNEL
);
79 desc
->name
= kstrdup(name
, GFP_KERNEL
);
86 desc
->fields
= fields
=
87 kzalloc(1 * sizeof(struct lttng_event_field
), GFP_KERNEL
);
93 fields
[0].name
= "ip";
94 fields
[0].type
.atype
= atype_integer
;
95 fields
[0].type
.u
.basic
.integer
.size
= sizeof(unsigned long) * CHAR_BIT
;
96 fields
[0].type
.u
.basic
.integer
.alignment
= lttng_alignof(unsigned long) * CHAR_BIT
;
97 fields
[0].type
.u
.basic
.integer
.signedness
= lttng_is_signed_type(unsigned long);
98 fields
[0].type
.u
.basic
.integer
.reverse_byte_order
= 0;
99 fields
[0].type
.u
.basic
.integer
.base
= 16;
100 fields
[0].type
.u
.basic
.integer
.encoding
= lttng_encode_none
;
102 desc
->owner
= THIS_MODULE
;
115 * Returns the inode struct from the current task and an fd. The inode is
116 * grabbed by this function and must be put once we are done with it using
119 static struct inode
*get_inode_from_fd(int fd
)
126 * Returns the file backing the given fd. Needs to be done inside an RCU
129 file
= lttng_lookup_fd_rcu(fd
);
131 printk(KERN_WARNING
"Cannot access file backing the fd(%d)\n", fd
);
136 /* Grab a reference on the inode. */
137 inode
= igrab(file
->f_path
.dentry
->d_inode
);
139 printk(KERN_WARNING
"Cannot grab a reference on the inode.\n");
145 int lttng_uprobes_add_callsite(struct lttng_event
*event
,
146 struct lttng_kernel_event_callsite __user
*callsite
)
149 struct lttng_uprobe_handler
*uprobe_handler
;
156 uprobe_handler
= kzalloc(sizeof(struct lttng_uprobe_handler
), GFP_KERNEL
);
157 if (!uprobe_handler
) {
158 printk(KERN_WARNING
"Error allocating uprobe_uprobe_handlers");
163 /* Ensure the memory we just allocated don't trigger page faults. */
164 wrapper_vmalloc_sync_mappings();
166 uprobe_handler
->event
= event
;
167 uprobe_handler
->up_consumer
.handler
= lttng_uprobes_handler_pre
;
169 ret
= copy_from_user(&uprobe_handler
->offset
, &callsite
->u
.uprobe
.offset
, sizeof(uint64_t));
174 ret
= wrapper_uprobe_register(event
->u
.uprobe
.inode
,
175 uprobe_handler
->offset
, &uprobe_handler
->up_consumer
);
177 printk(KERN_WARNING
"Error registering probe on inode %lu "
178 "and offset 0x%llx\n", event
->u
.uprobe
.inode
->i_ino
,
179 uprobe_handler
->offset
);
184 list_add(&uprobe_handler
->node
, &event
->u
.uprobe
.head
);
189 kfree(uprobe_handler
);
193 EXPORT_SYMBOL_GPL(lttng_uprobes_add_callsite
);
195 int lttng_uprobes_register(const char *name
, int fd
, struct lttng_event
*event
)
200 ret
= lttng_create_uprobe_event(name
, event
);
204 inode
= get_inode_from_fd(fd
);
206 printk(KERN_WARNING
"Cannot get inode from fd\n");
210 event
->u
.uprobe
.inode
= inode
;
211 INIT_LIST_HEAD(&event
->u
.uprobe
.head
);
216 kfree(event
->desc
->name
);
221 EXPORT_SYMBOL_GPL(lttng_uprobes_register
);
223 void lttng_uprobes_unregister(struct lttng_event
*event
)
225 struct lttng_uprobe_handler
*iter
, *tmp
;
228 * Iterate over the list of handler, remove each handler from the list
229 * and free the struct.
231 list_for_each_entry_safe(iter
, tmp
, &event
->u
.uprobe
.head
, node
) {
232 wrapper_uprobe_unregister(event
->u
.uprobe
.inode
, iter
->offset
,
234 list_del(&iter
->node
);
238 EXPORT_SYMBOL_GPL(lttng_uprobes_unregister
);
240 void lttng_uprobes_destroy_private(struct lttng_event
*event
)
242 iput(event
->u
.uprobe
.inode
);
243 kfree(event
->desc
->name
);
246 EXPORT_SYMBOL_GPL(lttng_uprobes_destroy_private
);
248 MODULE_LICENSE("GPL and additional rights");
249 MODULE_AUTHOR("Yannick Brosseau");
250 MODULE_DESCRIPTION("Linux Trace Toolkit Uprobes Support");