2 * ctf-visitor-generate-ir.c
4 * Common Trace Format metadata visitor (generates CTF IR objects).
6 * Based on older ctf-visitor-generate-io-struct.c.
8 * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
9 * Copyright 2015-2016 - Philippe Proulx <philippe.proulx@efficios.com>
11 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 #define BT_LOG_TAG "PLUGIN-CTF-METADATA-IR-VISITOR"
39 #include <babeltrace/assert-internal.h>
43 #include <babeltrace/common-internal.h>
44 #include <babeltrace/compat/uuid-internal.h>
45 #include <babeltrace/endian-internal.h>
46 #include <babeltrace/babeltrace.h>
53 /* Bit value (left shift) */
54 #define _BV(_val) (1 << (_val))
56 /* Bit is set in a set of bits */
57 #define _IS_SET(_set, _mask) (*(_set) & (_mask))
59 /* Set bit in a set of bits */
60 #define _SET(_set, _mask) (*(_set) |= (_mask))
62 /* Bits for verifying existing attributes in various declarations */
64 _CLOCK_NAME_SET
= _BV(0),
65 _CLOCK_UUID_SET
= _BV(1),
66 _CLOCK_FREQ_SET
= _BV(2),
67 _CLOCK_PRECISION_SET
= _BV(3),
68 _CLOCK_OFFSET_S_SET
= _BV(4),
69 _CLOCK_OFFSET_SET
= _BV(5),
70 _CLOCK_ABSOLUTE_SET
= _BV(6),
71 _CLOCK_DESCRIPTION_SET
= _BV(7),
75 _INTEGER_ALIGN_SET
= _BV(0),
76 _INTEGER_SIZE_SET
= _BV(1),
77 _INTEGER_BASE_SET
= _BV(2),
78 _INTEGER_ENCODING_SET
= _BV(3),
79 _INTEGER_BYTE_ORDER_SET
= _BV(4),
80 _INTEGER_SIGNED_SET
= _BV(5),
81 _INTEGER_MAP_SET
= _BV(6),
85 _FLOAT_ALIGN_SET
= _BV(0),
86 _FLOAT_MANT_DIG_SET
= _BV(1),
87 _FLOAT_EXP_DIG_SET
= _BV(2),
88 _FLOAT_BYTE_ORDER_SET
= _BV(3),
92 _STRING_ENCODING_SET
= _BV(0),
96 _TRACE_MINOR_SET
= _BV(0),
97 _TRACE_MAJOR_SET
= _BV(1),
98 _TRACE_BYTE_ORDER_SET
= _BV(2),
99 _TRACE_UUID_SET
= _BV(3),
100 _TRACE_PACKET_HEADER_SET
= _BV(4),
104 _STREAM_ID_SET
= _BV(0),
105 _STREAM_PACKET_CONTEXT_SET
= _BV(1),
106 _STREAM_EVENT_HEADER_SET
= _BV(2),
107 _STREAM_EVENT_CONTEXT_SET
= _BV(3),
111 _EVENT_NAME_SET
= _BV(0),
112 _EVENT_ID_SET
= _BV(1),
113 _EVENT_MODEL_EMF_URI_SET
= _BV(2),
114 _EVENT_STREAM_ID_SET
= _BV(3),
115 _EVENT_LOGLEVEL_SET
= _BV(4),
116 _EVENT_CONTEXT_SET
= _BV(5),
117 _EVENT_FIELDS_SET
= _BV(6),
125 LOGLEVEL_WARNING
= 4,
128 LOGLEVEL_DEBUG_SYSTEM
= 7,
129 LOGLEVEL_DEBUG_PROGRAM
= 8,
130 LOGLEVEL_DEBUG_PROCESS
= 9,
131 LOGLEVEL_DEBUG_MODULE
= 10,
132 LOGLEVEL_DEBUG_UNIT
= 11,
133 LOGLEVEL_DEBUG_FUNCTION
= 12,
134 LOGLEVEL_DEBUG_LINE
= 13,
139 /* Prefixes of type aliases */
140 #define _PREFIX_ALIAS 'a'
141 #define _PREFIX_ENUM 'e'
142 #define _PREFIX_STRUCT 's'
143 #define _PREFIX_VARIANT 'v'
145 /* First entry in a BT list */
146 #define _BT_LIST_FIRST_ENTRY(_ptr, _type, _member) \
147 bt_list_entry((_ptr)->next, _type, _member)
149 #define _BT_FIELD_TYPE_INIT(_name) struct bt_field_type *_name = NULL;
151 #define _BT_LOGE_DUP_ATTR(_node, _attr, _entity) \
152 _BT_LOGE_LINENO((_node)->lineno, \
153 "Duplicate attribute in %s: attr-name=\"%s\"", \
156 #define _BT_LOGE_NODE(_node, _msg, args...) \
157 _BT_LOGE_LINENO((_node)->lineno, _msg, ## args)
159 #define _BT_LOGW_NODE(_node, _msg, args...) \
160 _BT_LOGW_LINENO((_node)->lineno, _msg, ## args)
162 #define _BT_LOGV_NODE(_node, _msg, args...) \
163 _BT_LOGV_LINENO((_node)->lineno, _msg, ## args)
166 * Declaration scope of a visitor context. This represents a TSDL
167 * lexical scope, so that aliases and named structures, variants,
168 * and enumerations may be registered and looked up hierarchically.
170 struct ctx_decl_scope
{
172 * Alias name to field type.
174 * GQuark -> struct bt_field_type *
176 GHashTable
*decl_map
;
178 /* Parent scope; NULL if this is the root declaration scope */
179 struct ctx_decl_scope
*parent_scope
;
183 * Visitor context (private).
186 /* Trace being filled (owned by this) */
187 struct bt_trace
*trace
;
189 /* Current declaration scope (top of the stack) */
190 struct ctx_decl_scope
*current_scope
;
192 /* 1 if trace declaration is visited */
193 int is_trace_visited
;
195 /* 1 if this is an LTTng trace */
198 /* Eventual name suffix of the trace to set */
199 char *trace_name_suffix
;
201 /* Trace attributes */
202 enum bt_byte_order trace_bo
;
203 uint64_t trace_major
;
204 uint64_t trace_minor
;
205 unsigned char trace_uuid
[BABELTRACE_UUID_LEN
];
208 * Stream IDs to stream classes.
210 * int64_t -> struct bt_stream_class *
212 GHashTable
*stream_classes
;
214 /* Config passed by the user */
215 struct ctf_metadata_decoder_config decoder_config
;
221 struct ctf_visitor_generate_ir
{ };
224 * Creates a new declaration scope.
226 * @param par_scope Parent scope (NULL if creating a root scope)
227 * @returns New declaration scope, or NULL on error
230 struct ctx_decl_scope
*ctx_decl_scope_create(struct ctx_decl_scope
*par_scope
)
232 struct ctx_decl_scope
*scope
;
234 scope
= g_new(struct ctx_decl_scope
, 1);
236 BT_LOGE_STR("Failed to allocate one declaration scope.");
240 scope
->decl_map
= g_hash_table_new_full(g_direct_hash
, g_direct_equal
,
241 NULL
, (GDestroyNotify
) bt_put
);
242 scope
->parent_scope
= par_scope
;
249 * Destroys a declaration scope.
251 * This function does not destroy the parent scope.
253 * @param scope Scope to destroy
256 void ctx_decl_scope_destroy(struct ctx_decl_scope
*scope
)
262 g_hash_table_destroy(scope
->decl_map
);
270 * Returns the GQuark of a prefixed alias.
272 * @param prefix Prefix character
274 * @returns Associated GQuark, or 0 on error
277 GQuark
get_prefixed_named_quark(char prefix
, const char *name
)
283 /* Prefix character + original string + '\0' */
284 char *prname
= g_new(char, strlen(name
) + 2);
286 BT_LOGE_STR("Failed to allocate a string.");
290 sprintf(prname
, "%c%s", prefix
, name
);
291 qname
= g_quark_from_string(prname
);
299 * Looks up a prefixed type alias within a declaration scope.
301 * @param scope Declaration scope
302 * @param prefix Prefix character
303 * @param name Alias name
304 * @param level Number of levels to dig (-1 means infinite)
305 * @returns Declaration, or NULL if not found
308 struct bt_field_type
*ctx_decl_scope_lookup_prefix_alias(
309 struct ctx_decl_scope
*scope
, char prefix
,
310 const char *name
, int levels
)
314 _BT_FIELD_TYPE_INIT(decl
);
315 struct ctx_decl_scope
*cur_scope
= scope
;
319 qname
= get_prefixed_named_quark(prefix
, name
);
328 while (cur_scope
&& cur_levels
< levels
) {
329 decl
= g_hash_table_lookup(cur_scope
->decl_map
,
330 (gconstpointer
) GUINT_TO_POINTER(qname
));
332 /* Caller's reference */
337 cur_scope
= cur_scope
->parent_scope
;
348 * Looks up a type alias within a declaration scope.
350 * @param scope Declaration scope
351 * @param name Alias name
352 * @param level Number of levels to dig (-1 means infinite)
353 * @returns Declaration, or NULL if not found
356 struct bt_field_type
*ctx_decl_scope_lookup_alias(
357 struct ctx_decl_scope
*scope
, const char *name
, int levels
)
359 return ctx_decl_scope_lookup_prefix_alias(scope
, _PREFIX_ALIAS
,
364 * Looks up an enumeration within a declaration scope.
366 * @param scope Declaration scope
367 * @param name Enumeration name
368 * @param level Number of levels to dig (-1 means infinite)
369 * @returns Declaration, or NULL if not found
372 struct bt_field_type
*ctx_decl_scope_lookup_enum(
373 struct ctx_decl_scope
*scope
, const char *name
, int levels
)
375 return ctx_decl_scope_lookup_prefix_alias(scope
, _PREFIX_ENUM
,
380 * Looks up a structure within a declaration scope.
382 * @param scope Declaration scope
383 * @param name Structure name
384 * @param level Number of levels to dig (-1 means infinite)
385 * @returns Declaration, or NULL if not found
388 struct bt_field_type
*ctx_decl_scope_lookup_struct(
389 struct ctx_decl_scope
*scope
, const char *name
, int levels
)
391 return ctx_decl_scope_lookup_prefix_alias(scope
, _PREFIX_STRUCT
,
396 * Looks up a variant within a declaration scope.
398 * @param scope Declaration scope
399 * @param name Variant name
400 * @param level Number of levels to dig (-1 means infinite)
401 * @returns Declaration, or NULL if not found
404 struct bt_field_type
*ctx_decl_scope_lookup_variant(
405 struct ctx_decl_scope
*scope
, const char *name
, int levels
)
407 return ctx_decl_scope_lookup_prefix_alias(scope
, _PREFIX_VARIANT
,
412 * Registers a prefixed type alias within a declaration scope.
414 * @param scope Declaration scope
415 * @param prefix Prefix character
416 * @param name Alias name (non-NULL)
417 * @param decl Declaration to register
418 * @returns 0 if registration went okay, negative value otherwise
421 int ctx_decl_scope_register_prefix_alias(struct ctx_decl_scope
*scope
,
422 char prefix
, const char *name
, struct bt_field_type
*decl
)
426 _BT_FIELD_TYPE_INIT(edecl
);
431 qname
= get_prefixed_named_quark(prefix
, name
);
437 /* Make sure alias does not exist in local scope */
438 edecl
= ctx_decl_scope_lookup_prefix_alias(scope
, prefix
, name
, 1);
445 g_hash_table_insert(scope
->decl_map
,
446 GUINT_TO_POINTER(qname
), decl
);
448 /* Hash table's reference */
458 * Registers a type alias within a declaration scope.
460 * @param scope Declaration scope
461 * @param name Alias name (non-NULL)
462 * @param decl Declaration to register
463 * @returns 0 if registration went okay, negative value otherwise
466 int ctx_decl_scope_register_alias(struct ctx_decl_scope
*scope
,
467 const char *name
, struct bt_field_type
*decl
)
469 return ctx_decl_scope_register_prefix_alias(scope
, _PREFIX_ALIAS
,
474 * Registers an enumeration declaration within a declaration scope.
476 * @param scope Declaration scope
477 * @param name Enumeration name (non-NULL)
478 * @param decl Enumeration declaration to register
479 * @returns 0 if registration went okay, negative value otherwise
482 int ctx_decl_scope_register_enum(struct ctx_decl_scope
*scope
,
483 const char *name
, struct bt_field_type
*decl
)
485 return ctx_decl_scope_register_prefix_alias(scope
, _PREFIX_ENUM
,
490 * Registers a structure declaration within a declaration scope.
492 * @param scope Declaration scope
493 * @param name Structure name (non-NULL)
494 * @param decl Structure declaration to register
495 * @returns 0 if registration went okay, negative value otherwise
498 int ctx_decl_scope_register_struct(struct ctx_decl_scope
*scope
,
499 const char *name
, struct bt_field_type
*decl
)
501 return ctx_decl_scope_register_prefix_alias(scope
, _PREFIX_STRUCT
,
506 * Registers a variant declaration within a declaration scope.
508 * @param scope Declaration scope
509 * @param name Variant name (non-NULL)
510 * @param decl Variant declaration to register
511 * @returns 0 if registration went okay, negative value otherwise
514 int ctx_decl_scope_register_variant(struct ctx_decl_scope
*scope
,
515 const char *name
, struct bt_field_type
*decl
)
517 return ctx_decl_scope_register_prefix_alias(scope
, _PREFIX_VARIANT
,
522 * Destroys a visitor context.
524 * @param ctx Visitor context to destroy
527 void ctx_destroy(struct ctx
*ctx
)
529 struct ctx_decl_scope
*scope
;
531 * Destroy all scopes, from current one to the root scope.
538 scope
= ctx
->current_scope
;
541 struct ctx_decl_scope
*parent_scope
= scope
->parent_scope
;
543 ctx_decl_scope_destroy(scope
);
544 scope
= parent_scope
;
549 if (ctx
->stream_classes
) {
550 g_hash_table_destroy(ctx
->stream_classes
);
553 free(ctx
->trace_name_suffix
);
561 * Creates a new visitor context.
563 * @param trace Associated trace
564 * @returns New visitor context, or NULL on error
567 struct ctx
*ctx_create(struct bt_trace
*trace
,
568 const struct ctf_metadata_decoder_config
*decoder_config
,
569 const char *trace_name_suffix
)
571 struct ctx
*ctx
= NULL
;
572 struct ctx_decl_scope
*scope
= NULL
;
574 BT_ASSERT(decoder_config
);
576 ctx
= g_new0(struct ctx
, 1);
578 BT_LOGE_STR("Failed to allocate one visitor context.");
582 /* Root declaration scope */
583 scope
= ctx_decl_scope_create(NULL
);
585 BT_LOGE_STR("Cannot create declaration scope.");
589 ctx
->stream_classes
= g_hash_table_new_full(g_int64_hash
,
590 g_int64_equal
, g_free
, (GDestroyNotify
) bt_put
);
591 if (!ctx
->stream_classes
) {
592 BT_LOGE_STR("Failed to allocate a GHashTable.");
596 if (trace_name_suffix
) {
597 ctx
->trace_name_suffix
= strdup(trace_name_suffix
);
598 if (!ctx
->trace_name_suffix
) {
599 BT_LOGE_STR("Failed to copy string.");
605 ctx
->current_scope
= scope
;
607 ctx
->trace_bo
= BT_BYTE_ORDER_NATIVE
;
608 ctx
->decoder_config
= *decoder_config
;
613 ctx_decl_scope_destroy(scope
);
618 * Pushes a new declaration scope on top of a visitor context's
619 * declaration scope stack.
621 * @param ctx Visitor context
622 * @returns 0 on success, or a negative value on error
625 int ctx_push_scope(struct ctx
*ctx
)
628 struct ctx_decl_scope
*new_scope
;
631 new_scope
= ctx_decl_scope_create(ctx
->current_scope
);
633 BT_LOGE_STR("Cannot create declaration scope.");
638 ctx
->current_scope
= new_scope
;
645 void ctx_pop_scope(struct ctx
*ctx
)
647 struct ctx_decl_scope
*parent_scope
= NULL
;
651 if (!ctx
->current_scope
) {
655 parent_scope
= ctx
->current_scope
->parent_scope
;
656 ctx_decl_scope_destroy(ctx
->current_scope
);
657 ctx
->current_scope
= parent_scope
;
664 int visit_type_specifier_list(struct ctx
*ctx
, struct ctf_node
*ts_list
,
665 struct bt_field_type
**decl
);
668 char *remove_underscores_from_field_ref(const char *field_ref
)
674 UNDERSCORE_REMOVE_STATE_REMOVE_NEXT_UNDERSCORE
,
675 UNDERSCORE_REMOVE_STATE_DO_NOT_REMOVE_NEXT_UNDERSCORE
,
676 } state
= UNDERSCORE_REMOVE_STATE_REMOVE_NEXT_UNDERSCORE
;
678 BT_ASSERT(field_ref
);
679 ret
= calloc(strlen(field_ref
) + 1, 1);
681 BT_LOGE("Failed to allocate a string: size=%zu",
682 strlen(field_ref
) + 1);
689 while (*in_ch
!= '\0') {
693 /* Remove whitespace */
697 if (state
== UNDERSCORE_REMOVE_STATE_REMOVE_NEXT_UNDERSCORE
) {
699 state
= UNDERSCORE_REMOVE_STATE_DO_NOT_REMOVE_NEXT_UNDERSCORE
;
705 state
= UNDERSCORE_REMOVE_STATE_REMOVE_NEXT_UNDERSCORE
;
708 state
= UNDERSCORE_REMOVE_STATE_DO_NOT_REMOVE_NEXT_UNDERSCORE
;
723 int is_unary_string(struct bt_list_head
*head
)
726 struct ctf_node
*node
;
728 bt_list_for_each_entry(node
, head
, siblings
) {
729 if (node
->type
!= NODE_UNARY_EXPRESSION
) {
733 if (node
->u
.unary_expression
.type
!= UNARY_STRING
) {
742 char *concatenate_unary_strings(struct bt_list_head
*head
)
746 struct ctf_node
*node
;
748 str
= g_string_new(NULL
);
751 bt_list_for_each_entry(node
, head
, siblings
) {
755 node
->type
!= NODE_UNARY_EXPRESSION
||
756 node
->u
.unary_expression
.type
!= UNARY_STRING
||
759 node
->u
.unary_expression
.link
!=
767 switch (node
->u
.unary_expression
.link
) {
769 g_string_append(str
, ".");
771 case UNARY_ARROWLINK
:
772 g_string_append(str
, "->");
774 case UNARY_DOTDOTDOT
:
775 g_string_append(str
, "...");
781 src_string
= node
->u
.unary_expression
.u
.string
;
782 g_string_append(str
, src_string
);
786 /* Destroys the container, returns the underlying string */
787 return g_string_free(str
, FALSE
);
790 /* This always returns NULL */
791 return g_string_free(str
, TRUE
);
795 const char *get_map_clock_name_value(struct bt_list_head
*head
)
798 struct ctf_node
*node
;
799 const char *name
= NULL
;
801 bt_list_for_each_entry(node
, head
, siblings
) {
803 int uexpr_type
= node
->u
.unary_expression
.type
;
804 int uexpr_link
= node
->u
.unary_expression
.link
;
805 int cond
= node
->type
!= NODE_UNARY_EXPRESSION
||
806 uexpr_type
!= UNARY_STRING
||
807 !((uexpr_link
!= UNARY_LINK_UNKNOWN
) ^ (i
== 0));
812 /* Needs to be chained with . */
813 switch (node
->u
.unary_expression
.link
) {
816 case UNARY_ARROWLINK
:
817 case UNARY_DOTDOTDOT
:
823 src_string
= node
->u
.unary_expression
.u
.string
;
827 if (strcmp("clock", src_string
)) {
835 if (strcmp("value", src_string
)) {
840 /* Extra identifier, unknown */
854 int is_unary_unsigned(struct bt_list_head
*head
)
857 struct ctf_node
*node
;
859 bt_list_for_each_entry(node
, head
, siblings
) {
860 if (node
->type
!= NODE_UNARY_EXPRESSION
) {
864 if (node
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
873 int get_unary_unsigned(struct bt_list_head
*head
, uint64_t *value
)
877 struct ctf_node
*node
;
879 if (bt_list_empty(head
)) {
884 bt_list_for_each_entry(node
, head
, siblings
) {
885 int uexpr_type
= node
->u
.unary_expression
.type
;
886 int uexpr_link
= node
->u
.unary_expression
.link
;
887 int cond
= node
->type
!= NODE_UNARY_EXPRESSION
||
888 uexpr_type
!= UNARY_UNSIGNED_CONSTANT
||
889 uexpr_link
!= UNARY_LINK_UNKNOWN
|| i
!= 0;
891 _BT_LOGE_NODE(node
, "Invalid constant unsigned integer.");
896 *value
= node
->u
.unary_expression
.u
.unsigned_constant
;
905 int is_unary_signed(struct bt_list_head
*head
)
908 struct ctf_node
*node
;
910 bt_list_for_each_entry(node
, head
, siblings
) {
911 if (node
->type
!= NODE_UNARY_EXPRESSION
) {
915 if (node
->u
.unary_expression
.type
!= UNARY_SIGNED_CONSTANT
) {
924 int get_unary_signed(struct bt_list_head
*head
, int64_t *value
)
928 struct ctf_node
*node
;
930 bt_list_for_each_entry(node
, head
, siblings
) {
931 int uexpr_type
= node
->u
.unary_expression
.type
;
932 int uexpr_link
= node
->u
.unary_expression
.link
;
933 int cond
= node
->type
!= NODE_UNARY_EXPRESSION
||
934 (uexpr_type
!= UNARY_UNSIGNED_CONSTANT
&&
935 uexpr_type
!= UNARY_SIGNED_CONSTANT
) ||
936 uexpr_link
!= UNARY_LINK_UNKNOWN
|| i
!= 0;
942 switch (uexpr_type
) {
943 case UNARY_UNSIGNED_CONSTANT
:
945 node
->u
.unary_expression
.u
.unsigned_constant
;
947 case UNARY_SIGNED_CONSTANT
:
948 *value
= node
->u
.unary_expression
.u
.signed_constant
;
963 int get_unary_uuid(struct bt_list_head
*head
, unsigned char *uuid
)
967 struct ctf_node
*node
;
969 bt_list_for_each_entry(node
, head
, siblings
) {
970 int uexpr_type
= node
->u
.unary_expression
.type
;
971 int uexpr_link
= node
->u
.unary_expression
.link
;
972 const char *src_string
;
974 if (node
->type
!= NODE_UNARY_EXPRESSION
||
975 uexpr_type
!= UNARY_STRING
||
976 uexpr_link
!= UNARY_LINK_UNKNOWN
||
982 src_string
= node
->u
.unary_expression
.u
.string
;
983 ret
= bt_uuid_parse(src_string
, uuid
);
986 "Cannot parse UUID: uuid=\"%s\"", src_string
);
996 int get_boolean(struct ctf_node
*unary_expr
)
1000 if (unary_expr
->type
!= NODE_UNARY_EXPRESSION
) {
1001 _BT_LOGE_NODE(unary_expr
,
1002 "Expecting unary expression: node-type=%d",
1008 switch (unary_expr
->u
.unary_expression
.type
) {
1009 case UNARY_UNSIGNED_CONSTANT
:
1010 ret
= (unary_expr
->u
.unary_expression
.u
.unsigned_constant
!= 0);
1012 case UNARY_SIGNED_CONSTANT
:
1013 ret
= (unary_expr
->u
.unary_expression
.u
.signed_constant
!= 0);
1017 const char *str
= unary_expr
->u
.unary_expression
.u
.string
;
1019 if (!strcmp(str
, "true") || !strcmp(str
, "TRUE")) {
1021 } else if (!strcmp(str
, "false") || !strcmp(str
, "FALSE")) {
1024 _BT_LOGE_NODE(unary_expr
,
1025 "Unexpected boolean value: value=\"%s\"", str
);
1032 _BT_LOGE_NODE(unary_expr
,
1033 "Unexpected unary expression type: node-type=%d",
1034 unary_expr
->u
.unary_expression
.type
);
1044 enum bt_byte_order
byte_order_from_unary_expr(struct ctf_node
*unary_expr
)
1047 enum bt_byte_order bo
= BT_BYTE_ORDER_UNKNOWN
;
1049 if (unary_expr
->u
.unary_expression
.type
!= UNARY_STRING
) {
1050 _BT_LOGE_NODE(unary_expr
,
1051 "\"byte_order\" attribute: expecting `be`, `le`, `network`, or `native`.");
1055 str
= unary_expr
->u
.unary_expression
.u
.string
;
1057 if (!strcmp(str
, "be") || !strcmp(str
, "network")) {
1058 bo
= BT_BYTE_ORDER_BIG_ENDIAN
;
1059 } else if (!strcmp(str
, "le")) {
1060 bo
= BT_BYTE_ORDER_LITTLE_ENDIAN
;
1061 } else if (!strcmp(str
, "native")) {
1062 bo
= BT_BYTE_ORDER_NATIVE
;
1064 _BT_LOGE_NODE(unary_expr
,
1065 "Unexpected \"byte_order\" attribute value: "
1066 "expecting `be`, `le`, `network`, or `native`: value=\"%s\"",
1076 enum bt_byte_order
get_real_byte_order(struct ctx
*ctx
,
1077 struct ctf_node
*uexpr
)
1079 enum bt_byte_order bo
= byte_order_from_unary_expr(uexpr
);
1081 if (bo
== BT_BYTE_ORDER_NATIVE
) {
1082 bo
= bt_trace_get_native_byte_order(ctx
->trace
);
1089 int is_align_valid(uint64_t align
)
1091 return (align
!= 0) && !(align
& (align
- 1ULL));
1095 int get_type_specifier_name(struct ctx
*ctx
, struct ctf_node
*type_specifier
,
1100 if (type_specifier
->type
!= NODE_TYPE_SPECIFIER
) {
1101 _BT_LOGE_NODE(type_specifier
,
1102 "Unexpected node type: node-type=%d",
1103 type_specifier
->type
);
1108 switch (type_specifier
->u
.type_specifier
.type
) {
1110 g_string_append(str
, "void");
1113 g_string_append(str
, "char");
1115 case TYPESPEC_SHORT
:
1116 g_string_append(str
, "short");
1119 g_string_append(str
, "int");
1122 g_string_append(str
, "long");
1124 case TYPESPEC_FLOAT
:
1125 g_string_append(str
, "float");
1127 case TYPESPEC_DOUBLE
:
1128 g_string_append(str
, "double");
1130 case TYPESPEC_SIGNED
:
1131 g_string_append(str
, "signed");
1133 case TYPESPEC_UNSIGNED
:
1134 g_string_append(str
, "unsigned");
1137 g_string_append(str
, "bool");
1139 case TYPESPEC_COMPLEX
:
1140 g_string_append(str
, "_Complex");
1142 case TYPESPEC_IMAGINARY
:
1143 g_string_append(str
, "_Imaginary");
1145 case TYPESPEC_CONST
:
1146 g_string_append(str
, "const");
1148 case TYPESPEC_ID_TYPE
:
1149 if (type_specifier
->u
.type_specifier
.id_type
) {
1150 g_string_append(str
,
1151 type_specifier
->u
.type_specifier
.id_type
);
1154 case TYPESPEC_STRUCT
:
1156 struct ctf_node
*node
= type_specifier
->u
.type_specifier
.node
;
1158 if (!node
->u
._struct
.name
) {
1159 _BT_LOGE_NODE(node
, "Unexpected empty structure field type name.");
1164 g_string_append(str
, "struct ");
1165 g_string_append(str
, node
->u
._struct
.name
);
1168 case TYPESPEC_VARIANT
:
1170 struct ctf_node
*node
= type_specifier
->u
.type_specifier
.node
;
1172 if (!node
->u
.variant
.name
) {
1173 _BT_LOGE_NODE(node
, "Unexpected empty variant field type name.");
1178 g_string_append(str
, "variant ");
1179 g_string_append(str
, node
->u
.variant
.name
);
1184 struct ctf_node
*node
= type_specifier
->u
.type_specifier
.node
;
1186 if (!node
->u
._enum
.enum_id
) {
1188 "Unexpected empty enumeration field type (`enum`) name.");
1193 g_string_append(str
, "enum ");
1194 g_string_append(str
, node
->u
._enum
.enum_id
);
1197 case TYPESPEC_FLOATING_POINT
:
1198 case TYPESPEC_INTEGER
:
1199 case TYPESPEC_STRING
:
1201 _BT_LOGE_NODE(type_specifier
->u
.type_specifier
.node
,
1202 "Unexpected type specifier type: %d",
1203 type_specifier
->u
.type_specifier
.type
);
1213 int get_type_specifier_list_name(struct ctx
*ctx
,
1214 struct ctf_node
*type_specifier_list
, GString
*str
)
1217 struct ctf_node
*iter
;
1218 int alias_item_nr
= 0;
1219 struct bt_list_head
*head
=
1220 &type_specifier_list
->u
.type_specifier_list
.head
;
1222 bt_list_for_each_entry(iter
, head
, siblings
) {
1223 if (alias_item_nr
!= 0) {
1224 g_string_append(str
, " ");
1228 ret
= get_type_specifier_name(ctx
, iter
, str
);
1239 GQuark
create_typealias_identifier(struct ctx
*ctx
,
1240 struct ctf_node
*type_specifier_list
,
1241 struct ctf_node
*node_type_declarator
)
1247 struct ctf_node
*iter
;
1248 struct bt_list_head
*pointers
=
1249 &node_type_declarator
->u
.type_declarator
.pointers
;
1251 str
= g_string_new("");
1252 ret
= get_type_specifier_list_name(ctx
, type_specifier_list
, str
);
1254 g_string_free(str
, TRUE
);
1258 bt_list_for_each_entry(iter
, pointers
, siblings
) {
1259 g_string_append(str
, " *");
1261 if (iter
->u
.pointer
.const_qualifier
) {
1262 g_string_append(str
, " const");
1266 str_c
= g_string_free(str
, FALSE
);
1267 qalias
= g_quark_from_string(str_c
);
1275 int visit_type_declarator(struct ctx
*ctx
, struct ctf_node
*type_specifier_list
,
1276 GQuark
*field_name
, struct ctf_node
*node_type_declarator
,
1277 struct bt_field_type
**field_decl
,
1278 struct bt_field_type
*nested_decl
)
1281 * During this whole function, nested_decl is always OURS,
1282 * whereas field_decl is an output which we create, but
1283 * belongs to the caller (it is moved).
1289 /* Validate type declarator node */
1290 if (node_type_declarator
) {
1291 if (node_type_declarator
->u
.type_declarator
.type
==
1293 _BT_LOGE_NODE(node_type_declarator
,
1294 "Unexpected type declarator type: type=%d",
1295 node_type_declarator
->u
.type_declarator
.type
);
1300 /* TODO: GCC bitfields not supported yet */
1301 if (node_type_declarator
->u
.type_declarator
.bitfield_len
!=
1303 _BT_LOGE_NODE(node_type_declarator
,
1304 "GCC bitfields are not supported as of this version.");
1310 /* Find the right nested declaration if not provided */
1312 struct bt_list_head
*pointers
=
1313 &node_type_declarator
->u
.type_declarator
.pointers
;
1315 if (node_type_declarator
&& !bt_list_empty(pointers
)) {
1317 _BT_FIELD_TYPE_INIT(nested_decl_copy
);
1320 * If we have a pointer declarator, it HAS to
1321 * be present in the typealiases (else fail).
1323 qalias
= create_typealias_identifier(ctx
,
1324 type_specifier_list
, node_type_declarator
);
1326 ctx_decl_scope_lookup_alias(ctx
->current_scope
,
1327 g_quark_to_string(qalias
), -1);
1329 _BT_LOGE_NODE(node_type_declarator
,
1330 "Cannot find type alias: name=\"%s\"",
1331 g_quark_to_string(qalias
));
1336 /* Make a copy of it */
1337 nested_decl_copy
= bt_field_type_copy(nested_decl
);
1338 BT_PUT(nested_decl
);
1339 if (!nested_decl_copy
) {
1340 _BT_LOGE_NODE(node_type_declarator
,
1341 "Cannot copy nested field type.");
1346 BT_MOVE(nested_decl
, nested_decl_copy
);
1348 /* Force integer's base to 16 since it's a pointer */
1349 if (bt_field_type_is_integer(nested_decl
)) {
1350 ret
= bt_field_type_integer_set_base(
1352 BT_INTEGER_BASE_HEXADECIMAL
);
1353 BT_ASSERT(ret
== 0);
1356 ret
= visit_type_specifier_list(ctx
,
1357 type_specifier_list
, &nested_decl
);
1359 BT_ASSERT(!nested_decl
);
1365 BT_ASSERT(nested_decl
);
1367 if (!node_type_declarator
) {
1368 BT_MOVE(*field_decl
, nested_decl
);
1372 if (node_type_declarator
->u
.type_declarator
.type
== TYPEDEC_ID
) {
1373 if (node_type_declarator
->u
.type_declarator
.u
.id
) {
1375 node_type_declarator
->u
.type_declarator
.u
.id
;
1381 *field_name
= g_quark_from_string(id
);
1386 BT_MOVE(*field_decl
, nested_decl
);
1389 struct ctf_node
*first
;
1390 _BT_FIELD_TYPE_INIT(decl
);
1391 _BT_FIELD_TYPE_INIT(outer_field_decl
);
1392 struct bt_list_head
*length
=
1393 &node_type_declarator
->
1394 u
.type_declarator
.u
.nested
.length
;
1396 /* Create array/sequence, pass nested_decl as child */
1397 if (bt_list_empty(length
)) {
1398 _BT_LOGE_NODE(node_type_declarator
,
1399 "Expecting length field reference or value.");
1404 first
= _BT_LIST_FIRST_ENTRY(length
, struct ctf_node
, siblings
);
1405 if (first
->type
!= NODE_UNARY_EXPRESSION
) {
1406 _BT_LOGE_NODE(first
,
1407 "Unexpected node type: node-type=%d",
1413 switch (first
->u
.unary_expression
.type
) {
1414 case UNARY_UNSIGNED_CONSTANT
:
1417 _BT_FIELD_TYPE_INIT(array_decl
);
1419 len
= first
->u
.unary_expression
.u
.unsigned_constant
;
1420 array_decl
= bt_field_type_array_create(nested_decl
,
1422 BT_PUT(nested_decl
);
1424 _BT_LOGE_NODE(first
,
1425 "Cannot create array field type.");
1430 BT_MOVE(decl
, array_decl
);
1435 /* Lookup unsigned integer definition, create seq. */
1436 _BT_FIELD_TYPE_INIT(seq_decl
);
1437 char *length_name
= concatenate_unary_strings(length
);
1438 char *length_name_no_underscore
;
1441 _BT_LOGE_NODE(node_type_declarator
,
1442 "Cannot concatenate unary strings.");
1447 length_name_no_underscore
=
1448 remove_underscores_from_field_ref(length_name
);
1449 if (!length_name_no_underscore
) {
1450 /* remove_underscores_from_field_ref() logs errors */
1454 seq_decl
= bt_field_type_sequence_create(
1455 nested_decl
, length_name_no_underscore
);
1456 free(length_name_no_underscore
);
1457 g_free(length_name
);
1458 BT_PUT(nested_decl
);
1460 _BT_LOGE_NODE(node_type_declarator
,
1461 "Cannot create sequence field type.");
1466 BT_MOVE(decl
, seq_decl
);
1474 BT_ASSERT(!nested_decl
);
1476 BT_ASSERT(!*field_decl
);
1479 * At this point, we found the next nested declaration.
1480 * We currently own this (and lost the ownership of
1481 * nested_decl in the meantime). Pass this next
1482 * nested declaration as the content of the outer
1483 * container, MOVING its ownership.
1485 ret
= visit_type_declarator(ctx
, type_specifier_list
,
1487 node_type_declarator
->
1488 u
.type_declarator
.u
.nested
.type_declarator
,
1489 &outer_field_decl
, decl
);
1492 BT_ASSERT(!outer_field_decl
);
1497 BT_ASSERT(outer_field_decl
);
1498 BT_MOVE(*field_decl
, outer_field_decl
);
1502 BT_PUT(nested_decl
);
1503 BT_ASSERT(*field_decl
);
1508 BT_PUT(nested_decl
);
1509 BT_PUT(*field_decl
);
1515 int visit_struct_decl_field(struct ctx
*ctx
,
1516 struct bt_field_type
*struct_decl
,
1517 struct ctf_node
*type_specifier_list
,
1518 struct bt_list_head
*type_declarators
)
1521 struct ctf_node
*iter
;
1522 _BT_FIELD_TYPE_INIT(field_decl
);
1524 bt_list_for_each_entry(iter
, type_declarators
, siblings
) {
1527 const char *field_name
;
1528 _BT_FIELD_TYPE_INIT(efield_decl
);
1530 ret
= visit_type_declarator(ctx
, type_specifier_list
,
1531 &qfield_name
, iter
, &field_decl
, NULL
);
1533 BT_ASSERT(!field_decl
);
1534 _BT_LOGE_NODE(type_specifier_list
,
1535 "Cannot visit type declarator: ret=%d", ret
);
1539 BT_ASSERT(field_decl
);
1540 field_name
= g_quark_to_string(qfield_name
);
1542 /* Check if field with same name already exists */
1544 bt_field_type_structure_get_field_type_by_name(
1545 struct_decl
, field_name
);
1547 BT_PUT(efield_decl
);
1548 _BT_LOGE_NODE(type_specifier_list
,
1549 "Duplicate field in structure field type: "
1550 "field-name=\"%s\"", field_name
);
1555 /* Add field to structure */
1556 ret
= bt_field_type_structure_add_field(struct_decl
,
1557 field_decl
, field_name
);
1560 _BT_LOGE_NODE(type_specifier_list
,
1561 "Cannot add field to structure field type: "
1562 "field-name=\"%s\", ret=%d",
1563 g_quark_to_string(qfield_name
), ret
);
1577 int visit_variant_decl_field(struct ctx
*ctx
,
1578 struct bt_field_type
*variant_decl
,
1579 struct ctf_node
*type_specifier_list
,
1580 struct bt_list_head
*type_declarators
)
1583 struct ctf_node
*iter
;
1584 _BT_FIELD_TYPE_INIT(field_decl
);
1586 bt_list_for_each_entry(iter
, type_declarators
, siblings
) {
1589 const char *field_name
;
1590 _BT_FIELD_TYPE_INIT(efield_decl
);
1592 ret
= visit_type_declarator(ctx
, type_specifier_list
,
1593 &qfield_name
, iter
, &field_decl
, NULL
);
1595 BT_ASSERT(!field_decl
);
1596 _BT_LOGE_NODE(type_specifier_list
,
1597 "Cannot visit type declarator: ret=%d", ret
);
1601 BT_ASSERT(field_decl
);
1602 field_name
= g_quark_to_string(qfield_name
);
1604 /* Check if field with same name already exists */
1606 bt_field_type_variant_get_field_type_by_name(
1607 variant_decl
, field_name
);
1609 BT_PUT(efield_decl
);
1610 _BT_LOGE_NODE(type_specifier_list
,
1611 "Duplicate field in variant field type: "
1612 "field-name=\"%s\"", field_name
);
1617 /* Add field to structure */
1618 ret
= bt_field_type_variant_add_field(variant_decl
,
1619 field_decl
, field_name
);
1622 _BT_LOGE_NODE(type_specifier_list
,
1623 "Cannot add field to variant field type: "
1624 "field-name=\"%s\", ret=%d",
1625 g_quark_to_string(qfield_name
), ret
);
1639 int visit_typedef(struct ctx
*ctx
, struct ctf_node
*type_specifier_list
,
1640 struct bt_list_head
*type_declarators
)
1644 struct ctf_node
*iter
;
1645 _BT_FIELD_TYPE_INIT(type_decl
);
1647 bt_list_for_each_entry(iter
, type_declarators
, siblings
) {
1648 ret
= visit_type_declarator(ctx
, type_specifier_list
,
1649 &qidentifier
, iter
, &type_decl
, NULL
);
1652 "Cannot visit type declarator: ret=%d", ret
);
1657 /* Do not allow typedef and typealias of untagged variants */
1658 if (bt_field_type_is_variant(type_decl
)) {
1659 if (bt_field_type_variant_get_tag_name(type_decl
)) {
1661 "Type definition of untagged variant field type is not allowed.");
1667 ret
= ctx_decl_scope_register_alias(ctx
->current_scope
,
1668 g_quark_to_string(qidentifier
), type_decl
);
1671 "Cannot register type definition: name=\"%s\"",
1672 g_quark_to_string(qidentifier
));
1684 int visit_typealias(struct ctx
*ctx
, struct ctf_node
*target
,
1685 struct ctf_node
*alias
)
1689 struct ctf_node
*node
;
1690 GQuark qdummy_field_name
;
1691 _BT_FIELD_TYPE_INIT(type_decl
);
1693 /* Create target type declaration */
1694 if (bt_list_empty(&target
->u
.typealias_target
.type_declarators
)) {
1697 node
= _BT_LIST_FIRST_ENTRY(
1698 &target
->u
.typealias_target
.type_declarators
,
1699 struct ctf_node
, siblings
);
1702 ret
= visit_type_declarator(ctx
,
1703 target
->u
.typealias_target
.type_specifier_list
,
1704 &qdummy_field_name
, node
, &type_decl
, NULL
);
1706 BT_ASSERT(!type_decl
);
1708 "Cannot visit type declarator: ret=%d", ret
);
1712 /* Do not allow typedef and typealias of untagged variants */
1713 if (bt_field_type_is_variant(type_decl
)) {
1714 if (bt_field_type_variant_get_tag_name(type_decl
)) {
1715 _BT_LOGE_NODE(target
,
1716 "Type definition of untagged variant field type is not allowed.");
1723 * The semantic validator does not check whether the target is
1724 * abstract or not (if it has an identifier). Check it here.
1726 if (qdummy_field_name
!= 0) {
1727 _BT_LOGE_NODE(target
,
1728 "Expecting empty identifier: id=\"%s\"",
1729 g_quark_to_string(qdummy_field_name
));
1734 /* Create alias identifier */
1735 node
= _BT_LIST_FIRST_ENTRY(&alias
->u
.typealias_alias
.type_declarators
,
1736 struct ctf_node
, siblings
);
1737 qalias
= create_typealias_identifier(ctx
,
1738 alias
->u
.typealias_alias
.type_specifier_list
, node
);
1739 ret
= ctx_decl_scope_register_alias(ctx
->current_scope
,
1740 g_quark_to_string(qalias
), type_decl
);
1743 "Cannot register type alias: name=\"%s\"",
1744 g_quark_to_string(qalias
));
1755 int visit_struct_decl_entry(struct ctx
*ctx
, struct ctf_node
*entry_node
,
1756 struct bt_field_type
*struct_decl
)
1760 switch (entry_node
->type
) {
1762 ret
= visit_typedef(ctx
,
1763 entry_node
->u
._typedef
.type_specifier_list
,
1764 &entry_node
->u
._typedef
.type_declarators
);
1766 _BT_LOGE_NODE(entry_node
,
1767 "Cannot add type definition found in structure field type: ret=%d",
1772 case NODE_TYPEALIAS
:
1773 ret
= visit_typealias(ctx
, entry_node
->u
.typealias
.target
,
1774 entry_node
->u
.typealias
.alias
);
1776 _BT_LOGE_NODE(entry_node
,
1777 "Cannot add type alias found in structure field type: ret=%d",
1782 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
1784 ret
= visit_struct_decl_field(ctx
, struct_decl
,
1785 entry_node
->u
.struct_or_variant_declaration
.
1786 type_specifier_list
,
1787 &entry_node
->u
.struct_or_variant_declaration
.
1794 _BT_LOGE_NODE(entry_node
,
1795 "Unexpected node type: node-type=%d", entry_node
->type
);
1805 int visit_variant_decl_entry(struct ctx
*ctx
, struct ctf_node
*entry_node
,
1806 struct bt_field_type
*variant_decl
)
1810 switch (entry_node
->type
) {
1812 ret
= visit_typedef(ctx
,
1813 entry_node
->u
._typedef
.type_specifier_list
,
1814 &entry_node
->u
._typedef
.type_declarators
);
1816 _BT_LOGE_NODE(entry_node
,
1817 "Cannot add type definition found in variant field type: ret=%d",
1822 case NODE_TYPEALIAS
:
1823 ret
= visit_typealias(ctx
, entry_node
->u
.typealias
.target
,
1824 entry_node
->u
.typealias
.alias
);
1826 _BT_LOGE_NODE(entry_node
,
1827 "Cannot add type alias found in variant field type: ret=%d",
1832 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
1834 ret
= visit_variant_decl_field(ctx
, variant_decl
,
1835 entry_node
->u
.struct_or_variant_declaration
.
1836 type_specifier_list
,
1837 &entry_node
->u
.struct_or_variant_declaration
.
1844 _BT_LOGE_NODE(entry_node
,
1845 "Unexpected node type: node-type=%d",
1856 int visit_struct_decl(struct ctx
*ctx
, const char *name
,
1857 struct bt_list_head
*decl_list
, int has_body
,
1858 struct bt_list_head
*min_align
,
1859 struct bt_field_type
**struct_decl
)
1863 *struct_decl
= NULL
;
1865 /* For named struct (without body), lookup in declaration scope */
1867 _BT_FIELD_TYPE_INIT(struct_decl_copy
);
1870 BT_LOGE_STR("Bodyless structure field type: missing name.");
1875 *struct_decl
= ctx_decl_scope_lookup_struct(ctx
->current_scope
,
1877 if (!*struct_decl
) {
1878 BT_LOGE("Cannot find structure field type: name=\"struct %s\"",
1884 /* Make a copy of it */
1885 struct_decl_copy
= bt_field_type_copy(*struct_decl
);
1886 if (!struct_decl_copy
) {
1887 BT_LOGE_STR("Cannot create copy of structure field type.");
1892 BT_MOVE(*struct_decl
, struct_decl_copy
);
1894 struct ctf_node
*entry_node
;
1895 uint64_t min_align_value
= 0;
1898 _BT_FIELD_TYPE_INIT(estruct_decl
);
1900 estruct_decl
= ctx_decl_scope_lookup_struct(
1901 ctx
->current_scope
, name
, 1);
1903 BT_PUT(estruct_decl
);
1904 BT_LOGE("Structure field type already declared in local scope: "
1905 "name=\"struct %s\"", name
);
1911 if (!bt_list_empty(min_align
)) {
1912 ret
= get_unary_unsigned(min_align
, &min_align_value
);
1914 BT_LOGE("Unexpected unary expression for structure field type's `align` attribute: "
1920 *struct_decl
= bt_field_type_structure_create();
1921 if (!*struct_decl
) {
1922 BT_LOGE_STR("Cannot create empty structure field type.");
1927 if (min_align_value
!= 0) {
1928 ret
= bt_field_type_set_alignment(*struct_decl
,
1931 BT_LOGE("Cannot set structure field type's alignment: "
1937 ret
= ctx_push_scope(ctx
);
1939 BT_LOGE_STR("Cannot push scope.");
1943 bt_list_for_each_entry(entry_node
, decl_list
, siblings
) {
1944 ret
= visit_struct_decl_entry(ctx
, entry_node
,
1947 _BT_LOGE_NODE(entry_node
,
1948 "Cannot visit structure field type entry: "
1958 ret
= ctx_decl_scope_register_struct(ctx
->current_scope
,
1959 name
, *struct_decl
);
1961 BT_LOGE("Cannot register structure field type in declaration scope: "
1962 "name=\"struct %s\", ret=%d", name
, ret
);
1971 BT_PUT(*struct_decl
);
1977 int visit_variant_decl(struct ctx
*ctx
, const char *name
,
1978 const char *tag
, struct bt_list_head
*decl_list
,
1979 int has_body
, struct bt_field_type
**variant_decl
)
1982 _BT_FIELD_TYPE_INIT(untagged_variant_decl
);
1984 *variant_decl
= NULL
;
1986 /* For named variant (without body), lookup in declaration scope */
1988 _BT_FIELD_TYPE_INIT(variant_decl_copy
);
1991 BT_LOGE_STR("Bodyless variant field type: missing name.");
1996 untagged_variant_decl
=
1997 ctx_decl_scope_lookup_variant(ctx
->current_scope
,
1999 if (!untagged_variant_decl
) {
2000 BT_LOGE("Cannot find variant field type: name=\"variant %s\"",
2006 /* Make a copy of it */
2007 variant_decl_copy
= bt_field_type_copy(
2008 untagged_variant_decl
);
2009 if (!variant_decl_copy
) {
2010 BT_LOGE_STR("Cannot create copy of variant field type.");
2015 BT_MOVE(untagged_variant_decl
, variant_decl_copy
);
2017 struct ctf_node
*entry_node
;
2020 struct bt_field_type
*evariant_decl
=
2021 ctx_decl_scope_lookup_struct(ctx
->current_scope
,
2024 if (evariant_decl
) {
2025 BT_PUT(evariant_decl
);
2026 BT_LOGE("Variant field type already declared in local scope: "
2027 "name=\"variant %s\"", name
);
2033 untagged_variant_decl
= bt_field_type_variant_create(NULL
,
2035 if (!untagged_variant_decl
) {
2036 BT_LOGE_STR("Cannot create empty variant field type.");
2041 ret
= ctx_push_scope(ctx
);
2043 BT_LOGE_STR("Cannot push scope.");
2047 bt_list_for_each_entry(entry_node
, decl_list
, siblings
) {
2048 ret
= visit_variant_decl_entry(ctx
, entry_node
,
2049 untagged_variant_decl
);
2051 _BT_LOGE_NODE(entry_node
,
2052 "Cannot visit variant field type entry: "
2062 ret
= ctx_decl_scope_register_variant(
2063 ctx
->current_scope
, name
,
2064 untagged_variant_decl
);
2066 BT_LOGE("Cannot register variant field type in declaration scope: "
2067 "name=\"variant %s\", ret=%d", name
, ret
);
2074 * If tagged, create tagged variant and return; otherwise
2075 * return untagged variant.
2078 BT_MOVE(*variant_decl
, untagged_variant_decl
);
2081 * At this point, we have a fresh untagged variant; nobody
2082 * else owns it. Set its tag now.
2084 char *tag_no_underscore
=
2085 remove_underscores_from_field_ref(tag
);
2087 if (!tag_no_underscore
) {
2088 /* remove_underscores_from_field_ref() logs errors */
2092 ret
= bt_field_type_variant_set_tag_name(
2093 untagged_variant_decl
, tag_no_underscore
);
2094 free(tag_no_underscore
);
2096 BT_LOGE("Cannot set variant field type's tag name: "
2097 "tag-name=\"%s\"", tag
);
2101 BT_MOVE(*variant_decl
, untagged_variant_decl
);
2104 BT_ASSERT(!untagged_variant_decl
);
2105 BT_ASSERT(*variant_decl
);
2110 BT_PUT(untagged_variant_decl
);
2111 BT_PUT(*variant_decl
);
2117 int visit_enum_decl_entry(struct ctx
*ctx
, struct ctf_node
*enumerator
,
2118 struct bt_field_type
*enum_decl
, int64_t *last
, bt_bool is_signed
)
2122 struct ctf_node
*iter
;
2123 int64_t start
= 0, end
= 0;
2124 const char *label
= enumerator
->u
.enumerator
.id
;
2125 const char *effective_label
= label
;
2126 struct bt_list_head
*values
= &enumerator
->u
.enumerator
.values
;
2128 bt_list_for_each_entry(iter
, values
, siblings
) {
2131 if (iter
->type
!= NODE_UNARY_EXPRESSION
) {
2133 "Wrong expression for enumeration field type label: "
2134 "node-type=%d, label=\"%s\"", iter
->type
,
2146 switch (iter
->u
.unary_expression
.type
) {
2147 case UNARY_SIGNED_CONSTANT
:
2148 *target
= iter
->u
.unary_expression
.u
.signed_constant
;
2150 case UNARY_UNSIGNED_CONSTANT
:
2152 iter
->u
.unary_expression
.u
.unsigned_constant
;
2156 "Invalid enumeration field type entry: "
2157 "expecting constant signed or unsigned integer: "
2158 "node-type=%d, label=\"%s\"",
2159 iter
->u
.unary_expression
.type
, label
);
2166 "Invalid enumeration field type entry: label=\"%s\"",
2185 if (label
[0] == '_') {
2187 * Strip the first underscore of any enumeration field
2188 * type's label in case this enumeration FT is used as
2189 * a variant FT tag later. The variant FT choice names
2190 * could also start with `_`, in which case the prefix
2191 * is removed, and it the resulting choice name needs to
2194 effective_label
= &label
[1];
2198 ret
= bt_field_type_enumeration_signed_add_mapping(enum_decl
,
2199 effective_label
, start
, end
);
2201 ret
= bt_field_type_enumeration_unsigned_add_mapping(enum_decl
,
2202 effective_label
, (uint64_t) start
, (uint64_t) end
);
2205 _BT_LOGE_NODE(enumerator
,
2206 "Cannot add mapping to enumeration field type: "
2207 "label=\"%s\", ret=%d, "
2208 "start-value-unsigned=%" PRIu64
", "
2209 "end-value-unsigned=%" PRIu64
, label
, ret
,
2210 (uint64_t) start
, (uint64_t) end
);
2221 int visit_enum_decl(struct ctx
*ctx
, const char *name
,
2222 struct ctf_node
*container_type
,
2223 struct bt_list_head
*enumerator_list
,
2225 struct bt_field_type
**enum_decl
)
2229 _BT_FIELD_TYPE_INIT(integer_decl
);
2233 /* For named enum (without body), lookup in declaration scope */
2235 _BT_FIELD_TYPE_INIT(enum_decl_copy
);
2238 BT_LOGE_STR("Bodyless enumeration field type: missing name.");
2243 *enum_decl
= ctx_decl_scope_lookup_enum(ctx
->current_scope
,
2246 BT_LOGE("Cannot find enumeration field type: "
2247 "name=\"enum %s\"", name
);
2252 /* Make a copy of it */
2253 enum_decl_copy
= bt_field_type_copy(*enum_decl
);
2254 if (!enum_decl_copy
) {
2255 BT_LOGE_STR("Cannot create copy of enumeration field type.");
2261 BT_MOVE(*enum_decl
, enum_decl_copy
);
2263 struct ctf_node
*iter
;
2264 int64_t last_value
= 0;
2267 _BT_FIELD_TYPE_INIT(eenum_decl
);
2269 eenum_decl
= ctx_decl_scope_lookup_enum(
2270 ctx
->current_scope
, name
, 1);
2273 BT_LOGE("Enumeration field type already declared in local scope: "
2274 "name=\"enum %s\"", name
);
2280 if (!container_type
) {
2281 integer_decl
= ctx_decl_scope_lookup_alias(
2282 ctx
->current_scope
, "int", -1);
2283 if (!integer_decl
) {
2284 BT_LOGE_STR("Cannot find implicit `int` field type alias for enumeration field type.");
2289 ret
= visit_type_declarator(ctx
, container_type
,
2290 &qdummy_id
, NULL
, &integer_decl
, NULL
);
2292 BT_ASSERT(!integer_decl
);
2298 BT_ASSERT(integer_decl
);
2300 if (!bt_field_type_is_integer(integer_decl
)) {
2301 BT_LOGE("Container field type for enumeration field type is not an integer field type: "
2303 bt_common_field_type_id_string(
2304 bt_field_type_get_type_id(integer_decl
)));
2309 *enum_decl
= bt_field_type_enumeration_create(integer_decl
);
2311 BT_LOGE_STR("Cannot create enumeration field type.");
2316 bt_list_for_each_entry(iter
, enumerator_list
, siblings
) {
2317 ret
= visit_enum_decl_entry(ctx
, iter
, *enum_decl
,
2319 bt_field_type_integer_is_signed(integer_decl
));
2322 "Cannot visit enumeration field type entry: "
2329 ret
= ctx_decl_scope_register_enum(ctx
->current_scope
,
2332 BT_LOGE("Cannot register enumeration field type in declaration scope: "
2339 BT_PUT(integer_decl
);
2344 BT_PUT(integer_decl
);
2351 int visit_type_specifier(struct ctx
*ctx
,
2352 struct ctf_node
*type_specifier_list
,
2353 struct bt_field_type
**decl
)
2356 GString
*str
= NULL
;
2357 _BT_FIELD_TYPE_INIT(decl_copy
);
2360 str
= g_string_new("");
2361 ret
= get_type_specifier_list_name(ctx
, type_specifier_list
, str
);
2363 _BT_LOGE_NODE(type_specifier_list
,
2364 "Cannot get type specifier list's name: ret=%d", ret
);
2368 *decl
= ctx_decl_scope_lookup_alias(ctx
->current_scope
, str
->str
, -1);
2370 _BT_LOGE_NODE(type_specifier_list
,
2371 "Cannot find type alias: name=\"%s\"", str
->str
);
2376 /* Make a copy of the type declaration */
2377 decl_copy
= bt_field_type_copy(*decl
);
2379 _BT_LOGE_NODE(type_specifier_list
,
2380 "Cannot create field type copy.");
2385 BT_MOVE(*decl
, decl_copy
);
2386 (void) g_string_free(str
, TRUE
);
2393 (void) g_string_free(str
, TRUE
);
2402 int visit_integer_decl(struct ctx
*ctx
,
2403 struct bt_list_head
*expressions
,
2404 struct bt_field_type
**integer_decl
)
2408 bt_bool signedness
= 0;
2409 struct ctf_node
*expression
;
2410 uint64_t alignment
= 0, size
= 0;
2411 struct bt_clock_class
*mapped_clock
= NULL
;
2412 enum bt_string_encoding encoding
= BT_STRING_ENCODING_NONE
;
2413 enum bt_integer_base base
= BT_INTEGER_BASE_DECIMAL
;
2414 enum bt_byte_order byte_order
=
2415 bt_trace_get_native_byte_order(ctx
->trace
);
2417 *integer_decl
= NULL
;
2419 bt_list_for_each_entry(expression
, expressions
, siblings
) {
2420 struct ctf_node
*left
, *right
;
2422 left
= _BT_LIST_FIRST_ENTRY(&expression
->u
.ctf_expression
.left
,
2423 struct ctf_node
, siblings
);
2424 right
= _BT_LIST_FIRST_ENTRY(
2425 &expression
->u
.ctf_expression
.right
, struct ctf_node
,
2428 if (left
->u
.unary_expression
.type
!= UNARY_STRING
) {
2430 "Unexpected unary expression type: type=%d",
2431 left
->u
.unary_expression
.type
);
2436 if (!strcmp(left
->u
.unary_expression
.u
.string
, "signed")) {
2437 if (_IS_SET(&set
, _INTEGER_SIGNED_SET
)) {
2438 _BT_LOGE_DUP_ATTR(left
, "signed",
2439 "integer field type");
2444 signedness
= get_boolean(right
);
2445 if (signedness
< 0) {
2446 _BT_LOGE_NODE(right
,
2447 "Invalid boolean value for integer field type's `signed` attribute: "
2453 _SET(&set
, _INTEGER_SIGNED_SET
);
2454 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2456 if (_IS_SET(&set
, _INTEGER_BYTE_ORDER_SET
)) {
2457 _BT_LOGE_DUP_ATTR(left
, "byte_order",
2458 "integer field type");
2463 byte_order
= get_real_byte_order(ctx
, right
);
2464 if (byte_order
== BT_BYTE_ORDER_UNKNOWN
) {
2465 _BT_LOGE_NODE(right
,
2466 "Invalid `byte_order` attribute in integer field type: "
2472 _SET(&set
, _INTEGER_BYTE_ORDER_SET
);
2473 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "size")) {
2474 if (_IS_SET(&set
, _INTEGER_SIZE_SET
)) {
2475 _BT_LOGE_DUP_ATTR(left
, "size",
2476 "integer field type");
2481 if (right
->u
.unary_expression
.type
!=
2482 UNARY_UNSIGNED_CONSTANT
) {
2483 _BT_LOGE_NODE(right
,
2484 "Invalid `size` attribute in integer field type: "
2485 "expecting unsigned constant integer: "
2487 right
->u
.unary_expression
.type
);
2492 size
= right
->u
.unary_expression
.u
.unsigned_constant
;
2494 _BT_LOGE_NODE(right
,
2495 "Invalid `size` attribute in integer field type: "
2496 "expecting positive constant integer: "
2497 "size=%" PRIu64
, size
);
2500 } else if (size
> 64) {
2501 _BT_LOGE_NODE(right
,
2502 "Invalid `size` attribute in integer field type: "
2503 "integer fields over 64 bits are not supported as of this version: "
2504 "size=%" PRIu64
, size
);
2509 _SET(&set
, _INTEGER_SIZE_SET
);
2510 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2512 if (_IS_SET(&set
, _INTEGER_ALIGN_SET
)) {
2513 _BT_LOGE_DUP_ATTR(left
, "align",
2514 "integer field type");
2519 if (right
->u
.unary_expression
.type
!=
2520 UNARY_UNSIGNED_CONSTANT
) {
2521 _BT_LOGE_NODE(right
,
2522 "Invalid `align` attribute in integer field type: "
2523 "expecting unsigned constant integer: "
2525 right
->u
.unary_expression
.type
);
2531 right
->u
.unary_expression
.u
.unsigned_constant
;
2532 if (!is_align_valid(alignment
)) {
2533 _BT_LOGE_NODE(right
,
2534 "Invalid `align` attribute in integer field type: "
2535 "expecting power of two: "
2536 "align=%" PRIu64
, alignment
);
2541 _SET(&set
, _INTEGER_ALIGN_SET
);
2542 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "base")) {
2543 if (_IS_SET(&set
, _INTEGER_BASE_SET
)) {
2544 _BT_LOGE_DUP_ATTR(left
, "base",
2545 "integer field type");
2550 switch (right
->u
.unary_expression
.type
) {
2551 case UNARY_UNSIGNED_CONSTANT
:
2553 uint64_t constant
= right
->u
.unary_expression
.
2554 u
.unsigned_constant
;
2558 base
= BT_INTEGER_BASE_BINARY
;
2561 base
= BT_INTEGER_BASE_OCTAL
;
2564 base
= BT_INTEGER_BASE_DECIMAL
;
2567 base
= BT_INTEGER_BASE_HEXADECIMAL
;
2570 _BT_LOGE_NODE(right
,
2571 "Invalid `base` attribute in integer field type: "
2573 right
->u
.unary_expression
.u
.unsigned_constant
);
2581 char *s_right
= concatenate_unary_strings(
2582 &expression
->u
.ctf_expression
.right
);
2584 _BT_LOGE_NODE(right
,
2585 "Unexpected unary expression for integer field type's `base` attribute.");
2590 if (!strcmp(s_right
, "decimal") ||
2591 !strcmp(s_right
, "dec") ||
2592 !strcmp(s_right
, "d") ||
2593 !strcmp(s_right
, "i") ||
2594 !strcmp(s_right
, "u")) {
2595 base
= BT_INTEGER_BASE_DECIMAL
;
2596 } else if (!strcmp(s_right
, "hexadecimal") ||
2597 !strcmp(s_right
, "hex") ||
2598 !strcmp(s_right
, "x") ||
2599 !strcmp(s_right
, "X") ||
2600 !strcmp(s_right
, "p")) {
2601 base
= BT_INTEGER_BASE_HEXADECIMAL
;
2602 } else if (!strcmp(s_right
, "octal") ||
2603 !strcmp(s_right
, "oct") ||
2604 !strcmp(s_right
, "o")) {
2605 base
= BT_INTEGER_BASE_OCTAL
;
2606 } else if (!strcmp(s_right
, "binary") ||
2607 !strcmp(s_right
, "b")) {
2608 base
= BT_INTEGER_BASE_BINARY
;
2610 _BT_LOGE_NODE(right
,
2611 "Unexpected unary expression for integer field type's `base` attribute: "
2612 "base=\"%s\"", s_right
);
2622 _BT_LOGE_NODE(right
,
2623 "Invalid `base` attribute in integer field type: "
2624 "expecting unsigned constant integer or unary string.");
2629 _SET(&set
, _INTEGER_BASE_SET
);
2630 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2634 if (_IS_SET(&set
, _INTEGER_ENCODING_SET
)) {
2635 _BT_LOGE_DUP_ATTR(left
, "encoding",
2636 "integer field type");
2641 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
2642 _BT_LOGE_NODE(right
,
2643 "Invalid `encoding` attribute in integer field type: "
2644 "expecting unary string.");
2649 s_right
= concatenate_unary_strings(
2650 &expression
->u
.ctf_expression
.right
);
2652 _BT_LOGE_NODE(right
,
2653 "Unexpected unary expression for integer field type's `encoding` attribute.");
2658 if (!strcmp(s_right
, "UTF8") ||
2659 !strcmp(s_right
, "utf8") ||
2660 !strcmp(s_right
, "utf-8") ||
2661 !strcmp(s_right
, "UTF-8")) {
2662 encoding
= BT_STRING_ENCODING_UTF8
;
2663 } else if (!strcmp(s_right
, "ASCII") ||
2664 !strcmp(s_right
, "ascii")) {
2665 encoding
= BT_STRING_ENCODING_ASCII
;
2666 } else if (!strcmp(s_right
, "none")) {
2667 encoding
= BT_STRING_ENCODING_NONE
;
2669 _BT_LOGE_NODE(right
,
2670 "Invalid `encoding` attribute in integer field type: "
2671 "unknown encoding: encoding=\"%s\"",
2679 _SET(&set
, _INTEGER_ENCODING_SET
);
2680 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "map")) {
2681 const char *clock_name
;
2683 if (_IS_SET(&set
, _INTEGER_MAP_SET
)) {
2684 _BT_LOGE_DUP_ATTR(left
, "map",
2685 "integer field type");
2690 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
2691 _BT_LOGE_NODE(right
,
2692 "Invalid `map` attribute in integer field type: "
2693 "expecting unary string.");
2699 get_map_clock_name_value(
2700 &expression
->u
.ctf_expression
.right
);
2702 char *s_right
= concatenate_unary_strings(
2703 &expression
->u
.ctf_expression
.right
);
2706 _BT_LOGE_NODE(right
,
2707 "Unexpected unary expression for integer field type's `map` attribute.");
2712 _BT_LOGE_NODE(right
,
2713 "Invalid `map` attribute in integer field type: "
2714 "cannot find clock class at this point: name=\"%s\"",
2716 _SET(&set
, _INTEGER_MAP_SET
);
2721 mapped_clock
= bt_trace_get_clock_class_by_name(
2722 ctx
->trace
, clock_name
);
2723 if (!mapped_clock
) {
2724 _BT_LOGE_NODE(right
,
2725 "Invalid `map` attribute in integer field type: "
2726 "cannot find clock class at this point: name=\"%s\"",
2732 _SET(&set
, _INTEGER_MAP_SET
);
2735 "Unknown attribute in integer field type: "
2737 left
->u
.unary_expression
.u
.string
);
2741 if (!_IS_SET(&set
, _INTEGER_SIZE_SET
)) {
2742 BT_LOGE_STR("Missing `size` attribute in integer field type.");
2747 if (!_IS_SET(&set
, _INTEGER_ALIGN_SET
)) {
2748 if (size
% CHAR_BIT
) {
2749 /* Bit-packed alignment */
2752 /* Byte-packed alignment */
2753 alignment
= CHAR_BIT
;
2757 *integer_decl
= bt_field_type_integer_create((unsigned int) size
);
2758 if (!*integer_decl
) {
2759 BT_LOGE_STR("Cannot create integer field type.");
2764 ret
= bt_field_type_integer_set_is_signed(*integer_decl
, signedness
);
2765 ret
|= bt_field_type_integer_set_base(*integer_decl
, base
);
2766 ret
|= bt_field_type_integer_set_encoding(*integer_decl
, encoding
);
2767 ret
|= bt_field_type_set_alignment(*integer_decl
,
2768 (unsigned int) alignment
);
2769 ret
|= bt_field_type_set_byte_order(*integer_decl
, byte_order
);
2773 ret
|= bt_field_type_integer_set_mapped_clock_class(
2774 *integer_decl
, mapped_clock
);
2775 bt_put(mapped_clock
);
2776 mapped_clock
= NULL
;
2780 BT_LOGE_STR("Cannot configure integer field type.");
2789 bt_put(mapped_clock
);
2792 BT_PUT(*integer_decl
);
2798 int visit_floating_point_number_decl(struct ctx
*ctx
,
2799 struct bt_list_head
*expressions
,
2800 struct bt_field_type
**float_decl
)
2804 struct ctf_node
*expression
;
2805 uint64_t alignment
= 1, exp_dig
= 0, mant_dig
= 0;
2806 enum bt_byte_order byte_order
=
2807 bt_trace_get_native_byte_order(ctx
->trace
);
2811 bt_list_for_each_entry(expression
, expressions
, siblings
) {
2812 struct ctf_node
*left
, *right
;
2814 left
= _BT_LIST_FIRST_ENTRY(&expression
->u
.ctf_expression
.left
,
2815 struct ctf_node
, siblings
);
2816 right
= _BT_LIST_FIRST_ENTRY(
2817 &expression
->u
.ctf_expression
.right
, struct ctf_node
,
2820 if (left
->u
.unary_expression
.type
!= UNARY_STRING
) {
2822 "Unexpected unary expression type: type=%d",
2823 left
->u
.unary_expression
.type
);
2828 if (!strcmp(left
->u
.unary_expression
.u
.string
, "byte_order")) {
2829 if (_IS_SET(&set
, _FLOAT_BYTE_ORDER_SET
)) {
2830 _BT_LOGE_DUP_ATTR(left
, "byte_order",
2831 "floating point number field type");
2836 byte_order
= get_real_byte_order(ctx
, right
);
2837 if (byte_order
== BT_BYTE_ORDER_UNKNOWN
) {
2838 _BT_LOGE_NODE(right
,
2839 "Invalid `byte_order` attribute in floating point number field type: "
2845 _SET(&set
, _FLOAT_BYTE_ORDER_SET
);
2846 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2848 if (_IS_SET(&set
, _FLOAT_EXP_DIG_SET
)) {
2849 _BT_LOGE_DUP_ATTR(left
, "exp_dig",
2850 "floating point number field type");
2855 if (right
->u
.unary_expression
.type
!=
2856 UNARY_UNSIGNED_CONSTANT
) {
2857 _BT_LOGE_NODE(right
,
2858 "Invalid `exp_dig` attribute in floating point number field type: "
2859 "expecting unsigned constant integer: "
2861 right
->u
.unary_expression
.type
);
2866 exp_dig
= right
->u
.unary_expression
.u
.unsigned_constant
;
2867 _SET(&set
, _FLOAT_EXP_DIG_SET
);
2868 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2870 if (_IS_SET(&set
, _FLOAT_MANT_DIG_SET
)) {
2871 _BT_LOGE_DUP_ATTR(left
, "mant_dig",
2872 "floating point number field type");
2877 if (right
->u
.unary_expression
.type
!=
2878 UNARY_UNSIGNED_CONSTANT
) {
2879 _BT_LOGE_NODE(right
,
2880 "Invalid `mant_dig` attribute in floating point number field type: "
2881 "expecting unsigned constant integer: "
2883 right
->u
.unary_expression
.type
);
2888 mant_dig
= right
->u
.unary_expression
.u
.
2890 _SET(&set
, _FLOAT_MANT_DIG_SET
);
2891 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2893 if (_IS_SET(&set
, _FLOAT_ALIGN_SET
)) {
2894 _BT_LOGE_DUP_ATTR(left
, "align",
2895 "floating point number field type");
2900 if (right
->u
.unary_expression
.type
!=
2901 UNARY_UNSIGNED_CONSTANT
) {
2902 _BT_LOGE_NODE(right
,
2903 "Invalid `align` attribute in floating point number field type: "
2904 "expecting unsigned constant integer: "
2906 right
->u
.unary_expression
.type
);
2911 alignment
= right
->u
.unary_expression
.u
.
2914 if (!is_align_valid(alignment
)) {
2915 _BT_LOGE_NODE(right
,
2916 "Invalid `align` attribute in floating point number field type: "
2917 "expecting power of two: "
2918 "align=%" PRIu64
, alignment
);
2923 _SET(&set
, _FLOAT_ALIGN_SET
);
2926 "Unknown attribute in floating point number field type: "
2928 left
->u
.unary_expression
.u
.string
);
2932 if (!_IS_SET(&set
, _FLOAT_MANT_DIG_SET
)) {
2933 BT_LOGE_STR("Missing `mant_dig` attribute in floating point number field type.");
2938 if (!_IS_SET(&set
, _FLOAT_EXP_DIG_SET
)) {
2939 BT_LOGE_STR("Missing `exp_dig` attribute in floating point number field type.");
2944 if (!_IS_SET(&set
, _INTEGER_ALIGN_SET
)) {
2945 if ((mant_dig
+ exp_dig
) % CHAR_BIT
) {
2946 /* Bit-packed alignment */
2949 /* Byte-packed alignment */
2950 alignment
= CHAR_BIT
;
2954 *float_decl
= bt_field_type_floating_point_create();
2956 BT_LOGE_STR("Cannot create floating point number field type.");
2961 ret
= bt_field_type_floating_point_set_exponent_digits(
2962 *float_decl
, exp_dig
);
2963 ret
|= bt_field_type_floating_point_set_mantissa_digits(
2964 *float_decl
, mant_dig
);
2965 ret
|= bt_field_type_set_byte_order(*float_decl
, byte_order
);
2966 ret
|= bt_field_type_set_alignment(*float_decl
, alignment
);
2968 BT_LOGE_STR("Cannot configure floating point number field type.");
2976 BT_PUT(*float_decl
);
2982 int visit_string_decl(struct ctx
*ctx
,
2983 struct bt_list_head
*expressions
,
2984 struct bt_field_type
**string_decl
)
2988 struct ctf_node
*expression
;
2989 enum bt_string_encoding encoding
= BT_STRING_ENCODING_UTF8
;
2991 *string_decl
= NULL
;
2993 bt_list_for_each_entry(expression
, expressions
, siblings
) {
2994 struct ctf_node
*left
, *right
;
2996 left
= _BT_LIST_FIRST_ENTRY(&expression
->u
.ctf_expression
.left
,
2997 struct ctf_node
, siblings
);
2998 right
= _BT_LIST_FIRST_ENTRY(
2999 &expression
->u
.ctf_expression
.right
, struct ctf_node
,
3002 if (left
->u
.unary_expression
.type
!= UNARY_STRING
) {
3004 "Unexpected unary expression type: type=%d",
3005 left
->u
.unary_expression
.type
);
3010 if (!strcmp(left
->u
.unary_expression
.u
.string
, "encoding")) {
3013 if (_IS_SET(&set
, _STRING_ENCODING_SET
)) {
3014 _BT_LOGE_DUP_ATTR(left
, "encoding",
3015 "string field type");
3020 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
3021 _BT_LOGE_NODE(right
,
3022 "Invalid `encoding` attribute in string field type: "
3023 "expecting unary string.");
3028 s_right
= concatenate_unary_strings(
3029 &expression
->u
.ctf_expression
.right
);
3031 _BT_LOGE_NODE(right
,
3032 "Unexpected unary expression for string field type's `encoding` attribute.");
3037 if (!strcmp(s_right
, "UTF8") ||
3038 !strcmp(s_right
, "utf8") ||
3039 !strcmp(s_right
, "utf-8") ||
3040 !strcmp(s_right
, "UTF-8")) {
3041 encoding
= BT_STRING_ENCODING_UTF8
;
3042 } else if (!strcmp(s_right
, "ASCII") ||
3043 !strcmp(s_right
, "ascii")) {
3044 encoding
= BT_STRING_ENCODING_ASCII
;
3045 } else if (!strcmp(s_right
, "none")) {
3046 encoding
= BT_STRING_ENCODING_NONE
;
3048 _BT_LOGE_NODE(right
,
3049 "Invalid `encoding` attribute in string field type: "
3050 "unknown encoding: encoding=\"%s\"",
3058 _SET(&set
, _STRING_ENCODING_SET
);
3061 "Unknown attribute in string field type: "
3063 left
->u
.unary_expression
.u
.string
);
3067 *string_decl
= bt_field_type_string_create();
3068 if (!*string_decl
) {
3069 BT_LOGE_STR("Cannot create string field type.");
3074 ret
= bt_field_type_string_set_encoding(*string_decl
, encoding
);
3076 BT_LOGE_STR("Cannot configure string field type.");
3084 BT_PUT(*string_decl
);
3090 int visit_type_specifier_list(struct ctx
*ctx
,
3091 struct ctf_node
*ts_list
,
3092 struct bt_field_type
**decl
)
3095 struct ctf_node
*first
, *node
;
3099 if (ts_list
->type
!= NODE_TYPE_SPECIFIER_LIST
) {
3100 _BT_LOGE_NODE(ts_list
,
3101 "Unexpected node type: node-type=%d", ts_list
->type
);
3106 first
= _BT_LIST_FIRST_ENTRY(&ts_list
->u
.type_specifier_list
.head
,
3107 struct ctf_node
, siblings
);
3108 if (first
->type
!= NODE_TYPE_SPECIFIER
) {
3109 _BT_LOGE_NODE(first
,
3110 "Unexpected node type: node-type=%d", first
->type
);
3115 node
= first
->u
.type_specifier
.node
;
3117 switch (first
->u
.type_specifier
.type
) {
3118 case TYPESPEC_INTEGER
:
3119 ret
= visit_integer_decl(ctx
, &node
->u
.integer
.expressions
,
3126 case TYPESPEC_FLOATING_POINT
:
3127 ret
= visit_floating_point_number_decl(ctx
,
3128 &node
->u
.floating_point
.expressions
, decl
);
3134 case TYPESPEC_STRING
:
3135 ret
= visit_string_decl(ctx
,
3136 &node
->u
.string
.expressions
, decl
);
3142 case TYPESPEC_STRUCT
:
3143 ret
= visit_struct_decl(ctx
, node
->u
._struct
.name
,
3144 &node
->u
._struct
.declaration_list
,
3145 node
->u
._struct
.has_body
,
3146 &node
->u
._struct
.min_align
, decl
);
3152 case TYPESPEC_VARIANT
:
3153 ret
= visit_variant_decl(ctx
, node
->u
.variant
.name
,
3154 node
->u
.variant
.choice
,
3155 &node
->u
.variant
.declaration_list
,
3156 node
->u
.variant
.has_body
, decl
);
3163 ret
= visit_enum_decl(ctx
, node
->u
._enum
.enum_id
,
3164 node
->u
._enum
.container_type
,
3165 &node
->u
._enum
.enumerator_list
,
3166 node
->u
._enum
.has_body
, decl
);
3174 case TYPESPEC_SHORT
:
3177 case TYPESPEC_FLOAT
:
3178 case TYPESPEC_DOUBLE
:
3179 case TYPESPEC_SIGNED
:
3180 case TYPESPEC_UNSIGNED
:
3182 case TYPESPEC_COMPLEX
:
3183 case TYPESPEC_IMAGINARY
:
3184 case TYPESPEC_CONST
:
3185 case TYPESPEC_ID_TYPE
:
3186 ret
= visit_type_specifier(ctx
, ts_list
, decl
);
3188 _BT_LOGE_NODE(first
,
3189 "Cannot visit type specifier: ret=%d",
3196 _BT_LOGE_NODE(first
,
3197 "Unexpected type specifier type: node-type=%d",
3198 first
->u
.type_specifier
.type
);
3214 int visit_event_decl_entry(struct ctx
*ctx
, struct ctf_node
*node
,
3215 struct bt_event_class
*event_class
, int64_t *stream_id
,
3220 _BT_FIELD_TYPE_INIT(decl
);
3222 switch (node
->type
) {
3224 ret
= visit_typedef(ctx
, node
->u
._typedef
.type_specifier_list
,
3225 &node
->u
._typedef
.type_declarators
);
3228 "Cannot add type definition found in event class.");
3232 case NODE_TYPEALIAS
:
3233 ret
= visit_typealias(ctx
, node
->u
.typealias
.target
,
3234 node
->u
.typealias
.alias
);
3237 "Cannot add type alias found in event class.");
3241 case NODE_CTF_EXPRESSION
:
3243 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
3245 _BT_LOGE_NODE(node
, "Cannot concatenate unary strings.");
3250 if (!strcmp(left
, "name")) {
3251 /* This is already known at this stage */
3252 if (_IS_SET(set
, _EVENT_NAME_SET
)) {
3253 _BT_LOGE_DUP_ATTR(node
, "name",
3259 _SET(set
, _EVENT_NAME_SET
);
3260 } else if (!strcmp(left
, "id")) {
3263 if (_IS_SET(set
, _EVENT_ID_SET
)) {
3264 _BT_LOGE_DUP_ATTR(node
, "id",
3270 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
3272 /* Only read "id" if get_unary_unsigned() succeeded. */
3273 if (ret
|| (!ret
&& id
< 0)) {
3275 "Unexpected unary expression for event class's `id` attribute.");
3280 ret
= bt_event_class_set_id(event_class
, id
);
3283 "Cannot set event class's ID: "
3288 _SET(set
, _EVENT_ID_SET
);
3289 } else if (!strcmp(left
, "stream_id")) {
3290 if (_IS_SET(set
, _EVENT_STREAM_ID_SET
)) {
3291 _BT_LOGE_DUP_ATTR(node
, "stream_id",
3297 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
3298 (uint64_t *) stream_id
);
3300 * Only read "stream_id" if get_unary_unsigned()
3303 if (ret
|| (!ret
&& *stream_id
< 0)) {
3305 "Unexpected unary expression for event class's `stream_id` attribute.");
3310 _SET(set
, _EVENT_STREAM_ID_SET
);
3311 } else if (!strcmp(left
, "context")) {
3312 if (_IS_SET(set
, _EVENT_CONTEXT_SET
)) {
3314 "Duplicate `context` entry in event class.");
3319 ret
= visit_type_specifier_list(ctx
,
3320 _BT_LIST_FIRST_ENTRY(
3321 &node
->u
.ctf_expression
.right
,
3322 struct ctf_node
, siblings
),
3326 "Cannot create event class's context field type.");
3331 ret
= bt_event_class_set_context_field_type(
3336 "Cannot set event class's context field type.");
3340 _SET(set
, _EVENT_CONTEXT_SET
);
3341 } else if (!strcmp(left
, "fields")) {
3342 if (_IS_SET(set
, _EVENT_FIELDS_SET
)) {
3344 "Duplicate `fields` entry in event class.");
3349 ret
= visit_type_specifier_list(ctx
,
3350 _BT_LIST_FIRST_ENTRY(
3351 &node
->u
.ctf_expression
.right
,
3352 struct ctf_node
, siblings
),
3356 "Cannot create event class's payload field type.");
3361 ret
= bt_event_class_set_payload_field_type(
3366 "Cannot set event class's payload field type.");
3370 _SET(set
, _EVENT_FIELDS_SET
);
3371 } else if (!strcmp(left
, "loglevel")) {
3372 uint64_t loglevel_value
;
3373 enum bt_event_class_log_level log_level
=
3374 BT_EVENT_CLASS_LOG_LEVEL_UNSPECIFIED
;
3376 if (_IS_SET(set
, _EVENT_LOGLEVEL_SET
)) {
3377 _BT_LOGE_DUP_ATTR(node
, "loglevel",
3383 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
3387 "Unexpected unary expression for event class's `loglevel` attribute.");
3392 switch (loglevel_value
) {
3394 log_level
= BT_EVENT_CLASS_LOG_LEVEL_EMERGENCY
;
3397 log_level
= BT_EVENT_CLASS_LOG_LEVEL_ALERT
;
3400 log_level
= BT_EVENT_CLASS_LOG_LEVEL_CRITICAL
;
3403 log_level
= BT_EVENT_CLASS_LOG_LEVEL_ERROR
;
3406 log_level
= BT_EVENT_CLASS_LOG_LEVEL_WARNING
;
3409 log_level
= BT_EVENT_CLASS_LOG_LEVEL_NOTICE
;
3412 log_level
= BT_EVENT_CLASS_LOG_LEVEL_INFO
;
3415 log_level
= BT_EVENT_CLASS_LOG_LEVEL_DEBUG_SYSTEM
;
3418 log_level
= BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROGRAM
;
3421 log_level
= BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROCESS
;
3424 log_level
= BT_EVENT_CLASS_LOG_LEVEL_DEBUG_MODULE
;
3427 log_level
= BT_EVENT_CLASS_LOG_LEVEL_DEBUG_UNIT
;
3430 log_level
= BT_EVENT_CLASS_LOG_LEVEL_DEBUG_FUNCTION
;
3433 log_level
= BT_EVENT_CLASS_LOG_LEVEL_DEBUG_LINE
;
3436 log_level
= BT_EVENT_CLASS_LOG_LEVEL_DEBUG
;
3439 _BT_LOGW_NODE(node
, "Not setting event class's log level because its value is unknown: "
3440 "log-level=%" PRIu64
, loglevel_value
);
3443 if (log_level
!= BT_EVENT_CLASS_LOG_LEVEL_UNSPECIFIED
) {
3444 ret
= bt_event_class_set_log_level(
3445 event_class
, log_level
);
3448 "Cannot set event class's log level.");
3453 _SET(set
, _EVENT_LOGLEVEL_SET
);
3454 } else if (!strcmp(left
, "model.emf.uri")) {
3457 if (_IS_SET(set
, _EVENT_MODEL_EMF_URI_SET
)) {
3458 _BT_LOGE_DUP_ATTR(node
, "model.emf.uri",
3464 right
= concatenate_unary_strings(
3465 &node
->u
.ctf_expression
.right
);
3468 "Unexpected unary expression for event class's `model.emf.uri` attribute.");
3473 if (strlen(right
) == 0) {
3475 "Not setting event class's EMF URI because it's empty.");
3477 ret
= bt_event_class_set_emf_uri(
3478 event_class
, right
);
3481 "Cannot set event class's EMF URI.");
3487 _SET(set
, _EVENT_MODEL_EMF_URI_SET
);
3490 "Unknown attribute in event class: "
3491 "attr-name=\"%s\"", left
);
3516 char *get_event_decl_name(struct ctx
*ctx
, struct ctf_node
*node
)
3520 struct ctf_node
*iter
;
3521 struct bt_list_head
*decl_list
= &node
->u
.event
.declaration_list
;
3523 bt_list_for_each_entry(iter
, decl_list
, siblings
) {
3524 if (iter
->type
!= NODE_CTF_EXPRESSION
) {
3528 left
= concatenate_unary_strings(&iter
->u
.ctf_expression
.left
);
3531 "Cannot concatenate unary strings.");
3535 if (!strcmp(left
, "name")) {
3536 name
= concatenate_unary_strings(
3537 &iter
->u
.ctf_expression
.right
);
3540 "Unexpected unary expression for event class's `name` attribute.");
3562 int reset_event_decl_types(struct ctx
*ctx
,
3563 struct bt_event_class
*event_class
)
3568 ret
= bt_event_class_set_context_field_type(event_class
, NULL
);
3570 BT_LOGE("Cannot reset initial event class's context field type: "
3571 "event-name=\"%s\"",
3572 bt_event_class_get_name(event_class
));
3576 /* Event payload. */
3577 ret
= bt_event_class_set_payload_field_type(event_class
, NULL
);
3579 BT_LOGE("Cannot reset initial event class's payload field type: "
3580 "event-name=\"%s\"",
3581 bt_event_class_get_name(event_class
));
3589 int reset_stream_decl_types(struct ctx
*ctx
,
3590 struct bt_stream_class
*stream_class
)
3594 /* Packet context. */
3595 ret
= bt_stream_class_set_packet_context_field_type(stream_class
, NULL
);
3597 BT_LOGE_STR("Cannot reset initial stream class's packet context field type.");
3602 ret
= bt_stream_class_set_event_header_field_type(stream_class
, NULL
);
3604 BT_LOGE_STR("Cannot reset initial stream class's event header field type.");
3608 /* Event context. */
3609 ret
= bt_stream_class_set_event_context_field_type(stream_class
, NULL
);
3611 BT_LOGE_STR("Cannot reset initial stream class's event context field type.");
3619 struct bt_stream_class
*create_reset_stream_class(struct ctx
*ctx
)
3622 struct bt_stream_class
*stream_class
;
3624 stream_class
= bt_stream_class_create(NULL
);
3625 if (!stream_class
) {
3626 BT_LOGE_STR("Cannot create empty stream class.");
3631 * Set packet context, event header, and event context to NULL to
3632 * override the default ones.
3634 ret
= reset_stream_decl_types(ctx
, stream_class
);
3639 return stream_class
;
3642 BT_PUT(stream_class
);
3648 int visit_event_decl(struct ctx
*ctx
, struct ctf_node
*node
)
3653 struct ctf_node
*iter
;
3654 int64_t stream_id
= -1;
3655 char *event_name
= NULL
;
3656 struct bt_event_class
*event_class
= NULL
;
3657 struct bt_event_class
*eevent_class
;
3658 struct bt_stream_class
*stream_class
= NULL
;
3659 struct bt_list_head
*decl_list
= &node
->u
.event
.declaration_list
;
3660 bool pop_scope
= false;
3662 if (node
->visited
) {
3666 node
->visited
= TRUE
;
3667 event_name
= get_event_decl_name(ctx
, node
);
3670 "Missing `name` attribute in event class.");
3675 event_class
= bt_event_class_create(event_name
);
3678 * Unset context and fields to override the default ones.
3680 ret
= reset_event_decl_types(ctx
, event_class
);
3683 "Cannot reset event class's field types: "
3688 ret
= ctx_push_scope(ctx
);
3690 BT_LOGE_STR("Cannot push scope.");
3696 bt_list_for_each_entry(iter
, decl_list
, siblings
) {
3697 ret
= visit_event_decl_entry(ctx
, iter
, event_class
,
3700 _BT_LOGE_NODE(iter
, "Cannot visit event class's entry: "
3706 if (!_IS_SET(&set
, _EVENT_STREAM_ID_SET
)) {
3708 int64_t *new_stream_id
;
3709 struct bt_stream_class
*new_stream_class
;
3710 size_t stream_class_count
=
3711 g_hash_table_size(ctx
->stream_classes
) +
3712 bt_trace_get_stream_class_count(ctx
->trace
);
3715 * Allow missing stream_id if there is only a single
3718 switch (stream_class_count
) {
3720 /* Create implicit stream class if there's none */
3722 new_stream_class
= create_reset_stream_class(ctx
);
3723 if (!new_stream_class
) {
3725 "Cannot create empty stream class.");
3730 ret
= bt_stream_class_set_id(new_stream_class
,
3734 "Cannot set stream class's ID: "
3735 "id=0, ret=%d", ret
);
3736 BT_PUT(new_stream_class
);
3740 new_stream_id
= g_new0(int64_t, 1);
3741 if (!new_stream_id
) {
3742 BT_LOGE_STR("Failed to allocate a int64_t.");
3747 *new_stream_id
= stream_id
;
3749 /* Move reference to visitor's context */
3750 g_hash_table_insert(ctx
->stream_classes
,
3751 new_stream_id
, new_stream_class
);
3752 new_stream_id
= NULL
;
3753 new_stream_class
= NULL
;
3756 /* Single stream class: get its ID */
3757 if (g_hash_table_size(ctx
->stream_classes
) == 1) {
3758 keys
= g_hash_table_get_keys(ctx
->stream_classes
);
3759 stream_id
= *((int64_t *) keys
->data
);
3762 BT_ASSERT(bt_trace_get_stream_class_count(
3765 bt_trace_get_stream_class_by_index(
3767 BT_ASSERT(stream_class
);
3768 stream_id
= bt_stream_class_get_id(
3770 BT_PUT(stream_class
);
3775 "Missing `stream_id` attribute in event class.");
3781 BT_ASSERT(stream_id
>= 0);
3783 /* We have the stream ID now; get the stream class if found */
3784 stream_class
= g_hash_table_lookup(ctx
->stream_classes
, &stream_id
);
3785 bt_get(stream_class
);
3786 if (!stream_class
) {
3787 stream_class
= bt_trace_get_stream_class_by_id(ctx
->trace
,
3789 if (!stream_class
) {
3791 "Cannot find stream class at this point: "
3792 "id=%" PRId64
, stream_id
);
3798 BT_ASSERT(stream_class
);
3800 if (!_IS_SET(&set
, _EVENT_ID_SET
)) {
3801 /* Allow only one event without ID per stream */
3802 if (bt_stream_class_get_event_class_count(stream_class
) !=
3805 "Missing `id` attribute in event class.");
3811 ret
= bt_event_class_set_id(event_class
, 0);
3814 "Cannot set event class's ID: id=0, ret=%d",
3820 event_id
= bt_event_class_get_id(event_class
);
3822 _BT_LOGE_NODE(node
, "Cannot get event class's ID.");
3827 eevent_class
= bt_stream_class_get_event_class_by_id(stream_class
,
3830 BT_PUT(eevent_class
);
3832 "Duplicate event class (same ID) in the same stream class: "
3833 "id=%" PRId64
, event_id
);
3838 ret
= bt_stream_class_add_event_class(stream_class
, event_class
);
3839 BT_PUT(event_class
);
3842 "Cannot add event class to stream class: ret=%d", ret
);
3849 bt_put(event_class
);
3857 bt_put(stream_class
);
3862 int auto_map_field_to_trace_clock_class(struct ctx
*ctx
,
3863 struct bt_field_type
*ft
)
3865 struct bt_clock_class
*clock_class_to_map_to
= NULL
;
3866 struct bt_clock_class
*mapped_clock_class
= NULL
;
3868 int64_t clock_class_count
;
3870 if (!ft
|| !bt_field_type_is_integer(ft
)) {
3874 mapped_clock_class
=
3875 bt_field_type_integer_get_mapped_clock_class(ft
);
3876 if (mapped_clock_class
) {
3880 clock_class_count
= bt_trace_get_clock_class_count(ctx
->trace
);
3881 BT_ASSERT(clock_class_count
>= 0);
3883 switch (clock_class_count
) {
3886 * No clock class exists in the trace at this
3887 * point. Create an implicit one at 1 GHz,
3888 * named `default`, and use this clock class.
3890 clock_class_to_map_to
= bt_clock_class_create("default",
3892 if (!clock_class_to_map_to
) {
3893 BT_LOGE_STR("Cannot create a clock class.");
3898 ret
= bt_trace_add_clock_class(ctx
->trace
,
3899 clock_class_to_map_to
);
3901 BT_LOGE_STR("Cannot add clock class to trace.");
3907 * Only one clock class exists in the trace at
3908 * this point: use this one.
3910 clock_class_to_map_to
=
3911 bt_trace_get_clock_class_by_index(ctx
->trace
, 0);
3912 BT_ASSERT(clock_class_to_map_to
);
3916 * Timestamp field not mapped to a clock class
3917 * and there's more than one clock class in the
3918 * trace: this is an error.
3920 BT_LOGE_STR("Timestamp field found with no mapped clock class, "
3921 "but there's more than one clock class in the trace at this point.");
3926 BT_ASSERT(clock_class_to_map_to
);
3927 ret
= bt_field_type_integer_set_mapped_clock_class(ft
,
3928 clock_class_to_map_to
);
3930 BT_LOGE("Cannot map field type's field to trace's clock class: "
3931 "clock-class-name=\"%s\", ret=%d",
3932 bt_clock_class_get_name(clock_class_to_map_to
),
3938 bt_put(clock_class_to_map_to
);
3939 bt_put(mapped_clock_class
);
3944 int auto_map_fields_to_trace_clock_class(struct ctx
*ctx
,
3945 struct bt_field_type
*root_ft
, const char *field_name
)
3954 if (!bt_field_type_is_structure(root_ft
) &&
3955 !bt_field_type_is_variant(root_ft
)) {
3959 if (bt_field_type_is_structure(root_ft
)) {
3960 count
= bt_field_type_structure_get_field_count(root_ft
);
3962 count
= bt_field_type_variant_get_field_count(root_ft
);
3965 BT_ASSERT(count
>= 0);
3967 for (i
= 0; i
< count
; i
++) {
3968 _BT_FIELD_TYPE_INIT(ft
);
3971 if (bt_field_type_is_structure(root_ft
)) {
3972 ret
= bt_field_type_structure_get_field_by_index(
3973 root_ft
, &name
, &ft
, i
);
3974 } else if (bt_field_type_is_variant(root_ft
)) {
3975 ret
= bt_field_type_variant_get_field_by_index(
3976 root_ft
, &name
, &ft
, i
);
3979 BT_ASSERT(ret
== 0);
3981 if (strcmp(name
, field_name
) == 0) {
3982 ret
= auto_map_field_to_trace_clock_class(ctx
, ft
);
3984 BT_LOGE("Cannot automatically map field to trace's clock class: "
3985 "field-name=\"%s\"", field_name
);
3991 ret
= auto_map_fields_to_trace_clock_class(ctx
, ft
, field_name
);
3994 BT_LOGE("Cannot automatically map structure or variant field type's fields to trace's clock class: "
3995 "field-name=\"%s\", root-field-name=\"%s\"",
4006 int visit_stream_decl_entry(struct ctx
*ctx
, struct ctf_node
*node
,
4007 struct bt_stream_class
*stream_class
, int *set
)
4011 _BT_FIELD_TYPE_INIT(decl
);
4013 switch (node
->type
) {
4015 ret
= visit_typedef(ctx
, node
->u
._typedef
.type_specifier_list
,
4016 &node
->u
._typedef
.type_declarators
);
4019 "Cannot add type definition found in stream class.");
4023 case NODE_TYPEALIAS
:
4024 ret
= visit_typealias(ctx
, node
->u
.typealias
.target
,
4025 node
->u
.typealias
.alias
);
4028 "Cannot add type alias found in stream class.");
4032 case NODE_CTF_EXPRESSION
:
4034 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
4036 _BT_LOGE_NODE(node
, "Cannot concatenate unary strings.");
4041 if (!strcmp(left
, "id")) {
4045 if (_IS_SET(set
, _STREAM_ID_SET
)) {
4046 _BT_LOGE_DUP_ATTR(node
, "id",
4047 "stream declaration");
4052 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
4054 /* Only read "id" if get_unary_unsigned() succeeded. */
4055 if (ret
|| (!ret
&& id
< 0)) {
4057 "Unexpected unary expression for stream class's `id` attribute.");
4062 ptr
= g_hash_table_lookup(ctx
->stream_classes
, &id
);
4065 "Duplicate stream class (same ID): id=%" PRId64
,
4071 ret
= bt_stream_class_set_id(stream_class
, id
);
4074 "Cannot set stream class's ID: "
4075 "id=%" PRId64
", ret=%d", id
, ret
);
4079 _SET(set
, _STREAM_ID_SET
);
4080 } else if (!strcmp(left
, "event.header")) {
4081 if (_IS_SET(set
, _STREAM_EVENT_HEADER_SET
)) {
4083 "Duplicate `event.header` entry in stream class.");
4088 ret
= visit_type_specifier_list(ctx
,
4089 _BT_LIST_FIRST_ENTRY(
4090 &node
->u
.ctf_expression
.right
,
4091 struct ctf_node
, siblings
),
4095 "Cannot create stream class's event header field type.");
4100 ret
= auto_map_fields_to_trace_clock_class(ctx
,
4104 "Cannot automatically map specific event header field type fields named `timestamp` to trace's clock class.");
4108 ret
= bt_stream_class_set_event_header_field_type(
4109 stream_class
, decl
);
4113 "Cannot set stream class's event header field type.");
4117 _SET(set
, _STREAM_EVENT_HEADER_SET
);
4118 } else if (!strcmp(left
, "event.context")) {
4119 if (_IS_SET(set
, _STREAM_EVENT_CONTEXT_SET
)) {
4121 "Duplicate `event.context` entry in stream class.");
4126 ret
= visit_type_specifier_list(ctx
,
4127 _BT_LIST_FIRST_ENTRY(
4128 &node
->u
.ctf_expression
.right
,
4129 struct ctf_node
, siblings
),
4133 "Cannot create stream class's event context field type.");
4139 ret
= bt_stream_class_set_event_context_field_type(
4140 stream_class
, decl
);
4144 "Cannot set stream class's event context field type.");
4148 _SET(set
, _STREAM_EVENT_CONTEXT_SET
);
4149 } else if (!strcmp(left
, "packet.context")) {
4150 if (_IS_SET(set
, _STREAM_PACKET_CONTEXT_SET
)) {
4152 "Duplicate `packet.context` entry in stream class.");
4157 ret
= visit_type_specifier_list(ctx
,
4158 _BT_LIST_FIRST_ENTRY(
4159 &node
->u
.ctf_expression
.right
,
4160 struct ctf_node
, siblings
),
4164 "Cannot create stream class's packet context field type.");
4169 ret
= auto_map_fields_to_trace_clock_class(ctx
,
4170 decl
, "timestamp_begin");
4173 "Cannot automatically map specific packet context field type fields named `timestamp_begin` to trace's clock class.");
4177 ret
= auto_map_fields_to_trace_clock_class(ctx
,
4178 decl
, "timestamp_end");
4181 "Cannot automatically map specific packet context field type fields named `timestamp_end` to trace's clock class.");
4185 ret
= bt_stream_class_set_packet_context_field_type(
4186 stream_class
, decl
);
4190 "Cannot set stream class's packet context field type.");
4194 _SET(set
, _STREAM_PACKET_CONTEXT_SET
);
4197 "Unknown attribute in stream class: "
4198 "attr-name=\"%s\"", left
);
4221 int visit_stream_decl(struct ctx
*ctx
, struct ctf_node
*node
)
4227 struct ctf_node
*iter
;
4228 struct bt_stream_class
*stream_class
= NULL
;
4229 struct bt_stream_class
*existing_stream_class
= NULL
;
4230 struct bt_list_head
*decl_list
= &node
->u
.stream
.declaration_list
;
4232 if (node
->visited
) {
4236 node
->visited
= TRUE
;
4237 stream_class
= create_reset_stream_class(ctx
);
4238 if (!stream_class
) {
4239 _BT_LOGE_NODE(node
, "Cannot create empty stream class.");
4244 ret
= ctx_push_scope(ctx
);
4246 BT_LOGE_STR("Cannot push scope.");
4250 bt_list_for_each_entry(iter
, decl_list
, siblings
) {
4251 ret
= visit_stream_decl_entry(ctx
, iter
, stream_class
, &set
);
4254 "Cannot visit stream class's entry: "
4263 if (_IS_SET(&set
, _STREAM_ID_SET
)) {
4264 /* Check that packet header has stream_id field */
4265 _BT_FIELD_TYPE_INIT(stream_id_decl
);
4266 _BT_FIELD_TYPE_INIT(packet_header_decl
);
4268 packet_header_decl
=
4269 bt_trace_get_packet_header_field_type(ctx
->trace
);
4270 if (!packet_header_decl
) {
4272 "Stream class has a `id` attribute, "
4273 "but trace has no packet header field type.");
4278 bt_field_type_structure_get_field_type_by_name(
4279 packet_header_decl
, "stream_id");
4280 BT_PUT(packet_header_decl
);
4281 if (!stream_id_decl
) {
4283 "Stream class has a `id` attribute, "
4284 "but trace's packet header field type has no `stream_id` field.");
4288 if (!bt_field_type_is_integer(stream_id_decl
)) {
4289 BT_PUT(stream_id_decl
);
4291 "Stream class has a `id` attribute, "
4292 "but trace's packet header field type's `stream_id` field is not an integer field type.");
4296 BT_PUT(stream_id_decl
);
4298 /* Allow only _one_ ID-less stream */
4299 if (g_hash_table_size(ctx
->stream_classes
) != 0) {
4301 "Missing `id` attribute in stream class as there's more than one stream class in the trace.");
4306 /* Automatic ID: 0 */
4307 ret
= bt_stream_class_set_id(stream_class
, 0);
4308 BT_ASSERT(ret
== 0);
4311 id
= bt_stream_class_get_id(stream_class
);
4314 "Cannot get stream class's ID.");
4320 * Make sure that this stream class's ID is currently unique in
4323 existing_stream_class
= bt_trace_get_stream_class_by_id(ctx
->trace
,
4325 if (g_hash_table_lookup(ctx
->stream_classes
, &id
) ||
4326 existing_stream_class
) {
4328 "Duplicate stream class (same ID): id=%" PRId64
,
4334 new_id
= g_new0(int64_t, 1);
4336 BT_LOGE_STR("Failed to allocate a int64_t.");
4342 /* Move reference to visitor's context */
4343 g_hash_table_insert(ctx
->stream_classes
, new_id
,
4345 stream_class
= NULL
;
4349 bt_put(stream_class
);
4352 bt_put(existing_stream_class
);
4357 int visit_trace_decl_entry(struct ctx
*ctx
, struct ctf_node
*node
, int *set
)
4361 _BT_FIELD_TYPE_INIT(packet_header_decl
);
4363 switch (node
->type
) {
4365 ret
= visit_typedef(ctx
, node
->u
._typedef
.type_specifier_list
,
4366 &node
->u
._typedef
.type_declarators
);
4369 "Cannot add type definition found in trace (`trace` block).");
4373 case NODE_TYPEALIAS
:
4374 ret
= visit_typealias(ctx
, node
->u
.typealias
.target
,
4375 node
->u
.typealias
.alias
);
4378 "Cannot add type alias found in trace (`trace` block).");
4382 case NODE_CTF_EXPRESSION
:
4384 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
4386 _BT_LOGE_NODE(node
, "Cannot concatenate unary strings.");
4391 if (!strcmp(left
, "major")) {
4392 if (_IS_SET(set
, _TRACE_MAJOR_SET
)) {
4393 _BT_LOGE_DUP_ATTR(node
, "major", "trace");
4398 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
4402 "Unexpected unary expression for trace's `major` attribute.");
4407 _SET(set
, _TRACE_MAJOR_SET
);
4408 } else if (!strcmp(left
, "minor")) {
4409 if (_IS_SET(set
, _TRACE_MINOR_SET
)) {
4410 _BT_LOGE_DUP_ATTR(node
, "minor", "trace");
4415 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
4419 "Unexpected unary expression for trace's `minor` attribute.");
4424 _SET(set
, _TRACE_MINOR_SET
);
4425 } else if (!strcmp(left
, "uuid")) {
4426 if (_IS_SET(set
, _TRACE_UUID_SET
)) {
4427 _BT_LOGE_DUP_ATTR(node
, "uuid", "trace");
4432 ret
= get_unary_uuid(&node
->u
.ctf_expression
.right
,
4436 "Invalid trace's `uuid` attribute.");
4440 ret
= bt_trace_set_uuid(ctx
->trace
, ctx
->trace_uuid
);
4442 _BT_LOGE_NODE(node
, "Cannot set trace's UUID.");
4446 _SET(set
, _TRACE_UUID_SET
);
4447 } else if (!strcmp(left
, "byte_order")) {
4448 /* Native byte order is already known at this stage */
4449 if (_IS_SET(set
, _TRACE_BYTE_ORDER_SET
)) {
4450 _BT_LOGE_DUP_ATTR(node
, "byte_order",
4456 _SET(set
, _TRACE_BYTE_ORDER_SET
);
4457 } else if (!strcmp(left
, "packet.header")) {
4458 if (_IS_SET(set
, _TRACE_PACKET_HEADER_SET
)) {
4460 "Duplicate `packet.header` entry in trace.");
4465 ret
= visit_type_specifier_list(ctx
,
4466 _BT_LIST_FIRST_ENTRY(
4467 &node
->u
.ctf_expression
.right
,
4468 struct ctf_node
, siblings
),
4469 &packet_header_decl
);
4472 "Cannot create trace's packet header field type.");
4476 BT_ASSERT(packet_header_decl
);
4477 ret
= bt_trace_set_packet_header_field_type(ctx
->trace
,
4478 packet_header_decl
);
4479 BT_PUT(packet_header_decl
);
4482 "Cannot set trace's packet header field type.");
4486 _SET(set
, _TRACE_PACKET_HEADER_SET
);
4489 "Unknown attribute in stream class: "
4490 "attr-name=\"%s\"", left
);
4498 _BT_LOGE_NODE(node
, "Unknown expression in trace.");
4507 BT_PUT(packet_header_decl
);
4513 int visit_trace_decl(struct ctx
*ctx
, struct ctf_node
*node
)
4517 struct ctf_node
*iter
;
4518 struct bt_list_head
*decl_list
= &node
->u
.trace
.declaration_list
;
4520 if (node
->visited
) {
4524 node
->visited
= TRUE
;
4526 if (ctx
->is_trace_visited
) {
4527 _BT_LOGE_NODE(node
, "Duplicate trace (`trace` block).");
4532 ret
= ctx_push_scope(ctx
);
4534 BT_LOGE_STR("Cannot push scope.");
4538 bt_list_for_each_entry(iter
, decl_list
, siblings
) {
4539 ret
= visit_trace_decl_entry(ctx
, iter
, &set
);
4541 _BT_LOGE_NODE(iter
, "Cannot visit trace's entry (`trace` block): "
4550 if (!_IS_SET(&set
, _TRACE_MAJOR_SET
)) {
4552 "Missing `major` attribute in trace (`trace` block).");
4557 if (!_IS_SET(&set
, _TRACE_MINOR_SET
)) {
4559 "Missing `minor` attribute in trace (`trace` block).");
4564 if (!_IS_SET(&set
, _TRACE_BYTE_ORDER_SET
)) {
4566 "Missing `byte_order` attribute in trace (`trace` block).");
4571 ctx
->is_trace_visited
= TRUE
;
4581 int visit_env(struct ctx
*ctx
, struct ctf_node
*node
)
4585 struct ctf_node
*entry_node
;
4586 struct bt_list_head
*decl_list
= &node
->u
.env
.declaration_list
;
4588 if (node
->visited
) {
4592 node
->visited
= TRUE
;
4594 bt_list_for_each_entry(entry_node
, decl_list
, siblings
) {
4595 struct bt_list_head
*right_head
=
4596 &entry_node
->u
.ctf_expression
.right
;
4598 if (entry_node
->type
!= NODE_CTF_EXPRESSION
) {
4599 _BT_LOGE_NODE(entry_node
,
4600 "Wrong expression in environment entry: "
4601 "node-type=%d", entry_node
->type
);
4606 left
= concatenate_unary_strings(
4607 &entry_node
->u
.ctf_expression
.left
);
4609 _BT_LOGE_NODE(entry_node
,
4610 "Cannot get environment entry's name.");
4615 if (is_unary_string(right_head
)) {
4616 char *right
= concatenate_unary_strings(right_head
);
4619 _BT_LOGE_NODE(entry_node
,
4620 "Unexpected unary expression for environment entry's value: "
4621 "name=\"%s\"", left
);
4626 if (strcmp(left
, "tracer_name") == 0) {
4627 if (strncmp(right
, "lttng", 5) == 0) {
4628 BT_LOGI("Detected LTTng trace from `%s` environment value: "
4629 "tracer-name=\"%s\"",
4635 ret
= bt_trace_set_environment_field_string(
4636 ctx
->trace
, left
, right
);
4640 _BT_LOGE_NODE(entry_node
,
4641 "Cannot add string environment entry to trace: "
4642 "name=\"%s\", ret=%d", left
, ret
);
4645 } else if (is_unary_unsigned(right_head
) ||
4646 is_unary_signed(right_head
)) {
4649 if (is_unary_unsigned(right_head
)) {
4650 ret
= get_unary_unsigned(right_head
,
4653 ret
= get_unary_signed(right_head
, &v
);
4656 _BT_LOGE_NODE(entry_node
,
4657 "Unexpected unary expression for environment entry's value: "
4658 "name=\"%s\"", left
);
4663 ret
= bt_trace_set_environment_field_integer(
4664 ctx
->trace
, left
, v
);
4666 _BT_LOGE_NODE(entry_node
,
4667 "Cannot add integer environment entry to trace: "
4668 "name=\"%s\", ret=%d", left
, ret
);
4672 _BT_LOGW_NODE(entry_node
,
4673 "Environment entry has unknown type: "
4674 "name=\"%s\"", left
);
4691 int set_trace_byte_order(struct ctx
*ctx
, struct ctf_node
*trace_node
)
4696 struct ctf_node
*node
;
4697 struct bt_list_head
*decl_list
= &trace_node
->u
.trace
.declaration_list
;
4699 bt_list_for_each_entry(node
, decl_list
, siblings
) {
4700 if (node
->type
== NODE_CTF_EXPRESSION
) {
4701 struct ctf_node
*right_node
;
4703 left
= concatenate_unary_strings(
4704 &node
->u
.ctf_expression
.left
);
4707 "Cannot concatenate unary strings.");
4712 if (!strcmp(left
, "byte_order")) {
4713 enum bt_byte_order bo
;
4715 if (_IS_SET(&set
, _TRACE_BYTE_ORDER_SET
)) {
4716 _BT_LOGE_DUP_ATTR(node
, "byte_order",
4722 _SET(&set
, _TRACE_BYTE_ORDER_SET
);
4723 right_node
= _BT_LIST_FIRST_ENTRY(
4724 &node
->u
.ctf_expression
.right
,
4725 struct ctf_node
, siblings
);
4726 bo
= byte_order_from_unary_expr(right_node
);
4727 if (bo
== BT_BYTE_ORDER_UNKNOWN
) {
4729 "Invalid `byte_order` attribute in trace (`trace` block): "
4730 "expecting `le`, `be`, or `network`.");
4733 } else if (bo
== BT_BYTE_ORDER_NATIVE
) {
4735 "Invalid `byte_order` attribute in trace (`trace` block): "
4736 "cannot be set to `native` here.");
4742 ret
= bt_trace_set_native_byte_order(
4746 "Cannot set trace's byte order: "
4757 if (!_IS_SET(&set
, _TRACE_BYTE_ORDER_SET
)) {
4758 _BT_LOGE_NODE(trace_node
,
4759 "Missing `byte_order` attribute in trace (`trace` block).");
4773 int visit_clock_decl_entry(struct ctx
*ctx
, struct ctf_node
*entry_node
,
4774 struct bt_clock_class
*clock
, int *set
)
4779 if (entry_node
->type
!= NODE_CTF_EXPRESSION
) {
4780 _BT_LOGE_NODE(entry_node
,
4781 "Unexpected node type: node-type=%d",
4787 left
= concatenate_unary_strings(&entry_node
->u
.ctf_expression
.left
);
4789 _BT_LOGE_NODE(entry_node
, "Cannot concatenate unary strings.");
4794 if (!strcmp(left
, "name")) {
4797 if (_IS_SET(set
, _CLOCK_NAME_SET
)) {
4798 _BT_LOGE_DUP_ATTR(entry_node
, "name", "clock class");
4803 right
= concatenate_unary_strings(
4804 &entry_node
->u
.ctf_expression
.right
);
4806 _BT_LOGE_NODE(entry_node
,
4807 "Unexpected unary expression for clock class's `name` attribute.");
4812 ret
= bt_clock_class_set_name(clock
, right
);
4814 _BT_LOGE_NODE(entry_node
,
4815 "cannot set clock class's name");
4821 _SET(set
, _CLOCK_NAME_SET
);
4822 } else if (!strcmp(left
, "uuid")) {
4823 unsigned char uuid
[BABELTRACE_UUID_LEN
];
4825 if (_IS_SET(set
, _CLOCK_UUID_SET
)) {
4826 _BT_LOGE_DUP_ATTR(entry_node
, "uuid", "clock class");
4831 ret
= get_unary_uuid(&entry_node
->u
.ctf_expression
.right
, uuid
);
4833 _BT_LOGE_NODE(entry_node
,
4834 "Invalid clock class's `uuid` attribute.");
4838 ret
= bt_clock_class_set_uuid(clock
, uuid
);
4840 _BT_LOGE_NODE(entry_node
,
4841 "Cannot set clock class's UUID.");
4845 _SET(set
, _CLOCK_UUID_SET
);
4846 } else if (!strcmp(left
, "description")) {
4849 if (_IS_SET(set
, _CLOCK_DESCRIPTION_SET
)) {
4850 _BT_LOGE_DUP_ATTR(entry_node
, "description",
4856 right
= concatenate_unary_strings(
4857 &entry_node
->u
.ctf_expression
.right
);
4859 _BT_LOGE_NODE(entry_node
,
4860 "Unexpected unary expression for clock class's `description` attribute.");
4865 ret
= bt_clock_class_set_description(clock
, right
);
4867 _BT_LOGE_NODE(entry_node
,
4868 "Cannot set clock class's description.");
4874 _SET(set
, _CLOCK_DESCRIPTION_SET
);
4875 } else if (!strcmp(left
, "freq")) {
4876 uint64_t freq
= -1ULL;
4878 if (_IS_SET(set
, _CLOCK_FREQ_SET
)) {
4879 _BT_LOGE_DUP_ATTR(entry_node
, "freq", "clock class");
4884 ret
= get_unary_unsigned(
4885 &entry_node
->u
.ctf_expression
.right
, &freq
);
4887 _BT_LOGE_NODE(entry_node
,
4888 "Unexpected unary expression for clock class's `freq` attribute.");
4893 if (freq
== -1ULL || freq
== 0) {
4894 _BT_LOGE_NODE(entry_node
,
4895 "Invalid clock class frequency: freq=%" PRIu64
,
4901 ret
= bt_clock_class_set_frequency(clock
, freq
);
4903 _BT_LOGE_NODE(entry_node
,
4904 "Cannot set clock class's frequency.");
4908 _SET(set
, _CLOCK_FREQ_SET
);
4909 } else if (!strcmp(left
, "precision")) {
4912 if (_IS_SET(set
, _CLOCK_PRECISION_SET
)) {
4913 _BT_LOGE_DUP_ATTR(entry_node
, "precision",
4919 ret
= get_unary_unsigned(
4920 &entry_node
->u
.ctf_expression
.right
, &precision
);
4922 _BT_LOGE_NODE(entry_node
,
4923 "Unexpected unary expression for clock class's `precision` attribute.");
4928 ret
= bt_clock_class_set_precision(clock
, precision
);
4930 _BT_LOGE_NODE(entry_node
,
4931 "Cannot set clock class's precision.");
4935 _SET(set
, _CLOCK_PRECISION_SET
);
4936 } else if (!strcmp(left
, "offset_s")) {
4939 if (_IS_SET(set
, _CLOCK_OFFSET_S_SET
)) {
4940 _BT_LOGE_DUP_ATTR(entry_node
, "offset_s",
4946 ret
= get_unary_signed(
4947 &entry_node
->u
.ctf_expression
.right
, &offset_s
);
4949 _BT_LOGE_NODE(entry_node
,
4950 "Unexpected unary expression for clock class's `offset_s` attribute.");
4955 ret
= bt_clock_class_set_offset_s(clock
, offset_s
);
4957 _BT_LOGE_NODE(entry_node
,
4958 "Cannot set clock class's offset in seconds.");
4962 _SET(set
, _CLOCK_OFFSET_S_SET
);
4963 } else if (!strcmp(left
, "offset")) {
4966 if (_IS_SET(set
, _CLOCK_OFFSET_SET
)) {
4967 _BT_LOGE_DUP_ATTR(entry_node
, "offset", "clock class");
4972 ret
= get_unary_signed(
4973 &entry_node
->u
.ctf_expression
.right
, &offset
);
4975 _BT_LOGE_NODE(entry_node
,
4976 "Unexpected unary expression for clock class's `offset` attribute.");
4981 ret
= bt_clock_class_set_offset_cycles(clock
, offset
);
4983 _BT_LOGE_NODE(entry_node
,
4984 "Cannot set clock class's offset in cycles.");
4988 _SET(set
, _CLOCK_OFFSET_SET
);
4989 } else if (!strcmp(left
, "absolute")) {
4990 struct ctf_node
*right
;
4992 if (_IS_SET(set
, _CLOCK_ABSOLUTE_SET
)) {
4993 _BT_LOGE_DUP_ATTR(entry_node
, "absolute",
4999 right
= _BT_LIST_FIRST_ENTRY(
5000 &entry_node
->u
.ctf_expression
.right
,
5001 struct ctf_node
, siblings
);
5002 ret
= get_boolean(right
);
5004 _BT_LOGE_NODE(entry_node
,
5005 "Unexpected unary expression for clock class's `absolute` attribute.");
5010 ret
= bt_clock_class_set_is_absolute(clock
, ret
);
5012 _BT_LOGE_NODE(entry_node
,
5013 "Cannot set clock class's absolute flag.");
5017 _SET(set
, _CLOCK_ABSOLUTE_SET
);
5019 _BT_LOGW_NODE(entry_node
,
5020 "Unknown attribute in clock class: attr-name=\"%s\"",
5036 int64_t cycles_from_ns(uint64_t frequency
, int64_t ns
)
5041 if (frequency
== 1000000000ULL) {
5044 cycles
= (uint64_t) (((double) ns
* (double) frequency
) / 1e9
);
5051 int apply_clock_class_offset(struct ctx
*ctx
, struct bt_clock_class
*clock
)
5055 int64_t offset_cycles
;
5056 int64_t offset_to_apply
;
5058 freq
= bt_clock_class_get_frequency(clock
);
5059 if (freq
== -1ULL) {
5060 BT_LOGE_STR("Cannot get clock class's frequency.");
5065 ret
= bt_clock_class_get_offset_cycles(clock
, &offset_cycles
);
5067 BT_LOGE_STR("Cannot get clock class's offset in cycles.");
5073 ctx
->decoder_config
.clock_class_offset_s
* 1000000000LL +
5074 ctx
->decoder_config
.clock_class_offset_ns
;
5075 offset_cycles
+= cycles_from_ns(freq
, offset_to_apply
);
5076 ret
= bt_clock_class_set_offset_cycles(clock
, offset_cycles
);
5083 int visit_clock_decl(struct ctx
*ctx
, struct ctf_node
*clock_node
)
5087 struct bt_clock_class
*clock
;
5088 struct ctf_node
*entry_node
;
5089 struct bt_list_head
*decl_list
= &clock_node
->u
.clock
.declaration_list
;
5090 const char *clock_class_name
;
5092 if (clock_node
->visited
) {
5096 clock_node
->visited
= TRUE
;
5098 /* CTF 1.8's default frequency for a clock class is 1 GHz */
5099 clock
= bt_clock_class_create(NULL
, 1000000000);
5101 _BT_LOGE_NODE(clock_node
,
5102 "Cannot create default clock class.");
5107 bt_list_for_each_entry(entry_node
, decl_list
, siblings
) {
5108 ret
= visit_clock_decl_entry(ctx
, entry_node
, clock
, &set
);
5110 _BT_LOGE_NODE(entry_node
,
5111 "Cannot visit clock class's entry: ret=%d",
5117 if (!_IS_SET(&set
, _CLOCK_NAME_SET
)) {
5118 _BT_LOGE_NODE(clock_node
,
5119 "Missing `name` attribute in clock class.");
5124 clock_class_name
= bt_clock_class_get_name(clock
);
5125 BT_ASSERT(clock_class_name
);
5126 if (ctx
->is_lttng
&& strcmp(clock_class_name
, "monotonic") == 0) {
5128 * Old versions of LTTng forgot to set its clock class
5129 * as absolute, even if it is. This is important because
5130 * it's a condition to be able to sort notifications
5131 * from different sources.
5133 ret
= bt_clock_class_set_is_absolute(clock
, 1);
5135 _BT_LOGE_NODE(clock_node
,
5136 "Cannot set clock class's absolute flag.");
5141 ret
= apply_clock_class_offset(ctx
, clock
);
5143 _BT_LOGE_NODE(clock_node
,
5144 "Cannot apply clock class's custom offset.");
5148 ret
= bt_trace_add_clock_class(ctx
->trace
, clock
);
5150 _BT_LOGE_NODE(clock_node
,
5151 "Cannot add clock class to trace.");
5162 int visit_root_decl(struct ctx
*ctx
, struct ctf_node
*root_decl_node
)
5166 if (root_decl_node
->visited
) {
5170 root_decl_node
->visited
= TRUE
;
5172 switch (root_decl_node
->type
) {
5174 ret
= visit_typedef(ctx
,
5175 root_decl_node
->u
._typedef
.type_specifier_list
,
5176 &root_decl_node
->u
._typedef
.type_declarators
);
5178 _BT_LOGE_NODE(root_decl_node
,
5179 "Cannot add type definition found in root scope.");
5183 case NODE_TYPEALIAS
:
5184 ret
= visit_typealias(ctx
, root_decl_node
->u
.typealias
.target
,
5185 root_decl_node
->u
.typealias
.alias
);
5187 _BT_LOGE_NODE(root_decl_node
,
5188 "Cannot add type alias found in root scope.");
5192 case NODE_TYPE_SPECIFIER_LIST
:
5194 _BT_FIELD_TYPE_INIT(decl
);
5197 * Just add the type specifier to the root
5198 * declaration scope. Put local reference.
5200 ret
= visit_type_specifier_list(ctx
, root_decl_node
, &decl
);
5202 _BT_LOGE_NODE(root_decl_node
,
5203 "Cannot visit root scope's field type: "
5213 _BT_LOGE_NODE(root_decl_node
,
5214 "Unexpected node type: node-type=%d",
5215 root_decl_node
->type
);
5225 int set_trace_name(struct ctx
*ctx
)
5229 struct bt_value
*value
= NULL
;
5231 BT_ASSERT(bt_trace_get_stream_class_count(ctx
->trace
) == 0);
5232 name
= g_string_new(NULL
);
5234 BT_LOGE_STR("Failed to allocate a GString.");
5240 * Check if we have a trace environment string value named `hostname`.
5241 * If so, use it as the trace name's prefix.
5243 value
= bt_trace_get_environment_field_value_by_name(ctx
->trace
,
5245 if (value
&& bt_value_is_string(value
)) {
5246 const char *hostname
;
5248 ret
= bt_value_string_get(value
, &hostname
);
5249 BT_ASSERT(ret
== 0);
5250 g_string_append(name
, hostname
);
5252 if (ctx
->trace_name_suffix
) {
5253 g_string_append_c(name
, G_DIR_SEPARATOR
);
5257 if (ctx
->trace_name_suffix
) {
5258 g_string_append(name
, ctx
->trace_name_suffix
);
5261 ret
= bt_trace_set_name(ctx
->trace
, name
->str
);
5263 BT_LOGE("Cannot set trace's name: name=\"%s\"", name
->str
);
5276 g_string_free(name
, TRUE
);
5283 int move_ctx_stream_classes_to_trace(struct ctx
*ctx
)
5286 GHashTableIter iter
;
5287 gpointer key
, stream_class
;
5289 if (g_hash_table_size(ctx
->stream_classes
) > 0 &&
5290 bt_trace_get_stream_class_count(ctx
->trace
) == 0) {
5292 * We're about to add the first stream class to the
5293 * trace. This will freeze the trace, and after this
5294 * we cannot set the name anymore. At this point,
5295 * set the trace name.
5297 ret
= set_trace_name(ctx
);
5299 BT_LOGE_STR("Cannot set trace's name.");
5304 g_hash_table_iter_init(&iter
, ctx
->stream_classes
);
5306 while (g_hash_table_iter_next(&iter
, &key
, &stream_class
)) {
5307 ret
= bt_trace_add_stream_class(ctx
->trace
,
5310 int64_t id
= bt_stream_class_get_id(stream_class
);
5311 BT_LOGE("Cannot add stream class to trace: id=%" PRId64
,
5317 g_hash_table_remove_all(ctx
->stream_classes
);
5324 struct ctf_visitor_generate_ir
*ctf_visitor_generate_ir_create(
5325 const struct ctf_metadata_decoder_config
*decoder_config
,
5329 struct ctx
*ctx
= NULL
;
5330 struct bt_trace
*trace
;
5332 trace
= bt_trace_create();
5334 BT_LOGE_STR("Cannot create empty trace.");
5338 /* Set packet header to NULL to override the default one */
5339 ret
= bt_trace_set_packet_header_field_type(trace
, NULL
);
5341 BT_LOGE_STR("Cannot reset initial trace's packet header field type.");
5345 /* Create visitor's context */
5346 ctx
= ctx_create(trace
, decoder_config
, name
);
5348 BT_LOGE_STR("Cannot create visitor's context.");
5361 return (void *) ctx
;
5365 void ctf_visitor_generate_ir_destroy(struct ctf_visitor_generate_ir
*visitor
)
5367 ctx_destroy((void *) visitor
);
5371 struct bt_trace
*ctf_visitor_generate_ir_get_trace(
5372 struct ctf_visitor_generate_ir
*visitor
)
5374 struct ctx
*ctx
= (void *) visitor
;
5377 BT_ASSERT(ctx
->trace
);
5378 return bt_get(ctx
->trace
);
5382 int ctf_visitor_generate_ir_visit_node(struct ctf_visitor_generate_ir
*visitor
,
5383 struct ctf_node
*node
)
5386 struct ctx
*ctx
= (void *) visitor
;
5388 BT_LOGI_STR("Visiting metadata's AST to generate CTF IR objects.");
5390 switch (node
->type
) {
5393 struct ctf_node
*iter
;
5394 int got_trace_decl
= FALSE
;
5397 * The first thing we need is the native byte order of
5398 * the trace block, because early type aliases can have
5399 * a `byte_order` attribute set to `native`. If we don't
5400 * have the native byte order yet, and we don't have any
5401 * trace block yet, then fail with EINCOMPLETE.
5403 if (ctx
->trace_bo
== BT_BYTE_ORDER_NATIVE
) {
5404 bt_list_for_each_entry(iter
, &node
->u
.root
.trace
, siblings
) {
5405 if (got_trace_decl
) {
5407 "Duplicate trace (`trace` block).");
5412 ret
= set_trace_byte_order(ctx
, iter
);
5415 "Cannot set trace's native byte order: "
5420 got_trace_decl
= TRUE
;
5423 if (!got_trace_decl
) {
5424 BT_LOGD_STR("Incomplete AST: need trace (`trace` block).");
5430 BT_ASSERT(ctx
->trace_bo
== BT_BYTE_ORDER_LITTLE_ENDIAN
||
5431 ctx
->trace_bo
== BT_BYTE_ORDER_BIG_ENDIAN
);
5432 BT_ASSERT(ctx
->current_scope
&&
5433 ctx
->current_scope
->parent_scope
== NULL
);
5436 bt_list_for_each_entry(iter
, &node
->u
.root
.env
, siblings
) {
5437 ret
= visit_env(ctx
, iter
);
5440 "Cannot visit trace's environment (`env` block) entry: "
5446 BT_ASSERT(ctx
->current_scope
&&
5447 ctx
->current_scope
->parent_scope
== NULL
);
5450 * Visit clock blocks.
5452 bt_list_for_each_entry(iter
, &node
->u
.root
.clock
, siblings
) {
5453 ret
= visit_clock_decl(ctx
, iter
);
5456 "Cannot visit clock class: ret=%d",
5462 BT_ASSERT(ctx
->current_scope
&&
5463 ctx
->current_scope
->parent_scope
== NULL
);
5466 * Visit root declarations next, as they can be used by any
5469 bt_list_for_each_entry(iter
, &node
->u
.root
.declaration_list
,
5471 ret
= visit_root_decl(ctx
, iter
);
5474 "Cannot visit root entry: ret=%d",
5480 BT_ASSERT(ctx
->current_scope
&&
5481 ctx
->current_scope
->parent_scope
== NULL
);
5483 /* Callsite blocks are not supported */
5484 bt_list_for_each_entry(iter
, &node
->u
.root
.callsite
, siblings
) {
5486 "\"callsite\" blocks are not supported as of this version.");
5489 BT_ASSERT(ctx
->current_scope
&&
5490 ctx
->current_scope
->parent_scope
== NULL
);
5493 bt_list_for_each_entry(iter
, &node
->u
.root
.trace
, siblings
) {
5494 ret
= visit_trace_decl(ctx
, iter
);
5497 "Cannot visit trace (`trace` block): "
5503 BT_ASSERT(ctx
->current_scope
&&
5504 ctx
->current_scope
->parent_scope
== NULL
);
5507 bt_list_for_each_entry(iter
, &node
->u
.root
.stream
, siblings
) {
5508 ret
= visit_stream_decl(ctx
, iter
);
5511 "Cannot visit stream class: ret=%d",
5517 BT_ASSERT(ctx
->current_scope
&&
5518 ctx
->current_scope
->parent_scope
== NULL
);
5521 bt_list_for_each_entry(iter
, &node
->u
.root
.event
, siblings
) {
5522 ret
= visit_event_decl(ctx
, iter
);
5525 "Cannot visit event class: ret=%d",
5531 BT_ASSERT(ctx
->current_scope
&&
5532 ctx
->current_scope
->parent_scope
== NULL
);
5537 "Unexpected node type: node-type=%d",
5543 /* Move decoded stream classes to trace, if any */
5544 ret
= move_ctx_stream_classes_to_trace(ctx
);
5546 BT_LOGE("Cannot move stream classes to trace: ret=%d", ret
);