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
24 * This page_alloc.h wrapper needs to be included before gfpflags.h because it
25 * overrides a function with a define.
27 #include "wrapper/page_alloc.h"
29 #include <linux/module.h>
30 #include <linux/list.h>
31 #include <linux/mutex.h>
32 #include <linux/sched.h>
33 #include <linux/slab.h>
34 #include <linux/jiffies.h>
35 #include <linux/utsname.h>
36 #include <linux/err.h>
37 #include <linux/vmalloc.h>
39 #include "wrapper/uuid.h"
40 #include "wrapper/vmalloc.h" /* for wrapper_vmalloc_sync_all() */
41 #include "wrapper/random.h"
42 #include "wrapper/tracepoint.h"
43 #include "lttng-kernel-version.h"
44 #include "lttng-events.h"
45 #include "lttng-tracer.h"
46 #include "lttng-abi-old.h"
47 #include "wrapper/vzalloc.h"
48 #include "wrapper/ringbuffer/backend.h"
49 #include "wrapper/ringbuffer/frontend.h"
51 #define METADATA_CACHE_DEFAULT_SIZE 4096
53 static LIST_HEAD(sessions
);
54 static LIST_HEAD(lttng_transport_list
);
56 * Protect the sessions and metadata caches.
58 static DEFINE_MUTEX(sessions_mutex
);
59 static struct kmem_cache
*event_cache
;
61 static void _lttng_event_destroy(struct lttng_event
*event
);
62 static void _lttng_channel_destroy(struct lttng_channel
*chan
);
63 static int _lttng_event_unregister(struct lttng_event
*event
);
65 int _lttng_event_metadata_statedump(struct lttng_session
*session
,
66 struct lttng_channel
*chan
,
67 struct lttng_event
*event
);
69 int _lttng_session_metadata_statedump(struct lttng_session
*session
);
71 void _lttng_metadata_channel_hangup(struct lttng_metadata_stream
*stream
);
73 void synchronize_trace(void)
76 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0))
77 #ifdef CONFIG_PREEMPT_RT_FULL
80 #else /* (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)) */
81 #ifdef CONFIG_PREEMPT_RT
84 #endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)) */
87 struct lttng_session
*lttng_session_create(void)
89 struct lttng_session
*session
;
90 struct lttng_metadata_cache
*metadata_cache
;
92 mutex_lock(&sessions_mutex
);
93 session
= kzalloc(sizeof(struct lttng_session
), GFP_KERNEL
);
96 INIT_LIST_HEAD(&session
->chan
);
97 INIT_LIST_HEAD(&session
->events
);
98 uuid_le_gen(&session
->uuid
);
100 metadata_cache
= kzalloc(sizeof(struct lttng_metadata_cache
),
103 goto err_free_session
;
104 metadata_cache
->data
= lttng_vzalloc(METADATA_CACHE_DEFAULT_SIZE
);
105 if (!metadata_cache
->data
)
107 metadata_cache
->cache_alloc
= METADATA_CACHE_DEFAULT_SIZE
;
108 kref_init(&metadata_cache
->refcount
);
109 mutex_init(&metadata_cache
->lock
);
110 session
->metadata_cache
= metadata_cache
;
111 INIT_LIST_HEAD(&metadata_cache
->metadata_stream
);
112 memcpy(&metadata_cache
->uuid
, &session
->uuid
,
113 sizeof(metadata_cache
->uuid
));
114 list_add(&session
->list
, &sessions
);
115 mutex_unlock(&sessions_mutex
);
119 kfree(metadata_cache
);
123 mutex_unlock(&sessions_mutex
);
127 void metadata_cache_destroy(struct kref
*kref
)
129 struct lttng_metadata_cache
*cache
=
130 container_of(kref
, struct lttng_metadata_cache
, refcount
);
135 void lttng_session_destroy(struct lttng_session
*session
)
137 struct lttng_channel
*chan
, *tmpchan
;
138 struct lttng_event
*event
, *tmpevent
;
139 struct lttng_metadata_stream
*metadata_stream
;
142 mutex_lock(&sessions_mutex
);
143 ACCESS_ONCE(session
->active
) = 0;
144 list_for_each_entry(chan
, &session
->chan
, list
) {
145 ret
= lttng_syscalls_unregister(chan
);
148 list_for_each_entry(event
, &session
->events
, list
) {
149 ret
= _lttng_event_unregister(event
);
152 synchronize_trace(); /* Wait for in-flight events to complete */
153 list_for_each_entry_safe(event
, tmpevent
, &session
->events
, list
)
154 _lttng_event_destroy(event
);
155 list_for_each_entry_safe(chan
, tmpchan
, &session
->chan
, list
) {
156 BUG_ON(chan
->channel_type
== METADATA_CHANNEL
);
157 _lttng_channel_destroy(chan
);
159 list_for_each_entry(metadata_stream
, &session
->metadata_cache
->metadata_stream
, list
)
160 _lttng_metadata_channel_hangup(metadata_stream
);
161 kref_put(&session
->metadata_cache
->refcount
, metadata_cache_destroy
);
162 list_del(&session
->list
);
163 mutex_unlock(&sessions_mutex
);
167 int lttng_session_enable(struct lttng_session
*session
)
170 struct lttng_channel
*chan
;
172 mutex_lock(&sessions_mutex
);
173 if (session
->active
) {
179 * Snapshot the number of events per channel to know the type of header
182 list_for_each_entry(chan
, &session
->chan
, list
) {
183 if (chan
->header_type
)
184 continue; /* don't change it if session stop/restart */
185 if (chan
->free_event_id
< 31)
186 chan
->header_type
= 1; /* compact */
188 chan
->header_type
= 2; /* large */
191 /* Clear each stream's quiescent state. */
192 list_for_each_entry(chan
, &session
->chan
, list
) {
193 if (chan
->channel_type
!= METADATA_CHANNEL
)
194 lib_ring_buffer_clear_quiescent_channel(chan
->chan
);
197 ACCESS_ONCE(session
->active
) = 1;
198 ACCESS_ONCE(session
->been_active
) = 1;
199 ret
= _lttng_session_metadata_statedump(session
);
201 ACCESS_ONCE(session
->active
) = 0;
204 ret
= lttng_statedump_start(session
);
206 ACCESS_ONCE(session
->active
) = 0;
208 mutex_unlock(&sessions_mutex
);
212 int lttng_session_disable(struct lttng_session
*session
)
215 struct lttng_channel
*chan
;
217 mutex_lock(&sessions_mutex
);
218 if (!session
->active
) {
222 ACCESS_ONCE(session
->active
) = 0;
224 /* Set each stream's quiescent state. */
225 list_for_each_entry(chan
, &session
->chan
, list
) {
226 if (chan
->channel_type
!= METADATA_CHANNEL
)
227 lib_ring_buffer_set_quiescent_channel(chan
->chan
);
231 mutex_unlock(&sessions_mutex
);
235 int lttng_channel_enable(struct lttng_channel
*channel
)
239 if (channel
->channel_type
== METADATA_CHANNEL
)
241 old
= xchg(&channel
->enabled
, 1);
247 int lttng_channel_disable(struct lttng_channel
*channel
)
251 if (channel
->channel_type
== METADATA_CHANNEL
)
253 old
= xchg(&channel
->enabled
, 0);
259 int lttng_event_enable(struct lttng_event
*event
)
263 if (event
->chan
->channel_type
== METADATA_CHANNEL
)
265 old
= xchg(&event
->enabled
, 1);
271 int lttng_event_disable(struct lttng_event
*event
)
275 if (event
->chan
->channel_type
== METADATA_CHANNEL
)
277 old
= xchg(&event
->enabled
, 0);
283 static struct lttng_transport
*lttng_transport_find(const char *name
)
285 struct lttng_transport
*transport
;
287 list_for_each_entry(transport
, <tng_transport_list
, node
) {
288 if (!strcmp(transport
->name
, name
))
294 struct lttng_channel
*lttng_channel_create(struct lttng_session
*session
,
295 const char *transport_name
,
297 size_t subbuf_size
, size_t num_subbuf
,
298 unsigned int switch_timer_interval
,
299 unsigned int read_timer_interval
,
300 enum channel_type channel_type
)
302 struct lttng_channel
*chan
;
303 struct lttng_transport
*transport
= NULL
;
305 mutex_lock(&sessions_mutex
);
306 if (session
->been_active
&& channel_type
!= METADATA_CHANNEL
)
307 goto active
; /* Refuse to add channel to active session */
308 transport
= lttng_transport_find(transport_name
);
310 printk(KERN_WARNING
"LTTng transport %s not found\n",
314 if (!try_module_get(transport
->owner
)) {
315 printk(KERN_WARNING
"LTT : Can't lock transport module.\n");
318 chan
= kzalloc(sizeof(struct lttng_channel
), GFP_KERNEL
);
321 chan
->session
= session
;
322 chan
->id
= session
->free_chan_id
++;
323 chan
->ops
= &transport
->ops
;
325 * Note: the channel creation op already writes into the packet
326 * headers. Therefore the "chan" information used as input
327 * should be already accessible.
329 chan
->chan
= transport
->ops
.channel_create(transport_name
,
330 chan
, buf_addr
, subbuf_size
, num_subbuf
,
331 switch_timer_interval
, read_timer_interval
);
335 chan
->transport
= transport
;
336 chan
->channel_type
= channel_type
;
337 list_add(&chan
->list
, &session
->chan
);
338 mutex_unlock(&sessions_mutex
);
345 module_put(transport
->owner
);
348 mutex_unlock(&sessions_mutex
);
353 * Only used internally at session destruction for per-cpu channels, and
354 * when metadata channel is released.
355 * Needs to be called with sessions mutex held.
358 void _lttng_channel_destroy(struct lttng_channel
*chan
)
360 chan
->ops
->channel_destroy(chan
->chan
);
361 module_put(chan
->transport
->owner
);
362 list_del(&chan
->list
);
363 lttng_destroy_context(chan
->ctx
);
367 void lttng_metadata_channel_destroy(struct lttng_channel
*chan
)
369 BUG_ON(chan
->channel_type
!= METADATA_CHANNEL
);
371 /* Protect the metadata cache with the sessions_mutex. */
372 mutex_lock(&sessions_mutex
);
373 _lttng_channel_destroy(chan
);
374 mutex_unlock(&sessions_mutex
);
376 EXPORT_SYMBOL_GPL(lttng_metadata_channel_destroy
);
379 void _lttng_metadata_channel_hangup(struct lttng_metadata_stream
*stream
)
381 stream
->finalized
= 1;
382 wake_up_interruptible(&stream
->read_wait
);
386 * Supports event creation while tracing session is active.
388 struct lttng_event
*lttng_event_create(struct lttng_channel
*chan
,
389 struct lttng_kernel_event
*event_param
,
391 const struct lttng_event_desc
*internal_desc
)
393 struct lttng_event
*event
;
396 mutex_lock(&sessions_mutex
);
397 if (chan
->free_event_id
== -1U) {
402 * This is O(n^2) (for each event, the loop is called at event
403 * creation). Might require a hash if we have lots of events.
405 list_for_each_entry(event
, &chan
->session
->events
, list
) {
406 if (!strcmp(event
->desc
->name
, event_param
->name
)) {
408 * Allow events with the same name to appear in
409 * different channels.
411 if (event
->chan
== chan
) {
417 event
= kmem_cache_zalloc(event_cache
, GFP_KERNEL
);
423 event
->filter
= filter
;
424 event
->id
= chan
->free_event_id
++;
426 event
->instrumentation
= event_param
->instrumentation
;
427 /* Populate lttng_event structure before tracepoint registration. */
429 switch (event_param
->instrumentation
) {
430 case LTTNG_KERNEL_TRACEPOINT
:
431 event
->desc
= lttng_event_get(event_param
->name
);
436 ret
= lttng_wrapper_tracepoint_probe_register(event
->desc
->kname
,
437 event
->desc
->probe_callback
,
444 case LTTNG_KERNEL_KPROBE
:
445 ret
= lttng_kprobes_register(event_param
->name
,
446 event_param
->u
.kprobe
.symbol_name
,
447 event_param
->u
.kprobe
.offset
,
448 event_param
->u
.kprobe
.addr
,
454 ret
= try_module_get(event
->desc
->owner
);
457 case LTTNG_KERNEL_KRETPROBE
:
459 struct lttng_event
*event_return
;
461 /* kretprobe defines 2 events */
463 kmem_cache_zalloc(event_cache
, GFP_KERNEL
);
468 event_return
->chan
= chan
;
469 event_return
->filter
= filter
;
470 event_return
->id
= chan
->free_event_id
++;
471 event_return
->enabled
= 1;
472 event_return
->instrumentation
= event_param
->instrumentation
;
474 * Populate lttng_event structure before kretprobe registration.
477 ret
= lttng_kretprobes_register(event_param
->name
,
478 event_param
->u
.kretprobe
.symbol_name
,
479 event_param
->u
.kretprobe
.offset
,
480 event_param
->u
.kretprobe
.addr
,
481 event
, event_return
);
483 kmem_cache_free(event_cache
, event_return
);
487 /* Take 2 refs on the module: one per event. */
488 ret
= try_module_get(event
->desc
->owner
);
490 ret
= try_module_get(event
->desc
->owner
);
492 ret
= _lttng_event_metadata_statedump(chan
->session
, chan
,
494 WARN_ON_ONCE(ret
> 0);
496 kmem_cache_free(event_cache
, event_return
);
497 module_put(event
->desc
->owner
);
498 module_put(event
->desc
->owner
);
499 goto statedump_error
;
501 list_add(&event_return
->list
, &chan
->session
->events
);
504 case LTTNG_KERNEL_FUNCTION
:
505 ret
= lttng_ftrace_register(event_param
->name
,
506 event_param
->u
.ftrace
.symbol_name
,
511 ret
= try_module_get(event
->desc
->owner
);
514 case LTTNG_KERNEL_NOOP
:
515 event
->desc
= internal_desc
;
526 ret
= _lttng_event_metadata_statedump(chan
->session
, chan
, event
);
527 WARN_ON_ONCE(ret
> 0);
529 goto statedump_error
;
531 list_add(&event
->list
, &chan
->session
->events
);
532 mutex_unlock(&sessions_mutex
);
536 /* If a statedump error occurs, events will not be readable. */
538 kmem_cache_free(event_cache
, event
);
542 mutex_unlock(&sessions_mutex
);
547 * Only used internally at session destruction.
549 int _lttng_event_unregister(struct lttng_event
*event
)
553 switch (event
->instrumentation
) {
554 case LTTNG_KERNEL_TRACEPOINT
:
555 ret
= lttng_wrapper_tracepoint_probe_unregister(event
->desc
->kname
,
556 event
->desc
->probe_callback
,
561 case LTTNG_KERNEL_KPROBE
:
562 lttng_kprobes_unregister(event
);
565 case LTTNG_KERNEL_KRETPROBE
:
566 lttng_kretprobes_unregister(event
);
569 case LTTNG_KERNEL_FUNCTION
:
570 lttng_ftrace_unregister(event
);
573 case LTTNG_KERNEL_NOOP
:
583 * Only used internally at session destruction.
586 void _lttng_event_destroy(struct lttng_event
*event
)
588 switch (event
->instrumentation
) {
589 case LTTNG_KERNEL_TRACEPOINT
:
590 lttng_event_put(event
->desc
);
592 case LTTNG_KERNEL_KPROBE
:
593 module_put(event
->desc
->owner
);
594 lttng_kprobes_destroy_private(event
);
596 case LTTNG_KERNEL_KRETPROBE
:
597 module_put(event
->desc
->owner
);
598 lttng_kretprobes_destroy_private(event
);
600 case LTTNG_KERNEL_FUNCTION
:
601 module_put(event
->desc
->owner
);
602 lttng_ftrace_destroy_private(event
);
604 case LTTNG_KERNEL_NOOP
:
609 list_del(&event
->list
);
610 lttng_destroy_context(event
->ctx
);
611 kmem_cache_free(event_cache
, event
);
615 * Serialize at most one packet worth of metadata into a metadata
617 * We grab the metadata cache mutex to get exclusive access to our metadata
618 * buffer and to the metadata cache. Exclusive access to the metadata buffer
619 * allows us to do racy operations such as looking for remaining space left in
620 * packet and write, since mutual exclusion protects us from concurrent writes.
621 * Mutual exclusion on the metadata cache allow us to read the cache content
622 * without racing against reallocation of the cache by updates.
623 * Returns the number of bytes written in the channel, 0 if no data
624 * was written and a negative value on error.
626 int lttng_metadata_output_channel(struct lttng_metadata_stream
*stream
,
627 struct channel
*chan
)
629 struct lib_ring_buffer_ctx ctx
;
631 size_t len
, reserve_len
;
634 * Ensure we support mutiple get_next / put sequences followed by
635 * put_next. The metadata cache lock protects reading the metadata
636 * cache. It can indeed be read concurrently by "get_next_subbuf" and
637 * "flush" operations on the buffer invoked by different processes.
638 * Moreover, since the metadata cache memory can be reallocated, we
639 * need to have exclusive access against updates even though we only
642 mutex_lock(&stream
->metadata_cache
->lock
);
643 WARN_ON(stream
->metadata_in
< stream
->metadata_out
);
644 if (stream
->metadata_in
!= stream
->metadata_out
)
647 len
= stream
->metadata_cache
->metadata_written
-
651 reserve_len
= min_t(size_t,
652 stream
->transport
->ops
.packet_avail_size(chan
),
654 lib_ring_buffer_ctx_init(&ctx
, chan
, NULL
, reserve_len
,
657 * If reservation failed, return an error to the caller.
659 ret
= stream
->transport
->ops
.event_reserve(&ctx
, 0);
661 printk(KERN_WARNING
"LTTng: Metadata event reservation failed\n");
664 stream
->transport
->ops
.event_write(&ctx
,
665 stream
->metadata_cache
->data
+ stream
->metadata_in
,
667 stream
->transport
->ops
.event_commit(&ctx
);
668 stream
->metadata_in
+= reserve_len
;
672 mutex_unlock(&stream
->metadata_cache
->lock
);
677 * Write the metadata to the metadata cache.
678 * Must be called with sessions_mutex held.
679 * The metadata cache lock protects us from concurrent read access from
680 * thread outputting metadata content to ring buffer.
682 int lttng_metadata_printf(struct lttng_session
*session
,
683 const char *fmt
, ...)
688 struct lttng_metadata_stream
*stream
;
690 WARN_ON_ONCE(!ACCESS_ONCE(session
->active
));
693 str
= kvasprintf(GFP_KERNEL
, fmt
, ap
);
699 mutex_lock(&session
->metadata_cache
->lock
);
700 if (session
->metadata_cache
->metadata_written
+ len
>
701 session
->metadata_cache
->cache_alloc
) {
702 char *tmp_cache_realloc
;
703 unsigned int tmp_cache_alloc_size
;
705 tmp_cache_alloc_size
= max_t(unsigned int,
706 session
->metadata_cache
->cache_alloc
+ len
,
707 session
->metadata_cache
->cache_alloc
<< 1);
708 tmp_cache_realloc
= lttng_vzalloc(tmp_cache_alloc_size
);
709 if (!tmp_cache_realloc
)
711 if (session
->metadata_cache
->data
) {
712 memcpy(tmp_cache_realloc
,
713 session
->metadata_cache
->data
,
714 session
->metadata_cache
->cache_alloc
);
715 vfree(session
->metadata_cache
->data
);
718 session
->metadata_cache
->cache_alloc
= tmp_cache_alloc_size
;
719 session
->metadata_cache
->data
= tmp_cache_realloc
;
721 memcpy(session
->metadata_cache
->data
+
722 session
->metadata_cache
->metadata_written
,
724 session
->metadata_cache
->metadata_written
+= len
;
725 mutex_unlock(&session
->metadata_cache
->lock
);
728 list_for_each_entry(stream
, &session
->metadata_cache
->metadata_stream
, list
)
729 wake_up_interruptible(&stream
->read_wait
);
734 mutex_unlock(&session
->metadata_cache
->lock
);
740 * Must be called with sessions_mutex held.
743 int _lttng_field_statedump(struct lttng_session
*session
,
744 const struct lttng_event_field
*field
)
748 switch (field
->type
.atype
) {
750 ret
= lttng_metadata_printf(session
,
751 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n",
752 field
->type
.u
.basic
.integer
.size
,
753 field
->type
.u
.basic
.integer
.alignment
,
754 field
->type
.u
.basic
.integer
.signedness
,
755 (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_none
)
757 : (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
760 field
->type
.u
.basic
.integer
.base
,
762 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
764 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
769 ret
= lttng_metadata_printf(session
,
771 field
->type
.u
.basic
.enumeration
.name
,
776 const struct lttng_basic_type
*elem_type
;
778 elem_type
= &field
->type
.u
.array
.elem_type
;
779 ret
= lttng_metadata_printf(session
,
780 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n",
781 elem_type
->u
.basic
.integer
.size
,
782 elem_type
->u
.basic
.integer
.alignment
,
783 elem_type
->u
.basic
.integer
.signedness
,
784 (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
786 : (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
789 elem_type
->u
.basic
.integer
.base
,
791 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
793 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
795 field
->name
, field
->type
.u
.array
.length
);
800 const struct lttng_basic_type
*elem_type
;
801 const struct lttng_basic_type
*length_type
;
803 elem_type
= &field
->type
.u
.sequence
.elem_type
;
804 length_type
= &field
->type
.u
.sequence
.length_type
;
805 ret
= lttng_metadata_printf(session
,
806 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n",
807 length_type
->u
.basic
.integer
.size
,
808 (unsigned int) length_type
->u
.basic
.integer
.alignment
,
809 length_type
->u
.basic
.integer
.signedness
,
810 (length_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
812 : ((length_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
815 length_type
->u
.basic
.integer
.base
,
817 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
819 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
825 ret
= lttng_metadata_printf(session
,
826 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n",
827 elem_type
->u
.basic
.integer
.size
,
828 (unsigned int) elem_type
->u
.basic
.integer
.alignment
,
829 elem_type
->u
.basic
.integer
.signedness
,
830 (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
832 : ((elem_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
835 elem_type
->u
.basic
.integer
.base
,
837 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
839 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
847 /* Default encoding is UTF8 */
848 ret
= lttng_metadata_printf(session
,
850 field
->type
.u
.basic
.string
.encoding
== lttng_encode_ASCII
?
851 " { encoding = ASCII; }" : "",
862 int _lttng_context_metadata_statedump(struct lttng_session
*session
,
863 struct lttng_ctx
*ctx
)
870 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
871 const struct lttng_ctx_field
*field
= &ctx
->fields
[i
];
873 ret
= _lttng_field_statedump(session
, &field
->event_field
);
881 int _lttng_fields_metadata_statedump(struct lttng_session
*session
,
882 struct lttng_event
*event
)
884 const struct lttng_event_desc
*desc
= event
->desc
;
888 for (i
= 0; i
< desc
->nr_fields
; i
++) {
889 const struct lttng_event_field
*field
= &desc
->fields
[i
];
891 ret
= _lttng_field_statedump(session
, field
);
899 * Must be called with sessions_mutex held.
902 int _lttng_event_metadata_statedump(struct lttng_session
*session
,
903 struct lttng_channel
*chan
,
904 struct lttng_event
*event
)
908 if (event
->metadata_dumped
|| !ACCESS_ONCE(session
->active
))
910 if (chan
->channel_type
== METADATA_CHANNEL
)
913 ret
= lttng_metadata_printf(session
,
917 " stream_id = %u;\n",
925 ret
= lttng_metadata_printf(session
,
926 " context := struct {\n");
930 ret
= _lttng_context_metadata_statedump(session
, event
->ctx
);
934 ret
= lttng_metadata_printf(session
,
940 ret
= lttng_metadata_printf(session
,
941 " fields := struct {\n"
946 ret
= _lttng_fields_metadata_statedump(session
, event
);
951 * LTTng space reservation can only reserve multiples of the
954 ret
= lttng_metadata_printf(session
,
960 event
->metadata_dumped
= 1;
967 * Must be called with sessions_mutex held.
970 int _lttng_channel_metadata_statedump(struct lttng_session
*session
,
971 struct lttng_channel
*chan
)
975 if (chan
->metadata_dumped
|| !ACCESS_ONCE(session
->active
))
978 if (chan
->channel_type
== METADATA_CHANNEL
)
981 WARN_ON_ONCE(!chan
->header_type
);
982 ret
= lttng_metadata_printf(session
,
985 " event.header := %s;\n"
986 " packet.context := struct packet_context;\n",
988 chan
->header_type
== 1 ? "struct event_header_compact" :
989 "struct event_header_large");
994 ret
= lttng_metadata_printf(session
,
995 " event.context := struct {\n");
999 ret
= _lttng_context_metadata_statedump(session
, chan
->ctx
);
1003 ret
= lttng_metadata_printf(session
,
1009 ret
= lttng_metadata_printf(session
,
1012 chan
->metadata_dumped
= 1;
1018 * Must be called with sessions_mutex held.
1021 int _lttng_stream_packet_context_declare(struct lttng_session
*session
)
1023 return lttng_metadata_printf(session
,
1024 "struct packet_context {\n"
1025 " uint64_clock_monotonic_t timestamp_begin;\n"
1026 " uint64_clock_monotonic_t timestamp_end;\n"
1027 " uint64_t content_size;\n"
1028 " uint64_t packet_size;\n"
1029 " unsigned long events_discarded;\n"
1030 " uint32_t cpu_id;\n"
1037 * id: range: 0 - 30.
1038 * id 31 is reserved to indicate an extended header.
1041 * id: range: 0 - 65534.
1042 * id 65535 is reserved to indicate an extended header.
1044 * Must be called with sessions_mutex held.
1047 int _lttng_event_header_declare(struct lttng_session
*session
)
1049 return lttng_metadata_printf(session
,
1050 "struct event_header_compact {\n"
1051 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
1054 " uint27_clock_monotonic_t timestamp;\n"
1058 " uint64_clock_monotonic_t timestamp;\n"
1063 "struct event_header_large {\n"
1064 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
1067 " uint32_clock_monotonic_t timestamp;\n"
1071 " uint64_clock_monotonic_t timestamp;\n"
1075 lttng_alignof(uint32_t) * CHAR_BIT
,
1076 lttng_alignof(uint16_t) * CHAR_BIT
1081 * Approximation of NTP time of day to clock monotonic correlation,
1082 * taken at start of trace.
1083 * Yes, this is only an approximation. Yes, we can (and will) do better
1084 * in future versions.
1087 uint64_t measure_clock_offset(void)
1089 uint64_t offset
, monotonic
[2], realtime
;
1090 struct timespec rts
= { 0, 0 };
1091 unsigned long flags
;
1093 /* Disable interrupts to increase correlation precision. */
1094 local_irq_save(flags
);
1095 monotonic
[0] = trace_clock_read64();
1096 getnstimeofday(&rts
);
1097 monotonic
[1] = trace_clock_read64();
1098 local_irq_restore(flags
);
1100 offset
= (monotonic
[0] + monotonic
[1]) >> 1;
1101 realtime
= (uint64_t) rts
.tv_sec
* NSEC_PER_SEC
;
1102 realtime
+= rts
.tv_nsec
;
1103 offset
= realtime
- offset
;
1108 * Output metadata into this session's metadata buffers.
1109 * Must be called with sessions_mutex held.
1112 int _lttng_session_metadata_statedump(struct lttng_session
*session
)
1114 unsigned char *uuid_c
= session
->uuid
.b
;
1115 unsigned char uuid_s
[37], clock_uuid_s
[BOOT_ID_LEN
];
1116 struct lttng_channel
*chan
;
1117 struct lttng_event
*event
;
1120 if (!ACCESS_ONCE(session
->active
))
1122 if (session
->metadata_dumped
)
1125 snprintf(uuid_s
, sizeof(uuid_s
),
1126 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
1127 uuid_c
[0], uuid_c
[1], uuid_c
[2], uuid_c
[3],
1128 uuid_c
[4], uuid_c
[5], uuid_c
[6], uuid_c
[7],
1129 uuid_c
[8], uuid_c
[9], uuid_c
[10], uuid_c
[11],
1130 uuid_c
[12], uuid_c
[13], uuid_c
[14], uuid_c
[15]);
1132 ret
= lttng_metadata_printf(session
,
1133 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
1134 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
1135 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
1136 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
1137 "typealias integer { size = %u; align = %u; signed = false; } := unsigned long;\n"
1138 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
1139 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
1145 " byte_order = %s;\n"
1146 " packet.header := struct {\n"
1147 " uint32_t magic;\n"
1148 " uint8_t uuid[16];\n"
1149 " uint32_t stream_id;\n"
1152 lttng_alignof(uint8_t) * CHAR_BIT
,
1153 lttng_alignof(uint16_t) * CHAR_BIT
,
1154 lttng_alignof(uint32_t) * CHAR_BIT
,
1155 lttng_alignof(uint64_t) * CHAR_BIT
,
1156 sizeof(unsigned long) * CHAR_BIT
,
1157 lttng_alignof(unsigned long) * CHAR_BIT
,
1170 ret
= lttng_metadata_printf(session
,
1172 " hostname = \"%s\";\n"
1173 " domain = \"kernel\";\n"
1174 " sysname = \"%s\";\n"
1175 " kernel_release = \"%s\";\n"
1176 " kernel_version = \"%s\";\n"
1177 " tracer_name = \"lttng-modules\";\n"
1178 " tracer_major = %d;\n"
1179 " tracer_minor = %d;\n"
1180 " tracer_patchlevel = %d;\n"
1182 current
->nsproxy
->uts_ns
->name
.nodename
,
1186 LTTNG_MODULES_MAJOR_VERSION
,
1187 LTTNG_MODULES_MINOR_VERSION
,
1188 LTTNG_MODULES_PATCHLEVEL_VERSION
1193 ret
= lttng_metadata_printf(session
,
1201 if (!trace_clock_uuid(clock_uuid_s
)) {
1202 ret
= lttng_metadata_printf(session
,
1203 " uuid = \"%s\";\n",
1210 ret
= lttng_metadata_printf(session
,
1211 " description = \"Monotonic Clock\";\n"
1212 " freq = %llu; /* Frequency, in Hz */\n"
1213 " /* clock value offset from Epoch is: offset * (1/freq) */\n"
1216 (unsigned long long) trace_clock_freq(),
1217 (unsigned long long) measure_clock_offset()
1222 ret
= lttng_metadata_printf(session
,
1223 "typealias integer {\n"
1224 " size = 27; align = 1; signed = false;\n"
1225 " map = clock.monotonic.value;\n"
1226 "} := uint27_clock_monotonic_t;\n"
1228 "typealias integer {\n"
1229 " size = 32; align = %u; signed = false;\n"
1230 " map = clock.monotonic.value;\n"
1231 "} := uint32_clock_monotonic_t;\n"
1233 "typealias integer {\n"
1234 " size = 64; align = %u; signed = false;\n"
1235 " map = clock.monotonic.value;\n"
1236 "} := uint64_clock_monotonic_t;\n\n",
1237 lttng_alignof(uint32_t) * CHAR_BIT
,
1238 lttng_alignof(uint64_t) * CHAR_BIT
1243 ret
= _lttng_stream_packet_context_declare(session
);
1247 ret
= _lttng_event_header_declare(session
);
1252 list_for_each_entry(chan
, &session
->chan
, list
) {
1253 ret
= _lttng_channel_metadata_statedump(session
, chan
);
1258 list_for_each_entry(event
, &session
->events
, list
) {
1259 ret
= _lttng_event_metadata_statedump(session
, event
->chan
, event
);
1263 session
->metadata_dumped
= 1;
1269 * lttng_transport_register - LTT transport registration
1270 * @transport: transport structure
1272 * Registers a transport which can be used as output to extract the data out of
1273 * LTTng. The module calling this registration function must ensure that no
1274 * trap-inducing code will be executed by the transport functions. E.g.
1275 * vmalloc_sync_all() must be called between a vmalloc and the moment the memory
1276 * is made visible to the transport function. This registration acts as a
1277 * vmalloc_sync_all. Therefore, only if the module allocates virtual memory
1278 * after its registration must it synchronize the TLBs.
1280 void lttng_transport_register(struct lttng_transport
*transport
)
1283 * Make sure no page fault can be triggered by the module about to be
1284 * registered. We deal with this here so we don't have to call
1285 * vmalloc_sync_all() in each module's init.
1287 wrapper_vmalloc_sync_all();
1289 mutex_lock(&sessions_mutex
);
1290 list_add_tail(&transport
->node
, <tng_transport_list
);
1291 mutex_unlock(&sessions_mutex
);
1293 EXPORT_SYMBOL_GPL(lttng_transport_register
);
1296 * lttng_transport_unregister - LTT transport unregistration
1297 * @transport: transport structure
1299 void lttng_transport_unregister(struct lttng_transport
*transport
)
1301 mutex_lock(&sessions_mutex
);
1302 list_del(&transport
->node
);
1303 mutex_unlock(&sessions_mutex
);
1305 EXPORT_SYMBOL_GPL(lttng_transport_unregister
);
1307 static int __init
lttng_events_init(void)
1311 ret
= wrapper_lttng_fixup_sig(THIS_MODULE
);
1314 ret
= wrapper_get_pfnblock_flags_mask_init();
1317 ret
= lttng_tracepoint_init();
1320 event_cache
= KMEM_CACHE(lttng_event
, 0);
1325 ret
= lttng_abi_init();
1328 ret
= lttng_logger_init();
1336 kmem_cache_destroy(event_cache
);
1338 lttng_tracepoint_exit();
1342 module_init(lttng_events_init
);
1344 static void __exit
lttng_events_exit(void)
1346 struct lttng_session
*session
, *tmpsession
;
1348 lttng_logger_exit();
1350 list_for_each_entry_safe(session
, tmpsession
, &sessions
, list
)
1351 lttng_session_destroy(session
);
1352 kmem_cache_destroy(event_cache
);
1353 lttng_tracepoint_exit();
1356 module_exit(lttng_events_exit
);
1358 MODULE_LICENSE("GPL and additional rights");
1359 MODULE_AUTHOR("Mathieu Desnoyers <mathieu.desnoyers@efficios.com>");
1360 MODULE_DESCRIPTION("LTTng Events");
1361 MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION
) "."
1362 __stringify(LTTNG_MODULES_MINOR_VERSION
) "."
1363 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION
)
1364 LTTNG_MODULES_EXTRAVERSION
);