Move headers under include/
[lttng-modules.git] / 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 <linux/uprobes.h>
19 #include <lttng/lttng-events.h>
20 #include <lttng/lttng-tracer.h>
21 #include <wrapper/irqflags.h>
22 #include <ringbuffer/frontend_types.h>
23
24 static
25 int lttng_uprobes_handler_pre(struct uprobe_consumer *uc, struct pt_regs *regs)
26 {
27 struct lttng_uprobe_handler *uprobe_handler =
28 container_of(uc, struct lttng_uprobe_handler, up_consumer);
29 struct lttng_event *event = uprobe_handler->event;
30 struct lttng_probe_ctx lttng_probe_ctx = {
31 .event = event,
32 .interruptible = !lttng_regs_irqs_disabled(regs),
33 };
34 struct lttng_channel *chan = event->chan;
35 struct lib_ring_buffer_ctx ctx;
36 int ret;
37
38 struct {
39 unsigned long ip;
40 } payload;
41
42 if (unlikely(!READ_ONCE(chan->session->active)))
43 return 0;
44 if (unlikely(!READ_ONCE(chan->enabled)))
45 return 0;
46 if (unlikely(!READ_ONCE(event->enabled)))
47 return 0;
48
49 lib_ring_buffer_ctx_init(&ctx, chan->chan, &lttng_probe_ctx,
50 sizeof(payload), lttng_alignof(payload), -1);
51
52 ret = chan->ops->event_reserve(&ctx, event->id);
53 if (ret < 0)
54 return 0;
55
56 /* Event payload. */
57 payload.ip = (unsigned long)instruction_pointer(regs);
58
59 lib_ring_buffer_align_ctx(&ctx, lttng_alignof(payload));
60 chan->ops->event_write(&ctx, &payload, sizeof(payload));
61 chan->ops->event_commit(&ctx);
62 return 0;
63 }
64
65 /*
66 * Create event description.
67 */
68 static
69 int lttng_create_uprobe_event(const char *name, struct lttng_event *event)
70 {
71 struct lttng_event_desc *desc;
72 struct lttng_event_field *fields;
73 int ret;
74
75 desc = kzalloc(sizeof(*event->desc), GFP_KERNEL);
76 if (!desc)
77 return -ENOMEM;
78 desc->name = kstrdup(name, GFP_KERNEL);
79 if (!desc->name) {
80 ret = -ENOMEM;
81 goto error_str;
82 }
83
84 desc->nr_fields = 1;
85 desc->fields = fields =
86 kzalloc(1 * sizeof(struct lttng_event_field), GFP_KERNEL);
87
88 if (!desc->fields) {
89 ret = -ENOMEM;
90 goto error_fields;
91 }
92 fields[0].name = "ip";
93 fields[0].type.atype = atype_integer;
94 fields[0].type.u.integer.size = sizeof(unsigned long) * CHAR_BIT;
95 fields[0].type.u.integer.alignment = lttng_alignof(unsigned long) * CHAR_BIT;
96 fields[0].type.u.integer.signedness = lttng_is_signed_type(unsigned long);
97 fields[0].type.u.integer.reverse_byte_order = 0;
98 fields[0].type.u.integer.base = 16;
99 fields[0].type.u.integer.encoding = lttng_encode_none;
100
101 desc->owner = THIS_MODULE;
102 event->desc = desc;
103
104 return 0;
105
106 error_fields:
107 kfree(desc->name);
108 error_str:
109 kfree(desc);
110 return ret;
111 }
112
113 /*
114 * Returns the inode struct from the current task and an fd. The inode is
115 * grabbed by this function and must be put once we are done with it using
116 * iput().
117 */
118 static struct inode *get_inode_from_fd(int fd)
119 {
120 struct file *file;
121 struct inode *inode;
122
123 rcu_read_lock();
124 /*
125 * Returns the file backing the given fd. Needs to be done inside an RCU
126 * critical section.
127 */
128 file = fcheck(fd);
129 if (file == NULL) {
130 printk(KERN_WARNING "Cannot access file backing the fd(%d)\n", fd);
131 inode = NULL;
132 goto error;
133 }
134
135 /* Grab a reference on the inode. */
136 inode = igrab(file->f_path.dentry->d_inode);
137 if (inode == NULL)
138 printk(KERN_WARNING "Cannot grab a reference on the inode.\n");
139 error:
140 rcu_read_unlock();
141 return inode;
142 }
143
144 int lttng_uprobes_add_callsite(struct lttng_event *event,
145 struct lttng_kernel_event_callsite __user *callsite)
146 {
147 int ret = 0;
148 struct lttng_uprobe_handler *uprobe_handler;
149
150 if (!event) {
151 ret = -EINVAL;
152 goto end;
153 }
154
155 uprobe_handler = kzalloc(sizeof(struct lttng_uprobe_handler), GFP_KERNEL);
156 if (!uprobe_handler) {
157 printk(KERN_WARNING "Error allocating uprobe_uprobe_handlers");
158 ret = -ENOMEM;
159 goto end;
160 }
161
162 uprobe_handler->event = event;
163 uprobe_handler->up_consumer.handler = lttng_uprobes_handler_pre;
164
165 ret = copy_from_user(&uprobe_handler->offset, &callsite->u.uprobe.offset, sizeof(uint64_t));
166 if (ret) {
167 goto register_error;
168 }
169
170 ret = uprobe_register(event->u.uprobe.inode,
171 uprobe_handler->offset, &uprobe_handler->up_consumer);
172 if (ret) {
173 printk(KERN_WARNING "Error registering probe on inode %lu "
174 "and offset 0x%llx\n", event->u.uprobe.inode->i_ino,
175 uprobe_handler->offset);
176 ret = -1;
177 goto register_error;
178 }
179
180 list_add(&uprobe_handler->node, &event->u.uprobe.head);
181
182 return ret;
183
184 register_error:
185 kfree(uprobe_handler);
186 end:
187 return ret;
188 }
189 EXPORT_SYMBOL_GPL(lttng_uprobes_add_callsite);
190
191 int lttng_uprobes_register(const char *name, int fd, struct lttng_event *event)
192 {
193 int ret = 0;
194 struct inode *inode;
195
196 ret = lttng_create_uprobe_event(name, event);
197 if (ret)
198 goto error;
199
200 inode = get_inode_from_fd(fd);
201 if (!inode) {
202 printk(KERN_WARNING "Cannot get inode from fd\n");
203 ret = -EBADF;
204 goto inode_error;
205 }
206 event->u.uprobe.inode = inode;
207 INIT_LIST_HEAD(&event->u.uprobe.head);
208
209 return 0;
210
211 inode_error:
212 kfree(event->desc->name);
213 kfree(event->desc);
214 error:
215 return ret;
216 }
217 EXPORT_SYMBOL_GPL(lttng_uprobes_register);
218
219 void lttng_uprobes_unregister(struct lttng_event *event)
220 {
221 struct lttng_uprobe_handler *iter, *tmp;
222
223 /*
224 * Iterate over the list of handler, remove each handler from the list
225 * and free the struct.
226 */
227 list_for_each_entry_safe(iter, tmp, &event->u.uprobe.head, node) {
228 uprobe_unregister(event->u.uprobe.inode, iter->offset,
229 &iter->up_consumer);
230 list_del(&iter->node);
231 kfree(iter);
232 }
233 }
234 EXPORT_SYMBOL_GPL(lttng_uprobes_unregister);
235
236 void lttng_uprobes_destroy_private(struct lttng_event *event)
237 {
238 iput(event->u.uprobe.inode);
239 kfree(event->desc->name);
240 kfree(event->desc);
241 }
242 EXPORT_SYMBOL_GPL(lttng_uprobes_destroy_private);
243
244 MODULE_LICENSE("GPL and additional rights");
245 MODULE_AUTHOR("Yannick Brosseau");
246 MODULE_DESCRIPTION("Linux Trace Toolkit Uprobes Support");
This page took 0.034793 seconds and 4 git commands to generate.