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