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> |
c35f268c | 13 | #include <linux/file.h> |
3aed4dca | 14 | #include <linux/list.h> |
149b9a9d YB |
15 | #include <linux/module.h> |
16 | #include <linux/namei.h> | |
17 | #include <linux/slab.h> | |
3aed4dca | 18 | #include <linux/uaccess.h> |
853f5b15 MJ |
19 | #include <linux/uprobes.h> |
20 | ||
2df37e95 | 21 | #include <lttng/events.h> |
a67ba386 | 22 | #include <lttng/events-internal.h> |
2df37e95 | 23 | #include <lttng/tracer.h> |
149b9a9d | 24 | #include <wrapper/irqflags.h> |
24591303 | 25 | #include <ringbuffer/frontend_types.h> |
149b9a9d YB |
26 | #include <wrapper/vmalloc.h> |
27 | ||
28 | static | |
83b802dc | 29 | int lttng_uprobes_event_handler_pre(struct uprobe_consumer *uc, struct pt_regs *regs) |
149b9a9d | 30 | { |
3aed4dca FD |
31 | struct lttng_uprobe_handler *uprobe_handler = |
32 | container_of(uc, struct lttng_uprobe_handler, up_consumer); | |
e2d5dbc7 | 33 | struct lttng_kernel_event_common *event = uprobe_handler->event; |
a92e844e | 34 | struct lttng_kernel_probe_ctx lttng_probe_ctx = { |
e2d5dbc7 | 35 | .event = event, |
149b9a9d YB |
36 | .interruptible = !lttng_regs_irqs_disabled(regs), |
37 | }; | |
149b9a9d YB |
38 | struct { |
39 | unsigned long ip; | |
56377c91 | 40 | } payload; |
149b9a9d | 41 | |
e2d5dbc7 MD |
42 | switch (event->type) { |
43 | case LTTNG_KERNEL_EVENT_TYPE_RECORDER: | |
44 | { | |
45 | struct lttng_kernel_event_recorder *event_recorder = | |
46 | container_of(event, struct lttng_kernel_event_recorder, parent); | |
f7d06400 | 47 | struct lttng_kernel_channel_buffer *chan = event_recorder->chan; |
e2d5dbc7 | 48 | |
f7d06400 | 49 | if (unlikely(!LTTNG_READ_ONCE(chan->parent.session->active))) |
e2d5dbc7 | 50 | return 0; |
f7d06400 | 51 | if (unlikely(!LTTNG_READ_ONCE(chan->parent.enabled))) |
e2d5dbc7 MD |
52 | return 0; |
53 | break; | |
54 | } | |
55 | case LTTNG_KERNEL_EVENT_TYPE_NOTIFIER: | |
56 | break; | |
57 | default: | |
58 | WARN_ON_ONCE(1); | |
59 | } | |
9de67196 | 60 | |
e2d5dbc7 | 61 | if (unlikely(!LTTNG_READ_ONCE(event->enabled))) |
9de67196 FD |
62 | return 0; |
63 | ||
e2d5dbc7 MD |
64 | switch (event->type) { |
65 | case LTTNG_KERNEL_EVENT_TYPE_RECORDER: | |
66 | { | |
67 | struct lttng_kernel_event_recorder *event_recorder = | |
68 | container_of(event, struct lttng_kernel_event_recorder, parent); | |
f7d06400 | 69 | struct lttng_kernel_channel_buffer *chan = event_recorder->chan; |
8a57ec02 | 70 | struct lttng_kernel_ring_buffer_ctx ctx; |
e2d5dbc7 MD |
71 | int ret; |
72 | ||
b1199bd3 MD |
73 | lib_ring_buffer_ctx_init(&ctx, event_recorder, |
74 | sizeof(payload), lttng_alignof(payload), <tng_probe_ctx); | |
e2d5dbc7 | 75 | |
c2fb9c1c | 76 | ret = chan->ops->event_reserve(&ctx); |
e2d5dbc7 MD |
77 | if (ret < 0) |
78 | return 0; | |
79 | ||
80 | /* Event payload. */ | |
81 | payload.ip = (unsigned long)instruction_pointer(regs); | |
82 | ||
f5ffbd77 | 83 | chan->ops->event_write(&ctx, &payload, sizeof(payload), lttng_alignof(payload)); |
e2d5dbc7 MD |
84 | chan->ops->event_commit(&ctx); |
85 | break; | |
86 | } | |
87 | case LTTNG_KERNEL_EVENT_TYPE_NOTIFIER: | |
88 | { | |
89 | struct lttng_kernel_event_notifier *event_notifier = | |
90 | container_of(event, struct lttng_kernel_event_notifier, parent); | |
91 | struct lttng_kernel_notification_ctx notif_ctx; | |
92 | ||
93 | notif_ctx.eval_capture = LTTNG_READ_ONCE(event_notifier->eval_capture); | |
94 | event_notifier->notification_send(event_notifier, NULL, NULL, ¬if_ctx); | |
95 | break; | |
96 | } | |
97 | default: | |
98 | WARN_ON_ONCE(1); | |
99 | } | |
9de67196 FD |
100 | return 0; |
101 | } | |
102 | ||
6c8c025b MD |
103 | static const struct lttng_kernel_event_field *event_fields[] = { |
104 | lttng_kernel_static_event_field("ip", | |
105 | lttng_kernel_static_type_integer_from_type(unsigned long, __BYTE_ORDER, 16), | |
4697aac7 | 106 | false, false), |
6c8c025b MD |
107 | }; |
108 | ||
109 | static const struct lttng_kernel_tracepoint_class tp_class = { | |
110 | .nr_fields = ARRAY_SIZE(event_fields), | |
111 | .fields = event_fields, | |
112 | }; | |
437d5aa5 | 113 | |
149b9a9d YB |
114 | /* |
115 | * Create event description. | |
116 | */ | |
117 | static | |
17806c46 | 118 | int lttng_create_uprobe_event(const char *name, struct lttng_kernel_event_common *event) |
149b9a9d | 119 | { |
437d5aa5 | 120 | struct lttng_kernel_event_desc *desc; |
149b9a9d YB |
121 | int ret; |
122 | ||
437d5aa5 | 123 | desc = kzalloc(sizeof(*desc), GFP_KERNEL); |
149b9a9d YB |
124 | if (!desc) |
125 | return -ENOMEM; | |
6c8c025b | 126 | desc->tp_class = &tp_class; |
437d5aa5 MD |
127 | desc->event_name = kstrdup(name, GFP_KERNEL); |
128 | if (!desc->event_name) { | |
149b9a9d YB |
129 | ret = -ENOMEM; |
130 | goto error_str; | |
131 | } | |
149b9a9d | 132 | desc->owner = THIS_MODULE; |
17806c46 | 133 | event->priv->desc = desc; |
9de67196 FD |
134 | |
135 | return 0; | |
136 | ||
137 | error_str: | |
138 | kfree(desc); | |
139 | return ret; | |
140 | } | |
141 | ||
56377c91 FD |
142 | /* |
143 | * Returns the inode struct from the current task and an fd. The inode is | |
144 | * grabbed by this function and must be put once we are done with it using | |
145 | * iput(). | |
146 | */ | |
147 | static struct inode *get_inode_from_fd(int fd) | |
148 | { | |
149 | struct file *file; | |
150 | struct inode *inode; | |
151 | ||
152 | rcu_read_lock(); | |
153 | /* | |
154 | * Returns the file backing the given fd. Needs to be done inside an RCU | |
155 | * critical section. | |
156 | */ | |
c35f268c | 157 | file = lttng_lookup_fdget_rcu(fd); |
56377c91 | 158 | if (file == NULL) { |
5a15f70c | 159 | printk(KERN_WARNING "LTTng: Cannot access file backing the fd(%d)\n", fd); |
56377c91 FD |
160 | inode = NULL; |
161 | goto error; | |
162 | } | |
163 | ||
164 | /* Grab a reference on the inode. */ | |
165 | inode = igrab(file->f_path.dentry->d_inode); | |
166 | if (inode == NULL) | |
5a15f70c | 167 | printk(KERN_WARNING "LTTng: Cannot grab a reference on the inode.\n"); |
c35f268c | 168 | |
56377c91 FD |
169 | error: |
170 | rcu_read_unlock(); | |
c35f268c KS |
171 | if (file) |
172 | fput(file); | |
56377c91 FD |
173 | return inode; |
174 | } | |
175 | ||
83b802dc FD |
176 | |
177 | static | |
178 | int lttng_uprobes_add_callsite(struct lttng_uprobe *uprobe, | |
606828e4 | 179 | struct lttng_kernel_abi_event_callsite __user *callsite, |
83b802dc FD |
180 | int (*handler)(struct uprobe_consumer *self, struct pt_regs *regs), |
181 | void *priv_data) | |
149b9a9d | 182 | { |
3aed4dca FD |
183 | int ret = 0; |
184 | struct lttng_uprobe_handler *uprobe_handler; | |
185 | ||
83b802dc | 186 | if (!priv_data) { |
3aed4dca FD |
187 | ret = -EINVAL; |
188 | goto end; | |
189 | } | |
190 | ||
191 | uprobe_handler = kzalloc(sizeof(struct lttng_uprobe_handler), GFP_KERNEL); | |
192 | if (!uprobe_handler) { | |
5a15f70c | 193 | printk(KERN_WARNING "LTTng: Error allocating uprobe_handler"); |
3aed4dca FD |
194 | ret = -ENOMEM; |
195 | goto end; | |
196 | } | |
197 | ||
196bfc6a | 198 | /* Ensure the memory we just allocated don't notify page faults. */ |
263b6c88 | 199 | wrapper_vmalloc_sync_mappings(); |
3aed4dca | 200 | |
e2d5dbc7 | 201 | uprobe_handler->event = priv_data; |
83b802dc | 202 | uprobe_handler->up_consumer.handler = handler; |
3aed4dca FD |
203 | |
204 | ret = copy_from_user(&uprobe_handler->offset, &callsite->u.uprobe.offset, sizeof(uint64_t)); | |
205 | if (ret) { | |
206 | goto register_error; | |
207 | } | |
208 | ||
853f5b15 | 209 | ret = uprobe_register(uprobe->inode, |
3aed4dca FD |
210 | uprobe_handler->offset, &uprobe_handler->up_consumer); |
211 | if (ret) { | |
5a15f70c | 212 | printk(KERN_WARNING "LTTng: Error registering probe on inode %lu " |
83b802dc | 213 | "and offset 0x%llx\n", uprobe->inode->i_ino, |
3aed4dca FD |
214 | uprobe_handler->offset); |
215 | ret = -1; | |
216 | goto register_error; | |
217 | } | |
218 | ||
83b802dc | 219 | list_add(&uprobe_handler->node, &uprobe->head); |
3aed4dca FD |
220 | |
221 | return ret; | |
222 | ||
223 | register_error: | |
224 | kfree(uprobe_handler); | |
225 | end: | |
226 | return ret; | |
227 | } | |
3aed4dca | 228 | |
e2d5dbc7 | 229 | int lttng_uprobes_event_add_callsite(struct lttng_kernel_event_common *event, |
606828e4 | 230 | struct lttng_kernel_abi_event_callsite __user *callsite) |
83b802dc | 231 | { |
e2d5dbc7 MD |
232 | return lttng_uprobes_add_callsite(&event->priv->u.uprobe, callsite, |
233 | lttng_uprobes_event_handler_pre, event); | |
83b802dc FD |
234 | } |
235 | EXPORT_SYMBOL_GPL(lttng_uprobes_event_add_callsite); | |
236 | ||
237 | static | |
238 | int lttng_uprobes_register(struct lttng_uprobe *uprobe, int fd) | |
3aed4dca FD |
239 | { |
240 | int ret = 0; | |
56377c91 | 241 | struct inode *inode; |
149b9a9d | 242 | |
56377c91 FD |
243 | inode = get_inode_from_fd(fd); |
244 | if (!inode) { | |
5a15f70c | 245 | printk(KERN_WARNING "LTTng: Cannot get inode from fd\n"); |
56377c91 FD |
246 | ret = -EBADF; |
247 | goto inode_error; | |
248 | } | |
83b802dc FD |
249 | uprobe->inode = inode; |
250 | INIT_LIST_HEAD(&uprobe->head); | |
251 | ||
252 | inode_error: | |
253 | return ret; | |
254 | } | |
255 | ||
17806c46 | 256 | int lttng_uprobes_register_event(const char *name, int fd, struct lttng_kernel_event_common *event) |
83b802dc FD |
257 | { |
258 | int ret = 0; | |
259 | ||
17806c46 | 260 | ret = lttng_create_uprobe_event(name, event); |
83b802dc FD |
261 | if (ret) |
262 | goto error; | |
263 | ||
17806c46 | 264 | ret = lttng_uprobes_register(&event->priv->u.uprobe, fd); |
83b802dc FD |
265 | if (ret) |
266 | goto register_error; | |
149b9a9d | 267 | |
149b9a9d YB |
268 | return 0; |
269 | ||
83b802dc | 270 | register_error: |
17806c46 MD |
271 | kfree(event->priv->desc->event_name); |
272 | kfree(event->priv->desc); | |
149b9a9d YB |
273 | error: |
274 | return ret; | |
275 | } | |
83b802dc | 276 | EXPORT_SYMBOL_GPL(lttng_uprobes_register_event); |
149b9a9d | 277 | |
9de67196 FD |
278 | static |
279 | void lttng_uprobes_unregister(struct inode *inode, struct list_head *head) | |
149b9a9d | 280 | { |
3aed4dca FD |
281 | struct lttng_uprobe_handler *iter, *tmp; |
282 | ||
283 | /* | |
284 | * Iterate over the list of handler, remove each handler from the list | |
285 | * and free the struct. | |
286 | */ | |
9de67196 | 287 | list_for_each_entry_safe(iter, tmp, head, node) { |
853f5b15 | 288 | uprobe_unregister(inode, iter->offset, &iter->up_consumer); |
3aed4dca FD |
289 | list_del(&iter->node); |
290 | kfree(iter); | |
291 | } | |
9de67196 FD |
292 | } |
293 | ||
17806c46 | 294 | void lttng_uprobes_unregister_event(struct lttng_kernel_event_common *event) |
9de67196 | 295 | { |
17806c46 | 296 | lttng_uprobes_unregister(event->priv->u.uprobe.inode, &event->priv->u.uprobe.head); |
149b9a9d | 297 | } |
83b802dc | 298 | EXPORT_SYMBOL_GPL(lttng_uprobes_unregister_event); |
149b9a9d | 299 | |
17806c46 | 300 | void lttng_uprobes_destroy_event_private(struct lttng_kernel_event_common *event) |
149b9a9d | 301 | { |
17806c46 MD |
302 | iput(event->priv->u.uprobe.inode); |
303 | kfree(event->priv->desc->event_name); | |
304 | kfree(event->priv->desc); | |
149b9a9d | 305 | } |
83b802dc | 306 | EXPORT_SYMBOL_GPL(lttng_uprobes_destroy_event_private); |
149b9a9d YB |
307 | |
308 | MODULE_LICENSE("GPL and additional rights"); | |
309 | MODULE_AUTHOR("Yannick Brosseau"); | |
310 | MODULE_DESCRIPTION("Linux Trace Toolkit Uprobes Support"); |