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_CONTEXT_PID
:
154 return lttng_add_pid_to_ctx(ctx
);
155 case LTTNG_CONTEXT_PERF_COUNTER
:
163 * lttng_ioctl - lttng syscall through ioctl
169 * This ioctl implements lttng commands:
170 * LTTNG_KERNEL_SESSION
171 * Returns a LTTng trace session file descriptor
172 * LTTNG_KERNEL_TRACER_VERSION
173 * Returns the LTTng kernel tracer version
174 * LTTNG_KERNEL_TRACEPOINT_LIST
175 * Returns a file descriptor listing available tracepoints
177 * The returned session will be deleted when its file descriptor is closed.
180 long lttng_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
183 case LTTNG_KERNEL_SESSION
:
184 return lttng_abi_create_session();
185 case LTTNG_KERNEL_TRACER_VERSION
:
186 return lttng_abi_tracer_version(file
,
187 (struct lttng_kernel_tracer_version __user
*) arg
);
188 case LTTNG_KERNEL_TRACEPOINT_LIST
:
189 return lttng_abi_tracepoint_list();
195 static const struct file_operations lttng_fops
= {
196 .unlocked_ioctl
= lttng_ioctl
,
198 .compat_ioctl
= lttng_ioctl
,
203 * We tolerate no failure in this function (if one happens, we print a dmesg
204 * error, but cannot return any error, because the channel information is
208 void lttng_metadata_create_events(struct file
*channel_file
)
210 struct ltt_channel
*channel
= channel_file
->private_data
;
211 static struct lttng_kernel_event metadata_params
= {
212 .instrumentation
= LTTNG_KERNEL_TRACEPOINT
,
213 .name
= "lttng_metadata",
215 struct ltt_event
*event
;
219 * We tolerate no failure path after event creation. It will stay
220 * invariant for the rest of the session.
222 event
= ltt_event_create(channel
, &metadata_params
, NULL
);
231 return; /* not allowed to return error */
235 int lttng_abi_create_channel(struct file
*session_file
,
236 struct lttng_kernel_channel __user
*uchan_param
,
237 enum channel_type channel_type
)
239 struct ltt_session
*session
= session_file
->private_data
;
240 const struct file_operations
*fops
;
241 const char *transport_name
;
242 struct ltt_channel
*chan
;
243 struct file
*chan_file
;
244 struct lttng_kernel_channel chan_param
;
248 if (copy_from_user(&chan_param
, uchan_param
, sizeof(chan_param
)))
250 chan_fd
= get_unused_fd();
255 chan_file
= anon_inode_getfile("[lttng_channel]",
258 if (IS_ERR(chan_file
)) {
259 ret
= PTR_ERR(chan_file
);
262 switch (channel_type
) {
263 case PER_CPU_CHANNEL
:
264 transport_name
= chan_param
.overwrite
?
265 "relay-overwrite" : "relay-discard";
266 fops
= <tng_channel_fops
;
268 case METADATA_CHANNEL
:
269 transport_name
= "relay-metadata";
270 fops
= <tng_metadata_fops
;
273 transport_name
= "<unknown>";
277 * We tolerate no failure path after channel creation. It will stay
278 * invariant for the rest of the session.
280 chan
= ltt_channel_create(session
, transport_name
, NULL
,
281 chan_param
.subbuf_size
,
282 chan_param
.num_subbuf
,
283 chan_param
.switch_timer_interval
,
284 chan_param
.read_timer_interval
);
289 chan
->file
= chan_file
;
290 chan_file
->private_data
= chan
;
291 fd_install(chan_fd
, chan_file
);
292 if (channel_type
== METADATA_CHANNEL
) {
293 session
->metadata
= chan
;
294 lttng_metadata_create_events(chan_file
);
297 /* The channel created holds a reference on the session */
298 atomic_long_inc(&session_file
->f_count
);
305 put_unused_fd(chan_fd
);
311 * lttng_session_ioctl - lttng session fd ioctl
317 * This ioctl implements lttng commands:
318 * LTTNG_KERNEL_CHANNEL
319 * Returns a LTTng channel file descriptor
320 * LTTNG_KERNEL_SESSION_START
321 * Starts tracing session
322 * LTTNG_KERNEL_SESSION_STOP
323 * Stops tracing session
324 * LTTNG_KERNEL_METADATA
325 * Returns a LTTng metadata file descriptor
327 * The returned channel will be deleted when its file descriptor is closed.
330 long lttng_session_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
332 struct ltt_session
*session
= file
->private_data
;
335 case LTTNG_KERNEL_CHANNEL
:
336 return lttng_abi_create_channel(file
,
337 (struct lttng_kernel_channel __user
*) arg
,
339 case LTTNG_KERNEL_SESSION_START
:
340 return ltt_session_start(session
);
341 case LTTNG_KERNEL_SESSION_STOP
:
342 return ltt_session_stop(session
);
343 case LTTNG_KERNEL_METADATA
:
344 return lttng_abi_create_channel(file
,
345 (struct lttng_kernel_channel __user
*) arg
,
353 * Called when the last file reference is dropped.
355 * Big fat note: channels and events are invariant for the whole session after
356 * their creation. So this session destruction also destroys all channel and
357 * event structures specific to this session (they are not destroyed when their
358 * individual file is released).
361 int lttng_session_release(struct inode
*inode
, struct file
*file
)
363 struct ltt_session
*session
= file
->private_data
;
366 ltt_session_destroy(session
);
370 static const struct file_operations lttng_session_fops
= {
371 .release
= lttng_session_release
,
372 .unlocked_ioctl
= lttng_session_ioctl
,
374 .compat_ioctl
= lttng_session_ioctl
,
379 int lttng_abi_open_stream(struct file
*channel_file
)
381 struct ltt_channel
*channel
= channel_file
->private_data
;
382 struct lib_ring_buffer
*buf
;
384 struct file
*stream_file
;
386 buf
= channel
->ops
->buffer_read_open(channel
->chan
);
390 stream_fd
= get_unused_fd();
395 stream_file
= anon_inode_getfile("[lttng_stream]",
396 &lib_ring_buffer_file_operations
,
398 if (IS_ERR(stream_file
)) {
399 ret
= PTR_ERR(stream_file
);
403 * OPEN_FMODE, called within anon_inode_getfile/alloc_file, don't honor
404 * FMODE_LSEEK, FMODE_PREAD nor FMODE_PWRITE. We need to read from this
405 * file descriptor, so we set FMODE_PREAD here.
407 stream_file
->f_mode
|= FMODE_PREAD
;
408 fd_install(stream_fd
, stream_file
);
410 * The stream holds a reference to the channel within the generic ring
411 * buffer library, so no need to hold a refcount on the channel and
412 * session files here.
417 put_unused_fd(stream_fd
);
419 channel
->ops
->buffer_read_close(buf
);
424 int lttng_abi_create_event(struct file
*channel_file
,
425 struct lttng_kernel_event __user
*uevent_param
)
427 struct ltt_channel
*channel
= channel_file
->private_data
;
428 struct ltt_event
*event
;
429 struct lttng_kernel_event event_param
;
431 struct file
*event_file
;
433 if (copy_from_user(&event_param
, uevent_param
, sizeof(event_param
)))
435 event_param
.name
[LTTNG_SYM_NAME_LEN
- 1] = '\0';
436 switch (event_param
.instrumentation
) {
437 case LTTNG_KERNEL_KPROBE
:
438 event_param
.u
.kprobe
.symbol_name
[LTTNG_SYM_NAME_LEN
- 1] = '\0';
440 case LTTNG_KERNEL_FUNCTION
:
441 event_param
.u
.ftrace
.symbol_name
[LTTNG_SYM_NAME_LEN
- 1] = '\0';
446 event_fd
= get_unused_fd();
451 event_file
= anon_inode_getfile("[lttng_event]",
454 if (IS_ERR(event_file
)) {
455 ret
= PTR_ERR(event_file
);
459 * We tolerate no failure path after event creation. It will stay
460 * invariant for the rest of the session.
462 event
= ltt_event_create(channel
, &event_param
, NULL
);
467 event_file
->private_data
= event
;
468 fd_install(event_fd
, event_file
);
469 /* The event holds a reference on the channel */
470 atomic_long_inc(&channel_file
->f_count
);
476 put_unused_fd(event_fd
);
482 * lttng_channel_ioctl - lttng syscall through ioctl
488 * This ioctl implements lttng commands:
489 * LTTNG_KERNEL_STREAM
490 * Returns an event stream file descriptor or failure.
491 * (typically, one event stream records events from one CPU)
493 * Returns an event file descriptor or failure.
494 * LTTNG_KERNEL_CONTEXT
495 * Prepend a context field to each event in the channel
497 * Channel and event file descriptors also hold a reference on the session.
500 long lttng_channel_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
502 struct ltt_channel
*channel
= file
->private_data
;
505 case LTTNG_KERNEL_STREAM
:
506 return lttng_abi_open_stream(file
);
507 case LTTNG_KERNEL_EVENT
:
508 return lttng_abi_create_event(file
, (struct lttng_kernel_event __user
*) arg
);
509 case LTTNG_KERNEL_CONTEXT
:
510 return lttng_abi_add_context(file
,
511 (struct lttng_kernel_context __user
*) arg
,
512 &channel
->ctx
, channel
->session
);
519 * lttng_metadata_ioctl - lttng syscall through ioctl
525 * This ioctl implements lttng commands:
526 * LTTNG_KERNEL_STREAM
527 * Returns an event stream file descriptor or failure.
529 * Channel and event file descriptors also hold a reference on the session.
532 long lttng_metadata_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
535 case LTTNG_KERNEL_STREAM
:
536 return lttng_abi_open_stream(file
);
545 * lttng_channel_poll - lttng stream addition/removal monitoring
550 unsigned int lttng_channel_poll(struct file
*file
, poll_table
*wait
)
552 struct ltt_channel
*channel
= file
->private_data
;
553 unsigned int mask
= 0;
555 if (file
->f_mode
& FMODE_READ
) {
556 poll_wait_set_exclusive(wait
);
557 poll_wait(file
, &channel
->notify_wait
, wait
);
559 /* TODO: identify when the channel is being finalized. */
563 return POLLIN
| POLLRDNORM
;
571 int lttng_channel_release(struct inode
*inode
, struct file
*file
)
573 struct ltt_channel
*channel
= file
->private_data
;
576 fput(channel
->session
->file
);
580 static const struct file_operations lttng_channel_fops
= {
581 .release
= lttng_channel_release
,
584 .poll
= lttng_channel_poll
,
586 .unlocked_ioctl
= lttng_channel_ioctl
,
588 .compat_ioctl
= lttng_channel_ioctl
,
592 static const struct file_operations lttng_metadata_fops
= {
593 .release
= lttng_channel_release
,
594 .unlocked_ioctl
= lttng_metadata_ioctl
,
596 .compat_ioctl
= lttng_metadata_ioctl
,
601 * lttng_event_ioctl - lttng syscall through ioctl
607 * This ioctl implements lttng commands:
608 * LTTNG_KERNEL_STREAM
609 * Returns an event stream file descriptor or failure.
610 * (typically, one event stream records events from one CPU)
612 * Returns an event file descriptor or failure.
613 * LTTNG_KERNEL_CONTEXT
614 * Prepend a context field to each record of this event
617 long lttng_event_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
619 struct ltt_event
*event
= file
->private_data
;
622 case LTTNG_KERNEL_CONTEXT
:
623 return lttng_abi_add_context(file
,
624 (struct lttng_kernel_context __user
*) arg
,
625 &event
->ctx
, event
->chan
->session
);
632 int lttng_event_release(struct inode
*inode
, struct file
*file
)
634 struct ltt_event
*event
= file
->private_data
;
637 fput(event
->chan
->file
);
641 /* TODO: filter control ioctl */
642 static const struct file_operations lttng_event_fops
= {
643 .release
= lttng_event_release
,
644 .unlocked_ioctl
= lttng_event_ioctl
,
646 .compat_ioctl
= lttng_event_ioctl
,
650 int __init
ltt_debugfs_abi_init(void)
654 wrapper_vmalloc_sync_all();
655 lttng_dentry
= debugfs_create_file("lttng", S_IWUSR
, NULL
, NULL
,
657 if (IS_ERR(lttng_dentry
) || !lttng_dentry
) {
658 printk(KERN_ERR
"Error creating LTTng control file\n");
666 void __exit
ltt_debugfs_abi_exit(void)
668 debugfs_remove(lttng_dentry
);