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