6 * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; only
11 * version 2.1 of the License.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 * Mimic system calls for:
24 * - session creation, returns a file descriptor or failure.
25 * - channel creation, returns a file descriptor or failure.
26 * - Operates on a session file descriptor
27 * - Takes all channel options as parameters.
28 * - stream get, returns a file descriptor or failure.
29 * - Operates on a channel file descriptor.
30 * - stream notifier get, returns a file descriptor or failure.
31 * - Operates on a channel file descriptor.
32 * - event creation, returns a file descriptor or failure.
33 * - Operates on a channel file descriptor
34 * - Takes an event name as parameter
35 * - Takes an instrumentation source as parameter
36 * - e.g. tracepoints, dynamic_probes...
37 * - Takes instrumentation source specific arguments.
40 #include <linux/module.h>
41 #include <linux/proc_fs.h>
42 #include <linux/anon_inodes.h>
43 #include <linux/file.h>
44 #include <linux/uaccess.h>
45 #include <linux/slab.h>
46 #include <linux/err.h>
47 #include "wrapper/vmalloc.h" /* for wrapper_vmalloc_sync_all() */
48 #include "wrapper/ringbuffer/vfs.h"
49 #include "wrapper/ringbuffer/backend.h"
50 #include "wrapper/ringbuffer/frontend.h"
51 #include "wrapper/poll.h"
52 #include "lttng-abi.h"
53 #include "lttng-abi-old.h"
54 #include "lttng-events.h"
55 #include "lttng-tracer.h"
56 #include "lib/ringbuffer/frontend_types.h"
59 * This is LTTng's own personal way to create a system call as an external
60 * module. We use ioctl() on /proc/lttng.
63 static struct proc_dir_entry
*lttng_proc_dentry
;
64 static const struct file_operations lttng_fops
;
65 static const struct file_operations lttng_session_fops
;
66 static const struct file_operations lttng_channel_fops
;
67 static const struct file_operations lttng_metadata_fops
;
68 static const struct file_operations lttng_event_fops
;
69 static struct file_operations lttng_stream_ring_buffer_file_operations
;
72 * Teardown management: opened file descriptors keep a refcount on the module,
73 * so it can only exit when all file descriptors are closed.
77 int lttng_abi_create_session(void)
79 struct lttng_session
*session
;
80 struct file
*session_file
;
83 session
= lttng_session_create();
86 session_fd
= get_unused_fd();
91 session_file
= anon_inode_getfile("[lttng_session]",
94 if (IS_ERR(session_file
)) {
95 ret
= PTR_ERR(session_file
);
98 session
->file
= session_file
;
99 fd_install(session_fd
, session_file
);
103 put_unused_fd(session_fd
);
105 lttng_session_destroy(session
);
110 int lttng_abi_tracepoint_list(void)
112 struct file
*tracepoint_list_file
;
115 file_fd
= get_unused_fd();
121 tracepoint_list_file
= anon_inode_getfile("[lttng_session]",
122 <tng_tracepoint_list_fops
,
124 if (IS_ERR(tracepoint_list_file
)) {
125 ret
= PTR_ERR(tracepoint_list_file
);
128 ret
= lttng_tracepoint_list_fops
.open(NULL
, tracepoint_list_file
);
131 fd_install(file_fd
, tracepoint_list_file
);
139 fput(tracepoint_list_file
);
141 put_unused_fd(file_fd
);
147 void lttng_abi_tracer_version(struct lttng_kernel_tracer_version
*v
)
149 v
->major
= LTTNG_MODULES_MAJOR_VERSION
;
150 v
->minor
= LTTNG_MODULES_MINOR_VERSION
;
151 v
->patchlevel
= LTTNG_MODULES_PATCHLEVEL_VERSION
;
155 long lttng_abi_add_context(struct file
*file
,
156 struct lttng_kernel_context
*context_param
,
157 struct lttng_ctx
**ctx
, struct lttng_session
*session
)
160 if (session
->been_active
)
163 switch (context_param
->ctx
) {
164 case LTTNG_KERNEL_CONTEXT_PID
:
165 return lttng_add_pid_to_ctx(ctx
);
166 case LTTNG_KERNEL_CONTEXT_PRIO
:
167 return lttng_add_prio_to_ctx(ctx
);
168 case LTTNG_KERNEL_CONTEXT_NICE
:
169 return lttng_add_nice_to_ctx(ctx
);
170 case LTTNG_KERNEL_CONTEXT_VPID
:
171 return lttng_add_vpid_to_ctx(ctx
);
172 case LTTNG_KERNEL_CONTEXT_TID
:
173 return lttng_add_tid_to_ctx(ctx
);
174 case LTTNG_KERNEL_CONTEXT_VTID
:
175 return lttng_add_vtid_to_ctx(ctx
);
176 case LTTNG_KERNEL_CONTEXT_PPID
:
177 return lttng_add_ppid_to_ctx(ctx
);
178 case LTTNG_KERNEL_CONTEXT_VPPID
:
179 return lttng_add_vppid_to_ctx(ctx
);
180 case LTTNG_KERNEL_CONTEXT_PERF_COUNTER
:
181 context_param
->u
.perf_counter
.name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
182 return lttng_add_perf_counter_to_ctx(context_param
->u
.perf_counter
.type
,
183 context_param
->u
.perf_counter
.config
,
184 context_param
->u
.perf_counter
.name
,
186 case LTTNG_KERNEL_CONTEXT_PROCNAME
:
187 return lttng_add_procname_to_ctx(ctx
);
188 case LTTNG_KERNEL_CONTEXT_HOSTNAME
:
189 return lttng_add_hostname_to_ctx(ctx
);
196 * lttng_ioctl - lttng syscall through ioctl
202 * This ioctl implements lttng commands:
203 * LTTNG_KERNEL_SESSION
204 * Returns a LTTng trace session file descriptor
205 * LTTNG_KERNEL_TRACER_VERSION
206 * Returns the LTTng kernel tracer version
207 * LTTNG_KERNEL_TRACEPOINT_LIST
208 * Returns a file descriptor listing available tracepoints
209 * LTTNG_KERNEL_WAIT_QUIESCENT
210 * Returns after all previously running probes have completed
212 * The returned session will be deleted when its file descriptor is closed.
215 long lttng_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
218 case LTTNG_KERNEL_OLD_SESSION
:
219 case LTTNG_KERNEL_SESSION
:
220 return lttng_abi_create_session();
221 case LTTNG_KERNEL_OLD_TRACER_VERSION
:
223 struct lttng_kernel_tracer_version v
;
224 struct lttng_kernel_old_tracer_version oldv
;
225 struct lttng_kernel_old_tracer_version
*uversion
=
226 (struct lttng_kernel_old_tracer_version __user
*) arg
;
228 lttng_abi_tracer_version(&v
);
229 oldv
.major
= v
.major
;
230 oldv
.minor
= v
.minor
;
231 oldv
.patchlevel
= v
.patchlevel
;
233 if (copy_to_user(uversion
, &oldv
, sizeof(oldv
)))
237 case LTTNG_KERNEL_TRACER_VERSION
:
239 struct lttng_kernel_tracer_version version
;
240 struct lttng_kernel_tracer_version
*uversion
=
241 (struct lttng_kernel_tracer_version __user
*) arg
;
243 lttng_abi_tracer_version(&version
);
245 if (copy_to_user(uversion
, &version
, sizeof(version
)))
249 case LTTNG_KERNEL_OLD_TRACEPOINT_LIST
:
250 case LTTNG_KERNEL_TRACEPOINT_LIST
:
251 return lttng_abi_tracepoint_list();
252 case LTTNG_KERNEL_OLD_WAIT_QUIESCENT
:
253 case LTTNG_KERNEL_WAIT_QUIESCENT
:
256 case LTTNG_KERNEL_OLD_CALIBRATE
:
258 struct lttng_kernel_old_calibrate __user
*ucalibrate
=
259 (struct lttng_kernel_old_calibrate __user
*) arg
;
260 struct lttng_kernel_old_calibrate old_calibrate
;
261 struct lttng_kernel_calibrate calibrate
;
264 if (copy_from_user(&old_calibrate
, ucalibrate
, sizeof(old_calibrate
)))
266 calibrate
.type
= old_calibrate
.type
;
267 ret
= lttng_calibrate(&calibrate
);
268 if (copy_to_user(ucalibrate
, &old_calibrate
, sizeof(old_calibrate
)))
272 case LTTNG_KERNEL_CALIBRATE
:
274 struct lttng_kernel_calibrate __user
*ucalibrate
=
275 (struct lttng_kernel_calibrate __user
*) arg
;
276 struct lttng_kernel_calibrate calibrate
;
279 if (copy_from_user(&calibrate
, ucalibrate
, sizeof(calibrate
)))
281 ret
= lttng_calibrate(&calibrate
);
282 if (copy_to_user(ucalibrate
, &calibrate
, sizeof(calibrate
)))
291 static const struct file_operations lttng_fops
= {
292 .owner
= THIS_MODULE
,
293 .unlocked_ioctl
= lttng_ioctl
,
295 .compat_ioctl
= lttng_ioctl
,
300 int lttng_abi_create_channel(struct file
*session_file
,
301 struct lttng_kernel_channel
*chan_param
,
302 enum channel_type channel_type
)
304 struct lttng_session
*session
= session_file
->private_data
;
305 const struct file_operations
*fops
= NULL
;
306 const char *transport_name
;
307 struct lttng_channel
*chan
;
308 struct file
*chan_file
;
312 chan_fd
= get_unused_fd();
317 switch (channel_type
) {
318 case PER_CPU_CHANNEL
:
319 fops
= <tng_channel_fops
;
321 case METADATA_CHANNEL
:
322 fops
= <tng_metadata_fops
;
326 chan_file
= anon_inode_getfile("[lttng_channel]",
329 if (IS_ERR(chan_file
)) {
330 ret
= PTR_ERR(chan_file
);
333 switch (channel_type
) {
334 case PER_CPU_CHANNEL
:
335 if (chan_param
->output
== LTTNG_KERNEL_SPLICE
) {
336 transport_name
= chan_param
->overwrite
?
337 "relay-overwrite" : "relay-discard";
338 } else if (chan_param
->output
== LTTNG_KERNEL_MMAP
) {
339 transport_name
= chan_param
->overwrite
?
340 "relay-overwrite-mmap" : "relay-discard-mmap";
345 case METADATA_CHANNEL
:
346 if (chan_param
->output
== LTTNG_KERNEL_SPLICE
)
347 transport_name
= "relay-metadata";
348 else if (chan_param
->output
== LTTNG_KERNEL_MMAP
)
349 transport_name
= "relay-metadata-mmap";
354 transport_name
= "<unknown>";
358 * We tolerate no failure path after channel creation. It will stay
359 * invariant for the rest of the session.
361 chan
= lttng_channel_create(session
, transport_name
, NULL
,
362 chan_param
->subbuf_size
,
363 chan_param
->num_subbuf
,
364 chan_param
->switch_timer_interval
,
365 chan_param
->read_timer_interval
,
371 chan
->file
= chan_file
;
372 chan_file
->private_data
= chan
;
373 fd_install(chan_fd
, chan_file
);
374 atomic_long_inc(&session_file
->f_count
);
381 put_unused_fd(chan_fd
);
387 * lttng_session_ioctl - lttng session fd ioctl
393 * This ioctl implements lttng commands:
394 * LTTNG_KERNEL_CHANNEL
395 * Returns a LTTng channel file descriptor
396 * LTTNG_KERNEL_ENABLE
397 * Enables tracing for a session (weak enable)
398 * LTTNG_KERNEL_DISABLE
399 * Disables tracing for a session (strong disable)
400 * LTTNG_KERNEL_METADATA
401 * Returns a LTTng metadata file descriptor
403 * The returned channel will be deleted when its file descriptor is closed.
406 long lttng_session_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
408 struct lttng_session
*session
= file
->private_data
;
411 case LTTNG_KERNEL_OLD_CHANNEL
:
413 struct lttng_kernel_channel chan_param
;
414 struct lttng_kernel_old_channel old_chan_param
;
416 if (copy_from_user(&old_chan_param
,
417 (struct lttng_kernel_old_channel __user
*) arg
,
418 sizeof(struct lttng_kernel_old_channel
)))
420 chan_param
.overwrite
= old_chan_param
.overwrite
;
421 chan_param
.subbuf_size
= old_chan_param
.subbuf_size
;
422 chan_param
.num_subbuf
= old_chan_param
.num_subbuf
;
423 chan_param
.switch_timer_interval
= old_chan_param
.switch_timer_interval
;
424 chan_param
.read_timer_interval
= old_chan_param
.read_timer_interval
;
425 chan_param
.output
= old_chan_param
.output
;
427 return lttng_abi_create_channel(file
, &chan_param
,
430 case LTTNG_KERNEL_CHANNEL
:
432 struct lttng_kernel_channel chan_param
;
434 if (copy_from_user(&chan_param
,
435 (struct lttng_kernel_channel __user
*) arg
,
436 sizeof(struct lttng_kernel_channel
)))
438 return lttng_abi_create_channel(file
, &chan_param
,
441 case LTTNG_KERNEL_OLD_SESSION_START
:
442 case LTTNG_KERNEL_OLD_ENABLE
:
443 case LTTNG_KERNEL_SESSION_START
:
444 case LTTNG_KERNEL_ENABLE
:
445 return lttng_session_enable(session
);
446 case LTTNG_KERNEL_OLD_SESSION_STOP
:
447 case LTTNG_KERNEL_OLD_DISABLE
:
448 case LTTNG_KERNEL_SESSION_STOP
:
449 case LTTNG_KERNEL_DISABLE
:
450 return lttng_session_disable(session
);
451 case LTTNG_KERNEL_OLD_METADATA
:
453 struct lttng_kernel_channel chan_param
;
454 struct lttng_kernel_old_channel old_chan_param
;
456 if (copy_from_user(&old_chan_param
,
457 (struct lttng_kernel_old_channel __user
*) arg
,
458 sizeof(struct lttng_kernel_old_channel
)))
460 chan_param
.overwrite
= old_chan_param
.overwrite
;
461 chan_param
.subbuf_size
= old_chan_param
.subbuf_size
;
462 chan_param
.num_subbuf
= old_chan_param
.num_subbuf
;
463 chan_param
.switch_timer_interval
= old_chan_param
.switch_timer_interval
;
464 chan_param
.read_timer_interval
= old_chan_param
.read_timer_interval
;
465 chan_param
.output
= old_chan_param
.output
;
467 return lttng_abi_create_channel(file
, &chan_param
,
470 case LTTNG_KERNEL_METADATA
:
472 struct lttng_kernel_channel chan_param
;
474 if (copy_from_user(&chan_param
,
475 (struct lttng_kernel_channel __user
*) arg
,
476 sizeof(struct lttng_kernel_channel
)))
478 return lttng_abi_create_channel(file
, &chan_param
,
487 * Called when the last file reference is dropped.
489 * Big fat note: channels and events are invariant for the whole session after
490 * their creation. So this session destruction also destroys all channel and
491 * event structures specific to this session (they are not destroyed when their
492 * individual file is released).
495 int lttng_session_release(struct inode
*inode
, struct file
*file
)
497 struct lttng_session
*session
= file
->private_data
;
500 lttng_session_destroy(session
);
504 static const struct file_operations lttng_session_fops
= {
505 .owner
= THIS_MODULE
,
506 .release
= lttng_session_release
,
507 .unlocked_ioctl
= lttng_session_ioctl
,
509 .compat_ioctl
= lttng_session_ioctl
,
514 * lttng_metadata_ring_buffer_poll - LTTng ring buffer poll file operation
518 * Handles the poll operations for the metadata channels.
521 unsigned int lttng_metadata_ring_buffer_poll(struct file
*filp
,
524 struct lttng_metadata_stream
*stream
= filp
->private_data
;
525 struct lib_ring_buffer
*buf
= stream
->priv
;
527 unsigned int mask
= 0;
529 if (filp
->f_mode
& FMODE_READ
) {
530 poll_wait_set_exclusive(wait
);
531 poll_wait(filp
, &stream
->read_wait
, wait
);
533 finalized
= stream
->finalized
;
536 * lib_ring_buffer_is_finalized() contains a smp_rmb()
537 * ordering finalized load before offsets loads.
539 WARN_ON(atomic_long_read(&buf
->active_readers
) != 1);
544 mutex_lock(&stream
->metadata_cache
->lock
);
545 if (stream
->metadata_cache
->metadata_written
>
546 stream
->metadata_out
)
548 mutex_unlock(&stream
->metadata_cache
->lock
);
555 void lttng_metadata_ring_buffer_ioctl_put_next_subbuf(struct file
*filp
,
556 unsigned int cmd
, unsigned long arg
)
558 struct lttng_metadata_stream
*stream
= filp
->private_data
;
560 stream
->metadata_out
= stream
->metadata_in
;
564 long lttng_metadata_ring_buffer_ioctl(struct file
*filp
,
565 unsigned int cmd
, unsigned long arg
)
568 struct lttng_metadata_stream
*stream
= filp
->private_data
;
569 struct lib_ring_buffer
*buf
= stream
->priv
;
572 case RING_BUFFER_GET_NEXT_SUBBUF
:
574 struct lttng_metadata_stream
*stream
= filp
->private_data
;
575 struct lib_ring_buffer
*buf
= stream
->priv
;
576 struct channel
*chan
= buf
->backend
.chan
;
578 ret
= lttng_metadata_output_channel(stream
, chan
);
580 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
586 case RING_BUFFER_GET_SUBBUF
:
589 * Random access is not allowed for metadata channel.
593 case RING_BUFFER_FLUSH
:
595 struct lttng_metadata_stream
*stream
= filp
->private_data
;
596 struct lib_ring_buffer
*buf
= stream
->priv
;
597 struct channel
*chan
= buf
->backend
.chan
;
600 * Before doing the actual ring buffer flush, write up to one
601 * packet of metadata in the ring buffer.
603 ret
= lttng_metadata_output_channel(stream
, chan
);
611 /* PUT_SUBBUF is the one from lib ring buffer, unmodified. */
613 /* Performing lib ring buffer ioctl after our own. */
614 ret
= lib_ring_buffer_ioctl(filp
, cmd
, arg
, buf
);
619 case RING_BUFFER_PUT_NEXT_SUBBUF
:
621 lttng_metadata_ring_buffer_ioctl_put_next_subbuf(filp
,
634 long lttng_metadata_ring_buffer_compat_ioctl(struct file
*filp
,
635 unsigned int cmd
, unsigned long arg
)
638 struct lttng_metadata_stream
*stream
= filp
->private_data
;
639 struct lib_ring_buffer
*buf
= stream
->priv
;
642 case RING_BUFFER_GET_NEXT_SUBBUF
:
644 struct lttng_metadata_stream
*stream
= filp
->private_data
;
645 struct lib_ring_buffer
*buf
= stream
->priv
;
646 struct channel
*chan
= buf
->backend
.chan
;
648 ret
= lttng_metadata_output_channel(stream
, chan
);
650 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
656 case RING_BUFFER_GET_SUBBUF
:
659 * Random access is not allowed for metadata channel.
666 /* PUT_SUBBUF is the one from lib ring buffer, unmodified. */
668 /* Performing lib ring buffer ioctl after our own. */
669 ret
= lib_ring_buffer_compat_ioctl(filp
, cmd
, arg
, buf
);
674 case RING_BUFFER_PUT_NEXT_SUBBUF
:
676 lttng_metadata_ring_buffer_ioctl_put_next_subbuf(filp
,
689 * This is not used by anonymous file descriptors. This code is left
690 * there if we ever want to implement an inode with open() operation.
693 int lttng_metadata_ring_buffer_open(struct inode
*inode
, struct file
*file
)
695 struct lttng_metadata_stream
*stream
= inode
->i_private
;
696 struct lib_ring_buffer
*buf
= stream
->priv
;
698 file
->private_data
= buf
;
700 * Since life-time of metadata cache differs from that of
701 * session, we need to keep our own reference on the transport.
703 if (!try_module_get(stream
->transport
->owner
)) {
704 printk(KERN_WARNING
"LTT : Can't lock transport module.\n");
707 return lib_ring_buffer_open(inode
, file
, buf
);
711 int lttng_metadata_ring_buffer_release(struct inode
*inode
, struct file
*file
)
713 struct lttng_metadata_stream
*stream
= file
->private_data
;
714 struct lib_ring_buffer
*buf
= stream
->priv
;
716 kref_put(&stream
->metadata_cache
->refcount
, metadata_cache_destroy
);
717 module_put(stream
->transport
->owner
);
718 return lib_ring_buffer_release(inode
, file
, buf
);
722 ssize_t
lttng_metadata_ring_buffer_splice_read(struct file
*in
, loff_t
*ppos
,
723 struct pipe_inode_info
*pipe
, size_t len
,
726 struct lttng_metadata_stream
*stream
= in
->private_data
;
727 struct lib_ring_buffer
*buf
= stream
->priv
;
729 return lib_ring_buffer_splice_read(in
, ppos
, pipe
, len
,
734 int lttng_metadata_ring_buffer_mmap(struct file
*filp
,
735 struct vm_area_struct
*vma
)
737 struct lttng_metadata_stream
*stream
= filp
->private_data
;
738 struct lib_ring_buffer
*buf
= stream
->priv
;
740 return lib_ring_buffer_mmap(filp
, vma
, buf
);
744 const struct file_operations lttng_metadata_ring_buffer_file_operations
= {
745 .owner
= THIS_MODULE
,
746 .open
= lttng_metadata_ring_buffer_open
,
747 .release
= lttng_metadata_ring_buffer_release
,
748 .poll
= lttng_metadata_ring_buffer_poll
,
749 .splice_read
= lttng_metadata_ring_buffer_splice_read
,
750 .mmap
= lttng_metadata_ring_buffer_mmap
,
751 .unlocked_ioctl
= lttng_metadata_ring_buffer_ioctl
,
752 .llseek
= vfs_lib_ring_buffer_no_llseek
,
754 .compat_ioctl
= lttng_metadata_ring_buffer_compat_ioctl
,
759 int lttng_abi_create_stream_fd(struct file
*channel_file
, void *stream_priv
,
760 const struct file_operations
*fops
)
763 struct file
*stream_file
;
765 stream_fd
= get_unused_fd();
770 stream_file
= anon_inode_getfile("[lttng_stream]", fops
,
771 stream_priv
, O_RDWR
);
772 if (IS_ERR(stream_file
)) {
773 ret
= PTR_ERR(stream_file
);
777 * OPEN_FMODE, called within anon_inode_getfile/alloc_file, don't honor
778 * FMODE_LSEEK, FMODE_PREAD nor FMODE_PWRITE. We need to read from this
779 * file descriptor, so we set FMODE_PREAD here.
781 stream_file
->f_mode
|= FMODE_PREAD
;
782 fd_install(stream_fd
, stream_file
);
784 * The stream holds a reference to the channel within the generic ring
785 * buffer library, so no need to hold a refcount on the channel and
786 * session files here.
791 put_unused_fd(stream_fd
);
797 int lttng_abi_open_stream(struct file
*channel_file
)
799 struct lttng_channel
*channel
= channel_file
->private_data
;
800 struct lib_ring_buffer
*buf
;
804 buf
= channel
->ops
->buffer_read_open(channel
->chan
);
809 ret
= lttng_abi_create_stream_fd(channel_file
, stream_priv
,
810 <tng_stream_ring_buffer_file_operations
);
817 channel
->ops
->buffer_read_close(buf
);
822 int lttng_abi_open_metadata_stream(struct file
*channel_file
)
824 struct lttng_channel
*channel
= channel_file
->private_data
;
825 struct lttng_session
*session
= channel
->session
;
826 struct lib_ring_buffer
*buf
;
828 struct lttng_metadata_stream
*metadata_stream
;
831 buf
= channel
->ops
->buffer_read_open(channel
->chan
);
835 metadata_stream
= kzalloc(sizeof(struct lttng_metadata_stream
),
837 if (!metadata_stream
) {
841 metadata_stream
->metadata_cache
= session
->metadata_cache
;
842 init_waitqueue_head(&metadata_stream
->read_wait
);
843 metadata_stream
->priv
= buf
;
844 stream_priv
= metadata_stream
;
845 metadata_stream
->transport
= channel
->transport
;
848 * Since life-time of metadata cache differs from that of
849 * session, we need to keep our own reference on the transport.
851 if (!try_module_get(metadata_stream
->transport
->owner
)) {
852 printk(KERN_WARNING
"LTT : Can't lock transport module.\n");
857 ret
= lttng_abi_create_stream_fd(channel_file
, stream_priv
,
858 <tng_metadata_ring_buffer_file_operations
);
862 kref_get(&session
->metadata_cache
->refcount
);
863 list_add(&metadata_stream
->list
,
864 &session
->metadata_cache
->metadata_stream
);
868 module_put(metadata_stream
->transport
->owner
);
870 kfree(metadata_stream
);
872 channel
->ops
->buffer_read_close(buf
);
877 int lttng_abi_create_event(struct file
*channel_file
,
878 struct lttng_kernel_event
*event_param
)
880 struct lttng_channel
*channel
= channel_file
->private_data
;
881 struct lttng_event
*event
;
883 struct file
*event_file
;
885 event_param
->name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
886 switch (event_param
->instrumentation
) {
887 case LTTNG_KERNEL_KRETPROBE
:
888 event_param
->u
.kretprobe
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
890 case LTTNG_KERNEL_KPROBE
:
891 event_param
->u
.kprobe
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
893 case LTTNG_KERNEL_FUNCTION
:
894 event_param
->u
.ftrace
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
899 switch (event_param
->instrumentation
) {
901 event_fd
= get_unused_fd();
906 event_file
= anon_inode_getfile("[lttng_event]",
909 if (IS_ERR(event_file
)) {
910 ret
= PTR_ERR(event_file
);
914 * We tolerate no failure path after event creation. It
915 * will stay invariant for the rest of the session.
917 event
= lttng_event_create(channel
, event_param
, NULL
, NULL
);
918 WARN_ON_ONCE(!event
);
920 ret
= PTR_ERR(event
);
923 event_file
->private_data
= event
;
924 fd_install(event_fd
, event_file
);
925 /* The event holds a reference on the channel */
926 atomic_long_inc(&channel_file
->f_count
);
928 case LTTNG_KERNEL_SYSCALL
:
930 * Only all-syscall tracing supported for now.
932 if (event_param
->name
[0] != '\0')
934 ret
= lttng_syscalls_register(channel
, NULL
);
945 put_unused_fd(event_fd
);
951 * lttng_channel_ioctl - lttng syscall through ioctl
957 * This ioctl implements lttng commands:
958 * LTTNG_KERNEL_STREAM
959 * Returns an event stream file descriptor or failure.
960 * (typically, one event stream records events from one CPU)
962 * Returns an event file descriptor or failure.
963 * LTTNG_KERNEL_CONTEXT
964 * Prepend a context field to each event in the channel
965 * LTTNG_KERNEL_ENABLE
966 * Enable recording for events in this channel (weak enable)
967 * LTTNG_KERNEL_DISABLE
968 * Disable recording for events in this channel (strong disable)
970 * Channel and event file descriptors also hold a reference on the session.
973 long lttng_channel_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
975 struct lttng_channel
*channel
= file
->private_data
;
978 case LTTNG_KERNEL_OLD_STREAM
:
979 case LTTNG_KERNEL_STREAM
:
980 return lttng_abi_open_stream(file
);
981 case LTTNG_KERNEL_OLD_EVENT
:
983 struct lttng_kernel_event
*uevent_param
;
984 struct lttng_kernel_old_event
*old_uevent_param
;
987 uevent_param
= kmalloc(sizeof(struct lttng_kernel_event
),
993 old_uevent_param
= kmalloc(
994 sizeof(struct lttng_kernel_old_event
),
996 if (!old_uevent_param
) {
998 goto old_event_error_free_param
;
1000 if (copy_from_user(old_uevent_param
,
1001 (struct lttng_kernel_old_event __user
*) arg
,
1002 sizeof(struct lttng_kernel_old_event
))) {
1004 goto old_event_error_free_old_param
;
1007 memcpy(uevent_param
->name
, old_uevent_param
->name
,
1008 sizeof(uevent_param
->name
));
1009 uevent_param
->instrumentation
=
1010 old_uevent_param
->instrumentation
;
1012 switch (old_uevent_param
->instrumentation
) {
1013 case LTTNG_KERNEL_KPROBE
:
1014 uevent_param
->u
.kprobe
.addr
=
1015 old_uevent_param
->u
.kprobe
.addr
;
1016 uevent_param
->u
.kprobe
.offset
=
1017 old_uevent_param
->u
.kprobe
.offset
;
1018 memcpy(uevent_param
->u
.kprobe
.symbol_name
,
1019 old_uevent_param
->u
.kprobe
.symbol_name
,
1020 sizeof(uevent_param
->u
.kprobe
.symbol_name
));
1022 case LTTNG_KERNEL_KRETPROBE
:
1023 uevent_param
->u
.kretprobe
.addr
=
1024 old_uevent_param
->u
.kretprobe
.addr
;
1025 uevent_param
->u
.kretprobe
.offset
=
1026 old_uevent_param
->u
.kretprobe
.offset
;
1027 memcpy(uevent_param
->u
.kretprobe
.symbol_name
,
1028 old_uevent_param
->u
.kretprobe
.symbol_name
,
1029 sizeof(uevent_param
->u
.kretprobe
.symbol_name
));
1031 case LTTNG_KERNEL_FUNCTION
:
1032 memcpy(uevent_param
->u
.ftrace
.symbol_name
,
1033 old_uevent_param
->u
.ftrace
.symbol_name
,
1034 sizeof(uevent_param
->u
.ftrace
.symbol_name
));
1039 ret
= lttng_abi_create_event(file
, uevent_param
);
1041 old_event_error_free_old_param
:
1042 kfree(old_uevent_param
);
1043 old_event_error_free_param
:
1044 kfree(uevent_param
);
1048 case LTTNG_KERNEL_EVENT
:
1050 struct lttng_kernel_event uevent_param
;
1052 if (copy_from_user(&uevent_param
,
1053 (struct lttng_kernel_event __user
*) arg
,
1054 sizeof(uevent_param
)))
1056 return lttng_abi_create_event(file
, &uevent_param
);
1058 case LTTNG_KERNEL_OLD_CONTEXT
:
1060 struct lttng_kernel_context
*ucontext_param
;
1061 struct lttng_kernel_old_context
*old_ucontext_param
;
1064 ucontext_param
= kmalloc(sizeof(struct lttng_kernel_context
),
1066 if (!ucontext_param
) {
1070 old_ucontext_param
= kmalloc(sizeof(struct lttng_kernel_old_context
),
1072 if (!old_ucontext_param
) {
1074 goto old_ctx_error_free_param
;
1077 if (copy_from_user(old_ucontext_param
,
1078 (struct lttng_kernel_old_context __user
*) arg
,
1079 sizeof(struct lttng_kernel_old_context
))) {
1081 goto old_ctx_error_free_old_param
;
1083 ucontext_param
->ctx
= old_ucontext_param
->ctx
;
1084 memcpy(ucontext_param
->padding
, old_ucontext_param
->padding
,
1085 sizeof(ucontext_param
->padding
));
1086 /* only type that uses the union */
1087 if (old_ucontext_param
->ctx
== LTTNG_KERNEL_CONTEXT_PERF_COUNTER
) {
1088 ucontext_param
->u
.perf_counter
.type
=
1089 old_ucontext_param
->u
.perf_counter
.type
;
1090 ucontext_param
->u
.perf_counter
.config
=
1091 old_ucontext_param
->u
.perf_counter
.config
;
1092 memcpy(ucontext_param
->u
.perf_counter
.name
,
1093 old_ucontext_param
->u
.perf_counter
.name
,
1094 sizeof(ucontext_param
->u
.perf_counter
.name
));
1097 ret
= lttng_abi_add_context(file
,
1099 &channel
->ctx
, channel
->session
);
1101 old_ctx_error_free_old_param
:
1102 kfree(old_ucontext_param
);
1103 old_ctx_error_free_param
:
1104 kfree(ucontext_param
);
1108 case LTTNG_KERNEL_CONTEXT
:
1110 struct lttng_kernel_context ucontext_param
;
1112 if (copy_from_user(&ucontext_param
,
1113 (struct lttng_kernel_context __user
*) arg
,
1114 sizeof(ucontext_param
)))
1116 return lttng_abi_add_context(file
,
1118 &channel
->ctx
, channel
->session
);
1120 case LTTNG_KERNEL_OLD_ENABLE
:
1121 case LTTNG_KERNEL_ENABLE
:
1122 return lttng_channel_enable(channel
);
1123 case LTTNG_KERNEL_OLD_DISABLE
:
1124 case LTTNG_KERNEL_DISABLE
:
1125 return lttng_channel_disable(channel
);
1127 return -ENOIOCTLCMD
;
1133 * lttng_metadata_ioctl - lttng syscall through ioctl
1139 * This ioctl implements lttng commands:
1140 * LTTNG_KERNEL_STREAM
1141 * Returns an event stream file descriptor or failure.
1143 * Channel and event file descriptors also hold a reference on the session.
1146 long lttng_metadata_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1149 case LTTNG_KERNEL_OLD_STREAM
:
1150 case LTTNG_KERNEL_STREAM
:
1151 return lttng_abi_open_metadata_stream(file
);
1153 return -ENOIOCTLCMD
;
1158 * lttng_channel_poll - lttng stream addition/removal monitoring
1163 unsigned int lttng_channel_poll(struct file
*file
, poll_table
*wait
)
1165 struct lttng_channel
*channel
= file
->private_data
;
1166 unsigned int mask
= 0;
1168 if (file
->f_mode
& FMODE_READ
) {
1169 poll_wait_set_exclusive(wait
);
1170 poll_wait(file
, channel
->ops
->get_hp_wait_queue(channel
->chan
),
1173 if (channel
->ops
->is_disabled(channel
->chan
))
1175 if (channel
->ops
->is_finalized(channel
->chan
))
1177 if (channel
->ops
->buffer_has_read_closed_stream(channel
->chan
))
1178 return POLLIN
| POLLRDNORM
;
1186 int lttng_channel_release(struct inode
*inode
, struct file
*file
)
1188 struct lttng_channel
*channel
= file
->private_data
;
1191 fput(channel
->session
->file
);
1196 int lttng_metadata_channel_release(struct inode
*inode
, struct file
*file
)
1198 struct lttng_channel
*channel
= file
->private_data
;
1201 lttng_metadata_channel_destroy(channel
);
1202 fput(channel
->session
->file
);
1208 static const struct file_operations lttng_channel_fops
= {
1209 .owner
= THIS_MODULE
,
1210 .release
= lttng_channel_release
,
1211 .poll
= lttng_channel_poll
,
1212 .unlocked_ioctl
= lttng_channel_ioctl
,
1213 #ifdef CONFIG_COMPAT
1214 .compat_ioctl
= lttng_channel_ioctl
,
1218 static const struct file_operations lttng_metadata_fops
= {
1219 .owner
= THIS_MODULE
,
1220 .release
= lttng_metadata_channel_release
,
1221 .unlocked_ioctl
= lttng_metadata_ioctl
,
1222 #ifdef CONFIG_COMPAT
1223 .compat_ioctl
= lttng_metadata_ioctl
,
1228 * lttng_event_ioctl - lttng syscall through ioctl
1234 * This ioctl implements lttng commands:
1235 * LTTNG_KERNEL_CONTEXT
1236 * Prepend a context field to each record of this event
1237 * LTTNG_KERNEL_ENABLE
1238 * Enable recording for this event (weak enable)
1239 * LTTNG_KERNEL_DISABLE
1240 * Disable recording for this event (strong disable)
1243 long lttng_event_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1245 struct lttng_event
*event
= file
->private_data
;
1248 case LTTNG_KERNEL_OLD_CONTEXT
:
1250 struct lttng_kernel_context
*ucontext_param
;
1251 struct lttng_kernel_old_context
*old_ucontext_param
;
1254 ucontext_param
= kmalloc(sizeof(struct lttng_kernel_context
),
1256 if (!ucontext_param
) {
1260 old_ucontext_param
= kmalloc(sizeof(struct lttng_kernel_old_context
),
1262 if (!old_ucontext_param
) {
1264 goto old_ctx_error_free_param
;
1267 if (copy_from_user(old_ucontext_param
,
1268 (struct lttng_kernel_old_context __user
*) arg
,
1269 sizeof(struct lttng_kernel_old_context
))) {
1271 goto old_ctx_error_free_old_param
;
1273 ucontext_param
->ctx
= old_ucontext_param
->ctx
;
1274 memcpy(ucontext_param
->padding
, old_ucontext_param
->padding
,
1275 sizeof(ucontext_param
->padding
));
1276 /* only type that uses the union */
1277 if (old_ucontext_param
->ctx
== LTTNG_KERNEL_CONTEXT_PERF_COUNTER
) {
1278 ucontext_param
->u
.perf_counter
.type
=
1279 old_ucontext_param
->u
.perf_counter
.type
;
1280 ucontext_param
->u
.perf_counter
.config
=
1281 old_ucontext_param
->u
.perf_counter
.config
;
1282 memcpy(ucontext_param
->u
.perf_counter
.name
,
1283 old_ucontext_param
->u
.perf_counter
.name
,
1284 sizeof(ucontext_param
->u
.perf_counter
.name
));
1287 ret
= lttng_abi_add_context(file
,
1289 &event
->ctx
, event
->chan
->session
);
1291 old_ctx_error_free_old_param
:
1292 kfree(old_ucontext_param
);
1293 old_ctx_error_free_param
:
1294 kfree(ucontext_param
);
1298 case LTTNG_KERNEL_CONTEXT
:
1300 struct lttng_kernel_context ucontext_param
;
1302 if (copy_from_user(&ucontext_param
,
1303 (struct lttng_kernel_context __user
*) arg
,
1304 sizeof(ucontext_param
)))
1306 return lttng_abi_add_context(file
,
1308 &event
->ctx
, event
->chan
->session
);
1310 case LTTNG_KERNEL_OLD_ENABLE
:
1311 case LTTNG_KERNEL_ENABLE
:
1312 return lttng_event_enable(event
);
1313 case LTTNG_KERNEL_OLD_DISABLE
:
1314 case LTTNG_KERNEL_DISABLE
:
1315 return lttng_event_disable(event
);
1317 return -ENOIOCTLCMD
;
1322 int lttng_event_release(struct inode
*inode
, struct file
*file
)
1324 struct lttng_event
*event
= file
->private_data
;
1327 fput(event
->chan
->file
);
1331 /* TODO: filter control ioctl */
1332 static const struct file_operations lttng_event_fops
= {
1333 .owner
= THIS_MODULE
,
1334 .release
= lttng_event_release
,
1335 .unlocked_ioctl
= lttng_event_ioctl
,
1336 #ifdef CONFIG_COMPAT
1337 .compat_ioctl
= lttng_event_ioctl
,
1341 static int put_u64(uint64_t val
, unsigned long arg
)
1343 return put_user(val
, (uint64_t __user
*) arg
);
1346 static long lttng_stream_ring_buffer_ioctl(struct file
*filp
,
1347 unsigned int cmd
, unsigned long arg
)
1349 struct lib_ring_buffer
*buf
= filp
->private_data
;
1350 struct channel
*chan
= buf
->backend
.chan
;
1351 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1352 const struct lttng_channel_ops
*ops
= chan
->backend
.priv_ops
;
1355 if (atomic_read(&chan
->record_disabled
))
1359 case LTTNG_RING_BUFFER_GET_TIMESTAMP_BEGIN
:
1363 ret
= ops
->timestamp_begin(config
, buf
, &ts
);
1366 return put_u64(ts
, arg
);
1368 case LTTNG_RING_BUFFER_GET_TIMESTAMP_END
:
1372 ret
= ops
->timestamp_end(config
, buf
, &ts
);
1375 return put_u64(ts
, arg
);
1377 case LTTNG_RING_BUFFER_GET_EVENTS_DISCARDED
:
1381 ret
= ops
->events_discarded(config
, buf
, &ed
);
1384 return put_u64(ed
, arg
);
1386 case LTTNG_RING_BUFFER_GET_CONTENT_SIZE
:
1390 ret
= ops
->content_size(config
, buf
, &cs
);
1393 return put_u64(cs
, arg
);
1395 case LTTNG_RING_BUFFER_GET_PACKET_SIZE
:
1399 ret
= ops
->packet_size(config
, buf
, &ps
);
1402 return put_u64(ps
, arg
);
1404 case LTTNG_RING_BUFFER_GET_STREAM_ID
:
1408 ret
= ops
->stream_id(config
, buf
, &si
);
1411 return put_u64(si
, arg
);
1413 case LTTNG_RING_BUFFER_GET_CURRENT_TIMESTAMP
:
1417 ret
= ops
->current_timestamp(config
, buf
, &ts
);
1420 return put_u64(ts
, arg
);
1423 return lib_ring_buffer_file_operations
.unlocked_ioctl(filp
,
1431 #ifdef CONFIG_COMPAT
1432 static long lttng_stream_ring_buffer_compat_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_COMPAT_GET_TIMESTAMP_BEGIN
:
1449 ret
= ops
->timestamp_begin(config
, buf
, &ts
);
1452 return put_u64(ts
, arg
);
1454 case LTTNG_RING_BUFFER_COMPAT_GET_TIMESTAMP_END
:
1458 ret
= ops
->timestamp_end(config
, buf
, &ts
);
1461 return put_u64(ts
, arg
);
1463 case LTTNG_RING_BUFFER_COMPAT_GET_EVENTS_DISCARDED
:
1467 ret
= ops
->events_discarded(config
, buf
, &ed
);
1470 return put_u64(ed
, arg
);
1472 case LTTNG_RING_BUFFER_COMPAT_GET_CONTENT_SIZE
:
1476 ret
= ops
->content_size(config
, buf
, &cs
);
1479 return put_u64(cs
, arg
);
1481 case LTTNG_RING_BUFFER_COMPAT_GET_PACKET_SIZE
:
1485 ret
= ops
->packet_size(config
, buf
, &ps
);
1488 return put_u64(ps
, arg
);
1490 case LTTNG_RING_BUFFER_COMPAT_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
.compat_ioctl(filp
,
1516 #endif /* CONFIG_COMPAT */
1518 static void lttng_stream_override_ring_buffer_fops(void)
1520 lttng_stream_ring_buffer_file_operations
.owner
= THIS_MODULE
;
1521 lttng_stream_ring_buffer_file_operations
.open
=
1522 lib_ring_buffer_file_operations
.open
;
1523 lttng_stream_ring_buffer_file_operations
.release
=
1524 lib_ring_buffer_file_operations
.release
;
1525 lttng_stream_ring_buffer_file_operations
.poll
=
1526 lib_ring_buffer_file_operations
.poll
;
1527 lttng_stream_ring_buffer_file_operations
.splice_read
=
1528 lib_ring_buffer_file_operations
.splice_read
;
1529 lttng_stream_ring_buffer_file_operations
.mmap
=
1530 lib_ring_buffer_file_operations
.mmap
;
1531 lttng_stream_ring_buffer_file_operations
.unlocked_ioctl
=
1532 lttng_stream_ring_buffer_ioctl
;
1533 lttng_stream_ring_buffer_file_operations
.llseek
=
1534 lib_ring_buffer_file_operations
.llseek
;
1535 #ifdef CONFIG_COMPAT
1536 lttng_stream_ring_buffer_file_operations
.compat_ioctl
=
1537 lttng_stream_ring_buffer_compat_ioctl
;
1541 int __init
lttng_abi_init(void)
1545 wrapper_vmalloc_sync_all();
1546 lttng_proc_dentry
= proc_create_data("lttng", S_IRUSR
| S_IWUSR
, NULL
,
1549 if (!lttng_proc_dentry
) {
1550 printk(KERN_ERR
"Error creating LTTng control file\n");
1554 lttng_stream_override_ring_buffer_fops();
1560 /* No __exit annotation because used by init error path too. */
1561 void lttng_abi_exit(void)
1563 if (lttng_proc_dentry
)
1564 remove_proc_entry("lttng", NULL
);