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 fd_install(session_fd
, session_filp
);
82 put_unused_fd(session_fd
);
84 ltt_session_destroy(session
);
89 * lttng_ioctl - lttng syscall through ioctl
95 * This ioctl implements lttng commands:
97 * Returns a LTTng trace session file descriptor
99 * The returned session will be deleted when its file descriptor is closed.
102 long lttng_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg
)
106 return lttng_abi_create_session();
112 static const struct file_operations lttng_fops
= {
113 .unlocked_ioctl
= lttng_ioctl
,
115 .compat_ioctl
= lttng_ioctl
,
119 int lttng_abi_create_channel(struct file
*session_filp
,
120 struct lttng_channel __user
*uchan_param
)
122 struct ltt_session
*session
= session_filp
->private_data
;
123 struct ltt_channel
*chan
;
124 struct file
*chan_filp
;
125 struct lttng_channel chan_param
;
129 if (copy_from_user(&chan_param
, uchan_param
, sizeof(chan_param
)))
131 chan_fd
= get_unused_fd_flags(O_RDWR
);
136 chan_filp
= anon_inode_getfile("[lttng_channel]",
139 if (IS_ERR(chan_filp
)) {
140 ret
= PTR_ERR(chan_filp
);
144 * We tolerate no failure path after channel creation. It will stay
145 * invariant for the rest of the session.
147 chan
= ltt_channel_create(session
, chan_param
->overwrite
, NULL
,
148 chan_param
->subbuf_size
,
149 chan_param
->num_subbuf
,
150 chan_param
->switch_timer_interval
,
151 chan_param
->read_timer_interval
);
156 chan_filp
->private_data
= chan
;
157 fd_install(chan_fd
, chan_filp
);
158 /* The channel created holds a reference on the session */
159 atomic_inc(&session_filp
->f_count
);
166 put_unused_fd(chan_fd
);
172 * lttng_session_ioctl - lttng session fd ioctl
178 * This ioctl implements lttng commands:
180 * Returns a LTTng channel file descriptor
182 * The returned channel will be deleted when its file descriptor is closed.
185 long lttng_session_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg
)
189 return lttng_abi_create_channel(filp
, (struct lttng_channel __user
*)arg
);
196 * Called when the last file reference is dropped.
198 * Big fat note: channels and events are invariant for the whole session after
199 * their creation. So this session destruction also destroys all channel and
200 * event structures specific to this session (they are not destroyed when their
201 * individual file is released).
204 int lttng_session_release(struct inode
*inode
, struct file
*file
)
206 struct ltt_session
*session
= file
->private_data
;
207 return ltt_session_destroy(session
);
210 static const struct file_operations lttng_session_fops
= {
211 .release
= lttng_session_release
,
212 .unlocked_ioctl
= lttng_session_ioctl
,
214 .compat_ioctl
= lttng_session_ioctl
,
219 int lttng_abi_open_stream(struct file
*channel_filp
)
221 struct ltt_channel
*channel
= channel_filp
->private_data
;
222 struct lib_ring_buffer
*buf
;
225 buf
= ltt_buffer_read_open(channel
->chan
);
229 stream_fd
= get_unused_fd_flags(O_RDWR
);
234 stream_filp
= anon_inode_getfile("[lttng_stream]",
235 &lib_ring_buffer_file_operations
,
237 if (IS_ERR(stream_filp
)) {
238 ret
= PTR_ERR(stream_filp
);
241 fd_install(stream_fd
, stream_filp
);
242 /* The stream holds a reference on the channel */
243 atomic_inc(&channel_filp
->f_count
);
247 put_unused_fd(stream_fd
);
249 ltt_buffer_read_close(buf
);
254 int lttng_abi_create_event(struct file
*channel_filp
,
255 struct lttng_event __user
*uevent_param
)
257 struct ltt_channel
*channel
= channel_filp
->private_data
;
258 struct ltt_event
*event
;
260 struct lttng_event event_param
;
263 if (copy_from_user(&event_param
, uevent_param
, sizeof(event_param
)))
265 event_name
= kmalloc(PATH_MAX
, GFP_KERNEL
);
268 if (strncpy_from_user(event_name
, &uevent_param
->name
, PATH_MAX
)) {
272 event_name
[PATH_MAX
- 1] = '\0';
273 event_fd
= get_unused_fd_flags(O_RDWR
);
278 event_filp
= anon_inode_getfile("[lttng_event]",
279 <tng_event_fops
, /* TODO: filter */
281 if (IS_ERR(event_filp
)) {
282 ret
= PTR_ERR(event_filp
);
286 * We tolerate no failure path after event creation. It will stay
287 * invariant for the rest of the session.
289 event
= ltt_event_create(channel
, event_param
->itype
, event_name
, NULL
);
294 event_filp
->private_data
= event
;
295 fd_install(event_fd
, event_filp
);
296 /* The event holds a reference on the channel */
297 atomic_inc(&channel_filp
->f_count
);
304 put_unused_fd(event_fd
);
312 * lttng_channel_ioctl - lttng syscall through ioctl
318 * This ioctl implements lttng commands:
320 * Returns an event stream file descriptor or failure.
321 * (typically, one event stream records events from one CPU)
323 * Returns an event file descriptor or failure.
325 * The returned session will be deleted when its file descriptor is closed.
326 * Channel and event file descriptors also hold a reference on the session.
329 long lttng_channel_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg
)
333 return lttng_abi_open_stream(filp
);
335 return lttng_abi_create_event(filp
, (struct lttng_event __user
*)arg
);
342 * lttng_channel_poll - lttng stream addition/removal monitoring
347 unsigned int lttng_channel_poll(struct file
*filp
, poll_table
*wait
)
349 struct ltt_channel
*channel
= filp
->private_data
;
350 unsigned int mask
= 0;
352 if (filp
->f_mode
& FMODE_READ
) {
353 poll_wait_set_exclusive(wait
);
354 poll_wait(filp
, &channel
->notify_wait
, wait
);
356 /* TODO: identify when the channel is being finalized. */
360 return POLLIN
| POLLRDNORM
;
366 static const struct file_operations lttng_channel_fops
= {
367 .release
= lttng_channel_release
,
368 .poll
= lttng_channel_poll
,
369 .unlocked_ioctl
= lttng_channel_ioctl
,
371 .compat_ioctl
= lttng_channel_ioctl
,
375 static int __init
ltt_debugfs_abi_init(void)
379 lttng_dentry
= debugfs_create_file("lttng", NULL
);
380 if (IS_ERR(lttng_dentry
) || !lttng_dentry
)
381 printk(KERN_ERR
"Error creating LTTng control file\n");
389 static void __exit
ltt_debugfs_abi_exit(void)
391 debugfs_remote(lttng_dentry
);