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 <sys/prctl.h>
29 #include <urcu/compiler.h>
30 #include <urcu/uatomic.h>
31 #include <urcu/arch.h>
33 #include <lttng/tracepoint.h>
34 #include <lttng/ust-events.h>
36 #include <usterr-signal-safe.h>
40 #include "tracepoint-internal.h"
41 #include "ltt-tracer.h"
42 #include "ltt-tracer-core.h"
44 #include "../libringbuffer/shm.h"
47 #define PROCNAME_LEN 17
50 * The sessions mutex is the centralized mutex across UST tracing
51 * control and probe registration. All operations within this file are
52 * called by the communication thread, under ust_lock protection.
54 static pthread_mutex_t sessions_mutex
= PTHREAD_MUTEX_INITIALIZER
;
58 pthread_mutex_lock(&sessions_mutex
);
63 pthread_mutex_unlock(&sessions_mutex
);
66 static CDS_LIST_HEAD(sessions
);
69 * Wildcard list, containing the active wildcards.
70 * Protected by ust lock.
72 static CDS_LIST_HEAD(wildcard_list
);
75 * Pending probes hash table, containing the registered ltt events for
76 * which tracepoint probes are still missing. Protected by the sessions
79 #define PENDING_PROBE_HASH_BITS 6
80 #define PENDING_PROBE_HASH_SIZE (1 << PENDING_PROBE_HASH_BITS)
81 static struct cds_hlist_head pending_probe_table
[PENDING_PROBE_HASH_SIZE
];
83 struct ust_pending_probe
{
84 struct ltt_event
*event
;
85 struct cds_hlist_node node
;
86 enum lttng_ust_loglevel_type loglevel_type
;
91 static void _ltt_event_destroy(struct ltt_event
*event
);
92 static void _ltt_wildcard_destroy(struct session_wildcard
*sw
);
93 static void _ltt_channel_destroy(struct ltt_channel
*chan
);
94 static int _ltt_event_unregister(struct ltt_event
*event
);
96 int _ltt_event_metadata_statedump(struct ltt_session
*session
,
97 struct ltt_channel
*chan
,
98 struct ltt_event
*event
);
100 int _ltt_session_metadata_statedump(struct ltt_session
*session
);
102 int ltt_loglevel_match(const struct lttng_event_desc
*desc
,
103 enum lttng_ust_loglevel_type req_type
,
108 if (req_type
== LTTNG_UST_LOGLEVEL_ALL
)
111 ev_loglevel
= TRACE_DEFAULT
;
113 ev_loglevel
= *(*desc
->loglevel
);
115 case LTTNG_UST_LOGLEVEL_RANGE
:
116 if (ev_loglevel
<= req_loglevel
|| req_loglevel
== -1)
120 case LTTNG_UST_LOGLEVEL_SINGLE
:
121 if (ev_loglevel
== req_loglevel
|| req_loglevel
== -1)
125 case LTTNG_UST_LOGLEVEL_ALL
:
132 * Return wildcard for a given event name if the event name match the
133 * one of the wildcards.
134 * Must be called with ust lock held.
135 * Returns NULL if not present.
138 struct wildcard_entry
*match_wildcard(const struct lttng_event_desc
*desc
)
140 struct wildcard_entry
*e
;
142 cds_list_for_each_entry(e
, &wildcard_list
, list
) {
143 /* If only contain '*' */
144 if (strlen(e
->name
) == 1)
146 /* Compare excluding final '*' */
147 if (!strncmp(desc
->name
, e
->name
, strlen(e
->name
) - 1))
149 continue; /* goto next, no match */
151 if (ltt_loglevel_match(desc
,
156 /* no match, loop to next */
162 * called at event creation if probe is missing.
163 * called with session mutex held.
166 int add_pending_probe(struct ltt_event
*event
, const char *name
,
167 enum lttng_ust_loglevel_type loglevel_type
,
170 struct cds_hlist_head
*head
;
171 struct ust_pending_probe
*e
;
172 size_t name_len
= strlen(name
);
175 if (name_len
> LTTNG_UST_SYM_NAME_LEN
- 1) {
176 WARN("Truncating tracepoint name %s which exceeds size limits of %u chars", name
, LTTNG_UST_SYM_NAME_LEN
- 1);
177 name_len
= LTTNG_UST_SYM_NAME_LEN
- 1;
179 hash
= jhash(name
, name_len
, 0);
180 head
= &pending_probe_table
[hash
& (PENDING_PROBE_HASH_SIZE
- 1)];
181 e
= zmalloc(sizeof(struct ust_pending_probe
) + name_len
);
184 memcpy(&e
->name
[0], name
, name_len
+ 1);
185 e
->name
[name_len
] = '\0';
186 e
->loglevel_type
= loglevel_type
;
187 e
->loglevel
= loglevel
;
188 cds_hlist_add_head(&e
->node
, head
);
190 event
->pending_probe
= e
;
195 * remove a pending probe. called when at event teardown and when an
196 * event is fixed (probe is loaded).
197 * called with session mutex held.
200 void remove_pending_probe(struct ust_pending_probe
*e
)
204 cds_hlist_del(&e
->node
);
209 * Called at library load: connect the probe on the events pending on
211 * called with session mutex held.
213 int pending_probe_fix_events(const struct lttng_event_desc
*desc
)
215 struct cds_hlist_head
*head
;
216 struct cds_hlist_node
*node
, *p
;
217 struct ust_pending_probe
*e
;
218 const char *name
= desc
->name
;
220 struct lttng_ust_event event_param
;
221 size_t name_len
= strlen(name
);
226 struct wildcard_entry
*wildcard
;
228 wildcard
= match_wildcard(desc
);
229 if (strcmp(desc
->name
, "lttng_ust:metadata") && wildcard
) {
230 struct session_wildcard
*sw
;
232 cds_list_for_each_entry(sw
, &wildcard
->session_list
,
234 struct ltt_event
*ev
;
237 memcpy(&event_param
, &sw
->event_param
,
238 sizeof(event_param
));
239 memcpy(event_param
.name
,
241 sizeof(event_param
.name
));
243 ret
= ltt_event_create(sw
->chan
,
247 DBG("Error creating event");
250 cds_list_add(&ev
->wildcard_list
,
256 if (name_len
> LTTNG_UST_SYM_NAME_LEN
- 1) {
257 WARN("Truncating tracepoint name %s which exceeds size limits of %u chars", name
, LTTNG_UST_SYM_NAME_LEN
- 1);
258 name_len
= LTTNG_UST_SYM_NAME_LEN
- 1;
260 hash
= jhash(name
, name_len
, 0);
261 head
= &pending_probe_table
[hash
& (PENDING_PROBE_HASH_SIZE
- 1)];
262 cds_hlist_for_each_entry_safe(e
, node
, p
, head
, node
) {
263 struct ltt_event
*event
;
264 struct ltt_channel
*chan
;
266 if (!ltt_loglevel_match(desc
,
271 if (strncmp(name
, e
->name
, LTTNG_UST_SYM_NAME_LEN
- 1)) {
276 assert(!event
->desc
);
278 event
->pending_probe
= NULL
;
279 remove_pending_probe(e
);
280 ret
|= __tracepoint_probe_register(name
,
281 event
->desc
->probe_callback
,
282 event
, event
->desc
->signature
);
285 event
->id
= chan
->free_event_id
++;
286 ret
|= _ltt_event_metadata_statedump(chan
->session
, chan
,
292 void synchronize_trace(void)
297 struct ltt_session
*ltt_session_create(void)
299 struct ltt_session
*session
;
301 session
= zmalloc(sizeof(struct ltt_session
));
304 CDS_INIT_LIST_HEAD(&session
->chan
);
305 CDS_INIT_LIST_HEAD(&session
->events
);
306 CDS_INIT_LIST_HEAD(&session
->wildcards
);
307 uuid_generate(session
->uuid
);
308 cds_list_add(&session
->list
, &sessions
);
312 void ltt_session_destroy(struct ltt_session
*session
)
314 struct ltt_channel
*chan
, *tmpchan
;
315 struct ltt_event
*event
, *tmpevent
;
316 struct session_wildcard
*wildcard
, *tmpwildcard
;
319 CMM_ACCESS_ONCE(session
->active
) = 0;
320 cds_list_for_each_entry(event
, &session
->events
, list
) {
321 ret
= _ltt_event_unregister(event
);
324 synchronize_trace(); /* Wait for in-flight events to complete */
325 cds_list_for_each_entry_safe(wildcard
, tmpwildcard
, &session
->wildcards
, list
)
326 _ltt_wildcard_destroy(wildcard
);
327 cds_list_for_each_entry_safe(event
, tmpevent
, &session
->events
, list
)
328 _ltt_event_destroy(event
);
329 cds_list_for_each_entry_safe(chan
, tmpchan
, &session
->chan
, list
)
330 _ltt_channel_destroy(chan
);
331 cds_list_del(&session
->list
);
335 int ltt_session_enable(struct ltt_session
*session
)
338 struct ltt_channel
*chan
;
340 if (session
->active
) {
346 * Snapshot the number of events per channel to know the type of header
349 cds_list_for_each_entry(chan
, &session
->chan
, list
) {
350 if (chan
->header_type
)
351 continue; /* don't change it if session stop/restart */
352 if (chan
->free_event_id
< 31)
353 chan
->header_type
= 1; /* compact */
355 chan
->header_type
= 2; /* large */
358 CMM_ACCESS_ONCE(session
->active
) = 1;
359 CMM_ACCESS_ONCE(session
->been_active
) = 1;
360 ret
= _ltt_session_metadata_statedump(session
);
362 CMM_ACCESS_ONCE(session
->active
) = 0;
367 int ltt_session_disable(struct ltt_session
*session
)
371 if (!session
->active
) {
375 CMM_ACCESS_ONCE(session
->active
) = 0;
380 int ltt_channel_enable(struct ltt_channel
*channel
)
384 if (channel
== channel
->session
->metadata
)
386 old
= uatomic_xchg(&channel
->enabled
, 1);
392 int ltt_channel_disable(struct ltt_channel
*channel
)
396 if (channel
== channel
->session
->metadata
)
398 old
= uatomic_xchg(&channel
->enabled
, 0);
404 int ltt_event_enable(struct ltt_event
*event
)
408 if (event
->chan
== event
->chan
->session
->metadata
)
410 old
= uatomic_xchg(&event
->enabled
, 1);
416 int ltt_event_disable(struct ltt_event
*event
)
420 if (event
->chan
== event
->chan
->session
->metadata
)
422 old
= uatomic_xchg(&event
->enabled
, 0);
428 struct ltt_channel
*ltt_channel_create(struct ltt_session
*session
,
429 const char *transport_name
,
431 size_t subbuf_size
, size_t num_subbuf
,
432 unsigned int switch_timer_interval
,
433 unsigned int read_timer_interval
,
434 int **shm_fd
, int **wait_fd
,
435 uint64_t **memory_map_size
,
436 struct ltt_channel
*chan_priv_init
)
438 struct ltt_channel
*chan
= NULL
;
439 struct ltt_transport
*transport
;
441 if (session
->been_active
)
442 goto active
; /* Refuse to add channel to active session */
443 transport
= ltt_transport_find(transport_name
);
445 DBG("LTTng transport %s not found\n",
449 chan_priv_init
->id
= session
->free_chan_id
++;
450 chan_priv_init
->session
= session
;
452 * Note: the channel creation op already writes into the packet
453 * headers. Therefore the "chan" information used as input
454 * should be already accessible.
456 chan
= transport
->ops
.channel_create("[lttng]", buf_addr
,
457 subbuf_size
, num_subbuf
, switch_timer_interval
,
458 read_timer_interval
, shm_fd
, wait_fd
,
459 memory_map_size
, chan_priv_init
);
463 chan
->ops
= &transport
->ops
;
464 cds_list_add(&chan
->list
, &session
->chan
);
474 * Only used internally at session destruction.
477 void _ltt_channel_destroy(struct ltt_channel
*chan
)
479 cds_list_del(&chan
->list
);
480 lttng_destroy_context(chan
->ctx
);
481 chan
->ops
->channel_destroy(chan
);
485 * Supports event creation while tracing session is active.
487 int ltt_event_create(struct ltt_channel
*chan
,
488 struct lttng_ust_event
*event_param
,
490 struct ltt_event
**_event
)
492 const struct lttng_event_desc
*desc
= NULL
; /* silence gcc */
493 struct ltt_event
*event
;
496 if (chan
->used_event_id
== -1U) {
501 * This is O(n^2) (for each event, the loop is called at event
502 * creation). Might require a hash if we have lots of events.
504 cds_list_for_each_entry(event
, &chan
->session
->events
, list
) {
505 if (event
->desc
&& !strncmp(event
->desc
->name
,
507 LTTNG_UST_SYM_NAME_LEN
- 1)) {
514 * Check if loglevel match. Refuse to connect event if not.
516 if (event_param
->instrumentation
== LTTNG_UST_TRACEPOINT
) {
517 desc
= ltt_event_get(event_param
->name
);
519 if (!ltt_loglevel_match(desc
,
520 event_param
->loglevel_type
,
521 event_param
->loglevel
)) {
523 goto no_loglevel_match
;
527 * If descriptor is not there, it will be added to
531 event
= zmalloc(sizeof(struct ltt_event
));
537 event
->filter
= filter
;
539 * used_event_id counts the maximum number of event IDs that can
540 * register if all probes register.
542 chan
->used_event_id
++;
544 event
->instrumentation
= event_param
->instrumentation
;
545 /* Populate ltt_event structure before tracepoint registration. */
547 switch (event_param
->instrumentation
) {
548 case LTTNG_UST_TRACEPOINT
:
551 ret
= __tracepoint_probe_register(event_param
->name
,
552 event
->desc
->probe_callback
,
553 event
, event
->desc
->signature
);
556 event
->id
= chan
->free_event_id
++;
559 * If the probe is not present, event->desc stays NULL,
560 * waiting for the probe to register, and the event->id
563 ret
= add_pending_probe(event
, event_param
->name
,
564 event_param
->loglevel_type
,
565 event_param
->loglevel
);
567 goto add_pending_error
;
574 ret
= _ltt_event_metadata_statedump(chan
->session
, chan
, event
);
576 goto statedump_error
;
578 cds_list_add(&event
->list
, &chan
->session
->events
);
584 WARN_ON_ONCE(__tracepoint_probe_unregister(event_param
->name
,
585 event
->desc
->probe_callback
,
587 ltt_event_put(event
->desc
);
600 * Only used internally at session destruction.
602 int _ltt_event_unregister(struct ltt_event
*event
)
606 switch (event
->instrumentation
) {
607 case LTTNG_UST_TRACEPOINT
:
609 ret
= __tracepoint_probe_unregister(event
->desc
->name
,
610 event
->desc
->probe_callback
,
615 remove_pending_probe(event
->pending_probe
);
626 * Only used internally at session destruction.
629 void _ltt_event_destroy(struct ltt_event
*event
)
631 switch (event
->instrumentation
) {
632 case LTTNG_UST_TRACEPOINT
:
634 ltt_event_put(event
->desc
);
640 cds_list_del(&event
->list
);
641 lttng_destroy_context(event
->ctx
);
646 * We have exclusive access to our metadata buffer (protected by the
647 * ust_lock), so we can do racy operations such as looking for
648 * remaining space left in packet and write, since mutual exclusion
649 * protects us from concurrent writes.
651 int lttng_metadata_printf(struct ltt_session
*session
,
652 const char *fmt
, ...)
654 struct lttng_ust_lib_ring_buffer_ctx ctx
;
655 struct ltt_channel
*chan
= session
->metadata
;
657 int ret
= 0, waitret
;
658 size_t len
, reserve_len
, pos
;
661 WARN_ON_ONCE(!CMM_ACCESS_ONCE(session
->active
));
664 ret
= vasprintf(&str
, fmt
, ap
);
672 for (pos
= 0; pos
< len
; pos
+= reserve_len
) {
673 reserve_len
= min_t(size_t,
674 chan
->ops
->packet_avail_size(chan
->chan
, chan
->handle
),
676 lib_ring_buffer_ctx_init(&ctx
, chan
->chan
, NULL
, reserve_len
,
677 sizeof(char), -1, chan
->handle
);
679 * We don't care about metadata buffer's records lost
680 * count, because we always retry here. Report error if
681 * we need to bail out after timeout or being
684 waitret
= wait_cond_interruptible_timeout(
686 ret
= chan
->ops
->event_reserve(&ctx
, 0);
687 ret
!= -ENOBUFS
|| !ret
;
689 LTTNG_METADATA_TIMEOUT_MSEC
);
690 if (waitret
== -ETIMEDOUT
|| waitret
== -EINTR
|| ret
) {
691 DBG("LTTng: Failure to write metadata to buffers (%s)\n",
692 waitret
== -EINTR
? "interrupted" :
693 (ret
== -ENOBUFS
? "timeout" : "I/O error"));
694 if (waitret
== -EINTR
)
698 chan
->ops
->event_write(&ctx
, &str
[pos
], reserve_len
);
699 chan
->ops
->event_commit(&ctx
);
707 int _ltt_field_statedump(struct ltt_session
*session
,
708 const struct lttng_event_field
*field
)
712 switch (field
->type
.atype
) {
714 ret
= lttng_metadata_printf(session
,
715 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n",
716 field
->type
.u
.basic
.integer
.size
,
717 field
->type
.u
.basic
.integer
.alignment
,
718 field
->type
.u
.basic
.integer
.signedness
,
719 (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_none
)
721 : (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
724 field
->type
.u
.basic
.integer
.base
,
725 #if (BYTE_ORDER == BIG_ENDIAN)
726 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
728 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
733 ret
= lttng_metadata_printf(session
,
734 " floating_point { exp_dig = %u; mant_dig = %u; align = %u;%s } _%s;\n",
735 field
->type
.u
.basic
._float
.exp_dig
,
736 field
->type
.u
.basic
._float
.mant_dig
,
737 field
->type
.u
.basic
._float
.alignment
,
738 #if (BYTE_ORDER == BIG_ENDIAN)
739 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
741 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
746 ret
= lttng_metadata_printf(session
,
748 field
->type
.u
.basic
.enumeration
.name
,
753 const struct lttng_basic_type
*elem_type
;
755 elem_type
= &field
->type
.u
.array
.elem_type
;
756 ret
= lttng_metadata_printf(session
,
757 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n",
758 elem_type
->u
.basic
.integer
.size
,
759 elem_type
->u
.basic
.integer
.alignment
,
760 elem_type
->u
.basic
.integer
.signedness
,
761 (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
763 : (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
766 elem_type
->u
.basic
.integer
.base
,
767 #if (BYTE_ORDER == BIG_ENDIAN)
768 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
770 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
772 field
->name
, field
->type
.u
.array
.length
);
777 const struct lttng_basic_type
*elem_type
;
778 const struct lttng_basic_type
*length_type
;
780 elem_type
= &field
->type
.u
.sequence
.elem_type
;
781 length_type
= &field
->type
.u
.sequence
.length_type
;
782 ret
= lttng_metadata_printf(session
,
783 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n",
784 length_type
->u
.basic
.integer
.size
,
785 (unsigned int) length_type
->u
.basic
.integer
.alignment
,
786 length_type
->u
.basic
.integer
.signedness
,
787 (length_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
789 : ((length_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
792 length_type
->u
.basic
.integer
.base
,
793 #if (BYTE_ORDER == BIG_ENDIAN)
794 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
796 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
802 ret
= lttng_metadata_printf(session
,
803 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n",
804 elem_type
->u
.basic
.integer
.size
,
805 (unsigned int) elem_type
->u
.basic
.integer
.alignment
,
806 elem_type
->u
.basic
.integer
.signedness
,
807 (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
809 : ((elem_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
812 elem_type
->u
.basic
.integer
.base
,
813 #if (BYTE_ORDER == BIG_ENDIAN)
814 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
816 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
824 /* Default encoding is UTF8 */
825 ret
= lttng_metadata_printf(session
,
827 field
->type
.u
.basic
.string
.encoding
== lttng_encode_ASCII
?
828 " { encoding = ASCII; }" : "",
839 int _ltt_context_metadata_statedump(struct ltt_session
*session
,
840 struct lttng_ctx
*ctx
)
847 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
848 const struct lttng_ctx_field
*field
= &ctx
->fields
[i
];
850 ret
= _ltt_field_statedump(session
, &field
->event_field
);
858 int _ltt_fields_metadata_statedump(struct ltt_session
*session
,
859 struct ltt_event
*event
)
861 const struct lttng_event_desc
*desc
= event
->desc
;
865 for (i
= 0; i
< desc
->nr_fields
; i
++) {
866 const struct lttng_event_field
*field
= &desc
->fields
[i
];
868 ret
= _ltt_field_statedump(session
, field
);
876 int _ltt_event_metadata_statedump(struct ltt_session
*session
,
877 struct ltt_channel
*chan
,
878 struct ltt_event
*event
)
881 int loglevel
= TRACE_DEFAULT
;
883 if (event
->metadata_dumped
|| !CMM_ACCESS_ONCE(session
->active
))
885 if (chan
== session
->metadata
)
888 * Don't print events for which probe load is pending.
893 ret
= lttng_metadata_printf(session
,
897 " stream_id = %u;\n",
904 if (event
->desc
->loglevel
)
905 loglevel
= *(*event
->desc
->loglevel
);
907 ret
= lttng_metadata_printf(session
,
914 ret
= lttng_metadata_printf(session
,
915 " context := struct {\n");
919 ret
= _ltt_context_metadata_statedump(session
, event
->ctx
);
923 ret
= lttng_metadata_printf(session
,
929 ret
= lttng_metadata_printf(session
,
930 " fields := struct {\n"
935 ret
= _ltt_fields_metadata_statedump(session
, event
);
940 * LTTng space reservation can only reserve multiples of the
943 ret
= lttng_metadata_printf(session
,
949 event
->metadata_dumped
= 1;
956 int _ltt_channel_metadata_statedump(struct ltt_session
*session
,
957 struct ltt_channel
*chan
)
961 if (chan
->metadata_dumped
|| !CMM_ACCESS_ONCE(session
->active
))
963 if (chan
== session
->metadata
)
966 WARN_ON_ONCE(!chan
->header_type
);
967 ret
= lttng_metadata_printf(session
,
970 " event.header := %s;\n"
971 " packet.context := struct packet_context;\n",
973 chan
->header_type
== 1 ? "struct event_header_compact" :
974 "struct event_header_large");
979 ret
= lttng_metadata_printf(session
,
980 " event.context := struct {\n");
984 ret
= _ltt_context_metadata_statedump(session
, chan
->ctx
);
988 ret
= lttng_metadata_printf(session
,
994 ret
= lttng_metadata_printf(session
,
997 chan
->metadata_dumped
= 1;
1003 int _ltt_stream_packet_context_declare(struct ltt_session
*session
)
1005 return lttng_metadata_printf(session
,
1006 "struct packet_context {\n"
1007 " uint64_clock_monotonic_t timestamp_begin;\n"
1008 " uint64_clock_monotonic_t timestamp_end;\n"
1009 " uint32_t events_discarded;\n"
1010 " uint32_t content_size;\n"
1011 " uint32_t packet_size;\n"
1012 " uint32_t cpu_id;\n"
1019 * id: range: 0 - 30.
1020 * id 31 is reserved to indicate an extended header.
1023 * id: range: 0 - 65534.
1024 * id 65535 is reserved to indicate an extended header.
1027 int _ltt_event_header_declare(struct ltt_session
*session
)
1029 return lttng_metadata_printf(session
,
1030 "struct event_header_compact {\n"
1031 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
1034 " uint27_clock_monotonic_t timestamp;\n"
1038 " uint64_clock_monotonic_t timestamp;\n"
1043 "struct event_header_large {\n"
1044 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
1047 " uint32_clock_monotonic_t timestamp;\n"
1051 " uint64_clock_monotonic_t timestamp;\n"
1055 lttng_alignof(uint32_t) * CHAR_BIT
,
1056 lttng_alignof(uint16_t) * CHAR_BIT
1061 * Approximation of NTP time of day to clock monotonic correlation,
1062 * taken at start of trace.
1063 * Yes, this is only an approximation. Yes, we can (and will) do better
1064 * in future versions.
1067 uint64_t measure_clock_offset(void)
1069 uint64_t offset
, monotonic
[2], realtime
;
1070 struct timespec rts
= { 0, 0 };
1073 monotonic
[0] = trace_clock_read64();
1074 ret
= clock_gettime(CLOCK_REALTIME
, &rts
);
1077 monotonic
[1] = trace_clock_read64();
1078 offset
= (monotonic
[0] + monotonic
[1]) >> 1;
1079 realtime
= rts
.tv_sec
* 1000000000ULL;
1080 realtime
+= rts
.tv_nsec
;
1081 offset
= realtime
- offset
;
1086 * Output metadata into this session's metadata buffers.
1089 int _ltt_session_metadata_statedump(struct ltt_session
*session
)
1091 unsigned char *uuid_c
= session
->uuid
;
1092 char uuid_s
[37], clock_uuid_s
[CLOCK_UUID_LEN
];
1093 struct ltt_channel
*chan
;
1094 struct ltt_event
*event
;
1096 char procname
[PROCNAME_LEN
] = "";
1098 if (!CMM_ACCESS_ONCE(session
->active
))
1100 if (session
->metadata_dumped
)
1102 if (!session
->metadata
) {
1103 DBG("LTTng: attempt to start tracing, but metadata channel is not found. Operation abort.\n");
1107 snprintf(uuid_s
, sizeof(uuid_s
),
1108 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
1109 uuid_c
[0], uuid_c
[1], uuid_c
[2], uuid_c
[3],
1110 uuid_c
[4], uuid_c
[5], uuid_c
[6], uuid_c
[7],
1111 uuid_c
[8], uuid_c
[9], uuid_c
[10], uuid_c
[11],
1112 uuid_c
[12], uuid_c
[13], uuid_c
[14], uuid_c
[15]);
1114 ret
= lttng_metadata_printf(session
,
1115 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
1116 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
1117 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
1118 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
1119 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
1120 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
1126 " byte_order = %s;\n"
1127 " packet.header := struct {\n"
1128 " uint32_t magic;\n"
1129 " uint8_t uuid[16];\n"
1130 " uint32_t stream_id;\n"
1133 lttng_alignof(uint8_t) * CHAR_BIT
,
1134 lttng_alignof(uint16_t) * CHAR_BIT
,
1135 lttng_alignof(uint32_t) * CHAR_BIT
,
1136 lttng_alignof(uint64_t) * CHAR_BIT
,
1140 #if (BYTE_ORDER == BIG_ENDIAN)
1149 /* ignore error, just use empty string if error. */
1150 (void) prctl(PR_GET_NAME
, (unsigned long) procname
, 0, 0, 0);
1151 procname
[PROCNAME_LEN
- 1] = '\0';
1152 ret
= lttng_metadata_printf(session
,
1155 " procname = \"%s\";\n"
1156 " domain = \"ust\";\n"
1157 " tracer_name = \"lttng-ust\";\n"
1158 " tracer_major = %u;\n"
1159 " tracer_minor = %u;\n"
1160 " tracer_patchlevel = %u;\n"
1164 LTTNG_UST_MAJOR_VERSION
,
1165 LTTNG_UST_MINOR_VERSION
,
1166 LTTNG_UST_PATCHLEVEL_VERSION
1171 ret
= lttng_metadata_printf(session
,
1179 if (!trace_clock_uuid(clock_uuid_s
)) {
1180 ret
= lttng_metadata_printf(session
,
1181 " uuid = \"%s\";\n",
1188 ret
= lttng_metadata_printf(session
,
1189 " description = \"Monotonic Clock\";\n"
1190 " freq = %" PRIu64
"; /* Frequency, in Hz */\n"
1191 " /* clock value offset from Epoch is: offset * (1/freq) */\n"
1192 " offset = %" PRIu64
";\n"
1195 measure_clock_offset()
1200 ret
= lttng_metadata_printf(session
,
1201 "typealias integer {\n"
1202 " size = 27; align = 1; signed = false;\n"
1203 " map = clock.monotonic.value;\n"
1204 "} := uint27_clock_monotonic_t;\n"
1206 "typealias integer {\n"
1207 " size = 32; align = %u; signed = false;\n"
1208 " map = clock.monotonic.value;\n"
1209 "} := uint32_clock_monotonic_t;\n"
1211 "typealias integer {\n"
1212 " size = 64; align = %u; signed = false;\n"
1213 " map = clock.monotonic.value;\n"
1214 "} := uint64_clock_monotonic_t;\n\n",
1215 lttng_alignof(uint32_t) * CHAR_BIT
,
1216 lttng_alignof(uint64_t) * CHAR_BIT
1221 ret
= _ltt_stream_packet_context_declare(session
);
1225 ret
= _ltt_event_header_declare(session
);
1230 cds_list_for_each_entry(chan
, &session
->chan
, list
) {
1231 ret
= _ltt_channel_metadata_statedump(session
, chan
);
1236 cds_list_for_each_entry(event
, &session
->events
, list
) {
1237 ret
= _ltt_event_metadata_statedump(session
, event
->chan
, event
);
1241 session
->metadata_dumped
= 1;
1246 void lttng_ust_events_exit(void)
1248 struct ltt_session
*session
, *tmpsession
;
1250 cds_list_for_each_entry_safe(session
, tmpsession
, &sessions
, list
)
1251 ltt_session_destroy(session
);
1257 int wildcard_same_loglevel(struct wildcard_entry
*e
,
1258 enum lttng_ust_loglevel_type loglevel_type
,
1261 if (e
->loglevel_type
== loglevel_type
&& e
->loglevel
== loglevel
)
1269 int wildcard_is_within(struct wildcard_entry
*e
,
1270 enum lttng_ust_loglevel_type loglevel_type
,
1273 if (e
->loglevel_type
== LTTNG_UST_LOGLEVEL_ALL
1274 || e
->loglevel
== -1)
1276 switch (e
->loglevel_type
) {
1277 case LTTNG_UST_LOGLEVEL_RANGE
:
1278 switch (loglevel_type
) {
1279 case LTTNG_UST_LOGLEVEL_RANGE
:
1280 if (e
->loglevel
>= loglevel
)
1284 case LTTNG_UST_LOGLEVEL_SINGLE
:
1285 if (e
->loglevel
<= 0 && loglevel
== 0)
1290 case LTTNG_UST_LOGLEVEL_SINGLE
:
1291 switch (loglevel_type
) {
1292 case LTTNG_UST_LOGLEVEL_RANGE
:
1297 case LTTNG_UST_LOGLEVEL_SINGLE
:
1298 if (e
->loglevel
== loglevel
)
1308 * Add the wildcard to the wildcard list. Must be called with
1312 struct session_wildcard
*add_wildcard(struct ltt_channel
*chan
,
1313 struct lttng_ust_event
*event_param
)
1315 struct wildcard_entry
*e
;
1316 struct session_wildcard
*sw
;
1317 size_t name_len
= strlen(event_param
->name
) + 1;
1321 * Try to find global wildcard entry. Given that this is shared
1322 * across all sessions, we need to check for exact loglevel
1323 * match, not just whether contained within the existing ones.
1325 cds_list_for_each_entry(e
, &wildcard_list
, list
) {
1326 if (!strncmp(event_param
->name
, e
->name
,
1327 LTTNG_UST_SYM_NAME_LEN
- 1)) {
1328 if (wildcard_same_loglevel(e
,
1329 event_param
->loglevel_type
,
1330 event_param
->loglevel
)) {
1339 * Create global wildcard entry if not found. Using
1340 * zmalloc here to allocate a variable length element.
1341 * Could cause some memory fragmentation if overused.
1343 e
= zmalloc(sizeof(struct wildcard_entry
) + name_len
);
1345 return ERR_PTR(-ENOMEM
);
1346 memcpy(&e
->name
[0], event_param
->name
, name_len
);
1347 cds_list_add(&e
->list
, &wildcard_list
);
1348 CDS_INIT_LIST_HEAD(&e
->session_list
);
1351 /* session wildcard */
1352 cds_list_for_each_entry(sw
, &e
->session_list
, session_list
) {
1353 if (chan
== sw
->chan
) {
1354 DBG("wildcard %s busy for this channel",
1356 return ERR_PTR(-EEXIST
); /* Already there */
1359 sw
= zmalloc(sizeof(struct session_wildcard
));
1361 return ERR_PTR(-ENOMEM
);
1364 memcpy(&sw
->event_param
, event_param
, sizeof(sw
->event_param
));
1365 sw
->event_param
.instrumentation
= LTTNG_UST_TRACEPOINT
;
1366 sw
->event_param
.loglevel_type
= event_param
->loglevel_type
;
1367 sw
->event_param
.loglevel
= event_param
->loglevel
;
1368 CDS_INIT_LIST_HEAD(&sw
->events
);
1369 cds_list_add(&sw
->list
, &chan
->session
->wildcards
);
1370 cds_list_add(&sw
->session_list
, &e
->session_list
);
1372 ltt_probes_create_wildcard_events(e
, sw
);
1377 * Remove the wildcard from the wildcard list. Must be called with
1378 * ust_lock held. Only called at session teardown.
1381 void _remove_wildcard(struct session_wildcard
*wildcard
)
1383 struct ltt_event
*ev
, *tmp
;
1386 * Just remove the events owned (for enable/disable) by this
1387 * wildcard from the list. The session teardown will take care
1388 * of freeing the event memory.
1390 cds_list_for_each_entry_safe(ev
, tmp
, &wildcard
->events
,
1392 cds_list_del(&ev
->wildcard_list
);
1394 cds_list_del(&wildcard
->session_list
);
1395 cds_list_del(&wildcard
->list
);
1396 if (cds_list_empty(&wildcard
->entry
->session_list
)) {
1397 cds_list_del(&wildcard
->entry
->list
);
1398 free(wildcard
->entry
);
1403 int ltt_wildcard_create(struct ltt_channel
*chan
,
1404 struct lttng_ust_event
*event_param
,
1405 struct session_wildcard
**_sw
)
1407 struct session_wildcard
*sw
;
1409 sw
= add_wildcard(chan
, event_param
);
1410 if (!sw
|| IS_ERR(sw
)) {
1418 void _ltt_wildcard_destroy(struct session_wildcard
*sw
)
1420 _remove_wildcard(sw
);
1423 int ltt_wildcard_enable(struct session_wildcard
*wildcard
)
1425 struct ltt_event
*ev
;
1428 if (wildcard
->enabled
)
1430 cds_list_for_each_entry(ev
, &wildcard
->events
, wildcard_list
) {
1431 ret
= ltt_event_enable(ev
);
1433 DBG("Error: enable error.\n");
1437 wildcard
->enabled
= 1;
1441 int ltt_wildcard_disable(struct session_wildcard
*wildcard
)
1443 struct ltt_event
*ev
;
1446 if (!wildcard
->enabled
)
1448 cds_list_for_each_entry(ev
, &wildcard
->events
, wildcard_list
) {
1449 ret
= ltt_event_disable(ev
);
1451 DBG("Error: disable error.\n");
1455 wildcard
->enabled
= 0;
1460 * Take the TLS "fault" in libuuid if dlopen'd, which can take the
1461 * dynamic linker mutex, outside of the UST lock, since the UST lock is
1462 * taken in constructors, which are called with dynamic linker mutex
1465 void lttng_fixup_event_tls(void)
1467 unsigned char uuid
[37];
1469 (void) uuid_generate(uuid
);