1 /* SPDX-License-Identifier: (GPL-2.0-only or LGPL-2.1-only)
7 * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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.
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_mappings() */
34 #include <ringbuffer/vfs.h>
35 #include <ringbuffer/backend.h>
36 #include <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 <ringbuffer/frontend_types.h>
47 #include <ringbuffer/iterator.h>
50 * This is LTTng's own personal way to create a system call as an external
51 * module. We use ioctl() on /proc/lttng.
54 static struct proc_dir_entry
*lttng_proc_dentry
;
56 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(5,6,0))
57 static const struct proc_ops lttng_proc_ops
;
59 static const struct file_operations lttng_proc_ops
;
62 static const struct file_operations lttng_session_fops
;
63 static const struct file_operations lttng_event_notifier_group_fops
;
64 static const struct file_operations lttng_channel_fops
;
65 static const struct file_operations lttng_metadata_fops
;
66 static const struct file_operations lttng_event_fops
;
67 static struct file_operations lttng_stream_ring_buffer_file_operations
;
69 static int put_u64(uint64_t val
, unsigned long arg
);
70 static int put_u32(uint32_t val
, unsigned long arg
);
72 static int validate_zeroed_padding(char *p
, size_t len
)
76 for (i
= 0; i
< len
; i
++) {
84 * Teardown management: opened file descriptors keep a refcount on the module,
85 * so it can only exit when all file descriptors are closed.
89 int lttng_abi_create_session(void)
91 struct lttng_session
*session
;
92 struct file
*session_file
;
95 session
= lttng_session_create();
98 session_fd
= lttng_get_unused_fd();
103 session_file
= anon_inode_getfile("[lttng_session]",
106 if (IS_ERR(session_file
)) {
107 ret
= PTR_ERR(session_file
);
110 session
->file
= session_file
;
111 fd_install(session_fd
, session_file
);
115 put_unused_fd(session_fd
);
117 lttng_session_destroy(session
);
121 void event_notifier_send_notification_work_wakeup(struct irq_work
*entry
)
123 struct lttng_event_notifier_group
*event_notifier_group
=
124 container_of(entry
, struct lttng_event_notifier_group
,
126 wake_up_interruptible(&event_notifier_group
->read_wait
);
130 int lttng_abi_create_event_notifier_group(void)
132 struct lttng_event_notifier_group
*event_notifier_group
;
133 struct file
*event_notifier_group_file
;
134 int event_notifier_group_fd
, ret
;
136 event_notifier_group
= lttng_event_notifier_group_create();
137 if (!event_notifier_group
)
140 event_notifier_group_fd
= lttng_get_unused_fd();
141 if (event_notifier_group_fd
< 0) {
142 ret
= event_notifier_group_fd
;
145 event_notifier_group_file
= anon_inode_getfile("[lttng_event_notifier_group]",
146 <tng_event_notifier_group_fops
,
147 event_notifier_group
, O_RDWR
);
148 if (IS_ERR(event_notifier_group_file
)) {
149 ret
= PTR_ERR(event_notifier_group_file
);
153 event_notifier_group
->file
= event_notifier_group_file
;
154 init_waitqueue_head(&event_notifier_group
->read_wait
);
155 init_irq_work(&event_notifier_group
->wakeup_pending
,
156 event_notifier_send_notification_work_wakeup
);
157 fd_install(event_notifier_group_fd
, event_notifier_group_file
);
158 return event_notifier_group_fd
;
161 put_unused_fd(event_notifier_group_fd
);
163 lttng_event_notifier_group_destroy(event_notifier_group
);
168 int lttng_abi_tracepoint_list(void)
170 struct file
*tracepoint_list_file
;
173 file_fd
= lttng_get_unused_fd();
179 tracepoint_list_file
= anon_inode_getfile("[lttng_tracepoint_list]",
180 <tng_tracepoint_list_fops
,
182 if (IS_ERR(tracepoint_list_file
)) {
183 ret
= PTR_ERR(tracepoint_list_file
);
186 ret
= lttng_tracepoint_list_fops
.open(NULL
, tracepoint_list_file
);
189 fd_install(file_fd
, tracepoint_list_file
);
193 fput(tracepoint_list_file
);
195 put_unused_fd(file_fd
);
200 #ifndef CONFIG_HAVE_SYSCALL_TRACEPOINTS
202 int lttng_abi_syscall_list(void)
208 int lttng_abi_syscall_list(void)
210 struct file
*syscall_list_file
;
213 file_fd
= lttng_get_unused_fd();
219 syscall_list_file
= anon_inode_getfile("[lttng_syscall_list]",
220 <tng_syscall_list_fops
,
222 if (IS_ERR(syscall_list_file
)) {
223 ret
= PTR_ERR(syscall_list_file
);
226 ret
= lttng_syscall_list_fops
.open(NULL
, syscall_list_file
);
229 fd_install(file_fd
, syscall_list_file
);
233 fput(syscall_list_file
);
235 put_unused_fd(file_fd
);
242 void lttng_abi_tracer_version(struct lttng_kernel_tracer_version
*v
)
244 v
->major
= LTTNG_MODULES_MAJOR_VERSION
;
245 v
->minor
= LTTNG_MODULES_MINOR_VERSION
;
246 v
->patchlevel
= LTTNG_MODULES_PATCHLEVEL_VERSION
;
250 void lttng_abi_tracer_abi_version(struct lttng_kernel_tracer_abi_version
*v
)
252 v
->major
= LTTNG_MODULES_ABI_MAJOR_VERSION
;
253 v
->minor
= LTTNG_MODULES_ABI_MINOR_VERSION
;
257 long lttng_abi_add_context(struct file
*file
,
258 struct lttng_kernel_context
*context_param
,
259 struct lttng_ctx
**ctx
, struct lttng_session
*session
)
262 if (session
->been_active
)
265 switch (context_param
->ctx
) {
266 case LTTNG_KERNEL_CONTEXT_PID
:
267 return lttng_add_pid_to_ctx(ctx
);
268 case LTTNG_KERNEL_CONTEXT_PRIO
:
269 return lttng_add_prio_to_ctx(ctx
);
270 case LTTNG_KERNEL_CONTEXT_NICE
:
271 return lttng_add_nice_to_ctx(ctx
);
272 case LTTNG_KERNEL_CONTEXT_VPID
:
273 return lttng_add_vpid_to_ctx(ctx
);
274 case LTTNG_KERNEL_CONTEXT_TID
:
275 return lttng_add_tid_to_ctx(ctx
);
276 case LTTNG_KERNEL_CONTEXT_VTID
:
277 return lttng_add_vtid_to_ctx(ctx
);
278 case LTTNG_KERNEL_CONTEXT_PPID
:
279 return lttng_add_ppid_to_ctx(ctx
);
280 case LTTNG_KERNEL_CONTEXT_VPPID
:
281 return lttng_add_vppid_to_ctx(ctx
);
282 case LTTNG_KERNEL_CONTEXT_PERF_COUNTER
:
283 context_param
->u
.perf_counter
.name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
284 return lttng_add_perf_counter_to_ctx(context_param
->u
.perf_counter
.type
,
285 context_param
->u
.perf_counter
.config
,
286 context_param
->u
.perf_counter
.name
,
288 case LTTNG_KERNEL_CONTEXT_PROCNAME
:
289 return lttng_add_procname_to_ctx(ctx
);
290 case LTTNG_KERNEL_CONTEXT_HOSTNAME
:
291 return lttng_add_hostname_to_ctx(ctx
);
292 case LTTNG_KERNEL_CONTEXT_CPU_ID
:
293 return lttng_add_cpu_id_to_ctx(ctx
);
294 case LTTNG_KERNEL_CONTEXT_INTERRUPTIBLE
:
295 return lttng_add_interruptible_to_ctx(ctx
);
296 case LTTNG_KERNEL_CONTEXT_NEED_RESCHEDULE
:
297 return lttng_add_need_reschedule_to_ctx(ctx
);
298 case LTTNG_KERNEL_CONTEXT_PREEMPTIBLE
:
299 return lttng_add_preemptible_to_ctx(ctx
);
300 case LTTNG_KERNEL_CONTEXT_MIGRATABLE
:
301 return lttng_add_migratable_to_ctx(ctx
);
302 case LTTNG_KERNEL_CONTEXT_CALLSTACK_KERNEL
:
303 case LTTNG_KERNEL_CONTEXT_CALLSTACK_USER
:
304 return lttng_add_callstack_to_ctx(ctx
, context_param
->ctx
);
305 case LTTNG_KERNEL_CONTEXT_CGROUP_NS
:
306 return lttng_add_cgroup_ns_to_ctx(ctx
);
307 case LTTNG_KERNEL_CONTEXT_IPC_NS
:
308 return lttng_add_ipc_ns_to_ctx(ctx
);
309 case LTTNG_KERNEL_CONTEXT_MNT_NS
:
310 return lttng_add_mnt_ns_to_ctx(ctx
);
311 case LTTNG_KERNEL_CONTEXT_NET_NS
:
312 return lttng_add_net_ns_to_ctx(ctx
);
313 case LTTNG_KERNEL_CONTEXT_PID_NS
:
314 return lttng_add_pid_ns_to_ctx(ctx
);
315 case LTTNG_KERNEL_CONTEXT_USER_NS
:
316 return lttng_add_user_ns_to_ctx(ctx
);
317 case LTTNG_KERNEL_CONTEXT_UTS_NS
:
318 return lttng_add_uts_ns_to_ctx(ctx
);
319 case LTTNG_KERNEL_CONTEXT_UID
:
320 return lttng_add_uid_to_ctx(ctx
);
321 case LTTNG_KERNEL_CONTEXT_EUID
:
322 return lttng_add_euid_to_ctx(ctx
);
323 case LTTNG_KERNEL_CONTEXT_SUID
:
324 return lttng_add_suid_to_ctx(ctx
);
325 case LTTNG_KERNEL_CONTEXT_GID
:
326 return lttng_add_gid_to_ctx(ctx
);
327 case LTTNG_KERNEL_CONTEXT_EGID
:
328 return lttng_add_egid_to_ctx(ctx
);
329 case LTTNG_KERNEL_CONTEXT_SGID
:
330 return lttng_add_sgid_to_ctx(ctx
);
331 case LTTNG_KERNEL_CONTEXT_VUID
:
332 return lttng_add_vuid_to_ctx(ctx
);
333 case LTTNG_KERNEL_CONTEXT_VEUID
:
334 return lttng_add_veuid_to_ctx(ctx
);
335 case LTTNG_KERNEL_CONTEXT_VSUID
:
336 return lttng_add_vsuid_to_ctx(ctx
);
337 case LTTNG_KERNEL_CONTEXT_VGID
:
338 return lttng_add_vgid_to_ctx(ctx
);
339 case LTTNG_KERNEL_CONTEXT_VEGID
:
340 return lttng_add_vegid_to_ctx(ctx
);
341 case LTTNG_KERNEL_CONTEXT_VSGID
:
342 return lttng_add_vsgid_to_ctx(ctx
);
343 case LTTNG_KERNEL_CONTEXT_TIME_NS
:
344 return lttng_add_time_ns_to_ctx(ctx
);
351 * lttng_ioctl - lttng syscall through ioctl
357 * This ioctl implements lttng commands:
358 * LTTNG_KERNEL_SESSION
359 * Returns a LTTng trace session file descriptor
360 * LTTNG_KERNEL_TRACER_VERSION
361 * Returns the LTTng kernel tracer version
362 * LTTNG_KERNEL_TRACEPOINT_LIST
363 * Returns a file descriptor listing available tracepoints
364 * LTTNG_KERNEL_WAIT_QUIESCENT
365 * Returns after all previously running probes have completed
366 * LTTNG_KERNEL_TRACER_ABI_VERSION
367 * Returns the LTTng kernel tracer ABI version
368 * LTTNG_KERNEL_EVENT_NOTIFIER_GROUP_CREATE
369 * Returns a LTTng event notifier group file descriptor
371 * The returned session will be deleted when its file descriptor is closed.
374 long lttng_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
377 case LTTNG_KERNEL_OLD_SESSION
:
378 case LTTNG_KERNEL_SESSION
:
379 return lttng_abi_create_session();
380 case LTTNG_KERNEL_EVENT_NOTIFIER_GROUP_CREATE
:
381 return lttng_abi_create_event_notifier_group();
382 case LTTNG_KERNEL_OLD_TRACER_VERSION
:
384 struct lttng_kernel_tracer_version v
;
385 struct lttng_kernel_old_tracer_version oldv
;
386 struct lttng_kernel_old_tracer_version
*uversion
=
387 (struct lttng_kernel_old_tracer_version __user
*) arg
;
389 lttng_abi_tracer_version(&v
);
390 oldv
.major
= v
.major
;
391 oldv
.minor
= v
.minor
;
392 oldv
.patchlevel
= v
.patchlevel
;
394 if (copy_to_user(uversion
, &oldv
, sizeof(oldv
)))
398 case LTTNG_KERNEL_TRACER_VERSION
:
400 struct lttng_kernel_tracer_version version
;
401 struct lttng_kernel_tracer_version
*uversion
=
402 (struct lttng_kernel_tracer_version __user
*) arg
;
404 lttng_abi_tracer_version(&version
);
406 if (copy_to_user(uversion
, &version
, sizeof(version
)))
410 case LTTNG_KERNEL_TRACER_ABI_VERSION
:
412 struct lttng_kernel_tracer_abi_version version
;
413 struct lttng_kernel_tracer_abi_version
*uversion
=
414 (struct lttng_kernel_tracer_abi_version __user
*) arg
;
416 lttng_abi_tracer_abi_version(&version
);
418 if (copy_to_user(uversion
, &version
, sizeof(version
)))
422 case LTTNG_KERNEL_OLD_TRACEPOINT_LIST
:
423 case LTTNG_KERNEL_TRACEPOINT_LIST
:
424 return lttng_abi_tracepoint_list();
425 case LTTNG_KERNEL_SYSCALL_LIST
:
426 return lttng_abi_syscall_list();
427 case LTTNG_KERNEL_OLD_WAIT_QUIESCENT
:
428 case LTTNG_KERNEL_WAIT_QUIESCENT
:
431 case LTTNG_KERNEL_OLD_CALIBRATE
:
433 struct lttng_kernel_old_calibrate __user
*ucalibrate
=
434 (struct lttng_kernel_old_calibrate __user
*) arg
;
435 struct lttng_kernel_old_calibrate old_calibrate
;
436 struct lttng_kernel_calibrate calibrate
;
439 if (copy_from_user(&old_calibrate
, ucalibrate
, sizeof(old_calibrate
)))
441 calibrate
.type
= old_calibrate
.type
;
442 ret
= lttng_calibrate(&calibrate
);
443 if (copy_to_user(ucalibrate
, &old_calibrate
, sizeof(old_calibrate
)))
447 case LTTNG_KERNEL_CALIBRATE
:
449 struct lttng_kernel_calibrate __user
*ucalibrate
=
450 (struct lttng_kernel_calibrate __user
*) arg
;
451 struct lttng_kernel_calibrate calibrate
;
454 if (copy_from_user(&calibrate
, ucalibrate
, sizeof(calibrate
)))
456 ret
= lttng_calibrate(&calibrate
);
457 if (copy_to_user(ucalibrate
, &calibrate
, sizeof(calibrate
)))
466 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(5,6,0))
467 static const struct proc_ops lttng_proc_ops
= {
468 .proc_ioctl
= lttng_ioctl
,
470 .proc_compat_ioctl
= lttng_ioctl
,
471 #endif /* CONFIG_COMPAT */
474 static const struct file_operations lttng_proc_ops
= {
475 .owner
= THIS_MODULE
,
476 .unlocked_ioctl
= lttng_ioctl
,
478 .compat_ioctl
= lttng_ioctl
,
479 #endif /* CONFIG_COMPAT */
484 int lttng_abi_create_channel(struct file
*session_file
,
485 struct lttng_kernel_channel
*chan_param
,
486 enum channel_type channel_type
)
488 struct lttng_session
*session
= session_file
->private_data
;
489 const struct file_operations
*fops
= NULL
;
490 const char *transport_name
;
491 struct lttng_channel
*chan
;
492 struct file
*chan_file
;
496 chan_fd
= lttng_get_unused_fd();
501 switch (channel_type
) {
502 case PER_CPU_CHANNEL
:
503 fops
= <tng_channel_fops
;
505 case METADATA_CHANNEL
:
506 fops
= <tng_metadata_fops
;
510 chan_file
= anon_inode_getfile("[lttng_channel]",
513 if (IS_ERR(chan_file
)) {
514 ret
= PTR_ERR(chan_file
);
517 switch (channel_type
) {
518 case PER_CPU_CHANNEL
:
519 if (chan_param
->output
== LTTNG_KERNEL_SPLICE
) {
520 transport_name
= chan_param
->overwrite
?
521 "relay-overwrite" : "relay-discard";
522 } else if (chan_param
->output
== LTTNG_KERNEL_MMAP
) {
523 transport_name
= chan_param
->overwrite
?
524 "relay-overwrite-mmap" : "relay-discard-mmap";
529 case METADATA_CHANNEL
:
530 if (chan_param
->output
== LTTNG_KERNEL_SPLICE
)
531 transport_name
= "relay-metadata";
532 else if (chan_param
->output
== LTTNG_KERNEL_MMAP
)
533 transport_name
= "relay-metadata-mmap";
538 transport_name
= "<unknown>";
541 if (!atomic_long_add_unless(&session_file
->f_count
, 1, LONG_MAX
)) {
546 * We tolerate no failure path after channel creation. It will stay
547 * invariant for the rest of the session.
549 chan
= lttng_channel_create(session
, transport_name
, NULL
,
550 chan_param
->subbuf_size
,
551 chan_param
->num_subbuf
,
552 chan_param
->switch_timer_interval
,
553 chan_param
->read_timer_interval
,
559 chan
->file
= chan_file
;
560 chan_file
->private_data
= chan
;
561 fd_install(chan_fd
, chan_file
);
566 atomic_long_dec(&session_file
->f_count
);
570 put_unused_fd(chan_fd
);
576 int lttng_abi_session_set_name(struct lttng_session
*session
,
577 struct lttng_kernel_session_name
*name
)
581 len
= strnlen(name
->name
, LTTNG_KERNEL_SESSION_NAME_LEN
);
583 if (len
== LTTNG_KERNEL_SESSION_NAME_LEN
) {
584 /* Name is too long/malformed */
588 strcpy(session
->name
, name
->name
);
593 int lttng_abi_session_set_creation_time(struct lttng_session
*session
,
594 struct lttng_kernel_session_creation_time
*time
)
598 len
= strnlen(time
->iso8601
, LTTNG_KERNEL_SESSION_CREATION_TIME_ISO8601_LEN
);
600 if (len
== LTTNG_KERNEL_SESSION_CREATION_TIME_ISO8601_LEN
) {
601 /* Time is too long/malformed */
605 strcpy(session
->creation_time
, time
->iso8601
);
610 int lttng_counter_release(struct inode
*inode
, struct file
*file
)
612 struct lttng_counter
*counter
= file
->private_data
;
616 * Do not destroy the counter itself. Wait of the owner
617 * (event_notifier group) to be destroyed.
619 fput(counter
->owner
);
626 long lttng_counter_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
628 struct lttng_counter
*counter
= file
->private_data
;
629 size_t indexes
[LTTNG_KERNEL_COUNTER_DIMENSION_MAX
] = { 0 };
633 case LTTNG_KERNEL_COUNTER_READ
:
635 struct lttng_kernel_counter_read local_counter_read
;
636 struct lttng_kernel_counter_read __user
*ucounter_read
=
637 (struct lttng_kernel_counter_read __user
*) arg
;
638 bool overflow
, underflow
;
643 if (copy_from_user(&local_counter_read
, ucounter_read
,
644 sizeof(local_counter_read
)))
646 if (validate_zeroed_padding(local_counter_read
.padding
,
647 sizeof(local_counter_read
.padding
)))
650 /* Cast all indexes into size_t. */
651 for (i
= 0; i
< local_counter_read
.index
.number_dimensions
; i
++)
652 indexes
[i
] = (size_t) local_counter_read
.index
.dimension_indexes
[i
];
653 cpu
= local_counter_read
.cpu
;
655 ret
= lttng_kernel_counter_read(counter
, indexes
, cpu
, &value
,
656 &overflow
, &underflow
);
659 local_counter_read
.value
.value
= value
;
660 local_counter_read
.value
.overflow
= overflow
;
661 local_counter_read
.value
.underflow
= underflow
;
663 if (copy_to_user(&ucounter_read
->value
, &local_counter_read
.value
,
664 sizeof(local_counter_read
.value
)))
669 case LTTNG_KERNEL_COUNTER_AGGREGATE
:
671 struct lttng_kernel_counter_aggregate local_counter_aggregate
;
672 struct lttng_kernel_counter_aggregate __user
*ucounter_aggregate
=
673 (struct lttng_kernel_counter_aggregate __user
*) arg
;
674 bool overflow
, underflow
;
678 if (copy_from_user(&local_counter_aggregate
, ucounter_aggregate
,
679 sizeof(local_counter_aggregate
)))
681 if (validate_zeroed_padding(local_counter_aggregate
.padding
,
682 sizeof(local_counter_aggregate
.padding
)))
685 /* Cast all indexes into size_t. */
686 for (i
= 0; i
< local_counter_aggregate
.index
.number_dimensions
; i
++)
687 indexes
[i
] = (size_t) local_counter_aggregate
.index
.dimension_indexes
[i
];
689 ret
= lttng_kernel_counter_aggregate(counter
, indexes
, &value
,
690 &overflow
, &underflow
);
693 local_counter_aggregate
.value
.value
= value
;
694 local_counter_aggregate
.value
.overflow
= overflow
;
695 local_counter_aggregate
.value
.underflow
= underflow
;
697 if (copy_to_user(&ucounter_aggregate
->value
, &local_counter_aggregate
.value
,
698 sizeof(local_counter_aggregate
.value
)))
703 case LTTNG_KERNEL_COUNTER_CLEAR
:
705 struct lttng_kernel_counter_clear local_counter_clear
;
706 struct lttng_kernel_counter_clear __user
*ucounter_clear
=
707 (struct lttng_kernel_counter_clear __user
*) arg
;
709 if (copy_from_user(&local_counter_clear
, ucounter_clear
,
710 sizeof(local_counter_clear
)))
712 if (validate_zeroed_padding(local_counter_clear
.padding
,
713 sizeof(local_counter_clear
.padding
)))
716 /* Cast all indexes into size_t. */
717 for (i
= 0; i
< local_counter_clear
.index
.number_dimensions
; i
++)
718 indexes
[i
] = (size_t) local_counter_clear
.index
.dimension_indexes
[i
];
720 return lttng_kernel_counter_clear(counter
, indexes
);
728 static const struct file_operations lttng_counter_fops
= {
729 .owner
= THIS_MODULE
,
730 .release
= lttng_counter_release
,
731 .unlocked_ioctl
= lttng_counter_ioctl
,
733 .compat_ioctl
= lttng_counter_ioctl
,
739 enum tracker_type
get_tracker_type(struct lttng_kernel_tracker_args
*tracker
)
741 switch (tracker
->type
) {
742 case LTTNG_KERNEL_TRACKER_PID
:
744 case LTTNG_KERNEL_TRACKER_VPID
:
746 case LTTNG_KERNEL_TRACKER_UID
:
748 case LTTNG_KERNEL_TRACKER_VUID
:
750 case LTTNG_KERNEL_TRACKER_GID
:
752 case LTTNG_KERNEL_TRACKER_VGID
:
755 return TRACKER_UNKNOWN
;
760 * lttng_session_ioctl - lttng session fd ioctl
766 * This ioctl implements lttng commands:
767 * LTTNG_KERNEL_CHANNEL
768 * Returns a LTTng channel file descriptor
769 * LTTNG_KERNEL_ENABLE
770 * Enables tracing for a session (weak enable)
771 * LTTNG_KERNEL_DISABLE
772 * Disables tracing for a session (strong disable)
773 * LTTNG_KERNEL_METADATA
774 * Returns a LTTng metadata file descriptor
775 * LTTNG_KERNEL_SESSION_TRACK_PID
776 * Add PID to session PID tracker
777 * LTTNG_KERNEL_SESSION_UNTRACK_PID
778 * Remove PID from session PID tracker
779 * LTTNG_KERNEL_SESSION_TRACK_ID
781 * LTTNG_KERNEL_SESSION_UNTRACK_ID
782 * Remove ID from tracker
784 * The returned channel will be deleted when its file descriptor is closed.
787 long lttng_session_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
789 struct lttng_session
*session
= file
->private_data
;
790 struct lttng_kernel_channel chan_param
;
791 struct lttng_kernel_old_channel old_chan_param
;
794 case LTTNG_KERNEL_OLD_CHANNEL
:
796 if (copy_from_user(&old_chan_param
,
797 (struct lttng_kernel_old_channel __user
*) arg
,
798 sizeof(struct lttng_kernel_old_channel
)))
800 chan_param
.overwrite
= old_chan_param
.overwrite
;
801 chan_param
.subbuf_size
= old_chan_param
.subbuf_size
;
802 chan_param
.num_subbuf
= old_chan_param
.num_subbuf
;
803 chan_param
.switch_timer_interval
= old_chan_param
.switch_timer_interval
;
804 chan_param
.read_timer_interval
= old_chan_param
.read_timer_interval
;
805 chan_param
.output
= old_chan_param
.output
;
807 return lttng_abi_create_channel(file
, &chan_param
,
810 case LTTNG_KERNEL_CHANNEL
:
812 if (copy_from_user(&chan_param
,
813 (struct lttng_kernel_channel __user
*) arg
,
814 sizeof(struct lttng_kernel_channel
)))
816 return lttng_abi_create_channel(file
, &chan_param
,
819 case LTTNG_KERNEL_OLD_SESSION_START
:
820 case LTTNG_KERNEL_OLD_ENABLE
:
821 case LTTNG_KERNEL_SESSION_START
:
822 case LTTNG_KERNEL_ENABLE
:
823 return lttng_session_enable(session
);
824 case LTTNG_KERNEL_OLD_SESSION_STOP
:
825 case LTTNG_KERNEL_OLD_DISABLE
:
826 case LTTNG_KERNEL_SESSION_STOP
:
827 case LTTNG_KERNEL_DISABLE
:
828 return lttng_session_disable(session
);
829 case LTTNG_KERNEL_OLD_METADATA
:
831 if (copy_from_user(&old_chan_param
,
832 (struct lttng_kernel_old_channel __user
*) arg
,
833 sizeof(struct lttng_kernel_old_channel
)))
835 chan_param
.overwrite
= old_chan_param
.overwrite
;
836 chan_param
.subbuf_size
= old_chan_param
.subbuf_size
;
837 chan_param
.num_subbuf
= old_chan_param
.num_subbuf
;
838 chan_param
.switch_timer_interval
= old_chan_param
.switch_timer_interval
;
839 chan_param
.read_timer_interval
= old_chan_param
.read_timer_interval
;
840 chan_param
.output
= old_chan_param
.output
;
842 return lttng_abi_create_channel(file
, &chan_param
,
845 case LTTNG_KERNEL_METADATA
:
847 if (copy_from_user(&chan_param
,
848 (struct lttng_kernel_channel __user
*) arg
,
849 sizeof(struct lttng_kernel_channel
)))
851 return lttng_abi_create_channel(file
, &chan_param
,
854 case LTTNG_KERNEL_SESSION_TRACK_PID
:
855 return lttng_session_track_id(session
, TRACKER_PID
, (int) arg
);
856 case LTTNG_KERNEL_SESSION_UNTRACK_PID
:
857 return lttng_session_untrack_id(session
, TRACKER_PID
, (int) arg
);
858 case LTTNG_KERNEL_SESSION_TRACK_ID
:
860 struct lttng_kernel_tracker_args tracker
;
861 enum tracker_type tracker_type
;
863 if (copy_from_user(&tracker
,
864 (struct lttng_kernel_tracker_args __user
*) arg
,
865 sizeof(struct lttng_kernel_tracker_args
)))
867 tracker_type
= get_tracker_type(&tracker
);
868 if (tracker_type
== TRACKER_UNKNOWN
)
870 return lttng_session_track_id(session
, tracker_type
, tracker
.id
);
872 case LTTNG_KERNEL_SESSION_UNTRACK_ID
:
874 struct lttng_kernel_tracker_args tracker
;
875 enum tracker_type tracker_type
;
877 if (copy_from_user(&tracker
,
878 (struct lttng_kernel_tracker_args __user
*) arg
,
879 sizeof(struct lttng_kernel_tracker_args
)))
881 tracker_type
= get_tracker_type(&tracker
);
882 if (tracker_type
== TRACKER_UNKNOWN
)
884 return lttng_session_untrack_id(session
, tracker_type
,
887 case LTTNG_KERNEL_SESSION_LIST_TRACKER_PIDS
:
888 return lttng_session_list_tracker_ids(session
, TRACKER_PID
);
889 case LTTNG_KERNEL_SESSION_LIST_TRACKER_IDS
:
891 struct lttng_kernel_tracker_args tracker
;
892 enum tracker_type tracker_type
;
894 if (copy_from_user(&tracker
,
895 (struct lttng_kernel_tracker_args __user
*) arg
,
896 sizeof(struct lttng_kernel_tracker_args
)))
898 tracker_type
= get_tracker_type(&tracker
);
899 if (tracker_type
== TRACKER_UNKNOWN
)
901 return lttng_session_list_tracker_ids(session
, tracker_type
);
903 case LTTNG_KERNEL_SESSION_METADATA_REGEN
:
904 return lttng_session_metadata_regenerate(session
);
905 case LTTNG_KERNEL_SESSION_STATEDUMP
:
906 return lttng_session_statedump(session
);
907 case LTTNG_KERNEL_SESSION_SET_NAME
:
909 struct lttng_kernel_session_name name
;
911 if (copy_from_user(&name
,
912 (struct lttng_kernel_session_name __user
*) arg
,
913 sizeof(struct lttng_kernel_session_name
)))
915 return lttng_abi_session_set_name(session
, &name
);
917 case LTTNG_KERNEL_SESSION_SET_CREATION_TIME
:
919 struct lttng_kernel_session_creation_time time
;
921 if (copy_from_user(&time
,
922 (struct lttng_kernel_session_creation_time __user
*) arg
,
923 sizeof(struct lttng_kernel_session_creation_time
)))
925 return lttng_abi_session_set_creation_time(session
, &time
);
933 * Called when the last file reference is dropped.
935 * Big fat note: channels and events are invariant for the whole session after
936 * their creation. So this session destruction also destroys all channel and
937 * event structures specific to this session (they are not destroyed when their
938 * individual file is released).
941 int lttng_session_release(struct inode
*inode
, struct file
*file
)
943 struct lttng_session
*session
= file
->private_data
;
946 lttng_session_destroy(session
);
950 static const struct file_operations lttng_session_fops
= {
951 .owner
= THIS_MODULE
,
952 .release
= lttng_session_release
,
953 .unlocked_ioctl
= lttng_session_ioctl
,
955 .compat_ioctl
= lttng_session_ioctl
,
960 * When encountering empty buffer, flush current sub-buffer if non-empty
961 * and retry (if new data available to read after flush).
964 ssize_t
lttng_event_notifier_group_notif_read(struct file
*filp
, char __user
*user_buf
,
965 size_t count
, loff_t
*ppos
)
967 struct lttng_event_notifier_group
*event_notifier_group
= filp
->private_data
;
968 struct channel
*chan
= event_notifier_group
->chan
;
969 struct lib_ring_buffer
*buf
= event_notifier_group
->buf
;
970 ssize_t read_count
= 0, len
;
974 if (!lttng_access_ok(VERIFY_WRITE
, user_buf
, count
))
977 /* Finish copy of previous record */
979 if (read_count
< count
) {
980 len
= chan
->iter
.len_left
;
986 while (read_count
< count
) {
987 size_t copy_len
, space_left
;
989 len
= lib_ring_buffer_get_next_record(chan
, buf
);
993 * Check if buffer is finalized (end of file).
995 if (len
== -ENODATA
) {
996 /* A 0 read_count will tell about end of file */
999 if (filp
->f_flags
& O_NONBLOCK
) {
1001 read_count
= -EAGAIN
;
1007 * No data available at the moment, return what
1014 * Wait for returned len to be >= 0 or -ENODATA.
1016 error
= wait_event_interruptible(
1017 event_notifier_group
->read_wait
,
1018 ((len
= lib_ring_buffer_get_next_record(
1019 chan
, buf
)), len
!= -EAGAIN
));
1020 CHAN_WARN_ON(chan
, len
== -EBUSY
);
1025 CHAN_WARN_ON(chan
, len
< 0 && len
!= -ENODATA
);
1029 read_offset
= buf
->iter
.read_offset
;
1031 space_left
= count
- read_count
;
1032 if (len
<= space_left
) {
1034 chan
->iter
.len_left
= 0;
1037 copy_len
= space_left
;
1038 chan
->iter
.len_left
= len
- copy_len
;
1039 *ppos
= read_offset
+ copy_len
;
1041 if (__lib_ring_buffer_copy_to_user(&buf
->backend
, read_offset
,
1042 &user_buf
[read_count
],
1045 * Leave the len_left and ppos values at their current
1046 * state, as we currently have a valid event to read.
1050 read_count
+= copy_len
;
1056 chan
->iter
.len_left
= 0;
1059 lib_ring_buffer_put_current_record(buf
);
1064 * If the ring buffer is non empty (even just a partial subbuffer), return that
1065 * there is data available. Perform a ring buffer flush if we encounter a
1066 * non-empty ring buffer which does not have any consumeable subbuffer available.
1069 unsigned int lttng_event_notifier_group_notif_poll(struct file
*filp
,
1072 unsigned int mask
= 0;
1073 struct lttng_event_notifier_group
*event_notifier_group
= filp
->private_data
;
1074 struct channel
*chan
= event_notifier_group
->chan
;
1075 struct lib_ring_buffer
*buf
= event_notifier_group
->buf
;
1076 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1077 int finalized
, disabled
;
1078 unsigned long consumed
, offset
;
1079 size_t subbuffer_header_size
= config
->cb
.subbuffer_header_size();
1081 if (filp
->f_mode
& FMODE_READ
) {
1082 poll_wait_set_exclusive(wait
);
1083 poll_wait(filp
, &event_notifier_group
->read_wait
, wait
);
1085 finalized
= lib_ring_buffer_is_finalized(config
, buf
);
1086 disabled
= lib_ring_buffer_channel_is_disabled(chan
);
1089 * lib_ring_buffer_is_finalized() contains a smp_rmb() ordering
1090 * finalized load before offsets loads.
1092 WARN_ON(atomic_long_read(&buf
->active_readers
) != 1);
1097 offset
= lib_ring_buffer_get_offset(config
, buf
);
1098 consumed
= lib_ring_buffer_get_consumed(config
, buf
);
1101 * If there is no buffer available to consume.
1103 if (subbuf_trunc(offset
, chan
) - subbuf_trunc(consumed
, chan
) == 0) {
1105 * If there is a non-empty subbuffer, flush and try again.
1107 if (subbuf_offset(offset
, chan
) > subbuffer_header_size
) {
1108 lib_ring_buffer_switch_remote(buf
);
1116 * The memory barriers
1117 * __wait_event()/wake_up_interruptible() take
1118 * care of "raw_spin_is_locked" memory ordering.
1120 if (raw_spin_is_locked(&buf
->raw_tick_nohz_spinlock
))
1126 if (subbuf_trunc(offset
, chan
) - subbuf_trunc(consumed
, chan
)
1127 >= chan
->backend
.buf_size
)
1128 return POLLPRI
| POLLRDBAND
;
1130 return POLLIN
| POLLRDNORM
;
1138 * lttng_event_notifier_group_notif_open - event_notifier ring buffer open file operation
1139 * @inode: opened inode
1140 * @file: opened file
1142 * Open implementation. Makes sure only one open instance of a buffer is
1143 * done at a given moment.
1145 static int lttng_event_notifier_group_notif_open(struct inode
*inode
, struct file
*file
)
1147 struct lttng_event_notifier_group
*event_notifier_group
= inode
->i_private
;
1148 struct lib_ring_buffer
*buf
= event_notifier_group
->buf
;
1150 file
->private_data
= event_notifier_group
;
1151 return lib_ring_buffer_open(inode
, file
, buf
);
1155 * lttng_event_notifier_group_notif_release - event_notifier ring buffer release file operation
1156 * @inode: opened inode
1157 * @file: opened file
1159 * Release implementation.
1161 static int lttng_event_notifier_group_notif_release(struct inode
*inode
, struct file
*file
)
1163 struct lttng_event_notifier_group
*event_notifier_group
= file
->private_data
;
1164 struct lib_ring_buffer
*buf
= event_notifier_group
->buf
;
1167 ret
= lib_ring_buffer_release(inode
, file
, buf
);
1170 fput(event_notifier_group
->file
);
1174 static const struct file_operations lttng_event_notifier_group_notif_fops
= {
1175 .owner
= THIS_MODULE
,
1176 .open
= lttng_event_notifier_group_notif_open
,
1177 .release
= lttng_event_notifier_group_notif_release
,
1178 .read
= lttng_event_notifier_group_notif_read
,
1179 .poll
= lttng_event_notifier_group_notif_poll
,
1183 * lttng_metadata_ring_buffer_poll - LTTng ring buffer poll file operation
1187 * Handles the poll operations for the metadata channels.
1190 unsigned int lttng_metadata_ring_buffer_poll(struct file
*filp
,
1193 struct lttng_metadata_stream
*stream
= filp
->private_data
;
1194 struct lib_ring_buffer
*buf
= stream
->priv
;
1196 unsigned int mask
= 0;
1198 if (filp
->f_mode
& FMODE_READ
) {
1199 poll_wait_set_exclusive(wait
);
1200 poll_wait(filp
, &stream
->read_wait
, wait
);
1202 finalized
= stream
->finalized
;
1205 * lib_ring_buffer_is_finalized() contains a smp_rmb()
1206 * ordering finalized load before offsets loads.
1208 WARN_ON(atomic_long_read(&buf
->active_readers
) != 1);
1213 mutex_lock(&stream
->metadata_cache
->lock
);
1214 if (stream
->metadata_cache
->metadata_written
>
1215 stream
->metadata_out
)
1217 mutex_unlock(&stream
->metadata_cache
->lock
);
1224 void lttng_metadata_ring_buffer_ioctl_put_next_subbuf(struct file
*filp
,
1225 unsigned int cmd
, unsigned long arg
)
1227 struct lttng_metadata_stream
*stream
= filp
->private_data
;
1229 stream
->metadata_out
= stream
->metadata_in
;
1233 * Reset the counter of how much metadata has been consumed to 0. That way,
1234 * the consumer receives the content of the metadata cache unchanged. This is
1235 * different from the metadata_regenerate where the offset from epoch is
1236 * resampled, here we want the exact same content as the last time the metadata
1237 * was generated. This command is only possible if all the metadata written
1238 * in the cache has been output to the metadata stream to avoid corrupting the
1241 * Return 0 on success, a negative value on error.
1244 int lttng_metadata_cache_dump(struct lttng_metadata_stream
*stream
)
1247 struct lttng_metadata_cache
*cache
= stream
->metadata_cache
;
1249 mutex_lock(&cache
->lock
);
1250 if (stream
->metadata_out
!= cache
->metadata_written
) {
1254 stream
->metadata_out
= 0;
1255 stream
->metadata_in
= 0;
1256 wake_up_interruptible(&stream
->read_wait
);
1260 mutex_unlock(&cache
->lock
);
1265 long lttng_metadata_ring_buffer_ioctl(struct file
*filp
,
1266 unsigned int cmd
, unsigned long arg
)
1269 struct lttng_metadata_stream
*stream
= filp
->private_data
;
1270 struct lib_ring_buffer
*buf
= stream
->priv
;
1271 unsigned int rb_cmd
;
1274 if (cmd
== RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK
)
1275 rb_cmd
= RING_BUFFER_GET_NEXT_SUBBUF
;
1280 case RING_BUFFER_GET_NEXT_SUBBUF
:
1282 struct lttng_metadata_stream
*stream
= filp
->private_data
;
1283 struct lib_ring_buffer
*buf
= stream
->priv
;
1284 struct channel
*chan
= buf
->backend
.chan
;
1286 ret
= lttng_metadata_output_channel(stream
, chan
, NULL
);
1288 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
1294 case RING_BUFFER_GET_SUBBUF
:
1297 * Random access is not allowed for metadata channel.
1301 case RING_BUFFER_FLUSH_EMPTY
: /* Fall-through. */
1302 case RING_BUFFER_FLUSH
:
1304 struct lttng_metadata_stream
*stream
= filp
->private_data
;
1305 struct lib_ring_buffer
*buf
= stream
->priv
;
1306 struct channel
*chan
= buf
->backend
.chan
;
1309 * Before doing the actual ring buffer flush, write up to one
1310 * packet of metadata in the ring buffer.
1312 ret
= lttng_metadata_output_channel(stream
, chan
, NULL
);
1317 case RING_BUFFER_GET_METADATA_VERSION
:
1319 struct lttng_metadata_stream
*stream
= filp
->private_data
;
1321 return put_u64(stream
->version
, arg
);
1323 case RING_BUFFER_METADATA_CACHE_DUMP
:
1325 struct lttng_metadata_stream
*stream
= filp
->private_data
;
1327 return lttng_metadata_cache_dump(stream
);
1329 case RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK
:
1331 struct lttng_metadata_stream
*stream
= filp
->private_data
;
1332 struct lib_ring_buffer
*buf
= stream
->priv
;
1333 struct channel
*chan
= buf
->backend
.chan
;
1335 ret
= lttng_metadata_output_channel(stream
, chan
, &coherent
);
1337 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
1339 } else if (ret
< 0) {
1347 /* PUT_SUBBUF is the one from lib ring buffer, unmodified. */
1349 /* Performing lib ring buffer ioctl after our own. */
1350 ret
= lib_ring_buffer_ioctl(filp
, rb_cmd
, arg
, buf
);
1355 case RING_BUFFER_PUT_NEXT_SUBBUF
:
1357 lttng_metadata_ring_buffer_ioctl_put_next_subbuf(filp
,
1361 case RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK
:
1363 return put_u32(coherent
, arg
);
1372 #ifdef CONFIG_COMPAT
1374 long lttng_metadata_ring_buffer_compat_ioctl(struct file
*filp
,
1375 unsigned int cmd
, unsigned long arg
)
1378 struct lttng_metadata_stream
*stream
= filp
->private_data
;
1379 struct lib_ring_buffer
*buf
= stream
->priv
;
1380 unsigned int rb_cmd
;
1383 if (cmd
== RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK
)
1384 rb_cmd
= RING_BUFFER_GET_NEXT_SUBBUF
;
1389 case RING_BUFFER_GET_NEXT_SUBBUF
:
1391 struct lttng_metadata_stream
*stream
= filp
->private_data
;
1392 struct lib_ring_buffer
*buf
= stream
->priv
;
1393 struct channel
*chan
= buf
->backend
.chan
;
1395 ret
= lttng_metadata_output_channel(stream
, chan
, NULL
);
1397 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
1403 case RING_BUFFER_GET_SUBBUF
:
1406 * Random access is not allowed for metadata channel.
1410 case RING_BUFFER_FLUSH_EMPTY
: /* Fall-through. */
1411 case RING_BUFFER_FLUSH
:
1413 struct lttng_metadata_stream
*stream
= filp
->private_data
;
1414 struct lib_ring_buffer
*buf
= stream
->priv
;
1415 struct channel
*chan
= buf
->backend
.chan
;
1418 * Before doing the actual ring buffer flush, write up to one
1419 * packet of metadata in the ring buffer.
1421 ret
= lttng_metadata_output_channel(stream
, chan
, NULL
);
1426 case RING_BUFFER_GET_METADATA_VERSION
:
1428 struct lttng_metadata_stream
*stream
= filp
->private_data
;
1430 return put_u64(stream
->version
, arg
);
1432 case RING_BUFFER_METADATA_CACHE_DUMP
:
1434 struct lttng_metadata_stream
*stream
= filp
->private_data
;
1436 return lttng_metadata_cache_dump(stream
);
1438 case RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK
:
1440 struct lttng_metadata_stream
*stream
= filp
->private_data
;
1441 struct lib_ring_buffer
*buf
= stream
->priv
;
1442 struct channel
*chan
= buf
->backend
.chan
;
1444 ret
= lttng_metadata_output_channel(stream
, chan
, &coherent
);
1446 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
1448 } else if (ret
< 0) {
1456 /* PUT_SUBBUF is the one from lib ring buffer, unmodified. */
1458 /* Performing lib ring buffer ioctl after our own. */
1459 ret
= lib_ring_buffer_compat_ioctl(filp
, rb_cmd
, arg
, buf
);
1464 case RING_BUFFER_PUT_NEXT_SUBBUF
:
1466 lttng_metadata_ring_buffer_ioctl_put_next_subbuf(filp
,
1470 case RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK
:
1472 return put_u32(coherent
, arg
);
1483 * This is not used by anonymous file descriptors. This code is left
1484 * there if we ever want to implement an inode with open() operation.
1487 int lttng_metadata_ring_buffer_open(struct inode
*inode
, struct file
*file
)
1489 struct lttng_metadata_stream
*stream
= inode
->i_private
;
1490 struct lib_ring_buffer
*buf
= stream
->priv
;
1492 file
->private_data
= buf
;
1494 * Since life-time of metadata cache differs from that of
1495 * session, we need to keep our own reference on the transport.
1497 if (!try_module_get(stream
->transport
->owner
)) {
1498 printk(KERN_WARNING
"LTTng: Can't lock transport module.\n");
1501 return lib_ring_buffer_open(inode
, file
, buf
);
1505 int lttng_metadata_ring_buffer_release(struct inode
*inode
, struct file
*file
)
1507 struct lttng_metadata_stream
*stream
= file
->private_data
;
1508 struct lib_ring_buffer
*buf
= stream
->priv
;
1510 mutex_lock(&stream
->metadata_cache
->lock
);
1511 list_del(&stream
->list
);
1512 mutex_unlock(&stream
->metadata_cache
->lock
);
1513 kref_put(&stream
->metadata_cache
->refcount
, metadata_cache_destroy
);
1514 module_put(stream
->transport
->owner
);
1516 return lib_ring_buffer_release(inode
, file
, buf
);
1520 ssize_t
lttng_metadata_ring_buffer_splice_read(struct file
*in
, loff_t
*ppos
,
1521 struct pipe_inode_info
*pipe
, size_t len
,
1524 struct lttng_metadata_stream
*stream
= in
->private_data
;
1525 struct lib_ring_buffer
*buf
= stream
->priv
;
1527 return lib_ring_buffer_splice_read(in
, ppos
, pipe
, len
,
1532 int lttng_metadata_ring_buffer_mmap(struct file
*filp
,
1533 struct vm_area_struct
*vma
)
1535 struct lttng_metadata_stream
*stream
= filp
->private_data
;
1536 struct lib_ring_buffer
*buf
= stream
->priv
;
1538 return lib_ring_buffer_mmap(filp
, vma
, buf
);
1542 const struct file_operations lttng_metadata_ring_buffer_file_operations
= {
1543 .owner
= THIS_MODULE
,
1544 .open
= lttng_metadata_ring_buffer_open
,
1545 .release
= lttng_metadata_ring_buffer_release
,
1546 .poll
= lttng_metadata_ring_buffer_poll
,
1547 .splice_read
= lttng_metadata_ring_buffer_splice_read
,
1548 .mmap
= lttng_metadata_ring_buffer_mmap
,
1549 .unlocked_ioctl
= lttng_metadata_ring_buffer_ioctl
,
1550 .llseek
= vfs_lib_ring_buffer_no_llseek
,
1551 #ifdef CONFIG_COMPAT
1552 .compat_ioctl
= lttng_metadata_ring_buffer_compat_ioctl
,
1557 int lttng_abi_create_stream_fd(struct file
*channel_file
, void *stream_priv
,
1558 const struct file_operations
*fops
, const char *name
)
1561 struct file
*stream_file
;
1563 stream_fd
= lttng_get_unused_fd();
1564 if (stream_fd
< 0) {
1568 stream_file
= anon_inode_getfile(name
, fops
, stream_priv
, O_RDWR
);
1569 if (IS_ERR(stream_file
)) {
1570 ret
= PTR_ERR(stream_file
);
1574 * OPEN_FMODE, called within anon_inode_getfile/alloc_file, don't honor
1575 * FMODE_LSEEK, FMODE_PREAD nor FMODE_PWRITE. We need to read from this
1576 * file descriptor, so we set FMODE_PREAD here.
1578 stream_file
->f_mode
|= FMODE_PREAD
;
1579 fd_install(stream_fd
, stream_file
);
1581 * The stream holds a reference to the channel within the generic ring
1582 * buffer library, so no need to hold a refcount on the channel and
1583 * session files here.
1588 put_unused_fd(stream_fd
);
1594 int lttng_abi_open_stream(struct file
*channel_file
)
1596 struct lttng_channel
*channel
= channel_file
->private_data
;
1597 struct lib_ring_buffer
*buf
;
1601 buf
= channel
->ops
->buffer_read_open(channel
->chan
);
1606 ret
= lttng_abi_create_stream_fd(channel_file
, stream_priv
,
1607 <tng_stream_ring_buffer_file_operations
,
1615 channel
->ops
->buffer_read_close(buf
);
1620 int lttng_abi_open_metadata_stream(struct file
*channel_file
)
1622 struct lttng_channel
*channel
= channel_file
->private_data
;
1623 struct lttng_session
*session
= channel
->session
;
1624 struct lib_ring_buffer
*buf
;
1626 struct lttng_metadata_stream
*metadata_stream
;
1629 buf
= channel
->ops
->buffer_read_open(channel
->chan
);
1633 metadata_stream
= kzalloc(sizeof(struct lttng_metadata_stream
),
1635 if (!metadata_stream
) {
1639 metadata_stream
->metadata_cache
= session
->metadata_cache
;
1640 init_waitqueue_head(&metadata_stream
->read_wait
);
1641 metadata_stream
->priv
= buf
;
1642 stream_priv
= metadata_stream
;
1643 metadata_stream
->transport
= channel
->transport
;
1644 /* Initial state is an empty metadata, considered as incoherent. */
1645 metadata_stream
->coherent
= false;
1648 * Since life-time of metadata cache differs from that of
1649 * session, we need to keep our own reference on the transport.
1651 if (!try_module_get(metadata_stream
->transport
->owner
)) {
1652 printk(KERN_WARNING
"LTTng: Can't lock transport module.\n");
1657 if (!lttng_kref_get(&session
->metadata_cache
->refcount
)) {
1662 ret
= lttng_abi_create_stream_fd(channel_file
, stream_priv
,
1663 <tng_metadata_ring_buffer_file_operations
,
1664 "[lttng_metadata_stream]");
1668 mutex_lock(&session
->metadata_cache
->lock
);
1669 list_add(&metadata_stream
->list
,
1670 &session
->metadata_cache
->metadata_stream
);
1671 mutex_unlock(&session
->metadata_cache
->lock
);
1675 kref_put(&session
->metadata_cache
->refcount
, metadata_cache_destroy
);
1677 module_put(metadata_stream
->transport
->owner
);
1679 kfree(metadata_stream
);
1681 channel
->ops
->buffer_read_close(buf
);
1686 int lttng_abi_open_event_notifier_group_stream(struct file
*notif_file
)
1688 struct lttng_event_notifier_group
*event_notifier_group
= notif_file
->private_data
;
1689 struct channel
*chan
= event_notifier_group
->chan
;
1690 struct lib_ring_buffer
*buf
;
1694 buf
= event_notifier_group
->ops
->buffer_read_open(chan
);
1698 /* The event_notifier notification fd holds a reference on the event_notifier group */
1699 if (!atomic_long_add_unless(¬if_file
->f_count
, 1, LONG_MAX
)) {
1701 goto refcount_error
;
1703 event_notifier_group
->buf
= buf
;
1704 stream_priv
= event_notifier_group
;
1705 ret
= lttng_abi_create_stream_fd(notif_file
, stream_priv
,
1706 <tng_event_notifier_group_notif_fops
,
1707 "[lttng_event_notifier_stream]");
1714 atomic_long_dec(¬if_file
->f_count
);
1716 event_notifier_group
->ops
->buffer_read_close(buf
);
1721 int lttng_abi_validate_event_param(struct lttng_kernel_event
*event_param
)
1723 /* Limit ABI to implemented features. */
1724 switch (event_param
->instrumentation
) {
1725 case LTTNG_KERNEL_SYSCALL
:
1726 switch (event_param
->u
.syscall
.entryexit
) {
1727 case LTTNG_KERNEL_SYSCALL_ENTRY
:
1728 case LTTNG_KERNEL_SYSCALL_EXIT
:
1729 case LTTNG_KERNEL_SYSCALL_ENTRYEXIT
:
1734 switch (event_param
->u
.syscall
.abi
) {
1735 case LTTNG_KERNEL_SYSCALL_ABI_ALL
:
1740 switch (event_param
->u
.syscall
.match
) {
1741 case LTTNG_KERNEL_SYSCALL_MATCH_NAME
:
1748 case LTTNG_KERNEL_TRACEPOINT
: /* Fallthrough */
1749 case LTTNG_KERNEL_KPROBE
: /* Fallthrough */
1750 case LTTNG_KERNEL_KRETPROBE
: /* Fallthrough */
1751 case LTTNG_KERNEL_NOOP
: /* Fallthrough */
1752 case LTTNG_KERNEL_UPROBE
:
1755 case LTTNG_KERNEL_FUNCTION
: /* Fallthrough */
1763 int lttng_abi_create_event(struct file
*channel_file
,
1764 struct lttng_kernel_event
*event_param
)
1766 struct lttng_channel
*channel
= channel_file
->private_data
;
1768 struct file
*event_file
;
1771 event_param
->name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
1772 switch (event_param
->instrumentation
) {
1773 case LTTNG_KERNEL_KRETPROBE
:
1774 event_param
->u
.kretprobe
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
1776 case LTTNG_KERNEL_KPROBE
:
1777 event_param
->u
.kprobe
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
1779 case LTTNG_KERNEL_FUNCTION
:
1781 /* Not implemented. */
1786 event_fd
= lttng_get_unused_fd();
1791 event_file
= anon_inode_getfile("[lttng_event]",
1794 if (IS_ERR(event_file
)) {
1795 ret
= PTR_ERR(event_file
);
1798 /* The event holds a reference on the channel */
1799 if (!atomic_long_add_unless(&channel_file
->f_count
, 1, LONG_MAX
)) {
1801 goto refcount_error
;
1803 ret
= lttng_abi_validate_event_param(event_param
);
1806 if (event_param
->instrumentation
== LTTNG_KERNEL_TRACEPOINT
1807 || event_param
->instrumentation
== LTTNG_KERNEL_SYSCALL
) {
1808 struct lttng_event_enabler
*event_enabler
;
1810 if (strutils_is_star_glob_pattern(event_param
->name
)) {
1812 * If the event name is a star globbing pattern,
1813 * we create the special star globbing enabler.
1815 event_enabler
= lttng_event_enabler_create(LTTNG_ENABLER_FORMAT_STAR_GLOB
,
1816 event_param
, channel
);
1818 event_enabler
= lttng_event_enabler_create(LTTNG_ENABLER_FORMAT_NAME
,
1819 event_param
, channel
);
1821 priv
= event_enabler
;
1823 struct lttng_event
*event
;
1826 * We tolerate no failure path after event creation. It
1827 * will stay invariant for the rest of the session.
1829 event
= lttng_event_create(channel
, event_param
,
1831 event_param
->instrumentation
);
1832 WARN_ON_ONCE(!event
);
1833 if (IS_ERR(event
)) {
1834 ret
= PTR_ERR(event
);
1839 event_file
->private_data
= priv
;
1840 fd_install(event_fd
, event_file
);
1844 atomic_long_dec(&channel_file
->f_count
);
1848 put_unused_fd(event_fd
);
1854 long lttng_event_notifier_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1856 struct lttng_event_notifier
*event_notifier
;
1857 struct lttng_event_notifier_enabler
*event_notifier_enabler
;
1858 enum lttng_event_type
*evtype
= file
->private_data
;
1861 case LTTNG_KERNEL_ENABLE
:
1863 case LTTNG_TYPE_EVENT
:
1864 event_notifier
= file
->private_data
;
1865 return lttng_event_notifier_enable(event_notifier
);
1866 case LTTNG_TYPE_ENABLER
:
1867 event_notifier_enabler
= file
->private_data
;
1868 return lttng_event_notifier_enabler_enable(event_notifier_enabler
);
1873 case LTTNG_KERNEL_DISABLE
:
1875 case LTTNG_TYPE_EVENT
:
1876 event_notifier
= file
->private_data
;
1877 return lttng_event_notifier_disable(event_notifier
);
1878 case LTTNG_TYPE_ENABLER
:
1879 event_notifier_enabler
= file
->private_data
;
1880 return lttng_event_notifier_enabler_disable(event_notifier_enabler
);
1885 case LTTNG_KERNEL_FILTER
:
1887 case LTTNG_TYPE_EVENT
:
1889 case LTTNG_TYPE_ENABLER
:
1890 event_notifier_enabler
= file
->private_data
;
1891 return lttng_event_notifier_enabler_attach_filter_bytecode(
1892 event_notifier_enabler
,
1893 (struct lttng_kernel_filter_bytecode __user
*) arg
);
1899 case LTTNG_KERNEL_CAPTURE
:
1901 case LTTNG_TYPE_EVENT
:
1903 case LTTNG_TYPE_ENABLER
:
1904 event_notifier_enabler
= file
->private_data
;
1905 return lttng_event_notifier_enabler_attach_capture_bytecode(
1906 event_notifier_enabler
,
1907 (struct lttng_kernel_capture_bytecode __user
*) arg
);
1912 case LTTNG_KERNEL_ADD_CALLSITE
:
1914 case LTTNG_TYPE_EVENT
:
1915 event_notifier
= file
->private_data
;
1916 return lttng_event_notifier_add_callsite(event_notifier
,
1917 (struct lttng_kernel_event_callsite __user
*) arg
);
1918 case LTTNG_TYPE_ENABLER
:
1925 return -ENOIOCTLCMD
;
1930 int lttng_event_notifier_release(struct inode
*inode
, struct file
*file
)
1932 struct lttng_event_notifier
*event_notifier
;
1933 struct lttng_event_notifier_enabler
*event_notifier_enabler
;
1934 enum lttng_event_type
*evtype
= file
->private_data
;
1940 case LTTNG_TYPE_EVENT
:
1941 event_notifier
= file
->private_data
;
1943 fput(event_notifier
->group
->file
);
1945 case LTTNG_TYPE_ENABLER
:
1946 event_notifier_enabler
= file
->private_data
;
1947 if (event_notifier_enabler
)
1948 fput(event_notifier_enabler
->group
->file
);
1958 static const struct file_operations lttng_event_notifier_fops
= {
1959 .owner
= THIS_MODULE
,
1960 .release
= lttng_event_notifier_release
,
1961 .unlocked_ioctl
= lttng_event_notifier_ioctl
,
1962 #ifdef CONFIG_COMPAT
1963 .compat_ioctl
= lttng_event_notifier_ioctl
,
1968 int lttng_abi_create_event_notifier(struct file
*event_notifier_group_file
,
1969 struct lttng_kernel_event_notifier
*event_notifier_param
)
1971 struct lttng_event_notifier_group
*event_notifier_group
=
1972 event_notifier_group_file
->private_data
;
1973 int event_notifier_fd
, ret
;
1974 struct file
*event_notifier_file
;
1977 switch (event_notifier_param
->event
.instrumentation
) {
1978 case LTTNG_KERNEL_TRACEPOINT
:
1979 case LTTNG_KERNEL_UPROBE
:
1981 case LTTNG_KERNEL_KPROBE
:
1982 event_notifier_param
->event
.u
.kprobe
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
1984 case LTTNG_KERNEL_SYSCALL
:
1986 case LTTNG_KERNEL_KRETPROBE
:
1987 /* Placing an event notifier on kretprobe is not supported. */
1988 case LTTNG_KERNEL_FUNCTION
:
1989 case LTTNG_KERNEL_NOOP
:
1995 event_notifier_param
->event
.name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
1997 event_notifier_fd
= lttng_get_unused_fd();
1998 if (event_notifier_fd
< 0) {
1999 ret
= event_notifier_fd
;
2003 event_notifier_file
= anon_inode_getfile("[lttng_event_notifier]",
2004 <tng_event_notifier_fops
,
2006 if (IS_ERR(event_notifier_file
)) {
2007 ret
= PTR_ERR(event_notifier_file
);
2011 /* The event notifier holds a reference on the event notifier group. */
2012 if (!atomic_long_add_unless(&event_notifier_group_file
->f_count
, 1, LONG_MAX
)) {
2014 goto refcount_error
;
2017 if (event_notifier_param
->event
.instrumentation
== LTTNG_KERNEL_TRACEPOINT
2018 || event_notifier_param
->event
.instrumentation
== LTTNG_KERNEL_SYSCALL
) {
2019 struct lttng_event_notifier_enabler
*enabler
;
2021 if (strutils_is_star_glob_pattern(event_notifier_param
->event
.name
)) {
2023 * If the event name is a star globbing pattern,
2024 * we create the special star globbing enabler.
2026 enabler
= lttng_event_notifier_enabler_create(
2027 event_notifier_group
,
2028 LTTNG_ENABLER_FORMAT_STAR_GLOB
,
2029 event_notifier_param
);
2031 enabler
= lttng_event_notifier_enabler_create(
2032 event_notifier_group
,
2033 LTTNG_ENABLER_FORMAT_NAME
,
2034 event_notifier_param
);
2038 struct lttng_event_notifier
*event_notifier
;
2041 * We tolerate no failure path after event notifier creation.
2042 * It will stay invariant for the rest of the session.
2044 event_notifier
= lttng_event_notifier_create(NULL
,
2045 event_notifier_param
->event
.token
,
2046 event_notifier_param
->error_counter_index
,
2047 event_notifier_group
,
2048 event_notifier_param
, NULL
,
2049 event_notifier_param
->event
.instrumentation
);
2050 WARN_ON_ONCE(!event_notifier
);
2051 if (IS_ERR(event_notifier
)) {
2052 ret
= PTR_ERR(event_notifier
);
2053 goto event_notifier_error
;
2055 priv
= event_notifier
;
2057 event_notifier_file
->private_data
= priv
;
2058 fd_install(event_notifier_fd
, event_notifier_file
);
2059 return event_notifier_fd
;
2061 event_notifier_error
:
2062 atomic_long_dec(&event_notifier_group_file
->f_count
);
2064 fput(event_notifier_file
);
2066 put_unused_fd(event_notifier_fd
);
2073 long lttng_abi_event_notifier_group_create_error_counter(
2074 struct file
*event_notifier_group_file
,
2075 const struct lttng_kernel_counter_conf
*error_counter_conf
)
2077 int counter_fd
, ret
;
2078 char *counter_transport_name
;
2080 struct lttng_counter
*counter
= NULL
;
2081 struct file
*counter_file
;
2082 struct lttng_event_notifier_group
*event_notifier_group
=
2083 (struct lttng_event_notifier_group
*) event_notifier_group_file
->private_data
;
2085 if (error_counter_conf
->arithmetic
!= LTTNG_KERNEL_COUNTER_ARITHMETIC_MODULAR
) {
2086 printk(KERN_ERR
"LTTng: event_notifier: Error counter of the wrong arithmetic type.\n");
2090 if (error_counter_conf
->number_dimensions
!= 1) {
2091 printk(KERN_ERR
"LTTng: event_notifier: Error counter has more than one dimension.\n");
2095 switch (error_counter_conf
->bitness
) {
2096 case LTTNG_KERNEL_COUNTER_BITNESS_64
:
2097 counter_transport_name
= "counter-per-cpu-64-modular";
2099 case LTTNG_KERNEL_COUNTER_BITNESS_32
:
2100 counter_transport_name
= "counter-per-cpu-32-modular";
2107 * Lock sessions to provide mutual exclusion against concurrent
2108 * modification of event_notifier group, which would result in
2109 * overwriting the error counter if set concurrently.
2111 lttng_lock_sessions();
2113 if (event_notifier_group
->error_counter
) {
2114 printk(KERN_ERR
"Error counter already created in event_notifier group\n");
2119 counter_fd
= lttng_get_unused_fd();
2120 if (counter_fd
< 0) {
2125 counter_file
= anon_inode_getfile("[lttng_counter]",
2126 <tng_counter_fops
,
2128 if (IS_ERR(counter_file
)) {
2129 ret
= PTR_ERR(counter_file
);
2133 counter_len
= error_counter_conf
->dimensions
[0].size
;
2135 if (!atomic_long_add_unless(&event_notifier_group_file
->f_count
, 1, LONG_MAX
)) {
2137 goto refcount_error
;
2140 counter
= lttng_kernel_counter_create(counter_transport_name
,
2147 event_notifier_group
->error_counter
= counter
;
2148 event_notifier_group
->error_counter_len
= counter_len
;
2150 counter
->file
= counter_file
;
2151 counter
->owner
= event_notifier_group
->file
;
2152 counter_file
->private_data
= counter
;
2153 /* Ownership transferred. */
2156 fd_install(counter_fd
, counter_file
);
2157 lttng_unlock_sessions();
2162 atomic_long_dec(&event_notifier_group_file
->f_count
);
2166 put_unused_fd(counter_fd
);
2168 lttng_unlock_sessions();
2173 long lttng_event_notifier_group_ioctl(struct file
*file
, unsigned int cmd
,
2177 case LTTNG_KERNEL_EVENT_NOTIFIER_GROUP_NOTIFICATION_FD
:
2179 return lttng_abi_open_event_notifier_group_stream(file
);
2181 case LTTNG_KERNEL_EVENT_NOTIFIER_CREATE
:
2183 struct lttng_kernel_event_notifier uevent_notifier_param
;
2185 if (copy_from_user(&uevent_notifier_param
,
2186 (struct lttng_kernel_event_notifier __user
*) arg
,
2187 sizeof(uevent_notifier_param
)))
2189 return lttng_abi_create_event_notifier(file
, &uevent_notifier_param
);
2191 case LTTNG_KERNEL_COUNTER
:
2193 struct lttng_kernel_counter_conf uerror_counter_conf
;
2195 if (copy_from_user(&uerror_counter_conf
,
2196 (struct lttng_kernel_counter_conf __user
*) arg
,
2197 sizeof(uerror_counter_conf
)))
2199 return lttng_abi_event_notifier_group_create_error_counter(file
,
2200 &uerror_counter_conf
);
2203 return -ENOIOCTLCMD
;
2209 int lttng_event_notifier_group_release(struct inode
*inode
, struct file
*file
)
2211 struct lttng_event_notifier_group
*event_notifier_group
=
2214 if (event_notifier_group
)
2215 lttng_event_notifier_group_destroy(event_notifier_group
);
2219 static const struct file_operations lttng_event_notifier_group_fops
= {
2220 .owner
= THIS_MODULE
,
2221 .release
= lttng_event_notifier_group_release
,
2222 .unlocked_ioctl
= lttng_event_notifier_group_ioctl
,
2223 #ifdef CONFIG_COMPAT
2224 .compat_ioctl
= lttng_event_notifier_group_ioctl
,
2229 * lttng_channel_ioctl - lttng syscall through ioctl
2235 * This ioctl implements lttng commands:
2236 * LTTNG_KERNEL_STREAM
2237 * Returns an event stream file descriptor or failure.
2238 * (typically, one event stream records events from one CPU)
2239 * LTTNG_KERNEL_EVENT
2240 * Returns an event file descriptor or failure.
2241 * LTTNG_KERNEL_CONTEXT
2242 * Prepend a context field to each event in the channel
2243 * LTTNG_KERNEL_ENABLE
2244 * Enable recording for events in this channel (weak enable)
2245 * LTTNG_KERNEL_DISABLE
2246 * Disable recording for events in this channel (strong disable)
2248 * Channel and event file descriptors also hold a reference on the session.
2251 long lttng_channel_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
2253 struct lttng_channel
*channel
= file
->private_data
;
2256 case LTTNG_KERNEL_OLD_STREAM
:
2257 case LTTNG_KERNEL_STREAM
:
2258 return lttng_abi_open_stream(file
);
2259 case LTTNG_KERNEL_OLD_EVENT
:
2261 struct lttng_kernel_event
*uevent_param
;
2262 struct lttng_kernel_old_event
*old_uevent_param
;
2265 uevent_param
= kmalloc(sizeof(struct lttng_kernel_event
),
2267 if (!uevent_param
) {
2271 old_uevent_param
= kmalloc(
2272 sizeof(struct lttng_kernel_old_event
),
2274 if (!old_uevent_param
) {
2276 goto old_event_error_free_param
;
2278 if (copy_from_user(old_uevent_param
,
2279 (struct lttng_kernel_old_event __user
*) arg
,
2280 sizeof(struct lttng_kernel_old_event
))) {
2282 goto old_event_error_free_old_param
;
2285 memcpy(uevent_param
->name
, old_uevent_param
->name
,
2286 sizeof(uevent_param
->name
));
2287 uevent_param
->instrumentation
=
2288 old_uevent_param
->instrumentation
;
2290 switch (old_uevent_param
->instrumentation
) {
2291 case LTTNG_KERNEL_KPROBE
:
2292 uevent_param
->u
.kprobe
.addr
=
2293 old_uevent_param
->u
.kprobe
.addr
;
2294 uevent_param
->u
.kprobe
.offset
=
2295 old_uevent_param
->u
.kprobe
.offset
;
2296 memcpy(uevent_param
->u
.kprobe
.symbol_name
,
2297 old_uevent_param
->u
.kprobe
.symbol_name
,
2298 sizeof(uevent_param
->u
.kprobe
.symbol_name
));
2300 case LTTNG_KERNEL_KRETPROBE
:
2301 uevent_param
->u
.kretprobe
.addr
=
2302 old_uevent_param
->u
.kretprobe
.addr
;
2303 uevent_param
->u
.kretprobe
.offset
=
2304 old_uevent_param
->u
.kretprobe
.offset
;
2305 memcpy(uevent_param
->u
.kretprobe
.symbol_name
,
2306 old_uevent_param
->u
.kretprobe
.symbol_name
,
2307 sizeof(uevent_param
->u
.kretprobe
.symbol_name
));
2309 case LTTNG_KERNEL_FUNCTION
:
2311 /* Not implemented. */
2316 ret
= lttng_abi_create_event(file
, uevent_param
);
2318 old_event_error_free_old_param
:
2319 kfree(old_uevent_param
);
2320 old_event_error_free_param
:
2321 kfree(uevent_param
);
2325 case LTTNG_KERNEL_EVENT
:
2327 struct lttng_kernel_event uevent_param
;
2329 if (copy_from_user(&uevent_param
,
2330 (struct lttng_kernel_event __user
*) arg
,
2331 sizeof(uevent_param
)))
2333 return lttng_abi_create_event(file
, &uevent_param
);
2335 case LTTNG_KERNEL_OLD_CONTEXT
:
2337 struct lttng_kernel_context
*ucontext_param
;
2338 struct lttng_kernel_old_context
*old_ucontext_param
;
2341 ucontext_param
= kmalloc(sizeof(struct lttng_kernel_context
),
2343 if (!ucontext_param
) {
2347 old_ucontext_param
= kmalloc(sizeof(struct lttng_kernel_old_context
),
2349 if (!old_ucontext_param
) {
2351 goto old_ctx_error_free_param
;
2354 if (copy_from_user(old_ucontext_param
,
2355 (struct lttng_kernel_old_context __user
*) arg
,
2356 sizeof(struct lttng_kernel_old_context
))) {
2358 goto old_ctx_error_free_old_param
;
2360 ucontext_param
->ctx
= old_ucontext_param
->ctx
;
2361 memcpy(ucontext_param
->padding
, old_ucontext_param
->padding
,
2362 sizeof(ucontext_param
->padding
));
2363 /* only type that uses the union */
2364 if (old_ucontext_param
->ctx
== LTTNG_KERNEL_CONTEXT_PERF_COUNTER
) {
2365 ucontext_param
->u
.perf_counter
.type
=
2366 old_ucontext_param
->u
.perf_counter
.type
;
2367 ucontext_param
->u
.perf_counter
.config
=
2368 old_ucontext_param
->u
.perf_counter
.config
;
2369 memcpy(ucontext_param
->u
.perf_counter
.name
,
2370 old_ucontext_param
->u
.perf_counter
.name
,
2371 sizeof(ucontext_param
->u
.perf_counter
.name
));
2374 ret
= lttng_abi_add_context(file
,
2376 &channel
->ctx
, channel
->session
);
2378 old_ctx_error_free_old_param
:
2379 kfree(old_ucontext_param
);
2380 old_ctx_error_free_param
:
2381 kfree(ucontext_param
);
2385 case LTTNG_KERNEL_CONTEXT
:
2387 struct lttng_kernel_context ucontext_param
;
2389 if (copy_from_user(&ucontext_param
,
2390 (struct lttng_kernel_context __user
*) arg
,
2391 sizeof(ucontext_param
)))
2393 return lttng_abi_add_context(file
,
2395 &channel
->ctx
, channel
->session
);
2397 case LTTNG_KERNEL_OLD_ENABLE
:
2398 case LTTNG_KERNEL_ENABLE
:
2399 return lttng_channel_enable(channel
);
2400 case LTTNG_KERNEL_OLD_DISABLE
:
2401 case LTTNG_KERNEL_DISABLE
:
2402 return lttng_channel_disable(channel
);
2403 case LTTNG_KERNEL_SYSCALL_MASK
:
2404 return lttng_channel_syscall_mask(channel
,
2405 (struct lttng_kernel_syscall_mask __user
*) arg
);
2407 return -ENOIOCTLCMD
;
2412 * lttng_metadata_ioctl - lttng syscall through ioctl
2418 * This ioctl implements lttng commands:
2419 * LTTNG_KERNEL_STREAM
2420 * Returns an event stream file descriptor or failure.
2422 * Channel and event file descriptors also hold a reference on the session.
2425 long lttng_metadata_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
2428 case LTTNG_KERNEL_OLD_STREAM
:
2429 case LTTNG_KERNEL_STREAM
:
2430 return lttng_abi_open_metadata_stream(file
);
2432 return -ENOIOCTLCMD
;
2437 * lttng_channel_poll - lttng stream addition/removal monitoring
2442 unsigned int lttng_channel_poll(struct file
*file
, poll_table
*wait
)
2444 struct lttng_channel
*channel
= file
->private_data
;
2445 unsigned int mask
= 0;
2447 if (file
->f_mode
& FMODE_READ
) {
2448 poll_wait_set_exclusive(wait
);
2449 poll_wait(file
, channel
->ops
->get_hp_wait_queue(channel
->chan
),
2452 if (channel
->ops
->is_disabled(channel
->chan
))
2454 if (channel
->ops
->is_finalized(channel
->chan
))
2456 if (channel
->ops
->buffer_has_read_closed_stream(channel
->chan
))
2457 return POLLIN
| POLLRDNORM
;
2465 int lttng_channel_release(struct inode
*inode
, struct file
*file
)
2467 struct lttng_channel
*channel
= file
->private_data
;
2470 fput(channel
->session
->file
);
2475 int lttng_metadata_channel_release(struct inode
*inode
, struct file
*file
)
2477 struct lttng_channel
*channel
= file
->private_data
;
2480 fput(channel
->session
->file
);
2481 lttng_metadata_channel_destroy(channel
);
2487 static const struct file_operations lttng_channel_fops
= {
2488 .owner
= THIS_MODULE
,
2489 .release
= lttng_channel_release
,
2490 .poll
= lttng_channel_poll
,
2491 .unlocked_ioctl
= lttng_channel_ioctl
,
2492 #ifdef CONFIG_COMPAT
2493 .compat_ioctl
= lttng_channel_ioctl
,
2497 static const struct file_operations lttng_metadata_fops
= {
2498 .owner
= THIS_MODULE
,
2499 .release
= lttng_metadata_channel_release
,
2500 .unlocked_ioctl
= lttng_metadata_ioctl
,
2501 #ifdef CONFIG_COMPAT
2502 .compat_ioctl
= lttng_metadata_ioctl
,
2507 * lttng_event_ioctl - lttng syscall through ioctl
2513 * This ioctl implements lttng commands:
2514 * LTTNG_KERNEL_CONTEXT
2515 * Prepend a context field to each record of this event
2516 * LTTNG_KERNEL_ENABLE
2517 * Enable recording for this event (weak enable)
2518 * LTTNG_KERNEL_DISABLE
2519 * Disable recording for this event (strong disable)
2522 long lttng_event_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
2524 struct lttng_event
*event
;
2525 struct lttng_event_enabler
*event_enabler
;
2526 enum lttng_event_type
*evtype
= file
->private_data
;
2529 case LTTNG_KERNEL_OLD_CONTEXT
:
2531 /* Not implemented */
2534 case LTTNG_KERNEL_CONTEXT
:
2536 /* Not implemented */
2539 case LTTNG_KERNEL_OLD_ENABLE
:
2540 case LTTNG_KERNEL_ENABLE
:
2542 case LTTNG_TYPE_EVENT
:
2543 event
= file
->private_data
;
2544 return lttng_event_enable(event
);
2545 case LTTNG_TYPE_ENABLER
:
2546 event_enabler
= file
->private_data
;
2547 return lttng_event_enabler_enable(event_enabler
);
2552 case LTTNG_KERNEL_OLD_DISABLE
:
2553 case LTTNG_KERNEL_DISABLE
:
2555 case LTTNG_TYPE_EVENT
:
2556 event
= file
->private_data
;
2557 return lttng_event_disable(event
);
2558 case LTTNG_TYPE_ENABLER
:
2559 event_enabler
= file
->private_data
;
2560 return lttng_event_enabler_disable(event_enabler
);
2565 case LTTNG_KERNEL_FILTER
:
2567 case LTTNG_TYPE_EVENT
:
2569 case LTTNG_TYPE_ENABLER
:
2571 event_enabler
= file
->private_data
;
2572 return lttng_event_enabler_attach_filter_bytecode(
2574 (struct lttng_kernel_filter_bytecode __user
*) arg
);
2580 case LTTNG_KERNEL_ADD_CALLSITE
:
2582 case LTTNG_TYPE_EVENT
:
2583 event
= file
->private_data
;
2584 return lttng_event_add_callsite(event
,
2585 (struct lttng_kernel_event_callsite __user
*) arg
);
2586 case LTTNG_TYPE_ENABLER
:
2593 return -ENOIOCTLCMD
;
2598 int lttng_event_release(struct inode
*inode
, struct file
*file
)
2600 struct lttng_event
*event
;
2601 struct lttng_event_enabler
*event_enabler
;
2602 enum lttng_event_type
*evtype
= file
->private_data
;
2608 case LTTNG_TYPE_EVENT
:
2609 event
= file
->private_data
;
2611 fput(event
->chan
->file
);
2613 case LTTNG_TYPE_ENABLER
:
2614 event_enabler
= file
->private_data
;
2616 fput(event_enabler
->chan
->file
);
2626 /* TODO: filter control ioctl */
2627 static const struct file_operations lttng_event_fops
= {
2628 .owner
= THIS_MODULE
,
2629 .release
= lttng_event_release
,
2630 .unlocked_ioctl
= lttng_event_ioctl
,
2631 #ifdef CONFIG_COMPAT
2632 .compat_ioctl
= lttng_event_ioctl
,
2636 static int put_u64(uint64_t val
, unsigned long arg
)
2638 return put_user(val
, (uint64_t __user
*) arg
);
2641 static int put_u32(uint32_t val
, unsigned long arg
)
2643 return put_user(val
, (uint32_t __user
*) arg
);
2646 static long lttng_stream_ring_buffer_ioctl(struct file
*filp
,
2647 unsigned int cmd
, unsigned long arg
)
2649 struct lib_ring_buffer
*buf
= filp
->private_data
;
2650 struct channel
*chan
= buf
->backend
.chan
;
2651 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
2652 const struct lttng_channel_ops
*ops
= chan
->backend
.priv_ops
;
2655 if (atomic_read(&chan
->record_disabled
))
2659 case LTTNG_RING_BUFFER_GET_TIMESTAMP_BEGIN
:
2663 ret
= ops
->timestamp_begin(config
, buf
, &ts
);
2666 return put_u64(ts
, arg
);
2668 case LTTNG_RING_BUFFER_GET_TIMESTAMP_END
:
2672 ret
= ops
->timestamp_end(config
, buf
, &ts
);
2675 return put_u64(ts
, arg
);
2677 case LTTNG_RING_BUFFER_GET_EVENTS_DISCARDED
:
2681 ret
= ops
->events_discarded(config
, buf
, &ed
);
2684 return put_u64(ed
, arg
);
2686 case LTTNG_RING_BUFFER_GET_CONTENT_SIZE
:
2690 ret
= ops
->content_size(config
, buf
, &cs
);
2693 return put_u64(cs
, arg
);
2695 case LTTNG_RING_BUFFER_GET_PACKET_SIZE
:
2699 ret
= ops
->packet_size(config
, buf
, &ps
);
2702 return put_u64(ps
, arg
);
2704 case LTTNG_RING_BUFFER_GET_STREAM_ID
:
2708 ret
= ops
->stream_id(config
, buf
, &si
);
2711 return put_u64(si
, arg
);
2713 case LTTNG_RING_BUFFER_GET_CURRENT_TIMESTAMP
:
2717 ret
= ops
->current_timestamp(config
, buf
, &ts
);
2720 return put_u64(ts
, arg
);
2722 case LTTNG_RING_BUFFER_GET_SEQ_NUM
:
2726 ret
= ops
->sequence_number(config
, buf
, &seq
);
2729 return put_u64(seq
, arg
);
2731 case LTTNG_RING_BUFFER_INSTANCE_ID
:
2735 ret
= ops
->instance_id(config
, buf
, &id
);
2738 return put_u64(id
, arg
);
2741 return lib_ring_buffer_file_operations
.unlocked_ioctl(filp
,
2749 #ifdef CONFIG_COMPAT
2750 static long lttng_stream_ring_buffer_compat_ioctl(struct file
*filp
,
2751 unsigned int cmd
, unsigned long arg
)
2753 struct lib_ring_buffer
*buf
= filp
->private_data
;
2754 struct channel
*chan
= buf
->backend
.chan
;
2755 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
2756 const struct lttng_channel_ops
*ops
= chan
->backend
.priv_ops
;
2759 if (atomic_read(&chan
->record_disabled
))
2763 case LTTNG_RING_BUFFER_COMPAT_GET_TIMESTAMP_BEGIN
:
2767 ret
= ops
->timestamp_begin(config
, buf
, &ts
);
2770 return put_u64(ts
, arg
);
2772 case LTTNG_RING_BUFFER_COMPAT_GET_TIMESTAMP_END
:
2776 ret
= ops
->timestamp_end(config
, buf
, &ts
);
2779 return put_u64(ts
, arg
);
2781 case LTTNG_RING_BUFFER_COMPAT_GET_EVENTS_DISCARDED
:
2785 ret
= ops
->events_discarded(config
, buf
, &ed
);
2788 return put_u64(ed
, arg
);
2790 case LTTNG_RING_BUFFER_COMPAT_GET_CONTENT_SIZE
:
2794 ret
= ops
->content_size(config
, buf
, &cs
);
2797 return put_u64(cs
, arg
);
2799 case LTTNG_RING_BUFFER_COMPAT_GET_PACKET_SIZE
:
2803 ret
= ops
->packet_size(config
, buf
, &ps
);
2806 return put_u64(ps
, arg
);
2808 case LTTNG_RING_BUFFER_COMPAT_GET_STREAM_ID
:
2812 ret
= ops
->stream_id(config
, buf
, &si
);
2815 return put_u64(si
, arg
);
2817 case LTTNG_RING_BUFFER_GET_CURRENT_TIMESTAMP
:
2821 ret
= ops
->current_timestamp(config
, buf
, &ts
);
2824 return put_u64(ts
, arg
);
2826 case LTTNG_RING_BUFFER_COMPAT_GET_SEQ_NUM
:
2830 ret
= ops
->sequence_number(config
, buf
, &seq
);
2833 return put_u64(seq
, arg
);
2835 case LTTNG_RING_BUFFER_COMPAT_INSTANCE_ID
:
2839 ret
= ops
->instance_id(config
, buf
, &id
);
2842 return put_u64(id
, arg
);
2845 return lib_ring_buffer_file_operations
.compat_ioctl(filp
,
2852 #endif /* CONFIG_COMPAT */
2854 static void lttng_stream_override_ring_buffer_fops(void)
2856 lttng_stream_ring_buffer_file_operations
.owner
= THIS_MODULE
;
2857 lttng_stream_ring_buffer_file_operations
.open
=
2858 lib_ring_buffer_file_operations
.open
;
2859 lttng_stream_ring_buffer_file_operations
.release
=
2860 lib_ring_buffer_file_operations
.release
;
2861 lttng_stream_ring_buffer_file_operations
.poll
=
2862 lib_ring_buffer_file_operations
.poll
;
2863 lttng_stream_ring_buffer_file_operations
.splice_read
=
2864 lib_ring_buffer_file_operations
.splice_read
;
2865 lttng_stream_ring_buffer_file_operations
.mmap
=
2866 lib_ring_buffer_file_operations
.mmap
;
2867 lttng_stream_ring_buffer_file_operations
.unlocked_ioctl
=
2868 lttng_stream_ring_buffer_ioctl
;
2869 lttng_stream_ring_buffer_file_operations
.llseek
=
2870 lib_ring_buffer_file_operations
.llseek
;
2871 #ifdef CONFIG_COMPAT
2872 lttng_stream_ring_buffer_file_operations
.compat_ioctl
=
2873 lttng_stream_ring_buffer_compat_ioctl
;
2877 int __init
lttng_abi_init(void)
2881 wrapper_vmalloc_sync_mappings();
2884 ret
= lttng_tp_mempool_init();
2889 lttng_proc_dentry
= proc_create_data("lttng", S_IRUSR
| S_IWUSR
, NULL
,
2890 <tng_proc_ops
, NULL
);
2892 if (!lttng_proc_dentry
) {
2893 printk(KERN_ERR
"LTTng: Error creating control file\n");
2897 lttng_stream_override_ring_buffer_fops();
2901 lttng_tp_mempool_destroy();
2902 lttng_clock_unref();
2906 /* No __exit annotation because used by init error path too. */
2907 void lttng_abi_exit(void)
2909 lttng_tp_mempool_destroy();
2910 lttng_clock_unref();
2911 if (lttng_proc_dentry
)
2912 remove_proc_entry("lttng", NULL
);