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_tracepoint_list]",
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 void lttng_abi_tracer_abi_version(struct lttng_kernel_tracer_abi_version
*v
)
157 v
->major
= LTTNG_MODULES_ABI_MAJOR_VERSION
;
158 v
->minor
= LTTNG_MODULES_ABI_MINOR_VERSION
;
162 long lttng_abi_add_context(struct file
*file
,
163 struct lttng_kernel_context
*context_param
,
164 struct lttng_ctx
**ctx
, struct lttng_session
*session
)
167 if (session
->been_active
)
170 switch (context_param
->ctx
) {
171 case LTTNG_KERNEL_CONTEXT_PID
:
172 return lttng_add_pid_to_ctx(ctx
);
173 case LTTNG_KERNEL_CONTEXT_PRIO
:
174 return lttng_add_prio_to_ctx(ctx
);
175 case LTTNG_KERNEL_CONTEXT_NICE
:
176 return lttng_add_nice_to_ctx(ctx
);
177 case LTTNG_KERNEL_CONTEXT_VPID
:
178 return lttng_add_vpid_to_ctx(ctx
);
179 case LTTNG_KERNEL_CONTEXT_TID
:
180 return lttng_add_tid_to_ctx(ctx
);
181 case LTTNG_KERNEL_CONTEXT_VTID
:
182 return lttng_add_vtid_to_ctx(ctx
);
183 case LTTNG_KERNEL_CONTEXT_PPID
:
184 return lttng_add_ppid_to_ctx(ctx
);
185 case LTTNG_KERNEL_CONTEXT_VPPID
:
186 return lttng_add_vppid_to_ctx(ctx
);
187 case LTTNG_KERNEL_CONTEXT_PERF_COUNTER
:
188 context_param
->u
.perf_counter
.name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
189 return lttng_add_perf_counter_to_ctx(context_param
->u
.perf_counter
.type
,
190 context_param
->u
.perf_counter
.config
,
191 context_param
->u
.perf_counter
.name
,
193 case LTTNG_KERNEL_CONTEXT_PROCNAME
:
194 return lttng_add_procname_to_ctx(ctx
);
195 case LTTNG_KERNEL_CONTEXT_HOSTNAME
:
196 return lttng_add_hostname_to_ctx(ctx
);
203 * lttng_ioctl - lttng syscall through ioctl
209 * This ioctl implements lttng commands:
210 * LTTNG_KERNEL_SESSION
211 * Returns a LTTng trace session file descriptor
212 * LTTNG_KERNEL_TRACER_VERSION
213 * Returns the LTTng kernel tracer version
214 * LTTNG_KERNEL_TRACEPOINT_LIST
215 * Returns a file descriptor listing available tracepoints
216 * LTTNG_KERNEL_WAIT_QUIESCENT
217 * Returns after all previously running probes have completed
218 * LTTNG_KERNEL_TRACER_ABI_VERSION
219 * Returns the LTTng kernel tracer ABI version
221 * The returned session will be deleted when its file descriptor is closed.
224 long lttng_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
227 case LTTNG_KERNEL_OLD_SESSION
:
228 case LTTNG_KERNEL_SESSION
:
229 return lttng_abi_create_session();
230 case LTTNG_KERNEL_OLD_TRACER_VERSION
:
232 struct lttng_kernel_tracer_version v
;
233 struct lttng_kernel_old_tracer_version oldv
;
234 struct lttng_kernel_old_tracer_version
*uversion
=
235 (struct lttng_kernel_old_tracer_version __user
*) arg
;
237 lttng_abi_tracer_version(&v
);
238 oldv
.major
= v
.major
;
239 oldv
.minor
= v
.minor
;
240 oldv
.patchlevel
= v
.patchlevel
;
242 if (copy_to_user(uversion
, &oldv
, sizeof(oldv
)))
246 case LTTNG_KERNEL_TRACER_VERSION
:
248 struct lttng_kernel_tracer_version version
;
249 struct lttng_kernel_tracer_version
*uversion
=
250 (struct lttng_kernel_tracer_version __user
*) arg
;
252 lttng_abi_tracer_version(&version
);
254 if (copy_to_user(uversion
, &version
, sizeof(version
)))
258 case LTTNG_KERNEL_TRACER_ABI_VERSION
:
260 struct lttng_kernel_tracer_abi_version version
;
261 struct lttng_kernel_tracer_abi_version
*uversion
=
262 (struct lttng_kernel_tracer_abi_version __user
*) arg
;
264 lttng_abi_tracer_abi_version(&version
);
266 if (copy_to_user(uversion
, &version
, sizeof(version
)))
270 case LTTNG_KERNEL_OLD_TRACEPOINT_LIST
:
271 case LTTNG_KERNEL_TRACEPOINT_LIST
:
272 return lttng_abi_tracepoint_list();
273 case LTTNG_KERNEL_SYSCALL_LIST
:
274 return lttng_abi_syscall_list();
275 case LTTNG_KERNEL_OLD_WAIT_QUIESCENT
:
276 case LTTNG_KERNEL_WAIT_QUIESCENT
:
279 case LTTNG_KERNEL_OLD_CALIBRATE
:
281 struct lttng_kernel_old_calibrate __user
*ucalibrate
=
282 (struct lttng_kernel_old_calibrate __user
*) arg
;
283 struct lttng_kernel_old_calibrate old_calibrate
;
284 struct lttng_kernel_calibrate calibrate
;
287 if (copy_from_user(&old_calibrate
, ucalibrate
, sizeof(old_calibrate
)))
289 calibrate
.type
= old_calibrate
.type
;
290 ret
= lttng_calibrate(&calibrate
);
291 if (copy_to_user(ucalibrate
, &old_calibrate
, sizeof(old_calibrate
)))
295 case LTTNG_KERNEL_CALIBRATE
:
297 struct lttng_kernel_calibrate __user
*ucalibrate
=
298 (struct lttng_kernel_calibrate __user
*) arg
;
299 struct lttng_kernel_calibrate calibrate
;
302 if (copy_from_user(&calibrate
, ucalibrate
, sizeof(calibrate
)))
304 ret
= lttng_calibrate(&calibrate
);
305 if (copy_to_user(ucalibrate
, &calibrate
, sizeof(calibrate
)))
314 static const struct file_operations lttng_fops
= {
315 .owner
= THIS_MODULE
,
316 .unlocked_ioctl
= lttng_ioctl
,
318 .compat_ioctl
= lttng_ioctl
,
323 int lttng_abi_create_channel(struct file
*session_file
,
324 struct lttng_kernel_channel
*chan_param
,
325 enum channel_type channel_type
)
327 struct lttng_session
*session
= session_file
->private_data
;
328 const struct file_operations
*fops
= NULL
;
329 const char *transport_name
;
330 struct lttng_channel
*chan
;
331 struct file
*chan_file
;
335 chan_fd
= get_unused_fd();
340 switch (channel_type
) {
341 case PER_CPU_CHANNEL
:
342 fops
= <tng_channel_fops
;
344 case METADATA_CHANNEL
:
345 fops
= <tng_metadata_fops
;
349 chan_file
= anon_inode_getfile("[lttng_channel]",
352 if (IS_ERR(chan_file
)) {
353 ret
= PTR_ERR(chan_file
);
356 switch (channel_type
) {
357 case PER_CPU_CHANNEL
:
358 if (chan_param
->output
== LTTNG_KERNEL_SPLICE
) {
359 transport_name
= chan_param
->overwrite
?
360 "relay-overwrite" : "relay-discard";
361 } else if (chan_param
->output
== LTTNG_KERNEL_MMAP
) {
362 transport_name
= chan_param
->overwrite
?
363 "relay-overwrite-mmap" : "relay-discard-mmap";
368 case METADATA_CHANNEL
:
369 if (chan_param
->output
== LTTNG_KERNEL_SPLICE
)
370 transport_name
= "relay-metadata";
371 else if (chan_param
->output
== LTTNG_KERNEL_MMAP
)
372 transport_name
= "relay-metadata-mmap";
377 transport_name
= "<unknown>";
381 * We tolerate no failure path after channel creation. It will stay
382 * invariant for the rest of the session.
384 chan
= lttng_channel_create(session
, transport_name
, NULL
,
385 chan_param
->subbuf_size
,
386 chan_param
->num_subbuf
,
387 chan_param
->switch_timer_interval
,
388 chan_param
->read_timer_interval
,
394 chan
->file
= chan_file
;
395 chan_file
->private_data
= chan
;
396 fd_install(chan_fd
, chan_file
);
397 atomic_long_inc(&session_file
->f_count
);
404 put_unused_fd(chan_fd
);
410 * lttng_session_ioctl - lttng session fd ioctl
416 * This ioctl implements lttng commands:
417 * LTTNG_KERNEL_CHANNEL
418 * Returns a LTTng channel file descriptor
419 * LTTNG_KERNEL_ENABLE
420 * Enables tracing for a session (weak enable)
421 * LTTNG_KERNEL_DISABLE
422 * Disables tracing for a session (strong disable)
423 * LTTNG_KERNEL_METADATA
424 * Returns a LTTng metadata file descriptor
426 * The returned channel will be deleted when its file descriptor is closed.
429 long lttng_session_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
431 struct lttng_session
*session
= file
->private_data
;
434 case LTTNG_KERNEL_OLD_CHANNEL
:
436 struct lttng_kernel_channel chan_param
;
437 struct lttng_kernel_old_channel old_chan_param
;
439 if (copy_from_user(&old_chan_param
,
440 (struct lttng_kernel_old_channel __user
*) arg
,
441 sizeof(struct lttng_kernel_old_channel
)))
443 chan_param
.overwrite
= old_chan_param
.overwrite
;
444 chan_param
.subbuf_size
= old_chan_param
.subbuf_size
;
445 chan_param
.num_subbuf
= old_chan_param
.num_subbuf
;
446 chan_param
.switch_timer_interval
= old_chan_param
.switch_timer_interval
;
447 chan_param
.read_timer_interval
= old_chan_param
.read_timer_interval
;
448 chan_param
.output
= old_chan_param
.output
;
450 return lttng_abi_create_channel(file
, &chan_param
,
453 case LTTNG_KERNEL_CHANNEL
:
455 struct lttng_kernel_channel chan_param
;
457 if (copy_from_user(&chan_param
,
458 (struct lttng_kernel_channel __user
*) arg
,
459 sizeof(struct lttng_kernel_channel
)))
461 return lttng_abi_create_channel(file
, &chan_param
,
464 case LTTNG_KERNEL_OLD_SESSION_START
:
465 case LTTNG_KERNEL_OLD_ENABLE
:
466 case LTTNG_KERNEL_SESSION_START
:
467 case LTTNG_KERNEL_ENABLE
:
468 return lttng_session_enable(session
);
469 case LTTNG_KERNEL_OLD_SESSION_STOP
:
470 case LTTNG_KERNEL_OLD_DISABLE
:
471 case LTTNG_KERNEL_SESSION_STOP
:
472 case LTTNG_KERNEL_DISABLE
:
473 return lttng_session_disable(session
);
474 case LTTNG_KERNEL_OLD_METADATA
:
476 struct lttng_kernel_channel chan_param
;
477 struct lttng_kernel_old_channel old_chan_param
;
479 if (copy_from_user(&old_chan_param
,
480 (struct lttng_kernel_old_channel __user
*) arg
,
481 sizeof(struct lttng_kernel_old_channel
)))
483 chan_param
.overwrite
= old_chan_param
.overwrite
;
484 chan_param
.subbuf_size
= old_chan_param
.subbuf_size
;
485 chan_param
.num_subbuf
= old_chan_param
.num_subbuf
;
486 chan_param
.switch_timer_interval
= old_chan_param
.switch_timer_interval
;
487 chan_param
.read_timer_interval
= old_chan_param
.read_timer_interval
;
488 chan_param
.output
= old_chan_param
.output
;
490 return lttng_abi_create_channel(file
, &chan_param
,
493 case LTTNG_KERNEL_METADATA
:
495 struct lttng_kernel_channel chan_param
;
497 if (copy_from_user(&chan_param
,
498 (struct lttng_kernel_channel __user
*) arg
,
499 sizeof(struct lttng_kernel_channel
)))
501 return lttng_abi_create_channel(file
, &chan_param
,
510 * Called when the last file reference is dropped.
512 * Big fat note: channels and events are invariant for the whole session after
513 * their creation. So this session destruction also destroys all channel and
514 * event structures specific to this session (they are not destroyed when their
515 * individual file is released).
518 int lttng_session_release(struct inode
*inode
, struct file
*file
)
520 struct lttng_session
*session
= file
->private_data
;
523 lttng_session_destroy(session
);
527 static const struct file_operations lttng_session_fops
= {
528 .owner
= THIS_MODULE
,
529 .release
= lttng_session_release
,
530 .unlocked_ioctl
= lttng_session_ioctl
,
532 .compat_ioctl
= lttng_session_ioctl
,
537 * lttng_metadata_ring_buffer_poll - LTTng ring buffer poll file operation
541 * Handles the poll operations for the metadata channels.
544 unsigned int lttng_metadata_ring_buffer_poll(struct file
*filp
,
547 struct lttng_metadata_stream
*stream
= filp
->private_data
;
548 struct lib_ring_buffer
*buf
= stream
->priv
;
550 unsigned int mask
= 0;
552 if (filp
->f_mode
& FMODE_READ
) {
553 poll_wait_set_exclusive(wait
);
554 poll_wait(filp
, &stream
->read_wait
, wait
);
556 finalized
= stream
->finalized
;
559 * lib_ring_buffer_is_finalized() contains a smp_rmb()
560 * ordering finalized load before offsets loads.
562 WARN_ON(atomic_long_read(&buf
->active_readers
) != 1);
567 if (stream
->metadata_cache
->metadata_written
>
568 stream
->metadata_out
)
576 void lttng_metadata_ring_buffer_ioctl_put_next_subbuf(struct file
*filp
,
577 unsigned int cmd
, unsigned long arg
)
579 struct lttng_metadata_stream
*stream
= filp
->private_data
;
581 stream
->metadata_out
= stream
->metadata_in
;
585 long lttng_metadata_ring_buffer_ioctl(struct file
*filp
,
586 unsigned int cmd
, unsigned long arg
)
589 struct lttng_metadata_stream
*stream
= filp
->private_data
;
590 struct lib_ring_buffer
*buf
= stream
->priv
;
593 case RING_BUFFER_GET_NEXT_SUBBUF
:
595 struct lttng_metadata_stream
*stream
= filp
->private_data
;
596 struct lib_ring_buffer
*buf
= stream
->priv
;
597 struct channel
*chan
= buf
->backend
.chan
;
599 ret
= lttng_metadata_output_channel(stream
, chan
);
601 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
607 case RING_BUFFER_GET_SUBBUF
:
610 * Random access is not allowed for metadata channel.
614 case RING_BUFFER_FLUSH
:
616 struct lttng_metadata_stream
*stream
= filp
->private_data
;
617 struct lib_ring_buffer
*buf
= stream
->priv
;
618 struct channel
*chan
= buf
->backend
.chan
;
621 * Before doing the actual ring buffer flush, write up to one
622 * packet of metadata in the ring buffer.
624 ret
= lttng_metadata_output_channel(stream
, chan
);
632 /* PUT_SUBBUF is the one from lib ring buffer, unmodified. */
634 /* Performing lib ring buffer ioctl after our own. */
635 ret
= lib_ring_buffer_ioctl(filp
, cmd
, arg
, buf
);
640 case RING_BUFFER_PUT_NEXT_SUBBUF
:
642 lttng_metadata_ring_buffer_ioctl_put_next_subbuf(filp
,
655 long lttng_metadata_ring_buffer_compat_ioctl(struct file
*filp
,
656 unsigned int cmd
, unsigned long arg
)
659 struct lttng_metadata_stream
*stream
= filp
->private_data
;
660 struct lib_ring_buffer
*buf
= stream
->priv
;
663 case RING_BUFFER_GET_NEXT_SUBBUF
:
665 struct lttng_metadata_stream
*stream
= filp
->private_data
;
666 struct lib_ring_buffer
*buf
= stream
->priv
;
667 struct channel
*chan
= buf
->backend
.chan
;
669 ret
= lttng_metadata_output_channel(stream
, chan
);
671 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
677 case RING_BUFFER_GET_SUBBUF
:
680 * Random access is not allowed for metadata channel.
687 /* PUT_SUBBUF is the one from lib ring buffer, unmodified. */
689 /* Performing lib ring buffer ioctl after our own. */
690 ret
= lib_ring_buffer_compat_ioctl(filp
, cmd
, arg
, buf
);
695 case RING_BUFFER_PUT_NEXT_SUBBUF
:
697 lttng_metadata_ring_buffer_ioctl_put_next_subbuf(filp
,
710 * This is not used by anonymous file descriptors. This code is left
711 * there if we ever want to implement an inode with open() operation.
714 int lttng_metadata_ring_buffer_open(struct inode
*inode
, struct file
*file
)
716 struct lttng_metadata_stream
*stream
= inode
->i_private
;
717 struct lib_ring_buffer
*buf
= stream
->priv
;
719 file
->private_data
= buf
;
721 * Since life-time of metadata cache differs from that of
722 * session, we need to keep our own reference on the transport.
724 if (!try_module_get(stream
->transport
->owner
)) {
725 printk(KERN_WARNING
"LTT : Can't lock transport module.\n");
728 return lib_ring_buffer_open(inode
, file
, buf
);
732 int lttng_metadata_ring_buffer_release(struct inode
*inode
, struct file
*file
)
734 struct lttng_metadata_stream
*stream
= file
->private_data
;
735 struct lib_ring_buffer
*buf
= stream
->priv
;
737 kref_put(&stream
->metadata_cache
->refcount
, metadata_cache_destroy
);
738 module_put(stream
->transport
->owner
);
739 return lib_ring_buffer_release(inode
, file
, buf
);
743 ssize_t
lttng_metadata_ring_buffer_splice_read(struct file
*in
, loff_t
*ppos
,
744 struct pipe_inode_info
*pipe
, size_t len
,
747 struct lttng_metadata_stream
*stream
= in
->private_data
;
748 struct lib_ring_buffer
*buf
= stream
->priv
;
750 return lib_ring_buffer_splice_read(in
, ppos
, pipe
, len
,
755 int lttng_metadata_ring_buffer_mmap(struct file
*filp
,
756 struct vm_area_struct
*vma
)
758 struct lttng_metadata_stream
*stream
= filp
->private_data
;
759 struct lib_ring_buffer
*buf
= stream
->priv
;
761 return lib_ring_buffer_mmap(filp
, vma
, buf
);
765 const struct file_operations lttng_metadata_ring_buffer_file_operations
= {
766 .owner
= THIS_MODULE
,
767 .open
= lttng_metadata_ring_buffer_open
,
768 .release
= lttng_metadata_ring_buffer_release
,
769 .poll
= lttng_metadata_ring_buffer_poll
,
770 .splice_read
= lttng_metadata_ring_buffer_splice_read
,
771 .mmap
= lttng_metadata_ring_buffer_mmap
,
772 .unlocked_ioctl
= lttng_metadata_ring_buffer_ioctl
,
773 .llseek
= vfs_lib_ring_buffer_no_llseek
,
775 .compat_ioctl
= lttng_metadata_ring_buffer_compat_ioctl
,
780 int lttng_abi_create_stream_fd(struct file
*channel_file
, void *stream_priv
,
781 const struct file_operations
*fops
)
784 struct file
*stream_file
;
786 stream_fd
= get_unused_fd();
791 stream_file
= anon_inode_getfile("[lttng_stream]", fops
,
792 stream_priv
, O_RDWR
);
793 if (IS_ERR(stream_file
)) {
794 ret
= PTR_ERR(stream_file
);
798 * OPEN_FMODE, called within anon_inode_getfile/alloc_file, don't honor
799 * FMODE_LSEEK, FMODE_PREAD nor FMODE_PWRITE. We need to read from this
800 * file descriptor, so we set FMODE_PREAD here.
802 stream_file
->f_mode
|= FMODE_PREAD
;
803 fd_install(stream_fd
, stream_file
);
805 * The stream holds a reference to the channel within the generic ring
806 * buffer library, so no need to hold a refcount on the channel and
807 * session files here.
812 put_unused_fd(stream_fd
);
818 int lttng_abi_open_stream(struct file
*channel_file
)
820 struct lttng_channel
*channel
= channel_file
->private_data
;
821 struct lib_ring_buffer
*buf
;
825 buf
= channel
->ops
->buffer_read_open(channel
->chan
);
830 ret
= lttng_abi_create_stream_fd(channel_file
, stream_priv
,
831 <tng_stream_ring_buffer_file_operations
);
838 channel
->ops
->buffer_read_close(buf
);
843 int lttng_abi_open_metadata_stream(struct file
*channel_file
)
845 struct lttng_channel
*channel
= channel_file
->private_data
;
846 struct lttng_session
*session
= channel
->session
;
847 struct lib_ring_buffer
*buf
;
849 struct lttng_metadata_stream
*metadata_stream
;
852 buf
= channel
->ops
->buffer_read_open(channel
->chan
);
856 metadata_stream
= kzalloc(sizeof(struct lttng_metadata_stream
),
858 if (!metadata_stream
) {
862 metadata_stream
->metadata_cache
= session
->metadata_cache
;
863 init_waitqueue_head(&metadata_stream
->read_wait
);
864 metadata_stream
->priv
= buf
;
865 stream_priv
= metadata_stream
;
866 metadata_stream
->transport
= channel
->transport
;
867 mutex_init(&metadata_stream
->lock
);
870 * Since life-time of metadata cache differs from that of
871 * session, we need to keep our own reference on the transport.
873 if (!try_module_get(metadata_stream
->transport
->owner
)) {
874 printk(KERN_WARNING
"LTT : Can't lock transport module.\n");
879 ret
= lttng_abi_create_stream_fd(channel_file
, stream_priv
,
880 <tng_metadata_ring_buffer_file_operations
);
884 kref_get(&session
->metadata_cache
->refcount
);
885 list_add(&metadata_stream
->list
,
886 &session
->metadata_cache
->metadata_stream
);
890 module_put(metadata_stream
->transport
->owner
);
892 kfree(metadata_stream
);
894 channel
->ops
->buffer_read_close(buf
);
899 int lttng_abi_create_event(struct file
*channel_file
,
900 struct lttng_kernel_event
*event_param
)
902 struct lttng_channel
*channel
= channel_file
->private_data
;
903 struct lttng_event
*event
;
905 struct file
*event_file
;
907 event_param
->name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
908 switch (event_param
->instrumentation
) {
909 case LTTNG_KERNEL_KRETPROBE
:
910 event_param
->u
.kretprobe
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
912 case LTTNG_KERNEL_KPROBE
:
913 event_param
->u
.kprobe
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
915 case LTTNG_KERNEL_FUNCTION
:
916 event_param
->u
.ftrace
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
921 switch (event_param
->instrumentation
) {
923 event_fd
= get_unused_fd();
928 event_file
= anon_inode_getfile("[lttng_event]",
931 if (IS_ERR(event_file
)) {
932 ret
= PTR_ERR(event_file
);
936 * We tolerate no failure path after event creation. It
937 * will stay invariant for the rest of the session.
939 event
= lttng_event_create(channel
, event_param
, NULL
, NULL
);
940 WARN_ON_ONCE(!event
);
942 ret
= PTR_ERR(event
);
945 event_file
->private_data
= event
;
946 fd_install(event_fd
, event_file
);
947 /* The event holds a reference on the channel */
948 atomic_long_inc(&channel_file
->f_count
);
950 case LTTNG_KERNEL_SYSCALL
:
951 ret
= lttng_syscalls_register(channel
, NULL
);
955 if (event_param
->u
.syscall
.enable
) {
956 ret
= lttng_syscall_filter_enable(channel
,
957 event_param
->name
[0] == '\0' ?
958 NULL
: event_param
->name
);
963 ret
= lttng_syscall_filter_disable(channel
,
964 event_param
->name
[0] == '\0' ?
965 NULL
: event_param
->name
);
976 put_unused_fd(event_fd
);
982 * lttng_channel_ioctl - lttng syscall through ioctl
988 * This ioctl implements lttng commands:
989 * LTTNG_KERNEL_STREAM
990 * Returns an event stream file descriptor or failure.
991 * (typically, one event stream records events from one CPU)
993 * Returns an event file descriptor or failure.
994 * LTTNG_KERNEL_CONTEXT
995 * Prepend a context field to each event in the channel
996 * LTTNG_KERNEL_ENABLE
997 * Enable recording for events in this channel (weak enable)
998 * LTTNG_KERNEL_DISABLE
999 * Disable recording for events in this channel (strong disable)
1001 * Channel and event file descriptors also hold a reference on the session.
1004 long lttng_channel_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1006 struct lttng_channel
*channel
= file
->private_data
;
1009 case LTTNG_KERNEL_OLD_STREAM
:
1010 case LTTNG_KERNEL_STREAM
:
1011 return lttng_abi_open_stream(file
);
1012 case LTTNG_KERNEL_OLD_EVENT
:
1014 struct lttng_kernel_event
*uevent_param
;
1015 struct lttng_kernel_old_event
*old_uevent_param
;
1018 uevent_param
= kmalloc(sizeof(struct lttng_kernel_event
),
1020 if (!uevent_param
) {
1024 old_uevent_param
= kmalloc(
1025 sizeof(struct lttng_kernel_old_event
),
1027 if (!old_uevent_param
) {
1029 goto old_event_error_free_param
;
1031 if (copy_from_user(old_uevent_param
,
1032 (struct lttng_kernel_old_event __user
*) arg
,
1033 sizeof(struct lttng_kernel_old_event
))) {
1035 goto old_event_error_free_old_param
;
1038 memcpy(uevent_param
->name
, old_uevent_param
->name
,
1039 sizeof(uevent_param
->name
));
1040 uevent_param
->instrumentation
=
1041 old_uevent_param
->instrumentation
;
1043 switch (old_uevent_param
->instrumentation
) {
1044 case LTTNG_KERNEL_KPROBE
:
1045 uevent_param
->u
.kprobe
.addr
=
1046 old_uevent_param
->u
.kprobe
.addr
;
1047 uevent_param
->u
.kprobe
.offset
=
1048 old_uevent_param
->u
.kprobe
.offset
;
1049 memcpy(uevent_param
->u
.kprobe
.symbol_name
,
1050 old_uevent_param
->u
.kprobe
.symbol_name
,
1051 sizeof(uevent_param
->u
.kprobe
.symbol_name
));
1053 case LTTNG_KERNEL_KRETPROBE
:
1054 uevent_param
->u
.kretprobe
.addr
=
1055 old_uevent_param
->u
.kretprobe
.addr
;
1056 uevent_param
->u
.kretprobe
.offset
=
1057 old_uevent_param
->u
.kretprobe
.offset
;
1058 memcpy(uevent_param
->u
.kretprobe
.symbol_name
,
1059 old_uevent_param
->u
.kretprobe
.symbol_name
,
1060 sizeof(uevent_param
->u
.kretprobe
.symbol_name
));
1062 case LTTNG_KERNEL_FUNCTION
:
1063 memcpy(uevent_param
->u
.ftrace
.symbol_name
,
1064 old_uevent_param
->u
.ftrace
.symbol_name
,
1065 sizeof(uevent_param
->u
.ftrace
.symbol_name
));
1070 ret
= lttng_abi_create_event(file
, uevent_param
);
1072 old_event_error_free_old_param
:
1073 kfree(old_uevent_param
);
1074 old_event_error_free_param
:
1075 kfree(uevent_param
);
1079 case LTTNG_KERNEL_EVENT
:
1081 struct lttng_kernel_event uevent_param
;
1083 if (copy_from_user(&uevent_param
,
1084 (struct lttng_kernel_event __user
*) arg
,
1085 sizeof(uevent_param
)))
1087 return lttng_abi_create_event(file
, &uevent_param
);
1089 case LTTNG_KERNEL_OLD_CONTEXT
:
1091 struct lttng_kernel_context
*ucontext_param
;
1092 struct lttng_kernel_old_context
*old_ucontext_param
;
1095 ucontext_param
= kmalloc(sizeof(struct lttng_kernel_context
),
1097 if (!ucontext_param
) {
1101 old_ucontext_param
= kmalloc(sizeof(struct lttng_kernel_old_context
),
1103 if (!old_ucontext_param
) {
1105 goto old_ctx_error_free_param
;
1108 if (copy_from_user(old_ucontext_param
,
1109 (struct lttng_kernel_old_context __user
*) arg
,
1110 sizeof(struct lttng_kernel_old_context
))) {
1112 goto old_ctx_error_free_old_param
;
1114 ucontext_param
->ctx
= old_ucontext_param
->ctx
;
1115 memcpy(ucontext_param
->padding
, old_ucontext_param
->padding
,
1116 sizeof(ucontext_param
->padding
));
1117 /* only type that uses the union */
1118 if (old_ucontext_param
->ctx
== LTTNG_KERNEL_CONTEXT_PERF_COUNTER
) {
1119 ucontext_param
->u
.perf_counter
.type
=
1120 old_ucontext_param
->u
.perf_counter
.type
;
1121 ucontext_param
->u
.perf_counter
.config
=
1122 old_ucontext_param
->u
.perf_counter
.config
;
1123 memcpy(ucontext_param
->u
.perf_counter
.name
,
1124 old_ucontext_param
->u
.perf_counter
.name
,
1125 sizeof(ucontext_param
->u
.perf_counter
.name
));
1128 ret
= lttng_abi_add_context(file
,
1130 &channel
->ctx
, channel
->session
);
1132 old_ctx_error_free_old_param
:
1133 kfree(old_ucontext_param
);
1134 old_ctx_error_free_param
:
1135 kfree(ucontext_param
);
1139 case LTTNG_KERNEL_CONTEXT
:
1141 struct lttng_kernel_context ucontext_param
;
1143 if (copy_from_user(&ucontext_param
,
1144 (struct lttng_kernel_context __user
*) arg
,
1145 sizeof(ucontext_param
)))
1147 return lttng_abi_add_context(file
,
1149 &channel
->ctx
, channel
->session
);
1151 case LTTNG_KERNEL_OLD_ENABLE
:
1152 case LTTNG_KERNEL_ENABLE
:
1153 return lttng_channel_enable(channel
);
1154 case LTTNG_KERNEL_OLD_DISABLE
:
1155 case LTTNG_KERNEL_DISABLE
:
1156 return lttng_channel_disable(channel
);
1157 case LTTNG_KERNEL_SYSCALL_MASK
:
1158 return lttng_channel_syscall_mask(channel
,
1159 (struct lttng_kernel_syscall_mask __user
*) arg
);
1161 return -ENOIOCTLCMD
;
1167 * lttng_metadata_ioctl - lttng syscall through ioctl
1173 * This ioctl implements lttng commands:
1174 * LTTNG_KERNEL_STREAM
1175 * Returns an event stream file descriptor or failure.
1177 * Channel and event file descriptors also hold a reference on the session.
1180 long lttng_metadata_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1183 case LTTNG_KERNEL_OLD_STREAM
:
1184 case LTTNG_KERNEL_STREAM
:
1185 return lttng_abi_open_metadata_stream(file
);
1187 return -ENOIOCTLCMD
;
1192 * lttng_channel_poll - lttng stream addition/removal monitoring
1197 unsigned int lttng_channel_poll(struct file
*file
, poll_table
*wait
)
1199 struct lttng_channel
*channel
= file
->private_data
;
1200 unsigned int mask
= 0;
1202 if (file
->f_mode
& FMODE_READ
) {
1203 poll_wait_set_exclusive(wait
);
1204 poll_wait(file
, channel
->ops
->get_hp_wait_queue(channel
->chan
),
1207 if (channel
->ops
->is_disabled(channel
->chan
))
1209 if (channel
->ops
->is_finalized(channel
->chan
))
1211 if (channel
->ops
->buffer_has_read_closed_stream(channel
->chan
))
1212 return POLLIN
| POLLRDNORM
;
1220 int lttng_channel_release(struct inode
*inode
, struct file
*file
)
1222 struct lttng_channel
*channel
= file
->private_data
;
1225 fput(channel
->session
->file
);
1230 int lttng_metadata_channel_release(struct inode
*inode
, struct file
*file
)
1232 struct lttng_channel
*channel
= file
->private_data
;
1235 lttng_metadata_channel_destroy(channel
);
1236 fput(channel
->session
->file
);
1242 static const struct file_operations lttng_channel_fops
= {
1243 .owner
= THIS_MODULE
,
1244 .release
= lttng_channel_release
,
1245 .poll
= lttng_channel_poll
,
1246 .unlocked_ioctl
= lttng_channel_ioctl
,
1247 #ifdef CONFIG_COMPAT
1248 .compat_ioctl
= lttng_channel_ioctl
,
1252 static const struct file_operations lttng_metadata_fops
= {
1253 .owner
= THIS_MODULE
,
1254 .release
= lttng_metadata_channel_release
,
1255 .unlocked_ioctl
= lttng_metadata_ioctl
,
1256 #ifdef CONFIG_COMPAT
1257 .compat_ioctl
= lttng_metadata_ioctl
,
1262 * lttng_event_ioctl - lttng syscall through ioctl
1268 * This ioctl implements lttng commands:
1269 * LTTNG_KERNEL_CONTEXT
1270 * Prepend a context field to each record of this event
1271 * LTTNG_KERNEL_ENABLE
1272 * Enable recording for this event (weak enable)
1273 * LTTNG_KERNEL_DISABLE
1274 * Disable recording for this event (strong disable)
1277 long lttng_event_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1279 struct lttng_event
*event
= file
->private_data
;
1282 case LTTNG_KERNEL_OLD_CONTEXT
:
1284 struct lttng_kernel_context
*ucontext_param
;
1285 struct lttng_kernel_old_context
*old_ucontext_param
;
1288 ucontext_param
= kmalloc(sizeof(struct lttng_kernel_context
),
1290 if (!ucontext_param
) {
1294 old_ucontext_param
= kmalloc(sizeof(struct lttng_kernel_old_context
),
1296 if (!old_ucontext_param
) {
1298 goto old_ctx_error_free_param
;
1301 if (copy_from_user(old_ucontext_param
,
1302 (struct lttng_kernel_old_context __user
*) arg
,
1303 sizeof(struct lttng_kernel_old_context
))) {
1305 goto old_ctx_error_free_old_param
;
1307 ucontext_param
->ctx
= old_ucontext_param
->ctx
;
1308 memcpy(ucontext_param
->padding
, old_ucontext_param
->padding
,
1309 sizeof(ucontext_param
->padding
));
1310 /* only type that uses the union */
1311 if (old_ucontext_param
->ctx
== LTTNG_KERNEL_CONTEXT_PERF_COUNTER
) {
1312 ucontext_param
->u
.perf_counter
.type
=
1313 old_ucontext_param
->u
.perf_counter
.type
;
1314 ucontext_param
->u
.perf_counter
.config
=
1315 old_ucontext_param
->u
.perf_counter
.config
;
1316 memcpy(ucontext_param
->u
.perf_counter
.name
,
1317 old_ucontext_param
->u
.perf_counter
.name
,
1318 sizeof(ucontext_param
->u
.perf_counter
.name
));
1321 ret
= lttng_abi_add_context(file
,
1323 &event
->ctx
, event
->chan
->session
);
1325 old_ctx_error_free_old_param
:
1326 kfree(old_ucontext_param
);
1327 old_ctx_error_free_param
:
1328 kfree(ucontext_param
);
1332 case LTTNG_KERNEL_CONTEXT
:
1334 struct lttng_kernel_context ucontext_param
;
1336 if (copy_from_user(&ucontext_param
,
1337 (struct lttng_kernel_context __user
*) arg
,
1338 sizeof(ucontext_param
)))
1340 return lttng_abi_add_context(file
,
1342 &event
->ctx
, event
->chan
->session
);
1344 case LTTNG_KERNEL_OLD_ENABLE
:
1345 case LTTNG_KERNEL_ENABLE
:
1346 return lttng_event_enable(event
);
1347 case LTTNG_KERNEL_OLD_DISABLE
:
1348 case LTTNG_KERNEL_DISABLE
:
1349 return lttng_event_disable(event
);
1351 return -ENOIOCTLCMD
;
1356 int lttng_event_release(struct inode
*inode
, struct file
*file
)
1358 struct lttng_event
*event
= file
->private_data
;
1361 fput(event
->chan
->file
);
1365 /* TODO: filter control ioctl */
1366 static const struct file_operations lttng_event_fops
= {
1367 .owner
= THIS_MODULE
,
1368 .release
= lttng_event_release
,
1369 .unlocked_ioctl
= lttng_event_ioctl
,
1370 #ifdef CONFIG_COMPAT
1371 .compat_ioctl
= lttng_event_ioctl
,
1375 static int put_u64(uint64_t val
, unsigned long arg
)
1377 return put_user(val
, (uint64_t __user
*) arg
);
1380 static long lttng_stream_ring_buffer_ioctl(struct file
*filp
,
1381 unsigned int cmd
, unsigned long arg
)
1383 struct lib_ring_buffer
*buf
= filp
->private_data
;
1384 struct channel
*chan
= buf
->backend
.chan
;
1385 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1386 const struct lttng_channel_ops
*ops
= chan
->backend
.priv_ops
;
1389 if (atomic_read(&chan
->record_disabled
))
1393 case LTTNG_RING_BUFFER_GET_TIMESTAMP_BEGIN
:
1397 ret
= ops
->timestamp_begin(config
, buf
, &ts
);
1400 return put_u64(ts
, arg
);
1402 case LTTNG_RING_BUFFER_GET_TIMESTAMP_END
:
1406 ret
= ops
->timestamp_end(config
, buf
, &ts
);
1409 return put_u64(ts
, arg
);
1411 case LTTNG_RING_BUFFER_GET_EVENTS_DISCARDED
:
1415 ret
= ops
->events_discarded(config
, buf
, &ed
);
1418 return put_u64(ed
, arg
);
1420 case LTTNG_RING_BUFFER_GET_CONTENT_SIZE
:
1424 ret
= ops
->content_size(config
, buf
, &cs
);
1427 return put_u64(cs
, arg
);
1429 case LTTNG_RING_BUFFER_GET_PACKET_SIZE
:
1433 ret
= ops
->packet_size(config
, buf
, &ps
);
1436 return put_u64(ps
, arg
);
1438 case LTTNG_RING_BUFFER_GET_STREAM_ID
:
1442 ret
= ops
->stream_id(config
, buf
, &si
);
1445 return put_u64(si
, arg
);
1447 case LTTNG_RING_BUFFER_GET_CURRENT_TIMESTAMP
:
1451 ret
= ops
->current_timestamp(config
, buf
, &ts
);
1454 return put_u64(ts
, arg
);
1457 return lib_ring_buffer_file_operations
.unlocked_ioctl(filp
,
1465 #ifdef CONFIG_COMPAT
1466 static long lttng_stream_ring_buffer_compat_ioctl(struct file
*filp
,
1467 unsigned int cmd
, unsigned long arg
)
1469 struct lib_ring_buffer
*buf
= filp
->private_data
;
1470 struct channel
*chan
= buf
->backend
.chan
;
1471 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1472 const struct lttng_channel_ops
*ops
= chan
->backend
.priv_ops
;
1475 if (atomic_read(&chan
->record_disabled
))
1479 case LTTNG_RING_BUFFER_COMPAT_GET_TIMESTAMP_BEGIN
:
1483 ret
= ops
->timestamp_begin(config
, buf
, &ts
);
1486 return put_u64(ts
, arg
);
1488 case LTTNG_RING_BUFFER_COMPAT_GET_TIMESTAMP_END
:
1492 ret
= ops
->timestamp_end(config
, buf
, &ts
);
1495 return put_u64(ts
, arg
);
1497 case LTTNG_RING_BUFFER_COMPAT_GET_EVENTS_DISCARDED
:
1501 ret
= ops
->events_discarded(config
, buf
, &ed
);
1504 return put_u64(ed
, arg
);
1506 case LTTNG_RING_BUFFER_COMPAT_GET_CONTENT_SIZE
:
1510 ret
= ops
->content_size(config
, buf
, &cs
);
1513 return put_u64(cs
, arg
);
1515 case LTTNG_RING_BUFFER_COMPAT_GET_PACKET_SIZE
:
1519 ret
= ops
->packet_size(config
, buf
, &ps
);
1522 return put_u64(ps
, arg
);
1524 case LTTNG_RING_BUFFER_COMPAT_GET_STREAM_ID
:
1528 ret
= ops
->stream_id(config
, buf
, &si
);
1531 return put_u64(si
, arg
);
1533 case LTTNG_RING_BUFFER_GET_CURRENT_TIMESTAMP
:
1537 ret
= ops
->current_timestamp(config
, buf
, &ts
);
1540 return put_u64(ts
, arg
);
1543 return lib_ring_buffer_file_operations
.compat_ioctl(filp
,
1550 #endif /* CONFIG_COMPAT */
1552 static void lttng_stream_override_ring_buffer_fops(void)
1554 lttng_stream_ring_buffer_file_operations
.owner
= THIS_MODULE
;
1555 lttng_stream_ring_buffer_file_operations
.open
=
1556 lib_ring_buffer_file_operations
.open
;
1557 lttng_stream_ring_buffer_file_operations
.release
=
1558 lib_ring_buffer_file_operations
.release
;
1559 lttng_stream_ring_buffer_file_operations
.poll
=
1560 lib_ring_buffer_file_operations
.poll
;
1561 lttng_stream_ring_buffer_file_operations
.splice_read
=
1562 lib_ring_buffer_file_operations
.splice_read
;
1563 lttng_stream_ring_buffer_file_operations
.mmap
=
1564 lib_ring_buffer_file_operations
.mmap
;
1565 lttng_stream_ring_buffer_file_operations
.unlocked_ioctl
=
1566 lttng_stream_ring_buffer_ioctl
;
1567 lttng_stream_ring_buffer_file_operations
.llseek
=
1568 lib_ring_buffer_file_operations
.llseek
;
1569 #ifdef CONFIG_COMPAT
1570 lttng_stream_ring_buffer_file_operations
.compat_ioctl
=
1571 lttng_stream_ring_buffer_compat_ioctl
;
1575 int __init
lttng_abi_init(void)
1579 wrapper_vmalloc_sync_all();
1580 lttng_proc_dentry
= proc_create_data("lttng", S_IRUSR
| S_IWUSR
, NULL
,
1583 if (!lttng_proc_dentry
) {
1584 printk(KERN_ERR
"Error creating LTTng control file\n");
1588 lttng_stream_override_ring_buffer_fops();
1594 /* No __exit annotation because used by init error path too. */
1595 void lttng_abi_exit(void)
1597 if (lttng_proc_dentry
)
1598 remove_proc_entry("lttng", NULL
);