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 long lttng_abi_tracer_version(struct file
*file
,
89 struct lttng_kernel_tracer_version __user
*uversion_param
)
91 struct lttng_kernel_tracer_version v
;
93 v
.version
= LTTNG_VERSION
;
94 v
.patchlevel
= LTTNG_PATCHLEVEL
;
95 v
.sublevel
= LTTNG_SUBLEVEL
;
97 if (copy_to_user(uversion_param
, &v
, sizeof(v
)))
103 * lttng_ioctl - lttng syscall through ioctl
109 * This ioctl implements lttng commands:
110 * LTTNG_KERNEL_SESSION
111 * Returns a LTTng trace session file descriptor
113 * The returned session will be deleted when its file descriptor is closed.
116 long lttng_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
119 case LTTNG_KERNEL_SESSION
:
120 return lttng_abi_create_session();
121 case LTTNG_KERNEL_TRACER_VERSION
:
122 return lttng_abi_tracer_version(file
,
123 (struct lttng_kernel_tracer_version __user
*) arg
);
129 static const struct file_operations lttng_fops
= {
130 .unlocked_ioctl
= lttng_ioctl
,
132 .compat_ioctl
= lttng_ioctl
,
137 * We tolerate no failure in this function (if one happens, we print a dmesg
138 * error, but cannot return any error, because the channel information is
142 void lttng_metadata_create_events(struct file
*channel_file
)
144 struct ltt_channel
*channel
= channel_file
->private_data
;
145 char *event_name
= "lttng_metadata";
146 const struct lttng_event_desc
*event_desc
;
147 struct ltt_event
*event
;
150 event_desc
= ltt_event_get(event_name
);
156 * We tolerate no failure path after event creation. It will stay
157 * invariant for the rest of the session.
159 event
= ltt_event_create(channel
, event_name
, LTTNG_KERNEL_TRACEPOINTS
,
168 ltt_event_put(event_desc
);
171 return; /* not allowed to return error */
175 int lttng_abi_create_channel(struct file
*session_file
,
176 struct lttng_kernel_channel __user
*uchan_param
,
177 enum channel_type channel_type
)
179 struct ltt_session
*session
= session_file
->private_data
;
180 const struct file_operations
*fops
;
181 const char *transport_name
;
182 struct ltt_channel
*chan
;
183 struct file
*chan_file
;
184 struct lttng_kernel_channel chan_param
;
188 if (copy_from_user(&chan_param
, uchan_param
, sizeof(chan_param
)))
190 chan_fd
= get_unused_fd();
195 chan_file
= anon_inode_getfile("[lttng_channel]",
198 if (IS_ERR(chan_file
)) {
199 ret
= PTR_ERR(chan_file
);
202 switch (channel_type
) {
203 case PER_CPU_CHANNEL
:
204 transport_name
= chan_param
.overwrite
?
205 "relay-overwrite" : "relay-discard";
206 fops
= <tng_channel_fops
;
208 case METADATA_CHANNEL
:
209 transport_name
= "relay-metadata";
210 fops
= <tng_metadata_fops
;
213 transport_name
= "<unknown>";
217 * We tolerate no failure path after channel creation. It will stay
218 * invariant for the rest of the session.
220 chan
= ltt_channel_create(session
, transport_name
, NULL
,
221 chan_param
.subbuf_size
,
222 chan_param
.num_subbuf
,
223 chan_param
.switch_timer_interval
,
224 chan_param
.read_timer_interval
);
229 chan
->file
= chan_file
;
230 chan_file
->private_data
= chan
;
231 fd_install(chan_fd
, chan_file
);
232 if (channel_type
== METADATA_CHANNEL
) {
233 lttng_metadata_create_events(chan_file
);
234 session
->metadata
= chan
;
237 /* The channel created holds a reference on the session */
238 atomic_long_inc(&session_file
->f_count
);
245 put_unused_fd(chan_fd
);
251 * lttng_session_ioctl - lttng session fd ioctl
257 * This ioctl implements lttng commands:
258 * LTTNG_KERNEL_CHANNEL
259 * Returns a LTTng channel file descriptor
261 * The returned channel will be deleted when its file descriptor is closed.
264 long lttng_session_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
266 struct ltt_session
*session
= file
->private_data
;
269 case LTTNG_KERNEL_CHANNEL
:
270 return lttng_abi_create_channel(file
,
271 (struct lttng_kernel_channel __user
*) arg
,
273 case LTTNG_KERNEL_SESSION_START
:
274 return ltt_session_start(session
);
275 case LTTNG_KERNEL_SESSION_STOP
:
276 return ltt_session_stop(session
);
277 case LTTNG_KERNEL_METADATA
:
278 return lttng_abi_create_channel(file
,
279 (struct lttng_kernel_channel __user
*) arg
,
287 * Called when the last file reference is dropped.
289 * Big fat note: channels and events are invariant for the whole session after
290 * their creation. So this session destruction also destroys all channel and
291 * event structures specific to this session (they are not destroyed when their
292 * individual file is released).
295 int lttng_session_release(struct inode
*inode
, struct file
*file
)
297 struct ltt_session
*session
= file
->private_data
;
300 ltt_session_destroy(session
);
304 static const struct file_operations lttng_session_fops
= {
305 .release
= lttng_session_release
,
306 .unlocked_ioctl
= lttng_session_ioctl
,
308 .compat_ioctl
= lttng_session_ioctl
,
313 int lttng_abi_open_stream(struct file
*channel_file
)
315 struct ltt_channel
*channel
= channel_file
->private_data
;
316 struct lib_ring_buffer
*buf
;
318 struct file
*stream_file
;
320 buf
= channel
->ops
->buffer_read_open(channel
->chan
);
324 stream_fd
= get_unused_fd();
329 stream_file
= anon_inode_getfile("[lttng_stream]",
330 &lib_ring_buffer_file_operations
,
332 if (IS_ERR(stream_file
)) {
333 ret
= PTR_ERR(stream_file
);
337 * OPEN_FMODE, called within anon_inode_getfile/alloc_file, don't honor
338 * FMODE_LSEEK, FMODE_PREAD nor FMODE_PWRITE. We need to read from this
339 * file descriptor, so we set FMODE_PREAD here.
341 stream_file
->f_mode
|= FMODE_PREAD
;
342 fd_install(stream_fd
, stream_file
);
344 * The stream holds a reference to the channel within the generic ring
345 * buffer library, so no need to hold a refcount on the channel and
346 * session files here.
351 put_unused_fd(stream_fd
);
353 channel
->ops
->buffer_read_close(buf
);
358 int lttng_abi_create_event(struct file
*channel_file
,
359 struct lttng_kernel_event __user
*uevent_param
)
361 struct ltt_channel
*channel
= channel_file
->private_data
;
362 const struct lttng_event_desc
*event_desc
;
363 struct ltt_event
*event
;
365 struct lttng_kernel_event event_param
;
367 struct file
*event_file
;
369 if (copy_from_user(&event_param
, uevent_param
, sizeof(event_param
)))
371 event_name
= kmalloc(PATH_MAX
, GFP_KERNEL
);
374 if (strncpy_from_user(event_name
, uevent_param
->name
, PATH_MAX
) < 0) {
378 event_name
[PATH_MAX
- 1] = '\0';
380 event_desc
= ltt_event_get(event_name
);
385 event_fd
= get_unused_fd();
390 event_file
= anon_inode_getfile("[lttng_event]",
393 if (IS_ERR(event_file
)) {
394 ret
= PTR_ERR(event_file
);
398 * We tolerate no failure path after event creation. It will stay
399 * invariant for the rest of the session.
401 event
= ltt_event_create(channel
, event_name
, &event_param
,
407 event_file
->private_data
= event
;
408 fd_install(event_fd
, event_file
);
409 /* The event holds a reference on the channel */
410 atomic_long_inc(&channel_file
->f_count
);
417 put_unused_fd(event_fd
);
419 ltt_event_put(event_desc
);
427 * lttng_channel_ioctl - lttng syscall through ioctl
433 * This ioctl implements lttng commands:
434 * LTTNG_KERNEL_STREAM
435 * Returns an event stream file descriptor or failure.
436 * (typically, one event stream records events from one CPU)
438 * Returns an event file descriptor or failure.
440 * Channel and event file descriptors also hold a reference on the session.
443 long lttng_channel_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
446 case LTTNG_KERNEL_STREAM
:
447 return lttng_abi_open_stream(file
);
448 case LTTNG_KERNEL_EVENT
:
449 return lttng_abi_create_event(file
, (struct lttng_kernel_event __user
*) arg
);
456 * lttng_metadata_ioctl - lttng syscall through ioctl
462 * This ioctl implements lttng commands:
463 * LTTNG_KERNEL_STREAM
464 * Returns an event stream file descriptor or failure.
466 * Channel and event file descriptors also hold a reference on the session.
469 long lttng_metadata_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
472 case LTTNG_KERNEL_STREAM
:
473 return lttng_abi_open_stream(file
);
482 * lttng_channel_poll - lttng stream addition/removal monitoring
487 unsigned int lttng_channel_poll(struct file
*file
, poll_table
*wait
)
489 struct ltt_channel
*channel
= file
->private_data
;
490 unsigned int mask
= 0;
492 if (file
->f_mode
& FMODE_READ
) {
493 poll_wait_set_exclusive(wait
);
494 poll_wait(file
, &channel
->notify_wait
, wait
);
496 /* TODO: identify when the channel is being finalized. */
500 return POLLIN
| POLLRDNORM
;
508 int lttng_channel_release(struct inode
*inode
, struct file
*file
)
510 struct ltt_channel
*channel
= file
->private_data
;
513 fput(channel
->session
->file
);
517 static const struct file_operations lttng_channel_fops
= {
518 .release
= lttng_channel_release
,
521 .poll
= lttng_channel_poll
,
523 .unlocked_ioctl
= lttng_channel_ioctl
,
525 .compat_ioctl
= lttng_channel_ioctl
,
529 static const struct file_operations lttng_metadata_fops
= {
530 .release
= lttng_channel_release
,
531 .unlocked_ioctl
= lttng_metadata_ioctl
,
533 .compat_ioctl
= lttng_metadata_ioctl
,
538 int lttng_event_release(struct inode
*inode
, struct file
*file
)
540 struct ltt_event
*event
= file
->private_data
;
543 ltt_event_unregister(event
);
544 fput(event
->chan
->file
);
549 /* TODO: filter control ioctl */
550 static const struct file_operations lttng_event_fops
= {
551 .release
= lttng_event_release
,
554 int __init
ltt_debugfs_abi_init(void)
558 wrapper_vmalloc_sync_all();
559 lttng_dentry
= debugfs_create_file("lttng", S_IWUSR
, NULL
, NULL
,
561 if (IS_ERR(lttng_dentry
) || !lttng_dentry
) {
562 printk(KERN_ERR
"Error creating LTTng control file\n");
570 void __exit
ltt_debugfs_abi_exit(void)
572 debugfs_remove(lttng_dentry
);