Implement detailed syscall event probe
[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;
57105fc2
MD
223 case LTTNG_KERNEL_CALIBRATE:
224 {
3db41b2c
MD
225 struct lttng_kernel_calibrate __user *ucalibrate =
226 (struct lttng_kernel_calibrate __user *) arg;
227 struct lttng_kernel_calibrate calibrate;
57105fc2
MD
228 int ret;
229
230 if (copy_from_user(&calibrate, ucalibrate, sizeof(calibrate)))
231 return -EFAULT;
232 ret = lttng_calibrate(&calibrate);
233 if (copy_to_user(ucalibrate, &calibrate, sizeof(calibrate)))
234 return -EFAULT;
235 return ret;
236 }
ad1c05e1
MD
237 default:
238 return -ENOIOCTLCMD;
239 }
240}
241
ad1c05e1 242static const struct file_operations lttng_fops = {
a33c9927 243 .owner = THIS_MODULE,
ad1c05e1
MD
244 .unlocked_ioctl = lttng_ioctl,
245#ifdef CONFIG_COMPAT
03037b98 246 .compat_ioctl = lttng_ioctl,
ad1c05e1 247#endif
11b5a3c2 248};
ad1c05e1 249
5dbbdb43
MD
250/*
251 * We tolerate no failure in this function (if one happens, we print a dmesg
252 * error, but cannot return any error, because the channel information is
253 * invariant.
254 */
255static
256void lttng_metadata_create_events(struct file *channel_file)
257{
258 struct ltt_channel *channel = channel_file->private_data;
e70a4758 259 static struct lttng_kernel_event metadata_params = {
ab2277d6 260 .instrumentation = LTTNG_KERNEL_TRACEPOINT,
e70a4758
MD
261 .name = "lttng_metadata",
262 };
5dbbdb43 263 struct ltt_event *event;
5dbbdb43 264
5dbbdb43
MD
265 /*
266 * We tolerate no failure path after event creation. It will stay
267 * invariant for the rest of the session.
268 */
30bdb6e4 269 event = ltt_event_create(channel, &metadata_params, NULL);
5dbbdb43 270 if (!event) {
271b6681 271 goto create_error;
5dbbdb43
MD
272 }
273 return;
274
85a9ca7f 275create_error:
5dbbdb43
MD
276 WARN_ON(1);
277 return; /* not allowed to return error */
278}
279
280static
c0e31d2e 281int lttng_abi_create_channel(struct file *session_file,
38d024ae 282 struct lttng_kernel_channel __user *uchan_param,
5dbbdb43 283 enum channel_type channel_type)
baf20995 284{
c0e31d2e 285 struct ltt_session *session = session_file->private_data;
88dfd899 286 const struct file_operations *fops = NULL;
5dbbdb43 287 const char *transport_name;
baf20995 288 struct ltt_channel *chan;
c0e31d2e 289 struct file *chan_file;
38d024ae 290 struct lttng_kernel_channel chan_param;
baf20995 291 int chan_fd;
ad1c05e1 292 int ret = 0;
baf20995 293
653fe716 294 if (copy_from_user(&chan_param, uchan_param, sizeof(chan_param)))
baf20995 295 return -EFAULT;
1c25284c 296 chan_fd = get_unused_fd();
baf20995
MD
297 if (chan_fd < 0) {
298 ret = chan_fd;
299 goto fd_error;
300 }
88dfd899
MD
301 switch (channel_type) {
302 case PER_CPU_CHANNEL:
303 fops = &lttng_channel_fops;
304 break;
305 case METADATA_CHANNEL:
306 fops = &lttng_metadata_fops;
307 break;
308 }
309
c0e31d2e 310 chan_file = anon_inode_getfile("[lttng_channel]",
88dfd899 311 fops,
03037b98 312 NULL, O_RDWR);
c0e31d2e
MD
313 if (IS_ERR(chan_file)) {
314 ret = PTR_ERR(chan_file);
baf20995
MD
315 goto file_error;
316 }
5dbbdb43
MD
317 switch (channel_type) {
318 case PER_CPU_CHANNEL:
96ba7208
JD
319 if (chan_param.output == LTTNG_KERNEL_SPLICE) {
320 transport_name = chan_param.overwrite ?
321 "relay-overwrite" : "relay-discard";
322 } else if (chan_param.output == LTTNG_KERNEL_MMAP) {
323 transport_name = chan_param.overwrite ?
324 "relay-overwrite-mmap" : "relay-discard-mmap";
325 } else {
326 return -EINVAL;
327 }
5dbbdb43 328 break;
5dbbdb43 329 case METADATA_CHANNEL:
96ba7208
JD
330 if (chan_param.output == LTTNG_KERNEL_SPLICE)
331 transport_name = "relay-metadata";
332 else if (chan_param.output == LTTNG_KERNEL_MMAP)
333 transport_name = "relay-metadata-mmap";
334 else
335 return -EINVAL;
5dbbdb43
MD
336 break;
337 default:
338 transport_name = "<unknown>";
339 break;
340 }
03037b98
MD
341 /*
342 * We tolerate no failure path after channel creation. It will stay
343 * invariant for the rest of the session.
344 */
5dbbdb43 345 chan = ltt_channel_create(session, transport_name, NULL,
11b5a3c2
MD
346 chan_param.subbuf_size,
347 chan_param.num_subbuf,
348 chan_param.switch_timer_interval,
349 chan_param.read_timer_interval);
03037b98 350 if (!chan) {
f3d01b96 351 ret = -EINVAL;
03037b98
MD
352 goto chan_error;
353 }
11b5a3c2 354 chan->file = chan_file;
c0e31d2e
MD
355 chan_file->private_data = chan;
356 fd_install(chan_fd, chan_file);
cd4bd11f 357 if (channel_type == METADATA_CHANNEL) {
cd4bd11f 358 session->metadata = chan;
4f1c3952 359 lttng_metadata_create_events(chan_file);
cd4bd11f 360 }
5dbbdb43 361
ad1c05e1 362 /* The channel created holds a reference on the session */
b0caa15a 363 atomic_long_inc(&session_file->f_count);
ad1c05e1 364
baf20995
MD
365 return chan_fd;
366
03037b98 367chan_error:
c0e31d2e 368 fput(chan_file);
baf20995
MD
369file_error:
370 put_unused_fd(chan_fd);
371fd_error:
baf20995
MD
372 return ret;
373}
374
375/**
ad1c05e1 376 * lttng_session_ioctl - lttng session fd ioctl
baf20995 377 *
c0e31d2e 378 * @file: the file
baf20995
MD
379 * @cmd: the command
380 * @arg: command arg
381 *
382 * This ioctl implements lttng commands:
38d024ae 383 * LTTNG_KERNEL_CHANNEL
baf20995 384 * Returns a LTTng channel file descriptor
e64957da
MD
385 * LTTNG_KERNEL_ENABLE
386 * Enables tracing for a session (weak enable)
387 * LTTNG_KERNEL_DISABLE
388 * Disables tracing for a session (strong disable)
8070f5c0
MD
389 * LTTNG_KERNEL_METADATA
390 * Returns a LTTng metadata file descriptor
ad1c05e1
MD
391 *
392 * The returned channel will be deleted when its file descriptor is closed.
393 */
394static
c0e31d2e 395long lttng_session_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
ad1c05e1 396{
c0e31d2e
MD
397 struct ltt_session *session = file->private_data;
398
ad1c05e1 399 switch (cmd) {
38d024ae 400 case LTTNG_KERNEL_CHANNEL:
5dbbdb43 401 return lttng_abi_create_channel(file,
38d024ae 402 (struct lttng_kernel_channel __user *) arg,
5dbbdb43 403 PER_CPU_CHANNEL);
38d024ae 404 case LTTNG_KERNEL_SESSION_START:
e64957da
MD
405 case LTTNG_KERNEL_ENABLE:
406 return ltt_session_enable(session);
38d024ae 407 case LTTNG_KERNEL_SESSION_STOP:
e64957da
MD
408 case LTTNG_KERNEL_DISABLE:
409 return ltt_session_disable(session);
38d024ae 410 case LTTNG_KERNEL_METADATA:
5dbbdb43 411 return lttng_abi_create_channel(file,
38d024ae 412 (struct lttng_kernel_channel __user *) arg,
5dbbdb43 413 METADATA_CHANNEL);
ad1c05e1
MD
414 default:
415 return -ENOIOCTLCMD;
416 }
417}
418
03037b98
MD
419/*
420 * Called when the last file reference is dropped.
421 *
422 * Big fat note: channels and events are invariant for the whole session after
423 * their creation. So this session destruction also destroys all channel and
424 * event structures specific to this session (they are not destroyed when their
425 * individual file is released).
426 */
ad1c05e1 427static
03037b98 428int lttng_session_release(struct inode *inode, struct file *file)
ad1c05e1 429{
03037b98 430 struct ltt_session *session = file->private_data;
c269fff4
MD
431
432 if (session)
433 ltt_session_destroy(session);
11b5a3c2 434 return 0;
ad1c05e1 435}
ad1c05e1
MD
436
437static const struct file_operations lttng_session_fops = {
a33c9927 438 .owner = THIS_MODULE,
03037b98 439 .release = lttng_session_release,
ad1c05e1
MD
440 .unlocked_ioctl = lttng_session_ioctl,
441#ifdef CONFIG_COMPAT
03037b98 442 .compat_ioctl = lttng_session_ioctl,
ad1c05e1 443#endif
11b5a3c2 444};
ad1c05e1
MD
445
446static
c0e31d2e 447int lttng_abi_open_stream(struct file *channel_file)
ad1c05e1 448{
c0e31d2e 449 struct ltt_channel *channel = channel_file->private_data;
ad1c05e1
MD
450 struct lib_ring_buffer *buf;
451 int stream_fd, ret;
11b5a3c2 452 struct file *stream_file;
ad1c05e1 453
11b5a3c2 454 buf = channel->ops->buffer_read_open(channel->chan);
ad1c05e1
MD
455 if (!buf)
456 return -ENOENT;
457
1c25284c 458 stream_fd = get_unused_fd();
ad1c05e1
MD
459 if (stream_fd < 0) {
460 ret = stream_fd;
461 goto fd_error;
462 }
c0e31d2e 463 stream_file = anon_inode_getfile("[lttng_stream]",
7f57c73c 464 &lib_ring_buffer_file_operations,
ad1c05e1 465 buf, O_RDWR);
c0e31d2e
MD
466 if (IS_ERR(stream_file)) {
467 ret = PTR_ERR(stream_file);
ad1c05e1
MD
468 goto file_error;
469 }
409453cb
MD
470 /*
471 * OPEN_FMODE, called within anon_inode_getfile/alloc_file, don't honor
472 * FMODE_LSEEK, FMODE_PREAD nor FMODE_PWRITE. We need to read from this
473 * file descriptor, so we set FMODE_PREAD here.
474 */
d7b6f197 475 stream_file->f_mode |= FMODE_PREAD;
c0e31d2e 476 fd_install(stream_fd, stream_file);
dda6a249
MD
477 /*
478 * The stream holds a reference to the channel within the generic ring
479 * buffer library, so no need to hold a refcount on the channel and
480 * session files here.
481 */
ad1c05e1
MD
482 return stream_fd;
483
484file_error:
485 put_unused_fd(stream_fd);
486fd_error:
11b5a3c2 487 channel->ops->buffer_read_close(buf);
ad1c05e1
MD
488 return ret;
489}
490
653fe716 491static
c0e31d2e 492int lttng_abi_create_event(struct file *channel_file,
38d024ae 493 struct lttng_kernel_event __user *uevent_param)
653fe716 494{
c0e31d2e 495 struct ltt_channel *channel = channel_file->private_data;
653fe716 496 struct ltt_event *event;
38d024ae 497 struct lttng_kernel_event event_param;
653fe716 498 int event_fd, ret;
11b5a3c2 499 struct file *event_file;
653fe716
MD
500
501 if (copy_from_user(&event_param, uevent_param, sizeof(event_param)))
502 return -EFAULT;
30bdb6e4 503 event_param.name[LTTNG_SYM_NAME_LEN - 1] = '\0';
e0a7a7c4 504 switch (event_param.instrumentation) {
7371f44c
MD
505 case LTTNG_KERNEL_KRETPROBE:
506 event_param.u.kretprobe.symbol_name[LTTNG_SYM_NAME_LEN - 1] = '\0';
507 break;
ab2277d6 508 case LTTNG_KERNEL_KPROBE:
e0a7a7c4
MD
509 event_param.u.kprobe.symbol_name[LTTNG_SYM_NAME_LEN - 1] = '\0';
510 break;
ab2277d6 511 case LTTNG_KERNEL_FUNCTION:
e0a7a7c4
MD
512 event_param.u.ftrace.symbol_name[LTTNG_SYM_NAME_LEN - 1] = '\0';
513 break;
514 default:
515 break;
516 }
1ec65de1
MD
517 switch (event_param.instrumentation) {
518 default:
519 event_fd = get_unused_fd();
520 if (event_fd < 0) {
521 ret = event_fd;
522 goto fd_error;
523 }
524 event_file = anon_inode_getfile("[lttng_event]",
525 &lttng_event_fops,
526 NULL, O_RDWR);
527 if (IS_ERR(event_file)) {
528 ret = PTR_ERR(event_file);
529 goto file_error;
530 }
531 /*
532 * We tolerate no failure path after event creation. It
533 * will stay invariant for the rest of the session.
534 */
535 event = ltt_event_create(channel, &event_param, NULL);
536 if (!event) {
537 ret = -EINVAL;
538 goto event_error;
539 }
540 event_file->private_data = event;
541 fd_install(event_fd, event_file);
542 /* The event holds a reference on the channel */
543 atomic_long_inc(&channel_file->f_count);
544 break;
545 case LTTNG_KERNEL_SYSCALLS:
546 ret = lttng_syscalls_register(channel, NULL);
547 if (ret)
548 goto fd_error;
549 event_fd = 0;
550 break;
03037b98 551 }
653fe716
MD
552 return event_fd;
553
03037b98 554event_error:
c0e31d2e 555 fput(event_file);
653fe716
MD
556file_error:
557 put_unused_fd(event_fd);
558fd_error:
653fe716
MD
559 return ret;
560}
ad1c05e1
MD
561
562/**
563 * lttng_channel_ioctl - lttng syscall through ioctl
564 *
c0e31d2e 565 * @file: the file
ad1c05e1
MD
566 * @cmd: the command
567 * @arg: command arg
568 *
569 * This ioctl implements lttng commands:
38d024ae 570 * LTTNG_KERNEL_STREAM
ad1c05e1
MD
571 * Returns an event stream file descriptor or failure.
572 * (typically, one event stream records events from one CPU)
38d024ae 573 * LTTNG_KERNEL_EVENT
ad1c05e1 574 * Returns an event file descriptor or failure.
8070f5c0
MD
575 * LTTNG_KERNEL_CONTEXT
576 * Prepend a context field to each event in the channel
e64957da
MD
577 * LTTNG_KERNEL_ENABLE
578 * Enable recording for events in this channel (weak enable)
579 * LTTNG_KERNEL_DISABLE
580 * Disable recording for events in this channel (strong disable)
baf20995 581 *
baf20995
MD
582 * Channel and event file descriptors also hold a reference on the session.
583 */
ad1c05e1 584static
c0e31d2e 585long lttng_channel_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
baf20995 586{
8070f5c0
MD
587 struct ltt_channel *channel = file->private_data;
588
baf20995 589 switch (cmd) {
38d024ae 590 case LTTNG_KERNEL_STREAM:
c0e31d2e 591 return lttng_abi_open_stream(file);
38d024ae
MD
592 case LTTNG_KERNEL_EVENT:
593 return lttng_abi_create_event(file, (struct lttng_kernel_event __user *) arg);
8070f5c0
MD
594 case LTTNG_KERNEL_CONTEXT:
595 return lttng_abi_add_context(file,
596 (struct lttng_kernel_context __user *) arg,
597 &channel->ctx, channel->session);
e64957da
MD
598 case LTTNG_KERNEL_ENABLE:
599 return ltt_channel_enable(channel);
600 case LTTNG_KERNEL_DISABLE:
601 return ltt_channel_disable(channel);
baf20995
MD
602 default:
603 return -ENOIOCTLCMD;
604 }
605}
606
5dbbdb43
MD
607/**
608 * lttng_metadata_ioctl - lttng syscall through ioctl
609 *
610 * @file: the file
611 * @cmd: the command
612 * @arg: command arg
613 *
614 * This ioctl implements lttng commands:
38d024ae 615 * LTTNG_KERNEL_STREAM
5dbbdb43
MD
616 * Returns an event stream file descriptor or failure.
617 *
618 * Channel and event file descriptors also hold a reference on the session.
619 */
620static
621long lttng_metadata_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
622{
623 switch (cmd) {
38d024ae 624 case LTTNG_KERNEL_STREAM:
5dbbdb43
MD
625 return lttng_abi_open_stream(file);
626 default:
627 return -ENOIOCTLCMD;
628 }
629}
630
653fe716
MD
631/**
632 * lttng_channel_poll - lttng stream addition/removal monitoring
633 *
c0e31d2e 634 * @file: the file
653fe716
MD
635 * @wait: poll table
636 */
c0e31d2e 637unsigned int lttng_channel_poll(struct file *file, poll_table *wait)
653fe716 638{
c0e31d2e 639 struct ltt_channel *channel = file->private_data;
653fe716
MD
640 unsigned int mask = 0;
641
c0e31d2e 642 if (file->f_mode & FMODE_READ) {
a33e44a6 643 poll_wait_set_exclusive(wait);
24cedcfe
MD
644 poll_wait(file, channel->ops->get_hp_wait_queue(channel->chan),
645 wait);
653fe716 646
254ec7bc
MD
647 if (channel->ops->is_disabled(channel->chan))
648 return POLLERR;
24cedcfe 649 if (channel->ops->is_finalized(channel->chan))
653fe716 650 return POLLHUP;
f71ecafa 651 if (channel->ops->buffer_has_read_closed_stream(channel->chan))
653fe716 652 return POLLIN | POLLRDNORM;
f71ecafa 653 return 0;
653fe716
MD
654 }
655 return mask;
656
657}
658
0a84a57f
MD
659static
660int lttng_channel_release(struct inode *inode, struct file *file)
661{
662 struct ltt_channel *channel = file->private_data;
c269fff4
MD
663
664 if (channel)
665 fput(channel->session->file);
0a84a57f
MD
666 return 0;
667}
668
ad1c05e1 669static const struct file_operations lttng_channel_fops = {
a33c9927 670 .owner = THIS_MODULE,
03037b98 671 .release = lttng_channel_release,
653fe716 672 .poll = lttng_channel_poll,
ad1c05e1 673 .unlocked_ioctl = lttng_channel_ioctl,
baf20995 674#ifdef CONFIG_COMPAT
03037b98 675 .compat_ioctl = lttng_channel_ioctl,
baf20995 676#endif
11b5a3c2 677};
baf20995 678
5dbbdb43 679static const struct file_operations lttng_metadata_fops = {
a33c9927 680 .owner = THIS_MODULE,
5dbbdb43
MD
681 .release = lttng_channel_release,
682 .unlocked_ioctl = lttng_metadata_ioctl,
683#ifdef CONFIG_COMPAT
684 .compat_ioctl = lttng_metadata_ioctl,
685#endif
686};
687
8070f5c0
MD
688/**
689 * lttng_event_ioctl - lttng syscall through ioctl
690 *
691 * @file: the file
692 * @cmd: the command
693 * @arg: command arg
694 *
695 * This ioctl implements lttng commands:
8070f5c0
MD
696 * LTTNG_KERNEL_CONTEXT
697 * Prepend a context field to each record of this event
e64957da
MD
698 * LTTNG_KERNEL_ENABLE
699 * Enable recording for this event (weak enable)
700 * LTTNG_KERNEL_DISABLE
701 * Disable recording for this event (strong disable)
8070f5c0
MD
702 */
703static
704long lttng_event_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
705{
706 struct ltt_event *event = file->private_data;
707
708 switch (cmd) {
709 case LTTNG_KERNEL_CONTEXT:
710 return lttng_abi_add_context(file,
711 (struct lttng_kernel_context __user *) arg,
712 &event->ctx, event->chan->session);
e64957da
MD
713 case LTTNG_KERNEL_ENABLE:
714 return ltt_event_enable(event);
715 case LTTNG_KERNEL_DISABLE:
716 return ltt_event_disable(event);
8070f5c0
MD
717 default:
718 return -ENOIOCTLCMD;
719 }
720}
721
0a84a57f
MD
722static
723int lttng_event_release(struct inode *inode, struct file *file)
724{
725 struct ltt_event *event = file->private_data;
c269fff4 726
aa7c23a9 727 if (event)
c269fff4 728 fput(event->chan->file);
0a84a57f
MD
729 return 0;
730}
731
3b923e5b 732/* TODO: filter control ioctl */
0a84a57f 733static const struct file_operations lttng_event_fops = {
a33c9927 734 .owner = THIS_MODULE,
0a84a57f 735 .release = lttng_event_release,
8070f5c0
MD
736 .unlocked_ioctl = lttng_event_ioctl,
737#ifdef CONFIG_COMPAT
738 .compat_ioctl = lttng_event_ioctl,
739#endif
11b5a3c2 740};
0a84a57f 741
1c25284c 742int __init ltt_debugfs_abi_init(void)
baf20995
MD
743{
744 int ret = 0;
745
6d2a620c 746 wrapper_vmalloc_sync_all();
11b5a3c2 747 lttng_dentry = debugfs_create_file("lttng", S_IWUSR, NULL, NULL,
f3d01b96 748 &lttng_fops);
11b5a3c2 749 if (IS_ERR(lttng_dentry) || !lttng_dentry) {
baf20995
MD
750 printk(KERN_ERR "Error creating LTTng control file\n");
751 ret = -ENOMEM;
752 goto error;
753 }
754error:
755 return ret;
756}
757
1c25284c 758void __exit ltt_debugfs_abi_exit(void)
baf20995 759{
11b5a3c2 760 debugfs_remove(lttng_dentry);
baf20995 761}
This page took 0.062528 seconds and 4 git commands to generate.