Fix metadata, change ABI (for better future extensibility)
[lttng-modules.git] / ltt-debugfs-abi.c
1 /*
2 * ltt-debugfs-abi.c
3 *
4 * Copyright 2010 (c) - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 *
6 * LTTng debugfs ABI
7 *
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.
23 */
24
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"
36
37 /*
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.
40 */
41
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;
48
49 enum channel_type {
50 PER_CPU_CHANNEL,
51 METADATA_CHANNEL,
52 };
53
54 static
55 int lttng_abi_create_session(void)
56 {
57 struct ltt_session *session;
58 struct file *session_file;
59 int session_fd, ret;
60
61 session = ltt_session_create();
62 if (!session)
63 return -ENOMEM;
64 session_fd = get_unused_fd();
65 if (session_fd < 0) {
66 ret = session_fd;
67 goto fd_error;
68 }
69 session_file = anon_inode_getfile("[lttng_session]",
70 &lttng_session_fops,
71 session, O_RDWR);
72 if (IS_ERR(session_file)) {
73 ret = PTR_ERR(session_file);
74 goto file_error;
75 }
76 session->file = session_file;
77 fd_install(session_fd, session_file);
78 return session_fd;
79
80 file_error:
81 put_unused_fd(session_fd);
82 fd_error:
83 ltt_session_destroy(session);
84 return ret;
85 }
86
87 static
88 long lttng_abi_tracer_version(struct file *file,
89 struct lttng_kernel_tracer_version __user *uversion_param)
90 {
91 struct lttng_kernel_tracer_version v;
92
93 v.version = LTTNG_VERSION;
94 v.patchlevel = LTTNG_PATCHLEVEL;
95 v.sublevel = LTTNG_SUBLEVEL;
96
97 if (copy_to_user(uversion_param, &v, sizeof(v)))
98 return -EFAULT;
99 return 0;
100 }
101
102 /**
103 * lttng_ioctl - lttng syscall through ioctl
104 *
105 * @file: the file
106 * @cmd: the command
107 * @arg: command arg
108 *
109 * This ioctl implements lttng commands:
110 * LTTNG_KERNEL_SESSION
111 * Returns a LTTng trace session file descriptor
112 *
113 * The returned session will be deleted when its file descriptor is closed.
114 */
115 static
116 long lttng_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
117 {
118 switch (cmd) {
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);
124 default:
125 return -ENOIOCTLCMD;
126 }
127 }
128
129 static const struct file_operations lttng_fops = {
130 .unlocked_ioctl = lttng_ioctl,
131 #ifdef CONFIG_COMPAT
132 .compat_ioctl = lttng_ioctl,
133 #endif
134 };
135
136 /*
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
139 * invariant.
140 */
141 static
142 void lttng_metadata_create_events(struct file *channel_file)
143 {
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",
148 };
149 struct ltt_event *event;
150 int ret;
151
152 /*
153 * We tolerate no failure path after event creation. It will stay
154 * invariant for the rest of the session.
155 */
156 event = ltt_event_create(channel, &metadata_params, NULL);
157 if (!event) {
158 goto create_error;
159 ret = -EEXIST;
160 }
161 return;
162
163 create_error:
164 WARN_ON(1);
165 return; /* not allowed to return error */
166 }
167
168 static
169 int lttng_abi_create_channel(struct file *session_file,
170 struct lttng_kernel_channel __user *uchan_param,
171 enum channel_type channel_type)
172 {
173 struct ltt_session *session = session_file->private_data;
174 const struct file_operations *fops;
175 const char *transport_name;
176 struct ltt_channel *chan;
177 struct file *chan_file;
178 struct lttng_kernel_channel chan_param;
179 int chan_fd;
180 int ret = 0;
181
182 if (copy_from_user(&chan_param, uchan_param, sizeof(chan_param)))
183 return -EFAULT;
184 chan_fd = get_unused_fd();
185 if (chan_fd < 0) {
186 ret = chan_fd;
187 goto fd_error;
188 }
189 chan_file = anon_inode_getfile("[lttng_channel]",
190 &lttng_channel_fops,
191 NULL, O_RDWR);
192 if (IS_ERR(chan_file)) {
193 ret = PTR_ERR(chan_file);
194 goto file_error;
195 }
196 switch (channel_type) {
197 case PER_CPU_CHANNEL:
198 transport_name = chan_param.overwrite ?
199 "relay-overwrite" : "relay-discard";
200 fops = &lttng_channel_fops;
201 break;
202 case METADATA_CHANNEL:
203 transport_name = "relay-metadata";
204 fops = &lttng_metadata_fops;
205 break;
206 default:
207 transport_name = "<unknown>";
208 break;
209 }
210 /*
211 * We tolerate no failure path after channel creation. It will stay
212 * invariant for the rest of the session.
213 */
214 chan = ltt_channel_create(session, transport_name, NULL,
215 chan_param.subbuf_size,
216 chan_param.num_subbuf,
217 chan_param.switch_timer_interval,
218 chan_param.read_timer_interval);
219 if (!chan) {
220 ret = -EINVAL;
221 goto chan_error;
222 }
223 chan->file = chan_file;
224 chan_file->private_data = chan;
225 fd_install(chan_fd, chan_file);
226 if (channel_type == METADATA_CHANNEL) {
227 session->metadata = chan;
228 lttng_metadata_create_events(chan_file);
229 }
230
231 /* The channel created holds a reference on the session */
232 atomic_long_inc(&session_file->f_count);
233
234 return chan_fd;
235
236 chan_error:
237 fput(chan_file);
238 file_error:
239 put_unused_fd(chan_fd);
240 fd_error:
241 return ret;
242 }
243
244 /**
245 * lttng_session_ioctl - lttng session fd ioctl
246 *
247 * @file: the file
248 * @cmd: the command
249 * @arg: command arg
250 *
251 * This ioctl implements lttng commands:
252 * LTTNG_KERNEL_CHANNEL
253 * Returns a LTTng channel file descriptor
254 *
255 * The returned channel will be deleted when its file descriptor is closed.
256 */
257 static
258 long lttng_session_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
259 {
260 struct ltt_session *session = file->private_data;
261
262 switch (cmd) {
263 case LTTNG_KERNEL_CHANNEL:
264 return lttng_abi_create_channel(file,
265 (struct lttng_kernel_channel __user *) arg,
266 PER_CPU_CHANNEL);
267 case LTTNG_KERNEL_SESSION_START:
268 return ltt_session_start(session);
269 case LTTNG_KERNEL_SESSION_STOP:
270 return ltt_session_stop(session);
271 case LTTNG_KERNEL_METADATA:
272 return lttng_abi_create_channel(file,
273 (struct lttng_kernel_channel __user *) arg,
274 METADATA_CHANNEL);
275 default:
276 return -ENOIOCTLCMD;
277 }
278 }
279
280 /*
281 * Called when the last file reference is dropped.
282 *
283 * Big fat note: channels and events are invariant for the whole session after
284 * their creation. So this session destruction also destroys all channel and
285 * event structures specific to this session (they are not destroyed when their
286 * individual file is released).
287 */
288 static
289 int lttng_session_release(struct inode *inode, struct file *file)
290 {
291 struct ltt_session *session = file->private_data;
292
293 if (session)
294 ltt_session_destroy(session);
295 return 0;
296 }
297
298 static const struct file_operations lttng_session_fops = {
299 .release = lttng_session_release,
300 .unlocked_ioctl = lttng_session_ioctl,
301 #ifdef CONFIG_COMPAT
302 .compat_ioctl = lttng_session_ioctl,
303 #endif
304 };
305
306 static
307 int lttng_abi_open_stream(struct file *channel_file)
308 {
309 struct ltt_channel *channel = channel_file->private_data;
310 struct lib_ring_buffer *buf;
311 int stream_fd, ret;
312 struct file *stream_file;
313
314 buf = channel->ops->buffer_read_open(channel->chan);
315 if (!buf)
316 return -ENOENT;
317
318 stream_fd = get_unused_fd();
319 if (stream_fd < 0) {
320 ret = stream_fd;
321 goto fd_error;
322 }
323 stream_file = anon_inode_getfile("[lttng_stream]",
324 &lib_ring_buffer_file_operations,
325 buf, O_RDWR);
326 if (IS_ERR(stream_file)) {
327 ret = PTR_ERR(stream_file);
328 goto file_error;
329 }
330 /*
331 * OPEN_FMODE, called within anon_inode_getfile/alloc_file, don't honor
332 * FMODE_LSEEK, FMODE_PREAD nor FMODE_PWRITE. We need to read from this
333 * file descriptor, so we set FMODE_PREAD here.
334 */
335 stream_file->f_mode |= FMODE_PREAD;
336 fd_install(stream_fd, stream_file);
337 /*
338 * The stream holds a reference to the channel within the generic ring
339 * buffer library, so no need to hold a refcount on the channel and
340 * session files here.
341 */
342 return stream_fd;
343
344 file_error:
345 put_unused_fd(stream_fd);
346 fd_error:
347 channel->ops->buffer_read_close(buf);
348 return ret;
349 }
350
351 static
352 int lttng_abi_create_event(struct file *channel_file,
353 struct lttng_kernel_event __user *uevent_param)
354 {
355 struct ltt_channel *channel = channel_file->private_data;
356 struct ltt_event *event;
357 struct lttng_kernel_event event_param;
358 int event_fd, ret;
359 struct file *event_file;
360
361 if (copy_from_user(&event_param, uevent_param, sizeof(event_param)))
362 return -EFAULT;
363 event_param.name[LTTNG_SYM_NAME_LEN - 1] = '\0';
364 switch (event_param.instrumentation) {
365 case LTTNG_KERNEL_KPROBES:
366 event_param.u.kprobe.symbol_name[LTTNG_SYM_NAME_LEN - 1] = '\0';
367 break;
368 case LTTNG_KERNEL_FUNCTION_TRACER:
369 event_param.u.ftrace.symbol_name[LTTNG_SYM_NAME_LEN - 1] = '\0';
370 break;
371 default:
372 break;
373 }
374 event_fd = get_unused_fd();
375 if (event_fd < 0) {
376 ret = event_fd;
377 goto fd_error;
378 }
379 event_file = anon_inode_getfile("[lttng_event]",
380 &lttng_event_fops,
381 NULL, O_RDWR);
382 if (IS_ERR(event_file)) {
383 ret = PTR_ERR(event_file);
384 goto file_error;
385 }
386 /*
387 * We tolerate no failure path after event creation. It will stay
388 * invariant for the rest of the session.
389 */
390 event = ltt_event_create(channel, &event_param, NULL);
391 if (!event) {
392 ret = -EEXIST;
393 goto event_error;
394 }
395 event_file->private_data = event;
396 fd_install(event_fd, event_file);
397 /* The event holds a reference on the channel */
398 atomic_long_inc(&channel_file->f_count);
399 return event_fd;
400
401 event_error:
402 fput(event_file);
403 file_error:
404 put_unused_fd(event_fd);
405 fd_error:
406 return ret;
407 }
408
409 /**
410 * lttng_channel_ioctl - lttng syscall through ioctl
411 *
412 * @file: the file
413 * @cmd: the command
414 * @arg: command arg
415 *
416 * This ioctl implements lttng commands:
417 * LTTNG_KERNEL_STREAM
418 * Returns an event stream file descriptor or failure.
419 * (typically, one event stream records events from one CPU)
420 * LTTNG_KERNEL_EVENT
421 * Returns an event file descriptor or failure.
422 *
423 * Channel and event file descriptors also hold a reference on the session.
424 */
425 static
426 long lttng_channel_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
427 {
428 switch (cmd) {
429 case LTTNG_KERNEL_STREAM:
430 return lttng_abi_open_stream(file);
431 case LTTNG_KERNEL_EVENT:
432 return lttng_abi_create_event(file, (struct lttng_kernel_event __user *) arg);
433 default:
434 return -ENOIOCTLCMD;
435 }
436 }
437
438 /**
439 * lttng_metadata_ioctl - lttng syscall through ioctl
440 *
441 * @file: the file
442 * @cmd: the command
443 * @arg: command arg
444 *
445 * This ioctl implements lttng commands:
446 * LTTNG_KERNEL_STREAM
447 * Returns an event stream file descriptor or failure.
448 *
449 * Channel and event file descriptors also hold a reference on the session.
450 */
451 static
452 long lttng_metadata_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
453 {
454 switch (cmd) {
455 case LTTNG_KERNEL_STREAM:
456 return lttng_abi_open_stream(file);
457 default:
458 return -ENOIOCTLCMD;
459 }
460 }
461
462 /* TODO: poll */
463 #if 0
464 /**
465 * lttng_channel_poll - lttng stream addition/removal monitoring
466 *
467 * @file: the file
468 * @wait: poll table
469 */
470 unsigned int lttng_channel_poll(struct file *file, poll_table *wait)
471 {
472 struct ltt_channel *channel = file->private_data;
473 unsigned int mask = 0;
474
475 if (file->f_mode & FMODE_READ) {
476 poll_wait_set_exclusive(wait);
477 poll_wait(file, &channel->notify_wait, wait);
478
479 /* TODO: identify when the channel is being finalized. */
480 if (finalized)
481 return POLLHUP;
482 else
483 return POLLIN | POLLRDNORM;
484 }
485 return mask;
486
487 }
488 #endif //0
489
490 static
491 int lttng_channel_release(struct inode *inode, struct file *file)
492 {
493 struct ltt_channel *channel = file->private_data;
494
495 if (channel)
496 fput(channel->session->file);
497 return 0;
498 }
499
500 static const struct file_operations lttng_channel_fops = {
501 .release = lttng_channel_release,
502 /* TODO */
503 #if 0
504 .poll = lttng_channel_poll,
505 #endif //0
506 .unlocked_ioctl = lttng_channel_ioctl,
507 #ifdef CONFIG_COMPAT
508 .compat_ioctl = lttng_channel_ioctl,
509 #endif
510 };
511
512 static const struct file_operations lttng_metadata_fops = {
513 .release = lttng_channel_release,
514 .unlocked_ioctl = lttng_metadata_ioctl,
515 #ifdef CONFIG_COMPAT
516 .compat_ioctl = lttng_metadata_ioctl,
517 #endif
518 };
519
520 static
521 int lttng_event_release(struct inode *inode, struct file *file)
522 {
523 struct ltt_event *event = file->private_data;
524
525 if (event)
526 fput(event->chan->file);
527 return 0;
528 }
529
530 /* TODO: filter control ioctl */
531 static const struct file_operations lttng_event_fops = {
532 .release = lttng_event_release,
533 };
534
535 int __init ltt_debugfs_abi_init(void)
536 {
537 int ret = 0;
538
539 wrapper_vmalloc_sync_all();
540 lttng_dentry = debugfs_create_file("lttng", S_IWUSR, NULL, NULL,
541 &lttng_fops);
542 if (IS_ERR(lttng_dentry) || !lttng_dentry) {
543 printk(KERN_ERR "Error creating LTTng control file\n");
544 ret = -ENOMEM;
545 goto error;
546 }
547 error:
548 return ret;
549 }
550
551 void __exit ltt_debugfs_abi_exit(void)
552 {
553 debugfs_remove(lttng_dentry);
554 }
This page took 0.04015 seconds and 5 git commands to generate.