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_PERF_COUNTER
:
163 context_param
.u
.perf_counter
.name
[LTTNG_SYM_NAME_LEN
- 1] = '\0';
164 return lttng_add_perf_counter_to_ctx(context_param
.u
.perf_counter
.type
,
165 context_param
.u
.perf_counter
.config
,
166 context_param
.u
.perf_counter
.name
,
168 case LTTNG_KERNEL_CONTEXT_COMM
:
169 return lttng_add_comm_to_ctx(ctx
);
176 * lttng_ioctl - lttng syscall through ioctl
182 * This ioctl implements lttng commands:
183 * LTTNG_KERNEL_SESSION
184 * Returns a LTTng trace session file descriptor
185 * LTTNG_KERNEL_TRACER_VERSION
186 * Returns the LTTng kernel tracer version
187 * LTTNG_KERNEL_TRACEPOINT_LIST
188 * Returns a file descriptor listing available tracepoints
190 * The returned session will be deleted when its file descriptor is closed.
193 long lttng_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
196 case LTTNG_KERNEL_SESSION
:
197 return lttng_abi_create_session();
198 case LTTNG_KERNEL_TRACER_VERSION
:
199 return lttng_abi_tracer_version(file
,
200 (struct lttng_kernel_tracer_version __user
*) arg
);
201 case LTTNG_KERNEL_TRACEPOINT_LIST
:
202 return lttng_abi_tracepoint_list();
208 static const struct file_operations lttng_fops
= {
209 .unlocked_ioctl
= lttng_ioctl
,
211 .compat_ioctl
= lttng_ioctl
,
216 * We tolerate no failure in this function (if one happens, we print a dmesg
217 * error, but cannot return any error, because the channel information is
221 void lttng_metadata_create_events(struct file
*channel_file
)
223 struct ltt_channel
*channel
= channel_file
->private_data
;
224 static struct lttng_kernel_event metadata_params
= {
225 .instrumentation
= LTTNG_KERNEL_TRACEPOINT
,
226 .name
= "lttng_metadata",
228 struct ltt_event
*event
;
232 * We tolerate no failure path after event creation. It will stay
233 * invariant for the rest of the session.
235 event
= ltt_event_create(channel
, &metadata_params
, NULL
);
244 return; /* not allowed to return error */
248 int lttng_abi_create_channel(struct file
*session_file
,
249 struct lttng_kernel_channel __user
*uchan_param
,
250 enum channel_type channel_type
)
252 struct ltt_session
*session
= session_file
->private_data
;
253 const struct file_operations
*fops
;
254 const char *transport_name
;
255 struct ltt_channel
*chan
;
256 struct file
*chan_file
;
257 struct lttng_kernel_channel chan_param
;
261 if (copy_from_user(&chan_param
, uchan_param
, sizeof(chan_param
)))
263 chan_fd
= get_unused_fd();
268 chan_file
= anon_inode_getfile("[lttng_channel]",
271 if (IS_ERR(chan_file
)) {
272 ret
= PTR_ERR(chan_file
);
275 switch (channel_type
) {
276 case PER_CPU_CHANNEL
:
277 transport_name
= chan_param
.overwrite
?
278 "relay-overwrite" : "relay-discard";
279 fops
= <tng_channel_fops
;
281 case METADATA_CHANNEL
:
282 transport_name
= "relay-metadata";
283 fops
= <tng_metadata_fops
;
286 transport_name
= "<unknown>";
290 * We tolerate no failure path after channel creation. It will stay
291 * invariant for the rest of the session.
293 chan
= ltt_channel_create(session
, transport_name
, NULL
,
294 chan_param
.subbuf_size
,
295 chan_param
.num_subbuf
,
296 chan_param
.switch_timer_interval
,
297 chan_param
.read_timer_interval
);
302 chan
->file
= chan_file
;
303 chan_file
->private_data
= chan
;
304 fd_install(chan_fd
, chan_file
);
305 if (channel_type
== METADATA_CHANNEL
) {
306 session
->metadata
= chan
;
307 lttng_metadata_create_events(chan_file
);
310 /* The channel created holds a reference on the session */
311 atomic_long_inc(&session_file
->f_count
);
318 put_unused_fd(chan_fd
);
324 * lttng_session_ioctl - lttng session fd ioctl
330 * This ioctl implements lttng commands:
331 * LTTNG_KERNEL_CHANNEL
332 * Returns a LTTng channel file descriptor
333 * LTTNG_KERNEL_ENABLE
334 * Enables tracing for a session (weak enable)
335 * LTTNG_KERNEL_DISABLE
336 * Disables tracing for a session (strong disable)
337 * LTTNG_KERNEL_METADATA
338 * Returns a LTTng metadata file descriptor
340 * The returned channel will be deleted when its file descriptor is closed.
343 long lttng_session_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
345 struct ltt_session
*session
= file
->private_data
;
348 case LTTNG_KERNEL_CHANNEL
:
349 return lttng_abi_create_channel(file
,
350 (struct lttng_kernel_channel __user
*) arg
,
352 case LTTNG_KERNEL_SESSION_START
:
353 case LTTNG_KERNEL_ENABLE
:
354 return ltt_session_enable(session
);
355 case LTTNG_KERNEL_SESSION_STOP
:
356 case LTTNG_KERNEL_DISABLE
:
357 return ltt_session_disable(session
);
358 case LTTNG_KERNEL_METADATA
:
359 return lttng_abi_create_channel(file
,
360 (struct lttng_kernel_channel __user
*) arg
,
368 * Called when the last file reference is dropped.
370 * Big fat note: channels and events are invariant for the whole session after
371 * their creation. So this session destruction also destroys all channel and
372 * event structures specific to this session (they are not destroyed when their
373 * individual file is released).
376 int lttng_session_release(struct inode
*inode
, struct file
*file
)
378 struct ltt_session
*session
= file
->private_data
;
381 ltt_session_destroy(session
);
385 static const struct file_operations lttng_session_fops
= {
386 .release
= lttng_session_release
,
387 .unlocked_ioctl
= lttng_session_ioctl
,
389 .compat_ioctl
= lttng_session_ioctl
,
394 int lttng_abi_open_stream(struct file
*channel_file
)
396 struct ltt_channel
*channel
= channel_file
->private_data
;
397 struct lib_ring_buffer
*buf
;
399 struct file
*stream_file
;
401 buf
= channel
->ops
->buffer_read_open(channel
->chan
);
405 stream_fd
= get_unused_fd();
410 stream_file
= anon_inode_getfile("[lttng_stream]",
411 &lib_ring_buffer_file_operations
,
413 if (IS_ERR(stream_file
)) {
414 ret
= PTR_ERR(stream_file
);
418 * OPEN_FMODE, called within anon_inode_getfile/alloc_file, don't honor
419 * FMODE_LSEEK, FMODE_PREAD nor FMODE_PWRITE. We need to read from this
420 * file descriptor, so we set FMODE_PREAD here.
422 stream_file
->f_mode
|= FMODE_PREAD
;
423 fd_install(stream_fd
, stream_file
);
425 * The stream holds a reference to the channel within the generic ring
426 * buffer library, so no need to hold a refcount on the channel and
427 * session files here.
432 put_unused_fd(stream_fd
);
434 channel
->ops
->buffer_read_close(buf
);
439 int lttng_abi_create_event(struct file
*channel_file
,
440 struct lttng_kernel_event __user
*uevent_param
)
442 struct ltt_channel
*channel
= channel_file
->private_data
;
443 struct ltt_event
*event
;
444 struct lttng_kernel_event event_param
;
446 struct file
*event_file
;
448 if (copy_from_user(&event_param
, uevent_param
, sizeof(event_param
)))
450 event_param
.name
[LTTNG_SYM_NAME_LEN
- 1] = '\0';
451 switch (event_param
.instrumentation
) {
452 case LTTNG_KERNEL_KPROBE
:
453 event_param
.u
.kprobe
.symbol_name
[LTTNG_SYM_NAME_LEN
- 1] = '\0';
455 case LTTNG_KERNEL_FUNCTION
:
456 event_param
.u
.ftrace
.symbol_name
[LTTNG_SYM_NAME_LEN
- 1] = '\0';
461 event_fd
= get_unused_fd();
466 event_file
= anon_inode_getfile("[lttng_event]",
469 if (IS_ERR(event_file
)) {
470 ret
= PTR_ERR(event_file
);
474 * We tolerate no failure path after event creation. It will stay
475 * invariant for the rest of the session.
477 event
= ltt_event_create(channel
, &event_param
, NULL
);
482 event_file
->private_data
= event
;
483 fd_install(event_fd
, event_file
);
484 /* The event holds a reference on the channel */
485 atomic_long_inc(&channel_file
->f_count
);
491 put_unused_fd(event_fd
);
497 * lttng_channel_ioctl - lttng syscall through ioctl
503 * This ioctl implements lttng commands:
504 * LTTNG_KERNEL_STREAM
505 * Returns an event stream file descriptor or failure.
506 * (typically, one event stream records events from one CPU)
508 * Returns an event file descriptor or failure.
509 * LTTNG_KERNEL_CONTEXT
510 * Prepend a context field to each event in the channel
511 * LTTNG_KERNEL_ENABLE
512 * Enable recording for events in this channel (weak enable)
513 * LTTNG_KERNEL_DISABLE
514 * Disable recording for events in this channel (strong disable)
516 * Channel and event file descriptors also hold a reference on the session.
519 long lttng_channel_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
521 struct ltt_channel
*channel
= file
->private_data
;
524 case LTTNG_KERNEL_STREAM
:
525 return lttng_abi_open_stream(file
);
526 case LTTNG_KERNEL_EVENT
:
527 return lttng_abi_create_event(file
, (struct lttng_kernel_event __user
*) arg
);
528 case LTTNG_KERNEL_CONTEXT
:
529 return lttng_abi_add_context(file
,
530 (struct lttng_kernel_context __user
*) arg
,
531 &channel
->ctx
, channel
->session
);
532 case LTTNG_KERNEL_ENABLE
:
533 return ltt_channel_enable(channel
);
534 case LTTNG_KERNEL_DISABLE
:
535 return ltt_channel_disable(channel
);
542 * lttng_metadata_ioctl - lttng syscall through ioctl
548 * This ioctl implements lttng commands:
549 * LTTNG_KERNEL_STREAM
550 * Returns an event stream file descriptor or failure.
552 * Channel and event file descriptors also hold a reference on the session.
555 long lttng_metadata_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
558 case LTTNG_KERNEL_STREAM
:
559 return lttng_abi_open_stream(file
);
566 * lttng_channel_poll - lttng stream addition/removal monitoring
571 unsigned int lttng_channel_poll(struct file
*file
, poll_table
*wait
)
573 struct ltt_channel
*channel
= file
->private_data
;
574 unsigned int mask
= 0;
576 if (file
->f_mode
& FMODE_READ
) {
577 poll_wait_set_exclusive(wait
);
578 poll_wait(file
, channel
->ops
->get_hp_wait_queue(channel
->chan
),
581 if (channel
->ops
->is_disabled(channel
->chan
))
583 if (channel
->ops
->is_finalized(channel
->chan
))
586 return POLLIN
| POLLRDNORM
;
593 int lttng_channel_release(struct inode
*inode
, struct file
*file
)
595 struct ltt_channel
*channel
= file
->private_data
;
598 fput(channel
->session
->file
);
602 static const struct file_operations lttng_channel_fops
= {
603 .release
= lttng_channel_release
,
604 .poll
= lttng_channel_poll
,
605 .unlocked_ioctl
= lttng_channel_ioctl
,
607 .compat_ioctl
= lttng_channel_ioctl
,
611 static const struct file_operations lttng_metadata_fops
= {
612 .release
= lttng_channel_release
,
613 .unlocked_ioctl
= lttng_metadata_ioctl
,
615 .compat_ioctl
= lttng_metadata_ioctl
,
620 * lttng_event_ioctl - lttng syscall through ioctl
626 * This ioctl implements lttng commands:
627 * LTTNG_KERNEL_CONTEXT
628 * Prepend a context field to each record of this event
629 * LTTNG_KERNEL_ENABLE
630 * Enable recording for this event (weak enable)
631 * LTTNG_KERNEL_DISABLE
632 * Disable recording for this event (strong disable)
635 long lttng_event_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
637 struct ltt_event
*event
= file
->private_data
;
640 case LTTNG_KERNEL_CONTEXT
:
641 return lttng_abi_add_context(file
,
642 (struct lttng_kernel_context __user
*) arg
,
643 &event
->ctx
, event
->chan
->session
);
644 case LTTNG_KERNEL_ENABLE
:
645 return ltt_event_enable(event
);
646 case LTTNG_KERNEL_DISABLE
:
647 return ltt_event_disable(event
);
654 int lttng_event_release(struct inode
*inode
, struct file
*file
)
656 struct ltt_event
*event
= file
->private_data
;
659 fput(event
->chan
->file
);
663 /* TODO: filter control ioctl */
664 static const struct file_operations lttng_event_fops
= {
665 .release
= lttng_event_release
,
666 .unlocked_ioctl
= lttng_event_ioctl
,
668 .compat_ioctl
= lttng_event_ioctl
,
672 int __init
ltt_debugfs_abi_init(void)
676 wrapper_vmalloc_sync_all();
677 lttng_dentry
= debugfs_create_file("lttng", S_IWUSR
, NULL
, NULL
,
679 if (IS_ERR(lttng_dentry
) || !lttng_dentry
) {
680 printk(KERN_ERR
"Error creating LTTng control file\n");
688 void __exit
ltt_debugfs_abi_exit(void)
690 debugfs_remove(lttng_dentry
);