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 struct ltt_event
*ltt_event_create(struct ltt_channel
*chan
,
354 struct lttng_ust_event
*event_param
,
357 struct ltt_event
*event
;
360 if (chan
->used_event_id
== -1UL)
363 * This is O(n^2) (for each event, the loop is called at event
364 * creation). Might require a hash if we have lots of events.
366 cds_list_for_each_entry(event
, &chan
->session
->events
, list
)
367 if (event
->desc
&& !strcmp(event
->desc
->name
, event_param
->name
))
369 event
= zmalloc(sizeof(struct ltt_event
));
373 event
->filter
= filter
;
375 * used_event_id counts the maximum number of event IDs that can
376 * register if all probes register.
378 chan
->used_event_id
++;
380 event
->instrumentation
= event_param
->instrumentation
;
381 /* Populate ltt_event structure before tracepoint registration. */
383 switch (event_param
->instrumentation
) {
384 case LTTNG_UST_TRACEPOINT
:
385 event
->desc
= ltt_event_get(event_param
->name
);
387 ret
= __tracepoint_probe_register(event_param
->name
,
388 event
->desc
->probe_callback
,
392 event
->id
= chan
->free_event_id
++;
395 * If the probe is not present, event->desc stays NULL,
396 * waiting for the probe to register, and the event->id
399 ret
= add_pending_probe(event
, event_param
->name
);
401 goto add_pending_error
;
408 ret
= _ltt_event_metadata_statedump(chan
->session
, chan
, event
);
410 goto statedump_error
;
412 cds_list_add(&event
->list
, &chan
->session
->events
);
417 WARN_ON_ONCE(__tracepoint_probe_unregister(event_param
->name
,
418 event
->desc
->probe_callback
,
420 ltt_event_put(event
->desc
);
432 * Only used internally at session destruction.
434 int _ltt_event_unregister(struct ltt_event
*event
)
438 switch (event
->instrumentation
) {
439 case LTTNG_UST_TRACEPOINT
:
441 ret
= __tracepoint_probe_unregister(event
->desc
->name
,
442 event
->desc
->probe_callback
,
447 remove_pending_probe(event
->pending_probe
);
458 * Only used internally at session destruction.
461 void _ltt_event_destroy(struct ltt_event
*event
)
463 switch (event
->instrumentation
) {
464 case LTTNG_UST_TRACEPOINT
:
466 ltt_event_put(event
->desc
);
472 cds_list_del(&event
->list
);
473 lttng_destroy_context(event
->ctx
);
478 * We have exclusive access to our metadata buffer (protected by the
479 * ust_lock), so we can do racy operations such as looking for
480 * remaining space left in packet and write, since mutual exclusion
481 * protects us from concurrent writes.
483 int lttng_metadata_printf(struct ltt_session
*session
,
484 const char *fmt
, ...)
486 struct lttng_ust_lib_ring_buffer_ctx ctx
;
487 struct ltt_channel
*chan
= session
->metadata
;
489 int ret
= 0, waitret
;
490 size_t len
, reserve_len
, pos
;
493 WARN_ON_ONCE(!CMM_ACCESS_ONCE(session
->active
));
496 ret
= vasprintf(&str
, fmt
, ap
);
504 for (pos
= 0; pos
< len
; pos
+= reserve_len
) {
505 reserve_len
= min_t(size_t,
506 chan
->ops
->packet_avail_size(chan
->chan
, chan
->handle
),
508 lib_ring_buffer_ctx_init(&ctx
, chan
->chan
, NULL
, reserve_len
,
509 sizeof(char), -1, chan
->handle
);
511 * We don't care about metadata buffer's records lost
512 * count, because we always retry here. Report error if
513 * we need to bail out after timeout or being
516 waitret
= wait_cond_interruptible_timeout(
518 ret
= chan
->ops
->event_reserve(&ctx
, 0);
519 ret
!= -ENOBUFS
|| !ret
;
521 LTTNG_METADATA_TIMEOUT_MSEC
);
522 if (waitret
== -ETIMEDOUT
|| waitret
== -EINTR
|| ret
) {
523 DBG("LTTng: Failure to write metadata to buffers (%s)\n",
524 waitret
== -EINTR
? "interrupted" :
525 (ret
== -ENOBUFS
? "timeout" : "I/O error"));
526 if (waitret
== -EINTR
)
530 chan
->ops
->event_write(&ctx
, &str
[pos
], reserve_len
);
531 chan
->ops
->event_commit(&ctx
);
539 int _ltt_field_statedump(struct ltt_session
*session
,
540 const struct lttng_event_field
*field
)
544 switch (field
->type
.atype
) {
546 ret
= lttng_metadata_printf(session
,
547 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n",
548 field
->type
.u
.basic
.integer
.size
,
549 field
->type
.u
.basic
.integer
.alignment
,
550 field
->type
.u
.basic
.integer
.signedness
,
551 (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_none
)
553 : (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
556 field
->type
.u
.basic
.integer
.base
,
557 #if (BYTE_ORDER == BIG_ENDIAN)
558 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
560 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
565 ret
= lttng_metadata_printf(session
,
566 " floating_point { exp_dig = %u; mant_dig = %u; align = %u;%s } _%s;\n",
567 field
->type
.u
.basic
._float
.exp_dig
,
568 field
->type
.u
.basic
._float
.mant_dig
,
569 field
->type
.u
.basic
._float
.alignment
,
570 #if (BYTE_ORDER == BIG_ENDIAN)
571 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
573 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
578 ret
= lttng_metadata_printf(session
,
580 field
->type
.u
.basic
.enumeration
.name
,
585 const struct lttng_basic_type
*elem_type
;
587 elem_type
= &field
->type
.u
.array
.elem_type
;
588 ret
= lttng_metadata_printf(session
,
589 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n",
590 elem_type
->u
.basic
.integer
.size
,
591 elem_type
->u
.basic
.integer
.alignment
,
592 elem_type
->u
.basic
.integer
.signedness
,
593 (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
595 : (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
598 elem_type
->u
.basic
.integer
.base
,
599 #if (BYTE_ORDER == BIG_ENDIAN)
600 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
602 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
604 field
->name
, field
->type
.u
.array
.length
);
609 const struct lttng_basic_type
*elem_type
;
610 const struct lttng_basic_type
*length_type
;
612 elem_type
= &field
->type
.u
.sequence
.elem_type
;
613 length_type
= &field
->type
.u
.sequence
.length_type
;
614 ret
= lttng_metadata_printf(session
,
615 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n",
616 length_type
->u
.basic
.integer
.size
,
617 (unsigned int) length_type
->u
.basic
.integer
.alignment
,
618 length_type
->u
.basic
.integer
.signedness
,
619 (length_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
621 : ((length_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
624 length_type
->u
.basic
.integer
.base
,
625 #if (BYTE_ORDER == BIG_ENDIAN)
626 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
628 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
634 ret
= lttng_metadata_printf(session
,
635 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n",
636 elem_type
->u
.basic
.integer
.size
,
637 (unsigned int) elem_type
->u
.basic
.integer
.alignment
,
638 elem_type
->u
.basic
.integer
.signedness
,
639 (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
641 : ((elem_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
644 elem_type
->u
.basic
.integer
.base
,
645 #if (BYTE_ORDER == BIG_ENDIAN)
646 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
648 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
656 /* Default encoding is UTF8 */
657 ret
= lttng_metadata_printf(session
,
659 field
->type
.u
.basic
.string
.encoding
== lttng_encode_ASCII
?
660 " { encoding = ASCII; }" : "",
671 int _ltt_context_metadata_statedump(struct ltt_session
*session
,
672 struct lttng_ctx
*ctx
)
679 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
680 const struct lttng_ctx_field
*field
= &ctx
->fields
[i
];
682 ret
= _ltt_field_statedump(session
, &field
->event_field
);
690 int _ltt_fields_metadata_statedump(struct ltt_session
*session
,
691 struct ltt_event
*event
)
693 const struct lttng_event_desc
*desc
= event
->desc
;
697 for (i
= 0; i
< desc
->nr_fields
; i
++) {
698 const struct lttng_event_field
*field
= &desc
->fields
[i
];
700 ret
= _ltt_field_statedump(session
, field
);
708 int _ltt_event_metadata_statedump(struct ltt_session
*session
,
709 struct ltt_channel
*chan
,
710 struct ltt_event
*event
)
714 if (event
->metadata_dumped
|| !CMM_ACCESS_ONCE(session
->active
))
716 if (chan
== session
->metadata
)
719 * Don't print events for which probe load is pending.
724 ret
= lttng_metadata_printf(session
,
728 " stream_id = %u;\n",
736 ret
= lttng_metadata_printf(session
,
737 " context := struct {\n");
741 ret
= _ltt_context_metadata_statedump(session
, event
->ctx
);
745 ret
= lttng_metadata_printf(session
,
751 ret
= lttng_metadata_printf(session
,
752 " fields := struct {\n"
757 ret
= _ltt_fields_metadata_statedump(session
, event
);
762 * LTTng space reservation can only reserve multiples of the
765 ret
= lttng_metadata_printf(session
,
771 event
->metadata_dumped
= 1;
778 int _ltt_channel_metadata_statedump(struct ltt_session
*session
,
779 struct ltt_channel
*chan
)
783 if (chan
->metadata_dumped
|| !CMM_ACCESS_ONCE(session
->active
))
785 if (chan
== session
->metadata
)
788 WARN_ON_ONCE(!chan
->header_type
);
789 ret
= lttng_metadata_printf(session
,
792 " event.header := %s;\n"
793 " packet.context := struct packet_context;\n",
795 chan
->header_type
== 1 ? "struct event_header_compact" :
796 "struct event_header_large");
801 ret
= lttng_metadata_printf(session
,
802 " event.context := struct {\n");
806 ret
= _ltt_context_metadata_statedump(session
, chan
->ctx
);
810 ret
= lttng_metadata_printf(session
,
816 ret
= lttng_metadata_printf(session
,
819 chan
->metadata_dumped
= 1;
825 int _ltt_stream_packet_context_declare(struct ltt_session
*session
)
827 return lttng_metadata_printf(session
,
828 "struct packet_context {\n"
829 " uint64_t timestamp_begin;\n"
830 " uint64_t timestamp_end;\n"
831 " uint32_t events_discarded;\n"
832 " uint32_t content_size;\n"
833 " uint32_t packet_size;\n"
834 " uint32_t cpu_id;\n"
842 * id 31 is reserved to indicate an extended header.
845 * id: range: 0 - 65534.
846 * id 65535 is reserved to indicate an extended header.
849 int _ltt_event_header_declare(struct ltt_session
*session
)
851 return lttng_metadata_printf(session
,
852 "struct event_header_compact {\n"
853 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
856 " uint27_t timestamp;\n"
860 " uint64_t timestamp;\n"
865 "struct event_header_large {\n"
866 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
869 " uint32_t timestamp;\n"
873 " uint64_t timestamp;\n"
877 lttng_alignof(uint32_t) * CHAR_BIT
,
878 lttng_alignof(uint16_t) * CHAR_BIT
883 * Output metadata into this session's metadata buffers.
886 int _ltt_session_metadata_statedump(struct ltt_session
*session
)
888 unsigned char *uuid_c
= session
->uuid
;
890 struct ltt_channel
*chan
;
891 struct ltt_event
*event
;
894 if (!CMM_ACCESS_ONCE(session
->active
))
896 if (session
->metadata_dumped
)
898 if (!session
->metadata
) {
899 DBG("LTTng: attempt to start tracing, but metadata channel is not found. Operation abort.\n");
903 snprintf(uuid_s
, sizeof(uuid_s
),
904 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
905 uuid_c
[0], uuid_c
[1], uuid_c
[2], uuid_c
[3],
906 uuid_c
[4], uuid_c
[5], uuid_c
[6], uuid_c
[7],
907 uuid_c
[8], uuid_c
[9], uuid_c
[10], uuid_c
[11],
908 uuid_c
[12], uuid_c
[13], uuid_c
[14], uuid_c
[15]);
910 ret
= lttng_metadata_printf(session
,
911 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
912 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
913 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
914 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
915 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
916 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
922 " byte_order = %s;\n"
923 " packet.header := struct {\n"
925 " uint8_t uuid[16];\n"
926 " uint32_t stream_id;\n"
929 lttng_alignof(uint8_t) * CHAR_BIT
,
930 lttng_alignof(uint16_t) * CHAR_BIT
,
931 lttng_alignof(uint32_t) * CHAR_BIT
,
932 lttng_alignof(uint64_t) * CHAR_BIT
,
936 #if (BYTE_ORDER == BIG_ENDIAN)
945 ret
= _ltt_stream_packet_context_declare(session
);
949 ret
= _ltt_event_header_declare(session
);
954 cds_list_for_each_entry(chan
, &session
->chan
, list
) {
955 ret
= _ltt_channel_metadata_statedump(session
, chan
);
960 cds_list_for_each_entry(event
, &session
->events
, list
) {
961 ret
= _ltt_event_metadata_statedump(session
, event
->chan
, event
);
965 session
->metadata_dumped
= 1;
970 void lttng_ust_events_exit(void)
972 struct ltt_session
*session
, *tmpsession
;
974 cds_list_for_each_entry_safe(session
, tmpsession
, &sessions
, list
)
975 ltt_session_destroy(session
);