Add wait quiescent ioctl
[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 * 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 default:
216 return -ENOIOCTLCMD;
217 }
218 }
219
220 static const struct file_operations lttng_fops = {
221 .unlocked_ioctl = lttng_ioctl,
222 #ifdef CONFIG_COMPAT
223 .compat_ioctl = lttng_ioctl,
224 #endif
225 };
226
227 /*
228 * We tolerate no failure in this function (if one happens, we print a dmesg
229 * error, but cannot return any error, because the channel information is
230 * invariant.
231 */
232 static
233 void lttng_metadata_create_events(struct file *channel_file)
234 {
235 struct ltt_channel *channel = channel_file->private_data;
236 static struct lttng_kernel_event metadata_params = {
237 .instrumentation = LTTNG_KERNEL_TRACEPOINT,
238 .name = "lttng_metadata",
239 };
240 struct ltt_event *event;
241 int ret;
242
243 /*
244 * We tolerate no failure path after event creation. It will stay
245 * invariant for the rest of the session.
246 */
247 event = ltt_event_create(channel, &metadata_params, NULL);
248 if (!event) {
249 ret = -EINVAL;
250 goto create_error;
251 }
252 return;
253
254 create_error:
255 WARN_ON(1);
256 return; /* not allowed to return error */
257 }
258
259 static
260 int lttng_abi_create_channel(struct file *session_file,
261 struct lttng_kernel_channel __user *uchan_param,
262 enum channel_type channel_type)
263 {
264 struct ltt_session *session = session_file->private_data;
265 const struct file_operations *fops;
266 const char *transport_name;
267 struct ltt_channel *chan;
268 struct file *chan_file;
269 struct lttng_kernel_channel chan_param;
270 int chan_fd;
271 int ret = 0;
272
273 if (copy_from_user(&chan_param, uchan_param, sizeof(chan_param)))
274 return -EFAULT;
275 chan_fd = get_unused_fd();
276 if (chan_fd < 0) {
277 ret = chan_fd;
278 goto fd_error;
279 }
280 chan_file = anon_inode_getfile("[lttng_channel]",
281 &lttng_channel_fops,
282 NULL, O_RDWR);
283 if (IS_ERR(chan_file)) {
284 ret = PTR_ERR(chan_file);
285 goto file_error;
286 }
287 switch (channel_type) {
288 case PER_CPU_CHANNEL:
289 transport_name = chan_param.overwrite ?
290 "relay-overwrite" : "relay-discard";
291 fops = &lttng_channel_fops;
292 break;
293 case METADATA_CHANNEL:
294 transport_name = "relay-metadata";
295 fops = &lttng_metadata_fops;
296 break;
297 default:
298 transport_name = "<unknown>";
299 break;
300 }
301 /*
302 * We tolerate no failure path after channel creation. It will stay
303 * invariant for the rest of the session.
304 */
305 chan = ltt_channel_create(session, transport_name, NULL,
306 chan_param.subbuf_size,
307 chan_param.num_subbuf,
308 chan_param.switch_timer_interval,
309 chan_param.read_timer_interval);
310 if (!chan) {
311 ret = -EINVAL;
312 goto chan_error;
313 }
314 chan->file = chan_file;
315 chan_file->private_data = chan;
316 fd_install(chan_fd, chan_file);
317 if (channel_type == METADATA_CHANNEL) {
318 session->metadata = chan;
319 lttng_metadata_create_events(chan_file);
320 }
321
322 /* The channel created holds a reference on the session */
323 atomic_long_inc(&session_file->f_count);
324
325 return chan_fd;
326
327 chan_error:
328 fput(chan_file);
329 file_error:
330 put_unused_fd(chan_fd);
331 fd_error:
332 return ret;
333 }
334
335 /**
336 * lttng_session_ioctl - lttng session fd ioctl
337 *
338 * @file: the file
339 * @cmd: the command
340 * @arg: command arg
341 *
342 * This ioctl implements lttng commands:
343 * LTTNG_KERNEL_CHANNEL
344 * Returns a LTTng channel file descriptor
345 * LTTNG_KERNEL_ENABLE
346 * Enables tracing for a session (weak enable)
347 * LTTNG_KERNEL_DISABLE
348 * Disables tracing for a session (strong disable)
349 * LTTNG_KERNEL_METADATA
350 * Returns a LTTng metadata file descriptor
351 *
352 * The returned channel will be deleted when its file descriptor is closed.
353 */
354 static
355 long lttng_session_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
356 {
357 struct ltt_session *session = file->private_data;
358
359 switch (cmd) {
360 case LTTNG_KERNEL_CHANNEL:
361 return lttng_abi_create_channel(file,
362 (struct lttng_kernel_channel __user *) arg,
363 PER_CPU_CHANNEL);
364 case LTTNG_KERNEL_SESSION_START:
365 case LTTNG_KERNEL_ENABLE:
366 return ltt_session_enable(session);
367 case LTTNG_KERNEL_SESSION_STOP:
368 case LTTNG_KERNEL_DISABLE:
369 return ltt_session_disable(session);
370 case LTTNG_KERNEL_METADATA:
371 return lttng_abi_create_channel(file,
372 (struct lttng_kernel_channel __user *) arg,
373 METADATA_CHANNEL);
374 default:
375 return -ENOIOCTLCMD;
376 }
377 }
378
379 /*
380 * Called when the last file reference is dropped.
381 *
382 * Big fat note: channels and events are invariant for the whole session after
383 * their creation. So this session destruction also destroys all channel and
384 * event structures specific to this session (they are not destroyed when their
385 * individual file is released).
386 */
387 static
388 int lttng_session_release(struct inode *inode, struct file *file)
389 {
390 struct ltt_session *session = file->private_data;
391
392 if (session)
393 ltt_session_destroy(session);
394 return 0;
395 }
396
397 static const struct file_operations lttng_session_fops = {
398 .release = lttng_session_release,
399 .unlocked_ioctl = lttng_session_ioctl,
400 #ifdef CONFIG_COMPAT
401 .compat_ioctl = lttng_session_ioctl,
402 #endif
403 };
404
405 static
406 int lttng_abi_open_stream(struct file *channel_file)
407 {
408 struct ltt_channel *channel = channel_file->private_data;
409 struct lib_ring_buffer *buf;
410 int stream_fd, ret;
411 struct file *stream_file;
412
413 buf = channel->ops->buffer_read_open(channel->chan);
414 if (!buf)
415 return -ENOENT;
416
417 stream_fd = get_unused_fd();
418 if (stream_fd < 0) {
419 ret = stream_fd;
420 goto fd_error;
421 }
422 stream_file = anon_inode_getfile("[lttng_stream]",
423 &lib_ring_buffer_file_operations,
424 buf, O_RDWR);
425 if (IS_ERR(stream_file)) {
426 ret = PTR_ERR(stream_file);
427 goto file_error;
428 }
429 /*
430 * OPEN_FMODE, called within anon_inode_getfile/alloc_file, don't honor
431 * FMODE_LSEEK, FMODE_PREAD nor FMODE_PWRITE. We need to read from this
432 * file descriptor, so we set FMODE_PREAD here.
433 */
434 stream_file->f_mode |= FMODE_PREAD;
435 fd_install(stream_fd, stream_file);
436 /*
437 * The stream holds a reference to the channel within the generic ring
438 * buffer library, so no need to hold a refcount on the channel and
439 * session files here.
440 */
441 return stream_fd;
442
443 file_error:
444 put_unused_fd(stream_fd);
445 fd_error:
446 channel->ops->buffer_read_close(buf);
447 return ret;
448 }
449
450 static
451 int lttng_abi_create_event(struct file *channel_file,
452 struct lttng_kernel_event __user *uevent_param)
453 {
454 struct ltt_channel *channel = channel_file->private_data;
455 struct ltt_event *event;
456 struct lttng_kernel_event event_param;
457 int event_fd, ret;
458 struct file *event_file;
459
460 if (copy_from_user(&event_param, uevent_param, sizeof(event_param)))
461 return -EFAULT;
462 event_param.name[LTTNG_SYM_NAME_LEN - 1] = '\0';
463 switch (event_param.instrumentation) {
464 case LTTNG_KERNEL_KPROBE:
465 event_param.u.kprobe.symbol_name[LTTNG_SYM_NAME_LEN - 1] = '\0';
466 break;
467 case LTTNG_KERNEL_FUNCTION:
468 event_param.u.ftrace.symbol_name[LTTNG_SYM_NAME_LEN - 1] = '\0';
469 break;
470 default:
471 break;
472 }
473 event_fd = get_unused_fd();
474 if (event_fd < 0) {
475 ret = event_fd;
476 goto fd_error;
477 }
478 event_file = anon_inode_getfile("[lttng_event]",
479 &lttng_event_fops,
480 NULL, O_RDWR);
481 if (IS_ERR(event_file)) {
482 ret = PTR_ERR(event_file);
483 goto file_error;
484 }
485 /*
486 * We tolerate no failure path after event creation. It will stay
487 * invariant for the rest of the session.
488 */
489 event = ltt_event_create(channel, &event_param, NULL);
490 if (!event) {
491 ret = -EINVAL;
492 goto event_error;
493 }
494 event_file->private_data = event;
495 fd_install(event_fd, event_file);
496 /* The event holds a reference on the channel */
497 atomic_long_inc(&channel_file->f_count);
498 return event_fd;
499
500 event_error:
501 fput(event_file);
502 file_error:
503 put_unused_fd(event_fd);
504 fd_error:
505 return ret;
506 }
507
508 /**
509 * lttng_channel_ioctl - lttng syscall through ioctl
510 *
511 * @file: the file
512 * @cmd: the command
513 * @arg: command arg
514 *
515 * This ioctl implements lttng commands:
516 * LTTNG_KERNEL_STREAM
517 * Returns an event stream file descriptor or failure.
518 * (typically, one event stream records events from one CPU)
519 * LTTNG_KERNEL_EVENT
520 * Returns an event file descriptor or failure.
521 * LTTNG_KERNEL_CONTEXT
522 * Prepend a context field to each event in the channel
523 * LTTNG_KERNEL_ENABLE
524 * Enable recording for events in this channel (weak enable)
525 * LTTNG_KERNEL_DISABLE
526 * Disable recording for events in this channel (strong disable)
527 *
528 * Channel and event file descriptors also hold a reference on the session.
529 */
530 static
531 long lttng_channel_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
532 {
533 struct ltt_channel *channel = file->private_data;
534
535 switch (cmd) {
536 case LTTNG_KERNEL_STREAM:
537 return lttng_abi_open_stream(file);
538 case LTTNG_KERNEL_EVENT:
539 return lttng_abi_create_event(file, (struct lttng_kernel_event __user *) arg);
540 case LTTNG_KERNEL_CONTEXT:
541 return lttng_abi_add_context(file,
542 (struct lttng_kernel_context __user *) arg,
543 &channel->ctx, channel->session);
544 case LTTNG_KERNEL_ENABLE:
545 return ltt_channel_enable(channel);
546 case LTTNG_KERNEL_DISABLE:
547 return ltt_channel_disable(channel);
548 default:
549 return -ENOIOCTLCMD;
550 }
551 }
552
553 /**
554 * lttng_metadata_ioctl - lttng syscall through ioctl
555 *
556 * @file: the file
557 * @cmd: the command
558 * @arg: command arg
559 *
560 * This ioctl implements lttng commands:
561 * LTTNG_KERNEL_STREAM
562 * Returns an event stream file descriptor or failure.
563 *
564 * Channel and event file descriptors also hold a reference on the session.
565 */
566 static
567 long lttng_metadata_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
568 {
569 switch (cmd) {
570 case LTTNG_KERNEL_STREAM:
571 return lttng_abi_open_stream(file);
572 default:
573 return -ENOIOCTLCMD;
574 }
575 }
576
577 /**
578 * lttng_channel_poll - lttng stream addition/removal monitoring
579 *
580 * @file: the file
581 * @wait: poll table
582 */
583 unsigned int lttng_channel_poll(struct file *file, poll_table *wait)
584 {
585 struct ltt_channel *channel = file->private_data;
586 unsigned int mask = 0;
587
588 if (file->f_mode & FMODE_READ) {
589 poll_wait_set_exclusive(wait);
590 poll_wait(file, channel->ops->get_hp_wait_queue(channel->chan),
591 wait);
592
593 if (channel->ops->is_disabled(channel->chan))
594 return POLLERR;
595 if (channel->ops->is_finalized(channel->chan))
596 return POLLHUP;
597 else
598 return POLLIN | POLLRDNORM;
599 }
600 return mask;
601
602 }
603
604 static
605 int lttng_channel_release(struct inode *inode, struct file *file)
606 {
607 struct ltt_channel *channel = file->private_data;
608
609 if (channel)
610 fput(channel->session->file);
611 return 0;
612 }
613
614 static const struct file_operations lttng_channel_fops = {
615 .release = lttng_channel_release,
616 .poll = lttng_channel_poll,
617 .unlocked_ioctl = lttng_channel_ioctl,
618 #ifdef CONFIG_COMPAT
619 .compat_ioctl = lttng_channel_ioctl,
620 #endif
621 };
622
623 static const struct file_operations lttng_metadata_fops = {
624 .release = lttng_channel_release,
625 .unlocked_ioctl = lttng_metadata_ioctl,
626 #ifdef CONFIG_COMPAT
627 .compat_ioctl = lttng_metadata_ioctl,
628 #endif
629 };
630
631 /**
632 * lttng_event_ioctl - lttng syscall through ioctl
633 *
634 * @file: the file
635 * @cmd: the command
636 * @arg: command arg
637 *
638 * This ioctl implements lttng commands:
639 * LTTNG_KERNEL_CONTEXT
640 * Prepend a context field to each record of this event
641 * LTTNG_KERNEL_ENABLE
642 * Enable recording for this event (weak enable)
643 * LTTNG_KERNEL_DISABLE
644 * Disable recording for this event (strong disable)
645 */
646 static
647 long lttng_event_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
648 {
649 struct ltt_event *event = file->private_data;
650
651 switch (cmd) {
652 case LTTNG_KERNEL_CONTEXT:
653 return lttng_abi_add_context(file,
654 (struct lttng_kernel_context __user *) arg,
655 &event->ctx, event->chan->session);
656 case LTTNG_KERNEL_ENABLE:
657 return ltt_event_enable(event);
658 case LTTNG_KERNEL_DISABLE:
659 return ltt_event_disable(event);
660 default:
661 return -ENOIOCTLCMD;
662 }
663 }
664
665 static
666 int lttng_event_release(struct inode *inode, struct file *file)
667 {
668 struct ltt_event *event = file->private_data;
669
670 if (event)
671 fput(event->chan->file);
672 return 0;
673 }
674
675 /* TODO: filter control ioctl */
676 static const struct file_operations lttng_event_fops = {
677 .release = lttng_event_release,
678 .unlocked_ioctl = lttng_event_ioctl,
679 #ifdef CONFIG_COMPAT
680 .compat_ioctl = lttng_event_ioctl,
681 #endif
682 };
683
684 int __init ltt_debugfs_abi_init(void)
685 {
686 int ret = 0;
687
688 wrapper_vmalloc_sync_all();
689 lttng_dentry = debugfs_create_file("lttng", S_IWUSR, NULL, NULL,
690 &lttng_fops);
691 if (IS_ERR(lttng_dentry) || !lttng_dentry) {
692 printk(KERN_ERR "Error creating LTTng control file\n");
693 ret = -ENOMEM;
694 goto error;
695 }
696 error:
697 return ret;
698 }
699
700 void __exit ltt_debugfs_abi_exit(void)
701 {
702 debugfs_remove(lttng_dentry);
703 }
This page took 0.052171 seconds and 6 git commands to generate.