340f7ddc1d685cdace58f4357eece818398e587b
[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/module.h>
26 #include <linux/debugfs.h>
27 #include <linux/anon_inodes.h>
28 #include <linux/file.h>
29 #include <linux/uaccess.h>
30 #include <linux/slab.h>
31 #include <linux/ringbuffer/vfs.h>
32 #include "ltt-debugfs-abi.h"
33 #include "ltt-events.h"
34
35 /*
36 * This is LTTng's own personal way to create a system call as an external
37 * module. We use ioctl() on /sys/kernel/debug/lttng.
38 */
39
40 static struct dentry *lttng_dentry;
41 static const struct file_operations lttng_fops;
42 static const struct file_operations lttng_session_fops;
43 static const struct file_operations lttng_channel_fops;
44 static const struct file_operations lttng_metadata_fops;
45 static const struct file_operations lttng_event_fops;
46
47 enum channel_type {
48 PER_CPU_CHANNEL,
49 GLOBAL_CHANNEL,
50 METADATA_CHANNEL,
51 };
52
53 static
54 int lttng_abi_create_session(void)
55 {
56 struct ltt_session *session;
57 struct file *session_file;
58 int session_fd, ret;
59
60 session = ltt_session_create();
61 if (!session)
62 return -ENOMEM;
63 session_fd = get_unused_fd();
64 if (session_fd < 0) {
65 ret = session_fd;
66 goto fd_error;
67 }
68 session_file = anon_inode_getfile("[lttng_session]",
69 &lttng_session_fops,
70 session, O_RDWR);
71 if (IS_ERR(session_file)) {
72 ret = PTR_ERR(session_file);
73 goto file_error;
74 }
75 session->file = session_file;
76 fd_install(session_fd, session_file);
77 return session_fd;
78
79 file_error:
80 put_unused_fd(session_fd);
81 fd_error:
82 ltt_session_destroy(session);
83 return ret;
84 }
85
86 /**
87 * lttng_ioctl - lttng syscall through ioctl
88 *
89 * @file: the file
90 * @cmd: the command
91 * @arg: command arg
92 *
93 * This ioctl implements lttng commands:
94 * LTTNG_SESSION
95 * Returns a LTTng trace session file descriptor
96 *
97 * The returned session will be deleted when its file descriptor is closed.
98 */
99 static
100 long lttng_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
101 {
102 switch (cmd) {
103 case LTTNG_SESSION:
104 return lttng_abi_create_session();
105 default:
106 return -ENOIOCTLCMD;
107 }
108 }
109
110 static const struct file_operations lttng_fops = {
111 .unlocked_ioctl = lttng_ioctl,
112 #ifdef CONFIG_COMPAT
113 .compat_ioctl = lttng_ioctl,
114 #endif
115 };
116
117 /*
118 * We tolerate no failure in this function (if one happens, we print a dmesg
119 * error, but cannot return any error, because the channel information is
120 * invariant.
121 */
122 static
123 void lttng_metadata_create_events(struct file *channel_file)
124 {
125 struct ltt_channel *channel = channel_file->private_data;
126 char *event_name = "lttng-metadata";
127 struct ltt_event *event;
128 int ret;
129 void *probe;
130
131 probe = ltt_probe_get(event_name);
132 if (!probe) {
133 ret = -ENOENT;
134 goto probe_error;
135 }
136 /*
137 * We tolerate no failure path after event creation. It will stay
138 * invariant for the rest of the session.
139 */
140 event = ltt_event_create(channel, event_name, INSTRUM_TRACEPOINTS,
141 probe, NULL);
142 if (!event) {
143 goto event_error;
144 ret = -EEXIST;
145 }
146 return;
147
148 event_error:
149 ltt_probe_put(probe);
150 probe_error:
151 WARN_ON(1);
152 return; /* not allowed to return error */
153 }
154
155 static
156 int lttng_abi_create_channel(struct file *session_file,
157 struct lttng_channel __user *uchan_param,
158 enum channel_type channel_type)
159 {
160 struct ltt_session *session = session_file->private_data;
161 const struct file_operations *fops;
162 const char *transport_name;
163 struct ltt_channel *chan;
164 struct file *chan_file;
165 struct lttng_channel chan_param;
166 int chan_fd;
167 int ret = 0;
168
169 if (copy_from_user(&chan_param, uchan_param, sizeof(chan_param)))
170 return -EFAULT;
171 chan_fd = get_unused_fd();
172 if (chan_fd < 0) {
173 ret = chan_fd;
174 goto fd_error;
175 }
176 chan_file = anon_inode_getfile("[lttng_channel]",
177 &lttng_channel_fops,
178 NULL, O_RDWR);
179 if (IS_ERR(chan_file)) {
180 ret = PTR_ERR(chan_file);
181 goto file_error;
182 }
183 switch (channel_type) {
184 case PER_CPU_CHANNEL:
185 transport_name = chan_param.overwrite ?
186 "relay-overwrite" : "relay-discard";
187 fops = &lttng_channel_fops;
188 break;
189 case GLOBAL_CHANNEL:
190 transport_name = chan_param.overwrite ?
191 "global-relay-overwrite" : "global-relay-discard";
192 fops = &lttng_channel_fops;
193 break;
194 case METADATA_CHANNEL:
195 transport_name = "global-relay-discard";
196 fops = &lttng_metadata_fops;
197 break;
198 default:
199 transport_name = "<unknown>";
200 break;
201 }
202 /*
203 * We tolerate no failure path after channel creation. It will stay
204 * invariant for the rest of the session.
205 */
206 chan = ltt_channel_create(session, transport_name, NULL,
207 chan_param.subbuf_size,
208 chan_param.num_subbuf,
209 chan_param.switch_timer_interval,
210 chan_param.read_timer_interval);
211 if (!chan) {
212 ret = -EINVAL;
213 goto chan_error;
214 }
215 chan->file = chan_file;
216 chan_file->private_data = chan;
217 fd_install(chan_fd, chan_file);
218 if (channel_type == METADATA_CHANNEL)
219 lttng_metadata_create_events(chan_file);
220
221 /* The channel created holds a reference on the session */
222 atomic_long_inc(&session_file->f_count);
223
224 return chan_fd;
225
226 chan_error:
227 fput(chan_file);
228 file_error:
229 put_unused_fd(chan_fd);
230 fd_error:
231 return ret;
232 }
233
234 /**
235 * lttng_session_ioctl - lttng session fd ioctl
236 *
237 * @file: the file
238 * @cmd: the command
239 * @arg: command arg
240 *
241 * This ioctl implements lttng commands:
242 * LTTNG_CHANNEL
243 * Returns a LTTng channel file descriptor
244 *
245 * The returned channel will be deleted when its file descriptor is closed.
246 */
247 static
248 long lttng_session_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
249 {
250 struct ltt_session *session = file->private_data;
251
252 switch (cmd) {
253 case LTTNG_CHANNEL:
254 return lttng_abi_create_channel(file,
255 (struct lttng_channel __user *)arg,
256 PER_CPU_CHANNEL);
257 case LTTNG_SESSION_START:
258 return ltt_session_start(session);
259 case LTTNG_SESSION_STOP:
260 return ltt_session_stop(session);
261 case LTTNG_METADATA:
262 return lttng_abi_create_channel(file,
263 (struct lttng_channel __user *)arg,
264 METADATA_CHANNEL);
265 default:
266 return -ENOIOCTLCMD;
267 }
268 }
269
270 /*
271 * Called when the last file reference is dropped.
272 *
273 * Big fat note: channels and events are invariant for the whole session after
274 * their creation. So this session destruction also destroys all channel and
275 * event structures specific to this session (they are not destroyed when their
276 * individual file is released).
277 */
278 static
279 int lttng_session_release(struct inode *inode, struct file *file)
280 {
281 struct ltt_session *session = file->private_data;
282
283 if (session)
284 ltt_session_destroy(session);
285 return 0;
286 }
287
288 static const struct file_operations lttng_session_fops = {
289 .release = lttng_session_release,
290 .unlocked_ioctl = lttng_session_ioctl,
291 #ifdef CONFIG_COMPAT
292 .compat_ioctl = lttng_session_ioctl,
293 #endif
294 };
295
296 static
297 int lttng_abi_open_stream(struct file *channel_file)
298 {
299 struct ltt_channel *channel = channel_file->private_data;
300 struct lib_ring_buffer *buf;
301 int stream_fd, ret;
302 struct file *stream_file;
303
304 buf = channel->ops->buffer_read_open(channel->chan);
305 if (!buf)
306 return -ENOENT;
307
308 stream_fd = get_unused_fd();
309 if (stream_fd < 0) {
310 ret = stream_fd;
311 goto fd_error;
312 }
313 stream_file = anon_inode_getfile("[lttng_stream]",
314 &lib_ring_buffer_file_operations,
315 buf, O_RDWR);
316 if (IS_ERR(stream_file)) {
317 ret = PTR_ERR(stream_file);
318 goto file_error;
319 }
320 /*
321 * OPEN_FMODE, called within anon_inode_getfile/alloc_file, don't honor
322 * FMODE_LSEEK, FMODE_PREAD nor FMODE_PWRITE. We need to read from this
323 * file descriptor, so we set FMODE_PREAD here.
324 */
325 stream_file->f_mode |= FMODE_PREAD;
326 fd_install(stream_fd, stream_file);
327 /*
328 * The stream holds a reference to the channel within the generic ring
329 * buffer library, so no need to hold a refcount on the channel and
330 * session files here.
331 */
332 return stream_fd;
333
334 file_error:
335 put_unused_fd(stream_fd);
336 fd_error:
337 channel->ops->buffer_read_close(buf);
338 return ret;
339 }
340
341 static
342 int lttng_abi_create_event(struct file *channel_file,
343 struct lttng_event __user *uevent_param)
344 {
345 struct ltt_channel *channel = channel_file->private_data;
346 struct ltt_event *event;
347 char *event_name;
348 struct lttng_event event_param;
349 int event_fd, ret;
350 struct file *event_file;
351 void *probe;
352
353 if (copy_from_user(&event_param, uevent_param, sizeof(event_param)))
354 return -EFAULT;
355 event_name = kmalloc(PATH_MAX, GFP_KERNEL);
356 if (!event_name)
357 return -ENOMEM;
358 if (strncpy_from_user(event_name, uevent_param->name, PATH_MAX) < 0) {
359 ret = -EFAULT;
360 goto name_error;
361 }
362 event_name[PATH_MAX - 1] = '\0';
363
364 probe = ltt_probe_get(event_name);
365 if (!probe) {
366 ret = -ENOENT;
367 goto probe_error;
368 }
369 event_fd = get_unused_fd();
370 if (event_fd < 0) {
371 ret = event_fd;
372 goto fd_error;
373 }
374 event_file = anon_inode_getfile("[lttng_event]",
375 &lttng_event_fops,
376 NULL, O_RDWR);
377 if (IS_ERR(event_file)) {
378 ret = PTR_ERR(event_file);
379 goto file_error;
380 }
381 /*
382 * We tolerate no failure path after event creation. It will stay
383 * invariant for the rest of the session.
384 */
385 event = ltt_event_create(channel, event_name, event_param.itype,
386 probe, NULL);
387 if (!event) {
388 goto event_error;
389 ret = -EEXIST;
390 }
391 event_file->private_data = event;
392 fd_install(event_fd, event_file);
393 /* The event holds a reference on the channel */
394 atomic_long_inc(&channel_file->f_count);
395 kfree(event_name);
396 return event_fd;
397
398 event_error:
399 fput(event_file);
400 file_error:
401 put_unused_fd(event_fd);
402 fd_error:
403 ltt_probe_put(probe);
404 probe_error:
405 name_error:
406 kfree(event_name);
407 return ret;
408 }
409
410 /**
411 * lttng_channel_ioctl - lttng syscall through ioctl
412 *
413 * @file: the file
414 * @cmd: the command
415 * @arg: command arg
416 *
417 * This ioctl implements lttng commands:
418 * LTTNG_STREAM
419 * Returns an event stream file descriptor or failure.
420 * (typically, one event stream records events from one CPU)
421 * LTTNG_EVENT
422 * Returns an event file descriptor or failure.
423 *
424 * Channel and event file descriptors also hold a reference on the session.
425 */
426 static
427 long lttng_channel_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
428 {
429 switch (cmd) {
430 case LTTNG_STREAM:
431 return lttng_abi_open_stream(file);
432 case LTTNG_EVENT:
433 return lttng_abi_create_event(file, (struct lttng_event __user *)arg);
434 default:
435 return -ENOIOCTLCMD;
436 }
437 }
438
439 /**
440 * lttng_metadata_ioctl - lttng syscall through ioctl
441 *
442 * @file: the file
443 * @cmd: the command
444 * @arg: command arg
445 *
446 * This ioctl implements lttng commands:
447 * LTTNG_STREAM
448 * Returns an event stream file descriptor or failure.
449 *
450 * Channel and event file descriptors also hold a reference on the session.
451 */
452 static
453 long lttng_metadata_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
454 {
455 switch (cmd) {
456 case LTTNG_STREAM:
457 return lttng_abi_open_stream(file);
458 default:
459 return -ENOIOCTLCMD;
460 }
461 }
462
463 /* TODO: poll */
464 #if 0
465 /**
466 * lttng_channel_poll - lttng stream addition/removal monitoring
467 *
468 * @file: the file
469 * @wait: poll table
470 */
471 unsigned int lttng_channel_poll(struct file *file, poll_table *wait)
472 {
473 struct ltt_channel *channel = file->private_data;
474 unsigned int mask = 0;
475
476 if (file->f_mode & FMODE_READ) {
477 poll_wait_set_exclusive(wait);
478 poll_wait(file, &channel->notify_wait, wait);
479
480 /* TODO: identify when the channel is being finalized. */
481 if (finalized)
482 return POLLHUP;
483 else
484 return POLLIN | POLLRDNORM;
485 }
486 return mask;
487
488 }
489 #endif //0
490
491 static
492 int lttng_channel_release(struct inode *inode, struct file *file)
493 {
494 struct ltt_channel *channel = file->private_data;
495
496 if (channel)
497 fput(channel->session->file);
498 return 0;
499 }
500
501 static const struct file_operations lttng_channel_fops = {
502 .release = lttng_channel_release,
503 /* TODO */
504 #if 0
505 .poll = lttng_channel_poll,
506 #endif //0
507 .unlocked_ioctl = lttng_channel_ioctl,
508 #ifdef CONFIG_COMPAT
509 .compat_ioctl = lttng_channel_ioctl,
510 #endif
511 };
512
513 static const struct file_operations lttng_metadata_fops = {
514 .release = lttng_channel_release,
515 .unlocked_ioctl = lttng_metadata_ioctl,
516 #ifdef CONFIG_COMPAT
517 .compat_ioctl = lttng_metadata_ioctl,
518 #endif
519 };
520
521 static
522 int lttng_event_release(struct inode *inode, struct file *file)
523 {
524 struct ltt_event *event = file->private_data;
525
526 if (event)
527 fput(event->chan->file);
528 return 0;
529 }
530
531 /* TODO: filter control ioctl */
532 static const struct file_operations lttng_event_fops = {
533 .release = lttng_event_release,
534 };
535
536 int __init ltt_debugfs_abi_init(void)
537 {
538 int ret = 0;
539
540 lttng_dentry = debugfs_create_file("lttng", S_IWUSR, NULL, NULL,
541 &lttng_fops);
542 if (IS_ERR(lttng_dentry) || !lttng_dentry) {
543 printk(KERN_ERR "Error creating LTTng control file\n");
544 ret = -ENOMEM;
545 goto error;
546 }
547 error:
548 return ret;
549 }
550
551 void __exit ltt_debugfs_abi_exit(void)
552 {
553 debugfs_remove(lttng_dentry);
554 }
This page took 0.040009 seconds and 3 git commands to generate.