Import lib ring buffer into LTTng modules
[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
11b5a3c2 25#include <linux/module.h>
baf20995 26#include <linux/debugfs.h>
11b5a3c2
MD
27#include <linux/anon_inodes.h>
28#include <linux/file.h>
29#include <linux/uaccess.h>
30#include <linux/slab.h>
f3bc08c5 31#include "wrapper/ringbuffer/vfs.h"
11b5a3c2 32#include "ltt-debugfs-abi.h"
ad1c05e1 33#include "ltt-events.h"
baf20995
MD
34
35/*
36 * This is LTTng's own personal way to create a system call as an external
37 * module. We use ioctl() on /sys/kernel/debug/lttng.
38 */
39
40static struct dentry *lttng_dentry;
ad1c05e1
MD
41static const struct file_operations lttng_fops;
42static const struct file_operations lttng_session_fops;
43static const struct file_operations lttng_channel_fops;
5dbbdb43 44static const struct file_operations lttng_metadata_fops;
305d3e42 45static const struct file_operations lttng_event_fops;
baf20995 46
5dbbdb43
MD
47enum channel_type {
48 PER_CPU_CHANNEL,
49 GLOBAL_CHANNEL,
50 METADATA_CHANNEL,
51};
52
ad1c05e1 53static
baf20995
MD
54int lttng_abi_create_session(void)
55{
56 struct ltt_session *session;
c0e31d2e 57 struct file *session_file;
11b5a3c2 58 int session_fd, ret;
baf20995 59
653fe716 60 session = ltt_session_create();
baf20995
MD
61 if (!session)
62 return -ENOMEM;
1c25284c 63 session_fd = get_unused_fd();
baf20995
MD
64 if (session_fd < 0) {
65 ret = session_fd;
66 goto fd_error;
67 }
c0e31d2e 68 session_file = anon_inode_getfile("[lttng_session]",
ad1c05e1 69 &lttng_session_fops,
baf20995 70 session, O_RDWR);
c0e31d2e
MD
71 if (IS_ERR(session_file)) {
72 ret = PTR_ERR(session_file);
baf20995
MD
73 goto file_error;
74 }
c0e31d2e
MD
75 session->file = session_file;
76 fd_install(session_fd, session_file);
baf20995
MD
77 return session_fd;
78
79file_error:
80 put_unused_fd(session_fd);
81fd_error:
82 ltt_session_destroy(session);
83 return ret;
84}
85
ad1c05e1
MD
86/**
87 * lttng_ioctl - lttng syscall through ioctl
88 *
c0e31d2e 89 * @file: the file
ad1c05e1
MD
90 * @cmd: the command
91 * @arg: command arg
92 *
93 * This ioctl implements lttng commands:
94 * LTTNG_SESSION
95 * Returns a LTTng trace session file descriptor
96 *
97 * The returned session will be deleted when its file descriptor is closed.
98 */
99static
c0e31d2e 100long lttng_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
ad1c05e1
MD
101{
102 switch (cmd) {
103 case LTTNG_SESSION:
104 return lttng_abi_create_session();
105 default:
106 return -ENOIOCTLCMD;
107 }
108}
109
ad1c05e1
MD
110static const struct file_operations lttng_fops = {
111 .unlocked_ioctl = lttng_ioctl,
112#ifdef CONFIG_COMPAT
03037b98 113 .compat_ioctl = lttng_ioctl,
ad1c05e1 114#endif
11b5a3c2 115};
ad1c05e1 116
5dbbdb43
MD
117/*
118 * We tolerate no failure in this function (if one happens, we print a dmesg
119 * error, but cannot return any error, because the channel information is
120 * invariant.
121 */
122static
123void lttng_metadata_create_events(struct file *channel_file)
124{
125 struct ltt_channel *channel = channel_file->private_data;
126 char *event_name = "lttng-metadata";
127 struct ltt_event *event;
128 int ret;
129 void *probe;
130
131 probe = ltt_probe_get(event_name);
132 if (!probe) {
133 ret = -ENOENT;
134 goto probe_error;
135 }
136 /*
137 * We tolerate no failure path after event creation. It will stay
138 * invariant for the rest of the session.
139 */
140 event = ltt_event_create(channel, event_name, INSTRUM_TRACEPOINTS,
141 probe, NULL);
142 if (!event) {
143 goto event_error;
144 ret = -EEXIST;
145 }
146 return;
147
148event_error:
149 ltt_probe_put(probe);
150probe_error:
151 WARN_ON(1);
152 return; /* not allowed to return error */
153}
154
155static
c0e31d2e 156int lttng_abi_create_channel(struct file *session_file,
5dbbdb43
MD
157 struct lttng_channel __user *uchan_param,
158 enum channel_type channel_type)
baf20995 159{
c0e31d2e 160 struct ltt_session *session = session_file->private_data;
5dbbdb43
MD
161 const struct file_operations *fops;
162 const char *transport_name;
baf20995 163 struct ltt_channel *chan;
c0e31d2e 164 struct file *chan_file;
baf20995
MD
165 struct lttng_channel chan_param;
166 int chan_fd;
ad1c05e1 167 int ret = 0;
baf20995 168
653fe716 169 if (copy_from_user(&chan_param, uchan_param, sizeof(chan_param)))
baf20995 170 return -EFAULT;
1c25284c 171 chan_fd = get_unused_fd();
baf20995
MD
172 if (chan_fd < 0) {
173 ret = chan_fd;
174 goto fd_error;
175 }
c0e31d2e 176 chan_file = anon_inode_getfile("[lttng_channel]",
ad1c05e1 177 &lttng_channel_fops,
03037b98 178 NULL, O_RDWR);
c0e31d2e
MD
179 if (IS_ERR(chan_file)) {
180 ret = PTR_ERR(chan_file);
baf20995
MD
181 goto file_error;
182 }
5dbbdb43
MD
183 switch (channel_type) {
184 case PER_CPU_CHANNEL:
185 transport_name = chan_param.overwrite ?
186 "relay-overwrite" : "relay-discard";
187 fops = &lttng_channel_fops;
188 break;
189 case GLOBAL_CHANNEL:
190 transport_name = chan_param.overwrite ?
191 "global-relay-overwrite" : "global-relay-discard";
192 fops = &lttng_channel_fops;
193 break;
194 case METADATA_CHANNEL:
195 transport_name = "global-relay-discard";
196 fops = &lttng_metadata_fops;
197 break;
198 default:
199 transport_name = "<unknown>";
200 break;
201 }
03037b98
MD
202 /*
203 * We tolerate no failure path after channel creation. It will stay
204 * invariant for the rest of the session.
205 */
5dbbdb43 206 chan = ltt_channel_create(session, transport_name, NULL,
11b5a3c2
MD
207 chan_param.subbuf_size,
208 chan_param.num_subbuf,
209 chan_param.switch_timer_interval,
210 chan_param.read_timer_interval);
03037b98 211 if (!chan) {
f3d01b96 212 ret = -EINVAL;
03037b98
MD
213 goto chan_error;
214 }
11b5a3c2 215 chan->file = chan_file;
c0e31d2e
MD
216 chan_file->private_data = chan;
217 fd_install(chan_fd, chan_file);
5dbbdb43
MD
218 if (channel_type == METADATA_CHANNEL)
219 lttng_metadata_create_events(chan_file);
220
ad1c05e1 221 /* The channel created holds a reference on the session */
b0caa15a 222 atomic_long_inc(&session_file->f_count);
ad1c05e1 223
baf20995
MD
224 return chan_fd;
225
03037b98 226chan_error:
c0e31d2e 227 fput(chan_file);
baf20995
MD
228file_error:
229 put_unused_fd(chan_fd);
230fd_error:
baf20995
MD
231 return ret;
232}
233
234/**
ad1c05e1 235 * lttng_session_ioctl - lttng session fd ioctl
baf20995 236 *
c0e31d2e 237 * @file: the file
baf20995
MD
238 * @cmd: the command
239 * @arg: command arg
240 *
241 * This ioctl implements lttng commands:
baf20995
MD
242 * LTTNG_CHANNEL
243 * Returns a LTTng channel file descriptor
ad1c05e1
MD
244 *
245 * The returned channel will be deleted when its file descriptor is closed.
246 */
247static
c0e31d2e 248long lttng_session_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
ad1c05e1 249{
c0e31d2e
MD
250 struct ltt_session *session = file->private_data;
251
ad1c05e1
MD
252 switch (cmd) {
253 case LTTNG_CHANNEL:
5dbbdb43
MD
254 return lttng_abi_create_channel(file,
255 (struct lttng_channel __user *)arg,
256 PER_CPU_CHANNEL);
c0e31d2e
MD
257 case LTTNG_SESSION_START:
258 return ltt_session_start(session);
259 case LTTNG_SESSION_STOP:
260 return ltt_session_stop(session);
5dbbdb43
MD
261 case LTTNG_METADATA:
262 return lttng_abi_create_channel(file,
263 (struct lttng_channel __user *)arg,
264 METADATA_CHANNEL);
ad1c05e1
MD
265 default:
266 return -ENOIOCTLCMD;
267 }
268}
269
03037b98
MD
270/*
271 * Called when the last file reference is dropped.
272 *
273 * Big fat note: channels and events are invariant for the whole session after
274 * their creation. So this session destruction also destroys all channel and
275 * event structures specific to this session (they are not destroyed when their
276 * individual file is released).
277 */
ad1c05e1 278static
03037b98 279int lttng_session_release(struct inode *inode, struct file *file)
ad1c05e1 280{
03037b98 281 struct ltt_session *session = file->private_data;
c269fff4
MD
282
283 if (session)
284 ltt_session_destroy(session);
11b5a3c2 285 return 0;
ad1c05e1 286}
ad1c05e1
MD
287
288static const struct file_operations lttng_session_fops = {
03037b98 289 .release = lttng_session_release,
ad1c05e1
MD
290 .unlocked_ioctl = lttng_session_ioctl,
291#ifdef CONFIG_COMPAT
03037b98 292 .compat_ioctl = lttng_session_ioctl,
ad1c05e1 293#endif
11b5a3c2 294};
ad1c05e1
MD
295
296static
c0e31d2e 297int lttng_abi_open_stream(struct file *channel_file)
ad1c05e1 298{
c0e31d2e 299 struct ltt_channel *channel = channel_file->private_data;
ad1c05e1
MD
300 struct lib_ring_buffer *buf;
301 int stream_fd, ret;
11b5a3c2 302 struct file *stream_file;
ad1c05e1 303
11b5a3c2 304 buf = channel->ops->buffer_read_open(channel->chan);
ad1c05e1
MD
305 if (!buf)
306 return -ENOENT;
307
1c25284c 308 stream_fd = get_unused_fd();
ad1c05e1
MD
309 if (stream_fd < 0) {
310 ret = stream_fd;
311 goto fd_error;
312 }
c0e31d2e 313 stream_file = anon_inode_getfile("[lttng_stream]",
7f57c73c 314 &lib_ring_buffer_file_operations,
ad1c05e1 315 buf, O_RDWR);
c0e31d2e
MD
316 if (IS_ERR(stream_file)) {
317 ret = PTR_ERR(stream_file);
ad1c05e1
MD
318 goto file_error;
319 }
409453cb
MD
320 /*
321 * OPEN_FMODE, called within anon_inode_getfile/alloc_file, don't honor
322 * FMODE_LSEEK, FMODE_PREAD nor FMODE_PWRITE. We need to read from this
323 * file descriptor, so we set FMODE_PREAD here.
324 */
d7b6f197 325 stream_file->f_mode |= FMODE_PREAD;
c0e31d2e 326 fd_install(stream_fd, stream_file);
dda6a249
MD
327 /*
328 * The stream holds a reference to the channel within the generic ring
329 * buffer library, so no need to hold a refcount on the channel and
330 * session files here.
331 */
ad1c05e1
MD
332 return stream_fd;
333
334file_error:
335 put_unused_fd(stream_fd);
336fd_error:
11b5a3c2 337 channel->ops->buffer_read_close(buf);
ad1c05e1
MD
338 return ret;
339}
340
653fe716 341static
c0e31d2e 342int lttng_abi_create_event(struct file *channel_file,
653fe716
MD
343 struct lttng_event __user *uevent_param)
344{
c0e31d2e 345 struct ltt_channel *channel = channel_file->private_data;
653fe716
MD
346 struct ltt_event *event;
347 char *event_name;
348 struct lttng_event event_param;
349 int event_fd, ret;
11b5a3c2 350 struct file *event_file;
dda6a249 351 void *probe;
653fe716
MD
352
353 if (copy_from_user(&event_param, uevent_param, sizeof(event_param)))
354 return -EFAULT;
355 event_name = kmalloc(PATH_MAX, GFP_KERNEL);
356 if (!event_name)
357 return -ENOMEM;
981616e8 358 if (strncpy_from_user(event_name, uevent_param->name, PATH_MAX) < 0) {
653fe716
MD
359 ret = -EFAULT;
360 goto name_error;
361 }
362 event_name[PATH_MAX - 1] = '\0';
dda6a249
MD
363
364 probe = ltt_probe_get(event_name);
365 if (!probe) {
366 ret = -ENOENT;
367 goto probe_error;
368 }
1c25284c 369 event_fd = get_unused_fd();
653fe716
MD
370 if (event_fd < 0) {
371 ret = event_fd;
372 goto fd_error;
373 }
c0e31d2e 374 event_file = anon_inode_getfile("[lttng_event]",
3b923e5b 375 &lttng_event_fops,
03037b98 376 NULL, O_RDWR);
c0e31d2e
MD
377 if (IS_ERR(event_file)) {
378 ret = PTR_ERR(event_file);
653fe716
MD
379 goto file_error;
380 }
03037b98
MD
381 /*
382 * We tolerate no failure path after event creation. It will stay
383 * invariant for the rest of the session.
384 */
11b5a3c2 385 event = ltt_event_create(channel, event_name, event_param.itype,
dda6a249 386 probe, NULL);
03037b98
MD
387 if (!event) {
388 goto event_error;
389 ret = -EEXIST;
390 }
c0e31d2e
MD
391 event_file->private_data = event;
392 fd_install(event_fd, event_file);
653fe716 393 /* The event holds a reference on the channel */
b0caa15a 394 atomic_long_inc(&channel_file->f_count);
03037b98 395 kfree(event_name);
653fe716
MD
396 return event_fd;
397
03037b98 398event_error:
c0e31d2e 399 fput(event_file);
653fe716
MD
400file_error:
401 put_unused_fd(event_fd);
402fd_error:
dda6a249
MD
403 ltt_probe_put(probe);
404probe_error:
653fe716
MD
405name_error:
406 kfree(event_name);
407 return ret;
408}
ad1c05e1
MD
409
410/**
411 * lttng_channel_ioctl - lttng syscall through ioctl
412 *
c0e31d2e 413 * @file: the file
ad1c05e1
MD
414 * @cmd: the command
415 * @arg: command arg
416 *
417 * This ioctl implements lttng commands:
418 * LTTNG_STREAM
419 * Returns an event stream file descriptor or failure.
420 * (typically, one event stream records events from one CPU)
baf20995 421 * LTTNG_EVENT
ad1c05e1 422 * Returns an event file descriptor or failure.
baf20995 423 *
baf20995
MD
424 * Channel and event file descriptors also hold a reference on the session.
425 */
ad1c05e1 426static
c0e31d2e 427long lttng_channel_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
baf20995 428{
baf20995 429 switch (cmd) {
ad1c05e1 430 case LTTNG_STREAM:
c0e31d2e 431 return lttng_abi_open_stream(file);
baf20995 432 case LTTNG_EVENT:
c0e31d2e 433 return lttng_abi_create_event(file, (struct lttng_event __user *)arg);
baf20995
MD
434 default:
435 return -ENOIOCTLCMD;
436 }
437}
438
5dbbdb43
MD
439/**
440 * lttng_metadata_ioctl - lttng syscall through ioctl
441 *
442 * @file: the file
443 * @cmd: the command
444 * @arg: command arg
445 *
446 * This ioctl implements lttng commands:
447 * LTTNG_STREAM
448 * Returns an event stream file descriptor or failure.
449 *
450 * Channel and event file descriptors also hold a reference on the session.
451 */
452static
453long lttng_metadata_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
454{
455 switch (cmd) {
456 case LTTNG_STREAM:
457 return lttng_abi_open_stream(file);
458 default:
459 return -ENOIOCTLCMD;
460 }
461}
462
3b923e5b
MD
463/* TODO: poll */
464#if 0
653fe716
MD
465/**
466 * lttng_channel_poll - lttng stream addition/removal monitoring
467 *
c0e31d2e 468 * @file: the file
653fe716
MD
469 * @wait: poll table
470 */
c0e31d2e 471unsigned int lttng_channel_poll(struct file *file, poll_table *wait)
653fe716 472{
c0e31d2e 473 struct ltt_channel *channel = file->private_data;
653fe716
MD
474 unsigned int mask = 0;
475
c0e31d2e 476 if (file->f_mode & FMODE_READ) {
653fe716 477 poll_wait_set_exclusive(wait);
c0e31d2e 478 poll_wait(file, &channel->notify_wait, wait);
653fe716
MD
479
480 /* TODO: identify when the channel is being finalized. */
481 if (finalized)
482 return POLLHUP;
483 else
484 return POLLIN | POLLRDNORM;
485 }
486 return mask;
487
488}
3b923e5b 489#endif //0
653fe716 490
0a84a57f
MD
491static
492int lttng_channel_release(struct inode *inode, struct file *file)
493{
494 struct ltt_channel *channel = file->private_data;
c269fff4
MD
495
496 if (channel)
497 fput(channel->session->file);
0a84a57f
MD
498 return 0;
499}
500
ad1c05e1 501static const struct file_operations lttng_channel_fops = {
03037b98 502 .release = lttng_channel_release,
3b923e5b
MD
503/* TODO */
504#if 0
653fe716 505 .poll = lttng_channel_poll,
3b923e5b 506#endif //0
ad1c05e1 507 .unlocked_ioctl = lttng_channel_ioctl,
baf20995 508#ifdef CONFIG_COMPAT
03037b98 509 .compat_ioctl = lttng_channel_ioctl,
baf20995 510#endif
11b5a3c2 511};
baf20995 512
5dbbdb43
MD
513static const struct file_operations lttng_metadata_fops = {
514 .release = lttng_channel_release,
515 .unlocked_ioctl = lttng_metadata_ioctl,
516#ifdef CONFIG_COMPAT
517 .compat_ioctl = lttng_metadata_ioctl,
518#endif
519};
520
0a84a57f
MD
521static
522int lttng_event_release(struct inode *inode, struct file *file)
523{
524 struct ltt_event *event = file->private_data;
c269fff4
MD
525
526 if (event)
527 fput(event->chan->file);
0a84a57f
MD
528 return 0;
529}
530
3b923e5b 531/* TODO: filter control ioctl */
0a84a57f
MD
532static const struct file_operations lttng_event_fops = {
533 .release = lttng_event_release,
11b5a3c2 534};
0a84a57f 535
1c25284c 536int __init ltt_debugfs_abi_init(void)
baf20995
MD
537{
538 int ret = 0;
539
11b5a3c2 540 lttng_dentry = debugfs_create_file("lttng", S_IWUSR, NULL, NULL,
f3d01b96 541 &lttng_fops);
11b5a3c2 542 if (IS_ERR(lttng_dentry) || !lttng_dentry) {
baf20995
MD
543 printk(KERN_ERR "Error creating LTTng control file\n");
544 ret = -ENOMEM;
545 goto error;
546 }
547error:
548 return ret;
549}
550
1c25284c 551void __exit ltt_debugfs_abi_exit(void)
baf20995 552{
11b5a3c2 553 debugfs_remove(lttng_dentry);
baf20995 554}
This page took 0.049239 seconds and 4 git commands to generate.