2 * ctf-visitor-generate-ir.c
4 * Common Trace Format metadata visitor (generates CTF IR objects).
6 * Based on older ctf-visitor-generate-io-struct.c.
8 * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
9 * Copyright 2015-2016 - Philippe Proulx <philippe.proulx@efficios.com>
11 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 #define BT_LOG_TAG "PLUGIN-CTF-METADATA-IR-VISITOR"
39 #include <babeltrace/assert-internal.h>
43 #include <babeltrace/common-internal.h>
44 #include <babeltrace/compat/uuid-internal.h>
45 #include <babeltrace/endian-internal.h>
46 #include <babeltrace/babeltrace.h>
53 /* Bit value (left shift) */
54 #define _BV(_val) (1 << (_val))
56 /* Bit is set in a set of bits */
57 #define _IS_SET(_set, _mask) (*(_set) & (_mask))
59 /* Set bit in a set of bits */
60 #define _SET(_set, _mask) (*(_set) |= (_mask))
62 /* Bits for verifying existing attributes in various declarations */
64 _CLOCK_NAME_SET
= _BV(0),
65 _CLOCK_UUID_SET
= _BV(1),
66 _CLOCK_FREQ_SET
= _BV(2),
67 _CLOCK_PRECISION_SET
= _BV(3),
68 _CLOCK_OFFSET_S_SET
= _BV(4),
69 _CLOCK_OFFSET_SET
= _BV(5),
70 _CLOCK_ABSOLUTE_SET
= _BV(6),
71 _CLOCK_DESCRIPTION_SET
= _BV(7),
75 _INTEGER_ALIGN_SET
= _BV(0),
76 _INTEGER_SIZE_SET
= _BV(1),
77 _INTEGER_BASE_SET
= _BV(2),
78 _INTEGER_ENCODING_SET
= _BV(3),
79 _INTEGER_BYTE_ORDER_SET
= _BV(4),
80 _INTEGER_SIGNED_SET
= _BV(5),
81 _INTEGER_MAP_SET
= _BV(6),
85 _FLOAT_ALIGN_SET
= _BV(0),
86 _FLOAT_MANT_DIG_SET
= _BV(1),
87 _FLOAT_EXP_DIG_SET
= _BV(2),
88 _FLOAT_BYTE_ORDER_SET
= _BV(3),
92 _STRING_ENCODING_SET
= _BV(0),
96 _TRACE_MINOR_SET
= _BV(0),
97 _TRACE_MAJOR_SET
= _BV(1),
98 _TRACE_BYTE_ORDER_SET
= _BV(2),
99 _TRACE_UUID_SET
= _BV(3),
100 _TRACE_PACKET_HEADER_SET
= _BV(4),
104 _STREAM_ID_SET
= _BV(0),
105 _STREAM_PACKET_CONTEXT_SET
= _BV(1),
106 _STREAM_EVENT_HEADER_SET
= _BV(2),
107 _STREAM_EVENT_CONTEXT_SET
= _BV(3),
111 _EVENT_NAME_SET
= _BV(0),
112 _EVENT_ID_SET
= _BV(1),
113 _EVENT_MODEL_EMF_URI_SET
= _BV(2),
114 _EVENT_STREAM_ID_SET
= _BV(3),
115 _EVENT_LOGLEVEL_SET
= _BV(4),
116 _EVENT_CONTEXT_SET
= _BV(5),
117 _EVENT_FIELDS_SET
= _BV(6),
125 LOGLEVEL_WARNING
= 4,
128 LOGLEVEL_DEBUG_SYSTEM
= 7,
129 LOGLEVEL_DEBUG_PROGRAM
= 8,
130 LOGLEVEL_DEBUG_PROCESS
= 9,
131 LOGLEVEL_DEBUG_MODULE
= 10,
132 LOGLEVEL_DEBUG_UNIT
= 11,
133 LOGLEVEL_DEBUG_FUNCTION
= 12,
134 LOGLEVEL_DEBUG_LINE
= 13,
139 /* Prefixes of type aliases */
140 #define _PREFIX_ALIAS 'a'
141 #define _PREFIX_ENUM 'e'
142 #define _PREFIX_STRUCT 's'
143 #define _PREFIX_VARIANT 'v'
145 /* First entry in a BT list */
146 #define _BT_LIST_FIRST_ENTRY(_ptr, _type, _member) \
147 bt_list_entry((_ptr)->next, _type, _member)
149 #define _BT_FIELD_TYPE_INIT(_name) struct bt_field_type *_name = NULL;
151 #define _BT_LOGE_DUP_ATTR(_node, _attr, _entity) \
152 _BT_LOGE_LINENO((_node)->lineno, \
153 "Duplicate attribute in %s: attr-name=\"%s\"", \
156 #define _BT_LOGE_NODE(_node, _msg, args...) \
157 _BT_LOGE_LINENO((_node)->lineno, _msg, ## args)
159 #define _BT_LOGW_NODE(_node, _msg, args...) \
160 _BT_LOGW_LINENO((_node)->lineno, _msg, ## args)
162 #define _BT_LOGV_NODE(_node, _msg, args...) \
163 _BT_LOGV_LINENO((_node)->lineno, _msg, ## args)
166 * Declaration scope of a visitor context. This represents a TSDL
167 * lexical scope, so that aliases and named structures, variants,
168 * and enumerations may be registered and looked up hierarchically.
170 struct ctx_decl_scope
{
172 * Alias name to field type.
174 * GQuark -> struct bt_field_type *
176 GHashTable
*decl_map
;
178 /* Parent scope; NULL if this is the root declaration scope */
179 struct ctx_decl_scope
*parent_scope
;
183 * Visitor context (private).
186 /* Trace being filled (owned by this) */
187 struct bt_trace
*trace
;
189 /* Current declaration scope (top of the stack) */
190 struct ctx_decl_scope
*current_scope
;
192 /* 1 if trace declaration is visited */
193 int is_trace_visited
;
195 /* 1 if this is an LTTng trace */
198 /* Eventual name suffix of the trace to set */
199 char *trace_name_suffix
;
201 /* Trace attributes */
202 enum bt_byte_order trace_bo
;
203 uint64_t trace_major
;
204 uint64_t trace_minor
;
205 unsigned char trace_uuid
[BABELTRACE_UUID_LEN
];
208 * Stream IDs to stream classes.
210 * int64_t -> struct bt_stream_class *
212 GHashTable
*stream_classes
;
214 /* Config passed by the user */
215 struct ctf_metadata_decoder_config decoder_config
;
221 struct ctf_visitor_generate_ir
{ };
224 * Creates a new declaration scope.
226 * @param par_scope Parent scope (NULL if creating a root scope)
227 * @returns New declaration scope, or NULL on error
230 struct ctx_decl_scope
*ctx_decl_scope_create(struct ctx_decl_scope
*par_scope
)
232 struct ctx_decl_scope
*scope
;
234 scope
= g_new(struct ctx_decl_scope
, 1);
236 BT_LOGE_STR("Failed to allocate one declaration scope.");
240 scope
->decl_map
= g_hash_table_new_full(g_direct_hash
, g_direct_equal
,
241 NULL
, (GDestroyNotify
) bt_put
);
242 scope
->parent_scope
= par_scope
;
249 * Destroys a declaration scope.
251 * This function does not destroy the parent scope.
253 * @param scope Scope to destroy
256 void ctx_decl_scope_destroy(struct ctx_decl_scope
*scope
)
262 g_hash_table_destroy(scope
->decl_map
);
270 * Returns the GQuark of a prefixed alias.
272 * @param prefix Prefix character
274 * @returns Associated GQuark, or 0 on error
277 GQuark
get_prefixed_named_quark(char prefix
, const char *name
)
283 /* Prefix character + original string + '\0' */
284 char *prname
= g_new(char, strlen(name
) + 2);
286 BT_LOGE_STR("Failed to allocate a string.");
290 sprintf(prname
, "%c%s", prefix
, name
);
291 qname
= g_quark_from_string(prname
);
299 * Looks up a prefixed type alias within a declaration scope.
301 * @param scope Declaration scope
302 * @param prefix Prefix character
303 * @param name Alias name
304 * @param level Number of levels to dig (-1 means infinite)
305 * @returns Declaration, or NULL if not found
308 struct bt_field_type
*ctx_decl_scope_lookup_prefix_alias(
309 struct ctx_decl_scope
*scope
, char prefix
,
310 const char *name
, int levels
)
314 _BT_FIELD_TYPE_INIT(decl
);
315 struct ctx_decl_scope
*cur_scope
= scope
;
319 qname
= get_prefixed_named_quark(prefix
, name
);
328 while (cur_scope
&& cur_levels
< levels
) {
329 decl
= g_hash_table_lookup(cur_scope
->decl_map
,
330 (gconstpointer
) GUINT_TO_POINTER(qname
));
332 /* Caller's reference */
337 cur_scope
= cur_scope
->parent_scope
;
348 * Looks up a type alias within a declaration scope.
350 * @param scope Declaration scope
351 * @param name Alias name
352 * @param level Number of levels to dig (-1 means infinite)
353 * @returns Declaration, or NULL if not found
356 struct bt_field_type
*ctx_decl_scope_lookup_alias(
357 struct ctx_decl_scope
*scope
, const char *name
, int levels
)
359 return ctx_decl_scope_lookup_prefix_alias(scope
, _PREFIX_ALIAS
,
364 * Looks up an enumeration within a declaration scope.
366 * @param scope Declaration scope
367 * @param name Enumeration name
368 * @param level Number of levels to dig (-1 means infinite)
369 * @returns Declaration, or NULL if not found
372 struct bt_field_type
*ctx_decl_scope_lookup_enum(
373 struct ctx_decl_scope
*scope
, const char *name
, int levels
)
375 return ctx_decl_scope_lookup_prefix_alias(scope
, _PREFIX_ENUM
,
380 * Looks up a structure within a declaration scope.
382 * @param scope Declaration scope
383 * @param name Structure name
384 * @param level Number of levels to dig (-1 means infinite)
385 * @returns Declaration, or NULL if not found
388 struct bt_field_type
*ctx_decl_scope_lookup_struct(
389 struct ctx_decl_scope
*scope
, const char *name
, int levels
)
391 return ctx_decl_scope_lookup_prefix_alias(scope
, _PREFIX_STRUCT
,
396 * Looks up a variant within a declaration scope.
398 * @param scope Declaration scope
399 * @param name Variant name
400 * @param level Number of levels to dig (-1 means infinite)
401 * @returns Declaration, or NULL if not found
404 struct bt_field_type
*ctx_decl_scope_lookup_variant(
405 struct ctx_decl_scope
*scope
, const char *name
, int levels
)
407 return ctx_decl_scope_lookup_prefix_alias(scope
, _PREFIX_VARIANT
,
412 * Registers a prefixed type alias within a declaration scope.
414 * @param scope Declaration scope
415 * @param prefix Prefix character
416 * @param name Alias name (non-NULL)
417 * @param decl Declaration to register
418 * @returns 0 if registration went okay, negative value otherwise
421 int ctx_decl_scope_register_prefix_alias(struct ctx_decl_scope
*scope
,
422 char prefix
, const char *name
, struct bt_field_type
*decl
)
426 _BT_FIELD_TYPE_INIT(edecl
);
431 qname
= get_prefixed_named_quark(prefix
, name
);
437 /* Make sure alias does not exist in local scope */
438 edecl
= ctx_decl_scope_lookup_prefix_alias(scope
, prefix
, name
, 1);
445 g_hash_table_insert(scope
->decl_map
,
446 GUINT_TO_POINTER(qname
), decl
);
448 /* Hash table's reference */
458 * Registers a type alias within a declaration scope.
460 * @param scope Declaration scope
461 * @param name Alias name (non-NULL)
462 * @param decl Declaration to register
463 * @returns 0 if registration went okay, negative value otherwise
466 int ctx_decl_scope_register_alias(struct ctx_decl_scope
*scope
,
467 const char *name
, struct bt_field_type
*decl
)
469 return ctx_decl_scope_register_prefix_alias(scope
, _PREFIX_ALIAS
,
474 * Registers an enumeration declaration within a declaration scope.
476 * @param scope Declaration scope
477 * @param name Enumeration name (non-NULL)
478 * @param decl Enumeration declaration to register
479 * @returns 0 if registration went okay, negative value otherwise
482 int ctx_decl_scope_register_enum(struct ctx_decl_scope
*scope
,
483 const char *name
, struct bt_field_type
*decl
)
485 return ctx_decl_scope_register_prefix_alias(scope
, _PREFIX_ENUM
,
490 * Registers a structure declaration within a declaration scope.
492 * @param scope Declaration scope
493 * @param name Structure name (non-NULL)
494 * @param decl Structure declaration to register
495 * @returns 0 if registration went okay, negative value otherwise
498 int ctx_decl_scope_register_struct(struct ctx_decl_scope
*scope
,
499 const char *name
, struct bt_field_type
*decl
)
501 return ctx_decl_scope_register_prefix_alias(scope
, _PREFIX_STRUCT
,
506 * Registers a variant declaration within a declaration scope.
508 * @param scope Declaration scope
509 * @param name Variant name (non-NULL)
510 * @param decl Variant declaration to register
511 * @returns 0 if registration went okay, negative value otherwise
514 int ctx_decl_scope_register_variant(struct ctx_decl_scope
*scope
,
515 const char *name
, struct bt_field_type
*decl
)
517 return ctx_decl_scope_register_prefix_alias(scope
, _PREFIX_VARIANT
,
522 * Destroys a visitor context.
524 * @param ctx Visitor context to destroy
527 void ctx_destroy(struct ctx
*ctx
)
529 struct ctx_decl_scope
*scope
;
531 * Destroy all scopes, from current one to the root scope.
538 scope
= ctx
->current_scope
;
541 struct ctx_decl_scope
*parent_scope
= scope
->parent_scope
;
543 ctx_decl_scope_destroy(scope
);
544 scope
= parent_scope
;
549 if (ctx
->stream_classes
) {
550 g_hash_table_destroy(ctx
->stream_classes
);
553 free(ctx
->trace_name_suffix
);
561 * Creates a new visitor context.
563 * @param trace Associated trace
564 * @returns New visitor context, or NULL on error
567 struct ctx
*ctx_create(struct bt_trace
*trace
,
568 const struct ctf_metadata_decoder_config
*decoder_config
,
569 const char *trace_name_suffix
)
571 struct ctx
*ctx
= NULL
;
572 struct ctx_decl_scope
*scope
= NULL
;
574 BT_ASSERT(decoder_config
);
576 ctx
= g_new0(struct ctx
, 1);
578 BT_LOGE_STR("Failed to allocate one visitor context.");
582 /* Root declaration scope */
583 scope
= ctx_decl_scope_create(NULL
);
585 BT_LOGE_STR("Cannot create declaration scope.");
589 ctx
->stream_classes
= g_hash_table_new_full(g_int64_hash
,
590 g_int64_equal
, g_free
, (GDestroyNotify
) bt_put
);
591 if (!ctx
->stream_classes
) {
592 BT_LOGE_STR("Failed to allocate a GHashTable.");
596 if (trace_name_suffix
) {
597 ctx
->trace_name_suffix
= strdup(trace_name_suffix
);
598 if (!ctx
->trace_name_suffix
) {
599 BT_LOGE_STR("Failed to copy string.");
605 ctx
->current_scope
= scope
;
607 ctx
->trace_bo
= BT_BYTE_ORDER_NATIVE
;
608 ctx
->decoder_config
= *decoder_config
;
613 ctx_decl_scope_destroy(scope
);
618 * Pushes a new declaration scope on top of a visitor context's
619 * declaration scope stack.
621 * @param ctx Visitor context
622 * @returns 0 on success, or a negative value on error
625 int ctx_push_scope(struct ctx
*ctx
)
628 struct ctx_decl_scope
*new_scope
;
631 new_scope
= ctx_decl_scope_create(ctx
->current_scope
);
633 BT_LOGE_STR("Cannot create declaration scope.");
638 ctx
->current_scope
= new_scope
;
645 void ctx_pop_scope(struct ctx
*ctx
)
647 struct ctx_decl_scope
*parent_scope
= NULL
;
651 if (!ctx
->current_scope
) {
655 parent_scope
= ctx
->current_scope
->parent_scope
;
656 ctx_decl_scope_destroy(ctx
->current_scope
);
657 ctx
->current_scope
= parent_scope
;
664 int visit_type_specifier_list(struct ctx
*ctx
, struct ctf_node
*ts_list
,
665 struct bt_field_type
**decl
);
668 char *remove_underscores_from_field_ref(const char *field_ref
)
674 UNDERSCORE_REMOVE_STATE_REMOVE_NEXT_UNDERSCORE
,
675 UNDERSCORE_REMOVE_STATE_DO_NOT_REMOVE_NEXT_UNDERSCORE
,
676 } state
= UNDERSCORE_REMOVE_STATE_REMOVE_NEXT_UNDERSCORE
;
678 BT_ASSERT(field_ref
);
679 ret
= calloc(strlen(field_ref
) + 1, 1);
681 BT_LOGE("Failed to allocate a string: size=%zu",
682 strlen(field_ref
) + 1);
689 while (*in_ch
!= '\0') {
693 /* Remove whitespace */
697 if (state
== UNDERSCORE_REMOVE_STATE_REMOVE_NEXT_UNDERSCORE
) {
699 state
= UNDERSCORE_REMOVE_STATE_DO_NOT_REMOVE_NEXT_UNDERSCORE
;
705 state
= UNDERSCORE_REMOVE_STATE_REMOVE_NEXT_UNDERSCORE
;
708 state
= UNDERSCORE_REMOVE_STATE_DO_NOT_REMOVE_NEXT_UNDERSCORE
;
723 int is_unary_string(struct bt_list_head
*head
)
726 struct ctf_node
*node
;
728 bt_list_for_each_entry(node
, head
, siblings
) {
729 if (node
->type
!= NODE_UNARY_EXPRESSION
) {
733 if (node
->u
.unary_expression
.type
!= UNARY_STRING
) {
742 char *concatenate_unary_strings(struct bt_list_head
*head
)
746 struct ctf_node
*node
;
748 str
= g_string_new(NULL
);
751 bt_list_for_each_entry(node
, head
, siblings
) {
755 node
->type
!= NODE_UNARY_EXPRESSION
||
756 node
->u
.unary_expression
.type
!= UNARY_STRING
||
759 node
->u
.unary_expression
.link
!=
767 switch (node
->u
.unary_expression
.link
) {
769 g_string_append(str
, ".");
771 case UNARY_ARROWLINK
:
772 g_string_append(str
, "->");
774 case UNARY_DOTDOTDOT
:
775 g_string_append(str
, "...");
781 src_string
= node
->u
.unary_expression
.u
.string
;
782 g_string_append(str
, src_string
);
786 /* Destroys the container, returns the underlying string */
787 return g_string_free(str
, FALSE
);
790 /* This always returns NULL */
791 return g_string_free(str
, TRUE
);
795 const char *get_map_clock_name_value(struct bt_list_head
*head
)
798 struct ctf_node
*node
;
799 const char *name
= NULL
;
801 bt_list_for_each_entry(node
, head
, siblings
) {
803 int uexpr_type
= node
->u
.unary_expression
.type
;
804 int uexpr_link
= node
->u
.unary_expression
.link
;
805 int cond
= node
->type
!= NODE_UNARY_EXPRESSION
||
806 uexpr_type
!= UNARY_STRING
||
807 !((uexpr_link
!= UNARY_LINK_UNKNOWN
) ^ (i
== 0));
812 /* Needs to be chained with . */
813 switch (node
->u
.unary_expression
.link
) {
816 case UNARY_ARROWLINK
:
817 case UNARY_DOTDOTDOT
:
823 src_string
= node
->u
.unary_expression
.u
.string
;
827 if (strcmp("clock", src_string
)) {
835 if (strcmp("value", src_string
)) {
840 /* Extra identifier, unknown */
854 int is_unary_unsigned(struct bt_list_head
*head
)
857 struct ctf_node
*node
;
859 bt_list_for_each_entry(node
, head
, siblings
) {
860 if (node
->type
!= NODE_UNARY_EXPRESSION
) {
864 if (node
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
873 int get_unary_unsigned(struct bt_list_head
*head
, uint64_t *value
)
877 struct ctf_node
*node
;
881 if (bt_list_empty(head
)) {
886 bt_list_for_each_entry(node
, head
, siblings
) {
887 int uexpr_type
= node
->u
.unary_expression
.type
;
888 int uexpr_link
= node
->u
.unary_expression
.link
;
889 int cond
= node
->type
!= NODE_UNARY_EXPRESSION
||
890 uexpr_type
!= UNARY_UNSIGNED_CONSTANT
||
891 uexpr_link
!= UNARY_LINK_UNKNOWN
|| i
!= 0;
893 _BT_LOGE_NODE(node
, "Invalid constant unsigned integer.");
898 *value
= node
->u
.unary_expression
.u
.unsigned_constant
;
907 int is_unary_signed(struct bt_list_head
*head
)
910 struct ctf_node
*node
;
912 bt_list_for_each_entry(node
, head
, siblings
) {
913 if (node
->type
!= NODE_UNARY_EXPRESSION
) {
917 if (node
->u
.unary_expression
.type
!= UNARY_SIGNED_CONSTANT
) {
926 int get_unary_signed(struct bt_list_head
*head
, int64_t *value
)
930 struct ctf_node
*node
;
932 bt_list_for_each_entry(node
, head
, siblings
) {
933 int uexpr_type
= node
->u
.unary_expression
.type
;
934 int uexpr_link
= node
->u
.unary_expression
.link
;
935 int cond
= node
->type
!= NODE_UNARY_EXPRESSION
||
936 (uexpr_type
!= UNARY_UNSIGNED_CONSTANT
&&
937 uexpr_type
!= UNARY_SIGNED_CONSTANT
) ||
938 uexpr_link
!= UNARY_LINK_UNKNOWN
|| i
!= 0;
944 switch (uexpr_type
) {
945 case UNARY_UNSIGNED_CONSTANT
:
947 node
->u
.unary_expression
.u
.unsigned_constant
;
949 case UNARY_SIGNED_CONSTANT
:
950 *value
= node
->u
.unary_expression
.u
.signed_constant
;
965 int get_unary_uuid(struct bt_list_head
*head
, unsigned char *uuid
)
969 struct ctf_node
*node
;
971 bt_list_for_each_entry(node
, head
, siblings
) {
972 int uexpr_type
= node
->u
.unary_expression
.type
;
973 int uexpr_link
= node
->u
.unary_expression
.link
;
974 const char *src_string
;
976 if (node
->type
!= NODE_UNARY_EXPRESSION
||
977 uexpr_type
!= UNARY_STRING
||
978 uexpr_link
!= UNARY_LINK_UNKNOWN
||
984 src_string
= node
->u
.unary_expression
.u
.string
;
985 ret
= bt_uuid_parse(src_string
, uuid
);
988 "Cannot parse UUID: uuid=\"%s\"", src_string
);
998 int get_boolean(struct ctf_node
*unary_expr
)
1002 if (unary_expr
->type
!= NODE_UNARY_EXPRESSION
) {
1003 _BT_LOGE_NODE(unary_expr
,
1004 "Expecting unary expression: node-type=%d",
1010 switch (unary_expr
->u
.unary_expression
.type
) {
1011 case UNARY_UNSIGNED_CONSTANT
:
1012 ret
= (unary_expr
->u
.unary_expression
.u
.unsigned_constant
!= 0);
1014 case UNARY_SIGNED_CONSTANT
:
1015 ret
= (unary_expr
->u
.unary_expression
.u
.signed_constant
!= 0);
1019 const char *str
= unary_expr
->u
.unary_expression
.u
.string
;
1021 if (!strcmp(str
, "true") || !strcmp(str
, "TRUE")) {
1023 } else if (!strcmp(str
, "false") || !strcmp(str
, "FALSE")) {
1026 _BT_LOGE_NODE(unary_expr
,
1027 "Unexpected boolean value: value=\"%s\"", str
);
1034 _BT_LOGE_NODE(unary_expr
,
1035 "Unexpected unary expression type: node-type=%d",
1036 unary_expr
->u
.unary_expression
.type
);
1046 enum bt_byte_order
byte_order_from_unary_expr(struct ctf_node
*unary_expr
)
1049 enum bt_byte_order bo
= BT_BYTE_ORDER_UNKNOWN
;
1051 if (unary_expr
->u
.unary_expression
.type
!= UNARY_STRING
) {
1052 _BT_LOGE_NODE(unary_expr
,
1053 "\"byte_order\" attribute: expecting `be`, `le`, `network`, or `native`.");
1057 str
= unary_expr
->u
.unary_expression
.u
.string
;
1059 if (!strcmp(str
, "be") || !strcmp(str
, "network")) {
1060 bo
= BT_BYTE_ORDER_BIG_ENDIAN
;
1061 } else if (!strcmp(str
, "le")) {
1062 bo
= BT_BYTE_ORDER_LITTLE_ENDIAN
;
1063 } else if (!strcmp(str
, "native")) {
1064 bo
= BT_BYTE_ORDER_NATIVE
;
1066 _BT_LOGE_NODE(unary_expr
,
1067 "Unexpected \"byte_order\" attribute value: "
1068 "expecting `be`, `le`, `network`, or `native`: value=\"%s\"",
1078 enum bt_byte_order
get_real_byte_order(struct ctx
*ctx
,
1079 struct ctf_node
*uexpr
)
1081 enum bt_byte_order bo
= byte_order_from_unary_expr(uexpr
);
1083 if (bo
== BT_BYTE_ORDER_NATIVE
) {
1084 bo
= bt_trace_get_native_byte_order(ctx
->trace
);
1091 int is_align_valid(uint64_t align
)
1093 return (align
!= 0) && !(align
& (align
- 1ULL));
1097 int get_type_specifier_name(struct ctx
*ctx
, struct ctf_node
*type_specifier
,
1102 if (type_specifier
->type
!= NODE_TYPE_SPECIFIER
) {
1103 _BT_LOGE_NODE(type_specifier
,
1104 "Unexpected node type: node-type=%d",
1105 type_specifier
->type
);
1110 switch (type_specifier
->u
.type_specifier
.type
) {
1112 g_string_append(str
, "void");
1115 g_string_append(str
, "char");
1117 case TYPESPEC_SHORT
:
1118 g_string_append(str
, "short");
1121 g_string_append(str
, "int");
1124 g_string_append(str
, "long");
1126 case TYPESPEC_FLOAT
:
1127 g_string_append(str
, "float");
1129 case TYPESPEC_DOUBLE
:
1130 g_string_append(str
, "double");
1132 case TYPESPEC_SIGNED
:
1133 g_string_append(str
, "signed");
1135 case TYPESPEC_UNSIGNED
:
1136 g_string_append(str
, "unsigned");
1139 g_string_append(str
, "bool");
1141 case TYPESPEC_COMPLEX
:
1142 g_string_append(str
, "_Complex");
1144 case TYPESPEC_IMAGINARY
:
1145 g_string_append(str
, "_Imaginary");
1147 case TYPESPEC_CONST
:
1148 g_string_append(str
, "const");
1150 case TYPESPEC_ID_TYPE
:
1151 if (type_specifier
->u
.type_specifier
.id_type
) {
1152 g_string_append(str
,
1153 type_specifier
->u
.type_specifier
.id_type
);
1156 case TYPESPEC_STRUCT
:
1158 struct ctf_node
*node
= type_specifier
->u
.type_specifier
.node
;
1160 if (!node
->u
._struct
.name
) {
1161 _BT_LOGE_NODE(node
, "Unexpected empty structure field type name.");
1166 g_string_append(str
, "struct ");
1167 g_string_append(str
, node
->u
._struct
.name
);
1170 case TYPESPEC_VARIANT
:
1172 struct ctf_node
*node
= type_specifier
->u
.type_specifier
.node
;
1174 if (!node
->u
.variant
.name
) {
1175 _BT_LOGE_NODE(node
, "Unexpected empty variant field type name.");
1180 g_string_append(str
, "variant ");
1181 g_string_append(str
, node
->u
.variant
.name
);
1186 struct ctf_node
*node
= type_specifier
->u
.type_specifier
.node
;
1188 if (!node
->u
._enum
.enum_id
) {
1190 "Unexpected empty enumeration field type (`enum`) name.");
1195 g_string_append(str
, "enum ");
1196 g_string_append(str
, node
->u
._enum
.enum_id
);
1199 case TYPESPEC_FLOATING_POINT
:
1200 case TYPESPEC_INTEGER
:
1201 case TYPESPEC_STRING
:
1203 _BT_LOGE_NODE(type_specifier
->u
.type_specifier
.node
,
1204 "Unexpected type specifier type: %d",
1205 type_specifier
->u
.type_specifier
.type
);
1215 int get_type_specifier_list_name(struct ctx
*ctx
,
1216 struct ctf_node
*type_specifier_list
, GString
*str
)
1219 struct ctf_node
*iter
;
1220 int alias_item_nr
= 0;
1221 struct bt_list_head
*head
=
1222 &type_specifier_list
->u
.type_specifier_list
.head
;
1224 bt_list_for_each_entry(iter
, head
, siblings
) {
1225 if (alias_item_nr
!= 0) {
1226 g_string_append(str
, " ");
1230 ret
= get_type_specifier_name(ctx
, iter
, str
);
1241 GQuark
create_typealias_identifier(struct ctx
*ctx
,
1242 struct ctf_node
*type_specifier_list
,
1243 struct ctf_node
*node_type_declarator
)
1249 struct ctf_node
*iter
;
1250 struct bt_list_head
*pointers
=
1251 &node_type_declarator
->u
.type_declarator
.pointers
;
1253 str
= g_string_new("");
1254 ret
= get_type_specifier_list_name(ctx
, type_specifier_list
, str
);
1256 g_string_free(str
, TRUE
);
1260 bt_list_for_each_entry(iter
, pointers
, siblings
) {
1261 g_string_append(str
, " *");
1263 if (iter
->u
.pointer
.const_qualifier
) {
1264 g_string_append(str
, " const");
1268 str_c
= g_string_free(str
, FALSE
);
1269 qalias
= g_quark_from_string(str_c
);
1277 int visit_type_declarator(struct ctx
*ctx
, struct ctf_node
*type_specifier_list
,
1278 GQuark
*field_name
, struct ctf_node
*node_type_declarator
,
1279 struct bt_field_type
**field_decl
,
1280 struct bt_field_type
*nested_decl
)
1283 * During this whole function, nested_decl is always OURS,
1284 * whereas field_decl is an output which we create, but
1285 * belongs to the caller (it is moved).
1291 /* Validate type declarator node */
1292 if (node_type_declarator
) {
1293 if (node_type_declarator
->u
.type_declarator
.type
==
1295 _BT_LOGE_NODE(node_type_declarator
,
1296 "Unexpected type declarator type: type=%d",
1297 node_type_declarator
->u
.type_declarator
.type
);
1302 /* TODO: GCC bitfields not supported yet */
1303 if (node_type_declarator
->u
.type_declarator
.bitfield_len
!=
1305 _BT_LOGE_NODE(node_type_declarator
,
1306 "GCC bitfields are not supported as of this version.");
1312 /* Find the right nested declaration if not provided */
1314 struct bt_list_head
*pointers
=
1315 &node_type_declarator
->u
.type_declarator
.pointers
;
1317 if (node_type_declarator
&& !bt_list_empty(pointers
)) {
1319 _BT_FIELD_TYPE_INIT(nested_decl_copy
);
1322 * If we have a pointer declarator, it HAS to
1323 * be present in the typealiases (else fail).
1325 qalias
= create_typealias_identifier(ctx
,
1326 type_specifier_list
, node_type_declarator
);
1328 ctx_decl_scope_lookup_alias(ctx
->current_scope
,
1329 g_quark_to_string(qalias
), -1);
1331 _BT_LOGE_NODE(node_type_declarator
,
1332 "Cannot find type alias: name=\"%s\"",
1333 g_quark_to_string(qalias
));
1338 /* Make a copy of it */
1339 nested_decl_copy
= bt_field_type_copy(nested_decl
);
1340 BT_PUT(nested_decl
);
1341 if (!nested_decl_copy
) {
1342 _BT_LOGE_NODE(node_type_declarator
,
1343 "Cannot copy nested field type.");
1348 BT_MOVE(nested_decl
, nested_decl_copy
);
1350 /* Force integer's base to 16 since it's a pointer */
1351 if (bt_field_type_is_integer(nested_decl
)) {
1352 ret
= bt_field_type_integer_set_base(
1354 BT_INTEGER_BASE_HEXADECIMAL
);
1355 BT_ASSERT(ret
== 0);
1358 ret
= visit_type_specifier_list(ctx
,
1359 type_specifier_list
, &nested_decl
);
1361 BT_ASSERT(!nested_decl
);
1367 BT_ASSERT(nested_decl
);
1369 if (!node_type_declarator
) {
1370 BT_MOVE(*field_decl
, nested_decl
);
1374 if (node_type_declarator
->u
.type_declarator
.type
== TYPEDEC_ID
) {
1375 if (node_type_declarator
->u
.type_declarator
.u
.id
) {
1377 node_type_declarator
->u
.type_declarator
.u
.id
;
1383 *field_name
= g_quark_from_string(id
);
1388 BT_MOVE(*field_decl
, nested_decl
);
1391 struct ctf_node
*first
;
1392 _BT_FIELD_TYPE_INIT(decl
);
1393 _BT_FIELD_TYPE_INIT(outer_field_decl
);
1394 struct bt_list_head
*length
=
1395 &node_type_declarator
->
1396 u
.type_declarator
.u
.nested
.length
;
1398 /* Create array/sequence, pass nested_decl as child */
1399 if (bt_list_empty(length
)) {
1400 _BT_LOGE_NODE(node_type_declarator
,
1401 "Expecting length field reference or value.");
1406 first
= _BT_LIST_FIRST_ENTRY(length
, struct ctf_node
, siblings
);
1407 if (first
->type
!= NODE_UNARY_EXPRESSION
) {
1408 _BT_LOGE_NODE(first
,
1409 "Unexpected node type: node-type=%d",
1415 switch (first
->u
.unary_expression
.type
) {
1416 case UNARY_UNSIGNED_CONSTANT
:
1419 _BT_FIELD_TYPE_INIT(array_decl
);
1421 len
= first
->u
.unary_expression
.u
.unsigned_constant
;
1422 array_decl
= bt_field_type_array_create(nested_decl
,
1424 BT_PUT(nested_decl
);
1426 _BT_LOGE_NODE(first
,
1427 "Cannot create array field type.");
1432 BT_MOVE(decl
, array_decl
);
1437 /* Lookup unsigned integer definition, create seq. */
1438 _BT_FIELD_TYPE_INIT(seq_decl
);
1439 char *length_name
= concatenate_unary_strings(length
);
1440 char *length_name_no_underscore
;
1443 _BT_LOGE_NODE(node_type_declarator
,
1444 "Cannot concatenate unary strings.");
1449 length_name_no_underscore
=
1450 remove_underscores_from_field_ref(length_name
);
1451 if (!length_name_no_underscore
) {
1452 /* remove_underscores_from_field_ref() logs errors */
1456 seq_decl
= bt_field_type_sequence_create(
1457 nested_decl
, length_name_no_underscore
);
1458 free(length_name_no_underscore
);
1459 g_free(length_name
);
1460 BT_PUT(nested_decl
);
1462 _BT_LOGE_NODE(node_type_declarator
,
1463 "Cannot create sequence field type.");
1468 BT_MOVE(decl
, seq_decl
);
1476 BT_ASSERT(!nested_decl
);
1478 BT_ASSERT(!*field_decl
);
1481 * At this point, we found the next nested declaration.
1482 * We currently own this (and lost the ownership of
1483 * nested_decl in the meantime). Pass this next
1484 * nested declaration as the content of the outer
1485 * container, MOVING its ownership.
1487 ret
= visit_type_declarator(ctx
, type_specifier_list
,
1489 node_type_declarator
->
1490 u
.type_declarator
.u
.nested
.type_declarator
,
1491 &outer_field_decl
, decl
);
1494 BT_ASSERT(!outer_field_decl
);
1499 BT_ASSERT(outer_field_decl
);
1500 BT_MOVE(*field_decl
, outer_field_decl
);
1504 BT_PUT(nested_decl
);
1505 BT_ASSERT(*field_decl
);
1510 BT_PUT(nested_decl
);
1511 BT_PUT(*field_decl
);
1517 int visit_struct_decl_field(struct ctx
*ctx
,
1518 struct bt_field_type
*struct_decl
,
1519 struct ctf_node
*type_specifier_list
,
1520 struct bt_list_head
*type_declarators
)
1523 struct ctf_node
*iter
;
1524 _BT_FIELD_TYPE_INIT(field_decl
);
1526 bt_list_for_each_entry(iter
, type_declarators
, siblings
) {
1529 const char *field_name
;
1530 _BT_FIELD_TYPE_INIT(efield_decl
);
1532 ret
= visit_type_declarator(ctx
, type_specifier_list
,
1533 &qfield_name
, iter
, &field_decl
, NULL
);
1535 BT_ASSERT(!field_decl
);
1536 _BT_LOGE_NODE(type_specifier_list
,
1537 "Cannot visit type declarator: ret=%d", ret
);
1541 BT_ASSERT(field_decl
);
1542 field_name
= g_quark_to_string(qfield_name
);
1544 /* Check if field with same name already exists */
1546 bt_field_type_structure_get_field_type_by_name(
1547 struct_decl
, field_name
);
1549 BT_PUT(efield_decl
);
1550 _BT_LOGE_NODE(type_specifier_list
,
1551 "Duplicate field in structure field type: "
1552 "field-name=\"%s\"", field_name
);
1557 /* Add field to structure */
1558 ret
= bt_field_type_structure_add_field(struct_decl
,
1559 field_decl
, field_name
);
1562 _BT_LOGE_NODE(type_specifier_list
,
1563 "Cannot add field to structure field type: "
1564 "field-name=\"%s\", ret=%d",
1565 g_quark_to_string(qfield_name
), ret
);
1579 int visit_variant_decl_field(struct ctx
*ctx
,
1580 struct bt_field_type
*variant_decl
,
1581 struct ctf_node
*type_specifier_list
,
1582 struct bt_list_head
*type_declarators
)
1585 struct ctf_node
*iter
;
1586 _BT_FIELD_TYPE_INIT(field_decl
);
1588 bt_list_for_each_entry(iter
, type_declarators
, siblings
) {
1591 const char *field_name
;
1592 _BT_FIELD_TYPE_INIT(efield_decl
);
1594 ret
= visit_type_declarator(ctx
, type_specifier_list
,
1595 &qfield_name
, iter
, &field_decl
, NULL
);
1597 BT_ASSERT(!field_decl
);
1598 _BT_LOGE_NODE(type_specifier_list
,
1599 "Cannot visit type declarator: ret=%d", ret
);
1603 BT_ASSERT(field_decl
);
1604 field_name
= g_quark_to_string(qfield_name
);
1606 /* Check if field with same name already exists */
1608 bt_field_type_variant_get_field_type_by_name(
1609 variant_decl
, field_name
);
1611 BT_PUT(efield_decl
);
1612 _BT_LOGE_NODE(type_specifier_list
,
1613 "Duplicate field in variant field type: "
1614 "field-name=\"%s\"", field_name
);
1619 /* Add field to structure */
1620 ret
= bt_field_type_variant_add_field(variant_decl
,
1621 field_decl
, field_name
);
1624 _BT_LOGE_NODE(type_specifier_list
,
1625 "Cannot add field to variant field type: "
1626 "field-name=\"%s\", ret=%d",
1627 g_quark_to_string(qfield_name
), ret
);
1641 int visit_typedef(struct ctx
*ctx
, struct ctf_node
*type_specifier_list
,
1642 struct bt_list_head
*type_declarators
)
1646 struct ctf_node
*iter
;
1647 _BT_FIELD_TYPE_INIT(type_decl
);
1649 bt_list_for_each_entry(iter
, type_declarators
, siblings
) {
1650 ret
= visit_type_declarator(ctx
, type_specifier_list
,
1651 &qidentifier
, iter
, &type_decl
, NULL
);
1654 "Cannot visit type declarator: ret=%d", ret
);
1659 /* Do not allow typedef and typealias of untagged variants */
1660 if (bt_field_type_is_variant(type_decl
)) {
1661 if (bt_field_type_variant_get_tag_name(type_decl
)) {
1663 "Type definition of untagged variant field type is not allowed.");
1669 ret
= ctx_decl_scope_register_alias(ctx
->current_scope
,
1670 g_quark_to_string(qidentifier
), type_decl
);
1673 "Cannot register type definition: name=\"%s\"",
1674 g_quark_to_string(qidentifier
));
1686 int visit_typealias(struct ctx
*ctx
, struct ctf_node
*target
,
1687 struct ctf_node
*alias
)
1691 struct ctf_node
*node
;
1692 GQuark qdummy_field_name
;
1693 _BT_FIELD_TYPE_INIT(type_decl
);
1695 /* Create target type declaration */
1696 if (bt_list_empty(&target
->u
.typealias_target
.type_declarators
)) {
1699 node
= _BT_LIST_FIRST_ENTRY(
1700 &target
->u
.typealias_target
.type_declarators
,
1701 struct ctf_node
, siblings
);
1704 ret
= visit_type_declarator(ctx
,
1705 target
->u
.typealias_target
.type_specifier_list
,
1706 &qdummy_field_name
, node
, &type_decl
, NULL
);
1708 BT_ASSERT(!type_decl
);
1710 "Cannot visit type declarator: ret=%d", ret
);
1714 /* Do not allow typedef and typealias of untagged variants */
1715 if (bt_field_type_is_variant(type_decl
)) {
1716 if (bt_field_type_variant_get_tag_name(type_decl
)) {
1717 _BT_LOGE_NODE(target
,
1718 "Type definition of untagged variant field type is not allowed.");
1725 * The semantic validator does not check whether the target is
1726 * abstract or not (if it has an identifier). Check it here.
1728 if (qdummy_field_name
!= 0) {
1729 _BT_LOGE_NODE(target
,
1730 "Expecting empty identifier: id=\"%s\"",
1731 g_quark_to_string(qdummy_field_name
));
1736 /* Create alias identifier */
1737 node
= _BT_LIST_FIRST_ENTRY(&alias
->u
.typealias_alias
.type_declarators
,
1738 struct ctf_node
, siblings
);
1739 qalias
= create_typealias_identifier(ctx
,
1740 alias
->u
.typealias_alias
.type_specifier_list
, node
);
1741 ret
= ctx_decl_scope_register_alias(ctx
->current_scope
,
1742 g_quark_to_string(qalias
), type_decl
);
1745 "Cannot register type alias: name=\"%s\"",
1746 g_quark_to_string(qalias
));
1757 int visit_struct_decl_entry(struct ctx
*ctx
, struct ctf_node
*entry_node
,
1758 struct bt_field_type
*struct_decl
)
1762 switch (entry_node
->type
) {
1764 ret
= visit_typedef(ctx
,
1765 entry_node
->u
._typedef
.type_specifier_list
,
1766 &entry_node
->u
._typedef
.type_declarators
);
1768 _BT_LOGE_NODE(entry_node
,
1769 "Cannot add type definition found in structure field type: ret=%d",
1774 case NODE_TYPEALIAS
:
1775 ret
= visit_typealias(ctx
, entry_node
->u
.typealias
.target
,
1776 entry_node
->u
.typealias
.alias
);
1778 _BT_LOGE_NODE(entry_node
,
1779 "Cannot add type alias found in structure field type: ret=%d",
1784 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
1786 ret
= visit_struct_decl_field(ctx
, struct_decl
,
1787 entry_node
->u
.struct_or_variant_declaration
.
1788 type_specifier_list
,
1789 &entry_node
->u
.struct_or_variant_declaration
.
1796 _BT_LOGE_NODE(entry_node
,
1797 "Unexpected node type: node-type=%d", entry_node
->type
);
1807 int visit_variant_decl_entry(struct ctx
*ctx
, struct ctf_node
*entry_node
,
1808 struct bt_field_type
*variant_decl
)
1812 switch (entry_node
->type
) {
1814 ret
= visit_typedef(ctx
,
1815 entry_node
->u
._typedef
.type_specifier_list
,
1816 &entry_node
->u
._typedef
.type_declarators
);
1818 _BT_LOGE_NODE(entry_node
,
1819 "Cannot add type definition found in variant field type: ret=%d",
1824 case NODE_TYPEALIAS
:
1825 ret
= visit_typealias(ctx
, entry_node
->u
.typealias
.target
,
1826 entry_node
->u
.typealias
.alias
);
1828 _BT_LOGE_NODE(entry_node
,
1829 "Cannot add type alias found in variant field type: ret=%d",
1834 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
1836 ret
= visit_variant_decl_field(ctx
, variant_decl
,
1837 entry_node
->u
.struct_or_variant_declaration
.
1838 type_specifier_list
,
1839 &entry_node
->u
.struct_or_variant_declaration
.
1846 _BT_LOGE_NODE(entry_node
,
1847 "Unexpected node type: node-type=%d",
1858 int visit_struct_decl(struct ctx
*ctx
, const char *name
,
1859 struct bt_list_head
*decl_list
, int has_body
,
1860 struct bt_list_head
*min_align
,
1861 struct bt_field_type
**struct_decl
)
1865 *struct_decl
= NULL
;
1867 /* For named struct (without body), lookup in declaration scope */
1869 _BT_FIELD_TYPE_INIT(struct_decl_copy
);
1872 BT_LOGE_STR("Bodyless structure field type: missing name.");
1877 *struct_decl
= ctx_decl_scope_lookup_struct(ctx
->current_scope
,
1879 if (!*struct_decl
) {
1880 BT_LOGE("Cannot find structure field type: name=\"struct %s\"",
1886 /* Make a copy of it */
1887 struct_decl_copy
= bt_field_type_copy(*struct_decl
);
1888 if (!struct_decl_copy
) {
1889 BT_LOGE_STR("Cannot create copy of structure field type.");
1894 BT_MOVE(*struct_decl
, struct_decl_copy
);
1896 struct ctf_node
*entry_node
;
1897 uint64_t min_align_value
= 0;
1900 _BT_FIELD_TYPE_INIT(estruct_decl
);
1902 estruct_decl
= ctx_decl_scope_lookup_struct(
1903 ctx
->current_scope
, name
, 1);
1905 BT_PUT(estruct_decl
);
1906 BT_LOGE("Structure field type already declared in local scope: "
1907 "name=\"struct %s\"", name
);
1913 if (!bt_list_empty(min_align
)) {
1914 ret
= get_unary_unsigned(min_align
, &min_align_value
);
1916 BT_LOGE("Unexpected unary expression for structure field type's `align` attribute: "
1922 *struct_decl
= bt_field_type_structure_create();
1923 if (!*struct_decl
) {
1924 BT_LOGE_STR("Cannot create empty structure field type.");
1929 if (min_align_value
!= 0) {
1930 ret
= bt_field_type_set_alignment(*struct_decl
,
1933 BT_LOGE("Cannot set structure field type's alignment: "
1939 ret
= ctx_push_scope(ctx
);
1941 BT_LOGE_STR("Cannot push scope.");
1945 bt_list_for_each_entry(entry_node
, decl_list
, siblings
) {
1946 ret
= visit_struct_decl_entry(ctx
, entry_node
,
1949 _BT_LOGE_NODE(entry_node
,
1950 "Cannot visit structure field type entry: "
1960 ret
= ctx_decl_scope_register_struct(ctx
->current_scope
,
1961 name
, *struct_decl
);
1963 BT_LOGE("Cannot register structure field type in declaration scope: "
1964 "name=\"struct %s\", ret=%d", name
, ret
);
1973 BT_PUT(*struct_decl
);
1979 int visit_variant_decl(struct ctx
*ctx
, const char *name
,
1980 const char *tag
, struct bt_list_head
*decl_list
,
1981 int has_body
, struct bt_field_type
**variant_decl
)
1984 _BT_FIELD_TYPE_INIT(untagged_variant_decl
);
1986 *variant_decl
= NULL
;
1988 /* For named variant (without body), lookup in declaration scope */
1990 _BT_FIELD_TYPE_INIT(variant_decl_copy
);
1993 BT_LOGE_STR("Bodyless variant field type: missing name.");
1998 untagged_variant_decl
=
1999 ctx_decl_scope_lookup_variant(ctx
->current_scope
,
2001 if (!untagged_variant_decl
) {
2002 BT_LOGE("Cannot find variant field type: name=\"variant %s\"",
2008 /* Make a copy of it */
2009 variant_decl_copy
= bt_field_type_copy(
2010 untagged_variant_decl
);
2011 if (!variant_decl_copy
) {
2012 BT_LOGE_STR("Cannot create copy of variant field type.");
2017 BT_MOVE(untagged_variant_decl
, variant_decl_copy
);
2019 struct ctf_node
*entry_node
;
2022 struct bt_field_type
*evariant_decl
=
2023 ctx_decl_scope_lookup_struct(ctx
->current_scope
,
2026 if (evariant_decl
) {
2027 BT_PUT(evariant_decl
);
2028 BT_LOGE("Variant field type already declared in local scope: "
2029 "name=\"variant %s\"", name
);
2035 untagged_variant_decl
= bt_field_type_variant_create(NULL
,
2037 if (!untagged_variant_decl
) {
2038 BT_LOGE_STR("Cannot create empty variant field type.");
2043 ret
= ctx_push_scope(ctx
);
2045 BT_LOGE_STR("Cannot push scope.");
2049 bt_list_for_each_entry(entry_node
, decl_list
, siblings
) {
2050 ret
= visit_variant_decl_entry(ctx
, entry_node
,
2051 untagged_variant_decl
);
2053 _BT_LOGE_NODE(entry_node
,
2054 "Cannot visit variant field type entry: "
2064 ret
= ctx_decl_scope_register_variant(
2065 ctx
->current_scope
, name
,
2066 untagged_variant_decl
);
2068 BT_LOGE("Cannot register variant field type in declaration scope: "
2069 "name=\"variant %s\", ret=%d", name
, ret
);
2076 * If tagged, create tagged variant and return; otherwise
2077 * return untagged variant.
2080 BT_MOVE(*variant_decl
, untagged_variant_decl
);
2083 * At this point, we have a fresh untagged variant; nobody
2084 * else owns it. Set its tag now.
2086 char *tag_no_underscore
=
2087 remove_underscores_from_field_ref(tag
);
2089 if (!tag_no_underscore
) {
2090 /* remove_underscores_from_field_ref() logs errors */
2094 ret
= bt_field_type_variant_set_tag_name(
2095 untagged_variant_decl
, tag_no_underscore
);
2096 free(tag_no_underscore
);
2098 BT_LOGE("Cannot set variant field type's tag name: "
2099 "tag-name=\"%s\"", tag
);
2103 BT_MOVE(*variant_decl
, untagged_variant_decl
);
2106 BT_ASSERT(!untagged_variant_decl
);
2107 BT_ASSERT(*variant_decl
);
2112 BT_PUT(untagged_variant_decl
);
2113 BT_PUT(*variant_decl
);
2119 int visit_enum_decl_entry(struct ctx
*ctx
, struct ctf_node
*enumerator
,
2120 struct bt_field_type
*enum_decl
, int64_t *last
, bt_bool is_signed
)
2124 struct ctf_node
*iter
;
2125 int64_t start
= 0, end
= 0;
2126 const char *label
= enumerator
->u
.enumerator
.id
;
2127 const char *effective_label
= label
;
2128 struct bt_list_head
*values
= &enumerator
->u
.enumerator
.values
;
2130 bt_list_for_each_entry(iter
, values
, siblings
) {
2133 if (iter
->type
!= NODE_UNARY_EXPRESSION
) {
2135 "Wrong expression for enumeration field type label: "
2136 "node-type=%d, label=\"%s\"", iter
->type
,
2148 switch (iter
->u
.unary_expression
.type
) {
2149 case UNARY_SIGNED_CONSTANT
:
2150 *target
= iter
->u
.unary_expression
.u
.signed_constant
;
2152 case UNARY_UNSIGNED_CONSTANT
:
2154 iter
->u
.unary_expression
.u
.unsigned_constant
;
2158 "Invalid enumeration field type entry: "
2159 "expecting constant signed or unsigned integer: "
2160 "node-type=%d, label=\"%s\"",
2161 iter
->u
.unary_expression
.type
, label
);
2168 "Invalid enumeration field type entry: label=\"%s\"",
2187 if (label
[0] == '_') {
2189 * Strip the first underscore of any enumeration field
2190 * type's label in case this enumeration FT is used as
2191 * a variant FT tag later. The variant FT choice names
2192 * could also start with `_`, in which case the prefix
2193 * is removed, and it the resulting choice name needs to
2196 effective_label
= &label
[1];
2200 ret
= bt_field_type_enumeration_signed_add_mapping(enum_decl
,
2201 effective_label
, start
, end
);
2203 ret
= bt_field_type_enumeration_unsigned_add_mapping(enum_decl
,
2204 effective_label
, (uint64_t) start
, (uint64_t) end
);
2207 _BT_LOGE_NODE(enumerator
,
2208 "Cannot add mapping to enumeration field type: "
2209 "label=\"%s\", ret=%d, "
2210 "start-value-unsigned=%" PRIu64
", "
2211 "end-value-unsigned=%" PRIu64
, label
, ret
,
2212 (uint64_t) start
, (uint64_t) end
);
2223 int visit_enum_decl(struct ctx
*ctx
, const char *name
,
2224 struct ctf_node
*container_type
,
2225 struct bt_list_head
*enumerator_list
,
2227 struct bt_field_type
**enum_decl
)
2231 _BT_FIELD_TYPE_INIT(integer_decl
);
2235 /* For named enum (without body), lookup in declaration scope */
2237 _BT_FIELD_TYPE_INIT(enum_decl_copy
);
2240 BT_LOGE_STR("Bodyless enumeration field type: missing name.");
2245 *enum_decl
= ctx_decl_scope_lookup_enum(ctx
->current_scope
,
2248 BT_LOGE("Cannot find enumeration field type: "
2249 "name=\"enum %s\"", name
);
2254 /* Make a copy of it */
2255 enum_decl_copy
= bt_field_type_copy(*enum_decl
);
2256 if (!enum_decl_copy
) {
2257 BT_LOGE_STR("Cannot create copy of enumeration field type.");
2263 BT_MOVE(*enum_decl
, enum_decl_copy
);
2265 struct ctf_node
*iter
;
2266 int64_t last_value
= 0;
2269 _BT_FIELD_TYPE_INIT(eenum_decl
);
2271 eenum_decl
= ctx_decl_scope_lookup_enum(
2272 ctx
->current_scope
, name
, 1);
2275 BT_LOGE("Enumeration field type already declared in local scope: "
2276 "name=\"enum %s\"", name
);
2282 if (!container_type
) {
2283 integer_decl
= ctx_decl_scope_lookup_alias(
2284 ctx
->current_scope
, "int", -1);
2285 if (!integer_decl
) {
2286 BT_LOGE_STR("Cannot find implicit `int` field type alias for enumeration field type.");
2291 ret
= visit_type_declarator(ctx
, container_type
,
2292 &qdummy_id
, NULL
, &integer_decl
, NULL
);
2294 BT_ASSERT(!integer_decl
);
2300 BT_ASSERT(integer_decl
);
2302 if (!bt_field_type_is_integer(integer_decl
)) {
2303 BT_LOGE("Container field type for enumeration field type is not an integer field type: "
2305 bt_common_field_type_id_string(
2306 bt_field_type_get_type_id(integer_decl
)));
2311 *enum_decl
= bt_field_type_enumeration_create(integer_decl
);
2313 BT_LOGE_STR("Cannot create enumeration field type.");
2318 bt_list_for_each_entry(iter
, enumerator_list
, siblings
) {
2319 ret
= visit_enum_decl_entry(ctx
, iter
, *enum_decl
,
2321 bt_field_type_integer_is_signed(integer_decl
));
2324 "Cannot visit enumeration field type entry: "
2331 ret
= ctx_decl_scope_register_enum(ctx
->current_scope
,
2334 BT_LOGE("Cannot register enumeration field type in declaration scope: "
2341 BT_PUT(integer_decl
);
2346 BT_PUT(integer_decl
);
2353 int visit_type_specifier(struct ctx
*ctx
,
2354 struct ctf_node
*type_specifier_list
,
2355 struct bt_field_type
**decl
)
2358 GString
*str
= NULL
;
2359 _BT_FIELD_TYPE_INIT(decl_copy
);
2362 str
= g_string_new("");
2363 ret
= get_type_specifier_list_name(ctx
, type_specifier_list
, str
);
2365 _BT_LOGE_NODE(type_specifier_list
,
2366 "Cannot get type specifier list's name: ret=%d", ret
);
2370 *decl
= ctx_decl_scope_lookup_alias(ctx
->current_scope
, str
->str
, -1);
2372 _BT_LOGE_NODE(type_specifier_list
,
2373 "Cannot find type alias: name=\"%s\"", str
->str
);
2378 /* Make a copy of the type declaration */
2379 decl_copy
= bt_field_type_copy(*decl
);
2381 _BT_LOGE_NODE(type_specifier_list
,
2382 "Cannot create field type copy.");
2387 BT_MOVE(*decl
, decl_copy
);
2388 (void) g_string_free(str
, TRUE
);
2395 (void) g_string_free(str
, TRUE
);
2404 int visit_integer_decl(struct ctx
*ctx
,
2405 struct bt_list_head
*expressions
,
2406 struct bt_field_type
**integer_decl
)
2410 bt_bool signedness
= 0;
2411 struct ctf_node
*expression
;
2412 uint64_t alignment
= 0, size
= 0;
2413 struct bt_clock_class
*mapped_clock
= NULL
;
2414 enum bt_string_encoding encoding
= BT_STRING_ENCODING_NONE
;
2415 enum bt_integer_base base
= BT_INTEGER_BASE_DECIMAL
;
2416 enum bt_byte_order byte_order
=
2417 bt_trace_get_native_byte_order(ctx
->trace
);
2419 *integer_decl
= NULL
;
2421 bt_list_for_each_entry(expression
, expressions
, siblings
) {
2422 struct ctf_node
*left
, *right
;
2424 left
= _BT_LIST_FIRST_ENTRY(&expression
->u
.ctf_expression
.left
,
2425 struct ctf_node
, siblings
);
2426 right
= _BT_LIST_FIRST_ENTRY(
2427 &expression
->u
.ctf_expression
.right
, struct ctf_node
,
2430 if (left
->u
.unary_expression
.type
!= UNARY_STRING
) {
2432 "Unexpected unary expression type: type=%d",
2433 left
->u
.unary_expression
.type
);
2438 if (!strcmp(left
->u
.unary_expression
.u
.string
, "signed")) {
2439 if (_IS_SET(&set
, _INTEGER_SIGNED_SET
)) {
2440 _BT_LOGE_DUP_ATTR(left
, "signed",
2441 "integer field type");
2446 signedness
= get_boolean(right
);
2447 if (signedness
< 0) {
2448 _BT_LOGE_NODE(right
,
2449 "Invalid boolean value for integer field type's `signed` attribute: "
2455 _SET(&set
, _INTEGER_SIGNED_SET
);
2456 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2458 if (_IS_SET(&set
, _INTEGER_BYTE_ORDER_SET
)) {
2459 _BT_LOGE_DUP_ATTR(left
, "byte_order",
2460 "integer field type");
2465 byte_order
= get_real_byte_order(ctx
, right
);
2466 if (byte_order
== BT_BYTE_ORDER_UNKNOWN
) {
2467 _BT_LOGE_NODE(right
,
2468 "Invalid `byte_order` attribute in integer field type: "
2474 _SET(&set
, _INTEGER_BYTE_ORDER_SET
);
2475 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "size")) {
2476 if (_IS_SET(&set
, _INTEGER_SIZE_SET
)) {
2477 _BT_LOGE_DUP_ATTR(left
, "size",
2478 "integer field type");
2483 if (right
->u
.unary_expression
.type
!=
2484 UNARY_UNSIGNED_CONSTANT
) {
2485 _BT_LOGE_NODE(right
,
2486 "Invalid `size` attribute in integer field type: "
2487 "expecting unsigned constant integer: "
2489 right
->u
.unary_expression
.type
);
2494 size
= right
->u
.unary_expression
.u
.unsigned_constant
;
2496 _BT_LOGE_NODE(right
,
2497 "Invalid `size` attribute in integer field type: "
2498 "expecting positive constant integer: "
2499 "size=%" PRIu64
, size
);
2502 } else if (size
> 64) {
2503 _BT_LOGE_NODE(right
,
2504 "Invalid `size` attribute in integer field type: "
2505 "integer fields over 64 bits are not supported as of this version: "
2506 "size=%" PRIu64
, size
);
2511 _SET(&set
, _INTEGER_SIZE_SET
);
2512 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2514 if (_IS_SET(&set
, _INTEGER_ALIGN_SET
)) {
2515 _BT_LOGE_DUP_ATTR(left
, "align",
2516 "integer field type");
2521 if (right
->u
.unary_expression
.type
!=
2522 UNARY_UNSIGNED_CONSTANT
) {
2523 _BT_LOGE_NODE(right
,
2524 "Invalid `align` attribute in integer field type: "
2525 "expecting unsigned constant integer: "
2527 right
->u
.unary_expression
.type
);
2533 right
->u
.unary_expression
.u
.unsigned_constant
;
2534 if (!is_align_valid(alignment
)) {
2535 _BT_LOGE_NODE(right
,
2536 "Invalid `align` attribute in integer field type: "
2537 "expecting power of two: "
2538 "align=%" PRIu64
, alignment
);
2543 _SET(&set
, _INTEGER_ALIGN_SET
);
2544 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "base")) {
2545 if (_IS_SET(&set
, _INTEGER_BASE_SET
)) {
2546 _BT_LOGE_DUP_ATTR(left
, "base",
2547 "integer field type");
2552 switch (right
->u
.unary_expression
.type
) {
2553 case UNARY_UNSIGNED_CONSTANT
:
2555 uint64_t constant
= right
->u
.unary_expression
.
2556 u
.unsigned_constant
;
2560 base
= BT_INTEGER_BASE_BINARY
;
2563 base
= BT_INTEGER_BASE_OCTAL
;
2566 base
= BT_INTEGER_BASE_DECIMAL
;
2569 base
= BT_INTEGER_BASE_HEXADECIMAL
;
2572 _BT_LOGE_NODE(right
,
2573 "Invalid `base` attribute in integer field type: "
2575 right
->u
.unary_expression
.u
.unsigned_constant
);
2583 char *s_right
= concatenate_unary_strings(
2584 &expression
->u
.ctf_expression
.right
);
2586 _BT_LOGE_NODE(right
,
2587 "Unexpected unary expression for integer field type's `base` attribute.");
2592 if (!strcmp(s_right
, "decimal") ||
2593 !strcmp(s_right
, "dec") ||
2594 !strcmp(s_right
, "d") ||
2595 !strcmp(s_right
, "i") ||
2596 !strcmp(s_right
, "u")) {
2597 base
= BT_INTEGER_BASE_DECIMAL
;
2598 } else if (!strcmp(s_right
, "hexadecimal") ||
2599 !strcmp(s_right
, "hex") ||
2600 !strcmp(s_right
, "x") ||
2601 !strcmp(s_right
, "X") ||
2602 !strcmp(s_right
, "p")) {
2603 base
= BT_INTEGER_BASE_HEXADECIMAL
;
2604 } else if (!strcmp(s_right
, "octal") ||
2605 !strcmp(s_right
, "oct") ||
2606 !strcmp(s_right
, "o")) {
2607 base
= BT_INTEGER_BASE_OCTAL
;
2608 } else if (!strcmp(s_right
, "binary") ||
2609 !strcmp(s_right
, "b")) {
2610 base
= BT_INTEGER_BASE_BINARY
;
2612 _BT_LOGE_NODE(right
,
2613 "Unexpected unary expression for integer field type's `base` attribute: "
2614 "base=\"%s\"", s_right
);
2624 _BT_LOGE_NODE(right
,
2625 "Invalid `base` attribute in integer field type: "
2626 "expecting unsigned constant integer or unary string.");
2631 _SET(&set
, _INTEGER_BASE_SET
);
2632 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2636 if (_IS_SET(&set
, _INTEGER_ENCODING_SET
)) {
2637 _BT_LOGE_DUP_ATTR(left
, "encoding",
2638 "integer field type");
2643 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
2644 _BT_LOGE_NODE(right
,
2645 "Invalid `encoding` attribute in integer field type: "
2646 "expecting unary string.");
2651 s_right
= concatenate_unary_strings(
2652 &expression
->u
.ctf_expression
.right
);
2654 _BT_LOGE_NODE(right
,
2655 "Unexpected unary expression for integer field type's `encoding` attribute.");
2660 if (!strcmp(s_right
, "UTF8") ||
2661 !strcmp(s_right
, "utf8") ||
2662 !strcmp(s_right
, "utf-8") ||
2663 !strcmp(s_right
, "UTF-8")) {
2664 encoding
= BT_STRING_ENCODING_UTF8
;
2665 } else if (!strcmp(s_right
, "ASCII") ||
2666 !strcmp(s_right
, "ascii")) {
2667 encoding
= BT_STRING_ENCODING_ASCII
;
2668 } else if (!strcmp(s_right
, "none")) {
2669 encoding
= BT_STRING_ENCODING_NONE
;
2671 _BT_LOGE_NODE(right
,
2672 "Invalid `encoding` attribute in integer field type: "
2673 "unknown encoding: encoding=\"%s\"",
2681 _SET(&set
, _INTEGER_ENCODING_SET
);
2682 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "map")) {
2683 const char *clock_name
;
2685 if (_IS_SET(&set
, _INTEGER_MAP_SET
)) {
2686 _BT_LOGE_DUP_ATTR(left
, "map",
2687 "integer field type");
2692 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
2693 _BT_LOGE_NODE(right
,
2694 "Invalid `map` attribute in integer field type: "
2695 "expecting unary string.");
2701 get_map_clock_name_value(
2702 &expression
->u
.ctf_expression
.right
);
2704 char *s_right
= concatenate_unary_strings(
2705 &expression
->u
.ctf_expression
.right
);
2708 _BT_LOGE_NODE(right
,
2709 "Unexpected unary expression for integer field type's `map` attribute.");
2714 _BT_LOGE_NODE(right
,
2715 "Invalid `map` attribute in integer field type: "
2716 "cannot find clock class at this point: name=\"%s\"",
2718 _SET(&set
, _INTEGER_MAP_SET
);
2723 mapped_clock
= bt_trace_get_clock_class_by_name(
2724 ctx
->trace
, clock_name
);
2725 if (!mapped_clock
) {
2726 _BT_LOGE_NODE(right
,
2727 "Invalid `map` attribute in integer field type: "
2728 "cannot find clock class at this point: name=\"%s\"",
2734 _SET(&set
, _INTEGER_MAP_SET
);
2737 "Unknown attribute in integer field type: "
2739 left
->u
.unary_expression
.u
.string
);
2743 if (!_IS_SET(&set
, _INTEGER_SIZE_SET
)) {
2744 BT_LOGE_STR("Missing `size` attribute in integer field type.");
2749 if (!_IS_SET(&set
, _INTEGER_ALIGN_SET
)) {
2750 if (size
% CHAR_BIT
) {
2751 /* Bit-packed alignment */
2754 /* Byte-packed alignment */
2755 alignment
= CHAR_BIT
;
2759 *integer_decl
= bt_field_type_integer_create((unsigned int) size
);
2760 if (!*integer_decl
) {
2761 BT_LOGE_STR("Cannot create integer field type.");
2766 ret
= bt_field_type_integer_set_is_signed(*integer_decl
, signedness
);
2767 ret
|= bt_field_type_integer_set_base(*integer_decl
, base
);
2768 ret
|= bt_field_type_integer_set_encoding(*integer_decl
, encoding
);
2769 ret
|= bt_field_type_set_alignment(*integer_decl
,
2770 (unsigned int) alignment
);
2771 ret
|= bt_field_type_set_byte_order(*integer_decl
, byte_order
);
2775 ret
|= bt_field_type_integer_set_mapped_clock_class(
2776 *integer_decl
, mapped_clock
);
2777 bt_put(mapped_clock
);
2778 mapped_clock
= NULL
;
2782 BT_LOGE_STR("Cannot configure integer field type.");
2791 bt_put(mapped_clock
);
2794 BT_PUT(*integer_decl
);
2800 int visit_floating_point_number_decl(struct ctx
*ctx
,
2801 struct bt_list_head
*expressions
,
2802 struct bt_field_type
**float_decl
)
2806 struct ctf_node
*expression
;
2807 uint64_t alignment
= 1, exp_dig
= 0, mant_dig
= 0;
2808 enum bt_byte_order byte_order
=
2809 bt_trace_get_native_byte_order(ctx
->trace
);
2813 bt_list_for_each_entry(expression
, expressions
, siblings
) {
2814 struct ctf_node
*left
, *right
;
2816 left
= _BT_LIST_FIRST_ENTRY(&expression
->u
.ctf_expression
.left
,
2817 struct ctf_node
, siblings
);
2818 right
= _BT_LIST_FIRST_ENTRY(
2819 &expression
->u
.ctf_expression
.right
, struct ctf_node
,
2822 if (left
->u
.unary_expression
.type
!= UNARY_STRING
) {
2824 "Unexpected unary expression type: type=%d",
2825 left
->u
.unary_expression
.type
);
2830 if (!strcmp(left
->u
.unary_expression
.u
.string
, "byte_order")) {
2831 if (_IS_SET(&set
, _FLOAT_BYTE_ORDER_SET
)) {
2832 _BT_LOGE_DUP_ATTR(left
, "byte_order",
2833 "floating point number field type");
2838 byte_order
= get_real_byte_order(ctx
, right
);
2839 if (byte_order
== BT_BYTE_ORDER_UNKNOWN
) {
2840 _BT_LOGE_NODE(right
,
2841 "Invalid `byte_order` attribute in floating point number field type: "
2847 _SET(&set
, _FLOAT_BYTE_ORDER_SET
);
2848 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2850 if (_IS_SET(&set
, _FLOAT_EXP_DIG_SET
)) {
2851 _BT_LOGE_DUP_ATTR(left
, "exp_dig",
2852 "floating point number field type");
2857 if (right
->u
.unary_expression
.type
!=
2858 UNARY_UNSIGNED_CONSTANT
) {
2859 _BT_LOGE_NODE(right
,
2860 "Invalid `exp_dig` attribute in floating point number field type: "
2861 "expecting unsigned constant integer: "
2863 right
->u
.unary_expression
.type
);
2868 exp_dig
= right
->u
.unary_expression
.u
.unsigned_constant
;
2869 _SET(&set
, _FLOAT_EXP_DIG_SET
);
2870 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2872 if (_IS_SET(&set
, _FLOAT_MANT_DIG_SET
)) {
2873 _BT_LOGE_DUP_ATTR(left
, "mant_dig",
2874 "floating point number field type");
2879 if (right
->u
.unary_expression
.type
!=
2880 UNARY_UNSIGNED_CONSTANT
) {
2881 _BT_LOGE_NODE(right
,
2882 "Invalid `mant_dig` attribute in floating point number field type: "
2883 "expecting unsigned constant integer: "
2885 right
->u
.unary_expression
.type
);
2890 mant_dig
= right
->u
.unary_expression
.u
.
2892 _SET(&set
, _FLOAT_MANT_DIG_SET
);
2893 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2895 if (_IS_SET(&set
, _FLOAT_ALIGN_SET
)) {
2896 _BT_LOGE_DUP_ATTR(left
, "align",
2897 "floating point number field type");
2902 if (right
->u
.unary_expression
.type
!=
2903 UNARY_UNSIGNED_CONSTANT
) {
2904 _BT_LOGE_NODE(right
,
2905 "Invalid `align` attribute in floating point number field type: "
2906 "expecting unsigned constant integer: "
2908 right
->u
.unary_expression
.type
);
2913 alignment
= right
->u
.unary_expression
.u
.
2916 if (!is_align_valid(alignment
)) {
2917 _BT_LOGE_NODE(right
,
2918 "Invalid `align` attribute in floating point number field type: "
2919 "expecting power of two: "
2920 "align=%" PRIu64
, alignment
);
2925 _SET(&set
, _FLOAT_ALIGN_SET
);
2928 "Unknown attribute in floating point number field type: "
2930 left
->u
.unary_expression
.u
.string
);
2934 if (!_IS_SET(&set
, _FLOAT_MANT_DIG_SET
)) {
2935 BT_LOGE_STR("Missing `mant_dig` attribute in floating point number field type.");
2940 if (!_IS_SET(&set
, _FLOAT_EXP_DIG_SET
)) {
2941 BT_LOGE_STR("Missing `exp_dig` attribute in floating point number field type.");
2946 if (!_IS_SET(&set
, _INTEGER_ALIGN_SET
)) {
2947 if ((mant_dig
+ exp_dig
) % CHAR_BIT
) {
2948 /* Bit-packed alignment */
2951 /* Byte-packed alignment */
2952 alignment
= CHAR_BIT
;
2956 *float_decl
= bt_field_type_floating_point_create();
2958 BT_LOGE_STR("Cannot create floating point number field type.");
2963 ret
= bt_field_type_floating_point_set_exponent_digits(
2964 *float_decl
, exp_dig
);
2965 ret
|= bt_field_type_floating_point_set_mantissa_digits(
2966 *float_decl
, mant_dig
);
2967 ret
|= bt_field_type_set_byte_order(*float_decl
, byte_order
);
2968 ret
|= bt_field_type_set_alignment(*float_decl
, alignment
);
2970 BT_LOGE_STR("Cannot configure floating point number field type.");
2978 BT_PUT(*float_decl
);
2984 int visit_string_decl(struct ctx
*ctx
,
2985 struct bt_list_head
*expressions
,
2986 struct bt_field_type
**string_decl
)
2990 struct ctf_node
*expression
;
2991 enum bt_string_encoding encoding
= BT_STRING_ENCODING_UTF8
;
2993 *string_decl
= NULL
;
2995 bt_list_for_each_entry(expression
, expressions
, siblings
) {
2996 struct ctf_node
*left
, *right
;
2998 left
= _BT_LIST_FIRST_ENTRY(&expression
->u
.ctf_expression
.left
,
2999 struct ctf_node
, siblings
);
3000 right
= _BT_LIST_FIRST_ENTRY(
3001 &expression
->u
.ctf_expression
.right
, struct ctf_node
,
3004 if (left
->u
.unary_expression
.type
!= UNARY_STRING
) {
3006 "Unexpected unary expression type: type=%d",
3007 left
->u
.unary_expression
.type
);
3012 if (!strcmp(left
->u
.unary_expression
.u
.string
, "encoding")) {
3015 if (_IS_SET(&set
, _STRING_ENCODING_SET
)) {
3016 _BT_LOGE_DUP_ATTR(left
, "encoding",
3017 "string field type");
3022 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
3023 _BT_LOGE_NODE(right
,
3024 "Invalid `encoding` attribute in string field type: "
3025 "expecting unary string.");
3030 s_right
= concatenate_unary_strings(
3031 &expression
->u
.ctf_expression
.right
);
3033 _BT_LOGE_NODE(right
,
3034 "Unexpected unary expression for string field type's `encoding` attribute.");
3039 if (!strcmp(s_right
, "UTF8") ||
3040 !strcmp(s_right
, "utf8") ||
3041 !strcmp(s_right
, "utf-8") ||
3042 !strcmp(s_right
, "UTF-8")) {
3043 encoding
= BT_STRING_ENCODING_UTF8
;
3044 } else if (!strcmp(s_right
, "ASCII") ||
3045 !strcmp(s_right
, "ascii")) {
3046 encoding
= BT_STRING_ENCODING_ASCII
;
3047 } else if (!strcmp(s_right
, "none")) {
3048 encoding
= BT_STRING_ENCODING_NONE
;
3050 _BT_LOGE_NODE(right
,
3051 "Invalid `encoding` attribute in string field type: "
3052 "unknown encoding: encoding=\"%s\"",
3060 _SET(&set
, _STRING_ENCODING_SET
);
3063 "Unknown attribute in string field type: "
3065 left
->u
.unary_expression
.u
.string
);
3069 *string_decl
= bt_field_type_string_create();
3070 if (!*string_decl
) {
3071 BT_LOGE_STR("Cannot create string field type.");
3076 ret
= bt_field_type_string_set_encoding(*string_decl
, encoding
);
3078 BT_LOGE_STR("Cannot configure string field type.");
3086 BT_PUT(*string_decl
);
3092 int visit_type_specifier_list(struct ctx
*ctx
,
3093 struct ctf_node
*ts_list
,
3094 struct bt_field_type
**decl
)
3097 struct ctf_node
*first
, *node
;
3101 if (ts_list
->type
!= NODE_TYPE_SPECIFIER_LIST
) {
3102 _BT_LOGE_NODE(ts_list
,
3103 "Unexpected node type: node-type=%d", ts_list
->type
);
3108 first
= _BT_LIST_FIRST_ENTRY(&ts_list
->u
.type_specifier_list
.head
,
3109 struct ctf_node
, siblings
);
3110 if (first
->type
!= NODE_TYPE_SPECIFIER
) {
3111 _BT_LOGE_NODE(first
,
3112 "Unexpected node type: node-type=%d", first
->type
);
3117 node
= first
->u
.type_specifier
.node
;
3119 switch (first
->u
.type_specifier
.type
) {
3120 case TYPESPEC_INTEGER
:
3121 ret
= visit_integer_decl(ctx
, &node
->u
.integer
.expressions
,
3128 case TYPESPEC_FLOATING_POINT
:
3129 ret
= visit_floating_point_number_decl(ctx
,
3130 &node
->u
.floating_point
.expressions
, decl
);
3136 case TYPESPEC_STRING
:
3137 ret
= visit_string_decl(ctx
,
3138 &node
->u
.string
.expressions
, decl
);
3144 case TYPESPEC_STRUCT
:
3145 ret
= visit_struct_decl(ctx
, node
->u
._struct
.name
,
3146 &node
->u
._struct
.declaration_list
,
3147 node
->u
._struct
.has_body
,
3148 &node
->u
._struct
.min_align
, decl
);
3154 case TYPESPEC_VARIANT
:
3155 ret
= visit_variant_decl(ctx
, node
->u
.variant
.name
,
3156 node
->u
.variant
.choice
,
3157 &node
->u
.variant
.declaration_list
,
3158 node
->u
.variant
.has_body
, decl
);
3165 ret
= visit_enum_decl(ctx
, node
->u
._enum
.enum_id
,
3166 node
->u
._enum
.container_type
,
3167 &node
->u
._enum
.enumerator_list
,
3168 node
->u
._enum
.has_body
, decl
);
3176 case TYPESPEC_SHORT
:
3179 case TYPESPEC_FLOAT
:
3180 case TYPESPEC_DOUBLE
:
3181 case TYPESPEC_SIGNED
:
3182 case TYPESPEC_UNSIGNED
:
3184 case TYPESPEC_COMPLEX
:
3185 case TYPESPEC_IMAGINARY
:
3186 case TYPESPEC_CONST
:
3187 case TYPESPEC_ID_TYPE
:
3188 ret
= visit_type_specifier(ctx
, ts_list
, decl
);
3190 _BT_LOGE_NODE(first
,
3191 "Cannot visit type specifier: ret=%d",
3198 _BT_LOGE_NODE(first
,
3199 "Unexpected type specifier type: node-type=%d",
3200 first
->u
.type_specifier
.type
);
3216 int visit_event_decl_entry(struct ctx
*ctx
, struct ctf_node
*node
,
3217 struct bt_event_class
*event_class
, int64_t *stream_id
,
3222 _BT_FIELD_TYPE_INIT(decl
);
3224 switch (node
->type
) {
3226 ret
= visit_typedef(ctx
, node
->u
._typedef
.type_specifier_list
,
3227 &node
->u
._typedef
.type_declarators
);
3230 "Cannot add type definition found in event class.");
3234 case NODE_TYPEALIAS
:
3235 ret
= visit_typealias(ctx
, node
->u
.typealias
.target
,
3236 node
->u
.typealias
.alias
);
3239 "Cannot add type alias found in event class.");
3243 case NODE_CTF_EXPRESSION
:
3245 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
3247 _BT_LOGE_NODE(node
, "Cannot concatenate unary strings.");
3252 if (!strcmp(left
, "name")) {
3253 /* This is already known at this stage */
3254 if (_IS_SET(set
, _EVENT_NAME_SET
)) {
3255 _BT_LOGE_DUP_ATTR(node
, "name",
3261 _SET(set
, _EVENT_NAME_SET
);
3262 } else if (!strcmp(left
, "id")) {
3265 if (_IS_SET(set
, _EVENT_ID_SET
)) {
3266 _BT_LOGE_DUP_ATTR(node
, "id",
3272 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
3274 /* Only read "id" if get_unary_unsigned() succeeded. */
3275 if (ret
|| (!ret
&& id
< 0)) {
3277 "Unexpected unary expression for event class's `id` attribute.");
3282 ret
= bt_event_class_set_id(event_class
, id
);
3285 "Cannot set event class's ID: "
3290 _SET(set
, _EVENT_ID_SET
);
3291 } else if (!strcmp(left
, "stream_id")) {
3292 if (_IS_SET(set
, _EVENT_STREAM_ID_SET
)) {
3293 _BT_LOGE_DUP_ATTR(node
, "stream_id",
3299 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
3300 (uint64_t *) stream_id
);
3302 * Only read "stream_id" if get_unary_unsigned()
3305 if (ret
|| (!ret
&& *stream_id
< 0)) {
3307 "Unexpected unary expression for event class's `stream_id` attribute.");
3312 _SET(set
, _EVENT_STREAM_ID_SET
);
3313 } else if (!strcmp(left
, "context")) {
3314 if (_IS_SET(set
, _EVENT_CONTEXT_SET
)) {
3316 "Duplicate `context` entry in event class.");
3321 ret
= visit_type_specifier_list(ctx
,
3322 _BT_LIST_FIRST_ENTRY(
3323 &node
->u
.ctf_expression
.right
,
3324 struct ctf_node
, siblings
),
3328 "Cannot create event class's context field type.");
3333 ret
= bt_event_class_set_context_field_type(
3338 "Cannot set event class's context field type.");
3342 _SET(set
, _EVENT_CONTEXT_SET
);
3343 } else if (!strcmp(left
, "fields")) {
3344 if (_IS_SET(set
, _EVENT_FIELDS_SET
)) {
3346 "Duplicate `fields` entry in event class.");
3351 ret
= visit_type_specifier_list(ctx
,
3352 _BT_LIST_FIRST_ENTRY(
3353 &node
->u
.ctf_expression
.right
,
3354 struct ctf_node
, siblings
),
3358 "Cannot create event class's payload field type.");
3363 ret
= bt_event_class_set_payload_field_type(
3368 "Cannot set event class's payload field type.");
3372 _SET(set
, _EVENT_FIELDS_SET
);
3373 } else if (!strcmp(left
, "loglevel")) {
3374 uint64_t loglevel_value
;
3375 enum bt_event_class_log_level log_level
=
3376 BT_EVENT_CLASS_LOG_LEVEL_UNSPECIFIED
;
3378 if (_IS_SET(set
, _EVENT_LOGLEVEL_SET
)) {
3379 _BT_LOGE_DUP_ATTR(node
, "loglevel",
3385 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
3389 "Unexpected unary expression for event class's `loglevel` attribute.");
3394 switch (loglevel_value
) {
3396 log_level
= BT_EVENT_CLASS_LOG_LEVEL_EMERGENCY
;
3399 log_level
= BT_EVENT_CLASS_LOG_LEVEL_ALERT
;
3402 log_level
= BT_EVENT_CLASS_LOG_LEVEL_CRITICAL
;
3405 log_level
= BT_EVENT_CLASS_LOG_LEVEL_ERROR
;
3408 log_level
= BT_EVENT_CLASS_LOG_LEVEL_WARNING
;
3411 log_level
= BT_EVENT_CLASS_LOG_LEVEL_NOTICE
;
3414 log_level
= BT_EVENT_CLASS_LOG_LEVEL_INFO
;
3417 log_level
= BT_EVENT_CLASS_LOG_LEVEL_DEBUG_SYSTEM
;
3420 log_level
= BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROGRAM
;
3423 log_level
= BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROCESS
;
3426 log_level
= BT_EVENT_CLASS_LOG_LEVEL_DEBUG_MODULE
;
3429 log_level
= BT_EVENT_CLASS_LOG_LEVEL_DEBUG_UNIT
;
3432 log_level
= BT_EVENT_CLASS_LOG_LEVEL_DEBUG_FUNCTION
;
3435 log_level
= BT_EVENT_CLASS_LOG_LEVEL_DEBUG_LINE
;
3438 log_level
= BT_EVENT_CLASS_LOG_LEVEL_DEBUG
;
3441 _BT_LOGW_NODE(node
, "Not setting event class's log level because its value is unknown: "
3442 "log-level=%" PRIu64
, loglevel_value
);
3445 if (log_level
!= BT_EVENT_CLASS_LOG_LEVEL_UNSPECIFIED
) {
3446 ret
= bt_event_class_set_log_level(
3447 event_class
, log_level
);
3450 "Cannot set event class's log level.");
3455 _SET(set
, _EVENT_LOGLEVEL_SET
);
3456 } else if (!strcmp(left
, "model.emf.uri")) {
3459 if (_IS_SET(set
, _EVENT_MODEL_EMF_URI_SET
)) {
3460 _BT_LOGE_DUP_ATTR(node
, "model.emf.uri",
3466 right
= concatenate_unary_strings(
3467 &node
->u
.ctf_expression
.right
);
3470 "Unexpected unary expression for event class's `model.emf.uri` attribute.");
3475 if (strlen(right
) == 0) {
3477 "Not setting event class's EMF URI because it's empty.");
3479 ret
= bt_event_class_set_emf_uri(
3480 event_class
, right
);
3483 "Cannot set event class's EMF URI.");
3489 _SET(set
, _EVENT_MODEL_EMF_URI_SET
);
3492 "Unknown attribute in event class: "
3493 "attr-name=\"%s\"", left
);
3518 char *get_event_decl_name(struct ctx
*ctx
, struct ctf_node
*node
)
3522 struct ctf_node
*iter
;
3523 struct bt_list_head
*decl_list
= &node
->u
.event
.declaration_list
;
3525 bt_list_for_each_entry(iter
, decl_list
, siblings
) {
3526 if (iter
->type
!= NODE_CTF_EXPRESSION
) {
3530 left
= concatenate_unary_strings(&iter
->u
.ctf_expression
.left
);
3533 "Cannot concatenate unary strings.");
3537 if (!strcmp(left
, "name")) {
3538 name
= concatenate_unary_strings(
3539 &iter
->u
.ctf_expression
.right
);
3542 "Unexpected unary expression for event class's `name` attribute.");
3564 int reset_event_decl_types(struct ctx
*ctx
,
3565 struct bt_event_class
*event_class
)
3570 ret
= bt_event_class_set_context_field_type(event_class
, NULL
);
3572 BT_LOGE("Cannot reset initial event class's context field type: "
3573 "event-name=\"%s\"",
3574 bt_event_class_get_name(event_class
));
3578 /* Event payload. */
3579 ret
= bt_event_class_set_payload_field_type(event_class
, NULL
);
3581 BT_LOGE("Cannot reset initial event class's payload field type: "
3582 "event-name=\"%s\"",
3583 bt_event_class_get_name(event_class
));
3591 int reset_stream_decl_types(struct ctx
*ctx
,
3592 struct bt_stream_class
*stream_class
)
3596 /* Packet context. */
3597 ret
= bt_stream_class_set_packet_context_field_type(stream_class
, NULL
);
3599 BT_LOGE_STR("Cannot reset initial stream class's packet context field type.");
3604 ret
= bt_stream_class_set_event_header_field_type(stream_class
, NULL
);
3606 BT_LOGE_STR("Cannot reset initial stream class's event header field type.");
3610 /* Event context. */
3611 ret
= bt_stream_class_set_event_context_field_type(stream_class
, NULL
);
3613 BT_LOGE_STR("Cannot reset initial stream class's event context field type.");
3621 struct bt_stream_class
*create_reset_stream_class(struct ctx
*ctx
)
3624 struct bt_stream_class
*stream_class
;
3626 stream_class
= bt_stream_class_create(NULL
);
3627 if (!stream_class
) {
3628 BT_LOGE_STR("Cannot create empty stream class.");
3633 * Set packet context, event header, and event context to NULL to
3634 * override the default ones.
3636 ret
= reset_stream_decl_types(ctx
, stream_class
);
3641 return stream_class
;
3644 BT_PUT(stream_class
);
3650 int visit_event_decl(struct ctx
*ctx
, struct ctf_node
*node
)
3655 struct ctf_node
*iter
;
3656 int64_t stream_id
= -1;
3657 char *event_name
= NULL
;
3658 struct bt_event_class
*event_class
= NULL
;
3659 struct bt_event_class
*eevent_class
;
3660 struct bt_stream_class
*stream_class
= NULL
;
3661 struct bt_list_head
*decl_list
= &node
->u
.event
.declaration_list
;
3662 bool pop_scope
= false;
3664 if (node
->visited
) {
3668 node
->visited
= TRUE
;
3669 event_name
= get_event_decl_name(ctx
, node
);
3672 "Missing `name` attribute in event class.");
3677 event_class
= bt_event_class_create(event_name
);
3680 * Unset context and fields to override the default ones.
3682 ret
= reset_event_decl_types(ctx
, event_class
);
3685 "Cannot reset event class's field types: "
3690 ret
= ctx_push_scope(ctx
);
3692 BT_LOGE_STR("Cannot push scope.");
3698 bt_list_for_each_entry(iter
, decl_list
, siblings
) {
3699 ret
= visit_event_decl_entry(ctx
, iter
, event_class
,
3702 _BT_LOGE_NODE(iter
, "Cannot visit event class's entry: "
3708 if (!_IS_SET(&set
, _EVENT_STREAM_ID_SET
)) {
3710 int64_t *new_stream_id
;
3711 struct bt_stream_class
*new_stream_class
;
3712 size_t stream_class_count
=
3713 g_hash_table_size(ctx
->stream_classes
) +
3714 bt_trace_get_stream_class_count(ctx
->trace
);
3717 * Allow missing stream_id if there is only a single
3720 switch (stream_class_count
) {
3722 /* Create implicit stream class if there's none */
3724 new_stream_class
= create_reset_stream_class(ctx
);
3725 if (!new_stream_class
) {
3727 "Cannot create empty stream class.");
3732 ret
= bt_stream_class_set_id(new_stream_class
,
3736 "Cannot set stream class's ID: "
3737 "id=0, ret=%d", ret
);
3738 BT_PUT(new_stream_class
);
3742 new_stream_id
= g_new0(int64_t, 1);
3743 if (!new_stream_id
) {
3744 BT_LOGE_STR("Failed to allocate a int64_t.");
3749 *new_stream_id
= stream_id
;
3751 /* Move reference to visitor's context */
3752 g_hash_table_insert(ctx
->stream_classes
,
3753 new_stream_id
, new_stream_class
);
3754 new_stream_id
= NULL
;
3755 new_stream_class
= NULL
;
3758 /* Single stream class: get its ID */
3759 if (g_hash_table_size(ctx
->stream_classes
) == 1) {
3760 keys
= g_hash_table_get_keys(ctx
->stream_classes
);
3761 stream_id
= *((int64_t *) keys
->data
);
3764 BT_ASSERT(bt_trace_get_stream_class_count(
3767 bt_trace_get_stream_class_by_index(
3769 BT_ASSERT(stream_class
);
3770 stream_id
= bt_stream_class_get_id(
3772 BT_PUT(stream_class
);
3777 "Missing `stream_id` attribute in event class.");
3783 BT_ASSERT(stream_id
>= 0);
3785 /* We have the stream ID now; get the stream class if found */
3786 stream_class
= g_hash_table_lookup(ctx
->stream_classes
, &stream_id
);
3787 bt_get(stream_class
);
3788 if (!stream_class
) {
3789 stream_class
= bt_trace_get_stream_class_by_id(ctx
->trace
,
3791 if (!stream_class
) {
3793 "Cannot find stream class at this point: "
3794 "id=%" PRId64
, stream_id
);
3800 BT_ASSERT(stream_class
);
3802 if (!_IS_SET(&set
, _EVENT_ID_SET
)) {
3803 /* Allow only one event without ID per stream */
3804 if (bt_stream_class_get_event_class_count(stream_class
) !=
3807 "Missing `id` attribute in event class.");
3813 ret
= bt_event_class_set_id(event_class
, 0);
3816 "Cannot set event class's ID: id=0, ret=%d",
3822 event_id
= bt_event_class_get_id(event_class
);
3824 _BT_LOGE_NODE(node
, "Cannot get event class's ID.");
3829 eevent_class
= bt_stream_class_get_event_class_by_id(stream_class
,
3832 BT_PUT(eevent_class
);
3834 "Duplicate event class (same ID) in the same stream class: "
3835 "id=%" PRId64
, event_id
);
3840 ret
= bt_stream_class_add_event_class(stream_class
, event_class
);
3841 BT_PUT(event_class
);
3844 "Cannot add event class to stream class: ret=%d", ret
);
3851 bt_put(event_class
);
3859 bt_put(stream_class
);
3864 int auto_map_field_to_trace_clock_class(struct ctx
*ctx
,
3865 struct bt_field_type
*ft
)
3867 struct bt_clock_class
*clock_class_to_map_to
= NULL
;
3868 struct bt_clock_class
*mapped_clock_class
= NULL
;
3870 int64_t clock_class_count
;
3872 if (!ft
|| !bt_field_type_is_integer(ft
)) {
3876 mapped_clock_class
=
3877 bt_field_type_integer_get_mapped_clock_class(ft
);
3878 if (mapped_clock_class
) {
3882 clock_class_count
= bt_trace_get_clock_class_count(ctx
->trace
);
3883 BT_ASSERT(clock_class_count
>= 0);
3885 switch (clock_class_count
) {
3888 * No clock class exists in the trace at this
3889 * point. Create an implicit one at 1 GHz,
3890 * named `default`, and use this clock class.
3892 clock_class_to_map_to
= bt_clock_class_create("default",
3894 if (!clock_class_to_map_to
) {
3895 BT_LOGE_STR("Cannot create a clock class.");
3900 ret
= bt_trace_add_clock_class(ctx
->trace
,
3901 clock_class_to_map_to
);
3903 BT_LOGE_STR("Cannot add clock class to trace.");
3909 * Only one clock class exists in the trace at
3910 * this point: use this one.
3912 clock_class_to_map_to
=
3913 bt_trace_get_clock_class_by_index(ctx
->trace
, 0);
3914 BT_ASSERT(clock_class_to_map_to
);
3918 * Timestamp field not mapped to a clock class
3919 * and there's more than one clock class in the
3920 * trace: this is an error.
3922 BT_LOGE_STR("Timestamp field found with no mapped clock class, "
3923 "but there's more than one clock class in the trace at this point.");
3928 BT_ASSERT(clock_class_to_map_to
);
3929 ret
= bt_field_type_integer_set_mapped_clock_class(ft
,
3930 clock_class_to_map_to
);
3932 BT_LOGE("Cannot map field type's field to trace's clock class: "
3933 "clock-class-name=\"%s\", ret=%d",
3934 bt_clock_class_get_name(clock_class_to_map_to
),
3940 bt_put(clock_class_to_map_to
);
3941 bt_put(mapped_clock_class
);
3946 int auto_map_fields_to_trace_clock_class(struct ctx
*ctx
,
3947 struct bt_field_type
*root_ft
, const char *field_name
)
3956 if (!bt_field_type_is_structure(root_ft
) &&
3957 !bt_field_type_is_variant(root_ft
)) {
3961 if (bt_field_type_is_structure(root_ft
)) {
3962 count
= bt_field_type_structure_get_field_count(root_ft
);
3964 count
= bt_field_type_variant_get_field_count(root_ft
);
3967 BT_ASSERT(count
>= 0);
3969 for (i
= 0; i
< count
; i
++) {
3970 _BT_FIELD_TYPE_INIT(ft
);
3973 if (bt_field_type_is_structure(root_ft
)) {
3974 ret
= bt_field_type_structure_get_field_by_index(
3975 root_ft
, &name
, &ft
, i
);
3976 } else if (bt_field_type_is_variant(root_ft
)) {
3977 ret
= bt_field_type_variant_get_field_by_index(
3978 root_ft
, &name
, &ft
, i
);
3981 BT_ASSERT(ret
== 0);
3983 if (strcmp(name
, field_name
) == 0) {
3984 ret
= auto_map_field_to_trace_clock_class(ctx
, ft
);
3986 BT_LOGE("Cannot automatically map field to trace's clock class: "
3987 "field-name=\"%s\"", field_name
);
3993 ret
= auto_map_fields_to_trace_clock_class(ctx
, ft
, field_name
);
3996 BT_LOGE("Cannot automatically map structure or variant field type's fields to trace's clock class: "
3997 "field-name=\"%s\", root-field-name=\"%s\"",
4008 int visit_stream_decl_entry(struct ctx
*ctx
, struct ctf_node
*node
,
4009 struct bt_stream_class
*stream_class
, int *set
)
4013 _BT_FIELD_TYPE_INIT(decl
);
4015 switch (node
->type
) {
4017 ret
= visit_typedef(ctx
, node
->u
._typedef
.type_specifier_list
,
4018 &node
->u
._typedef
.type_declarators
);
4021 "Cannot add type definition found in stream class.");
4025 case NODE_TYPEALIAS
:
4026 ret
= visit_typealias(ctx
, node
->u
.typealias
.target
,
4027 node
->u
.typealias
.alias
);
4030 "Cannot add type alias found in stream class.");
4034 case NODE_CTF_EXPRESSION
:
4036 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
4038 _BT_LOGE_NODE(node
, "Cannot concatenate unary strings.");
4043 if (!strcmp(left
, "id")) {
4047 if (_IS_SET(set
, _STREAM_ID_SET
)) {
4048 _BT_LOGE_DUP_ATTR(node
, "id",
4049 "stream declaration");
4054 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
4056 /* Only read "id" if get_unary_unsigned() succeeded. */
4057 if (ret
|| (!ret
&& id
< 0)) {
4059 "Unexpected unary expression for stream class's `id` attribute.");
4064 ptr
= g_hash_table_lookup(ctx
->stream_classes
, &id
);
4067 "Duplicate stream class (same ID): id=%" PRId64
,
4073 ret
= bt_stream_class_set_id(stream_class
, id
);
4076 "Cannot set stream class's ID: "
4077 "id=%" PRId64
", ret=%d", id
, ret
);
4081 _SET(set
, _STREAM_ID_SET
);
4082 } else if (!strcmp(left
, "event.header")) {
4083 if (_IS_SET(set
, _STREAM_EVENT_HEADER_SET
)) {
4085 "Duplicate `event.header` entry in stream class.");
4090 ret
= visit_type_specifier_list(ctx
,
4091 _BT_LIST_FIRST_ENTRY(
4092 &node
->u
.ctf_expression
.right
,
4093 struct ctf_node
, siblings
),
4097 "Cannot create stream class's event header field type.");
4102 ret
= auto_map_fields_to_trace_clock_class(ctx
,
4106 "Cannot automatically map specific event header field type fields named `timestamp` to trace's clock class.");
4110 ret
= bt_stream_class_set_event_header_field_type(
4111 stream_class
, decl
);
4115 "Cannot set stream class's event header field type.");
4119 _SET(set
, _STREAM_EVENT_HEADER_SET
);
4120 } else if (!strcmp(left
, "event.context")) {
4121 if (_IS_SET(set
, _STREAM_EVENT_CONTEXT_SET
)) {
4123 "Duplicate `event.context` entry in stream class.");
4128 ret
= visit_type_specifier_list(ctx
,
4129 _BT_LIST_FIRST_ENTRY(
4130 &node
->u
.ctf_expression
.right
,
4131 struct ctf_node
, siblings
),
4135 "Cannot create stream class's event context field type.");
4141 ret
= bt_stream_class_set_event_context_field_type(
4142 stream_class
, decl
);
4146 "Cannot set stream class's event context field type.");
4150 _SET(set
, _STREAM_EVENT_CONTEXT_SET
);
4151 } else if (!strcmp(left
, "packet.context")) {
4152 if (_IS_SET(set
, _STREAM_PACKET_CONTEXT_SET
)) {
4154 "Duplicate `packet.context` entry in stream class.");
4159 ret
= visit_type_specifier_list(ctx
,
4160 _BT_LIST_FIRST_ENTRY(
4161 &node
->u
.ctf_expression
.right
,
4162 struct ctf_node
, siblings
),
4166 "Cannot create stream class's packet context field type.");
4171 ret
= auto_map_fields_to_trace_clock_class(ctx
,
4172 decl
, "timestamp_begin");
4175 "Cannot automatically map specific packet context field type fields named `timestamp_begin` to trace's clock class.");
4179 ret
= auto_map_fields_to_trace_clock_class(ctx
,
4180 decl
, "timestamp_end");
4183 "Cannot automatically map specific packet context field type fields named `timestamp_end` to trace's clock class.");
4187 ret
= bt_stream_class_set_packet_context_field_type(
4188 stream_class
, decl
);
4192 "Cannot set stream class's packet context field type.");
4196 _SET(set
, _STREAM_PACKET_CONTEXT_SET
);
4199 "Unknown attribute in stream class: "
4200 "attr-name=\"%s\"", left
);
4223 int visit_stream_decl(struct ctx
*ctx
, struct ctf_node
*node
)
4229 struct ctf_node
*iter
;
4230 struct bt_stream_class
*stream_class
= NULL
;
4231 struct bt_stream_class
*existing_stream_class
= NULL
;
4232 struct bt_list_head
*decl_list
= &node
->u
.stream
.declaration_list
;
4234 if (node
->visited
) {
4238 node
->visited
= TRUE
;
4239 stream_class
= create_reset_stream_class(ctx
);
4240 if (!stream_class
) {
4241 _BT_LOGE_NODE(node
, "Cannot create empty stream class.");
4246 ret
= ctx_push_scope(ctx
);
4248 BT_LOGE_STR("Cannot push scope.");
4252 bt_list_for_each_entry(iter
, decl_list
, siblings
) {
4253 ret
= visit_stream_decl_entry(ctx
, iter
, stream_class
, &set
);
4256 "Cannot visit stream class's entry: "
4265 if (_IS_SET(&set
, _STREAM_ID_SET
)) {
4266 /* Check that packet header has stream_id field */
4267 _BT_FIELD_TYPE_INIT(stream_id_decl
);
4268 _BT_FIELD_TYPE_INIT(packet_header_decl
);
4270 packet_header_decl
=
4271 bt_trace_get_packet_header_field_type(ctx
->trace
);
4272 if (!packet_header_decl
) {
4274 "Stream class has a `id` attribute, "
4275 "but trace has no packet header field type.");
4280 bt_field_type_structure_get_field_type_by_name(
4281 packet_header_decl
, "stream_id");
4282 BT_PUT(packet_header_decl
);
4283 if (!stream_id_decl
) {
4285 "Stream class has a `id` attribute, "
4286 "but trace's packet header field type has no `stream_id` field.");
4290 if (!bt_field_type_is_integer(stream_id_decl
)) {
4291 BT_PUT(stream_id_decl
);
4293 "Stream class has a `id` attribute, "
4294 "but trace's packet header field type's `stream_id` field is not an integer field type.");
4298 BT_PUT(stream_id_decl
);
4300 /* Allow only _one_ ID-less stream */
4301 if (g_hash_table_size(ctx
->stream_classes
) != 0) {
4303 "Missing `id` attribute in stream class as there's more than one stream class in the trace.");
4308 /* Automatic ID: 0 */
4309 ret
= bt_stream_class_set_id(stream_class
, 0);
4310 BT_ASSERT(ret
== 0);
4313 id
= bt_stream_class_get_id(stream_class
);
4316 "Cannot get stream class's ID.");
4322 * Make sure that this stream class's ID is currently unique in
4325 existing_stream_class
= bt_trace_get_stream_class_by_id(ctx
->trace
,
4327 if (g_hash_table_lookup(ctx
->stream_classes
, &id
) ||
4328 existing_stream_class
) {
4330 "Duplicate stream class (same ID): id=%" PRId64
,
4336 new_id
= g_new0(int64_t, 1);
4338 BT_LOGE_STR("Failed to allocate a int64_t.");
4344 /* Move reference to visitor's context */
4345 g_hash_table_insert(ctx
->stream_classes
, new_id
,
4347 stream_class
= NULL
;
4351 bt_put(stream_class
);
4354 bt_put(existing_stream_class
);
4359 int visit_trace_decl_entry(struct ctx
*ctx
, struct ctf_node
*node
, int *set
)
4363 _BT_FIELD_TYPE_INIT(packet_header_decl
);
4365 switch (node
->type
) {
4367 ret
= visit_typedef(ctx
, node
->u
._typedef
.type_specifier_list
,
4368 &node
->u
._typedef
.type_declarators
);
4371 "Cannot add type definition found in trace (`trace` block).");
4375 case NODE_TYPEALIAS
:
4376 ret
= visit_typealias(ctx
, node
->u
.typealias
.target
,
4377 node
->u
.typealias
.alias
);
4380 "Cannot add type alias found in trace (`trace` block).");
4384 case NODE_CTF_EXPRESSION
:
4386 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
4388 _BT_LOGE_NODE(node
, "Cannot concatenate unary strings.");
4393 if (!strcmp(left
, "major")) {
4394 if (_IS_SET(set
, _TRACE_MAJOR_SET
)) {
4395 _BT_LOGE_DUP_ATTR(node
, "major", "trace");
4400 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
4404 "Unexpected unary expression for trace's `major` attribute.");
4409 _SET(set
, _TRACE_MAJOR_SET
);
4410 } else if (!strcmp(left
, "minor")) {
4411 if (_IS_SET(set
, _TRACE_MINOR_SET
)) {
4412 _BT_LOGE_DUP_ATTR(node
, "minor", "trace");
4417 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
4421 "Unexpected unary expression for trace's `minor` attribute.");
4426 _SET(set
, _TRACE_MINOR_SET
);
4427 } else if (!strcmp(left
, "uuid")) {
4428 if (_IS_SET(set
, _TRACE_UUID_SET
)) {
4429 _BT_LOGE_DUP_ATTR(node
, "uuid", "trace");
4434 ret
= get_unary_uuid(&node
->u
.ctf_expression
.right
,
4438 "Invalid trace's `uuid` attribute.");
4442 ret
= bt_trace_set_uuid(ctx
->trace
, ctx
->trace_uuid
);
4444 _BT_LOGE_NODE(node
, "Cannot set trace's UUID.");
4448 _SET(set
, _TRACE_UUID_SET
);
4449 } else if (!strcmp(left
, "byte_order")) {
4450 /* Native byte order is already known at this stage */
4451 if (_IS_SET(set
, _TRACE_BYTE_ORDER_SET
)) {
4452 _BT_LOGE_DUP_ATTR(node
, "byte_order",
4458 _SET(set
, _TRACE_BYTE_ORDER_SET
);
4459 } else if (!strcmp(left
, "packet.header")) {
4460 if (_IS_SET(set
, _TRACE_PACKET_HEADER_SET
)) {
4462 "Duplicate `packet.header` entry in trace.");
4467 ret
= visit_type_specifier_list(ctx
,
4468 _BT_LIST_FIRST_ENTRY(
4469 &node
->u
.ctf_expression
.right
,
4470 struct ctf_node
, siblings
),
4471 &packet_header_decl
);
4474 "Cannot create trace's packet header field type.");
4478 BT_ASSERT(packet_header_decl
);
4479 ret
= bt_trace_set_packet_header_field_type(ctx
->trace
,
4480 packet_header_decl
);
4481 BT_PUT(packet_header_decl
);
4484 "Cannot set trace's packet header field type.");
4488 _SET(set
, _TRACE_PACKET_HEADER_SET
);
4491 "Unknown attribute in stream class: "
4492 "attr-name=\"%s\"", left
);
4500 _BT_LOGE_NODE(node
, "Unknown expression in trace.");
4509 BT_PUT(packet_header_decl
);
4515 int visit_trace_decl(struct ctx
*ctx
, struct ctf_node
*node
)
4519 struct ctf_node
*iter
;
4520 struct bt_list_head
*decl_list
= &node
->u
.trace
.declaration_list
;
4522 if (node
->visited
) {
4526 node
->visited
= TRUE
;
4528 if (ctx
->is_trace_visited
) {
4529 _BT_LOGE_NODE(node
, "Duplicate trace (`trace` block).");
4534 ret
= ctx_push_scope(ctx
);
4536 BT_LOGE_STR("Cannot push scope.");
4540 bt_list_for_each_entry(iter
, decl_list
, siblings
) {
4541 ret
= visit_trace_decl_entry(ctx
, iter
, &set
);
4543 _BT_LOGE_NODE(iter
, "Cannot visit trace's entry (`trace` block): "
4552 if (!_IS_SET(&set
, _TRACE_MAJOR_SET
)) {
4554 "Missing `major` attribute in trace (`trace` block).");
4559 if (!_IS_SET(&set
, _TRACE_MINOR_SET
)) {
4561 "Missing `minor` attribute in trace (`trace` block).");
4566 if (!_IS_SET(&set
, _TRACE_BYTE_ORDER_SET
)) {
4568 "Missing `byte_order` attribute in trace (`trace` block).");
4573 ctx
->is_trace_visited
= TRUE
;
4583 int visit_env(struct ctx
*ctx
, struct ctf_node
*node
)
4587 struct ctf_node
*entry_node
;
4588 struct bt_list_head
*decl_list
= &node
->u
.env
.declaration_list
;
4590 if (node
->visited
) {
4594 node
->visited
= TRUE
;
4596 bt_list_for_each_entry(entry_node
, decl_list
, siblings
) {
4597 struct bt_list_head
*right_head
=
4598 &entry_node
->u
.ctf_expression
.right
;
4600 if (entry_node
->type
!= NODE_CTF_EXPRESSION
) {
4601 _BT_LOGE_NODE(entry_node
,
4602 "Wrong expression in environment entry: "
4603 "node-type=%d", entry_node
->type
);
4608 left
= concatenate_unary_strings(
4609 &entry_node
->u
.ctf_expression
.left
);
4611 _BT_LOGE_NODE(entry_node
,
4612 "Cannot get environment entry's name.");
4617 if (is_unary_string(right_head
)) {
4618 char *right
= concatenate_unary_strings(right_head
);
4621 _BT_LOGE_NODE(entry_node
,
4622 "Unexpected unary expression for environment entry's value: "
4623 "name=\"%s\"", left
);
4628 if (strcmp(left
, "tracer_name") == 0) {
4629 if (strncmp(right
, "lttng", 5) == 0) {
4630 BT_LOGI("Detected LTTng trace from `%s` environment value: "
4631 "tracer-name=\"%s\"",
4637 ret
= bt_trace_set_environment_field_string(
4638 ctx
->trace
, left
, right
);
4642 _BT_LOGE_NODE(entry_node
,
4643 "Cannot add string environment entry to trace: "
4644 "name=\"%s\", ret=%d", left
, ret
);
4647 } else if (is_unary_unsigned(right_head
) ||
4648 is_unary_signed(right_head
)) {
4651 if (is_unary_unsigned(right_head
)) {
4652 ret
= get_unary_unsigned(right_head
,
4655 ret
= get_unary_signed(right_head
, &v
);
4658 _BT_LOGE_NODE(entry_node
,
4659 "Unexpected unary expression for environment entry's value: "
4660 "name=\"%s\"", left
);
4665 ret
= bt_trace_set_environment_field_integer(
4666 ctx
->trace
, left
, v
);
4668 _BT_LOGE_NODE(entry_node
,
4669 "Cannot add integer environment entry to trace: "
4670 "name=\"%s\", ret=%d", left
, ret
);
4674 _BT_LOGW_NODE(entry_node
,
4675 "Environment entry has unknown type: "
4676 "name=\"%s\"", left
);
4693 int set_trace_byte_order(struct ctx
*ctx
, struct ctf_node
*trace_node
)
4698 struct ctf_node
*node
;
4699 struct bt_list_head
*decl_list
= &trace_node
->u
.trace
.declaration_list
;
4701 bt_list_for_each_entry(node
, decl_list
, siblings
) {
4702 if (node
->type
== NODE_CTF_EXPRESSION
) {
4703 struct ctf_node
*right_node
;
4705 left
= concatenate_unary_strings(
4706 &node
->u
.ctf_expression
.left
);
4709 "Cannot concatenate unary strings.");
4714 if (!strcmp(left
, "byte_order")) {
4715 enum bt_byte_order bo
;
4717 if (_IS_SET(&set
, _TRACE_BYTE_ORDER_SET
)) {
4718 _BT_LOGE_DUP_ATTR(node
, "byte_order",
4724 _SET(&set
, _TRACE_BYTE_ORDER_SET
);
4725 right_node
= _BT_LIST_FIRST_ENTRY(
4726 &node
->u
.ctf_expression
.right
,
4727 struct ctf_node
, siblings
);
4728 bo
= byte_order_from_unary_expr(right_node
);
4729 if (bo
== BT_BYTE_ORDER_UNKNOWN
) {
4731 "Invalid `byte_order` attribute in trace (`trace` block): "
4732 "expecting `le`, `be`, or `network`.");
4735 } else if (bo
== BT_BYTE_ORDER_NATIVE
) {
4737 "Invalid `byte_order` attribute in trace (`trace` block): "
4738 "cannot be set to `native` here.");
4744 ret
= bt_trace_set_native_byte_order(
4748 "Cannot set trace's byte order: "
4759 if (!_IS_SET(&set
, _TRACE_BYTE_ORDER_SET
)) {
4760 _BT_LOGE_NODE(trace_node
,
4761 "Missing `byte_order` attribute in trace (`trace` block).");
4775 int visit_clock_decl_entry(struct ctx
*ctx
, struct ctf_node
*entry_node
,
4776 struct bt_clock_class
*clock
, int *set
)
4781 if (entry_node
->type
!= NODE_CTF_EXPRESSION
) {
4782 _BT_LOGE_NODE(entry_node
,
4783 "Unexpected node type: node-type=%d",
4789 left
= concatenate_unary_strings(&entry_node
->u
.ctf_expression
.left
);
4791 _BT_LOGE_NODE(entry_node
, "Cannot concatenate unary strings.");
4796 if (!strcmp(left
, "name")) {
4799 if (_IS_SET(set
, _CLOCK_NAME_SET
)) {
4800 _BT_LOGE_DUP_ATTR(entry_node
, "name", "clock class");
4805 right
= concatenate_unary_strings(
4806 &entry_node
->u
.ctf_expression
.right
);
4808 _BT_LOGE_NODE(entry_node
,
4809 "Unexpected unary expression for clock class's `name` attribute.");
4814 ret
= bt_clock_class_set_name(clock
, right
);
4816 _BT_LOGE_NODE(entry_node
,
4817 "cannot set clock class's name");
4823 _SET(set
, _CLOCK_NAME_SET
);
4824 } else if (!strcmp(left
, "uuid")) {
4825 unsigned char uuid
[BABELTRACE_UUID_LEN
];
4827 if (_IS_SET(set
, _CLOCK_UUID_SET
)) {
4828 _BT_LOGE_DUP_ATTR(entry_node
, "uuid", "clock class");
4833 ret
= get_unary_uuid(&entry_node
->u
.ctf_expression
.right
, uuid
);
4835 _BT_LOGE_NODE(entry_node
,
4836 "Invalid clock class's `uuid` attribute.");
4840 ret
= bt_clock_class_set_uuid(clock
, uuid
);
4842 _BT_LOGE_NODE(entry_node
,
4843 "Cannot set clock class's UUID.");
4847 _SET(set
, _CLOCK_UUID_SET
);
4848 } else if (!strcmp(left
, "description")) {
4851 if (_IS_SET(set
, _CLOCK_DESCRIPTION_SET
)) {
4852 _BT_LOGE_DUP_ATTR(entry_node
, "description",
4858 right
= concatenate_unary_strings(
4859 &entry_node
->u
.ctf_expression
.right
);
4861 _BT_LOGE_NODE(entry_node
,
4862 "Unexpected unary expression for clock class's `description` attribute.");
4867 ret
= bt_clock_class_set_description(clock
, right
);
4869 _BT_LOGE_NODE(entry_node
,
4870 "Cannot set clock class's description.");
4876 _SET(set
, _CLOCK_DESCRIPTION_SET
);
4877 } else if (!strcmp(left
, "freq")) {
4878 uint64_t freq
= -1ULL;
4880 if (_IS_SET(set
, _CLOCK_FREQ_SET
)) {
4881 _BT_LOGE_DUP_ATTR(entry_node
, "freq", "clock class");
4886 ret
= get_unary_unsigned(
4887 &entry_node
->u
.ctf_expression
.right
, &freq
);
4889 _BT_LOGE_NODE(entry_node
,
4890 "Unexpected unary expression for clock class's `freq` attribute.");
4895 if (freq
== -1ULL || freq
== 0) {
4896 _BT_LOGE_NODE(entry_node
,
4897 "Invalid clock class frequency: freq=%" PRIu64
,
4903 ret
= bt_clock_class_set_frequency(clock
, freq
);
4905 _BT_LOGE_NODE(entry_node
,
4906 "Cannot set clock class's frequency.");
4910 _SET(set
, _CLOCK_FREQ_SET
);
4911 } else if (!strcmp(left
, "precision")) {
4914 if (_IS_SET(set
, _CLOCK_PRECISION_SET
)) {
4915 _BT_LOGE_DUP_ATTR(entry_node
, "precision",
4921 ret
= get_unary_unsigned(
4922 &entry_node
->u
.ctf_expression
.right
, &precision
);
4924 _BT_LOGE_NODE(entry_node
,
4925 "Unexpected unary expression for clock class's `precision` attribute.");
4930 ret
= bt_clock_class_set_precision(clock
, precision
);
4932 _BT_LOGE_NODE(entry_node
,
4933 "Cannot set clock class's precision.");
4937 _SET(set
, _CLOCK_PRECISION_SET
);
4938 } else if (!strcmp(left
, "offset_s")) {
4941 if (_IS_SET(set
, _CLOCK_OFFSET_S_SET
)) {
4942 _BT_LOGE_DUP_ATTR(entry_node
, "offset_s",
4948 ret
= get_unary_signed(
4949 &entry_node
->u
.ctf_expression
.right
, &offset_s
);
4951 _BT_LOGE_NODE(entry_node
,
4952 "Unexpected unary expression for clock class's `offset_s` attribute.");
4957 ret
= bt_clock_class_set_offset_s(clock
, offset_s
);
4959 _BT_LOGE_NODE(entry_node
,
4960 "Cannot set clock class's offset in seconds.");
4964 _SET(set
, _CLOCK_OFFSET_S_SET
);
4965 } else if (!strcmp(left
, "offset")) {
4968 if (_IS_SET(set
, _CLOCK_OFFSET_SET
)) {
4969 _BT_LOGE_DUP_ATTR(entry_node
, "offset", "clock class");
4974 ret
= get_unary_signed(
4975 &entry_node
->u
.ctf_expression
.right
, &offset
);
4977 _BT_LOGE_NODE(entry_node
,
4978 "Unexpected unary expression for clock class's `offset` attribute.");
4983 ret
= bt_clock_class_set_offset_cycles(clock
, offset
);
4985 _BT_LOGE_NODE(entry_node
,
4986 "Cannot set clock class's offset in cycles.");
4990 _SET(set
, _CLOCK_OFFSET_SET
);
4991 } else if (!strcmp(left
, "absolute")) {
4992 struct ctf_node
*right
;
4994 if (_IS_SET(set
, _CLOCK_ABSOLUTE_SET
)) {
4995 _BT_LOGE_DUP_ATTR(entry_node
, "absolute",
5001 right
= _BT_LIST_FIRST_ENTRY(
5002 &entry_node
->u
.ctf_expression
.right
,
5003 struct ctf_node
, siblings
);
5004 ret
= get_boolean(right
);
5006 _BT_LOGE_NODE(entry_node
,
5007 "Unexpected unary expression for clock class's `absolute` attribute.");
5012 ret
= bt_clock_class_set_is_absolute(clock
, ret
);
5014 _BT_LOGE_NODE(entry_node
,
5015 "Cannot set clock class's absolute flag.");
5019 _SET(set
, _CLOCK_ABSOLUTE_SET
);
5021 _BT_LOGW_NODE(entry_node
,
5022 "Unknown attribute in clock class: attr-name=\"%s\"",
5038 int64_t cycles_from_ns(uint64_t frequency
, int64_t ns
)
5043 if (frequency
== 1000000000ULL) {
5046 cycles
= (uint64_t) (((double) ns
* (double) frequency
) / 1e9
);
5053 int apply_clock_class_offset(struct ctx
*ctx
, struct bt_clock_class
*clock
)
5057 int64_t offset_cycles
;
5058 int64_t offset_to_apply
;
5060 freq
= bt_clock_class_get_frequency(clock
);
5061 if (freq
== -1ULL) {
5062 BT_LOGE_STR("Cannot get clock class's frequency.");
5067 ret
= bt_clock_class_get_offset_cycles(clock
, &offset_cycles
);
5069 BT_LOGE_STR("Cannot get clock class's offset in cycles.");
5075 ctx
->decoder_config
.clock_class_offset_s
* 1000000000LL +
5076 ctx
->decoder_config
.clock_class_offset_ns
;
5077 offset_cycles
+= cycles_from_ns(freq
, offset_to_apply
);
5078 ret
= bt_clock_class_set_offset_cycles(clock
, offset_cycles
);
5085 int visit_clock_decl(struct ctx
*ctx
, struct ctf_node
*clock_node
)
5089 struct bt_clock_class
*clock
;
5090 struct ctf_node
*entry_node
;
5091 struct bt_list_head
*decl_list
= &clock_node
->u
.clock
.declaration_list
;
5092 const char *clock_class_name
;
5094 if (clock_node
->visited
) {
5098 clock_node
->visited
= TRUE
;
5100 /* CTF 1.8's default frequency for a clock class is 1 GHz */
5101 clock
= bt_clock_class_create(NULL
, 1000000000);
5103 _BT_LOGE_NODE(clock_node
,
5104 "Cannot create default clock class.");
5109 bt_list_for_each_entry(entry_node
, decl_list
, siblings
) {
5110 ret
= visit_clock_decl_entry(ctx
, entry_node
, clock
, &set
);
5112 _BT_LOGE_NODE(entry_node
,
5113 "Cannot visit clock class's entry: ret=%d",
5119 if (!_IS_SET(&set
, _CLOCK_NAME_SET
)) {
5120 _BT_LOGE_NODE(clock_node
,
5121 "Missing `name` attribute in clock class.");
5126 clock_class_name
= bt_clock_class_get_name(clock
);
5127 BT_ASSERT(clock_class_name
);
5128 if (ctx
->is_lttng
&& strcmp(clock_class_name
, "monotonic") == 0) {
5130 * Old versions of LTTng forgot to set its clock class
5131 * as absolute, even if it is. This is important because
5132 * it's a condition to be able to sort notifications
5133 * from different sources.
5135 ret
= bt_clock_class_set_is_absolute(clock
, 1);
5137 _BT_LOGE_NODE(clock_node
,
5138 "Cannot set clock class's absolute flag.");
5143 ret
= apply_clock_class_offset(ctx
, clock
);
5145 _BT_LOGE_NODE(clock_node
,
5146 "Cannot apply clock class's custom offset.");
5150 ret
= bt_trace_add_clock_class(ctx
->trace
, clock
);
5152 _BT_LOGE_NODE(clock_node
,
5153 "Cannot add clock class to trace.");
5164 int visit_root_decl(struct ctx
*ctx
, struct ctf_node
*root_decl_node
)
5168 if (root_decl_node
->visited
) {
5172 root_decl_node
->visited
= TRUE
;
5174 switch (root_decl_node
->type
) {
5176 ret
= visit_typedef(ctx
,
5177 root_decl_node
->u
._typedef
.type_specifier_list
,
5178 &root_decl_node
->u
._typedef
.type_declarators
);
5180 _BT_LOGE_NODE(root_decl_node
,
5181 "Cannot add type definition found in root scope.");
5185 case NODE_TYPEALIAS
:
5186 ret
= visit_typealias(ctx
, root_decl_node
->u
.typealias
.target
,
5187 root_decl_node
->u
.typealias
.alias
);
5189 _BT_LOGE_NODE(root_decl_node
,
5190 "Cannot add type alias found in root scope.");
5194 case NODE_TYPE_SPECIFIER_LIST
:
5196 _BT_FIELD_TYPE_INIT(decl
);
5199 * Just add the type specifier to the root
5200 * declaration scope. Put local reference.
5202 ret
= visit_type_specifier_list(ctx
, root_decl_node
, &decl
);
5204 _BT_LOGE_NODE(root_decl_node
,
5205 "Cannot visit root scope's field type: "
5215 _BT_LOGE_NODE(root_decl_node
,
5216 "Unexpected node type: node-type=%d",
5217 root_decl_node
->type
);
5227 int set_trace_name(struct ctx
*ctx
)
5231 struct bt_value
*value
= NULL
;
5233 BT_ASSERT(bt_trace_get_stream_class_count(ctx
->trace
) == 0);
5234 name
= g_string_new(NULL
);
5236 BT_LOGE_STR("Failed to allocate a GString.");
5242 * Check if we have a trace environment string value named `hostname`.
5243 * If so, use it as the trace name's prefix.
5245 value
= bt_trace_get_environment_field_value_by_name(ctx
->trace
,
5247 if (value
&& bt_value_is_string(value
)) {
5248 const char *hostname
;
5250 ret
= bt_value_string_get(value
, &hostname
);
5251 BT_ASSERT(ret
== 0);
5252 g_string_append(name
, hostname
);
5254 if (ctx
->trace_name_suffix
) {
5255 g_string_append_c(name
, G_DIR_SEPARATOR
);
5259 if (ctx
->trace_name_suffix
) {
5260 g_string_append(name
, ctx
->trace_name_suffix
);
5263 ret
= bt_trace_set_name(ctx
->trace
, name
->str
);
5265 BT_LOGE("Cannot set trace's name: name=\"%s\"", name
->str
);
5278 g_string_free(name
, TRUE
);
5285 int move_ctx_stream_classes_to_trace(struct ctx
*ctx
)
5288 GHashTableIter iter
;
5289 gpointer key
, stream_class
;
5291 if (g_hash_table_size(ctx
->stream_classes
) > 0 &&
5292 bt_trace_get_stream_class_count(ctx
->trace
) == 0) {
5294 * We're about to add the first stream class to the
5295 * trace. This will freeze the trace, and after this
5296 * we cannot set the name anymore. At this point,
5297 * set the trace name.
5299 ret
= set_trace_name(ctx
);
5301 BT_LOGE_STR("Cannot set trace's name.");
5306 g_hash_table_iter_init(&iter
, ctx
->stream_classes
);
5308 while (g_hash_table_iter_next(&iter
, &key
, &stream_class
)) {
5309 ret
= bt_trace_add_stream_class(ctx
->trace
,
5312 int64_t id
= bt_stream_class_get_id(stream_class
);
5313 BT_LOGE("Cannot add stream class to trace: id=%" PRId64
,
5319 g_hash_table_remove_all(ctx
->stream_classes
);
5326 struct ctf_visitor_generate_ir
*ctf_visitor_generate_ir_create(
5327 const struct ctf_metadata_decoder_config
*decoder_config
,
5331 struct ctx
*ctx
= NULL
;
5332 struct bt_trace
*trace
;
5334 trace
= bt_trace_create();
5336 BT_LOGE_STR("Cannot create empty trace.");
5340 /* Set packet header to NULL to override the default one */
5341 ret
= bt_trace_set_packet_header_field_type(trace
, NULL
);
5343 BT_LOGE_STR("Cannot reset initial trace's packet header field type.");
5347 /* Create visitor's context */
5348 ctx
= ctx_create(trace
, decoder_config
, name
);
5350 BT_LOGE_STR("Cannot create visitor's context.");
5363 return (void *) ctx
;
5367 void ctf_visitor_generate_ir_destroy(struct ctf_visitor_generate_ir
*visitor
)
5369 ctx_destroy((void *) visitor
);
5373 struct bt_trace
*ctf_visitor_generate_ir_get_trace(
5374 struct ctf_visitor_generate_ir
*visitor
)
5376 struct ctx
*ctx
= (void *) visitor
;
5379 BT_ASSERT(ctx
->trace
);
5380 return bt_get(ctx
->trace
);
5384 int ctf_visitor_generate_ir_visit_node(struct ctf_visitor_generate_ir
*visitor
,
5385 struct ctf_node
*node
)
5388 struct ctx
*ctx
= (void *) visitor
;
5390 BT_LOGI_STR("Visiting metadata's AST to generate CTF IR objects.");
5392 switch (node
->type
) {
5395 struct ctf_node
*iter
;
5396 int got_trace_decl
= FALSE
;
5399 * The first thing we need is the native byte order of
5400 * the trace block, because early type aliases can have
5401 * a `byte_order` attribute set to `native`. If we don't
5402 * have the native byte order yet, and we don't have any
5403 * trace block yet, then fail with EINCOMPLETE.
5405 if (ctx
->trace_bo
== BT_BYTE_ORDER_NATIVE
) {
5406 bt_list_for_each_entry(iter
, &node
->u
.root
.trace
, siblings
) {
5407 if (got_trace_decl
) {
5409 "Duplicate trace (`trace` block).");
5414 ret
= set_trace_byte_order(ctx
, iter
);
5417 "Cannot set trace's native byte order: "
5422 got_trace_decl
= TRUE
;
5425 if (!got_trace_decl
) {
5426 BT_LOGD_STR("Incomplete AST: need trace (`trace` block).");
5432 BT_ASSERT(ctx
->trace_bo
== BT_BYTE_ORDER_LITTLE_ENDIAN
||
5433 ctx
->trace_bo
== BT_BYTE_ORDER_BIG_ENDIAN
);
5434 BT_ASSERT(ctx
->current_scope
&&
5435 ctx
->current_scope
->parent_scope
== NULL
);
5438 bt_list_for_each_entry(iter
, &node
->u
.root
.env
, siblings
) {
5439 ret
= visit_env(ctx
, iter
);
5442 "Cannot visit trace's environment (`env` block) entry: "
5448 BT_ASSERT(ctx
->current_scope
&&
5449 ctx
->current_scope
->parent_scope
== NULL
);
5452 * Visit clock blocks.
5454 bt_list_for_each_entry(iter
, &node
->u
.root
.clock
, siblings
) {
5455 ret
= visit_clock_decl(ctx
, iter
);
5458 "Cannot visit clock class: ret=%d",
5464 BT_ASSERT(ctx
->current_scope
&&
5465 ctx
->current_scope
->parent_scope
== NULL
);
5468 * Visit root declarations next, as they can be used by any
5471 bt_list_for_each_entry(iter
, &node
->u
.root
.declaration_list
,
5473 ret
= visit_root_decl(ctx
, iter
);
5476 "Cannot visit root entry: ret=%d",
5482 BT_ASSERT(ctx
->current_scope
&&
5483 ctx
->current_scope
->parent_scope
== NULL
);
5485 /* Callsite blocks are not supported */
5486 bt_list_for_each_entry(iter
, &node
->u
.root
.callsite
, siblings
) {
5488 "\"callsite\" blocks are not supported as of this version.");
5491 BT_ASSERT(ctx
->current_scope
&&
5492 ctx
->current_scope
->parent_scope
== NULL
);
5495 bt_list_for_each_entry(iter
, &node
->u
.root
.trace
, siblings
) {
5496 ret
= visit_trace_decl(ctx
, iter
);
5499 "Cannot visit trace (`trace` block): "
5505 BT_ASSERT(ctx
->current_scope
&&
5506 ctx
->current_scope
->parent_scope
== NULL
);
5509 bt_list_for_each_entry(iter
, &node
->u
.root
.stream
, siblings
) {
5510 ret
= visit_stream_decl(ctx
, iter
);
5513 "Cannot visit stream class: ret=%d",
5519 BT_ASSERT(ctx
->current_scope
&&
5520 ctx
->current_scope
->parent_scope
== NULL
);
5523 bt_list_for_each_entry(iter
, &node
->u
.root
.event
, siblings
) {
5524 ret
= visit_event_decl(ctx
, iter
);
5527 "Cannot visit event class: ret=%d",
5533 BT_ASSERT(ctx
->current_scope
&&
5534 ctx
->current_scope
->parent_scope
== NULL
);
5539 "Unexpected node type: node-type=%d",
5545 /* Move decoded stream classes to trace, if any */
5546 ret
= move_ctx_stream_classes_to_trace(ctx
);
5548 BT_LOGE("Cannot move stream classes to trace: ret=%d", ret
);