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 "wrapper/kref.h"
54 #include "lttng-abi.h"
55 #include "lttng-abi-old.h"
56 #include "lttng-events.h"
57 #include "lttng-tracer.h"
58 #include "lib/ringbuffer/frontend_types.h"
61 * This is LTTng's own personal way to create a system call as an external
62 * module. We use ioctl() on /proc/lttng.
65 static struct proc_dir_entry
*lttng_proc_dentry
;
66 static const struct file_operations lttng_fops
;
67 static const struct file_operations lttng_session_fops
;
68 static const struct file_operations lttng_channel_fops
;
69 static const struct file_operations lttng_metadata_fops
;
70 static const struct file_operations lttng_event_fops
;
71 static struct file_operations lttng_stream_ring_buffer_file_operations
;
74 * Teardown management: opened file descriptors keep a refcount on the module,
75 * so it can only exit when all file descriptors are closed.
79 int lttng_abi_create_session(void)
81 struct lttng_session
*session
;
82 struct file
*session_file
;
85 session
= lttng_session_create();
88 session_fd
= lttng_get_unused_fd();
93 session_file
= anon_inode_getfile("[lttng_session]",
96 if (IS_ERR(session_file
)) {
97 ret
= PTR_ERR(session_file
);
100 session
->file
= session_file
;
101 fd_install(session_fd
, session_file
);
105 put_unused_fd(session_fd
);
107 lttng_session_destroy(session
);
112 int lttng_abi_tracepoint_list(void)
114 struct file
*tracepoint_list_file
;
117 file_fd
= lttng_get_unused_fd();
123 tracepoint_list_file
= anon_inode_getfile("[lttng_tracepoint_list]",
124 <tng_tracepoint_list_fops
,
126 if (IS_ERR(tracepoint_list_file
)) {
127 ret
= PTR_ERR(tracepoint_list_file
);
130 ret
= lttng_tracepoint_list_fops
.open(NULL
, tracepoint_list_file
);
133 fd_install(file_fd
, tracepoint_list_file
);
137 fput(tracepoint_list_file
);
139 put_unused_fd(file_fd
);
144 #ifndef CONFIG_HAVE_SYSCALL_TRACEPOINTS
146 int lttng_abi_syscall_list(void)
152 int lttng_abi_syscall_list(void)
154 struct file
*syscall_list_file
;
157 file_fd
= lttng_get_unused_fd();
163 syscall_list_file
= anon_inode_getfile("[lttng_syscall_list]",
164 <tng_syscall_list_fops
,
166 if (IS_ERR(syscall_list_file
)) {
167 ret
= PTR_ERR(syscall_list_file
);
170 ret
= lttng_syscall_list_fops
.open(NULL
, syscall_list_file
);
173 fd_install(file_fd
, syscall_list_file
);
177 fput(syscall_list_file
);
179 put_unused_fd(file_fd
);
186 void lttng_abi_tracer_version(struct lttng_kernel_tracer_version
*v
)
188 v
->major
= LTTNG_MODULES_MAJOR_VERSION
;
189 v
->minor
= LTTNG_MODULES_MINOR_VERSION
;
190 v
->patchlevel
= LTTNG_MODULES_PATCHLEVEL_VERSION
;
194 void lttng_abi_tracer_abi_version(struct lttng_kernel_tracer_abi_version
*v
)
196 v
->major
= LTTNG_MODULES_ABI_MAJOR_VERSION
;
197 v
->minor
= LTTNG_MODULES_ABI_MINOR_VERSION
;
201 long lttng_abi_add_context(struct file
*file
,
202 struct lttng_kernel_context
*context_param
,
203 struct lttng_ctx
**ctx
, struct lttng_session
*session
)
206 if (session
->been_active
)
209 switch (context_param
->ctx
) {
210 case LTTNG_KERNEL_CONTEXT_PID
:
211 return lttng_add_pid_to_ctx(ctx
);
212 case LTTNG_KERNEL_CONTEXT_PRIO
:
213 return lttng_add_prio_to_ctx(ctx
);
214 case LTTNG_KERNEL_CONTEXT_NICE
:
215 return lttng_add_nice_to_ctx(ctx
);
216 case LTTNG_KERNEL_CONTEXT_VPID
:
217 return lttng_add_vpid_to_ctx(ctx
);
218 case LTTNG_KERNEL_CONTEXT_TID
:
219 return lttng_add_tid_to_ctx(ctx
);
220 case LTTNG_KERNEL_CONTEXT_VTID
:
221 return lttng_add_vtid_to_ctx(ctx
);
222 case LTTNG_KERNEL_CONTEXT_PPID
:
223 return lttng_add_ppid_to_ctx(ctx
);
224 case LTTNG_KERNEL_CONTEXT_VPPID
:
225 return lttng_add_vppid_to_ctx(ctx
);
226 case LTTNG_KERNEL_CONTEXT_PERF_COUNTER
:
227 context_param
->u
.perf_counter
.name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
228 return lttng_add_perf_counter_to_ctx(context_param
->u
.perf_counter
.type
,
229 context_param
->u
.perf_counter
.config
,
230 context_param
->u
.perf_counter
.name
,
232 case LTTNG_KERNEL_CONTEXT_PROCNAME
:
233 return lttng_add_procname_to_ctx(ctx
);
234 case LTTNG_KERNEL_CONTEXT_HOSTNAME
:
235 return lttng_add_hostname_to_ctx(ctx
);
236 case LTTNG_KERNEL_CONTEXT_CPU_ID
:
237 return lttng_add_cpu_id_to_ctx(ctx
);
244 * lttng_ioctl - lttng syscall through ioctl
250 * This ioctl implements lttng commands:
251 * LTTNG_KERNEL_SESSION
252 * Returns a LTTng trace session file descriptor
253 * LTTNG_KERNEL_TRACER_VERSION
254 * Returns the LTTng kernel tracer version
255 * LTTNG_KERNEL_TRACEPOINT_LIST
256 * Returns a file descriptor listing available tracepoints
257 * LTTNG_KERNEL_WAIT_QUIESCENT
258 * Returns after all previously running probes have completed
259 * LTTNG_KERNEL_TRACER_ABI_VERSION
260 * Returns the LTTng kernel tracer ABI version
262 * The returned session will be deleted when its file descriptor is closed.
265 long lttng_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
268 case LTTNG_KERNEL_OLD_SESSION
:
269 case LTTNG_KERNEL_SESSION
:
270 return lttng_abi_create_session();
271 case LTTNG_KERNEL_OLD_TRACER_VERSION
:
273 struct lttng_kernel_tracer_version v
;
274 struct lttng_kernel_old_tracer_version oldv
;
275 struct lttng_kernel_old_tracer_version
*uversion
=
276 (struct lttng_kernel_old_tracer_version __user
*) arg
;
278 lttng_abi_tracer_version(&v
);
279 oldv
.major
= v
.major
;
280 oldv
.minor
= v
.minor
;
281 oldv
.patchlevel
= v
.patchlevel
;
283 if (copy_to_user(uversion
, &oldv
, sizeof(oldv
)))
287 case LTTNG_KERNEL_TRACER_VERSION
:
289 struct lttng_kernel_tracer_version version
;
290 struct lttng_kernel_tracer_version
*uversion
=
291 (struct lttng_kernel_tracer_version __user
*) arg
;
293 lttng_abi_tracer_version(&version
);
295 if (copy_to_user(uversion
, &version
, sizeof(version
)))
299 case LTTNG_KERNEL_TRACER_ABI_VERSION
:
301 struct lttng_kernel_tracer_abi_version version
;
302 struct lttng_kernel_tracer_abi_version
*uversion
=
303 (struct lttng_kernel_tracer_abi_version __user
*) arg
;
305 lttng_abi_tracer_abi_version(&version
);
307 if (copy_to_user(uversion
, &version
, sizeof(version
)))
311 case LTTNG_KERNEL_OLD_TRACEPOINT_LIST
:
312 case LTTNG_KERNEL_TRACEPOINT_LIST
:
313 return lttng_abi_tracepoint_list();
314 case LTTNG_KERNEL_SYSCALL_LIST
:
315 return lttng_abi_syscall_list();
316 case LTTNG_KERNEL_OLD_WAIT_QUIESCENT
:
317 case LTTNG_KERNEL_WAIT_QUIESCENT
:
320 case LTTNG_KERNEL_OLD_CALIBRATE
:
322 struct lttng_kernel_old_calibrate __user
*ucalibrate
=
323 (struct lttng_kernel_old_calibrate __user
*) arg
;
324 struct lttng_kernel_old_calibrate old_calibrate
;
325 struct lttng_kernel_calibrate calibrate
;
328 if (copy_from_user(&old_calibrate
, ucalibrate
, sizeof(old_calibrate
)))
330 calibrate
.type
= old_calibrate
.type
;
331 ret
= lttng_calibrate(&calibrate
);
332 if (copy_to_user(ucalibrate
, &old_calibrate
, sizeof(old_calibrate
)))
336 case LTTNG_KERNEL_CALIBRATE
:
338 struct lttng_kernel_calibrate __user
*ucalibrate
=
339 (struct lttng_kernel_calibrate __user
*) arg
;
340 struct lttng_kernel_calibrate calibrate
;
343 if (copy_from_user(&calibrate
, ucalibrate
, sizeof(calibrate
)))
345 ret
= lttng_calibrate(&calibrate
);
346 if (copy_to_user(ucalibrate
, &calibrate
, sizeof(calibrate
)))
355 static const struct file_operations lttng_fops
= {
356 .owner
= THIS_MODULE
,
357 .unlocked_ioctl
= lttng_ioctl
,
359 .compat_ioctl
= lttng_ioctl
,
364 int lttng_abi_create_channel(struct file
*session_file
,
365 struct lttng_kernel_channel
*chan_param
,
366 enum channel_type channel_type
)
368 struct lttng_session
*session
= session_file
->private_data
;
369 const struct file_operations
*fops
= NULL
;
370 const char *transport_name
;
371 struct lttng_channel
*chan
;
372 struct file
*chan_file
;
376 chan_fd
= lttng_get_unused_fd();
381 switch (channel_type
) {
382 case PER_CPU_CHANNEL
:
383 fops
= <tng_channel_fops
;
385 case METADATA_CHANNEL
:
386 fops
= <tng_metadata_fops
;
390 chan_file
= anon_inode_getfile("[lttng_channel]",
393 if (IS_ERR(chan_file
)) {
394 ret
= PTR_ERR(chan_file
);
397 switch (channel_type
) {
398 case PER_CPU_CHANNEL
:
399 if (chan_param
->output
== LTTNG_KERNEL_SPLICE
) {
400 transport_name
= chan_param
->overwrite
?
401 "relay-overwrite" : "relay-discard";
402 } else if (chan_param
->output
== LTTNG_KERNEL_MMAP
) {
403 transport_name
= chan_param
->overwrite
?
404 "relay-overwrite-mmap" : "relay-discard-mmap";
409 case METADATA_CHANNEL
:
410 if (chan_param
->output
== LTTNG_KERNEL_SPLICE
)
411 transport_name
= "relay-metadata";
412 else if (chan_param
->output
== LTTNG_KERNEL_MMAP
)
413 transport_name
= "relay-metadata-mmap";
418 transport_name
= "<unknown>";
421 if (atomic_long_add_unless(&session_file
->f_count
,
422 1, INT_MAX
) == INT_MAX
) {
426 * We tolerate no failure path after channel creation. It will stay
427 * invariant for the rest of the session.
429 chan
= lttng_channel_create(session
, transport_name
, NULL
,
430 chan_param
->subbuf_size
,
431 chan_param
->num_subbuf
,
432 chan_param
->switch_timer_interval
,
433 chan_param
->read_timer_interval
,
439 chan
->file
= chan_file
;
440 chan_file
->private_data
= chan
;
441 fd_install(chan_fd
, chan_file
);
446 atomic_long_dec(&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 mutex_lock(&stream
->metadata_cache
->lock
);
624 if (stream
->metadata_cache
->metadata_written
>
625 stream
->metadata_out
)
627 mutex_unlock(&stream
->metadata_cache
->lock
);
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
;
927 * Since life-time of metadata cache differs from that of
928 * session, we need to keep our own reference on the transport.
930 if (!try_module_get(metadata_stream
->transport
->owner
)) {
931 printk(KERN_WARNING
"LTT : Can't lock transport module.\n");
936 if (!lttng_kref_get(&session
->metadata_cache
->refcount
))
938 ret
= lttng_abi_create_stream_fd(channel_file
, stream_priv
,
939 <tng_metadata_ring_buffer_file_operations
);
943 list_add(&metadata_stream
->list
,
944 &session
->metadata_cache
->metadata_stream
);
948 kref_put(&session
->metadata_cache
->refcount
, metadata_cache_destroy
);
950 module_put(metadata_stream
->transport
->owner
);
952 kfree(metadata_stream
);
954 channel
->ops
->buffer_read_close(buf
);
959 int lttng_abi_create_event(struct file
*channel_file
,
960 struct lttng_kernel_event
*event_param
)
962 struct lttng_channel
*channel
= channel_file
->private_data
;
964 struct file
*event_file
;
967 event_param
->name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
968 switch (event_param
->instrumentation
) {
969 case LTTNG_KERNEL_KRETPROBE
:
970 event_param
->u
.kretprobe
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
972 case LTTNG_KERNEL_KPROBE
:
973 event_param
->u
.kprobe
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
975 case LTTNG_KERNEL_FUNCTION
:
976 event_param
->u
.ftrace
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
981 event_fd
= lttng_get_unused_fd();
986 event_file
= anon_inode_getfile("[lttng_event]",
989 if (IS_ERR(event_file
)) {
990 ret
= PTR_ERR(event_file
);
993 /* The event holds a reference on the channel */
994 if (atomic_long_add_unless(&channel_file
->f_count
,
995 1, INT_MAX
) == INT_MAX
) {
998 if (event_param
->instrumentation
== LTTNG_KERNEL_TRACEPOINT
999 || event_param
->instrumentation
== LTTNG_KERNEL_SYSCALL
) {
1000 struct lttng_enabler
*enabler
;
1002 if (event_param
->name
[strlen(event_param
->name
) - 1] == '*') {
1003 enabler
= lttng_enabler_create(LTTNG_ENABLER_WILDCARD
,
1004 event_param
, channel
);
1006 enabler
= lttng_enabler_create(LTTNG_ENABLER_NAME
,
1007 event_param
, channel
);
1011 struct lttng_event
*event
;
1014 * We tolerate no failure path after event creation. It
1015 * will stay invariant for the rest of the session.
1017 event
= lttng_event_create(channel
, event_param
,
1019 event_param
->instrumentation
);
1020 WARN_ON_ONCE(!event
);
1021 if (IS_ERR(event
)) {
1022 ret
= PTR_ERR(event
);
1027 event_file
->private_data
= priv
;
1028 fd_install(event_fd
, event_file
);
1032 atomic_long_dec(&channel_file
->f_count
);
1036 put_unused_fd(event_fd
);
1042 * lttng_channel_ioctl - lttng syscall through ioctl
1048 * This ioctl implements lttng commands:
1049 * LTTNG_KERNEL_STREAM
1050 * Returns an event stream file descriptor or failure.
1051 * (typically, one event stream records events from one CPU)
1052 * LTTNG_KERNEL_EVENT
1053 * Returns an event file descriptor or failure.
1054 * LTTNG_KERNEL_CONTEXT
1055 * Prepend a context field to each event in the channel
1056 * LTTNG_KERNEL_ENABLE
1057 * Enable recording for events in this channel (weak enable)
1058 * LTTNG_KERNEL_DISABLE
1059 * Disable recording for events in this channel (strong disable)
1061 * Channel and event file descriptors also hold a reference on the session.
1064 long lttng_channel_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1066 struct lttng_channel
*channel
= file
->private_data
;
1069 case LTTNG_KERNEL_OLD_STREAM
:
1070 case LTTNG_KERNEL_STREAM
:
1071 return lttng_abi_open_stream(file
);
1072 case LTTNG_KERNEL_OLD_EVENT
:
1074 struct lttng_kernel_event
*uevent_param
;
1075 struct lttng_kernel_old_event
*old_uevent_param
;
1078 uevent_param
= kmalloc(sizeof(struct lttng_kernel_event
),
1080 if (!uevent_param
) {
1084 old_uevent_param
= kmalloc(
1085 sizeof(struct lttng_kernel_old_event
),
1087 if (!old_uevent_param
) {
1089 goto old_event_error_free_param
;
1091 if (copy_from_user(old_uevent_param
,
1092 (struct lttng_kernel_old_event __user
*) arg
,
1093 sizeof(struct lttng_kernel_old_event
))) {
1095 goto old_event_error_free_old_param
;
1098 memcpy(uevent_param
->name
, old_uevent_param
->name
,
1099 sizeof(uevent_param
->name
));
1100 uevent_param
->instrumentation
=
1101 old_uevent_param
->instrumentation
;
1103 switch (old_uevent_param
->instrumentation
) {
1104 case LTTNG_KERNEL_KPROBE
:
1105 uevent_param
->u
.kprobe
.addr
=
1106 old_uevent_param
->u
.kprobe
.addr
;
1107 uevent_param
->u
.kprobe
.offset
=
1108 old_uevent_param
->u
.kprobe
.offset
;
1109 memcpy(uevent_param
->u
.kprobe
.symbol_name
,
1110 old_uevent_param
->u
.kprobe
.symbol_name
,
1111 sizeof(uevent_param
->u
.kprobe
.symbol_name
));
1113 case LTTNG_KERNEL_KRETPROBE
:
1114 uevent_param
->u
.kretprobe
.addr
=
1115 old_uevent_param
->u
.kretprobe
.addr
;
1116 uevent_param
->u
.kretprobe
.offset
=
1117 old_uevent_param
->u
.kretprobe
.offset
;
1118 memcpy(uevent_param
->u
.kretprobe
.symbol_name
,
1119 old_uevent_param
->u
.kretprobe
.symbol_name
,
1120 sizeof(uevent_param
->u
.kretprobe
.symbol_name
));
1122 case LTTNG_KERNEL_FUNCTION
:
1123 memcpy(uevent_param
->u
.ftrace
.symbol_name
,
1124 old_uevent_param
->u
.ftrace
.symbol_name
,
1125 sizeof(uevent_param
->u
.ftrace
.symbol_name
));
1130 ret
= lttng_abi_create_event(file
, uevent_param
);
1132 old_event_error_free_old_param
:
1133 kfree(old_uevent_param
);
1134 old_event_error_free_param
:
1135 kfree(uevent_param
);
1139 case LTTNG_KERNEL_EVENT
:
1141 struct lttng_kernel_event uevent_param
;
1143 if (copy_from_user(&uevent_param
,
1144 (struct lttng_kernel_event __user
*) arg
,
1145 sizeof(uevent_param
)))
1147 return lttng_abi_create_event(file
, &uevent_param
);
1149 case LTTNG_KERNEL_OLD_CONTEXT
:
1151 struct lttng_kernel_context
*ucontext_param
;
1152 struct lttng_kernel_old_context
*old_ucontext_param
;
1155 ucontext_param
= kmalloc(sizeof(struct lttng_kernel_context
),
1157 if (!ucontext_param
) {
1161 old_ucontext_param
= kmalloc(sizeof(struct lttng_kernel_old_context
),
1163 if (!old_ucontext_param
) {
1165 goto old_ctx_error_free_param
;
1168 if (copy_from_user(old_ucontext_param
,
1169 (struct lttng_kernel_old_context __user
*) arg
,
1170 sizeof(struct lttng_kernel_old_context
))) {
1172 goto old_ctx_error_free_old_param
;
1174 ucontext_param
->ctx
= old_ucontext_param
->ctx
;
1175 memcpy(ucontext_param
->padding
, old_ucontext_param
->padding
,
1176 sizeof(ucontext_param
->padding
));
1177 /* only type that uses the union */
1178 if (old_ucontext_param
->ctx
== LTTNG_KERNEL_CONTEXT_PERF_COUNTER
) {
1179 ucontext_param
->u
.perf_counter
.type
=
1180 old_ucontext_param
->u
.perf_counter
.type
;
1181 ucontext_param
->u
.perf_counter
.config
=
1182 old_ucontext_param
->u
.perf_counter
.config
;
1183 memcpy(ucontext_param
->u
.perf_counter
.name
,
1184 old_ucontext_param
->u
.perf_counter
.name
,
1185 sizeof(ucontext_param
->u
.perf_counter
.name
));
1188 ret
= lttng_abi_add_context(file
,
1190 &channel
->ctx
, channel
->session
);
1192 old_ctx_error_free_old_param
:
1193 kfree(old_ucontext_param
);
1194 old_ctx_error_free_param
:
1195 kfree(ucontext_param
);
1199 case LTTNG_KERNEL_CONTEXT
:
1201 struct lttng_kernel_context ucontext_param
;
1203 if (copy_from_user(&ucontext_param
,
1204 (struct lttng_kernel_context __user
*) arg
,
1205 sizeof(ucontext_param
)))
1207 return lttng_abi_add_context(file
,
1209 &channel
->ctx
, channel
->session
);
1211 case LTTNG_KERNEL_OLD_ENABLE
:
1212 case LTTNG_KERNEL_ENABLE
:
1213 return lttng_channel_enable(channel
);
1214 case LTTNG_KERNEL_OLD_DISABLE
:
1215 case LTTNG_KERNEL_DISABLE
:
1216 return lttng_channel_disable(channel
);
1217 case LTTNG_KERNEL_SYSCALL_MASK
:
1218 return lttng_channel_syscall_mask(channel
,
1219 (struct lttng_kernel_syscall_mask __user
*) arg
);
1221 return -ENOIOCTLCMD
;
1227 * lttng_metadata_ioctl - lttng syscall through ioctl
1233 * This ioctl implements lttng commands:
1234 * LTTNG_KERNEL_STREAM
1235 * Returns an event stream file descriptor or failure.
1237 * Channel and event file descriptors also hold a reference on the session.
1240 long lttng_metadata_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1243 case LTTNG_KERNEL_OLD_STREAM
:
1244 case LTTNG_KERNEL_STREAM
:
1245 return lttng_abi_open_metadata_stream(file
);
1247 return -ENOIOCTLCMD
;
1252 * lttng_channel_poll - lttng stream addition/removal monitoring
1257 unsigned int lttng_channel_poll(struct file
*file
, poll_table
*wait
)
1259 struct lttng_channel
*channel
= file
->private_data
;
1260 unsigned int mask
= 0;
1262 if (file
->f_mode
& FMODE_READ
) {
1263 poll_wait_set_exclusive(wait
);
1264 poll_wait(file
, channel
->ops
->get_hp_wait_queue(channel
->chan
),
1267 if (channel
->ops
->is_disabled(channel
->chan
))
1269 if (channel
->ops
->is_finalized(channel
->chan
))
1271 if (channel
->ops
->buffer_has_read_closed_stream(channel
->chan
))
1272 return POLLIN
| POLLRDNORM
;
1280 int lttng_channel_release(struct inode
*inode
, struct file
*file
)
1282 struct lttng_channel
*channel
= file
->private_data
;
1285 fput(channel
->session
->file
);
1290 int lttng_metadata_channel_release(struct inode
*inode
, struct file
*file
)
1292 struct lttng_channel
*channel
= file
->private_data
;
1295 fput(channel
->session
->file
);
1296 lttng_metadata_channel_destroy(channel
);
1302 static const struct file_operations lttng_channel_fops
= {
1303 .owner
= THIS_MODULE
,
1304 .release
= lttng_channel_release
,
1305 .poll
= lttng_channel_poll
,
1306 .unlocked_ioctl
= lttng_channel_ioctl
,
1307 #ifdef CONFIG_COMPAT
1308 .compat_ioctl
= lttng_channel_ioctl
,
1312 static const struct file_operations lttng_metadata_fops
= {
1313 .owner
= THIS_MODULE
,
1314 .release
= lttng_metadata_channel_release
,
1315 .unlocked_ioctl
= lttng_metadata_ioctl
,
1316 #ifdef CONFIG_COMPAT
1317 .compat_ioctl
= lttng_metadata_ioctl
,
1322 * lttng_event_ioctl - lttng syscall through ioctl
1328 * This ioctl implements lttng commands:
1329 * LTTNG_KERNEL_CONTEXT
1330 * Prepend a context field to each record of this event
1331 * LTTNG_KERNEL_ENABLE
1332 * Enable recording for this event (weak enable)
1333 * LTTNG_KERNEL_DISABLE
1334 * Disable recording for this event (strong disable)
1337 long lttng_event_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1339 struct lttng_event
*event
;
1340 struct lttng_enabler
*enabler
;
1341 enum lttng_event_type
*evtype
= file
->private_data
;
1344 case LTTNG_KERNEL_OLD_CONTEXT
:
1346 /* Not implemented */
1349 case LTTNG_KERNEL_CONTEXT
:
1351 /* Not implemented */
1354 case LTTNG_KERNEL_OLD_ENABLE
:
1355 case LTTNG_KERNEL_ENABLE
:
1357 case LTTNG_TYPE_EVENT
:
1358 event
= file
->private_data
;
1359 return lttng_event_enable(event
);
1360 case LTTNG_TYPE_ENABLER
:
1361 enabler
= file
->private_data
;
1362 return lttng_enabler_enable(enabler
);
1367 case LTTNG_KERNEL_OLD_DISABLE
:
1368 case LTTNG_KERNEL_DISABLE
:
1370 case LTTNG_TYPE_EVENT
:
1371 event
= file
->private_data
;
1372 return lttng_event_disable(event
);
1373 case LTTNG_TYPE_ENABLER
:
1374 enabler
= file
->private_data
;
1375 return lttng_enabler_disable(enabler
);
1380 case LTTNG_KERNEL_FILTER
:
1382 case LTTNG_TYPE_EVENT
:
1384 case LTTNG_TYPE_ENABLER
:
1386 enabler
= file
->private_data
;
1387 return lttng_enabler_attach_bytecode(enabler
,
1388 (struct lttng_kernel_filter_bytecode __user
*) arg
);
1393 return -ENOIOCTLCMD
;
1398 int lttng_event_release(struct inode
*inode
, struct file
*file
)
1400 struct lttng_event
*event
;
1401 struct lttng_enabler
*enabler
;
1402 enum lttng_event_type
*evtype
= file
->private_data
;
1408 case LTTNG_TYPE_EVENT
:
1409 event
= file
->private_data
;
1411 fput(event
->chan
->file
);
1413 case LTTNG_TYPE_ENABLER
:
1414 enabler
= file
->private_data
;
1416 fput(enabler
->chan
->file
);
1426 /* TODO: filter control ioctl */
1427 static const struct file_operations lttng_event_fops
= {
1428 .owner
= THIS_MODULE
,
1429 .release
= lttng_event_release
,
1430 .unlocked_ioctl
= lttng_event_ioctl
,
1431 #ifdef CONFIG_COMPAT
1432 .compat_ioctl
= lttng_event_ioctl
,
1436 static int put_u64(uint64_t val
, unsigned long arg
)
1438 return put_user(val
, (uint64_t __user
*) arg
);
1441 static long lttng_stream_ring_buffer_ioctl(struct file
*filp
,
1442 unsigned int cmd
, unsigned long arg
)
1444 struct lib_ring_buffer
*buf
= filp
->private_data
;
1445 struct channel
*chan
= buf
->backend
.chan
;
1446 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1447 const struct lttng_channel_ops
*ops
= chan
->backend
.priv_ops
;
1450 if (atomic_read(&chan
->record_disabled
))
1454 case LTTNG_RING_BUFFER_GET_TIMESTAMP_BEGIN
:
1458 ret
= ops
->timestamp_begin(config
, buf
, &ts
);
1461 return put_u64(ts
, arg
);
1463 case LTTNG_RING_BUFFER_GET_TIMESTAMP_END
:
1467 ret
= ops
->timestamp_end(config
, buf
, &ts
);
1470 return put_u64(ts
, arg
);
1472 case LTTNG_RING_BUFFER_GET_EVENTS_DISCARDED
:
1476 ret
= ops
->events_discarded(config
, buf
, &ed
);
1479 return put_u64(ed
, arg
);
1481 case LTTNG_RING_BUFFER_GET_CONTENT_SIZE
:
1485 ret
= ops
->content_size(config
, buf
, &cs
);
1488 return put_u64(cs
, arg
);
1490 case LTTNG_RING_BUFFER_GET_PACKET_SIZE
:
1494 ret
= ops
->packet_size(config
, buf
, &ps
);
1497 return put_u64(ps
, arg
);
1499 case LTTNG_RING_BUFFER_GET_STREAM_ID
:
1503 ret
= ops
->stream_id(config
, buf
, &si
);
1506 return put_u64(si
, arg
);
1508 case LTTNG_RING_BUFFER_GET_CURRENT_TIMESTAMP
:
1512 ret
= ops
->current_timestamp(config
, buf
, &ts
);
1515 return put_u64(ts
, arg
);
1518 return lib_ring_buffer_file_operations
.unlocked_ioctl(filp
,
1526 #ifdef CONFIG_COMPAT
1527 static long lttng_stream_ring_buffer_compat_ioctl(struct file
*filp
,
1528 unsigned int cmd
, unsigned long arg
)
1530 struct lib_ring_buffer
*buf
= filp
->private_data
;
1531 struct channel
*chan
= buf
->backend
.chan
;
1532 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1533 const struct lttng_channel_ops
*ops
= chan
->backend
.priv_ops
;
1536 if (atomic_read(&chan
->record_disabled
))
1540 case LTTNG_RING_BUFFER_COMPAT_GET_TIMESTAMP_BEGIN
:
1544 ret
= ops
->timestamp_begin(config
, buf
, &ts
);
1547 return put_u64(ts
, arg
);
1549 case LTTNG_RING_BUFFER_COMPAT_GET_TIMESTAMP_END
:
1553 ret
= ops
->timestamp_end(config
, buf
, &ts
);
1556 return put_u64(ts
, arg
);
1558 case LTTNG_RING_BUFFER_COMPAT_GET_EVENTS_DISCARDED
:
1562 ret
= ops
->events_discarded(config
, buf
, &ed
);
1565 return put_u64(ed
, arg
);
1567 case LTTNG_RING_BUFFER_COMPAT_GET_CONTENT_SIZE
:
1571 ret
= ops
->content_size(config
, buf
, &cs
);
1574 return put_u64(cs
, arg
);
1576 case LTTNG_RING_BUFFER_COMPAT_GET_PACKET_SIZE
:
1580 ret
= ops
->packet_size(config
, buf
, &ps
);
1583 return put_u64(ps
, arg
);
1585 case LTTNG_RING_BUFFER_COMPAT_GET_STREAM_ID
:
1589 ret
= ops
->stream_id(config
, buf
, &si
);
1592 return put_u64(si
, arg
);
1594 case LTTNG_RING_BUFFER_GET_CURRENT_TIMESTAMP
:
1598 ret
= ops
->current_timestamp(config
, buf
, &ts
);
1601 return put_u64(ts
, arg
);
1604 return lib_ring_buffer_file_operations
.compat_ioctl(filp
,
1611 #endif /* CONFIG_COMPAT */
1613 static void lttng_stream_override_ring_buffer_fops(void)
1615 lttng_stream_ring_buffer_file_operations
.owner
= THIS_MODULE
;
1616 lttng_stream_ring_buffer_file_operations
.open
=
1617 lib_ring_buffer_file_operations
.open
;
1618 lttng_stream_ring_buffer_file_operations
.release
=
1619 lib_ring_buffer_file_operations
.release
;
1620 lttng_stream_ring_buffer_file_operations
.poll
=
1621 lib_ring_buffer_file_operations
.poll
;
1622 lttng_stream_ring_buffer_file_operations
.splice_read
=
1623 lib_ring_buffer_file_operations
.splice_read
;
1624 lttng_stream_ring_buffer_file_operations
.mmap
=
1625 lib_ring_buffer_file_operations
.mmap
;
1626 lttng_stream_ring_buffer_file_operations
.unlocked_ioctl
=
1627 lttng_stream_ring_buffer_ioctl
;
1628 lttng_stream_ring_buffer_file_operations
.llseek
=
1629 lib_ring_buffer_file_operations
.llseek
;
1630 #ifdef CONFIG_COMPAT
1631 lttng_stream_ring_buffer_file_operations
.compat_ioctl
=
1632 lttng_stream_ring_buffer_compat_ioctl
;
1636 int __init
lttng_abi_init(void)
1640 wrapper_vmalloc_sync_all();
1641 lttng_proc_dentry
= proc_create_data("lttng", S_IRUSR
| S_IWUSR
, NULL
,
1644 if (!lttng_proc_dentry
) {
1645 printk(KERN_ERR
"Error creating LTTng control file\n");
1649 lttng_stream_override_ring_buffer_fops();
1655 /* No __exit annotation because used by init error path too. */
1656 void lttng_abi_exit(void)
1658 if (lttng_proc_dentry
)
1659 remove_proc_entry("lttng", NULL
);