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
);
140 fput(tracepoint_list_file
);
142 put_unused_fd(file_fd
);
147 #ifndef CONFIG_HAVE_SYSCALL_TRACEPOINTS
149 int lttng_abi_syscall_list(void)
155 int lttng_abi_syscall_list(void)
157 struct file
*syscall_list_file
;
160 file_fd
= lttng_get_unused_fd();
166 syscall_list_file
= anon_inode_getfile("[lttng_syscall_list]",
167 <tng_syscall_list_fops
,
169 if (IS_ERR(syscall_list_file
)) {
170 ret
= PTR_ERR(syscall_list_file
);
173 ret
= lttng_syscall_list_fops
.open(NULL
, syscall_list_file
);
176 fd_install(file_fd
, syscall_list_file
);
184 fput(syscall_list_file
);
186 put_unused_fd(file_fd
);
193 void lttng_abi_tracer_version(struct lttng_kernel_tracer_version
*v
)
195 v
->major
= LTTNG_MODULES_MAJOR_VERSION
;
196 v
->minor
= LTTNG_MODULES_MINOR_VERSION
;
197 v
->patchlevel
= LTTNG_MODULES_PATCHLEVEL_VERSION
;
201 void lttng_abi_tracer_abi_version(struct lttng_kernel_tracer_abi_version
*v
)
203 v
->major
= LTTNG_MODULES_ABI_MAJOR_VERSION
;
204 v
->minor
= LTTNG_MODULES_ABI_MINOR_VERSION
;
208 long lttng_abi_add_context(struct file
*file
,
209 struct lttng_kernel_context
*context_param
,
210 struct lttng_ctx
**ctx
, struct lttng_session
*session
)
213 if (session
->been_active
)
216 switch (context_param
->ctx
) {
217 case LTTNG_KERNEL_CONTEXT_PID
:
218 return lttng_add_pid_to_ctx(ctx
);
219 case LTTNG_KERNEL_CONTEXT_PRIO
:
220 return lttng_add_prio_to_ctx(ctx
);
221 case LTTNG_KERNEL_CONTEXT_NICE
:
222 return lttng_add_nice_to_ctx(ctx
);
223 case LTTNG_KERNEL_CONTEXT_VPID
:
224 return lttng_add_vpid_to_ctx(ctx
);
225 case LTTNG_KERNEL_CONTEXT_TID
:
226 return lttng_add_tid_to_ctx(ctx
);
227 case LTTNG_KERNEL_CONTEXT_VTID
:
228 return lttng_add_vtid_to_ctx(ctx
);
229 case LTTNG_KERNEL_CONTEXT_PPID
:
230 return lttng_add_ppid_to_ctx(ctx
);
231 case LTTNG_KERNEL_CONTEXT_VPPID
:
232 return lttng_add_vppid_to_ctx(ctx
);
233 case LTTNG_KERNEL_CONTEXT_PERF_COUNTER
:
234 context_param
->u
.perf_counter
.name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
235 return lttng_add_perf_counter_to_ctx(context_param
->u
.perf_counter
.type
,
236 context_param
->u
.perf_counter
.config
,
237 context_param
->u
.perf_counter
.name
,
239 case LTTNG_KERNEL_CONTEXT_PROCNAME
:
240 return lttng_add_procname_to_ctx(ctx
);
241 case LTTNG_KERNEL_CONTEXT_HOSTNAME
:
242 return lttng_add_hostname_to_ctx(ctx
);
243 case LTTNG_KERNEL_CONTEXT_CPU_ID
:
244 return lttng_add_cpu_id_to_ctx(ctx
);
251 * lttng_ioctl - lttng syscall through ioctl
257 * This ioctl implements lttng commands:
258 * LTTNG_KERNEL_SESSION
259 * Returns a LTTng trace session file descriptor
260 * LTTNG_KERNEL_TRACER_VERSION
261 * Returns the LTTng kernel tracer version
262 * LTTNG_KERNEL_TRACEPOINT_LIST
263 * Returns a file descriptor listing available tracepoints
264 * LTTNG_KERNEL_WAIT_QUIESCENT
265 * Returns after all previously running probes have completed
266 * LTTNG_KERNEL_TRACER_ABI_VERSION
267 * Returns the LTTng kernel tracer ABI version
269 * The returned session will be deleted when its file descriptor is closed.
272 long lttng_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
275 case LTTNG_KERNEL_OLD_SESSION
:
276 case LTTNG_KERNEL_SESSION
:
277 return lttng_abi_create_session();
278 case LTTNG_KERNEL_OLD_TRACER_VERSION
:
280 struct lttng_kernel_tracer_version v
;
281 struct lttng_kernel_old_tracer_version oldv
;
282 struct lttng_kernel_old_tracer_version
*uversion
=
283 (struct lttng_kernel_old_tracer_version __user
*) arg
;
285 lttng_abi_tracer_version(&v
);
286 oldv
.major
= v
.major
;
287 oldv
.minor
= v
.minor
;
288 oldv
.patchlevel
= v
.patchlevel
;
290 if (copy_to_user(uversion
, &oldv
, sizeof(oldv
)))
294 case LTTNG_KERNEL_TRACER_VERSION
:
296 struct lttng_kernel_tracer_version version
;
297 struct lttng_kernel_tracer_version
*uversion
=
298 (struct lttng_kernel_tracer_version __user
*) arg
;
300 lttng_abi_tracer_version(&version
);
302 if (copy_to_user(uversion
, &version
, sizeof(version
)))
306 case LTTNG_KERNEL_TRACER_ABI_VERSION
:
308 struct lttng_kernel_tracer_abi_version version
;
309 struct lttng_kernel_tracer_abi_version
*uversion
=
310 (struct lttng_kernel_tracer_abi_version __user
*) arg
;
312 lttng_abi_tracer_abi_version(&version
);
314 if (copy_to_user(uversion
, &version
, sizeof(version
)))
318 case LTTNG_KERNEL_OLD_TRACEPOINT_LIST
:
319 case LTTNG_KERNEL_TRACEPOINT_LIST
:
320 return lttng_abi_tracepoint_list();
321 case LTTNG_KERNEL_SYSCALL_LIST
:
322 return lttng_abi_syscall_list();
323 case LTTNG_KERNEL_OLD_WAIT_QUIESCENT
:
324 case LTTNG_KERNEL_WAIT_QUIESCENT
:
327 case LTTNG_KERNEL_OLD_CALIBRATE
:
329 struct lttng_kernel_old_calibrate __user
*ucalibrate
=
330 (struct lttng_kernel_old_calibrate __user
*) arg
;
331 struct lttng_kernel_old_calibrate old_calibrate
;
332 struct lttng_kernel_calibrate calibrate
;
335 if (copy_from_user(&old_calibrate
, ucalibrate
, sizeof(old_calibrate
)))
337 calibrate
.type
= old_calibrate
.type
;
338 ret
= lttng_calibrate(&calibrate
);
339 if (copy_to_user(ucalibrate
, &old_calibrate
, sizeof(old_calibrate
)))
343 case LTTNG_KERNEL_CALIBRATE
:
345 struct lttng_kernel_calibrate __user
*ucalibrate
=
346 (struct lttng_kernel_calibrate __user
*) arg
;
347 struct lttng_kernel_calibrate calibrate
;
350 if (copy_from_user(&calibrate
, ucalibrate
, sizeof(calibrate
)))
352 ret
= lttng_calibrate(&calibrate
);
353 if (copy_to_user(ucalibrate
, &calibrate
, sizeof(calibrate
)))
362 static const struct file_operations lttng_fops
= {
363 .owner
= THIS_MODULE
,
364 .unlocked_ioctl
= lttng_ioctl
,
366 .compat_ioctl
= lttng_ioctl
,
371 int lttng_abi_create_channel(struct file
*session_file
,
372 struct lttng_kernel_channel
*chan_param
,
373 enum channel_type channel_type
)
375 struct lttng_session
*session
= session_file
->private_data
;
376 const struct file_operations
*fops
= NULL
;
377 const char *transport_name
;
378 struct lttng_channel
*chan
;
379 struct file
*chan_file
;
383 chan_fd
= lttng_get_unused_fd();
388 switch (channel_type
) {
389 case PER_CPU_CHANNEL
:
390 fops
= <tng_channel_fops
;
392 case METADATA_CHANNEL
:
393 fops
= <tng_metadata_fops
;
397 chan_file
= anon_inode_getfile("[lttng_channel]",
400 if (IS_ERR(chan_file
)) {
401 ret
= PTR_ERR(chan_file
);
404 switch (channel_type
) {
405 case PER_CPU_CHANNEL
:
406 if (chan_param
->output
== LTTNG_KERNEL_SPLICE
) {
407 transport_name
= chan_param
->overwrite
?
408 "relay-overwrite" : "relay-discard";
409 } else if (chan_param
->output
== LTTNG_KERNEL_MMAP
) {
410 transport_name
= chan_param
->overwrite
?
411 "relay-overwrite-mmap" : "relay-discard-mmap";
416 case METADATA_CHANNEL
:
417 if (chan_param
->output
== LTTNG_KERNEL_SPLICE
)
418 transport_name
= "relay-metadata";
419 else if (chan_param
->output
== LTTNG_KERNEL_MMAP
)
420 transport_name
= "relay-metadata-mmap";
425 transport_name
= "<unknown>";
429 * We tolerate no failure path after channel creation. It will stay
430 * invariant for the rest of the session.
432 chan
= lttng_channel_create(session
, transport_name
, NULL
,
433 chan_param
->subbuf_size
,
434 chan_param
->num_subbuf
,
435 chan_param
->switch_timer_interval
,
436 chan_param
->read_timer_interval
,
442 chan
->file
= chan_file
;
443 chan_file
->private_data
= chan
;
444 fd_install(chan_fd
, chan_file
);
445 atomic_long_inc(&session_file
->f_count
);
452 put_unused_fd(chan_fd
);
458 * lttng_session_ioctl - lttng session fd ioctl
464 * This ioctl implements lttng commands:
465 * LTTNG_KERNEL_CHANNEL
466 * Returns a LTTng channel file descriptor
467 * LTTNG_KERNEL_ENABLE
468 * Enables tracing for a session (weak enable)
469 * LTTNG_KERNEL_DISABLE
470 * Disables tracing for a session (strong disable)
471 * LTTNG_KERNEL_METADATA
472 * Returns a LTTng metadata file descriptor
473 * LTTNG_KERNEL_SESSION_TRACK_PID
474 * Add PID to session tracker
475 * LTTNG_KERNEL_SESSION_UNTRACK_PID
476 * Remove PID from session tracker
478 * The returned channel will be deleted when its file descriptor is closed.
481 long lttng_session_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
483 struct lttng_session
*session
= file
->private_data
;
486 case LTTNG_KERNEL_OLD_CHANNEL
:
488 struct lttng_kernel_channel chan_param
;
489 struct lttng_kernel_old_channel old_chan_param
;
491 if (copy_from_user(&old_chan_param
,
492 (struct lttng_kernel_old_channel __user
*) arg
,
493 sizeof(struct lttng_kernel_old_channel
)))
495 chan_param
.overwrite
= old_chan_param
.overwrite
;
496 chan_param
.subbuf_size
= old_chan_param
.subbuf_size
;
497 chan_param
.num_subbuf
= old_chan_param
.num_subbuf
;
498 chan_param
.switch_timer_interval
= old_chan_param
.switch_timer_interval
;
499 chan_param
.read_timer_interval
= old_chan_param
.read_timer_interval
;
500 chan_param
.output
= old_chan_param
.output
;
502 return lttng_abi_create_channel(file
, &chan_param
,
505 case LTTNG_KERNEL_CHANNEL
:
507 struct lttng_kernel_channel chan_param
;
509 if (copy_from_user(&chan_param
,
510 (struct lttng_kernel_channel __user
*) arg
,
511 sizeof(struct lttng_kernel_channel
)))
513 return lttng_abi_create_channel(file
, &chan_param
,
516 case LTTNG_KERNEL_OLD_SESSION_START
:
517 case LTTNG_KERNEL_OLD_ENABLE
:
518 case LTTNG_KERNEL_SESSION_START
:
519 case LTTNG_KERNEL_ENABLE
:
520 return lttng_session_enable(session
);
521 case LTTNG_KERNEL_OLD_SESSION_STOP
:
522 case LTTNG_KERNEL_OLD_DISABLE
:
523 case LTTNG_KERNEL_SESSION_STOP
:
524 case LTTNG_KERNEL_DISABLE
:
525 return lttng_session_disable(session
);
526 case LTTNG_KERNEL_OLD_METADATA
:
528 struct lttng_kernel_channel chan_param
;
529 struct lttng_kernel_old_channel old_chan_param
;
531 if (copy_from_user(&old_chan_param
,
532 (struct lttng_kernel_old_channel __user
*) arg
,
533 sizeof(struct lttng_kernel_old_channel
)))
535 chan_param
.overwrite
= old_chan_param
.overwrite
;
536 chan_param
.subbuf_size
= old_chan_param
.subbuf_size
;
537 chan_param
.num_subbuf
= old_chan_param
.num_subbuf
;
538 chan_param
.switch_timer_interval
= old_chan_param
.switch_timer_interval
;
539 chan_param
.read_timer_interval
= old_chan_param
.read_timer_interval
;
540 chan_param
.output
= old_chan_param
.output
;
542 return lttng_abi_create_channel(file
, &chan_param
,
545 case LTTNG_KERNEL_METADATA
:
547 struct lttng_kernel_channel chan_param
;
549 if (copy_from_user(&chan_param
,
550 (struct lttng_kernel_channel __user
*) arg
,
551 sizeof(struct lttng_kernel_channel
)))
553 return lttng_abi_create_channel(file
, &chan_param
,
556 case LTTNG_KERNEL_SESSION_TRACK_PID
:
557 return lttng_session_track_pid(session
, (int) arg
);
558 case LTTNG_KERNEL_SESSION_UNTRACK_PID
:
559 return lttng_session_untrack_pid(session
, (int) arg
);
560 case LTTNG_KERNEL_SESSION_LIST_TRACKER_PIDS
:
561 return lttng_session_list_tracker_pids(session
);
568 * Called when the last file reference is dropped.
570 * Big fat note: channels and events are invariant for the whole session after
571 * their creation. So this session destruction also destroys all channel and
572 * event structures specific to this session (they are not destroyed when their
573 * individual file is released).
576 int lttng_session_release(struct inode
*inode
, struct file
*file
)
578 struct lttng_session
*session
= file
->private_data
;
581 lttng_session_destroy(session
);
585 static const struct file_operations lttng_session_fops
= {
586 .owner
= THIS_MODULE
,
587 .release
= lttng_session_release
,
588 .unlocked_ioctl
= lttng_session_ioctl
,
590 .compat_ioctl
= lttng_session_ioctl
,
595 * lttng_metadata_ring_buffer_poll - LTTng ring buffer poll file operation
599 * Handles the poll operations for the metadata channels.
602 unsigned int lttng_metadata_ring_buffer_poll(struct file
*filp
,
605 struct lttng_metadata_stream
*stream
= filp
->private_data
;
606 struct lib_ring_buffer
*buf
= stream
->priv
;
608 unsigned int mask
= 0;
610 if (filp
->f_mode
& FMODE_READ
) {
611 poll_wait_set_exclusive(wait
);
612 poll_wait(filp
, &stream
->read_wait
, wait
);
614 finalized
= stream
->finalized
;
617 * lib_ring_buffer_is_finalized() contains a smp_rmb()
618 * ordering finalized load before offsets loads.
620 WARN_ON(atomic_long_read(&buf
->active_readers
) != 1);
625 if (stream
->metadata_cache
->metadata_written
>
626 stream
->metadata_out
)
634 void lttng_metadata_ring_buffer_ioctl_put_next_subbuf(struct file
*filp
,
635 unsigned int cmd
, unsigned long arg
)
637 struct lttng_metadata_stream
*stream
= filp
->private_data
;
639 stream
->metadata_out
= stream
->metadata_in
;
643 long lttng_metadata_ring_buffer_ioctl(struct file
*filp
,
644 unsigned int cmd
, unsigned long arg
)
647 struct lttng_metadata_stream
*stream
= filp
->private_data
;
648 struct lib_ring_buffer
*buf
= stream
->priv
;
651 case RING_BUFFER_GET_NEXT_SUBBUF
:
653 struct lttng_metadata_stream
*stream
= filp
->private_data
;
654 struct lib_ring_buffer
*buf
= stream
->priv
;
655 struct channel
*chan
= buf
->backend
.chan
;
657 ret
= lttng_metadata_output_channel(stream
, chan
);
659 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
665 case RING_BUFFER_GET_SUBBUF
:
668 * Random access is not allowed for metadata channel.
672 case RING_BUFFER_FLUSH
:
674 struct lttng_metadata_stream
*stream
= filp
->private_data
;
675 struct lib_ring_buffer
*buf
= stream
->priv
;
676 struct channel
*chan
= buf
->backend
.chan
;
679 * Before doing the actual ring buffer flush, write up to one
680 * packet of metadata in the ring buffer.
682 ret
= lttng_metadata_output_channel(stream
, chan
);
690 /* PUT_SUBBUF is the one from lib ring buffer, unmodified. */
692 /* Performing lib ring buffer ioctl after our own. */
693 ret
= lib_ring_buffer_ioctl(filp
, cmd
, arg
, buf
);
698 case RING_BUFFER_PUT_NEXT_SUBBUF
:
700 lttng_metadata_ring_buffer_ioctl_put_next_subbuf(filp
,
713 long lttng_metadata_ring_buffer_compat_ioctl(struct file
*filp
,
714 unsigned int cmd
, unsigned long arg
)
717 struct lttng_metadata_stream
*stream
= filp
->private_data
;
718 struct lib_ring_buffer
*buf
= stream
->priv
;
721 case RING_BUFFER_GET_NEXT_SUBBUF
:
723 struct lttng_metadata_stream
*stream
= filp
->private_data
;
724 struct lib_ring_buffer
*buf
= stream
->priv
;
725 struct channel
*chan
= buf
->backend
.chan
;
727 ret
= lttng_metadata_output_channel(stream
, chan
);
729 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
735 case RING_BUFFER_GET_SUBBUF
:
738 * Random access is not allowed for metadata channel.
745 /* PUT_SUBBUF is the one from lib ring buffer, unmodified. */
747 /* Performing lib ring buffer ioctl after our own. */
748 ret
= lib_ring_buffer_compat_ioctl(filp
, cmd
, arg
, buf
);
753 case RING_BUFFER_PUT_NEXT_SUBBUF
:
755 lttng_metadata_ring_buffer_ioctl_put_next_subbuf(filp
,
768 * This is not used by anonymous file descriptors. This code is left
769 * there if we ever want to implement an inode with open() operation.
772 int lttng_metadata_ring_buffer_open(struct inode
*inode
, struct file
*file
)
774 struct lttng_metadata_stream
*stream
= inode
->i_private
;
775 struct lib_ring_buffer
*buf
= stream
->priv
;
777 file
->private_data
= buf
;
779 * Since life-time of metadata cache differs from that of
780 * session, we need to keep our own reference on the transport.
782 if (!try_module_get(stream
->transport
->owner
)) {
783 printk(KERN_WARNING
"LTT : Can't lock transport module.\n");
786 return lib_ring_buffer_open(inode
, file
, buf
);
790 int lttng_metadata_ring_buffer_release(struct inode
*inode
, struct file
*file
)
792 struct lttng_metadata_stream
*stream
= file
->private_data
;
793 struct lib_ring_buffer
*buf
= stream
->priv
;
795 kref_put(&stream
->metadata_cache
->refcount
, metadata_cache_destroy
);
796 module_put(stream
->transport
->owner
);
797 return lib_ring_buffer_release(inode
, file
, buf
);
801 ssize_t
lttng_metadata_ring_buffer_splice_read(struct file
*in
, loff_t
*ppos
,
802 struct pipe_inode_info
*pipe
, size_t len
,
805 struct lttng_metadata_stream
*stream
= in
->private_data
;
806 struct lib_ring_buffer
*buf
= stream
->priv
;
808 return lib_ring_buffer_splice_read(in
, ppos
, pipe
, len
,
813 int lttng_metadata_ring_buffer_mmap(struct file
*filp
,
814 struct vm_area_struct
*vma
)
816 struct lttng_metadata_stream
*stream
= filp
->private_data
;
817 struct lib_ring_buffer
*buf
= stream
->priv
;
819 return lib_ring_buffer_mmap(filp
, vma
, buf
);
823 const struct file_operations lttng_metadata_ring_buffer_file_operations
= {
824 .owner
= THIS_MODULE
,
825 .open
= lttng_metadata_ring_buffer_open
,
826 .release
= lttng_metadata_ring_buffer_release
,
827 .poll
= lttng_metadata_ring_buffer_poll
,
828 .splice_read
= lttng_metadata_ring_buffer_splice_read
,
829 .mmap
= lttng_metadata_ring_buffer_mmap
,
830 .unlocked_ioctl
= lttng_metadata_ring_buffer_ioctl
,
831 .llseek
= vfs_lib_ring_buffer_no_llseek
,
833 .compat_ioctl
= lttng_metadata_ring_buffer_compat_ioctl
,
838 int lttng_abi_create_stream_fd(struct file
*channel_file
, void *stream_priv
,
839 const struct file_operations
*fops
)
842 struct file
*stream_file
;
844 stream_fd
= lttng_get_unused_fd();
849 stream_file
= anon_inode_getfile("[lttng_stream]", fops
,
850 stream_priv
, O_RDWR
);
851 if (IS_ERR(stream_file
)) {
852 ret
= PTR_ERR(stream_file
);
856 * OPEN_FMODE, called within anon_inode_getfile/alloc_file, don't honor
857 * FMODE_LSEEK, FMODE_PREAD nor FMODE_PWRITE. We need to read from this
858 * file descriptor, so we set FMODE_PREAD here.
860 stream_file
->f_mode
|= FMODE_PREAD
;
861 fd_install(stream_fd
, stream_file
);
863 * The stream holds a reference to the channel within the generic ring
864 * buffer library, so no need to hold a refcount on the channel and
865 * session files here.
870 put_unused_fd(stream_fd
);
876 int lttng_abi_open_stream(struct file
*channel_file
)
878 struct lttng_channel
*channel
= channel_file
->private_data
;
879 struct lib_ring_buffer
*buf
;
883 buf
= channel
->ops
->buffer_read_open(channel
->chan
);
888 ret
= lttng_abi_create_stream_fd(channel_file
, stream_priv
,
889 <tng_stream_ring_buffer_file_operations
);
896 channel
->ops
->buffer_read_close(buf
);
901 int lttng_abi_open_metadata_stream(struct file
*channel_file
)
903 struct lttng_channel
*channel
= channel_file
->private_data
;
904 struct lttng_session
*session
= channel
->session
;
905 struct lib_ring_buffer
*buf
;
907 struct lttng_metadata_stream
*metadata_stream
;
910 buf
= channel
->ops
->buffer_read_open(channel
->chan
);
914 metadata_stream
= kzalloc(sizeof(struct lttng_metadata_stream
),
916 if (!metadata_stream
) {
920 metadata_stream
->metadata_cache
= session
->metadata_cache
;
921 init_waitqueue_head(&metadata_stream
->read_wait
);
922 metadata_stream
->priv
= buf
;
923 stream_priv
= metadata_stream
;
924 metadata_stream
->transport
= channel
->transport
;
925 mutex_init(&metadata_stream
->lock
);
928 * Since life-time of metadata cache differs from that of
929 * session, we need to keep our own reference on the transport.
931 if (!try_module_get(metadata_stream
->transport
->owner
)) {
932 printk(KERN_WARNING
"LTT : Can't lock transport module.\n");
937 ret
= lttng_abi_create_stream_fd(channel_file
, stream_priv
,
938 <tng_metadata_ring_buffer_file_operations
);
942 kref_get(&session
->metadata_cache
->refcount
);
943 list_add(&metadata_stream
->list
,
944 &session
->metadata_cache
->metadata_stream
);
948 module_put(metadata_stream
->transport
->owner
);
950 kfree(metadata_stream
);
952 channel
->ops
->buffer_read_close(buf
);
957 int lttng_abi_create_event(struct file
*channel_file
,
958 struct lttng_kernel_event
*event_param
)
960 struct lttng_channel
*channel
= channel_file
->private_data
;
962 struct file
*event_file
;
965 event_param
->name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
966 switch (event_param
->instrumentation
) {
967 case LTTNG_KERNEL_KRETPROBE
:
968 event_param
->u
.kretprobe
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
970 case LTTNG_KERNEL_KPROBE
:
971 event_param
->u
.kprobe
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
973 case LTTNG_KERNEL_FUNCTION
:
974 event_param
->u
.ftrace
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
979 event_fd
= lttng_get_unused_fd();
984 event_file
= anon_inode_getfile("[lttng_event]",
987 if (IS_ERR(event_file
)) {
988 ret
= PTR_ERR(event_file
);
991 if (event_param
->instrumentation
== LTTNG_KERNEL_TRACEPOINT
992 || event_param
->instrumentation
== LTTNG_KERNEL_SYSCALL
) {
993 struct lttng_enabler
*enabler
;
995 if (event_param
->name
[strlen(event_param
->name
) - 1] == '*') {
996 enabler
= lttng_enabler_create(LTTNG_ENABLER_WILDCARD
,
997 event_param
, channel
);
999 enabler
= lttng_enabler_create(LTTNG_ENABLER_NAME
,
1000 event_param
, channel
);
1004 struct lttng_event
*event
;
1007 * We tolerate no failure path after event creation. It
1008 * will stay invariant for the rest of the session.
1010 event
= lttng_event_create(channel
, event_param
,
1012 event_param
->instrumentation
);
1013 WARN_ON_ONCE(!event
);
1014 if (IS_ERR(event
)) {
1015 ret
= PTR_ERR(event
);
1020 event_file
->private_data
= priv
;
1021 fd_install(event_fd
, event_file
);
1022 /* The event holds a reference on the channel */
1023 atomic_long_inc(&channel_file
->f_count
);
1029 put_unused_fd(event_fd
);
1035 * lttng_channel_ioctl - lttng syscall through ioctl
1041 * This ioctl implements lttng commands:
1042 * LTTNG_KERNEL_STREAM
1043 * Returns an event stream file descriptor or failure.
1044 * (typically, one event stream records events from one CPU)
1045 * LTTNG_KERNEL_EVENT
1046 * Returns an event file descriptor or failure.
1047 * LTTNG_KERNEL_CONTEXT
1048 * Prepend a context field to each event in the channel
1049 * LTTNG_KERNEL_ENABLE
1050 * Enable recording for events in this channel (weak enable)
1051 * LTTNG_KERNEL_DISABLE
1052 * Disable recording for events in this channel (strong disable)
1054 * Channel and event file descriptors also hold a reference on the session.
1057 long lttng_channel_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1059 struct lttng_channel
*channel
= file
->private_data
;
1062 case LTTNG_KERNEL_OLD_STREAM
:
1063 case LTTNG_KERNEL_STREAM
:
1064 return lttng_abi_open_stream(file
);
1065 case LTTNG_KERNEL_OLD_EVENT
:
1067 struct lttng_kernel_event
*uevent_param
;
1068 struct lttng_kernel_old_event
*old_uevent_param
;
1071 uevent_param
= kmalloc(sizeof(struct lttng_kernel_event
),
1073 if (!uevent_param
) {
1077 old_uevent_param
= kmalloc(
1078 sizeof(struct lttng_kernel_old_event
),
1080 if (!old_uevent_param
) {
1082 goto old_event_error_free_param
;
1084 if (copy_from_user(old_uevent_param
,
1085 (struct lttng_kernel_old_event __user
*) arg
,
1086 sizeof(struct lttng_kernel_old_event
))) {
1088 goto old_event_error_free_old_param
;
1091 memcpy(uevent_param
->name
, old_uevent_param
->name
,
1092 sizeof(uevent_param
->name
));
1093 uevent_param
->instrumentation
=
1094 old_uevent_param
->instrumentation
;
1096 switch (old_uevent_param
->instrumentation
) {
1097 case LTTNG_KERNEL_KPROBE
:
1098 uevent_param
->u
.kprobe
.addr
=
1099 old_uevent_param
->u
.kprobe
.addr
;
1100 uevent_param
->u
.kprobe
.offset
=
1101 old_uevent_param
->u
.kprobe
.offset
;
1102 memcpy(uevent_param
->u
.kprobe
.symbol_name
,
1103 old_uevent_param
->u
.kprobe
.symbol_name
,
1104 sizeof(uevent_param
->u
.kprobe
.symbol_name
));
1106 case LTTNG_KERNEL_KRETPROBE
:
1107 uevent_param
->u
.kretprobe
.addr
=
1108 old_uevent_param
->u
.kretprobe
.addr
;
1109 uevent_param
->u
.kretprobe
.offset
=
1110 old_uevent_param
->u
.kretprobe
.offset
;
1111 memcpy(uevent_param
->u
.kretprobe
.symbol_name
,
1112 old_uevent_param
->u
.kretprobe
.symbol_name
,
1113 sizeof(uevent_param
->u
.kretprobe
.symbol_name
));
1115 case LTTNG_KERNEL_FUNCTION
:
1116 memcpy(uevent_param
->u
.ftrace
.symbol_name
,
1117 old_uevent_param
->u
.ftrace
.symbol_name
,
1118 sizeof(uevent_param
->u
.ftrace
.symbol_name
));
1123 ret
= lttng_abi_create_event(file
, uevent_param
);
1125 old_event_error_free_old_param
:
1126 kfree(old_uevent_param
);
1127 old_event_error_free_param
:
1128 kfree(uevent_param
);
1132 case LTTNG_KERNEL_EVENT
:
1134 struct lttng_kernel_event uevent_param
;
1136 if (copy_from_user(&uevent_param
,
1137 (struct lttng_kernel_event __user
*) arg
,
1138 sizeof(uevent_param
)))
1140 return lttng_abi_create_event(file
, &uevent_param
);
1142 case LTTNG_KERNEL_OLD_CONTEXT
:
1144 struct lttng_kernel_context
*ucontext_param
;
1145 struct lttng_kernel_old_context
*old_ucontext_param
;
1148 ucontext_param
= kmalloc(sizeof(struct lttng_kernel_context
),
1150 if (!ucontext_param
) {
1154 old_ucontext_param
= kmalloc(sizeof(struct lttng_kernel_old_context
),
1156 if (!old_ucontext_param
) {
1158 goto old_ctx_error_free_param
;
1161 if (copy_from_user(old_ucontext_param
,
1162 (struct lttng_kernel_old_context __user
*) arg
,
1163 sizeof(struct lttng_kernel_old_context
))) {
1165 goto old_ctx_error_free_old_param
;
1167 ucontext_param
->ctx
= old_ucontext_param
->ctx
;
1168 memcpy(ucontext_param
->padding
, old_ucontext_param
->padding
,
1169 sizeof(ucontext_param
->padding
));
1170 /* only type that uses the union */
1171 if (old_ucontext_param
->ctx
== LTTNG_KERNEL_CONTEXT_PERF_COUNTER
) {
1172 ucontext_param
->u
.perf_counter
.type
=
1173 old_ucontext_param
->u
.perf_counter
.type
;
1174 ucontext_param
->u
.perf_counter
.config
=
1175 old_ucontext_param
->u
.perf_counter
.config
;
1176 memcpy(ucontext_param
->u
.perf_counter
.name
,
1177 old_ucontext_param
->u
.perf_counter
.name
,
1178 sizeof(ucontext_param
->u
.perf_counter
.name
));
1181 ret
= lttng_abi_add_context(file
,
1183 &channel
->ctx
, channel
->session
);
1185 old_ctx_error_free_old_param
:
1186 kfree(old_ucontext_param
);
1187 old_ctx_error_free_param
:
1188 kfree(ucontext_param
);
1192 case LTTNG_KERNEL_CONTEXT
:
1194 struct lttng_kernel_context ucontext_param
;
1196 if (copy_from_user(&ucontext_param
,
1197 (struct lttng_kernel_context __user
*) arg
,
1198 sizeof(ucontext_param
)))
1200 return lttng_abi_add_context(file
,
1202 &channel
->ctx
, channel
->session
);
1204 case LTTNG_KERNEL_OLD_ENABLE
:
1205 case LTTNG_KERNEL_ENABLE
:
1206 return lttng_channel_enable(channel
);
1207 case LTTNG_KERNEL_OLD_DISABLE
:
1208 case LTTNG_KERNEL_DISABLE
:
1209 return lttng_channel_disable(channel
);
1210 case LTTNG_KERNEL_SYSCALL_MASK
:
1211 return lttng_channel_syscall_mask(channel
,
1212 (struct lttng_kernel_syscall_mask __user
*) arg
);
1214 return -ENOIOCTLCMD
;
1220 * lttng_metadata_ioctl - lttng syscall through ioctl
1226 * This ioctl implements lttng commands:
1227 * LTTNG_KERNEL_STREAM
1228 * Returns an event stream file descriptor or failure.
1230 * Channel and event file descriptors also hold a reference on the session.
1233 long lttng_metadata_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1236 case LTTNG_KERNEL_OLD_STREAM
:
1237 case LTTNG_KERNEL_STREAM
:
1238 return lttng_abi_open_metadata_stream(file
);
1240 return -ENOIOCTLCMD
;
1245 * lttng_channel_poll - lttng stream addition/removal monitoring
1250 unsigned int lttng_channel_poll(struct file
*file
, poll_table
*wait
)
1252 struct lttng_channel
*channel
= file
->private_data
;
1253 unsigned int mask
= 0;
1255 if (file
->f_mode
& FMODE_READ
) {
1256 poll_wait_set_exclusive(wait
);
1257 poll_wait(file
, channel
->ops
->get_hp_wait_queue(channel
->chan
),
1260 if (channel
->ops
->is_disabled(channel
->chan
))
1262 if (channel
->ops
->is_finalized(channel
->chan
))
1264 if (channel
->ops
->buffer_has_read_closed_stream(channel
->chan
))
1265 return POLLIN
| POLLRDNORM
;
1273 int lttng_channel_release(struct inode
*inode
, struct file
*file
)
1275 struct lttng_channel
*channel
= file
->private_data
;
1278 fput(channel
->session
->file
);
1283 int lttng_metadata_channel_release(struct inode
*inode
, struct file
*file
)
1285 struct lttng_channel
*channel
= file
->private_data
;
1288 lttng_metadata_channel_destroy(channel
);
1289 fput(channel
->session
->file
);
1295 static const struct file_operations lttng_channel_fops
= {
1296 .owner
= THIS_MODULE
,
1297 .release
= lttng_channel_release
,
1298 .poll
= lttng_channel_poll
,
1299 .unlocked_ioctl
= lttng_channel_ioctl
,
1300 #ifdef CONFIG_COMPAT
1301 .compat_ioctl
= lttng_channel_ioctl
,
1305 static const struct file_operations lttng_metadata_fops
= {
1306 .owner
= THIS_MODULE
,
1307 .release
= lttng_metadata_channel_release
,
1308 .unlocked_ioctl
= lttng_metadata_ioctl
,
1309 #ifdef CONFIG_COMPAT
1310 .compat_ioctl
= lttng_metadata_ioctl
,
1315 * lttng_event_ioctl - lttng syscall through ioctl
1321 * This ioctl implements lttng commands:
1322 * LTTNG_KERNEL_CONTEXT
1323 * Prepend a context field to each record of this event
1324 * LTTNG_KERNEL_ENABLE
1325 * Enable recording for this event (weak enable)
1326 * LTTNG_KERNEL_DISABLE
1327 * Disable recording for this event (strong disable)
1330 long lttng_event_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1332 struct lttng_event
*event
;
1333 struct lttng_enabler
*enabler
;
1334 enum lttng_event_type
*evtype
= file
->private_data
;
1337 case LTTNG_KERNEL_OLD_CONTEXT
:
1339 /* Not implemented */
1342 case LTTNG_KERNEL_CONTEXT
:
1344 /* Not implemented */
1347 case LTTNG_KERNEL_OLD_ENABLE
:
1348 case LTTNG_KERNEL_ENABLE
:
1350 case LTTNG_TYPE_EVENT
:
1351 event
= file
->private_data
;
1352 return lttng_event_enable(event
);
1353 case LTTNG_TYPE_ENABLER
:
1354 enabler
= file
->private_data
;
1355 return lttng_enabler_enable(enabler
);
1360 case LTTNG_KERNEL_OLD_DISABLE
:
1361 case LTTNG_KERNEL_DISABLE
:
1363 case LTTNG_TYPE_EVENT
:
1364 event
= file
->private_data
;
1365 return lttng_event_disable(event
);
1366 case LTTNG_TYPE_ENABLER
:
1367 enabler
= file
->private_data
;
1368 return lttng_enabler_disable(enabler
);
1373 case LTTNG_KERNEL_FILTER
:
1375 case LTTNG_TYPE_EVENT
:
1377 case LTTNG_TYPE_ENABLER
:
1379 enabler
= file
->private_data
;
1380 return lttng_enabler_attach_bytecode(enabler
,
1381 (struct lttng_kernel_filter_bytecode __user
*) arg
);
1386 return -ENOIOCTLCMD
;
1391 int lttng_event_release(struct inode
*inode
, struct file
*file
)
1393 struct lttng_event
*event
;
1394 struct lttng_enabler
*enabler
;
1395 enum lttng_event_type
*evtype
= file
->private_data
;
1401 case LTTNG_TYPE_EVENT
:
1402 event
= file
->private_data
;
1404 fput(event
->chan
->file
);
1406 case LTTNG_TYPE_ENABLER
:
1407 enabler
= file
->private_data
;
1409 fput(enabler
->chan
->file
);
1419 /* TODO: filter control ioctl */
1420 static const struct file_operations lttng_event_fops
= {
1421 .owner
= THIS_MODULE
,
1422 .release
= lttng_event_release
,
1423 .unlocked_ioctl
= lttng_event_ioctl
,
1424 #ifdef CONFIG_COMPAT
1425 .compat_ioctl
= lttng_event_ioctl
,
1429 static int put_u64(uint64_t val
, unsigned long arg
)
1431 return put_user(val
, (uint64_t __user
*) arg
);
1434 static long lttng_stream_ring_buffer_ioctl(struct file
*filp
,
1435 unsigned int cmd
, unsigned long arg
)
1437 struct lib_ring_buffer
*buf
= filp
->private_data
;
1438 struct channel
*chan
= buf
->backend
.chan
;
1439 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1440 const struct lttng_channel_ops
*ops
= chan
->backend
.priv_ops
;
1443 if (atomic_read(&chan
->record_disabled
))
1447 case LTTNG_RING_BUFFER_GET_TIMESTAMP_BEGIN
:
1451 ret
= ops
->timestamp_begin(config
, buf
, &ts
);
1454 return put_u64(ts
, arg
);
1456 case LTTNG_RING_BUFFER_GET_TIMESTAMP_END
:
1460 ret
= ops
->timestamp_end(config
, buf
, &ts
);
1463 return put_u64(ts
, arg
);
1465 case LTTNG_RING_BUFFER_GET_EVENTS_DISCARDED
:
1469 ret
= ops
->events_discarded(config
, buf
, &ed
);
1472 return put_u64(ed
, arg
);
1474 case LTTNG_RING_BUFFER_GET_CONTENT_SIZE
:
1478 ret
= ops
->content_size(config
, buf
, &cs
);
1481 return put_u64(cs
, arg
);
1483 case LTTNG_RING_BUFFER_GET_PACKET_SIZE
:
1487 ret
= ops
->packet_size(config
, buf
, &ps
);
1490 return put_u64(ps
, arg
);
1492 case LTTNG_RING_BUFFER_GET_STREAM_ID
:
1496 ret
= ops
->stream_id(config
, buf
, &si
);
1499 return put_u64(si
, arg
);
1501 case LTTNG_RING_BUFFER_GET_CURRENT_TIMESTAMP
:
1505 ret
= ops
->current_timestamp(config
, buf
, &ts
);
1508 return put_u64(ts
, arg
);
1511 return lib_ring_buffer_file_operations
.unlocked_ioctl(filp
,
1519 #ifdef CONFIG_COMPAT
1520 static long lttng_stream_ring_buffer_compat_ioctl(struct file
*filp
,
1521 unsigned int cmd
, unsigned long arg
)
1523 struct lib_ring_buffer
*buf
= filp
->private_data
;
1524 struct channel
*chan
= buf
->backend
.chan
;
1525 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1526 const struct lttng_channel_ops
*ops
= chan
->backend
.priv_ops
;
1529 if (atomic_read(&chan
->record_disabled
))
1533 case LTTNG_RING_BUFFER_COMPAT_GET_TIMESTAMP_BEGIN
:
1537 ret
= ops
->timestamp_begin(config
, buf
, &ts
);
1540 return put_u64(ts
, arg
);
1542 case LTTNG_RING_BUFFER_COMPAT_GET_TIMESTAMP_END
:
1546 ret
= ops
->timestamp_end(config
, buf
, &ts
);
1549 return put_u64(ts
, arg
);
1551 case LTTNG_RING_BUFFER_COMPAT_GET_EVENTS_DISCARDED
:
1555 ret
= ops
->events_discarded(config
, buf
, &ed
);
1558 return put_u64(ed
, arg
);
1560 case LTTNG_RING_BUFFER_COMPAT_GET_CONTENT_SIZE
:
1564 ret
= ops
->content_size(config
, buf
, &cs
);
1567 return put_u64(cs
, arg
);
1569 case LTTNG_RING_BUFFER_COMPAT_GET_PACKET_SIZE
:
1573 ret
= ops
->packet_size(config
, buf
, &ps
);
1576 return put_u64(ps
, arg
);
1578 case LTTNG_RING_BUFFER_COMPAT_GET_STREAM_ID
:
1582 ret
= ops
->stream_id(config
, buf
, &si
);
1585 return put_u64(si
, arg
);
1587 case LTTNG_RING_BUFFER_GET_CURRENT_TIMESTAMP
:
1591 ret
= ops
->current_timestamp(config
, buf
, &ts
);
1594 return put_u64(ts
, arg
);
1597 return lib_ring_buffer_file_operations
.compat_ioctl(filp
,
1604 #endif /* CONFIG_COMPAT */
1606 static void lttng_stream_override_ring_buffer_fops(void)
1608 lttng_stream_ring_buffer_file_operations
.owner
= THIS_MODULE
;
1609 lttng_stream_ring_buffer_file_operations
.open
=
1610 lib_ring_buffer_file_operations
.open
;
1611 lttng_stream_ring_buffer_file_operations
.release
=
1612 lib_ring_buffer_file_operations
.release
;
1613 lttng_stream_ring_buffer_file_operations
.poll
=
1614 lib_ring_buffer_file_operations
.poll
;
1615 lttng_stream_ring_buffer_file_operations
.splice_read
=
1616 lib_ring_buffer_file_operations
.splice_read
;
1617 lttng_stream_ring_buffer_file_operations
.mmap
=
1618 lib_ring_buffer_file_operations
.mmap
;
1619 lttng_stream_ring_buffer_file_operations
.unlocked_ioctl
=
1620 lttng_stream_ring_buffer_ioctl
;
1621 lttng_stream_ring_buffer_file_operations
.llseek
=
1622 lib_ring_buffer_file_operations
.llseek
;
1623 #ifdef CONFIG_COMPAT
1624 lttng_stream_ring_buffer_file_operations
.compat_ioctl
=
1625 lttng_stream_ring_buffer_compat_ioctl
;
1629 int __init
lttng_abi_init(void)
1633 wrapper_vmalloc_sync_all();
1634 lttng_proc_dentry
= proc_create_data("lttng", S_IRUSR
| S_IWUSR
, NULL
,
1637 if (!lttng_proc_dentry
) {
1638 printk(KERN_ERR
"Error creating LTTng control file\n");
1642 lttng_stream_override_ring_buffer_fops();
1648 /* No __exit annotation because used by init error path too. */
1649 void lttng_abi_exit(void)
1651 if (lttng_proc_dentry
)
1652 remove_proc_entry("lttng", NULL
);