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>
49 * This is LTTng's own personal way to create a system call as an external
50 * module. We use ioctl() on /proc/lttng.
53 static struct proc_dir_entry
*lttng_proc_dentry
;
55 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(5,6,0))
56 static const struct proc_ops lttng_proc_ops
;
58 static const struct file_operations lttng_proc_ops
;
61 static const struct file_operations lttng_session_fops
;
62 static const struct file_operations lttng_event_notifier_group_fops
;
63 static const struct file_operations lttng_channel_fops
;
64 static const struct file_operations lttng_metadata_fops
;
65 static const struct file_operations lttng_event_fops
;
66 static struct file_operations lttng_stream_ring_buffer_file_operations
;
68 static int put_u64(uint64_t val
, unsigned long arg
);
69 static int put_u32(uint32_t val
, unsigned long arg
);
72 * Teardown management: opened file descriptors keep a refcount on the module,
73 * so it can only exit when all file descriptors are closed.
77 int lttng_abi_create_session(void)
79 struct lttng_session
*session
;
80 struct file
*session_file
;
83 session
= lttng_session_create();
86 session_fd
= lttng_get_unused_fd();
91 session_file
= anon_inode_getfile("[lttng_session]",
94 if (IS_ERR(session_file
)) {
95 ret
= PTR_ERR(session_file
);
98 session
->file
= session_file
;
99 fd_install(session_fd
, session_file
);
103 put_unused_fd(session_fd
);
105 lttng_session_destroy(session
);
110 int lttng_abi_create_event_notifier_group(void)
112 struct lttng_event_notifier_group
*event_notifier_group
;
113 struct file
*event_notifier_group_file
;
114 int event_notifier_group_fd
, ret
;
116 event_notifier_group
= lttng_event_notifier_group_create();
117 if (!event_notifier_group
)
120 event_notifier_group_fd
= lttng_get_unused_fd();
121 if (event_notifier_group_fd
< 0) {
122 ret
= event_notifier_group_fd
;
125 event_notifier_group_file
= anon_inode_getfile("[lttng_event_notifier_group]",
126 <tng_event_notifier_group_fops
,
127 event_notifier_group
, O_RDWR
);
128 if (IS_ERR(event_notifier_group_file
)) {
129 ret
= PTR_ERR(event_notifier_group_file
);
133 event_notifier_group
->file
= event_notifier_group_file
;
134 fd_install(event_notifier_group_fd
, event_notifier_group_file
);
135 return event_notifier_group_fd
;
138 put_unused_fd(event_notifier_group_fd
);
140 lttng_event_notifier_group_destroy(event_notifier_group
);
145 int lttng_abi_tracepoint_list(void)
147 struct file
*tracepoint_list_file
;
150 file_fd
= lttng_get_unused_fd();
156 tracepoint_list_file
= anon_inode_getfile("[lttng_tracepoint_list]",
157 <tng_tracepoint_list_fops
,
159 if (IS_ERR(tracepoint_list_file
)) {
160 ret
= PTR_ERR(tracepoint_list_file
);
163 ret
= lttng_tracepoint_list_fops
.open(NULL
, tracepoint_list_file
);
166 fd_install(file_fd
, tracepoint_list_file
);
170 fput(tracepoint_list_file
);
172 put_unused_fd(file_fd
);
177 #ifndef CONFIG_HAVE_SYSCALL_TRACEPOINTS
179 int lttng_abi_syscall_list(void)
185 int lttng_abi_syscall_list(void)
187 struct file
*syscall_list_file
;
190 file_fd
= lttng_get_unused_fd();
196 syscall_list_file
= anon_inode_getfile("[lttng_syscall_list]",
197 <tng_syscall_list_fops
,
199 if (IS_ERR(syscall_list_file
)) {
200 ret
= PTR_ERR(syscall_list_file
);
203 ret
= lttng_syscall_list_fops
.open(NULL
, syscall_list_file
);
206 fd_install(file_fd
, syscall_list_file
);
210 fput(syscall_list_file
);
212 put_unused_fd(file_fd
);
219 void lttng_abi_tracer_version(struct lttng_kernel_tracer_version
*v
)
221 v
->major
= LTTNG_MODULES_MAJOR_VERSION
;
222 v
->minor
= LTTNG_MODULES_MINOR_VERSION
;
223 v
->patchlevel
= LTTNG_MODULES_PATCHLEVEL_VERSION
;
227 void lttng_abi_tracer_abi_version(struct lttng_kernel_tracer_abi_version
*v
)
229 v
->major
= LTTNG_MODULES_ABI_MAJOR_VERSION
;
230 v
->minor
= LTTNG_MODULES_ABI_MINOR_VERSION
;
234 long lttng_abi_add_context(struct file
*file
,
235 struct lttng_kernel_context
*context_param
,
236 struct lttng_ctx
**ctx
, struct lttng_session
*session
)
239 if (session
->been_active
)
242 switch (context_param
->ctx
) {
243 case LTTNG_KERNEL_CONTEXT_PID
:
244 return lttng_add_pid_to_ctx(ctx
);
245 case LTTNG_KERNEL_CONTEXT_PRIO
:
246 return lttng_add_prio_to_ctx(ctx
);
247 case LTTNG_KERNEL_CONTEXT_NICE
:
248 return lttng_add_nice_to_ctx(ctx
);
249 case LTTNG_KERNEL_CONTEXT_VPID
:
250 return lttng_add_vpid_to_ctx(ctx
);
251 case LTTNG_KERNEL_CONTEXT_TID
:
252 return lttng_add_tid_to_ctx(ctx
);
253 case LTTNG_KERNEL_CONTEXT_VTID
:
254 return lttng_add_vtid_to_ctx(ctx
);
255 case LTTNG_KERNEL_CONTEXT_PPID
:
256 return lttng_add_ppid_to_ctx(ctx
);
257 case LTTNG_KERNEL_CONTEXT_VPPID
:
258 return lttng_add_vppid_to_ctx(ctx
);
259 case LTTNG_KERNEL_CONTEXT_PERF_COUNTER
:
260 context_param
->u
.perf_counter
.name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
261 return lttng_add_perf_counter_to_ctx(context_param
->u
.perf_counter
.type
,
262 context_param
->u
.perf_counter
.config
,
263 context_param
->u
.perf_counter
.name
,
265 case LTTNG_KERNEL_CONTEXT_PROCNAME
:
266 return lttng_add_procname_to_ctx(ctx
);
267 case LTTNG_KERNEL_CONTEXT_HOSTNAME
:
268 return lttng_add_hostname_to_ctx(ctx
);
269 case LTTNG_KERNEL_CONTEXT_CPU_ID
:
270 return lttng_add_cpu_id_to_ctx(ctx
);
271 case LTTNG_KERNEL_CONTEXT_INTERRUPTIBLE
:
272 return lttng_add_interruptible_to_ctx(ctx
);
273 case LTTNG_KERNEL_CONTEXT_NEED_RESCHEDULE
:
274 return lttng_add_need_reschedule_to_ctx(ctx
);
275 case LTTNG_KERNEL_CONTEXT_PREEMPTIBLE
:
276 return lttng_add_preemptible_to_ctx(ctx
);
277 case LTTNG_KERNEL_CONTEXT_MIGRATABLE
:
278 return lttng_add_migratable_to_ctx(ctx
);
279 case LTTNG_KERNEL_CONTEXT_CALLSTACK_KERNEL
:
280 case LTTNG_KERNEL_CONTEXT_CALLSTACK_USER
:
281 return lttng_add_callstack_to_ctx(ctx
, context_param
->ctx
);
282 case LTTNG_KERNEL_CONTEXT_CGROUP_NS
:
283 return lttng_add_cgroup_ns_to_ctx(ctx
);
284 case LTTNG_KERNEL_CONTEXT_IPC_NS
:
285 return lttng_add_ipc_ns_to_ctx(ctx
);
286 case LTTNG_KERNEL_CONTEXT_MNT_NS
:
287 return lttng_add_mnt_ns_to_ctx(ctx
);
288 case LTTNG_KERNEL_CONTEXT_NET_NS
:
289 return lttng_add_net_ns_to_ctx(ctx
);
290 case LTTNG_KERNEL_CONTEXT_PID_NS
:
291 return lttng_add_pid_ns_to_ctx(ctx
);
292 case LTTNG_KERNEL_CONTEXT_USER_NS
:
293 return lttng_add_user_ns_to_ctx(ctx
);
294 case LTTNG_KERNEL_CONTEXT_UTS_NS
:
295 return lttng_add_uts_ns_to_ctx(ctx
);
296 case LTTNG_KERNEL_CONTEXT_UID
:
297 return lttng_add_uid_to_ctx(ctx
);
298 case LTTNG_KERNEL_CONTEXT_EUID
:
299 return lttng_add_euid_to_ctx(ctx
);
300 case LTTNG_KERNEL_CONTEXT_SUID
:
301 return lttng_add_suid_to_ctx(ctx
);
302 case LTTNG_KERNEL_CONTEXT_GID
:
303 return lttng_add_gid_to_ctx(ctx
);
304 case LTTNG_KERNEL_CONTEXT_EGID
:
305 return lttng_add_egid_to_ctx(ctx
);
306 case LTTNG_KERNEL_CONTEXT_SGID
:
307 return lttng_add_sgid_to_ctx(ctx
);
308 case LTTNG_KERNEL_CONTEXT_VUID
:
309 return lttng_add_vuid_to_ctx(ctx
);
310 case LTTNG_KERNEL_CONTEXT_VEUID
:
311 return lttng_add_veuid_to_ctx(ctx
);
312 case LTTNG_KERNEL_CONTEXT_VSUID
:
313 return lttng_add_vsuid_to_ctx(ctx
);
314 case LTTNG_KERNEL_CONTEXT_VGID
:
315 return lttng_add_vgid_to_ctx(ctx
);
316 case LTTNG_KERNEL_CONTEXT_VEGID
:
317 return lttng_add_vegid_to_ctx(ctx
);
318 case LTTNG_KERNEL_CONTEXT_VSGID
:
319 return lttng_add_vsgid_to_ctx(ctx
);
320 case LTTNG_KERNEL_CONTEXT_TIME_NS
:
321 return lttng_add_time_ns_to_ctx(ctx
);
328 * lttng_ioctl - lttng syscall through ioctl
334 * This ioctl implements lttng commands:
335 * LTTNG_KERNEL_SESSION
336 * Returns a LTTng trace session file descriptor
337 * LTTNG_KERNEL_TRACER_VERSION
338 * Returns the LTTng kernel tracer version
339 * LTTNG_KERNEL_TRACEPOINT_LIST
340 * Returns a file descriptor listing available tracepoints
341 * LTTNG_KERNEL_WAIT_QUIESCENT
342 * Returns after all previously running probes have completed
343 * LTTNG_KERNEL_TRACER_ABI_VERSION
344 * Returns the LTTng kernel tracer ABI version
345 * LTTNG_KERNEL_EVENT_NOTIFIER_GROUP_CREATE
346 * Returns a LTTng event notifier group file descriptor
348 * The returned session will be deleted when its file descriptor is closed.
351 long lttng_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
354 case LTTNG_KERNEL_OLD_SESSION
:
355 case LTTNG_KERNEL_SESSION
:
356 return lttng_abi_create_session();
357 case LTTNG_KERNEL_EVENT_NOTIFIER_GROUP_CREATE
:
358 return lttng_abi_create_event_notifier_group();
359 case LTTNG_KERNEL_OLD_TRACER_VERSION
:
361 struct lttng_kernel_tracer_version v
;
362 struct lttng_kernel_old_tracer_version oldv
;
363 struct lttng_kernel_old_tracer_version
*uversion
=
364 (struct lttng_kernel_old_tracer_version __user
*) arg
;
366 lttng_abi_tracer_version(&v
);
367 oldv
.major
= v
.major
;
368 oldv
.minor
= v
.minor
;
369 oldv
.patchlevel
= v
.patchlevel
;
371 if (copy_to_user(uversion
, &oldv
, sizeof(oldv
)))
375 case LTTNG_KERNEL_TRACER_VERSION
:
377 struct lttng_kernel_tracer_version version
;
378 struct lttng_kernel_tracer_version
*uversion
=
379 (struct lttng_kernel_tracer_version __user
*) arg
;
381 lttng_abi_tracer_version(&version
);
383 if (copy_to_user(uversion
, &version
, sizeof(version
)))
387 case LTTNG_KERNEL_TRACER_ABI_VERSION
:
389 struct lttng_kernel_tracer_abi_version version
;
390 struct lttng_kernel_tracer_abi_version
*uversion
=
391 (struct lttng_kernel_tracer_abi_version __user
*) arg
;
393 lttng_abi_tracer_abi_version(&version
);
395 if (copy_to_user(uversion
, &version
, sizeof(version
)))
399 case LTTNG_KERNEL_OLD_TRACEPOINT_LIST
:
400 case LTTNG_KERNEL_TRACEPOINT_LIST
:
401 return lttng_abi_tracepoint_list();
402 case LTTNG_KERNEL_SYSCALL_LIST
:
403 return lttng_abi_syscall_list();
404 case LTTNG_KERNEL_OLD_WAIT_QUIESCENT
:
405 case LTTNG_KERNEL_WAIT_QUIESCENT
:
408 case LTTNG_KERNEL_OLD_CALIBRATE
:
410 struct lttng_kernel_old_calibrate __user
*ucalibrate
=
411 (struct lttng_kernel_old_calibrate __user
*) arg
;
412 struct lttng_kernel_old_calibrate old_calibrate
;
413 struct lttng_kernel_calibrate calibrate
;
416 if (copy_from_user(&old_calibrate
, ucalibrate
, sizeof(old_calibrate
)))
418 calibrate
.type
= old_calibrate
.type
;
419 ret
= lttng_calibrate(&calibrate
);
420 if (copy_to_user(ucalibrate
, &old_calibrate
, sizeof(old_calibrate
)))
424 case LTTNG_KERNEL_CALIBRATE
:
426 struct lttng_kernel_calibrate __user
*ucalibrate
=
427 (struct lttng_kernel_calibrate __user
*) arg
;
428 struct lttng_kernel_calibrate calibrate
;
431 if (copy_from_user(&calibrate
, ucalibrate
, sizeof(calibrate
)))
433 ret
= lttng_calibrate(&calibrate
);
434 if (copy_to_user(ucalibrate
, &calibrate
, sizeof(calibrate
)))
443 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(5,6,0))
444 static const struct proc_ops lttng_proc_ops
= {
445 .proc_ioctl
= lttng_ioctl
,
447 .proc_compat_ioctl
= lttng_ioctl
,
448 #endif /* CONFIG_COMPAT */
451 static const struct file_operations lttng_proc_ops
= {
452 .owner
= THIS_MODULE
,
453 .unlocked_ioctl
= lttng_ioctl
,
455 .compat_ioctl
= lttng_ioctl
,
456 #endif /* CONFIG_COMPAT */
461 int lttng_abi_create_channel(struct file
*session_file
,
462 struct lttng_kernel_channel
*chan_param
,
463 enum channel_type channel_type
)
465 struct lttng_session
*session
= session_file
->private_data
;
466 const struct file_operations
*fops
= NULL
;
467 const char *transport_name
;
468 struct lttng_channel
*chan
;
469 struct file
*chan_file
;
473 chan_fd
= lttng_get_unused_fd();
478 switch (channel_type
) {
479 case PER_CPU_CHANNEL
:
480 fops
= <tng_channel_fops
;
482 case METADATA_CHANNEL
:
483 fops
= <tng_metadata_fops
;
487 chan_file
= anon_inode_getfile("[lttng_channel]",
490 if (IS_ERR(chan_file
)) {
491 ret
= PTR_ERR(chan_file
);
494 switch (channel_type
) {
495 case PER_CPU_CHANNEL
:
496 if (chan_param
->output
== LTTNG_KERNEL_SPLICE
) {
497 transport_name
= chan_param
->overwrite
?
498 "relay-overwrite" : "relay-discard";
499 } else if (chan_param
->output
== LTTNG_KERNEL_MMAP
) {
500 transport_name
= chan_param
->overwrite
?
501 "relay-overwrite-mmap" : "relay-discard-mmap";
506 case METADATA_CHANNEL
:
507 if (chan_param
->output
== LTTNG_KERNEL_SPLICE
)
508 transport_name
= "relay-metadata";
509 else if (chan_param
->output
== LTTNG_KERNEL_MMAP
)
510 transport_name
= "relay-metadata-mmap";
515 transport_name
= "<unknown>";
518 if (!atomic_long_add_unless(&session_file
->f_count
, 1, LONG_MAX
)) {
523 * We tolerate no failure path after channel creation. It will stay
524 * invariant for the rest of the session.
526 chan
= lttng_channel_create(session
, transport_name
, NULL
,
527 chan_param
->subbuf_size
,
528 chan_param
->num_subbuf
,
529 chan_param
->switch_timer_interval
,
530 chan_param
->read_timer_interval
,
536 chan
->file
= chan_file
;
537 chan_file
->private_data
= chan
;
538 fd_install(chan_fd
, chan_file
);
543 atomic_long_dec(&session_file
->f_count
);
547 put_unused_fd(chan_fd
);
553 int lttng_abi_session_set_name(struct lttng_session
*session
,
554 struct lttng_kernel_session_name
*name
)
558 len
= strnlen(name
->name
, LTTNG_KERNEL_SESSION_NAME_LEN
);
560 if (len
== LTTNG_KERNEL_SESSION_NAME_LEN
) {
561 /* Name is too long/malformed */
565 strcpy(session
->name
, name
->name
);
570 int lttng_abi_session_set_creation_time(struct lttng_session
*session
,
571 struct lttng_kernel_session_creation_time
*time
)
575 len
= strnlen(time
->iso8601
, LTTNG_KERNEL_SESSION_CREATION_TIME_ISO8601_LEN
);
577 if (len
== LTTNG_KERNEL_SESSION_CREATION_TIME_ISO8601_LEN
) {
578 /* Time is too long/malformed */
582 strcpy(session
->creation_time
, time
->iso8601
);
587 enum tracker_type
get_tracker_type(struct lttng_kernel_tracker_args
*tracker
)
589 switch (tracker
->type
) {
590 case LTTNG_KERNEL_TRACKER_PID
:
592 case LTTNG_KERNEL_TRACKER_VPID
:
594 case LTTNG_KERNEL_TRACKER_UID
:
596 case LTTNG_KERNEL_TRACKER_VUID
:
598 case LTTNG_KERNEL_TRACKER_GID
:
600 case LTTNG_KERNEL_TRACKER_VGID
:
603 return TRACKER_UNKNOWN
;
608 * lttng_session_ioctl - lttng session fd ioctl
614 * This ioctl implements lttng commands:
615 * LTTNG_KERNEL_CHANNEL
616 * Returns a LTTng channel file descriptor
617 * LTTNG_KERNEL_ENABLE
618 * Enables tracing for a session (weak enable)
619 * LTTNG_KERNEL_DISABLE
620 * Disables tracing for a session (strong disable)
621 * LTTNG_KERNEL_METADATA
622 * Returns a LTTng metadata file descriptor
623 * LTTNG_KERNEL_SESSION_TRACK_PID
624 * Add PID to session PID tracker
625 * LTTNG_KERNEL_SESSION_UNTRACK_PID
626 * Remove PID from session PID tracker
627 * LTTNG_KERNEL_SESSION_TRACK_ID
629 * LTTNG_KERNEL_SESSION_UNTRACK_ID
630 * Remove ID from tracker
632 * The returned channel will be deleted when its file descriptor is closed.
635 long lttng_session_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
637 struct lttng_session
*session
= file
->private_data
;
638 struct lttng_kernel_channel chan_param
;
639 struct lttng_kernel_old_channel old_chan_param
;
642 case LTTNG_KERNEL_OLD_CHANNEL
:
644 if (copy_from_user(&old_chan_param
,
645 (struct lttng_kernel_old_channel __user
*) arg
,
646 sizeof(struct lttng_kernel_old_channel
)))
648 chan_param
.overwrite
= old_chan_param
.overwrite
;
649 chan_param
.subbuf_size
= old_chan_param
.subbuf_size
;
650 chan_param
.num_subbuf
= old_chan_param
.num_subbuf
;
651 chan_param
.switch_timer_interval
= old_chan_param
.switch_timer_interval
;
652 chan_param
.read_timer_interval
= old_chan_param
.read_timer_interval
;
653 chan_param
.output
= old_chan_param
.output
;
655 return lttng_abi_create_channel(file
, &chan_param
,
658 case LTTNG_KERNEL_CHANNEL
:
660 if (copy_from_user(&chan_param
,
661 (struct lttng_kernel_channel __user
*) arg
,
662 sizeof(struct lttng_kernel_channel
)))
664 return lttng_abi_create_channel(file
, &chan_param
,
667 case LTTNG_KERNEL_OLD_SESSION_START
:
668 case LTTNG_KERNEL_OLD_ENABLE
:
669 case LTTNG_KERNEL_SESSION_START
:
670 case LTTNG_KERNEL_ENABLE
:
671 return lttng_session_enable(session
);
672 case LTTNG_KERNEL_OLD_SESSION_STOP
:
673 case LTTNG_KERNEL_OLD_DISABLE
:
674 case LTTNG_KERNEL_SESSION_STOP
:
675 case LTTNG_KERNEL_DISABLE
:
676 return lttng_session_disable(session
);
677 case LTTNG_KERNEL_OLD_METADATA
:
679 if (copy_from_user(&old_chan_param
,
680 (struct lttng_kernel_old_channel __user
*) arg
,
681 sizeof(struct lttng_kernel_old_channel
)))
683 chan_param
.overwrite
= old_chan_param
.overwrite
;
684 chan_param
.subbuf_size
= old_chan_param
.subbuf_size
;
685 chan_param
.num_subbuf
= old_chan_param
.num_subbuf
;
686 chan_param
.switch_timer_interval
= old_chan_param
.switch_timer_interval
;
687 chan_param
.read_timer_interval
= old_chan_param
.read_timer_interval
;
688 chan_param
.output
= old_chan_param
.output
;
690 return lttng_abi_create_channel(file
, &chan_param
,
693 case LTTNG_KERNEL_METADATA
:
695 if (copy_from_user(&chan_param
,
696 (struct lttng_kernel_channel __user
*) arg
,
697 sizeof(struct lttng_kernel_channel
)))
699 return lttng_abi_create_channel(file
, &chan_param
,
702 case LTTNG_KERNEL_SESSION_TRACK_PID
:
703 return lttng_session_track_id(session
, TRACKER_PID
, (int) arg
);
704 case LTTNG_KERNEL_SESSION_UNTRACK_PID
:
705 return lttng_session_untrack_id(session
, TRACKER_PID
, (int) arg
);
706 case LTTNG_KERNEL_SESSION_TRACK_ID
:
708 struct lttng_kernel_tracker_args tracker
;
709 enum tracker_type tracker_type
;
711 if (copy_from_user(&tracker
,
712 (struct lttng_kernel_tracker_args __user
*) arg
,
713 sizeof(struct lttng_kernel_tracker_args
)))
715 tracker_type
= get_tracker_type(&tracker
);
716 if (tracker_type
== TRACKER_UNKNOWN
)
718 return lttng_session_track_id(session
, tracker_type
, tracker
.id
);
720 case LTTNG_KERNEL_SESSION_UNTRACK_ID
:
722 struct lttng_kernel_tracker_args tracker
;
723 enum tracker_type tracker_type
;
725 if (copy_from_user(&tracker
,
726 (struct lttng_kernel_tracker_args __user
*) arg
,
727 sizeof(struct lttng_kernel_tracker_args
)))
729 tracker_type
= get_tracker_type(&tracker
);
730 if (tracker_type
== TRACKER_UNKNOWN
)
732 return lttng_session_untrack_id(session
, tracker_type
,
735 case LTTNG_KERNEL_SESSION_LIST_TRACKER_PIDS
:
736 return lttng_session_list_tracker_ids(session
, TRACKER_PID
);
737 case LTTNG_KERNEL_SESSION_LIST_TRACKER_IDS
:
739 struct lttng_kernel_tracker_args tracker
;
740 enum tracker_type tracker_type
;
742 if (copy_from_user(&tracker
,
743 (struct lttng_kernel_tracker_args __user
*) arg
,
744 sizeof(struct lttng_kernel_tracker_args
)))
746 tracker_type
= get_tracker_type(&tracker
);
747 if (tracker_type
== TRACKER_UNKNOWN
)
749 return lttng_session_list_tracker_ids(session
, tracker_type
);
751 case LTTNG_KERNEL_SESSION_METADATA_REGEN
:
752 return lttng_session_metadata_regenerate(session
);
753 case LTTNG_KERNEL_SESSION_STATEDUMP
:
754 return lttng_session_statedump(session
);
755 case LTTNG_KERNEL_SESSION_SET_NAME
:
757 struct lttng_kernel_session_name name
;
759 if (copy_from_user(&name
,
760 (struct lttng_kernel_session_name __user
*) arg
,
761 sizeof(struct lttng_kernel_session_name
)))
763 return lttng_abi_session_set_name(session
, &name
);
765 case LTTNG_KERNEL_SESSION_SET_CREATION_TIME
:
767 struct lttng_kernel_session_creation_time time
;
769 if (copy_from_user(&time
,
770 (struct lttng_kernel_session_creation_time __user
*) arg
,
771 sizeof(struct lttng_kernel_session_creation_time
)))
773 return lttng_abi_session_set_creation_time(session
, &time
);
781 * Called when the last file reference is dropped.
783 * Big fat note: channels and events are invariant for the whole session after
784 * their creation. So this session destruction also destroys all channel and
785 * event structures specific to this session (they are not destroyed when their
786 * individual file is released).
789 int lttng_session_release(struct inode
*inode
, struct file
*file
)
791 struct lttng_session
*session
= file
->private_data
;
794 lttng_session_destroy(session
);
798 static const struct file_operations lttng_session_fops
= {
799 .owner
= THIS_MODULE
,
800 .release
= lttng_session_release
,
801 .unlocked_ioctl
= lttng_session_ioctl
,
803 .compat_ioctl
= lttng_session_ioctl
,
808 * lttng_metadata_ring_buffer_poll - LTTng ring buffer poll file operation
812 * Handles the poll operations for the metadata channels.
815 unsigned int lttng_metadata_ring_buffer_poll(struct file
*filp
,
818 struct lttng_metadata_stream
*stream
= filp
->private_data
;
819 struct lib_ring_buffer
*buf
= stream
->priv
;
821 unsigned int mask
= 0;
823 if (filp
->f_mode
& FMODE_READ
) {
824 poll_wait_set_exclusive(wait
);
825 poll_wait(filp
, &stream
->read_wait
, wait
);
827 finalized
= stream
->finalized
;
830 * lib_ring_buffer_is_finalized() contains a smp_rmb()
831 * ordering finalized load before offsets loads.
833 WARN_ON(atomic_long_read(&buf
->active_readers
) != 1);
838 mutex_lock(&stream
->metadata_cache
->lock
);
839 if (stream
->metadata_cache
->metadata_written
>
840 stream
->metadata_out
)
842 mutex_unlock(&stream
->metadata_cache
->lock
);
849 void lttng_metadata_ring_buffer_ioctl_put_next_subbuf(struct file
*filp
,
850 unsigned int cmd
, unsigned long arg
)
852 struct lttng_metadata_stream
*stream
= filp
->private_data
;
854 stream
->metadata_out
= stream
->metadata_in
;
858 * Reset the counter of how much metadata has been consumed to 0. That way,
859 * the consumer receives the content of the metadata cache unchanged. This is
860 * different from the metadata_regenerate where the offset from epoch is
861 * resampled, here we want the exact same content as the last time the metadata
862 * was generated. This command is only possible if all the metadata written
863 * in the cache has been output to the metadata stream to avoid corrupting the
866 * Return 0 on success, a negative value on error.
869 int lttng_metadata_cache_dump(struct lttng_metadata_stream
*stream
)
872 struct lttng_metadata_cache
*cache
= stream
->metadata_cache
;
874 mutex_lock(&cache
->lock
);
875 if (stream
->metadata_out
!= cache
->metadata_written
) {
879 stream
->metadata_out
= 0;
880 stream
->metadata_in
= 0;
881 wake_up_interruptible(&stream
->read_wait
);
885 mutex_unlock(&cache
->lock
);
890 long lttng_metadata_ring_buffer_ioctl(struct file
*filp
,
891 unsigned int cmd
, unsigned long arg
)
894 struct lttng_metadata_stream
*stream
= filp
->private_data
;
895 struct lib_ring_buffer
*buf
= stream
->priv
;
899 if (cmd
== RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK
)
900 rb_cmd
= RING_BUFFER_GET_NEXT_SUBBUF
;
905 case RING_BUFFER_GET_NEXT_SUBBUF
:
907 struct lttng_metadata_stream
*stream
= filp
->private_data
;
908 struct lib_ring_buffer
*buf
= stream
->priv
;
909 struct channel
*chan
= buf
->backend
.chan
;
911 ret
= lttng_metadata_output_channel(stream
, chan
, NULL
);
913 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
919 case RING_BUFFER_GET_SUBBUF
:
922 * Random access is not allowed for metadata channel.
926 case RING_BUFFER_FLUSH_EMPTY
: /* Fall-through. */
927 case RING_BUFFER_FLUSH
:
929 struct lttng_metadata_stream
*stream
= filp
->private_data
;
930 struct lib_ring_buffer
*buf
= stream
->priv
;
931 struct channel
*chan
= buf
->backend
.chan
;
934 * Before doing the actual ring buffer flush, write up to one
935 * packet of metadata in the ring buffer.
937 ret
= lttng_metadata_output_channel(stream
, chan
, NULL
);
942 case RING_BUFFER_GET_METADATA_VERSION
:
944 struct lttng_metadata_stream
*stream
= filp
->private_data
;
946 return put_u64(stream
->version
, arg
);
948 case RING_BUFFER_METADATA_CACHE_DUMP
:
950 struct lttng_metadata_stream
*stream
= filp
->private_data
;
952 return lttng_metadata_cache_dump(stream
);
954 case RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK
:
956 struct lttng_metadata_stream
*stream
= filp
->private_data
;
957 struct lib_ring_buffer
*buf
= stream
->priv
;
958 struct channel
*chan
= buf
->backend
.chan
;
960 ret
= lttng_metadata_output_channel(stream
, chan
, &coherent
);
962 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
964 } else if (ret
< 0) {
972 /* PUT_SUBBUF is the one from lib ring buffer, unmodified. */
974 /* Performing lib ring buffer ioctl after our own. */
975 ret
= lib_ring_buffer_ioctl(filp
, rb_cmd
, arg
, buf
);
980 case RING_BUFFER_PUT_NEXT_SUBBUF
:
982 lttng_metadata_ring_buffer_ioctl_put_next_subbuf(filp
,
986 case RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK
:
988 return put_u32(coherent
, arg
);
999 long lttng_metadata_ring_buffer_compat_ioctl(struct file
*filp
,
1000 unsigned int cmd
, unsigned long arg
)
1003 struct lttng_metadata_stream
*stream
= filp
->private_data
;
1004 struct lib_ring_buffer
*buf
= stream
->priv
;
1005 unsigned int rb_cmd
;
1008 if (cmd
== RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK
)
1009 rb_cmd
= RING_BUFFER_GET_NEXT_SUBBUF
;
1014 case RING_BUFFER_GET_NEXT_SUBBUF
:
1016 struct lttng_metadata_stream
*stream
= filp
->private_data
;
1017 struct lib_ring_buffer
*buf
= stream
->priv
;
1018 struct channel
*chan
= buf
->backend
.chan
;
1020 ret
= lttng_metadata_output_channel(stream
, chan
, NULL
);
1022 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
1028 case RING_BUFFER_GET_SUBBUF
:
1031 * Random access is not allowed for metadata channel.
1035 case RING_BUFFER_FLUSH_EMPTY
: /* Fall-through. */
1036 case RING_BUFFER_FLUSH
:
1038 struct lttng_metadata_stream
*stream
= filp
->private_data
;
1039 struct lib_ring_buffer
*buf
= stream
->priv
;
1040 struct channel
*chan
= buf
->backend
.chan
;
1043 * Before doing the actual ring buffer flush, write up to one
1044 * packet of metadata in the ring buffer.
1046 ret
= lttng_metadata_output_channel(stream
, chan
, NULL
);
1051 case RING_BUFFER_GET_METADATA_VERSION
:
1053 struct lttng_metadata_stream
*stream
= filp
->private_data
;
1055 return put_u64(stream
->version
, arg
);
1057 case RING_BUFFER_METADATA_CACHE_DUMP
:
1059 struct lttng_metadata_stream
*stream
= filp
->private_data
;
1061 return lttng_metadata_cache_dump(stream
);
1063 case RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK
:
1065 struct lttng_metadata_stream
*stream
= filp
->private_data
;
1066 struct lib_ring_buffer
*buf
= stream
->priv
;
1067 struct channel
*chan
= buf
->backend
.chan
;
1069 ret
= lttng_metadata_output_channel(stream
, chan
, &coherent
);
1071 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
1073 } else if (ret
< 0) {
1081 /* PUT_SUBBUF is the one from lib ring buffer, unmodified. */
1083 /* Performing lib ring buffer ioctl after our own. */
1084 ret
= lib_ring_buffer_compat_ioctl(filp
, rb_cmd
, arg
, buf
);
1089 case RING_BUFFER_PUT_NEXT_SUBBUF
:
1091 lttng_metadata_ring_buffer_ioctl_put_next_subbuf(filp
,
1095 case RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK
:
1097 return put_u32(coherent
, arg
);
1108 * This is not used by anonymous file descriptors. This code is left
1109 * there if we ever want to implement an inode with open() operation.
1112 int lttng_metadata_ring_buffer_open(struct inode
*inode
, struct file
*file
)
1114 struct lttng_metadata_stream
*stream
= inode
->i_private
;
1115 struct lib_ring_buffer
*buf
= stream
->priv
;
1117 file
->private_data
= buf
;
1119 * Since life-time of metadata cache differs from that of
1120 * session, we need to keep our own reference on the transport.
1122 if (!try_module_get(stream
->transport
->owner
)) {
1123 printk(KERN_WARNING
"LTTng: Can't lock transport module.\n");
1126 return lib_ring_buffer_open(inode
, file
, buf
);
1130 int lttng_metadata_ring_buffer_release(struct inode
*inode
, struct file
*file
)
1132 struct lttng_metadata_stream
*stream
= file
->private_data
;
1133 struct lib_ring_buffer
*buf
= stream
->priv
;
1135 mutex_lock(&stream
->metadata_cache
->lock
);
1136 list_del(&stream
->list
);
1137 mutex_unlock(&stream
->metadata_cache
->lock
);
1138 kref_put(&stream
->metadata_cache
->refcount
, metadata_cache_destroy
);
1139 module_put(stream
->transport
->owner
);
1141 return lib_ring_buffer_release(inode
, file
, buf
);
1145 ssize_t
lttng_metadata_ring_buffer_splice_read(struct file
*in
, loff_t
*ppos
,
1146 struct pipe_inode_info
*pipe
, size_t len
,
1149 struct lttng_metadata_stream
*stream
= in
->private_data
;
1150 struct lib_ring_buffer
*buf
= stream
->priv
;
1152 return lib_ring_buffer_splice_read(in
, ppos
, pipe
, len
,
1157 int lttng_metadata_ring_buffer_mmap(struct file
*filp
,
1158 struct vm_area_struct
*vma
)
1160 struct lttng_metadata_stream
*stream
= filp
->private_data
;
1161 struct lib_ring_buffer
*buf
= stream
->priv
;
1163 return lib_ring_buffer_mmap(filp
, vma
, buf
);
1167 const struct file_operations lttng_metadata_ring_buffer_file_operations
= {
1168 .owner
= THIS_MODULE
,
1169 .open
= lttng_metadata_ring_buffer_open
,
1170 .release
= lttng_metadata_ring_buffer_release
,
1171 .poll
= lttng_metadata_ring_buffer_poll
,
1172 .splice_read
= lttng_metadata_ring_buffer_splice_read
,
1173 .mmap
= lttng_metadata_ring_buffer_mmap
,
1174 .unlocked_ioctl
= lttng_metadata_ring_buffer_ioctl
,
1175 .llseek
= vfs_lib_ring_buffer_no_llseek
,
1176 #ifdef CONFIG_COMPAT
1177 .compat_ioctl
= lttng_metadata_ring_buffer_compat_ioctl
,
1182 int lttng_abi_create_stream_fd(struct file
*channel_file
, void *stream_priv
,
1183 const struct file_operations
*fops
, const char *name
)
1186 struct file
*stream_file
;
1188 stream_fd
= lttng_get_unused_fd();
1189 if (stream_fd
< 0) {
1193 stream_file
= anon_inode_getfile(name
, fops
, stream_priv
, O_RDWR
);
1194 if (IS_ERR(stream_file
)) {
1195 ret
= PTR_ERR(stream_file
);
1199 * OPEN_FMODE, called within anon_inode_getfile/alloc_file, don't honor
1200 * FMODE_LSEEK, FMODE_PREAD nor FMODE_PWRITE. We need to read from this
1201 * file descriptor, so we set FMODE_PREAD here.
1203 stream_file
->f_mode
|= FMODE_PREAD
;
1204 fd_install(stream_fd
, stream_file
);
1206 * The stream holds a reference to the channel within the generic ring
1207 * buffer library, so no need to hold a refcount on the channel and
1208 * session files here.
1213 put_unused_fd(stream_fd
);
1219 int lttng_abi_open_stream(struct file
*channel_file
)
1221 struct lttng_channel
*channel
= channel_file
->private_data
;
1222 struct lib_ring_buffer
*buf
;
1226 buf
= channel
->ops
->buffer_read_open(channel
->chan
);
1231 ret
= lttng_abi_create_stream_fd(channel_file
, stream_priv
,
1232 <tng_stream_ring_buffer_file_operations
,
1240 channel
->ops
->buffer_read_close(buf
);
1245 int lttng_abi_open_metadata_stream(struct file
*channel_file
)
1247 struct lttng_channel
*channel
= channel_file
->private_data
;
1248 struct lttng_session
*session
= channel
->session
;
1249 struct lib_ring_buffer
*buf
;
1251 struct lttng_metadata_stream
*metadata_stream
;
1254 buf
= channel
->ops
->buffer_read_open(channel
->chan
);
1258 metadata_stream
= kzalloc(sizeof(struct lttng_metadata_stream
),
1260 if (!metadata_stream
) {
1264 metadata_stream
->metadata_cache
= session
->metadata_cache
;
1265 init_waitqueue_head(&metadata_stream
->read_wait
);
1266 metadata_stream
->priv
= buf
;
1267 stream_priv
= metadata_stream
;
1268 metadata_stream
->transport
= channel
->transport
;
1269 /* Initial state is an empty metadata, considered as incoherent. */
1270 metadata_stream
->coherent
= false;
1273 * Since life-time of metadata cache differs from that of
1274 * session, we need to keep our own reference on the transport.
1276 if (!try_module_get(metadata_stream
->transport
->owner
)) {
1277 printk(KERN_WARNING
"LTTng: Can't lock transport module.\n");
1282 if (!lttng_kref_get(&session
->metadata_cache
->refcount
)) {
1287 ret
= lttng_abi_create_stream_fd(channel_file
, stream_priv
,
1288 <tng_metadata_ring_buffer_file_operations
,
1289 "[lttng_metadata_stream]");
1293 mutex_lock(&session
->metadata_cache
->lock
);
1294 list_add(&metadata_stream
->list
,
1295 &session
->metadata_cache
->metadata_stream
);
1296 mutex_unlock(&session
->metadata_cache
->lock
);
1300 kref_put(&session
->metadata_cache
->refcount
, metadata_cache_destroy
);
1302 module_put(metadata_stream
->transport
->owner
);
1304 kfree(metadata_stream
);
1306 channel
->ops
->buffer_read_close(buf
);
1311 int lttng_abi_validate_event_param(struct lttng_kernel_event
*event_param
)
1313 /* Limit ABI to implemented features. */
1314 switch (event_param
->instrumentation
) {
1315 case LTTNG_KERNEL_SYSCALL
:
1316 switch (event_param
->u
.syscall
.entryexit
) {
1317 case LTTNG_KERNEL_SYSCALL_ENTRYEXIT
:
1322 switch (event_param
->u
.syscall
.abi
) {
1323 case LTTNG_KERNEL_SYSCALL_ABI_ALL
:
1328 switch (event_param
->u
.syscall
.match
) {
1329 case LTTNG_SYSCALL_MATCH_NAME
:
1336 case LTTNG_KERNEL_TRACEPOINT
: /* Fallthrough */
1337 case LTTNG_KERNEL_KPROBE
: /* Fallthrough */
1338 case LTTNG_KERNEL_KRETPROBE
: /* Fallthrough */
1339 case LTTNG_KERNEL_NOOP
: /* Fallthrough */
1340 case LTTNG_KERNEL_UPROBE
:
1343 case LTTNG_KERNEL_FUNCTION
: /* Fallthrough */
1351 int lttng_abi_create_event(struct file
*channel_file
,
1352 struct lttng_kernel_event
*event_param
)
1354 struct lttng_channel
*channel
= channel_file
->private_data
;
1356 struct file
*event_file
;
1359 event_param
->name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
1360 switch (event_param
->instrumentation
) {
1361 case LTTNG_KERNEL_KRETPROBE
:
1362 event_param
->u
.kretprobe
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
1364 case LTTNG_KERNEL_KPROBE
:
1365 event_param
->u
.kprobe
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
1367 case LTTNG_KERNEL_FUNCTION
:
1369 /* Not implemented. */
1374 event_fd
= lttng_get_unused_fd();
1379 event_file
= anon_inode_getfile("[lttng_event]",
1382 if (IS_ERR(event_file
)) {
1383 ret
= PTR_ERR(event_file
);
1386 /* The event holds a reference on the channel */
1387 if (!atomic_long_add_unless(&channel_file
->f_count
, 1, LONG_MAX
)) {
1389 goto refcount_error
;
1391 ret
= lttng_abi_validate_event_param(event_param
);
1394 if (event_param
->instrumentation
== LTTNG_KERNEL_TRACEPOINT
1395 || event_param
->instrumentation
== LTTNG_KERNEL_SYSCALL
) {
1396 struct lttng_event_enabler
*event_enabler
;
1398 if (strutils_is_star_glob_pattern(event_param
->name
)) {
1400 * If the event name is a star globbing pattern,
1401 * we create the special star globbing enabler.
1403 event_enabler
= lttng_event_enabler_create(LTTNG_ENABLER_FORMAT_STAR_GLOB
,
1404 event_param
, channel
);
1406 event_enabler
= lttng_event_enabler_create(LTTNG_ENABLER_FORMAT_NAME
,
1407 event_param
, channel
);
1409 priv
= event_enabler
;
1411 struct lttng_event
*event
;
1414 * We tolerate no failure path after event creation. It
1415 * will stay invariant for the rest of the session.
1417 event
= lttng_event_create(channel
, event_param
,
1419 event_param
->instrumentation
);
1420 WARN_ON_ONCE(!event
);
1421 if (IS_ERR(event
)) {
1422 ret
= PTR_ERR(event
);
1427 event_file
->private_data
= priv
;
1428 fd_install(event_fd
, event_file
);
1432 atomic_long_dec(&channel_file
->f_count
);
1436 put_unused_fd(event_fd
);
1442 long lttng_event_notifier_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1444 struct lttng_event_notifier_enabler
*event_notifier_enabler
;
1445 enum lttng_event_type
*evtype
= file
->private_data
;
1448 case LTTNG_KERNEL_ENABLE
:
1450 case LTTNG_TYPE_EVENT
:
1452 case LTTNG_TYPE_ENABLER
:
1453 event_notifier_enabler
= file
->private_data
;
1454 return lttng_event_notifier_enabler_enable(event_notifier_enabler
);
1459 case LTTNG_KERNEL_DISABLE
:
1461 case LTTNG_TYPE_EVENT
:
1463 case LTTNG_TYPE_ENABLER
:
1464 event_notifier_enabler
= file
->private_data
;
1465 return lttng_event_notifier_enabler_disable(event_notifier_enabler
);
1470 case LTTNG_KERNEL_FILTER
:
1472 case LTTNG_TYPE_EVENT
:
1474 case LTTNG_TYPE_ENABLER
:
1475 event_notifier_enabler
= file
->private_data
;
1476 return lttng_event_notifier_enabler_attach_bytecode(event_notifier_enabler
,
1477 (struct lttng_kernel_filter_bytecode __user
*) arg
);
1483 return -ENOIOCTLCMD
;
1488 int lttng_event_notifier_release(struct inode
*inode
, struct file
*file
)
1490 struct lttng_event_notifier
*event_notifier
;
1491 struct lttng_event_notifier_enabler
*event_notifier_enabler
;
1492 enum lttng_event_type
*evtype
= file
->private_data
;
1498 case LTTNG_TYPE_EVENT
:
1499 event_notifier
= file
->private_data
;
1501 fput(event_notifier
->group
->file
);
1503 case LTTNG_TYPE_ENABLER
:
1504 event_notifier_enabler
= file
->private_data
;
1505 if (event_notifier_enabler
)
1506 fput(event_notifier_enabler
->group
->file
);
1516 static const struct file_operations lttng_event_notifier_fops
= {
1517 .owner
= THIS_MODULE
,
1518 .release
= lttng_event_notifier_release
,
1519 .unlocked_ioctl
= lttng_event_notifier_ioctl
,
1520 #ifdef CONFIG_COMPAT
1521 .compat_ioctl
= lttng_event_notifier_ioctl
,
1526 int lttng_abi_create_event_notifier(struct file
*event_notifier_group_file
,
1527 struct lttng_kernel_event_notifier
*event_notifier_param
)
1529 struct lttng_event_notifier_group
*event_notifier_group
=
1530 event_notifier_group_file
->private_data
;
1531 int event_notifier_fd
, ret
;
1532 struct file
*event_notifier_file
;
1535 switch (event_notifier_param
->event
.instrumentation
) {
1536 case LTTNG_KERNEL_TRACEPOINT
:
1537 case LTTNG_KERNEL_KPROBE
:
1538 case LTTNG_KERNEL_UPROBE
:
1539 case LTTNG_KERNEL_KRETPROBE
:
1540 case LTTNG_KERNEL_FUNCTION
:
1541 case LTTNG_KERNEL_NOOP
:
1542 case LTTNG_KERNEL_SYSCALL
:
1548 event_notifier_param
->event
.name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
1550 event_notifier_fd
= lttng_get_unused_fd();
1551 if (event_notifier_fd
< 0) {
1552 ret
= event_notifier_fd
;
1556 event_notifier_file
= anon_inode_getfile("[lttng_event_notifier]",
1557 <tng_event_notifier_fops
,
1559 if (IS_ERR(event_notifier_file
)) {
1560 ret
= PTR_ERR(event_notifier_file
);
1564 /* The event notifier holds a reference on the event notifier group. */
1565 if (!atomic_long_add_unless(&event_notifier_group_file
->f_count
, 1, LONG_MAX
)) {
1567 goto refcount_error
;
1570 if (event_notifier_param
->event
.instrumentation
== LTTNG_KERNEL_TRACEPOINT
1571 || event_notifier_param
->event
.instrumentation
== LTTNG_KERNEL_SYSCALL
) {
1572 struct lttng_event_notifier_enabler
*enabler
;
1574 if (strutils_is_star_glob_pattern(event_notifier_param
->event
.name
)) {
1576 * If the event name is a star globbing pattern,
1577 * we create the special star globbing enabler.
1579 enabler
= lttng_event_notifier_enabler_create(
1580 event_notifier_group
,
1581 LTTNG_ENABLER_FORMAT_STAR_GLOB
,
1582 event_notifier_param
);
1584 enabler
= lttng_event_notifier_enabler_create(
1585 event_notifier_group
,
1586 LTTNG_ENABLER_FORMAT_NAME
,
1587 event_notifier_param
);
1591 struct lttng_event_notifier
*event_notifier
;
1594 * We tolerate no failure path after event notifier creation.
1595 * It will stay invariant for the rest of the session.
1597 event_notifier
= lttng_event_notifier_create(NULL
,
1598 event_notifier_param
->event
.token
, event_notifier_group
,
1599 event_notifier_param
, NULL
,
1600 event_notifier_param
->event
.instrumentation
);
1601 WARN_ON_ONCE(!event_notifier
);
1602 if (IS_ERR(event_notifier
)) {
1603 ret
= PTR_ERR(event_notifier
);
1604 goto event_notifier_error
;
1606 priv
= event_notifier
;
1608 event_notifier_file
->private_data
= priv
;
1609 fd_install(event_notifier_fd
, event_notifier_file
);
1610 return event_notifier_fd
;
1612 event_notifier_error
:
1613 atomic_long_dec(&event_notifier_group_file
->f_count
);
1615 fput(event_notifier_file
);
1617 put_unused_fd(event_notifier_fd
);
1624 long lttng_event_notifier_group_ioctl(struct file
*file
, unsigned int cmd
,
1628 case LTTNG_KERNEL_EVENT_NOTIFIER_CREATE
:
1630 struct lttng_kernel_event_notifier uevent_notifier_param
;
1632 if (copy_from_user(&uevent_notifier_param
,
1633 (struct lttng_kernel_event_notifier __user
*) arg
,
1634 sizeof(uevent_notifier_param
)))
1636 return lttng_abi_create_event_notifier(file
, &uevent_notifier_param
);
1639 return -ENOIOCTLCMD
;
1645 int lttng_event_notifier_group_release(struct inode
*inode
, struct file
*file
)
1647 struct lttng_event_notifier_group
*event_notifier_group
=
1650 if (event_notifier_group
)
1651 lttng_event_notifier_group_destroy(event_notifier_group
);
1655 static const struct file_operations lttng_event_notifier_group_fops
= {
1656 .owner
= THIS_MODULE
,
1657 .release
= lttng_event_notifier_group_release
,
1658 .unlocked_ioctl
= lttng_event_notifier_group_ioctl
,
1659 #ifdef CONFIG_COMPAT
1660 .compat_ioctl
= lttng_event_notifier_group_ioctl
,
1665 * lttng_channel_ioctl - lttng syscall through ioctl
1671 * This ioctl implements lttng commands:
1672 * LTTNG_KERNEL_STREAM
1673 * Returns an event stream file descriptor or failure.
1674 * (typically, one event stream records events from one CPU)
1675 * LTTNG_KERNEL_EVENT
1676 * Returns an event file descriptor or failure.
1677 * LTTNG_KERNEL_CONTEXT
1678 * Prepend a context field to each event in the channel
1679 * LTTNG_KERNEL_ENABLE
1680 * Enable recording for events in this channel (weak enable)
1681 * LTTNG_KERNEL_DISABLE
1682 * Disable recording for events in this channel (strong disable)
1684 * Channel and event file descriptors also hold a reference on the session.
1687 long lttng_channel_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1689 struct lttng_channel
*channel
= file
->private_data
;
1692 case LTTNG_KERNEL_OLD_STREAM
:
1693 case LTTNG_KERNEL_STREAM
:
1694 return lttng_abi_open_stream(file
);
1695 case LTTNG_KERNEL_OLD_EVENT
:
1697 struct lttng_kernel_event
*uevent_param
;
1698 struct lttng_kernel_old_event
*old_uevent_param
;
1701 uevent_param
= kmalloc(sizeof(struct lttng_kernel_event
),
1703 if (!uevent_param
) {
1707 old_uevent_param
= kmalloc(
1708 sizeof(struct lttng_kernel_old_event
),
1710 if (!old_uevent_param
) {
1712 goto old_event_error_free_param
;
1714 if (copy_from_user(old_uevent_param
,
1715 (struct lttng_kernel_old_event __user
*) arg
,
1716 sizeof(struct lttng_kernel_old_event
))) {
1718 goto old_event_error_free_old_param
;
1721 memcpy(uevent_param
->name
, old_uevent_param
->name
,
1722 sizeof(uevent_param
->name
));
1723 uevent_param
->instrumentation
=
1724 old_uevent_param
->instrumentation
;
1726 switch (old_uevent_param
->instrumentation
) {
1727 case LTTNG_KERNEL_KPROBE
:
1728 uevent_param
->u
.kprobe
.addr
=
1729 old_uevent_param
->u
.kprobe
.addr
;
1730 uevent_param
->u
.kprobe
.offset
=
1731 old_uevent_param
->u
.kprobe
.offset
;
1732 memcpy(uevent_param
->u
.kprobe
.symbol_name
,
1733 old_uevent_param
->u
.kprobe
.symbol_name
,
1734 sizeof(uevent_param
->u
.kprobe
.symbol_name
));
1736 case LTTNG_KERNEL_KRETPROBE
:
1737 uevent_param
->u
.kretprobe
.addr
=
1738 old_uevent_param
->u
.kretprobe
.addr
;
1739 uevent_param
->u
.kretprobe
.offset
=
1740 old_uevent_param
->u
.kretprobe
.offset
;
1741 memcpy(uevent_param
->u
.kretprobe
.symbol_name
,
1742 old_uevent_param
->u
.kretprobe
.symbol_name
,
1743 sizeof(uevent_param
->u
.kretprobe
.symbol_name
));
1745 case LTTNG_KERNEL_FUNCTION
:
1747 /* Not implemented. */
1752 ret
= lttng_abi_create_event(file
, uevent_param
);
1754 old_event_error_free_old_param
:
1755 kfree(old_uevent_param
);
1756 old_event_error_free_param
:
1757 kfree(uevent_param
);
1761 case LTTNG_KERNEL_EVENT
:
1763 struct lttng_kernel_event uevent_param
;
1765 if (copy_from_user(&uevent_param
,
1766 (struct lttng_kernel_event __user
*) arg
,
1767 sizeof(uevent_param
)))
1769 return lttng_abi_create_event(file
, &uevent_param
);
1771 case LTTNG_KERNEL_OLD_CONTEXT
:
1773 struct lttng_kernel_context
*ucontext_param
;
1774 struct lttng_kernel_old_context
*old_ucontext_param
;
1777 ucontext_param
= kmalloc(sizeof(struct lttng_kernel_context
),
1779 if (!ucontext_param
) {
1783 old_ucontext_param
= kmalloc(sizeof(struct lttng_kernel_old_context
),
1785 if (!old_ucontext_param
) {
1787 goto old_ctx_error_free_param
;
1790 if (copy_from_user(old_ucontext_param
,
1791 (struct lttng_kernel_old_context __user
*) arg
,
1792 sizeof(struct lttng_kernel_old_context
))) {
1794 goto old_ctx_error_free_old_param
;
1796 ucontext_param
->ctx
= old_ucontext_param
->ctx
;
1797 memcpy(ucontext_param
->padding
, old_ucontext_param
->padding
,
1798 sizeof(ucontext_param
->padding
));
1799 /* only type that uses the union */
1800 if (old_ucontext_param
->ctx
== LTTNG_KERNEL_CONTEXT_PERF_COUNTER
) {
1801 ucontext_param
->u
.perf_counter
.type
=
1802 old_ucontext_param
->u
.perf_counter
.type
;
1803 ucontext_param
->u
.perf_counter
.config
=
1804 old_ucontext_param
->u
.perf_counter
.config
;
1805 memcpy(ucontext_param
->u
.perf_counter
.name
,
1806 old_ucontext_param
->u
.perf_counter
.name
,
1807 sizeof(ucontext_param
->u
.perf_counter
.name
));
1810 ret
= lttng_abi_add_context(file
,
1812 &channel
->ctx
, channel
->session
);
1814 old_ctx_error_free_old_param
:
1815 kfree(old_ucontext_param
);
1816 old_ctx_error_free_param
:
1817 kfree(ucontext_param
);
1821 case LTTNG_KERNEL_CONTEXT
:
1823 struct lttng_kernel_context ucontext_param
;
1825 if (copy_from_user(&ucontext_param
,
1826 (struct lttng_kernel_context __user
*) arg
,
1827 sizeof(ucontext_param
)))
1829 return lttng_abi_add_context(file
,
1831 &channel
->ctx
, channel
->session
);
1833 case LTTNG_KERNEL_OLD_ENABLE
:
1834 case LTTNG_KERNEL_ENABLE
:
1835 return lttng_channel_enable(channel
);
1836 case LTTNG_KERNEL_OLD_DISABLE
:
1837 case LTTNG_KERNEL_DISABLE
:
1838 return lttng_channel_disable(channel
);
1839 case LTTNG_KERNEL_SYSCALL_MASK
:
1840 return lttng_channel_syscall_mask(channel
,
1841 (struct lttng_kernel_syscall_mask __user
*) arg
);
1843 return -ENOIOCTLCMD
;
1848 * lttng_metadata_ioctl - lttng syscall through ioctl
1854 * This ioctl implements lttng commands:
1855 * LTTNG_KERNEL_STREAM
1856 * Returns an event stream file descriptor or failure.
1858 * Channel and event file descriptors also hold a reference on the session.
1861 long lttng_metadata_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1864 case LTTNG_KERNEL_OLD_STREAM
:
1865 case LTTNG_KERNEL_STREAM
:
1866 return lttng_abi_open_metadata_stream(file
);
1868 return -ENOIOCTLCMD
;
1873 * lttng_channel_poll - lttng stream addition/removal monitoring
1878 unsigned int lttng_channel_poll(struct file
*file
, poll_table
*wait
)
1880 struct lttng_channel
*channel
= file
->private_data
;
1881 unsigned int mask
= 0;
1883 if (file
->f_mode
& FMODE_READ
) {
1884 poll_wait_set_exclusive(wait
);
1885 poll_wait(file
, channel
->ops
->get_hp_wait_queue(channel
->chan
),
1888 if (channel
->ops
->is_disabled(channel
->chan
))
1890 if (channel
->ops
->is_finalized(channel
->chan
))
1892 if (channel
->ops
->buffer_has_read_closed_stream(channel
->chan
))
1893 return POLLIN
| POLLRDNORM
;
1901 int lttng_channel_release(struct inode
*inode
, struct file
*file
)
1903 struct lttng_channel
*channel
= file
->private_data
;
1906 fput(channel
->session
->file
);
1911 int lttng_metadata_channel_release(struct inode
*inode
, struct file
*file
)
1913 struct lttng_channel
*channel
= file
->private_data
;
1916 fput(channel
->session
->file
);
1917 lttng_metadata_channel_destroy(channel
);
1923 static const struct file_operations lttng_channel_fops
= {
1924 .owner
= THIS_MODULE
,
1925 .release
= lttng_channel_release
,
1926 .poll
= lttng_channel_poll
,
1927 .unlocked_ioctl
= lttng_channel_ioctl
,
1928 #ifdef CONFIG_COMPAT
1929 .compat_ioctl
= lttng_channel_ioctl
,
1933 static const struct file_operations lttng_metadata_fops
= {
1934 .owner
= THIS_MODULE
,
1935 .release
= lttng_metadata_channel_release
,
1936 .unlocked_ioctl
= lttng_metadata_ioctl
,
1937 #ifdef CONFIG_COMPAT
1938 .compat_ioctl
= lttng_metadata_ioctl
,
1943 * lttng_event_ioctl - lttng syscall through ioctl
1949 * This ioctl implements lttng commands:
1950 * LTTNG_KERNEL_CONTEXT
1951 * Prepend a context field to each record of this event
1952 * LTTNG_KERNEL_ENABLE
1953 * Enable recording for this event (weak enable)
1954 * LTTNG_KERNEL_DISABLE
1955 * Disable recording for this event (strong disable)
1958 long lttng_event_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1960 struct lttng_event
*event
;
1961 struct lttng_event_enabler
*event_enabler
;
1962 enum lttng_event_type
*evtype
= file
->private_data
;
1965 case LTTNG_KERNEL_OLD_CONTEXT
:
1967 /* Not implemented */
1970 case LTTNG_KERNEL_CONTEXT
:
1972 /* Not implemented */
1975 case LTTNG_KERNEL_OLD_ENABLE
:
1976 case LTTNG_KERNEL_ENABLE
:
1978 case LTTNG_TYPE_EVENT
:
1979 event
= file
->private_data
;
1980 return lttng_event_enable(event
);
1981 case LTTNG_TYPE_ENABLER
:
1982 event_enabler
= file
->private_data
;
1983 return lttng_event_enabler_enable(event_enabler
);
1988 case LTTNG_KERNEL_OLD_DISABLE
:
1989 case LTTNG_KERNEL_DISABLE
:
1991 case LTTNG_TYPE_EVENT
:
1992 event
= file
->private_data
;
1993 return lttng_event_disable(event
);
1994 case LTTNG_TYPE_ENABLER
:
1995 event_enabler
= file
->private_data
;
1996 return lttng_event_enabler_disable(event_enabler
);
2001 case LTTNG_KERNEL_FILTER
:
2003 case LTTNG_TYPE_EVENT
:
2005 case LTTNG_TYPE_ENABLER
:
2007 event_enabler
= file
->private_data
;
2008 return lttng_event_enabler_attach_bytecode(event_enabler
,
2009 (struct lttng_kernel_filter_bytecode __user
*) arg
);
2015 case LTTNG_KERNEL_ADD_CALLSITE
:
2017 case LTTNG_TYPE_EVENT
:
2018 event
= file
->private_data
;
2019 return lttng_event_add_callsite(event
,
2020 (struct lttng_kernel_event_callsite __user
*) arg
);
2021 case LTTNG_TYPE_ENABLER
:
2028 return -ENOIOCTLCMD
;
2033 int lttng_event_release(struct inode
*inode
, struct file
*file
)
2035 struct lttng_event
*event
;
2036 struct lttng_event_enabler
*event_enabler
;
2037 enum lttng_event_type
*evtype
= file
->private_data
;
2043 case LTTNG_TYPE_EVENT
:
2044 event
= file
->private_data
;
2046 fput(event
->chan
->file
);
2048 case LTTNG_TYPE_ENABLER
:
2049 event_enabler
= file
->private_data
;
2051 fput(event_enabler
->chan
->file
);
2061 /* TODO: filter control ioctl */
2062 static const struct file_operations lttng_event_fops
= {
2063 .owner
= THIS_MODULE
,
2064 .release
= lttng_event_release
,
2065 .unlocked_ioctl
= lttng_event_ioctl
,
2066 #ifdef CONFIG_COMPAT
2067 .compat_ioctl
= lttng_event_ioctl
,
2071 static int put_u64(uint64_t val
, unsigned long arg
)
2073 return put_user(val
, (uint64_t __user
*) arg
);
2076 static int put_u32(uint32_t val
, unsigned long arg
)
2078 return put_user(val
, (uint32_t __user
*) arg
);
2081 static long lttng_stream_ring_buffer_ioctl(struct file
*filp
,
2082 unsigned int cmd
, unsigned long arg
)
2084 struct lib_ring_buffer
*buf
= filp
->private_data
;
2085 struct channel
*chan
= buf
->backend
.chan
;
2086 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
2087 const struct lttng_channel_ops
*ops
= chan
->backend
.priv_ops
;
2090 if (atomic_read(&chan
->record_disabled
))
2094 case LTTNG_RING_BUFFER_GET_TIMESTAMP_BEGIN
:
2098 ret
= ops
->timestamp_begin(config
, buf
, &ts
);
2101 return put_u64(ts
, arg
);
2103 case LTTNG_RING_BUFFER_GET_TIMESTAMP_END
:
2107 ret
= ops
->timestamp_end(config
, buf
, &ts
);
2110 return put_u64(ts
, arg
);
2112 case LTTNG_RING_BUFFER_GET_EVENTS_DISCARDED
:
2116 ret
= ops
->events_discarded(config
, buf
, &ed
);
2119 return put_u64(ed
, arg
);
2121 case LTTNG_RING_BUFFER_GET_CONTENT_SIZE
:
2125 ret
= ops
->content_size(config
, buf
, &cs
);
2128 return put_u64(cs
, arg
);
2130 case LTTNG_RING_BUFFER_GET_PACKET_SIZE
:
2134 ret
= ops
->packet_size(config
, buf
, &ps
);
2137 return put_u64(ps
, arg
);
2139 case LTTNG_RING_BUFFER_GET_STREAM_ID
:
2143 ret
= ops
->stream_id(config
, buf
, &si
);
2146 return put_u64(si
, arg
);
2148 case LTTNG_RING_BUFFER_GET_CURRENT_TIMESTAMP
:
2152 ret
= ops
->current_timestamp(config
, buf
, &ts
);
2155 return put_u64(ts
, arg
);
2157 case LTTNG_RING_BUFFER_GET_SEQ_NUM
:
2161 ret
= ops
->sequence_number(config
, buf
, &seq
);
2164 return put_u64(seq
, arg
);
2166 case LTTNG_RING_BUFFER_INSTANCE_ID
:
2170 ret
= ops
->instance_id(config
, buf
, &id
);
2173 return put_u64(id
, arg
);
2176 return lib_ring_buffer_file_operations
.unlocked_ioctl(filp
,
2184 #ifdef CONFIG_COMPAT
2185 static long lttng_stream_ring_buffer_compat_ioctl(struct file
*filp
,
2186 unsigned int cmd
, unsigned long arg
)
2188 struct lib_ring_buffer
*buf
= filp
->private_data
;
2189 struct channel
*chan
= buf
->backend
.chan
;
2190 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
2191 const struct lttng_channel_ops
*ops
= chan
->backend
.priv_ops
;
2194 if (atomic_read(&chan
->record_disabled
))
2198 case LTTNG_RING_BUFFER_COMPAT_GET_TIMESTAMP_BEGIN
:
2202 ret
= ops
->timestamp_begin(config
, buf
, &ts
);
2205 return put_u64(ts
, arg
);
2207 case LTTNG_RING_BUFFER_COMPAT_GET_TIMESTAMP_END
:
2211 ret
= ops
->timestamp_end(config
, buf
, &ts
);
2214 return put_u64(ts
, arg
);
2216 case LTTNG_RING_BUFFER_COMPAT_GET_EVENTS_DISCARDED
:
2220 ret
= ops
->events_discarded(config
, buf
, &ed
);
2223 return put_u64(ed
, arg
);
2225 case LTTNG_RING_BUFFER_COMPAT_GET_CONTENT_SIZE
:
2229 ret
= ops
->content_size(config
, buf
, &cs
);
2232 return put_u64(cs
, arg
);
2234 case LTTNG_RING_BUFFER_COMPAT_GET_PACKET_SIZE
:
2238 ret
= ops
->packet_size(config
, buf
, &ps
);
2241 return put_u64(ps
, arg
);
2243 case LTTNG_RING_BUFFER_COMPAT_GET_STREAM_ID
:
2247 ret
= ops
->stream_id(config
, buf
, &si
);
2250 return put_u64(si
, arg
);
2252 case LTTNG_RING_BUFFER_GET_CURRENT_TIMESTAMP
:
2256 ret
= ops
->current_timestamp(config
, buf
, &ts
);
2259 return put_u64(ts
, arg
);
2261 case LTTNG_RING_BUFFER_COMPAT_GET_SEQ_NUM
:
2265 ret
= ops
->sequence_number(config
, buf
, &seq
);
2268 return put_u64(seq
, arg
);
2270 case LTTNG_RING_BUFFER_COMPAT_INSTANCE_ID
:
2274 ret
= ops
->instance_id(config
, buf
, &id
);
2277 return put_u64(id
, arg
);
2280 return lib_ring_buffer_file_operations
.compat_ioctl(filp
,
2287 #endif /* CONFIG_COMPAT */
2289 static void lttng_stream_override_ring_buffer_fops(void)
2291 lttng_stream_ring_buffer_file_operations
.owner
= THIS_MODULE
;
2292 lttng_stream_ring_buffer_file_operations
.open
=
2293 lib_ring_buffer_file_operations
.open
;
2294 lttng_stream_ring_buffer_file_operations
.release
=
2295 lib_ring_buffer_file_operations
.release
;
2296 lttng_stream_ring_buffer_file_operations
.poll
=
2297 lib_ring_buffer_file_operations
.poll
;
2298 lttng_stream_ring_buffer_file_operations
.splice_read
=
2299 lib_ring_buffer_file_operations
.splice_read
;
2300 lttng_stream_ring_buffer_file_operations
.mmap
=
2301 lib_ring_buffer_file_operations
.mmap
;
2302 lttng_stream_ring_buffer_file_operations
.unlocked_ioctl
=
2303 lttng_stream_ring_buffer_ioctl
;
2304 lttng_stream_ring_buffer_file_operations
.llseek
=
2305 lib_ring_buffer_file_operations
.llseek
;
2306 #ifdef CONFIG_COMPAT
2307 lttng_stream_ring_buffer_file_operations
.compat_ioctl
=
2308 lttng_stream_ring_buffer_compat_ioctl
;
2312 int __init
lttng_abi_init(void)
2316 wrapper_vmalloc_sync_mappings();
2319 ret
= lttng_tp_mempool_init();
2324 lttng_proc_dentry
= proc_create_data("lttng", S_IRUSR
| S_IWUSR
, NULL
,
2325 <tng_proc_ops
, NULL
);
2327 if (!lttng_proc_dentry
) {
2328 printk(KERN_ERR
"LTTng: Error creating control file\n");
2332 lttng_stream_override_ring_buffer_fops();
2336 lttng_tp_mempool_destroy();
2337 lttng_clock_unref();
2341 /* No __exit annotation because used by init error path too. */
2342 void lttng_abi_exit(void)
2344 lttng_tp_mempool_destroy();
2345 lttng_clock_unref();
2346 if (lttng_proc_dentry
)
2347 remove_proc_entry("lttng", NULL
);