uprobe: Receive file descriptor from session instead of path to file
[lttng-modules.git] / probes / lttng-uprobes.c
CommitLineData
149b9a9d
YB
1/*
2 * probes/lttng-uprobes.c
3 *
4 * LTTng uprobes integration module.
5 *
6 * Copyright (C) 2013 Yannick Brosseau <yannick.brosseau@gmail.com>
7 * Copyright (C) 2009-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; only
12 * version 2.1 of the License.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24#include <linux/fdtable.h>
25#include <linux/module.h>
26#include <linux/namei.h>
27#include <linux/slab.h>
28#include <lttng-events.h>
29#include <lttng-tracer.h>
30#include <wrapper/irqflags.h>
31#include <wrapper/ringbuffer/frontend_types.h>
32#include <wrapper/uprobes.h>
33#include <wrapper/vmalloc.h>
34
35static
36int lttng_uprobes_handler_pre(struct uprobe_consumer *uc, struct pt_regs *regs)
37{
38 struct lttng_event *event =
39 container_of(uc, struct lttng_event, u.uprobe.up_consumer);
40 struct lttng_probe_ctx lttng_probe_ctx = {
41 .event = event,
42 .interruptible = !lttng_regs_irqs_disabled(regs),
43 };
44 struct lttng_channel *chan = event->chan;
45 struct lib_ring_buffer_ctx ctx;
46 int ret;
47
48 struct {
49 unsigned long ip;
56377c91 50 } payload;
149b9a9d
YB
51
52 if (unlikely(!ACCESS_ONCE(chan->session->active)))
53 return 0;
54 if (unlikely(!ACCESS_ONCE(chan->enabled)))
55 return 0;
56 if (unlikely(!ACCESS_ONCE(event->enabled)))
57 return 0;
58
59 lib_ring_buffer_ctx_init(&ctx, chan->chan, &lttng_probe_ctx,
60 sizeof(payload), lttng_alignof(payload), -1);
61
62 ret = chan->ops->event_reserve(&ctx, event->id);
63 if (ret < 0)
64 return 0;
65
66 /* Event payload. */
67 payload.ip = regs->ip;
56377c91 68
149b9a9d
YB
69 lib_ring_buffer_align_ctx(&ctx, lttng_alignof(payload));
70 chan->ops->event_write(&ctx, &payload, sizeof(payload));
71 chan->ops->event_commit(&ctx);
72 return 0;
73}
74
75/*
76 * Create event description.
77 */
78static
79int lttng_create_uprobe_event(const char *name, struct lttng_event *event)
80{
81 struct lttng_event_desc *desc;
82 struct lttng_event_field *fields;
83 int ret;
84
85 desc = kzalloc(sizeof(*event->desc), GFP_KERNEL);
86 if (!desc)
87 return -ENOMEM;
88 desc->name = kstrdup(name, GFP_KERNEL);
89 if (!desc->name) {
90 ret = -ENOMEM;
91 goto error_str;
92 }
93
94 desc->nr_fields = 1;
95 desc->fields = fields =
96 kzalloc(1 * sizeof(struct lttng_event_field), GFP_KERNEL);
97
98 if (!desc->fields) {
99 ret = -ENOMEM;
100 goto error_fields;
101 }
102 fields[0].name = "ip";
103 fields[0].type.atype = atype_integer;
104 fields[0].type.u.basic.integer.size = sizeof(unsigned long) * CHAR_BIT;
105 fields[0].type.u.basic.integer.alignment = lttng_alignof(unsigned long) * CHAR_BIT;
106 fields[0].type.u.basic.integer.signedness = lttng_is_signed_type(unsigned long);
107 fields[0].type.u.basic.integer.reverse_byte_order = 0;
108 fields[0].type.u.basic.integer.base = 16;
109 fields[0].type.u.basic.integer.encoding = lttng_encode_none;
110
111 desc->owner = THIS_MODULE;
112 event->desc = desc;
113
114 return 0;
115
116error_fields:
117 kfree(desc->name);
118error_str:
119 kfree(desc);
120 return ret;
121}
122
56377c91
FD
123/*
124 * Returns the inode struct from the current task and an fd. The inode is
125 * grabbed by this function and must be put once we are done with it using
126 * iput().
127 */
128static struct inode *get_inode_from_fd(int fd)
129{
130 struct file *file;
131 struct inode *inode;
132
133 rcu_read_lock();
134 /*
135 * Returns the file backing the given fd. Needs to be done inside an RCU
136 * critical section.
137 */
138 file = fcheck(fd);
139 if (file == NULL) {
140 printk(KERN_WARNING "Cannot access file backing the fd(%d)\n", fd);
141 inode = NULL;
142 goto error;
143 }
144
145 /* Grab a reference on the inode. */
146 inode = igrab(file->f_path.dentry->d_inode);
147 if (inode == NULL)
148 printk(KERN_WARNING "Cannot grab a reference on the inode.\n");
149error:
150 rcu_read_unlock();
151 return inode;
152}
153
149b9a9d 154int lttng_uprobes_register(const char *name,
56377c91 155 int fd,
149b9a9d
YB
156 uint64_t offset,
157 struct lttng_event *event)
158{
159 int ret;
56377c91 160 struct inode *inode;
149b9a9d
YB
161
162 ret = lttng_create_uprobe_event(name, event);
163 if (ret)
164 goto error;
165
56377c91
FD
166 inode = get_inode_from_fd(fd);
167 if (!inode) {
168 printk(KERN_WARNING "Cannot get inode from fd\n");
169 ret = -EBADF;
170 goto inode_error;
171 }
172
149b9a9d
YB
173 memset(&event->u.uprobe.up_consumer, 0,
174 sizeof(event->u.uprobe.up_consumer));
175
176 event->u.uprobe.up_consumer.handler = lttng_uprobes_handler_pre;
56377c91 177 event->u.uprobe.inode = inode;
149b9a9d
YB
178 event->u.uprobe.offset = offset;
179
180 /* Ensure the memory we just allocated don't trigger page faults. */
181 wrapper_vmalloc_sync_all();
149b9a9d
YB
182 ret = wrapper_uprobe_register(event->u.uprobe.inode,
183 event->u.uprobe.offset,
184 &event->u.uprobe.up_consumer);
185 if (ret) {
186 printk(KERN_WARNING "Error registering probe on inode %lu "
187 "and offset %llu\n", event->u.uprobe.inode->i_ino,
188 event->u.uprobe.offset);
189 goto register_error;
190 }
191 return 0;
192
193register_error:
194 iput(event->u.uprobe.inode);
56377c91 195inode_error:
149b9a9d
YB
196 kfree(event->desc->name);
197 kfree(event->desc);
198error:
199 return ret;
200}
201EXPORT_SYMBOL_GPL(lttng_uprobes_register);
202
203void lttng_uprobes_unregister(struct lttng_event *event)
204{
205 wrapper_uprobe_unregister(event->u.uprobe.inode,
206 event->u.uprobe.offset,
207 &event->u.uprobe.up_consumer);
208}
209EXPORT_SYMBOL_GPL(lttng_uprobes_unregister);
210
211void lttng_uprobes_destroy_private(struct lttng_event *event)
212{
213 iput(event->u.uprobe.inode);
214 kfree(event->desc->name);
215 kfree(event->desc);
216}
217EXPORT_SYMBOL_GPL(lttng_uprobes_destroy_private);
218
219MODULE_LICENSE("GPL and additional rights");
220MODULE_AUTHOR("Yannick Brosseau");
221MODULE_DESCRIPTION("Linux Trace Toolkit Uprobes Support");
This page took 0.030726 seconds and 4 git commands to generate.