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"
57 * This is LTTng's own personal way to create a system call as an external
58 * module. We use ioctl() on /proc/lttng.
61 static struct proc_dir_entry
*lttng_proc_dentry
;
62 static const struct file_operations lttng_fops
;
63 static const struct file_operations lttng_session_fops
;
64 static const struct file_operations lttng_channel_fops
;
65 static const struct file_operations lttng_metadata_fops
;
66 static const struct file_operations lttng_event_fops
;
67 static struct file_operations lttng_stream_ring_buffer_file_operations
;
70 * Teardown management: opened file descriptors keep a refcount on the module,
71 * so it can only exit when all file descriptors are closed.
75 int lttng_abi_create_session(void)
77 struct lttng_session
*session
;
78 struct file
*session_file
;
81 session
= lttng_session_create();
84 session_fd
= get_unused_fd();
89 session_file
= anon_inode_getfile("[lttng_session]",
92 if (IS_ERR(session_file
)) {
93 ret
= PTR_ERR(session_file
);
96 session
->file
= session_file
;
97 fd_install(session_fd
, session_file
);
101 put_unused_fd(session_fd
);
103 lttng_session_destroy(session
);
108 int lttng_abi_tracepoint_list(void)
110 struct file
*tracepoint_list_file
;
113 file_fd
= get_unused_fd();
119 tracepoint_list_file
= anon_inode_getfile("[lttng_session]",
120 <tng_tracepoint_list_fops
,
122 if (IS_ERR(tracepoint_list_file
)) {
123 ret
= PTR_ERR(tracepoint_list_file
);
126 ret
= lttng_tracepoint_list_fops
.open(NULL
, tracepoint_list_file
);
129 fd_install(file_fd
, tracepoint_list_file
);
137 fput(tracepoint_list_file
);
139 put_unused_fd(file_fd
);
145 void lttng_abi_tracer_version(struct lttng_kernel_tracer_version
*v
)
147 v
->major
= LTTNG_MODULES_MAJOR_VERSION
;
148 v
->minor
= LTTNG_MODULES_MINOR_VERSION
;
149 v
->patchlevel
= LTTNG_MODULES_PATCHLEVEL_VERSION
;
153 long lttng_abi_add_context(struct file
*file
,
154 struct lttng_kernel_context
*context_param
,
155 struct lttng_ctx
**ctx
, struct lttng_session
*session
)
158 if (session
->been_active
)
161 switch (context_param
->ctx
) {
162 case LTTNG_KERNEL_CONTEXT_PID
:
163 return lttng_add_pid_to_ctx(ctx
);
164 case LTTNG_KERNEL_CONTEXT_PRIO
:
165 return lttng_add_prio_to_ctx(ctx
);
166 case LTTNG_KERNEL_CONTEXT_NICE
:
167 return lttng_add_nice_to_ctx(ctx
);
168 case LTTNG_KERNEL_CONTEXT_VPID
:
169 return lttng_add_vpid_to_ctx(ctx
);
170 case LTTNG_KERNEL_CONTEXT_TID
:
171 return lttng_add_tid_to_ctx(ctx
);
172 case LTTNG_KERNEL_CONTEXT_VTID
:
173 return lttng_add_vtid_to_ctx(ctx
);
174 case LTTNG_KERNEL_CONTEXT_PPID
:
175 return lttng_add_ppid_to_ctx(ctx
);
176 case LTTNG_KERNEL_CONTEXT_VPPID
:
177 return lttng_add_vppid_to_ctx(ctx
);
178 case LTTNG_KERNEL_CONTEXT_PERF_COUNTER
:
179 context_param
->u
.perf_counter
.name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
180 return lttng_add_perf_counter_to_ctx(context_param
->u
.perf_counter
.type
,
181 context_param
->u
.perf_counter
.config
,
182 context_param
->u
.perf_counter
.name
,
184 case LTTNG_KERNEL_CONTEXT_PROCNAME
:
185 return lttng_add_procname_to_ctx(ctx
);
186 case LTTNG_KERNEL_CONTEXT_HOSTNAME
:
187 return lttng_add_hostname_to_ctx(ctx
);
194 * lttng_ioctl - lttng syscall through ioctl
200 * This ioctl implements lttng commands:
201 * LTTNG_KERNEL_SESSION
202 * Returns a LTTng trace session file descriptor
203 * LTTNG_KERNEL_TRACER_VERSION
204 * Returns the LTTng kernel tracer version
205 * LTTNG_KERNEL_TRACEPOINT_LIST
206 * Returns a file descriptor listing available tracepoints
207 * LTTNG_KERNEL_WAIT_QUIESCENT
208 * Returns after all previously running probes have completed
210 * The returned session will be deleted when its file descriptor is closed.
213 long lttng_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
216 case LTTNG_KERNEL_OLD_SESSION
:
217 case LTTNG_KERNEL_SESSION
:
218 return lttng_abi_create_session();
219 case LTTNG_KERNEL_OLD_TRACER_VERSION
:
221 struct lttng_kernel_tracer_version v
;
222 struct lttng_kernel_old_tracer_version oldv
;
223 struct lttng_kernel_old_tracer_version
*uversion
=
224 (struct lttng_kernel_old_tracer_version __user
*) arg
;
226 lttng_abi_tracer_version(&v
);
227 oldv
.major
= v
.major
;
228 oldv
.minor
= v
.minor
;
229 oldv
.patchlevel
= v
.patchlevel
;
231 if (copy_to_user(uversion
, &oldv
, sizeof(oldv
)))
235 case LTTNG_KERNEL_TRACER_VERSION
:
237 struct lttng_kernel_tracer_version version
;
238 struct lttng_kernel_tracer_version
*uversion
=
239 (struct lttng_kernel_tracer_version __user
*) arg
;
241 lttng_abi_tracer_version(&version
);
243 if (copy_to_user(uversion
, &version
, sizeof(version
)))
247 case LTTNG_KERNEL_OLD_TRACEPOINT_LIST
:
248 case LTTNG_KERNEL_TRACEPOINT_LIST
:
249 return lttng_abi_tracepoint_list();
250 case LTTNG_KERNEL_OLD_WAIT_QUIESCENT
:
251 case LTTNG_KERNEL_WAIT_QUIESCENT
:
254 case LTTNG_KERNEL_OLD_CALIBRATE
:
256 struct lttng_kernel_old_calibrate __user
*ucalibrate
=
257 (struct lttng_kernel_old_calibrate __user
*) arg
;
258 struct lttng_kernel_old_calibrate old_calibrate
;
259 struct lttng_kernel_calibrate calibrate
;
262 if (copy_from_user(&old_calibrate
, ucalibrate
, sizeof(old_calibrate
)))
264 calibrate
.type
= old_calibrate
.type
;
265 ret
= lttng_calibrate(&calibrate
);
266 if (copy_to_user(ucalibrate
, &old_calibrate
, sizeof(old_calibrate
)))
270 case LTTNG_KERNEL_CALIBRATE
:
272 struct lttng_kernel_calibrate __user
*ucalibrate
=
273 (struct lttng_kernel_calibrate __user
*) arg
;
274 struct lttng_kernel_calibrate calibrate
;
277 if (copy_from_user(&calibrate
, ucalibrate
, sizeof(calibrate
)))
279 ret
= lttng_calibrate(&calibrate
);
280 if (copy_to_user(ucalibrate
, &calibrate
, sizeof(calibrate
)))
289 static const struct file_operations lttng_fops
= {
290 .owner
= THIS_MODULE
,
291 .unlocked_ioctl
= lttng_ioctl
,
293 .compat_ioctl
= lttng_ioctl
,
298 int lttng_abi_create_channel(struct file
*session_file
,
299 struct lttng_kernel_channel
*chan_param
,
300 enum channel_type channel_type
)
302 struct lttng_session
*session
= session_file
->private_data
;
303 const struct file_operations
*fops
= NULL
;
304 const char *transport_name
;
305 struct lttng_channel
*chan
;
306 struct file
*chan_file
;
310 chan_fd
= get_unused_fd();
315 switch (channel_type
) {
316 case PER_CPU_CHANNEL
:
317 fops
= <tng_channel_fops
;
319 case METADATA_CHANNEL
:
320 fops
= <tng_metadata_fops
;
324 chan_file
= anon_inode_getfile("[lttng_channel]",
327 if (IS_ERR(chan_file
)) {
328 ret
= PTR_ERR(chan_file
);
331 switch (channel_type
) {
332 case PER_CPU_CHANNEL
:
333 if (chan_param
->output
== LTTNG_KERNEL_SPLICE
) {
334 transport_name
= chan_param
->overwrite
?
335 "relay-overwrite" : "relay-discard";
336 } else if (chan_param
->output
== LTTNG_KERNEL_MMAP
) {
337 transport_name
= chan_param
->overwrite
?
338 "relay-overwrite-mmap" : "relay-discard-mmap";
343 case METADATA_CHANNEL
:
344 if (chan_param
->output
== LTTNG_KERNEL_SPLICE
)
345 transport_name
= "relay-metadata";
346 else if (chan_param
->output
== LTTNG_KERNEL_MMAP
)
347 transport_name
= "relay-metadata-mmap";
352 transport_name
= "<unknown>";
356 * We tolerate no failure path after channel creation. It will stay
357 * invariant for the rest of the session.
359 chan
= lttng_channel_create(session
, transport_name
, NULL
,
360 chan_param
->subbuf_size
,
361 chan_param
->num_subbuf
,
362 chan_param
->switch_timer_interval
,
363 chan_param
->read_timer_interval
,
369 chan
->file
= chan_file
;
370 chan_file
->private_data
= chan
;
371 fd_install(chan_fd
, chan_file
);
372 atomic_long_inc(&session_file
->f_count
);
379 put_unused_fd(chan_fd
);
385 * lttng_session_ioctl - lttng session fd ioctl
391 * This ioctl implements lttng commands:
392 * LTTNG_KERNEL_CHANNEL
393 * Returns a LTTng channel file descriptor
394 * LTTNG_KERNEL_ENABLE
395 * Enables tracing for a session (weak enable)
396 * LTTNG_KERNEL_DISABLE
397 * Disables tracing for a session (strong disable)
398 * LTTNG_KERNEL_METADATA
399 * Returns a LTTng metadata file descriptor
401 * The returned channel will be deleted when its file descriptor is closed.
404 long lttng_session_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
406 struct lttng_session
*session
= file
->private_data
;
409 case LTTNG_KERNEL_OLD_CHANNEL
:
411 struct lttng_kernel_channel chan_param
;
412 struct lttng_kernel_old_channel old_chan_param
;
414 if (copy_from_user(&old_chan_param
,
415 (struct lttng_kernel_old_channel __user
*) arg
,
416 sizeof(struct lttng_kernel_old_channel
)))
418 chan_param
.overwrite
= old_chan_param
.overwrite
;
419 chan_param
.subbuf_size
= old_chan_param
.subbuf_size
;
420 chan_param
.num_subbuf
= old_chan_param
.num_subbuf
;
421 chan_param
.switch_timer_interval
= old_chan_param
.switch_timer_interval
;
422 chan_param
.read_timer_interval
= old_chan_param
.read_timer_interval
;
423 chan_param
.output
= old_chan_param
.output
;
425 return lttng_abi_create_channel(file
, &chan_param
,
428 case LTTNG_KERNEL_CHANNEL
:
430 struct lttng_kernel_channel chan_param
;
432 if (copy_from_user(&chan_param
,
433 (struct lttng_kernel_channel __user
*) arg
,
434 sizeof(struct lttng_kernel_channel
)))
436 return lttng_abi_create_channel(file
, &chan_param
,
439 case LTTNG_KERNEL_OLD_SESSION_START
:
440 case LTTNG_KERNEL_OLD_ENABLE
:
441 case LTTNG_KERNEL_SESSION_START
:
442 case LTTNG_KERNEL_ENABLE
:
443 return lttng_session_enable(session
);
444 case LTTNG_KERNEL_OLD_SESSION_STOP
:
445 case LTTNG_KERNEL_OLD_DISABLE
:
446 case LTTNG_KERNEL_SESSION_STOP
:
447 case LTTNG_KERNEL_DISABLE
:
448 return lttng_session_disable(session
);
449 case LTTNG_KERNEL_OLD_METADATA
:
451 struct lttng_kernel_channel chan_param
;
452 struct lttng_kernel_old_channel old_chan_param
;
454 if (copy_from_user(&old_chan_param
,
455 (struct lttng_kernel_old_channel __user
*) arg
,
456 sizeof(struct lttng_kernel_old_channel
)))
458 chan_param
.overwrite
= old_chan_param
.overwrite
;
459 chan_param
.subbuf_size
= old_chan_param
.subbuf_size
;
460 chan_param
.num_subbuf
= old_chan_param
.num_subbuf
;
461 chan_param
.switch_timer_interval
= old_chan_param
.switch_timer_interval
;
462 chan_param
.read_timer_interval
= old_chan_param
.read_timer_interval
;
463 chan_param
.output
= old_chan_param
.output
;
465 return lttng_abi_create_channel(file
, &chan_param
,
468 case LTTNG_KERNEL_METADATA
:
470 struct lttng_kernel_channel chan_param
;
472 if (copy_from_user(&chan_param
,
473 (struct lttng_kernel_channel __user
*) arg
,
474 sizeof(struct lttng_kernel_channel
)))
476 return lttng_abi_create_channel(file
, &chan_param
,
485 * Called when the last file reference is dropped.
487 * Big fat note: channels and events are invariant for the whole session after
488 * their creation. So this session destruction also destroys all channel and
489 * event structures specific to this session (they are not destroyed when their
490 * individual file is released).
493 int lttng_session_release(struct inode
*inode
, struct file
*file
)
495 struct lttng_session
*session
= file
->private_data
;
498 lttng_session_destroy(session
);
502 static const struct file_operations lttng_session_fops
= {
503 .owner
= THIS_MODULE
,
504 .release
= lttng_session_release
,
505 .unlocked_ioctl
= lttng_session_ioctl
,
507 .compat_ioctl
= lttng_session_ioctl
,
512 * lttng_metadata_ring_buffer_poll - LTTng ring buffer poll file operation
516 * Handles the poll operations for the metadata channels.
519 unsigned int lttng_metadata_ring_buffer_poll(struct file
*filp
,
522 struct lttng_metadata_stream
*stream
= filp
->private_data
;
523 struct lib_ring_buffer
*buf
= stream
->priv
;
525 unsigned int mask
= 0;
527 if (filp
->f_mode
& FMODE_READ
) {
528 poll_wait_set_exclusive(wait
);
529 poll_wait(filp
, &stream
->read_wait
, wait
);
531 finalized
= stream
->finalized
;
534 * lib_ring_buffer_is_finalized() contains a smp_rmb()
535 * ordering finalized load before offsets loads.
537 WARN_ON(atomic_long_read(&buf
->active_readers
) != 1);
542 if (stream
->metadata_cache
->metadata_written
>
543 stream
->metadata_out
)
551 int lttng_metadata_ring_buffer_ioctl_get_next_subbuf(struct file
*filp
,
552 unsigned int cmd
, unsigned long arg
)
554 struct lttng_metadata_stream
*stream
= filp
->private_data
;
555 struct lib_ring_buffer
*buf
= stream
->priv
;
556 struct channel
*chan
= buf
->backend
.chan
;
559 ret
= lttng_metadata_output_channel(stream
, chan
);
561 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
568 void lttng_metadata_ring_buffer_ioctl_put_next_subbuf(struct file
*filp
,
569 unsigned int cmd
, unsigned long arg
)
571 struct lttng_metadata_stream
*stream
= filp
->private_data
;
573 stream
->metadata_out
= stream
->metadata_in
;
577 long lttng_metadata_ring_buffer_ioctl(struct file
*filp
,
578 unsigned int cmd
, unsigned long arg
)
581 struct lttng_metadata_stream
*stream
= filp
->private_data
;
582 struct lib_ring_buffer
*buf
= stream
->priv
;
585 case RING_BUFFER_GET_NEXT_SUBBUF
:
587 ret
= lttng_metadata_ring_buffer_ioctl_get_next_subbuf(filp
,
593 case RING_BUFFER_GET_SUBBUF
:
596 * Random access is not allowed for metadata channel.
603 /* PUT_SUBBUF is the one from lib ring buffer, unmodified. */
605 /* Performing lib ring buffer ioctl after our own. */
606 ret
= lib_ring_buffer_ioctl(filp
, cmd
, arg
, buf
);
611 case RING_BUFFER_PUT_NEXT_SUBBUF
:
613 lttng_metadata_ring_buffer_ioctl_put_next_subbuf(filp
,
626 long lttng_metadata_ring_buffer_compat_ioctl(struct file
*filp
,
627 unsigned int cmd
, unsigned long arg
)
630 struct lttng_metadata_stream
*stream
= filp
->private_data
;
631 struct lib_ring_buffer
*buf
= stream
->priv
;
634 case RING_BUFFER_GET_NEXT_SUBBUF
:
636 ret
= lttng_metadata_ring_buffer_ioctl_get_next_subbuf(filp
,
642 case RING_BUFFER_GET_SUBBUF
:
645 * Random access is not allowed for metadata channel.
652 /* PUT_SUBBUF is the one from lib ring buffer, unmodified. */
654 /* Performing lib ring buffer ioctl after our own. */
655 ret
= lib_ring_buffer_compat_ioctl(filp
, cmd
, arg
, buf
);
660 case RING_BUFFER_PUT_NEXT_SUBBUF
:
662 lttng_metadata_ring_buffer_ioctl_put_next_subbuf(filp
,
675 * This is not used by anonymous file descriptors. This code is left
676 * there if we ever want to implement an inode with open() operation.
679 int lttng_metadata_ring_buffer_open(struct inode
*inode
, struct file
*file
)
681 struct lttng_metadata_stream
*stream
= inode
->i_private
;
682 struct lib_ring_buffer
*buf
= stream
->priv
;
684 file
->private_data
= buf
;
686 * Since life-time of metadata cache differs from that of
687 * session, we need to keep our own reference on the transport.
689 if (!try_module_get(stream
->transport
->owner
)) {
690 printk(KERN_WARNING
"LTT : Can't lock transport module.\n");
693 return lib_ring_buffer_open(inode
, file
, buf
);
697 int lttng_metadata_ring_buffer_release(struct inode
*inode
, struct file
*file
)
699 struct lttng_metadata_stream
*stream
= file
->private_data
;
700 struct lib_ring_buffer
*buf
= stream
->priv
;
702 kref_put(&stream
->metadata_cache
->refcount
, metadata_cache_destroy
);
703 module_put(stream
->transport
->owner
);
704 return lib_ring_buffer_release(inode
, file
, buf
);
708 ssize_t
lttng_metadata_ring_buffer_splice_read(struct file
*in
, loff_t
*ppos
,
709 struct pipe_inode_info
*pipe
, size_t len
,
712 struct lttng_metadata_stream
*stream
= in
->private_data
;
713 struct lib_ring_buffer
*buf
= stream
->priv
;
715 return lib_ring_buffer_splice_read(in
, ppos
, pipe
, len
,
720 int lttng_metadata_ring_buffer_mmap(struct file
*filp
,
721 struct vm_area_struct
*vma
)
723 struct lttng_metadata_stream
*stream
= filp
->private_data
;
724 struct lib_ring_buffer
*buf
= stream
->priv
;
726 return lib_ring_buffer_mmap(filp
, vma
, buf
);
730 const struct file_operations lttng_metadata_ring_buffer_file_operations
= {
731 .owner
= THIS_MODULE
,
732 .open
= lttng_metadata_ring_buffer_open
,
733 .release
= lttng_metadata_ring_buffer_release
,
734 .poll
= lttng_metadata_ring_buffer_poll
,
735 .splice_read
= lttng_metadata_ring_buffer_splice_read
,
736 .mmap
= lttng_metadata_ring_buffer_mmap
,
737 .unlocked_ioctl
= lttng_metadata_ring_buffer_ioctl
,
738 .llseek
= vfs_lib_ring_buffer_no_llseek
,
740 .compat_ioctl
= lttng_metadata_ring_buffer_compat_ioctl
,
745 int lttng_abi_create_stream_fd(struct file
*channel_file
, void *stream_priv
,
746 const struct file_operations
*fops
)
749 struct file
*stream_file
;
751 stream_fd
= get_unused_fd();
756 stream_file
= anon_inode_getfile("[lttng_stream]", fops
,
757 stream_priv
, O_RDWR
);
758 if (IS_ERR(stream_file
)) {
759 ret
= PTR_ERR(stream_file
);
763 * OPEN_FMODE, called within anon_inode_getfile/alloc_file, don't honor
764 * FMODE_LSEEK, FMODE_PREAD nor FMODE_PWRITE. We need to read from this
765 * file descriptor, so we set FMODE_PREAD here.
767 stream_file
->f_mode
|= FMODE_PREAD
;
768 fd_install(stream_fd
, stream_file
);
770 * The stream holds a reference to the channel within the generic ring
771 * buffer library, so no need to hold a refcount on the channel and
772 * session files here.
777 put_unused_fd(stream_fd
);
783 int lttng_abi_open_stream(struct file
*channel_file
)
785 struct lttng_channel
*channel
= channel_file
->private_data
;
786 struct lib_ring_buffer
*buf
;
790 buf
= channel
->ops
->buffer_read_open(channel
->chan
);
795 ret
= lttng_abi_create_stream_fd(channel_file
, stream_priv
,
796 <tng_stream_ring_buffer_file_operations
);
803 channel
->ops
->buffer_read_close(buf
);
808 int lttng_abi_open_metadata_stream(struct file
*channel_file
)
810 struct lttng_channel
*channel
= channel_file
->private_data
;
811 struct lttng_session
*session
= channel
->session
;
812 struct lib_ring_buffer
*buf
;
814 struct lttng_metadata_stream
*metadata_stream
;
817 buf
= channel
->ops
->buffer_read_open(channel
->chan
);
821 metadata_stream
= kzalloc(sizeof(struct lttng_metadata_stream
),
823 if (!metadata_stream
) {
827 metadata_stream
->metadata_cache
= session
->metadata_cache
;
828 init_waitqueue_head(&metadata_stream
->read_wait
);
829 metadata_stream
->priv
= buf
;
830 stream_priv
= metadata_stream
;
831 metadata_stream
->transport
= channel
->transport
;
834 * Since life-time of metadata cache differs from that of
835 * session, we need to keep our own reference on the transport.
837 if (!try_module_get(metadata_stream
->transport
->owner
)) {
838 printk(KERN_WARNING
"LTT : Can't lock transport module.\n");
843 ret
= lttng_abi_create_stream_fd(channel_file
, stream_priv
,
844 <tng_metadata_ring_buffer_file_operations
);
848 kref_get(&session
->metadata_cache
->refcount
);
849 list_add(&metadata_stream
->list
,
850 &session
->metadata_cache
->metadata_stream
);
854 module_put(metadata_stream
->transport
->owner
);
856 kfree(metadata_stream
);
858 channel
->ops
->buffer_read_close(buf
);
863 int lttng_abi_create_event(struct file
*channel_file
,
864 struct lttng_kernel_event
*event_param
)
866 struct lttng_channel
*channel
= channel_file
->private_data
;
867 struct lttng_event
*event
;
869 struct file
*event_file
;
871 event_param
->name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
872 switch (event_param
->instrumentation
) {
873 case LTTNG_KERNEL_KRETPROBE
:
874 event_param
->u
.kretprobe
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
876 case LTTNG_KERNEL_KPROBE
:
877 event_param
->u
.kprobe
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
879 case LTTNG_KERNEL_FUNCTION
:
880 event_param
->u
.ftrace
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
885 switch (event_param
->instrumentation
) {
887 event_fd
= get_unused_fd();
892 event_file
= anon_inode_getfile("[lttng_event]",
895 if (IS_ERR(event_file
)) {
896 ret
= PTR_ERR(event_file
);
900 * We tolerate no failure path after event creation. It
901 * will stay invariant for the rest of the session.
903 event
= lttng_event_create(channel
, event_param
, NULL
, NULL
);
908 event_file
->private_data
= event
;
909 fd_install(event_fd
, event_file
);
910 /* The event holds a reference on the channel */
911 atomic_long_inc(&channel_file
->f_count
);
913 case LTTNG_KERNEL_SYSCALL
:
915 * Only all-syscall tracing supported for now.
917 if (event_param
->name
[0] != '\0')
919 ret
= lttng_syscalls_register(channel
, NULL
);
930 put_unused_fd(event_fd
);
936 * lttng_channel_ioctl - lttng syscall through ioctl
942 * This ioctl implements lttng commands:
943 * LTTNG_KERNEL_STREAM
944 * Returns an event stream file descriptor or failure.
945 * (typically, one event stream records events from one CPU)
947 * Returns an event file descriptor or failure.
948 * LTTNG_KERNEL_CONTEXT
949 * Prepend a context field to each event in the channel
950 * LTTNG_KERNEL_ENABLE
951 * Enable recording for events in this channel (weak enable)
952 * LTTNG_KERNEL_DISABLE
953 * Disable recording for events in this channel (strong disable)
955 * Channel and event file descriptors also hold a reference on the session.
958 long lttng_channel_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
960 struct lttng_channel
*channel
= file
->private_data
;
963 case LTTNG_KERNEL_OLD_STREAM
:
964 case LTTNG_KERNEL_STREAM
:
965 return lttng_abi_open_stream(file
);
966 case LTTNG_KERNEL_OLD_EVENT
:
968 struct lttng_kernel_event
*uevent_param
;
969 struct lttng_kernel_old_event
*old_uevent_param
;
972 uevent_param
= kmalloc(sizeof(struct lttng_kernel_event
),
978 old_uevent_param
= kmalloc(
979 sizeof(struct lttng_kernel_old_event
),
981 if (!old_uevent_param
) {
983 goto old_event_error_free_param
;
985 if (copy_from_user(old_uevent_param
,
986 (struct lttng_kernel_old_event __user
*) arg
,
987 sizeof(struct lttng_kernel_old_event
))) {
989 goto old_event_error_free_old_param
;
992 memcpy(uevent_param
->name
, old_uevent_param
->name
,
993 sizeof(uevent_param
->name
));
994 uevent_param
->instrumentation
=
995 old_uevent_param
->instrumentation
;
997 switch (old_uevent_param
->instrumentation
) {
998 case LTTNG_KERNEL_KPROBE
:
999 uevent_param
->u
.kprobe
.addr
=
1000 old_uevent_param
->u
.kprobe
.addr
;
1001 uevent_param
->u
.kprobe
.offset
=
1002 old_uevent_param
->u
.kprobe
.offset
;
1003 memcpy(uevent_param
->u
.kprobe
.symbol_name
,
1004 old_uevent_param
->u
.kprobe
.symbol_name
,
1005 sizeof(uevent_param
->u
.kprobe
.symbol_name
));
1007 case LTTNG_KERNEL_KRETPROBE
:
1008 uevent_param
->u
.kretprobe
.addr
=
1009 old_uevent_param
->u
.kretprobe
.addr
;
1010 uevent_param
->u
.kretprobe
.offset
=
1011 old_uevent_param
->u
.kretprobe
.offset
;
1012 memcpy(uevent_param
->u
.kretprobe
.symbol_name
,
1013 old_uevent_param
->u
.kretprobe
.symbol_name
,
1014 sizeof(uevent_param
->u
.kretprobe
.symbol_name
));
1016 case LTTNG_KERNEL_FUNCTION
:
1017 memcpy(uevent_param
->u
.ftrace
.symbol_name
,
1018 old_uevent_param
->u
.ftrace
.symbol_name
,
1019 sizeof(uevent_param
->u
.ftrace
.symbol_name
));
1024 ret
= lttng_abi_create_event(file
, uevent_param
);
1026 old_event_error_free_old_param
:
1027 kfree(old_uevent_param
);
1028 old_event_error_free_param
:
1029 kfree(uevent_param
);
1033 case LTTNG_KERNEL_EVENT
:
1035 struct lttng_kernel_event uevent_param
;
1037 if (copy_from_user(&uevent_param
,
1038 (struct lttng_kernel_event __user
*) arg
,
1039 sizeof(uevent_param
)))
1041 return lttng_abi_create_event(file
, &uevent_param
);
1043 case LTTNG_KERNEL_OLD_CONTEXT
:
1045 struct lttng_kernel_context
*ucontext_param
;
1046 struct lttng_kernel_old_context
*old_ucontext_param
;
1049 ucontext_param
= kmalloc(sizeof(struct lttng_kernel_context
),
1051 if (!ucontext_param
) {
1055 old_ucontext_param
= kmalloc(sizeof(struct lttng_kernel_old_context
),
1057 if (!old_ucontext_param
) {
1059 goto old_ctx_error_free_param
;
1062 if (copy_from_user(old_ucontext_param
,
1063 (struct lttng_kernel_old_context __user
*) arg
,
1064 sizeof(struct lttng_kernel_old_context
))) {
1066 goto old_ctx_error_free_old_param
;
1068 ucontext_param
->ctx
= old_ucontext_param
->ctx
;
1069 memcpy(ucontext_param
->padding
, old_ucontext_param
->padding
,
1070 sizeof(ucontext_param
->padding
));
1071 /* only type that uses the union */
1072 if (old_ucontext_param
->ctx
== LTTNG_KERNEL_CONTEXT_PERF_COUNTER
) {
1073 ucontext_param
->u
.perf_counter
.type
=
1074 old_ucontext_param
->u
.perf_counter
.type
;
1075 ucontext_param
->u
.perf_counter
.config
=
1076 old_ucontext_param
->u
.perf_counter
.config
;
1077 memcpy(ucontext_param
->u
.perf_counter
.name
,
1078 old_ucontext_param
->u
.perf_counter
.name
,
1079 sizeof(ucontext_param
->u
.perf_counter
.name
));
1082 ret
= lttng_abi_add_context(file
,
1084 &channel
->ctx
, channel
->session
);
1086 old_ctx_error_free_old_param
:
1087 kfree(old_ucontext_param
);
1088 old_ctx_error_free_param
:
1089 kfree(ucontext_param
);
1093 case LTTNG_KERNEL_CONTEXT
:
1095 struct lttng_kernel_context ucontext_param
;
1097 if (copy_from_user(&ucontext_param
,
1098 (struct lttng_kernel_context __user
*) arg
,
1099 sizeof(ucontext_param
)))
1101 return lttng_abi_add_context(file
,
1103 &channel
->ctx
, channel
->session
);
1105 case LTTNG_KERNEL_OLD_ENABLE
:
1106 case LTTNG_KERNEL_ENABLE
:
1107 return lttng_channel_enable(channel
);
1108 case LTTNG_KERNEL_OLD_DISABLE
:
1109 case LTTNG_KERNEL_DISABLE
:
1110 return lttng_channel_disable(channel
);
1112 return -ENOIOCTLCMD
;
1118 * lttng_metadata_ioctl - lttng syscall through ioctl
1124 * This ioctl implements lttng commands:
1125 * LTTNG_KERNEL_STREAM
1126 * Returns an event stream file descriptor or failure.
1128 * Channel and event file descriptors also hold a reference on the session.
1131 long lttng_metadata_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1134 case LTTNG_KERNEL_OLD_STREAM
:
1135 case LTTNG_KERNEL_STREAM
:
1136 return lttng_abi_open_metadata_stream(file
);
1138 return -ENOIOCTLCMD
;
1143 * lttng_channel_poll - lttng stream addition/removal monitoring
1148 unsigned int lttng_channel_poll(struct file
*file
, poll_table
*wait
)
1150 struct lttng_channel
*channel
= file
->private_data
;
1151 unsigned int mask
= 0;
1153 if (file
->f_mode
& FMODE_READ
) {
1154 poll_wait_set_exclusive(wait
);
1155 poll_wait(file
, channel
->ops
->get_hp_wait_queue(channel
->chan
),
1158 if (channel
->ops
->is_disabled(channel
->chan
))
1160 if (channel
->ops
->is_finalized(channel
->chan
))
1162 if (channel
->ops
->buffer_has_read_closed_stream(channel
->chan
))
1163 return POLLIN
| POLLRDNORM
;
1171 int lttng_channel_release(struct inode
*inode
, struct file
*file
)
1173 struct lttng_channel
*channel
= file
->private_data
;
1176 fput(channel
->session
->file
);
1181 int lttng_metadata_channel_release(struct inode
*inode
, struct file
*file
)
1183 struct lttng_channel
*channel
= file
->private_data
;
1186 lttng_metadata_channel_destroy(channel
);
1187 fput(channel
->session
->file
);
1193 static const struct file_operations lttng_channel_fops
= {
1194 .owner
= THIS_MODULE
,
1195 .release
= lttng_channel_release
,
1196 .poll
= lttng_channel_poll
,
1197 .unlocked_ioctl
= lttng_channel_ioctl
,
1198 #ifdef CONFIG_COMPAT
1199 .compat_ioctl
= lttng_channel_ioctl
,
1203 static const struct file_operations lttng_metadata_fops
= {
1204 .owner
= THIS_MODULE
,
1205 .release
= lttng_metadata_channel_release
,
1206 .unlocked_ioctl
= lttng_metadata_ioctl
,
1207 #ifdef CONFIG_COMPAT
1208 .compat_ioctl
= lttng_metadata_ioctl
,
1213 * lttng_event_ioctl - lttng syscall through ioctl
1219 * This ioctl implements lttng commands:
1220 * LTTNG_KERNEL_CONTEXT
1221 * Prepend a context field to each record of this event
1222 * LTTNG_KERNEL_ENABLE
1223 * Enable recording for this event (weak enable)
1224 * LTTNG_KERNEL_DISABLE
1225 * Disable recording for this event (strong disable)
1228 long lttng_event_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1230 struct lttng_event
*event
= file
->private_data
;
1233 case LTTNG_KERNEL_OLD_CONTEXT
:
1235 struct lttng_kernel_context
*ucontext_param
;
1236 struct lttng_kernel_old_context
*old_ucontext_param
;
1239 ucontext_param
= kmalloc(sizeof(struct lttng_kernel_context
),
1241 if (!ucontext_param
) {
1245 old_ucontext_param
= kmalloc(sizeof(struct lttng_kernel_old_context
),
1247 if (!old_ucontext_param
) {
1249 goto old_ctx_error_free_param
;
1252 if (copy_from_user(old_ucontext_param
,
1253 (struct lttng_kernel_old_context __user
*) arg
,
1254 sizeof(struct lttng_kernel_old_context
))) {
1256 goto old_ctx_error_free_old_param
;
1258 ucontext_param
->ctx
= old_ucontext_param
->ctx
;
1259 memcpy(ucontext_param
->padding
, old_ucontext_param
->padding
,
1260 sizeof(ucontext_param
->padding
));
1261 /* only type that uses the union */
1262 if (old_ucontext_param
->ctx
== LTTNG_KERNEL_CONTEXT_PERF_COUNTER
) {
1263 ucontext_param
->u
.perf_counter
.type
=
1264 old_ucontext_param
->u
.perf_counter
.type
;
1265 ucontext_param
->u
.perf_counter
.config
=
1266 old_ucontext_param
->u
.perf_counter
.config
;
1267 memcpy(ucontext_param
->u
.perf_counter
.name
,
1268 old_ucontext_param
->u
.perf_counter
.name
,
1269 sizeof(ucontext_param
->u
.perf_counter
.name
));
1272 ret
= lttng_abi_add_context(file
,
1274 &event
->ctx
, event
->chan
->session
);
1276 old_ctx_error_free_old_param
:
1277 kfree(old_ucontext_param
);
1278 old_ctx_error_free_param
:
1279 kfree(ucontext_param
);
1283 case LTTNG_KERNEL_CONTEXT
:
1285 struct lttng_kernel_context ucontext_param
;
1287 if (copy_from_user(&ucontext_param
,
1288 (struct lttng_kernel_context __user
*) arg
,
1289 sizeof(ucontext_param
)))
1291 return lttng_abi_add_context(file
,
1293 &event
->ctx
, event
->chan
->session
);
1295 case LTTNG_KERNEL_OLD_ENABLE
:
1296 case LTTNG_KERNEL_ENABLE
:
1297 return lttng_event_enable(event
);
1298 case LTTNG_KERNEL_OLD_DISABLE
:
1299 case LTTNG_KERNEL_DISABLE
:
1300 return lttng_event_disable(event
);
1302 return -ENOIOCTLCMD
;
1307 int lttng_event_release(struct inode
*inode
, struct file
*file
)
1309 struct lttng_event
*event
= file
->private_data
;
1312 fput(event
->chan
->file
);
1316 /* TODO: filter control ioctl */
1317 static const struct file_operations lttng_event_fops
= {
1318 .owner
= THIS_MODULE
,
1319 .release
= lttng_event_release
,
1320 .unlocked_ioctl
= lttng_event_ioctl
,
1321 #ifdef CONFIG_COMPAT
1322 .compat_ioctl
= lttng_event_ioctl
,
1326 static long lttng_stream_ring_buffer_ioctl(struct file
*filp
,
1327 unsigned int cmd
, unsigned long arg
)
1331 return lib_ring_buffer_file_operations
.unlocked_ioctl(filp
,
1336 #ifdef CONFIG_COMPAT
1337 static long lttng_stream_ring_buffer_compat_ioctl(struct file
*filp
,
1338 unsigned int cmd
, unsigned long arg
)
1342 return lib_ring_buffer_file_operations
.compat_ioctl(filp
,
1346 #endif /* CONFIG_COMPAT */
1348 static void lttng_stream_override_ring_buffer_fops(void)
1350 lttng_stream_ring_buffer_file_operations
.owner
= THIS_MODULE
;
1351 lttng_stream_ring_buffer_file_operations
.open
=
1352 lib_ring_buffer_file_operations
.open
;
1353 lttng_stream_ring_buffer_file_operations
.release
=
1354 lib_ring_buffer_file_operations
.release
;
1355 lttng_stream_ring_buffer_file_operations
.poll
=
1356 lib_ring_buffer_file_operations
.poll
;
1357 lttng_stream_ring_buffer_file_operations
.splice_read
=
1358 lib_ring_buffer_file_operations
.splice_read
;
1359 lttng_stream_ring_buffer_file_operations
.mmap
=
1360 lib_ring_buffer_file_operations
.mmap
;
1361 lttng_stream_ring_buffer_file_operations
.unlocked_ioctl
=
1362 lttng_stream_ring_buffer_ioctl
;
1363 lttng_stream_ring_buffer_file_operations
.llseek
=
1364 lib_ring_buffer_file_operations
.llseek
;
1365 #ifdef CONFIG_COMPAT
1366 lttng_stream_ring_buffer_file_operations
.compat_ioctl
=
1367 lttng_stream_ring_buffer_compat_ioctl
;
1371 int __init
lttng_abi_init(void)
1375 wrapper_vmalloc_sync_all();
1376 lttng_proc_dentry
= proc_create_data("lttng", S_IRUSR
| S_IWUSR
, NULL
,
1379 if (!lttng_proc_dentry
) {
1380 printk(KERN_ERR
"Error creating LTTng control file\n");
1384 lttng_stream_override_ring_buffer_fops();
1390 void __exit
lttng_abi_exit(void)
1392 if (lttng_proc_dentry
)
1393 remove_proc_entry("lttng", NULL
);