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