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 <wrapper/barrier.h>
41 #include <lttng/string-utils.h>
42 #include <lttng/abi.h>
43 #include <lttng/abi-old.h>
44 #include <lttng/events.h>
45 #include <lttng/events-internal.h>
46 #include <lttng/tracer.h>
47 #include <lttng/tp-mempool.h>
48 #include <ringbuffer/frontend_types.h>
49 #include <ringbuffer/iterator.h>
52 * This is LTTng's own personal way to create a system call as an external
53 * module. We use ioctl() on /proc/lttng.
56 static struct proc_dir_entry
*lttng_proc_dentry
;
58 #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(5,6,0))
59 static const struct proc_ops lttng_proc_ops
;
61 static const struct file_operations lttng_proc_ops
;
64 static const struct file_operations lttng_session_fops
;
65 static const struct file_operations lttng_event_notifier_group_fops
;
66 static const struct file_operations lttng_channel_fops
;
67 static const struct file_operations lttng_metadata_fops
;
68 static const struct file_operations lttng_event_recorder_event_fops
;
69 static const struct file_operations lttng_event_recorder_enabler_fops
;
70 static struct file_operations lttng_stream_ring_buffer_file_operations
;
72 static int put_u64(uint64_t val
, unsigned long arg
);
73 static int put_u32(uint32_t val
, unsigned long arg
);
75 static int validate_zeroed_padding(char *p
, size_t len
)
79 for (i
= 0; i
< len
; i
++) {
87 * Teardown management: opened file descriptors keep a refcount on the module,
88 * so it can only exit when all file descriptors are closed.
92 int lttng_abi_create_session(void)
94 struct lttng_session
*session
;
95 struct file
*session_file
;
98 session
= lttng_session_create();
101 session_fd
= lttng_get_unused_fd();
102 if (session_fd
< 0) {
106 session_file
= anon_inode_getfile("[lttng_session]",
109 if (IS_ERR(session_file
)) {
110 ret
= PTR_ERR(session_file
);
113 session
->file
= session_file
;
114 fd_install(session_fd
, session_file
);
118 put_unused_fd(session_fd
);
120 lttng_session_destroy(session
);
124 void event_notifier_send_notification_work_wakeup(struct irq_work
*entry
)
126 struct lttng_event_notifier_group
*event_notifier_group
=
127 container_of(entry
, struct lttng_event_notifier_group
,
129 wake_up_interruptible(&event_notifier_group
->read_wait
);
133 int lttng_abi_create_event_notifier_group(void)
135 struct lttng_event_notifier_group
*event_notifier_group
;
136 struct file
*event_notifier_group_file
;
137 int event_notifier_group_fd
, ret
;
139 event_notifier_group
= lttng_event_notifier_group_create();
140 if (!event_notifier_group
)
143 event_notifier_group_fd
= lttng_get_unused_fd();
144 if (event_notifier_group_fd
< 0) {
145 ret
= event_notifier_group_fd
;
148 event_notifier_group_file
= anon_inode_getfile("[lttng_event_notifier_group]",
149 <tng_event_notifier_group_fops
,
150 event_notifier_group
, O_RDWR
);
151 if (IS_ERR(event_notifier_group_file
)) {
152 ret
= PTR_ERR(event_notifier_group_file
);
156 event_notifier_group
->file
= event_notifier_group_file
;
157 init_waitqueue_head(&event_notifier_group
->read_wait
);
158 init_irq_work(&event_notifier_group
->wakeup_pending
,
159 event_notifier_send_notification_work_wakeup
);
160 fd_install(event_notifier_group_fd
, event_notifier_group_file
);
161 return event_notifier_group_fd
;
164 put_unused_fd(event_notifier_group_fd
);
166 lttng_event_notifier_group_destroy(event_notifier_group
);
171 int lttng_abi_tracepoint_list(void)
173 struct file
*tracepoint_list_file
;
176 file_fd
= lttng_get_unused_fd();
182 tracepoint_list_file
= anon_inode_getfile("[lttng_tracepoint_list]",
183 <tng_tracepoint_list_fops
,
185 if (IS_ERR(tracepoint_list_file
)) {
186 ret
= PTR_ERR(tracepoint_list_file
);
189 ret
= lttng_tracepoint_list_fops
.open(NULL
, tracepoint_list_file
);
192 fd_install(file_fd
, tracepoint_list_file
);
196 fput(tracepoint_list_file
);
198 put_unused_fd(file_fd
);
203 #ifndef CONFIG_HAVE_SYSCALL_TRACEPOINTS
205 int lttng_abi_syscall_list(void)
211 int lttng_abi_syscall_list(void)
213 struct file
*syscall_list_file
;
216 file_fd
= lttng_get_unused_fd();
222 syscall_list_file
= anon_inode_getfile("[lttng_syscall_list]",
223 <tng_syscall_list_fops
,
225 if (IS_ERR(syscall_list_file
)) {
226 ret
= PTR_ERR(syscall_list_file
);
229 ret
= lttng_syscall_list_fops
.open(NULL
, syscall_list_file
);
232 fd_install(file_fd
, syscall_list_file
);
236 fput(syscall_list_file
);
238 put_unused_fd(file_fd
);
245 void lttng_abi_tracer_version(struct lttng_kernel_abi_tracer_version
*v
)
247 v
->major
= LTTNG_MODULES_MAJOR_VERSION
;
248 v
->minor
= LTTNG_MODULES_MINOR_VERSION
;
249 v
->patchlevel
= LTTNG_MODULES_PATCHLEVEL_VERSION
;
253 void lttng_abi_tracer_abi_version(struct lttng_kernel_abi_tracer_abi_version
*v
)
255 v
->major
= LTTNG_KERNEL_ABI_MAJOR_VERSION
;
256 v
->minor
= LTTNG_KERNEL_ABI_MINOR_VERSION
;
260 long lttng_abi_add_context(struct file
*file
,
261 struct lttng_kernel_abi_context
*context_param
,
262 struct lttng_kernel_ctx
**ctx
, struct lttng_session
*session
)
265 if (session
->been_active
)
268 switch (context_param
->ctx
) {
269 case LTTNG_KERNEL_ABI_CONTEXT_PID
:
270 return lttng_add_pid_to_ctx(ctx
);
271 case LTTNG_KERNEL_ABI_CONTEXT_PRIO
:
272 return lttng_add_prio_to_ctx(ctx
);
273 case LTTNG_KERNEL_ABI_CONTEXT_NICE
:
274 return lttng_add_nice_to_ctx(ctx
);
275 case LTTNG_KERNEL_ABI_CONTEXT_VPID
:
276 return lttng_add_vpid_to_ctx(ctx
);
277 case LTTNG_KERNEL_ABI_CONTEXT_TID
:
278 return lttng_add_tid_to_ctx(ctx
);
279 case LTTNG_KERNEL_ABI_CONTEXT_VTID
:
280 return lttng_add_vtid_to_ctx(ctx
);
281 case LTTNG_KERNEL_ABI_CONTEXT_PPID
:
282 return lttng_add_ppid_to_ctx(ctx
);
283 case LTTNG_KERNEL_ABI_CONTEXT_VPPID
:
284 return lttng_add_vppid_to_ctx(ctx
);
285 case LTTNG_KERNEL_ABI_CONTEXT_PERF_COUNTER
:
286 context_param
->u
.perf_counter
.name
[LTTNG_KERNEL_ABI_SYM_NAME_LEN
- 1] = '\0';
287 return lttng_add_perf_counter_to_ctx(context_param
->u
.perf_counter
.type
,
288 context_param
->u
.perf_counter
.config
,
289 context_param
->u
.perf_counter
.name
,
291 case LTTNG_KERNEL_ABI_CONTEXT_PROCNAME
:
292 return lttng_add_procname_to_ctx(ctx
);
293 case LTTNG_KERNEL_ABI_CONTEXT_HOSTNAME
:
294 return lttng_add_hostname_to_ctx(ctx
);
295 case LTTNG_KERNEL_ABI_CONTEXT_CPU_ID
:
296 return lttng_add_cpu_id_to_ctx(ctx
);
297 case LTTNG_KERNEL_ABI_CONTEXT_INTERRUPTIBLE
:
298 return lttng_add_interruptible_to_ctx(ctx
);
299 case LTTNG_KERNEL_ABI_CONTEXT_NEED_RESCHEDULE
:
300 return lttng_add_need_reschedule_to_ctx(ctx
);
301 case LTTNG_KERNEL_ABI_CONTEXT_PREEMPTIBLE
:
302 return lttng_add_preemptible_to_ctx(ctx
);
303 case LTTNG_KERNEL_ABI_CONTEXT_MIGRATABLE
:
304 return lttng_add_migratable_to_ctx(ctx
);
305 case LTTNG_KERNEL_ABI_CONTEXT_CALLSTACK_KERNEL
:
306 case LTTNG_KERNEL_ABI_CONTEXT_CALLSTACK_USER
:
307 return lttng_add_callstack_to_ctx(ctx
, context_param
->ctx
);
308 case LTTNG_KERNEL_ABI_CONTEXT_CGROUP_NS
:
309 return lttng_add_cgroup_ns_to_ctx(ctx
);
310 case LTTNG_KERNEL_ABI_CONTEXT_IPC_NS
:
311 return lttng_add_ipc_ns_to_ctx(ctx
);
312 case LTTNG_KERNEL_ABI_CONTEXT_MNT_NS
:
313 return lttng_add_mnt_ns_to_ctx(ctx
);
314 case LTTNG_KERNEL_ABI_CONTEXT_NET_NS
:
315 return lttng_add_net_ns_to_ctx(ctx
);
316 case LTTNG_KERNEL_ABI_CONTEXT_PID_NS
:
317 return lttng_add_pid_ns_to_ctx(ctx
);
318 case LTTNG_KERNEL_ABI_CONTEXT_USER_NS
:
319 return lttng_add_user_ns_to_ctx(ctx
);
320 case LTTNG_KERNEL_ABI_CONTEXT_UTS_NS
:
321 return lttng_add_uts_ns_to_ctx(ctx
);
322 case LTTNG_KERNEL_ABI_CONTEXT_UID
:
323 return lttng_add_uid_to_ctx(ctx
);
324 case LTTNG_KERNEL_ABI_CONTEXT_EUID
:
325 return lttng_add_euid_to_ctx(ctx
);
326 case LTTNG_KERNEL_ABI_CONTEXT_SUID
:
327 return lttng_add_suid_to_ctx(ctx
);
328 case LTTNG_KERNEL_ABI_CONTEXT_GID
:
329 return lttng_add_gid_to_ctx(ctx
);
330 case LTTNG_KERNEL_ABI_CONTEXT_EGID
:
331 return lttng_add_egid_to_ctx(ctx
);
332 case LTTNG_KERNEL_ABI_CONTEXT_SGID
:
333 return lttng_add_sgid_to_ctx(ctx
);
334 case LTTNG_KERNEL_ABI_CONTEXT_VUID
:
335 return lttng_add_vuid_to_ctx(ctx
);
336 case LTTNG_KERNEL_ABI_CONTEXT_VEUID
:
337 return lttng_add_veuid_to_ctx(ctx
);
338 case LTTNG_KERNEL_ABI_CONTEXT_VSUID
:
339 return lttng_add_vsuid_to_ctx(ctx
);
340 case LTTNG_KERNEL_ABI_CONTEXT_VGID
:
341 return lttng_add_vgid_to_ctx(ctx
);
342 case LTTNG_KERNEL_ABI_CONTEXT_VEGID
:
343 return lttng_add_vegid_to_ctx(ctx
);
344 case LTTNG_KERNEL_ABI_CONTEXT_VSGID
:
345 return lttng_add_vsgid_to_ctx(ctx
);
346 case LTTNG_KERNEL_ABI_CONTEXT_TIME_NS
:
347 return lttng_add_time_ns_to_ctx(ctx
);
354 * lttng_ioctl - lttng syscall through ioctl
360 * This ioctl implements lttng commands:
361 * LTTNG_KERNEL_ABI_SESSION
362 * Returns a LTTng trace session file descriptor
363 * LTTNG_KERNEL_ABI_TRACER_VERSION
364 * Returns the LTTng kernel tracer version
365 * LTTNG_KERNEL_ABI_TRACEPOINT_LIST
366 * Returns a file descriptor listing available tracepoints
367 * LTTNG_KERNEL_ABI_WAIT_QUIESCENT
368 * Returns after all previously running probes have completed
369 * LTTNG_KERNEL_ABI_TRACER_ABI_VERSION
370 * Returns the LTTng kernel tracer ABI version
371 * LTTNG_KERNEL_ABI_EVENT_NOTIFIER_GROUP_CREATE
372 * Returns a LTTng event notifier group file descriptor
374 * The returned session will be deleted when its file descriptor is closed.
377 long lttng_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
380 case LTTNG_KERNEL_ABI_OLD_SESSION
:
381 case LTTNG_KERNEL_ABI_SESSION
:
382 return lttng_abi_create_session();
383 case LTTNG_KERNEL_ABI_EVENT_NOTIFIER_GROUP_CREATE
:
384 return lttng_abi_create_event_notifier_group();
385 case LTTNG_KERNEL_ABI_OLD_TRACER_VERSION
:
387 struct lttng_kernel_abi_tracer_version v
;
388 struct lttng_kernel_abi_old_tracer_version oldv
;
389 struct lttng_kernel_abi_old_tracer_version
*uversion
=
390 (struct lttng_kernel_abi_old_tracer_version __user
*) arg
;
392 lttng_abi_tracer_version(&v
);
393 oldv
.major
= v
.major
;
394 oldv
.minor
= v
.minor
;
395 oldv
.patchlevel
= v
.patchlevel
;
397 if (copy_to_user(uversion
, &oldv
, sizeof(oldv
)))
401 case LTTNG_KERNEL_ABI_TRACER_VERSION
:
403 struct lttng_kernel_abi_tracer_version version
;
404 struct lttng_kernel_abi_tracer_version
*uversion
=
405 (struct lttng_kernel_abi_tracer_version __user
*) arg
;
407 lttng_abi_tracer_version(&version
);
409 if (copy_to_user(uversion
, &version
, sizeof(version
)))
413 case LTTNG_KERNEL_ABI_TRACER_ABI_VERSION
:
415 struct lttng_kernel_abi_tracer_abi_version version
;
416 struct lttng_kernel_abi_tracer_abi_version
*uversion
=
417 (struct lttng_kernel_abi_tracer_abi_version __user
*) arg
;
419 lttng_abi_tracer_abi_version(&version
);
421 if (copy_to_user(uversion
, &version
, sizeof(version
)))
425 case LTTNG_KERNEL_ABI_OLD_TRACEPOINT_LIST
:
426 case LTTNG_KERNEL_ABI_TRACEPOINT_LIST
:
427 return lttng_abi_tracepoint_list();
428 case LTTNG_KERNEL_ABI_SYSCALL_LIST
:
429 return lttng_abi_syscall_list();
430 case LTTNG_KERNEL_ABI_OLD_WAIT_QUIESCENT
:
431 case LTTNG_KERNEL_ABI_WAIT_QUIESCENT
:
434 case LTTNG_KERNEL_ABI_OLD_CALIBRATE
:
436 struct lttng_kernel_abi_old_calibrate __user
*ucalibrate
=
437 (struct lttng_kernel_abi_old_calibrate __user
*) arg
;
438 struct lttng_kernel_abi_old_calibrate old_calibrate
;
439 struct lttng_kernel_abi_calibrate calibrate
;
442 if (copy_from_user(&old_calibrate
, ucalibrate
, sizeof(old_calibrate
)))
444 calibrate
.type
= old_calibrate
.type
;
445 ret
= lttng_calibrate(&calibrate
);
446 if (copy_to_user(ucalibrate
, &old_calibrate
, sizeof(old_calibrate
)))
450 case LTTNG_KERNEL_ABI_CALIBRATE
:
452 struct lttng_kernel_abi_calibrate __user
*ucalibrate
=
453 (struct lttng_kernel_abi_calibrate __user
*) arg
;
454 struct lttng_kernel_abi_calibrate calibrate
;
457 if (copy_from_user(&calibrate
, ucalibrate
, sizeof(calibrate
)))
459 ret
= lttng_calibrate(&calibrate
);
460 if (copy_to_user(ucalibrate
, &calibrate
, sizeof(calibrate
)))
469 #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(5,6,0))
470 static const struct proc_ops lttng_proc_ops
= {
471 .proc_ioctl
= lttng_ioctl
,
473 .proc_compat_ioctl
= lttng_ioctl
,
474 #endif /* CONFIG_COMPAT */
477 static const struct file_operations lttng_proc_ops
= {
478 .owner
= THIS_MODULE
,
479 .unlocked_ioctl
= lttng_ioctl
,
481 .compat_ioctl
= lttng_ioctl
,
482 #endif /* CONFIG_COMPAT */
487 int lttng_abi_create_channel(struct file
*session_file
,
488 struct lttng_kernel_abi_channel
*chan_param
,
489 enum channel_type channel_type
)
491 struct lttng_session
*session
= session_file
->private_data
;
492 const struct file_operations
*fops
= NULL
;
493 const char *transport_name
;
494 struct lttng_channel
*chan
;
495 struct file
*chan_file
;
499 chan_fd
= lttng_get_unused_fd();
504 switch (channel_type
) {
505 case PER_CPU_CHANNEL
:
506 fops
= <tng_channel_fops
;
508 case METADATA_CHANNEL
:
509 fops
= <tng_metadata_fops
;
513 chan_file
= anon_inode_getfile("[lttng_channel]",
516 if (IS_ERR(chan_file
)) {
517 ret
= PTR_ERR(chan_file
);
520 switch (channel_type
) {
521 case PER_CPU_CHANNEL
:
522 if (chan_param
->output
== LTTNG_KERNEL_ABI_SPLICE
) {
523 transport_name
= chan_param
->overwrite
?
524 "relay-overwrite" : "relay-discard";
525 } else if (chan_param
->output
== LTTNG_KERNEL_ABI_MMAP
) {
526 transport_name
= chan_param
->overwrite
?
527 "relay-overwrite-mmap" : "relay-discard-mmap";
532 case METADATA_CHANNEL
:
533 if (chan_param
->output
== LTTNG_KERNEL_ABI_SPLICE
)
534 transport_name
= "relay-metadata";
535 else if (chan_param
->output
== LTTNG_KERNEL_ABI_MMAP
)
536 transport_name
= "relay-metadata-mmap";
541 transport_name
= "<unknown>";
544 if (!atomic_long_add_unless(&session_file
->f_count
, 1, LONG_MAX
)) {
549 * We tolerate no failure path after channel creation. It will stay
550 * invariant for the rest of the session.
552 chan
= lttng_channel_create(session
, transport_name
, NULL
,
553 chan_param
->subbuf_size
,
554 chan_param
->num_subbuf
,
555 chan_param
->switch_timer_interval
,
556 chan_param
->read_timer_interval
,
562 chan
->file
= chan_file
;
563 chan_file
->private_data
= chan
;
564 fd_install(chan_fd
, chan_file
);
569 atomic_long_dec(&session_file
->f_count
);
573 put_unused_fd(chan_fd
);
579 int lttng_abi_session_set_name(struct lttng_session
*session
,
580 struct lttng_kernel_abi_session_name
*name
)
584 len
= strnlen(name
->name
, LTTNG_KERNEL_ABI_SESSION_NAME_LEN
);
586 if (len
== LTTNG_KERNEL_ABI_SESSION_NAME_LEN
) {
587 /* Name is too long/malformed */
591 strcpy(session
->name
, name
->name
);
596 int lttng_abi_session_set_creation_time(struct lttng_session
*session
,
597 struct lttng_kernel_abi_session_creation_time
*time
)
601 len
= strnlen(time
->iso8601
, LTTNG_KERNEL_ABI_SESSION_CREATION_TIME_ISO8601_LEN
);
603 if (len
== LTTNG_KERNEL_ABI_SESSION_CREATION_TIME_ISO8601_LEN
) {
604 /* Time is too long/malformed */
608 strcpy(session
->creation_time
, time
->iso8601
);
613 int lttng_counter_release(struct inode
*inode
, struct file
*file
)
615 struct lttng_counter
*counter
= file
->private_data
;
619 * Do not destroy the counter itself. Wait of the owner
620 * (event_notifier group) to be destroyed.
622 fput(counter
->owner
);
629 long lttng_counter_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
631 struct lttng_counter
*counter
= file
->private_data
;
632 size_t indexes
[LTTNG_KERNEL_ABI_COUNTER_DIMENSION_MAX
] = { 0 };
636 case LTTNG_KERNEL_ABI_COUNTER_READ
:
638 struct lttng_kernel_abi_counter_read local_counter_read
;
639 struct lttng_kernel_abi_counter_read __user
*ucounter_read
=
640 (struct lttng_kernel_abi_counter_read __user
*) arg
;
641 bool overflow
, underflow
;
646 if (copy_from_user(&local_counter_read
, ucounter_read
,
647 sizeof(local_counter_read
)))
649 if (validate_zeroed_padding(local_counter_read
.padding
,
650 sizeof(local_counter_read
.padding
)))
653 /* Cast all indexes into size_t. */
654 for (i
= 0; i
< local_counter_read
.index
.number_dimensions
; i
++)
655 indexes
[i
] = (size_t) local_counter_read
.index
.dimension_indexes
[i
];
656 cpu
= local_counter_read
.cpu
;
658 ret
= lttng_kernel_counter_read(counter
, indexes
, cpu
, &value
,
659 &overflow
, &underflow
);
662 local_counter_read
.value
.value
= value
;
663 local_counter_read
.value
.overflow
= overflow
;
664 local_counter_read
.value
.underflow
= underflow
;
666 if (copy_to_user(&ucounter_read
->value
, &local_counter_read
.value
,
667 sizeof(local_counter_read
.value
)))
672 case LTTNG_KERNEL_ABI_COUNTER_AGGREGATE
:
674 struct lttng_kernel_abi_counter_aggregate local_counter_aggregate
;
675 struct lttng_kernel_abi_counter_aggregate __user
*ucounter_aggregate
=
676 (struct lttng_kernel_abi_counter_aggregate __user
*) arg
;
677 bool overflow
, underflow
;
681 if (copy_from_user(&local_counter_aggregate
, ucounter_aggregate
,
682 sizeof(local_counter_aggregate
)))
684 if (validate_zeroed_padding(local_counter_aggregate
.padding
,
685 sizeof(local_counter_aggregate
.padding
)))
688 /* Cast all indexes into size_t. */
689 for (i
= 0; i
< local_counter_aggregate
.index
.number_dimensions
; i
++)
690 indexes
[i
] = (size_t) local_counter_aggregate
.index
.dimension_indexes
[i
];
692 ret
= lttng_kernel_counter_aggregate(counter
, indexes
, &value
,
693 &overflow
, &underflow
);
696 local_counter_aggregate
.value
.value
= value
;
697 local_counter_aggregate
.value
.overflow
= overflow
;
698 local_counter_aggregate
.value
.underflow
= underflow
;
700 if (copy_to_user(&ucounter_aggregate
->value
, &local_counter_aggregate
.value
,
701 sizeof(local_counter_aggregate
.value
)))
706 case LTTNG_KERNEL_ABI_COUNTER_CLEAR
:
708 struct lttng_kernel_abi_counter_clear local_counter_clear
;
709 struct lttng_kernel_abi_counter_clear __user
*ucounter_clear
=
710 (struct lttng_kernel_abi_counter_clear __user
*) arg
;
712 if (copy_from_user(&local_counter_clear
, ucounter_clear
,
713 sizeof(local_counter_clear
)))
715 if (validate_zeroed_padding(local_counter_clear
.padding
,
716 sizeof(local_counter_clear
.padding
)))
719 /* Cast all indexes into size_t. */
720 for (i
= 0; i
< local_counter_clear
.index
.number_dimensions
; i
++)
721 indexes
[i
] = (size_t) local_counter_clear
.index
.dimension_indexes
[i
];
723 return lttng_kernel_counter_clear(counter
, indexes
);
731 static const struct file_operations lttng_counter_fops
= {
732 .owner
= THIS_MODULE
,
733 .release
= lttng_counter_release
,
734 .unlocked_ioctl
= lttng_counter_ioctl
,
736 .compat_ioctl
= lttng_counter_ioctl
,
742 enum tracker_type
get_tracker_type(struct lttng_kernel_abi_tracker_args
*tracker
)
744 switch (tracker
->type
) {
745 case LTTNG_KERNEL_ABI_TRACKER_PID
:
747 case LTTNG_KERNEL_ABI_TRACKER_VPID
:
749 case LTTNG_KERNEL_ABI_TRACKER_UID
:
751 case LTTNG_KERNEL_ABI_TRACKER_VUID
:
753 case LTTNG_KERNEL_ABI_TRACKER_GID
:
755 case LTTNG_KERNEL_ABI_TRACKER_VGID
:
758 return TRACKER_UNKNOWN
;
763 * lttng_session_ioctl - lttng session fd ioctl
769 * This ioctl implements lttng commands:
770 * LTTNG_KERNEL_ABI_CHANNEL
771 * Returns a LTTng channel file descriptor
772 * LTTNG_KERNEL_ABI_ENABLE
773 * Enables tracing for a session (weak enable)
774 * LTTNG_KERNEL_ABI_DISABLE
775 * Disables tracing for a session (strong disable)
776 * LTTNG_KERNEL_ABI_METADATA
777 * Returns a LTTng metadata file descriptor
778 * LTTNG_KERNEL_ABI_SESSION_TRACK_PID
779 * Add PID to session PID tracker
780 * LTTNG_KERNEL_ABI_SESSION_UNTRACK_PID
781 * Remove PID from session PID tracker
782 * LTTNG_KERNEL_ABI_SESSION_TRACK_ID
784 * LTTNG_KERNEL_ABI_SESSION_UNTRACK_ID
785 * Remove ID from tracker
787 * The returned channel will be deleted when its file descriptor is closed.
790 long lttng_session_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
792 struct lttng_session
*session
= file
->private_data
;
793 struct lttng_kernel_abi_channel chan_param
;
794 struct lttng_kernel_abi_old_channel old_chan_param
;
797 case LTTNG_KERNEL_ABI_OLD_CHANNEL
:
799 if (copy_from_user(&old_chan_param
,
800 (struct lttng_kernel_abi_old_channel __user
*) arg
,
801 sizeof(struct lttng_kernel_abi_old_channel
)))
803 chan_param
.overwrite
= old_chan_param
.overwrite
;
804 chan_param
.subbuf_size
= old_chan_param
.subbuf_size
;
805 chan_param
.num_subbuf
= old_chan_param
.num_subbuf
;
806 chan_param
.switch_timer_interval
= old_chan_param
.switch_timer_interval
;
807 chan_param
.read_timer_interval
= old_chan_param
.read_timer_interval
;
808 chan_param
.output
= old_chan_param
.output
;
810 return lttng_abi_create_channel(file
, &chan_param
,
813 case LTTNG_KERNEL_ABI_CHANNEL
:
815 if (copy_from_user(&chan_param
,
816 (struct lttng_kernel_abi_channel __user
*) arg
,
817 sizeof(struct lttng_kernel_abi_channel
)))
819 return lttng_abi_create_channel(file
, &chan_param
,
822 case LTTNG_KERNEL_ABI_OLD_SESSION_START
:
823 case LTTNG_KERNEL_ABI_OLD_ENABLE
:
824 case LTTNG_KERNEL_ABI_SESSION_START
:
825 case LTTNG_KERNEL_ABI_ENABLE
:
826 return lttng_session_enable(session
);
827 case LTTNG_KERNEL_ABI_OLD_SESSION_STOP
:
828 case LTTNG_KERNEL_ABI_OLD_DISABLE
:
829 case LTTNG_KERNEL_ABI_SESSION_STOP
:
830 case LTTNG_KERNEL_ABI_DISABLE
:
831 return lttng_session_disable(session
);
832 case LTTNG_KERNEL_ABI_OLD_METADATA
:
834 if (copy_from_user(&old_chan_param
,
835 (struct lttng_kernel_abi_old_channel __user
*) arg
,
836 sizeof(struct lttng_kernel_abi_old_channel
)))
838 chan_param
.overwrite
= old_chan_param
.overwrite
;
839 chan_param
.subbuf_size
= old_chan_param
.subbuf_size
;
840 chan_param
.num_subbuf
= old_chan_param
.num_subbuf
;
841 chan_param
.switch_timer_interval
= old_chan_param
.switch_timer_interval
;
842 chan_param
.read_timer_interval
= old_chan_param
.read_timer_interval
;
843 chan_param
.output
= old_chan_param
.output
;
845 return lttng_abi_create_channel(file
, &chan_param
,
848 case LTTNG_KERNEL_ABI_METADATA
:
850 if (copy_from_user(&chan_param
,
851 (struct lttng_kernel_abi_channel __user
*) arg
,
852 sizeof(struct lttng_kernel_abi_channel
)))
854 return lttng_abi_create_channel(file
, &chan_param
,
857 case LTTNG_KERNEL_ABI_SESSION_TRACK_PID
:
858 return lttng_session_track_id(session
, TRACKER_PID
, (int) arg
);
859 case LTTNG_KERNEL_ABI_SESSION_UNTRACK_PID
:
860 return lttng_session_untrack_id(session
, TRACKER_PID
, (int) arg
);
861 case LTTNG_KERNEL_ABI_SESSION_TRACK_ID
:
863 struct lttng_kernel_abi_tracker_args tracker
;
864 enum tracker_type tracker_type
;
866 if (copy_from_user(&tracker
,
867 (struct lttng_kernel_abi_tracker_args __user
*) arg
,
868 sizeof(struct lttng_kernel_abi_tracker_args
)))
870 tracker_type
= get_tracker_type(&tracker
);
871 if (tracker_type
== TRACKER_UNKNOWN
)
873 return lttng_session_track_id(session
, tracker_type
, tracker
.id
);
875 case LTTNG_KERNEL_ABI_SESSION_UNTRACK_ID
:
877 struct lttng_kernel_abi_tracker_args tracker
;
878 enum tracker_type tracker_type
;
880 if (copy_from_user(&tracker
,
881 (struct lttng_kernel_abi_tracker_args __user
*) arg
,
882 sizeof(struct lttng_kernel_abi_tracker_args
)))
884 tracker_type
= get_tracker_type(&tracker
);
885 if (tracker_type
== TRACKER_UNKNOWN
)
887 return lttng_session_untrack_id(session
, tracker_type
,
890 case LTTNG_KERNEL_ABI_SESSION_LIST_TRACKER_PIDS
:
891 return lttng_session_list_tracker_ids(session
, TRACKER_PID
);
892 case LTTNG_KERNEL_ABI_SESSION_LIST_TRACKER_IDS
:
894 struct lttng_kernel_abi_tracker_args tracker
;
895 enum tracker_type tracker_type
;
897 if (copy_from_user(&tracker
,
898 (struct lttng_kernel_abi_tracker_args __user
*) arg
,
899 sizeof(struct lttng_kernel_abi_tracker_args
)))
901 tracker_type
= get_tracker_type(&tracker
);
902 if (tracker_type
== TRACKER_UNKNOWN
)
904 return lttng_session_list_tracker_ids(session
, tracker_type
);
906 case LTTNG_KERNEL_ABI_SESSION_METADATA_REGEN
:
907 return lttng_session_metadata_regenerate(session
);
908 case LTTNG_KERNEL_ABI_SESSION_STATEDUMP
:
909 return lttng_session_statedump(session
);
910 case LTTNG_KERNEL_ABI_SESSION_SET_NAME
:
912 struct lttng_kernel_abi_session_name name
;
914 if (copy_from_user(&name
,
915 (struct lttng_kernel_abi_session_name __user
*) arg
,
916 sizeof(struct lttng_kernel_abi_session_name
)))
918 return lttng_abi_session_set_name(session
, &name
);
920 case LTTNG_KERNEL_ABI_SESSION_SET_CREATION_TIME
:
922 struct lttng_kernel_abi_session_creation_time time
;
924 if (copy_from_user(&time
,
925 (struct lttng_kernel_abi_session_creation_time __user
*) arg
,
926 sizeof(struct lttng_kernel_abi_session_creation_time
)))
928 return lttng_abi_session_set_creation_time(session
, &time
);
936 * Called when the last file reference is dropped.
938 * Big fat note: channels and events are invariant for the whole session after
939 * their creation. So this session destruction also destroys all channel and
940 * event structures specific to this session (they are not destroyed when their
941 * individual file is released).
944 int lttng_session_release(struct inode
*inode
, struct file
*file
)
946 struct lttng_session
*session
= file
->private_data
;
949 lttng_session_destroy(session
);
953 static const struct file_operations lttng_session_fops
= {
954 .owner
= THIS_MODULE
,
955 .release
= lttng_session_release
,
956 .unlocked_ioctl
= lttng_session_ioctl
,
958 .compat_ioctl
= lttng_session_ioctl
,
963 * When encountering empty buffer, flush current sub-buffer if non-empty
964 * and retry (if new data available to read after flush).
967 ssize_t
lttng_event_notifier_group_notif_read(struct file
*filp
, char __user
*user_buf
,
968 size_t count
, loff_t
*ppos
)
970 struct lttng_event_notifier_group
*event_notifier_group
= filp
->private_data
;
971 struct channel
*chan
= event_notifier_group
->chan
;
972 struct lib_ring_buffer
*buf
= event_notifier_group
->buf
;
973 ssize_t read_count
= 0, len
;
977 if (!lttng_access_ok(VERIFY_WRITE
, user_buf
, count
))
980 /* Finish copy of previous record */
982 if (read_count
< count
) {
983 len
= chan
->iter
.len_left
;
989 while (read_count
< count
) {
990 size_t copy_len
, space_left
;
992 len
= lib_ring_buffer_get_next_record(chan
, buf
);
996 * Check if buffer is finalized (end of file).
998 if (len
== -ENODATA
) {
999 /* A 0 read_count will tell about end of file */
1002 if (filp
->f_flags
& O_NONBLOCK
) {
1004 read_count
= -EAGAIN
;
1010 * No data available at the moment, return what
1017 * Wait for returned len to be >= 0 or -ENODATA.
1019 error
= wait_event_interruptible(
1020 event_notifier_group
->read_wait
,
1021 ((len
= lib_ring_buffer_get_next_record(
1022 chan
, buf
)), len
!= -EAGAIN
));
1023 CHAN_WARN_ON(chan
, len
== -EBUSY
);
1028 CHAN_WARN_ON(chan
, len
< 0 && len
!= -ENODATA
);
1032 read_offset
= buf
->iter
.read_offset
;
1034 space_left
= count
- read_count
;
1035 if (len
<= space_left
) {
1037 chan
->iter
.len_left
= 0;
1040 copy_len
= space_left
;
1041 chan
->iter
.len_left
= len
- copy_len
;
1042 *ppos
= read_offset
+ copy_len
;
1044 if (__lib_ring_buffer_copy_to_user(&buf
->backend
, read_offset
,
1045 &user_buf
[read_count
],
1048 * Leave the len_left and ppos values at their current
1049 * state, as we currently have a valid event to read.
1053 read_count
+= copy_len
;
1059 chan
->iter
.len_left
= 0;
1062 lib_ring_buffer_put_current_record(buf
);
1067 * If the ring buffer is non empty (even just a partial subbuffer), return that
1068 * there is data available. Perform a ring buffer flush if we encounter a
1069 * non-empty ring buffer which does not have any consumeable subbuffer available.
1072 unsigned int lttng_event_notifier_group_notif_poll(struct file
*filp
,
1075 unsigned int mask
= 0;
1076 struct lttng_event_notifier_group
*event_notifier_group
= filp
->private_data
;
1077 struct channel
*chan
= event_notifier_group
->chan
;
1078 struct lib_ring_buffer
*buf
= event_notifier_group
->buf
;
1079 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1080 int finalized
, disabled
;
1081 unsigned long consumed
, offset
;
1082 size_t subbuffer_header_size
= config
->cb
.subbuffer_header_size();
1084 if (filp
->f_mode
& FMODE_READ
) {
1085 poll_wait_set_exclusive(wait
);
1086 poll_wait(filp
, &event_notifier_group
->read_wait
, wait
);
1088 finalized
= lib_ring_buffer_is_finalized(config
, buf
);
1089 disabled
= lib_ring_buffer_channel_is_disabled(chan
);
1092 * lib_ring_buffer_is_finalized() contains a smp_rmb() ordering
1093 * finalized load before offsets loads.
1095 WARN_ON(atomic_long_read(&buf
->active_readers
) != 1);
1100 offset
= lib_ring_buffer_get_offset(config
, buf
);
1101 consumed
= lib_ring_buffer_get_consumed(config
, buf
);
1104 * If there is no buffer available to consume.
1106 if (subbuf_trunc(offset
, chan
) - subbuf_trunc(consumed
, chan
) == 0) {
1108 * If there is a non-empty subbuffer, flush and try again.
1110 if (subbuf_offset(offset
, chan
) > subbuffer_header_size
) {
1111 lib_ring_buffer_switch_remote(buf
);
1119 * The memory barriers
1120 * __wait_event()/wake_up_interruptible() take
1121 * care of "raw_spin_is_locked" memory ordering.
1123 if (raw_spin_is_locked(&buf
->raw_tick_nohz_spinlock
))
1129 if (subbuf_trunc(offset
, chan
) - subbuf_trunc(consumed
, chan
)
1130 >= chan
->backend
.buf_size
)
1131 return POLLPRI
| POLLRDBAND
;
1133 return POLLIN
| POLLRDNORM
;
1141 * lttng_event_notifier_group_notif_open - event_notifier ring buffer open file operation
1142 * @inode: opened inode
1143 * @file: opened file
1145 * Open implementation. Makes sure only one open instance of a buffer is
1146 * done at a given moment.
1148 static int lttng_event_notifier_group_notif_open(struct inode
*inode
, struct file
*file
)
1150 struct lttng_event_notifier_group
*event_notifier_group
= inode
->i_private
;
1151 struct lib_ring_buffer
*buf
= event_notifier_group
->buf
;
1153 file
->private_data
= event_notifier_group
;
1154 return lib_ring_buffer_open(inode
, file
, buf
);
1158 * lttng_event_notifier_group_notif_release - event_notifier ring buffer release file operation
1159 * @inode: opened inode
1160 * @file: opened file
1162 * Release implementation.
1164 static int lttng_event_notifier_group_notif_release(struct inode
*inode
, struct file
*file
)
1166 struct lttng_event_notifier_group
*event_notifier_group
= file
->private_data
;
1167 struct lib_ring_buffer
*buf
= event_notifier_group
->buf
;
1170 ret
= lib_ring_buffer_release(inode
, file
, buf
);
1173 fput(event_notifier_group
->file
);
1177 static const struct file_operations lttng_event_notifier_group_notif_fops
= {
1178 .owner
= THIS_MODULE
,
1179 .open
= lttng_event_notifier_group_notif_open
,
1180 .release
= lttng_event_notifier_group_notif_release
,
1181 .read
= lttng_event_notifier_group_notif_read
,
1182 .poll
= lttng_event_notifier_group_notif_poll
,
1186 * lttng_metadata_ring_buffer_poll - LTTng ring buffer poll file operation
1190 * Handles the poll operations for the metadata channels.
1193 unsigned int lttng_metadata_ring_buffer_poll(struct file
*filp
,
1196 struct lttng_metadata_stream
*stream
= filp
->private_data
;
1197 struct lib_ring_buffer
*buf
= stream
->priv
;
1199 unsigned int mask
= 0;
1201 if (filp
->f_mode
& FMODE_READ
) {
1202 poll_wait_set_exclusive(wait
);
1203 poll_wait(filp
, &stream
->read_wait
, wait
);
1205 finalized
= stream
->finalized
;
1208 * lib_ring_buffer_is_finalized() contains a smp_rmb()
1209 * ordering finalized load before offsets loads.
1211 WARN_ON(atomic_long_read(&buf
->active_readers
) != 1);
1216 mutex_lock(&stream
->metadata_cache
->lock
);
1217 if (stream
->metadata_cache
->metadata_written
>
1218 stream
->metadata_out
)
1220 mutex_unlock(&stream
->metadata_cache
->lock
);
1227 void lttng_metadata_ring_buffer_ioctl_put_next_subbuf(struct file
*filp
,
1228 unsigned int cmd
, unsigned long arg
)
1230 struct lttng_metadata_stream
*stream
= filp
->private_data
;
1232 stream
->metadata_out
= stream
->metadata_in
;
1236 * Reset the counter of how much metadata has been consumed to 0. That way,
1237 * the consumer receives the content of the metadata cache unchanged. This is
1238 * different from the metadata_regenerate where the offset from epoch is
1239 * resampled, here we want the exact same content as the last time the metadata
1240 * was generated. This command is only possible if all the metadata written
1241 * in the cache has been output to the metadata stream to avoid corrupting the
1244 * Return 0 on success, a negative value on error.
1247 int lttng_metadata_cache_dump(struct lttng_metadata_stream
*stream
)
1250 struct lttng_metadata_cache
*cache
= stream
->metadata_cache
;
1252 mutex_lock(&cache
->lock
);
1253 if (stream
->metadata_out
!= cache
->metadata_written
) {
1257 stream
->metadata_out
= 0;
1258 stream
->metadata_in
= 0;
1259 wake_up_interruptible(&stream
->read_wait
);
1263 mutex_unlock(&cache
->lock
);
1268 long lttng_metadata_ring_buffer_ioctl(struct file
*filp
,
1269 unsigned int cmd
, unsigned long arg
)
1272 struct lttng_metadata_stream
*stream
= filp
->private_data
;
1273 struct lib_ring_buffer
*buf
= stream
->priv
;
1274 unsigned int rb_cmd
;
1277 if (cmd
== LTTNG_KERNEL_ABI_RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK
)
1278 rb_cmd
= LTTNG_KERNEL_ABI_RING_BUFFER_GET_NEXT_SUBBUF
;
1283 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_NEXT_SUBBUF
:
1285 struct lttng_metadata_stream
*stream
= filp
->private_data
;
1286 struct lib_ring_buffer
*buf
= stream
->priv
;
1287 struct channel
*chan
= buf
->backend
.chan
;
1289 ret
= lttng_metadata_output_channel(stream
, chan
, NULL
);
1291 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
1297 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_SUBBUF
:
1300 * Random access is not allowed for metadata channel.
1304 case LTTNG_KERNEL_ABI_RING_BUFFER_FLUSH_EMPTY
: /* Fall-through. */
1305 case LTTNG_KERNEL_ABI_RING_BUFFER_FLUSH
:
1307 struct lttng_metadata_stream
*stream
= filp
->private_data
;
1308 struct lib_ring_buffer
*buf
= stream
->priv
;
1309 struct channel
*chan
= buf
->backend
.chan
;
1312 * Before doing the actual ring buffer flush, write up to one
1313 * packet of metadata in the ring buffer.
1315 ret
= lttng_metadata_output_channel(stream
, chan
, NULL
);
1320 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_METADATA_VERSION
:
1322 struct lttng_metadata_stream
*stream
= filp
->private_data
;
1324 return put_u64(stream
->version
, arg
);
1326 case LTTNG_KERNEL_ABI_RING_BUFFER_METADATA_CACHE_DUMP
:
1328 struct lttng_metadata_stream
*stream
= filp
->private_data
;
1330 return lttng_metadata_cache_dump(stream
);
1332 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK
:
1334 struct lttng_metadata_stream
*stream
= filp
->private_data
;
1335 struct lib_ring_buffer
*buf
= stream
->priv
;
1336 struct channel
*chan
= buf
->backend
.chan
;
1338 ret
= lttng_metadata_output_channel(stream
, chan
, &coherent
);
1340 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
1342 } else if (ret
< 0) {
1350 /* PUT_SUBBUF is the one from lib ring buffer, unmodified. */
1352 /* Performing lib ring buffer ioctl after our own. */
1353 ret
= lib_ring_buffer_ioctl(filp
, rb_cmd
, arg
, buf
);
1358 case LTTNG_KERNEL_ABI_RING_BUFFER_PUT_NEXT_SUBBUF
:
1360 lttng_metadata_ring_buffer_ioctl_put_next_subbuf(filp
,
1364 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK
:
1366 return put_u32(coherent
, arg
);
1375 #ifdef CONFIG_COMPAT
1377 long lttng_metadata_ring_buffer_compat_ioctl(struct file
*filp
,
1378 unsigned int cmd
, unsigned long arg
)
1381 struct lttng_metadata_stream
*stream
= filp
->private_data
;
1382 struct lib_ring_buffer
*buf
= stream
->priv
;
1383 unsigned int rb_cmd
;
1386 if (cmd
== LTTNG_KERNEL_ABI_RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK
)
1387 rb_cmd
= LTTNG_KERNEL_ABI_RING_BUFFER_GET_NEXT_SUBBUF
;
1392 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_NEXT_SUBBUF
:
1394 struct lttng_metadata_stream
*stream
= filp
->private_data
;
1395 struct lib_ring_buffer
*buf
= stream
->priv
;
1396 struct channel
*chan
= buf
->backend
.chan
;
1398 ret
= lttng_metadata_output_channel(stream
, chan
, NULL
);
1400 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
1406 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_SUBBUF
:
1409 * Random access is not allowed for metadata channel.
1413 case LTTNG_KERNEL_ABI_RING_BUFFER_FLUSH_EMPTY
: /* Fall-through. */
1414 case LTTNG_KERNEL_ABI_RING_BUFFER_FLUSH
:
1416 struct lttng_metadata_stream
*stream
= filp
->private_data
;
1417 struct lib_ring_buffer
*buf
= stream
->priv
;
1418 struct channel
*chan
= buf
->backend
.chan
;
1421 * Before doing the actual ring buffer flush, write up to one
1422 * packet of metadata in the ring buffer.
1424 ret
= lttng_metadata_output_channel(stream
, chan
, NULL
);
1429 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_METADATA_VERSION
:
1431 struct lttng_metadata_stream
*stream
= filp
->private_data
;
1433 return put_u64(stream
->version
, arg
);
1435 case LTTNG_KERNEL_ABI_RING_BUFFER_METADATA_CACHE_DUMP
:
1437 struct lttng_metadata_stream
*stream
= filp
->private_data
;
1439 return lttng_metadata_cache_dump(stream
);
1441 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK
:
1443 struct lttng_metadata_stream
*stream
= filp
->private_data
;
1444 struct lib_ring_buffer
*buf
= stream
->priv
;
1445 struct channel
*chan
= buf
->backend
.chan
;
1447 ret
= lttng_metadata_output_channel(stream
, chan
, &coherent
);
1449 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
1451 } else if (ret
< 0) {
1459 /* PUT_SUBBUF is the one from lib ring buffer, unmodified. */
1461 /* Performing lib ring buffer ioctl after our own. */
1462 ret
= lib_ring_buffer_compat_ioctl(filp
, rb_cmd
, arg
, buf
);
1467 case LTTNG_KERNEL_ABI_RING_BUFFER_PUT_NEXT_SUBBUF
:
1469 lttng_metadata_ring_buffer_ioctl_put_next_subbuf(filp
,
1473 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK
:
1475 return put_u32(coherent
, arg
);
1486 * This is not used by anonymous file descriptors. This code is left
1487 * there if we ever want to implement an inode with open() operation.
1490 int lttng_metadata_ring_buffer_open(struct inode
*inode
, struct file
*file
)
1492 struct lttng_metadata_stream
*stream
= inode
->i_private
;
1493 struct lib_ring_buffer
*buf
= stream
->priv
;
1495 file
->private_data
= buf
;
1497 * Since life-time of metadata cache differs from that of
1498 * session, we need to keep our own reference on the transport.
1500 if (!try_module_get(stream
->transport
->owner
)) {
1501 printk(KERN_WARNING
"LTTng: Can't lock transport module.\n");
1504 return lib_ring_buffer_open(inode
, file
, buf
);
1508 int lttng_metadata_ring_buffer_release(struct inode
*inode
, struct file
*file
)
1510 struct lttng_metadata_stream
*stream
= file
->private_data
;
1511 struct lib_ring_buffer
*buf
= stream
->priv
;
1513 mutex_lock(&stream
->metadata_cache
->lock
);
1514 list_del(&stream
->list
);
1515 mutex_unlock(&stream
->metadata_cache
->lock
);
1516 kref_put(&stream
->metadata_cache
->refcount
, metadata_cache_destroy
);
1517 module_put(stream
->transport
->owner
);
1519 return lib_ring_buffer_release(inode
, file
, buf
);
1523 ssize_t
lttng_metadata_ring_buffer_splice_read(struct file
*in
, loff_t
*ppos
,
1524 struct pipe_inode_info
*pipe
, size_t len
,
1527 struct lttng_metadata_stream
*stream
= in
->private_data
;
1528 struct lib_ring_buffer
*buf
= stream
->priv
;
1530 return lib_ring_buffer_splice_read(in
, ppos
, pipe
, len
,
1535 int lttng_metadata_ring_buffer_mmap(struct file
*filp
,
1536 struct vm_area_struct
*vma
)
1538 struct lttng_metadata_stream
*stream
= filp
->private_data
;
1539 struct lib_ring_buffer
*buf
= stream
->priv
;
1541 return lib_ring_buffer_mmap(filp
, vma
, buf
);
1545 const struct file_operations lttng_metadata_ring_buffer_file_operations
= {
1546 .owner
= THIS_MODULE
,
1547 .open
= lttng_metadata_ring_buffer_open
,
1548 .release
= lttng_metadata_ring_buffer_release
,
1549 .poll
= lttng_metadata_ring_buffer_poll
,
1550 .splice_read
= lttng_metadata_ring_buffer_splice_read
,
1551 .mmap
= lttng_metadata_ring_buffer_mmap
,
1552 .unlocked_ioctl
= lttng_metadata_ring_buffer_ioctl
,
1553 .llseek
= vfs_lib_ring_buffer_no_llseek
,
1554 #ifdef CONFIG_COMPAT
1555 .compat_ioctl
= lttng_metadata_ring_buffer_compat_ioctl
,
1560 int lttng_abi_create_stream_fd(struct file
*channel_file
, void *stream_priv
,
1561 const struct file_operations
*fops
, const char *name
)
1564 struct file
*stream_file
;
1566 stream_fd
= lttng_get_unused_fd();
1567 if (stream_fd
< 0) {
1571 stream_file
= anon_inode_getfile(name
, fops
, stream_priv
, O_RDWR
);
1572 if (IS_ERR(stream_file
)) {
1573 ret
= PTR_ERR(stream_file
);
1577 * OPEN_FMODE, called within anon_inode_getfile/alloc_file, don't honor
1578 * FMODE_LSEEK, FMODE_PREAD nor FMODE_PWRITE. We need to read from this
1579 * file descriptor, so we set FMODE_PREAD here.
1581 stream_file
->f_mode
|= FMODE_PREAD
;
1582 fd_install(stream_fd
, stream_file
);
1584 * The stream holds a reference to the channel within the generic ring
1585 * buffer library, so no need to hold a refcount on the channel and
1586 * session files here.
1591 put_unused_fd(stream_fd
);
1597 int lttng_abi_open_stream(struct file
*channel_file
)
1599 struct lttng_channel
*channel
= channel_file
->private_data
;
1600 struct lib_ring_buffer
*buf
;
1604 buf
= channel
->ops
->buffer_read_open(channel
->chan
);
1609 ret
= lttng_abi_create_stream_fd(channel_file
, stream_priv
,
1610 <tng_stream_ring_buffer_file_operations
,
1618 channel
->ops
->buffer_read_close(buf
);
1623 int lttng_abi_open_metadata_stream(struct file
*channel_file
)
1625 struct lttng_channel
*channel
= channel_file
->private_data
;
1626 struct lttng_session
*session
= channel
->session
;
1627 struct lib_ring_buffer
*buf
;
1629 struct lttng_metadata_stream
*metadata_stream
;
1632 buf
= channel
->ops
->buffer_read_open(channel
->chan
);
1636 metadata_stream
= kzalloc(sizeof(struct lttng_metadata_stream
),
1638 if (!metadata_stream
) {
1642 metadata_stream
->metadata_cache
= session
->metadata_cache
;
1643 init_waitqueue_head(&metadata_stream
->read_wait
);
1644 metadata_stream
->priv
= buf
;
1645 stream_priv
= metadata_stream
;
1646 metadata_stream
->transport
= channel
->transport
;
1647 /* Initial state is an empty metadata, considered as incoherent. */
1648 metadata_stream
->coherent
= false;
1651 * Since life-time of metadata cache differs from that of
1652 * session, we need to keep our own reference on the transport.
1654 if (!try_module_get(metadata_stream
->transport
->owner
)) {
1655 printk(KERN_WARNING
"LTTng: Can't lock transport module.\n");
1660 if (!lttng_kref_get(&session
->metadata_cache
->refcount
)) {
1665 ret
= lttng_abi_create_stream_fd(channel_file
, stream_priv
,
1666 <tng_metadata_ring_buffer_file_operations
,
1667 "[lttng_metadata_stream]");
1671 mutex_lock(&session
->metadata_cache
->lock
);
1672 list_add(&metadata_stream
->list
,
1673 &session
->metadata_cache
->metadata_stream
);
1674 mutex_unlock(&session
->metadata_cache
->lock
);
1678 kref_put(&session
->metadata_cache
->refcount
, metadata_cache_destroy
);
1680 module_put(metadata_stream
->transport
->owner
);
1682 kfree(metadata_stream
);
1684 channel
->ops
->buffer_read_close(buf
);
1689 int lttng_abi_open_event_notifier_group_stream(struct file
*notif_file
)
1691 struct lttng_event_notifier_group
*event_notifier_group
= notif_file
->private_data
;
1692 struct channel
*chan
= event_notifier_group
->chan
;
1693 struct lib_ring_buffer
*buf
;
1697 buf
= event_notifier_group
->ops
->buffer_read_open(chan
);
1701 /* The event_notifier notification fd holds a reference on the event_notifier group */
1702 if (!atomic_long_add_unless(¬if_file
->f_count
, 1, LONG_MAX
)) {
1704 goto refcount_error
;
1706 event_notifier_group
->buf
= buf
;
1707 stream_priv
= event_notifier_group
;
1708 ret
= lttng_abi_create_stream_fd(notif_file
, stream_priv
,
1709 <tng_event_notifier_group_notif_fops
,
1710 "[lttng_event_notifier_stream]");
1717 atomic_long_dec(¬if_file
->f_count
);
1719 event_notifier_group
->ops
->buffer_read_close(buf
);
1724 int lttng_abi_validate_event_param(struct lttng_kernel_abi_event
*event_param
)
1726 /* Limit ABI to implemented features. */
1727 switch (event_param
->instrumentation
) {
1728 case LTTNG_KERNEL_ABI_SYSCALL
:
1729 switch (event_param
->u
.syscall
.entryexit
) {
1730 case LTTNG_KERNEL_ABI_SYSCALL_ENTRY
: /* Fall-through */
1731 case LTTNG_KERNEL_ABI_SYSCALL_EXIT
: /* Fall-through */
1732 case LTTNG_KERNEL_ABI_SYSCALL_ENTRYEXIT
:
1737 switch (event_param
->u
.syscall
.abi
) {
1738 case LTTNG_KERNEL_ABI_SYSCALL_ABI_ALL
:
1743 switch (event_param
->u
.syscall
.match
) {
1744 case LTTNG_KERNEL_ABI_SYSCALL_MATCH_NAME
:
1751 case LTTNG_KERNEL_ABI_KRETPROBE
:
1752 switch (event_param
->u
.kretprobe
.entryexit
) {
1753 case LTTNG_KERNEL_ABI_SYSCALL_ENTRYEXIT
:
1755 case LTTNG_KERNEL_ABI_SYSCALL_ENTRY
: /* Fall-through */
1756 case LTTNG_KERNEL_ABI_SYSCALL_EXIT
: /* Fall-through */
1762 case LTTNG_KERNEL_ABI_TRACEPOINT
: /* Fall-through */
1763 case LTTNG_KERNEL_ABI_KPROBE
: /* Fall-through */
1764 case LTTNG_KERNEL_ABI_UPROBE
:
1767 case LTTNG_KERNEL_ABI_FUNCTION
: /* Fall-through */
1768 case LTTNG_KERNEL_ABI_NOOP
: /* Fall-through */
1776 int lttng_abi_create_event(struct file
*channel_file
,
1777 struct lttng_kernel_abi_event
*event_param
)
1779 const struct file_operations
*fops
;
1780 struct lttng_channel
*channel
= channel_file
->private_data
;
1782 struct file
*event_file
;
1785 event_param
->name
[LTTNG_KERNEL_ABI_SYM_NAME_LEN
- 1] = '\0';
1786 switch (event_param
->instrumentation
) {
1787 case LTTNG_KERNEL_ABI_KRETPROBE
:
1788 event_param
->u
.kretprobe
.symbol_name
[LTTNG_KERNEL_ABI_SYM_NAME_LEN
- 1] = '\0';
1790 case LTTNG_KERNEL_ABI_KPROBE
:
1791 event_param
->u
.kprobe
.symbol_name
[LTTNG_KERNEL_ABI_SYM_NAME_LEN
- 1] = '\0';
1793 case LTTNG_KERNEL_ABI_FUNCTION
:
1795 /* Not implemented. */
1801 switch (event_param
->instrumentation
) {
1802 case LTTNG_KERNEL_ABI_TRACEPOINT
: /* Fall-through */
1803 case LTTNG_KERNEL_ABI_SYSCALL
:
1804 fops
= <tng_event_recorder_enabler_fops
;
1806 case LTTNG_KERNEL_ABI_KPROBE
: /* Fall-through */
1807 case LTTNG_KERNEL_ABI_KRETPROBE
: /* Fall-through */
1808 case LTTNG_KERNEL_ABI_UPROBE
:
1809 fops
= <tng_event_recorder_event_fops
;
1812 case LTTNG_KERNEL_ABI_FUNCTION
: /* Fall-through */
1813 case LTTNG_KERNEL_ABI_NOOP
: /* Fall-through */
1818 event_fd
= lttng_get_unused_fd();
1823 event_file
= anon_inode_getfile("[lttng_event]",
1824 fops
, NULL
, O_RDWR
);
1825 if (IS_ERR(event_file
)) {
1826 ret
= PTR_ERR(event_file
);
1829 /* The event holds a reference on the channel */
1830 if (!atomic_long_add_unless(&channel_file
->f_count
, 1, LONG_MAX
)) {
1832 goto refcount_error
;
1834 ret
= lttng_abi_validate_event_param(event_param
);
1838 switch (event_param
->instrumentation
) {
1839 case LTTNG_KERNEL_ABI_TRACEPOINT
: /* Fall-through */
1840 case LTTNG_KERNEL_ABI_SYSCALL
:
1842 struct lttng_event_enabler
*event_enabler
;
1844 if (strutils_is_star_glob_pattern(event_param
->name
)) {
1846 * If the event name is a star globbing pattern,
1847 * we create the special star globbing enabler.
1849 event_enabler
= lttng_event_enabler_create(LTTNG_ENABLER_FORMAT_STAR_GLOB
,
1850 event_param
, channel
);
1852 event_enabler
= lttng_event_enabler_create(LTTNG_ENABLER_FORMAT_NAME
,
1853 event_param
, channel
);
1855 priv
= event_enabler
;
1859 case LTTNG_KERNEL_ABI_KPROBE
: /* Fall-through */
1860 case LTTNG_KERNEL_ABI_KRETPROBE
: /* Fall-through */
1861 case LTTNG_KERNEL_ABI_UPROBE
:
1863 struct lttng_event
*event
;
1866 * We tolerate no failure path after event creation. It
1867 * will stay invariant for the rest of the session.
1869 event
= lttng_event_create(channel
, event_param
,
1871 event_param
->instrumentation
);
1872 WARN_ON_ONCE(!event
);
1873 if (IS_ERR(event
)) {
1874 ret
= PTR_ERR(event
);
1881 case LTTNG_KERNEL_ABI_FUNCTION
: /* Fall-through */
1882 case LTTNG_KERNEL_ABI_NOOP
: /* Fall-through */
1887 event_file
->private_data
= priv
;
1888 fd_install(event_fd
, event_file
);
1892 atomic_long_dec(&channel_file
->f_count
);
1896 put_unused_fd(event_fd
);
1902 long lttng_event_notifier_event_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1904 struct lttng_event_notifier
*event_notifier
= file
->private_data
;
1907 case LTTNG_KERNEL_ABI_ENABLE
:
1908 return lttng_event_notifier_enable(event_notifier
);
1909 case LTTNG_KERNEL_ABI_DISABLE
:
1910 return lttng_event_notifier_disable(event_notifier
);
1911 case LTTNG_KERNEL_ABI_FILTER
:
1913 case LTTNG_KERNEL_ABI_CAPTURE
:
1915 case LTTNG_KERNEL_ABI_ADD_CALLSITE
:
1916 return lttng_event_notifier_add_callsite(event_notifier
,
1917 (struct lttng_kernel_abi_event_callsite __user
*) arg
);
1919 return -ENOIOCTLCMD
;
1924 long lttng_event_notifier_enabler_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1926 struct lttng_event_notifier_enabler
*event_notifier_enabler
= file
->private_data
;
1929 case LTTNG_KERNEL_ABI_ENABLE
:
1930 return lttng_event_notifier_enabler_enable(event_notifier_enabler
);
1931 case LTTNG_KERNEL_ABI_DISABLE
:
1932 return lttng_event_notifier_enabler_disable(event_notifier_enabler
);
1933 case LTTNG_KERNEL_ABI_FILTER
:
1934 return lttng_event_notifier_enabler_attach_filter_bytecode(
1935 event_notifier_enabler
,
1936 (struct lttng_kernel_abi_filter_bytecode __user
*) arg
);
1937 case LTTNG_KERNEL_ABI_CAPTURE
:
1938 return lttng_event_notifier_enabler_attach_capture_bytecode(
1939 event_notifier_enabler
,
1940 (struct lttng_kernel_abi_capture_bytecode __user
*) arg
);
1941 case LTTNG_KERNEL_ABI_ADD_CALLSITE
:
1944 return -ENOIOCTLCMD
;
1949 int lttng_event_notifier_event_release(struct inode
*inode
, struct file
*file
)
1951 struct lttng_event_notifier
*event_notifier
= file
->private_data
;
1954 fput(event_notifier
->group
->file
);
1959 int lttng_event_notifier_enabler_release(struct inode
*inode
, struct file
*file
)
1961 struct lttng_event_notifier_enabler
*event_notifier_enabler
= file
->private_data
;
1963 if (event_notifier_enabler
)
1964 fput(event_notifier_enabler
->group
->file
);
1968 static const struct file_operations lttng_event_notifier_event_fops
= {
1969 .owner
= THIS_MODULE
,
1970 .release
= lttng_event_notifier_event_release
,
1971 .unlocked_ioctl
= lttng_event_notifier_event_ioctl
,
1972 #ifdef CONFIG_COMPAT
1973 .compat_ioctl
= lttng_event_notifier_event_ioctl
,
1977 static const struct file_operations lttng_event_notifier_enabler_fops
= {
1978 .owner
= THIS_MODULE
,
1979 .release
= lttng_event_notifier_enabler_release
,
1980 .unlocked_ioctl
= lttng_event_notifier_enabler_ioctl
,
1981 #ifdef CONFIG_COMPAT
1982 .compat_ioctl
= lttng_event_notifier_enabler_ioctl
,
1987 int lttng_abi_create_event_notifier(struct file
*event_notifier_group_file
,
1988 struct lttng_kernel_abi_event_notifier
*event_notifier_param
)
1990 struct lttng_event_notifier_group
*event_notifier_group
=
1991 event_notifier_group_file
->private_data
;
1992 const struct file_operations
*fops
;
1993 int event_notifier_fd
, ret
;
1994 struct file
*event_notifier_file
;
1997 switch (event_notifier_param
->event
.instrumentation
) {
1998 case LTTNG_KERNEL_ABI_TRACEPOINT
:
1999 case LTTNG_KERNEL_ABI_UPROBE
:
2001 case LTTNG_KERNEL_ABI_KPROBE
:
2002 event_notifier_param
->event
.u
.kprobe
.symbol_name
[LTTNG_KERNEL_ABI_SYM_NAME_LEN
- 1] = '\0';
2004 case LTTNG_KERNEL_ABI_SYSCALL
:
2006 case LTTNG_KERNEL_ABI_KRETPROBE
:
2007 /* Placing an event notifier on kretprobe is not supported. */
2008 case LTTNG_KERNEL_ABI_FUNCTION
:
2009 case LTTNG_KERNEL_ABI_NOOP
:
2015 switch (event_notifier_param
->event
.instrumentation
) {
2016 case LTTNG_KERNEL_ABI_TRACEPOINT
: /* Fall-through */
2017 case LTTNG_KERNEL_ABI_SYSCALL
:
2018 fops
= <tng_event_notifier_enabler_fops
;
2020 case LTTNG_KERNEL_ABI_KPROBE
: /* Fall-through */
2021 case LTTNG_KERNEL_ABI_KRETPROBE
: /* Fall-through */
2022 case LTTNG_KERNEL_ABI_UPROBE
:
2023 fops
= <tng_event_notifier_event_fops
;
2026 case LTTNG_KERNEL_ABI_FUNCTION
: /* Fall-through */
2027 case LTTNG_KERNEL_ABI_NOOP
: /* Fall-through */
2033 event_notifier_param
->event
.name
[LTTNG_KERNEL_ABI_SYM_NAME_LEN
- 1] = '\0';
2035 event_notifier_fd
= lttng_get_unused_fd();
2036 if (event_notifier_fd
< 0) {
2037 ret
= event_notifier_fd
;
2041 event_notifier_file
= anon_inode_getfile("[lttng_event_notifier]",
2042 fops
, NULL
, O_RDWR
);
2043 if (IS_ERR(event_notifier_file
)) {
2044 ret
= PTR_ERR(event_notifier_file
);
2048 /* The event notifier holds a reference on the event notifier group. */
2049 if (!atomic_long_add_unless(&event_notifier_group_file
->f_count
, 1, LONG_MAX
)) {
2051 goto refcount_error
;
2054 ret
= lttng_abi_validate_event_param(&event_notifier_param
->event
);
2056 goto event_notifier_error
;
2058 switch (event_notifier_param
->event
.instrumentation
) {
2059 case LTTNG_KERNEL_ABI_TRACEPOINT
: /* Fall-through */
2060 case LTTNG_KERNEL_ABI_SYSCALL
:
2062 struct lttng_event_notifier_enabler
*enabler
;
2064 if (strutils_is_star_glob_pattern(event_notifier_param
->event
.name
)) {
2066 * If the event name is a star globbing pattern,
2067 * we create the special star globbing enabler.
2069 enabler
= lttng_event_notifier_enabler_create(
2070 event_notifier_group
,
2071 LTTNG_ENABLER_FORMAT_STAR_GLOB
,
2072 event_notifier_param
);
2074 enabler
= lttng_event_notifier_enabler_create(
2075 event_notifier_group
,
2076 LTTNG_ENABLER_FORMAT_NAME
,
2077 event_notifier_param
);
2083 case LTTNG_KERNEL_ABI_KPROBE
: /* Fall-through */
2084 case LTTNG_KERNEL_ABI_KRETPROBE
: /* Fall-through */
2085 case LTTNG_KERNEL_ABI_UPROBE
:
2087 struct lttng_event_notifier
*event_notifier
;
2090 * We tolerate no failure path after event notifier creation.
2091 * It will stay invariant for the rest of the session.
2093 event_notifier
= lttng_event_notifier_create(NULL
,
2094 event_notifier_param
->event
.token
,
2095 event_notifier_param
->error_counter_index
,
2096 event_notifier_group
,
2097 event_notifier_param
, NULL
,
2098 event_notifier_param
->event
.instrumentation
);
2099 WARN_ON_ONCE(!event_notifier
);
2100 if (IS_ERR(event_notifier
)) {
2101 ret
= PTR_ERR(event_notifier
);
2102 goto event_notifier_error
;
2104 priv
= event_notifier
;
2108 case LTTNG_KERNEL_ABI_FUNCTION
: /* Fall-through */
2109 case LTTNG_KERNEL_ABI_NOOP
: /* Fall-through */
2112 goto event_notifier_error
;
2114 event_notifier_file
->private_data
= priv
;
2115 fd_install(event_notifier_fd
, event_notifier_file
);
2116 return event_notifier_fd
;
2118 event_notifier_error
:
2119 atomic_long_dec(&event_notifier_group_file
->f_count
);
2121 fput(event_notifier_file
);
2123 put_unused_fd(event_notifier_fd
);
2130 long lttng_abi_event_notifier_group_create_error_counter(
2131 struct file
*event_notifier_group_file
,
2132 const struct lttng_kernel_abi_counter_conf
*error_counter_conf
)
2134 int counter_fd
, ret
;
2135 char *counter_transport_name
;
2137 struct lttng_counter
*counter
= NULL
;
2138 struct file
*counter_file
;
2139 struct lttng_event_notifier_group
*event_notifier_group
=
2140 (struct lttng_event_notifier_group
*) event_notifier_group_file
->private_data
;
2142 if (error_counter_conf
->arithmetic
!= LTTNG_KERNEL_ABI_COUNTER_ARITHMETIC_MODULAR
) {
2143 printk(KERN_ERR
"LTTng: event_notifier: Error counter of the wrong arithmetic type.\n");
2147 if (error_counter_conf
->number_dimensions
!= 1) {
2148 printk(KERN_ERR
"LTTng: event_notifier: Error counter has more than one dimension.\n");
2152 switch (error_counter_conf
->bitness
) {
2153 case LTTNG_KERNEL_ABI_COUNTER_BITNESS_64
:
2154 counter_transport_name
= "counter-per-cpu-64-modular";
2156 case LTTNG_KERNEL_ABI_COUNTER_BITNESS_32
:
2157 counter_transport_name
= "counter-per-cpu-32-modular";
2164 * Lock sessions to provide mutual exclusion against concurrent
2165 * modification of event_notifier group, which would result in
2166 * overwriting the error counter if set concurrently.
2168 lttng_lock_sessions();
2170 if (event_notifier_group
->error_counter
) {
2171 printk(KERN_ERR
"Error counter already created in event_notifier group\n");
2176 counter_fd
= lttng_get_unused_fd();
2177 if (counter_fd
< 0) {
2182 counter_file
= anon_inode_getfile("[lttng_counter]",
2183 <tng_counter_fops
,
2185 if (IS_ERR(counter_file
)) {
2186 ret
= PTR_ERR(counter_file
);
2190 counter_len
= error_counter_conf
->dimensions
[0].size
;
2192 if (!atomic_long_add_unless(&event_notifier_group_file
->f_count
, 1, LONG_MAX
)) {
2194 goto refcount_error
;
2197 counter
= lttng_kernel_counter_create(counter_transport_name
,
2204 event_notifier_group
->error_counter_len
= counter_len
;
2206 * store-release to publish error counter matches load-acquire
2207 * in record_error. Ensures the counter is created and the
2208 * error_counter_len is set before they are used.
2210 lttng_smp_store_release(&event_notifier_group
->error_counter
, counter
);
2212 counter
->file
= counter_file
;
2213 counter
->owner
= event_notifier_group
->file
;
2214 counter_file
->private_data
= counter
;
2215 /* Ownership transferred. */
2218 fd_install(counter_fd
, counter_file
);
2219 lttng_unlock_sessions();
2224 atomic_long_dec(&event_notifier_group_file
->f_count
);
2228 put_unused_fd(counter_fd
);
2230 lttng_unlock_sessions();
2235 long lttng_event_notifier_group_ioctl(struct file
*file
, unsigned int cmd
,
2239 case LTTNG_KERNEL_ABI_EVENT_NOTIFIER_GROUP_NOTIFICATION_FD
:
2241 return lttng_abi_open_event_notifier_group_stream(file
);
2243 case LTTNG_KERNEL_ABI_EVENT_NOTIFIER_CREATE
:
2245 struct lttng_kernel_abi_event_notifier uevent_notifier_param
;
2247 if (copy_from_user(&uevent_notifier_param
,
2248 (struct lttng_kernel_abi_event_notifier __user
*) arg
,
2249 sizeof(uevent_notifier_param
)))
2251 return lttng_abi_create_event_notifier(file
, &uevent_notifier_param
);
2253 case LTTNG_KERNEL_ABI_COUNTER
:
2255 struct lttng_kernel_abi_counter_conf uerror_counter_conf
;
2257 if (copy_from_user(&uerror_counter_conf
,
2258 (struct lttng_kernel_abi_counter_conf __user
*) arg
,
2259 sizeof(uerror_counter_conf
)))
2261 return lttng_abi_event_notifier_group_create_error_counter(file
,
2262 &uerror_counter_conf
);
2265 return -ENOIOCTLCMD
;
2271 int lttng_event_notifier_group_release(struct inode
*inode
, struct file
*file
)
2273 struct lttng_event_notifier_group
*event_notifier_group
=
2276 if (event_notifier_group
)
2277 lttng_event_notifier_group_destroy(event_notifier_group
);
2281 static const struct file_operations lttng_event_notifier_group_fops
= {
2282 .owner
= THIS_MODULE
,
2283 .release
= lttng_event_notifier_group_release
,
2284 .unlocked_ioctl
= lttng_event_notifier_group_ioctl
,
2285 #ifdef CONFIG_COMPAT
2286 .compat_ioctl
= lttng_event_notifier_group_ioctl
,
2291 * lttng_channel_ioctl - lttng syscall through ioctl
2297 * This ioctl implements lttng commands:
2298 * LTTNG_KERNEL_ABI_STREAM
2299 * Returns an event stream file descriptor or failure.
2300 * (typically, one event stream records events from one CPU)
2301 * LTTNG_KERNEL_ABI_EVENT
2302 * Returns an event file descriptor or failure.
2303 * LTTNG_KERNEL_ABI_CONTEXT
2304 * Prepend a context field to each event in the channel
2305 * LTTNG_KERNEL_ABI_ENABLE
2306 * Enable recording for events in this channel (weak enable)
2307 * LTTNG_KERNEL_ABI_DISABLE
2308 * Disable recording for events in this channel (strong disable)
2310 * Channel and event file descriptors also hold a reference on the session.
2313 long lttng_channel_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
2315 struct lttng_channel
*channel
= file
->private_data
;
2318 case LTTNG_KERNEL_ABI_OLD_STREAM
:
2319 case LTTNG_KERNEL_ABI_STREAM
:
2320 return lttng_abi_open_stream(file
);
2321 case LTTNG_KERNEL_ABI_OLD_EVENT
:
2323 struct lttng_kernel_abi_event
*uevent_param
;
2324 struct lttng_kernel_abi_old_event
*old_uevent_param
;
2327 uevent_param
= kmalloc(sizeof(struct lttng_kernel_abi_event
),
2329 if (!uevent_param
) {
2333 old_uevent_param
= kmalloc(
2334 sizeof(struct lttng_kernel_abi_old_event
),
2336 if (!old_uevent_param
) {
2338 goto old_event_error_free_param
;
2340 if (copy_from_user(old_uevent_param
,
2341 (struct lttng_kernel_abi_old_event __user
*) arg
,
2342 sizeof(struct lttng_kernel_abi_old_event
))) {
2344 goto old_event_error_free_old_param
;
2347 memcpy(uevent_param
->name
, old_uevent_param
->name
,
2348 sizeof(uevent_param
->name
));
2349 uevent_param
->instrumentation
=
2350 old_uevent_param
->instrumentation
;
2352 switch (old_uevent_param
->instrumentation
) {
2353 case LTTNG_KERNEL_ABI_KPROBE
:
2354 uevent_param
->u
.kprobe
.addr
=
2355 old_uevent_param
->u
.kprobe
.addr
;
2356 uevent_param
->u
.kprobe
.offset
=
2357 old_uevent_param
->u
.kprobe
.offset
;
2358 memcpy(uevent_param
->u
.kprobe
.symbol_name
,
2359 old_uevent_param
->u
.kprobe
.symbol_name
,
2360 sizeof(uevent_param
->u
.kprobe
.symbol_name
));
2362 case LTTNG_KERNEL_ABI_KRETPROBE
:
2363 uevent_param
->u
.kretprobe
.addr
=
2364 old_uevent_param
->u
.kretprobe
.addr
;
2365 uevent_param
->u
.kretprobe
.offset
=
2366 old_uevent_param
->u
.kretprobe
.offset
;
2367 memcpy(uevent_param
->u
.kretprobe
.symbol_name
,
2368 old_uevent_param
->u
.kretprobe
.symbol_name
,
2369 sizeof(uevent_param
->u
.kretprobe
.symbol_name
));
2371 case LTTNG_KERNEL_ABI_FUNCTION
:
2373 /* Not implemented. */
2378 ret
= lttng_abi_create_event(file
, uevent_param
);
2380 old_event_error_free_old_param
:
2381 kfree(old_uevent_param
);
2382 old_event_error_free_param
:
2383 kfree(uevent_param
);
2387 case LTTNG_KERNEL_ABI_EVENT
:
2389 struct lttng_kernel_abi_event uevent_param
;
2391 if (copy_from_user(&uevent_param
,
2392 (struct lttng_kernel_abi_event __user
*) arg
,
2393 sizeof(uevent_param
)))
2395 return lttng_abi_create_event(file
, &uevent_param
);
2397 case LTTNG_KERNEL_ABI_OLD_CONTEXT
:
2399 struct lttng_kernel_abi_context
*ucontext_param
;
2400 struct lttng_kernel_abi_old_context
*old_ucontext_param
;
2403 ucontext_param
= kmalloc(sizeof(struct lttng_kernel_abi_context
),
2405 if (!ucontext_param
) {
2409 old_ucontext_param
= kmalloc(sizeof(struct lttng_kernel_abi_old_context
),
2411 if (!old_ucontext_param
) {
2413 goto old_ctx_error_free_param
;
2416 if (copy_from_user(old_ucontext_param
,
2417 (struct lttng_kernel_abi_old_context __user
*) arg
,
2418 sizeof(struct lttng_kernel_abi_old_context
))) {
2420 goto old_ctx_error_free_old_param
;
2422 ucontext_param
->ctx
= old_ucontext_param
->ctx
;
2423 memcpy(ucontext_param
->padding
, old_ucontext_param
->padding
,
2424 sizeof(ucontext_param
->padding
));
2425 /* only type that uses the union */
2426 if (old_ucontext_param
->ctx
== LTTNG_KERNEL_ABI_CONTEXT_PERF_COUNTER
) {
2427 ucontext_param
->u
.perf_counter
.type
=
2428 old_ucontext_param
->u
.perf_counter
.type
;
2429 ucontext_param
->u
.perf_counter
.config
=
2430 old_ucontext_param
->u
.perf_counter
.config
;
2431 memcpy(ucontext_param
->u
.perf_counter
.name
,
2432 old_ucontext_param
->u
.perf_counter
.name
,
2433 sizeof(ucontext_param
->u
.perf_counter
.name
));
2436 ret
= lttng_abi_add_context(file
,
2438 &channel
->ctx
, channel
->session
);
2440 old_ctx_error_free_old_param
:
2441 kfree(old_ucontext_param
);
2442 old_ctx_error_free_param
:
2443 kfree(ucontext_param
);
2447 case LTTNG_KERNEL_ABI_CONTEXT
:
2449 struct lttng_kernel_abi_context ucontext_param
;
2451 if (copy_from_user(&ucontext_param
,
2452 (struct lttng_kernel_abi_context __user
*) arg
,
2453 sizeof(ucontext_param
)))
2455 return lttng_abi_add_context(file
,
2457 &channel
->ctx
, channel
->session
);
2459 case LTTNG_KERNEL_ABI_OLD_ENABLE
:
2460 case LTTNG_KERNEL_ABI_ENABLE
:
2461 return lttng_channel_enable(channel
);
2462 case LTTNG_KERNEL_ABI_OLD_DISABLE
:
2463 case LTTNG_KERNEL_ABI_DISABLE
:
2464 return lttng_channel_disable(channel
);
2465 case LTTNG_KERNEL_ABI_SYSCALL_MASK
:
2466 return lttng_channel_syscall_mask(channel
,
2467 (struct lttng_kernel_abi_syscall_mask __user
*) arg
);
2469 return -ENOIOCTLCMD
;
2474 * lttng_metadata_ioctl - lttng syscall through ioctl
2480 * This ioctl implements lttng commands:
2481 * LTTNG_KERNEL_ABI_STREAM
2482 * Returns an event stream file descriptor or failure.
2484 * Channel and event file descriptors also hold a reference on the session.
2487 long lttng_metadata_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
2490 case LTTNG_KERNEL_ABI_OLD_STREAM
:
2491 case LTTNG_KERNEL_ABI_STREAM
:
2492 return lttng_abi_open_metadata_stream(file
);
2494 return -ENOIOCTLCMD
;
2499 * lttng_channel_poll - lttng stream addition/removal monitoring
2504 unsigned int lttng_channel_poll(struct file
*file
, poll_table
*wait
)
2506 struct lttng_channel
*channel
= file
->private_data
;
2507 unsigned int mask
= 0;
2509 if (file
->f_mode
& FMODE_READ
) {
2510 poll_wait_set_exclusive(wait
);
2511 poll_wait(file
, channel
->ops
->get_hp_wait_queue(channel
->chan
),
2514 if (channel
->ops
->is_disabled(channel
->chan
))
2516 if (channel
->ops
->is_finalized(channel
->chan
))
2518 if (channel
->ops
->buffer_has_read_closed_stream(channel
->chan
))
2519 return POLLIN
| POLLRDNORM
;
2527 int lttng_channel_release(struct inode
*inode
, struct file
*file
)
2529 struct lttng_channel
*channel
= file
->private_data
;
2532 fput(channel
->session
->file
);
2537 int lttng_metadata_channel_release(struct inode
*inode
, struct file
*file
)
2539 struct lttng_channel
*channel
= file
->private_data
;
2542 fput(channel
->session
->file
);
2543 lttng_metadata_channel_destroy(channel
);
2549 static const struct file_operations lttng_channel_fops
= {
2550 .owner
= THIS_MODULE
,
2551 .release
= lttng_channel_release
,
2552 .poll
= lttng_channel_poll
,
2553 .unlocked_ioctl
= lttng_channel_ioctl
,
2554 #ifdef CONFIG_COMPAT
2555 .compat_ioctl
= lttng_channel_ioctl
,
2559 static const struct file_operations lttng_metadata_fops
= {
2560 .owner
= THIS_MODULE
,
2561 .release
= lttng_metadata_channel_release
,
2562 .unlocked_ioctl
= lttng_metadata_ioctl
,
2563 #ifdef CONFIG_COMPAT
2564 .compat_ioctl
= lttng_metadata_ioctl
,
2569 * lttng_event_recorder_event_ioctl - lttng syscall through ioctl
2575 * This ioctl implements lttng commands:
2576 * LTTNG_KERNEL_ABI_CONTEXT
2577 * Prepend a context field to each record of this event
2578 * LTTNG_KERNEL_ABI_ENABLE
2579 * Enable recording for this event (weak enable)
2580 * LTTNG_KERNEL_ABI_DISABLE
2581 * Disable recording for this event (strong disable)
2584 long lttng_event_recorder_event_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
2586 struct lttng_event
*event
= file
->private_data
;
2589 case LTTNG_KERNEL_ABI_OLD_CONTEXT
:
2591 /* Not implemented */
2594 case LTTNG_KERNEL_ABI_CONTEXT
:
2596 /* Not implemented */
2599 case LTTNG_KERNEL_ABI_OLD_ENABLE
:
2600 case LTTNG_KERNEL_ABI_ENABLE
:
2601 return lttng_event_enable(event
);
2602 case LTTNG_KERNEL_ABI_OLD_DISABLE
:
2603 case LTTNG_KERNEL_ABI_DISABLE
:
2604 return lttng_event_disable(event
);
2605 case LTTNG_KERNEL_ABI_FILTER
:
2607 case LTTNG_KERNEL_ABI_ADD_CALLSITE
:
2608 return lttng_event_add_callsite(event
,
2609 (struct lttng_kernel_abi_event_callsite __user
*) arg
);
2611 return -ENOIOCTLCMD
;
2616 * lttng_event_recorder_enabler_ioctl - lttng syscall through ioctl
2622 * This ioctl implements lttng commands:
2623 * LTTNG_KERNEL_ABI_CONTEXT
2624 * Prepend a context field to each record of this event
2625 * LTTNG_KERNEL_ABI_ENABLE
2626 * Enable recording for this event (weak enable)
2627 * LTTNG_KERNEL_ABI_DISABLE
2628 * Disable recording for this event (strong disable)
2631 long lttng_event_recorder_enabler_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
2633 struct lttng_event_enabler
*event_enabler
= file
->private_data
;
2636 case LTTNG_KERNEL_ABI_OLD_CONTEXT
:
2638 /* Not implemented */
2641 case LTTNG_KERNEL_ABI_CONTEXT
:
2643 /* Not implemented */
2646 case LTTNG_KERNEL_ABI_OLD_ENABLE
:
2647 case LTTNG_KERNEL_ABI_ENABLE
:
2648 return lttng_event_enabler_enable(event_enabler
);
2649 case LTTNG_KERNEL_ABI_OLD_DISABLE
:
2650 case LTTNG_KERNEL_ABI_DISABLE
:
2651 return lttng_event_enabler_disable(event_enabler
);
2652 case LTTNG_KERNEL_ABI_FILTER
:
2653 return lttng_event_enabler_attach_filter_bytecode(
2655 (struct lttng_kernel_abi_filter_bytecode __user
*) arg
);
2656 case LTTNG_KERNEL_ABI_ADD_CALLSITE
:
2659 return -ENOIOCTLCMD
;
2664 int lttng_event_recorder_event_release(struct inode
*inode
, struct file
*file
)
2666 struct lttng_event
*event
= file
->private_data
;
2669 fput(event
->chan
->file
);
2674 int lttng_event_recorder_enabler_release(struct inode
*inode
, struct file
*file
)
2676 struct lttng_event_enabler
*event_enabler
= file
->private_data
;
2679 fput(event_enabler
->chan
->file
);
2683 static const struct file_operations lttng_event_recorder_event_fops
= {
2684 .owner
= THIS_MODULE
,
2685 .release
= lttng_event_recorder_event_release
,
2686 .unlocked_ioctl
= lttng_event_recorder_event_ioctl
,
2687 #ifdef CONFIG_COMPAT
2688 .compat_ioctl
= lttng_event_recorder_event_ioctl
,
2692 static const struct file_operations lttng_event_recorder_enabler_fops
= {
2693 .owner
= THIS_MODULE
,
2694 .release
= lttng_event_recorder_enabler_release
,
2695 .unlocked_ioctl
= lttng_event_recorder_enabler_ioctl
,
2696 #ifdef CONFIG_COMPAT
2697 .compat_ioctl
= lttng_event_recorder_enabler_ioctl
,
2701 static int put_u64(uint64_t val
, unsigned long arg
)
2703 return put_user(val
, (uint64_t __user
*) arg
);
2706 static int put_u32(uint32_t val
, unsigned long arg
)
2708 return put_user(val
, (uint32_t __user
*) arg
);
2711 static long lttng_stream_ring_buffer_ioctl(struct file
*filp
,
2712 unsigned int cmd
, unsigned long arg
)
2714 struct lib_ring_buffer
*buf
= filp
->private_data
;
2715 struct channel
*chan
= buf
->backend
.chan
;
2716 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
2717 const struct lttng_channel_ops
*ops
= chan
->backend
.priv_ops
;
2720 if (atomic_read(&chan
->record_disabled
))
2724 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_TIMESTAMP_BEGIN
:
2728 ret
= ops
->timestamp_begin(config
, buf
, &ts
);
2731 return put_u64(ts
, arg
);
2733 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_TIMESTAMP_END
:
2737 ret
= ops
->timestamp_end(config
, buf
, &ts
);
2740 return put_u64(ts
, arg
);
2742 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_EVENTS_DISCARDED
:
2746 ret
= ops
->events_discarded(config
, buf
, &ed
);
2749 return put_u64(ed
, arg
);
2751 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_CONTENT_SIZE
:
2755 ret
= ops
->content_size(config
, buf
, &cs
);
2758 return put_u64(cs
, arg
);
2760 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_PACKET_SIZE
:
2764 ret
= ops
->packet_size(config
, buf
, &ps
);
2767 return put_u64(ps
, arg
);
2769 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_STREAM_ID
:
2773 ret
= ops
->stream_id(config
, buf
, &si
);
2776 return put_u64(si
, arg
);
2778 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_CURRENT_TIMESTAMP
:
2782 ret
= ops
->current_timestamp(config
, buf
, &ts
);
2785 return put_u64(ts
, arg
);
2787 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_SEQ_NUM
:
2791 ret
= ops
->sequence_number(config
, buf
, &seq
);
2794 return put_u64(seq
, arg
);
2796 case LTTNG_KERNEL_ABI_RING_BUFFER_INSTANCE_ID
:
2800 ret
= ops
->instance_id(config
, buf
, &id
);
2803 return put_u64(id
, arg
);
2806 return lib_ring_buffer_file_operations
.unlocked_ioctl(filp
,
2814 #ifdef CONFIG_COMPAT
2815 static long lttng_stream_ring_buffer_compat_ioctl(struct file
*filp
,
2816 unsigned int cmd
, unsigned long arg
)
2818 struct lib_ring_buffer
*buf
= filp
->private_data
;
2819 struct channel
*chan
= buf
->backend
.chan
;
2820 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
2821 const struct lttng_channel_ops
*ops
= chan
->backend
.priv_ops
;
2824 if (atomic_read(&chan
->record_disabled
))
2828 case LTTNG_KERNEL_ABI_RING_BUFFER_COMPAT_GET_TIMESTAMP_BEGIN
:
2832 ret
= ops
->timestamp_begin(config
, buf
, &ts
);
2835 return put_u64(ts
, arg
);
2837 case LTTNG_KERNEL_ABI_RING_BUFFER_COMPAT_GET_TIMESTAMP_END
:
2841 ret
= ops
->timestamp_end(config
, buf
, &ts
);
2844 return put_u64(ts
, arg
);
2846 case LTTNG_KERNEL_ABI_RING_BUFFER_COMPAT_GET_EVENTS_DISCARDED
:
2850 ret
= ops
->events_discarded(config
, buf
, &ed
);
2853 return put_u64(ed
, arg
);
2855 case LTTNG_KERNEL_ABI_RING_BUFFER_COMPAT_GET_CONTENT_SIZE
:
2859 ret
= ops
->content_size(config
, buf
, &cs
);
2862 return put_u64(cs
, arg
);
2864 case LTTNG_KERNEL_ABI_RING_BUFFER_COMPAT_GET_PACKET_SIZE
:
2868 ret
= ops
->packet_size(config
, buf
, &ps
);
2871 return put_u64(ps
, arg
);
2873 case LTTNG_KERNEL_ABI_RING_BUFFER_COMPAT_GET_STREAM_ID
:
2877 ret
= ops
->stream_id(config
, buf
, &si
);
2880 return put_u64(si
, arg
);
2882 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_CURRENT_TIMESTAMP
:
2886 ret
= ops
->current_timestamp(config
, buf
, &ts
);
2889 return put_u64(ts
, arg
);
2891 case LTTNG_KERNEL_ABI_RING_BUFFER_COMPAT_GET_SEQ_NUM
:
2895 ret
= ops
->sequence_number(config
, buf
, &seq
);
2898 return put_u64(seq
, arg
);
2900 case LTTNG_KERNEL_ABI_RING_BUFFER_COMPAT_INSTANCE_ID
:
2904 ret
= ops
->instance_id(config
, buf
, &id
);
2907 return put_u64(id
, arg
);
2910 return lib_ring_buffer_file_operations
.compat_ioctl(filp
,
2917 #endif /* CONFIG_COMPAT */
2919 static void lttng_stream_override_ring_buffer_fops(void)
2921 lttng_stream_ring_buffer_file_operations
.owner
= THIS_MODULE
;
2922 lttng_stream_ring_buffer_file_operations
.open
=
2923 lib_ring_buffer_file_operations
.open
;
2924 lttng_stream_ring_buffer_file_operations
.release
=
2925 lib_ring_buffer_file_operations
.release
;
2926 lttng_stream_ring_buffer_file_operations
.poll
=
2927 lib_ring_buffer_file_operations
.poll
;
2928 lttng_stream_ring_buffer_file_operations
.splice_read
=
2929 lib_ring_buffer_file_operations
.splice_read
;
2930 lttng_stream_ring_buffer_file_operations
.mmap
=
2931 lib_ring_buffer_file_operations
.mmap
;
2932 lttng_stream_ring_buffer_file_operations
.unlocked_ioctl
=
2933 lttng_stream_ring_buffer_ioctl
;
2934 lttng_stream_ring_buffer_file_operations
.llseek
=
2935 lib_ring_buffer_file_operations
.llseek
;
2936 #ifdef CONFIG_COMPAT
2937 lttng_stream_ring_buffer_file_operations
.compat_ioctl
=
2938 lttng_stream_ring_buffer_compat_ioctl
;
2942 int __init
lttng_abi_init(void)
2946 wrapper_vmalloc_sync_mappings();
2949 ret
= lttng_tp_mempool_init();
2954 lttng_proc_dentry
= proc_create_data("lttng", S_IRUSR
| S_IWUSR
, NULL
,
2955 <tng_proc_ops
, NULL
);
2957 if (!lttng_proc_dentry
) {
2958 printk(KERN_ERR
"LTTng: Error creating control file\n");
2962 lttng_stream_override_ring_buffer_fops();
2966 lttng_tp_mempool_destroy();
2967 lttng_clock_unref();
2971 /* No __exit annotation because used by init error path too. */
2972 void lttng_abi_exit(void)
2974 lttng_tp_mempool_destroy();
2975 lttng_clock_unref();
2976 if (lttng_proc_dentry
)
2977 remove_proc_entry("lttng", NULL
);