Metadata cache and multiple metadata channels
[lttng-modules.git] / lttng-abi.c
1 /*
2 * lttng-abi.c
3 *
4 * LTTng ABI
5 *
6 * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; only
11 * version 2.1 of the License.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 *
22 *
23 * Mimic system calls for:
24 * - session creation, returns a file descriptor or failure.
25 * - channel creation, returns a file descriptor or failure.
26 * - Operates on a session file descriptor
27 * - Takes all channel options as parameters.
28 * - stream get, returns a file descriptor or failure.
29 * - Operates on a channel file descriptor.
30 * - stream notifier get, returns a file descriptor or failure.
31 * - Operates on a channel file descriptor.
32 * - event creation, returns a file descriptor or failure.
33 * - Operates on a channel file descriptor
34 * - Takes an event name as parameter
35 * - Takes an instrumentation source as parameter
36 * - e.g. tracepoints, dynamic_probes...
37 * - Takes instrumentation source specific arguments.
38 */
39
40 #include <linux/module.h>
41 #include <linux/proc_fs.h>
42 #include <linux/anon_inodes.h>
43 #include <linux/file.h>
44 #include <linux/uaccess.h>
45 #include <linux/slab.h>
46 #include "wrapper/vmalloc.h" /* for wrapper_vmalloc_sync_all() */
47 #include "wrapper/ringbuffer/vfs.h"
48 #include "wrapper/ringbuffer/backend.h"
49 #include "wrapper/ringbuffer/frontend.h"
50 #include "wrapper/poll.h"
51 #include "lttng-abi.h"
52 #include "lttng-abi-old.h"
53 #include "lttng-events.h"
54 #include "lttng-tracer.h"
55
56 /*
57 * This is LTTng's own personal way to create a system call as an external
58 * module. We use ioctl() on /proc/lttng.
59 */
60
61 static struct proc_dir_entry *lttng_proc_dentry;
62 static const struct file_operations lttng_fops;
63 static const struct file_operations lttng_session_fops;
64 static const struct file_operations lttng_channel_fops;
65 static const struct file_operations lttng_metadata_fops;
66 static const struct file_operations lttng_event_fops;
67
68 /*
69 * Teardown management: opened file descriptors keep a refcount on the module,
70 * so it can only exit when all file descriptors are closed.
71 */
72
73 static
74 int lttng_abi_create_session(void)
75 {
76 struct lttng_session *session;
77 struct file *session_file;
78 int session_fd, ret;
79
80 session = lttng_session_create();
81 if (!session)
82 return -ENOMEM;
83 session_fd = get_unused_fd();
84 if (session_fd < 0) {
85 ret = session_fd;
86 goto fd_error;
87 }
88 session_file = anon_inode_getfile("[lttng_session]",
89 &lttng_session_fops,
90 session, O_RDWR);
91 if (IS_ERR(session_file)) {
92 ret = PTR_ERR(session_file);
93 goto file_error;
94 }
95 session->file = session_file;
96 fd_install(session_fd, session_file);
97 return session_fd;
98
99 file_error:
100 put_unused_fd(session_fd);
101 fd_error:
102 lttng_session_destroy(session);
103 return ret;
104 }
105
106 static
107 int lttng_abi_tracepoint_list(void)
108 {
109 struct file *tracepoint_list_file;
110 int file_fd, ret;
111
112 file_fd = get_unused_fd();
113 if (file_fd < 0) {
114 ret = file_fd;
115 goto fd_error;
116 }
117
118 tracepoint_list_file = anon_inode_getfile("[lttng_session]",
119 &lttng_tracepoint_list_fops,
120 NULL, O_RDWR);
121 if (IS_ERR(tracepoint_list_file)) {
122 ret = PTR_ERR(tracepoint_list_file);
123 goto file_error;
124 }
125 ret = lttng_tracepoint_list_fops.open(NULL, tracepoint_list_file);
126 if (ret < 0)
127 goto open_error;
128 fd_install(file_fd, tracepoint_list_file);
129 if (file_fd < 0) {
130 ret = file_fd;
131 goto fd_error;
132 }
133 return file_fd;
134
135 open_error:
136 fput(tracepoint_list_file);
137 file_error:
138 put_unused_fd(file_fd);
139 fd_error:
140 return ret;
141 }
142
143 static
144 void lttng_abi_tracer_version(struct lttng_kernel_tracer_version *v)
145 {
146 v->major = LTTNG_MODULES_MAJOR_VERSION;
147 v->minor = LTTNG_MODULES_MINOR_VERSION;
148 v->patchlevel = LTTNG_MODULES_PATCHLEVEL_VERSION;
149 }
150
151 static
152 long lttng_abi_add_context(struct file *file,
153 struct lttng_kernel_context *context_param,
154 struct lttng_ctx **ctx, struct lttng_session *session)
155 {
156
157 if (session->been_active)
158 return -EPERM;
159
160 switch (context_param->ctx) {
161 case LTTNG_KERNEL_CONTEXT_PID:
162 return lttng_add_pid_to_ctx(ctx);
163 case LTTNG_KERNEL_CONTEXT_PRIO:
164 return lttng_add_prio_to_ctx(ctx);
165 case LTTNG_KERNEL_CONTEXT_NICE:
166 return lttng_add_nice_to_ctx(ctx);
167 case LTTNG_KERNEL_CONTEXT_VPID:
168 return lttng_add_vpid_to_ctx(ctx);
169 case LTTNG_KERNEL_CONTEXT_TID:
170 return lttng_add_tid_to_ctx(ctx);
171 case LTTNG_KERNEL_CONTEXT_VTID:
172 return lttng_add_vtid_to_ctx(ctx);
173 case LTTNG_KERNEL_CONTEXT_PPID:
174 return lttng_add_ppid_to_ctx(ctx);
175 case LTTNG_KERNEL_CONTEXT_VPPID:
176 return lttng_add_vppid_to_ctx(ctx);
177 case LTTNG_KERNEL_CONTEXT_PERF_COUNTER:
178 context_param->u.perf_counter.name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
179 return lttng_add_perf_counter_to_ctx(context_param->u.perf_counter.type,
180 context_param->u.perf_counter.config,
181 context_param->u.perf_counter.name,
182 ctx);
183 case LTTNG_KERNEL_CONTEXT_PROCNAME:
184 return lttng_add_procname_to_ctx(ctx);
185 case LTTNG_KERNEL_CONTEXT_HOSTNAME:
186 return lttng_add_hostname_to_ctx(ctx);
187 default:
188 return -EINVAL;
189 }
190 }
191
192 /**
193 * lttng_ioctl - lttng syscall through ioctl
194 *
195 * @file: the file
196 * @cmd: the command
197 * @arg: command arg
198 *
199 * This ioctl implements lttng commands:
200 * LTTNG_KERNEL_SESSION
201 * Returns a LTTng trace session file descriptor
202 * LTTNG_KERNEL_TRACER_VERSION
203 * Returns the LTTng kernel tracer version
204 * LTTNG_KERNEL_TRACEPOINT_LIST
205 * Returns a file descriptor listing available tracepoints
206 * LTTNG_KERNEL_WAIT_QUIESCENT
207 * Returns after all previously running probes have completed
208 *
209 * The returned session will be deleted when its file descriptor is closed.
210 */
211 static
212 long lttng_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
213 {
214 switch (cmd) {
215 case LTTNG_KERNEL_OLD_SESSION:
216 case LTTNG_KERNEL_SESSION:
217 return lttng_abi_create_session();
218 case LTTNG_KERNEL_OLD_TRACER_VERSION:
219 {
220 struct lttng_kernel_tracer_version v;
221 struct lttng_kernel_old_tracer_version oldv;
222 struct lttng_kernel_old_tracer_version *uversion =
223 (struct lttng_kernel_old_tracer_version __user *) arg;
224
225 lttng_abi_tracer_version(&v);
226 oldv.major = v.major;
227 oldv.minor = v.minor;
228 oldv.patchlevel = v.patchlevel;
229
230 if (copy_to_user(uversion, &oldv, sizeof(oldv)))
231 return -EFAULT;
232 return 0;
233 }
234 case LTTNG_KERNEL_TRACER_VERSION:
235 {
236 struct lttng_kernel_tracer_version version;
237 struct lttng_kernel_tracer_version *uversion =
238 (struct lttng_kernel_tracer_version __user *) arg;
239
240 lttng_abi_tracer_version(&version);
241
242 if (copy_to_user(uversion, &version, sizeof(version)))
243 return -EFAULT;
244 return 0;
245 }
246 case LTTNG_KERNEL_OLD_TRACEPOINT_LIST:
247 case LTTNG_KERNEL_TRACEPOINT_LIST:
248 return lttng_abi_tracepoint_list();
249 case LTTNG_KERNEL_OLD_WAIT_QUIESCENT:
250 case LTTNG_KERNEL_WAIT_QUIESCENT:
251 synchronize_trace();
252 return 0;
253 case LTTNG_KERNEL_OLD_CALIBRATE:
254 {
255 struct lttng_kernel_old_calibrate __user *ucalibrate =
256 (struct lttng_kernel_old_calibrate __user *) arg;
257 struct lttng_kernel_old_calibrate old_calibrate;
258 struct lttng_kernel_calibrate calibrate;
259 int ret;
260
261 if (copy_from_user(&old_calibrate, ucalibrate, sizeof(old_calibrate)))
262 return -EFAULT;
263 calibrate.type = old_calibrate.type;
264 ret = lttng_calibrate(&calibrate);
265 if (copy_to_user(ucalibrate, &old_calibrate, sizeof(old_calibrate)))
266 return -EFAULT;
267 return ret;
268 }
269 case LTTNG_KERNEL_CALIBRATE:
270 {
271 struct lttng_kernel_calibrate __user *ucalibrate =
272 (struct lttng_kernel_calibrate __user *) arg;
273 struct lttng_kernel_calibrate calibrate;
274 int ret;
275
276 if (copy_from_user(&calibrate, ucalibrate, sizeof(calibrate)))
277 return -EFAULT;
278 ret = lttng_calibrate(&calibrate);
279 if (copy_to_user(ucalibrate, &calibrate, sizeof(calibrate)))
280 return -EFAULT;
281 return ret;
282 }
283 default:
284 return -ENOIOCTLCMD;
285 }
286 }
287
288 static const struct file_operations lttng_fops = {
289 .owner = THIS_MODULE,
290 .unlocked_ioctl = lttng_ioctl,
291 #ifdef CONFIG_COMPAT
292 .compat_ioctl = lttng_ioctl,
293 #endif
294 };
295
296 static
297 int lttng_abi_create_channel(struct file *session_file,
298 struct lttng_kernel_channel *chan_param,
299 enum channel_type channel_type)
300 {
301 struct lttng_session *session = session_file->private_data;
302 const struct file_operations *fops = NULL;
303 const char *transport_name;
304 struct lttng_channel *chan;
305 struct file *chan_file;
306 int chan_fd;
307 int ret = 0;
308
309 chan_fd = get_unused_fd();
310 if (chan_fd < 0) {
311 ret = chan_fd;
312 goto fd_error;
313 }
314 switch (channel_type) {
315 case PER_CPU_CHANNEL:
316 fops = &lttng_channel_fops;
317 break;
318 case METADATA_CHANNEL:
319 fops = &lttng_metadata_fops;
320 break;
321 }
322
323 chan_file = anon_inode_getfile("[lttng_channel]",
324 fops,
325 NULL, O_RDWR);
326 if (IS_ERR(chan_file)) {
327 ret = PTR_ERR(chan_file);
328 goto file_error;
329 }
330 switch (channel_type) {
331 case PER_CPU_CHANNEL:
332 if (chan_param->output == LTTNG_KERNEL_SPLICE) {
333 transport_name = chan_param->overwrite ?
334 "relay-overwrite" : "relay-discard";
335 } else if (chan_param->output == LTTNG_KERNEL_MMAP) {
336 transport_name = chan_param->overwrite ?
337 "relay-overwrite-mmap" : "relay-discard-mmap";
338 } else {
339 return -EINVAL;
340 }
341 break;
342 case METADATA_CHANNEL:
343 if (chan_param->output == LTTNG_KERNEL_SPLICE)
344 transport_name = "relay-metadata";
345 else if (chan_param->output == LTTNG_KERNEL_MMAP)
346 transport_name = "relay-metadata-mmap";
347 else
348 return -EINVAL;
349 break;
350 default:
351 transport_name = "<unknown>";
352 break;
353 }
354 /*
355 * We tolerate no failure path after channel creation. It will stay
356 * invariant for the rest of the session.
357 */
358 chan = lttng_channel_create(session, transport_name, NULL,
359 chan_param->subbuf_size,
360 chan_param->num_subbuf,
361 chan_param->switch_timer_interval,
362 chan_param->read_timer_interval,
363 channel_type);
364 if (!chan) {
365 ret = -EINVAL;
366 goto chan_error;
367 }
368 chan->file = chan_file;
369 chan_file->private_data = chan;
370 fd_install(chan_fd, chan_file);
371 atomic_long_inc(&session_file->f_count);
372
373 return chan_fd;
374
375 chan_error:
376 fput(chan_file);
377 file_error:
378 put_unused_fd(chan_fd);
379 fd_error:
380 return ret;
381 }
382
383 /**
384 * lttng_session_ioctl - lttng session fd ioctl
385 *
386 * @file: the file
387 * @cmd: the command
388 * @arg: command arg
389 *
390 * This ioctl implements lttng commands:
391 * LTTNG_KERNEL_CHANNEL
392 * Returns a LTTng channel file descriptor
393 * LTTNG_KERNEL_ENABLE
394 * Enables tracing for a session (weak enable)
395 * LTTNG_KERNEL_DISABLE
396 * Disables tracing for a session (strong disable)
397 * LTTNG_KERNEL_METADATA
398 * Returns a LTTng metadata file descriptor
399 *
400 * The returned channel will be deleted when its file descriptor is closed.
401 */
402 static
403 long lttng_session_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
404 {
405 struct lttng_session *session = file->private_data;
406
407 switch (cmd) {
408 case LTTNG_KERNEL_OLD_CHANNEL:
409 {
410 struct lttng_kernel_channel chan_param;
411 struct lttng_kernel_old_channel old_chan_param;
412
413 if (copy_from_user(&old_chan_param,
414 (struct lttng_kernel_old_channel __user *) arg,
415 sizeof(struct lttng_kernel_old_channel)))
416 return -EFAULT;
417 chan_param.overwrite = old_chan_param.overwrite;
418 chan_param.subbuf_size = old_chan_param.subbuf_size;
419 chan_param.num_subbuf = old_chan_param.num_subbuf;
420 chan_param.switch_timer_interval = old_chan_param.switch_timer_interval;
421 chan_param.read_timer_interval = old_chan_param.read_timer_interval;
422 chan_param.output = old_chan_param.output;
423
424 return lttng_abi_create_channel(file, &chan_param,
425 PER_CPU_CHANNEL);
426 }
427 case LTTNG_KERNEL_CHANNEL:
428 {
429 struct lttng_kernel_channel chan_param;
430
431 if (copy_from_user(&chan_param,
432 (struct lttng_kernel_channel __user *) arg,
433 sizeof(struct lttng_kernel_channel)))
434 return -EFAULT;
435 return lttng_abi_create_channel(file, &chan_param,
436 PER_CPU_CHANNEL);
437 }
438 case LTTNG_KERNEL_OLD_SESSION_START:
439 case LTTNG_KERNEL_OLD_ENABLE:
440 case LTTNG_KERNEL_SESSION_START:
441 case LTTNG_KERNEL_ENABLE:
442 return lttng_session_enable(session);
443 case LTTNG_KERNEL_OLD_SESSION_STOP:
444 case LTTNG_KERNEL_OLD_DISABLE:
445 case LTTNG_KERNEL_SESSION_STOP:
446 case LTTNG_KERNEL_DISABLE:
447 return lttng_session_disable(session);
448 case LTTNG_KERNEL_OLD_METADATA:
449 {
450 struct lttng_kernel_channel chan_param;
451 struct lttng_kernel_old_channel old_chan_param;
452
453 if (copy_from_user(&old_chan_param,
454 (struct lttng_kernel_old_channel __user *) arg,
455 sizeof(struct lttng_kernel_old_channel)))
456 return -EFAULT;
457 chan_param.overwrite = old_chan_param.overwrite;
458 chan_param.subbuf_size = old_chan_param.subbuf_size;
459 chan_param.num_subbuf = old_chan_param.num_subbuf;
460 chan_param.switch_timer_interval = old_chan_param.switch_timer_interval;
461 chan_param.read_timer_interval = old_chan_param.read_timer_interval;
462 chan_param.output = old_chan_param.output;
463
464 return lttng_abi_create_channel(file, &chan_param,
465 METADATA_CHANNEL);
466 }
467 case LTTNG_KERNEL_METADATA:
468 {
469 struct lttng_kernel_channel chan_param;
470
471 if (copy_from_user(&chan_param,
472 (struct lttng_kernel_channel __user *) arg,
473 sizeof(struct lttng_kernel_channel)))
474 return -EFAULT;
475 return lttng_abi_create_channel(file, &chan_param,
476 METADATA_CHANNEL);
477 }
478 default:
479 return -ENOIOCTLCMD;
480 }
481 }
482
483 /*
484 * Called when the last file reference is dropped.
485 *
486 * Big fat note: channels and events are invariant for the whole session after
487 * their creation. So this session destruction also destroys all channel and
488 * event structures specific to this session (they are not destroyed when their
489 * individual file is released).
490 */
491 static
492 int lttng_session_release(struct inode *inode, struct file *file)
493 {
494 struct lttng_session *session = file->private_data;
495
496 if (session)
497 lttng_session_destroy(session);
498 return 0;
499 }
500
501 static const struct file_operations lttng_session_fops = {
502 .owner = THIS_MODULE,
503 .release = lttng_session_release,
504 .unlocked_ioctl = lttng_session_ioctl,
505 #ifdef CONFIG_COMPAT
506 .compat_ioctl = lttng_session_ioctl,
507 #endif
508 };
509
510 /**
511 * lttng_metadata_ring_buffer_poll - LTTng ring buffer poll file operation
512 * @filp: the file
513 * @wait: poll table
514 *
515 * Handles the poll operations for the metadata channels.
516 */
517 static
518 unsigned int lttng_metadata_ring_buffer_poll(struct file *filp,
519 poll_table *wait)
520 {
521 struct lttng_metadata_stream *stream = filp->private_data;
522 struct lib_ring_buffer *buf = stream->priv;
523 int finalized;
524 unsigned int mask = 0;
525
526 if (filp->f_mode & FMODE_READ) {
527 poll_wait_set_exclusive(wait);
528 poll_wait(filp, &stream->read_wait, wait);
529
530 finalized = stream->finalized;
531
532 /*
533 * lib_ring_buffer_is_finalized() contains a smp_rmb()
534 * ordering finalized load before offsets loads.
535 */
536 WARN_ON(atomic_long_read(&buf->active_readers) != 1);
537
538 if (finalized)
539 mask |= POLLHUP;
540
541 if (stream->metadata_cache->metadata_written >
542 stream->metadata_cache_read)
543 mask |= POLLIN;
544 }
545
546 return mask;
547 }
548
549 static
550 int lttng_metadata_ring_buffer_ioctl_get_subbuf(struct file *filp,
551 unsigned int cmd, unsigned long arg)
552 {
553 struct lttng_metadata_stream *stream = filp->private_data;
554 struct lib_ring_buffer *buf = stream->priv;
555 struct channel *chan = buf->backend.chan;
556 struct lttng_channel *lttng_chan = channel_get_private(chan);
557 int ret;
558
559 ret = lttng_metadata_output_channel(lttng_chan, stream);
560 if (ret > 0) {
561 lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE);
562 ret = 0;
563 }
564 return ret;
565 }
566
567 static
568 long lttng_metadata_ring_buffer_ioctl(struct file *filp,
569 unsigned int cmd, unsigned long arg)
570 {
571 int ret;
572 struct lttng_metadata_stream *stream = filp->private_data;
573 struct lib_ring_buffer *buf = stream->priv;
574
575 switch (cmd) {
576 case RING_BUFFER_GET_SUBBUF:
577 case RING_BUFFER_GET_NEXT_SUBBUF:
578 {
579 ret = lttng_metadata_ring_buffer_ioctl_get_subbuf(filp,
580 cmd, arg);
581 if (ret < 0)
582 goto err;
583 break;
584 }
585 default:
586 break;
587 }
588 /* Performing lib ring buffer ioctl after our own. */
589 return lib_ring_buffer_ioctl(filp, cmd, arg, buf);
590
591 err:
592 return ret;
593 }
594
595 static
596 long lttng_metadata_ring_buffer_compat_ioctl(struct file *filp,
597 unsigned int cmd, unsigned long arg)
598 {
599 int ret;
600 struct lttng_metadata_stream *stream = filp->private_data;
601 struct lib_ring_buffer *buf = stream->priv;
602
603 switch (cmd) {
604 case RING_BUFFER_GET_SUBBUF:
605 case RING_BUFFER_GET_NEXT_SUBBUF:
606 {
607 ret = lttng_metadata_ring_buffer_ioctl_get_subbuf(filp,
608 cmd, arg);
609 if (ret < 0)
610 goto err;
611 break;
612 }
613 default:
614 break;
615 }
616 /* Performing lib ring buffer ioctl after our own. */
617 return lib_ring_buffer_compat_ioctl(filp, cmd, arg, buf);
618
619 err:
620 return ret;
621 }
622
623 static
624 int lttng_metadata_ring_buffer_open(struct inode *inode, struct file *file)
625 {
626 struct lttng_metadata_stream *stream = inode->i_private;
627 struct lib_ring_buffer *buf = stream->priv;
628
629 file->private_data = buf;
630 return lib_ring_buffer_open(inode, file, buf);
631 }
632
633 static
634 int lttng_metadata_ring_buffer_release(struct inode *inode, struct file *file)
635 {
636 struct lttng_metadata_stream *stream = file->private_data;
637 struct lib_ring_buffer *buf = stream->priv;
638
639 kref_put(&stream->metadata_cache->refcount, metadata_cache_destroy);
640
641 return lib_ring_buffer_release(inode, file, buf);
642 }
643
644 static
645 ssize_t lttng_metadata_ring_buffer_splice_read(struct file *in, loff_t *ppos,
646 struct pipe_inode_info *pipe, size_t len,
647 unsigned int flags)
648 {
649 struct lttng_metadata_stream *stream = in->private_data;
650 struct lib_ring_buffer *buf = stream->priv;
651
652 return lib_ring_buffer_splice_read(in, ppos, pipe, len,
653 flags, buf);
654 }
655
656 static
657 int lttng_metadata_ring_buffer_mmap(struct file *filp,
658 struct vm_area_struct *vma)
659 {
660 struct lttng_metadata_stream *stream = filp->private_data;
661 struct lib_ring_buffer *buf = stream->priv;
662
663 return lib_ring_buffer_mmap(filp, vma, buf);
664 }
665
666 static
667 const struct file_operations lttng_metadata_ring_buffer_file_operations = {
668 .owner = THIS_MODULE,
669 .open = lttng_metadata_ring_buffer_open,
670 .release = lttng_metadata_ring_buffer_release,
671 .poll = lttng_metadata_ring_buffer_poll,
672 .splice_read = lttng_metadata_ring_buffer_splice_read,
673 .mmap = lttng_metadata_ring_buffer_mmap,
674 .unlocked_ioctl = lttng_metadata_ring_buffer_ioctl,
675 .llseek = vfs_lib_ring_buffer_no_llseek,
676 #ifdef CONFIG_COMPAT
677 .compat_ioctl = lttng_metadata_ring_buffer_compat_ioctl,
678 #endif
679 };
680
681 static
682 int lttng_abi_create_stream_fd(struct file *channel_file, void *stream_priv,
683 const struct file_operations *fops)
684 {
685 int stream_fd, ret;
686 struct file *stream_file;
687
688 stream_fd = get_unused_fd();
689 if (stream_fd < 0) {
690 ret = stream_fd;
691 goto fd_error;
692 }
693 stream_file = anon_inode_getfile("[lttng_stream]", fops,
694 stream_priv, O_RDWR);
695 if (IS_ERR(stream_file)) {
696 ret = PTR_ERR(stream_file);
697 goto file_error;
698 }
699 /*
700 * OPEN_FMODE, called within anon_inode_getfile/alloc_file, don't honor
701 * FMODE_LSEEK, FMODE_PREAD nor FMODE_PWRITE. We need to read from this
702 * file descriptor, so we set FMODE_PREAD here.
703 */
704 stream_file->f_mode |= FMODE_PREAD;
705 fd_install(stream_fd, stream_file);
706 /*
707 * The stream holds a reference to the channel within the generic ring
708 * buffer library, so no need to hold a refcount on the channel and
709 * session files here.
710 */
711 return stream_fd;
712
713 file_error:
714 put_unused_fd(stream_fd);
715 fd_error:
716 return ret;
717 }
718
719 static
720 int lttng_abi_open_stream(struct file *channel_file)
721 {
722 struct lttng_channel *channel = channel_file->private_data;
723 struct lib_ring_buffer *buf;
724 int ret;
725 void *stream_priv;
726
727 buf = channel->ops->buffer_read_open(channel->chan);
728 if (!buf)
729 return -ENOENT;
730
731 stream_priv = buf;
732 ret = lttng_abi_create_stream_fd(channel_file, stream_priv,
733 &lib_ring_buffer_file_operations);
734 if (ret < 0)
735 goto fd_error;
736
737 return ret;
738
739 fd_error:
740 channel->ops->buffer_read_close(buf);
741 return ret;
742 }
743
744 static
745 int lttng_abi_open_metadata_stream(struct file *channel_file)
746 {
747 struct lttng_channel *channel = channel_file->private_data;
748 struct lttng_session *session = channel->session;
749 struct lib_ring_buffer *buf;
750 int ret;
751 struct lttng_metadata_stream *metadata_stream;
752 void *stream_priv;
753
754 buf = channel->ops->buffer_read_open(channel->chan);
755 if (!buf)
756 return -ENOENT;
757
758 metadata_stream = kzalloc(sizeof(struct lttng_metadata_stream),
759 GFP_KERNEL);
760 if (!metadata_stream)
761 return -ENOMEM;
762 metadata_stream->metadata_cache = session->metadata_cache;
763 init_waitqueue_head(&metadata_stream->read_wait);
764 metadata_stream->priv = buf;
765 stream_priv = metadata_stream;
766 ret = lttng_abi_create_stream_fd(channel_file, stream_priv,
767 &lttng_metadata_ring_buffer_file_operations);
768 if (ret < 0)
769 goto fd_error;
770
771 kref_get(&session->metadata_cache->refcount);
772 list_add(&metadata_stream->list,
773 &session->metadata_cache->metadata_stream);
774 return ret;
775
776 fd_error:
777 channel->ops->buffer_read_close(buf);
778 return ret;
779 }
780
781 static
782 int lttng_abi_create_event(struct file *channel_file,
783 struct lttng_kernel_event *event_param)
784 {
785 struct lttng_channel *channel = channel_file->private_data;
786 struct lttng_event *event;
787 int event_fd, ret;
788 struct file *event_file;
789
790 event_param->name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
791 switch (event_param->instrumentation) {
792 case LTTNG_KERNEL_KRETPROBE:
793 event_param->u.kretprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
794 break;
795 case LTTNG_KERNEL_KPROBE:
796 event_param->u.kprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
797 break;
798 case LTTNG_KERNEL_FUNCTION:
799 event_param->u.ftrace.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
800 break;
801 default:
802 break;
803 }
804 switch (event_param->instrumentation) {
805 default:
806 event_fd = get_unused_fd();
807 if (event_fd < 0) {
808 ret = event_fd;
809 goto fd_error;
810 }
811 event_file = anon_inode_getfile("[lttng_event]",
812 &lttng_event_fops,
813 NULL, O_RDWR);
814 if (IS_ERR(event_file)) {
815 ret = PTR_ERR(event_file);
816 goto file_error;
817 }
818 /*
819 * We tolerate no failure path after event creation. It
820 * will stay invariant for the rest of the session.
821 */
822 event = lttng_event_create(channel, event_param, NULL, NULL);
823 if (!event) {
824 ret = -EINVAL;
825 goto event_error;
826 }
827 event_file->private_data = event;
828 fd_install(event_fd, event_file);
829 /* The event holds a reference on the channel */
830 atomic_long_inc(&channel_file->f_count);
831 break;
832 case LTTNG_KERNEL_SYSCALL:
833 /*
834 * Only all-syscall tracing supported for now.
835 */
836 if (event_param->name[0] != '\0')
837 return -EINVAL;
838 ret = lttng_syscalls_register(channel, NULL);
839 if (ret)
840 goto fd_error;
841 event_fd = 0;
842 break;
843 }
844 return event_fd;
845
846 event_error:
847 fput(event_file);
848 file_error:
849 put_unused_fd(event_fd);
850 fd_error:
851 return ret;
852 }
853
854 /**
855 * lttng_channel_ioctl - lttng syscall through ioctl
856 *
857 * @file: the file
858 * @cmd: the command
859 * @arg: command arg
860 *
861 * This ioctl implements lttng commands:
862 * LTTNG_KERNEL_STREAM
863 * Returns an event stream file descriptor or failure.
864 * (typically, one event stream records events from one CPU)
865 * LTTNG_KERNEL_EVENT
866 * Returns an event file descriptor or failure.
867 * LTTNG_KERNEL_CONTEXT
868 * Prepend a context field to each event in the channel
869 * LTTNG_KERNEL_ENABLE
870 * Enable recording for events in this channel (weak enable)
871 * LTTNG_KERNEL_DISABLE
872 * Disable recording for events in this channel (strong disable)
873 *
874 * Channel and event file descriptors also hold a reference on the session.
875 */
876 static
877 long lttng_channel_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
878 {
879 struct lttng_channel *channel = file->private_data;
880
881 switch (cmd) {
882 case LTTNG_KERNEL_OLD_STREAM:
883 case LTTNG_KERNEL_STREAM:
884 return lttng_abi_open_stream(file);
885 case LTTNG_KERNEL_OLD_EVENT:
886 {
887 struct lttng_kernel_event *uevent_param;
888 struct lttng_kernel_old_event *old_uevent_param;
889 int ret;
890
891 uevent_param = kmalloc(sizeof(struct lttng_kernel_event),
892 GFP_KERNEL);
893 if (!uevent_param) {
894 ret = -ENOMEM;
895 goto old_event_end;
896 }
897 old_uevent_param = kmalloc(
898 sizeof(struct lttng_kernel_old_event),
899 GFP_KERNEL);
900 if (!old_uevent_param) {
901 ret = -ENOMEM;
902 goto old_event_error_free_param;
903 }
904 if (copy_from_user(old_uevent_param,
905 (struct lttng_kernel_old_event __user *) arg,
906 sizeof(struct lttng_kernel_old_event))) {
907 ret = -EFAULT;
908 goto old_event_error_free_old_param;
909 }
910
911 memcpy(uevent_param->name, old_uevent_param->name,
912 sizeof(uevent_param->name));
913 uevent_param->instrumentation =
914 old_uevent_param->instrumentation;
915
916 switch (old_uevent_param->instrumentation) {
917 case LTTNG_KERNEL_KPROBE:
918 uevent_param->u.kprobe.addr =
919 old_uevent_param->u.kprobe.addr;
920 uevent_param->u.kprobe.offset =
921 old_uevent_param->u.kprobe.offset;
922 memcpy(uevent_param->u.kprobe.symbol_name,
923 old_uevent_param->u.kprobe.symbol_name,
924 sizeof(uevent_param->u.kprobe.symbol_name));
925 break;
926 case LTTNG_KERNEL_KRETPROBE:
927 uevent_param->u.kretprobe.addr =
928 old_uevent_param->u.kretprobe.addr;
929 uevent_param->u.kretprobe.offset =
930 old_uevent_param->u.kretprobe.offset;
931 memcpy(uevent_param->u.kretprobe.symbol_name,
932 old_uevent_param->u.kretprobe.symbol_name,
933 sizeof(uevent_param->u.kretprobe.symbol_name));
934 break;
935 case LTTNG_KERNEL_FUNCTION:
936 memcpy(uevent_param->u.ftrace.symbol_name,
937 old_uevent_param->u.ftrace.symbol_name,
938 sizeof(uevent_param->u.ftrace.symbol_name));
939 break;
940 default:
941 break;
942 }
943 ret = lttng_abi_create_event(file, uevent_param);
944
945 old_event_error_free_old_param:
946 kfree(old_uevent_param);
947 old_event_error_free_param:
948 kfree(uevent_param);
949 old_event_end:
950 return ret;
951 }
952 case LTTNG_KERNEL_EVENT:
953 {
954 struct lttng_kernel_event uevent_param;
955
956 if (copy_from_user(&uevent_param,
957 (struct lttng_kernel_event __user *) arg,
958 sizeof(uevent_param)))
959 return -EFAULT;
960 return lttng_abi_create_event(file, &uevent_param);
961 }
962 case LTTNG_KERNEL_OLD_CONTEXT:
963 {
964 struct lttng_kernel_context *ucontext_param;
965 struct lttng_kernel_old_context *old_ucontext_param;
966 int ret;
967
968 ucontext_param = kmalloc(sizeof(struct lttng_kernel_context),
969 GFP_KERNEL);
970 if (!ucontext_param) {
971 ret = -ENOMEM;
972 goto old_ctx_end;
973 }
974 old_ucontext_param = kmalloc(sizeof(struct lttng_kernel_old_context),
975 GFP_KERNEL);
976 if (!old_ucontext_param) {
977 ret = -ENOMEM;
978 goto old_ctx_error_free_param;
979 }
980
981 if (copy_from_user(old_ucontext_param,
982 (struct lttng_kernel_old_context __user *) arg,
983 sizeof(struct lttng_kernel_old_context))) {
984 ret = -EFAULT;
985 goto old_ctx_error_free_old_param;
986 }
987 ucontext_param->ctx = old_ucontext_param->ctx;
988 memcpy(ucontext_param->padding, old_ucontext_param->padding,
989 sizeof(ucontext_param->padding));
990 /* only type that uses the union */
991 if (old_ucontext_param->ctx == LTTNG_KERNEL_CONTEXT_PERF_COUNTER) {
992 ucontext_param->u.perf_counter.type =
993 old_ucontext_param->u.perf_counter.type;
994 ucontext_param->u.perf_counter.config =
995 old_ucontext_param->u.perf_counter.config;
996 memcpy(ucontext_param->u.perf_counter.name,
997 old_ucontext_param->u.perf_counter.name,
998 sizeof(ucontext_param->u.perf_counter.name));
999 }
1000
1001 ret = lttng_abi_add_context(file,
1002 ucontext_param,
1003 &channel->ctx, channel->session);
1004
1005 old_ctx_error_free_old_param:
1006 kfree(old_ucontext_param);
1007 old_ctx_error_free_param:
1008 kfree(ucontext_param);
1009 old_ctx_end:
1010 return ret;
1011 }
1012 case LTTNG_KERNEL_CONTEXT:
1013 {
1014 struct lttng_kernel_context ucontext_param;
1015
1016 if (copy_from_user(&ucontext_param,
1017 (struct lttng_kernel_context __user *) arg,
1018 sizeof(ucontext_param)))
1019 return -EFAULT;
1020 return lttng_abi_add_context(file,
1021 &ucontext_param,
1022 &channel->ctx, channel->session);
1023 }
1024 case LTTNG_KERNEL_OLD_ENABLE:
1025 case LTTNG_KERNEL_ENABLE:
1026 return lttng_channel_enable(channel);
1027 case LTTNG_KERNEL_OLD_DISABLE:
1028 case LTTNG_KERNEL_DISABLE:
1029 return lttng_channel_disable(channel);
1030 default:
1031 return -ENOIOCTLCMD;
1032 }
1033
1034 }
1035
1036 /**
1037 * lttng_metadata_ioctl - lttng syscall through ioctl
1038 *
1039 * @file: the file
1040 * @cmd: the command
1041 * @arg: command arg
1042 *
1043 * This ioctl implements lttng commands:
1044 * LTTNG_KERNEL_STREAM
1045 * Returns an event stream file descriptor or failure.
1046 *
1047 * Channel and event file descriptors also hold a reference on the session.
1048 */
1049 static
1050 long lttng_metadata_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1051 {
1052 switch (cmd) {
1053 case LTTNG_KERNEL_OLD_STREAM:
1054 case LTTNG_KERNEL_STREAM:
1055 return lttng_abi_open_metadata_stream(file);
1056 default:
1057 return -ENOIOCTLCMD;
1058 }
1059 }
1060
1061 /**
1062 * lttng_channel_poll - lttng stream addition/removal monitoring
1063 *
1064 * @file: the file
1065 * @wait: poll table
1066 */
1067 unsigned int lttng_channel_poll(struct file *file, poll_table *wait)
1068 {
1069 struct lttng_channel *channel = file->private_data;
1070 unsigned int mask = 0;
1071
1072 if (file->f_mode & FMODE_READ) {
1073 poll_wait_set_exclusive(wait);
1074 poll_wait(file, channel->ops->get_hp_wait_queue(channel->chan),
1075 wait);
1076
1077 if (channel->ops->is_disabled(channel->chan))
1078 return POLLERR;
1079 if (channel->ops->is_finalized(channel->chan))
1080 return POLLHUP;
1081 if (channel->ops->buffer_has_read_closed_stream(channel->chan))
1082 return POLLIN | POLLRDNORM;
1083 return 0;
1084 }
1085 return mask;
1086
1087 }
1088
1089 static
1090 int lttng_channel_release(struct inode *inode, struct file *file)
1091 {
1092 struct lttng_channel *channel = file->private_data;
1093
1094 if (channel)
1095 fput(channel->session->file);
1096 return 0;
1097 }
1098
1099 static
1100 int lttng_metadata_channel_release(struct inode *inode, struct file *file)
1101 {
1102 struct lttng_channel *channel = file->private_data;
1103
1104 if (channel) {
1105 lttng_metadata_channel_destroy(channel);
1106 fput(channel->session->file);
1107 }
1108
1109 return 0;
1110 }
1111
1112 static const struct file_operations lttng_channel_fops = {
1113 .owner = THIS_MODULE,
1114 .release = lttng_channel_release,
1115 .poll = lttng_channel_poll,
1116 .unlocked_ioctl = lttng_channel_ioctl,
1117 #ifdef CONFIG_COMPAT
1118 .compat_ioctl = lttng_channel_ioctl,
1119 #endif
1120 };
1121
1122 static const struct file_operations lttng_metadata_fops = {
1123 .owner = THIS_MODULE,
1124 .release = lttng_metadata_channel_release,
1125 .unlocked_ioctl = lttng_metadata_ioctl,
1126 #ifdef CONFIG_COMPAT
1127 .compat_ioctl = lttng_metadata_ioctl,
1128 #endif
1129 };
1130
1131 /**
1132 * lttng_event_ioctl - lttng syscall through ioctl
1133 *
1134 * @file: the file
1135 * @cmd: the command
1136 * @arg: command arg
1137 *
1138 * This ioctl implements lttng commands:
1139 * LTTNG_KERNEL_CONTEXT
1140 * Prepend a context field to each record of this event
1141 * LTTNG_KERNEL_ENABLE
1142 * Enable recording for this event (weak enable)
1143 * LTTNG_KERNEL_DISABLE
1144 * Disable recording for this event (strong disable)
1145 */
1146 static
1147 long lttng_event_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1148 {
1149 struct lttng_event *event = file->private_data;
1150
1151 switch (cmd) {
1152 case LTTNG_KERNEL_OLD_CONTEXT:
1153 {
1154 struct lttng_kernel_context *ucontext_param;
1155 struct lttng_kernel_old_context *old_ucontext_param;
1156 int ret;
1157
1158 ucontext_param = kmalloc(sizeof(struct lttng_kernel_context),
1159 GFP_KERNEL);
1160 if (!ucontext_param) {
1161 ret = -ENOMEM;
1162 goto old_ctx_end;
1163 }
1164 old_ucontext_param = kmalloc(sizeof(struct lttng_kernel_old_context),
1165 GFP_KERNEL);
1166 if (!old_ucontext_param) {
1167 ret = -ENOMEM;
1168 goto old_ctx_error_free_param;
1169 }
1170
1171 if (copy_from_user(old_ucontext_param,
1172 (struct lttng_kernel_old_context __user *) arg,
1173 sizeof(struct lttng_kernel_old_context))) {
1174 ret = -EFAULT;
1175 goto old_ctx_error_free_old_param;
1176 }
1177 ucontext_param->ctx = old_ucontext_param->ctx;
1178 memcpy(ucontext_param->padding, old_ucontext_param->padding,
1179 sizeof(ucontext_param->padding));
1180 /* only type that uses the union */
1181 if (old_ucontext_param->ctx == LTTNG_KERNEL_CONTEXT_PERF_COUNTER) {
1182 ucontext_param->u.perf_counter.type =
1183 old_ucontext_param->u.perf_counter.type;
1184 ucontext_param->u.perf_counter.config =
1185 old_ucontext_param->u.perf_counter.config;
1186 memcpy(ucontext_param->u.perf_counter.name,
1187 old_ucontext_param->u.perf_counter.name,
1188 sizeof(ucontext_param->u.perf_counter.name));
1189 }
1190
1191 ret = lttng_abi_add_context(file,
1192 ucontext_param,
1193 &event->ctx, event->chan->session);
1194
1195 old_ctx_error_free_old_param:
1196 kfree(old_ucontext_param);
1197 old_ctx_error_free_param:
1198 kfree(ucontext_param);
1199 old_ctx_end:
1200 return ret;
1201 }
1202 case LTTNG_KERNEL_CONTEXT:
1203 {
1204 struct lttng_kernel_context ucontext_param;
1205
1206 if (copy_from_user(&ucontext_param,
1207 (struct lttng_kernel_context __user *) arg,
1208 sizeof(ucontext_param)))
1209 return -EFAULT;
1210 return lttng_abi_add_context(file,
1211 &ucontext_param,
1212 &event->ctx, event->chan->session);
1213 }
1214 case LTTNG_KERNEL_OLD_ENABLE:
1215 case LTTNG_KERNEL_ENABLE:
1216 return lttng_event_enable(event);
1217 case LTTNG_KERNEL_OLD_DISABLE:
1218 case LTTNG_KERNEL_DISABLE:
1219 return lttng_event_disable(event);
1220 default:
1221 return -ENOIOCTLCMD;
1222 }
1223 }
1224
1225 static
1226 int lttng_event_release(struct inode *inode, struct file *file)
1227 {
1228 struct lttng_event *event = file->private_data;
1229
1230 if (event)
1231 fput(event->chan->file);
1232 return 0;
1233 }
1234
1235 /* TODO: filter control ioctl */
1236 static const struct file_operations lttng_event_fops = {
1237 .owner = THIS_MODULE,
1238 .release = lttng_event_release,
1239 .unlocked_ioctl = lttng_event_ioctl,
1240 #ifdef CONFIG_COMPAT
1241 .compat_ioctl = lttng_event_ioctl,
1242 #endif
1243 };
1244
1245 int __init lttng_abi_init(void)
1246 {
1247 int ret = 0;
1248
1249 wrapper_vmalloc_sync_all();
1250 lttng_proc_dentry = proc_create_data("lttng", S_IRUSR | S_IWUSR, NULL,
1251 &lttng_fops, NULL);
1252
1253 if (!lttng_proc_dentry) {
1254 printk(KERN_ERR "Error creating LTTng control file\n");
1255 ret = -ENOMEM;
1256 goto error;
1257 }
1258 error:
1259 return ret;
1260 }
1261
1262 void __exit lttng_abi_exit(void)
1263 {
1264 if (lttng_proc_dentry)
1265 remove_proc_entry("lttng", NULL);
1266 }
This page took 0.086734 seconds and 5 git commands to generate.