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