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/vmalloc.h> /* for wrapper_vmalloc_sync_mappings() */
34 #include <wrapper/ringbuffer/vfs.h>
35 #include <wrapper/ringbuffer/backend.h>
36 #include <wrapper/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 <lib/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_channel_fops
;
63 static const struct file_operations lttng_metadata_fops
;
64 static const struct file_operations lttng_event_fops
;
65 static struct file_operations lttng_stream_ring_buffer_file_operations
;
67 static int put_u64(uint64_t val
, unsigned long arg
);
68 static int put_u32(uint32_t val
, unsigned long arg
);
71 * Teardown management: opened file descriptors keep a refcount on the module,
72 * so it can only exit when all file descriptors are closed.
76 int lttng_abi_create_session(void)
78 struct lttng_session
*session
;
79 struct file
*session_file
;
82 session
= lttng_session_create();
85 session_fd
= lttng_get_unused_fd();
90 session_file
= anon_inode_getfile("[lttng_session]",
93 if (IS_ERR(session_file
)) {
94 ret
= PTR_ERR(session_file
);
97 session
->file
= session_file
;
98 fd_install(session_fd
, session_file
);
102 put_unused_fd(session_fd
);
104 lttng_session_destroy(session
);
109 int lttng_abi_tracepoint_list(void)
111 struct file
*tracepoint_list_file
;
114 file_fd
= lttng_get_unused_fd();
120 tracepoint_list_file
= anon_inode_getfile("[lttng_tracepoint_list]",
121 <tng_tracepoint_list_fops
,
123 if (IS_ERR(tracepoint_list_file
)) {
124 ret
= PTR_ERR(tracepoint_list_file
);
127 ret
= lttng_tracepoint_list_fops
.open(NULL
, tracepoint_list_file
);
130 fd_install(file_fd
, tracepoint_list_file
);
134 fput(tracepoint_list_file
);
136 put_unused_fd(file_fd
);
141 #ifndef CONFIG_HAVE_SYSCALL_TRACEPOINTS
143 int lttng_abi_syscall_list(void)
149 int lttng_abi_syscall_list(void)
151 struct file
*syscall_list_file
;
154 file_fd
= lttng_get_unused_fd();
160 syscall_list_file
= anon_inode_getfile("[lttng_syscall_list]",
161 <tng_syscall_list_fops
,
163 if (IS_ERR(syscall_list_file
)) {
164 ret
= PTR_ERR(syscall_list_file
);
167 ret
= lttng_syscall_list_fops
.open(NULL
, syscall_list_file
);
170 fd_install(file_fd
, syscall_list_file
);
174 fput(syscall_list_file
);
176 put_unused_fd(file_fd
);
183 void lttng_abi_tracer_version(struct lttng_kernel_tracer_version
*v
)
185 v
->major
= LTTNG_MODULES_MAJOR_VERSION
;
186 v
->minor
= LTTNG_MODULES_MINOR_VERSION
;
187 v
->patchlevel
= LTTNG_MODULES_PATCHLEVEL_VERSION
;
191 void lttng_abi_tracer_abi_version(struct lttng_kernel_tracer_abi_version
*v
)
193 v
->major
= LTTNG_MODULES_ABI_MAJOR_VERSION
;
194 v
->minor
= LTTNG_MODULES_ABI_MINOR_VERSION
;
198 long lttng_abi_add_context(struct file
*file
,
199 struct lttng_kernel_context
*context_param
,
200 struct lttng_ctx
**ctx
, struct lttng_session
*session
)
203 if (session
->been_active
)
206 switch (context_param
->ctx
) {
207 case LTTNG_KERNEL_CONTEXT_PID
:
208 return lttng_add_pid_to_ctx(ctx
);
209 case LTTNG_KERNEL_CONTEXT_PRIO
:
210 return lttng_add_prio_to_ctx(ctx
);
211 case LTTNG_KERNEL_CONTEXT_NICE
:
212 return lttng_add_nice_to_ctx(ctx
);
213 case LTTNG_KERNEL_CONTEXT_VPID
:
214 return lttng_add_vpid_to_ctx(ctx
);
215 case LTTNG_KERNEL_CONTEXT_TID
:
216 return lttng_add_tid_to_ctx(ctx
);
217 case LTTNG_KERNEL_CONTEXT_VTID
:
218 return lttng_add_vtid_to_ctx(ctx
);
219 case LTTNG_KERNEL_CONTEXT_PPID
:
220 return lttng_add_ppid_to_ctx(ctx
);
221 case LTTNG_KERNEL_CONTEXT_VPPID
:
222 return lttng_add_vppid_to_ctx(ctx
);
223 case LTTNG_KERNEL_CONTEXT_PERF_COUNTER
:
224 context_param
->u
.perf_counter
.name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
225 return lttng_add_perf_counter_to_ctx(context_param
->u
.perf_counter
.type
,
226 context_param
->u
.perf_counter
.config
,
227 context_param
->u
.perf_counter
.name
,
229 case LTTNG_KERNEL_CONTEXT_PROCNAME
:
230 return lttng_add_procname_to_ctx(ctx
);
231 case LTTNG_KERNEL_CONTEXT_HOSTNAME
:
232 return lttng_add_hostname_to_ctx(ctx
);
233 case LTTNG_KERNEL_CONTEXT_CPU_ID
:
234 return lttng_add_cpu_id_to_ctx(ctx
);
235 case LTTNG_KERNEL_CONTEXT_INTERRUPTIBLE
:
236 return lttng_add_interruptible_to_ctx(ctx
);
237 case LTTNG_KERNEL_CONTEXT_NEED_RESCHEDULE
:
238 return lttng_add_need_reschedule_to_ctx(ctx
);
239 case LTTNG_KERNEL_CONTEXT_PREEMPTIBLE
:
240 return lttng_add_preemptible_to_ctx(ctx
);
241 case LTTNG_KERNEL_CONTEXT_MIGRATABLE
:
242 return lttng_add_migratable_to_ctx(ctx
);
243 case LTTNG_KERNEL_CONTEXT_CALLSTACK_KERNEL
:
244 case LTTNG_KERNEL_CONTEXT_CALLSTACK_USER
:
245 return lttng_add_callstack_to_ctx(ctx
, context_param
->ctx
);
246 case LTTNG_KERNEL_CONTEXT_CGROUP_NS
:
247 return lttng_add_cgroup_ns_to_ctx(ctx
);
248 case LTTNG_KERNEL_CONTEXT_IPC_NS
:
249 return lttng_add_ipc_ns_to_ctx(ctx
);
250 case LTTNG_KERNEL_CONTEXT_MNT_NS
:
251 return lttng_add_mnt_ns_to_ctx(ctx
);
252 case LTTNG_KERNEL_CONTEXT_NET_NS
:
253 return lttng_add_net_ns_to_ctx(ctx
);
254 case LTTNG_KERNEL_CONTEXT_PID_NS
:
255 return lttng_add_pid_ns_to_ctx(ctx
);
256 case LTTNG_KERNEL_CONTEXT_USER_NS
:
257 return lttng_add_user_ns_to_ctx(ctx
);
258 case LTTNG_KERNEL_CONTEXT_UTS_NS
:
259 return lttng_add_uts_ns_to_ctx(ctx
);
260 case LTTNG_KERNEL_CONTEXT_UID
:
261 return lttng_add_uid_to_ctx(ctx
);
262 case LTTNG_KERNEL_CONTEXT_EUID
:
263 return lttng_add_euid_to_ctx(ctx
);
264 case LTTNG_KERNEL_CONTEXT_SUID
:
265 return lttng_add_suid_to_ctx(ctx
);
266 case LTTNG_KERNEL_CONTEXT_GID
:
267 return lttng_add_gid_to_ctx(ctx
);
268 case LTTNG_KERNEL_CONTEXT_EGID
:
269 return lttng_add_egid_to_ctx(ctx
);
270 case LTTNG_KERNEL_CONTEXT_SGID
:
271 return lttng_add_sgid_to_ctx(ctx
);
272 case LTTNG_KERNEL_CONTEXT_VUID
:
273 return lttng_add_vuid_to_ctx(ctx
);
274 case LTTNG_KERNEL_CONTEXT_VEUID
:
275 return lttng_add_veuid_to_ctx(ctx
);
276 case LTTNG_KERNEL_CONTEXT_VSUID
:
277 return lttng_add_vsuid_to_ctx(ctx
);
278 case LTTNG_KERNEL_CONTEXT_VGID
:
279 return lttng_add_vgid_to_ctx(ctx
);
280 case LTTNG_KERNEL_CONTEXT_VEGID
:
281 return lttng_add_vegid_to_ctx(ctx
);
282 case LTTNG_KERNEL_CONTEXT_VSGID
:
283 return lttng_add_vsgid_to_ctx(ctx
);
290 * lttng_ioctl - lttng syscall through ioctl
296 * This ioctl implements lttng commands:
297 * LTTNG_KERNEL_SESSION
298 * Returns a LTTng trace session file descriptor
299 * LTTNG_KERNEL_TRACER_VERSION
300 * Returns the LTTng kernel tracer version
301 * LTTNG_KERNEL_TRACEPOINT_LIST
302 * Returns a file descriptor listing available tracepoints
303 * LTTNG_KERNEL_WAIT_QUIESCENT
304 * Returns after all previously running probes have completed
305 * LTTNG_KERNEL_TRACER_ABI_VERSION
306 * Returns the LTTng kernel tracer ABI version
308 * The returned session will be deleted when its file descriptor is closed.
311 long lttng_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
314 case LTTNG_KERNEL_OLD_SESSION
:
315 case LTTNG_KERNEL_SESSION
:
316 return lttng_abi_create_session();
317 case LTTNG_KERNEL_OLD_TRACER_VERSION
:
319 struct lttng_kernel_tracer_version v
;
320 struct lttng_kernel_old_tracer_version oldv
;
321 struct lttng_kernel_old_tracer_version
*uversion
=
322 (struct lttng_kernel_old_tracer_version __user
*) arg
;
324 lttng_abi_tracer_version(&v
);
325 oldv
.major
= v
.major
;
326 oldv
.minor
= v
.minor
;
327 oldv
.patchlevel
= v
.patchlevel
;
329 if (copy_to_user(uversion
, &oldv
, sizeof(oldv
)))
333 case LTTNG_KERNEL_TRACER_VERSION
:
335 struct lttng_kernel_tracer_version version
;
336 struct lttng_kernel_tracer_version
*uversion
=
337 (struct lttng_kernel_tracer_version __user
*) arg
;
339 lttng_abi_tracer_version(&version
);
341 if (copy_to_user(uversion
, &version
, sizeof(version
)))
345 case LTTNG_KERNEL_TRACER_ABI_VERSION
:
347 struct lttng_kernel_tracer_abi_version version
;
348 struct lttng_kernel_tracer_abi_version
*uversion
=
349 (struct lttng_kernel_tracer_abi_version __user
*) arg
;
351 lttng_abi_tracer_abi_version(&version
);
353 if (copy_to_user(uversion
, &version
, sizeof(version
)))
357 case LTTNG_KERNEL_OLD_TRACEPOINT_LIST
:
358 case LTTNG_KERNEL_TRACEPOINT_LIST
:
359 return lttng_abi_tracepoint_list();
360 case LTTNG_KERNEL_SYSCALL_LIST
:
361 return lttng_abi_syscall_list();
362 case LTTNG_KERNEL_OLD_WAIT_QUIESCENT
:
363 case LTTNG_KERNEL_WAIT_QUIESCENT
:
366 case LTTNG_KERNEL_OLD_CALIBRATE
:
368 struct lttng_kernel_old_calibrate __user
*ucalibrate
=
369 (struct lttng_kernel_old_calibrate __user
*) arg
;
370 struct lttng_kernel_old_calibrate old_calibrate
;
371 struct lttng_kernel_calibrate calibrate
;
374 if (copy_from_user(&old_calibrate
, ucalibrate
, sizeof(old_calibrate
)))
376 calibrate
.type
= old_calibrate
.type
;
377 ret
= lttng_calibrate(&calibrate
);
378 if (copy_to_user(ucalibrate
, &old_calibrate
, sizeof(old_calibrate
)))
382 case LTTNG_KERNEL_CALIBRATE
:
384 struct lttng_kernel_calibrate __user
*ucalibrate
=
385 (struct lttng_kernel_calibrate __user
*) arg
;
386 struct lttng_kernel_calibrate calibrate
;
389 if (copy_from_user(&calibrate
, ucalibrate
, sizeof(calibrate
)))
391 ret
= lttng_calibrate(&calibrate
);
392 if (copy_to_user(ucalibrate
, &calibrate
, sizeof(calibrate
)))
401 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(5,6,0))
402 static const struct proc_ops lttng_proc_ops
= {
403 .proc_ioctl
= lttng_ioctl
,
405 .proc_compat_ioctl
= lttng_ioctl
,
406 #endif /* CONFIG_COMPAT */
409 static const struct file_operations lttng_proc_ops
= {
410 .owner
= THIS_MODULE
,
411 .unlocked_ioctl
= lttng_ioctl
,
413 .compat_ioctl
= lttng_ioctl
,
414 #endif /* CONFIG_COMPAT */
419 int lttng_abi_create_channel(struct file
*session_file
,
420 struct lttng_kernel_channel
*chan_param
,
421 enum channel_type channel_type
)
423 struct lttng_session
*session
= session_file
->private_data
;
424 const struct file_operations
*fops
= NULL
;
425 const char *transport_name
;
426 struct lttng_channel
*chan
;
427 struct file
*chan_file
;
431 chan_fd
= lttng_get_unused_fd();
436 switch (channel_type
) {
437 case PER_CPU_CHANNEL
:
438 fops
= <tng_channel_fops
;
440 case METADATA_CHANNEL
:
441 fops
= <tng_metadata_fops
;
445 chan_file
= anon_inode_getfile("[lttng_channel]",
448 if (IS_ERR(chan_file
)) {
449 ret
= PTR_ERR(chan_file
);
452 switch (channel_type
) {
453 case PER_CPU_CHANNEL
:
454 if (chan_param
->output
== LTTNG_KERNEL_SPLICE
) {
455 transport_name
= chan_param
->overwrite
?
456 "relay-overwrite" : "relay-discard";
457 } else if (chan_param
->output
== LTTNG_KERNEL_MMAP
) {
458 transport_name
= chan_param
->overwrite
?
459 "relay-overwrite-mmap" : "relay-discard-mmap";
464 case METADATA_CHANNEL
:
465 if (chan_param
->output
== LTTNG_KERNEL_SPLICE
)
466 transport_name
= "relay-metadata";
467 else if (chan_param
->output
== LTTNG_KERNEL_MMAP
)
468 transport_name
= "relay-metadata-mmap";
473 transport_name
= "<unknown>";
476 if (!atomic_long_add_unless(&session_file
->f_count
, 1, LONG_MAX
)) {
481 * We tolerate no failure path after channel creation. It will stay
482 * invariant for the rest of the session.
484 chan
= lttng_channel_create(session
, transport_name
, NULL
,
485 chan_param
->subbuf_size
,
486 chan_param
->num_subbuf
,
487 chan_param
->switch_timer_interval
,
488 chan_param
->read_timer_interval
,
494 chan
->file
= chan_file
;
495 chan_file
->private_data
= chan
;
496 fd_install(chan_fd
, chan_file
);
501 atomic_long_dec(&session_file
->f_count
);
505 put_unused_fd(chan_fd
);
511 int lttng_abi_session_set_name(struct lttng_session
*session
,
512 struct lttng_kernel_session_name
*name
)
516 len
= strnlen(name
->name
, LTTNG_KERNEL_SESSION_NAME_LEN
);
518 if (len
== LTTNG_KERNEL_SESSION_NAME_LEN
) {
519 /* Name is too long/malformed */
523 strcpy(session
->name
, name
->name
);
528 int lttng_abi_session_set_creation_time(struct lttng_session
*session
,
529 struct lttng_kernel_session_creation_time
*time
)
533 len
= strnlen(time
->iso8601
, LTTNG_KERNEL_SESSION_CREATION_TIME_ISO8601_LEN
);
535 if (len
== LTTNG_KERNEL_SESSION_CREATION_TIME_ISO8601_LEN
) {
536 /* Time is too long/malformed */
540 strcpy(session
->creation_time
, time
->iso8601
);
545 enum tracker_type
get_tracker_type(struct lttng_kernel_tracker_args
*tracker
)
547 switch (tracker
->type
) {
548 case LTTNG_KERNEL_TRACKER_PID
:
550 case LTTNG_KERNEL_TRACKER_VPID
:
552 case LTTNG_KERNEL_TRACKER_UID
:
554 case LTTNG_KERNEL_TRACKER_VUID
:
556 case LTTNG_KERNEL_TRACKER_GID
:
558 case LTTNG_KERNEL_TRACKER_VGID
:
561 return TRACKER_UNKNOWN
;
566 * lttng_session_ioctl - lttng session fd ioctl
572 * This ioctl implements lttng commands:
573 * LTTNG_KERNEL_CHANNEL
574 * Returns a LTTng channel file descriptor
575 * LTTNG_KERNEL_ENABLE
576 * Enables tracing for a session (weak enable)
577 * LTTNG_KERNEL_DISABLE
578 * Disables tracing for a session (strong disable)
579 * LTTNG_KERNEL_METADATA
580 * Returns a LTTng metadata file descriptor
581 * LTTNG_KERNEL_SESSION_TRACK_PID
582 * Add PID to session PID tracker
583 * LTTNG_KERNEL_SESSION_UNTRACK_PID
584 * Remove PID from session PID tracker
585 * LTTNG_KERNEL_SESSION_TRACK_ID
587 * LTTNG_KERNEL_SESSION_UNTRACK_ID
588 * Remove ID from tracker
590 * The returned channel will be deleted when its file descriptor is closed.
593 long lttng_session_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
595 struct lttng_session
*session
= file
->private_data
;
596 struct lttng_kernel_channel chan_param
;
597 struct lttng_kernel_old_channel old_chan_param
;
600 case LTTNG_KERNEL_OLD_CHANNEL
:
602 if (copy_from_user(&old_chan_param
,
603 (struct lttng_kernel_old_channel __user
*) arg
,
604 sizeof(struct lttng_kernel_old_channel
)))
606 chan_param
.overwrite
= old_chan_param
.overwrite
;
607 chan_param
.subbuf_size
= old_chan_param
.subbuf_size
;
608 chan_param
.num_subbuf
= old_chan_param
.num_subbuf
;
609 chan_param
.switch_timer_interval
= old_chan_param
.switch_timer_interval
;
610 chan_param
.read_timer_interval
= old_chan_param
.read_timer_interval
;
611 chan_param
.output
= old_chan_param
.output
;
613 return lttng_abi_create_channel(file
, &chan_param
,
616 case LTTNG_KERNEL_CHANNEL
:
618 if (copy_from_user(&chan_param
,
619 (struct lttng_kernel_channel __user
*) arg
,
620 sizeof(struct lttng_kernel_channel
)))
622 return lttng_abi_create_channel(file
, &chan_param
,
625 case LTTNG_KERNEL_OLD_SESSION_START
:
626 case LTTNG_KERNEL_OLD_ENABLE
:
627 case LTTNG_KERNEL_SESSION_START
:
628 case LTTNG_KERNEL_ENABLE
:
629 return lttng_session_enable(session
);
630 case LTTNG_KERNEL_OLD_SESSION_STOP
:
631 case LTTNG_KERNEL_OLD_DISABLE
:
632 case LTTNG_KERNEL_SESSION_STOP
:
633 case LTTNG_KERNEL_DISABLE
:
634 return lttng_session_disable(session
);
635 case LTTNG_KERNEL_OLD_METADATA
:
637 if (copy_from_user(&old_chan_param
,
638 (struct lttng_kernel_old_channel __user
*) arg
,
639 sizeof(struct lttng_kernel_old_channel
)))
641 chan_param
.overwrite
= old_chan_param
.overwrite
;
642 chan_param
.subbuf_size
= old_chan_param
.subbuf_size
;
643 chan_param
.num_subbuf
= old_chan_param
.num_subbuf
;
644 chan_param
.switch_timer_interval
= old_chan_param
.switch_timer_interval
;
645 chan_param
.read_timer_interval
= old_chan_param
.read_timer_interval
;
646 chan_param
.output
= old_chan_param
.output
;
648 return lttng_abi_create_channel(file
, &chan_param
,
651 case LTTNG_KERNEL_METADATA
:
653 if (copy_from_user(&chan_param
,
654 (struct lttng_kernel_channel __user
*) arg
,
655 sizeof(struct lttng_kernel_channel
)))
657 return lttng_abi_create_channel(file
, &chan_param
,
660 case LTTNG_KERNEL_SESSION_TRACK_PID
:
661 return lttng_session_track_id(session
, TRACKER_PID
, (int) arg
);
662 case LTTNG_KERNEL_SESSION_UNTRACK_PID
:
663 return lttng_session_untrack_id(session
, TRACKER_PID
, (int) arg
);
664 case LTTNG_KERNEL_SESSION_TRACK_ID
:
666 struct lttng_kernel_tracker_args tracker
;
667 enum tracker_type tracker_type
;
669 if (copy_from_user(&tracker
,
670 (struct lttng_kernel_tracker_args __user
*) arg
,
671 sizeof(struct lttng_kernel_tracker_args
)))
673 tracker_type
= get_tracker_type(&tracker
);
674 if (tracker_type
== TRACKER_UNKNOWN
)
676 return lttng_session_track_id(session
, tracker_type
, tracker
.id
);
678 case LTTNG_KERNEL_SESSION_UNTRACK_ID
:
680 struct lttng_kernel_tracker_args tracker
;
681 enum tracker_type tracker_type
;
683 if (copy_from_user(&tracker
,
684 (struct lttng_kernel_tracker_args __user
*) arg
,
685 sizeof(struct lttng_kernel_tracker_args
)))
687 tracker_type
= get_tracker_type(&tracker
);
688 if (tracker_type
== TRACKER_UNKNOWN
)
690 return lttng_session_untrack_id(session
, tracker_type
,
693 case LTTNG_KERNEL_SESSION_LIST_TRACKER_PIDS
:
694 return lttng_session_list_tracker_ids(session
, TRACKER_PID
);
695 case LTTNG_KERNEL_SESSION_LIST_TRACKER_IDS
:
697 struct lttng_kernel_tracker_args tracker
;
698 enum tracker_type tracker_type
;
700 if (copy_from_user(&tracker
,
701 (struct lttng_kernel_tracker_args __user
*) arg
,
702 sizeof(struct lttng_kernel_tracker_args
)))
704 tracker_type
= get_tracker_type(&tracker
);
705 if (tracker_type
== TRACKER_UNKNOWN
)
707 return lttng_session_list_tracker_ids(session
, tracker_type
);
709 case LTTNG_KERNEL_SESSION_METADATA_REGEN
:
710 return lttng_session_metadata_regenerate(session
);
711 case LTTNG_KERNEL_SESSION_STATEDUMP
:
712 return lttng_session_statedump(session
);
713 case LTTNG_KERNEL_SESSION_SET_NAME
:
715 struct lttng_kernel_session_name name
;
717 if (copy_from_user(&name
,
718 (struct lttng_kernel_session_name __user
*) arg
,
719 sizeof(struct lttng_kernel_session_name
)))
721 return lttng_abi_session_set_name(session
, &name
);
723 case LTTNG_KERNEL_SESSION_SET_CREATION_TIME
:
725 struct lttng_kernel_session_creation_time time
;
727 if (copy_from_user(&time
,
728 (struct lttng_kernel_session_creation_time __user
*) arg
,
729 sizeof(struct lttng_kernel_session_creation_time
)))
731 return lttng_abi_session_set_creation_time(session
, &time
);
739 * Called when the last file reference is dropped.
741 * Big fat note: channels and events are invariant for the whole session after
742 * their creation. So this session destruction also destroys all channel and
743 * event structures specific to this session (they are not destroyed when their
744 * individual file is released).
747 int lttng_session_release(struct inode
*inode
, struct file
*file
)
749 struct lttng_session
*session
= file
->private_data
;
752 lttng_session_destroy(session
);
756 static const struct file_operations lttng_session_fops
= {
757 .owner
= THIS_MODULE
,
758 .release
= lttng_session_release
,
759 .unlocked_ioctl
= lttng_session_ioctl
,
761 .compat_ioctl
= lttng_session_ioctl
,
766 * lttng_metadata_ring_buffer_poll - LTTng ring buffer poll file operation
770 * Handles the poll operations for the metadata channels.
773 unsigned int lttng_metadata_ring_buffer_poll(struct file
*filp
,
776 struct lttng_metadata_stream
*stream
= filp
->private_data
;
777 struct lib_ring_buffer
*buf
= stream
->priv
;
779 unsigned int mask
= 0;
781 if (filp
->f_mode
& FMODE_READ
) {
782 poll_wait_set_exclusive(wait
);
783 poll_wait(filp
, &stream
->read_wait
, wait
);
785 finalized
= stream
->finalized
;
788 * lib_ring_buffer_is_finalized() contains a smp_rmb()
789 * ordering finalized load before offsets loads.
791 WARN_ON(atomic_long_read(&buf
->active_readers
) != 1);
796 mutex_lock(&stream
->metadata_cache
->lock
);
797 if (stream
->metadata_cache
->metadata_written
>
798 stream
->metadata_out
)
800 mutex_unlock(&stream
->metadata_cache
->lock
);
807 void lttng_metadata_ring_buffer_ioctl_put_next_subbuf(struct file
*filp
,
808 unsigned int cmd
, unsigned long arg
)
810 struct lttng_metadata_stream
*stream
= filp
->private_data
;
812 stream
->metadata_out
= stream
->metadata_in
;
816 * Reset the counter of how much metadata has been consumed to 0. That way,
817 * the consumer receives the content of the metadata cache unchanged. This is
818 * different from the metadata_regenerate where the offset from epoch is
819 * resampled, here we want the exact same content as the last time the metadata
820 * was generated. This command is only possible if all the metadata written
821 * in the cache has been output to the metadata stream to avoid corrupting the
824 * Return 0 on success, a negative value on error.
827 int lttng_metadata_cache_dump(struct lttng_metadata_stream
*stream
)
830 struct lttng_metadata_cache
*cache
= stream
->metadata_cache
;
832 mutex_lock(&cache
->lock
);
833 if (stream
->metadata_out
!= cache
->metadata_written
) {
837 stream
->metadata_out
= 0;
838 stream
->metadata_in
= 0;
839 wake_up_interruptible(&stream
->read_wait
);
843 mutex_unlock(&cache
->lock
);
848 long lttng_metadata_ring_buffer_ioctl(struct file
*filp
,
849 unsigned int cmd
, unsigned long arg
)
852 struct lttng_metadata_stream
*stream
= filp
->private_data
;
853 struct lib_ring_buffer
*buf
= stream
->priv
;
857 if (cmd
== RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK
)
858 rb_cmd
= RING_BUFFER_GET_NEXT_SUBBUF
;
863 case RING_BUFFER_GET_NEXT_SUBBUF
:
865 struct lttng_metadata_stream
*stream
= filp
->private_data
;
866 struct lib_ring_buffer
*buf
= stream
->priv
;
867 struct channel
*chan
= buf
->backend
.chan
;
869 ret
= lttng_metadata_output_channel(stream
, chan
, NULL
);
871 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
877 case RING_BUFFER_GET_SUBBUF
:
880 * Random access is not allowed for metadata channel.
884 case RING_BUFFER_FLUSH_EMPTY
: /* Fall-through. */
885 case RING_BUFFER_FLUSH
:
887 struct lttng_metadata_stream
*stream
= filp
->private_data
;
888 struct lib_ring_buffer
*buf
= stream
->priv
;
889 struct channel
*chan
= buf
->backend
.chan
;
892 * Before doing the actual ring buffer flush, write up to one
893 * packet of metadata in the ring buffer.
895 ret
= lttng_metadata_output_channel(stream
, chan
, NULL
);
900 case RING_BUFFER_GET_METADATA_VERSION
:
902 struct lttng_metadata_stream
*stream
= filp
->private_data
;
904 return put_u64(stream
->version
, arg
);
906 case RING_BUFFER_METADATA_CACHE_DUMP
:
908 struct lttng_metadata_stream
*stream
= filp
->private_data
;
910 return lttng_metadata_cache_dump(stream
);
912 case RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK
:
914 struct lttng_metadata_stream
*stream
= filp
->private_data
;
915 struct lib_ring_buffer
*buf
= stream
->priv
;
916 struct channel
*chan
= buf
->backend
.chan
;
918 ret
= lttng_metadata_output_channel(stream
, chan
, &coherent
);
920 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
922 } else if (ret
< 0) {
930 /* PUT_SUBBUF is the one from lib ring buffer, unmodified. */
932 /* Performing lib ring buffer ioctl after our own. */
933 ret
= lib_ring_buffer_ioctl(filp
, rb_cmd
, arg
, buf
);
938 case RING_BUFFER_PUT_NEXT_SUBBUF
:
940 lttng_metadata_ring_buffer_ioctl_put_next_subbuf(filp
,
944 case RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK
:
946 return put_u32(coherent
, arg
);
957 long lttng_metadata_ring_buffer_compat_ioctl(struct file
*filp
,
958 unsigned int cmd
, unsigned long arg
)
961 struct lttng_metadata_stream
*stream
= filp
->private_data
;
962 struct lib_ring_buffer
*buf
= stream
->priv
;
966 if (cmd
== RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK
)
967 rb_cmd
= RING_BUFFER_GET_NEXT_SUBBUF
;
972 case RING_BUFFER_GET_NEXT_SUBBUF
:
974 struct lttng_metadata_stream
*stream
= filp
->private_data
;
975 struct lib_ring_buffer
*buf
= stream
->priv
;
976 struct channel
*chan
= buf
->backend
.chan
;
978 ret
= lttng_metadata_output_channel(stream
, chan
, NULL
);
980 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
986 case RING_BUFFER_GET_SUBBUF
:
989 * Random access is not allowed for metadata channel.
993 case RING_BUFFER_FLUSH_EMPTY
: /* Fall-through. */
994 case RING_BUFFER_FLUSH
:
996 struct lttng_metadata_stream
*stream
= filp
->private_data
;
997 struct lib_ring_buffer
*buf
= stream
->priv
;
998 struct channel
*chan
= buf
->backend
.chan
;
1001 * Before doing the actual ring buffer flush, write up to one
1002 * packet of metadata in the ring buffer.
1004 ret
= lttng_metadata_output_channel(stream
, chan
, NULL
);
1009 case RING_BUFFER_GET_METADATA_VERSION
:
1011 struct lttng_metadata_stream
*stream
= filp
->private_data
;
1013 return put_u64(stream
->version
, arg
);
1015 case RING_BUFFER_METADATA_CACHE_DUMP
:
1017 struct lttng_metadata_stream
*stream
= filp
->private_data
;
1019 return lttng_metadata_cache_dump(stream
);
1021 case RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK
:
1023 struct lttng_metadata_stream
*stream
= filp
->private_data
;
1024 struct lib_ring_buffer
*buf
= stream
->priv
;
1025 struct channel
*chan
= buf
->backend
.chan
;
1027 ret
= lttng_metadata_output_channel(stream
, chan
, &coherent
);
1029 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
1031 } else if (ret
< 0) {
1039 /* PUT_SUBBUF is the one from lib ring buffer, unmodified. */
1041 /* Performing lib ring buffer ioctl after our own. */
1042 ret
= lib_ring_buffer_compat_ioctl(filp
, rb_cmd
, arg
, buf
);
1047 case RING_BUFFER_PUT_NEXT_SUBBUF
:
1049 lttng_metadata_ring_buffer_ioctl_put_next_subbuf(filp
,
1053 case RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK
:
1055 return put_u32(coherent
, arg
);
1066 * This is not used by anonymous file descriptors. This code is left
1067 * there if we ever want to implement an inode with open() operation.
1070 int lttng_metadata_ring_buffer_open(struct inode
*inode
, struct file
*file
)
1072 struct lttng_metadata_stream
*stream
= inode
->i_private
;
1073 struct lib_ring_buffer
*buf
= stream
->priv
;
1075 file
->private_data
= buf
;
1077 * Since life-time of metadata cache differs from that of
1078 * session, we need to keep our own reference on the transport.
1080 if (!try_module_get(stream
->transport
->owner
)) {
1081 printk(KERN_WARNING
"LTT : Can't lock transport module.\n");
1084 return lib_ring_buffer_open(inode
, file
, buf
);
1088 int lttng_metadata_ring_buffer_release(struct inode
*inode
, struct file
*file
)
1090 struct lttng_metadata_stream
*stream
= file
->private_data
;
1091 struct lib_ring_buffer
*buf
= stream
->priv
;
1093 kref_put(&stream
->metadata_cache
->refcount
, metadata_cache_destroy
);
1094 module_put(stream
->transport
->owner
);
1095 return lib_ring_buffer_release(inode
, file
, buf
);
1099 ssize_t
lttng_metadata_ring_buffer_splice_read(struct file
*in
, loff_t
*ppos
,
1100 struct pipe_inode_info
*pipe
, size_t len
,
1103 struct lttng_metadata_stream
*stream
= in
->private_data
;
1104 struct lib_ring_buffer
*buf
= stream
->priv
;
1106 return lib_ring_buffer_splice_read(in
, ppos
, pipe
, len
,
1111 int lttng_metadata_ring_buffer_mmap(struct file
*filp
,
1112 struct vm_area_struct
*vma
)
1114 struct lttng_metadata_stream
*stream
= filp
->private_data
;
1115 struct lib_ring_buffer
*buf
= stream
->priv
;
1117 return lib_ring_buffer_mmap(filp
, vma
, buf
);
1121 const struct file_operations lttng_metadata_ring_buffer_file_operations
= {
1122 .owner
= THIS_MODULE
,
1123 .open
= lttng_metadata_ring_buffer_open
,
1124 .release
= lttng_metadata_ring_buffer_release
,
1125 .poll
= lttng_metadata_ring_buffer_poll
,
1126 .splice_read
= lttng_metadata_ring_buffer_splice_read
,
1127 .mmap
= lttng_metadata_ring_buffer_mmap
,
1128 .unlocked_ioctl
= lttng_metadata_ring_buffer_ioctl
,
1129 .llseek
= vfs_lib_ring_buffer_no_llseek
,
1130 #ifdef CONFIG_COMPAT
1131 .compat_ioctl
= lttng_metadata_ring_buffer_compat_ioctl
,
1136 int lttng_abi_create_stream_fd(struct file
*channel_file
, void *stream_priv
,
1137 const struct file_operations
*fops
)
1140 struct file
*stream_file
;
1142 stream_fd
= lttng_get_unused_fd();
1143 if (stream_fd
< 0) {
1147 stream_file
= anon_inode_getfile("[lttng_stream]", fops
,
1148 stream_priv
, O_RDWR
);
1149 if (IS_ERR(stream_file
)) {
1150 ret
= PTR_ERR(stream_file
);
1154 * OPEN_FMODE, called within anon_inode_getfile/alloc_file, don't honor
1155 * FMODE_LSEEK, FMODE_PREAD nor FMODE_PWRITE. We need to read from this
1156 * file descriptor, so we set FMODE_PREAD here.
1158 stream_file
->f_mode
|= FMODE_PREAD
;
1159 fd_install(stream_fd
, stream_file
);
1161 * The stream holds a reference to the channel within the generic ring
1162 * buffer library, so no need to hold a refcount on the channel and
1163 * session files here.
1168 put_unused_fd(stream_fd
);
1174 int lttng_abi_open_stream(struct file
*channel_file
)
1176 struct lttng_channel
*channel
= channel_file
->private_data
;
1177 struct lib_ring_buffer
*buf
;
1181 buf
= channel
->ops
->buffer_read_open(channel
->chan
);
1186 ret
= lttng_abi_create_stream_fd(channel_file
, stream_priv
,
1187 <tng_stream_ring_buffer_file_operations
);
1194 channel
->ops
->buffer_read_close(buf
);
1199 int lttng_abi_open_metadata_stream(struct file
*channel_file
)
1201 struct lttng_channel
*channel
= channel_file
->private_data
;
1202 struct lttng_session
*session
= channel
->session
;
1203 struct lib_ring_buffer
*buf
;
1205 struct lttng_metadata_stream
*metadata_stream
;
1208 buf
= channel
->ops
->buffer_read_open(channel
->chan
);
1212 metadata_stream
= kzalloc(sizeof(struct lttng_metadata_stream
),
1214 if (!metadata_stream
) {
1218 metadata_stream
->metadata_cache
= session
->metadata_cache
;
1219 init_waitqueue_head(&metadata_stream
->read_wait
);
1220 metadata_stream
->priv
= buf
;
1221 stream_priv
= metadata_stream
;
1222 metadata_stream
->transport
= channel
->transport
;
1223 /* Initial state is an empty metadata, considered as incoherent. */
1224 metadata_stream
->coherent
= false;
1227 * Since life-time of metadata cache differs from that of
1228 * session, we need to keep our own reference on the transport.
1230 if (!try_module_get(metadata_stream
->transport
->owner
)) {
1231 printk(KERN_WARNING
"LTT : Can't lock transport module.\n");
1236 if (!lttng_kref_get(&session
->metadata_cache
->refcount
)) {
1241 ret
= lttng_abi_create_stream_fd(channel_file
, stream_priv
,
1242 <tng_metadata_ring_buffer_file_operations
);
1246 list_add(&metadata_stream
->list
,
1247 &session
->metadata_cache
->metadata_stream
);
1251 kref_put(&session
->metadata_cache
->refcount
, metadata_cache_destroy
);
1253 module_put(metadata_stream
->transport
->owner
);
1255 kfree(metadata_stream
);
1257 channel
->ops
->buffer_read_close(buf
);
1262 int lttng_abi_create_event(struct file
*channel_file
,
1263 struct lttng_kernel_event
*event_param
)
1265 struct lttng_channel
*channel
= channel_file
->private_data
;
1267 struct file
*event_file
;
1270 event_param
->name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
1271 switch (event_param
->instrumentation
) {
1272 case LTTNG_KERNEL_KRETPROBE
:
1273 event_param
->u
.kretprobe
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
1275 case LTTNG_KERNEL_KPROBE
:
1276 event_param
->u
.kprobe
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
1278 case LTTNG_KERNEL_FUNCTION
:
1280 /* Not implemented. */
1285 event_fd
= lttng_get_unused_fd();
1290 event_file
= anon_inode_getfile("[lttng_event]",
1293 if (IS_ERR(event_file
)) {
1294 ret
= PTR_ERR(event_file
);
1297 /* The event holds a reference on the channel */
1298 if (!atomic_long_add_unless(&channel_file
->f_count
, 1, LONG_MAX
)) {
1300 goto refcount_error
;
1302 if (event_param
->instrumentation
== LTTNG_KERNEL_TRACEPOINT
1303 || event_param
->instrumentation
== LTTNG_KERNEL_SYSCALL
) {
1304 struct lttng_enabler
*enabler
;
1306 if (strutils_is_star_glob_pattern(event_param
->name
)) {
1308 * If the event name is a star globbing pattern,
1309 * we create the special star globbing enabler.
1311 enabler
= lttng_enabler_create(LTTNG_ENABLER_STAR_GLOB
,
1312 event_param
, channel
);
1314 enabler
= lttng_enabler_create(LTTNG_ENABLER_NAME
,
1315 event_param
, channel
);
1319 struct lttng_event
*event
;
1322 * We tolerate no failure path after event creation. It
1323 * will stay invariant for the rest of the session.
1325 event
= lttng_event_create(channel
, event_param
,
1327 event_param
->instrumentation
);
1328 WARN_ON_ONCE(!event
);
1329 if (IS_ERR(event
)) {
1330 ret
= PTR_ERR(event
);
1335 event_file
->private_data
= priv
;
1336 fd_install(event_fd
, event_file
);
1340 atomic_long_dec(&channel_file
->f_count
);
1344 put_unused_fd(event_fd
);
1350 * lttng_channel_ioctl - lttng syscall through ioctl
1356 * This ioctl implements lttng commands:
1357 * LTTNG_KERNEL_STREAM
1358 * Returns an event stream file descriptor or failure.
1359 * (typically, one event stream records events from one CPU)
1360 * LTTNG_KERNEL_EVENT
1361 * Returns an event file descriptor or failure.
1362 * LTTNG_KERNEL_CONTEXT
1363 * Prepend a context field to each event in the channel
1364 * LTTNG_KERNEL_ENABLE
1365 * Enable recording for events in this channel (weak enable)
1366 * LTTNG_KERNEL_DISABLE
1367 * Disable recording for events in this channel (strong disable)
1369 * Channel and event file descriptors also hold a reference on the session.
1372 long lttng_channel_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1374 struct lttng_channel
*channel
= file
->private_data
;
1377 case LTTNG_KERNEL_OLD_STREAM
:
1378 case LTTNG_KERNEL_STREAM
:
1379 return lttng_abi_open_stream(file
);
1380 case LTTNG_KERNEL_OLD_EVENT
:
1382 struct lttng_kernel_event
*uevent_param
;
1383 struct lttng_kernel_old_event
*old_uevent_param
;
1386 uevent_param
= kmalloc(sizeof(struct lttng_kernel_event
),
1388 if (!uevent_param
) {
1392 old_uevent_param
= kmalloc(
1393 sizeof(struct lttng_kernel_old_event
),
1395 if (!old_uevent_param
) {
1397 goto old_event_error_free_param
;
1399 if (copy_from_user(old_uevent_param
,
1400 (struct lttng_kernel_old_event __user
*) arg
,
1401 sizeof(struct lttng_kernel_old_event
))) {
1403 goto old_event_error_free_old_param
;
1406 memcpy(uevent_param
->name
, old_uevent_param
->name
,
1407 sizeof(uevent_param
->name
));
1408 uevent_param
->instrumentation
=
1409 old_uevent_param
->instrumentation
;
1411 switch (old_uevent_param
->instrumentation
) {
1412 case LTTNG_KERNEL_KPROBE
:
1413 uevent_param
->u
.kprobe
.addr
=
1414 old_uevent_param
->u
.kprobe
.addr
;
1415 uevent_param
->u
.kprobe
.offset
=
1416 old_uevent_param
->u
.kprobe
.offset
;
1417 memcpy(uevent_param
->u
.kprobe
.symbol_name
,
1418 old_uevent_param
->u
.kprobe
.symbol_name
,
1419 sizeof(uevent_param
->u
.kprobe
.symbol_name
));
1421 case LTTNG_KERNEL_KRETPROBE
:
1422 uevent_param
->u
.kretprobe
.addr
=
1423 old_uevent_param
->u
.kretprobe
.addr
;
1424 uevent_param
->u
.kretprobe
.offset
=
1425 old_uevent_param
->u
.kretprobe
.offset
;
1426 memcpy(uevent_param
->u
.kretprobe
.symbol_name
,
1427 old_uevent_param
->u
.kretprobe
.symbol_name
,
1428 sizeof(uevent_param
->u
.kretprobe
.symbol_name
));
1430 case LTTNG_KERNEL_FUNCTION
:
1432 /* Not implemented. */
1437 ret
= lttng_abi_create_event(file
, uevent_param
);
1439 old_event_error_free_old_param
:
1440 kfree(old_uevent_param
);
1441 old_event_error_free_param
:
1442 kfree(uevent_param
);
1446 case LTTNG_KERNEL_EVENT
:
1448 struct lttng_kernel_event uevent_param
;
1450 if (copy_from_user(&uevent_param
,
1451 (struct lttng_kernel_event __user
*) arg
,
1452 sizeof(uevent_param
)))
1454 return lttng_abi_create_event(file
, &uevent_param
);
1456 case LTTNG_KERNEL_OLD_CONTEXT
:
1458 struct lttng_kernel_context
*ucontext_param
;
1459 struct lttng_kernel_old_context
*old_ucontext_param
;
1462 ucontext_param
= kmalloc(sizeof(struct lttng_kernel_context
),
1464 if (!ucontext_param
) {
1468 old_ucontext_param
= kmalloc(sizeof(struct lttng_kernel_old_context
),
1470 if (!old_ucontext_param
) {
1472 goto old_ctx_error_free_param
;
1475 if (copy_from_user(old_ucontext_param
,
1476 (struct lttng_kernel_old_context __user
*) arg
,
1477 sizeof(struct lttng_kernel_old_context
))) {
1479 goto old_ctx_error_free_old_param
;
1481 ucontext_param
->ctx
= old_ucontext_param
->ctx
;
1482 memcpy(ucontext_param
->padding
, old_ucontext_param
->padding
,
1483 sizeof(ucontext_param
->padding
));
1484 /* only type that uses the union */
1485 if (old_ucontext_param
->ctx
== LTTNG_KERNEL_CONTEXT_PERF_COUNTER
) {
1486 ucontext_param
->u
.perf_counter
.type
=
1487 old_ucontext_param
->u
.perf_counter
.type
;
1488 ucontext_param
->u
.perf_counter
.config
=
1489 old_ucontext_param
->u
.perf_counter
.config
;
1490 memcpy(ucontext_param
->u
.perf_counter
.name
,
1491 old_ucontext_param
->u
.perf_counter
.name
,
1492 sizeof(ucontext_param
->u
.perf_counter
.name
));
1495 ret
= lttng_abi_add_context(file
,
1497 &channel
->ctx
, channel
->session
);
1499 old_ctx_error_free_old_param
:
1500 kfree(old_ucontext_param
);
1501 old_ctx_error_free_param
:
1502 kfree(ucontext_param
);
1506 case LTTNG_KERNEL_CONTEXT
:
1508 struct lttng_kernel_context ucontext_param
;
1510 if (copy_from_user(&ucontext_param
,
1511 (struct lttng_kernel_context __user
*) arg
,
1512 sizeof(ucontext_param
)))
1514 return lttng_abi_add_context(file
,
1516 &channel
->ctx
, channel
->session
);
1518 case LTTNG_KERNEL_OLD_ENABLE
:
1519 case LTTNG_KERNEL_ENABLE
:
1520 return lttng_channel_enable(channel
);
1521 case LTTNG_KERNEL_OLD_DISABLE
:
1522 case LTTNG_KERNEL_DISABLE
:
1523 return lttng_channel_disable(channel
);
1524 case LTTNG_KERNEL_SYSCALL_MASK
:
1525 return lttng_channel_syscall_mask(channel
,
1526 (struct lttng_kernel_syscall_mask __user
*) arg
);
1528 return -ENOIOCTLCMD
;
1533 * lttng_metadata_ioctl - lttng syscall through ioctl
1539 * This ioctl implements lttng commands:
1540 * LTTNG_KERNEL_STREAM
1541 * Returns an event stream file descriptor or failure.
1543 * Channel and event file descriptors also hold a reference on the session.
1546 long lttng_metadata_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1549 case LTTNG_KERNEL_OLD_STREAM
:
1550 case LTTNG_KERNEL_STREAM
:
1551 return lttng_abi_open_metadata_stream(file
);
1553 return -ENOIOCTLCMD
;
1558 * lttng_channel_poll - lttng stream addition/removal monitoring
1563 unsigned int lttng_channel_poll(struct file
*file
, poll_table
*wait
)
1565 struct lttng_channel
*channel
= file
->private_data
;
1566 unsigned int mask
= 0;
1568 if (file
->f_mode
& FMODE_READ
) {
1569 poll_wait_set_exclusive(wait
);
1570 poll_wait(file
, channel
->ops
->get_hp_wait_queue(channel
->chan
),
1573 if (channel
->ops
->is_disabled(channel
->chan
))
1575 if (channel
->ops
->is_finalized(channel
->chan
))
1577 if (channel
->ops
->buffer_has_read_closed_stream(channel
->chan
))
1578 return POLLIN
| POLLRDNORM
;
1586 int lttng_channel_release(struct inode
*inode
, struct file
*file
)
1588 struct lttng_channel
*channel
= file
->private_data
;
1591 fput(channel
->session
->file
);
1596 int lttng_metadata_channel_release(struct inode
*inode
, struct file
*file
)
1598 struct lttng_channel
*channel
= file
->private_data
;
1601 fput(channel
->session
->file
);
1602 lttng_metadata_channel_destroy(channel
);
1608 static const struct file_operations lttng_channel_fops
= {
1609 .owner
= THIS_MODULE
,
1610 .release
= lttng_channel_release
,
1611 .poll
= lttng_channel_poll
,
1612 .unlocked_ioctl
= lttng_channel_ioctl
,
1613 #ifdef CONFIG_COMPAT
1614 .compat_ioctl
= lttng_channel_ioctl
,
1618 static const struct file_operations lttng_metadata_fops
= {
1619 .owner
= THIS_MODULE
,
1620 .release
= lttng_metadata_channel_release
,
1621 .unlocked_ioctl
= lttng_metadata_ioctl
,
1622 #ifdef CONFIG_COMPAT
1623 .compat_ioctl
= lttng_metadata_ioctl
,
1628 * lttng_event_ioctl - lttng syscall through ioctl
1634 * This ioctl implements lttng commands:
1635 * LTTNG_KERNEL_CONTEXT
1636 * Prepend a context field to each record of this event
1637 * LTTNG_KERNEL_ENABLE
1638 * Enable recording for this event (weak enable)
1639 * LTTNG_KERNEL_DISABLE
1640 * Disable recording for this event (strong disable)
1643 long lttng_event_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1645 struct lttng_event
*event
;
1646 struct lttng_enabler
*enabler
;
1647 enum lttng_event_type
*evtype
= file
->private_data
;
1650 case LTTNG_KERNEL_OLD_CONTEXT
:
1652 /* Not implemented */
1655 case LTTNG_KERNEL_CONTEXT
:
1657 /* Not implemented */
1660 case LTTNG_KERNEL_OLD_ENABLE
:
1661 case LTTNG_KERNEL_ENABLE
:
1663 case LTTNG_TYPE_EVENT
:
1664 event
= file
->private_data
;
1665 return lttng_event_enable(event
);
1666 case LTTNG_TYPE_ENABLER
:
1667 enabler
= file
->private_data
;
1668 return lttng_enabler_enable(enabler
);
1673 case LTTNG_KERNEL_OLD_DISABLE
:
1674 case LTTNG_KERNEL_DISABLE
:
1676 case LTTNG_TYPE_EVENT
:
1677 event
= file
->private_data
;
1678 return lttng_event_disable(event
);
1679 case LTTNG_TYPE_ENABLER
:
1680 enabler
= file
->private_data
;
1681 return lttng_enabler_disable(enabler
);
1686 case LTTNG_KERNEL_FILTER
:
1688 case LTTNG_TYPE_EVENT
:
1690 case LTTNG_TYPE_ENABLER
:
1692 enabler
= file
->private_data
;
1693 return lttng_enabler_attach_bytecode(enabler
,
1694 (struct lttng_kernel_filter_bytecode __user
*) arg
);
1700 case LTTNG_KERNEL_ADD_CALLSITE
:
1702 case LTTNG_TYPE_EVENT
:
1703 event
= file
->private_data
;
1704 return lttng_event_add_callsite(event
,
1705 (struct lttng_kernel_event_callsite __user
*) arg
);
1706 case LTTNG_TYPE_ENABLER
:
1713 return -ENOIOCTLCMD
;
1718 int lttng_event_release(struct inode
*inode
, struct file
*file
)
1720 struct lttng_event
*event
;
1721 struct lttng_enabler
*enabler
;
1722 enum lttng_event_type
*evtype
= file
->private_data
;
1728 case LTTNG_TYPE_EVENT
:
1729 event
= file
->private_data
;
1731 fput(event
->chan
->file
);
1733 case LTTNG_TYPE_ENABLER
:
1734 enabler
= file
->private_data
;
1736 fput(enabler
->chan
->file
);
1746 /* TODO: filter control ioctl */
1747 static const struct file_operations lttng_event_fops
= {
1748 .owner
= THIS_MODULE
,
1749 .release
= lttng_event_release
,
1750 .unlocked_ioctl
= lttng_event_ioctl
,
1751 #ifdef CONFIG_COMPAT
1752 .compat_ioctl
= lttng_event_ioctl
,
1756 static int put_u64(uint64_t val
, unsigned long arg
)
1758 return put_user(val
, (uint64_t __user
*) arg
);
1761 static int put_u32(uint32_t val
, unsigned long arg
)
1763 return put_user(val
, (uint32_t __user
*) arg
);
1766 static long lttng_stream_ring_buffer_ioctl(struct file
*filp
,
1767 unsigned int cmd
, unsigned long arg
)
1769 struct lib_ring_buffer
*buf
= filp
->private_data
;
1770 struct channel
*chan
= buf
->backend
.chan
;
1771 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1772 const struct lttng_channel_ops
*ops
= chan
->backend
.priv_ops
;
1775 if (atomic_read(&chan
->record_disabled
))
1779 case LTTNG_RING_BUFFER_GET_TIMESTAMP_BEGIN
:
1783 ret
= ops
->timestamp_begin(config
, buf
, &ts
);
1786 return put_u64(ts
, arg
);
1788 case LTTNG_RING_BUFFER_GET_TIMESTAMP_END
:
1792 ret
= ops
->timestamp_end(config
, buf
, &ts
);
1795 return put_u64(ts
, arg
);
1797 case LTTNG_RING_BUFFER_GET_EVENTS_DISCARDED
:
1801 ret
= ops
->events_discarded(config
, buf
, &ed
);
1804 return put_u64(ed
, arg
);
1806 case LTTNG_RING_BUFFER_GET_CONTENT_SIZE
:
1810 ret
= ops
->content_size(config
, buf
, &cs
);
1813 return put_u64(cs
, arg
);
1815 case LTTNG_RING_BUFFER_GET_PACKET_SIZE
:
1819 ret
= ops
->packet_size(config
, buf
, &ps
);
1822 return put_u64(ps
, arg
);
1824 case LTTNG_RING_BUFFER_GET_STREAM_ID
:
1828 ret
= ops
->stream_id(config
, buf
, &si
);
1831 return put_u64(si
, arg
);
1833 case LTTNG_RING_BUFFER_GET_CURRENT_TIMESTAMP
:
1837 ret
= ops
->current_timestamp(config
, buf
, &ts
);
1840 return put_u64(ts
, arg
);
1842 case LTTNG_RING_BUFFER_GET_SEQ_NUM
:
1846 ret
= ops
->sequence_number(config
, buf
, &seq
);
1849 return put_u64(seq
, arg
);
1851 case LTTNG_RING_BUFFER_INSTANCE_ID
:
1855 ret
= ops
->instance_id(config
, buf
, &id
);
1858 return put_u64(id
, arg
);
1861 return lib_ring_buffer_file_operations
.unlocked_ioctl(filp
,
1869 #ifdef CONFIG_COMPAT
1870 static long lttng_stream_ring_buffer_compat_ioctl(struct file
*filp
,
1871 unsigned int cmd
, unsigned long arg
)
1873 struct lib_ring_buffer
*buf
= filp
->private_data
;
1874 struct channel
*chan
= buf
->backend
.chan
;
1875 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1876 const struct lttng_channel_ops
*ops
= chan
->backend
.priv_ops
;
1879 if (atomic_read(&chan
->record_disabled
))
1883 case LTTNG_RING_BUFFER_COMPAT_GET_TIMESTAMP_BEGIN
:
1887 ret
= ops
->timestamp_begin(config
, buf
, &ts
);
1890 return put_u64(ts
, arg
);
1892 case LTTNG_RING_BUFFER_COMPAT_GET_TIMESTAMP_END
:
1896 ret
= ops
->timestamp_end(config
, buf
, &ts
);
1899 return put_u64(ts
, arg
);
1901 case LTTNG_RING_BUFFER_COMPAT_GET_EVENTS_DISCARDED
:
1905 ret
= ops
->events_discarded(config
, buf
, &ed
);
1908 return put_u64(ed
, arg
);
1910 case LTTNG_RING_BUFFER_COMPAT_GET_CONTENT_SIZE
:
1914 ret
= ops
->content_size(config
, buf
, &cs
);
1917 return put_u64(cs
, arg
);
1919 case LTTNG_RING_BUFFER_COMPAT_GET_PACKET_SIZE
:
1923 ret
= ops
->packet_size(config
, buf
, &ps
);
1926 return put_u64(ps
, arg
);
1928 case LTTNG_RING_BUFFER_COMPAT_GET_STREAM_ID
:
1932 ret
= ops
->stream_id(config
, buf
, &si
);
1935 return put_u64(si
, arg
);
1937 case LTTNG_RING_BUFFER_GET_CURRENT_TIMESTAMP
:
1941 ret
= ops
->current_timestamp(config
, buf
, &ts
);
1944 return put_u64(ts
, arg
);
1946 case LTTNG_RING_BUFFER_COMPAT_GET_SEQ_NUM
:
1950 ret
= ops
->sequence_number(config
, buf
, &seq
);
1953 return put_u64(seq
, arg
);
1955 case LTTNG_RING_BUFFER_COMPAT_INSTANCE_ID
:
1959 ret
= ops
->instance_id(config
, buf
, &id
);
1962 return put_u64(id
, arg
);
1965 return lib_ring_buffer_file_operations
.compat_ioctl(filp
,
1972 #endif /* CONFIG_COMPAT */
1974 static void lttng_stream_override_ring_buffer_fops(void)
1976 lttng_stream_ring_buffer_file_operations
.owner
= THIS_MODULE
;
1977 lttng_stream_ring_buffer_file_operations
.open
=
1978 lib_ring_buffer_file_operations
.open
;
1979 lttng_stream_ring_buffer_file_operations
.release
=
1980 lib_ring_buffer_file_operations
.release
;
1981 lttng_stream_ring_buffer_file_operations
.poll
=
1982 lib_ring_buffer_file_operations
.poll
;
1983 lttng_stream_ring_buffer_file_operations
.splice_read
=
1984 lib_ring_buffer_file_operations
.splice_read
;
1985 lttng_stream_ring_buffer_file_operations
.mmap
=
1986 lib_ring_buffer_file_operations
.mmap
;
1987 lttng_stream_ring_buffer_file_operations
.unlocked_ioctl
=
1988 lttng_stream_ring_buffer_ioctl
;
1989 lttng_stream_ring_buffer_file_operations
.llseek
=
1990 lib_ring_buffer_file_operations
.llseek
;
1991 #ifdef CONFIG_COMPAT
1992 lttng_stream_ring_buffer_file_operations
.compat_ioctl
=
1993 lttng_stream_ring_buffer_compat_ioctl
;
1997 int __init
lttng_abi_init(void)
2001 wrapper_vmalloc_sync_mappings();
2004 ret
= lttng_tp_mempool_init();
2009 lttng_proc_dentry
= proc_create_data("lttng", S_IRUSR
| S_IWUSR
, NULL
,
2010 <tng_proc_ops
, NULL
);
2012 if (!lttng_proc_dentry
) {
2013 printk(KERN_ERR
"Error creating LTTng control file\n");
2017 lttng_stream_override_ring_buffer_fops();
2021 lttng_tp_mempool_destroy();
2022 lttng_clock_unref();
2026 /* No __exit annotation because used by init error path too. */
2027 void lttng_abi_exit(void)
2029 lttng_tp_mempool_destroy();
2030 lttng_clock_unref();
2031 if (lttng_proc_dentry
)
2032 remove_proc_entry("lttng", NULL
);