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