1 /* SPDX-License-Identifier: (GPL-2.0 or LGPL-2.1)
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/compiler_attributes.h>
34 #include <wrapper/vmalloc.h> /* for wrapper_vmalloc_sync_mappings() */
35 #include <wrapper/ringbuffer/vfs.h>
36 #include <wrapper/ringbuffer/backend.h>
37 #include <wrapper/ringbuffer/frontend.h>
38 #include <wrapper/poll.h>
39 #include <wrapper/file.h>
40 #include <wrapper/kref.h>
41 #include <lttng-string-utils.h>
42 #include <lttng-abi.h>
43 #include <lttng-abi-old.h>
44 #include <lttng-events.h>
45 #include <lttng-tracer.h>
46 #include <lttng-tp-mempool.h>
47 #include <lib/ringbuffer/frontend_types.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 (LTTNG_LINUX_VERSION_CODE >= LTTNG_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_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_tracepoint_list(void)
112 struct file
*tracepoint_list_file
;
115 file_fd
= lttng_get_unused_fd();
121 tracepoint_list_file
= anon_inode_getfile("[lttng_tracepoint_list]",
122 <tng_tracepoint_list_fops
,
124 if (IS_ERR(tracepoint_list_file
)) {
125 ret
= PTR_ERR(tracepoint_list_file
);
128 ret
= lttng_tracepoint_list_fops
.open(NULL
, tracepoint_list_file
);
131 fd_install(file_fd
, tracepoint_list_file
);
135 fput(tracepoint_list_file
);
137 put_unused_fd(file_fd
);
142 #ifndef CONFIG_HAVE_SYSCALL_TRACEPOINTS
144 int lttng_abi_syscall_list(void)
150 int lttng_abi_syscall_list(void)
152 struct file
*syscall_list_file
;
155 file_fd
= lttng_get_unused_fd();
161 syscall_list_file
= anon_inode_getfile("[lttng_syscall_list]",
162 <tng_syscall_list_fops
,
164 if (IS_ERR(syscall_list_file
)) {
165 ret
= PTR_ERR(syscall_list_file
);
168 ret
= lttng_syscall_list_fops
.open(NULL
, syscall_list_file
);
171 fd_install(file_fd
, syscall_list_file
);
175 fput(syscall_list_file
);
177 put_unused_fd(file_fd
);
184 void lttng_abi_tracer_version(struct lttng_kernel_tracer_version
*v
)
186 v
->major
= LTTNG_MODULES_MAJOR_VERSION
;
187 v
->minor
= LTTNG_MODULES_MINOR_VERSION
;
188 v
->patchlevel
= LTTNG_MODULES_PATCHLEVEL_VERSION
;
192 void lttng_abi_tracer_abi_version(struct lttng_kernel_tracer_abi_version
*v
)
194 v
->major
= LTTNG_MODULES_ABI_MAJOR_VERSION
;
195 v
->minor
= LTTNG_MODULES_ABI_MINOR_VERSION
;
199 long lttng_abi_add_context(struct file
*file
,
200 struct lttng_kernel_context
*context_param
,
201 struct lttng_ctx
**ctx
, struct lttng_session
*session
)
204 if (session
->been_active
)
207 switch (context_param
->ctx
) {
208 case LTTNG_KERNEL_CONTEXT_PID
:
209 return lttng_add_pid_to_ctx(ctx
);
210 case LTTNG_KERNEL_CONTEXT_PRIO
:
211 return lttng_add_prio_to_ctx(ctx
);
212 case LTTNG_KERNEL_CONTEXT_NICE
:
213 return lttng_add_nice_to_ctx(ctx
);
214 case LTTNG_KERNEL_CONTEXT_VPID
:
215 return lttng_add_vpid_to_ctx(ctx
);
216 case LTTNG_KERNEL_CONTEXT_TID
:
217 return lttng_add_tid_to_ctx(ctx
);
218 case LTTNG_KERNEL_CONTEXT_VTID
:
219 return lttng_add_vtid_to_ctx(ctx
);
220 case LTTNG_KERNEL_CONTEXT_PPID
:
221 return lttng_add_ppid_to_ctx(ctx
);
222 case LTTNG_KERNEL_CONTEXT_VPPID
:
223 return lttng_add_vppid_to_ctx(ctx
);
224 case LTTNG_KERNEL_CONTEXT_PERF_COUNTER
:
225 context_param
->u
.perf_counter
.name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
226 return lttng_add_perf_counter_to_ctx(context_param
->u
.perf_counter
.type
,
227 context_param
->u
.perf_counter
.config
,
228 context_param
->u
.perf_counter
.name
,
230 case LTTNG_KERNEL_CONTEXT_PROCNAME
:
231 return lttng_add_procname_to_ctx(ctx
);
232 case LTTNG_KERNEL_CONTEXT_HOSTNAME
:
233 return lttng_add_hostname_to_ctx(ctx
);
234 case LTTNG_KERNEL_CONTEXT_CPU_ID
:
235 return lttng_add_cpu_id_to_ctx(ctx
);
236 case LTTNG_KERNEL_CONTEXT_INTERRUPTIBLE
:
237 return lttng_add_interruptible_to_ctx(ctx
);
238 case LTTNG_KERNEL_CONTEXT_NEED_RESCHEDULE
:
239 return lttng_add_need_reschedule_to_ctx(ctx
);
240 case LTTNG_KERNEL_CONTEXT_PREEMPTIBLE
:
241 return lttng_add_preemptible_to_ctx(ctx
);
242 case LTTNG_KERNEL_CONTEXT_MIGRATABLE
:
243 return lttng_add_migratable_to_ctx(ctx
);
244 case LTTNG_KERNEL_CONTEXT_CALLSTACK_KERNEL
:
245 case LTTNG_KERNEL_CONTEXT_CALLSTACK_USER
:
246 return lttng_add_callstack_to_ctx(ctx
, context_param
->ctx
);
247 case LTTNG_KERNEL_CONTEXT_CGROUP_NS
:
248 return lttng_add_cgroup_ns_to_ctx(ctx
);
249 case LTTNG_KERNEL_CONTEXT_IPC_NS
:
250 return lttng_add_ipc_ns_to_ctx(ctx
);
251 case LTTNG_KERNEL_CONTEXT_MNT_NS
:
252 return lttng_add_mnt_ns_to_ctx(ctx
);
253 case LTTNG_KERNEL_CONTEXT_NET_NS
:
254 return lttng_add_net_ns_to_ctx(ctx
);
255 case LTTNG_KERNEL_CONTEXT_PID_NS
:
256 return lttng_add_pid_ns_to_ctx(ctx
);
257 case LTTNG_KERNEL_CONTEXT_USER_NS
:
258 return lttng_add_user_ns_to_ctx(ctx
);
259 case LTTNG_KERNEL_CONTEXT_UTS_NS
:
260 return lttng_add_uts_ns_to_ctx(ctx
);
261 case LTTNG_KERNEL_CONTEXT_UID
:
262 return lttng_add_uid_to_ctx(ctx
);
263 case LTTNG_KERNEL_CONTEXT_EUID
:
264 return lttng_add_euid_to_ctx(ctx
);
265 case LTTNG_KERNEL_CONTEXT_SUID
:
266 return lttng_add_suid_to_ctx(ctx
);
267 case LTTNG_KERNEL_CONTEXT_GID
:
268 return lttng_add_gid_to_ctx(ctx
);
269 case LTTNG_KERNEL_CONTEXT_EGID
:
270 return lttng_add_egid_to_ctx(ctx
);
271 case LTTNG_KERNEL_CONTEXT_SGID
:
272 return lttng_add_sgid_to_ctx(ctx
);
273 case LTTNG_KERNEL_CONTEXT_VUID
:
274 return lttng_add_vuid_to_ctx(ctx
);
275 case LTTNG_KERNEL_CONTEXT_VEUID
:
276 return lttng_add_veuid_to_ctx(ctx
);
277 case LTTNG_KERNEL_CONTEXT_VSUID
:
278 return lttng_add_vsuid_to_ctx(ctx
);
279 case LTTNG_KERNEL_CONTEXT_VGID
:
280 return lttng_add_vgid_to_ctx(ctx
);
281 case LTTNG_KERNEL_CONTEXT_VEGID
:
282 return lttng_add_vegid_to_ctx(ctx
);
283 case LTTNG_KERNEL_CONTEXT_VSGID
:
284 return lttng_add_vsgid_to_ctx(ctx
);
291 * lttng_ioctl - lttng syscall through ioctl
297 * This ioctl implements lttng commands:
298 * LTTNG_KERNEL_SESSION
299 * Returns a LTTng trace session file descriptor
300 * LTTNG_KERNEL_TRACER_VERSION
301 * Returns the LTTng kernel tracer version
302 * LTTNG_KERNEL_TRACEPOINT_LIST
303 * Returns a file descriptor listing available tracepoints
304 * LTTNG_KERNEL_WAIT_QUIESCENT
305 * Returns after all previously running probes have completed
306 * LTTNG_KERNEL_TRACER_ABI_VERSION
307 * Returns the LTTng kernel tracer ABI version
309 * The returned session will be deleted when its file descriptor is closed.
312 long lttng_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
315 case LTTNG_KERNEL_OLD_SESSION
:
316 case LTTNG_KERNEL_SESSION
:
317 return lttng_abi_create_session();
318 case LTTNG_KERNEL_OLD_TRACER_VERSION
:
320 struct lttng_kernel_tracer_version v
;
321 struct lttng_kernel_old_tracer_version oldv
;
322 struct lttng_kernel_old_tracer_version
*uversion
=
323 (struct lttng_kernel_old_tracer_version __user
*) arg
;
325 lttng_abi_tracer_version(&v
);
326 oldv
.major
= v
.major
;
327 oldv
.minor
= v
.minor
;
328 oldv
.patchlevel
= v
.patchlevel
;
330 if (copy_to_user(uversion
, &oldv
, sizeof(oldv
)))
334 case LTTNG_KERNEL_TRACER_VERSION
:
336 struct lttng_kernel_tracer_version version
;
337 struct lttng_kernel_tracer_version
*uversion
=
338 (struct lttng_kernel_tracer_version __user
*) arg
;
340 lttng_abi_tracer_version(&version
);
342 if (copy_to_user(uversion
, &version
, sizeof(version
)))
346 case LTTNG_KERNEL_TRACER_ABI_VERSION
:
348 struct lttng_kernel_tracer_abi_version version
;
349 struct lttng_kernel_tracer_abi_version
*uversion
=
350 (struct lttng_kernel_tracer_abi_version __user
*) arg
;
352 lttng_abi_tracer_abi_version(&version
);
354 if (copy_to_user(uversion
, &version
, sizeof(version
)))
358 case LTTNG_KERNEL_OLD_TRACEPOINT_LIST
:
359 case LTTNG_KERNEL_TRACEPOINT_LIST
:
360 return lttng_abi_tracepoint_list();
361 case LTTNG_KERNEL_SYSCALL_LIST
:
362 return lttng_abi_syscall_list();
363 case LTTNG_KERNEL_OLD_WAIT_QUIESCENT
:
364 case LTTNG_KERNEL_WAIT_QUIESCENT
:
367 case LTTNG_KERNEL_OLD_CALIBRATE
:
369 struct lttng_kernel_old_calibrate __user
*ucalibrate
=
370 (struct lttng_kernel_old_calibrate __user
*) arg
;
371 struct lttng_kernel_old_calibrate old_calibrate
;
372 struct lttng_kernel_calibrate calibrate
;
375 if (copy_from_user(&old_calibrate
, ucalibrate
, sizeof(old_calibrate
)))
377 calibrate
.type
= old_calibrate
.type
;
378 ret
= lttng_calibrate(&calibrate
);
379 if (copy_to_user(ucalibrate
, &old_calibrate
, sizeof(old_calibrate
)))
383 case LTTNG_KERNEL_CALIBRATE
:
385 struct lttng_kernel_calibrate __user
*ucalibrate
=
386 (struct lttng_kernel_calibrate __user
*) arg
;
387 struct lttng_kernel_calibrate calibrate
;
390 if (copy_from_user(&calibrate
, ucalibrate
, sizeof(calibrate
)))
392 ret
= lttng_calibrate(&calibrate
);
393 if (copy_to_user(ucalibrate
, &calibrate
, sizeof(calibrate
)))
402 #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(5,6,0))
403 static const struct proc_ops lttng_proc_ops
= {
404 .proc_ioctl
= lttng_ioctl
,
406 .proc_compat_ioctl
= lttng_ioctl
,
407 #endif /* CONFIG_COMPAT */
410 static const struct file_operations lttng_proc_ops
= {
411 .owner
= THIS_MODULE
,
412 .unlocked_ioctl
= lttng_ioctl
,
414 .compat_ioctl
= lttng_ioctl
,
415 #endif /* CONFIG_COMPAT */
420 int lttng_abi_create_channel(struct file
*session_file
,
421 struct lttng_kernel_channel
*chan_param
,
422 enum channel_type channel_type
)
424 struct lttng_session
*session
= session_file
->private_data
;
425 const struct file_operations
*fops
= NULL
;
426 const char *transport_name
;
427 struct lttng_channel
*chan
;
428 struct file
*chan_file
;
432 chan_fd
= lttng_get_unused_fd();
437 switch (channel_type
) {
438 case PER_CPU_CHANNEL
:
439 fops
= <tng_channel_fops
;
441 case METADATA_CHANNEL
:
442 fops
= <tng_metadata_fops
;
446 chan_file
= anon_inode_getfile("[lttng_channel]",
449 if (IS_ERR(chan_file
)) {
450 ret
= PTR_ERR(chan_file
);
453 switch (channel_type
) {
454 case PER_CPU_CHANNEL
:
455 if (chan_param
->output
== LTTNG_KERNEL_SPLICE
) {
456 transport_name
= chan_param
->overwrite
?
457 "relay-overwrite" : "relay-discard";
458 } else if (chan_param
->output
== LTTNG_KERNEL_MMAP
) {
459 transport_name
= chan_param
->overwrite
?
460 "relay-overwrite-mmap" : "relay-discard-mmap";
465 case METADATA_CHANNEL
:
466 if (chan_param
->output
== LTTNG_KERNEL_SPLICE
)
467 transport_name
= "relay-metadata";
468 else if (chan_param
->output
== LTTNG_KERNEL_MMAP
)
469 transport_name
= "relay-metadata-mmap";
474 transport_name
= "<unknown>";
477 if (!atomic_long_add_unless(&session_file
->f_count
, 1, LONG_MAX
)) {
482 * We tolerate no failure path after channel creation. It will stay
483 * invariant for the rest of the session.
485 chan
= lttng_channel_create(session
, transport_name
, NULL
,
486 chan_param
->subbuf_size
,
487 chan_param
->num_subbuf
,
488 chan_param
->switch_timer_interval
,
489 chan_param
->read_timer_interval
,
495 chan
->file
= chan_file
;
496 chan_file
->private_data
= chan
;
497 fd_install(chan_fd
, chan_file
);
502 atomic_long_dec(&session_file
->f_count
);
506 put_unused_fd(chan_fd
);
512 int lttng_abi_session_set_name(struct lttng_session
*session
,
513 struct lttng_kernel_session_name
*name
)
517 len
= strnlen(name
->name
, LTTNG_KERNEL_SESSION_NAME_LEN
);
519 if (len
== LTTNG_KERNEL_SESSION_NAME_LEN
) {
520 /* Name is too long/malformed */
524 strcpy(session
->name
, name
->name
);
529 int lttng_abi_session_set_creation_time(struct lttng_session
*session
,
530 struct lttng_kernel_session_creation_time
*time
)
534 len
= strnlen(time
->iso8601
, LTTNG_KERNEL_SESSION_CREATION_TIME_ISO8601_LEN
);
536 if (len
== LTTNG_KERNEL_SESSION_CREATION_TIME_ISO8601_LEN
) {
537 /* Time is too long/malformed */
541 strcpy(session
->creation_time
, time
->iso8601
);
546 enum tracker_type
get_tracker_type(struct lttng_kernel_tracker_args
*tracker
)
548 switch (tracker
->type
) {
549 case LTTNG_KERNEL_TRACKER_PID
:
551 case LTTNG_KERNEL_TRACKER_VPID
:
553 case LTTNG_KERNEL_TRACKER_UID
:
555 case LTTNG_KERNEL_TRACKER_VUID
:
557 case LTTNG_KERNEL_TRACKER_GID
:
559 case LTTNG_KERNEL_TRACKER_VGID
:
562 return TRACKER_UNKNOWN
;
567 * lttng_session_ioctl - lttng session fd ioctl
573 * This ioctl implements lttng commands:
574 * LTTNG_KERNEL_CHANNEL
575 * Returns a LTTng channel file descriptor
576 * LTTNG_KERNEL_ENABLE
577 * Enables tracing for a session (weak enable)
578 * LTTNG_KERNEL_DISABLE
579 * Disables tracing for a session (strong disable)
580 * LTTNG_KERNEL_METADATA
581 * Returns a LTTng metadata file descriptor
582 * LTTNG_KERNEL_SESSION_TRACK_PID
583 * Add PID to session PID tracker
584 * LTTNG_KERNEL_SESSION_UNTRACK_PID
585 * Remove PID from session PID tracker
586 * LTTNG_KERNEL_SESSION_TRACK_ID
588 * LTTNG_KERNEL_SESSION_UNTRACK_ID
589 * Remove ID from tracker
591 * The returned channel will be deleted when its file descriptor is closed.
594 long lttng_session_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
596 struct lttng_session
*session
= file
->private_data
;
597 struct lttng_kernel_channel chan_param
;
598 struct lttng_kernel_old_channel old_chan_param
;
601 case LTTNG_KERNEL_OLD_CHANNEL
:
603 if (copy_from_user(&old_chan_param
,
604 (struct lttng_kernel_old_channel __user
*) arg
,
605 sizeof(struct lttng_kernel_old_channel
)))
607 chan_param
.overwrite
= old_chan_param
.overwrite
;
608 chan_param
.subbuf_size
= old_chan_param
.subbuf_size
;
609 chan_param
.num_subbuf
= old_chan_param
.num_subbuf
;
610 chan_param
.switch_timer_interval
= old_chan_param
.switch_timer_interval
;
611 chan_param
.read_timer_interval
= old_chan_param
.read_timer_interval
;
612 chan_param
.output
= old_chan_param
.output
;
614 return lttng_abi_create_channel(file
, &chan_param
,
617 case LTTNG_KERNEL_CHANNEL
:
619 if (copy_from_user(&chan_param
,
620 (struct lttng_kernel_channel __user
*) arg
,
621 sizeof(struct lttng_kernel_channel
)))
623 return lttng_abi_create_channel(file
, &chan_param
,
626 case LTTNG_KERNEL_OLD_SESSION_START
:
627 case LTTNG_KERNEL_OLD_ENABLE
:
628 case LTTNG_KERNEL_SESSION_START
:
629 case LTTNG_KERNEL_ENABLE
:
630 return lttng_session_enable(session
);
631 case LTTNG_KERNEL_OLD_SESSION_STOP
:
632 case LTTNG_KERNEL_OLD_DISABLE
:
633 case LTTNG_KERNEL_SESSION_STOP
:
634 case LTTNG_KERNEL_DISABLE
:
635 return lttng_session_disable(session
);
636 case LTTNG_KERNEL_OLD_METADATA
:
638 if (copy_from_user(&old_chan_param
,
639 (struct lttng_kernel_old_channel __user
*) arg
,
640 sizeof(struct lttng_kernel_old_channel
)))
642 chan_param
.overwrite
= old_chan_param
.overwrite
;
643 chan_param
.subbuf_size
= old_chan_param
.subbuf_size
;
644 chan_param
.num_subbuf
= old_chan_param
.num_subbuf
;
645 chan_param
.switch_timer_interval
= old_chan_param
.switch_timer_interval
;
646 chan_param
.read_timer_interval
= old_chan_param
.read_timer_interval
;
647 chan_param
.output
= old_chan_param
.output
;
649 return lttng_abi_create_channel(file
, &chan_param
,
652 case LTTNG_KERNEL_METADATA
:
654 if (copy_from_user(&chan_param
,
655 (struct lttng_kernel_channel __user
*) arg
,
656 sizeof(struct lttng_kernel_channel
)))
658 return lttng_abi_create_channel(file
, &chan_param
,
661 case LTTNG_KERNEL_SESSION_TRACK_PID
:
662 return lttng_session_track_id(session
, TRACKER_PID
, (int) arg
);
663 case LTTNG_KERNEL_SESSION_UNTRACK_PID
:
664 return lttng_session_untrack_id(session
, TRACKER_PID
, (int) arg
);
665 case LTTNG_KERNEL_SESSION_TRACK_ID
:
667 struct lttng_kernel_tracker_args tracker
;
668 enum tracker_type tracker_type
;
670 if (copy_from_user(&tracker
,
671 (struct lttng_kernel_tracker_args __user
*) arg
,
672 sizeof(struct lttng_kernel_tracker_args
)))
674 tracker_type
= get_tracker_type(&tracker
);
675 if (tracker_type
== TRACKER_UNKNOWN
)
677 return lttng_session_track_id(session
, tracker_type
, tracker
.id
);
679 case LTTNG_KERNEL_SESSION_UNTRACK_ID
:
681 struct lttng_kernel_tracker_args tracker
;
682 enum tracker_type tracker_type
;
684 if (copy_from_user(&tracker
,
685 (struct lttng_kernel_tracker_args __user
*) arg
,
686 sizeof(struct lttng_kernel_tracker_args
)))
688 tracker_type
= get_tracker_type(&tracker
);
689 if (tracker_type
== TRACKER_UNKNOWN
)
691 return lttng_session_untrack_id(session
, tracker_type
,
694 case LTTNG_KERNEL_SESSION_LIST_TRACKER_PIDS
:
695 return lttng_session_list_tracker_ids(session
, TRACKER_PID
);
696 case LTTNG_KERNEL_SESSION_LIST_TRACKER_IDS
:
698 struct lttng_kernel_tracker_args tracker
;
699 enum tracker_type tracker_type
;
701 if (copy_from_user(&tracker
,
702 (struct lttng_kernel_tracker_args __user
*) arg
,
703 sizeof(struct lttng_kernel_tracker_args
)))
705 tracker_type
= get_tracker_type(&tracker
);
706 if (tracker_type
== TRACKER_UNKNOWN
)
708 return lttng_session_list_tracker_ids(session
, tracker_type
);
710 case LTTNG_KERNEL_SESSION_METADATA_REGEN
:
711 return lttng_session_metadata_regenerate(session
);
712 case LTTNG_KERNEL_SESSION_STATEDUMP
:
713 return lttng_session_statedump(session
);
714 case LTTNG_KERNEL_SESSION_SET_NAME
:
716 struct lttng_kernel_session_name name
;
718 if (copy_from_user(&name
,
719 (struct lttng_kernel_session_name __user
*) arg
,
720 sizeof(struct lttng_kernel_session_name
)))
722 return lttng_abi_session_set_name(session
, &name
);
724 case LTTNG_KERNEL_SESSION_SET_CREATION_TIME
:
726 struct lttng_kernel_session_creation_time time
;
728 if (copy_from_user(&time
,
729 (struct lttng_kernel_session_creation_time __user
*) arg
,
730 sizeof(struct lttng_kernel_session_creation_time
)))
732 return lttng_abi_session_set_creation_time(session
, &time
);
740 * Called when the last file reference is dropped.
742 * Big fat note: channels and events are invariant for the whole session after
743 * their creation. So this session destruction also destroys all channel and
744 * event structures specific to this session (they are not destroyed when their
745 * individual file is released).
748 int lttng_session_release(struct inode
*inode
, struct file
*file
)
750 struct lttng_session
*session
= file
->private_data
;
753 lttng_session_destroy(session
);
757 static const struct file_operations lttng_session_fops
= {
758 .owner
= THIS_MODULE
,
759 .release
= lttng_session_release
,
760 .unlocked_ioctl
= lttng_session_ioctl
,
762 .compat_ioctl
= lttng_session_ioctl
,
767 * lttng_metadata_ring_buffer_poll - LTTng ring buffer poll file operation
771 * Handles the poll operations for the metadata channels.
774 unsigned int lttng_metadata_ring_buffer_poll(struct file
*filp
,
777 struct lttng_metadata_stream
*stream
= filp
->private_data
;
778 struct lib_ring_buffer
*buf
= stream
->priv
;
780 unsigned int mask
= 0;
782 if (filp
->f_mode
& FMODE_READ
) {
783 poll_wait_set_exclusive(wait
);
784 poll_wait(filp
, &stream
->read_wait
, wait
);
786 finalized
= stream
->finalized
;
789 * lib_ring_buffer_is_finalized() contains a smp_rmb()
790 * ordering finalized load before offsets loads.
792 WARN_ON(atomic_long_read(&buf
->active_readers
) != 1);
797 mutex_lock(&stream
->metadata_cache
->lock
);
798 if (stream
->metadata_cache
->metadata_written
>
799 stream
->metadata_out
)
801 mutex_unlock(&stream
->metadata_cache
->lock
);
808 void lttng_metadata_ring_buffer_ioctl_put_next_subbuf(struct file
*filp
,
809 unsigned int cmd
, unsigned long arg
)
811 struct lttng_metadata_stream
*stream
= filp
->private_data
;
813 stream
->metadata_out
= stream
->metadata_in
;
817 * Reset the counter of how much metadata has been consumed to 0. That way,
818 * the consumer receives the content of the metadata cache unchanged. This is
819 * different from the metadata_regenerate where the offset from epoch is
820 * resampled, here we want the exact same content as the last time the metadata
821 * was generated. This command is only possible if all the metadata written
822 * in the cache has been output to the metadata stream to avoid corrupting the
825 * Return 0 on success, a negative value on error.
828 int lttng_metadata_cache_dump(struct lttng_metadata_stream
*stream
)
831 struct lttng_metadata_cache
*cache
= stream
->metadata_cache
;
833 mutex_lock(&cache
->lock
);
834 if (stream
->metadata_out
!= cache
->metadata_written
) {
838 stream
->metadata_out
= 0;
839 stream
->metadata_in
= 0;
840 wake_up_interruptible(&stream
->read_wait
);
844 mutex_unlock(&cache
->lock
);
849 long lttng_metadata_ring_buffer_ioctl(struct file
*filp
,
850 unsigned int cmd
, unsigned long arg
)
853 struct lttng_metadata_stream
*stream
= filp
->private_data
;
854 struct lib_ring_buffer
*buf
= stream
->priv
;
858 if (cmd
== RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK
)
859 rb_cmd
= RING_BUFFER_GET_NEXT_SUBBUF
;
864 case RING_BUFFER_GET_NEXT_SUBBUF
:
866 struct lttng_metadata_stream
*stream
= filp
->private_data
;
867 struct lib_ring_buffer
*buf
= stream
->priv
;
868 struct channel
*chan
= buf
->backend
.chan
;
870 ret
= lttng_metadata_output_channel(stream
, chan
, NULL
);
872 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
878 case RING_BUFFER_GET_SUBBUF
:
881 * Random access is not allowed for metadata channel.
885 case RING_BUFFER_FLUSH_EMPTY
:
887 case RING_BUFFER_FLUSH
:
889 struct lttng_metadata_stream
*stream
= filp
->private_data
;
890 struct lib_ring_buffer
*buf
= stream
->priv
;
891 struct channel
*chan
= buf
->backend
.chan
;
894 * Before doing the actual ring buffer flush, write up to one
895 * packet of metadata in the ring buffer.
897 ret
= lttng_metadata_output_channel(stream
, chan
, NULL
);
902 case RING_BUFFER_GET_METADATA_VERSION
:
904 struct lttng_metadata_stream
*stream
= filp
->private_data
;
906 return put_u64(stream
->version
, arg
);
908 case RING_BUFFER_METADATA_CACHE_DUMP
:
910 struct lttng_metadata_stream
*stream
= filp
->private_data
;
912 return lttng_metadata_cache_dump(stream
);
914 case RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK
:
916 struct lttng_metadata_stream
*stream
= filp
->private_data
;
917 struct lib_ring_buffer
*buf
= stream
->priv
;
918 struct channel
*chan
= buf
->backend
.chan
;
920 ret
= lttng_metadata_output_channel(stream
, chan
, &coherent
);
922 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
924 } else if (ret
< 0) {
932 /* PUT_SUBBUF is the one from lib ring buffer, unmodified. */
934 /* Performing lib ring buffer ioctl after our own. */
935 ret
= lib_ring_buffer_ioctl(filp
, rb_cmd
, arg
, buf
);
940 case RING_BUFFER_PUT_NEXT_SUBBUF
:
942 lttng_metadata_ring_buffer_ioctl_put_next_subbuf(filp
,
946 case RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK
:
948 return put_u32(coherent
, arg
);
959 long lttng_metadata_ring_buffer_compat_ioctl(struct file
*filp
,
960 unsigned int cmd
, unsigned long arg
)
963 struct lttng_metadata_stream
*stream
= filp
->private_data
;
964 struct lib_ring_buffer
*buf
= stream
->priv
;
968 if (cmd
== RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK
)
969 rb_cmd
= RING_BUFFER_GET_NEXT_SUBBUF
;
974 case RING_BUFFER_GET_NEXT_SUBBUF
:
976 struct lttng_metadata_stream
*stream
= filp
->private_data
;
977 struct lib_ring_buffer
*buf
= stream
->priv
;
978 struct channel
*chan
= buf
->backend
.chan
;
980 ret
= lttng_metadata_output_channel(stream
, chan
, NULL
);
982 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
988 case RING_BUFFER_GET_SUBBUF
:
991 * Random access is not allowed for metadata channel.
995 case RING_BUFFER_FLUSH_EMPTY
:
997 case RING_BUFFER_FLUSH
:
999 struct lttng_metadata_stream
*stream
= filp
->private_data
;
1000 struct lib_ring_buffer
*buf
= stream
->priv
;
1001 struct channel
*chan
= buf
->backend
.chan
;
1004 * Before doing the actual ring buffer flush, write up to one
1005 * packet of metadata in the ring buffer.
1007 ret
= lttng_metadata_output_channel(stream
, chan
, NULL
);
1012 case RING_BUFFER_GET_METADATA_VERSION
:
1014 struct lttng_metadata_stream
*stream
= filp
->private_data
;
1016 return put_u64(stream
->version
, arg
);
1018 case RING_BUFFER_METADATA_CACHE_DUMP
:
1020 struct lttng_metadata_stream
*stream
= filp
->private_data
;
1022 return lttng_metadata_cache_dump(stream
);
1024 case RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK
:
1026 struct lttng_metadata_stream
*stream
= filp
->private_data
;
1027 struct lib_ring_buffer
*buf
= stream
->priv
;
1028 struct channel
*chan
= buf
->backend
.chan
;
1030 ret
= lttng_metadata_output_channel(stream
, chan
, &coherent
);
1032 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
1034 } else if (ret
< 0) {
1042 /* PUT_SUBBUF is the one from lib ring buffer, unmodified. */
1044 /* Performing lib ring buffer ioctl after our own. */
1045 ret
= lib_ring_buffer_compat_ioctl(filp
, rb_cmd
, arg
, buf
);
1050 case RING_BUFFER_PUT_NEXT_SUBBUF
:
1052 lttng_metadata_ring_buffer_ioctl_put_next_subbuf(filp
,
1056 case RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK
:
1058 return put_u32(coherent
, arg
);
1069 * This is not used by anonymous file descriptors. This code is left
1070 * there if we ever want to implement an inode with open() operation.
1073 int lttng_metadata_ring_buffer_open(struct inode
*inode
, struct file
*file
)
1075 struct lttng_metadata_stream
*stream
= inode
->i_private
;
1076 struct lib_ring_buffer
*buf
= stream
->priv
;
1078 file
->private_data
= buf
;
1080 * Since life-time of metadata cache differs from that of
1081 * session, we need to keep our own reference on the transport.
1083 if (!try_module_get(stream
->transport
->owner
)) {
1084 printk(KERN_WARNING
"LTT : Can't lock transport module.\n");
1087 return lib_ring_buffer_open(inode
, file
, buf
);
1091 int lttng_metadata_ring_buffer_release(struct inode
*inode
, struct file
*file
)
1093 struct lttng_metadata_stream
*stream
= file
->private_data
;
1094 struct lib_ring_buffer
*buf
= stream
->priv
;
1096 mutex_lock(&stream
->metadata_cache
->lock
);
1097 list_del(&stream
->list
);
1098 mutex_unlock(&stream
->metadata_cache
->lock
);
1099 kref_put(&stream
->metadata_cache
->refcount
, metadata_cache_destroy
);
1100 module_put(stream
->transport
->owner
);
1102 return lib_ring_buffer_release(inode
, file
, buf
);
1106 ssize_t
lttng_metadata_ring_buffer_splice_read(struct file
*in
, loff_t
*ppos
,
1107 struct pipe_inode_info
*pipe
, size_t len
,
1110 struct lttng_metadata_stream
*stream
= in
->private_data
;
1111 struct lib_ring_buffer
*buf
= stream
->priv
;
1113 return lib_ring_buffer_splice_read(in
, ppos
, pipe
, len
,
1118 int lttng_metadata_ring_buffer_mmap(struct file
*filp
,
1119 struct vm_area_struct
*vma
)
1121 struct lttng_metadata_stream
*stream
= filp
->private_data
;
1122 struct lib_ring_buffer
*buf
= stream
->priv
;
1124 return lib_ring_buffer_mmap(filp
, vma
, buf
);
1128 const struct file_operations lttng_metadata_ring_buffer_file_operations
= {
1129 .owner
= THIS_MODULE
,
1130 .open
= lttng_metadata_ring_buffer_open
,
1131 .release
= lttng_metadata_ring_buffer_release
,
1132 .poll
= lttng_metadata_ring_buffer_poll
,
1133 .splice_read
= lttng_metadata_ring_buffer_splice_read
,
1134 .mmap
= lttng_metadata_ring_buffer_mmap
,
1135 .unlocked_ioctl
= lttng_metadata_ring_buffer_ioctl
,
1136 .llseek
= vfs_lib_ring_buffer_no_llseek
,
1137 #ifdef CONFIG_COMPAT
1138 .compat_ioctl
= lttng_metadata_ring_buffer_compat_ioctl
,
1143 int lttng_abi_create_stream_fd(struct file
*channel_file
, void *stream_priv
,
1144 const struct file_operations
*fops
)
1147 struct file
*stream_file
;
1149 stream_fd
= lttng_get_unused_fd();
1150 if (stream_fd
< 0) {
1154 stream_file
= anon_inode_getfile("[lttng_stream]", fops
,
1155 stream_priv
, O_RDWR
);
1156 if (IS_ERR(stream_file
)) {
1157 ret
= PTR_ERR(stream_file
);
1161 * OPEN_FMODE, called within anon_inode_getfile/alloc_file, don't honor
1162 * FMODE_LSEEK, FMODE_PREAD nor FMODE_PWRITE. We need to read from this
1163 * file descriptor, so we set FMODE_PREAD here.
1165 stream_file
->f_mode
|= FMODE_PREAD
;
1166 fd_install(stream_fd
, stream_file
);
1168 * The stream holds a reference to the channel within the generic ring
1169 * buffer library, so no need to hold a refcount on the channel and
1170 * session files here.
1175 put_unused_fd(stream_fd
);
1181 int lttng_abi_open_stream(struct file
*channel_file
)
1183 struct lttng_channel
*channel
= channel_file
->private_data
;
1184 struct lib_ring_buffer
*buf
;
1188 buf
= channel
->ops
->buffer_read_open(channel
->chan
);
1193 ret
= lttng_abi_create_stream_fd(channel_file
, stream_priv
,
1194 <tng_stream_ring_buffer_file_operations
);
1201 channel
->ops
->buffer_read_close(buf
);
1206 int lttng_abi_open_metadata_stream(struct file
*channel_file
)
1208 struct lttng_channel
*channel
= channel_file
->private_data
;
1209 struct lttng_session
*session
= channel
->session
;
1210 struct lib_ring_buffer
*buf
;
1212 struct lttng_metadata_stream
*metadata_stream
;
1215 buf
= channel
->ops
->buffer_read_open(channel
->chan
);
1219 metadata_stream
= kzalloc(sizeof(struct lttng_metadata_stream
),
1221 if (!metadata_stream
) {
1225 metadata_stream
->metadata_cache
= session
->metadata_cache
;
1226 init_waitqueue_head(&metadata_stream
->read_wait
);
1227 metadata_stream
->priv
= buf
;
1228 stream_priv
= metadata_stream
;
1229 metadata_stream
->transport
= channel
->transport
;
1230 /* Initial state is an empty metadata, considered as incoherent. */
1231 metadata_stream
->coherent
= false;
1234 * Since life-time of metadata cache differs from that of
1235 * session, we need to keep our own reference on the transport.
1237 if (!try_module_get(metadata_stream
->transport
->owner
)) {
1238 printk(KERN_WARNING
"LTT : Can't lock transport module.\n");
1243 if (!lttng_kref_get(&session
->metadata_cache
->refcount
)) {
1248 ret
= lttng_abi_create_stream_fd(channel_file
, stream_priv
,
1249 <tng_metadata_ring_buffer_file_operations
);
1253 mutex_lock(&session
->metadata_cache
->lock
);
1254 list_add(&metadata_stream
->list
,
1255 &session
->metadata_cache
->metadata_stream
);
1256 mutex_unlock(&session
->metadata_cache
->lock
);
1260 kref_put(&session
->metadata_cache
->refcount
, metadata_cache_destroy
);
1262 module_put(metadata_stream
->transport
->owner
);
1264 kfree(metadata_stream
);
1266 channel
->ops
->buffer_read_close(buf
);
1271 int lttng_abi_validate_event_param(struct lttng_kernel_event
*event_param
)
1273 /* Limit ABI to implemented features. */
1274 switch (event_param
->instrumentation
) {
1275 case LTTNG_KERNEL_SYSCALL
:
1276 switch (event_param
->u
.syscall
.entryexit
) {
1277 case LTTNG_KERNEL_SYSCALL_ENTRYEXIT
:
1282 switch (event_param
->u
.syscall
.abi
) {
1283 case LTTNG_KERNEL_SYSCALL_ABI_ALL
:
1288 switch (event_param
->u
.syscall
.match
) {
1289 case LTTNG_SYSCALL_MATCH_NAME
:
1296 case LTTNG_KERNEL_TRACEPOINT
:
1298 case LTTNG_KERNEL_KPROBE
:
1300 case LTTNG_KERNEL_KRETPROBE
:
1302 case LTTNG_KERNEL_NOOP
:
1304 case LTTNG_KERNEL_UPROBE
:
1307 case LTTNG_KERNEL_FUNCTION
:
1316 int lttng_abi_create_event(struct file
*channel_file
,
1317 struct lttng_kernel_event
*event_param
)
1319 struct lttng_channel
*channel
= channel_file
->private_data
;
1321 struct file
*event_file
;
1324 event_param
->name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
1325 switch (event_param
->instrumentation
) {
1326 case LTTNG_KERNEL_KRETPROBE
:
1327 event_param
->u
.kretprobe
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
1329 case LTTNG_KERNEL_KPROBE
:
1330 event_param
->u
.kprobe
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
1332 case LTTNG_KERNEL_FUNCTION
:
1334 /* Not implemented. */
1339 event_fd
= lttng_get_unused_fd();
1344 event_file
= anon_inode_getfile("[lttng_event]",
1347 if (IS_ERR(event_file
)) {
1348 ret
= PTR_ERR(event_file
);
1351 /* The event holds a reference on the channel */
1352 if (!atomic_long_add_unless(&channel_file
->f_count
, 1, LONG_MAX
)) {
1354 goto refcount_error
;
1356 ret
= lttng_abi_validate_event_param(event_param
);
1359 if (event_param
->instrumentation
== LTTNG_KERNEL_TRACEPOINT
1360 || event_param
->instrumentation
== LTTNG_KERNEL_SYSCALL
) {
1361 struct lttng_enabler
*enabler
;
1363 if (strutils_is_star_glob_pattern(event_param
->name
)) {
1365 * If the event name is a star globbing pattern,
1366 * we create the special star globbing enabler.
1368 enabler
= lttng_enabler_create(LTTNG_ENABLER_STAR_GLOB
,
1369 event_param
, channel
);
1371 enabler
= lttng_enabler_create(LTTNG_ENABLER_NAME
,
1372 event_param
, channel
);
1376 struct lttng_event
*event
;
1379 * We tolerate no failure path after event creation. It
1380 * will stay invariant for the rest of the session.
1382 event
= lttng_event_create(channel
, event_param
,
1384 event_param
->instrumentation
);
1385 WARN_ON_ONCE(!event
);
1386 if (IS_ERR(event
)) {
1387 ret
= PTR_ERR(event
);
1392 event_file
->private_data
= priv
;
1393 fd_install(event_fd
, event_file
);
1397 atomic_long_dec(&channel_file
->f_count
);
1401 put_unused_fd(event_fd
);
1407 * lttng_channel_ioctl - lttng syscall through ioctl
1413 * This ioctl implements lttng commands:
1414 * LTTNG_KERNEL_STREAM
1415 * Returns an event stream file descriptor or failure.
1416 * (typically, one event stream records events from one CPU)
1417 * LTTNG_KERNEL_EVENT
1418 * Returns an event file descriptor or failure.
1419 * LTTNG_KERNEL_CONTEXT
1420 * Prepend a context field to each event in the channel
1421 * LTTNG_KERNEL_ENABLE
1422 * Enable recording for events in this channel (weak enable)
1423 * LTTNG_KERNEL_DISABLE
1424 * Disable recording for events in this channel (strong disable)
1426 * Channel and event file descriptors also hold a reference on the session.
1429 long lttng_channel_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1431 struct lttng_channel
*channel
= file
->private_data
;
1434 case LTTNG_KERNEL_OLD_STREAM
:
1435 case LTTNG_KERNEL_STREAM
:
1436 return lttng_abi_open_stream(file
);
1437 case LTTNG_KERNEL_OLD_EVENT
:
1439 struct lttng_kernel_event
*uevent_param
;
1440 struct lttng_kernel_old_event
*old_uevent_param
;
1443 uevent_param
= kmalloc(sizeof(struct lttng_kernel_event
),
1445 if (!uevent_param
) {
1449 old_uevent_param
= kmalloc(
1450 sizeof(struct lttng_kernel_old_event
),
1452 if (!old_uevent_param
) {
1454 goto old_event_error_free_param
;
1456 if (copy_from_user(old_uevent_param
,
1457 (struct lttng_kernel_old_event __user
*) arg
,
1458 sizeof(struct lttng_kernel_old_event
))) {
1460 goto old_event_error_free_old_param
;
1463 memcpy(uevent_param
->name
, old_uevent_param
->name
,
1464 sizeof(uevent_param
->name
));
1465 uevent_param
->instrumentation
=
1466 old_uevent_param
->instrumentation
;
1468 switch (old_uevent_param
->instrumentation
) {
1469 case LTTNG_KERNEL_KPROBE
:
1470 uevent_param
->u
.kprobe
.addr
=
1471 old_uevent_param
->u
.kprobe
.addr
;
1472 uevent_param
->u
.kprobe
.offset
=
1473 old_uevent_param
->u
.kprobe
.offset
;
1474 memcpy(uevent_param
->u
.kprobe
.symbol_name
,
1475 old_uevent_param
->u
.kprobe
.symbol_name
,
1476 sizeof(uevent_param
->u
.kprobe
.symbol_name
));
1478 case LTTNG_KERNEL_KRETPROBE
:
1479 uevent_param
->u
.kretprobe
.addr
=
1480 old_uevent_param
->u
.kretprobe
.addr
;
1481 uevent_param
->u
.kretprobe
.offset
=
1482 old_uevent_param
->u
.kretprobe
.offset
;
1483 memcpy(uevent_param
->u
.kretprobe
.symbol_name
,
1484 old_uevent_param
->u
.kretprobe
.symbol_name
,
1485 sizeof(uevent_param
->u
.kretprobe
.symbol_name
));
1487 case LTTNG_KERNEL_FUNCTION
:
1489 /* Not implemented. */
1494 ret
= lttng_abi_create_event(file
, uevent_param
);
1496 old_event_error_free_old_param
:
1497 kfree(old_uevent_param
);
1498 old_event_error_free_param
:
1499 kfree(uevent_param
);
1503 case LTTNG_KERNEL_EVENT
:
1505 struct lttng_kernel_event uevent_param
;
1507 if (copy_from_user(&uevent_param
,
1508 (struct lttng_kernel_event __user
*) arg
,
1509 sizeof(uevent_param
)))
1511 return lttng_abi_create_event(file
, &uevent_param
);
1513 case LTTNG_KERNEL_OLD_CONTEXT
:
1515 struct lttng_kernel_context
*ucontext_param
;
1516 struct lttng_kernel_old_context
*old_ucontext_param
;
1519 ucontext_param
= kmalloc(sizeof(struct lttng_kernel_context
),
1521 if (!ucontext_param
) {
1525 old_ucontext_param
= kmalloc(sizeof(struct lttng_kernel_old_context
),
1527 if (!old_ucontext_param
) {
1529 goto old_ctx_error_free_param
;
1532 if (copy_from_user(old_ucontext_param
,
1533 (struct lttng_kernel_old_context __user
*) arg
,
1534 sizeof(struct lttng_kernel_old_context
))) {
1536 goto old_ctx_error_free_old_param
;
1538 ucontext_param
->ctx
= old_ucontext_param
->ctx
;
1539 memcpy(ucontext_param
->padding
, old_ucontext_param
->padding
,
1540 sizeof(ucontext_param
->padding
));
1541 /* only type that uses the union */
1542 if (old_ucontext_param
->ctx
== LTTNG_KERNEL_CONTEXT_PERF_COUNTER
) {
1543 ucontext_param
->u
.perf_counter
.type
=
1544 old_ucontext_param
->u
.perf_counter
.type
;
1545 ucontext_param
->u
.perf_counter
.config
=
1546 old_ucontext_param
->u
.perf_counter
.config
;
1547 memcpy(ucontext_param
->u
.perf_counter
.name
,
1548 old_ucontext_param
->u
.perf_counter
.name
,
1549 sizeof(ucontext_param
->u
.perf_counter
.name
));
1552 ret
= lttng_abi_add_context(file
,
1554 &channel
->ctx
, channel
->session
);
1556 old_ctx_error_free_old_param
:
1557 kfree(old_ucontext_param
);
1558 old_ctx_error_free_param
:
1559 kfree(ucontext_param
);
1563 case LTTNG_KERNEL_CONTEXT
:
1565 struct lttng_kernel_context ucontext_param
;
1567 if (copy_from_user(&ucontext_param
,
1568 (struct lttng_kernel_context __user
*) arg
,
1569 sizeof(ucontext_param
)))
1571 return lttng_abi_add_context(file
,
1573 &channel
->ctx
, channel
->session
);
1575 case LTTNG_KERNEL_OLD_ENABLE
:
1576 case LTTNG_KERNEL_ENABLE
:
1577 return lttng_channel_enable(channel
);
1578 case LTTNG_KERNEL_OLD_DISABLE
:
1579 case LTTNG_KERNEL_DISABLE
:
1580 return lttng_channel_disable(channel
);
1581 case LTTNG_KERNEL_SYSCALL_MASK
:
1582 return lttng_channel_syscall_mask(channel
,
1583 (struct lttng_kernel_syscall_mask __user
*) arg
);
1585 return -ENOIOCTLCMD
;
1590 * lttng_metadata_ioctl - lttng syscall through ioctl
1596 * This ioctl implements lttng commands:
1597 * LTTNG_KERNEL_STREAM
1598 * Returns an event stream file descriptor or failure.
1600 * Channel and event file descriptors also hold a reference on the session.
1603 long lttng_metadata_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1606 case LTTNG_KERNEL_OLD_STREAM
:
1607 case LTTNG_KERNEL_STREAM
:
1608 return lttng_abi_open_metadata_stream(file
);
1610 return -ENOIOCTLCMD
;
1615 * lttng_channel_poll - lttng stream addition/removal monitoring
1620 unsigned int lttng_channel_poll(struct file
*file
, poll_table
*wait
)
1622 struct lttng_channel
*channel
= file
->private_data
;
1623 unsigned int mask
= 0;
1625 if (file
->f_mode
& FMODE_READ
) {
1626 poll_wait_set_exclusive(wait
);
1627 poll_wait(file
, channel
->ops
->get_hp_wait_queue(channel
->chan
),
1630 if (channel
->ops
->is_disabled(channel
->chan
))
1632 if (channel
->ops
->is_finalized(channel
->chan
))
1634 if (channel
->ops
->buffer_has_read_closed_stream(channel
->chan
))
1635 return POLLIN
| POLLRDNORM
;
1643 int lttng_channel_release(struct inode
*inode
, struct file
*file
)
1645 struct lttng_channel
*channel
= file
->private_data
;
1648 fput(channel
->session
->file
);
1653 int lttng_metadata_channel_release(struct inode
*inode
, struct file
*file
)
1655 struct lttng_channel
*channel
= file
->private_data
;
1658 fput(channel
->session
->file
);
1659 lttng_metadata_channel_destroy(channel
);
1665 static const struct file_operations lttng_channel_fops
= {
1666 .owner
= THIS_MODULE
,
1667 .release
= lttng_channel_release
,
1668 .poll
= lttng_channel_poll
,
1669 .unlocked_ioctl
= lttng_channel_ioctl
,
1670 #ifdef CONFIG_COMPAT
1671 .compat_ioctl
= lttng_channel_ioctl
,
1675 static const struct file_operations lttng_metadata_fops
= {
1676 .owner
= THIS_MODULE
,
1677 .release
= lttng_metadata_channel_release
,
1678 .unlocked_ioctl
= lttng_metadata_ioctl
,
1679 #ifdef CONFIG_COMPAT
1680 .compat_ioctl
= lttng_metadata_ioctl
,
1685 * lttng_event_ioctl - lttng syscall through ioctl
1691 * This ioctl implements lttng commands:
1692 * LTTNG_KERNEL_CONTEXT
1693 * Prepend a context field to each record of this event
1694 * LTTNG_KERNEL_ENABLE
1695 * Enable recording for this event (weak enable)
1696 * LTTNG_KERNEL_DISABLE
1697 * Disable recording for this event (strong disable)
1700 long lttng_event_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1702 struct lttng_event
*event
;
1703 struct lttng_enabler
*enabler
;
1704 enum lttng_event_type
*evtype
= file
->private_data
;
1707 case LTTNG_KERNEL_OLD_CONTEXT
:
1709 /* Not implemented */
1712 case LTTNG_KERNEL_CONTEXT
:
1714 /* Not implemented */
1717 case LTTNG_KERNEL_OLD_ENABLE
:
1718 case LTTNG_KERNEL_ENABLE
:
1720 case LTTNG_TYPE_EVENT
:
1721 event
= file
->private_data
;
1722 return lttng_event_enable(event
);
1723 case LTTNG_TYPE_ENABLER
:
1724 enabler
= file
->private_data
;
1725 return lttng_enabler_enable(enabler
);
1730 case LTTNG_KERNEL_OLD_DISABLE
:
1731 case LTTNG_KERNEL_DISABLE
:
1733 case LTTNG_TYPE_EVENT
:
1734 event
= file
->private_data
;
1735 return lttng_event_disable(event
);
1736 case LTTNG_TYPE_ENABLER
:
1737 enabler
= file
->private_data
;
1738 return lttng_enabler_disable(enabler
);
1743 case LTTNG_KERNEL_FILTER
:
1745 case LTTNG_TYPE_EVENT
:
1747 case LTTNG_TYPE_ENABLER
:
1749 enabler
= file
->private_data
;
1750 return lttng_enabler_attach_bytecode(enabler
,
1751 (struct lttng_kernel_filter_bytecode __user
*) arg
);
1757 case LTTNG_KERNEL_ADD_CALLSITE
:
1759 case LTTNG_TYPE_EVENT
:
1760 event
= file
->private_data
;
1761 return lttng_event_add_callsite(event
,
1762 (struct lttng_kernel_event_callsite __user
*) arg
);
1763 case LTTNG_TYPE_ENABLER
:
1770 return -ENOIOCTLCMD
;
1775 int lttng_event_release(struct inode
*inode
, struct file
*file
)
1777 struct lttng_event
*event
;
1778 struct lttng_enabler
*enabler
;
1779 enum lttng_event_type
*evtype
= file
->private_data
;
1785 case LTTNG_TYPE_EVENT
:
1786 event
= file
->private_data
;
1788 fput(event
->chan
->file
);
1790 case LTTNG_TYPE_ENABLER
:
1791 enabler
= file
->private_data
;
1793 fput(enabler
->chan
->file
);
1803 /* TODO: filter control ioctl */
1804 static const struct file_operations lttng_event_fops
= {
1805 .owner
= THIS_MODULE
,
1806 .release
= lttng_event_release
,
1807 .unlocked_ioctl
= lttng_event_ioctl
,
1808 #ifdef CONFIG_COMPAT
1809 .compat_ioctl
= lttng_event_ioctl
,
1813 static int put_u64(uint64_t val
, unsigned long arg
)
1815 return put_user(val
, (uint64_t __user
*) arg
);
1818 static int put_u32(uint32_t val
, unsigned long arg
)
1820 return put_user(val
, (uint32_t __user
*) arg
);
1823 static long lttng_stream_ring_buffer_ioctl(struct file
*filp
,
1824 unsigned int cmd
, unsigned long arg
)
1826 struct lib_ring_buffer
*buf
= filp
->private_data
;
1827 struct channel
*chan
= buf
->backend
.chan
;
1828 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1829 const struct lttng_channel_ops
*ops
= chan
->backend
.priv_ops
;
1832 if (atomic_read(&chan
->record_disabled
))
1836 case LTTNG_RING_BUFFER_GET_TIMESTAMP_BEGIN
:
1840 ret
= ops
->timestamp_begin(config
, buf
, &ts
);
1843 return put_u64(ts
, arg
);
1845 case LTTNG_RING_BUFFER_GET_TIMESTAMP_END
:
1849 ret
= ops
->timestamp_end(config
, buf
, &ts
);
1852 return put_u64(ts
, arg
);
1854 case LTTNG_RING_BUFFER_GET_EVENTS_DISCARDED
:
1858 ret
= ops
->events_discarded(config
, buf
, &ed
);
1861 return put_u64(ed
, arg
);
1863 case LTTNG_RING_BUFFER_GET_CONTENT_SIZE
:
1867 ret
= ops
->content_size(config
, buf
, &cs
);
1870 return put_u64(cs
, arg
);
1872 case LTTNG_RING_BUFFER_GET_PACKET_SIZE
:
1876 ret
= ops
->packet_size(config
, buf
, &ps
);
1879 return put_u64(ps
, arg
);
1881 case LTTNG_RING_BUFFER_GET_STREAM_ID
:
1885 ret
= ops
->stream_id(config
, buf
, &si
);
1888 return put_u64(si
, arg
);
1890 case LTTNG_RING_BUFFER_GET_CURRENT_TIMESTAMP
:
1894 ret
= ops
->current_timestamp(config
, buf
, &ts
);
1897 return put_u64(ts
, arg
);
1899 case LTTNG_RING_BUFFER_GET_SEQ_NUM
:
1903 ret
= ops
->sequence_number(config
, buf
, &seq
);
1906 return put_u64(seq
, arg
);
1908 case LTTNG_RING_BUFFER_INSTANCE_ID
:
1912 ret
= ops
->instance_id(config
, buf
, &id
);
1915 return put_u64(id
, arg
);
1918 return lib_ring_buffer_file_operations
.unlocked_ioctl(filp
,
1926 #ifdef CONFIG_COMPAT
1927 static long lttng_stream_ring_buffer_compat_ioctl(struct file
*filp
,
1928 unsigned int cmd
, unsigned long arg
)
1930 struct lib_ring_buffer
*buf
= filp
->private_data
;
1931 struct channel
*chan
= buf
->backend
.chan
;
1932 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1933 const struct lttng_channel_ops
*ops
= chan
->backend
.priv_ops
;
1936 if (atomic_read(&chan
->record_disabled
))
1940 case LTTNG_RING_BUFFER_COMPAT_GET_TIMESTAMP_BEGIN
:
1944 ret
= ops
->timestamp_begin(config
, buf
, &ts
);
1947 return put_u64(ts
, arg
);
1949 case LTTNG_RING_BUFFER_COMPAT_GET_TIMESTAMP_END
:
1953 ret
= ops
->timestamp_end(config
, buf
, &ts
);
1956 return put_u64(ts
, arg
);
1958 case LTTNG_RING_BUFFER_COMPAT_GET_EVENTS_DISCARDED
:
1962 ret
= ops
->events_discarded(config
, buf
, &ed
);
1965 return put_u64(ed
, arg
);
1967 case LTTNG_RING_BUFFER_COMPAT_GET_CONTENT_SIZE
:
1971 ret
= ops
->content_size(config
, buf
, &cs
);
1974 return put_u64(cs
, arg
);
1976 case LTTNG_RING_BUFFER_COMPAT_GET_PACKET_SIZE
:
1980 ret
= ops
->packet_size(config
, buf
, &ps
);
1983 return put_u64(ps
, arg
);
1985 case LTTNG_RING_BUFFER_COMPAT_GET_STREAM_ID
:
1989 ret
= ops
->stream_id(config
, buf
, &si
);
1992 return put_u64(si
, arg
);
1994 case LTTNG_RING_BUFFER_GET_CURRENT_TIMESTAMP
:
1998 ret
= ops
->current_timestamp(config
, buf
, &ts
);
2001 return put_u64(ts
, arg
);
2003 case LTTNG_RING_BUFFER_COMPAT_GET_SEQ_NUM
:
2007 ret
= ops
->sequence_number(config
, buf
, &seq
);
2010 return put_u64(seq
, arg
);
2012 case LTTNG_RING_BUFFER_COMPAT_INSTANCE_ID
:
2016 ret
= ops
->instance_id(config
, buf
, &id
);
2019 return put_u64(id
, arg
);
2022 return lib_ring_buffer_file_operations
.compat_ioctl(filp
,
2029 #endif /* CONFIG_COMPAT */
2031 static void lttng_stream_override_ring_buffer_fops(void)
2033 lttng_stream_ring_buffer_file_operations
.owner
= THIS_MODULE
;
2034 lttng_stream_ring_buffer_file_operations
.open
=
2035 lib_ring_buffer_file_operations
.open
;
2036 lttng_stream_ring_buffer_file_operations
.release
=
2037 lib_ring_buffer_file_operations
.release
;
2038 lttng_stream_ring_buffer_file_operations
.poll
=
2039 lib_ring_buffer_file_operations
.poll
;
2040 lttng_stream_ring_buffer_file_operations
.splice_read
=
2041 lib_ring_buffer_file_operations
.splice_read
;
2042 lttng_stream_ring_buffer_file_operations
.mmap
=
2043 lib_ring_buffer_file_operations
.mmap
;
2044 lttng_stream_ring_buffer_file_operations
.unlocked_ioctl
=
2045 lttng_stream_ring_buffer_ioctl
;
2046 lttng_stream_ring_buffer_file_operations
.llseek
=
2047 lib_ring_buffer_file_operations
.llseek
;
2048 #ifdef CONFIG_COMPAT
2049 lttng_stream_ring_buffer_file_operations
.compat_ioctl
=
2050 lttng_stream_ring_buffer_compat_ioctl
;
2054 int __init
lttng_abi_init(void)
2058 wrapper_vmalloc_sync_mappings();
2061 ret
= lttng_tp_mempool_init();
2066 lttng_proc_dentry
= proc_create_data("lttng", S_IRUSR
| S_IWUSR
, NULL
,
2067 <tng_proc_ops
, NULL
);
2069 if (!lttng_proc_dentry
) {
2070 printk(KERN_ERR
"Error creating LTTng control file\n");
2074 lttng_stream_override_ring_buffer_fops();
2078 lttng_tp_mempool_destroy();
2079 lttng_clock_unref();
2083 /* No __exit annotation because used by init error path too. */
2084 void lttng_abi_exit(void)
2086 lttng_tp_mempool_destroy();
2087 lttng_clock_unref();
2088 if (lttng_proc_dentry
)
2089 remove_proc_entry("lttng", NULL
);