Update ABI to select the output method
[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 * Dual LGPL v2.1/GPL v2 license.
25 */
26
27 #include <linux/module.h>
28 #include <linux/debugfs.h>
29 #include <linux/anon_inodes.h>
30 #include <linux/file.h>
31 #include <linux/uaccess.h>
32 #include <linux/slab.h>
33 #include "wrapper/vmalloc.h" /* for wrapper_vmalloc_sync_all() */
34 #include "wrapper/ringbuffer/vfs.h"
35 #include "wrapper/poll.h"
36 #include "ltt-debugfs-abi.h"
37 #include "ltt-events.h"
38 #include "ltt-tracer.h"
39
40 /*
41 * This is LTTng's own personal way to create a system call as an external
42 * module. We use ioctl() on /sys/kernel/debug/lttng.
43 */
44
45 static struct dentry *lttng_dentry;
46 static const struct file_operations lttng_fops;
47 static const struct file_operations lttng_session_fops;
48 static const struct file_operations lttng_channel_fops;
49 static const struct file_operations lttng_metadata_fops;
50 static const struct file_operations lttng_event_fops;
51
52 enum channel_type {
53 PER_CPU_CHANNEL,
54 METADATA_CHANNEL,
55 };
56
57 static
58 int lttng_abi_create_session(void)
59 {
60 struct ltt_session *session;
61 struct file *session_file;
62 int session_fd, ret;
63
64 session = ltt_session_create();
65 if (!session)
66 return -ENOMEM;
67 session_fd = get_unused_fd();
68 if (session_fd < 0) {
69 ret = session_fd;
70 goto fd_error;
71 }
72 session_file = anon_inode_getfile("[lttng_session]",
73 &lttng_session_fops,
74 session, O_RDWR);
75 if (IS_ERR(session_file)) {
76 ret = PTR_ERR(session_file);
77 goto file_error;
78 }
79 session->file = session_file;
80 fd_install(session_fd, session_file);
81 return session_fd;
82
83 file_error:
84 put_unused_fd(session_fd);
85 fd_error:
86 ltt_session_destroy(session);
87 return ret;
88 }
89
90 static
91 int lttng_abi_tracepoint_list(void)
92 {
93 struct file *tracepoint_list_file;
94 int file_fd, ret;
95
96 file_fd = get_unused_fd();
97 if (file_fd < 0) {
98 ret = file_fd;
99 goto fd_error;
100 }
101
102 tracepoint_list_file = anon_inode_getfile("[lttng_session]",
103 &lttng_tracepoint_list_fops,
104 NULL, O_RDWR);
105 if (IS_ERR(tracepoint_list_file)) {
106 ret = PTR_ERR(tracepoint_list_file);
107 goto file_error;
108 }
109 ret = lttng_tracepoint_list_fops.open(NULL, tracepoint_list_file);
110 if (ret < 0)
111 goto open_error;
112 fd_install(file_fd, tracepoint_list_file);
113 if (file_fd < 0) {
114 ret = file_fd;
115 goto fd_error;
116 }
117 return file_fd;
118
119 open_error:
120 fput(tracepoint_list_file);
121 file_error:
122 put_unused_fd(file_fd);
123 fd_error:
124 return ret;
125 }
126
127 static
128 long lttng_abi_tracer_version(struct file *file,
129 struct lttng_kernel_tracer_version __user *uversion_param)
130 {
131 struct lttng_kernel_tracer_version v;
132
133 v.version = LTTNG_VERSION;
134 v.patchlevel = LTTNG_PATCHLEVEL;
135 v.sublevel = LTTNG_SUBLEVEL;
136
137 if (copy_to_user(uversion_param, &v, sizeof(v)))
138 return -EFAULT;
139 return 0;
140 }
141
142 static
143 long lttng_abi_add_context(struct file *file,
144 struct lttng_kernel_context __user *ucontext_param,
145 struct lttng_ctx **ctx, struct ltt_session *session)
146 {
147 struct lttng_kernel_context context_param;
148
149 if (session->been_active)
150 return -EPERM;
151
152 if (copy_from_user(&context_param, ucontext_param, sizeof(context_param)))
153 return -EFAULT;
154
155 switch (context_param.ctx) {
156 case LTTNG_KERNEL_CONTEXT_PID:
157 return lttng_add_pid_to_ctx(ctx);
158 case LTTNG_KERNEL_CONTEXT_PRIO:
159 return lttng_add_prio_to_ctx(ctx);
160 case LTTNG_KERNEL_CONTEXT_NICE:
161 return lttng_add_nice_to_ctx(ctx);
162 case LTTNG_KERNEL_CONTEXT_VPID:
163 return lttng_add_vpid_to_ctx(ctx);
164 case LTTNG_KERNEL_CONTEXT_TID:
165 return lttng_add_tid_to_ctx(ctx);
166 case LTTNG_KERNEL_CONTEXT_VTID:
167 return lttng_add_vtid_to_ctx(ctx);
168 case LTTNG_KERNEL_CONTEXT_PPID:
169 return lttng_add_ppid_to_ctx(ctx);
170 case LTTNG_KERNEL_CONTEXT_VPPID:
171 return lttng_add_vppid_to_ctx(ctx);
172 case LTTNG_KERNEL_CONTEXT_PERF_COUNTER:
173 context_param.u.perf_counter.name[LTTNG_SYM_NAME_LEN - 1] = '\0';
174 return lttng_add_perf_counter_to_ctx(context_param.u.perf_counter.type,
175 context_param.u.perf_counter.config,
176 context_param.u.perf_counter.name,
177 ctx);
178 case LTTNG_KERNEL_CONTEXT_COMM:
179 return lttng_add_comm_to_ctx(ctx);
180 default:
181 return -EINVAL;
182 }
183 }
184
185 /**
186 * lttng_ioctl - lttng syscall through ioctl
187 *
188 * @file: the file
189 * @cmd: the command
190 * @arg: command arg
191 *
192 * This ioctl implements lttng commands:
193 * LTTNG_KERNEL_SESSION
194 * Returns a LTTng trace session file descriptor
195 * LTTNG_KERNEL_TRACER_VERSION
196 * Returns the LTTng kernel tracer version
197 * LTTNG_KERNEL_TRACEPOINT_LIST
198 * Returns a file descriptor listing available tracepoints
199 * LTTNG_KERNEL_WAIT_QUIESCENT
200 * Returns after all previously running probes have completed
201 *
202 * The returned session will be deleted when its file descriptor is closed.
203 */
204 static
205 long lttng_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
206 {
207 switch (cmd) {
208 case LTTNG_KERNEL_SESSION:
209 return lttng_abi_create_session();
210 case LTTNG_KERNEL_TRACER_VERSION:
211 return lttng_abi_tracer_version(file,
212 (struct lttng_kernel_tracer_version __user *) arg);
213 case LTTNG_KERNEL_TRACEPOINT_LIST:
214 return lttng_abi_tracepoint_list();
215 case LTTNG_KERNEL_WAIT_QUIESCENT:
216 synchronize_trace();
217 return 0;
218 default:
219 return -ENOIOCTLCMD;
220 }
221 }
222
223 static const struct file_operations lttng_fops = {
224 .unlocked_ioctl = lttng_ioctl,
225 #ifdef CONFIG_COMPAT
226 .compat_ioctl = lttng_ioctl,
227 #endif
228 };
229
230 /*
231 * We tolerate no failure in this function (if one happens, we print a dmesg
232 * error, but cannot return any error, because the channel information is
233 * invariant.
234 */
235 static
236 void lttng_metadata_create_events(struct file *channel_file)
237 {
238 struct ltt_channel *channel = channel_file->private_data;
239 static struct lttng_kernel_event metadata_params = {
240 .instrumentation = LTTNG_KERNEL_TRACEPOINT,
241 .name = "lttng_metadata",
242 };
243 struct ltt_event *event;
244 int ret;
245
246 /*
247 * We tolerate no failure path after event creation. It will stay
248 * invariant for the rest of the session.
249 */
250 event = ltt_event_create(channel, &metadata_params, NULL);
251 if (!event) {
252 ret = -EINVAL;
253 goto create_error;
254 }
255 return;
256
257 create_error:
258 WARN_ON(1);
259 return; /* not allowed to return error */
260 }
261
262 static
263 int lttng_abi_create_channel(struct file *session_file,
264 struct lttng_kernel_channel __user *uchan_param,
265 enum channel_type channel_type)
266 {
267 struct ltt_session *session = session_file->private_data;
268 const struct file_operations *fops;
269 const char *transport_name;
270 struct ltt_channel *chan;
271 struct file *chan_file;
272 struct lttng_kernel_channel chan_param;
273 int chan_fd;
274 int ret = 0;
275
276 if (copy_from_user(&chan_param, uchan_param, sizeof(chan_param)))
277 return -EFAULT;
278 chan_fd = get_unused_fd();
279 if (chan_fd < 0) {
280 ret = chan_fd;
281 goto fd_error;
282 }
283 chan_file = anon_inode_getfile("[lttng_channel]",
284 &lttng_channel_fops,
285 NULL, O_RDWR);
286 if (IS_ERR(chan_file)) {
287 ret = PTR_ERR(chan_file);
288 goto file_error;
289 }
290 switch (channel_type) {
291 case PER_CPU_CHANNEL:
292 if (chan_param.output == LTTNG_KERNEL_SPLICE) {
293 transport_name = chan_param.overwrite ?
294 "relay-overwrite" : "relay-discard";
295 } else if (chan_param.output == LTTNG_KERNEL_MMAP) {
296 transport_name = chan_param.overwrite ?
297 "relay-overwrite-mmap" : "relay-discard-mmap";
298 } else {
299 return -EINVAL;
300 }
301 fops = &lttng_channel_fops;
302 break;
303 case METADATA_CHANNEL:
304 if (chan_param.output == LTTNG_KERNEL_SPLICE)
305 transport_name = "relay-metadata";
306 else if (chan_param.output == LTTNG_KERNEL_MMAP)
307 transport_name = "relay-metadata-mmap";
308 else
309 return -EINVAL;
310 fops = &lttng_metadata_fops;
311 break;
312 default:
313 transport_name = "<unknown>";
314 break;
315 }
316 /*
317 * We tolerate no failure path after channel creation. It will stay
318 * invariant for the rest of the session.
319 */
320 chan = ltt_channel_create(session, transport_name, NULL,
321 chan_param.subbuf_size,
322 chan_param.num_subbuf,
323 chan_param.switch_timer_interval,
324 chan_param.read_timer_interval);
325 if (!chan) {
326 ret = -EINVAL;
327 goto chan_error;
328 }
329 chan->file = chan_file;
330 chan_file->private_data = chan;
331 fd_install(chan_fd, chan_file);
332 if (channel_type == METADATA_CHANNEL) {
333 session->metadata = chan;
334 lttng_metadata_create_events(chan_file);
335 }
336
337 /* The channel created holds a reference on the session */
338 atomic_long_inc(&session_file->f_count);
339
340 return chan_fd;
341
342 chan_error:
343 fput(chan_file);
344 file_error:
345 put_unused_fd(chan_fd);
346 fd_error:
347 return ret;
348 }
349
350 /**
351 * lttng_session_ioctl - lttng session fd ioctl
352 *
353 * @file: the file
354 * @cmd: the command
355 * @arg: command arg
356 *
357 * This ioctl implements lttng commands:
358 * LTTNG_KERNEL_CHANNEL
359 * Returns a LTTng channel file descriptor
360 * LTTNG_KERNEL_ENABLE
361 * Enables tracing for a session (weak enable)
362 * LTTNG_KERNEL_DISABLE
363 * Disables tracing for a session (strong disable)
364 * LTTNG_KERNEL_METADATA
365 * Returns a LTTng metadata file descriptor
366 *
367 * The returned channel will be deleted when its file descriptor is closed.
368 */
369 static
370 long lttng_session_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
371 {
372 struct ltt_session *session = file->private_data;
373
374 switch (cmd) {
375 case LTTNG_KERNEL_CHANNEL:
376 return lttng_abi_create_channel(file,
377 (struct lttng_kernel_channel __user *) arg,
378 PER_CPU_CHANNEL);
379 case LTTNG_KERNEL_SESSION_START:
380 case LTTNG_KERNEL_ENABLE:
381 return ltt_session_enable(session);
382 case LTTNG_KERNEL_SESSION_STOP:
383 case LTTNG_KERNEL_DISABLE:
384 return ltt_session_disable(session);
385 case LTTNG_KERNEL_METADATA:
386 return lttng_abi_create_channel(file,
387 (struct lttng_kernel_channel __user *) arg,
388 METADATA_CHANNEL);
389 default:
390 return -ENOIOCTLCMD;
391 }
392 }
393
394 /*
395 * Called when the last file reference is dropped.
396 *
397 * Big fat note: channels and events are invariant for the whole session after
398 * their creation. So this session destruction also destroys all channel and
399 * event structures specific to this session (they are not destroyed when their
400 * individual file is released).
401 */
402 static
403 int lttng_session_release(struct inode *inode, struct file *file)
404 {
405 struct ltt_session *session = file->private_data;
406
407 if (session)
408 ltt_session_destroy(session);
409 return 0;
410 }
411
412 static const struct file_operations lttng_session_fops = {
413 .release = lttng_session_release,
414 .unlocked_ioctl = lttng_session_ioctl,
415 #ifdef CONFIG_COMPAT
416 .compat_ioctl = lttng_session_ioctl,
417 #endif
418 };
419
420 static
421 int lttng_abi_open_stream(struct file *channel_file)
422 {
423 struct ltt_channel *channel = channel_file->private_data;
424 struct lib_ring_buffer *buf;
425 int stream_fd, ret;
426 struct file *stream_file;
427
428 buf = channel->ops->buffer_read_open(channel->chan);
429 if (!buf)
430 return -ENOENT;
431
432 stream_fd = get_unused_fd();
433 if (stream_fd < 0) {
434 ret = stream_fd;
435 goto fd_error;
436 }
437 stream_file = anon_inode_getfile("[lttng_stream]",
438 &lib_ring_buffer_file_operations,
439 buf, O_RDWR);
440 if (IS_ERR(stream_file)) {
441 ret = PTR_ERR(stream_file);
442 goto file_error;
443 }
444 /*
445 * OPEN_FMODE, called within anon_inode_getfile/alloc_file, don't honor
446 * FMODE_LSEEK, FMODE_PREAD nor FMODE_PWRITE. We need to read from this
447 * file descriptor, so we set FMODE_PREAD here.
448 */
449 stream_file->f_mode |= FMODE_PREAD;
450 fd_install(stream_fd, stream_file);
451 /*
452 * The stream holds a reference to the channel within the generic ring
453 * buffer library, so no need to hold a refcount on the channel and
454 * session files here.
455 */
456 return stream_fd;
457
458 file_error:
459 put_unused_fd(stream_fd);
460 fd_error:
461 channel->ops->buffer_read_close(buf);
462 return ret;
463 }
464
465 static
466 int lttng_abi_create_event(struct file *channel_file,
467 struct lttng_kernel_event __user *uevent_param)
468 {
469 struct ltt_channel *channel = channel_file->private_data;
470 struct ltt_event *event;
471 struct lttng_kernel_event event_param;
472 int event_fd, ret;
473 struct file *event_file;
474
475 if (copy_from_user(&event_param, uevent_param, sizeof(event_param)))
476 return -EFAULT;
477 event_param.name[LTTNG_SYM_NAME_LEN - 1] = '\0';
478 switch (event_param.instrumentation) {
479 case LTTNG_KERNEL_KPROBE:
480 event_param.u.kprobe.symbol_name[LTTNG_SYM_NAME_LEN - 1] = '\0';
481 break;
482 case LTTNG_KERNEL_FUNCTION:
483 event_param.u.ftrace.symbol_name[LTTNG_SYM_NAME_LEN - 1] = '\0';
484 break;
485 default:
486 break;
487 }
488 event_fd = get_unused_fd();
489 if (event_fd < 0) {
490 ret = event_fd;
491 goto fd_error;
492 }
493 event_file = anon_inode_getfile("[lttng_event]",
494 &lttng_event_fops,
495 NULL, O_RDWR);
496 if (IS_ERR(event_file)) {
497 ret = PTR_ERR(event_file);
498 goto file_error;
499 }
500 /*
501 * We tolerate no failure path after event creation. It will stay
502 * invariant for the rest of the session.
503 */
504 event = ltt_event_create(channel, &event_param, NULL);
505 if (!event) {
506 ret = -EINVAL;
507 goto event_error;
508 }
509 event_file->private_data = event;
510 fd_install(event_fd, event_file);
511 /* The event holds a reference on the channel */
512 atomic_long_inc(&channel_file->f_count);
513 return event_fd;
514
515 event_error:
516 fput(event_file);
517 file_error:
518 put_unused_fd(event_fd);
519 fd_error:
520 return ret;
521 }
522
523 /**
524 * lttng_channel_ioctl - lttng syscall through ioctl
525 *
526 * @file: the file
527 * @cmd: the command
528 * @arg: command arg
529 *
530 * This ioctl implements lttng commands:
531 * LTTNG_KERNEL_STREAM
532 * Returns an event stream file descriptor or failure.
533 * (typically, one event stream records events from one CPU)
534 * LTTNG_KERNEL_EVENT
535 * Returns an event file descriptor or failure.
536 * LTTNG_KERNEL_CONTEXT
537 * Prepend a context field to each event in the channel
538 * LTTNG_KERNEL_ENABLE
539 * Enable recording for events in this channel (weak enable)
540 * LTTNG_KERNEL_DISABLE
541 * Disable recording for events in this channel (strong disable)
542 *
543 * Channel and event file descriptors also hold a reference on the session.
544 */
545 static
546 long lttng_channel_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
547 {
548 struct ltt_channel *channel = file->private_data;
549
550 switch (cmd) {
551 case LTTNG_KERNEL_STREAM:
552 return lttng_abi_open_stream(file);
553 case LTTNG_KERNEL_EVENT:
554 return lttng_abi_create_event(file, (struct lttng_kernel_event __user *) arg);
555 case LTTNG_KERNEL_CONTEXT:
556 return lttng_abi_add_context(file,
557 (struct lttng_kernel_context __user *) arg,
558 &channel->ctx, channel->session);
559 case LTTNG_KERNEL_ENABLE:
560 return ltt_channel_enable(channel);
561 case LTTNG_KERNEL_DISABLE:
562 return ltt_channel_disable(channel);
563 default:
564 return -ENOIOCTLCMD;
565 }
566 }
567
568 /**
569 * lttng_metadata_ioctl - lttng syscall through ioctl
570 *
571 * @file: the file
572 * @cmd: the command
573 * @arg: command arg
574 *
575 * This ioctl implements lttng commands:
576 * LTTNG_KERNEL_STREAM
577 * Returns an event stream file descriptor or failure.
578 *
579 * Channel and event file descriptors also hold a reference on the session.
580 */
581 static
582 long lttng_metadata_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
583 {
584 switch (cmd) {
585 case LTTNG_KERNEL_STREAM:
586 return lttng_abi_open_stream(file);
587 default:
588 return -ENOIOCTLCMD;
589 }
590 }
591
592 /**
593 * lttng_channel_poll - lttng stream addition/removal monitoring
594 *
595 * @file: the file
596 * @wait: poll table
597 */
598 unsigned int lttng_channel_poll(struct file *file, poll_table *wait)
599 {
600 struct ltt_channel *channel = file->private_data;
601 unsigned int mask = 0;
602
603 if (file->f_mode & FMODE_READ) {
604 poll_wait_set_exclusive(wait);
605 poll_wait(file, channel->ops->get_hp_wait_queue(channel->chan),
606 wait);
607
608 if (channel->ops->is_disabled(channel->chan))
609 return POLLERR;
610 if (channel->ops->is_finalized(channel->chan))
611 return POLLHUP;
612 else
613 return POLLIN | POLLRDNORM;
614 }
615 return mask;
616
617 }
618
619 static
620 int lttng_channel_release(struct inode *inode, struct file *file)
621 {
622 struct ltt_channel *channel = file->private_data;
623
624 if (channel)
625 fput(channel->session->file);
626 return 0;
627 }
628
629 static const struct file_operations lttng_channel_fops = {
630 .release = lttng_channel_release,
631 .poll = lttng_channel_poll,
632 .unlocked_ioctl = lttng_channel_ioctl,
633 #ifdef CONFIG_COMPAT
634 .compat_ioctl = lttng_channel_ioctl,
635 #endif
636 };
637
638 static const struct file_operations lttng_metadata_fops = {
639 .release = lttng_channel_release,
640 .unlocked_ioctl = lttng_metadata_ioctl,
641 #ifdef CONFIG_COMPAT
642 .compat_ioctl = lttng_metadata_ioctl,
643 #endif
644 };
645
646 /**
647 * lttng_event_ioctl - lttng syscall through ioctl
648 *
649 * @file: the file
650 * @cmd: the command
651 * @arg: command arg
652 *
653 * This ioctl implements lttng commands:
654 * LTTNG_KERNEL_CONTEXT
655 * Prepend a context field to each record of this event
656 * LTTNG_KERNEL_ENABLE
657 * Enable recording for this event (weak enable)
658 * LTTNG_KERNEL_DISABLE
659 * Disable recording for this event (strong disable)
660 */
661 static
662 long lttng_event_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
663 {
664 struct ltt_event *event = file->private_data;
665
666 switch (cmd) {
667 case LTTNG_KERNEL_CONTEXT:
668 return lttng_abi_add_context(file,
669 (struct lttng_kernel_context __user *) arg,
670 &event->ctx, event->chan->session);
671 case LTTNG_KERNEL_ENABLE:
672 return ltt_event_enable(event);
673 case LTTNG_KERNEL_DISABLE:
674 return ltt_event_disable(event);
675 default:
676 return -ENOIOCTLCMD;
677 }
678 }
679
680 static
681 int lttng_event_release(struct inode *inode, struct file *file)
682 {
683 struct ltt_event *event = file->private_data;
684
685 if (event)
686 fput(event->chan->file);
687 return 0;
688 }
689
690 /* TODO: filter control ioctl */
691 static const struct file_operations lttng_event_fops = {
692 .release = lttng_event_release,
693 .unlocked_ioctl = lttng_event_ioctl,
694 #ifdef CONFIG_COMPAT
695 .compat_ioctl = lttng_event_ioctl,
696 #endif
697 };
698
699 int __init ltt_debugfs_abi_init(void)
700 {
701 int ret = 0;
702
703 wrapper_vmalloc_sync_all();
704 lttng_dentry = debugfs_create_file("lttng", S_IWUSR, NULL, NULL,
705 &lttng_fops);
706 if (IS_ERR(lttng_dentry) || !lttng_dentry) {
707 printk(KERN_ERR "Error creating LTTng control file\n");
708 ret = -ENOMEM;
709 goto error;
710 }
711 error:
712 return ret;
713 }
714
715 void __exit ltt_debugfs_abi_exit(void)
716 {
717 debugfs_remove(lttng_dentry);
718 }
This page took 0.045303 seconds and 4 git commands to generate.