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 long lttng_abi_tracer_version(struct file
*file
,
89 struct lttng_kernel_tracer_version __user
*uversion_param
)
91 struct lttng_kernel_tracer_version v
;
93 v
.version
= LTTNG_VERSION
;
94 v
.patchlevel
= LTTNG_PATCHLEVEL
;
95 v
.sublevel
= LTTNG_SUBLEVEL
;
97 if (copy_to_user(uversion_param
, &v
, sizeof(v
)))
103 * lttng_ioctl - lttng syscall through ioctl
109 * This ioctl implements lttng commands:
110 * LTTNG_KERNEL_SESSION
111 * Returns a LTTng trace session file descriptor
113 * The returned session will be deleted when its file descriptor is closed.
116 long lttng_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
119 case LTTNG_KERNEL_SESSION
:
120 return lttng_abi_create_session();
121 case LTTNG_KERNEL_TRACER_VERSION
:
122 return lttng_abi_tracer_version(file
,
123 (struct lttng_kernel_tracer_version __user
*) arg
);
129 static const struct file_operations lttng_fops
= {
130 .unlocked_ioctl
= lttng_ioctl
,
132 .compat_ioctl
= lttng_ioctl
,
137 * We tolerate no failure in this function (if one happens, we print a dmesg
138 * error, but cannot return any error, because the channel information is
142 void lttng_metadata_create_events(struct file
*channel_file
)
144 struct ltt_channel
*channel
= channel_file
->private_data
;
145 static struct lttng_kernel_event metadata_params
= {
146 .instrumentation
= LTTNG_KERNEL_TRACEPOINTS
,
147 .name
= "lttng_metadata",
149 char *event_name
= "lttng_metadata";
150 struct ltt_event
*event
;
154 * We tolerate no failure path after event creation. It will stay
155 * invariant for the rest of the session.
157 event
= ltt_event_create(channel
, event_name
, &metadata_params
, NULL
);
166 return; /* not allowed to return error */
170 int lttng_abi_create_channel(struct file
*session_file
,
171 struct lttng_kernel_channel __user
*uchan_param
,
172 enum channel_type channel_type
)
174 struct ltt_session
*session
= session_file
->private_data
;
175 const struct file_operations
*fops
;
176 const char *transport_name
;
177 struct ltt_channel
*chan
;
178 struct file
*chan_file
;
179 struct lttng_kernel_channel chan_param
;
183 if (copy_from_user(&chan_param
, uchan_param
, sizeof(chan_param
)))
185 chan_fd
= get_unused_fd();
190 chan_file
= anon_inode_getfile("[lttng_channel]",
193 if (IS_ERR(chan_file
)) {
194 ret
= PTR_ERR(chan_file
);
197 switch (channel_type
) {
198 case PER_CPU_CHANNEL
:
199 transport_name
= chan_param
.overwrite
?
200 "relay-overwrite" : "relay-discard";
201 fops
= <tng_channel_fops
;
203 case METADATA_CHANNEL
:
204 transport_name
= "relay-metadata";
205 fops
= <tng_metadata_fops
;
208 transport_name
= "<unknown>";
212 * We tolerate no failure path after channel creation. It will stay
213 * invariant for the rest of the session.
215 chan
= ltt_channel_create(session
, transport_name
, NULL
,
216 chan_param
.subbuf_size
,
217 chan_param
.num_subbuf
,
218 chan_param
.switch_timer_interval
,
219 chan_param
.read_timer_interval
);
224 chan
->file
= chan_file
;
225 chan_file
->private_data
= chan
;
226 fd_install(chan_fd
, chan_file
);
227 if (channel_type
== METADATA_CHANNEL
) {
228 session
->metadata
= chan
;
229 lttng_metadata_create_events(chan_file
);
232 /* The channel created holds a reference on the session */
233 atomic_long_inc(&session_file
->f_count
);
240 put_unused_fd(chan_fd
);
246 * lttng_session_ioctl - lttng session fd ioctl
252 * This ioctl implements lttng commands:
253 * LTTNG_KERNEL_CHANNEL
254 * Returns a LTTng channel file descriptor
256 * The returned channel will be deleted when its file descriptor is closed.
259 long lttng_session_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
261 struct ltt_session
*session
= file
->private_data
;
264 case LTTNG_KERNEL_CHANNEL
:
265 return lttng_abi_create_channel(file
,
266 (struct lttng_kernel_channel __user
*) arg
,
268 case LTTNG_KERNEL_SESSION_START
:
269 return ltt_session_start(session
);
270 case LTTNG_KERNEL_SESSION_STOP
:
271 return ltt_session_stop(session
);
272 case LTTNG_KERNEL_METADATA
:
273 return lttng_abi_create_channel(file
,
274 (struct lttng_kernel_channel __user
*) arg
,
282 * Called when the last file reference is dropped.
284 * Big fat note: channels and events are invariant for the whole session after
285 * their creation. So this session destruction also destroys all channel and
286 * event structures specific to this session (they are not destroyed when their
287 * individual file is released).
290 int lttng_session_release(struct inode
*inode
, struct file
*file
)
292 struct ltt_session
*session
= file
->private_data
;
295 ltt_session_destroy(session
);
299 static const struct file_operations lttng_session_fops
= {
300 .release
= lttng_session_release
,
301 .unlocked_ioctl
= lttng_session_ioctl
,
303 .compat_ioctl
= lttng_session_ioctl
,
308 int lttng_abi_open_stream(struct file
*channel_file
)
310 struct ltt_channel
*channel
= channel_file
->private_data
;
311 struct lib_ring_buffer
*buf
;
313 struct file
*stream_file
;
315 buf
= channel
->ops
->buffer_read_open(channel
->chan
);
319 stream_fd
= get_unused_fd();
324 stream_file
= anon_inode_getfile("[lttng_stream]",
325 &lib_ring_buffer_file_operations
,
327 if (IS_ERR(stream_file
)) {
328 ret
= PTR_ERR(stream_file
);
332 * OPEN_FMODE, called within anon_inode_getfile/alloc_file, don't honor
333 * FMODE_LSEEK, FMODE_PREAD nor FMODE_PWRITE. We need to read from this
334 * file descriptor, so we set FMODE_PREAD here.
336 stream_file
->f_mode
|= FMODE_PREAD
;
337 fd_install(stream_fd
, stream_file
);
339 * The stream holds a reference to the channel within the generic ring
340 * buffer library, so no need to hold a refcount on the channel and
341 * session files here.
346 put_unused_fd(stream_fd
);
348 channel
->ops
->buffer_read_close(buf
);
353 int lttng_abi_create_event(struct file
*channel_file
,
354 struct lttng_kernel_event __user
*uevent_param
)
356 struct ltt_channel
*channel
= channel_file
->private_data
;
357 struct ltt_event
*event
;
359 struct lttng_kernel_event event_param
;
361 struct file
*event_file
;
363 if (copy_from_user(&event_param
, uevent_param
, sizeof(event_param
)))
365 event_name
= kmalloc(PATH_MAX
, GFP_KERNEL
);
368 if (strncpy_from_user(event_name
, uevent_param
->name
, PATH_MAX
) < 0) {
372 event_name
[PATH_MAX
- 1] = '\0';
373 switch (event_param
.instrumentation
) {
374 case LTTNG_KERNEL_KPROBES
:
375 event_param
.u
.kprobe
.symbol_name
[LTTNG_SYM_NAME_LEN
- 1] = '\0';
377 case LTTNG_KERNEL_FUNCTION_TRACER
:
378 event_param
.u
.ftrace
.symbol_name
[LTTNG_SYM_NAME_LEN
- 1] = '\0';
383 event_fd
= get_unused_fd();
388 event_file
= anon_inode_getfile("[lttng_event]",
391 if (IS_ERR(event_file
)) {
392 ret
= PTR_ERR(event_file
);
396 * We tolerate no failure path after event creation. It will stay
397 * invariant for the rest of the session.
399 event
= ltt_event_create(channel
, event_name
, &event_param
, NULL
);
404 event_file
->private_data
= event
;
405 fd_install(event_fd
, event_file
);
406 /* The event holds a reference on the channel */
407 atomic_long_inc(&channel_file
->f_count
);
414 put_unused_fd(event_fd
);
422 * lttng_channel_ioctl - lttng syscall through ioctl
428 * This ioctl implements lttng commands:
429 * LTTNG_KERNEL_STREAM
430 * Returns an event stream file descriptor or failure.
431 * (typically, one event stream records events from one CPU)
433 * Returns an event file descriptor or failure.
435 * Channel and event file descriptors also hold a reference on the session.
438 long lttng_channel_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
441 case LTTNG_KERNEL_STREAM
:
442 return lttng_abi_open_stream(file
);
443 case LTTNG_KERNEL_EVENT
:
444 return lttng_abi_create_event(file
, (struct lttng_kernel_event __user
*) arg
);
451 * lttng_metadata_ioctl - lttng syscall through ioctl
457 * This ioctl implements lttng commands:
458 * LTTNG_KERNEL_STREAM
459 * Returns an event stream file descriptor or failure.
461 * Channel and event file descriptors also hold a reference on the session.
464 long lttng_metadata_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
467 case LTTNG_KERNEL_STREAM
:
468 return lttng_abi_open_stream(file
);
477 * lttng_channel_poll - lttng stream addition/removal monitoring
482 unsigned int lttng_channel_poll(struct file
*file
, poll_table
*wait
)
484 struct ltt_channel
*channel
= file
->private_data
;
485 unsigned int mask
= 0;
487 if (file
->f_mode
& FMODE_READ
) {
488 poll_wait_set_exclusive(wait
);
489 poll_wait(file
, &channel
->notify_wait
, wait
);
491 /* TODO: identify when the channel is being finalized. */
495 return POLLIN
| POLLRDNORM
;
503 int lttng_channel_release(struct inode
*inode
, struct file
*file
)
505 struct ltt_channel
*channel
= file
->private_data
;
508 fput(channel
->session
->file
);
512 static const struct file_operations lttng_channel_fops
= {
513 .release
= lttng_channel_release
,
516 .poll
= lttng_channel_poll
,
518 .unlocked_ioctl
= lttng_channel_ioctl
,
520 .compat_ioctl
= lttng_channel_ioctl
,
524 static const struct file_operations lttng_metadata_fops
= {
525 .release
= lttng_channel_release
,
526 .unlocked_ioctl
= lttng_metadata_ioctl
,
528 .compat_ioctl
= lttng_metadata_ioctl
,
533 int lttng_event_release(struct inode
*inode
, struct file
*file
)
535 struct ltt_event
*event
= file
->private_data
;
538 fput(event
->chan
->file
);
542 /* TODO: filter control ioctl */
543 static const struct file_operations lttng_event_fops
= {
544 .release
= lttng_event_release
,
547 int __init
ltt_debugfs_abi_init(void)
551 wrapper_vmalloc_sync_all();
552 lttng_dentry
= debugfs_create_file("lttng", S_IWUSR
, NULL
, NULL
,
554 if (IS_ERR(lttng_dentry
) || !lttng_dentry
) {
555 printk(KERN_ERR
"Error creating LTTng control file\n");
563 void __exit
ltt_debugfs_abi_exit(void)
565 debugfs_remove(lttng_dentry
);