Fix metadata event arg (null pointer exception)
[lttng-modules.git] / ltt-debugfs-abi.c
CommitLineData
baf20995
MD
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.
ad1c05e1
MD
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.
baf20995
MD
23 */
24
11b5a3c2 25#include <linux/module.h>
baf20995 26#include <linux/debugfs.h>
11b5a3c2
MD
27#include <linux/anon_inodes.h>
28#include <linux/file.h>
29#include <linux/uaccess.h>
30#include <linux/slab.h>
b13f3ebe 31#include "wrapper/vmalloc.h" /* for wrapper_vmalloc_sync_all() */
f3bc08c5 32#include "wrapper/ringbuffer/vfs.h"
11b5a3c2 33#include "ltt-debugfs-abi.h"
ad1c05e1 34#include "ltt-events.h"
80c16bcf 35#include "ltt-tracer.h"
baf20995
MD
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
42static struct dentry *lttng_dentry;
ad1c05e1
MD
43static const struct file_operations lttng_fops;
44static const struct file_operations lttng_session_fops;
45static const struct file_operations lttng_channel_fops;
5dbbdb43 46static const struct file_operations lttng_metadata_fops;
305d3e42 47static const struct file_operations lttng_event_fops;
baf20995 48
5dbbdb43
MD
49enum channel_type {
50 PER_CPU_CHANNEL,
5dbbdb43
MD
51 METADATA_CHANNEL,
52};
53
ad1c05e1 54static
baf20995
MD
55int lttng_abi_create_session(void)
56{
57 struct ltt_session *session;
c0e31d2e 58 struct file *session_file;
11b5a3c2 59 int session_fd, ret;
baf20995 60
653fe716 61 session = ltt_session_create();
baf20995
MD
62 if (!session)
63 return -ENOMEM;
1c25284c 64 session_fd = get_unused_fd();
baf20995
MD
65 if (session_fd < 0) {
66 ret = session_fd;
67 goto fd_error;
68 }
c0e31d2e 69 session_file = anon_inode_getfile("[lttng_session]",
ad1c05e1 70 &lttng_session_fops,
baf20995 71 session, O_RDWR);
c0e31d2e
MD
72 if (IS_ERR(session_file)) {
73 ret = PTR_ERR(session_file);
baf20995
MD
74 goto file_error;
75 }
c0e31d2e
MD
76 session->file = session_file;
77 fd_install(session_fd, session_file);
baf20995
MD
78 return session_fd;
79
80file_error:
81 put_unused_fd(session_fd);
82fd_error:
83 ltt_session_destroy(session);
84 return ret;
85}
86
80c16bcf
MD
87static
88long 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
ad1c05e1
MD
102/**
103 * lttng_ioctl - lttng syscall through ioctl
104 *
c0e31d2e 105 * @file: the file
ad1c05e1
MD
106 * @cmd: the command
107 * @arg: command arg
108 *
109 * This ioctl implements lttng commands:
38d024ae 110 * LTTNG_KERNEL_SESSION
ad1c05e1
MD
111 * Returns a LTTng trace session file descriptor
112 *
113 * The returned session will be deleted when its file descriptor is closed.
114 */
115static
c0e31d2e 116long lttng_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
ad1c05e1
MD
117{
118 switch (cmd) {
38d024ae 119 case LTTNG_KERNEL_SESSION:
ad1c05e1 120 return lttng_abi_create_session();
80c16bcf
MD
121 case LTTNG_KERNEL_TRACER_VERSION:
122 return lttng_abi_tracer_version(file,
123 (struct lttng_kernel_tracer_version __user *) arg);
ad1c05e1
MD
124 default:
125 return -ENOIOCTLCMD;
126 }
127}
128
ad1c05e1
MD
129static const struct file_operations lttng_fops = {
130 .unlocked_ioctl = lttng_ioctl,
131#ifdef CONFIG_COMPAT
03037b98 132 .compat_ioctl = lttng_ioctl,
ad1c05e1 133#endif
11b5a3c2 134};
ad1c05e1 135
5dbbdb43
MD
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 */
141static
142void lttng_metadata_create_events(struct file *channel_file)
143{
144 struct ltt_channel *channel = channel_file->private_data;
e70a4758
MD
145 static struct lttng_kernel_event metadata_params = {
146 .instrumentation = LTTNG_KERNEL_TRACEPOINTS,
147 .name = "lttng_metadata",
148 };
cd4bd11f 149 char *event_name = "lttng_metadata";
5dbbdb43
MD
150 struct ltt_event *event;
151 int ret;
5dbbdb43 152
5dbbdb43
MD
153 /*
154 * We tolerate no failure path after event creation. It will stay
155 * invariant for the rest of the session.
156 */
e70a4758 157 event = ltt_event_create(channel, event_name, &metadata_params, NULL);
5dbbdb43 158 if (!event) {
85a9ca7f 159 goto create_error;
5dbbdb43
MD
160 ret = -EEXIST;
161 }
162 return;
163
85a9ca7f 164create_error:
5dbbdb43
MD
165 WARN_ON(1);
166 return; /* not allowed to return error */
167}
168
169static
c0e31d2e 170int lttng_abi_create_channel(struct file *session_file,
38d024ae 171 struct lttng_kernel_channel __user *uchan_param,
5dbbdb43 172 enum channel_type channel_type)
baf20995 173{
c0e31d2e 174 struct ltt_session *session = session_file->private_data;
5dbbdb43
MD
175 const struct file_operations *fops;
176 const char *transport_name;
baf20995 177 struct ltt_channel *chan;
c0e31d2e 178 struct file *chan_file;
38d024ae 179 struct lttng_kernel_channel chan_param;
baf20995 180 int chan_fd;
ad1c05e1 181 int ret = 0;
baf20995 182
653fe716 183 if (copy_from_user(&chan_param, uchan_param, sizeof(chan_param)))
baf20995 184 return -EFAULT;
1c25284c 185 chan_fd = get_unused_fd();
baf20995
MD
186 if (chan_fd < 0) {
187 ret = chan_fd;
188 goto fd_error;
189 }
c0e31d2e 190 chan_file = anon_inode_getfile("[lttng_channel]",
ad1c05e1 191 &lttng_channel_fops,
03037b98 192 NULL, O_RDWR);
c0e31d2e
MD
193 if (IS_ERR(chan_file)) {
194 ret = PTR_ERR(chan_file);
baf20995
MD
195 goto file_error;
196 }
5dbbdb43
MD
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;
5dbbdb43 203 case METADATA_CHANNEL:
881833e3 204 transport_name = "relay-metadata";
5dbbdb43
MD
205 fops = &lttng_metadata_fops;
206 break;
207 default:
208 transport_name = "<unknown>";
209 break;
210 }
03037b98
MD
211 /*
212 * We tolerate no failure path after channel creation. It will stay
213 * invariant for the rest of the session.
214 */
5dbbdb43 215 chan = ltt_channel_create(session, transport_name, NULL,
11b5a3c2
MD
216 chan_param.subbuf_size,
217 chan_param.num_subbuf,
218 chan_param.switch_timer_interval,
219 chan_param.read_timer_interval);
03037b98 220 if (!chan) {
f3d01b96 221 ret = -EINVAL;
03037b98
MD
222 goto chan_error;
223 }
11b5a3c2 224 chan->file = chan_file;
c0e31d2e
MD
225 chan_file->private_data = chan;
226 fd_install(chan_fd, chan_file);
cd4bd11f 227 if (channel_type == METADATA_CHANNEL) {
cd4bd11f 228 session->metadata = chan;
4f1c3952 229 lttng_metadata_create_events(chan_file);
cd4bd11f 230 }
5dbbdb43 231
ad1c05e1 232 /* The channel created holds a reference on the session */
b0caa15a 233 atomic_long_inc(&session_file->f_count);
ad1c05e1 234
baf20995
MD
235 return chan_fd;
236
03037b98 237chan_error:
c0e31d2e 238 fput(chan_file);
baf20995
MD
239file_error:
240 put_unused_fd(chan_fd);
241fd_error:
baf20995
MD
242 return ret;
243}
244
245/**
ad1c05e1 246 * lttng_session_ioctl - lttng session fd ioctl
baf20995 247 *
c0e31d2e 248 * @file: the file
baf20995
MD
249 * @cmd: the command
250 * @arg: command arg
251 *
252 * This ioctl implements lttng commands:
38d024ae 253 * LTTNG_KERNEL_CHANNEL
baf20995 254 * Returns a LTTng channel file descriptor
ad1c05e1
MD
255 *
256 * The returned channel will be deleted when its file descriptor is closed.
257 */
258static
c0e31d2e 259long lttng_session_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
ad1c05e1 260{
c0e31d2e
MD
261 struct ltt_session *session = file->private_data;
262
ad1c05e1 263 switch (cmd) {
38d024ae 264 case LTTNG_KERNEL_CHANNEL:
5dbbdb43 265 return lttng_abi_create_channel(file,
38d024ae 266 (struct lttng_kernel_channel __user *) arg,
5dbbdb43 267 PER_CPU_CHANNEL);
38d024ae 268 case LTTNG_KERNEL_SESSION_START:
c0e31d2e 269 return ltt_session_start(session);
38d024ae 270 case LTTNG_KERNEL_SESSION_STOP:
c0e31d2e 271 return ltt_session_stop(session);
38d024ae 272 case LTTNG_KERNEL_METADATA:
5dbbdb43 273 return lttng_abi_create_channel(file,
38d024ae 274 (struct lttng_kernel_channel __user *) arg,
5dbbdb43 275 METADATA_CHANNEL);
ad1c05e1
MD
276 default:
277 return -ENOIOCTLCMD;
278 }
279}
280
03037b98
MD
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 */
ad1c05e1 289static
03037b98 290int lttng_session_release(struct inode *inode, struct file *file)
ad1c05e1 291{
03037b98 292 struct ltt_session *session = file->private_data;
c269fff4
MD
293
294 if (session)
295 ltt_session_destroy(session);
11b5a3c2 296 return 0;
ad1c05e1 297}
ad1c05e1
MD
298
299static const struct file_operations lttng_session_fops = {
03037b98 300 .release = lttng_session_release,
ad1c05e1
MD
301 .unlocked_ioctl = lttng_session_ioctl,
302#ifdef CONFIG_COMPAT
03037b98 303 .compat_ioctl = lttng_session_ioctl,
ad1c05e1 304#endif
11b5a3c2 305};
ad1c05e1
MD
306
307static
c0e31d2e 308int lttng_abi_open_stream(struct file *channel_file)
ad1c05e1 309{
c0e31d2e 310 struct ltt_channel *channel = channel_file->private_data;
ad1c05e1
MD
311 struct lib_ring_buffer *buf;
312 int stream_fd, ret;
11b5a3c2 313 struct file *stream_file;
ad1c05e1 314
11b5a3c2 315 buf = channel->ops->buffer_read_open(channel->chan);
ad1c05e1
MD
316 if (!buf)
317 return -ENOENT;
318
1c25284c 319 stream_fd = get_unused_fd();
ad1c05e1
MD
320 if (stream_fd < 0) {
321 ret = stream_fd;
322 goto fd_error;
323 }
c0e31d2e 324 stream_file = anon_inode_getfile("[lttng_stream]",
7f57c73c 325 &lib_ring_buffer_file_operations,
ad1c05e1 326 buf, O_RDWR);
c0e31d2e
MD
327 if (IS_ERR(stream_file)) {
328 ret = PTR_ERR(stream_file);
ad1c05e1
MD
329 goto file_error;
330 }
409453cb
MD
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 */
d7b6f197 336 stream_file->f_mode |= FMODE_PREAD;
c0e31d2e 337 fd_install(stream_fd, stream_file);
dda6a249
MD
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 */
ad1c05e1
MD
343 return stream_fd;
344
345file_error:
346 put_unused_fd(stream_fd);
347fd_error:
11b5a3c2 348 channel->ops->buffer_read_close(buf);
ad1c05e1
MD
349 return ret;
350}
351
653fe716 352static
c0e31d2e 353int lttng_abi_create_event(struct file *channel_file,
38d024ae 354 struct lttng_kernel_event __user *uevent_param)
653fe716 355{
c0e31d2e 356 struct ltt_channel *channel = channel_file->private_data;
653fe716
MD
357 struct ltt_event *event;
358 char *event_name;
38d024ae 359 struct lttng_kernel_event event_param;
653fe716 360 int event_fd, ret;
11b5a3c2 361 struct file *event_file;
653fe716
MD
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;
981616e8 368 if (strncpy_from_user(event_name, uevent_param->name, PATH_MAX) < 0) {
653fe716
MD
369 ret = -EFAULT;
370 goto name_error;
371 }
372 event_name[PATH_MAX - 1] = '\0';
e0a7a7c4
MD
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 }
1c25284c 383 event_fd = get_unused_fd();
653fe716
MD
384 if (event_fd < 0) {
385 ret = event_fd;
386 goto fd_error;
387 }
c0e31d2e 388 event_file = anon_inode_getfile("[lttng_event]",
3b923e5b 389 &lttng_event_fops,
03037b98 390 NULL, O_RDWR);
c0e31d2e
MD
391 if (IS_ERR(event_file)) {
392 ret = PTR_ERR(event_file);
653fe716
MD
393 goto file_error;
394 }
03037b98
MD
395 /*
396 * We tolerate no failure path after event creation. It will stay
397 * invariant for the rest of the session.
398 */
d3dbe23c 399 event = ltt_event_create(channel, event_name, &event_param, NULL);
03037b98 400 if (!event) {
03037b98 401 ret = -EEXIST;
d6d808f3 402 goto event_error;
03037b98 403 }
c0e31d2e
MD
404 event_file->private_data = event;
405 fd_install(event_fd, event_file);
653fe716 406 /* The event holds a reference on the channel */
b0caa15a 407 atomic_long_inc(&channel_file->f_count);
03037b98 408 kfree(event_name);
653fe716
MD
409 return event_fd;
410
03037b98 411event_error:
c0e31d2e 412 fput(event_file);
653fe716
MD
413file_error:
414 put_unused_fd(event_fd);
415fd_error:
653fe716
MD
416name_error:
417 kfree(event_name);
418 return ret;
419}
ad1c05e1
MD
420
421/**
422 * lttng_channel_ioctl - lttng syscall through ioctl
423 *
c0e31d2e 424 * @file: the file
ad1c05e1
MD
425 * @cmd: the command
426 * @arg: command arg
427 *
428 * This ioctl implements lttng commands:
38d024ae 429 * LTTNG_KERNEL_STREAM
ad1c05e1
MD
430 * Returns an event stream file descriptor or failure.
431 * (typically, one event stream records events from one CPU)
38d024ae 432 * LTTNG_KERNEL_EVENT
ad1c05e1 433 * Returns an event file descriptor or failure.
baf20995 434 *
baf20995
MD
435 * Channel and event file descriptors also hold a reference on the session.
436 */
ad1c05e1 437static
c0e31d2e 438long lttng_channel_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
baf20995 439{
baf20995 440 switch (cmd) {
38d024ae 441 case LTTNG_KERNEL_STREAM:
c0e31d2e 442 return lttng_abi_open_stream(file);
38d024ae
MD
443 case LTTNG_KERNEL_EVENT:
444 return lttng_abi_create_event(file, (struct lttng_kernel_event __user *) arg);
baf20995
MD
445 default:
446 return -ENOIOCTLCMD;
447 }
448}
449
5dbbdb43
MD
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:
38d024ae 458 * LTTNG_KERNEL_STREAM
5dbbdb43
MD
459 * Returns an event stream file descriptor or failure.
460 *
461 * Channel and event file descriptors also hold a reference on the session.
462 */
463static
464long lttng_metadata_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
465{
466 switch (cmd) {
38d024ae 467 case LTTNG_KERNEL_STREAM:
5dbbdb43
MD
468 return lttng_abi_open_stream(file);
469 default:
470 return -ENOIOCTLCMD;
471 }
472}
473
3b923e5b
MD
474/* TODO: poll */
475#if 0
653fe716
MD
476/**
477 * lttng_channel_poll - lttng stream addition/removal monitoring
478 *
c0e31d2e 479 * @file: the file
653fe716
MD
480 * @wait: poll table
481 */
c0e31d2e 482unsigned int lttng_channel_poll(struct file *file, poll_table *wait)
653fe716 483{
c0e31d2e 484 struct ltt_channel *channel = file->private_data;
653fe716
MD
485 unsigned int mask = 0;
486
c0e31d2e 487 if (file->f_mode & FMODE_READ) {
653fe716 488 poll_wait_set_exclusive(wait);
c0e31d2e 489 poll_wait(file, &channel->notify_wait, wait);
653fe716
MD
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}
3b923e5b 500#endif //0
653fe716 501
0a84a57f
MD
502static
503int lttng_channel_release(struct inode *inode, struct file *file)
504{
505 struct ltt_channel *channel = file->private_data;
c269fff4
MD
506
507 if (channel)
508 fput(channel->session->file);
0a84a57f
MD
509 return 0;
510}
511
ad1c05e1 512static const struct file_operations lttng_channel_fops = {
03037b98 513 .release = lttng_channel_release,
3b923e5b
MD
514/* TODO */
515#if 0
653fe716 516 .poll = lttng_channel_poll,
3b923e5b 517#endif //0
ad1c05e1 518 .unlocked_ioctl = lttng_channel_ioctl,
baf20995 519#ifdef CONFIG_COMPAT
03037b98 520 .compat_ioctl = lttng_channel_ioctl,
baf20995 521#endif
11b5a3c2 522};
baf20995 523
5dbbdb43
MD
524static 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
0a84a57f
MD
532static
533int lttng_event_release(struct inode *inode, struct file *file)
534{
535 struct ltt_event *event = file->private_data;
c269fff4 536
be066e6c
MD
537 if (event) {
538 ltt_event_unregister(event);
c269fff4 539 fput(event->chan->file);
be066e6c 540 }
0a84a57f
MD
541 return 0;
542}
543
3b923e5b 544/* TODO: filter control ioctl */
0a84a57f
MD
545static const struct file_operations lttng_event_fops = {
546 .release = lttng_event_release,
11b5a3c2 547};
0a84a57f 548
1c25284c 549int __init ltt_debugfs_abi_init(void)
baf20995
MD
550{
551 int ret = 0;
552
6d2a620c 553 wrapper_vmalloc_sync_all();
11b5a3c2 554 lttng_dentry = debugfs_create_file("lttng", S_IWUSR, NULL, NULL,
f3d01b96 555 &lttng_fops);
11b5a3c2 556 if (IS_ERR(lttng_dentry) || !lttng_dentry) {
baf20995
MD
557 printk(KERN_ERR "Error creating LTTng control file\n");
558 ret = -ENOMEM;
559 goto error;
560 }
561error:
562 return ret;
563}
564
1c25284c 565void __exit ltt_debugfs_abi_exit(void)
baf20995 566{
11b5a3c2 567 debugfs_remove(lttng_dentry);
baf20995 568}
This page took 0.05152 seconds and 4 git commands to generate.