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.
14 #include <urcu/list.h>
15 #include <urcu/hlist.h>
17 #include <uuid/uuid.h>
25 #include <urcu/compiler.h>
26 #include <urcu/uatomic.h>
27 #include <urcu/arch.h>
29 #include <lttng/tracepoint.h>
30 #include <lttng/ust-events.h>
32 #include <usterr-signal-safe.h>
35 #include "ltt-tracer.h"
36 #include "ltt-tracer-core.h"
38 #include "../libringbuffer/shm.h"
42 * The sessions mutex is the centralized mutex across UST tracing
43 * control and probe registration. All operations within this file are
44 * called by the communication thread, under ust_lock protection.
46 static pthread_mutex_t sessions_mutex
= PTHREAD_MUTEX_INITIALIZER
;
50 pthread_mutex_lock(&sessions_mutex
);
55 pthread_mutex_unlock(&sessions_mutex
);
58 static CDS_LIST_HEAD(sessions
);
61 * Pending probes hash table, containing the registered ltt events for
62 * which tracepoint probes are still missing. Protected by the sessions
65 #define PENDING_PROBE_HASH_BITS 6
66 #define PENDING_PROBE_HASH_SIZE (1 << PENDING_PROBE_HASH_BITS)
67 static struct cds_hlist_head pending_probe_table
[PENDING_PROBE_HASH_SIZE
];
69 struct ust_pending_probe
{
70 struct ltt_event
*event
;
71 struct cds_hlist_node node
;
75 static void _ltt_event_destroy(struct ltt_event
*event
);
76 static void _ltt_channel_destroy(struct ltt_channel
*chan
);
77 static int _ltt_event_unregister(struct ltt_event
*event
);
79 int _ltt_event_metadata_statedump(struct ltt_session
*session
,
80 struct ltt_channel
*chan
,
81 struct ltt_event
*event
);
83 int _ltt_session_metadata_statedump(struct ltt_session
*session
);
86 * called at event creation if probe is missing.
87 * called with session mutex held.
90 int add_pending_probe(struct ltt_event
*event
, const char *name
)
92 struct cds_hlist_head
*head
;
93 struct ust_pending_probe
*e
;
94 size_t name_len
= strlen(name
) + 1;
95 uint32_t hash
= jhash(name
, name_len
- 1, 0);
97 head
= &pending_probe_table
[hash
& (PENDING_PROBE_HASH_SIZE
- 1)];
98 e
= zmalloc(sizeof(struct ust_pending_probe
) + name_len
);
101 memcpy(&e
->name
[0], name
, name_len
);
102 cds_hlist_add_head(&e
->node
, head
);
104 event
->pending_probe
= e
;
109 * remove a pending probe. called when at event teardown and when an
110 * event is fixed (probe is loaded).
111 * called with session mutex held.
114 void remove_pending_probe(struct ust_pending_probe
*e
)
118 cds_hlist_del(&e
->node
);
123 * Called at library load: connect the probe on the events pending on
125 * called with session mutex held.
127 int pending_probe_fix_events(const struct lttng_event_desc
*desc
)
129 struct cds_hlist_head
*head
;
130 struct cds_hlist_node
*node
, *p
;
131 struct ust_pending_probe
*e
;
132 const char *name
= desc
->name
;
133 size_t name_len
= strlen(name
) + 1;
134 uint32_t hash
= jhash(name
, name_len
- 1, 0);
137 head
= &pending_probe_table
[hash
& (PENDING_PROBE_HASH_SIZE
- 1)];
138 cds_hlist_for_each_entry_safe(e
, node
, p
, head
, node
) {
139 struct ltt_event
*event
;
140 struct ltt_channel
*chan
;
142 if (strcmp(name
, e
->name
))
146 assert(!event
->desc
);
148 event
->pending_probe
= NULL
;
149 remove_pending_probe(e
);
150 ret
|= __tracepoint_probe_register(name
,
151 event
->desc
->probe_callback
,
155 event
->id
= chan
->free_event_id
++;
156 ret
|= _ltt_event_metadata_statedump(chan
->session
, chan
,
162 void synchronize_trace(void)
167 struct ltt_session
*ltt_session_create(void)
169 struct ltt_session
*session
;
171 session
= zmalloc(sizeof(struct ltt_session
));
174 CDS_INIT_LIST_HEAD(&session
->chan
);
175 CDS_INIT_LIST_HEAD(&session
->events
);
176 uuid_generate(session
->uuid
);
177 cds_list_add(&session
->list
, &sessions
);
181 void ltt_session_destroy(struct ltt_session
*session
)
183 struct ltt_channel
*chan
, *tmpchan
;
184 struct ltt_event
*event
, *tmpevent
;
187 CMM_ACCESS_ONCE(session
->active
) = 0;
188 cds_list_for_each_entry(event
, &session
->events
, list
) {
189 ret
= _ltt_event_unregister(event
);
192 synchronize_trace(); /* Wait for in-flight events to complete */
193 cds_list_for_each_entry_safe(event
, tmpevent
, &session
->events
, list
)
194 _ltt_event_destroy(event
);
195 cds_list_for_each_entry_safe(chan
, tmpchan
, &session
->chan
, list
)
196 _ltt_channel_destroy(chan
);
197 cds_list_del(&session
->list
);
201 int ltt_session_enable(struct ltt_session
*session
)
204 struct ltt_channel
*chan
;
206 if (session
->active
) {
212 * Snapshot the number of events per channel to know the type of header
215 cds_list_for_each_entry(chan
, &session
->chan
, list
) {
216 if (chan
->header_type
)
217 continue; /* don't change it if session stop/restart */
218 if (chan
->free_event_id
< 31)
219 chan
->header_type
= 1; /* compact */
221 chan
->header_type
= 2; /* large */
224 CMM_ACCESS_ONCE(session
->active
) = 1;
225 CMM_ACCESS_ONCE(session
->been_active
) = 1;
226 ret
= _ltt_session_metadata_statedump(session
);
228 CMM_ACCESS_ONCE(session
->active
) = 0;
233 int ltt_session_disable(struct ltt_session
*session
)
237 if (!session
->active
) {
241 CMM_ACCESS_ONCE(session
->active
) = 0;
246 int ltt_channel_enable(struct ltt_channel
*channel
)
250 if (channel
== channel
->session
->metadata
)
252 old
= uatomic_xchg(&channel
->enabled
, 1);
258 int ltt_channel_disable(struct ltt_channel
*channel
)
262 if (channel
== channel
->session
->metadata
)
264 old
= uatomic_xchg(&channel
->enabled
, 0);
270 int ltt_event_enable(struct ltt_event
*event
)
274 if (event
->chan
== event
->chan
->session
->metadata
)
276 old
= uatomic_xchg(&event
->enabled
, 1);
282 int ltt_event_disable(struct ltt_event
*event
)
286 if (event
->chan
== event
->chan
->session
->metadata
)
288 old
= uatomic_xchg(&event
->enabled
, 0);
294 struct ltt_channel
*ltt_channel_create(struct ltt_session
*session
,
295 const char *transport_name
,
297 size_t subbuf_size
, size_t num_subbuf
,
298 unsigned int switch_timer_interval
,
299 unsigned int read_timer_interval
,
300 int *shm_fd
, int *wait_fd
,
301 uint64_t *memory_map_size
,
302 struct ltt_channel
*chan_priv_init
)
304 struct ltt_channel
*chan
= NULL
;
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_priv_init
->id
= session
->free_chan_id
++;
316 chan_priv_init
->session
= session
;
318 * Note: the channel creation op already writes into the packet
319 * headers. Therefore the "chan" information used as input
320 * should be already accessible.
322 chan
= transport
->ops
.channel_create("[lttng]", buf_addr
,
323 subbuf_size
, num_subbuf
, switch_timer_interval
,
324 read_timer_interval
, shm_fd
, wait_fd
,
325 memory_map_size
, chan_priv_init
);
329 chan
->ops
= &transport
->ops
;
330 cds_list_add(&chan
->list
, &session
->chan
);
340 * Only used internally at session destruction.
343 void _ltt_channel_destroy(struct ltt_channel
*chan
)
345 cds_list_del(&chan
->list
);
346 lttng_destroy_context(chan
->ctx
);
347 chan
->ops
->channel_destroy(chan
);
351 * Supports event creation while tracing session is active.
353 int ltt_event_create(struct ltt_channel
*chan
,
354 struct lttng_ust_event
*event_param
,
356 struct ltt_event
**_event
)
358 struct ltt_event
*event
;
361 if (chan
->used_event_id
== -1UL) {
366 * This is O(n^2) (for each event, the loop is called at event
367 * creation). Might require a hash if we have lots of events.
369 cds_list_for_each_entry(event
, &chan
->session
->events
, list
) {
370 if (event
->desc
&& !strcmp(event
->desc
->name
, event_param
->name
)) {
375 event
= zmalloc(sizeof(struct ltt_event
));
381 event
->filter
= filter
;
383 * used_event_id counts the maximum number of event IDs that can
384 * register if all probes register.
386 chan
->used_event_id
++;
388 event
->instrumentation
= event_param
->instrumentation
;
389 /* Populate ltt_event structure before tracepoint registration. */
391 switch (event_param
->instrumentation
) {
392 case LTTNG_UST_TRACEPOINT
:
393 event
->desc
= ltt_event_get(event_param
->name
);
395 ret
= __tracepoint_probe_register(event_param
->name
,
396 event
->desc
->probe_callback
,
400 event
->id
= chan
->free_event_id
++;
403 * If the probe is not present, event->desc stays NULL,
404 * waiting for the probe to register, and the event->id
407 ret
= add_pending_probe(event
, event_param
->name
);
409 goto add_pending_error
;
412 case LTTNG_UST_TRACEPOINT_LOGLEVEL
:
414 * TODO: add tracepoint loglevel to hash table, with
415 * event info. Enable all events corresponding to
423 ret
= _ltt_event_metadata_statedump(chan
->session
, chan
, event
);
425 goto statedump_error
;
427 cds_list_add(&event
->list
, &chan
->session
->events
);
433 WARN_ON_ONCE(__tracepoint_probe_unregister(event_param
->name
,
434 event
->desc
->probe_callback
,
436 ltt_event_put(event
->desc
);
448 * Only used internally at session destruction.
450 int _ltt_event_unregister(struct ltt_event
*event
)
454 switch (event
->instrumentation
) {
455 case LTTNG_UST_TRACEPOINT
:
457 ret
= __tracepoint_probe_unregister(event
->desc
->name
,
458 event
->desc
->probe_callback
,
463 remove_pending_probe(event
->pending_probe
);
474 * Only used internally at session destruction.
477 void _ltt_event_destroy(struct ltt_event
*event
)
479 switch (event
->instrumentation
) {
480 case LTTNG_UST_TRACEPOINT
:
482 ltt_event_put(event
->desc
);
488 cds_list_del(&event
->list
);
489 lttng_destroy_context(event
->ctx
);
494 * We have exclusive access to our metadata buffer (protected by the
495 * ust_lock), so we can do racy operations such as looking for
496 * remaining space left in packet and write, since mutual exclusion
497 * protects us from concurrent writes.
499 int lttng_metadata_printf(struct ltt_session
*session
,
500 const char *fmt
, ...)
502 struct lttng_ust_lib_ring_buffer_ctx ctx
;
503 struct ltt_channel
*chan
= session
->metadata
;
505 int ret
= 0, waitret
;
506 size_t len
, reserve_len
, pos
;
509 WARN_ON_ONCE(!CMM_ACCESS_ONCE(session
->active
));
512 ret
= vasprintf(&str
, fmt
, ap
);
520 for (pos
= 0; pos
< len
; pos
+= reserve_len
) {
521 reserve_len
= min_t(size_t,
522 chan
->ops
->packet_avail_size(chan
->chan
, chan
->handle
),
524 lib_ring_buffer_ctx_init(&ctx
, chan
->chan
, NULL
, reserve_len
,
525 sizeof(char), -1, chan
->handle
);
527 * We don't care about metadata buffer's records lost
528 * count, because we always retry here. Report error if
529 * we need to bail out after timeout or being
532 waitret
= wait_cond_interruptible_timeout(
534 ret
= chan
->ops
->event_reserve(&ctx
, 0);
535 ret
!= -ENOBUFS
|| !ret
;
537 LTTNG_METADATA_TIMEOUT_MSEC
);
538 if (waitret
== -ETIMEDOUT
|| waitret
== -EINTR
|| ret
) {
539 DBG("LTTng: Failure to write metadata to buffers (%s)\n",
540 waitret
== -EINTR
? "interrupted" :
541 (ret
== -ENOBUFS
? "timeout" : "I/O error"));
542 if (waitret
== -EINTR
)
546 chan
->ops
->event_write(&ctx
, &str
[pos
], reserve_len
);
547 chan
->ops
->event_commit(&ctx
);
555 int _ltt_field_statedump(struct ltt_session
*session
,
556 const struct lttng_event_field
*field
)
560 switch (field
->type
.atype
) {
562 ret
= lttng_metadata_printf(session
,
563 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n",
564 field
->type
.u
.basic
.integer
.size
,
565 field
->type
.u
.basic
.integer
.alignment
,
566 field
->type
.u
.basic
.integer
.signedness
,
567 (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_none
)
569 : (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
572 field
->type
.u
.basic
.integer
.base
,
573 #if (BYTE_ORDER == BIG_ENDIAN)
574 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
576 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
581 ret
= lttng_metadata_printf(session
,
582 " floating_point { exp_dig = %u; mant_dig = %u; align = %u;%s } _%s;\n",
583 field
->type
.u
.basic
._float
.exp_dig
,
584 field
->type
.u
.basic
._float
.mant_dig
,
585 field
->type
.u
.basic
._float
.alignment
,
586 #if (BYTE_ORDER == BIG_ENDIAN)
587 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
589 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
594 ret
= lttng_metadata_printf(session
,
596 field
->type
.u
.basic
.enumeration
.name
,
601 const struct lttng_basic_type
*elem_type
;
603 elem_type
= &field
->type
.u
.array
.elem_type
;
604 ret
= lttng_metadata_printf(session
,
605 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n",
606 elem_type
->u
.basic
.integer
.size
,
607 elem_type
->u
.basic
.integer
.alignment
,
608 elem_type
->u
.basic
.integer
.signedness
,
609 (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
611 : (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
614 elem_type
->u
.basic
.integer
.base
,
615 #if (BYTE_ORDER == BIG_ENDIAN)
616 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
618 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
620 field
->name
, field
->type
.u
.array
.length
);
625 const struct lttng_basic_type
*elem_type
;
626 const struct lttng_basic_type
*length_type
;
628 elem_type
= &field
->type
.u
.sequence
.elem_type
;
629 length_type
= &field
->type
.u
.sequence
.length_type
;
630 ret
= lttng_metadata_printf(session
,
631 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n",
632 length_type
->u
.basic
.integer
.size
,
633 (unsigned int) length_type
->u
.basic
.integer
.alignment
,
634 length_type
->u
.basic
.integer
.signedness
,
635 (length_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
637 : ((length_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
640 length_type
->u
.basic
.integer
.base
,
641 #if (BYTE_ORDER == BIG_ENDIAN)
642 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
644 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
650 ret
= lttng_metadata_printf(session
,
651 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n",
652 elem_type
->u
.basic
.integer
.size
,
653 (unsigned int) elem_type
->u
.basic
.integer
.alignment
,
654 elem_type
->u
.basic
.integer
.signedness
,
655 (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
657 : ((elem_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
660 elem_type
->u
.basic
.integer
.base
,
661 #if (BYTE_ORDER == BIG_ENDIAN)
662 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
664 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
672 /* Default encoding is UTF8 */
673 ret
= lttng_metadata_printf(session
,
675 field
->type
.u
.basic
.string
.encoding
== lttng_encode_ASCII
?
676 " { encoding = ASCII; }" : "",
687 int _ltt_context_metadata_statedump(struct ltt_session
*session
,
688 struct lttng_ctx
*ctx
)
695 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
696 const struct lttng_ctx_field
*field
= &ctx
->fields
[i
];
698 ret
= _ltt_field_statedump(session
, &field
->event_field
);
706 int _ltt_fields_metadata_statedump(struct ltt_session
*session
,
707 struct ltt_event
*event
)
709 const struct lttng_event_desc
*desc
= event
->desc
;
713 for (i
= 0; i
< desc
->nr_fields
; i
++) {
714 const struct lttng_event_field
*field
= &desc
->fields
[i
];
716 ret
= _ltt_field_statedump(session
, field
);
724 int _ltt_event_metadata_statedump(struct ltt_session
*session
,
725 struct ltt_channel
*chan
,
726 struct ltt_event
*event
)
730 if (event
->metadata_dumped
|| !CMM_ACCESS_ONCE(session
->active
))
732 if (chan
== session
->metadata
)
735 * Don't print events for which probe load is pending.
740 ret
= lttng_metadata_printf(session
,
744 " stream_id = %u;\n",
752 ret
= lttng_metadata_printf(session
,
753 " context := struct {\n");
757 ret
= _ltt_context_metadata_statedump(session
, event
->ctx
);
761 ret
= lttng_metadata_printf(session
,
767 ret
= lttng_metadata_printf(session
,
768 " fields := struct {\n"
773 ret
= _ltt_fields_metadata_statedump(session
, event
);
778 * LTTng space reservation can only reserve multiples of the
781 ret
= lttng_metadata_printf(session
,
787 event
->metadata_dumped
= 1;
794 int _ltt_channel_metadata_statedump(struct ltt_session
*session
,
795 struct ltt_channel
*chan
)
799 if (chan
->metadata_dumped
|| !CMM_ACCESS_ONCE(session
->active
))
801 if (chan
== session
->metadata
)
804 WARN_ON_ONCE(!chan
->header_type
);
805 ret
= lttng_metadata_printf(session
,
808 " event.header := %s;\n"
809 " packet.context := struct packet_context;\n",
811 chan
->header_type
== 1 ? "struct event_header_compact" :
812 "struct event_header_large");
817 ret
= lttng_metadata_printf(session
,
818 " event.context := struct {\n");
822 ret
= _ltt_context_metadata_statedump(session
, chan
->ctx
);
826 ret
= lttng_metadata_printf(session
,
832 ret
= lttng_metadata_printf(session
,
835 chan
->metadata_dumped
= 1;
841 int _ltt_stream_packet_context_declare(struct ltt_session
*session
)
843 return lttng_metadata_printf(session
,
844 "struct packet_context {\n"
845 " uint64_t timestamp_begin;\n"
846 " uint64_t timestamp_end;\n"
847 " uint32_t events_discarded;\n"
848 " uint32_t content_size;\n"
849 " uint32_t packet_size;\n"
850 " uint32_t cpu_id;\n"
858 * id 31 is reserved to indicate an extended header.
861 * id: range: 0 - 65534.
862 * id 65535 is reserved to indicate an extended header.
865 int _ltt_event_header_declare(struct ltt_session
*session
)
867 return lttng_metadata_printf(session
,
868 "struct event_header_compact {\n"
869 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
872 " uint27_t timestamp;\n"
876 " uint64_t timestamp;\n"
881 "struct event_header_large {\n"
882 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
885 " uint32_t timestamp;\n"
889 " uint64_t timestamp;\n"
893 lttng_alignof(uint32_t) * CHAR_BIT
,
894 lttng_alignof(uint16_t) * CHAR_BIT
899 * Output metadata into this session's metadata buffers.
902 int _ltt_session_metadata_statedump(struct ltt_session
*session
)
904 unsigned char *uuid_c
= session
->uuid
;
906 struct ltt_channel
*chan
;
907 struct ltt_event
*event
;
910 if (!CMM_ACCESS_ONCE(session
->active
))
912 if (session
->metadata_dumped
)
914 if (!session
->metadata
) {
915 DBG("LTTng: attempt to start tracing, but metadata channel is not found. Operation abort.\n");
919 snprintf(uuid_s
, sizeof(uuid_s
),
920 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
921 uuid_c
[0], uuid_c
[1], uuid_c
[2], uuid_c
[3],
922 uuid_c
[4], uuid_c
[5], uuid_c
[6], uuid_c
[7],
923 uuid_c
[8], uuid_c
[9], uuid_c
[10], uuid_c
[11],
924 uuid_c
[12], uuid_c
[13], uuid_c
[14], uuid_c
[15]);
926 ret
= lttng_metadata_printf(session
,
927 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
928 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
929 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
930 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
931 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
932 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
938 " byte_order = %s;\n"
939 " packet.header := struct {\n"
941 " uint8_t uuid[16];\n"
942 " uint32_t stream_id;\n"
945 lttng_alignof(uint8_t) * CHAR_BIT
,
946 lttng_alignof(uint16_t) * CHAR_BIT
,
947 lttng_alignof(uint32_t) * CHAR_BIT
,
948 lttng_alignof(uint64_t) * CHAR_BIT
,
952 #if (BYTE_ORDER == BIG_ENDIAN)
961 ret
= _ltt_stream_packet_context_declare(session
);
965 ret
= _ltt_event_header_declare(session
);
970 cds_list_for_each_entry(chan
, &session
->chan
, list
) {
971 ret
= _ltt_channel_metadata_statedump(session
, chan
);
976 cds_list_for_each_entry(event
, &session
->events
, list
) {
977 ret
= _ltt_event_metadata_statedump(session
, event
->chan
, event
);
981 session
->metadata_dumped
= 1;
986 void lttng_ust_events_exit(void)
988 struct ltt_session
*session
, *tmpsession
;
990 cds_list_for_each_entry_safe(session
, tmpsession
, &sessions
, list
)
991 ltt_session_destroy(session
);