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 session
= zmalloc(sizeof(struct ltt_session
));
56 pthread_mutex_lock(&sessions_mutex
);
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 transport
->ops
.channel_create("[lttng]", chan
, buf_addr
,
225 subbuf_size
, num_subbuf
, switch_timer_interval
,
226 read_timer_interval
);
230 chan
->ops
= &transport
->ops
;
231 cds_list_add(&chan
->list
, &session
->chan
);
232 pthread_mutex_unlock(&sessions_mutex
);
240 pthread_mutex_unlock(&sessions_mutex
);
245 * Only used internally at session destruction.
248 void _ltt_channel_destroy(struct ltt_channel
*chan
)
250 chan
->ops
->channel_destroy(chan
);
251 cds_list_del(&chan
->list
);
252 lttng_destroy_context(chan
->ctx
);
257 * Supports event creation while tracing session is active.
259 struct ltt_event
*ltt_event_create(struct ltt_channel
*chan
,
260 struct lttng_ust_event
*event_param
,
263 struct ltt_event
*event
;
266 pthread_mutex_lock(&sessions_mutex
);
267 if (chan
->free_event_id
== -1UL)
270 * This is O(n^2) (for each event, the loop is called at event
271 * creation). Might require a hash if we have lots of events.
273 cds_list_for_each_entry(event
, &chan
->session
->events
, list
)
274 if (!strcmp(event
->desc
->name
, event_param
->name
))
276 event
= zmalloc(sizeof(struct ltt_event
));
280 event
->filter
= filter
;
281 event
->id
= chan
->free_event_id
++;
283 event
->instrumentation
= event_param
->instrumentation
;
284 /* Populate ltt_event structure before tracepoint registration. */
286 switch (event_param
->instrumentation
) {
287 case LTTNG_UST_TRACEPOINT
:
288 event
->desc
= ltt_event_get(event_param
->name
);
291 ret
= __tracepoint_probe_register(event_param
->name
,
292 event
->desc
->probe_callback
,
300 ret
= _ltt_event_metadata_statedump(chan
->session
, chan
, event
);
302 goto statedump_error
;
303 cds_list_add(&event
->list
, &chan
->session
->events
);
304 pthread_mutex_unlock(&sessions_mutex
);
308 WARN_ON_ONCE(__tracepoint_probe_unregister(event_param
->name
,
309 event
->desc
->probe_callback
,
311 ltt_event_put(event
->desc
);
317 pthread_mutex_unlock(&sessions_mutex
);
322 * Only used internally at session destruction.
324 int _ltt_event_unregister(struct ltt_event
*event
)
328 switch (event
->instrumentation
) {
329 case LTTNG_UST_TRACEPOINT
:
330 ret
= __tracepoint_probe_unregister(event
->desc
->name
,
331 event
->desc
->probe_callback
,
343 * Only used internally at session destruction.
346 void _ltt_event_destroy(struct ltt_event
*event
)
348 switch (event
->instrumentation
) {
349 case LTTNG_UST_TRACEPOINT
:
350 ltt_event_put(event
->desc
);
355 cds_list_del(&event
->list
);
356 lttng_destroy_context(event
->ctx
);
361 * We have exclusive access to our metadata buffer (protected by the
362 * sessions_mutex), so we can do racy operations such as looking for
363 * remaining space left in packet and write, since mutual exclusion
364 * protects us from concurrent writes.
366 int lttng_metadata_printf(struct ltt_session
*session
,
367 const char *fmt
, ...)
369 struct lib_ring_buffer_ctx ctx
;
370 struct ltt_channel
*chan
= session
->metadata
;
372 int ret
= 0, waitret
;
373 size_t len
, reserve_len
, pos
;
376 WARN_ON_ONCE(!CMM_ACCESS_ONCE(session
->active
));
379 ret
= vasprintf(&str
, fmt
, ap
);
387 for (pos
= 0; pos
< len
; pos
+= reserve_len
) {
388 reserve_len
= min_t(size_t,
389 chan
->ops
->packet_avail_size(chan
->chan
, chan
->handle
),
391 lib_ring_buffer_ctx_init(&ctx
, chan
->chan
, NULL
, reserve_len
,
392 sizeof(char), -1, chan
->handle
);
394 * We don't care about metadata buffer's records lost
395 * count, because we always retry here. Report error if
396 * we need to bail out after timeout or being
399 waitret
= wait_cond_interruptible_timeout(
401 ret
= chan
->ops
->event_reserve(&ctx
, 0);
402 ret
!= -ENOBUFS
|| !ret
;
404 LTTNG_METADATA_TIMEOUT_MSEC
);
405 if (!waitret
|| waitret
== -EINTR
|| ret
) {
406 DBG("LTTng: Failure to write metadata to buffers (%s)\n",
407 waitret
== -EINTR
? "interrupted" :
408 (ret
== -ENOBUFS
? "timeout" : "I/O error"));
409 if (waitret
== -EINTR
)
413 chan
->ops
->event_write(&ctx
, &str
[pos
], reserve_len
);
414 chan
->ops
->event_commit(&ctx
);
422 int _ltt_field_statedump(struct ltt_session
*session
,
423 const struct lttng_event_field
*field
)
427 switch (field
->type
.atype
) {
429 ret
= lttng_metadata_printf(session
,
430 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } %s;\n",
431 field
->type
.u
.basic
.integer
.size
,
432 field
->type
.u
.basic
.integer
.alignment
,
433 field
->type
.u
.basic
.integer
.signedness
,
434 (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_none
)
436 : (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
439 field
->type
.u
.basic
.integer
.base
,
441 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
443 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
448 ret
= lttng_metadata_printf(session
,
449 " floating_point { exp_dig = %u; mant_dig = %u; align = %u; } %s;\n",
450 field
->type
.u
.basic
._float
.exp_dig
,
451 field
->type
.u
.basic
._float
.mant_dig
,
452 field
->type
.u
.basic
._float
.alignment
,
454 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
456 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
461 ret
= lttng_metadata_printf(session
,
463 field
->type
.u
.basic
.enumeration
.name
,
468 const struct lttng_basic_type
*elem_type
;
470 elem_type
= &field
->type
.u
.array
.elem_type
;
471 ret
= lttng_metadata_printf(session
,
472 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } %s[%u];\n",
473 elem_type
->u
.basic
.integer
.size
,
474 elem_type
->u
.basic
.integer
.alignment
,
475 elem_type
->u
.basic
.integer
.signedness
,
476 (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
478 : (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
481 elem_type
->u
.basic
.integer
.base
,
483 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
485 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
487 field
->name
, field
->type
.u
.array
.length
);
492 const struct lttng_basic_type
*elem_type
;
493 const struct lttng_basic_type
*length_type
;
495 elem_type
= &field
->type
.u
.sequence
.elem_type
;
496 length_type
= &field
->type
.u
.sequence
.length_type
;
497 ret
= lttng_metadata_printf(session
,
498 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n",
499 length_type
->u
.basic
.integer
.size
,
500 (unsigned int) length_type
->u
.basic
.integer
.alignment
,
501 length_type
->u
.basic
.integer
.signedness
,
502 (length_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
504 : ((length_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
507 length_type
->u
.basic
.integer
.base
,
509 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
511 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
517 ret
= lttng_metadata_printf(session
,
518 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } %s[ __%s_length ];\n",
519 elem_type
->u
.basic
.integer
.size
,
520 (unsigned int) elem_type
->u
.basic
.integer
.alignment
,
521 elem_type
->u
.basic
.integer
.signedness
,
522 (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
524 : ((elem_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
527 elem_type
->u
.basic
.integer
.base
,
529 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
531 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
539 /* Default encoding is UTF8 */
540 ret
= lttng_metadata_printf(session
,
542 field
->type
.u
.basic
.string
.encoding
== lttng_encode_ASCII
?
543 " { encoding = ASCII; }" : "",
554 int _ltt_context_metadata_statedump(struct ltt_session
*session
,
555 struct lttng_ctx
*ctx
)
562 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
563 const struct lttng_ctx_field
*field
= &ctx
->fields
[i
];
565 ret
= _ltt_field_statedump(session
, &field
->event_field
);
573 int _ltt_fields_metadata_statedump(struct ltt_session
*session
,
574 struct ltt_event
*event
)
576 const struct lttng_event_desc
*desc
= event
->desc
;
580 for (i
= 0; i
< desc
->nr_fields
; i
++) {
581 const struct lttng_event_field
*field
= &desc
->fields
[i
];
583 ret
= _ltt_field_statedump(session
, field
);
591 int _ltt_event_metadata_statedump(struct ltt_session
*session
,
592 struct ltt_channel
*chan
,
593 struct ltt_event
*event
)
597 if (event
->metadata_dumped
|| !CMM_ACCESS_ONCE(session
->active
))
599 if (chan
== session
->metadata
)
602 ret
= lttng_metadata_printf(session
,
606 " stream_id = %u;\n",
614 ret
= lttng_metadata_printf(session
,
615 " context := struct {\n");
619 ret
= _ltt_context_metadata_statedump(session
, event
->ctx
);
623 ret
= lttng_metadata_printf(session
,
629 ret
= lttng_metadata_printf(session
,
630 " fields := struct {\n"
635 ret
= _ltt_fields_metadata_statedump(session
, event
);
640 * LTTng space reservation can only reserve multiples of the
643 ret
= lttng_metadata_printf(session
,
649 event
->metadata_dumped
= 1;
656 int _ltt_channel_metadata_statedump(struct ltt_session
*session
,
657 struct ltt_channel
*chan
)
661 if (chan
->metadata_dumped
|| !CMM_ACCESS_ONCE(session
->active
))
663 if (chan
== session
->metadata
)
666 WARN_ON_ONCE(!chan
->header_type
);
667 ret
= lttng_metadata_printf(session
,
670 " event.header := %s;\n"
671 " packet.context := struct packet_context;\n",
673 chan
->header_type
== 1 ? "struct event_header_compact" :
674 "struct event_header_large");
679 ret
= lttng_metadata_printf(session
,
680 " event.context := struct {\n");
684 ret
= _ltt_context_metadata_statedump(session
, chan
->ctx
);
688 ret
= lttng_metadata_printf(session
,
694 ret
= lttng_metadata_printf(session
,
697 chan
->metadata_dumped
= 1;
703 int _ltt_stream_packet_context_declare(struct ltt_session
*session
)
705 return lttng_metadata_printf(session
,
706 "struct packet_context {\n"
707 " uint64_t timestamp_begin;\n"
708 " uint64_t timestamp_end;\n"
709 " uint32_t events_discarded;\n"
710 " uint32_t content_size;\n"
711 " uint32_t packet_size;\n"
712 " uint32_t cpu_id;\n"
720 * id 31 is reserved to indicate an extended header.
723 * id: range: 0 - 65534.
724 * id 65535 is reserved to indicate an extended header.
727 int _ltt_event_header_declare(struct ltt_session
*session
)
729 return lttng_metadata_printf(session
,
730 "struct event_header_compact {\n"
731 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
734 " uint27_t timestamp;\n"
738 " uint64_t timestamp;\n"
743 "struct event_header_large {\n"
744 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
747 " uint32_t timestamp;\n"
751 " uint64_t timestamp;\n"
755 lttng_alignof(uint32_t) * CHAR_BIT
,
756 lttng_alignof(uint16_t) * CHAR_BIT
761 * Output metadata into this session's metadata buffers.
764 int _ltt_session_metadata_statedump(struct ltt_session
*session
)
766 unsigned char *uuid_c
= session
->uuid
;
768 struct ltt_channel
*chan
;
769 struct ltt_event
*event
;
772 if (!CMM_ACCESS_ONCE(session
->active
))
774 if (session
->metadata_dumped
)
776 if (!session
->metadata
) {
777 DBG("LTTng: attempt to start tracing, but metadata channel is not found. Operation abort.\n");
781 snprintf(uuid_s
, sizeof(uuid_s
),
782 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
783 uuid_c
[0], uuid_c
[1], uuid_c
[2], uuid_c
[3],
784 uuid_c
[4], uuid_c
[5], uuid_c
[6], uuid_c
[7],
785 uuid_c
[8], uuid_c
[9], uuid_c
[10], uuid_c
[11],
786 uuid_c
[12], uuid_c
[13], uuid_c
[14], uuid_c
[15]);
788 ret
= lttng_metadata_printf(session
,
789 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
790 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
791 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
792 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
793 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
794 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
800 " byte_order = %s;\n"
801 " packet.header := struct {\n"
803 " uint8_t uuid[16];\n"
804 " uint32_t stream_id;\n"
807 lttng_alignof(uint8_t) * CHAR_BIT
,
808 lttng_alignof(uint16_t) * CHAR_BIT
,
809 lttng_alignof(uint32_t) * CHAR_BIT
,
810 lttng_alignof(uint64_t) * CHAR_BIT
,
823 ret
= _ltt_stream_packet_context_declare(session
);
827 ret
= _ltt_event_header_declare(session
);
832 cds_list_for_each_entry(chan
, &session
->chan
, list
) {
833 ret
= _ltt_channel_metadata_statedump(session
, chan
);
838 cds_list_for_each_entry(event
, &session
->events
, list
) {
839 ret
= _ltt_event_metadata_statedump(session
, event
->chan
, event
);
843 session
->metadata_dumped
= 1;
849 * ltt_transport_register - LTT transport registration
850 * @transport: transport structure
852 * Registers a transport which can be used as output to extract the data out of
855 void ltt_transport_register(struct ltt_transport
*transport
)
857 pthread_mutex_lock(&sessions_mutex
);
858 cds_list_add_tail(&transport
->node
, <t_transport_list
);
859 pthread_mutex_unlock(&sessions_mutex
);
863 * ltt_transport_unregister - LTT transport unregistration
864 * @transport: transport structure
866 void ltt_transport_unregister(struct ltt_transport
*transport
)
868 pthread_mutex_lock(&sessions_mutex
);
869 cds_list_del(&transport
->node
);
870 pthread_mutex_unlock(&sessions_mutex
);
873 void ltt_events_exit(void)
875 struct ltt_session
*session
, *tmpsession
;
877 cds_list_for_each_entry_safe(session
, tmpsession
, &sessions
, list
)
878 ltt_session_destroy(session
);