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/symbols.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
;
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 * lttng_ioctl - lttng syscall through ioctl
94 * This ioctl implements lttng commands:
96 * Returns a LTTng trace session file descriptor
98 * The returned session will be deleted when its file descriptor is closed.
101 long lttng_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
105 return lttng_abi_create_session();
111 static const struct file_operations lttng_fops
= {
112 .unlocked_ioctl
= lttng_ioctl
,
114 .compat_ioctl
= lttng_ioctl
,
119 * We tolerate no failure in this function (if one happens, we print a dmesg
120 * error, but cannot return any error, because the channel information is
124 void lttng_metadata_create_events(struct file
*channel_file
)
126 struct ltt_channel
*channel
= channel_file
->private_data
;
127 char *event_name
= "lttng-metadata";
128 struct ltt_event
*event
;
132 probe
= ltt_probe_get(event_name
);
138 * We tolerate no failure path after event creation. It will stay
139 * invariant for the rest of the session.
141 event
= ltt_event_create(channel
, event_name
, INSTRUM_TRACEPOINTS
,
150 ltt_probe_put(probe
);
153 return; /* not allowed to return error */
157 int lttng_abi_create_channel(struct file
*session_file
,
158 struct lttng_channel __user
*uchan_param
,
159 enum channel_type channel_type
)
161 struct ltt_session
*session
= session_file
->private_data
;
162 const struct file_operations
*fops
;
163 const char *transport_name
;
164 struct ltt_channel
*chan
;
165 struct file
*chan_file
;
166 struct lttng_channel chan_param
;
170 if (copy_from_user(&chan_param
, uchan_param
, sizeof(chan_param
)))
172 chan_fd
= get_unused_fd();
177 chan_file
= anon_inode_getfile("[lttng_channel]",
180 if (IS_ERR(chan_file
)) {
181 ret
= PTR_ERR(chan_file
);
184 switch (channel_type
) {
185 case PER_CPU_CHANNEL
:
186 transport_name
= chan_param
.overwrite
?
187 "relay-overwrite" : "relay-discard";
188 fops
= <tng_channel_fops
;
191 transport_name
= chan_param
.overwrite
?
192 "global-relay-overwrite" : "global-relay-discard";
193 fops
= <tng_channel_fops
;
195 case METADATA_CHANNEL
:
196 transport_name
= "global-relay-discard";
197 fops
= <tng_metadata_fops
;
200 transport_name
= "<unknown>";
204 * We tolerate no failure path after channel creation. It will stay
205 * invariant for the rest of the session.
207 chan
= ltt_channel_create(session
, transport_name
, NULL
,
208 chan_param
.subbuf_size
,
209 chan_param
.num_subbuf
,
210 chan_param
.switch_timer_interval
,
211 chan_param
.read_timer_interval
);
216 chan
->file
= chan_file
;
217 chan_file
->private_data
= chan
;
218 fd_install(chan_fd
, chan_file
);
219 if (channel_type
== METADATA_CHANNEL
)
220 lttng_metadata_create_events(chan_file
);
222 /* The channel created holds a reference on the session */
223 atomic_long_inc(&session_file
->f_count
);
230 put_unused_fd(chan_fd
);
236 * lttng_session_ioctl - lttng session fd ioctl
242 * This ioctl implements lttng commands:
244 * Returns a LTTng channel file descriptor
246 * The returned channel will be deleted when its file descriptor is closed.
249 long lttng_session_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
251 struct ltt_session
*session
= file
->private_data
;
255 return lttng_abi_create_channel(file
,
256 (struct lttng_channel __user
*)arg
,
258 case LTTNG_SESSION_START
:
259 return ltt_session_start(session
);
260 case LTTNG_SESSION_STOP
:
261 return ltt_session_stop(session
);
263 return lttng_abi_create_channel(file
,
264 (struct lttng_channel __user
*)arg
,
272 * Called when the last file reference is dropped.
274 * Big fat note: channels and events are invariant for the whole session after
275 * their creation. So this session destruction also destroys all channel and
276 * event structures specific to this session (they are not destroyed when their
277 * individual file is released).
280 int lttng_session_release(struct inode
*inode
, struct file
*file
)
282 struct ltt_session
*session
= file
->private_data
;
285 ltt_session_destroy(session
);
289 static const struct file_operations lttng_session_fops
= {
290 .release
= lttng_session_release
,
291 .unlocked_ioctl
= lttng_session_ioctl
,
293 .compat_ioctl
= lttng_session_ioctl
,
298 int lttng_abi_open_stream(struct file
*channel_file
)
300 struct ltt_channel
*channel
= channel_file
->private_data
;
301 struct lib_ring_buffer
*buf
;
303 struct file
*stream_file
;
305 buf
= channel
->ops
->buffer_read_open(channel
->chan
);
309 stream_fd
= get_unused_fd();
314 stream_file
= anon_inode_getfile("[lttng_stream]",
315 &lib_ring_buffer_file_operations
,
317 if (IS_ERR(stream_file
)) {
318 ret
= PTR_ERR(stream_file
);
322 * OPEN_FMODE, called within anon_inode_getfile/alloc_file, don't honor
323 * FMODE_LSEEK, FMODE_PREAD nor FMODE_PWRITE. We need to read from this
324 * file descriptor, so we set FMODE_PREAD here.
326 stream_file
->f_mode
|= FMODE_PREAD
;
327 fd_install(stream_fd
, stream_file
);
329 * The stream holds a reference to the channel within the generic ring
330 * buffer library, so no need to hold a refcount on the channel and
331 * session files here.
336 put_unused_fd(stream_fd
);
338 channel
->ops
->buffer_read_close(buf
);
343 int lttng_abi_create_event(struct file
*channel_file
,
344 struct lttng_event __user
*uevent_param
)
346 struct ltt_channel
*channel
= channel_file
->private_data
;
347 struct ltt_event
*event
;
349 struct lttng_event event_param
;
351 struct file
*event_file
;
354 if (copy_from_user(&event_param
, uevent_param
, sizeof(event_param
)))
356 event_name
= kmalloc(PATH_MAX
, GFP_KERNEL
);
359 if (strncpy_from_user(event_name
, uevent_param
->name
, PATH_MAX
) < 0) {
363 event_name
[PATH_MAX
- 1] = '\0';
365 probe
= ltt_probe_get(event_name
);
370 event_fd
= get_unused_fd();
375 event_file
= anon_inode_getfile("[lttng_event]",
378 if (IS_ERR(event_file
)) {
379 ret
= PTR_ERR(event_file
);
383 * We tolerate no failure path after event creation. It will stay
384 * invariant for the rest of the session.
386 event
= ltt_event_create(channel
, event_name
, event_param
.itype
,
392 event_file
->private_data
= event
;
393 fd_install(event_fd
, event_file
);
394 /* The event holds a reference on the channel */
395 atomic_long_inc(&channel_file
->f_count
);
402 put_unused_fd(event_fd
);
404 ltt_probe_put(probe
);
412 * lttng_channel_ioctl - lttng syscall through ioctl
418 * This ioctl implements lttng commands:
420 * Returns an event stream file descriptor or failure.
421 * (typically, one event stream records events from one CPU)
423 * Returns an event file descriptor or failure.
425 * Channel and event file descriptors also hold a reference on the session.
428 long lttng_channel_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
432 return lttng_abi_open_stream(file
);
434 return lttng_abi_create_event(file
, (struct lttng_event __user
*)arg
);
441 * lttng_metadata_ioctl - lttng syscall through ioctl
447 * This ioctl implements lttng commands:
449 * Returns an event stream file descriptor or failure.
451 * Channel and event file descriptors also hold a reference on the session.
454 long lttng_metadata_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
458 return lttng_abi_open_stream(file
);
467 * lttng_channel_poll - lttng stream addition/removal monitoring
472 unsigned int lttng_channel_poll(struct file
*file
, poll_table
*wait
)
474 struct ltt_channel
*channel
= file
->private_data
;
475 unsigned int mask
= 0;
477 if (file
->f_mode
& FMODE_READ
) {
478 poll_wait_set_exclusive(wait
);
479 poll_wait(file
, &channel
->notify_wait
, wait
);
481 /* TODO: identify when the channel is being finalized. */
485 return POLLIN
| POLLRDNORM
;
493 int lttng_channel_release(struct inode
*inode
, struct file
*file
)
495 struct ltt_channel
*channel
= file
->private_data
;
498 fput(channel
->session
->file
);
502 static const struct file_operations lttng_channel_fops
= {
503 .release
= lttng_channel_release
,
506 .poll
= lttng_channel_poll
,
508 .unlocked_ioctl
= lttng_channel_ioctl
,
510 .compat_ioctl
= lttng_channel_ioctl
,
514 static const struct file_operations lttng_metadata_fops
= {
515 .release
= lttng_channel_release
,
516 .unlocked_ioctl
= lttng_metadata_ioctl
,
518 .compat_ioctl
= lttng_metadata_ioctl
,
523 int lttng_event_release(struct inode
*inode
, struct file
*file
)
525 struct ltt_event
*event
= file
->private_data
;
528 fput(event
->chan
->file
);
532 /* TODO: filter control ioctl */
533 static const struct file_operations lttng_event_fops
= {
534 .release
= lttng_event_release
,
537 int __init
ltt_debugfs_abi_init(void)
541 wrapper_vmalloc_sync_all();
542 lttng_dentry
= debugfs_create_file("lttng", S_IWUSR
, NULL
, NULL
,
544 if (IS_ERR(lttng_dentry
) || !lttng_dentry
) {
545 printk(KERN_ERR
"Error creating LTTng control file\n");
553 void __exit
ltt_debugfs_abi_exit(void)
555 debugfs_remove(lttng_dentry
);