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