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 * lttng_ioctl - lttng syscall through ioctl
146 * This ioctl implements lttng commands:
147 * LTTNG_KERNEL_SESSION
148 * Returns a LTTng trace session file descriptor
149 * LTTNG_KERNEL_TRACER_VERSION
150 * Returns the LTTng kernel tracer version
151 * LTTNG_KERNEL_TRACEPOINT_LIST
152 * Returns a file descriptor listing available tracepoints
154 * The returned session will be deleted when its file descriptor is closed.
157 long lttng_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
160 case LTTNG_KERNEL_SESSION
:
161 return lttng_abi_create_session();
162 case LTTNG_KERNEL_TRACER_VERSION
:
163 return lttng_abi_tracer_version(file
,
164 (struct lttng_kernel_tracer_version __user
*) arg
);
165 case LTTNG_KERNEL_TRACEPOINT_LIST
:
166 return lttng_abi_tracepoint_list();
172 static const struct file_operations lttng_fops
= {
173 .unlocked_ioctl
= lttng_ioctl
,
175 .compat_ioctl
= lttng_ioctl
,
180 * We tolerate no failure in this function (if one happens, we print a dmesg
181 * error, but cannot return any error, because the channel information is
185 void lttng_metadata_create_events(struct file
*channel_file
)
187 struct ltt_channel
*channel
= channel_file
->private_data
;
188 static struct lttng_kernel_event metadata_params
= {
189 .instrumentation
= LTTNG_KERNEL_TRACEPOINT
,
190 .name
= "lttng_metadata",
192 struct ltt_event
*event
;
196 * We tolerate no failure path after event creation. It will stay
197 * invariant for the rest of the session.
199 event
= ltt_event_create(channel
, &metadata_params
, NULL
);
208 return; /* not allowed to return error */
212 int lttng_abi_create_channel(struct file
*session_file
,
213 struct lttng_kernel_channel __user
*uchan_param
,
214 enum channel_type channel_type
)
216 struct ltt_session
*session
= session_file
->private_data
;
217 const struct file_operations
*fops
;
218 const char *transport_name
;
219 struct ltt_channel
*chan
;
220 struct file
*chan_file
;
221 struct lttng_kernel_channel chan_param
;
225 if (copy_from_user(&chan_param
, uchan_param
, sizeof(chan_param
)))
227 chan_fd
= get_unused_fd();
232 chan_file
= anon_inode_getfile("[lttng_channel]",
235 if (IS_ERR(chan_file
)) {
236 ret
= PTR_ERR(chan_file
);
239 switch (channel_type
) {
240 case PER_CPU_CHANNEL
:
241 transport_name
= chan_param
.overwrite
?
242 "relay-overwrite" : "relay-discard";
243 fops
= <tng_channel_fops
;
245 case METADATA_CHANNEL
:
246 transport_name
= "relay-metadata";
247 fops
= <tng_metadata_fops
;
250 transport_name
= "<unknown>";
254 * We tolerate no failure path after channel creation. It will stay
255 * invariant for the rest of the session.
257 chan
= ltt_channel_create(session
, transport_name
, NULL
,
258 chan_param
.subbuf_size
,
259 chan_param
.num_subbuf
,
260 chan_param
.switch_timer_interval
,
261 chan_param
.read_timer_interval
);
266 chan
->file
= chan_file
;
267 chan_file
->private_data
= chan
;
268 fd_install(chan_fd
, chan_file
);
269 if (channel_type
== METADATA_CHANNEL
) {
270 session
->metadata
= chan
;
271 lttng_metadata_create_events(chan_file
);
274 /* The channel created holds a reference on the session */
275 atomic_long_inc(&session_file
->f_count
);
282 put_unused_fd(chan_fd
);
288 * lttng_session_ioctl - lttng session fd ioctl
294 * This ioctl implements lttng commands:
295 * LTTNG_KERNEL_CHANNEL
296 * Returns a LTTng channel file descriptor
298 * The returned channel will be deleted when its file descriptor is closed.
301 long lttng_session_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
303 struct ltt_session
*session
= file
->private_data
;
306 case LTTNG_KERNEL_CHANNEL
:
307 return lttng_abi_create_channel(file
,
308 (struct lttng_kernel_channel __user
*) arg
,
310 case LTTNG_KERNEL_SESSION_START
:
311 return ltt_session_start(session
);
312 case LTTNG_KERNEL_SESSION_STOP
:
313 return ltt_session_stop(session
);
314 case LTTNG_KERNEL_METADATA
:
315 return lttng_abi_create_channel(file
,
316 (struct lttng_kernel_channel __user
*) arg
,
324 * Called when the last file reference is dropped.
326 * Big fat note: channels and events are invariant for the whole session after
327 * their creation. So this session destruction also destroys all channel and
328 * event structures specific to this session (they are not destroyed when their
329 * individual file is released).
332 int lttng_session_release(struct inode
*inode
, struct file
*file
)
334 struct ltt_session
*session
= file
->private_data
;
337 ltt_session_destroy(session
);
341 static const struct file_operations lttng_session_fops
= {
342 .release
= lttng_session_release
,
343 .unlocked_ioctl
= lttng_session_ioctl
,
345 .compat_ioctl
= lttng_session_ioctl
,
350 int lttng_abi_open_stream(struct file
*channel_file
)
352 struct ltt_channel
*channel
= channel_file
->private_data
;
353 struct lib_ring_buffer
*buf
;
355 struct file
*stream_file
;
357 buf
= channel
->ops
->buffer_read_open(channel
->chan
);
361 stream_fd
= get_unused_fd();
366 stream_file
= anon_inode_getfile("[lttng_stream]",
367 &lib_ring_buffer_file_operations
,
369 if (IS_ERR(stream_file
)) {
370 ret
= PTR_ERR(stream_file
);
374 * OPEN_FMODE, called within anon_inode_getfile/alloc_file, don't honor
375 * FMODE_LSEEK, FMODE_PREAD nor FMODE_PWRITE. We need to read from this
376 * file descriptor, so we set FMODE_PREAD here.
378 stream_file
->f_mode
|= FMODE_PREAD
;
379 fd_install(stream_fd
, stream_file
);
381 * The stream holds a reference to the channel within the generic ring
382 * buffer library, so no need to hold a refcount on the channel and
383 * session files here.
388 put_unused_fd(stream_fd
);
390 channel
->ops
->buffer_read_close(buf
);
395 int lttng_abi_create_event(struct file
*channel_file
,
396 struct lttng_kernel_event __user
*uevent_param
)
398 struct ltt_channel
*channel
= channel_file
->private_data
;
399 struct ltt_event
*event
;
400 struct lttng_kernel_event event_param
;
402 struct file
*event_file
;
404 if (copy_from_user(&event_param
, uevent_param
, sizeof(event_param
)))
406 event_param
.name
[LTTNG_SYM_NAME_LEN
- 1] = '\0';
407 switch (event_param
.instrumentation
) {
408 case LTTNG_KERNEL_KPROBE
:
409 event_param
.u
.kprobe
.symbol_name
[LTTNG_SYM_NAME_LEN
- 1] = '\0';
411 case LTTNG_KERNEL_FUNCTION
:
412 event_param
.u
.ftrace
.symbol_name
[LTTNG_SYM_NAME_LEN
- 1] = '\0';
417 event_fd
= get_unused_fd();
422 event_file
= anon_inode_getfile("[lttng_event]",
425 if (IS_ERR(event_file
)) {
426 ret
= PTR_ERR(event_file
);
430 * We tolerate no failure path after event creation. It will stay
431 * invariant for the rest of the session.
433 event
= ltt_event_create(channel
, &event_param
, NULL
);
438 event_file
->private_data
= event
;
439 fd_install(event_fd
, event_file
);
440 /* The event holds a reference on the channel */
441 atomic_long_inc(&channel_file
->f_count
);
447 put_unused_fd(event_fd
);
453 * lttng_channel_ioctl - lttng syscall through ioctl
459 * This ioctl implements lttng commands:
460 * LTTNG_KERNEL_STREAM
461 * Returns an event stream file descriptor or failure.
462 * (typically, one event stream records events from one CPU)
464 * Returns an event file descriptor or failure.
466 * Channel and event file descriptors also hold a reference on the session.
469 long lttng_channel_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
472 case LTTNG_KERNEL_STREAM
:
473 return lttng_abi_open_stream(file
);
474 case LTTNG_KERNEL_EVENT
:
475 return lttng_abi_create_event(file
, (struct lttng_kernel_event __user
*) arg
);
482 * lttng_metadata_ioctl - lttng syscall through ioctl
488 * This ioctl implements lttng commands:
489 * LTTNG_KERNEL_STREAM
490 * Returns an event stream file descriptor or failure.
492 * Channel and event file descriptors also hold a reference on the session.
495 long lttng_metadata_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
498 case LTTNG_KERNEL_STREAM
:
499 return lttng_abi_open_stream(file
);
508 * lttng_channel_poll - lttng stream addition/removal monitoring
513 unsigned int lttng_channel_poll(struct file
*file
, poll_table
*wait
)
515 struct ltt_channel
*channel
= file
->private_data
;
516 unsigned int mask
= 0;
518 if (file
->f_mode
& FMODE_READ
) {
519 poll_wait_set_exclusive(wait
);
520 poll_wait(file
, &channel
->notify_wait
, wait
);
522 /* TODO: identify when the channel is being finalized. */
526 return POLLIN
| POLLRDNORM
;
534 int lttng_channel_release(struct inode
*inode
, struct file
*file
)
536 struct ltt_channel
*channel
= file
->private_data
;
539 fput(channel
->session
->file
);
543 static const struct file_operations lttng_channel_fops
= {
544 .release
= lttng_channel_release
,
547 .poll
= lttng_channel_poll
,
549 .unlocked_ioctl
= lttng_channel_ioctl
,
551 .compat_ioctl
= lttng_channel_ioctl
,
555 static const struct file_operations lttng_metadata_fops
= {
556 .release
= lttng_channel_release
,
557 .unlocked_ioctl
= lttng_metadata_ioctl
,
559 .compat_ioctl
= lttng_metadata_ioctl
,
564 int lttng_event_release(struct inode
*inode
, struct file
*file
)
566 struct ltt_event
*event
= file
->private_data
;
569 fput(event
->chan
->file
);
573 /* TODO: filter control ioctl */
574 static const struct file_operations lttng_event_fops
= {
575 .release
= lttng_event_release
,
578 int __init
ltt_debugfs_abi_init(void)
582 wrapper_vmalloc_sync_all();
583 lttng_dentry
= debugfs_create_file("lttng", S_IWUSR
, NULL
, NULL
,
585 if (IS_ERR(lttng_dentry
) || !lttng_dentry
) {
586 printk(KERN_ERR
"Error creating LTTng control file\n");
594 void __exit
ltt_debugfs_abi_exit(void)
596 debugfs_remove(lttng_dentry
);