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 | ||
12 | #include <linux/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 | ||
66 | /* | |
67 | * Create event description. | |
68 | */ | |
69 | static | |
70 | int lttng_create_uprobe_event(const char *name, struct lttng_event *event) | |
71 | { | |
72 | struct lttng_event_desc *desc; | |
73 | struct lttng_event_field *fields; | |
74 | int ret; | |
75 | ||
76 | desc = kzalloc(sizeof(*event->desc), GFP_KERNEL); | |
77 | if (!desc) | |
78 | return -ENOMEM; | |
79 | desc->name = kstrdup(name, GFP_KERNEL); | |
80 | if (!desc->name) { | |
81 | ret = -ENOMEM; | |
82 | goto error_str; | |
83 | } | |
84 | ||
85 | desc->nr_fields = 1; | |
86 | desc->fields = fields = | |
87 | kzalloc(1 * sizeof(struct lttng_event_field), GFP_KERNEL); | |
88 | ||
89 | if (!desc->fields) { | |
90 | ret = -ENOMEM; | |
91 | goto error_fields; | |
92 | } | |
93 | fields[0].name = "ip"; | |
94 | fields[0].type.atype = atype_integer; | |
ceabb767 MD |
95 | fields[0].type.u.integer.size = sizeof(unsigned long) * CHAR_BIT; |
96 | fields[0].type.u.integer.alignment = lttng_alignof(unsigned long) * CHAR_BIT; | |
97 | fields[0].type.u.integer.signedness = lttng_is_signed_type(unsigned long); | |
98 | fields[0].type.u.integer.reverse_byte_order = 0; | |
99 | fields[0].type.u.integer.base = 16; | |
100 | fields[0].type.u.integer.encoding = lttng_encode_none; | |
149b9a9d YB |
101 | |
102 | desc->owner = THIS_MODULE; | |
103 | event->desc = desc; | |
104 | ||
105 | return 0; | |
106 | ||
107 | error_fields: | |
108 | kfree(desc->name); | |
109 | error_str: | |
110 | kfree(desc); | |
111 | return ret; | |
112 | } | |
113 | ||
56377c91 FD |
114 | /* |
115 | * Returns the inode struct from the current task and an fd. The inode is | |
116 | * grabbed by this function and must be put once we are done with it using | |
117 | * iput(). | |
118 | */ | |
119 | static struct inode *get_inode_from_fd(int fd) | |
120 | { | |
121 | struct file *file; | |
122 | struct inode *inode; | |
123 | ||
124 | rcu_read_lock(); | |
125 | /* | |
126 | * Returns the file backing the given fd. Needs to be done inside an RCU | |
127 | * critical section. | |
128 | */ | |
129 | file = fcheck(fd); | |
130 | if (file == NULL) { | |
5a15f70c | 131 | printk(KERN_WARNING "LTTng: Cannot access file backing the fd(%d)\n", fd); |
56377c91 FD |
132 | inode = NULL; |
133 | goto error; | |
134 | } | |
135 | ||
136 | /* Grab a reference on the inode. */ | |
137 | inode = igrab(file->f_path.dentry->d_inode); | |
138 | if (inode == NULL) | |
5a15f70c | 139 | printk(KERN_WARNING "LTTng: Cannot grab a reference on the inode.\n"); |
56377c91 FD |
140 | error: |
141 | rcu_read_unlock(); | |
142 | return inode; | |
143 | } | |
144 | ||
83b802dc FD |
145 | |
146 | static | |
147 | int lttng_uprobes_add_callsite(struct lttng_uprobe *uprobe, | |
148 | struct lttng_kernel_event_callsite __user *callsite, | |
149 | int (*handler)(struct uprobe_consumer *self, struct pt_regs *regs), | |
150 | void *priv_data) | |
149b9a9d | 151 | { |
3aed4dca FD |
152 | int ret = 0; |
153 | struct lttng_uprobe_handler *uprobe_handler; | |
154 | ||
83b802dc | 155 | if (!priv_data) { |
3aed4dca FD |
156 | ret = -EINVAL; |
157 | goto end; | |
158 | } | |
159 | ||
160 | uprobe_handler = kzalloc(sizeof(struct lttng_uprobe_handler), GFP_KERNEL); | |
161 | if (!uprobe_handler) { | |
5a15f70c | 162 | printk(KERN_WARNING "LTTng: Error allocating uprobe_handler"); |
3aed4dca FD |
163 | ret = -ENOMEM; |
164 | goto end; | |
165 | } | |
166 | ||
167 | /* Ensure the memory we just allocated don't trigger page faults. */ | |
263b6c88 | 168 | wrapper_vmalloc_sync_mappings(); |
3aed4dca | 169 | |
83b802dc FD |
170 | uprobe_handler->u.event = priv_data; |
171 | uprobe_handler->up_consumer.handler = handler; | |
3aed4dca FD |
172 | |
173 | ret = copy_from_user(&uprobe_handler->offset, &callsite->u.uprobe.offset, sizeof(uint64_t)); | |
174 | if (ret) { | |
175 | goto register_error; | |
176 | } | |
177 | ||
83b802dc | 178 | ret = wrapper_uprobe_register(uprobe->inode, |
3aed4dca FD |
179 | uprobe_handler->offset, &uprobe_handler->up_consumer); |
180 | if (ret) { | |
5a15f70c | 181 | printk(KERN_WARNING "LTTng: Error registering probe on inode %lu " |
83b802dc | 182 | "and offset 0x%llx\n", uprobe->inode->i_ino, |
3aed4dca FD |
183 | uprobe_handler->offset); |
184 | ret = -1; | |
185 | goto register_error; | |
186 | } | |
187 | ||
83b802dc | 188 | list_add(&uprobe_handler->node, &uprobe->head); |
3aed4dca FD |
189 | |
190 | return ret; | |
191 | ||
192 | register_error: | |
193 | kfree(uprobe_handler); | |
194 | end: | |
195 | return ret; | |
196 | } | |
3aed4dca | 197 | |
83b802dc FD |
198 | int lttng_uprobes_event_add_callsite(struct lttng_event *event, |
199 | struct lttng_kernel_event_callsite __user *callsite) | |
200 | { | |
201 | return lttng_uprobes_add_callsite(&event->u.uprobe, callsite, | |
202 | lttng_uprobes_event_handler_pre, event); | |
203 | } | |
204 | EXPORT_SYMBOL_GPL(lttng_uprobes_event_add_callsite); | |
205 | ||
206 | static | |
207 | int lttng_uprobes_register(struct lttng_uprobe *uprobe, int fd) | |
3aed4dca FD |
208 | { |
209 | int ret = 0; | |
56377c91 | 210 | struct inode *inode; |
149b9a9d | 211 | |
56377c91 FD |
212 | inode = get_inode_from_fd(fd); |
213 | if (!inode) { | |
5a15f70c | 214 | printk(KERN_WARNING "LTTng: Cannot get inode from fd\n"); |
56377c91 FD |
215 | ret = -EBADF; |
216 | goto inode_error; | |
217 | } | |
83b802dc FD |
218 | uprobe->inode = inode; |
219 | INIT_LIST_HEAD(&uprobe->head); | |
220 | ||
221 | inode_error: | |
222 | return ret; | |
223 | } | |
224 | ||
225 | int lttng_uprobes_register_event(const char *name, int fd, struct lttng_event *event) | |
226 | { | |
227 | int ret = 0; | |
228 | ||
229 | ret = lttng_create_uprobe_event(name, event); | |
230 | if (ret) | |
231 | goto error; | |
232 | ||
233 | ret = lttng_uprobes_register(&event->u.uprobe, fd); | |
234 | if (ret) | |
235 | goto register_error; | |
149b9a9d | 236 | |
149b9a9d YB |
237 | return 0; |
238 | ||
83b802dc | 239 | register_error: |
149b9a9d YB |
240 | kfree(event->desc->name); |
241 | kfree(event->desc); | |
242 | error: | |
243 | return ret; | |
244 | } | |
83b802dc | 245 | EXPORT_SYMBOL_GPL(lttng_uprobes_register_event); |
149b9a9d | 246 | |
83b802dc | 247 | void lttng_uprobes_unregister_event(struct lttng_event *event) |
149b9a9d | 248 | { |
3aed4dca FD |
249 | struct lttng_uprobe_handler *iter, *tmp; |
250 | ||
251 | /* | |
252 | * Iterate over the list of handler, remove each handler from the list | |
253 | * and free the struct. | |
254 | */ | |
255 | list_for_each_entry_safe(iter, tmp, &event->u.uprobe.head, node) { | |
256 | wrapper_uprobe_unregister(event->u.uprobe.inode, iter->offset, | |
257 | &iter->up_consumer); | |
258 | list_del(&iter->node); | |
259 | kfree(iter); | |
260 | } | |
149b9a9d | 261 | } |
83b802dc | 262 | EXPORT_SYMBOL_GPL(lttng_uprobes_unregister_event); |
149b9a9d | 263 | |
83b802dc | 264 | void lttng_uprobes_destroy_event_private(struct lttng_event *event) |
149b9a9d YB |
265 | { |
266 | iput(event->u.uprobe.inode); | |
267 | kfree(event->desc->name); | |
268 | kfree(event->desc); | |
269 | } | |
83b802dc | 270 | EXPORT_SYMBOL_GPL(lttng_uprobes_destroy_event_private); |
149b9a9d YB |
271 | |
272 | MODULE_LICENSE("GPL and additional rights"); | |
273 | MODULE_AUTHOR("Yannick Brosseau"); | |
274 | MODULE_DESCRIPTION("Linux Trace Toolkit Uprobes Support"); |