Add comment todo: event filter abi
[deliverable/lttng-modules.git] / ltt-debugfs-abi.c
1 /*
2 * ltt-debugfs-abi.c
3 *
4 * Copyright 2010 (c) - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 *
6 * LTTng debugfs ABI
7 *
8 * Mimic system calls for:
9 * - session creation, returns a file descriptor or failure.
10 * - channel creation, returns a file descriptor or failure.
11 * - Operates on a session file descriptor
12 * - Takes all channel options as parameters.
13 * - stream get, returns a file descriptor or failure.
14 * - Operates on a channel file descriptor.
15 * - stream notifier get, returns a file descriptor or failure.
16 * - Operates on a channel file descriptor.
17 * - event creation, returns a file descriptor or failure.
18 * - Operates on a channel file descriptor
19 * - Takes an event name as parameter
20 * - Takes an instrumentation source as parameter
21 * - e.g. tracepoints, dynamic_probes...
22 * - Takes instrumentation source specific arguments.
23 */
24
25 #include <linux/debugfs.h>
26 #include "ltt-events.h"
27
28 /*
29 * This is LTTng's own personal way to create a system call as an external
30 * module. We use ioctl() on /sys/kernel/debug/lttng.
31 */
32
33 static struct dentry *lttng_dentry;
34 static const struct file_operations lttng_fops;
35 static const struct file_operations lttng_session_fops;
36 static const struct file_operations lttng_channel_fops;
37 static const struct file_operations lttng_event_fops;
38
39 /*
40 * LTTng DebugFS ABI structures.
41 */
42
43 struct lttng_channel {
44 int overwrite; /* 1: overwrite, 0: discard */
45 u64 subbuf_size;
46 u64 num_subbuf;
47 unsigned int switch_timer_interval;
48 unsigned int read_timer_interval;
49 };
50
51 struct lttng_event {
52 enum instrum_type itype;
53 char name[];
54 };
55
56 static
57 int lttng_abi_create_session(void)
58 {
59 struct ltt_session *session;
60 struct file *session_filp;
61 int session_fd;
62
63 session = ltt_session_create();
64 if (!session)
65 return -ENOMEM;
66 session_fd = get_unused_fd_flags(O_RDWR);
67 if (session_fd < 0) {
68 ret = session_fd;
69 goto fd_error;
70 }
71 session_filp = anon_inode_getfile("[lttng_session]",
72 &lttng_session_fops,
73 session, O_RDWR);
74 if (IS_ERR(session_filp)) {
75 ret = PTR_ERR(session_filp);
76 goto file_error;
77 }
78 fd_install(session_fd, session_filp);
79 return session_fd;
80
81 file_error:
82 put_unused_fd(session_fd);
83 fd_error:
84 ltt_session_destroy(session);
85 return ret;
86 }
87
88 /**
89 * lttng_ioctl - lttng syscall through ioctl
90 *
91 * @filp: the file
92 * @cmd: the command
93 * @arg: command arg
94 *
95 * This ioctl implements lttng commands:
96 * LTTNG_SESSION
97 * Returns a LTTng trace session file descriptor
98 *
99 * The returned session will be deleted when its file descriptor is closed.
100 */
101 static
102 long lttng_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
103 {
104 switch (cmd) {
105 case LTTNG_SESSION:
106 return lttng_abi_create_session();
107 default:
108 return -ENOIOCTLCMD;
109 }
110 }
111
112 static const struct file_operations lttng_fops = {
113 .unlocked_ioctl = lttng_ioctl,
114 #ifdef CONFIG_COMPAT
115 .compat_ioctl = lttng_ioctl,
116 #endif
117 }
118
119 int lttng_abi_create_channel(struct file *session_filp,
120 struct lttng_channel __user *uchan_param)
121 {
122 struct ltt_session *session = session_filp->private_data;
123 struct ltt_channel *chan;
124 struct file *chan_filp;
125 struct lttng_channel chan_param;
126 int chan_fd;
127 int ret = 0;
128
129 if (copy_from_user(&chan_param, uchan_param, sizeof(chan_param)))
130 return -EFAULT;
131 chan_fd = get_unused_fd_flags(O_RDWR);
132 if (chan_fd < 0) {
133 ret = chan_fd;
134 goto fd_error;
135 }
136 chan_filp = anon_inode_getfile("[lttng_channel]",
137 &lttng_channel_fops,
138 NULL, O_RDWR);
139 if (IS_ERR(chan_filp)) {
140 ret = PTR_ERR(chan_filp);
141 goto file_error;
142 }
143 /*
144 * We tolerate no failure path after channel creation. It will stay
145 * invariant for the rest of the session.
146 */
147 chan = ltt_channel_create(session, chan_param->overwrite, NULL,
148 chan_param->subbuf_size,
149 chan_param->num_subbuf,
150 chan_param->switch_timer_interval,
151 chan_param->read_timer_interval);
152 if (!chan) {
153 ret = -ENOMEM;
154 goto chan_error;
155 }
156 chan_filp->private_data = chan;
157 fd_install(chan_fd, chan_filp);
158 /* The channel created holds a reference on the session */
159 atomic_inc(&session_filp->f_count);
160
161 return chan_fd;
162
163 chan_error:
164 fput(chan_filp);
165 file_error:
166 put_unused_fd(chan_fd);
167 fd_error:
168 return ret;
169 }
170
171 /**
172 * lttng_session_ioctl - lttng session fd ioctl
173 *
174 * @filp: the file
175 * @cmd: the command
176 * @arg: command arg
177 *
178 * This ioctl implements lttng commands:
179 * LTTNG_CHANNEL
180 * Returns a LTTng channel file descriptor
181 *
182 * The returned channel will be deleted when its file descriptor is closed.
183 */
184 static
185 long lttng_session_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
186 {
187 switch (cmd) {
188 case LTTNG_CHANNEL:
189 return lttng_abi_create_channel(filp, (struct lttng_channel __user *)arg);
190 default:
191 return -ENOIOCTLCMD;
192 }
193 }
194
195 /*
196 * Called when the last file reference is dropped.
197 *
198 * Big fat note: channels and events are invariant for the whole session after
199 * their creation. So this session destruction also destroys all channel and
200 * event structures specific to this session (they are not destroyed when their
201 * individual file is released).
202 */
203 static
204 int lttng_session_release(struct inode *inode, struct file *file)
205 {
206 struct ltt_session *session = file->private_data;
207 return ltt_session_destroy(session);
208 }
209
210 static const struct file_operations lttng_session_fops = {
211 .release = lttng_session_release,
212 .unlocked_ioctl = lttng_session_ioctl,
213 #ifdef CONFIG_COMPAT
214 .compat_ioctl = lttng_session_ioctl,
215 #endif
216 }
217
218 static
219 int lttng_abi_open_stream(struct file *channel_filp)
220 {
221 struct ltt_channel *channel = channel_filp->private_data;
222 struct lib_ring_buffer *buf;
223 int stream_fd, ret;
224
225 buf = ltt_buffer_read_open(channel->chan);
226 if (!buf)
227 return -ENOENT;
228
229 stream_fd = get_unused_fd_flags(O_RDWR);
230 if (stream_fd < 0) {
231 ret = stream_fd;
232 goto fd_error;
233 }
234 stream_filp = anon_inode_getfile("[lttng_stream]",
235 &lib_ring_buffer_file_operations,
236 buf, O_RDWR);
237 if (IS_ERR(stream_filp)) {
238 ret = PTR_ERR(stream_filp);
239 goto file_error;
240 }
241 fd_install(stream_fd, stream_filp);
242 /* The stream holds a reference on the channel */
243 atomic_inc(&channel_filp->f_count);
244 return stream_fd;
245
246 file_error:
247 put_unused_fd(stream_fd);
248 fd_error:
249 ltt_buffer_read_close(buf);
250 return ret;
251 }
252
253 static
254 int lttng_abi_create_event(struct file *channel_filp,
255 struct lttng_event __user *uevent_param)
256 {
257 struct ltt_channel *channel = channel_filp->private_data;
258 struct ltt_event *event;
259 char *event_name;
260 struct lttng_event event_param;
261 int event_fd, ret;
262
263 if (copy_from_user(&event_param, uevent_param, sizeof(event_param)))
264 return -EFAULT;
265 event_name = kmalloc(PATH_MAX, GFP_KERNEL);
266 if (!event_name)
267 return -ENOMEM;
268 if (strncpy_from_user(event_name, &uevent_param->name, PATH_MAX)) {
269 ret = -EFAULT;
270 goto name_error;
271 }
272 event_name[PATH_MAX - 1] = '\0';
273 event_fd = get_unused_fd_flags(O_RDWR);
274 if (event_fd < 0) {
275 ret = event_fd;
276 goto fd_error;
277 }
278 event_filp = anon_inode_getfile("[lttng_event]",
279 &lttng_event_fops, /* TODO: filter */
280 NULL, O_RDWR);
281 if (IS_ERR(event_filp)) {
282 ret = PTR_ERR(event_filp);
283 goto file_error;
284 }
285 /*
286 * We tolerate no failure path after event creation. It will stay
287 * invariant for the rest of the session.
288 */
289 event = ltt_event_create(channel, event_param->itype, event_name, NULL);
290 if (!event) {
291 goto event_error;
292 ret = -EEXIST;
293 }
294 event_filp->private_data = event;
295 fd_install(event_fd, event_filp);
296 /* The event holds a reference on the channel */
297 atomic_inc(&channel_filp->f_count);
298 kfree(event_name);
299 return event_fd;
300
301 event_error:
302 fput(event_filp);
303 file_error:
304 put_unused_fd(event_fd);
305 fd_error:
306 name_error:
307 kfree(event_name);
308 return ret;
309 }
310
311 /**
312 * lttng_channel_ioctl - lttng syscall through ioctl
313 *
314 * @filp: the file
315 * @cmd: the command
316 * @arg: command arg
317 *
318 * This ioctl implements lttng commands:
319 * LTTNG_STREAM
320 * Returns an event stream file descriptor or failure.
321 * (typically, one event stream records events from one CPU)
322 * LTTNG_EVENT
323 * Returns an event file descriptor or failure.
324 *
325 * The returned session will be deleted when its file descriptor is closed.
326 * Channel and event file descriptors also hold a reference on the session.
327 */
328 static
329 long lttng_channel_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
330 {
331 switch (cmd) {
332 case LTTNG_STREAM:
333 return lttng_abi_open_stream(filp);
334 case LTTNG_EVENT:
335 return lttng_abi_create_event(filp, (struct lttng_event __user *)arg);
336 default:
337 return -ENOIOCTLCMD;
338 }
339 }
340
341 /**
342 * lttng_channel_poll - lttng stream addition/removal monitoring
343 *
344 * @filp: the file
345 * @wait: poll table
346 */
347 unsigned int lttng_channel_poll(struct file *filp, poll_table *wait)
348 {
349 struct ltt_channel *channel = filp->private_data;
350 unsigned int mask = 0;
351
352 if (filp->f_mode & FMODE_READ) {
353 poll_wait_set_exclusive(wait);
354 poll_wait(filp, &channel->notify_wait, wait);
355
356 /* TODO: identify when the channel is being finalized. */
357 if (finalized)
358 return POLLHUP;
359 else
360 return POLLIN | POLLRDNORM;
361 }
362 return mask;
363
364 }
365
366 static const struct file_operations lttng_channel_fops = {
367 .release = lttng_channel_release,
368 .poll = lttng_channel_poll,
369 .unlocked_ioctl = lttng_channel_ioctl,
370 #ifdef CONFIG_COMPAT
371 .compat_ioctl = lttng_channel_ioctl,
372 #endif
373 }
374
375 static int __init ltt_debugfs_abi_init(void)
376 {
377 int ret = 0;
378
379 lttng_dentry = debugfs_create_file("lttng", NULL);
380 if (IS_ERR(lttng_dentry) || !lttng_dentry)
381 printk(KERN_ERR "Error creating LTTng control file\n");
382 ret = -ENOMEM;
383 goto error;
384 }
385 error:
386 return ret;
387 }
388
389 static void __exit ltt_debugfs_abi_exit(void)
390 {
391 debugfs_remote(lttng_dentry);
392 }
This page took 0.054711 seconds and 5 git commands to generate.