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>
28 #include <urcu/compiler.h>
29 #include <urcu/uatomic.h>
30 #include <urcu/arch.h>
32 #include <lttng/tracepoint.h>
33 #include <lttng/ust-events.h>
35 #include <usterr-signal-safe.h>
39 #include "tracepoint-internal.h"
40 #include "ltt-tracer.h"
41 #include "ltt-tracer-core.h"
43 #include "../libringbuffer/shm.h"
47 * The sessions mutex is the centralized mutex across UST tracing
48 * control and probe registration. All operations within this file are
49 * called by the communication thread, under ust_lock protection.
51 static pthread_mutex_t sessions_mutex
= PTHREAD_MUTEX_INITIALIZER
;
55 pthread_mutex_lock(&sessions_mutex
);
60 pthread_mutex_unlock(&sessions_mutex
);
63 static CDS_LIST_HEAD(sessions
);
66 * Wildcard list, containing the active wildcards.
67 * Protected by ust lock.
69 static CDS_LIST_HEAD(wildcard_list
);
72 * Pending probes hash table, containing the registered ltt events for
73 * which tracepoint probes are still missing. Protected by the sessions
76 #define PENDING_PROBE_HASH_BITS 6
77 #define PENDING_PROBE_HASH_SIZE (1 << PENDING_PROBE_HASH_BITS)
78 static struct cds_hlist_head pending_probe_table
[PENDING_PROBE_HASH_SIZE
];
80 struct ust_pending_probe
{
81 struct ltt_event
*event
;
82 struct cds_hlist_node node
;
83 enum lttng_ust_loglevel_type loglevel_type
;
88 static void _ltt_event_destroy(struct ltt_event
*event
);
89 static void _ltt_wildcard_destroy(struct session_wildcard
*sw
);
90 static void _ltt_channel_destroy(struct ltt_channel
*chan
);
91 static int _ltt_event_unregister(struct ltt_event
*event
);
93 int _ltt_event_metadata_statedump(struct ltt_session
*session
,
94 struct ltt_channel
*chan
,
95 struct ltt_event
*event
);
97 int _ltt_session_metadata_statedump(struct ltt_session
*session
);
99 int ltt_loglevel_match(const struct lttng_event_desc
*desc
,
100 enum lttng_ust_loglevel_type req_type
,
105 if (req_type
== LTTNG_UST_LOGLEVEL_ALL
)
108 ev_loglevel
= TRACE_DEFAULT
;
110 ev_loglevel
= *(*desc
->loglevel
);
112 case LTTNG_UST_LOGLEVEL_RANGE
:
113 if (ev_loglevel
<= req_loglevel
|| req_loglevel
== -1)
117 case LTTNG_UST_LOGLEVEL_SINGLE
:
118 if (ev_loglevel
== req_loglevel
|| req_loglevel
== -1)
122 case LTTNG_UST_LOGLEVEL_ALL
:
129 * Return wildcard for a given event name if the event name match the
130 * one of the wildcards.
131 * Must be called with ust lock held.
132 * Returns NULL if not present.
135 struct wildcard_entry
*match_wildcard(const struct lttng_event_desc
*desc
)
137 struct wildcard_entry
*e
;
139 cds_list_for_each_entry(e
, &wildcard_list
, list
) {
140 /* If only contain '*' */
141 if (strlen(e
->name
) == 1)
143 /* Compare excluding final '*' */
144 if (!strncmp(desc
->name
, e
->name
, strlen(e
->name
) - 1))
146 continue; /* goto next, no match */
148 if (ltt_loglevel_match(desc
,
153 /* no match, loop to next */
159 * called at event creation if probe is missing.
160 * called with session mutex held.
163 int add_pending_probe(struct ltt_event
*event
, const char *name
,
164 enum lttng_ust_loglevel_type loglevel_type
,
167 struct cds_hlist_head
*head
;
168 struct ust_pending_probe
*e
;
169 size_t name_len
= strlen(name
);
172 if (name_len
> LTTNG_UST_SYM_NAME_LEN
- 1) {
173 WARN("Truncating tracepoint name %s which exceeds size limits of %u chars", name
, LTTNG_UST_SYM_NAME_LEN
- 1);
174 name_len
= LTTNG_UST_SYM_NAME_LEN
- 1;
176 hash
= jhash(name
, name_len
, 0);
177 head
= &pending_probe_table
[hash
& (PENDING_PROBE_HASH_SIZE
- 1)];
178 e
= zmalloc(sizeof(struct ust_pending_probe
) + name_len
);
181 memcpy(&e
->name
[0], name
, name_len
+ 1);
182 e
->name
[name_len
] = '\0';
183 e
->loglevel_type
= loglevel_type
;
184 e
->loglevel
= loglevel
;
185 cds_hlist_add_head(&e
->node
, head
);
187 event
->pending_probe
= e
;
192 * remove a pending probe. called when at event teardown and when an
193 * event is fixed (probe is loaded).
194 * called with session mutex held.
197 void remove_pending_probe(struct ust_pending_probe
*e
)
201 cds_hlist_del(&e
->node
);
206 * Called at library load: connect the probe on the events pending on
208 * called with session mutex held.
210 int pending_probe_fix_events(const struct lttng_event_desc
*desc
)
212 struct cds_hlist_head
*head
;
213 struct cds_hlist_node
*node
, *p
;
214 struct ust_pending_probe
*e
;
215 const char *name
= desc
->name
;
217 struct lttng_ust_event event_param
;
218 size_t name_len
= strlen(name
);
223 struct wildcard_entry
*wildcard
;
225 wildcard
= match_wildcard(desc
);
226 if (strcmp(desc
->name
, "lttng_ust:metadata") && wildcard
) {
227 struct session_wildcard
*sw
;
229 cds_list_for_each_entry(sw
, &wildcard
->session_list
,
231 struct ltt_event
*ev
;
234 memcpy(&event_param
, &sw
->event_param
,
235 sizeof(event_param
));
236 memcpy(event_param
.name
,
238 sizeof(event_param
.name
));
240 ret
= ltt_event_create(sw
->chan
,
244 DBG("Error creating event");
247 cds_list_add(&ev
->wildcard_list
,
253 if (name_len
> LTTNG_UST_SYM_NAME_LEN
- 1) {
254 WARN("Truncating tracepoint name %s which exceeds size limits of %u chars", name
, LTTNG_UST_SYM_NAME_LEN
- 1);
255 name_len
= LTTNG_UST_SYM_NAME_LEN
- 1;
257 hash
= jhash(name
, name_len
, 0);
258 head
= &pending_probe_table
[hash
& (PENDING_PROBE_HASH_SIZE
- 1)];
259 cds_hlist_for_each_entry_safe(e
, node
, p
, head
, node
) {
260 struct ltt_event
*event
;
261 struct ltt_channel
*chan
;
263 if (!ltt_loglevel_match(desc
,
268 if (strncmp(name
, e
->name
, LTTNG_UST_SYM_NAME_LEN
- 1)) {
273 assert(!event
->desc
);
275 event
->pending_probe
= NULL
;
276 remove_pending_probe(e
);
277 ret
|= __tracepoint_probe_register(name
,
278 event
->desc
->probe_callback
,
282 event
->id
= chan
->free_event_id
++;
283 ret
|= _ltt_event_metadata_statedump(chan
->session
, chan
,
289 void synchronize_trace(void)
294 struct ltt_session
*ltt_session_create(void)
296 struct ltt_session
*session
;
298 session
= zmalloc(sizeof(struct ltt_session
));
301 CDS_INIT_LIST_HEAD(&session
->chan
);
302 CDS_INIT_LIST_HEAD(&session
->events
);
303 CDS_INIT_LIST_HEAD(&session
->wildcards
);
304 uuid_generate(session
->uuid
);
305 cds_list_add(&session
->list
, &sessions
);
309 void ltt_session_destroy(struct ltt_session
*session
)
311 struct ltt_channel
*chan
, *tmpchan
;
312 struct ltt_event
*event
, *tmpevent
;
313 struct session_wildcard
*wildcard
, *tmpwildcard
;
316 CMM_ACCESS_ONCE(session
->active
) = 0;
317 cds_list_for_each_entry(event
, &session
->events
, list
) {
318 ret
= _ltt_event_unregister(event
);
321 synchronize_trace(); /* Wait for in-flight events to complete */
322 cds_list_for_each_entry_safe(wildcard
, tmpwildcard
, &session
->wildcards
, list
)
323 _ltt_wildcard_destroy(wildcard
);
324 cds_list_for_each_entry_safe(event
, tmpevent
, &session
->events
, list
)
325 _ltt_event_destroy(event
);
326 cds_list_for_each_entry_safe(chan
, tmpchan
, &session
->chan
, list
)
327 _ltt_channel_destroy(chan
);
328 cds_list_del(&session
->list
);
332 int ltt_session_enable(struct ltt_session
*session
)
335 struct ltt_channel
*chan
;
337 if (session
->active
) {
343 * Snapshot the number of events per channel to know the type of header
346 cds_list_for_each_entry(chan
, &session
->chan
, list
) {
347 if (chan
->header_type
)
348 continue; /* don't change it if session stop/restart */
349 if (chan
->free_event_id
< 31)
350 chan
->header_type
= 1; /* compact */
352 chan
->header_type
= 2; /* large */
355 CMM_ACCESS_ONCE(session
->active
) = 1;
356 CMM_ACCESS_ONCE(session
->been_active
) = 1;
357 ret
= _ltt_session_metadata_statedump(session
);
359 CMM_ACCESS_ONCE(session
->active
) = 0;
364 int ltt_session_disable(struct ltt_session
*session
)
368 if (!session
->active
) {
372 CMM_ACCESS_ONCE(session
->active
) = 0;
377 int ltt_channel_enable(struct ltt_channel
*channel
)
381 if (channel
== channel
->session
->metadata
)
383 old
= uatomic_xchg(&channel
->enabled
, 1);
389 int ltt_channel_disable(struct ltt_channel
*channel
)
393 if (channel
== channel
->session
->metadata
)
395 old
= uatomic_xchg(&channel
->enabled
, 0);
401 int ltt_event_enable(struct ltt_event
*event
)
405 if (event
->chan
== event
->chan
->session
->metadata
)
407 old
= uatomic_xchg(&event
->enabled
, 1);
413 int ltt_event_disable(struct ltt_event
*event
)
417 if (event
->chan
== event
->chan
->session
->metadata
)
419 old
= uatomic_xchg(&event
->enabled
, 0);
425 struct ltt_channel
*ltt_channel_create(struct ltt_session
*session
,
426 const char *transport_name
,
428 size_t subbuf_size
, size_t num_subbuf
,
429 unsigned int switch_timer_interval
,
430 unsigned int read_timer_interval
,
431 int **shm_fd
, int **wait_fd
,
432 uint64_t **memory_map_size
,
433 struct ltt_channel
*chan_priv_init
)
435 struct ltt_channel
*chan
= NULL
;
436 struct ltt_transport
*transport
;
438 if (session
->been_active
)
439 goto active
; /* Refuse to add channel to active session */
440 transport
= ltt_transport_find(transport_name
);
442 DBG("LTTng transport %s not found\n",
446 chan_priv_init
->id
= session
->free_chan_id
++;
447 chan_priv_init
->session
= session
;
449 * Note: the channel creation op already writes into the packet
450 * headers. Therefore the "chan" information used as input
451 * should be already accessible.
453 chan
= transport
->ops
.channel_create("[lttng]", buf_addr
,
454 subbuf_size
, num_subbuf
, switch_timer_interval
,
455 read_timer_interval
, shm_fd
, wait_fd
,
456 memory_map_size
, chan_priv_init
);
460 chan
->ops
= &transport
->ops
;
461 cds_list_add(&chan
->list
, &session
->chan
);
471 * Only used internally at session destruction.
474 void _ltt_channel_destroy(struct ltt_channel
*chan
)
476 cds_list_del(&chan
->list
);
477 lttng_destroy_context(chan
->ctx
);
478 chan
->ops
->channel_destroy(chan
);
482 * Supports event creation while tracing session is active.
484 int ltt_event_create(struct ltt_channel
*chan
,
485 struct lttng_ust_event
*event_param
,
487 struct ltt_event
**_event
)
489 const struct lttng_event_desc
*desc
= NULL
; /* silence gcc */
490 struct ltt_event
*event
;
493 if (chan
->used_event_id
== -1UL) {
498 * This is O(n^2) (for each event, the loop is called at event
499 * creation). Might require a hash if we have lots of events.
501 cds_list_for_each_entry(event
, &chan
->session
->events
, list
) {
502 if (event
->desc
&& !strncmp(event
->desc
->name
,
504 LTTNG_UST_SYM_NAME_LEN
- 1)) {
511 * Check if loglevel match. Refuse to connect event if not.
513 if (event_param
->instrumentation
== LTTNG_UST_TRACEPOINT
) {
514 desc
= ltt_event_get(event_param
->name
);
516 if (!ltt_loglevel_match(desc
,
517 event_param
->loglevel_type
,
518 event_param
->loglevel
)) {
520 goto no_loglevel_match
;
524 * If descriptor is not there, it will be added to
528 event
= zmalloc(sizeof(struct ltt_event
));
534 event
->filter
= filter
;
536 * used_event_id counts the maximum number of event IDs that can
537 * register if all probes register.
539 chan
->used_event_id
++;
541 event
->instrumentation
= event_param
->instrumentation
;
542 /* Populate ltt_event structure before tracepoint registration. */
544 switch (event_param
->instrumentation
) {
545 case LTTNG_UST_TRACEPOINT
:
548 ret
= __tracepoint_probe_register(event_param
->name
,
549 event
->desc
->probe_callback
,
553 event
->id
= chan
->free_event_id
++;
556 * If the probe is not present, event->desc stays NULL,
557 * waiting for the probe to register, and the event->id
560 ret
= add_pending_probe(event
, event_param
->name
,
561 event_param
->loglevel_type
,
562 event_param
->loglevel
);
564 goto add_pending_error
;
571 ret
= _ltt_event_metadata_statedump(chan
->session
, chan
, event
);
573 goto statedump_error
;
575 cds_list_add(&event
->list
, &chan
->session
->events
);
581 WARN_ON_ONCE(__tracepoint_probe_unregister(event_param
->name
,
582 event
->desc
->probe_callback
,
584 ltt_event_put(event
->desc
);
597 * Only used internally at session destruction.
599 int _ltt_event_unregister(struct ltt_event
*event
)
603 switch (event
->instrumentation
) {
604 case LTTNG_UST_TRACEPOINT
:
606 ret
= __tracepoint_probe_unregister(event
->desc
->name
,
607 event
->desc
->probe_callback
,
612 remove_pending_probe(event
->pending_probe
);
623 * Only used internally at session destruction.
626 void _ltt_event_destroy(struct ltt_event
*event
)
628 switch (event
->instrumentation
) {
629 case LTTNG_UST_TRACEPOINT
:
631 ltt_event_put(event
->desc
);
637 cds_list_del(&event
->list
);
638 lttng_destroy_context(event
->ctx
);
643 * We have exclusive access to our metadata buffer (protected by the
644 * ust_lock), so we can do racy operations such as looking for
645 * remaining space left in packet and write, since mutual exclusion
646 * protects us from concurrent writes.
648 int lttng_metadata_printf(struct ltt_session
*session
,
649 const char *fmt
, ...)
651 struct lttng_ust_lib_ring_buffer_ctx ctx
;
652 struct ltt_channel
*chan
= session
->metadata
;
654 int ret
= 0, waitret
;
655 size_t len
, reserve_len
, pos
;
658 WARN_ON_ONCE(!CMM_ACCESS_ONCE(session
->active
));
661 ret
= vasprintf(&str
, fmt
, ap
);
669 for (pos
= 0; pos
< len
; pos
+= reserve_len
) {
670 reserve_len
= min_t(size_t,
671 chan
->ops
->packet_avail_size(chan
->chan
, chan
->handle
),
673 lib_ring_buffer_ctx_init(&ctx
, chan
->chan
, NULL
, reserve_len
,
674 sizeof(char), -1, chan
->handle
);
676 * We don't care about metadata buffer's records lost
677 * count, because we always retry here. Report error if
678 * we need to bail out after timeout or being
681 waitret
= wait_cond_interruptible_timeout(
683 ret
= chan
->ops
->event_reserve(&ctx
, 0);
684 ret
!= -ENOBUFS
|| !ret
;
686 LTTNG_METADATA_TIMEOUT_MSEC
);
687 if (waitret
== -ETIMEDOUT
|| waitret
== -EINTR
|| ret
) {
688 DBG("LTTng: Failure to write metadata to buffers (%s)\n",
689 waitret
== -EINTR
? "interrupted" :
690 (ret
== -ENOBUFS
? "timeout" : "I/O error"));
691 if (waitret
== -EINTR
)
695 chan
->ops
->event_write(&ctx
, &str
[pos
], reserve_len
);
696 chan
->ops
->event_commit(&ctx
);
704 int _ltt_field_statedump(struct ltt_session
*session
,
705 const struct lttng_event_field
*field
)
709 switch (field
->type
.atype
) {
711 ret
= lttng_metadata_printf(session
,
712 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n",
713 field
->type
.u
.basic
.integer
.size
,
714 field
->type
.u
.basic
.integer
.alignment
,
715 field
->type
.u
.basic
.integer
.signedness
,
716 (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_none
)
718 : (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
721 field
->type
.u
.basic
.integer
.base
,
722 #if (BYTE_ORDER == BIG_ENDIAN)
723 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
725 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
730 ret
= lttng_metadata_printf(session
,
731 " floating_point { exp_dig = %u; mant_dig = %u; align = %u;%s } _%s;\n",
732 field
->type
.u
.basic
._float
.exp_dig
,
733 field
->type
.u
.basic
._float
.mant_dig
,
734 field
->type
.u
.basic
._float
.alignment
,
735 #if (BYTE_ORDER == BIG_ENDIAN)
736 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
738 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
743 ret
= lttng_metadata_printf(session
,
745 field
->type
.u
.basic
.enumeration
.name
,
750 const struct lttng_basic_type
*elem_type
;
752 elem_type
= &field
->type
.u
.array
.elem_type
;
753 ret
= lttng_metadata_printf(session
,
754 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n",
755 elem_type
->u
.basic
.integer
.size
,
756 elem_type
->u
.basic
.integer
.alignment
,
757 elem_type
->u
.basic
.integer
.signedness
,
758 (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
760 : (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
763 elem_type
->u
.basic
.integer
.base
,
764 #if (BYTE_ORDER == BIG_ENDIAN)
765 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
767 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
769 field
->name
, field
->type
.u
.array
.length
);
774 const struct lttng_basic_type
*elem_type
;
775 const struct lttng_basic_type
*length_type
;
777 elem_type
= &field
->type
.u
.sequence
.elem_type
;
778 length_type
= &field
->type
.u
.sequence
.length_type
;
779 ret
= lttng_metadata_printf(session
,
780 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n",
781 length_type
->u
.basic
.integer
.size
,
782 (unsigned int) length_type
->u
.basic
.integer
.alignment
,
783 length_type
->u
.basic
.integer
.signedness
,
784 (length_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
786 : ((length_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
789 length_type
->u
.basic
.integer
.base
,
790 #if (BYTE_ORDER == BIG_ENDIAN)
791 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
793 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
799 ret
= lttng_metadata_printf(session
,
800 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n",
801 elem_type
->u
.basic
.integer
.size
,
802 (unsigned int) elem_type
->u
.basic
.integer
.alignment
,
803 elem_type
->u
.basic
.integer
.signedness
,
804 (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
806 : ((elem_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
809 elem_type
->u
.basic
.integer
.base
,
810 #if (BYTE_ORDER == BIG_ENDIAN)
811 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
813 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
821 /* Default encoding is UTF8 */
822 ret
= lttng_metadata_printf(session
,
824 field
->type
.u
.basic
.string
.encoding
== lttng_encode_ASCII
?
825 " { encoding = ASCII; }" : "",
836 int _ltt_context_metadata_statedump(struct ltt_session
*session
,
837 struct lttng_ctx
*ctx
)
844 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
845 const struct lttng_ctx_field
*field
= &ctx
->fields
[i
];
847 ret
= _ltt_field_statedump(session
, &field
->event_field
);
855 int _ltt_fields_metadata_statedump(struct ltt_session
*session
,
856 struct ltt_event
*event
)
858 const struct lttng_event_desc
*desc
= event
->desc
;
862 for (i
= 0; i
< desc
->nr_fields
; i
++) {
863 const struct lttng_event_field
*field
= &desc
->fields
[i
];
865 ret
= _ltt_field_statedump(session
, field
);
873 int _ltt_event_metadata_statedump(struct ltt_session
*session
,
874 struct ltt_channel
*chan
,
875 struct ltt_event
*event
)
879 if (event
->metadata_dumped
|| !CMM_ACCESS_ONCE(session
->active
))
881 if (chan
== session
->metadata
)
884 * Don't print events for which probe load is pending.
889 ret
= lttng_metadata_printf(session
,
893 " stream_id = %u;\n",
900 if (event
->desc
->loglevel
) {
901 ret
= lttng_metadata_printf(session
,
903 *(*event
->desc
->loglevel
));
909 ret
= lttng_metadata_printf(session
,
910 " context := struct {\n");
914 ret
= _ltt_context_metadata_statedump(session
, event
->ctx
);
918 ret
= lttng_metadata_printf(session
,
924 ret
= lttng_metadata_printf(session
,
925 " fields := struct {\n"
930 ret
= _ltt_fields_metadata_statedump(session
, event
);
935 * LTTng space reservation can only reserve multiples of the
938 ret
= lttng_metadata_printf(session
,
944 event
->metadata_dumped
= 1;
951 int _ltt_channel_metadata_statedump(struct ltt_session
*session
,
952 struct ltt_channel
*chan
)
956 if (chan
->metadata_dumped
|| !CMM_ACCESS_ONCE(session
->active
))
958 if (chan
== session
->metadata
)
961 WARN_ON_ONCE(!chan
->header_type
);
962 ret
= lttng_metadata_printf(session
,
965 " event.header := %s;\n"
966 " packet.context := struct packet_context;\n",
968 chan
->header_type
== 1 ? "struct event_header_compact" :
969 "struct event_header_large");
974 ret
= lttng_metadata_printf(session
,
975 " event.context := struct {\n");
979 ret
= _ltt_context_metadata_statedump(session
, chan
->ctx
);
983 ret
= lttng_metadata_printf(session
,
989 ret
= lttng_metadata_printf(session
,
992 chan
->metadata_dumped
= 1;
998 int _ltt_stream_packet_context_declare(struct ltt_session
*session
)
1000 return lttng_metadata_printf(session
,
1001 "struct packet_context {\n"
1002 " uint64_clock_monotonic_t timestamp_begin;\n"
1003 " uint64_clock_monotonic_t timestamp_end;\n"
1004 " uint32_t events_discarded;\n"
1005 " uint32_t content_size;\n"
1006 " uint32_t packet_size;\n"
1007 " uint32_t cpu_id;\n"
1014 * id: range: 0 - 30.
1015 * id 31 is reserved to indicate an extended header.
1018 * id: range: 0 - 65534.
1019 * id 65535 is reserved to indicate an extended header.
1022 int _ltt_event_header_declare(struct ltt_session
*session
)
1024 return lttng_metadata_printf(session
,
1025 "struct event_header_compact {\n"
1026 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
1029 " uint27_clock_monotonic_t timestamp;\n"
1033 " uint64_clock_monotonic_t timestamp;\n"
1038 "struct event_header_large {\n"
1039 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
1042 " uint32_clock_monotonic_t timestamp;\n"
1046 " uint64_clock_monotonic_t timestamp;\n"
1050 lttng_alignof(uint32_t) * CHAR_BIT
,
1051 lttng_alignof(uint16_t) * CHAR_BIT
1056 * Approximation of NTP time of day to clock monotonic correlation,
1057 * taken at start of trace.
1058 * Yes, this is only an approximation. Yes, we can (and will) do better
1059 * in future versions.
1062 uint64_t measure_clock_offset(void)
1064 uint64_t offset
, monotonic
[2], realtime
;
1065 struct timespec rts
= { 0, 0 };
1068 monotonic
[0] = trace_clock_read64();
1069 ret
= clock_gettime(CLOCK_REALTIME
, &rts
);
1072 monotonic
[1] = trace_clock_read64();
1073 offset
= (monotonic
[0] + monotonic
[1]) >> 1;
1074 realtime
= rts
.tv_sec
* 1000000000ULL;
1075 realtime
+= rts
.tv_nsec
;
1076 offset
= realtime
- offset
;
1081 * Output metadata into this session's metadata buffers.
1084 int _ltt_session_metadata_statedump(struct ltt_session
*session
)
1086 unsigned char *uuid_c
= session
->uuid
;
1087 char uuid_s
[37], clock_uuid_s
[CLOCK_UUID_LEN
];
1088 struct ltt_channel
*chan
;
1089 struct ltt_event
*event
;
1092 if (!CMM_ACCESS_ONCE(session
->active
))
1094 if (session
->metadata_dumped
)
1096 if (!session
->metadata
) {
1097 DBG("LTTng: attempt to start tracing, but metadata channel is not found. Operation abort.\n");
1101 snprintf(uuid_s
, sizeof(uuid_s
),
1102 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
1103 uuid_c
[0], uuid_c
[1], uuid_c
[2], uuid_c
[3],
1104 uuid_c
[4], uuid_c
[5], uuid_c
[6], uuid_c
[7],
1105 uuid_c
[8], uuid_c
[9], uuid_c
[10], uuid_c
[11],
1106 uuid_c
[12], uuid_c
[13], uuid_c
[14], uuid_c
[15]);
1108 ret
= lttng_metadata_printf(session
,
1109 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
1110 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
1111 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
1112 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
1113 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
1114 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
1120 " byte_order = %s;\n"
1121 " packet.header := struct {\n"
1122 " uint32_t magic;\n"
1123 " uint8_t uuid[16];\n"
1124 " uint32_t stream_id;\n"
1127 lttng_alignof(uint8_t) * CHAR_BIT
,
1128 lttng_alignof(uint16_t) * CHAR_BIT
,
1129 lttng_alignof(uint32_t) * CHAR_BIT
,
1130 lttng_alignof(uint64_t) * CHAR_BIT
,
1134 #if (BYTE_ORDER == BIG_ENDIAN)
1143 ret
= lttng_metadata_printf(session
,
1151 if (!trace_clock_uuid(clock_uuid_s
)) {
1152 ret
= lttng_metadata_printf(session
,
1153 " uuid = \"%s\";\n",
1160 ret
= lttng_metadata_printf(session
,
1161 " description = \"Monotonic Clock\";\n"
1162 " freq = %" PRIu64
"; /* Frequency, in Hz */\n"
1163 " /* clock value offset from Epoch is: offset * (1/freq) */\n"
1164 " offset = %" PRIu64
";\n"
1167 measure_clock_offset()
1172 ret
= lttng_metadata_printf(session
,
1173 "typealias integer {\n"
1174 " size = 27; align = 1; signed = false;\n"
1175 " map = clock.monotonic.value;\n"
1176 "} := uint27_clock_monotonic_t;\n"
1178 "typealias integer {\n"
1179 " size = 32; align = %u; signed = false;\n"
1180 " map = clock.monotonic.value;\n"
1181 "} := uint32_clock_monotonic_t;\n"
1183 "typealias integer {\n"
1184 " size = 64; align = %u; signed = false;\n"
1185 " map = clock.monotonic.value;\n"
1186 "} := uint64_clock_monotonic_t;\n\n",
1187 lttng_alignof(uint32_t) * CHAR_BIT
,
1188 lttng_alignof(uint64_t) * CHAR_BIT
1193 ret
= _ltt_stream_packet_context_declare(session
);
1197 ret
= _ltt_event_header_declare(session
);
1202 cds_list_for_each_entry(chan
, &session
->chan
, list
) {
1203 ret
= _ltt_channel_metadata_statedump(session
, chan
);
1208 cds_list_for_each_entry(event
, &session
->events
, list
) {
1209 ret
= _ltt_event_metadata_statedump(session
, event
->chan
, event
);
1213 session
->metadata_dumped
= 1;
1218 void lttng_ust_events_exit(void)
1220 struct ltt_session
*session
, *tmpsession
;
1222 cds_list_for_each_entry_safe(session
, tmpsession
, &sessions
, list
)
1223 ltt_session_destroy(session
);
1229 int wildcard_same_loglevel(struct wildcard_entry
*e
,
1230 enum lttng_ust_loglevel_type loglevel_type
,
1233 if (e
->loglevel_type
== loglevel_type
&& e
->loglevel
== loglevel
)
1241 int wildcard_is_within(struct wildcard_entry
*e
,
1242 enum lttng_ust_loglevel_type loglevel_type
,
1245 if (e
->loglevel_type
== LTTNG_UST_LOGLEVEL_ALL
1246 || e
->loglevel
== -1)
1248 switch (e
->loglevel_type
) {
1249 case LTTNG_UST_LOGLEVEL_RANGE
:
1250 switch (loglevel_type
) {
1251 case LTTNG_UST_LOGLEVEL_RANGE
:
1252 if (e
->loglevel
>= loglevel
)
1256 case LTTNG_UST_LOGLEVEL_SINGLE
:
1257 if (e
->loglevel
<= 0 && loglevel
== 0)
1262 case LTTNG_UST_LOGLEVEL_SINGLE
:
1263 switch (loglevel_type
) {
1264 case LTTNG_UST_LOGLEVEL_RANGE
:
1269 case LTTNG_UST_LOGLEVEL_SINGLE
:
1270 if (e
->loglevel
== loglevel
)
1280 * Add the wildcard to the wildcard list. Must be called with
1284 struct session_wildcard
*add_wildcard(struct ltt_channel
*chan
,
1285 struct lttng_ust_event
*event_param
)
1287 struct wildcard_entry
*e
;
1288 struct session_wildcard
*sw
;
1289 size_t name_len
= strlen(event_param
->name
) + 1;
1293 * Try to find global wildcard entry. Given that this is shared
1294 * across all sessions, we need to check for exact loglevel
1295 * match, not just whether contained within the existing ones.
1297 cds_list_for_each_entry(e
, &wildcard_list
, list
) {
1298 if (!strncmp(event_param
->name
, e
->name
,
1299 LTTNG_UST_SYM_NAME_LEN
- 1)) {
1300 if (wildcard_same_loglevel(e
,
1301 event_param
->loglevel_type
,
1302 event_param
->loglevel
)) {
1311 * Create global wildcard entry if not found. Using
1312 * zmalloc here to allocate a variable length element.
1313 * Could cause some memory fragmentation if overused.
1315 e
= zmalloc(sizeof(struct wildcard_entry
) + name_len
);
1317 return ERR_PTR(-ENOMEM
);
1318 memcpy(&e
->name
[0], event_param
->name
, name_len
);
1319 cds_list_add(&e
->list
, &wildcard_list
);
1320 CDS_INIT_LIST_HEAD(&e
->session_list
);
1323 /* session wildcard */
1324 cds_list_for_each_entry(sw
, &e
->session_list
, session_list
) {
1325 if (chan
== sw
->chan
) {
1326 DBG("wildcard %s busy for this channel",
1328 return ERR_PTR(-EEXIST
); /* Already there */
1331 sw
= zmalloc(sizeof(struct session_wildcard
));
1333 return ERR_PTR(-ENOMEM
);
1336 memcpy(&sw
->event_param
, event_param
, sizeof(sw
->event_param
));
1337 sw
->event_param
.instrumentation
= LTTNG_UST_TRACEPOINT
;
1338 sw
->event_param
.loglevel_type
= event_param
->loglevel_type
;
1339 sw
->event_param
.loglevel
= event_param
->loglevel
;
1340 CDS_INIT_LIST_HEAD(&sw
->events
);
1341 cds_list_add(&sw
->list
, &chan
->session
->wildcards
);
1342 cds_list_add(&sw
->session_list
, &e
->session_list
);
1344 ltt_probes_create_wildcard_events(e
, sw
);
1349 * Remove the wildcard from the wildcard list. Must be called with
1350 * ust_lock held. Only called at session teardown.
1353 void _remove_wildcard(struct session_wildcard
*wildcard
)
1355 struct ltt_event
*ev
, *tmp
;
1358 * Just remove the events owned (for enable/disable) by this
1359 * wildcard from the list. The session teardown will take care
1360 * of freeing the event memory.
1362 cds_list_for_each_entry_safe(ev
, tmp
, &wildcard
->events
,
1364 cds_list_del(&ev
->wildcard_list
);
1366 cds_list_del(&wildcard
->session_list
);
1367 cds_list_del(&wildcard
->list
);
1368 if (cds_list_empty(&wildcard
->entry
->session_list
)) {
1369 cds_list_del(&wildcard
->entry
->list
);
1370 free(wildcard
->entry
);
1375 int ltt_wildcard_create(struct ltt_channel
*chan
,
1376 struct lttng_ust_event
*event_param
,
1377 struct session_wildcard
**_sw
)
1379 struct session_wildcard
*sw
;
1381 sw
= add_wildcard(chan
, event_param
);
1382 if (!sw
|| IS_ERR(sw
)) {
1390 void _ltt_wildcard_destroy(struct session_wildcard
*sw
)
1392 _remove_wildcard(sw
);
1395 int ltt_wildcard_enable(struct session_wildcard
*wildcard
)
1397 struct ltt_event
*ev
;
1400 if (wildcard
->enabled
)
1402 cds_list_for_each_entry(ev
, &wildcard
->events
, wildcard_list
) {
1403 ret
= ltt_event_enable(ev
);
1405 DBG("Error: enable error.\n");
1409 wildcard
->enabled
= 1;
1413 int ltt_wildcard_disable(struct session_wildcard
*wildcard
)
1415 struct ltt_event
*ev
;
1418 if (!wildcard
->enabled
)
1420 cds_list_for_each_entry(ev
, &wildcard
->events
, wildcard_list
) {
1421 ret
= ltt_event_disable(ev
);
1423 DBG("Error: disable error.\n");
1427 wildcard
->enabled
= 0;