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