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