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();
99 tracepoint_list_file
= anon_inode_getfile("[lttng_session]",
100 <tng_tracepoint_list_fops
,
102 if (IS_ERR(tracepoint_list_file
)) {
103 ret
= PTR_ERR(tracepoint_list_file
);
106 ret
= lttng_tracepoint_list_fops
.open(NULL
, tracepoint_list_file
);
109 fd_install(file_fd
, tracepoint_list_file
);
117 fput(tracepoint_list_file
);
119 put_unused_fd(file_fd
);
125 long lttng_abi_tracer_version(struct file
*file
,
126 struct lttng_kernel_tracer_version __user
*uversion_param
)
128 struct lttng_kernel_tracer_version v
;
130 v
.version
= LTTNG_VERSION
;
131 v
.patchlevel
= LTTNG_PATCHLEVEL
;
132 v
.sublevel
= LTTNG_SUBLEVEL
;
134 if (copy_to_user(uversion_param
, &v
, sizeof(v
)))
140 long lttng_abi_add_context(struct file
*file
,
141 struct lttng_kernel_context __user
*ucontext_param
,
142 struct lttng_ctx
**ctx
, struct ltt_session
*session
)
144 struct lttng_kernel_context context_param
;
146 if (session
->been_active
)
149 if (copy_from_user(&context_param
, ucontext_param
, sizeof(context_param
)))
152 switch (context_param
.ctx
) {
153 case LTTNG_KERNEL_CONTEXT_PID
:
154 return lttng_add_pid_to_ctx(ctx
);
155 case LTTNG_KERNEL_CONTEXT_PERF_COUNTER
:
157 case LTTNG_KERNEL_CONTEXT_COMM
:
158 return lttng_add_comm_to_ctx(ctx
);
165 * lttng_ioctl - lttng syscall through ioctl
171 * This ioctl implements lttng commands:
172 * LTTNG_KERNEL_SESSION
173 * Returns a LTTng trace session file descriptor
174 * LTTNG_KERNEL_TRACER_VERSION
175 * Returns the LTTng kernel tracer version
176 * LTTNG_KERNEL_TRACEPOINT_LIST
177 * Returns a file descriptor listing available tracepoints
179 * The returned session will be deleted when its file descriptor is closed.
182 long lttng_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
185 case LTTNG_KERNEL_SESSION
:
186 return lttng_abi_create_session();
187 case LTTNG_KERNEL_TRACER_VERSION
:
188 return lttng_abi_tracer_version(file
,
189 (struct lttng_kernel_tracer_version __user
*) arg
);
190 case LTTNG_KERNEL_TRACEPOINT_LIST
:
191 return lttng_abi_tracepoint_list();
197 static const struct file_operations lttng_fops
= {
198 .unlocked_ioctl
= lttng_ioctl
,
200 .compat_ioctl
= lttng_ioctl
,
205 * We tolerate no failure in this function (if one happens, we print a dmesg
206 * error, but cannot return any error, because the channel information is
210 void lttng_metadata_create_events(struct file
*channel_file
)
212 struct ltt_channel
*channel
= channel_file
->private_data
;
213 static struct lttng_kernel_event metadata_params
= {
214 .instrumentation
= LTTNG_KERNEL_TRACEPOINT
,
215 .name
= "lttng_metadata",
217 struct ltt_event
*event
;
221 * We tolerate no failure path after event creation. It will stay
222 * invariant for the rest of the session.
224 event
= ltt_event_create(channel
, &metadata_params
, NULL
);
233 return; /* not allowed to return error */
237 int lttng_abi_create_channel(struct file
*session_file
,
238 struct lttng_kernel_channel __user
*uchan_param
,
239 enum channel_type channel_type
)
241 struct ltt_session
*session
= session_file
->private_data
;
242 const struct file_operations
*fops
;
243 const char *transport_name
;
244 struct ltt_channel
*chan
;
245 struct file
*chan_file
;
246 struct lttng_kernel_channel chan_param
;
250 if (copy_from_user(&chan_param
, uchan_param
, sizeof(chan_param
)))
252 chan_fd
= get_unused_fd();
257 chan_file
= anon_inode_getfile("[lttng_channel]",
260 if (IS_ERR(chan_file
)) {
261 ret
= PTR_ERR(chan_file
);
264 switch (channel_type
) {
265 case PER_CPU_CHANNEL
:
266 transport_name
= chan_param
.overwrite
?
267 "relay-overwrite" : "relay-discard";
268 fops
= <tng_channel_fops
;
270 case METADATA_CHANNEL
:
271 transport_name
= "relay-metadata";
272 fops
= <tng_metadata_fops
;
275 transport_name
= "<unknown>";
279 * We tolerate no failure path after channel creation. It will stay
280 * invariant for the rest of the session.
282 chan
= ltt_channel_create(session
, transport_name
, NULL
,
283 chan_param
.subbuf_size
,
284 chan_param
.num_subbuf
,
285 chan_param
.switch_timer_interval
,
286 chan_param
.read_timer_interval
);
291 chan
->file
= chan_file
;
292 chan_file
->private_data
= chan
;
293 fd_install(chan_fd
, chan_file
);
294 if (channel_type
== METADATA_CHANNEL
) {
295 session
->metadata
= chan
;
296 lttng_metadata_create_events(chan_file
);
299 /* The channel created holds a reference on the session */
300 atomic_long_inc(&session_file
->f_count
);
307 put_unused_fd(chan_fd
);
313 * lttng_session_ioctl - lttng session fd ioctl
319 * This ioctl implements lttng commands:
320 * LTTNG_KERNEL_CHANNEL
321 * Returns a LTTng channel file descriptor
322 * LTTNG_KERNEL_SESSION_START
323 * Starts tracing session
324 * LTTNG_KERNEL_SESSION_STOP
325 * Stops tracing session
326 * LTTNG_KERNEL_METADATA
327 * Returns a LTTng metadata file descriptor
329 * The returned channel will be deleted when its file descriptor is closed.
332 long lttng_session_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
334 struct ltt_session
*session
= file
->private_data
;
337 case LTTNG_KERNEL_CHANNEL
:
338 return lttng_abi_create_channel(file
,
339 (struct lttng_kernel_channel __user
*) arg
,
341 case LTTNG_KERNEL_SESSION_START
:
342 return ltt_session_start(session
);
343 case LTTNG_KERNEL_SESSION_STOP
:
344 return ltt_session_stop(session
);
345 case LTTNG_KERNEL_METADATA
:
346 return lttng_abi_create_channel(file
,
347 (struct lttng_kernel_channel __user
*) arg
,
355 * Called when the last file reference is dropped.
357 * Big fat note: channels and events are invariant for the whole session after
358 * their creation. So this session destruction also destroys all channel and
359 * event structures specific to this session (they are not destroyed when their
360 * individual file is released).
363 int lttng_session_release(struct inode
*inode
, struct file
*file
)
365 struct ltt_session
*session
= file
->private_data
;
368 ltt_session_destroy(session
);
372 static const struct file_operations lttng_session_fops
= {
373 .release
= lttng_session_release
,
374 .unlocked_ioctl
= lttng_session_ioctl
,
376 .compat_ioctl
= lttng_session_ioctl
,
381 int lttng_abi_open_stream(struct file
*channel_file
)
383 struct ltt_channel
*channel
= channel_file
->private_data
;
384 struct lib_ring_buffer
*buf
;
386 struct file
*stream_file
;
388 buf
= channel
->ops
->buffer_read_open(channel
->chan
);
392 stream_fd
= get_unused_fd();
397 stream_file
= anon_inode_getfile("[lttng_stream]",
398 &lib_ring_buffer_file_operations
,
400 if (IS_ERR(stream_file
)) {
401 ret
= PTR_ERR(stream_file
);
405 * OPEN_FMODE, called within anon_inode_getfile/alloc_file, don't honor
406 * FMODE_LSEEK, FMODE_PREAD nor FMODE_PWRITE. We need to read from this
407 * file descriptor, so we set FMODE_PREAD here.
409 stream_file
->f_mode
|= FMODE_PREAD
;
410 fd_install(stream_fd
, stream_file
);
412 * The stream holds a reference to the channel within the generic ring
413 * buffer library, so no need to hold a refcount on the channel and
414 * session files here.
419 put_unused_fd(stream_fd
);
421 channel
->ops
->buffer_read_close(buf
);
426 int lttng_abi_create_event(struct file
*channel_file
,
427 struct lttng_kernel_event __user
*uevent_param
)
429 struct ltt_channel
*channel
= channel_file
->private_data
;
430 struct ltt_event
*event
;
431 struct lttng_kernel_event event_param
;
433 struct file
*event_file
;
435 if (copy_from_user(&event_param
, uevent_param
, sizeof(event_param
)))
437 event_param
.name
[LTTNG_SYM_NAME_LEN
- 1] = '\0';
438 switch (event_param
.instrumentation
) {
439 case LTTNG_KERNEL_KPROBE
:
440 event_param
.u
.kprobe
.symbol_name
[LTTNG_SYM_NAME_LEN
- 1] = '\0';
442 case LTTNG_KERNEL_FUNCTION
:
443 event_param
.u
.ftrace
.symbol_name
[LTTNG_SYM_NAME_LEN
- 1] = '\0';
448 event_fd
= get_unused_fd();
453 event_file
= anon_inode_getfile("[lttng_event]",
456 if (IS_ERR(event_file
)) {
457 ret
= PTR_ERR(event_file
);
461 * We tolerate no failure path after event creation. It will stay
462 * invariant for the rest of the session.
464 event
= ltt_event_create(channel
, &event_param
, NULL
);
469 event_file
->private_data
= event
;
470 fd_install(event_fd
, event_file
);
471 /* The event holds a reference on the channel */
472 atomic_long_inc(&channel_file
->f_count
);
478 put_unused_fd(event_fd
);
484 * lttng_channel_ioctl - lttng syscall through ioctl
490 * This ioctl implements lttng commands:
491 * LTTNG_KERNEL_STREAM
492 * Returns an event stream file descriptor or failure.
493 * (typically, one event stream records events from one CPU)
495 * Returns an event file descriptor or failure.
496 * LTTNG_KERNEL_CONTEXT
497 * Prepend a context field to each event in the channel
499 * Channel and event file descriptors also hold a reference on the session.
502 long lttng_channel_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
504 struct ltt_channel
*channel
= file
->private_data
;
507 case LTTNG_KERNEL_STREAM
:
508 return lttng_abi_open_stream(file
);
509 case LTTNG_KERNEL_EVENT
:
510 return lttng_abi_create_event(file
, (struct lttng_kernel_event __user
*) arg
);
511 case LTTNG_KERNEL_CONTEXT
:
512 return lttng_abi_add_context(file
,
513 (struct lttng_kernel_context __user
*) arg
,
514 &channel
->ctx
, channel
->session
);
521 * lttng_metadata_ioctl - lttng syscall through ioctl
527 * This ioctl implements lttng commands:
528 * LTTNG_KERNEL_STREAM
529 * Returns an event stream file descriptor or failure.
531 * Channel and event file descriptors also hold a reference on the session.
534 long lttng_metadata_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
537 case LTTNG_KERNEL_STREAM
:
538 return lttng_abi_open_stream(file
);
547 * lttng_channel_poll - lttng stream addition/removal monitoring
552 unsigned int lttng_channel_poll(struct file
*file
, poll_table
*wait
)
554 struct ltt_channel
*channel
= file
->private_data
;
555 unsigned int mask
= 0;
557 if (file
->f_mode
& FMODE_READ
) {
558 poll_wait_set_exclusive(wait
);
559 poll_wait(file
, &channel
->notify_wait
, wait
);
561 /* TODO: identify when the channel is being finalized. */
565 return POLLIN
| POLLRDNORM
;
573 int lttng_channel_release(struct inode
*inode
, struct file
*file
)
575 struct ltt_channel
*channel
= file
->private_data
;
578 fput(channel
->session
->file
);
582 static const struct file_operations lttng_channel_fops
= {
583 .release
= lttng_channel_release
,
586 .poll
= lttng_channel_poll
,
588 .unlocked_ioctl
= lttng_channel_ioctl
,
590 .compat_ioctl
= lttng_channel_ioctl
,
594 static const struct file_operations lttng_metadata_fops
= {
595 .release
= lttng_channel_release
,
596 .unlocked_ioctl
= lttng_metadata_ioctl
,
598 .compat_ioctl
= lttng_metadata_ioctl
,
603 * lttng_event_ioctl - lttng syscall through ioctl
609 * This ioctl implements lttng commands:
610 * LTTNG_KERNEL_STREAM
611 * Returns an event stream file descriptor or failure.
612 * (typically, one event stream records events from one CPU)
614 * Returns an event file descriptor or failure.
615 * LTTNG_KERNEL_CONTEXT
616 * Prepend a context field to each record of this event
619 long lttng_event_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
621 struct ltt_event
*event
= file
->private_data
;
624 case LTTNG_KERNEL_CONTEXT
:
625 return lttng_abi_add_context(file
,
626 (struct lttng_kernel_context __user
*) arg
,
627 &event
->ctx
, event
->chan
->session
);
634 int lttng_event_release(struct inode
*inode
, struct file
*file
)
636 struct ltt_event
*event
= file
->private_data
;
639 fput(event
->chan
->file
);
643 /* TODO: filter control ioctl */
644 static const struct file_operations lttng_event_fops
= {
645 .release
= lttng_event_release
,
646 .unlocked_ioctl
= lttng_event_ioctl
,
648 .compat_ioctl
= lttng_event_ioctl
,
652 int __init
ltt_debugfs_abi_init(void)
656 wrapper_vmalloc_sync_all();
657 lttng_dentry
= debugfs_create_file("lttng", S_IWUSR
, NULL
, NULL
,
659 if (IS_ERR(lttng_dentry
) || !lttng_dentry
) {
660 printk(KERN_ERR
"Error creating LTTng control file\n");
668 void __exit
ltt_debugfs_abi_exit(void)
670 debugfs_remove(lttng_dentry
);