1 /* SPDX-License-Identifier: (GPL-2.0 or LGPL-2.1)
5 * Holds LTTng per-session event registry.
7 * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
11 * This page_alloc.h wrapper needs to be included before gfpflags.h because it
12 * overrides a function with a define.
14 #include "wrapper/page_alloc.h"
16 #include <linux/module.h>
17 #include <linux/mutex.h>
18 #include <linux/sched.h>
19 #include <linux/slab.h>
20 #include <linux/jiffies.h>
21 #include <linux/utsname.h>
22 #include <linux/err.h>
23 #include <linux/seq_file.h>
24 #include <linux/file.h>
25 #include <linux/anon_inodes.h>
26 #include <wrapper/file.h>
27 #include <linux/jhash.h>
28 #include <linux/uaccess.h>
29 #include <linux/vmalloc.h>
30 #include <linux/dmi.h>
32 #include <wrapper/uuid.h>
33 #include <wrapper/vmalloc.h> /* for wrapper_vmalloc_sync_mappings() */
34 #include <wrapper/random.h>
35 #include <wrapper/tracepoint.h>
36 #include <wrapper/list.h>
37 #include <wrapper/types.h>
38 #include <lttng-kernel-version.h>
39 #include <lttng-events.h>
40 #include <lttng-tracer.h>
41 #include <lttng-abi-old.h>
42 #include <lttng-endian.h>
43 #include <lttng-string-utils.h>
44 #include <wrapper/compiler_attributes.h>
45 #include <wrapper/ringbuffer/backend.h>
46 #include <wrapper/ringbuffer/frontend.h>
47 #include <wrapper/time.h>
49 #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(5,16,0) \
50 || LTTNG_RHEL_KERNEL_RANGE(5,14,0,162,0,0, 5,15,0,0,0,0))
51 #include <linux/stdarg.h>
56 #define METADATA_CACHE_DEFAULT_SIZE 4096
58 static LIST_HEAD(sessions
);
59 static LIST_HEAD(lttng_transport_list
);
61 * Protect the sessions and metadata caches.
63 static DEFINE_MUTEX(sessions_mutex
);
64 static struct kmem_cache
*event_cache
;
66 static void lttng_session_lazy_sync_enablers(struct lttng_session
*session
);
67 static void lttng_session_sync_enablers(struct lttng_session
*session
);
68 static void lttng_enabler_destroy(struct lttng_enabler
*enabler
);
70 static void _lttng_event_destroy(struct lttng_event
*event
);
71 static void _lttng_channel_destroy(struct lttng_channel
*chan
);
72 static int _lttng_event_unregister(struct lttng_event
*event
);
74 int _lttng_event_metadata_statedump(struct lttng_session
*session
,
75 struct lttng_channel
*chan
,
76 struct lttng_event
*event
);
78 int _lttng_session_metadata_statedump(struct lttng_session
*session
);
80 void _lttng_metadata_channel_hangup(struct lttng_metadata_stream
*stream
);
82 int _lttng_field_statedump(struct lttng_session
*session
,
83 const struct lttng_event_field
*field
,
86 void synchronize_trace(void)
88 #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(5,1,0) || \
89 LTTNG_RHEL_KERNEL_RANGE(4,18,0,193,0,0, 4,19,0,0,0,0))
95 #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(3,4,0))
96 #ifdef CONFIG_PREEMPT_RT_FULL
99 #else /* (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(3,4,0)) */
100 #ifdef CONFIG_PREEMPT_RT
103 #endif /* (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(3,4,0)) */
106 void lttng_lock_sessions(void)
108 mutex_lock(&sessions_mutex
);
111 void lttng_unlock_sessions(void)
113 mutex_unlock(&sessions_mutex
);
117 * Called with sessions lock held.
119 int lttng_session_active(void)
121 struct lttng_session
*iter
;
123 list_for_each_entry(iter
, &sessions
, list
) {
130 struct lttng_session
*lttng_session_create(void)
132 struct lttng_session
*session
;
133 struct lttng_metadata_cache
*metadata_cache
;
136 mutex_lock(&sessions_mutex
);
137 session
= lttng_kvzalloc(sizeof(struct lttng_session
), GFP_KERNEL
);
140 INIT_LIST_HEAD(&session
->chan
);
141 INIT_LIST_HEAD(&session
->events
);
142 lttng_guid_gen(&session
->uuid
);
144 metadata_cache
= kzalloc(sizeof(struct lttng_metadata_cache
),
147 goto err_free_session
;
148 metadata_cache
->data
= vzalloc(METADATA_CACHE_DEFAULT_SIZE
);
149 if (!metadata_cache
->data
)
151 metadata_cache
->cache_alloc
= METADATA_CACHE_DEFAULT_SIZE
;
152 kref_init(&metadata_cache
->refcount
);
153 mutex_init(&metadata_cache
->lock
);
154 session
->metadata_cache
= metadata_cache
;
155 INIT_LIST_HEAD(&metadata_cache
->metadata_stream
);
156 memcpy(&metadata_cache
->uuid
, &session
->uuid
,
157 sizeof(metadata_cache
->uuid
));
158 INIT_LIST_HEAD(&session
->enablers_head
);
159 for (i
= 0; i
< LTTNG_EVENT_HT_SIZE
; i
++)
160 INIT_HLIST_HEAD(&session
->events_ht
.table
[i
]);
161 list_add(&session
->list
, &sessions
);
162 session
->pid_tracker
.session
= session
;
163 session
->pid_tracker
.tracker_type
= TRACKER_PID
;
164 session
->vpid_tracker
.session
= session
;
165 session
->vpid_tracker
.tracker_type
= TRACKER_VPID
;
166 session
->uid_tracker
.session
= session
;
167 session
->uid_tracker
.tracker_type
= TRACKER_UID
;
168 session
->vuid_tracker
.session
= session
;
169 session
->vuid_tracker
.tracker_type
= TRACKER_VUID
;
170 session
->gid_tracker
.session
= session
;
171 session
->gid_tracker
.tracker_type
= TRACKER_GID
;
172 session
->vgid_tracker
.session
= session
;
173 session
->vgid_tracker
.tracker_type
= TRACKER_VGID
;
174 mutex_unlock(&sessions_mutex
);
178 kfree(metadata_cache
);
180 lttng_kvfree(session
);
182 mutex_unlock(&sessions_mutex
);
186 void metadata_cache_destroy(struct kref
*kref
)
188 struct lttng_metadata_cache
*cache
=
189 container_of(kref
, struct lttng_metadata_cache
, refcount
);
194 void lttng_session_destroy(struct lttng_session
*session
)
196 struct lttng_channel
*chan
, *tmpchan
;
197 struct lttng_event
*event
, *tmpevent
;
198 struct lttng_metadata_stream
*metadata_stream
;
199 struct lttng_enabler
*enabler
, *tmpenabler
;
202 mutex_lock(&sessions_mutex
);
203 WRITE_ONCE(session
->active
, 0);
204 list_for_each_entry(chan
, &session
->chan
, list
) {
205 ret
= lttng_syscalls_unregister(chan
);
208 list_for_each_entry(event
, &session
->events
, list
) {
209 ret
= _lttng_event_unregister(event
);
212 synchronize_trace(); /* Wait for in-flight events to complete */
213 list_for_each_entry(chan
, &session
->chan
, list
) {
214 ret
= lttng_syscalls_destroy(chan
);
217 list_for_each_entry_safe(enabler
, tmpenabler
,
218 &session
->enablers_head
, node
)
219 lttng_enabler_destroy(enabler
);
220 list_for_each_entry_safe(event
, tmpevent
, &session
->events
, list
)
221 _lttng_event_destroy(event
);
222 list_for_each_entry_safe(chan
, tmpchan
, &session
->chan
, list
) {
223 BUG_ON(chan
->channel_type
== METADATA_CHANNEL
);
224 _lttng_channel_destroy(chan
);
226 mutex_lock(&session
->metadata_cache
->lock
);
227 list_for_each_entry(metadata_stream
, &session
->metadata_cache
->metadata_stream
, list
)
228 _lttng_metadata_channel_hangup(metadata_stream
);
229 mutex_unlock(&session
->metadata_cache
->lock
);
230 lttng_id_tracker_destroy(&session
->pid_tracker
, false);
231 lttng_id_tracker_destroy(&session
->vpid_tracker
, false);
232 lttng_id_tracker_destroy(&session
->uid_tracker
, false);
233 lttng_id_tracker_destroy(&session
->vuid_tracker
, false);
234 lttng_id_tracker_destroy(&session
->gid_tracker
, false);
235 lttng_id_tracker_destroy(&session
->vgid_tracker
, false);
236 kref_put(&session
->metadata_cache
->refcount
, metadata_cache_destroy
);
237 list_del(&session
->list
);
238 mutex_unlock(&sessions_mutex
);
239 lttng_kvfree(session
);
242 int lttng_session_statedump(struct lttng_session
*session
)
246 mutex_lock(&sessions_mutex
);
247 ret
= lttng_statedump_start(session
);
248 mutex_unlock(&sessions_mutex
);
252 int lttng_session_enable(struct lttng_session
*session
)
255 struct lttng_channel
*chan
;
257 mutex_lock(&sessions_mutex
);
258 if (session
->active
) {
263 /* Set transient enabler state to "enabled" */
266 /* We need to sync enablers with session before activation. */
267 lttng_session_sync_enablers(session
);
270 * Snapshot the number of events per channel to know the type of header
273 list_for_each_entry(chan
, &session
->chan
, list
) {
274 if (chan
->header_type
)
275 continue; /* don't change it if session stop/restart */
276 if (chan
->free_event_id
< 31)
277 chan
->header_type
= 1; /* compact */
279 chan
->header_type
= 2; /* large */
282 /* Clear each stream's quiescent state. */
283 list_for_each_entry(chan
, &session
->chan
, list
) {
284 if (chan
->channel_type
!= METADATA_CHANNEL
)
285 lib_ring_buffer_clear_quiescent_channel(chan
->chan
);
288 WRITE_ONCE(session
->active
, 1);
289 WRITE_ONCE(session
->been_active
, 1);
290 ret
= _lttng_session_metadata_statedump(session
);
292 WRITE_ONCE(session
->active
, 0);
295 ret
= lttng_statedump_start(session
);
297 WRITE_ONCE(session
->active
, 0);
299 mutex_unlock(&sessions_mutex
);
303 int lttng_session_disable(struct lttng_session
*session
)
306 struct lttng_channel
*chan
;
308 mutex_lock(&sessions_mutex
);
309 if (!session
->active
) {
313 WRITE_ONCE(session
->active
, 0);
315 /* Set transient enabler state to "disabled" */
317 lttng_session_sync_enablers(session
);
319 /* Set each stream's quiescent state. */
320 list_for_each_entry(chan
, &session
->chan
, list
) {
321 if (chan
->channel_type
!= METADATA_CHANNEL
)
322 lib_ring_buffer_set_quiescent_channel(chan
->chan
);
325 mutex_unlock(&sessions_mutex
);
329 int lttng_session_metadata_regenerate(struct lttng_session
*session
)
332 struct lttng_channel
*chan
;
333 struct lttng_event
*event
;
334 struct lttng_metadata_cache
*cache
= session
->metadata_cache
;
335 struct lttng_metadata_stream
*stream
;
337 mutex_lock(&sessions_mutex
);
338 if (!session
->active
) {
343 mutex_lock(&cache
->lock
);
344 memset(cache
->data
, 0, cache
->cache_alloc
);
345 cache
->metadata_written
= 0;
347 list_for_each_entry(stream
, &session
->metadata_cache
->metadata_stream
, list
) {
348 stream
->metadata_out
= 0;
349 stream
->metadata_in
= 0;
351 mutex_unlock(&cache
->lock
);
353 session
->metadata_dumped
= 0;
354 list_for_each_entry(chan
, &session
->chan
, list
) {
355 chan
->metadata_dumped
= 0;
358 list_for_each_entry(event
, &session
->events
, list
) {
359 event
->metadata_dumped
= 0;
362 ret
= _lttng_session_metadata_statedump(session
);
365 mutex_unlock(&sessions_mutex
);
369 int lttng_channel_enable(struct lttng_channel
*channel
)
373 mutex_lock(&sessions_mutex
);
374 if (channel
->channel_type
== METADATA_CHANNEL
) {
378 if (channel
->enabled
) {
382 /* Set transient enabler state to "enabled" */
384 lttng_session_sync_enablers(channel
->session
);
385 /* Set atomically the state to "enabled" */
386 WRITE_ONCE(channel
->enabled
, 1);
388 mutex_unlock(&sessions_mutex
);
392 int lttng_channel_disable(struct lttng_channel
*channel
)
396 mutex_lock(&sessions_mutex
);
397 if (channel
->channel_type
== METADATA_CHANNEL
) {
401 if (!channel
->enabled
) {
405 /* Set atomically the state to "disabled" */
406 WRITE_ONCE(channel
->enabled
, 0);
407 /* Set transient enabler state to "enabled" */
409 lttng_session_sync_enablers(channel
->session
);
411 mutex_unlock(&sessions_mutex
);
415 int lttng_event_enable(struct lttng_event
*event
)
419 mutex_lock(&sessions_mutex
);
420 if (event
->chan
->channel_type
== METADATA_CHANNEL
) {
424 if (event
->enabled
) {
428 switch (event
->instrumentation
) {
429 case LTTNG_KERNEL_TRACEPOINT
:
430 case LTTNG_KERNEL_SYSCALL
:
433 case LTTNG_KERNEL_KPROBE
:
434 case LTTNG_KERNEL_UPROBE
:
435 case LTTNG_KERNEL_NOOP
:
436 WRITE_ONCE(event
->enabled
, 1);
438 case LTTNG_KERNEL_KRETPROBE
:
439 ret
= lttng_kretprobes_event_enable_state(event
, 1);
441 case LTTNG_KERNEL_FUNCTION
:
448 mutex_unlock(&sessions_mutex
);
452 int lttng_event_disable(struct lttng_event
*event
)
456 mutex_lock(&sessions_mutex
);
457 if (event
->chan
->channel_type
== METADATA_CHANNEL
) {
461 if (!event
->enabled
) {
465 switch (event
->instrumentation
) {
466 case LTTNG_KERNEL_TRACEPOINT
:
467 case LTTNG_KERNEL_SYSCALL
:
470 case LTTNG_KERNEL_KPROBE
:
471 case LTTNG_KERNEL_UPROBE
:
472 case LTTNG_KERNEL_NOOP
:
473 WRITE_ONCE(event
->enabled
, 0);
475 case LTTNG_KERNEL_KRETPROBE
:
476 ret
= lttng_kretprobes_event_enable_state(event
, 0);
478 case LTTNG_KERNEL_FUNCTION
:
485 mutex_unlock(&sessions_mutex
);
489 static struct lttng_transport
*lttng_transport_find(const char *name
)
491 struct lttng_transport
*transport
;
493 list_for_each_entry(transport
, <tng_transport_list
, node
) {
494 if (!strcmp(transport
->name
, name
))
500 struct lttng_channel
*lttng_channel_create(struct lttng_session
*session
,
501 const char *transport_name
,
503 size_t subbuf_size
, size_t num_subbuf
,
504 unsigned int switch_timer_interval
,
505 unsigned int read_timer_interval
,
506 enum channel_type channel_type
)
508 struct lttng_channel
*chan
;
509 struct lttng_transport
*transport
= NULL
;
511 mutex_lock(&sessions_mutex
);
512 if (session
->been_active
&& channel_type
!= METADATA_CHANNEL
)
513 goto active
; /* Refuse to add channel to active session */
514 transport
= lttng_transport_find(transport_name
);
516 printk(KERN_WARNING
"LTTng transport %s not found\n",
520 if (!try_module_get(transport
->owner
)) {
521 printk(KERN_WARNING
"LTT : Can't lock transport module.\n");
524 chan
= kzalloc(sizeof(struct lttng_channel
), GFP_KERNEL
);
527 chan
->session
= session
;
528 chan
->id
= session
->free_chan_id
++;
529 chan
->ops
= &transport
->ops
;
531 * Note: the channel creation op already writes into the packet
532 * headers. Therefore the "chan" information used as input
533 * should be already accessible.
535 chan
->chan
= transport
->ops
.channel_create(transport_name
,
536 chan
, buf_addr
, subbuf_size
, num_subbuf
,
537 switch_timer_interval
, read_timer_interval
);
542 chan
->transport
= transport
;
543 chan
->channel_type
= channel_type
;
544 list_add(&chan
->list
, &session
->chan
);
545 mutex_unlock(&sessions_mutex
);
552 module_put(transport
->owner
);
555 mutex_unlock(&sessions_mutex
);
560 * Only used internally at session destruction for per-cpu channels, and
561 * when metadata channel is released.
562 * Needs to be called with sessions mutex held.
565 void _lttng_channel_destroy(struct lttng_channel
*chan
)
567 chan
->ops
->channel_destroy(chan
->chan
);
568 module_put(chan
->transport
->owner
);
569 list_del(&chan
->list
);
570 lttng_destroy_context(chan
->ctx
);
574 void lttng_metadata_channel_destroy(struct lttng_channel
*chan
)
576 BUG_ON(chan
->channel_type
!= METADATA_CHANNEL
);
578 /* Protect the metadata cache with the sessions_mutex. */
579 mutex_lock(&sessions_mutex
);
580 _lttng_channel_destroy(chan
);
581 mutex_unlock(&sessions_mutex
);
583 EXPORT_SYMBOL_GPL(lttng_metadata_channel_destroy
);
586 void _lttng_metadata_channel_hangup(struct lttng_metadata_stream
*stream
)
588 stream
->finalized
= 1;
589 wake_up_interruptible(&stream
->read_wait
);
593 * Supports event creation while tracing session is active.
594 * Needs to be called with sessions mutex held.
596 struct lttng_event
*_lttng_event_create(struct lttng_channel
*chan
,
597 struct lttng_kernel_event
*event_param
,
599 const struct lttng_event_desc
*event_desc
,
600 enum lttng_kernel_instrumentation itype
)
602 struct lttng_session
*session
= chan
->session
;
603 struct lttng_event
*event
;
604 const char *event_name
;
605 struct hlist_head
*head
;
610 if (chan
->free_event_id
== -1U) {
616 case LTTNG_KERNEL_TRACEPOINT
:
617 event_name
= event_desc
->name
;
619 case LTTNG_KERNEL_KPROBE
:
620 case LTTNG_KERNEL_UPROBE
:
621 case LTTNG_KERNEL_KRETPROBE
:
622 case LTTNG_KERNEL_NOOP
:
623 case LTTNG_KERNEL_SYSCALL
:
624 event_name
= event_param
->name
;
626 case LTTNG_KERNEL_FUNCTION
:
633 name_len
= strlen(event_name
);
634 hash
= jhash(event_name
, name_len
, 0);
635 head
= &session
->events_ht
.table
[hash
& (LTTNG_EVENT_HT_SIZE
- 1)];
636 lttng_hlist_for_each_entry(event
, head
, hlist
) {
637 WARN_ON_ONCE(!event
->desc
);
638 if (!strncmp(event
->desc
->name
, event_name
,
639 LTTNG_KERNEL_SYM_NAME_LEN
- 1)
640 && chan
== event
->chan
) {
646 event
= kmem_cache_zalloc(event_cache
, GFP_KERNEL
);
652 event
->filter
= filter
;
653 event
->id
= chan
->free_event_id
++;
654 event
->instrumentation
= itype
;
655 event
->evtype
= LTTNG_TYPE_EVENT
;
656 INIT_LIST_HEAD(&event
->bytecode_runtime_head
);
657 INIT_LIST_HEAD(&event
->enablers_ref_head
);
660 case LTTNG_KERNEL_TRACEPOINT
:
661 /* Event will be enabled by enabler sync. */
663 event
->registered
= 0;
664 event
->desc
= lttng_event_get(event_name
);
669 /* Populate lttng_event structure before event registration. */
672 case LTTNG_KERNEL_KPROBE
:
674 * Needs to be explicitly enabled after creation, since
675 * we may want to apply filters.
678 event
->registered
= 1;
680 * Populate lttng_event structure before event
684 ret
= lttng_kprobes_register(event_name
,
685 event_param
->u
.kprobe
.symbol_name
,
686 event_param
->u
.kprobe
.offset
,
687 event_param
->u
.kprobe
.addr
,
693 ret
= try_module_get(event
->desc
->owner
);
696 case LTTNG_KERNEL_KRETPROBE
:
698 struct lttng_event
*event_return
;
700 /* kretprobe defines 2 events */
702 * Needs to be explicitly enabled after creation, since
703 * we may want to apply filters.
706 event
->registered
= 1;
708 kmem_cache_zalloc(event_cache
, GFP_KERNEL
);
713 event_return
->chan
= chan
;
714 event_return
->filter
= filter
;
715 event_return
->id
= chan
->free_event_id
++;
716 event_return
->enabled
= 0;
717 event_return
->registered
= 1;
718 event_return
->instrumentation
= itype
;
719 INIT_LIST_HEAD(&event_return
->bytecode_runtime_head
);
720 INIT_LIST_HEAD(&event_return
->enablers_ref_head
);
722 * Populate lttng_event structure before kretprobe registration.
725 ret
= lttng_kretprobes_register(event_name
,
726 event_param
->u
.kretprobe
.symbol_name
,
727 event_param
->u
.kretprobe
.offset
,
728 event_param
->u
.kretprobe
.addr
,
729 event
, event_return
);
731 kmem_cache_free(event_cache
, event_return
);
735 /* Take 2 refs on the module: one per event. */
736 ret
= try_module_get(event
->desc
->owner
);
738 ret
= try_module_get(event
->desc
->owner
);
740 ret
= _lttng_event_metadata_statedump(chan
->session
, chan
,
742 WARN_ON_ONCE(ret
> 0);
744 kmem_cache_free(event_cache
, event_return
);
745 module_put(event
->desc
->owner
);
746 module_put(event
->desc
->owner
);
747 goto statedump_error
;
749 list_add(&event_return
->list
, &chan
->session
->events
);
752 case LTTNG_KERNEL_NOOP
:
753 case LTTNG_KERNEL_SYSCALL
:
755 * Needs to be explicitly enabled after creation, since
756 * we may want to apply filters.
759 event
->registered
= 0;
760 event
->desc
= event_desc
;
761 switch (event_param
->u
.syscall
.entryexit
) {
762 case LTTNG_KERNEL_SYSCALL_ENTRYEXIT
:
765 case LTTNG_KERNEL_SYSCALL_ENTRY
:
766 event
->u
.syscall
.entryexit
= LTTNG_SYSCALL_ENTRY
;
768 case LTTNG_KERNEL_SYSCALL_EXIT
:
769 event
->u
.syscall
.entryexit
= LTTNG_SYSCALL_EXIT
;
772 switch (event_param
->u
.syscall
.abi
) {
773 case LTTNG_KERNEL_SYSCALL_ABI_ALL
:
776 case LTTNG_KERNEL_SYSCALL_ABI_NATIVE
:
777 event
->u
.syscall
.abi
= LTTNG_SYSCALL_ABI_NATIVE
;
779 case LTTNG_KERNEL_SYSCALL_ABI_COMPAT
:
780 event
->u
.syscall
.abi
= LTTNG_SYSCALL_ABI_COMPAT
;
788 case LTTNG_KERNEL_UPROBE
:
790 * Needs to be explicitly enabled after creation, since
791 * we may want to apply filters.
794 event
->registered
= 1;
797 * Populate lttng_event structure before event
802 ret
= lttng_uprobes_register(event_param
->name
,
803 event_param
->u
.uprobe
.fd
,
807 ret
= try_module_get(event
->desc
->owner
);
810 case LTTNG_KERNEL_FUNCTION
:
817 ret
= _lttng_event_metadata_statedump(chan
->session
, chan
, event
);
818 WARN_ON_ONCE(ret
> 0);
820 goto statedump_error
;
822 hlist_add_head(&event
->hlist
, head
);
823 list_add(&event
->list
, &chan
->session
->events
);
827 /* If a statedump error occurs, events will not be readable. */
829 kmem_cache_free(event_cache
, event
);
837 struct lttng_event
*lttng_event_create(struct lttng_channel
*chan
,
838 struct lttng_kernel_event
*event_param
,
840 const struct lttng_event_desc
*event_desc
,
841 enum lttng_kernel_instrumentation itype
)
843 struct lttng_event
*event
;
845 mutex_lock(&sessions_mutex
);
846 event
= _lttng_event_create(chan
, event_param
, filter
, event_desc
,
848 mutex_unlock(&sessions_mutex
);
852 /* Only used for tracepoints for now. */
854 void register_event(struct lttng_event
*event
)
856 const struct lttng_event_desc
*desc
;
859 if (event
->registered
)
863 switch (event
->instrumentation
) {
864 case LTTNG_KERNEL_TRACEPOINT
:
865 ret
= lttng_wrapper_tracepoint_probe_register(desc
->kname
,
866 desc
->probe_callback
,
869 case LTTNG_KERNEL_SYSCALL
:
870 ret
= lttng_syscall_filter_enable(event
->chan
, event
);
872 case LTTNG_KERNEL_KPROBE
:
873 case LTTNG_KERNEL_UPROBE
:
874 case LTTNG_KERNEL_KRETPROBE
:
875 case LTTNG_KERNEL_NOOP
:
878 case LTTNG_KERNEL_FUNCTION
:
884 event
->registered
= 1;
888 * Only used internally at session destruction.
890 int _lttng_event_unregister(struct lttng_event
*event
)
892 const struct lttng_event_desc
*desc
;
895 if (!event
->registered
)
899 switch (event
->instrumentation
) {
900 case LTTNG_KERNEL_TRACEPOINT
:
901 ret
= lttng_wrapper_tracepoint_probe_unregister(event
->desc
->kname
,
902 event
->desc
->probe_callback
,
905 case LTTNG_KERNEL_KPROBE
:
906 lttng_kprobes_unregister(event
);
909 case LTTNG_KERNEL_KRETPROBE
:
910 lttng_kretprobes_unregister(event
);
913 case LTTNG_KERNEL_SYSCALL
:
914 ret
= lttng_syscall_filter_disable(event
->chan
, event
);
916 case LTTNG_KERNEL_NOOP
:
919 case LTTNG_KERNEL_UPROBE
:
920 lttng_uprobes_unregister(event
);
923 case LTTNG_KERNEL_FUNCTION
:
929 event
->registered
= 0;
934 * Only used internally at session destruction.
937 void _lttng_event_destroy(struct lttng_event
*event
)
939 struct lttng_enabler_ref
*enabler_ref
, *tmp_enabler_ref
;
941 switch (event
->instrumentation
) {
942 case LTTNG_KERNEL_TRACEPOINT
:
943 lttng_event_put(event
->desc
);
945 case LTTNG_KERNEL_KPROBE
:
946 module_put(event
->desc
->owner
);
947 lttng_kprobes_destroy_private(event
);
949 case LTTNG_KERNEL_KRETPROBE
:
950 module_put(event
->desc
->owner
);
951 lttng_kretprobes_destroy_private(event
);
953 case LTTNG_KERNEL_NOOP
:
954 case LTTNG_KERNEL_SYSCALL
:
956 case LTTNG_KERNEL_UPROBE
:
957 module_put(event
->desc
->owner
);
958 lttng_uprobes_destroy_private(event
);
960 case LTTNG_KERNEL_FUNCTION
:
965 list_del(&event
->list
);
966 lttng_destroy_context(event
->ctx
);
967 lttng_free_event_filter_runtime(event
);
968 /* Free event enabler refs */
969 list_for_each_entry_safe(enabler_ref
, tmp_enabler_ref
,
970 &event
->enablers_ref_head
, node
)
972 kmem_cache_free(event_cache
, event
);
975 struct lttng_id_tracker
*get_tracker(struct lttng_session
*session
,
976 enum tracker_type tracker_type
)
978 switch (tracker_type
) {
980 return &session
->pid_tracker
;
982 return &session
->vpid_tracker
;
984 return &session
->uid_tracker
;
986 return &session
->vuid_tracker
;
988 return &session
->gid_tracker
;
990 return &session
->vgid_tracker
;
997 int lttng_session_track_id(struct lttng_session
*session
,
998 enum tracker_type tracker_type
, int id
)
1000 struct lttng_id_tracker
*tracker
;
1003 tracker
= get_tracker(session
, tracker_type
);
1008 mutex_lock(&sessions_mutex
);
1010 /* track all ids: destroy tracker. */
1011 lttng_id_tracker_destroy(tracker
, true);
1014 ret
= lttng_id_tracker_add(tracker
, id
);
1016 mutex_unlock(&sessions_mutex
);
1020 int lttng_session_untrack_id(struct lttng_session
*session
,
1021 enum tracker_type tracker_type
, int id
)
1023 struct lttng_id_tracker
*tracker
;
1026 tracker
= get_tracker(session
, tracker_type
);
1031 mutex_lock(&sessions_mutex
);
1033 /* untrack all ids: replace by empty tracker. */
1034 ret
= lttng_id_tracker_empty_set(tracker
);
1036 ret
= lttng_id_tracker_del(tracker
, id
);
1038 mutex_unlock(&sessions_mutex
);
1043 void *id_list_start(struct seq_file
*m
, loff_t
*pos
)
1045 struct lttng_id_tracker
*id_tracker
= m
->private;
1046 struct lttng_id_tracker_rcu
*id_tracker_p
= id_tracker
->p
;
1047 struct lttng_id_hash_node
*e
;
1050 mutex_lock(&sessions_mutex
);
1052 for (i
= 0; i
< LTTNG_ID_TABLE_SIZE
; i
++) {
1053 struct hlist_head
*head
= &id_tracker_p
->id_hash
[i
];
1055 lttng_hlist_for_each_entry(e
, head
, hlist
) {
1061 /* ID tracker disabled. */
1062 if (iter
>= *pos
&& iter
== 0) {
1063 return id_tracker_p
; /* empty tracker */
1071 /* Called with sessions_mutex held. */
1073 void *id_list_next(struct seq_file
*m
, void *p
, loff_t
*ppos
)
1075 struct lttng_id_tracker
*id_tracker
= m
->private;
1076 struct lttng_id_tracker_rcu
*id_tracker_p
= id_tracker
->p
;
1077 struct lttng_id_hash_node
*e
;
1082 for (i
= 0; i
< LTTNG_ID_TABLE_SIZE
; i
++) {
1083 struct hlist_head
*head
= &id_tracker_p
->id_hash
[i
];
1085 lttng_hlist_for_each_entry(e
, head
, hlist
) {
1086 if (iter
++ >= *ppos
)
1091 /* ID tracker disabled. */
1092 if (iter
>= *ppos
&& iter
== 0)
1093 return p
; /* empty tracker */
1102 void id_list_stop(struct seq_file
*m
, void *p
)
1104 mutex_unlock(&sessions_mutex
);
1108 int id_list_show(struct seq_file
*m
, void *p
)
1110 struct lttng_id_tracker
*id_tracker
= m
->private;
1111 struct lttng_id_tracker_rcu
*id_tracker_p
= id_tracker
->p
;
1114 if (p
== id_tracker_p
) {
1115 /* Tracker disabled. */
1118 const struct lttng_id_hash_node
*e
= p
;
1120 id
= lttng_id_tracker_get_node_id(e
);
1122 switch (id_tracker
->tracker_type
) {
1124 seq_printf(m
, "process { pid = %d; };\n", id
);
1127 seq_printf(m
, "process { vpid = %d; };\n", id
);
1130 seq_printf(m
, "user { uid = %d; };\n", id
);
1133 seq_printf(m
, "user { vuid = %d; };\n", id
);
1136 seq_printf(m
, "group { gid = %d; };\n", id
);
1139 seq_printf(m
, "group { vgid = %d; };\n", id
);
1142 seq_printf(m
, "UNKNOWN { field = %d };\n", id
);
1148 const struct seq_operations lttng_tracker_ids_list_seq_ops
= {
1149 .start
= id_list_start
,
1150 .next
= id_list_next
,
1151 .stop
= id_list_stop
,
1152 .show
= id_list_show
,
1156 int lttng_tracker_ids_list_open(struct inode
*inode
, struct file
*file
)
1158 return seq_open(file
, <tng_tracker_ids_list_seq_ops
);
1162 int lttng_tracker_ids_list_release(struct inode
*inode
, struct file
*file
)
1164 struct seq_file
*m
= file
->private_data
;
1165 struct lttng_id_tracker
*id_tracker
= m
->private;
1168 WARN_ON_ONCE(!id_tracker
);
1169 ret
= seq_release(inode
, file
);
1171 fput(id_tracker
->session
->file
);
1175 const struct file_operations lttng_tracker_ids_list_fops
= {
1176 .owner
= THIS_MODULE
,
1177 .open
= lttng_tracker_ids_list_open
,
1179 .llseek
= seq_lseek
,
1180 .release
= lttng_tracker_ids_list_release
,
1183 int lttng_session_list_tracker_ids(struct lttng_session
*session
,
1184 enum tracker_type tracker_type
)
1186 struct file
*tracker_ids_list_file
;
1190 file_fd
= lttng_get_unused_fd();
1196 tracker_ids_list_file
= anon_inode_getfile("[lttng_tracker_ids_list]",
1197 <tng_tracker_ids_list_fops
,
1199 if (IS_ERR(tracker_ids_list_file
)) {
1200 ret
= PTR_ERR(tracker_ids_list_file
);
1203 if (!atomic_long_add_unless(&session
->file
->f_count
, 1, LONG_MAX
)) {
1205 goto refcount_error
;
1207 ret
= lttng_tracker_ids_list_fops
.open(NULL
, tracker_ids_list_file
);
1210 m
= tracker_ids_list_file
->private_data
;
1212 m
->private = get_tracker(session
, tracker_type
);
1213 BUG_ON(!m
->private);
1214 fd_install(file_fd
, tracker_ids_list_file
);
1219 atomic_long_dec(&session
->file
->f_count
);
1221 fput(tracker_ids_list_file
);
1223 put_unused_fd(file_fd
);
1229 * Enabler management.
1232 int lttng_match_enabler_star_glob(const char *desc_name
,
1233 const char *pattern
)
1235 if (!strutils_star_glob_match(pattern
, LTTNG_SIZE_MAX
,
1236 desc_name
, LTTNG_SIZE_MAX
))
1242 int lttng_match_enabler_name(const char *desc_name
,
1245 if (strcmp(desc_name
, name
))
1251 int lttng_desc_match_enabler(const struct lttng_event_desc
*desc
,
1252 struct lttng_enabler
*enabler
)
1254 const char *desc_name
, *enabler_name
;
1255 bool compat
= false, entry
= false;
1257 enabler_name
= enabler
->event_param
.name
;
1258 switch (enabler
->event_param
.instrumentation
) {
1259 case LTTNG_KERNEL_TRACEPOINT
:
1260 desc_name
= desc
->name
;
1261 switch (enabler
->type
) {
1262 case LTTNG_ENABLER_STAR_GLOB
:
1263 return lttng_match_enabler_star_glob(desc_name
, enabler_name
);
1264 case LTTNG_ENABLER_NAME
:
1265 return lttng_match_enabler_name(desc_name
, enabler_name
);
1270 case LTTNG_KERNEL_SYSCALL
:
1271 desc_name
= desc
->name
;
1272 if (!strncmp(desc_name
, "compat_", strlen("compat_"))) {
1273 desc_name
+= strlen("compat_");
1276 if (!strncmp(desc_name
, "syscall_exit_",
1277 strlen("syscall_exit_"))) {
1278 desc_name
+= strlen("syscall_exit_");
1279 } else if (!strncmp(desc_name
, "syscall_entry_",
1280 strlen("syscall_entry_"))) {
1281 desc_name
+= strlen("syscall_entry_");
1287 switch (enabler
->event_param
.u
.syscall
.entryexit
) {
1288 case LTTNG_KERNEL_SYSCALL_ENTRYEXIT
:
1290 case LTTNG_KERNEL_SYSCALL_ENTRY
:
1294 case LTTNG_KERNEL_SYSCALL_EXIT
:
1301 switch (enabler
->event_param
.u
.syscall
.abi
) {
1302 case LTTNG_KERNEL_SYSCALL_ABI_ALL
:
1304 case LTTNG_KERNEL_SYSCALL_ABI_NATIVE
:
1308 case LTTNG_KERNEL_SYSCALL_ABI_COMPAT
:
1315 switch (enabler
->event_param
.u
.syscall
.match
) {
1316 case LTTNG_SYSCALL_MATCH_NAME
:
1317 switch (enabler
->type
) {
1318 case LTTNG_ENABLER_STAR_GLOB
:
1319 return lttng_match_enabler_star_glob(desc_name
, enabler_name
);
1320 case LTTNG_ENABLER_NAME
:
1321 return lttng_match_enabler_name(desc_name
, enabler_name
);
1326 case LTTNG_SYSCALL_MATCH_NR
:
1327 return -EINVAL
; /* Not implemented. */
1339 int lttng_event_match_enabler(struct lttng_event
*event
,
1340 struct lttng_enabler
*enabler
)
1342 if (enabler
->event_param
.instrumentation
!= event
->instrumentation
)
1344 if (lttng_desc_match_enabler(event
->desc
, enabler
)
1345 && event
->chan
== enabler
->chan
)
1352 struct lttng_enabler_ref
*lttng_event_enabler_ref(struct lttng_event
*event
,
1353 struct lttng_enabler
*enabler
)
1355 struct lttng_enabler_ref
*enabler_ref
;
1357 list_for_each_entry(enabler_ref
,
1358 &event
->enablers_ref_head
, node
) {
1359 if (enabler_ref
->ref
== enabler
)
1366 void lttng_create_tracepoint_if_missing(struct lttng_enabler
*enabler
)
1368 struct lttng_session
*session
= enabler
->chan
->session
;
1369 struct lttng_probe_desc
*probe_desc
;
1370 const struct lttng_event_desc
*desc
;
1372 struct list_head
*probe_list
;
1374 probe_list
= lttng_get_probe_list_head();
1376 * For each probe event, if we find that a probe event matches
1377 * our enabler, create an associated lttng_event if not
1380 list_for_each_entry(probe_desc
, probe_list
, head
) {
1381 for (i
= 0; i
< probe_desc
->nr_events
; i
++) {
1383 struct hlist_head
*head
;
1384 const char *event_name
;
1387 struct lttng_event
*event
;
1389 desc
= probe_desc
->event_desc
[i
];
1390 if (!lttng_desc_match_enabler(desc
, enabler
))
1392 event_name
= desc
->name
;
1393 name_len
= strlen(event_name
);
1396 * Check if already created.
1398 hash
= jhash(event_name
, name_len
, 0);
1399 head
= &session
->events_ht
.table
[hash
& (LTTNG_EVENT_HT_SIZE
- 1)];
1400 lttng_hlist_for_each_entry(event
, head
, hlist
) {
1401 if (event
->desc
== desc
1402 && event
->chan
== enabler
->chan
)
1409 * We need to create an event for this
1412 event
= _lttng_event_create(enabler
->chan
,
1414 LTTNG_KERNEL_TRACEPOINT
);
1416 printk(KERN_INFO
"Unable to create event %s\n",
1417 probe_desc
->event_desc
[i
]->name
);
1424 void lttng_create_syscall_if_missing(struct lttng_enabler
*enabler
)
1428 ret
= lttng_syscalls_register(enabler
->chan
, NULL
);
1433 * Create struct lttng_event if it is missing and present in the list of
1434 * tracepoint probes.
1435 * Should be called with sessions mutex held.
1438 void lttng_create_event_if_missing(struct lttng_enabler
*enabler
)
1440 switch (enabler
->event_param
.instrumentation
) {
1441 case LTTNG_KERNEL_TRACEPOINT
:
1442 lttng_create_tracepoint_if_missing(enabler
);
1444 case LTTNG_KERNEL_SYSCALL
:
1445 lttng_create_syscall_if_missing(enabler
);
1454 * Create events associated with an enabler (if not already present),
1455 * and add backward reference from the event to the enabler.
1456 * Should be called with sessions mutex held.
1459 int lttng_enabler_ref_events(struct lttng_enabler
*enabler
)
1461 struct lttng_channel
*chan
= enabler
->chan
;
1462 struct lttng_session
*session
= chan
->session
;
1463 struct lttng_event
*event
;
1465 if (enabler
->event_param
.instrumentation
== LTTNG_KERNEL_SYSCALL
&&
1466 enabler
->event_param
.u
.syscall
.entryexit
== LTTNG_KERNEL_SYSCALL_ENTRYEXIT
&&
1467 enabler
->event_param
.u
.syscall
.abi
== LTTNG_KERNEL_SYSCALL_ABI_ALL
&&
1468 enabler
->event_param
.u
.syscall
.match
== LTTNG_SYSCALL_MATCH_NAME
&&
1469 !strcmp(enabler
->event_param
.name
, "*")) {
1470 if (enabler
->enabled
)
1471 WRITE_ONCE(chan
->syscall_all
, 1);
1473 WRITE_ONCE(chan
->syscall_all
, 0);
1476 /* First ensure that probe events are created for this enabler. */
1477 lttng_create_event_if_missing(enabler
);
1479 /* For each event matching enabler in session event list. */
1480 list_for_each_entry(event
, &session
->events
, list
) {
1481 struct lttng_enabler_ref
*enabler_ref
;
1483 if (!lttng_event_match_enabler(event
, enabler
))
1485 enabler_ref
= lttng_event_enabler_ref(event
, enabler
);
1488 * If no backward ref, create it.
1489 * Add backward ref from event to enabler.
1491 enabler_ref
= kzalloc(sizeof(*enabler_ref
), GFP_KERNEL
);
1494 enabler_ref
->ref
= enabler
;
1495 list_add(&enabler_ref
->node
,
1496 &event
->enablers_ref_head
);
1500 * Link filter bytecodes if not linked yet.
1502 lttng_enabler_event_link_bytecode(event
, enabler
);
1504 /* TODO: merge event context. */
1510 * Called at module load: connect the probe on all enablers matching
1512 * Called with sessions lock held.
1514 int lttng_fix_pending_events(void)
1516 struct lttng_session
*session
;
1518 list_for_each_entry(session
, &sessions
, list
)
1519 lttng_session_lazy_sync_enablers(session
);
1523 struct lttng_enabler
*lttng_enabler_create(enum lttng_enabler_type type
,
1524 struct lttng_kernel_event
*event_param
,
1525 struct lttng_channel
*chan
)
1527 struct lttng_enabler
*enabler
;
1529 enabler
= kzalloc(sizeof(*enabler
), GFP_KERNEL
);
1532 enabler
->type
= type
;
1533 INIT_LIST_HEAD(&enabler
->filter_bytecode_head
);
1534 memcpy(&enabler
->event_param
, event_param
,
1535 sizeof(enabler
->event_param
));
1536 enabler
->chan
= chan
;
1538 enabler
->enabled
= 0;
1539 enabler
->evtype
= LTTNG_TYPE_ENABLER
;
1540 mutex_lock(&sessions_mutex
);
1541 list_add(&enabler
->node
, &enabler
->chan
->session
->enablers_head
);
1542 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
1543 mutex_unlock(&sessions_mutex
);
1547 int lttng_enabler_enable(struct lttng_enabler
*enabler
)
1549 mutex_lock(&sessions_mutex
);
1550 enabler
->enabled
= 1;
1551 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
1552 mutex_unlock(&sessions_mutex
);
1556 int lttng_enabler_disable(struct lttng_enabler
*enabler
)
1558 mutex_lock(&sessions_mutex
);
1559 enabler
->enabled
= 0;
1560 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
1561 mutex_unlock(&sessions_mutex
);
1565 int lttng_enabler_attach_bytecode(struct lttng_enabler
*enabler
,
1566 struct lttng_kernel_filter_bytecode __user
*bytecode
)
1568 struct lttng_filter_bytecode_node
*bytecode_node
;
1569 uint32_t bytecode_len
;
1572 ret
= get_user(bytecode_len
, &bytecode
->len
);
1575 bytecode_node
= kzalloc(sizeof(*bytecode_node
) + bytecode_len
,
1579 ret
= copy_from_user(&bytecode_node
->bc
, bytecode
,
1580 sizeof(*bytecode
) + bytecode_len
);
1583 bytecode_node
->enabler
= enabler
;
1584 /* Enforce length based on allocated size */
1585 bytecode_node
->bc
.len
= bytecode_len
;
1586 list_add_tail(&bytecode_node
->node
, &enabler
->filter_bytecode_head
);
1587 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
1591 kfree(bytecode_node
);
1595 int lttng_event_add_callsite(struct lttng_event
*event
,
1596 struct lttng_kernel_event_callsite __user
*callsite
)
1599 switch (event
->instrumentation
) {
1600 case LTTNG_KERNEL_UPROBE
:
1601 return lttng_uprobes_add_callsite(event
, callsite
);
1607 int lttng_enabler_attach_context(struct lttng_enabler
*enabler
,
1608 struct lttng_kernel_context
*context_param
)
1614 void lttng_enabler_destroy(struct lttng_enabler
*enabler
)
1616 struct lttng_filter_bytecode_node
*filter_node
, *tmp_filter_node
;
1618 /* Destroy filter bytecode */
1619 list_for_each_entry_safe(filter_node
, tmp_filter_node
,
1620 &enabler
->filter_bytecode_head
, node
) {
1624 /* Destroy contexts */
1625 lttng_destroy_context(enabler
->ctx
);
1627 list_del(&enabler
->node
);
1632 * lttng_session_sync_enablers should be called just before starting a
1634 * Should be called with sessions mutex held.
1637 void lttng_session_sync_enablers(struct lttng_session
*session
)
1639 struct lttng_enabler
*enabler
;
1640 struct lttng_event
*event
;
1642 list_for_each_entry(enabler
, &session
->enablers_head
, node
)
1643 lttng_enabler_ref_events(enabler
);
1645 * For each event, if at least one of its enablers is enabled,
1646 * and its channel and session transient states are enabled, we
1647 * enable the event, else we disable it.
1649 list_for_each_entry(event
, &session
->events
, list
) {
1650 struct lttng_enabler_ref
*enabler_ref
;
1651 struct lttng_bytecode_runtime
*runtime
;
1652 int enabled
= 0, has_enablers_without_bytecode
= 0;
1654 switch (event
->instrumentation
) {
1655 case LTTNG_KERNEL_TRACEPOINT
:
1656 case LTTNG_KERNEL_SYSCALL
:
1658 list_for_each_entry(enabler_ref
,
1659 &event
->enablers_ref_head
, node
) {
1660 if (enabler_ref
->ref
->enabled
) {
1667 /* Not handled with lazy sync. */
1671 * Enabled state is based on union of enablers, with
1672 * intesection of session and channel transient enable
1675 enabled
= enabled
&& session
->tstate
&& event
->chan
->tstate
;
1677 WRITE_ONCE(event
->enabled
, enabled
);
1679 * Sync tracepoint registration with event enabled
1683 register_event(event
);
1685 _lttng_event_unregister(event
);
1688 /* Check if has enablers without bytecode enabled */
1689 list_for_each_entry(enabler_ref
,
1690 &event
->enablers_ref_head
, node
) {
1691 if (enabler_ref
->ref
->enabled
1692 && list_empty(&enabler_ref
->ref
->filter_bytecode_head
)) {
1693 has_enablers_without_bytecode
= 1;
1697 event
->has_enablers_without_bytecode
=
1698 has_enablers_without_bytecode
;
1700 /* Enable filters */
1701 list_for_each_entry(runtime
,
1702 &event
->bytecode_runtime_head
, node
)
1703 lttng_filter_sync_state(runtime
);
1708 * Apply enablers to session events, adding events to session if need
1709 * be. It is required after each modification applied to an active
1710 * session, and right before session "start".
1711 * "lazy" sync means we only sync if required.
1712 * Should be called with sessions mutex held.
1715 void lttng_session_lazy_sync_enablers(struct lttng_session
*session
)
1717 /* We can skip if session is not active */
1718 if (!session
->active
)
1720 lttng_session_sync_enablers(session
);
1724 * Serialize at most one packet worth of metadata into a metadata
1726 * We grab the metadata cache mutex to get exclusive access to our metadata
1727 * buffer and to the metadata cache. Exclusive access to the metadata buffer
1728 * allows us to do racy operations such as looking for remaining space left in
1729 * packet and write, since mutual exclusion protects us from concurrent writes.
1730 * Mutual exclusion on the metadata cache allow us to read the cache content
1731 * without racing against reallocation of the cache by updates.
1732 * Returns the number of bytes written in the channel, 0 if no data
1733 * was written and a negative value on error.
1735 int lttng_metadata_output_channel(struct lttng_metadata_stream
*stream
,
1736 struct channel
*chan
, bool *coherent
)
1738 struct lib_ring_buffer_ctx ctx
;
1740 size_t len
, reserve_len
;
1743 * Ensure we support mutiple get_next / put sequences followed by
1744 * put_next. The metadata cache lock protects reading the metadata
1745 * cache. It can indeed be read concurrently by "get_next_subbuf" and
1746 * "flush" operations on the buffer invoked by different processes.
1747 * Moreover, since the metadata cache memory can be reallocated, we
1748 * need to have exclusive access against updates even though we only
1751 mutex_lock(&stream
->metadata_cache
->lock
);
1752 WARN_ON(stream
->metadata_in
< stream
->metadata_out
);
1753 if (stream
->metadata_in
!= stream
->metadata_out
)
1756 /* Metadata regenerated, change the version. */
1757 if (stream
->metadata_cache
->version
!= stream
->version
)
1758 stream
->version
= stream
->metadata_cache
->version
;
1760 len
= stream
->metadata_cache
->metadata_written
-
1761 stream
->metadata_in
;
1764 reserve_len
= min_t(size_t,
1765 stream
->transport
->ops
.packet_avail_size(chan
),
1767 lib_ring_buffer_ctx_init(&ctx
, chan
, NULL
, reserve_len
,
1770 * If reservation failed, return an error to the caller.
1772 ret
= stream
->transport
->ops
.event_reserve(&ctx
, 0);
1774 printk(KERN_WARNING
"LTTng: Metadata event reservation failed\n");
1775 stream
->coherent
= false;
1778 stream
->transport
->ops
.event_write(&ctx
,
1779 stream
->metadata_cache
->data
+ stream
->metadata_in
,
1781 stream
->transport
->ops
.event_commit(&ctx
);
1782 stream
->metadata_in
+= reserve_len
;
1783 if (reserve_len
< len
)
1784 stream
->coherent
= false;
1786 stream
->coherent
= true;
1791 *coherent
= stream
->coherent
;
1792 mutex_unlock(&stream
->metadata_cache
->lock
);
1797 void lttng_metadata_begin(struct lttng_session
*session
)
1799 if (atomic_inc_return(&session
->metadata_cache
->producing
) == 1)
1800 mutex_lock(&session
->metadata_cache
->lock
);
1804 void lttng_metadata_end(struct lttng_session
*session
)
1806 WARN_ON_ONCE(!atomic_read(&session
->metadata_cache
->producing
));
1807 if (atomic_dec_return(&session
->metadata_cache
->producing
) == 0) {
1808 struct lttng_metadata_stream
*stream
;
1810 list_for_each_entry(stream
, &session
->metadata_cache
->metadata_stream
, list
)
1811 wake_up_interruptible(&stream
->read_wait
);
1812 mutex_unlock(&session
->metadata_cache
->lock
);
1817 * Write the metadata to the metadata cache.
1818 * Must be called with sessions_mutex held.
1819 * The metadata cache lock protects us from concurrent read access from
1820 * thread outputting metadata content to ring buffer.
1821 * The content of the printf is printed as a single atomic metadata
1824 int lttng_metadata_printf(struct lttng_session
*session
,
1825 const char *fmt
, ...)
1831 WARN_ON_ONCE(!LTTNG_READ_ONCE(session
->active
));
1834 str
= kvasprintf(GFP_KERNEL
, fmt
, ap
);
1840 WARN_ON_ONCE(!atomic_read(&session
->metadata_cache
->producing
));
1841 if (session
->metadata_cache
->metadata_written
+ len
>
1842 session
->metadata_cache
->cache_alloc
) {
1843 char *tmp_cache_realloc
;
1844 unsigned int tmp_cache_alloc_size
;
1846 tmp_cache_alloc_size
= max_t(unsigned int,
1847 session
->metadata_cache
->cache_alloc
+ len
,
1848 session
->metadata_cache
->cache_alloc
<< 1);
1849 tmp_cache_realloc
= vzalloc(tmp_cache_alloc_size
);
1850 if (!tmp_cache_realloc
)
1852 if (session
->metadata_cache
->data
) {
1853 memcpy(tmp_cache_realloc
,
1854 session
->metadata_cache
->data
,
1855 session
->metadata_cache
->cache_alloc
);
1856 vfree(session
->metadata_cache
->data
);
1859 session
->metadata_cache
->cache_alloc
= tmp_cache_alloc_size
;
1860 session
->metadata_cache
->data
= tmp_cache_realloc
;
1862 memcpy(session
->metadata_cache
->data
+
1863 session
->metadata_cache
->metadata_written
,
1865 session
->metadata_cache
->metadata_written
+= len
;
1876 int print_tabs(struct lttng_session
*session
, size_t nesting
)
1880 for (i
= 0; i
< nesting
; i
++) {
1883 ret
= lttng_metadata_printf(session
, " ");
1892 * Must be called with sessions_mutex held.
1895 int _lttng_struct_type_statedump(struct lttng_session
*session
,
1896 const struct lttng_type
*type
,
1900 uint32_t i
, nr_fields
;
1902 ret
= print_tabs(session
, nesting
);
1905 ret
= lttng_metadata_printf(session
,
1909 nr_fields
= type
->u
._struct
.nr_fields
;
1910 for (i
= 0; i
< nr_fields
; i
++) {
1911 const struct lttng_event_field
*iter_field
;
1913 iter_field
= &type
->u
._struct
.fields
[i
];
1914 ret
= _lttng_field_statedump(session
, iter_field
, nesting
+ 1);
1918 ret
= print_tabs(session
, nesting
);
1921 ret
= lttng_metadata_printf(session
,
1927 * Must be called with sessions_mutex held.
1930 int _lttng_struct_statedump(struct lttng_session
*session
,
1931 const struct lttng_event_field
*field
,
1936 ret
= _lttng_struct_type_statedump(session
,
1937 &field
->type
, nesting
);
1940 ret
= lttng_metadata_printf(session
,
1947 * Must be called with sessions_mutex held.
1950 int _lttng_variant_type_statedump(struct lttng_session
*session
,
1951 const struct lttng_type
*type
,
1955 uint32_t i
, nr_choices
;
1957 ret
= print_tabs(session
, nesting
);
1960 ret
= lttng_metadata_printf(session
,
1961 "variant <_%s> {\n",
1962 type
->u
.variant
.tag_name
);
1965 nr_choices
= type
->u
.variant
.nr_choices
;
1966 for (i
= 0; i
< nr_choices
; i
++) {
1967 const struct lttng_event_field
*iter_field
;
1969 iter_field
= &type
->u
.variant
.choices
[i
];
1970 ret
= _lttng_field_statedump(session
, iter_field
, nesting
+ 1);
1974 ret
= print_tabs(session
, nesting
);
1977 ret
= lttng_metadata_printf(session
,
1983 * Must be called with sessions_mutex held.
1986 int _lttng_variant_statedump(struct lttng_session
*session
,
1987 const struct lttng_event_field
*field
,
1992 ret
= _lttng_variant_type_statedump(session
,
1993 &field
->type
, nesting
);
1996 ret
= lttng_metadata_printf(session
,
2003 * Must be called with sessions_mutex held.
2006 int _lttng_array_compound_statedump(struct lttng_session
*session
,
2007 const struct lttng_event_field
*field
,
2011 const struct lttng_type
*elem_type
;
2013 /* Only array of structures and variants are currently supported. */
2014 elem_type
= field
->type
.u
.array_compound
.elem_type
;
2015 switch (elem_type
->atype
) {
2017 ret
= _lttng_struct_type_statedump(session
, elem_type
, nesting
);
2022 ret
= _lttng_variant_type_statedump(session
, elem_type
, nesting
);
2029 ret
= lttng_metadata_printf(session
,
2032 field
->type
.u
.array_compound
.length
);
2037 * Must be called with sessions_mutex held.
2040 int _lttng_sequence_compound_statedump(struct lttng_session
*session
,
2041 const struct lttng_event_field
*field
,
2045 const char *length_name
;
2046 const struct lttng_type
*elem_type
;
2048 length_name
= field
->type
.u
.sequence_compound
.length_name
;
2050 /* Only array of structures and variants are currently supported. */
2051 elem_type
= field
->type
.u
.sequence_compound
.elem_type
;
2052 switch (elem_type
->atype
) {
2054 ret
= _lttng_struct_type_statedump(session
, elem_type
, nesting
);
2059 ret
= _lttng_variant_type_statedump(session
, elem_type
, nesting
);
2066 ret
= lttng_metadata_printf(session
,
2074 * Must be called with sessions_mutex held.
2077 int _lttng_enum_statedump(struct lttng_session
*session
,
2078 const struct lttng_event_field
*field
,
2081 const struct lttng_enum_desc
*enum_desc
;
2082 const struct lttng_integer_type
*container_type
;
2084 unsigned int i
, nr_entries
;
2086 enum_desc
= field
->type
.u
.basic
.enumeration
.desc
;
2087 container_type
= &field
->type
.u
.basic
.enumeration
.container_type
;
2088 nr_entries
= enum_desc
->nr_entries
;
2090 ret
= print_tabs(session
, nesting
);
2093 ret
= lttng_metadata_printf(session
,
2094 "enum : integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } {\n",
2095 container_type
->size
,
2096 container_type
->alignment
,
2097 container_type
->signedness
,
2098 (container_type
->encoding
== lttng_encode_none
)
2100 : (container_type
->encoding
== lttng_encode_UTF8
)
2103 container_type
->base
,
2104 #if __BYTE_ORDER == __BIG_ENDIAN
2105 container_type
->reverse_byte_order
? " byte_order = le;" : ""
2107 container_type
->reverse_byte_order
? " byte_order = be;" : ""
2112 /* Dump all entries */
2113 for (i
= 0; i
< nr_entries
; i
++) {
2114 const struct lttng_enum_entry
*entry
= &enum_desc
->entries
[i
];
2117 ret
= print_tabs(session
, nesting
+ 1);
2120 ret
= lttng_metadata_printf(session
,
2124 len
= strlen(entry
->string
);
2125 /* Escape the character '"' */
2126 for (j
= 0; j
< len
; j
++) {
2127 char c
= entry
->string
[j
];
2131 ret
= lttng_metadata_printf(session
,
2135 ret
= lttng_metadata_printf(session
,
2139 ret
= lttng_metadata_printf(session
,
2146 ret
= lttng_metadata_printf(session
, "\"");
2150 if (entry
->options
.is_auto
) {
2151 ret
= lttng_metadata_printf(session
, ",\n");
2155 ret
= lttng_metadata_printf(session
,
2159 if (entry
->start
.signedness
)
2160 ret
= lttng_metadata_printf(session
,
2161 "%lld", (long long) entry
->start
.value
);
2163 ret
= lttng_metadata_printf(session
,
2164 "%llu", entry
->start
.value
);
2167 if (entry
->start
.signedness
== entry
->end
.signedness
&&
2169 == entry
->end
.value
) {
2170 ret
= lttng_metadata_printf(session
,
2173 if (entry
->end
.signedness
) {
2174 ret
= lttng_metadata_printf(session
,
2176 (long long) entry
->end
.value
);
2178 ret
= lttng_metadata_printf(session
,
2187 ret
= print_tabs(session
, nesting
);
2190 ret
= lttng_metadata_printf(session
, "} _%s;\n",
2197 * Must be called with sessions_mutex held.
2200 int _lttng_field_statedump(struct lttng_session
*session
,
2201 const struct lttng_event_field
*field
,
2206 switch (field
->type
.atype
) {
2208 ret
= print_tabs(session
, nesting
);
2211 ret
= lttng_metadata_printf(session
,
2212 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n",
2213 field
->type
.u
.basic
.integer
.size
,
2214 field
->type
.u
.basic
.integer
.alignment
,
2215 field
->type
.u
.basic
.integer
.signedness
,
2216 (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_none
)
2218 : (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
2221 field
->type
.u
.basic
.integer
.base
,
2222 #if __BYTE_ORDER == __BIG_ENDIAN
2223 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
2225 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
2230 ret
= _lttng_enum_statedump(session
, field
, nesting
);
2233 case atype_array_bitfield
:
2235 const struct lttng_basic_type
*elem_type
;
2237 elem_type
= &field
->type
.u
.array
.elem_type
;
2238 if (field
->type
.u
.array
.elem_alignment
) {
2239 ret
= print_tabs(session
, nesting
);
2242 ret
= lttng_metadata_printf(session
,
2243 "struct { } align(%u) _%s_padding;\n",
2244 field
->type
.u
.array
.elem_alignment
* CHAR_BIT
,
2249 ret
= print_tabs(session
, nesting
);
2252 ret
= lttng_metadata_printf(session
,
2253 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n",
2254 elem_type
->u
.basic
.integer
.size
,
2255 elem_type
->u
.basic
.integer
.alignment
,
2256 elem_type
->u
.basic
.integer
.signedness
,
2257 (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
2259 : (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
2262 elem_type
->u
.basic
.integer
.base
,
2263 #if __BYTE_ORDER == __BIG_ENDIAN
2264 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
2266 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
2268 field
->name
, field
->type
.u
.array
.length
);
2271 case atype_sequence
:
2272 case atype_sequence_bitfield
:
2274 const struct lttng_basic_type
*elem_type
;
2275 const struct lttng_basic_type
*length_type
;
2277 elem_type
= &field
->type
.u
.sequence
.elem_type
;
2278 length_type
= &field
->type
.u
.sequence
.length_type
;
2279 ret
= print_tabs(session
, nesting
);
2282 ret
= lttng_metadata_printf(session
,
2283 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n",
2284 length_type
->u
.basic
.integer
.size
,
2285 (unsigned int) length_type
->u
.basic
.integer
.alignment
,
2286 length_type
->u
.basic
.integer
.signedness
,
2287 (length_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
2289 : ((length_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
2292 length_type
->u
.basic
.integer
.base
,
2293 #if __BYTE_ORDER == __BIG_ENDIAN
2294 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
2296 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
2302 if (field
->type
.u
.sequence
.elem_alignment
) {
2303 ret
= print_tabs(session
, nesting
);
2306 ret
= lttng_metadata_printf(session
,
2307 "struct { } align(%u) _%s_padding;\n",
2308 field
->type
.u
.sequence
.elem_alignment
* CHAR_BIT
,
2313 ret
= print_tabs(session
, nesting
);
2316 ret
= lttng_metadata_printf(session
,
2317 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n",
2318 elem_type
->u
.basic
.integer
.size
,
2319 (unsigned int) elem_type
->u
.basic
.integer
.alignment
,
2320 elem_type
->u
.basic
.integer
.signedness
,
2321 (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
2323 : ((elem_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
2326 elem_type
->u
.basic
.integer
.base
,
2327 #if __BYTE_ORDER == __BIG_ENDIAN
2328 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
2330 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
2338 /* Default encoding is UTF8 */
2339 ret
= print_tabs(session
, nesting
);
2342 ret
= lttng_metadata_printf(session
,
2344 field
->type
.u
.basic
.string
.encoding
== lttng_encode_ASCII
?
2345 " { encoding = ASCII; }" : "",
2349 ret
= _lttng_struct_statedump(session
, field
, nesting
);
2351 case atype_array_compound
:
2352 ret
= _lttng_array_compound_statedump(session
, field
, nesting
);
2354 case atype_sequence_compound
:
2355 ret
= _lttng_sequence_compound_statedump(session
, field
, nesting
);
2358 ret
= _lttng_variant_statedump(session
, field
, nesting
);
2369 int _lttng_context_metadata_statedump(struct lttng_session
*session
,
2370 struct lttng_ctx
*ctx
)
2377 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
2378 const struct lttng_ctx_field
*field
= &ctx
->fields
[i
];
2380 ret
= _lttng_field_statedump(session
, &field
->event_field
, 2);
2388 int _lttng_fields_metadata_statedump(struct lttng_session
*session
,
2389 struct lttng_event
*event
)
2391 const struct lttng_event_desc
*desc
= event
->desc
;
2395 for (i
= 0; i
< desc
->nr_fields
; i
++) {
2396 const struct lttng_event_field
*field
= &desc
->fields
[i
];
2398 ret
= _lttng_field_statedump(session
, field
, 2);
2406 * Must be called with sessions_mutex held.
2407 * The entire event metadata is printed as a single atomic metadata
2411 int _lttng_event_metadata_statedump(struct lttng_session
*session
,
2412 struct lttng_channel
*chan
,
2413 struct lttng_event
*event
)
2417 if (event
->metadata_dumped
|| !LTTNG_READ_ONCE(session
->active
))
2419 if (chan
->channel_type
== METADATA_CHANNEL
)
2422 lttng_metadata_begin(session
);
2424 ret
= lttng_metadata_printf(session
,
2428 " stream_id = %u;\n",
2436 ret
= lttng_metadata_printf(session
,
2437 " context := struct {\n");
2441 ret
= _lttng_context_metadata_statedump(session
, event
->ctx
);
2445 ret
= lttng_metadata_printf(session
,
2451 ret
= lttng_metadata_printf(session
,
2452 " fields := struct {\n"
2457 ret
= _lttng_fields_metadata_statedump(session
, event
);
2462 * LTTng space reservation can only reserve multiples of the
2465 ret
= lttng_metadata_printf(session
,
2471 event
->metadata_dumped
= 1;
2473 lttng_metadata_end(session
);
2479 * Must be called with sessions_mutex held.
2480 * The entire channel metadata is printed as a single atomic metadata
2484 int _lttng_channel_metadata_statedump(struct lttng_session
*session
,
2485 struct lttng_channel
*chan
)
2489 if (chan
->metadata_dumped
|| !LTTNG_READ_ONCE(session
->active
))
2492 if (chan
->channel_type
== METADATA_CHANNEL
)
2495 lttng_metadata_begin(session
);
2497 WARN_ON_ONCE(!chan
->header_type
);
2498 ret
= lttng_metadata_printf(session
,
2501 " event.header := %s;\n"
2502 " packet.context := struct packet_context;\n",
2504 chan
->header_type
== 1 ? "struct event_header_compact" :
2505 "struct event_header_large");
2510 ret
= lttng_metadata_printf(session
,
2511 " event.context := struct {\n");
2515 ret
= _lttng_context_metadata_statedump(session
, chan
->ctx
);
2519 ret
= lttng_metadata_printf(session
,
2525 ret
= lttng_metadata_printf(session
,
2528 chan
->metadata_dumped
= 1;
2530 lttng_metadata_end(session
);
2535 * Must be called with sessions_mutex held.
2538 int _lttng_stream_packet_context_declare(struct lttng_session
*session
)
2540 return lttng_metadata_printf(session
,
2541 "struct packet_context {\n"
2542 " uint64_clock_monotonic_t timestamp_begin;\n"
2543 " uint64_clock_monotonic_t timestamp_end;\n"
2544 " uint64_t content_size;\n"
2545 " uint64_t packet_size;\n"
2546 " uint64_t packet_seq_num;\n"
2547 " unsigned long events_discarded;\n"
2548 " uint32_t cpu_id;\n"
2555 * id: range: 0 - 30.
2556 * id 31 is reserved to indicate an extended header.
2559 * id: range: 0 - 65534.
2560 * id 65535 is reserved to indicate an extended header.
2562 * Must be called with sessions_mutex held.
2565 int _lttng_event_header_declare(struct lttng_session
*session
)
2567 return lttng_metadata_printf(session
,
2568 "struct event_header_compact {\n"
2569 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
2572 " uint27_clock_monotonic_t timestamp;\n"
2576 " uint64_clock_monotonic_t timestamp;\n"
2581 "struct event_header_large {\n"
2582 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
2585 " uint32_clock_monotonic_t timestamp;\n"
2589 " uint64_clock_monotonic_t timestamp;\n"
2593 lttng_alignof(uint32_t) * CHAR_BIT
,
2594 lttng_alignof(uint16_t) * CHAR_BIT
2599 * Approximation of NTP time of day to clock monotonic correlation,
2600 * taken at start of trace.
2601 * Yes, this is only an approximation. Yes, we can (and will) do better
2602 * in future versions.
2603 * This function may return a negative offset. It may happen if the
2604 * system sets the REALTIME clock to 0 after boot.
2606 * Use 64bit timespec on kernels that have it, this makes 32bit arch
2610 int64_t measure_clock_offset(void)
2612 uint64_t monotonic_avg
, monotonic
[2], realtime
;
2613 uint64_t tcf
= trace_clock_freq();
2615 unsigned long flags
;
2616 #ifdef LTTNG_KERNEL_HAS_TIMESPEC64
2617 struct timespec64 rts
= { 0, 0 };
2619 struct timespec rts
= { 0, 0 };
2622 /* Disable interrupts to increase correlation precision. */
2623 local_irq_save(flags
);
2624 monotonic
[0] = trace_clock_read64();
2625 #ifdef LTTNG_KERNEL_HAS_TIMESPEC64
2626 ktime_get_real_ts64(&rts
);
2628 getnstimeofday(&rts
);
2630 monotonic
[1] = trace_clock_read64();
2631 local_irq_restore(flags
);
2633 monotonic_avg
= (monotonic
[0] + monotonic
[1]) >> 1;
2634 realtime
= (uint64_t) rts
.tv_sec
* tcf
;
2635 if (tcf
== NSEC_PER_SEC
) {
2636 realtime
+= rts
.tv_nsec
;
2638 uint64_t n
= rts
.tv_nsec
* tcf
;
2640 do_div(n
, NSEC_PER_SEC
);
2643 offset
= (int64_t) realtime
- monotonic_avg
;
2648 int print_escaped_ctf_string(struct lttng_session
*session
, const char *string
)
2656 while (cur
!= '\0') {
2659 ret
= lttng_metadata_printf(session
, "%s", "\\n");
2663 ret
= lttng_metadata_printf(session
, "%c", '\\');
2666 /* We still print the current char */
2669 ret
= lttng_metadata_printf(session
, "%c", cur
);
2683 int print_metadata_escaped_field(struct lttng_session
*session
, const char *field
,
2684 const char *field_value
)
2688 ret
= lttng_metadata_printf(session
, " %s = \"", field
);
2692 ret
= print_escaped_ctf_string(session
, field_value
);
2696 ret
= lttng_metadata_printf(session
, "\";\n");
2703 * Output metadata into this session's metadata buffers.
2704 * Must be called with sessions_mutex held.
2707 int _lttng_session_metadata_statedump(struct lttng_session
*session
)
2709 unsigned char *uuid_c
= session
->uuid
.b
;
2710 unsigned char uuid_s
[37], clock_uuid_s
[BOOT_ID_LEN
];
2711 const char *product_uuid
;
2712 struct lttng_channel
*chan
;
2713 struct lttng_event
*event
;
2716 if (!LTTNG_READ_ONCE(session
->active
))
2719 lttng_metadata_begin(session
);
2721 if (session
->metadata_dumped
)
2724 snprintf(uuid_s
, sizeof(uuid_s
),
2725 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
2726 uuid_c
[0], uuid_c
[1], uuid_c
[2], uuid_c
[3],
2727 uuid_c
[4], uuid_c
[5], uuid_c
[6], uuid_c
[7],
2728 uuid_c
[8], uuid_c
[9], uuid_c
[10], uuid_c
[11],
2729 uuid_c
[12], uuid_c
[13], uuid_c
[14], uuid_c
[15]);
2731 ret
= lttng_metadata_printf(session
,
2732 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
2733 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
2734 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
2735 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
2736 "typealias integer { size = %u; align = %u; signed = false; } := unsigned long;\n"
2737 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
2738 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
2744 " byte_order = %s;\n"
2745 " packet.header := struct {\n"
2746 " uint32_t magic;\n"
2747 " uint8_t uuid[16];\n"
2748 " uint32_t stream_id;\n"
2749 " uint64_t stream_instance_id;\n"
2752 lttng_alignof(uint8_t) * CHAR_BIT
,
2753 lttng_alignof(uint16_t) * CHAR_BIT
,
2754 lttng_alignof(uint32_t) * CHAR_BIT
,
2755 lttng_alignof(uint64_t) * CHAR_BIT
,
2756 sizeof(unsigned long) * CHAR_BIT
,
2757 lttng_alignof(unsigned long) * CHAR_BIT
,
2761 #if __BYTE_ORDER == __BIG_ENDIAN
2770 ret
= lttng_metadata_printf(session
,
2772 " hostname = \"%s\";\n"
2773 " domain = \"kernel\";\n"
2774 " sysname = \"%s\";\n"
2775 " kernel_release = \"%s\";\n"
2776 " kernel_version = \"%s\";\n"
2777 " tracer_name = \"lttng-modules\";\n"
2778 " tracer_major = %d;\n"
2779 " tracer_minor = %d;\n"
2780 " tracer_patchlevel = %d;\n"
2781 " trace_buffering_scheme = \"global\";\n",
2782 current
->nsproxy
->uts_ns
->name
.nodename
,
2786 LTTNG_MODULES_MAJOR_VERSION
,
2787 LTTNG_MODULES_MINOR_VERSION
,
2788 LTTNG_MODULES_PATCHLEVEL_VERSION
2793 ret
= print_metadata_escaped_field(session
, "trace_name", session
->name
);
2796 ret
= print_metadata_escaped_field(session
, "trace_creation_datetime",
2797 session
->creation_time
);
2801 /* Add the product UUID to the 'env' section */
2802 product_uuid
= dmi_get_system_info(DMI_PRODUCT_UUID
);
2804 ret
= lttng_metadata_printf(session
,
2805 " product_uuid = \"%s\";\n",
2812 /* Close the 'env' section */
2813 ret
= lttng_metadata_printf(session
, "};\n\n");
2817 ret
= lttng_metadata_printf(session
,
2819 " name = \"%s\";\n",
2825 if (!trace_clock_uuid(clock_uuid_s
)) {
2826 ret
= lttng_metadata_printf(session
,
2827 " uuid = \"%s\";\n",
2834 ret
= lttng_metadata_printf(session
,
2835 " description = \"%s\";\n"
2836 " freq = %llu; /* Frequency, in Hz */\n"
2837 " /* clock value offset from Epoch is: offset * (1/freq) */\n"
2840 trace_clock_description(),
2841 (unsigned long long) trace_clock_freq(),
2842 (long long) measure_clock_offset()
2847 ret
= lttng_metadata_printf(session
,
2848 "typealias integer {\n"
2849 " size = 27; align = 1; signed = false;\n"
2850 " map = clock.%s.value;\n"
2851 "} := uint27_clock_monotonic_t;\n"
2853 "typealias integer {\n"
2854 " size = 32; align = %u; signed = false;\n"
2855 " map = clock.%s.value;\n"
2856 "} := uint32_clock_monotonic_t;\n"
2858 "typealias integer {\n"
2859 " size = 64; align = %u; signed = false;\n"
2860 " map = clock.%s.value;\n"
2861 "} := uint64_clock_monotonic_t;\n\n",
2863 lttng_alignof(uint32_t) * CHAR_BIT
,
2865 lttng_alignof(uint64_t) * CHAR_BIT
,
2871 ret
= _lttng_stream_packet_context_declare(session
);
2875 ret
= _lttng_event_header_declare(session
);
2880 list_for_each_entry(chan
, &session
->chan
, list
) {
2881 ret
= _lttng_channel_metadata_statedump(session
, chan
);
2886 list_for_each_entry(event
, &session
->events
, list
) {
2887 ret
= _lttng_event_metadata_statedump(session
, event
->chan
, event
);
2891 session
->metadata_dumped
= 1;
2893 lttng_metadata_end(session
);
2898 * lttng_transport_register - LTT transport registration
2899 * @transport: transport structure
2901 * Registers a transport which can be used as output to extract the data out of
2902 * LTTng. The module calling this registration function must ensure that no
2903 * trap-inducing code will be executed by the transport functions. E.g.
2904 * vmalloc_sync_mappings() must be called between a vmalloc and the moment the memory
2905 * is made visible to the transport function. This registration acts as a
2906 * vmalloc_sync_mappings. Therefore, only if the module allocates virtual memory
2907 * after its registration must it synchronize the TLBs.
2909 void lttng_transport_register(struct lttng_transport
*transport
)
2912 * Make sure no page fault can be triggered by the module about to be
2913 * registered. We deal with this here so we don't have to call
2914 * vmalloc_sync_mappings() in each module's init.
2916 wrapper_vmalloc_sync_mappings();
2918 mutex_lock(&sessions_mutex
);
2919 list_add_tail(&transport
->node
, <tng_transport_list
);
2920 mutex_unlock(&sessions_mutex
);
2922 EXPORT_SYMBOL_GPL(lttng_transport_register
);
2925 * lttng_transport_unregister - LTT transport unregistration
2926 * @transport: transport structure
2928 void lttng_transport_unregister(struct lttng_transport
*transport
)
2930 mutex_lock(&sessions_mutex
);
2931 list_del(&transport
->node
);
2932 mutex_unlock(&sessions_mutex
);
2934 EXPORT_SYMBOL_GPL(lttng_transport_unregister
);
2936 #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,10,0))
2938 enum cpuhp_state lttng_hp_prepare
;
2939 enum cpuhp_state lttng_hp_online
;
2941 static int lttng_hotplug_prepare(unsigned int cpu
, struct hlist_node
*node
)
2943 struct lttng_cpuhp_node
*lttng_node
;
2945 lttng_node
= container_of(node
, struct lttng_cpuhp_node
, node
);
2946 switch (lttng_node
->component
) {
2947 case LTTNG_RING_BUFFER_FRONTEND
:
2949 case LTTNG_RING_BUFFER_BACKEND
:
2950 return lttng_cpuhp_rb_backend_prepare(cpu
, lttng_node
);
2951 case LTTNG_RING_BUFFER_ITER
:
2953 case LTTNG_CONTEXT_PERF_COUNTERS
:
2960 static int lttng_hotplug_dead(unsigned int cpu
, struct hlist_node
*node
)
2962 struct lttng_cpuhp_node
*lttng_node
;
2964 lttng_node
= container_of(node
, struct lttng_cpuhp_node
, node
);
2965 switch (lttng_node
->component
) {
2966 case LTTNG_RING_BUFFER_FRONTEND
:
2967 return lttng_cpuhp_rb_frontend_dead(cpu
, lttng_node
);
2968 case LTTNG_RING_BUFFER_BACKEND
:
2970 case LTTNG_RING_BUFFER_ITER
:
2972 case LTTNG_CONTEXT_PERF_COUNTERS
:
2973 return lttng_cpuhp_perf_counter_dead(cpu
, lttng_node
);
2979 static int lttng_hotplug_online(unsigned int cpu
, struct hlist_node
*node
)
2981 struct lttng_cpuhp_node
*lttng_node
;
2983 lttng_node
= container_of(node
, struct lttng_cpuhp_node
, node
);
2984 switch (lttng_node
->component
) {
2985 case LTTNG_RING_BUFFER_FRONTEND
:
2986 return lttng_cpuhp_rb_frontend_online(cpu
, lttng_node
);
2987 case LTTNG_RING_BUFFER_BACKEND
:
2989 case LTTNG_RING_BUFFER_ITER
:
2990 return lttng_cpuhp_rb_iter_online(cpu
, lttng_node
);
2991 case LTTNG_CONTEXT_PERF_COUNTERS
:
2992 return lttng_cpuhp_perf_counter_online(cpu
, lttng_node
);
2998 static int lttng_hotplug_offline(unsigned int cpu
, struct hlist_node
*node
)
3000 struct lttng_cpuhp_node
*lttng_node
;
3002 lttng_node
= container_of(node
, struct lttng_cpuhp_node
, node
);
3003 switch (lttng_node
->component
) {
3004 case LTTNG_RING_BUFFER_FRONTEND
:
3005 return lttng_cpuhp_rb_frontend_offline(cpu
, lttng_node
);
3006 case LTTNG_RING_BUFFER_BACKEND
:
3008 case LTTNG_RING_BUFFER_ITER
:
3010 case LTTNG_CONTEXT_PERF_COUNTERS
:
3017 static int __init
lttng_init_cpu_hotplug(void)
3021 ret
= cpuhp_setup_state_multi(CPUHP_BP_PREPARE_DYN
, "lttng:prepare",
3022 lttng_hotplug_prepare
,
3023 lttng_hotplug_dead
);
3027 lttng_hp_prepare
= ret
;
3028 lttng_rb_set_hp_prepare(ret
);
3030 ret
= cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN
, "lttng:online",
3031 lttng_hotplug_online
,
3032 lttng_hotplug_offline
);
3034 cpuhp_remove_multi_state(lttng_hp_prepare
);
3035 lttng_hp_prepare
= 0;
3038 lttng_hp_online
= ret
;
3039 lttng_rb_set_hp_online(ret
);
3044 static void __exit
lttng_exit_cpu_hotplug(void)
3046 lttng_rb_set_hp_online(0);
3047 cpuhp_remove_multi_state(lttng_hp_online
);
3048 lttng_rb_set_hp_prepare(0);
3049 cpuhp_remove_multi_state(lttng_hp_prepare
);
3052 #else /* #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,10,0)) */
3053 static int lttng_init_cpu_hotplug(void)
3057 static void lttng_exit_cpu_hotplug(void)
3060 #endif /* #else #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,10,0)) */
3063 static int __init
lttng_events_init(void)
3067 ret
= wrapper_lttng_fixup_sig(THIS_MODULE
);
3070 ret
= wrapper_get_pfnblock_flags_mask_init();
3073 ret
= wrapper_get_pageblock_flags_mask_init();
3076 ret
= lttng_probes_init();
3079 ret
= lttng_context_init();
3082 ret
= lttng_tracepoint_init();
3085 event_cache
= KMEM_CACHE(lttng_event
, 0);
3090 ret
= lttng_abi_init();
3093 ret
= lttng_logger_init();
3096 ret
= lttng_init_cpu_hotplug();
3099 printk(KERN_NOTICE
"LTTng: Loaded modules v%s.%s.%s%s (%s)%s%s\n",
3100 __stringify(LTTNG_MODULES_MAJOR_VERSION
),
3101 __stringify(LTTNG_MODULES_MINOR_VERSION
),
3102 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION
),
3103 LTTNG_MODULES_EXTRAVERSION
,
3105 #ifdef LTTNG_EXTRA_VERSION_GIT
3106 LTTNG_EXTRA_VERSION_GIT
[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_GIT
,
3110 #ifdef LTTNG_EXTRA_VERSION_NAME
3111 LTTNG_EXTRA_VERSION_NAME
[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_NAME
);
3115 #ifdef CONFIG_LTTNG_EXPERIMENTAL_BITWISE_ENUM
3116 printk(KERN_NOTICE
"LTTng: Experimental bitwise enum enabled.\n");
3117 #endif /* CONFIG_LTTNG_EXPERIMENTAL_BITWISE_ENUM */
3121 lttng_logger_exit();
3125 kmem_cache_destroy(event_cache
);
3127 lttng_tracepoint_exit();
3129 lttng_context_exit();
3130 printk(KERN_NOTICE
"LTTng: Failed to load modules v%s.%s.%s%s (%s)%s%s\n",
3131 __stringify(LTTNG_MODULES_MAJOR_VERSION
),
3132 __stringify(LTTNG_MODULES_MINOR_VERSION
),
3133 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION
),
3134 LTTNG_MODULES_EXTRAVERSION
,
3136 #ifdef LTTNG_EXTRA_VERSION_GIT
3137 LTTNG_EXTRA_VERSION_GIT
[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_GIT
,
3141 #ifdef LTTNG_EXTRA_VERSION_NAME
3142 LTTNG_EXTRA_VERSION_NAME
[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_NAME
);
3149 module_init(lttng_events_init
);
3151 static void __exit
lttng_events_exit(void)
3153 struct lttng_session
*session
, *tmpsession
;
3155 lttng_exit_cpu_hotplug();
3156 lttng_logger_exit();
3158 list_for_each_entry_safe(session
, tmpsession
, &sessions
, list
)
3159 lttng_session_destroy(session
);
3160 kmem_cache_destroy(event_cache
);
3161 lttng_tracepoint_exit();
3162 lttng_context_exit();
3163 printk(KERN_NOTICE
"LTTng: Unloaded modules v%s.%s.%s%s (%s)%s%s\n",
3164 __stringify(LTTNG_MODULES_MAJOR_VERSION
),
3165 __stringify(LTTNG_MODULES_MINOR_VERSION
),
3166 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION
),
3167 LTTNG_MODULES_EXTRAVERSION
,
3169 #ifdef LTTNG_EXTRA_VERSION_GIT
3170 LTTNG_EXTRA_VERSION_GIT
[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_GIT
,
3174 #ifdef LTTNG_EXTRA_VERSION_NAME
3175 LTTNG_EXTRA_VERSION_NAME
[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_NAME
);
3181 module_exit(lttng_events_exit
);
3183 #include "extra_version/patches.i"
3184 #ifdef LTTNG_EXTRA_VERSION_GIT
3185 MODULE_INFO(extra_version_git
, LTTNG_EXTRA_VERSION_GIT
);
3187 #ifdef LTTNG_EXTRA_VERSION_NAME
3188 MODULE_INFO(extra_version_name
, LTTNG_EXTRA_VERSION_NAME
);
3190 MODULE_LICENSE("GPL and additional rights");
3191 MODULE_AUTHOR("Mathieu Desnoyers <mathieu.desnoyers@efficios.com>");
3192 MODULE_DESCRIPTION("LTTng tracer");
3193 MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION
) "."
3194 __stringify(LTTNG_MODULES_MINOR_VERSION
) "."
3195 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION
)
3196 LTTNG_MODULES_EXTRAVERSION
);