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