Commit | Line | Data |
---|---|---|
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 | ||
25 | static | |
83b802dc | 26 | int 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, <tng_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 |
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 | ||
99d223ad | 76 | event_notifier->send_notification(event_notifier, NULL, NULL); |
9de67196 FD |
77 | return 0; |
78 | } | |
79 | ||
149b9a9d YB |
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; | |
ceabb767 MD |
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; | |
149b9a9d YB |
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 | ||
9de67196 FD |
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 | ||
56377c91 FD |
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 | */ | |
0aebcd68 | 173 | file = lttng_lookup_fd_rcu(fd); |
56377c91 | 174 | if (file == NULL) { |
5a15f70c | 175 | printk(KERN_WARNING "LTTng: Cannot access file backing the fd(%d)\n", fd); |
56377c91 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) | |
5a15f70c | 183 | printk(KERN_WARNING "LTTng: Cannot grab a reference on the inode.\n"); |
56377c91 FD |
184 | error: |
185 | rcu_read_unlock(); | |
186 | return inode; | |
187 | } | |
188 | ||
83b802dc FD |
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) | |
149b9a9d | 195 | { |
3aed4dca FD |
196 | int ret = 0; |
197 | struct lttng_uprobe_handler *uprobe_handler; | |
198 | ||
83b802dc | 199 | if (!priv_data) { |
3aed4dca FD |
200 | ret = -EINVAL; |
201 | goto end; | |
202 | } | |
203 | ||
204 | uprobe_handler = kzalloc(sizeof(struct lttng_uprobe_handler), GFP_KERNEL); | |
205 | if (!uprobe_handler) { | |
5a15f70c | 206 | printk(KERN_WARNING "LTTng: Error allocating uprobe_handler"); |
3aed4dca FD |
207 | ret = -ENOMEM; |
208 | goto end; | |
209 | } | |
210 | ||
9de67196 | 211 | /* Ensure the memory we just allocated don't event_notifier page faults. */ |
263b6c88 | 212 | wrapper_vmalloc_sync_mappings(); |
3aed4dca | 213 | |
83b802dc FD |
214 | uprobe_handler->u.event = priv_data; |
215 | uprobe_handler->up_consumer.handler = handler; | |
3aed4dca FD |
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 | ||
83b802dc | 222 | ret = wrapper_uprobe_register(uprobe->inode, |
3aed4dca FD |
223 | uprobe_handler->offset, &uprobe_handler->up_consumer); |
224 | if (ret) { | |
5a15f70c | 225 | printk(KERN_WARNING "LTTng: Error registering probe on inode %lu " |
83b802dc | 226 | "and offset 0x%llx\n", uprobe->inode->i_ino, |
3aed4dca FD |
227 | uprobe_handler->offset); |
228 | ret = -1; | |
229 | goto register_error; | |
230 | } | |
231 | ||
83b802dc | 232 | list_add(&uprobe_handler->node, &uprobe->head); |
3aed4dca FD |
233 | |
234 | return ret; | |
235 | ||
236 | register_error: | |
237 | kfree(uprobe_handler); | |
238 | end: | |
239 | return ret; | |
240 | } | |
3aed4dca | 241 | |
83b802dc FD |
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 | ||
9de67196 FD |
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 | ||
83b802dc FD |
258 | static |
259 | int lttng_uprobes_register(struct lttng_uprobe *uprobe, int fd) | |
3aed4dca FD |
260 | { |
261 | int ret = 0; | |
56377c91 | 262 | struct inode *inode; |
149b9a9d | 263 | |
56377c91 FD |
264 | inode = get_inode_from_fd(fd); |
265 | if (!inode) { | |
5a15f70c | 266 | printk(KERN_WARNING "LTTng: Cannot get inode from fd\n"); |
56377c91 FD |
267 | ret = -EBADF; |
268 | goto inode_error; | |
269 | } | |
83b802dc FD |
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; | |
149b9a9d | 288 | |
149b9a9d YB |
289 | return 0; |
290 | ||
83b802dc | 291 | register_error: |
149b9a9d YB |
292 | kfree(event->desc->name); |
293 | kfree(event->desc); | |
294 | error: | |
295 | return ret; | |
296 | } | |
83b802dc | 297 | EXPORT_SYMBOL_GPL(lttng_uprobes_register_event); |
149b9a9d | 298 | |
9de67196 FD |
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) | |
149b9a9d | 324 | { |
3aed4dca FD |
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 | */ | |
9de67196 FD |
331 | list_for_each_entry_safe(iter, tmp, head, node) { |
332 | wrapper_uprobe_unregister(inode, iter->offset, &iter->up_consumer); | |
3aed4dca FD |
333 | list_del(&iter->node); |
334 | kfree(iter); | |
335 | } | |
9de67196 FD |
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); | |
149b9a9d | 342 | } |
83b802dc | 343 | EXPORT_SYMBOL_GPL(lttng_uprobes_unregister_event); |
149b9a9d | 344 | |
9de67196 FD |
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 | ||
83b802dc | 351 | void lttng_uprobes_destroy_event_private(struct lttng_event *event) |
149b9a9d YB |
352 | { |
353 | iput(event->u.uprobe.inode); | |
354 | kfree(event->desc->name); | |
355 | kfree(event->desc); | |
356 | } | |
83b802dc | 357 | EXPORT_SYMBOL_GPL(lttng_uprobes_destroy_event_private); |
149b9a9d | 358 | |
9de67196 FD |
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 | ||
149b9a9d YB |
367 | MODULE_LICENSE("GPL and additional rights"); |
368 | MODULE_AUTHOR("Yannick Brosseau"); | |
369 | MODULE_DESCRIPTION("Linux Trace Toolkit Uprobes Support"); |