4 * Holds LTTng per-session event registry.
6 * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; only
11 * version 2.1 of the License.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #include <linux/module.h>
24 #include <linux/list.h>
25 #include <linux/mutex.h>
26 #include <linux/sched.h>
27 #include <linux/slab.h>
28 #include <linux/jiffies.h>
29 #include <linux/utsname.h>
30 #include <linux/err.h>
31 #include "wrapper/uuid.h"
32 #include "wrapper/vmalloc.h" /* for wrapper_vmalloc_sync_all() */
33 #include "wrapper/random.h"
34 #include "wrapper/tracepoint.h"
35 #include "lttng-kernel-version.h"
36 #include "lttng-events.h"
37 #include "lttng-tracer.h"
38 #include "lttng-abi-old.h"
40 #define METADATA_CACHE_DEFAULT_SIZE 4096
42 static LIST_HEAD(sessions
);
43 static LIST_HEAD(lttng_transport_list
);
45 * Protect the sessions and metadata caches.
47 static DEFINE_MUTEX(sessions_mutex
);
48 static struct kmem_cache
*event_cache
;
50 static void _lttng_event_destroy(struct lttng_event
*event
);
51 static void _lttng_channel_destroy(struct lttng_channel
*chan
);
52 static int _lttng_event_unregister(struct lttng_event
*event
);
54 int _lttng_event_metadata_statedump(struct lttng_session
*session
,
55 struct lttng_channel
*chan
,
56 struct lttng_event
*event
);
58 int _lttng_session_metadata_statedump(struct lttng_session
*session
);
60 void _lttng_metadata_channel_hangup(struct lttng_metadata_stream
*stream
);
62 void synchronize_trace(void)
65 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0))
66 #ifdef CONFIG_PREEMPT_RT_FULL
69 #else /* (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)) */
70 #ifdef CONFIG_PREEMPT_RT
73 #endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)) */
76 struct lttng_session
*lttng_session_create(void)
78 struct lttng_session
*session
;
79 struct lttng_metadata_cache
*metadata_cache
;
81 mutex_lock(&sessions_mutex
);
82 session
= kzalloc(sizeof(struct lttng_session
), GFP_KERNEL
);
85 INIT_LIST_HEAD(&session
->chan
);
86 INIT_LIST_HEAD(&session
->events
);
87 uuid_le_gen(&session
->uuid
);
89 metadata_cache
= kzalloc(sizeof(struct lttng_metadata_cache
),
92 goto err_free_session
;
93 metadata_cache
->data
= kzalloc(METADATA_CACHE_DEFAULT_SIZE
,
95 if (!metadata_cache
->data
)
97 metadata_cache
->cache_alloc
= METADATA_CACHE_DEFAULT_SIZE
;
98 kref_init(&metadata_cache
->refcount
);
99 session
->metadata_cache
= metadata_cache
;
100 INIT_LIST_HEAD(&metadata_cache
->metadata_stream
);
101 list_add(&session
->list
, &sessions
);
102 mutex_unlock(&sessions_mutex
);
106 kfree(metadata_cache
);
110 mutex_unlock(&sessions_mutex
);
114 void metadata_cache_destroy(struct kref
*kref
)
116 struct lttng_metadata_cache
*cache
=
117 container_of(kref
, struct lttng_metadata_cache
, refcount
);
122 void lttng_session_destroy(struct lttng_session
*session
)
124 struct lttng_channel
*chan
, *tmpchan
;
125 struct lttng_event
*event
, *tmpevent
;
126 struct lttng_metadata_stream
*metadata_stream
;
129 mutex_lock(&sessions_mutex
);
130 ACCESS_ONCE(session
->active
) = 0;
131 list_for_each_entry(chan
, &session
->chan
, list
) {
132 ret
= lttng_syscalls_unregister(chan
);
135 list_for_each_entry(event
, &session
->events
, list
) {
136 ret
= _lttng_event_unregister(event
);
139 synchronize_trace(); /* Wait for in-flight events to complete */
140 list_for_each_entry_safe(event
, tmpevent
, &session
->events
, list
)
141 _lttng_event_destroy(event
);
142 list_for_each_entry_safe(chan
, tmpchan
, &session
->chan
, list
) {
143 BUG_ON(chan
->channel_type
== METADATA_CHANNEL
);
144 _lttng_channel_destroy(chan
);
146 list_for_each_entry(metadata_stream
, &session
->metadata_cache
->metadata_stream
, list
)
147 _lttng_metadata_channel_hangup(metadata_stream
);
148 kref_put(&session
->metadata_cache
->refcount
, metadata_cache_destroy
);
149 list_del(&session
->list
);
150 mutex_unlock(&sessions_mutex
);
154 int lttng_session_enable(struct lttng_session
*session
)
157 struct lttng_channel
*chan
;
159 mutex_lock(&sessions_mutex
);
160 if (session
->active
) {
166 * Snapshot the number of events per channel to know the type of header
169 list_for_each_entry(chan
, &session
->chan
, list
) {
170 if (chan
->header_type
)
171 continue; /* don't change it if session stop/restart */
172 if (chan
->free_event_id
< 31)
173 chan
->header_type
= 1; /* compact */
175 chan
->header_type
= 2; /* large */
178 ACCESS_ONCE(session
->active
) = 1;
179 ACCESS_ONCE(session
->been_active
) = 1;
180 ret
= _lttng_session_metadata_statedump(session
);
182 ACCESS_ONCE(session
->active
) = 0;
185 ret
= lttng_statedump_start(session
);
187 ACCESS_ONCE(session
->active
) = 0;
189 mutex_unlock(&sessions_mutex
);
193 int lttng_session_disable(struct lttng_session
*session
)
197 mutex_lock(&sessions_mutex
);
198 if (!session
->active
) {
202 ACCESS_ONCE(session
->active
) = 0;
204 mutex_unlock(&sessions_mutex
);
208 int lttng_channel_enable(struct lttng_channel
*channel
)
212 if (channel
->channel_type
== METADATA_CHANNEL
)
214 old
= xchg(&channel
->enabled
, 1);
220 int lttng_channel_disable(struct lttng_channel
*channel
)
224 if (channel
->channel_type
== METADATA_CHANNEL
)
226 old
= xchg(&channel
->enabled
, 0);
232 int lttng_event_enable(struct lttng_event
*event
)
236 if (event
->chan
->channel_type
== METADATA_CHANNEL
)
238 old
= xchg(&event
->enabled
, 1);
244 int lttng_event_disable(struct lttng_event
*event
)
248 if (event
->chan
->channel_type
== METADATA_CHANNEL
)
250 old
= xchg(&event
->enabled
, 0);
256 static struct lttng_transport
*lttng_transport_find(const char *name
)
258 struct lttng_transport
*transport
;
260 list_for_each_entry(transport
, <tng_transport_list
, node
) {
261 if (!strcmp(transport
->name
, name
))
267 struct lttng_channel
*lttng_channel_create(struct lttng_session
*session
,
268 const char *transport_name
,
270 size_t subbuf_size
, size_t num_subbuf
,
271 unsigned int switch_timer_interval
,
272 unsigned int read_timer_interval
,
273 enum channel_type channel_type
)
275 struct lttng_channel
*chan
;
276 struct lttng_transport
*transport
= NULL
;
278 mutex_lock(&sessions_mutex
);
279 if (session
->been_active
&& channel_type
!= METADATA_CHANNEL
)
280 goto active
; /* Refuse to add channel to active session */
281 transport
= lttng_transport_find(transport_name
);
283 printk(KERN_WARNING
"LTTng transport %s not found\n",
287 if (!try_module_get(transport
->owner
)) {
288 printk(KERN_WARNING
"LTT : Can't lock transport module.\n");
291 chan
= kzalloc(sizeof(struct lttng_channel
), GFP_KERNEL
);
294 chan
->session
= session
;
295 chan
->id
= session
->free_chan_id
++;
296 chan
->ops
= &transport
->ops
;
298 * Note: the channel creation op already writes into the packet
299 * headers. Therefore the "chan" information used as input
300 * should be already accessible.
302 chan
->chan
= transport
->ops
.channel_create(transport_name
,
303 chan
, buf_addr
, subbuf_size
, num_subbuf
,
304 switch_timer_interval
, read_timer_interval
);
308 chan
->transport
= transport
;
309 chan
->channel_type
= channel_type
;
310 list_add(&chan
->list
, &session
->chan
);
311 mutex_unlock(&sessions_mutex
);
318 module_put(transport
->owner
);
321 mutex_unlock(&sessions_mutex
);
326 * Only used internally at session destruction for per-cpu channels, and
327 * when metadata channel is released.
328 * Needs to be called with sessions mutex held.
331 void _lttng_channel_destroy(struct lttng_channel
*chan
)
333 chan
->ops
->channel_destroy(chan
->chan
);
334 module_put(chan
->transport
->owner
);
335 list_del(&chan
->list
);
336 lttng_destroy_context(chan
->ctx
);
340 void lttng_metadata_channel_destroy(struct lttng_channel
*chan
)
342 BUG_ON(chan
->channel_type
!= METADATA_CHANNEL
);
344 /* Protect the metadata cache with the sessions_mutex. */
345 mutex_lock(&sessions_mutex
);
346 _lttng_channel_destroy(chan
);
347 mutex_unlock(&sessions_mutex
);
349 EXPORT_SYMBOL_GPL(lttng_metadata_channel_destroy
);
352 void _lttng_metadata_channel_hangup(struct lttng_metadata_stream
*stream
)
354 stream
->finalized
= 1;
355 wake_up_interruptible(&stream
->read_wait
);
359 * Supports event creation while tracing session is active.
361 struct lttng_event
*lttng_event_create(struct lttng_channel
*chan
,
362 struct lttng_kernel_event
*event_param
,
364 const struct lttng_event_desc
*internal_desc
)
366 struct lttng_event
*event
;
369 mutex_lock(&sessions_mutex
);
370 if (chan
->free_event_id
== -1U) {
375 * This is O(n^2) (for each event, the loop is called at event
376 * creation). Might require a hash if we have lots of events.
378 list_for_each_entry(event
, &chan
->session
->events
, list
) {
379 if (!strcmp(event
->desc
->name
, event_param
->name
)) {
384 event
= kmem_cache_zalloc(event_cache
, GFP_KERNEL
);
390 event
->filter
= filter
;
391 event
->id
= chan
->free_event_id
++;
393 event
->instrumentation
= event_param
->instrumentation
;
394 /* Populate lttng_event structure before tracepoint registration. */
396 switch (event_param
->instrumentation
) {
397 case LTTNG_KERNEL_TRACEPOINT
:
398 event
->desc
= lttng_event_get(event_param
->name
);
403 ret
= lttng_wrapper_tracepoint_probe_register(event
->desc
->kname
,
404 event
->desc
->probe_callback
,
411 case LTTNG_KERNEL_KPROBE
:
412 ret
= lttng_kprobes_register(event_param
->name
,
413 event_param
->u
.kprobe
.symbol_name
,
414 event_param
->u
.kprobe
.offset
,
415 event_param
->u
.kprobe
.addr
,
421 ret
= try_module_get(event
->desc
->owner
);
424 case LTTNG_KERNEL_KRETPROBE
:
426 struct lttng_event
*event_return
;
428 /* kretprobe defines 2 events */
430 kmem_cache_zalloc(event_cache
, GFP_KERNEL
);
435 event_return
->chan
= chan
;
436 event_return
->filter
= filter
;
437 event_return
->id
= chan
->free_event_id
++;
438 event_return
->enabled
= 1;
439 event_return
->instrumentation
= event_param
->instrumentation
;
441 * Populate lttng_event structure before kretprobe registration.
444 ret
= lttng_kretprobes_register(event_param
->name
,
445 event_param
->u
.kretprobe
.symbol_name
,
446 event_param
->u
.kretprobe
.offset
,
447 event_param
->u
.kretprobe
.addr
,
448 event
, event_return
);
450 kmem_cache_free(event_cache
, event_return
);
454 /* Take 2 refs on the module: one per event. */
455 ret
= try_module_get(event
->desc
->owner
);
457 ret
= try_module_get(event
->desc
->owner
);
459 ret
= _lttng_event_metadata_statedump(chan
->session
, chan
,
461 WARN_ON_ONCE(ret
> 0);
463 kmem_cache_free(event_cache
, event_return
);
464 module_put(event
->desc
->owner
);
465 module_put(event
->desc
->owner
);
466 goto statedump_error
;
468 list_add(&event_return
->list
, &chan
->session
->events
);
471 case LTTNG_KERNEL_FUNCTION
:
472 ret
= lttng_ftrace_register(event_param
->name
,
473 event_param
->u
.ftrace
.symbol_name
,
478 ret
= try_module_get(event
->desc
->owner
);
481 case LTTNG_KERNEL_NOOP
:
482 event
->desc
= internal_desc
;
493 ret
= _lttng_event_metadata_statedump(chan
->session
, chan
, event
);
494 WARN_ON_ONCE(ret
> 0);
496 goto statedump_error
;
498 list_add(&event
->list
, &chan
->session
->events
);
499 mutex_unlock(&sessions_mutex
);
503 /* If a statedump error occurs, events will not be readable. */
505 kmem_cache_free(event_cache
, event
);
509 mutex_unlock(&sessions_mutex
);
514 * Only used internally at session destruction.
516 int _lttng_event_unregister(struct lttng_event
*event
)
520 switch (event
->instrumentation
) {
521 case LTTNG_KERNEL_TRACEPOINT
:
522 ret
= lttng_wrapper_tracepoint_probe_unregister(event
->desc
->kname
,
523 event
->desc
->probe_callback
,
528 case LTTNG_KERNEL_KPROBE
:
529 lttng_kprobes_unregister(event
);
532 case LTTNG_KERNEL_KRETPROBE
:
533 lttng_kretprobes_unregister(event
);
536 case LTTNG_KERNEL_FUNCTION
:
537 lttng_ftrace_unregister(event
);
540 case LTTNG_KERNEL_NOOP
:
550 * Only used internally at session destruction.
553 void _lttng_event_destroy(struct lttng_event
*event
)
555 switch (event
->instrumentation
) {
556 case LTTNG_KERNEL_TRACEPOINT
:
557 lttng_event_put(event
->desc
);
559 case LTTNG_KERNEL_KPROBE
:
560 module_put(event
->desc
->owner
);
561 lttng_kprobes_destroy_private(event
);
563 case LTTNG_KERNEL_KRETPROBE
:
564 module_put(event
->desc
->owner
);
565 lttng_kretprobes_destroy_private(event
);
567 case LTTNG_KERNEL_FUNCTION
:
568 module_put(event
->desc
->owner
);
569 lttng_ftrace_destroy_private(event
);
571 case LTTNG_KERNEL_NOOP
:
576 list_del(&event
->list
);
577 lttng_destroy_context(event
->ctx
);
578 kmem_cache_free(event_cache
, event
);
582 * Serialize at most one packet worth of metadata into a metadata
584 * We have exclusive access to our metadata buffer (protected by the
585 * sessions_mutex), so we can do racy operations such as looking for
586 * remaining space left in packet and write, since mutual exclusion
587 * protects us from concurrent writes.
588 * Returns the number of bytes written in the channel, 0 if no data
589 * was written and a negative value on error.
591 int lttng_metadata_output_channel(struct lttng_metadata_stream
*stream
,
592 struct channel
*chan
)
594 struct lib_ring_buffer_ctx ctx
;
596 size_t len
, reserve_len
;
599 * Ensure we support mutiple get_next / put sequences followed
600 * by put_next. The metadata stream lock internally protects
601 * reading the metadata cache. It can indeed be read
602 * concurrently by "get_next_subbuf" and "flush" operations on
603 * the buffer invoked by different processes.
605 mutex_lock(&stream
->lock
);
606 WARN_ON(stream
->metadata_in
< stream
->metadata_out
);
607 if (stream
->metadata_in
!= stream
->metadata_out
)
610 len
= stream
->metadata_cache
->metadata_written
-
614 reserve_len
= min_t(size_t,
615 stream
->transport
->ops
.packet_avail_size(chan
),
617 lib_ring_buffer_ctx_init(&ctx
, chan
, NULL
, reserve_len
,
620 * If reservation failed, return an error to the caller.
622 ret
= stream
->transport
->ops
.event_reserve(&ctx
, 0);
624 printk(KERN_WARNING
"LTTng: Metadata event reservation failed\n");
627 stream
->transport
->ops
.event_write(&ctx
,
628 stream
->metadata_cache
->data
+ stream
->metadata_in
,
630 stream
->transport
->ops
.event_commit(&ctx
);
631 stream
->metadata_in
+= reserve_len
;
635 mutex_unlock(&stream
->lock
);
640 * Write the metadata to the metadata cache.
641 * Must be called with sessions_mutex held.
643 int lttng_metadata_printf(struct lttng_session
*session
,
644 const char *fmt
, ...)
649 struct lttng_metadata_stream
*stream
;
651 WARN_ON_ONCE(!ACCESS_ONCE(session
->active
));
654 str
= kvasprintf(GFP_KERNEL
, fmt
, ap
);
660 if (session
->metadata_cache
->metadata_written
+ len
>
661 session
->metadata_cache
->cache_alloc
) {
662 char *tmp_cache_realloc
;
663 unsigned int tmp_cache_alloc_size
;
665 tmp_cache_alloc_size
= max_t(unsigned int,
666 session
->metadata_cache
->cache_alloc
+ len
,
667 session
->metadata_cache
->cache_alloc
<< 1);
668 tmp_cache_realloc
= krealloc(session
->metadata_cache
->data
,
669 tmp_cache_alloc_size
, GFP_KERNEL
);
670 if (!tmp_cache_realloc
)
672 session
->metadata_cache
->cache_alloc
= tmp_cache_alloc_size
;
673 session
->metadata_cache
->data
= tmp_cache_realloc
;
675 memcpy(session
->metadata_cache
->data
+
676 session
->metadata_cache
->metadata_written
,
678 session
->metadata_cache
->metadata_written
+= len
;
681 list_for_each_entry(stream
, &session
->metadata_cache
->metadata_stream
, list
)
682 wake_up_interruptible(&stream
->read_wait
);
692 * Must be called with sessions_mutex held.
695 int _lttng_field_statedump(struct lttng_session
*session
,
696 const struct lttng_event_field
*field
)
700 switch (field
->type
.atype
) {
702 ret
= lttng_metadata_printf(session
,
703 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n",
704 field
->type
.u
.basic
.integer
.size
,
705 field
->type
.u
.basic
.integer
.alignment
,
706 field
->type
.u
.basic
.integer
.signedness
,
707 (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_none
)
709 : (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
712 field
->type
.u
.basic
.integer
.base
,
714 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
716 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
721 ret
= lttng_metadata_printf(session
,
723 field
->type
.u
.basic
.enumeration
.name
,
728 const struct lttng_basic_type
*elem_type
;
730 elem_type
= &field
->type
.u
.array
.elem_type
;
731 ret
= lttng_metadata_printf(session
,
732 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n",
733 elem_type
->u
.basic
.integer
.size
,
734 elem_type
->u
.basic
.integer
.alignment
,
735 elem_type
->u
.basic
.integer
.signedness
,
736 (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
738 : (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
741 elem_type
->u
.basic
.integer
.base
,
743 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
745 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
747 field
->name
, field
->type
.u
.array
.length
);
752 const struct lttng_basic_type
*elem_type
;
753 const struct lttng_basic_type
*length_type
;
755 elem_type
= &field
->type
.u
.sequence
.elem_type
;
756 length_type
= &field
->type
.u
.sequence
.length_type
;
757 ret
= lttng_metadata_printf(session
,
758 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n",
759 length_type
->u
.basic
.integer
.size
,
760 (unsigned int) length_type
->u
.basic
.integer
.alignment
,
761 length_type
->u
.basic
.integer
.signedness
,
762 (length_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
764 : ((length_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
767 length_type
->u
.basic
.integer
.base
,
769 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
771 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
777 ret
= lttng_metadata_printf(session
,
778 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n",
779 elem_type
->u
.basic
.integer
.size
,
780 (unsigned int) elem_type
->u
.basic
.integer
.alignment
,
781 elem_type
->u
.basic
.integer
.signedness
,
782 (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
784 : ((elem_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
787 elem_type
->u
.basic
.integer
.base
,
789 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
791 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
799 /* Default encoding is UTF8 */
800 ret
= lttng_metadata_printf(session
,
802 field
->type
.u
.basic
.string
.encoding
== lttng_encode_ASCII
?
803 " { encoding = ASCII; }" : "",
814 int _lttng_context_metadata_statedump(struct lttng_session
*session
,
815 struct lttng_ctx
*ctx
)
822 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
823 const struct lttng_ctx_field
*field
= &ctx
->fields
[i
];
825 ret
= _lttng_field_statedump(session
, &field
->event_field
);
833 int _lttng_fields_metadata_statedump(struct lttng_session
*session
,
834 struct lttng_event
*event
)
836 const struct lttng_event_desc
*desc
= event
->desc
;
840 for (i
= 0; i
< desc
->nr_fields
; i
++) {
841 const struct lttng_event_field
*field
= &desc
->fields
[i
];
843 ret
= _lttng_field_statedump(session
, field
);
851 * Must be called with sessions_mutex held.
854 int _lttng_event_metadata_statedump(struct lttng_session
*session
,
855 struct lttng_channel
*chan
,
856 struct lttng_event
*event
)
860 if (event
->metadata_dumped
|| !ACCESS_ONCE(session
->active
))
862 if (chan
->channel_type
== METADATA_CHANNEL
)
865 ret
= lttng_metadata_printf(session
,
869 " stream_id = %u;\n",
877 ret
= lttng_metadata_printf(session
,
878 " context := struct {\n");
882 ret
= _lttng_context_metadata_statedump(session
, event
->ctx
);
886 ret
= lttng_metadata_printf(session
,
892 ret
= lttng_metadata_printf(session
,
893 " fields := struct {\n"
898 ret
= _lttng_fields_metadata_statedump(session
, event
);
903 * LTTng space reservation can only reserve multiples of the
906 ret
= lttng_metadata_printf(session
,
912 event
->metadata_dumped
= 1;
919 * Must be called with sessions_mutex held.
922 int _lttng_channel_metadata_statedump(struct lttng_session
*session
,
923 struct lttng_channel
*chan
)
927 if (chan
->metadata_dumped
|| !ACCESS_ONCE(session
->active
))
930 if (chan
->channel_type
== METADATA_CHANNEL
)
933 WARN_ON_ONCE(!chan
->header_type
);
934 ret
= lttng_metadata_printf(session
,
937 " event.header := %s;\n"
938 " packet.context := struct packet_context;\n",
940 chan
->header_type
== 1 ? "struct event_header_compact" :
941 "struct event_header_large");
946 ret
= lttng_metadata_printf(session
,
947 " event.context := struct {\n");
951 ret
= _lttng_context_metadata_statedump(session
, chan
->ctx
);
955 ret
= lttng_metadata_printf(session
,
961 ret
= lttng_metadata_printf(session
,
964 chan
->metadata_dumped
= 1;
970 * Must be called with sessions_mutex held.
973 int _lttng_stream_packet_context_declare(struct lttng_session
*session
)
975 return lttng_metadata_printf(session
,
976 "struct packet_context {\n"
977 " uint64_clock_monotonic_t timestamp_begin;\n"
978 " uint64_clock_monotonic_t timestamp_end;\n"
979 " uint64_t content_size;\n"
980 " uint64_t packet_size;\n"
981 " unsigned long events_discarded;\n"
982 " uint32_t cpu_id;\n"
990 * id 31 is reserved to indicate an extended header.
993 * id: range: 0 - 65534.
994 * id 65535 is reserved to indicate an extended header.
996 * Must be called with sessions_mutex held.
999 int _lttng_event_header_declare(struct lttng_session
*session
)
1001 return lttng_metadata_printf(session
,
1002 "struct event_header_compact {\n"
1003 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
1006 " uint27_clock_monotonic_t timestamp;\n"
1010 " uint64_clock_monotonic_t timestamp;\n"
1015 "struct event_header_large {\n"
1016 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
1019 " uint32_clock_monotonic_t timestamp;\n"
1023 " uint64_clock_monotonic_t timestamp;\n"
1027 lttng_alignof(uint32_t) * CHAR_BIT
,
1028 lttng_alignof(uint16_t) * CHAR_BIT
1033 * Approximation of NTP time of day to clock monotonic correlation,
1034 * taken at start of trace.
1035 * Yes, this is only an approximation. Yes, we can (and will) do better
1036 * in future versions.
1039 uint64_t measure_clock_offset(void)
1041 uint64_t offset
, monotonic
[2], realtime
;
1042 struct timespec rts
= { 0, 0 };
1043 unsigned long flags
;
1045 /* Disable interrupts to increase correlation precision. */
1046 local_irq_save(flags
);
1047 monotonic
[0] = trace_clock_read64();
1048 getnstimeofday(&rts
);
1049 monotonic
[1] = trace_clock_read64();
1050 local_irq_restore(flags
);
1052 offset
= (monotonic
[0] + monotonic
[1]) >> 1;
1053 realtime
= (uint64_t) rts
.tv_sec
* NSEC_PER_SEC
;
1054 realtime
+= rts
.tv_nsec
;
1055 offset
= realtime
- offset
;
1060 * Output metadata into this session's metadata buffers.
1061 * Must be called with sessions_mutex held.
1064 int _lttng_session_metadata_statedump(struct lttng_session
*session
)
1066 unsigned char *uuid_c
= session
->uuid
.b
;
1067 unsigned char uuid_s
[37], clock_uuid_s
[BOOT_ID_LEN
];
1068 struct lttng_channel
*chan
;
1069 struct lttng_event
*event
;
1072 if (!ACCESS_ONCE(session
->active
))
1074 if (session
->metadata_dumped
)
1077 snprintf(uuid_s
, sizeof(uuid_s
),
1078 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
1079 uuid_c
[0], uuid_c
[1], uuid_c
[2], uuid_c
[3],
1080 uuid_c
[4], uuid_c
[5], uuid_c
[6], uuid_c
[7],
1081 uuid_c
[8], uuid_c
[9], uuid_c
[10], uuid_c
[11],
1082 uuid_c
[12], uuid_c
[13], uuid_c
[14], uuid_c
[15]);
1084 ret
= lttng_metadata_printf(session
,
1085 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
1086 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
1087 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
1088 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
1089 "typealias integer { size = %u; align = %u; signed = false; } := unsigned long;\n"
1090 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
1091 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
1097 " byte_order = %s;\n"
1098 " packet.header := struct {\n"
1099 " uint32_t magic;\n"
1100 " uint8_t uuid[16];\n"
1101 " uint32_t stream_id;\n"
1104 lttng_alignof(uint8_t) * CHAR_BIT
,
1105 lttng_alignof(uint16_t) * CHAR_BIT
,
1106 lttng_alignof(uint32_t) * CHAR_BIT
,
1107 lttng_alignof(uint64_t) * CHAR_BIT
,
1108 sizeof(unsigned long) * CHAR_BIT
,
1109 lttng_alignof(unsigned long) * CHAR_BIT
,
1122 ret
= lttng_metadata_printf(session
,
1124 " hostname = \"%s\";\n"
1125 " domain = \"kernel\";\n"
1126 " sysname = \"%s\";\n"
1127 " kernel_release = \"%s\";\n"
1128 " kernel_version = \"%s\";\n"
1129 " tracer_name = \"lttng-modules\";\n"
1130 " tracer_major = %d;\n"
1131 " tracer_minor = %d;\n"
1132 " tracer_patchlevel = %d;\n"
1134 current
->nsproxy
->uts_ns
->name
.nodename
,
1138 LTTNG_MODULES_MAJOR_VERSION
,
1139 LTTNG_MODULES_MINOR_VERSION
,
1140 LTTNG_MODULES_PATCHLEVEL_VERSION
1145 ret
= lttng_metadata_printf(session
,
1153 if (!trace_clock_uuid(clock_uuid_s
)) {
1154 ret
= lttng_metadata_printf(session
,
1155 " uuid = \"%s\";\n",
1162 ret
= lttng_metadata_printf(session
,
1163 " description = \"Monotonic Clock\";\n"
1164 " freq = %llu; /* Frequency, in Hz */\n"
1165 " /* clock value offset from Epoch is: offset * (1/freq) */\n"
1168 (unsigned long long) trace_clock_freq(),
1169 (unsigned long long) measure_clock_offset()
1174 ret
= lttng_metadata_printf(session
,
1175 "typealias integer {\n"
1176 " size = 27; align = 1; signed = false;\n"
1177 " map = clock.monotonic.value;\n"
1178 "} := uint27_clock_monotonic_t;\n"
1180 "typealias integer {\n"
1181 " size = 32; align = %u; signed = false;\n"
1182 " map = clock.monotonic.value;\n"
1183 "} := uint32_clock_monotonic_t;\n"
1185 "typealias integer {\n"
1186 " size = 64; align = %u; signed = false;\n"
1187 " map = clock.monotonic.value;\n"
1188 "} := uint64_clock_monotonic_t;\n\n",
1189 lttng_alignof(uint32_t) * CHAR_BIT
,
1190 lttng_alignof(uint64_t) * CHAR_BIT
1195 ret
= _lttng_stream_packet_context_declare(session
);
1199 ret
= _lttng_event_header_declare(session
);
1204 list_for_each_entry(chan
, &session
->chan
, list
) {
1205 ret
= _lttng_channel_metadata_statedump(session
, chan
);
1210 list_for_each_entry(event
, &session
->events
, list
) {
1211 ret
= _lttng_event_metadata_statedump(session
, event
->chan
, event
);
1215 session
->metadata_dumped
= 1;
1221 * lttng_transport_register - LTT transport registration
1222 * @transport: transport structure
1224 * Registers a transport which can be used as output to extract the data out of
1225 * LTTng. The module calling this registration function must ensure that no
1226 * trap-inducing code will be executed by the transport functions. E.g.
1227 * vmalloc_sync_all() must be called between a vmalloc and the moment the memory
1228 * is made visible to the transport function. This registration acts as a
1229 * vmalloc_sync_all. Therefore, only if the module allocates virtual memory
1230 * after its registration must it synchronize the TLBs.
1232 void lttng_transport_register(struct lttng_transport
*transport
)
1235 * Make sure no page fault can be triggered by the module about to be
1236 * registered. We deal with this here so we don't have to call
1237 * vmalloc_sync_all() in each module's init.
1239 wrapper_vmalloc_sync_all();
1241 mutex_lock(&sessions_mutex
);
1242 list_add_tail(&transport
->node
, <tng_transport_list
);
1243 mutex_unlock(&sessions_mutex
);
1245 EXPORT_SYMBOL_GPL(lttng_transport_register
);
1248 * lttng_transport_unregister - LTT transport unregistration
1249 * @transport: transport structure
1251 void lttng_transport_unregister(struct lttng_transport
*transport
)
1253 mutex_lock(&sessions_mutex
);
1254 list_del(&transport
->node
);
1255 mutex_unlock(&sessions_mutex
);
1257 EXPORT_SYMBOL_GPL(lttng_transport_unregister
);
1259 static int __init
lttng_events_init(void)
1263 ret
= wrapper_lttng_fixup_sig(THIS_MODULE
);
1267 ret
= lttng_tracepoint_init();
1270 event_cache
= KMEM_CACHE(lttng_event
, 0);
1275 ret
= lttng_abi_init();
1278 ret
= lttng_logger_init();
1286 kmem_cache_destroy(event_cache
);
1288 lttng_tracepoint_exit();
1292 module_init(lttng_events_init
);
1294 static void __exit
lttng_events_exit(void)
1296 struct lttng_session
*session
, *tmpsession
;
1298 lttng_logger_exit();
1300 list_for_each_entry_safe(session
, tmpsession
, &sessions
, list
)
1301 lttng_session_destroy(session
);
1302 kmem_cache_destroy(event_cache
);
1303 lttng_tracepoint_exit();
1306 module_exit(lttng_events_exit
);
1308 MODULE_LICENSE("GPL and additional rights");
1309 MODULE_AUTHOR("Mathieu Desnoyers <mathieu.desnoyers@efficios.com>");
1310 MODULE_DESCRIPTION("LTTng Events");
1311 MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION
) "."
1312 __stringify(LTTNG_MODULES_MINOR_VERSION
) "."
1313 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION
)
1314 LTTNG_MODULES_EXTRAVERSION
);