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 "wrapper/vmalloc.h" /* for wrapper_vmalloc_sync_all() */
47 #include "wrapper/ringbuffer/vfs.h"
48 #include "wrapper/ringbuffer/backend.h"
49 #include "wrapper/ringbuffer/frontend.h"
50 #include "wrapper/poll.h"
51 #include "lttng-abi.h"
52 #include "lttng-abi-old.h"
53 #include "lttng-events.h"
54 #include "lttng-tracer.h"
55 #include "lib/ringbuffer/frontend_types.h"
58 * This is LTTng's own personal way to create a system call as an external
59 * module. We use ioctl() on /proc/lttng.
62 static struct proc_dir_entry
*lttng_proc_dentry
;
63 static const struct file_operations lttng_fops
;
64 static const struct file_operations lttng_session_fops
;
65 static const struct file_operations lttng_channel_fops
;
66 static const struct file_operations lttng_metadata_fops
;
67 static const struct file_operations lttng_event_fops
;
68 static struct file_operations lttng_stream_ring_buffer_file_operations
;
71 * Teardown management: opened file descriptors keep a refcount on the module,
72 * so it can only exit when all file descriptors are closed.
76 int lttng_abi_create_session(void)
78 struct lttng_session
*session
;
79 struct file
*session_file
;
82 session
= lttng_session_create();
85 session_fd
= get_unused_fd();
90 session_file
= anon_inode_getfile("[lttng_session]",
93 if (IS_ERR(session_file
)) {
94 ret
= PTR_ERR(session_file
);
97 session
->file
= session_file
;
98 fd_install(session_fd
, session_file
);
102 put_unused_fd(session_fd
);
104 lttng_session_destroy(session
);
109 int lttng_abi_tracepoint_list(void)
111 struct file
*tracepoint_list_file
;
114 file_fd
= get_unused_fd();
120 tracepoint_list_file
= anon_inode_getfile("[lttng_session]",
121 <tng_tracepoint_list_fops
,
123 if (IS_ERR(tracepoint_list_file
)) {
124 ret
= PTR_ERR(tracepoint_list_file
);
127 ret
= lttng_tracepoint_list_fops
.open(NULL
, tracepoint_list_file
);
130 fd_install(file_fd
, tracepoint_list_file
);
138 fput(tracepoint_list_file
);
140 put_unused_fd(file_fd
);
146 void lttng_abi_tracer_version(struct lttng_kernel_tracer_version
*v
)
148 v
->major
= LTTNG_MODULES_MAJOR_VERSION
;
149 v
->minor
= LTTNG_MODULES_MINOR_VERSION
;
150 v
->patchlevel
= LTTNG_MODULES_PATCHLEVEL_VERSION
;
154 long lttng_abi_add_context(struct file
*file
,
155 struct lttng_kernel_context
*context_param
,
156 struct lttng_ctx
**ctx
, struct lttng_session
*session
)
159 if (session
->been_active
)
162 switch (context_param
->ctx
) {
163 case LTTNG_KERNEL_CONTEXT_PID
:
164 return lttng_add_pid_to_ctx(ctx
);
165 case LTTNG_KERNEL_CONTEXT_PRIO
:
166 return lttng_add_prio_to_ctx(ctx
);
167 case LTTNG_KERNEL_CONTEXT_NICE
:
168 return lttng_add_nice_to_ctx(ctx
);
169 case LTTNG_KERNEL_CONTEXT_VPID
:
170 return lttng_add_vpid_to_ctx(ctx
);
171 case LTTNG_KERNEL_CONTEXT_TID
:
172 return lttng_add_tid_to_ctx(ctx
);
173 case LTTNG_KERNEL_CONTEXT_VTID
:
174 return lttng_add_vtid_to_ctx(ctx
);
175 case LTTNG_KERNEL_CONTEXT_PPID
:
176 return lttng_add_ppid_to_ctx(ctx
);
177 case LTTNG_KERNEL_CONTEXT_VPPID
:
178 return lttng_add_vppid_to_ctx(ctx
);
179 case LTTNG_KERNEL_CONTEXT_PERF_COUNTER
:
180 context_param
->u
.perf_counter
.name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
181 return lttng_add_perf_counter_to_ctx(context_param
->u
.perf_counter
.type
,
182 context_param
->u
.perf_counter
.config
,
183 context_param
->u
.perf_counter
.name
,
185 case LTTNG_KERNEL_CONTEXT_PROCNAME
:
186 return lttng_add_procname_to_ctx(ctx
);
187 case LTTNG_KERNEL_CONTEXT_HOSTNAME
:
188 return lttng_add_hostname_to_ctx(ctx
);
195 * lttng_ioctl - lttng syscall through ioctl
201 * This ioctl implements lttng commands:
202 * LTTNG_KERNEL_SESSION
203 * Returns a LTTng trace session file descriptor
204 * LTTNG_KERNEL_TRACER_VERSION
205 * Returns the LTTng kernel tracer version
206 * LTTNG_KERNEL_TRACEPOINT_LIST
207 * Returns a file descriptor listing available tracepoints
208 * LTTNG_KERNEL_WAIT_QUIESCENT
209 * Returns after all previously running probes have completed
211 * The returned session will be deleted when its file descriptor is closed.
214 long lttng_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
217 case LTTNG_KERNEL_OLD_SESSION
:
218 case LTTNG_KERNEL_SESSION
:
219 return lttng_abi_create_session();
220 case LTTNG_KERNEL_OLD_TRACER_VERSION
:
222 struct lttng_kernel_tracer_version v
;
223 struct lttng_kernel_old_tracer_version oldv
;
224 struct lttng_kernel_old_tracer_version
*uversion
=
225 (struct lttng_kernel_old_tracer_version __user
*) arg
;
227 lttng_abi_tracer_version(&v
);
228 oldv
.major
= v
.major
;
229 oldv
.minor
= v
.minor
;
230 oldv
.patchlevel
= v
.patchlevel
;
232 if (copy_to_user(uversion
, &oldv
, sizeof(oldv
)))
236 case LTTNG_KERNEL_TRACER_VERSION
:
238 struct lttng_kernel_tracer_version version
;
239 struct lttng_kernel_tracer_version
*uversion
=
240 (struct lttng_kernel_tracer_version __user
*) arg
;
242 lttng_abi_tracer_version(&version
);
244 if (copy_to_user(uversion
, &version
, sizeof(version
)))
248 case LTTNG_KERNEL_OLD_TRACEPOINT_LIST
:
249 case LTTNG_KERNEL_TRACEPOINT_LIST
:
250 return lttng_abi_tracepoint_list();
251 case LTTNG_KERNEL_OLD_WAIT_QUIESCENT
:
252 case LTTNG_KERNEL_WAIT_QUIESCENT
:
255 case LTTNG_KERNEL_OLD_CALIBRATE
:
257 struct lttng_kernel_old_calibrate __user
*ucalibrate
=
258 (struct lttng_kernel_old_calibrate __user
*) arg
;
259 struct lttng_kernel_old_calibrate old_calibrate
;
260 struct lttng_kernel_calibrate calibrate
;
263 if (copy_from_user(&old_calibrate
, ucalibrate
, sizeof(old_calibrate
)))
265 calibrate
.type
= old_calibrate
.type
;
266 ret
= lttng_calibrate(&calibrate
);
267 if (copy_to_user(ucalibrate
, &old_calibrate
, sizeof(old_calibrate
)))
271 case LTTNG_KERNEL_CALIBRATE
:
273 struct lttng_kernel_calibrate __user
*ucalibrate
=
274 (struct lttng_kernel_calibrate __user
*) arg
;
275 struct lttng_kernel_calibrate calibrate
;
278 if (copy_from_user(&calibrate
, ucalibrate
, sizeof(calibrate
)))
280 ret
= lttng_calibrate(&calibrate
);
281 if (copy_to_user(ucalibrate
, &calibrate
, sizeof(calibrate
)))
290 static const struct file_operations lttng_fops
= {
291 .owner
= THIS_MODULE
,
292 .unlocked_ioctl
= lttng_ioctl
,
294 .compat_ioctl
= lttng_ioctl
,
299 int lttng_abi_create_channel(struct file
*session_file
,
300 struct lttng_kernel_channel
*chan_param
,
301 enum channel_type channel_type
)
303 struct lttng_session
*session
= session_file
->private_data
;
304 const struct file_operations
*fops
= NULL
;
305 const char *transport_name
;
306 struct lttng_channel
*chan
;
307 struct file
*chan_file
;
311 chan_fd
= get_unused_fd();
316 switch (channel_type
) {
317 case PER_CPU_CHANNEL
:
318 fops
= <tng_channel_fops
;
320 case METADATA_CHANNEL
:
321 fops
= <tng_metadata_fops
;
325 chan_file
= anon_inode_getfile("[lttng_channel]",
328 if (IS_ERR(chan_file
)) {
329 ret
= PTR_ERR(chan_file
);
332 switch (channel_type
) {
333 case PER_CPU_CHANNEL
:
334 if (chan_param
->output
== LTTNG_KERNEL_SPLICE
) {
335 transport_name
= chan_param
->overwrite
?
336 "relay-overwrite" : "relay-discard";
337 } else if (chan_param
->output
== LTTNG_KERNEL_MMAP
) {
338 transport_name
= chan_param
->overwrite
?
339 "relay-overwrite-mmap" : "relay-discard-mmap";
344 case METADATA_CHANNEL
:
345 if (chan_param
->output
== LTTNG_KERNEL_SPLICE
)
346 transport_name
= "relay-metadata";
347 else if (chan_param
->output
== LTTNG_KERNEL_MMAP
)
348 transport_name
= "relay-metadata-mmap";
353 transport_name
= "<unknown>";
357 * We tolerate no failure path after channel creation. It will stay
358 * invariant for the rest of the session.
360 chan
= lttng_channel_create(session
, transport_name
, NULL
,
361 chan_param
->subbuf_size
,
362 chan_param
->num_subbuf
,
363 chan_param
->switch_timer_interval
,
364 chan_param
->read_timer_interval
,
370 chan
->file
= chan_file
;
371 chan_file
->private_data
= chan
;
372 fd_install(chan_fd
, chan_file
);
373 atomic_long_inc(&session_file
->f_count
);
380 put_unused_fd(chan_fd
);
386 * lttng_session_ioctl - lttng session fd ioctl
392 * This ioctl implements lttng commands:
393 * LTTNG_KERNEL_CHANNEL
394 * Returns a LTTng channel file descriptor
395 * LTTNG_KERNEL_ENABLE
396 * Enables tracing for a session (weak enable)
397 * LTTNG_KERNEL_DISABLE
398 * Disables tracing for a session (strong disable)
399 * LTTNG_KERNEL_METADATA
400 * Returns a LTTng metadata file descriptor
402 * The returned channel will be deleted when its file descriptor is closed.
405 long lttng_session_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
407 struct lttng_session
*session
= file
->private_data
;
410 case LTTNG_KERNEL_OLD_CHANNEL
:
412 struct lttng_kernel_channel chan_param
;
413 struct lttng_kernel_old_channel old_chan_param
;
415 if (copy_from_user(&old_chan_param
,
416 (struct lttng_kernel_old_channel __user
*) arg
,
417 sizeof(struct lttng_kernel_old_channel
)))
419 chan_param
.overwrite
= old_chan_param
.overwrite
;
420 chan_param
.subbuf_size
= old_chan_param
.subbuf_size
;
421 chan_param
.num_subbuf
= old_chan_param
.num_subbuf
;
422 chan_param
.switch_timer_interval
= old_chan_param
.switch_timer_interval
;
423 chan_param
.read_timer_interval
= old_chan_param
.read_timer_interval
;
424 chan_param
.output
= old_chan_param
.output
;
426 return lttng_abi_create_channel(file
, &chan_param
,
429 case LTTNG_KERNEL_CHANNEL
:
431 struct lttng_kernel_channel chan_param
;
433 if (copy_from_user(&chan_param
,
434 (struct lttng_kernel_channel __user
*) arg
,
435 sizeof(struct lttng_kernel_channel
)))
437 return lttng_abi_create_channel(file
, &chan_param
,
440 case LTTNG_KERNEL_OLD_SESSION_START
:
441 case LTTNG_KERNEL_OLD_ENABLE
:
442 case LTTNG_KERNEL_SESSION_START
:
443 case LTTNG_KERNEL_ENABLE
:
444 return lttng_session_enable(session
);
445 case LTTNG_KERNEL_OLD_SESSION_STOP
:
446 case LTTNG_KERNEL_OLD_DISABLE
:
447 case LTTNG_KERNEL_SESSION_STOP
:
448 case LTTNG_KERNEL_DISABLE
:
449 return lttng_session_disable(session
);
450 case LTTNG_KERNEL_OLD_METADATA
:
452 struct lttng_kernel_channel chan_param
;
453 struct lttng_kernel_old_channel old_chan_param
;
455 if (copy_from_user(&old_chan_param
,
456 (struct lttng_kernel_old_channel __user
*) arg
,
457 sizeof(struct lttng_kernel_old_channel
)))
459 chan_param
.overwrite
= old_chan_param
.overwrite
;
460 chan_param
.subbuf_size
= old_chan_param
.subbuf_size
;
461 chan_param
.num_subbuf
= old_chan_param
.num_subbuf
;
462 chan_param
.switch_timer_interval
= old_chan_param
.switch_timer_interval
;
463 chan_param
.read_timer_interval
= old_chan_param
.read_timer_interval
;
464 chan_param
.output
= old_chan_param
.output
;
466 return lttng_abi_create_channel(file
, &chan_param
,
469 case LTTNG_KERNEL_METADATA
:
471 struct lttng_kernel_channel chan_param
;
473 if (copy_from_user(&chan_param
,
474 (struct lttng_kernel_channel __user
*) arg
,
475 sizeof(struct lttng_kernel_channel
)))
477 return lttng_abi_create_channel(file
, &chan_param
,
486 * Called when the last file reference is dropped.
488 * Big fat note: channels and events are invariant for the whole session after
489 * their creation. So this session destruction also destroys all channel and
490 * event structures specific to this session (they are not destroyed when their
491 * individual file is released).
494 int lttng_session_release(struct inode
*inode
, struct file
*file
)
496 struct lttng_session
*session
= file
->private_data
;
499 lttng_session_destroy(session
);
503 static const struct file_operations lttng_session_fops
= {
504 .owner
= THIS_MODULE
,
505 .release
= lttng_session_release
,
506 .unlocked_ioctl
= lttng_session_ioctl
,
508 .compat_ioctl
= lttng_session_ioctl
,
513 * lttng_metadata_ring_buffer_poll - LTTng ring buffer poll file operation
517 * Handles the poll operations for the metadata channels.
520 unsigned int lttng_metadata_ring_buffer_poll(struct file
*filp
,
523 struct lttng_metadata_stream
*stream
= filp
->private_data
;
524 struct lib_ring_buffer
*buf
= stream
->priv
;
526 unsigned int mask
= 0;
528 if (filp
->f_mode
& FMODE_READ
) {
529 poll_wait_set_exclusive(wait
);
530 poll_wait(filp
, &stream
->read_wait
, wait
);
532 finalized
= stream
->finalized
;
535 * lib_ring_buffer_is_finalized() contains a smp_rmb()
536 * ordering finalized load before offsets loads.
538 WARN_ON(atomic_long_read(&buf
->active_readers
) != 1);
543 if (stream
->metadata_cache
->metadata_written
>
544 stream
->metadata_out
)
552 void lttng_metadata_ring_buffer_ioctl_put_next_subbuf(struct file
*filp
,
553 unsigned int cmd
, unsigned long arg
)
555 struct lttng_metadata_stream
*stream
= filp
->private_data
;
557 stream
->metadata_out
= stream
->metadata_in
;
561 long lttng_metadata_ring_buffer_ioctl(struct file
*filp
,
562 unsigned int cmd
, unsigned long arg
)
565 struct lttng_metadata_stream
*stream
= filp
->private_data
;
566 struct lib_ring_buffer
*buf
= stream
->priv
;
569 case RING_BUFFER_GET_NEXT_SUBBUF
:
571 struct lttng_metadata_stream
*stream
= filp
->private_data
;
572 struct lib_ring_buffer
*buf
= stream
->priv
;
573 struct channel
*chan
= buf
->backend
.chan
;
575 ret
= lttng_metadata_output_channel(stream
, chan
);
577 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
583 case RING_BUFFER_GET_SUBBUF
:
586 * Random access is not allowed for metadata channel.
590 case RING_BUFFER_FLUSH
:
592 struct lttng_metadata_stream
*stream
= filp
->private_data
;
593 struct lib_ring_buffer
*buf
= stream
->priv
;
594 struct channel
*chan
= buf
->backend
.chan
;
597 * Before doing the actual ring buffer flush, write up to one
598 * packet of metadata in the ring buffer.
600 ret
= lttng_metadata_output_channel(stream
, chan
);
608 /* PUT_SUBBUF is the one from lib ring buffer, unmodified. */
610 /* Performing lib ring buffer ioctl after our own. */
611 ret
= lib_ring_buffer_ioctl(filp
, cmd
, arg
, buf
);
616 case RING_BUFFER_PUT_NEXT_SUBBUF
:
618 lttng_metadata_ring_buffer_ioctl_put_next_subbuf(filp
,
631 long lttng_metadata_ring_buffer_compat_ioctl(struct file
*filp
,
632 unsigned int cmd
, unsigned long arg
)
635 struct lttng_metadata_stream
*stream
= filp
->private_data
;
636 struct lib_ring_buffer
*buf
= stream
->priv
;
639 case RING_BUFFER_GET_NEXT_SUBBUF
:
641 struct lttng_metadata_stream
*stream
= filp
->private_data
;
642 struct lib_ring_buffer
*buf
= stream
->priv
;
643 struct channel
*chan
= buf
->backend
.chan
;
645 ret
= lttng_metadata_output_channel(stream
, chan
);
647 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
653 case RING_BUFFER_GET_SUBBUF
:
656 * Random access is not allowed for metadata channel.
663 /* PUT_SUBBUF is the one from lib ring buffer, unmodified. */
665 /* Performing lib ring buffer ioctl after our own. */
666 ret
= lib_ring_buffer_compat_ioctl(filp
, cmd
, arg
, buf
);
671 case RING_BUFFER_PUT_NEXT_SUBBUF
:
673 lttng_metadata_ring_buffer_ioctl_put_next_subbuf(filp
,
686 * This is not used by anonymous file descriptors. This code is left
687 * there if we ever want to implement an inode with open() operation.
690 int lttng_metadata_ring_buffer_open(struct inode
*inode
, struct file
*file
)
692 struct lttng_metadata_stream
*stream
= inode
->i_private
;
693 struct lib_ring_buffer
*buf
= stream
->priv
;
695 file
->private_data
= buf
;
697 * Since life-time of metadata cache differs from that of
698 * session, we need to keep our own reference on the transport.
700 if (!try_module_get(stream
->transport
->owner
)) {
701 printk(KERN_WARNING
"LTT : Can't lock transport module.\n");
704 return lib_ring_buffer_open(inode
, file
, buf
);
708 int lttng_metadata_ring_buffer_release(struct inode
*inode
, struct file
*file
)
710 struct lttng_metadata_stream
*stream
= file
->private_data
;
711 struct lib_ring_buffer
*buf
= stream
->priv
;
713 kref_put(&stream
->metadata_cache
->refcount
, metadata_cache_destroy
);
714 module_put(stream
->transport
->owner
);
715 return lib_ring_buffer_release(inode
, file
, buf
);
719 ssize_t
lttng_metadata_ring_buffer_splice_read(struct file
*in
, loff_t
*ppos
,
720 struct pipe_inode_info
*pipe
, size_t len
,
723 struct lttng_metadata_stream
*stream
= in
->private_data
;
724 struct lib_ring_buffer
*buf
= stream
->priv
;
726 return lib_ring_buffer_splice_read(in
, ppos
, pipe
, len
,
731 int lttng_metadata_ring_buffer_mmap(struct file
*filp
,
732 struct vm_area_struct
*vma
)
734 struct lttng_metadata_stream
*stream
= filp
->private_data
;
735 struct lib_ring_buffer
*buf
= stream
->priv
;
737 return lib_ring_buffer_mmap(filp
, vma
, buf
);
741 const struct file_operations lttng_metadata_ring_buffer_file_operations
= {
742 .owner
= THIS_MODULE
,
743 .open
= lttng_metadata_ring_buffer_open
,
744 .release
= lttng_metadata_ring_buffer_release
,
745 .poll
= lttng_metadata_ring_buffer_poll
,
746 .splice_read
= lttng_metadata_ring_buffer_splice_read
,
747 .mmap
= lttng_metadata_ring_buffer_mmap
,
748 .unlocked_ioctl
= lttng_metadata_ring_buffer_ioctl
,
749 .llseek
= vfs_lib_ring_buffer_no_llseek
,
751 .compat_ioctl
= lttng_metadata_ring_buffer_compat_ioctl
,
756 int lttng_abi_create_stream_fd(struct file
*channel_file
, void *stream_priv
,
757 const struct file_operations
*fops
)
760 struct file
*stream_file
;
762 stream_fd
= get_unused_fd();
767 stream_file
= anon_inode_getfile("[lttng_stream]", fops
,
768 stream_priv
, O_RDWR
);
769 if (IS_ERR(stream_file
)) {
770 ret
= PTR_ERR(stream_file
);
774 * OPEN_FMODE, called within anon_inode_getfile/alloc_file, don't honor
775 * FMODE_LSEEK, FMODE_PREAD nor FMODE_PWRITE. We need to read from this
776 * file descriptor, so we set FMODE_PREAD here.
778 stream_file
->f_mode
|= FMODE_PREAD
;
779 fd_install(stream_fd
, stream_file
);
781 * The stream holds a reference to the channel within the generic ring
782 * buffer library, so no need to hold a refcount on the channel and
783 * session files here.
788 put_unused_fd(stream_fd
);
794 int lttng_abi_open_stream(struct file
*channel_file
)
796 struct lttng_channel
*channel
= channel_file
->private_data
;
797 struct lib_ring_buffer
*buf
;
801 buf
= channel
->ops
->buffer_read_open(channel
->chan
);
806 ret
= lttng_abi_create_stream_fd(channel_file
, stream_priv
,
807 <tng_stream_ring_buffer_file_operations
);
814 channel
->ops
->buffer_read_close(buf
);
819 int lttng_abi_open_metadata_stream(struct file
*channel_file
)
821 struct lttng_channel
*channel
= channel_file
->private_data
;
822 struct lttng_session
*session
= channel
->session
;
823 struct lib_ring_buffer
*buf
;
825 struct lttng_metadata_stream
*metadata_stream
;
828 buf
= channel
->ops
->buffer_read_open(channel
->chan
);
832 metadata_stream
= kzalloc(sizeof(struct lttng_metadata_stream
),
834 if (!metadata_stream
) {
838 metadata_stream
->metadata_cache
= session
->metadata_cache
;
839 init_waitqueue_head(&metadata_stream
->read_wait
);
840 metadata_stream
->priv
= buf
;
841 stream_priv
= metadata_stream
;
842 metadata_stream
->transport
= channel
->transport
;
845 * Since life-time of metadata cache differs from that of
846 * session, we need to keep our own reference on the transport.
848 if (!try_module_get(metadata_stream
->transport
->owner
)) {
849 printk(KERN_WARNING
"LTT : Can't lock transport module.\n");
854 ret
= lttng_abi_create_stream_fd(channel_file
, stream_priv
,
855 <tng_metadata_ring_buffer_file_operations
);
859 kref_get(&session
->metadata_cache
->refcount
);
860 list_add(&metadata_stream
->list
,
861 &session
->metadata_cache
->metadata_stream
);
865 module_put(metadata_stream
->transport
->owner
);
867 kfree(metadata_stream
);
869 channel
->ops
->buffer_read_close(buf
);
874 int lttng_abi_create_event(struct file
*channel_file
,
875 struct lttng_kernel_event
*event_param
)
877 struct lttng_channel
*channel
= channel_file
->private_data
;
878 struct lttng_event
*event
;
880 struct file
*event_file
;
882 event_param
->name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
883 switch (event_param
->instrumentation
) {
884 case LTTNG_KERNEL_KRETPROBE
:
885 event_param
->u
.kretprobe
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
887 case LTTNG_KERNEL_KPROBE
:
888 event_param
->u
.kprobe
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
890 case LTTNG_KERNEL_FUNCTION
:
891 event_param
->u
.ftrace
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
896 switch (event_param
->instrumentation
) {
898 event_fd
= get_unused_fd();
903 event_file
= anon_inode_getfile("[lttng_event]",
906 if (IS_ERR(event_file
)) {
907 ret
= PTR_ERR(event_file
);
911 * We tolerate no failure path after event creation. It
912 * will stay invariant for the rest of the session.
914 event
= lttng_event_create(channel
, event_param
, NULL
, NULL
);
919 event_file
->private_data
= event
;
920 fd_install(event_fd
, event_file
);
921 /* The event holds a reference on the channel */
922 atomic_long_inc(&channel_file
->f_count
);
924 case LTTNG_KERNEL_SYSCALL
:
926 * Only all-syscall tracing supported for now.
928 if (event_param
->name
[0] != '\0')
930 ret
= lttng_syscalls_register(channel
, NULL
);
941 put_unused_fd(event_fd
);
947 * lttng_channel_ioctl - lttng syscall through ioctl
953 * This ioctl implements lttng commands:
954 * LTTNG_KERNEL_STREAM
955 * Returns an event stream file descriptor or failure.
956 * (typically, one event stream records events from one CPU)
958 * Returns an event file descriptor or failure.
959 * LTTNG_KERNEL_CONTEXT
960 * Prepend a context field to each event in the channel
961 * LTTNG_KERNEL_ENABLE
962 * Enable recording for events in this channel (weak enable)
963 * LTTNG_KERNEL_DISABLE
964 * Disable recording for events in this channel (strong disable)
966 * Channel and event file descriptors also hold a reference on the session.
969 long lttng_channel_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
971 struct lttng_channel
*channel
= file
->private_data
;
974 case LTTNG_KERNEL_OLD_STREAM
:
975 case LTTNG_KERNEL_STREAM
:
976 return lttng_abi_open_stream(file
);
977 case LTTNG_KERNEL_OLD_EVENT
:
979 struct lttng_kernel_event
*uevent_param
;
980 struct lttng_kernel_old_event
*old_uevent_param
;
983 uevent_param
= kmalloc(sizeof(struct lttng_kernel_event
),
989 old_uevent_param
= kmalloc(
990 sizeof(struct lttng_kernel_old_event
),
992 if (!old_uevent_param
) {
994 goto old_event_error_free_param
;
996 if (copy_from_user(old_uevent_param
,
997 (struct lttng_kernel_old_event __user
*) arg
,
998 sizeof(struct lttng_kernel_old_event
))) {
1000 goto old_event_error_free_old_param
;
1003 memcpy(uevent_param
->name
, old_uevent_param
->name
,
1004 sizeof(uevent_param
->name
));
1005 uevent_param
->instrumentation
=
1006 old_uevent_param
->instrumentation
;
1008 switch (old_uevent_param
->instrumentation
) {
1009 case LTTNG_KERNEL_KPROBE
:
1010 uevent_param
->u
.kprobe
.addr
=
1011 old_uevent_param
->u
.kprobe
.addr
;
1012 uevent_param
->u
.kprobe
.offset
=
1013 old_uevent_param
->u
.kprobe
.offset
;
1014 memcpy(uevent_param
->u
.kprobe
.symbol_name
,
1015 old_uevent_param
->u
.kprobe
.symbol_name
,
1016 sizeof(uevent_param
->u
.kprobe
.symbol_name
));
1018 case LTTNG_KERNEL_KRETPROBE
:
1019 uevent_param
->u
.kretprobe
.addr
=
1020 old_uevent_param
->u
.kretprobe
.addr
;
1021 uevent_param
->u
.kretprobe
.offset
=
1022 old_uevent_param
->u
.kretprobe
.offset
;
1023 memcpy(uevent_param
->u
.kretprobe
.symbol_name
,
1024 old_uevent_param
->u
.kretprobe
.symbol_name
,
1025 sizeof(uevent_param
->u
.kretprobe
.symbol_name
));
1027 case LTTNG_KERNEL_FUNCTION
:
1028 memcpy(uevent_param
->u
.ftrace
.symbol_name
,
1029 old_uevent_param
->u
.ftrace
.symbol_name
,
1030 sizeof(uevent_param
->u
.ftrace
.symbol_name
));
1035 ret
= lttng_abi_create_event(file
, uevent_param
);
1037 old_event_error_free_old_param
:
1038 kfree(old_uevent_param
);
1039 old_event_error_free_param
:
1040 kfree(uevent_param
);
1044 case LTTNG_KERNEL_EVENT
:
1046 struct lttng_kernel_event uevent_param
;
1048 if (copy_from_user(&uevent_param
,
1049 (struct lttng_kernel_event __user
*) arg
,
1050 sizeof(uevent_param
)))
1052 return lttng_abi_create_event(file
, &uevent_param
);
1054 case LTTNG_KERNEL_OLD_CONTEXT
:
1056 struct lttng_kernel_context
*ucontext_param
;
1057 struct lttng_kernel_old_context
*old_ucontext_param
;
1060 ucontext_param
= kmalloc(sizeof(struct lttng_kernel_context
),
1062 if (!ucontext_param
) {
1066 old_ucontext_param
= kmalloc(sizeof(struct lttng_kernel_old_context
),
1068 if (!old_ucontext_param
) {
1070 goto old_ctx_error_free_param
;
1073 if (copy_from_user(old_ucontext_param
,
1074 (struct lttng_kernel_old_context __user
*) arg
,
1075 sizeof(struct lttng_kernel_old_context
))) {
1077 goto old_ctx_error_free_old_param
;
1079 ucontext_param
->ctx
= old_ucontext_param
->ctx
;
1080 memcpy(ucontext_param
->padding
, old_ucontext_param
->padding
,
1081 sizeof(ucontext_param
->padding
));
1082 /* only type that uses the union */
1083 if (old_ucontext_param
->ctx
== LTTNG_KERNEL_CONTEXT_PERF_COUNTER
) {
1084 ucontext_param
->u
.perf_counter
.type
=
1085 old_ucontext_param
->u
.perf_counter
.type
;
1086 ucontext_param
->u
.perf_counter
.config
=
1087 old_ucontext_param
->u
.perf_counter
.config
;
1088 memcpy(ucontext_param
->u
.perf_counter
.name
,
1089 old_ucontext_param
->u
.perf_counter
.name
,
1090 sizeof(ucontext_param
->u
.perf_counter
.name
));
1093 ret
= lttng_abi_add_context(file
,
1095 &channel
->ctx
, channel
->session
);
1097 old_ctx_error_free_old_param
:
1098 kfree(old_ucontext_param
);
1099 old_ctx_error_free_param
:
1100 kfree(ucontext_param
);
1104 case LTTNG_KERNEL_CONTEXT
:
1106 struct lttng_kernel_context ucontext_param
;
1108 if (copy_from_user(&ucontext_param
,
1109 (struct lttng_kernel_context __user
*) arg
,
1110 sizeof(ucontext_param
)))
1112 return lttng_abi_add_context(file
,
1114 &channel
->ctx
, channel
->session
);
1116 case LTTNG_KERNEL_OLD_ENABLE
:
1117 case LTTNG_KERNEL_ENABLE
:
1118 return lttng_channel_enable(channel
);
1119 case LTTNG_KERNEL_OLD_DISABLE
:
1120 case LTTNG_KERNEL_DISABLE
:
1121 return lttng_channel_disable(channel
);
1123 return -ENOIOCTLCMD
;
1129 * lttng_metadata_ioctl - lttng syscall through ioctl
1135 * This ioctl implements lttng commands:
1136 * LTTNG_KERNEL_STREAM
1137 * Returns an event stream file descriptor or failure.
1139 * Channel and event file descriptors also hold a reference on the session.
1142 long lttng_metadata_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1145 case LTTNG_KERNEL_OLD_STREAM
:
1146 case LTTNG_KERNEL_STREAM
:
1147 return lttng_abi_open_metadata_stream(file
);
1149 return -ENOIOCTLCMD
;
1154 * lttng_channel_poll - lttng stream addition/removal monitoring
1159 unsigned int lttng_channel_poll(struct file
*file
, poll_table
*wait
)
1161 struct lttng_channel
*channel
= file
->private_data
;
1162 unsigned int mask
= 0;
1164 if (file
->f_mode
& FMODE_READ
) {
1165 poll_wait_set_exclusive(wait
);
1166 poll_wait(file
, channel
->ops
->get_hp_wait_queue(channel
->chan
),
1169 if (channel
->ops
->is_disabled(channel
->chan
))
1171 if (channel
->ops
->is_finalized(channel
->chan
))
1173 if (channel
->ops
->buffer_has_read_closed_stream(channel
->chan
))
1174 return POLLIN
| POLLRDNORM
;
1182 int lttng_channel_release(struct inode
*inode
, struct file
*file
)
1184 struct lttng_channel
*channel
= file
->private_data
;
1187 fput(channel
->session
->file
);
1192 int lttng_metadata_channel_release(struct inode
*inode
, struct file
*file
)
1194 struct lttng_channel
*channel
= file
->private_data
;
1197 lttng_metadata_channel_destroy(channel
);
1198 fput(channel
->session
->file
);
1204 static const struct file_operations lttng_channel_fops
= {
1205 .owner
= THIS_MODULE
,
1206 .release
= lttng_channel_release
,
1207 .poll
= lttng_channel_poll
,
1208 .unlocked_ioctl
= lttng_channel_ioctl
,
1209 #ifdef CONFIG_COMPAT
1210 .compat_ioctl
= lttng_channel_ioctl
,
1214 static const struct file_operations lttng_metadata_fops
= {
1215 .owner
= THIS_MODULE
,
1216 .release
= lttng_metadata_channel_release
,
1217 .unlocked_ioctl
= lttng_metadata_ioctl
,
1218 #ifdef CONFIG_COMPAT
1219 .compat_ioctl
= lttng_metadata_ioctl
,
1224 * lttng_event_ioctl - lttng syscall through ioctl
1230 * This ioctl implements lttng commands:
1231 * LTTNG_KERNEL_CONTEXT
1232 * Prepend a context field to each record of this event
1233 * LTTNG_KERNEL_ENABLE
1234 * Enable recording for this event (weak enable)
1235 * LTTNG_KERNEL_DISABLE
1236 * Disable recording for this event (strong disable)
1239 long lttng_event_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1241 struct lttng_event
*event
= file
->private_data
;
1244 case LTTNG_KERNEL_OLD_CONTEXT
:
1246 struct lttng_kernel_context
*ucontext_param
;
1247 struct lttng_kernel_old_context
*old_ucontext_param
;
1250 ucontext_param
= kmalloc(sizeof(struct lttng_kernel_context
),
1252 if (!ucontext_param
) {
1256 old_ucontext_param
= kmalloc(sizeof(struct lttng_kernel_old_context
),
1258 if (!old_ucontext_param
) {
1260 goto old_ctx_error_free_param
;
1263 if (copy_from_user(old_ucontext_param
,
1264 (struct lttng_kernel_old_context __user
*) arg
,
1265 sizeof(struct lttng_kernel_old_context
))) {
1267 goto old_ctx_error_free_old_param
;
1269 ucontext_param
->ctx
= old_ucontext_param
->ctx
;
1270 memcpy(ucontext_param
->padding
, old_ucontext_param
->padding
,
1271 sizeof(ucontext_param
->padding
));
1272 /* only type that uses the union */
1273 if (old_ucontext_param
->ctx
== LTTNG_KERNEL_CONTEXT_PERF_COUNTER
) {
1274 ucontext_param
->u
.perf_counter
.type
=
1275 old_ucontext_param
->u
.perf_counter
.type
;
1276 ucontext_param
->u
.perf_counter
.config
=
1277 old_ucontext_param
->u
.perf_counter
.config
;
1278 memcpy(ucontext_param
->u
.perf_counter
.name
,
1279 old_ucontext_param
->u
.perf_counter
.name
,
1280 sizeof(ucontext_param
->u
.perf_counter
.name
));
1283 ret
= lttng_abi_add_context(file
,
1285 &event
->ctx
, event
->chan
->session
);
1287 old_ctx_error_free_old_param
:
1288 kfree(old_ucontext_param
);
1289 old_ctx_error_free_param
:
1290 kfree(ucontext_param
);
1294 case LTTNG_KERNEL_CONTEXT
:
1296 struct lttng_kernel_context ucontext_param
;
1298 if (copy_from_user(&ucontext_param
,
1299 (struct lttng_kernel_context __user
*) arg
,
1300 sizeof(ucontext_param
)))
1302 return lttng_abi_add_context(file
,
1304 &event
->ctx
, event
->chan
->session
);
1306 case LTTNG_KERNEL_OLD_ENABLE
:
1307 case LTTNG_KERNEL_ENABLE
:
1308 return lttng_event_enable(event
);
1309 case LTTNG_KERNEL_OLD_DISABLE
:
1310 case LTTNG_KERNEL_DISABLE
:
1311 return lttng_event_disable(event
);
1313 return -ENOIOCTLCMD
;
1318 int lttng_event_release(struct inode
*inode
, struct file
*file
)
1320 struct lttng_event
*event
= file
->private_data
;
1323 fput(event
->chan
->file
);
1327 /* TODO: filter control ioctl */
1328 static const struct file_operations lttng_event_fops
= {
1329 .owner
= THIS_MODULE
,
1330 .release
= lttng_event_release
,
1331 .unlocked_ioctl
= lttng_event_ioctl
,
1332 #ifdef CONFIG_COMPAT
1333 .compat_ioctl
= lttng_event_ioctl
,
1337 static int put_u64(uint64_t val
, unsigned long arg
)
1339 return put_user(val
, (uint64_t __user
*) arg
);
1342 static long lttng_stream_ring_buffer_ioctl(struct file
*filp
,
1343 unsigned int cmd
, unsigned long arg
)
1345 struct lib_ring_buffer
*buf
= filp
->private_data
;
1346 struct channel
*chan
= buf
->backend
.chan
;
1347 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1348 struct lttng_channel
*lttng_chan
= channel_get_private(chan
);
1351 if (atomic_read(&chan
->record_disabled
))
1355 case LTTNG_RING_BUFFER_GET_TIMESTAMP_BEGIN
:
1359 if (!lttng_chan
->ops
)
1361 ret
= lttng_chan
->ops
->timestamp_begin(config
, buf
, &ts
);
1364 return put_u64(ts
, arg
);
1366 case LTTNG_RING_BUFFER_GET_TIMESTAMP_END
:
1370 if (!lttng_chan
->ops
)
1372 ret
= lttng_chan
->ops
->timestamp_end(config
, buf
, &ts
);
1375 return put_u64(ts
, arg
);
1377 case LTTNG_RING_BUFFER_GET_EVENTS_DISCARDED
:
1381 if (!lttng_chan
->ops
)
1383 ret
= lttng_chan
->ops
->events_discarded(config
, buf
, &ed
);
1386 return put_u64(ed
, arg
);
1388 case LTTNG_RING_BUFFER_GET_CONTENT_SIZE
:
1392 if (!lttng_chan
->ops
)
1394 ret
= lttng_chan
->ops
->content_size(config
, buf
, &cs
);
1397 return put_u64(cs
, arg
);
1399 case LTTNG_RING_BUFFER_GET_PACKET_SIZE
:
1403 if (!lttng_chan
->ops
)
1405 ret
= lttng_chan
->ops
->packet_size(config
, buf
, &ps
);
1408 return put_u64(ps
, arg
);
1410 case LTTNG_RING_BUFFER_GET_STREAM_ID
:
1414 if (!lttng_chan
->ops
)
1416 ret
= lttng_chan
->ops
->stream_id(config
, buf
, &si
);
1419 return put_u64(si
, arg
);
1421 case LTTNG_RING_BUFFER_GET_CURRENT_TIMESTAMP
:
1425 if (!lttng_chan
->ops
)
1427 ret
= lttng_chan
->ops
->current_timestamp(config
, buf
, &ts
);
1430 return put_u64(ts
, arg
);
1433 return lib_ring_buffer_file_operations
.unlocked_ioctl(filp
,
1441 #ifdef CONFIG_COMPAT
1442 static long lttng_stream_ring_buffer_compat_ioctl(struct file
*filp
,
1443 unsigned int cmd
, unsigned long arg
)
1445 struct lib_ring_buffer
*buf
= filp
->private_data
;
1446 struct channel
*chan
= buf
->backend
.chan
;
1447 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1448 struct lttng_channel
*lttng_chan
= channel_get_private(chan
);
1451 if (atomic_read(&chan
->record_disabled
))
1455 case LTTNG_RING_BUFFER_COMPAT_GET_TIMESTAMP_BEGIN
:
1459 if (!lttng_chan
->ops
)
1461 ret
= lttng_chan
->ops
->timestamp_begin(config
, buf
, &ts
);
1464 return put_u64(ts
, arg
);
1466 case LTTNG_RING_BUFFER_COMPAT_GET_TIMESTAMP_END
:
1470 if (!lttng_chan
->ops
)
1472 ret
= lttng_chan
->ops
->timestamp_end(config
, buf
, &ts
);
1475 return put_u64(ts
, arg
);
1477 case LTTNG_RING_BUFFER_COMPAT_GET_EVENTS_DISCARDED
:
1481 if (!lttng_chan
->ops
)
1483 ret
= lttng_chan
->ops
->events_discarded(config
, buf
, &ed
);
1486 return put_u64(ed
, arg
);
1488 case LTTNG_RING_BUFFER_COMPAT_GET_CONTENT_SIZE
:
1492 if (!lttng_chan
->ops
)
1494 ret
= lttng_chan
->ops
->content_size(config
, buf
, &cs
);
1497 return put_u64(cs
, arg
);
1499 case LTTNG_RING_BUFFER_COMPAT_GET_PACKET_SIZE
:
1503 if (!lttng_chan
->ops
)
1505 ret
= lttng_chan
->ops
->packet_size(config
, buf
, &ps
);
1508 return put_u64(ps
, arg
);
1510 case LTTNG_RING_BUFFER_COMPAT_GET_STREAM_ID
:
1514 if (!lttng_chan
->ops
)
1516 ret
= lttng_chan
->ops
->stream_id(config
, buf
, &si
);
1519 return put_u64(si
, arg
);
1521 case LTTNG_RING_BUFFER_GET_CURRENT_TIMESTAMP
:
1525 if (!lttng_chan
->ops
)
1527 ret
= lttng_chan
->ops
->current_timestamp(config
, buf
, &ts
);
1530 return put_u64(ts
, arg
);
1533 return lib_ring_buffer_file_operations
.compat_ioctl(filp
,
1540 #endif /* CONFIG_COMPAT */
1542 static void lttng_stream_override_ring_buffer_fops(void)
1544 lttng_stream_ring_buffer_file_operations
.owner
= THIS_MODULE
;
1545 lttng_stream_ring_buffer_file_operations
.open
=
1546 lib_ring_buffer_file_operations
.open
;
1547 lttng_stream_ring_buffer_file_operations
.release
=
1548 lib_ring_buffer_file_operations
.release
;
1549 lttng_stream_ring_buffer_file_operations
.poll
=
1550 lib_ring_buffer_file_operations
.poll
;
1551 lttng_stream_ring_buffer_file_operations
.splice_read
=
1552 lib_ring_buffer_file_operations
.splice_read
;
1553 lttng_stream_ring_buffer_file_operations
.mmap
=
1554 lib_ring_buffer_file_operations
.mmap
;
1555 lttng_stream_ring_buffer_file_operations
.unlocked_ioctl
=
1556 lttng_stream_ring_buffer_ioctl
;
1557 lttng_stream_ring_buffer_file_operations
.llseek
=
1558 lib_ring_buffer_file_operations
.llseek
;
1559 #ifdef CONFIG_COMPAT
1560 lttng_stream_ring_buffer_file_operations
.compat_ioctl
=
1561 lttng_stream_ring_buffer_compat_ioctl
;
1565 int __init
lttng_abi_init(void)
1569 wrapper_vmalloc_sync_all();
1570 lttng_proc_dentry
= proc_create_data("lttng", S_IRUSR
| S_IWUSR
, NULL
,
1573 if (!lttng_proc_dentry
) {
1574 printk(KERN_ERR
"Error creating LTTng control file\n");
1578 lttng_stream_override_ring_buffer_fops();
1584 void __exit
lttng_abi_exit(void)
1586 if (lttng_proc_dentry
)
1587 remove_proc_entry("lttng", NULL
);