1 /* SPDX-License-Identifier: (GPL-2.0-only or LGPL-2.1-only)
7 * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
9 * Mimic system calls for:
10 * - session creation, returns a file descriptor or failure.
11 * - channel creation, returns a file descriptor or failure.
12 * - Operates on a session file descriptor
13 * - Takes all channel options as parameters.
14 * - stream get, returns a file descriptor or failure.
15 * - Operates on a channel file descriptor.
16 * - stream notifier get, returns a file descriptor or failure.
17 * - Operates on a channel file descriptor.
18 * - event creation, returns a file descriptor or failure.
19 * - Operates on a channel file descriptor
20 * - Takes an event name as parameter
21 * - Takes an instrumentation source as parameter
22 * - e.g. tracepoints, dynamic_probes...
23 * - Takes instrumentation source specific arguments.
26 #include <linux/module.h>
27 #include <linux/proc_fs.h>
28 #include <linux/anon_inodes.h>
29 #include <linux/file.h>
30 #include <linux/uaccess.h>
31 #include <linux/slab.h>
32 #include <linux/err.h>
33 #include <wrapper/vmalloc.h> /* for wrapper_vmalloc_sync_mappings() */
34 #include <ringbuffer/vfs.h>
35 #include <ringbuffer/backend.h>
36 #include <ringbuffer/frontend.h>
37 #include <wrapper/poll.h>
38 #include <wrapper/file.h>
39 #include <wrapper/kref.h>
40 #include <lttng/string-utils.h>
41 #include <lttng/abi.h>
42 #include <lttng/abi-old.h>
43 #include <lttng/events.h>
44 #include <lttng/tracer.h>
45 #include <lttng/tp-mempool.h>
46 #include <ringbuffer/frontend_types.h>
47 #include <ringbuffer/iterator.h>
50 * This is LTTng's own personal way to create a system call as an external
51 * module. We use ioctl() on /proc/lttng.
54 static struct proc_dir_entry
*lttng_proc_dentry
;
56 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(5,6,0))
57 static const struct proc_ops lttng_proc_ops
;
59 static const struct file_operations lttng_proc_ops
;
62 static const struct file_operations lttng_session_fops
;
63 static const struct file_operations lttng_event_notifier_group_fops
;
64 static const struct file_operations lttng_channel_fops
;
65 static const struct file_operations lttng_metadata_fops
;
66 static const struct file_operations lttng_event_fops
;
67 static struct file_operations lttng_stream_ring_buffer_file_operations
;
69 static int put_u64(uint64_t val
, unsigned long arg
);
70 static int put_u32(uint32_t val
, unsigned long arg
);
73 * Teardown management: opened file descriptors keep a refcount on the module,
74 * so it can only exit when all file descriptors are closed.
78 int lttng_abi_create_session(void)
80 struct lttng_session
*session
;
81 struct file
*session_file
;
84 session
= lttng_session_create();
87 session_fd
= lttng_get_unused_fd();
92 session_file
= anon_inode_getfile("[lttng_session]",
95 if (IS_ERR(session_file
)) {
96 ret
= PTR_ERR(session_file
);
99 session
->file
= session_file
;
100 fd_install(session_fd
, session_file
);
104 put_unused_fd(session_fd
);
106 lttng_session_destroy(session
);
110 void event_notifier_send_notification_work_wakeup(struct irq_work
*entry
)
112 struct lttng_event_notifier_group
*event_notifier_group
=
113 container_of(entry
, struct lttng_event_notifier_group
,
115 wake_up_interruptible(&event_notifier_group
->read_wait
);
119 int lttng_abi_create_event_notifier_group(void)
121 struct lttng_event_notifier_group
*event_notifier_group
;
122 struct file
*event_notifier_group_file
;
123 int event_notifier_group_fd
, ret
;
125 event_notifier_group
= lttng_event_notifier_group_create();
126 if (!event_notifier_group
)
129 event_notifier_group_fd
= lttng_get_unused_fd();
130 if (event_notifier_group_fd
< 0) {
131 ret
= event_notifier_group_fd
;
134 event_notifier_group_file
= anon_inode_getfile("[lttng_event_notifier_group]",
135 <tng_event_notifier_group_fops
,
136 event_notifier_group
, O_RDWR
);
137 if (IS_ERR(event_notifier_group_file
)) {
138 ret
= PTR_ERR(event_notifier_group_file
);
142 event_notifier_group
->file
= event_notifier_group_file
;
143 init_waitqueue_head(&event_notifier_group
->read_wait
);
144 init_irq_work(&event_notifier_group
->wakeup_pending
,
145 event_notifier_send_notification_work_wakeup
);
146 fd_install(event_notifier_group_fd
, event_notifier_group_file
);
147 return event_notifier_group_fd
;
150 put_unused_fd(event_notifier_group_fd
);
152 lttng_event_notifier_group_destroy(event_notifier_group
);
157 int lttng_abi_tracepoint_list(void)
159 struct file
*tracepoint_list_file
;
162 file_fd
= lttng_get_unused_fd();
168 tracepoint_list_file
= anon_inode_getfile("[lttng_tracepoint_list]",
169 <tng_tracepoint_list_fops
,
171 if (IS_ERR(tracepoint_list_file
)) {
172 ret
= PTR_ERR(tracepoint_list_file
);
175 ret
= lttng_tracepoint_list_fops
.open(NULL
, tracepoint_list_file
);
178 fd_install(file_fd
, tracepoint_list_file
);
182 fput(tracepoint_list_file
);
184 put_unused_fd(file_fd
);
189 #ifndef CONFIG_HAVE_SYSCALL_TRACEPOINTS
191 int lttng_abi_syscall_list(void)
197 int lttng_abi_syscall_list(void)
199 struct file
*syscall_list_file
;
202 file_fd
= lttng_get_unused_fd();
208 syscall_list_file
= anon_inode_getfile("[lttng_syscall_list]",
209 <tng_syscall_list_fops
,
211 if (IS_ERR(syscall_list_file
)) {
212 ret
= PTR_ERR(syscall_list_file
);
215 ret
= lttng_syscall_list_fops
.open(NULL
, syscall_list_file
);
218 fd_install(file_fd
, syscall_list_file
);
222 fput(syscall_list_file
);
224 put_unused_fd(file_fd
);
231 void lttng_abi_tracer_version(struct lttng_kernel_tracer_version
*v
)
233 v
->major
= LTTNG_MODULES_MAJOR_VERSION
;
234 v
->minor
= LTTNG_MODULES_MINOR_VERSION
;
235 v
->patchlevel
= LTTNG_MODULES_PATCHLEVEL_VERSION
;
239 void lttng_abi_tracer_abi_version(struct lttng_kernel_tracer_abi_version
*v
)
241 v
->major
= LTTNG_MODULES_ABI_MAJOR_VERSION
;
242 v
->minor
= LTTNG_MODULES_ABI_MINOR_VERSION
;
246 long lttng_abi_add_context(struct file
*file
,
247 struct lttng_kernel_context
*context_param
,
248 struct lttng_ctx
**ctx
, struct lttng_session
*session
)
251 if (session
->been_active
)
254 switch (context_param
->ctx
) {
255 case LTTNG_KERNEL_CONTEXT_PID
:
256 return lttng_add_pid_to_ctx(ctx
);
257 case LTTNG_KERNEL_CONTEXT_PRIO
:
258 return lttng_add_prio_to_ctx(ctx
);
259 case LTTNG_KERNEL_CONTEXT_NICE
:
260 return lttng_add_nice_to_ctx(ctx
);
261 case LTTNG_KERNEL_CONTEXT_VPID
:
262 return lttng_add_vpid_to_ctx(ctx
);
263 case LTTNG_KERNEL_CONTEXT_TID
:
264 return lttng_add_tid_to_ctx(ctx
);
265 case LTTNG_KERNEL_CONTEXT_VTID
:
266 return lttng_add_vtid_to_ctx(ctx
);
267 case LTTNG_KERNEL_CONTEXT_PPID
:
268 return lttng_add_ppid_to_ctx(ctx
);
269 case LTTNG_KERNEL_CONTEXT_VPPID
:
270 return lttng_add_vppid_to_ctx(ctx
);
271 case LTTNG_KERNEL_CONTEXT_PERF_COUNTER
:
272 context_param
->u
.perf_counter
.name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
273 return lttng_add_perf_counter_to_ctx(context_param
->u
.perf_counter
.type
,
274 context_param
->u
.perf_counter
.config
,
275 context_param
->u
.perf_counter
.name
,
277 case LTTNG_KERNEL_CONTEXT_PROCNAME
:
278 return lttng_add_procname_to_ctx(ctx
);
279 case LTTNG_KERNEL_CONTEXT_HOSTNAME
:
280 return lttng_add_hostname_to_ctx(ctx
);
281 case LTTNG_KERNEL_CONTEXT_CPU_ID
:
282 return lttng_add_cpu_id_to_ctx(ctx
);
283 case LTTNG_KERNEL_CONTEXT_INTERRUPTIBLE
:
284 return lttng_add_interruptible_to_ctx(ctx
);
285 case LTTNG_KERNEL_CONTEXT_NEED_RESCHEDULE
:
286 return lttng_add_need_reschedule_to_ctx(ctx
);
287 case LTTNG_KERNEL_CONTEXT_PREEMPTIBLE
:
288 return lttng_add_preemptible_to_ctx(ctx
);
289 case LTTNG_KERNEL_CONTEXT_MIGRATABLE
:
290 return lttng_add_migratable_to_ctx(ctx
);
291 case LTTNG_KERNEL_CONTEXT_CALLSTACK_KERNEL
:
292 case LTTNG_KERNEL_CONTEXT_CALLSTACK_USER
:
293 return lttng_add_callstack_to_ctx(ctx
, context_param
->ctx
);
294 case LTTNG_KERNEL_CONTEXT_CGROUP_NS
:
295 return lttng_add_cgroup_ns_to_ctx(ctx
);
296 case LTTNG_KERNEL_CONTEXT_IPC_NS
:
297 return lttng_add_ipc_ns_to_ctx(ctx
);
298 case LTTNG_KERNEL_CONTEXT_MNT_NS
:
299 return lttng_add_mnt_ns_to_ctx(ctx
);
300 case LTTNG_KERNEL_CONTEXT_NET_NS
:
301 return lttng_add_net_ns_to_ctx(ctx
);
302 case LTTNG_KERNEL_CONTEXT_PID_NS
:
303 return lttng_add_pid_ns_to_ctx(ctx
);
304 case LTTNG_KERNEL_CONTEXT_USER_NS
:
305 return lttng_add_user_ns_to_ctx(ctx
);
306 case LTTNG_KERNEL_CONTEXT_UTS_NS
:
307 return lttng_add_uts_ns_to_ctx(ctx
);
308 case LTTNG_KERNEL_CONTEXT_UID
:
309 return lttng_add_uid_to_ctx(ctx
);
310 case LTTNG_KERNEL_CONTEXT_EUID
:
311 return lttng_add_euid_to_ctx(ctx
);
312 case LTTNG_KERNEL_CONTEXT_SUID
:
313 return lttng_add_suid_to_ctx(ctx
);
314 case LTTNG_KERNEL_CONTEXT_GID
:
315 return lttng_add_gid_to_ctx(ctx
);
316 case LTTNG_KERNEL_CONTEXT_EGID
:
317 return lttng_add_egid_to_ctx(ctx
);
318 case LTTNG_KERNEL_CONTEXT_SGID
:
319 return lttng_add_sgid_to_ctx(ctx
);
320 case LTTNG_KERNEL_CONTEXT_VUID
:
321 return lttng_add_vuid_to_ctx(ctx
);
322 case LTTNG_KERNEL_CONTEXT_VEUID
:
323 return lttng_add_veuid_to_ctx(ctx
);
324 case LTTNG_KERNEL_CONTEXT_VSUID
:
325 return lttng_add_vsuid_to_ctx(ctx
);
326 case LTTNG_KERNEL_CONTEXT_VGID
:
327 return lttng_add_vgid_to_ctx(ctx
);
328 case LTTNG_KERNEL_CONTEXT_VEGID
:
329 return lttng_add_vegid_to_ctx(ctx
);
330 case LTTNG_KERNEL_CONTEXT_VSGID
:
331 return lttng_add_vsgid_to_ctx(ctx
);
332 case LTTNG_KERNEL_CONTEXT_TIME_NS
:
333 return lttng_add_time_ns_to_ctx(ctx
);
340 * lttng_ioctl - lttng syscall through ioctl
346 * This ioctl implements lttng commands:
347 * LTTNG_KERNEL_SESSION
348 * Returns a LTTng trace session file descriptor
349 * LTTNG_KERNEL_TRACER_VERSION
350 * Returns the LTTng kernel tracer version
351 * LTTNG_KERNEL_TRACEPOINT_LIST
352 * Returns a file descriptor listing available tracepoints
353 * LTTNG_KERNEL_WAIT_QUIESCENT
354 * Returns after all previously running probes have completed
355 * LTTNG_KERNEL_TRACER_ABI_VERSION
356 * Returns the LTTng kernel tracer ABI version
357 * LTTNG_KERNEL_EVENT_NOTIFIER_GROUP_CREATE
358 * Returns a LTTng event notifier group file descriptor
360 * The returned session will be deleted when its file descriptor is closed.
363 long lttng_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
366 case LTTNG_KERNEL_OLD_SESSION
:
367 case LTTNG_KERNEL_SESSION
:
368 return lttng_abi_create_session();
369 case LTTNG_KERNEL_EVENT_NOTIFIER_GROUP_CREATE
:
370 return lttng_abi_create_event_notifier_group();
371 case LTTNG_KERNEL_OLD_TRACER_VERSION
:
373 struct lttng_kernel_tracer_version v
;
374 struct lttng_kernel_old_tracer_version oldv
;
375 struct lttng_kernel_old_tracer_version
*uversion
=
376 (struct lttng_kernel_old_tracer_version __user
*) arg
;
378 lttng_abi_tracer_version(&v
);
379 oldv
.major
= v
.major
;
380 oldv
.minor
= v
.minor
;
381 oldv
.patchlevel
= v
.patchlevel
;
383 if (copy_to_user(uversion
, &oldv
, sizeof(oldv
)))
387 case LTTNG_KERNEL_TRACER_VERSION
:
389 struct lttng_kernel_tracer_version version
;
390 struct lttng_kernel_tracer_version
*uversion
=
391 (struct lttng_kernel_tracer_version __user
*) arg
;
393 lttng_abi_tracer_version(&version
);
395 if (copy_to_user(uversion
, &version
, sizeof(version
)))
399 case LTTNG_KERNEL_TRACER_ABI_VERSION
:
401 struct lttng_kernel_tracer_abi_version version
;
402 struct lttng_kernel_tracer_abi_version
*uversion
=
403 (struct lttng_kernel_tracer_abi_version __user
*) arg
;
405 lttng_abi_tracer_abi_version(&version
);
407 if (copy_to_user(uversion
, &version
, sizeof(version
)))
411 case LTTNG_KERNEL_OLD_TRACEPOINT_LIST
:
412 case LTTNG_KERNEL_TRACEPOINT_LIST
:
413 return lttng_abi_tracepoint_list();
414 case LTTNG_KERNEL_SYSCALL_LIST
:
415 return lttng_abi_syscall_list();
416 case LTTNG_KERNEL_OLD_WAIT_QUIESCENT
:
417 case LTTNG_KERNEL_WAIT_QUIESCENT
:
420 case LTTNG_KERNEL_OLD_CALIBRATE
:
422 struct lttng_kernel_old_calibrate __user
*ucalibrate
=
423 (struct lttng_kernel_old_calibrate __user
*) arg
;
424 struct lttng_kernel_old_calibrate old_calibrate
;
425 struct lttng_kernel_calibrate calibrate
;
428 if (copy_from_user(&old_calibrate
, ucalibrate
, sizeof(old_calibrate
)))
430 calibrate
.type
= old_calibrate
.type
;
431 ret
= lttng_calibrate(&calibrate
);
432 if (copy_to_user(ucalibrate
, &old_calibrate
, sizeof(old_calibrate
)))
436 case LTTNG_KERNEL_CALIBRATE
:
438 struct lttng_kernel_calibrate __user
*ucalibrate
=
439 (struct lttng_kernel_calibrate __user
*) arg
;
440 struct lttng_kernel_calibrate calibrate
;
443 if (copy_from_user(&calibrate
, ucalibrate
, sizeof(calibrate
)))
445 ret
= lttng_calibrate(&calibrate
);
446 if (copy_to_user(ucalibrate
, &calibrate
, sizeof(calibrate
)))
455 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(5,6,0))
456 static const struct proc_ops lttng_proc_ops
= {
457 .proc_ioctl
= lttng_ioctl
,
459 .proc_compat_ioctl
= lttng_ioctl
,
460 #endif /* CONFIG_COMPAT */
463 static const struct file_operations lttng_proc_ops
= {
464 .owner
= THIS_MODULE
,
465 .unlocked_ioctl
= lttng_ioctl
,
467 .compat_ioctl
= lttng_ioctl
,
468 #endif /* CONFIG_COMPAT */
473 int lttng_abi_create_channel(struct file
*session_file
,
474 struct lttng_kernel_channel
*chan_param
,
475 enum channel_type channel_type
)
477 struct lttng_session
*session
= session_file
->private_data
;
478 const struct file_operations
*fops
= NULL
;
479 const char *transport_name
;
480 struct lttng_channel
*chan
;
481 struct file
*chan_file
;
485 chan_fd
= lttng_get_unused_fd();
490 switch (channel_type
) {
491 case PER_CPU_CHANNEL
:
492 fops
= <tng_channel_fops
;
494 case METADATA_CHANNEL
:
495 fops
= <tng_metadata_fops
;
499 chan_file
= anon_inode_getfile("[lttng_channel]",
502 if (IS_ERR(chan_file
)) {
503 ret
= PTR_ERR(chan_file
);
506 switch (channel_type
) {
507 case PER_CPU_CHANNEL
:
508 if (chan_param
->output
== LTTNG_KERNEL_SPLICE
) {
509 transport_name
= chan_param
->overwrite
?
510 "relay-overwrite" : "relay-discard";
511 } else if (chan_param
->output
== LTTNG_KERNEL_MMAP
) {
512 transport_name
= chan_param
->overwrite
?
513 "relay-overwrite-mmap" : "relay-discard-mmap";
518 case METADATA_CHANNEL
:
519 if (chan_param
->output
== LTTNG_KERNEL_SPLICE
)
520 transport_name
= "relay-metadata";
521 else if (chan_param
->output
== LTTNG_KERNEL_MMAP
)
522 transport_name
= "relay-metadata-mmap";
527 transport_name
= "<unknown>";
530 if (!atomic_long_add_unless(&session_file
->f_count
, 1, LONG_MAX
)) {
535 * We tolerate no failure path after channel creation. It will stay
536 * invariant for the rest of the session.
538 chan
= lttng_channel_create(session
, transport_name
, NULL
,
539 chan_param
->subbuf_size
,
540 chan_param
->num_subbuf
,
541 chan_param
->switch_timer_interval
,
542 chan_param
->read_timer_interval
,
548 chan
->file
= chan_file
;
549 chan_file
->private_data
= chan
;
550 fd_install(chan_fd
, chan_file
);
555 atomic_long_dec(&session_file
->f_count
);
559 put_unused_fd(chan_fd
);
565 int lttng_abi_session_set_name(struct lttng_session
*session
,
566 struct lttng_kernel_session_name
*name
)
570 len
= strnlen(name
->name
, LTTNG_KERNEL_SESSION_NAME_LEN
);
572 if (len
== LTTNG_KERNEL_SESSION_NAME_LEN
) {
573 /* Name is too long/malformed */
577 strcpy(session
->name
, name
->name
);
582 int lttng_abi_session_set_creation_time(struct lttng_session
*session
,
583 struct lttng_kernel_session_creation_time
*time
)
587 len
= strnlen(time
->iso8601
, LTTNG_KERNEL_SESSION_CREATION_TIME_ISO8601_LEN
);
589 if (len
== LTTNG_KERNEL_SESSION_CREATION_TIME_ISO8601_LEN
) {
590 /* Time is too long/malformed */
594 strcpy(session
->creation_time
, time
->iso8601
);
599 enum tracker_type
get_tracker_type(struct lttng_kernel_tracker_args
*tracker
)
601 switch (tracker
->type
) {
602 case LTTNG_KERNEL_TRACKER_PID
:
604 case LTTNG_KERNEL_TRACKER_VPID
:
606 case LTTNG_KERNEL_TRACKER_UID
:
608 case LTTNG_KERNEL_TRACKER_VUID
:
610 case LTTNG_KERNEL_TRACKER_GID
:
612 case LTTNG_KERNEL_TRACKER_VGID
:
615 return TRACKER_UNKNOWN
;
620 * lttng_session_ioctl - lttng session fd ioctl
626 * This ioctl implements lttng commands:
627 * LTTNG_KERNEL_CHANNEL
628 * Returns a LTTng channel file descriptor
629 * LTTNG_KERNEL_ENABLE
630 * Enables tracing for a session (weak enable)
631 * LTTNG_KERNEL_DISABLE
632 * Disables tracing for a session (strong disable)
633 * LTTNG_KERNEL_METADATA
634 * Returns a LTTng metadata file descriptor
635 * LTTNG_KERNEL_SESSION_TRACK_PID
636 * Add PID to session PID tracker
637 * LTTNG_KERNEL_SESSION_UNTRACK_PID
638 * Remove PID from session PID tracker
639 * LTTNG_KERNEL_SESSION_TRACK_ID
641 * LTTNG_KERNEL_SESSION_UNTRACK_ID
642 * Remove ID from tracker
644 * The returned channel will be deleted when its file descriptor is closed.
647 long lttng_session_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
649 struct lttng_session
*session
= file
->private_data
;
650 struct lttng_kernel_channel chan_param
;
651 struct lttng_kernel_old_channel old_chan_param
;
654 case LTTNG_KERNEL_OLD_CHANNEL
:
656 if (copy_from_user(&old_chan_param
,
657 (struct lttng_kernel_old_channel __user
*) arg
,
658 sizeof(struct lttng_kernel_old_channel
)))
660 chan_param
.overwrite
= old_chan_param
.overwrite
;
661 chan_param
.subbuf_size
= old_chan_param
.subbuf_size
;
662 chan_param
.num_subbuf
= old_chan_param
.num_subbuf
;
663 chan_param
.switch_timer_interval
= old_chan_param
.switch_timer_interval
;
664 chan_param
.read_timer_interval
= old_chan_param
.read_timer_interval
;
665 chan_param
.output
= old_chan_param
.output
;
667 return lttng_abi_create_channel(file
, &chan_param
,
670 case LTTNG_KERNEL_CHANNEL
:
672 if (copy_from_user(&chan_param
,
673 (struct lttng_kernel_channel __user
*) arg
,
674 sizeof(struct lttng_kernel_channel
)))
676 return lttng_abi_create_channel(file
, &chan_param
,
679 case LTTNG_KERNEL_OLD_SESSION_START
:
680 case LTTNG_KERNEL_OLD_ENABLE
:
681 case LTTNG_KERNEL_SESSION_START
:
682 case LTTNG_KERNEL_ENABLE
:
683 return lttng_session_enable(session
);
684 case LTTNG_KERNEL_OLD_SESSION_STOP
:
685 case LTTNG_KERNEL_OLD_DISABLE
:
686 case LTTNG_KERNEL_SESSION_STOP
:
687 case LTTNG_KERNEL_DISABLE
:
688 return lttng_session_disable(session
);
689 case LTTNG_KERNEL_OLD_METADATA
:
691 if (copy_from_user(&old_chan_param
,
692 (struct lttng_kernel_old_channel __user
*) arg
,
693 sizeof(struct lttng_kernel_old_channel
)))
695 chan_param
.overwrite
= old_chan_param
.overwrite
;
696 chan_param
.subbuf_size
= old_chan_param
.subbuf_size
;
697 chan_param
.num_subbuf
= old_chan_param
.num_subbuf
;
698 chan_param
.switch_timer_interval
= old_chan_param
.switch_timer_interval
;
699 chan_param
.read_timer_interval
= old_chan_param
.read_timer_interval
;
700 chan_param
.output
= old_chan_param
.output
;
702 return lttng_abi_create_channel(file
, &chan_param
,
705 case LTTNG_KERNEL_METADATA
:
707 if (copy_from_user(&chan_param
,
708 (struct lttng_kernel_channel __user
*) arg
,
709 sizeof(struct lttng_kernel_channel
)))
711 return lttng_abi_create_channel(file
, &chan_param
,
714 case LTTNG_KERNEL_SESSION_TRACK_PID
:
715 return lttng_session_track_id(session
, TRACKER_PID
, (int) arg
);
716 case LTTNG_KERNEL_SESSION_UNTRACK_PID
:
717 return lttng_session_untrack_id(session
, TRACKER_PID
, (int) arg
);
718 case LTTNG_KERNEL_SESSION_TRACK_ID
:
720 struct lttng_kernel_tracker_args tracker
;
721 enum tracker_type tracker_type
;
723 if (copy_from_user(&tracker
,
724 (struct lttng_kernel_tracker_args __user
*) arg
,
725 sizeof(struct lttng_kernel_tracker_args
)))
727 tracker_type
= get_tracker_type(&tracker
);
728 if (tracker_type
== TRACKER_UNKNOWN
)
730 return lttng_session_track_id(session
, tracker_type
, tracker
.id
);
732 case LTTNG_KERNEL_SESSION_UNTRACK_ID
:
734 struct lttng_kernel_tracker_args tracker
;
735 enum tracker_type tracker_type
;
737 if (copy_from_user(&tracker
,
738 (struct lttng_kernel_tracker_args __user
*) arg
,
739 sizeof(struct lttng_kernel_tracker_args
)))
741 tracker_type
= get_tracker_type(&tracker
);
742 if (tracker_type
== TRACKER_UNKNOWN
)
744 return lttng_session_untrack_id(session
, tracker_type
,
747 case LTTNG_KERNEL_SESSION_LIST_TRACKER_PIDS
:
748 return lttng_session_list_tracker_ids(session
, TRACKER_PID
);
749 case LTTNG_KERNEL_SESSION_LIST_TRACKER_IDS
:
751 struct lttng_kernel_tracker_args tracker
;
752 enum tracker_type tracker_type
;
754 if (copy_from_user(&tracker
,
755 (struct lttng_kernel_tracker_args __user
*) arg
,
756 sizeof(struct lttng_kernel_tracker_args
)))
758 tracker_type
= get_tracker_type(&tracker
);
759 if (tracker_type
== TRACKER_UNKNOWN
)
761 return lttng_session_list_tracker_ids(session
, tracker_type
);
763 case LTTNG_KERNEL_SESSION_METADATA_REGEN
:
764 return lttng_session_metadata_regenerate(session
);
765 case LTTNG_KERNEL_SESSION_STATEDUMP
:
766 return lttng_session_statedump(session
);
767 case LTTNG_KERNEL_SESSION_SET_NAME
:
769 struct lttng_kernel_session_name name
;
771 if (copy_from_user(&name
,
772 (struct lttng_kernel_session_name __user
*) arg
,
773 sizeof(struct lttng_kernel_session_name
)))
775 return lttng_abi_session_set_name(session
, &name
);
777 case LTTNG_KERNEL_SESSION_SET_CREATION_TIME
:
779 struct lttng_kernel_session_creation_time time
;
781 if (copy_from_user(&time
,
782 (struct lttng_kernel_session_creation_time __user
*) arg
,
783 sizeof(struct lttng_kernel_session_creation_time
)))
785 return lttng_abi_session_set_creation_time(session
, &time
);
793 * Called when the last file reference is dropped.
795 * Big fat note: channels and events are invariant for the whole session after
796 * their creation. So this session destruction also destroys all channel and
797 * event structures specific to this session (they are not destroyed when their
798 * individual file is released).
801 int lttng_session_release(struct inode
*inode
, struct file
*file
)
803 struct lttng_session
*session
= file
->private_data
;
806 lttng_session_destroy(session
);
810 static const struct file_operations lttng_session_fops
= {
811 .owner
= THIS_MODULE
,
812 .release
= lttng_session_release
,
813 .unlocked_ioctl
= lttng_session_ioctl
,
815 .compat_ioctl
= lttng_session_ioctl
,
820 * When encountering empty buffer, flush current sub-buffer if non-empty
821 * and retry (if new data available to read after flush).
824 ssize_t
lttng_event_notifier_group_notif_read(struct file
*filp
, char __user
*user_buf
,
825 size_t count
, loff_t
*ppos
)
827 struct lttng_event_notifier_group
*event_notifier_group
= filp
->private_data
;
828 struct channel
*chan
= event_notifier_group
->chan
;
829 struct lib_ring_buffer
*buf
= event_notifier_group
->buf
;
830 ssize_t read_count
= 0, len
;
834 if (!lttng_access_ok(VERIFY_WRITE
, user_buf
, count
))
837 /* Finish copy of previous record */
839 if (read_count
< count
) {
840 len
= chan
->iter
.len_left
;
846 while (read_count
< count
) {
847 size_t copy_len
, space_left
;
849 len
= lib_ring_buffer_get_next_record(chan
, buf
);
853 * Check if buffer is finalized (end of file).
855 if (len
== -ENODATA
) {
856 /* A 0 read_count will tell about end of file */
859 if (filp
->f_flags
& O_NONBLOCK
) {
861 read_count
= -EAGAIN
;
867 * No data available at the moment, return what
874 * Wait for returned len to be >= 0 or -ENODATA.
876 error
= wait_event_interruptible(
877 event_notifier_group
->read_wait
,
878 ((len
= lib_ring_buffer_get_next_record(
879 chan
, buf
)), len
!= -EAGAIN
));
880 CHAN_WARN_ON(chan
, len
== -EBUSY
);
885 CHAN_WARN_ON(chan
, len
< 0 && len
!= -ENODATA
);
889 read_offset
= buf
->iter
.read_offset
;
891 space_left
= count
- read_count
;
892 if (len
<= space_left
) {
894 chan
->iter
.len_left
= 0;
897 copy_len
= space_left
;
898 chan
->iter
.len_left
= len
- copy_len
;
899 *ppos
= read_offset
+ copy_len
;
901 if (__lib_ring_buffer_copy_to_user(&buf
->backend
, read_offset
,
902 &user_buf
[read_count
],
905 * Leave the len_left and ppos values at their current
906 * state, as we currently have a valid event to read.
910 read_count
+= copy_len
;
916 chan
->iter
.len_left
= 0;
919 lib_ring_buffer_put_current_record(buf
);
924 * If the ring buffer is non empty (even just a partial subbuffer), return that
925 * there is data available. Perform a ring buffer flush if we encounter a
926 * non-empty ring buffer which does not have any consumeable subbuffer available.
929 unsigned int lttng_event_notifier_group_notif_poll(struct file
*filp
,
932 unsigned int mask
= 0;
933 struct lttng_event_notifier_group
*event_notifier_group
= filp
->private_data
;
934 struct channel
*chan
= event_notifier_group
->chan
;
935 struct lib_ring_buffer
*buf
= event_notifier_group
->buf
;
936 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
937 int finalized
, disabled
;
938 unsigned long consumed
, offset
;
939 size_t subbuffer_header_size
= config
->cb
.subbuffer_header_size();
941 if (filp
->f_mode
& FMODE_READ
) {
942 poll_wait_set_exclusive(wait
);
943 poll_wait(filp
, &event_notifier_group
->read_wait
, wait
);
945 finalized
= lib_ring_buffer_is_finalized(config
, buf
);
946 disabled
= lib_ring_buffer_channel_is_disabled(chan
);
949 * lib_ring_buffer_is_finalized() contains a smp_rmb() ordering
950 * finalized load before offsets loads.
952 WARN_ON(atomic_long_read(&buf
->active_readers
) != 1);
957 offset
= lib_ring_buffer_get_offset(config
, buf
);
958 consumed
= lib_ring_buffer_get_consumed(config
, buf
);
961 * If there is no buffer available to consume.
963 if (subbuf_trunc(offset
, chan
) - subbuf_trunc(consumed
, chan
) == 0) {
965 * If there is a non-empty subbuffer, flush and try again.
967 if (subbuf_offset(offset
, chan
) > subbuffer_header_size
) {
968 lib_ring_buffer_switch_remote(buf
);
976 * The memory barriers
977 * __wait_event()/wake_up_interruptible() take
978 * care of "raw_spin_is_locked" memory ordering.
980 if (raw_spin_is_locked(&buf
->raw_tick_nohz_spinlock
))
986 if (subbuf_trunc(offset
, chan
) - subbuf_trunc(consumed
, chan
)
987 >= chan
->backend
.buf_size
)
988 return POLLPRI
| POLLRDBAND
;
990 return POLLIN
| POLLRDNORM
;
998 * lttng_event_notifier_group_notif_open - event_notifier ring buffer open file operation
999 * @inode: opened inode
1000 * @file: opened file
1002 * Open implementation. Makes sure only one open instance of a buffer is
1003 * done at a given moment.
1005 static int lttng_event_notifier_group_notif_open(struct inode
*inode
, struct file
*file
)
1007 struct lttng_event_notifier_group
*event_notifier_group
= inode
->i_private
;
1008 struct lib_ring_buffer
*buf
= event_notifier_group
->buf
;
1010 file
->private_data
= event_notifier_group
;
1011 return lib_ring_buffer_open(inode
, file
, buf
);
1015 * lttng_event_notifier_group_notif_release - event_notifier ring buffer release file operation
1016 * @inode: opened inode
1017 * @file: opened file
1019 * Release implementation.
1021 static int lttng_event_notifier_group_notif_release(struct inode
*inode
, struct file
*file
)
1023 struct lttng_event_notifier_group
*event_notifier_group
= file
->private_data
;
1024 struct lib_ring_buffer
*buf
= event_notifier_group
->buf
;
1027 ret
= lib_ring_buffer_release(inode
, file
, buf
);
1030 fput(event_notifier_group
->file
);
1034 static const struct file_operations lttng_event_notifier_group_notif_fops
= {
1035 .owner
= THIS_MODULE
,
1036 .open
= lttng_event_notifier_group_notif_open
,
1037 .release
= lttng_event_notifier_group_notif_release
,
1038 .read
= lttng_event_notifier_group_notif_read
,
1039 .poll
= lttng_event_notifier_group_notif_poll
,
1043 * lttng_metadata_ring_buffer_poll - LTTng ring buffer poll file operation
1047 * Handles the poll operations for the metadata channels.
1050 unsigned int lttng_metadata_ring_buffer_poll(struct file
*filp
,
1053 struct lttng_metadata_stream
*stream
= filp
->private_data
;
1054 struct lib_ring_buffer
*buf
= stream
->priv
;
1056 unsigned int mask
= 0;
1058 if (filp
->f_mode
& FMODE_READ
) {
1059 poll_wait_set_exclusive(wait
);
1060 poll_wait(filp
, &stream
->read_wait
, wait
);
1062 finalized
= stream
->finalized
;
1065 * lib_ring_buffer_is_finalized() contains a smp_rmb()
1066 * ordering finalized load before offsets loads.
1068 WARN_ON(atomic_long_read(&buf
->active_readers
) != 1);
1073 mutex_lock(&stream
->metadata_cache
->lock
);
1074 if (stream
->metadata_cache
->metadata_written
>
1075 stream
->metadata_out
)
1077 mutex_unlock(&stream
->metadata_cache
->lock
);
1084 void lttng_metadata_ring_buffer_ioctl_put_next_subbuf(struct file
*filp
,
1085 unsigned int cmd
, unsigned long arg
)
1087 struct lttng_metadata_stream
*stream
= filp
->private_data
;
1089 stream
->metadata_out
= stream
->metadata_in
;
1093 * Reset the counter of how much metadata has been consumed to 0. That way,
1094 * the consumer receives the content of the metadata cache unchanged. This is
1095 * different from the metadata_regenerate where the offset from epoch is
1096 * resampled, here we want the exact same content as the last time the metadata
1097 * was generated. This command is only possible if all the metadata written
1098 * in the cache has been output to the metadata stream to avoid corrupting the
1101 * Return 0 on success, a negative value on error.
1104 int lttng_metadata_cache_dump(struct lttng_metadata_stream
*stream
)
1107 struct lttng_metadata_cache
*cache
= stream
->metadata_cache
;
1109 mutex_lock(&cache
->lock
);
1110 if (stream
->metadata_out
!= cache
->metadata_written
) {
1114 stream
->metadata_out
= 0;
1115 stream
->metadata_in
= 0;
1116 wake_up_interruptible(&stream
->read_wait
);
1120 mutex_unlock(&cache
->lock
);
1125 long lttng_metadata_ring_buffer_ioctl(struct file
*filp
,
1126 unsigned int cmd
, unsigned long arg
)
1129 struct lttng_metadata_stream
*stream
= filp
->private_data
;
1130 struct lib_ring_buffer
*buf
= stream
->priv
;
1131 unsigned int rb_cmd
;
1134 if (cmd
== RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK
)
1135 rb_cmd
= RING_BUFFER_GET_NEXT_SUBBUF
;
1140 case RING_BUFFER_GET_NEXT_SUBBUF
:
1142 struct lttng_metadata_stream
*stream
= filp
->private_data
;
1143 struct lib_ring_buffer
*buf
= stream
->priv
;
1144 struct channel
*chan
= buf
->backend
.chan
;
1146 ret
= lttng_metadata_output_channel(stream
, chan
, NULL
);
1148 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
1154 case RING_BUFFER_GET_SUBBUF
:
1157 * Random access is not allowed for metadata channel.
1161 case RING_BUFFER_FLUSH_EMPTY
: /* Fall-through. */
1162 case RING_BUFFER_FLUSH
:
1164 struct lttng_metadata_stream
*stream
= filp
->private_data
;
1165 struct lib_ring_buffer
*buf
= stream
->priv
;
1166 struct channel
*chan
= buf
->backend
.chan
;
1169 * Before doing the actual ring buffer flush, write up to one
1170 * packet of metadata in the ring buffer.
1172 ret
= lttng_metadata_output_channel(stream
, chan
, NULL
);
1177 case RING_BUFFER_GET_METADATA_VERSION
:
1179 struct lttng_metadata_stream
*stream
= filp
->private_data
;
1181 return put_u64(stream
->version
, arg
);
1183 case RING_BUFFER_METADATA_CACHE_DUMP
:
1185 struct lttng_metadata_stream
*stream
= filp
->private_data
;
1187 return lttng_metadata_cache_dump(stream
);
1189 case RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK
:
1191 struct lttng_metadata_stream
*stream
= filp
->private_data
;
1192 struct lib_ring_buffer
*buf
= stream
->priv
;
1193 struct channel
*chan
= buf
->backend
.chan
;
1195 ret
= lttng_metadata_output_channel(stream
, chan
, &coherent
);
1197 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
1199 } else if (ret
< 0) {
1207 /* PUT_SUBBUF is the one from lib ring buffer, unmodified. */
1209 /* Performing lib ring buffer ioctl after our own. */
1210 ret
= lib_ring_buffer_ioctl(filp
, rb_cmd
, arg
, buf
);
1215 case RING_BUFFER_PUT_NEXT_SUBBUF
:
1217 lttng_metadata_ring_buffer_ioctl_put_next_subbuf(filp
,
1221 case RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK
:
1223 return put_u32(coherent
, arg
);
1232 #ifdef CONFIG_COMPAT
1234 long lttng_metadata_ring_buffer_compat_ioctl(struct file
*filp
,
1235 unsigned int cmd
, unsigned long arg
)
1238 struct lttng_metadata_stream
*stream
= filp
->private_data
;
1239 struct lib_ring_buffer
*buf
= stream
->priv
;
1240 unsigned int rb_cmd
;
1243 if (cmd
== RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK
)
1244 rb_cmd
= RING_BUFFER_GET_NEXT_SUBBUF
;
1249 case RING_BUFFER_GET_NEXT_SUBBUF
:
1251 struct lttng_metadata_stream
*stream
= filp
->private_data
;
1252 struct lib_ring_buffer
*buf
= stream
->priv
;
1253 struct channel
*chan
= buf
->backend
.chan
;
1255 ret
= lttng_metadata_output_channel(stream
, chan
, NULL
);
1257 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
1263 case RING_BUFFER_GET_SUBBUF
:
1266 * Random access is not allowed for metadata channel.
1270 case RING_BUFFER_FLUSH_EMPTY
: /* Fall-through. */
1271 case RING_BUFFER_FLUSH
:
1273 struct lttng_metadata_stream
*stream
= filp
->private_data
;
1274 struct lib_ring_buffer
*buf
= stream
->priv
;
1275 struct channel
*chan
= buf
->backend
.chan
;
1278 * Before doing the actual ring buffer flush, write up to one
1279 * packet of metadata in the ring buffer.
1281 ret
= lttng_metadata_output_channel(stream
, chan
, NULL
);
1286 case RING_BUFFER_GET_METADATA_VERSION
:
1288 struct lttng_metadata_stream
*stream
= filp
->private_data
;
1290 return put_u64(stream
->version
, arg
);
1292 case RING_BUFFER_METADATA_CACHE_DUMP
:
1294 struct lttng_metadata_stream
*stream
= filp
->private_data
;
1296 return lttng_metadata_cache_dump(stream
);
1298 case RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK
:
1300 struct lttng_metadata_stream
*stream
= filp
->private_data
;
1301 struct lib_ring_buffer
*buf
= stream
->priv
;
1302 struct channel
*chan
= buf
->backend
.chan
;
1304 ret
= lttng_metadata_output_channel(stream
, chan
, &coherent
);
1306 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
1308 } else if (ret
< 0) {
1316 /* PUT_SUBBUF is the one from lib ring buffer, unmodified. */
1318 /* Performing lib ring buffer ioctl after our own. */
1319 ret
= lib_ring_buffer_compat_ioctl(filp
, rb_cmd
, arg
, buf
);
1324 case RING_BUFFER_PUT_NEXT_SUBBUF
:
1326 lttng_metadata_ring_buffer_ioctl_put_next_subbuf(filp
,
1330 case RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK
:
1332 return put_u32(coherent
, arg
);
1343 * This is not used by anonymous file descriptors. This code is left
1344 * there if we ever want to implement an inode with open() operation.
1347 int lttng_metadata_ring_buffer_open(struct inode
*inode
, struct file
*file
)
1349 struct lttng_metadata_stream
*stream
= inode
->i_private
;
1350 struct lib_ring_buffer
*buf
= stream
->priv
;
1352 file
->private_data
= buf
;
1354 * Since life-time of metadata cache differs from that of
1355 * session, we need to keep our own reference on the transport.
1357 if (!try_module_get(stream
->transport
->owner
)) {
1358 printk(KERN_WARNING
"LTTng: Can't lock transport module.\n");
1361 return lib_ring_buffer_open(inode
, file
, buf
);
1365 int lttng_metadata_ring_buffer_release(struct inode
*inode
, struct file
*file
)
1367 struct lttng_metadata_stream
*stream
= file
->private_data
;
1368 struct lib_ring_buffer
*buf
= stream
->priv
;
1370 mutex_lock(&stream
->metadata_cache
->lock
);
1371 list_del(&stream
->list
);
1372 mutex_unlock(&stream
->metadata_cache
->lock
);
1373 kref_put(&stream
->metadata_cache
->refcount
, metadata_cache_destroy
);
1374 module_put(stream
->transport
->owner
);
1376 return lib_ring_buffer_release(inode
, file
, buf
);
1380 ssize_t
lttng_metadata_ring_buffer_splice_read(struct file
*in
, loff_t
*ppos
,
1381 struct pipe_inode_info
*pipe
, size_t len
,
1384 struct lttng_metadata_stream
*stream
= in
->private_data
;
1385 struct lib_ring_buffer
*buf
= stream
->priv
;
1387 return lib_ring_buffer_splice_read(in
, ppos
, pipe
, len
,
1392 int lttng_metadata_ring_buffer_mmap(struct file
*filp
,
1393 struct vm_area_struct
*vma
)
1395 struct lttng_metadata_stream
*stream
= filp
->private_data
;
1396 struct lib_ring_buffer
*buf
= stream
->priv
;
1398 return lib_ring_buffer_mmap(filp
, vma
, buf
);
1402 const struct file_operations lttng_metadata_ring_buffer_file_operations
= {
1403 .owner
= THIS_MODULE
,
1404 .open
= lttng_metadata_ring_buffer_open
,
1405 .release
= lttng_metadata_ring_buffer_release
,
1406 .poll
= lttng_metadata_ring_buffer_poll
,
1407 .splice_read
= lttng_metadata_ring_buffer_splice_read
,
1408 .mmap
= lttng_metadata_ring_buffer_mmap
,
1409 .unlocked_ioctl
= lttng_metadata_ring_buffer_ioctl
,
1410 .llseek
= vfs_lib_ring_buffer_no_llseek
,
1411 #ifdef CONFIG_COMPAT
1412 .compat_ioctl
= lttng_metadata_ring_buffer_compat_ioctl
,
1417 int lttng_abi_create_stream_fd(struct file
*channel_file
, void *stream_priv
,
1418 const struct file_operations
*fops
, const char *name
)
1421 struct file
*stream_file
;
1423 stream_fd
= lttng_get_unused_fd();
1424 if (stream_fd
< 0) {
1428 stream_file
= anon_inode_getfile(name
, fops
, stream_priv
, O_RDWR
);
1429 if (IS_ERR(stream_file
)) {
1430 ret
= PTR_ERR(stream_file
);
1434 * OPEN_FMODE, called within anon_inode_getfile/alloc_file, don't honor
1435 * FMODE_LSEEK, FMODE_PREAD nor FMODE_PWRITE. We need to read from this
1436 * file descriptor, so we set FMODE_PREAD here.
1438 stream_file
->f_mode
|= FMODE_PREAD
;
1439 fd_install(stream_fd
, stream_file
);
1441 * The stream holds a reference to the channel within the generic ring
1442 * buffer library, so no need to hold a refcount on the channel and
1443 * session files here.
1448 put_unused_fd(stream_fd
);
1454 int lttng_abi_open_stream(struct file
*channel_file
)
1456 struct lttng_channel
*channel
= channel_file
->private_data
;
1457 struct lib_ring_buffer
*buf
;
1461 buf
= channel
->ops
->buffer_read_open(channel
->chan
);
1466 ret
= lttng_abi_create_stream_fd(channel_file
, stream_priv
,
1467 <tng_stream_ring_buffer_file_operations
,
1475 channel
->ops
->buffer_read_close(buf
);
1480 int lttng_abi_open_metadata_stream(struct file
*channel_file
)
1482 struct lttng_channel
*channel
= channel_file
->private_data
;
1483 struct lttng_session
*session
= channel
->session
;
1484 struct lib_ring_buffer
*buf
;
1486 struct lttng_metadata_stream
*metadata_stream
;
1489 buf
= channel
->ops
->buffer_read_open(channel
->chan
);
1493 metadata_stream
= kzalloc(sizeof(struct lttng_metadata_stream
),
1495 if (!metadata_stream
) {
1499 metadata_stream
->metadata_cache
= session
->metadata_cache
;
1500 init_waitqueue_head(&metadata_stream
->read_wait
);
1501 metadata_stream
->priv
= buf
;
1502 stream_priv
= metadata_stream
;
1503 metadata_stream
->transport
= channel
->transport
;
1504 /* Initial state is an empty metadata, considered as incoherent. */
1505 metadata_stream
->coherent
= false;
1508 * Since life-time of metadata cache differs from that of
1509 * session, we need to keep our own reference on the transport.
1511 if (!try_module_get(metadata_stream
->transport
->owner
)) {
1512 printk(KERN_WARNING
"LTTng: Can't lock transport module.\n");
1517 if (!lttng_kref_get(&session
->metadata_cache
->refcount
)) {
1522 ret
= lttng_abi_create_stream_fd(channel_file
, stream_priv
,
1523 <tng_metadata_ring_buffer_file_operations
,
1524 "[lttng_metadata_stream]");
1528 mutex_lock(&session
->metadata_cache
->lock
);
1529 list_add(&metadata_stream
->list
,
1530 &session
->metadata_cache
->metadata_stream
);
1531 mutex_unlock(&session
->metadata_cache
->lock
);
1535 kref_put(&session
->metadata_cache
->refcount
, metadata_cache_destroy
);
1537 module_put(metadata_stream
->transport
->owner
);
1539 kfree(metadata_stream
);
1541 channel
->ops
->buffer_read_close(buf
);
1546 int lttng_abi_open_event_notifier_group_stream(struct file
*notif_file
)
1548 struct lttng_event_notifier_group
*event_notifier_group
= notif_file
->private_data
;
1549 struct channel
*chan
= event_notifier_group
->chan
;
1550 struct lib_ring_buffer
*buf
;
1554 buf
= event_notifier_group
->ops
->buffer_read_open(chan
);
1558 /* The event_notifier notification fd holds a reference on the event_notifier group */
1559 if (!atomic_long_add_unless(¬if_file
->f_count
, 1, LONG_MAX
)) {
1561 goto refcount_error
;
1563 event_notifier_group
->buf
= buf
;
1564 stream_priv
= event_notifier_group
;
1565 ret
= lttng_abi_create_stream_fd(notif_file
, stream_priv
,
1566 <tng_event_notifier_group_notif_fops
,
1567 "[lttng_event_notifier_stream]");
1574 atomic_long_dec(¬if_file
->f_count
);
1576 event_notifier_group
->ops
->buffer_read_close(buf
);
1581 int lttng_abi_validate_event_param(struct lttng_kernel_event
*event_param
)
1583 /* Limit ABI to implemented features. */
1584 switch (event_param
->instrumentation
) {
1585 case LTTNG_KERNEL_SYSCALL
:
1586 switch (event_param
->u
.syscall
.entryexit
) {
1587 case LTTNG_KERNEL_SYSCALL_ENTRY
:
1588 case LTTNG_KERNEL_SYSCALL_EXIT
:
1589 case LTTNG_KERNEL_SYSCALL_ENTRYEXIT
:
1594 switch (event_param
->u
.syscall
.abi
) {
1595 case LTTNG_KERNEL_SYSCALL_ABI_ALL
:
1600 switch (event_param
->u
.syscall
.match
) {
1601 case LTTNG_KERNEL_SYSCALL_MATCH_NAME
:
1608 case LTTNG_KERNEL_TRACEPOINT
: /* Fallthrough */
1609 case LTTNG_KERNEL_KPROBE
: /* Fallthrough */
1610 case LTTNG_KERNEL_KRETPROBE
: /* Fallthrough */
1611 case LTTNG_KERNEL_NOOP
: /* Fallthrough */
1612 case LTTNG_KERNEL_UPROBE
:
1615 case LTTNG_KERNEL_FUNCTION
: /* Fallthrough */
1623 int lttng_abi_create_event(struct file
*channel_file
,
1624 struct lttng_kernel_event
*event_param
)
1626 struct lttng_channel
*channel
= channel_file
->private_data
;
1628 struct file
*event_file
;
1631 event_param
->name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
1632 switch (event_param
->instrumentation
) {
1633 case LTTNG_KERNEL_KRETPROBE
:
1634 event_param
->u
.kretprobe
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
1636 case LTTNG_KERNEL_KPROBE
:
1637 event_param
->u
.kprobe
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
1639 case LTTNG_KERNEL_FUNCTION
:
1641 /* Not implemented. */
1646 event_fd
= lttng_get_unused_fd();
1651 event_file
= anon_inode_getfile("[lttng_event]",
1654 if (IS_ERR(event_file
)) {
1655 ret
= PTR_ERR(event_file
);
1658 /* The event holds a reference on the channel */
1659 if (!atomic_long_add_unless(&channel_file
->f_count
, 1, LONG_MAX
)) {
1661 goto refcount_error
;
1663 ret
= lttng_abi_validate_event_param(event_param
);
1666 if (event_param
->instrumentation
== LTTNG_KERNEL_TRACEPOINT
1667 || event_param
->instrumentation
== LTTNG_KERNEL_SYSCALL
) {
1668 struct lttng_event_enabler
*event_enabler
;
1670 if (strutils_is_star_glob_pattern(event_param
->name
)) {
1672 * If the event name is a star globbing pattern,
1673 * we create the special star globbing enabler.
1675 event_enabler
= lttng_event_enabler_create(LTTNG_ENABLER_FORMAT_STAR_GLOB
,
1676 event_param
, channel
);
1678 event_enabler
= lttng_event_enabler_create(LTTNG_ENABLER_FORMAT_NAME
,
1679 event_param
, channel
);
1681 priv
= event_enabler
;
1683 struct lttng_event
*event
;
1686 * We tolerate no failure path after event creation. It
1687 * will stay invariant for the rest of the session.
1689 event
= lttng_event_create(channel
, event_param
,
1691 event_param
->instrumentation
);
1692 WARN_ON_ONCE(!event
);
1693 if (IS_ERR(event
)) {
1694 ret
= PTR_ERR(event
);
1699 event_file
->private_data
= priv
;
1700 fd_install(event_fd
, event_file
);
1704 atomic_long_dec(&channel_file
->f_count
);
1708 put_unused_fd(event_fd
);
1714 long lttng_event_notifier_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1716 struct lttng_event_notifier
*event_notifier
;
1717 struct lttng_event_notifier_enabler
*event_notifier_enabler
;
1718 enum lttng_event_type
*evtype
= file
->private_data
;
1721 case LTTNG_KERNEL_ENABLE
:
1723 case LTTNG_TYPE_EVENT
:
1724 event_notifier
= file
->private_data
;
1725 return lttng_event_notifier_enable(event_notifier
);
1726 case LTTNG_TYPE_ENABLER
:
1727 event_notifier_enabler
= file
->private_data
;
1728 return lttng_event_notifier_enabler_enable(event_notifier_enabler
);
1733 case LTTNG_KERNEL_DISABLE
:
1735 case LTTNG_TYPE_EVENT
:
1736 event_notifier
= file
->private_data
;
1737 return lttng_event_notifier_disable(event_notifier
);
1738 case LTTNG_TYPE_ENABLER
:
1739 event_notifier_enabler
= file
->private_data
;
1740 return lttng_event_notifier_enabler_disable(event_notifier_enabler
);
1745 case LTTNG_KERNEL_FILTER
:
1747 case LTTNG_TYPE_EVENT
:
1749 case LTTNG_TYPE_ENABLER
:
1750 event_notifier_enabler
= file
->private_data
;
1751 return lttng_event_notifier_enabler_attach_bytecode(event_notifier_enabler
,
1752 (struct lttng_kernel_filter_bytecode __user
*) arg
);
1757 case LTTNG_KERNEL_ADD_CALLSITE
:
1759 case LTTNG_TYPE_EVENT
:
1760 event_notifier
= file
->private_data
;
1761 return lttng_event_notifier_add_callsite(event_notifier
,
1762 (struct lttng_kernel_event_callsite __user
*) arg
);
1763 case LTTNG_TYPE_ENABLER
:
1770 return -ENOIOCTLCMD
;
1775 int lttng_event_notifier_release(struct inode
*inode
, struct file
*file
)
1777 struct lttng_event_notifier
*event_notifier
;
1778 struct lttng_event_notifier_enabler
*event_notifier_enabler
;
1779 enum lttng_event_type
*evtype
= file
->private_data
;
1785 case LTTNG_TYPE_EVENT
:
1786 event_notifier
= file
->private_data
;
1788 fput(event_notifier
->group
->file
);
1790 case LTTNG_TYPE_ENABLER
:
1791 event_notifier_enabler
= file
->private_data
;
1792 if (event_notifier_enabler
)
1793 fput(event_notifier_enabler
->group
->file
);
1803 static const struct file_operations lttng_event_notifier_fops
= {
1804 .owner
= THIS_MODULE
,
1805 .release
= lttng_event_notifier_release
,
1806 .unlocked_ioctl
= lttng_event_notifier_ioctl
,
1807 #ifdef CONFIG_COMPAT
1808 .compat_ioctl
= lttng_event_notifier_ioctl
,
1813 int lttng_abi_create_event_notifier(struct file
*event_notifier_group_file
,
1814 struct lttng_kernel_event_notifier
*event_notifier_param
)
1816 struct lttng_event_notifier_group
*event_notifier_group
=
1817 event_notifier_group_file
->private_data
;
1818 int event_notifier_fd
, ret
;
1819 struct file
*event_notifier_file
;
1822 switch (event_notifier_param
->event
.instrumentation
) {
1823 case LTTNG_KERNEL_TRACEPOINT
:
1824 case LTTNG_KERNEL_UPROBE
:
1826 case LTTNG_KERNEL_KPROBE
:
1827 event_notifier_param
->event
.u
.kprobe
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
1829 case LTTNG_KERNEL_KRETPROBE
:
1830 /* Placing an event notifier on kretprobe is not supported. */
1831 case LTTNG_KERNEL_FUNCTION
:
1832 case LTTNG_KERNEL_NOOP
:
1833 case LTTNG_KERNEL_SYSCALL
:
1839 event_notifier_param
->event
.name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
1841 event_notifier_fd
= lttng_get_unused_fd();
1842 if (event_notifier_fd
< 0) {
1843 ret
= event_notifier_fd
;
1847 event_notifier_file
= anon_inode_getfile("[lttng_event_notifier]",
1848 <tng_event_notifier_fops
,
1850 if (IS_ERR(event_notifier_file
)) {
1851 ret
= PTR_ERR(event_notifier_file
);
1855 /* The event notifier holds a reference on the event notifier group. */
1856 if (!atomic_long_add_unless(&event_notifier_group_file
->f_count
, 1, LONG_MAX
)) {
1858 goto refcount_error
;
1861 if (event_notifier_param
->event
.instrumentation
== LTTNG_KERNEL_TRACEPOINT
1862 || event_notifier_param
->event
.instrumentation
== LTTNG_KERNEL_SYSCALL
) {
1863 struct lttng_event_notifier_enabler
*enabler
;
1865 if (strutils_is_star_glob_pattern(event_notifier_param
->event
.name
)) {
1867 * If the event name is a star globbing pattern,
1868 * we create the special star globbing enabler.
1870 enabler
= lttng_event_notifier_enabler_create(
1871 event_notifier_group
,
1872 LTTNG_ENABLER_FORMAT_STAR_GLOB
,
1873 event_notifier_param
);
1875 enabler
= lttng_event_notifier_enabler_create(
1876 event_notifier_group
,
1877 LTTNG_ENABLER_FORMAT_NAME
,
1878 event_notifier_param
);
1882 struct lttng_event_notifier
*event_notifier
;
1885 * We tolerate no failure path after event notifier creation.
1886 * It will stay invariant for the rest of the session.
1888 event_notifier
= lttng_event_notifier_create(NULL
,
1889 event_notifier_param
->event
.token
, event_notifier_group
,
1890 event_notifier_param
, NULL
,
1891 event_notifier_param
->event
.instrumentation
);
1892 WARN_ON_ONCE(!event_notifier
);
1893 if (IS_ERR(event_notifier
)) {
1894 ret
= PTR_ERR(event_notifier
);
1895 goto event_notifier_error
;
1897 priv
= event_notifier
;
1899 event_notifier_file
->private_data
= priv
;
1900 fd_install(event_notifier_fd
, event_notifier_file
);
1901 return event_notifier_fd
;
1903 event_notifier_error
:
1904 atomic_long_dec(&event_notifier_group_file
->f_count
);
1906 fput(event_notifier_file
);
1908 put_unused_fd(event_notifier_fd
);
1915 long lttng_event_notifier_group_ioctl(struct file
*file
, unsigned int cmd
,
1919 case LTTNG_KERNEL_EVENT_NOTIFIER_GROUP_NOTIFICATION_FD
:
1921 return lttng_abi_open_event_notifier_group_stream(file
);
1923 case LTTNG_KERNEL_EVENT_NOTIFIER_CREATE
:
1925 struct lttng_kernel_event_notifier uevent_notifier_param
;
1927 if (copy_from_user(&uevent_notifier_param
,
1928 (struct lttng_kernel_event_notifier __user
*) arg
,
1929 sizeof(uevent_notifier_param
)))
1931 return lttng_abi_create_event_notifier(file
, &uevent_notifier_param
);
1934 return -ENOIOCTLCMD
;
1940 int lttng_event_notifier_group_release(struct inode
*inode
, struct file
*file
)
1942 struct lttng_event_notifier_group
*event_notifier_group
=
1945 if (event_notifier_group
)
1946 lttng_event_notifier_group_destroy(event_notifier_group
);
1950 static const struct file_operations lttng_event_notifier_group_fops
= {
1951 .owner
= THIS_MODULE
,
1952 .release
= lttng_event_notifier_group_release
,
1953 .unlocked_ioctl
= lttng_event_notifier_group_ioctl
,
1954 #ifdef CONFIG_COMPAT
1955 .compat_ioctl
= lttng_event_notifier_group_ioctl
,
1960 * lttng_channel_ioctl - lttng syscall through ioctl
1966 * This ioctl implements lttng commands:
1967 * LTTNG_KERNEL_STREAM
1968 * Returns an event stream file descriptor or failure.
1969 * (typically, one event stream records events from one CPU)
1970 * LTTNG_KERNEL_EVENT
1971 * Returns an event file descriptor or failure.
1972 * LTTNG_KERNEL_CONTEXT
1973 * Prepend a context field to each event in the channel
1974 * LTTNG_KERNEL_ENABLE
1975 * Enable recording for events in this channel (weak enable)
1976 * LTTNG_KERNEL_DISABLE
1977 * Disable recording for events in this channel (strong disable)
1979 * Channel and event file descriptors also hold a reference on the session.
1982 long lttng_channel_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1984 struct lttng_channel
*channel
= file
->private_data
;
1987 case LTTNG_KERNEL_OLD_STREAM
:
1988 case LTTNG_KERNEL_STREAM
:
1989 return lttng_abi_open_stream(file
);
1990 case LTTNG_KERNEL_OLD_EVENT
:
1992 struct lttng_kernel_event
*uevent_param
;
1993 struct lttng_kernel_old_event
*old_uevent_param
;
1996 uevent_param
= kmalloc(sizeof(struct lttng_kernel_event
),
1998 if (!uevent_param
) {
2002 old_uevent_param
= kmalloc(
2003 sizeof(struct lttng_kernel_old_event
),
2005 if (!old_uevent_param
) {
2007 goto old_event_error_free_param
;
2009 if (copy_from_user(old_uevent_param
,
2010 (struct lttng_kernel_old_event __user
*) arg
,
2011 sizeof(struct lttng_kernel_old_event
))) {
2013 goto old_event_error_free_old_param
;
2016 memcpy(uevent_param
->name
, old_uevent_param
->name
,
2017 sizeof(uevent_param
->name
));
2018 uevent_param
->instrumentation
=
2019 old_uevent_param
->instrumentation
;
2021 switch (old_uevent_param
->instrumentation
) {
2022 case LTTNG_KERNEL_KPROBE
:
2023 uevent_param
->u
.kprobe
.addr
=
2024 old_uevent_param
->u
.kprobe
.addr
;
2025 uevent_param
->u
.kprobe
.offset
=
2026 old_uevent_param
->u
.kprobe
.offset
;
2027 memcpy(uevent_param
->u
.kprobe
.symbol_name
,
2028 old_uevent_param
->u
.kprobe
.symbol_name
,
2029 sizeof(uevent_param
->u
.kprobe
.symbol_name
));
2031 case LTTNG_KERNEL_KRETPROBE
:
2032 uevent_param
->u
.kretprobe
.addr
=
2033 old_uevent_param
->u
.kretprobe
.addr
;
2034 uevent_param
->u
.kretprobe
.offset
=
2035 old_uevent_param
->u
.kretprobe
.offset
;
2036 memcpy(uevent_param
->u
.kretprobe
.symbol_name
,
2037 old_uevent_param
->u
.kretprobe
.symbol_name
,
2038 sizeof(uevent_param
->u
.kretprobe
.symbol_name
));
2040 case LTTNG_KERNEL_FUNCTION
:
2042 /* Not implemented. */
2047 ret
= lttng_abi_create_event(file
, uevent_param
);
2049 old_event_error_free_old_param
:
2050 kfree(old_uevent_param
);
2051 old_event_error_free_param
:
2052 kfree(uevent_param
);
2056 case LTTNG_KERNEL_EVENT
:
2058 struct lttng_kernel_event uevent_param
;
2060 if (copy_from_user(&uevent_param
,
2061 (struct lttng_kernel_event __user
*) arg
,
2062 sizeof(uevent_param
)))
2064 return lttng_abi_create_event(file
, &uevent_param
);
2066 case LTTNG_KERNEL_OLD_CONTEXT
:
2068 struct lttng_kernel_context
*ucontext_param
;
2069 struct lttng_kernel_old_context
*old_ucontext_param
;
2072 ucontext_param
= kmalloc(sizeof(struct lttng_kernel_context
),
2074 if (!ucontext_param
) {
2078 old_ucontext_param
= kmalloc(sizeof(struct lttng_kernel_old_context
),
2080 if (!old_ucontext_param
) {
2082 goto old_ctx_error_free_param
;
2085 if (copy_from_user(old_ucontext_param
,
2086 (struct lttng_kernel_old_context __user
*) arg
,
2087 sizeof(struct lttng_kernel_old_context
))) {
2089 goto old_ctx_error_free_old_param
;
2091 ucontext_param
->ctx
= old_ucontext_param
->ctx
;
2092 memcpy(ucontext_param
->padding
, old_ucontext_param
->padding
,
2093 sizeof(ucontext_param
->padding
));
2094 /* only type that uses the union */
2095 if (old_ucontext_param
->ctx
== LTTNG_KERNEL_CONTEXT_PERF_COUNTER
) {
2096 ucontext_param
->u
.perf_counter
.type
=
2097 old_ucontext_param
->u
.perf_counter
.type
;
2098 ucontext_param
->u
.perf_counter
.config
=
2099 old_ucontext_param
->u
.perf_counter
.config
;
2100 memcpy(ucontext_param
->u
.perf_counter
.name
,
2101 old_ucontext_param
->u
.perf_counter
.name
,
2102 sizeof(ucontext_param
->u
.perf_counter
.name
));
2105 ret
= lttng_abi_add_context(file
,
2107 &channel
->ctx
, channel
->session
);
2109 old_ctx_error_free_old_param
:
2110 kfree(old_ucontext_param
);
2111 old_ctx_error_free_param
:
2112 kfree(ucontext_param
);
2116 case LTTNG_KERNEL_CONTEXT
:
2118 struct lttng_kernel_context ucontext_param
;
2120 if (copy_from_user(&ucontext_param
,
2121 (struct lttng_kernel_context __user
*) arg
,
2122 sizeof(ucontext_param
)))
2124 return lttng_abi_add_context(file
,
2126 &channel
->ctx
, channel
->session
);
2128 case LTTNG_KERNEL_OLD_ENABLE
:
2129 case LTTNG_KERNEL_ENABLE
:
2130 return lttng_channel_enable(channel
);
2131 case LTTNG_KERNEL_OLD_DISABLE
:
2132 case LTTNG_KERNEL_DISABLE
:
2133 return lttng_channel_disable(channel
);
2134 case LTTNG_KERNEL_SYSCALL_MASK
:
2135 return lttng_channel_syscall_mask(channel
,
2136 (struct lttng_kernel_syscall_mask __user
*) arg
);
2138 return -ENOIOCTLCMD
;
2143 * lttng_metadata_ioctl - lttng syscall through ioctl
2149 * This ioctl implements lttng commands:
2150 * LTTNG_KERNEL_STREAM
2151 * Returns an event stream file descriptor or failure.
2153 * Channel and event file descriptors also hold a reference on the session.
2156 long lttng_metadata_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
2159 case LTTNG_KERNEL_OLD_STREAM
:
2160 case LTTNG_KERNEL_STREAM
:
2161 return lttng_abi_open_metadata_stream(file
);
2163 return -ENOIOCTLCMD
;
2168 * lttng_channel_poll - lttng stream addition/removal monitoring
2173 unsigned int lttng_channel_poll(struct file
*file
, poll_table
*wait
)
2175 struct lttng_channel
*channel
= file
->private_data
;
2176 unsigned int mask
= 0;
2178 if (file
->f_mode
& FMODE_READ
) {
2179 poll_wait_set_exclusive(wait
);
2180 poll_wait(file
, channel
->ops
->get_hp_wait_queue(channel
->chan
),
2183 if (channel
->ops
->is_disabled(channel
->chan
))
2185 if (channel
->ops
->is_finalized(channel
->chan
))
2187 if (channel
->ops
->buffer_has_read_closed_stream(channel
->chan
))
2188 return POLLIN
| POLLRDNORM
;
2196 int lttng_channel_release(struct inode
*inode
, struct file
*file
)
2198 struct lttng_channel
*channel
= file
->private_data
;
2201 fput(channel
->session
->file
);
2206 int lttng_metadata_channel_release(struct inode
*inode
, struct file
*file
)
2208 struct lttng_channel
*channel
= file
->private_data
;
2211 fput(channel
->session
->file
);
2212 lttng_metadata_channel_destroy(channel
);
2218 static const struct file_operations lttng_channel_fops
= {
2219 .owner
= THIS_MODULE
,
2220 .release
= lttng_channel_release
,
2221 .poll
= lttng_channel_poll
,
2222 .unlocked_ioctl
= lttng_channel_ioctl
,
2223 #ifdef CONFIG_COMPAT
2224 .compat_ioctl
= lttng_channel_ioctl
,
2228 static const struct file_operations lttng_metadata_fops
= {
2229 .owner
= THIS_MODULE
,
2230 .release
= lttng_metadata_channel_release
,
2231 .unlocked_ioctl
= lttng_metadata_ioctl
,
2232 #ifdef CONFIG_COMPAT
2233 .compat_ioctl
= lttng_metadata_ioctl
,
2238 * lttng_event_ioctl - lttng syscall through ioctl
2244 * This ioctl implements lttng commands:
2245 * LTTNG_KERNEL_CONTEXT
2246 * Prepend a context field to each record of this event
2247 * LTTNG_KERNEL_ENABLE
2248 * Enable recording for this event (weak enable)
2249 * LTTNG_KERNEL_DISABLE
2250 * Disable recording for this event (strong disable)
2253 long lttng_event_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
2255 struct lttng_event
*event
;
2256 struct lttng_event_enabler
*event_enabler
;
2257 enum lttng_event_type
*evtype
= file
->private_data
;
2260 case LTTNG_KERNEL_OLD_CONTEXT
:
2262 /* Not implemented */
2265 case LTTNG_KERNEL_CONTEXT
:
2267 /* Not implemented */
2270 case LTTNG_KERNEL_OLD_ENABLE
:
2271 case LTTNG_KERNEL_ENABLE
:
2273 case LTTNG_TYPE_EVENT
:
2274 event
= file
->private_data
;
2275 return lttng_event_enable(event
);
2276 case LTTNG_TYPE_ENABLER
:
2277 event_enabler
= file
->private_data
;
2278 return lttng_event_enabler_enable(event_enabler
);
2283 case LTTNG_KERNEL_OLD_DISABLE
:
2284 case LTTNG_KERNEL_DISABLE
:
2286 case LTTNG_TYPE_EVENT
:
2287 event
= file
->private_data
;
2288 return lttng_event_disable(event
);
2289 case LTTNG_TYPE_ENABLER
:
2290 event_enabler
= file
->private_data
;
2291 return lttng_event_enabler_disable(event_enabler
);
2296 case LTTNG_KERNEL_FILTER
:
2298 case LTTNG_TYPE_EVENT
:
2300 case LTTNG_TYPE_ENABLER
:
2302 event_enabler
= file
->private_data
;
2303 return lttng_event_enabler_attach_bytecode(event_enabler
,
2304 (struct lttng_kernel_filter_bytecode __user
*) arg
);
2310 case LTTNG_KERNEL_ADD_CALLSITE
:
2312 case LTTNG_TYPE_EVENT
:
2313 event
= file
->private_data
;
2314 return lttng_event_add_callsite(event
,
2315 (struct lttng_kernel_event_callsite __user
*) arg
);
2316 case LTTNG_TYPE_ENABLER
:
2323 return -ENOIOCTLCMD
;
2328 int lttng_event_release(struct inode
*inode
, struct file
*file
)
2330 struct lttng_event
*event
;
2331 struct lttng_event_enabler
*event_enabler
;
2332 enum lttng_event_type
*evtype
= file
->private_data
;
2338 case LTTNG_TYPE_EVENT
:
2339 event
= file
->private_data
;
2341 fput(event
->chan
->file
);
2343 case LTTNG_TYPE_ENABLER
:
2344 event_enabler
= file
->private_data
;
2346 fput(event_enabler
->chan
->file
);
2356 /* TODO: filter control ioctl */
2357 static const struct file_operations lttng_event_fops
= {
2358 .owner
= THIS_MODULE
,
2359 .release
= lttng_event_release
,
2360 .unlocked_ioctl
= lttng_event_ioctl
,
2361 #ifdef CONFIG_COMPAT
2362 .compat_ioctl
= lttng_event_ioctl
,
2366 static int put_u64(uint64_t val
, unsigned long arg
)
2368 return put_user(val
, (uint64_t __user
*) arg
);
2371 static int put_u32(uint32_t val
, unsigned long arg
)
2373 return put_user(val
, (uint32_t __user
*) arg
);
2376 static long lttng_stream_ring_buffer_ioctl(struct file
*filp
,
2377 unsigned int cmd
, unsigned long arg
)
2379 struct lib_ring_buffer
*buf
= filp
->private_data
;
2380 struct channel
*chan
= buf
->backend
.chan
;
2381 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
2382 const struct lttng_channel_ops
*ops
= chan
->backend
.priv_ops
;
2385 if (atomic_read(&chan
->record_disabled
))
2389 case LTTNG_RING_BUFFER_GET_TIMESTAMP_BEGIN
:
2393 ret
= ops
->timestamp_begin(config
, buf
, &ts
);
2396 return put_u64(ts
, arg
);
2398 case LTTNG_RING_BUFFER_GET_TIMESTAMP_END
:
2402 ret
= ops
->timestamp_end(config
, buf
, &ts
);
2405 return put_u64(ts
, arg
);
2407 case LTTNG_RING_BUFFER_GET_EVENTS_DISCARDED
:
2411 ret
= ops
->events_discarded(config
, buf
, &ed
);
2414 return put_u64(ed
, arg
);
2416 case LTTNG_RING_BUFFER_GET_CONTENT_SIZE
:
2420 ret
= ops
->content_size(config
, buf
, &cs
);
2423 return put_u64(cs
, arg
);
2425 case LTTNG_RING_BUFFER_GET_PACKET_SIZE
:
2429 ret
= ops
->packet_size(config
, buf
, &ps
);
2432 return put_u64(ps
, arg
);
2434 case LTTNG_RING_BUFFER_GET_STREAM_ID
:
2438 ret
= ops
->stream_id(config
, buf
, &si
);
2441 return put_u64(si
, arg
);
2443 case LTTNG_RING_BUFFER_GET_CURRENT_TIMESTAMP
:
2447 ret
= ops
->current_timestamp(config
, buf
, &ts
);
2450 return put_u64(ts
, arg
);
2452 case LTTNG_RING_BUFFER_GET_SEQ_NUM
:
2456 ret
= ops
->sequence_number(config
, buf
, &seq
);
2459 return put_u64(seq
, arg
);
2461 case LTTNG_RING_BUFFER_INSTANCE_ID
:
2465 ret
= ops
->instance_id(config
, buf
, &id
);
2468 return put_u64(id
, arg
);
2471 return lib_ring_buffer_file_operations
.unlocked_ioctl(filp
,
2479 #ifdef CONFIG_COMPAT
2480 static long lttng_stream_ring_buffer_compat_ioctl(struct file
*filp
,
2481 unsigned int cmd
, unsigned long arg
)
2483 struct lib_ring_buffer
*buf
= filp
->private_data
;
2484 struct channel
*chan
= buf
->backend
.chan
;
2485 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
2486 const struct lttng_channel_ops
*ops
= chan
->backend
.priv_ops
;
2489 if (atomic_read(&chan
->record_disabled
))
2493 case LTTNG_RING_BUFFER_COMPAT_GET_TIMESTAMP_BEGIN
:
2497 ret
= ops
->timestamp_begin(config
, buf
, &ts
);
2500 return put_u64(ts
, arg
);
2502 case LTTNG_RING_BUFFER_COMPAT_GET_TIMESTAMP_END
:
2506 ret
= ops
->timestamp_end(config
, buf
, &ts
);
2509 return put_u64(ts
, arg
);
2511 case LTTNG_RING_BUFFER_COMPAT_GET_EVENTS_DISCARDED
:
2515 ret
= ops
->events_discarded(config
, buf
, &ed
);
2518 return put_u64(ed
, arg
);
2520 case LTTNG_RING_BUFFER_COMPAT_GET_CONTENT_SIZE
:
2524 ret
= ops
->content_size(config
, buf
, &cs
);
2527 return put_u64(cs
, arg
);
2529 case LTTNG_RING_BUFFER_COMPAT_GET_PACKET_SIZE
:
2533 ret
= ops
->packet_size(config
, buf
, &ps
);
2536 return put_u64(ps
, arg
);
2538 case LTTNG_RING_BUFFER_COMPAT_GET_STREAM_ID
:
2542 ret
= ops
->stream_id(config
, buf
, &si
);
2545 return put_u64(si
, arg
);
2547 case LTTNG_RING_BUFFER_GET_CURRENT_TIMESTAMP
:
2551 ret
= ops
->current_timestamp(config
, buf
, &ts
);
2554 return put_u64(ts
, arg
);
2556 case LTTNG_RING_BUFFER_COMPAT_GET_SEQ_NUM
:
2560 ret
= ops
->sequence_number(config
, buf
, &seq
);
2563 return put_u64(seq
, arg
);
2565 case LTTNG_RING_BUFFER_COMPAT_INSTANCE_ID
:
2569 ret
= ops
->instance_id(config
, buf
, &id
);
2572 return put_u64(id
, arg
);
2575 return lib_ring_buffer_file_operations
.compat_ioctl(filp
,
2582 #endif /* CONFIG_COMPAT */
2584 static void lttng_stream_override_ring_buffer_fops(void)
2586 lttng_stream_ring_buffer_file_operations
.owner
= THIS_MODULE
;
2587 lttng_stream_ring_buffer_file_operations
.open
=
2588 lib_ring_buffer_file_operations
.open
;
2589 lttng_stream_ring_buffer_file_operations
.release
=
2590 lib_ring_buffer_file_operations
.release
;
2591 lttng_stream_ring_buffer_file_operations
.poll
=
2592 lib_ring_buffer_file_operations
.poll
;
2593 lttng_stream_ring_buffer_file_operations
.splice_read
=
2594 lib_ring_buffer_file_operations
.splice_read
;
2595 lttng_stream_ring_buffer_file_operations
.mmap
=
2596 lib_ring_buffer_file_operations
.mmap
;
2597 lttng_stream_ring_buffer_file_operations
.unlocked_ioctl
=
2598 lttng_stream_ring_buffer_ioctl
;
2599 lttng_stream_ring_buffer_file_operations
.llseek
=
2600 lib_ring_buffer_file_operations
.llseek
;
2601 #ifdef CONFIG_COMPAT
2602 lttng_stream_ring_buffer_file_operations
.compat_ioctl
=
2603 lttng_stream_ring_buffer_compat_ioctl
;
2607 int __init
lttng_abi_init(void)
2611 wrapper_vmalloc_sync_mappings();
2614 ret
= lttng_tp_mempool_init();
2619 lttng_proc_dentry
= proc_create_data("lttng", S_IRUSR
| S_IWUSR
, NULL
,
2620 <tng_proc_ops
, NULL
);
2622 if (!lttng_proc_dentry
) {
2623 printk(KERN_ERR
"LTTng: Error creating control file\n");
2627 lttng_stream_override_ring_buffer_fops();
2631 lttng_tp_mempool_destroy();
2632 lttng_clock_unref();
2636 /* No __exit annotation because used by init error path too. */
2637 void lttng_abi_exit(void)
2639 lttng_tp_mempool_destroy();
2640 lttng_clock_unref();
2641 if (lttng_proc_dentry
)
2642 remove_proc_entry("lttng", NULL
);