Add comm context
[lttng-modules.git] / ltt-debugfs-abi.c
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.
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.
23 */
24
25 #include <linux/module.h>
26 #include <linux/debugfs.h>
27 #include <linux/anon_inodes.h>
28 #include <linux/file.h>
29 #include <linux/uaccess.h>
30 #include <linux/slab.h>
31 #include "wrapper/vmalloc.h" /* for wrapper_vmalloc_sync_all() */
32 #include "wrapper/ringbuffer/vfs.h"
33 #include "ltt-debugfs-abi.h"
34 #include "ltt-events.h"
35 #include "ltt-tracer.h"
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
42 static struct dentry *lttng_dentry;
43 static const struct file_operations lttng_fops;
44 static const struct file_operations lttng_session_fops;
45 static const struct file_operations lttng_channel_fops;
46 static const struct file_operations lttng_metadata_fops;
47 static const struct file_operations lttng_event_fops;
48
49 enum channel_type {
50 PER_CPU_CHANNEL,
51 METADATA_CHANNEL,
52 };
53
54 static
55 int lttng_abi_create_session(void)
56 {
57 struct ltt_session *session;
58 struct file *session_file;
59 int session_fd, ret;
60
61 session = ltt_session_create();
62 if (!session)
63 return -ENOMEM;
64 session_fd = get_unused_fd();
65 if (session_fd < 0) {
66 ret = session_fd;
67 goto fd_error;
68 }
69 session_file = anon_inode_getfile("[lttng_session]",
70 &lttng_session_fops,
71 session, O_RDWR);
72 if (IS_ERR(session_file)) {
73 ret = PTR_ERR(session_file);
74 goto file_error;
75 }
76 session->file = session_file;
77 fd_install(session_fd, session_file);
78 return session_fd;
79
80 file_error:
81 put_unused_fd(session_fd);
82 fd_error:
83 ltt_session_destroy(session);
84 return ret;
85 }
86
87 static
88 int 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
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 }
106 ret = lttng_tracepoint_list_fops.open(NULL, tracepoint_list_file);
107 if (ret < 0)
108 goto open_error;
109 fd_install(file_fd, tracepoint_list_file);
110 if (file_fd < 0) {
111 ret = file_fd;
112 goto fd_error;
113 }
114 return file_fd;
115
116 open_error:
117 fput(tracepoint_list_file);
118 file_error:
119 put_unused_fd(file_fd);
120 fd_error:
121 return ret;
122 }
123
124 static
125 long 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
139 static
140 long lttng_abi_add_context(struct file *file,
141 struct lttng_kernel_context __user *ucontext_param,
142 struct lttng_ctx **ctx, struct ltt_session *session)
143 {
144 struct lttng_kernel_context context_param;
145
146 if (session->been_active)
147 return -EPERM;
148
149 if (copy_from_user(&context_param, ucontext_param, sizeof(context_param)))
150 return -EFAULT;
151
152 switch (context_param.ctx) {
153 case LTTNG_KERNEL_CONTEXT_PID:
154 return lttng_add_pid_to_ctx(ctx);
155 case LTTNG_KERNEL_CONTEXT_PERF_COUNTER:
156 return -ENOSYS;
157 case LTTNG_KERNEL_CONTEXT_COMM:
158 return lttng_add_comm_to_ctx(ctx);
159 default:
160 return -EINVAL;
161 }
162 }
163
164 /**
165 * lttng_ioctl - lttng syscall through ioctl
166 *
167 * @file: the file
168 * @cmd: the command
169 * @arg: command arg
170 *
171 * This ioctl implements lttng commands:
172 * LTTNG_KERNEL_SESSION
173 * Returns a LTTng trace session file descriptor
174 * LTTNG_KERNEL_TRACER_VERSION
175 * Returns the LTTng kernel tracer version
176 * LTTNG_KERNEL_TRACEPOINT_LIST
177 * Returns a file descriptor listing available tracepoints
178 *
179 * The returned session will be deleted when its file descriptor is closed.
180 */
181 static
182 long lttng_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
183 {
184 switch (cmd) {
185 case LTTNG_KERNEL_SESSION:
186 return lttng_abi_create_session();
187 case LTTNG_KERNEL_TRACER_VERSION:
188 return lttng_abi_tracer_version(file,
189 (struct lttng_kernel_tracer_version __user *) arg);
190 case LTTNG_KERNEL_TRACEPOINT_LIST:
191 return lttng_abi_tracepoint_list();
192 default:
193 return -ENOIOCTLCMD;
194 }
195 }
196
197 static const struct file_operations lttng_fops = {
198 .unlocked_ioctl = lttng_ioctl,
199 #ifdef CONFIG_COMPAT
200 .compat_ioctl = lttng_ioctl,
201 #endif
202 };
203
204 /*
205 * We tolerate no failure in this function (if one happens, we print a dmesg
206 * error, but cannot return any error, because the channel information is
207 * invariant.
208 */
209 static
210 void lttng_metadata_create_events(struct file *channel_file)
211 {
212 struct ltt_channel *channel = channel_file->private_data;
213 static struct lttng_kernel_event metadata_params = {
214 .instrumentation = LTTNG_KERNEL_TRACEPOINT,
215 .name = "lttng_metadata",
216 };
217 struct ltt_event *event;
218 int ret;
219
220 /*
221 * We tolerate no failure path after event creation. It will stay
222 * invariant for the rest of the session.
223 */
224 event = ltt_event_create(channel, &metadata_params, NULL);
225 if (!event) {
226 ret = -EINVAL;
227 goto create_error;
228 }
229 return;
230
231 create_error:
232 WARN_ON(1);
233 return; /* not allowed to return error */
234 }
235
236 static
237 int lttng_abi_create_channel(struct file *session_file,
238 struct lttng_kernel_channel __user *uchan_param,
239 enum channel_type channel_type)
240 {
241 struct ltt_session *session = session_file->private_data;
242 const struct file_operations *fops;
243 const char *transport_name;
244 struct ltt_channel *chan;
245 struct file *chan_file;
246 struct lttng_kernel_channel chan_param;
247 int chan_fd;
248 int ret = 0;
249
250 if (copy_from_user(&chan_param, uchan_param, sizeof(chan_param)))
251 return -EFAULT;
252 chan_fd = get_unused_fd();
253 if (chan_fd < 0) {
254 ret = chan_fd;
255 goto fd_error;
256 }
257 chan_file = anon_inode_getfile("[lttng_channel]",
258 &lttng_channel_fops,
259 NULL, O_RDWR);
260 if (IS_ERR(chan_file)) {
261 ret = PTR_ERR(chan_file);
262 goto file_error;
263 }
264 switch (channel_type) {
265 case PER_CPU_CHANNEL:
266 transport_name = chan_param.overwrite ?
267 "relay-overwrite" : "relay-discard";
268 fops = &lttng_channel_fops;
269 break;
270 case METADATA_CHANNEL:
271 transport_name = "relay-metadata";
272 fops = &lttng_metadata_fops;
273 break;
274 default:
275 transport_name = "<unknown>";
276 break;
277 }
278 /*
279 * We tolerate no failure path after channel creation. It will stay
280 * invariant for the rest of the session.
281 */
282 chan = ltt_channel_create(session, transport_name, NULL,
283 chan_param.subbuf_size,
284 chan_param.num_subbuf,
285 chan_param.switch_timer_interval,
286 chan_param.read_timer_interval);
287 if (!chan) {
288 ret = -EINVAL;
289 goto chan_error;
290 }
291 chan->file = chan_file;
292 chan_file->private_data = chan;
293 fd_install(chan_fd, chan_file);
294 if (channel_type == METADATA_CHANNEL) {
295 session->metadata = chan;
296 lttng_metadata_create_events(chan_file);
297 }
298
299 /* The channel created holds a reference on the session */
300 atomic_long_inc(&session_file->f_count);
301
302 return chan_fd;
303
304 chan_error:
305 fput(chan_file);
306 file_error:
307 put_unused_fd(chan_fd);
308 fd_error:
309 return ret;
310 }
311
312 /**
313 * lttng_session_ioctl - lttng session fd ioctl
314 *
315 * @file: the file
316 * @cmd: the command
317 * @arg: command arg
318 *
319 * This ioctl implements lttng commands:
320 * LTTNG_KERNEL_CHANNEL
321 * Returns a LTTng channel file descriptor
322 * LTTNG_KERNEL_SESSION_START
323 * Starts tracing session
324 * LTTNG_KERNEL_SESSION_STOP
325 * Stops tracing session
326 * LTTNG_KERNEL_METADATA
327 * Returns a LTTng metadata file descriptor
328 *
329 * The returned channel will be deleted when its file descriptor is closed.
330 */
331 static
332 long lttng_session_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
333 {
334 struct ltt_session *session = file->private_data;
335
336 switch (cmd) {
337 case LTTNG_KERNEL_CHANNEL:
338 return lttng_abi_create_channel(file,
339 (struct lttng_kernel_channel __user *) arg,
340 PER_CPU_CHANNEL);
341 case LTTNG_KERNEL_SESSION_START:
342 return ltt_session_start(session);
343 case LTTNG_KERNEL_SESSION_STOP:
344 return ltt_session_stop(session);
345 case LTTNG_KERNEL_METADATA:
346 return lttng_abi_create_channel(file,
347 (struct lttng_kernel_channel __user *) arg,
348 METADATA_CHANNEL);
349 default:
350 return -ENOIOCTLCMD;
351 }
352 }
353
354 /*
355 * Called when the last file reference is dropped.
356 *
357 * Big fat note: channels and events are invariant for the whole session after
358 * their creation. So this session destruction also destroys all channel and
359 * event structures specific to this session (they are not destroyed when their
360 * individual file is released).
361 */
362 static
363 int lttng_session_release(struct inode *inode, struct file *file)
364 {
365 struct ltt_session *session = file->private_data;
366
367 if (session)
368 ltt_session_destroy(session);
369 return 0;
370 }
371
372 static const struct file_operations lttng_session_fops = {
373 .release = lttng_session_release,
374 .unlocked_ioctl = lttng_session_ioctl,
375 #ifdef CONFIG_COMPAT
376 .compat_ioctl = lttng_session_ioctl,
377 #endif
378 };
379
380 static
381 int lttng_abi_open_stream(struct file *channel_file)
382 {
383 struct ltt_channel *channel = channel_file->private_data;
384 struct lib_ring_buffer *buf;
385 int stream_fd, ret;
386 struct file *stream_file;
387
388 buf = channel->ops->buffer_read_open(channel->chan);
389 if (!buf)
390 return -ENOENT;
391
392 stream_fd = get_unused_fd();
393 if (stream_fd < 0) {
394 ret = stream_fd;
395 goto fd_error;
396 }
397 stream_file = anon_inode_getfile("[lttng_stream]",
398 &lib_ring_buffer_file_operations,
399 buf, O_RDWR);
400 if (IS_ERR(stream_file)) {
401 ret = PTR_ERR(stream_file);
402 goto file_error;
403 }
404 /*
405 * OPEN_FMODE, called within anon_inode_getfile/alloc_file, don't honor
406 * FMODE_LSEEK, FMODE_PREAD nor FMODE_PWRITE. We need to read from this
407 * file descriptor, so we set FMODE_PREAD here.
408 */
409 stream_file->f_mode |= FMODE_PREAD;
410 fd_install(stream_fd, stream_file);
411 /*
412 * The stream holds a reference to the channel within the generic ring
413 * buffer library, so no need to hold a refcount on the channel and
414 * session files here.
415 */
416 return stream_fd;
417
418 file_error:
419 put_unused_fd(stream_fd);
420 fd_error:
421 channel->ops->buffer_read_close(buf);
422 return ret;
423 }
424
425 static
426 int lttng_abi_create_event(struct file *channel_file,
427 struct lttng_kernel_event __user *uevent_param)
428 {
429 struct ltt_channel *channel = channel_file->private_data;
430 struct ltt_event *event;
431 struct lttng_kernel_event event_param;
432 int event_fd, ret;
433 struct file *event_file;
434
435 if (copy_from_user(&event_param, uevent_param, sizeof(event_param)))
436 return -EFAULT;
437 event_param.name[LTTNG_SYM_NAME_LEN - 1] = '\0';
438 switch (event_param.instrumentation) {
439 case LTTNG_KERNEL_KPROBE:
440 event_param.u.kprobe.symbol_name[LTTNG_SYM_NAME_LEN - 1] = '\0';
441 break;
442 case LTTNG_KERNEL_FUNCTION:
443 event_param.u.ftrace.symbol_name[LTTNG_SYM_NAME_LEN - 1] = '\0';
444 break;
445 default:
446 break;
447 }
448 event_fd = get_unused_fd();
449 if (event_fd < 0) {
450 ret = event_fd;
451 goto fd_error;
452 }
453 event_file = anon_inode_getfile("[lttng_event]",
454 &lttng_event_fops,
455 NULL, O_RDWR);
456 if (IS_ERR(event_file)) {
457 ret = PTR_ERR(event_file);
458 goto file_error;
459 }
460 /*
461 * We tolerate no failure path after event creation. It will stay
462 * invariant for the rest of the session.
463 */
464 event = ltt_event_create(channel, &event_param, NULL);
465 if (!event) {
466 ret = -EINVAL;
467 goto event_error;
468 }
469 event_file->private_data = event;
470 fd_install(event_fd, event_file);
471 /* The event holds a reference on the channel */
472 atomic_long_inc(&channel_file->f_count);
473 return event_fd;
474
475 event_error:
476 fput(event_file);
477 file_error:
478 put_unused_fd(event_fd);
479 fd_error:
480 return ret;
481 }
482
483 /**
484 * lttng_channel_ioctl - lttng syscall through ioctl
485 *
486 * @file: the file
487 * @cmd: the command
488 * @arg: command arg
489 *
490 * This ioctl implements lttng commands:
491 * LTTNG_KERNEL_STREAM
492 * Returns an event stream file descriptor or failure.
493 * (typically, one event stream records events from one CPU)
494 * LTTNG_KERNEL_EVENT
495 * Returns an event file descriptor or failure.
496 * LTTNG_KERNEL_CONTEXT
497 * Prepend a context field to each event in the channel
498 *
499 * Channel and event file descriptors also hold a reference on the session.
500 */
501 static
502 long lttng_channel_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
503 {
504 struct ltt_channel *channel = file->private_data;
505
506 switch (cmd) {
507 case LTTNG_KERNEL_STREAM:
508 return lttng_abi_open_stream(file);
509 case LTTNG_KERNEL_EVENT:
510 return lttng_abi_create_event(file, (struct lttng_kernel_event __user *) arg);
511 case LTTNG_KERNEL_CONTEXT:
512 return lttng_abi_add_context(file,
513 (struct lttng_kernel_context __user *) arg,
514 &channel->ctx, channel->session);
515 default:
516 return -ENOIOCTLCMD;
517 }
518 }
519
520 /**
521 * lttng_metadata_ioctl - lttng syscall through ioctl
522 *
523 * @file: the file
524 * @cmd: the command
525 * @arg: command arg
526 *
527 * This ioctl implements lttng commands:
528 * LTTNG_KERNEL_STREAM
529 * Returns an event stream file descriptor or failure.
530 *
531 * Channel and event file descriptors also hold a reference on the session.
532 */
533 static
534 long lttng_metadata_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
535 {
536 switch (cmd) {
537 case LTTNG_KERNEL_STREAM:
538 return lttng_abi_open_stream(file);
539 default:
540 return -ENOIOCTLCMD;
541 }
542 }
543
544 /* TODO: poll */
545 #if 0
546 /**
547 * lttng_channel_poll - lttng stream addition/removal monitoring
548 *
549 * @file: the file
550 * @wait: poll table
551 */
552 unsigned int lttng_channel_poll(struct file *file, poll_table *wait)
553 {
554 struct ltt_channel *channel = file->private_data;
555 unsigned int mask = 0;
556
557 if (file->f_mode & FMODE_READ) {
558 poll_wait_set_exclusive(wait);
559 poll_wait(file, &channel->notify_wait, wait);
560
561 /* TODO: identify when the channel is being finalized. */
562 if (finalized)
563 return POLLHUP;
564 else
565 return POLLIN | POLLRDNORM;
566 }
567 return mask;
568
569 }
570 #endif //0
571
572 static
573 int lttng_channel_release(struct inode *inode, struct file *file)
574 {
575 struct ltt_channel *channel = file->private_data;
576
577 if (channel)
578 fput(channel->session->file);
579 return 0;
580 }
581
582 static const struct file_operations lttng_channel_fops = {
583 .release = lttng_channel_release,
584 /* TODO */
585 #if 0
586 .poll = lttng_channel_poll,
587 #endif //0
588 .unlocked_ioctl = lttng_channel_ioctl,
589 #ifdef CONFIG_COMPAT
590 .compat_ioctl = lttng_channel_ioctl,
591 #endif
592 };
593
594 static const struct file_operations lttng_metadata_fops = {
595 .release = lttng_channel_release,
596 .unlocked_ioctl = lttng_metadata_ioctl,
597 #ifdef CONFIG_COMPAT
598 .compat_ioctl = lttng_metadata_ioctl,
599 #endif
600 };
601
602 /**
603 * lttng_event_ioctl - lttng syscall through ioctl
604 *
605 * @file: the file
606 * @cmd: the command
607 * @arg: command arg
608 *
609 * This ioctl implements lttng commands:
610 * LTTNG_KERNEL_STREAM
611 * Returns an event stream file descriptor or failure.
612 * (typically, one event stream records events from one CPU)
613 * LTTNG_KERNEL_EVENT
614 * Returns an event file descriptor or failure.
615 * LTTNG_KERNEL_CONTEXT
616 * Prepend a context field to each record of this event
617 */
618 static
619 long lttng_event_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
620 {
621 struct ltt_event *event = file->private_data;
622
623 switch (cmd) {
624 case LTTNG_KERNEL_CONTEXT:
625 return lttng_abi_add_context(file,
626 (struct lttng_kernel_context __user *) arg,
627 &event->ctx, event->chan->session);
628 default:
629 return -ENOIOCTLCMD;
630 }
631 }
632
633 static
634 int lttng_event_release(struct inode *inode, struct file *file)
635 {
636 struct ltt_event *event = file->private_data;
637
638 if (event)
639 fput(event->chan->file);
640 return 0;
641 }
642
643 /* TODO: filter control ioctl */
644 static const struct file_operations lttng_event_fops = {
645 .release = lttng_event_release,
646 .unlocked_ioctl = lttng_event_ioctl,
647 #ifdef CONFIG_COMPAT
648 .compat_ioctl = lttng_event_ioctl,
649 #endif
650 };
651
652 int __init ltt_debugfs_abi_init(void)
653 {
654 int ret = 0;
655
656 wrapper_vmalloc_sync_all();
657 lttng_dentry = debugfs_create_file("lttng", S_IWUSR, NULL, NULL,
658 &lttng_fops);
659 if (IS_ERR(lttng_dentry) || !lttng_dentry) {
660 printk(KERN_ERR "Error creating LTTng control file\n");
661 ret = -ENOMEM;
662 goto error;
663 }
664 error:
665 return ret;
666 }
667
668 void __exit ltt_debugfs_abi_exit(void)
669 {
670 debugfs_remove(lttng_dentry);
671 }
This page took 0.043095 seconds and 4 git commands to generate.