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 <linux/vmalloc.h>
33 #include "wrapper/uuid.h"
34 #include "wrapper/vmalloc.h" /* for wrapper_vmalloc_sync_all() */
35 #include "wrapper/random.h"
36 #include "wrapper/tracepoint.h"
37 #include "lttng-kernel-version.h"
38 #include "lttng-events.h"
39 #include "lttng-tracer.h"
40 #include "lttng-abi-old.h"
42 #define METADATA_CACHE_DEFAULT_SIZE 4096
44 static LIST_HEAD(sessions
);
45 static LIST_HEAD(lttng_transport_list
);
47 * Protect the sessions and metadata caches.
49 static DEFINE_MUTEX(sessions_mutex
);
50 static struct kmem_cache
*event_cache
;
52 static void _lttng_event_destroy(struct lttng_event
*event
);
53 static void _lttng_channel_destroy(struct lttng_channel
*chan
);
54 static int _lttng_event_unregister(struct lttng_event
*event
);
56 int _lttng_event_metadata_statedump(struct lttng_session
*session
,
57 struct lttng_channel
*chan
,
58 struct lttng_event
*event
);
60 int _lttng_session_metadata_statedump(struct lttng_session
*session
);
62 void _lttng_metadata_channel_hangup(struct lttng_metadata_stream
*stream
);
64 void synchronize_trace(void)
67 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0))
68 #ifdef CONFIG_PREEMPT_RT_FULL
71 #else /* (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)) */
72 #ifdef CONFIG_PREEMPT_RT
75 #endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)) */
78 struct lttng_session
*lttng_session_create(void)
80 struct lttng_session
*session
;
81 struct lttng_metadata_cache
*metadata_cache
;
83 mutex_lock(&sessions_mutex
);
84 session
= kzalloc(sizeof(struct lttng_session
), GFP_KERNEL
);
87 INIT_LIST_HEAD(&session
->chan
);
88 INIT_LIST_HEAD(&session
->events
);
89 uuid_le_gen(&session
->uuid
);
91 metadata_cache
= kzalloc(sizeof(struct lttng_metadata_cache
),
94 goto err_free_session
;
95 metadata_cache
->data
= vzalloc(METADATA_CACHE_DEFAULT_SIZE
);
96 if (!metadata_cache
->data
)
98 metadata_cache
->cache_alloc
= METADATA_CACHE_DEFAULT_SIZE
;
99 kref_init(&metadata_cache
->refcount
);
100 mutex_init(&metadata_cache
->lock
);
101 session
->metadata_cache
= metadata_cache
;
102 INIT_LIST_HEAD(&metadata_cache
->metadata_stream
);
103 memcpy(&metadata_cache
->uuid
, &session
->uuid
,
104 sizeof(metadata_cache
->uuid
));
105 list_add(&session
->list
, &sessions
);
106 mutex_unlock(&sessions_mutex
);
110 kfree(metadata_cache
);
114 mutex_unlock(&sessions_mutex
);
118 void metadata_cache_destroy(struct kref
*kref
)
120 struct lttng_metadata_cache
*cache
=
121 container_of(kref
, struct lttng_metadata_cache
, refcount
);
126 void lttng_session_destroy(struct lttng_session
*session
)
128 struct lttng_channel
*chan
, *tmpchan
;
129 struct lttng_event
*event
, *tmpevent
;
130 struct lttng_metadata_stream
*metadata_stream
;
133 mutex_lock(&sessions_mutex
);
134 ACCESS_ONCE(session
->active
) = 0;
135 list_for_each_entry(chan
, &session
->chan
, list
) {
136 ret
= lttng_syscalls_unregister(chan
);
139 list_for_each_entry(event
, &session
->events
, list
) {
140 ret
= _lttng_event_unregister(event
);
143 synchronize_trace(); /* Wait for in-flight events to complete */
144 list_for_each_entry_safe(event
, tmpevent
, &session
->events
, list
)
145 _lttng_event_destroy(event
);
146 list_for_each_entry_safe(chan
, tmpchan
, &session
->chan
, list
) {
147 BUG_ON(chan
->channel_type
== METADATA_CHANNEL
);
148 _lttng_channel_destroy(chan
);
150 list_for_each_entry(metadata_stream
, &session
->metadata_cache
->metadata_stream
, list
)
151 _lttng_metadata_channel_hangup(metadata_stream
);
152 kref_put(&session
->metadata_cache
->refcount
, metadata_cache_destroy
);
153 list_del(&session
->list
);
154 mutex_unlock(&sessions_mutex
);
158 int lttng_session_enable(struct lttng_session
*session
)
161 struct lttng_channel
*chan
;
163 mutex_lock(&sessions_mutex
);
164 if (session
->active
) {
170 * Snapshot the number of events per channel to know the type of header
173 list_for_each_entry(chan
, &session
->chan
, list
) {
174 if (chan
->header_type
)
175 continue; /* don't change it if session stop/restart */
176 if (chan
->free_event_id
< 31)
177 chan
->header_type
= 1; /* compact */
179 chan
->header_type
= 2; /* large */
182 ACCESS_ONCE(session
->active
) = 1;
183 ACCESS_ONCE(session
->been_active
) = 1;
184 ret
= _lttng_session_metadata_statedump(session
);
186 ACCESS_ONCE(session
->active
) = 0;
189 ret
= lttng_statedump_start(session
);
191 ACCESS_ONCE(session
->active
) = 0;
193 mutex_unlock(&sessions_mutex
);
197 int lttng_session_disable(struct lttng_session
*session
)
201 mutex_lock(&sessions_mutex
);
202 if (!session
->active
) {
206 ACCESS_ONCE(session
->active
) = 0;
208 mutex_unlock(&sessions_mutex
);
212 int lttng_channel_enable(struct lttng_channel
*channel
)
216 if (channel
->channel_type
== METADATA_CHANNEL
)
218 old
= xchg(&channel
->enabled
, 1);
224 int lttng_channel_disable(struct lttng_channel
*channel
)
228 if (channel
->channel_type
== METADATA_CHANNEL
)
230 old
= xchg(&channel
->enabled
, 0);
236 int lttng_event_enable(struct lttng_event
*event
)
240 if (event
->chan
->channel_type
== METADATA_CHANNEL
)
242 old
= xchg(&event
->enabled
, 1);
248 int lttng_event_disable(struct lttng_event
*event
)
252 if (event
->chan
->channel_type
== METADATA_CHANNEL
)
254 old
= xchg(&event
->enabled
, 0);
260 static struct lttng_transport
*lttng_transport_find(const char *name
)
262 struct lttng_transport
*transport
;
264 list_for_each_entry(transport
, <tng_transport_list
, node
) {
265 if (!strcmp(transport
->name
, name
))
271 struct lttng_channel
*lttng_channel_create(struct lttng_session
*session
,
272 const char *transport_name
,
274 size_t subbuf_size
, size_t num_subbuf
,
275 unsigned int switch_timer_interval
,
276 unsigned int read_timer_interval
,
277 enum channel_type channel_type
)
279 struct lttng_channel
*chan
;
280 struct lttng_transport
*transport
= NULL
;
282 mutex_lock(&sessions_mutex
);
283 if (session
->been_active
&& channel_type
!= METADATA_CHANNEL
)
284 goto active
; /* Refuse to add channel to active session */
285 transport
= lttng_transport_find(transport_name
);
287 printk(KERN_WARNING
"LTTng transport %s not found\n",
291 if (!try_module_get(transport
->owner
)) {
292 printk(KERN_WARNING
"LTT : Can't lock transport module.\n");
295 chan
= kzalloc(sizeof(struct lttng_channel
), GFP_KERNEL
);
298 chan
->session
= session
;
299 chan
->id
= session
->free_chan_id
++;
300 chan
->ops
= &transport
->ops
;
302 * Note: the channel creation op already writes into the packet
303 * headers. Therefore the "chan" information used as input
304 * should be already accessible.
306 chan
->chan
= transport
->ops
.channel_create(transport_name
,
307 chan
, buf_addr
, subbuf_size
, num_subbuf
,
308 switch_timer_interval
, read_timer_interval
);
312 chan
->transport
= transport
;
313 chan
->channel_type
= channel_type
;
314 list_add(&chan
->list
, &session
->chan
);
315 mutex_unlock(&sessions_mutex
);
322 module_put(transport
->owner
);
325 mutex_unlock(&sessions_mutex
);
330 * Only used internally at session destruction for per-cpu channels, and
331 * when metadata channel is released.
332 * Needs to be called with sessions mutex held.
335 void _lttng_channel_destroy(struct lttng_channel
*chan
)
337 chan
->ops
->channel_destroy(chan
->chan
);
338 module_put(chan
->transport
->owner
);
339 list_del(&chan
->list
);
340 lttng_destroy_context(chan
->ctx
);
344 void lttng_metadata_channel_destroy(struct lttng_channel
*chan
)
346 BUG_ON(chan
->channel_type
!= METADATA_CHANNEL
);
348 /* Protect the metadata cache with the sessions_mutex. */
349 mutex_lock(&sessions_mutex
);
350 _lttng_channel_destroy(chan
);
351 mutex_unlock(&sessions_mutex
);
353 EXPORT_SYMBOL_GPL(lttng_metadata_channel_destroy
);
356 void _lttng_metadata_channel_hangup(struct lttng_metadata_stream
*stream
)
358 stream
->finalized
= 1;
359 wake_up_interruptible(&stream
->read_wait
);
363 * Supports event creation while tracing session is active.
365 struct lttng_event
*lttng_event_create(struct lttng_channel
*chan
,
366 struct lttng_kernel_event
*event_param
,
368 const struct lttng_event_desc
*internal_desc
)
370 struct lttng_event
*event
;
373 mutex_lock(&sessions_mutex
);
374 if (chan
->free_event_id
== -1U) {
379 * This is O(n^2) (for each event, the loop is called at event
380 * creation). Might require a hash if we have lots of events.
382 list_for_each_entry(event
, &chan
->session
->events
, list
) {
383 if (!strcmp(event
->desc
->name
, event_param
->name
)) {
388 event
= kmem_cache_zalloc(event_cache
, GFP_KERNEL
);
394 event
->filter
= filter
;
395 event
->id
= chan
->free_event_id
++;
397 event
->instrumentation
= event_param
->instrumentation
;
398 /* Populate lttng_event structure before tracepoint registration. */
400 switch (event_param
->instrumentation
) {
401 case LTTNG_KERNEL_TRACEPOINT
:
402 event
->desc
= lttng_event_get(event_param
->name
);
407 ret
= lttng_wrapper_tracepoint_probe_register(event
->desc
->kname
,
408 event
->desc
->probe_callback
,
415 case LTTNG_KERNEL_KPROBE
:
416 ret
= lttng_kprobes_register(event_param
->name
,
417 event_param
->u
.kprobe
.symbol_name
,
418 event_param
->u
.kprobe
.offset
,
419 event_param
->u
.kprobe
.addr
,
425 ret
= try_module_get(event
->desc
->owner
);
428 case LTTNG_KERNEL_KRETPROBE
:
430 struct lttng_event
*event_return
;
432 /* kretprobe defines 2 events */
434 kmem_cache_zalloc(event_cache
, GFP_KERNEL
);
439 event_return
->chan
= chan
;
440 event_return
->filter
= filter
;
441 event_return
->id
= chan
->free_event_id
++;
442 event_return
->enabled
= 1;
443 event_return
->instrumentation
= event_param
->instrumentation
;
445 * Populate lttng_event structure before kretprobe registration.
448 ret
= lttng_kretprobes_register(event_param
->name
,
449 event_param
->u
.kretprobe
.symbol_name
,
450 event_param
->u
.kretprobe
.offset
,
451 event_param
->u
.kretprobe
.addr
,
452 event
, event_return
);
454 kmem_cache_free(event_cache
, event_return
);
458 /* Take 2 refs on the module: one per event. */
459 ret
= try_module_get(event
->desc
->owner
);
461 ret
= try_module_get(event
->desc
->owner
);
463 ret
= _lttng_event_metadata_statedump(chan
->session
, chan
,
465 WARN_ON_ONCE(ret
> 0);
467 kmem_cache_free(event_cache
, event_return
);
468 module_put(event
->desc
->owner
);
469 module_put(event
->desc
->owner
);
470 goto statedump_error
;
472 list_add(&event_return
->list
, &chan
->session
->events
);
475 case LTTNG_KERNEL_FUNCTION
:
476 ret
= lttng_ftrace_register(event_param
->name
,
477 event_param
->u
.ftrace
.symbol_name
,
482 ret
= try_module_get(event
->desc
->owner
);
485 case LTTNG_KERNEL_NOOP
:
486 event
->desc
= internal_desc
;
497 ret
= _lttng_event_metadata_statedump(chan
->session
, chan
, event
);
498 WARN_ON_ONCE(ret
> 0);
500 goto statedump_error
;
502 list_add(&event
->list
, &chan
->session
->events
);
503 mutex_unlock(&sessions_mutex
);
507 /* If a statedump error occurs, events will not be readable. */
509 kmem_cache_free(event_cache
, event
);
513 mutex_unlock(&sessions_mutex
);
518 * Only used internally at session destruction.
520 int _lttng_event_unregister(struct lttng_event
*event
)
524 switch (event
->instrumentation
) {
525 case LTTNG_KERNEL_TRACEPOINT
:
526 ret
= lttng_wrapper_tracepoint_probe_unregister(event
->desc
->kname
,
527 event
->desc
->probe_callback
,
532 case LTTNG_KERNEL_KPROBE
:
533 lttng_kprobes_unregister(event
);
536 case LTTNG_KERNEL_KRETPROBE
:
537 lttng_kretprobes_unregister(event
);
540 case LTTNG_KERNEL_FUNCTION
:
541 lttng_ftrace_unregister(event
);
544 case LTTNG_KERNEL_NOOP
:
554 * Only used internally at session destruction.
557 void _lttng_event_destroy(struct lttng_event
*event
)
559 switch (event
->instrumentation
) {
560 case LTTNG_KERNEL_TRACEPOINT
:
561 lttng_event_put(event
->desc
);
563 case LTTNG_KERNEL_KPROBE
:
564 module_put(event
->desc
->owner
);
565 lttng_kprobes_destroy_private(event
);
567 case LTTNG_KERNEL_KRETPROBE
:
568 module_put(event
->desc
->owner
);
569 lttng_kretprobes_destroy_private(event
);
571 case LTTNG_KERNEL_FUNCTION
:
572 module_put(event
->desc
->owner
);
573 lttng_ftrace_destroy_private(event
);
575 case LTTNG_KERNEL_NOOP
:
580 list_del(&event
->list
);
581 lttng_destroy_context(event
->ctx
);
582 kmem_cache_free(event_cache
, event
);
586 * Serialize at most one packet worth of metadata into a metadata
588 * We grab the metadata cache mutex to get exclusive access to our metadata
589 * buffer and to the metadata cache. Exclusive access to the metadata buffer
590 * allows us to do racy operations such as looking for remaining space left in
591 * packet and write, since mutual exclusion protects us from concurrent writes.
592 * Mutual exclusion on the metadata cache allow us to read the cache content
593 * without racing against reallocation of the cache by updates.
594 * Returns the number of bytes written in the channel, 0 if no data
595 * was written and a negative value on error.
597 int lttng_metadata_output_channel(struct lttng_metadata_stream
*stream
,
598 struct channel
*chan
)
600 struct lib_ring_buffer_ctx ctx
;
602 size_t len
, reserve_len
;
605 * Ensure we support mutiple get_next / put sequences followed by
606 * put_next. The metadata cache lock protects reading the metadata
607 * cache. It can indeed be read concurrently by "get_next_subbuf" and
608 * "flush" operations on the buffer invoked by different processes.
609 * Moreover, since the metadata cache memory can be reallocated, we
610 * need to have exclusive access against updates even though we only
613 mutex_lock(&stream
->metadata_cache
->lock
);
614 WARN_ON(stream
->metadata_in
< stream
->metadata_out
);
615 if (stream
->metadata_in
!= stream
->metadata_out
)
618 len
= stream
->metadata_cache
->metadata_written
-
622 reserve_len
= min_t(size_t,
623 stream
->transport
->ops
.packet_avail_size(chan
),
625 lib_ring_buffer_ctx_init(&ctx
, chan
, NULL
, reserve_len
,
628 * If reservation failed, return an error to the caller.
630 ret
= stream
->transport
->ops
.event_reserve(&ctx
, 0);
632 printk(KERN_WARNING
"LTTng: Metadata event reservation failed\n");
635 stream
->transport
->ops
.event_write(&ctx
,
636 stream
->metadata_cache
->data
+ stream
->metadata_in
,
638 stream
->transport
->ops
.event_commit(&ctx
);
639 stream
->metadata_in
+= reserve_len
;
643 mutex_unlock(&stream
->metadata_cache
->lock
);
648 * Write the metadata to the metadata cache.
649 * Must be called with sessions_mutex held.
650 * The metadata cache lock protects us from concurrent read access from
651 * thread outputting metadata content to ring buffer.
653 int lttng_metadata_printf(struct lttng_session
*session
,
654 const char *fmt
, ...)
659 struct lttng_metadata_stream
*stream
;
661 WARN_ON_ONCE(!ACCESS_ONCE(session
->active
));
664 str
= kvasprintf(GFP_KERNEL
, fmt
, ap
);
670 mutex_lock(&session
->metadata_cache
->lock
);
671 if (session
->metadata_cache
->metadata_written
+ len
>
672 session
->metadata_cache
->cache_alloc
) {
673 char *tmp_cache_realloc
;
674 unsigned int tmp_cache_alloc_size
;
676 tmp_cache_alloc_size
= max_t(unsigned int,
677 session
->metadata_cache
->cache_alloc
+ len
,
678 session
->metadata_cache
->cache_alloc
<< 1);
679 tmp_cache_realloc
= vzalloc(tmp_cache_alloc_size
);
680 if (!tmp_cache_realloc
)
682 if (session
->metadata_cache
->data
) {
683 memcpy(tmp_cache_realloc
,
684 session
->metadata_cache
->data
,
685 session
->metadata_cache
->cache_alloc
);
686 vfree(session
->metadata_cache
->data
);
689 session
->metadata_cache
->cache_alloc
= tmp_cache_alloc_size
;
690 session
->metadata_cache
->data
= tmp_cache_realloc
;
692 memcpy(session
->metadata_cache
->data
+
693 session
->metadata_cache
->metadata_written
,
695 session
->metadata_cache
->metadata_written
+= len
;
696 mutex_unlock(&session
->metadata_cache
->lock
);
699 list_for_each_entry(stream
, &session
->metadata_cache
->metadata_stream
, list
)
700 wake_up_interruptible(&stream
->read_wait
);
705 mutex_unlock(&session
->metadata_cache
->lock
);
711 * Must be called with sessions_mutex held.
714 int _lttng_field_statedump(struct lttng_session
*session
,
715 const struct lttng_event_field
*field
)
719 switch (field
->type
.atype
) {
721 ret
= lttng_metadata_printf(session
,
722 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n",
723 field
->type
.u
.basic
.integer
.size
,
724 field
->type
.u
.basic
.integer
.alignment
,
725 field
->type
.u
.basic
.integer
.signedness
,
726 (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_none
)
728 : (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
731 field
->type
.u
.basic
.integer
.base
,
733 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
735 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
740 ret
= lttng_metadata_printf(session
,
742 field
->type
.u
.basic
.enumeration
.name
,
747 const struct lttng_basic_type
*elem_type
;
749 elem_type
= &field
->type
.u
.array
.elem_type
;
750 ret
= lttng_metadata_printf(session
,
751 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n",
752 elem_type
->u
.basic
.integer
.size
,
753 elem_type
->u
.basic
.integer
.alignment
,
754 elem_type
->u
.basic
.integer
.signedness
,
755 (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
757 : (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
760 elem_type
->u
.basic
.integer
.base
,
762 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
764 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
766 field
->name
, field
->type
.u
.array
.length
);
771 const struct lttng_basic_type
*elem_type
;
772 const struct lttng_basic_type
*length_type
;
774 elem_type
= &field
->type
.u
.sequence
.elem_type
;
775 length_type
= &field
->type
.u
.sequence
.length_type
;
776 ret
= lttng_metadata_printf(session
,
777 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n",
778 length_type
->u
.basic
.integer
.size
,
779 (unsigned int) length_type
->u
.basic
.integer
.alignment
,
780 length_type
->u
.basic
.integer
.signedness
,
781 (length_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
783 : ((length_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
786 length_type
->u
.basic
.integer
.base
,
788 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
790 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
796 ret
= lttng_metadata_printf(session
,
797 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n",
798 elem_type
->u
.basic
.integer
.size
,
799 (unsigned int) elem_type
->u
.basic
.integer
.alignment
,
800 elem_type
->u
.basic
.integer
.signedness
,
801 (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
803 : ((elem_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
806 elem_type
->u
.basic
.integer
.base
,
808 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
810 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
818 /* Default encoding is UTF8 */
819 ret
= lttng_metadata_printf(session
,
821 field
->type
.u
.basic
.string
.encoding
== lttng_encode_ASCII
?
822 " { encoding = ASCII; }" : "",
833 int _lttng_context_metadata_statedump(struct lttng_session
*session
,
834 struct lttng_ctx
*ctx
)
841 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
842 const struct lttng_ctx_field
*field
= &ctx
->fields
[i
];
844 ret
= _lttng_field_statedump(session
, &field
->event_field
);
852 int _lttng_fields_metadata_statedump(struct lttng_session
*session
,
853 struct lttng_event
*event
)
855 const struct lttng_event_desc
*desc
= event
->desc
;
859 for (i
= 0; i
< desc
->nr_fields
; i
++) {
860 const struct lttng_event_field
*field
= &desc
->fields
[i
];
862 ret
= _lttng_field_statedump(session
, field
);
870 * Must be called with sessions_mutex held.
873 int _lttng_event_metadata_statedump(struct lttng_session
*session
,
874 struct lttng_channel
*chan
,
875 struct lttng_event
*event
)
879 if (event
->metadata_dumped
|| !ACCESS_ONCE(session
->active
))
881 if (chan
->channel_type
== METADATA_CHANNEL
)
884 ret
= lttng_metadata_printf(session
,
888 " stream_id = %u;\n",
896 ret
= lttng_metadata_printf(session
,
897 " context := struct {\n");
901 ret
= _lttng_context_metadata_statedump(session
, event
->ctx
);
905 ret
= lttng_metadata_printf(session
,
911 ret
= lttng_metadata_printf(session
,
912 " fields := struct {\n"
917 ret
= _lttng_fields_metadata_statedump(session
, event
);
922 * LTTng space reservation can only reserve multiples of the
925 ret
= lttng_metadata_printf(session
,
931 event
->metadata_dumped
= 1;
938 * Must be called with sessions_mutex held.
941 int _lttng_channel_metadata_statedump(struct lttng_session
*session
,
942 struct lttng_channel
*chan
)
946 if (chan
->metadata_dumped
|| !ACCESS_ONCE(session
->active
))
949 if (chan
->channel_type
== METADATA_CHANNEL
)
952 WARN_ON_ONCE(!chan
->header_type
);
953 ret
= lttng_metadata_printf(session
,
956 " event.header := %s;\n"
957 " packet.context := struct packet_context;\n",
959 chan
->header_type
== 1 ? "struct event_header_compact" :
960 "struct event_header_large");
965 ret
= lttng_metadata_printf(session
,
966 " event.context := struct {\n");
970 ret
= _lttng_context_metadata_statedump(session
, chan
->ctx
);
974 ret
= lttng_metadata_printf(session
,
980 ret
= lttng_metadata_printf(session
,
983 chan
->metadata_dumped
= 1;
989 * Must be called with sessions_mutex held.
992 int _lttng_stream_packet_context_declare(struct lttng_session
*session
)
994 return lttng_metadata_printf(session
,
995 "struct packet_context {\n"
996 " uint64_clock_monotonic_t timestamp_begin;\n"
997 " uint64_clock_monotonic_t timestamp_end;\n"
998 " uint64_t content_size;\n"
999 " uint64_t packet_size;\n"
1000 " unsigned long events_discarded;\n"
1001 " uint32_t cpu_id;\n"
1008 * id: range: 0 - 30.
1009 * id 31 is reserved to indicate an extended header.
1012 * id: range: 0 - 65534.
1013 * id 65535 is reserved to indicate an extended header.
1015 * Must be called with sessions_mutex held.
1018 int _lttng_event_header_declare(struct lttng_session
*session
)
1020 return lttng_metadata_printf(session
,
1021 "struct event_header_compact {\n"
1022 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
1025 " uint27_clock_monotonic_t timestamp;\n"
1029 " uint64_clock_monotonic_t timestamp;\n"
1034 "struct event_header_large {\n"
1035 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
1038 " uint32_clock_monotonic_t timestamp;\n"
1042 " uint64_clock_monotonic_t timestamp;\n"
1046 lttng_alignof(uint32_t) * CHAR_BIT
,
1047 lttng_alignof(uint16_t) * CHAR_BIT
1052 * Approximation of NTP time of day to clock monotonic correlation,
1053 * taken at start of trace.
1054 * Yes, this is only an approximation. Yes, we can (and will) do better
1055 * in future versions.
1058 uint64_t measure_clock_offset(void)
1060 uint64_t offset
, monotonic
[2], realtime
;
1061 struct timespec rts
= { 0, 0 };
1062 unsigned long flags
;
1064 /* Disable interrupts to increase correlation precision. */
1065 local_irq_save(flags
);
1066 monotonic
[0] = trace_clock_read64();
1067 getnstimeofday(&rts
);
1068 monotonic
[1] = trace_clock_read64();
1069 local_irq_restore(flags
);
1071 offset
= (monotonic
[0] + monotonic
[1]) >> 1;
1072 realtime
= (uint64_t) rts
.tv_sec
* NSEC_PER_SEC
;
1073 realtime
+= rts
.tv_nsec
;
1074 offset
= realtime
- offset
;
1079 * Output metadata into this session's metadata buffers.
1080 * Must be called with sessions_mutex held.
1083 int _lttng_session_metadata_statedump(struct lttng_session
*session
)
1085 unsigned char *uuid_c
= session
->uuid
.b
;
1086 unsigned char uuid_s
[37], clock_uuid_s
[BOOT_ID_LEN
];
1087 struct lttng_channel
*chan
;
1088 struct lttng_event
*event
;
1091 if (!ACCESS_ONCE(session
->active
))
1093 if (session
->metadata_dumped
)
1096 snprintf(uuid_s
, sizeof(uuid_s
),
1097 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
1098 uuid_c
[0], uuid_c
[1], uuid_c
[2], uuid_c
[3],
1099 uuid_c
[4], uuid_c
[5], uuid_c
[6], uuid_c
[7],
1100 uuid_c
[8], uuid_c
[9], uuid_c
[10], uuid_c
[11],
1101 uuid_c
[12], uuid_c
[13], uuid_c
[14], uuid_c
[15]);
1103 ret
= lttng_metadata_printf(session
,
1104 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
1105 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
1106 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
1107 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
1108 "typealias integer { size = %u; align = %u; signed = false; } := unsigned long;\n"
1109 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
1110 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
1116 " byte_order = %s;\n"
1117 " packet.header := struct {\n"
1118 " uint32_t magic;\n"
1119 " uint8_t uuid[16];\n"
1120 " uint32_t stream_id;\n"
1123 lttng_alignof(uint8_t) * CHAR_BIT
,
1124 lttng_alignof(uint16_t) * CHAR_BIT
,
1125 lttng_alignof(uint32_t) * CHAR_BIT
,
1126 lttng_alignof(uint64_t) * CHAR_BIT
,
1127 sizeof(unsigned long) * CHAR_BIT
,
1128 lttng_alignof(unsigned long) * CHAR_BIT
,
1141 ret
= lttng_metadata_printf(session
,
1143 " hostname = \"%s\";\n"
1144 " domain = \"kernel\";\n"
1145 " sysname = \"%s\";\n"
1146 " kernel_release = \"%s\";\n"
1147 " kernel_version = \"%s\";\n"
1148 " tracer_name = \"lttng-modules\";\n"
1149 " tracer_major = %d;\n"
1150 " tracer_minor = %d;\n"
1151 " tracer_patchlevel = %d;\n"
1153 current
->nsproxy
->uts_ns
->name
.nodename
,
1157 LTTNG_MODULES_MAJOR_VERSION
,
1158 LTTNG_MODULES_MINOR_VERSION
,
1159 LTTNG_MODULES_PATCHLEVEL_VERSION
1164 ret
= lttng_metadata_printf(session
,
1172 if (!trace_clock_uuid(clock_uuid_s
)) {
1173 ret
= lttng_metadata_printf(session
,
1174 " uuid = \"%s\";\n",
1181 ret
= lttng_metadata_printf(session
,
1182 " description = \"Monotonic Clock\";\n"
1183 " freq = %llu; /* Frequency, in Hz */\n"
1184 " /* clock value offset from Epoch is: offset * (1/freq) */\n"
1187 (unsigned long long) trace_clock_freq(),
1188 (unsigned long long) measure_clock_offset()
1193 ret
= lttng_metadata_printf(session
,
1194 "typealias integer {\n"
1195 " size = 27; align = 1; signed = false;\n"
1196 " map = clock.monotonic.value;\n"
1197 "} := uint27_clock_monotonic_t;\n"
1199 "typealias integer {\n"
1200 " size = 32; align = %u; signed = false;\n"
1201 " map = clock.monotonic.value;\n"
1202 "} := uint32_clock_monotonic_t;\n"
1204 "typealias integer {\n"
1205 " size = 64; align = %u; signed = false;\n"
1206 " map = clock.monotonic.value;\n"
1207 "} := uint64_clock_monotonic_t;\n\n",
1208 lttng_alignof(uint32_t) * CHAR_BIT
,
1209 lttng_alignof(uint64_t) * CHAR_BIT
1214 ret
= _lttng_stream_packet_context_declare(session
);
1218 ret
= _lttng_event_header_declare(session
);
1223 list_for_each_entry(chan
, &session
->chan
, list
) {
1224 ret
= _lttng_channel_metadata_statedump(session
, chan
);
1229 list_for_each_entry(event
, &session
->events
, list
) {
1230 ret
= _lttng_event_metadata_statedump(session
, event
->chan
, event
);
1234 session
->metadata_dumped
= 1;
1240 * lttng_transport_register - LTT transport registration
1241 * @transport: transport structure
1243 * Registers a transport which can be used as output to extract the data out of
1244 * LTTng. The module calling this registration function must ensure that no
1245 * trap-inducing code will be executed by the transport functions. E.g.
1246 * vmalloc_sync_all() must be called between a vmalloc and the moment the memory
1247 * is made visible to the transport function. This registration acts as a
1248 * vmalloc_sync_all. Therefore, only if the module allocates virtual memory
1249 * after its registration must it synchronize the TLBs.
1251 void lttng_transport_register(struct lttng_transport
*transport
)
1254 * Make sure no page fault can be triggered by the module about to be
1255 * registered. We deal with this here so we don't have to call
1256 * vmalloc_sync_all() in each module's init.
1258 wrapper_vmalloc_sync_all();
1260 mutex_lock(&sessions_mutex
);
1261 list_add_tail(&transport
->node
, <tng_transport_list
);
1262 mutex_unlock(&sessions_mutex
);
1264 EXPORT_SYMBOL_GPL(lttng_transport_register
);
1267 * lttng_transport_unregister - LTT transport unregistration
1268 * @transport: transport structure
1270 void lttng_transport_unregister(struct lttng_transport
*transport
)
1272 mutex_lock(&sessions_mutex
);
1273 list_del(&transport
->node
);
1274 mutex_unlock(&sessions_mutex
);
1276 EXPORT_SYMBOL_GPL(lttng_transport_unregister
);
1278 static int __init
lttng_events_init(void)
1282 ret
= wrapper_lttng_fixup_sig(THIS_MODULE
);
1286 ret
= lttng_tracepoint_init();
1289 event_cache
= KMEM_CACHE(lttng_event
, 0);
1294 ret
= lttng_abi_init();
1297 ret
= lttng_logger_init();
1305 kmem_cache_destroy(event_cache
);
1307 lttng_tracepoint_exit();
1311 module_init(lttng_events_init
);
1313 static void __exit
lttng_events_exit(void)
1315 struct lttng_session
*session
, *tmpsession
;
1317 lttng_logger_exit();
1319 list_for_each_entry_safe(session
, tmpsession
, &sessions
, list
)
1320 lttng_session_destroy(session
);
1321 kmem_cache_destroy(event_cache
);
1322 lttng_tracepoint_exit();
1325 module_exit(lttng_events_exit
);
1327 MODULE_LICENSE("GPL and additional rights");
1328 MODULE_AUTHOR("Mathieu Desnoyers <mathieu.desnoyers@efficios.com>");
1329 MODULE_DESCRIPTION("LTTng Events");
1330 MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION
) "."
1331 __stringify(LTTNG_MODULES_MINOR_VERSION
) "."
1332 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION
)
1333 LTTNG_MODULES_EXTRAVERSION
);