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/proc_fs.h>
30 #include <linux/anon_inodes.h>
31 #include <linux/file.h>
32 #include <linux/uaccess.h>
33 #include <linux/slab.h>
34 #include "wrapper/vmalloc.h" /* for wrapper_vmalloc_sync_all() */
35 #include "wrapper/ringbuffer/vfs.h"
36 #include "wrapper/poll.h"
37 #include "ltt-debugfs-abi.h"
38 #include "ltt-events.h"
39 #include "ltt-tracer.h"
42 * This is LTTng's own personal way to create a system call as an external
43 * module. We use ioctl() on /sys/kernel/debug/lttng.
46 static struct dentry
*lttng_dentry
;
47 static struct proc_dir_entry
*lttng_proc_dentry
;
48 static const struct file_operations lttng_fops
;
49 static const struct file_operations lttng_session_fops
;
50 static const struct file_operations lttng_channel_fops
;
51 static const struct file_operations lttng_metadata_fops
;
52 static const struct file_operations lttng_event_fops
;
55 * Teardown management: opened file descriptors keep a refcount on the module,
56 * so it can only exit when all file descriptors are closed.
65 int lttng_abi_create_session(void)
67 struct ltt_session
*session
;
68 struct file
*session_file
;
71 session
= ltt_session_create();
74 session_fd
= get_unused_fd();
79 session_file
= anon_inode_getfile("[lttng_session]",
82 if (IS_ERR(session_file
)) {
83 ret
= PTR_ERR(session_file
);
86 session
->file
= session_file
;
87 fd_install(session_fd
, session_file
);
91 put_unused_fd(session_fd
);
93 ltt_session_destroy(session
);
98 int lttng_abi_tracepoint_list(void)
100 struct file
*tracepoint_list_file
;
103 file_fd
= get_unused_fd();
109 tracepoint_list_file
= anon_inode_getfile("[lttng_session]",
110 <tng_tracepoint_list_fops
,
112 if (IS_ERR(tracepoint_list_file
)) {
113 ret
= PTR_ERR(tracepoint_list_file
);
116 ret
= lttng_tracepoint_list_fops
.open(NULL
, tracepoint_list_file
);
119 fd_install(file_fd
, tracepoint_list_file
);
127 fput(tracepoint_list_file
);
129 put_unused_fd(file_fd
);
135 long lttng_abi_tracer_version(struct file
*file
,
136 struct lttng_kernel_tracer_version __user
*uversion_param
)
138 struct lttng_kernel_tracer_version v
;
140 v
.version
= LTTNG_VERSION
;
141 v
.patchlevel
= LTTNG_PATCHLEVEL
;
142 v
.sublevel
= LTTNG_SUBLEVEL
;
144 if (copy_to_user(uversion_param
, &v
, sizeof(v
)))
150 long lttng_abi_add_context(struct file
*file
,
151 struct lttng_kernel_context __user
*ucontext_param
,
152 struct lttng_ctx
**ctx
, struct ltt_session
*session
)
154 struct lttng_kernel_context context_param
;
156 if (session
->been_active
)
159 if (copy_from_user(&context_param
, ucontext_param
, sizeof(context_param
)))
162 switch (context_param
.ctx
) {
163 case LTTNG_KERNEL_CONTEXT_PID
:
164 return lttng_add_pid_to_ctx(ctx
);
165 case LTTNG_KERNEL_CONTEXT_PRIO
:
166 return lttng_add_prio_to_ctx(ctx
);
167 case LTTNG_KERNEL_CONTEXT_NICE
:
168 return lttng_add_nice_to_ctx(ctx
);
169 case LTTNG_KERNEL_CONTEXT_VPID
:
170 return lttng_add_vpid_to_ctx(ctx
);
171 case LTTNG_KERNEL_CONTEXT_TID
:
172 return lttng_add_tid_to_ctx(ctx
);
173 case LTTNG_KERNEL_CONTEXT_VTID
:
174 return lttng_add_vtid_to_ctx(ctx
);
175 case LTTNG_KERNEL_CONTEXT_PPID
:
176 return lttng_add_ppid_to_ctx(ctx
);
177 case LTTNG_KERNEL_CONTEXT_VPPID
:
178 return lttng_add_vppid_to_ctx(ctx
);
179 case LTTNG_KERNEL_CONTEXT_PERF_COUNTER
:
180 context_param
.u
.perf_counter
.name
[LTTNG_SYM_NAME_LEN
- 1] = '\0';
181 return lttng_add_perf_counter_to_ctx(context_param
.u
.perf_counter
.type
,
182 context_param
.u
.perf_counter
.config
,
183 context_param
.u
.perf_counter
.name
,
185 case LTTNG_KERNEL_CONTEXT_PROCNAME
:
186 return lttng_add_procname_to_ctx(ctx
);
193 * lttng_ioctl - lttng syscall through ioctl
199 * This ioctl implements lttng commands:
200 * LTTNG_KERNEL_SESSION
201 * Returns a LTTng trace session file descriptor
202 * LTTNG_KERNEL_TRACER_VERSION
203 * Returns the LTTng kernel tracer version
204 * LTTNG_KERNEL_TRACEPOINT_LIST
205 * Returns a file descriptor listing available tracepoints
206 * LTTNG_KERNEL_WAIT_QUIESCENT
207 * Returns after all previously running probes have completed
209 * The returned session will be deleted when its file descriptor is closed.
212 long lttng_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
215 case LTTNG_KERNEL_SESSION
:
216 return lttng_abi_create_session();
217 case LTTNG_KERNEL_TRACER_VERSION
:
218 return lttng_abi_tracer_version(file
,
219 (struct lttng_kernel_tracer_version __user
*) arg
);
220 case LTTNG_KERNEL_TRACEPOINT_LIST
:
221 return lttng_abi_tracepoint_list();
222 case LTTNG_KERNEL_WAIT_QUIESCENT
:
225 case LTTNG_KERNEL_CALIBRATE
:
227 struct lttng_kernel_calibrate __user
*ucalibrate
=
228 (struct lttng_kernel_calibrate __user
*) arg
;
229 struct lttng_kernel_calibrate calibrate
;
232 if (copy_from_user(&calibrate
, ucalibrate
, sizeof(calibrate
)))
234 ret
= lttng_calibrate(&calibrate
);
235 if (copy_to_user(ucalibrate
, &calibrate
, sizeof(calibrate
)))
244 static const struct file_operations lttng_fops
= {
245 .owner
= THIS_MODULE
,
246 .unlocked_ioctl
= lttng_ioctl
,
248 .compat_ioctl
= lttng_ioctl
,
253 * We tolerate no failure in this function (if one happens, we print a dmesg
254 * error, but cannot return any error, because the channel information is
258 void lttng_metadata_create_events(struct file
*channel_file
)
260 struct ltt_channel
*channel
= channel_file
->private_data
;
261 static struct lttng_kernel_event metadata_params
= {
262 .instrumentation
= LTTNG_KERNEL_TRACEPOINT
,
263 .name
= "lttng_metadata",
265 struct ltt_event
*event
;
268 * We tolerate no failure path after event creation. It will stay
269 * invariant for the rest of the session.
271 event
= ltt_event_create(channel
, &metadata_params
, NULL
, NULL
);
279 return; /* not allowed to return error */
283 int lttng_abi_create_channel(struct file
*session_file
,
284 struct lttng_kernel_channel __user
*uchan_param
,
285 enum channel_type channel_type
)
287 struct ltt_session
*session
= session_file
->private_data
;
288 const struct file_operations
*fops
= NULL
;
289 const char *transport_name
;
290 struct ltt_channel
*chan
;
291 struct file
*chan_file
;
292 struct lttng_kernel_channel chan_param
;
296 if (copy_from_user(&chan_param
, uchan_param
, sizeof(chan_param
)))
298 chan_fd
= get_unused_fd();
303 switch (channel_type
) {
304 case PER_CPU_CHANNEL
:
305 fops
= <tng_channel_fops
;
307 case METADATA_CHANNEL
:
308 fops
= <tng_metadata_fops
;
312 chan_file
= anon_inode_getfile("[lttng_channel]",
315 if (IS_ERR(chan_file
)) {
316 ret
= PTR_ERR(chan_file
);
319 switch (channel_type
) {
320 case PER_CPU_CHANNEL
:
321 if (chan_param
.output
== LTTNG_KERNEL_SPLICE
) {
322 transport_name
= chan_param
.overwrite
?
323 "relay-overwrite" : "relay-discard";
324 } else if (chan_param
.output
== LTTNG_KERNEL_MMAP
) {
325 transport_name
= chan_param
.overwrite
?
326 "relay-overwrite-mmap" : "relay-discard-mmap";
331 case METADATA_CHANNEL
:
332 if (chan_param
.output
== LTTNG_KERNEL_SPLICE
)
333 transport_name
= "relay-metadata";
334 else if (chan_param
.output
== LTTNG_KERNEL_MMAP
)
335 transport_name
= "relay-metadata-mmap";
340 transport_name
= "<unknown>";
344 * We tolerate no failure path after channel creation. It will stay
345 * invariant for the rest of the session.
347 chan
= ltt_channel_create(session
, transport_name
, NULL
,
348 chan_param
.subbuf_size
,
349 chan_param
.num_subbuf
,
350 chan_param
.switch_timer_interval
,
351 chan_param
.read_timer_interval
);
356 chan
->file
= chan_file
;
357 chan_file
->private_data
= chan
;
358 fd_install(chan_fd
, chan_file
);
359 if (channel_type
== METADATA_CHANNEL
) {
360 session
->metadata
= chan
;
361 lttng_metadata_create_events(chan_file
);
364 /* The channel created holds a reference on the session */
365 atomic_long_inc(&session_file
->f_count
);
372 put_unused_fd(chan_fd
);
378 * lttng_session_ioctl - lttng session fd ioctl
384 * This ioctl implements lttng commands:
385 * LTTNG_KERNEL_CHANNEL
386 * Returns a LTTng channel file descriptor
387 * LTTNG_KERNEL_ENABLE
388 * Enables tracing for a session (weak enable)
389 * LTTNG_KERNEL_DISABLE
390 * Disables tracing for a session (strong disable)
391 * LTTNG_KERNEL_METADATA
392 * Returns a LTTng metadata file descriptor
394 * The returned channel will be deleted when its file descriptor is closed.
397 long lttng_session_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
399 struct ltt_session
*session
= file
->private_data
;
402 case LTTNG_KERNEL_CHANNEL
:
403 return lttng_abi_create_channel(file
,
404 (struct lttng_kernel_channel __user
*) arg
,
406 case LTTNG_KERNEL_SESSION_START
:
407 case LTTNG_KERNEL_ENABLE
:
408 return ltt_session_enable(session
);
409 case LTTNG_KERNEL_SESSION_STOP
:
410 case LTTNG_KERNEL_DISABLE
:
411 return ltt_session_disable(session
);
412 case LTTNG_KERNEL_METADATA
:
413 return lttng_abi_create_channel(file
,
414 (struct lttng_kernel_channel __user
*) arg
,
422 * Called when the last file reference is dropped.
424 * Big fat note: channels and events are invariant for the whole session after
425 * their creation. So this session destruction also destroys all channel and
426 * event structures specific to this session (they are not destroyed when their
427 * individual file is released).
430 int lttng_session_release(struct inode
*inode
, struct file
*file
)
432 struct ltt_session
*session
= file
->private_data
;
435 ltt_session_destroy(session
);
439 static const struct file_operations lttng_session_fops
= {
440 .owner
= THIS_MODULE
,
441 .release
= lttng_session_release
,
442 .unlocked_ioctl
= lttng_session_ioctl
,
444 .compat_ioctl
= lttng_session_ioctl
,
449 int lttng_abi_open_stream(struct file
*channel_file
)
451 struct ltt_channel
*channel
= channel_file
->private_data
;
452 struct lib_ring_buffer
*buf
;
454 struct file
*stream_file
;
456 buf
= channel
->ops
->buffer_read_open(channel
->chan
);
460 stream_fd
= get_unused_fd();
465 stream_file
= anon_inode_getfile("[lttng_stream]",
466 &lib_ring_buffer_file_operations
,
468 if (IS_ERR(stream_file
)) {
469 ret
= PTR_ERR(stream_file
);
473 * OPEN_FMODE, called within anon_inode_getfile/alloc_file, don't honor
474 * FMODE_LSEEK, FMODE_PREAD nor FMODE_PWRITE. We need to read from this
475 * file descriptor, so we set FMODE_PREAD here.
477 stream_file
->f_mode
|= FMODE_PREAD
;
478 fd_install(stream_fd
, stream_file
);
480 * The stream holds a reference to the channel within the generic ring
481 * buffer library, so no need to hold a refcount on the channel and
482 * session files here.
487 put_unused_fd(stream_fd
);
489 channel
->ops
->buffer_read_close(buf
);
494 int lttng_abi_create_event(struct file
*channel_file
,
495 struct lttng_kernel_event __user
*uevent_param
)
497 struct ltt_channel
*channel
= channel_file
->private_data
;
498 struct ltt_event
*event
;
499 struct lttng_kernel_event event_param
;
501 struct file
*event_file
;
503 if (copy_from_user(&event_param
, uevent_param
, sizeof(event_param
)))
505 event_param
.name
[LTTNG_SYM_NAME_LEN
- 1] = '\0';
506 switch (event_param
.instrumentation
) {
507 case LTTNG_KERNEL_KRETPROBE
:
508 event_param
.u
.kretprobe
.symbol_name
[LTTNG_SYM_NAME_LEN
- 1] = '\0';
510 case LTTNG_KERNEL_KPROBE
:
511 event_param
.u
.kprobe
.symbol_name
[LTTNG_SYM_NAME_LEN
- 1] = '\0';
513 case LTTNG_KERNEL_FUNCTION
:
514 event_param
.u
.ftrace
.symbol_name
[LTTNG_SYM_NAME_LEN
- 1] = '\0';
519 switch (event_param
.instrumentation
) {
521 event_fd
= get_unused_fd();
526 event_file
= anon_inode_getfile("[lttng_event]",
529 if (IS_ERR(event_file
)) {
530 ret
= PTR_ERR(event_file
);
534 * We tolerate no failure path after event creation. It
535 * will stay invariant for the rest of the session.
537 event
= ltt_event_create(channel
, &event_param
, NULL
, NULL
);
542 event_file
->private_data
= event
;
543 fd_install(event_fd
, event_file
);
544 /* The event holds a reference on the channel */
545 atomic_long_inc(&channel_file
->f_count
);
547 case LTTNG_KERNEL_SYSCALL
:
549 * Only all-syscall tracing supported for now.
551 if (event_param
.name
[0] != '\0')
553 ret
= lttng_syscalls_register(channel
, NULL
);
564 put_unused_fd(event_fd
);
570 * lttng_channel_ioctl - lttng syscall through ioctl
576 * This ioctl implements lttng commands:
577 * LTTNG_KERNEL_STREAM
578 * Returns an event stream file descriptor or failure.
579 * (typically, one event stream records events from one CPU)
581 * Returns an event file descriptor or failure.
582 * LTTNG_KERNEL_CONTEXT
583 * Prepend a context field to each event in the channel
584 * LTTNG_KERNEL_ENABLE
585 * Enable recording for events in this channel (weak enable)
586 * LTTNG_KERNEL_DISABLE
587 * Disable recording for events in this channel (strong disable)
589 * Channel and event file descriptors also hold a reference on the session.
592 long lttng_channel_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
594 struct ltt_channel
*channel
= file
->private_data
;
597 case LTTNG_KERNEL_STREAM
:
598 return lttng_abi_open_stream(file
);
599 case LTTNG_KERNEL_EVENT
:
600 return lttng_abi_create_event(file
, (struct lttng_kernel_event __user
*) arg
);
601 case LTTNG_KERNEL_CONTEXT
:
602 return lttng_abi_add_context(file
,
603 (struct lttng_kernel_context __user
*) arg
,
604 &channel
->ctx
, channel
->session
);
605 case LTTNG_KERNEL_ENABLE
:
606 return ltt_channel_enable(channel
);
607 case LTTNG_KERNEL_DISABLE
:
608 return ltt_channel_disable(channel
);
615 * lttng_metadata_ioctl - lttng syscall through ioctl
621 * This ioctl implements lttng commands:
622 * LTTNG_KERNEL_STREAM
623 * Returns an event stream file descriptor or failure.
625 * Channel and event file descriptors also hold a reference on the session.
628 long lttng_metadata_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
631 case LTTNG_KERNEL_STREAM
:
632 return lttng_abi_open_stream(file
);
639 * lttng_channel_poll - lttng stream addition/removal monitoring
644 unsigned int lttng_channel_poll(struct file
*file
, poll_table
*wait
)
646 struct ltt_channel
*channel
= file
->private_data
;
647 unsigned int mask
= 0;
649 if (file
->f_mode
& FMODE_READ
) {
650 poll_wait_set_exclusive(wait
);
651 poll_wait(file
, channel
->ops
->get_hp_wait_queue(channel
->chan
),
654 if (channel
->ops
->is_disabled(channel
->chan
))
656 if (channel
->ops
->is_finalized(channel
->chan
))
658 if (channel
->ops
->buffer_has_read_closed_stream(channel
->chan
))
659 return POLLIN
| POLLRDNORM
;
667 int lttng_channel_release(struct inode
*inode
, struct file
*file
)
669 struct ltt_channel
*channel
= file
->private_data
;
672 fput(channel
->session
->file
);
676 static const struct file_operations lttng_channel_fops
= {
677 .owner
= THIS_MODULE
,
678 .release
= lttng_channel_release
,
679 .poll
= lttng_channel_poll
,
680 .unlocked_ioctl
= lttng_channel_ioctl
,
682 .compat_ioctl
= lttng_channel_ioctl
,
686 static const struct file_operations lttng_metadata_fops
= {
687 .owner
= THIS_MODULE
,
688 .release
= lttng_channel_release
,
689 .unlocked_ioctl
= lttng_metadata_ioctl
,
691 .compat_ioctl
= lttng_metadata_ioctl
,
696 * lttng_event_ioctl - lttng syscall through ioctl
702 * This ioctl implements lttng commands:
703 * LTTNG_KERNEL_CONTEXT
704 * Prepend a context field to each record of this event
705 * LTTNG_KERNEL_ENABLE
706 * Enable recording for this event (weak enable)
707 * LTTNG_KERNEL_DISABLE
708 * Disable recording for this event (strong disable)
711 long lttng_event_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
713 struct ltt_event
*event
= file
->private_data
;
716 case LTTNG_KERNEL_CONTEXT
:
717 return lttng_abi_add_context(file
,
718 (struct lttng_kernel_context __user
*) arg
,
719 &event
->ctx
, event
->chan
->session
);
720 case LTTNG_KERNEL_ENABLE
:
721 return ltt_event_enable(event
);
722 case LTTNG_KERNEL_DISABLE
:
723 return ltt_event_disable(event
);
730 int lttng_event_release(struct inode
*inode
, struct file
*file
)
732 struct ltt_event
*event
= file
->private_data
;
735 fput(event
->chan
->file
);
739 /* TODO: filter control ioctl */
740 static const struct file_operations lttng_event_fops
= {
741 .owner
= THIS_MODULE
,
742 .release
= lttng_event_release
,
743 .unlocked_ioctl
= lttng_event_ioctl
,
745 .compat_ioctl
= lttng_event_ioctl
,
749 int __init
ltt_debugfs_abi_init(void)
753 wrapper_vmalloc_sync_all();
754 lttng_dentry
= debugfs_create_file("lttng", S_IWUSR
, NULL
, NULL
,
756 if (IS_ERR(lttng_dentry
))
759 lttng_proc_dentry
= proc_create_data("lttng", S_IWUSR
, NULL
,
762 if (!lttng_dentry
&& !lttng_proc_dentry
) {
763 printk(KERN_ERR
"Error creating LTTng control file\n");
771 void __exit
ltt_debugfs_abi_exit(void)
774 debugfs_remove(lttng_dentry
);
775 if (lttng_proc_dentry
)
776 remove_proc_entry("lttng", NULL
);