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
;
39 * LTTng DebugFS ABI structures.
42 struct lttng_channel
{
43 int session
; /* Session file descriptor */
44 int overwrite
; /* 1: overwrite, 0: discard */
47 unsigned int switch_timer_interval
;
48 unsigned int read_timer_interval
;
52 int channel
; /* Channel file descriptor */
53 enum instrum_type itype
;
58 int lttng_abi_create_session(void)
60 struct ltt_session
*session
;
61 struct file
*session_file
;
64 session
= ltt_session_create()
67 session_fd
= get_unused_fd_flags(O_RDWR
);
72 session_file
= anon_inode_getfile("[lttng_session]",
75 if (IS_ERR(session_file
)) {
76 ret
= PTR_ERR(session_file
);
79 fd_install(session_fd
, session_file
);
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();
115 long lttng_compat_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg
)
119 return lttng_abi_create_session();
126 static const struct file_operations lttng_fops
= {
127 .unlocked_ioctl
= lttng_ioctl
,
129 .compat_ioctl
= lttng_compat_ioctl
,
133 int lttng_abi_create_channel(struct file
*session_filp
,
134 struct lttng_channel __user
*uchan_param
)
136 struct ltt_session
*session
= session_filp
->private_data
;
137 struct ltt_channel
*chan
;
138 struct file
*chan_filp
;
139 struct lttng_channel chan_param
;
143 if (copy_from_user(&chan_param
, ucham_param
, sizeof(chan_param
)))
145 chan
= ltt_channel_create(session
, chan_param
->overwrite
, NULL
,
146 chan_param
->subbuf_size
,
147 chan_param
->num_subbuf
,
148 chan_param
->switch_timer_interval
,
149 chan_param
->read_timer_interval
);
154 chan_fd
= get_unused_fd_flags(O_RDWR
);
159 chan_filp
= anon_inode_getfile("[lttng_channel]",
162 if (IS_ERR(chan_filp
)) {
163 ret
= PTR_ERR(chan_filp
);
167 /* The channel created holds a reference on the session */
168 atomic_inc(&session_filp
->f_count
);
173 put_unused_fd(chan_fd
);
175 ltt_channel_destroy(chan
);
181 * lttng_session_ioctl - lttng session fd ioctl
187 * This ioctl implements lttng commands:
189 * Returns a LTTng channel file descriptor
191 * The returned channel will be deleted when its file descriptor is closed.
194 long lttng_session_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg
)
198 return lttng_abi_create_channel(filp
, (struct lttng_channel __user
*)arg
);
206 long lttng_session_compat_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg
)
210 return lttng_abi_create_channel(filp
, (struct lttng_channel __user
*)arg
);
217 static const struct file_operations lttng_session_fops
= {
218 .unlocked_ioctl
= lttng_session_ioctl
,
220 .compat_ioctl
= lttng_session_compat_ioctl
,
225 int lttng_abi_open_stream(struct file
*channel_filp
)
227 struct ltt_channel
*channel
= channel_filp
->private_data
;
228 struct lib_ring_buffer
*buf
;
231 buf
= ltt_buffer_read_open(channel
->chan
);
235 stream_fd
= get_unused_fd_flags(O_RDWR
);
240 stream_filp
= anon_inode_getfile("[lttng_stream]",
243 if (IS_ERR(stream_filp
)) {
244 ret
= PTR_ERR(stream_filp
);
248 /* The stream holds a reference on the channel */
249 atomic_inc(&channel_filp
->f_count
);
253 put_unused_fd(stream_fd
);
255 ltt_buffer_read_close(buf
);
261 * lttng_channel_ioctl - lttng syscall through ioctl
267 * This ioctl implements lttng commands:
269 * Returns an event stream file descriptor or failure.
270 * (typically, one event stream records events from one CPU)
271 * LTTNG_STREAM_NOTIFIER
272 * Returns a file descriptor that can be used to monitor
273 * addition/removal of streams to/from a channel. (e.g. notifier
274 * called on CPU hotplug).
276 * Returns an event file descriptor or failure.
278 * The returned session will be deleted when its file descriptor is closed.
279 * Channel and event file descriptors also hold a reference on the session.
282 long lttng_channel_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg
)
286 return lttng_abi_open_stream(filp
);
287 case LTTNG_STREAM_NOTIFIER
:
288 return lttng_abi_open_stream_notifier(filp
);
290 return lttng_abi_create_event(filp
, (struct lttng_event __user
*)arg
);
298 long lttng_channel_compat_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg
)
302 return lttng_abi_get_stream(filp
);
303 case LTTNG_STREAM_NOTIFIER
:
304 return lttng_abi_get_stream_notifier(filp
);
306 return lttng_abi_create_event(filp
, (struct lttng_event __user
*)arg
);
313 static const struct file_operations lttng_channel_fops
= {
314 .unlocked_ioctl
= lttng_channel_ioctl
,
316 .compat_ioctl
= lttng_channel_compat_ioctl
,
320 static int __init
ltt_debugfs_abi_init(void)
324 lttng_dentry
= debugfs_create_file("lttng", NULL
);
325 if (IS_ERR(lttng_dentry
) || !lttng_dentry
)
326 printk(KERN_ERR
"Error creating LTTng control file\n");
334 static void __exit
ltt_debugfs_abi_exit(void)
336 debugfs_remote(lttng_dentry
);