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-2018 - 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 #include "ctf-meta-visitors.h"
55 /* Bit value (left shift) */
56 #define _BV(_val) (1 << (_val))
58 /* Bit is set in a set of bits */
59 #define _IS_SET(_set, _mask) (*(_set) & (_mask))
61 /* Set bit in a set of bits */
62 #define _SET(_set, _mask) (*(_set) |= (_mask))
64 /* Try to push scope, or go to the `error` label */
65 #define _TRY_PUSH_SCOPE_OR_GOTO_ERROR() \
67 ret = ctx_push_scope(ctx); \
69 BT_LOGE_STR("Cannot push scope."); \
74 /* Bits for verifying existing attributes in various declarations */
76 _CLOCK_NAME_SET
= _BV(0),
77 _CLOCK_UUID_SET
= _BV(1),
78 _CLOCK_FREQ_SET
= _BV(2),
79 _CLOCK_PRECISION_SET
= _BV(3),
80 _CLOCK_OFFSET_S_SET
= _BV(4),
81 _CLOCK_OFFSET_SET
= _BV(5),
82 _CLOCK_ABSOLUTE_SET
= _BV(6),
83 _CLOCK_DESCRIPTION_SET
= _BV(7),
87 _INTEGER_ALIGN_SET
= _BV(0),
88 _INTEGER_SIZE_SET
= _BV(1),
89 _INTEGER_BASE_SET
= _BV(2),
90 _INTEGER_ENCODING_SET
= _BV(3),
91 _INTEGER_BYTE_ORDER_SET
= _BV(4),
92 _INTEGER_SIGNED_SET
= _BV(5),
93 _INTEGER_MAP_SET
= _BV(6),
97 _FLOAT_ALIGN_SET
= _BV(0),
98 _FLOAT_MANT_DIG_SET
= _BV(1),
99 _FLOAT_EXP_DIG_SET
= _BV(2),
100 _FLOAT_BYTE_ORDER_SET
= _BV(3),
104 _STRING_ENCODING_SET
= _BV(0),
108 _TRACE_MINOR_SET
= _BV(0),
109 _TRACE_MAJOR_SET
= _BV(1),
110 _TRACE_BYTE_ORDER_SET
= _BV(2),
111 _TRACE_UUID_SET
= _BV(3),
112 _TRACE_PACKET_HEADER_SET
= _BV(4),
116 _STREAM_ID_SET
= _BV(0),
117 _STREAM_PACKET_CONTEXT_SET
= _BV(1),
118 _STREAM_EVENT_HEADER_SET
= _BV(2),
119 _STREAM_EVENT_CONTEXT_SET
= _BV(3),
123 _EVENT_NAME_SET
= _BV(0),
124 _EVENT_ID_SET
= _BV(1),
125 _EVENT_MODEL_EMF_URI_SET
= _BV(2),
126 _EVENT_STREAM_ID_SET
= _BV(3),
127 _EVENT_LOG_LEVEL_SET
= _BV(4),
128 _EVENT_CONTEXT_SET
= _BV(5),
129 _EVENT_FIELDS_SET
= _BV(6),
137 LOG_LEVEL_WARNING
= 4,
138 LOG_LEVEL_NOTICE
= 5,
140 LOG_LEVEL_DEBUG_SYSTEM
= 7,
141 LOG_LEVEL_DEBUG_PROGRAM
= 8,
142 LOG_LEVEL_DEBUG_PROCESS
= 9,
143 LOG_LEVEL_DEBUG_MODULE
= 10,
144 LOG_LEVEL_DEBUG_UNIT
= 11,
145 LOG_LEVEL_DEBUG_FUNCTION
= 12,
146 LOG_LEVEL_DEBUG_LINE
= 13,
147 LOG_LEVEL_DEBUG
= 14,
151 /* Prefixes of type aliases */
152 #define _PREFIX_ALIAS 'a'
153 #define _PREFIX_ENUM 'e'
154 #define _PREFIX_STRUCT 's'
155 #define _PREFIX_VARIANT 'v'
157 /* First entry in a BT list */
158 #define _BT_LIST_FIRST_ENTRY(_ptr, _type, _member) \
159 bt_list_entry((_ptr)->next, _type, _member)
161 #define _BT_LOGE_DUP_ATTR(_node, _attr, _entity) \
162 _BT_LOGE_LINENO((_node)->lineno, \
163 "Duplicate attribute in %s: attr-name=\"%s\"", \
166 #define _BT_LOGE_NODE(_node, _msg, args...) \
167 _BT_LOGE_LINENO((_node)->lineno, _msg, ## args)
169 #define _BT_LOGW_NODE(_node, _msg, args...) \
170 _BT_LOGW_LINENO((_node)->lineno, _msg, ## args)
172 #define _BT_LOGV_NODE(_node, _msg, args...) \
173 _BT_LOGV_LINENO((_node)->lineno, _msg, ## args)
176 * Declaration scope of a visitor context. This represents a TSDL
177 * lexical scope, so that aliases and named structures, variants,
178 * and enumerations may be registered and looked up hierarchically.
180 struct ctx_decl_scope
{
182 * Alias name to field type.
184 * GQuark -> struct ctf_field_type * (owned by this)
186 GHashTable
*decl_map
;
188 /* Parent scope; NULL if this is the root declaration scope */
189 struct ctx_decl_scope
*parent_scope
;
193 * Visitor context (private).
196 /* Trace IR trace being filled (owned by this) */
197 struct bt_trace
*trace
;
199 /* CTF meta trace being filled (owned by this) */
200 struct ctf_trace_class
*ctf_tc
;
202 /* Current declaration scope (top of the stack) (owned by this) */
203 struct ctx_decl_scope
*current_scope
;
205 /* True if trace declaration is visited */
206 bool is_trace_visited
;
208 /* True if this is an LTTng trace */
211 /* Eventual name suffix of the trace to set (owned by this) */
212 char *trace_class_name_suffix
;
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
) ctf_field_type_destroy
);
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 levels Number of levels to dig into (-1 means infinite)
305 * @param copy True to return a copy
306 * @returns Declaration (owned by caller if \p copy is true),
307 * or NULL if not found
310 struct ctf_field_type
*ctx_decl_scope_lookup_prefix_alias(
311 struct ctx_decl_scope
*scope
, char prefix
, const char *name
,
312 int levels
, bool copy
)
316 struct ctf_field_type
*decl
= NULL
;
317 struct ctx_decl_scope
*cur_scope
= scope
;
321 qname
= get_prefixed_named_quark(prefix
, name
);
330 while (cur_scope
&& cur_levels
< levels
) {
331 decl
= g_hash_table_lookup(cur_scope
->decl_map
,
332 (gconstpointer
) GUINT_TO_POINTER(qname
));
334 /* Caller's reference */
336 decl
= ctf_field_type_copy(decl
);
343 cur_scope
= cur_scope
->parent_scope
;
352 * Looks up a type alias within a declaration scope.
354 * @param scope Declaration scope
355 * @param name Alias name
356 * @param levels Number of levels to dig into (-1 means infinite)
357 * @param copy True to return a copy
358 * @returns Declaration (owned by caller if \p copy is true),
359 * or NULL if not found
362 struct ctf_field_type
*ctx_decl_scope_lookup_alias(
363 struct ctx_decl_scope
*scope
, const char *name
, int levels
,
366 return ctx_decl_scope_lookup_prefix_alias(scope
, _PREFIX_ALIAS
,
371 * Looks up an enumeration within a declaration scope.
373 * @param scope Declaration scope
374 * @param name Enumeration name
375 * @param levels Number of levels to dig into (-1 means infinite)
376 * @param copy True to return a copy
377 * @returns Declaration (owned by caller if \p copy is true),
378 * or NULL if not found
381 struct ctf_field_type_enum
*ctx_decl_scope_lookup_enum(
382 struct ctx_decl_scope
*scope
, const char *name
, int levels
,
385 return (void *) ctx_decl_scope_lookup_prefix_alias(scope
, _PREFIX_ENUM
,
390 * Looks up a structure within a declaration scope.
392 * @param scope Declaration scope
393 * @param name Structure name
394 * @param levels Number of levels to dig into (-1 means infinite)
395 * @param copy True to return a copy
396 * @returns Declaration (owned by caller if \p copy is true),
397 * or NULL if not found
400 struct ctf_field_type_struct
*ctx_decl_scope_lookup_struct(
401 struct ctx_decl_scope
*scope
, const char *name
, int levels
,
404 return (void *) ctx_decl_scope_lookup_prefix_alias(scope
,
405 _PREFIX_STRUCT
, name
, levels
, copy
);
409 * Looks up a variant within a declaration scope.
411 * @param scope Declaration scope
412 * @param name Variant name
413 * @param levels Number of levels to dig into (-1 means infinite)
414 * @param copy True to return a copy
415 * @returns Declaration (owned by caller if \p copy is true),
416 * or NULL if not found
419 struct ctf_field_type_variant
*ctx_decl_scope_lookup_variant(
420 struct ctx_decl_scope
*scope
, const char *name
, int levels
,
423 return (void *) ctx_decl_scope_lookup_prefix_alias(scope
,
424 _PREFIX_VARIANT
, name
, levels
, copy
);
428 * Registers a prefixed type alias within a declaration scope.
430 * @param scope Declaration scope
431 * @param prefix Prefix character
432 * @param name Alias name (non-NULL)
433 * @param decl Field type to register (copied)
434 * @returns 0 if registration went okay, negative value otherwise
437 int ctx_decl_scope_register_prefix_alias(struct ctx_decl_scope
*scope
,
438 char prefix
, const char *name
, struct ctf_field_type
*decl
)
446 qname
= get_prefixed_named_quark(prefix
, name
);
452 /* Make sure alias does not exist in local scope */
453 if (ctx_decl_scope_lookup_prefix_alias(scope
, prefix
, name
, 1,
459 decl
= ctf_field_type_copy(decl
);
461 g_hash_table_insert(scope
->decl_map
, GUINT_TO_POINTER(qname
), decl
);
468 * Registers a type alias within a declaration scope.
470 * @param scope Declaration scope
471 * @param name Alias name (non-NULL)
472 * @param decl Field type to register (copied)
473 * @returns 0 if registration went okay, negative value otherwise
476 int ctx_decl_scope_register_alias(struct ctx_decl_scope
*scope
,
477 const char *name
, struct ctf_field_type
*decl
)
479 return ctx_decl_scope_register_prefix_alias(scope
, _PREFIX_ALIAS
,
480 name
, (void *) decl
);
484 * Registers an enumeration declaration within a declaration scope.
486 * @param scope Declaration scope
487 * @param name Enumeration name (non-NULL)
488 * @param decl Enumeration field type to register (copied)
489 * @returns 0 if registration went okay, negative value otherwise
492 int ctx_decl_scope_register_enum(struct ctx_decl_scope
*scope
,
493 const char *name
, struct ctf_field_type_enum
*decl
)
495 return ctx_decl_scope_register_prefix_alias(scope
, _PREFIX_ENUM
,
496 name
, (void *) decl
);
500 * Registers a structure declaration within a declaration scope.
502 * @param scope Declaration scope
503 * @param name Structure name (non-NULL)
504 * @param decl Structure field type to register (copied)
505 * @returns 0 if registration went okay, negative value otherwise
508 int ctx_decl_scope_register_struct(struct ctx_decl_scope
*scope
,
509 const char *name
, struct ctf_field_type_struct
*decl
)
511 return ctx_decl_scope_register_prefix_alias(scope
, _PREFIX_STRUCT
,
512 name
, (void *) decl
);
516 * Registers a variant declaration within a declaration scope.
518 * @param scope Declaration scope
519 * @param name Variant name (non-NULL)
520 * @param decl Variant field type to register
521 * @returns 0 if registration went okay, negative value otherwise
524 int ctx_decl_scope_register_variant(struct ctx_decl_scope
*scope
,
525 const char *name
, struct ctf_field_type_variant
*decl
)
527 return ctx_decl_scope_register_prefix_alias(scope
, _PREFIX_VARIANT
,
528 name
, (void *) decl
);
532 * Destroys a visitor context.
534 * @param ctx Visitor context to destroy
537 void ctx_destroy(struct ctx
*ctx
)
539 struct ctx_decl_scope
*scope
;
545 scope
= ctx
->current_scope
;
548 * Destroy all scopes, from current one to the root scope.
551 struct ctx_decl_scope
*parent_scope
= scope
->parent_scope
;
553 ctx_decl_scope_destroy(scope
);
554 scope
= parent_scope
;
560 ctf_trace_class_destroy(ctx
->ctf_tc
);
563 if (ctx
->trace_class_name_suffix
) {
564 free(ctx
->trace_class_name_suffix
);
574 * Creates a new visitor context.
576 * @param trace Associated trace
577 * @returns New visitor context, or NULL on error
580 struct ctx
*ctx_create(const struct ctf_metadata_decoder_config
*decoder_config
,
581 const char *trace_class_name_suffix
)
583 struct ctx
*ctx
= NULL
;
585 BT_ASSERT(decoder_config
);
587 ctx
= g_new0(struct ctx
, 1);
589 BT_LOGE_STR("Failed to allocate one visitor context.");
593 ctx
->trace
= bt_trace_create();
595 BT_LOGE_STR("Cannot create empty trace.");
599 ctx
->ctf_tc
= ctf_trace_class_create();
601 BT_LOGE_STR("Cannot create CTF trace class.");
605 /* Root declaration scope */
606 ctx
->current_scope
= ctx_decl_scope_create(NULL
);
607 if (!ctx
->current_scope
) {
608 BT_LOGE_STR("Cannot create declaration scope.");
612 if (trace_class_name_suffix
) {
613 ctx
->trace_class_name_suffix
= strdup(trace_class_name_suffix
);
614 if (!ctx
->trace_class_name_suffix
) {
615 BT_LOGE_STR("Failed to copy string.");
620 ctx
->decoder_config
= *decoder_config
;
632 * Pushes a new declaration scope on top of a visitor context's
633 * declaration scope stack.
635 * @param ctx Visitor context
636 * @returns 0 on success, or a negative value on error
639 int ctx_push_scope(struct ctx
*ctx
)
642 struct ctx_decl_scope
*new_scope
;
645 new_scope
= ctx_decl_scope_create(ctx
->current_scope
);
647 BT_LOGE_STR("Cannot create declaration scope.");
652 ctx
->current_scope
= new_scope
;
659 void ctx_pop_scope(struct ctx
*ctx
)
661 struct ctx_decl_scope
*parent_scope
= NULL
;
665 if (!ctx
->current_scope
) {
669 parent_scope
= ctx
->current_scope
->parent_scope
;
670 ctx_decl_scope_destroy(ctx
->current_scope
);
671 ctx
->current_scope
= parent_scope
;
678 int visit_type_specifier_list(struct ctx
*ctx
, struct ctf_node
*ts_list
,
679 struct ctf_field_type
**decl
);
682 char *remove_underscores_from_field_ref(const char *field_ref
)
688 UNDERSCORE_REMOVE_STATE_REMOVE_NEXT_UNDERSCORE
,
689 UNDERSCORE_REMOVE_STATE_DO_NOT_REMOVE_NEXT_UNDERSCORE
,
690 } state
= UNDERSCORE_REMOVE_STATE_REMOVE_NEXT_UNDERSCORE
;
692 BT_ASSERT(field_ref
);
693 ret
= calloc(strlen(field_ref
) + 1, 1);
695 BT_LOGE("Failed to allocate a string: size=%zu",
696 strlen(field_ref
) + 1);
703 while (*in_ch
!= '\0') {
707 /* Remove whitespace */
711 if (state
== UNDERSCORE_REMOVE_STATE_REMOVE_NEXT_UNDERSCORE
) {
713 state
= UNDERSCORE_REMOVE_STATE_DO_NOT_REMOVE_NEXT_UNDERSCORE
;
719 state
= UNDERSCORE_REMOVE_STATE_REMOVE_NEXT_UNDERSCORE
;
722 state
= UNDERSCORE_REMOVE_STATE_DO_NOT_REMOVE_NEXT_UNDERSCORE
;
737 int is_unary_string(struct bt_list_head
*head
)
740 struct ctf_node
*node
;
742 bt_list_for_each_entry(node
, head
, siblings
) {
743 if (node
->type
!= NODE_UNARY_EXPRESSION
) {
747 if (node
->u
.unary_expression
.type
!= UNARY_STRING
) {
756 char *concatenate_unary_strings(struct bt_list_head
*head
)
760 struct ctf_node
*node
;
762 str
= g_string_new(NULL
);
765 bt_list_for_each_entry(node
, head
, siblings
) {
769 node
->type
!= NODE_UNARY_EXPRESSION
||
770 node
->u
.unary_expression
.type
!= UNARY_STRING
||
773 node
->u
.unary_expression
.link
!=
781 switch (node
->u
.unary_expression
.link
) {
783 g_string_append(str
, ".");
785 case UNARY_ARROWLINK
:
786 g_string_append(str
, "->");
788 case UNARY_DOTDOTDOT
:
789 g_string_append(str
, "...");
795 src_string
= node
->u
.unary_expression
.u
.string
;
796 g_string_append(str
, src_string
);
800 /* Destroys the container, returns the underlying string */
801 return g_string_free(str
, FALSE
);
804 /* This always returns NULL */
805 return g_string_free(str
, TRUE
);
809 const char *get_map_clock_name_value(struct bt_list_head
*head
)
812 struct ctf_node
*node
;
813 const char *name
= NULL
;
815 bt_list_for_each_entry(node
, head
, siblings
) {
817 int uexpr_type
= node
->u
.unary_expression
.type
;
818 int uexpr_link
= node
->u
.unary_expression
.link
;
819 int cond
= node
->type
!= NODE_UNARY_EXPRESSION
||
820 uexpr_type
!= UNARY_STRING
||
821 !((uexpr_link
!= UNARY_LINK_UNKNOWN
) ^ (i
== 0));
826 /* Needs to be chained with . */
827 switch (node
->u
.unary_expression
.link
) {
830 case UNARY_ARROWLINK
:
831 case UNARY_DOTDOTDOT
:
837 src_string
= node
->u
.unary_expression
.u
.string
;
841 if (strcmp("clock", src_string
)) {
849 if (strcmp("value", src_string
)) {
854 /* Extra identifier, unknown */
868 int is_unary_unsigned(struct bt_list_head
*head
)
871 struct ctf_node
*node
;
873 bt_list_for_each_entry(node
, head
, siblings
) {
874 if (node
->type
!= NODE_UNARY_EXPRESSION
) {
878 if (node
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
887 int get_unary_unsigned(struct bt_list_head
*head
, uint64_t *value
)
891 struct ctf_node
*node
;
895 if (bt_list_empty(head
)) {
900 bt_list_for_each_entry(node
, head
, siblings
) {
901 int uexpr_type
= node
->u
.unary_expression
.type
;
902 int uexpr_link
= node
->u
.unary_expression
.link
;
903 int cond
= node
->type
!= NODE_UNARY_EXPRESSION
||
904 uexpr_type
!= UNARY_UNSIGNED_CONSTANT
||
905 uexpr_link
!= UNARY_LINK_UNKNOWN
|| i
!= 0;
907 _BT_LOGE_NODE(node
, "Invalid constant unsigned integer.");
912 *value
= node
->u
.unary_expression
.u
.unsigned_constant
;
921 int is_unary_signed(struct bt_list_head
*head
)
924 struct ctf_node
*node
;
926 bt_list_for_each_entry(node
, head
, siblings
) {
927 if (node
->type
!= NODE_UNARY_EXPRESSION
) {
931 if (node
->u
.unary_expression
.type
!= UNARY_SIGNED_CONSTANT
) {
940 int get_unary_signed(struct bt_list_head
*head
, int64_t *value
)
944 struct ctf_node
*node
;
946 bt_list_for_each_entry(node
, head
, siblings
) {
947 int uexpr_type
= node
->u
.unary_expression
.type
;
948 int uexpr_link
= node
->u
.unary_expression
.link
;
949 int cond
= node
->type
!= NODE_UNARY_EXPRESSION
||
950 (uexpr_type
!= UNARY_UNSIGNED_CONSTANT
&&
951 uexpr_type
!= UNARY_SIGNED_CONSTANT
) ||
952 uexpr_link
!= UNARY_LINK_UNKNOWN
|| i
!= 0;
958 switch (uexpr_type
) {
959 case UNARY_UNSIGNED_CONSTANT
:
961 node
->u
.unary_expression
.u
.unsigned_constant
;
963 case UNARY_SIGNED_CONSTANT
:
964 *value
= node
->u
.unary_expression
.u
.signed_constant
;
979 int get_unary_uuid(struct bt_list_head
*head
, unsigned char *uuid
)
983 struct ctf_node
*node
;
985 bt_list_for_each_entry(node
, head
, siblings
) {
986 int uexpr_type
= node
->u
.unary_expression
.type
;
987 int uexpr_link
= node
->u
.unary_expression
.link
;
988 const char *src_string
;
990 if (node
->type
!= NODE_UNARY_EXPRESSION
||
991 uexpr_type
!= UNARY_STRING
||
992 uexpr_link
!= UNARY_LINK_UNKNOWN
||
998 src_string
= node
->u
.unary_expression
.u
.string
;
999 ret
= bt_uuid_parse(src_string
, uuid
);
1002 "Cannot parse UUID: uuid=\"%s\"", src_string
);
1012 int get_boolean(struct ctf_node
*unary_expr
)
1016 if (unary_expr
->type
!= NODE_UNARY_EXPRESSION
) {
1017 _BT_LOGE_NODE(unary_expr
,
1018 "Expecting unary expression: node-type=%d",
1024 switch (unary_expr
->u
.unary_expression
.type
) {
1025 case UNARY_UNSIGNED_CONSTANT
:
1026 ret
= (unary_expr
->u
.unary_expression
.u
.unsigned_constant
!= 0);
1028 case UNARY_SIGNED_CONSTANT
:
1029 ret
= (unary_expr
->u
.unary_expression
.u
.signed_constant
!= 0);
1033 const char *str
= unary_expr
->u
.unary_expression
.u
.string
;
1035 if (!strcmp(str
, "true") || !strcmp(str
, "TRUE")) {
1037 } else if (!strcmp(str
, "false") || !strcmp(str
, "FALSE")) {
1040 _BT_LOGE_NODE(unary_expr
,
1041 "Unexpected boolean value: value=\"%s\"", str
);
1048 _BT_LOGE_NODE(unary_expr
,
1049 "Unexpected unary expression type: node-type=%d",
1050 unary_expr
->u
.unary_expression
.type
);
1060 enum ctf_byte_order
byte_order_from_unary_expr(struct ctf_node
*unary_expr
)
1063 enum ctf_byte_order bo
= -1;
1065 if (unary_expr
->u
.unary_expression
.type
!= UNARY_STRING
) {
1066 _BT_LOGE_NODE(unary_expr
,
1067 "\"byte_order\" attribute: expecting `be`, `le`, `network`, or `native`.");
1071 str
= unary_expr
->u
.unary_expression
.u
.string
;
1073 if (!strcmp(str
, "be") || !strcmp(str
, "network")) {
1074 bo
= CTF_BYTE_ORDER_BIG
;
1075 } else if (!strcmp(str
, "le")) {
1076 bo
= CTF_BYTE_ORDER_LITTLE
;
1077 } else if (!strcmp(str
, "native")) {
1078 bo
= CTF_BYTE_ORDER_DEFAULT
;
1080 _BT_LOGE_NODE(unary_expr
,
1081 "Unexpected \"byte_order\" attribute value: "
1082 "expecting `be`, `le`, `network`, or `native`: value=\"%s\"",
1092 enum ctf_byte_order
get_real_byte_order(struct ctx
*ctx
,
1093 struct ctf_node
*uexpr
)
1095 enum ctf_byte_order bo
= byte_order_from_unary_expr(uexpr
);
1097 if (bo
== CTF_BYTE_ORDER_DEFAULT
) {
1098 bo
= ctx
->ctf_tc
->default_byte_order
;
1105 int is_align_valid(uint64_t align
)
1107 return (align
!= 0) && !(align
& (align
- UINT64_C(1)));
1111 int get_type_specifier_name(struct ctx
*ctx
, struct ctf_node
*type_specifier
,
1116 if (type_specifier
->type
!= NODE_TYPE_SPECIFIER
) {
1117 _BT_LOGE_NODE(type_specifier
,
1118 "Unexpected node type: node-type=%d",
1119 type_specifier
->type
);
1124 switch (type_specifier
->u
.type_specifier
.type
) {
1126 g_string_append(str
, "void");
1129 g_string_append(str
, "char");
1131 case TYPESPEC_SHORT
:
1132 g_string_append(str
, "short");
1135 g_string_append(str
, "int");
1138 g_string_append(str
, "long");
1140 case TYPESPEC_FLOAT
:
1141 g_string_append(str
, "float");
1143 case TYPESPEC_DOUBLE
:
1144 g_string_append(str
, "double");
1146 case TYPESPEC_SIGNED
:
1147 g_string_append(str
, "signed");
1149 case TYPESPEC_UNSIGNED
:
1150 g_string_append(str
, "unsigned");
1153 g_string_append(str
, "bool");
1155 case TYPESPEC_COMPLEX
:
1156 g_string_append(str
, "_Complex");
1158 case TYPESPEC_IMAGINARY
:
1159 g_string_append(str
, "_Imaginary");
1161 case TYPESPEC_CONST
:
1162 g_string_append(str
, "const");
1164 case TYPESPEC_ID_TYPE
:
1165 if (type_specifier
->u
.type_specifier
.id_type
) {
1166 g_string_append(str
,
1167 type_specifier
->u
.type_specifier
.id_type
);
1170 case TYPESPEC_STRUCT
:
1172 struct ctf_node
*node
= type_specifier
->u
.type_specifier
.node
;
1174 if (!node
->u
._struct
.name
) {
1175 _BT_LOGE_NODE(node
, "Unexpected empty structure field type name.");
1180 g_string_append(str
, "struct ");
1181 g_string_append(str
, node
->u
._struct
.name
);
1184 case TYPESPEC_VARIANT
:
1186 struct ctf_node
*node
= type_specifier
->u
.type_specifier
.node
;
1188 if (!node
->u
.variant
.name
) {
1189 _BT_LOGE_NODE(node
, "Unexpected empty variant field type name.");
1194 g_string_append(str
, "variant ");
1195 g_string_append(str
, node
->u
.variant
.name
);
1200 struct ctf_node
*node
= type_specifier
->u
.type_specifier
.node
;
1202 if (!node
->u
._enum
.enum_id
) {
1204 "Unexpected empty enumeration field type (`enum`) name.");
1209 g_string_append(str
, "enum ");
1210 g_string_append(str
, node
->u
._enum
.enum_id
);
1213 case TYPESPEC_FLOATING_POINT
:
1214 case TYPESPEC_INTEGER
:
1215 case TYPESPEC_STRING
:
1217 _BT_LOGE_NODE(type_specifier
->u
.type_specifier
.node
,
1218 "Unexpected type specifier type: %d",
1219 type_specifier
->u
.type_specifier
.type
);
1229 int get_type_specifier_list_name(struct ctx
*ctx
,
1230 struct ctf_node
*type_specifier_list
, GString
*str
)
1233 struct ctf_node
*iter
;
1234 int alias_item_nr
= 0;
1235 struct bt_list_head
*head
=
1236 &type_specifier_list
->u
.type_specifier_list
.head
;
1238 bt_list_for_each_entry(iter
, head
, siblings
) {
1239 if (alias_item_nr
!= 0) {
1240 g_string_append(str
, " ");
1244 ret
= get_type_specifier_name(ctx
, iter
, str
);
1255 GQuark
create_typealias_identifier(struct ctx
*ctx
,
1256 struct ctf_node
*type_specifier_list
,
1257 struct ctf_node
*node_type_declarator
)
1263 struct ctf_node
*iter
;
1264 struct bt_list_head
*pointers
=
1265 &node_type_declarator
->u
.type_declarator
.pointers
;
1267 str
= g_string_new("");
1268 ret
= get_type_specifier_list_name(ctx
, type_specifier_list
, str
);
1270 g_string_free(str
, TRUE
);
1274 bt_list_for_each_entry(iter
, pointers
, siblings
) {
1275 g_string_append(str
, " *");
1277 if (iter
->u
.pointer
.const_qualifier
) {
1278 g_string_append(str
, " const");
1282 str_c
= g_string_free(str
, FALSE
);
1283 qalias
= g_quark_from_string(str_c
);
1291 int visit_type_declarator(struct ctx
*ctx
, struct ctf_node
*type_specifier_list
,
1292 GQuark
*field_name
, struct ctf_node
*node_type_declarator
,
1293 struct ctf_field_type
**field_decl
,
1294 struct ctf_field_type
*nested_decl
)
1297 * During this whole function, nested_decl is always OURS,
1298 * whereas field_decl is an output which we create, but
1299 * belongs to the caller (it is moved).
1304 /* Validate type declarator node */
1305 if (node_type_declarator
) {
1306 if (node_type_declarator
->u
.type_declarator
.type
==
1308 _BT_LOGE_NODE(node_type_declarator
,
1309 "Unexpected type declarator type: type=%d",
1310 node_type_declarator
->u
.type_declarator
.type
);
1315 /* TODO: GCC bitfields not supported yet */
1316 if (node_type_declarator
->u
.type_declarator
.bitfield_len
!=
1318 _BT_LOGE_NODE(node_type_declarator
,
1319 "GCC bitfields are not supported as of this version.");
1325 /* Find the right nested declaration if not provided */
1327 struct bt_list_head
*pointers
=
1328 &node_type_declarator
->u
.type_declarator
.pointers
;
1330 if (node_type_declarator
&& !bt_list_empty(pointers
)) {
1334 * If we have a pointer declarator, it HAS to
1335 * be present in the typealiases (else fail).
1337 qalias
= create_typealias_identifier(ctx
,
1338 type_specifier_list
, node_type_declarator
);
1340 ctx_decl_scope_lookup_alias(ctx
->current_scope
,
1341 g_quark_to_string(qalias
), -1, true);
1343 _BT_LOGE_NODE(node_type_declarator
,
1344 "Cannot find type alias: name=\"%s\"",
1345 g_quark_to_string(qalias
));
1350 if (nested_decl
->id
== CTF_FIELD_TYPE_ID_INT
) {
1351 /* Pointer: force integer's base to 16 */
1352 struct ctf_field_type_int
*int_ft
=
1353 (void *) nested_decl
;
1356 BT_FIELD_TYPE_INTEGER_PREFERRED_DISPLAY_BASE_HEXADECIMAL
;
1359 ret
= visit_type_specifier_list(ctx
,
1360 type_specifier_list
, &nested_decl
);
1362 BT_ASSERT(!nested_decl
);
1368 BT_ASSERT(nested_decl
);
1370 if (!node_type_declarator
) {
1371 *field_decl
= nested_decl
;
1376 if (node_type_declarator
->u
.type_declarator
.type
== TYPEDEC_ID
) {
1377 if (node_type_declarator
->u
.type_declarator
.u
.id
) {
1379 node_type_declarator
->u
.type_declarator
.u
.id
;
1385 *field_name
= g_quark_from_string(id
);
1390 *field_decl
= nested_decl
;
1394 struct ctf_node
*first
;
1395 struct ctf_field_type
*decl
= NULL
;
1396 struct ctf_field_type
*outer_field_decl
= NULL
;
1397 struct bt_list_head
*length
=
1398 &node_type_declarator
->
1399 u
.type_declarator
.u
.nested
.length
;
1401 /* Create array/sequence, pass nested_decl as child */
1402 if (bt_list_empty(length
)) {
1403 _BT_LOGE_NODE(node_type_declarator
,
1404 "Expecting length field reference or value.");
1409 first
= _BT_LIST_FIRST_ENTRY(length
, struct ctf_node
, siblings
);
1410 if (first
->type
!= NODE_UNARY_EXPRESSION
) {
1411 _BT_LOGE_NODE(first
,
1412 "Unexpected node type: node-type=%d",
1418 switch (first
->u
.unary_expression
.type
) {
1419 case UNARY_UNSIGNED_CONSTANT
:
1421 struct ctf_field_type_array
*array_decl
= NULL
;
1423 array_decl
= ctf_field_type_array_create();
1424 BT_ASSERT(array_decl
);
1425 array_decl
->length
=
1426 first
->u
.unary_expression
.u
.unsigned_constant
;
1427 array_decl
->base
.elem_ft
= nested_decl
;
1429 decl
= (void *) array_decl
;
1434 /* Lookup unsigned integer definition, create seq. */
1435 struct ctf_field_type_sequence
*seq_decl
= NULL
;
1436 char *length_name
= concatenate_unary_strings(length
);
1439 _BT_LOGE_NODE(node_type_declarator
,
1440 "Cannot concatenate unary strings.");
1445 if (strncmp(length_name
, "env.", 4) == 0) {
1446 /* This is, in fact, an array */
1447 const char *env_entry_name
= &length_name
[4];
1448 struct ctf_trace_class_env_entry
*env_entry
=
1449 ctf_trace_class_borrow_env_entry_by_name(
1450 ctx
->ctf_tc
, env_entry_name
);
1451 struct ctf_field_type_array
*array_decl
;
1454 _BT_LOGE_NODE(node_type_declarator
,
1455 "Cannot find environment entry: "
1456 "name=\"%s\"", env_entry_name
);
1461 if (env_entry
->type
!= CTF_TRACE_CLASS_ENV_ENTRY_TYPE_INT
) {
1462 _BT_LOGE_NODE(node_type_declarator
,
1463 "Wrong environment entry type "
1464 "(expecting integer): "
1465 "name=\"%s\"", env_entry_name
);
1470 if (env_entry
->value
.i
< 0) {
1471 _BT_LOGE_NODE(node_type_declarator
,
1472 "Invalid, negative array length: "
1473 "env-entry-name=\"%s\", "
1476 env_entry
->value
.i
);
1481 array_decl
= ctf_field_type_array_create();
1482 BT_ASSERT(array_decl
);
1483 array_decl
->length
=
1484 (uint64_t) env_entry
->value
.i
;
1485 array_decl
->base
.elem_ft
= nested_decl
;
1487 decl
= (void *) array_decl
;
1489 char *length_name_no_underscore
=
1490 remove_underscores_from_field_ref(
1492 if (!length_name_no_underscore
) {
1494 * remove_underscores_from_field_ref()
1500 seq_decl
= ctf_field_type_sequence_create();
1501 BT_ASSERT(seq_decl
);
1502 seq_decl
->base
.elem_ft
= nested_decl
;
1504 g_string_assign(seq_decl
->length_ref
,
1505 length_name_no_underscore
);
1506 free(length_name_no_underscore
);
1507 decl
= (void *) seq_decl
;
1510 g_free(length_name
);
1518 BT_ASSERT(!nested_decl
);
1520 BT_ASSERT(!*field_decl
);
1523 * At this point, we found the next nested declaration.
1524 * We currently own this (and lost the ownership of
1525 * nested_decl in the meantime). Pass this next
1526 * nested declaration as the content of the outer
1527 * container, MOVING its ownership.
1529 ret
= visit_type_declarator(ctx
, type_specifier_list
,
1531 node_type_declarator
->
1532 u
.type_declarator
.u
.nested
.type_declarator
,
1533 &outer_field_decl
, decl
);
1536 BT_ASSERT(!outer_field_decl
);
1541 BT_ASSERT(outer_field_decl
);
1542 *field_decl
= outer_field_decl
;
1543 outer_field_decl
= NULL
;
1546 BT_ASSERT(*field_decl
);
1550 ctf_field_type_destroy(*field_decl
);
1558 ctf_field_type_destroy(nested_decl
);
1564 int visit_struct_decl_field(struct ctx
*ctx
,
1565 struct ctf_field_type_struct
*struct_decl
,
1566 struct ctf_node
*type_specifier_list
,
1567 struct bt_list_head
*type_declarators
)
1570 struct ctf_node
*iter
;
1571 struct ctf_field_type
*field_decl
= NULL
;
1573 bt_list_for_each_entry(iter
, type_declarators
, siblings
) {
1576 const char *field_name
;
1578 ret
= visit_type_declarator(ctx
, type_specifier_list
,
1579 &qfield_name
, iter
, &field_decl
, NULL
);
1581 BT_ASSERT(!field_decl
);
1582 _BT_LOGE_NODE(type_specifier_list
,
1583 "Cannot visit type declarator: ret=%d", ret
);
1587 BT_ASSERT(field_decl
);
1588 field_name
= g_quark_to_string(qfield_name
);
1590 /* Check if field with same name already exists */
1591 if (ctf_field_type_struct_borrow_member_by_name(
1592 struct_decl
, field_name
)) {
1593 _BT_LOGE_NODE(type_specifier_list
,
1594 "Duplicate field in structure field type: "
1595 "field-name=\"%s\"", field_name
);
1600 /* Add field to structure */
1601 ctf_field_type_struct_append_member(struct_decl
,
1602 field_name
, field_decl
);
1609 ctf_field_type_destroy(field_decl
);
1615 int visit_variant_decl_field(struct ctx
*ctx
,
1616 struct ctf_field_type_variant
*variant_decl
,
1617 struct ctf_node
*type_specifier_list
,
1618 struct bt_list_head
*type_declarators
)
1621 struct ctf_node
*iter
;
1622 struct ctf_field_type
*field_decl
= NULL
;
1624 bt_list_for_each_entry(iter
, type_declarators
, siblings
) {
1627 const char *field_name
;
1629 ret
= visit_type_declarator(ctx
, type_specifier_list
,
1630 &qfield_name
, iter
, &field_decl
, NULL
);
1632 BT_ASSERT(!field_decl
);
1633 _BT_LOGE_NODE(type_specifier_list
,
1634 "Cannot visit type declarator: ret=%d", ret
);
1638 BT_ASSERT(field_decl
);
1639 field_name
= g_quark_to_string(qfield_name
);
1641 /* Check if field with same name already exists */
1642 if (ctf_field_type_variant_borrow_option_by_name(
1643 variant_decl
, field_name
)) {
1644 _BT_LOGE_NODE(type_specifier_list
,
1645 "Duplicate field in variant field type: "
1646 "field-name=\"%s\"", field_name
);
1651 /* Add field to structure */
1652 ctf_field_type_variant_append_option(variant_decl
,
1653 field_name
, field_decl
);
1660 ctf_field_type_destroy(field_decl
);
1666 int visit_typedef(struct ctx
*ctx
, struct ctf_node
*type_specifier_list
,
1667 struct bt_list_head
*type_declarators
)
1671 struct ctf_node
*iter
;
1672 struct ctf_field_type
*type_decl
= NULL
;
1674 bt_list_for_each_entry(iter
, type_declarators
, siblings
) {
1675 ret
= visit_type_declarator(ctx
, type_specifier_list
,
1676 &qidentifier
, iter
, &type_decl
, NULL
);
1679 "Cannot visit type declarator: ret=%d", ret
);
1684 /* Do not allow typedef and typealias of untagged variants */
1685 if (type_decl
->id
== CTF_FIELD_TYPE_ID_VARIANT
) {
1686 struct ctf_field_type_variant
*var_ft
=
1689 if (var_ft
->tag_path
.path
->len
== 0) {
1691 "Type definition of untagged variant field type is not allowed.");
1697 ret
= ctx_decl_scope_register_alias(ctx
->current_scope
,
1698 g_quark_to_string(qidentifier
), type_decl
);
1701 "Cannot register type definition: name=\"%s\"",
1702 g_quark_to_string(qidentifier
));
1708 ctf_field_type_destroy(type_decl
);
1714 int visit_typealias(struct ctx
*ctx
, struct ctf_node
*target
,
1715 struct ctf_node
*alias
)
1719 struct ctf_node
*node
;
1720 GQuark qdummy_field_name
;
1721 struct ctf_field_type
*type_decl
= NULL
;
1723 /* Create target type declaration */
1724 if (bt_list_empty(&target
->u
.typealias_target
.type_declarators
)) {
1727 node
= _BT_LIST_FIRST_ENTRY(
1728 &target
->u
.typealias_target
.type_declarators
,
1729 struct ctf_node
, siblings
);
1732 ret
= visit_type_declarator(ctx
,
1733 target
->u
.typealias_target
.type_specifier_list
,
1734 &qdummy_field_name
, node
, &type_decl
, NULL
);
1736 BT_ASSERT(!type_decl
);
1738 "Cannot visit type declarator: ret=%d", ret
);
1742 /* Do not allow typedef and typealias of untagged variants */
1743 if (type_decl
->id
== CTF_FIELD_TYPE_ID_VARIANT
) {
1744 struct ctf_field_type_variant
*var_ft
= (void *) type_decl
;
1746 if (var_ft
->tag_path
.path
->len
== 0) {
1747 _BT_LOGE_NODE(target
,
1748 "Type definition of untagged variant field type is not allowed.");
1755 * The semantic validator does not check whether the target is
1756 * abstract or not (if it has an identifier). Check it here.
1758 if (qdummy_field_name
!= 0) {
1759 _BT_LOGE_NODE(target
,
1760 "Expecting empty identifier: id=\"%s\"",
1761 g_quark_to_string(qdummy_field_name
));
1766 /* Create alias identifier */
1767 node
= _BT_LIST_FIRST_ENTRY(&alias
->u
.typealias_alias
.type_declarators
,
1768 struct ctf_node
, siblings
);
1769 qalias
= create_typealias_identifier(ctx
,
1770 alias
->u
.typealias_alias
.type_specifier_list
, node
);
1771 ret
= ctx_decl_scope_register_alias(ctx
->current_scope
,
1772 g_quark_to_string(qalias
), type_decl
);
1775 "Cannot register type alias: name=\"%s\"",
1776 g_quark_to_string(qalias
));
1781 ctf_field_type_destroy(type_decl
);
1787 int visit_struct_decl_entry(struct ctx
*ctx
, struct ctf_node
*entry_node
,
1788 struct ctf_field_type_struct
*struct_decl
)
1792 switch (entry_node
->type
) {
1794 ret
= visit_typedef(ctx
,
1795 entry_node
->u
._typedef
.type_specifier_list
,
1796 &entry_node
->u
._typedef
.type_declarators
);
1798 _BT_LOGE_NODE(entry_node
,
1799 "Cannot add type definition found in structure field type: ret=%d",
1804 case NODE_TYPEALIAS
:
1805 ret
= visit_typealias(ctx
, entry_node
->u
.typealias
.target
,
1806 entry_node
->u
.typealias
.alias
);
1808 _BT_LOGE_NODE(entry_node
,
1809 "Cannot add type alias found in structure field type: ret=%d",
1814 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
1816 ret
= visit_struct_decl_field(ctx
, struct_decl
,
1817 entry_node
->u
.struct_or_variant_declaration
.
1818 type_specifier_list
,
1819 &entry_node
->u
.struct_or_variant_declaration
.
1826 _BT_LOGE_NODE(entry_node
,
1827 "Unexpected node type: node-type=%d", entry_node
->type
);
1837 int visit_variant_decl_entry(struct ctx
*ctx
, struct ctf_node
*entry_node
,
1838 struct ctf_field_type_variant
*variant_decl
)
1842 switch (entry_node
->type
) {
1844 ret
= visit_typedef(ctx
,
1845 entry_node
->u
._typedef
.type_specifier_list
,
1846 &entry_node
->u
._typedef
.type_declarators
);
1848 _BT_LOGE_NODE(entry_node
,
1849 "Cannot add type definition found in variant field type: ret=%d",
1854 case NODE_TYPEALIAS
:
1855 ret
= visit_typealias(ctx
, entry_node
->u
.typealias
.target
,
1856 entry_node
->u
.typealias
.alias
);
1858 _BT_LOGE_NODE(entry_node
,
1859 "Cannot add type alias found in variant field type: ret=%d",
1864 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
1866 ret
= visit_variant_decl_field(ctx
, variant_decl
,
1867 entry_node
->u
.struct_or_variant_declaration
.
1868 type_specifier_list
,
1869 &entry_node
->u
.struct_or_variant_declaration
.
1876 _BT_LOGE_NODE(entry_node
,
1877 "Unexpected node type: node-type=%d",
1888 int visit_struct_decl(struct ctx
*ctx
, const char *name
,
1889 struct bt_list_head
*decl_list
, int has_body
,
1890 struct bt_list_head
*min_align
,
1891 struct ctf_field_type_struct
**struct_decl
)
1895 BT_ASSERT(struct_decl
);
1896 *struct_decl
= NULL
;
1898 /* For named struct (without body), lookup in declaration scope */
1901 BT_LOGE_STR("Bodyless structure field type: missing name.");
1906 *struct_decl
= ctx_decl_scope_lookup_struct(ctx
->current_scope
,
1908 if (!*struct_decl
) {
1909 BT_LOGE("Cannot find structure field type: name=\"struct %s\"",
1915 struct ctf_node
*entry_node
;
1916 uint64_t min_align_value
= 0;
1919 if (ctx_decl_scope_lookup_struct(
1920 ctx
->current_scope
, name
, 1, false)) {
1921 BT_LOGE("Structure field type already declared in local scope: "
1922 "name=\"struct %s\"", name
);
1928 if (!bt_list_empty(min_align
)) {
1929 ret
= get_unary_unsigned(min_align
, &min_align_value
);
1931 BT_LOGE("Unexpected unary expression for structure field type's `align` attribute: "
1937 *struct_decl
= ctf_field_type_struct_create();
1938 BT_ASSERT(*struct_decl
);
1940 if (min_align_value
!= 0) {
1941 (*struct_decl
)->base
.alignment
= min_align_value
;
1944 _TRY_PUSH_SCOPE_OR_GOTO_ERROR();
1946 bt_list_for_each_entry(entry_node
, decl_list
, siblings
) {
1947 ret
= visit_struct_decl_entry(ctx
, entry_node
,
1950 _BT_LOGE_NODE(entry_node
,
1951 "Cannot visit structure field type entry: "
1961 ret
= ctx_decl_scope_register_struct(ctx
->current_scope
,
1962 name
, *struct_decl
);
1964 BT_LOGE("Cannot register structure field type in declaration scope: "
1965 "name=\"struct %s\", ret=%d", name
, ret
);
1974 ctf_field_type_destroy((void *) *struct_decl
);
1975 *struct_decl
= NULL
;
1980 int visit_variant_decl(struct ctx
*ctx
, const char *name
,
1981 const char *tag
, struct bt_list_head
*decl_list
,
1982 int has_body
, struct ctf_field_type_variant
**variant_decl
)
1985 struct ctf_field_type_variant
*untagged_variant_decl
= NULL
;
1987 BT_ASSERT(variant_decl
);
1988 *variant_decl
= NULL
;
1990 /* For named variant (without body), lookup in declaration scope */
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 struct ctf_node
*entry_node
;
2011 if (ctx_decl_scope_lookup_variant(ctx
->current_scope
,
2013 BT_LOGE("Variant field type already declared in local scope: "
2014 "name=\"variant %s\"", name
);
2020 untagged_variant_decl
= ctf_field_type_variant_create();
2021 BT_ASSERT(untagged_variant_decl
);
2022 _TRY_PUSH_SCOPE_OR_GOTO_ERROR();
2024 bt_list_for_each_entry(entry_node
, decl_list
, siblings
) {
2025 ret
= visit_variant_decl_entry(ctx
, entry_node
,
2026 untagged_variant_decl
);
2028 _BT_LOGE_NODE(entry_node
,
2029 "Cannot visit variant field type entry: "
2039 ret
= ctx_decl_scope_register_variant(
2040 ctx
->current_scope
, name
,
2041 untagged_variant_decl
);
2043 BT_LOGE("Cannot register variant field type in declaration scope: "
2044 "name=\"variant %s\", ret=%d", name
, ret
);
2051 * If tagged, create tagged variant and return; otherwise
2052 * return untagged variant.
2055 *variant_decl
= untagged_variant_decl
;
2056 untagged_variant_decl
= NULL
;
2059 * At this point, we have a fresh untagged variant; nobody
2060 * else owns it. Set its tag now.
2062 char *tag_no_underscore
=
2063 remove_underscores_from_field_ref(tag
);
2065 if (!tag_no_underscore
) {
2066 /* remove_underscores_from_field_ref() logs errors */
2070 g_string_assign(untagged_variant_decl
->tag_ref
,
2072 free(tag_no_underscore
);
2073 *variant_decl
= untagged_variant_decl
;
2074 untagged_variant_decl
= NULL
;
2077 BT_ASSERT(!untagged_variant_decl
);
2078 BT_ASSERT(*variant_decl
);
2082 ctf_field_type_destroy((void *) untagged_variant_decl
);
2083 untagged_variant_decl
= NULL
;
2084 ctf_field_type_destroy((void *) *variant_decl
);
2085 *variant_decl
= NULL
;
2098 int visit_enum_decl_entry(struct ctx
*ctx
, struct ctf_node
*enumerator
,
2099 struct ctf_field_type_enum
*enum_decl
, struct uori
*last
)
2103 struct ctf_node
*iter
;
2104 struct uori start
= {
2112 const char *label
= enumerator
->u
.enumerator
.id
;
2113 const char *effective_label
= label
;
2114 struct bt_list_head
*values
= &enumerator
->u
.enumerator
.values
;
2116 bt_list_for_each_entry(iter
, values
, siblings
) {
2117 struct uori
*target
;
2119 if (iter
->type
!= NODE_UNARY_EXPRESSION
) {
2121 "Wrong expression for enumeration field type label: "
2122 "node-type=%d, label=\"%s\"", iter
->type
,
2134 switch (iter
->u
.unary_expression
.type
) {
2135 case UNARY_SIGNED_CONSTANT
:
2136 target
->is_signed
= true;
2138 iter
->u
.unary_expression
.u
.signed_constant
;
2140 case UNARY_UNSIGNED_CONSTANT
:
2141 target
->is_signed
= false;
2143 iter
->u
.unary_expression
.u
.unsigned_constant
;
2147 "Invalid enumeration field type entry: "
2148 "expecting constant signed or unsigned integer: "
2149 "node-type=%d, label=\"%s\"",
2150 iter
->u
.unary_expression
.type
, label
);
2157 "Invalid enumeration field type entry: label=\"%s\"",
2174 if (end
.is_signed
) {
2175 last
->value
.i
= end
.value
.i
+ 1;
2177 last
->value
.u
= end
.value
.u
+ 1;
2180 if (label
[0] == '_') {
2182 * Strip the first underscore of any enumeration field
2183 * type's label in case this enumeration FT is used as
2184 * a variant FT tag later. The variant FT choice names
2185 * could also start with `_`, in which case the prefix
2186 * is removed, and it the resulting choice name needs to
2189 effective_label
= &label
[1];
2192 ctf_field_type_enum_append_mapping(enum_decl
, effective_label
,
2193 start
.value
.u
, end
.value
.u
);
2201 int visit_enum_decl(struct ctx
*ctx
, const char *name
,
2202 struct ctf_node
*container_type
,
2203 struct bt_list_head
*enumerator_list
,
2204 int has_body
, struct ctf_field_type_enum
**enum_decl
)
2208 struct ctf_field_type_int
*integer_decl
= NULL
;
2210 BT_ASSERT(enum_decl
);
2213 /* For named enum (without body), lookup in declaration scope */
2216 BT_LOGE_STR("Bodyless enumeration field type: missing name.");
2221 *enum_decl
= ctx_decl_scope_lookup_enum(ctx
->current_scope
,
2224 BT_LOGE("Cannot find enumeration field type: "
2225 "name=\"enum %s\"", name
);
2230 struct ctf_node
*iter
;
2231 struct uori last_value
= {
2237 if (ctx_decl_scope_lookup_enum(ctx
->current_scope
,
2239 BT_LOGE("Enumeration field type already declared in local scope: "
2240 "name=\"enum %s\"", name
);
2246 if (!container_type
) {
2247 integer_decl
= (void *) ctx_decl_scope_lookup_alias(
2248 ctx
->current_scope
, "int", -1, true);
2249 if (!integer_decl
) {
2250 BT_LOGE_STR("Cannot find implicit `int` field type alias for enumeration field type.");
2255 ret
= visit_type_declarator(ctx
, container_type
,
2256 &qdummy_id
, NULL
, (void *) &integer_decl
,
2259 BT_ASSERT(!integer_decl
);
2265 BT_ASSERT(integer_decl
);
2267 if (integer_decl
->base
.base
.id
!= CTF_FIELD_TYPE_ID_INT
) {
2268 BT_LOGE("Container field type for enumeration field type is not an integer field type: "
2269 "ft-id=%d", integer_decl
->base
.base
.id
);
2274 *enum_decl
= ctf_field_type_enum_create();
2275 BT_ASSERT(*enum_decl
);
2276 (*enum_decl
)->base
.base
.base
.alignment
=
2277 integer_decl
->base
.base
.alignment
;
2278 ctf_field_type_int_copy_content((void *) *enum_decl
,
2279 (void *) integer_decl
);
2280 last_value
.is_signed
= (*enum_decl
)->base
.is_signed
;
2282 bt_list_for_each_entry(iter
, enumerator_list
, siblings
) {
2283 ret
= visit_enum_decl_entry(ctx
, iter
, *enum_decl
,
2287 "Cannot visit enumeration field type entry: "
2294 ret
= ctx_decl_scope_register_enum(ctx
->current_scope
,
2297 BT_LOGE("Cannot register enumeration field type in declaration scope: "
2307 ctf_field_type_destroy((void *) *enum_decl
);
2311 ctf_field_type_destroy((void *) integer_decl
);
2312 integer_decl
= NULL
;
2317 int visit_type_specifier(struct ctx
*ctx
,
2318 struct ctf_node
*type_specifier_list
,
2319 struct ctf_field_type
**decl
)
2322 GString
*str
= NULL
;
2325 str
= g_string_new("");
2326 ret
= get_type_specifier_list_name(ctx
, type_specifier_list
, str
);
2328 _BT_LOGE_NODE(type_specifier_list
,
2329 "Cannot get type specifier list's name: ret=%d", ret
);
2333 *decl
= ctx_decl_scope_lookup_alias(ctx
->current_scope
, str
->str
, -1,
2336 _BT_LOGE_NODE(type_specifier_list
,
2337 "Cannot find type alias: name=\"%s\"", str
->str
);
2345 ctf_field_type_destroy(*decl
);
2350 g_string_free(str
, TRUE
);
2357 int visit_integer_decl(struct ctx
*ctx
,
2358 struct bt_list_head
*expressions
,
2359 struct ctf_field_type_int
**integer_decl
)
2364 struct ctf_node
*expression
;
2365 uint64_t alignment
= 0, size
= 0;
2366 struct bt_clock_class
*mapped_clock_class
= NULL
;
2367 enum ctf_encoding encoding
= CTF_ENCODING_NONE
;
2368 enum bt_field_type_integer_preferred_display_base base
=
2369 BT_FIELD_TYPE_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL
;
2370 enum ctf_byte_order byte_order
= ctx
->ctf_tc
->default_byte_order
;
2372 *integer_decl
= NULL
;
2374 bt_list_for_each_entry(expression
, expressions
, siblings
) {
2375 struct ctf_node
*left
, *right
;
2377 left
= _BT_LIST_FIRST_ENTRY(&expression
->u
.ctf_expression
.left
,
2378 struct ctf_node
, siblings
);
2379 right
= _BT_LIST_FIRST_ENTRY(
2380 &expression
->u
.ctf_expression
.right
, struct ctf_node
,
2383 if (left
->u
.unary_expression
.type
!= UNARY_STRING
) {
2385 "Unexpected unary expression type: type=%d",
2386 left
->u
.unary_expression
.type
);
2391 if (!strcmp(left
->u
.unary_expression
.u
.string
, "signed")) {
2392 if (_IS_SET(&set
, _INTEGER_SIGNED_SET
)) {
2393 _BT_LOGE_DUP_ATTR(left
, "signed",
2394 "integer field type");
2399 signedness
= get_boolean(right
);
2400 if (signedness
< 0) {
2401 _BT_LOGE_NODE(right
,
2402 "Invalid boolean value for integer field type's `signed` attribute: "
2408 _SET(&set
, _INTEGER_SIGNED_SET
);
2409 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2411 if (_IS_SET(&set
, _INTEGER_BYTE_ORDER_SET
)) {
2412 _BT_LOGE_DUP_ATTR(left
, "byte_order",
2413 "integer field type");
2418 byte_order
= get_real_byte_order(ctx
, right
);
2419 if (byte_order
== -1) {
2420 _BT_LOGE_NODE(right
,
2421 "Invalid `byte_order` attribute in integer field type: "
2427 _SET(&set
, _INTEGER_BYTE_ORDER_SET
);
2428 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "size")) {
2429 if (_IS_SET(&set
, _INTEGER_SIZE_SET
)) {
2430 _BT_LOGE_DUP_ATTR(left
, "size",
2431 "integer field type");
2436 if (right
->u
.unary_expression
.type
!=
2437 UNARY_UNSIGNED_CONSTANT
) {
2438 _BT_LOGE_NODE(right
,
2439 "Invalid `size` attribute in integer field type: "
2440 "expecting unsigned constant integer: "
2442 right
->u
.unary_expression
.type
);
2447 size
= right
->u
.unary_expression
.u
.unsigned_constant
;
2449 _BT_LOGE_NODE(right
,
2450 "Invalid `size` attribute in integer field type: "
2451 "expecting positive constant integer: "
2452 "size=%" PRIu64
, size
);
2455 } else if (size
> 64) {
2456 _BT_LOGE_NODE(right
,
2457 "Invalid `size` attribute in integer field type: "
2458 "integer fields over 64 bits are not supported as of this version: "
2459 "size=%" PRIu64
, size
);
2464 _SET(&set
, _INTEGER_SIZE_SET
);
2465 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2467 if (_IS_SET(&set
, _INTEGER_ALIGN_SET
)) {
2468 _BT_LOGE_DUP_ATTR(left
, "align",
2469 "integer field type");
2474 if (right
->u
.unary_expression
.type
!=
2475 UNARY_UNSIGNED_CONSTANT
) {
2476 _BT_LOGE_NODE(right
,
2477 "Invalid `align` attribute in integer field type: "
2478 "expecting unsigned constant integer: "
2480 right
->u
.unary_expression
.type
);
2486 right
->u
.unary_expression
.u
.unsigned_constant
;
2487 if (!is_align_valid(alignment
)) {
2488 _BT_LOGE_NODE(right
,
2489 "Invalid `align` attribute in integer field type: "
2490 "expecting power of two: "
2491 "align=%" PRIu64
, alignment
);
2496 _SET(&set
, _INTEGER_ALIGN_SET
);
2497 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "base")) {
2498 if (_IS_SET(&set
, _INTEGER_BASE_SET
)) {
2499 _BT_LOGE_DUP_ATTR(left
, "base",
2500 "integer field type");
2505 switch (right
->u
.unary_expression
.type
) {
2506 case UNARY_UNSIGNED_CONSTANT
:
2508 uint64_t constant
= right
->u
.unary_expression
.
2509 u
.unsigned_constant
;
2513 base
= BT_FIELD_TYPE_INTEGER_PREFERRED_DISPLAY_BASE_BINARY
;
2516 base
= BT_FIELD_TYPE_INTEGER_PREFERRED_DISPLAY_BASE_OCTAL
;
2519 base
= BT_FIELD_TYPE_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL
;
2522 base
= BT_FIELD_TYPE_INTEGER_PREFERRED_DISPLAY_BASE_HEXADECIMAL
;
2525 _BT_LOGE_NODE(right
,
2526 "Invalid `base` attribute in integer field type: "
2528 right
->u
.unary_expression
.u
.unsigned_constant
);
2536 char *s_right
= concatenate_unary_strings(
2537 &expression
->u
.ctf_expression
.right
);
2539 _BT_LOGE_NODE(right
,
2540 "Unexpected unary expression for integer field type's `base` attribute.");
2545 if (!strcmp(s_right
, "decimal") ||
2546 !strcmp(s_right
, "dec") ||
2547 !strcmp(s_right
, "d") ||
2548 !strcmp(s_right
, "i") ||
2549 !strcmp(s_right
, "u")) {
2550 base
= BT_FIELD_TYPE_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL
;
2551 } else if (!strcmp(s_right
, "hexadecimal") ||
2552 !strcmp(s_right
, "hex") ||
2553 !strcmp(s_right
, "x") ||
2554 !strcmp(s_right
, "X") ||
2555 !strcmp(s_right
, "p")) {
2556 base
= BT_FIELD_TYPE_INTEGER_PREFERRED_DISPLAY_BASE_HEXADECIMAL
;
2557 } else if (!strcmp(s_right
, "octal") ||
2558 !strcmp(s_right
, "oct") ||
2559 !strcmp(s_right
, "o")) {
2560 base
= BT_FIELD_TYPE_INTEGER_PREFERRED_DISPLAY_BASE_OCTAL
;
2561 } else if (!strcmp(s_right
, "binary") ||
2562 !strcmp(s_right
, "b")) {
2563 base
= BT_FIELD_TYPE_INTEGER_PREFERRED_DISPLAY_BASE_BINARY
;
2565 _BT_LOGE_NODE(right
,
2566 "Unexpected unary expression for integer field type's `base` attribute: "
2567 "base=\"%s\"", s_right
);
2577 _BT_LOGE_NODE(right
,
2578 "Invalid `base` attribute in integer field type: "
2579 "expecting unsigned constant integer or unary string.");
2584 _SET(&set
, _INTEGER_BASE_SET
);
2585 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2589 if (_IS_SET(&set
, _INTEGER_ENCODING_SET
)) {
2590 _BT_LOGE_DUP_ATTR(left
, "encoding",
2591 "integer field type");
2596 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
2597 _BT_LOGE_NODE(right
,
2598 "Invalid `encoding` attribute in integer field type: "
2599 "expecting unary string.");
2604 s_right
= concatenate_unary_strings(
2605 &expression
->u
.ctf_expression
.right
);
2607 _BT_LOGE_NODE(right
,
2608 "Unexpected unary expression for integer field type's `encoding` attribute.");
2613 if (!strcmp(s_right
, "UTF8") ||
2614 !strcmp(s_right
, "utf8") ||
2615 !strcmp(s_right
, "utf-8") ||
2616 !strcmp(s_right
, "UTF-8") ||
2617 !strcmp(s_right
, "ASCII") ||
2618 !strcmp(s_right
, "ascii")) {
2619 encoding
= CTF_ENCODING_UTF8
;
2620 } else if (!strcmp(s_right
, "none")) {
2621 encoding
= CTF_ENCODING_NONE
;
2623 _BT_LOGE_NODE(right
,
2624 "Invalid `encoding` attribute in integer field type: "
2625 "unknown encoding: encoding=\"%s\"",
2633 _SET(&set
, _INTEGER_ENCODING_SET
);
2634 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "map")) {
2635 const char *clock_name
;
2637 if (_IS_SET(&set
, _INTEGER_MAP_SET
)) {
2638 _BT_LOGE_DUP_ATTR(left
, "map",
2639 "integer field type");
2644 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
2645 _BT_LOGE_NODE(right
,
2646 "Invalid `map` attribute in integer field type: "
2647 "expecting unary string.");
2653 get_map_clock_name_value(
2654 &expression
->u
.ctf_expression
.right
);
2656 char *s_right
= concatenate_unary_strings(
2657 &expression
->u
.ctf_expression
.right
);
2660 _BT_LOGE_NODE(right
,
2661 "Unexpected unary expression for integer field type's `map` attribute.");
2666 _BT_LOGE_NODE(right
,
2667 "Invalid `map` attribute in integer field type: "
2668 "cannot find clock class at this point: name=\"%s\"",
2670 _SET(&set
, _INTEGER_MAP_SET
);
2675 mapped_clock_class
=
2676 ctf_trace_class_borrow_clock_class_by_name(
2677 ctx
->ctf_tc
, clock_name
);
2678 if (!mapped_clock_class
) {
2679 _BT_LOGE_NODE(right
,
2680 "Invalid `map` attribute in integer field type: "
2681 "cannot find clock class at this point: name=\"%s\"",
2687 _SET(&set
, _INTEGER_MAP_SET
);
2690 "Unknown attribute in integer field type: "
2692 left
->u
.unary_expression
.u
.string
);
2696 if (!_IS_SET(&set
, _INTEGER_SIZE_SET
)) {
2697 BT_LOGE_STR("Missing `size` attribute in integer field type.");
2702 if (!_IS_SET(&set
, _INTEGER_ALIGN_SET
)) {
2703 if (size
% CHAR_BIT
) {
2704 /* Bit-packed alignment */
2707 /* Byte-packed alignment */
2708 alignment
= CHAR_BIT
;
2712 *integer_decl
= ctf_field_type_int_create();
2713 BT_ASSERT(*integer_decl
);
2714 (*integer_decl
)->base
.base
.alignment
= alignment
;
2715 (*integer_decl
)->base
.byte_order
= byte_order
;
2716 (*integer_decl
)->base
.size
= size
;
2717 (*integer_decl
)->is_signed
= (signedness
> 0);
2718 (*integer_decl
)->disp_base
= base
;
2719 (*integer_decl
)->encoding
= encoding
;
2720 (*integer_decl
)->mapped_clock_class
= bt_get(mapped_clock_class
);
2724 ctf_field_type_destroy((void *) *integer_decl
);
2725 *integer_decl
= NULL
;
2730 int visit_floating_point_number_decl(struct ctx
*ctx
,
2731 struct bt_list_head
*expressions
,
2732 struct ctf_field_type_float
**float_decl
)
2736 struct ctf_node
*expression
;
2737 uint64_t alignment
= 1, exp_dig
= 0, mant_dig
= 0;
2738 enum ctf_byte_order byte_order
= ctx
->ctf_tc
->default_byte_order
;
2742 bt_list_for_each_entry(expression
, expressions
, siblings
) {
2743 struct ctf_node
*left
, *right
;
2745 left
= _BT_LIST_FIRST_ENTRY(&expression
->u
.ctf_expression
.left
,
2746 struct ctf_node
, siblings
);
2747 right
= _BT_LIST_FIRST_ENTRY(
2748 &expression
->u
.ctf_expression
.right
, struct ctf_node
,
2751 if (left
->u
.unary_expression
.type
!= UNARY_STRING
) {
2753 "Unexpected unary expression type: type=%d",
2754 left
->u
.unary_expression
.type
);
2759 if (!strcmp(left
->u
.unary_expression
.u
.string
, "byte_order")) {
2760 if (_IS_SET(&set
, _FLOAT_BYTE_ORDER_SET
)) {
2761 _BT_LOGE_DUP_ATTR(left
, "byte_order",
2762 "floating point number field type");
2767 byte_order
= get_real_byte_order(ctx
, right
);
2768 if (byte_order
== -1) {
2769 _BT_LOGE_NODE(right
,
2770 "Invalid `byte_order` attribute in floating point number field type: "
2776 _SET(&set
, _FLOAT_BYTE_ORDER_SET
);
2777 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2779 if (_IS_SET(&set
, _FLOAT_EXP_DIG_SET
)) {
2780 _BT_LOGE_DUP_ATTR(left
, "exp_dig",
2781 "floating point number field type");
2786 if (right
->u
.unary_expression
.type
!=
2787 UNARY_UNSIGNED_CONSTANT
) {
2788 _BT_LOGE_NODE(right
,
2789 "Invalid `exp_dig` attribute in floating point number field type: "
2790 "expecting unsigned constant integer: "
2792 right
->u
.unary_expression
.type
);
2797 exp_dig
= right
->u
.unary_expression
.u
.unsigned_constant
;
2798 _SET(&set
, _FLOAT_EXP_DIG_SET
);
2799 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2801 if (_IS_SET(&set
, _FLOAT_MANT_DIG_SET
)) {
2802 _BT_LOGE_DUP_ATTR(left
, "mant_dig",
2803 "floating point number field type");
2808 if (right
->u
.unary_expression
.type
!=
2809 UNARY_UNSIGNED_CONSTANT
) {
2810 _BT_LOGE_NODE(right
,
2811 "Invalid `mant_dig` attribute in floating point number field type: "
2812 "expecting unsigned constant integer: "
2814 right
->u
.unary_expression
.type
);
2819 mant_dig
= right
->u
.unary_expression
.u
.
2821 _SET(&set
, _FLOAT_MANT_DIG_SET
);
2822 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2824 if (_IS_SET(&set
, _FLOAT_ALIGN_SET
)) {
2825 _BT_LOGE_DUP_ATTR(left
, "align",
2826 "floating point number field type");
2831 if (right
->u
.unary_expression
.type
!=
2832 UNARY_UNSIGNED_CONSTANT
) {
2833 _BT_LOGE_NODE(right
,
2834 "Invalid `align` attribute in floating point number field type: "
2835 "expecting unsigned constant integer: "
2837 right
->u
.unary_expression
.type
);
2842 alignment
= right
->u
.unary_expression
.u
.
2845 if (!is_align_valid(alignment
)) {
2846 _BT_LOGE_NODE(right
,
2847 "Invalid `align` attribute in floating point number field type: "
2848 "expecting power of two: "
2849 "align=%" PRIu64
, alignment
);
2854 _SET(&set
, _FLOAT_ALIGN_SET
);
2857 "Unknown attribute in floating point number field type: "
2859 left
->u
.unary_expression
.u
.string
);
2863 if (!_IS_SET(&set
, _FLOAT_MANT_DIG_SET
)) {
2864 BT_LOGE_STR("Missing `mant_dig` attribute in floating point number field type.");
2869 if (!_IS_SET(&set
, _FLOAT_EXP_DIG_SET
)) {
2870 BT_LOGE_STR("Missing `exp_dig` attribute in floating point number field type.");
2875 if (mant_dig
!= 24 && mant_dig
!= 53) {
2876 BT_LOGE_STR("`mant_dig` attribute: expecting 24 or 53.");
2881 if (mant_dig
== 24 && exp_dig
!= 8) {
2882 BT_LOGE_STR("`exp_dig` attribute: expecting 8 because `mant_dig` is 24.");
2887 if (mant_dig
== 53 && exp_dig
!= 11) {
2888 BT_LOGE_STR("`exp_dig` attribute: expecting 11 because `mant_dig` is 53.");
2893 if (!_IS_SET(&set
, _INTEGER_ALIGN_SET
)) {
2894 if ((mant_dig
+ exp_dig
) % CHAR_BIT
) {
2895 /* Bit-packed alignment */
2898 /* Byte-packed alignment */
2899 alignment
= CHAR_BIT
;
2903 *float_decl
= ctf_field_type_float_create();
2904 BT_ASSERT(*float_decl
);
2905 (*float_decl
)->base
.base
.alignment
= alignment
;
2906 (*float_decl
)->base
.byte_order
= byte_order
;
2907 (*float_decl
)->base
.size
= mant_dig
+ exp_dig
;
2911 ctf_field_type_destroy((void *) *float_decl
);
2917 int visit_string_decl(struct ctx
*ctx
,
2918 struct bt_list_head
*expressions
,
2919 struct ctf_field_type_string
**string_decl
)
2923 struct ctf_node
*expression
;
2924 enum ctf_encoding encoding
= CTF_ENCODING_UTF8
;
2926 *string_decl
= NULL
;
2928 bt_list_for_each_entry(expression
, expressions
, siblings
) {
2929 struct ctf_node
*left
, *right
;
2931 left
= _BT_LIST_FIRST_ENTRY(&expression
->u
.ctf_expression
.left
,
2932 struct ctf_node
, siblings
);
2933 right
= _BT_LIST_FIRST_ENTRY(
2934 &expression
->u
.ctf_expression
.right
, struct ctf_node
,
2937 if (left
->u
.unary_expression
.type
!= UNARY_STRING
) {
2939 "Unexpected unary expression type: type=%d",
2940 left
->u
.unary_expression
.type
);
2945 if (!strcmp(left
->u
.unary_expression
.u
.string
, "encoding")) {
2948 if (_IS_SET(&set
, _STRING_ENCODING_SET
)) {
2949 _BT_LOGE_DUP_ATTR(left
, "encoding",
2950 "string field type");
2955 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
2956 _BT_LOGE_NODE(right
,
2957 "Invalid `encoding` attribute in string field type: "
2958 "expecting unary string.");
2963 s_right
= concatenate_unary_strings(
2964 &expression
->u
.ctf_expression
.right
);
2966 _BT_LOGE_NODE(right
,
2967 "Unexpected unary expression for string field type's `encoding` attribute.");
2972 if (!strcmp(s_right
, "UTF8") ||
2973 !strcmp(s_right
, "utf8") ||
2974 !strcmp(s_right
, "utf-8") ||
2975 !strcmp(s_right
, "UTF-8") ||
2976 !strcmp(s_right
, "ASCII") ||
2977 !strcmp(s_right
, "ascii")) {
2978 encoding
= CTF_ENCODING_UTF8
;
2979 } else if (!strcmp(s_right
, "none")) {
2980 encoding
= CTF_ENCODING_NONE
;
2982 _BT_LOGE_NODE(right
,
2983 "Invalid `encoding` attribute in string field type: "
2984 "unknown encoding: encoding=\"%s\"",
2992 _SET(&set
, _STRING_ENCODING_SET
);
2995 "Unknown attribute in string field type: "
2997 left
->u
.unary_expression
.u
.string
);
3001 *string_decl
= ctf_field_type_string_create();
3002 BT_ASSERT(*string_decl
);
3003 (*string_decl
)->encoding
= encoding
;
3007 ctf_field_type_destroy((void *) *string_decl
);
3008 *string_decl
= NULL
;
3013 int visit_type_specifier_list(struct ctx
*ctx
,
3014 struct ctf_node
*ts_list
, struct ctf_field_type
**decl
)
3017 struct ctf_node
*first
, *node
;
3021 if (ts_list
->type
!= NODE_TYPE_SPECIFIER_LIST
) {
3022 _BT_LOGE_NODE(ts_list
,
3023 "Unexpected node type: node-type=%d", ts_list
->type
);
3028 first
= _BT_LIST_FIRST_ENTRY(&ts_list
->u
.type_specifier_list
.head
,
3029 struct ctf_node
, siblings
);
3030 if (first
->type
!= NODE_TYPE_SPECIFIER
) {
3031 _BT_LOGE_NODE(first
,
3032 "Unexpected node type: node-type=%d", first
->type
);
3037 node
= first
->u
.type_specifier
.node
;
3039 switch (first
->u
.type_specifier
.type
) {
3040 case TYPESPEC_INTEGER
:
3041 ret
= visit_integer_decl(ctx
, &node
->u
.integer
.expressions
,
3048 case TYPESPEC_FLOATING_POINT
:
3049 ret
= visit_floating_point_number_decl(ctx
,
3050 &node
->u
.floating_point
.expressions
, (void *) decl
);
3056 case TYPESPEC_STRING
:
3057 ret
= visit_string_decl(ctx
,
3058 &node
->u
.string
.expressions
, (void *) decl
);
3064 case TYPESPEC_STRUCT
:
3065 ret
= visit_struct_decl(ctx
, node
->u
._struct
.name
,
3066 &node
->u
._struct
.declaration_list
,
3067 node
->u
._struct
.has_body
,
3068 &node
->u
._struct
.min_align
, (void *) decl
);
3074 case TYPESPEC_VARIANT
:
3075 ret
= visit_variant_decl(ctx
, node
->u
.variant
.name
,
3076 node
->u
.variant
.choice
,
3077 &node
->u
.variant
.declaration_list
,
3078 node
->u
.variant
.has_body
, (void *) decl
);
3085 ret
= visit_enum_decl(ctx
, node
->u
._enum
.enum_id
,
3086 node
->u
._enum
.container_type
,
3087 &node
->u
._enum
.enumerator_list
,
3088 node
->u
._enum
.has_body
, (void *) decl
);
3096 case TYPESPEC_SHORT
:
3099 case TYPESPEC_FLOAT
:
3100 case TYPESPEC_DOUBLE
:
3101 case TYPESPEC_SIGNED
:
3102 case TYPESPEC_UNSIGNED
:
3104 case TYPESPEC_COMPLEX
:
3105 case TYPESPEC_IMAGINARY
:
3106 case TYPESPEC_CONST
:
3107 case TYPESPEC_ID_TYPE
:
3108 ret
= visit_type_specifier(ctx
, ts_list
, decl
);
3110 _BT_LOGE_NODE(first
,
3111 "Cannot visit type specifier: ret=%d",
3118 _BT_LOGE_NODE(first
,
3119 "Unexpected type specifier type: node-type=%d",
3120 first
->u
.type_specifier
.type
);
3129 ctf_field_type_destroy((void *) *decl
);
3135 int visit_event_decl_entry(struct ctx
*ctx
, struct ctf_node
*node
,
3136 struct ctf_event_class
*event_class
, uint64_t *stream_id
,
3142 switch (node
->type
) {
3144 ret
= visit_typedef(ctx
, node
->u
._typedef
.type_specifier_list
,
3145 &node
->u
._typedef
.type_declarators
);
3148 "Cannot add type definition found in event class.");
3152 case NODE_TYPEALIAS
:
3153 ret
= visit_typealias(ctx
, node
->u
.typealias
.target
,
3154 node
->u
.typealias
.alias
);
3157 "Cannot add type alias found in event class.");
3161 case NODE_CTF_EXPRESSION
:
3163 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
3165 _BT_LOGE_NODE(node
, "Cannot concatenate unary strings.");
3170 if (!strcmp(left
, "name")) {
3171 /* This is already known at this stage */
3172 if (_IS_SET(set
, _EVENT_NAME_SET
)) {
3173 _BT_LOGE_DUP_ATTR(node
, "name", "event class");
3178 _SET(set
, _EVENT_NAME_SET
);
3179 } else if (!strcmp(left
, "id")) {
3182 if (_IS_SET(set
, _EVENT_ID_SET
)) {
3183 _BT_LOGE_DUP_ATTR(node
, "id", "event class");
3188 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
3190 /* Only read "id" if get_unary_unsigned() succeeded. */
3191 if (ret
|| (!ret
&& id
< 0)) {
3193 "Unexpected unary expression for event class's `id` attribute.");
3198 event_class
->id
= id
;
3199 _SET(set
, _EVENT_ID_SET
);
3200 } else if (!strcmp(left
, "stream_id")) {
3201 if (_IS_SET(set
, _EVENT_STREAM_ID_SET
)) {
3202 _BT_LOGE_DUP_ATTR(node
, "stream_id",
3208 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
3212 * Only read "stream_id" if get_unary_unsigned()
3215 if (ret
|| (!ret
&& *stream_id
< 0)) {
3217 "Unexpected unary expression for event class's `stream_id` attribute.");
3222 _SET(set
, _EVENT_STREAM_ID_SET
);
3223 } else if (!strcmp(left
, "context")) {
3224 if (_IS_SET(set
, _EVENT_CONTEXT_SET
)) {
3226 "Duplicate `context` entry in event class.");
3231 ret
= visit_type_specifier_list(ctx
,
3232 _BT_LIST_FIRST_ENTRY(
3233 &node
->u
.ctf_expression
.right
,
3234 struct ctf_node
, siblings
),
3235 &event_class
->spec_context_ft
);
3238 "Cannot create event class's context field type.");
3242 BT_ASSERT(event_class
->spec_context_ft
);
3243 _SET(set
, _EVENT_CONTEXT_SET
);
3244 } else if (!strcmp(left
, "fields")) {
3245 if (_IS_SET(set
, _EVENT_FIELDS_SET
)) {
3247 "Duplicate `fields` entry in event class.");
3252 ret
= visit_type_specifier_list(ctx
,
3253 _BT_LIST_FIRST_ENTRY(
3254 &node
->u
.ctf_expression
.right
,
3255 struct ctf_node
, siblings
),
3256 &event_class
->payload_ft
);
3259 "Cannot create event class's payload field type.");
3263 BT_ASSERT(event_class
->payload_ft
);
3264 _SET(set
, _EVENT_FIELDS_SET
);
3265 } else if (!strcmp(left
, "loglevel")) {
3266 uint64_t loglevel_value
;
3267 enum bt_event_class_log_level log_level
= -1;
3269 if (_IS_SET(set
, _EVENT_LOG_LEVEL_SET
)) {
3270 _BT_LOGE_DUP_ATTR(node
, "loglevel",
3276 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
3280 "Unexpected unary expression for event class's `loglevel` attribute.");
3285 switch (loglevel_value
) {
3287 log_level
= BT_EVENT_CLASS_LOG_LEVEL_EMERGENCY
;
3290 log_level
= BT_EVENT_CLASS_LOG_LEVEL_ALERT
;
3293 log_level
= BT_EVENT_CLASS_LOG_LEVEL_CRITICAL
;
3296 log_level
= BT_EVENT_CLASS_LOG_LEVEL_ERROR
;
3299 log_level
= BT_EVENT_CLASS_LOG_LEVEL_WARNING
;
3302 log_level
= BT_EVENT_CLASS_LOG_LEVEL_NOTICE
;
3305 log_level
= BT_EVENT_CLASS_LOG_LEVEL_INFO
;
3308 log_level
= BT_EVENT_CLASS_LOG_LEVEL_DEBUG_SYSTEM
;
3311 log_level
= BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROGRAM
;
3314 log_level
= BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROCESS
;
3317 log_level
= BT_EVENT_CLASS_LOG_LEVEL_DEBUG_MODULE
;
3320 log_level
= BT_EVENT_CLASS_LOG_LEVEL_DEBUG_UNIT
;
3323 log_level
= BT_EVENT_CLASS_LOG_LEVEL_DEBUG_FUNCTION
;
3326 log_level
= BT_EVENT_CLASS_LOG_LEVEL_DEBUG_LINE
;
3329 log_level
= BT_EVENT_CLASS_LOG_LEVEL_DEBUG
;
3332 _BT_LOGW_NODE(node
, "Not setting event class's log level because its value is unknown: "
3333 "log-level=%" PRIu64
, loglevel_value
);
3336 if (log_level
!= -1) {
3337 event_class
->log_level
= log_level
;
3340 _SET(set
, _EVENT_LOG_LEVEL_SET
);
3341 } else if (!strcmp(left
, "model.emf.uri")) {
3344 if (_IS_SET(set
, _EVENT_MODEL_EMF_URI_SET
)) {
3345 _BT_LOGE_DUP_ATTR(node
, "model.emf.uri",
3351 right
= concatenate_unary_strings(
3352 &node
->u
.ctf_expression
.right
);
3355 "Unexpected unary expression for event class's `model.emf.uri` attribute.");
3360 if (strlen(right
) == 0) {
3362 "Not setting event class's EMF URI because it's empty.");
3364 g_string_assign(event_class
->emf_uri
,
3369 _SET(set
, _EVENT_MODEL_EMF_URI_SET
);
3372 "Unknown attribute in event class: "
3373 "attr-name=\"%s\"", left
);
3397 char *get_event_decl_name(struct ctx
*ctx
, struct ctf_node
*node
)
3401 struct ctf_node
*iter
;
3402 struct bt_list_head
*decl_list
= &node
->u
.event
.declaration_list
;
3404 bt_list_for_each_entry(iter
, decl_list
, siblings
) {
3405 if (iter
->type
!= NODE_CTF_EXPRESSION
) {
3409 left
= concatenate_unary_strings(&iter
->u
.ctf_expression
.left
);
3412 "Cannot concatenate unary strings.");
3416 if (!strcmp(left
, "name")) {
3417 name
= concatenate_unary_strings(
3418 &iter
->u
.ctf_expression
.right
);
3421 "Unexpected unary expression for event class's `name` attribute.");
3442 int visit_event_decl(struct ctx
*ctx
, struct ctf_node
*node
)
3446 struct ctf_node
*iter
;
3447 uint64_t stream_id
= 0;
3448 char *event_name
= NULL
;
3449 struct ctf_event_class
*event_class
= NULL
;
3450 struct ctf_stream_class
*stream_class
= NULL
;
3451 struct bt_list_head
*decl_list
= &node
->u
.event
.declaration_list
;
3452 bool pop_scope
= false;
3454 if (node
->visited
) {
3458 node
->visited
= TRUE
;
3459 event_name
= get_event_decl_name(ctx
, node
);
3462 "Missing `name` attribute in event class.");
3467 event_class
= ctf_event_class_create();
3468 BT_ASSERT(event_class
);
3469 g_string_assign(event_class
->name
, event_name
);
3470 _TRY_PUSH_SCOPE_OR_GOTO_ERROR();
3473 bt_list_for_each_entry(iter
, decl_list
, siblings
) {
3474 ret
= visit_event_decl_entry(ctx
, iter
, event_class
,
3477 _BT_LOGE_NODE(iter
, "Cannot visit event class's entry: "
3483 if (!_IS_SET(&set
, _EVENT_STREAM_ID_SET
)) {
3485 * Allow missing stream_id if there is only a single
3488 switch (ctx
->ctf_tc
->stream_classes
->len
) {
3490 /* Create implicit stream class if there's none */
3492 stream_class
= ctf_stream_class_create();
3493 BT_ASSERT(stream_class
);
3494 stream_class
->id
= stream_id
;
3495 g_ptr_array_add(ctx
->ctf_tc
->stream_classes
,
3497 stream_class
= stream_class
;
3500 /* Single stream class: get its ID */
3501 stream_class
= ctx
->ctf_tc
->stream_classes
->pdata
[0];
3502 stream_id
= stream_class
->id
;
3506 "Missing `stream_id` attribute in event class.");
3512 /* We have the stream ID now; get the stream class if found */
3513 if (!stream_class
) {
3514 stream_class
= ctf_trace_class_borrow_stream_class_by_id(
3515 ctx
->ctf_tc
, stream_id
);
3516 if (!stream_class
) {
3518 "Cannot find stream class at this point: "
3519 "id=%" PRId64
, stream_id
);
3525 BT_ASSERT(stream_class
);
3527 if (!_IS_SET(&set
, _EVENT_ID_SET
)) {
3528 /* Allow only one event without ID per stream */
3529 if (stream_class
->event_classes
->len
!= 0) {
3531 "Missing `id` attribute in event class.");
3537 event_class
->id
= 0;
3540 if (ctf_stream_class_borrow_event_class_by_id(stream_class
,
3543 "Duplicate event class (same ID) in the same stream class: "
3544 "id=%" PRId64
, event_class
->id
);
3549 ctf_stream_class_append_event_class(stream_class
, event_class
);
3554 ctf_event_class_destroy(event_class
);
3574 int auto_map_field_to_trace_clock_class(struct ctx
*ctx
,
3575 struct ctf_field_type
*ft
)
3577 struct bt_clock_class
*clock_class_to_map_to
= NULL
;
3578 struct ctf_field_type_int
*int_ft
= (void *) ft
;
3580 uint64_t clock_class_count
;
3586 if (ft
->id
!= CTF_FIELD_TYPE_ID_INT
&&
3587 ft
->id
!= CTF_FIELD_TYPE_ID_ENUM
) {
3591 if (int_ft
->mapped_clock_class
) {
3592 /* Already mapped */
3596 clock_class_count
= ctx
->ctf_tc
->clock_classes
->len
;
3598 switch (clock_class_count
) {
3601 * No clock class exists in the trace at this point. Create an
3602 * implicit one at 1 GHz, named `default`, and use this clock
3605 clock_class_to_map_to
= bt_clock_class_create();
3606 BT_ASSERT(clock_class_to_map_to
);
3607 ret
= bt_clock_class_set_frequency(clock_class_to_map_to
,
3608 UINT64_C(1000000000));
3609 BT_ASSERT(ret
== 0);
3610 ret
= bt_clock_class_set_name(clock_class_to_map_to
,
3612 BT_ASSERT(ret
== 0);
3613 g_ptr_array_add(ctx
->ctf_tc
->clock_classes
,
3614 bt_get(clock_class_to_map_to
));
3618 * Only one clock class exists in the trace at this point: use
3621 clock_class_to_map_to
=
3622 bt_get(ctx
->ctf_tc
->clock_classes
->pdata
[0]);
3626 * Timestamp field not mapped to a clock class and there's more
3627 * than one clock class in the trace: this is an error.
3629 BT_LOGE_STR("Timestamp field found with no mapped clock class, "
3630 "but there's more than one clock class in the trace at this point.");
3635 BT_ASSERT(clock_class_to_map_to
);
3636 int_ft
->mapped_clock_class
= bt_get(clock_class_to_map_to
);
3639 bt_put(clock_class_to_map_to
);
3644 int auto_map_fields_to_trace_clock_class(struct ctx
*ctx
,
3645 struct ctf_field_type
*root_ft
, const char *field_name
)
3649 struct ctf_field_type_struct
*struct_ft
= (void *) root_ft
;
3650 struct ctf_field_type_variant
*var_ft
= (void *) root_ft
;
3656 if (root_ft
->id
!= CTF_FIELD_TYPE_ID_STRUCT
&&
3657 root_ft
->id
!= CTF_FIELD_TYPE_ID_VARIANT
) {
3661 if (root_ft
->id
== CTF_FIELD_TYPE_ID_STRUCT
) {
3662 count
= struct_ft
->members
->len
;
3664 count
= var_ft
->options
->len
;
3667 for (i
= 0; i
< count
; i
++) {
3668 struct ctf_named_field_type
*named_ft
= NULL
;
3670 if (root_ft
->id
== CTF_FIELD_TYPE_ID_STRUCT
) {
3671 named_ft
= ctf_field_type_struct_borrow_member_by_index(
3673 } else if (root_ft
->id
== CTF_FIELD_TYPE_ID_VARIANT
) {
3674 named_ft
= ctf_field_type_variant_borrow_option_by_index(
3678 if (strcmp(named_ft
->name
->str
, field_name
) == 0) {
3679 ret
= auto_map_field_to_trace_clock_class(ctx
,
3682 BT_LOGE("Cannot automatically map field to trace's clock class: "
3683 "field-name=\"%s\"", field_name
);
3688 ret
= auto_map_fields_to_trace_clock_class(ctx
, named_ft
->ft
,
3691 BT_LOGE("Cannot automatically map structure or variant field type's fields to trace's clock class: "
3692 "field-name=\"%s\", root-field-name=\"%s\"",
3693 field_name
, named_ft
->name
->str
);
3703 int visit_stream_decl_entry(struct ctx
*ctx
, struct ctf_node
*node
,
3704 struct ctf_stream_class
*stream_class
, int *set
)
3709 switch (node
->type
) {
3711 ret
= visit_typedef(ctx
, node
->u
._typedef
.type_specifier_list
,
3712 &node
->u
._typedef
.type_declarators
);
3715 "Cannot add type definition found in stream class.");
3719 case NODE_TYPEALIAS
:
3720 ret
= visit_typealias(ctx
, node
->u
.typealias
.target
,
3721 node
->u
.typealias
.alias
);
3724 "Cannot add type alias found in stream class.");
3728 case NODE_CTF_EXPRESSION
:
3730 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
3732 _BT_LOGE_NODE(node
, "Cannot concatenate unary strings.");
3737 if (!strcmp(left
, "id")) {
3740 if (_IS_SET(set
, _STREAM_ID_SET
)) {
3741 _BT_LOGE_DUP_ATTR(node
, "id",
3742 "stream declaration");
3747 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
3750 /* Only read "id" if get_unary_unsigned() succeeded. */
3751 if (ret
|| (!ret
&& id
< 0)) {
3753 "Unexpected unary expression for stream class's `id` attribute.");
3758 if (ctf_trace_class_borrow_stream_class_by_id(
3761 "Duplicate stream class (same ID): id=%" PRId64
,
3767 stream_class
->id
= id
;
3768 _SET(set
, _STREAM_ID_SET
);
3769 } else if (!strcmp(left
, "event.header")) {
3770 if (_IS_SET(set
, _STREAM_EVENT_HEADER_SET
)) {
3772 "Duplicate `event.header` entry in stream class.");
3777 ret
= visit_type_specifier_list(ctx
,
3778 _BT_LIST_FIRST_ENTRY(
3779 &node
->u
.ctf_expression
.right
,
3780 struct ctf_node
, siblings
),
3781 &stream_class
->event_header_ft
);
3784 "Cannot create stream class's event header field type.");
3788 BT_ASSERT(stream_class
->event_header_ft
);
3789 ret
= auto_map_fields_to_trace_clock_class(ctx
,
3790 stream_class
->event_header_ft
, "timestamp");
3793 "Cannot automatically map specific event header field type fields named `timestamp` to trace's clock class.");
3797 _SET(set
, _STREAM_EVENT_HEADER_SET
);
3798 } else if (!strcmp(left
, "event.context")) {
3799 if (_IS_SET(set
, _STREAM_EVENT_CONTEXT_SET
)) {
3801 "Duplicate `event.context` entry in stream class.");
3806 ret
= visit_type_specifier_list(ctx
,
3807 _BT_LIST_FIRST_ENTRY(
3808 &node
->u
.ctf_expression
.right
,
3809 struct ctf_node
, siblings
),
3810 &stream_class
->event_common_context_ft
);
3813 "Cannot create stream class's event context field type.");
3817 BT_ASSERT(stream_class
->event_common_context_ft
);
3818 _SET(set
, _STREAM_EVENT_CONTEXT_SET
);
3819 } else if (!strcmp(left
, "packet.context")) {
3820 if (_IS_SET(set
, _STREAM_PACKET_CONTEXT_SET
)) {
3822 "Duplicate `packet.context` entry in stream class.");
3827 ret
= visit_type_specifier_list(ctx
,
3828 _BT_LIST_FIRST_ENTRY(
3829 &node
->u
.ctf_expression
.right
,
3830 struct ctf_node
, siblings
),
3831 &stream_class
->packet_context_ft
);
3834 "Cannot create stream class's packet context field type.");
3838 BT_ASSERT(stream_class
->packet_context_ft
);
3839 ret
= auto_map_fields_to_trace_clock_class(ctx
,
3840 stream_class
->packet_context_ft
,
3844 "Cannot automatically map specific packet context field type fields named `timestamp_begin` to trace's clock class.");
3848 ret
= auto_map_fields_to_trace_clock_class(ctx
,
3849 stream_class
->packet_context_ft
,
3853 "Cannot automatically map specific packet context field type fields named `timestamp_end` to trace's clock class.");
3857 _SET(set
, _STREAM_PACKET_CONTEXT_SET
);
3860 "Unknown attribute in stream class: "
3861 "attr-name=\"%s\"", left
);
3882 int visit_stream_decl(struct ctx
*ctx
, struct ctf_node
*node
)
3886 struct ctf_node
*iter
;
3887 struct ctf_stream_class
*stream_class
= NULL
;
3888 struct bt_list_head
*decl_list
= &node
->u
.stream
.declaration_list
;
3890 if (node
->visited
) {
3894 node
->visited
= TRUE
;
3895 stream_class
= ctf_stream_class_create();
3896 BT_ASSERT(stream_class
);
3897 _TRY_PUSH_SCOPE_OR_GOTO_ERROR();
3899 bt_list_for_each_entry(iter
, decl_list
, siblings
) {
3900 ret
= visit_stream_decl_entry(ctx
, iter
, stream_class
, &set
);
3903 "Cannot visit stream class's entry: "
3912 if (_IS_SET(&set
, _STREAM_ID_SET
)) {
3913 /* Check that packet header has `stream_id` field */
3914 struct ctf_named_field_type
*named_ft
= NULL
;
3916 if (!ctx
->ctf_tc
->packet_header_ft
) {
3918 "Stream class has a `id` attribute, "
3919 "but trace has no packet header field type.");
3923 named_ft
= ctf_field_type_struct_borrow_member_by_name(
3924 (void *) ctx
->ctf_tc
->packet_header_ft
, "stream_id");
3927 "Stream class has a `id` attribute, "
3928 "but trace's packet header field type has no `stream_id` field.");
3932 if (named_ft
->ft
->id
!= CTF_FIELD_TYPE_ID_INT
&&
3933 named_ft
->ft
->id
!= CTF_FIELD_TYPE_ID_ENUM
) {
3935 "Stream class has a `id` attribute, "
3936 "but trace's packet header field type's `stream_id` field is not an integer field type.");
3940 /* Allow only _one_ ID-less stream */
3941 if (ctx
->ctf_tc
->stream_classes
->len
!= 0) {
3943 "Missing `id` attribute in stream class as there's more than one stream class in the trace.");
3948 /* Automatic ID: 0 */
3949 stream_class
->id
= 0;
3953 * Make sure that this stream class's ID is currently unique in
3956 if (ctf_trace_class_borrow_stream_class_by_id(ctx
->ctf_tc
,
3957 stream_class
->id
)) {
3959 "Duplicate stream class (same ID): id=%" PRId64
,
3965 g_ptr_array_add(ctx
->ctf_tc
->stream_classes
, stream_class
);
3966 stream_class
= NULL
;
3970 ctf_stream_class_destroy(stream_class
);
3971 stream_class
= NULL
;
3978 int visit_trace_decl_entry(struct ctx
*ctx
, struct ctf_node
*node
, int *set
)
3984 switch (node
->type
) {
3986 ret
= visit_typedef(ctx
, node
->u
._typedef
.type_specifier_list
,
3987 &node
->u
._typedef
.type_declarators
);
3990 "Cannot add type definition found in trace (`trace` block).");
3994 case NODE_TYPEALIAS
:
3995 ret
= visit_typealias(ctx
, node
->u
.typealias
.target
,
3996 node
->u
.typealias
.alias
);
3999 "Cannot add type alias found in trace (`trace` block).");
4003 case NODE_CTF_EXPRESSION
:
4005 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
4007 _BT_LOGE_NODE(node
, "Cannot concatenate unary strings.");
4012 if (!strcmp(left
, "major")) {
4013 if (_IS_SET(set
, _TRACE_MAJOR_SET
)) {
4014 _BT_LOGE_DUP_ATTR(node
, "major", "trace");
4019 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
4023 "Unexpected unary expression for trace's `major` attribute.");
4030 "Invalid trace's `minor` attribute: expecting 1.");
4034 ctx
->ctf_tc
->major
= val
;
4035 _SET(set
, _TRACE_MAJOR_SET
);
4036 } else if (!strcmp(left
, "minor")) {
4037 if (_IS_SET(set
, _TRACE_MINOR_SET
)) {
4038 _BT_LOGE_DUP_ATTR(node
, "minor", "trace");
4043 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
4047 "Unexpected unary expression for trace's `minor` attribute.");
4054 "Invalid trace's `minor` attribute: expecting 8.");
4058 ctx
->ctf_tc
->minor
= val
;
4059 _SET(set
, _TRACE_MINOR_SET
);
4060 } else if (!strcmp(left
, "uuid")) {
4061 if (_IS_SET(set
, _TRACE_UUID_SET
)) {
4062 _BT_LOGE_DUP_ATTR(node
, "uuid", "trace");
4067 ret
= get_unary_uuid(&node
->u
.ctf_expression
.right
,
4071 "Invalid trace's `uuid` attribute.");
4075 ctx
->ctf_tc
->is_uuid_set
= true;
4076 _SET(set
, _TRACE_UUID_SET
);
4077 } else if (!strcmp(left
, "byte_order")) {
4078 /* Default byte order is already known at this stage */
4079 if (_IS_SET(set
, _TRACE_BYTE_ORDER_SET
)) {
4080 _BT_LOGE_DUP_ATTR(node
, "byte_order",
4086 BT_ASSERT(ctx
->ctf_tc
->default_byte_order
!= -1);
4087 _SET(set
, _TRACE_BYTE_ORDER_SET
);
4088 } else if (!strcmp(left
, "packet.header")) {
4089 if (_IS_SET(set
, _TRACE_PACKET_HEADER_SET
)) {
4091 "Duplicate `packet.header` entry in trace.");
4096 ret
= visit_type_specifier_list(ctx
,
4097 _BT_LIST_FIRST_ENTRY(
4098 &node
->u
.ctf_expression
.right
,
4099 struct ctf_node
, siblings
),
4100 &ctx
->ctf_tc
->packet_header_ft
);
4103 "Cannot create trace's packet header field type.");
4107 BT_ASSERT(ctx
->ctf_tc
->packet_header_ft
);
4108 _SET(set
, _TRACE_PACKET_HEADER_SET
);
4111 "Unknown attribute in stream class: "
4112 "attr-name=\"%s\"", left
);
4120 _BT_LOGE_NODE(node
, "Unknown expression in trace.");
4133 int visit_trace_decl(struct ctx
*ctx
, struct ctf_node
*node
)
4137 struct ctf_node
*iter
;
4138 struct bt_list_head
*decl_list
= &node
->u
.trace
.declaration_list
;
4140 if (node
->visited
) {
4144 node
->visited
= TRUE
;
4146 if (ctx
->is_trace_visited
) {
4147 _BT_LOGE_NODE(node
, "Duplicate trace (`trace` block).");
4152 _TRY_PUSH_SCOPE_OR_GOTO_ERROR();
4154 bt_list_for_each_entry(iter
, decl_list
, siblings
) {
4155 ret
= visit_trace_decl_entry(ctx
, iter
, &set
);
4157 _BT_LOGE_NODE(iter
, "Cannot visit trace's entry (`trace` block): "
4166 if (!_IS_SET(&set
, _TRACE_MAJOR_SET
)) {
4168 "Missing `major` attribute in trace (`trace` block).");
4173 if (!_IS_SET(&set
, _TRACE_MINOR_SET
)) {
4175 "Missing `minor` attribute in trace (`trace` block).");
4180 if (!_IS_SET(&set
, _TRACE_BYTE_ORDER_SET
)) {
4182 "Missing `byte_order` attribute in trace (`trace` block).");
4187 ctx
->is_trace_visited
= true;
4197 int visit_env(struct ctx
*ctx
, struct ctf_node
*node
)
4201 struct ctf_node
*entry_node
;
4202 struct bt_list_head
*decl_list
= &node
->u
.env
.declaration_list
;
4204 if (node
->visited
) {
4208 node
->visited
= TRUE
;
4210 bt_list_for_each_entry(entry_node
, decl_list
, siblings
) {
4211 struct bt_list_head
*right_head
=
4212 &entry_node
->u
.ctf_expression
.right
;
4214 if (entry_node
->type
!= NODE_CTF_EXPRESSION
) {
4215 _BT_LOGE_NODE(entry_node
,
4216 "Wrong expression in environment entry: "
4217 "node-type=%d", entry_node
->type
);
4222 left
= concatenate_unary_strings(
4223 &entry_node
->u
.ctf_expression
.left
);
4225 _BT_LOGE_NODE(entry_node
,
4226 "Cannot get environment entry's name.");
4231 if (is_unary_string(right_head
)) {
4232 char *right
= concatenate_unary_strings(right_head
);
4235 _BT_LOGE_NODE(entry_node
,
4236 "Unexpected unary expression for environment entry's value: "
4237 "name=\"%s\"", left
);
4242 if (strcmp(left
, "tracer_name") == 0) {
4243 if (strncmp(right
, "lttng", 5) == 0) {
4244 BT_LOGI("Detected LTTng trace from `%s` environment value: "
4245 "tracer-name=\"%s\"",
4247 ctx
->is_lttng
= true;
4251 ctf_trace_class_append_env_entry(ctx
->ctf_tc
,
4252 left
, CTF_TRACE_CLASS_ENV_ENTRY_TYPE_STR
,
4255 } else if (is_unary_unsigned(right_head
) ||
4256 is_unary_signed(right_head
)) {
4259 if (is_unary_unsigned(right_head
)) {
4260 ret
= get_unary_unsigned(right_head
,
4263 ret
= get_unary_signed(right_head
, &v
);
4266 _BT_LOGE_NODE(entry_node
,
4267 "Unexpected unary expression for environment entry's value: "
4268 "name=\"%s\"", left
);
4273 ctf_trace_class_append_env_entry(ctx
->ctf_tc
,
4274 left
, CTF_TRACE_CLASS_ENV_ENTRY_TYPE_INT
,
4277 _BT_LOGW_NODE(entry_node
,
4278 "Environment entry has unknown type: "
4279 "name=\"%s\"", left
);
4295 int set_trace_byte_order(struct ctx
*ctx
, struct ctf_node
*trace_node
)
4300 struct ctf_node
*node
;
4301 struct bt_list_head
*decl_list
= &trace_node
->u
.trace
.declaration_list
;
4303 bt_list_for_each_entry(node
, decl_list
, siblings
) {
4304 if (node
->type
== NODE_CTF_EXPRESSION
) {
4305 struct ctf_node
*right_node
;
4307 left
= concatenate_unary_strings(
4308 &node
->u
.ctf_expression
.left
);
4311 "Cannot concatenate unary strings.");
4316 if (!strcmp(left
, "byte_order")) {
4317 enum ctf_byte_order bo
;
4319 if (_IS_SET(&set
, _TRACE_BYTE_ORDER_SET
)) {
4320 _BT_LOGE_DUP_ATTR(node
, "byte_order",
4326 _SET(&set
, _TRACE_BYTE_ORDER_SET
);
4327 right_node
= _BT_LIST_FIRST_ENTRY(
4328 &node
->u
.ctf_expression
.right
,
4329 struct ctf_node
, siblings
);
4330 bo
= byte_order_from_unary_expr(right_node
);
4333 "Invalid `byte_order` attribute in trace (`trace` block): "
4334 "expecting `le`, `be`, or `network`.");
4337 } else if (bo
== CTF_BYTE_ORDER_DEFAULT
) {
4339 "Invalid `byte_order` attribute in trace (`trace` block): "
4340 "cannot be set to `native` here.");
4345 ctx
->ctf_tc
->default_byte_order
= bo
;
4353 if (!_IS_SET(&set
, _TRACE_BYTE_ORDER_SET
)) {
4354 _BT_LOGE_NODE(trace_node
,
4355 "Missing `byte_order` attribute in trace (`trace` block).");
4368 int visit_clock_decl_entry(struct ctx
*ctx
, struct ctf_node
*entry_node
,
4369 struct bt_clock_class
*clock
, int *set
, int64_t *offset_seconds
,
4370 uint64_t *offset_cycles
)
4375 if (entry_node
->type
!= NODE_CTF_EXPRESSION
) {
4376 _BT_LOGE_NODE(entry_node
,
4377 "Unexpected node type: node-type=%d",
4383 left
= concatenate_unary_strings(&entry_node
->u
.ctf_expression
.left
);
4385 _BT_LOGE_NODE(entry_node
, "Cannot concatenate unary strings.");
4390 if (!strcmp(left
, "name")) {
4393 if (_IS_SET(set
, _CLOCK_NAME_SET
)) {
4394 _BT_LOGE_DUP_ATTR(entry_node
, "name", "clock class");
4399 right
= concatenate_unary_strings(
4400 &entry_node
->u
.ctf_expression
.right
);
4402 _BT_LOGE_NODE(entry_node
,
4403 "Unexpected unary expression for clock class's `name` attribute.");
4408 ret
= bt_clock_class_set_name(clock
, right
);
4410 _BT_LOGE_NODE(entry_node
,
4411 "cannot set clock class's name");
4417 _SET(set
, _CLOCK_NAME_SET
);
4418 } else if (!strcmp(left
, "uuid")) {
4419 uint8_t uuid
[BABELTRACE_UUID_LEN
];
4421 if (_IS_SET(set
, _CLOCK_UUID_SET
)) {
4422 _BT_LOGE_DUP_ATTR(entry_node
, "uuid", "clock class");
4427 ret
= get_unary_uuid(&entry_node
->u
.ctf_expression
.right
, uuid
);
4429 _BT_LOGE_NODE(entry_node
,
4430 "Invalid clock class's `uuid` attribute.");
4434 ret
= bt_clock_class_set_uuid(clock
, uuid
);
4436 _BT_LOGE_NODE(entry_node
,
4437 "Cannot set clock class's UUID.");
4441 _SET(set
, _CLOCK_UUID_SET
);
4442 } else if (!strcmp(left
, "description")) {
4445 if (_IS_SET(set
, _CLOCK_DESCRIPTION_SET
)) {
4446 _BT_LOGE_DUP_ATTR(entry_node
, "description",
4452 right
= concatenate_unary_strings(
4453 &entry_node
->u
.ctf_expression
.right
);
4455 _BT_LOGE_NODE(entry_node
,
4456 "Unexpected unary expression for clock class's `description` attribute.");
4461 ret
= bt_clock_class_set_description(clock
, right
);
4463 _BT_LOGE_NODE(entry_node
,
4464 "Cannot set clock class's description.");
4470 _SET(set
, _CLOCK_DESCRIPTION_SET
);
4471 } else if (!strcmp(left
, "freq")) {
4472 uint64_t freq
= UINT64_C(-1);
4474 if (_IS_SET(set
, _CLOCK_FREQ_SET
)) {
4475 _BT_LOGE_DUP_ATTR(entry_node
, "freq", "clock class");
4480 ret
= get_unary_unsigned(
4481 &entry_node
->u
.ctf_expression
.right
, &freq
);
4483 _BT_LOGE_NODE(entry_node
,
4484 "Unexpected unary expression for clock class's `freq` attribute.");
4489 if (freq
== UINT64_C(-1) || freq
== 0) {
4490 _BT_LOGE_NODE(entry_node
,
4491 "Invalid clock class frequency: freq=%" PRIu64
,
4497 ret
= bt_clock_class_set_frequency(clock
, freq
);
4499 _BT_LOGE_NODE(entry_node
,
4500 "Cannot set clock class's frequency.");
4504 _SET(set
, _CLOCK_FREQ_SET
);
4505 } else if (!strcmp(left
, "precision")) {
4508 if (_IS_SET(set
, _CLOCK_PRECISION_SET
)) {
4509 _BT_LOGE_DUP_ATTR(entry_node
, "precision",
4515 ret
= get_unary_unsigned(
4516 &entry_node
->u
.ctf_expression
.right
, &precision
);
4518 _BT_LOGE_NODE(entry_node
,
4519 "Unexpected unary expression for clock class's `precision` attribute.");
4524 ret
= bt_clock_class_set_precision(clock
, precision
);
4526 _BT_LOGE_NODE(entry_node
,
4527 "Cannot set clock class's precision.");
4531 _SET(set
, _CLOCK_PRECISION_SET
);
4532 } else if (!strcmp(left
, "offset_s")) {
4533 if (_IS_SET(set
, _CLOCK_OFFSET_S_SET
)) {
4534 _BT_LOGE_DUP_ATTR(entry_node
, "offset_s",
4540 ret
= get_unary_signed(
4541 &entry_node
->u
.ctf_expression
.right
, offset_seconds
);
4543 _BT_LOGE_NODE(entry_node
,
4544 "Unexpected unary expression for clock class's `offset_s` attribute.");
4549 _SET(set
, _CLOCK_OFFSET_S_SET
);
4550 } else if (!strcmp(left
, "offset")) {
4551 if (_IS_SET(set
, _CLOCK_OFFSET_SET
)) {
4552 _BT_LOGE_DUP_ATTR(entry_node
, "offset", "clock class");
4557 ret
= get_unary_unsigned(
4558 &entry_node
->u
.ctf_expression
.right
, offset_cycles
);
4560 _BT_LOGE_NODE(entry_node
,
4561 "Unexpected unary expression for clock class's `offset` attribute.");
4566 _SET(set
, _CLOCK_OFFSET_SET
);
4567 } else if (!strcmp(left
, "absolute")) {
4568 struct ctf_node
*right
;
4570 if (_IS_SET(set
, _CLOCK_ABSOLUTE_SET
)) {
4571 _BT_LOGE_DUP_ATTR(entry_node
, "absolute",
4577 right
= _BT_LIST_FIRST_ENTRY(
4578 &entry_node
->u
.ctf_expression
.right
,
4579 struct ctf_node
, siblings
);
4580 ret
= get_boolean(right
);
4582 _BT_LOGE_NODE(entry_node
,
4583 "Unexpected unary expression for clock class's `absolute` attribute.");
4588 ret
= bt_clock_class_set_is_absolute(clock
, ret
);
4590 _BT_LOGE_NODE(entry_node
,
4591 "Cannot set clock class's absolute flag.");
4595 _SET(set
, _CLOCK_ABSOLUTE_SET
);
4597 _BT_LOGW_NODE(entry_node
,
4598 "Unknown attribute in clock class: attr-name=\"%s\"",
4612 uint64_t cycles_from_ns(uint64_t frequency
, uint64_t ns
)
4617 if (frequency
== UINT64_C(1000000000)) {
4620 cycles
= (uint64_t) (((double) ns
* (double) frequency
) / 1e9
);
4627 void calibrate_clock_class_offsets(int64_t *offset_seconds
,
4628 uint64_t *offset_cycles
, uint64_t freq
)
4630 if (*offset_cycles
>= freq
) {
4631 const uint64_t s_in_offset_cycles
= *offset_cycles
/ freq
;
4633 *offset_seconds
+= (int64_t) s_in_offset_cycles
;
4634 *offset_cycles
-= (s_in_offset_cycles
* freq
);
4639 void apply_clock_class_offset(struct ctx
*ctx
, struct bt_clock_class
*clock
)
4643 int64_t offset_s_to_apply
= ctx
->decoder_config
.clock_class_offset_s
;
4644 uint64_t offset_ns_to_apply
;
4645 int64_t cur_offset_s
;
4646 uint64_t cur_offset_cycles
;
4648 if (ctx
->decoder_config
.clock_class_offset_s
== 0 &&
4649 ctx
->decoder_config
.clock_class_offset_ns
== 0) {
4653 /* Transfer nanoseconds to seconds as much as possible */
4654 if (ctx
->decoder_config
.clock_class_offset_ns
< 0) {
4655 const int64_t abs_ns
= -ctx
->decoder_config
.clock_class_offset_ns
;
4656 const int64_t abs_extra_s
= abs_ns
/ INT64_C(1000000000) + 1;
4657 const int64_t extra_s
= -abs_extra_s
;
4658 const int64_t offset_ns
= ctx
->decoder_config
.clock_class_offset_ns
-
4659 (extra_s
* INT64_C(1000000000));
4661 BT_ASSERT(offset_ns
> 0);
4662 offset_ns_to_apply
= (uint64_t) offset_ns
;
4663 offset_s_to_apply
+= extra_s
;
4665 const int64_t extra_s
= ctx
->decoder_config
.clock_class_offset_ns
/
4666 INT64_C(1000000000);
4667 const int64_t offset_ns
= ctx
->decoder_config
.clock_class_offset_ns
-
4668 (extra_s
* INT64_C(1000000000));
4670 BT_ASSERT(offset_ns
>= 0);
4671 offset_ns_to_apply
= (uint64_t) offset_ns
;
4672 offset_s_to_apply
+= extra_s
;
4675 freq
= bt_clock_class_get_frequency(clock
);
4676 bt_clock_class_get_offset(clock
, &cur_offset_s
, &cur_offset_cycles
);
4679 cur_offset_s
+= offset_s_to_apply
;
4680 cur_offset_cycles
+= cycles_from_ns(freq
, offset_ns_to_apply
);
4683 * Recalibrate offsets because the part in cycles can be greater
4684 * than the frequency at this point.
4686 calibrate_clock_class_offsets(&cur_offset_s
, &cur_offset_cycles
, freq
);
4688 /* Set final offsets */
4689 ret
= bt_clock_class_set_offset(clock
, cur_offset_s
, cur_offset_cycles
);
4690 BT_ASSERT(ret
== 0);
4697 int visit_clock_decl(struct ctx
*ctx
, struct ctf_node
*clock_node
)
4701 struct bt_clock_class
*clock
;
4702 struct ctf_node
*entry_node
;
4703 struct bt_list_head
*decl_list
= &clock_node
->u
.clock
.declaration_list
;
4704 const char *clock_class_name
;
4705 int64_t offset_seconds
= 0;
4706 uint64_t offset_cycles
= 0;
4709 if (clock_node
->visited
) {
4713 clock_node
->visited
= TRUE
;
4715 /* CTF 1.8's default frequency for a clock class is 1 GHz */
4716 clock
= bt_clock_class_create();
4718 _BT_LOGE_NODE(clock_node
,
4719 "Cannot create default clock class.");
4724 /* CTF: not absolute by default */
4725 ret
= bt_clock_class_set_is_absolute(clock
, BT_FALSE
);
4727 _BT_LOGE_NODE(clock_node
,
4728 "Cannot set clock class's absolute flag.");
4732 bt_list_for_each_entry(entry_node
, decl_list
, siblings
) {
4733 ret
= visit_clock_decl_entry(ctx
, entry_node
, clock
, &set
,
4734 &offset_seconds
, &offset_cycles
);
4736 _BT_LOGE_NODE(entry_node
,
4737 "Cannot visit clock class's entry: ret=%d",
4743 if (!_IS_SET(&set
, _CLOCK_NAME_SET
)) {
4744 _BT_LOGE_NODE(clock_node
,
4745 "Missing `name` attribute in clock class.");
4750 clock_class_name
= bt_clock_class_get_name(clock
);
4751 BT_ASSERT(clock_class_name
);
4752 if (ctx
->is_lttng
&& strcmp(clock_class_name
, "monotonic") == 0) {
4754 * Old versions of LTTng forgot to set its clock class
4755 * as absolute, even if it is. This is important because
4756 * it's a condition to be able to sort notifications
4757 * from different sources.
4759 ret
= bt_clock_class_set_is_absolute(clock
, BT_TRUE
);
4761 _BT_LOGE_NODE(clock_node
,
4762 "Cannot set clock class's absolute flag.");
4768 * Adjust offsets so that the part in cycles is less than the
4769 * frequency (move to the part in seconds).
4771 freq
= bt_clock_class_get_frequency(clock
);
4772 calibrate_clock_class_offsets(&offset_seconds
, &offset_cycles
, freq
);
4773 BT_ASSERT(offset_cycles
< bt_clock_class_get_frequency(clock
));
4774 ret
= bt_clock_class_set_offset(clock
, offset_seconds
, offset_cycles
);
4775 BT_ASSERT(ret
== 0);
4776 apply_clock_class_offset(ctx
, clock
);
4777 g_ptr_array_add(ctx
->ctf_tc
->clock_classes
, bt_get(clock
));
4785 int visit_root_decl(struct ctx
*ctx
, struct ctf_node
*root_decl_node
)
4789 if (root_decl_node
->visited
) {
4793 root_decl_node
->visited
= TRUE
;
4795 switch (root_decl_node
->type
) {
4797 ret
= visit_typedef(ctx
,
4798 root_decl_node
->u
._typedef
.type_specifier_list
,
4799 &root_decl_node
->u
._typedef
.type_declarators
);
4801 _BT_LOGE_NODE(root_decl_node
,
4802 "Cannot add type definition found in root scope.");
4806 case NODE_TYPEALIAS
:
4807 ret
= visit_typealias(ctx
, root_decl_node
->u
.typealias
.target
,
4808 root_decl_node
->u
.typealias
.alias
);
4810 _BT_LOGE_NODE(root_decl_node
,
4811 "Cannot add type alias found in root scope.");
4815 case NODE_TYPE_SPECIFIER_LIST
:
4817 struct ctf_field_type
*decl
= NULL
;
4820 * Just add the type specifier to the root
4821 * declaration scope. Put local reference.
4823 ret
= visit_type_specifier_list(ctx
, root_decl_node
, &decl
);
4825 _BT_LOGE_NODE(root_decl_node
,
4826 "Cannot visit root scope's field type: "
4832 ctf_field_type_destroy(decl
);
4837 _BT_LOGE_NODE(root_decl_node
,
4838 "Unexpected node type: node-type=%d",
4839 root_decl_node
->type
);
4849 int try_set_trace_class_name(struct ctx
*ctx
)
4851 GString
*name
= NULL
;
4853 struct ctf_trace_class_env_entry
*env_entry
;
4855 if (ctx
->ctf_tc
->name
->len
> 0) {
4860 name
= g_string_new(NULL
);
4862 BT_LOGE_STR("Failed to allocate a GString.");
4868 * Check if we have a trace environment string value named `hostname`.
4869 * If so, use it as the trace name's prefix.
4871 env_entry
= ctf_trace_class_borrow_env_entry_by_name(ctx
->ctf_tc
,
4874 env_entry
->type
== CTF_TRACE_CLASS_ENV_ENTRY_TYPE_STR
) {
4875 g_string_append(name
, env_entry
->value
.str
->str
);
4877 if (ctx
->trace_class_name_suffix
) {
4878 g_string_append_c(name
, G_DIR_SEPARATOR
);
4882 if (ctx
->trace_class_name_suffix
) {
4883 g_string_append(name
, ctx
->trace_class_name_suffix
);
4886 g_string_assign(ctx
->ctf_tc
->name
, name
->str
);
4891 g_string_free(name
, TRUE
);
4898 struct ctf_visitor_generate_ir
*ctf_visitor_generate_ir_create(
4899 const struct ctf_metadata_decoder_config
*decoder_config
,
4902 struct ctx
*ctx
= NULL
;
4904 /* Create visitor's context */
4905 ctx
= ctx_create(decoder_config
, name
);
4907 BT_LOGE_STR("Cannot create visitor's context.");
4918 return (void *) ctx
;
4922 void ctf_visitor_generate_ir_destroy(struct ctf_visitor_generate_ir
*visitor
)
4924 ctx_destroy((void *) visitor
);
4928 struct bt_trace
*ctf_visitor_generate_ir_get_ir_trace(
4929 struct ctf_visitor_generate_ir
*visitor
)
4931 struct ctx
*ctx
= (void *) visitor
;
4934 BT_ASSERT(ctx
->trace
);
4935 return bt_get(ctx
->trace
);
4939 struct ctf_trace_class
*ctf_visitor_generate_ir_borrow_ctf_trace_class(
4940 struct ctf_visitor_generate_ir
*visitor
)
4942 struct ctx
*ctx
= (void *) visitor
;
4945 BT_ASSERT(ctx
->ctf_tc
);
4950 int ctf_visitor_generate_ir_visit_node(struct ctf_visitor_generate_ir
*visitor
,
4951 struct ctf_node
*node
)
4954 struct ctx
*ctx
= (void *) visitor
;
4956 BT_LOGI_STR("Visiting metadata's AST to generate CTF IR objects.");
4958 switch (node
->type
) {
4961 struct ctf_node
*iter
;
4962 bool got_trace_decl
= false;
4965 * The first thing we need is the native byte order of
4966 * the trace block, because early type aliases can have
4967 * a `byte_order` attribute set to `native`. If we don't
4968 * have the native byte order yet, and we don't have any
4969 * trace block yet, then fail with EINCOMPLETE.
4971 if (ctx
->ctf_tc
->default_byte_order
== -1) {
4972 bt_list_for_each_entry(iter
, &node
->u
.root
.trace
, siblings
) {
4973 if (got_trace_decl
) {
4975 "Duplicate trace (`trace` block).");
4980 ret
= set_trace_byte_order(ctx
, iter
);
4983 "Cannot set trace's native byte order: "
4988 got_trace_decl
= true;
4991 if (!got_trace_decl
) {
4992 BT_LOGD_STR("Incomplete AST: need trace (`trace` block).");
4998 BT_ASSERT(ctx
->ctf_tc
->default_byte_order
== CTF_BYTE_ORDER_LITTLE
||
4999 ctx
->ctf_tc
->default_byte_order
== CTF_BYTE_ORDER_BIG
);
5000 BT_ASSERT(ctx
->current_scope
&&
5001 ctx
->current_scope
->parent_scope
== NULL
);
5004 bt_list_for_each_entry(iter
, &node
->u
.root
.env
, siblings
) {
5005 ret
= visit_env(ctx
, iter
);
5008 "Cannot visit trace's environment (`env` block) entry: "
5014 BT_ASSERT(ctx
->current_scope
&&
5015 ctx
->current_scope
->parent_scope
== NULL
);
5018 * Visit clock blocks.
5020 bt_list_for_each_entry(iter
, &node
->u
.root
.clock
, siblings
) {
5021 ret
= visit_clock_decl(ctx
, iter
);
5024 "Cannot visit clock class: ret=%d",
5030 BT_ASSERT(ctx
->current_scope
&&
5031 ctx
->current_scope
->parent_scope
== NULL
);
5034 * Visit root declarations next, as they can be used by any
5037 bt_list_for_each_entry(iter
, &node
->u
.root
.declaration_list
,
5039 ret
= visit_root_decl(ctx
, iter
);
5042 "Cannot visit root entry: ret=%d",
5048 BT_ASSERT(ctx
->current_scope
&&
5049 ctx
->current_scope
->parent_scope
== NULL
);
5051 /* Callsite blocks are not supported */
5052 bt_list_for_each_entry(iter
, &node
->u
.root
.callsite
, siblings
) {
5054 "\"callsite\" blocks are not supported as of this version.");
5057 BT_ASSERT(ctx
->current_scope
&&
5058 ctx
->current_scope
->parent_scope
== NULL
);
5061 bt_list_for_each_entry(iter
, &node
->u
.root
.trace
, siblings
) {
5062 ret
= visit_trace_decl(ctx
, iter
);
5065 "Cannot visit trace (`trace` block): "
5071 BT_ASSERT(ctx
->current_scope
&&
5072 ctx
->current_scope
->parent_scope
== NULL
);
5075 bt_list_for_each_entry(iter
, &node
->u
.root
.stream
, siblings
) {
5076 ret
= visit_stream_decl(ctx
, iter
);
5079 "Cannot visit stream class: ret=%d",
5085 BT_ASSERT(ctx
->current_scope
&&
5086 ctx
->current_scope
->parent_scope
== NULL
);
5089 bt_list_for_each_entry(iter
, &node
->u
.root
.event
, siblings
) {
5090 ret
= visit_event_decl(ctx
, iter
);
5093 "Cannot visit event class: ret=%d",
5099 BT_ASSERT(ctx
->current_scope
&&
5100 ctx
->current_scope
->parent_scope
== NULL
);
5105 "Unexpected node type: node-type=%d",
5111 /* Update default clock classes */
5112 ret
= ctf_trace_class_update_default_clock_classes(ctx
->ctf_tc
);
5118 /* Set trace's name, if not already done */
5119 ret
= try_set_trace_class_name(ctx
);
5125 /* Update trace class meanings */
5126 ret
= ctf_trace_class_update_meanings(ctx
->ctf_tc
);
5132 /* Update text arrays and sequences */
5133 ret
= ctf_trace_class_update_text_array_sequence(ctx
->ctf_tc
);
5139 /* Resolve sequence lengths and variant tags */
5140 ret
= ctf_trace_class_resolve_field_types(ctx
->ctf_tc
);
5146 /* Update "in IR" for field types */
5147 ret
= ctf_trace_class_update_in_ir(ctx
->ctf_tc
);
5153 /* Update saved value indexes */
5154 ret
= ctf_trace_class_update_value_storing_indexes(ctx
->ctf_tc
);
5160 /* Validate what we have so far */
5161 ret
= ctf_trace_class_validate(ctx
->ctf_tc
);
5167 /* Copy new CTF metadata -> new IR metadata */
5168 ret
= ctf_trace_class_translate(ctx
->trace
, ctx
->ctf_tc
);