4 * Copyright 2010 (c) - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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.
24 * Dual LGPL v2.1/GPL v2 license.
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"
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.
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
;
53 * Teardown management: opened file descriptors keep a refcount on the module,
54 * so it can only exit when all file descriptors are closed.
63 int lttng_abi_create_session(void)
65 struct ltt_session
*session
;
66 struct file
*session_file
;
69 session
= ltt_session_create();
72 session_fd
= get_unused_fd();
77 session_file
= anon_inode_getfile("[lttng_session]",
80 if (IS_ERR(session_file
)) {
81 ret
= PTR_ERR(session_file
);
84 session
->file
= session_file
;
85 fd_install(session_fd
, session_file
);
89 put_unused_fd(session_fd
);
91 ltt_session_destroy(session
);
96 int lttng_abi_tracepoint_list(void)
98 struct file
*tracepoint_list_file
;
101 file_fd
= get_unused_fd();
107 tracepoint_list_file
= anon_inode_getfile("[lttng_session]",
108 <tng_tracepoint_list_fops
,
110 if (IS_ERR(tracepoint_list_file
)) {
111 ret
= PTR_ERR(tracepoint_list_file
);
114 ret
= lttng_tracepoint_list_fops
.open(NULL
, tracepoint_list_file
);
117 fd_install(file_fd
, tracepoint_list_file
);
125 fput(tracepoint_list_file
);
127 put_unused_fd(file_fd
);
133 long lttng_abi_tracer_version(struct file
*file
,
134 struct lttng_kernel_tracer_version __user
*uversion_param
)
136 struct lttng_kernel_tracer_version v
;
138 v
.version
= LTTNG_VERSION
;
139 v
.patchlevel
= LTTNG_PATCHLEVEL
;
140 v
.sublevel
= LTTNG_SUBLEVEL
;
142 if (copy_to_user(uversion_param
, &v
, sizeof(v
)))
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
)
152 struct lttng_kernel_context context_param
;
154 if (session
->been_active
)
157 if (copy_from_user(&context_param
, ucontext_param
, sizeof(context_param
)))
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
,
183 case LTTNG_KERNEL_CONTEXT_COMM
:
184 return lttng_add_comm_to_ctx(ctx
);
191 * lttng_ioctl - lttng syscall through ioctl
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
207 * The returned session will be deleted when its file descriptor is closed.
210 long lttng_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
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
:
228 static const struct file_operations lttng_fops
= {
229 .owner
= THIS_MODULE
,
230 .unlocked_ioctl
= lttng_ioctl
,
232 .compat_ioctl
= lttng_ioctl
,
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
242 void lttng_metadata_create_events(struct file
*channel_file
)
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",
249 struct ltt_event
*event
;
253 * We tolerate no failure path after event creation. It will stay
254 * invariant for the rest of the session.
256 event
= ltt_event_create(channel
, &metadata_params
, NULL
);
265 return; /* not allowed to return error */
269 int lttng_abi_create_channel(struct file
*session_file
,
270 struct lttng_kernel_channel __user
*uchan_param
,
271 enum channel_type channel_type
)
273 struct ltt_session
*session
= session_file
->private_data
;
274 const struct file_operations
*fops
;
275 const char *transport_name
;
276 struct ltt_channel
*chan
;
277 struct file
*chan_file
;
278 struct lttng_kernel_channel chan_param
;
282 if (copy_from_user(&chan_param
, uchan_param
, sizeof(chan_param
)))
284 chan_fd
= get_unused_fd();
289 chan_file
= anon_inode_getfile("[lttng_channel]",
292 if (IS_ERR(chan_file
)) {
293 ret
= PTR_ERR(chan_file
);
296 switch (channel_type
) {
297 case PER_CPU_CHANNEL
:
298 if (chan_param
.output
== LTTNG_KERNEL_SPLICE
) {
299 transport_name
= chan_param
.overwrite
?
300 "relay-overwrite" : "relay-discard";
301 } else if (chan_param
.output
== LTTNG_KERNEL_MMAP
) {
302 transport_name
= chan_param
.overwrite
?
303 "relay-overwrite-mmap" : "relay-discard-mmap";
307 fops
= <tng_channel_fops
;
309 case METADATA_CHANNEL
:
310 if (chan_param
.output
== LTTNG_KERNEL_SPLICE
)
311 transport_name
= "relay-metadata";
312 else if (chan_param
.output
== LTTNG_KERNEL_MMAP
)
313 transport_name
= "relay-metadata-mmap";
316 fops
= <tng_metadata_fops
;
319 transport_name
= "<unknown>";
323 * We tolerate no failure path after channel creation. It will stay
324 * invariant for the rest of the session.
326 chan
= ltt_channel_create(session
, transport_name
, NULL
,
327 chan_param
.subbuf_size
,
328 chan_param
.num_subbuf
,
329 chan_param
.switch_timer_interval
,
330 chan_param
.read_timer_interval
);
335 chan
->file
= chan_file
;
336 chan_file
->private_data
= chan
;
337 fd_install(chan_fd
, chan_file
);
338 if (channel_type
== METADATA_CHANNEL
) {
339 session
->metadata
= chan
;
340 lttng_metadata_create_events(chan_file
);
343 /* The channel created holds a reference on the session */
344 atomic_long_inc(&session_file
->f_count
);
351 put_unused_fd(chan_fd
);
357 * lttng_session_ioctl - lttng session fd ioctl
363 * This ioctl implements lttng commands:
364 * LTTNG_KERNEL_CHANNEL
365 * Returns a LTTng channel file descriptor
366 * LTTNG_KERNEL_ENABLE
367 * Enables tracing for a session (weak enable)
368 * LTTNG_KERNEL_DISABLE
369 * Disables tracing for a session (strong disable)
370 * LTTNG_KERNEL_METADATA
371 * Returns a LTTng metadata file descriptor
373 * The returned channel will be deleted when its file descriptor is closed.
376 long lttng_session_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
378 struct ltt_session
*session
= file
->private_data
;
381 case LTTNG_KERNEL_CHANNEL
:
382 return lttng_abi_create_channel(file
,
383 (struct lttng_kernel_channel __user
*) arg
,
385 case LTTNG_KERNEL_SESSION_START
:
386 case LTTNG_KERNEL_ENABLE
:
387 return ltt_session_enable(session
);
388 case LTTNG_KERNEL_SESSION_STOP
:
389 case LTTNG_KERNEL_DISABLE
:
390 return ltt_session_disable(session
);
391 case LTTNG_KERNEL_METADATA
:
392 return lttng_abi_create_channel(file
,
393 (struct lttng_kernel_channel __user
*) arg
,
401 * Called when the last file reference is dropped.
403 * Big fat note: channels and events are invariant for the whole session after
404 * their creation. So this session destruction also destroys all channel and
405 * event structures specific to this session (they are not destroyed when their
406 * individual file is released).
409 int lttng_session_release(struct inode
*inode
, struct file
*file
)
411 struct ltt_session
*session
= file
->private_data
;
414 ltt_session_destroy(session
);
418 static const struct file_operations lttng_session_fops
= {
419 .owner
= THIS_MODULE
,
420 .release
= lttng_session_release
,
421 .unlocked_ioctl
= lttng_session_ioctl
,
423 .compat_ioctl
= lttng_session_ioctl
,
428 int lttng_abi_open_stream(struct file
*channel_file
)
430 struct ltt_channel
*channel
= channel_file
->private_data
;
431 struct lib_ring_buffer
*buf
;
433 struct file
*stream_file
;
435 buf
= channel
->ops
->buffer_read_open(channel
->chan
);
439 stream_fd
= get_unused_fd();
444 stream_file
= anon_inode_getfile("[lttng_stream]",
445 &lib_ring_buffer_file_operations
,
447 if (IS_ERR(stream_file
)) {
448 ret
= PTR_ERR(stream_file
);
452 * OPEN_FMODE, called within anon_inode_getfile/alloc_file, don't honor
453 * FMODE_LSEEK, FMODE_PREAD nor FMODE_PWRITE. We need to read from this
454 * file descriptor, so we set FMODE_PREAD here.
456 stream_file
->f_mode
|= FMODE_PREAD
;
457 fd_install(stream_fd
, stream_file
);
459 * The stream holds a reference to the channel within the generic ring
460 * buffer library, so no need to hold a refcount on the channel and
461 * session files here.
466 put_unused_fd(stream_fd
);
468 channel
->ops
->buffer_read_close(buf
);
473 int lttng_abi_create_event(struct file
*channel_file
,
474 struct lttng_kernel_event __user
*uevent_param
)
476 struct ltt_channel
*channel
= channel_file
->private_data
;
477 struct ltt_event
*event
;
478 struct lttng_kernel_event event_param
;
480 struct file
*event_file
;
482 if (copy_from_user(&event_param
, uevent_param
, sizeof(event_param
)))
484 event_param
.name
[LTTNG_SYM_NAME_LEN
- 1] = '\0';
485 switch (event_param
.instrumentation
) {
486 case LTTNG_KERNEL_KPROBE
:
487 event_param
.u
.kprobe
.symbol_name
[LTTNG_SYM_NAME_LEN
- 1] = '\0';
489 case LTTNG_KERNEL_FUNCTION
:
490 event_param
.u
.ftrace
.symbol_name
[LTTNG_SYM_NAME_LEN
- 1] = '\0';
495 event_fd
= get_unused_fd();
500 event_file
= anon_inode_getfile("[lttng_event]",
503 if (IS_ERR(event_file
)) {
504 ret
= PTR_ERR(event_file
);
508 * We tolerate no failure path after event creation. It will stay
509 * invariant for the rest of the session.
511 event
= ltt_event_create(channel
, &event_param
, NULL
);
516 event_file
->private_data
= event
;
517 fd_install(event_fd
, event_file
);
518 /* The event holds a reference on the channel */
519 atomic_long_inc(&channel_file
->f_count
);
525 put_unused_fd(event_fd
);
531 * lttng_channel_ioctl - lttng syscall through ioctl
537 * This ioctl implements lttng commands:
538 * LTTNG_KERNEL_STREAM
539 * Returns an event stream file descriptor or failure.
540 * (typically, one event stream records events from one CPU)
542 * Returns an event file descriptor or failure.
543 * LTTNG_KERNEL_CONTEXT
544 * Prepend a context field to each event in the channel
545 * LTTNG_KERNEL_ENABLE
546 * Enable recording for events in this channel (weak enable)
547 * LTTNG_KERNEL_DISABLE
548 * Disable recording for events in this channel (strong disable)
550 * Channel and event file descriptors also hold a reference on the session.
553 long lttng_channel_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
555 struct ltt_channel
*channel
= file
->private_data
;
558 case LTTNG_KERNEL_STREAM
:
559 return lttng_abi_open_stream(file
);
560 case LTTNG_KERNEL_EVENT
:
561 return lttng_abi_create_event(file
, (struct lttng_kernel_event __user
*) arg
);
562 case LTTNG_KERNEL_CONTEXT
:
563 return lttng_abi_add_context(file
,
564 (struct lttng_kernel_context __user
*) arg
,
565 &channel
->ctx
, channel
->session
);
566 case LTTNG_KERNEL_ENABLE
:
567 return ltt_channel_enable(channel
);
568 case LTTNG_KERNEL_DISABLE
:
569 return ltt_channel_disable(channel
);
576 * lttng_metadata_ioctl - lttng syscall through ioctl
582 * This ioctl implements lttng commands:
583 * LTTNG_KERNEL_STREAM
584 * Returns an event stream file descriptor or failure.
586 * Channel and event file descriptors also hold a reference on the session.
589 long lttng_metadata_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
592 case LTTNG_KERNEL_STREAM
:
593 return lttng_abi_open_stream(file
);
600 * lttng_channel_poll - lttng stream addition/removal monitoring
605 unsigned int lttng_channel_poll(struct file
*file
, poll_table
*wait
)
607 struct ltt_channel
*channel
= file
->private_data
;
608 unsigned int mask
= 0;
610 if (file
->f_mode
& FMODE_READ
) {
611 poll_wait_set_exclusive(wait
);
612 poll_wait(file
, channel
->ops
->get_hp_wait_queue(channel
->chan
),
615 if (channel
->ops
->is_disabled(channel
->chan
))
617 if (channel
->ops
->is_finalized(channel
->chan
))
619 if (channel
->ops
->buffer_has_read_closed_stream(channel
->chan
))
620 return POLLIN
| POLLRDNORM
;
628 int lttng_channel_release(struct inode
*inode
, struct file
*file
)
630 struct ltt_channel
*channel
= file
->private_data
;
633 fput(channel
->session
->file
);
637 static const struct file_operations lttng_channel_fops
= {
638 .owner
= THIS_MODULE
,
639 .release
= lttng_channel_release
,
640 .poll
= lttng_channel_poll
,
641 .unlocked_ioctl
= lttng_channel_ioctl
,
643 .compat_ioctl
= lttng_channel_ioctl
,
647 static const struct file_operations lttng_metadata_fops
= {
648 .owner
= THIS_MODULE
,
649 .release
= lttng_channel_release
,
650 .unlocked_ioctl
= lttng_metadata_ioctl
,
652 .compat_ioctl
= lttng_metadata_ioctl
,
657 * lttng_event_ioctl - lttng syscall through ioctl
663 * This ioctl implements lttng commands:
664 * LTTNG_KERNEL_CONTEXT
665 * Prepend a context field to each record of this event
666 * LTTNG_KERNEL_ENABLE
667 * Enable recording for this event (weak enable)
668 * LTTNG_KERNEL_DISABLE
669 * Disable recording for this event (strong disable)
672 long lttng_event_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
674 struct ltt_event
*event
= file
->private_data
;
677 case LTTNG_KERNEL_CONTEXT
:
678 return lttng_abi_add_context(file
,
679 (struct lttng_kernel_context __user
*) arg
,
680 &event
->ctx
, event
->chan
->session
);
681 case LTTNG_KERNEL_ENABLE
:
682 return ltt_event_enable(event
);
683 case LTTNG_KERNEL_DISABLE
:
684 return ltt_event_disable(event
);
691 int lttng_event_release(struct inode
*inode
, struct file
*file
)
693 struct ltt_event
*event
= file
->private_data
;
696 fput(event
->chan
->file
);
700 /* TODO: filter control ioctl */
701 static const struct file_operations lttng_event_fops
= {
702 .owner
= THIS_MODULE
,
703 .release
= lttng_event_release
,
704 .unlocked_ioctl
= lttng_event_ioctl
,
706 .compat_ioctl
= lttng_event_ioctl
,
710 int __init
ltt_debugfs_abi_init(void)
714 wrapper_vmalloc_sync_all();
715 lttng_dentry
= debugfs_create_file("lttng", S_IWUSR
, NULL
, NULL
,
717 if (IS_ERR(lttng_dentry
) || !lttng_dentry
) {
718 printk(KERN_ERR
"Error creating LTTng control file\n");
726 void __exit
ltt_debugfs_abi_exit(void)
728 debugfs_remove(lttng_dentry
);