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