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/debugfs.h>
26 #include "ltt-events.h"
29 * This is LTTng's own personal way to create a system call as an external
30 * module. We use ioctl() on /sys/kernel/debug/lttng.
33 static struct dentry
*lttng_dentry
;
34 static const struct file_operations lttng_fops
;
35 static const struct file_operations lttng_session_fops
;
36 static const struct file_operations lttng_channel_fops
;
37 static const struct file_operations lttng_event_fops
;
40 * LTTng DebugFS ABI structures.
43 struct lttng_channel
{
44 int overwrite
; /* 1: overwrite, 0: discard */
47 unsigned int switch_timer_interval
;
48 unsigned int read_timer_interval
;
52 enum instrum_type itype
;
57 int lttng_abi_create_session(void)
59 struct ltt_session
*session
;
60 struct file
*session_filp
;
63 session
= ltt_session_create();
66 session_fd
= get_unused_fd_flags(O_RDWR
);
71 session_filp
= anon_inode_getfile("[lttng_session]",
74 if (IS_ERR(session_filp
)) {
75 ret
= PTR_ERR(session_filp
);
78 session
->file
= session_filp
;
79 fd_install(session_fd
, session_filp
);
83 put_unused_fd(session_fd
);
85 ltt_session_destroy(session
);
90 * lttng_ioctl - lttng syscall through ioctl
96 * This ioctl implements lttng commands:
98 * Returns a LTTng trace session file descriptor
100 * The returned session will be deleted when its file descriptor is closed.
103 long lttng_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg
)
107 return lttng_abi_create_session();
113 static const struct file_operations lttng_fops
= {
114 .unlocked_ioctl
= lttng_ioctl
,
116 .compat_ioctl
= lttng_ioctl
,
120 int lttng_abi_create_channel(struct file
*session_filp
,
121 struct lttng_channel __user
*uchan_param
)
123 struct ltt_session
*session
= session_filp
->private_data
;
124 struct ltt_channel
*chan
;
125 struct file
*chan_filp
;
126 struct lttng_channel chan_param
;
130 if (copy_from_user(&chan_param
, uchan_param
, sizeof(chan_param
)))
132 chan_fd
= get_unused_fd_flags(O_RDWR
);
137 chan_filp
= anon_inode_getfile("[lttng_channel]",
140 if (IS_ERR(chan_filp
)) {
141 ret
= PTR_ERR(chan_filp
);
145 * We tolerate no failure path after channel creation. It will stay
146 * invariant for the rest of the session.
148 chan
= ltt_channel_create(session
, chan_param
->overwrite
, NULL
,
149 chan_param
->subbuf_size
,
150 chan_param
->num_subbuf
,
151 chan_param
->switch_timer_interval
,
152 chan_param
->read_timer_interval
);
157 channel
->file
= chan_filp
;
158 chan_filp
->private_data
= chan
;
159 fd_install(chan_fd
, chan_filp
);
160 /* The channel created holds a reference on the session */
161 atomic_inc(&session_filp
->f_count
);
168 put_unused_fd(chan_fd
);
174 * lttng_session_ioctl - lttng session fd ioctl
180 * This ioctl implements lttng commands:
182 * Returns a LTTng channel file descriptor
184 * The returned channel will be deleted when its file descriptor is closed.
187 long lttng_session_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg
)
191 return lttng_abi_create_channel(filp
, (struct lttng_channel __user
*)arg
);
198 * Called when the last file reference is dropped.
200 * Big fat note: channels and events are invariant for the whole session after
201 * their creation. So this session destruction also destroys all channel and
202 * event structures specific to this session (they are not destroyed when their
203 * individual file is released).
206 int lttng_session_release(struct inode
*inode
, struct file
*file
)
208 struct ltt_session
*session
= file
->private_data
;
209 return ltt_session_destroy(session
);
212 static const struct file_operations lttng_session_fops
= {
213 .release
= lttng_session_release
,
214 .unlocked_ioctl
= lttng_session_ioctl
,
216 .compat_ioctl
= lttng_session_ioctl
,
221 int lttng_abi_open_stream(struct file
*channel_filp
)
223 struct ltt_channel
*channel
= channel_filp
->private_data
;
224 struct lib_ring_buffer
*buf
;
227 buf
= ltt_buffer_read_open(channel
->chan
);
231 stream_fd
= get_unused_fd_flags(O_RDWR
);
236 stream_filp
= anon_inode_getfile("[lttng_stream]",
237 &lib_ring_buffer_file_operations
,
239 if (IS_ERR(stream_filp
)) {
240 ret
= PTR_ERR(stream_filp
);
243 fd_install(stream_fd
, stream_filp
);
244 /* The stream holds a reference on the channel */
245 atomic_inc(&channel_filp
->f_count
);
249 put_unused_fd(stream_fd
);
251 ltt_buffer_read_close(buf
);
256 int lttng_abi_create_event(struct file
*channel_filp
,
257 struct lttng_event __user
*uevent_param
)
259 struct ltt_channel
*channel
= channel_filp
->private_data
;
260 struct ltt_event
*event
;
262 struct lttng_event event_param
;
265 if (copy_from_user(&event_param
, uevent_param
, sizeof(event_param
)))
267 event_name
= kmalloc(PATH_MAX
, GFP_KERNEL
);
270 if (strncpy_from_user(event_name
, &uevent_param
->name
, PATH_MAX
)) {
274 event_name
[PATH_MAX
- 1] = '\0';
275 event_fd
= get_unused_fd_flags(O_RDWR
);
280 event_filp
= anon_inode_getfile("[lttng_event]",
281 <tng_event_fops
, /* TODO: filter */
283 if (IS_ERR(event_filp
)) {
284 ret
= PTR_ERR(event_filp
);
288 * We tolerate no failure path after event creation. It will stay
289 * invariant for the rest of the session.
291 event
= ltt_event_create(channel
, event_param
->itype
, event_name
, NULL
);
296 event_filp
->private_data
= event
;
297 fd_install(event_fd
, event_filp
);
298 /* The event holds a reference on the channel */
299 atomic_inc(&channel_filp
->f_count
);
306 put_unused_fd(event_fd
);
314 * lttng_channel_ioctl - lttng syscall through ioctl
320 * This ioctl implements lttng commands:
322 * Returns an event stream file descriptor or failure.
323 * (typically, one event stream records events from one CPU)
325 * Returns an event file descriptor or failure.
327 * The returned session will be deleted when its file descriptor is closed.
328 * Channel and event file descriptors also hold a reference on the session.
331 long lttng_channel_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg
)
335 return lttng_abi_open_stream(filp
);
337 return lttng_abi_create_event(filp
, (struct lttng_event __user
*)arg
);
344 * lttng_channel_poll - lttng stream addition/removal monitoring
349 unsigned int lttng_channel_poll(struct file
*filp
, poll_table
*wait
)
351 struct ltt_channel
*channel
= filp
->private_data
;
352 unsigned int mask
= 0;
354 if (filp
->f_mode
& FMODE_READ
) {
355 poll_wait_set_exclusive(wait
);
356 poll_wait(filp
, &channel
->notify_wait
, wait
);
358 /* TODO: identify when the channel is being finalized. */
362 return POLLIN
| POLLRDNORM
;
369 int lttng_channel_release(struct inode
*inode
, struct file
*file
)
371 struct ltt_channel
*channel
= file
->private_data
;
372 fput(channel
->session
->file
);
376 static const struct file_operations lttng_channel_fops
= {
377 .release
= lttng_channel_release
,
378 .poll
= lttng_channel_poll
,
379 .unlocked_ioctl
= lttng_channel_ioctl
,
381 .compat_ioctl
= lttng_channel_ioctl
,
386 int lttng_event_release(struct inode
*inode
, struct file
*file
)
388 struct ltt_event
*event
= file
->private_data
;
389 fput(event
->chan
->file
);
393 static const struct file_operations lttng_event_fops
= {
394 .release
= lttng_event_release
,
397 static int __init
ltt_debugfs_abi_init(void)
401 lttng_dentry
= debugfs_create_file("lttng", NULL
);
402 if (IS_ERR(lttng_dentry
) || !lttng_dentry
)
403 printk(KERN_ERR
"Error creating LTTng control file\n");
411 static void __exit
ltt_debugfs_abi_exit(void)
413 debugfs_remote(lttng_dentry
);