1 /* SPDX-License-Identifier: (GPL-2.0-only or LGPL-2.1-only)
7 * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
9 * Mimic system calls for:
10 * - session creation, returns a file descriptor or failure.
11 * - channel creation, returns a file descriptor or failure.
12 * - Operates on a session file descriptor
13 * - Takes all channel options as parameters.
14 * - stream get, returns a file descriptor or failure.
15 * - Operates on a channel file descriptor.
16 * - stream notifier get, returns a file descriptor or failure.
17 * - Operates on a channel file descriptor.
18 * - event creation, returns a file descriptor or failure.
19 * - Operates on a channel file descriptor
20 * - Takes an event name as parameter
21 * - Takes an instrumentation source as parameter
22 * - e.g. tracepoints, dynamic_probes...
23 * - Takes instrumentation source specific arguments.
26 #include <linux/module.h>
27 #include <linux/proc_fs.h>
28 #include <linux/anon_inodes.h>
29 #include <linux/file.h>
30 #include <linux/uaccess.h>
31 #include <linux/slab.h>
32 #include <linux/err.h>
33 #include <wrapper/vmalloc.h> /* for wrapper_vmalloc_sync_mappings() */
34 #include <ringbuffer/vfs.h>
35 #include <ringbuffer/backend.h>
36 #include <ringbuffer/frontend.h>
37 #include <wrapper/poll.h>
38 #include <wrapper/file.h>
39 #include <wrapper/kref.h>
40 #include <lttng/string-utils.h>
41 #include <lttng/abi.h>
42 #include <lttng/abi-old.h>
43 #include <lttng/events.h>
44 #include <lttng/tracer.h>
45 #include <lttng/tp-mempool.h>
46 #include <ringbuffer/frontend_types.h>
49 * This is LTTng's own personal way to create a system call as an external
50 * module. We use ioctl() on /proc/lttng.
53 static struct proc_dir_entry
*lttng_proc_dentry
;
55 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(5,6,0))
56 static const struct proc_ops lttng_proc_ops
;
58 static const struct file_operations lttng_proc_ops
;
61 static const struct file_operations lttng_session_fops
;
62 static const struct file_operations lttng_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
);
284 case LTTNG_KERNEL_CONTEXT_TIME_NS
:
285 return lttng_add_time_ns_to_ctx(ctx
);
292 * lttng_ioctl - lttng syscall through ioctl
298 * This ioctl implements lttng commands:
299 * LTTNG_KERNEL_SESSION
300 * Returns a LTTng trace session file descriptor
301 * LTTNG_KERNEL_TRACER_VERSION
302 * Returns the LTTng kernel tracer version
303 * LTTNG_KERNEL_TRACEPOINT_LIST
304 * Returns a file descriptor listing available tracepoints
305 * LTTNG_KERNEL_WAIT_QUIESCENT
306 * Returns after all previously running probes have completed
307 * LTTNG_KERNEL_TRACER_ABI_VERSION
308 * Returns the LTTng kernel tracer ABI version
310 * The returned session will be deleted when its file descriptor is closed.
313 long lttng_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
316 case LTTNG_KERNEL_OLD_SESSION
:
317 case LTTNG_KERNEL_SESSION
:
318 return lttng_abi_create_session();
319 case LTTNG_KERNEL_OLD_TRACER_VERSION
:
321 struct lttng_kernel_tracer_version v
;
322 struct lttng_kernel_old_tracer_version oldv
;
323 struct lttng_kernel_old_tracer_version
*uversion
=
324 (struct lttng_kernel_old_tracer_version __user
*) arg
;
326 lttng_abi_tracer_version(&v
);
327 oldv
.major
= v
.major
;
328 oldv
.minor
= v
.minor
;
329 oldv
.patchlevel
= v
.patchlevel
;
331 if (copy_to_user(uversion
, &oldv
, sizeof(oldv
)))
335 case LTTNG_KERNEL_TRACER_VERSION
:
337 struct lttng_kernel_tracer_version version
;
338 struct lttng_kernel_tracer_version
*uversion
=
339 (struct lttng_kernel_tracer_version __user
*) arg
;
341 lttng_abi_tracer_version(&version
);
343 if (copy_to_user(uversion
, &version
, sizeof(version
)))
347 case LTTNG_KERNEL_TRACER_ABI_VERSION
:
349 struct lttng_kernel_tracer_abi_version version
;
350 struct lttng_kernel_tracer_abi_version
*uversion
=
351 (struct lttng_kernel_tracer_abi_version __user
*) arg
;
353 lttng_abi_tracer_abi_version(&version
);
355 if (copy_to_user(uversion
, &version
, sizeof(version
)))
359 case LTTNG_KERNEL_OLD_TRACEPOINT_LIST
:
360 case LTTNG_KERNEL_TRACEPOINT_LIST
:
361 return lttng_abi_tracepoint_list();
362 case LTTNG_KERNEL_SYSCALL_LIST
:
363 return lttng_abi_syscall_list();
364 case LTTNG_KERNEL_OLD_WAIT_QUIESCENT
:
365 case LTTNG_KERNEL_WAIT_QUIESCENT
:
368 case LTTNG_KERNEL_OLD_CALIBRATE
:
370 struct lttng_kernel_old_calibrate __user
*ucalibrate
=
371 (struct lttng_kernel_old_calibrate __user
*) arg
;
372 struct lttng_kernel_old_calibrate old_calibrate
;
373 struct lttng_kernel_calibrate calibrate
;
376 if (copy_from_user(&old_calibrate
, ucalibrate
, sizeof(old_calibrate
)))
378 calibrate
.type
= old_calibrate
.type
;
379 ret
= lttng_calibrate(&calibrate
);
380 if (copy_to_user(ucalibrate
, &old_calibrate
, sizeof(old_calibrate
)))
384 case LTTNG_KERNEL_CALIBRATE
:
386 struct lttng_kernel_calibrate __user
*ucalibrate
=
387 (struct lttng_kernel_calibrate __user
*) arg
;
388 struct lttng_kernel_calibrate calibrate
;
391 if (copy_from_user(&calibrate
, ucalibrate
, sizeof(calibrate
)))
393 ret
= lttng_calibrate(&calibrate
);
394 if (copy_to_user(ucalibrate
, &calibrate
, sizeof(calibrate
)))
403 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(5,6,0))
404 static const struct proc_ops lttng_proc_ops
= {
405 .proc_ioctl
= lttng_ioctl
,
407 .proc_compat_ioctl
= lttng_ioctl
,
408 #endif /* CONFIG_COMPAT */
411 static const struct file_operations lttng_proc_ops
= {
412 .owner
= THIS_MODULE
,
413 .unlocked_ioctl
= lttng_ioctl
,
415 .compat_ioctl
= lttng_ioctl
,
416 #endif /* CONFIG_COMPAT */
421 int lttng_abi_create_channel(struct file
*session_file
,
422 struct lttng_kernel_channel
*chan_param
,
423 enum channel_type channel_type
)
425 struct lttng_session
*session
= session_file
->private_data
;
426 const struct file_operations
*fops
= NULL
;
427 const char *transport_name
;
428 struct lttng_channel
*chan
;
429 struct file
*chan_file
;
433 chan_fd
= lttng_get_unused_fd();
438 switch (channel_type
) {
439 case PER_CPU_CHANNEL
:
440 fops
= <tng_channel_fops
;
442 case METADATA_CHANNEL
:
443 fops
= <tng_metadata_fops
;
447 chan_file
= anon_inode_getfile("[lttng_channel]",
450 if (IS_ERR(chan_file
)) {
451 ret
= PTR_ERR(chan_file
);
454 switch (channel_type
) {
455 case PER_CPU_CHANNEL
:
456 if (chan_param
->output
== LTTNG_KERNEL_SPLICE
) {
457 transport_name
= chan_param
->overwrite
?
458 "relay-overwrite" : "relay-discard";
459 } else if (chan_param
->output
== LTTNG_KERNEL_MMAP
) {
460 transport_name
= chan_param
->overwrite
?
461 "relay-overwrite-mmap" : "relay-discard-mmap";
466 case METADATA_CHANNEL
:
467 if (chan_param
->output
== LTTNG_KERNEL_SPLICE
)
468 transport_name
= "relay-metadata";
469 else if (chan_param
->output
== LTTNG_KERNEL_MMAP
)
470 transport_name
= "relay-metadata-mmap";
475 transport_name
= "<unknown>";
478 if (!atomic_long_add_unless(&session_file
->f_count
, 1, LONG_MAX
)) {
483 * We tolerate no failure path after channel creation. It will stay
484 * invariant for the rest of the session.
486 chan
= lttng_channel_create(session
, transport_name
, NULL
,
487 chan_param
->subbuf_size
,
488 chan_param
->num_subbuf
,
489 chan_param
->switch_timer_interval
,
490 chan_param
->read_timer_interval
,
496 chan
->file
= chan_file
;
497 chan_file
->private_data
= chan
;
498 fd_install(chan_fd
, chan_file
);
503 atomic_long_dec(&session_file
->f_count
);
507 put_unused_fd(chan_fd
);
513 int lttng_abi_session_set_name(struct lttng_session
*session
,
514 struct lttng_kernel_session_name
*name
)
518 len
= strnlen(name
->name
, LTTNG_KERNEL_SESSION_NAME_LEN
);
520 if (len
== LTTNG_KERNEL_SESSION_NAME_LEN
) {
521 /* Name is too long/malformed */
525 strcpy(session
->name
, name
->name
);
530 int lttng_abi_session_set_creation_time(struct lttng_session
*session
,
531 struct lttng_kernel_session_creation_time
*time
)
535 len
= strnlen(time
->iso8601
, LTTNG_KERNEL_SESSION_CREATION_TIME_ISO8601_LEN
);
537 if (len
== LTTNG_KERNEL_SESSION_CREATION_TIME_ISO8601_LEN
) {
538 /* Time is too long/malformed */
542 strcpy(session
->creation_time
, time
->iso8601
);
547 enum tracker_type
get_tracker_type(struct lttng_kernel_tracker_args
*tracker
)
549 switch (tracker
->type
) {
550 case LTTNG_KERNEL_TRACKER_PID
:
552 case LTTNG_KERNEL_TRACKER_VPID
:
554 case LTTNG_KERNEL_TRACKER_UID
:
556 case LTTNG_KERNEL_TRACKER_VUID
:
558 case LTTNG_KERNEL_TRACKER_GID
:
560 case LTTNG_KERNEL_TRACKER_VGID
:
563 return TRACKER_UNKNOWN
;
568 * lttng_session_ioctl - lttng session fd ioctl
574 * This ioctl implements lttng commands:
575 * LTTNG_KERNEL_CHANNEL
576 * Returns a LTTng channel file descriptor
577 * LTTNG_KERNEL_ENABLE
578 * Enables tracing for a session (weak enable)
579 * LTTNG_KERNEL_DISABLE
580 * Disables tracing for a session (strong disable)
581 * LTTNG_KERNEL_METADATA
582 * Returns a LTTng metadata file descriptor
583 * LTTNG_KERNEL_SESSION_TRACK_PID
584 * Add PID to session PID tracker
585 * LTTNG_KERNEL_SESSION_UNTRACK_PID
586 * Remove PID from session PID tracker
587 * LTTNG_KERNEL_SESSION_TRACK_ID
589 * LTTNG_KERNEL_SESSION_UNTRACK_ID
590 * Remove ID from tracker
592 * The returned channel will be deleted when its file descriptor is closed.
595 long lttng_session_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
597 struct lttng_session
*session
= file
->private_data
;
598 struct lttng_kernel_channel chan_param
;
599 struct lttng_kernel_old_channel old_chan_param
;
602 case LTTNG_KERNEL_OLD_CHANNEL
:
604 if (copy_from_user(&old_chan_param
,
605 (struct lttng_kernel_old_channel __user
*) arg
,
606 sizeof(struct lttng_kernel_old_channel
)))
608 chan_param
.overwrite
= old_chan_param
.overwrite
;
609 chan_param
.subbuf_size
= old_chan_param
.subbuf_size
;
610 chan_param
.num_subbuf
= old_chan_param
.num_subbuf
;
611 chan_param
.switch_timer_interval
= old_chan_param
.switch_timer_interval
;
612 chan_param
.read_timer_interval
= old_chan_param
.read_timer_interval
;
613 chan_param
.output
= old_chan_param
.output
;
615 return lttng_abi_create_channel(file
, &chan_param
,
618 case LTTNG_KERNEL_CHANNEL
:
620 if (copy_from_user(&chan_param
,
621 (struct lttng_kernel_channel __user
*) arg
,
622 sizeof(struct lttng_kernel_channel
)))
624 return lttng_abi_create_channel(file
, &chan_param
,
627 case LTTNG_KERNEL_OLD_SESSION_START
:
628 case LTTNG_KERNEL_OLD_ENABLE
:
629 case LTTNG_KERNEL_SESSION_START
:
630 case LTTNG_KERNEL_ENABLE
:
631 return lttng_session_enable(session
);
632 case LTTNG_KERNEL_OLD_SESSION_STOP
:
633 case LTTNG_KERNEL_OLD_DISABLE
:
634 case LTTNG_KERNEL_SESSION_STOP
:
635 case LTTNG_KERNEL_DISABLE
:
636 return lttng_session_disable(session
);
637 case LTTNG_KERNEL_OLD_METADATA
:
639 if (copy_from_user(&old_chan_param
,
640 (struct lttng_kernel_old_channel __user
*) arg
,
641 sizeof(struct lttng_kernel_old_channel
)))
643 chan_param
.overwrite
= old_chan_param
.overwrite
;
644 chan_param
.subbuf_size
= old_chan_param
.subbuf_size
;
645 chan_param
.num_subbuf
= old_chan_param
.num_subbuf
;
646 chan_param
.switch_timer_interval
= old_chan_param
.switch_timer_interval
;
647 chan_param
.read_timer_interval
= old_chan_param
.read_timer_interval
;
648 chan_param
.output
= old_chan_param
.output
;
650 return lttng_abi_create_channel(file
, &chan_param
,
653 case LTTNG_KERNEL_METADATA
:
655 if (copy_from_user(&chan_param
,
656 (struct lttng_kernel_channel __user
*) arg
,
657 sizeof(struct lttng_kernel_channel
)))
659 return lttng_abi_create_channel(file
, &chan_param
,
662 case LTTNG_KERNEL_SESSION_TRACK_PID
:
663 return lttng_session_track_id(session
, TRACKER_PID
, (int) arg
);
664 case LTTNG_KERNEL_SESSION_UNTRACK_PID
:
665 return lttng_session_untrack_id(session
, TRACKER_PID
, (int) arg
);
666 case LTTNG_KERNEL_SESSION_TRACK_ID
:
668 struct lttng_kernel_tracker_args tracker
;
669 enum tracker_type tracker_type
;
671 if (copy_from_user(&tracker
,
672 (struct lttng_kernel_tracker_args __user
*) arg
,
673 sizeof(struct lttng_kernel_tracker_args
)))
675 tracker_type
= get_tracker_type(&tracker
);
676 if (tracker_type
== TRACKER_UNKNOWN
)
678 return lttng_session_track_id(session
, tracker_type
, tracker
.id
);
680 case LTTNG_KERNEL_SESSION_UNTRACK_ID
:
682 struct lttng_kernel_tracker_args tracker
;
683 enum tracker_type tracker_type
;
685 if (copy_from_user(&tracker
,
686 (struct lttng_kernel_tracker_args __user
*) arg
,
687 sizeof(struct lttng_kernel_tracker_args
)))
689 tracker_type
= get_tracker_type(&tracker
);
690 if (tracker_type
== TRACKER_UNKNOWN
)
692 return lttng_session_untrack_id(session
, tracker_type
,
695 case LTTNG_KERNEL_SESSION_LIST_TRACKER_PIDS
:
696 return lttng_session_list_tracker_ids(session
, TRACKER_PID
);
697 case LTTNG_KERNEL_SESSION_LIST_TRACKER_IDS
:
699 struct lttng_kernel_tracker_args tracker
;
700 enum tracker_type tracker_type
;
702 if (copy_from_user(&tracker
,
703 (struct lttng_kernel_tracker_args __user
*) arg
,
704 sizeof(struct lttng_kernel_tracker_args
)))
706 tracker_type
= get_tracker_type(&tracker
);
707 if (tracker_type
== TRACKER_UNKNOWN
)
709 return lttng_session_list_tracker_ids(session
, tracker_type
);
711 case LTTNG_KERNEL_SESSION_METADATA_REGEN
:
712 return lttng_session_metadata_regenerate(session
);
713 case LTTNG_KERNEL_SESSION_STATEDUMP
:
714 return lttng_session_statedump(session
);
715 case LTTNG_KERNEL_SESSION_SET_NAME
:
717 struct lttng_kernel_session_name name
;
719 if (copy_from_user(&name
,
720 (struct lttng_kernel_session_name __user
*) arg
,
721 sizeof(struct lttng_kernel_session_name
)))
723 return lttng_abi_session_set_name(session
, &name
);
725 case LTTNG_KERNEL_SESSION_SET_CREATION_TIME
:
727 struct lttng_kernel_session_creation_time time
;
729 if (copy_from_user(&time
,
730 (struct lttng_kernel_session_creation_time __user
*) arg
,
731 sizeof(struct lttng_kernel_session_creation_time
)))
733 return lttng_abi_session_set_creation_time(session
, &time
);
741 * Called when the last file reference is dropped.
743 * Big fat note: channels and events are invariant for the whole session after
744 * their creation. So this session destruction also destroys all channel and
745 * event structures specific to this session (they are not destroyed when their
746 * individual file is released).
749 int lttng_session_release(struct inode
*inode
, struct file
*file
)
751 struct lttng_session
*session
= file
->private_data
;
754 lttng_session_destroy(session
);
758 static const struct file_operations lttng_session_fops
= {
759 .owner
= THIS_MODULE
,
760 .release
= lttng_session_release
,
761 .unlocked_ioctl
= lttng_session_ioctl
,
763 .compat_ioctl
= lttng_session_ioctl
,
768 * lttng_metadata_ring_buffer_poll - LTTng ring buffer poll file operation
772 * Handles the poll operations for the metadata channels.
775 unsigned int lttng_metadata_ring_buffer_poll(struct file
*filp
,
778 struct lttng_metadata_stream
*stream
= filp
->private_data
;
779 struct lib_ring_buffer
*buf
= stream
->priv
;
781 unsigned int mask
= 0;
783 if (filp
->f_mode
& FMODE_READ
) {
784 poll_wait_set_exclusive(wait
);
785 poll_wait(filp
, &stream
->read_wait
, wait
);
787 finalized
= stream
->finalized
;
790 * lib_ring_buffer_is_finalized() contains a smp_rmb()
791 * ordering finalized load before offsets loads.
793 WARN_ON(atomic_long_read(&buf
->active_readers
) != 1);
798 mutex_lock(&stream
->metadata_cache
->lock
);
799 if (stream
->metadata_cache
->metadata_written
>
800 stream
->metadata_out
)
802 mutex_unlock(&stream
->metadata_cache
->lock
);
809 void lttng_metadata_ring_buffer_ioctl_put_next_subbuf(struct file
*filp
,
810 unsigned int cmd
, unsigned long arg
)
812 struct lttng_metadata_stream
*stream
= filp
->private_data
;
814 stream
->metadata_out
= stream
->metadata_in
;
818 * Reset the counter of how much metadata has been consumed to 0. That way,
819 * the consumer receives the content of the metadata cache unchanged. This is
820 * different from the metadata_regenerate where the offset from epoch is
821 * resampled, here we want the exact same content as the last time the metadata
822 * was generated. This command is only possible if all the metadata written
823 * in the cache has been output to the metadata stream to avoid corrupting the
826 * Return 0 on success, a negative value on error.
829 int lttng_metadata_cache_dump(struct lttng_metadata_stream
*stream
)
832 struct lttng_metadata_cache
*cache
= stream
->metadata_cache
;
834 mutex_lock(&cache
->lock
);
835 if (stream
->metadata_out
!= cache
->metadata_written
) {
839 stream
->metadata_out
= 0;
840 stream
->metadata_in
= 0;
841 wake_up_interruptible(&stream
->read_wait
);
845 mutex_unlock(&cache
->lock
);
850 long lttng_metadata_ring_buffer_ioctl(struct file
*filp
,
851 unsigned int cmd
, unsigned long arg
)
854 struct lttng_metadata_stream
*stream
= filp
->private_data
;
855 struct lib_ring_buffer
*buf
= stream
->priv
;
859 if (cmd
== RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK
)
860 rb_cmd
= RING_BUFFER_GET_NEXT_SUBBUF
;
865 case RING_BUFFER_GET_NEXT_SUBBUF
:
867 struct lttng_metadata_stream
*stream
= filp
->private_data
;
868 struct lib_ring_buffer
*buf
= stream
->priv
;
869 struct channel
*chan
= buf
->backend
.chan
;
871 ret
= lttng_metadata_output_channel(stream
, chan
, NULL
);
873 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
879 case RING_BUFFER_GET_SUBBUF
:
882 * Random access is not allowed for metadata channel.
886 case RING_BUFFER_FLUSH_EMPTY
: /* Fall-through. */
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
: /* Fall-through. */
996 case RING_BUFFER_FLUSH
:
998 struct lttng_metadata_stream
*stream
= filp
->private_data
;
999 struct lib_ring_buffer
*buf
= stream
->priv
;
1000 struct channel
*chan
= buf
->backend
.chan
;
1003 * Before doing the actual ring buffer flush, write up to one
1004 * packet of metadata in the ring buffer.
1006 ret
= lttng_metadata_output_channel(stream
, chan
, NULL
);
1011 case RING_BUFFER_GET_METADATA_VERSION
:
1013 struct lttng_metadata_stream
*stream
= filp
->private_data
;
1015 return put_u64(stream
->version
, arg
);
1017 case RING_BUFFER_METADATA_CACHE_DUMP
:
1019 struct lttng_metadata_stream
*stream
= filp
->private_data
;
1021 return lttng_metadata_cache_dump(stream
);
1023 case RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK
:
1025 struct lttng_metadata_stream
*stream
= filp
->private_data
;
1026 struct lib_ring_buffer
*buf
= stream
->priv
;
1027 struct channel
*chan
= buf
->backend
.chan
;
1029 ret
= lttng_metadata_output_channel(stream
, chan
, &coherent
);
1031 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
1033 } else if (ret
< 0) {
1041 /* PUT_SUBBUF is the one from lib ring buffer, unmodified. */
1043 /* Performing lib ring buffer ioctl after our own. */
1044 ret
= lib_ring_buffer_compat_ioctl(filp
, rb_cmd
, arg
, buf
);
1049 case RING_BUFFER_PUT_NEXT_SUBBUF
:
1051 lttng_metadata_ring_buffer_ioctl_put_next_subbuf(filp
,
1055 case RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK
:
1057 return put_u32(coherent
, arg
);
1068 * This is not used by anonymous file descriptors. This code is left
1069 * there if we ever want to implement an inode with open() operation.
1072 int lttng_metadata_ring_buffer_open(struct inode
*inode
, struct file
*file
)
1074 struct lttng_metadata_stream
*stream
= inode
->i_private
;
1075 struct lib_ring_buffer
*buf
= stream
->priv
;
1077 file
->private_data
= buf
;
1079 * Since life-time of metadata cache differs from that of
1080 * session, we need to keep our own reference on the transport.
1082 if (!try_module_get(stream
->transport
->owner
)) {
1083 printk(KERN_WARNING
"LTTng: Can't lock transport module.\n");
1086 return lib_ring_buffer_open(inode
, file
, buf
);
1090 int lttng_metadata_ring_buffer_release(struct inode
*inode
, struct file
*file
)
1092 struct lttng_metadata_stream
*stream
= file
->private_data
;
1093 struct lib_ring_buffer
*buf
= stream
->priv
;
1095 mutex_lock(&stream
->metadata_cache
->lock
);
1096 list_del(&stream
->list
);
1097 mutex_unlock(&stream
->metadata_cache
->lock
);
1098 kref_put(&stream
->metadata_cache
->refcount
, metadata_cache_destroy
);
1099 module_put(stream
->transport
->owner
);
1101 return lib_ring_buffer_release(inode
, file
, buf
);
1105 ssize_t
lttng_metadata_ring_buffer_splice_read(struct file
*in
, loff_t
*ppos
,
1106 struct pipe_inode_info
*pipe
, size_t len
,
1109 struct lttng_metadata_stream
*stream
= in
->private_data
;
1110 struct lib_ring_buffer
*buf
= stream
->priv
;
1112 return lib_ring_buffer_splice_read(in
, ppos
, pipe
, len
,
1117 int lttng_metadata_ring_buffer_mmap(struct file
*filp
,
1118 struct vm_area_struct
*vma
)
1120 struct lttng_metadata_stream
*stream
= filp
->private_data
;
1121 struct lib_ring_buffer
*buf
= stream
->priv
;
1123 return lib_ring_buffer_mmap(filp
, vma
, buf
);
1127 const struct file_operations lttng_metadata_ring_buffer_file_operations
= {
1128 .owner
= THIS_MODULE
,
1129 .open
= lttng_metadata_ring_buffer_open
,
1130 .release
= lttng_metadata_ring_buffer_release
,
1131 .poll
= lttng_metadata_ring_buffer_poll
,
1132 .splice_read
= lttng_metadata_ring_buffer_splice_read
,
1133 .mmap
= lttng_metadata_ring_buffer_mmap
,
1134 .unlocked_ioctl
= lttng_metadata_ring_buffer_ioctl
,
1135 .llseek
= vfs_lib_ring_buffer_no_llseek
,
1136 #ifdef CONFIG_COMPAT
1137 .compat_ioctl
= lttng_metadata_ring_buffer_compat_ioctl
,
1142 int lttng_abi_create_stream_fd(struct file
*channel_file
, void *stream_priv
,
1143 const struct file_operations
*fops
, const char *name
)
1146 struct file
*stream_file
;
1148 stream_fd
= lttng_get_unused_fd();
1149 if (stream_fd
< 0) {
1153 stream_file
= anon_inode_getfile(name
, fops
, stream_priv
, O_RDWR
);
1154 if (IS_ERR(stream_file
)) {
1155 ret
= PTR_ERR(stream_file
);
1159 * OPEN_FMODE, called within anon_inode_getfile/alloc_file, don't honor
1160 * FMODE_LSEEK, FMODE_PREAD nor FMODE_PWRITE. We need to read from this
1161 * file descriptor, so we set FMODE_PREAD here.
1163 stream_file
->f_mode
|= FMODE_PREAD
;
1164 fd_install(stream_fd
, stream_file
);
1166 * The stream holds a reference to the channel within the generic ring
1167 * buffer library, so no need to hold a refcount on the channel and
1168 * session files here.
1173 put_unused_fd(stream_fd
);
1179 int lttng_abi_open_stream(struct file
*channel_file
)
1181 struct lttng_channel
*channel
= channel_file
->private_data
;
1182 struct lib_ring_buffer
*buf
;
1186 buf
= channel
->ops
->buffer_read_open(channel
->chan
);
1191 ret
= lttng_abi_create_stream_fd(channel_file
, stream_priv
,
1192 <tng_stream_ring_buffer_file_operations
,
1200 channel
->ops
->buffer_read_close(buf
);
1205 int lttng_abi_open_metadata_stream(struct file
*channel_file
)
1207 struct lttng_channel
*channel
= channel_file
->private_data
;
1208 struct lttng_session
*session
= channel
->session
;
1209 struct lib_ring_buffer
*buf
;
1211 struct lttng_metadata_stream
*metadata_stream
;
1214 buf
= channel
->ops
->buffer_read_open(channel
->chan
);
1218 metadata_stream
= kzalloc(sizeof(struct lttng_metadata_stream
),
1220 if (!metadata_stream
) {
1224 metadata_stream
->metadata_cache
= session
->metadata_cache
;
1225 init_waitqueue_head(&metadata_stream
->read_wait
);
1226 metadata_stream
->priv
= buf
;
1227 stream_priv
= metadata_stream
;
1228 metadata_stream
->transport
= channel
->transport
;
1229 /* Initial state is an empty metadata, considered as incoherent. */
1230 metadata_stream
->coherent
= false;
1233 * Since life-time of metadata cache differs from that of
1234 * session, we need to keep our own reference on the transport.
1236 if (!try_module_get(metadata_stream
->transport
->owner
)) {
1237 printk(KERN_WARNING
"LTTng: Can't lock transport module.\n");
1242 if (!lttng_kref_get(&session
->metadata_cache
->refcount
)) {
1247 ret
= lttng_abi_create_stream_fd(channel_file
, stream_priv
,
1248 <tng_metadata_ring_buffer_file_operations
,
1249 "[lttng_metadata_stream]");
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
: /* Fallthrough */
1297 case LTTNG_KERNEL_KPROBE
: /* Fallthrough */
1298 case LTTNG_KERNEL_KRETPROBE
: /* Fallthrough */
1299 case LTTNG_KERNEL_NOOP
: /* Fallthrough */
1300 case LTTNG_KERNEL_UPROBE
:
1303 case LTTNG_KERNEL_FUNCTION
: /* Fallthrough */
1311 int lttng_abi_create_event(struct file
*channel_file
,
1312 struct lttng_kernel_event
*event_param
)
1314 struct lttng_channel
*channel
= channel_file
->private_data
;
1316 struct file
*event_file
;
1319 event_param
->name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
1320 switch (event_param
->instrumentation
) {
1321 case LTTNG_KERNEL_KRETPROBE
:
1322 event_param
->u
.kretprobe
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
1324 case LTTNG_KERNEL_KPROBE
:
1325 event_param
->u
.kprobe
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
1327 case LTTNG_KERNEL_FUNCTION
:
1329 /* Not implemented. */
1334 event_fd
= lttng_get_unused_fd();
1339 event_file
= anon_inode_getfile("[lttng_event]",
1342 if (IS_ERR(event_file
)) {
1343 ret
= PTR_ERR(event_file
);
1346 /* The event holds a reference on the channel */
1347 if (!atomic_long_add_unless(&channel_file
->f_count
, 1, LONG_MAX
)) {
1349 goto refcount_error
;
1351 ret
= lttng_abi_validate_event_param(event_param
);
1354 if (event_param
->instrumentation
== LTTNG_KERNEL_TRACEPOINT
1355 || event_param
->instrumentation
== LTTNG_KERNEL_SYSCALL
) {
1356 struct lttng_event_enabler
*event_enabler
;
1358 if (strutils_is_star_glob_pattern(event_param
->name
)) {
1360 * If the event name is a star globbing pattern,
1361 * we create the special star globbing enabler.
1363 event_enabler
= lttng_event_enabler_create(LTTNG_ENABLER_FORMAT_STAR_GLOB
,
1364 event_param
, channel
);
1366 event_enabler
= lttng_event_enabler_create(LTTNG_ENABLER_FORMAT_NAME
,
1367 event_param
, channel
);
1369 priv
= event_enabler
;
1371 struct lttng_event
*event
;
1374 * We tolerate no failure path after event creation. It
1375 * will stay invariant for the rest of the session.
1377 event
= lttng_event_create(channel
, event_param
,
1379 event_param
->instrumentation
);
1380 WARN_ON_ONCE(!event
);
1381 if (IS_ERR(event
)) {
1382 ret
= PTR_ERR(event
);
1387 event_file
->private_data
= priv
;
1388 fd_install(event_fd
, event_file
);
1392 atomic_long_dec(&channel_file
->f_count
);
1396 put_unused_fd(event_fd
);
1402 * lttng_channel_ioctl - lttng syscall through ioctl
1408 * This ioctl implements lttng commands:
1409 * LTTNG_KERNEL_STREAM
1410 * Returns an event stream file descriptor or failure.
1411 * (typically, one event stream records events from one CPU)
1412 * LTTNG_KERNEL_EVENT
1413 * Returns an event file descriptor or failure.
1414 * LTTNG_KERNEL_CONTEXT
1415 * Prepend a context field to each event in the channel
1416 * LTTNG_KERNEL_ENABLE
1417 * Enable recording for events in this channel (weak enable)
1418 * LTTNG_KERNEL_DISABLE
1419 * Disable recording for events in this channel (strong disable)
1421 * Channel and event file descriptors also hold a reference on the session.
1424 long lttng_channel_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1426 struct lttng_channel
*channel
= file
->private_data
;
1429 case LTTNG_KERNEL_OLD_STREAM
:
1430 case LTTNG_KERNEL_STREAM
:
1431 return lttng_abi_open_stream(file
);
1432 case LTTNG_KERNEL_OLD_EVENT
:
1434 struct lttng_kernel_event
*uevent_param
;
1435 struct lttng_kernel_old_event
*old_uevent_param
;
1438 uevent_param
= kmalloc(sizeof(struct lttng_kernel_event
),
1440 if (!uevent_param
) {
1444 old_uevent_param
= kmalloc(
1445 sizeof(struct lttng_kernel_old_event
),
1447 if (!old_uevent_param
) {
1449 goto old_event_error_free_param
;
1451 if (copy_from_user(old_uevent_param
,
1452 (struct lttng_kernel_old_event __user
*) arg
,
1453 sizeof(struct lttng_kernel_old_event
))) {
1455 goto old_event_error_free_old_param
;
1458 memcpy(uevent_param
->name
, old_uevent_param
->name
,
1459 sizeof(uevent_param
->name
));
1460 uevent_param
->instrumentation
=
1461 old_uevent_param
->instrumentation
;
1463 switch (old_uevent_param
->instrumentation
) {
1464 case LTTNG_KERNEL_KPROBE
:
1465 uevent_param
->u
.kprobe
.addr
=
1466 old_uevent_param
->u
.kprobe
.addr
;
1467 uevent_param
->u
.kprobe
.offset
=
1468 old_uevent_param
->u
.kprobe
.offset
;
1469 memcpy(uevent_param
->u
.kprobe
.symbol_name
,
1470 old_uevent_param
->u
.kprobe
.symbol_name
,
1471 sizeof(uevent_param
->u
.kprobe
.symbol_name
));
1473 case LTTNG_KERNEL_KRETPROBE
:
1474 uevent_param
->u
.kretprobe
.addr
=
1475 old_uevent_param
->u
.kretprobe
.addr
;
1476 uevent_param
->u
.kretprobe
.offset
=
1477 old_uevent_param
->u
.kretprobe
.offset
;
1478 memcpy(uevent_param
->u
.kretprobe
.symbol_name
,
1479 old_uevent_param
->u
.kretprobe
.symbol_name
,
1480 sizeof(uevent_param
->u
.kretprobe
.symbol_name
));
1482 case LTTNG_KERNEL_FUNCTION
:
1484 /* Not implemented. */
1489 ret
= lttng_abi_create_event(file
, uevent_param
);
1491 old_event_error_free_old_param
:
1492 kfree(old_uevent_param
);
1493 old_event_error_free_param
:
1494 kfree(uevent_param
);
1498 case LTTNG_KERNEL_EVENT
:
1500 struct lttng_kernel_event uevent_param
;
1502 if (copy_from_user(&uevent_param
,
1503 (struct lttng_kernel_event __user
*) arg
,
1504 sizeof(uevent_param
)))
1506 return lttng_abi_create_event(file
, &uevent_param
);
1508 case LTTNG_KERNEL_OLD_CONTEXT
:
1510 struct lttng_kernel_context
*ucontext_param
;
1511 struct lttng_kernel_old_context
*old_ucontext_param
;
1514 ucontext_param
= kmalloc(sizeof(struct lttng_kernel_context
),
1516 if (!ucontext_param
) {
1520 old_ucontext_param
= kmalloc(sizeof(struct lttng_kernel_old_context
),
1522 if (!old_ucontext_param
) {
1524 goto old_ctx_error_free_param
;
1527 if (copy_from_user(old_ucontext_param
,
1528 (struct lttng_kernel_old_context __user
*) arg
,
1529 sizeof(struct lttng_kernel_old_context
))) {
1531 goto old_ctx_error_free_old_param
;
1533 ucontext_param
->ctx
= old_ucontext_param
->ctx
;
1534 memcpy(ucontext_param
->padding
, old_ucontext_param
->padding
,
1535 sizeof(ucontext_param
->padding
));
1536 /* only type that uses the union */
1537 if (old_ucontext_param
->ctx
== LTTNG_KERNEL_CONTEXT_PERF_COUNTER
) {
1538 ucontext_param
->u
.perf_counter
.type
=
1539 old_ucontext_param
->u
.perf_counter
.type
;
1540 ucontext_param
->u
.perf_counter
.config
=
1541 old_ucontext_param
->u
.perf_counter
.config
;
1542 memcpy(ucontext_param
->u
.perf_counter
.name
,
1543 old_ucontext_param
->u
.perf_counter
.name
,
1544 sizeof(ucontext_param
->u
.perf_counter
.name
));
1547 ret
= lttng_abi_add_context(file
,
1549 &channel
->ctx
, channel
->session
);
1551 old_ctx_error_free_old_param
:
1552 kfree(old_ucontext_param
);
1553 old_ctx_error_free_param
:
1554 kfree(ucontext_param
);
1558 case LTTNG_KERNEL_CONTEXT
:
1560 struct lttng_kernel_context ucontext_param
;
1562 if (copy_from_user(&ucontext_param
,
1563 (struct lttng_kernel_context __user
*) arg
,
1564 sizeof(ucontext_param
)))
1566 return lttng_abi_add_context(file
,
1568 &channel
->ctx
, channel
->session
);
1570 case LTTNG_KERNEL_OLD_ENABLE
:
1571 case LTTNG_KERNEL_ENABLE
:
1572 return lttng_channel_enable(channel
);
1573 case LTTNG_KERNEL_OLD_DISABLE
:
1574 case LTTNG_KERNEL_DISABLE
:
1575 return lttng_channel_disable(channel
);
1576 case LTTNG_KERNEL_SYSCALL_MASK
:
1577 return lttng_channel_syscall_mask(channel
,
1578 (struct lttng_kernel_syscall_mask __user
*) arg
);
1580 return -ENOIOCTLCMD
;
1585 * lttng_metadata_ioctl - lttng syscall through ioctl
1591 * This ioctl implements lttng commands:
1592 * LTTNG_KERNEL_STREAM
1593 * Returns an event stream file descriptor or failure.
1595 * Channel and event file descriptors also hold a reference on the session.
1598 long lttng_metadata_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1601 case LTTNG_KERNEL_OLD_STREAM
:
1602 case LTTNG_KERNEL_STREAM
:
1603 return lttng_abi_open_metadata_stream(file
);
1605 return -ENOIOCTLCMD
;
1610 * lttng_channel_poll - lttng stream addition/removal monitoring
1615 unsigned int lttng_channel_poll(struct file
*file
, poll_table
*wait
)
1617 struct lttng_channel
*channel
= file
->private_data
;
1618 unsigned int mask
= 0;
1620 if (file
->f_mode
& FMODE_READ
) {
1621 poll_wait_set_exclusive(wait
);
1622 poll_wait(file
, channel
->ops
->get_hp_wait_queue(channel
->chan
),
1625 if (channel
->ops
->is_disabled(channel
->chan
))
1627 if (channel
->ops
->is_finalized(channel
->chan
))
1629 if (channel
->ops
->buffer_has_read_closed_stream(channel
->chan
))
1630 return POLLIN
| POLLRDNORM
;
1638 int lttng_channel_release(struct inode
*inode
, struct file
*file
)
1640 struct lttng_channel
*channel
= file
->private_data
;
1643 fput(channel
->session
->file
);
1648 int lttng_metadata_channel_release(struct inode
*inode
, struct file
*file
)
1650 struct lttng_channel
*channel
= file
->private_data
;
1653 fput(channel
->session
->file
);
1654 lttng_metadata_channel_destroy(channel
);
1660 static const struct file_operations lttng_channel_fops
= {
1661 .owner
= THIS_MODULE
,
1662 .release
= lttng_channel_release
,
1663 .poll
= lttng_channel_poll
,
1664 .unlocked_ioctl
= lttng_channel_ioctl
,
1665 #ifdef CONFIG_COMPAT
1666 .compat_ioctl
= lttng_channel_ioctl
,
1670 static const struct file_operations lttng_metadata_fops
= {
1671 .owner
= THIS_MODULE
,
1672 .release
= lttng_metadata_channel_release
,
1673 .unlocked_ioctl
= lttng_metadata_ioctl
,
1674 #ifdef CONFIG_COMPAT
1675 .compat_ioctl
= lttng_metadata_ioctl
,
1680 * lttng_event_ioctl - lttng syscall through ioctl
1686 * This ioctl implements lttng commands:
1687 * LTTNG_KERNEL_CONTEXT
1688 * Prepend a context field to each record of this event
1689 * LTTNG_KERNEL_ENABLE
1690 * Enable recording for this event (weak enable)
1691 * LTTNG_KERNEL_DISABLE
1692 * Disable recording for this event (strong disable)
1695 long lttng_event_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1697 struct lttng_event
*event
;
1698 struct lttng_event_enabler
*event_enabler
;
1699 enum lttng_event_type
*evtype
= file
->private_data
;
1702 case LTTNG_KERNEL_OLD_CONTEXT
:
1704 /* Not implemented */
1707 case LTTNG_KERNEL_CONTEXT
:
1709 /* Not implemented */
1712 case LTTNG_KERNEL_OLD_ENABLE
:
1713 case LTTNG_KERNEL_ENABLE
:
1715 case LTTNG_TYPE_EVENT
:
1716 event
= file
->private_data
;
1717 return lttng_event_enable(event
);
1718 case LTTNG_TYPE_ENABLER
:
1719 event_enabler
= file
->private_data
;
1720 return lttng_event_enabler_enable(event_enabler
);
1725 case LTTNG_KERNEL_OLD_DISABLE
:
1726 case LTTNG_KERNEL_DISABLE
:
1728 case LTTNG_TYPE_EVENT
:
1729 event
= file
->private_data
;
1730 return lttng_event_disable(event
);
1731 case LTTNG_TYPE_ENABLER
:
1732 event_enabler
= file
->private_data
;
1733 return lttng_event_enabler_disable(event_enabler
);
1738 case LTTNG_KERNEL_FILTER
:
1740 case LTTNG_TYPE_EVENT
:
1742 case LTTNG_TYPE_ENABLER
:
1744 event_enabler
= file
->private_data
;
1745 return lttng_event_enabler_attach_bytecode(event_enabler
,
1746 (struct lttng_kernel_filter_bytecode __user
*) arg
);
1752 case LTTNG_KERNEL_ADD_CALLSITE
:
1754 case LTTNG_TYPE_EVENT
:
1755 event
= file
->private_data
;
1756 return lttng_event_add_callsite(event
,
1757 (struct lttng_kernel_event_callsite __user
*) arg
);
1758 case LTTNG_TYPE_ENABLER
:
1765 return -ENOIOCTLCMD
;
1770 int lttng_event_release(struct inode
*inode
, struct file
*file
)
1772 struct lttng_event
*event
;
1773 struct lttng_event_enabler
*event_enabler
;
1774 enum lttng_event_type
*evtype
= file
->private_data
;
1780 case LTTNG_TYPE_EVENT
:
1781 event
= file
->private_data
;
1783 fput(event
->chan
->file
);
1785 case LTTNG_TYPE_ENABLER
:
1786 event_enabler
= file
->private_data
;
1788 fput(event_enabler
->chan
->file
);
1798 /* TODO: filter control ioctl */
1799 static const struct file_operations lttng_event_fops
= {
1800 .owner
= THIS_MODULE
,
1801 .release
= lttng_event_release
,
1802 .unlocked_ioctl
= lttng_event_ioctl
,
1803 #ifdef CONFIG_COMPAT
1804 .compat_ioctl
= lttng_event_ioctl
,
1808 static int put_u64(uint64_t val
, unsigned long arg
)
1810 return put_user(val
, (uint64_t __user
*) arg
);
1813 static int put_u32(uint32_t val
, unsigned long arg
)
1815 return put_user(val
, (uint32_t __user
*) arg
);
1818 static long lttng_stream_ring_buffer_ioctl(struct file
*filp
,
1819 unsigned int cmd
, unsigned long arg
)
1821 struct lib_ring_buffer
*buf
= filp
->private_data
;
1822 struct channel
*chan
= buf
->backend
.chan
;
1823 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1824 const struct lttng_channel_ops
*ops
= chan
->backend
.priv_ops
;
1827 if (atomic_read(&chan
->record_disabled
))
1831 case LTTNG_RING_BUFFER_GET_TIMESTAMP_BEGIN
:
1835 ret
= ops
->timestamp_begin(config
, buf
, &ts
);
1838 return put_u64(ts
, arg
);
1840 case LTTNG_RING_BUFFER_GET_TIMESTAMP_END
:
1844 ret
= ops
->timestamp_end(config
, buf
, &ts
);
1847 return put_u64(ts
, arg
);
1849 case LTTNG_RING_BUFFER_GET_EVENTS_DISCARDED
:
1853 ret
= ops
->events_discarded(config
, buf
, &ed
);
1856 return put_u64(ed
, arg
);
1858 case LTTNG_RING_BUFFER_GET_CONTENT_SIZE
:
1862 ret
= ops
->content_size(config
, buf
, &cs
);
1865 return put_u64(cs
, arg
);
1867 case LTTNG_RING_BUFFER_GET_PACKET_SIZE
:
1871 ret
= ops
->packet_size(config
, buf
, &ps
);
1874 return put_u64(ps
, arg
);
1876 case LTTNG_RING_BUFFER_GET_STREAM_ID
:
1880 ret
= ops
->stream_id(config
, buf
, &si
);
1883 return put_u64(si
, arg
);
1885 case LTTNG_RING_BUFFER_GET_CURRENT_TIMESTAMP
:
1889 ret
= ops
->current_timestamp(config
, buf
, &ts
);
1892 return put_u64(ts
, arg
);
1894 case LTTNG_RING_BUFFER_GET_SEQ_NUM
:
1898 ret
= ops
->sequence_number(config
, buf
, &seq
);
1901 return put_u64(seq
, arg
);
1903 case LTTNG_RING_BUFFER_INSTANCE_ID
:
1907 ret
= ops
->instance_id(config
, buf
, &id
);
1910 return put_u64(id
, arg
);
1913 return lib_ring_buffer_file_operations
.unlocked_ioctl(filp
,
1921 #ifdef CONFIG_COMPAT
1922 static long lttng_stream_ring_buffer_compat_ioctl(struct file
*filp
,
1923 unsigned int cmd
, unsigned long arg
)
1925 struct lib_ring_buffer
*buf
= filp
->private_data
;
1926 struct channel
*chan
= buf
->backend
.chan
;
1927 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1928 const struct lttng_channel_ops
*ops
= chan
->backend
.priv_ops
;
1931 if (atomic_read(&chan
->record_disabled
))
1935 case LTTNG_RING_BUFFER_COMPAT_GET_TIMESTAMP_BEGIN
:
1939 ret
= ops
->timestamp_begin(config
, buf
, &ts
);
1942 return put_u64(ts
, arg
);
1944 case LTTNG_RING_BUFFER_COMPAT_GET_TIMESTAMP_END
:
1948 ret
= ops
->timestamp_end(config
, buf
, &ts
);
1951 return put_u64(ts
, arg
);
1953 case LTTNG_RING_BUFFER_COMPAT_GET_EVENTS_DISCARDED
:
1957 ret
= ops
->events_discarded(config
, buf
, &ed
);
1960 return put_u64(ed
, arg
);
1962 case LTTNG_RING_BUFFER_COMPAT_GET_CONTENT_SIZE
:
1966 ret
= ops
->content_size(config
, buf
, &cs
);
1969 return put_u64(cs
, arg
);
1971 case LTTNG_RING_BUFFER_COMPAT_GET_PACKET_SIZE
:
1975 ret
= ops
->packet_size(config
, buf
, &ps
);
1978 return put_u64(ps
, arg
);
1980 case LTTNG_RING_BUFFER_COMPAT_GET_STREAM_ID
:
1984 ret
= ops
->stream_id(config
, buf
, &si
);
1987 return put_u64(si
, arg
);
1989 case LTTNG_RING_BUFFER_GET_CURRENT_TIMESTAMP
:
1993 ret
= ops
->current_timestamp(config
, buf
, &ts
);
1996 return put_u64(ts
, arg
);
1998 case LTTNG_RING_BUFFER_COMPAT_GET_SEQ_NUM
:
2002 ret
= ops
->sequence_number(config
, buf
, &seq
);
2005 return put_u64(seq
, arg
);
2007 case LTTNG_RING_BUFFER_COMPAT_INSTANCE_ID
:
2011 ret
= ops
->instance_id(config
, buf
, &id
);
2014 return put_u64(id
, arg
);
2017 return lib_ring_buffer_file_operations
.compat_ioctl(filp
,
2024 #endif /* CONFIG_COMPAT */
2026 static void lttng_stream_override_ring_buffer_fops(void)
2028 lttng_stream_ring_buffer_file_operations
.owner
= THIS_MODULE
;
2029 lttng_stream_ring_buffer_file_operations
.open
=
2030 lib_ring_buffer_file_operations
.open
;
2031 lttng_stream_ring_buffer_file_operations
.release
=
2032 lib_ring_buffer_file_operations
.release
;
2033 lttng_stream_ring_buffer_file_operations
.poll
=
2034 lib_ring_buffer_file_operations
.poll
;
2035 lttng_stream_ring_buffer_file_operations
.splice_read
=
2036 lib_ring_buffer_file_operations
.splice_read
;
2037 lttng_stream_ring_buffer_file_operations
.mmap
=
2038 lib_ring_buffer_file_operations
.mmap
;
2039 lttng_stream_ring_buffer_file_operations
.unlocked_ioctl
=
2040 lttng_stream_ring_buffer_ioctl
;
2041 lttng_stream_ring_buffer_file_operations
.llseek
=
2042 lib_ring_buffer_file_operations
.llseek
;
2043 #ifdef CONFIG_COMPAT
2044 lttng_stream_ring_buffer_file_operations
.compat_ioctl
=
2045 lttng_stream_ring_buffer_compat_ioctl
;
2049 int __init
lttng_abi_init(void)
2053 wrapper_vmalloc_sync_mappings();
2056 ret
= lttng_tp_mempool_init();
2061 lttng_proc_dentry
= proc_create_data("lttng", S_IRUSR
| S_IWUSR
, NULL
,
2062 <tng_proc_ops
, NULL
);
2064 if (!lttng_proc_dentry
) {
2065 printk(KERN_ERR
"LTTng: Error creating control file\n");
2069 lttng_stream_override_ring_buffer_fops();
2073 lttng_tp_mempool_destroy();
2074 lttng_clock_unref();
2078 /* No __exit annotation because used by init error path too. */
2079 void lttng_abi_exit(void)
2081 lttng_tp_mempool_destroy();
2082 lttng_clock_unref();
2083 if (lttng_proc_dentry
)
2084 remove_proc_entry("lttng", NULL
);