Update readme
[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.
17baffe2
MD
23 *
24 * Dual LGPL v2.1/GPL v2 license.
baf20995
MD
25 */
26
11b5a3c2 27#include <linux/module.h>
baf20995 28#include <linux/debugfs.h>
11b5a3c2
MD
29#include <linux/anon_inodes.h>
30#include <linux/file.h>
31#include <linux/uaccess.h>
32#include <linux/slab.h>
b13f3ebe 33#include "wrapper/vmalloc.h" /* for wrapper_vmalloc_sync_all() */
f3bc08c5 34#include "wrapper/ringbuffer/vfs.h"
24cedcfe 35#include "wrapper/poll.h"
11b5a3c2 36#include "ltt-debugfs-abi.h"
ad1c05e1 37#include "ltt-events.h"
80c16bcf 38#include "ltt-tracer.h"
baf20995
MD
39
40/*
41 * This is LTTng's own personal way to create a system call as an external
42 * module. We use ioctl() on /sys/kernel/debug/lttng.
43 */
44
45static struct dentry *lttng_dentry;
ad1c05e1
MD
46static const struct file_operations lttng_fops;
47static const struct file_operations lttng_session_fops;
48static const struct file_operations lttng_channel_fops;
5dbbdb43 49static const struct file_operations lttng_metadata_fops;
305d3e42 50static const struct file_operations lttng_event_fops;
baf20995 51
a33c9927
MD
52/*
53 * Teardown management: opened file descriptors keep a refcount on the module,
54 * so it can only exit when all file descriptors are closed.
55 */
56
5dbbdb43
MD
57enum channel_type {
58 PER_CPU_CHANNEL,
5dbbdb43
MD
59 METADATA_CHANNEL,
60};
61
ad1c05e1 62static
baf20995
MD
63int lttng_abi_create_session(void)
64{
65 struct ltt_session *session;
c0e31d2e 66 struct file *session_file;
11b5a3c2 67 int session_fd, ret;
baf20995 68
653fe716 69 session = ltt_session_create();
baf20995
MD
70 if (!session)
71 return -ENOMEM;
1c25284c 72 session_fd = get_unused_fd();
baf20995
MD
73 if (session_fd < 0) {
74 ret = session_fd;
75 goto fd_error;
76 }
c0e31d2e 77 session_file = anon_inode_getfile("[lttng_session]",
ad1c05e1 78 &lttng_session_fops,
baf20995 79 session, O_RDWR);
c0e31d2e
MD
80 if (IS_ERR(session_file)) {
81 ret = PTR_ERR(session_file);
baf20995
MD
82 goto file_error;
83 }
c0e31d2e
MD
84 session->file = session_file;
85 fd_install(session_fd, session_file);
baf20995
MD
86 return session_fd;
87
88file_error:
89 put_unused_fd(session_fd);
90fd_error:
91 ltt_session_destroy(session);
92 return ret;
93}
94
271b6681
MD
95static
96int lttng_abi_tracepoint_list(void)
97{
98 struct file *tracepoint_list_file;
99 int file_fd, ret;
100
101 file_fd = get_unused_fd();
102 if (file_fd < 0) {
103 ret = file_fd;
104 goto fd_error;
105 }
30f18bf0 106
271b6681
MD
107 tracepoint_list_file = anon_inode_getfile("[lttng_session]",
108 &lttng_tracepoint_list_fops,
109 NULL, O_RDWR);
110 if (IS_ERR(tracepoint_list_file)) {
111 ret = PTR_ERR(tracepoint_list_file);
112 goto file_error;
113 }
30f18bf0
MD
114 ret = lttng_tracepoint_list_fops.open(NULL, tracepoint_list_file);
115 if (ret < 0)
116 goto open_error;
271b6681 117 fd_install(file_fd, tracepoint_list_file);
30f18bf0
MD
118 if (file_fd < 0) {
119 ret = file_fd;
120 goto fd_error;
121 }
271b6681
MD
122 return file_fd;
123
30f18bf0
MD
124open_error:
125 fput(tracepoint_list_file);
271b6681
MD
126file_error:
127 put_unused_fd(file_fd);
128fd_error:
129 return ret;
130}
131
80c16bcf
MD
132static
133long lttng_abi_tracer_version(struct file *file,
134 struct lttng_kernel_tracer_version __user *uversion_param)
135{
136 struct lttng_kernel_tracer_version v;
137
138 v.version = LTTNG_VERSION;
139 v.patchlevel = LTTNG_PATCHLEVEL;
140 v.sublevel = LTTNG_SUBLEVEL;
141
142 if (copy_to_user(uversion_param, &v, sizeof(v)))
143 return -EFAULT;
144 return 0;
145}
146
8070f5c0
MD
147static
148long lttng_abi_add_context(struct file *file,
149 struct lttng_kernel_context __user *ucontext_param,
150 struct lttng_ctx **ctx, struct ltt_session *session)
151{
152 struct lttng_kernel_context context_param;
153
154 if (session->been_active)
155 return -EPERM;
156
157 if (copy_from_user(&context_param, ucontext_param, sizeof(context_param)))
158 return -EFAULT;
159
160 switch (context_param.ctx) {
12a313a5 161 case LTTNG_KERNEL_CONTEXT_PID:
8070f5c0 162 return lttng_add_pid_to_ctx(ctx);
a8ad3613
MD
163 case LTTNG_KERNEL_CONTEXT_PRIO:
164 return lttng_add_prio_to_ctx(ctx);
53f1f0ca
MD
165 case LTTNG_KERNEL_CONTEXT_NICE:
166 return lttng_add_nice_to_ctx(ctx);
b64bc438
MD
167 case LTTNG_KERNEL_CONTEXT_VPID:
168 return lttng_add_vpid_to_ctx(ctx);
169 case LTTNG_KERNEL_CONTEXT_TID:
170 return lttng_add_tid_to_ctx(ctx);
171 case LTTNG_KERNEL_CONTEXT_VTID:
172 return lttng_add_vtid_to_ctx(ctx);
173 case LTTNG_KERNEL_CONTEXT_PPID:
174 return lttng_add_ppid_to_ctx(ctx);
175 case LTTNG_KERNEL_CONTEXT_VPPID:
176 return lttng_add_vppid_to_ctx(ctx);
12a313a5 177 case LTTNG_KERNEL_CONTEXT_PERF_COUNTER:
c24a0d71
MD
178 context_param.u.perf_counter.name[LTTNG_SYM_NAME_LEN - 1] = '\0';
179 return lttng_add_perf_counter_to_ctx(context_param.u.perf_counter.type,
180 context_param.u.perf_counter.config,
181 context_param.u.perf_counter.name,
182 ctx);
25b2f99a
MD
183 case LTTNG_KERNEL_CONTEXT_COMM:
184 return lttng_add_comm_to_ctx(ctx);
8070f5c0
MD
185 default:
186 return -EINVAL;
187 }
188}
189
ad1c05e1
MD
190/**
191 * lttng_ioctl - lttng syscall through ioctl
192 *
c0e31d2e 193 * @file: the file
ad1c05e1
MD
194 * @cmd: the command
195 * @arg: command arg
196 *
197 * This ioctl implements lttng commands:
38d024ae 198 * LTTNG_KERNEL_SESSION
ad1c05e1 199 * Returns a LTTng trace session file descriptor
271b6681
MD
200 * LTTNG_KERNEL_TRACER_VERSION
201 * Returns the LTTng kernel tracer version
202 * LTTNG_KERNEL_TRACEPOINT_LIST
203 * Returns a file descriptor listing available tracepoints
360f38ea
MD
204 * LTTNG_KERNEL_WAIT_QUIESCENT
205 * Returns after all previously running probes have completed
ad1c05e1
MD
206 *
207 * The returned session will be deleted when its file descriptor is closed.
208 */
209static
c0e31d2e 210long lttng_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
ad1c05e1
MD
211{
212 switch (cmd) {
38d024ae 213 case LTTNG_KERNEL_SESSION:
ad1c05e1 214 return lttng_abi_create_session();
80c16bcf
MD
215 case LTTNG_KERNEL_TRACER_VERSION:
216 return lttng_abi_tracer_version(file,
217 (struct lttng_kernel_tracer_version __user *) arg);
271b6681
MD
218 case LTTNG_KERNEL_TRACEPOINT_LIST:
219 return lttng_abi_tracepoint_list();
5f7f9078
MD
220 case LTTNG_KERNEL_WAIT_QUIESCENT:
221 synchronize_trace();
222 return 0;
ad1c05e1
MD
223 default:
224 return -ENOIOCTLCMD;
225 }
226}
227
ad1c05e1 228static const struct file_operations lttng_fops = {
a33c9927 229 .owner = THIS_MODULE,
ad1c05e1
MD
230 .unlocked_ioctl = lttng_ioctl,
231#ifdef CONFIG_COMPAT
03037b98 232 .compat_ioctl = lttng_ioctl,
ad1c05e1 233#endif
11b5a3c2 234};
ad1c05e1 235
5dbbdb43
MD
236/*
237 * We tolerate no failure in this function (if one happens, we print a dmesg
238 * error, but cannot return any error, because the channel information is
239 * invariant.
240 */
241static
242void lttng_metadata_create_events(struct file *channel_file)
243{
244 struct ltt_channel *channel = channel_file->private_data;
e70a4758 245 static struct lttng_kernel_event metadata_params = {
ab2277d6 246 .instrumentation = LTTNG_KERNEL_TRACEPOINT,
e70a4758
MD
247 .name = "lttng_metadata",
248 };
5dbbdb43
MD
249 struct ltt_event *event;
250 int ret;
5dbbdb43 251
5dbbdb43
MD
252 /*
253 * We tolerate no failure path after event creation. It will stay
254 * invariant for the rest of the session.
255 */
30bdb6e4 256 event = ltt_event_create(channel, &metadata_params, NULL);
5dbbdb43 257 if (!event) {
588e6a7f 258 ret = -EINVAL;
271b6681 259 goto create_error;
5dbbdb43
MD
260 }
261 return;
262
85a9ca7f 263create_error:
5dbbdb43
MD
264 WARN_ON(1);
265 return; /* not allowed to return error */
266}
267
268static
c0e31d2e 269int lttng_abi_create_channel(struct file *session_file,
38d024ae 270 struct lttng_kernel_channel __user *uchan_param,
5dbbdb43 271 enum channel_type channel_type)
baf20995 272{
c0e31d2e 273 struct ltt_session *session = session_file->private_data;
5dbbdb43
MD
274 const struct file_operations *fops;
275 const char *transport_name;
baf20995 276 struct ltt_channel *chan;
c0e31d2e 277 struct file *chan_file;
38d024ae 278 struct lttng_kernel_channel chan_param;
baf20995 279 int chan_fd;
ad1c05e1 280 int ret = 0;
baf20995 281
653fe716 282 if (copy_from_user(&chan_param, uchan_param, sizeof(chan_param)))
baf20995 283 return -EFAULT;
1c25284c 284 chan_fd = get_unused_fd();
baf20995
MD
285 if (chan_fd < 0) {
286 ret = chan_fd;
287 goto fd_error;
288 }
c0e31d2e 289 chan_file = anon_inode_getfile("[lttng_channel]",
ad1c05e1 290 &lttng_channel_fops,
03037b98 291 NULL, O_RDWR);
c0e31d2e
MD
292 if (IS_ERR(chan_file)) {
293 ret = PTR_ERR(chan_file);
baf20995
MD
294 goto file_error;
295 }
5dbbdb43
MD
296 switch (channel_type) {
297 case PER_CPU_CHANNEL:
96ba7208
JD
298 if (chan_param.output == LTTNG_KERNEL_SPLICE) {
299 transport_name = chan_param.overwrite ?
300 "relay-overwrite" : "relay-discard";
301 } else if (chan_param.output == LTTNG_KERNEL_MMAP) {
302 transport_name = chan_param.overwrite ?
303 "relay-overwrite-mmap" : "relay-discard-mmap";
304 } else {
305 return -EINVAL;
306 }
5dbbdb43
MD
307 fops = &lttng_channel_fops;
308 break;
5dbbdb43 309 case METADATA_CHANNEL:
96ba7208
JD
310 if (chan_param.output == LTTNG_KERNEL_SPLICE)
311 transport_name = "relay-metadata";
312 else if (chan_param.output == LTTNG_KERNEL_MMAP)
313 transport_name = "relay-metadata-mmap";
314 else
315 return -EINVAL;
5dbbdb43
MD
316 fops = &lttng_metadata_fops;
317 break;
318 default:
319 transport_name = "<unknown>";
320 break;
321 }
03037b98
MD
322 /*
323 * We tolerate no failure path after channel creation. It will stay
324 * invariant for the rest of the session.
325 */
5dbbdb43 326 chan = ltt_channel_create(session, transport_name, NULL,
11b5a3c2
MD
327 chan_param.subbuf_size,
328 chan_param.num_subbuf,
329 chan_param.switch_timer_interval,
330 chan_param.read_timer_interval);
03037b98 331 if (!chan) {
f3d01b96 332 ret = -EINVAL;
03037b98
MD
333 goto chan_error;
334 }
11b5a3c2 335 chan->file = chan_file;
c0e31d2e
MD
336 chan_file->private_data = chan;
337 fd_install(chan_fd, chan_file);
cd4bd11f 338 if (channel_type == METADATA_CHANNEL) {
cd4bd11f 339 session->metadata = chan;
4f1c3952 340 lttng_metadata_create_events(chan_file);
cd4bd11f 341 }
5dbbdb43 342
ad1c05e1 343 /* The channel created holds a reference on the session */
b0caa15a 344 atomic_long_inc(&session_file->f_count);
ad1c05e1 345
baf20995
MD
346 return chan_fd;
347
03037b98 348chan_error:
c0e31d2e 349 fput(chan_file);
baf20995
MD
350file_error:
351 put_unused_fd(chan_fd);
352fd_error:
baf20995
MD
353 return ret;
354}
355
356/**
ad1c05e1 357 * lttng_session_ioctl - lttng session fd ioctl
baf20995 358 *
c0e31d2e 359 * @file: the file
baf20995
MD
360 * @cmd: the command
361 * @arg: command arg
362 *
363 * This ioctl implements lttng commands:
38d024ae 364 * LTTNG_KERNEL_CHANNEL
baf20995 365 * Returns a LTTng channel file descriptor
e64957da
MD
366 * LTTNG_KERNEL_ENABLE
367 * Enables tracing for a session (weak enable)
368 * LTTNG_KERNEL_DISABLE
369 * Disables tracing for a session (strong disable)
8070f5c0
MD
370 * LTTNG_KERNEL_METADATA
371 * Returns a LTTng metadata file descriptor
ad1c05e1
MD
372 *
373 * The returned channel will be deleted when its file descriptor is closed.
374 */
375static
c0e31d2e 376long lttng_session_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
ad1c05e1 377{
c0e31d2e
MD
378 struct ltt_session *session = file->private_data;
379
ad1c05e1 380 switch (cmd) {
38d024ae 381 case LTTNG_KERNEL_CHANNEL:
5dbbdb43 382 return lttng_abi_create_channel(file,
38d024ae 383 (struct lttng_kernel_channel __user *) arg,
5dbbdb43 384 PER_CPU_CHANNEL);
38d024ae 385 case LTTNG_KERNEL_SESSION_START:
e64957da
MD
386 case LTTNG_KERNEL_ENABLE:
387 return ltt_session_enable(session);
38d024ae 388 case LTTNG_KERNEL_SESSION_STOP:
e64957da
MD
389 case LTTNG_KERNEL_DISABLE:
390 return ltt_session_disable(session);
38d024ae 391 case LTTNG_KERNEL_METADATA:
5dbbdb43 392 return lttng_abi_create_channel(file,
38d024ae 393 (struct lttng_kernel_channel __user *) arg,
5dbbdb43 394 METADATA_CHANNEL);
ad1c05e1
MD
395 default:
396 return -ENOIOCTLCMD;
397 }
398}
399
03037b98
MD
400/*
401 * Called when the last file reference is dropped.
402 *
403 * Big fat note: channels and events are invariant for the whole session after
404 * their creation. So this session destruction also destroys all channel and
405 * event structures specific to this session (they are not destroyed when their
406 * individual file is released).
407 */
ad1c05e1 408static
03037b98 409int lttng_session_release(struct inode *inode, struct file *file)
ad1c05e1 410{
03037b98 411 struct ltt_session *session = file->private_data;
c269fff4
MD
412
413 if (session)
414 ltt_session_destroy(session);
11b5a3c2 415 return 0;
ad1c05e1 416}
ad1c05e1
MD
417
418static const struct file_operations lttng_session_fops = {
a33c9927 419 .owner = THIS_MODULE,
03037b98 420 .release = lttng_session_release,
ad1c05e1
MD
421 .unlocked_ioctl = lttng_session_ioctl,
422#ifdef CONFIG_COMPAT
03037b98 423 .compat_ioctl = lttng_session_ioctl,
ad1c05e1 424#endif
11b5a3c2 425};
ad1c05e1
MD
426
427static
c0e31d2e 428int lttng_abi_open_stream(struct file *channel_file)
ad1c05e1 429{
c0e31d2e 430 struct ltt_channel *channel = channel_file->private_data;
ad1c05e1
MD
431 struct lib_ring_buffer *buf;
432 int stream_fd, ret;
11b5a3c2 433 struct file *stream_file;
ad1c05e1 434
11b5a3c2 435 buf = channel->ops->buffer_read_open(channel->chan);
ad1c05e1
MD
436 if (!buf)
437 return -ENOENT;
438
1c25284c 439 stream_fd = get_unused_fd();
ad1c05e1
MD
440 if (stream_fd < 0) {
441 ret = stream_fd;
442 goto fd_error;
443 }
c0e31d2e 444 stream_file = anon_inode_getfile("[lttng_stream]",
7f57c73c 445 &lib_ring_buffer_file_operations,
ad1c05e1 446 buf, O_RDWR);
c0e31d2e
MD
447 if (IS_ERR(stream_file)) {
448 ret = PTR_ERR(stream_file);
ad1c05e1
MD
449 goto file_error;
450 }
409453cb
MD
451 /*
452 * OPEN_FMODE, called within anon_inode_getfile/alloc_file, don't honor
453 * FMODE_LSEEK, FMODE_PREAD nor FMODE_PWRITE. We need to read from this
454 * file descriptor, so we set FMODE_PREAD here.
455 */
d7b6f197 456 stream_file->f_mode |= FMODE_PREAD;
c0e31d2e 457 fd_install(stream_fd, stream_file);
dda6a249
MD
458 /*
459 * The stream holds a reference to the channel within the generic ring
460 * buffer library, so no need to hold a refcount on the channel and
461 * session files here.
462 */
ad1c05e1
MD
463 return stream_fd;
464
465file_error:
466 put_unused_fd(stream_fd);
467fd_error:
11b5a3c2 468 channel->ops->buffer_read_close(buf);
ad1c05e1
MD
469 return ret;
470}
471
653fe716 472static
c0e31d2e 473int lttng_abi_create_event(struct file *channel_file,
38d024ae 474 struct lttng_kernel_event __user *uevent_param)
653fe716 475{
c0e31d2e 476 struct ltt_channel *channel = channel_file->private_data;
653fe716 477 struct ltt_event *event;
38d024ae 478 struct lttng_kernel_event event_param;
653fe716 479 int event_fd, ret;
11b5a3c2 480 struct file *event_file;
653fe716
MD
481
482 if (copy_from_user(&event_param, uevent_param, sizeof(event_param)))
483 return -EFAULT;
30bdb6e4 484 event_param.name[LTTNG_SYM_NAME_LEN - 1] = '\0';
e0a7a7c4 485 switch (event_param.instrumentation) {
ab2277d6 486 case LTTNG_KERNEL_KPROBE:
e0a7a7c4
MD
487 event_param.u.kprobe.symbol_name[LTTNG_SYM_NAME_LEN - 1] = '\0';
488 break;
ab2277d6 489 case LTTNG_KERNEL_FUNCTION:
e0a7a7c4
MD
490 event_param.u.ftrace.symbol_name[LTTNG_SYM_NAME_LEN - 1] = '\0';
491 break;
492 default:
493 break;
494 }
1c25284c 495 event_fd = get_unused_fd();
653fe716
MD
496 if (event_fd < 0) {
497 ret = event_fd;
498 goto fd_error;
499 }
c0e31d2e 500 event_file = anon_inode_getfile("[lttng_event]",
3b923e5b 501 &lttng_event_fops,
03037b98 502 NULL, O_RDWR);
c0e31d2e
MD
503 if (IS_ERR(event_file)) {
504 ret = PTR_ERR(event_file);
653fe716
MD
505 goto file_error;
506 }
03037b98
MD
507 /*
508 * We tolerate no failure path after event creation. It will stay
509 * invariant for the rest of the session.
510 */
30bdb6e4 511 event = ltt_event_create(channel, &event_param, NULL);
03037b98 512 if (!event) {
271b6681 513 ret = -EINVAL;
d6d808f3 514 goto event_error;
03037b98 515 }
c0e31d2e
MD
516 event_file->private_data = event;
517 fd_install(event_fd, event_file);
653fe716 518 /* The event holds a reference on the channel */
b0caa15a 519 atomic_long_inc(&channel_file->f_count);
653fe716
MD
520 return event_fd;
521
03037b98 522event_error:
c0e31d2e 523 fput(event_file);
653fe716
MD
524file_error:
525 put_unused_fd(event_fd);
526fd_error:
653fe716
MD
527 return ret;
528}
ad1c05e1
MD
529
530/**
531 * lttng_channel_ioctl - lttng syscall through ioctl
532 *
c0e31d2e 533 * @file: the file
ad1c05e1
MD
534 * @cmd: the command
535 * @arg: command arg
536 *
537 * This ioctl implements lttng commands:
38d024ae 538 * LTTNG_KERNEL_STREAM
ad1c05e1
MD
539 * Returns an event stream file descriptor or failure.
540 * (typically, one event stream records events from one CPU)
38d024ae 541 * LTTNG_KERNEL_EVENT
ad1c05e1 542 * Returns an event file descriptor or failure.
8070f5c0
MD
543 * LTTNG_KERNEL_CONTEXT
544 * Prepend a context field to each event in the channel
e64957da
MD
545 * LTTNG_KERNEL_ENABLE
546 * Enable recording for events in this channel (weak enable)
547 * LTTNG_KERNEL_DISABLE
548 * Disable recording for events in this channel (strong disable)
baf20995 549 *
baf20995
MD
550 * Channel and event file descriptors also hold a reference on the session.
551 */
ad1c05e1 552static
c0e31d2e 553long lttng_channel_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
baf20995 554{
8070f5c0
MD
555 struct ltt_channel *channel = file->private_data;
556
baf20995 557 switch (cmd) {
38d024ae 558 case LTTNG_KERNEL_STREAM:
c0e31d2e 559 return lttng_abi_open_stream(file);
38d024ae
MD
560 case LTTNG_KERNEL_EVENT:
561 return lttng_abi_create_event(file, (struct lttng_kernel_event __user *) arg);
8070f5c0
MD
562 case LTTNG_KERNEL_CONTEXT:
563 return lttng_abi_add_context(file,
564 (struct lttng_kernel_context __user *) arg,
565 &channel->ctx, channel->session);
e64957da
MD
566 case LTTNG_KERNEL_ENABLE:
567 return ltt_channel_enable(channel);
568 case LTTNG_KERNEL_DISABLE:
569 return ltt_channel_disable(channel);
baf20995
MD
570 default:
571 return -ENOIOCTLCMD;
572 }
573}
574
5dbbdb43
MD
575/**
576 * lttng_metadata_ioctl - lttng syscall through ioctl
577 *
578 * @file: the file
579 * @cmd: the command
580 * @arg: command arg
581 *
582 * This ioctl implements lttng commands:
38d024ae 583 * LTTNG_KERNEL_STREAM
5dbbdb43
MD
584 * Returns an event stream file descriptor or failure.
585 *
586 * Channel and event file descriptors also hold a reference on the session.
587 */
588static
589long lttng_metadata_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
590{
591 switch (cmd) {
38d024ae 592 case LTTNG_KERNEL_STREAM:
5dbbdb43
MD
593 return lttng_abi_open_stream(file);
594 default:
595 return -ENOIOCTLCMD;
596 }
597}
598
653fe716
MD
599/**
600 * lttng_channel_poll - lttng stream addition/removal monitoring
601 *
c0e31d2e 602 * @file: the file
653fe716
MD
603 * @wait: poll table
604 */
c0e31d2e 605unsigned int lttng_channel_poll(struct file *file, poll_table *wait)
653fe716 606{
c0e31d2e 607 struct ltt_channel *channel = file->private_data;
653fe716
MD
608 unsigned int mask = 0;
609
c0e31d2e 610 if (file->f_mode & FMODE_READ) {
a33e44a6 611 poll_wait_set_exclusive(wait);
24cedcfe
MD
612 poll_wait(file, channel->ops->get_hp_wait_queue(channel->chan),
613 wait);
653fe716 614
254ec7bc
MD
615 if (channel->ops->is_disabled(channel->chan))
616 return POLLERR;
24cedcfe 617 if (channel->ops->is_finalized(channel->chan))
653fe716 618 return POLLHUP;
f71ecafa 619 if (channel->ops->buffer_has_read_closed_stream(channel->chan))
653fe716 620 return POLLIN | POLLRDNORM;
f71ecafa 621 return 0;
653fe716
MD
622 }
623 return mask;
624
625}
626
0a84a57f
MD
627static
628int lttng_channel_release(struct inode *inode, struct file *file)
629{
630 struct ltt_channel *channel = file->private_data;
c269fff4
MD
631
632 if (channel)
633 fput(channel->session->file);
0a84a57f
MD
634 return 0;
635}
636
ad1c05e1 637static const struct file_operations lttng_channel_fops = {
a33c9927 638 .owner = THIS_MODULE,
03037b98 639 .release = lttng_channel_release,
653fe716 640 .poll = lttng_channel_poll,
ad1c05e1 641 .unlocked_ioctl = lttng_channel_ioctl,
baf20995 642#ifdef CONFIG_COMPAT
03037b98 643 .compat_ioctl = lttng_channel_ioctl,
baf20995 644#endif
11b5a3c2 645};
baf20995 646
5dbbdb43 647static const struct file_operations lttng_metadata_fops = {
a33c9927 648 .owner = THIS_MODULE,
5dbbdb43
MD
649 .release = lttng_channel_release,
650 .unlocked_ioctl = lttng_metadata_ioctl,
651#ifdef CONFIG_COMPAT
652 .compat_ioctl = lttng_metadata_ioctl,
653#endif
654};
655
8070f5c0
MD
656/**
657 * lttng_event_ioctl - lttng syscall through ioctl
658 *
659 * @file: the file
660 * @cmd: the command
661 * @arg: command arg
662 *
663 * This ioctl implements lttng commands:
8070f5c0
MD
664 * LTTNG_KERNEL_CONTEXT
665 * Prepend a context field to each record of this event
e64957da
MD
666 * LTTNG_KERNEL_ENABLE
667 * Enable recording for this event (weak enable)
668 * LTTNG_KERNEL_DISABLE
669 * Disable recording for this event (strong disable)
8070f5c0
MD
670 */
671static
672long lttng_event_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
673{
674 struct ltt_event *event = file->private_data;
675
676 switch (cmd) {
677 case LTTNG_KERNEL_CONTEXT:
678 return lttng_abi_add_context(file,
679 (struct lttng_kernel_context __user *) arg,
680 &event->ctx, event->chan->session);
e64957da
MD
681 case LTTNG_KERNEL_ENABLE:
682 return ltt_event_enable(event);
683 case LTTNG_KERNEL_DISABLE:
684 return ltt_event_disable(event);
8070f5c0
MD
685 default:
686 return -ENOIOCTLCMD;
687 }
688}
689
0a84a57f
MD
690static
691int lttng_event_release(struct inode *inode, struct file *file)
692{
693 struct ltt_event *event = file->private_data;
c269fff4 694
aa7c23a9 695 if (event)
c269fff4 696 fput(event->chan->file);
0a84a57f
MD
697 return 0;
698}
699
3b923e5b 700/* TODO: filter control ioctl */
0a84a57f 701static const struct file_operations lttng_event_fops = {
a33c9927 702 .owner = THIS_MODULE,
0a84a57f 703 .release = lttng_event_release,
8070f5c0
MD
704 .unlocked_ioctl = lttng_event_ioctl,
705#ifdef CONFIG_COMPAT
706 .compat_ioctl = lttng_event_ioctl,
707#endif
11b5a3c2 708};
0a84a57f 709
1c25284c 710int __init ltt_debugfs_abi_init(void)
baf20995
MD
711{
712 int ret = 0;
713
6d2a620c 714 wrapper_vmalloc_sync_all();
11b5a3c2 715 lttng_dentry = debugfs_create_file("lttng", S_IWUSR, NULL, NULL,
f3d01b96 716 &lttng_fops);
11b5a3c2 717 if (IS_ERR(lttng_dentry) || !lttng_dentry) {
baf20995
MD
718 printk(KERN_ERR "Error creating LTTng control file\n");
719 ret = -ENOMEM;
720 goto error;
721 }
722error:
723 return ret;
724}
725
1c25284c 726void __exit ltt_debugfs_abi_exit(void)
baf20995 727{
11b5a3c2 728 debugfs_remove(lttng_dentry);
baf20995 729}
This page took 0.061767 seconds and 4 git commands to generate.