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 <linux/kref.h>
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 <lttng-string-utils.h>
40 #include <lttng-abi.h>
41 #include <lttng-abi-old.h>
42 #include <lttng-events.h>
43 #include <lttng-tracer.h>
44 #include <lttng-tp-mempool.h>
45 #include <lib/ringbuffer/frontend_types.h>
48 * This is LTTng's own personal way to create a system call as an external
49 * module. We use ioctl() on /proc/lttng.
52 static struct proc_dir_entry
*lttng_proc_dentry
;
54 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(5,6,0))
55 static const struct proc_ops lttng_proc_ops
;
57 static const struct file_operations lttng_proc_ops
;
60 static const struct file_operations lttng_session_fops
;
61 static const struct file_operations lttng_channel_fops
;
62 static const struct file_operations lttng_metadata_fops
;
63 static const struct file_operations lttng_event_fops
;
64 static struct file_operations lttng_stream_ring_buffer_file_operations
;
66 static int put_u64(uint64_t val
, unsigned long arg
);
69 * Teardown management: opened file descriptors keep a refcount on the module,
70 * so it can only exit when all file descriptors are closed.
74 int lttng_abi_create_session(void)
76 struct lttng_session
*session
;
77 struct file
*session_file
;
80 session
= lttng_session_create();
83 session_fd
= lttng_get_unused_fd();
88 session_file
= anon_inode_getfile("[lttng_session]",
91 if (IS_ERR(session_file
)) {
92 ret
= PTR_ERR(session_file
);
95 session
->file
= session_file
;
96 fd_install(session_fd
, session_file
);
100 put_unused_fd(session_fd
);
102 lttng_session_destroy(session
);
107 int lttng_abi_tracepoint_list(void)
109 struct file
*tracepoint_list_file
;
112 file_fd
= lttng_get_unused_fd();
118 tracepoint_list_file
= anon_inode_getfile("[lttng_tracepoint_list]",
119 <tng_tracepoint_list_fops
,
121 if (IS_ERR(tracepoint_list_file
)) {
122 ret
= PTR_ERR(tracepoint_list_file
);
125 ret
= lttng_tracepoint_list_fops
.open(NULL
, tracepoint_list_file
);
128 fd_install(file_fd
, tracepoint_list_file
);
132 fput(tracepoint_list_file
);
134 put_unused_fd(file_fd
);
139 #ifndef CONFIG_HAVE_SYSCALL_TRACEPOINTS
141 int lttng_abi_syscall_list(void)
147 int lttng_abi_syscall_list(void)
149 struct file
*syscall_list_file
;
152 file_fd
= lttng_get_unused_fd();
158 syscall_list_file
= anon_inode_getfile("[lttng_syscall_list]",
159 <tng_syscall_list_fops
,
161 if (IS_ERR(syscall_list_file
)) {
162 ret
= PTR_ERR(syscall_list_file
);
165 ret
= lttng_syscall_list_fops
.open(NULL
, syscall_list_file
);
168 fd_install(file_fd
, syscall_list_file
);
172 fput(syscall_list_file
);
174 put_unused_fd(file_fd
);
181 void lttng_abi_tracer_version(struct lttng_kernel_tracer_version
*v
)
183 v
->major
= LTTNG_MODULES_MAJOR_VERSION
;
184 v
->minor
= LTTNG_MODULES_MINOR_VERSION
;
185 v
->patchlevel
= LTTNG_MODULES_PATCHLEVEL_VERSION
;
189 void lttng_abi_tracer_abi_version(struct lttng_kernel_tracer_abi_version
*v
)
191 v
->major
= LTTNG_MODULES_ABI_MAJOR_VERSION
;
192 v
->minor
= LTTNG_MODULES_ABI_MINOR_VERSION
;
196 long lttng_abi_add_context(struct file
*file
,
197 struct lttng_kernel_context
*context_param
,
198 struct lttng_ctx
**ctx
, struct lttng_session
*session
)
201 if (session
->been_active
)
204 switch (context_param
->ctx
) {
205 case LTTNG_KERNEL_CONTEXT_PID
:
206 return lttng_add_pid_to_ctx(ctx
);
207 case LTTNG_KERNEL_CONTEXT_PRIO
:
208 return lttng_add_prio_to_ctx(ctx
);
209 case LTTNG_KERNEL_CONTEXT_NICE
:
210 return lttng_add_nice_to_ctx(ctx
);
211 case LTTNG_KERNEL_CONTEXT_VPID
:
212 return lttng_add_vpid_to_ctx(ctx
);
213 case LTTNG_KERNEL_CONTEXT_TID
:
214 return lttng_add_tid_to_ctx(ctx
);
215 case LTTNG_KERNEL_CONTEXT_VTID
:
216 return lttng_add_vtid_to_ctx(ctx
);
217 case LTTNG_KERNEL_CONTEXT_PPID
:
218 return lttng_add_ppid_to_ctx(ctx
);
219 case LTTNG_KERNEL_CONTEXT_VPPID
:
220 return lttng_add_vppid_to_ctx(ctx
);
221 case LTTNG_KERNEL_CONTEXT_PERF_COUNTER
:
222 context_param
->u
.perf_counter
.name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
223 return lttng_add_perf_counter_to_ctx(context_param
->u
.perf_counter
.type
,
224 context_param
->u
.perf_counter
.config
,
225 context_param
->u
.perf_counter
.name
,
227 case LTTNG_KERNEL_CONTEXT_PROCNAME
:
228 return lttng_add_procname_to_ctx(ctx
);
229 case LTTNG_KERNEL_CONTEXT_HOSTNAME
:
230 return lttng_add_hostname_to_ctx(ctx
);
231 case LTTNG_KERNEL_CONTEXT_CPU_ID
:
232 return lttng_add_cpu_id_to_ctx(ctx
);
233 case LTTNG_KERNEL_CONTEXT_INTERRUPTIBLE
:
234 return lttng_add_interruptible_to_ctx(ctx
);
235 case LTTNG_KERNEL_CONTEXT_NEED_RESCHEDULE
:
236 return lttng_add_need_reschedule_to_ctx(ctx
);
237 case LTTNG_KERNEL_CONTEXT_PREEMPTIBLE
:
238 return lttng_add_preemptible_to_ctx(ctx
);
239 case LTTNG_KERNEL_CONTEXT_MIGRATABLE
:
240 return lttng_add_migratable_to_ctx(ctx
);
241 case LTTNG_KERNEL_CONTEXT_CALLSTACK_KERNEL
:
242 case LTTNG_KERNEL_CONTEXT_CALLSTACK_USER
:
243 return lttng_add_callstack_to_ctx(ctx
, context_param
->ctx
);
244 case LTTNG_KERNEL_CONTEXT_CGROUP_NS
:
245 return lttng_add_cgroup_ns_to_ctx(ctx
);
246 case LTTNG_KERNEL_CONTEXT_IPC_NS
:
247 return lttng_add_ipc_ns_to_ctx(ctx
);
248 case LTTNG_KERNEL_CONTEXT_MNT_NS
:
249 return lttng_add_mnt_ns_to_ctx(ctx
);
250 case LTTNG_KERNEL_CONTEXT_NET_NS
:
251 return lttng_add_net_ns_to_ctx(ctx
);
252 case LTTNG_KERNEL_CONTEXT_PID_NS
:
253 return lttng_add_pid_ns_to_ctx(ctx
);
254 case LTTNG_KERNEL_CONTEXT_USER_NS
:
255 return lttng_add_user_ns_to_ctx(ctx
);
256 case LTTNG_KERNEL_CONTEXT_UTS_NS
:
257 return lttng_add_uts_ns_to_ctx(ctx
);
258 case LTTNG_KERNEL_CONTEXT_UID
:
259 return lttng_add_uid_to_ctx(ctx
);
260 case LTTNG_KERNEL_CONTEXT_EUID
:
261 return lttng_add_euid_to_ctx(ctx
);
262 case LTTNG_KERNEL_CONTEXT_SUID
:
263 return lttng_add_suid_to_ctx(ctx
);
264 case LTTNG_KERNEL_CONTEXT_GID
:
265 return lttng_add_gid_to_ctx(ctx
);
266 case LTTNG_KERNEL_CONTEXT_EGID
:
267 return lttng_add_egid_to_ctx(ctx
);
268 case LTTNG_KERNEL_CONTEXT_SGID
:
269 return lttng_add_sgid_to_ctx(ctx
);
270 case LTTNG_KERNEL_CONTEXT_VUID
:
271 return lttng_add_vuid_to_ctx(ctx
);
272 case LTTNG_KERNEL_CONTEXT_VEUID
:
273 return lttng_add_veuid_to_ctx(ctx
);
274 case LTTNG_KERNEL_CONTEXT_VSUID
:
275 return lttng_add_vsuid_to_ctx(ctx
);
276 case LTTNG_KERNEL_CONTEXT_VGID
:
277 return lttng_add_vgid_to_ctx(ctx
);
278 case LTTNG_KERNEL_CONTEXT_VEGID
:
279 return lttng_add_vegid_to_ctx(ctx
);
280 case LTTNG_KERNEL_CONTEXT_VSGID
:
281 return lttng_add_vsgid_to_ctx(ctx
);
288 * lttng_ioctl - lttng syscall through ioctl
294 * This ioctl implements lttng commands:
295 * LTTNG_KERNEL_SESSION
296 * Returns a LTTng trace session file descriptor
297 * LTTNG_KERNEL_TRACER_VERSION
298 * Returns the LTTng kernel tracer version
299 * LTTNG_KERNEL_TRACEPOINT_LIST
300 * Returns a file descriptor listing available tracepoints
301 * LTTNG_KERNEL_WAIT_QUIESCENT
302 * Returns after all previously running probes have completed
303 * LTTNG_KERNEL_TRACER_ABI_VERSION
304 * Returns the LTTng kernel tracer ABI version
306 * The returned session will be deleted when its file descriptor is closed.
309 long lttng_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
312 case LTTNG_KERNEL_OLD_SESSION
:
313 case LTTNG_KERNEL_SESSION
:
314 return lttng_abi_create_session();
315 case LTTNG_KERNEL_OLD_TRACER_VERSION
:
317 struct lttng_kernel_tracer_version v
;
318 struct lttng_kernel_old_tracer_version oldv
;
319 struct lttng_kernel_old_tracer_version
*uversion
=
320 (struct lttng_kernel_old_tracer_version __user
*) arg
;
322 lttng_abi_tracer_version(&v
);
323 oldv
.major
= v
.major
;
324 oldv
.minor
= v
.minor
;
325 oldv
.patchlevel
= v
.patchlevel
;
327 if (copy_to_user(uversion
, &oldv
, sizeof(oldv
)))
331 case LTTNG_KERNEL_TRACER_VERSION
:
333 struct lttng_kernel_tracer_version version
;
334 struct lttng_kernel_tracer_version
*uversion
=
335 (struct lttng_kernel_tracer_version __user
*) arg
;
337 lttng_abi_tracer_version(&version
);
339 if (copy_to_user(uversion
, &version
, sizeof(version
)))
343 case LTTNG_KERNEL_TRACER_ABI_VERSION
:
345 struct lttng_kernel_tracer_abi_version version
;
346 struct lttng_kernel_tracer_abi_version
*uversion
=
347 (struct lttng_kernel_tracer_abi_version __user
*) arg
;
349 lttng_abi_tracer_abi_version(&version
);
351 if (copy_to_user(uversion
, &version
, sizeof(version
)))
355 case LTTNG_KERNEL_OLD_TRACEPOINT_LIST
:
356 case LTTNG_KERNEL_TRACEPOINT_LIST
:
357 return lttng_abi_tracepoint_list();
358 case LTTNG_KERNEL_SYSCALL_LIST
:
359 return lttng_abi_syscall_list();
360 case LTTNG_KERNEL_OLD_WAIT_QUIESCENT
:
361 case LTTNG_KERNEL_WAIT_QUIESCENT
:
364 case LTTNG_KERNEL_OLD_CALIBRATE
:
366 struct lttng_kernel_old_calibrate __user
*ucalibrate
=
367 (struct lttng_kernel_old_calibrate __user
*) arg
;
368 struct lttng_kernel_old_calibrate old_calibrate
;
369 struct lttng_kernel_calibrate calibrate
;
372 if (copy_from_user(&old_calibrate
, ucalibrate
, sizeof(old_calibrate
)))
374 calibrate
.type
= old_calibrate
.type
;
375 ret
= lttng_calibrate(&calibrate
);
376 if (copy_to_user(ucalibrate
, &old_calibrate
, sizeof(old_calibrate
)))
380 case LTTNG_KERNEL_CALIBRATE
:
382 struct lttng_kernel_calibrate __user
*ucalibrate
=
383 (struct lttng_kernel_calibrate __user
*) arg
;
384 struct lttng_kernel_calibrate calibrate
;
387 if (copy_from_user(&calibrate
, ucalibrate
, sizeof(calibrate
)))
389 ret
= lttng_calibrate(&calibrate
);
390 if (copy_to_user(ucalibrate
, &calibrate
, sizeof(calibrate
)))
399 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(5,6,0))
400 static const struct proc_ops lttng_proc_ops
= {
401 .proc_ioctl
= lttng_ioctl
,
403 .proc_compat_ioctl
= lttng_ioctl
,
404 #endif /* CONFIG_COMPAT */
407 static const struct file_operations lttng_proc_ops
= {
408 .owner
= THIS_MODULE
,
409 .unlocked_ioctl
= lttng_ioctl
,
411 .compat_ioctl
= lttng_ioctl
,
412 #endif /* CONFIG_COMPAT */
417 int lttng_abi_create_channel(struct file
*session_file
,
418 struct lttng_kernel_channel
*chan_param
,
419 enum channel_type channel_type
)
421 struct lttng_session
*session
= session_file
->private_data
;
422 const struct file_operations
*fops
= NULL
;
423 const char *transport_name
;
424 struct lttng_channel
*chan
;
425 struct file
*chan_file
;
429 chan_fd
= lttng_get_unused_fd();
434 switch (channel_type
) {
435 case PER_CPU_CHANNEL
:
436 fops
= <tng_channel_fops
;
438 case METADATA_CHANNEL
:
439 fops
= <tng_metadata_fops
;
443 chan_file
= anon_inode_getfile("[lttng_channel]",
446 if (IS_ERR(chan_file
)) {
447 ret
= PTR_ERR(chan_file
);
450 switch (channel_type
) {
451 case PER_CPU_CHANNEL
:
452 if (chan_param
->output
== LTTNG_KERNEL_SPLICE
) {
453 transport_name
= chan_param
->overwrite
?
454 "relay-overwrite" : "relay-discard";
455 } else if (chan_param
->output
== LTTNG_KERNEL_MMAP
) {
456 transport_name
= chan_param
->overwrite
?
457 "relay-overwrite-mmap" : "relay-discard-mmap";
462 case METADATA_CHANNEL
:
463 if (chan_param
->output
== LTTNG_KERNEL_SPLICE
)
464 transport_name
= "relay-metadata";
465 else if (chan_param
->output
== LTTNG_KERNEL_MMAP
)
466 transport_name
= "relay-metadata-mmap";
471 transport_name
= "<unknown>";
474 if (!atomic_long_add_unless(&session_file
->f_count
, 1, LONG_MAX
)) {
479 * We tolerate no failure path after channel creation. It will stay
480 * invariant for the rest of the session.
482 chan
= lttng_channel_create(session
, transport_name
, NULL
,
483 chan_param
->subbuf_size
,
484 chan_param
->num_subbuf
,
485 chan_param
->switch_timer_interval
,
486 chan_param
->read_timer_interval
,
492 chan
->file
= chan_file
;
493 chan_file
->private_data
= chan
;
494 fd_install(chan_fd
, chan_file
);
499 atomic_long_dec(&session_file
->f_count
);
503 put_unused_fd(chan_fd
);
509 int lttng_abi_session_set_name(struct lttng_session
*session
,
510 struct lttng_kernel_session_name
*name
)
514 len
= strnlen(name
->name
, LTTNG_KERNEL_SESSION_NAME_LEN
);
516 if (len
== LTTNG_KERNEL_SESSION_NAME_LEN
) {
517 /* Name is too long/malformed */
521 strcpy(session
->name
, name
->name
);
526 int lttng_abi_session_set_creation_time(struct lttng_session
*session
,
527 struct lttng_kernel_session_creation_time
*time
)
531 len
= strnlen(time
->iso8601
, LTTNG_KERNEL_SESSION_CREATION_TIME_ISO8601_LEN
);
533 if (len
== LTTNG_KERNEL_SESSION_CREATION_TIME_ISO8601_LEN
) {
534 /* Time is too long/malformed */
538 strcpy(session
->creation_time
, time
->iso8601
);
543 enum tracker_type
get_tracker_type(struct lttng_kernel_tracker_args
*tracker
)
545 switch (tracker
->type
) {
546 case LTTNG_KERNEL_TRACKER_PID
:
548 case LTTNG_KERNEL_TRACKER_VPID
:
550 case LTTNG_KERNEL_TRACKER_UID
:
552 case LTTNG_KERNEL_TRACKER_VUID
:
554 case LTTNG_KERNEL_TRACKER_GID
:
556 case LTTNG_KERNEL_TRACKER_VGID
:
559 return TRACKER_UNKNOWN
;
564 * lttng_session_ioctl - lttng session fd ioctl
570 * This ioctl implements lttng commands:
571 * LTTNG_KERNEL_CHANNEL
572 * Returns a LTTng channel file descriptor
573 * LTTNG_KERNEL_ENABLE
574 * Enables tracing for a session (weak enable)
575 * LTTNG_KERNEL_DISABLE
576 * Disables tracing for a session (strong disable)
577 * LTTNG_KERNEL_METADATA
578 * Returns a LTTng metadata file descriptor
579 * LTTNG_KERNEL_SESSION_TRACK_PID
580 * Add PID to session PID tracker
581 * LTTNG_KERNEL_SESSION_UNTRACK_PID
582 * Remove PID from session PID tracker
583 * LTTNG_KERNEL_SESSION_TRACK_ID
585 * LTTNG_KERNEL_SESSION_UNTRACK_ID
586 * Remove ID from tracker
588 * The returned channel will be deleted when its file descriptor is closed.
591 long lttng_session_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
593 struct lttng_session
*session
= file
->private_data
;
594 struct lttng_kernel_channel chan_param
;
595 struct lttng_kernel_old_channel old_chan_param
;
598 case LTTNG_KERNEL_OLD_CHANNEL
:
600 if (copy_from_user(&old_chan_param
,
601 (struct lttng_kernel_old_channel __user
*) arg
,
602 sizeof(struct lttng_kernel_old_channel
)))
604 chan_param
.overwrite
= old_chan_param
.overwrite
;
605 chan_param
.subbuf_size
= old_chan_param
.subbuf_size
;
606 chan_param
.num_subbuf
= old_chan_param
.num_subbuf
;
607 chan_param
.switch_timer_interval
= old_chan_param
.switch_timer_interval
;
608 chan_param
.read_timer_interval
= old_chan_param
.read_timer_interval
;
609 chan_param
.output
= old_chan_param
.output
;
611 return lttng_abi_create_channel(file
, &chan_param
,
614 case LTTNG_KERNEL_CHANNEL
:
616 if (copy_from_user(&chan_param
,
617 (struct lttng_kernel_channel __user
*) arg
,
618 sizeof(struct lttng_kernel_channel
)))
620 return lttng_abi_create_channel(file
, &chan_param
,
623 case LTTNG_KERNEL_OLD_SESSION_START
:
624 case LTTNG_KERNEL_OLD_ENABLE
:
625 case LTTNG_KERNEL_SESSION_START
:
626 case LTTNG_KERNEL_ENABLE
:
627 return lttng_session_enable(session
);
628 case LTTNG_KERNEL_OLD_SESSION_STOP
:
629 case LTTNG_KERNEL_OLD_DISABLE
:
630 case LTTNG_KERNEL_SESSION_STOP
:
631 case LTTNG_KERNEL_DISABLE
:
632 return lttng_session_disable(session
);
633 case LTTNG_KERNEL_OLD_METADATA
:
635 if (copy_from_user(&old_chan_param
,
636 (struct lttng_kernel_old_channel __user
*) arg
,
637 sizeof(struct lttng_kernel_old_channel
)))
639 chan_param
.overwrite
= old_chan_param
.overwrite
;
640 chan_param
.subbuf_size
= old_chan_param
.subbuf_size
;
641 chan_param
.num_subbuf
= old_chan_param
.num_subbuf
;
642 chan_param
.switch_timer_interval
= old_chan_param
.switch_timer_interval
;
643 chan_param
.read_timer_interval
= old_chan_param
.read_timer_interval
;
644 chan_param
.output
= old_chan_param
.output
;
646 return lttng_abi_create_channel(file
, &chan_param
,
649 case LTTNG_KERNEL_METADATA
:
651 if (copy_from_user(&chan_param
,
652 (struct lttng_kernel_channel __user
*) arg
,
653 sizeof(struct lttng_kernel_channel
)))
655 return lttng_abi_create_channel(file
, &chan_param
,
658 case LTTNG_KERNEL_SESSION_TRACK_PID
:
659 return lttng_session_track_id(session
, TRACKER_PID
, (int) arg
);
660 case LTTNG_KERNEL_SESSION_UNTRACK_PID
:
661 return lttng_session_untrack_id(session
, TRACKER_PID
, (int) arg
);
662 case LTTNG_KERNEL_SESSION_TRACK_ID
:
664 struct lttng_kernel_tracker_args tracker
;
665 enum tracker_type tracker_type
;
667 if (copy_from_user(&tracker
,
668 (struct lttng_kernel_tracker_args __user
*) arg
,
669 sizeof(struct lttng_kernel_tracker_args
)))
671 tracker_type
= get_tracker_type(&tracker
);
672 if (tracker_type
== TRACKER_UNKNOWN
)
674 return lttng_session_track_id(session
, tracker_type
, tracker
.id
);
676 case LTTNG_KERNEL_SESSION_UNTRACK_ID
:
678 struct lttng_kernel_tracker_args tracker
;
679 enum tracker_type tracker_type
;
681 if (copy_from_user(&tracker
,
682 (struct lttng_kernel_tracker_args __user
*) arg
,
683 sizeof(struct lttng_kernel_tracker_args
)))
685 tracker_type
= get_tracker_type(&tracker
);
686 if (tracker_type
== TRACKER_UNKNOWN
)
688 return lttng_session_untrack_id(session
, tracker_type
,
691 case LTTNG_KERNEL_SESSION_LIST_TRACKER_PIDS
:
692 return lttng_session_list_tracker_ids(session
, TRACKER_PID
);
693 case LTTNG_KERNEL_SESSION_LIST_TRACKER_IDS
:
695 struct lttng_kernel_tracker_args tracker
;
696 enum tracker_type tracker_type
;
698 if (copy_from_user(&tracker
,
699 (struct lttng_kernel_tracker_args __user
*) arg
,
700 sizeof(struct lttng_kernel_tracker_args
)))
702 tracker_type
= get_tracker_type(&tracker
);
703 if (tracker_type
== TRACKER_UNKNOWN
)
705 return lttng_session_list_tracker_ids(session
, tracker_type
);
707 case LTTNG_KERNEL_SESSION_METADATA_REGEN
:
708 return lttng_session_metadata_regenerate(session
);
709 case LTTNG_KERNEL_SESSION_STATEDUMP
:
710 return lttng_session_statedump(session
);
711 case LTTNG_KERNEL_SESSION_SET_NAME
:
713 struct lttng_kernel_session_name name
;
715 if (copy_from_user(&name
,
716 (struct lttng_kernel_session_name __user
*) arg
,
717 sizeof(struct lttng_kernel_session_name
)))
719 return lttng_abi_session_set_name(session
, &name
);
721 case LTTNG_KERNEL_SESSION_SET_CREATION_TIME
:
723 struct lttng_kernel_session_creation_time time
;
725 if (copy_from_user(&time
,
726 (struct lttng_kernel_session_creation_time __user
*) arg
,
727 sizeof(struct lttng_kernel_session_creation_time
)))
729 return lttng_abi_session_set_creation_time(session
, &time
);
737 * Called when the last file reference is dropped.
739 * Big fat note: channels and events are invariant for the whole session after
740 * their creation. So this session destruction also destroys all channel and
741 * event structures specific to this session (they are not destroyed when their
742 * individual file is released).
745 int lttng_session_release(struct inode
*inode
, struct file
*file
)
747 struct lttng_session
*session
= file
->private_data
;
750 lttng_session_destroy(session
);
754 static const struct file_operations lttng_session_fops
= {
755 .owner
= THIS_MODULE
,
756 .release
= lttng_session_release
,
757 .unlocked_ioctl
= lttng_session_ioctl
,
759 .compat_ioctl
= lttng_session_ioctl
,
764 * lttng_metadata_ring_buffer_poll - LTTng ring buffer poll file operation
768 * Handles the poll operations for the metadata channels.
771 unsigned int lttng_metadata_ring_buffer_poll(struct file
*filp
,
774 struct lttng_metadata_stream
*stream
= filp
->private_data
;
775 struct lib_ring_buffer
*buf
= stream
->priv
;
777 unsigned int mask
= 0;
779 if (filp
->f_mode
& FMODE_READ
) {
780 poll_wait_set_exclusive(wait
);
781 poll_wait(filp
, &stream
->read_wait
, wait
);
783 finalized
= stream
->finalized
;
786 * lib_ring_buffer_is_finalized() contains a smp_rmb()
787 * ordering finalized load before offsets loads.
789 WARN_ON(atomic_long_read(&buf
->active_readers
) != 1);
794 mutex_lock(&stream
->metadata_cache
->lock
);
795 if (stream
->metadata_cache
->metadata_written
>
796 stream
->metadata_out
)
798 mutex_unlock(&stream
->metadata_cache
->lock
);
805 void lttng_metadata_ring_buffer_ioctl_put_next_subbuf(struct file
*filp
,
806 unsigned int cmd
, unsigned long arg
)
808 struct lttng_metadata_stream
*stream
= filp
->private_data
;
810 stream
->metadata_out
= stream
->metadata_in
;
814 * Reset the counter of how much metadata has been consumed to 0. That way,
815 * the consumer receives the content of the metadata cache unchanged. This is
816 * different from the metadata_regenerate where the offset from epoch is
817 * resampled, here we want the exact same content as the last time the metadata
818 * was generated. This command is only possible if all the metadata written
819 * in the cache has been output to the metadata stream to avoid corrupting the
822 * Return 0 on success, a negative value on error.
825 int lttng_metadata_cache_dump(struct lttng_metadata_stream
*stream
)
828 struct lttng_metadata_cache
*cache
= stream
->metadata_cache
;
830 mutex_lock(&cache
->lock
);
831 if (stream
->metadata_out
!= cache
->metadata_written
) {
835 stream
->metadata_out
= 0;
836 stream
->metadata_in
= 0;
837 wake_up_interruptible(&stream
->read_wait
);
841 mutex_unlock(&cache
->lock
);
846 long lttng_metadata_ring_buffer_ioctl(struct file
*filp
,
847 unsigned int cmd
, unsigned long arg
)
850 struct lttng_metadata_stream
*stream
= filp
->private_data
;
851 struct lib_ring_buffer
*buf
= stream
->priv
;
854 case RING_BUFFER_GET_NEXT_SUBBUF
:
856 struct lttng_metadata_stream
*stream
= filp
->private_data
;
857 struct lib_ring_buffer
*buf
= stream
->priv
;
858 struct channel
*chan
= buf
->backend
.chan
;
860 ret
= lttng_metadata_output_channel(stream
, chan
);
862 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
868 case RING_BUFFER_GET_SUBBUF
:
871 * Random access is not allowed for metadata channel.
875 case RING_BUFFER_FLUSH_EMPTY
: /* Fall-through. */
876 case RING_BUFFER_FLUSH
:
878 struct lttng_metadata_stream
*stream
= filp
->private_data
;
879 struct lib_ring_buffer
*buf
= stream
->priv
;
880 struct channel
*chan
= buf
->backend
.chan
;
883 * Before doing the actual ring buffer flush, write up to one
884 * packet of metadata in the ring buffer.
886 ret
= lttng_metadata_output_channel(stream
, chan
);
891 case RING_BUFFER_GET_METADATA_VERSION
:
893 struct lttng_metadata_stream
*stream
= filp
->private_data
;
895 return put_u64(stream
->version
, arg
);
897 case RING_BUFFER_METADATA_CACHE_DUMP
:
899 struct lttng_metadata_stream
*stream
= filp
->private_data
;
901 return lttng_metadata_cache_dump(stream
);
906 /* PUT_SUBBUF is the one from lib ring buffer, unmodified. */
908 /* Performing lib ring buffer ioctl after our own. */
909 ret
= lib_ring_buffer_ioctl(filp
, cmd
, arg
, buf
);
914 case RING_BUFFER_PUT_NEXT_SUBBUF
:
916 lttng_metadata_ring_buffer_ioctl_put_next_subbuf(filp
,
929 long lttng_metadata_ring_buffer_compat_ioctl(struct file
*filp
,
930 unsigned int cmd
, unsigned long arg
)
933 struct lttng_metadata_stream
*stream
= filp
->private_data
;
934 struct lib_ring_buffer
*buf
= stream
->priv
;
937 case RING_BUFFER_GET_NEXT_SUBBUF
:
939 struct lttng_metadata_stream
*stream
= filp
->private_data
;
940 struct lib_ring_buffer
*buf
= stream
->priv
;
941 struct channel
*chan
= buf
->backend
.chan
;
943 ret
= lttng_metadata_output_channel(stream
, chan
);
945 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
951 case RING_BUFFER_GET_SUBBUF
:
954 * Random access is not allowed for metadata channel.
958 case RING_BUFFER_FLUSH_EMPTY
: /* Fall-through. */
959 case RING_BUFFER_FLUSH
:
961 struct lttng_metadata_stream
*stream
= filp
->private_data
;
962 struct lib_ring_buffer
*buf
= stream
->priv
;
963 struct channel
*chan
= buf
->backend
.chan
;
966 * Before doing the actual ring buffer flush, write up to one
967 * packet of metadata in the ring buffer.
969 ret
= lttng_metadata_output_channel(stream
, chan
);
974 case RING_BUFFER_GET_METADATA_VERSION
:
976 struct lttng_metadata_stream
*stream
= filp
->private_data
;
978 return put_u64(stream
->version
, arg
);
980 case RING_BUFFER_METADATA_CACHE_DUMP
:
982 struct lttng_metadata_stream
*stream
= filp
->private_data
;
984 return lttng_metadata_cache_dump(stream
);
989 /* PUT_SUBBUF is the one from lib ring buffer, unmodified. */
991 /* Performing lib ring buffer ioctl after our own. */
992 ret
= lib_ring_buffer_compat_ioctl(filp
, cmd
, arg
, buf
);
997 case RING_BUFFER_PUT_NEXT_SUBBUF
:
999 lttng_metadata_ring_buffer_ioctl_put_next_subbuf(filp
,
1012 * This is not used by anonymous file descriptors. This code is left
1013 * there if we ever want to implement an inode with open() operation.
1016 int lttng_metadata_ring_buffer_open(struct inode
*inode
, struct file
*file
)
1018 struct lttng_metadata_stream
*stream
= inode
->i_private
;
1019 struct lib_ring_buffer
*buf
= stream
->priv
;
1021 file
->private_data
= buf
;
1023 * Since life-time of metadata cache differs from that of
1024 * session, we need to keep our own reference on the transport.
1026 if (!try_module_get(stream
->transport
->owner
)) {
1027 printk(KERN_WARNING
"LTT : Can't lock transport module.\n");
1030 return lib_ring_buffer_open(inode
, file
, buf
);
1034 int lttng_metadata_ring_buffer_release(struct inode
*inode
, struct file
*file
)
1036 struct lttng_metadata_stream
*stream
= file
->private_data
;
1037 struct lib_ring_buffer
*buf
= stream
->priv
;
1039 kref_put(&stream
->metadata_cache
->refcount
, metadata_cache_destroy
);
1040 module_put(stream
->transport
->owner
);
1041 return lib_ring_buffer_release(inode
, file
, buf
);
1045 ssize_t
lttng_metadata_ring_buffer_splice_read(struct file
*in
, loff_t
*ppos
,
1046 struct pipe_inode_info
*pipe
, size_t len
,
1049 struct lttng_metadata_stream
*stream
= in
->private_data
;
1050 struct lib_ring_buffer
*buf
= stream
->priv
;
1052 return lib_ring_buffer_splice_read(in
, ppos
, pipe
, len
,
1057 int lttng_metadata_ring_buffer_mmap(struct file
*filp
,
1058 struct vm_area_struct
*vma
)
1060 struct lttng_metadata_stream
*stream
= filp
->private_data
;
1061 struct lib_ring_buffer
*buf
= stream
->priv
;
1063 return lib_ring_buffer_mmap(filp
, vma
, buf
);
1067 const struct file_operations lttng_metadata_ring_buffer_file_operations
= {
1068 .owner
= THIS_MODULE
,
1069 .open
= lttng_metadata_ring_buffer_open
,
1070 .release
= lttng_metadata_ring_buffer_release
,
1071 .poll
= lttng_metadata_ring_buffer_poll
,
1072 .splice_read
= lttng_metadata_ring_buffer_splice_read
,
1073 .mmap
= lttng_metadata_ring_buffer_mmap
,
1074 .unlocked_ioctl
= lttng_metadata_ring_buffer_ioctl
,
1075 .llseek
= vfs_lib_ring_buffer_no_llseek
,
1076 #ifdef CONFIG_COMPAT
1077 .compat_ioctl
= lttng_metadata_ring_buffer_compat_ioctl
,
1082 int lttng_abi_create_stream_fd(struct file
*channel_file
, void *stream_priv
,
1083 const struct file_operations
*fops
)
1086 struct file
*stream_file
;
1088 stream_fd
= lttng_get_unused_fd();
1089 if (stream_fd
< 0) {
1093 stream_file
= anon_inode_getfile("[lttng_stream]", fops
,
1094 stream_priv
, O_RDWR
);
1095 if (IS_ERR(stream_file
)) {
1096 ret
= PTR_ERR(stream_file
);
1100 * OPEN_FMODE, called within anon_inode_getfile/alloc_file, don't honor
1101 * FMODE_LSEEK, FMODE_PREAD nor FMODE_PWRITE. We need to read from this
1102 * file descriptor, so we set FMODE_PREAD here.
1104 stream_file
->f_mode
|= FMODE_PREAD
;
1105 fd_install(stream_fd
, stream_file
);
1107 * The stream holds a reference to the channel within the generic ring
1108 * buffer library, so no need to hold a refcount on the channel and
1109 * session files here.
1114 put_unused_fd(stream_fd
);
1120 int lttng_abi_open_stream(struct file
*channel_file
)
1122 struct lttng_channel
*channel
= channel_file
->private_data
;
1123 struct lib_ring_buffer
*buf
;
1127 buf
= channel
->ops
->buffer_read_open(channel
->chan
);
1132 ret
= lttng_abi_create_stream_fd(channel_file
, stream_priv
,
1133 <tng_stream_ring_buffer_file_operations
);
1140 channel
->ops
->buffer_read_close(buf
);
1145 int lttng_abi_open_metadata_stream(struct file
*channel_file
)
1147 struct lttng_channel
*channel
= channel_file
->private_data
;
1148 struct lttng_session
*session
= channel
->session
;
1149 struct lib_ring_buffer
*buf
;
1151 struct lttng_metadata_stream
*metadata_stream
;
1154 buf
= channel
->ops
->buffer_read_open(channel
->chan
);
1158 metadata_stream
= kzalloc(sizeof(struct lttng_metadata_stream
),
1160 if (!metadata_stream
) {
1164 metadata_stream
->metadata_cache
= session
->metadata_cache
;
1165 init_waitqueue_head(&metadata_stream
->read_wait
);
1166 metadata_stream
->priv
= buf
;
1167 stream_priv
= metadata_stream
;
1168 metadata_stream
->transport
= channel
->transport
;
1171 * Since life-time of metadata cache differs from that of
1172 * session, we need to keep our own reference on the transport.
1174 if (!try_module_get(metadata_stream
->transport
->owner
)) {
1175 printk(KERN_WARNING
"LTT : Can't lock transport module.\n");
1180 kref_get(&session
->metadata_cache
->refcount
);
1181 ret
= lttng_abi_create_stream_fd(channel_file
, stream_priv
,
1182 <tng_metadata_ring_buffer_file_operations
);
1186 list_add(&metadata_stream
->list
,
1187 &session
->metadata_cache
->metadata_stream
);
1191 kref_put(&session
->metadata_cache
->refcount
, metadata_cache_destroy
);
1192 module_put(metadata_stream
->transport
->owner
);
1194 kfree(metadata_stream
);
1196 channel
->ops
->buffer_read_close(buf
);
1201 int lttng_abi_create_event(struct file
*channel_file
,
1202 struct lttng_kernel_event
*event_param
)
1204 struct lttng_channel
*channel
= channel_file
->private_data
;
1206 struct file
*event_file
;
1209 event_param
->name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
1210 switch (event_param
->instrumentation
) {
1211 case LTTNG_KERNEL_KRETPROBE
:
1212 event_param
->u
.kretprobe
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
1214 case LTTNG_KERNEL_KPROBE
:
1215 event_param
->u
.kprobe
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
1217 case LTTNG_KERNEL_FUNCTION
:
1219 /* Not implemented. */
1224 event_fd
= lttng_get_unused_fd();
1229 event_file
= anon_inode_getfile("[lttng_event]",
1232 if (IS_ERR(event_file
)) {
1233 ret
= PTR_ERR(event_file
);
1236 /* The event holds a reference on the channel */
1237 if (!atomic_long_add_unless(&channel_file
->f_count
, 1, LONG_MAX
)) {
1239 goto refcount_error
;
1241 if (event_param
->instrumentation
== LTTNG_KERNEL_TRACEPOINT
1242 || event_param
->instrumentation
== LTTNG_KERNEL_SYSCALL
) {
1243 struct lttng_enabler
*enabler
;
1245 if (strutils_is_star_glob_pattern(event_param
->name
)) {
1247 * If the event name is a star globbing pattern,
1248 * we create the special star globbing enabler.
1250 enabler
= lttng_enabler_create(LTTNG_ENABLER_STAR_GLOB
,
1251 event_param
, channel
);
1253 enabler
= lttng_enabler_create(LTTNG_ENABLER_NAME
,
1254 event_param
, channel
);
1258 struct lttng_event
*event
;
1261 * We tolerate no failure path after event creation. It
1262 * will stay invariant for the rest of the session.
1264 event
= lttng_event_create(channel
, event_param
,
1266 event_param
->instrumentation
);
1267 WARN_ON_ONCE(!event
);
1268 if (IS_ERR(event
)) {
1269 ret
= PTR_ERR(event
);
1274 event_file
->private_data
= priv
;
1275 fd_install(event_fd
, event_file
);
1279 atomic_long_dec(&channel_file
->f_count
);
1283 put_unused_fd(event_fd
);
1289 * lttng_channel_ioctl - lttng syscall through ioctl
1295 * This ioctl implements lttng commands:
1296 * LTTNG_KERNEL_STREAM
1297 * Returns an event stream file descriptor or failure.
1298 * (typically, one event stream records events from one CPU)
1299 * LTTNG_KERNEL_EVENT
1300 * Returns an event file descriptor or failure.
1301 * LTTNG_KERNEL_CONTEXT
1302 * Prepend a context field to each event in the channel
1303 * LTTNG_KERNEL_ENABLE
1304 * Enable recording for events in this channel (weak enable)
1305 * LTTNG_KERNEL_DISABLE
1306 * Disable recording for events in this channel (strong disable)
1308 * Channel and event file descriptors also hold a reference on the session.
1311 long lttng_channel_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1313 struct lttng_channel
*channel
= file
->private_data
;
1316 case LTTNG_KERNEL_OLD_STREAM
:
1317 case LTTNG_KERNEL_STREAM
:
1318 return lttng_abi_open_stream(file
);
1319 case LTTNG_KERNEL_OLD_EVENT
:
1321 struct lttng_kernel_event
*uevent_param
;
1322 struct lttng_kernel_old_event
*old_uevent_param
;
1325 uevent_param
= kmalloc(sizeof(struct lttng_kernel_event
),
1327 if (!uevent_param
) {
1331 old_uevent_param
= kmalloc(
1332 sizeof(struct lttng_kernel_old_event
),
1334 if (!old_uevent_param
) {
1336 goto old_event_error_free_param
;
1338 if (copy_from_user(old_uevent_param
,
1339 (struct lttng_kernel_old_event __user
*) arg
,
1340 sizeof(struct lttng_kernel_old_event
))) {
1342 goto old_event_error_free_old_param
;
1345 memcpy(uevent_param
->name
, old_uevent_param
->name
,
1346 sizeof(uevent_param
->name
));
1347 uevent_param
->instrumentation
=
1348 old_uevent_param
->instrumentation
;
1350 switch (old_uevent_param
->instrumentation
) {
1351 case LTTNG_KERNEL_KPROBE
:
1352 uevent_param
->u
.kprobe
.addr
=
1353 old_uevent_param
->u
.kprobe
.addr
;
1354 uevent_param
->u
.kprobe
.offset
=
1355 old_uevent_param
->u
.kprobe
.offset
;
1356 memcpy(uevent_param
->u
.kprobe
.symbol_name
,
1357 old_uevent_param
->u
.kprobe
.symbol_name
,
1358 sizeof(uevent_param
->u
.kprobe
.symbol_name
));
1360 case LTTNG_KERNEL_KRETPROBE
:
1361 uevent_param
->u
.kretprobe
.addr
=
1362 old_uevent_param
->u
.kretprobe
.addr
;
1363 uevent_param
->u
.kretprobe
.offset
=
1364 old_uevent_param
->u
.kretprobe
.offset
;
1365 memcpy(uevent_param
->u
.kretprobe
.symbol_name
,
1366 old_uevent_param
->u
.kretprobe
.symbol_name
,
1367 sizeof(uevent_param
->u
.kretprobe
.symbol_name
));
1369 case LTTNG_KERNEL_FUNCTION
:
1371 /* Not implemented. */
1376 ret
= lttng_abi_create_event(file
, uevent_param
);
1378 old_event_error_free_old_param
:
1379 kfree(old_uevent_param
);
1380 old_event_error_free_param
:
1381 kfree(uevent_param
);
1385 case LTTNG_KERNEL_EVENT
:
1387 struct lttng_kernel_event uevent_param
;
1389 if (copy_from_user(&uevent_param
,
1390 (struct lttng_kernel_event __user
*) arg
,
1391 sizeof(uevent_param
)))
1393 return lttng_abi_create_event(file
, &uevent_param
);
1395 case LTTNG_KERNEL_OLD_CONTEXT
:
1397 struct lttng_kernel_context
*ucontext_param
;
1398 struct lttng_kernel_old_context
*old_ucontext_param
;
1401 ucontext_param
= kmalloc(sizeof(struct lttng_kernel_context
),
1403 if (!ucontext_param
) {
1407 old_ucontext_param
= kmalloc(sizeof(struct lttng_kernel_old_context
),
1409 if (!old_ucontext_param
) {
1411 goto old_ctx_error_free_param
;
1414 if (copy_from_user(old_ucontext_param
,
1415 (struct lttng_kernel_old_context __user
*) arg
,
1416 sizeof(struct lttng_kernel_old_context
))) {
1418 goto old_ctx_error_free_old_param
;
1420 ucontext_param
->ctx
= old_ucontext_param
->ctx
;
1421 memcpy(ucontext_param
->padding
, old_ucontext_param
->padding
,
1422 sizeof(ucontext_param
->padding
));
1423 /* only type that uses the union */
1424 if (old_ucontext_param
->ctx
== LTTNG_KERNEL_CONTEXT_PERF_COUNTER
) {
1425 ucontext_param
->u
.perf_counter
.type
=
1426 old_ucontext_param
->u
.perf_counter
.type
;
1427 ucontext_param
->u
.perf_counter
.config
=
1428 old_ucontext_param
->u
.perf_counter
.config
;
1429 memcpy(ucontext_param
->u
.perf_counter
.name
,
1430 old_ucontext_param
->u
.perf_counter
.name
,
1431 sizeof(ucontext_param
->u
.perf_counter
.name
));
1434 ret
= lttng_abi_add_context(file
,
1436 &channel
->ctx
, channel
->session
);
1438 old_ctx_error_free_old_param
:
1439 kfree(old_ucontext_param
);
1440 old_ctx_error_free_param
:
1441 kfree(ucontext_param
);
1445 case LTTNG_KERNEL_CONTEXT
:
1447 struct lttng_kernel_context ucontext_param
;
1449 if (copy_from_user(&ucontext_param
,
1450 (struct lttng_kernel_context __user
*) arg
,
1451 sizeof(ucontext_param
)))
1453 return lttng_abi_add_context(file
,
1455 &channel
->ctx
, channel
->session
);
1457 case LTTNG_KERNEL_OLD_ENABLE
:
1458 case LTTNG_KERNEL_ENABLE
:
1459 return lttng_channel_enable(channel
);
1460 case LTTNG_KERNEL_OLD_DISABLE
:
1461 case LTTNG_KERNEL_DISABLE
:
1462 return lttng_channel_disable(channel
);
1463 case LTTNG_KERNEL_SYSCALL_MASK
:
1464 return lttng_channel_syscall_mask(channel
,
1465 (struct lttng_kernel_syscall_mask __user
*) arg
);
1467 return -ENOIOCTLCMD
;
1472 * lttng_metadata_ioctl - lttng syscall through ioctl
1478 * This ioctl implements lttng commands:
1479 * LTTNG_KERNEL_STREAM
1480 * Returns an event stream file descriptor or failure.
1482 * Channel and event file descriptors also hold a reference on the session.
1485 long lttng_metadata_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1488 case LTTNG_KERNEL_OLD_STREAM
:
1489 case LTTNG_KERNEL_STREAM
:
1490 return lttng_abi_open_metadata_stream(file
);
1492 return -ENOIOCTLCMD
;
1497 * lttng_channel_poll - lttng stream addition/removal monitoring
1502 unsigned int lttng_channel_poll(struct file
*file
, poll_table
*wait
)
1504 struct lttng_channel
*channel
= file
->private_data
;
1505 unsigned int mask
= 0;
1507 if (file
->f_mode
& FMODE_READ
) {
1508 poll_wait_set_exclusive(wait
);
1509 poll_wait(file
, channel
->ops
->get_hp_wait_queue(channel
->chan
),
1512 if (channel
->ops
->is_disabled(channel
->chan
))
1514 if (channel
->ops
->is_finalized(channel
->chan
))
1516 if (channel
->ops
->buffer_has_read_closed_stream(channel
->chan
))
1517 return POLLIN
| POLLRDNORM
;
1525 int lttng_channel_release(struct inode
*inode
, struct file
*file
)
1527 struct lttng_channel
*channel
= file
->private_data
;
1530 fput(channel
->session
->file
);
1535 int lttng_metadata_channel_release(struct inode
*inode
, struct file
*file
)
1537 struct lttng_channel
*channel
= file
->private_data
;
1540 fput(channel
->session
->file
);
1541 lttng_metadata_channel_destroy(channel
);
1547 static const struct file_operations lttng_channel_fops
= {
1548 .owner
= THIS_MODULE
,
1549 .release
= lttng_channel_release
,
1550 .poll
= lttng_channel_poll
,
1551 .unlocked_ioctl
= lttng_channel_ioctl
,
1552 #ifdef CONFIG_COMPAT
1553 .compat_ioctl
= lttng_channel_ioctl
,
1557 static const struct file_operations lttng_metadata_fops
= {
1558 .owner
= THIS_MODULE
,
1559 .release
= lttng_metadata_channel_release
,
1560 .unlocked_ioctl
= lttng_metadata_ioctl
,
1561 #ifdef CONFIG_COMPAT
1562 .compat_ioctl
= lttng_metadata_ioctl
,
1567 * lttng_event_ioctl - lttng syscall through ioctl
1573 * This ioctl implements lttng commands:
1574 * LTTNG_KERNEL_CONTEXT
1575 * Prepend a context field to each record of this event
1576 * LTTNG_KERNEL_ENABLE
1577 * Enable recording for this event (weak enable)
1578 * LTTNG_KERNEL_DISABLE
1579 * Disable recording for this event (strong disable)
1582 long lttng_event_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1584 struct lttng_event
*event
;
1585 struct lttng_enabler
*enabler
;
1586 enum lttng_event_type
*evtype
= file
->private_data
;
1589 case LTTNG_KERNEL_OLD_CONTEXT
:
1591 /* Not implemented */
1594 case LTTNG_KERNEL_CONTEXT
:
1596 /* Not implemented */
1599 case LTTNG_KERNEL_OLD_ENABLE
:
1600 case LTTNG_KERNEL_ENABLE
:
1602 case LTTNG_TYPE_EVENT
:
1603 event
= file
->private_data
;
1604 return lttng_event_enable(event
);
1605 case LTTNG_TYPE_ENABLER
:
1606 enabler
= file
->private_data
;
1607 return lttng_enabler_enable(enabler
);
1612 case LTTNG_KERNEL_OLD_DISABLE
:
1613 case LTTNG_KERNEL_DISABLE
:
1615 case LTTNG_TYPE_EVENT
:
1616 event
= file
->private_data
;
1617 return lttng_event_disable(event
);
1618 case LTTNG_TYPE_ENABLER
:
1619 enabler
= file
->private_data
;
1620 return lttng_enabler_disable(enabler
);
1625 case LTTNG_KERNEL_FILTER
:
1627 case LTTNG_TYPE_EVENT
:
1629 case LTTNG_TYPE_ENABLER
:
1631 enabler
= file
->private_data
;
1632 return lttng_enabler_attach_bytecode(enabler
,
1633 (struct lttng_kernel_filter_bytecode __user
*) arg
);
1639 case LTTNG_KERNEL_ADD_CALLSITE
:
1641 case LTTNG_TYPE_EVENT
:
1642 event
= file
->private_data
;
1643 return lttng_event_add_callsite(event
,
1644 (struct lttng_kernel_event_callsite __user
*) arg
);
1645 case LTTNG_TYPE_ENABLER
:
1652 return -ENOIOCTLCMD
;
1657 int lttng_event_release(struct inode
*inode
, struct file
*file
)
1659 struct lttng_event
*event
;
1660 struct lttng_enabler
*enabler
;
1661 enum lttng_event_type
*evtype
= file
->private_data
;
1667 case LTTNG_TYPE_EVENT
:
1668 event
= file
->private_data
;
1670 fput(event
->chan
->file
);
1672 case LTTNG_TYPE_ENABLER
:
1673 enabler
= file
->private_data
;
1675 fput(enabler
->chan
->file
);
1685 /* TODO: filter control ioctl */
1686 static const struct file_operations lttng_event_fops
= {
1687 .owner
= THIS_MODULE
,
1688 .release
= lttng_event_release
,
1689 .unlocked_ioctl
= lttng_event_ioctl
,
1690 #ifdef CONFIG_COMPAT
1691 .compat_ioctl
= lttng_event_ioctl
,
1695 static int put_u64(uint64_t val
, unsigned long arg
)
1697 return put_user(val
, (uint64_t __user
*) arg
);
1700 static long lttng_stream_ring_buffer_ioctl(struct file
*filp
,
1701 unsigned int cmd
, unsigned long arg
)
1703 struct lib_ring_buffer
*buf
= filp
->private_data
;
1704 struct channel
*chan
= buf
->backend
.chan
;
1705 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1706 const struct lttng_channel_ops
*ops
= chan
->backend
.priv_ops
;
1709 if (atomic_read(&chan
->record_disabled
))
1713 case LTTNG_RING_BUFFER_GET_TIMESTAMP_BEGIN
:
1717 ret
= ops
->timestamp_begin(config
, buf
, &ts
);
1720 return put_u64(ts
, arg
);
1722 case LTTNG_RING_BUFFER_GET_TIMESTAMP_END
:
1726 ret
= ops
->timestamp_end(config
, buf
, &ts
);
1729 return put_u64(ts
, arg
);
1731 case LTTNG_RING_BUFFER_GET_EVENTS_DISCARDED
:
1735 ret
= ops
->events_discarded(config
, buf
, &ed
);
1738 return put_u64(ed
, arg
);
1740 case LTTNG_RING_BUFFER_GET_CONTENT_SIZE
:
1744 ret
= ops
->content_size(config
, buf
, &cs
);
1747 return put_u64(cs
, arg
);
1749 case LTTNG_RING_BUFFER_GET_PACKET_SIZE
:
1753 ret
= ops
->packet_size(config
, buf
, &ps
);
1756 return put_u64(ps
, arg
);
1758 case LTTNG_RING_BUFFER_GET_STREAM_ID
:
1762 ret
= ops
->stream_id(config
, buf
, &si
);
1765 return put_u64(si
, arg
);
1767 case LTTNG_RING_BUFFER_GET_CURRENT_TIMESTAMP
:
1771 ret
= ops
->current_timestamp(config
, buf
, &ts
);
1774 return put_u64(ts
, arg
);
1776 case LTTNG_RING_BUFFER_GET_SEQ_NUM
:
1780 ret
= ops
->sequence_number(config
, buf
, &seq
);
1783 return put_u64(seq
, arg
);
1785 case LTTNG_RING_BUFFER_INSTANCE_ID
:
1789 ret
= ops
->instance_id(config
, buf
, &id
);
1792 return put_u64(id
, arg
);
1795 return lib_ring_buffer_file_operations
.unlocked_ioctl(filp
,
1803 #ifdef CONFIG_COMPAT
1804 static long lttng_stream_ring_buffer_compat_ioctl(struct file
*filp
,
1805 unsigned int cmd
, unsigned long arg
)
1807 struct lib_ring_buffer
*buf
= filp
->private_data
;
1808 struct channel
*chan
= buf
->backend
.chan
;
1809 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1810 const struct lttng_channel_ops
*ops
= chan
->backend
.priv_ops
;
1813 if (atomic_read(&chan
->record_disabled
))
1817 case LTTNG_RING_BUFFER_COMPAT_GET_TIMESTAMP_BEGIN
:
1821 ret
= ops
->timestamp_begin(config
, buf
, &ts
);
1824 return put_u64(ts
, arg
);
1826 case LTTNG_RING_BUFFER_COMPAT_GET_TIMESTAMP_END
:
1830 ret
= ops
->timestamp_end(config
, buf
, &ts
);
1833 return put_u64(ts
, arg
);
1835 case LTTNG_RING_BUFFER_COMPAT_GET_EVENTS_DISCARDED
:
1839 ret
= ops
->events_discarded(config
, buf
, &ed
);
1842 return put_u64(ed
, arg
);
1844 case LTTNG_RING_BUFFER_COMPAT_GET_CONTENT_SIZE
:
1848 ret
= ops
->content_size(config
, buf
, &cs
);
1851 return put_u64(cs
, arg
);
1853 case LTTNG_RING_BUFFER_COMPAT_GET_PACKET_SIZE
:
1857 ret
= ops
->packet_size(config
, buf
, &ps
);
1860 return put_u64(ps
, arg
);
1862 case LTTNG_RING_BUFFER_COMPAT_GET_STREAM_ID
:
1866 ret
= ops
->stream_id(config
, buf
, &si
);
1869 return put_u64(si
, arg
);
1871 case LTTNG_RING_BUFFER_GET_CURRENT_TIMESTAMP
:
1875 ret
= ops
->current_timestamp(config
, buf
, &ts
);
1878 return put_u64(ts
, arg
);
1880 case LTTNG_RING_BUFFER_COMPAT_GET_SEQ_NUM
:
1884 ret
= ops
->sequence_number(config
, buf
, &seq
);
1887 return put_u64(seq
, arg
);
1889 case LTTNG_RING_BUFFER_COMPAT_INSTANCE_ID
:
1893 ret
= ops
->instance_id(config
, buf
, &id
);
1896 return put_u64(id
, arg
);
1899 return lib_ring_buffer_file_operations
.compat_ioctl(filp
,
1906 #endif /* CONFIG_COMPAT */
1908 static void lttng_stream_override_ring_buffer_fops(void)
1910 lttng_stream_ring_buffer_file_operations
.owner
= THIS_MODULE
;
1911 lttng_stream_ring_buffer_file_operations
.open
=
1912 lib_ring_buffer_file_operations
.open
;
1913 lttng_stream_ring_buffer_file_operations
.release
=
1914 lib_ring_buffer_file_operations
.release
;
1915 lttng_stream_ring_buffer_file_operations
.poll
=
1916 lib_ring_buffer_file_operations
.poll
;
1917 lttng_stream_ring_buffer_file_operations
.splice_read
=
1918 lib_ring_buffer_file_operations
.splice_read
;
1919 lttng_stream_ring_buffer_file_operations
.mmap
=
1920 lib_ring_buffer_file_operations
.mmap
;
1921 lttng_stream_ring_buffer_file_operations
.unlocked_ioctl
=
1922 lttng_stream_ring_buffer_ioctl
;
1923 lttng_stream_ring_buffer_file_operations
.llseek
=
1924 lib_ring_buffer_file_operations
.llseek
;
1925 #ifdef CONFIG_COMPAT
1926 lttng_stream_ring_buffer_file_operations
.compat_ioctl
=
1927 lttng_stream_ring_buffer_compat_ioctl
;
1931 int __init
lttng_abi_init(void)
1937 ret
= lttng_tp_mempool_init();
1942 lttng_proc_dentry
= proc_create_data("lttng", S_IRUSR
| S_IWUSR
, NULL
,
1943 <tng_proc_ops
, NULL
);
1945 if (!lttng_proc_dentry
) {
1946 printk(KERN_ERR
"Error creating LTTng control file\n");
1950 lttng_stream_override_ring_buffer_fops();
1954 lttng_tp_mempool_destroy();
1955 lttng_clock_unref();
1959 /* No __exit annotation because used by init error path too. */
1960 void lttng_abi_exit(void)
1962 lttng_tp_mempool_destroy();
1963 lttng_clock_unref();
1964 if (lttng_proc_dentry
)
1965 remove_proc_entry("lttng", NULL
);