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
;
69 * Teardown management: opened file descriptors keep a refcount on the module,
70 * so it can only exit when all file descriptors are closed.
74 int lttng_abi_create_session(void)
76 struct lttng_session
*session
;
77 struct file
*session_file
;
80 session
= lttng_session_create();
83 session_fd
= get_unused_fd();
88 session_file
= anon_inode_getfile("[lttng_session]",
91 if (IS_ERR(session_file
)) {
92 ret
= PTR_ERR(session_file
);
95 session
->file
= session_file
;
96 fd_install(session_fd
, session_file
);
100 put_unused_fd(session_fd
);
102 lttng_session_destroy(session
);
107 int lttng_abi_tracepoint_list(void)
109 struct file
*tracepoint_list_file
;
112 file_fd
= get_unused_fd();
118 tracepoint_list_file
= anon_inode_getfile("[lttng_session]",
119 <tng_tracepoint_list_fops
,
121 if (IS_ERR(tracepoint_list_file
)) {
122 ret
= PTR_ERR(tracepoint_list_file
);
125 ret
= lttng_tracepoint_list_fops
.open(NULL
, tracepoint_list_file
);
128 fd_install(file_fd
, tracepoint_list_file
);
136 fput(tracepoint_list_file
);
138 put_unused_fd(file_fd
);
144 void lttng_abi_tracer_version(struct lttng_kernel_tracer_version
*v
)
146 v
->major
= LTTNG_MODULES_MAJOR_VERSION
;
147 v
->minor
= LTTNG_MODULES_MINOR_VERSION
;
148 v
->patchlevel
= LTTNG_MODULES_PATCHLEVEL_VERSION
;
152 long lttng_abi_add_context(struct file
*file
,
153 struct lttng_kernel_context
*context_param
,
154 struct lttng_ctx
**ctx
, struct lttng_session
*session
)
157 if (session
->been_active
)
160 switch (context_param
->ctx
) {
161 case LTTNG_KERNEL_CONTEXT_PID
:
162 return lttng_add_pid_to_ctx(ctx
);
163 case LTTNG_KERNEL_CONTEXT_PRIO
:
164 return lttng_add_prio_to_ctx(ctx
);
165 case LTTNG_KERNEL_CONTEXT_NICE
:
166 return lttng_add_nice_to_ctx(ctx
);
167 case LTTNG_KERNEL_CONTEXT_VPID
:
168 return lttng_add_vpid_to_ctx(ctx
);
169 case LTTNG_KERNEL_CONTEXT_TID
:
170 return lttng_add_tid_to_ctx(ctx
);
171 case LTTNG_KERNEL_CONTEXT_VTID
:
172 return lttng_add_vtid_to_ctx(ctx
);
173 case LTTNG_KERNEL_CONTEXT_PPID
:
174 return lttng_add_ppid_to_ctx(ctx
);
175 case LTTNG_KERNEL_CONTEXT_VPPID
:
176 return lttng_add_vppid_to_ctx(ctx
);
177 case LTTNG_KERNEL_CONTEXT_PERF_COUNTER
:
178 context_param
->u
.perf_counter
.name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
179 return lttng_add_perf_counter_to_ctx(context_param
->u
.perf_counter
.type
,
180 context_param
->u
.perf_counter
.config
,
181 context_param
->u
.perf_counter
.name
,
183 case LTTNG_KERNEL_CONTEXT_PROCNAME
:
184 return lttng_add_procname_to_ctx(ctx
);
185 case LTTNG_KERNEL_CONTEXT_HOSTNAME
:
186 return lttng_add_hostname_to_ctx(ctx
);
193 * lttng_ioctl - lttng syscall through ioctl
199 * This ioctl implements lttng commands:
200 * LTTNG_KERNEL_SESSION
201 * Returns a LTTng trace session file descriptor
202 * LTTNG_KERNEL_TRACER_VERSION
203 * Returns the LTTng kernel tracer version
204 * LTTNG_KERNEL_TRACEPOINT_LIST
205 * Returns a file descriptor listing available tracepoints
206 * LTTNG_KERNEL_WAIT_QUIESCENT
207 * Returns after all previously running probes have completed
209 * The returned session will be deleted when its file descriptor is closed.
212 long lttng_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
215 case LTTNG_KERNEL_OLD_SESSION
:
216 case LTTNG_KERNEL_SESSION
:
217 return lttng_abi_create_session();
218 case LTTNG_KERNEL_OLD_TRACER_VERSION
:
220 struct lttng_kernel_tracer_version v
;
221 struct lttng_kernel_old_tracer_version oldv
;
222 struct lttng_kernel_old_tracer_version
*uversion
=
223 (struct lttng_kernel_old_tracer_version __user
*) arg
;
225 lttng_abi_tracer_version(&v
);
226 oldv
.major
= v
.major
;
227 oldv
.minor
= v
.minor
;
228 oldv
.patchlevel
= v
.patchlevel
;
230 if (copy_to_user(uversion
, &oldv
, sizeof(oldv
)))
234 case LTTNG_KERNEL_TRACER_VERSION
:
236 struct lttng_kernel_tracer_version version
;
237 struct lttng_kernel_tracer_version
*uversion
=
238 (struct lttng_kernel_tracer_version __user
*) arg
;
240 lttng_abi_tracer_version(&version
);
242 if (copy_to_user(uversion
, &version
, sizeof(version
)))
246 case LTTNG_KERNEL_OLD_TRACEPOINT_LIST
:
247 case LTTNG_KERNEL_TRACEPOINT_LIST
:
248 return lttng_abi_tracepoint_list();
249 case LTTNG_KERNEL_OLD_WAIT_QUIESCENT
:
250 case LTTNG_KERNEL_WAIT_QUIESCENT
:
253 case LTTNG_KERNEL_OLD_CALIBRATE
:
255 struct lttng_kernel_old_calibrate __user
*ucalibrate
=
256 (struct lttng_kernel_old_calibrate __user
*) arg
;
257 struct lttng_kernel_old_calibrate old_calibrate
;
258 struct lttng_kernel_calibrate calibrate
;
261 if (copy_from_user(&old_calibrate
, ucalibrate
, sizeof(old_calibrate
)))
263 calibrate
.type
= old_calibrate
.type
;
264 ret
= lttng_calibrate(&calibrate
);
265 if (copy_to_user(ucalibrate
, &old_calibrate
, sizeof(old_calibrate
)))
269 case LTTNG_KERNEL_CALIBRATE
:
271 struct lttng_kernel_calibrate __user
*ucalibrate
=
272 (struct lttng_kernel_calibrate __user
*) arg
;
273 struct lttng_kernel_calibrate calibrate
;
276 if (copy_from_user(&calibrate
, ucalibrate
, sizeof(calibrate
)))
278 ret
= lttng_calibrate(&calibrate
);
279 if (copy_to_user(ucalibrate
, &calibrate
, sizeof(calibrate
)))
288 static const struct file_operations lttng_fops
= {
289 .owner
= THIS_MODULE
,
290 .unlocked_ioctl
= lttng_ioctl
,
292 .compat_ioctl
= lttng_ioctl
,
297 int lttng_abi_create_channel(struct file
*session_file
,
298 struct lttng_kernel_channel
*chan_param
,
299 enum channel_type channel_type
)
301 struct lttng_session
*session
= session_file
->private_data
;
302 const struct file_operations
*fops
= NULL
;
303 const char *transport_name
;
304 struct lttng_channel
*chan
;
305 struct file
*chan_file
;
309 chan_fd
= get_unused_fd();
314 switch (channel_type
) {
315 case PER_CPU_CHANNEL
:
316 fops
= <tng_channel_fops
;
318 case METADATA_CHANNEL
:
319 fops
= <tng_metadata_fops
;
323 chan_file
= anon_inode_getfile("[lttng_channel]",
326 if (IS_ERR(chan_file
)) {
327 ret
= PTR_ERR(chan_file
);
330 switch (channel_type
) {
331 case PER_CPU_CHANNEL
:
332 if (chan_param
->output
== LTTNG_KERNEL_SPLICE
) {
333 transport_name
= chan_param
->overwrite
?
334 "relay-overwrite" : "relay-discard";
335 } else if (chan_param
->output
== LTTNG_KERNEL_MMAP
) {
336 transport_name
= chan_param
->overwrite
?
337 "relay-overwrite-mmap" : "relay-discard-mmap";
342 case METADATA_CHANNEL
:
343 if (chan_param
->output
== LTTNG_KERNEL_SPLICE
)
344 transport_name
= "relay-metadata";
345 else if (chan_param
->output
== LTTNG_KERNEL_MMAP
)
346 transport_name
= "relay-metadata-mmap";
351 transport_name
= "<unknown>";
355 * We tolerate no failure path after channel creation. It will stay
356 * invariant for the rest of the session.
358 chan
= lttng_channel_create(session
, transport_name
, NULL
,
359 chan_param
->subbuf_size
,
360 chan_param
->num_subbuf
,
361 chan_param
->switch_timer_interval
,
362 chan_param
->read_timer_interval
,
368 chan
->file
= chan_file
;
369 chan_file
->private_data
= chan
;
370 fd_install(chan_fd
, chan_file
);
371 atomic_long_inc(&session_file
->f_count
);
378 put_unused_fd(chan_fd
);
384 * lttng_session_ioctl - lttng session fd ioctl
390 * This ioctl implements lttng commands:
391 * LTTNG_KERNEL_CHANNEL
392 * Returns a LTTng channel file descriptor
393 * LTTNG_KERNEL_ENABLE
394 * Enables tracing for a session (weak enable)
395 * LTTNG_KERNEL_DISABLE
396 * Disables tracing for a session (strong disable)
397 * LTTNG_KERNEL_METADATA
398 * Returns a LTTng metadata file descriptor
400 * The returned channel will be deleted when its file descriptor is closed.
403 long lttng_session_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
405 struct lttng_session
*session
= file
->private_data
;
408 case LTTNG_KERNEL_OLD_CHANNEL
:
410 struct lttng_kernel_channel chan_param
;
411 struct lttng_kernel_old_channel old_chan_param
;
413 if (copy_from_user(&old_chan_param
,
414 (struct lttng_kernel_old_channel __user
*) arg
,
415 sizeof(struct lttng_kernel_old_channel
)))
417 chan_param
.overwrite
= old_chan_param
.overwrite
;
418 chan_param
.subbuf_size
= old_chan_param
.subbuf_size
;
419 chan_param
.num_subbuf
= old_chan_param
.num_subbuf
;
420 chan_param
.switch_timer_interval
= old_chan_param
.switch_timer_interval
;
421 chan_param
.read_timer_interval
= old_chan_param
.read_timer_interval
;
422 chan_param
.output
= old_chan_param
.output
;
424 return lttng_abi_create_channel(file
, &chan_param
,
427 case LTTNG_KERNEL_CHANNEL
:
429 struct lttng_kernel_channel chan_param
;
431 if (copy_from_user(&chan_param
,
432 (struct lttng_kernel_channel __user
*) arg
,
433 sizeof(struct lttng_kernel_channel
)))
435 return lttng_abi_create_channel(file
, &chan_param
,
438 case LTTNG_KERNEL_OLD_SESSION_START
:
439 case LTTNG_KERNEL_OLD_ENABLE
:
440 case LTTNG_KERNEL_SESSION_START
:
441 case LTTNG_KERNEL_ENABLE
:
442 return lttng_session_enable(session
);
443 case LTTNG_KERNEL_OLD_SESSION_STOP
:
444 case LTTNG_KERNEL_OLD_DISABLE
:
445 case LTTNG_KERNEL_SESSION_STOP
:
446 case LTTNG_KERNEL_DISABLE
:
447 return lttng_session_disable(session
);
448 case LTTNG_KERNEL_OLD_METADATA
:
450 struct lttng_kernel_channel chan_param
;
451 struct lttng_kernel_old_channel old_chan_param
;
453 if (copy_from_user(&old_chan_param
,
454 (struct lttng_kernel_old_channel __user
*) arg
,
455 sizeof(struct lttng_kernel_old_channel
)))
457 chan_param
.overwrite
= old_chan_param
.overwrite
;
458 chan_param
.subbuf_size
= old_chan_param
.subbuf_size
;
459 chan_param
.num_subbuf
= old_chan_param
.num_subbuf
;
460 chan_param
.switch_timer_interval
= old_chan_param
.switch_timer_interval
;
461 chan_param
.read_timer_interval
= old_chan_param
.read_timer_interval
;
462 chan_param
.output
= old_chan_param
.output
;
464 return lttng_abi_create_channel(file
, &chan_param
,
467 case LTTNG_KERNEL_METADATA
:
469 struct lttng_kernel_channel chan_param
;
471 if (copy_from_user(&chan_param
,
472 (struct lttng_kernel_channel __user
*) arg
,
473 sizeof(struct lttng_kernel_channel
)))
475 return lttng_abi_create_channel(file
, &chan_param
,
484 * Called when the last file reference is dropped.
486 * Big fat note: channels and events are invariant for the whole session after
487 * their creation. So this session destruction also destroys all channel and
488 * event structures specific to this session (they are not destroyed when their
489 * individual file is released).
492 int lttng_session_release(struct inode
*inode
, struct file
*file
)
494 struct lttng_session
*session
= file
->private_data
;
497 lttng_session_destroy(session
);
501 static const struct file_operations lttng_session_fops
= {
502 .owner
= THIS_MODULE
,
503 .release
= lttng_session_release
,
504 .unlocked_ioctl
= lttng_session_ioctl
,
506 .compat_ioctl
= lttng_session_ioctl
,
511 * lttng_metadata_ring_buffer_poll - LTTng ring buffer poll file operation
515 * Handles the poll operations for the metadata channels.
518 unsigned int lttng_metadata_ring_buffer_poll(struct file
*filp
,
521 struct lttng_metadata_stream
*stream
= filp
->private_data
;
522 struct lib_ring_buffer
*buf
= stream
->priv
;
524 unsigned int mask
= 0;
526 if (filp
->f_mode
& FMODE_READ
) {
527 poll_wait_set_exclusive(wait
);
528 poll_wait(filp
, &stream
->read_wait
, wait
);
530 finalized
= stream
->finalized
;
533 * lib_ring_buffer_is_finalized() contains a smp_rmb()
534 * ordering finalized load before offsets loads.
536 WARN_ON(atomic_long_read(&buf
->active_readers
) != 1);
541 if (stream
->metadata_cache
->metadata_written
>
542 stream
->metadata_out
)
550 int lttng_metadata_ring_buffer_ioctl_get_next_subbuf(struct file
*filp
,
551 unsigned int cmd
, unsigned long arg
)
553 struct lttng_metadata_stream
*stream
= filp
->private_data
;
554 struct lib_ring_buffer
*buf
= stream
->priv
;
555 struct channel
*chan
= buf
->backend
.chan
;
556 struct lttng_channel
*lttng_chan
= channel_get_private(chan
);
559 ret
= lttng_metadata_output_channel(lttng_chan
, stream
);
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 int lttng_metadata_ring_buffer_open(struct inode
*inode
, struct file
*file
)
677 struct lttng_metadata_stream
*stream
= inode
->i_private
;
678 struct lib_ring_buffer
*buf
= stream
->priv
;
680 file
->private_data
= buf
;
681 return lib_ring_buffer_open(inode
, file
, buf
);
685 int lttng_metadata_ring_buffer_release(struct inode
*inode
, struct file
*file
)
687 struct lttng_metadata_stream
*stream
= file
->private_data
;
688 struct lib_ring_buffer
*buf
= stream
->priv
;
689 struct channel
*chan
= buf
->backend
.chan
;
690 struct lttng_channel
*lttng_chan
= channel_get_private(chan
);
692 kref_put(&stream
->metadata_cache
->refcount
, metadata_cache_destroy
);
693 fput(lttng_chan
->file
);
695 return lib_ring_buffer_release(inode
, file
, buf
);
699 ssize_t
lttng_metadata_ring_buffer_splice_read(struct file
*in
, loff_t
*ppos
,
700 struct pipe_inode_info
*pipe
, size_t len
,
703 struct lttng_metadata_stream
*stream
= in
->private_data
;
704 struct lib_ring_buffer
*buf
= stream
->priv
;
706 return lib_ring_buffer_splice_read(in
, ppos
, pipe
, len
,
711 int lttng_metadata_ring_buffer_mmap(struct file
*filp
,
712 struct vm_area_struct
*vma
)
714 struct lttng_metadata_stream
*stream
= filp
->private_data
;
715 struct lib_ring_buffer
*buf
= stream
->priv
;
717 return lib_ring_buffer_mmap(filp
, vma
, buf
);
721 const struct file_operations lttng_metadata_ring_buffer_file_operations
= {
722 .owner
= THIS_MODULE
,
723 .open
= lttng_metadata_ring_buffer_open
,
724 .release
= lttng_metadata_ring_buffer_release
,
725 .poll
= lttng_metadata_ring_buffer_poll
,
726 .splice_read
= lttng_metadata_ring_buffer_splice_read
,
727 .mmap
= lttng_metadata_ring_buffer_mmap
,
728 .unlocked_ioctl
= lttng_metadata_ring_buffer_ioctl
,
729 .llseek
= vfs_lib_ring_buffer_no_llseek
,
731 .compat_ioctl
= lttng_metadata_ring_buffer_compat_ioctl
,
736 int lttng_abi_create_stream_fd(struct file
*channel_file
, void *stream_priv
,
737 const struct file_operations
*fops
)
740 struct file
*stream_file
;
742 stream_fd
= get_unused_fd();
747 stream_file
= anon_inode_getfile("[lttng_stream]", fops
,
748 stream_priv
, O_RDWR
);
749 if (IS_ERR(stream_file
)) {
750 ret
= PTR_ERR(stream_file
);
754 * OPEN_FMODE, called within anon_inode_getfile/alloc_file, don't honor
755 * FMODE_LSEEK, FMODE_PREAD nor FMODE_PWRITE. We need to read from this
756 * file descriptor, so we set FMODE_PREAD here.
758 stream_file
->f_mode
|= FMODE_PREAD
;
759 fd_install(stream_fd
, stream_file
);
761 * The stream holds a reference to the channel within the generic ring
762 * buffer library, so no need to hold a refcount on the channel and
763 * session files here.
768 put_unused_fd(stream_fd
);
774 int lttng_abi_open_stream(struct file
*channel_file
)
776 struct lttng_channel
*channel
= channel_file
->private_data
;
777 struct lib_ring_buffer
*buf
;
781 buf
= channel
->ops
->buffer_read_open(channel
->chan
);
786 ret
= lttng_abi_create_stream_fd(channel_file
, stream_priv
,
787 &lib_ring_buffer_file_operations
);
794 channel
->ops
->buffer_read_close(buf
);
799 int lttng_abi_open_metadata_stream(struct file
*channel_file
)
801 struct lttng_channel
*channel
= channel_file
->private_data
;
802 struct lttng_session
*session
= channel
->session
;
803 struct lib_ring_buffer
*buf
;
805 struct lttng_metadata_stream
*metadata_stream
;
808 buf
= channel
->ops
->buffer_read_open(channel
->chan
);
812 metadata_stream
= kzalloc(sizeof(struct lttng_metadata_stream
),
814 if (!metadata_stream
)
816 metadata_stream
->metadata_cache
= session
->metadata_cache
;
817 init_waitqueue_head(&metadata_stream
->read_wait
);
818 metadata_stream
->priv
= buf
;
819 stream_priv
= metadata_stream
;
820 ret
= lttng_abi_create_stream_fd(channel_file
, stream_priv
,
821 <tng_metadata_ring_buffer_file_operations
);
825 atomic_long_inc(&channel_file
->f_count
);
826 kref_get(&session
->metadata_cache
->refcount
);
827 list_add(&metadata_stream
->list
,
828 &session
->metadata_cache
->metadata_stream
);
832 channel
->ops
->buffer_read_close(buf
);
837 int lttng_abi_create_event(struct file
*channel_file
,
838 struct lttng_kernel_event
*event_param
)
840 struct lttng_channel
*channel
= channel_file
->private_data
;
841 struct lttng_event
*event
;
843 struct file
*event_file
;
845 event_param
->name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
846 switch (event_param
->instrumentation
) {
847 case LTTNG_KERNEL_KRETPROBE
:
848 event_param
->u
.kretprobe
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
850 case LTTNG_KERNEL_KPROBE
:
851 event_param
->u
.kprobe
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
853 case LTTNG_KERNEL_FUNCTION
:
854 event_param
->u
.ftrace
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
859 switch (event_param
->instrumentation
) {
861 event_fd
= get_unused_fd();
866 event_file
= anon_inode_getfile("[lttng_event]",
869 if (IS_ERR(event_file
)) {
870 ret
= PTR_ERR(event_file
);
874 * We tolerate no failure path after event creation. It
875 * will stay invariant for the rest of the session.
877 event
= lttng_event_create(channel
, event_param
, NULL
, NULL
);
882 event_file
->private_data
= event
;
883 fd_install(event_fd
, event_file
);
884 /* The event holds a reference on the channel */
885 atomic_long_inc(&channel_file
->f_count
);
887 case LTTNG_KERNEL_SYSCALL
:
889 * Only all-syscall tracing supported for now.
891 if (event_param
->name
[0] != '\0')
893 ret
= lttng_syscalls_register(channel
, NULL
);
904 put_unused_fd(event_fd
);
910 * lttng_channel_ioctl - lttng syscall through ioctl
916 * This ioctl implements lttng commands:
917 * LTTNG_KERNEL_STREAM
918 * Returns an event stream file descriptor or failure.
919 * (typically, one event stream records events from one CPU)
921 * Returns an event file descriptor or failure.
922 * LTTNG_KERNEL_CONTEXT
923 * Prepend a context field to each event in the channel
924 * LTTNG_KERNEL_ENABLE
925 * Enable recording for events in this channel (weak enable)
926 * LTTNG_KERNEL_DISABLE
927 * Disable recording for events in this channel (strong disable)
929 * Channel and event file descriptors also hold a reference on the session.
932 long lttng_channel_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
934 struct lttng_channel
*channel
= file
->private_data
;
937 case LTTNG_KERNEL_OLD_STREAM
:
938 case LTTNG_KERNEL_STREAM
:
939 return lttng_abi_open_stream(file
);
940 case LTTNG_KERNEL_OLD_EVENT
:
942 struct lttng_kernel_event
*uevent_param
;
943 struct lttng_kernel_old_event
*old_uevent_param
;
946 uevent_param
= kmalloc(sizeof(struct lttng_kernel_event
),
952 old_uevent_param
= kmalloc(
953 sizeof(struct lttng_kernel_old_event
),
955 if (!old_uevent_param
) {
957 goto old_event_error_free_param
;
959 if (copy_from_user(old_uevent_param
,
960 (struct lttng_kernel_old_event __user
*) arg
,
961 sizeof(struct lttng_kernel_old_event
))) {
963 goto old_event_error_free_old_param
;
966 memcpy(uevent_param
->name
, old_uevent_param
->name
,
967 sizeof(uevent_param
->name
));
968 uevent_param
->instrumentation
=
969 old_uevent_param
->instrumentation
;
971 switch (old_uevent_param
->instrumentation
) {
972 case LTTNG_KERNEL_KPROBE
:
973 uevent_param
->u
.kprobe
.addr
=
974 old_uevent_param
->u
.kprobe
.addr
;
975 uevent_param
->u
.kprobe
.offset
=
976 old_uevent_param
->u
.kprobe
.offset
;
977 memcpy(uevent_param
->u
.kprobe
.symbol_name
,
978 old_uevent_param
->u
.kprobe
.symbol_name
,
979 sizeof(uevent_param
->u
.kprobe
.symbol_name
));
981 case LTTNG_KERNEL_KRETPROBE
:
982 uevent_param
->u
.kretprobe
.addr
=
983 old_uevent_param
->u
.kretprobe
.addr
;
984 uevent_param
->u
.kretprobe
.offset
=
985 old_uevent_param
->u
.kretprobe
.offset
;
986 memcpy(uevent_param
->u
.kretprobe
.symbol_name
,
987 old_uevent_param
->u
.kretprobe
.symbol_name
,
988 sizeof(uevent_param
->u
.kretprobe
.symbol_name
));
990 case LTTNG_KERNEL_FUNCTION
:
991 memcpy(uevent_param
->u
.ftrace
.symbol_name
,
992 old_uevent_param
->u
.ftrace
.symbol_name
,
993 sizeof(uevent_param
->u
.ftrace
.symbol_name
));
998 ret
= lttng_abi_create_event(file
, uevent_param
);
1000 old_event_error_free_old_param
:
1001 kfree(old_uevent_param
);
1002 old_event_error_free_param
:
1003 kfree(uevent_param
);
1007 case LTTNG_KERNEL_EVENT
:
1009 struct lttng_kernel_event uevent_param
;
1011 if (copy_from_user(&uevent_param
,
1012 (struct lttng_kernel_event __user
*) arg
,
1013 sizeof(uevent_param
)))
1015 return lttng_abi_create_event(file
, &uevent_param
);
1017 case LTTNG_KERNEL_OLD_CONTEXT
:
1019 struct lttng_kernel_context
*ucontext_param
;
1020 struct lttng_kernel_old_context
*old_ucontext_param
;
1023 ucontext_param
= kmalloc(sizeof(struct lttng_kernel_context
),
1025 if (!ucontext_param
) {
1029 old_ucontext_param
= kmalloc(sizeof(struct lttng_kernel_old_context
),
1031 if (!old_ucontext_param
) {
1033 goto old_ctx_error_free_param
;
1036 if (copy_from_user(old_ucontext_param
,
1037 (struct lttng_kernel_old_context __user
*) arg
,
1038 sizeof(struct lttng_kernel_old_context
))) {
1040 goto old_ctx_error_free_old_param
;
1042 ucontext_param
->ctx
= old_ucontext_param
->ctx
;
1043 memcpy(ucontext_param
->padding
, old_ucontext_param
->padding
,
1044 sizeof(ucontext_param
->padding
));
1045 /* only type that uses the union */
1046 if (old_ucontext_param
->ctx
== LTTNG_KERNEL_CONTEXT_PERF_COUNTER
) {
1047 ucontext_param
->u
.perf_counter
.type
=
1048 old_ucontext_param
->u
.perf_counter
.type
;
1049 ucontext_param
->u
.perf_counter
.config
=
1050 old_ucontext_param
->u
.perf_counter
.config
;
1051 memcpy(ucontext_param
->u
.perf_counter
.name
,
1052 old_ucontext_param
->u
.perf_counter
.name
,
1053 sizeof(ucontext_param
->u
.perf_counter
.name
));
1056 ret
= lttng_abi_add_context(file
,
1058 &channel
->ctx
, channel
->session
);
1060 old_ctx_error_free_old_param
:
1061 kfree(old_ucontext_param
);
1062 old_ctx_error_free_param
:
1063 kfree(ucontext_param
);
1067 case LTTNG_KERNEL_CONTEXT
:
1069 struct lttng_kernel_context ucontext_param
;
1071 if (copy_from_user(&ucontext_param
,
1072 (struct lttng_kernel_context __user
*) arg
,
1073 sizeof(ucontext_param
)))
1075 return lttng_abi_add_context(file
,
1077 &channel
->ctx
, channel
->session
);
1079 case LTTNG_KERNEL_OLD_ENABLE
:
1080 case LTTNG_KERNEL_ENABLE
:
1081 return lttng_channel_enable(channel
);
1082 case LTTNG_KERNEL_OLD_DISABLE
:
1083 case LTTNG_KERNEL_DISABLE
:
1084 return lttng_channel_disable(channel
);
1086 return -ENOIOCTLCMD
;
1092 * lttng_metadata_ioctl - lttng syscall through ioctl
1098 * This ioctl implements lttng commands:
1099 * LTTNG_KERNEL_STREAM
1100 * Returns an event stream file descriptor or failure.
1102 * Channel and event file descriptors also hold a reference on the session.
1105 long lttng_metadata_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1108 case LTTNG_KERNEL_OLD_STREAM
:
1109 case LTTNG_KERNEL_STREAM
:
1110 return lttng_abi_open_metadata_stream(file
);
1112 return -ENOIOCTLCMD
;
1117 * lttng_channel_poll - lttng stream addition/removal monitoring
1122 unsigned int lttng_channel_poll(struct file
*file
, poll_table
*wait
)
1124 struct lttng_channel
*channel
= file
->private_data
;
1125 unsigned int mask
= 0;
1127 if (file
->f_mode
& FMODE_READ
) {
1128 poll_wait_set_exclusive(wait
);
1129 poll_wait(file
, channel
->ops
->get_hp_wait_queue(channel
->chan
),
1132 if (channel
->ops
->is_disabled(channel
->chan
))
1134 if (channel
->ops
->is_finalized(channel
->chan
))
1136 if (channel
->ops
->buffer_has_read_closed_stream(channel
->chan
))
1137 return POLLIN
| POLLRDNORM
;
1145 int lttng_channel_release(struct inode
*inode
, struct file
*file
)
1147 struct lttng_channel
*channel
= file
->private_data
;
1150 fput(channel
->session
->file
);
1155 int lttng_metadata_channel_release(struct inode
*inode
, struct file
*file
)
1157 struct lttng_channel
*channel
= file
->private_data
;
1160 lttng_metadata_channel_destroy(channel
);
1161 fput(channel
->session
->file
);
1167 static const struct file_operations lttng_channel_fops
= {
1168 .owner
= THIS_MODULE
,
1169 .release
= lttng_channel_release
,
1170 .poll
= lttng_channel_poll
,
1171 .unlocked_ioctl
= lttng_channel_ioctl
,
1172 #ifdef CONFIG_COMPAT
1173 .compat_ioctl
= lttng_channel_ioctl
,
1177 static const struct file_operations lttng_metadata_fops
= {
1178 .owner
= THIS_MODULE
,
1179 .release
= lttng_metadata_channel_release
,
1180 .unlocked_ioctl
= lttng_metadata_ioctl
,
1181 #ifdef CONFIG_COMPAT
1182 .compat_ioctl
= lttng_metadata_ioctl
,
1187 * lttng_event_ioctl - lttng syscall through ioctl
1193 * This ioctl implements lttng commands:
1194 * LTTNG_KERNEL_CONTEXT
1195 * Prepend a context field to each record of this event
1196 * LTTNG_KERNEL_ENABLE
1197 * Enable recording for this event (weak enable)
1198 * LTTNG_KERNEL_DISABLE
1199 * Disable recording for this event (strong disable)
1202 long lttng_event_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1204 struct lttng_event
*event
= file
->private_data
;
1207 case LTTNG_KERNEL_OLD_CONTEXT
:
1209 struct lttng_kernel_context
*ucontext_param
;
1210 struct lttng_kernel_old_context
*old_ucontext_param
;
1213 ucontext_param
= kmalloc(sizeof(struct lttng_kernel_context
),
1215 if (!ucontext_param
) {
1219 old_ucontext_param
= kmalloc(sizeof(struct lttng_kernel_old_context
),
1221 if (!old_ucontext_param
) {
1223 goto old_ctx_error_free_param
;
1226 if (copy_from_user(old_ucontext_param
,
1227 (struct lttng_kernel_old_context __user
*) arg
,
1228 sizeof(struct lttng_kernel_old_context
))) {
1230 goto old_ctx_error_free_old_param
;
1232 ucontext_param
->ctx
= old_ucontext_param
->ctx
;
1233 memcpy(ucontext_param
->padding
, old_ucontext_param
->padding
,
1234 sizeof(ucontext_param
->padding
));
1235 /* only type that uses the union */
1236 if (old_ucontext_param
->ctx
== LTTNG_KERNEL_CONTEXT_PERF_COUNTER
) {
1237 ucontext_param
->u
.perf_counter
.type
=
1238 old_ucontext_param
->u
.perf_counter
.type
;
1239 ucontext_param
->u
.perf_counter
.config
=
1240 old_ucontext_param
->u
.perf_counter
.config
;
1241 memcpy(ucontext_param
->u
.perf_counter
.name
,
1242 old_ucontext_param
->u
.perf_counter
.name
,
1243 sizeof(ucontext_param
->u
.perf_counter
.name
));
1246 ret
= lttng_abi_add_context(file
,
1248 &event
->ctx
, event
->chan
->session
);
1250 old_ctx_error_free_old_param
:
1251 kfree(old_ucontext_param
);
1252 old_ctx_error_free_param
:
1253 kfree(ucontext_param
);
1257 case LTTNG_KERNEL_CONTEXT
:
1259 struct lttng_kernel_context ucontext_param
;
1261 if (copy_from_user(&ucontext_param
,
1262 (struct lttng_kernel_context __user
*) arg
,
1263 sizeof(ucontext_param
)))
1265 return lttng_abi_add_context(file
,
1267 &event
->ctx
, event
->chan
->session
);
1269 case LTTNG_KERNEL_OLD_ENABLE
:
1270 case LTTNG_KERNEL_ENABLE
:
1271 return lttng_event_enable(event
);
1272 case LTTNG_KERNEL_OLD_DISABLE
:
1273 case LTTNG_KERNEL_DISABLE
:
1274 return lttng_event_disable(event
);
1276 return -ENOIOCTLCMD
;
1281 int lttng_event_release(struct inode
*inode
, struct file
*file
)
1283 struct lttng_event
*event
= file
->private_data
;
1286 fput(event
->chan
->file
);
1290 /* TODO: filter control ioctl */
1291 static const struct file_operations lttng_event_fops
= {
1292 .owner
= THIS_MODULE
,
1293 .release
= lttng_event_release
,
1294 .unlocked_ioctl
= lttng_event_ioctl
,
1295 #ifdef CONFIG_COMPAT
1296 .compat_ioctl
= lttng_event_ioctl
,
1300 int __init
lttng_abi_init(void)
1304 wrapper_vmalloc_sync_all();
1305 lttng_proc_dentry
= proc_create_data("lttng", S_IRUSR
| S_IWUSR
, NULL
,
1308 if (!lttng_proc_dentry
) {
1309 printk(KERN_ERR
"Error creating LTTng control file\n");
1317 void __exit
lttng_abi_exit(void)
1319 if (lttng_proc_dentry
)
1320 remove_proc_entry("lttng", NULL
);