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.
25 #include <linux/module.h>
26 #include <linux/debugfs.h>
27 #include <linux/anon_inodes.h>
28 #include <linux/file.h>
29 #include <linux/uaccess.h>
30 #include <linux/slab.h>
31 #include "wrapper/vmalloc.h" /* for wrapper_vmalloc_sync_all() */
32 #include "wrapper/ringbuffer/vfs.h"
33 #include "ltt-debugfs-abi.h"
34 #include "ltt-events.h"
35 #include "ltt-tracer.h"
38 * This is LTTng's own personal way to create a system call as an external
39 * module. We use ioctl() on /sys/kernel/debug/lttng.
42 static struct dentry
*lttng_dentry
;
43 static const struct file_operations lttng_fops
;
44 static const struct file_operations lttng_session_fops
;
45 static const struct file_operations lttng_channel_fops
;
46 static const struct file_operations lttng_metadata_fops
;
47 static const struct file_operations lttng_event_fops
;
55 int lttng_abi_create_session(void)
57 struct ltt_session
*session
;
58 struct file
*session_file
;
61 session
= ltt_session_create();
64 session_fd
= get_unused_fd();
69 session_file
= anon_inode_getfile("[lttng_session]",
72 if (IS_ERR(session_file
)) {
73 ret
= PTR_ERR(session_file
);
76 session
->file
= session_file
;
77 fd_install(session_fd
, session_file
);
81 put_unused_fd(session_fd
);
83 ltt_session_destroy(session
);
88 int lttng_abi_tracepoint_list(void)
90 struct file
*tracepoint_list_file
;
93 file_fd
= get_unused_fd();
99 tracepoint_list_file
= anon_inode_getfile("[lttng_session]",
100 <tng_tracepoint_list_fops
,
102 if (IS_ERR(tracepoint_list_file
)) {
103 ret
= PTR_ERR(tracepoint_list_file
);
106 ret
= lttng_tracepoint_list_fops
.open(NULL
, tracepoint_list_file
);
109 fd_install(file_fd
, tracepoint_list_file
);
117 fput(tracepoint_list_file
);
119 put_unused_fd(file_fd
);
125 long lttng_abi_tracer_version(struct file
*file
,
126 struct lttng_kernel_tracer_version __user
*uversion_param
)
128 struct lttng_kernel_tracer_version v
;
130 v
.version
= LTTNG_VERSION
;
131 v
.patchlevel
= LTTNG_PATCHLEVEL
;
132 v
.sublevel
= LTTNG_SUBLEVEL
;
134 if (copy_to_user(uversion_param
, &v
, sizeof(v
)))
140 long lttng_abi_add_context(struct file
*file
,
141 struct lttng_kernel_context __user
*ucontext_param
,
142 struct lttng_ctx
**ctx
, struct ltt_session
*session
)
144 struct lttng_kernel_context context_param
;
146 if (session
->been_active
)
149 if (copy_from_user(&context_param
, ucontext_param
, sizeof(context_param
)))
152 switch (context_param
.ctx
) {
153 case LTTNG_KERNEL_CONTEXT_PID
:
154 return lttng_add_pid_to_ctx(ctx
);
155 case LTTNG_KERNEL_CONTEXT_PRIO
:
156 return lttng_add_prio_to_ctx(ctx
);
157 case LTTNG_KERNEL_CONTEXT_NICE
:
158 return lttng_add_nice_to_ctx(ctx
);
159 case LTTNG_KERNEL_CONTEXT_PERF_COUNTER
:
161 case LTTNG_KERNEL_CONTEXT_COMM
:
162 return lttng_add_comm_to_ctx(ctx
);
169 * lttng_ioctl - lttng syscall through ioctl
175 * This ioctl implements lttng commands:
176 * LTTNG_KERNEL_SESSION
177 * Returns a LTTng trace session file descriptor
178 * LTTNG_KERNEL_TRACER_VERSION
179 * Returns the LTTng kernel tracer version
180 * LTTNG_KERNEL_TRACEPOINT_LIST
181 * Returns a file descriptor listing available tracepoints
183 * The returned session will be deleted when its file descriptor is closed.
186 long lttng_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
189 case LTTNG_KERNEL_SESSION
:
190 return lttng_abi_create_session();
191 case LTTNG_KERNEL_TRACER_VERSION
:
192 return lttng_abi_tracer_version(file
,
193 (struct lttng_kernel_tracer_version __user
*) arg
);
194 case LTTNG_KERNEL_TRACEPOINT_LIST
:
195 return lttng_abi_tracepoint_list();
201 static const struct file_operations lttng_fops
= {
202 .unlocked_ioctl
= lttng_ioctl
,
204 .compat_ioctl
= lttng_ioctl
,
209 * We tolerate no failure in this function (if one happens, we print a dmesg
210 * error, but cannot return any error, because the channel information is
214 void lttng_metadata_create_events(struct file
*channel_file
)
216 struct ltt_channel
*channel
= channel_file
->private_data
;
217 static struct lttng_kernel_event metadata_params
= {
218 .instrumentation
= LTTNG_KERNEL_TRACEPOINT
,
219 .name
= "lttng_metadata",
221 struct ltt_event
*event
;
225 * We tolerate no failure path after event creation. It will stay
226 * invariant for the rest of the session.
228 event
= ltt_event_create(channel
, &metadata_params
, NULL
);
237 return; /* not allowed to return error */
241 int lttng_abi_create_channel(struct file
*session_file
,
242 struct lttng_kernel_channel __user
*uchan_param
,
243 enum channel_type channel_type
)
245 struct ltt_session
*session
= session_file
->private_data
;
246 const struct file_operations
*fops
;
247 const char *transport_name
;
248 struct ltt_channel
*chan
;
249 struct file
*chan_file
;
250 struct lttng_kernel_channel chan_param
;
254 if (copy_from_user(&chan_param
, uchan_param
, sizeof(chan_param
)))
256 chan_fd
= get_unused_fd();
261 chan_file
= anon_inode_getfile("[lttng_channel]",
264 if (IS_ERR(chan_file
)) {
265 ret
= PTR_ERR(chan_file
);
268 switch (channel_type
) {
269 case PER_CPU_CHANNEL
:
270 transport_name
= chan_param
.overwrite
?
271 "relay-overwrite" : "relay-discard";
272 fops
= <tng_channel_fops
;
274 case METADATA_CHANNEL
:
275 transport_name
= "relay-metadata";
276 fops
= <tng_metadata_fops
;
279 transport_name
= "<unknown>";
283 * We tolerate no failure path after channel creation. It will stay
284 * invariant for the rest of the session.
286 chan
= ltt_channel_create(session
, transport_name
, NULL
,
287 chan_param
.subbuf_size
,
288 chan_param
.num_subbuf
,
289 chan_param
.switch_timer_interval
,
290 chan_param
.read_timer_interval
);
295 chan
->file
= chan_file
;
296 chan_file
->private_data
= chan
;
297 fd_install(chan_fd
, chan_file
);
298 if (channel_type
== METADATA_CHANNEL
) {
299 session
->metadata
= chan
;
300 lttng_metadata_create_events(chan_file
);
303 /* The channel created holds a reference on the session */
304 atomic_long_inc(&session_file
->f_count
);
311 put_unused_fd(chan_fd
);
317 * lttng_session_ioctl - lttng session fd ioctl
323 * This ioctl implements lttng commands:
324 * LTTNG_KERNEL_CHANNEL
325 * Returns a LTTng channel file descriptor
326 * LTTNG_KERNEL_SESSION_START
327 * Starts tracing session
328 * LTTNG_KERNEL_SESSION_STOP
329 * Stops tracing session
330 * LTTNG_KERNEL_METADATA
331 * Returns a LTTng metadata file descriptor
333 * The returned channel will be deleted when its file descriptor is closed.
336 long lttng_session_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
338 struct ltt_session
*session
= file
->private_data
;
341 case LTTNG_KERNEL_CHANNEL
:
342 return lttng_abi_create_channel(file
,
343 (struct lttng_kernel_channel __user
*) arg
,
345 case LTTNG_KERNEL_SESSION_START
:
346 return ltt_session_start(session
);
347 case LTTNG_KERNEL_SESSION_STOP
:
348 return ltt_session_stop(session
);
349 case LTTNG_KERNEL_METADATA
:
350 return lttng_abi_create_channel(file
,
351 (struct lttng_kernel_channel __user
*) arg
,
359 * Called when the last file reference is dropped.
361 * Big fat note: channels and events are invariant for the whole session after
362 * their creation. So this session destruction also destroys all channel and
363 * event structures specific to this session (they are not destroyed when their
364 * individual file is released).
367 int lttng_session_release(struct inode
*inode
, struct file
*file
)
369 struct ltt_session
*session
= file
->private_data
;
372 ltt_session_destroy(session
);
376 static const struct file_operations lttng_session_fops
= {
377 .release
= lttng_session_release
,
378 .unlocked_ioctl
= lttng_session_ioctl
,
380 .compat_ioctl
= lttng_session_ioctl
,
385 int lttng_abi_open_stream(struct file
*channel_file
)
387 struct ltt_channel
*channel
= channel_file
->private_data
;
388 struct lib_ring_buffer
*buf
;
390 struct file
*stream_file
;
392 buf
= channel
->ops
->buffer_read_open(channel
->chan
);
396 stream_fd
= get_unused_fd();
401 stream_file
= anon_inode_getfile("[lttng_stream]",
402 &lib_ring_buffer_file_operations
,
404 if (IS_ERR(stream_file
)) {
405 ret
= PTR_ERR(stream_file
);
409 * OPEN_FMODE, called within anon_inode_getfile/alloc_file, don't honor
410 * FMODE_LSEEK, FMODE_PREAD nor FMODE_PWRITE. We need to read from this
411 * file descriptor, so we set FMODE_PREAD here.
413 stream_file
->f_mode
|= FMODE_PREAD
;
414 fd_install(stream_fd
, stream_file
);
416 * The stream holds a reference to the channel within the generic ring
417 * buffer library, so no need to hold a refcount on the channel and
418 * session files here.
423 put_unused_fd(stream_fd
);
425 channel
->ops
->buffer_read_close(buf
);
430 int lttng_abi_create_event(struct file
*channel_file
,
431 struct lttng_kernel_event __user
*uevent_param
)
433 struct ltt_channel
*channel
= channel_file
->private_data
;
434 struct ltt_event
*event
;
435 struct lttng_kernel_event event_param
;
437 struct file
*event_file
;
439 if (copy_from_user(&event_param
, uevent_param
, sizeof(event_param
)))
441 event_param
.name
[LTTNG_SYM_NAME_LEN
- 1] = '\0';
442 switch (event_param
.instrumentation
) {
443 case LTTNG_KERNEL_KPROBE
:
444 event_param
.u
.kprobe
.symbol_name
[LTTNG_SYM_NAME_LEN
- 1] = '\0';
446 case LTTNG_KERNEL_FUNCTION
:
447 event_param
.u
.ftrace
.symbol_name
[LTTNG_SYM_NAME_LEN
- 1] = '\0';
452 event_fd
= get_unused_fd();
457 event_file
= anon_inode_getfile("[lttng_event]",
460 if (IS_ERR(event_file
)) {
461 ret
= PTR_ERR(event_file
);
465 * We tolerate no failure path after event creation. It will stay
466 * invariant for the rest of the session.
468 event
= ltt_event_create(channel
, &event_param
, NULL
);
473 event_file
->private_data
= event
;
474 fd_install(event_fd
, event_file
);
475 /* The event holds a reference on the channel */
476 atomic_long_inc(&channel_file
->f_count
);
482 put_unused_fd(event_fd
);
488 * lttng_channel_ioctl - lttng syscall through ioctl
494 * This ioctl implements lttng commands:
495 * LTTNG_KERNEL_STREAM
496 * Returns an event stream file descriptor or failure.
497 * (typically, one event stream records events from one CPU)
499 * Returns an event file descriptor or failure.
500 * LTTNG_KERNEL_CONTEXT
501 * Prepend a context field to each event in the channel
503 * Channel and event file descriptors also hold a reference on the session.
506 long lttng_channel_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
508 struct ltt_channel
*channel
= file
->private_data
;
511 case LTTNG_KERNEL_STREAM
:
512 return lttng_abi_open_stream(file
);
513 case LTTNG_KERNEL_EVENT
:
514 return lttng_abi_create_event(file
, (struct lttng_kernel_event __user
*) arg
);
515 case LTTNG_KERNEL_CONTEXT
:
516 return lttng_abi_add_context(file
,
517 (struct lttng_kernel_context __user
*) arg
,
518 &channel
->ctx
, channel
->session
);
525 * lttng_metadata_ioctl - lttng syscall through ioctl
531 * This ioctl implements lttng commands:
532 * LTTNG_KERNEL_STREAM
533 * Returns an event stream file descriptor or failure.
535 * Channel and event file descriptors also hold a reference on the session.
538 long lttng_metadata_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
541 case LTTNG_KERNEL_STREAM
:
542 return lttng_abi_open_stream(file
);
551 * lttng_channel_poll - lttng stream addition/removal monitoring
556 unsigned int lttng_channel_poll(struct file
*file
, poll_table
*wait
)
558 struct ltt_channel
*channel
= file
->private_data
;
559 unsigned int mask
= 0;
561 if (file
->f_mode
& FMODE_READ
) {
562 poll_wait_set_exclusive(wait
);
563 poll_wait(file
, &channel
->notify_wait
, wait
);
565 /* TODO: identify when the channel is being finalized. */
569 return POLLIN
| POLLRDNORM
;
577 int lttng_channel_release(struct inode
*inode
, struct file
*file
)
579 struct ltt_channel
*channel
= file
->private_data
;
582 fput(channel
->session
->file
);
586 static const struct file_operations lttng_channel_fops
= {
587 .release
= lttng_channel_release
,
590 .poll
= lttng_channel_poll
,
592 .unlocked_ioctl
= lttng_channel_ioctl
,
594 .compat_ioctl
= lttng_channel_ioctl
,
598 static const struct file_operations lttng_metadata_fops
= {
599 .release
= lttng_channel_release
,
600 .unlocked_ioctl
= lttng_metadata_ioctl
,
602 .compat_ioctl
= lttng_metadata_ioctl
,
607 * lttng_event_ioctl - lttng syscall through ioctl
613 * This ioctl implements lttng commands:
614 * LTTNG_KERNEL_STREAM
615 * Returns an event stream file descriptor or failure.
616 * (typically, one event stream records events from one CPU)
618 * Returns an event file descriptor or failure.
619 * LTTNG_KERNEL_CONTEXT
620 * Prepend a context field to each record of this event
623 long lttng_event_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
625 struct ltt_event
*event
= file
->private_data
;
628 case LTTNG_KERNEL_CONTEXT
:
629 return lttng_abi_add_context(file
,
630 (struct lttng_kernel_context __user
*) arg
,
631 &event
->ctx
, event
->chan
->session
);
638 int lttng_event_release(struct inode
*inode
, struct file
*file
)
640 struct ltt_event
*event
= file
->private_data
;
643 fput(event
->chan
->file
);
647 /* TODO: filter control ioctl */
648 static const struct file_operations lttng_event_fops
= {
649 .release
= lttng_event_release
,
650 .unlocked_ioctl
= lttng_event_ioctl
,
652 .compat_ioctl
= lttng_event_ioctl
,
656 int __init
ltt_debugfs_abi_init(void)
660 wrapper_vmalloc_sync_all();
661 lttng_dentry
= debugfs_create_file("lttng", S_IWUSR
, NULL
, NULL
,
663 if (IS_ERR(lttng_dentry
) || !lttng_dentry
) {
664 printk(KERN_ERR
"Error creating LTTng control file\n");
672 void __exit
ltt_debugfs_abi_exit(void)
674 debugfs_remove(lttng_dentry
);