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_safe(enabler
, tmpenabler
,
205 &session
->enablers_head
, node
)
206 lttng_enabler_destroy(enabler
);
207 list_for_each_entry_safe(event
, tmpevent
, &session
->events
, list
)
208 _lttng_event_destroy(event
);
209 list_for_each_entry_safe(chan
, tmpchan
, &session
->chan
, list
) {
210 BUG_ON(chan
->channel_type
== METADATA_CHANNEL
);
211 _lttng_channel_destroy(chan
);
213 mutex_lock(&session
->metadata_cache
->lock
);
214 list_for_each_entry(metadata_stream
, &session
->metadata_cache
->metadata_stream
, list
)
215 _lttng_metadata_channel_hangup(metadata_stream
);
216 mutex_unlock(&session
->metadata_cache
->lock
);
217 lttng_id_tracker_destroy(&session
->pid_tracker
, false);
218 lttng_id_tracker_destroy(&session
->vpid_tracker
, false);
219 lttng_id_tracker_destroy(&session
->uid_tracker
, false);
220 lttng_id_tracker_destroy(&session
->vuid_tracker
, false);
221 lttng_id_tracker_destroy(&session
->gid_tracker
, false);
222 lttng_id_tracker_destroy(&session
->vgid_tracker
, false);
223 kref_put(&session
->metadata_cache
->refcount
, metadata_cache_destroy
);
224 list_del(&session
->list
);
225 mutex_unlock(&sessions_mutex
);
226 lttng_kvfree(session
);
229 int lttng_session_statedump(struct lttng_session
*session
)
233 mutex_lock(&sessions_mutex
);
234 ret
= lttng_statedump_start(session
);
235 mutex_unlock(&sessions_mutex
);
239 int lttng_session_enable(struct lttng_session
*session
)
242 struct lttng_channel
*chan
;
244 mutex_lock(&sessions_mutex
);
245 if (session
->active
) {
250 /* Set transient enabler state to "enabled" */
253 /* We need to sync enablers with session before activation. */
254 lttng_session_sync_enablers(session
);
257 * Snapshot the number of events per channel to know the type of header
260 list_for_each_entry(chan
, &session
->chan
, list
) {
261 if (chan
->header_type
)
262 continue; /* don't change it if session stop/restart */
263 if (chan
->free_event_id
< 31)
264 chan
->header_type
= 1; /* compact */
266 chan
->header_type
= 2; /* large */
269 /* Clear each stream's quiescent state. */
270 list_for_each_entry(chan
, &session
->chan
, list
) {
271 if (chan
->channel_type
!= METADATA_CHANNEL
)
272 lib_ring_buffer_clear_quiescent_channel(chan
->chan
);
275 WRITE_ONCE(session
->active
, 1);
276 WRITE_ONCE(session
->been_active
, 1);
277 ret
= _lttng_session_metadata_statedump(session
);
279 WRITE_ONCE(session
->active
, 0);
282 ret
= lttng_statedump_start(session
);
284 WRITE_ONCE(session
->active
, 0);
286 mutex_unlock(&sessions_mutex
);
290 int lttng_session_disable(struct lttng_session
*session
)
293 struct lttng_channel
*chan
;
295 mutex_lock(&sessions_mutex
);
296 if (!session
->active
) {
300 WRITE_ONCE(session
->active
, 0);
302 /* Set transient enabler state to "disabled" */
304 lttng_session_sync_enablers(session
);
306 /* Set each stream's quiescent state. */
307 list_for_each_entry(chan
, &session
->chan
, list
) {
308 if (chan
->channel_type
!= METADATA_CHANNEL
)
309 lib_ring_buffer_set_quiescent_channel(chan
->chan
);
312 mutex_unlock(&sessions_mutex
);
316 int lttng_session_metadata_regenerate(struct lttng_session
*session
)
319 struct lttng_channel
*chan
;
320 struct lttng_event
*event
;
321 struct lttng_metadata_cache
*cache
= session
->metadata_cache
;
322 struct lttng_metadata_stream
*stream
;
324 mutex_lock(&sessions_mutex
);
325 if (!session
->active
) {
330 mutex_lock(&cache
->lock
);
331 memset(cache
->data
, 0, cache
->cache_alloc
);
332 cache
->metadata_written
= 0;
334 list_for_each_entry(stream
, &session
->metadata_cache
->metadata_stream
, list
) {
335 stream
->metadata_out
= 0;
336 stream
->metadata_in
= 0;
338 mutex_unlock(&cache
->lock
);
340 session
->metadata_dumped
= 0;
341 list_for_each_entry(chan
, &session
->chan
, list
) {
342 chan
->metadata_dumped
= 0;
345 list_for_each_entry(event
, &session
->events
, list
) {
346 event
->metadata_dumped
= 0;
349 ret
= _lttng_session_metadata_statedump(session
);
352 mutex_unlock(&sessions_mutex
);
356 int lttng_channel_enable(struct lttng_channel
*channel
)
360 mutex_lock(&sessions_mutex
);
361 if (channel
->channel_type
== METADATA_CHANNEL
) {
365 if (channel
->enabled
) {
369 /* Set transient enabler state to "enabled" */
371 lttng_session_sync_enablers(channel
->session
);
372 /* Set atomically the state to "enabled" */
373 WRITE_ONCE(channel
->enabled
, 1);
375 mutex_unlock(&sessions_mutex
);
379 int lttng_channel_disable(struct lttng_channel
*channel
)
383 mutex_lock(&sessions_mutex
);
384 if (channel
->channel_type
== METADATA_CHANNEL
) {
388 if (!channel
->enabled
) {
392 /* Set atomically the state to "disabled" */
393 WRITE_ONCE(channel
->enabled
, 0);
394 /* Set transient enabler state to "enabled" */
396 lttng_session_sync_enablers(channel
->session
);
398 mutex_unlock(&sessions_mutex
);
402 int lttng_event_enable(struct lttng_event
*event
)
406 mutex_lock(&sessions_mutex
);
407 if (event
->chan
->channel_type
== METADATA_CHANNEL
) {
411 if (event
->enabled
) {
415 switch (event
->instrumentation
) {
416 case LTTNG_KERNEL_TRACEPOINT
:
417 case LTTNG_KERNEL_SYSCALL
:
420 case LTTNG_KERNEL_KPROBE
:
421 case LTTNG_KERNEL_UPROBE
:
422 case LTTNG_KERNEL_NOOP
:
423 WRITE_ONCE(event
->enabled
, 1);
425 case LTTNG_KERNEL_KRETPROBE
:
426 ret
= lttng_kretprobes_event_enable_state(event
, 1);
428 case LTTNG_KERNEL_FUNCTION
: /* Fall-through. */
434 mutex_unlock(&sessions_mutex
);
438 int lttng_event_disable(struct lttng_event
*event
)
442 mutex_lock(&sessions_mutex
);
443 if (event
->chan
->channel_type
== METADATA_CHANNEL
) {
447 if (!event
->enabled
) {
451 switch (event
->instrumentation
) {
452 case LTTNG_KERNEL_TRACEPOINT
:
453 case LTTNG_KERNEL_SYSCALL
:
456 case LTTNG_KERNEL_KPROBE
:
457 case LTTNG_KERNEL_UPROBE
:
458 case LTTNG_KERNEL_NOOP
:
459 WRITE_ONCE(event
->enabled
, 0);
461 case LTTNG_KERNEL_KRETPROBE
:
462 ret
= lttng_kretprobes_event_enable_state(event
, 0);
464 case LTTNG_KERNEL_FUNCTION
: /* Fall-through. */
470 mutex_unlock(&sessions_mutex
);
474 static struct lttng_transport
*lttng_transport_find(const char *name
)
476 struct lttng_transport
*transport
;
478 list_for_each_entry(transport
, <tng_transport_list
, node
) {
479 if (!strcmp(transport
->name
, name
))
485 struct lttng_channel
*lttng_channel_create(struct lttng_session
*session
,
486 const char *transport_name
,
488 size_t subbuf_size
, size_t num_subbuf
,
489 unsigned int switch_timer_interval
,
490 unsigned int read_timer_interval
,
491 enum channel_type channel_type
)
493 struct lttng_channel
*chan
;
494 struct lttng_transport
*transport
= NULL
;
496 mutex_lock(&sessions_mutex
);
497 if (session
->been_active
&& channel_type
!= METADATA_CHANNEL
)
498 goto active
; /* Refuse to add channel to active session */
499 transport
= lttng_transport_find(transport_name
);
501 printk(KERN_WARNING
"LTTng transport %s not found\n",
505 if (!try_module_get(transport
->owner
)) {
506 printk(KERN_WARNING
"LTT : Can't lock transport module.\n");
509 chan
= kzalloc(sizeof(struct lttng_channel
), GFP_KERNEL
);
512 chan
->session
= session
;
513 chan
->id
= session
->free_chan_id
++;
514 chan
->ops
= &transport
->ops
;
516 * Note: the channel creation op already writes into the packet
517 * headers. Therefore the "chan" information used as input
518 * should be already accessible.
520 chan
->chan
= transport
->ops
.channel_create(transport_name
,
521 chan
, buf_addr
, subbuf_size
, num_subbuf
,
522 switch_timer_interval
, read_timer_interval
);
527 chan
->transport
= transport
;
528 chan
->channel_type
= channel_type
;
529 list_add(&chan
->list
, &session
->chan
);
530 mutex_unlock(&sessions_mutex
);
537 module_put(transport
->owner
);
540 mutex_unlock(&sessions_mutex
);
545 * Only used internally at session destruction for per-cpu channels, and
546 * when metadata channel is released.
547 * Needs to be called with sessions mutex held.
550 void _lttng_channel_destroy(struct lttng_channel
*chan
)
552 chan
->ops
->channel_destroy(chan
->chan
);
553 module_put(chan
->transport
->owner
);
554 list_del(&chan
->list
);
555 lttng_destroy_context(chan
->ctx
);
559 void lttng_metadata_channel_destroy(struct lttng_channel
*chan
)
561 BUG_ON(chan
->channel_type
!= METADATA_CHANNEL
);
563 /* Protect the metadata cache with the sessions_mutex. */
564 mutex_lock(&sessions_mutex
);
565 _lttng_channel_destroy(chan
);
566 mutex_unlock(&sessions_mutex
);
568 EXPORT_SYMBOL_GPL(lttng_metadata_channel_destroy
);
571 void _lttng_metadata_channel_hangup(struct lttng_metadata_stream
*stream
)
573 stream
->finalized
= 1;
574 wake_up_interruptible(&stream
->read_wait
);
578 * Supports event creation while tracing session is active.
579 * Needs to be called with sessions mutex held.
581 struct lttng_event
*_lttng_event_create(struct lttng_channel
*chan
,
582 struct lttng_kernel_event
*event_param
,
584 const struct lttng_event_desc
*event_desc
,
585 enum lttng_kernel_instrumentation itype
)
587 struct lttng_session
*session
= chan
->session
;
588 struct lttng_event
*event
;
589 const char *event_name
;
590 struct hlist_head
*head
;
595 if (chan
->free_event_id
== -1U) {
601 case LTTNG_KERNEL_TRACEPOINT
:
602 event_name
= event_desc
->name
;
604 case LTTNG_KERNEL_KPROBE
:
605 case LTTNG_KERNEL_UPROBE
:
606 case LTTNG_KERNEL_KRETPROBE
:
607 case LTTNG_KERNEL_NOOP
:
608 case LTTNG_KERNEL_SYSCALL
:
609 event_name
= event_param
->name
;
611 case LTTNG_KERNEL_FUNCTION
: /* Fall-through. */
617 name_len
= strlen(event_name
);
618 hash
= jhash(event_name
, name_len
, 0);
619 head
= &session
->events_ht
.table
[hash
& (LTTNG_EVENT_HT_SIZE
- 1)];
620 lttng_hlist_for_each_entry(event
, head
, hlist
) {
621 WARN_ON_ONCE(!event
->desc
);
622 if (!strncmp(event
->desc
->name
, event_name
,
623 LTTNG_KERNEL_SYM_NAME_LEN
- 1)
624 && chan
== event
->chan
) {
630 event
= kmem_cache_zalloc(event_cache
, GFP_KERNEL
);
636 event
->filter
= filter
;
637 event
->id
= chan
->free_event_id
++;
638 event
->instrumentation
= itype
;
639 event
->evtype
= LTTNG_TYPE_EVENT
;
640 INIT_LIST_HEAD(&event
->bytecode_runtime_head
);
641 INIT_LIST_HEAD(&event
->enablers_ref_head
);
644 case LTTNG_KERNEL_TRACEPOINT
:
645 /* Event will be enabled by enabler sync. */
647 event
->registered
= 0;
648 event
->desc
= lttng_event_get(event_name
);
653 /* Populate lttng_event structure before event registration. */
656 case LTTNG_KERNEL_KPROBE
:
658 * Needs to be explicitly enabled after creation, since
659 * we may want to apply filters.
662 event
->registered
= 1;
664 * Populate lttng_event structure before event
668 ret
= lttng_kprobes_register(event_name
,
669 event_param
->u
.kprobe
.symbol_name
,
670 event_param
->u
.kprobe
.offset
,
671 event_param
->u
.kprobe
.addr
,
677 ret
= try_module_get(event
->desc
->owner
);
680 case LTTNG_KERNEL_KRETPROBE
:
682 struct lttng_event
*event_return
;
684 /* kretprobe defines 2 events */
686 * Needs to be explicitly enabled after creation, since
687 * we may want to apply filters.
690 event
->registered
= 1;
692 kmem_cache_zalloc(event_cache
, GFP_KERNEL
);
697 event_return
->chan
= chan
;
698 event_return
->filter
= filter
;
699 event_return
->id
= chan
->free_event_id
++;
700 event_return
->enabled
= 0;
701 event_return
->registered
= 1;
702 event_return
->instrumentation
= itype
;
704 * Populate lttng_event structure before kretprobe registration.
707 ret
= lttng_kretprobes_register(event_name
,
708 event_param
->u
.kretprobe
.symbol_name
,
709 event_param
->u
.kretprobe
.offset
,
710 event_param
->u
.kretprobe
.addr
,
711 event
, event_return
);
713 kmem_cache_free(event_cache
, event_return
);
717 /* Take 2 refs on the module: one per event. */
718 ret
= try_module_get(event
->desc
->owner
);
720 ret
= try_module_get(event
->desc
->owner
);
722 ret
= _lttng_event_metadata_statedump(chan
->session
, chan
,
724 WARN_ON_ONCE(ret
> 0);
726 kmem_cache_free(event_cache
, event_return
);
727 module_put(event
->desc
->owner
);
728 module_put(event
->desc
->owner
);
729 goto statedump_error
;
731 list_add(&event_return
->list
, &chan
->session
->events
);
734 case LTTNG_KERNEL_NOOP
:
735 case LTTNG_KERNEL_SYSCALL
:
737 * Needs to be explicitly enabled after creation, since
738 * we may want to apply filters.
741 event
->registered
= 0;
742 event
->desc
= event_desc
;
748 case LTTNG_KERNEL_UPROBE
:
750 * Needs to be explicitly enabled after creation, since
751 * we may want to apply filters.
754 event
->registered
= 1;
757 * Populate lttng_event structure before event
762 ret
= lttng_uprobes_register(event_param
->name
,
763 event_param
->u
.uprobe
.fd
,
767 ret
= try_module_get(event
->desc
->owner
);
770 case LTTNG_KERNEL_FUNCTION
: /* Fall-through */
776 ret
= _lttng_event_metadata_statedump(chan
->session
, chan
, event
);
777 WARN_ON_ONCE(ret
> 0);
779 goto statedump_error
;
781 hlist_add_head(&event
->hlist
, head
);
782 list_add(&event
->list
, &chan
->session
->events
);
786 /* If a statedump error occurs, events will not be readable. */
788 kmem_cache_free(event_cache
, event
);
796 struct lttng_event
*lttng_event_create(struct lttng_channel
*chan
,
797 struct lttng_kernel_event
*event_param
,
799 const struct lttng_event_desc
*event_desc
,
800 enum lttng_kernel_instrumentation itype
)
802 struct lttng_event
*event
;
804 mutex_lock(&sessions_mutex
);
805 event
= _lttng_event_create(chan
, event_param
, filter
, event_desc
,
807 mutex_unlock(&sessions_mutex
);
811 /* Only used for tracepoints for now. */
813 void register_event(struct lttng_event
*event
)
815 const struct lttng_event_desc
*desc
;
818 if (event
->registered
)
822 switch (event
->instrumentation
) {
823 case LTTNG_KERNEL_TRACEPOINT
:
824 ret
= lttng_wrapper_tracepoint_probe_register(desc
->kname
,
825 desc
->probe_callback
,
828 case LTTNG_KERNEL_SYSCALL
:
829 ret
= lttng_syscall_filter_enable(event
->chan
,
832 case LTTNG_KERNEL_KPROBE
:
833 case LTTNG_KERNEL_UPROBE
:
834 case LTTNG_KERNEL_KRETPROBE
:
835 case LTTNG_KERNEL_NOOP
:
838 case LTTNG_KERNEL_FUNCTION
: /* Fall-through */
843 event
->registered
= 1;
847 * Only used internally at session destruction.
849 int _lttng_event_unregister(struct lttng_event
*event
)
851 const struct lttng_event_desc
*desc
;
854 if (!event
->registered
)
858 switch (event
->instrumentation
) {
859 case LTTNG_KERNEL_TRACEPOINT
:
860 ret
= lttng_wrapper_tracepoint_probe_unregister(event
->desc
->kname
,
861 event
->desc
->probe_callback
,
864 case LTTNG_KERNEL_KPROBE
:
865 lttng_kprobes_unregister(event
);
868 case LTTNG_KERNEL_KRETPROBE
:
869 lttng_kretprobes_unregister(event
);
872 case LTTNG_KERNEL_SYSCALL
:
873 ret
= lttng_syscall_filter_disable(event
->chan
,
876 case LTTNG_KERNEL_NOOP
:
879 case LTTNG_KERNEL_UPROBE
:
880 lttng_uprobes_unregister(event
);
883 case LTTNG_KERNEL_FUNCTION
: /* Fall-through */
888 event
->registered
= 0;
893 * Only used internally at session destruction.
896 void _lttng_event_destroy(struct lttng_event
*event
)
898 switch (event
->instrumentation
) {
899 case LTTNG_KERNEL_TRACEPOINT
:
900 lttng_event_put(event
->desc
);
902 case LTTNG_KERNEL_KPROBE
:
903 module_put(event
->desc
->owner
);
904 lttng_kprobes_destroy_private(event
);
906 case LTTNG_KERNEL_KRETPROBE
:
907 module_put(event
->desc
->owner
);
908 lttng_kretprobes_destroy_private(event
);
910 case LTTNG_KERNEL_NOOP
:
911 case LTTNG_KERNEL_SYSCALL
:
913 case LTTNG_KERNEL_UPROBE
:
914 module_put(event
->desc
->owner
);
915 lttng_uprobes_destroy_private(event
);
917 case LTTNG_KERNEL_FUNCTION
: /* Fall-through */
921 list_del(&event
->list
);
922 lttng_destroy_context(event
->ctx
);
923 kmem_cache_free(event_cache
, event
);
926 struct lttng_id_tracker
*get_tracker(struct lttng_session
*session
,
927 enum tracker_type tracker_type
)
929 switch (tracker_type
) {
931 return &session
->pid_tracker
;
933 return &session
->vpid_tracker
;
935 return &session
->uid_tracker
;
937 return &session
->vuid_tracker
;
939 return &session
->gid_tracker
;
941 return &session
->vgid_tracker
;
948 int lttng_session_track_id(struct lttng_session
*session
,
949 enum tracker_type tracker_type
, int id
)
951 struct lttng_id_tracker
*tracker
;
954 tracker
= get_tracker(session
, tracker_type
);
959 mutex_lock(&sessions_mutex
);
961 /* track all ids: destroy tracker. */
962 lttng_id_tracker_destroy(tracker
, true);
965 ret
= lttng_id_tracker_add(tracker
, id
);
967 mutex_unlock(&sessions_mutex
);
971 int lttng_session_untrack_id(struct lttng_session
*session
,
972 enum tracker_type tracker_type
, int id
)
974 struct lttng_id_tracker
*tracker
;
977 tracker
= get_tracker(session
, tracker_type
);
982 mutex_lock(&sessions_mutex
);
984 /* untrack all ids: replace by empty tracker. */
985 ret
= lttng_id_tracker_empty_set(tracker
);
987 ret
= lttng_id_tracker_del(tracker
, id
);
989 mutex_unlock(&sessions_mutex
);
994 void *id_list_start(struct seq_file
*m
, loff_t
*pos
)
996 struct lttng_id_tracker
*id_tracker
= m
->private;
997 struct lttng_id_tracker_rcu
*id_tracker_p
= id_tracker
->p
;
998 struct lttng_id_hash_node
*e
;
1001 mutex_lock(&sessions_mutex
);
1003 for (i
= 0; i
< LTTNG_ID_TABLE_SIZE
; i
++) {
1004 struct hlist_head
*head
= &id_tracker_p
->id_hash
[i
];
1006 lttng_hlist_for_each_entry(e
, head
, hlist
) {
1012 /* ID tracker disabled. */
1013 if (iter
>= *pos
&& iter
== 0) {
1014 return id_tracker_p
; /* empty tracker */
1022 /* Called with sessions_mutex held. */
1024 void *id_list_next(struct seq_file
*m
, void *p
, loff_t
*ppos
)
1026 struct lttng_id_tracker
*id_tracker
= m
->private;
1027 struct lttng_id_tracker_rcu
*id_tracker_p
= id_tracker
->p
;
1028 struct lttng_id_hash_node
*e
;
1033 for (i
= 0; i
< LTTNG_ID_TABLE_SIZE
; i
++) {
1034 struct hlist_head
*head
= &id_tracker_p
->id_hash
[i
];
1036 lttng_hlist_for_each_entry(e
, head
, hlist
) {
1037 if (iter
++ >= *ppos
)
1042 /* ID tracker disabled. */
1043 if (iter
>= *ppos
&& iter
== 0)
1044 return p
; /* empty tracker */
1053 void id_list_stop(struct seq_file
*m
, void *p
)
1055 mutex_unlock(&sessions_mutex
);
1059 int id_list_show(struct seq_file
*m
, void *p
)
1061 struct lttng_id_tracker
*id_tracker
= m
->private;
1062 struct lttng_id_tracker_rcu
*id_tracker_p
= id_tracker
->p
;
1065 if (p
== id_tracker_p
) {
1066 /* Tracker disabled. */
1069 const struct lttng_id_hash_node
*e
= p
;
1071 id
= lttng_id_tracker_get_node_id(e
);
1073 switch (id_tracker
->tracker_type
) {
1075 seq_printf(m
, "process { pid = %d; };\n", id
);
1078 seq_printf(m
, "process { vpid = %d; };\n", id
);
1081 seq_printf(m
, "user { uid = %d; };\n", id
);
1084 seq_printf(m
, "user { vuid = %d; };\n", id
);
1087 seq_printf(m
, "group { gid = %d; };\n", id
);
1090 seq_printf(m
, "group { vgid = %d; };\n", id
);
1093 seq_printf(m
, "UNKNOWN { field = %d };\n", id
);
1099 const struct seq_operations lttng_tracker_ids_list_seq_ops
= {
1100 .start
= id_list_start
,
1101 .next
= id_list_next
,
1102 .stop
= id_list_stop
,
1103 .show
= id_list_show
,
1107 int lttng_tracker_ids_list_open(struct inode
*inode
, struct file
*file
)
1109 return seq_open(file
, <tng_tracker_ids_list_seq_ops
);
1113 int lttng_tracker_ids_list_release(struct inode
*inode
, struct file
*file
)
1115 struct seq_file
*m
= file
->private_data
;
1116 struct lttng_id_tracker
*id_tracker
= m
->private;
1119 WARN_ON_ONCE(!id_tracker
);
1120 ret
= seq_release(inode
, file
);
1122 fput(id_tracker
->session
->file
);
1126 const struct file_operations lttng_tracker_ids_list_fops
= {
1127 .owner
= THIS_MODULE
,
1128 .open
= lttng_tracker_ids_list_open
,
1130 .llseek
= seq_lseek
,
1131 .release
= lttng_tracker_ids_list_release
,
1134 int lttng_session_list_tracker_ids(struct lttng_session
*session
,
1135 enum tracker_type tracker_type
)
1137 struct file
*tracker_ids_list_file
;
1141 file_fd
= lttng_get_unused_fd();
1147 tracker_ids_list_file
= anon_inode_getfile("[lttng_tracker_ids_list]",
1148 <tng_tracker_ids_list_fops
,
1150 if (IS_ERR(tracker_ids_list_file
)) {
1151 ret
= PTR_ERR(tracker_ids_list_file
);
1154 if (!atomic_long_add_unless(&session
->file
->f_count
, 1, LONG_MAX
)) {
1156 goto refcount_error
;
1158 ret
= lttng_tracker_ids_list_fops
.open(NULL
, tracker_ids_list_file
);
1161 m
= tracker_ids_list_file
->private_data
;
1163 m
->private = get_tracker(session
, tracker_type
);
1164 BUG_ON(!m
->private);
1165 fd_install(file_fd
, tracker_ids_list_file
);
1170 atomic_long_dec(&session
->file
->f_count
);
1172 fput(tracker_ids_list_file
);
1174 put_unused_fd(file_fd
);
1180 * Enabler management.
1183 int lttng_match_enabler_star_glob(const char *desc_name
,
1184 const char *pattern
)
1186 if (!strutils_star_glob_match(pattern
, LTTNG_SIZE_MAX
,
1187 desc_name
, LTTNG_SIZE_MAX
))
1193 int lttng_match_enabler_name(const char *desc_name
,
1196 if (strcmp(desc_name
, name
))
1202 int lttng_desc_match_enabler(const struct lttng_event_desc
*desc
,
1203 struct lttng_enabler
*enabler
)
1205 const char *desc_name
, *enabler_name
;
1207 enabler_name
= enabler
->event_param
.name
;
1208 switch (enabler
->event_param
.instrumentation
) {
1209 case LTTNG_KERNEL_TRACEPOINT
:
1210 desc_name
= desc
->name
;
1212 case LTTNG_KERNEL_SYSCALL
:
1213 desc_name
= desc
->name
;
1214 if (!strncmp(desc_name
, "compat_", strlen("compat_")))
1215 desc_name
+= strlen("compat_");
1216 if (!strncmp(desc_name
, "syscall_exit_",
1217 strlen("syscall_exit_"))) {
1218 desc_name
+= strlen("syscall_exit_");
1219 } else if (!strncmp(desc_name
, "syscall_entry_",
1220 strlen("syscall_entry_"))) {
1221 desc_name
+= strlen("syscall_entry_");
1231 switch (enabler
->type
) {
1232 case LTTNG_ENABLER_STAR_GLOB
:
1233 return lttng_match_enabler_star_glob(desc_name
, enabler_name
);
1234 case LTTNG_ENABLER_NAME
:
1235 return lttng_match_enabler_name(desc_name
, enabler_name
);
1242 int lttng_event_match_enabler(struct lttng_event
*event
,
1243 struct lttng_enabler
*enabler
)
1245 if (enabler
->event_param
.instrumentation
!= event
->instrumentation
)
1247 if (lttng_desc_match_enabler(event
->desc
, enabler
)
1248 && event
->chan
== enabler
->chan
)
1255 struct lttng_enabler_ref
*lttng_event_enabler_ref(struct lttng_event
*event
,
1256 struct lttng_enabler
*enabler
)
1258 struct lttng_enabler_ref
*enabler_ref
;
1260 list_for_each_entry(enabler_ref
,
1261 &event
->enablers_ref_head
, node
) {
1262 if (enabler_ref
->ref
== enabler
)
1269 void lttng_create_tracepoint_if_missing(struct lttng_enabler
*enabler
)
1271 struct lttng_session
*session
= enabler
->chan
->session
;
1272 struct lttng_probe_desc
*probe_desc
;
1273 const struct lttng_event_desc
*desc
;
1275 struct list_head
*probe_list
;
1277 probe_list
= lttng_get_probe_list_head();
1279 * For each probe event, if we find that a probe event matches
1280 * our enabler, create an associated lttng_event if not
1283 list_for_each_entry(probe_desc
, probe_list
, head
) {
1284 for (i
= 0; i
< probe_desc
->nr_events
; i
++) {
1286 struct hlist_head
*head
;
1287 const char *event_name
;
1290 struct lttng_event
*event
;
1292 desc
= probe_desc
->event_desc
[i
];
1293 if (!lttng_desc_match_enabler(desc
, enabler
))
1295 event_name
= desc
->name
;
1296 name_len
= strlen(event_name
);
1299 * Check if already created.
1301 hash
= jhash(event_name
, name_len
, 0);
1302 head
= &session
->events_ht
.table
[hash
& (LTTNG_EVENT_HT_SIZE
- 1)];
1303 lttng_hlist_for_each_entry(event
, head
, hlist
) {
1304 if (event
->desc
== desc
1305 && event
->chan
== enabler
->chan
)
1312 * We need to create an event for this
1315 event
= _lttng_event_create(enabler
->chan
,
1317 LTTNG_KERNEL_TRACEPOINT
);
1319 printk(KERN_INFO
"Unable to create event %s\n",
1320 probe_desc
->event_desc
[i
]->name
);
1327 void lttng_create_syscall_if_missing(struct lttng_enabler
*enabler
)
1331 ret
= lttng_syscalls_register(enabler
->chan
, NULL
);
1336 * Create struct lttng_event if it is missing and present in the list of
1337 * tracepoint probes.
1338 * Should be called with sessions mutex held.
1341 void lttng_create_event_if_missing(struct lttng_enabler
*enabler
)
1343 switch (enabler
->event_param
.instrumentation
) {
1344 case LTTNG_KERNEL_TRACEPOINT
:
1345 lttng_create_tracepoint_if_missing(enabler
);
1347 case LTTNG_KERNEL_SYSCALL
:
1348 lttng_create_syscall_if_missing(enabler
);
1357 * Create events associated with an enabler (if not already present),
1358 * and add backward reference from the event to the enabler.
1359 * Should be called with sessions mutex held.
1362 int lttng_enabler_ref_events(struct lttng_enabler
*enabler
)
1364 struct lttng_session
*session
= enabler
->chan
->session
;
1365 struct lttng_event
*event
;
1367 /* First ensure that probe events are created for this enabler. */
1368 lttng_create_event_if_missing(enabler
);
1370 /* For each event matching enabler in session event list. */
1371 list_for_each_entry(event
, &session
->events
, list
) {
1372 struct lttng_enabler_ref
*enabler_ref
;
1374 if (!lttng_event_match_enabler(event
, enabler
))
1376 enabler_ref
= lttng_event_enabler_ref(event
, enabler
);
1379 * If no backward ref, create it.
1380 * Add backward ref from event to enabler.
1382 enabler_ref
= kzalloc(sizeof(*enabler_ref
), GFP_KERNEL
);
1385 enabler_ref
->ref
= enabler
;
1386 list_add(&enabler_ref
->node
,
1387 &event
->enablers_ref_head
);
1391 * Link filter bytecodes if not linked yet.
1393 lttng_enabler_event_link_bytecode(event
, enabler
);
1395 /* TODO: merge event context. */
1401 * Called at module load: connect the probe on all enablers matching
1403 * Called with sessions lock held.
1405 int lttng_fix_pending_events(void)
1407 struct lttng_session
*session
;
1409 list_for_each_entry(session
, &sessions
, list
)
1410 lttng_session_lazy_sync_enablers(session
);
1414 struct lttng_enabler
*lttng_enabler_create(enum lttng_enabler_type type
,
1415 struct lttng_kernel_event
*event_param
,
1416 struct lttng_channel
*chan
)
1418 struct lttng_enabler
*enabler
;
1420 enabler
= kzalloc(sizeof(*enabler
), GFP_KERNEL
);
1423 enabler
->type
= type
;
1424 INIT_LIST_HEAD(&enabler
->filter_bytecode_head
);
1425 memcpy(&enabler
->event_param
, event_param
,
1426 sizeof(enabler
->event_param
));
1427 enabler
->chan
= chan
;
1429 enabler
->enabled
= 0;
1430 enabler
->evtype
= LTTNG_TYPE_ENABLER
;
1431 mutex_lock(&sessions_mutex
);
1432 list_add(&enabler
->node
, &enabler
->chan
->session
->enablers_head
);
1433 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
1434 mutex_unlock(&sessions_mutex
);
1438 int lttng_enabler_enable(struct lttng_enabler
*enabler
)
1440 mutex_lock(&sessions_mutex
);
1441 enabler
->enabled
= 1;
1442 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
1443 mutex_unlock(&sessions_mutex
);
1447 int lttng_enabler_disable(struct lttng_enabler
*enabler
)
1449 mutex_lock(&sessions_mutex
);
1450 enabler
->enabled
= 0;
1451 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
1452 mutex_unlock(&sessions_mutex
);
1456 int lttng_enabler_attach_bytecode(struct lttng_enabler
*enabler
,
1457 struct lttng_kernel_filter_bytecode __user
*bytecode
)
1459 struct lttng_filter_bytecode_node
*bytecode_node
;
1460 uint32_t bytecode_len
;
1463 ret
= get_user(bytecode_len
, &bytecode
->len
);
1466 bytecode_node
= kzalloc(sizeof(*bytecode_node
) + bytecode_len
,
1470 ret
= copy_from_user(&bytecode_node
->bc
, bytecode
,
1471 sizeof(*bytecode
) + bytecode_len
);
1474 bytecode_node
->enabler
= enabler
;
1475 /* Enforce length based on allocated size */
1476 bytecode_node
->bc
.len
= bytecode_len
;
1477 list_add_tail(&bytecode_node
->node
, &enabler
->filter_bytecode_head
);
1478 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
1482 kfree(bytecode_node
);
1486 int lttng_event_add_callsite(struct lttng_event
*event
,
1487 struct lttng_kernel_event_callsite __user
*callsite
)
1490 switch (event
->instrumentation
) {
1491 case LTTNG_KERNEL_UPROBE
:
1492 return lttng_uprobes_add_callsite(event
, callsite
);
1498 int lttng_enabler_attach_context(struct lttng_enabler
*enabler
,
1499 struct lttng_kernel_context
*context_param
)
1505 void lttng_enabler_destroy(struct lttng_enabler
*enabler
)
1507 struct lttng_filter_bytecode_node
*filter_node
, *tmp_filter_node
;
1509 /* Destroy filter bytecode */
1510 list_for_each_entry_safe(filter_node
, tmp_filter_node
,
1511 &enabler
->filter_bytecode_head
, node
) {
1515 /* Destroy contexts */
1516 lttng_destroy_context(enabler
->ctx
);
1518 list_del(&enabler
->node
);
1523 * lttng_session_sync_enablers should be called just before starting a
1525 * Should be called with sessions mutex held.
1528 void lttng_session_sync_enablers(struct lttng_session
*session
)
1530 struct lttng_enabler
*enabler
;
1531 struct lttng_event
*event
;
1533 list_for_each_entry(enabler
, &session
->enablers_head
, node
)
1534 lttng_enabler_ref_events(enabler
);
1536 * For each event, if at least one of its enablers is enabled,
1537 * and its channel and session transient states are enabled, we
1538 * enable the event, else we disable it.
1540 list_for_each_entry(event
, &session
->events
, list
) {
1541 struct lttng_enabler_ref
*enabler_ref
;
1542 struct lttng_bytecode_runtime
*runtime
;
1543 int enabled
= 0, has_enablers_without_bytecode
= 0;
1545 switch (event
->instrumentation
) {
1546 case LTTNG_KERNEL_TRACEPOINT
:
1547 case LTTNG_KERNEL_SYSCALL
:
1549 list_for_each_entry(enabler_ref
,
1550 &event
->enablers_ref_head
, node
) {
1551 if (enabler_ref
->ref
->enabled
) {
1558 /* Not handled with lazy sync. */
1562 * Enabled state is based on union of enablers, with
1563 * intesection of session and channel transient enable
1566 enabled
= enabled
&& session
->tstate
&& event
->chan
->tstate
;
1568 WRITE_ONCE(event
->enabled
, enabled
);
1570 * Sync tracepoint registration with event enabled
1574 register_event(event
);
1576 _lttng_event_unregister(event
);
1579 /* Check if has enablers without bytecode enabled */
1580 list_for_each_entry(enabler_ref
,
1581 &event
->enablers_ref_head
, node
) {
1582 if (enabler_ref
->ref
->enabled
1583 && list_empty(&enabler_ref
->ref
->filter_bytecode_head
)) {
1584 has_enablers_without_bytecode
= 1;
1588 event
->has_enablers_without_bytecode
=
1589 has_enablers_without_bytecode
;
1591 /* Enable filters */
1592 list_for_each_entry(runtime
,
1593 &event
->bytecode_runtime_head
, node
)
1594 lttng_filter_sync_state(runtime
);
1599 * Apply enablers to session events, adding events to session if need
1600 * be. It is required after each modification applied to an active
1601 * session, and right before session "start".
1602 * "lazy" sync means we only sync if required.
1603 * Should be called with sessions mutex held.
1606 void lttng_session_lazy_sync_enablers(struct lttng_session
*session
)
1608 /* We can skip if session is not active */
1609 if (!session
->active
)
1611 lttng_session_sync_enablers(session
);
1615 * Serialize at most one packet worth of metadata into a metadata
1617 * We grab the metadata cache mutex to get exclusive access to our metadata
1618 * buffer and to the metadata cache. Exclusive access to the metadata buffer
1619 * allows us to do racy operations such as looking for remaining space left in
1620 * packet and write, since mutual exclusion protects us from concurrent writes.
1621 * Mutual exclusion on the metadata cache allow us to read the cache content
1622 * without racing against reallocation of the cache by updates.
1623 * Returns the number of bytes written in the channel, 0 if no data
1624 * was written and a negative value on error.
1626 int lttng_metadata_output_channel(struct lttng_metadata_stream
*stream
,
1627 struct channel
*chan
, bool *coherent
)
1629 struct lib_ring_buffer_ctx ctx
;
1631 size_t len
, reserve_len
;
1634 * Ensure we support mutiple get_next / put sequences followed by
1635 * put_next. The metadata cache lock protects reading the metadata
1636 * cache. It can indeed be read concurrently by "get_next_subbuf" and
1637 * "flush" operations on the buffer invoked by different processes.
1638 * Moreover, since the metadata cache memory can be reallocated, we
1639 * need to have exclusive access against updates even though we only
1642 mutex_lock(&stream
->metadata_cache
->lock
);
1643 WARN_ON(stream
->metadata_in
< stream
->metadata_out
);
1644 if (stream
->metadata_in
!= stream
->metadata_out
)
1647 /* Metadata regenerated, change the version. */
1648 if (stream
->metadata_cache
->version
!= stream
->version
)
1649 stream
->version
= stream
->metadata_cache
->version
;
1651 len
= stream
->metadata_cache
->metadata_written
-
1652 stream
->metadata_in
;
1655 reserve_len
= min_t(size_t,
1656 stream
->transport
->ops
.packet_avail_size(chan
),
1658 lib_ring_buffer_ctx_init(&ctx
, chan
, NULL
, reserve_len
,
1661 * If reservation failed, return an error to the caller.
1663 ret
= stream
->transport
->ops
.event_reserve(&ctx
, 0);
1665 printk(KERN_WARNING
"LTTng: Metadata event reservation failed\n");
1666 stream
->coherent
= false;
1669 stream
->transport
->ops
.event_write(&ctx
,
1670 stream
->metadata_cache
->data
+ stream
->metadata_in
,
1672 stream
->transport
->ops
.event_commit(&ctx
);
1673 stream
->metadata_in
+= reserve_len
;
1674 if (reserve_len
< len
)
1675 stream
->coherent
= false;
1677 stream
->coherent
= true;
1682 *coherent
= stream
->coherent
;
1683 mutex_unlock(&stream
->metadata_cache
->lock
);
1688 void lttng_metadata_begin(struct lttng_session
*session
)
1690 if (atomic_inc_return(&session
->metadata_cache
->producing
) == 1)
1691 mutex_lock(&session
->metadata_cache
->lock
);
1695 void lttng_metadata_end(struct lttng_session
*session
)
1697 WARN_ON_ONCE(!atomic_read(&session
->metadata_cache
->producing
));
1698 if (atomic_dec_return(&session
->metadata_cache
->producing
) == 0) {
1699 struct lttng_metadata_stream
*stream
;
1701 list_for_each_entry(stream
, &session
->metadata_cache
->metadata_stream
, list
)
1702 wake_up_interruptible(&stream
->read_wait
);
1703 mutex_unlock(&session
->metadata_cache
->lock
);
1708 * Write the metadata to the metadata cache.
1709 * Must be called with sessions_mutex held.
1710 * The metadata cache lock protects us from concurrent read access from
1711 * thread outputting metadata content to ring buffer.
1712 * The content of the printf is printed as a single atomic metadata
1715 int lttng_metadata_printf(struct lttng_session
*session
,
1716 const char *fmt
, ...)
1722 WARN_ON_ONCE(!READ_ONCE(session
->active
));
1725 str
= kvasprintf(GFP_KERNEL
, fmt
, ap
);
1731 WARN_ON_ONCE(!atomic_read(&session
->metadata_cache
->producing
));
1732 if (session
->metadata_cache
->metadata_written
+ len
>
1733 session
->metadata_cache
->cache_alloc
) {
1734 char *tmp_cache_realloc
;
1735 unsigned int tmp_cache_alloc_size
;
1737 tmp_cache_alloc_size
= max_t(unsigned int,
1738 session
->metadata_cache
->cache_alloc
+ len
,
1739 session
->metadata_cache
->cache_alloc
<< 1);
1740 tmp_cache_realloc
= vzalloc(tmp_cache_alloc_size
);
1741 if (!tmp_cache_realloc
)
1743 if (session
->metadata_cache
->data
) {
1744 memcpy(tmp_cache_realloc
,
1745 session
->metadata_cache
->data
,
1746 session
->metadata_cache
->cache_alloc
);
1747 vfree(session
->metadata_cache
->data
);
1750 session
->metadata_cache
->cache_alloc
= tmp_cache_alloc_size
;
1751 session
->metadata_cache
->data
= tmp_cache_realloc
;
1753 memcpy(session
->metadata_cache
->data
+
1754 session
->metadata_cache
->metadata_written
,
1756 session
->metadata_cache
->metadata_written
+= len
;
1767 int print_tabs(struct lttng_session
*session
, size_t nesting
)
1771 for (i
= 0; i
< nesting
; i
++) {
1774 ret
= lttng_metadata_printf(session
, " ");
1783 * Must be called with sessions_mutex held.
1786 int _lttng_struct_type_statedump(struct lttng_session
*session
,
1787 const struct lttng_type
*type
,
1791 uint32_t i
, nr_fields
;
1793 ret
= print_tabs(session
, nesting
);
1796 ret
= lttng_metadata_printf(session
,
1800 nr_fields
= type
->u
._struct
.nr_fields
;
1801 for (i
= 0; i
< nr_fields
; i
++) {
1802 const struct lttng_event_field
*iter_field
;
1804 iter_field
= &type
->u
._struct
.fields
[i
];
1805 ret
= _lttng_field_statedump(session
, iter_field
, nesting
+ 1);
1809 ret
= print_tabs(session
, nesting
);
1812 ret
= lttng_metadata_printf(session
,
1818 * Must be called with sessions_mutex held.
1821 int _lttng_struct_statedump(struct lttng_session
*session
,
1822 const struct lttng_event_field
*field
,
1827 ret
= _lttng_struct_type_statedump(session
,
1828 &field
->type
, nesting
);
1831 ret
= lttng_metadata_printf(session
,
1838 * Must be called with sessions_mutex held.
1841 int _lttng_variant_type_statedump(struct lttng_session
*session
,
1842 const struct lttng_type
*type
,
1846 uint32_t i
, nr_choices
;
1848 ret
= print_tabs(session
, nesting
);
1851 ret
= lttng_metadata_printf(session
,
1852 "variant <_%s> {\n",
1853 type
->u
.variant
.tag_name
);
1856 nr_choices
= type
->u
.variant
.nr_choices
;
1857 for (i
= 0; i
< nr_choices
; i
++) {
1858 const struct lttng_event_field
*iter_field
;
1860 iter_field
= &type
->u
.variant
.choices
[i
];
1861 ret
= _lttng_field_statedump(session
, iter_field
, nesting
+ 1);
1865 ret
= print_tabs(session
, nesting
);
1868 ret
= lttng_metadata_printf(session
,
1874 * Must be called with sessions_mutex held.
1877 int _lttng_variant_statedump(struct lttng_session
*session
,
1878 const struct lttng_event_field
*field
,
1883 ret
= _lttng_variant_type_statedump(session
,
1884 &field
->type
, nesting
);
1887 ret
= lttng_metadata_printf(session
,
1894 * Must be called with sessions_mutex held.
1897 int _lttng_array_compound_statedump(struct lttng_session
*session
,
1898 const struct lttng_event_field
*field
,
1902 const struct lttng_type
*elem_type
;
1904 /* Only array of structures and variants are currently supported. */
1905 elem_type
= field
->type
.u
.array_compound
.elem_type
;
1906 switch (elem_type
->atype
) {
1908 ret
= _lttng_struct_type_statedump(session
, elem_type
, nesting
);
1913 ret
= _lttng_variant_type_statedump(session
, elem_type
, nesting
);
1920 ret
= lttng_metadata_printf(session
,
1923 field
->type
.u
.array_compound
.length
);
1928 * Must be called with sessions_mutex held.
1931 int _lttng_sequence_compound_statedump(struct lttng_session
*session
,
1932 const struct lttng_event_field
*field
,
1936 const char *length_name
;
1937 const struct lttng_type
*elem_type
;
1939 length_name
= field
->type
.u
.sequence_compound
.length_name
;
1941 /* Only array of structures and variants are currently supported. */
1942 elem_type
= field
->type
.u
.sequence_compound
.elem_type
;
1943 switch (elem_type
->atype
) {
1945 ret
= _lttng_struct_type_statedump(session
, elem_type
, nesting
);
1950 ret
= _lttng_variant_type_statedump(session
, elem_type
, nesting
);
1957 ret
= lttng_metadata_printf(session
,
1965 * Must be called with sessions_mutex held.
1968 int _lttng_enum_statedump(struct lttng_session
*session
,
1969 const struct lttng_event_field
*field
,
1972 const struct lttng_enum_desc
*enum_desc
;
1973 const struct lttng_integer_type
*container_type
;
1975 unsigned int i
, nr_entries
;
1977 enum_desc
= field
->type
.u
.basic
.enumeration
.desc
;
1978 container_type
= &field
->type
.u
.basic
.enumeration
.container_type
;
1979 nr_entries
= enum_desc
->nr_entries
;
1981 ret
= print_tabs(session
, nesting
);
1984 ret
= lttng_metadata_printf(session
,
1985 "enum : integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } {\n",
1986 container_type
->size
,
1987 container_type
->alignment
,
1988 container_type
->signedness
,
1989 (container_type
->encoding
== lttng_encode_none
)
1991 : (container_type
->encoding
== lttng_encode_UTF8
)
1994 container_type
->base
,
1995 #if __BYTE_ORDER == __BIG_ENDIAN
1996 container_type
->reverse_byte_order
? " byte_order = le;" : ""
1998 container_type
->reverse_byte_order
? " byte_order = be;" : ""
2003 /* Dump all entries */
2004 for (i
= 0; i
< nr_entries
; i
++) {
2005 const struct lttng_enum_entry
*entry
= &enum_desc
->entries
[i
];
2008 ret
= print_tabs(session
, nesting
+ 1);
2011 ret
= lttng_metadata_printf(session
,
2015 len
= strlen(entry
->string
);
2016 /* Escape the character '"' */
2017 for (j
= 0; j
< len
; j
++) {
2018 char c
= entry
->string
[j
];
2022 ret
= lttng_metadata_printf(session
,
2026 ret
= lttng_metadata_printf(session
,
2030 ret
= lttng_metadata_printf(session
,
2037 ret
= lttng_metadata_printf(session
, "\"");
2041 if (entry
->options
.is_auto
) {
2042 ret
= lttng_metadata_printf(session
, ",\n");
2046 ret
= lttng_metadata_printf(session
,
2050 if (entry
->start
.signedness
)
2051 ret
= lttng_metadata_printf(session
,
2052 "%lld", (long long) entry
->start
.value
);
2054 ret
= lttng_metadata_printf(session
,
2055 "%llu", entry
->start
.value
);
2058 if (entry
->start
.signedness
== entry
->end
.signedness
&&
2060 == entry
->end
.value
) {
2061 ret
= lttng_metadata_printf(session
,
2064 if (entry
->end
.signedness
) {
2065 ret
= lttng_metadata_printf(session
,
2067 (long long) entry
->end
.value
);
2069 ret
= lttng_metadata_printf(session
,
2078 ret
= print_tabs(session
, nesting
);
2081 ret
= lttng_metadata_printf(session
, "} _%s;\n",
2088 * Must be called with sessions_mutex held.
2091 int _lttng_field_statedump(struct lttng_session
*session
,
2092 const struct lttng_event_field
*field
,
2097 switch (field
->type
.atype
) {
2099 ret
= print_tabs(session
, nesting
);
2102 ret
= lttng_metadata_printf(session
,
2103 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n",
2104 field
->type
.u
.basic
.integer
.size
,
2105 field
->type
.u
.basic
.integer
.alignment
,
2106 field
->type
.u
.basic
.integer
.signedness
,
2107 (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_none
)
2109 : (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
2112 field
->type
.u
.basic
.integer
.base
,
2113 #if __BYTE_ORDER == __BIG_ENDIAN
2114 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
2116 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
2121 ret
= _lttng_enum_statedump(session
, field
, nesting
);
2124 case atype_array_bitfield
:
2126 const struct lttng_basic_type
*elem_type
;
2128 elem_type
= &field
->type
.u
.array
.elem_type
;
2129 if (field
->type
.u
.array
.elem_alignment
) {
2130 ret
= print_tabs(session
, nesting
);
2133 ret
= lttng_metadata_printf(session
,
2134 "struct { } align(%u) _%s_padding;\n",
2135 field
->type
.u
.array
.elem_alignment
* CHAR_BIT
,
2140 ret
= print_tabs(session
, nesting
);
2143 ret
= lttng_metadata_printf(session
,
2144 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n",
2145 elem_type
->u
.basic
.integer
.size
,
2146 elem_type
->u
.basic
.integer
.alignment
,
2147 elem_type
->u
.basic
.integer
.signedness
,
2148 (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
2150 : (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
2153 elem_type
->u
.basic
.integer
.base
,
2154 #if __BYTE_ORDER == __BIG_ENDIAN
2155 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
2157 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
2159 field
->name
, field
->type
.u
.array
.length
);
2162 case atype_sequence
:
2163 case atype_sequence_bitfield
:
2165 const struct lttng_basic_type
*elem_type
;
2166 const struct lttng_basic_type
*length_type
;
2168 elem_type
= &field
->type
.u
.sequence
.elem_type
;
2169 length_type
= &field
->type
.u
.sequence
.length_type
;
2170 ret
= print_tabs(session
, nesting
);
2173 ret
= lttng_metadata_printf(session
,
2174 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n",
2175 length_type
->u
.basic
.integer
.size
,
2176 (unsigned int) length_type
->u
.basic
.integer
.alignment
,
2177 length_type
->u
.basic
.integer
.signedness
,
2178 (length_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
2180 : ((length_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
2183 length_type
->u
.basic
.integer
.base
,
2184 #if __BYTE_ORDER == __BIG_ENDIAN
2185 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
2187 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
2193 if (field
->type
.u
.sequence
.elem_alignment
) {
2194 ret
= print_tabs(session
, nesting
);
2197 ret
= lttng_metadata_printf(session
,
2198 "struct { } align(%u) _%s_padding;\n",
2199 field
->type
.u
.sequence
.elem_alignment
* CHAR_BIT
,
2204 ret
= print_tabs(session
, nesting
);
2207 ret
= lttng_metadata_printf(session
,
2208 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n",
2209 elem_type
->u
.basic
.integer
.size
,
2210 (unsigned int) elem_type
->u
.basic
.integer
.alignment
,
2211 elem_type
->u
.basic
.integer
.signedness
,
2212 (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
2214 : ((elem_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
2217 elem_type
->u
.basic
.integer
.base
,
2218 #if __BYTE_ORDER == __BIG_ENDIAN
2219 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
2221 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
2229 /* Default encoding is UTF8 */
2230 ret
= print_tabs(session
, nesting
);
2233 ret
= lttng_metadata_printf(session
,
2235 field
->type
.u
.basic
.string
.encoding
== lttng_encode_ASCII
?
2236 " { encoding = ASCII; }" : "",
2240 ret
= _lttng_struct_statedump(session
, field
, nesting
);
2242 case atype_array_compound
:
2243 ret
= _lttng_array_compound_statedump(session
, field
, nesting
);
2245 case atype_sequence_compound
:
2246 ret
= _lttng_sequence_compound_statedump(session
, field
, nesting
);
2249 ret
= _lttng_variant_statedump(session
, field
, nesting
);
2260 int _lttng_context_metadata_statedump(struct lttng_session
*session
,
2261 struct lttng_ctx
*ctx
)
2268 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
2269 const struct lttng_ctx_field
*field
= &ctx
->fields
[i
];
2271 ret
= _lttng_field_statedump(session
, &field
->event_field
, 2);
2279 int _lttng_fields_metadata_statedump(struct lttng_session
*session
,
2280 struct lttng_event
*event
)
2282 const struct lttng_event_desc
*desc
= event
->desc
;
2286 for (i
= 0; i
< desc
->nr_fields
; i
++) {
2287 const struct lttng_event_field
*field
= &desc
->fields
[i
];
2289 ret
= _lttng_field_statedump(session
, field
, 2);
2297 * Must be called with sessions_mutex held.
2298 * The entire event metadata is printed as a single atomic metadata
2302 int _lttng_event_metadata_statedump(struct lttng_session
*session
,
2303 struct lttng_channel
*chan
,
2304 struct lttng_event
*event
)
2308 if (event
->metadata_dumped
|| !READ_ONCE(session
->active
))
2310 if (chan
->channel_type
== METADATA_CHANNEL
)
2313 lttng_metadata_begin(session
);
2315 ret
= lttng_metadata_printf(session
,
2319 " stream_id = %u;\n",
2327 ret
= lttng_metadata_printf(session
,
2328 " context := struct {\n");
2332 ret
= _lttng_context_metadata_statedump(session
, event
->ctx
);
2336 ret
= lttng_metadata_printf(session
,
2342 ret
= lttng_metadata_printf(session
,
2343 " fields := struct {\n"
2348 ret
= _lttng_fields_metadata_statedump(session
, event
);
2353 * LTTng space reservation can only reserve multiples of the
2356 ret
= lttng_metadata_printf(session
,
2362 event
->metadata_dumped
= 1;
2364 lttng_metadata_end(session
);
2370 * Must be called with sessions_mutex held.
2371 * The entire channel metadata is printed as a single atomic metadata
2375 int _lttng_channel_metadata_statedump(struct lttng_session
*session
,
2376 struct lttng_channel
*chan
)
2380 if (chan
->metadata_dumped
|| !READ_ONCE(session
->active
))
2383 if (chan
->channel_type
== METADATA_CHANNEL
)
2386 lttng_metadata_begin(session
);
2388 WARN_ON_ONCE(!chan
->header_type
);
2389 ret
= lttng_metadata_printf(session
,
2392 " event.header := %s;\n"
2393 " packet.context := struct packet_context;\n",
2395 chan
->header_type
== 1 ? "struct event_header_compact" :
2396 "struct event_header_large");
2401 ret
= lttng_metadata_printf(session
,
2402 " event.context := struct {\n");
2406 ret
= _lttng_context_metadata_statedump(session
, chan
->ctx
);
2410 ret
= lttng_metadata_printf(session
,
2416 ret
= lttng_metadata_printf(session
,
2419 chan
->metadata_dumped
= 1;
2421 lttng_metadata_end(session
);
2426 * Must be called with sessions_mutex held.
2429 int _lttng_stream_packet_context_declare(struct lttng_session
*session
)
2431 return lttng_metadata_printf(session
,
2432 "struct packet_context {\n"
2433 " uint64_clock_monotonic_t timestamp_begin;\n"
2434 " uint64_clock_monotonic_t timestamp_end;\n"
2435 " uint64_t content_size;\n"
2436 " uint64_t packet_size;\n"
2437 " uint64_t packet_seq_num;\n"
2438 " unsigned long events_discarded;\n"
2439 " uint32_t cpu_id;\n"
2446 * id: range: 0 - 30.
2447 * id 31 is reserved to indicate an extended header.
2450 * id: range: 0 - 65534.
2451 * id 65535 is reserved to indicate an extended header.
2453 * Must be called with sessions_mutex held.
2456 int _lttng_event_header_declare(struct lttng_session
*session
)
2458 return lttng_metadata_printf(session
,
2459 "struct event_header_compact {\n"
2460 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
2463 " uint27_clock_monotonic_t timestamp;\n"
2467 " uint64_clock_monotonic_t timestamp;\n"
2472 "struct event_header_large {\n"
2473 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
2476 " uint32_clock_monotonic_t timestamp;\n"
2480 " uint64_clock_monotonic_t timestamp;\n"
2484 lttng_alignof(uint32_t) * CHAR_BIT
,
2485 lttng_alignof(uint16_t) * CHAR_BIT
2490 * Approximation of NTP time of day to clock monotonic correlation,
2491 * taken at start of trace.
2492 * Yes, this is only an approximation. Yes, we can (and will) do better
2493 * in future versions.
2494 * This function may return a negative offset. It may happen if the
2495 * system sets the REALTIME clock to 0 after boot.
2497 * Use 64bit timespec on kernels that have it, this makes 32bit arch
2501 int64_t measure_clock_offset(void)
2503 uint64_t monotonic_avg
, monotonic
[2], realtime
;
2504 uint64_t tcf
= trace_clock_freq();
2506 unsigned long flags
;
2507 #ifdef LTTNG_KERNEL_HAS_TIMESPEC64
2508 struct timespec64 rts
= { 0, 0 };
2510 struct timespec rts
= { 0, 0 };
2513 /* Disable interrupts to increase correlation precision. */
2514 local_irq_save(flags
);
2515 monotonic
[0] = trace_clock_read64();
2516 #ifdef LTTNG_KERNEL_HAS_TIMESPEC64
2517 ktime_get_real_ts64(&rts
);
2519 getnstimeofday(&rts
);
2521 monotonic
[1] = trace_clock_read64();
2522 local_irq_restore(flags
);
2524 monotonic_avg
= (monotonic
[0] + monotonic
[1]) >> 1;
2525 realtime
= (uint64_t) rts
.tv_sec
* tcf
;
2526 if (tcf
== NSEC_PER_SEC
) {
2527 realtime
+= rts
.tv_nsec
;
2529 uint64_t n
= rts
.tv_nsec
* tcf
;
2531 do_div(n
, NSEC_PER_SEC
);
2534 offset
= (int64_t) realtime
- monotonic_avg
;
2539 int print_escaped_ctf_string(struct lttng_session
*session
, const char *string
)
2547 while (cur
!= '\0') {
2550 ret
= lttng_metadata_printf(session
, "%s", "\\n");
2554 ret
= lttng_metadata_printf(session
, "%c", '\\');
2557 /* We still print the current char */
2560 ret
= lttng_metadata_printf(session
, "%c", cur
);
2574 int print_metadata_escaped_field(struct lttng_session
*session
, const char *field
,
2575 const char *field_value
)
2579 ret
= lttng_metadata_printf(session
, " %s = \"", field
);
2583 ret
= print_escaped_ctf_string(session
, field_value
);
2587 ret
= lttng_metadata_printf(session
, "\";\n");
2594 * Output metadata into this session's metadata buffers.
2595 * Must be called with sessions_mutex held.
2598 int _lttng_session_metadata_statedump(struct lttng_session
*session
)
2600 unsigned char *uuid_c
= session
->uuid
.b
;
2601 unsigned char uuid_s
[37], clock_uuid_s
[BOOT_ID_LEN
];
2602 const char *product_uuid
;
2603 struct lttng_channel
*chan
;
2604 struct lttng_event
*event
;
2607 if (!READ_ONCE(session
->active
))
2610 lttng_metadata_begin(session
);
2612 if (session
->metadata_dumped
)
2615 snprintf(uuid_s
, sizeof(uuid_s
),
2616 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
2617 uuid_c
[0], uuid_c
[1], uuid_c
[2], uuid_c
[3],
2618 uuid_c
[4], uuid_c
[5], uuid_c
[6], uuid_c
[7],
2619 uuid_c
[8], uuid_c
[9], uuid_c
[10], uuid_c
[11],
2620 uuid_c
[12], uuid_c
[13], uuid_c
[14], uuid_c
[15]);
2622 ret
= lttng_metadata_printf(session
,
2623 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
2624 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
2625 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
2626 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
2627 "typealias integer { size = %u; align = %u; signed = false; } := unsigned long;\n"
2628 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
2629 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
2635 " byte_order = %s;\n"
2636 " packet.header := struct {\n"
2637 " uint32_t magic;\n"
2638 " uint8_t uuid[16];\n"
2639 " uint32_t stream_id;\n"
2640 " uint64_t stream_instance_id;\n"
2643 lttng_alignof(uint8_t) * CHAR_BIT
,
2644 lttng_alignof(uint16_t) * CHAR_BIT
,
2645 lttng_alignof(uint32_t) * CHAR_BIT
,
2646 lttng_alignof(uint64_t) * CHAR_BIT
,
2647 sizeof(unsigned long) * CHAR_BIT
,
2648 lttng_alignof(unsigned long) * CHAR_BIT
,
2652 #if __BYTE_ORDER == __BIG_ENDIAN
2661 ret
= lttng_metadata_printf(session
,
2663 " hostname = \"%s\";\n"
2664 " domain = \"kernel\";\n"
2665 " sysname = \"%s\";\n"
2666 " kernel_release = \"%s\";\n"
2667 " kernel_version = \"%s\";\n"
2668 " tracer_name = \"lttng-modules\";\n"
2669 " tracer_major = %d;\n"
2670 " tracer_minor = %d;\n"
2671 " tracer_patchlevel = %d;\n"
2672 " trace_buffering_scheme = \"global\";\n",
2673 current
->nsproxy
->uts_ns
->name
.nodename
,
2677 LTTNG_MODULES_MAJOR_VERSION
,
2678 LTTNG_MODULES_MINOR_VERSION
,
2679 LTTNG_MODULES_PATCHLEVEL_VERSION
2684 ret
= print_metadata_escaped_field(session
, "trace_name", session
->name
);
2687 ret
= print_metadata_escaped_field(session
, "trace_creation_datetime",
2688 session
->creation_time
);
2692 /* Add the product UUID to the 'env' section */
2693 product_uuid
= dmi_get_system_info(DMI_PRODUCT_UUID
);
2695 ret
= lttng_metadata_printf(session
,
2696 " product_uuid = \"%s\";\n",
2703 /* Close the 'env' section */
2704 ret
= lttng_metadata_printf(session
, "};\n\n");
2708 ret
= lttng_metadata_printf(session
,
2710 " name = \"%s\";\n",
2716 if (!trace_clock_uuid(clock_uuid_s
)) {
2717 ret
= lttng_metadata_printf(session
,
2718 " uuid = \"%s\";\n",
2725 ret
= lttng_metadata_printf(session
,
2726 " description = \"%s\";\n"
2727 " freq = %llu; /* Frequency, in Hz */\n"
2728 " /* clock value offset from Epoch is: offset * (1/freq) */\n"
2731 trace_clock_description(),
2732 (unsigned long long) trace_clock_freq(),
2733 (long long) measure_clock_offset()
2738 ret
= lttng_metadata_printf(session
,
2739 "typealias integer {\n"
2740 " size = 27; align = 1; signed = false;\n"
2741 " map = clock.%s.value;\n"
2742 "} := uint27_clock_monotonic_t;\n"
2744 "typealias integer {\n"
2745 " size = 32; align = %u; signed = false;\n"
2746 " map = clock.%s.value;\n"
2747 "} := uint32_clock_monotonic_t;\n"
2749 "typealias integer {\n"
2750 " size = 64; align = %u; signed = false;\n"
2751 " map = clock.%s.value;\n"
2752 "} := uint64_clock_monotonic_t;\n\n",
2754 lttng_alignof(uint32_t) * CHAR_BIT
,
2756 lttng_alignof(uint64_t) * CHAR_BIT
,
2762 ret
= _lttng_stream_packet_context_declare(session
);
2766 ret
= _lttng_event_header_declare(session
);
2771 list_for_each_entry(chan
, &session
->chan
, list
) {
2772 ret
= _lttng_channel_metadata_statedump(session
, chan
);
2777 list_for_each_entry(event
, &session
->events
, list
) {
2778 ret
= _lttng_event_metadata_statedump(session
, event
->chan
, event
);
2782 session
->metadata_dumped
= 1;
2784 lttng_metadata_end(session
);
2789 * lttng_transport_register - LTT transport registration
2790 * @transport: transport structure
2792 * Registers a transport which can be used as output to extract the data out of
2793 * LTTng. The module calling this registration function must ensure that no
2794 * trap-inducing code will be executed by the transport functions. E.g.
2795 * vmalloc_sync_mappings() must be called between a vmalloc and the moment the memory
2796 * is made visible to the transport function. This registration acts as a
2797 * vmalloc_sync_mappings. Therefore, only if the module allocates virtual memory
2798 * after its registration must it synchronize the TLBs.
2800 void lttng_transport_register(struct lttng_transport
*transport
)
2803 * Make sure no page fault can be triggered by the module about to be
2804 * registered. We deal with this here so we don't have to call
2805 * vmalloc_sync_mappings() in each module's init.
2807 wrapper_vmalloc_sync_mappings();
2809 mutex_lock(&sessions_mutex
);
2810 list_add_tail(&transport
->node
, <tng_transport_list
);
2811 mutex_unlock(&sessions_mutex
);
2813 EXPORT_SYMBOL_GPL(lttng_transport_register
);
2816 * lttng_transport_unregister - LTT transport unregistration
2817 * @transport: transport structure
2819 void lttng_transport_unregister(struct lttng_transport
*transport
)
2821 mutex_lock(&sessions_mutex
);
2822 list_del(&transport
->node
);
2823 mutex_unlock(&sessions_mutex
);
2825 EXPORT_SYMBOL_GPL(lttng_transport_unregister
);
2827 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0))
2829 enum cpuhp_state lttng_hp_prepare
;
2830 enum cpuhp_state lttng_hp_online
;
2832 static int lttng_hotplug_prepare(unsigned int cpu
, struct hlist_node
*node
)
2834 struct lttng_cpuhp_node
*lttng_node
;
2836 lttng_node
= container_of(node
, struct lttng_cpuhp_node
, node
);
2837 switch (lttng_node
->component
) {
2838 case LTTNG_RING_BUFFER_FRONTEND
:
2840 case LTTNG_RING_BUFFER_BACKEND
:
2841 return lttng_cpuhp_rb_backend_prepare(cpu
, lttng_node
);
2842 case LTTNG_RING_BUFFER_ITER
:
2844 case LTTNG_CONTEXT_PERF_COUNTERS
:
2851 static int lttng_hotplug_dead(unsigned int cpu
, struct hlist_node
*node
)
2853 struct lttng_cpuhp_node
*lttng_node
;
2855 lttng_node
= container_of(node
, struct lttng_cpuhp_node
, node
);
2856 switch (lttng_node
->component
) {
2857 case LTTNG_RING_BUFFER_FRONTEND
:
2858 return lttng_cpuhp_rb_frontend_dead(cpu
, lttng_node
);
2859 case LTTNG_RING_BUFFER_BACKEND
:
2861 case LTTNG_RING_BUFFER_ITER
:
2863 case LTTNG_CONTEXT_PERF_COUNTERS
:
2864 return lttng_cpuhp_perf_counter_dead(cpu
, lttng_node
);
2870 static int lttng_hotplug_online(unsigned int cpu
, struct hlist_node
*node
)
2872 struct lttng_cpuhp_node
*lttng_node
;
2874 lttng_node
= container_of(node
, struct lttng_cpuhp_node
, node
);
2875 switch (lttng_node
->component
) {
2876 case LTTNG_RING_BUFFER_FRONTEND
:
2877 return lttng_cpuhp_rb_frontend_online(cpu
, lttng_node
);
2878 case LTTNG_RING_BUFFER_BACKEND
:
2880 case LTTNG_RING_BUFFER_ITER
:
2881 return lttng_cpuhp_rb_iter_online(cpu
, lttng_node
);
2882 case LTTNG_CONTEXT_PERF_COUNTERS
:
2883 return lttng_cpuhp_perf_counter_online(cpu
, lttng_node
);
2889 static int lttng_hotplug_offline(unsigned int cpu
, struct hlist_node
*node
)
2891 struct lttng_cpuhp_node
*lttng_node
;
2893 lttng_node
= container_of(node
, struct lttng_cpuhp_node
, node
);
2894 switch (lttng_node
->component
) {
2895 case LTTNG_RING_BUFFER_FRONTEND
:
2896 return lttng_cpuhp_rb_frontend_offline(cpu
, lttng_node
);
2897 case LTTNG_RING_BUFFER_BACKEND
:
2899 case LTTNG_RING_BUFFER_ITER
:
2901 case LTTNG_CONTEXT_PERF_COUNTERS
:
2908 static int __init
lttng_init_cpu_hotplug(void)
2912 ret
= cpuhp_setup_state_multi(CPUHP_BP_PREPARE_DYN
, "lttng:prepare",
2913 lttng_hotplug_prepare
,
2914 lttng_hotplug_dead
);
2918 lttng_hp_prepare
= ret
;
2919 lttng_rb_set_hp_prepare(ret
);
2921 ret
= cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN
, "lttng:online",
2922 lttng_hotplug_online
,
2923 lttng_hotplug_offline
);
2925 cpuhp_remove_multi_state(lttng_hp_prepare
);
2926 lttng_hp_prepare
= 0;
2929 lttng_hp_online
= ret
;
2930 lttng_rb_set_hp_online(ret
);
2935 static void __exit
lttng_exit_cpu_hotplug(void)
2937 lttng_rb_set_hp_online(0);
2938 cpuhp_remove_multi_state(lttng_hp_online
);
2939 lttng_rb_set_hp_prepare(0);
2940 cpuhp_remove_multi_state(lttng_hp_prepare
);
2943 #else /* #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
2944 static int lttng_init_cpu_hotplug(void)
2948 static void lttng_exit_cpu_hotplug(void)
2951 #endif /* #else #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
2954 static int __init
lttng_events_init(void)
2958 ret
= wrapper_lttng_fixup_sig(THIS_MODULE
);
2961 ret
= wrapper_get_pfnblock_flags_mask_init();
2964 ret
= wrapper_get_pageblock_flags_mask_init();
2967 ret
= lttng_probes_init();
2970 ret
= lttng_context_init();
2973 ret
= lttng_tracepoint_init();
2976 event_cache
= KMEM_CACHE(lttng_event
, 0);
2981 ret
= lttng_abi_init();
2984 ret
= lttng_logger_init();
2987 ret
= lttng_init_cpu_hotplug();
2990 printk(KERN_NOTICE
"LTTng: Loaded modules v%s.%s.%s%s (%s)%s%s\n",
2991 __stringify(LTTNG_MODULES_MAJOR_VERSION
),
2992 __stringify(LTTNG_MODULES_MINOR_VERSION
),
2993 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION
),
2994 LTTNG_MODULES_EXTRAVERSION
,
2996 #ifdef LTTNG_EXTRA_VERSION_GIT
2997 LTTNG_EXTRA_VERSION_GIT
[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_GIT
,
3001 #ifdef LTTNG_EXTRA_VERSION_NAME
3002 LTTNG_EXTRA_VERSION_NAME
[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_NAME
);
3009 lttng_logger_exit();
3013 kmem_cache_destroy(event_cache
);
3015 lttng_tracepoint_exit();
3017 lttng_context_exit();
3018 printk(KERN_NOTICE
"LTTng: Failed to load modules v%s.%s.%s%s (%s)%s%s\n",
3019 __stringify(LTTNG_MODULES_MAJOR_VERSION
),
3020 __stringify(LTTNG_MODULES_MINOR_VERSION
),
3021 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION
),
3022 LTTNG_MODULES_EXTRAVERSION
,
3024 #ifdef LTTNG_EXTRA_VERSION_GIT
3025 LTTNG_EXTRA_VERSION_GIT
[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_GIT
,
3029 #ifdef LTTNG_EXTRA_VERSION_NAME
3030 LTTNG_EXTRA_VERSION_NAME
[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_NAME
);
3037 module_init(lttng_events_init
);
3039 static void __exit
lttng_events_exit(void)
3041 struct lttng_session
*session
, *tmpsession
;
3043 lttng_exit_cpu_hotplug();
3044 lttng_logger_exit();
3046 list_for_each_entry_safe(session
, tmpsession
, &sessions
, list
)
3047 lttng_session_destroy(session
);
3048 kmem_cache_destroy(event_cache
);
3049 lttng_tracepoint_exit();
3050 lttng_context_exit();
3051 printk(KERN_NOTICE
"LTTng: Unloaded modules v%s.%s.%s%s (%s)%s%s\n",
3052 __stringify(LTTNG_MODULES_MAJOR_VERSION
),
3053 __stringify(LTTNG_MODULES_MINOR_VERSION
),
3054 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION
),
3055 LTTNG_MODULES_EXTRAVERSION
,
3057 #ifdef LTTNG_EXTRA_VERSION_GIT
3058 LTTNG_EXTRA_VERSION_GIT
[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_GIT
,
3062 #ifdef LTTNG_EXTRA_VERSION_NAME
3063 LTTNG_EXTRA_VERSION_NAME
[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_NAME
);
3069 module_exit(lttng_events_exit
);
3071 #include "extra_version/patches.i"
3072 #ifdef LTTNG_EXTRA_VERSION_GIT
3073 MODULE_INFO(extra_version_git
, LTTNG_EXTRA_VERSION_GIT
);
3075 #ifdef LTTNG_EXTRA_VERSION_NAME
3076 MODULE_INFO(extra_version_name
, LTTNG_EXTRA_VERSION_NAME
);
3078 MODULE_LICENSE("GPL and additional rights");
3079 MODULE_AUTHOR("Mathieu Desnoyers <mathieu.desnoyers@efficios.com>");
3080 MODULE_DESCRIPTION("LTTng tracer");
3081 MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION
) "."
3082 __stringify(LTTNG_MODULES_MINOR_VERSION
) "."
3083 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION
)
3084 LTTNG_MODULES_EXTRAVERSION
);