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 list_for_each_entry(metadata_stream
, &session
->metadata_cache
->metadata_stream
, list
)
214 _lttng_metadata_channel_hangup(metadata_stream
);
215 lttng_id_tracker_destroy(&session
->pid_tracker
, false);
216 lttng_id_tracker_destroy(&session
->vpid_tracker
, false);
217 lttng_id_tracker_destroy(&session
->uid_tracker
, false);
218 lttng_id_tracker_destroy(&session
->vuid_tracker
, false);
219 lttng_id_tracker_destroy(&session
->gid_tracker
, false);
220 lttng_id_tracker_destroy(&session
->vgid_tracker
, false);
221 kref_put(&session
->metadata_cache
->refcount
, metadata_cache_destroy
);
222 list_del(&session
->list
);
223 mutex_unlock(&sessions_mutex
);
224 lttng_kvfree(session
);
227 int lttng_session_statedump(struct lttng_session
*session
)
231 mutex_lock(&sessions_mutex
);
232 ret
= lttng_statedump_start(session
);
233 mutex_unlock(&sessions_mutex
);
237 int lttng_session_enable(struct lttng_session
*session
)
240 struct lttng_channel
*chan
;
242 mutex_lock(&sessions_mutex
);
243 if (session
->active
) {
248 /* Set transient enabler state to "enabled" */
251 /* We need to sync enablers with session before activation. */
252 lttng_session_sync_enablers(session
);
255 * Snapshot the number of events per channel to know the type of header
258 list_for_each_entry(chan
, &session
->chan
, list
) {
259 if (chan
->header_type
)
260 continue; /* don't change it if session stop/restart */
261 if (chan
->free_event_id
< 31)
262 chan
->header_type
= 1; /* compact */
264 chan
->header_type
= 2; /* large */
267 /* Clear each stream's quiescent state. */
268 list_for_each_entry(chan
, &session
->chan
, list
) {
269 if (chan
->channel_type
!= METADATA_CHANNEL
)
270 lib_ring_buffer_clear_quiescent_channel(chan
->chan
);
273 WRITE_ONCE(session
->active
, 1);
274 WRITE_ONCE(session
->been_active
, 1);
275 ret
= _lttng_session_metadata_statedump(session
);
277 WRITE_ONCE(session
->active
, 0);
280 ret
= lttng_statedump_start(session
);
282 WRITE_ONCE(session
->active
, 0);
284 mutex_unlock(&sessions_mutex
);
288 int lttng_session_disable(struct lttng_session
*session
)
291 struct lttng_channel
*chan
;
293 mutex_lock(&sessions_mutex
);
294 if (!session
->active
) {
298 WRITE_ONCE(session
->active
, 0);
300 /* Set transient enabler state to "disabled" */
302 lttng_session_sync_enablers(session
);
304 /* Set each stream's quiescent state. */
305 list_for_each_entry(chan
, &session
->chan
, list
) {
306 if (chan
->channel_type
!= METADATA_CHANNEL
)
307 lib_ring_buffer_set_quiescent_channel(chan
->chan
);
310 mutex_unlock(&sessions_mutex
);
314 int lttng_session_metadata_regenerate(struct lttng_session
*session
)
317 struct lttng_channel
*chan
;
318 struct lttng_event
*event
;
319 struct lttng_metadata_cache
*cache
= session
->metadata_cache
;
320 struct lttng_metadata_stream
*stream
;
322 mutex_lock(&sessions_mutex
);
323 if (!session
->active
) {
328 mutex_lock(&cache
->lock
);
329 memset(cache
->data
, 0, cache
->cache_alloc
);
330 cache
->metadata_written
= 0;
332 list_for_each_entry(stream
, &session
->metadata_cache
->metadata_stream
, list
) {
333 stream
->metadata_out
= 0;
334 stream
->metadata_in
= 0;
336 mutex_unlock(&cache
->lock
);
338 session
->metadata_dumped
= 0;
339 list_for_each_entry(chan
, &session
->chan
, list
) {
340 chan
->metadata_dumped
= 0;
343 list_for_each_entry(event
, &session
->events
, list
) {
344 event
->metadata_dumped
= 0;
347 ret
= _lttng_session_metadata_statedump(session
);
350 mutex_unlock(&sessions_mutex
);
354 int lttng_channel_enable(struct lttng_channel
*channel
)
358 mutex_lock(&sessions_mutex
);
359 if (channel
->channel_type
== METADATA_CHANNEL
) {
363 if (channel
->enabled
) {
367 /* Set transient enabler state to "enabled" */
369 lttng_session_sync_enablers(channel
->session
);
370 /* Set atomically the state to "enabled" */
371 WRITE_ONCE(channel
->enabled
, 1);
373 mutex_unlock(&sessions_mutex
);
377 int lttng_channel_disable(struct lttng_channel
*channel
)
381 mutex_lock(&sessions_mutex
);
382 if (channel
->channel_type
== METADATA_CHANNEL
) {
386 if (!channel
->enabled
) {
390 /* Set atomically the state to "disabled" */
391 WRITE_ONCE(channel
->enabled
, 0);
392 /* Set transient enabler state to "enabled" */
394 lttng_session_sync_enablers(channel
->session
);
396 mutex_unlock(&sessions_mutex
);
400 int lttng_event_enable(struct lttng_event
*event
)
404 mutex_lock(&sessions_mutex
);
405 if (event
->chan
->channel_type
== METADATA_CHANNEL
) {
409 if (event
->enabled
) {
413 switch (event
->instrumentation
) {
414 case LTTNG_KERNEL_TRACEPOINT
:
415 case LTTNG_KERNEL_SYSCALL
:
418 case LTTNG_KERNEL_KPROBE
:
419 case LTTNG_KERNEL_UPROBE
:
420 case LTTNG_KERNEL_NOOP
:
421 WRITE_ONCE(event
->enabled
, 1);
423 case LTTNG_KERNEL_KRETPROBE
:
424 ret
= lttng_kretprobes_event_enable_state(event
, 1);
426 case LTTNG_KERNEL_FUNCTION
: /* Fall-through. */
432 mutex_unlock(&sessions_mutex
);
436 int lttng_event_disable(struct lttng_event
*event
)
440 mutex_lock(&sessions_mutex
);
441 if (event
->chan
->channel_type
== METADATA_CHANNEL
) {
445 if (!event
->enabled
) {
449 switch (event
->instrumentation
) {
450 case LTTNG_KERNEL_TRACEPOINT
:
451 case LTTNG_KERNEL_SYSCALL
:
454 case LTTNG_KERNEL_KPROBE
:
455 case LTTNG_KERNEL_UPROBE
:
456 case LTTNG_KERNEL_NOOP
:
457 WRITE_ONCE(event
->enabled
, 0);
459 case LTTNG_KERNEL_KRETPROBE
:
460 ret
= lttng_kretprobes_event_enable_state(event
, 0);
462 case LTTNG_KERNEL_FUNCTION
: /* Fall-through. */
468 mutex_unlock(&sessions_mutex
);
472 static struct lttng_transport
*lttng_transport_find(const char *name
)
474 struct lttng_transport
*transport
;
476 list_for_each_entry(transport
, <tng_transport_list
, node
) {
477 if (!strcmp(transport
->name
, name
))
483 struct lttng_channel
*lttng_channel_create(struct lttng_session
*session
,
484 const char *transport_name
,
486 size_t subbuf_size
, size_t num_subbuf
,
487 unsigned int switch_timer_interval
,
488 unsigned int read_timer_interval
,
489 enum channel_type channel_type
)
491 struct lttng_channel
*chan
;
492 struct lttng_transport
*transport
= NULL
;
494 mutex_lock(&sessions_mutex
);
495 if (session
->been_active
&& channel_type
!= METADATA_CHANNEL
)
496 goto active
; /* Refuse to add channel to active session */
497 transport
= lttng_transport_find(transport_name
);
499 printk(KERN_WARNING
"LTTng transport %s not found\n",
503 if (!try_module_get(transport
->owner
)) {
504 printk(KERN_WARNING
"LTT : Can't lock transport module.\n");
507 chan
= kzalloc(sizeof(struct lttng_channel
), GFP_KERNEL
);
510 chan
->session
= session
;
511 chan
->id
= session
->free_chan_id
++;
512 chan
->ops
= &transport
->ops
;
514 * Note: the channel creation op already writes into the packet
515 * headers. Therefore the "chan" information used as input
516 * should be already accessible.
518 chan
->chan
= transport
->ops
.channel_create(transport_name
,
519 chan
, buf_addr
, subbuf_size
, num_subbuf
,
520 switch_timer_interval
, read_timer_interval
);
525 chan
->transport
= transport
;
526 chan
->channel_type
= channel_type
;
527 list_add(&chan
->list
, &session
->chan
);
528 mutex_unlock(&sessions_mutex
);
535 module_put(transport
->owner
);
538 mutex_unlock(&sessions_mutex
);
543 * Only used internally at session destruction for per-cpu channels, and
544 * when metadata channel is released.
545 * Needs to be called with sessions mutex held.
548 void _lttng_channel_destroy(struct lttng_channel
*chan
)
550 chan
->ops
->channel_destroy(chan
->chan
);
551 module_put(chan
->transport
->owner
);
552 list_del(&chan
->list
);
553 lttng_destroy_context(chan
->ctx
);
557 void lttng_metadata_channel_destroy(struct lttng_channel
*chan
)
559 BUG_ON(chan
->channel_type
!= METADATA_CHANNEL
);
561 /* Protect the metadata cache with the sessions_mutex. */
562 mutex_lock(&sessions_mutex
);
563 _lttng_channel_destroy(chan
);
564 mutex_unlock(&sessions_mutex
);
566 EXPORT_SYMBOL_GPL(lttng_metadata_channel_destroy
);
569 void _lttng_metadata_channel_hangup(struct lttng_metadata_stream
*stream
)
571 stream
->finalized
= 1;
572 wake_up_interruptible(&stream
->read_wait
);
576 * Supports event creation while tracing session is active.
577 * Needs to be called with sessions mutex held.
579 struct lttng_event
*_lttng_event_create(struct lttng_channel
*chan
,
580 struct lttng_kernel_event
*event_param
,
582 const struct lttng_event_desc
*event_desc
,
583 enum lttng_kernel_instrumentation itype
)
585 struct lttng_session
*session
= chan
->session
;
586 struct lttng_event
*event
;
587 const char *event_name
;
588 struct hlist_head
*head
;
593 if (chan
->free_event_id
== -1U) {
599 case LTTNG_KERNEL_TRACEPOINT
:
600 event_name
= event_desc
->name
;
602 case LTTNG_KERNEL_KPROBE
:
603 case LTTNG_KERNEL_UPROBE
:
604 case LTTNG_KERNEL_KRETPROBE
:
605 case LTTNG_KERNEL_NOOP
:
606 case LTTNG_KERNEL_SYSCALL
:
607 event_name
= event_param
->name
;
609 case LTTNG_KERNEL_FUNCTION
: /* Fall-through. */
615 name_len
= strlen(event_name
);
616 hash
= jhash(event_name
, name_len
, 0);
617 head
= &session
->events_ht
.table
[hash
& (LTTNG_EVENT_HT_SIZE
- 1)];
618 lttng_hlist_for_each_entry(event
, head
, hlist
) {
619 WARN_ON_ONCE(!event
->desc
);
620 if (!strncmp(event
->desc
->name
, event_name
,
621 LTTNG_KERNEL_SYM_NAME_LEN
- 1)
622 && chan
== event
->chan
) {
628 event
= kmem_cache_zalloc(event_cache
, GFP_KERNEL
);
634 event
->filter
= filter
;
635 event
->id
= chan
->free_event_id
++;
636 event
->instrumentation
= itype
;
637 event
->evtype
= LTTNG_TYPE_EVENT
;
638 INIT_LIST_HEAD(&event
->bytecode_runtime_head
);
639 INIT_LIST_HEAD(&event
->enablers_ref_head
);
642 case LTTNG_KERNEL_TRACEPOINT
:
643 /* Event will be enabled by enabler sync. */
645 event
->registered
= 0;
646 event
->desc
= lttng_event_get(event_name
);
651 /* Populate lttng_event structure before event registration. */
654 case LTTNG_KERNEL_KPROBE
:
656 * Needs to be explicitly enabled after creation, since
657 * we may want to apply filters.
660 event
->registered
= 1;
662 * Populate lttng_event structure before event
666 ret
= lttng_kprobes_register(event_name
,
667 event_param
->u
.kprobe
.symbol_name
,
668 event_param
->u
.kprobe
.offset
,
669 event_param
->u
.kprobe
.addr
,
675 ret
= try_module_get(event
->desc
->owner
);
678 case LTTNG_KERNEL_KRETPROBE
:
680 struct lttng_event
*event_return
;
682 /* kretprobe defines 2 events */
684 * Needs to be explicitly enabled after creation, since
685 * we may want to apply filters.
688 event
->registered
= 1;
690 kmem_cache_zalloc(event_cache
, GFP_KERNEL
);
695 event_return
->chan
= chan
;
696 event_return
->filter
= filter
;
697 event_return
->id
= chan
->free_event_id
++;
698 event_return
->enabled
= 0;
699 event_return
->registered
= 1;
700 event_return
->instrumentation
= itype
;
702 * Populate lttng_event structure before kretprobe registration.
705 ret
= lttng_kretprobes_register(event_name
,
706 event_param
->u
.kretprobe
.symbol_name
,
707 event_param
->u
.kretprobe
.offset
,
708 event_param
->u
.kretprobe
.addr
,
709 event
, event_return
);
711 kmem_cache_free(event_cache
, event_return
);
715 /* Take 2 refs on the module: one per event. */
716 ret
= try_module_get(event
->desc
->owner
);
718 ret
= try_module_get(event
->desc
->owner
);
720 ret
= _lttng_event_metadata_statedump(chan
->session
, chan
,
722 WARN_ON_ONCE(ret
> 0);
724 kmem_cache_free(event_cache
, event_return
);
725 module_put(event
->desc
->owner
);
726 module_put(event
->desc
->owner
);
727 goto statedump_error
;
729 list_add(&event_return
->list
, &chan
->session
->events
);
732 case LTTNG_KERNEL_NOOP
:
733 case LTTNG_KERNEL_SYSCALL
:
735 * Needs to be explicitly enabled after creation, since
736 * we may want to apply filters.
739 event
->registered
= 0;
740 event
->desc
= event_desc
;
746 case LTTNG_KERNEL_UPROBE
:
748 * Needs to be explicitly enabled after creation, since
749 * we may want to apply filters.
752 event
->registered
= 1;
755 * Populate lttng_event structure before event
760 ret
= lttng_uprobes_register(event_param
->name
,
761 event_param
->u
.uprobe
.fd
,
765 ret
= try_module_get(event
->desc
->owner
);
768 case LTTNG_KERNEL_FUNCTION
: /* Fall-through */
774 ret
= _lttng_event_metadata_statedump(chan
->session
, chan
, event
);
775 WARN_ON_ONCE(ret
> 0);
777 goto statedump_error
;
779 hlist_add_head(&event
->hlist
, head
);
780 list_add(&event
->list
, &chan
->session
->events
);
784 /* If a statedump error occurs, events will not be readable. */
786 kmem_cache_free(event_cache
, event
);
794 struct lttng_event
*lttng_event_create(struct lttng_channel
*chan
,
795 struct lttng_kernel_event
*event_param
,
797 const struct lttng_event_desc
*event_desc
,
798 enum lttng_kernel_instrumentation itype
)
800 struct lttng_event
*event
;
802 mutex_lock(&sessions_mutex
);
803 event
= _lttng_event_create(chan
, event_param
, filter
, event_desc
,
805 mutex_unlock(&sessions_mutex
);
809 /* Only used for tracepoints for now. */
811 void register_event(struct lttng_event
*event
)
813 const struct lttng_event_desc
*desc
;
816 if (event
->registered
)
820 switch (event
->instrumentation
) {
821 case LTTNG_KERNEL_TRACEPOINT
:
822 ret
= lttng_wrapper_tracepoint_probe_register(desc
->kname
,
823 desc
->probe_callback
,
826 case LTTNG_KERNEL_SYSCALL
:
827 ret
= lttng_syscall_filter_enable(event
->chan
,
830 case LTTNG_KERNEL_KPROBE
:
831 case LTTNG_KERNEL_UPROBE
:
832 case LTTNG_KERNEL_KRETPROBE
:
833 case LTTNG_KERNEL_NOOP
:
836 case LTTNG_KERNEL_FUNCTION
: /* Fall-through */
841 event
->registered
= 1;
845 * Only used internally at session destruction.
847 int _lttng_event_unregister(struct lttng_event
*event
)
849 const struct lttng_event_desc
*desc
;
852 if (!event
->registered
)
856 switch (event
->instrumentation
) {
857 case LTTNG_KERNEL_TRACEPOINT
:
858 ret
= lttng_wrapper_tracepoint_probe_unregister(event
->desc
->kname
,
859 event
->desc
->probe_callback
,
862 case LTTNG_KERNEL_KPROBE
:
863 lttng_kprobes_unregister(event
);
866 case LTTNG_KERNEL_KRETPROBE
:
867 lttng_kretprobes_unregister(event
);
870 case LTTNG_KERNEL_SYSCALL
:
871 ret
= lttng_syscall_filter_disable(event
->chan
,
874 case LTTNG_KERNEL_NOOP
:
877 case LTTNG_KERNEL_UPROBE
:
878 lttng_uprobes_unregister(event
);
881 case LTTNG_KERNEL_FUNCTION
: /* Fall-through */
886 event
->registered
= 0;
891 * Only used internally at session destruction.
894 void _lttng_event_destroy(struct lttng_event
*event
)
896 switch (event
->instrumentation
) {
897 case LTTNG_KERNEL_TRACEPOINT
:
898 lttng_event_put(event
->desc
);
900 case LTTNG_KERNEL_KPROBE
:
901 module_put(event
->desc
->owner
);
902 lttng_kprobes_destroy_private(event
);
904 case LTTNG_KERNEL_KRETPROBE
:
905 module_put(event
->desc
->owner
);
906 lttng_kretprobes_destroy_private(event
);
908 case LTTNG_KERNEL_NOOP
:
909 case LTTNG_KERNEL_SYSCALL
:
911 case LTTNG_KERNEL_UPROBE
:
912 module_put(event
->desc
->owner
);
913 lttng_uprobes_destroy_private(event
);
915 case LTTNG_KERNEL_FUNCTION
: /* Fall-through */
919 list_del(&event
->list
);
920 lttng_destroy_context(event
->ctx
);
921 kmem_cache_free(event_cache
, event
);
924 struct lttng_id_tracker
*get_tracker(struct lttng_session
*session
,
925 enum tracker_type tracker_type
)
927 switch (tracker_type
) {
929 return &session
->pid_tracker
;
931 return &session
->vpid_tracker
;
933 return &session
->uid_tracker
;
935 return &session
->vuid_tracker
;
937 return &session
->gid_tracker
;
939 return &session
->vgid_tracker
;
946 int lttng_session_track_id(struct lttng_session
*session
,
947 enum tracker_type tracker_type
, int id
)
949 struct lttng_id_tracker
*tracker
;
952 tracker
= get_tracker(session
, tracker_type
);
957 mutex_lock(&sessions_mutex
);
959 /* track all ids: destroy tracker. */
960 lttng_id_tracker_destroy(tracker
, true);
963 ret
= lttng_id_tracker_add(tracker
, id
);
965 mutex_unlock(&sessions_mutex
);
969 int lttng_session_untrack_id(struct lttng_session
*session
,
970 enum tracker_type tracker_type
, int id
)
972 struct lttng_id_tracker
*tracker
;
975 tracker
= get_tracker(session
, tracker_type
);
980 mutex_lock(&sessions_mutex
);
982 /* untrack all ids: replace by empty tracker. */
983 ret
= lttng_id_tracker_empty_set(tracker
);
985 ret
= lttng_id_tracker_del(tracker
, id
);
987 mutex_unlock(&sessions_mutex
);
992 void *id_list_start(struct seq_file
*m
, loff_t
*pos
)
994 struct lttng_id_tracker
*id_tracker
= m
->private;
995 struct lttng_id_tracker_rcu
*id_tracker_p
= id_tracker
->p
;
996 struct lttng_id_hash_node
*e
;
999 mutex_lock(&sessions_mutex
);
1001 for (i
= 0; i
< LTTNG_ID_TABLE_SIZE
; i
++) {
1002 struct hlist_head
*head
= &id_tracker_p
->id_hash
[i
];
1004 lttng_hlist_for_each_entry(e
, head
, hlist
) {
1010 /* ID tracker disabled. */
1011 if (iter
>= *pos
&& iter
== 0) {
1012 return id_tracker_p
; /* empty tracker */
1020 /* Called with sessions_mutex held. */
1022 void *id_list_next(struct seq_file
*m
, void *p
, loff_t
*ppos
)
1024 struct lttng_id_tracker
*id_tracker
= m
->private;
1025 struct lttng_id_tracker_rcu
*id_tracker_p
= id_tracker
->p
;
1026 struct lttng_id_hash_node
*e
;
1031 for (i
= 0; i
< LTTNG_ID_TABLE_SIZE
; i
++) {
1032 struct hlist_head
*head
= &id_tracker_p
->id_hash
[i
];
1034 lttng_hlist_for_each_entry(e
, head
, hlist
) {
1035 if (iter
++ >= *ppos
)
1040 /* ID tracker disabled. */
1041 if (iter
>= *ppos
&& iter
== 0)
1042 return p
; /* empty tracker */
1051 void id_list_stop(struct seq_file
*m
, void *p
)
1053 mutex_unlock(&sessions_mutex
);
1057 int id_list_show(struct seq_file
*m
, void *p
)
1059 struct lttng_id_tracker
*id_tracker
= m
->private;
1060 struct lttng_id_tracker_rcu
*id_tracker_p
= id_tracker
->p
;
1063 if (p
== id_tracker_p
) {
1064 /* Tracker disabled. */
1067 const struct lttng_id_hash_node
*e
= p
;
1069 id
= lttng_id_tracker_get_node_id(e
);
1071 switch (id_tracker
->tracker_type
) {
1073 seq_printf(m
, "process { pid = %d; };\n", id
);
1076 seq_printf(m
, "process { vpid = %d; };\n", id
);
1079 seq_printf(m
, "user { uid = %d; };\n", id
);
1082 seq_printf(m
, "user { vuid = %d; };\n", id
);
1085 seq_printf(m
, "group { gid = %d; };\n", id
);
1088 seq_printf(m
, "group { vgid = %d; };\n", id
);
1091 seq_printf(m
, "UNKNOWN { field = %d };\n", id
);
1097 const struct seq_operations lttng_tracker_ids_list_seq_ops
= {
1098 .start
= id_list_start
,
1099 .next
= id_list_next
,
1100 .stop
= id_list_stop
,
1101 .show
= id_list_show
,
1105 int lttng_tracker_ids_list_open(struct inode
*inode
, struct file
*file
)
1107 return seq_open(file
, <tng_tracker_ids_list_seq_ops
);
1111 int lttng_tracker_ids_list_release(struct inode
*inode
, struct file
*file
)
1113 struct seq_file
*m
= file
->private_data
;
1114 struct lttng_id_tracker
*id_tracker
= m
->private;
1117 WARN_ON_ONCE(!id_tracker
);
1118 ret
= seq_release(inode
, file
);
1120 fput(id_tracker
->session
->file
);
1124 const struct file_operations lttng_tracker_ids_list_fops
= {
1125 .owner
= THIS_MODULE
,
1126 .open
= lttng_tracker_ids_list_open
,
1128 .llseek
= seq_lseek
,
1129 .release
= lttng_tracker_ids_list_release
,
1132 int lttng_session_list_tracker_ids(struct lttng_session
*session
,
1133 enum tracker_type tracker_type
)
1135 struct file
*tracker_ids_list_file
;
1139 file_fd
= lttng_get_unused_fd();
1145 tracker_ids_list_file
= anon_inode_getfile("[lttng_tracker_ids_list]",
1146 <tng_tracker_ids_list_fops
,
1148 if (IS_ERR(tracker_ids_list_file
)) {
1149 ret
= PTR_ERR(tracker_ids_list_file
);
1152 if (!atomic_long_add_unless(&session
->file
->f_count
, 1, LONG_MAX
)) {
1154 goto refcount_error
;
1156 ret
= lttng_tracker_ids_list_fops
.open(NULL
, tracker_ids_list_file
);
1159 m
= tracker_ids_list_file
->private_data
;
1161 m
->private = get_tracker(session
, tracker_type
);
1162 BUG_ON(!m
->private);
1163 fd_install(file_fd
, tracker_ids_list_file
);
1168 atomic_long_dec(&session
->file
->f_count
);
1170 fput(tracker_ids_list_file
);
1172 put_unused_fd(file_fd
);
1178 * Enabler management.
1181 int lttng_match_enabler_star_glob(const char *desc_name
,
1182 const char *pattern
)
1184 if (!strutils_star_glob_match(pattern
, LTTNG_SIZE_MAX
,
1185 desc_name
, LTTNG_SIZE_MAX
))
1191 int lttng_match_enabler_name(const char *desc_name
,
1194 if (strcmp(desc_name
, name
))
1200 int lttng_desc_match_enabler(const struct lttng_event_desc
*desc
,
1201 struct lttng_enabler
*enabler
)
1203 const char *desc_name
, *enabler_name
;
1205 enabler_name
= enabler
->event_param
.name
;
1206 switch (enabler
->event_param
.instrumentation
) {
1207 case LTTNG_KERNEL_TRACEPOINT
:
1208 desc_name
= desc
->name
;
1210 case LTTNG_KERNEL_SYSCALL
:
1211 desc_name
= desc
->name
;
1212 if (!strncmp(desc_name
, "compat_", strlen("compat_")))
1213 desc_name
+= strlen("compat_");
1214 if (!strncmp(desc_name
, "syscall_exit_",
1215 strlen("syscall_exit_"))) {
1216 desc_name
+= strlen("syscall_exit_");
1217 } else if (!strncmp(desc_name
, "syscall_entry_",
1218 strlen("syscall_entry_"))) {
1219 desc_name
+= strlen("syscall_entry_");
1229 switch (enabler
->type
) {
1230 case LTTNG_ENABLER_STAR_GLOB
:
1231 return lttng_match_enabler_star_glob(desc_name
, enabler_name
);
1232 case LTTNG_ENABLER_NAME
:
1233 return lttng_match_enabler_name(desc_name
, enabler_name
);
1240 int lttng_event_match_enabler(struct lttng_event
*event
,
1241 struct lttng_enabler
*enabler
)
1243 if (enabler
->event_param
.instrumentation
!= event
->instrumentation
)
1245 if (lttng_desc_match_enabler(event
->desc
, enabler
)
1246 && event
->chan
== enabler
->chan
)
1253 struct lttng_enabler_ref
*lttng_event_enabler_ref(struct lttng_event
*event
,
1254 struct lttng_enabler
*enabler
)
1256 struct lttng_enabler_ref
*enabler_ref
;
1258 list_for_each_entry(enabler_ref
,
1259 &event
->enablers_ref_head
, node
) {
1260 if (enabler_ref
->ref
== enabler
)
1267 void lttng_create_tracepoint_if_missing(struct lttng_enabler
*enabler
)
1269 struct lttng_session
*session
= enabler
->chan
->session
;
1270 struct lttng_probe_desc
*probe_desc
;
1271 const struct lttng_event_desc
*desc
;
1273 struct list_head
*probe_list
;
1275 probe_list
= lttng_get_probe_list_head();
1277 * For each probe event, if we find that a probe event matches
1278 * our enabler, create an associated lttng_event if not
1281 list_for_each_entry(probe_desc
, probe_list
, head
) {
1282 for (i
= 0; i
< probe_desc
->nr_events
; i
++) {
1284 struct hlist_head
*head
;
1285 const char *event_name
;
1288 struct lttng_event
*event
;
1290 desc
= probe_desc
->event_desc
[i
];
1291 if (!lttng_desc_match_enabler(desc
, enabler
))
1293 event_name
= desc
->name
;
1294 name_len
= strlen(event_name
);
1297 * Check if already created.
1299 hash
= jhash(event_name
, name_len
, 0);
1300 head
= &session
->events_ht
.table
[hash
& (LTTNG_EVENT_HT_SIZE
- 1)];
1301 lttng_hlist_for_each_entry(event
, head
, hlist
) {
1302 if (event
->desc
== desc
1303 && event
->chan
== enabler
->chan
)
1310 * We need to create an event for this
1313 event
= _lttng_event_create(enabler
->chan
,
1315 LTTNG_KERNEL_TRACEPOINT
);
1317 printk(KERN_INFO
"Unable to create event %s\n",
1318 probe_desc
->event_desc
[i
]->name
);
1325 void lttng_create_syscall_if_missing(struct lttng_enabler
*enabler
)
1329 ret
= lttng_syscalls_register(enabler
->chan
, NULL
);
1334 * Create struct lttng_event if it is missing and present in the list of
1335 * tracepoint probes.
1336 * Should be called with sessions mutex held.
1339 void lttng_create_event_if_missing(struct lttng_enabler
*enabler
)
1341 switch (enabler
->event_param
.instrumentation
) {
1342 case LTTNG_KERNEL_TRACEPOINT
:
1343 lttng_create_tracepoint_if_missing(enabler
);
1345 case LTTNG_KERNEL_SYSCALL
:
1346 lttng_create_syscall_if_missing(enabler
);
1355 * Create events associated with an enabler (if not already present),
1356 * and add backward reference from the event to the enabler.
1357 * Should be called with sessions mutex held.
1360 int lttng_enabler_ref_events(struct lttng_enabler
*enabler
)
1362 struct lttng_session
*session
= enabler
->chan
->session
;
1363 struct lttng_event
*event
;
1365 /* First ensure that probe events are created for this enabler. */
1366 lttng_create_event_if_missing(enabler
);
1368 /* For each event matching enabler in session event list. */
1369 list_for_each_entry(event
, &session
->events
, list
) {
1370 struct lttng_enabler_ref
*enabler_ref
;
1372 if (!lttng_event_match_enabler(event
, enabler
))
1374 enabler_ref
= lttng_event_enabler_ref(event
, enabler
);
1377 * If no backward ref, create it.
1378 * Add backward ref from event to enabler.
1380 enabler_ref
= kzalloc(sizeof(*enabler_ref
), GFP_KERNEL
);
1383 enabler_ref
->ref
= enabler
;
1384 list_add(&enabler_ref
->node
,
1385 &event
->enablers_ref_head
);
1389 * Link filter bytecodes if not linked yet.
1391 lttng_enabler_event_link_bytecode(event
, enabler
);
1393 /* TODO: merge event context. */
1399 * Called at module load: connect the probe on all enablers matching
1401 * Called with sessions lock held.
1403 int lttng_fix_pending_events(void)
1405 struct lttng_session
*session
;
1407 list_for_each_entry(session
, &sessions
, list
)
1408 lttng_session_lazy_sync_enablers(session
);
1412 struct lttng_enabler
*lttng_enabler_create(enum lttng_enabler_type type
,
1413 struct lttng_kernel_event
*event_param
,
1414 struct lttng_channel
*chan
)
1416 struct lttng_enabler
*enabler
;
1418 enabler
= kzalloc(sizeof(*enabler
), GFP_KERNEL
);
1421 enabler
->type
= type
;
1422 INIT_LIST_HEAD(&enabler
->filter_bytecode_head
);
1423 memcpy(&enabler
->event_param
, event_param
,
1424 sizeof(enabler
->event_param
));
1425 enabler
->chan
= chan
;
1427 enabler
->enabled
= 0;
1428 enabler
->evtype
= LTTNG_TYPE_ENABLER
;
1429 mutex_lock(&sessions_mutex
);
1430 list_add(&enabler
->node
, &enabler
->chan
->session
->enablers_head
);
1431 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
1432 mutex_unlock(&sessions_mutex
);
1436 int lttng_enabler_enable(struct lttng_enabler
*enabler
)
1438 mutex_lock(&sessions_mutex
);
1439 enabler
->enabled
= 1;
1440 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
1441 mutex_unlock(&sessions_mutex
);
1445 int lttng_enabler_disable(struct lttng_enabler
*enabler
)
1447 mutex_lock(&sessions_mutex
);
1448 enabler
->enabled
= 0;
1449 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
1450 mutex_unlock(&sessions_mutex
);
1454 int lttng_enabler_attach_bytecode(struct lttng_enabler
*enabler
,
1455 struct lttng_kernel_filter_bytecode __user
*bytecode
)
1457 struct lttng_filter_bytecode_node
*bytecode_node
;
1458 uint32_t bytecode_len
;
1461 ret
= get_user(bytecode_len
, &bytecode
->len
);
1464 bytecode_node
= kzalloc(sizeof(*bytecode_node
) + bytecode_len
,
1468 ret
= copy_from_user(&bytecode_node
->bc
, bytecode
,
1469 sizeof(*bytecode
) + bytecode_len
);
1472 bytecode_node
->enabler
= enabler
;
1473 /* Enforce length based on allocated size */
1474 bytecode_node
->bc
.len
= bytecode_len
;
1475 list_add_tail(&bytecode_node
->node
, &enabler
->filter_bytecode_head
);
1476 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
1480 kfree(bytecode_node
);
1484 int lttng_event_add_callsite(struct lttng_event
*event
,
1485 struct lttng_kernel_event_callsite __user
*callsite
)
1488 switch (event
->instrumentation
) {
1489 case LTTNG_KERNEL_UPROBE
:
1490 return lttng_uprobes_add_callsite(event
, callsite
);
1496 int lttng_enabler_attach_context(struct lttng_enabler
*enabler
,
1497 struct lttng_kernel_context
*context_param
)
1503 void lttng_enabler_destroy(struct lttng_enabler
*enabler
)
1505 struct lttng_filter_bytecode_node
*filter_node
, *tmp_filter_node
;
1507 /* Destroy filter bytecode */
1508 list_for_each_entry_safe(filter_node
, tmp_filter_node
,
1509 &enabler
->filter_bytecode_head
, node
) {
1513 /* Destroy contexts */
1514 lttng_destroy_context(enabler
->ctx
);
1516 list_del(&enabler
->node
);
1521 * lttng_session_sync_enablers should be called just before starting a
1523 * Should be called with sessions mutex held.
1526 void lttng_session_sync_enablers(struct lttng_session
*session
)
1528 struct lttng_enabler
*enabler
;
1529 struct lttng_event
*event
;
1531 list_for_each_entry(enabler
, &session
->enablers_head
, node
)
1532 lttng_enabler_ref_events(enabler
);
1534 * For each event, if at least one of its enablers is enabled,
1535 * and its channel and session transient states are enabled, we
1536 * enable the event, else we disable it.
1538 list_for_each_entry(event
, &session
->events
, list
) {
1539 struct lttng_enabler_ref
*enabler_ref
;
1540 struct lttng_bytecode_runtime
*runtime
;
1541 int enabled
= 0, has_enablers_without_bytecode
= 0;
1543 switch (event
->instrumentation
) {
1544 case LTTNG_KERNEL_TRACEPOINT
:
1545 case LTTNG_KERNEL_SYSCALL
:
1547 list_for_each_entry(enabler_ref
,
1548 &event
->enablers_ref_head
, node
) {
1549 if (enabler_ref
->ref
->enabled
) {
1556 /* Not handled with lazy sync. */
1560 * Enabled state is based on union of enablers, with
1561 * intesection of session and channel transient enable
1564 enabled
= enabled
&& session
->tstate
&& event
->chan
->tstate
;
1566 WRITE_ONCE(event
->enabled
, enabled
);
1568 * Sync tracepoint registration with event enabled
1572 register_event(event
);
1574 _lttng_event_unregister(event
);
1577 /* Check if has enablers without bytecode enabled */
1578 list_for_each_entry(enabler_ref
,
1579 &event
->enablers_ref_head
, node
) {
1580 if (enabler_ref
->ref
->enabled
1581 && list_empty(&enabler_ref
->ref
->filter_bytecode_head
)) {
1582 has_enablers_without_bytecode
= 1;
1586 event
->has_enablers_without_bytecode
=
1587 has_enablers_without_bytecode
;
1589 /* Enable filters */
1590 list_for_each_entry(runtime
,
1591 &event
->bytecode_runtime_head
, node
)
1592 lttng_filter_sync_state(runtime
);
1597 * Apply enablers to session events, adding events to session if need
1598 * be. It is required after each modification applied to an active
1599 * session, and right before session "start".
1600 * "lazy" sync means we only sync if required.
1601 * Should be called with sessions mutex held.
1604 void lttng_session_lazy_sync_enablers(struct lttng_session
*session
)
1606 /* We can skip if session is not active */
1607 if (!session
->active
)
1609 lttng_session_sync_enablers(session
);
1613 * Serialize at most one packet worth of metadata into a metadata
1615 * We grab the metadata cache mutex to get exclusive access to our metadata
1616 * buffer and to the metadata cache. Exclusive access to the metadata buffer
1617 * allows us to do racy operations such as looking for remaining space left in
1618 * packet and write, since mutual exclusion protects us from concurrent writes.
1619 * Mutual exclusion on the metadata cache allow us to read the cache content
1620 * without racing against reallocation of the cache by updates.
1621 * Returns the number of bytes written in the channel, 0 if no data
1622 * was written and a negative value on error.
1624 int lttng_metadata_output_channel(struct lttng_metadata_stream
*stream
,
1625 struct channel
*chan
, bool *coherent
)
1627 struct lib_ring_buffer_ctx ctx
;
1629 size_t len
, reserve_len
;
1632 * Ensure we support mutiple get_next / put sequences followed by
1633 * put_next. The metadata cache lock protects reading the metadata
1634 * cache. It can indeed be read concurrently by "get_next_subbuf" and
1635 * "flush" operations on the buffer invoked by different processes.
1636 * Moreover, since the metadata cache memory can be reallocated, we
1637 * need to have exclusive access against updates even though we only
1640 mutex_lock(&stream
->metadata_cache
->lock
);
1641 WARN_ON(stream
->metadata_in
< stream
->metadata_out
);
1642 if (stream
->metadata_in
!= stream
->metadata_out
)
1645 /* Metadata regenerated, change the version. */
1646 if (stream
->metadata_cache
->version
!= stream
->version
)
1647 stream
->version
= stream
->metadata_cache
->version
;
1649 len
= stream
->metadata_cache
->metadata_written
-
1650 stream
->metadata_in
;
1653 reserve_len
= min_t(size_t,
1654 stream
->transport
->ops
.packet_avail_size(chan
),
1656 lib_ring_buffer_ctx_init(&ctx
, chan
, NULL
, reserve_len
,
1659 * If reservation failed, return an error to the caller.
1661 ret
= stream
->transport
->ops
.event_reserve(&ctx
, 0);
1663 printk(KERN_WARNING
"LTTng: Metadata event reservation failed\n");
1664 stream
->coherent
= false;
1667 stream
->transport
->ops
.event_write(&ctx
,
1668 stream
->metadata_cache
->data
+ stream
->metadata_in
,
1670 stream
->transport
->ops
.event_commit(&ctx
);
1671 stream
->metadata_in
+= reserve_len
;
1672 if (reserve_len
< len
)
1673 stream
->coherent
= false;
1675 stream
->coherent
= true;
1680 *coherent
= stream
->coherent
;
1681 mutex_unlock(&stream
->metadata_cache
->lock
);
1686 void lttng_metadata_begin(struct lttng_session
*session
)
1688 if (atomic_inc_return(&session
->metadata_cache
->producing
) == 1)
1689 mutex_lock(&session
->metadata_cache
->lock
);
1693 void lttng_metadata_end(struct lttng_session
*session
)
1695 WARN_ON_ONCE(!atomic_read(&session
->metadata_cache
->producing
));
1696 if (atomic_dec_return(&session
->metadata_cache
->producing
) == 0) {
1697 struct lttng_metadata_stream
*stream
;
1699 list_for_each_entry(stream
, &session
->metadata_cache
->metadata_stream
, list
)
1700 wake_up_interruptible(&stream
->read_wait
);
1701 mutex_unlock(&session
->metadata_cache
->lock
);
1706 * Write the metadata to the metadata cache.
1707 * Must be called with sessions_mutex held.
1708 * The metadata cache lock protects us from concurrent read access from
1709 * thread outputting metadata content to ring buffer.
1710 * The content of the printf is printed as a single atomic metadata
1713 int lttng_metadata_printf(struct lttng_session
*session
,
1714 const char *fmt
, ...)
1720 WARN_ON_ONCE(!READ_ONCE(session
->active
));
1723 str
= kvasprintf(GFP_KERNEL
, fmt
, ap
);
1729 WARN_ON_ONCE(!atomic_read(&session
->metadata_cache
->producing
));
1730 if (session
->metadata_cache
->metadata_written
+ len
>
1731 session
->metadata_cache
->cache_alloc
) {
1732 char *tmp_cache_realloc
;
1733 unsigned int tmp_cache_alloc_size
;
1735 tmp_cache_alloc_size
= max_t(unsigned int,
1736 session
->metadata_cache
->cache_alloc
+ len
,
1737 session
->metadata_cache
->cache_alloc
<< 1);
1738 tmp_cache_realloc
= vzalloc(tmp_cache_alloc_size
);
1739 if (!tmp_cache_realloc
)
1741 if (session
->metadata_cache
->data
) {
1742 memcpy(tmp_cache_realloc
,
1743 session
->metadata_cache
->data
,
1744 session
->metadata_cache
->cache_alloc
);
1745 vfree(session
->metadata_cache
->data
);
1748 session
->metadata_cache
->cache_alloc
= tmp_cache_alloc_size
;
1749 session
->metadata_cache
->data
= tmp_cache_realloc
;
1751 memcpy(session
->metadata_cache
->data
+
1752 session
->metadata_cache
->metadata_written
,
1754 session
->metadata_cache
->metadata_written
+= len
;
1765 int print_tabs(struct lttng_session
*session
, size_t nesting
)
1769 for (i
= 0; i
< nesting
; i
++) {
1772 ret
= lttng_metadata_printf(session
, " ");
1781 * Must be called with sessions_mutex held.
1784 int _lttng_struct_type_statedump(struct lttng_session
*session
,
1785 const struct lttng_type
*type
,
1789 uint32_t i
, nr_fields
;
1791 ret
= print_tabs(session
, nesting
);
1794 ret
= lttng_metadata_printf(session
,
1798 nr_fields
= type
->u
._struct
.nr_fields
;
1799 for (i
= 0; i
< nr_fields
; i
++) {
1800 const struct lttng_event_field
*iter_field
;
1802 iter_field
= &type
->u
._struct
.fields
[i
];
1803 ret
= _lttng_field_statedump(session
, iter_field
, nesting
+ 1);
1807 ret
= print_tabs(session
, nesting
);
1810 ret
= lttng_metadata_printf(session
,
1816 * Must be called with sessions_mutex held.
1819 int _lttng_struct_statedump(struct lttng_session
*session
,
1820 const struct lttng_event_field
*field
,
1825 ret
= _lttng_struct_type_statedump(session
,
1826 &field
->type
, nesting
);
1829 ret
= lttng_metadata_printf(session
,
1836 * Must be called with sessions_mutex held.
1839 int _lttng_variant_type_statedump(struct lttng_session
*session
,
1840 const struct lttng_type
*type
,
1844 uint32_t i
, nr_choices
;
1846 ret
= print_tabs(session
, nesting
);
1849 ret
= lttng_metadata_printf(session
,
1850 "variant <_%s> {\n",
1851 type
->u
.variant
.tag_name
);
1854 nr_choices
= type
->u
.variant
.nr_choices
;
1855 for (i
= 0; i
< nr_choices
; i
++) {
1856 const struct lttng_event_field
*iter_field
;
1858 iter_field
= &type
->u
.variant
.choices
[i
];
1859 ret
= _lttng_field_statedump(session
, iter_field
, nesting
+ 1);
1863 ret
= print_tabs(session
, nesting
);
1866 ret
= lttng_metadata_printf(session
,
1872 * Must be called with sessions_mutex held.
1875 int _lttng_variant_statedump(struct lttng_session
*session
,
1876 const struct lttng_event_field
*field
,
1881 ret
= _lttng_variant_type_statedump(session
,
1882 &field
->type
, nesting
);
1885 ret
= lttng_metadata_printf(session
,
1892 * Must be called with sessions_mutex held.
1895 int _lttng_array_compound_statedump(struct lttng_session
*session
,
1896 const struct lttng_event_field
*field
,
1900 const struct lttng_type
*elem_type
;
1902 /* Only array of structures and variants are currently supported. */
1903 elem_type
= field
->type
.u
.array_compound
.elem_type
;
1904 switch (elem_type
->atype
) {
1906 ret
= _lttng_struct_type_statedump(session
, elem_type
, nesting
);
1911 ret
= _lttng_variant_type_statedump(session
, elem_type
, nesting
);
1918 ret
= lttng_metadata_printf(session
,
1921 field
->type
.u
.array_compound
.length
);
1926 * Must be called with sessions_mutex held.
1929 int _lttng_sequence_compound_statedump(struct lttng_session
*session
,
1930 const struct lttng_event_field
*field
,
1934 const char *length_name
;
1935 const struct lttng_type
*elem_type
;
1937 length_name
= field
->type
.u
.sequence_compound
.length_name
;
1939 /* Only array of structures and variants are currently supported. */
1940 elem_type
= field
->type
.u
.sequence_compound
.elem_type
;
1941 switch (elem_type
->atype
) {
1943 ret
= _lttng_struct_type_statedump(session
, elem_type
, nesting
);
1948 ret
= _lttng_variant_type_statedump(session
, elem_type
, nesting
);
1955 ret
= lttng_metadata_printf(session
,
1963 * Must be called with sessions_mutex held.
1966 int _lttng_enum_statedump(struct lttng_session
*session
,
1967 const struct lttng_event_field
*field
,
1970 const struct lttng_enum_desc
*enum_desc
;
1971 const struct lttng_integer_type
*container_type
;
1973 unsigned int i
, nr_entries
;
1975 enum_desc
= field
->type
.u
.basic
.enumeration
.desc
;
1976 container_type
= &field
->type
.u
.basic
.enumeration
.container_type
;
1977 nr_entries
= enum_desc
->nr_entries
;
1979 ret
= print_tabs(session
, nesting
);
1982 ret
= lttng_metadata_printf(session
,
1983 "enum : integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } {\n",
1984 container_type
->size
,
1985 container_type
->alignment
,
1986 container_type
->signedness
,
1987 (container_type
->encoding
== lttng_encode_none
)
1989 : (container_type
->encoding
== lttng_encode_UTF8
)
1992 container_type
->base
,
1993 #if __BYTE_ORDER == __BIG_ENDIAN
1994 container_type
->reverse_byte_order
? " byte_order = le;" : ""
1996 container_type
->reverse_byte_order
? " byte_order = be;" : ""
2001 /* Dump all entries */
2002 for (i
= 0; i
< nr_entries
; i
++) {
2003 const struct lttng_enum_entry
*entry
= &enum_desc
->entries
[i
];
2006 ret
= print_tabs(session
, nesting
+ 1);
2009 ret
= lttng_metadata_printf(session
,
2013 len
= strlen(entry
->string
);
2014 /* Escape the character '"' */
2015 for (j
= 0; j
< len
; j
++) {
2016 char c
= entry
->string
[j
];
2020 ret
= lttng_metadata_printf(session
,
2024 ret
= lttng_metadata_printf(session
,
2028 ret
= lttng_metadata_printf(session
,
2035 ret
= lttng_metadata_printf(session
, "\"");
2039 if (entry
->options
.is_auto
) {
2040 ret
= lttng_metadata_printf(session
, ",\n");
2044 ret
= lttng_metadata_printf(session
,
2048 if (entry
->start
.signedness
)
2049 ret
= lttng_metadata_printf(session
,
2050 "%lld", (long long) entry
->start
.value
);
2052 ret
= lttng_metadata_printf(session
,
2053 "%llu", entry
->start
.value
);
2056 if (entry
->start
.signedness
== entry
->end
.signedness
&&
2058 == entry
->end
.value
) {
2059 ret
= lttng_metadata_printf(session
,
2062 if (entry
->end
.signedness
) {
2063 ret
= lttng_metadata_printf(session
,
2065 (long long) entry
->end
.value
);
2067 ret
= lttng_metadata_printf(session
,
2076 ret
= print_tabs(session
, nesting
);
2079 ret
= lttng_metadata_printf(session
, "} _%s;\n",
2086 * Must be called with sessions_mutex held.
2089 int _lttng_field_statedump(struct lttng_session
*session
,
2090 const struct lttng_event_field
*field
,
2095 switch (field
->type
.atype
) {
2097 ret
= print_tabs(session
, nesting
);
2100 ret
= lttng_metadata_printf(session
,
2101 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n",
2102 field
->type
.u
.basic
.integer
.size
,
2103 field
->type
.u
.basic
.integer
.alignment
,
2104 field
->type
.u
.basic
.integer
.signedness
,
2105 (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_none
)
2107 : (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
2110 field
->type
.u
.basic
.integer
.base
,
2111 #if __BYTE_ORDER == __BIG_ENDIAN
2112 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
2114 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
2119 ret
= _lttng_enum_statedump(session
, field
, nesting
);
2122 case atype_array_bitfield
:
2124 const struct lttng_basic_type
*elem_type
;
2126 elem_type
= &field
->type
.u
.array
.elem_type
;
2127 if (field
->type
.u
.array
.elem_alignment
) {
2128 ret
= print_tabs(session
, nesting
);
2131 ret
= lttng_metadata_printf(session
,
2132 "struct { } align(%u) _%s_padding;\n",
2133 field
->type
.u
.array
.elem_alignment
* CHAR_BIT
,
2138 ret
= print_tabs(session
, nesting
);
2141 ret
= lttng_metadata_printf(session
,
2142 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n",
2143 elem_type
->u
.basic
.integer
.size
,
2144 elem_type
->u
.basic
.integer
.alignment
,
2145 elem_type
->u
.basic
.integer
.signedness
,
2146 (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
2148 : (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
2151 elem_type
->u
.basic
.integer
.base
,
2152 #if __BYTE_ORDER == __BIG_ENDIAN
2153 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
2155 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
2157 field
->name
, field
->type
.u
.array
.length
);
2160 case atype_sequence
:
2161 case atype_sequence_bitfield
:
2163 const struct lttng_basic_type
*elem_type
;
2164 const struct lttng_basic_type
*length_type
;
2166 elem_type
= &field
->type
.u
.sequence
.elem_type
;
2167 length_type
= &field
->type
.u
.sequence
.length_type
;
2168 ret
= print_tabs(session
, nesting
);
2171 ret
= lttng_metadata_printf(session
,
2172 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n",
2173 length_type
->u
.basic
.integer
.size
,
2174 (unsigned int) length_type
->u
.basic
.integer
.alignment
,
2175 length_type
->u
.basic
.integer
.signedness
,
2176 (length_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
2178 : ((length_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
2181 length_type
->u
.basic
.integer
.base
,
2182 #if __BYTE_ORDER == __BIG_ENDIAN
2183 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
2185 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
2191 if (field
->type
.u
.sequence
.elem_alignment
) {
2192 ret
= print_tabs(session
, nesting
);
2195 ret
= lttng_metadata_printf(session
,
2196 "struct { } align(%u) _%s_padding;\n",
2197 field
->type
.u
.sequence
.elem_alignment
* CHAR_BIT
,
2202 ret
= print_tabs(session
, nesting
);
2205 ret
= lttng_metadata_printf(session
,
2206 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n",
2207 elem_type
->u
.basic
.integer
.size
,
2208 (unsigned int) elem_type
->u
.basic
.integer
.alignment
,
2209 elem_type
->u
.basic
.integer
.signedness
,
2210 (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
2212 : ((elem_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
2215 elem_type
->u
.basic
.integer
.base
,
2216 #if __BYTE_ORDER == __BIG_ENDIAN
2217 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
2219 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
2227 /* Default encoding is UTF8 */
2228 ret
= print_tabs(session
, nesting
);
2231 ret
= lttng_metadata_printf(session
,
2233 field
->type
.u
.basic
.string
.encoding
== lttng_encode_ASCII
?
2234 " { encoding = ASCII; }" : "",
2238 ret
= _lttng_struct_statedump(session
, field
, nesting
);
2240 case atype_array_compound
:
2241 ret
= _lttng_array_compound_statedump(session
, field
, nesting
);
2243 case atype_sequence_compound
:
2244 ret
= _lttng_sequence_compound_statedump(session
, field
, nesting
);
2247 ret
= _lttng_variant_statedump(session
, field
, nesting
);
2258 int _lttng_context_metadata_statedump(struct lttng_session
*session
,
2259 struct lttng_ctx
*ctx
)
2266 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
2267 const struct lttng_ctx_field
*field
= &ctx
->fields
[i
];
2269 ret
= _lttng_field_statedump(session
, &field
->event_field
, 2);
2277 int _lttng_fields_metadata_statedump(struct lttng_session
*session
,
2278 struct lttng_event
*event
)
2280 const struct lttng_event_desc
*desc
= event
->desc
;
2284 for (i
= 0; i
< desc
->nr_fields
; i
++) {
2285 const struct lttng_event_field
*field
= &desc
->fields
[i
];
2287 ret
= _lttng_field_statedump(session
, field
, 2);
2295 * Must be called with sessions_mutex held.
2296 * The entire event metadata is printed as a single atomic metadata
2300 int _lttng_event_metadata_statedump(struct lttng_session
*session
,
2301 struct lttng_channel
*chan
,
2302 struct lttng_event
*event
)
2306 if (event
->metadata_dumped
|| !READ_ONCE(session
->active
))
2308 if (chan
->channel_type
== METADATA_CHANNEL
)
2311 lttng_metadata_begin(session
);
2313 ret
= lttng_metadata_printf(session
,
2317 " stream_id = %u;\n",
2325 ret
= lttng_metadata_printf(session
,
2326 " context := struct {\n");
2330 ret
= _lttng_context_metadata_statedump(session
, event
->ctx
);
2334 ret
= lttng_metadata_printf(session
,
2340 ret
= lttng_metadata_printf(session
,
2341 " fields := struct {\n"
2346 ret
= _lttng_fields_metadata_statedump(session
, event
);
2351 * LTTng space reservation can only reserve multiples of the
2354 ret
= lttng_metadata_printf(session
,
2360 event
->metadata_dumped
= 1;
2362 lttng_metadata_end(session
);
2368 * Must be called with sessions_mutex held.
2369 * The entire channel metadata is printed as a single atomic metadata
2373 int _lttng_channel_metadata_statedump(struct lttng_session
*session
,
2374 struct lttng_channel
*chan
)
2378 if (chan
->metadata_dumped
|| !READ_ONCE(session
->active
))
2381 if (chan
->channel_type
== METADATA_CHANNEL
)
2384 lttng_metadata_begin(session
);
2386 WARN_ON_ONCE(!chan
->header_type
);
2387 ret
= lttng_metadata_printf(session
,
2390 " event.header := %s;\n"
2391 " packet.context := struct packet_context;\n",
2393 chan
->header_type
== 1 ? "struct event_header_compact" :
2394 "struct event_header_large");
2399 ret
= lttng_metadata_printf(session
,
2400 " event.context := struct {\n");
2404 ret
= _lttng_context_metadata_statedump(session
, chan
->ctx
);
2408 ret
= lttng_metadata_printf(session
,
2414 ret
= lttng_metadata_printf(session
,
2417 chan
->metadata_dumped
= 1;
2419 lttng_metadata_end(session
);
2424 * Must be called with sessions_mutex held.
2427 int _lttng_stream_packet_context_declare(struct lttng_session
*session
)
2429 return lttng_metadata_printf(session
,
2430 "struct packet_context {\n"
2431 " uint64_clock_monotonic_t timestamp_begin;\n"
2432 " uint64_clock_monotonic_t timestamp_end;\n"
2433 " uint64_t content_size;\n"
2434 " uint64_t packet_size;\n"
2435 " uint64_t packet_seq_num;\n"
2436 " unsigned long events_discarded;\n"
2437 " uint32_t cpu_id;\n"
2444 * id: range: 0 - 30.
2445 * id 31 is reserved to indicate an extended header.
2448 * id: range: 0 - 65534.
2449 * id 65535 is reserved to indicate an extended header.
2451 * Must be called with sessions_mutex held.
2454 int _lttng_event_header_declare(struct lttng_session
*session
)
2456 return lttng_metadata_printf(session
,
2457 "struct event_header_compact {\n"
2458 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
2461 " uint27_clock_monotonic_t timestamp;\n"
2465 " uint64_clock_monotonic_t timestamp;\n"
2470 "struct event_header_large {\n"
2471 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
2474 " uint32_clock_monotonic_t timestamp;\n"
2478 " uint64_clock_monotonic_t timestamp;\n"
2482 lttng_alignof(uint32_t) * CHAR_BIT
,
2483 lttng_alignof(uint16_t) * CHAR_BIT
2488 * Approximation of NTP time of day to clock monotonic correlation,
2489 * taken at start of trace.
2490 * Yes, this is only an approximation. Yes, we can (and will) do better
2491 * in future versions.
2492 * This function may return a negative offset. It may happen if the
2493 * system sets the REALTIME clock to 0 after boot.
2495 * Use 64bit timespec on kernels that have it, this makes 32bit arch
2499 int64_t measure_clock_offset(void)
2501 uint64_t monotonic_avg
, monotonic
[2], realtime
;
2502 uint64_t tcf
= trace_clock_freq();
2504 unsigned long flags
;
2505 #ifdef LTTNG_KERNEL_HAS_TIMESPEC64
2506 struct timespec64 rts
= { 0, 0 };
2508 struct timespec rts
= { 0, 0 };
2511 /* Disable interrupts to increase correlation precision. */
2512 local_irq_save(flags
);
2513 monotonic
[0] = trace_clock_read64();
2514 #ifdef LTTNG_KERNEL_HAS_TIMESPEC64
2515 ktime_get_real_ts64(&rts
);
2517 getnstimeofday(&rts
);
2519 monotonic
[1] = trace_clock_read64();
2520 local_irq_restore(flags
);
2522 monotonic_avg
= (monotonic
[0] + monotonic
[1]) >> 1;
2523 realtime
= (uint64_t) rts
.tv_sec
* tcf
;
2524 if (tcf
== NSEC_PER_SEC
) {
2525 realtime
+= rts
.tv_nsec
;
2527 uint64_t n
= rts
.tv_nsec
* tcf
;
2529 do_div(n
, NSEC_PER_SEC
);
2532 offset
= (int64_t) realtime
- monotonic_avg
;
2537 int print_escaped_ctf_string(struct lttng_session
*session
, const char *string
)
2545 while (cur
!= '\0') {
2548 ret
= lttng_metadata_printf(session
, "%s", "\\n");
2552 ret
= lttng_metadata_printf(session
, "%c", '\\');
2555 /* We still print the current char */
2558 ret
= lttng_metadata_printf(session
, "%c", cur
);
2572 int print_metadata_escaped_field(struct lttng_session
*session
, const char *field
,
2573 const char *field_value
)
2577 ret
= lttng_metadata_printf(session
, " %s = \"", field
);
2581 ret
= print_escaped_ctf_string(session
, field_value
);
2585 ret
= lttng_metadata_printf(session
, "\";\n");
2592 * Output metadata into this session's metadata buffers.
2593 * Must be called with sessions_mutex held.
2596 int _lttng_session_metadata_statedump(struct lttng_session
*session
)
2598 unsigned char *uuid_c
= session
->uuid
.b
;
2599 unsigned char uuid_s
[37], clock_uuid_s
[BOOT_ID_LEN
];
2600 const char *product_uuid
;
2601 struct lttng_channel
*chan
;
2602 struct lttng_event
*event
;
2605 if (!READ_ONCE(session
->active
))
2608 lttng_metadata_begin(session
);
2610 if (session
->metadata_dumped
)
2613 snprintf(uuid_s
, sizeof(uuid_s
),
2614 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
2615 uuid_c
[0], uuid_c
[1], uuid_c
[2], uuid_c
[3],
2616 uuid_c
[4], uuid_c
[5], uuid_c
[6], uuid_c
[7],
2617 uuid_c
[8], uuid_c
[9], uuid_c
[10], uuid_c
[11],
2618 uuid_c
[12], uuid_c
[13], uuid_c
[14], uuid_c
[15]);
2620 ret
= lttng_metadata_printf(session
,
2621 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
2622 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
2623 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
2624 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
2625 "typealias integer { size = %u; align = %u; signed = false; } := unsigned long;\n"
2626 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
2627 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
2633 " byte_order = %s;\n"
2634 " packet.header := struct {\n"
2635 " uint32_t magic;\n"
2636 " uint8_t uuid[16];\n"
2637 " uint32_t stream_id;\n"
2638 " uint64_t stream_instance_id;\n"
2641 lttng_alignof(uint8_t) * CHAR_BIT
,
2642 lttng_alignof(uint16_t) * CHAR_BIT
,
2643 lttng_alignof(uint32_t) * CHAR_BIT
,
2644 lttng_alignof(uint64_t) * CHAR_BIT
,
2645 sizeof(unsigned long) * CHAR_BIT
,
2646 lttng_alignof(unsigned long) * CHAR_BIT
,
2650 #if __BYTE_ORDER == __BIG_ENDIAN
2659 ret
= lttng_metadata_printf(session
,
2661 " hostname = \"%s\";\n"
2662 " domain = \"kernel\";\n"
2663 " sysname = \"%s\";\n"
2664 " kernel_release = \"%s\";\n"
2665 " kernel_version = \"%s\";\n"
2666 " tracer_name = \"lttng-modules\";\n"
2667 " tracer_major = %d;\n"
2668 " tracer_minor = %d;\n"
2669 " tracer_patchlevel = %d;\n"
2670 " trace_buffering_scheme = \"global\";\n",
2671 current
->nsproxy
->uts_ns
->name
.nodename
,
2675 LTTNG_MODULES_MAJOR_VERSION
,
2676 LTTNG_MODULES_MINOR_VERSION
,
2677 LTTNG_MODULES_PATCHLEVEL_VERSION
2682 ret
= print_metadata_escaped_field(session
, "trace_name", session
->name
);
2685 ret
= print_metadata_escaped_field(session
, "trace_creation_datetime",
2686 session
->creation_time
);
2690 /* Add the product UUID to the 'env' section */
2691 product_uuid
= dmi_get_system_info(DMI_PRODUCT_UUID
);
2693 ret
= lttng_metadata_printf(session
,
2694 " product_uuid = \"%s\";\n",
2701 /* Close the 'env' section */
2702 ret
= lttng_metadata_printf(session
, "};\n\n");
2706 ret
= lttng_metadata_printf(session
,
2708 " name = \"%s\";\n",
2714 if (!trace_clock_uuid(clock_uuid_s
)) {
2715 ret
= lttng_metadata_printf(session
,
2716 " uuid = \"%s\";\n",
2723 ret
= lttng_metadata_printf(session
,
2724 " description = \"%s\";\n"
2725 " freq = %llu; /* Frequency, in Hz */\n"
2726 " /* clock value offset from Epoch is: offset * (1/freq) */\n"
2729 trace_clock_description(),
2730 (unsigned long long) trace_clock_freq(),
2731 (long long) measure_clock_offset()
2736 ret
= lttng_metadata_printf(session
,
2737 "typealias integer {\n"
2738 " size = 27; align = 1; signed = false;\n"
2739 " map = clock.%s.value;\n"
2740 "} := uint27_clock_monotonic_t;\n"
2742 "typealias integer {\n"
2743 " size = 32; align = %u; signed = false;\n"
2744 " map = clock.%s.value;\n"
2745 "} := uint32_clock_monotonic_t;\n"
2747 "typealias integer {\n"
2748 " size = 64; align = %u; signed = false;\n"
2749 " map = clock.%s.value;\n"
2750 "} := uint64_clock_monotonic_t;\n\n",
2752 lttng_alignof(uint32_t) * CHAR_BIT
,
2754 lttng_alignof(uint64_t) * CHAR_BIT
,
2760 ret
= _lttng_stream_packet_context_declare(session
);
2764 ret
= _lttng_event_header_declare(session
);
2769 list_for_each_entry(chan
, &session
->chan
, list
) {
2770 ret
= _lttng_channel_metadata_statedump(session
, chan
);
2775 list_for_each_entry(event
, &session
->events
, list
) {
2776 ret
= _lttng_event_metadata_statedump(session
, event
->chan
, event
);
2780 session
->metadata_dumped
= 1;
2782 lttng_metadata_end(session
);
2787 * lttng_transport_register - LTT transport registration
2788 * @transport: transport structure
2790 * Registers a transport which can be used as output to extract the data out of
2791 * LTTng. The module calling this registration function must ensure that no
2792 * trap-inducing code will be executed by the transport functions. E.g.
2793 * vmalloc_sync_mappings() must be called between a vmalloc and the moment the memory
2794 * is made visible to the transport function. This registration acts as a
2795 * vmalloc_sync_mappings. Therefore, only if the module allocates virtual memory
2796 * after its registration must it synchronize the TLBs.
2798 void lttng_transport_register(struct lttng_transport
*transport
)
2801 * Make sure no page fault can be triggered by the module about to be
2802 * registered. We deal with this here so we don't have to call
2803 * vmalloc_sync_mappings() in each module's init.
2805 wrapper_vmalloc_sync_mappings();
2807 mutex_lock(&sessions_mutex
);
2808 list_add_tail(&transport
->node
, <tng_transport_list
);
2809 mutex_unlock(&sessions_mutex
);
2811 EXPORT_SYMBOL_GPL(lttng_transport_register
);
2814 * lttng_transport_unregister - LTT transport unregistration
2815 * @transport: transport structure
2817 void lttng_transport_unregister(struct lttng_transport
*transport
)
2819 mutex_lock(&sessions_mutex
);
2820 list_del(&transport
->node
);
2821 mutex_unlock(&sessions_mutex
);
2823 EXPORT_SYMBOL_GPL(lttng_transport_unregister
);
2825 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0))
2827 enum cpuhp_state lttng_hp_prepare
;
2828 enum cpuhp_state lttng_hp_online
;
2830 static int lttng_hotplug_prepare(unsigned int cpu
, struct hlist_node
*node
)
2832 struct lttng_cpuhp_node
*lttng_node
;
2834 lttng_node
= container_of(node
, struct lttng_cpuhp_node
, node
);
2835 switch (lttng_node
->component
) {
2836 case LTTNG_RING_BUFFER_FRONTEND
:
2838 case LTTNG_RING_BUFFER_BACKEND
:
2839 return lttng_cpuhp_rb_backend_prepare(cpu
, lttng_node
);
2840 case LTTNG_RING_BUFFER_ITER
:
2842 case LTTNG_CONTEXT_PERF_COUNTERS
:
2849 static int lttng_hotplug_dead(unsigned int cpu
, struct hlist_node
*node
)
2851 struct lttng_cpuhp_node
*lttng_node
;
2853 lttng_node
= container_of(node
, struct lttng_cpuhp_node
, node
);
2854 switch (lttng_node
->component
) {
2855 case LTTNG_RING_BUFFER_FRONTEND
:
2856 return lttng_cpuhp_rb_frontend_dead(cpu
, lttng_node
);
2857 case LTTNG_RING_BUFFER_BACKEND
:
2859 case LTTNG_RING_BUFFER_ITER
:
2861 case LTTNG_CONTEXT_PERF_COUNTERS
:
2862 return lttng_cpuhp_perf_counter_dead(cpu
, lttng_node
);
2868 static int lttng_hotplug_online(unsigned int cpu
, struct hlist_node
*node
)
2870 struct lttng_cpuhp_node
*lttng_node
;
2872 lttng_node
= container_of(node
, struct lttng_cpuhp_node
, node
);
2873 switch (lttng_node
->component
) {
2874 case LTTNG_RING_BUFFER_FRONTEND
:
2875 return lttng_cpuhp_rb_frontend_online(cpu
, lttng_node
);
2876 case LTTNG_RING_BUFFER_BACKEND
:
2878 case LTTNG_RING_BUFFER_ITER
:
2879 return lttng_cpuhp_rb_iter_online(cpu
, lttng_node
);
2880 case LTTNG_CONTEXT_PERF_COUNTERS
:
2881 return lttng_cpuhp_perf_counter_online(cpu
, lttng_node
);
2887 static int lttng_hotplug_offline(unsigned int cpu
, struct hlist_node
*node
)
2889 struct lttng_cpuhp_node
*lttng_node
;
2891 lttng_node
= container_of(node
, struct lttng_cpuhp_node
, node
);
2892 switch (lttng_node
->component
) {
2893 case LTTNG_RING_BUFFER_FRONTEND
:
2894 return lttng_cpuhp_rb_frontend_offline(cpu
, lttng_node
);
2895 case LTTNG_RING_BUFFER_BACKEND
:
2897 case LTTNG_RING_BUFFER_ITER
:
2899 case LTTNG_CONTEXT_PERF_COUNTERS
:
2906 static int __init
lttng_init_cpu_hotplug(void)
2910 ret
= cpuhp_setup_state_multi(CPUHP_BP_PREPARE_DYN
, "lttng:prepare",
2911 lttng_hotplug_prepare
,
2912 lttng_hotplug_dead
);
2916 lttng_hp_prepare
= ret
;
2917 lttng_rb_set_hp_prepare(ret
);
2919 ret
= cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN
, "lttng:online",
2920 lttng_hotplug_online
,
2921 lttng_hotplug_offline
);
2923 cpuhp_remove_multi_state(lttng_hp_prepare
);
2924 lttng_hp_prepare
= 0;
2927 lttng_hp_online
= ret
;
2928 lttng_rb_set_hp_online(ret
);
2933 static void __exit
lttng_exit_cpu_hotplug(void)
2935 lttng_rb_set_hp_online(0);
2936 cpuhp_remove_multi_state(lttng_hp_online
);
2937 lttng_rb_set_hp_prepare(0);
2938 cpuhp_remove_multi_state(lttng_hp_prepare
);
2941 #else /* #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
2942 static int lttng_init_cpu_hotplug(void)
2946 static void lttng_exit_cpu_hotplug(void)
2949 #endif /* #else #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
2952 static int __init
lttng_events_init(void)
2956 ret
= wrapper_lttng_fixup_sig(THIS_MODULE
);
2959 ret
= wrapper_get_pfnblock_flags_mask_init();
2962 ret
= wrapper_get_pageblock_flags_mask_init();
2965 ret
= lttng_probes_init();
2968 ret
= lttng_context_init();
2971 ret
= lttng_tracepoint_init();
2974 event_cache
= KMEM_CACHE(lttng_event
, 0);
2979 ret
= lttng_abi_init();
2982 ret
= lttng_logger_init();
2985 ret
= lttng_init_cpu_hotplug();
2988 printk(KERN_NOTICE
"LTTng: Loaded modules v%s.%s.%s%s (%s)%s%s\n",
2989 __stringify(LTTNG_MODULES_MAJOR_VERSION
),
2990 __stringify(LTTNG_MODULES_MINOR_VERSION
),
2991 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION
),
2992 LTTNG_MODULES_EXTRAVERSION
,
2994 #ifdef LTTNG_EXTRA_VERSION_GIT
2995 LTTNG_EXTRA_VERSION_GIT
[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_GIT
,
2999 #ifdef LTTNG_EXTRA_VERSION_NAME
3000 LTTNG_EXTRA_VERSION_NAME
[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_NAME
);
3007 lttng_logger_exit();
3011 kmem_cache_destroy(event_cache
);
3013 lttng_tracepoint_exit();
3015 lttng_context_exit();
3016 printk(KERN_NOTICE
"LTTng: Failed to load modules v%s.%s.%s%s (%s)%s%s\n",
3017 __stringify(LTTNG_MODULES_MAJOR_VERSION
),
3018 __stringify(LTTNG_MODULES_MINOR_VERSION
),
3019 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION
),
3020 LTTNG_MODULES_EXTRAVERSION
,
3022 #ifdef LTTNG_EXTRA_VERSION_GIT
3023 LTTNG_EXTRA_VERSION_GIT
[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_GIT
,
3027 #ifdef LTTNG_EXTRA_VERSION_NAME
3028 LTTNG_EXTRA_VERSION_NAME
[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_NAME
);
3035 module_init(lttng_events_init
);
3037 static void __exit
lttng_events_exit(void)
3039 struct lttng_session
*session
, *tmpsession
;
3041 lttng_exit_cpu_hotplug();
3042 lttng_logger_exit();
3044 list_for_each_entry_safe(session
, tmpsession
, &sessions
, list
)
3045 lttng_session_destroy(session
);
3046 kmem_cache_destroy(event_cache
);
3047 lttng_tracepoint_exit();
3048 lttng_context_exit();
3049 printk(KERN_NOTICE
"LTTng: Unloaded modules v%s.%s.%s%s (%s)%s%s\n",
3050 __stringify(LTTNG_MODULES_MAJOR_VERSION
),
3051 __stringify(LTTNG_MODULES_MINOR_VERSION
),
3052 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION
),
3053 LTTNG_MODULES_EXTRAVERSION
,
3055 #ifdef LTTNG_EXTRA_VERSION_GIT
3056 LTTNG_EXTRA_VERSION_GIT
[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_GIT
,
3060 #ifdef LTTNG_EXTRA_VERSION_NAME
3061 LTTNG_EXTRA_VERSION_NAME
[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_NAME
);
3067 module_exit(lttng_events_exit
);
3069 #include "extra_version/patches.i"
3070 #ifdef LTTNG_EXTRA_VERSION_GIT
3071 MODULE_INFO(extra_version_git
, LTTNG_EXTRA_VERSION_GIT
);
3073 #ifdef LTTNG_EXTRA_VERSION_NAME
3074 MODULE_INFO(extra_version_name
, LTTNG_EXTRA_VERSION_NAME
);
3076 MODULE_LICENSE("GPL and additional rights");
3077 MODULE_AUTHOR("Mathieu Desnoyers <mathieu.desnoyers@efficios.com>");
3078 MODULE_DESCRIPTION("LTTng tracer");
3079 MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION
) "."
3080 __stringify(LTTNG_MODULES_MINOR_VERSION
) "."
3081 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION
)
3082 LTTNG_MODULES_EXTRAVERSION
);