Fix: kvm x86 probes side-effect
[lttng-modules.git] / lttng-abi.c
... / ...
CommitLineData
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
61static struct proc_dir_entry *lttng_proc_dentry;
62static const struct file_operations lttng_fops;
63static const struct file_operations lttng_session_fops;
64static const struct file_operations lttng_channel_fops;
65static const struct file_operations lttng_metadata_fops;
66static 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
73static
74int 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
99file_error:
100 put_unused_fd(session_fd);
101fd_error:
102 lttng_session_destroy(session);
103 return ret;
104}
105
106static
107int 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
135open_error:
136 fput(tracepoint_list_file);
137file_error:
138 put_unused_fd(file_fd);
139fd_error:
140 return ret;
141}
142
143static
144void 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
151static
152long 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 */
211static
212long 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
288static 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
296static
297int 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
375chan_error:
376 fput(chan_file);
377file_error:
378 put_unused_fd(chan_fd);
379fd_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 */
402static
403long 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 */
491static
492int 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
501static 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 */
517static
518unsigned 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_out)
543 mask |= POLLIN;
544 }
545
546 return mask;
547}
548
549static
550int lttng_metadata_ring_buffer_ioctl_get_next_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
567static
568void lttng_metadata_ring_buffer_ioctl_put_next_subbuf(struct file *filp,
569 unsigned int cmd, unsigned long arg)
570{
571 struct lttng_metadata_stream *stream = filp->private_data;
572
573 stream->metadata_out = stream->metadata_in;
574}
575
576static
577long lttng_metadata_ring_buffer_ioctl(struct file *filp,
578 unsigned int cmd, unsigned long arg)
579{
580 int ret;
581 struct lttng_metadata_stream *stream = filp->private_data;
582 struct lib_ring_buffer *buf = stream->priv;
583
584 switch (cmd) {
585 case RING_BUFFER_GET_NEXT_SUBBUF:
586 {
587 ret = lttng_metadata_ring_buffer_ioctl_get_next_subbuf(filp,
588 cmd, arg);
589 if (ret < 0)
590 goto err;
591 break;
592 }
593 case RING_BUFFER_GET_SUBBUF:
594 {
595 /*
596 * Random access is not allowed for metadata channel.
597 */
598 return -ENOSYS;
599 }
600 default:
601 break;
602 }
603 /* PUT_SUBBUF is the one from lib ring buffer, unmodified. */
604
605 /* Performing lib ring buffer ioctl after our own. */
606 ret = lib_ring_buffer_ioctl(filp, cmd, arg, buf);
607 if (ret < 0)
608 goto err;
609
610 switch (cmd) {
611 case RING_BUFFER_PUT_NEXT_SUBBUF:
612 {
613 lttng_metadata_ring_buffer_ioctl_put_next_subbuf(filp,
614 cmd, arg);
615 break;
616 }
617 default:
618 break;
619 }
620err:
621 return ret;
622}
623
624#ifdef CONFIG_COMPAT
625static
626long lttng_metadata_ring_buffer_compat_ioctl(struct file *filp,
627 unsigned int cmd, unsigned long arg)
628{
629 int ret;
630 struct lttng_metadata_stream *stream = filp->private_data;
631 struct lib_ring_buffer *buf = stream->priv;
632
633 switch (cmd) {
634 case RING_BUFFER_GET_NEXT_SUBBUF:
635 {
636 ret = lttng_metadata_ring_buffer_ioctl_get_next_subbuf(filp,
637 cmd, arg);
638 if (ret < 0)
639 goto err;
640 break;
641 }
642 case RING_BUFFER_GET_SUBBUF:
643 {
644 /*
645 * Random access is not allowed for metadata channel.
646 */
647 return -ENOSYS;
648 }
649 default:
650 break;
651 }
652 /* PUT_SUBBUF is the one from lib ring buffer, unmodified. */
653
654 /* Performing lib ring buffer ioctl after our own. */
655 ret = lib_ring_buffer_compat_ioctl(filp, cmd, arg, buf);
656 if (ret < 0)
657 goto err;
658
659 switch (cmd) {
660 case RING_BUFFER_PUT_NEXT_SUBBUF:
661 {
662 lttng_metadata_ring_buffer_ioctl_put_next_subbuf(filp,
663 cmd, arg);
664 break;
665 }
666 default:
667 break;
668 }
669err:
670 return ret;
671}
672#endif
673
674static
675int lttng_metadata_ring_buffer_open(struct inode *inode, struct file *file)
676{
677 struct lttng_metadata_stream *stream = inode->i_private;
678 struct lib_ring_buffer *buf = stream->priv;
679
680 file->private_data = buf;
681 return lib_ring_buffer_open(inode, file, buf);
682}
683
684static
685int lttng_metadata_ring_buffer_release(struct inode *inode, struct file *file)
686{
687 struct lttng_metadata_stream *stream = file->private_data;
688 struct lib_ring_buffer *buf = stream->priv;
689
690 kref_put(&stream->metadata_cache->refcount, metadata_cache_destroy);
691
692 return lib_ring_buffer_release(inode, file, buf);
693}
694
695static
696ssize_t lttng_metadata_ring_buffer_splice_read(struct file *in, loff_t *ppos,
697 struct pipe_inode_info *pipe, size_t len,
698 unsigned int flags)
699{
700 struct lttng_metadata_stream *stream = in->private_data;
701 struct lib_ring_buffer *buf = stream->priv;
702
703 return lib_ring_buffer_splice_read(in, ppos, pipe, len,
704 flags, buf);
705}
706
707static
708int lttng_metadata_ring_buffer_mmap(struct file *filp,
709 struct vm_area_struct *vma)
710{
711 struct lttng_metadata_stream *stream = filp->private_data;
712 struct lib_ring_buffer *buf = stream->priv;
713
714 return lib_ring_buffer_mmap(filp, vma, buf);
715}
716
717static
718const struct file_operations lttng_metadata_ring_buffer_file_operations = {
719 .owner = THIS_MODULE,
720 .open = lttng_metadata_ring_buffer_open,
721 .release = lttng_metadata_ring_buffer_release,
722 .poll = lttng_metadata_ring_buffer_poll,
723 .splice_read = lttng_metadata_ring_buffer_splice_read,
724 .mmap = lttng_metadata_ring_buffer_mmap,
725 .unlocked_ioctl = lttng_metadata_ring_buffer_ioctl,
726 .llseek = vfs_lib_ring_buffer_no_llseek,
727#ifdef CONFIG_COMPAT
728 .compat_ioctl = lttng_metadata_ring_buffer_compat_ioctl,
729#endif
730};
731
732static
733int lttng_abi_create_stream_fd(struct file *channel_file, void *stream_priv,
734 const struct file_operations *fops)
735{
736 int stream_fd, ret;
737 struct file *stream_file;
738
739 stream_fd = get_unused_fd();
740 if (stream_fd < 0) {
741 ret = stream_fd;
742 goto fd_error;
743 }
744 stream_file = anon_inode_getfile("[lttng_stream]", fops,
745 stream_priv, O_RDWR);
746 if (IS_ERR(stream_file)) {
747 ret = PTR_ERR(stream_file);
748 goto file_error;
749 }
750 /*
751 * OPEN_FMODE, called within anon_inode_getfile/alloc_file, don't honor
752 * FMODE_LSEEK, FMODE_PREAD nor FMODE_PWRITE. We need to read from this
753 * file descriptor, so we set FMODE_PREAD here.
754 */
755 stream_file->f_mode |= FMODE_PREAD;
756 fd_install(stream_fd, stream_file);
757 /*
758 * The stream holds a reference to the channel within the generic ring
759 * buffer library, so no need to hold a refcount on the channel and
760 * session files here.
761 */
762 return stream_fd;
763
764file_error:
765 put_unused_fd(stream_fd);
766fd_error:
767 return ret;
768}
769
770static
771int lttng_abi_open_stream(struct file *channel_file)
772{
773 struct lttng_channel *channel = channel_file->private_data;
774 struct lib_ring_buffer *buf;
775 int ret;
776 void *stream_priv;
777
778 buf = channel->ops->buffer_read_open(channel->chan);
779 if (!buf)
780 return -ENOENT;
781
782 stream_priv = buf;
783 ret = lttng_abi_create_stream_fd(channel_file, stream_priv,
784 &lib_ring_buffer_file_operations);
785 if (ret < 0)
786 goto fd_error;
787
788 return ret;
789
790fd_error:
791 channel->ops->buffer_read_close(buf);
792 return ret;
793}
794
795static
796int lttng_abi_open_metadata_stream(struct file *channel_file)
797{
798 struct lttng_channel *channel = channel_file->private_data;
799 struct lttng_session *session = channel->session;
800 struct lib_ring_buffer *buf;
801 int ret;
802 struct lttng_metadata_stream *metadata_stream;
803 void *stream_priv;
804
805 buf = channel->ops->buffer_read_open(channel->chan);
806 if (!buf)
807 return -ENOENT;
808
809 metadata_stream = kzalloc(sizeof(struct lttng_metadata_stream),
810 GFP_KERNEL);
811 if (!metadata_stream)
812 return -ENOMEM;
813 metadata_stream->metadata_cache = session->metadata_cache;
814 init_waitqueue_head(&metadata_stream->read_wait);
815 metadata_stream->priv = buf;
816 stream_priv = metadata_stream;
817 ret = lttng_abi_create_stream_fd(channel_file, stream_priv,
818 &lttng_metadata_ring_buffer_file_operations);
819 if (ret < 0)
820 goto fd_error;
821
822 kref_get(&session->metadata_cache->refcount);
823 list_add(&metadata_stream->list,
824 &session->metadata_cache->metadata_stream);
825 return ret;
826
827fd_error:
828 channel->ops->buffer_read_close(buf);
829 return ret;
830}
831
832static
833int lttng_abi_create_event(struct file *channel_file,
834 struct lttng_kernel_event *event_param)
835{
836 struct lttng_channel *channel = channel_file->private_data;
837 struct lttng_event *event;
838 int event_fd, ret;
839 struct file *event_file;
840
841 event_param->name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
842 switch (event_param->instrumentation) {
843 case LTTNG_KERNEL_KRETPROBE:
844 event_param->u.kretprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
845 break;
846 case LTTNG_KERNEL_KPROBE:
847 event_param->u.kprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
848 break;
849 case LTTNG_KERNEL_FUNCTION:
850 event_param->u.ftrace.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
851 break;
852 default:
853 break;
854 }
855 switch (event_param->instrumentation) {
856 default:
857 event_fd = get_unused_fd();
858 if (event_fd < 0) {
859 ret = event_fd;
860 goto fd_error;
861 }
862 event_file = anon_inode_getfile("[lttng_event]",
863 &lttng_event_fops,
864 NULL, O_RDWR);
865 if (IS_ERR(event_file)) {
866 ret = PTR_ERR(event_file);
867 goto file_error;
868 }
869 /*
870 * We tolerate no failure path after event creation. It
871 * will stay invariant for the rest of the session.
872 */
873 event = lttng_event_create(channel, event_param, NULL, NULL);
874 if (!event) {
875 ret = -EINVAL;
876 goto event_error;
877 }
878 event_file->private_data = event;
879 fd_install(event_fd, event_file);
880 /* The event holds a reference on the channel */
881 atomic_long_inc(&channel_file->f_count);
882 break;
883 case LTTNG_KERNEL_SYSCALL:
884 /*
885 * Only all-syscall tracing supported for now.
886 */
887 if (event_param->name[0] != '\0')
888 return -EINVAL;
889 ret = lttng_syscalls_register(channel, NULL);
890 if (ret)
891 goto fd_error;
892 event_fd = 0;
893 break;
894 }
895 return event_fd;
896
897event_error:
898 fput(event_file);
899file_error:
900 put_unused_fd(event_fd);
901fd_error:
902 return ret;
903}
904
905/**
906 * lttng_channel_ioctl - lttng syscall through ioctl
907 *
908 * @file: the file
909 * @cmd: the command
910 * @arg: command arg
911 *
912 * This ioctl implements lttng commands:
913 * LTTNG_KERNEL_STREAM
914 * Returns an event stream file descriptor or failure.
915 * (typically, one event stream records events from one CPU)
916 * LTTNG_KERNEL_EVENT
917 * Returns an event file descriptor or failure.
918 * LTTNG_KERNEL_CONTEXT
919 * Prepend a context field to each event in the channel
920 * LTTNG_KERNEL_ENABLE
921 * Enable recording for events in this channel (weak enable)
922 * LTTNG_KERNEL_DISABLE
923 * Disable recording for events in this channel (strong disable)
924 *
925 * Channel and event file descriptors also hold a reference on the session.
926 */
927static
928long lttng_channel_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
929{
930 struct lttng_channel *channel = file->private_data;
931
932 switch (cmd) {
933 case LTTNG_KERNEL_OLD_STREAM:
934 case LTTNG_KERNEL_STREAM:
935 return lttng_abi_open_stream(file);
936 case LTTNG_KERNEL_OLD_EVENT:
937 {
938 struct lttng_kernel_event *uevent_param;
939 struct lttng_kernel_old_event *old_uevent_param;
940 int ret;
941
942 uevent_param = kmalloc(sizeof(struct lttng_kernel_event),
943 GFP_KERNEL);
944 if (!uevent_param) {
945 ret = -ENOMEM;
946 goto old_event_end;
947 }
948 old_uevent_param = kmalloc(
949 sizeof(struct lttng_kernel_old_event),
950 GFP_KERNEL);
951 if (!old_uevent_param) {
952 ret = -ENOMEM;
953 goto old_event_error_free_param;
954 }
955 if (copy_from_user(old_uevent_param,
956 (struct lttng_kernel_old_event __user *) arg,
957 sizeof(struct lttng_kernel_old_event))) {
958 ret = -EFAULT;
959 goto old_event_error_free_old_param;
960 }
961
962 memcpy(uevent_param->name, old_uevent_param->name,
963 sizeof(uevent_param->name));
964 uevent_param->instrumentation =
965 old_uevent_param->instrumentation;
966
967 switch (old_uevent_param->instrumentation) {
968 case LTTNG_KERNEL_KPROBE:
969 uevent_param->u.kprobe.addr =
970 old_uevent_param->u.kprobe.addr;
971 uevent_param->u.kprobe.offset =
972 old_uevent_param->u.kprobe.offset;
973 memcpy(uevent_param->u.kprobe.symbol_name,
974 old_uevent_param->u.kprobe.symbol_name,
975 sizeof(uevent_param->u.kprobe.symbol_name));
976 break;
977 case LTTNG_KERNEL_KRETPROBE:
978 uevent_param->u.kretprobe.addr =
979 old_uevent_param->u.kretprobe.addr;
980 uevent_param->u.kretprobe.offset =
981 old_uevent_param->u.kretprobe.offset;
982 memcpy(uevent_param->u.kretprobe.symbol_name,
983 old_uevent_param->u.kretprobe.symbol_name,
984 sizeof(uevent_param->u.kretprobe.symbol_name));
985 break;
986 case LTTNG_KERNEL_FUNCTION:
987 memcpy(uevent_param->u.ftrace.symbol_name,
988 old_uevent_param->u.ftrace.symbol_name,
989 sizeof(uevent_param->u.ftrace.symbol_name));
990 break;
991 default:
992 break;
993 }
994 ret = lttng_abi_create_event(file, uevent_param);
995
996old_event_error_free_old_param:
997 kfree(old_uevent_param);
998old_event_error_free_param:
999 kfree(uevent_param);
1000old_event_end:
1001 return ret;
1002 }
1003 case LTTNG_KERNEL_EVENT:
1004 {
1005 struct lttng_kernel_event uevent_param;
1006
1007 if (copy_from_user(&uevent_param,
1008 (struct lttng_kernel_event __user *) arg,
1009 sizeof(uevent_param)))
1010 return -EFAULT;
1011 return lttng_abi_create_event(file, &uevent_param);
1012 }
1013 case LTTNG_KERNEL_OLD_CONTEXT:
1014 {
1015 struct lttng_kernel_context *ucontext_param;
1016 struct lttng_kernel_old_context *old_ucontext_param;
1017 int ret;
1018
1019 ucontext_param = kmalloc(sizeof(struct lttng_kernel_context),
1020 GFP_KERNEL);
1021 if (!ucontext_param) {
1022 ret = -ENOMEM;
1023 goto old_ctx_end;
1024 }
1025 old_ucontext_param = kmalloc(sizeof(struct lttng_kernel_old_context),
1026 GFP_KERNEL);
1027 if (!old_ucontext_param) {
1028 ret = -ENOMEM;
1029 goto old_ctx_error_free_param;
1030 }
1031
1032 if (copy_from_user(old_ucontext_param,
1033 (struct lttng_kernel_old_context __user *) arg,
1034 sizeof(struct lttng_kernel_old_context))) {
1035 ret = -EFAULT;
1036 goto old_ctx_error_free_old_param;
1037 }
1038 ucontext_param->ctx = old_ucontext_param->ctx;
1039 memcpy(ucontext_param->padding, old_ucontext_param->padding,
1040 sizeof(ucontext_param->padding));
1041 /* only type that uses the union */
1042 if (old_ucontext_param->ctx == LTTNG_KERNEL_CONTEXT_PERF_COUNTER) {
1043 ucontext_param->u.perf_counter.type =
1044 old_ucontext_param->u.perf_counter.type;
1045 ucontext_param->u.perf_counter.config =
1046 old_ucontext_param->u.perf_counter.config;
1047 memcpy(ucontext_param->u.perf_counter.name,
1048 old_ucontext_param->u.perf_counter.name,
1049 sizeof(ucontext_param->u.perf_counter.name));
1050 }
1051
1052 ret = lttng_abi_add_context(file,
1053 ucontext_param,
1054 &channel->ctx, channel->session);
1055
1056old_ctx_error_free_old_param:
1057 kfree(old_ucontext_param);
1058old_ctx_error_free_param:
1059 kfree(ucontext_param);
1060old_ctx_end:
1061 return ret;
1062 }
1063 case LTTNG_KERNEL_CONTEXT:
1064 {
1065 struct lttng_kernel_context ucontext_param;
1066
1067 if (copy_from_user(&ucontext_param,
1068 (struct lttng_kernel_context __user *) arg,
1069 sizeof(ucontext_param)))
1070 return -EFAULT;
1071 return lttng_abi_add_context(file,
1072 &ucontext_param,
1073 &channel->ctx, channel->session);
1074 }
1075 case LTTNG_KERNEL_OLD_ENABLE:
1076 case LTTNG_KERNEL_ENABLE:
1077 return lttng_channel_enable(channel);
1078 case LTTNG_KERNEL_OLD_DISABLE:
1079 case LTTNG_KERNEL_DISABLE:
1080 return lttng_channel_disable(channel);
1081 default:
1082 return -ENOIOCTLCMD;
1083 }
1084
1085}
1086
1087/**
1088 * lttng_metadata_ioctl - lttng syscall through ioctl
1089 *
1090 * @file: the file
1091 * @cmd: the command
1092 * @arg: command arg
1093 *
1094 * This ioctl implements lttng commands:
1095 * LTTNG_KERNEL_STREAM
1096 * Returns an event stream file descriptor or failure.
1097 *
1098 * Channel and event file descriptors also hold a reference on the session.
1099 */
1100static
1101long lttng_metadata_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1102{
1103 switch (cmd) {
1104 case LTTNG_KERNEL_OLD_STREAM:
1105 case LTTNG_KERNEL_STREAM:
1106 return lttng_abi_open_metadata_stream(file);
1107 default:
1108 return -ENOIOCTLCMD;
1109 }
1110}
1111
1112/**
1113 * lttng_channel_poll - lttng stream addition/removal monitoring
1114 *
1115 * @file: the file
1116 * @wait: poll table
1117 */
1118unsigned int lttng_channel_poll(struct file *file, poll_table *wait)
1119{
1120 struct lttng_channel *channel = file->private_data;
1121 unsigned int mask = 0;
1122
1123 if (file->f_mode & FMODE_READ) {
1124 poll_wait_set_exclusive(wait);
1125 poll_wait(file, channel->ops->get_hp_wait_queue(channel->chan),
1126 wait);
1127
1128 if (channel->ops->is_disabled(channel->chan))
1129 return POLLERR;
1130 if (channel->ops->is_finalized(channel->chan))
1131 return POLLHUP;
1132 if (channel->ops->buffer_has_read_closed_stream(channel->chan))
1133 return POLLIN | POLLRDNORM;
1134 return 0;
1135 }
1136 return mask;
1137
1138}
1139
1140static
1141int lttng_channel_release(struct inode *inode, struct file *file)
1142{
1143 struct lttng_channel *channel = file->private_data;
1144
1145 if (channel)
1146 fput(channel->session->file);
1147 return 0;
1148}
1149
1150static
1151int lttng_metadata_channel_release(struct inode *inode, struct file *file)
1152{
1153 struct lttng_channel *channel = file->private_data;
1154
1155 if (channel) {
1156 lttng_metadata_channel_destroy(channel);
1157 fput(channel->session->file);
1158 }
1159
1160 return 0;
1161}
1162
1163static const struct file_operations lttng_channel_fops = {
1164 .owner = THIS_MODULE,
1165 .release = lttng_channel_release,
1166 .poll = lttng_channel_poll,
1167 .unlocked_ioctl = lttng_channel_ioctl,
1168#ifdef CONFIG_COMPAT
1169 .compat_ioctl = lttng_channel_ioctl,
1170#endif
1171};
1172
1173static const struct file_operations lttng_metadata_fops = {
1174 .owner = THIS_MODULE,
1175 .release = lttng_metadata_channel_release,
1176 .unlocked_ioctl = lttng_metadata_ioctl,
1177#ifdef CONFIG_COMPAT
1178 .compat_ioctl = lttng_metadata_ioctl,
1179#endif
1180};
1181
1182/**
1183 * lttng_event_ioctl - lttng syscall through ioctl
1184 *
1185 * @file: the file
1186 * @cmd: the command
1187 * @arg: command arg
1188 *
1189 * This ioctl implements lttng commands:
1190 * LTTNG_KERNEL_CONTEXT
1191 * Prepend a context field to each record of this event
1192 * LTTNG_KERNEL_ENABLE
1193 * Enable recording for this event (weak enable)
1194 * LTTNG_KERNEL_DISABLE
1195 * Disable recording for this event (strong disable)
1196 */
1197static
1198long lttng_event_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1199{
1200 struct lttng_event *event = file->private_data;
1201
1202 switch (cmd) {
1203 case LTTNG_KERNEL_OLD_CONTEXT:
1204 {
1205 struct lttng_kernel_context *ucontext_param;
1206 struct lttng_kernel_old_context *old_ucontext_param;
1207 int ret;
1208
1209 ucontext_param = kmalloc(sizeof(struct lttng_kernel_context),
1210 GFP_KERNEL);
1211 if (!ucontext_param) {
1212 ret = -ENOMEM;
1213 goto old_ctx_end;
1214 }
1215 old_ucontext_param = kmalloc(sizeof(struct lttng_kernel_old_context),
1216 GFP_KERNEL);
1217 if (!old_ucontext_param) {
1218 ret = -ENOMEM;
1219 goto old_ctx_error_free_param;
1220 }
1221
1222 if (copy_from_user(old_ucontext_param,
1223 (struct lttng_kernel_old_context __user *) arg,
1224 sizeof(struct lttng_kernel_old_context))) {
1225 ret = -EFAULT;
1226 goto old_ctx_error_free_old_param;
1227 }
1228 ucontext_param->ctx = old_ucontext_param->ctx;
1229 memcpy(ucontext_param->padding, old_ucontext_param->padding,
1230 sizeof(ucontext_param->padding));
1231 /* only type that uses the union */
1232 if (old_ucontext_param->ctx == LTTNG_KERNEL_CONTEXT_PERF_COUNTER) {
1233 ucontext_param->u.perf_counter.type =
1234 old_ucontext_param->u.perf_counter.type;
1235 ucontext_param->u.perf_counter.config =
1236 old_ucontext_param->u.perf_counter.config;
1237 memcpy(ucontext_param->u.perf_counter.name,
1238 old_ucontext_param->u.perf_counter.name,
1239 sizeof(ucontext_param->u.perf_counter.name));
1240 }
1241
1242 ret = lttng_abi_add_context(file,
1243 ucontext_param,
1244 &event->ctx, event->chan->session);
1245
1246old_ctx_error_free_old_param:
1247 kfree(old_ucontext_param);
1248old_ctx_error_free_param:
1249 kfree(ucontext_param);
1250old_ctx_end:
1251 return ret;
1252 }
1253 case LTTNG_KERNEL_CONTEXT:
1254 {
1255 struct lttng_kernel_context ucontext_param;
1256
1257 if (copy_from_user(&ucontext_param,
1258 (struct lttng_kernel_context __user *) arg,
1259 sizeof(ucontext_param)))
1260 return -EFAULT;
1261 return lttng_abi_add_context(file,
1262 &ucontext_param,
1263 &event->ctx, event->chan->session);
1264 }
1265 case LTTNG_KERNEL_OLD_ENABLE:
1266 case LTTNG_KERNEL_ENABLE:
1267 return lttng_event_enable(event);
1268 case LTTNG_KERNEL_OLD_DISABLE:
1269 case LTTNG_KERNEL_DISABLE:
1270 return lttng_event_disable(event);
1271 default:
1272 return -ENOIOCTLCMD;
1273 }
1274}
1275
1276static
1277int lttng_event_release(struct inode *inode, struct file *file)
1278{
1279 struct lttng_event *event = file->private_data;
1280
1281 if (event)
1282 fput(event->chan->file);
1283 return 0;
1284}
1285
1286/* TODO: filter control ioctl */
1287static const struct file_operations lttng_event_fops = {
1288 .owner = THIS_MODULE,
1289 .release = lttng_event_release,
1290 .unlocked_ioctl = lttng_event_ioctl,
1291#ifdef CONFIG_COMPAT
1292 .compat_ioctl = lttng_event_ioctl,
1293#endif
1294};
1295
1296int __init lttng_abi_init(void)
1297{
1298 int ret = 0;
1299
1300 wrapper_vmalloc_sync_all();
1301 lttng_proc_dentry = proc_create_data("lttng", S_IRUSR | S_IWUSR, NULL,
1302 &lttng_fops, NULL);
1303
1304 if (!lttng_proc_dentry) {
1305 printk(KERN_ERR "Error creating LTTng control file\n");
1306 ret = -ENOMEM;
1307 goto error;
1308 }
1309error:
1310 return ret;
1311}
1312
1313void __exit lttng_abi_exit(void)
1314{
1315 if (lttng_proc_dentry)
1316 remove_proc_entry("lttng", NULL);
1317}
This page took 0.026259 seconds and 4 git commands to generate.