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