6 * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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.
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.
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
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.
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 <linux/err.h>
47 #include "wrapper/vmalloc.h" /* for wrapper_vmalloc_sync_all() */
48 #include "wrapper/ringbuffer/vfs.h"
49 #include "wrapper/ringbuffer/backend.h"
50 #include "wrapper/ringbuffer/frontend.h"
51 #include "wrapper/poll.h"
52 #include "wrapper/file.h"
53 #include "lttng-abi.h"
54 #include "lttng-abi-old.h"
55 #include "lttng-events.h"
56 #include "lttng-tracer.h"
57 #include "lib/ringbuffer/frontend_types.h"
60 * This is LTTng's own personal way to create a system call as an external
61 * module. We use ioctl() on /proc/lttng.
64 static struct proc_dir_entry
*lttng_proc_dentry
;
65 static const struct file_operations lttng_fops
;
66 static const struct file_operations lttng_session_fops
;
67 static const struct file_operations lttng_channel_fops
;
68 static const struct file_operations lttng_metadata_fops
;
69 static const struct file_operations lttng_event_fops
;
70 static struct file_operations lttng_stream_ring_buffer_file_operations
;
73 * Teardown management: opened file descriptors keep a refcount on the module,
74 * so it can only exit when all file descriptors are closed.
78 int lttng_abi_create_session(void)
80 struct lttng_session
*session
;
81 struct file
*session_file
;
84 session
= lttng_session_create();
87 session_fd
= lttng_get_unused_fd();
92 session_file
= anon_inode_getfile("[lttng_session]",
95 if (IS_ERR(session_file
)) {
96 ret
= PTR_ERR(session_file
);
99 session
->file
= session_file
;
100 fd_install(session_fd
, session_file
);
104 put_unused_fd(session_fd
);
106 lttng_session_destroy(session
);
111 int lttng_abi_tracepoint_list(void)
113 struct file
*tracepoint_list_file
;
116 file_fd
= lttng_get_unused_fd();
122 tracepoint_list_file
= anon_inode_getfile("[lttng_tracepoint_list]",
123 <tng_tracepoint_list_fops
,
125 if (IS_ERR(tracepoint_list_file
)) {
126 ret
= PTR_ERR(tracepoint_list_file
);
129 ret
= lttng_tracepoint_list_fops
.open(NULL
, tracepoint_list_file
);
132 fd_install(file_fd
, tracepoint_list_file
);
136 fput(tracepoint_list_file
);
138 put_unused_fd(file_fd
);
143 #ifndef CONFIG_HAVE_SYSCALL_TRACEPOINTS
145 int lttng_abi_syscall_list(void)
151 int lttng_abi_syscall_list(void)
153 struct file
*syscall_list_file
;
156 file_fd
= lttng_get_unused_fd();
162 syscall_list_file
= anon_inode_getfile("[lttng_syscall_list]",
163 <tng_syscall_list_fops
,
165 if (IS_ERR(syscall_list_file
)) {
166 ret
= PTR_ERR(syscall_list_file
);
169 ret
= lttng_syscall_list_fops
.open(NULL
, syscall_list_file
);
172 fd_install(file_fd
, syscall_list_file
);
176 fput(syscall_list_file
);
178 put_unused_fd(file_fd
);
185 void lttng_abi_tracer_version(struct lttng_kernel_tracer_version
*v
)
187 v
->major
= LTTNG_MODULES_MAJOR_VERSION
;
188 v
->minor
= LTTNG_MODULES_MINOR_VERSION
;
189 v
->patchlevel
= LTTNG_MODULES_PATCHLEVEL_VERSION
;
193 void lttng_abi_tracer_abi_version(struct lttng_kernel_tracer_abi_version
*v
)
195 v
->major
= LTTNG_MODULES_ABI_MAJOR_VERSION
;
196 v
->minor
= LTTNG_MODULES_ABI_MINOR_VERSION
;
200 long lttng_abi_add_context(struct file
*file
,
201 struct lttng_kernel_context
*context_param
,
202 struct lttng_ctx
**ctx
, struct lttng_session
*session
)
205 if (session
->been_active
)
208 switch (context_param
->ctx
) {
209 case LTTNG_KERNEL_CONTEXT_PID
:
210 return lttng_add_pid_to_ctx(ctx
);
211 case LTTNG_KERNEL_CONTEXT_PRIO
:
212 return lttng_add_prio_to_ctx(ctx
);
213 case LTTNG_KERNEL_CONTEXT_NICE
:
214 return lttng_add_nice_to_ctx(ctx
);
215 case LTTNG_KERNEL_CONTEXT_VPID
:
216 return lttng_add_vpid_to_ctx(ctx
);
217 case LTTNG_KERNEL_CONTEXT_TID
:
218 return lttng_add_tid_to_ctx(ctx
);
219 case LTTNG_KERNEL_CONTEXT_VTID
:
220 return lttng_add_vtid_to_ctx(ctx
);
221 case LTTNG_KERNEL_CONTEXT_PPID
:
222 return lttng_add_ppid_to_ctx(ctx
);
223 case LTTNG_KERNEL_CONTEXT_VPPID
:
224 return lttng_add_vppid_to_ctx(ctx
);
225 case LTTNG_KERNEL_CONTEXT_PERF_COUNTER
:
226 context_param
->u
.perf_counter
.name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
227 return lttng_add_perf_counter_to_ctx(context_param
->u
.perf_counter
.type
,
228 context_param
->u
.perf_counter
.config
,
229 context_param
->u
.perf_counter
.name
,
231 case LTTNG_KERNEL_CONTEXT_PROCNAME
:
232 return lttng_add_procname_to_ctx(ctx
);
233 case LTTNG_KERNEL_CONTEXT_HOSTNAME
:
234 return lttng_add_hostname_to_ctx(ctx
);
235 case LTTNG_KERNEL_CONTEXT_CPU_ID
:
236 return lttng_add_cpu_id_to_ctx(ctx
);
243 * lttng_ioctl - lttng syscall through ioctl
249 * This ioctl implements lttng commands:
250 * LTTNG_KERNEL_SESSION
251 * Returns a LTTng trace session file descriptor
252 * LTTNG_KERNEL_TRACER_VERSION
253 * Returns the LTTng kernel tracer version
254 * LTTNG_KERNEL_TRACEPOINT_LIST
255 * Returns a file descriptor listing available tracepoints
256 * LTTNG_KERNEL_WAIT_QUIESCENT
257 * Returns after all previously running probes have completed
258 * LTTNG_KERNEL_TRACER_ABI_VERSION
259 * Returns the LTTng kernel tracer ABI version
261 * The returned session will be deleted when its file descriptor is closed.
264 long lttng_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
267 case LTTNG_KERNEL_OLD_SESSION
:
268 case LTTNG_KERNEL_SESSION
:
269 return lttng_abi_create_session();
270 case LTTNG_KERNEL_OLD_TRACER_VERSION
:
272 struct lttng_kernel_tracer_version v
;
273 struct lttng_kernel_old_tracer_version oldv
;
274 struct lttng_kernel_old_tracer_version
*uversion
=
275 (struct lttng_kernel_old_tracer_version __user
*) arg
;
277 lttng_abi_tracer_version(&v
);
278 oldv
.major
= v
.major
;
279 oldv
.minor
= v
.minor
;
280 oldv
.patchlevel
= v
.patchlevel
;
282 if (copy_to_user(uversion
, &oldv
, sizeof(oldv
)))
286 case LTTNG_KERNEL_TRACER_VERSION
:
288 struct lttng_kernel_tracer_version version
;
289 struct lttng_kernel_tracer_version
*uversion
=
290 (struct lttng_kernel_tracer_version __user
*) arg
;
292 lttng_abi_tracer_version(&version
);
294 if (copy_to_user(uversion
, &version
, sizeof(version
)))
298 case LTTNG_KERNEL_TRACER_ABI_VERSION
:
300 struct lttng_kernel_tracer_abi_version version
;
301 struct lttng_kernel_tracer_abi_version
*uversion
=
302 (struct lttng_kernel_tracer_abi_version __user
*) arg
;
304 lttng_abi_tracer_abi_version(&version
);
306 if (copy_to_user(uversion
, &version
, sizeof(version
)))
310 case LTTNG_KERNEL_OLD_TRACEPOINT_LIST
:
311 case LTTNG_KERNEL_TRACEPOINT_LIST
:
312 return lttng_abi_tracepoint_list();
313 case LTTNG_KERNEL_SYSCALL_LIST
:
314 return lttng_abi_syscall_list();
315 case LTTNG_KERNEL_OLD_WAIT_QUIESCENT
:
316 case LTTNG_KERNEL_WAIT_QUIESCENT
:
319 case LTTNG_KERNEL_OLD_CALIBRATE
:
321 struct lttng_kernel_old_calibrate __user
*ucalibrate
=
322 (struct lttng_kernel_old_calibrate __user
*) arg
;
323 struct lttng_kernel_old_calibrate old_calibrate
;
324 struct lttng_kernel_calibrate calibrate
;
327 if (copy_from_user(&old_calibrate
, ucalibrate
, sizeof(old_calibrate
)))
329 calibrate
.type
= old_calibrate
.type
;
330 ret
= lttng_calibrate(&calibrate
);
331 if (copy_to_user(ucalibrate
, &old_calibrate
, sizeof(old_calibrate
)))
335 case LTTNG_KERNEL_CALIBRATE
:
337 struct lttng_kernel_calibrate __user
*ucalibrate
=
338 (struct lttng_kernel_calibrate __user
*) arg
;
339 struct lttng_kernel_calibrate calibrate
;
342 if (copy_from_user(&calibrate
, ucalibrate
, sizeof(calibrate
)))
344 ret
= lttng_calibrate(&calibrate
);
345 if (copy_to_user(ucalibrate
, &calibrate
, sizeof(calibrate
)))
354 static const struct file_operations lttng_fops
= {
355 .owner
= THIS_MODULE
,
356 .unlocked_ioctl
= lttng_ioctl
,
358 .compat_ioctl
= lttng_ioctl
,
363 int lttng_abi_create_channel(struct file
*session_file
,
364 struct lttng_kernel_channel
*chan_param
,
365 enum channel_type channel_type
)
367 struct lttng_session
*session
= session_file
->private_data
;
368 const struct file_operations
*fops
= NULL
;
369 const char *transport_name
;
370 struct lttng_channel
*chan
;
371 struct file
*chan_file
;
375 chan_fd
= lttng_get_unused_fd();
380 switch (channel_type
) {
381 case PER_CPU_CHANNEL
:
382 fops
= <tng_channel_fops
;
384 case METADATA_CHANNEL
:
385 fops
= <tng_metadata_fops
;
389 chan_file
= anon_inode_getfile("[lttng_channel]",
392 if (IS_ERR(chan_file
)) {
393 ret
= PTR_ERR(chan_file
);
396 switch (channel_type
) {
397 case PER_CPU_CHANNEL
:
398 if (chan_param
->output
== LTTNG_KERNEL_SPLICE
) {
399 transport_name
= chan_param
->overwrite
?
400 "relay-overwrite" : "relay-discard";
401 } else if (chan_param
->output
== LTTNG_KERNEL_MMAP
) {
402 transport_name
= chan_param
->overwrite
?
403 "relay-overwrite-mmap" : "relay-discard-mmap";
408 case METADATA_CHANNEL
:
409 if (chan_param
->output
== LTTNG_KERNEL_SPLICE
)
410 transport_name
= "relay-metadata";
411 else if (chan_param
->output
== LTTNG_KERNEL_MMAP
)
412 transport_name
= "relay-metadata-mmap";
417 transport_name
= "<unknown>";
421 * We tolerate no failure path after channel creation. It will stay
422 * invariant for the rest of the session.
424 chan
= lttng_channel_create(session
, transport_name
, NULL
,
425 chan_param
->subbuf_size
,
426 chan_param
->num_subbuf
,
427 chan_param
->switch_timer_interval
,
428 chan_param
->read_timer_interval
,
434 chan
->file
= chan_file
;
435 chan_file
->private_data
= chan
;
436 fd_install(chan_fd
, chan_file
);
437 atomic_long_inc(&session_file
->f_count
);
444 put_unused_fd(chan_fd
);
450 * lttng_session_ioctl - lttng session fd ioctl
456 * This ioctl implements lttng commands:
457 * LTTNG_KERNEL_CHANNEL
458 * Returns a LTTng channel file descriptor
459 * LTTNG_KERNEL_ENABLE
460 * Enables tracing for a session (weak enable)
461 * LTTNG_KERNEL_DISABLE
462 * Disables tracing for a session (strong disable)
463 * LTTNG_KERNEL_METADATA
464 * Returns a LTTng metadata file descriptor
465 * LTTNG_KERNEL_SESSION_TRACK_PID
466 * Add PID to session tracker
467 * LTTNG_KERNEL_SESSION_UNTRACK_PID
468 * Remove PID from session tracker
470 * The returned channel will be deleted when its file descriptor is closed.
473 long lttng_session_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
475 struct lttng_session
*session
= file
->private_data
;
478 case LTTNG_KERNEL_OLD_CHANNEL
:
480 struct lttng_kernel_channel chan_param
;
481 struct lttng_kernel_old_channel old_chan_param
;
483 if (copy_from_user(&old_chan_param
,
484 (struct lttng_kernel_old_channel __user
*) arg
,
485 sizeof(struct lttng_kernel_old_channel
)))
487 chan_param
.overwrite
= old_chan_param
.overwrite
;
488 chan_param
.subbuf_size
= old_chan_param
.subbuf_size
;
489 chan_param
.num_subbuf
= old_chan_param
.num_subbuf
;
490 chan_param
.switch_timer_interval
= old_chan_param
.switch_timer_interval
;
491 chan_param
.read_timer_interval
= old_chan_param
.read_timer_interval
;
492 chan_param
.output
= old_chan_param
.output
;
494 return lttng_abi_create_channel(file
, &chan_param
,
497 case LTTNG_KERNEL_CHANNEL
:
499 struct lttng_kernel_channel chan_param
;
501 if (copy_from_user(&chan_param
,
502 (struct lttng_kernel_channel __user
*) arg
,
503 sizeof(struct lttng_kernel_channel
)))
505 return lttng_abi_create_channel(file
, &chan_param
,
508 case LTTNG_KERNEL_OLD_SESSION_START
:
509 case LTTNG_KERNEL_OLD_ENABLE
:
510 case LTTNG_KERNEL_SESSION_START
:
511 case LTTNG_KERNEL_ENABLE
:
512 return lttng_session_enable(session
);
513 case LTTNG_KERNEL_OLD_SESSION_STOP
:
514 case LTTNG_KERNEL_OLD_DISABLE
:
515 case LTTNG_KERNEL_SESSION_STOP
:
516 case LTTNG_KERNEL_DISABLE
:
517 return lttng_session_disable(session
);
518 case LTTNG_KERNEL_OLD_METADATA
:
520 struct lttng_kernel_channel chan_param
;
521 struct lttng_kernel_old_channel old_chan_param
;
523 if (copy_from_user(&old_chan_param
,
524 (struct lttng_kernel_old_channel __user
*) arg
,
525 sizeof(struct lttng_kernel_old_channel
)))
527 chan_param
.overwrite
= old_chan_param
.overwrite
;
528 chan_param
.subbuf_size
= old_chan_param
.subbuf_size
;
529 chan_param
.num_subbuf
= old_chan_param
.num_subbuf
;
530 chan_param
.switch_timer_interval
= old_chan_param
.switch_timer_interval
;
531 chan_param
.read_timer_interval
= old_chan_param
.read_timer_interval
;
532 chan_param
.output
= old_chan_param
.output
;
534 return lttng_abi_create_channel(file
, &chan_param
,
537 case LTTNG_KERNEL_METADATA
:
539 struct lttng_kernel_channel chan_param
;
541 if (copy_from_user(&chan_param
,
542 (struct lttng_kernel_channel __user
*) arg
,
543 sizeof(struct lttng_kernel_channel
)))
545 return lttng_abi_create_channel(file
, &chan_param
,
548 case LTTNG_KERNEL_SESSION_TRACK_PID
:
549 return lttng_session_track_pid(session
, (int) arg
);
550 case LTTNG_KERNEL_SESSION_UNTRACK_PID
:
551 return lttng_session_untrack_pid(session
, (int) arg
);
552 case LTTNG_KERNEL_SESSION_LIST_TRACKER_PIDS
:
553 return lttng_session_list_tracker_pids(session
);
560 * Called when the last file reference is dropped.
562 * Big fat note: channels and events are invariant for the whole session after
563 * their creation. So this session destruction also destroys all channel and
564 * event structures specific to this session (they are not destroyed when their
565 * individual file is released).
568 int lttng_session_release(struct inode
*inode
, struct file
*file
)
570 struct lttng_session
*session
= file
->private_data
;
573 lttng_session_destroy(session
);
577 static const struct file_operations lttng_session_fops
= {
578 .owner
= THIS_MODULE
,
579 .release
= lttng_session_release
,
580 .unlocked_ioctl
= lttng_session_ioctl
,
582 .compat_ioctl
= lttng_session_ioctl
,
587 * lttng_metadata_ring_buffer_poll - LTTng ring buffer poll file operation
591 * Handles the poll operations for the metadata channels.
594 unsigned int lttng_metadata_ring_buffer_poll(struct file
*filp
,
597 struct lttng_metadata_stream
*stream
= filp
->private_data
;
598 struct lib_ring_buffer
*buf
= stream
->priv
;
600 unsigned int mask
= 0;
602 if (filp
->f_mode
& FMODE_READ
) {
603 poll_wait_set_exclusive(wait
);
604 poll_wait(filp
, &stream
->read_wait
, wait
);
606 finalized
= stream
->finalized
;
609 * lib_ring_buffer_is_finalized() contains a smp_rmb()
610 * ordering finalized load before offsets loads.
612 WARN_ON(atomic_long_read(&buf
->active_readers
) != 1);
617 mutex_lock(&stream
->metadata_cache
->lock
);
618 if (stream
->metadata_cache
->metadata_written
>
619 stream
->metadata_out
)
621 mutex_unlock(&stream
->metadata_cache
->lock
);
628 void lttng_metadata_ring_buffer_ioctl_put_next_subbuf(struct file
*filp
,
629 unsigned int cmd
, unsigned long arg
)
631 struct lttng_metadata_stream
*stream
= filp
->private_data
;
633 stream
->metadata_out
= stream
->metadata_in
;
637 long lttng_metadata_ring_buffer_ioctl(struct file
*filp
,
638 unsigned int cmd
, unsigned long arg
)
641 struct lttng_metadata_stream
*stream
= filp
->private_data
;
642 struct lib_ring_buffer
*buf
= stream
->priv
;
645 case RING_BUFFER_GET_NEXT_SUBBUF
:
647 struct lttng_metadata_stream
*stream
= filp
->private_data
;
648 struct lib_ring_buffer
*buf
= stream
->priv
;
649 struct channel
*chan
= buf
->backend
.chan
;
651 ret
= lttng_metadata_output_channel(stream
, chan
);
653 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
659 case RING_BUFFER_GET_SUBBUF
:
662 * Random access is not allowed for metadata channel.
666 case RING_BUFFER_FLUSH
:
668 struct lttng_metadata_stream
*stream
= filp
->private_data
;
669 struct lib_ring_buffer
*buf
= stream
->priv
;
670 struct channel
*chan
= buf
->backend
.chan
;
673 * Before doing the actual ring buffer flush, write up to one
674 * packet of metadata in the ring buffer.
676 ret
= lttng_metadata_output_channel(stream
, chan
);
684 /* PUT_SUBBUF is the one from lib ring buffer, unmodified. */
686 /* Performing lib ring buffer ioctl after our own. */
687 ret
= lib_ring_buffer_ioctl(filp
, cmd
, arg
, buf
);
692 case RING_BUFFER_PUT_NEXT_SUBBUF
:
694 lttng_metadata_ring_buffer_ioctl_put_next_subbuf(filp
,
707 long lttng_metadata_ring_buffer_compat_ioctl(struct file
*filp
,
708 unsigned int cmd
, unsigned long arg
)
711 struct lttng_metadata_stream
*stream
= filp
->private_data
;
712 struct lib_ring_buffer
*buf
= stream
->priv
;
715 case RING_BUFFER_GET_NEXT_SUBBUF
:
717 struct lttng_metadata_stream
*stream
= filp
->private_data
;
718 struct lib_ring_buffer
*buf
= stream
->priv
;
719 struct channel
*chan
= buf
->backend
.chan
;
721 ret
= lttng_metadata_output_channel(stream
, chan
);
723 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
729 case RING_BUFFER_GET_SUBBUF
:
732 * Random access is not allowed for metadata channel.
739 /* PUT_SUBBUF is the one from lib ring buffer, unmodified. */
741 /* Performing lib ring buffer ioctl after our own. */
742 ret
= lib_ring_buffer_compat_ioctl(filp
, cmd
, arg
, buf
);
747 case RING_BUFFER_PUT_NEXT_SUBBUF
:
749 lttng_metadata_ring_buffer_ioctl_put_next_subbuf(filp
,
762 * This is not used by anonymous file descriptors. This code is left
763 * there if we ever want to implement an inode with open() operation.
766 int lttng_metadata_ring_buffer_open(struct inode
*inode
, struct file
*file
)
768 struct lttng_metadata_stream
*stream
= inode
->i_private
;
769 struct lib_ring_buffer
*buf
= stream
->priv
;
771 file
->private_data
= buf
;
773 * Since life-time of metadata cache differs from that of
774 * session, we need to keep our own reference on the transport.
776 if (!try_module_get(stream
->transport
->owner
)) {
777 printk(KERN_WARNING
"LTT : Can't lock transport module.\n");
780 return lib_ring_buffer_open(inode
, file
, buf
);
784 int lttng_metadata_ring_buffer_release(struct inode
*inode
, struct file
*file
)
786 struct lttng_metadata_stream
*stream
= file
->private_data
;
787 struct lib_ring_buffer
*buf
= stream
->priv
;
789 kref_put(&stream
->metadata_cache
->refcount
, metadata_cache_destroy
);
790 module_put(stream
->transport
->owner
);
791 return lib_ring_buffer_release(inode
, file
, buf
);
795 ssize_t
lttng_metadata_ring_buffer_splice_read(struct file
*in
, loff_t
*ppos
,
796 struct pipe_inode_info
*pipe
, size_t len
,
799 struct lttng_metadata_stream
*stream
= in
->private_data
;
800 struct lib_ring_buffer
*buf
= stream
->priv
;
802 return lib_ring_buffer_splice_read(in
, ppos
, pipe
, len
,
807 int lttng_metadata_ring_buffer_mmap(struct file
*filp
,
808 struct vm_area_struct
*vma
)
810 struct lttng_metadata_stream
*stream
= filp
->private_data
;
811 struct lib_ring_buffer
*buf
= stream
->priv
;
813 return lib_ring_buffer_mmap(filp
, vma
, buf
);
817 const struct file_operations lttng_metadata_ring_buffer_file_operations
= {
818 .owner
= THIS_MODULE
,
819 .open
= lttng_metadata_ring_buffer_open
,
820 .release
= lttng_metadata_ring_buffer_release
,
821 .poll
= lttng_metadata_ring_buffer_poll
,
822 .splice_read
= lttng_metadata_ring_buffer_splice_read
,
823 .mmap
= lttng_metadata_ring_buffer_mmap
,
824 .unlocked_ioctl
= lttng_metadata_ring_buffer_ioctl
,
825 .llseek
= vfs_lib_ring_buffer_no_llseek
,
827 .compat_ioctl
= lttng_metadata_ring_buffer_compat_ioctl
,
832 int lttng_abi_create_stream_fd(struct file
*channel_file
, void *stream_priv
,
833 const struct file_operations
*fops
)
836 struct file
*stream_file
;
838 stream_fd
= lttng_get_unused_fd();
843 stream_file
= anon_inode_getfile("[lttng_stream]", fops
,
844 stream_priv
, O_RDWR
);
845 if (IS_ERR(stream_file
)) {
846 ret
= PTR_ERR(stream_file
);
850 * OPEN_FMODE, called within anon_inode_getfile/alloc_file, don't honor
851 * FMODE_LSEEK, FMODE_PREAD nor FMODE_PWRITE. We need to read from this
852 * file descriptor, so we set FMODE_PREAD here.
854 stream_file
->f_mode
|= FMODE_PREAD
;
855 fd_install(stream_fd
, stream_file
);
857 * The stream holds a reference to the channel within the generic ring
858 * buffer library, so no need to hold a refcount on the channel and
859 * session files here.
864 put_unused_fd(stream_fd
);
870 int lttng_abi_open_stream(struct file
*channel_file
)
872 struct lttng_channel
*channel
= channel_file
->private_data
;
873 struct lib_ring_buffer
*buf
;
877 buf
= channel
->ops
->buffer_read_open(channel
->chan
);
882 ret
= lttng_abi_create_stream_fd(channel_file
, stream_priv
,
883 <tng_stream_ring_buffer_file_operations
);
890 channel
->ops
->buffer_read_close(buf
);
895 int lttng_abi_open_metadata_stream(struct file
*channel_file
)
897 struct lttng_channel
*channel
= channel_file
->private_data
;
898 struct lttng_session
*session
= channel
->session
;
899 struct lib_ring_buffer
*buf
;
901 struct lttng_metadata_stream
*metadata_stream
;
904 buf
= channel
->ops
->buffer_read_open(channel
->chan
);
908 metadata_stream
= kzalloc(sizeof(struct lttng_metadata_stream
),
910 if (!metadata_stream
) {
914 metadata_stream
->metadata_cache
= session
->metadata_cache
;
915 init_waitqueue_head(&metadata_stream
->read_wait
);
916 metadata_stream
->priv
= buf
;
917 stream_priv
= metadata_stream
;
918 metadata_stream
->transport
= channel
->transport
;
921 * Since life-time of metadata cache differs from that of
922 * session, we need to keep our own reference on the transport.
924 if (!try_module_get(metadata_stream
->transport
->owner
)) {
925 printk(KERN_WARNING
"LTT : Can't lock transport module.\n");
930 ret
= lttng_abi_create_stream_fd(channel_file
, stream_priv
,
931 <tng_metadata_ring_buffer_file_operations
);
935 kref_get(&session
->metadata_cache
->refcount
);
936 list_add(&metadata_stream
->list
,
937 &session
->metadata_cache
->metadata_stream
);
941 module_put(metadata_stream
->transport
->owner
);
943 kfree(metadata_stream
);
945 channel
->ops
->buffer_read_close(buf
);
950 int lttng_abi_create_event(struct file
*channel_file
,
951 struct lttng_kernel_event
*event_param
)
953 struct lttng_channel
*channel
= channel_file
->private_data
;
955 struct file
*event_file
;
958 event_param
->name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
959 switch (event_param
->instrumentation
) {
960 case LTTNG_KERNEL_KRETPROBE
:
961 event_param
->u
.kretprobe
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
963 case LTTNG_KERNEL_KPROBE
:
964 event_param
->u
.kprobe
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
966 case LTTNG_KERNEL_FUNCTION
:
967 event_param
->u
.ftrace
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
972 event_fd
= lttng_get_unused_fd();
977 event_file
= anon_inode_getfile("[lttng_event]",
980 if (IS_ERR(event_file
)) {
981 ret
= PTR_ERR(event_file
);
984 if (event_param
->instrumentation
== LTTNG_KERNEL_TRACEPOINT
985 || event_param
->instrumentation
== LTTNG_KERNEL_SYSCALL
) {
986 struct lttng_enabler
*enabler
;
988 if (event_param
->name
[strlen(event_param
->name
) - 1] == '*') {
989 enabler
= lttng_enabler_create(LTTNG_ENABLER_WILDCARD
,
990 event_param
, channel
);
992 enabler
= lttng_enabler_create(LTTNG_ENABLER_NAME
,
993 event_param
, channel
);
997 struct lttng_event
*event
;
1000 * We tolerate no failure path after event creation. It
1001 * will stay invariant for the rest of the session.
1003 event
= lttng_event_create(channel
, event_param
,
1005 event_param
->instrumentation
);
1006 WARN_ON_ONCE(!event
);
1007 if (IS_ERR(event
)) {
1008 ret
= PTR_ERR(event
);
1013 event_file
->private_data
= priv
;
1014 fd_install(event_fd
, event_file
);
1015 /* The event holds a reference on the channel */
1016 atomic_long_inc(&channel_file
->f_count
);
1022 put_unused_fd(event_fd
);
1028 * lttng_channel_ioctl - lttng syscall through ioctl
1034 * This ioctl implements lttng commands:
1035 * LTTNG_KERNEL_STREAM
1036 * Returns an event stream file descriptor or failure.
1037 * (typically, one event stream records events from one CPU)
1038 * LTTNG_KERNEL_EVENT
1039 * Returns an event file descriptor or failure.
1040 * LTTNG_KERNEL_CONTEXT
1041 * Prepend a context field to each event in the channel
1042 * LTTNG_KERNEL_ENABLE
1043 * Enable recording for events in this channel (weak enable)
1044 * LTTNG_KERNEL_DISABLE
1045 * Disable recording for events in this channel (strong disable)
1047 * Channel and event file descriptors also hold a reference on the session.
1050 long lttng_channel_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1052 struct lttng_channel
*channel
= file
->private_data
;
1055 case LTTNG_KERNEL_OLD_STREAM
:
1056 case LTTNG_KERNEL_STREAM
:
1057 return lttng_abi_open_stream(file
);
1058 case LTTNG_KERNEL_OLD_EVENT
:
1060 struct lttng_kernel_event
*uevent_param
;
1061 struct lttng_kernel_old_event
*old_uevent_param
;
1064 uevent_param
= kmalloc(sizeof(struct lttng_kernel_event
),
1066 if (!uevent_param
) {
1070 old_uevent_param
= kmalloc(
1071 sizeof(struct lttng_kernel_old_event
),
1073 if (!old_uevent_param
) {
1075 goto old_event_error_free_param
;
1077 if (copy_from_user(old_uevent_param
,
1078 (struct lttng_kernel_old_event __user
*) arg
,
1079 sizeof(struct lttng_kernel_old_event
))) {
1081 goto old_event_error_free_old_param
;
1084 memcpy(uevent_param
->name
, old_uevent_param
->name
,
1085 sizeof(uevent_param
->name
));
1086 uevent_param
->instrumentation
=
1087 old_uevent_param
->instrumentation
;
1089 switch (old_uevent_param
->instrumentation
) {
1090 case LTTNG_KERNEL_KPROBE
:
1091 uevent_param
->u
.kprobe
.addr
=
1092 old_uevent_param
->u
.kprobe
.addr
;
1093 uevent_param
->u
.kprobe
.offset
=
1094 old_uevent_param
->u
.kprobe
.offset
;
1095 memcpy(uevent_param
->u
.kprobe
.symbol_name
,
1096 old_uevent_param
->u
.kprobe
.symbol_name
,
1097 sizeof(uevent_param
->u
.kprobe
.symbol_name
));
1099 case LTTNG_KERNEL_KRETPROBE
:
1100 uevent_param
->u
.kretprobe
.addr
=
1101 old_uevent_param
->u
.kretprobe
.addr
;
1102 uevent_param
->u
.kretprobe
.offset
=
1103 old_uevent_param
->u
.kretprobe
.offset
;
1104 memcpy(uevent_param
->u
.kretprobe
.symbol_name
,
1105 old_uevent_param
->u
.kretprobe
.symbol_name
,
1106 sizeof(uevent_param
->u
.kretprobe
.symbol_name
));
1108 case LTTNG_KERNEL_FUNCTION
:
1109 memcpy(uevent_param
->u
.ftrace
.symbol_name
,
1110 old_uevent_param
->u
.ftrace
.symbol_name
,
1111 sizeof(uevent_param
->u
.ftrace
.symbol_name
));
1116 ret
= lttng_abi_create_event(file
, uevent_param
);
1118 old_event_error_free_old_param
:
1119 kfree(old_uevent_param
);
1120 old_event_error_free_param
:
1121 kfree(uevent_param
);
1125 case LTTNG_KERNEL_EVENT
:
1127 struct lttng_kernel_event uevent_param
;
1129 if (copy_from_user(&uevent_param
,
1130 (struct lttng_kernel_event __user
*) arg
,
1131 sizeof(uevent_param
)))
1133 return lttng_abi_create_event(file
, &uevent_param
);
1135 case LTTNG_KERNEL_OLD_CONTEXT
:
1137 struct lttng_kernel_context
*ucontext_param
;
1138 struct lttng_kernel_old_context
*old_ucontext_param
;
1141 ucontext_param
= kmalloc(sizeof(struct lttng_kernel_context
),
1143 if (!ucontext_param
) {
1147 old_ucontext_param
= kmalloc(sizeof(struct lttng_kernel_old_context
),
1149 if (!old_ucontext_param
) {
1151 goto old_ctx_error_free_param
;
1154 if (copy_from_user(old_ucontext_param
,
1155 (struct lttng_kernel_old_context __user
*) arg
,
1156 sizeof(struct lttng_kernel_old_context
))) {
1158 goto old_ctx_error_free_old_param
;
1160 ucontext_param
->ctx
= old_ucontext_param
->ctx
;
1161 memcpy(ucontext_param
->padding
, old_ucontext_param
->padding
,
1162 sizeof(ucontext_param
->padding
));
1163 /* only type that uses the union */
1164 if (old_ucontext_param
->ctx
== LTTNG_KERNEL_CONTEXT_PERF_COUNTER
) {
1165 ucontext_param
->u
.perf_counter
.type
=
1166 old_ucontext_param
->u
.perf_counter
.type
;
1167 ucontext_param
->u
.perf_counter
.config
=
1168 old_ucontext_param
->u
.perf_counter
.config
;
1169 memcpy(ucontext_param
->u
.perf_counter
.name
,
1170 old_ucontext_param
->u
.perf_counter
.name
,
1171 sizeof(ucontext_param
->u
.perf_counter
.name
));
1174 ret
= lttng_abi_add_context(file
,
1176 &channel
->ctx
, channel
->session
);
1178 old_ctx_error_free_old_param
:
1179 kfree(old_ucontext_param
);
1180 old_ctx_error_free_param
:
1181 kfree(ucontext_param
);
1185 case LTTNG_KERNEL_CONTEXT
:
1187 struct lttng_kernel_context ucontext_param
;
1189 if (copy_from_user(&ucontext_param
,
1190 (struct lttng_kernel_context __user
*) arg
,
1191 sizeof(ucontext_param
)))
1193 return lttng_abi_add_context(file
,
1195 &channel
->ctx
, channel
->session
);
1197 case LTTNG_KERNEL_OLD_ENABLE
:
1198 case LTTNG_KERNEL_ENABLE
:
1199 return lttng_channel_enable(channel
);
1200 case LTTNG_KERNEL_OLD_DISABLE
:
1201 case LTTNG_KERNEL_DISABLE
:
1202 return lttng_channel_disable(channel
);
1203 case LTTNG_KERNEL_SYSCALL_MASK
:
1204 return lttng_channel_syscall_mask(channel
,
1205 (struct lttng_kernel_syscall_mask __user
*) arg
);
1207 return -ENOIOCTLCMD
;
1213 * lttng_metadata_ioctl - lttng syscall through ioctl
1219 * This ioctl implements lttng commands:
1220 * LTTNG_KERNEL_STREAM
1221 * Returns an event stream file descriptor or failure.
1223 * Channel and event file descriptors also hold a reference on the session.
1226 long lttng_metadata_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1229 case LTTNG_KERNEL_OLD_STREAM
:
1230 case LTTNG_KERNEL_STREAM
:
1231 return lttng_abi_open_metadata_stream(file
);
1233 return -ENOIOCTLCMD
;
1238 * lttng_channel_poll - lttng stream addition/removal monitoring
1243 unsigned int lttng_channel_poll(struct file
*file
, poll_table
*wait
)
1245 struct lttng_channel
*channel
= file
->private_data
;
1246 unsigned int mask
= 0;
1248 if (file
->f_mode
& FMODE_READ
) {
1249 poll_wait_set_exclusive(wait
);
1250 poll_wait(file
, channel
->ops
->get_hp_wait_queue(channel
->chan
),
1253 if (channel
->ops
->is_disabled(channel
->chan
))
1255 if (channel
->ops
->is_finalized(channel
->chan
))
1257 if (channel
->ops
->buffer_has_read_closed_stream(channel
->chan
))
1258 return POLLIN
| POLLRDNORM
;
1266 int lttng_channel_release(struct inode
*inode
, struct file
*file
)
1268 struct lttng_channel
*channel
= file
->private_data
;
1271 fput(channel
->session
->file
);
1276 int lttng_metadata_channel_release(struct inode
*inode
, struct file
*file
)
1278 struct lttng_channel
*channel
= file
->private_data
;
1281 fput(channel
->session
->file
);
1282 lttng_metadata_channel_destroy(channel
);
1288 static const struct file_operations lttng_channel_fops
= {
1289 .owner
= THIS_MODULE
,
1290 .release
= lttng_channel_release
,
1291 .poll
= lttng_channel_poll
,
1292 .unlocked_ioctl
= lttng_channel_ioctl
,
1293 #ifdef CONFIG_COMPAT
1294 .compat_ioctl
= lttng_channel_ioctl
,
1298 static const struct file_operations lttng_metadata_fops
= {
1299 .owner
= THIS_MODULE
,
1300 .release
= lttng_metadata_channel_release
,
1301 .unlocked_ioctl
= lttng_metadata_ioctl
,
1302 #ifdef CONFIG_COMPAT
1303 .compat_ioctl
= lttng_metadata_ioctl
,
1308 * lttng_event_ioctl - lttng syscall through ioctl
1314 * This ioctl implements lttng commands:
1315 * LTTNG_KERNEL_CONTEXT
1316 * Prepend a context field to each record of this event
1317 * LTTNG_KERNEL_ENABLE
1318 * Enable recording for this event (weak enable)
1319 * LTTNG_KERNEL_DISABLE
1320 * Disable recording for this event (strong disable)
1323 long lttng_event_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1325 struct lttng_event
*event
;
1326 struct lttng_enabler
*enabler
;
1327 enum lttng_event_type
*evtype
= file
->private_data
;
1330 case LTTNG_KERNEL_OLD_CONTEXT
:
1332 /* Not implemented */
1335 case LTTNG_KERNEL_CONTEXT
:
1337 /* Not implemented */
1340 case LTTNG_KERNEL_OLD_ENABLE
:
1341 case LTTNG_KERNEL_ENABLE
:
1343 case LTTNG_TYPE_EVENT
:
1344 event
= file
->private_data
;
1345 return lttng_event_enable(event
);
1346 case LTTNG_TYPE_ENABLER
:
1347 enabler
= file
->private_data
;
1348 return lttng_enabler_enable(enabler
);
1353 case LTTNG_KERNEL_OLD_DISABLE
:
1354 case LTTNG_KERNEL_DISABLE
:
1356 case LTTNG_TYPE_EVENT
:
1357 event
= file
->private_data
;
1358 return lttng_event_disable(event
);
1359 case LTTNG_TYPE_ENABLER
:
1360 enabler
= file
->private_data
;
1361 return lttng_enabler_disable(enabler
);
1366 case LTTNG_KERNEL_FILTER
:
1368 case LTTNG_TYPE_EVENT
:
1370 case LTTNG_TYPE_ENABLER
:
1372 enabler
= file
->private_data
;
1373 return lttng_enabler_attach_bytecode(enabler
,
1374 (struct lttng_kernel_filter_bytecode __user
*) arg
);
1379 return -ENOIOCTLCMD
;
1384 int lttng_event_release(struct inode
*inode
, struct file
*file
)
1386 struct lttng_event
*event
;
1387 struct lttng_enabler
*enabler
;
1388 enum lttng_event_type
*evtype
= file
->private_data
;
1394 case LTTNG_TYPE_EVENT
:
1395 event
= file
->private_data
;
1397 fput(event
->chan
->file
);
1399 case LTTNG_TYPE_ENABLER
:
1400 enabler
= file
->private_data
;
1402 fput(enabler
->chan
->file
);
1412 /* TODO: filter control ioctl */
1413 static const struct file_operations lttng_event_fops
= {
1414 .owner
= THIS_MODULE
,
1415 .release
= lttng_event_release
,
1416 .unlocked_ioctl
= lttng_event_ioctl
,
1417 #ifdef CONFIG_COMPAT
1418 .compat_ioctl
= lttng_event_ioctl
,
1422 static int put_u64(uint64_t val
, unsigned long arg
)
1424 return put_user(val
, (uint64_t __user
*) arg
);
1427 static long lttng_stream_ring_buffer_ioctl(struct file
*filp
,
1428 unsigned int cmd
, unsigned long arg
)
1430 struct lib_ring_buffer
*buf
= filp
->private_data
;
1431 struct channel
*chan
= buf
->backend
.chan
;
1432 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1433 const struct lttng_channel_ops
*ops
= chan
->backend
.priv_ops
;
1436 if (atomic_read(&chan
->record_disabled
))
1440 case LTTNG_RING_BUFFER_GET_TIMESTAMP_BEGIN
:
1444 ret
= ops
->timestamp_begin(config
, buf
, &ts
);
1447 return put_u64(ts
, arg
);
1449 case LTTNG_RING_BUFFER_GET_TIMESTAMP_END
:
1453 ret
= ops
->timestamp_end(config
, buf
, &ts
);
1456 return put_u64(ts
, arg
);
1458 case LTTNG_RING_BUFFER_GET_EVENTS_DISCARDED
:
1462 ret
= ops
->events_discarded(config
, buf
, &ed
);
1465 return put_u64(ed
, arg
);
1467 case LTTNG_RING_BUFFER_GET_CONTENT_SIZE
:
1471 ret
= ops
->content_size(config
, buf
, &cs
);
1474 return put_u64(cs
, arg
);
1476 case LTTNG_RING_BUFFER_GET_PACKET_SIZE
:
1480 ret
= ops
->packet_size(config
, buf
, &ps
);
1483 return put_u64(ps
, arg
);
1485 case LTTNG_RING_BUFFER_GET_STREAM_ID
:
1489 ret
= ops
->stream_id(config
, buf
, &si
);
1492 return put_u64(si
, arg
);
1494 case LTTNG_RING_BUFFER_GET_CURRENT_TIMESTAMP
:
1498 ret
= ops
->current_timestamp(config
, buf
, &ts
);
1501 return put_u64(ts
, arg
);
1504 return lib_ring_buffer_file_operations
.unlocked_ioctl(filp
,
1512 #ifdef CONFIG_COMPAT
1513 static long lttng_stream_ring_buffer_compat_ioctl(struct file
*filp
,
1514 unsigned int cmd
, unsigned long arg
)
1516 struct lib_ring_buffer
*buf
= filp
->private_data
;
1517 struct channel
*chan
= buf
->backend
.chan
;
1518 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1519 const struct lttng_channel_ops
*ops
= chan
->backend
.priv_ops
;
1522 if (atomic_read(&chan
->record_disabled
))
1526 case LTTNG_RING_BUFFER_COMPAT_GET_TIMESTAMP_BEGIN
:
1530 ret
= ops
->timestamp_begin(config
, buf
, &ts
);
1533 return put_u64(ts
, arg
);
1535 case LTTNG_RING_BUFFER_COMPAT_GET_TIMESTAMP_END
:
1539 ret
= ops
->timestamp_end(config
, buf
, &ts
);
1542 return put_u64(ts
, arg
);
1544 case LTTNG_RING_BUFFER_COMPAT_GET_EVENTS_DISCARDED
:
1548 ret
= ops
->events_discarded(config
, buf
, &ed
);
1551 return put_u64(ed
, arg
);
1553 case LTTNG_RING_BUFFER_COMPAT_GET_CONTENT_SIZE
:
1557 ret
= ops
->content_size(config
, buf
, &cs
);
1560 return put_u64(cs
, arg
);
1562 case LTTNG_RING_BUFFER_COMPAT_GET_PACKET_SIZE
:
1566 ret
= ops
->packet_size(config
, buf
, &ps
);
1569 return put_u64(ps
, arg
);
1571 case LTTNG_RING_BUFFER_COMPAT_GET_STREAM_ID
:
1575 ret
= ops
->stream_id(config
, buf
, &si
);
1578 return put_u64(si
, arg
);
1580 case LTTNG_RING_BUFFER_GET_CURRENT_TIMESTAMP
:
1584 ret
= ops
->current_timestamp(config
, buf
, &ts
);
1587 return put_u64(ts
, arg
);
1590 return lib_ring_buffer_file_operations
.compat_ioctl(filp
,
1597 #endif /* CONFIG_COMPAT */
1599 static void lttng_stream_override_ring_buffer_fops(void)
1601 lttng_stream_ring_buffer_file_operations
.owner
= THIS_MODULE
;
1602 lttng_stream_ring_buffer_file_operations
.open
=
1603 lib_ring_buffer_file_operations
.open
;
1604 lttng_stream_ring_buffer_file_operations
.release
=
1605 lib_ring_buffer_file_operations
.release
;
1606 lttng_stream_ring_buffer_file_operations
.poll
=
1607 lib_ring_buffer_file_operations
.poll
;
1608 lttng_stream_ring_buffer_file_operations
.splice_read
=
1609 lib_ring_buffer_file_operations
.splice_read
;
1610 lttng_stream_ring_buffer_file_operations
.mmap
=
1611 lib_ring_buffer_file_operations
.mmap
;
1612 lttng_stream_ring_buffer_file_operations
.unlocked_ioctl
=
1613 lttng_stream_ring_buffer_ioctl
;
1614 lttng_stream_ring_buffer_file_operations
.llseek
=
1615 lib_ring_buffer_file_operations
.llseek
;
1616 #ifdef CONFIG_COMPAT
1617 lttng_stream_ring_buffer_file_operations
.compat_ioctl
=
1618 lttng_stream_ring_buffer_compat_ioctl
;
1622 int __init
lttng_abi_init(void)
1626 wrapper_vmalloc_sync_all();
1627 lttng_proc_dentry
= proc_create_data("lttng", S_IRUSR
| S_IWUSR
, NULL
,
1630 if (!lttng_proc_dentry
) {
1631 printk(KERN_ERR
"Error creating LTTng control file\n");
1635 lttng_stream_override_ring_buffer_fops();
1641 /* No __exit annotation because used by init error path too. */
1642 void lttng_abi_exit(void)
1644 if (lttng_proc_dentry
)
1645 remove_proc_entry("lttng", NULL
);