Cleanup: event_notifier -> notify in comments
[lttng-modules.git] / src / probes / lttng-uprobes.c
CommitLineData
b7cdc182 1/* SPDX-License-Identifier: (GPL-2.0-only OR LGPL-2.1-only)
8134eb60 2 *
149b9a9d
YB
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 *
149b9a9d
YB
10 */
11
0aebcd68 12#include <wrapper/fdtable.h>
3aed4dca 13#include <linux/list.h>
149b9a9d
YB
14#include <linux/module.h>
15#include <linux/namei.h>
16#include <linux/slab.h>
3aed4dca 17#include <linux/uaccess.h>
2df37e95
MD
18#include <lttng/events.h>
19#include <lttng/tracer.h>
149b9a9d 20#include <wrapper/irqflags.h>
24591303 21#include <ringbuffer/frontend_types.h>
149b9a9d
YB
22#include <wrapper/uprobes.h>
23#include <wrapper/vmalloc.h>
24
25static
83b802dc 26int lttng_uprobes_event_handler_pre(struct uprobe_consumer *uc, struct pt_regs *regs)
149b9a9d 27{
3aed4dca
FD
28 struct lttng_uprobe_handler *uprobe_handler =
29 container_of(uc, struct lttng_uprobe_handler, up_consumer);
83b802dc 30 struct lttng_event *event = uprobe_handler->u.event;
149b9a9d
YB
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;
56377c91 41 } payload;
149b9a9d 42
585e5dcc 43 if (unlikely(!LTTNG_READ_ONCE(chan->session->active)))
149b9a9d 44 return 0;
585e5dcc 45 if (unlikely(!LTTNG_READ_ONCE(chan->enabled)))
149b9a9d 46 return 0;
585e5dcc 47 if (unlikely(!LTTNG_READ_ONCE(event->enabled)))
149b9a9d
YB
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. */
7445d54f 58 payload.ip = (unsigned long)instruction_pointer(regs);
56377c91 59
149b9a9d
YB
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
9de67196
FD
66static
67int 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;
c3eddb2e 72 struct lttng_kernel_notifier_ctx notif_ctx;
9de67196
FD
73
74 if (unlikely(!READ_ONCE(event_notifier->enabled)))
75 return 0;
76
c3eddb2e
MD
77 notif_ctx.eval_capture = LTTNG_READ_ONCE(event_notifier->eval_capture);
78 event_notifier->send_notification(event_notifier, NULL, NULL, &notif_ctx);
9de67196
FD
79 return 0;
80}
81
149b9a9d
YB
82/*
83 * Create event description.
84 */
85static
86int 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.atype = atype_integer;
ceabb767
MD
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;
149b9a9d
YB
117
118 desc->owner = THIS_MODULE;
119 event->desc = desc;
120
121 return 0;
122
123error_fields:
124 kfree(desc->name);
125error_str:
126 kfree(desc);
127 return ret;
128}
129
9de67196
FD
130/*
131 * Create event_notifier description.
132 */
133static
134int 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
155error_str:
156 kfree(desc);
157 return ret;
158}
159
56377c91
FD
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 */
165static 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 */
0aebcd68 175 file = lttng_lookup_fd_rcu(fd);
56377c91 176 if (file == NULL) {
5a15f70c 177 printk(KERN_WARNING "LTTng: Cannot access file backing the fd(%d)\n", fd);
56377c91
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)
5a15f70c 185 printk(KERN_WARNING "LTTng: Cannot grab a reference on the inode.\n");
56377c91
FD
186error:
187 rcu_read_unlock();
188 return inode;
189}
190
83b802dc
FD
191
192static
193int 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)
149b9a9d 197{
3aed4dca
FD
198 int ret = 0;
199 struct lttng_uprobe_handler *uprobe_handler;
200
83b802dc 201 if (!priv_data) {
3aed4dca
FD
202 ret = -EINVAL;
203 goto end;
204 }
205
206 uprobe_handler = kzalloc(sizeof(struct lttng_uprobe_handler), GFP_KERNEL);
207 if (!uprobe_handler) {
5a15f70c 208 printk(KERN_WARNING "LTTng: Error allocating uprobe_handler");
3aed4dca
FD
209 ret = -ENOMEM;
210 goto end;
211 }
212
196bfc6a 213 /* Ensure the memory we just allocated don't notify page faults. */
263b6c88 214 wrapper_vmalloc_sync_mappings();
3aed4dca 215
83b802dc
FD
216 uprobe_handler->u.event = priv_data;
217 uprobe_handler->up_consumer.handler = handler;
3aed4dca
FD
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
83b802dc 224 ret = wrapper_uprobe_register(uprobe->inode,
3aed4dca
FD
225 uprobe_handler->offset, &uprobe_handler->up_consumer);
226 if (ret) {
5a15f70c 227 printk(KERN_WARNING "LTTng: Error registering probe on inode %lu "
83b802dc 228 "and offset 0x%llx\n", uprobe->inode->i_ino,
3aed4dca
FD
229 uprobe_handler->offset);
230 ret = -1;
231 goto register_error;
232 }
233
83b802dc 234 list_add(&uprobe_handler->node, &uprobe->head);
3aed4dca
FD
235
236 return ret;
237
238register_error:
239 kfree(uprobe_handler);
240end:
241 return ret;
242}
3aed4dca 243
83b802dc
FD
244int 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}
250EXPORT_SYMBOL_GPL(lttng_uprobes_event_add_callsite);
251
9de67196
FD
252int 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}
258EXPORT_SYMBOL_GPL(lttng_uprobes_event_notifier_add_callsite);
259
83b802dc
FD
260static
261int lttng_uprobes_register(struct lttng_uprobe *uprobe, int fd)
3aed4dca
FD
262{
263 int ret = 0;
56377c91 264 struct inode *inode;
149b9a9d 265
56377c91
FD
266 inode = get_inode_from_fd(fd);
267 if (!inode) {
5a15f70c 268 printk(KERN_WARNING "LTTng: Cannot get inode from fd\n");
56377c91
FD
269 ret = -EBADF;
270 goto inode_error;
271 }
83b802dc
FD
272 uprobe->inode = inode;
273 INIT_LIST_HEAD(&uprobe->head);
274
275inode_error:
276 return ret;
277}
278
279int 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;
149b9a9d 290
149b9a9d
YB
291 return 0;
292
83b802dc 293register_error:
149b9a9d
YB
294 kfree(event->desc->name);
295 kfree(event->desc);
296error:
297 return ret;
298}
83b802dc 299EXPORT_SYMBOL_GPL(lttng_uprobes_register_event);
149b9a9d 300
9de67196
FD
301int 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
316register_error:
317 kfree(event_notifier->desc->name);
318 kfree(event_notifier->desc);
319error:
320 return ret;
321}
322EXPORT_SYMBOL_GPL(lttng_uprobes_register_event_notifier);
323
324static
325void lttng_uprobes_unregister(struct inode *inode, struct list_head *head)
149b9a9d 326{
3aed4dca
FD
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 */
9de67196
FD
333 list_for_each_entry_safe(iter, tmp, head, node) {
334 wrapper_uprobe_unregister(inode, iter->offset, &iter->up_consumer);
3aed4dca
FD
335 list_del(&iter->node);
336 kfree(iter);
337 }
9de67196
FD
338
339}
340
341void lttng_uprobes_unregister_event(struct lttng_event *event)
342{
343 lttng_uprobes_unregister(event->u.uprobe.inode, &event->u.uprobe.head);
149b9a9d 344}
83b802dc 345EXPORT_SYMBOL_GPL(lttng_uprobes_unregister_event);
149b9a9d 346
9de67196
FD
347void 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}
351EXPORT_SYMBOL_GPL(lttng_uprobes_unregister_event_notifier);
352
83b802dc 353void lttng_uprobes_destroy_event_private(struct lttng_event *event)
149b9a9d
YB
354{
355 iput(event->u.uprobe.inode);
356 kfree(event->desc->name);
357 kfree(event->desc);
358}
83b802dc 359EXPORT_SYMBOL_GPL(lttng_uprobes_destroy_event_private);
149b9a9d 360
9de67196
FD
361void 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}
367EXPORT_SYMBOL_GPL(lttng_uprobes_destroy_event_notifier_private);
368
149b9a9d
YB
369MODULE_LICENSE("GPL and additional rights");
370MODULE_AUTHOR("Yannick Brosseau");
371MODULE_DESCRIPTION("Linux Trace Toolkit Uprobes Support");
This page took 0.070924 seconds and 4 git commands to generate.