2 * SPDX-License-Identifier: MIT
4 * Copyright 2010 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * Copyright 2015-2018 Philippe Proulx <philippe.proulx@efficios.com>
7 * Common Trace Format metadata visitor (generates CTF IR objects).
10 #define BT_COMP_LOG_SELF_COMP (ctx->log_cfg.self_comp)
11 #define BT_COMP_LOG_SELF_COMP_CLASS (ctx->log_cfg.self_comp_class)
12 #define BT_LOG_OUTPUT_LEVEL (ctx->log_cfg.log_level)
13 #define BT_LOG_TAG "PLUGIN/CTF/META/IR-VISITOR"
14 #include "logging/comp-logging.h"
22 #include "common/assert.h"
27 #include "common/common.h"
28 #include "common/uuid.h"
29 #include "compat/endian.h"
30 #include <babeltrace2/babeltrace.h>
32 #include "logging.hpp"
33 #include "scanner.hpp"
35 #include "decoder.hpp"
36 #include "ctf-meta.hpp"
37 #include "ctf-meta-visitors.hpp"
39 /* Bit value (left shift) */
40 #define _BV(_val) (1 << (_val))
42 /* Bit is set in a set of bits */
43 #define _IS_SET(_set, _mask) (*(_set) & (_mask))
45 /* Set bit in a set of bits */
46 #define _SET(_set, _mask) (*(_set) |= (_mask))
48 /* Try to push scope, or go to the `error` label */
49 #define _TRY_PUSH_SCOPE_OR_GOTO_ERROR() \
51 ret = ctx_push_scope(ctx); \
53 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Cannot push scope."); \
58 /* Bits for verifying existing attributes in various declarations */
61 _CLOCK_NAME_SET
= _BV(0),
62 _CLOCK_UUID_SET
= _BV(1),
63 _CLOCK_FREQ_SET
= _BV(2),
64 _CLOCK_PRECISION_SET
= _BV(3),
65 _CLOCK_OFFSET_S_SET
= _BV(4),
66 _CLOCK_OFFSET_SET
= _BV(5),
67 _CLOCK_ABSOLUTE_SET
= _BV(6),
68 _CLOCK_DESCRIPTION_SET
= _BV(7),
73 _INTEGER_ALIGN_SET
= _BV(0),
74 _INTEGER_SIZE_SET
= _BV(1),
75 _INTEGER_BASE_SET
= _BV(2),
76 _INTEGER_ENCODING_SET
= _BV(3),
77 _INTEGER_BYTE_ORDER_SET
= _BV(4),
78 _INTEGER_SIGNED_SET
= _BV(5),
79 _INTEGER_MAP_SET
= _BV(6),
84 _FLOAT_ALIGN_SET
= _BV(0),
85 _FLOAT_MANT_DIG_SET
= _BV(1),
86 _FLOAT_EXP_DIG_SET
= _BV(2),
87 _FLOAT_BYTE_ORDER_SET
= _BV(3),
92 _STRING_ENCODING_SET
= _BV(0),
97 _TRACE_MINOR_SET
= _BV(0),
98 _TRACE_MAJOR_SET
= _BV(1),
99 _TRACE_BYTE_ORDER_SET
= _BV(2),
100 _TRACE_UUID_SET
= _BV(3),
101 _TRACE_PACKET_HEADER_SET
= _BV(4),
106 _STREAM_ID_SET
= _BV(0),
107 _STREAM_PACKET_CONTEXT_SET
= _BV(1),
108 _STREAM_EVENT_HEADER_SET
= _BV(2),
109 _STREAM_EVENT_CONTEXT_SET
= _BV(3),
114 _EVENT_NAME_SET
= _BV(0),
115 _EVENT_ID_SET
= _BV(1),
116 _EVENT_MODEL_EMF_URI_SET
= _BV(2),
117 _EVENT_STREAM_ID_SET
= _BV(3),
118 _EVENT_LOG_LEVEL_SET
= _BV(4),
119 _EVENT_CONTEXT_SET
= _BV(5),
120 _EVENT_FIELDS_SET
= _BV(6),
129 LOG_LEVEL_WARNING
= 4,
130 LOG_LEVEL_NOTICE
= 5,
132 LOG_LEVEL_DEBUG_SYSTEM
= 7,
133 LOG_LEVEL_DEBUG_PROGRAM
= 8,
134 LOG_LEVEL_DEBUG_PROCESS
= 9,
135 LOG_LEVEL_DEBUG_MODULE
= 10,
136 LOG_LEVEL_DEBUG_UNIT
= 11,
137 LOG_LEVEL_DEBUG_FUNCTION
= 12,
138 LOG_LEVEL_DEBUG_LINE
= 13,
139 LOG_LEVEL_DEBUG
= 14,
143 /* Prefixes of class aliases */
144 #define _PREFIX_ALIAS 'a'
145 #define _PREFIX_ENUM 'e'
146 #define _PREFIX_STRUCT 's'
147 #define _PREFIX_VARIANT 'v'
149 /* First entry in a BT list */
150 #define _BT_LIST_FIRST_ENTRY(_ptr, _class, _member) bt_list_entry((_ptr)->next, _class, _member)
152 #define _BT_COMP_LOGE_APPEND_CAUSE_DUP_ATTR(_node, _attr, _entity) \
153 _BT_COMP_LOGE_APPEND_CAUSE_LINENO( \
154 (_node)->lineno, "Duplicate attribute in %s: attr-name=\"%s\"", _entity, _attr)
156 #define _BT_COMP_LOGE_NODE(_node, _msg, args...) _BT_COMP_LOGE_LINENO((_node)->lineno, _msg, ##args)
158 #define _BT_COMP_LOGE_APPEND_CAUSE_NODE(_node, _msg, args...) \
159 _BT_COMP_LOGE_APPEND_CAUSE_LINENO((_node)->lineno, _msg, ##args)
161 #define _BT_COMP_LOGW_NODE(_node, _msg, args...) _BT_COMP_LOGW_LINENO((_node)->lineno, _msg, ##args)
163 #define _BT_COMP_LOGT_NODE(_node, _msg, args...) _BT_COMP_LOGT_LINENO((_node)->lineno, _msg, ##args)
166 * Declaration scope of a visitor context. This represents a TSDL
167 * lexical scope, so that aliases and named structures, variants,
168 * and enumerations may be registered and looked up hierarchically.
170 struct ctx_decl_scope
173 * Alias name to field class.
175 * GQuark -> struct ctf_field_class * (owned by this)
177 GHashTable
*decl_map
;
179 /* Parent scope; NULL if this is the root declaration scope */
180 struct ctx_decl_scope
*parent_scope
;
184 * Visitor context (private).
186 struct ctf_visitor_generate_ir
188 struct meta_log_config log_cfg
;
190 /* Trace IR trace class being filled (owned by this) */
191 bt_trace_class
*trace_class
;
193 /* CTF meta trace being filled (owned by this) */
194 struct ctf_trace_class
*ctf_tc
;
196 /* Current declaration scope (top of the stack) (owned by this) */
197 struct ctx_decl_scope
*current_scope
;
199 /* True if trace declaration is visited */
200 bool is_trace_visited
;
202 /* True if this is an LTTng trace */
205 /* Config passed by the user */
206 struct ctf_metadata_decoder_config decoder_config
;
212 struct ctf_visitor_generate_ir
;
215 * Creates a new declaration scope.
217 * @param par_scope Parent scope (NULL if creating a root scope)
218 * @returns New declaration scope, or NULL on error
220 static struct ctx_decl_scope
*ctx_decl_scope_create(struct ctf_visitor_generate_ir
*ctx
,
221 struct ctx_decl_scope
*par_scope
)
223 struct ctx_decl_scope
*scope
;
225 scope
= g_new(struct ctx_decl_scope
, 1);
227 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Failed to allocate one declaration scope.");
231 scope
->decl_map
= g_hash_table_new_full(g_direct_hash
, g_direct_equal
, NULL
,
232 (GDestroyNotify
) ctf_field_class_destroy
);
233 scope
->parent_scope
= par_scope
;
240 * Destroys a declaration scope.
242 * This function does not destroy the parent scope.
244 * @param scope Scope to destroy
246 static void ctx_decl_scope_destroy(struct ctx_decl_scope
*scope
)
252 g_hash_table_destroy(scope
->decl_map
);
260 * Returns the GQuark of a prefixed alias.
262 * @param prefix Prefix character
264 * @returns Associated GQuark, or 0 on error
266 static GQuark
get_prefixed_named_quark(struct ctf_visitor_generate_ir
*ctx
, char prefix
,
270 std::string prname
= std::string
{prefix
} + name
;
271 return g_quark_from_string(prname
.c_str());
275 * Looks up a prefixed class alias within a declaration scope.
277 * @param scope Declaration scope
278 * @param prefix Prefix character
279 * @param name Alias name
280 * @param levels Number of levels to dig into (-1 means infinite)
281 * @param copy True to return a copy
282 * @returns Declaration (owned by caller if \p copy is true),
283 * or NULL if not found
285 static struct ctf_field_class
*
286 ctx_decl_scope_lookup_prefix_alias(struct ctf_visitor_generate_ir
*ctx
,
287 struct ctx_decl_scope
*scope
, char prefix
, const char *name
,
288 int levels
, bool copy
)
292 struct ctf_field_class
*decl
= NULL
;
293 struct ctx_decl_scope
*cur_scope
= scope
;
297 qname
= get_prefixed_named_quark(ctx
, prefix
, name
);
306 while (cur_scope
&& cur_levels
< levels
) {
307 decl
= (ctf_field_class
*) g_hash_table_lookup(cur_scope
->decl_map
,
308 (gconstpointer
) GUINT_TO_POINTER(qname
));
310 /* Caller's reference */
312 decl
= ctf_field_class_copy(decl
);
319 cur_scope
= cur_scope
->parent_scope
;
328 * Looks up a class alias within a declaration scope.
330 * @param scope Declaration scope
331 * @param name Alias name
332 * @param levels Number of levels to dig into (-1 means infinite)
333 * @param copy True to return a copy
334 * @returns Declaration (owned by caller if \p copy is true),
335 * or NULL if not found
337 static struct ctf_field_class
*ctx_decl_scope_lookup_alias(struct ctf_visitor_generate_ir
*ctx
,
338 struct ctx_decl_scope
*scope
,
339 const char *name
, int levels
, bool copy
)
341 return ctx_decl_scope_lookup_prefix_alias(ctx
, scope
, _PREFIX_ALIAS
, name
, levels
, copy
);
345 * Looks up an enumeration within a declaration scope.
347 * @param scope Declaration scope
348 * @param name Enumeration name
349 * @param levels Number of levels to dig into (-1 means infinite)
350 * @param copy True to return a copy
351 * @returns Declaration (owned by caller if \p copy is true),
352 * or NULL if not found
354 static struct ctf_field_class_enum
*ctx_decl_scope_lookup_enum(struct ctf_visitor_generate_ir
*ctx
,
355 struct ctx_decl_scope
*scope
,
356 const char *name
, int levels
,
359 return ctf_field_class_as_enum(
360 ctx_decl_scope_lookup_prefix_alias(ctx
, scope
, _PREFIX_ENUM
, name
, levels
, copy
));
364 * Looks up a structure within a declaration scope.
366 * @param scope Declaration scope
367 * @param name Structure name
368 * @param levels Number of levels to dig into (-1 means infinite)
369 * @param copy True to return a copy
370 * @returns Declaration (owned by caller if \p copy is true),
371 * or NULL if not found
373 static struct ctf_field_class_struct
*
374 ctx_decl_scope_lookup_struct(struct ctf_visitor_generate_ir
*ctx
, struct ctx_decl_scope
*scope
,
375 const char *name
, int levels
, bool copy
)
377 return ctf_field_class_as_struct(
378 ctx_decl_scope_lookup_prefix_alias(ctx
, scope
, _PREFIX_STRUCT
, name
, levels
, copy
));
382 * Looks up a variant within a declaration scope.
384 * @param scope Declaration scope
385 * @param name Variant name
386 * @param levels Number of levels to dig into (-1 means infinite)
387 * @param copy True to return a copy
388 * @returns Declaration (owned by caller if \p copy is true),
389 * or NULL if not found
391 static struct ctf_field_class_variant
*
392 ctx_decl_scope_lookup_variant(struct ctf_visitor_generate_ir
*ctx
, struct ctx_decl_scope
*scope
,
393 const char *name
, int levels
, bool copy
)
395 return ctf_field_class_as_variant(
396 ctx_decl_scope_lookup_prefix_alias(ctx
, scope
, _PREFIX_VARIANT
, name
, levels
, copy
));
400 * Registers a prefixed class alias within a declaration scope.
402 * @param scope Declaration scope
403 * @param prefix Prefix character
404 * @param name Alias name (non-NULL)
405 * @param decl Field class to register (copied)
406 * @returns 0 if registration went okay, negative value otherwise
408 static int ctx_decl_scope_register_prefix_alias(struct ctf_visitor_generate_ir
*ctx
,
409 struct ctx_decl_scope
*scope
, char prefix
,
410 const char *name
, struct ctf_field_class
*decl
)
418 qname
= get_prefixed_named_quark(ctx
, prefix
, name
);
424 /* Make sure alias does not exist in local scope */
425 if (ctx_decl_scope_lookup_prefix_alias(ctx
, scope
, prefix
, name
, 1, false)) {
430 decl
= ctf_field_class_copy(decl
);
432 g_hash_table_insert(scope
->decl_map
, GUINT_TO_POINTER(qname
), decl
);
439 * Registers a class alias within a declaration scope.
441 * @param scope Declaration scope
442 * @param name Alias name (non-NULL)
443 * @param decl Field class to register (copied)
444 * @returns 0 if registration went okay, negative value otherwise
446 static int ctx_decl_scope_register_alias(struct ctf_visitor_generate_ir
*ctx
,
447 struct ctx_decl_scope
*scope
, const char *name
,
448 struct ctf_field_class
*decl
)
450 return ctx_decl_scope_register_prefix_alias(ctx
, scope
, _PREFIX_ALIAS
, name
, decl
);
454 * Registers an enumeration declaration within a declaration scope.
456 * @param scope Declaration scope
457 * @param name Enumeration name (non-NULL)
458 * @param decl Enumeration field class to register (copied)
459 * @returns 0 if registration went okay, negative value otherwise
461 static int ctx_decl_scope_register_enum(struct ctf_visitor_generate_ir
*ctx
,
462 struct ctx_decl_scope
*scope
, const char *name
,
463 struct ctf_field_class_enum
*decl
)
465 return ctx_decl_scope_register_prefix_alias(ctx
, scope
, _PREFIX_ENUM
, name
,
466 &decl
->base
.base
.base
);
470 * Registers a structure declaration within a declaration scope.
472 * @param scope Declaration scope
473 * @param name Structure name (non-NULL)
474 * @param decl Structure field class to register (copied)
475 * @returns 0 if registration went okay, negative value otherwise
477 static int ctx_decl_scope_register_struct(struct ctf_visitor_generate_ir
*ctx
,
478 struct ctx_decl_scope
*scope
, const char *name
,
479 struct ctf_field_class_struct
*decl
)
481 return ctx_decl_scope_register_prefix_alias(ctx
, scope
, _PREFIX_STRUCT
, name
, &decl
->base
);
485 * Registers a variant declaration within a declaration scope.
487 * @param scope Declaration scope
488 * @param name Variant name (non-NULL)
489 * @param decl Variant field class to register
490 * @returns 0 if registration went okay, negative value otherwise
492 static int ctx_decl_scope_register_variant(struct ctf_visitor_generate_ir
*ctx
,
493 struct ctx_decl_scope
*scope
, const char *name
,
494 struct ctf_field_class_variant
*decl
)
496 return ctx_decl_scope_register_prefix_alias(ctx
, scope
, _PREFIX_VARIANT
, name
, &decl
->base
);
500 * Destroys a visitor context.
502 * @param ctx Visitor context to destroy
504 static void ctx_destroy(struct ctf_visitor_generate_ir
*ctx
)
506 struct ctx_decl_scope
*scope
;
512 scope
= ctx
->current_scope
;
515 * Destroy all scopes, from current one to the root scope.
518 struct ctx_decl_scope
*parent_scope
= scope
->parent_scope
;
520 ctx_decl_scope_destroy(scope
);
521 scope
= parent_scope
;
524 bt_trace_class_put_ref(ctx
->trace_class
);
527 ctf_trace_class_destroy(ctx
->ctf_tc
);
537 * Creates a new visitor context.
539 * @param trace Associated trace
540 * @returns New visitor context, or NULL on error
542 static struct ctf_visitor_generate_ir
*
543 ctx_create(const struct ctf_metadata_decoder_config
*decoder_config
)
545 struct ctf_visitor_generate_ir
*ctx
= NULL
;
547 BT_ASSERT(decoder_config
);
549 ctx
= g_new0(struct ctf_visitor_generate_ir
, 1);
551 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR
, decoder_config
->log_level
, decoder_config
->self_comp
,
552 "Failed to allocate one visitor context.");
556 ctx
->log_cfg
.log_level
= decoder_config
->log_level
;
557 ctx
->log_cfg
.self_comp
= decoder_config
->self_comp
;
558 ctx
->log_cfg
.self_comp_class
= decoder_config
->self_comp_class
;
560 if (decoder_config
->self_comp
) {
561 ctx
->trace_class
= bt_trace_class_create(decoder_config
->self_comp
);
562 if (!ctx
->trace_class
) {
563 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Cannot create empty trace class.");
568 ctx
->ctf_tc
= ctf_trace_class_create();
570 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Cannot create CTF trace class.");
574 /* Root declaration scope */
575 ctx
->current_scope
= ctx_decl_scope_create(ctx
, NULL
);
576 if (!ctx
->current_scope
) {
577 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Cannot create declaration scope.");
581 ctx
->decoder_config
= *decoder_config
;
593 * Pushes a new declaration scope on top of a visitor context's
594 * declaration scope stack.
596 * @param ctx Visitor context
597 * @returns 0 on success, or a negative value on error
599 static int ctx_push_scope(struct ctf_visitor_generate_ir
*ctx
)
602 struct ctx_decl_scope
*new_scope
;
605 new_scope
= ctx_decl_scope_create(ctx
, ctx
->current_scope
);
607 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Cannot create declaration scope.");
612 ctx
->current_scope
= new_scope
;
618 static void ctx_pop_scope(struct ctf_visitor_generate_ir
*ctx
)
620 struct ctx_decl_scope
*parent_scope
= NULL
;
624 if (!ctx
->current_scope
) {
628 parent_scope
= ctx
->current_scope
->parent_scope
;
629 ctx_decl_scope_destroy(ctx
->current_scope
);
630 ctx
->current_scope
= parent_scope
;
636 static int visit_field_class_specifier_list(struct ctf_visitor_generate_ir
*ctx
,
637 struct ctf_node
*ts_list
,
638 struct ctf_field_class
**decl
);
640 static int is_unary_string(struct bt_list_head
*head
)
643 struct ctf_node
*node
;
645 bt_list_for_each_entry (node
, head
, siblings
) {
646 if (node
->type
!= NODE_UNARY_EXPRESSION
) {
650 if (node
->u
.unary_expression
.type
!= UNARY_STRING
) {
658 static const char *get_map_clock_name_value(struct bt_list_head
*head
)
661 struct ctf_node
*node
;
662 const char *name
= NULL
;
664 bt_list_for_each_entry (node
, head
, siblings
) {
666 int uexpr_type
= node
->u
.unary_expression
.type
;
667 int uexpr_link
= node
->u
.unary_expression
.link
;
668 int cond
= node
->type
!= NODE_UNARY_EXPRESSION
|| uexpr_type
!= UNARY_STRING
||
669 !((uexpr_link
!= UNARY_LINK_UNKNOWN
) ^ (i
== 0));
674 /* Needs to be chained with . */
675 switch (node
->u
.unary_expression
.link
) {
678 case UNARY_ARROWLINK
:
679 case UNARY_DOTDOTDOT
:
685 src_string
= node
->u
.unary_expression
.u
.string
;
689 if (strcmp("clock", src_string
)) {
697 if (strcmp("value", src_string
)) {
702 /* Extra identifier, unknown */
715 static int is_unary_unsigned(struct bt_list_head
*head
)
718 struct ctf_node
*node
;
720 bt_list_for_each_entry (node
, head
, siblings
) {
721 if (node
->type
!= NODE_UNARY_EXPRESSION
) {
725 if (node
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
733 static int get_unary_unsigned(struct ctf_visitor_generate_ir
*ctx
, struct bt_list_head
*head
,
738 struct ctf_node
*node
;
742 if (bt_list_empty(head
)) {
747 bt_list_for_each_entry (node
, head
, siblings
) {
748 int uexpr_type
= node
->u
.unary_expression
.type
;
749 int uexpr_link
= node
->u
.unary_expression
.link
;
750 int cond
= node
->type
!= NODE_UNARY_EXPRESSION
|| uexpr_type
!= UNARY_UNSIGNED_CONSTANT
||
751 uexpr_link
!= UNARY_LINK_UNKNOWN
|| i
!= 0;
753 _BT_COMP_LOGE_APPEND_CAUSE_NODE(node
, "Invalid constant unsigned integer.");
758 *value
= node
->u
.unary_expression
.u
.unsigned_constant
;
766 static int is_unary_signed(struct bt_list_head
*head
)
769 struct ctf_node
*node
;
771 bt_list_for_each_entry (node
, head
, siblings
) {
772 if (node
->type
!= NODE_UNARY_EXPRESSION
) {
776 if (node
->u
.unary_expression
.type
!= UNARY_SIGNED_CONSTANT
) {
784 static int get_unary_signed(struct bt_list_head
*head
, int64_t *value
)
788 struct ctf_node
*node
;
790 bt_list_for_each_entry (node
, head
, siblings
) {
791 int uexpr_type
= node
->u
.unary_expression
.type
;
792 int uexpr_link
= node
->u
.unary_expression
.link
;
793 int cond
= node
->type
!= NODE_UNARY_EXPRESSION
||
794 (uexpr_type
!= UNARY_UNSIGNED_CONSTANT
&& uexpr_type
!= UNARY_SIGNED_CONSTANT
) ||
795 uexpr_link
!= UNARY_LINK_UNKNOWN
|| i
!= 0;
801 switch (uexpr_type
) {
802 case UNARY_UNSIGNED_CONSTANT
:
803 *value
= (int64_t) node
->u
.unary_expression
.u
.unsigned_constant
;
805 case UNARY_SIGNED_CONSTANT
:
806 *value
= node
->u
.unary_expression
.u
.signed_constant
;
820 static int get_unary_uuid(struct ctf_visitor_generate_ir
*ctx
, struct bt_list_head
*head
,
823 return ctf_ast_get_unary_uuid(head
, uuid
, ctx
->log_cfg
.log_level
, ctx
->log_cfg
.self_comp
);
826 static int get_boolean(struct ctf_visitor_generate_ir
*ctx
, struct ctf_node
*unary_expr
)
830 if (unary_expr
->type
!= NODE_UNARY_EXPRESSION
) {
831 _BT_COMP_LOGE_APPEND_CAUSE_NODE(unary_expr
, "Expecting unary expression: node-type=%d",
837 switch (unary_expr
->u
.unary_expression
.type
) {
838 case UNARY_UNSIGNED_CONSTANT
:
839 ret
= (unary_expr
->u
.unary_expression
.u
.unsigned_constant
!= 0);
841 case UNARY_SIGNED_CONSTANT
:
842 ret
= (unary_expr
->u
.unary_expression
.u
.signed_constant
!= 0);
846 const char *str
= unary_expr
->u
.unary_expression
.u
.string
;
848 if (strcmp(str
, "true") == 0 || strcmp(str
, "TRUE") == 0) {
850 } else if (strcmp(str
, "false") == 0 || strcmp(str
, "FALSE") == 0) {
853 _BT_COMP_LOGE_APPEND_CAUSE_NODE(unary_expr
, "Unexpected boolean value: value=\"%s\"",
861 _BT_COMP_LOGE_APPEND_CAUSE_NODE(unary_expr
,
862 "Unexpected unary expression type: node-type=%d",
863 unary_expr
->u
.unary_expression
.type
);
872 static enum ctf_byte_order
byte_order_from_unary_expr(struct ctf_visitor_generate_ir
*ctx
,
873 struct ctf_node
*unary_expr
)
876 enum ctf_byte_order bo
= CTF_BYTE_ORDER_UNKNOWN
;
878 if (unary_expr
->u
.unary_expression
.type
!= UNARY_STRING
) {
879 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
880 unary_expr
, "\"byte_order\" attribute: expecting `be`, `le`, `network`, or `native`.");
884 str
= unary_expr
->u
.unary_expression
.u
.string
;
886 if (strcmp(str
, "be") == 0 || strcmp(str
, "network") == 0) {
887 bo
= CTF_BYTE_ORDER_BIG
;
888 } else if (strcmp(str
, "le") == 0) {
889 bo
= CTF_BYTE_ORDER_LITTLE
;
890 } else if (strcmp(str
, "native") == 0) {
891 bo
= CTF_BYTE_ORDER_DEFAULT
;
893 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
895 "Unexpected \"byte_order\" attribute value: "
896 "expecting `be`, `le`, `network`, or `native`: value=\"%s\"",
905 static enum ctf_byte_order
get_real_byte_order(struct ctf_visitor_generate_ir
*ctx
,
906 struct ctf_node
*uexpr
)
908 enum ctf_byte_order bo
= byte_order_from_unary_expr(ctx
, uexpr
);
910 if (bo
== CTF_BYTE_ORDER_DEFAULT
) {
911 bo
= ctx
->ctf_tc
->default_byte_order
;
917 static int is_align_valid(uint64_t align
)
919 return (align
!= 0) && !(align
& (align
- UINT64_C(1)));
922 static int get_class_specifier_name(struct ctf_visitor_generate_ir
*ctx
,
923 struct ctf_node
*cls_specifier
, GString
*str
)
927 if (cls_specifier
->type
!= NODE_TYPE_SPECIFIER
) {
928 _BT_COMP_LOGE_APPEND_CAUSE_NODE(cls_specifier
, "Unexpected node type: node-type=%d",
929 cls_specifier
->type
);
934 switch (cls_specifier
->u
.field_class_specifier
.type
) {
936 g_string_append(str
, "void");
939 g_string_append(str
, "char");
942 g_string_append(str
, "short");
945 g_string_append(str
, "int");
948 g_string_append(str
, "long");
951 g_string_append(str
, "float");
953 case TYPESPEC_DOUBLE
:
954 g_string_append(str
, "double");
956 case TYPESPEC_SIGNED
:
957 g_string_append(str
, "signed");
959 case TYPESPEC_UNSIGNED
:
960 g_string_append(str
, "unsigned");
963 g_string_append(str
, "bool");
965 case TYPESPEC_COMPLEX
:
966 g_string_append(str
, "_Complex");
968 case TYPESPEC_IMAGINARY
:
969 g_string_append(str
, "_Imaginary");
972 g_string_append(str
, "const");
974 case TYPESPEC_ID_TYPE
:
975 if (cls_specifier
->u
.field_class_specifier
.id_type
) {
976 g_string_append(str
, cls_specifier
->u
.field_class_specifier
.id_type
);
979 case TYPESPEC_STRUCT
:
981 struct ctf_node
*node
= cls_specifier
->u
.field_class_specifier
.node
;
983 if (!node
->u
._struct
.name
) {
984 _BT_COMP_LOGE_APPEND_CAUSE_NODE(node
, "Unexpected empty structure field class name.");
989 g_string_append(str
, "struct ");
990 g_string_append(str
, node
->u
._struct
.name
);
993 case TYPESPEC_VARIANT
:
995 struct ctf_node
*node
= cls_specifier
->u
.field_class_specifier
.node
;
997 if (!node
->u
.variant
.name
) {
998 _BT_COMP_LOGE_APPEND_CAUSE_NODE(node
, "Unexpected empty variant field class name.");
1003 g_string_append(str
, "variant ");
1004 g_string_append(str
, node
->u
.variant
.name
);
1009 struct ctf_node
*node
= cls_specifier
->u
.field_class_specifier
.node
;
1011 if (!node
->u
._enum
.enum_id
) {
1012 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
1013 node
, "Unexpected empty enumeration field class (`enum`) name.");
1018 g_string_append(str
, "enum ");
1019 g_string_append(str
, node
->u
._enum
.enum_id
);
1022 case TYPESPEC_FLOATING_POINT
:
1023 case TYPESPEC_INTEGER
:
1024 case TYPESPEC_STRING
:
1026 _BT_COMP_LOGE_APPEND_CAUSE_NODE(cls_specifier
->u
.field_class_specifier
.node
,
1027 "Unexpected field class specifier type: %d",
1028 cls_specifier
->u
.field_class_specifier
.type
);
1037 static int get_class_specifier_list_name(struct ctf_visitor_generate_ir
*ctx
,
1038 struct ctf_node
*cls_specifier_list
, GString
*str
)
1041 struct ctf_node
*iter
;
1042 int alias_item_nr
= 0;
1043 struct bt_list_head
*head
= &cls_specifier_list
->u
.field_class_specifier_list
.head
;
1045 bt_list_for_each_entry (iter
, head
, siblings
) {
1046 if (alias_item_nr
!= 0) {
1047 g_string_append(str
, " ");
1051 ret
= get_class_specifier_name(ctx
, iter
, str
);
1061 static GQuark
create_class_alias_identifier(struct ctf_visitor_generate_ir
*ctx
,
1062 struct ctf_node
*cls_specifier_list
,
1063 struct ctf_node
*node_field_class_declarator
)
1069 struct ctf_node
*iter
;
1070 struct bt_list_head
*pointers
= &node_field_class_declarator
->u
.field_class_declarator
.pointers
;
1072 str
= g_string_new("");
1073 ret
= get_class_specifier_list_name(ctx
, cls_specifier_list
, str
);
1075 g_string_free(str
, TRUE
);
1079 bt_list_for_each_entry (iter
, pointers
, siblings
) {
1080 g_string_append(str
, " *");
1082 if (iter
->u
.pointer
.const_qualifier
) {
1083 g_string_append(str
, " const");
1087 str_c
= g_string_free(str
, FALSE
);
1088 qalias
= g_quark_from_string(str_c
);
1095 static int visit_field_class_declarator(struct ctf_visitor_generate_ir
*ctx
,
1096 struct ctf_node
*cls_specifier_list
, GQuark
*field_name
,
1097 struct ctf_node
*node_field_class_declarator
,
1098 struct ctf_field_class
**field_decl
,
1099 struct ctf_field_class
*nested_decl
)
1102 * During this whole function, nested_decl is always OURS,
1103 * whereas field_decl is an output which we create, but
1104 * belongs to the caller (it is moved).
1109 /* Validate field class declarator node */
1110 if (node_field_class_declarator
) {
1111 if (node_field_class_declarator
->u
.field_class_declarator
.type
== TYPEDEC_UNKNOWN
) {
1112 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
1113 node_field_class_declarator
, "Unexpected field class declarator type: type=%d",
1114 node_field_class_declarator
->u
.field_class_declarator
.type
);
1119 /* TODO: GCC bitfields not supported yet */
1120 if (node_field_class_declarator
->u
.field_class_declarator
.bitfield_len
!= NULL
) {
1121 _BT_COMP_LOGE_APPEND_CAUSE_NODE(node_field_class_declarator
,
1122 "GCC bitfields are not supported as of this version.");
1128 /* Find the right nested declaration if not provided */
1130 if (node_field_class_declarator
&&
1131 !bt_list_empty(&node_field_class_declarator
->u
.field_class_declarator
.pointers
)) {
1135 * If we have a pointer declarator, it HAS to
1136 * be present in the field class aliases (else
1140 create_class_alias_identifier(ctx
, cls_specifier_list
, node_field_class_declarator
);
1141 nested_decl
= ctx_decl_scope_lookup_alias(ctx
, ctx
->current_scope
,
1142 g_quark_to_string(qalias
), -1, true);
1144 _BT_COMP_LOGE_APPEND_CAUSE_NODE(node_field_class_declarator
,
1145 "Cannot find class alias: name=\"%s\"",
1146 g_quark_to_string(qalias
));
1151 if (nested_decl
->type
== CTF_FIELD_CLASS_TYPE_INT
) {
1152 /* Pointer: force integer's base to 16 */
1153 struct ctf_field_class_int
*int_fc
= ctf_field_class_as_int(nested_decl
);
1155 int_fc
->disp_base
= BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_HEXADECIMAL
;
1158 ret
= visit_field_class_specifier_list(ctx
, cls_specifier_list
, &nested_decl
);
1160 BT_ASSERT(!nested_decl
);
1166 BT_ASSERT(nested_decl
);
1168 if (!node_field_class_declarator
) {
1169 *field_decl
= nested_decl
;
1174 if (node_field_class_declarator
->u
.field_class_declarator
.type
== TYPEDEC_ID
) {
1175 if (node_field_class_declarator
->u
.field_class_declarator
.u
.id
) {
1176 const char *id
= node_field_class_declarator
->u
.field_class_declarator
.u
.id
;
1178 *field_name
= g_quark_from_string(id
);
1183 *field_decl
= nested_decl
;
1187 struct ctf_node
*first
;
1188 struct ctf_field_class
*decl
= NULL
;
1189 struct ctf_field_class
*outer_field_decl
= NULL
;
1190 struct bt_list_head
*length
=
1191 &node_field_class_declarator
->u
.field_class_declarator
.u
.nested
.length
;
1193 /* Create array/sequence, pass nested_decl as child */
1194 if (bt_list_empty(length
)) {
1195 _BT_COMP_LOGE_APPEND_CAUSE_NODE(node_field_class_declarator
,
1196 "Expecting length field reference or value.");
1201 first
= _BT_LIST_FIRST_ENTRY(length
, struct ctf_node
, siblings
);
1202 if (first
->type
!= NODE_UNARY_EXPRESSION
) {
1203 _BT_COMP_LOGE_APPEND_CAUSE_NODE(first
, "Unexpected node type: node-type=%d",
1209 switch (first
->u
.unary_expression
.type
) {
1210 case UNARY_UNSIGNED_CONSTANT
:
1212 struct ctf_field_class_array
*array_decl
= NULL
;
1214 array_decl
= ctf_field_class_array_create();
1215 BT_ASSERT(array_decl
);
1216 array_decl
->length
= first
->u
.unary_expression
.u
.unsigned_constant
;
1217 array_decl
->base
.elem_fc
= nested_decl
;
1219 decl
= &array_decl
->base
.base
;
1224 /* Lookup unsigned integer definition, create seq. */
1225 struct ctf_field_class_sequence
*seq_decl
= NULL
;
1226 char *length_name
= ctf_ast_concatenate_unary_strings(length
);
1229 _BT_COMP_LOGE_APPEND_CAUSE_NODE(node_field_class_declarator
,
1230 "Cannot concatenate unary strings.");
1235 if (strncmp(length_name
, "env.", 4) == 0) {
1236 /* This is, in fact, an array */
1237 const char *env_entry_name
= &length_name
[4];
1238 struct ctf_trace_class_env_entry
*env_entry
=
1239 ctf_trace_class_borrow_env_entry_by_name(ctx
->ctf_tc
, env_entry_name
);
1240 struct ctf_field_class_array
*array_decl
;
1243 _BT_COMP_LOGE_APPEND_CAUSE_NODE(node_field_class_declarator
,
1244 "Cannot find environment entry: "
1251 if (env_entry
->type
!= CTF_TRACE_CLASS_ENV_ENTRY_TYPE_INT
) {
1252 _BT_COMP_LOGE_APPEND_CAUSE_NODE(node_field_class_declarator
,
1253 "Wrong environment entry type "
1254 "(expecting integer): "
1261 if (env_entry
->value
.i
< 0) {
1262 _BT_COMP_LOGE_APPEND_CAUSE_NODE(node_field_class_declarator
,
1263 "Invalid, negative array length: "
1264 "env-entry-name=\"%s\", "
1266 env_entry_name
, env_entry
->value
.i
);
1271 array_decl
= ctf_field_class_array_create();
1272 BT_ASSERT(array_decl
);
1273 array_decl
->length
= (uint64_t) env_entry
->value
.i
;
1274 array_decl
->base
.elem_fc
= nested_decl
;
1276 decl
= &array_decl
->base
.base
;
1278 seq_decl
= ctf_field_class_sequence_create();
1279 BT_ASSERT(seq_decl
);
1280 seq_decl
->base
.elem_fc
= nested_decl
;
1282 g_string_assign(seq_decl
->length_ref
, length_name
);
1283 decl
= &seq_decl
->base
.base
;
1286 g_free(length_name
);
1294 BT_ASSERT(!nested_decl
);
1296 BT_ASSERT(!*field_decl
);
1299 * At this point, we found the next nested declaration.
1300 * We currently own this (and lost the ownership of
1301 * nested_decl in the meantime). Pass this next
1302 * nested declaration as the content of the outer
1303 * container, MOVING its ownership.
1305 ret
= visit_field_class_declarator(
1306 ctx
, cls_specifier_list
, field_name
,
1307 node_field_class_declarator
->u
.field_class_declarator
.u
.nested
.field_class_declarator
,
1308 &outer_field_decl
, decl
);
1311 BT_ASSERT(!outer_field_decl
);
1316 BT_ASSERT(outer_field_decl
);
1317 *field_decl
= outer_field_decl
;
1318 outer_field_decl
= NULL
;
1321 BT_ASSERT(*field_decl
);
1325 ctf_field_class_destroy(*field_decl
);
1333 ctf_field_class_destroy(nested_decl
);
1338 static int visit_struct_decl_field(struct ctf_visitor_generate_ir
*ctx
,
1339 struct ctf_field_class_struct
*struct_decl
,
1340 struct ctf_node
*cls_specifier_list
,
1341 struct bt_list_head
*field_class_declarators
)
1344 struct ctf_node
*iter
;
1345 struct ctf_field_class
*field_decl
= NULL
;
1347 bt_list_for_each_entry (iter
, field_class_declarators
, siblings
) {
1350 const char *field_name
;
1352 ret
= visit_field_class_declarator(ctx
, cls_specifier_list
, &qfield_name
, iter
, &field_decl
,
1355 BT_ASSERT(!field_decl
);
1356 _BT_COMP_LOGE_APPEND_CAUSE_NODE(cls_specifier_list
,
1357 "Cannot visit field class declarator: ret=%d", ret
);
1361 BT_ASSERT(field_decl
);
1362 field_name
= g_quark_to_string(qfield_name
);
1364 /* Check if field with same name already exists */
1365 if (ctf_field_class_struct_borrow_member_by_name(struct_decl
, field_name
)) {
1366 _BT_COMP_LOGE_APPEND_CAUSE_NODE(cls_specifier_list
,
1367 "Duplicate field in structure field class: "
1368 "field-name=\"%s\"",
1374 /* Add field to structure */
1375 ctf_field_class_struct_append_member(struct_decl
, field_name
, field_decl
);
1382 ctf_field_class_destroy(field_decl
);
1387 static int visit_variant_decl_field(struct ctf_visitor_generate_ir
*ctx
,
1388 struct ctf_field_class_variant
*variant_decl
,
1389 struct ctf_node
*cls_specifier_list
,
1390 struct bt_list_head
*field_class_declarators
)
1393 struct ctf_node
*iter
;
1394 struct ctf_field_class
*field_decl
= NULL
;
1396 bt_list_for_each_entry (iter
, field_class_declarators
, siblings
) {
1399 const char *field_name
;
1401 ret
= visit_field_class_declarator(ctx
, cls_specifier_list
, &qfield_name
, iter
, &field_decl
,
1404 BT_ASSERT(!field_decl
);
1405 _BT_COMP_LOGE_APPEND_CAUSE_NODE(cls_specifier_list
,
1406 "Cannot visit field class declarator: ret=%d", ret
);
1410 BT_ASSERT(field_decl
);
1411 field_name
= g_quark_to_string(qfield_name
);
1413 /* Check if field with same name already exists */
1414 if (ctf_field_class_variant_borrow_option_by_name(variant_decl
, field_name
)) {
1415 _BT_COMP_LOGE_APPEND_CAUSE_NODE(cls_specifier_list
,
1416 "Duplicate field in variant field class: "
1417 "field-name=\"%s\"",
1423 /* Add field to structure */
1424 ctf_field_class_variant_append_option(variant_decl
, field_name
, field_decl
);
1431 ctf_field_class_destroy(field_decl
);
1436 static int visit_field_class_def(struct ctf_visitor_generate_ir
*ctx
,
1437 struct ctf_node
*cls_specifier_list
,
1438 struct bt_list_head
*field_class_declarators
)
1442 struct ctf_node
*iter
;
1443 struct ctf_field_class
*class_decl
= NULL
;
1445 bt_list_for_each_entry (iter
, field_class_declarators
, siblings
) {
1446 ret
= visit_field_class_declarator(ctx
, cls_specifier_list
, &qidentifier
, iter
, &class_decl
,
1449 _BT_COMP_LOGE_APPEND_CAUSE_NODE(iter
, "Cannot visit field class declarator: ret=%d",
1455 /* Do not allow field class def and alias of untagged variants */
1456 if (class_decl
->type
== CTF_FIELD_CLASS_TYPE_VARIANT
) {
1457 struct ctf_field_class_variant
*var_fc
= ctf_field_class_as_variant(class_decl
);
1459 if (var_fc
->tag_path
.path
->len
== 0) {
1460 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
1461 iter
, "Type definition of untagged variant field class is not allowed.");
1467 ret
= ctx_decl_scope_register_alias(ctx
, ctx
->current_scope
, g_quark_to_string(qidentifier
),
1470 _BT_COMP_LOGE_APPEND_CAUSE_NODE(iter
, "Cannot register field class alias: name=\"%s\"",
1471 g_quark_to_string(qidentifier
));
1477 ctf_field_class_destroy(class_decl
);
1482 static int visit_field_class_alias(struct ctf_visitor_generate_ir
*ctx
, struct ctf_node
*target
,
1483 struct ctf_node
*alias
)
1487 struct ctf_node
*node
;
1488 GQuark qdummy_field_name
;
1489 struct ctf_field_class
*class_decl
= NULL
;
1491 /* Create target field class */
1492 if (bt_list_empty(&target
->u
.field_class_alias_target
.field_class_declarators
)) {
1495 node
= _BT_LIST_FIRST_ENTRY(&target
->u
.field_class_alias_target
.field_class_declarators
,
1496 struct ctf_node
, siblings
);
1499 ret
= visit_field_class_declarator(
1500 ctx
, target
->u
.field_class_alias_target
.field_class_specifier_list
, &qdummy_field_name
,
1501 node
, &class_decl
, NULL
);
1503 BT_ASSERT(!class_decl
);
1504 _BT_COMP_LOGE_APPEND_CAUSE_NODE(node
, "Cannot visit field class declarator: ret=%d", ret
);
1508 /* Do not allow field class def and alias of untagged variants */
1509 if (class_decl
->type
== CTF_FIELD_CLASS_TYPE_VARIANT
) {
1510 struct ctf_field_class_variant
*var_fc
= ctf_field_class_as_variant(class_decl
);
1512 if (var_fc
->tag_path
.path
->len
== 0) {
1513 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
1514 target
, "Type definition of untagged variant field class is not allowed.");
1521 * The semantic validator does not check whether the target is
1522 * abstract or not (if it has an identifier). Check it here.
1524 if (qdummy_field_name
!= 0) {
1525 _BT_COMP_LOGE_APPEND_CAUSE_NODE(target
, "Expecting empty identifier: id=\"%s\"",
1526 g_quark_to_string(qdummy_field_name
));
1531 /* Create alias identifier */
1532 node
= _BT_LIST_FIRST_ENTRY(&alias
->u
.field_class_alias_name
.field_class_declarators
,
1533 struct ctf_node
, siblings
);
1534 qalias
= create_class_alias_identifier(
1535 ctx
, alias
->u
.field_class_alias_name
.field_class_specifier_list
, node
);
1536 ret
= ctx_decl_scope_register_alias(ctx
, ctx
->current_scope
, g_quark_to_string(qalias
),
1539 _BT_COMP_LOGE_APPEND_CAUSE_NODE(node
, "Cannot register class alias: name=\"%s\"",
1540 g_quark_to_string(qalias
));
1545 ctf_field_class_destroy(class_decl
);
1550 static int visit_struct_decl_entry(struct ctf_visitor_generate_ir
*ctx
, struct ctf_node
*entry_node
,
1551 struct ctf_field_class_struct
*struct_decl
)
1555 switch (entry_node
->type
) {
1557 ret
= visit_field_class_def(ctx
, entry_node
->u
.field_class_def
.field_class_specifier_list
,
1558 &entry_node
->u
.field_class_def
.field_class_declarators
);
1560 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
1561 entry_node
, "Cannot add field class found in structure field class: ret=%d", ret
);
1565 case NODE_TYPEALIAS
:
1566 ret
= visit_field_class_alias(ctx
, entry_node
->u
.field_class_alias
.target
,
1567 entry_node
->u
.field_class_alias
.alias
);
1569 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
1570 entry_node
, "Cannot add field class alias found in structure field class: ret=%d",
1575 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
1577 ret
= visit_struct_decl_field(
1579 entry_node
->u
.struct_or_variant_declaration
.field_class_specifier_list
,
1580 &entry_node
->u
.struct_or_variant_declaration
.field_class_declarators
);
1586 _BT_COMP_LOGE_APPEND_CAUSE_NODE(entry_node
, "Unexpected node type: node-type=%d",
1596 static int visit_variant_decl_entry(struct ctf_visitor_generate_ir
*ctx
,
1597 struct ctf_node
*entry_node
,
1598 struct ctf_field_class_variant
*variant_decl
)
1602 switch (entry_node
->type
) {
1604 ret
= visit_field_class_def(ctx
, entry_node
->u
.field_class_def
.field_class_specifier_list
,
1605 &entry_node
->u
.field_class_def
.field_class_declarators
);
1607 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
1608 entry_node
, "Cannot add field class found in variant field class: ret=%d", ret
);
1612 case NODE_TYPEALIAS
:
1613 ret
= visit_field_class_alias(ctx
, entry_node
->u
.field_class_alias
.target
,
1614 entry_node
->u
.field_class_alias
.alias
);
1616 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
1617 entry_node
, "Cannot add field class alias found in variant field class: ret=%d",
1622 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
1624 ret
= visit_variant_decl_field(
1626 entry_node
->u
.struct_or_variant_declaration
.field_class_specifier_list
,
1627 &entry_node
->u
.struct_or_variant_declaration
.field_class_declarators
);
1633 _BT_COMP_LOGE_APPEND_CAUSE_NODE(entry_node
, "Unexpected node type: node-type=%d",
1643 static int visit_struct_decl(struct ctf_visitor_generate_ir
*ctx
, const char *name
,
1644 struct bt_list_head
*decl_list
, int has_body
,
1645 struct bt_list_head
*min_align
,
1646 struct ctf_field_class_struct
**struct_decl
)
1650 BT_ASSERT(struct_decl
);
1651 *struct_decl
= NULL
;
1653 /* For named struct (without body), lookup in declaration scope */
1656 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(
1657 "Bodyless structure field class: missing name.");
1662 *struct_decl
= ctx_decl_scope_lookup_struct(ctx
, ctx
->current_scope
, name
, -1, true);
1663 if (!*struct_decl
) {
1664 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(
1665 "Cannot find structure field class: name=\"struct %s\"", name
);
1670 struct ctf_node
*entry_node
;
1671 uint64_t min_align_value
= 0;
1674 if (ctx_decl_scope_lookup_struct(ctx
, ctx
->current_scope
, name
, 1, false)) {
1675 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(
1676 "Structure field class already declared in local scope: "
1677 "name=\"struct %s\"",
1684 if (!bt_list_empty(min_align
)) {
1685 ret
= get_unary_unsigned(ctx
, min_align
, &min_align_value
);
1687 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(
1688 "Unexpected unary expression for structure field class's `align` attribute: "
1695 *struct_decl
= ctf_field_class_struct_create();
1696 BT_ASSERT(*struct_decl
);
1698 if (min_align_value
!= 0) {
1699 (*struct_decl
)->base
.alignment
= min_align_value
;
1702 _TRY_PUSH_SCOPE_OR_GOTO_ERROR();
1704 bt_list_for_each_entry (entry_node
, decl_list
, siblings
) {
1705 ret
= visit_struct_decl_entry(ctx
, entry_node
, *struct_decl
);
1707 _BT_COMP_LOGE_APPEND_CAUSE_NODE(entry_node
,
1708 "Cannot visit structure field class entry: "
1719 ret
= ctx_decl_scope_register_struct(ctx
, ctx
->current_scope
, name
, *struct_decl
);
1721 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(
1722 "Cannot register structure field class in declaration scope: "
1723 "name=\"struct %s\", ret=%d",
1733 ctf_field_class_destroy(&(*struct_decl
)->base
);
1734 *struct_decl
= NULL
;
1738 static int visit_variant_decl(struct ctf_visitor_generate_ir
*ctx
, const char *name
,
1739 const char *tag
, struct bt_list_head
*decl_list
, int has_body
,
1740 struct ctf_field_class_variant
**variant_decl
)
1743 struct ctf_field_class_variant
*untagged_variant_decl
= NULL
;
1745 BT_ASSERT(variant_decl
);
1746 *variant_decl
= NULL
;
1748 /* For named variant (without body), lookup in declaration scope */
1751 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Bodyless variant field class: missing name.");
1756 untagged_variant_decl
=
1757 ctx_decl_scope_lookup_variant(ctx
, ctx
->current_scope
, name
, -1, true);
1758 if (!untagged_variant_decl
) {
1759 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(
1760 "Cannot find variant field class: name=\"variant %s\"", name
);
1765 struct ctf_node
*entry_node
;
1768 if (ctx_decl_scope_lookup_variant(ctx
, ctx
->current_scope
, name
, 1, false)) {
1769 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(
1770 "Variant field class already declared in local scope: "
1771 "name=\"variant %s\"",
1778 untagged_variant_decl
= ctf_field_class_variant_create();
1779 BT_ASSERT(untagged_variant_decl
);
1780 _TRY_PUSH_SCOPE_OR_GOTO_ERROR();
1782 bt_list_for_each_entry (entry_node
, decl_list
, siblings
) {
1783 ret
= visit_variant_decl_entry(ctx
, entry_node
, untagged_variant_decl
);
1785 _BT_COMP_LOGE_APPEND_CAUSE_NODE(entry_node
,
1786 "Cannot visit variant field class entry: "
1797 ret
= ctx_decl_scope_register_variant(ctx
, ctx
->current_scope
, name
,
1798 untagged_variant_decl
);
1800 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(
1801 "Cannot register variant field class in declaration scope: "
1802 "name=\"variant %s\", ret=%d",
1810 * If tagged, create tagged variant and return; otherwise
1811 * return untagged variant.
1814 *variant_decl
= untagged_variant_decl
;
1815 untagged_variant_decl
= NULL
;
1818 * At this point, we have a fresh untagged variant; nobody
1819 * else owns it. Set its tag now.
1821 g_string_assign(untagged_variant_decl
->tag_ref
, tag
);
1822 *variant_decl
= untagged_variant_decl
;
1823 untagged_variant_decl
= NULL
;
1826 BT_ASSERT(!untagged_variant_decl
);
1827 BT_ASSERT(*variant_decl
);
1831 ctf_field_class_destroy(&untagged_variant_decl
->base
);
1832 untagged_variant_decl
= NULL
;
1833 ctf_field_class_destroy(&(*variant_decl
)->base
);
1834 *variant_decl
= NULL
;
1848 static int visit_enum_decl_entry(struct ctf_visitor_generate_ir
*ctx
, struct ctf_node
*enumerator
,
1849 struct ctf_field_class_enum
*enum_decl
, struct uori
*last
)
1853 struct ctf_node
*iter
;
1854 struct uori start
= {
1868 const char *label
= enumerator
->u
.enumerator
.id
;
1869 struct bt_list_head
*values
= &enumerator
->u
.enumerator
.values
;
1871 bt_list_for_each_entry (iter
, values
, siblings
) {
1872 struct uori
*target
;
1874 if (iter
->type
!= NODE_UNARY_EXPRESSION
) {
1875 _BT_COMP_LOGE_APPEND_CAUSE_NODE(iter
,
1876 "Wrong expression for enumeration field class label: "
1877 "node-type=%d, label=\"%s\"",
1889 switch (iter
->u
.unary_expression
.type
) {
1890 case UNARY_SIGNED_CONSTANT
:
1891 target
->is_signed
= true;
1892 target
->value
.i
= iter
->u
.unary_expression
.u
.signed_constant
;
1894 case UNARY_UNSIGNED_CONSTANT
:
1895 target
->is_signed
= false;
1896 target
->value
.u
= iter
->u
.unary_expression
.u
.unsigned_constant
;
1899 _BT_COMP_LOGE_APPEND_CAUSE_NODE(iter
,
1900 "Invalid enumeration field class entry: "
1901 "expecting constant signed or unsigned integer: "
1902 "node-type=%d, label=\"%s\"",
1903 iter
->u
.unary_expression
.type
, label
);
1909 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
1910 iter
, "Invalid enumeration field class entry: label=\"%s\"", label
);
1926 if (end
.is_signed
) {
1927 last
->value
.i
= end
.value
.i
+ 1;
1929 last
->value
.u
= end
.value
.u
+ 1;
1932 ctf_field_class_enum_map_range(enum_decl
, label
, start
.value
.u
, end
.value
.u
);
1939 static int visit_enum_decl(struct ctf_visitor_generate_ir
*ctx
, const char *name
,
1940 struct ctf_node
*container_cls
, struct bt_list_head
*enumerator_list
,
1941 int has_body
, struct ctf_field_class_enum
**enum_decl
)
1945 struct ctf_field_class_int
*integer_decl
= NULL
;
1947 BT_ASSERT(enum_decl
);
1950 /* For named enum (without body), lookup in declaration scope */
1953 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(
1954 "Bodyless enumeration field class: missing name.");
1959 *enum_decl
= ctx_decl_scope_lookup_enum(ctx
, ctx
->current_scope
, name
, -1, true);
1961 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Cannot find enumeration field class: "
1968 struct ctf_node
*iter
;
1969 struct uori last_value
= {
1978 if (ctx_decl_scope_lookup_enum(ctx
, ctx
->current_scope
, name
, 1, false)) {
1979 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(
1980 "Enumeration field class already declared in local scope: "
1988 if (!container_cls
) {
1989 integer_decl
= ctf_field_class_as_int(
1990 ctx_decl_scope_lookup_alias(ctx
, ctx
->current_scope
, "int", -1, true));
1991 if (!integer_decl
) {
1992 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(
1993 "Cannot find implicit `int` field class alias for enumeration field class.");
1998 ctf_field_class
*decl
;
2000 ret
= visit_field_class_declarator(ctx
, container_cls
, &qdummy_id
, NULL
, &decl
, NULL
);
2007 integer_decl
= ctf_field_class_as_int(decl
);
2010 BT_ASSERT(integer_decl
);
2012 if (integer_decl
->base
.base
.type
!= CTF_FIELD_CLASS_TYPE_INT
) {
2013 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(
2014 "Container field class for enumeration field class is not an integer field class: "
2016 integer_decl
->base
.base
.type
);
2021 *enum_decl
= ctf_field_class_enum_create();
2022 BT_ASSERT(*enum_decl
);
2023 (*enum_decl
)->base
.base
.base
.alignment
= integer_decl
->base
.base
.alignment
;
2024 ctf_field_class_int_copy_content(&(*enum_decl
)->base
, integer_decl
);
2025 last_value
.is_signed
= (*enum_decl
)->base
.is_signed
;
2027 bt_list_for_each_entry (iter
, enumerator_list
, siblings
) {
2028 ret
= visit_enum_decl_entry(ctx
, iter
, *enum_decl
, &last_value
);
2030 _BT_COMP_LOGE_APPEND_CAUSE_NODE(iter
,
2031 "Cannot visit enumeration field class entry: "
2039 ret
= ctx_decl_scope_register_enum(ctx
, ctx
->current_scope
, name
, *enum_decl
);
2041 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(
2042 "Cannot register enumeration field class in declaration scope: "
2053 ctf_field_class_destroy(&(*enum_decl
)->base
.base
.base
);
2057 ctf_field_class_destroy(&integer_decl
->base
.base
);
2058 integer_decl
= NULL
;
2062 static int visit_field_class_specifier(struct ctf_visitor_generate_ir
*ctx
,
2063 struct ctf_node
*cls_specifier_list
,
2064 struct ctf_field_class
**decl
)
2067 GString
*str
= NULL
;
2070 str
= g_string_new("");
2071 ret
= get_class_specifier_list_name(ctx
, cls_specifier_list
, str
);
2073 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
2074 cls_specifier_list
, "Cannot get field class specifier list's name: ret=%d", ret
);
2078 *decl
= ctx_decl_scope_lookup_alias(ctx
, ctx
->current_scope
, str
->str
, -1, true);
2080 _BT_COMP_LOGE_APPEND_CAUSE_NODE(cls_specifier_list
,
2081 "Cannot find field class alias: name=\"%s\"", str
->str
);
2089 ctf_field_class_destroy(*decl
);
2094 g_string_free(str
, TRUE
);
2100 static int visit_integer_decl(struct ctf_visitor_generate_ir
*ctx
, struct bt_list_head
*expressions
,
2101 struct ctf_field_class_int
**integer_decl
)
2106 struct ctf_node
*expression
;
2107 uint64_t alignment
= 0, size
= 0;
2108 struct ctf_clock_class
*mapped_clock_class
= NULL
;
2109 enum ctf_encoding encoding
= CTF_ENCODING_NONE
;
2110 bt_field_class_integer_preferred_display_base base
=
2111 BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL
;
2112 enum ctf_byte_order byte_order
= ctx
->ctf_tc
->default_byte_order
;
2114 *integer_decl
= NULL
;
2116 bt_list_for_each_entry (expression
, expressions
, siblings
) {
2117 struct ctf_node
*left
, *right
;
2119 left
= _BT_LIST_FIRST_ENTRY(&expression
->u
.ctf_expression
.left
, struct ctf_node
, siblings
);
2121 _BT_LIST_FIRST_ENTRY(&expression
->u
.ctf_expression
.right
, struct ctf_node
, siblings
);
2123 if (left
->u
.unary_expression
.type
!= UNARY_STRING
) {
2124 _BT_COMP_LOGE_APPEND_CAUSE_NODE(left
, "Unexpected unary expression type: type=%d",
2125 left
->u
.unary_expression
.type
);
2130 if (strcmp(left
->u
.unary_expression
.u
.string
, "signed") == 0) {
2131 if (_IS_SET(&set
, _INTEGER_SIGNED_SET
)) {
2132 _BT_COMP_LOGE_APPEND_CAUSE_DUP_ATTR(left
, "signed", "integer field class");
2137 signedness
= get_boolean(ctx
, right
);
2138 if (signedness
< 0) {
2139 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
2141 "Invalid boolean value for integer field class's `signed` attribute: "
2148 _SET(&set
, _INTEGER_SIGNED_SET
);
2149 } else if (strcmp(left
->u
.unary_expression
.u
.string
, "byte_order") == 0) {
2150 if (_IS_SET(&set
, _INTEGER_BYTE_ORDER_SET
)) {
2151 _BT_COMP_LOGE_APPEND_CAUSE_DUP_ATTR(left
, "byte_order", "integer field class");
2156 byte_order
= get_real_byte_order(ctx
, right
);
2157 if (byte_order
== CTF_BYTE_ORDER_UNKNOWN
) {
2158 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
2160 "Invalid `byte_order` attribute in integer field class: "
2167 _SET(&set
, _INTEGER_BYTE_ORDER_SET
);
2168 } else if (strcmp(left
->u
.unary_expression
.u
.string
, "size") == 0) {
2169 if (_IS_SET(&set
, _INTEGER_SIZE_SET
)) {
2170 _BT_COMP_LOGE_APPEND_CAUSE_DUP_ATTR(left
, "size", "integer field class");
2175 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
2176 _BT_COMP_LOGE_APPEND_CAUSE_NODE(right
,
2177 "Invalid `size` attribute in integer field class: "
2178 "expecting unsigned constant integer: "
2180 right
->u
.unary_expression
.type
);
2185 size
= right
->u
.unary_expression
.u
.unsigned_constant
;
2187 _BT_COMP_LOGE_APPEND_CAUSE_NODE(right
,
2188 "Invalid `size` attribute in integer field class: "
2189 "expecting positive constant integer: "
2194 } else if (size
> 64) {
2195 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
2197 "Invalid `size` attribute in integer field class: "
2198 "integer fields over 64 bits are not supported as of this version: "
2205 _SET(&set
, _INTEGER_SIZE_SET
);
2206 } else if (strcmp(left
->u
.unary_expression
.u
.string
, "align") == 0) {
2207 if (_IS_SET(&set
, _INTEGER_ALIGN_SET
)) {
2208 _BT_COMP_LOGE_APPEND_CAUSE_DUP_ATTR(left
, "align", "integer field class");
2213 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
2214 _BT_COMP_LOGE_APPEND_CAUSE_NODE(right
,
2215 "Invalid `align` attribute in integer field class: "
2216 "expecting unsigned constant integer: "
2218 right
->u
.unary_expression
.type
);
2223 alignment
= right
->u
.unary_expression
.u
.unsigned_constant
;
2224 if (!is_align_valid(alignment
)) {
2225 _BT_COMP_LOGE_APPEND_CAUSE_NODE(right
,
2226 "Invalid `align` attribute in integer field class: "
2227 "expecting power of two: "
2234 _SET(&set
, _INTEGER_ALIGN_SET
);
2235 } else if (strcmp(left
->u
.unary_expression
.u
.string
, "base") == 0) {
2236 if (_IS_SET(&set
, _INTEGER_BASE_SET
)) {
2237 _BT_COMP_LOGE_APPEND_CAUSE_DUP_ATTR(left
, "base", "integer field class");
2242 switch (right
->u
.unary_expression
.type
) {
2243 case UNARY_UNSIGNED_CONSTANT
:
2245 uint64_t constant
= right
->u
.unary_expression
.u
.unsigned_constant
;
2249 base
= BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_BINARY
;
2252 base
= BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_OCTAL
;
2255 base
= BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL
;
2258 base
= BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_HEXADECIMAL
;
2261 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
2263 "Invalid `base` attribute in integer field class: "
2265 right
->u
.unary_expression
.u
.unsigned_constant
);
2274 ctf_ast_concatenate_unary_strings(&expression
->u
.ctf_expression
.right
);
2276 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
2278 "Unexpected unary expression for integer field class's `base` attribute.");
2283 if (strcmp(s_right
, "decimal") == 0 || strcmp(s_right
, "dec") == 0 ||
2284 strcmp(s_right
, "d") == 0 || strcmp(s_right
, "i") == 0 ||
2285 strcmp(s_right
, "u") == 0) {
2286 base
= BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL
;
2287 } else if (strcmp(s_right
, "hexadecimal") == 0 || strcmp(s_right
, "hex") == 0 ||
2288 strcmp(s_right
, "x") == 0 || strcmp(s_right
, "X") == 0 ||
2289 strcmp(s_right
, "p") == 0) {
2290 base
= BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_HEXADECIMAL
;
2291 } else if (strcmp(s_right
, "octal") == 0 || strcmp(s_right
, "oct") == 0 ||
2292 strcmp(s_right
, "o") == 0) {
2293 base
= BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_OCTAL
;
2294 } else if (strcmp(s_right
, "binary") == 0 || strcmp(s_right
, "b") == 0) {
2295 base
= BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_BINARY
;
2297 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
2299 "Unexpected unary expression for integer field class's `base` attribute: "
2311 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
2312 right
, "Invalid `base` attribute in integer field class: "
2313 "expecting unsigned constant integer or unary string.");
2318 _SET(&set
, _INTEGER_BASE_SET
);
2319 } else if (strcmp(left
->u
.unary_expression
.u
.string
, "encoding") == 0) {
2322 if (_IS_SET(&set
, _INTEGER_ENCODING_SET
)) {
2323 _BT_COMP_LOGE_APPEND_CAUSE_DUP_ATTR(left
, "encoding", "integer field class");
2328 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
2329 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
2330 right
, "Invalid `encoding` attribute in integer field class: "
2331 "expecting unary string.");
2336 s_right
= ctf_ast_concatenate_unary_strings(&expression
->u
.ctf_expression
.right
);
2338 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
2340 "Unexpected unary expression for integer field class's `encoding` attribute.");
2345 if (strcmp(s_right
, "UTF8") == 0 || strcmp(s_right
, "utf8") == 0 ||
2346 strcmp(s_right
, "utf-8") == 0 || strcmp(s_right
, "UTF-8") == 0 ||
2347 strcmp(s_right
, "ASCII") == 0 || strcmp(s_right
, "ascii") == 0) {
2348 encoding
= CTF_ENCODING_UTF8
;
2349 } else if (strcmp(s_right
, "none") == 0) {
2350 encoding
= CTF_ENCODING_NONE
;
2352 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
2354 "Invalid `encoding` attribute in integer field class: "
2355 "unknown encoding: encoding=\"%s\"",
2363 _SET(&set
, _INTEGER_ENCODING_SET
);
2364 } else if (strcmp(left
->u
.unary_expression
.u
.string
, "map") == 0) {
2365 const char *clock_name
;
2367 if (_IS_SET(&set
, _INTEGER_MAP_SET
)) {
2368 _BT_COMP_LOGE_APPEND_CAUSE_DUP_ATTR(left
, "map", "integer field class");
2373 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
2374 _BT_COMP_LOGE_APPEND_CAUSE_NODE(right
,
2375 "Invalid `map` attribute in integer field class: "
2376 "expecting unary string.");
2381 clock_name
= get_map_clock_name_value(&expression
->u
.ctf_expression
.right
);
2384 ctf_ast_concatenate_unary_strings(&expression
->u
.ctf_expression
.right
);
2387 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
2389 "Unexpected unary expression for integer field class's `map` attribute.");
2394 _BT_COMP_LOGE_NODE(right
,
2395 "Invalid `map` attribute in integer field class: "
2396 "cannot find clock class at this point: name=\"%s\"",
2398 _SET(&set
, _INTEGER_MAP_SET
);
2403 mapped_clock_class
=
2404 ctf_trace_class_borrow_clock_class_by_name(ctx
->ctf_tc
, clock_name
);
2405 if (!mapped_clock_class
) {
2406 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
2408 "Invalid `map` attribute in integer field class: "
2409 "cannot find clock class at this point: name=\"%s\"",
2415 _SET(&set
, _INTEGER_MAP_SET
);
2417 _BT_COMP_LOGW_NODE(left
,
2418 "Unknown attribute in integer field class: "
2420 left
->u
.unary_expression
.u
.string
);
2424 if (!_IS_SET(&set
, _INTEGER_SIZE_SET
)) {
2425 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(
2426 "Missing `size` attribute in integer field class.");
2431 if (!_IS_SET(&set
, _INTEGER_ALIGN_SET
)) {
2432 if (size
% CHAR_BIT
) {
2433 /* Bit-packed alignment */
2436 /* Byte-packed alignment */
2437 alignment
= CHAR_BIT
;
2441 *integer_decl
= ctf_field_class_int_create();
2442 BT_ASSERT(*integer_decl
);
2443 (*integer_decl
)->base
.base
.alignment
= alignment
;
2444 (*integer_decl
)->base
.byte_order
= byte_order
;
2445 (*integer_decl
)->base
.size
= size
;
2446 (*integer_decl
)->is_signed
= (signedness
> 0);
2447 (*integer_decl
)->disp_base
= base
;
2448 (*integer_decl
)->encoding
= encoding
;
2449 (*integer_decl
)->mapped_clock_class
= mapped_clock_class
;
2453 ctf_field_class_destroy(&(*integer_decl
)->base
.base
);
2454 *integer_decl
= NULL
;
2458 static int visit_floating_point_number_decl(struct ctf_visitor_generate_ir
*ctx
,
2459 struct bt_list_head
*expressions
,
2460 struct ctf_field_class_float
**float_decl
)
2464 struct ctf_node
*expression
;
2465 uint64_t alignment
= 1, exp_dig
= 0, mant_dig
= 0;
2466 enum ctf_byte_order byte_order
= ctx
->ctf_tc
->default_byte_order
;
2470 bt_list_for_each_entry (expression
, expressions
, siblings
) {
2471 struct ctf_node
*left
, *right
;
2473 left
= _BT_LIST_FIRST_ENTRY(&expression
->u
.ctf_expression
.left
, struct ctf_node
, siblings
);
2475 _BT_LIST_FIRST_ENTRY(&expression
->u
.ctf_expression
.right
, struct ctf_node
, siblings
);
2477 if (left
->u
.unary_expression
.type
!= UNARY_STRING
) {
2478 _BT_COMP_LOGE_APPEND_CAUSE_NODE(left
, "Unexpected unary expression type: type=%d",
2479 left
->u
.unary_expression
.type
);
2484 if (strcmp(left
->u
.unary_expression
.u
.string
, "byte_order") == 0) {
2485 if (_IS_SET(&set
, _FLOAT_BYTE_ORDER_SET
)) {
2486 _BT_COMP_LOGE_APPEND_CAUSE_DUP_ATTR(left
, "byte_order",
2487 "floating point number field class");
2492 byte_order
= get_real_byte_order(ctx
, right
);
2493 if (byte_order
== CTF_BYTE_ORDER_UNKNOWN
) {
2494 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
2496 "Invalid `byte_order` attribute in floating point number field class: "
2503 _SET(&set
, _FLOAT_BYTE_ORDER_SET
);
2504 } else if (strcmp(left
->u
.unary_expression
.u
.string
, "exp_dig") == 0) {
2505 if (_IS_SET(&set
, _FLOAT_EXP_DIG_SET
)) {
2506 _BT_COMP_LOGE_APPEND_CAUSE_DUP_ATTR(left
, "exp_dig",
2507 "floating point number field class");
2512 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
2513 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
2515 "Invalid `exp_dig` attribute in floating point number field class: "
2516 "expecting unsigned constant integer: "
2518 right
->u
.unary_expression
.type
);
2523 exp_dig
= right
->u
.unary_expression
.u
.unsigned_constant
;
2524 _SET(&set
, _FLOAT_EXP_DIG_SET
);
2525 } else if (strcmp(left
->u
.unary_expression
.u
.string
, "mant_dig") == 0) {
2526 if (_IS_SET(&set
, _FLOAT_MANT_DIG_SET
)) {
2527 _BT_COMP_LOGE_APPEND_CAUSE_DUP_ATTR(left
, "mant_dig",
2528 "floating point number field class");
2533 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
2534 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
2536 "Invalid `mant_dig` attribute in floating point number field class: "
2537 "expecting unsigned constant integer: "
2539 right
->u
.unary_expression
.type
);
2544 mant_dig
= right
->u
.unary_expression
.u
.unsigned_constant
;
2545 _SET(&set
, _FLOAT_MANT_DIG_SET
);
2546 } else if (strcmp(left
->u
.unary_expression
.u
.string
, "align") == 0) {
2547 if (_IS_SET(&set
, _FLOAT_ALIGN_SET
)) {
2548 _BT_COMP_LOGE_APPEND_CAUSE_DUP_ATTR(left
, "align",
2549 "floating point number field class");
2554 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
2555 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
2557 "Invalid `align` attribute in floating point number field class: "
2558 "expecting unsigned constant integer: "
2560 right
->u
.unary_expression
.type
);
2565 alignment
= right
->u
.unary_expression
.u
.unsigned_constant
;
2567 if (!is_align_valid(alignment
)) {
2568 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
2570 "Invalid `align` attribute in floating point number field class: "
2571 "expecting power of two: "
2578 _SET(&set
, _FLOAT_ALIGN_SET
);
2580 _BT_COMP_LOGW_NODE(left
,
2581 "Unknown attribute in floating point number field class: "
2583 left
->u
.unary_expression
.u
.string
);
2587 if (!_IS_SET(&set
, _FLOAT_MANT_DIG_SET
)) {
2588 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(
2589 "Missing `mant_dig` attribute in floating point number field class.");
2594 if (!_IS_SET(&set
, _FLOAT_EXP_DIG_SET
)) {
2595 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(
2596 "Missing `exp_dig` attribute in floating point number field class.");
2601 if (mant_dig
!= 24 && mant_dig
!= 53) {
2602 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("`mant_dig` attribute: expecting 24 or 53.");
2607 if (mant_dig
== 24 && exp_dig
!= 8) {
2608 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(
2609 "`exp_dig` attribute: expecting 8 because `mant_dig` is 24.");
2614 if (mant_dig
== 53 && exp_dig
!= 11) {
2615 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(
2616 "`exp_dig` attribute: expecting 11 because `mant_dig` is 53.");
2621 if (!_IS_SET(&set
, _INTEGER_ALIGN_SET
)) {
2622 if ((mant_dig
+ exp_dig
) % CHAR_BIT
) {
2623 /* Bit-packed alignment */
2626 /* Byte-packed alignment */
2627 alignment
= CHAR_BIT
;
2631 *float_decl
= ctf_field_class_float_create();
2632 BT_ASSERT(*float_decl
);
2633 (*float_decl
)->base
.base
.alignment
= alignment
;
2634 (*float_decl
)->base
.byte_order
= byte_order
;
2635 (*float_decl
)->base
.size
= mant_dig
+ exp_dig
;
2639 ctf_field_class_destroy(&(*float_decl
)->base
.base
);
2644 static int visit_string_decl(struct ctf_visitor_generate_ir
*ctx
, struct bt_list_head
*expressions
,
2645 struct ctf_field_class_string
**string_decl
)
2649 struct ctf_node
*expression
;
2650 enum ctf_encoding encoding
= CTF_ENCODING_UTF8
;
2652 *string_decl
= NULL
;
2654 bt_list_for_each_entry (expression
, expressions
, siblings
) {
2655 struct ctf_node
*left
, *right
;
2657 left
= _BT_LIST_FIRST_ENTRY(&expression
->u
.ctf_expression
.left
, struct ctf_node
, siblings
);
2659 _BT_LIST_FIRST_ENTRY(&expression
->u
.ctf_expression
.right
, struct ctf_node
, siblings
);
2661 if (left
->u
.unary_expression
.type
!= UNARY_STRING
) {
2662 _BT_COMP_LOGE_APPEND_CAUSE_NODE(left
, "Unexpected unary expression type: type=%d",
2663 left
->u
.unary_expression
.type
);
2668 if (strcmp(left
->u
.unary_expression
.u
.string
, "encoding") == 0) {
2671 if (_IS_SET(&set
, _STRING_ENCODING_SET
)) {
2672 _BT_COMP_LOGE_APPEND_CAUSE_DUP_ATTR(left
, "encoding", "string field class");
2677 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
2678 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
2679 right
, "Invalid `encoding` attribute in string field class: "
2680 "expecting unary string.");
2685 s_right
= ctf_ast_concatenate_unary_strings(&expression
->u
.ctf_expression
.right
);
2687 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
2689 "Unexpected unary expression for string field class's `encoding` attribute.");
2694 if (strcmp(s_right
, "UTF8") == 0 || strcmp(s_right
, "utf8") == 0 ||
2695 strcmp(s_right
, "utf-8") == 0 || strcmp(s_right
, "UTF-8") == 0 ||
2696 strcmp(s_right
, "ASCII") == 0 || strcmp(s_right
, "ascii") == 0) {
2697 encoding
= CTF_ENCODING_UTF8
;
2698 } else if (strcmp(s_right
, "none") == 0) {
2699 encoding
= CTF_ENCODING_NONE
;
2701 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
2703 "Invalid `encoding` attribute in string field class: "
2704 "unknown encoding: encoding=\"%s\"",
2712 _SET(&set
, _STRING_ENCODING_SET
);
2714 _BT_COMP_LOGW_NODE(left
,
2715 "Unknown attribute in string field class: "
2717 left
->u
.unary_expression
.u
.string
);
2721 *string_decl
= ctf_field_class_string_create();
2722 BT_ASSERT(*string_decl
);
2723 (*string_decl
)->encoding
= encoding
;
2727 ctf_field_class_destroy(&(*string_decl
)->base
);
2728 *string_decl
= NULL
;
2732 static int visit_field_class_specifier_list(struct ctf_visitor_generate_ir
*ctx
,
2733 struct ctf_node
*ts_list
, struct ctf_field_class
**decl
)
2736 struct ctf_node
*first
, *node
;
2740 if (ts_list
->type
!= NODE_TYPE_SPECIFIER_LIST
) {
2741 _BT_COMP_LOGE_APPEND_CAUSE_NODE(ts_list
, "Unexpected node type: node-type=%d",
2747 first
= _BT_LIST_FIRST_ENTRY(&ts_list
->u
.field_class_specifier_list
.head
, struct ctf_node
,
2749 if (first
->type
!= NODE_TYPE_SPECIFIER
) {
2750 _BT_COMP_LOGE_APPEND_CAUSE_NODE(first
, "Unexpected node type: node-type=%d", first
->type
);
2755 node
= first
->u
.field_class_specifier
.node
;
2757 switch (first
->u
.field_class_specifier
.type
) {
2758 case TYPESPEC_INTEGER
:
2760 ctf_field_class_int
*int_decl
;
2762 ret
= visit_integer_decl(ctx
, &node
->u
.integer
.expressions
, &int_decl
);
2764 BT_ASSERT(!int_decl
);
2768 *decl
= &int_decl
->base
.base
;
2771 case TYPESPEC_FLOATING_POINT
:
2773 ctf_field_class_float
*float_decl
;
2776 visit_floating_point_number_decl(ctx
, &node
->u
.floating_point
.expressions
, &float_decl
);
2778 BT_ASSERT(!float_decl
);
2782 *decl
= &float_decl
->base
.base
;
2785 case TYPESPEC_STRING
:
2787 ctf_field_class_string
*string_decl
;
2789 ret
= visit_string_decl(ctx
, &node
->u
.string
.expressions
, &string_decl
);
2791 BT_ASSERT(!string_decl
);
2795 *decl
= &string_decl
->base
;
2798 case TYPESPEC_STRUCT
:
2800 ctf_field_class_struct
*struct_decl
;
2802 ret
= visit_struct_decl(ctx
, node
->u
._struct
.name
, &node
->u
._struct
.declaration_list
,
2803 node
->u
._struct
.has_body
, &node
->u
._struct
.min_align
, &struct_decl
);
2805 BT_ASSERT(!struct_decl
);
2809 *decl
= &struct_decl
->base
;
2812 case TYPESPEC_VARIANT
:
2814 ctf_field_class_variant
*variant_decl
;
2816 ret
= visit_variant_decl(ctx
, node
->u
.variant
.name
, node
->u
.variant
.choice
,
2817 &node
->u
.variant
.declaration_list
, node
->u
.variant
.has_body
,
2820 BT_ASSERT(!variant_decl
);
2824 *decl
= &variant_decl
->base
;
2829 ctf_field_class_enum
*enum_decl
;
2831 ret
= visit_enum_decl(ctx
, node
->u
._enum
.enum_id
, node
->u
._enum
.container_field_class
,
2832 &node
->u
._enum
.enumerator_list
, node
->u
._enum
.has_body
, &enum_decl
);
2834 BT_ASSERT(!enum_decl
);
2838 *decl
= &enum_decl
->base
.base
.base
;
2843 case TYPESPEC_SHORT
:
2846 case TYPESPEC_FLOAT
:
2847 case TYPESPEC_DOUBLE
:
2848 case TYPESPEC_SIGNED
:
2849 case TYPESPEC_UNSIGNED
:
2851 case TYPESPEC_COMPLEX
:
2852 case TYPESPEC_IMAGINARY
:
2853 case TYPESPEC_CONST
:
2854 case TYPESPEC_ID_TYPE
:
2855 ret
= visit_field_class_specifier(ctx
, ts_list
, decl
);
2857 _BT_COMP_LOGE_APPEND_CAUSE_NODE(first
, "Cannot visit field class specifier: ret=%d",
2864 _BT_COMP_LOGE_APPEND_CAUSE_NODE(first
,
2865 "Unexpected field class specifier type: node-type=%d",
2866 first
->u
.field_class_specifier
.type
);
2875 ctf_field_class_destroy(*decl
);
2880 static int visit_event_decl_entry(struct ctf_visitor_generate_ir
*ctx
, struct ctf_node
*node
,
2881 struct ctf_event_class
*event_class
, uint64_t *stream_id
,
2887 switch (node
->type
) {
2889 ret
= visit_field_class_def(ctx
, node
->u
.field_class_def
.field_class_specifier_list
,
2890 &node
->u
.field_class_def
.field_class_declarators
);
2892 _BT_COMP_LOGE_APPEND_CAUSE_NODE(node
, "Cannot add field class found in event class.");
2896 case NODE_TYPEALIAS
:
2897 ret
= visit_field_class_alias(ctx
, node
->u
.field_class_alias
.target
,
2898 node
->u
.field_class_alias
.alias
);
2900 _BT_COMP_LOGE_APPEND_CAUSE_NODE(node
,
2901 "Cannot add field class alias found in event class.");
2905 case NODE_CTF_EXPRESSION
:
2907 left
= ctf_ast_concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
2909 _BT_COMP_LOGE_APPEND_CAUSE_NODE(node
, "Cannot concatenate unary strings.");
2914 if (strcmp(left
, "name") == 0) {
2915 /* This is already known at this stage */
2916 if (_IS_SET(set
, _EVENT_NAME_SET
)) {
2917 _BT_COMP_LOGE_APPEND_CAUSE_DUP_ATTR(node
, "name", "event class");
2922 _SET(set
, _EVENT_NAME_SET
);
2923 } else if (strcmp(left
, "id") == 0) {
2926 if (_IS_SET(set
, _EVENT_ID_SET
)) {
2927 _BT_COMP_LOGE_APPEND_CAUSE_DUP_ATTR(node
, "id", "event class");
2932 ret
= get_unary_unsigned(ctx
, &node
->u
.ctf_expression
.right
, (uint64_t *) &id
);
2933 /* Only read "id" if get_unary_unsigned() succeeded. */
2934 if (ret
|| (!ret
&& id
< 0)) {
2935 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
2936 node
, "Unexpected unary expression for event class's `id` attribute.");
2941 event_class
->id
= id
;
2942 _SET(set
, _EVENT_ID_SET
);
2943 } else if (strcmp(left
, "stream_id") == 0) {
2944 if (_IS_SET(set
, _EVENT_STREAM_ID_SET
)) {
2945 _BT_COMP_LOGE_APPEND_CAUSE_DUP_ATTR(node
, "stream_id", "event class");
2950 ret
= get_unary_unsigned(ctx
, &node
->u
.ctf_expression
.right
, stream_id
);
2953 * Only read "stream_id" if get_unary_unsigned()
2957 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
2958 node
, "Unexpected unary expression for event class's `stream_id` attribute.");
2963 _SET(set
, _EVENT_STREAM_ID_SET
);
2964 } else if (strcmp(left
, "context") == 0) {
2965 if (_IS_SET(set
, _EVENT_CONTEXT_SET
)) {
2966 _BT_COMP_LOGE_APPEND_CAUSE_NODE(node
, "Duplicate `context` entry in event class.");
2971 ret
= visit_field_class_specifier_list(
2972 ctx
, _BT_LIST_FIRST_ENTRY(&node
->u
.ctf_expression
.right
, struct ctf_node
, siblings
),
2973 &event_class
->spec_context_fc
);
2975 _BT_COMP_LOGE_APPEND_CAUSE_NODE(node
,
2976 "Cannot create event class's context field class.");
2980 BT_ASSERT(event_class
->spec_context_fc
);
2981 _SET(set
, _EVENT_CONTEXT_SET
);
2982 } else if (strcmp(left
, "fields") == 0) {
2983 if (_IS_SET(set
, _EVENT_FIELDS_SET
)) {
2984 _BT_COMP_LOGE_APPEND_CAUSE_NODE(node
, "Duplicate `fields` entry in event class.");
2989 ret
= visit_field_class_specifier_list(
2990 ctx
, _BT_LIST_FIRST_ENTRY(&node
->u
.ctf_expression
.right
, struct ctf_node
, siblings
),
2991 &event_class
->payload_fc
);
2993 _BT_COMP_LOGE_APPEND_CAUSE_NODE(node
,
2994 "Cannot create event class's payload field class.");
2998 BT_ASSERT(event_class
->payload_fc
);
2999 _SET(set
, _EVENT_FIELDS_SET
);
3000 } else if (strcmp(left
, "loglevel") == 0) {
3001 uint64_t loglevel_value
;
3002 bool is_log_level_known
= true;
3003 bt_event_class_log_level log_level
;
3005 if (_IS_SET(set
, _EVENT_LOG_LEVEL_SET
)) {
3006 _BT_COMP_LOGE_APPEND_CAUSE_DUP_ATTR(node
, "loglevel", "event class");
3011 ret
= get_unary_unsigned(ctx
, &node
->u
.ctf_expression
.right
, &loglevel_value
);
3013 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
3014 node
, "Unexpected unary expression for event class's `loglevel` attribute.");
3019 switch (loglevel_value
) {
3021 log_level
= BT_EVENT_CLASS_LOG_LEVEL_EMERGENCY
;
3024 log_level
= BT_EVENT_CLASS_LOG_LEVEL_ALERT
;
3027 log_level
= BT_EVENT_CLASS_LOG_LEVEL_CRITICAL
;
3030 log_level
= BT_EVENT_CLASS_LOG_LEVEL_ERROR
;
3033 log_level
= BT_EVENT_CLASS_LOG_LEVEL_WARNING
;
3036 log_level
= BT_EVENT_CLASS_LOG_LEVEL_NOTICE
;
3039 log_level
= BT_EVENT_CLASS_LOG_LEVEL_INFO
;
3042 log_level
= BT_EVENT_CLASS_LOG_LEVEL_DEBUG_SYSTEM
;
3045 log_level
= BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROGRAM
;
3048 log_level
= BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROCESS
;
3051 log_level
= BT_EVENT_CLASS_LOG_LEVEL_DEBUG_MODULE
;
3054 log_level
= BT_EVENT_CLASS_LOG_LEVEL_DEBUG_UNIT
;
3057 log_level
= BT_EVENT_CLASS_LOG_LEVEL_DEBUG_FUNCTION
;
3060 log_level
= BT_EVENT_CLASS_LOG_LEVEL_DEBUG_LINE
;
3063 log_level
= BT_EVENT_CLASS_LOG_LEVEL_DEBUG
;
3066 is_log_level_known
= false;
3069 "Not setting event class's log level because its value is unknown: "
3070 "log-level=%" PRIu64
,
3074 if (is_log_level_known
) {
3075 ctf_event_class_set_log_level(event_class
, log_level
);
3078 _SET(set
, _EVENT_LOG_LEVEL_SET
);
3079 } else if (strcmp(left
, "model.emf.uri") == 0) {
3082 if (_IS_SET(set
, _EVENT_MODEL_EMF_URI_SET
)) {
3083 _BT_COMP_LOGE_APPEND_CAUSE_DUP_ATTR(node
, "model.emf.uri", "event class");
3088 right
= ctf_ast_concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
3090 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
3092 "Unexpected unary expression for event class's `model.emf.uri` attribute.");
3097 if (strlen(right
) == 0) {
3098 _BT_COMP_LOGW_NODE(node
, "Not setting event class's EMF URI because it's empty.");
3100 g_string_assign(event_class
->emf_uri
, right
);
3104 _SET(set
, _EVENT_MODEL_EMF_URI_SET
);
3106 _BT_COMP_LOGW_NODE(node
,
3107 "Unknown attribute in event class: "
3130 static char *get_event_decl_name(struct ctf_visitor_generate_ir
*ctx
, struct ctf_node
*node
)
3134 struct ctf_node
*iter
;
3135 struct bt_list_head
*decl_list
= &node
->u
.event
.declaration_list
;
3137 bt_list_for_each_entry (iter
, decl_list
, siblings
) {
3138 if (iter
->type
!= NODE_CTF_EXPRESSION
) {
3142 left
= ctf_ast_concatenate_unary_strings(&iter
->u
.ctf_expression
.left
);
3144 _BT_COMP_LOGE_APPEND_CAUSE_NODE(iter
, "Cannot concatenate unary strings.");
3148 if (strcmp(left
, "name") == 0) {
3149 name
= ctf_ast_concatenate_unary_strings(&iter
->u
.ctf_expression
.right
);
3151 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
3152 iter
, "Unexpected unary expression for event class's `name` attribute.");
3172 static int visit_event_decl(struct ctf_visitor_generate_ir
*ctx
, struct ctf_node
*node
)
3176 struct ctf_node
*iter
;
3177 uint64_t stream_id
= 0;
3178 char *event_name
= NULL
;
3179 struct ctf_event_class
*event_class
= NULL
;
3180 struct ctf_stream_class
*stream_class
= NULL
;
3181 struct bt_list_head
*decl_list
= &node
->u
.event
.declaration_list
;
3182 bool pop_scope
= false;
3184 if (node
->visited
) {
3188 node
->visited
= TRUE
;
3189 event_name
= get_event_decl_name(ctx
, node
);
3191 _BT_COMP_LOGE_APPEND_CAUSE_NODE(node
, "Missing `name` attribute in event class.");
3196 event_class
= ctf_event_class_create();
3197 BT_ASSERT(event_class
);
3198 g_string_assign(event_class
->name
, event_name
);
3199 _TRY_PUSH_SCOPE_OR_GOTO_ERROR();
3202 bt_list_for_each_entry (iter
, decl_list
, siblings
) {
3203 ret
= visit_event_decl_entry(ctx
, iter
, event_class
, &stream_id
, &set
);
3205 _BT_COMP_LOGE_APPEND_CAUSE_NODE(iter
,
3206 "Cannot visit event class's entry: "
3213 if (!_IS_SET(&set
, _EVENT_STREAM_ID_SET
)) {
3215 * Allow missing stream_id if there is only a single
3218 switch (ctx
->ctf_tc
->stream_classes
->len
) {
3220 /* Create implicit stream class if there's none */
3222 stream_class
= ctf_stream_class_create();
3223 BT_ASSERT(stream_class
);
3224 stream_class
->id
= stream_id
;
3225 g_ptr_array_add(ctx
->ctf_tc
->stream_classes
, stream_class
);
3228 /* Single stream class: get its ID */
3229 stream_class
= (ctf_stream_class
*) ctx
->ctf_tc
->stream_classes
->pdata
[0];
3230 stream_id
= stream_class
->id
;
3233 _BT_COMP_LOGE_APPEND_CAUSE_NODE(node
, "Missing `stream_id` attribute in event class.");
3239 /* We have the stream ID now; get the stream class if found */
3240 if (!stream_class
) {
3241 stream_class
= ctf_trace_class_borrow_stream_class_by_id(ctx
->ctf_tc
, stream_id
);
3242 if (!stream_class
) {
3243 _BT_COMP_LOGE_APPEND_CAUSE_NODE(node
,
3244 "Cannot find stream class at this point: "
3252 BT_ASSERT(stream_class
);
3254 if (!_IS_SET(&set
, _EVENT_ID_SET
)) {
3255 /* Allow only one event without ID per stream */
3256 if (stream_class
->event_classes
->len
!= 0) {
3257 _BT_COMP_LOGE_APPEND_CAUSE_NODE(node
, "Missing `id` attribute in event class.");
3263 event_class
->id
= 0;
3266 if (ctf_stream_class_borrow_event_class_by_id(stream_class
, event_class
->id
)) {
3267 _BT_COMP_LOGE_APPEND_CAUSE_NODE(node
,
3268 "Duplicate event class (same ID) in the same stream class: "
3275 ctf_stream_class_append_event_class(stream_class
, event_class
);
3280 ctf_event_class_destroy(event_class
);
3297 static int auto_map_field_to_trace_clock_class(struct ctf_visitor_generate_ir
*ctx
,
3298 struct ctf_field_class
*fc
)
3300 struct ctf_clock_class
*clock_class_to_map_to
= NULL
;
3301 uint64_t clock_class_count
;
3307 if (fc
->type
!= CTF_FIELD_CLASS_TYPE_INT
&& fc
->type
!= CTF_FIELD_CLASS_TYPE_ENUM
) {
3311 ctf_field_class_int
*int_fc
= ctf_field_class_as_int(fc
);
3313 if (int_fc
->mapped_clock_class
) {
3314 /* Already mapped */
3318 clock_class_count
= ctx
->ctf_tc
->clock_classes
->len
;
3320 switch (clock_class_count
) {
3323 * No clock class exists in the trace at this point. Create an
3324 * implicit one at 1 GHz, named `default`, and use this clock
3327 clock_class_to_map_to
= ctf_clock_class_create();
3328 BT_ASSERT(clock_class_to_map_to
);
3329 clock_class_to_map_to
->frequency
= UINT64_C(1000000000);
3330 g_string_assign(clock_class_to_map_to
->name
, "default");
3331 g_ptr_array_add(ctx
->ctf_tc
->clock_classes
, clock_class_to_map_to
);
3335 * Only one clock class exists in the trace at this point: use
3338 clock_class_to_map_to
= (ctf_clock_class
*) ctx
->ctf_tc
->clock_classes
->pdata
[0];
3342 * Timestamp field not mapped to a clock class and there's more
3343 * than one clock class in the trace: this is an error.
3345 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(
3346 "Timestamp field found with no mapped clock class, "
3347 "but there's more than one clock class in the trace at this point.");
3351 BT_ASSERT(clock_class_to_map_to
);
3352 int_fc
->mapped_clock_class
= clock_class_to_map_to
;
3357 static int auto_map_fields_to_trace_clock_class(struct ctf_visitor_generate_ir
*ctx
,
3358 struct ctf_field_class
*root_fc
,
3359 const char *field_name
)
3363 struct ctf_field_class_struct
*struct_fc
= (ctf_field_class_struct
*) root_fc
;
3364 struct ctf_field_class_variant
*var_fc
= (ctf_field_class_variant
*) root_fc
;
3370 if (root_fc
->type
!= CTF_FIELD_CLASS_TYPE_STRUCT
&&
3371 root_fc
->type
!= CTF_FIELD_CLASS_TYPE_VARIANT
) {
3375 if (root_fc
->type
== CTF_FIELD_CLASS_TYPE_STRUCT
) {
3376 count
= struct_fc
->members
->len
;
3378 count
= var_fc
->options
->len
;
3381 for (i
= 0; i
< count
; i
++) {
3382 struct ctf_named_field_class
*named_fc
= NULL
;
3384 if (root_fc
->type
== CTF_FIELD_CLASS_TYPE_STRUCT
) {
3385 named_fc
= ctf_field_class_struct_borrow_member_by_index(struct_fc
, i
);
3386 } else if (root_fc
->type
== CTF_FIELD_CLASS_TYPE_VARIANT
) {
3387 named_fc
= ctf_field_class_variant_borrow_option_by_index(var_fc
, i
);
3392 if (strcmp(named_fc
->name
->str
, field_name
) == 0) {
3393 ret
= auto_map_field_to_trace_clock_class(ctx
, named_fc
->fc
);
3395 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(
3396 "Cannot automatically map field to trace's clock class: "
3397 "field-name=\"%s\"",
3403 ret
= auto_map_fields_to_trace_clock_class(ctx
, named_fc
->fc
, field_name
);
3405 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(
3406 "Cannot automatically map structure or variant field class's fields to trace's clock class: "
3407 "field-name=\"%s\", root-field-name=\"%s\"",
3408 field_name
, named_fc
->name
->str
);
3417 static int visit_stream_decl_entry(struct ctf_visitor_generate_ir
*ctx
, struct ctf_node
*node
,
3418 struct ctf_stream_class
*stream_class
, int *set
)
3423 switch (node
->type
) {
3425 ret
= visit_field_class_def(ctx
, node
->u
.field_class_def
.field_class_specifier_list
,
3426 &node
->u
.field_class_def
.field_class_declarators
);
3428 _BT_COMP_LOGE_APPEND_CAUSE_NODE(node
, "Cannot add field class found in stream class.");
3432 case NODE_TYPEALIAS
:
3433 ret
= visit_field_class_alias(ctx
, node
->u
.field_class_alias
.target
,
3434 node
->u
.field_class_alias
.alias
);
3436 _BT_COMP_LOGE_APPEND_CAUSE_NODE(node
,
3437 "Cannot add field class alias found in stream class.");
3441 case NODE_CTF_EXPRESSION
:
3443 left
= ctf_ast_concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
3445 _BT_COMP_LOGE_APPEND_CAUSE_NODE(node
, "Cannot concatenate unary strings.");
3450 if (strcmp(left
, "id") == 0) {
3453 if (_IS_SET(set
, _STREAM_ID_SET
)) {
3454 _BT_COMP_LOGE_APPEND_CAUSE_DUP_ATTR(node
, "id", "stream declaration");
3459 ret
= get_unary_unsigned(ctx
, &node
->u
.ctf_expression
.right
, (uint64_t *) &id
);
3461 /* Only read "id" if get_unary_unsigned() succeeded. */
3462 if (ret
|| (!ret
&& id
< 0)) {
3463 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
3464 node
, "Unexpected unary expression for stream class's `id` attribute.");
3469 if (ctf_trace_class_borrow_stream_class_by_id(ctx
->ctf_tc
, id
)) {
3470 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
3471 node
, "Duplicate stream class (same ID): id=%" PRId64
, id
);
3476 stream_class
->id
= id
;
3477 _SET(set
, _STREAM_ID_SET
);
3478 } else if (strcmp(left
, "event.header") == 0) {
3479 if (_IS_SET(set
, _STREAM_EVENT_HEADER_SET
)) {
3480 _BT_COMP_LOGE_APPEND_CAUSE_NODE(node
,
3481 "Duplicate `event.header` entry in stream class.");
3486 ret
= visit_field_class_specifier_list(
3487 ctx
, _BT_LIST_FIRST_ENTRY(&node
->u
.ctf_expression
.right
, struct ctf_node
, siblings
),
3488 &stream_class
->event_header_fc
);
3490 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
3491 node
, "Cannot create stream class's event header field class.");
3495 BT_ASSERT(stream_class
->event_header_fc
);
3496 ret
= auto_map_fields_to_trace_clock_class(ctx
, stream_class
->event_header_fc
,
3499 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
3501 "Cannot automatically map specific event header field class fields named `timestamp` to trace's clock class.");
3505 _SET(set
, _STREAM_EVENT_HEADER_SET
);
3506 } else if (strcmp(left
, "event.context") == 0) {
3507 if (_IS_SET(set
, _STREAM_EVENT_CONTEXT_SET
)) {
3508 _BT_COMP_LOGE_APPEND_CAUSE_NODE(node
,
3509 "Duplicate `event.context` entry in stream class.");
3514 ret
= visit_field_class_specifier_list(
3515 ctx
, _BT_LIST_FIRST_ENTRY(&node
->u
.ctf_expression
.right
, struct ctf_node
, siblings
),
3516 &stream_class
->event_common_context_fc
);
3518 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
3519 node
, "Cannot create stream class's event context field class.");
3523 BT_ASSERT(stream_class
->event_common_context_fc
);
3524 _SET(set
, _STREAM_EVENT_CONTEXT_SET
);
3525 } else if (strcmp(left
, "packet.context") == 0) {
3526 if (_IS_SET(set
, _STREAM_PACKET_CONTEXT_SET
)) {
3527 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
3528 node
, "Duplicate `packet.context` entry in stream class.");
3533 ret
= visit_field_class_specifier_list(
3534 ctx
, _BT_LIST_FIRST_ENTRY(&node
->u
.ctf_expression
.right
, struct ctf_node
, siblings
),
3535 &stream_class
->packet_context_fc
);
3537 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
3538 node
, "Cannot create stream class's packet context field class.");
3542 BT_ASSERT(stream_class
->packet_context_fc
);
3543 ret
= auto_map_fields_to_trace_clock_class(ctx
, stream_class
->packet_context_fc
,
3546 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
3548 "Cannot automatically map specific packet context field class fields named `timestamp_begin` to trace's clock class.");
3552 ret
= auto_map_fields_to_trace_clock_class(ctx
, stream_class
->packet_context_fc
,
3555 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
3557 "Cannot automatically map specific packet context field class fields named `timestamp_end` to trace's clock class.");
3561 _SET(set
, _STREAM_PACKET_CONTEXT_SET
);
3563 _BT_COMP_LOGW_NODE(node
,
3564 "Unknown attribute in stream class: "
3586 static int visit_stream_decl(struct ctf_visitor_generate_ir
*ctx
, struct ctf_node
*node
)
3590 struct ctf_node
*iter
;
3591 struct ctf_stream_class
*stream_class
= NULL
;
3592 struct bt_list_head
*decl_list
= &node
->u
.stream
.declaration_list
;
3594 if (node
->visited
) {
3598 node
->visited
= TRUE
;
3599 stream_class
= ctf_stream_class_create();
3600 BT_ASSERT(stream_class
);
3601 _TRY_PUSH_SCOPE_OR_GOTO_ERROR();
3603 bt_list_for_each_entry (iter
, decl_list
, siblings
) {
3604 ret
= visit_stream_decl_entry(ctx
, iter
, stream_class
, &set
);
3606 _BT_COMP_LOGE_APPEND_CAUSE_NODE(iter
,
3607 "Cannot visit stream class's entry: "
3617 if (_IS_SET(&set
, _STREAM_ID_SET
)) {
3618 /* Check that packet header has `stream_id` field */
3619 struct ctf_named_field_class
*named_fc
= NULL
;
3621 if (!ctx
->ctf_tc
->packet_header_fc
) {
3622 _BT_COMP_LOGE_APPEND_CAUSE_NODE(node
, "Stream class has a `id` attribute, "
3623 "but trace has no packet header field class.");
3628 named_fc
= ctf_field_class_struct_borrow_member_by_name(
3629 ctf_field_class_as_struct(ctx
->ctf_tc
->packet_header_fc
), "stream_id");
3631 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
3632 node
, "Stream class has a `id` attribute, "
3633 "but trace's packet header field class has no `stream_id` field.");
3638 if (named_fc
->fc
->type
!= CTF_FIELD_CLASS_TYPE_INT
&&
3639 named_fc
->fc
->type
!= CTF_FIELD_CLASS_TYPE_ENUM
) {
3640 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
3642 "Stream class has a `id` attribute, "
3643 "but trace's packet header field class's `stream_id` field is not an integer field class.");
3648 /* Allow only _one_ ID-less stream */
3649 if (ctx
->ctf_tc
->stream_classes
->len
!= 0) {
3650 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
3652 "Missing `id` attribute in stream class as there's more than one stream class in the trace.");
3657 /* Automatic ID: 0 */
3658 stream_class
->id
= 0;
3662 * Make sure that this stream class's ID is currently unique in
3665 if (ctf_trace_class_borrow_stream_class_by_id(ctx
->ctf_tc
, stream_class
->id
)) {
3666 _BT_COMP_LOGE_APPEND_CAUSE_NODE(node
, "Duplicate stream class (same ID): id=%" PRId64
,
3672 g_ptr_array_add(ctx
->ctf_tc
->stream_classes
, stream_class
);
3673 stream_class
= NULL
;
3677 ctf_stream_class_destroy(stream_class
);
3678 stream_class
= NULL
;
3684 static int visit_trace_decl_entry(struct ctf_visitor_generate_ir
*ctx
, struct ctf_node
*node
,
3691 switch (node
->type
) {
3693 ret
= visit_field_class_def(ctx
, node
->u
.field_class_def
.field_class_specifier_list
,
3694 &node
->u
.field_class_def
.field_class_declarators
);
3696 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
3697 node
, "Cannot add field class found in trace (`trace` block).");
3701 case NODE_TYPEALIAS
:
3702 ret
= visit_field_class_alias(ctx
, node
->u
.field_class_alias
.target
,
3703 node
->u
.field_class_alias
.alias
);
3705 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
3706 node
, "Cannot add field class alias found in trace (`trace` block).");
3710 case NODE_CTF_EXPRESSION
:
3712 left
= ctf_ast_concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
3714 _BT_COMP_LOGE_APPEND_CAUSE_NODE(node
, "Cannot concatenate unary strings.");
3719 if (strcmp(left
, "major") == 0) {
3720 if (_IS_SET(set
, _TRACE_MAJOR_SET
)) {
3721 _BT_COMP_LOGE_APPEND_CAUSE_DUP_ATTR(node
, "major", "trace");
3726 ret
= get_unary_unsigned(ctx
, &node
->u
.ctf_expression
.right
, &val
);
3728 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
3729 node
, "Unexpected unary expression for trace's `major` attribute.");
3735 _BT_COMP_LOGE_APPEND_CAUSE_NODE(node
,
3736 "Invalid trace's `minor` attribute: expecting 1.");
3741 ctx
->ctf_tc
->major
= val
;
3742 _SET(set
, _TRACE_MAJOR_SET
);
3743 } else if (strcmp(left
, "minor") == 0) {
3744 if (_IS_SET(set
, _TRACE_MINOR_SET
)) {
3745 _BT_COMP_LOGE_APPEND_CAUSE_DUP_ATTR(node
, "minor", "trace");
3750 ret
= get_unary_unsigned(ctx
, &node
->u
.ctf_expression
.right
, &val
);
3752 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
3753 node
, "Unexpected unary expression for trace's `minor` attribute.");
3759 _BT_COMP_LOGE_APPEND_CAUSE_NODE(node
,
3760 "Invalid trace's `minor` attribute: expecting 8.");
3765 ctx
->ctf_tc
->minor
= val
;
3766 _SET(set
, _TRACE_MINOR_SET
);
3767 } else if (strcmp(left
, "uuid") == 0) {
3768 if (_IS_SET(set
, _TRACE_UUID_SET
)) {
3769 _BT_COMP_LOGE_APPEND_CAUSE_DUP_ATTR(node
, "uuid", "trace");
3774 ret
= get_unary_uuid(ctx
, &node
->u
.ctf_expression
.right
, ctx
->ctf_tc
->uuid
);
3776 _BT_COMP_LOGE_APPEND_CAUSE_NODE(node
, "Invalid trace's `uuid` attribute.");
3780 ctx
->ctf_tc
->is_uuid_set
= true;
3781 _SET(set
, _TRACE_UUID_SET
);
3782 } else if (strcmp(left
, "byte_order") == 0) {
3783 /* Default byte order is already known at this stage */
3784 if (_IS_SET(set
, _TRACE_BYTE_ORDER_SET
)) {
3785 _BT_COMP_LOGE_APPEND_CAUSE_DUP_ATTR(node
, "byte_order", "trace");
3790 BT_ASSERT(ctx
->ctf_tc
->default_byte_order
!= CTF_BYTE_ORDER_UNKNOWN
);
3791 _SET(set
, _TRACE_BYTE_ORDER_SET
);
3792 } else if (strcmp(left
, "packet.header") == 0) {
3793 if (_IS_SET(set
, _TRACE_PACKET_HEADER_SET
)) {
3794 _BT_COMP_LOGE_APPEND_CAUSE_NODE(node
, "Duplicate `packet.header` entry in trace.");
3799 ret
= visit_field_class_specifier_list(
3800 ctx
, _BT_LIST_FIRST_ENTRY(&node
->u
.ctf_expression
.right
, struct ctf_node
, siblings
),
3801 &ctx
->ctf_tc
->packet_header_fc
);
3803 _BT_COMP_LOGE_APPEND_CAUSE_NODE(node
,
3804 "Cannot create trace's packet header field class.");
3808 BT_ASSERT(ctx
->ctf_tc
->packet_header_fc
);
3809 _SET(set
, _TRACE_PACKET_HEADER_SET
);
3811 _BT_COMP_LOGW_NODE(node
,
3812 "Unknown attribute in stream class: "
3822 _BT_COMP_LOGE_APPEND_CAUSE_NODE(node
, "Unknown expression in trace.");
3834 static int visit_trace_decl(struct ctf_visitor_generate_ir
*ctx
, struct ctf_node
*node
)
3838 struct ctf_node
*iter
;
3839 struct bt_list_head
*decl_list
= &node
->u
.trace
.declaration_list
;
3841 if (node
->visited
) {
3845 node
->visited
= TRUE
;
3847 if (ctx
->is_trace_visited
) {
3848 _BT_COMP_LOGE_APPEND_CAUSE_NODE(node
, "Duplicate trace (`trace` block).");
3853 _TRY_PUSH_SCOPE_OR_GOTO_ERROR();
3855 bt_list_for_each_entry (iter
, decl_list
, siblings
) {
3856 ret
= visit_trace_decl_entry(ctx
, iter
, &set
);
3858 _BT_COMP_LOGE_APPEND_CAUSE_NODE(iter
,
3859 "Cannot visit trace's entry (`trace` block): "
3869 if (!_IS_SET(&set
, _TRACE_MAJOR_SET
)) {
3870 _BT_COMP_LOGE_APPEND_CAUSE_NODE(node
,
3871 "Missing `major` attribute in trace (`trace` block).");
3876 if (!_IS_SET(&set
, _TRACE_MINOR_SET
)) {
3877 _BT_COMP_LOGE_APPEND_CAUSE_NODE(node
,
3878 "Missing `minor` attribute in trace (`trace` block).");
3883 if (!_IS_SET(&set
, _TRACE_BYTE_ORDER_SET
)) {
3884 _BT_COMP_LOGE_APPEND_CAUSE_NODE(node
,
3885 "Missing `byte_order` attribute in trace (`trace` block).");
3890 ctx
->is_trace_visited
= true;
3899 static int visit_env(struct ctf_visitor_generate_ir
*ctx
, struct ctf_node
*node
)
3903 struct ctf_node
*entry_node
;
3904 struct bt_list_head
*decl_list
= &node
->u
.env
.declaration_list
;
3906 if (node
->visited
) {
3910 node
->visited
= TRUE
;
3912 bt_list_for_each_entry (entry_node
, decl_list
, siblings
) {
3913 struct bt_list_head
*right_head
= &entry_node
->u
.ctf_expression
.right
;
3915 if (entry_node
->type
!= NODE_CTF_EXPRESSION
) {
3916 _BT_COMP_LOGE_APPEND_CAUSE_NODE(entry_node
,
3917 "Wrong expression in environment entry: "
3924 left
= ctf_ast_concatenate_unary_strings(&entry_node
->u
.ctf_expression
.left
);
3926 _BT_COMP_LOGE_APPEND_CAUSE_NODE(entry_node
, "Cannot get environment entry's name.");
3931 if (is_unary_string(right_head
)) {
3932 char *right
= ctf_ast_concatenate_unary_strings(right_head
);
3935 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
3937 "Unexpected unary expression for environment entry's value: "
3944 if (strcmp(left
, "tracer_name") == 0) {
3945 if (strncmp(right
, "lttng", 5) == 0) {
3946 BT_COMP_LOGI("Detected LTTng trace from `%s` environment value: "
3947 "tracer-name=\"%s\"",
3949 ctx
->is_lttng
= true;
3953 ctf_trace_class_append_env_entry(ctx
->ctf_tc
, left
, CTF_TRACE_CLASS_ENV_ENTRY_TYPE_STR
,
3956 } else if (is_unary_unsigned(right_head
) || is_unary_signed(right_head
)) {
3959 if (is_unary_unsigned(right_head
)) {
3960 ret
= get_unary_unsigned(ctx
, right_head
, (uint64_t *) &v
);
3962 ret
= get_unary_signed(right_head
, &v
);
3965 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
3967 "Unexpected unary expression for environment entry's value: "
3974 ctf_trace_class_append_env_entry(ctx
->ctf_tc
, left
, CTF_TRACE_CLASS_ENV_ENTRY_TYPE_INT
,
3977 _BT_COMP_LOGW_NODE(entry_node
,
3978 "Environment entry has unknown type: "
3995 static int set_trace_byte_order(struct ctf_visitor_generate_ir
*ctx
, struct ctf_node
*trace_node
)
4000 struct ctf_node
*node
;
4001 struct bt_list_head
*decl_list
= &trace_node
->u
.trace
.declaration_list
;
4003 bt_list_for_each_entry (node
, decl_list
, siblings
) {
4004 if (node
->type
== NODE_CTF_EXPRESSION
) {
4005 struct ctf_node
*right_node
;
4007 left
= ctf_ast_concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
4009 _BT_COMP_LOGE_APPEND_CAUSE_NODE(node
, "Cannot concatenate unary strings.");
4014 if (strcmp(left
, "byte_order") == 0) {
4015 enum ctf_byte_order bo
;
4017 if (_IS_SET(&set
, _TRACE_BYTE_ORDER_SET
)) {
4018 _BT_COMP_LOGE_APPEND_CAUSE_DUP_ATTR(node
, "byte_order", "trace");
4023 _SET(&set
, _TRACE_BYTE_ORDER_SET
);
4025 _BT_LIST_FIRST_ENTRY(&node
->u
.ctf_expression
.right
, struct ctf_node
, siblings
);
4026 bo
= byte_order_from_unary_expr(ctx
, right_node
);
4027 if (bo
== CTF_BYTE_ORDER_UNKNOWN
) {
4028 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
4029 node
, "Invalid `byte_order` attribute in trace (`trace` block): "
4030 "expecting `le`, `be`, or `network`.");
4033 } else if (bo
== CTF_BYTE_ORDER_DEFAULT
) {
4034 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
4035 node
, "Invalid `byte_order` attribute in trace (`trace` block): "
4036 "cannot be set to `native` here.");
4041 ctx
->ctf_tc
->default_byte_order
= bo
;
4049 if (!_IS_SET(&set
, _TRACE_BYTE_ORDER_SET
)) {
4050 _BT_COMP_LOGE_APPEND_CAUSE_NODE(trace_node
,
4051 "Missing `byte_order` attribute in trace (`trace` block).");
4063 static int visit_clock_decl_entry(struct ctf_visitor_generate_ir
*ctx
, struct ctf_node
*entry_node
,
4064 struct ctf_clock_class
*clock
, int *set
, int64_t *offset_seconds
,
4065 uint64_t *offset_cycles
)
4070 if (entry_node
->type
!= NODE_CTF_EXPRESSION
) {
4071 _BT_COMP_LOGE_APPEND_CAUSE_NODE(entry_node
, "Unexpected node type: node-type=%d",
4077 left
= ctf_ast_concatenate_unary_strings(&entry_node
->u
.ctf_expression
.left
);
4079 _BT_COMP_LOGE_APPEND_CAUSE_NODE(entry_node
, "Cannot concatenate unary strings.");
4084 if (strcmp(left
, "name") == 0) {
4087 if (_IS_SET(set
, _CLOCK_NAME_SET
)) {
4088 _BT_COMP_LOGE_APPEND_CAUSE_DUP_ATTR(entry_node
, "name", "clock class");
4093 right
= ctf_ast_concatenate_unary_strings(&entry_node
->u
.ctf_expression
.right
);
4095 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
4096 entry_node
, "Unexpected unary expression for clock class's `name` attribute.");
4101 g_string_assign(clock
->name
, right
);
4103 _SET(set
, _CLOCK_NAME_SET
);
4104 } else if (strcmp(left
, "uuid") == 0) {
4107 if (_IS_SET(set
, _CLOCK_UUID_SET
)) {
4108 _BT_COMP_LOGE_APPEND_CAUSE_DUP_ATTR(entry_node
, "uuid", "clock class");
4113 ret
= get_unary_uuid(ctx
, &entry_node
->u
.ctf_expression
.right
, uuid
);
4115 _BT_COMP_LOGE_APPEND_CAUSE_NODE(entry_node
, "Invalid clock class's `uuid` attribute.");
4119 clock
->has_uuid
= true;
4120 bt_uuid_copy(clock
->uuid
, uuid
);
4121 _SET(set
, _CLOCK_UUID_SET
);
4122 } else if (strcmp(left
, "description") == 0) {
4125 if (_IS_SET(set
, _CLOCK_DESCRIPTION_SET
)) {
4126 _BT_COMP_LOGE_APPEND_CAUSE_DUP_ATTR(entry_node
, "description", "clock class");
4131 right
= ctf_ast_concatenate_unary_strings(&entry_node
->u
.ctf_expression
.right
);
4133 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
4135 "Unexpected unary expression for clock class's `description` attribute.");
4140 g_string_assign(clock
->description
, right
);
4142 _SET(set
, _CLOCK_DESCRIPTION_SET
);
4143 } else if (strcmp(left
, "freq") == 0) {
4144 uint64_t freq
= UINT64_C(-1);
4146 if (_IS_SET(set
, _CLOCK_FREQ_SET
)) {
4147 _BT_COMP_LOGE_APPEND_CAUSE_DUP_ATTR(entry_node
, "freq", "clock class");
4152 ret
= get_unary_unsigned(ctx
, &entry_node
->u
.ctf_expression
.right
, &freq
);
4154 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
4155 entry_node
, "Unexpected unary expression for clock class's `freq` attribute.");
4160 if (freq
== UINT64_C(-1) || freq
== 0) {
4161 _BT_COMP_LOGE_APPEND_CAUSE_NODE(entry_node
,
4162 "Invalid clock class frequency: freq=%" PRIu64
, freq
);
4167 clock
->frequency
= freq
;
4168 _SET(set
, _CLOCK_FREQ_SET
);
4169 } else if (strcmp(left
, "precision") == 0) {
4172 if (_IS_SET(set
, _CLOCK_PRECISION_SET
)) {
4173 _BT_COMP_LOGE_APPEND_CAUSE_DUP_ATTR(entry_node
, "precision", "clock class");
4178 ret
= get_unary_unsigned(ctx
, &entry_node
->u
.ctf_expression
.right
, &precision
);
4180 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
4181 entry_node
, "Unexpected unary expression for clock class's `precision` attribute.");
4186 clock
->precision
= precision
;
4187 _SET(set
, _CLOCK_PRECISION_SET
);
4188 } else if (strcmp(left
, "offset_s") == 0) {
4189 if (_IS_SET(set
, _CLOCK_OFFSET_S_SET
)) {
4190 _BT_COMP_LOGE_APPEND_CAUSE_DUP_ATTR(entry_node
, "offset_s", "clock class");
4195 ret
= get_unary_signed(&entry_node
->u
.ctf_expression
.right
, offset_seconds
);
4197 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
4198 entry_node
, "Unexpected unary expression for clock class's `offset_s` attribute.");
4203 _SET(set
, _CLOCK_OFFSET_S_SET
);
4204 } else if (strcmp(left
, "offset") == 0) {
4205 if (_IS_SET(set
, _CLOCK_OFFSET_SET
)) {
4206 _BT_COMP_LOGE_APPEND_CAUSE_DUP_ATTR(entry_node
, "offset", "clock class");
4211 ret
= get_unary_unsigned(ctx
, &entry_node
->u
.ctf_expression
.right
, offset_cycles
);
4213 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
4214 entry_node
, "Unexpected unary expression for clock class's `offset` attribute.");
4219 _SET(set
, _CLOCK_OFFSET_SET
);
4220 } else if (strcmp(left
, "absolute") == 0) {
4221 struct ctf_node
*right
;
4223 if (_IS_SET(set
, _CLOCK_ABSOLUTE_SET
)) {
4224 _BT_COMP_LOGE_APPEND_CAUSE_DUP_ATTR(entry_node
, "absolute", "clock class");
4230 _BT_LIST_FIRST_ENTRY(&entry_node
->u
.ctf_expression
.right
, struct ctf_node
, siblings
);
4231 ret
= get_boolean(ctx
, right
);
4233 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
4234 entry_node
, "Unexpected unary expression for clock class's `absolute` attribute.");
4239 clock
->is_absolute
= ret
;
4240 _SET(set
, _CLOCK_ABSOLUTE_SET
);
4242 _BT_COMP_LOGW_NODE(entry_node
, "Unknown attribute in clock class: attr-name=\"%s\"", left
);
4254 static inline uint64_t cycles_from_ns(uint64_t frequency
, uint64_t ns
)
4259 if (frequency
== UINT64_C(1000000000)) {
4262 cycles
= (uint64_t) (((double) ns
* (double) frequency
) / 1e9
);
4268 static void calibrate_clock_class_offsets(int64_t *offset_seconds
, uint64_t *offset_cycles
,
4271 if (*offset_cycles
>= freq
) {
4272 const uint64_t s_in_offset_cycles
= *offset_cycles
/ freq
;
4274 *offset_seconds
+= (int64_t) s_in_offset_cycles
;
4275 *offset_cycles
-= (s_in_offset_cycles
* freq
);
4279 static void apply_clock_class_is_absolute(struct ctf_visitor_generate_ir
*ctx
,
4280 struct ctf_clock_class
*clock
)
4282 if (ctx
->decoder_config
.force_clock_class_origin_unix_epoch
) {
4283 clock
->is_absolute
= true;
4289 static void apply_clock_class_offset(struct ctf_visitor_generate_ir
*ctx
,
4290 struct ctf_clock_class
*clock
)
4293 int64_t offset_s_to_apply
= ctx
->decoder_config
.clock_class_offset_s
;
4294 uint64_t offset_ns_to_apply
;
4295 int64_t cur_offset_s
;
4296 uint64_t cur_offset_cycles
;
4298 if (ctx
->decoder_config
.clock_class_offset_s
== 0 &&
4299 ctx
->decoder_config
.clock_class_offset_ns
== 0) {
4303 /* Transfer nanoseconds to seconds as much as possible */
4304 if (ctx
->decoder_config
.clock_class_offset_ns
< 0) {
4305 const int64_t abs_ns
= -ctx
->decoder_config
.clock_class_offset_ns
;
4306 const int64_t abs_extra_s
= abs_ns
/ INT64_C(1000000000) + 1;
4307 const int64_t extra_s
= -abs_extra_s
;
4308 const int64_t offset_ns
=
4309 ctx
->decoder_config
.clock_class_offset_ns
- (extra_s
* INT64_C(1000000000));
4311 BT_ASSERT(offset_ns
> 0);
4312 offset_ns_to_apply
= (uint64_t) offset_ns
;
4313 offset_s_to_apply
+= extra_s
;
4315 const int64_t extra_s
= ctx
->decoder_config
.clock_class_offset_ns
/ INT64_C(1000000000);
4316 const int64_t offset_ns
=
4317 ctx
->decoder_config
.clock_class_offset_ns
- (extra_s
* INT64_C(1000000000));
4319 BT_ASSERT(offset_ns
>= 0);
4320 offset_ns_to_apply
= (uint64_t) offset_ns
;
4321 offset_s_to_apply
+= extra_s
;
4324 freq
= clock
->frequency
;
4325 cur_offset_s
= clock
->offset_seconds
;
4326 cur_offset_cycles
= clock
->offset_cycles
;
4329 cur_offset_s
+= offset_s_to_apply
;
4330 cur_offset_cycles
+= cycles_from_ns(freq
, offset_ns_to_apply
);
4333 * Recalibrate offsets because the part in cycles can be greater
4334 * than the frequency at this point.
4336 calibrate_clock_class_offsets(&cur_offset_s
, &cur_offset_cycles
, freq
);
4338 /* Set final offsets */
4339 clock
->offset_seconds
= cur_offset_s
;
4340 clock
->offset_cycles
= cur_offset_cycles
;
4346 static int visit_clock_decl(struct ctf_visitor_generate_ir
*ctx
, struct ctf_node
*clock_node
)
4350 struct ctf_clock_class
*clock
;
4351 struct ctf_node
*entry_node
;
4352 struct bt_list_head
*decl_list
= &clock_node
->u
.clock
.declaration_list
;
4353 const char *clock_class_name
;
4354 int64_t offset_seconds
= 0;
4355 uint64_t offset_cycles
= 0;
4358 if (clock_node
->visited
) {
4362 clock_node
->visited
= TRUE
;
4364 /* CTF 1.8's default frequency for a clock class is 1 GHz */
4365 clock
= ctf_clock_class_create();
4367 _BT_COMP_LOGE_APPEND_CAUSE_NODE(clock_node
, "Cannot create default clock class.");
4372 bt_list_for_each_entry (entry_node
, decl_list
, siblings
) {
4373 ret
= visit_clock_decl_entry(ctx
, entry_node
, clock
, &set
, &offset_seconds
, &offset_cycles
);
4375 _BT_COMP_LOGE_APPEND_CAUSE_NODE(entry_node
, "Cannot visit clock class's entry: ret=%d",
4381 if (!_IS_SET(&set
, _CLOCK_NAME_SET
)) {
4382 _BT_COMP_LOGE_APPEND_CAUSE_NODE(clock_node
, "Missing `name` attribute in clock class.");
4387 clock_class_name
= clock
->name
->str
;
4388 BT_ASSERT(clock_class_name
);
4389 if (ctx
->is_lttng
&& strcmp(clock_class_name
, "monotonic") == 0) {
4391 * Old versions of LTTng forgot to set its clock class
4392 * as absolute, even if it is. This is important because
4393 * it's a condition to be able to sort messages
4394 * from different sources.
4396 clock
->is_absolute
= true;
4400 * Adjust offsets so that the part in cycles is less than the
4401 * frequency (move to the part in seconds).
4403 freq
= clock
->frequency
;
4404 calibrate_clock_class_offsets(&offset_seconds
, &offset_cycles
, freq
);
4405 BT_ASSERT(offset_cycles
< clock
->frequency
);
4406 clock
->offset_seconds
= offset_seconds
;
4407 clock
->offset_cycles
= offset_cycles
;
4408 apply_clock_class_offset(ctx
, clock
);
4409 apply_clock_class_is_absolute(ctx
, clock
);
4410 g_ptr_array_add(ctx
->ctf_tc
->clock_classes
, clock
);
4415 ctf_clock_class_destroy(clock
);
4421 static int visit_root_decl(struct ctf_visitor_generate_ir
*ctx
, struct ctf_node
*root_decl_node
)
4425 if (root_decl_node
->visited
) {
4429 root_decl_node
->visited
= TRUE
;
4431 switch (root_decl_node
->type
) {
4434 visit_field_class_def(ctx
, root_decl_node
->u
.field_class_def
.field_class_specifier_list
,
4435 &root_decl_node
->u
.field_class_def
.field_class_declarators
);
4437 _BT_COMP_LOGE_APPEND_CAUSE_NODE(root_decl_node
,
4438 "Cannot add field class found in root scope.");
4442 case NODE_TYPEALIAS
:
4443 ret
= visit_field_class_alias(ctx
, root_decl_node
->u
.field_class_alias
.target
,
4444 root_decl_node
->u
.field_class_alias
.alias
);
4446 _BT_COMP_LOGE_APPEND_CAUSE_NODE(root_decl_node
,
4447 "Cannot add field class alias found in root scope.");
4451 case NODE_TYPE_SPECIFIER_LIST
:
4453 struct ctf_field_class
*decl
= NULL
;
4456 * Just add the field class specifier to the root
4457 * declaration scope. Put local reference.
4459 ret
= visit_field_class_specifier_list(ctx
, root_decl_node
, &decl
);
4461 _BT_COMP_LOGE_APPEND_CAUSE_NODE(root_decl_node
,
4462 "Cannot visit root scope's field class: "
4469 ctf_field_class_destroy(decl
);
4474 _BT_COMP_LOGE_APPEND_CAUSE_NODE(root_decl_node
, "Unexpected node type: node-type=%d",
4475 root_decl_node
->type
);
4485 struct ctf_visitor_generate_ir
*
4486 ctf_visitor_generate_ir_create(const struct ctf_metadata_decoder_config
*decoder_config
)
4488 struct ctf_visitor_generate_ir
*ctx
= NULL
;
4490 /* Create visitor's context */
4491 ctx
= ctx_create(decoder_config
);
4493 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR
, decoder_config
->log_level
, decoder_config
->self_comp
,
4494 "Cannot create visitor's context.");
4509 void ctf_visitor_generate_ir_destroy(struct ctf_visitor_generate_ir
*visitor
)
4511 ctx_destroy(visitor
);
4515 bt_trace_class
*ctf_visitor_generate_ir_get_ir_trace_class(struct ctf_visitor_generate_ir
*ctx
)
4519 if (ctx
->trace_class
) {
4520 bt_trace_class_get_ref(ctx
->trace_class
);
4523 return ctx
->trace_class
;
4527 struct ctf_trace_class
*
4528 ctf_visitor_generate_ir_borrow_ctf_trace_class(struct ctf_visitor_generate_ir
*ctx
)
4531 BT_ASSERT_DBG(ctx
->ctf_tc
);
4536 int ctf_visitor_generate_ir_visit_node(struct ctf_visitor_generate_ir
*ctx
, struct ctf_node
*node
)
4540 BT_COMP_LOGI_STR("Visiting metadata's AST to generate CTF IR objects.");
4542 switch (node
->type
) {
4545 struct ctf_node
*iter
;
4546 bool got_trace_decl
= false;
4549 * The first thing we need is the native byte order of
4550 * the trace block, because early class aliases can have
4551 * a `byte_order` attribute set to `native`. If we don't
4552 * have the native byte order yet, and we don't have any
4553 * trace block yet, then fail with EINCOMPLETE.
4555 if (ctx
->ctf_tc
->default_byte_order
== CTF_BYTE_ORDER_UNKNOWN
) {
4556 bt_list_for_each_entry (iter
, &node
->u
.root
.trace
, siblings
) {
4557 if (got_trace_decl
) {
4558 _BT_COMP_LOGE_APPEND_CAUSE_NODE(node
, "Duplicate trace (`trace` block).");
4563 ret
= set_trace_byte_order(ctx
, iter
);
4565 _BT_COMP_LOGE_APPEND_CAUSE_NODE(node
,
4566 "Cannot set trace's native byte order: "
4572 got_trace_decl
= true;
4575 if (!got_trace_decl
) {
4576 BT_COMP_LOGD_STR("Incomplete AST: need trace (`trace` block).");
4582 BT_ASSERT(ctx
->ctf_tc
->default_byte_order
== CTF_BYTE_ORDER_LITTLE
||
4583 ctx
->ctf_tc
->default_byte_order
== CTF_BYTE_ORDER_BIG
);
4584 BT_ASSERT(ctx
->current_scope
&& !ctx
->current_scope
->parent_scope
);
4587 bt_list_for_each_entry (iter
, &node
->u
.root
.env
, siblings
) {
4588 ret
= visit_env(ctx
, iter
);
4590 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
4592 "Cannot visit trace's environment (`env` block) entry: "
4599 BT_ASSERT(ctx
->current_scope
&& !ctx
->current_scope
->parent_scope
);
4602 * Visit clock blocks.
4604 bt_list_for_each_entry (iter
, &node
->u
.root
.clock
, siblings
) {
4605 ret
= visit_clock_decl(ctx
, iter
);
4607 _BT_COMP_LOGE_APPEND_CAUSE_NODE(iter
, "Cannot visit clock class: ret=%d", ret
);
4612 BT_ASSERT(ctx
->current_scope
&& !ctx
->current_scope
->parent_scope
);
4615 * Visit root declarations next, as they can be used by any
4618 bt_list_for_each_entry (iter
, &node
->u
.root
.declaration_list
, siblings
) {
4619 ret
= visit_root_decl(ctx
, iter
);
4621 _BT_COMP_LOGE_APPEND_CAUSE_NODE(iter
, "Cannot visit root entry: ret=%d", ret
);
4626 BT_ASSERT(ctx
->current_scope
&& !ctx
->current_scope
->parent_scope
);
4628 /* Callsite blocks are not supported */
4629 bt_list_for_each_entry (iter
, &node
->u
.root
.callsite
, siblings
) {
4630 _BT_COMP_LOGW_NODE(iter
, "\"callsite\" blocks are not supported as of this version.");
4633 BT_ASSERT(ctx
->current_scope
&& !ctx
->current_scope
->parent_scope
);
4636 bt_list_for_each_entry (iter
, &node
->u
.root
.trace
, siblings
) {
4637 ret
= visit_trace_decl(ctx
, iter
);
4639 _BT_COMP_LOGE_APPEND_CAUSE_NODE(iter
,
4640 "Cannot visit trace (`trace` block): "
4647 BT_ASSERT(ctx
->current_scope
&& !ctx
->current_scope
->parent_scope
);
4650 bt_list_for_each_entry (iter
, &node
->u
.root
.stream
, siblings
) {
4651 ret
= visit_stream_decl(ctx
, iter
);
4653 _BT_COMP_LOGE_APPEND_CAUSE_NODE(iter
, "Cannot visit stream class: ret=%d", ret
);
4658 BT_ASSERT(ctx
->current_scope
&& !ctx
->current_scope
->parent_scope
);
4661 bt_list_for_each_entry (iter
, &node
->u
.root
.event
, siblings
) {
4662 ret
= visit_event_decl(ctx
, iter
);
4664 _BT_COMP_LOGE_APPEND_CAUSE_NODE(iter
, "Cannot visit event class: ret=%d", ret
);
4669 BT_ASSERT(ctx
->current_scope
&& !ctx
->current_scope
->parent_scope
);
4673 _BT_COMP_LOGE_APPEND_CAUSE_NODE(node
, "Unexpected node type: node-type=%d", node
->type
);
4678 /* Update default clock classes */
4679 ret
= ctf_trace_class_update_default_clock_classes(ctx
->ctf_tc
, &ctx
->log_cfg
);
4685 /* Update trace class meanings */
4686 ret
= ctf_trace_class_update_meanings(ctx
->ctf_tc
);
4692 /* Update stream class configuration */
4693 ret
= ctf_trace_class_update_stream_class_config(ctx
->ctf_tc
);
4699 /* Update text arrays and sequences */
4700 ret
= ctf_trace_class_update_text_array_sequence(ctx
->ctf_tc
);
4706 /* Update structure/array/sequence alignments */
4707 ret
= ctf_trace_class_update_alignments(ctx
->ctf_tc
);
4713 /* Resolve sequence lengths and variant tags */
4714 ret
= ctf_trace_class_resolve_field_classes(ctx
->ctf_tc
, &ctx
->log_cfg
);
4720 if (ctx
->trace_class
) {
4722 * Update "in IR" for field classes.
4724 * If we have no IR trace class, then we'll have no way
4725 * to create IR fields anyway, so we leave all the
4726 * `in_ir` members false.
4728 ret
= ctf_trace_class_update_in_ir(ctx
->ctf_tc
);
4735 /* Update saved value indexes */
4736 ret
= ctf_trace_class_update_value_storing_indexes(ctx
->ctf_tc
);
4742 /* Validate what we have so far */
4743 ret
= ctf_trace_class_validate(ctx
->ctf_tc
, &ctx
->log_cfg
);
4750 * If there are fields which are not related to the CTF format
4751 * itself in the packet header and in event header field
4752 * classes, warn about it because they are never translated.
4754 ctf_trace_class_warn_meaningless_header_fields(ctx
->ctf_tc
, &ctx
->log_cfg
);
4756 if (ctx
->trace_class
) {
4757 /* Copy new CTF metadata -> new IR metadata */
4758 ret
= ctf_trace_class_translate(ctx
->log_cfg
.self_comp
, ctx
->trace_class
, ctx
->ctf_tc
);