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 mutex_init(&metadata_cache
->lock
);
100 session
->metadata_cache
= metadata_cache
;
101 INIT_LIST_HEAD(&metadata_cache
->metadata_stream
);
102 memcpy(&metadata_cache
->uuid
, &session
->uuid
,
103 sizeof(metadata_cache
->uuid
));
104 list_add(&session
->list
, &sessions
);
105 mutex_unlock(&sessions_mutex
);
109 kfree(metadata_cache
);
113 mutex_unlock(&sessions_mutex
);
117 void metadata_cache_destroy(struct kref
*kref
)
119 struct lttng_metadata_cache
*cache
=
120 container_of(kref
, struct lttng_metadata_cache
, refcount
);
125 void lttng_session_destroy(struct lttng_session
*session
)
127 struct lttng_channel
*chan
, *tmpchan
;
128 struct lttng_event
*event
, *tmpevent
;
129 struct lttng_metadata_stream
*metadata_stream
;
132 mutex_lock(&sessions_mutex
);
133 ACCESS_ONCE(session
->active
) = 0;
134 list_for_each_entry(chan
, &session
->chan
, list
) {
135 ret
= lttng_syscalls_unregister(chan
);
138 list_for_each_entry(event
, &session
->events
, list
) {
139 ret
= _lttng_event_unregister(event
);
142 synchronize_trace(); /* Wait for in-flight events to complete */
143 list_for_each_entry_safe(event
, tmpevent
, &session
->events
, list
)
144 _lttng_event_destroy(event
);
145 list_for_each_entry_safe(chan
, tmpchan
, &session
->chan
, list
) {
146 BUG_ON(chan
->channel_type
== METADATA_CHANNEL
);
147 _lttng_channel_destroy(chan
);
149 list_for_each_entry(metadata_stream
, &session
->metadata_cache
->metadata_stream
, list
)
150 _lttng_metadata_channel_hangup(metadata_stream
);
151 kref_put(&session
->metadata_cache
->refcount
, metadata_cache_destroy
);
152 list_del(&session
->list
);
153 mutex_unlock(&sessions_mutex
);
157 int lttng_session_enable(struct lttng_session
*session
)
160 struct lttng_channel
*chan
;
162 mutex_lock(&sessions_mutex
);
163 if (session
->active
) {
169 * Snapshot the number of events per channel to know the type of header
172 list_for_each_entry(chan
, &session
->chan
, list
) {
173 if (chan
->header_type
)
174 continue; /* don't change it if session stop/restart */
175 if (chan
->free_event_id
< 31)
176 chan
->header_type
= 1; /* compact */
178 chan
->header_type
= 2; /* large */
181 ACCESS_ONCE(session
->active
) = 1;
182 ACCESS_ONCE(session
->been_active
) = 1;
183 ret
= _lttng_session_metadata_statedump(session
);
185 ACCESS_ONCE(session
->active
) = 0;
188 ret
= lttng_statedump_start(session
);
190 ACCESS_ONCE(session
->active
) = 0;
192 mutex_unlock(&sessions_mutex
);
196 int lttng_session_disable(struct lttng_session
*session
)
200 mutex_lock(&sessions_mutex
);
201 if (!session
->active
) {
205 ACCESS_ONCE(session
->active
) = 0;
207 mutex_unlock(&sessions_mutex
);
211 int lttng_channel_enable(struct lttng_channel
*channel
)
215 if (channel
->channel_type
== METADATA_CHANNEL
)
217 old
= xchg(&channel
->enabled
, 1);
223 int lttng_channel_disable(struct lttng_channel
*channel
)
227 if (channel
->channel_type
== METADATA_CHANNEL
)
229 old
= xchg(&channel
->enabled
, 0);
235 int lttng_event_enable(struct lttng_event
*event
)
239 if (event
->chan
->channel_type
== METADATA_CHANNEL
)
241 old
= xchg(&event
->enabled
, 1);
247 int lttng_event_disable(struct lttng_event
*event
)
251 if (event
->chan
->channel_type
== METADATA_CHANNEL
)
253 old
= xchg(&event
->enabled
, 0);
259 static struct lttng_transport
*lttng_transport_find(const char *name
)
261 struct lttng_transport
*transport
;
263 list_for_each_entry(transport
, <tng_transport_list
, node
) {
264 if (!strcmp(transport
->name
, name
))
270 struct lttng_channel
*lttng_channel_create(struct lttng_session
*session
,
271 const char *transport_name
,
273 size_t subbuf_size
, size_t num_subbuf
,
274 unsigned int switch_timer_interval
,
275 unsigned int read_timer_interval
,
276 enum channel_type channel_type
)
278 struct lttng_channel
*chan
;
279 struct lttng_transport
*transport
= NULL
;
281 mutex_lock(&sessions_mutex
);
282 if (session
->been_active
&& channel_type
!= METADATA_CHANNEL
)
283 goto active
; /* Refuse to add channel to active session */
284 transport
= lttng_transport_find(transport_name
);
286 printk(KERN_WARNING
"LTTng transport %s not found\n",
290 if (!try_module_get(transport
->owner
)) {
291 printk(KERN_WARNING
"LTT : Can't lock transport module.\n");
294 chan
= kzalloc(sizeof(struct lttng_channel
), GFP_KERNEL
);
297 chan
->session
= session
;
298 chan
->id
= session
->free_chan_id
++;
299 chan
->ops
= &transport
->ops
;
301 * Note: the channel creation op already writes into the packet
302 * headers. Therefore the "chan" information used as input
303 * should be already accessible.
305 chan
->chan
= transport
->ops
.channel_create(transport_name
,
306 chan
, buf_addr
, subbuf_size
, num_subbuf
,
307 switch_timer_interval
, read_timer_interval
);
311 chan
->transport
= transport
;
312 chan
->channel_type
= channel_type
;
313 list_add(&chan
->list
, &session
->chan
);
314 mutex_unlock(&sessions_mutex
);
321 module_put(transport
->owner
);
324 mutex_unlock(&sessions_mutex
);
329 * Only used internally at session destruction for per-cpu channels, and
330 * when metadata channel is released.
331 * Needs to be called with sessions mutex held.
334 void _lttng_channel_destroy(struct lttng_channel
*chan
)
336 chan
->ops
->channel_destroy(chan
->chan
);
337 module_put(chan
->transport
->owner
);
338 list_del(&chan
->list
);
339 lttng_destroy_context(chan
->ctx
);
343 void lttng_metadata_channel_destroy(struct lttng_channel
*chan
)
345 BUG_ON(chan
->channel_type
!= METADATA_CHANNEL
);
347 /* Protect the metadata cache with the sessions_mutex. */
348 mutex_lock(&sessions_mutex
);
349 _lttng_channel_destroy(chan
);
350 mutex_unlock(&sessions_mutex
);
352 EXPORT_SYMBOL_GPL(lttng_metadata_channel_destroy
);
355 void _lttng_metadata_channel_hangup(struct lttng_metadata_stream
*stream
)
357 stream
->finalized
= 1;
358 wake_up_interruptible(&stream
->read_wait
);
362 * Supports event creation while tracing session is active.
364 struct lttng_event
*lttng_event_create(struct lttng_channel
*chan
,
365 struct lttng_kernel_event
*event_param
,
367 const struct lttng_event_desc
*internal_desc
)
369 struct lttng_event
*event
;
372 mutex_lock(&sessions_mutex
);
373 if (chan
->free_event_id
== -1U) {
378 * This is O(n^2) (for each event, the loop is called at event
379 * creation). Might require a hash if we have lots of events.
381 list_for_each_entry(event
, &chan
->session
->events
, list
) {
382 if (!strcmp(event
->desc
->name
, event_param
->name
)) {
387 event
= kmem_cache_zalloc(event_cache
, GFP_KERNEL
);
393 event
->filter
= filter
;
394 event
->id
= chan
->free_event_id
++;
396 event
->instrumentation
= event_param
->instrumentation
;
397 /* Populate lttng_event structure before tracepoint registration. */
399 switch (event_param
->instrumentation
) {
400 case LTTNG_KERNEL_TRACEPOINT
:
401 event
->desc
= lttng_event_get(event_param
->name
);
406 ret
= lttng_wrapper_tracepoint_probe_register(event
->desc
->kname
,
407 event
->desc
->probe_callback
,
414 case LTTNG_KERNEL_KPROBE
:
415 ret
= lttng_kprobes_register(event_param
->name
,
416 event_param
->u
.kprobe
.symbol_name
,
417 event_param
->u
.kprobe
.offset
,
418 event_param
->u
.kprobe
.addr
,
424 ret
= try_module_get(event
->desc
->owner
);
427 case LTTNG_KERNEL_KRETPROBE
:
429 struct lttng_event
*event_return
;
431 /* kretprobe defines 2 events */
433 kmem_cache_zalloc(event_cache
, GFP_KERNEL
);
438 event_return
->chan
= chan
;
439 event_return
->filter
= filter
;
440 event_return
->id
= chan
->free_event_id
++;
441 event_return
->enabled
= 1;
442 event_return
->instrumentation
= event_param
->instrumentation
;
444 * Populate lttng_event structure before kretprobe registration.
447 ret
= lttng_kretprobes_register(event_param
->name
,
448 event_param
->u
.kretprobe
.symbol_name
,
449 event_param
->u
.kretprobe
.offset
,
450 event_param
->u
.kretprobe
.addr
,
451 event
, event_return
);
453 kmem_cache_free(event_cache
, event_return
);
457 /* Take 2 refs on the module: one per event. */
458 ret
= try_module_get(event
->desc
->owner
);
460 ret
= try_module_get(event
->desc
->owner
);
462 ret
= _lttng_event_metadata_statedump(chan
->session
, chan
,
464 WARN_ON_ONCE(ret
> 0);
466 kmem_cache_free(event_cache
, event_return
);
467 module_put(event
->desc
->owner
);
468 module_put(event
->desc
->owner
);
469 goto statedump_error
;
471 list_add(&event_return
->list
, &chan
->session
->events
);
474 case LTTNG_KERNEL_FUNCTION
:
475 ret
= lttng_ftrace_register(event_param
->name
,
476 event_param
->u
.ftrace
.symbol_name
,
481 ret
= try_module_get(event
->desc
->owner
);
484 case LTTNG_KERNEL_NOOP
:
485 event
->desc
= internal_desc
;
496 ret
= _lttng_event_metadata_statedump(chan
->session
, chan
, event
);
497 WARN_ON_ONCE(ret
> 0);
499 goto statedump_error
;
501 list_add(&event
->list
, &chan
->session
->events
);
502 mutex_unlock(&sessions_mutex
);
506 /* If a statedump error occurs, events will not be readable. */
508 kmem_cache_free(event_cache
, event
);
512 mutex_unlock(&sessions_mutex
);
517 * Only used internally at session destruction.
519 int _lttng_event_unregister(struct lttng_event
*event
)
523 switch (event
->instrumentation
) {
524 case LTTNG_KERNEL_TRACEPOINT
:
525 ret
= lttng_wrapper_tracepoint_probe_unregister(event
->desc
->kname
,
526 event
->desc
->probe_callback
,
531 case LTTNG_KERNEL_KPROBE
:
532 lttng_kprobes_unregister(event
);
535 case LTTNG_KERNEL_KRETPROBE
:
536 lttng_kretprobes_unregister(event
);
539 case LTTNG_KERNEL_FUNCTION
:
540 lttng_ftrace_unregister(event
);
543 case LTTNG_KERNEL_NOOP
:
553 * Only used internally at session destruction.
556 void _lttng_event_destroy(struct lttng_event
*event
)
558 switch (event
->instrumentation
) {
559 case LTTNG_KERNEL_TRACEPOINT
:
560 lttng_event_put(event
->desc
);
562 case LTTNG_KERNEL_KPROBE
:
563 module_put(event
->desc
->owner
);
564 lttng_kprobes_destroy_private(event
);
566 case LTTNG_KERNEL_KRETPROBE
:
567 module_put(event
->desc
->owner
);
568 lttng_kretprobes_destroy_private(event
);
570 case LTTNG_KERNEL_FUNCTION
:
571 module_put(event
->desc
->owner
);
572 lttng_ftrace_destroy_private(event
);
574 case LTTNG_KERNEL_NOOP
:
579 list_del(&event
->list
);
580 lttng_destroy_context(event
->ctx
);
581 kmem_cache_free(event_cache
, event
);
585 * Serialize at most one packet worth of metadata into a metadata
587 * We grab the metadata cache mutex to get exclusive access to our metadata
588 * buffer and to the metadata cache. Exclusive access to the metadata buffer
589 * allows us to do racy operations such as looking for remaining space left in
590 * packet and write, since mutual exclusion protects us from concurrent writes.
591 * Mutual exclusion on the metadata cache allow us to read the cache content
592 * without racing against reallocation of the cache by updates.
593 * Returns the number of bytes written in the channel, 0 if no data
594 * was written and a negative value on error.
596 int lttng_metadata_output_channel(struct lttng_metadata_stream
*stream
,
597 struct channel
*chan
)
599 struct lib_ring_buffer_ctx ctx
;
601 size_t len
, reserve_len
;
604 * Ensure we support mutiple get_next / put sequences followed by
605 * put_next. The metadata cache lock protects reading the metadata
606 * cache. It can indeed be read concurrently by "get_next_subbuf" and
607 * "flush" operations on the buffer invoked by different processes.
608 * Moreover, since the metadata cache memory can be reallocated, we
609 * need to have exclusive access against updates even though we only
612 mutex_lock(&stream
->metadata_cache
->lock
);
613 WARN_ON(stream
->metadata_in
< stream
->metadata_out
);
614 if (stream
->metadata_in
!= stream
->metadata_out
)
617 len
= stream
->metadata_cache
->metadata_written
-
621 reserve_len
= min_t(size_t,
622 stream
->transport
->ops
.packet_avail_size(chan
),
624 lib_ring_buffer_ctx_init(&ctx
, chan
, NULL
, reserve_len
,
627 * If reservation failed, return an error to the caller.
629 ret
= stream
->transport
->ops
.event_reserve(&ctx
, 0);
631 printk(KERN_WARNING
"LTTng: Metadata event reservation failed\n");
634 stream
->transport
->ops
.event_write(&ctx
,
635 stream
->metadata_cache
->data
+ stream
->metadata_in
,
637 stream
->transport
->ops
.event_commit(&ctx
);
638 stream
->metadata_in
+= reserve_len
;
642 mutex_unlock(&stream
->metadata_cache
->lock
);
647 * Write the metadata to the metadata cache.
648 * Must be called with sessions_mutex held.
649 * The metadata cache lock protects us from concurrent read access from
650 * thread outputting metadata content to ring buffer.
652 int lttng_metadata_printf(struct lttng_session
*session
,
653 const char *fmt
, ...)
658 struct lttng_metadata_stream
*stream
;
660 WARN_ON_ONCE(!ACCESS_ONCE(session
->active
));
663 str
= kvasprintf(GFP_KERNEL
, fmt
, ap
);
669 mutex_lock(&session
->metadata_cache
->lock
);
670 if (session
->metadata_cache
->metadata_written
+ len
>
671 session
->metadata_cache
->cache_alloc
) {
672 char *tmp_cache_realloc
;
673 unsigned int tmp_cache_alloc_size
;
675 tmp_cache_alloc_size
= max_t(unsigned int,
676 session
->metadata_cache
->cache_alloc
+ len
,
677 session
->metadata_cache
->cache_alloc
<< 1);
678 tmp_cache_realloc
= krealloc(session
->metadata_cache
->data
,
679 tmp_cache_alloc_size
, GFP_KERNEL
);
680 if (!tmp_cache_realloc
)
682 session
->metadata_cache
->cache_alloc
= tmp_cache_alloc_size
;
683 session
->metadata_cache
->data
= tmp_cache_realloc
;
685 memcpy(session
->metadata_cache
->data
+
686 session
->metadata_cache
->metadata_written
,
688 session
->metadata_cache
->metadata_written
+= len
;
689 mutex_unlock(&session
->metadata_cache
->lock
);
692 list_for_each_entry(stream
, &session
->metadata_cache
->metadata_stream
, list
)
693 wake_up_interruptible(&stream
->read_wait
);
698 mutex_unlock(&session
->metadata_cache
->lock
);
704 * Must be called with sessions_mutex held.
707 int _lttng_field_statedump(struct lttng_session
*session
,
708 const struct lttng_event_field
*field
)
712 switch (field
->type
.atype
) {
714 ret
= lttng_metadata_printf(session
,
715 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n",
716 field
->type
.u
.basic
.integer
.size
,
717 field
->type
.u
.basic
.integer
.alignment
,
718 field
->type
.u
.basic
.integer
.signedness
,
719 (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_none
)
721 : (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
724 field
->type
.u
.basic
.integer
.base
,
726 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
728 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
733 ret
= lttng_metadata_printf(session
,
735 field
->type
.u
.basic
.enumeration
.name
,
740 const struct lttng_basic_type
*elem_type
;
742 elem_type
= &field
->type
.u
.array
.elem_type
;
743 ret
= lttng_metadata_printf(session
,
744 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n",
745 elem_type
->u
.basic
.integer
.size
,
746 elem_type
->u
.basic
.integer
.alignment
,
747 elem_type
->u
.basic
.integer
.signedness
,
748 (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
750 : (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
753 elem_type
->u
.basic
.integer
.base
,
755 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
757 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
759 field
->name
, field
->type
.u
.array
.length
);
764 const struct lttng_basic_type
*elem_type
;
765 const struct lttng_basic_type
*length_type
;
767 elem_type
= &field
->type
.u
.sequence
.elem_type
;
768 length_type
= &field
->type
.u
.sequence
.length_type
;
769 ret
= lttng_metadata_printf(session
,
770 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n",
771 length_type
->u
.basic
.integer
.size
,
772 (unsigned int) length_type
->u
.basic
.integer
.alignment
,
773 length_type
->u
.basic
.integer
.signedness
,
774 (length_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
776 : ((length_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
779 length_type
->u
.basic
.integer
.base
,
781 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
783 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
789 ret
= lttng_metadata_printf(session
,
790 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n",
791 elem_type
->u
.basic
.integer
.size
,
792 (unsigned int) elem_type
->u
.basic
.integer
.alignment
,
793 elem_type
->u
.basic
.integer
.signedness
,
794 (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
796 : ((elem_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
799 elem_type
->u
.basic
.integer
.base
,
801 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
803 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
811 /* Default encoding is UTF8 */
812 ret
= lttng_metadata_printf(session
,
814 field
->type
.u
.basic
.string
.encoding
== lttng_encode_ASCII
?
815 " { encoding = ASCII; }" : "",
826 int _lttng_context_metadata_statedump(struct lttng_session
*session
,
827 struct lttng_ctx
*ctx
)
834 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
835 const struct lttng_ctx_field
*field
= &ctx
->fields
[i
];
837 ret
= _lttng_field_statedump(session
, &field
->event_field
);
845 int _lttng_fields_metadata_statedump(struct lttng_session
*session
,
846 struct lttng_event
*event
)
848 const struct lttng_event_desc
*desc
= event
->desc
;
852 for (i
= 0; i
< desc
->nr_fields
; i
++) {
853 const struct lttng_event_field
*field
= &desc
->fields
[i
];
855 ret
= _lttng_field_statedump(session
, field
);
863 * Must be called with sessions_mutex held.
866 int _lttng_event_metadata_statedump(struct lttng_session
*session
,
867 struct lttng_channel
*chan
,
868 struct lttng_event
*event
)
872 if (event
->metadata_dumped
|| !ACCESS_ONCE(session
->active
))
874 if (chan
->channel_type
== METADATA_CHANNEL
)
877 ret
= lttng_metadata_printf(session
,
881 " stream_id = %u;\n",
889 ret
= lttng_metadata_printf(session
,
890 " context := struct {\n");
894 ret
= _lttng_context_metadata_statedump(session
, event
->ctx
);
898 ret
= lttng_metadata_printf(session
,
904 ret
= lttng_metadata_printf(session
,
905 " fields := struct {\n"
910 ret
= _lttng_fields_metadata_statedump(session
, event
);
915 * LTTng space reservation can only reserve multiples of the
918 ret
= lttng_metadata_printf(session
,
924 event
->metadata_dumped
= 1;
931 * Must be called with sessions_mutex held.
934 int _lttng_channel_metadata_statedump(struct lttng_session
*session
,
935 struct lttng_channel
*chan
)
939 if (chan
->metadata_dumped
|| !ACCESS_ONCE(session
->active
))
942 if (chan
->channel_type
== METADATA_CHANNEL
)
945 WARN_ON_ONCE(!chan
->header_type
);
946 ret
= lttng_metadata_printf(session
,
949 " event.header := %s;\n"
950 " packet.context := struct packet_context;\n",
952 chan
->header_type
== 1 ? "struct event_header_compact" :
953 "struct event_header_large");
958 ret
= lttng_metadata_printf(session
,
959 " event.context := struct {\n");
963 ret
= _lttng_context_metadata_statedump(session
, chan
->ctx
);
967 ret
= lttng_metadata_printf(session
,
973 ret
= lttng_metadata_printf(session
,
976 chan
->metadata_dumped
= 1;
982 * Must be called with sessions_mutex held.
985 int _lttng_stream_packet_context_declare(struct lttng_session
*session
)
987 return lttng_metadata_printf(session
,
988 "struct packet_context {\n"
989 " uint64_clock_monotonic_t timestamp_begin;\n"
990 " uint64_clock_monotonic_t timestamp_end;\n"
991 " uint64_t content_size;\n"
992 " uint64_t packet_size;\n"
993 " unsigned long events_discarded;\n"
994 " uint32_t cpu_id;\n"
1001 * id: range: 0 - 30.
1002 * id 31 is reserved to indicate an extended header.
1005 * id: range: 0 - 65534.
1006 * id 65535 is reserved to indicate an extended header.
1008 * Must be called with sessions_mutex held.
1011 int _lttng_event_header_declare(struct lttng_session
*session
)
1013 return lttng_metadata_printf(session
,
1014 "struct event_header_compact {\n"
1015 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
1018 " uint27_clock_monotonic_t timestamp;\n"
1022 " uint64_clock_monotonic_t timestamp;\n"
1027 "struct event_header_large {\n"
1028 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
1031 " uint32_clock_monotonic_t timestamp;\n"
1035 " uint64_clock_monotonic_t timestamp;\n"
1039 lttng_alignof(uint32_t) * CHAR_BIT
,
1040 lttng_alignof(uint16_t) * CHAR_BIT
1045 * Approximation of NTP time of day to clock monotonic correlation,
1046 * taken at start of trace.
1047 * Yes, this is only an approximation. Yes, we can (and will) do better
1048 * in future versions.
1051 uint64_t measure_clock_offset(void)
1053 uint64_t offset
, monotonic
[2], realtime
;
1054 struct timespec rts
= { 0, 0 };
1055 unsigned long flags
;
1057 /* Disable interrupts to increase correlation precision. */
1058 local_irq_save(flags
);
1059 monotonic
[0] = trace_clock_read64();
1060 getnstimeofday(&rts
);
1061 monotonic
[1] = trace_clock_read64();
1062 local_irq_restore(flags
);
1064 offset
= (monotonic
[0] + monotonic
[1]) >> 1;
1065 realtime
= (uint64_t) rts
.tv_sec
* NSEC_PER_SEC
;
1066 realtime
+= rts
.tv_nsec
;
1067 offset
= realtime
- offset
;
1072 * Output metadata into this session's metadata buffers.
1073 * Must be called with sessions_mutex held.
1076 int _lttng_session_metadata_statedump(struct lttng_session
*session
)
1078 unsigned char *uuid_c
= session
->uuid
.b
;
1079 unsigned char uuid_s
[37], clock_uuid_s
[BOOT_ID_LEN
];
1080 struct lttng_channel
*chan
;
1081 struct lttng_event
*event
;
1084 if (!ACCESS_ONCE(session
->active
))
1086 if (session
->metadata_dumped
)
1089 snprintf(uuid_s
, sizeof(uuid_s
),
1090 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
1091 uuid_c
[0], uuid_c
[1], uuid_c
[2], uuid_c
[3],
1092 uuid_c
[4], uuid_c
[5], uuid_c
[6], uuid_c
[7],
1093 uuid_c
[8], uuid_c
[9], uuid_c
[10], uuid_c
[11],
1094 uuid_c
[12], uuid_c
[13], uuid_c
[14], uuid_c
[15]);
1096 ret
= lttng_metadata_printf(session
,
1097 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
1098 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
1099 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
1100 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
1101 "typealias integer { size = %u; align = %u; signed = false; } := unsigned long;\n"
1102 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
1103 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
1109 " byte_order = %s;\n"
1110 " packet.header := struct {\n"
1111 " uint32_t magic;\n"
1112 " uint8_t uuid[16];\n"
1113 " uint32_t stream_id;\n"
1116 lttng_alignof(uint8_t) * CHAR_BIT
,
1117 lttng_alignof(uint16_t) * CHAR_BIT
,
1118 lttng_alignof(uint32_t) * CHAR_BIT
,
1119 lttng_alignof(uint64_t) * CHAR_BIT
,
1120 sizeof(unsigned long) * CHAR_BIT
,
1121 lttng_alignof(unsigned long) * CHAR_BIT
,
1134 ret
= lttng_metadata_printf(session
,
1136 " hostname = \"%s\";\n"
1137 " domain = \"kernel\";\n"
1138 " sysname = \"%s\";\n"
1139 " kernel_release = \"%s\";\n"
1140 " kernel_version = \"%s\";\n"
1141 " tracer_name = \"lttng-modules\";\n"
1142 " tracer_major = %d;\n"
1143 " tracer_minor = %d;\n"
1144 " tracer_patchlevel = %d;\n"
1146 current
->nsproxy
->uts_ns
->name
.nodename
,
1150 LTTNG_MODULES_MAJOR_VERSION
,
1151 LTTNG_MODULES_MINOR_VERSION
,
1152 LTTNG_MODULES_PATCHLEVEL_VERSION
1157 ret
= lttng_metadata_printf(session
,
1165 if (!trace_clock_uuid(clock_uuid_s
)) {
1166 ret
= lttng_metadata_printf(session
,
1167 " uuid = \"%s\";\n",
1174 ret
= lttng_metadata_printf(session
,
1175 " description = \"Monotonic Clock\";\n"
1176 " freq = %llu; /* Frequency, in Hz */\n"
1177 " /* clock value offset from Epoch is: offset * (1/freq) */\n"
1180 (unsigned long long) trace_clock_freq(),
1181 (unsigned long long) measure_clock_offset()
1186 ret
= lttng_metadata_printf(session
,
1187 "typealias integer {\n"
1188 " size = 27; align = 1; signed = false;\n"
1189 " map = clock.monotonic.value;\n"
1190 "} := uint27_clock_monotonic_t;\n"
1192 "typealias integer {\n"
1193 " size = 32; align = %u; signed = false;\n"
1194 " map = clock.monotonic.value;\n"
1195 "} := uint32_clock_monotonic_t;\n"
1197 "typealias integer {\n"
1198 " size = 64; align = %u; signed = false;\n"
1199 " map = clock.monotonic.value;\n"
1200 "} := uint64_clock_monotonic_t;\n\n",
1201 lttng_alignof(uint32_t) * CHAR_BIT
,
1202 lttng_alignof(uint64_t) * CHAR_BIT
1207 ret
= _lttng_stream_packet_context_declare(session
);
1211 ret
= _lttng_event_header_declare(session
);
1216 list_for_each_entry(chan
, &session
->chan
, list
) {
1217 ret
= _lttng_channel_metadata_statedump(session
, chan
);
1222 list_for_each_entry(event
, &session
->events
, list
) {
1223 ret
= _lttng_event_metadata_statedump(session
, event
->chan
, event
);
1227 session
->metadata_dumped
= 1;
1233 * lttng_transport_register - LTT transport registration
1234 * @transport: transport structure
1236 * Registers a transport which can be used as output to extract the data out of
1237 * LTTng. The module calling this registration function must ensure that no
1238 * trap-inducing code will be executed by the transport functions. E.g.
1239 * vmalloc_sync_all() must be called between a vmalloc and the moment the memory
1240 * is made visible to the transport function. This registration acts as a
1241 * vmalloc_sync_all. Therefore, only if the module allocates virtual memory
1242 * after its registration must it synchronize the TLBs.
1244 void lttng_transport_register(struct lttng_transport
*transport
)
1247 * Make sure no page fault can be triggered by the module about to be
1248 * registered. We deal with this here so we don't have to call
1249 * vmalloc_sync_all() in each module's init.
1251 wrapper_vmalloc_sync_all();
1253 mutex_lock(&sessions_mutex
);
1254 list_add_tail(&transport
->node
, <tng_transport_list
);
1255 mutex_unlock(&sessions_mutex
);
1257 EXPORT_SYMBOL_GPL(lttng_transport_register
);
1260 * lttng_transport_unregister - LTT transport unregistration
1261 * @transport: transport structure
1263 void lttng_transport_unregister(struct lttng_transport
*transport
)
1265 mutex_lock(&sessions_mutex
);
1266 list_del(&transport
->node
);
1267 mutex_unlock(&sessions_mutex
);
1269 EXPORT_SYMBOL_GPL(lttng_transport_unregister
);
1271 static int __init
lttng_events_init(void)
1275 ret
= wrapper_lttng_fixup_sig(THIS_MODULE
);
1279 ret
= lttng_tracepoint_init();
1282 event_cache
= KMEM_CACHE(lttng_event
, 0);
1287 ret
= lttng_abi_init();
1290 ret
= lttng_logger_init();
1298 kmem_cache_destroy(event_cache
);
1300 lttng_tracepoint_exit();
1304 module_init(lttng_events_init
);
1306 static void __exit
lttng_events_exit(void)
1308 struct lttng_session
*session
, *tmpsession
;
1310 lttng_logger_exit();
1312 list_for_each_entry_safe(session
, tmpsession
, &sessions
, list
)
1313 lttng_session_destroy(session
);
1314 kmem_cache_destroy(event_cache
);
1315 lttng_tracepoint_exit();
1318 module_exit(lttng_events_exit
);
1320 MODULE_LICENSE("GPL and additional rights");
1321 MODULE_AUTHOR("Mathieu Desnoyers <mathieu.desnoyers@efficios.com>");
1322 MODULE_DESCRIPTION("LTTng Events");
1323 MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION
) "."
1324 __stringify(LTTNG_MODULES_MINOR_VERSION
) "."
1325 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION
)
1326 LTTNG_MODULES_EXTRAVERSION
);