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 memcpy(&metadata_cache
->uuid
, &session
->uuid
,
102 sizeof(metadata_cache
->uuid
));
103 list_add(&session
->list
, &sessions
);
104 mutex_unlock(&sessions_mutex
);
108 kfree(metadata_cache
);
112 mutex_unlock(&sessions_mutex
);
116 void metadata_cache_destroy(struct kref
*kref
)
118 struct lttng_metadata_cache
*cache
=
119 container_of(kref
, struct lttng_metadata_cache
, refcount
);
124 void lttng_session_destroy(struct lttng_session
*session
)
126 struct lttng_channel
*chan
, *tmpchan
;
127 struct lttng_event
*event
, *tmpevent
;
128 struct lttng_metadata_stream
*metadata_stream
;
131 mutex_lock(&sessions_mutex
);
132 ACCESS_ONCE(session
->active
) = 0;
133 list_for_each_entry(chan
, &session
->chan
, list
) {
134 ret
= lttng_syscalls_unregister(chan
);
137 list_for_each_entry(event
, &session
->events
, list
) {
138 ret
= _lttng_event_unregister(event
);
141 synchronize_trace(); /* Wait for in-flight events to complete */
142 list_for_each_entry_safe(event
, tmpevent
, &session
->events
, list
)
143 _lttng_event_destroy(event
);
144 list_for_each_entry_safe(chan
, tmpchan
, &session
->chan
, list
) {
145 BUG_ON(chan
->channel_type
== METADATA_CHANNEL
);
146 _lttng_channel_destroy(chan
);
148 list_for_each_entry(metadata_stream
, &session
->metadata_cache
->metadata_stream
, list
)
149 _lttng_metadata_channel_hangup(metadata_stream
);
150 kref_put(&session
->metadata_cache
->refcount
, metadata_cache_destroy
);
151 list_del(&session
->list
);
152 mutex_unlock(&sessions_mutex
);
156 int lttng_session_enable(struct lttng_session
*session
)
159 struct lttng_channel
*chan
;
161 mutex_lock(&sessions_mutex
);
162 if (session
->active
) {
168 * Snapshot the number of events per channel to know the type of header
171 list_for_each_entry(chan
, &session
->chan
, list
) {
172 if (chan
->header_type
)
173 continue; /* don't change it if session stop/restart */
174 if (chan
->free_event_id
< 31)
175 chan
->header_type
= 1; /* compact */
177 chan
->header_type
= 2; /* large */
180 ACCESS_ONCE(session
->active
) = 1;
181 ACCESS_ONCE(session
->been_active
) = 1;
182 ret
= _lttng_session_metadata_statedump(session
);
184 ACCESS_ONCE(session
->active
) = 0;
187 ret
= lttng_statedump_start(session
);
189 ACCESS_ONCE(session
->active
) = 0;
191 mutex_unlock(&sessions_mutex
);
195 int lttng_session_disable(struct lttng_session
*session
)
199 mutex_lock(&sessions_mutex
);
200 if (!session
->active
) {
204 ACCESS_ONCE(session
->active
) = 0;
206 mutex_unlock(&sessions_mutex
);
210 int lttng_channel_enable(struct lttng_channel
*channel
)
214 if (channel
->channel_type
== METADATA_CHANNEL
)
216 old
= xchg(&channel
->enabled
, 1);
222 int lttng_channel_disable(struct lttng_channel
*channel
)
226 if (channel
->channel_type
== METADATA_CHANNEL
)
228 old
= xchg(&channel
->enabled
, 0);
234 int lttng_event_enable(struct lttng_event
*event
)
238 if (event
->chan
->channel_type
== METADATA_CHANNEL
)
240 old
= xchg(&event
->enabled
, 1);
246 int lttng_event_disable(struct lttng_event
*event
)
250 if (event
->chan
->channel_type
== METADATA_CHANNEL
)
252 old
= xchg(&event
->enabled
, 0);
258 static struct lttng_transport
*lttng_transport_find(const char *name
)
260 struct lttng_transport
*transport
;
262 list_for_each_entry(transport
, <tng_transport_list
, node
) {
263 if (!strcmp(transport
->name
, name
))
269 struct lttng_channel
*lttng_channel_create(struct lttng_session
*session
,
270 const char *transport_name
,
272 size_t subbuf_size
, size_t num_subbuf
,
273 unsigned int switch_timer_interval
,
274 unsigned int read_timer_interval
,
275 enum channel_type channel_type
)
277 struct lttng_channel
*chan
;
278 struct lttng_transport
*transport
= NULL
;
280 mutex_lock(&sessions_mutex
);
281 if (session
->been_active
&& channel_type
!= METADATA_CHANNEL
)
282 goto active
; /* Refuse to add channel to active session */
283 transport
= lttng_transport_find(transport_name
);
285 printk(KERN_WARNING
"LTTng transport %s not found\n",
289 if (!try_module_get(transport
->owner
)) {
290 printk(KERN_WARNING
"LTT : Can't lock transport module.\n");
293 chan
= kzalloc(sizeof(struct lttng_channel
), GFP_KERNEL
);
296 chan
->session
= session
;
297 chan
->id
= session
->free_chan_id
++;
298 chan
->ops
= &transport
->ops
;
300 * Note: the channel creation op already writes into the packet
301 * headers. Therefore the "chan" information used as input
302 * should be already accessible.
304 chan
->chan
= transport
->ops
.channel_create(transport_name
,
305 chan
, buf_addr
, subbuf_size
, num_subbuf
,
306 switch_timer_interval
, read_timer_interval
);
310 chan
->transport
= transport
;
311 chan
->channel_type
= channel_type
;
312 list_add(&chan
->list
, &session
->chan
);
313 mutex_unlock(&sessions_mutex
);
320 module_put(transport
->owner
);
323 mutex_unlock(&sessions_mutex
);
328 * Only used internally at session destruction for per-cpu channels, and
329 * when metadata channel is released.
330 * Needs to be called with sessions mutex held.
333 void _lttng_channel_destroy(struct lttng_channel
*chan
)
335 chan
->ops
->channel_destroy(chan
->chan
);
336 module_put(chan
->transport
->owner
);
337 list_del(&chan
->list
);
338 lttng_destroy_context(chan
->ctx
);
342 void lttng_metadata_channel_destroy(struct lttng_channel
*chan
)
344 BUG_ON(chan
->channel_type
!= METADATA_CHANNEL
);
346 /* Protect the metadata cache with the sessions_mutex. */
347 mutex_lock(&sessions_mutex
);
348 _lttng_channel_destroy(chan
);
349 mutex_unlock(&sessions_mutex
);
351 EXPORT_SYMBOL_GPL(lttng_metadata_channel_destroy
);
354 void _lttng_metadata_channel_hangup(struct lttng_metadata_stream
*stream
)
356 stream
->finalized
= 1;
357 wake_up_interruptible(&stream
->read_wait
);
361 * Supports event creation while tracing session is active.
363 struct lttng_event
*lttng_event_create(struct lttng_channel
*chan
,
364 struct lttng_kernel_event
*event_param
,
366 const struct lttng_event_desc
*internal_desc
)
368 struct lttng_event
*event
;
371 mutex_lock(&sessions_mutex
);
372 if (chan
->free_event_id
== -1U) {
377 * This is O(n^2) (for each event, the loop is called at event
378 * creation). Might require a hash if we have lots of events.
380 list_for_each_entry(event
, &chan
->session
->events
, list
) {
381 if (!strcmp(event
->desc
->name
, event_param
->name
)) {
386 event
= kmem_cache_zalloc(event_cache
, GFP_KERNEL
);
392 event
->filter
= filter
;
393 event
->id
= chan
->free_event_id
++;
395 event
->instrumentation
= event_param
->instrumentation
;
396 /* Populate lttng_event structure before tracepoint registration. */
398 switch (event_param
->instrumentation
) {
399 case LTTNG_KERNEL_TRACEPOINT
:
400 event
->desc
= lttng_event_get(event_param
->name
);
405 ret
= lttng_wrapper_tracepoint_probe_register(event
->desc
->kname
,
406 event
->desc
->probe_callback
,
413 case LTTNG_KERNEL_KPROBE
:
414 ret
= lttng_kprobes_register(event_param
->name
,
415 event_param
->u
.kprobe
.symbol_name
,
416 event_param
->u
.kprobe
.offset
,
417 event_param
->u
.kprobe
.addr
,
423 ret
= try_module_get(event
->desc
->owner
);
426 case LTTNG_KERNEL_KRETPROBE
:
428 struct lttng_event
*event_return
;
430 /* kretprobe defines 2 events */
432 kmem_cache_zalloc(event_cache
, GFP_KERNEL
);
437 event_return
->chan
= chan
;
438 event_return
->filter
= filter
;
439 event_return
->id
= chan
->free_event_id
++;
440 event_return
->enabled
= 1;
441 event_return
->instrumentation
= event_param
->instrumentation
;
443 * Populate lttng_event structure before kretprobe registration.
446 ret
= lttng_kretprobes_register(event_param
->name
,
447 event_param
->u
.kretprobe
.symbol_name
,
448 event_param
->u
.kretprobe
.offset
,
449 event_param
->u
.kretprobe
.addr
,
450 event
, event_return
);
452 kmem_cache_free(event_cache
, event_return
);
456 /* Take 2 refs on the module: one per event. */
457 ret
= try_module_get(event
->desc
->owner
);
459 ret
= try_module_get(event
->desc
->owner
);
461 ret
= _lttng_event_metadata_statedump(chan
->session
, chan
,
463 WARN_ON_ONCE(ret
> 0);
465 kmem_cache_free(event_cache
, event_return
);
466 module_put(event
->desc
->owner
);
467 module_put(event
->desc
->owner
);
468 goto statedump_error
;
470 list_add(&event_return
->list
, &chan
->session
->events
);
473 case LTTNG_KERNEL_FUNCTION
:
474 ret
= lttng_ftrace_register(event_param
->name
,
475 event_param
->u
.ftrace
.symbol_name
,
480 ret
= try_module_get(event
->desc
->owner
);
483 case LTTNG_KERNEL_NOOP
:
484 event
->desc
= internal_desc
;
495 ret
= _lttng_event_metadata_statedump(chan
->session
, chan
, event
);
496 WARN_ON_ONCE(ret
> 0);
498 goto statedump_error
;
500 list_add(&event
->list
, &chan
->session
->events
);
501 mutex_unlock(&sessions_mutex
);
505 /* If a statedump error occurs, events will not be readable. */
507 kmem_cache_free(event_cache
, event
);
511 mutex_unlock(&sessions_mutex
);
516 * Only used internally at session destruction.
518 int _lttng_event_unregister(struct lttng_event
*event
)
522 switch (event
->instrumentation
) {
523 case LTTNG_KERNEL_TRACEPOINT
:
524 ret
= lttng_wrapper_tracepoint_probe_unregister(event
->desc
->kname
,
525 event
->desc
->probe_callback
,
530 case LTTNG_KERNEL_KPROBE
:
531 lttng_kprobes_unregister(event
);
534 case LTTNG_KERNEL_KRETPROBE
:
535 lttng_kretprobes_unregister(event
);
538 case LTTNG_KERNEL_FUNCTION
:
539 lttng_ftrace_unregister(event
);
542 case LTTNG_KERNEL_NOOP
:
552 * Only used internally at session destruction.
555 void _lttng_event_destroy(struct lttng_event
*event
)
557 switch (event
->instrumentation
) {
558 case LTTNG_KERNEL_TRACEPOINT
:
559 lttng_event_put(event
->desc
);
561 case LTTNG_KERNEL_KPROBE
:
562 module_put(event
->desc
->owner
);
563 lttng_kprobes_destroy_private(event
);
565 case LTTNG_KERNEL_KRETPROBE
:
566 module_put(event
->desc
->owner
);
567 lttng_kretprobes_destroy_private(event
);
569 case LTTNG_KERNEL_FUNCTION
:
570 module_put(event
->desc
->owner
);
571 lttng_ftrace_destroy_private(event
);
573 case LTTNG_KERNEL_NOOP
:
578 list_del(&event
->list
);
579 lttng_destroy_context(event
->ctx
);
580 kmem_cache_free(event_cache
, event
);
584 * Serialize at most one packet worth of metadata into a metadata
586 * We have exclusive access to our metadata buffer (protected by the
587 * sessions_mutex), so we can do racy operations such as looking for
588 * remaining space left in packet and write, since mutual exclusion
589 * protects us from concurrent writes.
590 * Returns the number of bytes written in the channel, 0 if no data
591 * was written and a negative value on error.
593 int lttng_metadata_output_channel(struct lttng_metadata_stream
*stream
,
594 struct channel
*chan
)
596 struct lib_ring_buffer_ctx ctx
;
598 size_t len
, reserve_len
;
601 * Ensure we support mutiple get_next / put sequences followed
602 * by put_next. The metadata stream lock internally protects
603 * reading the metadata cache. It can indeed be read
604 * concurrently by "get_next_subbuf" and "flush" operations on
605 * the buffer invoked by different processes.
607 mutex_lock(&stream
->lock
);
608 WARN_ON(stream
->metadata_in
< stream
->metadata_out
);
609 if (stream
->metadata_in
!= stream
->metadata_out
)
612 len
= stream
->metadata_cache
->metadata_written
-
616 reserve_len
= min_t(size_t,
617 stream
->transport
->ops
.packet_avail_size(chan
),
619 lib_ring_buffer_ctx_init(&ctx
, chan
, NULL
, reserve_len
,
622 * If reservation failed, return an error to the caller.
624 ret
= stream
->transport
->ops
.event_reserve(&ctx
, 0);
626 printk(KERN_WARNING
"LTTng: Metadata event reservation failed\n");
629 stream
->transport
->ops
.event_write(&ctx
,
630 stream
->metadata_cache
->data
+ stream
->metadata_in
,
632 stream
->transport
->ops
.event_commit(&ctx
);
633 stream
->metadata_in
+= reserve_len
;
637 mutex_unlock(&stream
->lock
);
642 * Write the metadata to the metadata cache.
643 * Must be called with sessions_mutex held.
645 int lttng_metadata_printf(struct lttng_session
*session
,
646 const char *fmt
, ...)
651 struct lttng_metadata_stream
*stream
;
653 WARN_ON_ONCE(!ACCESS_ONCE(session
->active
));
656 str
= kvasprintf(GFP_KERNEL
, fmt
, ap
);
662 if (session
->metadata_cache
->metadata_written
+ len
>
663 session
->metadata_cache
->cache_alloc
) {
664 char *tmp_cache_realloc
;
665 unsigned int tmp_cache_alloc_size
;
667 tmp_cache_alloc_size
= max_t(unsigned int,
668 session
->metadata_cache
->cache_alloc
+ len
,
669 session
->metadata_cache
->cache_alloc
<< 1);
670 tmp_cache_realloc
= krealloc(session
->metadata_cache
->data
,
671 tmp_cache_alloc_size
, GFP_KERNEL
);
672 if (!tmp_cache_realloc
)
674 session
->metadata_cache
->cache_alloc
= tmp_cache_alloc_size
;
675 session
->metadata_cache
->data
= tmp_cache_realloc
;
677 memcpy(session
->metadata_cache
->data
+
678 session
->metadata_cache
->metadata_written
,
680 session
->metadata_cache
->metadata_written
+= len
;
683 list_for_each_entry(stream
, &session
->metadata_cache
->metadata_stream
, list
)
684 wake_up_interruptible(&stream
->read_wait
);
694 * Must be called with sessions_mutex held.
697 int _lttng_field_statedump(struct lttng_session
*session
,
698 const struct lttng_event_field
*field
)
702 switch (field
->type
.atype
) {
704 ret
= lttng_metadata_printf(session
,
705 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n",
706 field
->type
.u
.basic
.integer
.size
,
707 field
->type
.u
.basic
.integer
.alignment
,
708 field
->type
.u
.basic
.integer
.signedness
,
709 (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_none
)
711 : (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
714 field
->type
.u
.basic
.integer
.base
,
716 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
718 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
723 ret
= lttng_metadata_printf(session
,
725 field
->type
.u
.basic
.enumeration
.name
,
730 const struct lttng_basic_type
*elem_type
;
732 elem_type
= &field
->type
.u
.array
.elem_type
;
733 ret
= lttng_metadata_printf(session
,
734 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n",
735 elem_type
->u
.basic
.integer
.size
,
736 elem_type
->u
.basic
.integer
.alignment
,
737 elem_type
->u
.basic
.integer
.signedness
,
738 (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
740 : (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
743 elem_type
->u
.basic
.integer
.base
,
745 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
747 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
749 field
->name
, field
->type
.u
.array
.length
);
754 const struct lttng_basic_type
*elem_type
;
755 const struct lttng_basic_type
*length_type
;
757 elem_type
= &field
->type
.u
.sequence
.elem_type
;
758 length_type
= &field
->type
.u
.sequence
.length_type
;
759 ret
= lttng_metadata_printf(session
,
760 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n",
761 length_type
->u
.basic
.integer
.size
,
762 (unsigned int) length_type
->u
.basic
.integer
.alignment
,
763 length_type
->u
.basic
.integer
.signedness
,
764 (length_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
766 : ((length_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
769 length_type
->u
.basic
.integer
.base
,
771 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
773 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
779 ret
= lttng_metadata_printf(session
,
780 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n",
781 elem_type
->u
.basic
.integer
.size
,
782 (unsigned int) 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;" : "",
801 /* Default encoding is UTF8 */
802 ret
= lttng_metadata_printf(session
,
804 field
->type
.u
.basic
.string
.encoding
== lttng_encode_ASCII
?
805 " { encoding = ASCII; }" : "",
816 int _lttng_context_metadata_statedump(struct lttng_session
*session
,
817 struct lttng_ctx
*ctx
)
824 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
825 const struct lttng_ctx_field
*field
= &ctx
->fields
[i
];
827 ret
= _lttng_field_statedump(session
, &field
->event_field
);
835 int _lttng_fields_metadata_statedump(struct lttng_session
*session
,
836 struct lttng_event
*event
)
838 const struct lttng_event_desc
*desc
= event
->desc
;
842 for (i
= 0; i
< desc
->nr_fields
; i
++) {
843 const struct lttng_event_field
*field
= &desc
->fields
[i
];
845 ret
= _lttng_field_statedump(session
, field
);
853 * Must be called with sessions_mutex held.
856 int _lttng_event_metadata_statedump(struct lttng_session
*session
,
857 struct lttng_channel
*chan
,
858 struct lttng_event
*event
)
862 if (event
->metadata_dumped
|| !ACCESS_ONCE(session
->active
))
864 if (chan
->channel_type
== METADATA_CHANNEL
)
867 ret
= lttng_metadata_printf(session
,
871 " stream_id = %u;\n",
879 ret
= lttng_metadata_printf(session
,
880 " context := struct {\n");
884 ret
= _lttng_context_metadata_statedump(session
, event
->ctx
);
888 ret
= lttng_metadata_printf(session
,
894 ret
= lttng_metadata_printf(session
,
895 " fields := struct {\n"
900 ret
= _lttng_fields_metadata_statedump(session
, event
);
905 * LTTng space reservation can only reserve multiples of the
908 ret
= lttng_metadata_printf(session
,
914 event
->metadata_dumped
= 1;
921 * Must be called with sessions_mutex held.
924 int _lttng_channel_metadata_statedump(struct lttng_session
*session
,
925 struct lttng_channel
*chan
)
929 if (chan
->metadata_dumped
|| !ACCESS_ONCE(session
->active
))
932 if (chan
->channel_type
== METADATA_CHANNEL
)
935 WARN_ON_ONCE(!chan
->header_type
);
936 ret
= lttng_metadata_printf(session
,
939 " event.header := %s;\n"
940 " packet.context := struct packet_context;\n",
942 chan
->header_type
== 1 ? "struct event_header_compact" :
943 "struct event_header_large");
948 ret
= lttng_metadata_printf(session
,
949 " event.context := struct {\n");
953 ret
= _lttng_context_metadata_statedump(session
, chan
->ctx
);
957 ret
= lttng_metadata_printf(session
,
963 ret
= lttng_metadata_printf(session
,
966 chan
->metadata_dumped
= 1;
972 * Must be called with sessions_mutex held.
975 int _lttng_stream_packet_context_declare(struct lttng_session
*session
)
977 return lttng_metadata_printf(session
,
978 "struct packet_context {\n"
979 " uint64_clock_monotonic_t timestamp_begin;\n"
980 " uint64_clock_monotonic_t timestamp_end;\n"
981 " uint64_t content_size;\n"
982 " uint64_t packet_size;\n"
983 " unsigned long events_discarded;\n"
984 " uint32_t cpu_id;\n"
992 * id 31 is reserved to indicate an extended header.
995 * id: range: 0 - 65534.
996 * id 65535 is reserved to indicate an extended header.
998 * Must be called with sessions_mutex held.
1001 int _lttng_event_header_declare(struct lttng_session
*session
)
1003 return lttng_metadata_printf(session
,
1004 "struct event_header_compact {\n"
1005 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
1008 " uint27_clock_monotonic_t timestamp;\n"
1012 " uint64_clock_monotonic_t timestamp;\n"
1017 "struct event_header_large {\n"
1018 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
1021 " uint32_clock_monotonic_t timestamp;\n"
1025 " uint64_clock_monotonic_t timestamp;\n"
1029 lttng_alignof(uint32_t) * CHAR_BIT
,
1030 lttng_alignof(uint16_t) * CHAR_BIT
1035 * Approximation of NTP time of day to clock monotonic correlation,
1036 * taken at start of trace.
1037 * Yes, this is only an approximation. Yes, we can (and will) do better
1038 * in future versions.
1041 uint64_t measure_clock_offset(void)
1043 uint64_t offset
, monotonic
[2], realtime
;
1044 struct timespec rts
= { 0, 0 };
1045 unsigned long flags
;
1047 /* Disable interrupts to increase correlation precision. */
1048 local_irq_save(flags
);
1049 monotonic
[0] = trace_clock_read64();
1050 getnstimeofday(&rts
);
1051 monotonic
[1] = trace_clock_read64();
1052 local_irq_restore(flags
);
1054 offset
= (monotonic
[0] + monotonic
[1]) >> 1;
1055 realtime
= (uint64_t) rts
.tv_sec
* NSEC_PER_SEC
;
1056 realtime
+= rts
.tv_nsec
;
1057 offset
= realtime
- offset
;
1062 * Output metadata into this session's metadata buffers.
1063 * Must be called with sessions_mutex held.
1066 int _lttng_session_metadata_statedump(struct lttng_session
*session
)
1068 unsigned char *uuid_c
= session
->uuid
.b
;
1069 unsigned char uuid_s
[37], clock_uuid_s
[BOOT_ID_LEN
];
1070 struct lttng_channel
*chan
;
1071 struct lttng_event
*event
;
1074 if (!ACCESS_ONCE(session
->active
))
1076 if (session
->metadata_dumped
)
1079 snprintf(uuid_s
, sizeof(uuid_s
),
1080 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
1081 uuid_c
[0], uuid_c
[1], uuid_c
[2], uuid_c
[3],
1082 uuid_c
[4], uuid_c
[5], uuid_c
[6], uuid_c
[7],
1083 uuid_c
[8], uuid_c
[9], uuid_c
[10], uuid_c
[11],
1084 uuid_c
[12], uuid_c
[13], uuid_c
[14], uuid_c
[15]);
1086 ret
= lttng_metadata_printf(session
,
1087 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
1088 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
1089 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
1090 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
1091 "typealias integer { size = %u; align = %u; signed = false; } := unsigned long;\n"
1092 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
1093 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
1099 " byte_order = %s;\n"
1100 " packet.header := struct {\n"
1101 " uint32_t magic;\n"
1102 " uint8_t uuid[16];\n"
1103 " uint32_t stream_id;\n"
1106 lttng_alignof(uint8_t) * CHAR_BIT
,
1107 lttng_alignof(uint16_t) * CHAR_BIT
,
1108 lttng_alignof(uint32_t) * CHAR_BIT
,
1109 lttng_alignof(uint64_t) * CHAR_BIT
,
1110 sizeof(unsigned long) * CHAR_BIT
,
1111 lttng_alignof(unsigned long) * CHAR_BIT
,
1124 ret
= lttng_metadata_printf(session
,
1126 " hostname = \"%s\";\n"
1127 " domain = \"kernel\";\n"
1128 " sysname = \"%s\";\n"
1129 " kernel_release = \"%s\";\n"
1130 " kernel_version = \"%s\";\n"
1131 " tracer_name = \"lttng-modules\";\n"
1132 " tracer_major = %d;\n"
1133 " tracer_minor = %d;\n"
1134 " tracer_patchlevel = %d;\n"
1136 current
->nsproxy
->uts_ns
->name
.nodename
,
1140 LTTNG_MODULES_MAJOR_VERSION
,
1141 LTTNG_MODULES_MINOR_VERSION
,
1142 LTTNG_MODULES_PATCHLEVEL_VERSION
1147 ret
= lttng_metadata_printf(session
,
1155 if (!trace_clock_uuid(clock_uuid_s
)) {
1156 ret
= lttng_metadata_printf(session
,
1157 " uuid = \"%s\";\n",
1164 ret
= lttng_metadata_printf(session
,
1165 " description = \"Monotonic Clock\";\n"
1166 " freq = %llu; /* Frequency, in Hz */\n"
1167 " /* clock value offset from Epoch is: offset * (1/freq) */\n"
1170 (unsigned long long) trace_clock_freq(),
1171 (unsigned long long) measure_clock_offset()
1176 ret
= lttng_metadata_printf(session
,
1177 "typealias integer {\n"
1178 " size = 27; align = 1; signed = false;\n"
1179 " map = clock.monotonic.value;\n"
1180 "} := uint27_clock_monotonic_t;\n"
1182 "typealias integer {\n"
1183 " size = 32; align = %u; signed = false;\n"
1184 " map = clock.monotonic.value;\n"
1185 "} := uint32_clock_monotonic_t;\n"
1187 "typealias integer {\n"
1188 " size = 64; align = %u; signed = false;\n"
1189 " map = clock.monotonic.value;\n"
1190 "} := uint64_clock_monotonic_t;\n\n",
1191 lttng_alignof(uint32_t) * CHAR_BIT
,
1192 lttng_alignof(uint64_t) * CHAR_BIT
1197 ret
= _lttng_stream_packet_context_declare(session
);
1201 ret
= _lttng_event_header_declare(session
);
1206 list_for_each_entry(chan
, &session
->chan
, list
) {
1207 ret
= _lttng_channel_metadata_statedump(session
, chan
);
1212 list_for_each_entry(event
, &session
->events
, list
) {
1213 ret
= _lttng_event_metadata_statedump(session
, event
->chan
, event
);
1217 session
->metadata_dumped
= 1;
1223 * lttng_transport_register - LTT transport registration
1224 * @transport: transport structure
1226 * Registers a transport which can be used as output to extract the data out of
1227 * LTTng. The module calling this registration function must ensure that no
1228 * trap-inducing code will be executed by the transport functions. E.g.
1229 * vmalloc_sync_all() must be called between a vmalloc and the moment the memory
1230 * is made visible to the transport function. This registration acts as a
1231 * vmalloc_sync_all. Therefore, only if the module allocates virtual memory
1232 * after its registration must it synchronize the TLBs.
1234 void lttng_transport_register(struct lttng_transport
*transport
)
1237 * Make sure no page fault can be triggered by the module about to be
1238 * registered. We deal with this here so we don't have to call
1239 * vmalloc_sync_all() in each module's init.
1241 wrapper_vmalloc_sync_all();
1243 mutex_lock(&sessions_mutex
);
1244 list_add_tail(&transport
->node
, <tng_transport_list
);
1245 mutex_unlock(&sessions_mutex
);
1247 EXPORT_SYMBOL_GPL(lttng_transport_register
);
1250 * lttng_transport_unregister - LTT transport unregistration
1251 * @transport: transport structure
1253 void lttng_transport_unregister(struct lttng_transport
*transport
)
1255 mutex_lock(&sessions_mutex
);
1256 list_del(&transport
->node
);
1257 mutex_unlock(&sessions_mutex
);
1259 EXPORT_SYMBOL_GPL(lttng_transport_unregister
);
1261 static int __init
lttng_events_init(void)
1265 ret
= wrapper_lttng_fixup_sig(THIS_MODULE
);
1269 ret
= lttng_tracepoint_init();
1272 event_cache
= KMEM_CACHE(lttng_event
, 0);
1277 ret
= lttng_abi_init();
1280 ret
= lttng_logger_init();
1288 kmem_cache_destroy(event_cache
);
1290 lttng_tracepoint_exit();
1294 module_init(lttng_events_init
);
1296 static void __exit
lttng_events_exit(void)
1298 struct lttng_session
*session
, *tmpsession
;
1300 lttng_logger_exit();
1302 list_for_each_entry_safe(session
, tmpsession
, &sessions
, list
)
1303 lttng_session_destroy(session
);
1304 kmem_cache_destroy(event_cache
);
1305 lttng_tracepoint_exit();
1308 module_exit(lttng_events_exit
);
1310 MODULE_LICENSE("GPL and additional rights");
1311 MODULE_AUTHOR("Mathieu Desnoyers <mathieu.desnoyers@efficios.com>");
1312 MODULE_DESCRIPTION("LTTng Events");
1313 MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION
) "."
1314 __stringify(LTTNG_MODULES_MINOR_VERSION
) "."
1315 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION
)
1316 LTTNG_MODULES_EXTRAVERSION
);