Implement capturing payload on event notifier
[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 <linux/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
73 if (unlikely(!READ_ONCE(event_notifier->enabled)))
74 return 0;
75
76 event_notifier->send_notification(event_notifier, NULL, NULL);
77 return 0;
78 }
79
80 /*
81 * Create event description.
82 */
83 static
84 int lttng_create_uprobe_event(const char *name, struct lttng_event *event)
85 {
86 struct lttng_event_desc *desc;
87 struct lttng_event_field *fields;
88 int ret;
89
90 desc = kzalloc(sizeof(*event->desc), GFP_KERNEL);
91 if (!desc)
92 return -ENOMEM;
93 desc->name = kstrdup(name, GFP_KERNEL);
94 if (!desc->name) {
95 ret = -ENOMEM;
96 goto error_str;
97 }
98
99 desc->nr_fields = 1;
100 desc->fields = fields =
101 kzalloc(1 * sizeof(struct lttng_event_field), GFP_KERNEL);
102
103 if (!desc->fields) {
104 ret = -ENOMEM;
105 goto error_fields;
106 }
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;
115
116 desc->owner = THIS_MODULE;
117 event->desc = desc;
118
119 return 0;
120
121 error_fields:
122 kfree(desc->name);
123 error_str:
124 kfree(desc);
125 return ret;
126 }
127
128 /*
129 * Create event_notifier description.
130 */
131 static
132 int lttng_create_uprobe_event_notifier(const char *name, struct lttng_event_notifier *event_notifier)
133 {
134 struct lttng_event_desc *desc;
135 int ret;
136
137 desc = kzalloc(sizeof(*event_notifier->desc), GFP_KERNEL);
138 if (!desc)
139 return -ENOMEM;
140 desc->name = kstrdup(name, GFP_KERNEL);
141 if (!desc->name) {
142 ret = -ENOMEM;
143 goto error_str;
144 }
145
146 desc->nr_fields = 0;
147
148 desc->owner = THIS_MODULE;
149 event_notifier->desc = desc;
150
151 return 0;
152
153 error_str:
154 kfree(desc);
155 return ret;
156 }
157
158 /*
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
161 * iput().
162 */
163 static struct inode *get_inode_from_fd(int fd)
164 {
165 struct file *file;
166 struct inode *inode;
167
168 rcu_read_lock();
169 /*
170 * Returns the file backing the given fd. Needs to be done inside an RCU
171 * critical section.
172 */
173 file = fcheck(fd);
174 if (file == NULL) {
175 printk(KERN_WARNING "LTTng: Cannot access file backing the fd(%d)\n", fd);
176 inode = NULL;
177 goto error;
178 }
179
180 /* Grab a reference on the inode. */
181 inode = igrab(file->f_path.dentry->d_inode);
182 if (inode == NULL)
183 printk(KERN_WARNING "LTTng: Cannot grab a reference on the inode.\n");
184 error:
185 rcu_read_unlock();
186 return inode;
187 }
188
189
190 static
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),
194 void *priv_data)
195 {
196 int ret = 0;
197 struct lttng_uprobe_handler *uprobe_handler;
198
199 if (!priv_data) {
200 ret = -EINVAL;
201 goto end;
202 }
203
204 uprobe_handler = kzalloc(sizeof(struct lttng_uprobe_handler), GFP_KERNEL);
205 if (!uprobe_handler) {
206 printk(KERN_WARNING "LTTng: Error allocating uprobe_handler");
207 ret = -ENOMEM;
208 goto end;
209 }
210
211 /* Ensure the memory we just allocated don't event_notifier page faults. */
212 wrapper_vmalloc_sync_mappings();
213
214 uprobe_handler->u.event = priv_data;
215 uprobe_handler->up_consumer.handler = handler;
216
217 ret = copy_from_user(&uprobe_handler->offset, &callsite->u.uprobe.offset, sizeof(uint64_t));
218 if (ret) {
219 goto register_error;
220 }
221
222 ret = wrapper_uprobe_register(uprobe->inode,
223 uprobe_handler->offset, &uprobe_handler->up_consumer);
224 if (ret) {
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);
228 ret = -1;
229 goto register_error;
230 }
231
232 list_add(&uprobe_handler->node, &uprobe->head);
233
234 return ret;
235
236 register_error:
237 kfree(uprobe_handler);
238 end:
239 return ret;
240 }
241
242 int lttng_uprobes_event_add_callsite(struct lttng_event *event,
243 struct lttng_kernel_event_callsite __user *callsite)
244 {
245 return lttng_uprobes_add_callsite(&event->u.uprobe, callsite,
246 lttng_uprobes_event_handler_pre, event);
247 }
248 EXPORT_SYMBOL_GPL(lttng_uprobes_event_add_callsite);
249
250 int lttng_uprobes_event_notifier_add_callsite(struct lttng_event_notifier *event_notifier,
251 struct lttng_kernel_event_callsite __user *callsite)
252 {
253 return lttng_uprobes_add_callsite(&event_notifier->u.uprobe, callsite,
254 lttng_uprobes_event_notifier_handler_pre, event_notifier);
255 }
256 EXPORT_SYMBOL_GPL(lttng_uprobes_event_notifier_add_callsite);
257
258 static
259 int lttng_uprobes_register(struct lttng_uprobe *uprobe, int fd)
260 {
261 int ret = 0;
262 struct inode *inode;
263
264 inode = get_inode_from_fd(fd);
265 if (!inode) {
266 printk(KERN_WARNING "LTTng: Cannot get inode from fd\n");
267 ret = -EBADF;
268 goto inode_error;
269 }
270 uprobe->inode = inode;
271 INIT_LIST_HEAD(&uprobe->head);
272
273 inode_error:
274 return ret;
275 }
276
277 int lttng_uprobes_register_event(const char *name, int fd, struct lttng_event *event)
278 {
279 int ret = 0;
280
281 ret = lttng_create_uprobe_event(name, event);
282 if (ret)
283 goto error;
284
285 ret = lttng_uprobes_register(&event->u.uprobe, fd);
286 if (ret)
287 goto register_error;
288
289 return 0;
290
291 register_error:
292 kfree(event->desc->name);
293 kfree(event->desc);
294 error:
295 return ret;
296 }
297 EXPORT_SYMBOL_GPL(lttng_uprobes_register_event);
298
299 int lttng_uprobes_register_event_notifier(const char *name, int fd,
300 struct lttng_event_notifier *event_notifier)
301 {
302 int ret = 0;
303
304 ret = lttng_create_uprobe_event_notifier(name, event_notifier);
305 if (ret)
306 goto error;
307
308 ret = lttng_uprobes_register(&event_notifier->u.uprobe, fd);
309 if (ret)
310 goto register_error;
311
312 return 0;
313
314 register_error:
315 kfree(event_notifier->desc->name);
316 kfree(event_notifier->desc);
317 error:
318 return ret;
319 }
320 EXPORT_SYMBOL_GPL(lttng_uprobes_register_event_notifier);
321
322 static
323 void lttng_uprobes_unregister(struct inode *inode, struct list_head *head)
324 {
325 struct lttng_uprobe_handler *iter, *tmp;
326
327 /*
328 * Iterate over the list of handler, remove each handler from the list
329 * and free the struct.
330 */
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);
334 kfree(iter);
335 }
336
337 }
338
339 void lttng_uprobes_unregister_event(struct lttng_event *event)
340 {
341 lttng_uprobes_unregister(event->u.uprobe.inode, &event->u.uprobe.head);
342 }
343 EXPORT_SYMBOL_GPL(lttng_uprobes_unregister_event);
344
345 void lttng_uprobes_unregister_event_notifier(struct lttng_event_notifier *event_notifier)
346 {
347 lttng_uprobes_unregister(event_notifier->u.uprobe.inode, &event_notifier->u.uprobe.head);
348 }
349 EXPORT_SYMBOL_GPL(lttng_uprobes_unregister_event_notifier);
350
351 void lttng_uprobes_destroy_event_private(struct lttng_event *event)
352 {
353 iput(event->u.uprobe.inode);
354 kfree(event->desc->name);
355 kfree(event->desc);
356 }
357 EXPORT_SYMBOL_GPL(lttng_uprobes_destroy_event_private);
358
359 void lttng_uprobes_destroy_event_notifier_private(struct lttng_event_notifier *event_notifier)
360 {
361 iput(event_notifier->u.uprobe.inode);
362 kfree(event_notifier->desc->name);
363 kfree(event_notifier->desc);
364 }
365 EXPORT_SYMBOL_GPL(lttng_uprobes_destroy_event_notifier_private);
366
367 MODULE_LICENSE("GPL and additional rights");
368 MODULE_AUTHOR("Yannick Brosseau");
369 MODULE_DESCRIPTION("Linux Trace Toolkit Uprobes Support");
This page took 0.035842 seconds and 4 git commands to generate.