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>
18 #include <urcu/compiler.h>
19 #include <urcu/uatomic.h>
20 #include <uuid/uuid.h>
21 #include <lttng/tracepoint.h>
25 #include <lttng/ust-events.h>
26 #include <lttng/usterr-signal-safe.h>
27 #include "lttng/core.h"
28 #include "ltt-tracer.h"
29 #include "ltt-tracer-core.h"
31 #include "../libringbuffer/shm.h"
34 #include <urcu/arch.h>
38 * The sessions mutex is the centralized mutex across UST tracing
39 * control and probe registration. All operations within this file are
40 * called by the communication thread, under ust_lock protection.
42 static DEFINE_MUTEX(sessions_mutex
);
46 pthread_mutex_lock(&sessions_mutex
);
51 pthread_mutex_unlock(&sessions_mutex
);
54 static CDS_LIST_HEAD(sessions
);
57 * Pending probes hash table, containing the registered ltt events for
58 * which tracepoint probes are still missing. Protected by the sessions
61 #define PENDING_PROBE_HASH_BITS 6
62 #define PENDING_PROBE_HASH_SIZE (1 << PENDING_PROBE_HASH_BITS)
63 static struct cds_hlist_head pending_probe_table
[PENDING_PROBE_HASH_SIZE
];
65 struct ust_pending_probe
{
66 struct ltt_event
*event
;
67 struct cds_hlist_node node
;
71 static void _ltt_event_destroy(struct ltt_event
*event
);
72 static void _ltt_channel_destroy(struct ltt_channel
*chan
);
73 static int _ltt_event_unregister(struct ltt_event
*event
);
75 int _ltt_event_metadata_statedump(struct ltt_session
*session
,
76 struct ltt_channel
*chan
,
77 struct ltt_event
*event
);
79 int _ltt_session_metadata_statedump(struct ltt_session
*session
);
82 * called at event creation if probe is missing.
83 * called with session mutex held.
86 int add_pending_probe(struct ltt_event
*event
, const char *name
)
88 struct cds_hlist_head
*head
;
89 struct ust_pending_probe
*e
;
90 size_t name_len
= strlen(name
) + 1;
91 uint32_t hash
= jhash(name
, name_len
- 1, 0);
93 head
= &pending_probe_table
[hash
& (PENDING_PROBE_HASH_SIZE
- 1)];
94 e
= zmalloc(sizeof(struct ust_pending_probe
) + name_len
);
97 memcpy(&e
->name
[0], name
, name_len
);
98 cds_hlist_add_head(&e
->node
, head
);
100 event
->pending_probe
= e
;
105 * remove a pending probe. called when at event teardown and when an
106 * event is fixed (probe is loaded).
107 * called with session mutex held.
110 void remove_pending_probe(struct ust_pending_probe
*e
)
114 cds_hlist_del(&e
->node
);
119 * Called at library load: connect the probe on the events pending on
121 * called with session mutex held.
123 int pending_probe_fix_events(const struct lttng_event_desc
*desc
)
125 struct cds_hlist_head
*head
;
126 struct cds_hlist_node
*node
, *p
;
127 struct ust_pending_probe
*e
;
128 const char *name
= desc
->name
;
129 size_t name_len
= strlen(name
) + 1;
130 uint32_t hash
= jhash(name
, name_len
- 1, 0);
133 head
= &pending_probe_table
[hash
& (PENDING_PROBE_HASH_SIZE
- 1)];
134 cds_hlist_for_each_entry_safe(e
, node
, p
, head
, node
) {
135 struct ltt_event
*event
;
136 struct ltt_channel
*chan
;
138 if (strcmp(name
, e
->name
))
142 assert(!event
->desc
);
144 event
->pending_probe
= NULL
;
145 remove_pending_probe(e
);
146 ret
|= __tracepoint_probe_register(name
,
147 event
->desc
->probe_callback
,
151 event
->id
= chan
->free_event_id
++;
152 ret
|= _ltt_event_metadata_statedump(chan
->session
, chan
,
158 void synchronize_trace(void)
163 struct ltt_session
*ltt_session_create(void)
165 struct ltt_session
*session
;
167 session
= zmalloc(sizeof(struct ltt_session
));
170 CDS_INIT_LIST_HEAD(&session
->chan
);
171 CDS_INIT_LIST_HEAD(&session
->events
);
172 uuid_generate(session
->uuid
);
173 cds_list_add(&session
->list
, &sessions
);
177 void ltt_session_destroy(struct ltt_session
*session
)
179 struct ltt_channel
*chan
, *tmpchan
;
180 struct ltt_event
*event
, *tmpevent
;
183 CMM_ACCESS_ONCE(session
->active
) = 0;
184 cds_list_for_each_entry(event
, &session
->events
, list
) {
185 ret
= _ltt_event_unregister(event
);
188 synchronize_trace(); /* Wait for in-flight events to complete */
189 cds_list_for_each_entry_safe(event
, tmpevent
, &session
->events
, list
)
190 _ltt_event_destroy(event
);
191 cds_list_for_each_entry_safe(chan
, tmpchan
, &session
->chan
, list
)
192 _ltt_channel_destroy(chan
);
193 cds_list_del(&session
->list
);
197 int ltt_session_enable(struct ltt_session
*session
)
200 struct ltt_channel
*chan
;
202 if (session
->active
) {
208 * Snapshot the number of events per channel to know the type of header
211 cds_list_for_each_entry(chan
, &session
->chan
, list
) {
212 if (chan
->header_type
)
213 continue; /* don't change it if session stop/restart */
214 if (chan
->free_event_id
< 31)
215 chan
->header_type
= 1; /* compact */
217 chan
->header_type
= 2; /* large */
220 CMM_ACCESS_ONCE(session
->active
) = 1;
221 CMM_ACCESS_ONCE(session
->been_active
) = 1;
222 ret
= _ltt_session_metadata_statedump(session
);
224 CMM_ACCESS_ONCE(session
->active
) = 0;
229 int ltt_session_disable(struct ltt_session
*session
)
233 if (!session
->active
) {
237 CMM_ACCESS_ONCE(session
->active
) = 0;
242 int ltt_channel_enable(struct ltt_channel
*channel
)
246 if (channel
== channel
->session
->metadata
)
248 old
= uatomic_xchg(&channel
->enabled
, 1);
254 int ltt_channel_disable(struct ltt_channel
*channel
)
258 if (channel
== channel
->session
->metadata
)
260 old
= uatomic_xchg(&channel
->enabled
, 0);
266 int ltt_event_enable(struct ltt_event
*event
)
270 if (event
->chan
== event
->chan
->session
->metadata
)
272 old
= uatomic_xchg(&event
->enabled
, 1);
278 int ltt_event_disable(struct ltt_event
*event
)
282 if (event
->chan
== event
->chan
->session
->metadata
)
284 old
= uatomic_xchg(&event
->enabled
, 0);
290 struct ltt_channel
*ltt_channel_create(struct ltt_session
*session
,
291 const char *transport_name
,
293 size_t subbuf_size
, size_t num_subbuf
,
294 unsigned int switch_timer_interval
,
295 unsigned int read_timer_interval
,
296 int *shm_fd
, int *wait_fd
,
297 uint64_t *memory_map_size
,
298 struct ltt_channel
*chan_priv_init
)
300 struct ltt_channel
*chan
= NULL
;
301 struct ltt_transport
*transport
;
303 if (session
->been_active
)
304 goto active
; /* Refuse to add channel to active session */
305 transport
= ltt_transport_find(transport_name
);
307 DBG("LTTng transport %s not found\n",
311 chan_priv_init
->id
= session
->free_chan_id
++;
312 chan_priv_init
->session
= session
;
314 * Note: the channel creation op already writes into the packet
315 * headers. Therefore the "chan" information used as input
316 * should be already accessible.
318 chan
= transport
->ops
.channel_create("[lttng]", buf_addr
,
319 subbuf_size
, num_subbuf
, switch_timer_interval
,
320 read_timer_interval
, shm_fd
, wait_fd
,
321 memory_map_size
, chan_priv_init
);
325 chan
->ops
= &transport
->ops
;
326 cds_list_add(&chan
->list
, &session
->chan
);
336 * Only used internally at session destruction.
339 void _ltt_channel_destroy(struct ltt_channel
*chan
)
341 cds_list_del(&chan
->list
);
342 lttng_destroy_context(chan
->ctx
);
343 chan
->ops
->channel_destroy(chan
);
347 * Supports event creation while tracing session is active.
349 struct ltt_event
*ltt_event_create(struct ltt_channel
*chan
,
350 struct lttng_ust_event
*event_param
,
353 struct ltt_event
*event
;
356 if (chan
->used_event_id
== -1UL)
359 * This is O(n^2) (for each event, the loop is called at event
360 * creation). Might require a hash if we have lots of events.
362 cds_list_for_each_entry(event
, &chan
->session
->events
, list
)
363 if (event
->desc
&& !strcmp(event
->desc
->name
, event_param
->name
))
365 event
= zmalloc(sizeof(struct ltt_event
));
369 event
->filter
= filter
;
371 * used_event_id counts the maximum number of event IDs that can
372 * register if all probes register.
374 chan
->used_event_id
++;
376 event
->instrumentation
= event_param
->instrumentation
;
377 /* Populate ltt_event structure before tracepoint registration. */
379 switch (event_param
->instrumentation
) {
380 case LTTNG_UST_TRACEPOINT
:
381 event
->desc
= ltt_event_get(event_param
->name
);
383 ret
= __tracepoint_probe_register(event_param
->name
,
384 event
->desc
->probe_callback
,
388 event
->id
= chan
->free_event_id
++;
391 * If the probe is not present, event->desc stays NULL,
392 * waiting for the probe to register, and the event->id
395 ret
= add_pending_probe(event
, event_param
->name
);
397 goto add_pending_error
;
404 ret
= _ltt_event_metadata_statedump(chan
->session
, chan
, event
);
406 goto statedump_error
;
408 cds_list_add(&event
->list
, &chan
->session
->events
);
413 WARN_ON_ONCE(__tracepoint_probe_unregister(event_param
->name
,
414 event
->desc
->probe_callback
,
416 ltt_event_put(event
->desc
);
428 * Only used internally at session destruction.
430 int _ltt_event_unregister(struct ltt_event
*event
)
434 switch (event
->instrumentation
) {
435 case LTTNG_UST_TRACEPOINT
:
437 ret
= __tracepoint_probe_unregister(event
->desc
->name
,
438 event
->desc
->probe_callback
,
443 remove_pending_probe(event
->pending_probe
);
454 * Only used internally at session destruction.
457 void _ltt_event_destroy(struct ltt_event
*event
)
459 switch (event
->instrumentation
) {
460 case LTTNG_UST_TRACEPOINT
:
462 ltt_event_put(event
->desc
);
468 cds_list_del(&event
->list
);
469 lttng_destroy_context(event
->ctx
);
474 * We have exclusive access to our metadata buffer (protected by the
475 * ust_lock), so we can do racy operations such as looking for
476 * remaining space left in packet and write, since mutual exclusion
477 * protects us from concurrent writes.
479 int lttng_metadata_printf(struct ltt_session
*session
,
480 const char *fmt
, ...)
482 struct lttng_ust_lib_ring_buffer_ctx ctx
;
483 struct ltt_channel
*chan
= session
->metadata
;
485 int ret
= 0, waitret
;
486 size_t len
, reserve_len
, pos
;
489 WARN_ON_ONCE(!CMM_ACCESS_ONCE(session
->active
));
492 ret
= vasprintf(&str
, fmt
, ap
);
500 for (pos
= 0; pos
< len
; pos
+= reserve_len
) {
501 reserve_len
= min_t(size_t,
502 chan
->ops
->packet_avail_size(chan
->chan
, chan
->handle
),
504 lib_ring_buffer_ctx_init(&ctx
, chan
->chan
, NULL
, reserve_len
,
505 sizeof(char), -1, chan
->handle
);
507 * We don't care about metadata buffer's records lost
508 * count, because we always retry here. Report error if
509 * we need to bail out after timeout or being
512 waitret
= wait_cond_interruptible_timeout(
514 ret
= chan
->ops
->event_reserve(&ctx
, 0);
515 ret
!= -ENOBUFS
|| !ret
;
517 LTTNG_METADATA_TIMEOUT_MSEC
);
518 if (waitret
== -ETIMEDOUT
|| waitret
== -EINTR
|| ret
) {
519 DBG("LTTng: Failure to write metadata to buffers (%s)\n",
520 waitret
== -EINTR
? "interrupted" :
521 (ret
== -ENOBUFS
? "timeout" : "I/O error"));
522 if (waitret
== -EINTR
)
526 chan
->ops
->event_write(&ctx
, &str
[pos
], reserve_len
);
527 chan
->ops
->event_commit(&ctx
);
535 int _ltt_field_statedump(struct ltt_session
*session
,
536 const struct lttng_event_field
*field
)
540 switch (field
->type
.atype
) {
542 ret
= lttng_metadata_printf(session
,
543 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n",
544 field
->type
.u
.basic
.integer
.size
,
545 field
->type
.u
.basic
.integer
.alignment
,
546 field
->type
.u
.basic
.integer
.signedness
,
547 (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_none
)
549 : (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
552 field
->type
.u
.basic
.integer
.base
,
553 #if (BYTE_ORDER == BIG_ENDIAN)
554 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
556 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
561 ret
= lttng_metadata_printf(session
,
562 " floating_point { exp_dig = %u; mant_dig = %u; align = %u;%s } _%s;\n",
563 field
->type
.u
.basic
._float
.exp_dig
,
564 field
->type
.u
.basic
._float
.mant_dig
,
565 field
->type
.u
.basic
._float
.alignment
,
566 #if (BYTE_ORDER == BIG_ENDIAN)
567 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
569 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
574 ret
= lttng_metadata_printf(session
,
576 field
->type
.u
.basic
.enumeration
.name
,
581 const struct lttng_basic_type
*elem_type
;
583 elem_type
= &field
->type
.u
.array
.elem_type
;
584 ret
= lttng_metadata_printf(session
,
585 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n",
586 elem_type
->u
.basic
.integer
.size
,
587 elem_type
->u
.basic
.integer
.alignment
,
588 elem_type
->u
.basic
.integer
.signedness
,
589 (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
591 : (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
594 elem_type
->u
.basic
.integer
.base
,
595 #if (BYTE_ORDER == BIG_ENDIAN)
596 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
598 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
600 field
->name
, field
->type
.u
.array
.length
);
605 const struct lttng_basic_type
*elem_type
;
606 const struct lttng_basic_type
*length_type
;
608 elem_type
= &field
->type
.u
.sequence
.elem_type
;
609 length_type
= &field
->type
.u
.sequence
.length_type
;
610 ret
= lttng_metadata_printf(session
,
611 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n",
612 length_type
->u
.basic
.integer
.size
,
613 (unsigned int) length_type
->u
.basic
.integer
.alignment
,
614 length_type
->u
.basic
.integer
.signedness
,
615 (length_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
617 : ((length_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
620 length_type
->u
.basic
.integer
.base
,
621 #if (BYTE_ORDER == BIG_ENDIAN)
622 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
624 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
630 ret
= lttng_metadata_printf(session
,
631 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n",
632 elem_type
->u
.basic
.integer
.size
,
633 (unsigned int) elem_type
->u
.basic
.integer
.alignment
,
634 elem_type
->u
.basic
.integer
.signedness
,
635 (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
637 : ((elem_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
640 elem_type
->u
.basic
.integer
.base
,
641 #if (BYTE_ORDER == BIG_ENDIAN)
642 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
644 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
652 /* Default encoding is UTF8 */
653 ret
= lttng_metadata_printf(session
,
655 field
->type
.u
.basic
.string
.encoding
== lttng_encode_ASCII
?
656 " { encoding = ASCII; }" : "",
667 int _ltt_context_metadata_statedump(struct ltt_session
*session
,
668 struct lttng_ctx
*ctx
)
675 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
676 const struct lttng_ctx_field
*field
= &ctx
->fields
[i
];
678 ret
= _ltt_field_statedump(session
, &field
->event_field
);
686 int _ltt_fields_metadata_statedump(struct ltt_session
*session
,
687 struct ltt_event
*event
)
689 const struct lttng_event_desc
*desc
= event
->desc
;
693 for (i
= 0; i
< desc
->nr_fields
; i
++) {
694 const struct lttng_event_field
*field
= &desc
->fields
[i
];
696 ret
= _ltt_field_statedump(session
, field
);
704 int _ltt_event_metadata_statedump(struct ltt_session
*session
,
705 struct ltt_channel
*chan
,
706 struct ltt_event
*event
)
710 if (event
->metadata_dumped
|| !CMM_ACCESS_ONCE(session
->active
))
712 if (chan
== session
->metadata
)
715 * Don't print events for which probe load is pending.
720 ret
= lttng_metadata_printf(session
,
724 " stream_id = %u;\n",
732 ret
= lttng_metadata_printf(session
,
733 " context := struct {\n");
737 ret
= _ltt_context_metadata_statedump(session
, event
->ctx
);
741 ret
= lttng_metadata_printf(session
,
747 ret
= lttng_metadata_printf(session
,
748 " fields := struct {\n"
753 ret
= _ltt_fields_metadata_statedump(session
, event
);
758 * LTTng space reservation can only reserve multiples of the
761 ret
= lttng_metadata_printf(session
,
767 event
->metadata_dumped
= 1;
774 int _ltt_channel_metadata_statedump(struct ltt_session
*session
,
775 struct ltt_channel
*chan
)
779 if (chan
->metadata_dumped
|| !CMM_ACCESS_ONCE(session
->active
))
781 if (chan
== session
->metadata
)
784 WARN_ON_ONCE(!chan
->header_type
);
785 ret
= lttng_metadata_printf(session
,
788 " event.header := %s;\n"
789 " packet.context := struct packet_context;\n",
791 chan
->header_type
== 1 ? "struct event_header_compact" :
792 "struct event_header_large");
797 ret
= lttng_metadata_printf(session
,
798 " event.context := struct {\n");
802 ret
= _ltt_context_metadata_statedump(session
, chan
->ctx
);
806 ret
= lttng_metadata_printf(session
,
812 ret
= lttng_metadata_printf(session
,
815 chan
->metadata_dumped
= 1;
821 int _ltt_stream_packet_context_declare(struct ltt_session
*session
)
823 return lttng_metadata_printf(session
,
824 "struct packet_context {\n"
825 " uint64_t timestamp_begin;\n"
826 " uint64_t timestamp_end;\n"
827 " uint32_t events_discarded;\n"
828 " uint32_t content_size;\n"
829 " uint32_t packet_size;\n"
830 " uint32_t cpu_id;\n"
838 * id 31 is reserved to indicate an extended header.
841 * id: range: 0 - 65534.
842 * id 65535 is reserved to indicate an extended header.
845 int _ltt_event_header_declare(struct ltt_session
*session
)
847 return lttng_metadata_printf(session
,
848 "struct event_header_compact {\n"
849 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
852 " uint27_t timestamp;\n"
856 " uint64_t timestamp;\n"
861 "struct event_header_large {\n"
862 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
865 " uint32_t timestamp;\n"
869 " uint64_t timestamp;\n"
873 lttng_alignof(uint32_t) * CHAR_BIT
,
874 lttng_alignof(uint16_t) * CHAR_BIT
879 * Output metadata into this session's metadata buffers.
882 int _ltt_session_metadata_statedump(struct ltt_session
*session
)
884 unsigned char *uuid_c
= session
->uuid
;
886 struct ltt_channel
*chan
;
887 struct ltt_event
*event
;
890 if (!CMM_ACCESS_ONCE(session
->active
))
892 if (session
->metadata_dumped
)
894 if (!session
->metadata
) {
895 DBG("LTTng: attempt to start tracing, but metadata channel is not found. Operation abort.\n");
899 snprintf(uuid_s
, sizeof(uuid_s
),
900 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
901 uuid_c
[0], uuid_c
[1], uuid_c
[2], uuid_c
[3],
902 uuid_c
[4], uuid_c
[5], uuid_c
[6], uuid_c
[7],
903 uuid_c
[8], uuid_c
[9], uuid_c
[10], uuid_c
[11],
904 uuid_c
[12], uuid_c
[13], uuid_c
[14], uuid_c
[15]);
906 ret
= lttng_metadata_printf(session
,
907 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
908 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
909 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
910 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
911 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
912 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
918 " byte_order = %s;\n"
919 " packet.header := struct {\n"
921 " uint8_t uuid[16];\n"
922 " uint32_t stream_id;\n"
925 lttng_alignof(uint8_t) * CHAR_BIT
,
926 lttng_alignof(uint16_t) * CHAR_BIT
,
927 lttng_alignof(uint32_t) * CHAR_BIT
,
928 lttng_alignof(uint64_t) * CHAR_BIT
,
932 #if (BYTE_ORDER == BIG_ENDIAN)
941 ret
= _ltt_stream_packet_context_declare(session
);
945 ret
= _ltt_event_header_declare(session
);
950 cds_list_for_each_entry(chan
, &session
->chan
, list
) {
951 ret
= _ltt_channel_metadata_statedump(session
, chan
);
956 cds_list_for_each_entry(event
, &session
->events
, list
) {
957 ret
= _ltt_event_metadata_statedump(session
, event
->chan
, event
);
961 session
->metadata_dumped
= 1;
966 void lttng_ust_events_exit(void)
968 struct ltt_session
*session
, *tmpsession
;
970 cds_list_for_each_entry_safe(session
, tmpsession
, &sessions
, list
)
971 ltt_session_destroy(session
);