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_PERF_COUNTER
:
159 case LTTNG_KERNEL_CONTEXT_COMM
:
160 return lttng_add_comm_to_ctx(ctx
);
167 * lttng_ioctl - lttng syscall through ioctl
173 * This ioctl implements lttng commands:
174 * LTTNG_KERNEL_SESSION
175 * Returns a LTTng trace session file descriptor
176 * LTTNG_KERNEL_TRACER_VERSION
177 * Returns the LTTng kernel tracer version
178 * LTTNG_KERNEL_TRACEPOINT_LIST
179 * Returns a file descriptor listing available tracepoints
181 * The returned session will be deleted when its file descriptor is closed.
184 long lttng_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
187 case LTTNG_KERNEL_SESSION
:
188 return lttng_abi_create_session();
189 case LTTNG_KERNEL_TRACER_VERSION
:
190 return lttng_abi_tracer_version(file
,
191 (struct lttng_kernel_tracer_version __user
*) arg
);
192 case LTTNG_KERNEL_TRACEPOINT_LIST
:
193 return lttng_abi_tracepoint_list();
199 static const struct file_operations lttng_fops
= {
200 .unlocked_ioctl
= lttng_ioctl
,
202 .compat_ioctl
= lttng_ioctl
,
207 * We tolerate no failure in this function (if one happens, we print a dmesg
208 * error, but cannot return any error, because the channel information is
212 void lttng_metadata_create_events(struct file
*channel_file
)
214 struct ltt_channel
*channel
= channel_file
->private_data
;
215 static struct lttng_kernel_event metadata_params
= {
216 .instrumentation
= LTTNG_KERNEL_TRACEPOINT
,
217 .name
= "lttng_metadata",
219 struct ltt_event
*event
;
223 * We tolerate no failure path after event creation. It will stay
224 * invariant for the rest of the session.
226 event
= ltt_event_create(channel
, &metadata_params
, NULL
);
235 return; /* not allowed to return error */
239 int lttng_abi_create_channel(struct file
*session_file
,
240 struct lttng_kernel_channel __user
*uchan_param
,
241 enum channel_type channel_type
)
243 struct ltt_session
*session
= session_file
->private_data
;
244 const struct file_operations
*fops
;
245 const char *transport_name
;
246 struct ltt_channel
*chan
;
247 struct file
*chan_file
;
248 struct lttng_kernel_channel chan_param
;
252 if (copy_from_user(&chan_param
, uchan_param
, sizeof(chan_param
)))
254 chan_fd
= get_unused_fd();
259 chan_file
= anon_inode_getfile("[lttng_channel]",
262 if (IS_ERR(chan_file
)) {
263 ret
= PTR_ERR(chan_file
);
266 switch (channel_type
) {
267 case PER_CPU_CHANNEL
:
268 transport_name
= chan_param
.overwrite
?
269 "relay-overwrite" : "relay-discard";
270 fops
= <tng_channel_fops
;
272 case METADATA_CHANNEL
:
273 transport_name
= "relay-metadata";
274 fops
= <tng_metadata_fops
;
277 transport_name
= "<unknown>";
281 * We tolerate no failure path after channel creation. It will stay
282 * invariant for the rest of the session.
284 chan
= ltt_channel_create(session
, transport_name
, NULL
,
285 chan_param
.subbuf_size
,
286 chan_param
.num_subbuf
,
287 chan_param
.switch_timer_interval
,
288 chan_param
.read_timer_interval
);
293 chan
->file
= chan_file
;
294 chan_file
->private_data
= chan
;
295 fd_install(chan_fd
, chan_file
);
296 if (channel_type
== METADATA_CHANNEL
) {
297 session
->metadata
= chan
;
298 lttng_metadata_create_events(chan_file
);
301 /* The channel created holds a reference on the session */
302 atomic_long_inc(&session_file
->f_count
);
309 put_unused_fd(chan_fd
);
315 * lttng_session_ioctl - lttng session fd ioctl
321 * This ioctl implements lttng commands:
322 * LTTNG_KERNEL_CHANNEL
323 * Returns a LTTng channel file descriptor
324 * LTTNG_KERNEL_SESSION_START
325 * Starts tracing session
326 * LTTNG_KERNEL_SESSION_STOP
327 * Stops tracing session
328 * LTTNG_KERNEL_METADATA
329 * Returns a LTTng metadata file descriptor
331 * The returned channel will be deleted when its file descriptor is closed.
334 long lttng_session_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
336 struct ltt_session
*session
= file
->private_data
;
339 case LTTNG_KERNEL_CHANNEL
:
340 return lttng_abi_create_channel(file
,
341 (struct lttng_kernel_channel __user
*) arg
,
343 case LTTNG_KERNEL_SESSION_START
:
344 return ltt_session_start(session
);
345 case LTTNG_KERNEL_SESSION_STOP
:
346 return ltt_session_stop(session
);
347 case LTTNG_KERNEL_METADATA
:
348 return lttng_abi_create_channel(file
,
349 (struct lttng_kernel_channel __user
*) arg
,
357 * Called when the last file reference is dropped.
359 * Big fat note: channels and events are invariant for the whole session after
360 * their creation. So this session destruction also destroys all channel and
361 * event structures specific to this session (they are not destroyed when their
362 * individual file is released).
365 int lttng_session_release(struct inode
*inode
, struct file
*file
)
367 struct ltt_session
*session
= file
->private_data
;
370 ltt_session_destroy(session
);
374 static const struct file_operations lttng_session_fops
= {
375 .release
= lttng_session_release
,
376 .unlocked_ioctl
= lttng_session_ioctl
,
378 .compat_ioctl
= lttng_session_ioctl
,
383 int lttng_abi_open_stream(struct file
*channel_file
)
385 struct ltt_channel
*channel
= channel_file
->private_data
;
386 struct lib_ring_buffer
*buf
;
388 struct file
*stream_file
;
390 buf
= channel
->ops
->buffer_read_open(channel
->chan
);
394 stream_fd
= get_unused_fd();
399 stream_file
= anon_inode_getfile("[lttng_stream]",
400 &lib_ring_buffer_file_operations
,
402 if (IS_ERR(stream_file
)) {
403 ret
= PTR_ERR(stream_file
);
407 * OPEN_FMODE, called within anon_inode_getfile/alloc_file, don't honor
408 * FMODE_LSEEK, FMODE_PREAD nor FMODE_PWRITE. We need to read from this
409 * file descriptor, so we set FMODE_PREAD here.
411 stream_file
->f_mode
|= FMODE_PREAD
;
412 fd_install(stream_fd
, stream_file
);
414 * The stream holds a reference to the channel within the generic ring
415 * buffer library, so no need to hold a refcount on the channel and
416 * session files here.
421 put_unused_fd(stream_fd
);
423 channel
->ops
->buffer_read_close(buf
);
428 int lttng_abi_create_event(struct file
*channel_file
,
429 struct lttng_kernel_event __user
*uevent_param
)
431 struct ltt_channel
*channel
= channel_file
->private_data
;
432 struct ltt_event
*event
;
433 struct lttng_kernel_event event_param
;
435 struct file
*event_file
;
437 if (copy_from_user(&event_param
, uevent_param
, sizeof(event_param
)))
439 event_param
.name
[LTTNG_SYM_NAME_LEN
- 1] = '\0';
440 switch (event_param
.instrumentation
) {
441 case LTTNG_KERNEL_KPROBE
:
442 event_param
.u
.kprobe
.symbol_name
[LTTNG_SYM_NAME_LEN
- 1] = '\0';
444 case LTTNG_KERNEL_FUNCTION
:
445 event_param
.u
.ftrace
.symbol_name
[LTTNG_SYM_NAME_LEN
- 1] = '\0';
450 event_fd
= get_unused_fd();
455 event_file
= anon_inode_getfile("[lttng_event]",
458 if (IS_ERR(event_file
)) {
459 ret
= PTR_ERR(event_file
);
463 * We tolerate no failure path after event creation. It will stay
464 * invariant for the rest of the session.
466 event
= ltt_event_create(channel
, &event_param
, NULL
);
471 event_file
->private_data
= event
;
472 fd_install(event_fd
, event_file
);
473 /* The event holds a reference on the channel */
474 atomic_long_inc(&channel_file
->f_count
);
480 put_unused_fd(event_fd
);
486 * lttng_channel_ioctl - lttng syscall through ioctl
492 * This ioctl implements lttng commands:
493 * LTTNG_KERNEL_STREAM
494 * Returns an event stream file descriptor or failure.
495 * (typically, one event stream records events from one CPU)
497 * Returns an event file descriptor or failure.
498 * LTTNG_KERNEL_CONTEXT
499 * Prepend a context field to each event in the channel
501 * Channel and event file descriptors also hold a reference on the session.
504 long lttng_channel_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
506 struct ltt_channel
*channel
= file
->private_data
;
509 case LTTNG_KERNEL_STREAM
:
510 return lttng_abi_open_stream(file
);
511 case LTTNG_KERNEL_EVENT
:
512 return lttng_abi_create_event(file
, (struct lttng_kernel_event __user
*) arg
);
513 case LTTNG_KERNEL_CONTEXT
:
514 return lttng_abi_add_context(file
,
515 (struct lttng_kernel_context __user
*) arg
,
516 &channel
->ctx
, channel
->session
);
523 * lttng_metadata_ioctl - lttng syscall through ioctl
529 * This ioctl implements lttng commands:
530 * LTTNG_KERNEL_STREAM
531 * Returns an event stream file descriptor or failure.
533 * Channel and event file descriptors also hold a reference on the session.
536 long lttng_metadata_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
539 case LTTNG_KERNEL_STREAM
:
540 return lttng_abi_open_stream(file
);
549 * lttng_channel_poll - lttng stream addition/removal monitoring
554 unsigned int lttng_channel_poll(struct file
*file
, poll_table
*wait
)
556 struct ltt_channel
*channel
= file
->private_data
;
557 unsigned int mask
= 0;
559 if (file
->f_mode
& FMODE_READ
) {
560 poll_wait_set_exclusive(wait
);
561 poll_wait(file
, &channel
->notify_wait
, wait
);
563 /* TODO: identify when the channel is being finalized. */
567 return POLLIN
| POLLRDNORM
;
575 int lttng_channel_release(struct inode
*inode
, struct file
*file
)
577 struct ltt_channel
*channel
= file
->private_data
;
580 fput(channel
->session
->file
);
584 static const struct file_operations lttng_channel_fops
= {
585 .release
= lttng_channel_release
,
588 .poll
= lttng_channel_poll
,
590 .unlocked_ioctl
= lttng_channel_ioctl
,
592 .compat_ioctl
= lttng_channel_ioctl
,
596 static const struct file_operations lttng_metadata_fops
= {
597 .release
= lttng_channel_release
,
598 .unlocked_ioctl
= lttng_metadata_ioctl
,
600 .compat_ioctl
= lttng_metadata_ioctl
,
605 * lttng_event_ioctl - lttng syscall through ioctl
611 * This ioctl implements lttng commands:
612 * LTTNG_KERNEL_STREAM
613 * Returns an event stream file descriptor or failure.
614 * (typically, one event stream records events from one CPU)
616 * Returns an event file descriptor or failure.
617 * LTTNG_KERNEL_CONTEXT
618 * Prepend a context field to each record of this event
621 long lttng_event_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
623 struct ltt_event
*event
= file
->private_data
;
626 case LTTNG_KERNEL_CONTEXT
:
627 return lttng_abi_add_context(file
,
628 (struct lttng_kernel_context __user
*) arg
,
629 &event
->ctx
, event
->chan
->session
);
636 int lttng_event_release(struct inode
*inode
, struct file
*file
)
638 struct ltt_event
*event
= file
->private_data
;
641 fput(event
->chan
->file
);
645 /* TODO: filter control ioctl */
646 static const struct file_operations lttng_event_fops
= {
647 .release
= lttng_event_release
,
648 .unlocked_ioctl
= lttng_event_ioctl
,
650 .compat_ioctl
= lttng_event_ioctl
,
654 int __init
ltt_debugfs_abi_init(void)
658 wrapper_vmalloc_sync_all();
659 lttng_dentry
= debugfs_create_file("lttng", S_IWUSR
, NULL
, NULL
,
661 if (IS_ERR(lttng_dentry
) || !lttng_dentry
) {
662 printk(KERN_ERR
"Error creating LTTng control file\n");
670 void __exit
ltt_debugfs_abi_exit(void)
672 debugfs_remove(lttng_dentry
);