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
);
148 void lttng_abi_tracer_version(struct lttng_kernel_tracer_version
*v
)
150 v
->major
= LTTNG_MODULES_MAJOR_VERSION
;
151 v
->minor
= LTTNG_MODULES_MINOR_VERSION
;
152 v
->patchlevel
= LTTNG_MODULES_PATCHLEVEL_VERSION
;
156 void lttng_abi_tracer_abi_version(struct lttng_kernel_tracer_abi_version
*v
)
158 v
->major
= LTTNG_MODULES_ABI_MAJOR_VERSION
;
159 v
->minor
= LTTNG_MODULES_ABI_MINOR_VERSION
;
163 long lttng_abi_add_context(struct file
*file
,
164 struct lttng_kernel_context
*context_param
,
165 struct lttng_ctx
**ctx
, struct lttng_session
*session
)
168 if (session
->been_active
)
171 switch (context_param
->ctx
) {
172 case LTTNG_KERNEL_CONTEXT_PID
:
173 return lttng_add_pid_to_ctx(ctx
);
174 case LTTNG_KERNEL_CONTEXT_PRIO
:
175 return lttng_add_prio_to_ctx(ctx
);
176 case LTTNG_KERNEL_CONTEXT_NICE
:
177 return lttng_add_nice_to_ctx(ctx
);
178 case LTTNG_KERNEL_CONTEXT_VPID
:
179 return lttng_add_vpid_to_ctx(ctx
);
180 case LTTNG_KERNEL_CONTEXT_TID
:
181 return lttng_add_tid_to_ctx(ctx
);
182 case LTTNG_KERNEL_CONTEXT_VTID
:
183 return lttng_add_vtid_to_ctx(ctx
);
184 case LTTNG_KERNEL_CONTEXT_PPID
:
185 return lttng_add_ppid_to_ctx(ctx
);
186 case LTTNG_KERNEL_CONTEXT_VPPID
:
187 return lttng_add_vppid_to_ctx(ctx
);
188 case LTTNG_KERNEL_CONTEXT_PERF_COUNTER
:
189 context_param
->u
.perf_counter
.name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
190 return lttng_add_perf_counter_to_ctx(context_param
->u
.perf_counter
.type
,
191 context_param
->u
.perf_counter
.config
,
192 context_param
->u
.perf_counter
.name
,
194 case LTTNG_KERNEL_CONTEXT_PROCNAME
:
195 return lttng_add_procname_to_ctx(ctx
);
196 case LTTNG_KERNEL_CONTEXT_HOSTNAME
:
197 return lttng_add_hostname_to_ctx(ctx
);
204 * lttng_ioctl - lttng syscall through ioctl
210 * This ioctl implements lttng commands:
211 * LTTNG_KERNEL_SESSION
212 * Returns a LTTng trace session file descriptor
213 * LTTNG_KERNEL_TRACER_VERSION
214 * Returns the LTTng kernel tracer version
215 * LTTNG_KERNEL_TRACEPOINT_LIST
216 * Returns a file descriptor listing available tracepoints
217 * LTTNG_KERNEL_WAIT_QUIESCENT
218 * Returns after all previously running probes have completed
219 * LTTNG_KERNEL_TRACER_ABI_VERSION
220 * Returns the LTTng kernel tracer ABI version
222 * The returned session will be deleted when its file descriptor is closed.
225 long lttng_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
228 case LTTNG_KERNEL_OLD_SESSION
:
229 case LTTNG_KERNEL_SESSION
:
230 return lttng_abi_create_session();
231 case LTTNG_KERNEL_OLD_TRACER_VERSION
:
233 struct lttng_kernel_tracer_version v
;
234 struct lttng_kernel_old_tracer_version oldv
;
235 struct lttng_kernel_old_tracer_version
*uversion
=
236 (struct lttng_kernel_old_tracer_version __user
*) arg
;
238 lttng_abi_tracer_version(&v
);
239 oldv
.major
= v
.major
;
240 oldv
.minor
= v
.minor
;
241 oldv
.patchlevel
= v
.patchlevel
;
243 if (copy_to_user(uversion
, &oldv
, sizeof(oldv
)))
247 case LTTNG_KERNEL_TRACER_VERSION
:
249 struct lttng_kernel_tracer_version version
;
250 struct lttng_kernel_tracer_version
*uversion
=
251 (struct lttng_kernel_tracer_version __user
*) arg
;
253 lttng_abi_tracer_version(&version
);
255 if (copy_to_user(uversion
, &version
, sizeof(version
)))
259 case LTTNG_KERNEL_TRACER_ABI_VERSION
:
261 struct lttng_kernel_tracer_abi_version version
;
262 struct lttng_kernel_tracer_abi_version
*uversion
=
263 (struct lttng_kernel_tracer_abi_version __user
*) arg
;
265 lttng_abi_tracer_abi_version(&version
);
267 if (copy_to_user(uversion
, &version
, sizeof(version
)))
271 case LTTNG_KERNEL_OLD_TRACEPOINT_LIST
:
272 case LTTNG_KERNEL_TRACEPOINT_LIST
:
273 return lttng_abi_tracepoint_list();
274 case LTTNG_KERNEL_SYSCALL_LIST
:
275 return lttng_abi_syscall_list();
276 case LTTNG_KERNEL_OLD_WAIT_QUIESCENT
:
277 case LTTNG_KERNEL_WAIT_QUIESCENT
:
280 case LTTNG_KERNEL_OLD_CALIBRATE
:
282 struct lttng_kernel_old_calibrate __user
*ucalibrate
=
283 (struct lttng_kernel_old_calibrate __user
*) arg
;
284 struct lttng_kernel_old_calibrate old_calibrate
;
285 struct lttng_kernel_calibrate calibrate
;
288 if (copy_from_user(&old_calibrate
, ucalibrate
, sizeof(old_calibrate
)))
290 calibrate
.type
= old_calibrate
.type
;
291 ret
= lttng_calibrate(&calibrate
);
292 if (copy_to_user(ucalibrate
, &old_calibrate
, sizeof(old_calibrate
)))
296 case LTTNG_KERNEL_CALIBRATE
:
298 struct lttng_kernel_calibrate __user
*ucalibrate
=
299 (struct lttng_kernel_calibrate __user
*) arg
;
300 struct lttng_kernel_calibrate calibrate
;
303 if (copy_from_user(&calibrate
, ucalibrate
, sizeof(calibrate
)))
305 ret
= lttng_calibrate(&calibrate
);
306 if (copy_to_user(ucalibrate
, &calibrate
, sizeof(calibrate
)))
315 static const struct file_operations lttng_fops
= {
316 .owner
= THIS_MODULE
,
317 .unlocked_ioctl
= lttng_ioctl
,
319 .compat_ioctl
= lttng_ioctl
,
324 int lttng_abi_create_channel(struct file
*session_file
,
325 struct lttng_kernel_channel
*chan_param
,
326 enum channel_type channel_type
)
328 struct lttng_session
*session
= session_file
->private_data
;
329 const struct file_operations
*fops
= NULL
;
330 const char *transport_name
;
331 struct lttng_channel
*chan
;
332 struct file
*chan_file
;
336 chan_fd
= lttng_get_unused_fd();
341 switch (channel_type
) {
342 case PER_CPU_CHANNEL
:
343 fops
= <tng_channel_fops
;
345 case METADATA_CHANNEL
:
346 fops
= <tng_metadata_fops
;
350 chan_file
= anon_inode_getfile("[lttng_channel]",
353 if (IS_ERR(chan_file
)) {
354 ret
= PTR_ERR(chan_file
);
357 switch (channel_type
) {
358 case PER_CPU_CHANNEL
:
359 if (chan_param
->output
== LTTNG_KERNEL_SPLICE
) {
360 transport_name
= chan_param
->overwrite
?
361 "relay-overwrite" : "relay-discard";
362 } else if (chan_param
->output
== LTTNG_KERNEL_MMAP
) {
363 transport_name
= chan_param
->overwrite
?
364 "relay-overwrite-mmap" : "relay-discard-mmap";
369 case METADATA_CHANNEL
:
370 if (chan_param
->output
== LTTNG_KERNEL_SPLICE
)
371 transport_name
= "relay-metadata";
372 else if (chan_param
->output
== LTTNG_KERNEL_MMAP
)
373 transport_name
= "relay-metadata-mmap";
378 transport_name
= "<unknown>";
382 * We tolerate no failure path after channel creation. It will stay
383 * invariant for the rest of the session.
385 chan
= lttng_channel_create(session
, transport_name
, NULL
,
386 chan_param
->subbuf_size
,
387 chan_param
->num_subbuf
,
388 chan_param
->switch_timer_interval
,
389 chan_param
->read_timer_interval
,
395 chan
->file
= chan_file
;
396 chan_file
->private_data
= chan
;
397 fd_install(chan_fd
, chan_file
);
398 atomic_long_inc(&session_file
->f_count
);
405 put_unused_fd(chan_fd
);
411 * lttng_session_ioctl - lttng session fd ioctl
417 * This ioctl implements lttng commands:
418 * LTTNG_KERNEL_CHANNEL
419 * Returns a LTTng channel file descriptor
420 * LTTNG_KERNEL_ENABLE
421 * Enables tracing for a session (weak enable)
422 * LTTNG_KERNEL_DISABLE
423 * Disables tracing for a session (strong disable)
424 * LTTNG_KERNEL_METADATA
425 * Returns a LTTng metadata file descriptor
427 * The returned channel will be deleted when its file descriptor is closed.
430 long lttng_session_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
432 struct lttng_session
*session
= file
->private_data
;
435 case LTTNG_KERNEL_OLD_CHANNEL
:
437 struct lttng_kernel_channel chan_param
;
438 struct lttng_kernel_old_channel old_chan_param
;
440 if (copy_from_user(&old_chan_param
,
441 (struct lttng_kernel_old_channel __user
*) arg
,
442 sizeof(struct lttng_kernel_old_channel
)))
444 chan_param
.overwrite
= old_chan_param
.overwrite
;
445 chan_param
.subbuf_size
= old_chan_param
.subbuf_size
;
446 chan_param
.num_subbuf
= old_chan_param
.num_subbuf
;
447 chan_param
.switch_timer_interval
= old_chan_param
.switch_timer_interval
;
448 chan_param
.read_timer_interval
= old_chan_param
.read_timer_interval
;
449 chan_param
.output
= old_chan_param
.output
;
451 return lttng_abi_create_channel(file
, &chan_param
,
454 case LTTNG_KERNEL_CHANNEL
:
456 struct lttng_kernel_channel chan_param
;
458 if (copy_from_user(&chan_param
,
459 (struct lttng_kernel_channel __user
*) arg
,
460 sizeof(struct lttng_kernel_channel
)))
462 return lttng_abi_create_channel(file
, &chan_param
,
465 case LTTNG_KERNEL_OLD_SESSION_START
:
466 case LTTNG_KERNEL_OLD_ENABLE
:
467 case LTTNG_KERNEL_SESSION_START
:
468 case LTTNG_KERNEL_ENABLE
:
469 return lttng_session_enable(session
);
470 case LTTNG_KERNEL_OLD_SESSION_STOP
:
471 case LTTNG_KERNEL_OLD_DISABLE
:
472 case LTTNG_KERNEL_SESSION_STOP
:
473 case LTTNG_KERNEL_DISABLE
:
474 return lttng_session_disable(session
);
475 case LTTNG_KERNEL_OLD_METADATA
:
477 struct lttng_kernel_channel chan_param
;
478 struct lttng_kernel_old_channel old_chan_param
;
480 if (copy_from_user(&old_chan_param
,
481 (struct lttng_kernel_old_channel __user
*) arg
,
482 sizeof(struct lttng_kernel_old_channel
)))
484 chan_param
.overwrite
= old_chan_param
.overwrite
;
485 chan_param
.subbuf_size
= old_chan_param
.subbuf_size
;
486 chan_param
.num_subbuf
= old_chan_param
.num_subbuf
;
487 chan_param
.switch_timer_interval
= old_chan_param
.switch_timer_interval
;
488 chan_param
.read_timer_interval
= old_chan_param
.read_timer_interval
;
489 chan_param
.output
= old_chan_param
.output
;
491 return lttng_abi_create_channel(file
, &chan_param
,
494 case LTTNG_KERNEL_METADATA
:
496 struct lttng_kernel_channel chan_param
;
498 if (copy_from_user(&chan_param
,
499 (struct lttng_kernel_channel __user
*) arg
,
500 sizeof(struct lttng_kernel_channel
)))
502 return lttng_abi_create_channel(file
, &chan_param
,
511 * Called when the last file reference is dropped.
513 * Big fat note: channels and events are invariant for the whole session after
514 * their creation. So this session destruction also destroys all channel and
515 * event structures specific to this session (they are not destroyed when their
516 * individual file is released).
519 int lttng_session_release(struct inode
*inode
, struct file
*file
)
521 struct lttng_session
*session
= file
->private_data
;
524 lttng_session_destroy(session
);
528 static const struct file_operations lttng_session_fops
= {
529 .owner
= THIS_MODULE
,
530 .release
= lttng_session_release
,
531 .unlocked_ioctl
= lttng_session_ioctl
,
533 .compat_ioctl
= lttng_session_ioctl
,
538 * lttng_metadata_ring_buffer_poll - LTTng ring buffer poll file operation
542 * Handles the poll operations for the metadata channels.
545 unsigned int lttng_metadata_ring_buffer_poll(struct file
*filp
,
548 struct lttng_metadata_stream
*stream
= filp
->private_data
;
549 struct lib_ring_buffer
*buf
= stream
->priv
;
551 unsigned int mask
= 0;
553 if (filp
->f_mode
& FMODE_READ
) {
554 poll_wait_set_exclusive(wait
);
555 poll_wait(filp
, &stream
->read_wait
, wait
);
557 finalized
= stream
->finalized
;
560 * lib_ring_buffer_is_finalized() contains a smp_rmb()
561 * ordering finalized load before offsets loads.
563 WARN_ON(atomic_long_read(&buf
->active_readers
) != 1);
568 mutex_lock(&stream
->metadata_cache
->lock
);
569 if (stream
->metadata_cache
->metadata_written
>
570 stream
->metadata_out
)
572 mutex_unlock(&stream
->metadata_cache
->lock
);
579 void lttng_metadata_ring_buffer_ioctl_put_next_subbuf(struct file
*filp
,
580 unsigned int cmd
, unsigned long arg
)
582 struct lttng_metadata_stream
*stream
= filp
->private_data
;
584 stream
->metadata_out
= stream
->metadata_in
;
588 long lttng_metadata_ring_buffer_ioctl(struct file
*filp
,
589 unsigned int cmd
, unsigned long arg
)
592 struct lttng_metadata_stream
*stream
= filp
->private_data
;
593 struct lib_ring_buffer
*buf
= stream
->priv
;
596 case RING_BUFFER_GET_NEXT_SUBBUF
:
598 struct lttng_metadata_stream
*stream
= filp
->private_data
;
599 struct lib_ring_buffer
*buf
= stream
->priv
;
600 struct channel
*chan
= buf
->backend
.chan
;
602 ret
= lttng_metadata_output_channel(stream
, chan
);
604 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
610 case RING_BUFFER_GET_SUBBUF
:
613 * Random access is not allowed for metadata channel.
617 case RING_BUFFER_FLUSH
:
619 struct lttng_metadata_stream
*stream
= filp
->private_data
;
620 struct lib_ring_buffer
*buf
= stream
->priv
;
621 struct channel
*chan
= buf
->backend
.chan
;
624 * Before doing the actual ring buffer flush, write up to one
625 * packet of metadata in the ring buffer.
627 ret
= lttng_metadata_output_channel(stream
, chan
);
635 /* PUT_SUBBUF is the one from lib ring buffer, unmodified. */
637 /* Performing lib ring buffer ioctl after our own. */
638 ret
= lib_ring_buffer_ioctl(filp
, cmd
, arg
, buf
);
643 case RING_BUFFER_PUT_NEXT_SUBBUF
:
645 lttng_metadata_ring_buffer_ioctl_put_next_subbuf(filp
,
658 long lttng_metadata_ring_buffer_compat_ioctl(struct file
*filp
,
659 unsigned int cmd
, unsigned long arg
)
662 struct lttng_metadata_stream
*stream
= filp
->private_data
;
663 struct lib_ring_buffer
*buf
= stream
->priv
;
666 case RING_BUFFER_GET_NEXT_SUBBUF
:
668 struct lttng_metadata_stream
*stream
= filp
->private_data
;
669 struct lib_ring_buffer
*buf
= stream
->priv
;
670 struct channel
*chan
= buf
->backend
.chan
;
672 ret
= lttng_metadata_output_channel(stream
, chan
);
674 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
680 case RING_BUFFER_GET_SUBBUF
:
683 * Random access is not allowed for metadata channel.
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_compat_ioctl(filp
, cmd
, arg
, buf
);
698 case RING_BUFFER_PUT_NEXT_SUBBUF
:
700 lttng_metadata_ring_buffer_ioctl_put_next_subbuf(filp
,
713 * This is not used by anonymous file descriptors. This code is left
714 * there if we ever want to implement an inode with open() operation.
717 int lttng_metadata_ring_buffer_open(struct inode
*inode
, struct file
*file
)
719 struct lttng_metadata_stream
*stream
= inode
->i_private
;
720 struct lib_ring_buffer
*buf
= stream
->priv
;
722 file
->private_data
= buf
;
724 * Since life-time of metadata cache differs from that of
725 * session, we need to keep our own reference on the transport.
727 if (!try_module_get(stream
->transport
->owner
)) {
728 printk(KERN_WARNING
"LTT : Can't lock transport module.\n");
731 return lib_ring_buffer_open(inode
, file
, buf
);
735 int lttng_metadata_ring_buffer_release(struct inode
*inode
, struct file
*file
)
737 struct lttng_metadata_stream
*stream
= file
->private_data
;
738 struct lib_ring_buffer
*buf
= stream
->priv
;
740 kref_put(&stream
->metadata_cache
->refcount
, metadata_cache_destroy
);
741 module_put(stream
->transport
->owner
);
742 return lib_ring_buffer_release(inode
, file
, buf
);
746 ssize_t
lttng_metadata_ring_buffer_splice_read(struct file
*in
, loff_t
*ppos
,
747 struct pipe_inode_info
*pipe
, size_t len
,
750 struct lttng_metadata_stream
*stream
= in
->private_data
;
751 struct lib_ring_buffer
*buf
= stream
->priv
;
753 return lib_ring_buffer_splice_read(in
, ppos
, pipe
, len
,
758 int lttng_metadata_ring_buffer_mmap(struct file
*filp
,
759 struct vm_area_struct
*vma
)
761 struct lttng_metadata_stream
*stream
= filp
->private_data
;
762 struct lib_ring_buffer
*buf
= stream
->priv
;
764 return lib_ring_buffer_mmap(filp
, vma
, buf
);
768 const struct file_operations lttng_metadata_ring_buffer_file_operations
= {
769 .owner
= THIS_MODULE
,
770 .open
= lttng_metadata_ring_buffer_open
,
771 .release
= lttng_metadata_ring_buffer_release
,
772 .poll
= lttng_metadata_ring_buffer_poll
,
773 .splice_read
= lttng_metadata_ring_buffer_splice_read
,
774 .mmap
= lttng_metadata_ring_buffer_mmap
,
775 .unlocked_ioctl
= lttng_metadata_ring_buffer_ioctl
,
776 .llseek
= vfs_lib_ring_buffer_no_llseek
,
778 .compat_ioctl
= lttng_metadata_ring_buffer_compat_ioctl
,
783 int lttng_abi_create_stream_fd(struct file
*channel_file
, void *stream_priv
,
784 const struct file_operations
*fops
)
787 struct file
*stream_file
;
789 stream_fd
= lttng_get_unused_fd();
794 stream_file
= anon_inode_getfile("[lttng_stream]", fops
,
795 stream_priv
, O_RDWR
);
796 if (IS_ERR(stream_file
)) {
797 ret
= PTR_ERR(stream_file
);
801 * OPEN_FMODE, called within anon_inode_getfile/alloc_file, don't honor
802 * FMODE_LSEEK, FMODE_PREAD nor FMODE_PWRITE. We need to read from this
803 * file descriptor, so we set FMODE_PREAD here.
805 stream_file
->f_mode
|= FMODE_PREAD
;
806 fd_install(stream_fd
, stream_file
);
808 * The stream holds a reference to the channel within the generic ring
809 * buffer library, so no need to hold a refcount on the channel and
810 * session files here.
815 put_unused_fd(stream_fd
);
821 int lttng_abi_open_stream(struct file
*channel_file
)
823 struct lttng_channel
*channel
= channel_file
->private_data
;
824 struct lib_ring_buffer
*buf
;
828 buf
= channel
->ops
->buffer_read_open(channel
->chan
);
833 ret
= lttng_abi_create_stream_fd(channel_file
, stream_priv
,
834 <tng_stream_ring_buffer_file_operations
);
841 channel
->ops
->buffer_read_close(buf
);
846 int lttng_abi_open_metadata_stream(struct file
*channel_file
)
848 struct lttng_channel
*channel
= channel_file
->private_data
;
849 struct lttng_session
*session
= channel
->session
;
850 struct lib_ring_buffer
*buf
;
852 struct lttng_metadata_stream
*metadata_stream
;
855 buf
= channel
->ops
->buffer_read_open(channel
->chan
);
859 metadata_stream
= kzalloc(sizeof(struct lttng_metadata_stream
),
861 if (!metadata_stream
) {
865 metadata_stream
->metadata_cache
= session
->metadata_cache
;
866 init_waitqueue_head(&metadata_stream
->read_wait
);
867 metadata_stream
->priv
= buf
;
868 stream_priv
= metadata_stream
;
869 metadata_stream
->transport
= channel
->transport
;
872 * Since life-time of metadata cache differs from that of
873 * session, we need to keep our own reference on the transport.
875 if (!try_module_get(metadata_stream
->transport
->owner
)) {
876 printk(KERN_WARNING
"LTT : Can't lock transport module.\n");
881 ret
= lttng_abi_create_stream_fd(channel_file
, stream_priv
,
882 <tng_metadata_ring_buffer_file_operations
);
886 kref_get(&session
->metadata_cache
->refcount
);
887 list_add(&metadata_stream
->list
,
888 &session
->metadata_cache
->metadata_stream
);
892 module_put(metadata_stream
->transport
->owner
);
894 kfree(metadata_stream
);
896 channel
->ops
->buffer_read_close(buf
);
901 int lttng_abi_create_event(struct file
*channel_file
,
902 struct lttng_kernel_event
*event_param
)
904 struct lttng_channel
*channel
= channel_file
->private_data
;
905 struct lttng_event
*event
;
907 struct file
*event_file
;
909 event_param
->name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
910 switch (event_param
->instrumentation
) {
911 case LTTNG_KERNEL_KRETPROBE
:
912 event_param
->u
.kretprobe
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
914 case LTTNG_KERNEL_KPROBE
:
915 event_param
->u
.kprobe
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
917 case LTTNG_KERNEL_FUNCTION
:
918 event_param
->u
.ftrace
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
923 switch (event_param
->instrumentation
) {
925 event_fd
= lttng_get_unused_fd();
930 event_file
= anon_inode_getfile("[lttng_event]",
933 if (IS_ERR(event_file
)) {
934 ret
= PTR_ERR(event_file
);
938 * We tolerate no failure path after event creation. It
939 * will stay invariant for the rest of the session.
941 event
= lttng_event_create(channel
, event_param
, NULL
, NULL
);
942 WARN_ON_ONCE(!event
);
944 ret
= PTR_ERR(event
);
947 event_file
->private_data
= event
;
948 fd_install(event_fd
, event_file
);
949 /* The event holds a reference on the channel */
950 atomic_long_inc(&channel_file
->f_count
);
952 case LTTNG_KERNEL_SYSCALL
:
953 ret
= lttng_syscalls_register(channel
, NULL
);
957 if (event_param
->u
.syscall
.enable
) {
958 ret
= lttng_syscall_filter_enable(channel
,
959 event_param
->name
[0] == '\0' ?
960 NULL
: event_param
->name
);
965 ret
= lttng_syscall_filter_disable(channel
,
966 event_param
->name
[0] == '\0' ?
967 NULL
: event_param
->name
);
978 put_unused_fd(event_fd
);
984 * lttng_channel_ioctl - lttng syscall through ioctl
990 * This ioctl implements lttng commands:
991 * LTTNG_KERNEL_STREAM
992 * Returns an event stream file descriptor or failure.
993 * (typically, one event stream records events from one CPU)
995 * Returns an event file descriptor or failure.
996 * LTTNG_KERNEL_CONTEXT
997 * Prepend a context field to each event in the channel
998 * LTTNG_KERNEL_ENABLE
999 * Enable recording for events in this channel (weak enable)
1000 * LTTNG_KERNEL_DISABLE
1001 * Disable recording for events in this channel (strong disable)
1003 * Channel and event file descriptors also hold a reference on the session.
1006 long lttng_channel_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1008 struct lttng_channel
*channel
= file
->private_data
;
1011 case LTTNG_KERNEL_OLD_STREAM
:
1012 case LTTNG_KERNEL_STREAM
:
1013 return lttng_abi_open_stream(file
);
1014 case LTTNG_KERNEL_OLD_EVENT
:
1016 struct lttng_kernel_event
*uevent_param
;
1017 struct lttng_kernel_old_event
*old_uevent_param
;
1020 uevent_param
= kmalloc(sizeof(struct lttng_kernel_event
),
1022 if (!uevent_param
) {
1026 old_uevent_param
= kmalloc(
1027 sizeof(struct lttng_kernel_old_event
),
1029 if (!old_uevent_param
) {
1031 goto old_event_error_free_param
;
1033 if (copy_from_user(old_uevent_param
,
1034 (struct lttng_kernel_old_event __user
*) arg
,
1035 sizeof(struct lttng_kernel_old_event
))) {
1037 goto old_event_error_free_old_param
;
1040 memcpy(uevent_param
->name
, old_uevent_param
->name
,
1041 sizeof(uevent_param
->name
));
1042 uevent_param
->instrumentation
=
1043 old_uevent_param
->instrumentation
;
1045 switch (old_uevent_param
->instrumentation
) {
1046 case LTTNG_KERNEL_KPROBE
:
1047 uevent_param
->u
.kprobe
.addr
=
1048 old_uevent_param
->u
.kprobe
.addr
;
1049 uevent_param
->u
.kprobe
.offset
=
1050 old_uevent_param
->u
.kprobe
.offset
;
1051 memcpy(uevent_param
->u
.kprobe
.symbol_name
,
1052 old_uevent_param
->u
.kprobe
.symbol_name
,
1053 sizeof(uevent_param
->u
.kprobe
.symbol_name
));
1055 case LTTNG_KERNEL_KRETPROBE
:
1056 uevent_param
->u
.kretprobe
.addr
=
1057 old_uevent_param
->u
.kretprobe
.addr
;
1058 uevent_param
->u
.kretprobe
.offset
=
1059 old_uevent_param
->u
.kretprobe
.offset
;
1060 memcpy(uevent_param
->u
.kretprobe
.symbol_name
,
1061 old_uevent_param
->u
.kretprobe
.symbol_name
,
1062 sizeof(uevent_param
->u
.kretprobe
.symbol_name
));
1064 case LTTNG_KERNEL_FUNCTION
:
1065 memcpy(uevent_param
->u
.ftrace
.symbol_name
,
1066 old_uevent_param
->u
.ftrace
.symbol_name
,
1067 sizeof(uevent_param
->u
.ftrace
.symbol_name
));
1072 ret
= lttng_abi_create_event(file
, uevent_param
);
1074 old_event_error_free_old_param
:
1075 kfree(old_uevent_param
);
1076 old_event_error_free_param
:
1077 kfree(uevent_param
);
1081 case LTTNG_KERNEL_EVENT
:
1083 struct lttng_kernel_event uevent_param
;
1085 if (copy_from_user(&uevent_param
,
1086 (struct lttng_kernel_event __user
*) arg
,
1087 sizeof(uevent_param
)))
1089 return lttng_abi_create_event(file
, &uevent_param
);
1091 case LTTNG_KERNEL_OLD_CONTEXT
:
1093 struct lttng_kernel_context
*ucontext_param
;
1094 struct lttng_kernel_old_context
*old_ucontext_param
;
1097 ucontext_param
= kmalloc(sizeof(struct lttng_kernel_context
),
1099 if (!ucontext_param
) {
1103 old_ucontext_param
= kmalloc(sizeof(struct lttng_kernel_old_context
),
1105 if (!old_ucontext_param
) {
1107 goto old_ctx_error_free_param
;
1110 if (copy_from_user(old_ucontext_param
,
1111 (struct lttng_kernel_old_context __user
*) arg
,
1112 sizeof(struct lttng_kernel_old_context
))) {
1114 goto old_ctx_error_free_old_param
;
1116 ucontext_param
->ctx
= old_ucontext_param
->ctx
;
1117 memcpy(ucontext_param
->padding
, old_ucontext_param
->padding
,
1118 sizeof(ucontext_param
->padding
));
1119 /* only type that uses the union */
1120 if (old_ucontext_param
->ctx
== LTTNG_KERNEL_CONTEXT_PERF_COUNTER
) {
1121 ucontext_param
->u
.perf_counter
.type
=
1122 old_ucontext_param
->u
.perf_counter
.type
;
1123 ucontext_param
->u
.perf_counter
.config
=
1124 old_ucontext_param
->u
.perf_counter
.config
;
1125 memcpy(ucontext_param
->u
.perf_counter
.name
,
1126 old_ucontext_param
->u
.perf_counter
.name
,
1127 sizeof(ucontext_param
->u
.perf_counter
.name
));
1130 ret
= lttng_abi_add_context(file
,
1132 &channel
->ctx
, channel
->session
);
1134 old_ctx_error_free_old_param
:
1135 kfree(old_ucontext_param
);
1136 old_ctx_error_free_param
:
1137 kfree(ucontext_param
);
1141 case LTTNG_KERNEL_CONTEXT
:
1143 struct lttng_kernel_context ucontext_param
;
1145 if (copy_from_user(&ucontext_param
,
1146 (struct lttng_kernel_context __user
*) arg
,
1147 sizeof(ucontext_param
)))
1149 return lttng_abi_add_context(file
,
1151 &channel
->ctx
, channel
->session
);
1153 case LTTNG_KERNEL_OLD_ENABLE
:
1154 case LTTNG_KERNEL_ENABLE
:
1155 return lttng_channel_enable(channel
);
1156 case LTTNG_KERNEL_OLD_DISABLE
:
1157 case LTTNG_KERNEL_DISABLE
:
1158 return lttng_channel_disable(channel
);
1159 case LTTNG_KERNEL_SYSCALL_MASK
:
1160 return lttng_channel_syscall_mask(channel
,
1161 (struct lttng_kernel_syscall_mask __user
*) arg
);
1163 return -ENOIOCTLCMD
;
1169 * lttng_metadata_ioctl - lttng syscall through ioctl
1175 * This ioctl implements lttng commands:
1176 * LTTNG_KERNEL_STREAM
1177 * Returns an event stream file descriptor or failure.
1179 * Channel and event file descriptors also hold a reference on the session.
1182 long lttng_metadata_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1185 case LTTNG_KERNEL_OLD_STREAM
:
1186 case LTTNG_KERNEL_STREAM
:
1187 return lttng_abi_open_metadata_stream(file
);
1189 return -ENOIOCTLCMD
;
1194 * lttng_channel_poll - lttng stream addition/removal monitoring
1199 unsigned int lttng_channel_poll(struct file
*file
, poll_table
*wait
)
1201 struct lttng_channel
*channel
= file
->private_data
;
1202 unsigned int mask
= 0;
1204 if (file
->f_mode
& FMODE_READ
) {
1205 poll_wait_set_exclusive(wait
);
1206 poll_wait(file
, channel
->ops
->get_hp_wait_queue(channel
->chan
),
1209 if (channel
->ops
->is_disabled(channel
->chan
))
1211 if (channel
->ops
->is_finalized(channel
->chan
))
1213 if (channel
->ops
->buffer_has_read_closed_stream(channel
->chan
))
1214 return POLLIN
| POLLRDNORM
;
1222 int lttng_channel_release(struct inode
*inode
, struct file
*file
)
1224 struct lttng_channel
*channel
= file
->private_data
;
1227 fput(channel
->session
->file
);
1232 int lttng_metadata_channel_release(struct inode
*inode
, struct file
*file
)
1234 struct lttng_channel
*channel
= file
->private_data
;
1237 lttng_metadata_channel_destroy(channel
);
1238 fput(channel
->session
->file
);
1244 static const struct file_operations lttng_channel_fops
= {
1245 .owner
= THIS_MODULE
,
1246 .release
= lttng_channel_release
,
1247 .poll
= lttng_channel_poll
,
1248 .unlocked_ioctl
= lttng_channel_ioctl
,
1249 #ifdef CONFIG_COMPAT
1250 .compat_ioctl
= lttng_channel_ioctl
,
1254 static const struct file_operations lttng_metadata_fops
= {
1255 .owner
= THIS_MODULE
,
1256 .release
= lttng_metadata_channel_release
,
1257 .unlocked_ioctl
= lttng_metadata_ioctl
,
1258 #ifdef CONFIG_COMPAT
1259 .compat_ioctl
= lttng_metadata_ioctl
,
1264 * lttng_event_ioctl - lttng syscall through ioctl
1270 * This ioctl implements lttng commands:
1271 * LTTNG_KERNEL_CONTEXT
1272 * Prepend a context field to each record of this event
1273 * LTTNG_KERNEL_ENABLE
1274 * Enable recording for this event (weak enable)
1275 * LTTNG_KERNEL_DISABLE
1276 * Disable recording for this event (strong disable)
1279 long lttng_event_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1281 struct lttng_event
*event
= file
->private_data
;
1284 case LTTNG_KERNEL_OLD_CONTEXT
:
1286 struct lttng_kernel_context
*ucontext_param
;
1287 struct lttng_kernel_old_context
*old_ucontext_param
;
1290 ucontext_param
= kmalloc(sizeof(struct lttng_kernel_context
),
1292 if (!ucontext_param
) {
1296 old_ucontext_param
= kmalloc(sizeof(struct lttng_kernel_old_context
),
1298 if (!old_ucontext_param
) {
1300 goto old_ctx_error_free_param
;
1303 if (copy_from_user(old_ucontext_param
,
1304 (struct lttng_kernel_old_context __user
*) arg
,
1305 sizeof(struct lttng_kernel_old_context
))) {
1307 goto old_ctx_error_free_old_param
;
1309 ucontext_param
->ctx
= old_ucontext_param
->ctx
;
1310 memcpy(ucontext_param
->padding
, old_ucontext_param
->padding
,
1311 sizeof(ucontext_param
->padding
));
1312 /* only type that uses the union */
1313 if (old_ucontext_param
->ctx
== LTTNG_KERNEL_CONTEXT_PERF_COUNTER
) {
1314 ucontext_param
->u
.perf_counter
.type
=
1315 old_ucontext_param
->u
.perf_counter
.type
;
1316 ucontext_param
->u
.perf_counter
.config
=
1317 old_ucontext_param
->u
.perf_counter
.config
;
1318 memcpy(ucontext_param
->u
.perf_counter
.name
,
1319 old_ucontext_param
->u
.perf_counter
.name
,
1320 sizeof(ucontext_param
->u
.perf_counter
.name
));
1323 ret
= lttng_abi_add_context(file
,
1325 &event
->ctx
, event
->chan
->session
);
1327 old_ctx_error_free_old_param
:
1328 kfree(old_ucontext_param
);
1329 old_ctx_error_free_param
:
1330 kfree(ucontext_param
);
1334 case LTTNG_KERNEL_CONTEXT
:
1336 struct lttng_kernel_context ucontext_param
;
1338 if (copy_from_user(&ucontext_param
,
1339 (struct lttng_kernel_context __user
*) arg
,
1340 sizeof(ucontext_param
)))
1342 return lttng_abi_add_context(file
,
1344 &event
->ctx
, event
->chan
->session
);
1346 case LTTNG_KERNEL_OLD_ENABLE
:
1347 case LTTNG_KERNEL_ENABLE
:
1348 return lttng_event_enable(event
);
1349 case LTTNG_KERNEL_OLD_DISABLE
:
1350 case LTTNG_KERNEL_DISABLE
:
1351 return lttng_event_disable(event
);
1353 return -ENOIOCTLCMD
;
1358 int lttng_event_release(struct inode
*inode
, struct file
*file
)
1360 struct lttng_event
*event
= file
->private_data
;
1363 fput(event
->chan
->file
);
1367 /* TODO: filter control ioctl */
1368 static const struct file_operations lttng_event_fops
= {
1369 .owner
= THIS_MODULE
,
1370 .release
= lttng_event_release
,
1371 .unlocked_ioctl
= lttng_event_ioctl
,
1372 #ifdef CONFIG_COMPAT
1373 .compat_ioctl
= lttng_event_ioctl
,
1377 static int put_u64(uint64_t val
, unsigned long arg
)
1379 return put_user(val
, (uint64_t __user
*) arg
);
1382 static long lttng_stream_ring_buffer_ioctl(struct file
*filp
,
1383 unsigned int cmd
, unsigned long arg
)
1385 struct lib_ring_buffer
*buf
= filp
->private_data
;
1386 struct channel
*chan
= buf
->backend
.chan
;
1387 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1388 const struct lttng_channel_ops
*ops
= chan
->backend
.priv_ops
;
1391 if (atomic_read(&chan
->record_disabled
))
1395 case LTTNG_RING_BUFFER_GET_TIMESTAMP_BEGIN
:
1399 ret
= ops
->timestamp_begin(config
, buf
, &ts
);
1402 return put_u64(ts
, arg
);
1404 case LTTNG_RING_BUFFER_GET_TIMESTAMP_END
:
1408 ret
= ops
->timestamp_end(config
, buf
, &ts
);
1411 return put_u64(ts
, arg
);
1413 case LTTNG_RING_BUFFER_GET_EVENTS_DISCARDED
:
1417 ret
= ops
->events_discarded(config
, buf
, &ed
);
1420 return put_u64(ed
, arg
);
1422 case LTTNG_RING_BUFFER_GET_CONTENT_SIZE
:
1426 ret
= ops
->content_size(config
, buf
, &cs
);
1429 return put_u64(cs
, arg
);
1431 case LTTNG_RING_BUFFER_GET_PACKET_SIZE
:
1435 ret
= ops
->packet_size(config
, buf
, &ps
);
1438 return put_u64(ps
, arg
);
1440 case LTTNG_RING_BUFFER_GET_STREAM_ID
:
1444 ret
= ops
->stream_id(config
, buf
, &si
);
1447 return put_u64(si
, arg
);
1449 case LTTNG_RING_BUFFER_GET_CURRENT_TIMESTAMP
:
1453 ret
= ops
->current_timestamp(config
, buf
, &ts
);
1456 return put_u64(ts
, arg
);
1459 return lib_ring_buffer_file_operations
.unlocked_ioctl(filp
,
1467 #ifdef CONFIG_COMPAT
1468 static long lttng_stream_ring_buffer_compat_ioctl(struct file
*filp
,
1469 unsigned int cmd
, unsigned long arg
)
1471 struct lib_ring_buffer
*buf
= filp
->private_data
;
1472 struct channel
*chan
= buf
->backend
.chan
;
1473 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1474 const struct lttng_channel_ops
*ops
= chan
->backend
.priv_ops
;
1477 if (atomic_read(&chan
->record_disabled
))
1481 case LTTNG_RING_BUFFER_COMPAT_GET_TIMESTAMP_BEGIN
:
1485 ret
= ops
->timestamp_begin(config
, buf
, &ts
);
1488 return put_u64(ts
, arg
);
1490 case LTTNG_RING_BUFFER_COMPAT_GET_TIMESTAMP_END
:
1494 ret
= ops
->timestamp_end(config
, buf
, &ts
);
1497 return put_u64(ts
, arg
);
1499 case LTTNG_RING_BUFFER_COMPAT_GET_EVENTS_DISCARDED
:
1503 ret
= ops
->events_discarded(config
, buf
, &ed
);
1506 return put_u64(ed
, arg
);
1508 case LTTNG_RING_BUFFER_COMPAT_GET_CONTENT_SIZE
:
1512 ret
= ops
->content_size(config
, buf
, &cs
);
1515 return put_u64(cs
, arg
);
1517 case LTTNG_RING_BUFFER_COMPAT_GET_PACKET_SIZE
:
1521 ret
= ops
->packet_size(config
, buf
, &ps
);
1524 return put_u64(ps
, arg
);
1526 case LTTNG_RING_BUFFER_COMPAT_GET_STREAM_ID
:
1530 ret
= ops
->stream_id(config
, buf
, &si
);
1533 return put_u64(si
, arg
);
1535 case LTTNG_RING_BUFFER_GET_CURRENT_TIMESTAMP
:
1539 ret
= ops
->current_timestamp(config
, buf
, &ts
);
1542 return put_u64(ts
, arg
);
1545 return lib_ring_buffer_file_operations
.compat_ioctl(filp
,
1552 #endif /* CONFIG_COMPAT */
1554 static void lttng_stream_override_ring_buffer_fops(void)
1556 lttng_stream_ring_buffer_file_operations
.owner
= THIS_MODULE
;
1557 lttng_stream_ring_buffer_file_operations
.open
=
1558 lib_ring_buffer_file_operations
.open
;
1559 lttng_stream_ring_buffer_file_operations
.release
=
1560 lib_ring_buffer_file_operations
.release
;
1561 lttng_stream_ring_buffer_file_operations
.poll
=
1562 lib_ring_buffer_file_operations
.poll
;
1563 lttng_stream_ring_buffer_file_operations
.splice_read
=
1564 lib_ring_buffer_file_operations
.splice_read
;
1565 lttng_stream_ring_buffer_file_operations
.mmap
=
1566 lib_ring_buffer_file_operations
.mmap
;
1567 lttng_stream_ring_buffer_file_operations
.unlocked_ioctl
=
1568 lttng_stream_ring_buffer_ioctl
;
1569 lttng_stream_ring_buffer_file_operations
.llseek
=
1570 lib_ring_buffer_file_operations
.llseek
;
1571 #ifdef CONFIG_COMPAT
1572 lttng_stream_ring_buffer_file_operations
.compat_ioctl
=
1573 lttng_stream_ring_buffer_compat_ioctl
;
1577 int __init
lttng_abi_init(void)
1581 wrapper_vmalloc_sync_all();
1582 lttng_proc_dentry
= proc_create_data("lttng", S_IRUSR
| S_IWUSR
, NULL
,
1585 if (!lttng_proc_dentry
) {
1586 printk(KERN_ERR
"Error creating LTTng control file\n");
1590 lttng_stream_override_ring_buffer_fops();
1596 /* No __exit annotation because used by init error path too. */
1597 void lttng_abi_exit(void)
1599 if (lttng_proc_dentry
)
1600 remove_proc_entry("lttng", NULL
);