Add file operations on streams
[lttng-modules.git] / ltt-debugfs-abi.c
CommitLineData
baf20995
MD
1/*
2 * ltt-debugfs-abi.c
3 *
4 * Copyright 2010 (c) - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 *
6 * LTTng debugfs ABI
7 *
8 * Mimic system calls for:
9 * - session creation, returns a file descriptor or failure.
ad1c05e1
MD
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.
baf20995
MD
23 */
24
25#include <linux/debugfs.h>
ad1c05e1 26#include "ltt-events.h"
baf20995
MD
27
28/*
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.
31 */
32
33static struct dentry *lttng_dentry;
ad1c05e1
MD
34static const struct file_operations lttng_fops;
35static const struct file_operations lttng_session_fops;
36static const struct file_operations lttng_channel_fops;
baf20995
MD
37
38/*
39 * LTTng DebugFS ABI structures.
40 */
41
42struct lttng_channel {
baf20995
MD
43 int overwrite; /* 1: overwrite, 0: discard */
44 u64 subbuf_size;
45 u64 num_subbuf;
46 unsigned int switch_timer_interval;
47 unsigned int read_timer_interval;
48};
49
50struct lttng_event {
baf20995
MD
51 enum instrum_type itype;
52 char name[];
53};
54
ad1c05e1 55static
baf20995
MD
56int lttng_abi_create_session(void)
57{
58 struct ltt_session *session;
03037b98 59 struct file *session_filp;
baf20995
MD
60 int session_fd;
61
653fe716 62 session = ltt_session_create();
baf20995
MD
63 if (!session)
64 return -ENOMEM;
65 session_fd = get_unused_fd_flags(O_RDWR);
66 if (session_fd < 0) {
67 ret = session_fd;
68 goto fd_error;
69 }
03037b98 70 session_filp = anon_inode_getfile("[lttng_session]",
ad1c05e1 71 &lttng_session_fops,
baf20995 72 session, O_RDWR);
03037b98
MD
73 if (IS_ERR(session_filp)) {
74 ret = PTR_ERR(session_filp);
baf20995
MD
75 goto file_error;
76 }
03037b98 77 fd_install(session_fd, session_filp);
baf20995
MD
78 return session_fd;
79
80file_error:
81 put_unused_fd(session_fd);
82fd_error:
83 ltt_session_destroy(session);
84 return ret;
85}
86
ad1c05e1
MD
87/**
88 * lttng_ioctl - lttng syscall through ioctl
89 *
90 * @filp: the file
91 * @cmd: the command
92 * @arg: command arg
93 *
94 * This ioctl implements lttng commands:
95 * LTTNG_SESSION
96 * Returns a LTTng trace session file descriptor
97 *
98 * The returned session will be deleted when its file descriptor is closed.
99 */
100static
101long lttng_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
102{
103 switch (cmd) {
104 case LTTNG_SESSION:
105 return lttng_abi_create_session();
106 default:
107 return -ENOIOCTLCMD;
108 }
109}
110
ad1c05e1
MD
111static const struct file_operations lttng_fops = {
112 .unlocked_ioctl = lttng_ioctl,
113#ifdef CONFIG_COMPAT
03037b98 114 .compat_ioctl = lttng_ioctl,
ad1c05e1
MD
115#endif
116}
117
118int lttng_abi_create_channel(struct file *session_filp,
119 struct lttng_channel __user *uchan_param)
baf20995 120{
ad1c05e1 121 struct ltt_session *session = session_filp->private_data;
baf20995 122 struct ltt_channel *chan;
ad1c05e1 123 struct file *chan_filp;
baf20995
MD
124 struct lttng_channel chan_param;
125 int chan_fd;
ad1c05e1 126 int ret = 0;
baf20995 127
653fe716 128 if (copy_from_user(&chan_param, uchan_param, sizeof(chan_param)))
baf20995 129 return -EFAULT;
baf20995
MD
130 chan_fd = get_unused_fd_flags(O_RDWR);
131 if (chan_fd < 0) {
132 ret = chan_fd;
133 goto fd_error;
134 }
ad1c05e1
MD
135 chan_filp = anon_inode_getfile("[lttng_channel]",
136 &lttng_channel_fops,
03037b98 137 NULL, O_RDWR);
ad1c05e1
MD
138 if (IS_ERR(chan_filp)) {
139 ret = PTR_ERR(chan_filp);
baf20995
MD
140 goto file_error;
141 }
03037b98
MD
142 /*
143 * We tolerate no failure path after channel creation. It will stay
144 * invariant for the rest of the session.
145 */
146 chan = ltt_channel_create(session, chan_param->overwrite, NULL,
147 chan_param->subbuf_size,
148 chan_param->num_subbuf,
149 chan_param->switch_timer_interval,
150 chan_param->read_timer_interval);
151 if (!chan) {
152 ret = -ENOMEM;
153 goto chan_error;
154 }
155 chan_filp->private_data = chan;
156 fd_install(chan_fd, chan_filp);
ad1c05e1
MD
157 /* The channel created holds a reference on the session */
158 atomic_inc(&session_filp->f_count);
159
baf20995
MD
160 return chan_fd;
161
03037b98
MD
162chan_error:
163 fput(chan_filp);
baf20995
MD
164file_error:
165 put_unused_fd(chan_fd);
166fd_error:
baf20995
MD
167 return ret;
168}
169
170/**
ad1c05e1 171 * lttng_session_ioctl - lttng session fd ioctl
baf20995
MD
172 *
173 * @filp: the file
174 * @cmd: the command
175 * @arg: command arg
176 *
177 * This ioctl implements lttng commands:
baf20995
MD
178 * LTTNG_CHANNEL
179 * Returns a LTTng channel file descriptor
ad1c05e1
MD
180 *
181 * The returned channel will be deleted when its file descriptor is closed.
182 */
183static
184long lttng_session_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
185{
186 switch (cmd) {
187 case LTTNG_CHANNEL:
188 return lttng_abi_create_channel(filp, (struct lttng_channel __user *)arg);
189 default:
190 return -ENOIOCTLCMD;
191 }
192}
193
03037b98
MD
194/*
195 * Called when the last file reference is dropped.
196 *
197 * Big fat note: channels and events are invariant for the whole session after
198 * their creation. So this session destruction also destroys all channel and
199 * event structures specific to this session (they are not destroyed when their
200 * individual file is released).
201 */
ad1c05e1 202static
03037b98 203int lttng_session_release(struct inode *inode, struct file *file)
ad1c05e1 204{
03037b98
MD
205 struct ltt_session *session = file->private_data;
206 return ltt_session_destroy(session);
ad1c05e1 207}
ad1c05e1
MD
208
209static const struct file_operations lttng_session_fops = {
03037b98 210 .release = lttng_session_release,
ad1c05e1
MD
211 .unlocked_ioctl = lttng_session_ioctl,
212#ifdef CONFIG_COMPAT
03037b98 213 .compat_ioctl = lttng_session_ioctl,
ad1c05e1
MD
214#endif
215}
216
217static
218int lttng_abi_open_stream(struct file *channel_filp)
219{
220 struct ltt_channel *channel = channel_filp->private_data;
221 struct lib_ring_buffer *buf;
222 int stream_fd, ret;
223
224 buf = ltt_buffer_read_open(channel->chan);
225 if (!buf)
226 return -ENOENT;
227
228 stream_fd = get_unused_fd_flags(O_RDWR);
229 if (stream_fd < 0) {
230 ret = stream_fd;
231 goto fd_error;
232 }
233 stream_filp = anon_inode_getfile("[lttng_stream]",
7f57c73c 234 &lib_ring_buffer_file_operations,
ad1c05e1
MD
235 buf, O_RDWR);
236 if (IS_ERR(stream_filp)) {
237 ret = PTR_ERR(stream_filp);
238 goto file_error;
239 }
03037b98 240 fd_install(stream_fd, stream_filp);
ad1c05e1
MD
241 /* The stream holds a reference on the channel */
242 atomic_inc(&channel_filp->f_count);
243 return stream_fd;
244
245file_error:
246 put_unused_fd(stream_fd);
247fd_error:
248 ltt_buffer_read_close(buf);
249 return ret;
250}
251
653fe716
MD
252static
253int lttng_abi_create_event(struct file *channel_filp,
254 struct lttng_event __user *uevent_param)
255{
256 struct ltt_channel *channel = channel_filp->private_data;
257 struct ltt_event *event;
258 char *event_name;
259 struct lttng_event event_param;
260 int event_fd, ret;
261
262 if (copy_from_user(&event_param, uevent_param, sizeof(event_param)))
263 return -EFAULT;
264 event_name = kmalloc(PATH_MAX, GFP_KERNEL);
265 if (!event_name)
266 return -ENOMEM;
267 if (strncpy_from_user(event_name, &uevent_param->name, PATH_MAX)) {
268 ret = -EFAULT;
269 goto name_error;
270 }
271 event_name[PATH_MAX - 1] = '\0';
653fe716
MD
272 event_fd = get_unused_fd_flags(O_RDWR);
273 if (event_fd < 0) {
274 ret = event_fd;
275 goto fd_error;
276 }
277 event_filp = anon_inode_getfile("[lttng_event]",
03037b98
MD
278 &lttng_event_fops,
279 NULL, O_RDWR);
653fe716
MD
280 if (IS_ERR(event_filp)) {
281 ret = PTR_ERR(event_filp);
282 goto file_error;
283 }
03037b98
MD
284 /*
285 * We tolerate no failure path after event creation. It will stay
286 * invariant for the rest of the session.
287 */
288 event = ltt_event_create(channel, event_param->itype, event_name, NULL);
289 if (!event) {
290 goto event_error;
291 ret = -EEXIST;
292 }
293 event_filp->private_data = event;
294 fd_install(event_fd, event_filp);
653fe716
MD
295 /* The event holds a reference on the channel */
296 atomic_inc(&channel_filp->f_count);
03037b98 297 kfree(event_name);
653fe716
MD
298 return event_fd;
299
03037b98
MD
300event_error:
301 fput(event_filp);
653fe716
MD
302file_error:
303 put_unused_fd(event_fd);
304fd_error:
653fe716
MD
305name_error:
306 kfree(event_name);
307 return ret;
308}
ad1c05e1
MD
309
310/**
311 * lttng_channel_ioctl - lttng syscall through ioctl
312 *
313 * @filp: the file
314 * @cmd: the command
315 * @arg: command arg
316 *
317 * This ioctl implements lttng commands:
318 * LTTNG_STREAM
319 * Returns an event stream file descriptor or failure.
320 * (typically, one event stream records events from one CPU)
baf20995 321 * LTTNG_EVENT
ad1c05e1 322 * Returns an event file descriptor or failure.
baf20995
MD
323 *
324 * The returned session will be deleted when its file descriptor is closed.
325 * Channel and event file descriptors also hold a reference on the session.
326 */
ad1c05e1
MD
327static
328long lttng_channel_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
baf20995 329{
baf20995 330 switch (cmd) {
ad1c05e1
MD
331 case LTTNG_STREAM:
332 return lttng_abi_open_stream(filp);
baf20995 333 case LTTNG_EVENT:
ad1c05e1 334 return lttng_abi_create_event(filp, (struct lttng_event __user *)arg);
baf20995
MD
335 default:
336 return -ENOIOCTLCMD;
337 }
338}
339
653fe716
MD
340/**
341 * lttng_channel_poll - lttng stream addition/removal monitoring
342 *
343 * @filp: the file
344 * @wait: poll table
345 */
346unsigned int lttng_channel_poll(struct file *filp, poll_table *wait)
347{
348 struct ltt_channel *channel = filp->private_data;
349 unsigned int mask = 0;
350
351 if (filp->f_mode & FMODE_READ) {
352 poll_wait_set_exclusive(wait);
353 poll_wait(filp, &channel->notify_wait, wait);
354
355 /* TODO: identify when the channel is being finalized. */
356 if (finalized)
357 return POLLHUP;
358 else
359 return POLLIN | POLLRDNORM;
360 }
361 return mask;
362
363}
364
ad1c05e1 365static const struct file_operations lttng_channel_fops = {
03037b98 366 .release = lttng_channel_release,
653fe716 367 .poll = lttng_channel_poll,
ad1c05e1 368 .unlocked_ioctl = lttng_channel_ioctl,
baf20995 369#ifdef CONFIG_COMPAT
03037b98 370 .compat_ioctl = lttng_channel_ioctl,
baf20995
MD
371#endif
372}
373
374static int __init ltt_debugfs_abi_init(void)
375{
376 int ret = 0;
377
378 lttng_dentry = debugfs_create_file("lttng", NULL);
379 if (IS_ERR(lttng_dentry) || !lttng_dentry)
380 printk(KERN_ERR "Error creating LTTng control file\n");
381 ret = -ENOMEM;
382 goto error;
383 }
384error:
385 return ret;
386}
387
388static void __exit ltt_debugfs_abi_exit(void)
389{
390 debugfs_remote(lttng_dentry);
391}
This page took 0.039711 seconds and 4 git commands to generate.