1 /* SPDX-License-Identifier: (GPL-2.0-only or LGPL-2.1-only)
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 <ringbuffer/backend.h>
45 #include <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_type_statedump(struct lttng_session
*session
,
75 const struct lttng_type
*type
,
78 int _lttng_field_statedump(struct lttng_session
*session
,
79 const struct lttng_event_field
*field
,
82 void synchronize_trace(void)
84 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(5,1,0))
90 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0))
91 #ifdef CONFIG_PREEMPT_RT_FULL
94 #else /* (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)) */
95 #ifdef CONFIG_PREEMPT_RT
98 #endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)) */
101 void lttng_lock_sessions(void)
103 mutex_lock(&sessions_mutex
);
106 void lttng_unlock_sessions(void)
108 mutex_unlock(&sessions_mutex
);
112 * Called with sessions lock held.
114 int lttng_session_active(void)
116 struct lttng_session
*iter
;
118 list_for_each_entry(iter
, &sessions
, list
) {
125 struct lttng_session
*lttng_session_create(void)
127 struct lttng_session
*session
;
128 struct lttng_metadata_cache
*metadata_cache
;
131 mutex_lock(&sessions_mutex
);
132 session
= lttng_kvzalloc(sizeof(struct lttng_session
), GFP_KERNEL
);
135 INIT_LIST_HEAD(&session
->chan
);
136 INIT_LIST_HEAD(&session
->events
);
137 lttng_guid_gen(&session
->uuid
);
139 metadata_cache
= kzalloc(sizeof(struct lttng_metadata_cache
),
142 goto err_free_session
;
143 metadata_cache
->data
= vzalloc(METADATA_CACHE_DEFAULT_SIZE
);
144 if (!metadata_cache
->data
)
146 metadata_cache
->cache_alloc
= METADATA_CACHE_DEFAULT_SIZE
;
147 kref_init(&metadata_cache
->refcount
);
148 mutex_init(&metadata_cache
->lock
);
149 session
->metadata_cache
= metadata_cache
;
150 INIT_LIST_HEAD(&metadata_cache
->metadata_stream
);
151 memcpy(&metadata_cache
->uuid
, &session
->uuid
,
152 sizeof(metadata_cache
->uuid
));
153 INIT_LIST_HEAD(&session
->enablers_head
);
154 for (i
= 0; i
< LTTNG_EVENT_HT_SIZE
; i
++)
155 INIT_HLIST_HEAD(&session
->events_ht
.table
[i
]);
156 list_add(&session
->list
, &sessions
);
157 session
->pid_tracker
.session
= session
;
158 session
->pid_tracker
.tracker_type
= TRACKER_PID
;
159 session
->vpid_tracker
.session
= session
;
160 session
->vpid_tracker
.tracker_type
= TRACKER_VPID
;
161 session
->uid_tracker
.session
= session
;
162 session
->uid_tracker
.tracker_type
= TRACKER_UID
;
163 session
->vuid_tracker
.session
= session
;
164 session
->vuid_tracker
.tracker_type
= TRACKER_VUID
;
165 session
->gid_tracker
.session
= session
;
166 session
->gid_tracker
.tracker_type
= TRACKER_GID
;
167 session
->vgid_tracker
.session
= session
;
168 session
->vgid_tracker
.tracker_type
= TRACKER_VGID
;
169 mutex_unlock(&sessions_mutex
);
173 kfree(metadata_cache
);
175 lttng_kvfree(session
);
177 mutex_unlock(&sessions_mutex
);
181 void metadata_cache_destroy(struct kref
*kref
)
183 struct lttng_metadata_cache
*cache
=
184 container_of(kref
, struct lttng_metadata_cache
, refcount
);
189 void lttng_session_destroy(struct lttng_session
*session
)
191 struct lttng_channel
*chan
, *tmpchan
;
192 struct lttng_event
*event
, *tmpevent
;
193 struct lttng_metadata_stream
*metadata_stream
;
194 struct lttng_enabler
*enabler
, *tmpenabler
;
197 mutex_lock(&sessions_mutex
);
198 WRITE_ONCE(session
->active
, 0);
199 list_for_each_entry(chan
, &session
->chan
, list
) {
200 ret
= lttng_syscalls_unregister(chan
);
203 list_for_each_entry(event
, &session
->events
, list
) {
204 ret
= _lttng_event_unregister(event
);
207 synchronize_trace(); /* Wait for in-flight events to complete */
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
"LTTng: 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
;
752 case LTTNG_KERNEL_UPROBE
:
754 * Needs to be explicitly enabled after creation, since
755 * we may want to apply filters.
758 event
->registered
= 1;
761 * Populate lttng_event structure before event
766 ret
= lttng_uprobes_register(event_param
->name
,
767 event_param
->u
.uprobe
.fd
,
771 ret
= try_module_get(event
->desc
->owner
);
774 case LTTNG_KERNEL_FUNCTION
: /* Fall-through */
780 ret
= _lttng_event_metadata_statedump(chan
->session
, chan
, event
);
781 WARN_ON_ONCE(ret
> 0);
783 goto statedump_error
;
785 hlist_add_head(&event
->hlist
, head
);
786 list_add(&event
->list
, &chan
->session
->events
);
790 /* If a statedump error occurs, events will not be readable. */
792 kmem_cache_free(event_cache
, event
);
800 struct lttng_event
*lttng_event_create(struct lttng_channel
*chan
,
801 struct lttng_kernel_event
*event_param
,
803 const struct lttng_event_desc
*event_desc
,
804 enum lttng_kernel_instrumentation itype
)
806 struct lttng_event
*event
;
808 mutex_lock(&sessions_mutex
);
809 event
= _lttng_event_create(chan
, event_param
, filter
, event_desc
,
811 mutex_unlock(&sessions_mutex
);
815 /* Only used for tracepoints for now. */
817 void register_event(struct lttng_event
*event
)
819 const struct lttng_event_desc
*desc
;
822 if (event
->registered
)
826 switch (event
->instrumentation
) {
827 case LTTNG_KERNEL_TRACEPOINT
:
828 ret
= lttng_wrapper_tracepoint_probe_register(desc
->kname
,
829 desc
->probe_callback
,
832 case LTTNG_KERNEL_SYSCALL
:
833 ret
= lttng_syscall_filter_enable(event
->chan
,
836 case LTTNG_KERNEL_KPROBE
:
837 case LTTNG_KERNEL_UPROBE
:
838 case LTTNG_KERNEL_KRETPROBE
:
839 case LTTNG_KERNEL_NOOP
:
842 case LTTNG_KERNEL_FUNCTION
: /* Fall-through */
847 event
->registered
= 1;
851 * Only used internally at session destruction.
853 int _lttng_event_unregister(struct lttng_event
*event
)
855 const struct lttng_event_desc
*desc
;
858 if (!event
->registered
)
862 switch (event
->instrumentation
) {
863 case LTTNG_KERNEL_TRACEPOINT
:
864 ret
= lttng_wrapper_tracepoint_probe_unregister(event
->desc
->kname
,
865 event
->desc
->probe_callback
,
868 case LTTNG_KERNEL_KPROBE
:
869 lttng_kprobes_unregister(event
);
872 case LTTNG_KERNEL_KRETPROBE
:
873 lttng_kretprobes_unregister(event
);
876 case LTTNG_KERNEL_SYSCALL
:
877 ret
= lttng_syscall_filter_disable(event
->chan
,
880 case LTTNG_KERNEL_NOOP
:
883 case LTTNG_KERNEL_UPROBE
:
884 lttng_uprobes_unregister(event
);
887 case LTTNG_KERNEL_FUNCTION
: /* Fall-through */
892 event
->registered
= 0;
897 * Only used internally at session destruction.
900 void _lttng_event_destroy(struct lttng_event
*event
)
902 switch (event
->instrumentation
) {
903 case LTTNG_KERNEL_TRACEPOINT
:
904 lttng_event_put(event
->desc
);
906 case LTTNG_KERNEL_KPROBE
:
907 module_put(event
->desc
->owner
);
908 lttng_kprobes_destroy_private(event
);
910 case LTTNG_KERNEL_KRETPROBE
:
911 module_put(event
->desc
->owner
);
912 lttng_kretprobes_destroy_private(event
);
914 case LTTNG_KERNEL_NOOP
:
915 case LTTNG_KERNEL_SYSCALL
:
917 case LTTNG_KERNEL_UPROBE
:
918 module_put(event
->desc
->owner
);
919 lttng_uprobes_destroy_private(event
);
921 case LTTNG_KERNEL_FUNCTION
: /* Fall-through */
925 list_del(&event
->list
);
926 lttng_destroy_context(event
->ctx
);
927 kmem_cache_free(event_cache
, event
);
930 struct lttng_id_tracker
*get_tracker(struct lttng_session
*session
,
931 enum tracker_type tracker_type
)
933 switch (tracker_type
) {
935 return &session
->pid_tracker
;
937 return &session
->vpid_tracker
;
939 return &session
->uid_tracker
;
941 return &session
->vuid_tracker
;
943 return &session
->gid_tracker
;
945 return &session
->vgid_tracker
;
952 int lttng_session_track_id(struct lttng_session
*session
,
953 enum tracker_type tracker_type
, int id
)
955 struct lttng_id_tracker
*tracker
;
958 tracker
= get_tracker(session
, tracker_type
);
963 mutex_lock(&sessions_mutex
);
965 /* track all ids: destroy tracker. */
966 lttng_id_tracker_destroy(tracker
, true);
969 ret
= lttng_id_tracker_add(tracker
, id
);
971 mutex_unlock(&sessions_mutex
);
975 int lttng_session_untrack_id(struct lttng_session
*session
,
976 enum tracker_type tracker_type
, int id
)
978 struct lttng_id_tracker
*tracker
;
981 tracker
= get_tracker(session
, tracker_type
);
986 mutex_lock(&sessions_mutex
);
988 /* untrack all ids: replace by empty tracker. */
989 ret
= lttng_id_tracker_empty_set(tracker
);
991 ret
= lttng_id_tracker_del(tracker
, id
);
993 mutex_unlock(&sessions_mutex
);
998 void *id_list_start(struct seq_file
*m
, loff_t
*pos
)
1000 struct lttng_id_tracker
*id_tracker
= m
->private;
1001 struct lttng_id_tracker_rcu
*id_tracker_p
= id_tracker
->p
;
1002 struct lttng_id_hash_node
*e
;
1005 mutex_lock(&sessions_mutex
);
1007 for (i
= 0; i
< LTTNG_ID_TABLE_SIZE
; i
++) {
1008 struct hlist_head
*head
= &id_tracker_p
->id_hash
[i
];
1010 lttng_hlist_for_each_entry(e
, head
, hlist
) {
1016 /* ID tracker disabled. */
1017 if (iter
>= *pos
&& iter
== 0) {
1018 return id_tracker_p
; /* empty tracker */
1026 /* Called with sessions_mutex held. */
1028 void *id_list_next(struct seq_file
*m
, void *p
, loff_t
*ppos
)
1030 struct lttng_id_tracker
*id_tracker
= m
->private;
1031 struct lttng_id_tracker_rcu
*id_tracker_p
= id_tracker
->p
;
1032 struct lttng_id_hash_node
*e
;
1037 for (i
= 0; i
< LTTNG_ID_TABLE_SIZE
; i
++) {
1038 struct hlist_head
*head
= &id_tracker_p
->id_hash
[i
];
1040 lttng_hlist_for_each_entry(e
, head
, hlist
) {
1041 if (iter
++ >= *ppos
)
1046 /* ID tracker disabled. */
1047 if (iter
>= *ppos
&& iter
== 0)
1048 return p
; /* empty tracker */
1057 void id_list_stop(struct seq_file
*m
, void *p
)
1059 mutex_unlock(&sessions_mutex
);
1063 int id_list_show(struct seq_file
*m
, void *p
)
1065 struct lttng_id_tracker
*id_tracker
= m
->private;
1066 struct lttng_id_tracker_rcu
*id_tracker_p
= id_tracker
->p
;
1069 if (p
== id_tracker_p
) {
1070 /* Tracker disabled. */
1073 const struct lttng_id_hash_node
*e
= p
;
1075 id
= lttng_id_tracker_get_node_id(e
);
1077 switch (id_tracker
->tracker_type
) {
1079 seq_printf(m
, "process { pid = %d; };\n", id
);
1082 seq_printf(m
, "process { vpid = %d; };\n", id
);
1085 seq_printf(m
, "user { uid = %d; };\n", id
);
1088 seq_printf(m
, "user { vuid = %d; };\n", id
);
1091 seq_printf(m
, "group { gid = %d; };\n", id
);
1094 seq_printf(m
, "group { vgid = %d; };\n", id
);
1097 seq_printf(m
, "UNKNOWN { field = %d };\n", id
);
1103 const struct seq_operations lttng_tracker_ids_list_seq_ops
= {
1104 .start
= id_list_start
,
1105 .next
= id_list_next
,
1106 .stop
= id_list_stop
,
1107 .show
= id_list_show
,
1111 int lttng_tracker_ids_list_open(struct inode
*inode
, struct file
*file
)
1113 return seq_open(file
, <tng_tracker_ids_list_seq_ops
);
1117 int lttng_tracker_ids_list_release(struct inode
*inode
, struct file
*file
)
1119 struct seq_file
*m
= file
->private_data
;
1120 struct lttng_id_tracker
*id_tracker
= m
->private;
1123 WARN_ON_ONCE(!id_tracker
);
1124 ret
= seq_release(inode
, file
);
1126 fput(id_tracker
->session
->file
);
1130 const struct file_operations lttng_tracker_ids_list_fops
= {
1131 .owner
= THIS_MODULE
,
1132 .open
= lttng_tracker_ids_list_open
,
1134 .llseek
= seq_lseek
,
1135 .release
= lttng_tracker_ids_list_release
,
1138 int lttng_session_list_tracker_ids(struct lttng_session
*session
,
1139 enum tracker_type tracker_type
)
1141 struct file
*tracker_ids_list_file
;
1145 file_fd
= lttng_get_unused_fd();
1151 tracker_ids_list_file
= anon_inode_getfile("[lttng_tracker_ids_list]",
1152 <tng_tracker_ids_list_fops
,
1154 if (IS_ERR(tracker_ids_list_file
)) {
1155 ret
= PTR_ERR(tracker_ids_list_file
);
1158 if (!atomic_long_add_unless(&session
->file
->f_count
, 1, LONG_MAX
)) {
1160 goto refcount_error
;
1162 ret
= lttng_tracker_ids_list_fops
.open(NULL
, tracker_ids_list_file
);
1165 m
= tracker_ids_list_file
->private_data
;
1167 m
->private = get_tracker(session
, tracker_type
);
1168 BUG_ON(!m
->private);
1169 fd_install(file_fd
, tracker_ids_list_file
);
1174 atomic_long_dec(&session
->file
->f_count
);
1176 fput(tracker_ids_list_file
);
1178 put_unused_fd(file_fd
);
1184 * Enabler management.
1187 int lttng_match_enabler_star_glob(const char *desc_name
,
1188 const char *pattern
)
1190 if (!strutils_star_glob_match(pattern
, LTTNG_SIZE_MAX
,
1191 desc_name
, LTTNG_SIZE_MAX
))
1197 int lttng_match_enabler_name(const char *desc_name
,
1200 if (strcmp(desc_name
, name
))
1206 int lttng_desc_match_enabler(const struct lttng_event_desc
*desc
,
1207 struct lttng_enabler
*enabler
)
1209 const char *desc_name
, *enabler_name
;
1211 enabler_name
= enabler
->event_param
.name
;
1212 switch (enabler
->event_param
.instrumentation
) {
1213 case LTTNG_KERNEL_TRACEPOINT
:
1214 desc_name
= desc
->name
;
1216 case LTTNG_KERNEL_SYSCALL
:
1217 desc_name
= desc
->name
;
1218 if (!strncmp(desc_name
, "compat_", strlen("compat_")))
1219 desc_name
+= strlen("compat_");
1220 if (!strncmp(desc_name
, "syscall_exit_",
1221 strlen("syscall_exit_"))) {
1222 desc_name
+= strlen("syscall_exit_");
1223 } else if (!strncmp(desc_name
, "syscall_entry_",
1224 strlen("syscall_entry_"))) {
1225 desc_name
+= strlen("syscall_entry_");
1235 switch (enabler
->type
) {
1236 case LTTNG_ENABLER_STAR_GLOB
:
1237 return lttng_match_enabler_star_glob(desc_name
, enabler_name
);
1238 case LTTNG_ENABLER_NAME
:
1239 return lttng_match_enabler_name(desc_name
, enabler_name
);
1246 int lttng_event_match_enabler(struct lttng_event
*event
,
1247 struct lttng_enabler
*enabler
)
1249 if (enabler
->event_param
.instrumentation
!= event
->instrumentation
)
1251 if (lttng_desc_match_enabler(event
->desc
, enabler
)
1252 && event
->chan
== enabler
->chan
)
1259 struct lttng_enabler_ref
*lttng_event_enabler_ref(struct lttng_event
*event
,
1260 struct lttng_enabler
*enabler
)
1262 struct lttng_enabler_ref
*enabler_ref
;
1264 list_for_each_entry(enabler_ref
,
1265 &event
->enablers_ref_head
, node
) {
1266 if (enabler_ref
->ref
== enabler
)
1273 void lttng_create_tracepoint_if_missing(struct lttng_enabler
*enabler
)
1275 struct lttng_session
*session
= enabler
->chan
->session
;
1276 struct lttng_probe_desc
*probe_desc
;
1277 const struct lttng_event_desc
*desc
;
1279 struct list_head
*probe_list
;
1281 probe_list
= lttng_get_probe_list_head();
1283 * For each probe event, if we find that a probe event matches
1284 * our enabler, create an associated lttng_event if not
1287 list_for_each_entry(probe_desc
, probe_list
, head
) {
1288 for (i
= 0; i
< probe_desc
->nr_events
; i
++) {
1290 struct hlist_head
*head
;
1291 const char *event_name
;
1294 struct lttng_event
*event
;
1296 desc
= probe_desc
->event_desc
[i
];
1297 if (!lttng_desc_match_enabler(desc
, enabler
))
1299 event_name
= desc
->name
;
1300 name_len
= strlen(event_name
);
1303 * Check if already created.
1305 hash
= jhash(event_name
, name_len
, 0);
1306 head
= &session
->events_ht
.table
[hash
& (LTTNG_EVENT_HT_SIZE
- 1)];
1307 lttng_hlist_for_each_entry(event
, head
, hlist
) {
1308 if (event
->desc
== desc
1309 && event
->chan
== enabler
->chan
)
1316 * We need to create an event for this
1319 event
= _lttng_event_create(enabler
->chan
,
1321 LTTNG_KERNEL_TRACEPOINT
);
1323 printk(KERN_INFO
"LTTng: Unable to create event %s\n",
1324 probe_desc
->event_desc
[i
]->name
);
1331 void lttng_create_syscall_if_missing(struct lttng_enabler
*enabler
)
1335 ret
= lttng_syscalls_register(enabler
->chan
, NULL
);
1340 * Create struct lttng_event if it is missing and present in the list of
1341 * tracepoint probes.
1342 * Should be called with sessions mutex held.
1345 void lttng_create_event_if_missing(struct lttng_enabler
*enabler
)
1347 switch (enabler
->event_param
.instrumentation
) {
1348 case LTTNG_KERNEL_TRACEPOINT
:
1349 lttng_create_tracepoint_if_missing(enabler
);
1351 case LTTNG_KERNEL_SYSCALL
:
1352 lttng_create_syscall_if_missing(enabler
);
1361 * Create events associated with an enabler (if not already present),
1362 * and add backward reference from the event to the enabler.
1363 * Should be called with sessions mutex held.
1366 int lttng_enabler_ref_events(struct lttng_enabler
*enabler
)
1368 struct lttng_session
*session
= enabler
->chan
->session
;
1369 struct lttng_event
*event
;
1371 /* First ensure that probe events are created for this enabler. */
1372 lttng_create_event_if_missing(enabler
);
1374 /* For each event matching enabler in session event list. */
1375 list_for_each_entry(event
, &session
->events
, list
) {
1376 struct lttng_enabler_ref
*enabler_ref
;
1378 if (!lttng_event_match_enabler(event
, enabler
))
1380 enabler_ref
= lttng_event_enabler_ref(event
, enabler
);
1383 * If no backward ref, create it.
1384 * Add backward ref from event to enabler.
1386 enabler_ref
= kzalloc(sizeof(*enabler_ref
), GFP_KERNEL
);
1389 enabler_ref
->ref
= enabler
;
1390 list_add(&enabler_ref
->node
,
1391 &event
->enablers_ref_head
);
1395 * Link filter bytecodes if not linked yet.
1397 lttng_enabler_event_link_bytecode(event
, enabler
);
1399 /* TODO: merge event context. */
1405 * Called at module load: connect the probe on all enablers matching
1407 * Called with sessions lock held.
1409 int lttng_fix_pending_events(void)
1411 struct lttng_session
*session
;
1413 list_for_each_entry(session
, &sessions
, list
)
1414 lttng_session_lazy_sync_enablers(session
);
1418 struct lttng_enabler
*lttng_enabler_create(enum lttng_enabler_type type
,
1419 struct lttng_kernel_event
*event_param
,
1420 struct lttng_channel
*chan
)
1422 struct lttng_enabler
*enabler
;
1424 enabler
= kzalloc(sizeof(*enabler
), GFP_KERNEL
);
1427 enabler
->type
= type
;
1428 INIT_LIST_HEAD(&enabler
->filter_bytecode_head
);
1429 memcpy(&enabler
->event_param
, event_param
,
1430 sizeof(enabler
->event_param
));
1431 enabler
->chan
= chan
;
1433 enabler
->enabled
= 0;
1434 enabler
->evtype
= LTTNG_TYPE_ENABLER
;
1435 mutex_lock(&sessions_mutex
);
1436 list_add(&enabler
->node
, &enabler
->chan
->session
->enablers_head
);
1437 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
1438 mutex_unlock(&sessions_mutex
);
1442 int lttng_enabler_enable(struct lttng_enabler
*enabler
)
1444 mutex_lock(&sessions_mutex
);
1445 enabler
->enabled
= 1;
1446 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
1447 mutex_unlock(&sessions_mutex
);
1451 int lttng_enabler_disable(struct lttng_enabler
*enabler
)
1453 mutex_lock(&sessions_mutex
);
1454 enabler
->enabled
= 0;
1455 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
1456 mutex_unlock(&sessions_mutex
);
1460 int lttng_enabler_attach_bytecode(struct lttng_enabler
*enabler
,
1461 struct lttng_kernel_filter_bytecode __user
*bytecode
)
1463 struct lttng_filter_bytecode_node
*bytecode_node
;
1464 uint32_t bytecode_len
;
1467 ret
= get_user(bytecode_len
, &bytecode
->len
);
1470 bytecode_node
= kzalloc(sizeof(*bytecode_node
) + bytecode_len
,
1474 ret
= copy_from_user(&bytecode_node
->bc
, bytecode
,
1475 sizeof(*bytecode
) + bytecode_len
);
1478 bytecode_node
->enabler
= enabler
;
1479 /* Enforce length based on allocated size */
1480 bytecode_node
->bc
.len
= bytecode_len
;
1481 list_add_tail(&bytecode_node
->node
, &enabler
->filter_bytecode_head
);
1482 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
1486 kfree(bytecode_node
);
1490 int lttng_event_add_callsite(struct lttng_event
*event
,
1491 struct lttng_kernel_event_callsite __user
*callsite
)
1494 switch (event
->instrumentation
) {
1495 case LTTNG_KERNEL_UPROBE
:
1496 return lttng_uprobes_add_callsite(event
, callsite
);
1502 int lttng_enabler_attach_context(struct lttng_enabler
*enabler
,
1503 struct lttng_kernel_context
*context_param
)
1509 void lttng_enabler_destroy(struct lttng_enabler
*enabler
)
1511 struct lttng_filter_bytecode_node
*filter_node
, *tmp_filter_node
;
1513 /* Destroy filter bytecode */
1514 list_for_each_entry_safe(filter_node
, tmp_filter_node
,
1515 &enabler
->filter_bytecode_head
, node
) {
1519 /* Destroy contexts */
1520 lttng_destroy_context(enabler
->ctx
);
1522 list_del(&enabler
->node
);
1527 * lttng_session_sync_enablers should be called just before starting a
1529 * Should be called with sessions mutex held.
1532 void lttng_session_sync_enablers(struct lttng_session
*session
)
1534 struct lttng_enabler
*enabler
;
1535 struct lttng_event
*event
;
1537 list_for_each_entry(enabler
, &session
->enablers_head
, node
)
1538 lttng_enabler_ref_events(enabler
);
1540 * For each event, if at least one of its enablers is enabled,
1541 * and its channel and session transient states are enabled, we
1542 * enable the event, else we disable it.
1544 list_for_each_entry(event
, &session
->events
, list
) {
1545 struct lttng_enabler_ref
*enabler_ref
;
1546 struct lttng_bytecode_runtime
*runtime
;
1547 int enabled
= 0, has_enablers_without_bytecode
= 0;
1549 switch (event
->instrumentation
) {
1550 case LTTNG_KERNEL_TRACEPOINT
:
1551 case LTTNG_KERNEL_SYSCALL
:
1553 list_for_each_entry(enabler_ref
,
1554 &event
->enablers_ref_head
, node
) {
1555 if (enabler_ref
->ref
->enabled
) {
1562 /* Not handled with lazy sync. */
1566 * Enabled state is based on union of enablers, with
1567 * intesection of session and channel transient enable
1570 enabled
= enabled
&& session
->tstate
&& event
->chan
->tstate
;
1572 WRITE_ONCE(event
->enabled
, enabled
);
1574 * Sync tracepoint registration with event enabled
1578 register_event(event
);
1580 _lttng_event_unregister(event
);
1583 /* Check if has enablers without bytecode enabled */
1584 list_for_each_entry(enabler_ref
,
1585 &event
->enablers_ref_head
, node
) {
1586 if (enabler_ref
->ref
->enabled
1587 && list_empty(&enabler_ref
->ref
->filter_bytecode_head
)) {
1588 has_enablers_without_bytecode
= 1;
1592 event
->has_enablers_without_bytecode
=
1593 has_enablers_without_bytecode
;
1595 /* Enable filters */
1596 list_for_each_entry(runtime
,
1597 &event
->bytecode_runtime_head
, node
)
1598 lttng_filter_sync_state(runtime
);
1603 * Apply enablers to session events, adding events to session if need
1604 * be. It is required after each modification applied to an active
1605 * session, and right before session "start".
1606 * "lazy" sync means we only sync if required.
1607 * Should be called with sessions mutex held.
1610 void lttng_session_lazy_sync_enablers(struct lttng_session
*session
)
1612 /* We can skip if session is not active */
1613 if (!session
->active
)
1615 lttng_session_sync_enablers(session
);
1619 * Serialize at most one packet worth of metadata into a metadata
1621 * We grab the metadata cache mutex to get exclusive access to our metadata
1622 * buffer and to the metadata cache. Exclusive access to the metadata buffer
1623 * allows us to do racy operations such as looking for remaining space left in
1624 * packet and write, since mutual exclusion protects us from concurrent writes.
1625 * Mutual exclusion on the metadata cache allow us to read the cache content
1626 * without racing against reallocation of the cache by updates.
1627 * Returns the number of bytes written in the channel, 0 if no data
1628 * was written and a negative value on error.
1630 int lttng_metadata_output_channel(struct lttng_metadata_stream
*stream
,
1631 struct channel
*chan
, bool *coherent
)
1633 struct lib_ring_buffer_ctx ctx
;
1635 size_t len
, reserve_len
;
1638 * Ensure we support mutiple get_next / put sequences followed by
1639 * put_next. The metadata cache lock protects reading the metadata
1640 * cache. It can indeed be read concurrently by "get_next_subbuf" and
1641 * "flush" operations on the buffer invoked by different processes.
1642 * Moreover, since the metadata cache memory can be reallocated, we
1643 * need to have exclusive access against updates even though we only
1646 mutex_lock(&stream
->metadata_cache
->lock
);
1647 WARN_ON(stream
->metadata_in
< stream
->metadata_out
);
1648 if (stream
->metadata_in
!= stream
->metadata_out
)
1651 /* Metadata regenerated, change the version. */
1652 if (stream
->metadata_cache
->version
!= stream
->version
)
1653 stream
->version
= stream
->metadata_cache
->version
;
1655 len
= stream
->metadata_cache
->metadata_written
-
1656 stream
->metadata_in
;
1659 reserve_len
= min_t(size_t,
1660 stream
->transport
->ops
.packet_avail_size(chan
),
1662 lib_ring_buffer_ctx_init(&ctx
, chan
, NULL
, reserve_len
,
1665 * If reservation failed, return an error to the caller.
1667 ret
= stream
->transport
->ops
.event_reserve(&ctx
, 0);
1669 printk(KERN_WARNING
"LTTng: Metadata event reservation failed\n");
1670 stream
->coherent
= false;
1673 stream
->transport
->ops
.event_write(&ctx
,
1674 stream
->metadata_cache
->data
+ stream
->metadata_in
,
1676 stream
->transport
->ops
.event_commit(&ctx
);
1677 stream
->metadata_in
+= reserve_len
;
1678 if (reserve_len
< len
)
1679 stream
->coherent
= false;
1681 stream
->coherent
= true;
1686 *coherent
= stream
->coherent
;
1687 mutex_unlock(&stream
->metadata_cache
->lock
);
1692 void lttng_metadata_begin(struct lttng_session
*session
)
1694 if (atomic_inc_return(&session
->metadata_cache
->producing
) == 1)
1695 mutex_lock(&session
->metadata_cache
->lock
);
1699 void lttng_metadata_end(struct lttng_session
*session
)
1701 WARN_ON_ONCE(!atomic_read(&session
->metadata_cache
->producing
));
1702 if (atomic_dec_return(&session
->metadata_cache
->producing
) == 0) {
1703 struct lttng_metadata_stream
*stream
;
1705 list_for_each_entry(stream
, &session
->metadata_cache
->metadata_stream
, list
)
1706 wake_up_interruptible(&stream
->read_wait
);
1707 mutex_unlock(&session
->metadata_cache
->lock
);
1712 * Write the metadata to the metadata cache.
1713 * Must be called with sessions_mutex held.
1714 * The metadata cache lock protects us from concurrent read access from
1715 * thread outputting metadata content to ring buffer.
1716 * The content of the printf is printed as a single atomic metadata
1719 int lttng_metadata_printf(struct lttng_session
*session
,
1720 const char *fmt
, ...)
1726 WARN_ON_ONCE(!LTTNG_READ_ONCE(session
->active
));
1729 str
= kvasprintf(GFP_KERNEL
, fmt
, ap
);
1735 WARN_ON_ONCE(!atomic_read(&session
->metadata_cache
->producing
));
1736 if (session
->metadata_cache
->metadata_written
+ len
>
1737 session
->metadata_cache
->cache_alloc
) {
1738 char *tmp_cache_realloc
;
1739 unsigned int tmp_cache_alloc_size
;
1741 tmp_cache_alloc_size
= max_t(unsigned int,
1742 session
->metadata_cache
->cache_alloc
+ len
,
1743 session
->metadata_cache
->cache_alloc
<< 1);
1744 tmp_cache_realloc
= vzalloc(tmp_cache_alloc_size
);
1745 if (!tmp_cache_realloc
)
1747 if (session
->metadata_cache
->data
) {
1748 memcpy(tmp_cache_realloc
,
1749 session
->metadata_cache
->data
,
1750 session
->metadata_cache
->cache_alloc
);
1751 vfree(session
->metadata_cache
->data
);
1754 session
->metadata_cache
->cache_alloc
= tmp_cache_alloc_size
;
1755 session
->metadata_cache
->data
= tmp_cache_realloc
;
1757 memcpy(session
->metadata_cache
->data
+
1758 session
->metadata_cache
->metadata_written
,
1760 session
->metadata_cache
->metadata_written
+= len
;
1771 int print_tabs(struct lttng_session
*session
, size_t nesting
)
1775 for (i
= 0; i
< nesting
; i
++) {
1778 ret
= lttng_metadata_printf(session
, " ");
1787 int lttng_field_name_statedump(struct lttng_session
*session
,
1788 const struct lttng_event_field
*field
,
1791 return lttng_metadata_printf(session
, " _%s;\n", field
->name
);
1795 int _lttng_integer_type_statedump(struct lttng_session
*session
,
1796 const struct lttng_type
*type
,
1801 WARN_ON_ONCE(type
->atype
!= atype_integer
);
1802 ret
= print_tabs(session
, nesting
);
1805 ret
= lttng_metadata_printf(session
,
1806 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s }",
1807 type
->u
.integer
.size
,
1808 type
->u
.integer
.alignment
,
1809 type
->u
.integer
.signedness
,
1810 (type
->u
.integer
.encoding
== lttng_encode_none
)
1812 : (type
->u
.integer
.encoding
== lttng_encode_UTF8
)
1815 type
->u
.integer
.base
,
1816 #if __BYTE_ORDER == __BIG_ENDIAN
1817 type
->u
.integer
.reverse_byte_order
? " byte_order = le;" : ""
1819 type
->u
.integer
.reverse_byte_order
? " byte_order = be;" : ""
1826 * Must be called with sessions_mutex held.
1829 int _lttng_struct_type_statedump(struct lttng_session
*session
,
1830 const struct lttng_type
*type
,
1834 uint32_t i
, nr_fields
;
1835 unsigned int alignment
;
1837 WARN_ON_ONCE(type
->atype
!= atype_struct_nestable
);
1839 ret
= print_tabs(session
, nesting
);
1842 ret
= lttng_metadata_printf(session
,
1846 nr_fields
= type
->u
.struct_nestable
.nr_fields
;
1847 for (i
= 0; i
< nr_fields
; i
++) {
1848 const struct lttng_event_field
*iter_field
;
1850 iter_field
= &type
->u
.struct_nestable
.fields
[i
];
1851 ret
= _lttng_field_statedump(session
, iter_field
, nesting
+ 1);
1855 ret
= print_tabs(session
, nesting
);
1858 alignment
= type
->u
.struct_nestable
.alignment
;
1860 ret
= lttng_metadata_printf(session
,
1864 ret
= lttng_metadata_printf(session
,
1871 * Must be called with sessions_mutex held.
1874 int _lttng_struct_field_statedump(struct lttng_session
*session
,
1875 const struct lttng_event_field
*field
,
1880 ret
= _lttng_struct_type_statedump(session
,
1881 &field
->type
, nesting
);
1884 return lttng_field_name_statedump(session
, field
, nesting
);
1888 * Must be called with sessions_mutex held.
1891 int _lttng_variant_type_statedump(struct lttng_session
*session
,
1892 const struct lttng_type
*type
,
1896 uint32_t i
, nr_choices
;
1898 WARN_ON_ONCE(type
->atype
!= atype_variant_nestable
);
1900 * CTF 1.8 does not allow expressing nonzero variant alignment in a nestable way.
1902 if (type
->u
.variant_nestable
.alignment
!= 0)
1904 ret
= print_tabs(session
, nesting
);
1907 ret
= lttng_metadata_printf(session
,
1908 "variant <_%s> {\n",
1909 type
->u
.variant_nestable
.tag_name
);
1912 nr_choices
= type
->u
.variant_nestable
.nr_choices
;
1913 for (i
= 0; i
< nr_choices
; i
++) {
1914 const struct lttng_event_field
*iter_field
;
1916 iter_field
= &type
->u
.variant_nestable
.choices
[i
];
1917 ret
= _lttng_field_statedump(session
, iter_field
, nesting
+ 1);
1921 ret
= print_tabs(session
, nesting
);
1924 ret
= lttng_metadata_printf(session
,
1930 * Must be called with sessions_mutex held.
1933 int _lttng_variant_field_statedump(struct lttng_session
*session
,
1934 const struct lttng_event_field
*field
,
1939 ret
= _lttng_variant_type_statedump(session
,
1940 &field
->type
, nesting
);
1943 return lttng_field_name_statedump(session
, field
, nesting
);
1947 * Must be called with sessions_mutex held.
1950 int _lttng_array_field_statedump(struct lttng_session
*session
,
1951 const struct lttng_event_field
*field
,
1955 const struct lttng_type
*elem_type
;
1957 WARN_ON_ONCE(field
->type
.atype
!= atype_array_nestable
);
1959 if (field
->type
.u
.array_nestable
.alignment
) {
1960 ret
= print_tabs(session
, nesting
);
1963 ret
= lttng_metadata_printf(session
,
1964 "struct { } align(%u) _%s_padding;\n",
1965 field
->type
.u
.array_nestable
.alignment
* CHAR_BIT
,
1971 * Nested compound types: Only array of structures and variants are
1972 * currently supported.
1974 elem_type
= field
->type
.u
.array_nestable
.elem_type
;
1975 switch (elem_type
->atype
) {
1977 case atype_struct_nestable
:
1978 case atype_variant_nestable
:
1979 ret
= _lttng_type_statedump(session
, elem_type
, nesting
);
1987 ret
= lttng_metadata_printf(session
,
1990 field
->type
.u
.array_nestable
.length
);
1995 * Must be called with sessions_mutex held.
1998 int _lttng_sequence_field_statedump(struct lttng_session
*session
,
1999 const struct lttng_event_field
*field
,
2003 const char *length_name
;
2004 const struct lttng_type
*elem_type
;
2006 WARN_ON_ONCE(field
->type
.atype
!= atype_sequence_nestable
);
2008 length_name
= field
->type
.u
.sequence_nestable
.length_name
;
2010 if (field
->type
.u
.sequence_nestable
.alignment
) {
2011 ret
= print_tabs(session
, nesting
);
2014 ret
= lttng_metadata_printf(session
,
2015 "struct { } align(%u) _%s_padding;\n",
2016 field
->type
.u
.sequence_nestable
.alignment
* CHAR_BIT
,
2023 * Nested compound types: Only array of structures and variants are
2024 * currently supported.
2026 elem_type
= field
->type
.u
.sequence_nestable
.elem_type
;
2027 switch (elem_type
->atype
) {
2029 case atype_struct_nestable
:
2030 case atype_variant_nestable
:
2031 ret
= _lttng_type_statedump(session
, elem_type
, nesting
);
2039 ret
= lttng_metadata_printf(session
,
2042 field
->type
.u
.sequence_nestable
.length_name
);
2047 * Must be called with sessions_mutex held.
2050 int _lttng_enum_type_statedump(struct lttng_session
*session
,
2051 const struct lttng_type
*type
,
2054 const struct lttng_enum_desc
*enum_desc
;
2055 const struct lttng_type
*container_type
;
2057 unsigned int i
, nr_entries
;
2059 container_type
= type
->u
.enum_nestable
.container_type
;
2060 if (container_type
->atype
!= atype_integer
) {
2064 enum_desc
= type
->u
.enum_nestable
.desc
;
2065 nr_entries
= enum_desc
->nr_entries
;
2067 ret
= print_tabs(session
, nesting
);
2070 ret
= lttng_metadata_printf(session
, "enum : ");
2073 ret
= _lttng_integer_type_statedump(session
, container_type
, 0);
2076 ret
= lttng_metadata_printf(session
, " {\n");
2079 /* Dump all entries */
2080 for (i
= 0; i
< nr_entries
; i
++) {
2081 const struct lttng_enum_entry
*entry
= &enum_desc
->entries
[i
];
2084 ret
= print_tabs(session
, nesting
+ 1);
2087 ret
= lttng_metadata_printf(session
,
2091 len
= strlen(entry
->string
);
2092 /* Escape the character '"' */
2093 for (j
= 0; j
< len
; j
++) {
2094 char c
= entry
->string
[j
];
2098 ret
= lttng_metadata_printf(session
,
2102 ret
= lttng_metadata_printf(session
,
2106 ret
= lttng_metadata_printf(session
,
2113 ret
= lttng_metadata_printf(session
, "\"");
2117 if (entry
->options
.is_auto
) {
2118 ret
= lttng_metadata_printf(session
, ",\n");
2122 ret
= lttng_metadata_printf(session
,
2126 if (entry
->start
.signedness
)
2127 ret
= lttng_metadata_printf(session
,
2128 "%lld", (long long) entry
->start
.value
);
2130 ret
= lttng_metadata_printf(session
,
2131 "%llu", entry
->start
.value
);
2134 if (entry
->start
.signedness
== entry
->end
.signedness
&&
2136 == entry
->end
.value
) {
2137 ret
= lttng_metadata_printf(session
,
2140 if (entry
->end
.signedness
) {
2141 ret
= lttng_metadata_printf(session
,
2143 (long long) entry
->end
.value
);
2145 ret
= lttng_metadata_printf(session
,
2154 ret
= print_tabs(session
, nesting
);
2157 ret
= lttng_metadata_printf(session
, "}");
2163 * Must be called with sessions_mutex held.
2166 int _lttng_enum_field_statedump(struct lttng_session
*session
,
2167 const struct lttng_event_field
*field
,
2172 ret
= _lttng_enum_type_statedump(session
, &field
->type
, nesting
);
2175 return lttng_field_name_statedump(session
, field
, nesting
);
2179 int _lttng_integer_field_statedump(struct lttng_session
*session
,
2180 const struct lttng_event_field
*field
,
2185 ret
= _lttng_integer_type_statedump(session
, &field
->type
, nesting
);
2188 return lttng_field_name_statedump(session
, field
, nesting
);
2192 int _lttng_string_type_statedump(struct lttng_session
*session
,
2193 const struct lttng_type
*type
,
2198 WARN_ON_ONCE(type
->atype
!= atype_string
);
2199 /* Default encoding is UTF8 */
2200 ret
= print_tabs(session
, nesting
);
2203 ret
= lttng_metadata_printf(session
,
2205 type
->u
.string
.encoding
== lttng_encode_ASCII
?
2206 " { encoding = ASCII; }" : "");
2211 int _lttng_string_field_statedump(struct lttng_session
*session
,
2212 const struct lttng_event_field
*field
,
2217 WARN_ON_ONCE(field
->type
.atype
!= atype_string
);
2218 ret
= _lttng_string_type_statedump(session
, &field
->type
, nesting
);
2221 return lttng_field_name_statedump(session
, field
, nesting
);
2225 * Must be called with sessions_mutex held.
2228 int _lttng_type_statedump(struct lttng_session
*session
,
2229 const struct lttng_type
*type
,
2234 switch (type
->atype
) {
2236 ret
= _lttng_integer_type_statedump(session
, type
, nesting
);
2238 case atype_enum_nestable
:
2239 ret
= _lttng_enum_type_statedump(session
, type
, nesting
);
2242 ret
= _lttng_string_type_statedump(session
, type
, nesting
);
2244 case atype_struct_nestable
:
2245 ret
= _lttng_struct_type_statedump(session
, type
, nesting
);
2247 case atype_variant_nestable
:
2248 ret
= _lttng_variant_type_statedump(session
, type
, nesting
);
2251 /* Nested arrays and sequences are not supported yet. */
2252 case atype_array_nestable
:
2253 case atype_sequence_nestable
:
2262 * Must be called with sessions_mutex held.
2265 int _lttng_field_statedump(struct lttng_session
*session
,
2266 const struct lttng_event_field
*field
,
2271 switch (field
->type
.atype
) {
2273 ret
= _lttng_integer_field_statedump(session
, field
, nesting
);
2275 case atype_enum_nestable
:
2276 ret
= _lttng_enum_field_statedump(session
, field
, nesting
);
2279 ret
= _lttng_string_field_statedump(session
, field
, nesting
);
2281 case atype_struct_nestable
:
2282 ret
= _lttng_struct_field_statedump(session
, field
, nesting
);
2284 case atype_array_nestable
:
2285 ret
= _lttng_array_field_statedump(session
, field
, nesting
);
2287 case atype_sequence_nestable
:
2288 ret
= _lttng_sequence_field_statedump(session
, field
, nesting
);
2290 case atype_variant_nestable
:
2291 ret
= _lttng_variant_field_statedump(session
, field
, nesting
);
2302 int _lttng_context_metadata_statedump(struct lttng_session
*session
,
2303 struct lttng_ctx
*ctx
)
2310 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
2311 const struct lttng_ctx_field
*field
= &ctx
->fields
[i
];
2313 ret
= _lttng_field_statedump(session
, &field
->event_field
, 2);
2321 int _lttng_fields_metadata_statedump(struct lttng_session
*session
,
2322 struct lttng_event
*event
)
2324 const struct lttng_event_desc
*desc
= event
->desc
;
2328 for (i
= 0; i
< desc
->nr_fields
; i
++) {
2329 const struct lttng_event_field
*field
= &desc
->fields
[i
];
2331 ret
= _lttng_field_statedump(session
, field
, 2);
2339 * Must be called with sessions_mutex held.
2340 * The entire event metadata is printed as a single atomic metadata
2344 int _lttng_event_metadata_statedump(struct lttng_session
*session
,
2345 struct lttng_channel
*chan
,
2346 struct lttng_event
*event
)
2350 if (event
->metadata_dumped
|| !LTTNG_READ_ONCE(session
->active
))
2352 if (chan
->channel_type
== METADATA_CHANNEL
)
2355 lttng_metadata_begin(session
);
2357 ret
= lttng_metadata_printf(session
,
2361 " stream_id = %u;\n",
2369 ret
= lttng_metadata_printf(session
,
2370 " context := struct {\n");
2374 ret
= _lttng_context_metadata_statedump(session
, event
->ctx
);
2378 ret
= lttng_metadata_printf(session
,
2384 ret
= lttng_metadata_printf(session
,
2385 " fields := struct {\n"
2390 ret
= _lttng_fields_metadata_statedump(session
, event
);
2395 * LTTng space reservation can only reserve multiples of the
2398 ret
= lttng_metadata_printf(session
,
2404 event
->metadata_dumped
= 1;
2406 lttng_metadata_end(session
);
2412 * Must be called with sessions_mutex held.
2413 * The entire channel metadata is printed as a single atomic metadata
2417 int _lttng_channel_metadata_statedump(struct lttng_session
*session
,
2418 struct lttng_channel
*chan
)
2422 if (chan
->metadata_dumped
|| !LTTNG_READ_ONCE(session
->active
))
2425 if (chan
->channel_type
== METADATA_CHANNEL
)
2428 lttng_metadata_begin(session
);
2430 WARN_ON_ONCE(!chan
->header_type
);
2431 ret
= lttng_metadata_printf(session
,
2434 " event.header := %s;\n"
2435 " packet.context := struct packet_context;\n",
2437 chan
->header_type
== 1 ? "struct event_header_compact" :
2438 "struct event_header_large");
2443 ret
= lttng_metadata_printf(session
,
2444 " event.context := struct {\n");
2448 ret
= _lttng_context_metadata_statedump(session
, chan
->ctx
);
2452 ret
= lttng_metadata_printf(session
,
2458 ret
= lttng_metadata_printf(session
,
2461 chan
->metadata_dumped
= 1;
2463 lttng_metadata_end(session
);
2468 * Must be called with sessions_mutex held.
2471 int _lttng_stream_packet_context_declare(struct lttng_session
*session
)
2473 return lttng_metadata_printf(session
,
2474 "struct packet_context {\n"
2475 " uint64_clock_monotonic_t timestamp_begin;\n"
2476 " uint64_clock_monotonic_t timestamp_end;\n"
2477 " uint64_t content_size;\n"
2478 " uint64_t packet_size;\n"
2479 " uint64_t packet_seq_num;\n"
2480 " unsigned long events_discarded;\n"
2481 " uint32_t cpu_id;\n"
2488 * id: range: 0 - 30.
2489 * id 31 is reserved to indicate an extended header.
2492 * id: range: 0 - 65534.
2493 * id 65535 is reserved to indicate an extended header.
2495 * Must be called with sessions_mutex held.
2498 int _lttng_event_header_declare(struct lttng_session
*session
)
2500 return lttng_metadata_printf(session
,
2501 "struct event_header_compact {\n"
2502 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
2505 " uint27_clock_monotonic_t timestamp;\n"
2509 " uint64_clock_monotonic_t timestamp;\n"
2514 "struct event_header_large {\n"
2515 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
2518 " uint32_clock_monotonic_t timestamp;\n"
2522 " uint64_clock_monotonic_t timestamp;\n"
2526 lttng_alignof(uint32_t) * CHAR_BIT
,
2527 lttng_alignof(uint16_t) * CHAR_BIT
2532 * Approximation of NTP time of day to clock monotonic correlation,
2533 * taken at start of trace.
2534 * Yes, this is only an approximation. Yes, we can (and will) do better
2535 * in future versions.
2536 * This function may return a negative offset. It may happen if the
2537 * system sets the REALTIME clock to 0 after boot.
2539 * Use 64bit timespec on kernels that have it, this makes 32bit arch
2543 int64_t measure_clock_offset(void)
2545 uint64_t monotonic_avg
, monotonic
[2], realtime
;
2546 uint64_t tcf
= trace_clock_freq();
2548 unsigned long flags
;
2549 #ifdef LTTNG_KERNEL_HAS_TIMESPEC64
2550 struct timespec64 rts
= { 0, 0 };
2552 struct timespec rts
= { 0, 0 };
2555 /* Disable interrupts to increase correlation precision. */
2556 local_irq_save(flags
);
2557 monotonic
[0] = trace_clock_read64();
2558 #ifdef LTTNG_KERNEL_HAS_TIMESPEC64
2559 ktime_get_real_ts64(&rts
);
2561 getnstimeofday(&rts
);
2563 monotonic
[1] = trace_clock_read64();
2564 local_irq_restore(flags
);
2566 monotonic_avg
= (monotonic
[0] + monotonic
[1]) >> 1;
2567 realtime
= (uint64_t) rts
.tv_sec
* tcf
;
2568 if (tcf
== NSEC_PER_SEC
) {
2569 realtime
+= rts
.tv_nsec
;
2571 uint64_t n
= rts
.tv_nsec
* tcf
;
2573 do_div(n
, NSEC_PER_SEC
);
2576 offset
= (int64_t) realtime
- monotonic_avg
;
2581 int print_escaped_ctf_string(struct lttng_session
*session
, const char *string
)
2589 while (cur
!= '\0') {
2592 ret
= lttng_metadata_printf(session
, "%s", "\\n");
2596 ret
= lttng_metadata_printf(session
, "%c", '\\');
2599 /* We still print the current char */
2602 ret
= lttng_metadata_printf(session
, "%c", cur
);
2616 int print_metadata_escaped_field(struct lttng_session
*session
, const char *field
,
2617 const char *field_value
)
2621 ret
= lttng_metadata_printf(session
, " %s = \"", field
);
2625 ret
= print_escaped_ctf_string(session
, field_value
);
2629 ret
= lttng_metadata_printf(session
, "\";\n");
2636 * Output metadata into this session's metadata buffers.
2637 * Must be called with sessions_mutex held.
2640 int _lttng_session_metadata_statedump(struct lttng_session
*session
)
2642 unsigned char *uuid_c
= session
->uuid
.b
;
2643 unsigned char uuid_s
[37], clock_uuid_s
[BOOT_ID_LEN
];
2644 const char *product_uuid
;
2645 struct lttng_channel
*chan
;
2646 struct lttng_event
*event
;
2649 if (!LTTNG_READ_ONCE(session
->active
))
2652 lttng_metadata_begin(session
);
2654 if (session
->metadata_dumped
)
2657 snprintf(uuid_s
, sizeof(uuid_s
),
2658 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
2659 uuid_c
[0], uuid_c
[1], uuid_c
[2], uuid_c
[3],
2660 uuid_c
[4], uuid_c
[5], uuid_c
[6], uuid_c
[7],
2661 uuid_c
[8], uuid_c
[9], uuid_c
[10], uuid_c
[11],
2662 uuid_c
[12], uuid_c
[13], uuid_c
[14], uuid_c
[15]);
2664 ret
= lttng_metadata_printf(session
,
2665 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
2666 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
2667 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
2668 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
2669 "typealias integer { size = %u; align = %u; signed = false; } := unsigned long;\n"
2670 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
2671 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
2677 " byte_order = %s;\n"
2678 " packet.header := struct {\n"
2679 " uint32_t magic;\n"
2680 " uint8_t uuid[16];\n"
2681 " uint32_t stream_id;\n"
2682 " uint64_t stream_instance_id;\n"
2685 lttng_alignof(uint8_t) * CHAR_BIT
,
2686 lttng_alignof(uint16_t) * CHAR_BIT
,
2687 lttng_alignof(uint32_t) * CHAR_BIT
,
2688 lttng_alignof(uint64_t) * CHAR_BIT
,
2689 sizeof(unsigned long) * CHAR_BIT
,
2690 lttng_alignof(unsigned long) * CHAR_BIT
,
2694 #if __BYTE_ORDER == __BIG_ENDIAN
2703 ret
= lttng_metadata_printf(session
,
2705 " hostname = \"%s\";\n"
2706 " domain = \"kernel\";\n"
2707 " sysname = \"%s\";\n"
2708 " kernel_release = \"%s\";\n"
2709 " kernel_version = \"%s\";\n"
2710 " tracer_name = \"lttng-modules\";\n"
2711 " tracer_major = %d;\n"
2712 " tracer_minor = %d;\n"
2713 " tracer_patchlevel = %d;\n"
2714 " trace_buffering_scheme = \"global\";\n",
2715 current
->nsproxy
->uts_ns
->name
.nodename
,
2719 LTTNG_MODULES_MAJOR_VERSION
,
2720 LTTNG_MODULES_MINOR_VERSION
,
2721 LTTNG_MODULES_PATCHLEVEL_VERSION
2726 ret
= print_metadata_escaped_field(session
, "trace_name", session
->name
);
2729 ret
= print_metadata_escaped_field(session
, "trace_creation_datetime",
2730 session
->creation_time
);
2734 /* Add the product UUID to the 'env' section */
2735 product_uuid
= dmi_get_system_info(DMI_PRODUCT_UUID
);
2737 ret
= lttng_metadata_printf(session
,
2738 " product_uuid = \"%s\";\n",
2745 /* Close the 'env' section */
2746 ret
= lttng_metadata_printf(session
, "};\n\n");
2750 ret
= lttng_metadata_printf(session
,
2752 " name = \"%s\";\n",
2758 if (!trace_clock_uuid(clock_uuid_s
)) {
2759 ret
= lttng_metadata_printf(session
,
2760 " uuid = \"%s\";\n",
2767 ret
= lttng_metadata_printf(session
,
2768 " description = \"%s\";\n"
2769 " freq = %llu; /* Frequency, in Hz */\n"
2770 " /* clock value offset from Epoch is: offset * (1/freq) */\n"
2773 trace_clock_description(),
2774 (unsigned long long) trace_clock_freq(),
2775 (long long) measure_clock_offset()
2780 ret
= lttng_metadata_printf(session
,
2781 "typealias integer {\n"
2782 " size = 27; align = 1; signed = false;\n"
2783 " map = clock.%s.value;\n"
2784 "} := uint27_clock_monotonic_t;\n"
2786 "typealias integer {\n"
2787 " size = 32; align = %u; signed = false;\n"
2788 " map = clock.%s.value;\n"
2789 "} := uint32_clock_monotonic_t;\n"
2791 "typealias integer {\n"
2792 " size = 64; align = %u; signed = false;\n"
2793 " map = clock.%s.value;\n"
2794 "} := uint64_clock_monotonic_t;\n\n",
2796 lttng_alignof(uint32_t) * CHAR_BIT
,
2798 lttng_alignof(uint64_t) * CHAR_BIT
,
2804 ret
= _lttng_stream_packet_context_declare(session
);
2808 ret
= _lttng_event_header_declare(session
);
2813 list_for_each_entry(chan
, &session
->chan
, list
) {
2814 ret
= _lttng_channel_metadata_statedump(session
, chan
);
2819 list_for_each_entry(event
, &session
->events
, list
) {
2820 ret
= _lttng_event_metadata_statedump(session
, event
->chan
, event
);
2824 session
->metadata_dumped
= 1;
2826 lttng_metadata_end(session
);
2831 * lttng_transport_register - LTT transport registration
2832 * @transport: transport structure
2834 * Registers a transport which can be used as output to extract the data out of
2835 * LTTng. The module calling this registration function must ensure that no
2836 * trap-inducing code will be executed by the transport functions. E.g.
2837 * vmalloc_sync_mappings() must be called between a vmalloc and the moment the memory
2838 * is made visible to the transport function. This registration acts as a
2839 * vmalloc_sync_mappings. Therefore, only if the module allocates virtual memory
2840 * after its registration must it synchronize the TLBs.
2842 void lttng_transport_register(struct lttng_transport
*transport
)
2845 * Make sure no page fault can be triggered by the module about to be
2846 * registered. We deal with this here so we don't have to call
2847 * vmalloc_sync_mappings() in each module's init.
2849 wrapper_vmalloc_sync_mappings();
2851 mutex_lock(&sessions_mutex
);
2852 list_add_tail(&transport
->node
, <tng_transport_list
);
2853 mutex_unlock(&sessions_mutex
);
2855 EXPORT_SYMBOL_GPL(lttng_transport_register
);
2858 * lttng_transport_unregister - LTT transport unregistration
2859 * @transport: transport structure
2861 void lttng_transport_unregister(struct lttng_transport
*transport
)
2863 mutex_lock(&sessions_mutex
);
2864 list_del(&transport
->node
);
2865 mutex_unlock(&sessions_mutex
);
2867 EXPORT_SYMBOL_GPL(lttng_transport_unregister
);
2869 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0))
2871 enum cpuhp_state lttng_hp_prepare
;
2872 enum cpuhp_state lttng_hp_online
;
2874 static int lttng_hotplug_prepare(unsigned int cpu
, struct hlist_node
*node
)
2876 struct lttng_cpuhp_node
*lttng_node
;
2878 lttng_node
= container_of(node
, struct lttng_cpuhp_node
, node
);
2879 switch (lttng_node
->component
) {
2880 case LTTNG_RING_BUFFER_FRONTEND
:
2882 case LTTNG_RING_BUFFER_BACKEND
:
2883 return lttng_cpuhp_rb_backend_prepare(cpu
, lttng_node
);
2884 case LTTNG_RING_BUFFER_ITER
:
2886 case LTTNG_CONTEXT_PERF_COUNTERS
:
2893 static int lttng_hotplug_dead(unsigned int cpu
, struct hlist_node
*node
)
2895 struct lttng_cpuhp_node
*lttng_node
;
2897 lttng_node
= container_of(node
, struct lttng_cpuhp_node
, node
);
2898 switch (lttng_node
->component
) {
2899 case LTTNG_RING_BUFFER_FRONTEND
:
2900 return lttng_cpuhp_rb_frontend_dead(cpu
, lttng_node
);
2901 case LTTNG_RING_BUFFER_BACKEND
:
2903 case LTTNG_RING_BUFFER_ITER
:
2905 case LTTNG_CONTEXT_PERF_COUNTERS
:
2906 return lttng_cpuhp_perf_counter_dead(cpu
, lttng_node
);
2912 static int lttng_hotplug_online(unsigned int cpu
, struct hlist_node
*node
)
2914 struct lttng_cpuhp_node
*lttng_node
;
2916 lttng_node
= container_of(node
, struct lttng_cpuhp_node
, node
);
2917 switch (lttng_node
->component
) {
2918 case LTTNG_RING_BUFFER_FRONTEND
:
2919 return lttng_cpuhp_rb_frontend_online(cpu
, lttng_node
);
2920 case LTTNG_RING_BUFFER_BACKEND
:
2922 case LTTNG_RING_BUFFER_ITER
:
2923 return lttng_cpuhp_rb_iter_online(cpu
, lttng_node
);
2924 case LTTNG_CONTEXT_PERF_COUNTERS
:
2925 return lttng_cpuhp_perf_counter_online(cpu
, lttng_node
);
2931 static int lttng_hotplug_offline(unsigned int cpu
, struct hlist_node
*node
)
2933 struct lttng_cpuhp_node
*lttng_node
;
2935 lttng_node
= container_of(node
, struct lttng_cpuhp_node
, node
);
2936 switch (lttng_node
->component
) {
2937 case LTTNG_RING_BUFFER_FRONTEND
:
2938 return lttng_cpuhp_rb_frontend_offline(cpu
, lttng_node
);
2939 case LTTNG_RING_BUFFER_BACKEND
:
2941 case LTTNG_RING_BUFFER_ITER
:
2943 case LTTNG_CONTEXT_PERF_COUNTERS
:
2950 static int __init
lttng_init_cpu_hotplug(void)
2954 ret
= cpuhp_setup_state_multi(CPUHP_BP_PREPARE_DYN
, "lttng:prepare",
2955 lttng_hotplug_prepare
,
2956 lttng_hotplug_dead
);
2960 lttng_hp_prepare
= ret
;
2961 lttng_rb_set_hp_prepare(ret
);
2963 ret
= cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN
, "lttng:online",
2964 lttng_hotplug_online
,
2965 lttng_hotplug_offline
);
2967 cpuhp_remove_multi_state(lttng_hp_prepare
);
2968 lttng_hp_prepare
= 0;
2971 lttng_hp_online
= ret
;
2972 lttng_rb_set_hp_online(ret
);
2977 static void __exit
lttng_exit_cpu_hotplug(void)
2979 lttng_rb_set_hp_online(0);
2980 cpuhp_remove_multi_state(lttng_hp_online
);
2981 lttng_rb_set_hp_prepare(0);
2982 cpuhp_remove_multi_state(lttng_hp_prepare
);
2985 #else /* #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
2986 static int lttng_init_cpu_hotplug(void)
2990 static void lttng_exit_cpu_hotplug(void)
2993 #endif /* #else #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
2996 static int __init
lttng_events_init(void)
3000 ret
= wrapper_lttng_fixup_sig(THIS_MODULE
);
3003 ret
= wrapper_get_pfnblock_flags_mask_init();
3006 ret
= wrapper_get_pageblock_flags_mask_init();
3009 ret
= lttng_probes_init();
3012 ret
= lttng_context_init();
3015 ret
= lttng_tracepoint_init();
3018 event_cache
= KMEM_CACHE(lttng_event
, 0);
3023 ret
= lttng_abi_init();
3026 ret
= lttng_logger_init();
3029 ret
= lttng_init_cpu_hotplug();
3032 printk(KERN_NOTICE
"LTTng: Loaded modules v%s.%s.%s%s (%s)%s%s\n",
3033 __stringify(LTTNG_MODULES_MAJOR_VERSION
),
3034 __stringify(LTTNG_MODULES_MINOR_VERSION
),
3035 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION
),
3036 LTTNG_MODULES_EXTRAVERSION
,
3038 #ifdef LTTNG_EXTRA_VERSION_GIT
3039 LTTNG_EXTRA_VERSION_GIT
[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_GIT
,
3043 #ifdef LTTNG_EXTRA_VERSION_NAME
3044 LTTNG_EXTRA_VERSION_NAME
[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_NAME
);
3051 lttng_logger_exit();
3055 kmem_cache_destroy(event_cache
);
3057 lttng_tracepoint_exit();
3059 lttng_context_exit();
3060 printk(KERN_NOTICE
"LTTng: Failed to load modules v%s.%s.%s%s (%s)%s%s\n",
3061 __stringify(LTTNG_MODULES_MAJOR_VERSION
),
3062 __stringify(LTTNG_MODULES_MINOR_VERSION
),
3063 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION
),
3064 LTTNG_MODULES_EXTRAVERSION
,
3066 #ifdef LTTNG_EXTRA_VERSION_GIT
3067 LTTNG_EXTRA_VERSION_GIT
[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_GIT
,
3071 #ifdef LTTNG_EXTRA_VERSION_NAME
3072 LTTNG_EXTRA_VERSION_NAME
[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_NAME
);
3079 module_init(lttng_events_init
);
3081 static void __exit
lttng_events_exit(void)
3083 struct lttng_session
*session
, *tmpsession
;
3085 lttng_exit_cpu_hotplug();
3086 lttng_logger_exit();
3088 list_for_each_entry_safe(session
, tmpsession
, &sessions
, list
)
3089 lttng_session_destroy(session
);
3090 kmem_cache_destroy(event_cache
);
3091 lttng_tracepoint_exit();
3092 lttng_context_exit();
3093 printk(KERN_NOTICE
"LTTng: Unloaded modules v%s.%s.%s%s (%s)%s%s\n",
3094 __stringify(LTTNG_MODULES_MAJOR_VERSION
),
3095 __stringify(LTTNG_MODULES_MINOR_VERSION
),
3096 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION
),
3097 LTTNG_MODULES_EXTRAVERSION
,
3099 #ifdef LTTNG_EXTRA_VERSION_GIT
3100 LTTNG_EXTRA_VERSION_GIT
[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_GIT
,
3104 #ifdef LTTNG_EXTRA_VERSION_NAME
3105 LTTNG_EXTRA_VERSION_NAME
[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_NAME
);
3111 module_exit(lttng_events_exit
);
3113 #include <generated/patches.h>
3114 #ifdef LTTNG_EXTRA_VERSION_GIT
3115 MODULE_INFO(extra_version_git
, LTTNG_EXTRA_VERSION_GIT
);
3117 #ifdef LTTNG_EXTRA_VERSION_NAME
3118 MODULE_INFO(extra_version_name
, LTTNG_EXTRA_VERSION_NAME
);
3120 MODULE_LICENSE("GPL and additional rights");
3121 MODULE_AUTHOR("Mathieu Desnoyers <mathieu.desnoyers@efficios.com>");
3122 MODULE_DESCRIPTION("LTTng tracer");
3123 MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION
) "."
3124 __stringify(LTTNG_MODULES_MINOR_VERSION
) "."
3125 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION
)
3126 LTTNG_MODULES_EXTRAVERSION
);