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 "ltt-debugfs-abi.h"
36 #include "ltt-events.h"
37 #include "ltt-tracer.h"
40 * This is LTTng's own personal way to create a system call as an external
41 * module. We use ioctl() on /sys/kernel/debug/lttng.
44 static struct dentry
*lttng_dentry
;
45 static const struct file_operations lttng_fops
;
46 static const struct file_operations lttng_session_fops
;
47 static const struct file_operations lttng_channel_fops
;
48 static const struct file_operations lttng_metadata_fops
;
49 static const struct file_operations lttng_event_fops
;
57 int lttng_abi_create_session(void)
59 struct ltt_session
*session
;
60 struct file
*session_file
;
63 session
= ltt_session_create();
66 session_fd
= get_unused_fd();
71 session_file
= anon_inode_getfile("[lttng_session]",
74 if (IS_ERR(session_file
)) {
75 ret
= PTR_ERR(session_file
);
78 session
->file
= session_file
;
79 fd_install(session_fd
, session_file
);
83 put_unused_fd(session_fd
);
85 ltt_session_destroy(session
);
90 int lttng_abi_tracepoint_list(void)
92 struct file
*tracepoint_list_file
;
95 file_fd
= get_unused_fd();
101 tracepoint_list_file
= anon_inode_getfile("[lttng_session]",
102 <tng_tracepoint_list_fops
,
104 if (IS_ERR(tracepoint_list_file
)) {
105 ret
= PTR_ERR(tracepoint_list_file
);
108 ret
= lttng_tracepoint_list_fops
.open(NULL
, tracepoint_list_file
);
111 fd_install(file_fd
, tracepoint_list_file
);
119 fput(tracepoint_list_file
);
121 put_unused_fd(file_fd
);
127 long lttng_abi_tracer_version(struct file
*file
,
128 struct lttng_kernel_tracer_version __user
*uversion_param
)
130 struct lttng_kernel_tracer_version v
;
132 v
.version
= LTTNG_VERSION
;
133 v
.patchlevel
= LTTNG_PATCHLEVEL
;
134 v
.sublevel
= LTTNG_SUBLEVEL
;
136 if (copy_to_user(uversion_param
, &v
, sizeof(v
)))
142 long lttng_abi_add_context(struct file
*file
,
143 struct lttng_kernel_context __user
*ucontext_param
,
144 struct lttng_ctx
**ctx
, struct ltt_session
*session
)
146 struct lttng_kernel_context context_param
;
148 if (session
->been_active
)
151 if (copy_from_user(&context_param
, ucontext_param
, sizeof(context_param
)))
154 switch (context_param
.ctx
) {
155 case LTTNG_KERNEL_CONTEXT_PID
:
156 return lttng_add_pid_to_ctx(ctx
);
157 case LTTNG_KERNEL_CONTEXT_PRIO
:
158 return lttng_add_prio_to_ctx(ctx
);
159 case LTTNG_KERNEL_CONTEXT_NICE
:
160 return lttng_add_nice_to_ctx(ctx
);
161 case LTTNG_KERNEL_CONTEXT_PERF_COUNTER
:
162 context_param
.u
.perf_counter
.name
[LTTNG_SYM_NAME_LEN
- 1] = '\0';
163 return lttng_add_perf_counter_to_ctx(context_param
.u
.perf_counter
.type
,
164 context_param
.u
.perf_counter
.config
,
165 context_param
.u
.perf_counter
.name
,
167 case LTTNG_KERNEL_CONTEXT_COMM
:
168 return lttng_add_comm_to_ctx(ctx
);
175 * lttng_ioctl - lttng syscall through ioctl
181 * This ioctl implements lttng commands:
182 * LTTNG_KERNEL_SESSION
183 * Returns a LTTng trace session file descriptor
184 * LTTNG_KERNEL_TRACER_VERSION
185 * Returns the LTTng kernel tracer version
186 * LTTNG_KERNEL_TRACEPOINT_LIST
187 * Returns a file descriptor listing available tracepoints
189 * The returned session will be deleted when its file descriptor is closed.
192 long lttng_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
195 case LTTNG_KERNEL_SESSION
:
196 return lttng_abi_create_session();
197 case LTTNG_KERNEL_TRACER_VERSION
:
198 return lttng_abi_tracer_version(file
,
199 (struct lttng_kernel_tracer_version __user
*) arg
);
200 case LTTNG_KERNEL_TRACEPOINT_LIST
:
201 return lttng_abi_tracepoint_list();
207 static const struct file_operations lttng_fops
= {
208 .unlocked_ioctl
= lttng_ioctl
,
210 .compat_ioctl
= lttng_ioctl
,
215 * We tolerate no failure in this function (if one happens, we print a dmesg
216 * error, but cannot return any error, because the channel information is
220 void lttng_metadata_create_events(struct file
*channel_file
)
222 struct ltt_channel
*channel
= channel_file
->private_data
;
223 static struct lttng_kernel_event metadata_params
= {
224 .instrumentation
= LTTNG_KERNEL_TRACEPOINT
,
225 .name
= "lttng_metadata",
227 struct ltt_event
*event
;
231 * We tolerate no failure path after event creation. It will stay
232 * invariant for the rest of the session.
234 event
= ltt_event_create(channel
, &metadata_params
, NULL
);
243 return; /* not allowed to return error */
247 int lttng_abi_create_channel(struct file
*session_file
,
248 struct lttng_kernel_channel __user
*uchan_param
,
249 enum channel_type channel_type
)
251 struct ltt_session
*session
= session_file
->private_data
;
252 const struct file_operations
*fops
;
253 const char *transport_name
;
254 struct ltt_channel
*chan
;
255 struct file
*chan_file
;
256 struct lttng_kernel_channel chan_param
;
260 if (copy_from_user(&chan_param
, uchan_param
, sizeof(chan_param
)))
262 chan_fd
= get_unused_fd();
267 chan_file
= anon_inode_getfile("[lttng_channel]",
270 if (IS_ERR(chan_file
)) {
271 ret
= PTR_ERR(chan_file
);
274 switch (channel_type
) {
275 case PER_CPU_CHANNEL
:
276 transport_name
= chan_param
.overwrite
?
277 "relay-overwrite" : "relay-discard";
278 fops
= <tng_channel_fops
;
280 case METADATA_CHANNEL
:
281 transport_name
= "relay-metadata";
282 fops
= <tng_metadata_fops
;
285 transport_name
= "<unknown>";
289 * We tolerate no failure path after channel creation. It will stay
290 * invariant for the rest of the session.
292 chan
= ltt_channel_create(session
, transport_name
, NULL
,
293 chan_param
.subbuf_size
,
294 chan_param
.num_subbuf
,
295 chan_param
.switch_timer_interval
,
296 chan_param
.read_timer_interval
);
301 chan
->file
= chan_file
;
302 chan_file
->private_data
= chan
;
303 fd_install(chan_fd
, chan_file
);
304 if (channel_type
== METADATA_CHANNEL
) {
305 session
->metadata
= chan
;
306 lttng_metadata_create_events(chan_file
);
309 /* The channel created holds a reference on the session */
310 atomic_long_inc(&session_file
->f_count
);
317 put_unused_fd(chan_fd
);
323 * lttng_session_ioctl - lttng session fd ioctl
329 * This ioctl implements lttng commands:
330 * LTTNG_KERNEL_CHANNEL
331 * Returns a LTTng channel file descriptor
332 * LTTNG_KERNEL_SESSION_START
333 * Starts tracing session
334 * LTTNG_KERNEL_SESSION_STOP
335 * Stops tracing session
336 * LTTNG_KERNEL_METADATA
337 * Returns a LTTng metadata file descriptor
339 * The returned channel will be deleted when its file descriptor is closed.
342 long lttng_session_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
344 struct ltt_session
*session
= file
->private_data
;
347 case LTTNG_KERNEL_CHANNEL
:
348 return lttng_abi_create_channel(file
,
349 (struct lttng_kernel_channel __user
*) arg
,
351 case LTTNG_KERNEL_SESSION_START
:
352 return ltt_session_start(session
);
353 case LTTNG_KERNEL_SESSION_STOP
:
354 return ltt_session_stop(session
);
355 case LTTNG_KERNEL_METADATA
:
356 return lttng_abi_create_channel(file
,
357 (struct lttng_kernel_channel __user
*) arg
,
365 * Called when the last file reference is dropped.
367 * Big fat note: channels and events are invariant for the whole session after
368 * their creation. So this session destruction also destroys all channel and
369 * event structures specific to this session (they are not destroyed when their
370 * individual file is released).
373 int lttng_session_release(struct inode
*inode
, struct file
*file
)
375 struct ltt_session
*session
= file
->private_data
;
378 ltt_session_destroy(session
);
382 static const struct file_operations lttng_session_fops
= {
383 .release
= lttng_session_release
,
384 .unlocked_ioctl
= lttng_session_ioctl
,
386 .compat_ioctl
= lttng_session_ioctl
,
391 int lttng_abi_open_stream(struct file
*channel_file
)
393 struct ltt_channel
*channel
= channel_file
->private_data
;
394 struct lib_ring_buffer
*buf
;
396 struct file
*stream_file
;
398 buf
= channel
->ops
->buffer_read_open(channel
->chan
);
402 stream_fd
= get_unused_fd();
407 stream_file
= anon_inode_getfile("[lttng_stream]",
408 &lib_ring_buffer_file_operations
,
410 if (IS_ERR(stream_file
)) {
411 ret
= PTR_ERR(stream_file
);
415 * OPEN_FMODE, called within anon_inode_getfile/alloc_file, don't honor
416 * FMODE_LSEEK, FMODE_PREAD nor FMODE_PWRITE. We need to read from this
417 * file descriptor, so we set FMODE_PREAD here.
419 stream_file
->f_mode
|= FMODE_PREAD
;
420 fd_install(stream_fd
, stream_file
);
422 * The stream holds a reference to the channel within the generic ring
423 * buffer library, so no need to hold a refcount on the channel and
424 * session files here.
429 put_unused_fd(stream_fd
);
431 channel
->ops
->buffer_read_close(buf
);
436 int lttng_abi_create_event(struct file
*channel_file
,
437 struct lttng_kernel_event __user
*uevent_param
)
439 struct ltt_channel
*channel
= channel_file
->private_data
;
440 struct ltt_event
*event
;
441 struct lttng_kernel_event event_param
;
443 struct file
*event_file
;
445 if (copy_from_user(&event_param
, uevent_param
, sizeof(event_param
)))
447 event_param
.name
[LTTNG_SYM_NAME_LEN
- 1] = '\0';
448 switch (event_param
.instrumentation
) {
449 case LTTNG_KERNEL_KPROBE
:
450 event_param
.u
.kprobe
.symbol_name
[LTTNG_SYM_NAME_LEN
- 1] = '\0';
452 case LTTNG_KERNEL_FUNCTION
:
453 event_param
.u
.ftrace
.symbol_name
[LTTNG_SYM_NAME_LEN
- 1] = '\0';
458 event_fd
= get_unused_fd();
463 event_file
= anon_inode_getfile("[lttng_event]",
466 if (IS_ERR(event_file
)) {
467 ret
= PTR_ERR(event_file
);
471 * We tolerate no failure path after event creation. It will stay
472 * invariant for the rest of the session.
474 event
= ltt_event_create(channel
, &event_param
, NULL
);
479 event_file
->private_data
= event
;
480 fd_install(event_fd
, event_file
);
481 /* The event holds a reference on the channel */
482 atomic_long_inc(&channel_file
->f_count
);
488 put_unused_fd(event_fd
);
494 * lttng_channel_ioctl - lttng syscall through ioctl
500 * This ioctl implements lttng commands:
501 * LTTNG_KERNEL_STREAM
502 * Returns an event stream file descriptor or failure.
503 * (typically, one event stream records events from one CPU)
505 * Returns an event file descriptor or failure.
506 * LTTNG_KERNEL_CONTEXT
507 * Prepend a context field to each event in the channel
509 * Channel and event file descriptors also hold a reference on the session.
512 long lttng_channel_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
514 struct ltt_channel
*channel
= file
->private_data
;
517 case LTTNG_KERNEL_STREAM
:
518 return lttng_abi_open_stream(file
);
519 case LTTNG_KERNEL_EVENT
:
520 return lttng_abi_create_event(file
, (struct lttng_kernel_event __user
*) arg
);
521 case LTTNG_KERNEL_CONTEXT
:
522 return lttng_abi_add_context(file
,
523 (struct lttng_kernel_context __user
*) arg
,
524 &channel
->ctx
, channel
->session
);
531 * lttng_metadata_ioctl - lttng syscall through ioctl
537 * This ioctl implements lttng commands:
538 * LTTNG_KERNEL_STREAM
539 * Returns an event stream file descriptor or failure.
541 * Channel and event file descriptors also hold a reference on the session.
544 long lttng_metadata_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
547 case LTTNG_KERNEL_STREAM
:
548 return lttng_abi_open_stream(file
);
557 * lttng_channel_poll - lttng stream addition/removal monitoring
562 unsigned int lttng_channel_poll(struct file
*file
, poll_table
*wait
)
564 struct ltt_channel
*channel
= file
->private_data
;
565 unsigned int mask
= 0;
567 if (file
->f_mode
& FMODE_READ
) {
568 poll_wait_set_exclusive(wait
);
569 poll_wait(file
, &channel
->notify_wait
, wait
);
571 /* TODO: identify when the channel is being finalized. */
575 return POLLIN
| POLLRDNORM
;
583 int lttng_channel_release(struct inode
*inode
, struct file
*file
)
585 struct ltt_channel
*channel
= file
->private_data
;
588 fput(channel
->session
->file
);
592 static const struct file_operations lttng_channel_fops
= {
593 .release
= lttng_channel_release
,
596 .poll
= lttng_channel_poll
,
598 .unlocked_ioctl
= lttng_channel_ioctl
,
600 .compat_ioctl
= lttng_channel_ioctl
,
604 static const struct file_operations lttng_metadata_fops
= {
605 .release
= lttng_channel_release
,
606 .unlocked_ioctl
= lttng_metadata_ioctl
,
608 .compat_ioctl
= lttng_metadata_ioctl
,
613 * lttng_event_ioctl - lttng syscall through ioctl
619 * This ioctl implements lttng commands:
620 * LTTNG_KERNEL_STREAM
621 * Returns an event stream file descriptor or failure.
622 * (typically, one event stream records events from one CPU)
624 * Returns an event file descriptor or failure.
625 * LTTNG_KERNEL_CONTEXT
626 * Prepend a context field to each record of this event
629 long lttng_event_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
631 struct ltt_event
*event
= file
->private_data
;
634 case LTTNG_KERNEL_CONTEXT
:
635 return lttng_abi_add_context(file
,
636 (struct lttng_kernel_context __user
*) arg
,
637 &event
->ctx
, event
->chan
->session
);
644 int lttng_event_release(struct inode
*inode
, struct file
*file
)
646 struct ltt_event
*event
= file
->private_data
;
649 fput(event
->chan
->file
);
653 /* TODO: filter control ioctl */
654 static const struct file_operations lttng_event_fops
= {
655 .release
= lttng_event_release
,
656 .unlocked_ioctl
= lttng_event_ioctl
,
658 .compat_ioctl
= lttng_event_ioctl
,
662 int __init
ltt_debugfs_abi_init(void)
666 wrapper_vmalloc_sync_all();
667 lttng_dentry
= debugfs_create_file("lttng", S_IWUSR
, NULL
, NULL
,
669 if (IS_ERR(lttng_dentry
) || !lttng_dentry
) {
670 printk(KERN_ERR
"Error creating LTTng control file\n");
678 void __exit
ltt_debugfs_abi_exit(void)
680 debugfs_remove(lttng_dentry
);