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_kernel_event_recorder
*event
;
1866 * We tolerate no failure path after event creation. It
1867 * will stay invariant for the rest of the session.
1869 event
= lttng_kernel_event_recorder_create(channel
, event_param
,
1870 NULL
, event_param
->instrumentation
);
1871 WARN_ON_ONCE(!event
);
1872 if (IS_ERR(event
)) {
1873 ret
= PTR_ERR(event
);
1880 case LTTNG_KERNEL_ABI_FUNCTION
: /* Fall-through */
1881 case LTTNG_KERNEL_ABI_NOOP
: /* Fall-through */
1886 event_file
->private_data
= priv
;
1887 fd_install(event_fd
, event_file
);
1891 atomic_long_dec(&channel_file
->f_count
);
1895 put_unused_fd(event_fd
);
1901 long lttng_event_notifier_event_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1903 struct lttng_kernel_event_notifier
*event_notifier
= file
->private_data
;
1906 case LTTNG_KERNEL_ABI_ENABLE
:
1907 return lttng_event_notifier_enable(event_notifier
);
1908 case LTTNG_KERNEL_ABI_DISABLE
:
1909 return lttng_event_notifier_disable(event_notifier
);
1910 case LTTNG_KERNEL_ABI_FILTER
:
1912 case LTTNG_KERNEL_ABI_CAPTURE
:
1914 case LTTNG_KERNEL_ABI_ADD_CALLSITE
:
1915 return lttng_event_add_callsite(&event_notifier
->parent
,
1916 (struct lttng_kernel_abi_event_callsite __user
*) arg
);
1918 return -ENOIOCTLCMD
;
1923 long lttng_event_notifier_enabler_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1925 struct lttng_event_notifier_enabler
*event_notifier_enabler
= file
->private_data
;
1928 case LTTNG_KERNEL_ABI_ENABLE
:
1929 return lttng_event_notifier_enabler_enable(event_notifier_enabler
);
1930 case LTTNG_KERNEL_ABI_DISABLE
:
1931 return lttng_event_notifier_enabler_disable(event_notifier_enabler
);
1932 case LTTNG_KERNEL_ABI_FILTER
:
1933 return lttng_event_notifier_enabler_attach_filter_bytecode(
1934 event_notifier_enabler
,
1935 (struct lttng_kernel_abi_filter_bytecode __user
*) arg
);
1936 case LTTNG_KERNEL_ABI_CAPTURE
:
1937 return lttng_event_notifier_enabler_attach_capture_bytecode(
1938 event_notifier_enabler
,
1939 (struct lttng_kernel_abi_capture_bytecode __user
*) arg
);
1940 case LTTNG_KERNEL_ABI_ADD_CALLSITE
:
1943 return -ENOIOCTLCMD
;
1948 int lttng_event_notifier_event_release(struct inode
*inode
, struct file
*file
)
1950 struct lttng_kernel_event_notifier
*event_notifier
= file
->private_data
;
1953 fput(event_notifier
->priv
->group
->file
);
1958 int lttng_event_notifier_enabler_release(struct inode
*inode
, struct file
*file
)
1960 struct lttng_event_notifier_enabler
*event_notifier_enabler
= file
->private_data
;
1962 if (event_notifier_enabler
)
1963 fput(event_notifier_enabler
->group
->file
);
1967 static const struct file_operations lttng_event_notifier_event_fops
= {
1968 .owner
= THIS_MODULE
,
1969 .release
= lttng_event_notifier_event_release
,
1970 .unlocked_ioctl
= lttng_event_notifier_event_ioctl
,
1971 #ifdef CONFIG_COMPAT
1972 .compat_ioctl
= lttng_event_notifier_event_ioctl
,
1976 static const struct file_operations lttng_event_notifier_enabler_fops
= {
1977 .owner
= THIS_MODULE
,
1978 .release
= lttng_event_notifier_enabler_release
,
1979 .unlocked_ioctl
= lttng_event_notifier_enabler_ioctl
,
1980 #ifdef CONFIG_COMPAT
1981 .compat_ioctl
= lttng_event_notifier_enabler_ioctl
,
1986 int lttng_abi_create_event_notifier(struct file
*event_notifier_group_file
,
1987 struct lttng_kernel_abi_event_notifier
*event_notifier_param
)
1989 struct lttng_event_notifier_group
*event_notifier_group
=
1990 event_notifier_group_file
->private_data
;
1991 const struct file_operations
*fops
;
1992 int event_notifier_fd
, ret
;
1993 struct file
*event_notifier_file
;
1996 switch (event_notifier_param
->event
.instrumentation
) {
1997 case LTTNG_KERNEL_ABI_TRACEPOINT
:
1998 case LTTNG_KERNEL_ABI_UPROBE
:
2000 case LTTNG_KERNEL_ABI_KPROBE
:
2001 event_notifier_param
->event
.u
.kprobe
.symbol_name
[LTTNG_KERNEL_ABI_SYM_NAME_LEN
- 1] = '\0';
2003 case LTTNG_KERNEL_ABI_SYSCALL
:
2005 case LTTNG_KERNEL_ABI_KRETPROBE
:
2006 /* Placing an event notifier on kretprobe is not supported. */
2007 case LTTNG_KERNEL_ABI_FUNCTION
:
2008 case LTTNG_KERNEL_ABI_NOOP
:
2014 switch (event_notifier_param
->event
.instrumentation
) {
2015 case LTTNG_KERNEL_ABI_TRACEPOINT
: /* Fall-through */
2016 case LTTNG_KERNEL_ABI_SYSCALL
:
2017 fops
= <tng_event_notifier_enabler_fops
;
2019 case LTTNG_KERNEL_ABI_KPROBE
: /* Fall-through */
2020 case LTTNG_KERNEL_ABI_KRETPROBE
: /* Fall-through */
2021 case LTTNG_KERNEL_ABI_UPROBE
:
2022 fops
= <tng_event_notifier_event_fops
;
2025 case LTTNG_KERNEL_ABI_FUNCTION
: /* Fall-through */
2026 case LTTNG_KERNEL_ABI_NOOP
: /* Fall-through */
2032 event_notifier_param
->event
.name
[LTTNG_KERNEL_ABI_SYM_NAME_LEN
- 1] = '\0';
2034 event_notifier_fd
= lttng_get_unused_fd();
2035 if (event_notifier_fd
< 0) {
2036 ret
= event_notifier_fd
;
2040 event_notifier_file
= anon_inode_getfile("[lttng_event_notifier]",
2041 fops
, NULL
, O_RDWR
);
2042 if (IS_ERR(event_notifier_file
)) {
2043 ret
= PTR_ERR(event_notifier_file
);
2047 /* The event notifier holds a reference on the event notifier group. */
2048 if (!atomic_long_add_unless(&event_notifier_group_file
->f_count
, 1, LONG_MAX
)) {
2050 goto refcount_error
;
2053 ret
= lttng_abi_validate_event_param(&event_notifier_param
->event
);
2055 goto event_notifier_error
;
2057 switch (event_notifier_param
->event
.instrumentation
) {
2058 case LTTNG_KERNEL_ABI_TRACEPOINT
: /* Fall-through */
2059 case LTTNG_KERNEL_ABI_SYSCALL
:
2061 struct lttng_event_notifier_enabler
*enabler
;
2063 if (strutils_is_star_glob_pattern(event_notifier_param
->event
.name
)) {
2065 * If the event name is a star globbing pattern,
2066 * we create the special star globbing enabler.
2068 enabler
= lttng_event_notifier_enabler_create(
2069 event_notifier_group
,
2070 LTTNG_ENABLER_FORMAT_STAR_GLOB
,
2071 event_notifier_param
);
2073 enabler
= lttng_event_notifier_enabler_create(
2074 event_notifier_group
,
2075 LTTNG_ENABLER_FORMAT_NAME
,
2076 event_notifier_param
);
2082 case LTTNG_KERNEL_ABI_KPROBE
: /* Fall-through */
2083 case LTTNG_KERNEL_ABI_KRETPROBE
: /* Fall-through */
2084 case LTTNG_KERNEL_ABI_UPROBE
:
2086 struct lttng_kernel_event_notifier
*event_notifier
;
2089 * We tolerate no failure path after event notifier creation.
2090 * It will stay invariant for the rest of the session.
2092 event_notifier
= lttng_event_notifier_create(NULL
,
2093 event_notifier_param
->event
.token
,
2094 event_notifier_param
->error_counter_index
,
2095 event_notifier_group
,
2096 event_notifier_param
,
2097 event_notifier_param
->event
.instrumentation
);
2098 WARN_ON_ONCE(!event_notifier
);
2099 if (IS_ERR(event_notifier
)) {
2100 ret
= PTR_ERR(event_notifier
);
2101 goto event_notifier_error
;
2103 priv
= event_notifier
;
2107 case LTTNG_KERNEL_ABI_FUNCTION
: /* Fall-through */
2108 case LTTNG_KERNEL_ABI_NOOP
: /* Fall-through */
2111 goto event_notifier_error
;
2113 event_notifier_file
->private_data
= priv
;
2114 fd_install(event_notifier_fd
, event_notifier_file
);
2115 return event_notifier_fd
;
2117 event_notifier_error
:
2118 atomic_long_dec(&event_notifier_group_file
->f_count
);
2120 fput(event_notifier_file
);
2122 put_unused_fd(event_notifier_fd
);
2129 long lttng_abi_event_notifier_group_create_error_counter(
2130 struct file
*event_notifier_group_file
,
2131 const struct lttng_kernel_abi_counter_conf
*error_counter_conf
)
2133 int counter_fd
, ret
;
2134 char *counter_transport_name
;
2136 struct lttng_counter
*counter
= NULL
;
2137 struct file
*counter_file
;
2138 struct lttng_event_notifier_group
*event_notifier_group
=
2139 (struct lttng_event_notifier_group
*) event_notifier_group_file
->private_data
;
2141 if (error_counter_conf
->arithmetic
!= LTTNG_KERNEL_ABI_COUNTER_ARITHMETIC_MODULAR
) {
2142 printk(KERN_ERR
"LTTng: event_notifier: Error counter of the wrong arithmetic type.\n");
2146 if (error_counter_conf
->number_dimensions
!= 1) {
2147 printk(KERN_ERR
"LTTng: event_notifier: Error counter has more than one dimension.\n");
2151 switch (error_counter_conf
->bitness
) {
2152 case LTTNG_KERNEL_ABI_COUNTER_BITNESS_64
:
2153 counter_transport_name
= "counter-per-cpu-64-modular";
2155 case LTTNG_KERNEL_ABI_COUNTER_BITNESS_32
:
2156 counter_transport_name
= "counter-per-cpu-32-modular";
2163 * Lock sessions to provide mutual exclusion against concurrent
2164 * modification of event_notifier group, which would result in
2165 * overwriting the error counter if set concurrently.
2167 lttng_lock_sessions();
2169 if (event_notifier_group
->error_counter
) {
2170 printk(KERN_ERR
"Error counter already created in event_notifier group\n");
2175 counter_fd
= lttng_get_unused_fd();
2176 if (counter_fd
< 0) {
2181 counter_file
= anon_inode_getfile("[lttng_counter]",
2182 <tng_counter_fops
,
2184 if (IS_ERR(counter_file
)) {
2185 ret
= PTR_ERR(counter_file
);
2189 counter_len
= error_counter_conf
->dimensions
[0].size
;
2191 if (!atomic_long_add_unless(&event_notifier_group_file
->f_count
, 1, LONG_MAX
)) {
2193 goto refcount_error
;
2196 counter
= lttng_kernel_counter_create(counter_transport_name
,
2203 event_notifier_group
->error_counter_len
= counter_len
;
2205 * store-release to publish error counter matches load-acquire
2206 * in record_error. Ensures the counter is created and the
2207 * error_counter_len is set before they are used.
2209 lttng_smp_store_release(&event_notifier_group
->error_counter
, counter
);
2211 counter
->file
= counter_file
;
2212 counter
->owner
= event_notifier_group
->file
;
2213 counter_file
->private_data
= counter
;
2214 /* Ownership transferred. */
2217 fd_install(counter_fd
, counter_file
);
2218 lttng_unlock_sessions();
2223 atomic_long_dec(&event_notifier_group_file
->f_count
);
2227 put_unused_fd(counter_fd
);
2229 lttng_unlock_sessions();
2234 long lttng_event_notifier_group_ioctl(struct file
*file
, unsigned int cmd
,
2238 case LTTNG_KERNEL_ABI_EVENT_NOTIFIER_GROUP_NOTIFICATION_FD
:
2240 return lttng_abi_open_event_notifier_group_stream(file
);
2242 case LTTNG_KERNEL_ABI_EVENT_NOTIFIER_CREATE
:
2244 struct lttng_kernel_abi_event_notifier uevent_notifier_param
;
2246 if (copy_from_user(&uevent_notifier_param
,
2247 (struct lttng_kernel_abi_event_notifier __user
*) arg
,
2248 sizeof(uevent_notifier_param
)))
2250 return lttng_abi_create_event_notifier(file
, &uevent_notifier_param
);
2252 case LTTNG_KERNEL_ABI_COUNTER
:
2254 struct lttng_kernel_abi_counter_conf uerror_counter_conf
;
2256 if (copy_from_user(&uerror_counter_conf
,
2257 (struct lttng_kernel_abi_counter_conf __user
*) arg
,
2258 sizeof(uerror_counter_conf
)))
2260 return lttng_abi_event_notifier_group_create_error_counter(file
,
2261 &uerror_counter_conf
);
2264 return -ENOIOCTLCMD
;
2270 int lttng_event_notifier_group_release(struct inode
*inode
, struct file
*file
)
2272 struct lttng_event_notifier_group
*event_notifier_group
=
2275 if (event_notifier_group
)
2276 lttng_event_notifier_group_destroy(event_notifier_group
);
2280 static const struct file_operations lttng_event_notifier_group_fops
= {
2281 .owner
= THIS_MODULE
,
2282 .release
= lttng_event_notifier_group_release
,
2283 .unlocked_ioctl
= lttng_event_notifier_group_ioctl
,
2284 #ifdef CONFIG_COMPAT
2285 .compat_ioctl
= lttng_event_notifier_group_ioctl
,
2290 * lttng_channel_ioctl - lttng syscall through ioctl
2296 * This ioctl implements lttng commands:
2297 * LTTNG_KERNEL_ABI_STREAM
2298 * Returns an event stream file descriptor or failure.
2299 * (typically, one event stream records events from one CPU)
2300 * LTTNG_KERNEL_ABI_EVENT
2301 * Returns an event file descriptor or failure.
2302 * LTTNG_KERNEL_ABI_CONTEXT
2303 * Prepend a context field to each event in the channel
2304 * LTTNG_KERNEL_ABI_ENABLE
2305 * Enable recording for events in this channel (weak enable)
2306 * LTTNG_KERNEL_ABI_DISABLE
2307 * Disable recording for events in this channel (strong disable)
2309 * Channel and event file descriptors also hold a reference on the session.
2312 long lttng_channel_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
2314 struct lttng_channel
*channel
= file
->private_data
;
2317 case LTTNG_KERNEL_ABI_OLD_STREAM
:
2318 case LTTNG_KERNEL_ABI_STREAM
:
2319 return lttng_abi_open_stream(file
);
2320 case LTTNG_KERNEL_ABI_OLD_EVENT
:
2322 struct lttng_kernel_abi_event
*uevent_param
;
2323 struct lttng_kernel_abi_old_event
*old_uevent_param
;
2326 uevent_param
= kmalloc(sizeof(struct lttng_kernel_abi_event
),
2328 if (!uevent_param
) {
2332 old_uevent_param
= kmalloc(
2333 sizeof(struct lttng_kernel_abi_old_event
),
2335 if (!old_uevent_param
) {
2337 goto old_event_error_free_param
;
2339 if (copy_from_user(old_uevent_param
,
2340 (struct lttng_kernel_abi_old_event __user
*) arg
,
2341 sizeof(struct lttng_kernel_abi_old_event
))) {
2343 goto old_event_error_free_old_param
;
2346 memcpy(uevent_param
->name
, old_uevent_param
->name
,
2347 sizeof(uevent_param
->name
));
2348 uevent_param
->instrumentation
=
2349 old_uevent_param
->instrumentation
;
2351 switch (old_uevent_param
->instrumentation
) {
2352 case LTTNG_KERNEL_ABI_KPROBE
:
2353 uevent_param
->u
.kprobe
.addr
=
2354 old_uevent_param
->u
.kprobe
.addr
;
2355 uevent_param
->u
.kprobe
.offset
=
2356 old_uevent_param
->u
.kprobe
.offset
;
2357 memcpy(uevent_param
->u
.kprobe
.symbol_name
,
2358 old_uevent_param
->u
.kprobe
.symbol_name
,
2359 sizeof(uevent_param
->u
.kprobe
.symbol_name
));
2361 case LTTNG_KERNEL_ABI_KRETPROBE
:
2362 uevent_param
->u
.kretprobe
.addr
=
2363 old_uevent_param
->u
.kretprobe
.addr
;
2364 uevent_param
->u
.kretprobe
.offset
=
2365 old_uevent_param
->u
.kretprobe
.offset
;
2366 memcpy(uevent_param
->u
.kretprobe
.symbol_name
,
2367 old_uevent_param
->u
.kretprobe
.symbol_name
,
2368 sizeof(uevent_param
->u
.kretprobe
.symbol_name
));
2370 case LTTNG_KERNEL_ABI_FUNCTION
:
2372 /* Not implemented. */
2377 ret
= lttng_abi_create_event(file
, uevent_param
);
2379 old_event_error_free_old_param
:
2380 kfree(old_uevent_param
);
2381 old_event_error_free_param
:
2382 kfree(uevent_param
);
2386 case LTTNG_KERNEL_ABI_EVENT
:
2388 struct lttng_kernel_abi_event uevent_param
;
2390 if (copy_from_user(&uevent_param
,
2391 (struct lttng_kernel_abi_event __user
*) arg
,
2392 sizeof(uevent_param
)))
2394 return lttng_abi_create_event(file
, &uevent_param
);
2396 case LTTNG_KERNEL_ABI_OLD_CONTEXT
:
2398 struct lttng_kernel_abi_context
*ucontext_param
;
2399 struct lttng_kernel_abi_old_context
*old_ucontext_param
;
2402 ucontext_param
= kmalloc(sizeof(struct lttng_kernel_abi_context
),
2404 if (!ucontext_param
) {
2408 old_ucontext_param
= kmalloc(sizeof(struct lttng_kernel_abi_old_context
),
2410 if (!old_ucontext_param
) {
2412 goto old_ctx_error_free_param
;
2415 if (copy_from_user(old_ucontext_param
,
2416 (struct lttng_kernel_abi_old_context __user
*) arg
,
2417 sizeof(struct lttng_kernel_abi_old_context
))) {
2419 goto old_ctx_error_free_old_param
;
2421 ucontext_param
->ctx
= old_ucontext_param
->ctx
;
2422 memcpy(ucontext_param
->padding
, old_ucontext_param
->padding
,
2423 sizeof(ucontext_param
->padding
));
2424 /* only type that uses the union */
2425 if (old_ucontext_param
->ctx
== LTTNG_KERNEL_ABI_CONTEXT_PERF_COUNTER
) {
2426 ucontext_param
->u
.perf_counter
.type
=
2427 old_ucontext_param
->u
.perf_counter
.type
;
2428 ucontext_param
->u
.perf_counter
.config
=
2429 old_ucontext_param
->u
.perf_counter
.config
;
2430 memcpy(ucontext_param
->u
.perf_counter
.name
,
2431 old_ucontext_param
->u
.perf_counter
.name
,
2432 sizeof(ucontext_param
->u
.perf_counter
.name
));
2435 ret
= lttng_abi_add_context(file
,
2437 &channel
->ctx
, channel
->session
);
2439 old_ctx_error_free_old_param
:
2440 kfree(old_ucontext_param
);
2441 old_ctx_error_free_param
:
2442 kfree(ucontext_param
);
2446 case LTTNG_KERNEL_ABI_CONTEXT
:
2448 struct lttng_kernel_abi_context ucontext_param
;
2450 if (copy_from_user(&ucontext_param
,
2451 (struct lttng_kernel_abi_context __user
*) arg
,
2452 sizeof(ucontext_param
)))
2454 return lttng_abi_add_context(file
,
2456 &channel
->ctx
, channel
->session
);
2458 case LTTNG_KERNEL_ABI_OLD_ENABLE
:
2459 case LTTNG_KERNEL_ABI_ENABLE
:
2460 return lttng_channel_enable(channel
);
2461 case LTTNG_KERNEL_ABI_OLD_DISABLE
:
2462 case LTTNG_KERNEL_ABI_DISABLE
:
2463 return lttng_channel_disable(channel
);
2464 case LTTNG_KERNEL_ABI_SYSCALL_MASK
:
2465 return lttng_channel_syscall_mask(channel
,
2466 (struct lttng_kernel_abi_syscall_mask __user
*) arg
);
2468 return -ENOIOCTLCMD
;
2473 * lttng_metadata_ioctl - lttng syscall through ioctl
2479 * This ioctl implements lttng commands:
2480 * LTTNG_KERNEL_ABI_STREAM
2481 * Returns an event stream file descriptor or failure.
2483 * Channel and event file descriptors also hold a reference on the session.
2486 long lttng_metadata_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
2489 case LTTNG_KERNEL_ABI_OLD_STREAM
:
2490 case LTTNG_KERNEL_ABI_STREAM
:
2491 return lttng_abi_open_metadata_stream(file
);
2493 return -ENOIOCTLCMD
;
2498 * lttng_channel_poll - lttng stream addition/removal monitoring
2503 unsigned int lttng_channel_poll(struct file
*file
, poll_table
*wait
)
2505 struct lttng_channel
*channel
= file
->private_data
;
2506 unsigned int mask
= 0;
2508 if (file
->f_mode
& FMODE_READ
) {
2509 poll_wait_set_exclusive(wait
);
2510 poll_wait(file
, channel
->ops
->get_hp_wait_queue(channel
->chan
),
2513 if (channel
->ops
->is_disabled(channel
->chan
))
2515 if (channel
->ops
->is_finalized(channel
->chan
))
2517 if (channel
->ops
->buffer_has_read_closed_stream(channel
->chan
))
2518 return POLLIN
| POLLRDNORM
;
2526 int lttng_channel_release(struct inode
*inode
, struct file
*file
)
2528 struct lttng_channel
*channel
= file
->private_data
;
2531 fput(channel
->session
->file
);
2536 int lttng_metadata_channel_release(struct inode
*inode
, struct file
*file
)
2538 struct lttng_channel
*channel
= file
->private_data
;
2541 fput(channel
->session
->file
);
2542 lttng_metadata_channel_destroy(channel
);
2548 static const struct file_operations lttng_channel_fops
= {
2549 .owner
= THIS_MODULE
,
2550 .release
= lttng_channel_release
,
2551 .poll
= lttng_channel_poll
,
2552 .unlocked_ioctl
= lttng_channel_ioctl
,
2553 #ifdef CONFIG_COMPAT
2554 .compat_ioctl
= lttng_channel_ioctl
,
2558 static const struct file_operations lttng_metadata_fops
= {
2559 .owner
= THIS_MODULE
,
2560 .release
= lttng_metadata_channel_release
,
2561 .unlocked_ioctl
= lttng_metadata_ioctl
,
2562 #ifdef CONFIG_COMPAT
2563 .compat_ioctl
= lttng_metadata_ioctl
,
2568 * lttng_event_recorder_event_ioctl - lttng syscall through ioctl
2574 * This ioctl implements lttng commands:
2575 * LTTNG_KERNEL_ABI_CONTEXT
2576 * Prepend a context field to each record of this event
2577 * LTTNG_KERNEL_ABI_ENABLE
2578 * Enable recording for this event (weak enable)
2579 * LTTNG_KERNEL_ABI_DISABLE
2580 * Disable recording for this event (strong disable)
2583 long lttng_event_recorder_event_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
2585 struct lttng_kernel_event_recorder
*event_recorder
= file
->private_data
;
2588 case LTTNG_KERNEL_ABI_OLD_CONTEXT
:
2590 /* Not implemented */
2593 case LTTNG_KERNEL_ABI_CONTEXT
:
2595 /* Not implemented */
2598 case LTTNG_KERNEL_ABI_OLD_ENABLE
:
2599 case LTTNG_KERNEL_ABI_ENABLE
:
2600 return lttng_event_enable(event_recorder
);
2601 case LTTNG_KERNEL_ABI_OLD_DISABLE
:
2602 case LTTNG_KERNEL_ABI_DISABLE
:
2603 return lttng_event_disable(event_recorder
);
2604 case LTTNG_KERNEL_ABI_FILTER
:
2606 case LTTNG_KERNEL_ABI_ADD_CALLSITE
:
2607 return lttng_event_add_callsite(&event_recorder
->parent
,
2608 (struct lttng_kernel_abi_event_callsite __user
*) arg
);
2610 return -ENOIOCTLCMD
;
2615 * lttng_event_recorder_enabler_ioctl - lttng syscall through ioctl
2621 * This ioctl implements lttng commands:
2622 * LTTNG_KERNEL_ABI_CONTEXT
2623 * Prepend a context field to each record of this event
2624 * LTTNG_KERNEL_ABI_ENABLE
2625 * Enable recording for this event (weak enable)
2626 * LTTNG_KERNEL_ABI_DISABLE
2627 * Disable recording for this event (strong disable)
2630 long lttng_event_recorder_enabler_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
2632 struct lttng_event_enabler
*event_enabler
= file
->private_data
;
2635 case LTTNG_KERNEL_ABI_OLD_CONTEXT
:
2637 /* Not implemented */
2640 case LTTNG_KERNEL_ABI_CONTEXT
:
2642 /* Not implemented */
2645 case LTTNG_KERNEL_ABI_OLD_ENABLE
:
2646 case LTTNG_KERNEL_ABI_ENABLE
:
2647 return lttng_event_enabler_enable(event_enabler
);
2648 case LTTNG_KERNEL_ABI_OLD_DISABLE
:
2649 case LTTNG_KERNEL_ABI_DISABLE
:
2650 return lttng_event_enabler_disable(event_enabler
);
2651 case LTTNG_KERNEL_ABI_FILTER
:
2652 return lttng_event_enabler_attach_filter_bytecode(
2654 (struct lttng_kernel_abi_filter_bytecode __user
*) arg
);
2655 case LTTNG_KERNEL_ABI_ADD_CALLSITE
:
2658 return -ENOIOCTLCMD
;
2663 int lttng_event_recorder_event_release(struct inode
*inode
, struct file
*file
)
2665 struct lttng_kernel_event_recorder
*event
= file
->private_data
;
2668 fput(event
->chan
->file
);
2673 int lttng_event_recorder_enabler_release(struct inode
*inode
, struct file
*file
)
2675 struct lttng_event_enabler
*event_enabler
= file
->private_data
;
2678 fput(event_enabler
->chan
->file
);
2682 static const struct file_operations lttng_event_recorder_event_fops
= {
2683 .owner
= THIS_MODULE
,
2684 .release
= lttng_event_recorder_event_release
,
2685 .unlocked_ioctl
= lttng_event_recorder_event_ioctl
,
2686 #ifdef CONFIG_COMPAT
2687 .compat_ioctl
= lttng_event_recorder_event_ioctl
,
2691 static const struct file_operations lttng_event_recorder_enabler_fops
= {
2692 .owner
= THIS_MODULE
,
2693 .release
= lttng_event_recorder_enabler_release
,
2694 .unlocked_ioctl
= lttng_event_recorder_enabler_ioctl
,
2695 #ifdef CONFIG_COMPAT
2696 .compat_ioctl
= lttng_event_recorder_enabler_ioctl
,
2700 static int put_u64(uint64_t val
, unsigned long arg
)
2702 return put_user(val
, (uint64_t __user
*) arg
);
2705 static int put_u32(uint32_t val
, unsigned long arg
)
2707 return put_user(val
, (uint32_t __user
*) arg
);
2710 static long lttng_stream_ring_buffer_ioctl(struct file
*filp
,
2711 unsigned int cmd
, unsigned long arg
)
2713 struct lib_ring_buffer
*buf
= filp
->private_data
;
2714 struct channel
*chan
= buf
->backend
.chan
;
2715 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
2716 const struct lttng_channel_ops
*ops
= chan
->backend
.priv_ops
;
2719 if (atomic_read(&chan
->record_disabled
))
2723 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_TIMESTAMP_BEGIN
:
2727 ret
= ops
->timestamp_begin(config
, buf
, &ts
);
2730 return put_u64(ts
, arg
);
2732 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_TIMESTAMP_END
:
2736 ret
= ops
->timestamp_end(config
, buf
, &ts
);
2739 return put_u64(ts
, arg
);
2741 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_EVENTS_DISCARDED
:
2745 ret
= ops
->events_discarded(config
, buf
, &ed
);
2748 return put_u64(ed
, arg
);
2750 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_CONTENT_SIZE
:
2754 ret
= ops
->content_size(config
, buf
, &cs
);
2757 return put_u64(cs
, arg
);
2759 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_PACKET_SIZE
:
2763 ret
= ops
->packet_size(config
, buf
, &ps
);
2766 return put_u64(ps
, arg
);
2768 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_STREAM_ID
:
2772 ret
= ops
->stream_id(config
, buf
, &si
);
2775 return put_u64(si
, arg
);
2777 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_CURRENT_TIMESTAMP
:
2781 ret
= ops
->current_timestamp(config
, buf
, &ts
);
2784 return put_u64(ts
, arg
);
2786 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_SEQ_NUM
:
2790 ret
= ops
->sequence_number(config
, buf
, &seq
);
2793 return put_u64(seq
, arg
);
2795 case LTTNG_KERNEL_ABI_RING_BUFFER_INSTANCE_ID
:
2799 ret
= ops
->instance_id(config
, buf
, &id
);
2802 return put_u64(id
, arg
);
2805 return lib_ring_buffer_file_operations
.unlocked_ioctl(filp
,
2813 #ifdef CONFIG_COMPAT
2814 static long lttng_stream_ring_buffer_compat_ioctl(struct file
*filp
,
2815 unsigned int cmd
, unsigned long arg
)
2817 struct lib_ring_buffer
*buf
= filp
->private_data
;
2818 struct channel
*chan
= buf
->backend
.chan
;
2819 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
2820 const struct lttng_channel_ops
*ops
= chan
->backend
.priv_ops
;
2823 if (atomic_read(&chan
->record_disabled
))
2827 case LTTNG_KERNEL_ABI_RING_BUFFER_COMPAT_GET_TIMESTAMP_BEGIN
:
2831 ret
= ops
->timestamp_begin(config
, buf
, &ts
);
2834 return put_u64(ts
, arg
);
2836 case LTTNG_KERNEL_ABI_RING_BUFFER_COMPAT_GET_TIMESTAMP_END
:
2840 ret
= ops
->timestamp_end(config
, buf
, &ts
);
2843 return put_u64(ts
, arg
);
2845 case LTTNG_KERNEL_ABI_RING_BUFFER_COMPAT_GET_EVENTS_DISCARDED
:
2849 ret
= ops
->events_discarded(config
, buf
, &ed
);
2852 return put_u64(ed
, arg
);
2854 case LTTNG_KERNEL_ABI_RING_BUFFER_COMPAT_GET_CONTENT_SIZE
:
2858 ret
= ops
->content_size(config
, buf
, &cs
);
2861 return put_u64(cs
, arg
);
2863 case LTTNG_KERNEL_ABI_RING_BUFFER_COMPAT_GET_PACKET_SIZE
:
2867 ret
= ops
->packet_size(config
, buf
, &ps
);
2870 return put_u64(ps
, arg
);
2872 case LTTNG_KERNEL_ABI_RING_BUFFER_COMPAT_GET_STREAM_ID
:
2876 ret
= ops
->stream_id(config
, buf
, &si
);
2879 return put_u64(si
, arg
);
2881 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_CURRENT_TIMESTAMP
:
2885 ret
= ops
->current_timestamp(config
, buf
, &ts
);
2888 return put_u64(ts
, arg
);
2890 case LTTNG_KERNEL_ABI_RING_BUFFER_COMPAT_GET_SEQ_NUM
:
2894 ret
= ops
->sequence_number(config
, buf
, &seq
);
2897 return put_u64(seq
, arg
);
2899 case LTTNG_KERNEL_ABI_RING_BUFFER_COMPAT_INSTANCE_ID
:
2903 ret
= ops
->instance_id(config
, buf
, &id
);
2906 return put_u64(id
, arg
);
2909 return lib_ring_buffer_file_operations
.compat_ioctl(filp
,
2916 #endif /* CONFIG_COMPAT */
2918 static void lttng_stream_override_ring_buffer_fops(void)
2920 lttng_stream_ring_buffer_file_operations
.owner
= THIS_MODULE
;
2921 lttng_stream_ring_buffer_file_operations
.open
=
2922 lib_ring_buffer_file_operations
.open
;
2923 lttng_stream_ring_buffer_file_operations
.release
=
2924 lib_ring_buffer_file_operations
.release
;
2925 lttng_stream_ring_buffer_file_operations
.poll
=
2926 lib_ring_buffer_file_operations
.poll
;
2927 lttng_stream_ring_buffer_file_operations
.splice_read
=
2928 lib_ring_buffer_file_operations
.splice_read
;
2929 lttng_stream_ring_buffer_file_operations
.mmap
=
2930 lib_ring_buffer_file_operations
.mmap
;
2931 lttng_stream_ring_buffer_file_operations
.unlocked_ioctl
=
2932 lttng_stream_ring_buffer_ioctl
;
2933 lttng_stream_ring_buffer_file_operations
.llseek
=
2934 lib_ring_buffer_file_operations
.llseek
;
2935 #ifdef CONFIG_COMPAT
2936 lttng_stream_ring_buffer_file_operations
.compat_ioctl
=
2937 lttng_stream_ring_buffer_compat_ioctl
;
2941 int __init
lttng_abi_init(void)
2945 wrapper_vmalloc_sync_mappings();
2948 ret
= lttng_tp_mempool_init();
2953 lttng_proc_dentry
= proc_create_data("lttng", S_IRUSR
| S_IWUSR
, NULL
,
2954 <tng_proc_ops
, NULL
);
2956 if (!lttng_proc_dentry
) {
2957 printk(KERN_ERR
"LTTng: Error creating control file\n");
2961 lttng_stream_override_ring_buffer_fops();
2965 lttng_tp_mempool_destroy();
2966 lttng_clock_unref();
2970 /* No __exit annotation because used by init error path too. */
2971 void lttng_abi_exit(void)
2973 lttng_tp_mempool_destroy();
2974 lttng_clock_unref();
2975 if (lttng_proc_dentry
)
2976 remove_proc_entry("lttng", NULL
);