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.
13 #include <urcu/list.h>
14 #include <urcu/hlist.h>
16 #include <uuid/uuid.h>
24 #include <lttng/ust-endian.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>
40 #include "tracepoint-internal.h"
41 #include "ltt-tracer.h"
42 #include "ltt-tracer-core.h"
44 #include "../libringbuffer/shm.h"
48 * The sessions mutex is the centralized mutex across UST tracing
49 * control and probe registration. All operations within this file are
50 * called by the communication thread, under ust_lock protection.
52 static pthread_mutex_t sessions_mutex
= PTHREAD_MUTEX_INITIALIZER
;
56 pthread_mutex_lock(&sessions_mutex
);
61 pthread_mutex_unlock(&sessions_mutex
);
64 static CDS_LIST_HEAD(sessions
);
67 * Wildcard list, containing the active wildcards.
68 * Protected by ust lock.
70 static CDS_LIST_HEAD(wildcard_list
);
73 * Pending probes hash table, containing the registered ltt events for
74 * which tracepoint probes are still missing. Protected by the sessions
77 #define PENDING_PROBE_HASH_BITS 6
78 #define PENDING_PROBE_HASH_SIZE (1 << PENDING_PROBE_HASH_BITS)
79 static struct cds_hlist_head pending_probe_table
[PENDING_PROBE_HASH_SIZE
];
81 struct ust_pending_probe
{
82 struct ltt_event
*event
;
83 struct cds_hlist_node node
;
84 enum lttng_ust_loglevel_type loglevel_type
;
89 static void _ltt_event_destroy(struct ltt_event
*event
);
90 static void _ltt_wildcard_destroy(struct session_wildcard
*sw
);
91 static void _ltt_channel_destroy(struct ltt_channel
*chan
);
92 static int _ltt_event_unregister(struct ltt_event
*event
);
94 int _ltt_event_metadata_statedump(struct ltt_session
*session
,
95 struct ltt_channel
*chan
,
96 struct ltt_event
*event
);
98 int _ltt_session_metadata_statedump(struct ltt_session
*session
);
100 int ltt_loglevel_match(const struct lttng_event_desc
*desc
,
101 enum lttng_ust_loglevel_type req_type
,
106 if (req_type
== LTTNG_UST_LOGLEVEL_ALL
)
109 ev_loglevel
= TRACE_DEFAULT
;
111 ev_loglevel
= *(*desc
->loglevel
);
113 case LTTNG_UST_LOGLEVEL_RANGE
:
114 if (ev_loglevel
<= req_loglevel
|| req_loglevel
== -1)
118 case LTTNG_UST_LOGLEVEL_SINGLE
:
119 if (ev_loglevel
== req_loglevel
|| req_loglevel
== -1)
123 case LTTNG_UST_LOGLEVEL_ALL
:
130 * Return wildcard for a given event name if the event name match the
131 * one of the wildcards.
132 * Must be called with ust lock held.
133 * Returns NULL if not present.
136 struct wildcard_entry
*match_wildcard(const struct lttng_event_desc
*desc
)
138 struct wildcard_entry
*e
;
140 cds_list_for_each_entry(e
, &wildcard_list
, list
) {
141 /* If only contain '*' */
142 if (strlen(e
->name
) == 1)
144 /* Compare excluding final '*' */
145 if (!strncmp(desc
->name
, e
->name
, strlen(e
->name
) - 1))
147 continue; /* goto next, no match */
149 if (ltt_loglevel_match(desc
,
154 /* no match, loop to next */
160 * called at event creation if probe is missing.
161 * called with session mutex held.
164 int add_pending_probe(struct ltt_event
*event
, const char *name
,
165 enum lttng_ust_loglevel_type loglevel_type
,
168 struct cds_hlist_head
*head
;
169 struct ust_pending_probe
*e
;
170 size_t name_len
= strlen(name
);
173 if (name_len
> LTTNG_UST_SYM_NAME_LEN
- 1) {
174 WARN("Truncating tracepoint name %s which exceeds size limits of %u chars", name
, LTTNG_UST_SYM_NAME_LEN
- 1);
175 name_len
= LTTNG_UST_SYM_NAME_LEN
- 1;
177 hash
= jhash(name
, name_len
, 0);
178 head
= &pending_probe_table
[hash
& (PENDING_PROBE_HASH_SIZE
- 1)];
179 e
= zmalloc(sizeof(struct ust_pending_probe
) + name_len
);
182 memcpy(&e
->name
[0], name
, name_len
+ 1);
183 e
->name
[name_len
] = '\0';
184 e
->loglevel_type
= loglevel_type
;
185 e
->loglevel
= loglevel
;
186 cds_hlist_add_head(&e
->node
, head
);
188 event
->pending_probe
= e
;
193 * remove a pending probe. called when at event teardown and when an
194 * event is fixed (probe is loaded).
195 * called with session mutex held.
198 void remove_pending_probe(struct ust_pending_probe
*e
)
202 cds_hlist_del(&e
->node
);
207 * Called at library load: connect the probe on the events pending on
209 * called with session mutex held.
211 int pending_probe_fix_events(const struct lttng_event_desc
*desc
)
213 struct cds_hlist_head
*head
;
214 struct cds_hlist_node
*node
, *p
;
215 struct ust_pending_probe
*e
;
216 const char *name
= desc
->name
;
218 struct lttng_ust_event event_param
;
219 size_t name_len
= strlen(name
);
224 struct wildcard_entry
*wildcard
;
226 wildcard
= match_wildcard(desc
);
227 if (strcmp(desc
->name
, "lttng_ust:metadata") && wildcard
) {
228 struct session_wildcard
*sw
;
230 cds_list_for_each_entry(sw
, &wildcard
->session_list
,
232 struct ltt_event
*ev
;
235 memcpy(&event_param
, &sw
->event_param
,
236 sizeof(event_param
));
237 memcpy(event_param
.name
,
239 sizeof(event_param
.name
));
241 ret
= ltt_event_create(sw
->chan
,
245 DBG("Error creating event");
248 cds_list_add(&ev
->wildcard_list
,
254 if (name_len
> LTTNG_UST_SYM_NAME_LEN
- 1) {
255 WARN("Truncating tracepoint name %s which exceeds size limits of %u chars", name
, LTTNG_UST_SYM_NAME_LEN
- 1);
256 name_len
= LTTNG_UST_SYM_NAME_LEN
- 1;
258 hash
= jhash(name
, name_len
, 0);
259 head
= &pending_probe_table
[hash
& (PENDING_PROBE_HASH_SIZE
- 1)];
260 cds_hlist_for_each_entry_safe(e
, node
, p
, head
, node
) {
261 struct ltt_event
*event
;
262 struct ltt_channel
*chan
;
264 if (!ltt_loglevel_match(desc
,
269 if (strncmp(name
, e
->name
, LTTNG_UST_SYM_NAME_LEN
- 1)) {
274 assert(!event
->desc
);
276 event
->pending_probe
= NULL
;
277 remove_pending_probe(e
);
278 ret
|= __tracepoint_probe_register(name
,
279 event
->desc
->probe_callback
,
283 event
->id
= chan
->free_event_id
++;
284 ret
|= _ltt_event_metadata_statedump(chan
->session
, chan
,
290 void synchronize_trace(void)
295 struct ltt_session
*ltt_session_create(void)
297 struct ltt_session
*session
;
299 session
= zmalloc(sizeof(struct ltt_session
));
302 CDS_INIT_LIST_HEAD(&session
->chan
);
303 CDS_INIT_LIST_HEAD(&session
->events
);
304 CDS_INIT_LIST_HEAD(&session
->wildcards
);
305 uuid_generate(session
->uuid
);
306 cds_list_add(&session
->list
, &sessions
);
310 void ltt_session_destroy(struct ltt_session
*session
)
312 struct ltt_channel
*chan
, *tmpchan
;
313 struct ltt_event
*event
, *tmpevent
;
314 struct session_wildcard
*wildcard
, *tmpwildcard
;
317 CMM_ACCESS_ONCE(session
->active
) = 0;
318 cds_list_for_each_entry(event
, &session
->events
, list
) {
319 ret
= _ltt_event_unregister(event
);
322 synchronize_trace(); /* Wait for in-flight events to complete */
323 cds_list_for_each_entry_safe(wildcard
, tmpwildcard
, &session
->wildcards
, list
)
324 _ltt_wildcard_destroy(wildcard
);
325 cds_list_for_each_entry_safe(event
, tmpevent
, &session
->events
, list
)
326 _ltt_event_destroy(event
);
327 cds_list_for_each_entry_safe(chan
, tmpchan
, &session
->chan
, list
)
328 _ltt_channel_destroy(chan
);
329 cds_list_del(&session
->list
);
333 int ltt_session_enable(struct ltt_session
*session
)
336 struct ltt_channel
*chan
;
338 if (session
->active
) {
344 * Snapshot the number of events per channel to know the type of header
347 cds_list_for_each_entry(chan
, &session
->chan
, list
) {
348 if (chan
->header_type
)
349 continue; /* don't change it if session stop/restart */
350 if (chan
->free_event_id
< 31)
351 chan
->header_type
= 1; /* compact */
353 chan
->header_type
= 2; /* large */
356 CMM_ACCESS_ONCE(session
->active
) = 1;
357 CMM_ACCESS_ONCE(session
->been_active
) = 1;
358 ret
= _ltt_session_metadata_statedump(session
);
360 CMM_ACCESS_ONCE(session
->active
) = 0;
365 int ltt_session_disable(struct ltt_session
*session
)
369 if (!session
->active
) {
373 CMM_ACCESS_ONCE(session
->active
) = 0;
378 int ltt_channel_enable(struct ltt_channel
*channel
)
382 if (channel
== channel
->session
->metadata
)
384 old
= uatomic_xchg(&channel
->enabled
, 1);
390 int ltt_channel_disable(struct ltt_channel
*channel
)
394 if (channel
== channel
->session
->metadata
)
396 old
= uatomic_xchg(&channel
->enabled
, 0);
402 int ltt_event_enable(struct ltt_event
*event
)
406 if (event
->chan
== event
->chan
->session
->metadata
)
408 old
= uatomic_xchg(&event
->enabled
, 1);
414 int ltt_event_disable(struct ltt_event
*event
)
418 if (event
->chan
== event
->chan
->session
->metadata
)
420 old
= uatomic_xchg(&event
->enabled
, 0);
426 struct ltt_channel
*ltt_channel_create(struct ltt_session
*session
,
427 const char *transport_name
,
429 size_t subbuf_size
, size_t num_subbuf
,
430 unsigned int switch_timer_interval
,
431 unsigned int read_timer_interval
,
432 int **shm_fd
, int **wait_fd
,
433 uint64_t **memory_map_size
,
434 struct ltt_channel
*chan_priv_init
)
436 struct ltt_channel
*chan
= NULL
;
437 struct ltt_transport
*transport
;
439 if (session
->been_active
)
440 goto active
; /* Refuse to add channel to active session */
441 transport
= ltt_transport_find(transport_name
);
443 DBG("LTTng transport %s not found\n",
447 chan_priv_init
->id
= session
->free_chan_id
++;
448 chan_priv_init
->session
= session
;
450 * Note: the channel creation op already writes into the packet
451 * headers. Therefore the "chan" information used as input
452 * should be already accessible.
454 chan
= transport
->ops
.channel_create("[lttng]", buf_addr
,
455 subbuf_size
, num_subbuf
, switch_timer_interval
,
456 read_timer_interval
, shm_fd
, wait_fd
,
457 memory_map_size
, chan_priv_init
);
461 chan
->ops
= &transport
->ops
;
462 cds_list_add(&chan
->list
, &session
->chan
);
472 * Only used internally at session destruction.
475 void _ltt_channel_destroy(struct ltt_channel
*chan
)
477 cds_list_del(&chan
->list
);
478 lttng_destroy_context(chan
->ctx
);
479 chan
->ops
->channel_destroy(chan
);
483 * Supports event creation while tracing session is active.
485 int ltt_event_create(struct ltt_channel
*chan
,
486 struct lttng_ust_event
*event_param
,
488 struct ltt_event
**_event
)
490 const struct lttng_event_desc
*desc
= NULL
; /* silence gcc */
491 struct ltt_event
*event
;
494 if (chan
->used_event_id
== -1U) {
499 * This is O(n^2) (for each event, the loop is called at event
500 * creation). Might require a hash if we have lots of events.
502 cds_list_for_each_entry(event
, &chan
->session
->events
, list
) {
503 if (event
->desc
&& !strncmp(event
->desc
->name
,
505 LTTNG_UST_SYM_NAME_LEN
- 1)) {
512 * Check if loglevel match. Refuse to connect event if not.
514 if (event_param
->instrumentation
== LTTNG_UST_TRACEPOINT
) {
515 desc
= ltt_event_get(event_param
->name
);
517 if (!ltt_loglevel_match(desc
,
518 event_param
->loglevel_type
,
519 event_param
->loglevel
)) {
521 goto no_loglevel_match
;
525 * If descriptor is not there, it will be added to
529 event
= zmalloc(sizeof(struct ltt_event
));
535 event
->filter
= filter
;
537 * used_event_id counts the maximum number of event IDs that can
538 * register if all probes register.
540 chan
->used_event_id
++;
542 event
->instrumentation
= event_param
->instrumentation
;
543 /* Populate ltt_event structure before tracepoint registration. */
545 switch (event_param
->instrumentation
) {
546 case LTTNG_UST_TRACEPOINT
:
549 ret
= __tracepoint_probe_register(event_param
->name
,
550 event
->desc
->probe_callback
,
554 event
->id
= chan
->free_event_id
++;
557 * If the probe is not present, event->desc stays NULL,
558 * waiting for the probe to register, and the event->id
561 ret
= add_pending_probe(event
, event_param
->name
,
562 event_param
->loglevel_type
,
563 event_param
->loglevel
);
565 goto add_pending_error
;
572 ret
= _ltt_event_metadata_statedump(chan
->session
, chan
, event
);
574 goto statedump_error
;
576 cds_list_add(&event
->list
, &chan
->session
->events
);
582 WARN_ON_ONCE(__tracepoint_probe_unregister(event_param
->name
,
583 event
->desc
->probe_callback
,
585 ltt_event_put(event
->desc
);
598 * Only used internally at session destruction.
600 int _ltt_event_unregister(struct ltt_event
*event
)
604 switch (event
->instrumentation
) {
605 case LTTNG_UST_TRACEPOINT
:
607 ret
= __tracepoint_probe_unregister(event
->desc
->name
,
608 event
->desc
->probe_callback
,
613 remove_pending_probe(event
->pending_probe
);
624 * Only used internally at session destruction.
627 void _ltt_event_destroy(struct ltt_event
*event
)
629 switch (event
->instrumentation
) {
630 case LTTNG_UST_TRACEPOINT
:
632 ltt_event_put(event
->desc
);
638 cds_list_del(&event
->list
);
639 lttng_destroy_context(event
->ctx
);
644 * We have exclusive access to our metadata buffer (protected by the
645 * ust_lock), so we can do racy operations such as looking for
646 * remaining space left in packet and write, since mutual exclusion
647 * protects us from concurrent writes.
649 int lttng_metadata_printf(struct ltt_session
*session
,
650 const char *fmt
, ...)
652 struct lttng_ust_lib_ring_buffer_ctx ctx
;
653 struct ltt_channel
*chan
= session
->metadata
;
655 int ret
= 0, waitret
;
656 size_t len
, reserve_len
, pos
;
659 WARN_ON_ONCE(!CMM_ACCESS_ONCE(session
->active
));
662 ret
= vasprintf(&str
, fmt
, ap
);
670 for (pos
= 0; pos
< len
; pos
+= reserve_len
) {
671 reserve_len
= min_t(size_t,
672 chan
->ops
->packet_avail_size(chan
->chan
, chan
->handle
),
674 lib_ring_buffer_ctx_init(&ctx
, chan
->chan
, NULL
, reserve_len
,
675 sizeof(char), -1, chan
->handle
);
677 * We don't care about metadata buffer's records lost
678 * count, because we always retry here. Report error if
679 * we need to bail out after timeout or being
682 waitret
= wait_cond_interruptible_timeout(
684 ret
= chan
->ops
->event_reserve(&ctx
, 0);
685 ret
!= -ENOBUFS
|| !ret
;
687 LTTNG_METADATA_TIMEOUT_MSEC
);
688 if (waitret
== -ETIMEDOUT
|| waitret
== -EINTR
|| ret
) {
689 DBG("LTTng: Failure to write metadata to buffers (%s)\n",
690 waitret
== -EINTR
? "interrupted" :
691 (ret
== -ENOBUFS
? "timeout" : "I/O error"));
692 if (waitret
== -EINTR
)
696 chan
->ops
->event_write(&ctx
, &str
[pos
], reserve_len
);
697 chan
->ops
->event_commit(&ctx
);
705 int _ltt_field_statedump(struct ltt_session
*session
,
706 const struct lttng_event_field
*field
)
710 switch (field
->type
.atype
) {
712 ret
= lttng_metadata_printf(session
,
713 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n",
714 field
->type
.u
.basic
.integer
.size
,
715 field
->type
.u
.basic
.integer
.alignment
,
716 field
->type
.u
.basic
.integer
.signedness
,
717 (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_none
)
719 : (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
722 field
->type
.u
.basic
.integer
.base
,
723 #if (BYTE_ORDER == BIG_ENDIAN)
724 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
726 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
731 ret
= lttng_metadata_printf(session
,
732 " floating_point { exp_dig = %u; mant_dig = %u; align = %u;%s } _%s;\n",
733 field
->type
.u
.basic
._float
.exp_dig
,
734 field
->type
.u
.basic
._float
.mant_dig
,
735 field
->type
.u
.basic
._float
.alignment
,
736 #if (BYTE_ORDER == BIG_ENDIAN)
737 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
739 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
744 ret
= lttng_metadata_printf(session
,
746 field
->type
.u
.basic
.enumeration
.name
,
751 const struct lttng_basic_type
*elem_type
;
753 elem_type
= &field
->type
.u
.array
.elem_type
;
754 ret
= lttng_metadata_printf(session
,
755 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n",
756 elem_type
->u
.basic
.integer
.size
,
757 elem_type
->u
.basic
.integer
.alignment
,
758 elem_type
->u
.basic
.integer
.signedness
,
759 (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
761 : (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
764 elem_type
->u
.basic
.integer
.base
,
765 #if (BYTE_ORDER == BIG_ENDIAN)
766 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
768 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
770 field
->name
, field
->type
.u
.array
.length
);
775 const struct lttng_basic_type
*elem_type
;
776 const struct lttng_basic_type
*length_type
;
778 elem_type
= &field
->type
.u
.sequence
.elem_type
;
779 length_type
= &field
->type
.u
.sequence
.length_type
;
780 ret
= lttng_metadata_printf(session
,
781 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n",
782 length_type
->u
.basic
.integer
.size
,
783 (unsigned int) length_type
->u
.basic
.integer
.alignment
,
784 length_type
->u
.basic
.integer
.signedness
,
785 (length_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
787 : ((length_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
790 length_type
->u
.basic
.integer
.base
,
791 #if (BYTE_ORDER == BIG_ENDIAN)
792 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
794 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
800 ret
= lttng_metadata_printf(session
,
801 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n",
802 elem_type
->u
.basic
.integer
.size
,
803 (unsigned int) elem_type
->u
.basic
.integer
.alignment
,
804 elem_type
->u
.basic
.integer
.signedness
,
805 (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
807 : ((elem_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
810 elem_type
->u
.basic
.integer
.base
,
811 #if (BYTE_ORDER == BIG_ENDIAN)
812 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
814 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
822 /* Default encoding is UTF8 */
823 ret
= lttng_metadata_printf(session
,
825 field
->type
.u
.basic
.string
.encoding
== lttng_encode_ASCII
?
826 " { encoding = ASCII; }" : "",
837 int _ltt_context_metadata_statedump(struct ltt_session
*session
,
838 struct lttng_ctx
*ctx
)
845 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
846 const struct lttng_ctx_field
*field
= &ctx
->fields
[i
];
848 ret
= _ltt_field_statedump(session
, &field
->event_field
);
856 int _ltt_fields_metadata_statedump(struct ltt_session
*session
,
857 struct ltt_event
*event
)
859 const struct lttng_event_desc
*desc
= event
->desc
;
863 for (i
= 0; i
< desc
->nr_fields
; i
++) {
864 const struct lttng_event_field
*field
= &desc
->fields
[i
];
866 ret
= _ltt_field_statedump(session
, field
);
874 int _ltt_event_metadata_statedump(struct ltt_session
*session
,
875 struct ltt_channel
*chan
,
876 struct ltt_event
*event
)
879 int loglevel
= TRACE_DEFAULT
;
881 if (event
->metadata_dumped
|| !CMM_ACCESS_ONCE(session
->active
))
883 if (chan
== session
->metadata
)
886 * Don't print events for which probe load is pending.
891 ret
= lttng_metadata_printf(session
,
895 " stream_id = %u;\n",
902 if (event
->desc
->loglevel
)
903 loglevel
= *(*event
->desc
->loglevel
);
905 ret
= lttng_metadata_printf(session
,
912 ret
= lttng_metadata_printf(session
,
913 " context := struct {\n");
917 ret
= _ltt_context_metadata_statedump(session
, event
->ctx
);
921 ret
= lttng_metadata_printf(session
,
927 ret
= lttng_metadata_printf(session
,
928 " fields := struct {\n"
933 ret
= _ltt_fields_metadata_statedump(session
, event
);
938 * LTTng space reservation can only reserve multiples of the
941 ret
= lttng_metadata_printf(session
,
947 event
->metadata_dumped
= 1;
954 int _ltt_channel_metadata_statedump(struct ltt_session
*session
,
955 struct ltt_channel
*chan
)
959 if (chan
->metadata_dumped
|| !CMM_ACCESS_ONCE(session
->active
))
961 if (chan
== session
->metadata
)
964 WARN_ON_ONCE(!chan
->header_type
);
965 ret
= lttng_metadata_printf(session
,
968 " event.header := %s;\n"
969 " packet.context := struct packet_context;\n",
971 chan
->header_type
== 1 ? "struct event_header_compact" :
972 "struct event_header_large");
977 ret
= lttng_metadata_printf(session
,
978 " event.context := struct {\n");
982 ret
= _ltt_context_metadata_statedump(session
, chan
->ctx
);
986 ret
= lttng_metadata_printf(session
,
992 ret
= lttng_metadata_printf(session
,
995 chan
->metadata_dumped
= 1;
1001 int _ltt_stream_packet_context_declare(struct ltt_session
*session
)
1003 return lttng_metadata_printf(session
,
1004 "struct packet_context {\n"
1005 " uint64_clock_monotonic_t timestamp_begin;\n"
1006 " uint64_clock_monotonic_t timestamp_end;\n"
1007 " uint32_t events_discarded;\n"
1008 " uint32_t content_size;\n"
1009 " uint32_t packet_size;\n"
1010 " uint32_t cpu_id;\n"
1017 * id: range: 0 - 30.
1018 * id 31 is reserved to indicate an extended header.
1021 * id: range: 0 - 65534.
1022 * id 65535 is reserved to indicate an extended header.
1025 int _ltt_event_header_declare(struct ltt_session
*session
)
1027 return lttng_metadata_printf(session
,
1028 "struct event_header_compact {\n"
1029 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
1032 " uint27_clock_monotonic_t timestamp;\n"
1036 " uint64_clock_monotonic_t timestamp;\n"
1041 "struct event_header_large {\n"
1042 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
1045 " uint32_clock_monotonic_t timestamp;\n"
1049 " uint64_clock_monotonic_t timestamp;\n"
1053 lttng_alignof(uint32_t) * CHAR_BIT
,
1054 lttng_alignof(uint16_t) * CHAR_BIT
1059 * Approximation of NTP time of day to clock monotonic correlation,
1060 * taken at start of trace.
1061 * Yes, this is only an approximation. Yes, we can (and will) do better
1062 * in future versions.
1065 uint64_t measure_clock_offset(void)
1067 uint64_t offset
, monotonic
[2], realtime
;
1068 struct timespec rts
= { 0, 0 };
1071 monotonic
[0] = trace_clock_read64();
1072 ret
= clock_gettime(CLOCK_REALTIME
, &rts
);
1075 monotonic
[1] = trace_clock_read64();
1076 offset
= (monotonic
[0] + monotonic
[1]) >> 1;
1077 realtime
= rts
.tv_sec
* 1000000000ULL;
1078 realtime
+= rts
.tv_nsec
;
1079 offset
= realtime
- offset
;
1084 * Output metadata into this session's metadata buffers.
1087 int _ltt_session_metadata_statedump(struct ltt_session
*session
)
1089 unsigned char *uuid_c
= session
->uuid
;
1090 char uuid_s
[37], clock_uuid_s
[CLOCK_UUID_LEN
];
1091 struct ltt_channel
*chan
;
1092 struct ltt_event
*event
;
1094 char procname
[LTTNG_UST_PROCNAME_LEN
] = "";
1096 if (!CMM_ACCESS_ONCE(session
->active
))
1098 if (session
->metadata_dumped
)
1100 if (!session
->metadata
) {
1101 DBG("LTTng: attempt to start tracing, but metadata channel is not found. Operation abort.\n");
1105 snprintf(uuid_s
, sizeof(uuid_s
),
1106 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
1107 uuid_c
[0], uuid_c
[1], uuid_c
[2], uuid_c
[3],
1108 uuid_c
[4], uuid_c
[5], uuid_c
[6], uuid_c
[7],
1109 uuid_c
[8], uuid_c
[9], uuid_c
[10], uuid_c
[11],
1110 uuid_c
[12], uuid_c
[13], uuid_c
[14], uuid_c
[15]);
1112 ret
= lttng_metadata_printf(session
,
1113 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
1114 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
1115 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
1116 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
1117 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
1118 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
1124 " byte_order = %s;\n"
1125 " packet.header := struct {\n"
1126 " uint32_t magic;\n"
1127 " uint8_t uuid[16];\n"
1128 " uint32_t stream_id;\n"
1131 lttng_alignof(uint8_t) * CHAR_BIT
,
1132 lttng_alignof(uint16_t) * CHAR_BIT
,
1133 lttng_alignof(uint32_t) * CHAR_BIT
,
1134 lttng_alignof(uint64_t) * CHAR_BIT
,
1138 #if (BYTE_ORDER == BIG_ENDIAN)
1147 /* ignore error, just use empty string if error. */
1148 lttng_ust_getprocname(procname
);
1149 procname
[LTTNG_UST_PROCNAME_LEN
- 1] = '\0';
1150 ret
= lttng_metadata_printf(session
,
1153 " procname = \"%s\";\n"
1154 " domain = \"ust\";\n"
1155 " tracer_name = \"lttng-ust\";\n"
1156 " tracer_major = %u;\n"
1157 " tracer_minor = %u;\n"
1158 " tracer_patchlevel = %u;\n"
1162 LTTNG_UST_MAJOR_VERSION
,
1163 LTTNG_UST_MINOR_VERSION
,
1164 LTTNG_UST_PATCHLEVEL_VERSION
1169 ret
= lttng_metadata_printf(session
,
1177 if (!trace_clock_uuid(clock_uuid_s
)) {
1178 ret
= lttng_metadata_printf(session
,
1179 " uuid = \"%s\";\n",
1186 ret
= lttng_metadata_printf(session
,
1187 " description = \"Monotonic Clock\";\n"
1188 " freq = %" PRIu64
"; /* Frequency, in Hz */\n"
1189 " /* clock value offset from Epoch is: offset * (1/freq) */\n"
1190 " offset = %" PRIu64
";\n"
1193 measure_clock_offset()
1198 ret
= lttng_metadata_printf(session
,
1199 "typealias integer {\n"
1200 " size = 27; align = 1; signed = false;\n"
1201 " map = clock.monotonic.value;\n"
1202 "} := uint27_clock_monotonic_t;\n"
1204 "typealias integer {\n"
1205 " size = 32; align = %u; signed = false;\n"
1206 " map = clock.monotonic.value;\n"
1207 "} := uint32_clock_monotonic_t;\n"
1209 "typealias integer {\n"
1210 " size = 64; align = %u; signed = false;\n"
1211 " map = clock.monotonic.value;\n"
1212 "} := uint64_clock_monotonic_t;\n\n",
1213 lttng_alignof(uint32_t) * CHAR_BIT
,
1214 lttng_alignof(uint64_t) * CHAR_BIT
1219 ret
= _ltt_stream_packet_context_declare(session
);
1223 ret
= _ltt_event_header_declare(session
);
1228 cds_list_for_each_entry(chan
, &session
->chan
, list
) {
1229 ret
= _ltt_channel_metadata_statedump(session
, chan
);
1234 cds_list_for_each_entry(event
, &session
->events
, list
) {
1235 ret
= _ltt_event_metadata_statedump(session
, event
->chan
, event
);
1239 session
->metadata_dumped
= 1;
1244 void lttng_ust_events_exit(void)
1246 struct ltt_session
*session
, *tmpsession
;
1248 cds_list_for_each_entry_safe(session
, tmpsession
, &sessions
, list
)
1249 ltt_session_destroy(session
);
1255 int wildcard_same_loglevel(struct wildcard_entry
*e
,
1256 enum lttng_ust_loglevel_type loglevel_type
,
1259 if (e
->loglevel_type
== loglevel_type
&& e
->loglevel
== loglevel
)
1267 int wildcard_is_within(struct wildcard_entry
*e
,
1268 enum lttng_ust_loglevel_type loglevel_type
,
1271 if (e
->loglevel_type
== LTTNG_UST_LOGLEVEL_ALL
1272 || e
->loglevel
== -1)
1274 switch (e
->loglevel_type
) {
1275 case LTTNG_UST_LOGLEVEL_RANGE
:
1276 switch (loglevel_type
) {
1277 case LTTNG_UST_LOGLEVEL_RANGE
:
1278 if (e
->loglevel
>= loglevel
)
1282 case LTTNG_UST_LOGLEVEL_SINGLE
:
1283 if (e
->loglevel
<= 0 && loglevel
== 0)
1288 case LTTNG_UST_LOGLEVEL_SINGLE
:
1289 switch (loglevel_type
) {
1290 case LTTNG_UST_LOGLEVEL_RANGE
:
1295 case LTTNG_UST_LOGLEVEL_SINGLE
:
1296 if (e
->loglevel
== loglevel
)
1306 * Add the wildcard to the wildcard list. Must be called with
1310 struct session_wildcard
*add_wildcard(struct ltt_channel
*chan
,
1311 struct lttng_ust_event
*event_param
)
1313 struct wildcard_entry
*e
;
1314 struct session_wildcard
*sw
;
1315 size_t name_len
= strlen(event_param
->name
) + 1;
1319 * Try to find global wildcard entry. Given that this is shared
1320 * across all sessions, we need to check for exact loglevel
1321 * match, not just whether contained within the existing ones.
1323 cds_list_for_each_entry(e
, &wildcard_list
, list
) {
1324 if (!strncmp(event_param
->name
, e
->name
,
1325 LTTNG_UST_SYM_NAME_LEN
- 1)) {
1326 if (wildcard_same_loglevel(e
,
1327 event_param
->loglevel_type
,
1328 event_param
->loglevel
)) {
1337 * Create global wildcard entry if not found. Using
1338 * zmalloc here to allocate a variable length element.
1339 * Could cause some memory fragmentation if overused.
1341 e
= zmalloc(sizeof(struct wildcard_entry
) + name_len
);
1343 return ERR_PTR(-ENOMEM
);
1344 memcpy(&e
->name
[0], event_param
->name
, name_len
);
1345 cds_list_add(&e
->list
, &wildcard_list
);
1346 CDS_INIT_LIST_HEAD(&e
->session_list
);
1349 /* session wildcard */
1350 cds_list_for_each_entry(sw
, &e
->session_list
, session_list
) {
1351 if (chan
== sw
->chan
) {
1352 DBG("wildcard %s busy for this channel",
1354 return ERR_PTR(-EEXIST
); /* Already there */
1357 sw
= zmalloc(sizeof(struct session_wildcard
));
1359 return ERR_PTR(-ENOMEM
);
1362 memcpy(&sw
->event_param
, event_param
, sizeof(sw
->event_param
));
1363 sw
->event_param
.instrumentation
= LTTNG_UST_TRACEPOINT
;
1364 sw
->event_param
.loglevel_type
= event_param
->loglevel_type
;
1365 sw
->event_param
.loglevel
= event_param
->loglevel
;
1366 CDS_INIT_LIST_HEAD(&sw
->events
);
1367 cds_list_add(&sw
->list
, &chan
->session
->wildcards
);
1368 cds_list_add(&sw
->session_list
, &e
->session_list
);
1370 ltt_probes_create_wildcard_events(e
, sw
);
1375 * Remove the wildcard from the wildcard list. Must be called with
1376 * ust_lock held. Only called at session teardown.
1379 void _remove_wildcard(struct session_wildcard
*wildcard
)
1381 struct ltt_event
*ev
, *tmp
;
1384 * Just remove the events owned (for enable/disable) by this
1385 * wildcard from the list. The session teardown will take care
1386 * of freeing the event memory.
1388 cds_list_for_each_entry_safe(ev
, tmp
, &wildcard
->events
,
1390 cds_list_del(&ev
->wildcard_list
);
1392 cds_list_del(&wildcard
->session_list
);
1393 cds_list_del(&wildcard
->list
);
1394 if (cds_list_empty(&wildcard
->entry
->session_list
)) {
1395 cds_list_del(&wildcard
->entry
->list
);
1396 free(wildcard
->entry
);
1401 int ltt_wildcard_create(struct ltt_channel
*chan
,
1402 struct lttng_ust_event
*event_param
,
1403 struct session_wildcard
**_sw
)
1405 struct session_wildcard
*sw
;
1407 sw
= add_wildcard(chan
, event_param
);
1408 if (!sw
|| IS_ERR(sw
)) {
1416 void _ltt_wildcard_destroy(struct session_wildcard
*sw
)
1418 _remove_wildcard(sw
);
1421 int ltt_wildcard_enable(struct session_wildcard
*wildcard
)
1423 struct ltt_event
*ev
;
1426 if (wildcard
->enabled
)
1428 cds_list_for_each_entry(ev
, &wildcard
->events
, wildcard_list
) {
1429 ret
= ltt_event_enable(ev
);
1431 DBG("Error: enable error.\n");
1435 wildcard
->enabled
= 1;
1439 int ltt_wildcard_disable(struct session_wildcard
*wildcard
)
1441 struct ltt_event
*ev
;
1444 if (!wildcard
->enabled
)
1446 cds_list_for_each_entry(ev
, &wildcard
->events
, wildcard_list
) {
1447 ret
= ltt_event_disable(ev
);
1449 DBG("Error: disable error.\n");
1453 wildcard
->enabled
= 0;