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