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 "lttng-abi.h"
53 #include "lttng-abi-old.h"
54 #include "lttng-events.h"
55 #include "lttng-tracer.h"
56 #include "lib/ringbuffer/frontend_types.h"
59 * This is LTTng's own personal way to create a system call as an external
60 * module. We use ioctl() on /proc/lttng.
63 static struct proc_dir_entry
*lttng_proc_dentry
;
64 static const struct file_operations lttng_fops
;
65 static const struct file_operations lttng_session_fops
;
66 static const struct file_operations lttng_channel_fops
;
67 static const struct file_operations lttng_metadata_fops
;
68 static const struct file_operations lttng_event_fops
;
69 static struct file_operations lttng_stream_ring_buffer_file_operations
;
72 * Teardown management: opened file descriptors keep a refcount on the module,
73 * so it can only exit when all file descriptors are closed.
77 int lttng_abi_create_session(void)
79 struct lttng_session
*session
;
80 struct file
*session_file
;
83 session
= lttng_session_create();
86 session_fd
= get_unused_fd();
91 session_file
= anon_inode_getfile("[lttng_session]",
94 if (IS_ERR(session_file
)) {
95 ret
= PTR_ERR(session_file
);
98 session
->file
= session_file
;
99 fd_install(session_fd
, session_file
);
103 put_unused_fd(session_fd
);
105 lttng_session_destroy(session
);
110 int lttng_abi_tracepoint_list(void)
112 struct file
*tracepoint_list_file
;
115 file_fd
= get_unused_fd();
121 tracepoint_list_file
= anon_inode_getfile("[lttng_tracepoint_list]",
122 <tng_tracepoint_list_fops
,
124 if (IS_ERR(tracepoint_list_file
)) {
125 ret
= PTR_ERR(tracepoint_list_file
);
128 ret
= lttng_tracepoint_list_fops
.open(NULL
, tracepoint_list_file
);
131 fd_install(file_fd
, tracepoint_list_file
);
139 fput(tracepoint_list_file
);
141 put_unused_fd(file_fd
);
147 int lttng_abi_syscall_list(void)
149 struct file
*syscall_list_file
;
152 file_fd
= get_unused_fd();
158 syscall_list_file
= anon_inode_getfile("[lttng_syscall_list]",
159 <tng_syscall_list_fops
,
161 if (IS_ERR(syscall_list_file
)) {
162 ret
= PTR_ERR(syscall_list_file
);
165 ret
= lttng_syscall_list_fops
.open(NULL
, syscall_list_file
);
168 fd_install(file_fd
, syscall_list_file
);
176 fput(syscall_list_file
);
178 put_unused_fd(file_fd
);
184 void lttng_abi_tracer_version(struct lttng_kernel_tracer_version
*v
)
186 v
->major
= LTTNG_MODULES_MAJOR_VERSION
;
187 v
->minor
= LTTNG_MODULES_MINOR_VERSION
;
188 v
->patchlevel
= LTTNG_MODULES_PATCHLEVEL_VERSION
;
192 void lttng_abi_tracer_abi_version(struct lttng_kernel_tracer_abi_version
*v
)
194 v
->major
= LTTNG_MODULES_ABI_MAJOR_VERSION
;
195 v
->minor
= LTTNG_MODULES_ABI_MINOR_VERSION
;
199 long lttng_abi_add_context(struct file
*file
,
200 struct lttng_kernel_context
*context_param
,
201 struct lttng_ctx
**ctx
, struct lttng_session
*session
)
204 if (session
->been_active
)
207 switch (context_param
->ctx
) {
208 case LTTNG_KERNEL_CONTEXT_PID
:
209 return lttng_add_pid_to_ctx(ctx
);
210 case LTTNG_KERNEL_CONTEXT_PRIO
:
211 return lttng_add_prio_to_ctx(ctx
);
212 case LTTNG_KERNEL_CONTEXT_NICE
:
213 return lttng_add_nice_to_ctx(ctx
);
214 case LTTNG_KERNEL_CONTEXT_VPID
:
215 return lttng_add_vpid_to_ctx(ctx
);
216 case LTTNG_KERNEL_CONTEXT_TID
:
217 return lttng_add_tid_to_ctx(ctx
);
218 case LTTNG_KERNEL_CONTEXT_VTID
:
219 return lttng_add_vtid_to_ctx(ctx
);
220 case LTTNG_KERNEL_CONTEXT_PPID
:
221 return lttng_add_ppid_to_ctx(ctx
);
222 case LTTNG_KERNEL_CONTEXT_VPPID
:
223 return lttng_add_vppid_to_ctx(ctx
);
224 case LTTNG_KERNEL_CONTEXT_PERF_COUNTER
:
225 context_param
->u
.perf_counter
.name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
226 return lttng_add_perf_counter_to_ctx(context_param
->u
.perf_counter
.type
,
227 context_param
->u
.perf_counter
.config
,
228 context_param
->u
.perf_counter
.name
,
230 case LTTNG_KERNEL_CONTEXT_PROCNAME
:
231 return lttng_add_procname_to_ctx(ctx
);
232 case LTTNG_KERNEL_CONTEXT_HOSTNAME
:
233 return lttng_add_hostname_to_ctx(ctx
);
240 * lttng_ioctl - lttng syscall through ioctl
246 * This ioctl implements lttng commands:
247 * LTTNG_KERNEL_SESSION
248 * Returns a LTTng trace session file descriptor
249 * LTTNG_KERNEL_TRACER_VERSION
250 * Returns the LTTng kernel tracer version
251 * LTTNG_KERNEL_TRACEPOINT_LIST
252 * Returns a file descriptor listing available tracepoints
253 * LTTNG_KERNEL_WAIT_QUIESCENT
254 * Returns after all previously running probes have completed
255 * LTTNG_KERNEL_TRACER_ABI_VERSION
256 * Returns the LTTng kernel tracer ABI version
258 * The returned session will be deleted when its file descriptor is closed.
261 long lttng_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
264 case LTTNG_KERNEL_OLD_SESSION
:
265 case LTTNG_KERNEL_SESSION
:
266 return lttng_abi_create_session();
267 case LTTNG_KERNEL_OLD_TRACER_VERSION
:
269 struct lttng_kernel_tracer_version v
;
270 struct lttng_kernel_old_tracer_version oldv
;
271 struct lttng_kernel_old_tracer_version
*uversion
=
272 (struct lttng_kernel_old_tracer_version __user
*) arg
;
274 lttng_abi_tracer_version(&v
);
275 oldv
.major
= v
.major
;
276 oldv
.minor
= v
.minor
;
277 oldv
.patchlevel
= v
.patchlevel
;
279 if (copy_to_user(uversion
, &oldv
, sizeof(oldv
)))
283 case LTTNG_KERNEL_TRACER_VERSION
:
285 struct lttng_kernel_tracer_version version
;
286 struct lttng_kernel_tracer_version
*uversion
=
287 (struct lttng_kernel_tracer_version __user
*) arg
;
289 lttng_abi_tracer_version(&version
);
291 if (copy_to_user(uversion
, &version
, sizeof(version
)))
295 case LTTNG_KERNEL_TRACER_ABI_VERSION
:
297 struct lttng_kernel_tracer_abi_version version
;
298 struct lttng_kernel_tracer_abi_version
*uversion
=
299 (struct lttng_kernel_tracer_abi_version __user
*) arg
;
301 lttng_abi_tracer_abi_version(&version
);
303 if (copy_to_user(uversion
, &version
, sizeof(version
)))
307 case LTTNG_KERNEL_OLD_TRACEPOINT_LIST
:
308 case LTTNG_KERNEL_TRACEPOINT_LIST
:
309 return lttng_abi_tracepoint_list();
310 case LTTNG_KERNEL_SYSCALL_LIST
:
311 return lttng_abi_syscall_list();
312 case LTTNG_KERNEL_OLD_WAIT_QUIESCENT
:
313 case LTTNG_KERNEL_WAIT_QUIESCENT
:
316 case LTTNG_KERNEL_OLD_CALIBRATE
:
318 struct lttng_kernel_old_calibrate __user
*ucalibrate
=
319 (struct lttng_kernel_old_calibrate __user
*) arg
;
320 struct lttng_kernel_old_calibrate old_calibrate
;
321 struct lttng_kernel_calibrate calibrate
;
324 if (copy_from_user(&old_calibrate
, ucalibrate
, sizeof(old_calibrate
)))
326 calibrate
.type
= old_calibrate
.type
;
327 ret
= lttng_calibrate(&calibrate
);
328 if (copy_to_user(ucalibrate
, &old_calibrate
, sizeof(old_calibrate
)))
332 case LTTNG_KERNEL_CALIBRATE
:
334 struct lttng_kernel_calibrate __user
*ucalibrate
=
335 (struct lttng_kernel_calibrate __user
*) arg
;
336 struct lttng_kernel_calibrate calibrate
;
339 if (copy_from_user(&calibrate
, ucalibrate
, sizeof(calibrate
)))
341 ret
= lttng_calibrate(&calibrate
);
342 if (copy_to_user(ucalibrate
, &calibrate
, sizeof(calibrate
)))
351 static const struct file_operations lttng_fops
= {
352 .owner
= THIS_MODULE
,
353 .unlocked_ioctl
= lttng_ioctl
,
355 .compat_ioctl
= lttng_ioctl
,
360 int lttng_abi_create_channel(struct file
*session_file
,
361 struct lttng_kernel_channel
*chan_param
,
362 enum channel_type channel_type
)
364 struct lttng_session
*session
= session_file
->private_data
;
365 const struct file_operations
*fops
= NULL
;
366 const char *transport_name
;
367 struct lttng_channel
*chan
;
368 struct file
*chan_file
;
372 chan_fd
= get_unused_fd();
377 switch (channel_type
) {
378 case PER_CPU_CHANNEL
:
379 fops
= <tng_channel_fops
;
381 case METADATA_CHANNEL
:
382 fops
= <tng_metadata_fops
;
386 chan_file
= anon_inode_getfile("[lttng_channel]",
389 if (IS_ERR(chan_file
)) {
390 ret
= PTR_ERR(chan_file
);
393 switch (channel_type
) {
394 case PER_CPU_CHANNEL
:
395 if (chan_param
->output
== LTTNG_KERNEL_SPLICE
) {
396 transport_name
= chan_param
->overwrite
?
397 "relay-overwrite" : "relay-discard";
398 } else if (chan_param
->output
== LTTNG_KERNEL_MMAP
) {
399 transport_name
= chan_param
->overwrite
?
400 "relay-overwrite-mmap" : "relay-discard-mmap";
405 case METADATA_CHANNEL
:
406 if (chan_param
->output
== LTTNG_KERNEL_SPLICE
)
407 transport_name
= "relay-metadata";
408 else if (chan_param
->output
== LTTNG_KERNEL_MMAP
)
409 transport_name
= "relay-metadata-mmap";
414 transport_name
= "<unknown>";
418 * We tolerate no failure path after channel creation. It will stay
419 * invariant for the rest of the session.
421 chan
= lttng_channel_create(session
, transport_name
, NULL
,
422 chan_param
->subbuf_size
,
423 chan_param
->num_subbuf
,
424 chan_param
->switch_timer_interval
,
425 chan_param
->read_timer_interval
,
431 chan
->file
= chan_file
;
432 chan_file
->private_data
= chan
;
433 fd_install(chan_fd
, chan_file
);
434 atomic_long_inc(&session_file
->f_count
);
441 put_unused_fd(chan_fd
);
447 * lttng_session_ioctl - lttng session fd ioctl
453 * This ioctl implements lttng commands:
454 * LTTNG_KERNEL_CHANNEL
455 * Returns a LTTng channel file descriptor
456 * LTTNG_KERNEL_ENABLE
457 * Enables tracing for a session (weak enable)
458 * LTTNG_KERNEL_DISABLE
459 * Disables tracing for a session (strong disable)
460 * LTTNG_KERNEL_METADATA
461 * Returns a LTTng metadata file descriptor
463 * The returned channel will be deleted when its file descriptor is closed.
466 long lttng_session_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
468 struct lttng_session
*session
= file
->private_data
;
471 case LTTNG_KERNEL_OLD_CHANNEL
:
473 struct lttng_kernel_channel chan_param
;
474 struct lttng_kernel_old_channel old_chan_param
;
476 if (copy_from_user(&old_chan_param
,
477 (struct lttng_kernel_old_channel __user
*) arg
,
478 sizeof(struct lttng_kernel_old_channel
)))
480 chan_param
.overwrite
= old_chan_param
.overwrite
;
481 chan_param
.subbuf_size
= old_chan_param
.subbuf_size
;
482 chan_param
.num_subbuf
= old_chan_param
.num_subbuf
;
483 chan_param
.switch_timer_interval
= old_chan_param
.switch_timer_interval
;
484 chan_param
.read_timer_interval
= old_chan_param
.read_timer_interval
;
485 chan_param
.output
= old_chan_param
.output
;
487 return lttng_abi_create_channel(file
, &chan_param
,
490 case LTTNG_KERNEL_CHANNEL
:
492 struct lttng_kernel_channel chan_param
;
494 if (copy_from_user(&chan_param
,
495 (struct lttng_kernel_channel __user
*) arg
,
496 sizeof(struct lttng_kernel_channel
)))
498 return lttng_abi_create_channel(file
, &chan_param
,
501 case LTTNG_KERNEL_OLD_SESSION_START
:
502 case LTTNG_KERNEL_OLD_ENABLE
:
503 case LTTNG_KERNEL_SESSION_START
:
504 case LTTNG_KERNEL_ENABLE
:
505 return lttng_session_enable(session
);
506 case LTTNG_KERNEL_OLD_SESSION_STOP
:
507 case LTTNG_KERNEL_OLD_DISABLE
:
508 case LTTNG_KERNEL_SESSION_STOP
:
509 case LTTNG_KERNEL_DISABLE
:
510 return lttng_session_disable(session
);
511 case LTTNG_KERNEL_OLD_METADATA
:
513 struct lttng_kernel_channel chan_param
;
514 struct lttng_kernel_old_channel old_chan_param
;
516 if (copy_from_user(&old_chan_param
,
517 (struct lttng_kernel_old_channel __user
*) arg
,
518 sizeof(struct lttng_kernel_old_channel
)))
520 chan_param
.overwrite
= old_chan_param
.overwrite
;
521 chan_param
.subbuf_size
= old_chan_param
.subbuf_size
;
522 chan_param
.num_subbuf
= old_chan_param
.num_subbuf
;
523 chan_param
.switch_timer_interval
= old_chan_param
.switch_timer_interval
;
524 chan_param
.read_timer_interval
= old_chan_param
.read_timer_interval
;
525 chan_param
.output
= old_chan_param
.output
;
527 return lttng_abi_create_channel(file
, &chan_param
,
530 case LTTNG_KERNEL_METADATA
:
532 struct lttng_kernel_channel chan_param
;
534 if (copy_from_user(&chan_param
,
535 (struct lttng_kernel_channel __user
*) arg
,
536 sizeof(struct lttng_kernel_channel
)))
538 return lttng_abi_create_channel(file
, &chan_param
,
547 * Called when the last file reference is dropped.
549 * Big fat note: channels and events are invariant for the whole session after
550 * their creation. So this session destruction also destroys all channel and
551 * event structures specific to this session (they are not destroyed when their
552 * individual file is released).
555 int lttng_session_release(struct inode
*inode
, struct file
*file
)
557 struct lttng_session
*session
= file
->private_data
;
560 lttng_session_destroy(session
);
564 static const struct file_operations lttng_session_fops
= {
565 .owner
= THIS_MODULE
,
566 .release
= lttng_session_release
,
567 .unlocked_ioctl
= lttng_session_ioctl
,
569 .compat_ioctl
= lttng_session_ioctl
,
574 * lttng_metadata_ring_buffer_poll - LTTng ring buffer poll file operation
578 * Handles the poll operations for the metadata channels.
581 unsigned int lttng_metadata_ring_buffer_poll(struct file
*filp
,
584 struct lttng_metadata_stream
*stream
= filp
->private_data
;
585 struct lib_ring_buffer
*buf
= stream
->priv
;
587 unsigned int mask
= 0;
589 if (filp
->f_mode
& FMODE_READ
) {
590 poll_wait_set_exclusive(wait
);
591 poll_wait(filp
, &stream
->read_wait
, wait
);
593 finalized
= stream
->finalized
;
596 * lib_ring_buffer_is_finalized() contains a smp_rmb()
597 * ordering finalized load before offsets loads.
599 WARN_ON(atomic_long_read(&buf
->active_readers
) != 1);
604 if (stream
->metadata_cache
->metadata_written
>
605 stream
->metadata_out
)
613 void lttng_metadata_ring_buffer_ioctl_put_next_subbuf(struct file
*filp
,
614 unsigned int cmd
, unsigned long arg
)
616 struct lttng_metadata_stream
*stream
= filp
->private_data
;
618 stream
->metadata_out
= stream
->metadata_in
;
622 long lttng_metadata_ring_buffer_ioctl(struct file
*filp
,
623 unsigned int cmd
, unsigned long arg
)
626 struct lttng_metadata_stream
*stream
= filp
->private_data
;
627 struct lib_ring_buffer
*buf
= stream
->priv
;
630 case RING_BUFFER_GET_NEXT_SUBBUF
:
632 struct lttng_metadata_stream
*stream
= filp
->private_data
;
633 struct lib_ring_buffer
*buf
= stream
->priv
;
634 struct channel
*chan
= buf
->backend
.chan
;
636 ret
= lttng_metadata_output_channel(stream
, chan
);
638 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
644 case RING_BUFFER_GET_SUBBUF
:
647 * Random access is not allowed for metadata channel.
651 case RING_BUFFER_FLUSH
:
653 struct lttng_metadata_stream
*stream
= filp
->private_data
;
654 struct lib_ring_buffer
*buf
= stream
->priv
;
655 struct channel
*chan
= buf
->backend
.chan
;
658 * Before doing the actual ring buffer flush, write up to one
659 * packet of metadata in the ring buffer.
661 ret
= lttng_metadata_output_channel(stream
, chan
);
669 /* PUT_SUBBUF is the one from lib ring buffer, unmodified. */
671 /* Performing lib ring buffer ioctl after our own. */
672 ret
= lib_ring_buffer_ioctl(filp
, cmd
, arg
, buf
);
677 case RING_BUFFER_PUT_NEXT_SUBBUF
:
679 lttng_metadata_ring_buffer_ioctl_put_next_subbuf(filp
,
692 long lttng_metadata_ring_buffer_compat_ioctl(struct file
*filp
,
693 unsigned int cmd
, unsigned long arg
)
696 struct lttng_metadata_stream
*stream
= filp
->private_data
;
697 struct lib_ring_buffer
*buf
= stream
->priv
;
700 case RING_BUFFER_GET_NEXT_SUBBUF
:
702 struct lttng_metadata_stream
*stream
= filp
->private_data
;
703 struct lib_ring_buffer
*buf
= stream
->priv
;
704 struct channel
*chan
= buf
->backend
.chan
;
706 ret
= lttng_metadata_output_channel(stream
, chan
);
708 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
714 case RING_BUFFER_GET_SUBBUF
:
717 * Random access is not allowed for metadata channel.
724 /* PUT_SUBBUF is the one from lib ring buffer, unmodified. */
726 /* Performing lib ring buffer ioctl after our own. */
727 ret
= lib_ring_buffer_compat_ioctl(filp
, cmd
, arg
, buf
);
732 case RING_BUFFER_PUT_NEXT_SUBBUF
:
734 lttng_metadata_ring_buffer_ioctl_put_next_subbuf(filp
,
747 * This is not used by anonymous file descriptors. This code is left
748 * there if we ever want to implement an inode with open() operation.
751 int lttng_metadata_ring_buffer_open(struct inode
*inode
, struct file
*file
)
753 struct lttng_metadata_stream
*stream
= inode
->i_private
;
754 struct lib_ring_buffer
*buf
= stream
->priv
;
756 file
->private_data
= buf
;
758 * Since life-time of metadata cache differs from that of
759 * session, we need to keep our own reference on the transport.
761 if (!try_module_get(stream
->transport
->owner
)) {
762 printk(KERN_WARNING
"LTT : Can't lock transport module.\n");
765 return lib_ring_buffer_open(inode
, file
, buf
);
769 int lttng_metadata_ring_buffer_release(struct inode
*inode
, struct file
*file
)
771 struct lttng_metadata_stream
*stream
= file
->private_data
;
772 struct lib_ring_buffer
*buf
= stream
->priv
;
774 kref_put(&stream
->metadata_cache
->refcount
, metadata_cache_destroy
);
775 module_put(stream
->transport
->owner
);
776 return lib_ring_buffer_release(inode
, file
, buf
);
780 ssize_t
lttng_metadata_ring_buffer_splice_read(struct file
*in
, loff_t
*ppos
,
781 struct pipe_inode_info
*pipe
, size_t len
,
784 struct lttng_metadata_stream
*stream
= in
->private_data
;
785 struct lib_ring_buffer
*buf
= stream
->priv
;
787 return lib_ring_buffer_splice_read(in
, ppos
, pipe
, len
,
792 int lttng_metadata_ring_buffer_mmap(struct file
*filp
,
793 struct vm_area_struct
*vma
)
795 struct lttng_metadata_stream
*stream
= filp
->private_data
;
796 struct lib_ring_buffer
*buf
= stream
->priv
;
798 return lib_ring_buffer_mmap(filp
, vma
, buf
);
802 const struct file_operations lttng_metadata_ring_buffer_file_operations
= {
803 .owner
= THIS_MODULE
,
804 .open
= lttng_metadata_ring_buffer_open
,
805 .release
= lttng_metadata_ring_buffer_release
,
806 .poll
= lttng_metadata_ring_buffer_poll
,
807 .splice_read
= lttng_metadata_ring_buffer_splice_read
,
808 .mmap
= lttng_metadata_ring_buffer_mmap
,
809 .unlocked_ioctl
= lttng_metadata_ring_buffer_ioctl
,
810 .llseek
= vfs_lib_ring_buffer_no_llseek
,
812 .compat_ioctl
= lttng_metadata_ring_buffer_compat_ioctl
,
817 int lttng_abi_create_stream_fd(struct file
*channel_file
, void *stream_priv
,
818 const struct file_operations
*fops
)
821 struct file
*stream_file
;
823 stream_fd
= get_unused_fd();
828 stream_file
= anon_inode_getfile("[lttng_stream]", fops
,
829 stream_priv
, O_RDWR
);
830 if (IS_ERR(stream_file
)) {
831 ret
= PTR_ERR(stream_file
);
835 * OPEN_FMODE, called within anon_inode_getfile/alloc_file, don't honor
836 * FMODE_LSEEK, FMODE_PREAD nor FMODE_PWRITE. We need to read from this
837 * file descriptor, so we set FMODE_PREAD here.
839 stream_file
->f_mode
|= FMODE_PREAD
;
840 fd_install(stream_fd
, stream_file
);
842 * The stream holds a reference to the channel within the generic ring
843 * buffer library, so no need to hold a refcount on the channel and
844 * session files here.
849 put_unused_fd(stream_fd
);
855 int lttng_abi_open_stream(struct file
*channel_file
)
857 struct lttng_channel
*channel
= channel_file
->private_data
;
858 struct lib_ring_buffer
*buf
;
862 buf
= channel
->ops
->buffer_read_open(channel
->chan
);
867 ret
= lttng_abi_create_stream_fd(channel_file
, stream_priv
,
868 <tng_stream_ring_buffer_file_operations
);
875 channel
->ops
->buffer_read_close(buf
);
880 int lttng_abi_open_metadata_stream(struct file
*channel_file
)
882 struct lttng_channel
*channel
= channel_file
->private_data
;
883 struct lttng_session
*session
= channel
->session
;
884 struct lib_ring_buffer
*buf
;
886 struct lttng_metadata_stream
*metadata_stream
;
889 buf
= channel
->ops
->buffer_read_open(channel
->chan
);
893 metadata_stream
= kzalloc(sizeof(struct lttng_metadata_stream
),
895 if (!metadata_stream
) {
899 metadata_stream
->metadata_cache
= session
->metadata_cache
;
900 init_waitqueue_head(&metadata_stream
->read_wait
);
901 metadata_stream
->priv
= buf
;
902 stream_priv
= metadata_stream
;
903 metadata_stream
->transport
= channel
->transport
;
904 mutex_init(&metadata_stream
->lock
);
907 * Since life-time of metadata cache differs from that of
908 * session, we need to keep our own reference on the transport.
910 if (!try_module_get(metadata_stream
->transport
->owner
)) {
911 printk(KERN_WARNING
"LTT : Can't lock transport module.\n");
916 ret
= lttng_abi_create_stream_fd(channel_file
, stream_priv
,
917 <tng_metadata_ring_buffer_file_operations
);
921 kref_get(&session
->metadata_cache
->refcount
);
922 list_add(&metadata_stream
->list
,
923 &session
->metadata_cache
->metadata_stream
);
927 module_put(metadata_stream
->transport
->owner
);
929 kfree(metadata_stream
);
931 channel
->ops
->buffer_read_close(buf
);
936 int lttng_abi_create_event(struct file
*channel_file
,
937 struct lttng_kernel_event
*event_param
)
939 struct lttng_channel
*channel
= channel_file
->private_data
;
940 struct lttng_event
*event
;
942 struct file
*event_file
;
944 event_param
->name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
945 switch (event_param
->instrumentation
) {
946 case LTTNG_KERNEL_KRETPROBE
:
947 event_param
->u
.kretprobe
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
949 case LTTNG_KERNEL_KPROBE
:
950 event_param
->u
.kprobe
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
952 case LTTNG_KERNEL_FUNCTION
:
953 event_param
->u
.ftrace
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
958 switch (event_param
->instrumentation
) {
960 event_fd
= get_unused_fd();
965 event_file
= anon_inode_getfile("[lttng_event]",
968 if (IS_ERR(event_file
)) {
969 ret
= PTR_ERR(event_file
);
973 * We tolerate no failure path after event creation. It
974 * will stay invariant for the rest of the session.
976 event
= lttng_event_create(channel
, event_param
, NULL
, NULL
);
977 WARN_ON_ONCE(!event
);
979 ret
= PTR_ERR(event
);
982 event_file
->private_data
= event
;
983 fd_install(event_fd
, event_file
);
984 /* The event holds a reference on the channel */
985 atomic_long_inc(&channel_file
->f_count
);
987 case LTTNG_KERNEL_SYSCALL
:
988 ret
= lttng_syscalls_register(channel
, NULL
);
992 if (event_param
->u
.syscall
.enable
) {
993 ret
= lttng_syscall_filter_enable(channel
,
994 event_param
->name
[0] == '\0' ?
995 NULL
: event_param
->name
);
1000 ret
= lttng_syscall_filter_disable(channel
,
1001 event_param
->name
[0] == '\0' ?
1002 NULL
: event_param
->name
);
1013 put_unused_fd(event_fd
);
1019 * lttng_channel_ioctl - lttng syscall through ioctl
1025 * This ioctl implements lttng commands:
1026 * LTTNG_KERNEL_STREAM
1027 * Returns an event stream file descriptor or failure.
1028 * (typically, one event stream records events from one CPU)
1029 * LTTNG_KERNEL_EVENT
1030 * Returns an event file descriptor or failure.
1031 * LTTNG_KERNEL_CONTEXT
1032 * Prepend a context field to each event in the channel
1033 * LTTNG_KERNEL_ENABLE
1034 * Enable recording for events in this channel (weak enable)
1035 * LTTNG_KERNEL_DISABLE
1036 * Disable recording for events in this channel (strong disable)
1038 * Channel and event file descriptors also hold a reference on the session.
1041 long lttng_channel_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1043 struct lttng_channel
*channel
= file
->private_data
;
1046 case LTTNG_KERNEL_OLD_STREAM
:
1047 case LTTNG_KERNEL_STREAM
:
1048 return lttng_abi_open_stream(file
);
1049 case LTTNG_KERNEL_OLD_EVENT
:
1051 struct lttng_kernel_event
*uevent_param
;
1052 struct lttng_kernel_old_event
*old_uevent_param
;
1055 uevent_param
= kmalloc(sizeof(struct lttng_kernel_event
),
1057 if (!uevent_param
) {
1061 old_uevent_param
= kmalloc(
1062 sizeof(struct lttng_kernel_old_event
),
1064 if (!old_uevent_param
) {
1066 goto old_event_error_free_param
;
1068 if (copy_from_user(old_uevent_param
,
1069 (struct lttng_kernel_old_event __user
*) arg
,
1070 sizeof(struct lttng_kernel_old_event
))) {
1072 goto old_event_error_free_old_param
;
1075 memcpy(uevent_param
->name
, old_uevent_param
->name
,
1076 sizeof(uevent_param
->name
));
1077 uevent_param
->instrumentation
=
1078 old_uevent_param
->instrumentation
;
1080 switch (old_uevent_param
->instrumentation
) {
1081 case LTTNG_KERNEL_KPROBE
:
1082 uevent_param
->u
.kprobe
.addr
=
1083 old_uevent_param
->u
.kprobe
.addr
;
1084 uevent_param
->u
.kprobe
.offset
=
1085 old_uevent_param
->u
.kprobe
.offset
;
1086 memcpy(uevent_param
->u
.kprobe
.symbol_name
,
1087 old_uevent_param
->u
.kprobe
.symbol_name
,
1088 sizeof(uevent_param
->u
.kprobe
.symbol_name
));
1090 case LTTNG_KERNEL_KRETPROBE
:
1091 uevent_param
->u
.kretprobe
.addr
=
1092 old_uevent_param
->u
.kretprobe
.addr
;
1093 uevent_param
->u
.kretprobe
.offset
=
1094 old_uevent_param
->u
.kretprobe
.offset
;
1095 memcpy(uevent_param
->u
.kretprobe
.symbol_name
,
1096 old_uevent_param
->u
.kretprobe
.symbol_name
,
1097 sizeof(uevent_param
->u
.kretprobe
.symbol_name
));
1099 case LTTNG_KERNEL_FUNCTION
:
1100 memcpy(uevent_param
->u
.ftrace
.symbol_name
,
1101 old_uevent_param
->u
.ftrace
.symbol_name
,
1102 sizeof(uevent_param
->u
.ftrace
.symbol_name
));
1107 ret
= lttng_abi_create_event(file
, uevent_param
);
1109 old_event_error_free_old_param
:
1110 kfree(old_uevent_param
);
1111 old_event_error_free_param
:
1112 kfree(uevent_param
);
1116 case LTTNG_KERNEL_EVENT
:
1118 struct lttng_kernel_event uevent_param
;
1120 if (copy_from_user(&uevent_param
,
1121 (struct lttng_kernel_event __user
*) arg
,
1122 sizeof(uevent_param
)))
1124 return lttng_abi_create_event(file
, &uevent_param
);
1126 case LTTNG_KERNEL_OLD_CONTEXT
:
1128 struct lttng_kernel_context
*ucontext_param
;
1129 struct lttng_kernel_old_context
*old_ucontext_param
;
1132 ucontext_param
= kmalloc(sizeof(struct lttng_kernel_context
),
1134 if (!ucontext_param
) {
1138 old_ucontext_param
= kmalloc(sizeof(struct lttng_kernel_old_context
),
1140 if (!old_ucontext_param
) {
1142 goto old_ctx_error_free_param
;
1145 if (copy_from_user(old_ucontext_param
,
1146 (struct lttng_kernel_old_context __user
*) arg
,
1147 sizeof(struct lttng_kernel_old_context
))) {
1149 goto old_ctx_error_free_old_param
;
1151 ucontext_param
->ctx
= old_ucontext_param
->ctx
;
1152 memcpy(ucontext_param
->padding
, old_ucontext_param
->padding
,
1153 sizeof(ucontext_param
->padding
));
1154 /* only type that uses the union */
1155 if (old_ucontext_param
->ctx
== LTTNG_KERNEL_CONTEXT_PERF_COUNTER
) {
1156 ucontext_param
->u
.perf_counter
.type
=
1157 old_ucontext_param
->u
.perf_counter
.type
;
1158 ucontext_param
->u
.perf_counter
.config
=
1159 old_ucontext_param
->u
.perf_counter
.config
;
1160 memcpy(ucontext_param
->u
.perf_counter
.name
,
1161 old_ucontext_param
->u
.perf_counter
.name
,
1162 sizeof(ucontext_param
->u
.perf_counter
.name
));
1165 ret
= lttng_abi_add_context(file
,
1167 &channel
->ctx
, channel
->session
);
1169 old_ctx_error_free_old_param
:
1170 kfree(old_ucontext_param
);
1171 old_ctx_error_free_param
:
1172 kfree(ucontext_param
);
1176 case LTTNG_KERNEL_CONTEXT
:
1178 struct lttng_kernel_context ucontext_param
;
1180 if (copy_from_user(&ucontext_param
,
1181 (struct lttng_kernel_context __user
*) arg
,
1182 sizeof(ucontext_param
)))
1184 return lttng_abi_add_context(file
,
1186 &channel
->ctx
, channel
->session
);
1188 case LTTNG_KERNEL_OLD_ENABLE
:
1189 case LTTNG_KERNEL_ENABLE
:
1190 return lttng_channel_enable(channel
);
1191 case LTTNG_KERNEL_OLD_DISABLE
:
1192 case LTTNG_KERNEL_DISABLE
:
1193 return lttng_channel_disable(channel
);
1194 case LTTNG_KERNEL_SYSCALL_MASK
:
1195 return lttng_channel_syscall_mask(channel
,
1196 (struct lttng_kernel_syscall_mask __user
*) arg
);
1198 return -ENOIOCTLCMD
;
1204 * lttng_metadata_ioctl - lttng syscall through ioctl
1210 * This ioctl implements lttng commands:
1211 * LTTNG_KERNEL_STREAM
1212 * Returns an event stream file descriptor or failure.
1214 * Channel and event file descriptors also hold a reference on the session.
1217 long lttng_metadata_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1220 case LTTNG_KERNEL_OLD_STREAM
:
1221 case LTTNG_KERNEL_STREAM
:
1222 return lttng_abi_open_metadata_stream(file
);
1224 return -ENOIOCTLCMD
;
1229 * lttng_channel_poll - lttng stream addition/removal monitoring
1234 unsigned int lttng_channel_poll(struct file
*file
, poll_table
*wait
)
1236 struct lttng_channel
*channel
= file
->private_data
;
1237 unsigned int mask
= 0;
1239 if (file
->f_mode
& FMODE_READ
) {
1240 poll_wait_set_exclusive(wait
);
1241 poll_wait(file
, channel
->ops
->get_hp_wait_queue(channel
->chan
),
1244 if (channel
->ops
->is_disabled(channel
->chan
))
1246 if (channel
->ops
->is_finalized(channel
->chan
))
1248 if (channel
->ops
->buffer_has_read_closed_stream(channel
->chan
))
1249 return POLLIN
| POLLRDNORM
;
1257 int lttng_channel_release(struct inode
*inode
, struct file
*file
)
1259 struct lttng_channel
*channel
= file
->private_data
;
1262 fput(channel
->session
->file
);
1267 int lttng_metadata_channel_release(struct inode
*inode
, struct file
*file
)
1269 struct lttng_channel
*channel
= file
->private_data
;
1272 lttng_metadata_channel_destroy(channel
);
1273 fput(channel
->session
->file
);
1279 static const struct file_operations lttng_channel_fops
= {
1280 .owner
= THIS_MODULE
,
1281 .release
= lttng_channel_release
,
1282 .poll
= lttng_channel_poll
,
1283 .unlocked_ioctl
= lttng_channel_ioctl
,
1284 #ifdef CONFIG_COMPAT
1285 .compat_ioctl
= lttng_channel_ioctl
,
1289 static const struct file_operations lttng_metadata_fops
= {
1290 .owner
= THIS_MODULE
,
1291 .release
= lttng_metadata_channel_release
,
1292 .unlocked_ioctl
= lttng_metadata_ioctl
,
1293 #ifdef CONFIG_COMPAT
1294 .compat_ioctl
= lttng_metadata_ioctl
,
1299 * lttng_event_ioctl - lttng syscall through ioctl
1305 * This ioctl implements lttng commands:
1306 * LTTNG_KERNEL_CONTEXT
1307 * Prepend a context field to each record of this event
1308 * LTTNG_KERNEL_ENABLE
1309 * Enable recording for this event (weak enable)
1310 * LTTNG_KERNEL_DISABLE
1311 * Disable recording for this event (strong disable)
1314 long lttng_event_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1316 struct lttng_event
*event
= file
->private_data
;
1319 case LTTNG_KERNEL_OLD_CONTEXT
:
1321 struct lttng_kernel_context
*ucontext_param
;
1322 struct lttng_kernel_old_context
*old_ucontext_param
;
1325 ucontext_param
= kmalloc(sizeof(struct lttng_kernel_context
),
1327 if (!ucontext_param
) {
1331 old_ucontext_param
= kmalloc(sizeof(struct lttng_kernel_old_context
),
1333 if (!old_ucontext_param
) {
1335 goto old_ctx_error_free_param
;
1338 if (copy_from_user(old_ucontext_param
,
1339 (struct lttng_kernel_old_context __user
*) arg
,
1340 sizeof(struct lttng_kernel_old_context
))) {
1342 goto old_ctx_error_free_old_param
;
1344 ucontext_param
->ctx
= old_ucontext_param
->ctx
;
1345 memcpy(ucontext_param
->padding
, old_ucontext_param
->padding
,
1346 sizeof(ucontext_param
->padding
));
1347 /* only type that uses the union */
1348 if (old_ucontext_param
->ctx
== LTTNG_KERNEL_CONTEXT_PERF_COUNTER
) {
1349 ucontext_param
->u
.perf_counter
.type
=
1350 old_ucontext_param
->u
.perf_counter
.type
;
1351 ucontext_param
->u
.perf_counter
.config
=
1352 old_ucontext_param
->u
.perf_counter
.config
;
1353 memcpy(ucontext_param
->u
.perf_counter
.name
,
1354 old_ucontext_param
->u
.perf_counter
.name
,
1355 sizeof(ucontext_param
->u
.perf_counter
.name
));
1358 ret
= lttng_abi_add_context(file
,
1360 &event
->ctx
, event
->chan
->session
);
1362 old_ctx_error_free_old_param
:
1363 kfree(old_ucontext_param
);
1364 old_ctx_error_free_param
:
1365 kfree(ucontext_param
);
1369 case LTTNG_KERNEL_CONTEXT
:
1371 struct lttng_kernel_context ucontext_param
;
1373 if (copy_from_user(&ucontext_param
,
1374 (struct lttng_kernel_context __user
*) arg
,
1375 sizeof(ucontext_param
)))
1377 return lttng_abi_add_context(file
,
1379 &event
->ctx
, event
->chan
->session
);
1381 case LTTNG_KERNEL_OLD_ENABLE
:
1382 case LTTNG_KERNEL_ENABLE
:
1383 return lttng_event_enable(event
);
1384 case LTTNG_KERNEL_OLD_DISABLE
:
1385 case LTTNG_KERNEL_DISABLE
:
1386 return lttng_event_disable(event
);
1388 return -ENOIOCTLCMD
;
1393 int lttng_event_release(struct inode
*inode
, struct file
*file
)
1395 struct lttng_event
*event
= file
->private_data
;
1398 fput(event
->chan
->file
);
1402 /* TODO: filter control ioctl */
1403 static const struct file_operations lttng_event_fops
= {
1404 .owner
= THIS_MODULE
,
1405 .release
= lttng_event_release
,
1406 .unlocked_ioctl
= lttng_event_ioctl
,
1407 #ifdef CONFIG_COMPAT
1408 .compat_ioctl
= lttng_event_ioctl
,
1412 static int put_u64(uint64_t val
, unsigned long arg
)
1414 return put_user(val
, (uint64_t __user
*) arg
);
1417 static long lttng_stream_ring_buffer_ioctl(struct file
*filp
,
1418 unsigned int cmd
, unsigned long arg
)
1420 struct lib_ring_buffer
*buf
= filp
->private_data
;
1421 struct channel
*chan
= buf
->backend
.chan
;
1422 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1423 const struct lttng_channel_ops
*ops
= chan
->backend
.priv_ops
;
1426 if (atomic_read(&chan
->record_disabled
))
1430 case LTTNG_RING_BUFFER_GET_TIMESTAMP_BEGIN
:
1434 ret
= ops
->timestamp_begin(config
, buf
, &ts
);
1437 return put_u64(ts
, arg
);
1439 case LTTNG_RING_BUFFER_GET_TIMESTAMP_END
:
1443 ret
= ops
->timestamp_end(config
, buf
, &ts
);
1446 return put_u64(ts
, arg
);
1448 case LTTNG_RING_BUFFER_GET_EVENTS_DISCARDED
:
1452 ret
= ops
->events_discarded(config
, buf
, &ed
);
1455 return put_u64(ed
, arg
);
1457 case LTTNG_RING_BUFFER_GET_CONTENT_SIZE
:
1461 ret
= ops
->content_size(config
, buf
, &cs
);
1464 return put_u64(cs
, arg
);
1466 case LTTNG_RING_BUFFER_GET_PACKET_SIZE
:
1470 ret
= ops
->packet_size(config
, buf
, &ps
);
1473 return put_u64(ps
, arg
);
1475 case LTTNG_RING_BUFFER_GET_STREAM_ID
:
1479 ret
= ops
->stream_id(config
, buf
, &si
);
1482 return put_u64(si
, arg
);
1484 case LTTNG_RING_BUFFER_GET_CURRENT_TIMESTAMP
:
1488 ret
= ops
->current_timestamp(config
, buf
, &ts
);
1491 return put_u64(ts
, arg
);
1494 return lib_ring_buffer_file_operations
.unlocked_ioctl(filp
,
1502 #ifdef CONFIG_COMPAT
1503 static long lttng_stream_ring_buffer_compat_ioctl(struct file
*filp
,
1504 unsigned int cmd
, unsigned long arg
)
1506 struct lib_ring_buffer
*buf
= filp
->private_data
;
1507 struct channel
*chan
= buf
->backend
.chan
;
1508 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1509 const struct lttng_channel_ops
*ops
= chan
->backend
.priv_ops
;
1512 if (atomic_read(&chan
->record_disabled
))
1516 case LTTNG_RING_BUFFER_COMPAT_GET_TIMESTAMP_BEGIN
:
1520 ret
= ops
->timestamp_begin(config
, buf
, &ts
);
1523 return put_u64(ts
, arg
);
1525 case LTTNG_RING_BUFFER_COMPAT_GET_TIMESTAMP_END
:
1529 ret
= ops
->timestamp_end(config
, buf
, &ts
);
1532 return put_u64(ts
, arg
);
1534 case LTTNG_RING_BUFFER_COMPAT_GET_EVENTS_DISCARDED
:
1538 ret
= ops
->events_discarded(config
, buf
, &ed
);
1541 return put_u64(ed
, arg
);
1543 case LTTNG_RING_BUFFER_COMPAT_GET_CONTENT_SIZE
:
1547 ret
= ops
->content_size(config
, buf
, &cs
);
1550 return put_u64(cs
, arg
);
1552 case LTTNG_RING_BUFFER_COMPAT_GET_PACKET_SIZE
:
1556 ret
= ops
->packet_size(config
, buf
, &ps
);
1559 return put_u64(ps
, arg
);
1561 case LTTNG_RING_BUFFER_COMPAT_GET_STREAM_ID
:
1565 ret
= ops
->stream_id(config
, buf
, &si
);
1568 return put_u64(si
, arg
);
1570 case LTTNG_RING_BUFFER_GET_CURRENT_TIMESTAMP
:
1574 ret
= ops
->current_timestamp(config
, buf
, &ts
);
1577 return put_u64(ts
, arg
);
1580 return lib_ring_buffer_file_operations
.compat_ioctl(filp
,
1587 #endif /* CONFIG_COMPAT */
1589 static void lttng_stream_override_ring_buffer_fops(void)
1591 lttng_stream_ring_buffer_file_operations
.owner
= THIS_MODULE
;
1592 lttng_stream_ring_buffer_file_operations
.open
=
1593 lib_ring_buffer_file_operations
.open
;
1594 lttng_stream_ring_buffer_file_operations
.release
=
1595 lib_ring_buffer_file_operations
.release
;
1596 lttng_stream_ring_buffer_file_operations
.poll
=
1597 lib_ring_buffer_file_operations
.poll
;
1598 lttng_stream_ring_buffer_file_operations
.splice_read
=
1599 lib_ring_buffer_file_operations
.splice_read
;
1600 lttng_stream_ring_buffer_file_operations
.mmap
=
1601 lib_ring_buffer_file_operations
.mmap
;
1602 lttng_stream_ring_buffer_file_operations
.unlocked_ioctl
=
1603 lttng_stream_ring_buffer_ioctl
;
1604 lttng_stream_ring_buffer_file_operations
.llseek
=
1605 lib_ring_buffer_file_operations
.llseek
;
1606 #ifdef CONFIG_COMPAT
1607 lttng_stream_ring_buffer_file_operations
.compat_ioctl
=
1608 lttng_stream_ring_buffer_compat_ioctl
;
1612 int __init
lttng_abi_init(void)
1616 wrapper_vmalloc_sync_all();
1617 lttng_proc_dentry
= proc_create_data("lttng", S_IRUSR
| S_IWUSR
, NULL
,
1620 if (!lttng_proc_dentry
) {
1621 printk(KERN_ERR
"Error creating LTTng control file\n");
1625 lttng_stream_override_ring_buffer_fops();
1631 /* No __exit annotation because used by init error path too. */
1632 void lttng_abi_exit(void)
1634 if (lttng_proc_dentry
)
1635 remove_proc_entry("lttng", NULL
);