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/ringbuffer/backend.h>
45 #include <wrapper/ringbuffer/frontend.h>
46 #include <wrapper/time.h>
48 #define METADATA_CACHE_DEFAULT_SIZE 4096
50 static LIST_HEAD(sessions
);
51 static LIST_HEAD(lttng_transport_list
);
53 * Protect the sessions and metadata caches.
55 static DEFINE_MUTEX(sessions_mutex
);
56 static struct kmem_cache
*event_cache
;
58 static void lttng_session_lazy_sync_enablers(struct lttng_session
*session
);
59 static void lttng_session_sync_enablers(struct lttng_session
*session
);
60 static void lttng_enabler_destroy(struct lttng_enabler
*enabler
);
62 static void _lttng_event_destroy(struct lttng_event
*event
);
63 static void _lttng_channel_destroy(struct lttng_channel
*chan
);
64 static int _lttng_event_unregister(struct lttng_event
*event
);
66 int _lttng_event_metadata_statedump(struct lttng_session
*session
,
67 struct lttng_channel
*chan
,
68 struct lttng_event
*event
);
70 int _lttng_session_metadata_statedump(struct lttng_session
*session
);
72 void _lttng_metadata_channel_hangup(struct lttng_metadata_stream
*stream
);
74 int _lttng_field_statedump(struct lttng_session
*session
,
75 const struct lttng_event_field
*field
,
78 void synchronize_trace(void)
80 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(5,1,0))
86 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0))
87 #ifdef CONFIG_PREEMPT_RT_FULL
90 #else /* (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)) */
91 #ifdef CONFIG_PREEMPT_RT
94 #endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)) */
97 void lttng_lock_sessions(void)
99 mutex_lock(&sessions_mutex
);
102 void lttng_unlock_sessions(void)
104 mutex_unlock(&sessions_mutex
);
108 * Called with sessions lock held.
110 int lttng_session_active(void)
112 struct lttng_session
*iter
;
114 list_for_each_entry(iter
, &sessions
, list
) {
121 struct lttng_session
*lttng_session_create(void)
123 struct lttng_session
*session
;
124 struct lttng_metadata_cache
*metadata_cache
;
127 mutex_lock(&sessions_mutex
);
128 session
= lttng_kvzalloc(sizeof(struct lttng_session
), GFP_KERNEL
);
131 INIT_LIST_HEAD(&session
->chan
);
132 INIT_LIST_HEAD(&session
->events
);
133 lttng_guid_gen(&session
->uuid
);
135 metadata_cache
= kzalloc(sizeof(struct lttng_metadata_cache
),
138 goto err_free_session
;
139 metadata_cache
->data
= vzalloc(METADATA_CACHE_DEFAULT_SIZE
);
140 if (!metadata_cache
->data
)
142 metadata_cache
->cache_alloc
= METADATA_CACHE_DEFAULT_SIZE
;
143 kref_init(&metadata_cache
->refcount
);
144 mutex_init(&metadata_cache
->lock
);
145 session
->metadata_cache
= metadata_cache
;
146 INIT_LIST_HEAD(&metadata_cache
->metadata_stream
);
147 memcpy(&metadata_cache
->uuid
, &session
->uuid
,
148 sizeof(metadata_cache
->uuid
));
149 INIT_LIST_HEAD(&session
->enablers_head
);
150 for (i
= 0; i
< LTTNG_EVENT_HT_SIZE
; i
++)
151 INIT_HLIST_HEAD(&session
->events_ht
.table
[i
]);
152 list_add(&session
->list
, &sessions
);
153 session
->pid_tracker
.session
= session
;
154 session
->pid_tracker
.tracker_type
= TRACKER_PID
;
155 session
->vpid_tracker
.session
= session
;
156 session
->vpid_tracker
.tracker_type
= TRACKER_VPID
;
157 session
->uid_tracker
.session
= session
;
158 session
->uid_tracker
.tracker_type
= TRACKER_UID
;
159 session
->vuid_tracker
.session
= session
;
160 session
->vuid_tracker
.tracker_type
= TRACKER_VUID
;
161 session
->gid_tracker
.session
= session
;
162 session
->gid_tracker
.tracker_type
= TRACKER_GID
;
163 session
->vgid_tracker
.session
= session
;
164 session
->vgid_tracker
.tracker_type
= TRACKER_VGID
;
165 mutex_unlock(&sessions_mutex
);
169 kfree(metadata_cache
);
171 lttng_kvfree(session
);
173 mutex_unlock(&sessions_mutex
);
177 void metadata_cache_destroy(struct kref
*kref
)
179 struct lttng_metadata_cache
*cache
=
180 container_of(kref
, struct lttng_metadata_cache
, refcount
);
185 void lttng_session_destroy(struct lttng_session
*session
)
187 struct lttng_channel
*chan
, *tmpchan
;
188 struct lttng_event
*event
, *tmpevent
;
189 struct lttng_metadata_stream
*metadata_stream
;
190 struct lttng_enabler
*enabler
, *tmpenabler
;
193 mutex_lock(&sessions_mutex
);
194 WRITE_ONCE(session
->active
, 0);
195 list_for_each_entry(chan
, &session
->chan
, list
) {
196 ret
= lttng_syscalls_unregister(chan
);
199 list_for_each_entry(event
, &session
->events
, list
) {
200 ret
= _lttng_event_unregister(event
);
203 synchronize_trace(); /* Wait for in-flight events to complete */
204 list_for_each_entry(chan
, &session
->chan
, list
) {
205 ret
= lttng_syscalls_destroy(chan
);
208 list_for_each_entry_safe(enabler
, tmpenabler
,
209 &session
->enablers_head
, node
)
210 lttng_enabler_destroy(enabler
);
211 list_for_each_entry_safe(event
, tmpevent
, &session
->events
, list
)
212 _lttng_event_destroy(event
);
213 list_for_each_entry_safe(chan
, tmpchan
, &session
->chan
, list
) {
214 BUG_ON(chan
->channel_type
== METADATA_CHANNEL
);
215 _lttng_channel_destroy(chan
);
217 mutex_lock(&session
->metadata_cache
->lock
);
218 list_for_each_entry(metadata_stream
, &session
->metadata_cache
->metadata_stream
, list
)
219 _lttng_metadata_channel_hangup(metadata_stream
);
220 mutex_unlock(&session
->metadata_cache
->lock
);
221 lttng_id_tracker_destroy(&session
->pid_tracker
, false);
222 lttng_id_tracker_destroy(&session
->vpid_tracker
, false);
223 lttng_id_tracker_destroy(&session
->uid_tracker
, false);
224 lttng_id_tracker_destroy(&session
->vuid_tracker
, false);
225 lttng_id_tracker_destroy(&session
->gid_tracker
, false);
226 lttng_id_tracker_destroy(&session
->vgid_tracker
, false);
227 kref_put(&session
->metadata_cache
->refcount
, metadata_cache_destroy
);
228 list_del(&session
->list
);
229 mutex_unlock(&sessions_mutex
);
230 lttng_kvfree(session
);
233 int lttng_session_statedump(struct lttng_session
*session
)
237 mutex_lock(&sessions_mutex
);
238 ret
= lttng_statedump_start(session
);
239 mutex_unlock(&sessions_mutex
);
243 int lttng_session_enable(struct lttng_session
*session
)
246 struct lttng_channel
*chan
;
248 mutex_lock(&sessions_mutex
);
249 if (session
->active
) {
254 /* Set transient enabler state to "enabled" */
257 /* We need to sync enablers with session before activation. */
258 lttng_session_sync_enablers(session
);
261 * Snapshot the number of events per channel to know the type of header
264 list_for_each_entry(chan
, &session
->chan
, list
) {
265 if (chan
->header_type
)
266 continue; /* don't change it if session stop/restart */
267 if (chan
->free_event_id
< 31)
268 chan
->header_type
= 1; /* compact */
270 chan
->header_type
= 2; /* large */
273 /* Clear each stream's quiescent state. */
274 list_for_each_entry(chan
, &session
->chan
, list
) {
275 if (chan
->channel_type
!= METADATA_CHANNEL
)
276 lib_ring_buffer_clear_quiescent_channel(chan
->chan
);
279 WRITE_ONCE(session
->active
, 1);
280 WRITE_ONCE(session
->been_active
, 1);
281 ret
= _lttng_session_metadata_statedump(session
);
283 WRITE_ONCE(session
->active
, 0);
286 ret
= lttng_statedump_start(session
);
288 WRITE_ONCE(session
->active
, 0);
290 mutex_unlock(&sessions_mutex
);
294 int lttng_session_disable(struct lttng_session
*session
)
297 struct lttng_channel
*chan
;
299 mutex_lock(&sessions_mutex
);
300 if (!session
->active
) {
304 WRITE_ONCE(session
->active
, 0);
306 /* Set transient enabler state to "disabled" */
308 lttng_session_sync_enablers(session
);
310 /* Set each stream's quiescent state. */
311 list_for_each_entry(chan
, &session
->chan
, list
) {
312 if (chan
->channel_type
!= METADATA_CHANNEL
)
313 lib_ring_buffer_set_quiescent_channel(chan
->chan
);
316 mutex_unlock(&sessions_mutex
);
320 int lttng_session_metadata_regenerate(struct lttng_session
*session
)
323 struct lttng_channel
*chan
;
324 struct lttng_event
*event
;
325 struct lttng_metadata_cache
*cache
= session
->metadata_cache
;
326 struct lttng_metadata_stream
*stream
;
328 mutex_lock(&sessions_mutex
);
329 if (!session
->active
) {
334 mutex_lock(&cache
->lock
);
335 memset(cache
->data
, 0, cache
->cache_alloc
);
336 cache
->metadata_written
= 0;
338 list_for_each_entry(stream
, &session
->metadata_cache
->metadata_stream
, list
) {
339 stream
->metadata_out
= 0;
340 stream
->metadata_in
= 0;
342 mutex_unlock(&cache
->lock
);
344 session
->metadata_dumped
= 0;
345 list_for_each_entry(chan
, &session
->chan
, list
) {
346 chan
->metadata_dumped
= 0;
349 list_for_each_entry(event
, &session
->events
, list
) {
350 event
->metadata_dumped
= 0;
353 ret
= _lttng_session_metadata_statedump(session
);
356 mutex_unlock(&sessions_mutex
);
360 int lttng_channel_enable(struct lttng_channel
*channel
)
364 mutex_lock(&sessions_mutex
);
365 if (channel
->channel_type
== METADATA_CHANNEL
) {
369 if (channel
->enabled
) {
373 /* Set transient enabler state to "enabled" */
375 lttng_session_sync_enablers(channel
->session
);
376 /* Set atomically the state to "enabled" */
377 WRITE_ONCE(channel
->enabled
, 1);
379 mutex_unlock(&sessions_mutex
);
383 int lttng_channel_disable(struct lttng_channel
*channel
)
387 mutex_lock(&sessions_mutex
);
388 if (channel
->channel_type
== METADATA_CHANNEL
) {
392 if (!channel
->enabled
) {
396 /* Set atomically the state to "disabled" */
397 WRITE_ONCE(channel
->enabled
, 0);
398 /* Set transient enabler state to "enabled" */
400 lttng_session_sync_enablers(channel
->session
);
402 mutex_unlock(&sessions_mutex
);
406 int lttng_event_enable(struct lttng_event
*event
)
410 mutex_lock(&sessions_mutex
);
411 if (event
->chan
->channel_type
== METADATA_CHANNEL
) {
415 if (event
->enabled
) {
419 switch (event
->instrumentation
) {
420 case LTTNG_KERNEL_TRACEPOINT
:
421 case LTTNG_KERNEL_SYSCALL
:
424 case LTTNG_KERNEL_KPROBE
:
425 case LTTNG_KERNEL_UPROBE
:
426 case LTTNG_KERNEL_NOOP
:
427 WRITE_ONCE(event
->enabled
, 1);
429 case LTTNG_KERNEL_KRETPROBE
:
430 ret
= lttng_kretprobes_event_enable_state(event
, 1);
432 case LTTNG_KERNEL_FUNCTION
: /* Fall-through. */
438 mutex_unlock(&sessions_mutex
);
442 int lttng_event_disable(struct lttng_event
*event
)
446 mutex_lock(&sessions_mutex
);
447 if (event
->chan
->channel_type
== METADATA_CHANNEL
) {
451 if (!event
->enabled
) {
455 switch (event
->instrumentation
) {
456 case LTTNG_KERNEL_TRACEPOINT
:
457 case LTTNG_KERNEL_SYSCALL
:
460 case LTTNG_KERNEL_KPROBE
:
461 case LTTNG_KERNEL_UPROBE
:
462 case LTTNG_KERNEL_NOOP
:
463 WRITE_ONCE(event
->enabled
, 0);
465 case LTTNG_KERNEL_KRETPROBE
:
466 ret
= lttng_kretprobes_event_enable_state(event
, 0);
468 case LTTNG_KERNEL_FUNCTION
: /* Fall-through. */
474 mutex_unlock(&sessions_mutex
);
478 static struct lttng_transport
*lttng_transport_find(const char *name
)
480 struct lttng_transport
*transport
;
482 list_for_each_entry(transport
, <tng_transport_list
, node
) {
483 if (!strcmp(transport
->name
, name
))
489 struct lttng_channel
*lttng_channel_create(struct lttng_session
*session
,
490 const char *transport_name
,
492 size_t subbuf_size
, size_t num_subbuf
,
493 unsigned int switch_timer_interval
,
494 unsigned int read_timer_interval
,
495 enum channel_type channel_type
)
497 struct lttng_channel
*chan
;
498 struct lttng_transport
*transport
= NULL
;
500 mutex_lock(&sessions_mutex
);
501 if (session
->been_active
&& channel_type
!= METADATA_CHANNEL
)
502 goto active
; /* Refuse to add channel to active session */
503 transport
= lttng_transport_find(transport_name
);
505 printk(KERN_WARNING
"LTTng transport %s not found\n",
509 if (!try_module_get(transport
->owner
)) {
510 printk(KERN_WARNING
"LTT : Can't lock transport module.\n");
513 chan
= kzalloc(sizeof(struct lttng_channel
), GFP_KERNEL
);
516 chan
->session
= session
;
517 chan
->id
= session
->free_chan_id
++;
518 chan
->ops
= &transport
->ops
;
520 * Note: the channel creation op already writes into the packet
521 * headers. Therefore the "chan" information used as input
522 * should be already accessible.
524 chan
->chan
= transport
->ops
.channel_create(transport_name
,
525 chan
, buf_addr
, subbuf_size
, num_subbuf
,
526 switch_timer_interval
, read_timer_interval
);
531 chan
->transport
= transport
;
532 chan
->channel_type
= channel_type
;
533 list_add(&chan
->list
, &session
->chan
);
534 mutex_unlock(&sessions_mutex
);
541 module_put(transport
->owner
);
544 mutex_unlock(&sessions_mutex
);
549 * Only used internally at session destruction for per-cpu channels, and
550 * when metadata channel is released.
551 * Needs to be called with sessions mutex held.
554 void _lttng_channel_destroy(struct lttng_channel
*chan
)
556 chan
->ops
->channel_destroy(chan
->chan
);
557 module_put(chan
->transport
->owner
);
558 list_del(&chan
->list
);
559 lttng_destroy_context(chan
->ctx
);
563 void lttng_metadata_channel_destroy(struct lttng_channel
*chan
)
565 BUG_ON(chan
->channel_type
!= METADATA_CHANNEL
);
567 /* Protect the metadata cache with the sessions_mutex. */
568 mutex_lock(&sessions_mutex
);
569 _lttng_channel_destroy(chan
);
570 mutex_unlock(&sessions_mutex
);
572 EXPORT_SYMBOL_GPL(lttng_metadata_channel_destroy
);
575 void _lttng_metadata_channel_hangup(struct lttng_metadata_stream
*stream
)
577 stream
->finalized
= 1;
578 wake_up_interruptible(&stream
->read_wait
);
582 * Supports event creation while tracing session is active.
583 * Needs to be called with sessions mutex held.
585 struct lttng_event
*_lttng_event_create(struct lttng_channel
*chan
,
586 struct lttng_kernel_event
*event_param
,
588 const struct lttng_event_desc
*event_desc
,
589 enum lttng_kernel_instrumentation itype
)
591 struct lttng_session
*session
= chan
->session
;
592 struct lttng_event
*event
;
593 const char *event_name
;
594 struct hlist_head
*head
;
599 if (chan
->free_event_id
== -1U) {
605 case LTTNG_KERNEL_TRACEPOINT
:
606 event_name
= event_desc
->name
;
608 case LTTNG_KERNEL_KPROBE
:
609 case LTTNG_KERNEL_UPROBE
:
610 case LTTNG_KERNEL_KRETPROBE
:
611 case LTTNG_KERNEL_NOOP
:
612 case LTTNG_KERNEL_SYSCALL
:
613 event_name
= event_param
->name
;
615 case LTTNG_KERNEL_FUNCTION
: /* Fall-through. */
621 name_len
= strlen(event_name
);
622 hash
= jhash(event_name
, name_len
, 0);
623 head
= &session
->events_ht
.table
[hash
& (LTTNG_EVENT_HT_SIZE
- 1)];
624 lttng_hlist_for_each_entry(event
, head
, hlist
) {
625 WARN_ON_ONCE(!event
->desc
);
626 if (!strncmp(event
->desc
->name
, event_name
,
627 LTTNG_KERNEL_SYM_NAME_LEN
- 1)
628 && chan
== event
->chan
) {
634 event
= kmem_cache_zalloc(event_cache
, GFP_KERNEL
);
640 event
->filter
= filter
;
641 event
->id
= chan
->free_event_id
++;
642 event
->instrumentation
= itype
;
643 event
->evtype
= LTTNG_TYPE_EVENT
;
644 INIT_LIST_HEAD(&event
->bytecode_runtime_head
);
645 INIT_LIST_HEAD(&event
->enablers_ref_head
);
648 case LTTNG_KERNEL_TRACEPOINT
:
649 /* Event will be enabled by enabler sync. */
651 event
->registered
= 0;
652 event
->desc
= lttng_event_get(event_name
);
657 /* Populate lttng_event structure before event registration. */
660 case LTTNG_KERNEL_KPROBE
:
662 * Needs to be explicitly enabled after creation, since
663 * we may want to apply filters.
666 event
->registered
= 1;
668 * Populate lttng_event structure before event
672 ret
= lttng_kprobes_register(event_name
,
673 event_param
->u
.kprobe
.symbol_name
,
674 event_param
->u
.kprobe
.offset
,
675 event_param
->u
.kprobe
.addr
,
681 ret
= try_module_get(event
->desc
->owner
);
684 case LTTNG_KERNEL_KRETPROBE
:
686 struct lttng_event
*event_return
;
688 /* kretprobe defines 2 events */
690 * Needs to be explicitly enabled after creation, since
691 * we may want to apply filters.
694 event
->registered
= 1;
696 kmem_cache_zalloc(event_cache
, GFP_KERNEL
);
701 event_return
->chan
= chan
;
702 event_return
->filter
= filter
;
703 event_return
->id
= chan
->free_event_id
++;
704 event_return
->enabled
= 0;
705 event_return
->registered
= 1;
706 event_return
->instrumentation
= itype
;
708 * Populate lttng_event structure before kretprobe registration.
711 ret
= lttng_kretprobes_register(event_name
,
712 event_param
->u
.kretprobe
.symbol_name
,
713 event_param
->u
.kretprobe
.offset
,
714 event_param
->u
.kretprobe
.addr
,
715 event
, event_return
);
717 kmem_cache_free(event_cache
, event_return
);
721 /* Take 2 refs on the module: one per event. */
722 ret
= try_module_get(event
->desc
->owner
);
724 ret
= try_module_get(event
->desc
->owner
);
726 ret
= _lttng_event_metadata_statedump(chan
->session
, chan
,
728 WARN_ON_ONCE(ret
> 0);
730 kmem_cache_free(event_cache
, event_return
);
731 module_put(event
->desc
->owner
);
732 module_put(event
->desc
->owner
);
733 goto statedump_error
;
735 list_add(&event_return
->list
, &chan
->session
->events
);
738 case LTTNG_KERNEL_NOOP
:
739 case LTTNG_KERNEL_SYSCALL
:
741 * Needs to be explicitly enabled after creation, since
742 * we may want to apply filters.
745 event
->registered
= 0;
746 event
->desc
= event_desc
;
747 switch (event_param
->u
.syscall
.entryexit
) {
748 case LTTNG_KERNEL_SYSCALL_ENTRYEXIT
:
751 case LTTNG_KERNEL_SYSCALL_ENTRY
:
752 event
->u
.syscall
.entryexit
= LTTNG_SYSCALL_ENTRY
;
754 case LTTNG_KERNEL_SYSCALL_EXIT
:
755 event
->u
.syscall
.entryexit
= LTTNG_SYSCALL_EXIT
;
758 switch (event_param
->u
.syscall
.abi
) {
759 case LTTNG_KERNEL_SYSCALL_ABI_ALL
:
762 case LTTNG_KERNEL_SYSCALL_ABI_NATIVE
:
763 event
->u
.syscall
.abi
= LTTNG_SYSCALL_ABI_NATIVE
;
765 case LTTNG_KERNEL_SYSCALL_ABI_COMPAT
:
766 event
->u
.syscall
.abi
= LTTNG_SYSCALL_ABI_COMPAT
;
774 case LTTNG_KERNEL_UPROBE
:
776 * Needs to be explicitly enabled after creation, since
777 * we may want to apply filters.
780 event
->registered
= 1;
783 * Populate lttng_event structure before event
788 ret
= lttng_uprobes_register(event_param
->name
,
789 event_param
->u
.uprobe
.fd
,
793 ret
= try_module_get(event
->desc
->owner
);
796 case LTTNG_KERNEL_FUNCTION
: /* Fall-through */
802 ret
= _lttng_event_metadata_statedump(chan
->session
, chan
, event
);
803 WARN_ON_ONCE(ret
> 0);
805 goto statedump_error
;
807 hlist_add_head(&event
->hlist
, head
);
808 list_add(&event
->list
, &chan
->session
->events
);
812 /* If a statedump error occurs, events will not be readable. */
814 kmem_cache_free(event_cache
, event
);
822 struct lttng_event
*lttng_event_create(struct lttng_channel
*chan
,
823 struct lttng_kernel_event
*event_param
,
825 const struct lttng_event_desc
*event_desc
,
826 enum lttng_kernel_instrumentation itype
)
828 struct lttng_event
*event
;
830 mutex_lock(&sessions_mutex
);
831 event
= _lttng_event_create(chan
, event_param
, filter
, event_desc
,
833 mutex_unlock(&sessions_mutex
);
837 /* Only used for tracepoints for now. */
839 void register_event(struct lttng_event
*event
)
841 const struct lttng_event_desc
*desc
;
844 if (event
->registered
)
848 switch (event
->instrumentation
) {
849 case LTTNG_KERNEL_TRACEPOINT
:
850 ret
= lttng_wrapper_tracepoint_probe_register(desc
->kname
,
851 desc
->probe_callback
,
854 case LTTNG_KERNEL_SYSCALL
:
855 ret
= lttng_syscall_filter_enable(event
->chan
, event
);
857 case LTTNG_KERNEL_KPROBE
:
858 case LTTNG_KERNEL_UPROBE
:
859 case LTTNG_KERNEL_KRETPROBE
:
860 case LTTNG_KERNEL_NOOP
:
863 case LTTNG_KERNEL_FUNCTION
: /* Fall-through */
868 event
->registered
= 1;
872 * Only used internally at session destruction.
874 int _lttng_event_unregister(struct lttng_event
*event
)
876 const struct lttng_event_desc
*desc
;
879 if (!event
->registered
)
883 switch (event
->instrumentation
) {
884 case LTTNG_KERNEL_TRACEPOINT
:
885 ret
= lttng_wrapper_tracepoint_probe_unregister(event
->desc
->kname
,
886 event
->desc
->probe_callback
,
889 case LTTNG_KERNEL_KPROBE
:
890 lttng_kprobes_unregister(event
);
893 case LTTNG_KERNEL_KRETPROBE
:
894 lttng_kretprobes_unregister(event
);
897 case LTTNG_KERNEL_SYSCALL
:
898 ret
= lttng_syscall_filter_disable(event
->chan
, event
);
900 case LTTNG_KERNEL_NOOP
:
903 case LTTNG_KERNEL_UPROBE
:
904 lttng_uprobes_unregister(event
);
907 case LTTNG_KERNEL_FUNCTION
: /* Fall-through */
912 event
->registered
= 0;
917 * Only used internally at session destruction.
920 void _lttng_event_destroy(struct lttng_event
*event
)
922 switch (event
->instrumentation
) {
923 case LTTNG_KERNEL_TRACEPOINT
:
924 lttng_event_put(event
->desc
);
926 case LTTNG_KERNEL_KPROBE
:
927 module_put(event
->desc
->owner
);
928 lttng_kprobes_destroy_private(event
);
930 case LTTNG_KERNEL_KRETPROBE
:
931 module_put(event
->desc
->owner
);
932 lttng_kretprobes_destroy_private(event
);
934 case LTTNG_KERNEL_NOOP
:
935 case LTTNG_KERNEL_SYSCALL
:
937 case LTTNG_KERNEL_UPROBE
:
938 module_put(event
->desc
->owner
);
939 lttng_uprobes_destroy_private(event
);
941 case LTTNG_KERNEL_FUNCTION
: /* Fall-through */
945 list_del(&event
->list
);
946 lttng_destroy_context(event
->ctx
);
947 kmem_cache_free(event_cache
, event
);
950 struct lttng_id_tracker
*get_tracker(struct lttng_session
*session
,
951 enum tracker_type tracker_type
)
953 switch (tracker_type
) {
955 return &session
->pid_tracker
;
957 return &session
->vpid_tracker
;
959 return &session
->uid_tracker
;
961 return &session
->vuid_tracker
;
963 return &session
->gid_tracker
;
965 return &session
->vgid_tracker
;
972 int lttng_session_track_id(struct lttng_session
*session
,
973 enum tracker_type tracker_type
, int id
)
975 struct lttng_id_tracker
*tracker
;
978 tracker
= get_tracker(session
, tracker_type
);
983 mutex_lock(&sessions_mutex
);
985 /* track all ids: destroy tracker. */
986 lttng_id_tracker_destroy(tracker
, true);
989 ret
= lttng_id_tracker_add(tracker
, id
);
991 mutex_unlock(&sessions_mutex
);
995 int lttng_session_untrack_id(struct lttng_session
*session
,
996 enum tracker_type tracker_type
, int id
)
998 struct lttng_id_tracker
*tracker
;
1001 tracker
= get_tracker(session
, tracker_type
);
1006 mutex_lock(&sessions_mutex
);
1008 /* untrack all ids: replace by empty tracker. */
1009 ret
= lttng_id_tracker_empty_set(tracker
);
1011 ret
= lttng_id_tracker_del(tracker
, id
);
1013 mutex_unlock(&sessions_mutex
);
1018 void *id_list_start(struct seq_file
*m
, loff_t
*pos
)
1020 struct lttng_id_tracker
*id_tracker
= m
->private;
1021 struct lttng_id_tracker_rcu
*id_tracker_p
= id_tracker
->p
;
1022 struct lttng_id_hash_node
*e
;
1025 mutex_lock(&sessions_mutex
);
1027 for (i
= 0; i
< LTTNG_ID_TABLE_SIZE
; i
++) {
1028 struct hlist_head
*head
= &id_tracker_p
->id_hash
[i
];
1030 lttng_hlist_for_each_entry(e
, head
, hlist
) {
1036 /* ID tracker disabled. */
1037 if (iter
>= *pos
&& iter
== 0) {
1038 return id_tracker_p
; /* empty tracker */
1046 /* Called with sessions_mutex held. */
1048 void *id_list_next(struct seq_file
*m
, void *p
, loff_t
*ppos
)
1050 struct lttng_id_tracker
*id_tracker
= m
->private;
1051 struct lttng_id_tracker_rcu
*id_tracker_p
= id_tracker
->p
;
1052 struct lttng_id_hash_node
*e
;
1057 for (i
= 0; i
< LTTNG_ID_TABLE_SIZE
; i
++) {
1058 struct hlist_head
*head
= &id_tracker_p
->id_hash
[i
];
1060 lttng_hlist_for_each_entry(e
, head
, hlist
) {
1061 if (iter
++ >= *ppos
)
1066 /* ID tracker disabled. */
1067 if (iter
>= *ppos
&& iter
== 0)
1068 return p
; /* empty tracker */
1077 void id_list_stop(struct seq_file
*m
, void *p
)
1079 mutex_unlock(&sessions_mutex
);
1083 int id_list_show(struct seq_file
*m
, void *p
)
1085 struct lttng_id_tracker
*id_tracker
= m
->private;
1086 struct lttng_id_tracker_rcu
*id_tracker_p
= id_tracker
->p
;
1089 if (p
== id_tracker_p
) {
1090 /* Tracker disabled. */
1093 const struct lttng_id_hash_node
*e
= p
;
1095 id
= lttng_id_tracker_get_node_id(e
);
1097 switch (id_tracker
->tracker_type
) {
1099 seq_printf(m
, "process { pid = %d; };\n", id
);
1102 seq_printf(m
, "process { vpid = %d; };\n", id
);
1105 seq_printf(m
, "user { uid = %d; };\n", id
);
1108 seq_printf(m
, "user { vuid = %d; };\n", id
);
1111 seq_printf(m
, "group { gid = %d; };\n", id
);
1114 seq_printf(m
, "group { vgid = %d; };\n", id
);
1117 seq_printf(m
, "UNKNOWN { field = %d };\n", id
);
1123 const struct seq_operations lttng_tracker_ids_list_seq_ops
= {
1124 .start
= id_list_start
,
1125 .next
= id_list_next
,
1126 .stop
= id_list_stop
,
1127 .show
= id_list_show
,
1131 int lttng_tracker_ids_list_open(struct inode
*inode
, struct file
*file
)
1133 return seq_open(file
, <tng_tracker_ids_list_seq_ops
);
1137 int lttng_tracker_ids_list_release(struct inode
*inode
, struct file
*file
)
1139 struct seq_file
*m
= file
->private_data
;
1140 struct lttng_id_tracker
*id_tracker
= m
->private;
1143 WARN_ON_ONCE(!id_tracker
);
1144 ret
= seq_release(inode
, file
);
1146 fput(id_tracker
->session
->file
);
1150 const struct file_operations lttng_tracker_ids_list_fops
= {
1151 .owner
= THIS_MODULE
,
1152 .open
= lttng_tracker_ids_list_open
,
1154 .llseek
= seq_lseek
,
1155 .release
= lttng_tracker_ids_list_release
,
1158 int lttng_session_list_tracker_ids(struct lttng_session
*session
,
1159 enum tracker_type tracker_type
)
1161 struct file
*tracker_ids_list_file
;
1165 file_fd
= lttng_get_unused_fd();
1171 tracker_ids_list_file
= anon_inode_getfile("[lttng_tracker_ids_list]",
1172 <tng_tracker_ids_list_fops
,
1174 if (IS_ERR(tracker_ids_list_file
)) {
1175 ret
= PTR_ERR(tracker_ids_list_file
);
1178 if (!atomic_long_add_unless(&session
->file
->f_count
, 1, LONG_MAX
)) {
1180 goto refcount_error
;
1182 ret
= lttng_tracker_ids_list_fops
.open(NULL
, tracker_ids_list_file
);
1185 m
= tracker_ids_list_file
->private_data
;
1187 m
->private = get_tracker(session
, tracker_type
);
1188 BUG_ON(!m
->private);
1189 fd_install(file_fd
, tracker_ids_list_file
);
1194 atomic_long_dec(&session
->file
->f_count
);
1196 fput(tracker_ids_list_file
);
1198 put_unused_fd(file_fd
);
1204 * Enabler management.
1207 int lttng_match_enabler_star_glob(const char *desc_name
,
1208 const char *pattern
)
1210 if (!strutils_star_glob_match(pattern
, LTTNG_SIZE_MAX
,
1211 desc_name
, LTTNG_SIZE_MAX
))
1217 int lttng_match_enabler_name(const char *desc_name
,
1220 if (strcmp(desc_name
, name
))
1226 int lttng_desc_match_enabler(const struct lttng_event_desc
*desc
,
1227 struct lttng_enabler
*enabler
)
1229 const char *desc_name
, *enabler_name
;
1230 bool compat
= false, entry
= false;
1232 enabler_name
= enabler
->event_param
.name
;
1233 switch (enabler
->event_param
.instrumentation
) {
1234 case LTTNG_KERNEL_TRACEPOINT
:
1235 desc_name
= desc
->name
;
1236 switch (enabler
->type
) {
1237 case LTTNG_ENABLER_STAR_GLOB
:
1238 return lttng_match_enabler_star_glob(desc_name
, enabler_name
);
1239 case LTTNG_ENABLER_NAME
:
1240 return lttng_match_enabler_name(desc_name
, enabler_name
);
1245 case LTTNG_KERNEL_SYSCALL
:
1246 desc_name
= desc
->name
;
1247 if (!strncmp(desc_name
, "compat_", strlen("compat_"))) {
1248 desc_name
+= strlen("compat_");
1251 if (!strncmp(desc_name
, "syscall_exit_",
1252 strlen("syscall_exit_"))) {
1253 desc_name
+= strlen("syscall_exit_");
1254 } else if (!strncmp(desc_name
, "syscall_entry_",
1255 strlen("syscall_entry_"))) {
1256 desc_name
+= strlen("syscall_entry_");
1262 switch (enabler
->event_param
.u
.syscall
.entryexit
) {
1263 case LTTNG_KERNEL_SYSCALL_ENTRYEXIT
:
1265 case LTTNG_KERNEL_SYSCALL_ENTRY
:
1269 case LTTNG_KERNEL_SYSCALL_EXIT
:
1276 switch (enabler
->event_param
.u
.syscall
.abi
) {
1277 case LTTNG_KERNEL_SYSCALL_ABI_ALL
:
1279 case LTTNG_KERNEL_SYSCALL_ABI_NATIVE
:
1283 case LTTNG_KERNEL_SYSCALL_ABI_COMPAT
:
1290 switch (enabler
->event_param
.u
.syscall
.match
) {
1291 case LTTNG_SYSCALL_MATCH_NAME
:
1292 switch (enabler
->type
) {
1293 case LTTNG_ENABLER_STAR_GLOB
:
1294 return lttng_match_enabler_star_glob(desc_name
, enabler_name
);
1295 case LTTNG_ENABLER_NAME
:
1296 return lttng_match_enabler_name(desc_name
, enabler_name
);
1301 case LTTNG_SYSCALL_MATCH_NR
:
1302 return -EINVAL
; /* Not implemented. */
1314 int lttng_event_match_enabler(struct lttng_event
*event
,
1315 struct lttng_enabler
*enabler
)
1317 if (enabler
->event_param
.instrumentation
!= event
->instrumentation
)
1319 if (lttng_desc_match_enabler(event
->desc
, enabler
)
1320 && event
->chan
== enabler
->chan
)
1327 struct lttng_enabler_ref
*lttng_event_enabler_ref(struct lttng_event
*event
,
1328 struct lttng_enabler
*enabler
)
1330 struct lttng_enabler_ref
*enabler_ref
;
1332 list_for_each_entry(enabler_ref
,
1333 &event
->enablers_ref_head
, node
) {
1334 if (enabler_ref
->ref
== enabler
)
1341 void lttng_create_tracepoint_if_missing(struct lttng_enabler
*enabler
)
1343 struct lttng_session
*session
= enabler
->chan
->session
;
1344 struct lttng_probe_desc
*probe_desc
;
1345 const struct lttng_event_desc
*desc
;
1347 struct list_head
*probe_list
;
1349 probe_list
= lttng_get_probe_list_head();
1351 * For each probe event, if we find that a probe event matches
1352 * our enabler, create an associated lttng_event if not
1355 list_for_each_entry(probe_desc
, probe_list
, head
) {
1356 for (i
= 0; i
< probe_desc
->nr_events
; i
++) {
1358 struct hlist_head
*head
;
1359 const char *event_name
;
1362 struct lttng_event
*event
;
1364 desc
= probe_desc
->event_desc
[i
];
1365 if (!lttng_desc_match_enabler(desc
, enabler
))
1367 event_name
= desc
->name
;
1368 name_len
= strlen(event_name
);
1371 * Check if already created.
1373 hash
= jhash(event_name
, name_len
, 0);
1374 head
= &session
->events_ht
.table
[hash
& (LTTNG_EVENT_HT_SIZE
- 1)];
1375 lttng_hlist_for_each_entry(event
, head
, hlist
) {
1376 if (event
->desc
== desc
1377 && event
->chan
== enabler
->chan
)
1384 * We need to create an event for this
1387 event
= _lttng_event_create(enabler
->chan
,
1389 LTTNG_KERNEL_TRACEPOINT
);
1391 printk(KERN_INFO
"Unable to create event %s\n",
1392 probe_desc
->event_desc
[i
]->name
);
1399 void lttng_create_syscall_if_missing(struct lttng_enabler
*enabler
)
1403 ret
= lttng_syscalls_register(enabler
->chan
, NULL
);
1408 * Create struct lttng_event if it is missing and present in the list of
1409 * tracepoint probes.
1410 * Should be called with sessions mutex held.
1413 void lttng_create_event_if_missing(struct lttng_enabler
*enabler
)
1415 switch (enabler
->event_param
.instrumentation
) {
1416 case LTTNG_KERNEL_TRACEPOINT
:
1417 lttng_create_tracepoint_if_missing(enabler
);
1419 case LTTNG_KERNEL_SYSCALL
:
1420 lttng_create_syscall_if_missing(enabler
);
1429 * Create events associated with an enabler (if not already present),
1430 * and add backward reference from the event to the enabler.
1431 * Should be called with sessions mutex held.
1434 int lttng_enabler_ref_events(struct lttng_enabler
*enabler
)
1436 struct lttng_channel
*chan
= enabler
->chan
;
1437 struct lttng_session
*session
= chan
->session
;
1438 struct lttng_event
*event
;
1440 if (enabler
->event_param
.instrumentation
== LTTNG_KERNEL_SYSCALL
&&
1441 enabler
->event_param
.u
.syscall
.entryexit
== LTTNG_KERNEL_SYSCALL_ENTRYEXIT
&&
1442 enabler
->event_param
.u
.syscall
.abi
== LTTNG_KERNEL_SYSCALL_ABI_ALL
&&
1443 enabler
->event_param
.u
.syscall
.match
== LTTNG_SYSCALL_MATCH_NAME
&&
1444 !strcmp(enabler
->event_param
.name
, "*")) {
1445 if (enabler
->enabled
)
1446 WRITE_ONCE(chan
->syscall_all
, 1);
1448 WRITE_ONCE(chan
->syscall_all
, 0);
1451 /* First ensure that probe events are created for this enabler. */
1452 lttng_create_event_if_missing(enabler
);
1454 /* For each event matching enabler in session event list. */
1455 list_for_each_entry(event
, &session
->events
, list
) {
1456 struct lttng_enabler_ref
*enabler_ref
;
1458 if (!lttng_event_match_enabler(event
, enabler
))
1460 enabler_ref
= lttng_event_enabler_ref(event
, enabler
);
1463 * If no backward ref, create it.
1464 * Add backward ref from event to enabler.
1466 enabler_ref
= kzalloc(sizeof(*enabler_ref
), GFP_KERNEL
);
1469 enabler_ref
->ref
= enabler
;
1470 list_add(&enabler_ref
->node
,
1471 &event
->enablers_ref_head
);
1475 * Link filter bytecodes if not linked yet.
1477 lttng_enabler_event_link_bytecode(event
, enabler
);
1479 /* TODO: merge event context. */
1485 * Called at module load: connect the probe on all enablers matching
1487 * Called with sessions lock held.
1489 int lttng_fix_pending_events(void)
1491 struct lttng_session
*session
;
1493 list_for_each_entry(session
, &sessions
, list
)
1494 lttng_session_lazy_sync_enablers(session
);
1498 struct lttng_enabler
*lttng_enabler_create(enum lttng_enabler_type type
,
1499 struct lttng_kernel_event
*event_param
,
1500 struct lttng_channel
*chan
)
1502 struct lttng_enabler
*enabler
;
1504 enabler
= kzalloc(sizeof(*enabler
), GFP_KERNEL
);
1507 enabler
->type
= type
;
1508 INIT_LIST_HEAD(&enabler
->filter_bytecode_head
);
1509 memcpy(&enabler
->event_param
, event_param
,
1510 sizeof(enabler
->event_param
));
1511 enabler
->chan
= chan
;
1513 enabler
->enabled
= 0;
1514 enabler
->evtype
= LTTNG_TYPE_ENABLER
;
1515 mutex_lock(&sessions_mutex
);
1516 list_add(&enabler
->node
, &enabler
->chan
->session
->enablers_head
);
1517 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
1518 mutex_unlock(&sessions_mutex
);
1522 int lttng_enabler_enable(struct lttng_enabler
*enabler
)
1524 mutex_lock(&sessions_mutex
);
1525 enabler
->enabled
= 1;
1526 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
1527 mutex_unlock(&sessions_mutex
);
1531 int lttng_enabler_disable(struct lttng_enabler
*enabler
)
1533 mutex_lock(&sessions_mutex
);
1534 enabler
->enabled
= 0;
1535 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
1536 mutex_unlock(&sessions_mutex
);
1540 int lttng_enabler_attach_bytecode(struct lttng_enabler
*enabler
,
1541 struct lttng_kernel_filter_bytecode __user
*bytecode
)
1543 struct lttng_filter_bytecode_node
*bytecode_node
;
1544 uint32_t bytecode_len
;
1547 ret
= get_user(bytecode_len
, &bytecode
->len
);
1550 bytecode_node
= kzalloc(sizeof(*bytecode_node
) + bytecode_len
,
1554 ret
= copy_from_user(&bytecode_node
->bc
, bytecode
,
1555 sizeof(*bytecode
) + bytecode_len
);
1558 bytecode_node
->enabler
= enabler
;
1559 /* Enforce length based on allocated size */
1560 bytecode_node
->bc
.len
= bytecode_len
;
1561 list_add_tail(&bytecode_node
->node
, &enabler
->filter_bytecode_head
);
1562 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
1566 kfree(bytecode_node
);
1570 int lttng_event_add_callsite(struct lttng_event
*event
,
1571 struct lttng_kernel_event_callsite __user
*callsite
)
1574 switch (event
->instrumentation
) {
1575 case LTTNG_KERNEL_UPROBE
:
1576 return lttng_uprobes_add_callsite(event
, callsite
);
1582 int lttng_enabler_attach_context(struct lttng_enabler
*enabler
,
1583 struct lttng_kernel_context
*context_param
)
1589 void lttng_enabler_destroy(struct lttng_enabler
*enabler
)
1591 struct lttng_filter_bytecode_node
*filter_node
, *tmp_filter_node
;
1593 /* Destroy filter bytecode */
1594 list_for_each_entry_safe(filter_node
, tmp_filter_node
,
1595 &enabler
->filter_bytecode_head
, node
) {
1599 /* Destroy contexts */
1600 lttng_destroy_context(enabler
->ctx
);
1602 list_del(&enabler
->node
);
1607 * lttng_session_sync_enablers should be called just before starting a
1609 * Should be called with sessions mutex held.
1612 void lttng_session_sync_enablers(struct lttng_session
*session
)
1614 struct lttng_enabler
*enabler
;
1615 struct lttng_event
*event
;
1617 list_for_each_entry(enabler
, &session
->enablers_head
, node
)
1618 lttng_enabler_ref_events(enabler
);
1620 * For each event, if at least one of its enablers is enabled,
1621 * and its channel and session transient states are enabled, we
1622 * enable the event, else we disable it.
1624 list_for_each_entry(event
, &session
->events
, list
) {
1625 struct lttng_enabler_ref
*enabler_ref
;
1626 struct lttng_bytecode_runtime
*runtime
;
1627 int enabled
= 0, has_enablers_without_bytecode
= 0;
1629 switch (event
->instrumentation
) {
1630 case LTTNG_KERNEL_TRACEPOINT
:
1631 case LTTNG_KERNEL_SYSCALL
:
1633 list_for_each_entry(enabler_ref
,
1634 &event
->enablers_ref_head
, node
) {
1635 if (enabler_ref
->ref
->enabled
) {
1642 /* Not handled with lazy sync. */
1646 * Enabled state is based on union of enablers, with
1647 * intesection of session and channel transient enable
1650 enabled
= enabled
&& session
->tstate
&& event
->chan
->tstate
;
1652 WRITE_ONCE(event
->enabled
, enabled
);
1654 * Sync tracepoint registration with event enabled
1658 register_event(event
);
1660 _lttng_event_unregister(event
);
1663 /* Check if has enablers without bytecode enabled */
1664 list_for_each_entry(enabler_ref
,
1665 &event
->enablers_ref_head
, node
) {
1666 if (enabler_ref
->ref
->enabled
1667 && list_empty(&enabler_ref
->ref
->filter_bytecode_head
)) {
1668 has_enablers_without_bytecode
= 1;
1672 event
->has_enablers_without_bytecode
=
1673 has_enablers_without_bytecode
;
1675 /* Enable filters */
1676 list_for_each_entry(runtime
,
1677 &event
->bytecode_runtime_head
, node
)
1678 lttng_filter_sync_state(runtime
);
1683 * Apply enablers to session events, adding events to session if need
1684 * be. It is required after each modification applied to an active
1685 * session, and right before session "start".
1686 * "lazy" sync means we only sync if required.
1687 * Should be called with sessions mutex held.
1690 void lttng_session_lazy_sync_enablers(struct lttng_session
*session
)
1692 /* We can skip if session is not active */
1693 if (!session
->active
)
1695 lttng_session_sync_enablers(session
);
1699 * Serialize at most one packet worth of metadata into a metadata
1701 * We grab the metadata cache mutex to get exclusive access to our metadata
1702 * buffer and to the metadata cache. Exclusive access to the metadata buffer
1703 * allows us to do racy operations such as looking for remaining space left in
1704 * packet and write, since mutual exclusion protects us from concurrent writes.
1705 * Mutual exclusion on the metadata cache allow us to read the cache content
1706 * without racing against reallocation of the cache by updates.
1707 * Returns the number of bytes written in the channel, 0 if no data
1708 * was written and a negative value on error.
1710 int lttng_metadata_output_channel(struct lttng_metadata_stream
*stream
,
1711 struct channel
*chan
, bool *coherent
)
1713 struct lib_ring_buffer_ctx ctx
;
1715 size_t len
, reserve_len
;
1718 * Ensure we support mutiple get_next / put sequences followed by
1719 * put_next. The metadata cache lock protects reading the metadata
1720 * cache. It can indeed be read concurrently by "get_next_subbuf" and
1721 * "flush" operations on the buffer invoked by different processes.
1722 * Moreover, since the metadata cache memory can be reallocated, we
1723 * need to have exclusive access against updates even though we only
1726 mutex_lock(&stream
->metadata_cache
->lock
);
1727 WARN_ON(stream
->metadata_in
< stream
->metadata_out
);
1728 if (stream
->metadata_in
!= stream
->metadata_out
)
1731 /* Metadata regenerated, change the version. */
1732 if (stream
->metadata_cache
->version
!= stream
->version
)
1733 stream
->version
= stream
->metadata_cache
->version
;
1735 len
= stream
->metadata_cache
->metadata_written
-
1736 stream
->metadata_in
;
1739 reserve_len
= min_t(size_t,
1740 stream
->transport
->ops
.packet_avail_size(chan
),
1742 lib_ring_buffer_ctx_init(&ctx
, chan
, NULL
, reserve_len
,
1745 * If reservation failed, return an error to the caller.
1747 ret
= stream
->transport
->ops
.event_reserve(&ctx
, 0);
1749 printk(KERN_WARNING
"LTTng: Metadata event reservation failed\n");
1750 stream
->coherent
= false;
1753 stream
->transport
->ops
.event_write(&ctx
,
1754 stream
->metadata_cache
->data
+ stream
->metadata_in
,
1756 stream
->transport
->ops
.event_commit(&ctx
);
1757 stream
->metadata_in
+= reserve_len
;
1758 if (reserve_len
< len
)
1759 stream
->coherent
= false;
1761 stream
->coherent
= true;
1766 *coherent
= stream
->coherent
;
1767 mutex_unlock(&stream
->metadata_cache
->lock
);
1772 void lttng_metadata_begin(struct lttng_session
*session
)
1774 if (atomic_inc_return(&session
->metadata_cache
->producing
) == 1)
1775 mutex_lock(&session
->metadata_cache
->lock
);
1779 void lttng_metadata_end(struct lttng_session
*session
)
1781 WARN_ON_ONCE(!atomic_read(&session
->metadata_cache
->producing
));
1782 if (atomic_dec_return(&session
->metadata_cache
->producing
) == 0) {
1783 struct lttng_metadata_stream
*stream
;
1785 list_for_each_entry(stream
, &session
->metadata_cache
->metadata_stream
, list
)
1786 wake_up_interruptible(&stream
->read_wait
);
1787 mutex_unlock(&session
->metadata_cache
->lock
);
1792 * Write the metadata to the metadata cache.
1793 * Must be called with sessions_mutex held.
1794 * The metadata cache lock protects us from concurrent read access from
1795 * thread outputting metadata content to ring buffer.
1796 * The content of the printf is printed as a single atomic metadata
1799 int lttng_metadata_printf(struct lttng_session
*session
,
1800 const char *fmt
, ...)
1806 WARN_ON_ONCE(!LTTNG_READ_ONCE(session
->active
));
1809 str
= kvasprintf(GFP_KERNEL
, fmt
, ap
);
1815 WARN_ON_ONCE(!atomic_read(&session
->metadata_cache
->producing
));
1816 if (session
->metadata_cache
->metadata_written
+ len
>
1817 session
->metadata_cache
->cache_alloc
) {
1818 char *tmp_cache_realloc
;
1819 unsigned int tmp_cache_alloc_size
;
1821 tmp_cache_alloc_size
= max_t(unsigned int,
1822 session
->metadata_cache
->cache_alloc
+ len
,
1823 session
->metadata_cache
->cache_alloc
<< 1);
1824 tmp_cache_realloc
= vzalloc(tmp_cache_alloc_size
);
1825 if (!tmp_cache_realloc
)
1827 if (session
->metadata_cache
->data
) {
1828 memcpy(tmp_cache_realloc
,
1829 session
->metadata_cache
->data
,
1830 session
->metadata_cache
->cache_alloc
);
1831 vfree(session
->metadata_cache
->data
);
1834 session
->metadata_cache
->cache_alloc
= tmp_cache_alloc_size
;
1835 session
->metadata_cache
->data
= tmp_cache_realloc
;
1837 memcpy(session
->metadata_cache
->data
+
1838 session
->metadata_cache
->metadata_written
,
1840 session
->metadata_cache
->metadata_written
+= len
;
1851 int print_tabs(struct lttng_session
*session
, size_t nesting
)
1855 for (i
= 0; i
< nesting
; i
++) {
1858 ret
= lttng_metadata_printf(session
, " ");
1867 * Must be called with sessions_mutex held.
1870 int _lttng_struct_type_statedump(struct lttng_session
*session
,
1871 const struct lttng_type
*type
,
1875 uint32_t i
, nr_fields
;
1877 ret
= print_tabs(session
, nesting
);
1880 ret
= lttng_metadata_printf(session
,
1884 nr_fields
= type
->u
._struct
.nr_fields
;
1885 for (i
= 0; i
< nr_fields
; i
++) {
1886 const struct lttng_event_field
*iter_field
;
1888 iter_field
= &type
->u
._struct
.fields
[i
];
1889 ret
= _lttng_field_statedump(session
, iter_field
, nesting
+ 1);
1893 ret
= print_tabs(session
, nesting
);
1896 ret
= lttng_metadata_printf(session
,
1902 * Must be called with sessions_mutex held.
1905 int _lttng_struct_statedump(struct lttng_session
*session
,
1906 const struct lttng_event_field
*field
,
1911 ret
= _lttng_struct_type_statedump(session
,
1912 &field
->type
, nesting
);
1915 ret
= lttng_metadata_printf(session
,
1922 * Must be called with sessions_mutex held.
1925 int _lttng_variant_type_statedump(struct lttng_session
*session
,
1926 const struct lttng_type
*type
,
1930 uint32_t i
, nr_choices
;
1932 ret
= print_tabs(session
, nesting
);
1935 ret
= lttng_metadata_printf(session
,
1936 "variant <_%s> {\n",
1937 type
->u
.variant
.tag_name
);
1940 nr_choices
= type
->u
.variant
.nr_choices
;
1941 for (i
= 0; i
< nr_choices
; i
++) {
1942 const struct lttng_event_field
*iter_field
;
1944 iter_field
= &type
->u
.variant
.choices
[i
];
1945 ret
= _lttng_field_statedump(session
, iter_field
, nesting
+ 1);
1949 ret
= print_tabs(session
, nesting
);
1952 ret
= lttng_metadata_printf(session
,
1958 * Must be called with sessions_mutex held.
1961 int _lttng_variant_statedump(struct lttng_session
*session
,
1962 const struct lttng_event_field
*field
,
1967 ret
= _lttng_variant_type_statedump(session
,
1968 &field
->type
, nesting
);
1971 ret
= lttng_metadata_printf(session
,
1978 * Must be called with sessions_mutex held.
1981 int _lttng_array_compound_statedump(struct lttng_session
*session
,
1982 const struct lttng_event_field
*field
,
1986 const struct lttng_type
*elem_type
;
1988 /* Only array of structures and variants are currently supported. */
1989 elem_type
= field
->type
.u
.array_compound
.elem_type
;
1990 switch (elem_type
->atype
) {
1992 ret
= _lttng_struct_type_statedump(session
, elem_type
, nesting
);
1997 ret
= _lttng_variant_type_statedump(session
, elem_type
, nesting
);
2004 ret
= lttng_metadata_printf(session
,
2007 field
->type
.u
.array_compound
.length
);
2012 * Must be called with sessions_mutex held.
2015 int _lttng_sequence_compound_statedump(struct lttng_session
*session
,
2016 const struct lttng_event_field
*field
,
2020 const char *length_name
;
2021 const struct lttng_type
*elem_type
;
2023 length_name
= field
->type
.u
.sequence_compound
.length_name
;
2025 /* Only array of structures and variants are currently supported. */
2026 elem_type
= field
->type
.u
.sequence_compound
.elem_type
;
2027 switch (elem_type
->atype
) {
2029 ret
= _lttng_struct_type_statedump(session
, elem_type
, nesting
);
2034 ret
= _lttng_variant_type_statedump(session
, elem_type
, nesting
);
2041 ret
= lttng_metadata_printf(session
,
2049 * Must be called with sessions_mutex held.
2052 int _lttng_enum_statedump(struct lttng_session
*session
,
2053 const struct lttng_event_field
*field
,
2056 const struct lttng_enum_desc
*enum_desc
;
2057 const struct lttng_integer_type
*container_type
;
2059 unsigned int i
, nr_entries
;
2061 enum_desc
= field
->type
.u
.basic
.enumeration
.desc
;
2062 container_type
= &field
->type
.u
.basic
.enumeration
.container_type
;
2063 nr_entries
= enum_desc
->nr_entries
;
2065 ret
= print_tabs(session
, nesting
);
2068 ret
= lttng_metadata_printf(session
,
2069 "enum : integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } {\n",
2070 container_type
->size
,
2071 container_type
->alignment
,
2072 container_type
->signedness
,
2073 (container_type
->encoding
== lttng_encode_none
)
2075 : (container_type
->encoding
== lttng_encode_UTF8
)
2078 container_type
->base
,
2079 #if __BYTE_ORDER == __BIG_ENDIAN
2080 container_type
->reverse_byte_order
? " byte_order = le;" : ""
2082 container_type
->reverse_byte_order
? " byte_order = be;" : ""
2087 /* Dump all entries */
2088 for (i
= 0; i
< nr_entries
; i
++) {
2089 const struct lttng_enum_entry
*entry
= &enum_desc
->entries
[i
];
2092 ret
= print_tabs(session
, nesting
+ 1);
2095 ret
= lttng_metadata_printf(session
,
2099 len
= strlen(entry
->string
);
2100 /* Escape the character '"' */
2101 for (j
= 0; j
< len
; j
++) {
2102 char c
= entry
->string
[j
];
2106 ret
= lttng_metadata_printf(session
,
2110 ret
= lttng_metadata_printf(session
,
2114 ret
= lttng_metadata_printf(session
,
2121 ret
= lttng_metadata_printf(session
, "\"");
2125 if (entry
->options
.is_auto
) {
2126 ret
= lttng_metadata_printf(session
, ",\n");
2130 ret
= lttng_metadata_printf(session
,
2134 if (entry
->start
.signedness
)
2135 ret
= lttng_metadata_printf(session
,
2136 "%lld", (long long) entry
->start
.value
);
2138 ret
= lttng_metadata_printf(session
,
2139 "%llu", entry
->start
.value
);
2142 if (entry
->start
.signedness
== entry
->end
.signedness
&&
2144 == entry
->end
.value
) {
2145 ret
= lttng_metadata_printf(session
,
2148 if (entry
->end
.signedness
) {
2149 ret
= lttng_metadata_printf(session
,
2151 (long long) entry
->end
.value
);
2153 ret
= lttng_metadata_printf(session
,
2162 ret
= print_tabs(session
, nesting
);
2165 ret
= lttng_metadata_printf(session
, "} _%s;\n",
2172 * Must be called with sessions_mutex held.
2175 int _lttng_field_statedump(struct lttng_session
*session
,
2176 const struct lttng_event_field
*field
,
2181 switch (field
->type
.atype
) {
2183 ret
= print_tabs(session
, nesting
);
2186 ret
= lttng_metadata_printf(session
,
2187 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n",
2188 field
->type
.u
.basic
.integer
.size
,
2189 field
->type
.u
.basic
.integer
.alignment
,
2190 field
->type
.u
.basic
.integer
.signedness
,
2191 (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_none
)
2193 : (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
2196 field
->type
.u
.basic
.integer
.base
,
2197 #if __BYTE_ORDER == __BIG_ENDIAN
2198 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
2200 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
2205 ret
= _lttng_enum_statedump(session
, field
, nesting
);
2208 case atype_array_bitfield
:
2210 const struct lttng_basic_type
*elem_type
;
2212 elem_type
= &field
->type
.u
.array
.elem_type
;
2213 if (field
->type
.u
.array
.elem_alignment
) {
2214 ret
= print_tabs(session
, nesting
);
2217 ret
= lttng_metadata_printf(session
,
2218 "struct { } align(%u) _%s_padding;\n",
2219 field
->type
.u
.array
.elem_alignment
* CHAR_BIT
,
2224 ret
= print_tabs(session
, nesting
);
2227 ret
= lttng_metadata_printf(session
,
2228 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n",
2229 elem_type
->u
.basic
.integer
.size
,
2230 elem_type
->u
.basic
.integer
.alignment
,
2231 elem_type
->u
.basic
.integer
.signedness
,
2232 (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
2234 : (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
2237 elem_type
->u
.basic
.integer
.base
,
2238 #if __BYTE_ORDER == __BIG_ENDIAN
2239 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
2241 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
2243 field
->name
, field
->type
.u
.array
.length
);
2246 case atype_sequence
:
2247 case atype_sequence_bitfield
:
2249 const struct lttng_basic_type
*elem_type
;
2250 const struct lttng_basic_type
*length_type
;
2252 elem_type
= &field
->type
.u
.sequence
.elem_type
;
2253 length_type
= &field
->type
.u
.sequence
.length_type
;
2254 ret
= print_tabs(session
, nesting
);
2257 ret
= lttng_metadata_printf(session
,
2258 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n",
2259 length_type
->u
.basic
.integer
.size
,
2260 (unsigned int) length_type
->u
.basic
.integer
.alignment
,
2261 length_type
->u
.basic
.integer
.signedness
,
2262 (length_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
2264 : ((length_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
2267 length_type
->u
.basic
.integer
.base
,
2268 #if __BYTE_ORDER == __BIG_ENDIAN
2269 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
2271 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
2277 if (field
->type
.u
.sequence
.elem_alignment
) {
2278 ret
= print_tabs(session
, nesting
);
2281 ret
= lttng_metadata_printf(session
,
2282 "struct { } align(%u) _%s_padding;\n",
2283 field
->type
.u
.sequence
.elem_alignment
* CHAR_BIT
,
2288 ret
= print_tabs(session
, nesting
);
2291 ret
= lttng_metadata_printf(session
,
2292 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n",
2293 elem_type
->u
.basic
.integer
.size
,
2294 (unsigned int) elem_type
->u
.basic
.integer
.alignment
,
2295 elem_type
->u
.basic
.integer
.signedness
,
2296 (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
2298 : ((elem_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
2301 elem_type
->u
.basic
.integer
.base
,
2302 #if __BYTE_ORDER == __BIG_ENDIAN
2303 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
2305 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
2313 /* Default encoding is UTF8 */
2314 ret
= print_tabs(session
, nesting
);
2317 ret
= lttng_metadata_printf(session
,
2319 field
->type
.u
.basic
.string
.encoding
== lttng_encode_ASCII
?
2320 " { encoding = ASCII; }" : "",
2324 ret
= _lttng_struct_statedump(session
, field
, nesting
);
2326 case atype_array_compound
:
2327 ret
= _lttng_array_compound_statedump(session
, field
, nesting
);
2329 case atype_sequence_compound
:
2330 ret
= _lttng_sequence_compound_statedump(session
, field
, nesting
);
2333 ret
= _lttng_variant_statedump(session
, field
, nesting
);
2344 int _lttng_context_metadata_statedump(struct lttng_session
*session
,
2345 struct lttng_ctx
*ctx
)
2352 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
2353 const struct lttng_ctx_field
*field
= &ctx
->fields
[i
];
2355 ret
= _lttng_field_statedump(session
, &field
->event_field
, 2);
2363 int _lttng_fields_metadata_statedump(struct lttng_session
*session
,
2364 struct lttng_event
*event
)
2366 const struct lttng_event_desc
*desc
= event
->desc
;
2370 for (i
= 0; i
< desc
->nr_fields
; i
++) {
2371 const struct lttng_event_field
*field
= &desc
->fields
[i
];
2373 ret
= _lttng_field_statedump(session
, field
, 2);
2381 * Must be called with sessions_mutex held.
2382 * The entire event metadata is printed as a single atomic metadata
2386 int _lttng_event_metadata_statedump(struct lttng_session
*session
,
2387 struct lttng_channel
*chan
,
2388 struct lttng_event
*event
)
2392 if (event
->metadata_dumped
|| !LTTNG_READ_ONCE(session
->active
))
2394 if (chan
->channel_type
== METADATA_CHANNEL
)
2397 lttng_metadata_begin(session
);
2399 ret
= lttng_metadata_printf(session
,
2403 " stream_id = %u;\n",
2411 ret
= lttng_metadata_printf(session
,
2412 " context := struct {\n");
2416 ret
= _lttng_context_metadata_statedump(session
, event
->ctx
);
2420 ret
= lttng_metadata_printf(session
,
2426 ret
= lttng_metadata_printf(session
,
2427 " fields := struct {\n"
2432 ret
= _lttng_fields_metadata_statedump(session
, event
);
2437 * LTTng space reservation can only reserve multiples of the
2440 ret
= lttng_metadata_printf(session
,
2446 event
->metadata_dumped
= 1;
2448 lttng_metadata_end(session
);
2454 * Must be called with sessions_mutex held.
2455 * The entire channel metadata is printed as a single atomic metadata
2459 int _lttng_channel_metadata_statedump(struct lttng_session
*session
,
2460 struct lttng_channel
*chan
)
2464 if (chan
->metadata_dumped
|| !LTTNG_READ_ONCE(session
->active
))
2467 if (chan
->channel_type
== METADATA_CHANNEL
)
2470 lttng_metadata_begin(session
);
2472 WARN_ON_ONCE(!chan
->header_type
);
2473 ret
= lttng_metadata_printf(session
,
2476 " event.header := %s;\n"
2477 " packet.context := struct packet_context;\n",
2479 chan
->header_type
== 1 ? "struct event_header_compact" :
2480 "struct event_header_large");
2485 ret
= lttng_metadata_printf(session
,
2486 " event.context := struct {\n");
2490 ret
= _lttng_context_metadata_statedump(session
, chan
->ctx
);
2494 ret
= lttng_metadata_printf(session
,
2500 ret
= lttng_metadata_printf(session
,
2503 chan
->metadata_dumped
= 1;
2505 lttng_metadata_end(session
);
2510 * Must be called with sessions_mutex held.
2513 int _lttng_stream_packet_context_declare(struct lttng_session
*session
)
2515 return lttng_metadata_printf(session
,
2516 "struct packet_context {\n"
2517 " uint64_clock_monotonic_t timestamp_begin;\n"
2518 " uint64_clock_monotonic_t timestamp_end;\n"
2519 " uint64_t content_size;\n"
2520 " uint64_t packet_size;\n"
2521 " uint64_t packet_seq_num;\n"
2522 " unsigned long events_discarded;\n"
2523 " uint32_t cpu_id;\n"
2530 * id: range: 0 - 30.
2531 * id 31 is reserved to indicate an extended header.
2534 * id: range: 0 - 65534.
2535 * id 65535 is reserved to indicate an extended header.
2537 * Must be called with sessions_mutex held.
2540 int _lttng_event_header_declare(struct lttng_session
*session
)
2542 return lttng_metadata_printf(session
,
2543 "struct event_header_compact {\n"
2544 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
2547 " uint27_clock_monotonic_t timestamp;\n"
2551 " uint64_clock_monotonic_t timestamp;\n"
2556 "struct event_header_large {\n"
2557 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
2560 " uint32_clock_monotonic_t timestamp;\n"
2564 " uint64_clock_monotonic_t timestamp;\n"
2568 lttng_alignof(uint32_t) * CHAR_BIT
,
2569 lttng_alignof(uint16_t) * CHAR_BIT
2574 * Approximation of NTP time of day to clock monotonic correlation,
2575 * taken at start of trace.
2576 * Yes, this is only an approximation. Yes, we can (and will) do better
2577 * in future versions.
2578 * This function may return a negative offset. It may happen if the
2579 * system sets the REALTIME clock to 0 after boot.
2581 * Use 64bit timespec on kernels that have it, this makes 32bit arch
2585 int64_t measure_clock_offset(void)
2587 uint64_t monotonic_avg
, monotonic
[2], realtime
;
2588 uint64_t tcf
= trace_clock_freq();
2590 unsigned long flags
;
2591 #ifdef LTTNG_KERNEL_HAS_TIMESPEC64
2592 struct timespec64 rts
= { 0, 0 };
2594 struct timespec rts
= { 0, 0 };
2597 /* Disable interrupts to increase correlation precision. */
2598 local_irq_save(flags
);
2599 monotonic
[0] = trace_clock_read64();
2600 #ifdef LTTNG_KERNEL_HAS_TIMESPEC64
2601 ktime_get_real_ts64(&rts
);
2603 getnstimeofday(&rts
);
2605 monotonic
[1] = trace_clock_read64();
2606 local_irq_restore(flags
);
2608 monotonic_avg
= (monotonic
[0] + monotonic
[1]) >> 1;
2609 realtime
= (uint64_t) rts
.tv_sec
* tcf
;
2610 if (tcf
== NSEC_PER_SEC
) {
2611 realtime
+= rts
.tv_nsec
;
2613 uint64_t n
= rts
.tv_nsec
* tcf
;
2615 do_div(n
, NSEC_PER_SEC
);
2618 offset
= (int64_t) realtime
- monotonic_avg
;
2623 int print_escaped_ctf_string(struct lttng_session
*session
, const char *string
)
2631 while (cur
!= '\0') {
2634 ret
= lttng_metadata_printf(session
, "%s", "\\n");
2638 ret
= lttng_metadata_printf(session
, "%c", '\\');
2641 /* We still print the current char */
2644 ret
= lttng_metadata_printf(session
, "%c", cur
);
2658 int print_metadata_escaped_field(struct lttng_session
*session
, const char *field
,
2659 const char *field_value
)
2663 ret
= lttng_metadata_printf(session
, " %s = \"", field
);
2667 ret
= print_escaped_ctf_string(session
, field_value
);
2671 ret
= lttng_metadata_printf(session
, "\";\n");
2678 * Output metadata into this session's metadata buffers.
2679 * Must be called with sessions_mutex held.
2682 int _lttng_session_metadata_statedump(struct lttng_session
*session
)
2684 unsigned char *uuid_c
= session
->uuid
.b
;
2685 unsigned char uuid_s
[37], clock_uuid_s
[BOOT_ID_LEN
];
2686 const char *product_uuid
;
2687 struct lttng_channel
*chan
;
2688 struct lttng_event
*event
;
2691 if (!LTTNG_READ_ONCE(session
->active
))
2694 lttng_metadata_begin(session
);
2696 if (session
->metadata_dumped
)
2699 snprintf(uuid_s
, sizeof(uuid_s
),
2700 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
2701 uuid_c
[0], uuid_c
[1], uuid_c
[2], uuid_c
[3],
2702 uuid_c
[4], uuid_c
[5], uuid_c
[6], uuid_c
[7],
2703 uuid_c
[8], uuid_c
[9], uuid_c
[10], uuid_c
[11],
2704 uuid_c
[12], uuid_c
[13], uuid_c
[14], uuid_c
[15]);
2706 ret
= lttng_metadata_printf(session
,
2707 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
2708 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
2709 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
2710 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
2711 "typealias integer { size = %u; align = %u; signed = false; } := unsigned long;\n"
2712 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
2713 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
2719 " byte_order = %s;\n"
2720 " packet.header := struct {\n"
2721 " uint32_t magic;\n"
2722 " uint8_t uuid[16];\n"
2723 " uint32_t stream_id;\n"
2724 " uint64_t stream_instance_id;\n"
2727 lttng_alignof(uint8_t) * CHAR_BIT
,
2728 lttng_alignof(uint16_t) * CHAR_BIT
,
2729 lttng_alignof(uint32_t) * CHAR_BIT
,
2730 lttng_alignof(uint64_t) * CHAR_BIT
,
2731 sizeof(unsigned long) * CHAR_BIT
,
2732 lttng_alignof(unsigned long) * CHAR_BIT
,
2736 #if __BYTE_ORDER == __BIG_ENDIAN
2745 ret
= lttng_metadata_printf(session
,
2747 " hostname = \"%s\";\n"
2748 " domain = \"kernel\";\n"
2749 " sysname = \"%s\";\n"
2750 " kernel_release = \"%s\";\n"
2751 " kernel_version = \"%s\";\n"
2752 " tracer_name = \"lttng-modules\";\n"
2753 " tracer_major = %d;\n"
2754 " tracer_minor = %d;\n"
2755 " tracer_patchlevel = %d;\n"
2756 " trace_buffering_scheme = \"global\";\n",
2757 current
->nsproxy
->uts_ns
->name
.nodename
,
2761 LTTNG_MODULES_MAJOR_VERSION
,
2762 LTTNG_MODULES_MINOR_VERSION
,
2763 LTTNG_MODULES_PATCHLEVEL_VERSION
2768 ret
= print_metadata_escaped_field(session
, "trace_name", session
->name
);
2771 ret
= print_metadata_escaped_field(session
, "trace_creation_datetime",
2772 session
->creation_time
);
2776 /* Add the product UUID to the 'env' section */
2777 product_uuid
= dmi_get_system_info(DMI_PRODUCT_UUID
);
2779 ret
= lttng_metadata_printf(session
,
2780 " product_uuid = \"%s\";\n",
2787 /* Close the 'env' section */
2788 ret
= lttng_metadata_printf(session
, "};\n\n");
2792 ret
= lttng_metadata_printf(session
,
2794 " name = \"%s\";\n",
2800 if (!trace_clock_uuid(clock_uuid_s
)) {
2801 ret
= lttng_metadata_printf(session
,
2802 " uuid = \"%s\";\n",
2809 ret
= lttng_metadata_printf(session
,
2810 " description = \"%s\";\n"
2811 " freq = %llu; /* Frequency, in Hz */\n"
2812 " /* clock value offset from Epoch is: offset * (1/freq) */\n"
2815 trace_clock_description(),
2816 (unsigned long long) trace_clock_freq(),
2817 (long long) measure_clock_offset()
2822 ret
= lttng_metadata_printf(session
,
2823 "typealias integer {\n"
2824 " size = 27; align = 1; signed = false;\n"
2825 " map = clock.%s.value;\n"
2826 "} := uint27_clock_monotonic_t;\n"
2828 "typealias integer {\n"
2829 " size = 32; align = %u; signed = false;\n"
2830 " map = clock.%s.value;\n"
2831 "} := uint32_clock_monotonic_t;\n"
2833 "typealias integer {\n"
2834 " size = 64; align = %u; signed = false;\n"
2835 " map = clock.%s.value;\n"
2836 "} := uint64_clock_monotonic_t;\n\n",
2838 lttng_alignof(uint32_t) * CHAR_BIT
,
2840 lttng_alignof(uint64_t) * CHAR_BIT
,
2846 ret
= _lttng_stream_packet_context_declare(session
);
2850 ret
= _lttng_event_header_declare(session
);
2855 list_for_each_entry(chan
, &session
->chan
, list
) {
2856 ret
= _lttng_channel_metadata_statedump(session
, chan
);
2861 list_for_each_entry(event
, &session
->events
, list
) {
2862 ret
= _lttng_event_metadata_statedump(session
, event
->chan
, event
);
2866 session
->metadata_dumped
= 1;
2868 lttng_metadata_end(session
);
2873 * lttng_transport_register - LTT transport registration
2874 * @transport: transport structure
2876 * Registers a transport which can be used as output to extract the data out of
2877 * LTTng. The module calling this registration function must ensure that no
2878 * trap-inducing code will be executed by the transport functions. E.g.
2879 * vmalloc_sync_mappings() must be called between a vmalloc and the moment the memory
2880 * is made visible to the transport function. This registration acts as a
2881 * vmalloc_sync_mappings. Therefore, only if the module allocates virtual memory
2882 * after its registration must it synchronize the TLBs.
2884 void lttng_transport_register(struct lttng_transport
*transport
)
2887 * Make sure no page fault can be triggered by the module about to be
2888 * registered. We deal with this here so we don't have to call
2889 * vmalloc_sync_mappings() in each module's init.
2891 wrapper_vmalloc_sync_mappings();
2893 mutex_lock(&sessions_mutex
);
2894 list_add_tail(&transport
->node
, <tng_transport_list
);
2895 mutex_unlock(&sessions_mutex
);
2897 EXPORT_SYMBOL_GPL(lttng_transport_register
);
2900 * lttng_transport_unregister - LTT transport unregistration
2901 * @transport: transport structure
2903 void lttng_transport_unregister(struct lttng_transport
*transport
)
2905 mutex_lock(&sessions_mutex
);
2906 list_del(&transport
->node
);
2907 mutex_unlock(&sessions_mutex
);
2909 EXPORT_SYMBOL_GPL(lttng_transport_unregister
);
2911 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0))
2913 enum cpuhp_state lttng_hp_prepare
;
2914 enum cpuhp_state lttng_hp_online
;
2916 static int lttng_hotplug_prepare(unsigned int cpu
, struct hlist_node
*node
)
2918 struct lttng_cpuhp_node
*lttng_node
;
2920 lttng_node
= container_of(node
, struct lttng_cpuhp_node
, node
);
2921 switch (lttng_node
->component
) {
2922 case LTTNG_RING_BUFFER_FRONTEND
:
2924 case LTTNG_RING_BUFFER_BACKEND
:
2925 return lttng_cpuhp_rb_backend_prepare(cpu
, lttng_node
);
2926 case LTTNG_RING_BUFFER_ITER
:
2928 case LTTNG_CONTEXT_PERF_COUNTERS
:
2935 static int lttng_hotplug_dead(unsigned int cpu
, struct hlist_node
*node
)
2937 struct lttng_cpuhp_node
*lttng_node
;
2939 lttng_node
= container_of(node
, struct lttng_cpuhp_node
, node
);
2940 switch (lttng_node
->component
) {
2941 case LTTNG_RING_BUFFER_FRONTEND
:
2942 return lttng_cpuhp_rb_frontend_dead(cpu
, lttng_node
);
2943 case LTTNG_RING_BUFFER_BACKEND
:
2945 case LTTNG_RING_BUFFER_ITER
:
2947 case LTTNG_CONTEXT_PERF_COUNTERS
:
2948 return lttng_cpuhp_perf_counter_dead(cpu
, lttng_node
);
2954 static int lttng_hotplug_online(unsigned int cpu
, struct hlist_node
*node
)
2956 struct lttng_cpuhp_node
*lttng_node
;
2958 lttng_node
= container_of(node
, struct lttng_cpuhp_node
, node
);
2959 switch (lttng_node
->component
) {
2960 case LTTNG_RING_BUFFER_FRONTEND
:
2961 return lttng_cpuhp_rb_frontend_online(cpu
, lttng_node
);
2962 case LTTNG_RING_BUFFER_BACKEND
:
2964 case LTTNG_RING_BUFFER_ITER
:
2965 return lttng_cpuhp_rb_iter_online(cpu
, lttng_node
);
2966 case LTTNG_CONTEXT_PERF_COUNTERS
:
2967 return lttng_cpuhp_perf_counter_online(cpu
, lttng_node
);
2973 static int lttng_hotplug_offline(unsigned int cpu
, struct hlist_node
*node
)
2975 struct lttng_cpuhp_node
*lttng_node
;
2977 lttng_node
= container_of(node
, struct lttng_cpuhp_node
, node
);
2978 switch (lttng_node
->component
) {
2979 case LTTNG_RING_BUFFER_FRONTEND
:
2980 return lttng_cpuhp_rb_frontend_offline(cpu
, lttng_node
);
2981 case LTTNG_RING_BUFFER_BACKEND
:
2983 case LTTNG_RING_BUFFER_ITER
:
2985 case LTTNG_CONTEXT_PERF_COUNTERS
:
2992 static int __init
lttng_init_cpu_hotplug(void)
2996 ret
= cpuhp_setup_state_multi(CPUHP_BP_PREPARE_DYN
, "lttng:prepare",
2997 lttng_hotplug_prepare
,
2998 lttng_hotplug_dead
);
3002 lttng_hp_prepare
= ret
;
3003 lttng_rb_set_hp_prepare(ret
);
3005 ret
= cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN
, "lttng:online",
3006 lttng_hotplug_online
,
3007 lttng_hotplug_offline
);
3009 cpuhp_remove_multi_state(lttng_hp_prepare
);
3010 lttng_hp_prepare
= 0;
3013 lttng_hp_online
= ret
;
3014 lttng_rb_set_hp_online(ret
);
3019 static void __exit
lttng_exit_cpu_hotplug(void)
3021 lttng_rb_set_hp_online(0);
3022 cpuhp_remove_multi_state(lttng_hp_online
);
3023 lttng_rb_set_hp_prepare(0);
3024 cpuhp_remove_multi_state(lttng_hp_prepare
);
3027 #else /* #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
3028 static int lttng_init_cpu_hotplug(void)
3032 static void lttng_exit_cpu_hotplug(void)
3035 #endif /* #else #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
3038 static int __init
lttng_events_init(void)
3042 ret
= wrapper_lttng_fixup_sig(THIS_MODULE
);
3045 ret
= wrapper_get_pfnblock_flags_mask_init();
3048 ret
= wrapper_get_pageblock_flags_mask_init();
3051 ret
= lttng_probes_init();
3054 ret
= lttng_context_init();
3057 ret
= lttng_tracepoint_init();
3060 event_cache
= KMEM_CACHE(lttng_event
, 0);
3065 ret
= lttng_abi_init();
3068 ret
= lttng_logger_init();
3071 ret
= lttng_init_cpu_hotplug();
3074 printk(KERN_NOTICE
"LTTng: Loaded modules v%s.%s.%s%s (%s)%s%s\n",
3075 __stringify(LTTNG_MODULES_MAJOR_VERSION
),
3076 __stringify(LTTNG_MODULES_MINOR_VERSION
),
3077 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION
),
3078 LTTNG_MODULES_EXTRAVERSION
,
3080 #ifdef LTTNG_EXTRA_VERSION_GIT
3081 LTTNG_EXTRA_VERSION_GIT
[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_GIT
,
3085 #ifdef LTTNG_EXTRA_VERSION_NAME
3086 LTTNG_EXTRA_VERSION_NAME
[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_NAME
);
3093 lttng_logger_exit();
3097 kmem_cache_destroy(event_cache
);
3099 lttng_tracepoint_exit();
3101 lttng_context_exit();
3102 printk(KERN_NOTICE
"LTTng: Failed to load modules v%s.%s.%s%s (%s)%s%s\n",
3103 __stringify(LTTNG_MODULES_MAJOR_VERSION
),
3104 __stringify(LTTNG_MODULES_MINOR_VERSION
),
3105 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION
),
3106 LTTNG_MODULES_EXTRAVERSION
,
3108 #ifdef LTTNG_EXTRA_VERSION_GIT
3109 LTTNG_EXTRA_VERSION_GIT
[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_GIT
,
3113 #ifdef LTTNG_EXTRA_VERSION_NAME
3114 LTTNG_EXTRA_VERSION_NAME
[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_NAME
);
3121 module_init(lttng_events_init
);
3123 static void __exit
lttng_events_exit(void)
3125 struct lttng_session
*session
, *tmpsession
;
3127 lttng_exit_cpu_hotplug();
3128 lttng_logger_exit();
3130 list_for_each_entry_safe(session
, tmpsession
, &sessions
, list
)
3131 lttng_session_destroy(session
);
3132 kmem_cache_destroy(event_cache
);
3133 lttng_tracepoint_exit();
3134 lttng_context_exit();
3135 printk(KERN_NOTICE
"LTTng: Unloaded modules v%s.%s.%s%s (%s)%s%s\n",
3136 __stringify(LTTNG_MODULES_MAJOR_VERSION
),
3137 __stringify(LTTNG_MODULES_MINOR_VERSION
),
3138 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION
),
3139 LTTNG_MODULES_EXTRAVERSION
,
3141 #ifdef LTTNG_EXTRA_VERSION_GIT
3142 LTTNG_EXTRA_VERSION_GIT
[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_GIT
,
3146 #ifdef LTTNG_EXTRA_VERSION_NAME
3147 LTTNG_EXTRA_VERSION_NAME
[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_NAME
);
3153 module_exit(lttng_events_exit
);
3155 #include "extra_version/patches.i"
3156 #ifdef LTTNG_EXTRA_VERSION_GIT
3157 MODULE_INFO(extra_version_git
, LTTNG_EXTRA_VERSION_GIT
);
3159 #ifdef LTTNG_EXTRA_VERSION_NAME
3160 MODULE_INFO(extra_version_name
, LTTNG_EXTRA_VERSION_NAME
);
3162 MODULE_LICENSE("GPL and additional rights");
3163 MODULE_AUTHOR("Mathieu Desnoyers <mathieu.desnoyers@efficios.com>");
3164 MODULE_DESCRIPTION("LTTng tracer");
3165 MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION
) "."
3166 __stringify(LTTNG_MODULES_MINOR_VERSION
) "."
3167 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION
)
3168 LTTNG_MODULES_EXTRAVERSION
);