Migrate tracepoint instrumentation to TP_FIELDS
[lttng-modules.git] / lttng-abi.c
1 /*
2 * lttng-abi.c
3 *
4 * LTTng ABI
5 *
6 * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; only
11 * version 2.1 of the License.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 *
22 *
23 * Mimic system calls for:
24 * - session creation, returns a file descriptor or failure.
25 * - channel creation, returns a file descriptor or failure.
26 * - Operates on a session file descriptor
27 * - Takes all channel options as parameters.
28 * - stream get, returns a file descriptor or failure.
29 * - Operates on a channel file descriptor.
30 * - stream notifier get, returns a file descriptor or failure.
31 * - Operates on a channel file descriptor.
32 * - event creation, returns a file descriptor or failure.
33 * - Operates on a channel file descriptor
34 * - Takes an event name as parameter
35 * - Takes an instrumentation source as parameter
36 * - e.g. tracepoints, dynamic_probes...
37 * - Takes instrumentation source specific arguments.
38 */
39
40 #include <linux/module.h>
41 #include <linux/proc_fs.h>
42 #include <linux/anon_inodes.h>
43 #include <linux/file.h>
44 #include <linux/uaccess.h>
45 #include <linux/slab.h>
46 #include <linux/err.h>
47 #include "wrapper/vmalloc.h" /* for wrapper_vmalloc_sync_all() */
48 #include "wrapper/ringbuffer/vfs.h"
49 #include "wrapper/ringbuffer/backend.h"
50 #include "wrapper/ringbuffer/frontend.h"
51 #include "wrapper/poll.h"
52 #include "wrapper/file.h"
53 #include "lttng-abi.h"
54 #include "lttng-abi-old.h"
55 #include "lttng-events.h"
56 #include "lttng-tracer.h"
57 #include "lib/ringbuffer/frontend_types.h"
58
59 /*
60 * This is LTTng's own personal way to create a system call as an external
61 * module. We use ioctl() on /proc/lttng.
62 */
63
64 static struct proc_dir_entry *lttng_proc_dentry;
65 static const struct file_operations lttng_fops;
66 static const struct file_operations lttng_session_fops;
67 static const struct file_operations lttng_channel_fops;
68 static const struct file_operations lttng_metadata_fops;
69 static const struct file_operations lttng_event_fops;
70 static struct file_operations lttng_stream_ring_buffer_file_operations;
71
72 /*
73 * Teardown management: opened file descriptors keep a refcount on the module,
74 * so it can only exit when all file descriptors are closed.
75 */
76
77 static
78 int lttng_abi_create_session(void)
79 {
80 struct lttng_session *session;
81 struct file *session_file;
82 int session_fd, ret;
83
84 session = lttng_session_create();
85 if (!session)
86 return -ENOMEM;
87 session_fd = lttng_get_unused_fd();
88 if (session_fd < 0) {
89 ret = session_fd;
90 goto fd_error;
91 }
92 session_file = anon_inode_getfile("[lttng_session]",
93 &lttng_session_fops,
94 session, O_RDWR);
95 if (IS_ERR(session_file)) {
96 ret = PTR_ERR(session_file);
97 goto file_error;
98 }
99 session->file = session_file;
100 fd_install(session_fd, session_file);
101 return session_fd;
102
103 file_error:
104 put_unused_fd(session_fd);
105 fd_error:
106 lttng_session_destroy(session);
107 return ret;
108 }
109
110 static
111 int lttng_abi_tracepoint_list(void)
112 {
113 struct file *tracepoint_list_file;
114 int file_fd, ret;
115
116 file_fd = lttng_get_unused_fd();
117 if (file_fd < 0) {
118 ret = file_fd;
119 goto fd_error;
120 }
121
122 tracepoint_list_file = anon_inode_getfile("[lttng_tracepoint_list]",
123 &lttng_tracepoint_list_fops,
124 NULL, O_RDWR);
125 if (IS_ERR(tracepoint_list_file)) {
126 ret = PTR_ERR(tracepoint_list_file);
127 goto file_error;
128 }
129 ret = lttng_tracepoint_list_fops.open(NULL, tracepoint_list_file);
130 if (ret < 0)
131 goto open_error;
132 fd_install(file_fd, tracepoint_list_file);
133 if (file_fd < 0) {
134 ret = file_fd;
135 goto fd_error;
136 }
137 return file_fd;
138
139 open_error:
140 fput(tracepoint_list_file);
141 file_error:
142 put_unused_fd(file_fd);
143 fd_error:
144 return ret;
145 }
146
147 #ifndef CONFIG_HAVE_SYSCALL_TRACEPOINTS
148 static inline
149 int lttng_abi_syscall_list(void)
150 {
151 return -ENOSYS;
152 }
153 #else
154 static
155 int lttng_abi_syscall_list(void)
156 {
157 struct file *syscall_list_file;
158 int file_fd, ret;
159
160 file_fd = lttng_get_unused_fd();
161 if (file_fd < 0) {
162 ret = file_fd;
163 goto fd_error;
164 }
165
166 syscall_list_file = anon_inode_getfile("[lttng_syscall_list]",
167 &lttng_syscall_list_fops,
168 NULL, O_RDWR);
169 if (IS_ERR(syscall_list_file)) {
170 ret = PTR_ERR(syscall_list_file);
171 goto file_error;
172 }
173 ret = lttng_syscall_list_fops.open(NULL, syscall_list_file);
174 if (ret < 0)
175 goto open_error;
176 fd_install(file_fd, syscall_list_file);
177 if (file_fd < 0) {
178 ret = file_fd;
179 goto fd_error;
180 }
181 return file_fd;
182
183 open_error:
184 fput(syscall_list_file);
185 file_error:
186 put_unused_fd(file_fd);
187 fd_error:
188 return ret;
189 }
190 #endif
191
192 static
193 void lttng_abi_tracer_version(struct lttng_kernel_tracer_version *v)
194 {
195 v->major = LTTNG_MODULES_MAJOR_VERSION;
196 v->minor = LTTNG_MODULES_MINOR_VERSION;
197 v->patchlevel = LTTNG_MODULES_PATCHLEVEL_VERSION;
198 }
199
200 static
201 void lttng_abi_tracer_abi_version(struct lttng_kernel_tracer_abi_version *v)
202 {
203 v->major = LTTNG_MODULES_ABI_MAJOR_VERSION;
204 v->minor = LTTNG_MODULES_ABI_MINOR_VERSION;
205 }
206
207 static
208 long lttng_abi_add_context(struct file *file,
209 struct lttng_kernel_context *context_param,
210 struct lttng_ctx **ctx, struct lttng_session *session)
211 {
212
213 if (session->been_active)
214 return -EPERM;
215
216 switch (context_param->ctx) {
217 case LTTNG_KERNEL_CONTEXT_PID:
218 return lttng_add_pid_to_ctx(ctx);
219 case LTTNG_KERNEL_CONTEXT_PRIO:
220 return lttng_add_prio_to_ctx(ctx);
221 case LTTNG_KERNEL_CONTEXT_NICE:
222 return lttng_add_nice_to_ctx(ctx);
223 case LTTNG_KERNEL_CONTEXT_VPID:
224 return lttng_add_vpid_to_ctx(ctx);
225 case LTTNG_KERNEL_CONTEXT_TID:
226 return lttng_add_tid_to_ctx(ctx);
227 case LTTNG_KERNEL_CONTEXT_VTID:
228 return lttng_add_vtid_to_ctx(ctx);
229 case LTTNG_KERNEL_CONTEXT_PPID:
230 return lttng_add_ppid_to_ctx(ctx);
231 case LTTNG_KERNEL_CONTEXT_VPPID:
232 return lttng_add_vppid_to_ctx(ctx);
233 case LTTNG_KERNEL_CONTEXT_PERF_COUNTER:
234 context_param->u.perf_counter.name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
235 return lttng_add_perf_counter_to_ctx(context_param->u.perf_counter.type,
236 context_param->u.perf_counter.config,
237 context_param->u.perf_counter.name,
238 ctx);
239 case LTTNG_KERNEL_CONTEXT_PROCNAME:
240 return lttng_add_procname_to_ctx(ctx);
241 case LTTNG_KERNEL_CONTEXT_HOSTNAME:
242 return lttng_add_hostname_to_ctx(ctx);
243 default:
244 return -EINVAL;
245 }
246 }
247
248 /**
249 * lttng_ioctl - lttng syscall through ioctl
250 *
251 * @file: the file
252 * @cmd: the command
253 * @arg: command arg
254 *
255 * This ioctl implements lttng commands:
256 * LTTNG_KERNEL_SESSION
257 * Returns a LTTng trace session file descriptor
258 * LTTNG_KERNEL_TRACER_VERSION
259 * Returns the LTTng kernel tracer version
260 * LTTNG_KERNEL_TRACEPOINT_LIST
261 * Returns a file descriptor listing available tracepoints
262 * LTTNG_KERNEL_WAIT_QUIESCENT
263 * Returns after all previously running probes have completed
264 * LTTNG_KERNEL_TRACER_ABI_VERSION
265 * Returns the LTTng kernel tracer ABI version
266 *
267 * The returned session will be deleted when its file descriptor is closed.
268 */
269 static
270 long lttng_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
271 {
272 switch (cmd) {
273 case LTTNG_KERNEL_OLD_SESSION:
274 case LTTNG_KERNEL_SESSION:
275 return lttng_abi_create_session();
276 case LTTNG_KERNEL_OLD_TRACER_VERSION:
277 {
278 struct lttng_kernel_tracer_version v;
279 struct lttng_kernel_old_tracer_version oldv;
280 struct lttng_kernel_old_tracer_version *uversion =
281 (struct lttng_kernel_old_tracer_version __user *) arg;
282
283 lttng_abi_tracer_version(&v);
284 oldv.major = v.major;
285 oldv.minor = v.minor;
286 oldv.patchlevel = v.patchlevel;
287
288 if (copy_to_user(uversion, &oldv, sizeof(oldv)))
289 return -EFAULT;
290 return 0;
291 }
292 case LTTNG_KERNEL_TRACER_VERSION:
293 {
294 struct lttng_kernel_tracer_version version;
295 struct lttng_kernel_tracer_version *uversion =
296 (struct lttng_kernel_tracer_version __user *) arg;
297
298 lttng_abi_tracer_version(&version);
299
300 if (copy_to_user(uversion, &version, sizeof(version)))
301 return -EFAULT;
302 return 0;
303 }
304 case LTTNG_KERNEL_TRACER_ABI_VERSION:
305 {
306 struct lttng_kernel_tracer_abi_version version;
307 struct lttng_kernel_tracer_abi_version *uversion =
308 (struct lttng_kernel_tracer_abi_version __user *) arg;
309
310 lttng_abi_tracer_abi_version(&version);
311
312 if (copy_to_user(uversion, &version, sizeof(version)))
313 return -EFAULT;
314 return 0;
315 }
316 case LTTNG_KERNEL_OLD_TRACEPOINT_LIST:
317 case LTTNG_KERNEL_TRACEPOINT_LIST:
318 return lttng_abi_tracepoint_list();
319 case LTTNG_KERNEL_SYSCALL_LIST:
320 return lttng_abi_syscall_list();
321 case LTTNG_KERNEL_OLD_WAIT_QUIESCENT:
322 case LTTNG_KERNEL_WAIT_QUIESCENT:
323 synchronize_trace();
324 return 0;
325 case LTTNG_KERNEL_OLD_CALIBRATE:
326 {
327 struct lttng_kernel_old_calibrate __user *ucalibrate =
328 (struct lttng_kernel_old_calibrate __user *) arg;
329 struct lttng_kernel_old_calibrate old_calibrate;
330 struct lttng_kernel_calibrate calibrate;
331 int ret;
332
333 if (copy_from_user(&old_calibrate, ucalibrate, sizeof(old_calibrate)))
334 return -EFAULT;
335 calibrate.type = old_calibrate.type;
336 ret = lttng_calibrate(&calibrate);
337 if (copy_to_user(ucalibrate, &old_calibrate, sizeof(old_calibrate)))
338 return -EFAULT;
339 return ret;
340 }
341 case LTTNG_KERNEL_CALIBRATE:
342 {
343 struct lttng_kernel_calibrate __user *ucalibrate =
344 (struct lttng_kernel_calibrate __user *) arg;
345 struct lttng_kernel_calibrate calibrate;
346 int ret;
347
348 if (copy_from_user(&calibrate, ucalibrate, sizeof(calibrate)))
349 return -EFAULT;
350 ret = lttng_calibrate(&calibrate);
351 if (copy_to_user(ucalibrate, &calibrate, sizeof(calibrate)))
352 return -EFAULT;
353 return ret;
354 }
355 default:
356 return -ENOIOCTLCMD;
357 }
358 }
359
360 static const struct file_operations lttng_fops = {
361 .owner = THIS_MODULE,
362 .unlocked_ioctl = lttng_ioctl,
363 #ifdef CONFIG_COMPAT
364 .compat_ioctl = lttng_ioctl,
365 #endif
366 };
367
368 static
369 int lttng_abi_create_channel(struct file *session_file,
370 struct lttng_kernel_channel *chan_param,
371 enum channel_type channel_type)
372 {
373 struct lttng_session *session = session_file->private_data;
374 const struct file_operations *fops = NULL;
375 const char *transport_name;
376 struct lttng_channel *chan;
377 struct file *chan_file;
378 int chan_fd;
379 int ret = 0;
380
381 chan_fd = lttng_get_unused_fd();
382 if (chan_fd < 0) {
383 ret = chan_fd;
384 goto fd_error;
385 }
386 switch (channel_type) {
387 case PER_CPU_CHANNEL:
388 fops = &lttng_channel_fops;
389 break;
390 case METADATA_CHANNEL:
391 fops = &lttng_metadata_fops;
392 break;
393 }
394
395 chan_file = anon_inode_getfile("[lttng_channel]",
396 fops,
397 NULL, O_RDWR);
398 if (IS_ERR(chan_file)) {
399 ret = PTR_ERR(chan_file);
400 goto file_error;
401 }
402 switch (channel_type) {
403 case PER_CPU_CHANNEL:
404 if (chan_param->output == LTTNG_KERNEL_SPLICE) {
405 transport_name = chan_param->overwrite ?
406 "relay-overwrite" : "relay-discard";
407 } else if (chan_param->output == LTTNG_KERNEL_MMAP) {
408 transport_name = chan_param->overwrite ?
409 "relay-overwrite-mmap" : "relay-discard-mmap";
410 } else {
411 return -EINVAL;
412 }
413 break;
414 case METADATA_CHANNEL:
415 if (chan_param->output == LTTNG_KERNEL_SPLICE)
416 transport_name = "relay-metadata";
417 else if (chan_param->output == LTTNG_KERNEL_MMAP)
418 transport_name = "relay-metadata-mmap";
419 else
420 return -EINVAL;
421 break;
422 default:
423 transport_name = "<unknown>";
424 break;
425 }
426 /*
427 * We tolerate no failure path after channel creation. It will stay
428 * invariant for the rest of the session.
429 */
430 chan = lttng_channel_create(session, transport_name, NULL,
431 chan_param->subbuf_size,
432 chan_param->num_subbuf,
433 chan_param->switch_timer_interval,
434 chan_param->read_timer_interval,
435 channel_type);
436 if (!chan) {
437 ret = -EINVAL;
438 goto chan_error;
439 }
440 chan->file = chan_file;
441 chan_file->private_data = chan;
442 fd_install(chan_fd, chan_file);
443 atomic_long_inc(&session_file->f_count);
444
445 return chan_fd;
446
447 chan_error:
448 fput(chan_file);
449 file_error:
450 put_unused_fd(chan_fd);
451 fd_error:
452 return ret;
453 }
454
455 /**
456 * lttng_session_ioctl - lttng session fd ioctl
457 *
458 * @file: the file
459 * @cmd: the command
460 * @arg: command arg
461 *
462 * This ioctl implements lttng commands:
463 * LTTNG_KERNEL_CHANNEL
464 * Returns a LTTng channel file descriptor
465 * LTTNG_KERNEL_ENABLE
466 * Enables tracing for a session (weak enable)
467 * LTTNG_KERNEL_DISABLE
468 * Disables tracing for a session (strong disable)
469 * LTTNG_KERNEL_METADATA
470 * Returns a LTTng metadata file descriptor
471 * LTTNG_KERNEL_SESSION_TRACK_PID
472 * Add PID to session tracker
473 * LTTNG_KERNEL_SESSION_UNTRACK_PID
474 * Remove PID from session tracker
475 *
476 * The returned channel will be deleted when its file descriptor is closed.
477 */
478 static
479 long lttng_session_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
480 {
481 struct lttng_session *session = file->private_data;
482
483 switch (cmd) {
484 case LTTNG_KERNEL_OLD_CHANNEL:
485 {
486 struct lttng_kernel_channel chan_param;
487 struct lttng_kernel_old_channel old_chan_param;
488
489 if (copy_from_user(&old_chan_param,
490 (struct lttng_kernel_old_channel __user *) arg,
491 sizeof(struct lttng_kernel_old_channel)))
492 return -EFAULT;
493 chan_param.overwrite = old_chan_param.overwrite;
494 chan_param.subbuf_size = old_chan_param.subbuf_size;
495 chan_param.num_subbuf = old_chan_param.num_subbuf;
496 chan_param.switch_timer_interval = old_chan_param.switch_timer_interval;
497 chan_param.read_timer_interval = old_chan_param.read_timer_interval;
498 chan_param.output = old_chan_param.output;
499
500 return lttng_abi_create_channel(file, &chan_param,
501 PER_CPU_CHANNEL);
502 }
503 case LTTNG_KERNEL_CHANNEL:
504 {
505 struct lttng_kernel_channel chan_param;
506
507 if (copy_from_user(&chan_param,
508 (struct lttng_kernel_channel __user *) arg,
509 sizeof(struct lttng_kernel_channel)))
510 return -EFAULT;
511 return lttng_abi_create_channel(file, &chan_param,
512 PER_CPU_CHANNEL);
513 }
514 case LTTNG_KERNEL_OLD_SESSION_START:
515 case LTTNG_KERNEL_OLD_ENABLE:
516 case LTTNG_KERNEL_SESSION_START:
517 case LTTNG_KERNEL_ENABLE:
518 return lttng_session_enable(session);
519 case LTTNG_KERNEL_OLD_SESSION_STOP:
520 case LTTNG_KERNEL_OLD_DISABLE:
521 case LTTNG_KERNEL_SESSION_STOP:
522 case LTTNG_KERNEL_DISABLE:
523 return lttng_session_disable(session);
524 case LTTNG_KERNEL_OLD_METADATA:
525 {
526 struct lttng_kernel_channel chan_param;
527 struct lttng_kernel_old_channel old_chan_param;
528
529 if (copy_from_user(&old_chan_param,
530 (struct lttng_kernel_old_channel __user *) arg,
531 sizeof(struct lttng_kernel_old_channel)))
532 return -EFAULT;
533 chan_param.overwrite = old_chan_param.overwrite;
534 chan_param.subbuf_size = old_chan_param.subbuf_size;
535 chan_param.num_subbuf = old_chan_param.num_subbuf;
536 chan_param.switch_timer_interval = old_chan_param.switch_timer_interval;
537 chan_param.read_timer_interval = old_chan_param.read_timer_interval;
538 chan_param.output = old_chan_param.output;
539
540 return lttng_abi_create_channel(file, &chan_param,
541 METADATA_CHANNEL);
542 }
543 case LTTNG_KERNEL_METADATA:
544 {
545 struct lttng_kernel_channel chan_param;
546
547 if (copy_from_user(&chan_param,
548 (struct lttng_kernel_channel __user *) arg,
549 sizeof(struct lttng_kernel_channel)))
550 return -EFAULT;
551 return lttng_abi_create_channel(file, &chan_param,
552 METADATA_CHANNEL);
553 }
554 case LTTNG_KERNEL_SESSION_TRACK_PID:
555 return lttng_session_track_pid(session, (int) arg);
556 case LTTNG_KERNEL_SESSION_UNTRACK_PID:
557 return lttng_session_untrack_pid(session, (int) arg);
558 case LTTNG_KERNEL_SESSION_LIST_TRACKER_PIDS:
559 return lttng_session_list_tracker_pids(session);
560 default:
561 return -ENOIOCTLCMD;
562 }
563 }
564
565 /*
566 * Called when the last file reference is dropped.
567 *
568 * Big fat note: channels and events are invariant for the whole session after
569 * their creation. So this session destruction also destroys all channel and
570 * event structures specific to this session (they are not destroyed when their
571 * individual file is released).
572 */
573 static
574 int lttng_session_release(struct inode *inode, struct file *file)
575 {
576 struct lttng_session *session = file->private_data;
577
578 if (session)
579 lttng_session_destroy(session);
580 return 0;
581 }
582
583 static const struct file_operations lttng_session_fops = {
584 .owner = THIS_MODULE,
585 .release = lttng_session_release,
586 .unlocked_ioctl = lttng_session_ioctl,
587 #ifdef CONFIG_COMPAT
588 .compat_ioctl = lttng_session_ioctl,
589 #endif
590 };
591
592 /**
593 * lttng_metadata_ring_buffer_poll - LTTng ring buffer poll file operation
594 * @filp: the file
595 * @wait: poll table
596 *
597 * Handles the poll operations for the metadata channels.
598 */
599 static
600 unsigned int lttng_metadata_ring_buffer_poll(struct file *filp,
601 poll_table *wait)
602 {
603 struct lttng_metadata_stream *stream = filp->private_data;
604 struct lib_ring_buffer *buf = stream->priv;
605 int finalized;
606 unsigned int mask = 0;
607
608 if (filp->f_mode & FMODE_READ) {
609 poll_wait_set_exclusive(wait);
610 poll_wait(filp, &stream->read_wait, wait);
611
612 finalized = stream->finalized;
613
614 /*
615 * lib_ring_buffer_is_finalized() contains a smp_rmb()
616 * ordering finalized load before offsets loads.
617 */
618 WARN_ON(atomic_long_read(&buf->active_readers) != 1);
619
620 if (finalized)
621 mask |= POLLHUP;
622
623 if (stream->metadata_cache->metadata_written >
624 stream->metadata_out)
625 mask |= POLLIN;
626 }
627
628 return mask;
629 }
630
631 static
632 void lttng_metadata_ring_buffer_ioctl_put_next_subbuf(struct file *filp,
633 unsigned int cmd, unsigned long arg)
634 {
635 struct lttng_metadata_stream *stream = filp->private_data;
636
637 stream->metadata_out = stream->metadata_in;
638 }
639
640 static
641 long lttng_metadata_ring_buffer_ioctl(struct file *filp,
642 unsigned int cmd, unsigned long arg)
643 {
644 int ret;
645 struct lttng_metadata_stream *stream = filp->private_data;
646 struct lib_ring_buffer *buf = stream->priv;
647
648 switch (cmd) {
649 case RING_BUFFER_GET_NEXT_SUBBUF:
650 {
651 struct lttng_metadata_stream *stream = filp->private_data;
652 struct lib_ring_buffer *buf = stream->priv;
653 struct channel *chan = buf->backend.chan;
654
655 ret = lttng_metadata_output_channel(stream, chan);
656 if (ret > 0) {
657 lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE);
658 ret = 0;
659 } else if (ret < 0)
660 goto err;
661 break;
662 }
663 case RING_BUFFER_GET_SUBBUF:
664 {
665 /*
666 * Random access is not allowed for metadata channel.
667 */
668 return -ENOSYS;
669 }
670 case RING_BUFFER_FLUSH:
671 {
672 struct lttng_metadata_stream *stream = filp->private_data;
673 struct lib_ring_buffer *buf = stream->priv;
674 struct channel *chan = buf->backend.chan;
675
676 /*
677 * Before doing the actual ring buffer flush, write up to one
678 * packet of metadata in the ring buffer.
679 */
680 ret = lttng_metadata_output_channel(stream, chan);
681 if (ret < 0)
682 goto err;
683 break;
684 }
685 default:
686 break;
687 }
688 /* PUT_SUBBUF is the one from lib ring buffer, unmodified. */
689
690 /* Performing lib ring buffer ioctl after our own. */
691 ret = lib_ring_buffer_ioctl(filp, cmd, arg, buf);
692 if (ret < 0)
693 goto err;
694
695 switch (cmd) {
696 case RING_BUFFER_PUT_NEXT_SUBBUF:
697 {
698 lttng_metadata_ring_buffer_ioctl_put_next_subbuf(filp,
699 cmd, arg);
700 break;
701 }
702 default:
703 break;
704 }
705 err:
706 return ret;
707 }
708
709 #ifdef CONFIG_COMPAT
710 static
711 long lttng_metadata_ring_buffer_compat_ioctl(struct file *filp,
712 unsigned int cmd, unsigned long arg)
713 {
714 int ret;
715 struct lttng_metadata_stream *stream = filp->private_data;
716 struct lib_ring_buffer *buf = stream->priv;
717
718 switch (cmd) {
719 case RING_BUFFER_GET_NEXT_SUBBUF:
720 {
721 struct lttng_metadata_stream *stream = filp->private_data;
722 struct lib_ring_buffer *buf = stream->priv;
723 struct channel *chan = buf->backend.chan;
724
725 ret = lttng_metadata_output_channel(stream, chan);
726 if (ret > 0) {
727 lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE);
728 ret = 0;
729 } else if (ret < 0)
730 goto err;
731 break;
732 }
733 case RING_BUFFER_GET_SUBBUF:
734 {
735 /*
736 * Random access is not allowed for metadata channel.
737 */
738 return -ENOSYS;
739 }
740 default:
741 break;
742 }
743 /* PUT_SUBBUF is the one from lib ring buffer, unmodified. */
744
745 /* Performing lib ring buffer ioctl after our own. */
746 ret = lib_ring_buffer_compat_ioctl(filp, cmd, arg, buf);
747 if (ret < 0)
748 goto err;
749
750 switch (cmd) {
751 case RING_BUFFER_PUT_NEXT_SUBBUF:
752 {
753 lttng_metadata_ring_buffer_ioctl_put_next_subbuf(filp,
754 cmd, arg);
755 break;
756 }
757 default:
758 break;
759 }
760 err:
761 return ret;
762 }
763 #endif
764
765 /*
766 * This is not used by anonymous file descriptors. This code is left
767 * there if we ever want to implement an inode with open() operation.
768 */
769 static
770 int lttng_metadata_ring_buffer_open(struct inode *inode, struct file *file)
771 {
772 struct lttng_metadata_stream *stream = inode->i_private;
773 struct lib_ring_buffer *buf = stream->priv;
774
775 file->private_data = buf;
776 /*
777 * Since life-time of metadata cache differs from that of
778 * session, we need to keep our own reference on the transport.
779 */
780 if (!try_module_get(stream->transport->owner)) {
781 printk(KERN_WARNING "LTT : Can't lock transport module.\n");
782 return -EBUSY;
783 }
784 return lib_ring_buffer_open(inode, file, buf);
785 }
786
787 static
788 int lttng_metadata_ring_buffer_release(struct inode *inode, struct file *file)
789 {
790 struct lttng_metadata_stream *stream = file->private_data;
791 struct lib_ring_buffer *buf = stream->priv;
792
793 kref_put(&stream->metadata_cache->refcount, metadata_cache_destroy);
794 module_put(stream->transport->owner);
795 return lib_ring_buffer_release(inode, file, buf);
796 }
797
798 static
799 ssize_t lttng_metadata_ring_buffer_splice_read(struct file *in, loff_t *ppos,
800 struct pipe_inode_info *pipe, size_t len,
801 unsigned int flags)
802 {
803 struct lttng_metadata_stream *stream = in->private_data;
804 struct lib_ring_buffer *buf = stream->priv;
805
806 return lib_ring_buffer_splice_read(in, ppos, pipe, len,
807 flags, buf);
808 }
809
810 static
811 int lttng_metadata_ring_buffer_mmap(struct file *filp,
812 struct vm_area_struct *vma)
813 {
814 struct lttng_metadata_stream *stream = filp->private_data;
815 struct lib_ring_buffer *buf = stream->priv;
816
817 return lib_ring_buffer_mmap(filp, vma, buf);
818 }
819
820 static
821 const struct file_operations lttng_metadata_ring_buffer_file_operations = {
822 .owner = THIS_MODULE,
823 .open = lttng_metadata_ring_buffer_open,
824 .release = lttng_metadata_ring_buffer_release,
825 .poll = lttng_metadata_ring_buffer_poll,
826 .splice_read = lttng_metadata_ring_buffer_splice_read,
827 .mmap = lttng_metadata_ring_buffer_mmap,
828 .unlocked_ioctl = lttng_metadata_ring_buffer_ioctl,
829 .llseek = vfs_lib_ring_buffer_no_llseek,
830 #ifdef CONFIG_COMPAT
831 .compat_ioctl = lttng_metadata_ring_buffer_compat_ioctl,
832 #endif
833 };
834
835 static
836 int lttng_abi_create_stream_fd(struct file *channel_file, void *stream_priv,
837 const struct file_operations *fops)
838 {
839 int stream_fd, ret;
840 struct file *stream_file;
841
842 stream_fd = lttng_get_unused_fd();
843 if (stream_fd < 0) {
844 ret = stream_fd;
845 goto fd_error;
846 }
847 stream_file = anon_inode_getfile("[lttng_stream]", fops,
848 stream_priv, O_RDWR);
849 if (IS_ERR(stream_file)) {
850 ret = PTR_ERR(stream_file);
851 goto file_error;
852 }
853 /*
854 * OPEN_FMODE, called within anon_inode_getfile/alloc_file, don't honor
855 * FMODE_LSEEK, FMODE_PREAD nor FMODE_PWRITE. We need to read from this
856 * file descriptor, so we set FMODE_PREAD here.
857 */
858 stream_file->f_mode |= FMODE_PREAD;
859 fd_install(stream_fd, stream_file);
860 /*
861 * The stream holds a reference to the channel within the generic ring
862 * buffer library, so no need to hold a refcount on the channel and
863 * session files here.
864 */
865 return stream_fd;
866
867 file_error:
868 put_unused_fd(stream_fd);
869 fd_error:
870 return ret;
871 }
872
873 static
874 int lttng_abi_open_stream(struct file *channel_file)
875 {
876 struct lttng_channel *channel = channel_file->private_data;
877 struct lib_ring_buffer *buf;
878 int ret;
879 void *stream_priv;
880
881 buf = channel->ops->buffer_read_open(channel->chan);
882 if (!buf)
883 return -ENOENT;
884
885 stream_priv = buf;
886 ret = lttng_abi_create_stream_fd(channel_file, stream_priv,
887 &lttng_stream_ring_buffer_file_operations);
888 if (ret < 0)
889 goto fd_error;
890
891 return ret;
892
893 fd_error:
894 channel->ops->buffer_read_close(buf);
895 return ret;
896 }
897
898 static
899 int lttng_abi_open_metadata_stream(struct file *channel_file)
900 {
901 struct lttng_channel *channel = channel_file->private_data;
902 struct lttng_session *session = channel->session;
903 struct lib_ring_buffer *buf;
904 int ret;
905 struct lttng_metadata_stream *metadata_stream;
906 void *stream_priv;
907
908 buf = channel->ops->buffer_read_open(channel->chan);
909 if (!buf)
910 return -ENOENT;
911
912 metadata_stream = kzalloc(sizeof(struct lttng_metadata_stream),
913 GFP_KERNEL);
914 if (!metadata_stream) {
915 ret = -ENOMEM;
916 goto nomem;
917 }
918 metadata_stream->metadata_cache = session->metadata_cache;
919 init_waitqueue_head(&metadata_stream->read_wait);
920 metadata_stream->priv = buf;
921 stream_priv = metadata_stream;
922 metadata_stream->transport = channel->transport;
923 mutex_init(&metadata_stream->lock);
924
925 /*
926 * Since life-time of metadata cache differs from that of
927 * session, we need to keep our own reference on the transport.
928 */
929 if (!try_module_get(metadata_stream->transport->owner)) {
930 printk(KERN_WARNING "LTT : Can't lock transport module.\n");
931 ret = -EINVAL;
932 goto notransport;
933 }
934
935 ret = lttng_abi_create_stream_fd(channel_file, stream_priv,
936 &lttng_metadata_ring_buffer_file_operations);
937 if (ret < 0)
938 goto fd_error;
939
940 kref_get(&session->metadata_cache->refcount);
941 list_add(&metadata_stream->list,
942 &session->metadata_cache->metadata_stream);
943 return ret;
944
945 fd_error:
946 module_put(metadata_stream->transport->owner);
947 notransport:
948 kfree(metadata_stream);
949 nomem:
950 channel->ops->buffer_read_close(buf);
951 return ret;
952 }
953
954 static
955 int lttng_abi_create_event(struct file *channel_file,
956 struct lttng_kernel_event *event_param)
957 {
958 struct lttng_channel *channel = channel_file->private_data;
959 int event_fd, ret;
960 struct file *event_file;
961 void *priv;
962
963 event_param->name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
964 switch (event_param->instrumentation) {
965 case LTTNG_KERNEL_KRETPROBE:
966 event_param->u.kretprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
967 break;
968 case LTTNG_KERNEL_KPROBE:
969 event_param->u.kprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
970 break;
971 case LTTNG_KERNEL_FUNCTION:
972 event_param->u.ftrace.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
973 break;
974 default:
975 break;
976 }
977 event_fd = lttng_get_unused_fd();
978 if (event_fd < 0) {
979 ret = event_fd;
980 goto fd_error;
981 }
982 event_file = anon_inode_getfile("[lttng_event]",
983 &lttng_event_fops,
984 NULL, O_RDWR);
985 if (IS_ERR(event_file)) {
986 ret = PTR_ERR(event_file);
987 goto file_error;
988 }
989 if (event_param->instrumentation == LTTNG_KERNEL_TRACEPOINT
990 || event_param->instrumentation == LTTNG_KERNEL_SYSCALL) {
991 struct lttng_enabler *enabler;
992
993 if (event_param->name[strlen(event_param->name) - 1] == '*') {
994 enabler = lttng_enabler_create(LTTNG_ENABLER_WILDCARD,
995 event_param, channel);
996 } else {
997 enabler = lttng_enabler_create(LTTNG_ENABLER_NAME,
998 event_param, channel);
999 }
1000 priv = enabler;
1001 } else {
1002 struct lttng_event *event;
1003
1004 /*
1005 * We tolerate no failure path after event creation. It
1006 * will stay invariant for the rest of the session.
1007 */
1008 event = lttng_event_create(channel, event_param,
1009 NULL, NULL,
1010 event_param->instrumentation);
1011 WARN_ON_ONCE(!event);
1012 if (IS_ERR(event)) {
1013 ret = PTR_ERR(event);
1014 goto event_error;
1015 }
1016 priv = event;
1017 }
1018 event_file->private_data = priv;
1019 fd_install(event_fd, event_file);
1020 /* The event holds a reference on the channel */
1021 atomic_long_inc(&channel_file->f_count);
1022 return event_fd;
1023
1024 event_error:
1025 fput(event_file);
1026 file_error:
1027 put_unused_fd(event_fd);
1028 fd_error:
1029 return ret;
1030 }
1031
1032 /**
1033 * lttng_channel_ioctl - lttng syscall through ioctl
1034 *
1035 * @file: the file
1036 * @cmd: the command
1037 * @arg: command arg
1038 *
1039 * This ioctl implements lttng commands:
1040 * LTTNG_KERNEL_STREAM
1041 * Returns an event stream file descriptor or failure.
1042 * (typically, one event stream records events from one CPU)
1043 * LTTNG_KERNEL_EVENT
1044 * Returns an event file descriptor or failure.
1045 * LTTNG_KERNEL_CONTEXT
1046 * Prepend a context field to each event in the channel
1047 * LTTNG_KERNEL_ENABLE
1048 * Enable recording for events in this channel (weak enable)
1049 * LTTNG_KERNEL_DISABLE
1050 * Disable recording for events in this channel (strong disable)
1051 *
1052 * Channel and event file descriptors also hold a reference on the session.
1053 */
1054 static
1055 long lttng_channel_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1056 {
1057 struct lttng_channel *channel = file->private_data;
1058
1059 switch (cmd) {
1060 case LTTNG_KERNEL_OLD_STREAM:
1061 case LTTNG_KERNEL_STREAM:
1062 return lttng_abi_open_stream(file);
1063 case LTTNG_KERNEL_OLD_EVENT:
1064 {
1065 struct lttng_kernel_event *uevent_param;
1066 struct lttng_kernel_old_event *old_uevent_param;
1067 int ret;
1068
1069 uevent_param = kmalloc(sizeof(struct lttng_kernel_event),
1070 GFP_KERNEL);
1071 if (!uevent_param) {
1072 ret = -ENOMEM;
1073 goto old_event_end;
1074 }
1075 old_uevent_param = kmalloc(
1076 sizeof(struct lttng_kernel_old_event),
1077 GFP_KERNEL);
1078 if (!old_uevent_param) {
1079 ret = -ENOMEM;
1080 goto old_event_error_free_param;
1081 }
1082 if (copy_from_user(old_uevent_param,
1083 (struct lttng_kernel_old_event __user *) arg,
1084 sizeof(struct lttng_kernel_old_event))) {
1085 ret = -EFAULT;
1086 goto old_event_error_free_old_param;
1087 }
1088
1089 memcpy(uevent_param->name, old_uevent_param->name,
1090 sizeof(uevent_param->name));
1091 uevent_param->instrumentation =
1092 old_uevent_param->instrumentation;
1093
1094 switch (old_uevent_param->instrumentation) {
1095 case LTTNG_KERNEL_KPROBE:
1096 uevent_param->u.kprobe.addr =
1097 old_uevent_param->u.kprobe.addr;
1098 uevent_param->u.kprobe.offset =
1099 old_uevent_param->u.kprobe.offset;
1100 memcpy(uevent_param->u.kprobe.symbol_name,
1101 old_uevent_param->u.kprobe.symbol_name,
1102 sizeof(uevent_param->u.kprobe.symbol_name));
1103 break;
1104 case LTTNG_KERNEL_KRETPROBE:
1105 uevent_param->u.kretprobe.addr =
1106 old_uevent_param->u.kretprobe.addr;
1107 uevent_param->u.kretprobe.offset =
1108 old_uevent_param->u.kretprobe.offset;
1109 memcpy(uevent_param->u.kretprobe.symbol_name,
1110 old_uevent_param->u.kretprobe.symbol_name,
1111 sizeof(uevent_param->u.kretprobe.symbol_name));
1112 break;
1113 case LTTNG_KERNEL_FUNCTION:
1114 memcpy(uevent_param->u.ftrace.symbol_name,
1115 old_uevent_param->u.ftrace.symbol_name,
1116 sizeof(uevent_param->u.ftrace.symbol_name));
1117 break;
1118 default:
1119 break;
1120 }
1121 ret = lttng_abi_create_event(file, uevent_param);
1122
1123 old_event_error_free_old_param:
1124 kfree(old_uevent_param);
1125 old_event_error_free_param:
1126 kfree(uevent_param);
1127 old_event_end:
1128 return ret;
1129 }
1130 case LTTNG_KERNEL_EVENT:
1131 {
1132 struct lttng_kernel_event uevent_param;
1133
1134 if (copy_from_user(&uevent_param,
1135 (struct lttng_kernel_event __user *) arg,
1136 sizeof(uevent_param)))
1137 return -EFAULT;
1138 return lttng_abi_create_event(file, &uevent_param);
1139 }
1140 case LTTNG_KERNEL_OLD_CONTEXT:
1141 {
1142 struct lttng_kernel_context *ucontext_param;
1143 struct lttng_kernel_old_context *old_ucontext_param;
1144 int ret;
1145
1146 ucontext_param = kmalloc(sizeof(struct lttng_kernel_context),
1147 GFP_KERNEL);
1148 if (!ucontext_param) {
1149 ret = -ENOMEM;
1150 goto old_ctx_end;
1151 }
1152 old_ucontext_param = kmalloc(sizeof(struct lttng_kernel_old_context),
1153 GFP_KERNEL);
1154 if (!old_ucontext_param) {
1155 ret = -ENOMEM;
1156 goto old_ctx_error_free_param;
1157 }
1158
1159 if (copy_from_user(old_ucontext_param,
1160 (struct lttng_kernel_old_context __user *) arg,
1161 sizeof(struct lttng_kernel_old_context))) {
1162 ret = -EFAULT;
1163 goto old_ctx_error_free_old_param;
1164 }
1165 ucontext_param->ctx = old_ucontext_param->ctx;
1166 memcpy(ucontext_param->padding, old_ucontext_param->padding,
1167 sizeof(ucontext_param->padding));
1168 /* only type that uses the union */
1169 if (old_ucontext_param->ctx == LTTNG_KERNEL_CONTEXT_PERF_COUNTER) {
1170 ucontext_param->u.perf_counter.type =
1171 old_ucontext_param->u.perf_counter.type;
1172 ucontext_param->u.perf_counter.config =
1173 old_ucontext_param->u.perf_counter.config;
1174 memcpy(ucontext_param->u.perf_counter.name,
1175 old_ucontext_param->u.perf_counter.name,
1176 sizeof(ucontext_param->u.perf_counter.name));
1177 }
1178
1179 ret = lttng_abi_add_context(file,
1180 ucontext_param,
1181 &channel->ctx, channel->session);
1182
1183 old_ctx_error_free_old_param:
1184 kfree(old_ucontext_param);
1185 old_ctx_error_free_param:
1186 kfree(ucontext_param);
1187 old_ctx_end:
1188 return ret;
1189 }
1190 case LTTNG_KERNEL_CONTEXT:
1191 {
1192 struct lttng_kernel_context ucontext_param;
1193
1194 if (copy_from_user(&ucontext_param,
1195 (struct lttng_kernel_context __user *) arg,
1196 sizeof(ucontext_param)))
1197 return -EFAULT;
1198 return lttng_abi_add_context(file,
1199 &ucontext_param,
1200 &channel->ctx, channel->session);
1201 }
1202 case LTTNG_KERNEL_OLD_ENABLE:
1203 case LTTNG_KERNEL_ENABLE:
1204 return lttng_channel_enable(channel);
1205 case LTTNG_KERNEL_OLD_DISABLE:
1206 case LTTNG_KERNEL_DISABLE:
1207 return lttng_channel_disable(channel);
1208 case LTTNG_KERNEL_SYSCALL_MASK:
1209 return lttng_channel_syscall_mask(channel,
1210 (struct lttng_kernel_syscall_mask __user *) arg);
1211 default:
1212 return -ENOIOCTLCMD;
1213 }
1214
1215 }
1216
1217 /**
1218 * lttng_metadata_ioctl - lttng syscall through ioctl
1219 *
1220 * @file: the file
1221 * @cmd: the command
1222 * @arg: command arg
1223 *
1224 * This ioctl implements lttng commands:
1225 * LTTNG_KERNEL_STREAM
1226 * Returns an event stream file descriptor or failure.
1227 *
1228 * Channel and event file descriptors also hold a reference on the session.
1229 */
1230 static
1231 long lttng_metadata_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1232 {
1233 switch (cmd) {
1234 case LTTNG_KERNEL_OLD_STREAM:
1235 case LTTNG_KERNEL_STREAM:
1236 return lttng_abi_open_metadata_stream(file);
1237 default:
1238 return -ENOIOCTLCMD;
1239 }
1240 }
1241
1242 /**
1243 * lttng_channel_poll - lttng stream addition/removal monitoring
1244 *
1245 * @file: the file
1246 * @wait: poll table
1247 */
1248 unsigned int lttng_channel_poll(struct file *file, poll_table *wait)
1249 {
1250 struct lttng_channel *channel = file->private_data;
1251 unsigned int mask = 0;
1252
1253 if (file->f_mode & FMODE_READ) {
1254 poll_wait_set_exclusive(wait);
1255 poll_wait(file, channel->ops->get_hp_wait_queue(channel->chan),
1256 wait);
1257
1258 if (channel->ops->is_disabled(channel->chan))
1259 return POLLERR;
1260 if (channel->ops->is_finalized(channel->chan))
1261 return POLLHUP;
1262 if (channel->ops->buffer_has_read_closed_stream(channel->chan))
1263 return POLLIN | POLLRDNORM;
1264 return 0;
1265 }
1266 return mask;
1267
1268 }
1269
1270 static
1271 int lttng_channel_release(struct inode *inode, struct file *file)
1272 {
1273 struct lttng_channel *channel = file->private_data;
1274
1275 if (channel)
1276 fput(channel->session->file);
1277 return 0;
1278 }
1279
1280 static
1281 int lttng_metadata_channel_release(struct inode *inode, struct file *file)
1282 {
1283 struct lttng_channel *channel = file->private_data;
1284
1285 if (channel) {
1286 lttng_metadata_channel_destroy(channel);
1287 fput(channel->session->file);
1288 }
1289
1290 return 0;
1291 }
1292
1293 static const struct file_operations lttng_channel_fops = {
1294 .owner = THIS_MODULE,
1295 .release = lttng_channel_release,
1296 .poll = lttng_channel_poll,
1297 .unlocked_ioctl = lttng_channel_ioctl,
1298 #ifdef CONFIG_COMPAT
1299 .compat_ioctl = lttng_channel_ioctl,
1300 #endif
1301 };
1302
1303 static const struct file_operations lttng_metadata_fops = {
1304 .owner = THIS_MODULE,
1305 .release = lttng_metadata_channel_release,
1306 .unlocked_ioctl = lttng_metadata_ioctl,
1307 #ifdef CONFIG_COMPAT
1308 .compat_ioctl = lttng_metadata_ioctl,
1309 #endif
1310 };
1311
1312 /**
1313 * lttng_event_ioctl - lttng syscall through ioctl
1314 *
1315 * @file: the file
1316 * @cmd: the command
1317 * @arg: command arg
1318 *
1319 * This ioctl implements lttng commands:
1320 * LTTNG_KERNEL_CONTEXT
1321 * Prepend a context field to each record of this event
1322 * LTTNG_KERNEL_ENABLE
1323 * Enable recording for this event (weak enable)
1324 * LTTNG_KERNEL_DISABLE
1325 * Disable recording for this event (strong disable)
1326 */
1327 static
1328 long lttng_event_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1329 {
1330 struct lttng_event *event;
1331 struct lttng_enabler *enabler;
1332 enum lttng_event_type *evtype = file->private_data;
1333
1334 switch (cmd) {
1335 case LTTNG_KERNEL_OLD_CONTEXT:
1336 {
1337 /* Not implemented */
1338 return -ENOSYS;
1339 }
1340 case LTTNG_KERNEL_CONTEXT:
1341 {
1342 /* Not implemented */
1343 return -ENOSYS;
1344 }
1345 case LTTNG_KERNEL_OLD_ENABLE:
1346 case LTTNG_KERNEL_ENABLE:
1347 switch (*evtype) {
1348 case LTTNG_TYPE_EVENT:
1349 event = file->private_data;
1350 return lttng_event_enable(event);
1351 case LTTNG_TYPE_ENABLER:
1352 enabler = file->private_data;
1353 return lttng_enabler_enable(enabler);
1354 default:
1355 WARN_ON_ONCE(1);
1356 return -ENOSYS;
1357 }
1358 case LTTNG_KERNEL_OLD_DISABLE:
1359 case LTTNG_KERNEL_DISABLE:
1360 switch (*evtype) {
1361 case LTTNG_TYPE_EVENT:
1362 event = file->private_data;
1363 return lttng_event_disable(event);
1364 case LTTNG_TYPE_ENABLER:
1365 enabler = file->private_data;
1366 return lttng_enabler_disable(enabler);
1367 default:
1368 WARN_ON_ONCE(1);
1369 return -ENOSYS;
1370 }
1371 case LTTNG_KERNEL_FILTER:
1372 switch (*evtype) {
1373 case LTTNG_TYPE_EVENT:
1374 return -EINVAL;
1375 case LTTNG_TYPE_ENABLER:
1376 {
1377 enabler = file->private_data;
1378 return lttng_enabler_attach_bytecode(enabler,
1379 (struct lttng_kernel_filter_bytecode __user *) arg);
1380 }
1381
1382 }
1383 default:
1384 return -ENOIOCTLCMD;
1385 }
1386 }
1387
1388 static
1389 int lttng_event_release(struct inode *inode, struct file *file)
1390 {
1391 struct lttng_event *event;
1392 struct lttng_enabler *enabler;
1393 enum lttng_event_type *evtype = file->private_data;
1394
1395 if (!evtype)
1396 return 0;
1397
1398 switch (*evtype) {
1399 case LTTNG_TYPE_EVENT:
1400 event = file->private_data;
1401 if (event)
1402 fput(event->chan->file);
1403 break;
1404 case LTTNG_TYPE_ENABLER:
1405 enabler = file->private_data;
1406 if (enabler)
1407 fput(enabler->chan->file);
1408 break;
1409 default:
1410 WARN_ON_ONCE(1);
1411 break;
1412 }
1413
1414 return 0;
1415 }
1416
1417 /* TODO: filter control ioctl */
1418 static const struct file_operations lttng_event_fops = {
1419 .owner = THIS_MODULE,
1420 .release = lttng_event_release,
1421 .unlocked_ioctl = lttng_event_ioctl,
1422 #ifdef CONFIG_COMPAT
1423 .compat_ioctl = lttng_event_ioctl,
1424 #endif
1425 };
1426
1427 static int put_u64(uint64_t val, unsigned long arg)
1428 {
1429 return put_user(val, (uint64_t __user *) arg);
1430 }
1431
1432 static long lttng_stream_ring_buffer_ioctl(struct file *filp,
1433 unsigned int cmd, unsigned long arg)
1434 {
1435 struct lib_ring_buffer *buf = filp->private_data;
1436 struct channel *chan = buf->backend.chan;
1437 const struct lib_ring_buffer_config *config = &chan->backend.config;
1438 const struct lttng_channel_ops *ops = chan->backend.priv_ops;
1439 int ret;
1440
1441 if (atomic_read(&chan->record_disabled))
1442 return -EIO;
1443
1444 switch (cmd) {
1445 case LTTNG_RING_BUFFER_GET_TIMESTAMP_BEGIN:
1446 {
1447 uint64_t ts;
1448
1449 ret = ops->timestamp_begin(config, buf, &ts);
1450 if (ret < 0)
1451 goto error;
1452 return put_u64(ts, arg);
1453 }
1454 case LTTNG_RING_BUFFER_GET_TIMESTAMP_END:
1455 {
1456 uint64_t ts;
1457
1458 ret = ops->timestamp_end(config, buf, &ts);
1459 if (ret < 0)
1460 goto error;
1461 return put_u64(ts, arg);
1462 }
1463 case LTTNG_RING_BUFFER_GET_EVENTS_DISCARDED:
1464 {
1465 uint64_t ed;
1466
1467 ret = ops->events_discarded(config, buf, &ed);
1468 if (ret < 0)
1469 goto error;
1470 return put_u64(ed, arg);
1471 }
1472 case LTTNG_RING_BUFFER_GET_CONTENT_SIZE:
1473 {
1474 uint64_t cs;
1475
1476 ret = ops->content_size(config, buf, &cs);
1477 if (ret < 0)
1478 goto error;
1479 return put_u64(cs, arg);
1480 }
1481 case LTTNG_RING_BUFFER_GET_PACKET_SIZE:
1482 {
1483 uint64_t ps;
1484
1485 ret = ops->packet_size(config, buf, &ps);
1486 if (ret < 0)
1487 goto error;
1488 return put_u64(ps, arg);
1489 }
1490 case LTTNG_RING_BUFFER_GET_STREAM_ID:
1491 {
1492 uint64_t si;
1493
1494 ret = ops->stream_id(config, buf, &si);
1495 if (ret < 0)
1496 goto error;
1497 return put_u64(si, arg);
1498 }
1499 case LTTNG_RING_BUFFER_GET_CURRENT_TIMESTAMP:
1500 {
1501 uint64_t ts;
1502
1503 ret = ops->current_timestamp(config, buf, &ts);
1504 if (ret < 0)
1505 goto error;
1506 return put_u64(ts, arg);
1507 }
1508 default:
1509 return lib_ring_buffer_file_operations.unlocked_ioctl(filp,
1510 cmd, arg);
1511 }
1512
1513 error:
1514 return -ENOSYS;
1515 }
1516
1517 #ifdef CONFIG_COMPAT
1518 static long lttng_stream_ring_buffer_compat_ioctl(struct file *filp,
1519 unsigned int cmd, unsigned long arg)
1520 {
1521 struct lib_ring_buffer *buf = filp->private_data;
1522 struct channel *chan = buf->backend.chan;
1523 const struct lib_ring_buffer_config *config = &chan->backend.config;
1524 const struct lttng_channel_ops *ops = chan->backend.priv_ops;
1525 int ret;
1526
1527 if (atomic_read(&chan->record_disabled))
1528 return -EIO;
1529
1530 switch (cmd) {
1531 case LTTNG_RING_BUFFER_COMPAT_GET_TIMESTAMP_BEGIN:
1532 {
1533 uint64_t ts;
1534
1535 ret = ops->timestamp_begin(config, buf, &ts);
1536 if (ret < 0)
1537 goto error;
1538 return put_u64(ts, arg);
1539 }
1540 case LTTNG_RING_BUFFER_COMPAT_GET_TIMESTAMP_END:
1541 {
1542 uint64_t ts;
1543
1544 ret = ops->timestamp_end(config, buf, &ts);
1545 if (ret < 0)
1546 goto error;
1547 return put_u64(ts, arg);
1548 }
1549 case LTTNG_RING_BUFFER_COMPAT_GET_EVENTS_DISCARDED:
1550 {
1551 uint64_t ed;
1552
1553 ret = ops->events_discarded(config, buf, &ed);
1554 if (ret < 0)
1555 goto error;
1556 return put_u64(ed, arg);
1557 }
1558 case LTTNG_RING_BUFFER_COMPAT_GET_CONTENT_SIZE:
1559 {
1560 uint64_t cs;
1561
1562 ret = ops->content_size(config, buf, &cs);
1563 if (ret < 0)
1564 goto error;
1565 return put_u64(cs, arg);
1566 }
1567 case LTTNG_RING_BUFFER_COMPAT_GET_PACKET_SIZE:
1568 {
1569 uint64_t ps;
1570
1571 ret = ops->packet_size(config, buf, &ps);
1572 if (ret < 0)
1573 goto error;
1574 return put_u64(ps, arg);
1575 }
1576 case LTTNG_RING_BUFFER_COMPAT_GET_STREAM_ID:
1577 {
1578 uint64_t si;
1579
1580 ret = ops->stream_id(config, buf, &si);
1581 if (ret < 0)
1582 goto error;
1583 return put_u64(si, arg);
1584 }
1585 case LTTNG_RING_BUFFER_GET_CURRENT_TIMESTAMP:
1586 {
1587 uint64_t ts;
1588
1589 ret = ops->current_timestamp(config, buf, &ts);
1590 if (ret < 0)
1591 goto error;
1592 return put_u64(ts, arg);
1593 }
1594 default:
1595 return lib_ring_buffer_file_operations.compat_ioctl(filp,
1596 cmd, arg);
1597 }
1598
1599 error:
1600 return -ENOSYS;
1601 }
1602 #endif /* CONFIG_COMPAT */
1603
1604 static void lttng_stream_override_ring_buffer_fops(void)
1605 {
1606 lttng_stream_ring_buffer_file_operations.owner = THIS_MODULE;
1607 lttng_stream_ring_buffer_file_operations.open =
1608 lib_ring_buffer_file_operations.open;
1609 lttng_stream_ring_buffer_file_operations.release =
1610 lib_ring_buffer_file_operations.release;
1611 lttng_stream_ring_buffer_file_operations.poll =
1612 lib_ring_buffer_file_operations.poll;
1613 lttng_stream_ring_buffer_file_operations.splice_read =
1614 lib_ring_buffer_file_operations.splice_read;
1615 lttng_stream_ring_buffer_file_operations.mmap =
1616 lib_ring_buffer_file_operations.mmap;
1617 lttng_stream_ring_buffer_file_operations.unlocked_ioctl =
1618 lttng_stream_ring_buffer_ioctl;
1619 lttng_stream_ring_buffer_file_operations.llseek =
1620 lib_ring_buffer_file_operations.llseek;
1621 #ifdef CONFIG_COMPAT
1622 lttng_stream_ring_buffer_file_operations.compat_ioctl =
1623 lttng_stream_ring_buffer_compat_ioctl;
1624 #endif
1625 }
1626
1627 int __init lttng_abi_init(void)
1628 {
1629 int ret = 0;
1630
1631 wrapper_vmalloc_sync_all();
1632 lttng_proc_dentry = proc_create_data("lttng", S_IRUSR | S_IWUSR, NULL,
1633 &lttng_fops, NULL);
1634
1635 if (!lttng_proc_dentry) {
1636 printk(KERN_ERR "Error creating LTTng control file\n");
1637 ret = -ENOMEM;
1638 goto error;
1639 }
1640 lttng_stream_override_ring_buffer_fops();
1641
1642 error:
1643 return ret;
1644 }
1645
1646 /* No __exit annotation because used by init error path too. */
1647 void lttng_abi_exit(void)
1648 {
1649 if (lttng_proc_dentry)
1650 remove_proc_entry("lttng", NULL);
1651 }
This page took 0.091923 seconds and 5 git commands to generate.