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