Cleanup: Rename abstract types to lttng_kernel_type
[lttng-modules.git] / src / probes / lttng-uprobes.c
1 /* SPDX-License-Identifier: (GPL-2.0-only OR LGPL-2.1-only)
2 *
3 * probes/lttng-uprobes.c
4 *
5 * LTTng uprobes integration module.
6 *
7 * Copyright (C) 2013 Yannick Brosseau <yannick.brosseau@gmail.com>
8 * Copyright (C) 2009-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
9 *
10 */
11
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>
24
25 static
26 int lttng_uprobes_event_handler_pre(struct uprobe_consumer *uc, struct pt_regs *regs)
27 {
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 = {
32 .event = event,
33 .interruptible = !lttng_regs_irqs_disabled(regs),
34 };
35 struct lttng_channel *chan = event->chan;
36 struct lib_ring_buffer_ctx ctx;
37 int ret;
38
39 struct {
40 unsigned long ip;
41 } payload;
42
43 if (unlikely(!LTTNG_READ_ONCE(chan->session->active)))
44 return 0;
45 if (unlikely(!LTTNG_READ_ONCE(chan->enabled)))
46 return 0;
47 if (unlikely(!LTTNG_READ_ONCE(event->enabled)))
48 return 0;
49
50 lib_ring_buffer_ctx_init(&ctx, chan->chan, &lttng_probe_ctx,
51 sizeof(payload), lttng_alignof(payload), -1);
52
53 ret = chan->ops->event_reserve(&ctx, event->id);
54 if (ret < 0)
55 return 0;
56
57 /* Event payload. */
58 payload.ip = (unsigned long)instruction_pointer(regs);
59
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);
63 return 0;
64 }
65
66 static
67 int lttng_uprobes_event_notifier_handler_pre(struct uprobe_consumer *uc, struct pt_regs *regs)
68 {
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;
72 struct lttng_kernel_notifier_ctx notif_ctx;
73
74 if (unlikely(!READ_ONCE(event_notifier->enabled)))
75 return 0;
76
77 notif_ctx.eval_capture = LTTNG_READ_ONCE(event_notifier->eval_capture);
78 event_notifier->send_notification(event_notifier, NULL, NULL, &notif_ctx);
79 return 0;
80 }
81
82 /*
83 * Create event description.
84 */
85 static
86 int lttng_create_uprobe_event(const char *name, struct lttng_event *event)
87 {
88 struct lttng_event_desc *desc;
89 struct lttng_event_field *fields;
90 int ret;
91
92 desc = kzalloc(sizeof(*event->desc), GFP_KERNEL);
93 if (!desc)
94 return -ENOMEM;
95 desc->name = kstrdup(name, GFP_KERNEL);
96 if (!desc->name) {
97 ret = -ENOMEM;
98 goto error_str;
99 }
100
101 desc->nr_fields = 1;
102 desc->fields = fields =
103 kzalloc(1 * sizeof(struct lttng_event_field), GFP_KERNEL);
104
105 if (!desc->fields) {
106 ret = -ENOMEM;
107 goto error_fields;
108 }
109 fields[0].name = "ip";
110 fields[0].type.type = lttng_kernel_type_integer;
111 fields[0].type.u.integer.size = sizeof(unsigned long) * CHAR_BIT;
112 fields[0].type.u.integer.alignment = lttng_alignof(unsigned long) * CHAR_BIT;
113 fields[0].type.u.integer.signedness = lttng_is_signed_type(unsigned long);
114 fields[0].type.u.integer.reverse_byte_order = 0;
115 fields[0].type.u.integer.base = 16;
116 fields[0].type.u.integer.encoding = lttng_encode_none;
117
118 desc->owner = THIS_MODULE;
119 event->desc = desc;
120
121 return 0;
122
123 error_fields:
124 kfree(desc->name);
125 error_str:
126 kfree(desc);
127 return ret;
128 }
129
130 /*
131 * Create event_notifier description.
132 */
133 static
134 int lttng_create_uprobe_event_notifier(const char *name, struct lttng_event_notifier *event_notifier)
135 {
136 struct lttng_event_desc *desc;
137 int ret;
138
139 desc = kzalloc(sizeof(*event_notifier->desc), GFP_KERNEL);
140 if (!desc)
141 return -ENOMEM;
142 desc->name = kstrdup(name, GFP_KERNEL);
143 if (!desc->name) {
144 ret = -ENOMEM;
145 goto error_str;
146 }
147
148 desc->nr_fields = 0;
149
150 desc->owner = THIS_MODULE;
151 event_notifier->desc = desc;
152
153 return 0;
154
155 error_str:
156 kfree(desc);
157 return ret;
158 }
159
160 /*
161 * Returns the inode struct from the current task and an fd. The inode is
162 * grabbed by this function and must be put once we are done with it using
163 * iput().
164 */
165 static struct inode *get_inode_from_fd(int fd)
166 {
167 struct file *file;
168 struct inode *inode;
169
170 rcu_read_lock();
171 /*
172 * Returns the file backing the given fd. Needs to be done inside an RCU
173 * critical section.
174 */
175 file = lttng_lookup_fd_rcu(fd);
176 if (file == NULL) {
177 printk(KERN_WARNING "LTTng: Cannot access file backing the fd(%d)\n", fd);
178 inode = NULL;
179 goto error;
180 }
181
182 /* Grab a reference on the inode. */
183 inode = igrab(file->f_path.dentry->d_inode);
184 if (inode == NULL)
185 printk(KERN_WARNING "LTTng: Cannot grab a reference on the inode.\n");
186 error:
187 rcu_read_unlock();
188 return inode;
189 }
190
191
192 static
193 int lttng_uprobes_add_callsite(struct lttng_uprobe *uprobe,
194 struct lttng_kernel_event_callsite __user *callsite,
195 int (*handler)(struct uprobe_consumer *self, struct pt_regs *regs),
196 void *priv_data)
197 {
198 int ret = 0;
199 struct lttng_uprobe_handler *uprobe_handler;
200
201 if (!priv_data) {
202 ret = -EINVAL;
203 goto end;
204 }
205
206 uprobe_handler = kzalloc(sizeof(struct lttng_uprobe_handler), GFP_KERNEL);
207 if (!uprobe_handler) {
208 printk(KERN_WARNING "LTTng: Error allocating uprobe_handler");
209 ret = -ENOMEM;
210 goto end;
211 }
212
213 /* Ensure the memory we just allocated don't notify page faults. */
214 wrapper_vmalloc_sync_mappings();
215
216 uprobe_handler->u.event = priv_data;
217 uprobe_handler->up_consumer.handler = handler;
218
219 ret = copy_from_user(&uprobe_handler->offset, &callsite->u.uprobe.offset, sizeof(uint64_t));
220 if (ret) {
221 goto register_error;
222 }
223
224 ret = wrapper_uprobe_register(uprobe->inode,
225 uprobe_handler->offset, &uprobe_handler->up_consumer);
226 if (ret) {
227 printk(KERN_WARNING "LTTng: Error registering probe on inode %lu "
228 "and offset 0x%llx\n", uprobe->inode->i_ino,
229 uprobe_handler->offset);
230 ret = -1;
231 goto register_error;
232 }
233
234 list_add(&uprobe_handler->node, &uprobe->head);
235
236 return ret;
237
238 register_error:
239 kfree(uprobe_handler);
240 end:
241 return ret;
242 }
243
244 int lttng_uprobes_event_add_callsite(struct lttng_event *event,
245 struct lttng_kernel_event_callsite __user *callsite)
246 {
247 return lttng_uprobes_add_callsite(&event->u.uprobe, callsite,
248 lttng_uprobes_event_handler_pre, event);
249 }
250 EXPORT_SYMBOL_GPL(lttng_uprobes_event_add_callsite);
251
252 int lttng_uprobes_event_notifier_add_callsite(struct lttng_event_notifier *event_notifier,
253 struct lttng_kernel_event_callsite __user *callsite)
254 {
255 return lttng_uprobes_add_callsite(&event_notifier->u.uprobe, callsite,
256 lttng_uprobes_event_notifier_handler_pre, event_notifier);
257 }
258 EXPORT_SYMBOL_GPL(lttng_uprobes_event_notifier_add_callsite);
259
260 static
261 int lttng_uprobes_register(struct lttng_uprobe *uprobe, int fd)
262 {
263 int ret = 0;
264 struct inode *inode;
265
266 inode = get_inode_from_fd(fd);
267 if (!inode) {
268 printk(KERN_WARNING "LTTng: Cannot get inode from fd\n");
269 ret = -EBADF;
270 goto inode_error;
271 }
272 uprobe->inode = inode;
273 INIT_LIST_HEAD(&uprobe->head);
274
275 inode_error:
276 return ret;
277 }
278
279 int lttng_uprobes_register_event(const char *name, int fd, struct lttng_event *event)
280 {
281 int ret = 0;
282
283 ret = lttng_create_uprobe_event(name, event);
284 if (ret)
285 goto error;
286
287 ret = lttng_uprobes_register(&event->u.uprobe, fd);
288 if (ret)
289 goto register_error;
290
291 return 0;
292
293 register_error:
294 kfree(event->desc->name);
295 kfree(event->desc);
296 error:
297 return ret;
298 }
299 EXPORT_SYMBOL_GPL(lttng_uprobes_register_event);
300
301 int lttng_uprobes_register_event_notifier(const char *name, int fd,
302 struct lttng_event_notifier *event_notifier)
303 {
304 int ret = 0;
305
306 ret = lttng_create_uprobe_event_notifier(name, event_notifier);
307 if (ret)
308 goto error;
309
310 ret = lttng_uprobes_register(&event_notifier->u.uprobe, fd);
311 if (ret)
312 goto register_error;
313
314 return 0;
315
316 register_error:
317 kfree(event_notifier->desc->name);
318 kfree(event_notifier->desc);
319 error:
320 return ret;
321 }
322 EXPORT_SYMBOL_GPL(lttng_uprobes_register_event_notifier);
323
324 static
325 void lttng_uprobes_unregister(struct inode *inode, struct list_head *head)
326 {
327 struct lttng_uprobe_handler *iter, *tmp;
328
329 /*
330 * Iterate over the list of handler, remove each handler from the list
331 * and free the struct.
332 */
333 list_for_each_entry_safe(iter, tmp, head, node) {
334 wrapper_uprobe_unregister(inode, iter->offset, &iter->up_consumer);
335 list_del(&iter->node);
336 kfree(iter);
337 }
338
339 }
340
341 void lttng_uprobes_unregister_event(struct lttng_event *event)
342 {
343 lttng_uprobes_unregister(event->u.uprobe.inode, &event->u.uprobe.head);
344 }
345 EXPORT_SYMBOL_GPL(lttng_uprobes_unregister_event);
346
347 void lttng_uprobes_unregister_event_notifier(struct lttng_event_notifier *event_notifier)
348 {
349 lttng_uprobes_unregister(event_notifier->u.uprobe.inode, &event_notifier->u.uprobe.head);
350 }
351 EXPORT_SYMBOL_GPL(lttng_uprobes_unregister_event_notifier);
352
353 void lttng_uprobes_destroy_event_private(struct lttng_event *event)
354 {
355 iput(event->u.uprobe.inode);
356 kfree(event->desc->name);
357 kfree(event->desc);
358 }
359 EXPORT_SYMBOL_GPL(lttng_uprobes_destroy_event_private);
360
361 void lttng_uprobes_destroy_event_notifier_private(struct lttng_event_notifier *event_notifier)
362 {
363 iput(event_notifier->u.uprobe.inode);
364 kfree(event_notifier->desc->name);
365 kfree(event_notifier->desc);
366 }
367 EXPORT_SYMBOL_GPL(lttng_uprobes_destroy_event_notifier_private);
368
369 MODULE_LICENSE("GPL and additional rights");
370 MODULE_AUTHOR("Yannick Brosseau");
371 MODULE_DESCRIPTION("Linux Trace Toolkit Uprobes Support");
This page took 0.045167 seconds and 4 git commands to generate.