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 static struct lttng_kernel_event metadata_params
= {
146 .instrumentation
= LTTNG_KERNEL_TRACEPOINT
,
147 .name
= "lttng_metadata",
149 struct ltt_event
*event
;
153 * We tolerate no failure path after event creation. It will stay
154 * invariant for the rest of the session.
156 event
= ltt_event_create(channel
, &metadata_params
, NULL
);
165 return; /* not allowed to return error */
169 int lttng_abi_create_channel(struct file
*session_file
,
170 struct lttng_kernel_channel __user
*uchan_param
,
171 enum channel_type channel_type
)
173 struct ltt_session
*session
= session_file
->private_data
;
174 const struct file_operations
*fops
;
175 const char *transport_name
;
176 struct ltt_channel
*chan
;
177 struct file
*chan_file
;
178 struct lttng_kernel_channel chan_param
;
182 if (copy_from_user(&chan_param
, uchan_param
, sizeof(chan_param
)))
184 chan_fd
= get_unused_fd();
189 chan_file
= anon_inode_getfile("[lttng_channel]",
192 if (IS_ERR(chan_file
)) {
193 ret
= PTR_ERR(chan_file
);
196 switch (channel_type
) {
197 case PER_CPU_CHANNEL
:
198 transport_name
= chan_param
.overwrite
?
199 "relay-overwrite" : "relay-discard";
200 fops
= <tng_channel_fops
;
202 case METADATA_CHANNEL
:
203 transport_name
= "relay-metadata";
204 fops
= <tng_metadata_fops
;
207 transport_name
= "<unknown>";
211 * We tolerate no failure path after channel creation. It will stay
212 * invariant for the rest of the session.
214 chan
= ltt_channel_create(session
, transport_name
, NULL
,
215 chan_param
.subbuf_size
,
216 chan_param
.num_subbuf
,
217 chan_param
.switch_timer_interval
,
218 chan_param
.read_timer_interval
);
223 chan
->file
= chan_file
;
224 chan_file
->private_data
= chan
;
225 fd_install(chan_fd
, chan_file
);
226 if (channel_type
== METADATA_CHANNEL
) {
227 session
->metadata
= chan
;
228 lttng_metadata_create_events(chan_file
);
231 /* The channel created holds a reference on the session */
232 atomic_long_inc(&session_file
->f_count
);
239 put_unused_fd(chan_fd
);
245 * lttng_session_ioctl - lttng session fd ioctl
251 * This ioctl implements lttng commands:
252 * LTTNG_KERNEL_CHANNEL
253 * Returns a LTTng channel file descriptor
255 * The returned channel will be deleted when its file descriptor is closed.
258 long lttng_session_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
260 struct ltt_session
*session
= file
->private_data
;
263 case LTTNG_KERNEL_CHANNEL
:
264 return lttng_abi_create_channel(file
,
265 (struct lttng_kernel_channel __user
*) arg
,
267 case LTTNG_KERNEL_SESSION_START
:
268 return ltt_session_start(session
);
269 case LTTNG_KERNEL_SESSION_STOP
:
270 return ltt_session_stop(session
);
271 case LTTNG_KERNEL_METADATA
:
272 return lttng_abi_create_channel(file
,
273 (struct lttng_kernel_channel __user
*) arg
,
281 * Called when the last file reference is dropped.
283 * Big fat note: channels and events are invariant for the whole session after
284 * their creation. So this session destruction also destroys all channel and
285 * event structures specific to this session (they are not destroyed when their
286 * individual file is released).
289 int lttng_session_release(struct inode
*inode
, struct file
*file
)
291 struct ltt_session
*session
= file
->private_data
;
294 ltt_session_destroy(session
);
298 static const struct file_operations lttng_session_fops
= {
299 .release
= lttng_session_release
,
300 .unlocked_ioctl
= lttng_session_ioctl
,
302 .compat_ioctl
= lttng_session_ioctl
,
307 int lttng_abi_open_stream(struct file
*channel_file
)
309 struct ltt_channel
*channel
= channel_file
->private_data
;
310 struct lib_ring_buffer
*buf
;
312 struct file
*stream_file
;
314 buf
= channel
->ops
->buffer_read_open(channel
->chan
);
318 stream_fd
= get_unused_fd();
323 stream_file
= anon_inode_getfile("[lttng_stream]",
324 &lib_ring_buffer_file_operations
,
326 if (IS_ERR(stream_file
)) {
327 ret
= PTR_ERR(stream_file
);
331 * OPEN_FMODE, called within anon_inode_getfile/alloc_file, don't honor
332 * FMODE_LSEEK, FMODE_PREAD nor FMODE_PWRITE. We need to read from this
333 * file descriptor, so we set FMODE_PREAD here.
335 stream_file
->f_mode
|= FMODE_PREAD
;
336 fd_install(stream_fd
, stream_file
);
338 * The stream holds a reference to the channel within the generic ring
339 * buffer library, so no need to hold a refcount on the channel and
340 * session files here.
345 put_unused_fd(stream_fd
);
347 channel
->ops
->buffer_read_close(buf
);
352 int lttng_abi_create_event(struct file
*channel_file
,
353 struct lttng_kernel_event __user
*uevent_param
)
355 struct ltt_channel
*channel
= channel_file
->private_data
;
356 struct ltt_event
*event
;
357 struct lttng_kernel_event event_param
;
359 struct file
*event_file
;
361 if (copy_from_user(&event_param
, uevent_param
, sizeof(event_param
)))
363 event_param
.name
[LTTNG_SYM_NAME_LEN
- 1] = '\0';
364 switch (event_param
.instrumentation
) {
365 case LTTNG_KERNEL_KPROBE
:
366 event_param
.u
.kprobe
.symbol_name
[LTTNG_SYM_NAME_LEN
- 1] = '\0';
368 case LTTNG_KERNEL_FUNCTION
:
369 event_param
.u
.ftrace
.symbol_name
[LTTNG_SYM_NAME_LEN
- 1] = '\0';
374 event_fd
= get_unused_fd();
379 event_file
= anon_inode_getfile("[lttng_event]",
382 if (IS_ERR(event_file
)) {
383 ret
= PTR_ERR(event_file
);
387 * We tolerate no failure path after event creation. It will stay
388 * invariant for the rest of the session.
390 event
= ltt_event_create(channel
, &event_param
, NULL
);
395 event_file
->private_data
= event
;
396 fd_install(event_fd
, event_file
);
397 /* The event holds a reference on the channel */
398 atomic_long_inc(&channel_file
->f_count
);
404 put_unused_fd(event_fd
);
410 * lttng_channel_ioctl - lttng syscall through ioctl
416 * This ioctl implements lttng commands:
417 * LTTNG_KERNEL_STREAM
418 * Returns an event stream file descriptor or failure.
419 * (typically, one event stream records events from one CPU)
421 * Returns an event file descriptor or failure.
423 * Channel and event file descriptors also hold a reference on the session.
426 long lttng_channel_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
429 case LTTNG_KERNEL_STREAM
:
430 return lttng_abi_open_stream(file
);
431 case LTTNG_KERNEL_EVENT
:
432 return lttng_abi_create_event(file
, (struct lttng_kernel_event __user
*) arg
);
439 * lttng_metadata_ioctl - lttng syscall through ioctl
445 * This ioctl implements lttng commands:
446 * LTTNG_KERNEL_STREAM
447 * Returns an event stream file descriptor or failure.
449 * Channel and event file descriptors also hold a reference on the session.
452 long lttng_metadata_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
455 case LTTNG_KERNEL_STREAM
:
456 return lttng_abi_open_stream(file
);
465 * lttng_channel_poll - lttng stream addition/removal monitoring
470 unsigned int lttng_channel_poll(struct file
*file
, poll_table
*wait
)
472 struct ltt_channel
*channel
= file
->private_data
;
473 unsigned int mask
= 0;
475 if (file
->f_mode
& FMODE_READ
) {
476 poll_wait_set_exclusive(wait
);
477 poll_wait(file
, &channel
->notify_wait
, wait
);
479 /* TODO: identify when the channel is being finalized. */
483 return POLLIN
| POLLRDNORM
;
491 int lttng_channel_release(struct inode
*inode
, struct file
*file
)
493 struct ltt_channel
*channel
= file
->private_data
;
496 fput(channel
->session
->file
);
500 static const struct file_operations lttng_channel_fops
= {
501 .release
= lttng_channel_release
,
504 .poll
= lttng_channel_poll
,
506 .unlocked_ioctl
= lttng_channel_ioctl
,
508 .compat_ioctl
= lttng_channel_ioctl
,
512 static const struct file_operations lttng_metadata_fops
= {
513 .release
= lttng_channel_release
,
514 .unlocked_ioctl
= lttng_metadata_ioctl
,
516 .compat_ioctl
= lttng_metadata_ioctl
,
521 int lttng_event_release(struct inode
*inode
, struct file
*file
)
523 struct ltt_event
*event
= file
->private_data
;
526 fput(event
->chan
->file
);
530 /* TODO: filter control ioctl */
531 static const struct file_operations lttng_event_fops
= {
532 .release
= lttng_event_release
,
535 int __init
ltt_debugfs_abi_init(void)
539 wrapper_vmalloc_sync_all();
540 lttng_dentry
= debugfs_create_file("lttng", S_IWUSR
, NULL
, NULL
,
542 if (IS_ERR(lttng_dentry
) || !lttng_dentry
) {
543 printk(KERN_ERR
"Error creating LTTng control file\n");
551 void __exit
ltt_debugfs_abi_exit(void)
553 debugfs_remove(lttng_dentry
);