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
;
58 int lttng_abi_create_session(void)
60 struct ltt_session
*session
;
61 struct file
*session_file
;
64 session
= ltt_session_create();
67 session_fd
= get_unused_fd();
72 session_file
= anon_inode_getfile("[lttng_session]",
75 if (IS_ERR(session_file
)) {
76 ret
= PTR_ERR(session_file
);
79 session
->file
= session_file
;
80 fd_install(session_fd
, session_file
);
84 put_unused_fd(session_fd
);
86 ltt_session_destroy(session
);
91 int lttng_abi_tracepoint_list(void)
93 struct file
*tracepoint_list_file
;
96 file_fd
= get_unused_fd();
102 tracepoint_list_file
= anon_inode_getfile("[lttng_session]",
103 <tng_tracepoint_list_fops
,
105 if (IS_ERR(tracepoint_list_file
)) {
106 ret
= PTR_ERR(tracepoint_list_file
);
109 ret
= lttng_tracepoint_list_fops
.open(NULL
, tracepoint_list_file
);
112 fd_install(file_fd
, tracepoint_list_file
);
120 fput(tracepoint_list_file
);
122 put_unused_fd(file_fd
);
128 long lttng_abi_tracer_version(struct file
*file
,
129 struct lttng_kernel_tracer_version __user
*uversion_param
)
131 struct lttng_kernel_tracer_version v
;
133 v
.version
= LTTNG_VERSION
;
134 v
.patchlevel
= LTTNG_PATCHLEVEL
;
135 v
.sublevel
= LTTNG_SUBLEVEL
;
137 if (copy_to_user(uversion_param
, &v
, sizeof(v
)))
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
)
147 struct lttng_kernel_context context_param
;
149 if (session
->been_active
)
152 if (copy_from_user(&context_param
, ucontext_param
, sizeof(context_param
)))
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
,
178 case LTTNG_KERNEL_CONTEXT_COMM
:
179 return lttng_add_comm_to_ctx(ctx
);
186 * lttng_ioctl - lttng syscall through ioctl
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
202 * The returned session will be deleted when its file descriptor is closed.
205 long lttng_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
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
:
223 static const struct file_operations lttng_fops
= {
224 .unlocked_ioctl
= lttng_ioctl
,
226 .compat_ioctl
= lttng_ioctl
,
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
236 void lttng_metadata_create_events(struct file
*channel_file
)
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",
243 struct ltt_event
*event
;
247 * We tolerate no failure path after event creation. It will stay
248 * invariant for the rest of the session.
250 event
= ltt_event_create(channel
, &metadata_params
, NULL
);
259 return; /* not allowed to return error */
263 int lttng_abi_create_channel(struct file
*session_file
,
264 struct lttng_kernel_channel __user
*uchan_param
,
265 enum channel_type channel_type
)
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
;
276 if (copy_from_user(&chan_param
, uchan_param
, sizeof(chan_param
)))
278 chan_fd
= get_unused_fd();
283 chan_file
= anon_inode_getfile("[lttng_channel]",
286 if (IS_ERR(chan_file
)) {
287 ret
= PTR_ERR(chan_file
);
290 switch (channel_type
) {
291 case PER_CPU_CHANNEL
:
292 transport_name
= chan_param
.overwrite
?
293 "relay-overwrite" : "relay-discard";
294 fops
= <tng_channel_fops
;
296 case METADATA_CHANNEL
:
297 transport_name
= "relay-metadata";
298 fops
= <tng_metadata_fops
;
301 transport_name
= "<unknown>";
305 * We tolerate no failure path after channel creation. It will stay
306 * invariant for the rest of the session.
308 chan
= ltt_channel_create(session
, transport_name
, NULL
,
309 chan_param
.subbuf_size
,
310 chan_param
.num_subbuf
,
311 chan_param
.switch_timer_interval
,
312 chan_param
.read_timer_interval
);
317 chan
->file
= chan_file
;
318 chan_file
->private_data
= chan
;
319 fd_install(chan_fd
, chan_file
);
320 if (channel_type
== METADATA_CHANNEL
) {
321 session
->metadata
= chan
;
322 lttng_metadata_create_events(chan_file
);
325 /* The channel created holds a reference on the session */
326 atomic_long_inc(&session_file
->f_count
);
333 put_unused_fd(chan_fd
);
339 * lttng_session_ioctl - lttng session fd ioctl
345 * This ioctl implements lttng commands:
346 * LTTNG_KERNEL_CHANNEL
347 * Returns a LTTng channel file descriptor
348 * LTTNG_KERNEL_ENABLE
349 * Enables tracing for a session (weak enable)
350 * LTTNG_KERNEL_DISABLE
351 * Disables tracing for a session (strong disable)
352 * LTTNG_KERNEL_METADATA
353 * Returns a LTTng metadata file descriptor
355 * The returned channel will be deleted when its file descriptor is closed.
358 long lttng_session_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
360 struct ltt_session
*session
= file
->private_data
;
363 case LTTNG_KERNEL_CHANNEL
:
364 return lttng_abi_create_channel(file
,
365 (struct lttng_kernel_channel __user
*) arg
,
367 case LTTNG_KERNEL_SESSION_START
:
368 case LTTNG_KERNEL_ENABLE
:
369 return ltt_session_enable(session
);
370 case LTTNG_KERNEL_SESSION_STOP
:
371 case LTTNG_KERNEL_DISABLE
:
372 return ltt_session_disable(session
);
373 case LTTNG_KERNEL_METADATA
:
374 return lttng_abi_create_channel(file
,
375 (struct lttng_kernel_channel __user
*) arg
,
383 * Called when the last file reference is dropped.
385 * Big fat note: channels and events are invariant for the whole session after
386 * their creation. So this session destruction also destroys all channel and
387 * event structures specific to this session (they are not destroyed when their
388 * individual file is released).
391 int lttng_session_release(struct inode
*inode
, struct file
*file
)
393 struct ltt_session
*session
= file
->private_data
;
396 ltt_session_destroy(session
);
400 static const struct file_operations lttng_session_fops
= {
401 .release
= lttng_session_release
,
402 .unlocked_ioctl
= lttng_session_ioctl
,
404 .compat_ioctl
= lttng_session_ioctl
,
409 int lttng_abi_open_stream(struct file
*channel_file
)
411 struct ltt_channel
*channel
= channel_file
->private_data
;
412 struct lib_ring_buffer
*buf
;
414 struct file
*stream_file
;
416 buf
= channel
->ops
->buffer_read_open(channel
->chan
);
420 stream_fd
= get_unused_fd();
425 stream_file
= anon_inode_getfile("[lttng_stream]",
426 &lib_ring_buffer_file_operations
,
428 if (IS_ERR(stream_file
)) {
429 ret
= PTR_ERR(stream_file
);
433 * OPEN_FMODE, called within anon_inode_getfile/alloc_file, don't honor
434 * FMODE_LSEEK, FMODE_PREAD nor FMODE_PWRITE. We need to read from this
435 * file descriptor, so we set FMODE_PREAD here.
437 stream_file
->f_mode
|= FMODE_PREAD
;
438 fd_install(stream_fd
, stream_file
);
440 * The stream holds a reference to the channel within the generic ring
441 * buffer library, so no need to hold a refcount on the channel and
442 * session files here.
447 put_unused_fd(stream_fd
);
449 channel
->ops
->buffer_read_close(buf
);
454 int lttng_abi_create_event(struct file
*channel_file
,
455 struct lttng_kernel_event __user
*uevent_param
)
457 struct ltt_channel
*channel
= channel_file
->private_data
;
458 struct ltt_event
*event
;
459 struct lttng_kernel_event event_param
;
461 struct file
*event_file
;
463 if (copy_from_user(&event_param
, uevent_param
, sizeof(event_param
)))
465 event_param
.name
[LTTNG_SYM_NAME_LEN
- 1] = '\0';
466 switch (event_param
.instrumentation
) {
467 case LTTNG_KERNEL_KPROBE
:
468 event_param
.u
.kprobe
.symbol_name
[LTTNG_SYM_NAME_LEN
- 1] = '\0';
470 case LTTNG_KERNEL_FUNCTION
:
471 event_param
.u
.ftrace
.symbol_name
[LTTNG_SYM_NAME_LEN
- 1] = '\0';
476 event_fd
= get_unused_fd();
481 event_file
= anon_inode_getfile("[lttng_event]",
484 if (IS_ERR(event_file
)) {
485 ret
= PTR_ERR(event_file
);
489 * We tolerate no failure path after event creation. It will stay
490 * invariant for the rest of the session.
492 event
= ltt_event_create(channel
, &event_param
, NULL
);
497 event_file
->private_data
= event
;
498 fd_install(event_fd
, event_file
);
499 /* The event holds a reference on the channel */
500 atomic_long_inc(&channel_file
->f_count
);
506 put_unused_fd(event_fd
);
512 * lttng_channel_ioctl - lttng syscall through ioctl
518 * This ioctl implements lttng commands:
519 * LTTNG_KERNEL_STREAM
520 * Returns an event stream file descriptor or failure.
521 * (typically, one event stream records events from one CPU)
523 * Returns an event file descriptor or failure.
524 * LTTNG_KERNEL_CONTEXT
525 * Prepend a context field to each event in the channel
526 * LTTNG_KERNEL_ENABLE
527 * Enable recording for events in this channel (weak enable)
528 * LTTNG_KERNEL_DISABLE
529 * Disable recording for events in this channel (strong disable)
531 * Channel and event file descriptors also hold a reference on the session.
534 long lttng_channel_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
536 struct ltt_channel
*channel
= file
->private_data
;
539 case LTTNG_KERNEL_STREAM
:
540 return lttng_abi_open_stream(file
);
541 case LTTNG_KERNEL_EVENT
:
542 return lttng_abi_create_event(file
, (struct lttng_kernel_event __user
*) arg
);
543 case LTTNG_KERNEL_CONTEXT
:
544 return lttng_abi_add_context(file
,
545 (struct lttng_kernel_context __user
*) arg
,
546 &channel
->ctx
, channel
->session
);
547 case LTTNG_KERNEL_ENABLE
:
548 return ltt_channel_enable(channel
);
549 case LTTNG_KERNEL_DISABLE
:
550 return ltt_channel_disable(channel
);
557 * lttng_metadata_ioctl - lttng syscall through ioctl
563 * This ioctl implements lttng commands:
564 * LTTNG_KERNEL_STREAM
565 * Returns an event stream file descriptor or failure.
567 * Channel and event file descriptors also hold a reference on the session.
570 long lttng_metadata_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
573 case LTTNG_KERNEL_STREAM
:
574 return lttng_abi_open_stream(file
);
581 * lttng_channel_poll - lttng stream addition/removal monitoring
586 unsigned int lttng_channel_poll(struct file
*file
, poll_table
*wait
)
588 struct ltt_channel
*channel
= file
->private_data
;
589 unsigned int mask
= 0;
591 if (file
->f_mode
& FMODE_READ
) {
592 poll_wait_set_exclusive(wait
);
593 poll_wait(file
, channel
->ops
->get_hp_wait_queue(channel
->chan
),
596 if (channel
->ops
->is_disabled(channel
->chan
))
598 if (channel
->ops
->is_finalized(channel
->chan
))
601 return POLLIN
| POLLRDNORM
;
608 int lttng_channel_release(struct inode
*inode
, struct file
*file
)
610 struct ltt_channel
*channel
= file
->private_data
;
613 fput(channel
->session
->file
);
617 static const struct file_operations lttng_channel_fops
= {
618 .release
= lttng_channel_release
,
619 .poll
= lttng_channel_poll
,
620 .unlocked_ioctl
= lttng_channel_ioctl
,
622 .compat_ioctl
= lttng_channel_ioctl
,
626 static const struct file_operations lttng_metadata_fops
= {
627 .release
= lttng_channel_release
,
628 .unlocked_ioctl
= lttng_metadata_ioctl
,
630 .compat_ioctl
= lttng_metadata_ioctl
,
635 * lttng_event_ioctl - lttng syscall through ioctl
641 * This ioctl implements lttng commands:
642 * LTTNG_KERNEL_CONTEXT
643 * Prepend a context field to each record of this event
644 * LTTNG_KERNEL_ENABLE
645 * Enable recording for this event (weak enable)
646 * LTTNG_KERNEL_DISABLE
647 * Disable recording for this event (strong disable)
650 long lttng_event_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
652 struct ltt_event
*event
= file
->private_data
;
655 case LTTNG_KERNEL_CONTEXT
:
656 return lttng_abi_add_context(file
,
657 (struct lttng_kernel_context __user
*) arg
,
658 &event
->ctx
, event
->chan
->session
);
659 case LTTNG_KERNEL_ENABLE
:
660 return ltt_event_enable(event
);
661 case LTTNG_KERNEL_DISABLE
:
662 return ltt_event_disable(event
);
669 int lttng_event_release(struct inode
*inode
, struct file
*file
)
671 struct ltt_event
*event
= file
->private_data
;
674 fput(event
->chan
->file
);
678 /* TODO: filter control ioctl */
679 static const struct file_operations lttng_event_fops
= {
680 .release
= lttng_event_release
,
681 .unlocked_ioctl
= lttng_event_ioctl
,
683 .compat_ioctl
= lttng_event_ioctl
,
687 int __init
ltt_debugfs_abi_init(void)
691 wrapper_vmalloc_sync_all();
692 lttng_dentry
= debugfs_create_file("lttng", S_IWUSR
, NULL
, NULL
,
694 if (IS_ERR(lttng_dentry
) || !lttng_dentry
) {
695 printk(KERN_ERR
"Error creating LTTng control file\n");
703 void __exit
ltt_debugfs_abi_exit(void)
705 debugfs_remove(lttng_dentry
);