Namespace uprobe functions relating to events
[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 /*
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;
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;
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
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) {
131 printk(KERN_WARNING "LTTng: Cannot access file backing the fd(%d)\n", 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)
139 printk(KERN_WARNING "LTTng: Cannot grab a reference on the inode.\n");
140 error:
141 rcu_read_unlock();
142 return inode;
143 }
144
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)
151 {
152 int ret = 0;
153 struct lttng_uprobe_handler *uprobe_handler;
154
155 if (!priv_data) {
156 ret = -EINVAL;
157 goto end;
158 }
159
160 uprobe_handler = kzalloc(sizeof(struct lttng_uprobe_handler), GFP_KERNEL);
161 if (!uprobe_handler) {
162 printk(KERN_WARNING "LTTng: Error allocating uprobe_handler");
163 ret = -ENOMEM;
164 goto end;
165 }
166
167 /* Ensure the memory we just allocated don't trigger page faults. */
168 wrapper_vmalloc_sync_mappings();
169
170 uprobe_handler->u.event = priv_data;
171 uprobe_handler->up_consumer.handler = handler;
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
178 ret = wrapper_uprobe_register(uprobe->inode,
179 uprobe_handler->offset, &uprobe_handler->up_consumer);
180 if (ret) {
181 printk(KERN_WARNING "LTTng: Error registering probe on inode %lu "
182 "and offset 0x%llx\n", uprobe->inode->i_ino,
183 uprobe_handler->offset);
184 ret = -1;
185 goto register_error;
186 }
187
188 list_add(&uprobe_handler->node, &uprobe->head);
189
190 return ret;
191
192 register_error:
193 kfree(uprobe_handler);
194 end:
195 return ret;
196 }
197
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)
208 {
209 int ret = 0;
210 struct inode *inode;
211
212 inode = get_inode_from_fd(fd);
213 if (!inode) {
214 printk(KERN_WARNING "LTTng: Cannot get inode from fd\n");
215 ret = -EBADF;
216 goto inode_error;
217 }
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;
236
237 return 0;
238
239 register_error:
240 kfree(event->desc->name);
241 kfree(event->desc);
242 error:
243 return ret;
244 }
245 EXPORT_SYMBOL_GPL(lttng_uprobes_register_event);
246
247 void lttng_uprobes_unregister_event(struct lttng_event *event)
248 {
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 }
261 }
262 EXPORT_SYMBOL_GPL(lttng_uprobes_unregister_event);
263
264 void lttng_uprobes_destroy_event_private(struct lttng_event *event)
265 {
266 iput(event->u.uprobe.inode);
267 kfree(event->desc->name);
268 kfree(event->desc);
269 }
270 EXPORT_SYMBOL_GPL(lttng_uprobes_destroy_event_private);
271
272 MODULE_LICENSE("GPL and additional rights");
273 MODULE_AUTHOR("Yannick Brosseau");
274 MODULE_DESCRIPTION("Linux Trace Toolkit Uprobes Support");
This page took 0.034789 seconds and 4 git commands to generate.