4 * Copyright 2010 (c) - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 * Holds LTTng per-session event registry.
8 * Dual LGPL v2.1/GPL v2 license.
13 #include <urcu/list.h>
16 #include <urcu/compiler.h>
17 #include <urcu/uatomic.h>
18 #include <uuid/uuid.h>
19 #include <ust/tracepoint.h>
21 #include "usterr_signal_safe.h"
23 #include "ltt-events.h"
24 #include "ltt-tracer.h"
27 static CDS_LIST_HEAD(sessions
);
28 static CDS_LIST_HEAD(ltt_transport_list
);
29 static DEFINE_MUTEX(sessions_mutex
);
31 static void _ltt_event_destroy(struct ltt_event
*event
);
32 static void _ltt_channel_destroy(struct ltt_channel
*chan
);
33 static int _ltt_event_unregister(struct ltt_event
*event
);
35 int _ltt_event_metadata_statedump(struct ltt_session
*session
,
36 struct ltt_channel
*chan
,
37 struct ltt_event
*event
);
39 int _ltt_session_metadata_statedump(struct ltt_session
*session
);
41 void synchronize_trace(void)
46 struct ltt_session
*ltt_session_create(void)
48 struct ltt_session
*session
;
50 pthread_mutex_lock(&sessions_mutex
);
51 session
= zmalloc(sizeof(struct ltt_session
));
54 CDS_INIT_LIST_HEAD(&session
->chan
);
55 CDS_INIT_LIST_HEAD(&session
->events
);
56 uuid_generate(session
->uuid
);
57 cds_list_add(&session
->list
, &sessions
);
58 pthread_mutex_unlock(&sessions_mutex
);
62 void ltt_session_destroy(struct ltt_session
*session
)
64 struct ltt_channel
*chan
, *tmpchan
;
65 struct ltt_event
*event
, *tmpevent
;
68 pthread_mutex_lock(&sessions_mutex
);
69 CMM_ACCESS_ONCE(session
->active
) = 0;
70 cds_list_for_each_entry(event
, &session
->events
, list
) {
71 ret
= _ltt_event_unregister(event
);
74 synchronize_trace(); /* Wait for in-flight events to complete */
75 cds_list_for_each_entry_safe(event
, tmpevent
, &session
->events
, list
)
76 _ltt_event_destroy(event
);
77 cds_list_for_each_entry_safe(chan
, tmpchan
, &session
->chan
, list
)
78 _ltt_channel_destroy(chan
);
79 cds_list_del(&session
->list
);
80 pthread_mutex_unlock(&sessions_mutex
);
84 int ltt_session_enable(struct ltt_session
*session
)
87 struct ltt_channel
*chan
;
89 pthread_mutex_lock(&sessions_mutex
);
90 if (session
->active
) {
96 * Snapshot the number of events per channel to know the type of header
99 cds_list_for_each_entry(chan
, &session
->chan
, list
) {
100 if (chan
->header_type
)
101 continue; /* don't change it if session stop/restart */
102 if (chan
->free_event_id
< 31)
103 chan
->header_type
= 1; /* compact */
105 chan
->header_type
= 2; /* large */
108 CMM_ACCESS_ONCE(session
->active
) = 1;
109 CMM_ACCESS_ONCE(session
->been_active
) = 1;
110 ret
= _ltt_session_metadata_statedump(session
);
112 CMM_ACCESS_ONCE(session
->active
) = 0;
114 pthread_mutex_unlock(&sessions_mutex
);
118 int ltt_session_disable(struct ltt_session
*session
)
122 pthread_mutex_lock(&sessions_mutex
);
123 if (!session
->active
) {
127 CMM_ACCESS_ONCE(session
->active
) = 0;
129 pthread_mutex_unlock(&sessions_mutex
);
133 int ltt_channel_enable(struct ltt_channel
*channel
)
137 if (channel
== channel
->session
->metadata
)
139 old
= uatomic_xchg(&channel
->enabled
, 1);
145 int ltt_channel_disable(struct ltt_channel
*channel
)
149 if (channel
== channel
->session
->metadata
)
151 old
= uatomic_xchg(&channel
->enabled
, 0);
157 int ltt_event_enable(struct ltt_event
*event
)
161 if (event
->chan
== event
->chan
->session
->metadata
)
163 old
= uatomic_xchg(&event
->enabled
, 1);
169 int ltt_event_disable(struct ltt_event
*event
)
173 if (event
->chan
== event
->chan
->session
->metadata
)
175 old
= uatomic_xchg(&event
->enabled
, 0);
181 static struct ltt_transport
*ltt_transport_find(const char *name
)
183 struct ltt_transport
*transport
;
185 cds_list_for_each_entry(transport
, <t_transport_list
, node
) {
186 if (!strcmp(transport
->name
, name
))
192 struct ltt_channel
*ltt_channel_create(struct ltt_session
*session
,
193 const char *transport_name
,
195 size_t subbuf_size
, size_t num_subbuf
,
196 unsigned int switch_timer_interval
,
197 unsigned int read_timer_interval
,
200 struct ltt_channel
*chan
;
201 struct ltt_transport
*transport
;
203 pthread_mutex_lock(&sessions_mutex
);
204 if (session
->been_active
)
205 goto active
; /* Refuse to add channel to active session */
206 transport
= ltt_transport_find(transport_name
);
208 DBG("LTTng transport %s not found\n",
212 chan
= zmalloc(sizeof(struct ltt_channel
));
215 chan
->session
= session
;
216 chan
->id
= session
->free_chan_id
++;
218 * Note: the channel creation op already writes into the packet
219 * headers. Therefore the "chan" information used as input
220 * should be already accessible.
222 chan
->chan
= transport
->ops
.channel_create("[lttng]", chan
, buf_addr
,
223 subbuf_size
, num_subbuf
, switch_timer_interval
,
224 read_timer_interval
, shmid
);
228 chan
->ops
= &transport
->ops
;
229 cds_list_add(&chan
->list
, &session
->chan
);
230 pthread_mutex_unlock(&sessions_mutex
);
238 pthread_mutex_unlock(&sessions_mutex
);
243 * Only used internally at session destruction.
246 void _ltt_channel_destroy(struct ltt_channel
*chan
)
248 chan
->ops
->channel_destroy(chan
->chan
);
249 cds_list_del(&chan
->list
);
250 lttng_destroy_context(chan
->ctx
);
255 * Supports event creation while tracing session is active.
257 struct ltt_event
*ltt_event_create(struct ltt_channel
*chan
,
258 struct lttng_ust_event
*event_param
,
261 struct ltt_event
*event
;
264 pthread_mutex_lock(&sessions_mutex
);
265 if (chan
->free_event_id
== -1UL)
268 * This is O(n^2) (for each event, the loop is called at event
269 * creation). Might require a hash if we have lots of events.
271 cds_list_for_each_entry(event
, &chan
->session
->events
, list
)
272 if (!strcmp(event
->desc
->name
, event_param
->name
))
274 event
= zmalloc(sizeof(struct ltt_event
));
278 event
->filter
= filter
;
279 event
->id
= chan
->free_event_id
++;
281 event
->instrumentation
= event_param
->instrumentation
;
282 /* Populate ltt_event structure before tracepoint registration. */
284 switch (event_param
->instrumentation
) {
285 case LTTNG_UST_TRACEPOINT
:
286 event
->desc
= ltt_event_get(event_param
->name
);
289 ret
= __tracepoint_probe_register(event_param
->name
,
290 event
->desc
->probe_callback
,
298 ret
= _ltt_event_metadata_statedump(chan
->session
, chan
, event
);
300 goto statedump_error
;
301 cds_list_add(&event
->list
, &chan
->session
->events
);
302 pthread_mutex_unlock(&sessions_mutex
);
306 WARN_ON_ONCE(__tracepoint_probe_unregister(event_param
->name
,
307 event
->desc
->probe_callback
,
309 ltt_event_put(event
->desc
);
315 pthread_mutex_unlock(&sessions_mutex
);
320 * Only used internally at session destruction.
322 int _ltt_event_unregister(struct ltt_event
*event
)
326 switch (event
->instrumentation
) {
327 case LTTNG_UST_TRACEPOINT
:
328 ret
= __tracepoint_probe_unregister(event
->desc
->name
,
329 event
->desc
->probe_callback
,
341 * Only used internally at session destruction.
344 void _ltt_event_destroy(struct ltt_event
*event
)
346 switch (event
->instrumentation
) {
347 case LTTNG_UST_TRACEPOINT
:
348 ltt_event_put(event
->desc
);
353 cds_list_del(&event
->list
);
354 lttng_destroy_context(event
->ctx
);
359 * We have exclusive access to our metadata buffer (protected by the
360 * sessions_mutex), so we can do racy operations such as looking for
361 * remaining space left in packet and write, since mutual exclusion
362 * protects us from concurrent writes.
364 int lttng_metadata_printf(struct ltt_session
*session
,
365 const char *fmt
, ...)
367 struct lib_ring_buffer_ctx ctx
;
368 struct ltt_channel
*chan
= session
->metadata
;
370 int ret
= 0, waitret
;
371 size_t len
, reserve_len
, pos
;
374 WARN_ON_ONCE(!CMM_ACCESS_ONCE(session
->active
));
377 ret
= vasprintf(&str
, fmt
, ap
);
385 for (pos
= 0; pos
< len
; pos
+= reserve_len
) {
386 reserve_len
= min_t(size_t,
387 chan
->ops
->packet_avail_size(chan
->chan
),
389 lib_ring_buffer_ctx_init(&ctx
, chan
->chan
, NULL
, reserve_len
,
392 * We don't care about metadata buffer's records lost
393 * count, because we always retry here. Report error if
394 * we need to bail out after timeout or being
397 waitret
= wait_cond_interruptible_timeout(
399 ret
= chan
->ops
->event_reserve(&ctx
, 0);
400 ret
!= -ENOBUFS
|| !ret
;
402 LTTNG_METADATA_TIMEOUT_MSEC
);
403 if (!waitret
|| waitret
== -EINTR
|| ret
) {
404 DBG("LTTng: Failure to write metadata to buffers (%s)\n",
405 waitret
== -EINTR
? "interrupted" :
406 (ret
== -ENOBUFS
? "timeout" : "I/O error"));
407 if (waitret
== -EINTR
)
411 chan
->ops
->event_write(&ctx
, &str
[pos
], reserve_len
);
412 chan
->ops
->event_commit(&ctx
);
420 int _ltt_field_statedump(struct ltt_session
*session
,
421 const struct lttng_event_field
*field
)
425 switch (field
->type
.atype
) {
427 ret
= lttng_metadata_printf(session
,
428 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } %s;\n",
429 field
->type
.u
.basic
.integer
.size
,
430 field
->type
.u
.basic
.integer
.alignment
,
431 field
->type
.u
.basic
.integer
.signedness
,
432 (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_none
)
434 : (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
437 field
->type
.u
.basic
.integer
.base
,
439 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
441 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
446 ret
= lttng_metadata_printf(session
,
447 " floating_point { exp_dig = %u; mant_dig = %u; align = %u; } %s;\n",
448 field
->type
.u
.basic
._float
.exp_dig
,
449 field
->type
.u
.basic
._float
.mant_dig
,
450 field
->type
.u
.basic
._float
.alignment
,
452 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
454 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
459 ret
= lttng_metadata_printf(session
,
461 field
->type
.u
.basic
.enumeration
.name
,
466 const struct lttng_basic_type
*elem_type
;
468 elem_type
= &field
->type
.u
.array
.elem_type
;
469 ret
= lttng_metadata_printf(session
,
470 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } %s[%u];\n",
471 elem_type
->u
.basic
.integer
.size
,
472 elem_type
->u
.basic
.integer
.alignment
,
473 elem_type
->u
.basic
.integer
.signedness
,
474 (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
476 : (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
479 elem_type
->u
.basic
.integer
.base
,
481 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
483 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
485 field
->name
, field
->type
.u
.array
.length
);
490 const struct lttng_basic_type
*elem_type
;
491 const struct lttng_basic_type
*length_type
;
493 elem_type
= &field
->type
.u
.sequence
.elem_type
;
494 length_type
= &field
->type
.u
.sequence
.length_type
;
495 ret
= lttng_metadata_printf(session
,
496 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n",
497 length_type
->u
.basic
.integer
.size
,
498 (unsigned int) length_type
->u
.basic
.integer
.alignment
,
499 length_type
->u
.basic
.integer
.signedness
,
500 (length_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
502 : ((length_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
505 length_type
->u
.basic
.integer
.base
,
507 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
509 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
515 ret
= lttng_metadata_printf(session
,
516 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } %s[ __%s_length ];\n",
517 elem_type
->u
.basic
.integer
.size
,
518 (unsigned int) elem_type
->u
.basic
.integer
.alignment
,
519 elem_type
->u
.basic
.integer
.signedness
,
520 (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
522 : ((elem_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
525 elem_type
->u
.basic
.integer
.base
,
527 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
529 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
537 /* Default encoding is UTF8 */
538 ret
= lttng_metadata_printf(session
,
540 field
->type
.u
.basic
.string
.encoding
== lttng_encode_ASCII
?
541 " { encoding = ASCII; }" : "",
552 int _ltt_context_metadata_statedump(struct ltt_session
*session
,
553 struct lttng_ctx
*ctx
)
560 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
561 const struct lttng_ctx_field
*field
= &ctx
->fields
[i
];
563 ret
= _ltt_field_statedump(session
, &field
->event_field
);
571 int _ltt_fields_metadata_statedump(struct ltt_session
*session
,
572 struct ltt_event
*event
)
574 const struct lttng_event_desc
*desc
= event
->desc
;
578 for (i
= 0; i
< desc
->nr_fields
; i
++) {
579 const struct lttng_event_field
*field
= &desc
->fields
[i
];
581 ret
= _ltt_field_statedump(session
, field
);
589 int _ltt_event_metadata_statedump(struct ltt_session
*session
,
590 struct ltt_channel
*chan
,
591 struct ltt_event
*event
)
595 if (event
->metadata_dumped
|| !CMM_ACCESS_ONCE(session
->active
))
597 if (chan
== session
->metadata
)
600 ret
= lttng_metadata_printf(session
,
604 " stream_id = %u;\n",
612 ret
= lttng_metadata_printf(session
,
613 " context := struct {\n");
617 ret
= _ltt_context_metadata_statedump(session
, event
->ctx
);
621 ret
= lttng_metadata_printf(session
,
627 ret
= lttng_metadata_printf(session
,
628 " fields := struct {\n"
633 ret
= _ltt_fields_metadata_statedump(session
, event
);
638 * LTTng space reservation can only reserve multiples of the
641 ret
= lttng_metadata_printf(session
,
647 event
->metadata_dumped
= 1;
654 int _ltt_channel_metadata_statedump(struct ltt_session
*session
,
655 struct ltt_channel
*chan
)
659 if (chan
->metadata_dumped
|| !CMM_ACCESS_ONCE(session
->active
))
661 if (chan
== session
->metadata
)
664 WARN_ON_ONCE(!chan
->header_type
);
665 ret
= lttng_metadata_printf(session
,
668 " event.header := %s;\n"
669 " packet.context := struct packet_context;\n",
671 chan
->header_type
== 1 ? "struct event_header_compact" :
672 "struct event_header_large");
677 ret
= lttng_metadata_printf(session
,
678 " event.context := struct {\n");
682 ret
= _ltt_context_metadata_statedump(session
, chan
->ctx
);
686 ret
= lttng_metadata_printf(session
,
692 ret
= lttng_metadata_printf(session
,
695 chan
->metadata_dumped
= 1;
701 int _ltt_stream_packet_context_declare(struct ltt_session
*session
)
703 return lttng_metadata_printf(session
,
704 "struct packet_context {\n"
705 " uint64_t timestamp_begin;\n"
706 " uint64_t timestamp_end;\n"
707 " uint32_t events_discarded;\n"
708 " uint32_t content_size;\n"
709 " uint32_t packet_size;\n"
710 " uint32_t cpu_id;\n"
718 * id 31 is reserved to indicate an extended header.
721 * id: range: 0 - 65534.
722 * id 65535 is reserved to indicate an extended header.
725 int _ltt_event_header_declare(struct ltt_session
*session
)
727 return lttng_metadata_printf(session
,
728 "struct event_header_compact {\n"
729 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
732 " uint27_t timestamp;\n"
736 " uint64_t timestamp;\n"
741 "struct event_header_large {\n"
742 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
745 " uint32_t timestamp;\n"
749 " uint64_t timestamp;\n"
753 ltt_alignof(uint32_t) * CHAR_BIT
,
754 ltt_alignof(uint16_t) * CHAR_BIT
759 * Output metadata into this session's metadata buffers.
762 int _ltt_session_metadata_statedump(struct ltt_session
*session
)
764 unsigned char *uuid_c
= session
->uuid
;
766 struct ltt_channel
*chan
;
767 struct ltt_event
*event
;
770 if (!CMM_ACCESS_ONCE(session
->active
))
772 if (session
->metadata_dumped
)
774 if (!session
->metadata
) {
775 DBG("LTTng: attempt to start tracing, but metadata channel is not found. Operation abort.\n");
779 snprintf(uuid_s
, sizeof(uuid_s
),
780 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
781 uuid_c
[0], uuid_c
[1], uuid_c
[2], uuid_c
[3],
782 uuid_c
[4], uuid_c
[5], uuid_c
[6], uuid_c
[7],
783 uuid_c
[8], uuid_c
[9], uuid_c
[10], uuid_c
[11],
784 uuid_c
[12], uuid_c
[13], uuid_c
[14], uuid_c
[15]);
786 ret
= lttng_metadata_printf(session
,
787 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
788 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
789 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
790 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
791 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
792 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
798 " byte_order = %s;\n"
799 " packet.header := struct {\n"
801 " uint8_t uuid[16];\n"
802 " uint32_t stream_id;\n"
805 ltt_alignof(uint8_t) * CHAR_BIT
,
806 ltt_alignof(uint16_t) * CHAR_BIT
,
807 ltt_alignof(uint32_t) * CHAR_BIT
,
808 ltt_alignof(uint64_t) * CHAR_BIT
,
821 ret
= _ltt_stream_packet_context_declare(session
);
825 ret
= _ltt_event_header_declare(session
);
830 cds_list_for_each_entry(chan
, &session
->chan
, list
) {
831 ret
= _ltt_channel_metadata_statedump(session
, chan
);
836 cds_list_for_each_entry(event
, &session
->events
, list
) {
837 ret
= _ltt_event_metadata_statedump(session
, event
->chan
, event
);
841 session
->metadata_dumped
= 1;
847 * ltt_transport_register - LTT transport registration
848 * @transport: transport structure
850 * Registers a transport which can be used as output to extract the data out of
853 void ltt_transport_register(struct ltt_transport
*transport
)
855 pthread_mutex_lock(&sessions_mutex
);
856 cds_list_add_tail(&transport
->node
, <t_transport_list
);
857 pthread_mutex_unlock(&sessions_mutex
);
861 * ltt_transport_unregister - LTT transport unregistration
862 * @transport: transport structure
864 void ltt_transport_unregister(struct ltt_transport
*transport
)
866 pthread_mutex_lock(&sessions_mutex
);
867 cds_list_del(&transport
->node
);
868 pthread_mutex_unlock(&sessions_mutex
);
872 void __attribute__((destructor
)) ltt_events_exit(void)
874 struct ltt_session
*session
, *tmpsession
;
876 cds_list_for_each_entry_safe(session
, tmpsession
, &sessions
, list
)
877 ltt_session_destroy(session
);