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 "wrapper/uuid.h"
31 #include "wrapper/vmalloc.h" /* for wrapper_vmalloc_sync_all() */
32 #include "wrapper/random.h"
33 #include "wrapper/tracepoint.h"
34 #include "lttng-kernel-version.h"
35 #include "lttng-events.h"
36 #include "lttng-tracer.h"
37 #include "lttng-abi-old.h"
39 #define METADATA_CACHE_DEFAULT_SIZE 4096
41 static LIST_HEAD(sessions
);
42 static LIST_HEAD(lttng_transport_list
);
44 * Protect the sessions and metadata caches.
46 static DEFINE_MUTEX(sessions_mutex
);
47 static struct kmem_cache
*event_cache
;
49 static void _lttng_event_destroy(struct lttng_event
*event
);
50 static void _lttng_channel_destroy(struct lttng_channel
*chan
);
51 static int _lttng_event_unregister(struct lttng_event
*event
);
53 int _lttng_event_metadata_statedump(struct lttng_session
*session
,
54 struct lttng_channel
*chan
,
55 struct lttng_event
*event
);
57 int _lttng_session_metadata_statedump(struct lttng_session
*session
);
59 void _lttng_metadata_channel_hangup(struct lttng_metadata_stream
*stream
);
61 void synchronize_trace(void)
64 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0))
65 #ifdef CONFIG_PREEMPT_RT_FULL
68 #else /* (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)) */
69 #ifdef CONFIG_PREEMPT_RT
72 #endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)) */
75 struct lttng_session
*lttng_session_create(void)
77 struct lttng_session
*session
;
78 struct lttng_metadata_cache
*metadata_cache
;
80 mutex_lock(&sessions_mutex
);
81 session
= kzalloc(sizeof(struct lttng_session
), GFP_KERNEL
);
84 INIT_LIST_HEAD(&session
->chan
);
85 INIT_LIST_HEAD(&session
->events
);
86 uuid_le_gen(&session
->uuid
);
88 metadata_cache
= kzalloc(sizeof(struct lttng_metadata_cache
),
91 goto err_free_session
;
92 metadata_cache
->data
= kzalloc(METADATA_CACHE_DEFAULT_SIZE
,
94 if (!metadata_cache
->data
)
96 metadata_cache
->cache_alloc
= METADATA_CACHE_DEFAULT_SIZE
;
97 kref_init(&metadata_cache
->refcount
);
98 session
->metadata_cache
= metadata_cache
;
99 INIT_LIST_HEAD(&metadata_cache
->metadata_stream
);
100 list_add(&session
->list
, &sessions
);
101 mutex_unlock(&sessions_mutex
);
105 kfree(metadata_cache
);
109 mutex_unlock(&sessions_mutex
);
113 void metadata_cache_destroy(struct kref
*kref
)
115 struct lttng_metadata_cache
*cache
=
116 container_of(kref
, struct lttng_metadata_cache
, refcount
);
121 void lttng_session_destroy(struct lttng_session
*session
)
123 struct lttng_channel
*chan
, *tmpchan
;
124 struct lttng_event
*event
, *tmpevent
;
125 struct lttng_metadata_stream
*metadata_stream
;
128 mutex_lock(&sessions_mutex
);
129 ACCESS_ONCE(session
->active
) = 0;
130 list_for_each_entry(chan
, &session
->chan
, list
) {
131 ret
= lttng_syscalls_unregister(chan
);
134 list_for_each_entry(event
, &session
->events
, list
) {
135 ret
= _lttng_event_unregister(event
);
138 synchronize_trace(); /* Wait for in-flight events to complete */
139 list_for_each_entry_safe(event
, tmpevent
, &session
->events
, list
)
140 _lttng_event_destroy(event
);
141 list_for_each_entry_safe(chan
, tmpchan
, &session
->chan
, list
) {
142 BUG_ON(chan
->channel_type
== METADATA_CHANNEL
);
143 _lttng_channel_destroy(chan
);
145 list_for_each_entry(metadata_stream
, &session
->metadata_cache
->metadata_stream
, list
)
146 _lttng_metadata_channel_hangup(metadata_stream
);
147 kref_put(&session
->metadata_cache
->refcount
, metadata_cache_destroy
);
148 list_del(&session
->list
);
149 mutex_unlock(&sessions_mutex
);
153 int lttng_session_enable(struct lttng_session
*session
)
156 struct lttng_channel
*chan
;
158 mutex_lock(&sessions_mutex
);
159 if (session
->active
) {
165 * Snapshot the number of events per channel to know the type of header
168 list_for_each_entry(chan
, &session
->chan
, list
) {
169 if (chan
->header_type
)
170 continue; /* don't change it if session stop/restart */
171 if (chan
->free_event_id
< 31)
172 chan
->header_type
= 1; /* compact */
174 chan
->header_type
= 2; /* large */
177 ACCESS_ONCE(session
->active
) = 1;
178 ACCESS_ONCE(session
->been_active
) = 1;
179 ret
= _lttng_session_metadata_statedump(session
);
181 ACCESS_ONCE(session
->active
) = 0;
184 ret
= lttng_statedump_start(session
);
186 ACCESS_ONCE(session
->active
) = 0;
188 mutex_unlock(&sessions_mutex
);
192 int lttng_session_disable(struct lttng_session
*session
)
196 mutex_lock(&sessions_mutex
);
197 if (!session
->active
) {
201 ACCESS_ONCE(session
->active
) = 0;
203 mutex_unlock(&sessions_mutex
);
207 int lttng_channel_enable(struct lttng_channel
*channel
)
211 if (channel
->channel_type
== METADATA_CHANNEL
)
213 old
= xchg(&channel
->enabled
, 1);
219 int lttng_channel_disable(struct lttng_channel
*channel
)
223 if (channel
->channel_type
== METADATA_CHANNEL
)
225 old
= xchg(&channel
->enabled
, 0);
231 int lttng_event_enable(struct lttng_event
*event
)
235 if (event
->chan
->channel_type
== METADATA_CHANNEL
)
237 old
= xchg(&event
->enabled
, 1);
243 int lttng_event_disable(struct lttng_event
*event
)
247 if (event
->chan
->channel_type
== METADATA_CHANNEL
)
249 old
= xchg(&event
->enabled
, 0);
255 static struct lttng_transport
*lttng_transport_find(const char *name
)
257 struct lttng_transport
*transport
;
259 list_for_each_entry(transport
, <tng_transport_list
, node
) {
260 if (!strcmp(transport
->name
, name
))
266 struct lttng_channel
*lttng_channel_create(struct lttng_session
*session
,
267 const char *transport_name
,
269 size_t subbuf_size
, size_t num_subbuf
,
270 unsigned int switch_timer_interval
,
271 unsigned int read_timer_interval
,
272 enum channel_type channel_type
)
274 struct lttng_channel
*chan
;
275 struct lttng_transport
*transport
= NULL
;
277 mutex_lock(&sessions_mutex
);
278 if (session
->been_active
&& channel_type
!= METADATA_CHANNEL
)
279 goto active
; /* Refuse to add channel to active session */
280 transport
= lttng_transport_find(transport_name
);
282 printk(KERN_WARNING
"LTTng transport %s not found\n",
286 if (!try_module_get(transport
->owner
)) {
287 printk(KERN_WARNING
"LTT : Can't lock transport module.\n");
290 chan
= kzalloc(sizeof(struct lttng_channel
), GFP_KERNEL
);
293 chan
->session
= session
;
294 chan
->id
= session
->free_chan_id
++;
295 chan
->ops
= &transport
->ops
;
297 * Note: the channel creation op already writes into the packet
298 * headers. Therefore the "chan" information used as input
299 * should be already accessible.
301 chan
->chan
= transport
->ops
.channel_create(transport_name
,
302 chan
, buf_addr
, subbuf_size
, num_subbuf
,
303 switch_timer_interval
, read_timer_interval
);
307 chan
->transport
= transport
;
308 chan
->channel_type
= channel_type
;
309 list_add(&chan
->list
, &session
->chan
);
310 mutex_unlock(&sessions_mutex
);
317 module_put(transport
->owner
);
320 mutex_unlock(&sessions_mutex
);
325 * Only used internally at session destruction for per-cpu channels, and
326 * when metadata channel is released.
327 * Needs to be called with sessions mutex held.
330 void _lttng_channel_destroy(struct lttng_channel
*chan
)
332 chan
->ops
->channel_destroy(chan
->chan
);
333 module_put(chan
->transport
->owner
);
334 list_del(&chan
->list
);
335 lttng_destroy_context(chan
->ctx
);
339 void lttng_metadata_channel_destroy(struct lttng_channel
*chan
)
341 BUG_ON(chan
->channel_type
!= METADATA_CHANNEL
);
343 /* Protect the metadata cache with the sessions_mutex. */
344 mutex_lock(&sessions_mutex
);
345 _lttng_channel_destroy(chan
);
346 mutex_unlock(&sessions_mutex
);
348 EXPORT_SYMBOL_GPL(lttng_metadata_channel_destroy
);
351 void _lttng_metadata_channel_hangup(struct lttng_metadata_stream
*stream
)
353 stream
->finalized
= 1;
354 wake_up_interruptible(&stream
->read_wait
);
358 * Supports event creation while tracing session is active.
360 struct lttng_event
*lttng_event_create(struct lttng_channel
*chan
,
361 struct lttng_kernel_event
*event_param
,
363 const struct lttng_event_desc
*internal_desc
)
365 struct lttng_event
*event
;
368 mutex_lock(&sessions_mutex
);
369 if (chan
->free_event_id
== -1U)
372 * This is O(n^2) (for each event, the loop is called at event
373 * creation). Might require a hash if we have lots of events.
375 list_for_each_entry(event
, &chan
->session
->events
, list
)
376 if (!strcmp(event
->desc
->name
, event_param
->name
))
378 event
= kmem_cache_zalloc(event_cache
, GFP_KERNEL
);
382 event
->filter
= filter
;
383 event
->id
= chan
->free_event_id
++;
385 event
->instrumentation
= event_param
->instrumentation
;
386 /* Populate lttng_event structure before tracepoint registration. */
388 switch (event_param
->instrumentation
) {
389 case LTTNG_KERNEL_TRACEPOINT
:
390 event
->desc
= lttng_event_get(event_param
->name
);
393 ret
= kabi_2635_tracepoint_probe_register(event_param
->name
,
394 event
->desc
->probe_callback
,
399 case LTTNG_KERNEL_KPROBE
:
400 ret
= lttng_kprobes_register(event_param
->name
,
401 event_param
->u
.kprobe
.symbol_name
,
402 event_param
->u
.kprobe
.offset
,
403 event_param
->u
.kprobe
.addr
,
407 ret
= try_module_get(event
->desc
->owner
);
410 case LTTNG_KERNEL_KRETPROBE
:
412 struct lttng_event
*event_return
;
414 /* kretprobe defines 2 events */
416 kmem_cache_zalloc(event_cache
, GFP_KERNEL
);
419 event_return
->chan
= chan
;
420 event_return
->filter
= filter
;
421 event_return
->id
= chan
->free_event_id
++;
422 event_return
->enabled
= 1;
423 event_return
->instrumentation
= event_param
->instrumentation
;
425 * Populate lttng_event structure before kretprobe registration.
428 ret
= lttng_kretprobes_register(event_param
->name
,
429 event_param
->u
.kretprobe
.symbol_name
,
430 event_param
->u
.kretprobe
.offset
,
431 event_param
->u
.kretprobe
.addr
,
432 event
, event_return
);
434 kmem_cache_free(event_cache
, event_return
);
437 /* Take 2 refs on the module: one per event. */
438 ret
= try_module_get(event
->desc
->owner
);
440 ret
= try_module_get(event
->desc
->owner
);
442 ret
= _lttng_event_metadata_statedump(chan
->session
, chan
,
445 kmem_cache_free(event_cache
, event_return
);
446 module_put(event
->desc
->owner
);
447 module_put(event
->desc
->owner
);
448 goto statedump_error
;
450 list_add(&event_return
->list
, &chan
->session
->events
);
453 case LTTNG_KERNEL_FUNCTION
:
454 ret
= lttng_ftrace_register(event_param
->name
,
455 event_param
->u
.ftrace
.symbol_name
,
459 ret
= try_module_get(event
->desc
->owner
);
462 case LTTNG_KERNEL_NOOP
:
463 event
->desc
= internal_desc
;
470 ret
= _lttng_event_metadata_statedump(chan
->session
, chan
, event
);
472 goto statedump_error
;
473 list_add(&event
->list
, &chan
->session
->events
);
474 mutex_unlock(&sessions_mutex
);
478 /* If a statedump error occurs, events will not be readable. */
480 kmem_cache_free(event_cache
, event
);
484 mutex_unlock(&sessions_mutex
);
489 * Only used internally at session destruction.
491 int _lttng_event_unregister(struct lttng_event
*event
)
495 switch (event
->instrumentation
) {
496 case LTTNG_KERNEL_TRACEPOINT
:
497 ret
= kabi_2635_tracepoint_probe_unregister(event
->desc
->name
,
498 event
->desc
->probe_callback
,
503 case LTTNG_KERNEL_KPROBE
:
504 lttng_kprobes_unregister(event
);
507 case LTTNG_KERNEL_KRETPROBE
:
508 lttng_kretprobes_unregister(event
);
511 case LTTNG_KERNEL_FUNCTION
:
512 lttng_ftrace_unregister(event
);
515 case LTTNG_KERNEL_NOOP
:
525 * Only used internally at session destruction.
528 void _lttng_event_destroy(struct lttng_event
*event
)
530 switch (event
->instrumentation
) {
531 case LTTNG_KERNEL_TRACEPOINT
:
532 lttng_event_put(event
->desc
);
534 case LTTNG_KERNEL_KPROBE
:
535 module_put(event
->desc
->owner
);
536 lttng_kprobes_destroy_private(event
);
538 case LTTNG_KERNEL_KRETPROBE
:
539 module_put(event
->desc
->owner
);
540 lttng_kretprobes_destroy_private(event
);
542 case LTTNG_KERNEL_FUNCTION
:
543 module_put(event
->desc
->owner
);
544 lttng_ftrace_destroy_private(event
);
546 case LTTNG_KERNEL_NOOP
:
551 list_del(&event
->list
);
552 lttng_destroy_context(event
->ctx
);
553 kmem_cache_free(event_cache
, event
);
557 * Serialize at most one packet worth of metadata into a metadata
559 * We have exclusive access to our metadata buffer (protected by the
560 * sessions_mutex), so we can do racy operations such as looking for
561 * remaining space left in packet and write, since mutual exclusion
562 * protects us from concurrent writes.
563 * Returns the number of bytes written in the channel, 0 if no data
564 * was written and a negative value on error.
566 int lttng_metadata_output_channel(struct lttng_metadata_stream
*stream
,
567 struct channel
*chan
)
569 struct lib_ring_buffer_ctx ctx
;
571 size_t len
, reserve_len
;
574 * Ensure we support mutiple get_next / put sequences followed
577 WARN_ON(stream
->metadata_in
< stream
->metadata_out
);
578 if (stream
->metadata_in
!= stream
->metadata_out
)
581 len
= stream
->metadata_cache
->metadata_written
-
585 reserve_len
= min_t(size_t,
586 stream
->transport
->ops
.packet_avail_size(chan
),
588 lib_ring_buffer_ctx_init(&ctx
, chan
, NULL
, reserve_len
,
591 * If reservation failed, return an error to the caller.
593 ret
= stream
->transport
->ops
.event_reserve(&ctx
, 0);
595 printk(KERN_WARNING
"LTTng: Metadata event reservation failed\n");
598 stream
->transport
->ops
.event_write(&ctx
,
599 stream
->metadata_cache
->data
+ stream
->metadata_in
,
601 stream
->transport
->ops
.event_commit(&ctx
);
602 stream
->metadata_in
+= reserve_len
;
610 * Write the metadata to the metadata cache.
611 * Must be called with sessions_mutex held.
613 int lttng_metadata_printf(struct lttng_session
*session
,
614 const char *fmt
, ...)
619 struct lttng_metadata_stream
*stream
;
621 WARN_ON_ONCE(!ACCESS_ONCE(session
->active
));
624 str
= kvasprintf(GFP_KERNEL
, fmt
, ap
);
630 if (session
->metadata_cache
->metadata_written
+ len
>
631 session
->metadata_cache
->cache_alloc
) {
632 char *tmp_cache_realloc
;
633 unsigned int tmp_cache_alloc_size
;
635 tmp_cache_alloc_size
= max_t(unsigned int,
636 session
->metadata_cache
->cache_alloc
+ len
,
637 session
->metadata_cache
->cache_alloc
<< 1);
638 tmp_cache_realloc
= krealloc(session
->metadata_cache
->data
,
639 tmp_cache_alloc_size
, GFP_KERNEL
);
640 if (!tmp_cache_realloc
)
642 session
->metadata_cache
->cache_alloc
= tmp_cache_alloc_size
;
643 session
->metadata_cache
->data
= tmp_cache_realloc
;
645 memcpy(session
->metadata_cache
->data
+
646 session
->metadata_cache
->metadata_written
,
648 session
->metadata_cache
->metadata_written
+= len
;
651 list_for_each_entry(stream
, &session
->metadata_cache
->metadata_stream
, list
)
652 wake_up_interruptible(&stream
->read_wait
);
662 * Must be called with sessions_mutex held.
665 int _lttng_field_statedump(struct lttng_session
*session
,
666 const struct lttng_event_field
*field
)
670 switch (field
->type
.atype
) {
672 ret
= lttng_metadata_printf(session
,
673 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n",
674 field
->type
.u
.basic
.integer
.size
,
675 field
->type
.u
.basic
.integer
.alignment
,
676 field
->type
.u
.basic
.integer
.signedness
,
677 (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_none
)
679 : (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
682 field
->type
.u
.basic
.integer
.base
,
684 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
686 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
691 ret
= lttng_metadata_printf(session
,
693 field
->type
.u
.basic
.enumeration
.name
,
698 const struct lttng_basic_type
*elem_type
;
700 elem_type
= &field
->type
.u
.array
.elem_type
;
701 ret
= lttng_metadata_printf(session
,
702 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n",
703 elem_type
->u
.basic
.integer
.size
,
704 elem_type
->u
.basic
.integer
.alignment
,
705 elem_type
->u
.basic
.integer
.signedness
,
706 (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
708 : (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
711 elem_type
->u
.basic
.integer
.base
,
713 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
715 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
717 field
->name
, field
->type
.u
.array
.length
);
722 const struct lttng_basic_type
*elem_type
;
723 const struct lttng_basic_type
*length_type
;
725 elem_type
= &field
->type
.u
.sequence
.elem_type
;
726 length_type
= &field
->type
.u
.sequence
.length_type
;
727 ret
= lttng_metadata_printf(session
,
728 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n",
729 length_type
->u
.basic
.integer
.size
,
730 (unsigned int) length_type
->u
.basic
.integer
.alignment
,
731 length_type
->u
.basic
.integer
.signedness
,
732 (length_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
734 : ((length_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
737 length_type
->u
.basic
.integer
.base
,
739 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
741 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
747 ret
= lttng_metadata_printf(session
,
748 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n",
749 elem_type
->u
.basic
.integer
.size
,
750 (unsigned int) elem_type
->u
.basic
.integer
.alignment
,
751 elem_type
->u
.basic
.integer
.signedness
,
752 (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
754 : ((elem_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
757 elem_type
->u
.basic
.integer
.base
,
759 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
761 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
769 /* Default encoding is UTF8 */
770 ret
= lttng_metadata_printf(session
,
772 field
->type
.u
.basic
.string
.encoding
== lttng_encode_ASCII
?
773 " { encoding = ASCII; }" : "",
784 int _lttng_context_metadata_statedump(struct lttng_session
*session
,
785 struct lttng_ctx
*ctx
)
792 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
793 const struct lttng_ctx_field
*field
= &ctx
->fields
[i
];
795 ret
= _lttng_field_statedump(session
, &field
->event_field
);
803 int _lttng_fields_metadata_statedump(struct lttng_session
*session
,
804 struct lttng_event
*event
)
806 const struct lttng_event_desc
*desc
= event
->desc
;
810 for (i
= 0; i
< desc
->nr_fields
; i
++) {
811 const struct lttng_event_field
*field
= &desc
->fields
[i
];
813 ret
= _lttng_field_statedump(session
, field
);
821 * Must be called with sessions_mutex held.
824 int _lttng_event_metadata_statedump(struct lttng_session
*session
,
825 struct lttng_channel
*chan
,
826 struct lttng_event
*event
)
830 if (event
->metadata_dumped
|| !ACCESS_ONCE(session
->active
))
832 if (chan
->channel_type
== METADATA_CHANNEL
)
835 ret
= lttng_metadata_printf(session
,
839 " stream_id = %u;\n",
847 ret
= lttng_metadata_printf(session
,
848 " context := struct {\n");
852 ret
= _lttng_context_metadata_statedump(session
, event
->ctx
);
856 ret
= lttng_metadata_printf(session
,
862 ret
= lttng_metadata_printf(session
,
863 " fields := struct {\n"
868 ret
= _lttng_fields_metadata_statedump(session
, event
);
873 * LTTng space reservation can only reserve multiples of the
876 ret
= lttng_metadata_printf(session
,
882 event
->metadata_dumped
= 1;
889 * Must be called with sessions_mutex held.
892 int _lttng_channel_metadata_statedump(struct lttng_session
*session
,
893 struct lttng_channel
*chan
)
897 if (chan
->metadata_dumped
|| !ACCESS_ONCE(session
->active
))
900 if (chan
->channel_type
== METADATA_CHANNEL
)
903 WARN_ON_ONCE(!chan
->header_type
);
904 ret
= lttng_metadata_printf(session
,
907 " event.header := %s;\n"
908 " packet.context := struct packet_context;\n",
910 chan
->header_type
== 1 ? "struct event_header_compact" :
911 "struct event_header_large");
916 ret
= lttng_metadata_printf(session
,
917 " event.context := struct {\n");
921 ret
= _lttng_context_metadata_statedump(session
, chan
->ctx
);
925 ret
= lttng_metadata_printf(session
,
931 ret
= lttng_metadata_printf(session
,
934 chan
->metadata_dumped
= 1;
940 * Must be called with sessions_mutex held.
943 int _lttng_stream_packet_context_declare(struct lttng_session
*session
)
945 return lttng_metadata_printf(session
,
946 "struct packet_context {\n"
947 " uint64_clock_monotonic_t timestamp_begin;\n"
948 " uint64_clock_monotonic_t timestamp_end;\n"
949 " uint64_t content_size;\n"
950 " uint64_t packet_size;\n"
951 " unsigned long events_discarded;\n"
952 " uint32_t cpu_id;\n"
960 * id 31 is reserved to indicate an extended header.
963 * id: range: 0 - 65534.
964 * id 65535 is reserved to indicate an extended header.
966 * Must be called with sessions_mutex held.
969 int _lttng_event_header_declare(struct lttng_session
*session
)
971 return lttng_metadata_printf(session
,
972 "struct event_header_compact {\n"
973 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
976 " uint27_clock_monotonic_t timestamp;\n"
980 " uint64_clock_monotonic_t timestamp;\n"
985 "struct event_header_large {\n"
986 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
989 " uint32_clock_monotonic_t timestamp;\n"
993 " uint64_clock_monotonic_t timestamp;\n"
997 lttng_alignof(uint32_t) * CHAR_BIT
,
998 lttng_alignof(uint16_t) * CHAR_BIT
1003 * Approximation of NTP time of day to clock monotonic correlation,
1004 * taken at start of trace.
1005 * Yes, this is only an approximation. Yes, we can (and will) do better
1006 * in future versions.
1009 uint64_t measure_clock_offset(void)
1011 uint64_t offset
, monotonic
[2], realtime
;
1012 struct timespec rts
= { 0, 0 };
1013 unsigned long flags
;
1015 /* Disable interrupts to increase correlation precision. */
1016 local_irq_save(flags
);
1017 monotonic
[0] = trace_clock_read64();
1018 getnstimeofday(&rts
);
1019 monotonic
[1] = trace_clock_read64();
1020 local_irq_restore(flags
);
1022 offset
= (monotonic
[0] + monotonic
[1]) >> 1;
1023 realtime
= (uint64_t) rts
.tv_sec
* NSEC_PER_SEC
;
1024 realtime
+= rts
.tv_nsec
;
1025 offset
= realtime
- offset
;
1030 * Output metadata into this session's metadata buffers.
1031 * Must be called with sessions_mutex held.
1034 int _lttng_session_metadata_statedump(struct lttng_session
*session
)
1036 unsigned char *uuid_c
= session
->uuid
.b
;
1037 unsigned char uuid_s
[37], clock_uuid_s
[BOOT_ID_LEN
];
1038 struct lttng_channel
*chan
;
1039 struct lttng_event
*event
;
1042 if (!ACCESS_ONCE(session
->active
))
1044 if (session
->metadata_dumped
)
1047 snprintf(uuid_s
, sizeof(uuid_s
),
1048 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
1049 uuid_c
[0], uuid_c
[1], uuid_c
[2], uuid_c
[3],
1050 uuid_c
[4], uuid_c
[5], uuid_c
[6], uuid_c
[7],
1051 uuid_c
[8], uuid_c
[9], uuid_c
[10], uuid_c
[11],
1052 uuid_c
[12], uuid_c
[13], uuid_c
[14], uuid_c
[15]);
1054 ret
= lttng_metadata_printf(session
,
1055 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
1056 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
1057 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
1058 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
1059 "typealias integer { size = %u; align = %u; signed = false; } := unsigned long;\n"
1060 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
1061 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
1067 " byte_order = %s;\n"
1068 " packet.header := struct {\n"
1069 " uint32_t magic;\n"
1070 " uint8_t uuid[16];\n"
1071 " uint32_t stream_id;\n"
1074 lttng_alignof(uint8_t) * CHAR_BIT
,
1075 lttng_alignof(uint16_t) * CHAR_BIT
,
1076 lttng_alignof(uint32_t) * CHAR_BIT
,
1077 lttng_alignof(uint64_t) * CHAR_BIT
,
1078 sizeof(unsigned long) * CHAR_BIT
,
1079 lttng_alignof(unsigned long) * CHAR_BIT
,
1092 ret
= lttng_metadata_printf(session
,
1094 " hostname = \"%s\";\n"
1095 " domain = \"kernel\";\n"
1096 " sysname = \"%s\";\n"
1097 " kernel_release = \"%s\";\n"
1098 " kernel_version = \"%s\";\n"
1099 " tracer_name = \"lttng-modules\";\n"
1100 " tracer_major = %d;\n"
1101 " tracer_minor = %d;\n"
1102 " tracer_patchlevel = %d;\n"
1104 current
->nsproxy
->uts_ns
->name
.nodename
,
1108 LTTNG_MODULES_MAJOR_VERSION
,
1109 LTTNG_MODULES_MINOR_VERSION
,
1110 LTTNG_MODULES_PATCHLEVEL_VERSION
1115 ret
= lttng_metadata_printf(session
,
1123 if (!trace_clock_uuid(clock_uuid_s
)) {
1124 ret
= lttng_metadata_printf(session
,
1125 " uuid = \"%s\";\n",
1132 ret
= lttng_metadata_printf(session
,
1133 " description = \"Monotonic Clock\";\n"
1134 " freq = %llu; /* Frequency, in Hz */\n"
1135 " /* clock value offset from Epoch is: offset * (1/freq) */\n"
1138 (unsigned long long) trace_clock_freq(),
1139 (unsigned long long) measure_clock_offset()
1144 ret
= lttng_metadata_printf(session
,
1145 "typealias integer {\n"
1146 " size = 27; align = 1; signed = false;\n"
1147 " map = clock.monotonic.value;\n"
1148 "} := uint27_clock_monotonic_t;\n"
1150 "typealias integer {\n"
1151 " size = 32; align = %u; signed = false;\n"
1152 " map = clock.monotonic.value;\n"
1153 "} := uint32_clock_monotonic_t;\n"
1155 "typealias integer {\n"
1156 " size = 64; align = %u; signed = false;\n"
1157 " map = clock.monotonic.value;\n"
1158 "} := uint64_clock_monotonic_t;\n\n",
1159 lttng_alignof(uint32_t) * CHAR_BIT
,
1160 lttng_alignof(uint64_t) * CHAR_BIT
1165 ret
= _lttng_stream_packet_context_declare(session
);
1169 ret
= _lttng_event_header_declare(session
);
1174 list_for_each_entry(chan
, &session
->chan
, list
) {
1175 ret
= _lttng_channel_metadata_statedump(session
, chan
);
1180 list_for_each_entry(event
, &session
->events
, list
) {
1181 ret
= _lttng_event_metadata_statedump(session
, event
->chan
, event
);
1185 session
->metadata_dumped
= 1;
1191 * lttng_transport_register - LTT transport registration
1192 * @transport: transport structure
1194 * Registers a transport which can be used as output to extract the data out of
1195 * LTTng. The module calling this registration function must ensure that no
1196 * trap-inducing code will be executed by the transport functions. E.g.
1197 * vmalloc_sync_all() must be called between a vmalloc and the moment the memory
1198 * is made visible to the transport function. This registration acts as a
1199 * vmalloc_sync_all. Therefore, only if the module allocates virtual memory
1200 * after its registration must it synchronize the TLBs.
1202 void lttng_transport_register(struct lttng_transport
*transport
)
1205 * Make sure no page fault can be triggered by the module about to be
1206 * registered. We deal with this here so we don't have to call
1207 * vmalloc_sync_all() in each module's init.
1209 wrapper_vmalloc_sync_all();
1211 mutex_lock(&sessions_mutex
);
1212 list_add_tail(&transport
->node
, <tng_transport_list
);
1213 mutex_unlock(&sessions_mutex
);
1215 EXPORT_SYMBOL_GPL(lttng_transport_register
);
1218 * lttng_transport_unregister - LTT transport unregistration
1219 * @transport: transport structure
1221 void lttng_transport_unregister(struct lttng_transport
*transport
)
1223 mutex_lock(&sessions_mutex
);
1224 list_del(&transport
->node
);
1225 mutex_unlock(&sessions_mutex
);
1227 EXPORT_SYMBOL_GPL(lttng_transport_unregister
);
1229 static int __init
lttng_events_init(void)
1233 event_cache
= KMEM_CACHE(lttng_event
, 0);
1236 ret
= lttng_abi_init();
1241 kmem_cache_destroy(event_cache
);
1245 module_init(lttng_events_init
);
1247 static void __exit
lttng_events_exit(void)
1249 struct lttng_session
*session
, *tmpsession
;
1252 list_for_each_entry_safe(session
, tmpsession
, &sessions
, list
)
1253 lttng_session_destroy(session
);
1254 kmem_cache_destroy(event_cache
);
1257 module_exit(lttng_events_exit
);
1259 MODULE_LICENSE("GPL and additional rights");
1260 MODULE_AUTHOR("Mathieu Desnoyers <mathieu.desnoyers@efficios.com>");
1261 MODULE_DESCRIPTION("LTTng Events");