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/poll.h"
49 #include "lttng-abi.h"
50 #include "lttng-events.h"
51 #include "lttng-tracer.h"
54 * This is LTTng's own personal way to create a system call as an external
55 * module. We use ioctl() on /proc/lttng.
58 static struct proc_dir_entry
*lttng_proc_dentry
;
59 static const struct file_operations lttng_fops
;
60 static const struct file_operations lttng_session_fops
;
61 static const struct file_operations lttng_channel_fops
;
62 static const struct file_operations lttng_metadata_fops
;
63 static const struct file_operations lttng_event_fops
;
66 * Teardown management: opened file descriptors keep a refcount on the module,
67 * so it can only exit when all file descriptors are closed.
76 int lttng_abi_create_session(void)
78 struct lttng_session
*session
;
79 struct file
*session_file
;
82 session
= lttng_session_create();
85 session_fd
= get_unused_fd();
90 session_file
= anon_inode_getfile("[lttng_session]",
93 if (IS_ERR(session_file
)) {
94 ret
= PTR_ERR(session_file
);
97 session
->file
= session_file
;
98 fd_install(session_fd
, session_file
);
102 put_unused_fd(session_fd
);
104 lttng_session_destroy(session
);
109 int lttng_abi_tracepoint_list(void)
111 struct file
*tracepoint_list_file
;
114 file_fd
= get_unused_fd();
120 tracepoint_list_file
= anon_inode_getfile("[lttng_session]",
121 <tng_tracepoint_list_fops
,
123 if (IS_ERR(tracepoint_list_file
)) {
124 ret
= PTR_ERR(tracepoint_list_file
);
127 ret
= lttng_tracepoint_list_fops
.open(NULL
, tracepoint_list_file
);
130 fd_install(file_fd
, tracepoint_list_file
);
138 fput(tracepoint_list_file
);
140 put_unused_fd(file_fd
);
146 long lttng_abi_tracer_version(struct file
*file
,
147 struct lttng_kernel_tracer_version __user
*uversion_param
)
149 struct lttng_kernel_tracer_version v
;
151 v
.major
= LTTNG_MODULES_MAJOR_VERSION
;
152 v
.minor
= LTTNG_MODULES_MINOR_VERSION
;
153 v
.patchlevel
= LTTNG_MODULES_PATCHLEVEL_VERSION
;
155 if (copy_to_user(uversion_param
, &v
, sizeof(v
)))
161 long lttng_abi_add_context(struct file
*file
,
162 struct lttng_kernel_context __user
*ucontext_param
,
163 struct lttng_ctx
**ctx
, struct lttng_session
*session
)
165 struct lttng_kernel_context context_param
;
167 if (session
->been_active
)
170 if (copy_from_user(&context_param
, ucontext_param
, sizeof(context_param
)))
173 switch (context_param
.ctx
) {
174 case LTTNG_KERNEL_CONTEXT_PID
:
175 return lttng_add_pid_to_ctx(ctx
);
176 case LTTNG_KERNEL_CONTEXT_PRIO
:
177 return lttng_add_prio_to_ctx(ctx
);
178 case LTTNG_KERNEL_CONTEXT_NICE
:
179 return lttng_add_nice_to_ctx(ctx
);
180 case LTTNG_KERNEL_CONTEXT_VPID
:
181 return lttng_add_vpid_to_ctx(ctx
);
182 case LTTNG_KERNEL_CONTEXT_TID
:
183 return lttng_add_tid_to_ctx(ctx
);
184 case LTTNG_KERNEL_CONTEXT_VTID
:
185 return lttng_add_vtid_to_ctx(ctx
);
186 case LTTNG_KERNEL_CONTEXT_PPID
:
187 return lttng_add_ppid_to_ctx(ctx
);
188 case LTTNG_KERNEL_CONTEXT_VPPID
:
189 return lttng_add_vppid_to_ctx(ctx
);
190 case LTTNG_KERNEL_CONTEXT_PERF_COUNTER
:
191 context_param
.u
.perf_counter
.name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
192 return lttng_add_perf_counter_to_ctx(context_param
.u
.perf_counter
.type
,
193 context_param
.u
.perf_counter
.config
,
194 context_param
.u
.perf_counter
.name
,
196 case LTTNG_KERNEL_CONTEXT_PROCNAME
:
197 return lttng_add_procname_to_ctx(ctx
);
198 case LTTNG_KERNEL_CONTEXT_HOSTNAME
:
199 return lttng_add_hostname_to_ctx(ctx
);
206 * lttng_ioctl - lttng syscall through ioctl
212 * This ioctl implements lttng commands:
213 * LTTNG_KERNEL_SESSION
214 * Returns a LTTng trace session file descriptor
215 * LTTNG_KERNEL_TRACER_VERSION
216 * Returns the LTTng kernel tracer version
217 * LTTNG_KERNEL_TRACEPOINT_LIST
218 * Returns a file descriptor listing available tracepoints
219 * LTTNG_KERNEL_WAIT_QUIESCENT
220 * Returns after all previously running probes have completed
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_SESSION
:
229 return lttng_abi_create_session();
230 case LTTNG_KERNEL_TRACER_VERSION
:
231 return lttng_abi_tracer_version(file
,
232 (struct lttng_kernel_tracer_version __user
*) arg
);
233 case LTTNG_KERNEL_TRACEPOINT_LIST
:
234 return lttng_abi_tracepoint_list();
235 case LTTNG_KERNEL_WAIT_QUIESCENT
:
238 case LTTNG_KERNEL_CALIBRATE
:
240 struct lttng_kernel_calibrate __user
*ucalibrate
=
241 (struct lttng_kernel_calibrate __user
*) arg
;
242 struct lttng_kernel_calibrate calibrate
;
245 if (copy_from_user(&calibrate
, ucalibrate
, sizeof(calibrate
)))
247 ret
= lttng_calibrate(&calibrate
);
248 if (copy_to_user(ucalibrate
, &calibrate
, sizeof(calibrate
)))
257 static const struct file_operations lttng_fops
= {
258 .owner
= THIS_MODULE
,
259 .unlocked_ioctl
= lttng_ioctl
,
261 .compat_ioctl
= lttng_ioctl
,
266 * We tolerate no failure in this function (if one happens, we print a dmesg
267 * error, but cannot return any error, because the channel information is
271 void lttng_metadata_create_events(struct file
*channel_file
)
273 struct lttng_channel
*channel
= channel_file
->private_data
;
274 static struct lttng_kernel_event metadata_params
= {
275 .instrumentation
= LTTNG_KERNEL_TRACEPOINT
,
276 .name
= "lttng_metadata",
278 struct lttng_event
*event
;
281 * We tolerate no failure path after event creation. It will stay
282 * invariant for the rest of the session.
284 event
= lttng_event_create(channel
, &metadata_params
, NULL
, NULL
);
292 return; /* not allowed to return error */
296 int lttng_abi_create_channel(struct file
*session_file
,
297 struct lttng_kernel_channel __user
*uchan_param
,
298 enum channel_type channel_type
)
300 struct lttng_session
*session
= session_file
->private_data
;
301 const struct file_operations
*fops
= NULL
;
302 const char *transport_name
;
303 struct lttng_channel
*chan
;
304 struct file
*chan_file
;
305 struct lttng_kernel_channel chan_param
;
309 if (copy_from_user(&chan_param
, uchan_param
, sizeof(chan_param
)))
311 chan_fd
= get_unused_fd();
316 switch (channel_type
) {
317 case PER_CPU_CHANNEL
:
318 fops
= <tng_channel_fops
;
320 case METADATA_CHANNEL
:
321 fops
= <tng_metadata_fops
;
325 chan_file
= anon_inode_getfile("[lttng_channel]",
328 if (IS_ERR(chan_file
)) {
329 ret
= PTR_ERR(chan_file
);
332 switch (channel_type
) {
333 case PER_CPU_CHANNEL
:
334 if (chan_param
.output
== LTTNG_KERNEL_SPLICE
) {
335 transport_name
= chan_param
.overwrite
?
336 "relay-overwrite" : "relay-discard";
337 } else if (chan_param
.output
== LTTNG_KERNEL_MMAP
) {
338 transport_name
= chan_param
.overwrite
?
339 "relay-overwrite-mmap" : "relay-discard-mmap";
344 case METADATA_CHANNEL
:
345 if (chan_param
.output
== LTTNG_KERNEL_SPLICE
)
346 transport_name
= "relay-metadata";
347 else if (chan_param
.output
== LTTNG_KERNEL_MMAP
)
348 transport_name
= "relay-metadata-mmap";
353 transport_name
= "<unknown>";
357 * We tolerate no failure path after channel creation. It will stay
358 * invariant for the rest of the session.
360 chan
= lttng_channel_create(session
, transport_name
, NULL
,
361 chan_param
.subbuf_size
,
362 chan_param
.num_subbuf
,
363 chan_param
.switch_timer_interval
,
364 chan_param
.read_timer_interval
);
369 chan
->file
= chan_file
;
370 chan_file
->private_data
= chan
;
371 fd_install(chan_fd
, chan_file
);
372 if (channel_type
== METADATA_CHANNEL
) {
373 session
->metadata
= chan
;
374 lttng_metadata_create_events(chan_file
);
377 /* The channel created holds a reference on the session */
378 atomic_long_inc(&session_file
->f_count
);
385 put_unused_fd(chan_fd
);
391 * lttng_session_ioctl - lttng session fd ioctl
397 * This ioctl implements lttng commands:
398 * LTTNG_KERNEL_CHANNEL
399 * Returns a LTTng channel file descriptor
400 * LTTNG_KERNEL_ENABLE
401 * Enables tracing for a session (weak enable)
402 * LTTNG_KERNEL_DISABLE
403 * Disables tracing for a session (strong disable)
404 * LTTNG_KERNEL_METADATA
405 * Returns a LTTng metadata file descriptor
407 * The returned channel will be deleted when its file descriptor is closed.
410 long lttng_session_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
412 struct lttng_session
*session
= file
->private_data
;
415 case LTTNG_KERNEL_CHANNEL
:
416 return lttng_abi_create_channel(file
,
417 (struct lttng_kernel_channel __user
*) arg
,
419 case LTTNG_KERNEL_SESSION_START
:
420 case LTTNG_KERNEL_ENABLE
:
421 return lttng_session_enable(session
);
422 case LTTNG_KERNEL_SESSION_STOP
:
423 case LTTNG_KERNEL_DISABLE
:
424 return lttng_session_disable(session
);
425 case LTTNG_KERNEL_METADATA
:
426 return lttng_abi_create_channel(file
,
427 (struct lttng_kernel_channel __user
*) arg
,
435 * Called when the last file reference is dropped.
437 * Big fat note: channels and events are invariant for the whole session after
438 * their creation. So this session destruction also destroys all channel and
439 * event structures specific to this session (they are not destroyed when their
440 * individual file is released).
443 int lttng_session_release(struct inode
*inode
, struct file
*file
)
445 struct lttng_session
*session
= file
->private_data
;
448 lttng_session_destroy(session
);
452 static const struct file_operations lttng_session_fops
= {
453 .owner
= THIS_MODULE
,
454 .release
= lttng_session_release
,
455 .unlocked_ioctl
= lttng_session_ioctl
,
457 .compat_ioctl
= lttng_session_ioctl
,
462 int lttng_abi_open_stream(struct file
*channel_file
)
464 struct lttng_channel
*channel
= channel_file
->private_data
;
465 struct lib_ring_buffer
*buf
;
467 struct file
*stream_file
;
469 buf
= channel
->ops
->buffer_read_open(channel
->chan
);
473 stream_fd
= get_unused_fd();
478 stream_file
= anon_inode_getfile("[lttng_stream]",
479 &lib_ring_buffer_file_operations
,
481 if (IS_ERR(stream_file
)) {
482 ret
= PTR_ERR(stream_file
);
486 * OPEN_FMODE, called within anon_inode_getfile/alloc_file, don't honor
487 * FMODE_LSEEK, FMODE_PREAD nor FMODE_PWRITE. We need to read from this
488 * file descriptor, so we set FMODE_PREAD here.
490 stream_file
->f_mode
|= FMODE_PREAD
;
491 fd_install(stream_fd
, stream_file
);
493 * The stream holds a reference to the channel within the generic ring
494 * buffer library, so no need to hold a refcount on the channel and
495 * session files here.
500 put_unused_fd(stream_fd
);
502 channel
->ops
->buffer_read_close(buf
);
507 int lttng_abi_create_event(struct file
*channel_file
,
508 struct lttng_kernel_event __user
*uevent_param
)
510 struct lttng_channel
*channel
= channel_file
->private_data
;
511 struct lttng_event
*event
;
512 struct lttng_kernel_event event_param
;
514 struct file
*event_file
;
516 if (copy_from_user(&event_param
, uevent_param
, sizeof(event_param
)))
518 event_param
.name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
519 switch (event_param
.instrumentation
) {
520 case LTTNG_KERNEL_KRETPROBE
:
521 event_param
.u
.kretprobe
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
523 case LTTNG_KERNEL_KPROBE
:
524 event_param
.u
.kprobe
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
526 case LTTNG_KERNEL_FUNCTION
:
527 event_param
.u
.ftrace
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
532 switch (event_param
.instrumentation
) {
534 event_fd
= get_unused_fd();
539 event_file
= anon_inode_getfile("[lttng_event]",
542 if (IS_ERR(event_file
)) {
543 ret
= PTR_ERR(event_file
);
547 * We tolerate no failure path after event creation. It
548 * will stay invariant for the rest of the session.
550 event
= lttng_event_create(channel
, &event_param
, NULL
, NULL
);
555 event_file
->private_data
= event
;
556 fd_install(event_fd
, event_file
);
557 /* The event holds a reference on the channel */
558 atomic_long_inc(&channel_file
->f_count
);
560 case LTTNG_KERNEL_SYSCALL
:
562 * Only all-syscall tracing supported for now.
564 if (event_param
.name
[0] != '\0')
566 ret
= lttng_syscalls_register(channel
, NULL
);
577 put_unused_fd(event_fd
);
583 * lttng_channel_ioctl - lttng syscall through ioctl
589 * This ioctl implements lttng commands:
590 * LTTNG_KERNEL_STREAM
591 * Returns an event stream file descriptor or failure.
592 * (typically, one event stream records events from one CPU)
594 * Returns an event file descriptor or failure.
595 * LTTNG_KERNEL_CONTEXT
596 * Prepend a context field to each event in the channel
597 * LTTNG_KERNEL_ENABLE
598 * Enable recording for events in this channel (weak enable)
599 * LTTNG_KERNEL_DISABLE
600 * Disable recording for events in this channel (strong disable)
602 * Channel and event file descriptors also hold a reference on the session.
605 long lttng_channel_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
607 struct lttng_channel
*channel
= file
->private_data
;
610 case LTTNG_KERNEL_STREAM
:
611 return lttng_abi_open_stream(file
);
612 case LTTNG_KERNEL_EVENT
:
613 return lttng_abi_create_event(file
, (struct lttng_kernel_event __user
*) arg
);
614 case LTTNG_KERNEL_CONTEXT
:
615 return lttng_abi_add_context(file
,
616 (struct lttng_kernel_context __user
*) arg
,
617 &channel
->ctx
, channel
->session
);
618 case LTTNG_KERNEL_ENABLE
:
619 return lttng_channel_enable(channel
);
620 case LTTNG_KERNEL_DISABLE
:
621 return lttng_channel_disable(channel
);
628 * lttng_metadata_ioctl - lttng syscall through ioctl
634 * This ioctl implements lttng commands:
635 * LTTNG_KERNEL_STREAM
636 * Returns an event stream file descriptor or failure.
638 * Channel and event file descriptors also hold a reference on the session.
641 long lttng_metadata_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
644 case LTTNG_KERNEL_STREAM
:
645 return lttng_abi_open_stream(file
);
652 * lttng_channel_poll - lttng stream addition/removal monitoring
657 unsigned int lttng_channel_poll(struct file
*file
, poll_table
*wait
)
659 struct lttng_channel
*channel
= file
->private_data
;
660 unsigned int mask
= 0;
662 if (file
->f_mode
& FMODE_READ
) {
663 poll_wait_set_exclusive(wait
);
664 poll_wait(file
, channel
->ops
->get_hp_wait_queue(channel
->chan
),
667 if (channel
->ops
->is_disabled(channel
->chan
))
669 if (channel
->ops
->is_finalized(channel
->chan
))
671 if (channel
->ops
->buffer_has_read_closed_stream(channel
->chan
))
672 return POLLIN
| POLLRDNORM
;
680 int lttng_channel_release(struct inode
*inode
, struct file
*file
)
682 struct lttng_channel
*channel
= file
->private_data
;
685 fput(channel
->session
->file
);
689 static const struct file_operations lttng_channel_fops
= {
690 .owner
= THIS_MODULE
,
691 .release
= lttng_channel_release
,
692 .poll
= lttng_channel_poll
,
693 .unlocked_ioctl
= lttng_channel_ioctl
,
695 .compat_ioctl
= lttng_channel_ioctl
,
699 static const struct file_operations lttng_metadata_fops
= {
700 .owner
= THIS_MODULE
,
701 .release
= lttng_channel_release
,
702 .unlocked_ioctl
= lttng_metadata_ioctl
,
704 .compat_ioctl
= lttng_metadata_ioctl
,
709 * lttng_event_ioctl - lttng syscall through ioctl
715 * This ioctl implements lttng commands:
716 * LTTNG_KERNEL_CONTEXT
717 * Prepend a context field to each record of this event
718 * LTTNG_KERNEL_ENABLE
719 * Enable recording for this event (weak enable)
720 * LTTNG_KERNEL_DISABLE
721 * Disable recording for this event (strong disable)
724 long lttng_event_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
726 struct lttng_event
*event
= file
->private_data
;
729 case LTTNG_KERNEL_CONTEXT
:
730 return lttng_abi_add_context(file
,
731 (struct lttng_kernel_context __user
*) arg
,
732 &event
->ctx
, event
->chan
->session
);
733 case LTTNG_KERNEL_ENABLE
:
734 return lttng_event_enable(event
);
735 case LTTNG_KERNEL_DISABLE
:
736 return lttng_event_disable(event
);
743 int lttng_event_release(struct inode
*inode
, struct file
*file
)
745 struct lttng_event
*event
= file
->private_data
;
748 fput(event
->chan
->file
);
752 /* TODO: filter control ioctl */
753 static const struct file_operations lttng_event_fops
= {
754 .owner
= THIS_MODULE
,
755 .release
= lttng_event_release
,
756 .unlocked_ioctl
= lttng_event_ioctl
,
758 .compat_ioctl
= lttng_event_ioctl
,
762 int __init
lttng_abi_init(void)
766 wrapper_vmalloc_sync_all();
767 lttng_proc_dentry
= proc_create_data("lttng", S_IRUSR
| S_IWUSR
, NULL
,
770 if (!lttng_proc_dentry
) {
771 printk(KERN_ERR
"Error creating LTTng control file\n");
779 void __exit
lttng_abi_exit(void)
781 if (lttng_proc_dentry
)
782 remove_proc_entry("lttng", NULL
);