4 * Copyright 2010 (c) - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 * Mimic system calls for:
9 * - session creation, returns a file descriptor or failure.
10 * - channel creation, returns a file descriptor or failure.
11 * - Operates on a session file descriptor
12 * - Takes all channel options as parameters.
13 * - stream get, returns a file descriptor or failure.
14 * - Operates on a channel file descriptor.
15 * - stream notifier get, returns a file descriptor or failure.
16 * - Operates on a channel file descriptor.
17 * - event creation, returns a file descriptor or failure.
18 * - Operates on a channel file descriptor
19 * - Takes an event name as parameter
20 * - Takes an instrumentation source as parameter
21 * - e.g. tracepoints, dynamic_probes...
22 * - Takes instrumentation source specific arguments.
25 #include <linux/module.h>
26 #include <linux/debugfs.h>
27 #include <linux/anon_inodes.h>
28 #include <linux/file.h>
29 #include <linux/uaccess.h>
30 #include <linux/slab.h>
31 #include "wrapper/vmalloc.h" /* for wrapper_vmalloc_sync_all() */
32 #include "wrapper/ringbuffer/vfs.h"
33 #include "ltt-debugfs-abi.h"
34 #include "ltt-events.h"
35 #include "ltt-tracer.h"
38 * This is LTTng's own personal way to create a system call as an external
39 * module. We use ioctl() on /sys/kernel/debug/lttng.
42 static struct dentry
*lttng_dentry
;
43 static const struct file_operations lttng_fops
;
44 static const struct file_operations lttng_session_fops
;
45 static const struct file_operations lttng_channel_fops
;
46 static const struct file_operations lttng_metadata_fops
;
47 static const struct file_operations lttng_event_fops
;
55 int lttng_abi_create_session(void)
57 struct ltt_session
*session
;
58 struct file
*session_file
;
61 session
= ltt_session_create();
64 session_fd
= get_unused_fd();
69 session_file
= anon_inode_getfile("[lttng_session]",
72 if (IS_ERR(session_file
)) {
73 ret
= PTR_ERR(session_file
);
76 session
->file
= session_file
;
77 fd_install(session_fd
, session_file
);
81 put_unused_fd(session_fd
);
83 ltt_session_destroy(session
);
88 int lttng_abi_tracepoint_list(void)
90 struct file
*tracepoint_list_file
;
93 file_fd
= get_unused_fd();
98 tracepoint_list_file
= anon_inode_getfile("[lttng_session]",
99 <tng_tracepoint_list_fops
,
101 if (IS_ERR(tracepoint_list_file
)) {
102 ret
= PTR_ERR(tracepoint_list_file
);
105 fd_install(file_fd
, tracepoint_list_file
);
109 put_unused_fd(file_fd
);
115 long lttng_abi_tracer_version(struct file
*file
,
116 struct lttng_kernel_tracer_version __user
*uversion_param
)
118 struct lttng_kernel_tracer_version v
;
120 v
.version
= LTTNG_VERSION
;
121 v
.patchlevel
= LTTNG_PATCHLEVEL
;
122 v
.sublevel
= LTTNG_SUBLEVEL
;
124 if (copy_to_user(uversion_param
, &v
, sizeof(v
)))
130 * lttng_ioctl - lttng syscall through ioctl
136 * This ioctl implements lttng commands:
137 * LTTNG_KERNEL_SESSION
138 * Returns a LTTng trace session file descriptor
139 * LTTNG_KERNEL_TRACER_VERSION
140 * Returns the LTTng kernel tracer version
141 * LTTNG_KERNEL_TRACEPOINT_LIST
142 * Returns a file descriptor listing available tracepoints
144 * The returned session will be deleted when its file descriptor is closed.
147 long lttng_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
150 case LTTNG_KERNEL_SESSION
:
151 return lttng_abi_create_session();
152 case LTTNG_KERNEL_TRACER_VERSION
:
153 return lttng_abi_tracer_version(file
,
154 (struct lttng_kernel_tracer_version __user
*) arg
);
155 case LTTNG_KERNEL_TRACEPOINT_LIST
:
156 return lttng_abi_tracepoint_list();
162 static const struct file_operations lttng_fops
= {
163 .unlocked_ioctl
= lttng_ioctl
,
165 .compat_ioctl
= lttng_ioctl
,
170 * We tolerate no failure in this function (if one happens, we print a dmesg
171 * error, but cannot return any error, because the channel information is
175 void lttng_metadata_create_events(struct file
*channel_file
)
177 struct ltt_channel
*channel
= channel_file
->private_data
;
178 static struct lttng_kernel_event metadata_params
= {
179 .instrumentation
= LTTNG_KERNEL_TRACEPOINT
,
180 .name
= "lttng_metadata",
182 struct ltt_event
*event
;
186 * We tolerate no failure path after event creation. It will stay
187 * invariant for the rest of the session.
189 event
= ltt_event_create(channel
, &metadata_params
, NULL
);
198 return; /* not allowed to return error */
202 int lttng_abi_create_channel(struct file
*session_file
,
203 struct lttng_kernel_channel __user
*uchan_param
,
204 enum channel_type channel_type
)
206 struct ltt_session
*session
= session_file
->private_data
;
207 const struct file_operations
*fops
;
208 const char *transport_name
;
209 struct ltt_channel
*chan
;
210 struct file
*chan_file
;
211 struct lttng_kernel_channel chan_param
;
215 if (copy_from_user(&chan_param
, uchan_param
, sizeof(chan_param
)))
217 chan_fd
= get_unused_fd();
222 chan_file
= anon_inode_getfile("[lttng_channel]",
225 if (IS_ERR(chan_file
)) {
226 ret
= PTR_ERR(chan_file
);
229 switch (channel_type
) {
230 case PER_CPU_CHANNEL
:
231 transport_name
= chan_param
.overwrite
?
232 "relay-overwrite" : "relay-discard";
233 fops
= <tng_channel_fops
;
235 case METADATA_CHANNEL
:
236 transport_name
= "relay-metadata";
237 fops
= <tng_metadata_fops
;
240 transport_name
= "<unknown>";
244 * We tolerate no failure path after channel creation. It will stay
245 * invariant for the rest of the session.
247 chan
= ltt_channel_create(session
, transport_name
, NULL
,
248 chan_param
.subbuf_size
,
249 chan_param
.num_subbuf
,
250 chan_param
.switch_timer_interval
,
251 chan_param
.read_timer_interval
);
256 chan
->file
= chan_file
;
257 chan_file
->private_data
= chan
;
258 fd_install(chan_fd
, chan_file
);
259 if (channel_type
== METADATA_CHANNEL
) {
260 session
->metadata
= chan
;
261 lttng_metadata_create_events(chan_file
);
264 /* The channel created holds a reference on the session */
265 atomic_long_inc(&session_file
->f_count
);
272 put_unused_fd(chan_fd
);
278 * lttng_session_ioctl - lttng session fd ioctl
284 * This ioctl implements lttng commands:
285 * LTTNG_KERNEL_CHANNEL
286 * Returns a LTTng channel file descriptor
288 * The returned channel will be deleted when its file descriptor is closed.
291 long lttng_session_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
293 struct ltt_session
*session
= file
->private_data
;
296 case LTTNG_KERNEL_CHANNEL
:
297 return lttng_abi_create_channel(file
,
298 (struct lttng_kernel_channel __user
*) arg
,
300 case LTTNG_KERNEL_SESSION_START
:
301 return ltt_session_start(session
);
302 case LTTNG_KERNEL_SESSION_STOP
:
303 return ltt_session_stop(session
);
304 case LTTNG_KERNEL_METADATA
:
305 return lttng_abi_create_channel(file
,
306 (struct lttng_kernel_channel __user
*) arg
,
314 * Called when the last file reference is dropped.
316 * Big fat note: channels and events are invariant for the whole session after
317 * their creation. So this session destruction also destroys all channel and
318 * event structures specific to this session (they are not destroyed when their
319 * individual file is released).
322 int lttng_session_release(struct inode
*inode
, struct file
*file
)
324 struct ltt_session
*session
= file
->private_data
;
327 ltt_session_destroy(session
);
331 static const struct file_operations lttng_session_fops
= {
332 .release
= lttng_session_release
,
333 .unlocked_ioctl
= lttng_session_ioctl
,
335 .compat_ioctl
= lttng_session_ioctl
,
340 int lttng_abi_open_stream(struct file
*channel_file
)
342 struct ltt_channel
*channel
= channel_file
->private_data
;
343 struct lib_ring_buffer
*buf
;
345 struct file
*stream_file
;
347 buf
= channel
->ops
->buffer_read_open(channel
->chan
);
351 stream_fd
= get_unused_fd();
356 stream_file
= anon_inode_getfile("[lttng_stream]",
357 &lib_ring_buffer_file_operations
,
359 if (IS_ERR(stream_file
)) {
360 ret
= PTR_ERR(stream_file
);
364 * OPEN_FMODE, called within anon_inode_getfile/alloc_file, don't honor
365 * FMODE_LSEEK, FMODE_PREAD nor FMODE_PWRITE. We need to read from this
366 * file descriptor, so we set FMODE_PREAD here.
368 stream_file
->f_mode
|= FMODE_PREAD
;
369 fd_install(stream_fd
, stream_file
);
371 * The stream holds a reference to the channel within the generic ring
372 * buffer library, so no need to hold a refcount on the channel and
373 * session files here.
378 put_unused_fd(stream_fd
);
380 channel
->ops
->buffer_read_close(buf
);
385 int lttng_abi_create_event(struct file
*channel_file
,
386 struct lttng_kernel_event __user
*uevent_param
)
388 struct ltt_channel
*channel
= channel_file
->private_data
;
389 struct ltt_event
*event
;
390 struct lttng_kernel_event event_param
;
392 struct file
*event_file
;
394 if (copy_from_user(&event_param
, uevent_param
, sizeof(event_param
)))
396 event_param
.name
[LTTNG_SYM_NAME_LEN
- 1] = '\0';
397 switch (event_param
.instrumentation
) {
398 case LTTNG_KERNEL_KPROBE
:
399 event_param
.u
.kprobe
.symbol_name
[LTTNG_SYM_NAME_LEN
- 1] = '\0';
401 case LTTNG_KERNEL_FUNCTION
:
402 event_param
.u
.ftrace
.symbol_name
[LTTNG_SYM_NAME_LEN
- 1] = '\0';
407 event_fd
= get_unused_fd();
412 event_file
= anon_inode_getfile("[lttng_event]",
415 if (IS_ERR(event_file
)) {
416 ret
= PTR_ERR(event_file
);
420 * We tolerate no failure path after event creation. It will stay
421 * invariant for the rest of the session.
423 event
= ltt_event_create(channel
, &event_param
, NULL
);
428 event_file
->private_data
= event
;
429 fd_install(event_fd
, event_file
);
430 /* The event holds a reference on the channel */
431 atomic_long_inc(&channel_file
->f_count
);
437 put_unused_fd(event_fd
);
443 * lttng_channel_ioctl - lttng syscall through ioctl
449 * This ioctl implements lttng commands:
450 * LTTNG_KERNEL_STREAM
451 * Returns an event stream file descriptor or failure.
452 * (typically, one event stream records events from one CPU)
454 * Returns an event file descriptor or failure.
456 * Channel and event file descriptors also hold a reference on the session.
459 long lttng_channel_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
462 case LTTNG_KERNEL_STREAM
:
463 return lttng_abi_open_stream(file
);
464 case LTTNG_KERNEL_EVENT
:
465 return lttng_abi_create_event(file
, (struct lttng_kernel_event __user
*) arg
);
472 * lttng_metadata_ioctl - lttng syscall through ioctl
478 * This ioctl implements lttng commands:
479 * LTTNG_KERNEL_STREAM
480 * Returns an event stream file descriptor or failure.
482 * Channel and event file descriptors also hold a reference on the session.
485 long lttng_metadata_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
488 case LTTNG_KERNEL_STREAM
:
489 return lttng_abi_open_stream(file
);
498 * lttng_channel_poll - lttng stream addition/removal monitoring
503 unsigned int lttng_channel_poll(struct file
*file
, poll_table
*wait
)
505 struct ltt_channel
*channel
= file
->private_data
;
506 unsigned int mask
= 0;
508 if (file
->f_mode
& FMODE_READ
) {
509 poll_wait_set_exclusive(wait
);
510 poll_wait(file
, &channel
->notify_wait
, wait
);
512 /* TODO: identify when the channel is being finalized. */
516 return POLLIN
| POLLRDNORM
;
524 int lttng_channel_release(struct inode
*inode
, struct file
*file
)
526 struct ltt_channel
*channel
= file
->private_data
;
529 fput(channel
->session
->file
);
533 static const struct file_operations lttng_channel_fops
= {
534 .release
= lttng_channel_release
,
537 .poll
= lttng_channel_poll
,
539 .unlocked_ioctl
= lttng_channel_ioctl
,
541 .compat_ioctl
= lttng_channel_ioctl
,
545 static const struct file_operations lttng_metadata_fops
= {
546 .release
= lttng_channel_release
,
547 .unlocked_ioctl
= lttng_metadata_ioctl
,
549 .compat_ioctl
= lttng_metadata_ioctl
,
554 int lttng_event_release(struct inode
*inode
, struct file
*file
)
556 struct ltt_event
*event
= file
->private_data
;
559 fput(event
->chan
->file
);
563 /* TODO: filter control ioctl */
564 static const struct file_operations lttng_event_fops
= {
565 .release
= lttng_event_release
,
568 int __init
ltt_debugfs_abi_init(void)
572 wrapper_vmalloc_sync_all();
573 lttng_dentry
= debugfs_create_file("lttng", S_IWUSR
, NULL
, NULL
,
575 if (IS_ERR(lttng_dentry
) || !lttng_dentry
) {
576 printk(KERN_ERR
"Error creating LTTng control file\n");
584 void __exit
ltt_debugfs_abi_exit(void)
586 debugfs_remove(lttng_dentry
);