6 * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; only
11 * version 2.1 of the License.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 * Mimic system calls for:
24 * - session creation, returns a file descriptor or failure.
25 * - channel creation, returns a file descriptor or failure.
26 * - Operates on a session file descriptor
27 * - Takes all channel options as parameters.
28 * - stream get, returns a file descriptor or failure.
29 * - Operates on a channel file descriptor.
30 * - stream notifier get, returns a file descriptor or failure.
31 * - Operates on a channel file descriptor.
32 * - event creation, returns a file descriptor or failure.
33 * - Operates on a channel file descriptor
34 * - Takes an event name as parameter
35 * - Takes an instrumentation source as parameter
36 * - e.g. tracepoints, dynamic_probes...
37 * - Takes instrumentation source specific arguments.
40 #include <linux/module.h>
41 #include <linux/proc_fs.h>
42 #include <linux/anon_inodes.h>
43 #include <linux/file.h>
44 #include <linux/uaccess.h>
45 #include <linux/slab.h>
46 #include <linux/err.h>
47 #include "wrapper/vmalloc.h" /* for wrapper_vmalloc_sync_all() */
48 #include "wrapper/ringbuffer/vfs.h"
49 #include "wrapper/ringbuffer/backend.h"
50 #include "wrapper/ringbuffer/frontend.h"
51 #include "wrapper/poll.h"
52 #include "wrapper/file.h"
53 #include "lttng-abi.h"
54 #include "lttng-abi-old.h"
55 #include "lttng-events.h"
56 #include "lttng-tracer.h"
57 #include "lib/ringbuffer/frontend_types.h"
60 * This is LTTng's own personal way to create a system call as an external
61 * module. We use ioctl() on /proc/lttng.
64 static struct proc_dir_entry
*lttng_proc_dentry
;
65 static const struct file_operations lttng_fops
;
66 static const struct file_operations lttng_session_fops
;
67 static const struct file_operations lttng_channel_fops
;
68 static const struct file_operations lttng_metadata_fops
;
69 static const struct file_operations lttng_event_fops
;
70 static struct file_operations lttng_stream_ring_buffer_file_operations
;
73 * Teardown management: opened file descriptors keep a refcount on the module,
74 * so it can only exit when all file descriptors are closed.
78 int lttng_abi_create_session(void)
80 struct lttng_session
*session
;
81 struct file
*session_file
;
84 session
= lttng_session_create();
87 session_fd
= lttng_get_unused_fd();
92 session_file
= anon_inode_getfile("[lttng_session]",
95 if (IS_ERR(session_file
)) {
96 ret
= PTR_ERR(session_file
);
99 session
->file
= session_file
;
100 fd_install(session_fd
, session_file
);
104 put_unused_fd(session_fd
);
106 lttng_session_destroy(session
);
111 int lttng_abi_tracepoint_list(void)
113 struct file
*tracepoint_list_file
;
116 file_fd
= lttng_get_unused_fd();
122 tracepoint_list_file
= anon_inode_getfile("[lttng_tracepoint_list]",
123 <tng_tracepoint_list_fops
,
125 if (IS_ERR(tracepoint_list_file
)) {
126 ret
= PTR_ERR(tracepoint_list_file
);
129 ret
= lttng_tracepoint_list_fops
.open(NULL
, tracepoint_list_file
);
132 fd_install(file_fd
, tracepoint_list_file
);
140 fput(tracepoint_list_file
);
142 put_unused_fd(file_fd
);
148 void lttng_abi_tracer_version(struct lttng_kernel_tracer_version
*v
)
150 v
->major
= LTTNG_MODULES_MAJOR_VERSION
;
151 v
->minor
= LTTNG_MODULES_MINOR_VERSION
;
152 v
->patchlevel
= LTTNG_MODULES_PATCHLEVEL_VERSION
;
156 void lttng_abi_tracer_abi_version(struct lttng_kernel_tracer_abi_version
*v
)
158 v
->major
= LTTNG_MODULES_ABI_MAJOR_VERSION
;
159 v
->minor
= LTTNG_MODULES_ABI_MINOR_VERSION
;
163 long lttng_abi_add_context(struct file
*file
,
164 struct lttng_kernel_context
*context_param
,
165 struct lttng_ctx
**ctx
, struct lttng_session
*session
)
168 if (session
->been_active
)
171 switch (context_param
->ctx
) {
172 case LTTNG_KERNEL_CONTEXT_PID
:
173 return lttng_add_pid_to_ctx(ctx
);
174 case LTTNG_KERNEL_CONTEXT_PRIO
:
175 return lttng_add_prio_to_ctx(ctx
);
176 case LTTNG_KERNEL_CONTEXT_NICE
:
177 return lttng_add_nice_to_ctx(ctx
);
178 case LTTNG_KERNEL_CONTEXT_VPID
:
179 return lttng_add_vpid_to_ctx(ctx
);
180 case LTTNG_KERNEL_CONTEXT_TID
:
181 return lttng_add_tid_to_ctx(ctx
);
182 case LTTNG_KERNEL_CONTEXT_VTID
:
183 return lttng_add_vtid_to_ctx(ctx
);
184 case LTTNG_KERNEL_CONTEXT_PPID
:
185 return lttng_add_ppid_to_ctx(ctx
);
186 case LTTNG_KERNEL_CONTEXT_VPPID
:
187 return lttng_add_vppid_to_ctx(ctx
);
188 case LTTNG_KERNEL_CONTEXT_PERF_COUNTER
:
189 context_param
->u
.perf_counter
.name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
190 return lttng_add_perf_counter_to_ctx(context_param
->u
.perf_counter
.type
,
191 context_param
->u
.perf_counter
.config
,
192 context_param
->u
.perf_counter
.name
,
194 case LTTNG_KERNEL_CONTEXT_PROCNAME
:
195 return lttng_add_procname_to_ctx(ctx
);
196 case LTTNG_KERNEL_CONTEXT_HOSTNAME
:
197 return lttng_add_hostname_to_ctx(ctx
);
204 * lttng_ioctl - lttng syscall through ioctl
210 * This ioctl implements lttng commands:
211 * LTTNG_KERNEL_SESSION
212 * Returns a LTTng trace session file descriptor
213 * LTTNG_KERNEL_TRACER_VERSION
214 * Returns the LTTng kernel tracer version
215 * LTTNG_KERNEL_TRACEPOINT_LIST
216 * Returns a file descriptor listing available tracepoints
217 * LTTNG_KERNEL_WAIT_QUIESCENT
218 * Returns after all previously running probes have completed
219 * LTTNG_KERNEL_TRACER_ABI_VERSION
220 * Returns the LTTng kernel tracer ABI version
222 * The returned session will be deleted when its file descriptor is closed.
225 long lttng_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
228 case LTTNG_KERNEL_OLD_SESSION
:
229 case LTTNG_KERNEL_SESSION
:
230 return lttng_abi_create_session();
231 case LTTNG_KERNEL_OLD_TRACER_VERSION
:
233 struct lttng_kernel_tracer_version v
;
234 struct lttng_kernel_old_tracer_version oldv
;
235 struct lttng_kernel_old_tracer_version
*uversion
=
236 (struct lttng_kernel_old_tracer_version __user
*) arg
;
238 lttng_abi_tracer_version(&v
);
239 oldv
.major
= v
.major
;
240 oldv
.minor
= v
.minor
;
241 oldv
.patchlevel
= v
.patchlevel
;
243 if (copy_to_user(uversion
, &oldv
, sizeof(oldv
)))
247 case LTTNG_KERNEL_TRACER_VERSION
:
249 struct lttng_kernel_tracer_version version
;
250 struct lttng_kernel_tracer_version
*uversion
=
251 (struct lttng_kernel_tracer_version __user
*) arg
;
253 lttng_abi_tracer_version(&version
);
255 if (copy_to_user(uversion
, &version
, sizeof(version
)))
259 case LTTNG_KERNEL_TRACER_ABI_VERSION
:
261 struct lttng_kernel_tracer_abi_version version
;
262 struct lttng_kernel_tracer_abi_version
*uversion
=
263 (struct lttng_kernel_tracer_abi_version __user
*) arg
;
265 lttng_abi_tracer_abi_version(&version
);
267 if (copy_to_user(uversion
, &version
, sizeof(version
)))
271 case LTTNG_KERNEL_OLD_TRACEPOINT_LIST
:
272 case LTTNG_KERNEL_TRACEPOINT_LIST
:
273 return lttng_abi_tracepoint_list();
274 case LTTNG_KERNEL_SYSCALL_LIST
:
275 return lttng_abi_syscall_list();
276 case LTTNG_KERNEL_OLD_WAIT_QUIESCENT
:
277 case LTTNG_KERNEL_WAIT_QUIESCENT
:
280 case LTTNG_KERNEL_OLD_CALIBRATE
:
282 struct lttng_kernel_old_calibrate __user
*ucalibrate
=
283 (struct lttng_kernel_old_calibrate __user
*) arg
;
284 struct lttng_kernel_old_calibrate old_calibrate
;
285 struct lttng_kernel_calibrate calibrate
;
288 if (copy_from_user(&old_calibrate
, ucalibrate
, sizeof(old_calibrate
)))
290 calibrate
.type
= old_calibrate
.type
;
291 ret
= lttng_calibrate(&calibrate
);
292 if (copy_to_user(ucalibrate
, &old_calibrate
, sizeof(old_calibrate
)))
296 case LTTNG_KERNEL_CALIBRATE
:
298 struct lttng_kernel_calibrate __user
*ucalibrate
=
299 (struct lttng_kernel_calibrate __user
*) arg
;
300 struct lttng_kernel_calibrate calibrate
;
303 if (copy_from_user(&calibrate
, ucalibrate
, sizeof(calibrate
)))
305 ret
= lttng_calibrate(&calibrate
);
306 if (copy_to_user(ucalibrate
, &calibrate
, sizeof(calibrate
)))
315 static const struct file_operations lttng_fops
= {
316 .owner
= THIS_MODULE
,
317 .unlocked_ioctl
= lttng_ioctl
,
319 .compat_ioctl
= lttng_ioctl
,
324 int lttng_abi_create_channel(struct file
*session_file
,
325 struct lttng_kernel_channel
*chan_param
,
326 enum channel_type channel_type
)
328 struct lttng_session
*session
= session_file
->private_data
;
329 const struct file_operations
*fops
= NULL
;
330 const char *transport_name
;
331 struct lttng_channel
*chan
;
332 struct file
*chan_file
;
336 chan_fd
= lttng_get_unused_fd();
341 switch (channel_type
) {
342 case PER_CPU_CHANNEL
:
343 fops
= <tng_channel_fops
;
345 case METADATA_CHANNEL
:
346 fops
= <tng_metadata_fops
;
350 chan_file
= anon_inode_getfile("[lttng_channel]",
353 if (IS_ERR(chan_file
)) {
354 ret
= PTR_ERR(chan_file
);
357 switch (channel_type
) {
358 case PER_CPU_CHANNEL
:
359 if (chan_param
->output
== LTTNG_KERNEL_SPLICE
) {
360 transport_name
= chan_param
->overwrite
?
361 "relay-overwrite" : "relay-discard";
362 } else if (chan_param
->output
== LTTNG_KERNEL_MMAP
) {
363 transport_name
= chan_param
->overwrite
?
364 "relay-overwrite-mmap" : "relay-discard-mmap";
369 case METADATA_CHANNEL
:
370 if (chan_param
->output
== LTTNG_KERNEL_SPLICE
)
371 transport_name
= "relay-metadata";
372 else if (chan_param
->output
== LTTNG_KERNEL_MMAP
)
373 transport_name
= "relay-metadata-mmap";
378 transport_name
= "<unknown>";
382 * We tolerate no failure path after channel creation. It will stay
383 * invariant for the rest of the session.
385 chan
= lttng_channel_create(session
, transport_name
, NULL
,
386 chan_param
->subbuf_size
,
387 chan_param
->num_subbuf
,
388 chan_param
->switch_timer_interval
,
389 chan_param
->read_timer_interval
,
395 chan
->file
= chan_file
;
396 chan_file
->private_data
= chan
;
397 fd_install(chan_fd
, chan_file
);
398 atomic_long_inc(&session_file
->f_count
);
405 put_unused_fd(chan_fd
);
411 * lttng_session_ioctl - lttng session fd ioctl
417 * This ioctl implements lttng commands:
418 * LTTNG_KERNEL_CHANNEL
419 * Returns a LTTng channel file descriptor
420 * LTTNG_KERNEL_ENABLE
421 * Enables tracing for a session (weak enable)
422 * LTTNG_KERNEL_DISABLE
423 * Disables tracing for a session (strong disable)
424 * LTTNG_KERNEL_METADATA
425 * Returns a LTTng metadata file descriptor
426 * LTTNG_KERNEL_SESSION_TRACK_PID
427 * Add PID to session tracker
428 * LTTNG_KERNEL_SESSION_UNTRACK_PID
429 * Remove PID from session tracker
431 * The returned channel will be deleted when its file descriptor is closed.
434 long lttng_session_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
436 struct lttng_session
*session
= file
->private_data
;
439 case LTTNG_KERNEL_OLD_CHANNEL
:
441 struct lttng_kernel_channel chan_param
;
442 struct lttng_kernel_old_channel old_chan_param
;
444 if (copy_from_user(&old_chan_param
,
445 (struct lttng_kernel_old_channel __user
*) arg
,
446 sizeof(struct lttng_kernel_old_channel
)))
448 chan_param
.overwrite
= old_chan_param
.overwrite
;
449 chan_param
.subbuf_size
= old_chan_param
.subbuf_size
;
450 chan_param
.num_subbuf
= old_chan_param
.num_subbuf
;
451 chan_param
.switch_timer_interval
= old_chan_param
.switch_timer_interval
;
452 chan_param
.read_timer_interval
= old_chan_param
.read_timer_interval
;
453 chan_param
.output
= old_chan_param
.output
;
455 return lttng_abi_create_channel(file
, &chan_param
,
458 case LTTNG_KERNEL_CHANNEL
:
460 struct lttng_kernel_channel chan_param
;
462 if (copy_from_user(&chan_param
,
463 (struct lttng_kernel_channel __user
*) arg
,
464 sizeof(struct lttng_kernel_channel
)))
466 return lttng_abi_create_channel(file
, &chan_param
,
469 case LTTNG_KERNEL_OLD_SESSION_START
:
470 case LTTNG_KERNEL_OLD_ENABLE
:
471 case LTTNG_KERNEL_SESSION_START
:
472 case LTTNG_KERNEL_ENABLE
:
473 return lttng_session_enable(session
);
474 case LTTNG_KERNEL_OLD_SESSION_STOP
:
475 case LTTNG_KERNEL_OLD_DISABLE
:
476 case LTTNG_KERNEL_SESSION_STOP
:
477 case LTTNG_KERNEL_DISABLE
:
478 return lttng_session_disable(session
);
479 case LTTNG_KERNEL_OLD_METADATA
:
481 struct lttng_kernel_channel chan_param
;
482 struct lttng_kernel_old_channel old_chan_param
;
484 if (copy_from_user(&old_chan_param
,
485 (struct lttng_kernel_old_channel __user
*) arg
,
486 sizeof(struct lttng_kernel_old_channel
)))
488 chan_param
.overwrite
= old_chan_param
.overwrite
;
489 chan_param
.subbuf_size
= old_chan_param
.subbuf_size
;
490 chan_param
.num_subbuf
= old_chan_param
.num_subbuf
;
491 chan_param
.switch_timer_interval
= old_chan_param
.switch_timer_interval
;
492 chan_param
.read_timer_interval
= old_chan_param
.read_timer_interval
;
493 chan_param
.output
= old_chan_param
.output
;
495 return lttng_abi_create_channel(file
, &chan_param
,
498 case LTTNG_KERNEL_METADATA
:
500 struct lttng_kernel_channel chan_param
;
502 if (copy_from_user(&chan_param
,
503 (struct lttng_kernel_channel __user
*) arg
,
504 sizeof(struct lttng_kernel_channel
)))
506 return lttng_abi_create_channel(file
, &chan_param
,
509 case LTTNG_KERNEL_SESSION_TRACK_PID
:
510 return lttng_session_track_pid(session
, (int) arg
);
511 case LTTNG_KERNEL_SESSION_UNTRACK_PID
:
512 return lttng_session_untrack_pid(session
, (int) arg
);
513 case LTTNG_KERNEL_SESSION_LIST_TRACKER_PIDS
:
514 return lttng_session_list_tracker_pids(session
);
521 * Called when the last file reference is dropped.
523 * Big fat note: channels and events are invariant for the whole session after
524 * their creation. So this session destruction also destroys all channel and
525 * event structures specific to this session (they are not destroyed when their
526 * individual file is released).
529 int lttng_session_release(struct inode
*inode
, struct file
*file
)
531 struct lttng_session
*session
= file
->private_data
;
534 lttng_session_destroy(session
);
538 static const struct file_operations lttng_session_fops
= {
539 .owner
= THIS_MODULE
,
540 .release
= lttng_session_release
,
541 .unlocked_ioctl
= lttng_session_ioctl
,
543 .compat_ioctl
= lttng_session_ioctl
,
548 * lttng_metadata_ring_buffer_poll - LTTng ring buffer poll file operation
552 * Handles the poll operations for the metadata channels.
555 unsigned int lttng_metadata_ring_buffer_poll(struct file
*filp
,
558 struct lttng_metadata_stream
*stream
= filp
->private_data
;
559 struct lib_ring_buffer
*buf
= stream
->priv
;
561 unsigned int mask
= 0;
563 if (filp
->f_mode
& FMODE_READ
) {
564 poll_wait_set_exclusive(wait
);
565 poll_wait(filp
, &stream
->read_wait
, wait
);
567 finalized
= stream
->finalized
;
570 * lib_ring_buffer_is_finalized() contains a smp_rmb()
571 * ordering finalized load before offsets loads.
573 WARN_ON(atomic_long_read(&buf
->active_readers
) != 1);
578 if (stream
->metadata_cache
->metadata_written
>
579 stream
->metadata_out
)
587 void lttng_metadata_ring_buffer_ioctl_put_next_subbuf(struct file
*filp
,
588 unsigned int cmd
, unsigned long arg
)
590 struct lttng_metadata_stream
*stream
= filp
->private_data
;
592 stream
->metadata_out
= stream
->metadata_in
;
596 long lttng_metadata_ring_buffer_ioctl(struct file
*filp
,
597 unsigned int cmd
, unsigned long arg
)
600 struct lttng_metadata_stream
*stream
= filp
->private_data
;
601 struct lib_ring_buffer
*buf
= stream
->priv
;
604 case RING_BUFFER_GET_NEXT_SUBBUF
:
606 struct lttng_metadata_stream
*stream
= filp
->private_data
;
607 struct lib_ring_buffer
*buf
= stream
->priv
;
608 struct channel
*chan
= buf
->backend
.chan
;
610 ret
= lttng_metadata_output_channel(stream
, chan
);
612 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
618 case RING_BUFFER_GET_SUBBUF
:
621 * Random access is not allowed for metadata channel.
625 case RING_BUFFER_FLUSH
:
627 struct lttng_metadata_stream
*stream
= filp
->private_data
;
628 struct lib_ring_buffer
*buf
= stream
->priv
;
629 struct channel
*chan
= buf
->backend
.chan
;
632 * Before doing the actual ring buffer flush, write up to one
633 * packet of metadata in the ring buffer.
635 ret
= lttng_metadata_output_channel(stream
, chan
);
643 /* PUT_SUBBUF is the one from lib ring buffer, unmodified. */
645 /* Performing lib ring buffer ioctl after our own. */
646 ret
= lib_ring_buffer_ioctl(filp
, cmd
, arg
, buf
);
651 case RING_BUFFER_PUT_NEXT_SUBBUF
:
653 lttng_metadata_ring_buffer_ioctl_put_next_subbuf(filp
,
666 long lttng_metadata_ring_buffer_compat_ioctl(struct file
*filp
,
667 unsigned int cmd
, unsigned long arg
)
670 struct lttng_metadata_stream
*stream
= filp
->private_data
;
671 struct lib_ring_buffer
*buf
= stream
->priv
;
674 case RING_BUFFER_GET_NEXT_SUBBUF
:
676 struct lttng_metadata_stream
*stream
= filp
->private_data
;
677 struct lib_ring_buffer
*buf
= stream
->priv
;
678 struct channel
*chan
= buf
->backend
.chan
;
680 ret
= lttng_metadata_output_channel(stream
, chan
);
682 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
688 case RING_BUFFER_GET_SUBBUF
:
691 * Random access is not allowed for metadata channel.
698 /* PUT_SUBBUF is the one from lib ring buffer, unmodified. */
700 /* Performing lib ring buffer ioctl after our own. */
701 ret
= lib_ring_buffer_compat_ioctl(filp
, cmd
, arg
, buf
);
706 case RING_BUFFER_PUT_NEXT_SUBBUF
:
708 lttng_metadata_ring_buffer_ioctl_put_next_subbuf(filp
,
721 * This is not used by anonymous file descriptors. This code is left
722 * there if we ever want to implement an inode with open() operation.
725 int lttng_metadata_ring_buffer_open(struct inode
*inode
, struct file
*file
)
727 struct lttng_metadata_stream
*stream
= inode
->i_private
;
728 struct lib_ring_buffer
*buf
= stream
->priv
;
730 file
->private_data
= buf
;
732 * Since life-time of metadata cache differs from that of
733 * session, we need to keep our own reference on the transport.
735 if (!try_module_get(stream
->transport
->owner
)) {
736 printk(KERN_WARNING
"LTT : Can't lock transport module.\n");
739 return lib_ring_buffer_open(inode
, file
, buf
);
743 int lttng_metadata_ring_buffer_release(struct inode
*inode
, struct file
*file
)
745 struct lttng_metadata_stream
*stream
= file
->private_data
;
746 struct lib_ring_buffer
*buf
= stream
->priv
;
748 kref_put(&stream
->metadata_cache
->refcount
, metadata_cache_destroy
);
749 module_put(stream
->transport
->owner
);
750 return lib_ring_buffer_release(inode
, file
, buf
);
754 ssize_t
lttng_metadata_ring_buffer_splice_read(struct file
*in
, loff_t
*ppos
,
755 struct pipe_inode_info
*pipe
, size_t len
,
758 struct lttng_metadata_stream
*stream
= in
->private_data
;
759 struct lib_ring_buffer
*buf
= stream
->priv
;
761 return lib_ring_buffer_splice_read(in
, ppos
, pipe
, len
,
766 int lttng_metadata_ring_buffer_mmap(struct file
*filp
,
767 struct vm_area_struct
*vma
)
769 struct lttng_metadata_stream
*stream
= filp
->private_data
;
770 struct lib_ring_buffer
*buf
= stream
->priv
;
772 return lib_ring_buffer_mmap(filp
, vma
, buf
);
776 const struct file_operations lttng_metadata_ring_buffer_file_operations
= {
777 .owner
= THIS_MODULE
,
778 .open
= lttng_metadata_ring_buffer_open
,
779 .release
= lttng_metadata_ring_buffer_release
,
780 .poll
= lttng_metadata_ring_buffer_poll
,
781 .splice_read
= lttng_metadata_ring_buffer_splice_read
,
782 .mmap
= lttng_metadata_ring_buffer_mmap
,
783 .unlocked_ioctl
= lttng_metadata_ring_buffer_ioctl
,
784 .llseek
= vfs_lib_ring_buffer_no_llseek
,
786 .compat_ioctl
= lttng_metadata_ring_buffer_compat_ioctl
,
791 int lttng_abi_create_stream_fd(struct file
*channel_file
, void *stream_priv
,
792 const struct file_operations
*fops
)
795 struct file
*stream_file
;
797 stream_fd
= lttng_get_unused_fd();
802 stream_file
= anon_inode_getfile("[lttng_stream]", fops
,
803 stream_priv
, O_RDWR
);
804 if (IS_ERR(stream_file
)) {
805 ret
= PTR_ERR(stream_file
);
809 * OPEN_FMODE, called within anon_inode_getfile/alloc_file, don't honor
810 * FMODE_LSEEK, FMODE_PREAD nor FMODE_PWRITE. We need to read from this
811 * file descriptor, so we set FMODE_PREAD here.
813 stream_file
->f_mode
|= FMODE_PREAD
;
814 fd_install(stream_fd
, stream_file
);
816 * The stream holds a reference to the channel within the generic ring
817 * buffer library, so no need to hold a refcount on the channel and
818 * session files here.
823 put_unused_fd(stream_fd
);
829 int lttng_abi_open_stream(struct file
*channel_file
)
831 struct lttng_channel
*channel
= channel_file
->private_data
;
832 struct lib_ring_buffer
*buf
;
836 buf
= channel
->ops
->buffer_read_open(channel
->chan
);
841 ret
= lttng_abi_create_stream_fd(channel_file
, stream_priv
,
842 <tng_stream_ring_buffer_file_operations
);
849 channel
->ops
->buffer_read_close(buf
);
854 int lttng_abi_open_metadata_stream(struct file
*channel_file
)
856 struct lttng_channel
*channel
= channel_file
->private_data
;
857 struct lttng_session
*session
= channel
->session
;
858 struct lib_ring_buffer
*buf
;
860 struct lttng_metadata_stream
*metadata_stream
;
863 buf
= channel
->ops
->buffer_read_open(channel
->chan
);
867 metadata_stream
= kzalloc(sizeof(struct lttng_metadata_stream
),
869 if (!metadata_stream
) {
873 metadata_stream
->metadata_cache
= session
->metadata_cache
;
874 init_waitqueue_head(&metadata_stream
->read_wait
);
875 metadata_stream
->priv
= buf
;
876 stream_priv
= metadata_stream
;
877 metadata_stream
->transport
= channel
->transport
;
878 mutex_init(&metadata_stream
->lock
);
881 * Since life-time of metadata cache differs from that of
882 * session, we need to keep our own reference on the transport.
884 if (!try_module_get(metadata_stream
->transport
->owner
)) {
885 printk(KERN_WARNING
"LTT : Can't lock transport module.\n");
890 ret
= lttng_abi_create_stream_fd(channel_file
, stream_priv
,
891 <tng_metadata_ring_buffer_file_operations
);
895 kref_get(&session
->metadata_cache
->refcount
);
896 list_add(&metadata_stream
->list
,
897 &session
->metadata_cache
->metadata_stream
);
901 module_put(metadata_stream
->transport
->owner
);
903 kfree(metadata_stream
);
905 channel
->ops
->buffer_read_close(buf
);
910 int lttng_abi_create_event(struct file
*channel_file
,
911 struct lttng_kernel_event
*event_param
)
913 struct lttng_channel
*channel
= channel_file
->private_data
;
915 struct file
*event_file
;
918 event_param
->name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
919 switch (event_param
->instrumentation
) {
920 case LTTNG_KERNEL_KRETPROBE
:
921 event_param
->u
.kretprobe
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
923 case LTTNG_KERNEL_KPROBE
:
924 event_param
->u
.kprobe
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
926 case LTTNG_KERNEL_FUNCTION
:
927 event_param
->u
.ftrace
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
932 switch (event_param
->instrumentation
) {
934 event_fd
= lttng_get_unused_fd();
939 event_file
= anon_inode_getfile("[lttng_event]",
942 if (IS_ERR(event_file
)) {
943 ret
= PTR_ERR(event_file
);
946 if (event_param
->instrumentation
== LTTNG_KERNEL_TRACEPOINT
) {
947 struct lttng_enabler
*enabler
;
949 if (event_param
->name
[strlen(event_param
->name
) - 1] == '*') {
950 enabler
= lttng_enabler_create(LTTNG_ENABLER_WILDCARD
,
951 event_param
, channel
);
953 enabler
= lttng_enabler_create(LTTNG_ENABLER_NAME
,
954 event_param
, channel
);
958 struct lttng_event
*event
;
961 * We tolerate no failure path after event creation. It
962 * will stay invariant for the rest of the session.
964 event
= lttng_event_create(channel
, event_param
,
966 event_param
->instrumentation
);
967 WARN_ON_ONCE(!event
);
969 ret
= PTR_ERR(event
);
974 event_file
->private_data
= priv
;
975 fd_install(event_fd
, event_file
);
976 /* The event holds a reference on the channel */
977 atomic_long_inc(&channel_file
->f_count
);
979 case LTTNG_KERNEL_SYSCALL
:
980 ret
= lttng_syscalls_register(channel
, NULL
);
984 if (event_param
->u
.syscall
.enable
) {
985 ret
= lttng_syscall_filter_enable(channel
,
986 !strcmp(event_param
->name
, "*") ?
987 NULL
: event_param
->name
);
992 ret
= lttng_syscall_filter_disable(channel
,
993 !strcmp(event_param
->name
, "*") ?
994 NULL
: event_param
->name
);
1005 put_unused_fd(event_fd
);
1011 * lttng_channel_ioctl - lttng syscall through ioctl
1017 * This ioctl implements lttng commands:
1018 * LTTNG_KERNEL_STREAM
1019 * Returns an event stream file descriptor or failure.
1020 * (typically, one event stream records events from one CPU)
1021 * LTTNG_KERNEL_EVENT
1022 * Returns an event file descriptor or failure.
1023 * LTTNG_KERNEL_CONTEXT
1024 * Prepend a context field to each event in the channel
1025 * LTTNG_KERNEL_ENABLE
1026 * Enable recording for events in this channel (weak enable)
1027 * LTTNG_KERNEL_DISABLE
1028 * Disable recording for events in this channel (strong disable)
1030 * Channel and event file descriptors also hold a reference on the session.
1033 long lttng_channel_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1035 struct lttng_channel
*channel
= file
->private_data
;
1038 case LTTNG_KERNEL_OLD_STREAM
:
1039 case LTTNG_KERNEL_STREAM
:
1040 return lttng_abi_open_stream(file
);
1041 case LTTNG_KERNEL_OLD_EVENT
:
1043 struct lttng_kernel_event
*uevent_param
;
1044 struct lttng_kernel_old_event
*old_uevent_param
;
1047 uevent_param
= kmalloc(sizeof(struct lttng_kernel_event
),
1049 if (!uevent_param
) {
1053 old_uevent_param
= kmalloc(
1054 sizeof(struct lttng_kernel_old_event
),
1056 if (!old_uevent_param
) {
1058 goto old_event_error_free_param
;
1060 if (copy_from_user(old_uevent_param
,
1061 (struct lttng_kernel_old_event __user
*) arg
,
1062 sizeof(struct lttng_kernel_old_event
))) {
1064 goto old_event_error_free_old_param
;
1067 memcpy(uevent_param
->name
, old_uevent_param
->name
,
1068 sizeof(uevent_param
->name
));
1069 uevent_param
->instrumentation
=
1070 old_uevent_param
->instrumentation
;
1072 switch (old_uevent_param
->instrumentation
) {
1073 case LTTNG_KERNEL_KPROBE
:
1074 uevent_param
->u
.kprobe
.addr
=
1075 old_uevent_param
->u
.kprobe
.addr
;
1076 uevent_param
->u
.kprobe
.offset
=
1077 old_uevent_param
->u
.kprobe
.offset
;
1078 memcpy(uevent_param
->u
.kprobe
.symbol_name
,
1079 old_uevent_param
->u
.kprobe
.symbol_name
,
1080 sizeof(uevent_param
->u
.kprobe
.symbol_name
));
1082 case LTTNG_KERNEL_KRETPROBE
:
1083 uevent_param
->u
.kretprobe
.addr
=
1084 old_uevent_param
->u
.kretprobe
.addr
;
1085 uevent_param
->u
.kretprobe
.offset
=
1086 old_uevent_param
->u
.kretprobe
.offset
;
1087 memcpy(uevent_param
->u
.kretprobe
.symbol_name
,
1088 old_uevent_param
->u
.kretprobe
.symbol_name
,
1089 sizeof(uevent_param
->u
.kretprobe
.symbol_name
));
1091 case LTTNG_KERNEL_FUNCTION
:
1092 memcpy(uevent_param
->u
.ftrace
.symbol_name
,
1093 old_uevent_param
->u
.ftrace
.symbol_name
,
1094 sizeof(uevent_param
->u
.ftrace
.symbol_name
));
1099 ret
= lttng_abi_create_event(file
, uevent_param
);
1101 old_event_error_free_old_param
:
1102 kfree(old_uevent_param
);
1103 old_event_error_free_param
:
1104 kfree(uevent_param
);
1108 case LTTNG_KERNEL_EVENT
:
1110 struct lttng_kernel_event uevent_param
;
1112 if (copy_from_user(&uevent_param
,
1113 (struct lttng_kernel_event __user
*) arg
,
1114 sizeof(uevent_param
)))
1116 return lttng_abi_create_event(file
, &uevent_param
);
1118 case LTTNG_KERNEL_OLD_CONTEXT
:
1120 struct lttng_kernel_context
*ucontext_param
;
1121 struct lttng_kernel_old_context
*old_ucontext_param
;
1124 ucontext_param
= kmalloc(sizeof(struct lttng_kernel_context
),
1126 if (!ucontext_param
) {
1130 old_ucontext_param
= kmalloc(sizeof(struct lttng_kernel_old_context
),
1132 if (!old_ucontext_param
) {
1134 goto old_ctx_error_free_param
;
1137 if (copy_from_user(old_ucontext_param
,
1138 (struct lttng_kernel_old_context __user
*) arg
,
1139 sizeof(struct lttng_kernel_old_context
))) {
1141 goto old_ctx_error_free_old_param
;
1143 ucontext_param
->ctx
= old_ucontext_param
->ctx
;
1144 memcpy(ucontext_param
->padding
, old_ucontext_param
->padding
,
1145 sizeof(ucontext_param
->padding
));
1146 /* only type that uses the union */
1147 if (old_ucontext_param
->ctx
== LTTNG_KERNEL_CONTEXT_PERF_COUNTER
) {
1148 ucontext_param
->u
.perf_counter
.type
=
1149 old_ucontext_param
->u
.perf_counter
.type
;
1150 ucontext_param
->u
.perf_counter
.config
=
1151 old_ucontext_param
->u
.perf_counter
.config
;
1152 memcpy(ucontext_param
->u
.perf_counter
.name
,
1153 old_ucontext_param
->u
.perf_counter
.name
,
1154 sizeof(ucontext_param
->u
.perf_counter
.name
));
1157 ret
= lttng_abi_add_context(file
,
1159 &channel
->ctx
, channel
->session
);
1161 old_ctx_error_free_old_param
:
1162 kfree(old_ucontext_param
);
1163 old_ctx_error_free_param
:
1164 kfree(ucontext_param
);
1168 case LTTNG_KERNEL_CONTEXT
:
1170 struct lttng_kernel_context ucontext_param
;
1172 if (copy_from_user(&ucontext_param
,
1173 (struct lttng_kernel_context __user
*) arg
,
1174 sizeof(ucontext_param
)))
1176 return lttng_abi_add_context(file
,
1178 &channel
->ctx
, channel
->session
);
1180 case LTTNG_KERNEL_OLD_ENABLE
:
1181 case LTTNG_KERNEL_ENABLE
:
1182 return lttng_channel_enable(channel
);
1183 case LTTNG_KERNEL_OLD_DISABLE
:
1184 case LTTNG_KERNEL_DISABLE
:
1185 return lttng_channel_disable(channel
);
1186 case LTTNG_KERNEL_SYSCALL_MASK
:
1187 return lttng_channel_syscall_mask(channel
,
1188 (struct lttng_kernel_syscall_mask __user
*) arg
);
1190 return -ENOIOCTLCMD
;
1196 * lttng_metadata_ioctl - lttng syscall through ioctl
1202 * This ioctl implements lttng commands:
1203 * LTTNG_KERNEL_STREAM
1204 * Returns an event stream file descriptor or failure.
1206 * Channel and event file descriptors also hold a reference on the session.
1209 long lttng_metadata_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1212 case LTTNG_KERNEL_OLD_STREAM
:
1213 case LTTNG_KERNEL_STREAM
:
1214 return lttng_abi_open_metadata_stream(file
);
1216 return -ENOIOCTLCMD
;
1221 * lttng_channel_poll - lttng stream addition/removal monitoring
1226 unsigned int lttng_channel_poll(struct file
*file
, poll_table
*wait
)
1228 struct lttng_channel
*channel
= file
->private_data
;
1229 unsigned int mask
= 0;
1231 if (file
->f_mode
& FMODE_READ
) {
1232 poll_wait_set_exclusive(wait
);
1233 poll_wait(file
, channel
->ops
->get_hp_wait_queue(channel
->chan
),
1236 if (channel
->ops
->is_disabled(channel
->chan
))
1238 if (channel
->ops
->is_finalized(channel
->chan
))
1240 if (channel
->ops
->buffer_has_read_closed_stream(channel
->chan
))
1241 return POLLIN
| POLLRDNORM
;
1249 int lttng_channel_release(struct inode
*inode
, struct file
*file
)
1251 struct lttng_channel
*channel
= file
->private_data
;
1254 fput(channel
->session
->file
);
1259 int lttng_metadata_channel_release(struct inode
*inode
, struct file
*file
)
1261 struct lttng_channel
*channel
= file
->private_data
;
1264 lttng_metadata_channel_destroy(channel
);
1265 fput(channel
->session
->file
);
1271 static const struct file_operations lttng_channel_fops
= {
1272 .owner
= THIS_MODULE
,
1273 .release
= lttng_channel_release
,
1274 .poll
= lttng_channel_poll
,
1275 .unlocked_ioctl
= lttng_channel_ioctl
,
1276 #ifdef CONFIG_COMPAT
1277 .compat_ioctl
= lttng_channel_ioctl
,
1281 static const struct file_operations lttng_metadata_fops
= {
1282 .owner
= THIS_MODULE
,
1283 .release
= lttng_metadata_channel_release
,
1284 .unlocked_ioctl
= lttng_metadata_ioctl
,
1285 #ifdef CONFIG_COMPAT
1286 .compat_ioctl
= lttng_metadata_ioctl
,
1291 * lttng_event_ioctl - lttng syscall through ioctl
1297 * This ioctl implements lttng commands:
1298 * LTTNG_KERNEL_CONTEXT
1299 * Prepend a context field to each record of this event
1300 * LTTNG_KERNEL_ENABLE
1301 * Enable recording for this event (weak enable)
1302 * LTTNG_KERNEL_DISABLE
1303 * Disable recording for this event (strong disable)
1306 long lttng_event_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1308 struct lttng_event
*event
;
1309 struct lttng_enabler
*enabler
;
1310 enum lttng_event_type
*evtype
= file
->private_data
;
1313 case LTTNG_KERNEL_OLD_CONTEXT
:
1315 /* Not implemented */
1318 case LTTNG_KERNEL_CONTEXT
:
1320 /* Not implemented */
1323 case LTTNG_KERNEL_OLD_ENABLE
:
1324 case LTTNG_KERNEL_ENABLE
:
1326 case LTTNG_TYPE_EVENT
:
1327 event
= file
->private_data
;
1328 return lttng_event_enable(event
);
1329 case LTTNG_TYPE_ENABLER
:
1330 enabler
= file
->private_data
;
1331 return lttng_enabler_enable(enabler
);
1336 case LTTNG_KERNEL_OLD_DISABLE
:
1337 case LTTNG_KERNEL_DISABLE
:
1339 case LTTNG_TYPE_EVENT
:
1340 event
= file
->private_data
;
1341 return lttng_event_disable(event
);
1342 case LTTNG_TYPE_ENABLER
:
1343 enabler
= file
->private_data
;
1344 return lttng_enabler_disable(enabler
);
1350 return -ENOIOCTLCMD
;
1355 int lttng_event_release(struct inode
*inode
, struct file
*file
)
1357 struct lttng_event
*event
;
1358 struct lttng_enabler
*enabler
;
1359 enum lttng_event_type
*evtype
= file
->private_data
;
1365 case LTTNG_TYPE_EVENT
:
1366 event
= file
->private_data
;
1368 fput(event
->chan
->file
);
1370 case LTTNG_TYPE_ENABLER
:
1371 enabler
= file
->private_data
;
1373 fput(enabler
->chan
->file
);
1383 /* TODO: filter control ioctl */
1384 static const struct file_operations lttng_event_fops
= {
1385 .owner
= THIS_MODULE
,
1386 .release
= lttng_event_release
,
1387 .unlocked_ioctl
= lttng_event_ioctl
,
1388 #ifdef CONFIG_COMPAT
1389 .compat_ioctl
= lttng_event_ioctl
,
1393 static int put_u64(uint64_t val
, unsigned long arg
)
1395 return put_user(val
, (uint64_t __user
*) arg
);
1398 static long lttng_stream_ring_buffer_ioctl(struct file
*filp
,
1399 unsigned int cmd
, unsigned long arg
)
1401 struct lib_ring_buffer
*buf
= filp
->private_data
;
1402 struct channel
*chan
= buf
->backend
.chan
;
1403 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1404 const struct lttng_channel_ops
*ops
= chan
->backend
.priv_ops
;
1407 if (atomic_read(&chan
->record_disabled
))
1411 case LTTNG_RING_BUFFER_GET_TIMESTAMP_BEGIN
:
1415 ret
= ops
->timestamp_begin(config
, buf
, &ts
);
1418 return put_u64(ts
, arg
);
1420 case LTTNG_RING_BUFFER_GET_TIMESTAMP_END
:
1424 ret
= ops
->timestamp_end(config
, buf
, &ts
);
1427 return put_u64(ts
, arg
);
1429 case LTTNG_RING_BUFFER_GET_EVENTS_DISCARDED
:
1433 ret
= ops
->events_discarded(config
, buf
, &ed
);
1436 return put_u64(ed
, arg
);
1438 case LTTNG_RING_BUFFER_GET_CONTENT_SIZE
:
1442 ret
= ops
->content_size(config
, buf
, &cs
);
1445 return put_u64(cs
, arg
);
1447 case LTTNG_RING_BUFFER_GET_PACKET_SIZE
:
1451 ret
= ops
->packet_size(config
, buf
, &ps
);
1454 return put_u64(ps
, arg
);
1456 case LTTNG_RING_BUFFER_GET_STREAM_ID
:
1460 ret
= ops
->stream_id(config
, buf
, &si
);
1463 return put_u64(si
, arg
);
1465 case LTTNG_RING_BUFFER_GET_CURRENT_TIMESTAMP
:
1469 ret
= ops
->current_timestamp(config
, buf
, &ts
);
1472 return put_u64(ts
, arg
);
1475 return lib_ring_buffer_file_operations
.unlocked_ioctl(filp
,
1483 #ifdef CONFIG_COMPAT
1484 static long lttng_stream_ring_buffer_compat_ioctl(struct file
*filp
,
1485 unsigned int cmd
, unsigned long arg
)
1487 struct lib_ring_buffer
*buf
= filp
->private_data
;
1488 struct channel
*chan
= buf
->backend
.chan
;
1489 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1490 const struct lttng_channel_ops
*ops
= chan
->backend
.priv_ops
;
1493 if (atomic_read(&chan
->record_disabled
))
1497 case LTTNG_RING_BUFFER_COMPAT_GET_TIMESTAMP_BEGIN
:
1501 ret
= ops
->timestamp_begin(config
, buf
, &ts
);
1504 return put_u64(ts
, arg
);
1506 case LTTNG_RING_BUFFER_COMPAT_GET_TIMESTAMP_END
:
1510 ret
= ops
->timestamp_end(config
, buf
, &ts
);
1513 return put_u64(ts
, arg
);
1515 case LTTNG_RING_BUFFER_COMPAT_GET_EVENTS_DISCARDED
:
1519 ret
= ops
->events_discarded(config
, buf
, &ed
);
1522 return put_u64(ed
, arg
);
1524 case LTTNG_RING_BUFFER_COMPAT_GET_CONTENT_SIZE
:
1528 ret
= ops
->content_size(config
, buf
, &cs
);
1531 return put_u64(cs
, arg
);
1533 case LTTNG_RING_BUFFER_COMPAT_GET_PACKET_SIZE
:
1537 ret
= ops
->packet_size(config
, buf
, &ps
);
1540 return put_u64(ps
, arg
);
1542 case LTTNG_RING_BUFFER_COMPAT_GET_STREAM_ID
:
1546 ret
= ops
->stream_id(config
, buf
, &si
);
1549 return put_u64(si
, arg
);
1551 case LTTNG_RING_BUFFER_GET_CURRENT_TIMESTAMP
:
1555 ret
= ops
->current_timestamp(config
, buf
, &ts
);
1558 return put_u64(ts
, arg
);
1561 return lib_ring_buffer_file_operations
.compat_ioctl(filp
,
1568 #endif /* CONFIG_COMPAT */
1570 static void lttng_stream_override_ring_buffer_fops(void)
1572 lttng_stream_ring_buffer_file_operations
.owner
= THIS_MODULE
;
1573 lttng_stream_ring_buffer_file_operations
.open
=
1574 lib_ring_buffer_file_operations
.open
;
1575 lttng_stream_ring_buffer_file_operations
.release
=
1576 lib_ring_buffer_file_operations
.release
;
1577 lttng_stream_ring_buffer_file_operations
.poll
=
1578 lib_ring_buffer_file_operations
.poll
;
1579 lttng_stream_ring_buffer_file_operations
.splice_read
=
1580 lib_ring_buffer_file_operations
.splice_read
;
1581 lttng_stream_ring_buffer_file_operations
.mmap
=
1582 lib_ring_buffer_file_operations
.mmap
;
1583 lttng_stream_ring_buffer_file_operations
.unlocked_ioctl
=
1584 lttng_stream_ring_buffer_ioctl
;
1585 lttng_stream_ring_buffer_file_operations
.llseek
=
1586 lib_ring_buffer_file_operations
.llseek
;
1587 #ifdef CONFIG_COMPAT
1588 lttng_stream_ring_buffer_file_operations
.compat_ioctl
=
1589 lttng_stream_ring_buffer_compat_ioctl
;
1593 int __init
lttng_abi_init(void)
1597 wrapper_vmalloc_sync_all();
1598 lttng_proc_dentry
= proc_create_data("lttng", S_IRUSR
| S_IWUSR
, NULL
,
1601 if (!lttng_proc_dentry
) {
1602 printk(KERN_ERR
"Error creating LTTng control file\n");
1606 lttng_stream_override_ring_buffer_fops();
1612 /* No __exit annotation because used by init error path too. */
1613 void lttng_abi_exit(void)
1615 if (lttng_proc_dentry
)
1616 remove_proc_entry("lttng", NULL
);