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>
14 #include <urcu/hlist.h>
17 #include <urcu/compiler.h>
18 #include <urcu/uatomic.h>
19 #include <uuid/uuid.h>
20 #include <ust/tracepoint.h>
24 #include <ust/lttng-events.h>
25 #include <ust/usterr-signal-safe.h>
27 #include "ltt-tracer.h"
28 #include "ltt-tracer-core.h"
30 #include "../libringbuffer/shm.h"
33 #include <ust/kcompat/jhash.h>
36 * The sessions mutex is the centralized mutex across UST tracing
37 * control and probe registration. All operations within this file are
38 * called by the communication thread, under ust_lock protection.
40 static DEFINE_MUTEX(sessions_mutex
);
44 pthread_mutex_lock(&sessions_mutex
);
49 pthread_mutex_unlock(&sessions_mutex
);
52 static CDS_LIST_HEAD(sessions
);
53 static CDS_LIST_HEAD(ltt_transport_list
);
56 * Pending probes hash table, containing the registered ltt events for
57 * which tracepoint probes are still missing. Protected by the sessions
60 #define PENDING_PROBE_HASH_BITS 6
61 #define PENDING_PROBE_HASH_SIZE (1 << PENDING_PROBE_HASH_BITS)
62 static struct cds_hlist_head pending_probe_table
[PENDING_PROBE_HASH_SIZE
];
64 struct ust_pending_probe
{
65 struct ltt_event
*event
;
66 struct cds_hlist_node node
;
70 static void _ltt_event_destroy(struct ltt_event
*event
);
71 static void _ltt_channel_destroy(struct ltt_channel
*chan
);
72 static int _ltt_event_unregister(struct ltt_event
*event
);
74 int _ltt_event_metadata_statedump(struct ltt_session
*session
,
75 struct ltt_channel
*chan
,
76 struct ltt_event
*event
);
78 int _ltt_session_metadata_statedump(struct ltt_session
*session
);
81 * called at event creation if probe is missing.
82 * called with session mutex held.
85 int add_pending_probe(struct ltt_event
*event
, const char *name
)
87 struct cds_hlist_head
*head
;
88 struct ust_pending_probe
*e
;
89 size_t name_len
= strlen(name
) + 1;
90 u32 hash
= jhash(name
, name_len
- 1, 0);
92 head
= &pending_probe_table
[hash
& (PENDING_PROBE_HASH_SIZE
- 1)];
93 e
= zmalloc(sizeof(struct ust_pending_probe
) + name_len
);
96 memcpy(&e
->name
[0], name
, name_len
);
97 cds_hlist_add_head(&e
->node
, head
);
99 event
->pending_probe
= e
;
104 * remove a pending probe. called when at event teardown and when an
105 * event is fixed (probe is loaded).
106 * called with session mutex held.
109 void remove_pending_probe(struct ust_pending_probe
*e
)
113 cds_hlist_del(&e
->node
);
118 * Called at library load: connect the probe on the events pending on
120 * called with session mutex held.
122 int pending_probe_fix_events(const struct lttng_event_desc
*desc
)
124 struct cds_hlist_head
*head
;
125 struct cds_hlist_node
*node
, *p
;
126 struct ust_pending_probe
*e
;
127 const char *name
= desc
->name
;
128 size_t name_len
= strlen(name
) + 1;
129 u32 hash
= jhash(name
, name_len
- 1, 0);
132 head
= &pending_probe_table
[hash
& (PENDING_PROBE_HASH_SIZE
- 1)];
133 cds_hlist_for_each_entry_safe(e
, node
, p
, head
, node
) {
134 struct ltt_event
*event
;
135 struct ltt_channel
*chan
;
137 if (strcmp(name
, e
->name
))
141 assert(!event
->desc
);
143 event
->pending_probe
= NULL
;
144 remove_pending_probe(e
);
145 ret
|= __tracepoint_probe_register(name
,
146 event
->desc
->probe_callback
,
148 ret
|= _ltt_event_metadata_statedump(chan
->session
, chan
,
154 void synchronize_trace(void)
159 struct ltt_session
*ltt_session_create(void)
161 struct ltt_session
*session
;
163 session
= zmalloc(sizeof(struct ltt_session
));
166 CDS_INIT_LIST_HEAD(&session
->chan
);
167 CDS_INIT_LIST_HEAD(&session
->events
);
168 uuid_generate(session
->uuid
);
169 cds_list_add(&session
->list
, &sessions
);
173 void ltt_session_destroy(struct ltt_session
*session
)
175 struct ltt_channel
*chan
, *tmpchan
;
176 struct ltt_event
*event
, *tmpevent
;
179 CMM_ACCESS_ONCE(session
->active
) = 0;
180 cds_list_for_each_entry(event
, &session
->events
, list
) {
181 ret
= _ltt_event_unregister(event
);
184 synchronize_trace(); /* Wait for in-flight events to complete */
185 cds_list_for_each_entry_safe(event
, tmpevent
, &session
->events
, list
)
186 _ltt_event_destroy(event
);
187 cds_list_for_each_entry_safe(chan
, tmpchan
, &session
->chan
, list
)
188 _ltt_channel_destroy(chan
);
189 cds_list_del(&session
->list
);
193 int ltt_session_enable(struct ltt_session
*session
)
196 struct ltt_channel
*chan
;
198 if (session
->active
) {
204 * Snapshot the number of events per channel to know the type of header
207 cds_list_for_each_entry(chan
, &session
->chan
, list
) {
208 if (chan
->header_type
)
209 continue; /* don't change it if session stop/restart */
210 if (chan
->free_event_id
< 31)
211 chan
->header_type
= 1; /* compact */
213 chan
->header_type
= 2; /* large */
216 CMM_ACCESS_ONCE(session
->active
) = 1;
217 CMM_ACCESS_ONCE(session
->been_active
) = 1;
218 ret
= _ltt_session_metadata_statedump(session
);
220 CMM_ACCESS_ONCE(session
->active
) = 0;
225 int ltt_session_disable(struct ltt_session
*session
)
229 if (!session
->active
) {
233 CMM_ACCESS_ONCE(session
->active
) = 0;
238 int ltt_channel_enable(struct ltt_channel
*channel
)
242 if (channel
== channel
->session
->metadata
)
244 old
= uatomic_xchg(&channel
->enabled
, 1);
250 int ltt_channel_disable(struct ltt_channel
*channel
)
254 if (channel
== channel
->session
->metadata
)
256 old
= uatomic_xchg(&channel
->enabled
, 0);
262 int ltt_event_enable(struct ltt_event
*event
)
266 if (event
->chan
== event
->chan
->session
->metadata
)
268 old
= uatomic_xchg(&event
->enabled
, 1);
274 int ltt_event_disable(struct ltt_event
*event
)
278 if (event
->chan
== event
->chan
->session
->metadata
)
280 old
= uatomic_xchg(&event
->enabled
, 0);
286 static struct ltt_transport
*ltt_transport_find(const char *name
)
288 struct ltt_transport
*transport
;
290 cds_list_for_each_entry(transport
, <t_transport_list
, node
) {
291 if (!strcmp(transport
->name
, name
))
297 struct ltt_channel
*ltt_channel_create(struct ltt_session
*session
,
298 const char *transport_name
,
300 size_t subbuf_size
, size_t num_subbuf
,
301 unsigned int switch_timer_interval
,
302 unsigned int read_timer_interval
)
304 struct ltt_channel
*chan
;
305 struct ltt_transport
*transport
;
307 if (session
->been_active
)
308 goto active
; /* Refuse to add channel to active session */
309 transport
= ltt_transport_find(transport_name
);
311 DBG("LTTng transport %s not found\n",
315 chan
= zmalloc(sizeof(struct ltt_channel
));
318 chan
->session
= session
;
319 chan
->id
= session
->free_chan_id
++;
321 * Note: the channel creation op already writes into the packet
322 * headers. Therefore the "chan" information used as input
323 * should be already accessible.
325 transport
->ops
.channel_create("[lttng]", chan
, buf_addr
,
326 subbuf_size
, num_subbuf
, switch_timer_interval
,
327 read_timer_interval
);
331 chan
->ops
= &transport
->ops
;
332 cds_list_add(&chan
->list
, &session
->chan
);
344 * Only used internally at session destruction.
347 void _ltt_channel_destroy(struct ltt_channel
*chan
)
349 chan
->ops
->channel_destroy(chan
);
350 cds_list_del(&chan
->list
);
351 lttng_destroy_context(chan
->ctx
);
356 * Supports event creation while tracing session is active.
358 struct ltt_event
*ltt_event_create(struct ltt_channel
*chan
,
359 struct lttng_ust_event
*event_param
,
362 struct ltt_event
*event
;
365 if (chan
->used_event_id
== -1UL)
368 * This is O(n^2) (for each event, the loop is called at event
369 * creation). Might require a hash if we have lots of events.
371 cds_list_for_each_entry(event
, &chan
->session
->events
, list
)
372 if (event
->desc
&& !strcmp(event
->desc
->name
, event_param
->name
))
374 event
= zmalloc(sizeof(struct ltt_event
));
378 event
->filter
= filter
;
380 * used_event_id counts the maximum number of event IDs that can
381 * register if all probes register.
383 chan
->used_event_id
++;
385 event
->instrumentation
= event_param
->instrumentation
;
386 /* Populate ltt_event structure before tracepoint registration. */
388 switch (event_param
->instrumentation
) {
389 case LTTNG_UST_TRACEPOINT
:
390 event
->desc
= ltt_event_get(event_param
->name
);
392 ret
= __tracepoint_probe_register(event_param
->name
,
393 event
->desc
->probe_callback
,
397 event
->id
= chan
->free_event_id
++;
400 * If the probe is not present, event->desc stays NULL,
401 * waiting for the probe to register, and the event->id
404 ret
= add_pending_probe(event
, event_param
->name
);
406 goto add_pending_error
;
413 ret
= _ltt_event_metadata_statedump(chan
->session
, chan
, event
);
415 goto statedump_error
;
417 cds_list_add(&event
->list
, &chan
->session
->events
);
422 WARN_ON_ONCE(__tracepoint_probe_unregister(event_param
->name
,
423 event
->desc
->probe_callback
,
425 ltt_event_put(event
->desc
);
437 * Only used internally at session destruction.
439 int _ltt_event_unregister(struct ltt_event
*event
)
443 switch (event
->instrumentation
) {
444 case LTTNG_UST_TRACEPOINT
:
446 ret
= __tracepoint_probe_unregister(event
->desc
->name
,
447 event
->desc
->probe_callback
,
452 remove_pending_probe(event
->pending_probe
);
463 * Only used internally at session destruction.
466 void _ltt_event_destroy(struct ltt_event
*event
)
468 switch (event
->instrumentation
) {
469 case LTTNG_UST_TRACEPOINT
:
471 ltt_event_put(event
->desc
);
477 cds_list_del(&event
->list
);
478 lttng_destroy_context(event
->ctx
);
483 * We have exclusive access to our metadata buffer (protected by the
484 * ust_lock), so we can do racy operations such as looking for
485 * remaining space left in packet and write, since mutual exclusion
486 * protects us from concurrent writes.
488 int lttng_metadata_printf(struct ltt_session
*session
,
489 const char *fmt
, ...)
491 struct lib_ring_buffer_ctx ctx
;
492 struct ltt_channel
*chan
= session
->metadata
;
494 int ret
= 0, waitret
;
495 size_t len
, reserve_len
, pos
;
498 WARN_ON_ONCE(!CMM_ACCESS_ONCE(session
->active
));
501 ret
= vasprintf(&str
, fmt
, ap
);
509 for (pos
= 0; pos
< len
; pos
+= reserve_len
) {
510 reserve_len
= min_t(size_t,
511 chan
->ops
->packet_avail_size(chan
->chan
, chan
->handle
),
513 lib_ring_buffer_ctx_init(&ctx
, chan
->chan
, NULL
, reserve_len
,
514 sizeof(char), -1, chan
->handle
);
516 * We don't care about metadata buffer's records lost
517 * count, because we always retry here. Report error if
518 * we need to bail out after timeout or being
521 waitret
= wait_cond_interruptible_timeout(
523 ret
= chan
->ops
->event_reserve(&ctx
, 0);
524 ret
!= -ENOBUFS
|| !ret
;
526 LTTNG_METADATA_TIMEOUT_MSEC
);
527 if (waitret
== -ETIMEDOUT
|| waitret
== -EINTR
|| ret
) {
528 DBG("LTTng: Failure to write metadata to buffers (%s)\n",
529 waitret
== -EINTR
? "interrupted" :
530 (ret
== -ENOBUFS
? "timeout" : "I/O error"));
531 if (waitret
== -EINTR
)
535 chan
->ops
->event_write(&ctx
, &str
[pos
], reserve_len
);
536 chan
->ops
->event_commit(&ctx
);
544 int _ltt_field_statedump(struct ltt_session
*session
,
545 const struct lttng_event_field
*field
)
549 switch (field
->type
.atype
) {
551 ret
= lttng_metadata_printf(session
,
552 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n",
553 field
->type
.u
.basic
.integer
.size
,
554 field
->type
.u
.basic
.integer
.alignment
,
555 field
->type
.u
.basic
.integer
.signedness
,
556 (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_none
)
558 : (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
561 field
->type
.u
.basic
.integer
.base
,
563 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
565 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
570 ret
= lttng_metadata_printf(session
,
571 " floating_point { exp_dig = %u; mant_dig = %u; align = %u; } _%s;\n",
572 field
->type
.u
.basic
._float
.exp_dig
,
573 field
->type
.u
.basic
._float
.mant_dig
,
574 field
->type
.u
.basic
._float
.alignment
,
576 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
578 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
583 ret
= lttng_metadata_printf(session
,
585 field
->type
.u
.basic
.enumeration
.name
,
590 const struct lttng_basic_type
*elem_type
;
592 elem_type
= &field
->type
.u
.array
.elem_type
;
593 ret
= lttng_metadata_printf(session
,
594 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n",
595 elem_type
->u
.basic
.integer
.size
,
596 elem_type
->u
.basic
.integer
.alignment
,
597 elem_type
->u
.basic
.integer
.signedness
,
598 (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
600 : (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
603 elem_type
->u
.basic
.integer
.base
,
605 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
607 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
609 field
->name
, field
->type
.u
.array
.length
);
614 const struct lttng_basic_type
*elem_type
;
615 const struct lttng_basic_type
*length_type
;
617 elem_type
= &field
->type
.u
.sequence
.elem_type
;
618 length_type
= &field
->type
.u
.sequence
.length_type
;
619 ret
= lttng_metadata_printf(session
,
620 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n",
621 length_type
->u
.basic
.integer
.size
,
622 (unsigned int) length_type
->u
.basic
.integer
.alignment
,
623 length_type
->u
.basic
.integer
.signedness
,
624 (length_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
626 : ((length_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
629 length_type
->u
.basic
.integer
.base
,
631 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
633 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
639 ret
= lttng_metadata_printf(session
,
640 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n",
641 elem_type
->u
.basic
.integer
.size
,
642 (unsigned int) elem_type
->u
.basic
.integer
.alignment
,
643 elem_type
->u
.basic
.integer
.signedness
,
644 (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
646 : ((elem_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
649 elem_type
->u
.basic
.integer
.base
,
651 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
653 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
661 /* Default encoding is UTF8 */
662 ret
= lttng_metadata_printf(session
,
664 field
->type
.u
.basic
.string
.encoding
== lttng_encode_ASCII
?
665 " { encoding = ASCII; }" : "",
676 int _ltt_context_metadata_statedump(struct ltt_session
*session
,
677 struct lttng_ctx
*ctx
)
684 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
685 const struct lttng_ctx_field
*field
= &ctx
->fields
[i
];
687 ret
= _ltt_field_statedump(session
, &field
->event_field
);
695 int _ltt_fields_metadata_statedump(struct ltt_session
*session
,
696 struct ltt_event
*event
)
698 const struct lttng_event_desc
*desc
= event
->desc
;
702 for (i
= 0; i
< desc
->nr_fields
; i
++) {
703 const struct lttng_event_field
*field
= &desc
->fields
[i
];
705 ret
= _ltt_field_statedump(session
, field
);
713 int _ltt_event_metadata_statedump(struct ltt_session
*session
,
714 struct ltt_channel
*chan
,
715 struct ltt_event
*event
)
719 if (event
->metadata_dumped
|| !CMM_ACCESS_ONCE(session
->active
))
721 if (chan
== session
->metadata
)
724 * Don't print events for which probe load is pending.
729 ret
= lttng_metadata_printf(session
,
733 " stream_id = %u;\n",
741 ret
= lttng_metadata_printf(session
,
742 " context := struct {\n");
746 ret
= _ltt_context_metadata_statedump(session
, event
->ctx
);
750 ret
= lttng_metadata_printf(session
,
756 ret
= lttng_metadata_printf(session
,
757 " fields := struct {\n"
762 ret
= _ltt_fields_metadata_statedump(session
, event
);
767 * LTTng space reservation can only reserve multiples of the
770 ret
= lttng_metadata_printf(session
,
776 event
->metadata_dumped
= 1;
783 int _ltt_channel_metadata_statedump(struct ltt_session
*session
,
784 struct ltt_channel
*chan
)
788 if (chan
->metadata_dumped
|| !CMM_ACCESS_ONCE(session
->active
))
790 if (chan
== session
->metadata
)
793 WARN_ON_ONCE(!chan
->header_type
);
794 ret
= lttng_metadata_printf(session
,
797 " event.header := %s;\n"
798 " packet.context := struct packet_context;\n",
800 chan
->header_type
== 1 ? "struct event_header_compact" :
801 "struct event_header_large");
806 ret
= lttng_metadata_printf(session
,
807 " event.context := struct {\n");
811 ret
= _ltt_context_metadata_statedump(session
, chan
->ctx
);
815 ret
= lttng_metadata_printf(session
,
821 ret
= lttng_metadata_printf(session
,
824 chan
->metadata_dumped
= 1;
830 int _ltt_stream_packet_context_declare(struct ltt_session
*session
)
832 return lttng_metadata_printf(session
,
833 "struct packet_context {\n"
834 " uint64_t timestamp_begin;\n"
835 " uint64_t timestamp_end;\n"
836 " uint32_t events_discarded;\n"
837 " uint32_t content_size;\n"
838 " uint32_t packet_size;\n"
839 " uint32_t cpu_id;\n"
847 * id 31 is reserved to indicate an extended header.
850 * id: range: 0 - 65534.
851 * id 65535 is reserved to indicate an extended header.
854 int _ltt_event_header_declare(struct ltt_session
*session
)
856 return lttng_metadata_printf(session
,
857 "struct event_header_compact {\n"
858 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
861 " uint27_t timestamp;\n"
865 " uint64_t timestamp;\n"
870 "struct event_header_large {\n"
871 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
874 " uint32_t timestamp;\n"
878 " uint64_t timestamp;\n"
882 lttng_alignof(uint32_t) * CHAR_BIT
,
883 lttng_alignof(uint16_t) * CHAR_BIT
888 * Output metadata into this session's metadata buffers.
891 int _ltt_session_metadata_statedump(struct ltt_session
*session
)
893 unsigned char *uuid_c
= session
->uuid
;
895 struct ltt_channel
*chan
;
896 struct ltt_event
*event
;
899 if (!CMM_ACCESS_ONCE(session
->active
))
901 if (session
->metadata_dumped
)
903 if (!session
->metadata
) {
904 DBG("LTTng: attempt to start tracing, but metadata channel is not found. Operation abort.\n");
908 snprintf(uuid_s
, sizeof(uuid_s
),
909 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
910 uuid_c
[0], uuid_c
[1], uuid_c
[2], uuid_c
[3],
911 uuid_c
[4], uuid_c
[5], uuid_c
[6], uuid_c
[7],
912 uuid_c
[8], uuid_c
[9], uuid_c
[10], uuid_c
[11],
913 uuid_c
[12], uuid_c
[13], uuid_c
[14], uuid_c
[15]);
915 ret
= lttng_metadata_printf(session
,
916 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
917 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
918 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
919 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
920 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
921 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
927 " byte_order = %s;\n"
928 " packet.header := struct {\n"
930 " uint8_t uuid[16];\n"
931 " uint32_t stream_id;\n"
934 lttng_alignof(uint8_t) * CHAR_BIT
,
935 lttng_alignof(uint16_t) * CHAR_BIT
,
936 lttng_alignof(uint32_t) * CHAR_BIT
,
937 lttng_alignof(uint64_t) * CHAR_BIT
,
950 ret
= _ltt_stream_packet_context_declare(session
);
954 ret
= _ltt_event_header_declare(session
);
959 cds_list_for_each_entry(chan
, &session
->chan
, list
) {
960 ret
= _ltt_channel_metadata_statedump(session
, chan
);
965 cds_list_for_each_entry(event
, &session
->events
, list
) {
966 ret
= _ltt_event_metadata_statedump(session
, event
->chan
, event
);
970 session
->metadata_dumped
= 1;
976 * ltt_transport_register - LTT transport registration
977 * @transport: transport structure
979 * Registers a transport which can be used as output to extract the data out of
980 * LTTng. Called with ust_lock held.
982 void ltt_transport_register(struct ltt_transport
*transport
)
984 cds_list_add_tail(&transport
->node
, <t_transport_list
);
988 * ltt_transport_unregister - LTT transport unregistration
989 * @transport: transport structure
990 * Called with ust_lock held.
992 void ltt_transport_unregister(struct ltt_transport
*transport
)
994 cds_list_del(&transport
->node
);
997 void ltt_events_exit(void)
999 struct ltt_session
*session
, *tmpsession
;
1001 cds_list_for_each_entry_safe(session
, tmpsession
, &sessions
, list
)
1002 ltt_session_destroy(session
);