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