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