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 "wrapper/uuid.h"
38 #include "wrapper/vmalloc.h" /* for wrapper_vmalloc_sync_all() */
39 #include "wrapper/random.h"
40 #include "wrapper/tracepoint.h"
41 #include "lttng-kernel-version.h"
42 #include "lttng-events.h"
43 #include "lttng-tracer.h"
44 #include "lttng-abi-old.h"
46 #define METADATA_CACHE_DEFAULT_SIZE 4096
48 static LIST_HEAD(sessions
);
49 static LIST_HEAD(lttng_transport_list
);
51 * Protect the sessions and metadata caches.
53 static DEFINE_MUTEX(sessions_mutex
);
54 static struct kmem_cache
*event_cache
;
56 static void _lttng_event_destroy(struct lttng_event
*event
);
57 static void _lttng_channel_destroy(struct lttng_channel
*chan
);
58 static int _lttng_event_unregister(struct lttng_event
*event
);
60 int _lttng_event_metadata_statedump(struct lttng_session
*session
,
61 struct lttng_channel
*chan
,
62 struct lttng_event
*event
);
64 int _lttng_session_metadata_statedump(struct lttng_session
*session
);
66 void _lttng_metadata_channel_hangup(struct lttng_metadata_stream
*stream
);
68 void synchronize_trace(void)
71 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0))
72 #ifdef CONFIG_PREEMPT_RT_FULL
75 #else /* (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)) */
76 #ifdef CONFIG_PREEMPT_RT
79 #endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)) */
82 struct lttng_session
*lttng_session_create(void)
84 struct lttng_session
*session
;
85 struct lttng_metadata_cache
*metadata_cache
;
87 mutex_lock(&sessions_mutex
);
88 session
= kzalloc(sizeof(struct lttng_session
), GFP_KERNEL
);
91 INIT_LIST_HEAD(&session
->chan
);
92 INIT_LIST_HEAD(&session
->events
);
93 uuid_le_gen(&session
->uuid
);
95 metadata_cache
= kzalloc(sizeof(struct lttng_metadata_cache
),
98 goto err_free_session
;
99 metadata_cache
->data
= kzalloc(METADATA_CACHE_DEFAULT_SIZE
,
101 if (!metadata_cache
->data
)
103 metadata_cache
->cache_alloc
= METADATA_CACHE_DEFAULT_SIZE
;
104 kref_init(&metadata_cache
->refcount
);
105 session
->metadata_cache
= metadata_cache
;
106 INIT_LIST_HEAD(&metadata_cache
->metadata_stream
);
107 memcpy(&metadata_cache
->uuid
, &session
->uuid
,
108 sizeof(metadata_cache
->uuid
));
109 list_add(&session
->list
, &sessions
);
110 mutex_unlock(&sessions_mutex
);
114 kfree(metadata_cache
);
118 mutex_unlock(&sessions_mutex
);
122 void metadata_cache_destroy(struct kref
*kref
)
124 struct lttng_metadata_cache
*cache
=
125 container_of(kref
, struct lttng_metadata_cache
, refcount
);
130 void lttng_session_destroy(struct lttng_session
*session
)
132 struct lttng_channel
*chan
, *tmpchan
;
133 struct lttng_event
*event
, *tmpevent
;
134 struct lttng_metadata_stream
*metadata_stream
;
137 mutex_lock(&sessions_mutex
);
138 ACCESS_ONCE(session
->active
) = 0;
139 list_for_each_entry(chan
, &session
->chan
, list
) {
140 ret
= lttng_syscalls_unregister(chan
);
143 list_for_each_entry(event
, &session
->events
, list
) {
144 ret
= _lttng_event_unregister(event
);
147 synchronize_trace(); /* Wait for in-flight events to complete */
148 list_for_each_entry_safe(event
, tmpevent
, &session
->events
, list
)
149 _lttng_event_destroy(event
);
150 list_for_each_entry_safe(chan
, tmpchan
, &session
->chan
, list
) {
151 BUG_ON(chan
->channel_type
== METADATA_CHANNEL
);
152 _lttng_channel_destroy(chan
);
154 list_for_each_entry(metadata_stream
, &session
->metadata_cache
->metadata_stream
, list
)
155 _lttng_metadata_channel_hangup(metadata_stream
);
156 kref_put(&session
->metadata_cache
->refcount
, metadata_cache_destroy
);
157 list_del(&session
->list
);
158 mutex_unlock(&sessions_mutex
);
162 int lttng_session_enable(struct lttng_session
*session
)
165 struct lttng_channel
*chan
;
167 mutex_lock(&sessions_mutex
);
168 if (session
->active
) {
174 * Snapshot the number of events per channel to know the type of header
177 list_for_each_entry(chan
, &session
->chan
, list
) {
178 if (chan
->header_type
)
179 continue; /* don't change it if session stop/restart */
180 if (chan
->free_event_id
< 31)
181 chan
->header_type
= 1; /* compact */
183 chan
->header_type
= 2; /* large */
186 ACCESS_ONCE(session
->active
) = 1;
187 ACCESS_ONCE(session
->been_active
) = 1;
188 ret
= _lttng_session_metadata_statedump(session
);
190 ACCESS_ONCE(session
->active
) = 0;
193 ret
= lttng_statedump_start(session
);
195 ACCESS_ONCE(session
->active
) = 0;
197 mutex_unlock(&sessions_mutex
);
201 int lttng_session_disable(struct lttng_session
*session
)
205 mutex_lock(&sessions_mutex
);
206 if (!session
->active
) {
210 ACCESS_ONCE(session
->active
) = 0;
212 mutex_unlock(&sessions_mutex
);
216 int lttng_channel_enable(struct lttng_channel
*channel
)
220 if (channel
->channel_type
== METADATA_CHANNEL
)
222 old
= xchg(&channel
->enabled
, 1);
228 int lttng_channel_disable(struct lttng_channel
*channel
)
232 if (channel
->channel_type
== METADATA_CHANNEL
)
234 old
= xchg(&channel
->enabled
, 0);
240 int lttng_event_enable(struct lttng_event
*event
)
244 if (event
->chan
->channel_type
== METADATA_CHANNEL
)
246 old
= xchg(&event
->enabled
, 1);
252 int lttng_event_disable(struct lttng_event
*event
)
256 if (event
->chan
->channel_type
== METADATA_CHANNEL
)
258 old
= xchg(&event
->enabled
, 0);
264 static struct lttng_transport
*lttng_transport_find(const char *name
)
266 struct lttng_transport
*transport
;
268 list_for_each_entry(transport
, <tng_transport_list
, node
) {
269 if (!strcmp(transport
->name
, name
))
275 struct lttng_channel
*lttng_channel_create(struct lttng_session
*session
,
276 const char *transport_name
,
278 size_t subbuf_size
, size_t num_subbuf
,
279 unsigned int switch_timer_interval
,
280 unsigned int read_timer_interval
,
281 enum channel_type channel_type
)
283 struct lttng_channel
*chan
;
284 struct lttng_transport
*transport
= NULL
;
286 mutex_lock(&sessions_mutex
);
287 if (session
->been_active
&& channel_type
!= METADATA_CHANNEL
)
288 goto active
; /* Refuse to add channel to active session */
289 transport
= lttng_transport_find(transport_name
);
291 printk(KERN_WARNING
"LTTng transport %s not found\n",
295 if (!try_module_get(transport
->owner
)) {
296 printk(KERN_WARNING
"LTT : Can't lock transport module.\n");
299 chan
= kzalloc(sizeof(struct lttng_channel
), GFP_KERNEL
);
302 chan
->session
= session
;
303 chan
->id
= session
->free_chan_id
++;
304 chan
->ops
= &transport
->ops
;
306 * Note: the channel creation op already writes into the packet
307 * headers. Therefore the "chan" information used as input
308 * should be already accessible.
310 chan
->chan
= transport
->ops
.channel_create(transport_name
,
311 chan
, buf_addr
, subbuf_size
, num_subbuf
,
312 switch_timer_interval
, read_timer_interval
);
316 chan
->transport
= transport
;
317 chan
->channel_type
= channel_type
;
318 list_add(&chan
->list
, &session
->chan
);
319 mutex_unlock(&sessions_mutex
);
326 module_put(transport
->owner
);
329 mutex_unlock(&sessions_mutex
);
334 * Only used internally at session destruction for per-cpu channels, and
335 * when metadata channel is released.
336 * Needs to be called with sessions mutex held.
339 void _lttng_channel_destroy(struct lttng_channel
*chan
)
341 chan
->ops
->channel_destroy(chan
->chan
);
342 module_put(chan
->transport
->owner
);
343 list_del(&chan
->list
);
344 lttng_destroy_context(chan
->ctx
);
348 void lttng_metadata_channel_destroy(struct lttng_channel
*chan
)
350 BUG_ON(chan
->channel_type
!= METADATA_CHANNEL
);
352 /* Protect the metadata cache with the sessions_mutex. */
353 mutex_lock(&sessions_mutex
);
354 _lttng_channel_destroy(chan
);
355 mutex_unlock(&sessions_mutex
);
357 EXPORT_SYMBOL_GPL(lttng_metadata_channel_destroy
);
360 void _lttng_metadata_channel_hangup(struct lttng_metadata_stream
*stream
)
362 stream
->finalized
= 1;
363 wake_up_interruptible(&stream
->read_wait
);
367 * Supports event creation while tracing session is active.
369 struct lttng_event
*lttng_event_create(struct lttng_channel
*chan
,
370 struct lttng_kernel_event
*event_param
,
372 const struct lttng_event_desc
*internal_desc
)
374 struct lttng_event
*event
;
377 mutex_lock(&sessions_mutex
);
378 if (chan
->free_event_id
== -1U) {
383 * This is O(n^2) (for each event, the loop is called at event
384 * creation). Might require a hash if we have lots of events.
386 list_for_each_entry(event
, &chan
->session
->events
, list
) {
387 if (!strcmp(event
->desc
->name
, event_param
->name
)) {
389 * Allow events with the same name to appear in
390 * different channels.
392 if (event
->chan
== chan
) {
398 event
= kmem_cache_zalloc(event_cache
, GFP_KERNEL
);
404 event
->filter
= filter
;
405 event
->id
= chan
->free_event_id
++;
407 event
->instrumentation
= event_param
->instrumentation
;
408 /* Populate lttng_event structure before tracepoint registration. */
410 switch (event_param
->instrumentation
) {
411 case LTTNG_KERNEL_TRACEPOINT
:
412 event
->desc
= lttng_event_get(event_param
->name
);
417 ret
= lttng_wrapper_tracepoint_probe_register(event
->desc
->kname
,
418 event
->desc
->probe_callback
,
425 case LTTNG_KERNEL_KPROBE
:
426 ret
= lttng_kprobes_register(event_param
->name
,
427 event_param
->u
.kprobe
.symbol_name
,
428 event_param
->u
.kprobe
.offset
,
429 event_param
->u
.kprobe
.addr
,
435 ret
= try_module_get(event
->desc
->owner
);
438 case LTTNG_KERNEL_KRETPROBE
:
440 struct lttng_event
*event_return
;
442 /* kretprobe defines 2 events */
444 kmem_cache_zalloc(event_cache
, GFP_KERNEL
);
449 event_return
->chan
= chan
;
450 event_return
->filter
= filter
;
451 event_return
->id
= chan
->free_event_id
++;
452 event_return
->enabled
= 1;
453 event_return
->instrumentation
= event_param
->instrumentation
;
455 * Populate lttng_event structure before kretprobe registration.
458 ret
= lttng_kretprobes_register(event_param
->name
,
459 event_param
->u
.kretprobe
.symbol_name
,
460 event_param
->u
.kretprobe
.offset
,
461 event_param
->u
.kretprobe
.addr
,
462 event
, event_return
);
464 kmem_cache_free(event_cache
, event_return
);
468 /* Take 2 refs on the module: one per event. */
469 ret
= try_module_get(event
->desc
->owner
);
471 ret
= try_module_get(event
->desc
->owner
);
473 ret
= _lttng_event_metadata_statedump(chan
->session
, chan
,
475 WARN_ON_ONCE(ret
> 0);
477 kmem_cache_free(event_cache
, event_return
);
478 module_put(event
->desc
->owner
);
479 module_put(event
->desc
->owner
);
480 goto statedump_error
;
482 list_add(&event_return
->list
, &chan
->session
->events
);
485 case LTTNG_KERNEL_FUNCTION
:
486 ret
= lttng_ftrace_register(event_param
->name
,
487 event_param
->u
.ftrace
.symbol_name
,
492 ret
= try_module_get(event
->desc
->owner
);
495 case LTTNG_KERNEL_NOOP
:
496 event
->desc
= internal_desc
;
507 ret
= _lttng_event_metadata_statedump(chan
->session
, chan
, event
);
508 WARN_ON_ONCE(ret
> 0);
510 goto statedump_error
;
512 list_add(&event
->list
, &chan
->session
->events
);
513 mutex_unlock(&sessions_mutex
);
517 /* If a statedump error occurs, events will not be readable. */
519 kmem_cache_free(event_cache
, event
);
523 mutex_unlock(&sessions_mutex
);
528 * Only used internally at session destruction.
530 int _lttng_event_unregister(struct lttng_event
*event
)
534 switch (event
->instrumentation
) {
535 case LTTNG_KERNEL_TRACEPOINT
:
536 ret
= lttng_wrapper_tracepoint_probe_unregister(event
->desc
->kname
,
537 event
->desc
->probe_callback
,
542 case LTTNG_KERNEL_KPROBE
:
543 lttng_kprobes_unregister(event
);
546 case LTTNG_KERNEL_KRETPROBE
:
547 lttng_kretprobes_unregister(event
);
550 case LTTNG_KERNEL_FUNCTION
:
551 lttng_ftrace_unregister(event
);
554 case LTTNG_KERNEL_NOOP
:
564 * Only used internally at session destruction.
567 void _lttng_event_destroy(struct lttng_event
*event
)
569 switch (event
->instrumentation
) {
570 case LTTNG_KERNEL_TRACEPOINT
:
571 lttng_event_put(event
->desc
);
573 case LTTNG_KERNEL_KPROBE
:
574 module_put(event
->desc
->owner
);
575 lttng_kprobes_destroy_private(event
);
577 case LTTNG_KERNEL_KRETPROBE
:
578 module_put(event
->desc
->owner
);
579 lttng_kretprobes_destroy_private(event
);
581 case LTTNG_KERNEL_FUNCTION
:
582 module_put(event
->desc
->owner
);
583 lttng_ftrace_destroy_private(event
);
585 case LTTNG_KERNEL_NOOP
:
590 list_del(&event
->list
);
591 lttng_destroy_context(event
->ctx
);
592 kmem_cache_free(event_cache
, event
);
596 * Serialize at most one packet worth of metadata into a metadata
598 * We have exclusive access to our metadata buffer (protected by the
599 * sessions_mutex), so we can do racy operations such as looking for
600 * remaining space left in packet and write, since mutual exclusion
601 * protects us from concurrent writes.
602 * Returns the number of bytes written in the channel, 0 if no data
603 * was written and a negative value on error.
605 int lttng_metadata_output_channel(struct lttng_metadata_stream
*stream
,
606 struct channel
*chan
)
608 struct lib_ring_buffer_ctx ctx
;
610 size_t len
, reserve_len
;
613 * Ensure we support mutiple get_next / put sequences followed
614 * by put_next. The metadata stream lock internally protects
615 * reading the metadata cache. It can indeed be read
616 * concurrently by "get_next_subbuf" and "flush" operations on
617 * the buffer invoked by different processes.
619 mutex_lock(&stream
->lock
);
620 WARN_ON(stream
->metadata_in
< stream
->metadata_out
);
621 if (stream
->metadata_in
!= stream
->metadata_out
)
624 len
= stream
->metadata_cache
->metadata_written
-
628 reserve_len
= min_t(size_t,
629 stream
->transport
->ops
.packet_avail_size(chan
),
631 lib_ring_buffer_ctx_init(&ctx
, chan
, NULL
, reserve_len
,
634 * If reservation failed, return an error to the caller.
636 ret
= stream
->transport
->ops
.event_reserve(&ctx
, 0);
638 printk(KERN_WARNING
"LTTng: Metadata event reservation failed\n");
641 stream
->transport
->ops
.event_write(&ctx
,
642 stream
->metadata_cache
->data
+ stream
->metadata_in
,
644 stream
->transport
->ops
.event_commit(&ctx
);
645 stream
->metadata_in
+= reserve_len
;
649 mutex_unlock(&stream
->lock
);
654 * Write the metadata to the metadata cache.
655 * Must be called with sessions_mutex held.
657 int lttng_metadata_printf(struct lttng_session
*session
,
658 const char *fmt
, ...)
663 struct lttng_metadata_stream
*stream
;
665 WARN_ON_ONCE(!ACCESS_ONCE(session
->active
));
668 str
= kvasprintf(GFP_KERNEL
, fmt
, ap
);
674 if (session
->metadata_cache
->metadata_written
+ len
>
675 session
->metadata_cache
->cache_alloc
) {
676 char *tmp_cache_realloc
;
677 unsigned int tmp_cache_alloc_size
;
679 tmp_cache_alloc_size
= max_t(unsigned int,
680 session
->metadata_cache
->cache_alloc
+ len
,
681 session
->metadata_cache
->cache_alloc
<< 1);
682 tmp_cache_realloc
= krealloc(session
->metadata_cache
->data
,
683 tmp_cache_alloc_size
, GFP_KERNEL
);
684 if (!tmp_cache_realloc
)
686 session
->metadata_cache
->cache_alloc
= tmp_cache_alloc_size
;
687 session
->metadata_cache
->data
= tmp_cache_realloc
;
689 memcpy(session
->metadata_cache
->data
+
690 session
->metadata_cache
->metadata_written
,
692 session
->metadata_cache
->metadata_written
+= len
;
695 list_for_each_entry(stream
, &session
->metadata_cache
->metadata_stream
, list
)
696 wake_up_interruptible(&stream
->read_wait
);
706 * Must be called with sessions_mutex held.
709 int _lttng_field_statedump(struct lttng_session
*session
,
710 const struct lttng_event_field
*field
)
714 switch (field
->type
.atype
) {
716 ret
= lttng_metadata_printf(session
,
717 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n",
718 field
->type
.u
.basic
.integer
.size
,
719 field
->type
.u
.basic
.integer
.alignment
,
720 field
->type
.u
.basic
.integer
.signedness
,
721 (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_none
)
723 : (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
726 field
->type
.u
.basic
.integer
.base
,
728 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
730 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
735 ret
= lttng_metadata_printf(session
,
737 field
->type
.u
.basic
.enumeration
.name
,
742 const struct lttng_basic_type
*elem_type
;
744 elem_type
= &field
->type
.u
.array
.elem_type
;
745 ret
= lttng_metadata_printf(session
,
746 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n",
747 elem_type
->u
.basic
.integer
.size
,
748 elem_type
->u
.basic
.integer
.alignment
,
749 elem_type
->u
.basic
.integer
.signedness
,
750 (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
752 : (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
755 elem_type
->u
.basic
.integer
.base
,
757 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
759 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
761 field
->name
, field
->type
.u
.array
.length
);
766 const struct lttng_basic_type
*elem_type
;
767 const struct lttng_basic_type
*length_type
;
769 elem_type
= &field
->type
.u
.sequence
.elem_type
;
770 length_type
= &field
->type
.u
.sequence
.length_type
;
771 ret
= lttng_metadata_printf(session
,
772 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n",
773 length_type
->u
.basic
.integer
.size
,
774 (unsigned int) length_type
->u
.basic
.integer
.alignment
,
775 length_type
->u
.basic
.integer
.signedness
,
776 (length_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
778 : ((length_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
781 length_type
->u
.basic
.integer
.base
,
783 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
785 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
791 ret
= lttng_metadata_printf(session
,
792 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n",
793 elem_type
->u
.basic
.integer
.size
,
794 (unsigned int) elem_type
->u
.basic
.integer
.alignment
,
795 elem_type
->u
.basic
.integer
.signedness
,
796 (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
798 : ((elem_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
801 elem_type
->u
.basic
.integer
.base
,
803 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
805 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
813 /* Default encoding is UTF8 */
814 ret
= lttng_metadata_printf(session
,
816 field
->type
.u
.basic
.string
.encoding
== lttng_encode_ASCII
?
817 " { encoding = ASCII; }" : "",
828 int _lttng_context_metadata_statedump(struct lttng_session
*session
,
829 struct lttng_ctx
*ctx
)
836 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
837 const struct lttng_ctx_field
*field
= &ctx
->fields
[i
];
839 ret
= _lttng_field_statedump(session
, &field
->event_field
);
847 int _lttng_fields_metadata_statedump(struct lttng_session
*session
,
848 struct lttng_event
*event
)
850 const struct lttng_event_desc
*desc
= event
->desc
;
854 for (i
= 0; i
< desc
->nr_fields
; i
++) {
855 const struct lttng_event_field
*field
= &desc
->fields
[i
];
857 ret
= _lttng_field_statedump(session
, field
);
865 * Must be called with sessions_mutex held.
868 int _lttng_event_metadata_statedump(struct lttng_session
*session
,
869 struct lttng_channel
*chan
,
870 struct lttng_event
*event
)
874 if (event
->metadata_dumped
|| !ACCESS_ONCE(session
->active
))
876 if (chan
->channel_type
== METADATA_CHANNEL
)
879 ret
= lttng_metadata_printf(session
,
883 " stream_id = %u;\n",
891 ret
= lttng_metadata_printf(session
,
892 " context := struct {\n");
896 ret
= _lttng_context_metadata_statedump(session
, event
->ctx
);
900 ret
= lttng_metadata_printf(session
,
906 ret
= lttng_metadata_printf(session
,
907 " fields := struct {\n"
912 ret
= _lttng_fields_metadata_statedump(session
, event
);
917 * LTTng space reservation can only reserve multiples of the
920 ret
= lttng_metadata_printf(session
,
926 event
->metadata_dumped
= 1;
933 * Must be called with sessions_mutex held.
936 int _lttng_channel_metadata_statedump(struct lttng_session
*session
,
937 struct lttng_channel
*chan
)
941 if (chan
->metadata_dumped
|| !ACCESS_ONCE(session
->active
))
944 if (chan
->channel_type
== METADATA_CHANNEL
)
947 WARN_ON_ONCE(!chan
->header_type
);
948 ret
= lttng_metadata_printf(session
,
951 " event.header := %s;\n"
952 " packet.context := struct packet_context;\n",
954 chan
->header_type
== 1 ? "struct event_header_compact" :
955 "struct event_header_large");
960 ret
= lttng_metadata_printf(session
,
961 " event.context := struct {\n");
965 ret
= _lttng_context_metadata_statedump(session
, chan
->ctx
);
969 ret
= lttng_metadata_printf(session
,
975 ret
= lttng_metadata_printf(session
,
978 chan
->metadata_dumped
= 1;
984 * Must be called with sessions_mutex held.
987 int _lttng_stream_packet_context_declare(struct lttng_session
*session
)
989 return lttng_metadata_printf(session
,
990 "struct packet_context {\n"
991 " uint64_clock_monotonic_t timestamp_begin;\n"
992 " uint64_clock_monotonic_t timestamp_end;\n"
993 " uint64_t content_size;\n"
994 " uint64_t packet_size;\n"
995 " unsigned long events_discarded;\n"
996 " uint32_t cpu_id;\n"
1003 * id: range: 0 - 30.
1004 * id 31 is reserved to indicate an extended header.
1007 * id: range: 0 - 65534.
1008 * id 65535 is reserved to indicate an extended header.
1010 * Must be called with sessions_mutex held.
1013 int _lttng_event_header_declare(struct lttng_session
*session
)
1015 return lttng_metadata_printf(session
,
1016 "struct event_header_compact {\n"
1017 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
1020 " uint27_clock_monotonic_t timestamp;\n"
1024 " uint64_clock_monotonic_t timestamp;\n"
1029 "struct event_header_large {\n"
1030 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
1033 " uint32_clock_monotonic_t timestamp;\n"
1037 " uint64_clock_monotonic_t timestamp;\n"
1041 lttng_alignof(uint32_t) * CHAR_BIT
,
1042 lttng_alignof(uint16_t) * CHAR_BIT
1047 * Approximation of NTP time of day to clock monotonic correlation,
1048 * taken at start of trace.
1049 * Yes, this is only an approximation. Yes, we can (and will) do better
1050 * in future versions.
1053 uint64_t measure_clock_offset(void)
1055 uint64_t offset
, monotonic
[2], realtime
;
1056 struct timespec rts
= { 0, 0 };
1057 unsigned long flags
;
1059 /* Disable interrupts to increase correlation precision. */
1060 local_irq_save(flags
);
1061 monotonic
[0] = trace_clock_read64();
1062 getnstimeofday(&rts
);
1063 monotonic
[1] = trace_clock_read64();
1064 local_irq_restore(flags
);
1066 offset
= (monotonic
[0] + monotonic
[1]) >> 1;
1067 realtime
= (uint64_t) rts
.tv_sec
* NSEC_PER_SEC
;
1068 realtime
+= rts
.tv_nsec
;
1069 offset
= realtime
- offset
;
1074 * Output metadata into this session's metadata buffers.
1075 * Must be called with sessions_mutex held.
1078 int _lttng_session_metadata_statedump(struct lttng_session
*session
)
1080 unsigned char *uuid_c
= session
->uuid
.b
;
1081 unsigned char uuid_s
[37], clock_uuid_s
[BOOT_ID_LEN
];
1082 struct lttng_channel
*chan
;
1083 struct lttng_event
*event
;
1086 if (!ACCESS_ONCE(session
->active
))
1088 if (session
->metadata_dumped
)
1091 snprintf(uuid_s
, sizeof(uuid_s
),
1092 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
1093 uuid_c
[0], uuid_c
[1], uuid_c
[2], uuid_c
[3],
1094 uuid_c
[4], uuid_c
[5], uuid_c
[6], uuid_c
[7],
1095 uuid_c
[8], uuid_c
[9], uuid_c
[10], uuid_c
[11],
1096 uuid_c
[12], uuid_c
[13], uuid_c
[14], uuid_c
[15]);
1098 ret
= lttng_metadata_printf(session
,
1099 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
1100 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
1101 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
1102 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
1103 "typealias integer { size = %u; align = %u; signed = false; } := unsigned long;\n"
1104 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
1105 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
1111 " byte_order = %s;\n"
1112 " packet.header := struct {\n"
1113 " uint32_t magic;\n"
1114 " uint8_t uuid[16];\n"
1115 " uint32_t stream_id;\n"
1118 lttng_alignof(uint8_t) * CHAR_BIT
,
1119 lttng_alignof(uint16_t) * CHAR_BIT
,
1120 lttng_alignof(uint32_t) * CHAR_BIT
,
1121 lttng_alignof(uint64_t) * CHAR_BIT
,
1122 sizeof(unsigned long) * CHAR_BIT
,
1123 lttng_alignof(unsigned long) * CHAR_BIT
,
1136 ret
= lttng_metadata_printf(session
,
1138 " hostname = \"%s\";\n"
1139 " domain = \"kernel\";\n"
1140 " sysname = \"%s\";\n"
1141 " kernel_release = \"%s\";\n"
1142 " kernel_version = \"%s\";\n"
1143 " tracer_name = \"lttng-modules\";\n"
1144 " tracer_major = %d;\n"
1145 " tracer_minor = %d;\n"
1146 " tracer_patchlevel = %d;\n"
1148 current
->nsproxy
->uts_ns
->name
.nodename
,
1152 LTTNG_MODULES_MAJOR_VERSION
,
1153 LTTNG_MODULES_MINOR_VERSION
,
1154 LTTNG_MODULES_PATCHLEVEL_VERSION
1159 ret
= lttng_metadata_printf(session
,
1167 if (!trace_clock_uuid(clock_uuid_s
)) {
1168 ret
= lttng_metadata_printf(session
,
1169 " uuid = \"%s\";\n",
1176 ret
= lttng_metadata_printf(session
,
1177 " description = \"Monotonic Clock\";\n"
1178 " freq = %llu; /* Frequency, in Hz */\n"
1179 " /* clock value offset from Epoch is: offset * (1/freq) */\n"
1182 (unsigned long long) trace_clock_freq(),
1183 (unsigned long long) measure_clock_offset()
1188 ret
= lttng_metadata_printf(session
,
1189 "typealias integer {\n"
1190 " size = 27; align = 1; signed = false;\n"
1191 " map = clock.monotonic.value;\n"
1192 "} := uint27_clock_monotonic_t;\n"
1194 "typealias integer {\n"
1195 " size = 32; align = %u; signed = false;\n"
1196 " map = clock.monotonic.value;\n"
1197 "} := uint32_clock_monotonic_t;\n"
1199 "typealias integer {\n"
1200 " size = 64; align = %u; signed = false;\n"
1201 " map = clock.monotonic.value;\n"
1202 "} := uint64_clock_monotonic_t;\n\n",
1203 lttng_alignof(uint32_t) * CHAR_BIT
,
1204 lttng_alignof(uint64_t) * CHAR_BIT
1209 ret
= _lttng_stream_packet_context_declare(session
);
1213 ret
= _lttng_event_header_declare(session
);
1218 list_for_each_entry(chan
, &session
->chan
, list
) {
1219 ret
= _lttng_channel_metadata_statedump(session
, chan
);
1224 list_for_each_entry(event
, &session
->events
, list
) {
1225 ret
= _lttng_event_metadata_statedump(session
, event
->chan
, event
);
1229 session
->metadata_dumped
= 1;
1235 * lttng_transport_register - LTT transport registration
1236 * @transport: transport structure
1238 * Registers a transport which can be used as output to extract the data out of
1239 * LTTng. The module calling this registration function must ensure that no
1240 * trap-inducing code will be executed by the transport functions. E.g.
1241 * vmalloc_sync_all() must be called between a vmalloc and the moment the memory
1242 * is made visible to the transport function. This registration acts as a
1243 * vmalloc_sync_all. Therefore, only if the module allocates virtual memory
1244 * after its registration must it synchronize the TLBs.
1246 void lttng_transport_register(struct lttng_transport
*transport
)
1249 * Make sure no page fault can be triggered by the module about to be
1250 * registered. We deal with this here so we don't have to call
1251 * vmalloc_sync_all() in each module's init.
1253 wrapper_vmalloc_sync_all();
1255 mutex_lock(&sessions_mutex
);
1256 list_add_tail(&transport
->node
, <tng_transport_list
);
1257 mutex_unlock(&sessions_mutex
);
1259 EXPORT_SYMBOL_GPL(lttng_transport_register
);
1262 * lttng_transport_unregister - LTT transport unregistration
1263 * @transport: transport structure
1265 void lttng_transport_unregister(struct lttng_transport
*transport
)
1267 mutex_lock(&sessions_mutex
);
1268 list_del(&transport
->node
);
1269 mutex_unlock(&sessions_mutex
);
1271 EXPORT_SYMBOL_GPL(lttng_transport_unregister
);
1273 static int __init
lttng_events_init(void)
1277 ret
= wrapper_lttng_fixup_sig(THIS_MODULE
);
1280 ret
= wrapper_get_pfnblock_flags_mask_init();
1283 ret
= lttng_tracepoint_init();
1286 event_cache
= KMEM_CACHE(lttng_event
, 0);
1291 ret
= lttng_abi_init();
1294 ret
= lttng_logger_init();
1302 kmem_cache_destroy(event_cache
);
1304 lttng_tracepoint_exit();
1308 module_init(lttng_events_init
);
1310 static void __exit
lttng_events_exit(void)
1312 struct lttng_session
*session
, *tmpsession
;
1314 lttng_logger_exit();
1316 list_for_each_entry_safe(session
, tmpsession
, &sessions
, list
)
1317 lttng_session_destroy(session
);
1318 kmem_cache_destroy(event_cache
);
1319 lttng_tracepoint_exit();
1322 module_exit(lttng_events_exit
);
1324 MODULE_LICENSE("GPL and additional rights");
1325 MODULE_AUTHOR("Mathieu Desnoyers <mathieu.desnoyers@efficios.com>");
1326 MODULE_DESCRIPTION("LTTng Events");
1327 MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION
) "."
1328 __stringify(LTTNG_MODULES_MINOR_VERSION
) "."
1329 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION
)
1330 LTTNG_MODULES_EXTRAVERSION
);