Add syscall tracing
[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"
baf20995
MD
35
36/*
37 * This is LTTng's own personal way to create a system call as an external
38 * module. We use ioctl() on /sys/kernel/debug/lttng.
39 */
40
41static struct dentry *lttng_dentry;
ad1c05e1
MD
42static const struct file_operations lttng_fops;
43static const struct file_operations lttng_session_fops;
44static const struct file_operations lttng_channel_fops;
5dbbdb43 45static const struct file_operations lttng_metadata_fops;
305d3e42 46static const struct file_operations lttng_event_fops;
baf20995 47
5dbbdb43
MD
48enum channel_type {
49 PER_CPU_CHANNEL,
5dbbdb43
MD
50 METADATA_CHANNEL,
51};
52
ad1c05e1 53static
baf20995
MD
54int lttng_abi_create_session(void)
55{
56 struct ltt_session *session;
c0e31d2e 57 struct file *session_file;
11b5a3c2 58 int session_fd, ret;
baf20995 59
653fe716 60 session = ltt_session_create();
baf20995
MD
61 if (!session)
62 return -ENOMEM;
1c25284c 63 session_fd = get_unused_fd();
baf20995
MD
64 if (session_fd < 0) {
65 ret = session_fd;
66 goto fd_error;
67 }
c0e31d2e 68 session_file = anon_inode_getfile("[lttng_session]",
ad1c05e1 69 &lttng_session_fops,
baf20995 70 session, O_RDWR);
c0e31d2e
MD
71 if (IS_ERR(session_file)) {
72 ret = PTR_ERR(session_file);
baf20995
MD
73 goto file_error;
74 }
c0e31d2e
MD
75 session->file = session_file;
76 fd_install(session_fd, session_file);
baf20995
MD
77 return session_fd;
78
79file_error:
80 put_unused_fd(session_fd);
81fd_error:
82 ltt_session_destroy(session);
83 return ret;
84}
85
ad1c05e1
MD
86/**
87 * lttng_ioctl - lttng syscall through ioctl
88 *
c0e31d2e 89 * @file: the file
ad1c05e1
MD
90 * @cmd: the command
91 * @arg: command arg
92 *
93 * This ioctl implements lttng commands:
94 * LTTNG_SESSION
95 * Returns a LTTng trace session file descriptor
96 *
97 * The returned session will be deleted when its file descriptor is closed.
98 */
99static
c0e31d2e 100long lttng_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
ad1c05e1
MD
101{
102 switch (cmd) {
103 case LTTNG_SESSION:
104 return lttng_abi_create_session();
105 default:
106 return -ENOIOCTLCMD;
107 }
108}
109
ad1c05e1
MD
110static const struct file_operations lttng_fops = {
111 .unlocked_ioctl = lttng_ioctl,
112#ifdef CONFIG_COMPAT
03037b98 113 .compat_ioctl = lttng_ioctl,
ad1c05e1 114#endif
11b5a3c2 115};
ad1c05e1 116
5dbbdb43
MD
117/*
118 * We tolerate no failure in this function (if one happens, we print a dmesg
119 * error, but cannot return any error, because the channel information is
120 * invariant.
121 */
122static
123void lttng_metadata_create_events(struct file *channel_file)
124{
125 struct ltt_channel *channel = channel_file->private_data;
cd4bd11f 126 char *event_name = "lttng_metadata";
85a9ca7f 127 const struct lttng_event_desc *event_desc;
5dbbdb43
MD
128 struct ltt_event *event;
129 int ret;
5dbbdb43 130
85a9ca7f
MD
131 event_desc = ltt_event_get(event_name);
132 if (!event_desc) {
5dbbdb43 133 ret = -ENOENT;
85a9ca7f 134 goto get_error;
5dbbdb43
MD
135 }
136 /*
137 * We tolerate no failure path after event creation. It will stay
138 * invariant for the rest of the session.
139 */
140 event = ltt_event_create(channel, event_name, INSTRUM_TRACEPOINTS,
85a9ca7f 141 event_desc, NULL);
5dbbdb43 142 if (!event) {
85a9ca7f 143 goto create_error;
5dbbdb43
MD
144 ret = -EEXIST;
145 }
146 return;
147
85a9ca7f
MD
148create_error:
149 ltt_event_put(event_desc);
150get_error:
5dbbdb43
MD
151 WARN_ON(1);
152 return; /* not allowed to return error */
153}
154
155static
c0e31d2e 156int lttng_abi_create_channel(struct file *session_file,
5dbbdb43
MD
157 struct lttng_channel __user *uchan_param,
158 enum channel_type channel_type)
baf20995 159{
c0e31d2e 160 struct ltt_session *session = session_file->private_data;
5dbbdb43
MD
161 const struct file_operations *fops;
162 const char *transport_name;
baf20995 163 struct ltt_channel *chan;
c0e31d2e 164 struct file *chan_file;
baf20995
MD
165 struct lttng_channel chan_param;
166 int chan_fd;
ad1c05e1 167 int ret = 0;
baf20995 168
653fe716 169 if (copy_from_user(&chan_param, uchan_param, sizeof(chan_param)))
baf20995 170 return -EFAULT;
1c25284c 171 chan_fd = get_unused_fd();
baf20995
MD
172 if (chan_fd < 0) {
173 ret = chan_fd;
174 goto fd_error;
175 }
c0e31d2e 176 chan_file = anon_inode_getfile("[lttng_channel]",
ad1c05e1 177 &lttng_channel_fops,
03037b98 178 NULL, O_RDWR);
c0e31d2e
MD
179 if (IS_ERR(chan_file)) {
180 ret = PTR_ERR(chan_file);
baf20995
MD
181 goto file_error;
182 }
5dbbdb43
MD
183 switch (channel_type) {
184 case PER_CPU_CHANNEL:
185 transport_name = chan_param.overwrite ?
186 "relay-overwrite" : "relay-discard";
187 fops = &lttng_channel_fops;
188 break;
5dbbdb43 189 case METADATA_CHANNEL:
881833e3 190 transport_name = "relay-metadata";
5dbbdb43
MD
191 fops = &lttng_metadata_fops;
192 break;
193 default:
194 transport_name = "<unknown>";
195 break;
196 }
03037b98
MD
197 /*
198 * We tolerate no failure path after channel creation. It will stay
199 * invariant for the rest of the session.
200 */
5dbbdb43 201 chan = ltt_channel_create(session, transport_name, NULL,
11b5a3c2
MD
202 chan_param.subbuf_size,
203 chan_param.num_subbuf,
204 chan_param.switch_timer_interval,
205 chan_param.read_timer_interval);
03037b98 206 if (!chan) {
f3d01b96 207 ret = -EINVAL;
03037b98
MD
208 goto chan_error;
209 }
11b5a3c2 210 chan->file = chan_file;
c0e31d2e
MD
211 chan_file->private_data = chan;
212 fd_install(chan_fd, chan_file);
cd4bd11f 213 if (channel_type == METADATA_CHANNEL) {
5dbbdb43 214 lttng_metadata_create_events(chan_file);
cd4bd11f
MD
215 session->metadata = chan;
216 }
5dbbdb43 217
ad1c05e1 218 /* The channel created holds a reference on the session */
b0caa15a 219 atomic_long_inc(&session_file->f_count);
ad1c05e1 220
baf20995
MD
221 return chan_fd;
222
03037b98 223chan_error:
c0e31d2e 224 fput(chan_file);
baf20995
MD
225file_error:
226 put_unused_fd(chan_fd);
227fd_error:
baf20995
MD
228 return ret;
229}
230
231/**
ad1c05e1 232 * lttng_session_ioctl - lttng session fd ioctl
baf20995 233 *
c0e31d2e 234 * @file: the file
baf20995
MD
235 * @cmd: the command
236 * @arg: command arg
237 *
238 * This ioctl implements lttng commands:
baf20995
MD
239 * LTTNG_CHANNEL
240 * Returns a LTTng channel file descriptor
ad1c05e1
MD
241 *
242 * The returned channel will be deleted when its file descriptor is closed.
243 */
244static
c0e31d2e 245long lttng_session_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
ad1c05e1 246{
c0e31d2e
MD
247 struct ltt_session *session = file->private_data;
248
ad1c05e1
MD
249 switch (cmd) {
250 case LTTNG_CHANNEL:
5dbbdb43
MD
251 return lttng_abi_create_channel(file,
252 (struct lttng_channel __user *)arg,
253 PER_CPU_CHANNEL);
c0e31d2e
MD
254 case LTTNG_SESSION_START:
255 return ltt_session_start(session);
256 case LTTNG_SESSION_STOP:
257 return ltt_session_stop(session);
5dbbdb43
MD
258 case LTTNG_METADATA:
259 return lttng_abi_create_channel(file,
260 (struct lttng_channel __user *)arg,
261 METADATA_CHANNEL);
ad1c05e1
MD
262 default:
263 return -ENOIOCTLCMD;
264 }
265}
266
03037b98
MD
267/*
268 * Called when the last file reference is dropped.
269 *
270 * Big fat note: channels and events are invariant for the whole session after
271 * their creation. So this session destruction also destroys all channel and
272 * event structures specific to this session (they are not destroyed when their
273 * individual file is released).
274 */
ad1c05e1 275static
03037b98 276int lttng_session_release(struct inode *inode, struct file *file)
ad1c05e1 277{
03037b98 278 struct ltt_session *session = file->private_data;
c269fff4
MD
279
280 if (session)
281 ltt_session_destroy(session);
11b5a3c2 282 return 0;
ad1c05e1 283}
ad1c05e1
MD
284
285static const struct file_operations lttng_session_fops = {
03037b98 286 .release = lttng_session_release,
ad1c05e1
MD
287 .unlocked_ioctl = lttng_session_ioctl,
288#ifdef CONFIG_COMPAT
03037b98 289 .compat_ioctl = lttng_session_ioctl,
ad1c05e1 290#endif
11b5a3c2 291};
ad1c05e1
MD
292
293static
c0e31d2e 294int lttng_abi_open_stream(struct file *channel_file)
ad1c05e1 295{
c0e31d2e 296 struct ltt_channel *channel = channel_file->private_data;
ad1c05e1
MD
297 struct lib_ring_buffer *buf;
298 int stream_fd, ret;
11b5a3c2 299 struct file *stream_file;
ad1c05e1 300
11b5a3c2 301 buf = channel->ops->buffer_read_open(channel->chan);
ad1c05e1
MD
302 if (!buf)
303 return -ENOENT;
304
1c25284c 305 stream_fd = get_unused_fd();
ad1c05e1
MD
306 if (stream_fd < 0) {
307 ret = stream_fd;
308 goto fd_error;
309 }
c0e31d2e 310 stream_file = anon_inode_getfile("[lttng_stream]",
7f57c73c 311 &lib_ring_buffer_file_operations,
ad1c05e1 312 buf, O_RDWR);
c0e31d2e
MD
313 if (IS_ERR(stream_file)) {
314 ret = PTR_ERR(stream_file);
ad1c05e1
MD
315 goto file_error;
316 }
409453cb
MD
317 /*
318 * OPEN_FMODE, called within anon_inode_getfile/alloc_file, don't honor
319 * FMODE_LSEEK, FMODE_PREAD nor FMODE_PWRITE. We need to read from this
320 * file descriptor, so we set FMODE_PREAD here.
321 */
d7b6f197 322 stream_file->f_mode |= FMODE_PREAD;
c0e31d2e 323 fd_install(stream_fd, stream_file);
dda6a249
MD
324 /*
325 * The stream holds a reference to the channel within the generic ring
326 * buffer library, so no need to hold a refcount on the channel and
327 * session files here.
328 */
ad1c05e1
MD
329 return stream_fd;
330
331file_error:
332 put_unused_fd(stream_fd);
333fd_error:
11b5a3c2 334 channel->ops->buffer_read_close(buf);
ad1c05e1
MD
335 return ret;
336}
337
653fe716 338static
c0e31d2e 339int lttng_abi_create_event(struct file *channel_file,
653fe716
MD
340 struct lttng_event __user *uevent_param)
341{
c0e31d2e 342 struct ltt_channel *channel = channel_file->private_data;
85a9ca7f 343 const struct lttng_event_desc *event_desc;
653fe716
MD
344 struct ltt_event *event;
345 char *event_name;
346 struct lttng_event event_param;
347 int event_fd, ret;
11b5a3c2 348 struct file *event_file;
653fe716
MD
349
350 if (copy_from_user(&event_param, uevent_param, sizeof(event_param)))
351 return -EFAULT;
352 event_name = kmalloc(PATH_MAX, GFP_KERNEL);
353 if (!event_name)
354 return -ENOMEM;
981616e8 355 if (strncpy_from_user(event_name, uevent_param->name, PATH_MAX) < 0) {
653fe716
MD
356 ret = -EFAULT;
357 goto name_error;
358 }
359 event_name[PATH_MAX - 1] = '\0';
dda6a249 360
85a9ca7f
MD
361 event_desc = ltt_event_get(event_name);
362 if (!event_desc) {
dda6a249 363 ret = -ENOENT;
85a9ca7f 364 goto get_error;
dda6a249 365 }
1c25284c 366 event_fd = get_unused_fd();
653fe716
MD
367 if (event_fd < 0) {
368 ret = event_fd;
369 goto fd_error;
370 }
c0e31d2e 371 event_file = anon_inode_getfile("[lttng_event]",
3b923e5b 372 &lttng_event_fops,
03037b98 373 NULL, O_RDWR);
c0e31d2e
MD
374 if (IS_ERR(event_file)) {
375 ret = PTR_ERR(event_file);
653fe716
MD
376 goto file_error;
377 }
03037b98
MD
378 /*
379 * We tolerate no failure path after event creation. It will stay
380 * invariant for the rest of the session.
381 */
11b5a3c2 382 event = ltt_event_create(channel, event_name, event_param.itype,
85a9ca7f 383 event_desc, NULL);
03037b98
MD
384 if (!event) {
385 goto event_error;
386 ret = -EEXIST;
387 }
c0e31d2e
MD
388 event_file->private_data = event;
389 fd_install(event_fd, event_file);
653fe716 390 /* The event holds a reference on the channel */
b0caa15a 391 atomic_long_inc(&channel_file->f_count);
03037b98 392 kfree(event_name);
653fe716
MD
393 return event_fd;
394
03037b98 395event_error:
c0e31d2e 396 fput(event_file);
653fe716
MD
397file_error:
398 put_unused_fd(event_fd);
399fd_error:
85a9ca7f
MD
400 ltt_event_put(event_desc);
401get_error:
653fe716
MD
402name_error:
403 kfree(event_name);
404 return ret;
405}
ad1c05e1
MD
406
407/**
408 * lttng_channel_ioctl - lttng syscall through ioctl
409 *
c0e31d2e 410 * @file: the file
ad1c05e1
MD
411 * @cmd: the command
412 * @arg: command arg
413 *
414 * This ioctl implements lttng commands:
415 * LTTNG_STREAM
416 * Returns an event stream file descriptor or failure.
417 * (typically, one event stream records events from one CPU)
baf20995 418 * LTTNG_EVENT
ad1c05e1 419 * Returns an event file descriptor or failure.
baf20995 420 *
baf20995
MD
421 * Channel and event file descriptors also hold a reference on the session.
422 */
ad1c05e1 423static
c0e31d2e 424long lttng_channel_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
baf20995 425{
baf20995 426 switch (cmd) {
ad1c05e1 427 case LTTNG_STREAM:
c0e31d2e 428 return lttng_abi_open_stream(file);
baf20995 429 case LTTNG_EVENT:
c0e31d2e 430 return lttng_abi_create_event(file, (struct lttng_event __user *)arg);
baf20995
MD
431 default:
432 return -ENOIOCTLCMD;
433 }
434}
435
5dbbdb43
MD
436/**
437 * lttng_metadata_ioctl - lttng syscall through ioctl
438 *
439 * @file: the file
440 * @cmd: the command
441 * @arg: command arg
442 *
443 * This ioctl implements lttng commands:
444 * LTTNG_STREAM
445 * Returns an event stream file descriptor or failure.
446 *
447 * Channel and event file descriptors also hold a reference on the session.
448 */
449static
450long lttng_metadata_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
451{
452 switch (cmd) {
453 case LTTNG_STREAM:
454 return lttng_abi_open_stream(file);
455 default:
456 return -ENOIOCTLCMD;
457 }
458}
459
3b923e5b
MD
460/* TODO: poll */
461#if 0
653fe716
MD
462/**
463 * lttng_channel_poll - lttng stream addition/removal monitoring
464 *
c0e31d2e 465 * @file: the file
653fe716
MD
466 * @wait: poll table
467 */
c0e31d2e 468unsigned int lttng_channel_poll(struct file *file, poll_table *wait)
653fe716 469{
c0e31d2e 470 struct ltt_channel *channel = file->private_data;
653fe716
MD
471 unsigned int mask = 0;
472
c0e31d2e 473 if (file->f_mode & FMODE_READ) {
653fe716 474 poll_wait_set_exclusive(wait);
c0e31d2e 475 poll_wait(file, &channel->notify_wait, wait);
653fe716
MD
476
477 /* TODO: identify when the channel is being finalized. */
478 if (finalized)
479 return POLLHUP;
480 else
481 return POLLIN | POLLRDNORM;
482 }
483 return mask;
484
485}
3b923e5b 486#endif //0
653fe716 487
0a84a57f
MD
488static
489int lttng_channel_release(struct inode *inode, struct file *file)
490{
491 struct ltt_channel *channel = file->private_data;
c269fff4
MD
492
493 if (channel)
494 fput(channel->session->file);
0a84a57f
MD
495 return 0;
496}
497
ad1c05e1 498static const struct file_operations lttng_channel_fops = {
03037b98 499 .release = lttng_channel_release,
3b923e5b
MD
500/* TODO */
501#if 0
653fe716 502 .poll = lttng_channel_poll,
3b923e5b 503#endif //0
ad1c05e1 504 .unlocked_ioctl = lttng_channel_ioctl,
baf20995 505#ifdef CONFIG_COMPAT
03037b98 506 .compat_ioctl = lttng_channel_ioctl,
baf20995 507#endif
11b5a3c2 508};
baf20995 509
5dbbdb43
MD
510static const struct file_operations lttng_metadata_fops = {
511 .release = lttng_channel_release,
512 .unlocked_ioctl = lttng_metadata_ioctl,
513#ifdef CONFIG_COMPAT
514 .compat_ioctl = lttng_metadata_ioctl,
515#endif
516};
517
0a84a57f
MD
518static
519int lttng_event_release(struct inode *inode, struct file *file)
520{
521 struct ltt_event *event = file->private_data;
c269fff4 522
be066e6c
MD
523 if (event) {
524 ltt_event_unregister(event);
c269fff4 525 fput(event->chan->file);
be066e6c 526 }
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.050038 seconds and 4 git commands to generate.