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 mutex_init(&metadata_cache
->lock
);
106 session
->metadata_cache
= metadata_cache
;
107 INIT_LIST_HEAD(&metadata_cache
->metadata_stream
);
108 memcpy(&metadata_cache
->uuid
, &session
->uuid
,
109 sizeof(metadata_cache
->uuid
));
110 list_add(&session
->list
, &sessions
);
111 mutex_unlock(&sessions_mutex
);
115 kfree(metadata_cache
);
119 mutex_unlock(&sessions_mutex
);
123 void metadata_cache_destroy(struct kref
*kref
)
125 struct lttng_metadata_cache
*cache
=
126 container_of(kref
, struct lttng_metadata_cache
, refcount
);
131 void lttng_session_destroy(struct lttng_session
*session
)
133 struct lttng_channel
*chan
, *tmpchan
;
134 struct lttng_event
*event
, *tmpevent
;
135 struct lttng_metadata_stream
*metadata_stream
;
138 mutex_lock(&sessions_mutex
);
139 ACCESS_ONCE(session
->active
) = 0;
140 list_for_each_entry(chan
, &session
->chan
, list
) {
141 ret
= lttng_syscalls_unregister(chan
);
144 list_for_each_entry(event
, &session
->events
, list
) {
145 ret
= _lttng_event_unregister(event
);
148 synchronize_trace(); /* Wait for in-flight events to complete */
149 list_for_each_entry_safe(event
, tmpevent
, &session
->events
, list
)
150 _lttng_event_destroy(event
);
151 list_for_each_entry_safe(chan
, tmpchan
, &session
->chan
, list
) {
152 BUG_ON(chan
->channel_type
== METADATA_CHANNEL
);
153 _lttng_channel_destroy(chan
);
155 list_for_each_entry(metadata_stream
, &session
->metadata_cache
->metadata_stream
, list
)
156 _lttng_metadata_channel_hangup(metadata_stream
);
157 kref_put(&session
->metadata_cache
->refcount
, metadata_cache_destroy
);
158 list_del(&session
->list
);
159 mutex_unlock(&sessions_mutex
);
163 int lttng_session_enable(struct lttng_session
*session
)
166 struct lttng_channel
*chan
;
168 mutex_lock(&sessions_mutex
);
169 if (session
->active
) {
175 * Snapshot the number of events per channel to know the type of header
178 list_for_each_entry(chan
, &session
->chan
, list
) {
179 if (chan
->header_type
)
180 continue; /* don't change it if session stop/restart */
181 if (chan
->free_event_id
< 31)
182 chan
->header_type
= 1; /* compact */
184 chan
->header_type
= 2; /* large */
187 ACCESS_ONCE(session
->active
) = 1;
188 ACCESS_ONCE(session
->been_active
) = 1;
189 ret
= _lttng_session_metadata_statedump(session
);
191 ACCESS_ONCE(session
->active
) = 0;
194 ret
= lttng_statedump_start(session
);
196 ACCESS_ONCE(session
->active
) = 0;
198 mutex_unlock(&sessions_mutex
);
202 int lttng_session_disable(struct lttng_session
*session
)
206 mutex_lock(&sessions_mutex
);
207 if (!session
->active
) {
211 ACCESS_ONCE(session
->active
) = 0;
213 mutex_unlock(&sessions_mutex
);
217 int lttng_channel_enable(struct lttng_channel
*channel
)
221 if (channel
->channel_type
== METADATA_CHANNEL
)
223 old
= xchg(&channel
->enabled
, 1);
229 int lttng_channel_disable(struct lttng_channel
*channel
)
233 if (channel
->channel_type
== METADATA_CHANNEL
)
235 old
= xchg(&channel
->enabled
, 0);
241 int lttng_event_enable(struct lttng_event
*event
)
245 if (event
->chan
->channel_type
== METADATA_CHANNEL
)
247 old
= xchg(&event
->enabled
, 1);
253 int lttng_event_disable(struct lttng_event
*event
)
257 if (event
->chan
->channel_type
== METADATA_CHANNEL
)
259 old
= xchg(&event
->enabled
, 0);
265 static struct lttng_transport
*lttng_transport_find(const char *name
)
267 struct lttng_transport
*transport
;
269 list_for_each_entry(transport
, <tng_transport_list
, node
) {
270 if (!strcmp(transport
->name
, name
))
276 struct lttng_channel
*lttng_channel_create(struct lttng_session
*session
,
277 const char *transport_name
,
279 size_t subbuf_size
, size_t num_subbuf
,
280 unsigned int switch_timer_interval
,
281 unsigned int read_timer_interval
,
282 enum channel_type channel_type
)
284 struct lttng_channel
*chan
;
285 struct lttng_transport
*transport
= NULL
;
287 mutex_lock(&sessions_mutex
);
288 if (session
->been_active
&& channel_type
!= METADATA_CHANNEL
)
289 goto active
; /* Refuse to add channel to active session */
290 transport
= lttng_transport_find(transport_name
);
292 printk(KERN_WARNING
"LTTng transport %s not found\n",
296 if (!try_module_get(transport
->owner
)) {
297 printk(KERN_WARNING
"LTT : Can't lock transport module.\n");
300 chan
= kzalloc(sizeof(struct lttng_channel
), GFP_KERNEL
);
303 chan
->session
= session
;
304 chan
->id
= session
->free_chan_id
++;
305 chan
->ops
= &transport
->ops
;
307 * Note: the channel creation op already writes into the packet
308 * headers. Therefore the "chan" information used as input
309 * should be already accessible.
311 chan
->chan
= transport
->ops
.channel_create(transport_name
,
312 chan
, buf_addr
, subbuf_size
, num_subbuf
,
313 switch_timer_interval
, read_timer_interval
);
317 chan
->transport
= transport
;
318 chan
->channel_type
= channel_type
;
319 list_add(&chan
->list
, &session
->chan
);
320 mutex_unlock(&sessions_mutex
);
327 module_put(transport
->owner
);
330 mutex_unlock(&sessions_mutex
);
335 * Only used internally at session destruction for per-cpu channels, and
336 * when metadata channel is released.
337 * Needs to be called with sessions mutex held.
340 void _lttng_channel_destroy(struct lttng_channel
*chan
)
342 chan
->ops
->channel_destroy(chan
->chan
);
343 module_put(chan
->transport
->owner
);
344 list_del(&chan
->list
);
345 lttng_destroy_context(chan
->ctx
);
349 void lttng_metadata_channel_destroy(struct lttng_channel
*chan
)
351 BUG_ON(chan
->channel_type
!= METADATA_CHANNEL
);
353 /* Protect the metadata cache with the sessions_mutex. */
354 mutex_lock(&sessions_mutex
);
355 _lttng_channel_destroy(chan
);
356 mutex_unlock(&sessions_mutex
);
358 EXPORT_SYMBOL_GPL(lttng_metadata_channel_destroy
);
361 void _lttng_metadata_channel_hangup(struct lttng_metadata_stream
*stream
)
363 stream
->finalized
= 1;
364 wake_up_interruptible(&stream
->read_wait
);
368 * Supports event creation while tracing session is active.
370 struct lttng_event
*lttng_event_create(struct lttng_channel
*chan
,
371 struct lttng_kernel_event
*event_param
,
373 const struct lttng_event_desc
*internal_desc
)
375 struct lttng_event
*event
;
378 mutex_lock(&sessions_mutex
);
379 if (chan
->free_event_id
== -1U) {
384 * This is O(n^2) (for each event, the loop is called at event
385 * creation). Might require a hash if we have lots of events.
387 list_for_each_entry(event
, &chan
->session
->events
, list
) {
388 if (!strcmp(event
->desc
->name
, event_param
->name
)) {
390 * Allow events with the same name to appear in
391 * different channels.
393 if (event
->chan
== chan
) {
399 event
= kmem_cache_zalloc(event_cache
, GFP_KERNEL
);
405 event
->filter
= filter
;
406 event
->id
= chan
->free_event_id
++;
408 event
->instrumentation
= event_param
->instrumentation
;
409 /* Populate lttng_event structure before tracepoint registration. */
411 switch (event_param
->instrumentation
) {
412 case LTTNG_KERNEL_TRACEPOINT
:
413 event
->desc
= lttng_event_get(event_param
->name
);
418 ret
= lttng_wrapper_tracepoint_probe_register(event
->desc
->kname
,
419 event
->desc
->probe_callback
,
426 case LTTNG_KERNEL_KPROBE
:
427 ret
= lttng_kprobes_register(event_param
->name
,
428 event_param
->u
.kprobe
.symbol_name
,
429 event_param
->u
.kprobe
.offset
,
430 event_param
->u
.kprobe
.addr
,
436 ret
= try_module_get(event
->desc
->owner
);
439 case LTTNG_KERNEL_KRETPROBE
:
441 struct lttng_event
*event_return
;
443 /* kretprobe defines 2 events */
445 kmem_cache_zalloc(event_cache
, GFP_KERNEL
);
450 event_return
->chan
= chan
;
451 event_return
->filter
= filter
;
452 event_return
->id
= chan
->free_event_id
++;
453 event_return
->enabled
= 1;
454 event_return
->instrumentation
= event_param
->instrumentation
;
456 * Populate lttng_event structure before kretprobe registration.
459 ret
= lttng_kretprobes_register(event_param
->name
,
460 event_param
->u
.kretprobe
.symbol_name
,
461 event_param
->u
.kretprobe
.offset
,
462 event_param
->u
.kretprobe
.addr
,
463 event
, event_return
);
465 kmem_cache_free(event_cache
, event_return
);
469 /* Take 2 refs on the module: one per event. */
470 ret
= try_module_get(event
->desc
->owner
);
472 ret
= try_module_get(event
->desc
->owner
);
474 ret
= _lttng_event_metadata_statedump(chan
->session
, chan
,
476 WARN_ON_ONCE(ret
> 0);
478 kmem_cache_free(event_cache
, event_return
);
479 module_put(event
->desc
->owner
);
480 module_put(event
->desc
->owner
);
481 goto statedump_error
;
483 list_add(&event_return
->list
, &chan
->session
->events
);
486 case LTTNG_KERNEL_FUNCTION
:
487 ret
= lttng_ftrace_register(event_param
->name
,
488 event_param
->u
.ftrace
.symbol_name
,
493 ret
= try_module_get(event
->desc
->owner
);
496 case LTTNG_KERNEL_NOOP
:
497 event
->desc
= internal_desc
;
508 ret
= _lttng_event_metadata_statedump(chan
->session
, chan
, event
);
509 WARN_ON_ONCE(ret
> 0);
511 goto statedump_error
;
513 list_add(&event
->list
, &chan
->session
->events
);
514 mutex_unlock(&sessions_mutex
);
518 /* If a statedump error occurs, events will not be readable. */
520 kmem_cache_free(event_cache
, event
);
524 mutex_unlock(&sessions_mutex
);
529 * Only used internally at session destruction.
531 int _lttng_event_unregister(struct lttng_event
*event
)
535 switch (event
->instrumentation
) {
536 case LTTNG_KERNEL_TRACEPOINT
:
537 ret
= lttng_wrapper_tracepoint_probe_unregister(event
->desc
->kname
,
538 event
->desc
->probe_callback
,
543 case LTTNG_KERNEL_KPROBE
:
544 lttng_kprobes_unregister(event
);
547 case LTTNG_KERNEL_KRETPROBE
:
548 lttng_kretprobes_unregister(event
);
551 case LTTNG_KERNEL_FUNCTION
:
552 lttng_ftrace_unregister(event
);
555 case LTTNG_KERNEL_NOOP
:
565 * Only used internally at session destruction.
568 void _lttng_event_destroy(struct lttng_event
*event
)
570 switch (event
->instrumentation
) {
571 case LTTNG_KERNEL_TRACEPOINT
:
572 lttng_event_put(event
->desc
);
574 case LTTNG_KERNEL_KPROBE
:
575 module_put(event
->desc
->owner
);
576 lttng_kprobes_destroy_private(event
);
578 case LTTNG_KERNEL_KRETPROBE
:
579 module_put(event
->desc
->owner
);
580 lttng_kretprobes_destroy_private(event
);
582 case LTTNG_KERNEL_FUNCTION
:
583 module_put(event
->desc
->owner
);
584 lttng_ftrace_destroy_private(event
);
586 case LTTNG_KERNEL_NOOP
:
591 list_del(&event
->list
);
592 lttng_destroy_context(event
->ctx
);
593 kmem_cache_free(event_cache
, event
);
597 * Serialize at most one packet worth of metadata into a metadata
599 * We grab the metadata cache mutex to get exclusive access to our metadata
600 * buffer and to the metadata cache. Exclusive access to the metadata buffer
601 * allows us to do racy operations such as looking for remaining space left in
602 * packet and write, since mutual exclusion protects us from concurrent writes.
603 * Mutual exclusion on the metadata cache allow us to read the cache content
604 * without racing against reallocation of the cache by updates.
605 * Returns the number of bytes written in the channel, 0 if no data
606 * was written and a negative value on error.
608 int lttng_metadata_output_channel(struct lttng_metadata_stream
*stream
,
609 struct channel
*chan
)
611 struct lib_ring_buffer_ctx ctx
;
613 size_t len
, reserve_len
;
616 * Ensure we support mutiple get_next / put sequences followed by
617 * put_next. The metadata cache lock protects reading the metadata
618 * cache. It can indeed be read concurrently by "get_next_subbuf" and
619 * "flush" operations on the buffer invoked by different processes.
620 * Moreover, since the metadata cache memory can be reallocated, we
621 * need to have exclusive access against updates even though we only
624 mutex_lock(&stream
->metadata_cache
->lock
);
625 WARN_ON(stream
->metadata_in
< stream
->metadata_out
);
626 if (stream
->metadata_in
!= stream
->metadata_out
)
629 len
= stream
->metadata_cache
->metadata_written
-
633 reserve_len
= min_t(size_t,
634 stream
->transport
->ops
.packet_avail_size(chan
),
636 lib_ring_buffer_ctx_init(&ctx
, chan
, NULL
, reserve_len
,
639 * If reservation failed, return an error to the caller.
641 ret
= stream
->transport
->ops
.event_reserve(&ctx
, 0);
643 printk(KERN_WARNING
"LTTng: Metadata event reservation failed\n");
646 stream
->transport
->ops
.event_write(&ctx
,
647 stream
->metadata_cache
->data
+ stream
->metadata_in
,
649 stream
->transport
->ops
.event_commit(&ctx
);
650 stream
->metadata_in
+= reserve_len
;
654 mutex_unlock(&stream
->metadata_cache
->lock
);
659 * Write the metadata to the metadata cache.
660 * Must be called with sessions_mutex held.
661 * The metadata cache lock protects us from concurrent read access from
662 * thread outputting metadata content to ring buffer.
664 int lttng_metadata_printf(struct lttng_session
*session
,
665 const char *fmt
, ...)
670 struct lttng_metadata_stream
*stream
;
672 WARN_ON_ONCE(!ACCESS_ONCE(session
->active
));
675 str
= kvasprintf(GFP_KERNEL
, fmt
, ap
);
681 mutex_lock(&session
->metadata_cache
->lock
);
682 if (session
->metadata_cache
->metadata_written
+ len
>
683 session
->metadata_cache
->cache_alloc
) {
684 char *tmp_cache_realloc
;
685 unsigned int tmp_cache_alloc_size
;
687 tmp_cache_alloc_size
= max_t(unsigned int,
688 session
->metadata_cache
->cache_alloc
+ len
,
689 session
->metadata_cache
->cache_alloc
<< 1);
690 tmp_cache_realloc
= krealloc(session
->metadata_cache
->data
,
691 tmp_cache_alloc_size
, GFP_KERNEL
);
692 if (!tmp_cache_realloc
)
694 session
->metadata_cache
->cache_alloc
= tmp_cache_alloc_size
;
695 session
->metadata_cache
->data
= tmp_cache_realloc
;
697 memcpy(session
->metadata_cache
->data
+
698 session
->metadata_cache
->metadata_written
,
700 session
->metadata_cache
->metadata_written
+= len
;
701 mutex_unlock(&session
->metadata_cache
->lock
);
704 list_for_each_entry(stream
, &session
->metadata_cache
->metadata_stream
, list
)
705 wake_up_interruptible(&stream
->read_wait
);
710 mutex_unlock(&session
->metadata_cache
->lock
);
716 * Must be called with sessions_mutex held.
719 int _lttng_field_statedump(struct lttng_session
*session
,
720 const struct lttng_event_field
*field
)
724 switch (field
->type
.atype
) {
726 ret
= lttng_metadata_printf(session
,
727 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n",
728 field
->type
.u
.basic
.integer
.size
,
729 field
->type
.u
.basic
.integer
.alignment
,
730 field
->type
.u
.basic
.integer
.signedness
,
731 (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_none
)
733 : (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
736 field
->type
.u
.basic
.integer
.base
,
738 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
740 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
745 ret
= lttng_metadata_printf(session
,
747 field
->type
.u
.basic
.enumeration
.name
,
752 const struct lttng_basic_type
*elem_type
;
754 elem_type
= &field
->type
.u
.array
.elem_type
;
755 ret
= lttng_metadata_printf(session
,
756 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n",
757 elem_type
->u
.basic
.integer
.size
,
758 elem_type
->u
.basic
.integer
.alignment
,
759 elem_type
->u
.basic
.integer
.signedness
,
760 (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
762 : (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
765 elem_type
->u
.basic
.integer
.base
,
767 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
769 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
771 field
->name
, field
->type
.u
.array
.length
);
776 const struct lttng_basic_type
*elem_type
;
777 const struct lttng_basic_type
*length_type
;
779 elem_type
= &field
->type
.u
.sequence
.elem_type
;
780 length_type
= &field
->type
.u
.sequence
.length_type
;
781 ret
= lttng_metadata_printf(session
,
782 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n",
783 length_type
->u
.basic
.integer
.size
,
784 (unsigned int) length_type
->u
.basic
.integer
.alignment
,
785 length_type
->u
.basic
.integer
.signedness
,
786 (length_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
788 : ((length_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
791 length_type
->u
.basic
.integer
.base
,
793 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
795 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
801 ret
= lttng_metadata_printf(session
,
802 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n",
803 elem_type
->u
.basic
.integer
.size
,
804 (unsigned int) elem_type
->u
.basic
.integer
.alignment
,
805 elem_type
->u
.basic
.integer
.signedness
,
806 (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
808 : ((elem_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
811 elem_type
->u
.basic
.integer
.base
,
813 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
815 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
823 /* Default encoding is UTF8 */
824 ret
= lttng_metadata_printf(session
,
826 field
->type
.u
.basic
.string
.encoding
== lttng_encode_ASCII
?
827 " { encoding = ASCII; }" : "",
838 int _lttng_context_metadata_statedump(struct lttng_session
*session
,
839 struct lttng_ctx
*ctx
)
846 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
847 const struct lttng_ctx_field
*field
= &ctx
->fields
[i
];
849 ret
= _lttng_field_statedump(session
, &field
->event_field
);
857 int _lttng_fields_metadata_statedump(struct lttng_session
*session
,
858 struct lttng_event
*event
)
860 const struct lttng_event_desc
*desc
= event
->desc
;
864 for (i
= 0; i
< desc
->nr_fields
; i
++) {
865 const struct lttng_event_field
*field
= &desc
->fields
[i
];
867 ret
= _lttng_field_statedump(session
, field
);
875 * Must be called with sessions_mutex held.
878 int _lttng_event_metadata_statedump(struct lttng_session
*session
,
879 struct lttng_channel
*chan
,
880 struct lttng_event
*event
)
884 if (event
->metadata_dumped
|| !ACCESS_ONCE(session
->active
))
886 if (chan
->channel_type
== METADATA_CHANNEL
)
889 ret
= lttng_metadata_printf(session
,
893 " stream_id = %u;\n",
901 ret
= lttng_metadata_printf(session
,
902 " context := struct {\n");
906 ret
= _lttng_context_metadata_statedump(session
, event
->ctx
);
910 ret
= lttng_metadata_printf(session
,
916 ret
= lttng_metadata_printf(session
,
917 " fields := struct {\n"
922 ret
= _lttng_fields_metadata_statedump(session
, event
);
927 * LTTng space reservation can only reserve multiples of the
930 ret
= lttng_metadata_printf(session
,
936 event
->metadata_dumped
= 1;
943 * Must be called with sessions_mutex held.
946 int _lttng_channel_metadata_statedump(struct lttng_session
*session
,
947 struct lttng_channel
*chan
)
951 if (chan
->metadata_dumped
|| !ACCESS_ONCE(session
->active
))
954 if (chan
->channel_type
== METADATA_CHANNEL
)
957 WARN_ON_ONCE(!chan
->header_type
);
958 ret
= lttng_metadata_printf(session
,
961 " event.header := %s;\n"
962 " packet.context := struct packet_context;\n",
964 chan
->header_type
== 1 ? "struct event_header_compact" :
965 "struct event_header_large");
970 ret
= lttng_metadata_printf(session
,
971 " event.context := struct {\n");
975 ret
= _lttng_context_metadata_statedump(session
, chan
->ctx
);
979 ret
= lttng_metadata_printf(session
,
985 ret
= lttng_metadata_printf(session
,
988 chan
->metadata_dumped
= 1;
994 * Must be called with sessions_mutex held.
997 int _lttng_stream_packet_context_declare(struct lttng_session
*session
)
999 return lttng_metadata_printf(session
,
1000 "struct packet_context {\n"
1001 " uint64_clock_monotonic_t timestamp_begin;\n"
1002 " uint64_clock_monotonic_t timestamp_end;\n"
1003 " uint64_t content_size;\n"
1004 " uint64_t packet_size;\n"
1005 " unsigned long events_discarded;\n"
1006 " uint32_t cpu_id;\n"
1013 * id: range: 0 - 30.
1014 * id 31 is reserved to indicate an extended header.
1017 * id: range: 0 - 65534.
1018 * id 65535 is reserved to indicate an extended header.
1020 * Must be called with sessions_mutex held.
1023 int _lttng_event_header_declare(struct lttng_session
*session
)
1025 return lttng_metadata_printf(session
,
1026 "struct event_header_compact {\n"
1027 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
1030 " uint27_clock_monotonic_t timestamp;\n"
1034 " uint64_clock_monotonic_t timestamp;\n"
1039 "struct event_header_large {\n"
1040 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
1043 " uint32_clock_monotonic_t timestamp;\n"
1047 " uint64_clock_monotonic_t timestamp;\n"
1051 lttng_alignof(uint32_t) * CHAR_BIT
,
1052 lttng_alignof(uint16_t) * CHAR_BIT
1057 * Approximation of NTP time of day to clock monotonic correlation,
1058 * taken at start of trace.
1059 * Yes, this is only an approximation. Yes, we can (and will) do better
1060 * in future versions.
1063 uint64_t measure_clock_offset(void)
1065 uint64_t offset
, monotonic
[2], realtime
;
1066 struct timespec rts
= { 0, 0 };
1067 unsigned long flags
;
1069 /* Disable interrupts to increase correlation precision. */
1070 local_irq_save(flags
);
1071 monotonic
[0] = trace_clock_read64();
1072 getnstimeofday(&rts
);
1073 monotonic
[1] = trace_clock_read64();
1074 local_irq_restore(flags
);
1076 offset
= (monotonic
[0] + monotonic
[1]) >> 1;
1077 realtime
= (uint64_t) rts
.tv_sec
* NSEC_PER_SEC
;
1078 realtime
+= rts
.tv_nsec
;
1079 offset
= realtime
- offset
;
1084 * Output metadata into this session's metadata buffers.
1085 * Must be called with sessions_mutex held.
1088 int _lttng_session_metadata_statedump(struct lttng_session
*session
)
1090 unsigned char *uuid_c
= session
->uuid
.b
;
1091 unsigned char uuid_s
[37], clock_uuid_s
[BOOT_ID_LEN
];
1092 struct lttng_channel
*chan
;
1093 struct lttng_event
*event
;
1096 if (!ACCESS_ONCE(session
->active
))
1098 if (session
->metadata_dumped
)
1101 snprintf(uuid_s
, sizeof(uuid_s
),
1102 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
1103 uuid_c
[0], uuid_c
[1], uuid_c
[2], uuid_c
[3],
1104 uuid_c
[4], uuid_c
[5], uuid_c
[6], uuid_c
[7],
1105 uuid_c
[8], uuid_c
[9], uuid_c
[10], uuid_c
[11],
1106 uuid_c
[12], uuid_c
[13], uuid_c
[14], uuid_c
[15]);
1108 ret
= lttng_metadata_printf(session
,
1109 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
1110 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
1111 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
1112 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
1113 "typealias integer { size = %u; align = %u; signed = false; } := unsigned long;\n"
1114 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
1115 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
1121 " byte_order = %s;\n"
1122 " packet.header := struct {\n"
1123 " uint32_t magic;\n"
1124 " uint8_t uuid[16];\n"
1125 " uint32_t stream_id;\n"
1128 lttng_alignof(uint8_t) * CHAR_BIT
,
1129 lttng_alignof(uint16_t) * CHAR_BIT
,
1130 lttng_alignof(uint32_t) * CHAR_BIT
,
1131 lttng_alignof(uint64_t) * CHAR_BIT
,
1132 sizeof(unsigned long) * CHAR_BIT
,
1133 lttng_alignof(unsigned long) * CHAR_BIT
,
1146 ret
= lttng_metadata_printf(session
,
1148 " hostname = \"%s\";\n"
1149 " domain = \"kernel\";\n"
1150 " sysname = \"%s\";\n"
1151 " kernel_release = \"%s\";\n"
1152 " kernel_version = \"%s\";\n"
1153 " tracer_name = \"lttng-modules\";\n"
1154 " tracer_major = %d;\n"
1155 " tracer_minor = %d;\n"
1156 " tracer_patchlevel = %d;\n"
1158 current
->nsproxy
->uts_ns
->name
.nodename
,
1162 LTTNG_MODULES_MAJOR_VERSION
,
1163 LTTNG_MODULES_MINOR_VERSION
,
1164 LTTNG_MODULES_PATCHLEVEL_VERSION
1169 ret
= lttng_metadata_printf(session
,
1177 if (!trace_clock_uuid(clock_uuid_s
)) {
1178 ret
= lttng_metadata_printf(session
,
1179 " uuid = \"%s\";\n",
1186 ret
= lttng_metadata_printf(session
,
1187 " description = \"Monotonic Clock\";\n"
1188 " freq = %llu; /* Frequency, in Hz */\n"
1189 " /* clock value offset from Epoch is: offset * (1/freq) */\n"
1192 (unsigned long long) trace_clock_freq(),
1193 (unsigned long long) measure_clock_offset()
1198 ret
= lttng_metadata_printf(session
,
1199 "typealias integer {\n"
1200 " size = 27; align = 1; signed = false;\n"
1201 " map = clock.monotonic.value;\n"
1202 "} := uint27_clock_monotonic_t;\n"
1204 "typealias integer {\n"
1205 " size = 32; align = %u; signed = false;\n"
1206 " map = clock.monotonic.value;\n"
1207 "} := uint32_clock_monotonic_t;\n"
1209 "typealias integer {\n"
1210 " size = 64; align = %u; signed = false;\n"
1211 " map = clock.monotonic.value;\n"
1212 "} := uint64_clock_monotonic_t;\n\n",
1213 lttng_alignof(uint32_t) * CHAR_BIT
,
1214 lttng_alignof(uint64_t) * CHAR_BIT
1219 ret
= _lttng_stream_packet_context_declare(session
);
1223 ret
= _lttng_event_header_declare(session
);
1228 list_for_each_entry(chan
, &session
->chan
, list
) {
1229 ret
= _lttng_channel_metadata_statedump(session
, chan
);
1234 list_for_each_entry(event
, &session
->events
, list
) {
1235 ret
= _lttng_event_metadata_statedump(session
, event
->chan
, event
);
1239 session
->metadata_dumped
= 1;
1245 * lttng_transport_register - LTT transport registration
1246 * @transport: transport structure
1248 * Registers a transport which can be used as output to extract the data out of
1249 * LTTng. The module calling this registration function must ensure that no
1250 * trap-inducing code will be executed by the transport functions. E.g.
1251 * vmalloc_sync_all() must be called between a vmalloc and the moment the memory
1252 * is made visible to the transport function. This registration acts as a
1253 * vmalloc_sync_all. Therefore, only if the module allocates virtual memory
1254 * after its registration must it synchronize the TLBs.
1256 void lttng_transport_register(struct lttng_transport
*transport
)
1259 * Make sure no page fault can be triggered by the module about to be
1260 * registered. We deal with this here so we don't have to call
1261 * vmalloc_sync_all() in each module's init.
1263 wrapper_vmalloc_sync_all();
1265 mutex_lock(&sessions_mutex
);
1266 list_add_tail(&transport
->node
, <tng_transport_list
);
1267 mutex_unlock(&sessions_mutex
);
1269 EXPORT_SYMBOL_GPL(lttng_transport_register
);
1272 * lttng_transport_unregister - LTT transport unregistration
1273 * @transport: transport structure
1275 void lttng_transport_unregister(struct lttng_transport
*transport
)
1277 mutex_lock(&sessions_mutex
);
1278 list_del(&transport
->node
);
1279 mutex_unlock(&sessions_mutex
);
1281 EXPORT_SYMBOL_GPL(lttng_transport_unregister
);
1283 static int __init
lttng_events_init(void)
1287 ret
= wrapper_lttng_fixup_sig(THIS_MODULE
);
1290 ret
= wrapper_get_pfnblock_flags_mask_init();
1293 ret
= lttng_tracepoint_init();
1296 event_cache
= KMEM_CACHE(lttng_event
, 0);
1301 ret
= lttng_abi_init();
1304 ret
= lttng_logger_init();
1312 kmem_cache_destroy(event_cache
);
1314 lttng_tracepoint_exit();
1318 module_init(lttng_events_init
);
1320 static void __exit
lttng_events_exit(void)
1322 struct lttng_session
*session
, *tmpsession
;
1324 lttng_logger_exit();
1326 list_for_each_entry_safe(session
, tmpsession
, &sessions
, list
)
1327 lttng_session_destroy(session
);
1328 kmem_cache_destroy(event_cache
);
1329 lttng_tracepoint_exit();
1332 module_exit(lttng_events_exit
);
1334 MODULE_LICENSE("GPL and additional rights");
1335 MODULE_AUTHOR("Mathieu Desnoyers <mathieu.desnoyers@efficios.com>");
1336 MODULE_DESCRIPTION("LTTng Events");
1337 MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION
) "."
1338 __stringify(LTTNG_MODULES_MINOR_VERSION
) "."
1339 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION
)
1340 LTTNG_MODULES_EXTRAVERSION
);