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"
43 #include <babeltrace/compat/uuid-internal.h>
44 #include <babeltrace/endian-internal.h>
45 #include <babeltrace/babeltrace.h>
46 #include <babeltrace/ctf-ir/field-types-internal.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 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
;
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
);
1356 ret
= visit_type_specifier_list(ctx
,
1357 type_specifier_list
, &nested_decl
);
1359 assert(!nested_decl
);
1365 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 assert(!nested_decl
);
1476 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 assert(!outer_field_decl
);
1497 assert(outer_field_decl
);
1498 BT_MOVE(*field_decl
, outer_field_decl
);
1502 BT_PUT(nested_decl
);
1503 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 assert(!field_decl
);
1534 _BT_LOGE_NODE(type_specifier_list
,
1535 "Cannot visit type declarator: ret=%d", ret
);
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 assert(!field_decl
);
1596 _BT_LOGE_NODE(type_specifier_list
,
1597 "Cannot visit type declarator: ret=%d", ret
);
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
);
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 assert(!untagged_variant_decl
);
2105 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 struct bt_list_head
*values
= &enumerator
->u
.enumerator
.values
;
2127 bt_list_for_each_entry(iter
, values
, siblings
) {
2130 if (iter
->type
!= NODE_UNARY_EXPRESSION
) {
2132 "Wrong expression for enumeration field type label: "
2133 "node-type=%d, label=\"%s\"", iter
->type
,
2145 switch (iter
->u
.unary_expression
.type
) {
2146 case UNARY_SIGNED_CONSTANT
:
2147 *target
= iter
->u
.unary_expression
.u
.signed_constant
;
2149 case UNARY_UNSIGNED_CONSTANT
:
2151 iter
->u
.unary_expression
.u
.unsigned_constant
;
2155 "Invalid enumeration field type entry: "
2156 "expecting constant signed or unsigned integer: "
2157 "node-type=%d, label=\"%s\"",
2158 iter
->u
.unary_expression
.type
, label
);
2165 "Invalid enumeration field type entry: label=\"%s\"",
2185 ret
= bt_ctf_field_type_enumeration_add_mapping(enum_decl
, label
,
2188 ret
= bt_field_type_enumeration_add_mapping_unsigned(enum_decl
,
2189 label
, (uint64_t) start
, (uint64_t) end
);
2192 _BT_LOGE_NODE(enumerator
,
2193 "Cannot add mapping to enumeration field type: "
2194 "label=\"%s\", ret=%d, "
2195 "start-value-unsigned=%" PRIu64
", "
2196 "end-value-unsigned=%" PRIu64
, label
, ret
,
2197 (uint64_t) start
, (uint64_t) end
);
2208 int visit_enum_decl(struct ctx
*ctx
, const char *name
,
2209 struct ctf_node
*container_type
,
2210 struct bt_list_head
*enumerator_list
,
2212 struct bt_field_type
**enum_decl
)
2216 _BT_FIELD_TYPE_INIT(integer_decl
);
2220 /* For named enum (without body), lookup in declaration scope */
2222 _BT_FIELD_TYPE_INIT(enum_decl_copy
);
2225 BT_LOGE_STR("Bodyless enumeration field type: missing name.");
2230 *enum_decl
= ctx_decl_scope_lookup_enum(ctx
->current_scope
,
2233 BT_LOGE("Cannot find enumeration field type: "
2234 "name=\"enum %s\"", name
);
2239 /* Make a copy of it */
2240 enum_decl_copy
= bt_field_type_copy(*enum_decl
);
2241 if (!enum_decl_copy
) {
2242 BT_LOGE_STR("Cannot create copy of enumeration field type.");
2248 BT_MOVE(*enum_decl
, enum_decl_copy
);
2250 struct ctf_node
*iter
;
2251 int64_t last_value
= 0;
2254 _BT_FIELD_TYPE_INIT(eenum_decl
);
2256 eenum_decl
= ctx_decl_scope_lookup_enum(
2257 ctx
->current_scope
, name
, 1);
2260 BT_LOGE("Enumeration field type already declared in local scope: "
2261 "name=\"enum %s\"", name
);
2267 if (!container_type
) {
2268 integer_decl
= ctx_decl_scope_lookup_alias(
2269 ctx
->current_scope
, "int", -1);
2270 if (!integer_decl
) {
2271 BT_LOGE_STR("Cannot find implicit `int` field type alias for enumeration field type.");
2276 ret
= visit_type_declarator(ctx
, container_type
,
2277 &qdummy_id
, NULL
, &integer_decl
, NULL
);
2279 assert(!integer_decl
);
2285 assert(integer_decl
);
2287 if (!bt_field_type_is_integer(integer_decl
)) {
2288 BT_LOGE("Container field type for enumeration field type is not an integer field type: "
2290 bt_field_type_id_string(
2291 bt_field_type_get_type_id(integer_decl
)));
2296 *enum_decl
= bt_field_type_enumeration_create(integer_decl
);
2298 BT_LOGE_STR("Cannot create enumeration field type.");
2303 bt_list_for_each_entry(iter
, enumerator_list
, siblings
) {
2304 ret
= visit_enum_decl_entry(ctx
, iter
, *enum_decl
,
2306 bt_field_type_integer_is_signed(integer_decl
));
2309 "Cannot visit enumeration field type entry: "
2316 ret
= ctx_decl_scope_register_enum(ctx
->current_scope
,
2319 BT_LOGE("Cannot register enumeration field type in declaration scope: "
2326 BT_PUT(integer_decl
);
2331 BT_PUT(integer_decl
);
2338 int visit_type_specifier(struct ctx
*ctx
,
2339 struct ctf_node
*type_specifier_list
,
2340 struct bt_field_type
**decl
)
2343 GString
*str
= NULL
;
2344 _BT_FIELD_TYPE_INIT(decl_copy
);
2347 str
= g_string_new("");
2348 ret
= get_type_specifier_list_name(ctx
, type_specifier_list
, str
);
2350 _BT_LOGE_NODE(type_specifier_list
,
2351 "Cannot get type specifier list's name: ret=%d", ret
);
2355 *decl
= ctx_decl_scope_lookup_alias(ctx
->current_scope
, str
->str
, -1);
2357 _BT_LOGE_NODE(type_specifier_list
,
2358 "Cannot find type alias: name=\"%s\"", str
->str
);
2363 /* Make a copy of the type declaration */
2364 decl_copy
= bt_field_type_copy(*decl
);
2366 _BT_LOGE_NODE(type_specifier_list
,
2367 "Cannot create field type copy.");
2372 BT_MOVE(*decl
, decl_copy
);
2373 (void) g_string_free(str
, TRUE
);
2380 (void) g_string_free(str
, TRUE
);
2389 int visit_integer_decl(struct ctx
*ctx
,
2390 struct bt_list_head
*expressions
,
2391 struct bt_field_type
**integer_decl
)
2395 bt_bool signedness
= 0;
2396 struct ctf_node
*expression
;
2397 uint64_t alignment
= 0, size
= 0;
2398 struct bt_clock_class
*mapped_clock
= NULL
;
2399 enum bt_string_encoding encoding
= BT_STRING_ENCODING_NONE
;
2400 enum bt_integer_base base
= BT_INTEGER_BASE_DECIMAL
;
2401 enum bt_byte_order byte_order
=
2402 bt_trace_get_native_byte_order(ctx
->trace
);
2404 *integer_decl
= NULL
;
2406 bt_list_for_each_entry(expression
, expressions
, siblings
) {
2407 struct ctf_node
*left
, *right
;
2409 left
= _BT_LIST_FIRST_ENTRY(&expression
->u
.ctf_expression
.left
,
2410 struct ctf_node
, siblings
);
2411 right
= _BT_LIST_FIRST_ENTRY(
2412 &expression
->u
.ctf_expression
.right
, struct ctf_node
,
2415 if (left
->u
.unary_expression
.type
!= UNARY_STRING
) {
2417 "Unexpected unary expression type: type=%d",
2418 left
->u
.unary_expression
.type
);
2423 if (!strcmp(left
->u
.unary_expression
.u
.string
, "signed")) {
2424 if (_IS_SET(&set
, _INTEGER_SIGNED_SET
)) {
2425 _BT_LOGE_DUP_ATTR(left
, "signed",
2426 "integer field type");
2431 signedness
= get_boolean(right
);
2432 if (signedness
< 0) {
2433 _BT_LOGE_NODE(right
,
2434 "Invalid boolean value for integer field type's `signed` attribute: "
2440 _SET(&set
, _INTEGER_SIGNED_SET
);
2441 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2443 if (_IS_SET(&set
, _INTEGER_BYTE_ORDER_SET
)) {
2444 _BT_LOGE_DUP_ATTR(left
, "byte_order",
2445 "integer field type");
2450 byte_order
= get_real_byte_order(ctx
, right
);
2451 if (byte_order
== BT_BYTE_ORDER_UNKNOWN
) {
2452 _BT_LOGE_NODE(right
,
2453 "Invalid `byte_order` attribute in integer field type: "
2459 _SET(&set
, _INTEGER_BYTE_ORDER_SET
);
2460 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "size")) {
2461 if (_IS_SET(&set
, _INTEGER_SIZE_SET
)) {
2462 _BT_LOGE_DUP_ATTR(left
, "size",
2463 "integer field type");
2468 if (right
->u
.unary_expression
.type
!=
2469 UNARY_UNSIGNED_CONSTANT
) {
2470 _BT_LOGE_NODE(right
,
2471 "Invalid `size` attribute in integer field type: "
2472 "expecting unsigned constant integer: "
2474 right
->u
.unary_expression
.type
);
2479 size
= right
->u
.unary_expression
.u
.unsigned_constant
;
2481 _BT_LOGE_NODE(right
,
2482 "Invalid `size` attribute in integer field type: "
2483 "expecting positive constant integer: "
2484 "size=%" PRIu64
, size
);
2487 } else if (size
> 64) {
2488 _BT_LOGE_NODE(right
,
2489 "Invalid `size` attribute in integer field type: "
2490 "integer fields over 64 bits are not supported as of this version: "
2491 "size=%" PRIu64
, size
);
2496 _SET(&set
, _INTEGER_SIZE_SET
);
2497 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2499 if (_IS_SET(&set
, _INTEGER_ALIGN_SET
)) {
2500 _BT_LOGE_DUP_ATTR(left
, "align",
2501 "integer field type");
2506 if (right
->u
.unary_expression
.type
!=
2507 UNARY_UNSIGNED_CONSTANT
) {
2508 _BT_LOGE_NODE(right
,
2509 "Invalid `align` attribute in integer field type: "
2510 "expecting unsigned constant integer: "
2512 right
->u
.unary_expression
.type
);
2518 right
->u
.unary_expression
.u
.unsigned_constant
;
2519 if (!is_align_valid(alignment
)) {
2520 _BT_LOGE_NODE(right
,
2521 "Invalid `align` attribute in integer field type: "
2522 "expecting power of two: "
2523 "align=%" PRIu64
, alignment
);
2528 _SET(&set
, _INTEGER_ALIGN_SET
);
2529 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "base")) {
2530 if (_IS_SET(&set
, _INTEGER_BASE_SET
)) {
2531 _BT_LOGE_DUP_ATTR(left
, "base",
2532 "integer field type");
2537 switch (right
->u
.unary_expression
.type
) {
2538 case UNARY_UNSIGNED_CONSTANT
:
2540 uint64_t constant
= right
->u
.unary_expression
.
2541 u
.unsigned_constant
;
2545 base
= BT_INTEGER_BASE_BINARY
;
2548 base
= BT_INTEGER_BASE_OCTAL
;
2551 base
= BT_INTEGER_BASE_DECIMAL
;
2554 base
= BT_INTEGER_BASE_HEXADECIMAL
;
2557 _BT_LOGE_NODE(right
,
2558 "Invalid `base` attribute in integer field type: "
2560 right
->u
.unary_expression
.u
.unsigned_constant
);
2568 char *s_right
= concatenate_unary_strings(
2569 &expression
->u
.ctf_expression
.right
);
2571 _BT_LOGE_NODE(right
,
2572 "Unexpected unary expression for integer field type's `base` attribute.");
2577 if (!strcmp(s_right
, "decimal") ||
2578 !strcmp(s_right
, "dec") ||
2579 !strcmp(s_right
, "d") ||
2580 !strcmp(s_right
, "i") ||
2581 !strcmp(s_right
, "u")) {
2582 base
= BT_INTEGER_BASE_DECIMAL
;
2583 } else if (!strcmp(s_right
, "hexadecimal") ||
2584 !strcmp(s_right
, "hex") ||
2585 !strcmp(s_right
, "x") ||
2586 !strcmp(s_right
, "X") ||
2587 !strcmp(s_right
, "p")) {
2588 base
= BT_INTEGER_BASE_HEXADECIMAL
;
2589 } else if (!strcmp(s_right
, "octal") ||
2590 !strcmp(s_right
, "oct") ||
2591 !strcmp(s_right
, "o")) {
2592 base
= BT_INTEGER_BASE_OCTAL
;
2593 } else if (!strcmp(s_right
, "binary") ||
2594 !strcmp(s_right
, "b")) {
2595 base
= BT_INTEGER_BASE_BINARY
;
2597 _BT_LOGE_NODE(right
,
2598 "Unexpected unary expression for integer field type's `base` attribute: "
2599 "base=\"%s\"", s_right
);
2609 _BT_LOGE_NODE(right
,
2610 "Invalid `base` attribute in integer field type: "
2611 "expecting unsigned constant integer or unary string.");
2616 _SET(&set
, _INTEGER_BASE_SET
);
2617 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2621 if (_IS_SET(&set
, _INTEGER_ENCODING_SET
)) {
2622 _BT_LOGE_DUP_ATTR(left
, "encoding",
2623 "integer field type");
2628 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
2629 _BT_LOGE_NODE(right
,
2630 "Invalid `encoding` attribute in integer field type: "
2631 "expecting unary string.");
2636 s_right
= concatenate_unary_strings(
2637 &expression
->u
.ctf_expression
.right
);
2639 _BT_LOGE_NODE(right
,
2640 "Unexpected unary expression for integer field type's `encoding` attribute.");
2645 if (!strcmp(s_right
, "UTF8") ||
2646 !strcmp(s_right
, "utf8") ||
2647 !strcmp(s_right
, "utf-8") ||
2648 !strcmp(s_right
, "UTF-8")) {
2649 encoding
= BT_STRING_ENCODING_UTF8
;
2650 } else if (!strcmp(s_right
, "ASCII") ||
2651 !strcmp(s_right
, "ascii")) {
2652 encoding
= BT_STRING_ENCODING_ASCII
;
2653 } else if (!strcmp(s_right
, "none")) {
2654 encoding
= BT_STRING_ENCODING_NONE
;
2656 _BT_LOGE_NODE(right
,
2657 "Invalid `encoding` attribute in integer field type: "
2658 "unknown encoding: encoding=\"%s\"",
2666 _SET(&set
, _INTEGER_ENCODING_SET
);
2667 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "map")) {
2668 const char *clock_name
;
2670 if (_IS_SET(&set
, _INTEGER_MAP_SET
)) {
2671 _BT_LOGE_DUP_ATTR(left
, "map",
2672 "integer field type");
2677 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
2678 _BT_LOGE_NODE(right
,
2679 "Invalid `map` attribute in integer field type: "
2680 "expecting unary string.");
2686 get_map_clock_name_value(
2687 &expression
->u
.ctf_expression
.right
);
2689 char *s_right
= concatenate_unary_strings(
2690 &expression
->u
.ctf_expression
.right
);
2693 _BT_LOGE_NODE(right
,
2694 "Unexpected unary expression for integer field type's `map` attribute.");
2699 _BT_LOGE_NODE(right
,
2700 "Invalid `map` attribute in integer field type: "
2701 "cannot find clock class at this point: name=\"%s\"",
2703 _SET(&set
, _INTEGER_MAP_SET
);
2708 mapped_clock
= bt_trace_get_clock_class_by_name(
2709 ctx
->trace
, clock_name
);
2710 if (!mapped_clock
) {
2711 _BT_LOGE_NODE(right
,
2712 "Invalid `map` attribute in integer field type: "
2713 "cannot find clock class at this point: name=\"%s\"",
2719 _SET(&set
, _INTEGER_MAP_SET
);
2722 "Unknown attribute in integer field type: "
2724 left
->u
.unary_expression
.u
.string
);
2728 if (!_IS_SET(&set
, _INTEGER_SIZE_SET
)) {
2729 BT_LOGE_STR("Missing `size` attribute in integer field type.");
2734 if (!_IS_SET(&set
, _INTEGER_ALIGN_SET
)) {
2735 if (size
% CHAR_BIT
) {
2736 /* Bit-packed alignment */
2739 /* Byte-packed alignment */
2740 alignment
= CHAR_BIT
;
2744 *integer_decl
= bt_field_type_integer_create((unsigned int) size
);
2745 if (!*integer_decl
) {
2746 BT_LOGE_STR("Cannot create integer field type.");
2751 ret
= bt_field_type_integer_set_is_signed(*integer_decl
, signedness
);
2752 ret
|= bt_field_type_integer_set_base(*integer_decl
, base
);
2753 ret
|= bt_field_type_integer_set_encoding(*integer_decl
, encoding
);
2754 ret
|= bt_field_type_set_alignment(*integer_decl
,
2755 (unsigned int) alignment
);
2756 ret
|= bt_field_type_set_byte_order(*integer_decl
, byte_order
);
2760 ret
|= bt_field_type_integer_set_mapped_clock_class(
2761 *integer_decl
, mapped_clock
);
2762 bt_put(mapped_clock
);
2763 mapped_clock
= NULL
;
2767 BT_LOGE_STR("Cannot configure integer field type.");
2776 bt_put(mapped_clock
);
2779 BT_PUT(*integer_decl
);
2785 int visit_floating_point_number_decl(struct ctx
*ctx
,
2786 struct bt_list_head
*expressions
,
2787 struct bt_field_type
**float_decl
)
2791 struct ctf_node
*expression
;
2792 uint64_t alignment
= 1, exp_dig
= 0, mant_dig
= 0;
2793 enum bt_byte_order byte_order
=
2794 bt_trace_get_native_byte_order(ctx
->trace
);
2798 bt_list_for_each_entry(expression
, expressions
, siblings
) {
2799 struct ctf_node
*left
, *right
;
2801 left
= _BT_LIST_FIRST_ENTRY(&expression
->u
.ctf_expression
.left
,
2802 struct ctf_node
, siblings
);
2803 right
= _BT_LIST_FIRST_ENTRY(
2804 &expression
->u
.ctf_expression
.right
, struct ctf_node
,
2807 if (left
->u
.unary_expression
.type
!= UNARY_STRING
) {
2809 "Unexpected unary expression type: type=%d",
2810 left
->u
.unary_expression
.type
);
2815 if (!strcmp(left
->u
.unary_expression
.u
.string
, "byte_order")) {
2816 if (_IS_SET(&set
, _FLOAT_BYTE_ORDER_SET
)) {
2817 _BT_LOGE_DUP_ATTR(left
, "byte_order",
2818 "floating point number field type");
2823 byte_order
= get_real_byte_order(ctx
, right
);
2824 if (byte_order
== BT_BYTE_ORDER_UNKNOWN
) {
2825 _BT_LOGE_NODE(right
,
2826 "Invalid `byte_order` attribute in floating point number field type: "
2832 _SET(&set
, _FLOAT_BYTE_ORDER_SET
);
2833 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2835 if (_IS_SET(&set
, _FLOAT_EXP_DIG_SET
)) {
2836 _BT_LOGE_DUP_ATTR(left
, "exp_dig",
2837 "floating point number field type");
2842 if (right
->u
.unary_expression
.type
!=
2843 UNARY_UNSIGNED_CONSTANT
) {
2844 _BT_LOGE_NODE(right
,
2845 "Invalid `exp_dig` attribute in floating point number field type: "
2846 "expecting unsigned constant integer: "
2848 right
->u
.unary_expression
.type
);
2853 exp_dig
= right
->u
.unary_expression
.u
.unsigned_constant
;
2854 _SET(&set
, _FLOAT_EXP_DIG_SET
);
2855 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2857 if (_IS_SET(&set
, _FLOAT_MANT_DIG_SET
)) {
2858 _BT_LOGE_DUP_ATTR(left
, "mant_dig",
2859 "floating point number field type");
2864 if (right
->u
.unary_expression
.type
!=
2865 UNARY_UNSIGNED_CONSTANT
) {
2866 _BT_LOGE_NODE(right
,
2867 "Invalid `mant_dig` attribute in floating point number field type: "
2868 "expecting unsigned constant integer: "
2870 right
->u
.unary_expression
.type
);
2875 mant_dig
= right
->u
.unary_expression
.u
.
2877 _SET(&set
, _FLOAT_MANT_DIG_SET
);
2878 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2880 if (_IS_SET(&set
, _FLOAT_ALIGN_SET
)) {
2881 _BT_LOGE_DUP_ATTR(left
, "align",
2882 "floating point number field type");
2887 if (right
->u
.unary_expression
.type
!=
2888 UNARY_UNSIGNED_CONSTANT
) {
2889 _BT_LOGE_NODE(right
,
2890 "Invalid `align` attribute in floating point number field type: "
2891 "expecting unsigned constant integer: "
2893 right
->u
.unary_expression
.type
);
2898 alignment
= right
->u
.unary_expression
.u
.
2901 if (!is_align_valid(alignment
)) {
2902 _BT_LOGE_NODE(right
,
2903 "Invalid `align` attribute in floating point number field type: "
2904 "expecting power of two: "
2905 "align=%" PRIu64
, alignment
);
2910 _SET(&set
, _FLOAT_ALIGN_SET
);
2913 "Unknown attribute in floating point number field type: "
2915 left
->u
.unary_expression
.u
.string
);
2919 if (!_IS_SET(&set
, _FLOAT_MANT_DIG_SET
)) {
2920 BT_LOGE_STR("Missing `mant_dig` attribute in floating point number field type.");
2925 if (!_IS_SET(&set
, _FLOAT_EXP_DIG_SET
)) {
2926 BT_LOGE_STR("Missing `exp_dig` attribute in floating point number field type.");
2931 if (!_IS_SET(&set
, _INTEGER_ALIGN_SET
)) {
2932 if ((mant_dig
+ exp_dig
) % CHAR_BIT
) {
2933 /* Bit-packed alignment */
2936 /* Byte-packed alignment */
2937 alignment
= CHAR_BIT
;
2941 *float_decl
= bt_field_type_floating_point_create();
2943 BT_LOGE_STR("Cannot create floating point number field type.");
2948 ret
= bt_field_type_floating_point_set_exponent_digits(
2949 *float_decl
, exp_dig
);
2950 ret
|= bt_field_type_floating_point_set_mantissa_digits(
2951 *float_decl
, mant_dig
);
2952 ret
|= bt_field_type_set_byte_order(*float_decl
, byte_order
);
2953 ret
|= bt_field_type_set_alignment(*float_decl
, alignment
);
2955 BT_LOGE_STR("Cannot configure floating point number field type.");
2963 BT_PUT(*float_decl
);
2969 int visit_string_decl(struct ctx
*ctx
,
2970 struct bt_list_head
*expressions
,
2971 struct bt_field_type
**string_decl
)
2975 struct ctf_node
*expression
;
2976 enum bt_string_encoding encoding
= BT_STRING_ENCODING_UTF8
;
2978 *string_decl
= NULL
;
2980 bt_list_for_each_entry(expression
, expressions
, siblings
) {
2981 struct ctf_node
*left
, *right
;
2983 left
= _BT_LIST_FIRST_ENTRY(&expression
->u
.ctf_expression
.left
,
2984 struct ctf_node
, siblings
);
2985 right
= _BT_LIST_FIRST_ENTRY(
2986 &expression
->u
.ctf_expression
.right
, struct ctf_node
,
2989 if (left
->u
.unary_expression
.type
!= UNARY_STRING
) {
2991 "Unexpected unary expression type: type=%d",
2992 left
->u
.unary_expression
.type
);
2997 if (!strcmp(left
->u
.unary_expression
.u
.string
, "encoding")) {
3000 if (_IS_SET(&set
, _STRING_ENCODING_SET
)) {
3001 _BT_LOGE_DUP_ATTR(left
, "encoding",
3002 "string field type");
3007 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
3008 _BT_LOGE_NODE(right
,
3009 "Invalid `encoding` attribute in string field type: "
3010 "expecting unary string.");
3015 s_right
= concatenate_unary_strings(
3016 &expression
->u
.ctf_expression
.right
);
3018 _BT_LOGE_NODE(right
,
3019 "Unexpected unary expression for string field type's `encoding` attribute.");
3024 if (!strcmp(s_right
, "UTF8") ||
3025 !strcmp(s_right
, "utf8") ||
3026 !strcmp(s_right
, "utf-8") ||
3027 !strcmp(s_right
, "UTF-8")) {
3028 encoding
= BT_STRING_ENCODING_UTF8
;
3029 } else if (!strcmp(s_right
, "ASCII") ||
3030 !strcmp(s_right
, "ascii")) {
3031 encoding
= BT_STRING_ENCODING_ASCII
;
3032 } else if (!strcmp(s_right
, "none")) {
3033 encoding
= BT_STRING_ENCODING_NONE
;
3035 _BT_LOGE_NODE(right
,
3036 "Invalid `encoding` attribute in string field type: "
3037 "unknown encoding: encoding=\"%s\"",
3045 _SET(&set
, _STRING_ENCODING_SET
);
3048 "Unknown attribute in string field type: "
3050 left
->u
.unary_expression
.u
.string
);
3054 *string_decl
= bt_field_type_string_create();
3055 if (!*string_decl
) {
3056 BT_LOGE_STR("Cannot create string field type.");
3061 ret
= bt_field_type_string_set_encoding(*string_decl
, encoding
);
3063 BT_LOGE_STR("Cannot configure string field type.");
3071 BT_PUT(*string_decl
);
3077 int visit_type_specifier_list(struct ctx
*ctx
,
3078 struct ctf_node
*ts_list
,
3079 struct bt_field_type
**decl
)
3082 struct ctf_node
*first
, *node
;
3086 if (ts_list
->type
!= NODE_TYPE_SPECIFIER_LIST
) {
3087 _BT_LOGE_NODE(ts_list
,
3088 "Unexpected node type: node-type=%d", ts_list
->type
);
3093 first
= _BT_LIST_FIRST_ENTRY(&ts_list
->u
.type_specifier_list
.head
,
3094 struct ctf_node
, siblings
);
3095 if (first
->type
!= NODE_TYPE_SPECIFIER
) {
3096 _BT_LOGE_NODE(first
,
3097 "Unexpected node type: node-type=%d", first
->type
);
3102 node
= first
->u
.type_specifier
.node
;
3104 switch (first
->u
.type_specifier
.type
) {
3105 case TYPESPEC_INTEGER
:
3106 ret
= visit_integer_decl(ctx
, &node
->u
.integer
.expressions
,
3113 case TYPESPEC_FLOATING_POINT
:
3114 ret
= visit_floating_point_number_decl(ctx
,
3115 &node
->u
.floating_point
.expressions
, decl
);
3121 case TYPESPEC_STRING
:
3122 ret
= visit_string_decl(ctx
,
3123 &node
->u
.string
.expressions
, decl
);
3129 case TYPESPEC_STRUCT
:
3130 ret
= visit_struct_decl(ctx
, node
->u
._struct
.name
,
3131 &node
->u
._struct
.declaration_list
,
3132 node
->u
._struct
.has_body
,
3133 &node
->u
._struct
.min_align
, decl
);
3139 case TYPESPEC_VARIANT
:
3140 ret
= visit_variant_decl(ctx
, node
->u
.variant
.name
,
3141 node
->u
.variant
.choice
,
3142 &node
->u
.variant
.declaration_list
,
3143 node
->u
.variant
.has_body
, decl
);
3150 ret
= visit_enum_decl(ctx
, node
->u
._enum
.enum_id
,
3151 node
->u
._enum
.container_type
,
3152 &node
->u
._enum
.enumerator_list
,
3153 node
->u
._enum
.has_body
, decl
);
3161 case TYPESPEC_SHORT
:
3164 case TYPESPEC_FLOAT
:
3165 case TYPESPEC_DOUBLE
:
3166 case TYPESPEC_SIGNED
:
3167 case TYPESPEC_UNSIGNED
:
3169 case TYPESPEC_COMPLEX
:
3170 case TYPESPEC_IMAGINARY
:
3171 case TYPESPEC_CONST
:
3172 case TYPESPEC_ID_TYPE
:
3173 ret
= visit_type_specifier(ctx
, ts_list
, decl
);
3175 _BT_LOGE_NODE(first
,
3176 "Cannot visit type specifier: ret=%d",
3183 _BT_LOGE_NODE(first
,
3184 "Unexpected type specifier type: node-type=%d",
3185 first
->u
.type_specifier
.type
);
3201 int visit_event_decl_entry(struct ctx
*ctx
, struct ctf_node
*node
,
3202 struct bt_event_class
*event_class
, int64_t *stream_id
,
3207 _BT_FIELD_TYPE_INIT(decl
);
3209 switch (node
->type
) {
3211 ret
= visit_typedef(ctx
, node
->u
._typedef
.type_specifier_list
,
3212 &node
->u
._typedef
.type_declarators
);
3215 "Cannot add type definition found in event class.");
3219 case NODE_TYPEALIAS
:
3220 ret
= visit_typealias(ctx
, node
->u
.typealias
.target
,
3221 node
->u
.typealias
.alias
);
3224 "Cannot add type alias found in event class.");
3228 case NODE_CTF_EXPRESSION
:
3230 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
3232 _BT_LOGE_NODE(node
, "Cannot concatenate unary strings.");
3237 if (!strcmp(left
, "name")) {
3238 /* This is already known at this stage */
3239 if (_IS_SET(set
, _EVENT_NAME_SET
)) {
3240 _BT_LOGE_DUP_ATTR(node
, "name",
3246 _SET(set
, _EVENT_NAME_SET
);
3247 } else if (!strcmp(left
, "id")) {
3250 if (_IS_SET(set
, _EVENT_ID_SET
)) {
3251 _BT_LOGE_DUP_ATTR(node
, "id",
3257 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
3259 /* Only read "id" if get_unary_unsigned() succeeded. */
3260 if (ret
|| (!ret
&& id
< 0)) {
3262 "Unexpected unary expression for event class's `id` attribute.");
3267 ret
= bt_event_class_set_id(event_class
, id
);
3270 "Cannot set event class's ID: "
3275 _SET(set
, _EVENT_ID_SET
);
3276 } else if (!strcmp(left
, "stream_id")) {
3277 if (_IS_SET(set
, _EVENT_STREAM_ID_SET
)) {
3278 _BT_LOGE_DUP_ATTR(node
, "stream_id",
3284 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
3285 (uint64_t *) stream_id
);
3287 * Only read "stream_id" if get_unary_unsigned()
3290 if (ret
|| (!ret
&& *stream_id
< 0)) {
3292 "Unexpected unary expression for event class's `stream_id` attribute.");
3297 _SET(set
, _EVENT_STREAM_ID_SET
);
3298 } else if (!strcmp(left
, "context")) {
3299 if (_IS_SET(set
, _EVENT_CONTEXT_SET
)) {
3301 "Duplicate `context` entry in event class.");
3306 ret
= visit_type_specifier_list(ctx
,
3307 _BT_LIST_FIRST_ENTRY(
3308 &node
->u
.ctf_expression
.right
,
3309 struct ctf_node
, siblings
),
3313 "Cannot create event class's context field type.");
3318 ret
= bt_event_class_set_context_type(
3323 "Cannot set event class's context field type.");
3327 _SET(set
, _EVENT_CONTEXT_SET
);
3328 } else if (!strcmp(left
, "fields")) {
3329 if (_IS_SET(set
, _EVENT_FIELDS_SET
)) {
3331 "Duplicate `fields` entry in event class.");
3336 ret
= visit_type_specifier_list(ctx
,
3337 _BT_LIST_FIRST_ENTRY(
3338 &node
->u
.ctf_expression
.right
,
3339 struct ctf_node
, siblings
),
3343 "Cannot create event class's payload field type.");
3348 ret
= bt_event_class_set_payload_type(
3353 "Cannot set event class's payload field type.");
3357 _SET(set
, _EVENT_FIELDS_SET
);
3358 } else if (!strcmp(left
, "loglevel")) {
3359 uint64_t loglevel_value
;
3360 enum bt_event_class_log_level log_level
=
3361 BT_EVENT_CLASS_LOG_LEVEL_UNSPECIFIED
;
3363 if (_IS_SET(set
, _EVENT_LOGLEVEL_SET
)) {
3364 _BT_LOGE_DUP_ATTR(node
, "loglevel",
3370 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
3374 "Unexpected unary expression for event class's `loglevel` attribute.");
3379 switch (loglevel_value
) {
3381 log_level
= BT_EVENT_CLASS_LOG_LEVEL_EMERGENCY
;
3384 log_level
= BT_EVENT_CLASS_LOG_LEVEL_ALERT
;
3387 log_level
= BT_EVENT_CLASS_LOG_LEVEL_CRITICAL
;
3390 log_level
= BT_EVENT_CLASS_LOG_LEVEL_ERROR
;
3393 log_level
= BT_EVENT_CLASS_LOG_LEVEL_WARNING
;
3396 log_level
= BT_EVENT_CLASS_LOG_LEVEL_NOTICE
;
3399 log_level
= BT_EVENT_CLASS_LOG_LEVEL_INFO
;
3402 log_level
= BT_EVENT_CLASS_LOG_LEVEL_DEBUG_SYSTEM
;
3405 log_level
= BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROGRAM
;
3408 log_level
= BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROCESS
;
3411 log_level
= BT_EVENT_CLASS_LOG_LEVEL_DEBUG_MODULE
;
3414 log_level
= BT_EVENT_CLASS_LOG_LEVEL_DEBUG_UNIT
;
3417 log_level
= BT_EVENT_CLASS_LOG_LEVEL_DEBUG_FUNCTION
;
3420 log_level
= BT_EVENT_CLASS_LOG_LEVEL_DEBUG_LINE
;
3423 log_level
= BT_EVENT_CLASS_LOG_LEVEL_DEBUG
;
3426 _BT_LOGW_NODE(node
, "Not setting event class's log level because its value is unknown: "
3427 "log-level=%" PRIu64
, loglevel_value
);
3430 if (log_level
!= BT_EVENT_CLASS_LOG_LEVEL_UNSPECIFIED
) {
3431 ret
= bt_event_class_set_log_level(
3432 event_class
, log_level
);
3435 "Cannot set event class's log level.");
3440 _SET(set
, _EVENT_LOGLEVEL_SET
);
3441 } else if (!strcmp(left
, "model.emf.uri")) {
3444 if (_IS_SET(set
, _EVENT_MODEL_EMF_URI_SET
)) {
3445 _BT_LOGE_DUP_ATTR(node
, "model.emf.uri",
3451 right
= concatenate_unary_strings(
3452 &node
->u
.ctf_expression
.right
);
3455 "Unexpected unary expression for event class's `model.emf.uri` attribute.");
3460 if (strlen(right
) == 0) {
3462 "Not setting event class's EMF URI because it's empty.");
3464 ret
= bt_event_class_set_emf_uri(
3465 event_class
, right
);
3468 "Cannot set event class's EMF URI.");
3474 _SET(set
, _EVENT_MODEL_EMF_URI_SET
);
3477 "Unknown attribute in event class: "
3478 "attr-name=\"%s\"", left
);
3503 char *get_event_decl_name(struct ctx
*ctx
, struct ctf_node
*node
)
3507 struct ctf_node
*iter
;
3508 struct bt_list_head
*decl_list
= &node
->u
.event
.declaration_list
;
3510 bt_list_for_each_entry(iter
, decl_list
, siblings
) {
3511 if (iter
->type
!= NODE_CTF_EXPRESSION
) {
3515 left
= concatenate_unary_strings(&iter
->u
.ctf_expression
.left
);
3518 "Cannot concatenate unary strings.");
3522 if (!strcmp(left
, "name")) {
3523 name
= concatenate_unary_strings(
3524 &iter
->u
.ctf_expression
.right
);
3527 "Unexpected unary expression for event class's `name` attribute.");
3549 int reset_event_decl_types(struct ctx
*ctx
,
3550 struct bt_event_class
*event_class
)
3555 ret
= bt_event_class_set_context_type(event_class
, NULL
);
3557 BT_LOGE("Cannot reset initial event class's context field type: "
3558 "event-name=\"%s\"",
3559 bt_event_class_get_name(event_class
));
3563 /* Event payload. */
3564 ret
= bt_event_class_set_payload_type(event_class
, NULL
);
3566 BT_LOGE("Cannot reset initial event class's payload field type: "
3567 "event-name=\"%s\"",
3568 bt_event_class_get_name(event_class
));
3576 int reset_stream_decl_types(struct ctx
*ctx
,
3577 struct bt_stream_class
*stream_class
)
3581 /* Packet context. */
3582 ret
= bt_stream_class_set_packet_context_type(stream_class
, NULL
);
3584 BT_LOGE_STR("Cannot reset initial stream class's packet context field type.");
3589 ret
= bt_stream_class_set_event_header_type(stream_class
, NULL
);
3591 BT_LOGE_STR("Cannot reset initial stream class's event header field type.");
3595 /* Event context. */
3596 ret
= bt_stream_class_set_event_context_type(stream_class
, NULL
);
3598 BT_LOGE_STR("Cannot reset initial stream class's event context field type.");
3606 struct bt_stream_class
*create_reset_stream_class(struct ctx
*ctx
)
3609 struct bt_stream_class
*stream_class
;
3611 stream_class
= bt_stream_class_create_empty(NULL
);
3612 if (!stream_class
) {
3613 BT_LOGE_STR("Cannot create empty stream class.");
3618 * Set packet context, event header, and event context to NULL to
3619 * override the default ones.
3621 ret
= reset_stream_decl_types(ctx
, stream_class
);
3626 return stream_class
;
3629 BT_PUT(stream_class
);
3635 int visit_event_decl(struct ctx
*ctx
, struct ctf_node
*node
)
3640 struct ctf_node
*iter
;
3641 int64_t stream_id
= -1;
3642 char *event_name
= NULL
;
3643 struct bt_event_class
*event_class
= NULL
;
3644 struct bt_event_class
*eevent_class
;
3645 struct bt_stream_class
*stream_class
= NULL
;
3646 struct bt_list_head
*decl_list
= &node
->u
.event
.declaration_list
;
3647 bool pop_scope
= false;
3649 if (node
->visited
) {
3653 node
->visited
= TRUE
;
3654 event_name
= get_event_decl_name(ctx
, node
);
3657 "Missing `name` attribute in event class.");
3662 event_class
= bt_event_class_create(event_name
);
3665 * Unset context and fields to override the default ones.
3667 ret
= reset_event_decl_types(ctx
, event_class
);
3670 "Cannot reset event class's field types: "
3675 ret
= ctx_push_scope(ctx
);
3677 BT_LOGE_STR("Cannot push scope.");
3683 bt_list_for_each_entry(iter
, decl_list
, siblings
) {
3684 ret
= visit_event_decl_entry(ctx
, iter
, event_class
,
3687 _BT_LOGE_NODE(iter
, "Cannot visit event class's entry: "
3693 if (!_IS_SET(&set
, _EVENT_STREAM_ID_SET
)) {
3695 int64_t *new_stream_id
;
3696 struct bt_stream_class
*new_stream_class
;
3697 size_t stream_class_count
=
3698 g_hash_table_size(ctx
->stream_classes
) +
3699 bt_trace_get_stream_class_count(ctx
->trace
);
3702 * Allow missing stream_id if there is only a single
3705 switch (stream_class_count
) {
3707 /* Create implicit stream class if there's none */
3709 new_stream_class
= create_reset_stream_class(ctx
);
3710 if (!new_stream_class
) {
3712 "Cannot create empty stream class.");
3717 ret
= bt_stream_class_set_id(new_stream_class
,
3721 "Cannot set stream class's ID: "
3722 "id=0, ret=%d", ret
);
3723 BT_PUT(new_stream_class
);
3727 new_stream_id
= g_new0(int64_t, 1);
3728 if (!new_stream_id
) {
3729 BT_LOGE_STR("Failed to allocate a int64_t.");
3734 *new_stream_id
= stream_id
;
3736 /* Move reference to visitor's context */
3737 g_hash_table_insert(ctx
->stream_classes
,
3738 new_stream_id
, new_stream_class
);
3739 new_stream_id
= NULL
;
3740 new_stream_class
= NULL
;
3743 /* Single stream class: get its ID */
3744 if (g_hash_table_size(ctx
->stream_classes
) == 1) {
3745 keys
= g_hash_table_get_keys(ctx
->stream_classes
);
3746 stream_id
= *((int64_t *) keys
->data
);
3749 assert(bt_trace_get_stream_class_count(
3752 bt_trace_get_stream_class_by_index(
3754 assert(stream_class
);
3755 stream_id
= bt_stream_class_get_id(
3757 BT_PUT(stream_class
);
3762 "Missing `stream_id` attribute in event class.");
3768 assert(stream_id
>= 0);
3770 /* We have the stream ID now; get the stream class if found */
3771 stream_class
= g_hash_table_lookup(ctx
->stream_classes
, &stream_id
);
3772 bt_get(stream_class
);
3773 if (!stream_class
) {
3774 stream_class
= bt_trace_get_stream_class_by_id(ctx
->trace
,
3776 if (!stream_class
) {
3778 "Cannot find stream class at this point: "
3779 "id=%" PRId64
, stream_id
);
3785 assert(stream_class
);
3787 if (!_IS_SET(&set
, _EVENT_ID_SET
)) {
3788 /* Allow only one event without ID per stream */
3789 if (bt_stream_class_get_event_class_count(stream_class
) !=
3792 "Missing `id` attribute in event class.");
3798 ret
= bt_event_class_set_id(event_class
, 0);
3801 "Cannot set event class's ID: id=0, ret=%d",
3807 event_id
= bt_event_class_get_id(event_class
);
3809 _BT_LOGE_NODE(node
, "Cannot get event class's ID.");
3814 eevent_class
= bt_stream_class_get_event_class_by_id(stream_class
,
3817 BT_PUT(eevent_class
);
3819 "Duplicate event class (same ID) in the same stream class: "
3820 "id=%" PRId64
, event_id
);
3825 ret
= bt_stream_class_add_event_class(stream_class
, event_class
);
3826 BT_PUT(event_class
);
3829 "Cannot add event class to stream class: ret=%d", ret
);
3836 bt_put(event_class
);
3844 bt_put(stream_class
);
3849 int auto_map_field_to_trace_clock_class(struct ctx
*ctx
,
3850 struct bt_field_type
*ft
)
3852 struct bt_clock_class
*clock_class_to_map_to
= NULL
;
3853 struct bt_clock_class
*mapped_clock_class
= NULL
;
3855 int64_t clock_class_count
;
3857 if (!ft
|| !bt_field_type_is_integer(ft
)) {
3861 mapped_clock_class
=
3862 bt_field_type_integer_get_mapped_clock_class(ft
);
3863 if (mapped_clock_class
) {
3867 clock_class_count
= bt_trace_get_clock_class_count(ctx
->trace
);
3868 assert(clock_class_count
>= 0);
3870 switch (clock_class_count
) {
3873 * No clock class exists in the trace at this
3874 * point. Create an implicit one at 1 GHz,
3875 * named `default`, and use this clock class.
3877 clock_class_to_map_to
= bt_clock_class_create("default",
3879 if (!clock_class_to_map_to
) {
3880 BT_LOGE_STR("Cannot create a clock class.");
3885 ret
= bt_trace_add_clock_class(ctx
->trace
,
3886 clock_class_to_map_to
);
3888 BT_LOGE_STR("Cannot add clock class to trace.");
3894 * Only one clock class exists in the trace at
3895 * this point: use this one.
3897 clock_class_to_map_to
=
3898 bt_trace_get_clock_class_by_index(ctx
->trace
, 0);
3899 assert(clock_class_to_map_to
);
3903 * Timestamp field not mapped to a clock class
3904 * and there's more than one clock class in the
3905 * trace: this is an error.
3907 BT_LOGE_STR("Timestamp field found with no mapped clock class, "
3908 "but there's more than one clock class in the trace at this point.");
3913 assert(clock_class_to_map_to
);
3914 ret
= bt_field_type_integer_set_mapped_clock_class(ft
,
3915 clock_class_to_map_to
);
3917 BT_LOGE("Cannot map field type's field to trace's clock class: "
3918 "clock-class-name=\"%s\", ret=%d",
3919 bt_clock_class_get_name(clock_class_to_map_to
),
3925 bt_put(clock_class_to_map_to
);
3926 bt_put(mapped_clock_class
);
3931 int auto_map_fields_to_trace_clock_class(struct ctx
*ctx
,
3932 struct bt_field_type
*root_ft
, const char *field_name
)
3941 if (!bt_field_type_is_structure(root_ft
) &&
3942 !bt_field_type_is_variant(root_ft
)) {
3946 if (bt_field_type_is_structure(root_ft
)) {
3947 count
= bt_field_type_structure_get_field_count(root_ft
);
3949 count
= bt_field_type_variant_get_field_count(root_ft
);
3954 for (i
= 0; i
< count
; i
++) {
3955 _BT_FIELD_TYPE_INIT(ft
);
3958 if (bt_field_type_is_structure(root_ft
)) {
3959 ret
= bt_field_type_structure_get_field_by_index(
3960 root_ft
, &name
, &ft
, i
);
3961 } else if (bt_field_type_is_variant(root_ft
)) {
3962 ret
= bt_field_type_variant_get_field_by_index(
3963 root_ft
, &name
, &ft
, i
);
3968 if (strcmp(name
, field_name
) == 0) {
3969 ret
= auto_map_field_to_trace_clock_class(ctx
, ft
);
3971 BT_LOGE("Cannot automatically map field to trace's clock class: "
3972 "field-name=\"%s\"", field_name
);
3978 ret
= auto_map_fields_to_trace_clock_class(ctx
, ft
, field_name
);
3981 BT_LOGE("Cannot automatically map structure or variant field type's fields to trace's clock class: "
3982 "field-name=\"%s\", root-field-name=\"%s\"",
3993 int visit_stream_decl_entry(struct ctx
*ctx
, struct ctf_node
*node
,
3994 struct bt_stream_class
*stream_class
, int *set
)
3998 _BT_FIELD_TYPE_INIT(decl
);
4000 switch (node
->type
) {
4002 ret
= visit_typedef(ctx
, node
->u
._typedef
.type_specifier_list
,
4003 &node
->u
._typedef
.type_declarators
);
4006 "Cannot add type definition found in stream class.");
4010 case NODE_TYPEALIAS
:
4011 ret
= visit_typealias(ctx
, node
->u
.typealias
.target
,
4012 node
->u
.typealias
.alias
);
4015 "Cannot add type alias found in stream class.");
4019 case NODE_CTF_EXPRESSION
:
4021 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
4023 _BT_LOGE_NODE(node
, "Cannot concatenate unary strings.");
4028 if (!strcmp(left
, "id")) {
4032 if (_IS_SET(set
, _STREAM_ID_SET
)) {
4033 _BT_LOGE_DUP_ATTR(node
, "id",
4034 "stream declaration");
4039 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
4041 /* Only read "id" if get_unary_unsigned() succeeded. */
4042 if (ret
|| (!ret
&& id
< 0)) {
4044 "Unexpected unary expression for stream class's `id` attribute.");
4049 ptr
= g_hash_table_lookup(ctx
->stream_classes
, &id
);
4052 "Duplicate stream class (same ID): id=%" PRId64
,
4058 ret
= bt_stream_class_set_id(stream_class
, id
);
4061 "Cannot set stream class's ID: "
4062 "id=%" PRId64
", ret=%d", id
, ret
);
4066 _SET(set
, _STREAM_ID_SET
);
4067 } else if (!strcmp(left
, "event.header")) {
4068 if (_IS_SET(set
, _STREAM_EVENT_HEADER_SET
)) {
4070 "Duplicate `event.header` entry in stream class.");
4075 ret
= visit_type_specifier_list(ctx
,
4076 _BT_LIST_FIRST_ENTRY(
4077 &node
->u
.ctf_expression
.right
,
4078 struct ctf_node
, siblings
),
4082 "Cannot create stream class's event header field type.");
4087 ret
= auto_map_fields_to_trace_clock_class(ctx
,
4091 "Cannot automatically map specific event header field type fields named `timestamp` to trace's clock class.");
4095 ret
= bt_stream_class_set_event_header_type(
4096 stream_class
, decl
);
4100 "Cannot set stream class's event header field type.");
4104 _SET(set
, _STREAM_EVENT_HEADER_SET
);
4105 } else if (!strcmp(left
, "event.context")) {
4106 if (_IS_SET(set
, _STREAM_EVENT_CONTEXT_SET
)) {
4108 "Duplicate `event.context` entry in stream class.");
4113 ret
= visit_type_specifier_list(ctx
,
4114 _BT_LIST_FIRST_ENTRY(
4115 &node
->u
.ctf_expression
.right
,
4116 struct ctf_node
, siblings
),
4120 "Cannot create stream class's event context field type.");
4126 ret
= bt_stream_class_set_event_context_type(
4127 stream_class
, decl
);
4131 "Cannot set stream class's event context field type.");
4135 _SET(set
, _STREAM_EVENT_CONTEXT_SET
);
4136 } else if (!strcmp(left
, "packet.context")) {
4137 if (_IS_SET(set
, _STREAM_PACKET_CONTEXT_SET
)) {
4139 "Duplicate `packet.context` entry in stream class.");
4144 ret
= visit_type_specifier_list(ctx
,
4145 _BT_LIST_FIRST_ENTRY(
4146 &node
->u
.ctf_expression
.right
,
4147 struct ctf_node
, siblings
),
4151 "Cannot create stream class's packet context field type.");
4156 ret
= auto_map_fields_to_trace_clock_class(ctx
,
4157 decl
, "timestamp_begin");
4160 "Cannot automatically map specific packet context field type fields named `timestamp_begin` to trace's clock class.");
4164 ret
= auto_map_fields_to_trace_clock_class(ctx
,
4165 decl
, "timestamp_end");
4168 "Cannot automatically map specific packet context field type fields named `timestamp_end` to trace's clock class.");
4172 ret
= bt_stream_class_set_packet_context_type(
4173 stream_class
, decl
);
4177 "Cannot set stream class's packet context field type.");
4181 _SET(set
, _STREAM_PACKET_CONTEXT_SET
);
4184 "Unknown attribute in stream class: "
4185 "attr-name=\"%s\"", left
);
4208 int visit_stream_decl(struct ctx
*ctx
, struct ctf_node
*node
)
4214 struct ctf_node
*iter
;
4215 struct bt_stream_class
*stream_class
= NULL
;
4216 struct bt_stream_class
*existing_stream_class
= NULL
;
4217 struct bt_list_head
*decl_list
= &node
->u
.stream
.declaration_list
;
4219 if (node
->visited
) {
4223 node
->visited
= TRUE
;
4224 stream_class
= create_reset_stream_class(ctx
);
4225 if (!stream_class
) {
4226 _BT_LOGE_NODE(node
, "Cannot create empty stream class.");
4231 ret
= ctx_push_scope(ctx
);
4233 BT_LOGE_STR("Cannot push scope.");
4237 bt_list_for_each_entry(iter
, decl_list
, siblings
) {
4238 ret
= visit_stream_decl_entry(ctx
, iter
, stream_class
, &set
);
4241 "Cannot visit stream class's entry: "
4250 if (_IS_SET(&set
, _STREAM_ID_SET
)) {
4251 /* Check that packet header has stream_id field */
4252 _BT_FIELD_TYPE_INIT(stream_id_decl
);
4253 _BT_FIELD_TYPE_INIT(packet_header_decl
);
4255 packet_header_decl
=
4256 bt_trace_get_packet_header_type(ctx
->trace
);
4257 if (!packet_header_decl
) {
4259 "Stream class has a `id` attribute, "
4260 "but trace has no packet header field type.");
4265 bt_field_type_structure_get_field_type_by_name(
4266 packet_header_decl
, "stream_id");
4267 BT_PUT(packet_header_decl
);
4268 if (!stream_id_decl
) {
4270 "Stream class has a `id` attribute, "
4271 "but trace's packet header field type has no `stream_id` field.");
4275 if (!bt_field_type_is_integer(stream_id_decl
)) {
4276 BT_PUT(stream_id_decl
);
4278 "Stream class has a `id` attribute, "
4279 "but trace's packet header field type's `stream_id` field is not an integer field type.");
4283 BT_PUT(stream_id_decl
);
4285 /* Allow only _one_ ID-less stream */
4286 if (g_hash_table_size(ctx
->stream_classes
) != 0) {
4288 "Missing `id` attribute in stream class as there's more than one stream class in the trace.");
4293 /* Automatic ID: 0 */
4294 ret
= bt_stream_class_set_id(stream_class
, 0);
4298 id
= bt_stream_class_get_id(stream_class
);
4301 "Cannot get stream class's ID.");
4307 * Make sure that this stream class's ID is currently unique in
4310 existing_stream_class
= bt_trace_get_stream_class_by_id(ctx
->trace
,
4312 if (g_hash_table_lookup(ctx
->stream_classes
, &id
) ||
4313 existing_stream_class
) {
4315 "Duplicate stream class (same ID): id=%" PRId64
,
4321 new_id
= g_new0(int64_t, 1);
4323 BT_LOGE_STR("Failed to allocate a int64_t.");
4329 /* Move reference to visitor's context */
4330 g_hash_table_insert(ctx
->stream_classes
, new_id
,
4332 stream_class
= NULL
;
4336 bt_put(stream_class
);
4339 bt_put(existing_stream_class
);
4344 int visit_trace_decl_entry(struct ctx
*ctx
, struct ctf_node
*node
, int *set
)
4348 _BT_FIELD_TYPE_INIT(packet_header_decl
);
4350 switch (node
->type
) {
4352 ret
= visit_typedef(ctx
, node
->u
._typedef
.type_specifier_list
,
4353 &node
->u
._typedef
.type_declarators
);
4356 "Cannot add type definition found in trace (`trace` block).");
4360 case NODE_TYPEALIAS
:
4361 ret
= visit_typealias(ctx
, node
->u
.typealias
.target
,
4362 node
->u
.typealias
.alias
);
4365 "Cannot add type alias found in trace (`trace` block).");
4369 case NODE_CTF_EXPRESSION
:
4371 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
4373 _BT_LOGE_NODE(node
, "Cannot concatenate unary strings.");
4378 if (!strcmp(left
, "major")) {
4379 if (_IS_SET(set
, _TRACE_MAJOR_SET
)) {
4380 _BT_LOGE_DUP_ATTR(node
, "major", "trace");
4385 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
4389 "Unexpected unary expression for trace's `major` attribute.");
4394 _SET(set
, _TRACE_MAJOR_SET
);
4395 } else if (!strcmp(left
, "minor")) {
4396 if (_IS_SET(set
, _TRACE_MINOR_SET
)) {
4397 _BT_LOGE_DUP_ATTR(node
, "minor", "trace");
4402 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
4406 "Unexpected unary expression for trace's `minor` attribute.");
4411 _SET(set
, _TRACE_MINOR_SET
);
4412 } else if (!strcmp(left
, "uuid")) {
4413 if (_IS_SET(set
, _TRACE_UUID_SET
)) {
4414 _BT_LOGE_DUP_ATTR(node
, "uuid", "trace");
4419 ret
= get_unary_uuid(&node
->u
.ctf_expression
.right
,
4423 "Invalid trace's `uuid` attribute.");
4427 ret
= bt_trace_set_uuid(ctx
->trace
, ctx
->trace_uuid
);
4429 _BT_LOGE_NODE(node
, "Cannot set trace's UUID.");
4433 _SET(set
, _TRACE_UUID_SET
);
4434 } else if (!strcmp(left
, "byte_order")) {
4435 /* Native byte order is already known at this stage */
4436 if (_IS_SET(set
, _TRACE_BYTE_ORDER_SET
)) {
4437 _BT_LOGE_DUP_ATTR(node
, "byte_order",
4443 _SET(set
, _TRACE_BYTE_ORDER_SET
);
4444 } else if (!strcmp(left
, "packet.header")) {
4445 if (_IS_SET(set
, _TRACE_PACKET_HEADER_SET
)) {
4447 "Duplicate `packet.header` entry in trace.");
4452 ret
= visit_type_specifier_list(ctx
,
4453 _BT_LIST_FIRST_ENTRY(
4454 &node
->u
.ctf_expression
.right
,
4455 struct ctf_node
, siblings
),
4456 &packet_header_decl
);
4459 "Cannot create trace's packet header field type.");
4463 assert(packet_header_decl
);
4464 ret
= bt_trace_set_packet_header_type(ctx
->trace
,
4465 packet_header_decl
);
4466 BT_PUT(packet_header_decl
);
4469 "Cannot set trace's packet header field type.");
4473 _SET(set
, _TRACE_PACKET_HEADER_SET
);
4476 "Unknown attribute in stream class: "
4477 "attr-name=\"%s\"", left
);
4485 _BT_LOGE_NODE(node
, "Unknown expression in trace.");
4494 BT_PUT(packet_header_decl
);
4500 int visit_trace_decl(struct ctx
*ctx
, struct ctf_node
*node
)
4504 struct ctf_node
*iter
;
4505 struct bt_list_head
*decl_list
= &node
->u
.trace
.declaration_list
;
4507 if (node
->visited
) {
4511 node
->visited
= TRUE
;
4513 if (ctx
->is_trace_visited
) {
4514 _BT_LOGE_NODE(node
, "Duplicate trace (`trace` block).");
4519 ret
= ctx_push_scope(ctx
);
4521 BT_LOGE_STR("Cannot push scope.");
4525 bt_list_for_each_entry(iter
, decl_list
, siblings
) {
4526 ret
= visit_trace_decl_entry(ctx
, iter
, &set
);
4528 _BT_LOGE_NODE(iter
, "Cannot visit trace's entry (`trace` block): "
4537 if (!_IS_SET(&set
, _TRACE_MAJOR_SET
)) {
4539 "Missing `major` attribute in trace (`trace` block).");
4544 if (!_IS_SET(&set
, _TRACE_MINOR_SET
)) {
4546 "Missing `minor` attribute in trace (`trace` block).");
4551 if (!_IS_SET(&set
, _TRACE_BYTE_ORDER_SET
)) {
4553 "Missing `byte_order` attribute in trace (`trace` block).");
4558 ctx
->is_trace_visited
= TRUE
;
4568 int visit_env(struct ctx
*ctx
, struct ctf_node
*node
)
4572 struct ctf_node
*entry_node
;
4573 struct bt_list_head
*decl_list
= &node
->u
.env
.declaration_list
;
4575 if (node
->visited
) {
4579 node
->visited
= TRUE
;
4581 bt_list_for_each_entry(entry_node
, decl_list
, siblings
) {
4582 struct bt_list_head
*right_head
=
4583 &entry_node
->u
.ctf_expression
.right
;
4585 if (entry_node
->type
!= NODE_CTF_EXPRESSION
) {
4586 _BT_LOGE_NODE(entry_node
,
4587 "Wrong expression in environment entry: "
4588 "node-type=%d", entry_node
->type
);
4593 left
= concatenate_unary_strings(
4594 &entry_node
->u
.ctf_expression
.left
);
4596 _BT_LOGE_NODE(entry_node
,
4597 "Cannot get environment entry's name.");
4602 if (is_unary_string(right_head
)) {
4603 char *right
= concatenate_unary_strings(right_head
);
4606 _BT_LOGE_NODE(entry_node
,
4607 "Unexpected unary expression for environment entry's value: "
4608 "name=\"%s\"", left
);
4613 if (strcmp(left
, "tracer_name") == 0) {
4614 if (strncmp(right
, "lttng", 5) == 0) {
4615 BT_LOGI("Detected LTTng trace from `%s` environment value: "
4616 "tracer-name=\"%s\"",
4622 ret
= bt_trace_set_environment_field_string(
4623 ctx
->trace
, left
, right
);
4627 _BT_LOGE_NODE(entry_node
,
4628 "Cannot add string environment entry to trace: "
4629 "name=\"%s\", ret=%d", left
, ret
);
4632 } else if (is_unary_unsigned(right_head
) ||
4633 is_unary_signed(right_head
)) {
4636 if (is_unary_unsigned(right_head
)) {
4637 ret
= get_unary_unsigned(right_head
,
4640 ret
= get_unary_signed(right_head
, &v
);
4643 _BT_LOGE_NODE(entry_node
,
4644 "Unexpected unary expression for environment entry's value: "
4645 "name=\"%s\"", left
);
4650 ret
= bt_trace_set_environment_field_integer(
4651 ctx
->trace
, left
, v
);
4653 _BT_LOGE_NODE(entry_node
,
4654 "Cannot add integer environment entry to trace: "
4655 "name=\"%s\", ret=%d", left
, ret
);
4659 _BT_LOGW_NODE(entry_node
,
4660 "Environment entry has unknown type: "
4661 "name=\"%s\"", left
);
4678 int set_trace_byte_order(struct ctx
*ctx
, struct ctf_node
*trace_node
)
4683 struct ctf_node
*node
;
4684 struct bt_list_head
*decl_list
= &trace_node
->u
.trace
.declaration_list
;
4686 bt_list_for_each_entry(node
, decl_list
, siblings
) {
4687 if (node
->type
== NODE_CTF_EXPRESSION
) {
4688 struct ctf_node
*right_node
;
4690 left
= concatenate_unary_strings(
4691 &node
->u
.ctf_expression
.left
);
4694 "Cannot concatenate unary strings.");
4699 if (!strcmp(left
, "byte_order")) {
4700 enum bt_byte_order bo
;
4702 if (_IS_SET(&set
, _TRACE_BYTE_ORDER_SET
)) {
4703 _BT_LOGE_DUP_ATTR(node
, "byte_order",
4709 _SET(&set
, _TRACE_BYTE_ORDER_SET
);
4710 right_node
= _BT_LIST_FIRST_ENTRY(
4711 &node
->u
.ctf_expression
.right
,
4712 struct ctf_node
, siblings
);
4713 bo
= byte_order_from_unary_expr(right_node
);
4714 if (bo
== BT_BYTE_ORDER_UNKNOWN
) {
4716 "Invalid `byte_order` attribute in trace (`trace` block): "
4717 "expecting `le`, `be`, or `network`.");
4720 } else if (bo
== BT_BYTE_ORDER_NATIVE
) {
4722 "Invalid `byte_order` attribute in trace (`trace` block): "
4723 "cannot be set to `native` here.");
4729 ret
= bt_trace_set_native_byte_order(
4733 "Cannot set trace's byte order: "
4744 if (!_IS_SET(&set
, _TRACE_BYTE_ORDER_SET
)) {
4745 _BT_LOGE_NODE(trace_node
,
4746 "Missing `byte_order` attribute in trace (`trace` block).");
4760 int visit_clock_decl_entry(struct ctx
*ctx
, struct ctf_node
*entry_node
,
4761 struct bt_clock_class
*clock
, int *set
)
4766 if (entry_node
->type
!= NODE_CTF_EXPRESSION
) {
4767 _BT_LOGE_NODE(entry_node
,
4768 "Unexpected node type: node-type=%d",
4774 left
= concatenate_unary_strings(&entry_node
->u
.ctf_expression
.left
);
4776 _BT_LOGE_NODE(entry_node
, "Cannot concatenate unary strings.");
4781 if (!strcmp(left
, "name")) {
4784 if (_IS_SET(set
, _CLOCK_NAME_SET
)) {
4785 _BT_LOGE_DUP_ATTR(entry_node
, "name", "clock class");
4790 right
= concatenate_unary_strings(
4791 &entry_node
->u
.ctf_expression
.right
);
4793 _BT_LOGE_NODE(entry_node
,
4794 "Unexpected unary expression for clock class's `name` attribute.");
4799 ret
= bt_clock_class_set_name(clock
, right
);
4801 _BT_LOGE_NODE(entry_node
,
4802 "cannot set clock class's name");
4808 _SET(set
, _CLOCK_NAME_SET
);
4809 } else if (!strcmp(left
, "uuid")) {
4810 unsigned char uuid
[BABELTRACE_UUID_LEN
];
4812 if (_IS_SET(set
, _CLOCK_UUID_SET
)) {
4813 _BT_LOGE_DUP_ATTR(entry_node
, "uuid", "clock class");
4818 ret
= get_unary_uuid(&entry_node
->u
.ctf_expression
.right
, uuid
);
4820 _BT_LOGE_NODE(entry_node
,
4821 "Invalid clock class's `uuid` attribute.");
4825 ret
= bt_clock_class_set_uuid(clock
, uuid
);
4827 _BT_LOGE_NODE(entry_node
,
4828 "Cannot set clock class's UUID.");
4832 _SET(set
, _CLOCK_UUID_SET
);
4833 } else if (!strcmp(left
, "description")) {
4836 if (_IS_SET(set
, _CLOCK_DESCRIPTION_SET
)) {
4837 _BT_LOGE_DUP_ATTR(entry_node
, "description",
4843 right
= concatenate_unary_strings(
4844 &entry_node
->u
.ctf_expression
.right
);
4846 _BT_LOGE_NODE(entry_node
,
4847 "Unexpected unary expression for clock class's `description` attribute.");
4852 ret
= bt_clock_class_set_description(clock
, right
);
4854 _BT_LOGE_NODE(entry_node
,
4855 "Cannot set clock class's description.");
4861 _SET(set
, _CLOCK_DESCRIPTION_SET
);
4862 } else if (!strcmp(left
, "freq")) {
4865 if (_IS_SET(set
, _CLOCK_FREQ_SET
)) {
4866 _BT_LOGE_DUP_ATTR(entry_node
, "freq", "clock class");
4871 ret
= get_unary_unsigned(
4872 &entry_node
->u
.ctf_expression
.right
, &freq
);
4874 _BT_LOGE_NODE(entry_node
,
4875 "Unexpected unary expression for clock class's `freq` attribute.");
4880 if (freq
== -1ULL || freq
== 0) {
4881 _BT_LOGE_NODE(entry_node
,
4882 "Invalid clock class frequency: freq=%" PRIu64
,
4888 ret
= bt_clock_class_set_frequency(clock
, freq
);
4890 _BT_LOGE_NODE(entry_node
,
4891 "Cannot set clock class's frequency.");
4895 _SET(set
, _CLOCK_FREQ_SET
);
4896 } else if (!strcmp(left
, "precision")) {
4899 if (_IS_SET(set
, _CLOCK_PRECISION_SET
)) {
4900 _BT_LOGE_DUP_ATTR(entry_node
, "precision",
4906 ret
= get_unary_unsigned(
4907 &entry_node
->u
.ctf_expression
.right
, &precision
);
4909 _BT_LOGE_NODE(entry_node
,
4910 "Unexpected unary expression for clock class's `precision` attribute.");
4915 ret
= bt_clock_class_set_precision(clock
, precision
);
4917 _BT_LOGE_NODE(entry_node
,
4918 "Cannot set clock class's precision.");
4922 _SET(set
, _CLOCK_PRECISION_SET
);
4923 } else if (!strcmp(left
, "offset_s")) {
4926 if (_IS_SET(set
, _CLOCK_OFFSET_S_SET
)) {
4927 _BT_LOGE_DUP_ATTR(entry_node
, "offset_s",
4933 ret
= get_unary_signed(
4934 &entry_node
->u
.ctf_expression
.right
, &offset_s
);
4936 _BT_LOGE_NODE(entry_node
,
4937 "Unexpected unary expression for clock class's `offset_s` attribute.");
4942 ret
= bt_clock_class_set_offset_s(clock
, offset_s
);
4944 _BT_LOGE_NODE(entry_node
,
4945 "Cannot set clock class's offset in seconds.");
4949 _SET(set
, _CLOCK_OFFSET_S_SET
);
4950 } else if (!strcmp(left
, "offset")) {
4953 if (_IS_SET(set
, _CLOCK_OFFSET_SET
)) {
4954 _BT_LOGE_DUP_ATTR(entry_node
, "offset", "clock class");
4959 ret
= get_unary_signed(
4960 &entry_node
->u
.ctf_expression
.right
, &offset
);
4962 _BT_LOGE_NODE(entry_node
,
4963 "Unexpected unary expression for clock class's `offset` attribute.");
4968 ret
= bt_clock_class_set_offset_cycles(clock
, offset
);
4970 _BT_LOGE_NODE(entry_node
,
4971 "Cannot set clock class's offset in cycles.");
4975 _SET(set
, _CLOCK_OFFSET_SET
);
4976 } else if (!strcmp(left
, "absolute")) {
4977 struct ctf_node
*right
;
4979 if (_IS_SET(set
, _CLOCK_ABSOLUTE_SET
)) {
4980 _BT_LOGE_DUP_ATTR(entry_node
, "absolute",
4986 right
= _BT_LIST_FIRST_ENTRY(
4987 &entry_node
->u
.ctf_expression
.right
,
4988 struct ctf_node
, siblings
);
4989 ret
= get_boolean(right
);
4991 _BT_LOGE_NODE(entry_node
,
4992 "Unexpected unary expression for clock class's `absolute` attribute.");
4997 ret
= bt_clock_class_set_is_absolute(clock
, ret
);
4999 _BT_LOGE_NODE(entry_node
,
5000 "Cannot set clock class's absolute flag.");
5004 _SET(set
, _CLOCK_ABSOLUTE_SET
);
5006 _BT_LOGW_NODE(entry_node
,
5007 "Unknown attribute in clock class: attr-name=\"%s\"",
5023 int64_t cycles_from_ns(uint64_t frequency
, int64_t ns
)
5028 if (frequency
== 1000000000ULL) {
5031 cycles
= (uint64_t) (((double) ns
* (double) frequency
) / 1e9
);
5038 int apply_clock_class_offset(struct ctx
*ctx
, struct bt_clock_class
*clock
)
5042 int64_t offset_cycles
;
5043 int64_t offset_to_apply
;
5045 freq
= bt_clock_class_get_frequency(clock
);
5046 if (freq
== -1ULL) {
5047 BT_LOGE_STR("Cannot get clock class's frequency.");
5052 ret
= bt_clock_class_get_offset_cycles(clock
, &offset_cycles
);
5054 BT_LOGE_STR("Cannot get clock class's offset in cycles.");
5060 ctx
->decoder_config
.clock_class_offset_s
* 1000000000LL +
5061 ctx
->decoder_config
.clock_class_offset_ns
;
5062 offset_cycles
+= cycles_from_ns(freq
, offset_to_apply
);
5063 ret
= bt_clock_class_set_offset_cycles(clock
, offset_cycles
);
5070 int visit_clock_decl(struct ctx
*ctx
, struct ctf_node
*clock_node
)
5074 struct bt_clock_class
*clock
;
5075 struct ctf_node
*entry_node
;
5076 struct bt_list_head
*decl_list
= &clock_node
->u
.clock
.declaration_list
;
5077 const char *clock_class_name
;
5079 if (clock_node
->visited
) {
5083 clock_node
->visited
= TRUE
;
5085 /* CTF 1.8's default frequency for a clock class is 1 GHz */
5086 clock
= bt_clock_class_create(NULL
, 1000000000);
5088 _BT_LOGE_NODE(clock_node
,
5089 "Cannot create default clock class.");
5094 bt_list_for_each_entry(entry_node
, decl_list
, siblings
) {
5095 ret
= visit_clock_decl_entry(ctx
, entry_node
, clock
, &set
);
5097 _BT_LOGE_NODE(entry_node
,
5098 "Cannot visit clock class's entry: ret=%d",
5104 if (!_IS_SET(&set
, _CLOCK_NAME_SET
)) {
5105 _BT_LOGE_NODE(clock_node
,
5106 "Missing `name` attribute in clock class.");
5111 clock_class_name
= bt_clock_class_get_name(clock
);
5112 assert(clock_class_name
);
5113 if (ctx
->is_lttng
&& strcmp(clock_class_name
, "monotonic") == 0) {
5115 * Old versions of LTTng forgot to set its clock class
5116 * as absolute, even if it is. This is important because
5117 * it's a condition to be able to sort notifications
5118 * from different sources.
5120 ret
= bt_clock_class_set_is_absolute(clock
, 1);
5122 _BT_LOGE_NODE(clock_node
,
5123 "Cannot set clock class's absolute flag.");
5128 ret
= apply_clock_class_offset(ctx
, clock
);
5130 _BT_LOGE_NODE(clock_node
,
5131 "Cannot apply clock class's custom offset.");
5135 ret
= bt_trace_add_clock_class(ctx
->trace
, clock
);
5137 _BT_LOGE_NODE(clock_node
,
5138 "Cannot add clock class to trace.");
5149 int visit_root_decl(struct ctx
*ctx
, struct ctf_node
*root_decl_node
)
5153 if (root_decl_node
->visited
) {
5157 root_decl_node
->visited
= TRUE
;
5159 switch (root_decl_node
->type
) {
5161 ret
= visit_typedef(ctx
,
5162 root_decl_node
->u
._typedef
.type_specifier_list
,
5163 &root_decl_node
->u
._typedef
.type_declarators
);
5165 _BT_LOGE_NODE(root_decl_node
,
5166 "Cannot add type definition found in root scope.");
5170 case NODE_TYPEALIAS
:
5171 ret
= visit_typealias(ctx
, root_decl_node
->u
.typealias
.target
,
5172 root_decl_node
->u
.typealias
.alias
);
5174 _BT_LOGE_NODE(root_decl_node
,
5175 "Cannot add type alias found in root scope.");
5179 case NODE_TYPE_SPECIFIER_LIST
:
5181 _BT_FIELD_TYPE_INIT(decl
);
5184 * Just add the type specifier to the root
5185 * declaration scope. Put local reference.
5187 ret
= visit_type_specifier_list(ctx
, root_decl_node
, &decl
);
5189 _BT_LOGE_NODE(root_decl_node
,
5190 "Cannot visit root scope's field type: "
5200 _BT_LOGE_NODE(root_decl_node
,
5201 "Unexpected node type: node-type=%d",
5202 root_decl_node
->type
);
5212 int set_trace_name(struct ctx
*ctx
)
5216 struct bt_value
*value
= NULL
;
5218 assert(bt_trace_get_stream_class_count(ctx
->trace
) == 0);
5219 name
= g_string_new(NULL
);
5221 BT_LOGE_STR("Failed to allocate a GString.");
5227 * Check if we have a trace environment string value named `hostname`.
5228 * If so, use it as the trace name's prefix.
5230 value
= bt_trace_get_environment_field_value_by_name(ctx
->trace
,
5232 if (bt_value_is_string(value
)) {
5233 const char *hostname
;
5235 ret
= bt_value_string_get(value
, &hostname
);
5237 g_string_append(name
, hostname
);
5239 if (ctx
->trace_name_suffix
) {
5240 g_string_append_c(name
, G_DIR_SEPARATOR
);
5244 if (ctx
->trace_name_suffix
) {
5245 g_string_append(name
, ctx
->trace_name_suffix
);
5248 ret
= bt_trace_set_name(ctx
->trace
, name
->str
);
5250 BT_LOGE("Cannot set trace's name: name=\"%s\"", name
->str
);
5263 g_string_free(name
, TRUE
);
5270 int move_ctx_stream_classes_to_trace(struct ctx
*ctx
)
5273 GHashTableIter iter
;
5274 gpointer key
, stream_class
;
5276 if (g_hash_table_size(ctx
->stream_classes
) > 0 &&
5277 bt_trace_get_stream_class_count(ctx
->trace
) == 0) {
5279 * We're about to add the first stream class to the
5280 * trace. This will freeze the trace, and after this
5281 * we cannot set the name anymore. At this point,
5282 * set the trace name.
5284 ret
= set_trace_name(ctx
);
5286 BT_LOGE_STR("Cannot set trace's name.");
5291 g_hash_table_iter_init(&iter
, ctx
->stream_classes
);
5293 while (g_hash_table_iter_next(&iter
, &key
, &stream_class
)) {
5294 ret
= bt_trace_add_stream_class(ctx
->trace
,
5297 int64_t id
= bt_stream_class_get_id(stream_class
);
5298 BT_LOGE("Cannot add stream class to trace: id=%" PRId64
,
5304 g_hash_table_remove_all(ctx
->stream_classes
);
5311 struct ctf_visitor_generate_ir
*ctf_visitor_generate_ir_create(
5312 const struct ctf_metadata_decoder_config
*decoder_config
,
5316 struct ctx
*ctx
= NULL
;
5317 struct bt_trace
*trace
;
5319 trace
= bt_trace_create();
5321 BT_LOGE_STR("Cannot create empty trace.");
5325 /* Set packet header to NULL to override the default one */
5326 ret
= bt_trace_set_packet_header_type(trace
, NULL
);
5328 BT_LOGE_STR("Cannot reset initial trace's packet header field type.");
5332 /* Create visitor's context */
5333 ctx
= ctx_create(trace
, decoder_config
, name
);
5335 BT_LOGE_STR("Cannot create visitor's context.");
5348 return (void *) ctx
;
5352 void ctf_visitor_generate_ir_destroy(struct ctf_visitor_generate_ir
*visitor
)
5354 ctx_destroy((void *) visitor
);
5358 struct bt_trace
*ctf_visitor_generate_ir_get_trace(
5359 struct ctf_visitor_generate_ir
*visitor
)
5361 struct ctx
*ctx
= (void *) visitor
;
5365 return bt_get(ctx
->trace
);
5369 int ctf_visitor_generate_ir_visit_node(struct ctf_visitor_generate_ir
*visitor
,
5370 struct ctf_node
*node
)
5373 struct ctx
*ctx
= (void *) visitor
;
5375 BT_LOGI_STR("Visiting metadata's AST to generate CTF IR objects.");
5377 switch (node
->type
) {
5380 struct ctf_node
*iter
;
5381 int got_trace_decl
= FALSE
;
5384 * The first thing we need is the native byte order of
5385 * the trace block, because early type aliases can have
5386 * a `byte_order` attribute set to `native`. If we don't
5387 * have the native byte order yet, and we don't have any
5388 * trace block yet, then fail with EINCOMPLETE.
5390 if (ctx
->trace_bo
== BT_BYTE_ORDER_NATIVE
) {
5391 bt_list_for_each_entry(iter
, &node
->u
.root
.trace
, siblings
) {
5392 if (got_trace_decl
) {
5394 "Duplicate trace (`trace` block).");
5399 ret
= set_trace_byte_order(ctx
, iter
);
5402 "Cannot set trace's native byte order: "
5407 got_trace_decl
= TRUE
;
5410 if (!got_trace_decl
) {
5411 BT_LOGD_STR("Incomplete AST: need trace (`trace` block).");
5417 assert(ctx
->trace_bo
== BT_BYTE_ORDER_LITTLE_ENDIAN
||
5418 ctx
->trace_bo
== BT_BYTE_ORDER_BIG_ENDIAN
);
5419 assert(ctx
->current_scope
&&
5420 ctx
->current_scope
->parent_scope
== NULL
);
5423 bt_list_for_each_entry(iter
, &node
->u
.root
.env
, siblings
) {
5424 ret
= visit_env(ctx
, iter
);
5427 "Cannot visit trace's environment (`env` block) entry: "
5433 assert(ctx
->current_scope
&&
5434 ctx
->current_scope
->parent_scope
== NULL
);
5437 * Visit clock blocks.
5439 bt_list_for_each_entry(iter
, &node
->u
.root
.clock
, siblings
) {
5440 ret
= visit_clock_decl(ctx
, iter
);
5443 "Cannot visit clock class: ret=%d",
5449 assert(ctx
->current_scope
&&
5450 ctx
->current_scope
->parent_scope
== NULL
);
5453 * Visit root declarations next, as they can be used by any
5456 bt_list_for_each_entry(iter
, &node
->u
.root
.declaration_list
,
5458 ret
= visit_root_decl(ctx
, iter
);
5461 "Cannot visit root entry: ret=%d",
5467 assert(ctx
->current_scope
&&
5468 ctx
->current_scope
->parent_scope
== NULL
);
5470 /* Callsite blocks are not supported */
5471 bt_list_for_each_entry(iter
, &node
->u
.root
.callsite
, siblings
) {
5473 "\"callsite\" blocks are not supported as of this version.");
5476 assert(ctx
->current_scope
&&
5477 ctx
->current_scope
->parent_scope
== NULL
);
5480 bt_list_for_each_entry(iter
, &node
->u
.root
.trace
, siblings
) {
5481 ret
= visit_trace_decl(ctx
, iter
);
5484 "Cannot visit trace (`trace` block): "
5490 assert(ctx
->current_scope
&&
5491 ctx
->current_scope
->parent_scope
== NULL
);
5494 bt_list_for_each_entry(iter
, &node
->u
.root
.stream
, siblings
) {
5495 ret
= visit_stream_decl(ctx
, iter
);
5498 "Cannot visit stream class: ret=%d",
5504 assert(ctx
->current_scope
&&
5505 ctx
->current_scope
->parent_scope
== NULL
);
5508 bt_list_for_each_entry(iter
, &node
->u
.root
.event
, siblings
) {
5509 ret
= visit_event_decl(ctx
, iter
);
5512 "Cannot visit event class: ret=%d",
5518 assert(ctx
->current_scope
&&
5519 ctx
->current_scope
->parent_scope
== NULL
);
5524 "Unexpected node type: node-type=%d",
5530 /* Move decoded stream classes to trace, if any */
5531 ret
= move_ctx_stream_classes_to_trace(ctx
);
5533 BT_LOGE("Cannot move stream classes to trace: ret=%d", ret
);