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
);
249 * lttng_ioctl - lttng syscall through ioctl
255 * This ioctl implements lttng commands:
256 * LTTNG_KERNEL_SESSION
257 * Returns a LTTng trace session file descriptor
258 * LTTNG_KERNEL_TRACER_VERSION
259 * Returns the LTTng kernel tracer version
260 * LTTNG_KERNEL_TRACEPOINT_LIST
261 * Returns a file descriptor listing available tracepoints
262 * LTTNG_KERNEL_WAIT_QUIESCENT
263 * Returns after all previously running probes have completed
264 * LTTNG_KERNEL_TRACER_ABI_VERSION
265 * Returns the LTTng kernel tracer ABI version
267 * The returned session will be deleted when its file descriptor is closed.
270 long lttng_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
273 case LTTNG_KERNEL_OLD_SESSION
:
274 case LTTNG_KERNEL_SESSION
:
275 return lttng_abi_create_session();
276 case LTTNG_KERNEL_OLD_TRACER_VERSION
:
278 struct lttng_kernel_tracer_version v
;
279 struct lttng_kernel_old_tracer_version oldv
;
280 struct lttng_kernel_old_tracer_version
*uversion
=
281 (struct lttng_kernel_old_tracer_version __user
*) arg
;
283 lttng_abi_tracer_version(&v
);
284 oldv
.major
= v
.major
;
285 oldv
.minor
= v
.minor
;
286 oldv
.patchlevel
= v
.patchlevel
;
288 if (copy_to_user(uversion
, &oldv
, sizeof(oldv
)))
292 case LTTNG_KERNEL_TRACER_VERSION
:
294 struct lttng_kernel_tracer_version version
;
295 struct lttng_kernel_tracer_version
*uversion
=
296 (struct lttng_kernel_tracer_version __user
*) arg
;
298 lttng_abi_tracer_version(&version
);
300 if (copy_to_user(uversion
, &version
, sizeof(version
)))
304 case LTTNG_KERNEL_TRACER_ABI_VERSION
:
306 struct lttng_kernel_tracer_abi_version version
;
307 struct lttng_kernel_tracer_abi_version
*uversion
=
308 (struct lttng_kernel_tracer_abi_version __user
*) arg
;
310 lttng_abi_tracer_abi_version(&version
);
312 if (copy_to_user(uversion
, &version
, sizeof(version
)))
316 case LTTNG_KERNEL_OLD_TRACEPOINT_LIST
:
317 case LTTNG_KERNEL_TRACEPOINT_LIST
:
318 return lttng_abi_tracepoint_list();
319 case LTTNG_KERNEL_SYSCALL_LIST
:
320 return lttng_abi_syscall_list();
321 case LTTNG_KERNEL_OLD_WAIT_QUIESCENT
:
322 case LTTNG_KERNEL_WAIT_QUIESCENT
:
325 case LTTNG_KERNEL_OLD_CALIBRATE
:
327 struct lttng_kernel_old_calibrate __user
*ucalibrate
=
328 (struct lttng_kernel_old_calibrate __user
*) arg
;
329 struct lttng_kernel_old_calibrate old_calibrate
;
330 struct lttng_kernel_calibrate calibrate
;
333 if (copy_from_user(&old_calibrate
, ucalibrate
, sizeof(old_calibrate
)))
335 calibrate
.type
= old_calibrate
.type
;
336 ret
= lttng_calibrate(&calibrate
);
337 if (copy_to_user(ucalibrate
, &old_calibrate
, sizeof(old_calibrate
)))
341 case LTTNG_KERNEL_CALIBRATE
:
343 struct lttng_kernel_calibrate __user
*ucalibrate
=
344 (struct lttng_kernel_calibrate __user
*) arg
;
345 struct lttng_kernel_calibrate calibrate
;
348 if (copy_from_user(&calibrate
, ucalibrate
, sizeof(calibrate
)))
350 ret
= lttng_calibrate(&calibrate
);
351 if (copy_to_user(ucalibrate
, &calibrate
, sizeof(calibrate
)))
360 static const struct file_operations lttng_fops
= {
361 .owner
= THIS_MODULE
,
362 .unlocked_ioctl
= lttng_ioctl
,
364 .compat_ioctl
= lttng_ioctl
,
369 int lttng_abi_create_channel(struct file
*session_file
,
370 struct lttng_kernel_channel
*chan_param
,
371 enum channel_type channel_type
)
373 struct lttng_session
*session
= session_file
->private_data
;
374 const struct file_operations
*fops
= NULL
;
375 const char *transport_name
;
376 struct lttng_channel
*chan
;
377 struct file
*chan_file
;
381 chan_fd
= lttng_get_unused_fd();
386 switch (channel_type
) {
387 case PER_CPU_CHANNEL
:
388 fops
= <tng_channel_fops
;
390 case METADATA_CHANNEL
:
391 fops
= <tng_metadata_fops
;
395 chan_file
= anon_inode_getfile("[lttng_channel]",
398 if (IS_ERR(chan_file
)) {
399 ret
= PTR_ERR(chan_file
);
402 switch (channel_type
) {
403 case PER_CPU_CHANNEL
:
404 if (chan_param
->output
== LTTNG_KERNEL_SPLICE
) {
405 transport_name
= chan_param
->overwrite
?
406 "relay-overwrite" : "relay-discard";
407 } else if (chan_param
->output
== LTTNG_KERNEL_MMAP
) {
408 transport_name
= chan_param
->overwrite
?
409 "relay-overwrite-mmap" : "relay-discard-mmap";
414 case METADATA_CHANNEL
:
415 if (chan_param
->output
== LTTNG_KERNEL_SPLICE
)
416 transport_name
= "relay-metadata";
417 else if (chan_param
->output
== LTTNG_KERNEL_MMAP
)
418 transport_name
= "relay-metadata-mmap";
423 transport_name
= "<unknown>";
427 * We tolerate no failure path after channel creation. It will stay
428 * invariant for the rest of the session.
430 chan
= lttng_channel_create(session
, transport_name
, NULL
,
431 chan_param
->subbuf_size
,
432 chan_param
->num_subbuf
,
433 chan_param
->switch_timer_interval
,
434 chan_param
->read_timer_interval
,
440 chan
->file
= chan_file
;
441 chan_file
->private_data
= chan
;
442 fd_install(chan_fd
, chan_file
);
443 atomic_long_inc(&session_file
->f_count
);
450 put_unused_fd(chan_fd
);
456 * lttng_session_ioctl - lttng session fd ioctl
462 * This ioctl implements lttng commands:
463 * LTTNG_KERNEL_CHANNEL
464 * Returns a LTTng channel file descriptor
465 * LTTNG_KERNEL_ENABLE
466 * Enables tracing for a session (weak enable)
467 * LTTNG_KERNEL_DISABLE
468 * Disables tracing for a session (strong disable)
469 * LTTNG_KERNEL_METADATA
470 * Returns a LTTng metadata file descriptor
471 * LTTNG_KERNEL_SESSION_TRACK_PID
472 * Add PID to session tracker
473 * LTTNG_KERNEL_SESSION_UNTRACK_PID
474 * Remove PID from session tracker
476 * The returned channel will be deleted when its file descriptor is closed.
479 long lttng_session_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
481 struct lttng_session
*session
= file
->private_data
;
484 case LTTNG_KERNEL_OLD_CHANNEL
:
486 struct lttng_kernel_channel chan_param
;
487 struct lttng_kernel_old_channel old_chan_param
;
489 if (copy_from_user(&old_chan_param
,
490 (struct lttng_kernel_old_channel __user
*) arg
,
491 sizeof(struct lttng_kernel_old_channel
)))
493 chan_param
.overwrite
= old_chan_param
.overwrite
;
494 chan_param
.subbuf_size
= old_chan_param
.subbuf_size
;
495 chan_param
.num_subbuf
= old_chan_param
.num_subbuf
;
496 chan_param
.switch_timer_interval
= old_chan_param
.switch_timer_interval
;
497 chan_param
.read_timer_interval
= old_chan_param
.read_timer_interval
;
498 chan_param
.output
= old_chan_param
.output
;
500 return lttng_abi_create_channel(file
, &chan_param
,
503 case LTTNG_KERNEL_CHANNEL
:
505 struct lttng_kernel_channel chan_param
;
507 if (copy_from_user(&chan_param
,
508 (struct lttng_kernel_channel __user
*) arg
,
509 sizeof(struct lttng_kernel_channel
)))
511 return lttng_abi_create_channel(file
, &chan_param
,
514 case LTTNG_KERNEL_OLD_SESSION_START
:
515 case LTTNG_KERNEL_OLD_ENABLE
:
516 case LTTNG_KERNEL_SESSION_START
:
517 case LTTNG_KERNEL_ENABLE
:
518 return lttng_session_enable(session
);
519 case LTTNG_KERNEL_OLD_SESSION_STOP
:
520 case LTTNG_KERNEL_OLD_DISABLE
:
521 case LTTNG_KERNEL_SESSION_STOP
:
522 case LTTNG_KERNEL_DISABLE
:
523 return lttng_session_disable(session
);
524 case LTTNG_KERNEL_OLD_METADATA
:
526 struct lttng_kernel_channel chan_param
;
527 struct lttng_kernel_old_channel old_chan_param
;
529 if (copy_from_user(&old_chan_param
,
530 (struct lttng_kernel_old_channel __user
*) arg
,
531 sizeof(struct lttng_kernel_old_channel
)))
533 chan_param
.overwrite
= old_chan_param
.overwrite
;
534 chan_param
.subbuf_size
= old_chan_param
.subbuf_size
;
535 chan_param
.num_subbuf
= old_chan_param
.num_subbuf
;
536 chan_param
.switch_timer_interval
= old_chan_param
.switch_timer_interval
;
537 chan_param
.read_timer_interval
= old_chan_param
.read_timer_interval
;
538 chan_param
.output
= old_chan_param
.output
;
540 return lttng_abi_create_channel(file
, &chan_param
,
543 case LTTNG_KERNEL_METADATA
:
545 struct lttng_kernel_channel chan_param
;
547 if (copy_from_user(&chan_param
,
548 (struct lttng_kernel_channel __user
*) arg
,
549 sizeof(struct lttng_kernel_channel
)))
551 return lttng_abi_create_channel(file
, &chan_param
,
554 case LTTNG_KERNEL_SESSION_TRACK_PID
:
555 return lttng_session_track_pid(session
, (int) arg
);
556 case LTTNG_KERNEL_SESSION_UNTRACK_PID
:
557 return lttng_session_untrack_pid(session
, (int) arg
);
558 case LTTNG_KERNEL_SESSION_LIST_TRACKER_PIDS
:
559 return lttng_session_list_tracker_pids(session
);
566 * Called when the last file reference is dropped.
568 * Big fat note: channels and events are invariant for the whole session after
569 * their creation. So this session destruction also destroys all channel and
570 * event structures specific to this session (they are not destroyed when their
571 * individual file is released).
574 int lttng_session_release(struct inode
*inode
, struct file
*file
)
576 struct lttng_session
*session
= file
->private_data
;
579 lttng_session_destroy(session
);
583 static const struct file_operations lttng_session_fops
= {
584 .owner
= THIS_MODULE
,
585 .release
= lttng_session_release
,
586 .unlocked_ioctl
= lttng_session_ioctl
,
588 .compat_ioctl
= lttng_session_ioctl
,
593 * lttng_metadata_ring_buffer_poll - LTTng ring buffer poll file operation
597 * Handles the poll operations for the metadata channels.
600 unsigned int lttng_metadata_ring_buffer_poll(struct file
*filp
,
603 struct lttng_metadata_stream
*stream
= filp
->private_data
;
604 struct lib_ring_buffer
*buf
= stream
->priv
;
606 unsigned int mask
= 0;
608 if (filp
->f_mode
& FMODE_READ
) {
609 poll_wait_set_exclusive(wait
);
610 poll_wait(filp
, &stream
->read_wait
, wait
);
612 finalized
= stream
->finalized
;
615 * lib_ring_buffer_is_finalized() contains a smp_rmb()
616 * ordering finalized load before offsets loads.
618 WARN_ON(atomic_long_read(&buf
->active_readers
) != 1);
623 if (stream
->metadata_cache
->metadata_written
>
624 stream
->metadata_out
)
632 void lttng_metadata_ring_buffer_ioctl_put_next_subbuf(struct file
*filp
,
633 unsigned int cmd
, unsigned long arg
)
635 struct lttng_metadata_stream
*stream
= filp
->private_data
;
637 stream
->metadata_out
= stream
->metadata_in
;
641 long lttng_metadata_ring_buffer_ioctl(struct file
*filp
,
642 unsigned int cmd
, unsigned long arg
)
645 struct lttng_metadata_stream
*stream
= filp
->private_data
;
646 struct lib_ring_buffer
*buf
= stream
->priv
;
649 case RING_BUFFER_GET_NEXT_SUBBUF
:
651 struct lttng_metadata_stream
*stream
= filp
->private_data
;
652 struct lib_ring_buffer
*buf
= stream
->priv
;
653 struct channel
*chan
= buf
->backend
.chan
;
655 ret
= lttng_metadata_output_channel(stream
, chan
);
657 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
663 case RING_BUFFER_GET_SUBBUF
:
666 * Random access is not allowed for metadata channel.
670 case RING_BUFFER_FLUSH
:
672 struct lttng_metadata_stream
*stream
= filp
->private_data
;
673 struct lib_ring_buffer
*buf
= stream
->priv
;
674 struct channel
*chan
= buf
->backend
.chan
;
677 * Before doing the actual ring buffer flush, write up to one
678 * packet of metadata in the ring buffer.
680 ret
= lttng_metadata_output_channel(stream
, chan
);
688 /* PUT_SUBBUF is the one from lib ring buffer, unmodified. */
690 /* Performing lib ring buffer ioctl after our own. */
691 ret
= lib_ring_buffer_ioctl(filp
, cmd
, arg
, buf
);
696 case RING_BUFFER_PUT_NEXT_SUBBUF
:
698 lttng_metadata_ring_buffer_ioctl_put_next_subbuf(filp
,
711 long lttng_metadata_ring_buffer_compat_ioctl(struct file
*filp
,
712 unsigned int cmd
, unsigned long arg
)
715 struct lttng_metadata_stream
*stream
= filp
->private_data
;
716 struct lib_ring_buffer
*buf
= stream
->priv
;
719 case RING_BUFFER_GET_NEXT_SUBBUF
:
721 struct lttng_metadata_stream
*stream
= filp
->private_data
;
722 struct lib_ring_buffer
*buf
= stream
->priv
;
723 struct channel
*chan
= buf
->backend
.chan
;
725 ret
= lttng_metadata_output_channel(stream
, chan
);
727 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
733 case RING_BUFFER_GET_SUBBUF
:
736 * Random access is not allowed for metadata channel.
743 /* PUT_SUBBUF is the one from lib ring buffer, unmodified. */
745 /* Performing lib ring buffer ioctl after our own. */
746 ret
= lib_ring_buffer_compat_ioctl(filp
, cmd
, arg
, buf
);
751 case RING_BUFFER_PUT_NEXT_SUBBUF
:
753 lttng_metadata_ring_buffer_ioctl_put_next_subbuf(filp
,
766 * This is not used by anonymous file descriptors. This code is left
767 * there if we ever want to implement an inode with open() operation.
770 int lttng_metadata_ring_buffer_open(struct inode
*inode
, struct file
*file
)
772 struct lttng_metadata_stream
*stream
= inode
->i_private
;
773 struct lib_ring_buffer
*buf
= stream
->priv
;
775 file
->private_data
= buf
;
777 * Since life-time of metadata cache differs from that of
778 * session, we need to keep our own reference on the transport.
780 if (!try_module_get(stream
->transport
->owner
)) {
781 printk(KERN_WARNING
"LTT : Can't lock transport module.\n");
784 return lib_ring_buffer_open(inode
, file
, buf
);
788 int lttng_metadata_ring_buffer_release(struct inode
*inode
, struct file
*file
)
790 struct lttng_metadata_stream
*stream
= file
->private_data
;
791 struct lib_ring_buffer
*buf
= stream
->priv
;
793 kref_put(&stream
->metadata_cache
->refcount
, metadata_cache_destroy
);
794 module_put(stream
->transport
->owner
);
795 return lib_ring_buffer_release(inode
, file
, buf
);
799 ssize_t
lttng_metadata_ring_buffer_splice_read(struct file
*in
, loff_t
*ppos
,
800 struct pipe_inode_info
*pipe
, size_t len
,
803 struct lttng_metadata_stream
*stream
= in
->private_data
;
804 struct lib_ring_buffer
*buf
= stream
->priv
;
806 return lib_ring_buffer_splice_read(in
, ppos
, pipe
, len
,
811 int lttng_metadata_ring_buffer_mmap(struct file
*filp
,
812 struct vm_area_struct
*vma
)
814 struct lttng_metadata_stream
*stream
= filp
->private_data
;
815 struct lib_ring_buffer
*buf
= stream
->priv
;
817 return lib_ring_buffer_mmap(filp
, vma
, buf
);
821 const struct file_operations lttng_metadata_ring_buffer_file_operations
= {
822 .owner
= THIS_MODULE
,
823 .open
= lttng_metadata_ring_buffer_open
,
824 .release
= lttng_metadata_ring_buffer_release
,
825 .poll
= lttng_metadata_ring_buffer_poll
,
826 .splice_read
= lttng_metadata_ring_buffer_splice_read
,
827 .mmap
= lttng_metadata_ring_buffer_mmap
,
828 .unlocked_ioctl
= lttng_metadata_ring_buffer_ioctl
,
829 .llseek
= vfs_lib_ring_buffer_no_llseek
,
831 .compat_ioctl
= lttng_metadata_ring_buffer_compat_ioctl
,
836 int lttng_abi_create_stream_fd(struct file
*channel_file
, void *stream_priv
,
837 const struct file_operations
*fops
)
840 struct file
*stream_file
;
842 stream_fd
= lttng_get_unused_fd();
847 stream_file
= anon_inode_getfile("[lttng_stream]", fops
,
848 stream_priv
, O_RDWR
);
849 if (IS_ERR(stream_file
)) {
850 ret
= PTR_ERR(stream_file
);
854 * OPEN_FMODE, called within anon_inode_getfile/alloc_file, don't honor
855 * FMODE_LSEEK, FMODE_PREAD nor FMODE_PWRITE. We need to read from this
856 * file descriptor, so we set FMODE_PREAD here.
858 stream_file
->f_mode
|= FMODE_PREAD
;
859 fd_install(stream_fd
, stream_file
);
861 * The stream holds a reference to the channel within the generic ring
862 * buffer library, so no need to hold a refcount on the channel and
863 * session files here.
868 put_unused_fd(stream_fd
);
874 int lttng_abi_open_stream(struct file
*channel_file
)
876 struct lttng_channel
*channel
= channel_file
->private_data
;
877 struct lib_ring_buffer
*buf
;
881 buf
= channel
->ops
->buffer_read_open(channel
->chan
);
886 ret
= lttng_abi_create_stream_fd(channel_file
, stream_priv
,
887 <tng_stream_ring_buffer_file_operations
);
894 channel
->ops
->buffer_read_close(buf
);
899 int lttng_abi_open_metadata_stream(struct file
*channel_file
)
901 struct lttng_channel
*channel
= channel_file
->private_data
;
902 struct lttng_session
*session
= channel
->session
;
903 struct lib_ring_buffer
*buf
;
905 struct lttng_metadata_stream
*metadata_stream
;
908 buf
= channel
->ops
->buffer_read_open(channel
->chan
);
912 metadata_stream
= kzalloc(sizeof(struct lttng_metadata_stream
),
914 if (!metadata_stream
) {
918 metadata_stream
->metadata_cache
= session
->metadata_cache
;
919 init_waitqueue_head(&metadata_stream
->read_wait
);
920 metadata_stream
->priv
= buf
;
921 stream_priv
= metadata_stream
;
922 metadata_stream
->transport
= channel
->transport
;
923 mutex_init(&metadata_stream
->lock
);
926 * Since life-time of metadata cache differs from that of
927 * session, we need to keep our own reference on the transport.
929 if (!try_module_get(metadata_stream
->transport
->owner
)) {
930 printk(KERN_WARNING
"LTT : Can't lock transport module.\n");
935 ret
= lttng_abi_create_stream_fd(channel_file
, stream_priv
,
936 <tng_metadata_ring_buffer_file_operations
);
940 kref_get(&session
->metadata_cache
->refcount
);
941 list_add(&metadata_stream
->list
,
942 &session
->metadata_cache
->metadata_stream
);
946 module_put(metadata_stream
->transport
->owner
);
948 kfree(metadata_stream
);
950 channel
->ops
->buffer_read_close(buf
);
955 int lttng_abi_create_event(struct file
*channel_file
,
956 struct lttng_kernel_event
*event_param
)
958 struct lttng_channel
*channel
= channel_file
->private_data
;
960 struct file
*event_file
;
963 event_param
->name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
964 switch (event_param
->instrumentation
) {
965 case LTTNG_KERNEL_KRETPROBE
:
966 event_param
->u
.kretprobe
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
968 case LTTNG_KERNEL_KPROBE
:
969 event_param
->u
.kprobe
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
971 case LTTNG_KERNEL_FUNCTION
:
972 event_param
->u
.ftrace
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
977 event_fd
= lttng_get_unused_fd();
982 event_file
= anon_inode_getfile("[lttng_event]",
985 if (IS_ERR(event_file
)) {
986 ret
= PTR_ERR(event_file
);
989 if (event_param
->instrumentation
== LTTNG_KERNEL_TRACEPOINT
990 || event_param
->instrumentation
== LTTNG_KERNEL_SYSCALL
) {
991 struct lttng_enabler
*enabler
;
993 if (event_param
->name
[strlen(event_param
->name
) - 1] == '*') {
994 enabler
= lttng_enabler_create(LTTNG_ENABLER_WILDCARD
,
995 event_param
, channel
);
997 enabler
= lttng_enabler_create(LTTNG_ENABLER_NAME
,
998 event_param
, channel
);
1002 struct lttng_event
*event
;
1005 * We tolerate no failure path after event creation. It
1006 * will stay invariant for the rest of the session.
1008 event
= lttng_event_create(channel
, event_param
,
1010 event_param
->instrumentation
);
1011 WARN_ON_ONCE(!event
);
1012 if (IS_ERR(event
)) {
1013 ret
= PTR_ERR(event
);
1018 event_file
->private_data
= priv
;
1019 fd_install(event_fd
, event_file
);
1020 /* The event holds a reference on the channel */
1021 atomic_long_inc(&channel_file
->f_count
);
1027 put_unused_fd(event_fd
);
1033 * lttng_channel_ioctl - lttng syscall through ioctl
1039 * This ioctl implements lttng commands:
1040 * LTTNG_KERNEL_STREAM
1041 * Returns an event stream file descriptor or failure.
1042 * (typically, one event stream records events from one CPU)
1043 * LTTNG_KERNEL_EVENT
1044 * Returns an event file descriptor or failure.
1045 * LTTNG_KERNEL_CONTEXT
1046 * Prepend a context field to each event in the channel
1047 * LTTNG_KERNEL_ENABLE
1048 * Enable recording for events in this channel (weak enable)
1049 * LTTNG_KERNEL_DISABLE
1050 * Disable recording for events in this channel (strong disable)
1052 * Channel and event file descriptors also hold a reference on the session.
1055 long lttng_channel_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1057 struct lttng_channel
*channel
= file
->private_data
;
1060 case LTTNG_KERNEL_OLD_STREAM
:
1061 case LTTNG_KERNEL_STREAM
:
1062 return lttng_abi_open_stream(file
);
1063 case LTTNG_KERNEL_OLD_EVENT
:
1065 struct lttng_kernel_event
*uevent_param
;
1066 struct lttng_kernel_old_event
*old_uevent_param
;
1069 uevent_param
= kmalloc(sizeof(struct lttng_kernel_event
),
1071 if (!uevent_param
) {
1075 old_uevent_param
= kmalloc(
1076 sizeof(struct lttng_kernel_old_event
),
1078 if (!old_uevent_param
) {
1080 goto old_event_error_free_param
;
1082 if (copy_from_user(old_uevent_param
,
1083 (struct lttng_kernel_old_event __user
*) arg
,
1084 sizeof(struct lttng_kernel_old_event
))) {
1086 goto old_event_error_free_old_param
;
1089 memcpy(uevent_param
->name
, old_uevent_param
->name
,
1090 sizeof(uevent_param
->name
));
1091 uevent_param
->instrumentation
=
1092 old_uevent_param
->instrumentation
;
1094 switch (old_uevent_param
->instrumentation
) {
1095 case LTTNG_KERNEL_KPROBE
:
1096 uevent_param
->u
.kprobe
.addr
=
1097 old_uevent_param
->u
.kprobe
.addr
;
1098 uevent_param
->u
.kprobe
.offset
=
1099 old_uevent_param
->u
.kprobe
.offset
;
1100 memcpy(uevent_param
->u
.kprobe
.symbol_name
,
1101 old_uevent_param
->u
.kprobe
.symbol_name
,
1102 sizeof(uevent_param
->u
.kprobe
.symbol_name
));
1104 case LTTNG_KERNEL_KRETPROBE
:
1105 uevent_param
->u
.kretprobe
.addr
=
1106 old_uevent_param
->u
.kretprobe
.addr
;
1107 uevent_param
->u
.kretprobe
.offset
=
1108 old_uevent_param
->u
.kretprobe
.offset
;
1109 memcpy(uevent_param
->u
.kretprobe
.symbol_name
,
1110 old_uevent_param
->u
.kretprobe
.symbol_name
,
1111 sizeof(uevent_param
->u
.kretprobe
.symbol_name
));
1113 case LTTNG_KERNEL_FUNCTION
:
1114 memcpy(uevent_param
->u
.ftrace
.symbol_name
,
1115 old_uevent_param
->u
.ftrace
.symbol_name
,
1116 sizeof(uevent_param
->u
.ftrace
.symbol_name
));
1121 ret
= lttng_abi_create_event(file
, uevent_param
);
1123 old_event_error_free_old_param
:
1124 kfree(old_uevent_param
);
1125 old_event_error_free_param
:
1126 kfree(uevent_param
);
1130 case LTTNG_KERNEL_EVENT
:
1132 struct lttng_kernel_event uevent_param
;
1134 if (copy_from_user(&uevent_param
,
1135 (struct lttng_kernel_event __user
*) arg
,
1136 sizeof(uevent_param
)))
1138 return lttng_abi_create_event(file
, &uevent_param
);
1140 case LTTNG_KERNEL_OLD_CONTEXT
:
1142 struct lttng_kernel_context
*ucontext_param
;
1143 struct lttng_kernel_old_context
*old_ucontext_param
;
1146 ucontext_param
= kmalloc(sizeof(struct lttng_kernel_context
),
1148 if (!ucontext_param
) {
1152 old_ucontext_param
= kmalloc(sizeof(struct lttng_kernel_old_context
),
1154 if (!old_ucontext_param
) {
1156 goto old_ctx_error_free_param
;
1159 if (copy_from_user(old_ucontext_param
,
1160 (struct lttng_kernel_old_context __user
*) arg
,
1161 sizeof(struct lttng_kernel_old_context
))) {
1163 goto old_ctx_error_free_old_param
;
1165 ucontext_param
->ctx
= old_ucontext_param
->ctx
;
1166 memcpy(ucontext_param
->padding
, old_ucontext_param
->padding
,
1167 sizeof(ucontext_param
->padding
));
1168 /* only type that uses the union */
1169 if (old_ucontext_param
->ctx
== LTTNG_KERNEL_CONTEXT_PERF_COUNTER
) {
1170 ucontext_param
->u
.perf_counter
.type
=
1171 old_ucontext_param
->u
.perf_counter
.type
;
1172 ucontext_param
->u
.perf_counter
.config
=
1173 old_ucontext_param
->u
.perf_counter
.config
;
1174 memcpy(ucontext_param
->u
.perf_counter
.name
,
1175 old_ucontext_param
->u
.perf_counter
.name
,
1176 sizeof(ucontext_param
->u
.perf_counter
.name
));
1179 ret
= lttng_abi_add_context(file
,
1181 &channel
->ctx
, channel
->session
);
1183 old_ctx_error_free_old_param
:
1184 kfree(old_ucontext_param
);
1185 old_ctx_error_free_param
:
1186 kfree(ucontext_param
);
1190 case LTTNG_KERNEL_CONTEXT
:
1192 struct lttng_kernel_context ucontext_param
;
1194 if (copy_from_user(&ucontext_param
,
1195 (struct lttng_kernel_context __user
*) arg
,
1196 sizeof(ucontext_param
)))
1198 return lttng_abi_add_context(file
,
1200 &channel
->ctx
, channel
->session
);
1202 case LTTNG_KERNEL_OLD_ENABLE
:
1203 case LTTNG_KERNEL_ENABLE
:
1204 return lttng_channel_enable(channel
);
1205 case LTTNG_KERNEL_OLD_DISABLE
:
1206 case LTTNG_KERNEL_DISABLE
:
1207 return lttng_channel_disable(channel
);
1208 case LTTNG_KERNEL_SYSCALL_MASK
:
1209 return lttng_channel_syscall_mask(channel
,
1210 (struct lttng_kernel_syscall_mask __user
*) arg
);
1212 return -ENOIOCTLCMD
;
1218 * lttng_metadata_ioctl - lttng syscall through ioctl
1224 * This ioctl implements lttng commands:
1225 * LTTNG_KERNEL_STREAM
1226 * Returns an event stream file descriptor or failure.
1228 * Channel and event file descriptors also hold a reference on the session.
1231 long lttng_metadata_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1234 case LTTNG_KERNEL_OLD_STREAM
:
1235 case LTTNG_KERNEL_STREAM
:
1236 return lttng_abi_open_metadata_stream(file
);
1238 return -ENOIOCTLCMD
;
1243 * lttng_channel_poll - lttng stream addition/removal monitoring
1248 unsigned int lttng_channel_poll(struct file
*file
, poll_table
*wait
)
1250 struct lttng_channel
*channel
= file
->private_data
;
1251 unsigned int mask
= 0;
1253 if (file
->f_mode
& FMODE_READ
) {
1254 poll_wait_set_exclusive(wait
);
1255 poll_wait(file
, channel
->ops
->get_hp_wait_queue(channel
->chan
),
1258 if (channel
->ops
->is_disabled(channel
->chan
))
1260 if (channel
->ops
->is_finalized(channel
->chan
))
1262 if (channel
->ops
->buffer_has_read_closed_stream(channel
->chan
))
1263 return POLLIN
| POLLRDNORM
;
1271 int lttng_channel_release(struct inode
*inode
, struct file
*file
)
1273 struct lttng_channel
*channel
= file
->private_data
;
1276 fput(channel
->session
->file
);
1281 int lttng_metadata_channel_release(struct inode
*inode
, struct file
*file
)
1283 struct lttng_channel
*channel
= file
->private_data
;
1286 lttng_metadata_channel_destroy(channel
);
1287 fput(channel
->session
->file
);
1293 static const struct file_operations lttng_channel_fops
= {
1294 .owner
= THIS_MODULE
,
1295 .release
= lttng_channel_release
,
1296 .poll
= lttng_channel_poll
,
1297 .unlocked_ioctl
= lttng_channel_ioctl
,
1298 #ifdef CONFIG_COMPAT
1299 .compat_ioctl
= lttng_channel_ioctl
,
1303 static const struct file_operations lttng_metadata_fops
= {
1304 .owner
= THIS_MODULE
,
1305 .release
= lttng_metadata_channel_release
,
1306 .unlocked_ioctl
= lttng_metadata_ioctl
,
1307 #ifdef CONFIG_COMPAT
1308 .compat_ioctl
= lttng_metadata_ioctl
,
1313 * lttng_event_ioctl - lttng syscall through ioctl
1319 * This ioctl implements lttng commands:
1320 * LTTNG_KERNEL_CONTEXT
1321 * Prepend a context field to each record of this event
1322 * LTTNG_KERNEL_ENABLE
1323 * Enable recording for this event (weak enable)
1324 * LTTNG_KERNEL_DISABLE
1325 * Disable recording for this event (strong disable)
1328 long lttng_event_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1330 struct lttng_event
*event
;
1331 struct lttng_enabler
*enabler
;
1332 enum lttng_event_type
*evtype
= file
->private_data
;
1335 case LTTNG_KERNEL_OLD_CONTEXT
:
1337 /* Not implemented */
1340 case LTTNG_KERNEL_CONTEXT
:
1342 /* Not implemented */
1345 case LTTNG_KERNEL_OLD_ENABLE
:
1346 case LTTNG_KERNEL_ENABLE
:
1348 case LTTNG_TYPE_EVENT
:
1349 event
= file
->private_data
;
1350 return lttng_event_enable(event
);
1351 case LTTNG_TYPE_ENABLER
:
1352 enabler
= file
->private_data
;
1353 return lttng_enabler_enable(enabler
);
1358 case LTTNG_KERNEL_OLD_DISABLE
:
1359 case LTTNG_KERNEL_DISABLE
:
1361 case LTTNG_TYPE_EVENT
:
1362 event
= file
->private_data
;
1363 return lttng_event_disable(event
);
1364 case LTTNG_TYPE_ENABLER
:
1365 enabler
= file
->private_data
;
1366 return lttng_enabler_disable(enabler
);
1371 case LTTNG_KERNEL_FILTER
:
1373 case LTTNG_TYPE_EVENT
:
1375 case LTTNG_TYPE_ENABLER
:
1377 enabler
= file
->private_data
;
1378 return lttng_enabler_attach_bytecode(enabler
,
1379 (struct lttng_kernel_filter_bytecode __user
*) arg
);
1384 return -ENOIOCTLCMD
;
1389 int lttng_event_release(struct inode
*inode
, struct file
*file
)
1391 struct lttng_event
*event
;
1392 struct lttng_enabler
*enabler
;
1393 enum lttng_event_type
*evtype
= file
->private_data
;
1399 case LTTNG_TYPE_EVENT
:
1400 event
= file
->private_data
;
1402 fput(event
->chan
->file
);
1404 case LTTNG_TYPE_ENABLER
:
1405 enabler
= file
->private_data
;
1407 fput(enabler
->chan
->file
);
1417 /* TODO: filter control ioctl */
1418 static const struct file_operations lttng_event_fops
= {
1419 .owner
= THIS_MODULE
,
1420 .release
= lttng_event_release
,
1421 .unlocked_ioctl
= lttng_event_ioctl
,
1422 #ifdef CONFIG_COMPAT
1423 .compat_ioctl
= lttng_event_ioctl
,
1427 static int put_u64(uint64_t val
, unsigned long arg
)
1429 return put_user(val
, (uint64_t __user
*) arg
);
1432 static long lttng_stream_ring_buffer_ioctl(struct file
*filp
,
1433 unsigned int cmd
, unsigned long arg
)
1435 struct lib_ring_buffer
*buf
= filp
->private_data
;
1436 struct channel
*chan
= buf
->backend
.chan
;
1437 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1438 const struct lttng_channel_ops
*ops
= chan
->backend
.priv_ops
;
1441 if (atomic_read(&chan
->record_disabled
))
1445 case LTTNG_RING_BUFFER_GET_TIMESTAMP_BEGIN
:
1449 ret
= ops
->timestamp_begin(config
, buf
, &ts
);
1452 return put_u64(ts
, arg
);
1454 case LTTNG_RING_BUFFER_GET_TIMESTAMP_END
:
1458 ret
= ops
->timestamp_end(config
, buf
, &ts
);
1461 return put_u64(ts
, arg
);
1463 case LTTNG_RING_BUFFER_GET_EVENTS_DISCARDED
:
1467 ret
= ops
->events_discarded(config
, buf
, &ed
);
1470 return put_u64(ed
, arg
);
1472 case LTTNG_RING_BUFFER_GET_CONTENT_SIZE
:
1476 ret
= ops
->content_size(config
, buf
, &cs
);
1479 return put_u64(cs
, arg
);
1481 case LTTNG_RING_BUFFER_GET_PACKET_SIZE
:
1485 ret
= ops
->packet_size(config
, buf
, &ps
);
1488 return put_u64(ps
, arg
);
1490 case LTTNG_RING_BUFFER_GET_STREAM_ID
:
1494 ret
= ops
->stream_id(config
, buf
, &si
);
1497 return put_u64(si
, arg
);
1499 case LTTNG_RING_BUFFER_GET_CURRENT_TIMESTAMP
:
1503 ret
= ops
->current_timestamp(config
, buf
, &ts
);
1506 return put_u64(ts
, arg
);
1509 return lib_ring_buffer_file_operations
.unlocked_ioctl(filp
,
1517 #ifdef CONFIG_COMPAT
1518 static long lttng_stream_ring_buffer_compat_ioctl(struct file
*filp
,
1519 unsigned int cmd
, unsigned long arg
)
1521 struct lib_ring_buffer
*buf
= filp
->private_data
;
1522 struct channel
*chan
= buf
->backend
.chan
;
1523 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1524 const struct lttng_channel_ops
*ops
= chan
->backend
.priv_ops
;
1527 if (atomic_read(&chan
->record_disabled
))
1531 case LTTNG_RING_BUFFER_COMPAT_GET_TIMESTAMP_BEGIN
:
1535 ret
= ops
->timestamp_begin(config
, buf
, &ts
);
1538 return put_u64(ts
, arg
);
1540 case LTTNG_RING_BUFFER_COMPAT_GET_TIMESTAMP_END
:
1544 ret
= ops
->timestamp_end(config
, buf
, &ts
);
1547 return put_u64(ts
, arg
);
1549 case LTTNG_RING_BUFFER_COMPAT_GET_EVENTS_DISCARDED
:
1553 ret
= ops
->events_discarded(config
, buf
, &ed
);
1556 return put_u64(ed
, arg
);
1558 case LTTNG_RING_BUFFER_COMPAT_GET_CONTENT_SIZE
:
1562 ret
= ops
->content_size(config
, buf
, &cs
);
1565 return put_u64(cs
, arg
);
1567 case LTTNG_RING_BUFFER_COMPAT_GET_PACKET_SIZE
:
1571 ret
= ops
->packet_size(config
, buf
, &ps
);
1574 return put_u64(ps
, arg
);
1576 case LTTNG_RING_BUFFER_COMPAT_GET_STREAM_ID
:
1580 ret
= ops
->stream_id(config
, buf
, &si
);
1583 return put_u64(si
, arg
);
1585 case LTTNG_RING_BUFFER_GET_CURRENT_TIMESTAMP
:
1589 ret
= ops
->current_timestamp(config
, buf
, &ts
);
1592 return put_u64(ts
, arg
);
1595 return lib_ring_buffer_file_operations
.compat_ioctl(filp
,
1602 #endif /* CONFIG_COMPAT */
1604 static void lttng_stream_override_ring_buffer_fops(void)
1606 lttng_stream_ring_buffer_file_operations
.owner
= THIS_MODULE
;
1607 lttng_stream_ring_buffer_file_operations
.open
=
1608 lib_ring_buffer_file_operations
.open
;
1609 lttng_stream_ring_buffer_file_operations
.release
=
1610 lib_ring_buffer_file_operations
.release
;
1611 lttng_stream_ring_buffer_file_operations
.poll
=
1612 lib_ring_buffer_file_operations
.poll
;
1613 lttng_stream_ring_buffer_file_operations
.splice_read
=
1614 lib_ring_buffer_file_operations
.splice_read
;
1615 lttng_stream_ring_buffer_file_operations
.mmap
=
1616 lib_ring_buffer_file_operations
.mmap
;
1617 lttng_stream_ring_buffer_file_operations
.unlocked_ioctl
=
1618 lttng_stream_ring_buffer_ioctl
;
1619 lttng_stream_ring_buffer_file_operations
.llseek
=
1620 lib_ring_buffer_file_operations
.llseek
;
1621 #ifdef CONFIG_COMPAT
1622 lttng_stream_ring_buffer_file_operations
.compat_ioctl
=
1623 lttng_stream_ring_buffer_compat_ioctl
;
1627 int __init
lttng_abi_init(void)
1631 wrapper_vmalloc_sync_all();
1632 lttng_proc_dentry
= proc_create_data("lttng", S_IRUSR
| S_IWUSR
, NULL
,
1635 if (!lttng_proc_dentry
) {
1636 printk(KERN_ERR
"Error creating LTTng control file\n");
1640 lttng_stream_override_ring_buffer_fops();
1646 /* No __exit annotation because used by init error path too. */
1647 void lttng_abi_exit(void)
1649 if (lttng_proc_dentry
)
1650 remove_proc_entry("lttng", NULL
);