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"
37 * This is LTTng's own personal way to create a system call as an external
38 * module. We use ioctl() on /sys/kernel/debug/lttng.
41 static struct dentry
*lttng_dentry
;
42 static const struct file_operations lttng_fops
;
43 static const struct file_operations lttng_session_fops
;
44 static const struct file_operations lttng_channel_fops
;
45 static const struct file_operations lttng_metadata_fops
;
46 static const struct file_operations lttng_event_fops
;
54 int lttng_abi_create_session(void)
56 struct ltt_session
*session
;
57 struct file
*session_file
;
60 session
= ltt_session_create();
63 session_fd
= get_unused_fd();
68 session_file
= anon_inode_getfile("[lttng_session]",
71 if (IS_ERR(session_file
)) {
72 ret
= PTR_ERR(session_file
);
75 session
->file
= session_file
;
76 fd_install(session_fd
, session_file
);
80 put_unused_fd(session_fd
);
82 ltt_session_destroy(session
);
87 * lttng_ioctl - lttng syscall through ioctl
93 * This ioctl implements lttng commands:
95 * Returns a LTTng trace session file descriptor
97 * The returned session will be deleted when its file descriptor is closed.
100 long lttng_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
104 return lttng_abi_create_session();
110 static const struct file_operations lttng_fops
= {
111 .unlocked_ioctl
= lttng_ioctl
,
113 .compat_ioctl
= lttng_ioctl
,
118 * We tolerate no failure in this function (if one happens, we print a dmesg
119 * error, but cannot return any error, because the channel information is
123 void lttng_metadata_create_events(struct file
*channel_file
)
125 struct ltt_channel
*channel
= channel_file
->private_data
;
126 char *event_name
= "lttng-metadata";
127 const struct lttng_event_desc
*event_desc
;
128 struct ltt_event
*event
;
131 event_desc
= ltt_event_get(event_name
);
137 * We tolerate no failure path after event creation. It will stay
138 * invariant for the rest of the session.
140 event
= ltt_event_create(channel
, event_name
, INSTRUM_TRACEPOINTS
,
149 ltt_event_put(event_desc
);
152 return; /* not allowed to return error */
156 int lttng_abi_create_channel(struct file
*session_file
,
157 struct lttng_channel __user
*uchan_param
,
158 enum channel_type channel_type
)
160 struct ltt_session
*session
= session_file
->private_data
;
161 const struct file_operations
*fops
;
162 const char *transport_name
;
163 struct ltt_channel
*chan
;
164 struct file
*chan_file
;
165 struct lttng_channel chan_param
;
169 if (copy_from_user(&chan_param
, uchan_param
, sizeof(chan_param
)))
171 chan_fd
= get_unused_fd();
176 chan_file
= anon_inode_getfile("[lttng_channel]",
179 if (IS_ERR(chan_file
)) {
180 ret
= PTR_ERR(chan_file
);
183 switch (channel_type
) {
184 case PER_CPU_CHANNEL
:
185 transport_name
= chan_param
.overwrite
?
186 "relay-overwrite" : "relay-discard";
187 fops
= <tng_channel_fops
;
189 case METADATA_CHANNEL
:
190 transport_name
= "relay-metadata";
191 fops
= <tng_metadata_fops
;
194 transport_name
= "<unknown>";
198 * We tolerate no failure path after channel creation. It will stay
199 * invariant for the rest of the session.
201 chan
= ltt_channel_create(session
, transport_name
, NULL
,
202 chan_param
.subbuf_size
,
203 chan_param
.num_subbuf
,
204 chan_param
.switch_timer_interval
,
205 chan_param
.read_timer_interval
);
210 chan
->file
= chan_file
;
211 chan_file
->private_data
= chan
;
212 fd_install(chan_fd
, chan_file
);
213 if (channel_type
== METADATA_CHANNEL
)
214 lttng_metadata_create_events(chan_file
);
216 /* The channel created holds a reference on the session */
217 atomic_long_inc(&session_file
->f_count
);
224 put_unused_fd(chan_fd
);
230 * lttng_session_ioctl - lttng session fd ioctl
236 * This ioctl implements lttng commands:
238 * Returns a LTTng channel file descriptor
240 * The returned channel will be deleted when its file descriptor is closed.
243 long lttng_session_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
245 struct ltt_session
*session
= file
->private_data
;
249 return lttng_abi_create_channel(file
,
250 (struct lttng_channel __user
*)arg
,
252 case LTTNG_SESSION_START
:
253 return ltt_session_start(session
);
254 case LTTNG_SESSION_STOP
:
255 return ltt_session_stop(session
);
257 return lttng_abi_create_channel(file
,
258 (struct lttng_channel __user
*)arg
,
266 * Called when the last file reference is dropped.
268 * Big fat note: channels and events are invariant for the whole session after
269 * their creation. So this session destruction also destroys all channel and
270 * event structures specific to this session (they are not destroyed when their
271 * individual file is released).
274 int lttng_session_release(struct inode
*inode
, struct file
*file
)
276 struct ltt_session
*session
= file
->private_data
;
279 ltt_session_destroy(session
);
283 static const struct file_operations lttng_session_fops
= {
284 .release
= lttng_session_release
,
285 .unlocked_ioctl
= lttng_session_ioctl
,
287 .compat_ioctl
= lttng_session_ioctl
,
292 int lttng_abi_open_stream(struct file
*channel_file
)
294 struct ltt_channel
*channel
= channel_file
->private_data
;
295 struct lib_ring_buffer
*buf
;
297 struct file
*stream_file
;
299 buf
= channel
->ops
->buffer_read_open(channel
->chan
);
303 stream_fd
= get_unused_fd();
308 stream_file
= anon_inode_getfile("[lttng_stream]",
309 &lib_ring_buffer_file_operations
,
311 if (IS_ERR(stream_file
)) {
312 ret
= PTR_ERR(stream_file
);
316 * OPEN_FMODE, called within anon_inode_getfile/alloc_file, don't honor
317 * FMODE_LSEEK, FMODE_PREAD nor FMODE_PWRITE. We need to read from this
318 * file descriptor, so we set FMODE_PREAD here.
320 stream_file
->f_mode
|= FMODE_PREAD
;
321 fd_install(stream_fd
, stream_file
);
323 * The stream holds a reference to the channel within the generic ring
324 * buffer library, so no need to hold a refcount on the channel and
325 * session files here.
330 put_unused_fd(stream_fd
);
332 channel
->ops
->buffer_read_close(buf
);
337 int lttng_abi_create_event(struct file
*channel_file
,
338 struct lttng_event __user
*uevent_param
)
340 struct ltt_channel
*channel
= channel_file
->private_data
;
341 const struct lttng_event_desc
*event_desc
;
342 struct ltt_event
*event
;
344 struct lttng_event event_param
;
346 struct file
*event_file
;
348 if (copy_from_user(&event_param
, uevent_param
, sizeof(event_param
)))
350 event_name
= kmalloc(PATH_MAX
, GFP_KERNEL
);
353 if (strncpy_from_user(event_name
, uevent_param
->name
, PATH_MAX
) < 0) {
357 event_name
[PATH_MAX
- 1] = '\0';
359 event_desc
= ltt_event_get(event_name
);
364 event_fd
= get_unused_fd();
369 event_file
= anon_inode_getfile("[lttng_event]",
372 if (IS_ERR(event_file
)) {
373 ret
= PTR_ERR(event_file
);
377 * We tolerate no failure path after event creation. It will stay
378 * invariant for the rest of the session.
380 event
= ltt_event_create(channel
, event_name
, event_param
.itype
,
386 event_file
->private_data
= event
;
387 fd_install(event_fd
, event_file
);
388 /* The event holds a reference on the channel */
389 atomic_long_inc(&channel_file
->f_count
);
396 put_unused_fd(event_fd
);
398 ltt_event_put(event_desc
);
406 * lttng_channel_ioctl - lttng syscall through ioctl
412 * This ioctl implements lttng commands:
414 * Returns an event stream file descriptor or failure.
415 * (typically, one event stream records events from one CPU)
417 * Returns an event file descriptor or failure.
419 * Channel and event file descriptors also hold a reference on the session.
422 long lttng_channel_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
426 return lttng_abi_open_stream(file
);
428 return lttng_abi_create_event(file
, (struct lttng_event __user
*)arg
);
435 * lttng_metadata_ioctl - lttng syscall through ioctl
441 * This ioctl implements lttng commands:
443 * Returns an event stream file descriptor or failure.
445 * Channel and event file descriptors also hold a reference on the session.
448 long lttng_metadata_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
452 return lttng_abi_open_stream(file
);
461 * lttng_channel_poll - lttng stream addition/removal monitoring
466 unsigned int lttng_channel_poll(struct file
*file
, poll_table
*wait
)
468 struct ltt_channel
*channel
= file
->private_data
;
469 unsigned int mask
= 0;
471 if (file
->f_mode
& FMODE_READ
) {
472 poll_wait_set_exclusive(wait
);
473 poll_wait(file
, &channel
->notify_wait
, wait
);
475 /* TODO: identify when the channel is being finalized. */
479 return POLLIN
| POLLRDNORM
;
487 int lttng_channel_release(struct inode
*inode
, struct file
*file
)
489 struct ltt_channel
*channel
= file
->private_data
;
492 fput(channel
->session
->file
);
496 static const struct file_operations lttng_channel_fops
= {
497 .release
= lttng_channel_release
,
500 .poll
= lttng_channel_poll
,
502 .unlocked_ioctl
= lttng_channel_ioctl
,
504 .compat_ioctl
= lttng_channel_ioctl
,
508 static const struct file_operations lttng_metadata_fops
= {
509 .release
= lttng_channel_release
,
510 .unlocked_ioctl
= lttng_metadata_ioctl
,
512 .compat_ioctl
= lttng_metadata_ioctl
,
517 int lttng_event_release(struct inode
*inode
, struct file
*file
)
519 struct ltt_event
*event
= file
->private_data
;
522 fput(event
->chan
->file
);
526 /* TODO: filter control ioctl */
527 static const struct file_operations lttng_event_fops
= {
528 .release
= lttng_event_release
,
531 int __init
ltt_debugfs_abi_init(void)
535 wrapper_vmalloc_sync_all();
536 lttng_dentry
= debugfs_create_file("lttng", S_IWUSR
, NULL
, NULL
,
538 if (IS_ERR(lttng_dentry
) || !lttng_dentry
) {
539 printk(KERN_ERR
"Error creating LTTng control file\n");
547 void __exit
ltt_debugfs_abi_exit(void)
549 debugfs_remove(lttng_dentry
);