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>
23 #include <ust/lttng-events.h>
24 #include <ust/usterr-signal-safe.h>
26 #include "ltt-tracer.h"
28 #include "../libringbuffer/shm.h"
30 static CDS_LIST_HEAD(sessions
);
31 static CDS_LIST_HEAD(ltt_transport_list
);
32 static DEFINE_MUTEX(sessions_mutex
);
34 static void _ltt_event_destroy(struct ltt_event
*event
);
35 static void _ltt_channel_destroy(struct ltt_channel
*chan
);
36 static int _ltt_event_unregister(struct ltt_event
*event
);
38 int _ltt_event_metadata_statedump(struct ltt_session
*session
,
39 struct ltt_channel
*chan
,
40 struct ltt_event
*event
);
42 int _ltt_session_metadata_statedump(struct ltt_session
*session
);
44 void synchronize_trace(void)
49 struct ltt_session
*ltt_session_create(void)
51 struct ltt_session
*session
;
53 pthread_mutex_lock(&sessions_mutex
);
54 session
= zmalloc(sizeof(struct ltt_session
));
57 CDS_INIT_LIST_HEAD(&session
->chan
);
58 CDS_INIT_LIST_HEAD(&session
->events
);
59 uuid_generate(session
->uuid
);
60 cds_list_add(&session
->list
, &sessions
);
61 pthread_mutex_unlock(&sessions_mutex
);
65 void ltt_session_destroy(struct ltt_session
*session
)
67 struct ltt_channel
*chan
, *tmpchan
;
68 struct ltt_event
*event
, *tmpevent
;
71 pthread_mutex_lock(&sessions_mutex
);
72 CMM_ACCESS_ONCE(session
->active
) = 0;
73 cds_list_for_each_entry(event
, &session
->events
, list
) {
74 ret
= _ltt_event_unregister(event
);
77 synchronize_trace(); /* Wait for in-flight events to complete */
78 cds_list_for_each_entry_safe(event
, tmpevent
, &session
->events
, list
)
79 _ltt_event_destroy(event
);
80 cds_list_for_each_entry_safe(chan
, tmpchan
, &session
->chan
, list
)
81 _ltt_channel_destroy(chan
);
82 cds_list_del(&session
->list
);
83 pthread_mutex_unlock(&sessions_mutex
);
87 int ltt_session_enable(struct ltt_session
*session
)
90 struct ltt_channel
*chan
;
92 pthread_mutex_lock(&sessions_mutex
);
93 if (session
->active
) {
99 * Snapshot the number of events per channel to know the type of header
102 cds_list_for_each_entry(chan
, &session
->chan
, list
) {
103 if (chan
->header_type
)
104 continue; /* don't change it if session stop/restart */
105 if (chan
->free_event_id
< 31)
106 chan
->header_type
= 1; /* compact */
108 chan
->header_type
= 2; /* large */
111 CMM_ACCESS_ONCE(session
->active
) = 1;
112 CMM_ACCESS_ONCE(session
->been_active
) = 1;
113 ret
= _ltt_session_metadata_statedump(session
);
115 CMM_ACCESS_ONCE(session
->active
) = 0;
117 pthread_mutex_unlock(&sessions_mutex
);
121 int ltt_session_disable(struct ltt_session
*session
)
125 pthread_mutex_lock(&sessions_mutex
);
126 if (!session
->active
) {
130 CMM_ACCESS_ONCE(session
->active
) = 0;
132 pthread_mutex_unlock(&sessions_mutex
);
136 int ltt_channel_enable(struct ltt_channel
*channel
)
140 if (channel
== channel
->session
->metadata
)
142 old
= uatomic_xchg(&channel
->enabled
, 1);
148 int ltt_channel_disable(struct ltt_channel
*channel
)
152 if (channel
== channel
->session
->metadata
)
154 old
= uatomic_xchg(&channel
->enabled
, 0);
160 int ltt_event_enable(struct ltt_event
*event
)
164 if (event
->chan
== event
->chan
->session
->metadata
)
166 old
= uatomic_xchg(&event
->enabled
, 1);
172 int ltt_event_disable(struct ltt_event
*event
)
176 if (event
->chan
== event
->chan
->session
->metadata
)
178 old
= uatomic_xchg(&event
->enabled
, 0);
184 static struct ltt_transport
*ltt_transport_find(const char *name
)
186 struct ltt_transport
*transport
;
188 cds_list_for_each_entry(transport
, <t_transport_list
, node
) {
189 if (!strcmp(transport
->name
, name
))
195 struct ltt_channel
*ltt_channel_create(struct ltt_session
*session
,
196 const char *transport_name
,
198 size_t subbuf_size
, size_t num_subbuf
,
199 unsigned int switch_timer_interval
,
200 unsigned int read_timer_interval
)
202 struct ltt_channel
*chan
;
203 struct ltt_transport
*transport
;
205 pthread_mutex_lock(&sessions_mutex
);
206 if (session
->been_active
)
207 goto active
; /* Refuse to add channel to active session */
208 transport
= ltt_transport_find(transport_name
);
210 DBG("LTTng transport %s not found\n",
214 chan
= zmalloc(sizeof(struct ltt_channel
));
217 chan
->session
= session
;
218 chan
->id
= session
->free_chan_id
++;
220 * Note: the channel creation op already writes into the packet
221 * headers. Therefore the "chan" information used as input
222 * should be already accessible.
224 chan
->handle
= transport
->ops
.channel_create("[lttng]", chan
, buf_addr
,
225 subbuf_size
, num_subbuf
, switch_timer_interval
,
226 read_timer_interval
);
227 chan
->chan
= shmp(chan
->handle
->header
->chan
);
231 chan
->ops
= &transport
->ops
;
232 cds_list_add(&chan
->list
, &session
->chan
);
233 pthread_mutex_unlock(&sessions_mutex
);
241 pthread_mutex_unlock(&sessions_mutex
);
246 * Only used internally at session destruction.
249 void _ltt_channel_destroy(struct ltt_channel
*chan
)
251 chan
->ops
->channel_destroy(chan
->handle
);
252 cds_list_del(&chan
->list
);
253 lttng_destroy_context(chan
->ctx
);
258 * Supports event creation while tracing session is active.
260 struct ltt_event
*ltt_event_create(struct ltt_channel
*chan
,
261 struct lttng_ust_event
*event_param
,
264 struct ltt_event
*event
;
267 pthread_mutex_lock(&sessions_mutex
);
268 if (chan
->free_event_id
== -1UL)
271 * This is O(n^2) (for each event, the loop is called at event
272 * creation). Might require a hash if we have lots of events.
274 cds_list_for_each_entry(event
, &chan
->session
->events
, list
)
275 if (!strcmp(event
->desc
->name
, event_param
->name
))
277 event
= zmalloc(sizeof(struct ltt_event
));
281 event
->filter
= filter
;
282 event
->id
= chan
->free_event_id
++;
284 event
->instrumentation
= event_param
->instrumentation
;
285 /* Populate ltt_event structure before tracepoint registration. */
287 switch (event_param
->instrumentation
) {
288 case LTTNG_UST_TRACEPOINT
:
289 event
->desc
= ltt_event_get(event_param
->name
);
292 ret
= __tracepoint_probe_register(event_param
->name
,
293 event
->desc
->probe_callback
,
301 ret
= _ltt_event_metadata_statedump(chan
->session
, chan
, event
);
303 goto statedump_error
;
304 cds_list_add(&event
->list
, &chan
->session
->events
);
305 pthread_mutex_unlock(&sessions_mutex
);
309 WARN_ON_ONCE(__tracepoint_probe_unregister(event_param
->name
,
310 event
->desc
->probe_callback
,
312 ltt_event_put(event
->desc
);
318 pthread_mutex_unlock(&sessions_mutex
);
323 * Only used internally at session destruction.
325 int _ltt_event_unregister(struct ltt_event
*event
)
329 switch (event
->instrumentation
) {
330 case LTTNG_UST_TRACEPOINT
:
331 ret
= __tracepoint_probe_unregister(event
->desc
->name
,
332 event
->desc
->probe_callback
,
344 * Only used internally at session destruction.
347 void _ltt_event_destroy(struct ltt_event
*event
)
349 switch (event
->instrumentation
) {
350 case LTTNG_UST_TRACEPOINT
:
351 ltt_event_put(event
->desc
);
356 cds_list_del(&event
->list
);
357 lttng_destroy_context(event
->ctx
);
362 * We have exclusive access to our metadata buffer (protected by the
363 * sessions_mutex), so we can do racy operations such as looking for
364 * remaining space left in packet and write, since mutual exclusion
365 * protects us from concurrent writes.
367 int lttng_metadata_printf(struct ltt_session
*session
,
368 const char *fmt
, ...)
370 struct lib_ring_buffer_ctx ctx
;
371 struct ltt_channel
*chan
= session
->metadata
;
373 int ret
= 0, waitret
;
374 size_t len
, reserve_len
, pos
;
377 WARN_ON_ONCE(!CMM_ACCESS_ONCE(session
->active
));
380 ret
= vasprintf(&str
, fmt
, ap
);
388 for (pos
= 0; pos
< len
; pos
+= reserve_len
) {
389 reserve_len
= min_t(size_t,
390 chan
->ops
->packet_avail_size(chan
->chan
),
392 lib_ring_buffer_ctx_init(&ctx
, chan
->chan
, NULL
, reserve_len
,
395 * We don't care about metadata buffer's records lost
396 * count, because we always retry here. Report error if
397 * we need to bail out after timeout or being
400 waitret
= wait_cond_interruptible_timeout(
402 ret
= chan
->ops
->event_reserve(&ctx
, 0);
403 ret
!= -ENOBUFS
|| !ret
;
405 LTTNG_METADATA_TIMEOUT_MSEC
);
406 if (!waitret
|| waitret
== -EINTR
|| ret
) {
407 DBG("LTTng: Failure to write metadata to buffers (%s)\n",
408 waitret
== -EINTR
? "interrupted" :
409 (ret
== -ENOBUFS
? "timeout" : "I/O error"));
410 if (waitret
== -EINTR
)
414 chan
->ops
->event_write(&ctx
, &str
[pos
], reserve_len
);
415 chan
->ops
->event_commit(&ctx
);
423 int _ltt_field_statedump(struct ltt_session
*session
,
424 const struct lttng_event_field
*field
)
428 switch (field
->type
.atype
) {
430 ret
= lttng_metadata_printf(session
,
431 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } %s;\n",
432 field
->type
.u
.basic
.integer
.size
,
433 field
->type
.u
.basic
.integer
.alignment
,
434 field
->type
.u
.basic
.integer
.signedness
,
435 (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_none
)
437 : (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
440 field
->type
.u
.basic
.integer
.base
,
442 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
444 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
449 ret
= lttng_metadata_printf(session
,
450 " floating_point { exp_dig = %u; mant_dig = %u; align = %u; } %s;\n",
451 field
->type
.u
.basic
._float
.exp_dig
,
452 field
->type
.u
.basic
._float
.mant_dig
,
453 field
->type
.u
.basic
._float
.alignment
,
455 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
457 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
462 ret
= lttng_metadata_printf(session
,
464 field
->type
.u
.basic
.enumeration
.name
,
469 const struct lttng_basic_type
*elem_type
;
471 elem_type
= &field
->type
.u
.array
.elem_type
;
472 ret
= lttng_metadata_printf(session
,
473 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } %s[%u];\n",
474 elem_type
->u
.basic
.integer
.size
,
475 elem_type
->u
.basic
.integer
.alignment
,
476 elem_type
->u
.basic
.integer
.signedness
,
477 (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
479 : (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
482 elem_type
->u
.basic
.integer
.base
,
484 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
486 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
488 field
->name
, field
->type
.u
.array
.length
);
493 const struct lttng_basic_type
*elem_type
;
494 const struct lttng_basic_type
*length_type
;
496 elem_type
= &field
->type
.u
.sequence
.elem_type
;
497 length_type
= &field
->type
.u
.sequence
.length_type
;
498 ret
= lttng_metadata_printf(session
,
499 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n",
500 length_type
->u
.basic
.integer
.size
,
501 (unsigned int) length_type
->u
.basic
.integer
.alignment
,
502 length_type
->u
.basic
.integer
.signedness
,
503 (length_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
505 : ((length_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
508 length_type
->u
.basic
.integer
.base
,
510 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
512 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
518 ret
= lttng_metadata_printf(session
,
519 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } %s[ __%s_length ];\n",
520 elem_type
->u
.basic
.integer
.size
,
521 (unsigned int) elem_type
->u
.basic
.integer
.alignment
,
522 elem_type
->u
.basic
.integer
.signedness
,
523 (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
525 : ((elem_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
528 elem_type
->u
.basic
.integer
.base
,
530 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
532 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
540 /* Default encoding is UTF8 */
541 ret
= lttng_metadata_printf(session
,
543 field
->type
.u
.basic
.string
.encoding
== lttng_encode_ASCII
?
544 " { encoding = ASCII; }" : "",
555 int _ltt_context_metadata_statedump(struct ltt_session
*session
,
556 struct lttng_ctx
*ctx
)
563 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
564 const struct lttng_ctx_field
*field
= &ctx
->fields
[i
];
566 ret
= _ltt_field_statedump(session
, &field
->event_field
);
574 int _ltt_fields_metadata_statedump(struct ltt_session
*session
,
575 struct ltt_event
*event
)
577 const struct lttng_event_desc
*desc
= event
->desc
;
581 for (i
= 0; i
< desc
->nr_fields
; i
++) {
582 const struct lttng_event_field
*field
= &desc
->fields
[i
];
584 ret
= _ltt_field_statedump(session
, field
);
592 int _ltt_event_metadata_statedump(struct ltt_session
*session
,
593 struct ltt_channel
*chan
,
594 struct ltt_event
*event
)
598 if (event
->metadata_dumped
|| !CMM_ACCESS_ONCE(session
->active
))
600 if (chan
== session
->metadata
)
603 ret
= lttng_metadata_printf(session
,
607 " stream_id = %u;\n",
615 ret
= lttng_metadata_printf(session
,
616 " context := struct {\n");
620 ret
= _ltt_context_metadata_statedump(session
, event
->ctx
);
624 ret
= lttng_metadata_printf(session
,
630 ret
= lttng_metadata_printf(session
,
631 " fields := struct {\n"
636 ret
= _ltt_fields_metadata_statedump(session
, event
);
641 * LTTng space reservation can only reserve multiples of the
644 ret
= lttng_metadata_printf(session
,
650 event
->metadata_dumped
= 1;
657 int _ltt_channel_metadata_statedump(struct ltt_session
*session
,
658 struct ltt_channel
*chan
)
662 if (chan
->metadata_dumped
|| !CMM_ACCESS_ONCE(session
->active
))
664 if (chan
== session
->metadata
)
667 WARN_ON_ONCE(!chan
->header_type
);
668 ret
= lttng_metadata_printf(session
,
671 " event.header := %s;\n"
672 " packet.context := struct packet_context;\n",
674 chan
->header_type
== 1 ? "struct event_header_compact" :
675 "struct event_header_large");
680 ret
= lttng_metadata_printf(session
,
681 " event.context := struct {\n");
685 ret
= _ltt_context_metadata_statedump(session
, chan
->ctx
);
689 ret
= lttng_metadata_printf(session
,
695 ret
= lttng_metadata_printf(session
,
698 chan
->metadata_dumped
= 1;
704 int _ltt_stream_packet_context_declare(struct ltt_session
*session
)
706 return lttng_metadata_printf(session
,
707 "struct packet_context {\n"
708 " uint64_t timestamp_begin;\n"
709 " uint64_t timestamp_end;\n"
710 " uint32_t events_discarded;\n"
711 " uint32_t content_size;\n"
712 " uint32_t packet_size;\n"
713 " uint32_t cpu_id;\n"
721 * id 31 is reserved to indicate an extended header.
724 * id: range: 0 - 65534.
725 * id 65535 is reserved to indicate an extended header.
728 int _ltt_event_header_declare(struct ltt_session
*session
)
730 return lttng_metadata_printf(session
,
731 "struct event_header_compact {\n"
732 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
735 " uint27_t timestamp;\n"
739 " uint64_t timestamp;\n"
744 "struct event_header_large {\n"
745 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
748 " uint32_t timestamp;\n"
752 " uint64_t timestamp;\n"
756 lttng_alignof(uint32_t) * CHAR_BIT
,
757 lttng_alignof(uint16_t) * CHAR_BIT
762 * Output metadata into this session's metadata buffers.
765 int _ltt_session_metadata_statedump(struct ltt_session
*session
)
767 unsigned char *uuid_c
= session
->uuid
;
769 struct ltt_channel
*chan
;
770 struct ltt_event
*event
;
773 if (!CMM_ACCESS_ONCE(session
->active
))
775 if (session
->metadata_dumped
)
777 if (!session
->metadata
) {
778 DBG("LTTng: attempt to start tracing, but metadata channel is not found. Operation abort.\n");
782 snprintf(uuid_s
, sizeof(uuid_s
),
783 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
784 uuid_c
[0], uuid_c
[1], uuid_c
[2], uuid_c
[3],
785 uuid_c
[4], uuid_c
[5], uuid_c
[6], uuid_c
[7],
786 uuid_c
[8], uuid_c
[9], uuid_c
[10], uuid_c
[11],
787 uuid_c
[12], uuid_c
[13], uuid_c
[14], uuid_c
[15]);
789 ret
= lttng_metadata_printf(session
,
790 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
791 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
792 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
793 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
794 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
795 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
801 " byte_order = %s;\n"
802 " packet.header := struct {\n"
804 " uint8_t uuid[16];\n"
805 " uint32_t stream_id;\n"
808 lttng_alignof(uint8_t) * CHAR_BIT
,
809 lttng_alignof(uint16_t) * CHAR_BIT
,
810 lttng_alignof(uint32_t) * CHAR_BIT
,
811 lttng_alignof(uint64_t) * CHAR_BIT
,
824 ret
= _ltt_stream_packet_context_declare(session
);
828 ret
= _ltt_event_header_declare(session
);
833 cds_list_for_each_entry(chan
, &session
->chan
, list
) {
834 ret
= _ltt_channel_metadata_statedump(session
, chan
);
839 cds_list_for_each_entry(event
, &session
->events
, list
) {
840 ret
= _ltt_event_metadata_statedump(session
, event
->chan
, event
);
844 session
->metadata_dumped
= 1;
850 * ltt_transport_register - LTT transport registration
851 * @transport: transport structure
853 * Registers a transport which can be used as output to extract the data out of
856 void ltt_transport_register(struct ltt_transport
*transport
)
858 pthread_mutex_lock(&sessions_mutex
);
859 cds_list_add_tail(&transport
->node
, <t_transport_list
);
860 pthread_mutex_unlock(&sessions_mutex
);
864 * ltt_transport_unregister - LTT transport unregistration
865 * @transport: transport structure
867 void ltt_transport_unregister(struct ltt_transport
*transport
)
869 pthread_mutex_lock(&sessions_mutex
);
870 cds_list_del(&transport
->node
);
871 pthread_mutex_unlock(&sessions_mutex
);
875 void __attribute__((destructor
)) ltt_events_exit(void)
877 struct ltt_session
*session
, *tmpsession
;
879 cds_list_for_each_entry_safe(session
, tmpsession
, &sessions
, list
)
880 ltt_session_destroy(session
);