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