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).
13 #include "logging.hpp"
15 #include "common/assert.h"
16 #include "common/common.h"
17 #include "common/uuid.h"
18 #include "compat/endian.h" /* IWYU pragma: keep */
19 #include "cpp-common/bt2c/logging.hpp"
22 #include "ctf-meta-visitors.hpp"
24 /* Bit value (left shift) */
25 #define _BV(_val) (1 << (_val))
27 /* Bit is set in a set of bits */
28 #define _IS_SET(_set, _mask) (*(_set) & (_mask))
30 /* Set bit in a set of bits */
31 #define _SET(_set, _mask) (*(_set) |= (_mask))
33 /* Try to push scope, or go to the `error` label */
34 #define _TRY_PUSH_SCOPE_OR_GOTO_ERROR() \
36 ret = ctx_push_scope(ctx); \
38 BT_CPPLOGE_APPEND_CAUSE_SPEC(ctx->logger, "Cannot push scope."); \
43 /* Bits for verifying existing attributes in various declarations */
46 _CLOCK_NAME_SET
= _BV(0),
47 _CLOCK_UUID_SET
= _BV(1),
48 _CLOCK_FREQ_SET
= _BV(2),
49 _CLOCK_PRECISION_SET
= _BV(3),
50 _CLOCK_OFFSET_S_SET
= _BV(4),
51 _CLOCK_OFFSET_SET
= _BV(5),
52 _CLOCK_ABSOLUTE_SET
= _BV(6),
53 _CLOCK_DESCRIPTION_SET
= _BV(7),
58 _INTEGER_ALIGN_SET
= _BV(0),
59 _INTEGER_SIZE_SET
= _BV(1),
60 _INTEGER_BASE_SET
= _BV(2),
61 _INTEGER_ENCODING_SET
= _BV(3),
62 _INTEGER_BYTE_ORDER_SET
= _BV(4),
63 _INTEGER_SIGNED_SET
= _BV(5),
64 _INTEGER_MAP_SET
= _BV(6),
69 _FLOAT_ALIGN_SET
= _BV(0),
70 _FLOAT_MANT_DIG_SET
= _BV(1),
71 _FLOAT_EXP_DIG_SET
= _BV(2),
72 _FLOAT_BYTE_ORDER_SET
= _BV(3),
77 _STRING_ENCODING_SET
= _BV(0),
82 _TRACE_MINOR_SET
= _BV(0),
83 _TRACE_MAJOR_SET
= _BV(1),
84 _TRACE_BYTE_ORDER_SET
= _BV(2),
85 _TRACE_UUID_SET
= _BV(3),
86 _TRACE_PACKET_HEADER_SET
= _BV(4),
91 _STREAM_ID_SET
= _BV(0),
92 _STREAM_PACKET_CONTEXT_SET
= _BV(1),
93 _STREAM_EVENT_HEADER_SET
= _BV(2),
94 _STREAM_EVENT_CONTEXT_SET
= _BV(3),
99 _EVENT_NAME_SET
= _BV(0),
100 _EVENT_ID_SET
= _BV(1),
101 _EVENT_MODEL_EMF_URI_SET
= _BV(2),
102 _EVENT_STREAM_ID_SET
= _BV(3),
103 _EVENT_LOG_LEVEL_SET
= _BV(4),
104 _EVENT_CONTEXT_SET
= _BV(5),
105 _EVENT_FIELDS_SET
= _BV(6),
114 LOG_LEVEL_WARNING
= 4,
115 LOG_LEVEL_NOTICE
= 5,
117 LOG_LEVEL_DEBUG_SYSTEM
= 7,
118 LOG_LEVEL_DEBUG_PROGRAM
= 8,
119 LOG_LEVEL_DEBUG_PROCESS
= 9,
120 LOG_LEVEL_DEBUG_MODULE
= 10,
121 LOG_LEVEL_DEBUG_UNIT
= 11,
122 LOG_LEVEL_DEBUG_FUNCTION
= 12,
123 LOG_LEVEL_DEBUG_LINE
= 13,
124 LOG_LEVEL_DEBUG
= 14,
128 /* Prefixes of class aliases */
129 #define _PREFIX_ALIAS 'a'
130 #define _PREFIX_ENUM 'e'
131 #define _PREFIX_STRUCT 's'
132 #define _PREFIX_VARIANT 'v'
134 /* First entry in a BT list */
135 #define _BT_LIST_FIRST_ENTRY(_ptr, _class, _member) bt_list_entry((_ptr)->next, _class, _member)
137 #define _BT_CPPLOGE_APPEND_CAUSE_DUP_ATTR(_node, _attr, _entity) \
138 _BT_CPPLOGE_APPEND_CAUSE_LINENO(ctx->logger, (_node)->lineno, \
139 "Duplicate attribute in {}: attr-name=\"{}\"", _entity, _attr)
141 #define _BT_CPPLOGE_NODE(_node, _msg, args...) \
142 _BT_CPPLOGE_LINENO(ctx->logger, (_node)->lineno, _msg, ##args)
144 #define _BT_CPPLOGE_APPEND_CAUSE_NODE(_node, _msg, args...) \
145 _BT_CPPLOGE_APPEND_CAUSE_LINENO(ctx->logger, (_node)->lineno, _msg, ##args)
147 #define _BT_CPPLOGW_NODE(_node, _msg, args...) \
148 _BT_CPPLOGW_LINENO(ctx->logger, (_node)->lineno, _msg, ##args)
150 #define _BT_CPPLOGT_NODE(_node, _msg, args...) \
151 _BT_CPPLOGT_LINENO(ctx->logger, (_node)->lineno, _msg, ##args)
154 * Declaration scope of a visitor context. This represents a TSDL
155 * lexical scope, so that aliases and named structures, variants,
156 * and enumerations may be registered and looked up hierarchically.
158 struct ctx_decl_scope
161 * Alias name to field class.
163 * GQuark -> struct ctf_field_class * (owned by this)
165 GHashTable
*decl_map
;
167 /* Parent scope; NULL if this is the root declaration scope */
168 struct ctx_decl_scope
*parent_scope
;
172 * Creates a new declaration scope.
174 * @param par_scope Parent scope (NULL if creating a root scope)
175 * @returns New declaration scope, or NULL on error
177 static struct ctx_decl_scope
*ctx_decl_scope_create(struct ctf_visitor_generate_ir
*ctx
,
178 struct ctx_decl_scope
*par_scope
)
180 struct ctx_decl_scope
*scope
;
182 scope
= g_new(struct ctx_decl_scope
, 1);
184 BT_CPPLOGE_APPEND_CAUSE_SPEC(ctx
->logger
, "Failed to allocate one declaration scope.");
188 scope
->decl_map
= g_hash_table_new_full(g_direct_hash
, g_direct_equal
, NULL
,
189 (GDestroyNotify
) ctf_field_class_destroy
);
190 scope
->parent_scope
= par_scope
;
197 * Destroys a declaration scope.
199 * This function does not destroy the parent scope.
201 * @param scope Scope to destroy
203 static void ctx_decl_scope_destroy(struct ctx_decl_scope
*scope
)
209 g_hash_table_destroy(scope
->decl_map
);
217 * Returns the GQuark of a prefixed alias.
219 * @param prefix Prefix character
221 * @returns Associated GQuark, or 0 on error
223 static GQuark
get_prefixed_named_quark(char prefix
, const char *name
)
226 std::string prname
= std::string
{prefix
} + name
;
227 return g_quark_from_string(prname
.c_str());
231 * Looks up a prefixed class alias within a declaration scope.
233 * @param scope Declaration scope
234 * @param prefix Prefix character
235 * @param name Alias name
236 * @param levels Number of levels to dig into (-1 means infinite)
237 * @param copy True to return a copy
238 * @returns Declaration (owned by caller if \p copy is true),
239 * or NULL if not found
241 static struct ctf_field_class
*ctx_decl_scope_lookup_prefix_alias(struct ctx_decl_scope
*scope
,
242 char prefix
, const char *name
,
243 int levels
, bool copy
)
247 struct ctf_field_class
*decl
= NULL
;
248 struct ctx_decl_scope
*cur_scope
= scope
;
252 qname
= get_prefixed_named_quark(prefix
, name
);
261 while (cur_scope
&& cur_levels
< levels
) {
262 decl
= (ctf_field_class
*) g_hash_table_lookup(cur_scope
->decl_map
,
263 (gconstpointer
) GUINT_TO_POINTER(qname
));
265 /* Caller's reference */
267 decl
= ctf_field_class_copy(decl
);
274 cur_scope
= cur_scope
->parent_scope
;
283 * Looks up a class alias within a declaration scope.
285 * @param scope Declaration scope
286 * @param name Alias name
287 * @param levels Number of levels to dig into (-1 means infinite)
288 * @param copy True to return a copy
289 * @returns Declaration (owned by caller if \p copy is true),
290 * or NULL if not found
292 static struct ctf_field_class
*ctx_decl_scope_lookup_alias(struct ctx_decl_scope
*scope
,
293 const char *name
, int levels
, bool copy
)
295 return ctx_decl_scope_lookup_prefix_alias(scope
, _PREFIX_ALIAS
, name
, levels
, copy
);
299 * Looks up an enumeration within a declaration scope.
301 * @param scope Declaration scope
302 * @param name Enumeration name
303 * @param levels Number of levels to dig into (-1 means infinite)
304 * @param copy True to return a copy
305 * @returns Declaration (owned by caller if \p copy is true),
306 * or NULL if not found
308 static struct ctf_field_class_enum
*
309 ctx_decl_scope_lookup_enum(struct ctx_decl_scope
*scope
, const char *name
, int levels
, bool copy
)
311 return ctf_field_class_as_enum(
312 ctx_decl_scope_lookup_prefix_alias(scope
, _PREFIX_ENUM
, name
, levels
, copy
));
316 * Looks up a structure within a declaration scope.
318 * @param scope Declaration scope
319 * @param name Structure name
320 * @param levels Number of levels to dig into (-1 means infinite)
321 * @param copy True to return a copy
322 * @returns Declaration (owned by caller if \p copy is true),
323 * or NULL if not found
325 static struct ctf_field_class_struct
*
326 ctx_decl_scope_lookup_struct(struct ctx_decl_scope
*scope
, const char *name
, int levels
, bool copy
)
328 return ctf_field_class_as_struct(
329 ctx_decl_scope_lookup_prefix_alias(scope
, _PREFIX_STRUCT
, name
, levels
, copy
));
333 * Looks up a variant within a declaration scope.
335 * @param scope Declaration scope
336 * @param name Variant name
337 * @param levels Number of levels to dig into (-1 means infinite)
338 * @param copy True to return a copy
339 * @returns Declaration (owned by caller if \p copy is true),
340 * or NULL if not found
342 static struct ctf_field_class_variant
*
343 ctx_decl_scope_lookup_variant(struct ctx_decl_scope
*scope
, const char *name
, int levels
, bool copy
)
345 return ctf_field_class_as_variant(
346 ctx_decl_scope_lookup_prefix_alias(scope
, _PREFIX_VARIANT
, name
, levels
, copy
));
350 * Registers a prefixed class alias within a declaration scope.
352 * @param scope Declaration scope
353 * @param prefix Prefix character
354 * @param name Alias name (non-NULL)
355 * @param decl Field class to register (copied)
356 * @returns 0 if registration went okay, negative value otherwise
358 static int ctx_decl_scope_register_prefix_alias(struct ctx_decl_scope
*scope
, char prefix
,
359 const char *name
, struct ctf_field_class
*decl
)
367 qname
= get_prefixed_named_quark(prefix
, name
);
373 /* Make sure alias does not exist in local scope */
374 if (ctx_decl_scope_lookup_prefix_alias(scope
, prefix
, name
, 1, false)) {
379 decl
= ctf_field_class_copy(decl
);
381 g_hash_table_insert(scope
->decl_map
, GUINT_TO_POINTER(qname
), decl
);
388 * Registers a class alias within a declaration scope.
390 * @param scope Declaration scope
391 * @param name Alias name (non-NULL)
392 * @param decl Field class to register (copied)
393 * @returns 0 if registration went okay, negative value otherwise
395 static int ctx_decl_scope_register_alias(struct ctx_decl_scope
*scope
, const char *name
,
396 struct ctf_field_class
*decl
)
398 return ctx_decl_scope_register_prefix_alias(scope
, _PREFIX_ALIAS
, name
, decl
);
402 * Registers an enumeration declaration within a declaration scope.
404 * @param scope Declaration scope
405 * @param name Enumeration name (non-NULL)
406 * @param decl Enumeration field class to register (copied)
407 * @returns 0 if registration went okay, negative value otherwise
409 static int ctx_decl_scope_register_enum(struct ctx_decl_scope
*scope
, const char *name
,
410 struct ctf_field_class_enum
*decl
)
412 return ctx_decl_scope_register_prefix_alias(scope
, _PREFIX_ENUM
, name
, &decl
->base
.base
.base
);
416 * Registers a structure declaration within a declaration scope.
418 * @param scope Declaration scope
419 * @param name Structure name (non-NULL)
420 * @param decl Structure field class to register (copied)
421 * @returns 0 if registration went okay, negative value otherwise
423 static int ctx_decl_scope_register_struct(struct ctx_decl_scope
*scope
, const char *name
,
424 struct ctf_field_class_struct
*decl
)
426 return ctx_decl_scope_register_prefix_alias(scope
, _PREFIX_STRUCT
, name
, &decl
->base
);
430 * Registers a variant declaration within a declaration scope.
432 * @param scope Declaration scope
433 * @param name Variant name (non-NULL)
434 * @param decl Variant field class to register
435 * @returns 0 if registration went okay, negative value otherwise
437 static int ctx_decl_scope_register_variant(struct ctx_decl_scope
*scope
, const char *name
,
438 struct ctf_field_class_variant
*decl
)
440 return ctx_decl_scope_register_prefix_alias(scope
, _PREFIX_VARIANT
, name
, &decl
->base
);
443 ctf_visitor_generate_ir::~ctf_visitor_generate_ir()
445 struct ctx_decl_scope
*scope
= this->current_scope
;
448 * Destroy all scopes, from current one to the root scope.
451 struct ctx_decl_scope
*parent_scope
= scope
->parent_scope
;
453 ctx_decl_scope_destroy(scope
);
454 scope
= parent_scope
;
458 ctf_trace_class_destroy(this->ctf_tc
);
463 * Creates a new visitor context.
465 * @param trace Associated trace
466 * @returns New visitor context, or NULL on error
468 static ctf_visitor_generate_ir::UP
469 ctx_create(const struct ctf_metadata_decoder_config
*decoder_config
, const bt2c::Logger
& logger
)
471 BT_ASSERT(decoder_config
);
473 ctf_visitor_generate_ir::UP ctx
{new ctf_visitor_generate_ir
{*decoder_config
, logger
}};
475 if (decoder_config
->self_comp
) {
476 bt_trace_class
*trace_class
= bt_trace_class_create(decoder_config
->self_comp
);
478 BT_CPPLOGE_APPEND_CAUSE_SPEC(ctx
->logger
, "Cannot create empty trace class.");
482 ctx
->trace_class
= bt2::TraceClass::Shared::createWithoutRef(trace_class
);
485 ctx
->ctf_tc
= ctf_trace_class_create();
487 BT_CPPLOGE_APPEND_CAUSE_SPEC(ctx
->logger
, "Cannot create CTF trace class.");
491 /* Root declaration scope */
492 ctx
->current_scope
= ctx_decl_scope_create(ctx
.get(), NULL
);
493 if (!ctx
->current_scope
) {
494 BT_CPPLOGE_APPEND_CAUSE_SPEC(ctx
->logger
, "Cannot create declaration scope.");
508 * Pushes a new declaration scope on top of a visitor context's
509 * declaration scope stack.
511 * @param ctx Visitor context
512 * @returns 0 on success, or a negative value on error
514 static int ctx_push_scope(struct ctf_visitor_generate_ir
*ctx
)
517 struct ctx_decl_scope
*new_scope
;
520 new_scope
= ctx_decl_scope_create(ctx
, ctx
->current_scope
);
522 BT_CPPLOGE_APPEND_CAUSE_SPEC(ctx
->logger
, "Cannot create declaration scope.");
527 ctx
->current_scope
= new_scope
;
533 static void ctx_pop_scope(struct ctf_visitor_generate_ir
*ctx
)
535 struct ctx_decl_scope
*parent_scope
= NULL
;
539 if (!ctx
->current_scope
) {
543 parent_scope
= ctx
->current_scope
->parent_scope
;
544 ctx_decl_scope_destroy(ctx
->current_scope
);
545 ctx
->current_scope
= parent_scope
;
551 static int visit_field_class_specifier_list(struct ctf_visitor_generate_ir
*ctx
,
552 struct ctf_node
*ts_list
,
553 struct ctf_field_class
**decl
);
555 static int is_unary_string(struct bt_list_head
*head
)
558 struct ctf_node
*node
;
560 bt_list_for_each_entry (node
, head
, siblings
) {
561 if (node
->type
!= NODE_UNARY_EXPRESSION
) {
565 if (node
->u
.unary_expression
.type
!= UNARY_STRING
) {
573 static const char *get_map_clock_name_value(struct bt_list_head
*head
)
576 struct ctf_node
*node
;
577 const char *name
= NULL
;
579 bt_list_for_each_entry (node
, head
, siblings
) {
581 int uexpr_type
= node
->u
.unary_expression
.type
;
582 int uexpr_link
= node
->u
.unary_expression
.link
;
583 int cond
= node
->type
!= NODE_UNARY_EXPRESSION
|| uexpr_type
!= UNARY_STRING
||
584 !((uexpr_link
!= UNARY_LINK_UNKNOWN
) ^ (i
== 0));
589 /* Needs to be chained with . */
590 switch (node
->u
.unary_expression
.link
) {
593 case UNARY_ARROWLINK
:
594 case UNARY_DOTDOTDOT
:
600 src_string
= node
->u
.unary_expression
.u
.string
;
604 if (strcmp("clock", src_string
)) {
612 if (strcmp("value", src_string
)) {
617 /* Extra identifier, unknown */
630 static int is_unary_unsigned(struct bt_list_head
*head
)
633 struct ctf_node
*node
;
635 bt_list_for_each_entry (node
, head
, siblings
) {
636 if (node
->type
!= NODE_UNARY_EXPRESSION
) {
640 if (node
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
648 static int get_unary_unsigned(struct ctf_visitor_generate_ir
*ctx
, struct bt_list_head
*head
,
653 struct ctf_node
*node
;
657 if (bt_list_empty(head
)) {
662 bt_list_for_each_entry (node
, head
, siblings
) {
663 int uexpr_type
= node
->u
.unary_expression
.type
;
664 int uexpr_link
= node
->u
.unary_expression
.link
;
665 int cond
= node
->type
!= NODE_UNARY_EXPRESSION
|| uexpr_type
!= UNARY_UNSIGNED_CONSTANT
||
666 uexpr_link
!= UNARY_LINK_UNKNOWN
|| i
!= 0;
668 _BT_CPPLOGE_APPEND_CAUSE_NODE(node
, "Invalid constant unsigned integer.");
673 *value
= node
->u
.unary_expression
.u
.unsigned_constant
;
681 static int is_unary_signed(struct bt_list_head
*head
)
684 struct ctf_node
*node
;
686 bt_list_for_each_entry (node
, head
, siblings
) {
687 if (node
->type
!= NODE_UNARY_EXPRESSION
) {
691 if (node
->u
.unary_expression
.type
!= UNARY_SIGNED_CONSTANT
) {
699 static int get_unary_signed(struct bt_list_head
*head
, int64_t *value
)
703 struct ctf_node
*node
;
705 bt_list_for_each_entry (node
, head
, siblings
) {
706 int uexpr_type
= node
->u
.unary_expression
.type
;
707 int uexpr_link
= node
->u
.unary_expression
.link
;
708 int cond
= node
->type
!= NODE_UNARY_EXPRESSION
||
709 (uexpr_type
!= UNARY_UNSIGNED_CONSTANT
&& uexpr_type
!= UNARY_SIGNED_CONSTANT
) ||
710 uexpr_link
!= UNARY_LINK_UNKNOWN
|| i
!= 0;
716 switch (uexpr_type
) {
717 case UNARY_UNSIGNED_CONSTANT
:
718 *value
= (int64_t) node
->u
.unary_expression
.u
.unsigned_constant
;
720 case UNARY_SIGNED_CONSTANT
:
721 *value
= node
->u
.unary_expression
.u
.signed_constant
;
735 static int get_unary_uuid(struct ctf_visitor_generate_ir
*ctx
, struct bt_list_head
*head
,
738 return ctf_ast_get_unary_uuid(head
, uuid
, ctx
->logger
);
741 static int get_boolean(struct ctf_visitor_generate_ir
*ctx
, struct ctf_node
*unary_expr
)
745 if (unary_expr
->type
!= NODE_UNARY_EXPRESSION
) {
746 _BT_CPPLOGE_APPEND_CAUSE_NODE(unary_expr
, "Expecting unary expression: node-type={}",
752 switch (unary_expr
->u
.unary_expression
.type
) {
753 case UNARY_UNSIGNED_CONSTANT
:
754 ret
= (unary_expr
->u
.unary_expression
.u
.unsigned_constant
!= 0);
756 case UNARY_SIGNED_CONSTANT
:
757 ret
= (unary_expr
->u
.unary_expression
.u
.signed_constant
!= 0);
761 const char *str
= unary_expr
->u
.unary_expression
.u
.string
;
763 if (strcmp(str
, "true") == 0 || strcmp(str
, "TRUE") == 0) {
765 } else if (strcmp(str
, "false") == 0 || strcmp(str
, "FALSE") == 0) {
768 _BT_CPPLOGE_APPEND_CAUSE_NODE(unary_expr
, "Unexpected boolean value: value=\"{}\"",
776 _BT_CPPLOGE_APPEND_CAUSE_NODE(unary_expr
, "Unexpected unary expression type: node-type={}",
777 unary_expr
->u
.unary_expression
.type
);
786 static enum ctf_byte_order
byte_order_from_unary_expr(struct ctf_visitor_generate_ir
*ctx
,
787 struct ctf_node
*unary_expr
)
790 enum ctf_byte_order bo
= CTF_BYTE_ORDER_UNKNOWN
;
792 if (unary_expr
->u
.unary_expression
.type
!= UNARY_STRING
) {
793 _BT_CPPLOGE_APPEND_CAUSE_NODE(
794 unary_expr
, "\"byte_order\" attribute: expecting `be`, `le`, `network`, or `native`.");
798 str
= unary_expr
->u
.unary_expression
.u
.string
;
800 if (strcmp(str
, "be") == 0 || strcmp(str
, "network") == 0) {
801 bo
= CTF_BYTE_ORDER_BIG
;
802 } else if (strcmp(str
, "le") == 0) {
803 bo
= CTF_BYTE_ORDER_LITTLE
;
804 } else if (strcmp(str
, "native") == 0) {
805 bo
= CTF_BYTE_ORDER_DEFAULT
;
807 _BT_CPPLOGE_APPEND_CAUSE_NODE(unary_expr
,
808 "Unexpected \"byte_order\" attribute value: "
809 "expecting `be`, `le`, `network`, or `native`: value=\"{}\"",
818 static enum ctf_byte_order
get_real_byte_order(struct ctf_visitor_generate_ir
*ctx
,
819 struct ctf_node
*uexpr
)
821 enum ctf_byte_order bo
= byte_order_from_unary_expr(ctx
, uexpr
);
823 if (bo
== CTF_BYTE_ORDER_DEFAULT
) {
824 bo
= ctx
->ctf_tc
->default_byte_order
;
830 static int is_align_valid(uint64_t align
)
832 return (align
!= 0) && !(align
& (align
- UINT64_C(1)));
835 static int get_class_specifier_name(struct ctf_visitor_generate_ir
*ctx
,
836 struct ctf_node
*cls_specifier
, GString
*str
)
840 if (cls_specifier
->type
!= NODE_TYPE_SPECIFIER
) {
841 _BT_CPPLOGE_APPEND_CAUSE_NODE(cls_specifier
, "Unexpected node type: node-type={}",
842 cls_specifier
->type
);
847 switch (cls_specifier
->u
.field_class_specifier
.type
) {
849 g_string_append(str
, "void");
852 g_string_append(str
, "char");
855 g_string_append(str
, "short");
858 g_string_append(str
, "int");
861 g_string_append(str
, "long");
864 g_string_append(str
, "float");
866 case TYPESPEC_DOUBLE
:
867 g_string_append(str
, "double");
869 case TYPESPEC_SIGNED
:
870 g_string_append(str
, "signed");
872 case TYPESPEC_UNSIGNED
:
873 g_string_append(str
, "unsigned");
876 g_string_append(str
, "bool");
878 case TYPESPEC_COMPLEX
:
879 g_string_append(str
, "_Complex");
881 case TYPESPEC_IMAGINARY
:
882 g_string_append(str
, "_Imaginary");
885 g_string_append(str
, "const");
887 case TYPESPEC_ID_TYPE
:
888 if (cls_specifier
->u
.field_class_specifier
.id_type
) {
889 g_string_append(str
, cls_specifier
->u
.field_class_specifier
.id_type
);
892 case TYPESPEC_STRUCT
:
894 struct ctf_node
*node
= cls_specifier
->u
.field_class_specifier
.node
;
896 if (!node
->u
._struct
.name
) {
897 _BT_CPPLOGE_APPEND_CAUSE_NODE(node
, "Unexpected empty structure field class name.");
902 g_string_append(str
, "struct ");
903 g_string_append(str
, node
->u
._struct
.name
);
906 case TYPESPEC_VARIANT
:
908 struct ctf_node
*node
= cls_specifier
->u
.field_class_specifier
.node
;
910 if (!node
->u
.variant
.name
) {
911 _BT_CPPLOGE_APPEND_CAUSE_NODE(node
, "Unexpected empty variant field class name.");
916 g_string_append(str
, "variant ");
917 g_string_append(str
, node
->u
.variant
.name
);
922 struct ctf_node
*node
= cls_specifier
->u
.field_class_specifier
.node
;
924 if (!node
->u
._enum
.enum_id
) {
925 _BT_CPPLOGE_APPEND_CAUSE_NODE(
926 node
, "Unexpected empty enumeration field class (`enum`) name.");
931 g_string_append(str
, "enum ");
932 g_string_append(str
, node
->u
._enum
.enum_id
);
935 case TYPESPEC_FLOATING_POINT
:
936 case TYPESPEC_INTEGER
:
937 case TYPESPEC_STRING
:
939 _BT_CPPLOGE_APPEND_CAUSE_NODE(cls_specifier
->u
.field_class_specifier
.node
,
940 "Unexpected field class specifier type: {}",
941 cls_specifier
->u
.field_class_specifier
.type
);
950 static int get_class_specifier_list_name(struct ctf_visitor_generate_ir
*ctx
,
951 struct ctf_node
*cls_specifier_list
, GString
*str
)
954 struct ctf_node
*iter
;
955 int alias_item_nr
= 0;
956 struct bt_list_head
*head
= &cls_specifier_list
->u
.field_class_specifier_list
.head
;
958 bt_list_for_each_entry (iter
, head
, siblings
) {
959 if (alias_item_nr
!= 0) {
960 g_string_append(str
, " ");
964 ret
= get_class_specifier_name(ctx
, iter
, str
);
974 static GQuark
create_class_alias_identifier(struct ctf_visitor_generate_ir
*ctx
,
975 struct ctf_node
*cls_specifier_list
,
976 struct ctf_node
*node_field_class_declarator
)
982 struct ctf_node
*iter
;
983 struct bt_list_head
*pointers
= &node_field_class_declarator
->u
.field_class_declarator
.pointers
;
985 str
= g_string_new("");
986 ret
= get_class_specifier_list_name(ctx
, cls_specifier_list
, str
);
988 g_string_free(str
, TRUE
);
992 bt_list_for_each_entry (iter
, pointers
, siblings
) {
993 g_string_append(str
, " *");
995 if (iter
->u
.pointer
.const_qualifier
) {
996 g_string_append(str
, " const");
1000 str_c
= g_string_free(str
, FALSE
);
1001 qalias
= g_quark_from_string(str_c
);
1008 static int visit_field_class_declarator(struct ctf_visitor_generate_ir
*ctx
,
1009 struct ctf_node
*cls_specifier_list
, GQuark
*field_name
,
1010 struct ctf_node
*node_field_class_declarator
,
1011 struct ctf_field_class
**field_decl
,
1012 struct ctf_field_class
*nested_decl
)
1015 * During this whole function, nested_decl is always OURS,
1016 * whereas field_decl is an output which we create, but
1017 * belongs to the caller (it is moved).
1022 /* Validate field class declarator node */
1023 if (node_field_class_declarator
) {
1024 if (node_field_class_declarator
->u
.field_class_declarator
.type
== TYPEDEC_UNKNOWN
) {
1025 _BT_CPPLOGE_APPEND_CAUSE_NODE(
1026 node_field_class_declarator
, "Unexpected field class declarator type: type={}",
1027 node_field_class_declarator
->u
.field_class_declarator
.type
);
1032 /* TODO: GCC bitfields not supported yet */
1033 if (node_field_class_declarator
->u
.field_class_declarator
.bitfield_len
!= NULL
) {
1034 _BT_CPPLOGE_APPEND_CAUSE_NODE(node_field_class_declarator
,
1035 "GCC bitfields are not supported as of this version.");
1041 /* Find the right nested declaration if not provided */
1043 if (node_field_class_declarator
&&
1044 !bt_list_empty(&node_field_class_declarator
->u
.field_class_declarator
.pointers
)) {
1048 * If we have a pointer declarator, it HAS to
1049 * be present in the field class aliases (else
1053 create_class_alias_identifier(ctx
, cls_specifier_list
, node_field_class_declarator
);
1054 nested_decl
= ctx_decl_scope_lookup_alias(ctx
->current_scope
, g_quark_to_string(qalias
),
1057 _BT_CPPLOGE_APPEND_CAUSE_NODE(node_field_class_declarator
,
1058 "Cannot find class alias: name=\"{}\"",
1059 g_quark_to_string(qalias
));
1064 if (nested_decl
->type
== CTF_FIELD_CLASS_TYPE_INT
) {
1065 /* Pointer: force integer's base to 16 */
1066 struct ctf_field_class_int
*int_fc
= ctf_field_class_as_int(nested_decl
);
1068 int_fc
->disp_base
= BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_HEXADECIMAL
;
1071 ret
= visit_field_class_specifier_list(ctx
, cls_specifier_list
, &nested_decl
);
1073 BT_ASSERT(!nested_decl
);
1079 BT_ASSERT(nested_decl
);
1081 if (!node_field_class_declarator
) {
1082 *field_decl
= nested_decl
;
1087 if (node_field_class_declarator
->u
.field_class_declarator
.type
== TYPEDEC_ID
) {
1088 if (node_field_class_declarator
->u
.field_class_declarator
.u
.id
) {
1089 const char *id
= node_field_class_declarator
->u
.field_class_declarator
.u
.id
;
1091 *field_name
= g_quark_from_string(id
);
1096 *field_decl
= nested_decl
;
1100 struct ctf_node
*first
;
1101 struct ctf_field_class
*decl
= NULL
;
1102 struct ctf_field_class
*outer_field_decl
= NULL
;
1103 struct bt_list_head
*length
=
1104 &node_field_class_declarator
->u
.field_class_declarator
.u
.nested
.length
;
1106 /* Create array/sequence, pass nested_decl as child */
1107 if (bt_list_empty(length
)) {
1108 _BT_CPPLOGE_APPEND_CAUSE_NODE(node_field_class_declarator
,
1109 "Expecting length field reference or value.");
1114 first
= _BT_LIST_FIRST_ENTRY(length
, struct ctf_node
, siblings
);
1115 if (first
->type
!= NODE_UNARY_EXPRESSION
) {
1116 _BT_CPPLOGE_APPEND_CAUSE_NODE(first
, "Unexpected node type: node-type={}", first
->type
);
1121 switch (first
->u
.unary_expression
.type
) {
1122 case UNARY_UNSIGNED_CONSTANT
:
1124 struct ctf_field_class_array
*array_decl
= NULL
;
1126 array_decl
= ctf_field_class_array_create();
1127 BT_ASSERT(array_decl
);
1128 array_decl
->length
= first
->u
.unary_expression
.u
.unsigned_constant
;
1129 array_decl
->base
.elem_fc
= nested_decl
;
1131 decl
= &array_decl
->base
.base
;
1136 /* Lookup unsigned integer definition, create seq. */
1137 struct ctf_field_class_sequence
*seq_decl
= NULL
;
1138 char *length_name
= ctf_ast_concatenate_unary_strings(length
);
1141 _BT_CPPLOGE_APPEND_CAUSE_NODE(node_field_class_declarator
,
1142 "Cannot concatenate unary strings.");
1147 if (strncmp(length_name
, "env.", 4) == 0) {
1148 /* This is, in fact, an array */
1149 const char *env_entry_name
= &length_name
[4];
1150 struct ctf_trace_class_env_entry
*env_entry
=
1151 ctf_trace_class_borrow_env_entry_by_name(ctx
->ctf_tc
, env_entry_name
);
1152 struct ctf_field_class_array
*array_decl
;
1155 _BT_CPPLOGE_APPEND_CAUSE_NODE(node_field_class_declarator
,
1156 "Cannot find environment entry: "
1163 if (env_entry
->type
!= CTF_TRACE_CLASS_ENV_ENTRY_TYPE_INT
) {
1164 _BT_CPPLOGE_APPEND_CAUSE_NODE(node_field_class_declarator
,
1165 "Wrong environment entry type "
1166 "(expecting integer): "
1173 if (env_entry
->value
.i
< 0) {
1174 _BT_CPPLOGE_APPEND_CAUSE_NODE(node_field_class_declarator
,
1175 "Invalid, negative array length: "
1176 "env-entry-name=\"{}\", "
1178 env_entry_name
, env_entry
->value
.i
);
1183 array_decl
= ctf_field_class_array_create();
1184 BT_ASSERT(array_decl
);
1185 array_decl
->length
= (uint64_t) env_entry
->value
.i
;
1186 array_decl
->base
.elem_fc
= nested_decl
;
1188 decl
= &array_decl
->base
.base
;
1190 seq_decl
= ctf_field_class_sequence_create();
1191 BT_ASSERT(seq_decl
);
1192 seq_decl
->base
.elem_fc
= nested_decl
;
1194 g_string_assign(seq_decl
->length_ref
, length_name
);
1195 decl
= &seq_decl
->base
.base
;
1198 g_free(length_name
);
1206 BT_ASSERT(!nested_decl
);
1208 BT_ASSERT(!*field_decl
);
1211 * At this point, we found the next nested declaration.
1212 * We currently own this (and lost the ownership of
1213 * nested_decl in the meantime). Pass this next
1214 * nested declaration as the content of the outer
1215 * container, MOVING its ownership.
1217 ret
= visit_field_class_declarator(
1218 ctx
, cls_specifier_list
, field_name
,
1219 node_field_class_declarator
->u
.field_class_declarator
.u
.nested
.field_class_declarator
,
1220 &outer_field_decl
, decl
);
1223 BT_ASSERT(!outer_field_decl
);
1228 BT_ASSERT(outer_field_decl
);
1229 *field_decl
= outer_field_decl
;
1230 outer_field_decl
= NULL
;
1233 BT_ASSERT(*field_decl
);
1237 ctf_field_class_destroy(*field_decl
);
1245 ctf_field_class_destroy(nested_decl
);
1250 static int visit_struct_decl_field(struct ctf_visitor_generate_ir
*ctx
,
1251 struct ctf_field_class_struct
*struct_decl
,
1252 struct ctf_node
*cls_specifier_list
,
1253 struct bt_list_head
*field_class_declarators
)
1256 struct ctf_node
*iter
;
1257 struct ctf_field_class
*field_decl
= NULL
;
1259 bt_list_for_each_entry (iter
, field_class_declarators
, siblings
) {
1262 const char *field_name
;
1264 ret
= visit_field_class_declarator(ctx
, cls_specifier_list
, &qfield_name
, iter
, &field_decl
,
1267 BT_ASSERT(!field_decl
);
1268 _BT_CPPLOGE_APPEND_CAUSE_NODE(cls_specifier_list
,
1269 "Cannot visit field class declarator: ret={}", ret
);
1273 BT_ASSERT(field_decl
);
1274 field_name
= g_quark_to_string(qfield_name
);
1276 /* Check if field with same name already exists */
1277 if (ctf_field_class_struct_borrow_member_by_name(struct_decl
, field_name
)) {
1278 _BT_CPPLOGE_APPEND_CAUSE_NODE(cls_specifier_list
,
1279 "Duplicate field in structure field class: "
1280 "field-name=\"{}\"",
1286 /* Add field to structure */
1287 ctf_field_class_struct_append_member(struct_decl
, field_name
, field_decl
);
1294 ctf_field_class_destroy(field_decl
);
1299 static int visit_variant_decl_field(struct ctf_visitor_generate_ir
*ctx
,
1300 struct ctf_field_class_variant
*variant_decl
,
1301 struct ctf_node
*cls_specifier_list
,
1302 struct bt_list_head
*field_class_declarators
)
1305 struct ctf_node
*iter
;
1306 struct ctf_field_class
*field_decl
= NULL
;
1308 bt_list_for_each_entry (iter
, field_class_declarators
, siblings
) {
1311 const char *field_name
;
1313 ret
= visit_field_class_declarator(ctx
, cls_specifier_list
, &qfield_name
, iter
, &field_decl
,
1316 BT_ASSERT(!field_decl
);
1317 _BT_CPPLOGE_APPEND_CAUSE_NODE(cls_specifier_list
,
1318 "Cannot visit field class declarator: ret={}", ret
);
1322 BT_ASSERT(field_decl
);
1323 field_name
= g_quark_to_string(qfield_name
);
1325 /* Check if field with same name already exists */
1326 if (ctf_field_class_variant_borrow_option_by_name(variant_decl
, field_name
)) {
1327 _BT_CPPLOGE_APPEND_CAUSE_NODE(cls_specifier_list
,
1328 "Duplicate field in variant field class: "
1329 "field-name=\"{}\"",
1335 /* Add field to structure */
1336 ctf_field_class_variant_append_option(variant_decl
, field_name
, field_decl
);
1343 ctf_field_class_destroy(field_decl
);
1348 static int visit_field_class_def(struct ctf_visitor_generate_ir
*ctx
,
1349 struct ctf_node
*cls_specifier_list
,
1350 struct bt_list_head
*field_class_declarators
)
1354 struct ctf_node
*iter
;
1355 struct ctf_field_class
*class_decl
= NULL
;
1357 bt_list_for_each_entry (iter
, field_class_declarators
, siblings
) {
1358 ret
= visit_field_class_declarator(ctx
, cls_specifier_list
, &qidentifier
, iter
, &class_decl
,
1361 _BT_CPPLOGE_APPEND_CAUSE_NODE(iter
, "Cannot visit field class declarator: ret={}", ret
);
1366 /* Do not allow field class def and alias of untagged variants */
1367 if (class_decl
->type
== CTF_FIELD_CLASS_TYPE_VARIANT
) {
1368 struct ctf_field_class_variant
*var_fc
= ctf_field_class_as_variant(class_decl
);
1370 if (var_fc
->tag_path
.path
->len
== 0) {
1371 _BT_CPPLOGE_APPEND_CAUSE_NODE(
1372 iter
, "Type definition of untagged variant field class is not allowed.");
1378 ret
= ctx_decl_scope_register_alias(ctx
->current_scope
, g_quark_to_string(qidentifier
),
1381 _BT_CPPLOGE_APPEND_CAUSE_NODE(iter
, "Cannot register field class alias: name=\"{}\"",
1382 g_quark_to_string(qidentifier
));
1388 ctf_field_class_destroy(class_decl
);
1393 static int visit_field_class_alias(struct ctf_visitor_generate_ir
*ctx
, struct ctf_node
*target
,
1394 struct ctf_node
*alias
)
1398 struct ctf_node
*node
;
1399 GQuark qdummy_field_name
;
1400 struct ctf_field_class
*class_decl
= NULL
;
1402 /* Create target field class */
1403 if (bt_list_empty(&target
->u
.field_class_alias_target
.field_class_declarators
)) {
1406 node
= _BT_LIST_FIRST_ENTRY(&target
->u
.field_class_alias_target
.field_class_declarators
,
1407 struct ctf_node
, siblings
);
1410 ret
= visit_field_class_declarator(
1411 ctx
, target
->u
.field_class_alias_target
.field_class_specifier_list
, &qdummy_field_name
,
1412 node
, &class_decl
, NULL
);
1414 BT_ASSERT(!class_decl
);
1415 _BT_CPPLOGE_APPEND_CAUSE_NODE(node
, "Cannot visit field class declarator: ret={}", ret
);
1419 /* Do not allow field class def and alias of untagged variants */
1420 if (class_decl
->type
== CTF_FIELD_CLASS_TYPE_VARIANT
) {
1421 struct ctf_field_class_variant
*var_fc
= ctf_field_class_as_variant(class_decl
);
1423 if (var_fc
->tag_path
.path
->len
== 0) {
1424 _BT_CPPLOGE_APPEND_CAUSE_NODE(
1425 target
, "Type definition of untagged variant field class is not allowed.");
1432 * The semantic validator does not check whether the target is
1433 * abstract or not (if it has an identifier). Check it here.
1435 if (qdummy_field_name
!= 0) {
1436 _BT_CPPLOGE_APPEND_CAUSE_NODE(target
, "Expecting empty identifier: id=\"{}\"",
1437 g_quark_to_string(qdummy_field_name
));
1442 /* Create alias identifier */
1443 node
= _BT_LIST_FIRST_ENTRY(&alias
->u
.field_class_alias_name
.field_class_declarators
,
1444 struct ctf_node
, siblings
);
1445 qalias
= create_class_alias_identifier(
1446 ctx
, alias
->u
.field_class_alias_name
.field_class_specifier_list
, node
);
1447 ret
= ctx_decl_scope_register_alias(ctx
->current_scope
, g_quark_to_string(qalias
), class_decl
);
1449 _BT_CPPLOGE_APPEND_CAUSE_NODE(node
, "Cannot register class alias: name=\"{}\"",
1450 g_quark_to_string(qalias
));
1455 ctf_field_class_destroy(class_decl
);
1460 static int visit_struct_decl_entry(struct ctf_visitor_generate_ir
*ctx
, struct ctf_node
*entry_node
,
1461 struct ctf_field_class_struct
*struct_decl
)
1465 switch (entry_node
->type
) {
1467 ret
= visit_field_class_def(ctx
, entry_node
->u
.field_class_def
.field_class_specifier_list
,
1468 &entry_node
->u
.field_class_def
.field_class_declarators
);
1470 _BT_CPPLOGE_APPEND_CAUSE_NODE(
1471 entry_node
, "Cannot add field class found in structure field class: ret={}", ret
);
1475 case NODE_TYPEALIAS
:
1476 ret
= visit_field_class_alias(ctx
, entry_node
->u
.field_class_alias
.target
,
1477 entry_node
->u
.field_class_alias
.alias
);
1479 _BT_CPPLOGE_APPEND_CAUSE_NODE(
1480 entry_node
, "Cannot add field class alias found in structure field class: ret={}",
1485 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
1487 ret
= visit_struct_decl_field(
1489 entry_node
->u
.struct_or_variant_declaration
.field_class_specifier_list
,
1490 &entry_node
->u
.struct_or_variant_declaration
.field_class_declarators
);
1496 _BT_CPPLOGE_APPEND_CAUSE_NODE(entry_node
, "Unexpected node type: node-type={}",
1506 static int visit_variant_decl_entry(struct ctf_visitor_generate_ir
*ctx
,
1507 struct ctf_node
*entry_node
,
1508 struct ctf_field_class_variant
*variant_decl
)
1512 switch (entry_node
->type
) {
1514 ret
= visit_field_class_def(ctx
, entry_node
->u
.field_class_def
.field_class_specifier_list
,
1515 &entry_node
->u
.field_class_def
.field_class_declarators
);
1517 _BT_CPPLOGE_APPEND_CAUSE_NODE(
1518 entry_node
, "Cannot add field class found in variant field class: ret={}", ret
);
1522 case NODE_TYPEALIAS
:
1523 ret
= visit_field_class_alias(ctx
, entry_node
->u
.field_class_alias
.target
,
1524 entry_node
->u
.field_class_alias
.alias
);
1526 _BT_CPPLOGE_APPEND_CAUSE_NODE(
1527 entry_node
, "Cannot add field class alias found in variant field class: ret={}",
1532 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
1534 ret
= visit_variant_decl_field(
1536 entry_node
->u
.struct_or_variant_declaration
.field_class_specifier_list
,
1537 &entry_node
->u
.struct_or_variant_declaration
.field_class_declarators
);
1543 _BT_CPPLOGE_APPEND_CAUSE_NODE(entry_node
, "Unexpected node type: node-type={}",
1553 static int visit_struct_decl(struct ctf_visitor_generate_ir
*ctx
, const char *name
,
1554 struct bt_list_head
*decl_list
, int has_body
,
1555 struct bt_list_head
*min_align
,
1556 struct ctf_field_class_struct
**struct_decl
)
1560 BT_ASSERT(struct_decl
);
1561 *struct_decl
= NULL
;
1563 /* For named struct (without body), lookup in declaration scope */
1566 BT_CPPLOGE_APPEND_CAUSE_SPEC(ctx
->logger
,
1567 "Bodyless structure field class: missing name.");
1572 *struct_decl
= ctx_decl_scope_lookup_struct(ctx
->current_scope
, name
, -1, true);
1573 if (!*struct_decl
) {
1574 BT_CPPLOGE_APPEND_CAUSE_SPEC(
1575 ctx
->logger
, "Cannot find structure field class: name=\"struct {}\"", name
);
1580 struct ctf_node
*entry_node
;
1581 uint64_t min_align_value
= 0;
1584 if (ctx_decl_scope_lookup_struct(ctx
->current_scope
, name
, 1, false)) {
1585 BT_CPPLOGE_APPEND_CAUSE_SPEC(
1587 "Structure field class already declared in local scope: "
1588 "name=\"struct {}\"",
1595 if (!bt_list_empty(min_align
)) {
1596 ret
= get_unary_unsigned(ctx
, min_align
, &min_align_value
);
1598 BT_CPPLOGE_APPEND_CAUSE_SPEC(
1600 "Unexpected unary expression for structure field class's `align` attribute: "
1607 *struct_decl
= ctf_field_class_struct_create();
1608 BT_ASSERT(*struct_decl
);
1610 if (min_align_value
!= 0) {
1611 (*struct_decl
)->base
.alignment
= min_align_value
;
1614 _TRY_PUSH_SCOPE_OR_GOTO_ERROR();
1616 bt_list_for_each_entry (entry_node
, decl_list
, siblings
) {
1617 ret
= visit_struct_decl_entry(ctx
, entry_node
, *struct_decl
);
1619 _BT_CPPLOGE_APPEND_CAUSE_NODE(entry_node
,
1620 "Cannot visit structure field class entry: "
1631 ret
= ctx_decl_scope_register_struct(ctx
->current_scope
, name
, *struct_decl
);
1633 BT_CPPLOGE_APPEND_CAUSE_SPEC(
1635 "Cannot register structure field class in declaration scope: "
1636 "name=\"struct {}\", ret={}",
1646 ctf_field_class_destroy(&(*struct_decl
)->base
);
1647 *struct_decl
= NULL
;
1651 static int visit_variant_decl(struct ctf_visitor_generate_ir
*ctx
, const char *name
,
1652 const char *tag
, struct bt_list_head
*decl_list
, int has_body
,
1653 struct ctf_field_class_variant
**variant_decl
)
1656 struct ctf_field_class_variant
*untagged_variant_decl
= NULL
;
1658 BT_ASSERT(variant_decl
);
1659 *variant_decl
= NULL
;
1661 /* For named variant (without body), lookup in declaration scope */
1664 BT_CPPLOGE_APPEND_CAUSE_SPEC(ctx
->logger
,
1665 "Bodyless variant field class: missing name.");
1670 untagged_variant_decl
= ctx_decl_scope_lookup_variant(ctx
->current_scope
, name
, -1, true);
1671 if (!untagged_variant_decl
) {
1672 BT_CPPLOGE_APPEND_CAUSE_SPEC(
1673 ctx
->logger
, "Cannot find variant field class: name=\"variant {}\"", name
);
1678 struct ctf_node
*entry_node
;
1681 if (ctx_decl_scope_lookup_variant(ctx
->current_scope
, name
, 1, false)) {
1682 BT_CPPLOGE_APPEND_CAUSE_SPEC(ctx
->logger
,
1683 "Variant field class already declared in local scope: "
1684 "name=\"variant {}\"",
1691 untagged_variant_decl
= ctf_field_class_variant_create();
1692 BT_ASSERT(untagged_variant_decl
);
1693 _TRY_PUSH_SCOPE_OR_GOTO_ERROR();
1695 bt_list_for_each_entry (entry_node
, decl_list
, siblings
) {
1696 ret
= visit_variant_decl_entry(ctx
, entry_node
, untagged_variant_decl
);
1698 _BT_CPPLOGE_APPEND_CAUSE_NODE(entry_node
,
1699 "Cannot visit variant field class entry: "
1710 ret
= ctx_decl_scope_register_variant(ctx
->current_scope
, name
, untagged_variant_decl
);
1712 BT_CPPLOGE_APPEND_CAUSE_SPEC(
1714 "Cannot register variant field class in declaration scope: "
1715 "name=\"variant {}\", ret={}",
1723 * If tagged, create tagged variant and return; otherwise
1724 * return untagged variant.
1727 *variant_decl
= untagged_variant_decl
;
1728 untagged_variant_decl
= NULL
;
1731 * At this point, we have a fresh untagged variant; nobody
1732 * else owns it. Set its tag now.
1734 g_string_assign(untagged_variant_decl
->tag_ref
, tag
);
1735 *variant_decl
= untagged_variant_decl
;
1736 untagged_variant_decl
= NULL
;
1739 BT_ASSERT(!untagged_variant_decl
);
1740 BT_ASSERT(*variant_decl
);
1744 ctf_field_class_destroy(&untagged_variant_decl
->base
);
1745 untagged_variant_decl
= NULL
;
1746 ctf_field_class_destroy(&(*variant_decl
)->base
);
1747 *variant_decl
= NULL
;
1761 static int visit_enum_decl_entry(struct ctf_visitor_generate_ir
*ctx
, struct ctf_node
*enumerator
,
1762 struct ctf_field_class_enum
*enum_decl
, struct uori
*last
)
1766 struct ctf_node
*iter
;
1767 struct uori start
= {
1781 const char *label
= enumerator
->u
.enumerator
.id
;
1782 struct bt_list_head
*values
= &enumerator
->u
.enumerator
.values
;
1784 bt_list_for_each_entry (iter
, values
, siblings
) {
1785 struct uori
*target
;
1787 if (iter
->type
!= NODE_UNARY_EXPRESSION
) {
1788 _BT_CPPLOGE_APPEND_CAUSE_NODE(iter
,
1789 "Wrong expression for enumeration field class label: "
1790 "node-type={}, label=\"{}\"",
1802 switch (iter
->u
.unary_expression
.type
) {
1803 case UNARY_SIGNED_CONSTANT
:
1804 target
->is_signed
= true;
1805 target
->value
.i
= iter
->u
.unary_expression
.u
.signed_constant
;
1807 case UNARY_UNSIGNED_CONSTANT
:
1808 target
->is_signed
= false;
1809 target
->value
.u
= iter
->u
.unary_expression
.u
.unsigned_constant
;
1812 _BT_CPPLOGE_APPEND_CAUSE_NODE(iter
,
1813 "Invalid enumeration field class entry: "
1814 "expecting constant signed or unsigned integer: "
1815 "node-type={}, label=\"{}\"",
1816 iter
->u
.unary_expression
.type
, label
);
1822 _BT_CPPLOGE_APPEND_CAUSE_NODE(
1823 iter
, "Invalid enumeration field class entry: label=\"{}\"", label
);
1839 if (end
.is_signed
) {
1840 last
->value
.i
= end
.value
.i
+ 1;
1842 last
->value
.u
= end
.value
.u
+ 1;
1845 ctf_field_class_enum_map_range(enum_decl
, label
, start
.value
.u
, end
.value
.u
);
1852 static int visit_enum_decl(struct ctf_visitor_generate_ir
*ctx
, const char *name
,
1853 struct ctf_node
*container_cls
, struct bt_list_head
*enumerator_list
,
1854 int has_body
, struct ctf_field_class_enum
**enum_decl
)
1858 struct ctf_field_class_int
*integer_decl
= NULL
;
1860 BT_ASSERT(enum_decl
);
1863 /* For named enum (without body), lookup in declaration scope */
1866 BT_CPPLOGE_APPEND_CAUSE_SPEC(ctx
->logger
,
1867 "Bodyless enumeration field class: missing name.");
1872 *enum_decl
= ctx_decl_scope_lookup_enum(ctx
->current_scope
, name
, -1, true);
1874 BT_CPPLOGE_APPEND_CAUSE_SPEC(ctx
->logger
,
1875 "Cannot find enumeration field class: "
1882 struct ctf_node
*iter
;
1883 struct uori last_value
= {
1892 if (ctx_decl_scope_lookup_enum(ctx
->current_scope
, name
, 1, false)) {
1893 BT_CPPLOGE_APPEND_CAUSE_SPEC(
1895 "Enumeration field class already declared in local scope: "
1903 if (!container_cls
) {
1904 integer_decl
= ctf_field_class_as_int(
1905 ctx_decl_scope_lookup_alias(ctx
->current_scope
, "int", -1, true));
1906 if (!integer_decl
) {
1907 BT_CPPLOGE_APPEND_CAUSE_SPEC(
1909 "Cannot find implicit `int` field class alias for enumeration field class.");
1914 ctf_field_class
*decl
;
1916 ret
= visit_field_class_declarator(ctx
, container_cls
, &qdummy_id
, NULL
, &decl
, NULL
);
1923 integer_decl
= ctf_field_class_as_int(decl
);
1926 BT_ASSERT(integer_decl
);
1928 if (integer_decl
->base
.base
.type
!= CTF_FIELD_CLASS_TYPE_INT
) {
1929 BT_CPPLOGE_APPEND_CAUSE_SPEC(
1931 "Container field class for enumeration field class is not an integer field class: "
1933 integer_decl
->base
.base
.type
);
1938 *enum_decl
= ctf_field_class_enum_create();
1939 BT_ASSERT(*enum_decl
);
1940 (*enum_decl
)->base
.base
.base
.alignment
= integer_decl
->base
.base
.alignment
;
1941 ctf_field_class_int_copy_content(&(*enum_decl
)->base
, integer_decl
);
1942 last_value
.is_signed
= (*enum_decl
)->base
.is_signed
;
1944 bt_list_for_each_entry (iter
, enumerator_list
, siblings
) {
1945 ret
= visit_enum_decl_entry(ctx
, iter
, *enum_decl
, &last_value
);
1947 _BT_CPPLOGE_APPEND_CAUSE_NODE(iter
,
1948 "Cannot visit enumeration field class entry: "
1956 ret
= ctx_decl_scope_register_enum(ctx
->current_scope
, name
, *enum_decl
);
1958 BT_CPPLOGE_APPEND_CAUSE_SPEC(
1960 "Cannot register enumeration field class in declaration scope: "
1971 ctf_field_class_destroy(&(*enum_decl
)->base
.base
.base
);
1975 ctf_field_class_destroy(&integer_decl
->base
.base
);
1976 integer_decl
= NULL
;
1980 static int visit_field_class_specifier(struct ctf_visitor_generate_ir
*ctx
,
1981 struct ctf_node
*cls_specifier_list
,
1982 struct ctf_field_class
**decl
)
1985 GString
*str
= NULL
;
1988 str
= g_string_new("");
1989 ret
= get_class_specifier_list_name(ctx
, cls_specifier_list
, str
);
1991 _BT_CPPLOGE_APPEND_CAUSE_NODE(cls_specifier_list
,
1992 "Cannot get field class specifier list's name: ret={}", ret
);
1996 *decl
= ctx_decl_scope_lookup_alias(ctx
->current_scope
, str
->str
, -1, true);
1998 _BT_CPPLOGE_APPEND_CAUSE_NODE(cls_specifier_list
,
1999 "Cannot find field class alias: name=\"{}\"", str
->str
);
2007 ctf_field_class_destroy(*decl
);
2012 g_string_free(str
, TRUE
);
2018 static int visit_integer_decl(struct ctf_visitor_generate_ir
*ctx
, struct bt_list_head
*expressions
,
2019 struct ctf_field_class_int
**integer_decl
)
2024 struct ctf_node
*expression
;
2025 uint64_t alignment
= 0, size
= 0;
2026 struct ctf_clock_class
*mapped_clock_class
= NULL
;
2027 enum ctf_encoding encoding
= CTF_ENCODING_NONE
;
2028 bt_field_class_integer_preferred_display_base base
=
2029 BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL
;
2030 enum ctf_byte_order byte_order
= ctx
->ctf_tc
->default_byte_order
;
2032 *integer_decl
= NULL
;
2034 bt_list_for_each_entry (expression
, expressions
, siblings
) {
2035 struct ctf_node
*left
, *right
;
2037 left
= _BT_LIST_FIRST_ENTRY(&expression
->u
.ctf_expression
.left
, struct ctf_node
, siblings
);
2039 _BT_LIST_FIRST_ENTRY(&expression
->u
.ctf_expression
.right
, struct ctf_node
, siblings
);
2041 if (left
->u
.unary_expression
.type
!= UNARY_STRING
) {
2042 _BT_CPPLOGE_APPEND_CAUSE_NODE(left
, "Unexpected unary expression type: type={}",
2043 left
->u
.unary_expression
.type
);
2048 if (strcmp(left
->u
.unary_expression
.u
.string
, "signed") == 0) {
2049 if (_IS_SET(&set
, _INTEGER_SIGNED_SET
)) {
2050 _BT_CPPLOGE_APPEND_CAUSE_DUP_ATTR(left
, "signed", "integer field class");
2055 signedness
= get_boolean(ctx
, right
);
2056 if (signedness
< 0) {
2057 _BT_CPPLOGE_APPEND_CAUSE_NODE(
2059 "Invalid boolean value for integer field class's `signed` attribute: "
2066 _SET(&set
, _INTEGER_SIGNED_SET
);
2067 } else if (strcmp(left
->u
.unary_expression
.u
.string
, "byte_order") == 0) {
2068 if (_IS_SET(&set
, _INTEGER_BYTE_ORDER_SET
)) {
2069 _BT_CPPLOGE_APPEND_CAUSE_DUP_ATTR(left
, "byte_order", "integer field class");
2074 byte_order
= get_real_byte_order(ctx
, right
);
2075 if (byte_order
== CTF_BYTE_ORDER_UNKNOWN
) {
2076 _BT_CPPLOGE_APPEND_CAUSE_NODE(
2078 "Invalid `byte_order` attribute in integer field class: "
2085 _SET(&set
, _INTEGER_BYTE_ORDER_SET
);
2086 } else if (strcmp(left
->u
.unary_expression
.u
.string
, "size") == 0) {
2087 if (_IS_SET(&set
, _INTEGER_SIZE_SET
)) {
2088 _BT_CPPLOGE_APPEND_CAUSE_DUP_ATTR(left
, "size", "integer field class");
2093 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
2094 _BT_CPPLOGE_APPEND_CAUSE_NODE(right
,
2095 "Invalid `size` attribute in integer field class: "
2096 "expecting unsigned constant integer: "
2098 right
->u
.unary_expression
.type
);
2103 size
= right
->u
.unary_expression
.u
.unsigned_constant
;
2105 _BT_CPPLOGE_APPEND_CAUSE_NODE(right
,
2106 "Invalid `size` attribute in integer field class: "
2107 "expecting positive constant integer: "
2112 } else if (size
> 64) {
2113 _BT_CPPLOGE_APPEND_CAUSE_NODE(
2115 "Invalid `size` attribute in integer field class: "
2116 "integer fields over 64 bits are not supported as of this version: "
2123 _SET(&set
, _INTEGER_SIZE_SET
);
2124 } else if (strcmp(left
->u
.unary_expression
.u
.string
, "align") == 0) {
2125 if (_IS_SET(&set
, _INTEGER_ALIGN_SET
)) {
2126 _BT_CPPLOGE_APPEND_CAUSE_DUP_ATTR(left
, "align", "integer field class");
2131 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
2132 _BT_CPPLOGE_APPEND_CAUSE_NODE(right
,
2133 "Invalid `align` attribute in integer field class: "
2134 "expecting unsigned constant integer: "
2136 right
->u
.unary_expression
.type
);
2141 alignment
= right
->u
.unary_expression
.u
.unsigned_constant
;
2142 if (!is_align_valid(alignment
)) {
2143 _BT_CPPLOGE_APPEND_CAUSE_NODE(right
,
2144 "Invalid `align` attribute in integer field class: "
2145 "expecting power of two: "
2152 _SET(&set
, _INTEGER_ALIGN_SET
);
2153 } else if (strcmp(left
->u
.unary_expression
.u
.string
, "base") == 0) {
2154 if (_IS_SET(&set
, _INTEGER_BASE_SET
)) {
2155 _BT_CPPLOGE_APPEND_CAUSE_DUP_ATTR(left
, "base", "integer field class");
2160 switch (right
->u
.unary_expression
.type
) {
2161 case UNARY_UNSIGNED_CONSTANT
:
2163 uint64_t constant
= right
->u
.unary_expression
.u
.unsigned_constant
;
2167 base
= BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_BINARY
;
2170 base
= BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_OCTAL
;
2173 base
= BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL
;
2176 base
= BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_HEXADECIMAL
;
2179 _BT_CPPLOGE_APPEND_CAUSE_NODE(
2181 "Invalid `base` attribute in integer field class: "
2183 right
->u
.unary_expression
.u
.unsigned_constant
);
2192 ctf_ast_concatenate_unary_strings(&expression
->u
.ctf_expression
.right
);
2194 _BT_CPPLOGE_APPEND_CAUSE_NODE(
2196 "Unexpected unary expression for integer field class's `base` attribute.");
2201 if (strcmp(s_right
, "decimal") == 0 || strcmp(s_right
, "dec") == 0 ||
2202 strcmp(s_right
, "d") == 0 || strcmp(s_right
, "i") == 0 ||
2203 strcmp(s_right
, "u") == 0) {
2204 base
= BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL
;
2205 } else if (strcmp(s_right
, "hexadecimal") == 0 || strcmp(s_right
, "hex") == 0 ||
2206 strcmp(s_right
, "x") == 0 || strcmp(s_right
, "X") == 0 ||
2207 strcmp(s_right
, "p") == 0) {
2208 base
= BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_HEXADECIMAL
;
2209 } else if (strcmp(s_right
, "octal") == 0 || strcmp(s_right
, "oct") == 0 ||
2210 strcmp(s_right
, "o") == 0) {
2211 base
= BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_OCTAL
;
2212 } else if (strcmp(s_right
, "binary") == 0 || strcmp(s_right
, "b") == 0) {
2213 base
= BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_BINARY
;
2215 _BT_CPPLOGE_APPEND_CAUSE_NODE(
2217 "Unexpected unary expression for integer field class's `base` attribute: "
2229 _BT_CPPLOGE_APPEND_CAUSE_NODE(
2230 right
, "Invalid `base` attribute in integer field class: "
2231 "expecting unsigned constant integer or unary string.");
2236 _SET(&set
, _INTEGER_BASE_SET
);
2237 } else if (strcmp(left
->u
.unary_expression
.u
.string
, "encoding") == 0) {
2240 if (_IS_SET(&set
, _INTEGER_ENCODING_SET
)) {
2241 _BT_CPPLOGE_APPEND_CAUSE_DUP_ATTR(left
, "encoding", "integer field class");
2246 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
2247 _BT_CPPLOGE_APPEND_CAUSE_NODE(
2248 right
, "Invalid `encoding` attribute in integer field class: "
2249 "expecting unary string.");
2254 s_right
= ctf_ast_concatenate_unary_strings(&expression
->u
.ctf_expression
.right
);
2256 _BT_CPPLOGE_APPEND_CAUSE_NODE(
2258 "Unexpected unary expression for integer field class's `encoding` attribute.");
2263 if (strcmp(s_right
, "UTF8") == 0 || strcmp(s_right
, "utf8") == 0 ||
2264 strcmp(s_right
, "utf-8") == 0 || strcmp(s_right
, "UTF-8") == 0 ||
2265 strcmp(s_right
, "ASCII") == 0 || strcmp(s_right
, "ascii") == 0) {
2266 encoding
= CTF_ENCODING_UTF8
;
2267 } else if (strcmp(s_right
, "none") == 0) {
2268 encoding
= CTF_ENCODING_NONE
;
2270 _BT_CPPLOGE_APPEND_CAUSE_NODE(
2272 "Invalid `encoding` attribute in integer field class: "
2273 "unknown encoding: encoding=\"{}\"",
2281 _SET(&set
, _INTEGER_ENCODING_SET
);
2282 } else if (strcmp(left
->u
.unary_expression
.u
.string
, "map") == 0) {
2283 const char *clock_name
;
2285 if (_IS_SET(&set
, _INTEGER_MAP_SET
)) {
2286 _BT_CPPLOGE_APPEND_CAUSE_DUP_ATTR(left
, "map", "integer field class");
2291 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
2292 _BT_CPPLOGE_APPEND_CAUSE_NODE(right
,
2293 "Invalid `map` attribute in integer field class: "
2294 "expecting unary string.");
2299 clock_name
= get_map_clock_name_value(&expression
->u
.ctf_expression
.right
);
2302 ctf_ast_concatenate_unary_strings(&expression
->u
.ctf_expression
.right
);
2305 _BT_CPPLOGE_APPEND_CAUSE_NODE(
2307 "Unexpected unary expression for integer field class's `map` attribute.");
2312 _BT_CPPLOGE_NODE(right
,
2313 "Invalid `map` attribute in integer field class: "
2314 "cannot find clock class at this point: name=\"{}\"",
2316 _SET(&set
, _INTEGER_MAP_SET
);
2321 mapped_clock_class
=
2322 ctf_trace_class_borrow_clock_class_by_name(ctx
->ctf_tc
, clock_name
);
2323 if (!mapped_clock_class
) {
2324 _BT_CPPLOGE_APPEND_CAUSE_NODE(right
,
2325 "Invalid `map` attribute in integer field class: "
2326 "cannot find clock class at this point: name=\"{}\"",
2332 _SET(&set
, _INTEGER_MAP_SET
);
2334 _BT_CPPLOGW_NODE(left
,
2335 "Unknown attribute in integer field class: "
2337 left
->u
.unary_expression
.u
.string
);
2341 if (!_IS_SET(&set
, _INTEGER_SIZE_SET
)) {
2342 BT_CPPLOGE_APPEND_CAUSE_SPEC(ctx
->logger
,
2343 "Missing `size` attribute in integer field class.");
2348 if (!_IS_SET(&set
, _INTEGER_ALIGN_SET
)) {
2349 if (size
% CHAR_BIT
) {
2350 /* Bit-packed alignment */
2353 /* Byte-packed alignment */
2354 alignment
= CHAR_BIT
;
2358 *integer_decl
= ctf_field_class_int_create();
2359 BT_ASSERT(*integer_decl
);
2360 (*integer_decl
)->base
.base
.alignment
= alignment
;
2361 (*integer_decl
)->base
.byte_order
= byte_order
;
2362 (*integer_decl
)->base
.size
= size
;
2363 (*integer_decl
)->is_signed
= (signedness
> 0);
2364 (*integer_decl
)->disp_base
= base
;
2365 (*integer_decl
)->encoding
= encoding
;
2366 (*integer_decl
)->mapped_clock_class
= mapped_clock_class
;
2370 ctf_field_class_destroy(&(*integer_decl
)->base
.base
);
2371 *integer_decl
= NULL
;
2375 static int visit_floating_point_number_decl(struct ctf_visitor_generate_ir
*ctx
,
2376 struct bt_list_head
*expressions
,
2377 struct ctf_field_class_float
**float_decl
)
2381 struct ctf_node
*expression
;
2382 uint64_t alignment
= 1, exp_dig
= 0, mant_dig
= 0;
2383 enum ctf_byte_order byte_order
= ctx
->ctf_tc
->default_byte_order
;
2387 bt_list_for_each_entry (expression
, expressions
, siblings
) {
2388 struct ctf_node
*left
, *right
;
2390 left
= _BT_LIST_FIRST_ENTRY(&expression
->u
.ctf_expression
.left
, struct ctf_node
, siblings
);
2392 _BT_LIST_FIRST_ENTRY(&expression
->u
.ctf_expression
.right
, struct ctf_node
, siblings
);
2394 if (left
->u
.unary_expression
.type
!= UNARY_STRING
) {
2395 _BT_CPPLOGE_APPEND_CAUSE_NODE(left
, "Unexpected unary expression type: type={}",
2396 left
->u
.unary_expression
.type
);
2401 if (strcmp(left
->u
.unary_expression
.u
.string
, "byte_order") == 0) {
2402 if (_IS_SET(&set
, _FLOAT_BYTE_ORDER_SET
)) {
2403 _BT_CPPLOGE_APPEND_CAUSE_DUP_ATTR(left
, "byte_order",
2404 "floating point number field class");
2409 byte_order
= get_real_byte_order(ctx
, right
);
2410 if (byte_order
== CTF_BYTE_ORDER_UNKNOWN
) {
2411 _BT_CPPLOGE_APPEND_CAUSE_NODE(
2413 "Invalid `byte_order` attribute in floating point number field class: "
2420 _SET(&set
, _FLOAT_BYTE_ORDER_SET
);
2421 } else if (strcmp(left
->u
.unary_expression
.u
.string
, "exp_dig") == 0) {
2422 if (_IS_SET(&set
, _FLOAT_EXP_DIG_SET
)) {
2423 _BT_CPPLOGE_APPEND_CAUSE_DUP_ATTR(left
, "exp_dig",
2424 "floating point number field class");
2429 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
2430 _BT_CPPLOGE_APPEND_CAUSE_NODE(
2432 "Invalid `exp_dig` attribute in floating point number field class: "
2433 "expecting unsigned constant integer: "
2435 right
->u
.unary_expression
.type
);
2440 exp_dig
= right
->u
.unary_expression
.u
.unsigned_constant
;
2441 _SET(&set
, _FLOAT_EXP_DIG_SET
);
2442 } else if (strcmp(left
->u
.unary_expression
.u
.string
, "mant_dig") == 0) {
2443 if (_IS_SET(&set
, _FLOAT_MANT_DIG_SET
)) {
2444 _BT_CPPLOGE_APPEND_CAUSE_DUP_ATTR(left
, "mant_dig",
2445 "floating point number field class");
2450 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
2451 _BT_CPPLOGE_APPEND_CAUSE_NODE(
2453 "Invalid `mant_dig` attribute in floating point number field class: "
2454 "expecting unsigned constant integer: "
2456 right
->u
.unary_expression
.type
);
2461 mant_dig
= right
->u
.unary_expression
.u
.unsigned_constant
;
2462 _SET(&set
, _FLOAT_MANT_DIG_SET
);
2463 } else if (strcmp(left
->u
.unary_expression
.u
.string
, "align") == 0) {
2464 if (_IS_SET(&set
, _FLOAT_ALIGN_SET
)) {
2465 _BT_CPPLOGE_APPEND_CAUSE_DUP_ATTR(left
, "align",
2466 "floating point number field class");
2471 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
2472 _BT_CPPLOGE_APPEND_CAUSE_NODE(
2474 "Invalid `align` attribute in floating point number field class: "
2475 "expecting unsigned constant integer: "
2477 right
->u
.unary_expression
.type
);
2482 alignment
= right
->u
.unary_expression
.u
.unsigned_constant
;
2484 if (!is_align_valid(alignment
)) {
2485 _BT_CPPLOGE_APPEND_CAUSE_NODE(
2487 "Invalid `align` attribute in floating point number field class: "
2488 "expecting power of two: "
2495 _SET(&set
, _FLOAT_ALIGN_SET
);
2497 _BT_CPPLOGW_NODE(left
,
2498 "Unknown attribute in floating point number field class: "
2500 left
->u
.unary_expression
.u
.string
);
2504 if (!_IS_SET(&set
, _FLOAT_MANT_DIG_SET
)) {
2505 BT_CPPLOGE_APPEND_CAUSE_SPEC(
2506 ctx
->logger
, "Missing `mant_dig` attribute in floating point number field class.");
2511 if (!_IS_SET(&set
, _FLOAT_EXP_DIG_SET
)) {
2512 BT_CPPLOGE_APPEND_CAUSE_SPEC(
2513 ctx
->logger
, "Missing `exp_dig` attribute in floating point number field class.");
2518 if (mant_dig
!= 24 && mant_dig
!= 53) {
2519 BT_CPPLOGE_APPEND_CAUSE_SPEC(ctx
->logger
, "`mant_dig` attribute: expecting 24 or 53.");
2524 if (mant_dig
== 24 && exp_dig
!= 8) {
2525 BT_CPPLOGE_APPEND_CAUSE_SPEC(ctx
->logger
,
2526 "`exp_dig` attribute: expecting 8 because `mant_dig` is 24.");
2531 if (mant_dig
== 53 && exp_dig
!= 11) {
2532 BT_CPPLOGE_APPEND_CAUSE_SPEC(ctx
->logger
,
2533 "`exp_dig` attribute: expecting 11 because `mant_dig` is 53.");
2538 if (!_IS_SET(&set
, _INTEGER_ALIGN_SET
)) {
2539 if ((mant_dig
+ exp_dig
) % CHAR_BIT
) {
2540 /* Bit-packed alignment */
2543 /* Byte-packed alignment */
2544 alignment
= CHAR_BIT
;
2548 *float_decl
= ctf_field_class_float_create();
2549 BT_ASSERT(*float_decl
);
2550 (*float_decl
)->base
.base
.alignment
= alignment
;
2551 (*float_decl
)->base
.byte_order
= byte_order
;
2552 (*float_decl
)->base
.size
= mant_dig
+ exp_dig
;
2556 ctf_field_class_destroy(&(*float_decl
)->base
.base
);
2561 static int visit_string_decl(struct ctf_visitor_generate_ir
*ctx
, struct bt_list_head
*expressions
,
2562 struct ctf_field_class_string
**string_decl
)
2566 struct ctf_node
*expression
;
2567 enum ctf_encoding encoding
= CTF_ENCODING_UTF8
;
2569 *string_decl
= NULL
;
2571 bt_list_for_each_entry (expression
, expressions
, siblings
) {
2572 struct ctf_node
*left
, *right
;
2574 left
= _BT_LIST_FIRST_ENTRY(&expression
->u
.ctf_expression
.left
, struct ctf_node
, siblings
);
2576 _BT_LIST_FIRST_ENTRY(&expression
->u
.ctf_expression
.right
, struct ctf_node
, siblings
);
2578 if (left
->u
.unary_expression
.type
!= UNARY_STRING
) {
2579 _BT_CPPLOGE_APPEND_CAUSE_NODE(left
, "Unexpected unary expression type: type={}",
2580 left
->u
.unary_expression
.type
);
2585 if (strcmp(left
->u
.unary_expression
.u
.string
, "encoding") == 0) {
2588 if (_IS_SET(&set
, _STRING_ENCODING_SET
)) {
2589 _BT_CPPLOGE_APPEND_CAUSE_DUP_ATTR(left
, "encoding", "string field class");
2594 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
2595 _BT_CPPLOGE_APPEND_CAUSE_NODE(right
,
2596 "Invalid `encoding` attribute in string field class: "
2597 "expecting unary string.");
2602 s_right
= ctf_ast_concatenate_unary_strings(&expression
->u
.ctf_expression
.right
);
2604 _BT_CPPLOGE_APPEND_CAUSE_NODE(
2606 "Unexpected unary expression for string field class's `encoding` attribute.");
2611 if (strcmp(s_right
, "UTF8") == 0 || strcmp(s_right
, "utf8") == 0 ||
2612 strcmp(s_right
, "utf-8") == 0 || strcmp(s_right
, "UTF-8") == 0 ||
2613 strcmp(s_right
, "ASCII") == 0 || strcmp(s_right
, "ascii") == 0) {
2614 encoding
= CTF_ENCODING_UTF8
;
2615 } else if (strcmp(s_right
, "none") == 0) {
2616 encoding
= CTF_ENCODING_NONE
;
2618 _BT_CPPLOGE_APPEND_CAUSE_NODE(right
,
2619 "Invalid `encoding` attribute in string field class: "
2620 "unknown encoding: encoding=\"{}\"",
2628 _SET(&set
, _STRING_ENCODING_SET
);
2630 _BT_CPPLOGW_NODE(left
,
2631 "Unknown attribute in string field class: "
2633 left
->u
.unary_expression
.u
.string
);
2637 *string_decl
= ctf_field_class_string_create();
2638 BT_ASSERT(*string_decl
);
2639 (*string_decl
)->encoding
= encoding
;
2643 ctf_field_class_destroy(&(*string_decl
)->base
);
2644 *string_decl
= NULL
;
2648 static int visit_field_class_specifier_list(struct ctf_visitor_generate_ir
*ctx
,
2649 struct ctf_node
*ts_list
, struct ctf_field_class
**decl
)
2652 struct ctf_node
*first
, *node
;
2656 if (ts_list
->type
!= NODE_TYPE_SPECIFIER_LIST
) {
2657 _BT_CPPLOGE_APPEND_CAUSE_NODE(ts_list
, "Unexpected node type: node-type={}", ts_list
->type
);
2662 first
= _BT_LIST_FIRST_ENTRY(&ts_list
->u
.field_class_specifier_list
.head
, struct ctf_node
,
2664 if (first
->type
!= NODE_TYPE_SPECIFIER
) {
2665 _BT_CPPLOGE_APPEND_CAUSE_NODE(first
, "Unexpected node type: node-type={}", first
->type
);
2670 node
= first
->u
.field_class_specifier
.node
;
2672 switch (first
->u
.field_class_specifier
.type
) {
2673 case TYPESPEC_INTEGER
:
2675 ctf_field_class_int
*int_decl
;
2677 ret
= visit_integer_decl(ctx
, &node
->u
.integer
.expressions
, &int_decl
);
2679 BT_ASSERT(!int_decl
);
2683 *decl
= &int_decl
->base
.base
;
2686 case TYPESPEC_FLOATING_POINT
:
2688 ctf_field_class_float
*float_decl
;
2691 visit_floating_point_number_decl(ctx
, &node
->u
.floating_point
.expressions
, &float_decl
);
2693 BT_ASSERT(!float_decl
);
2697 *decl
= &float_decl
->base
.base
;
2700 case TYPESPEC_STRING
:
2702 ctf_field_class_string
*string_decl
;
2704 ret
= visit_string_decl(ctx
, &node
->u
.string
.expressions
, &string_decl
);
2706 BT_ASSERT(!string_decl
);
2710 *decl
= &string_decl
->base
;
2713 case TYPESPEC_STRUCT
:
2715 ctf_field_class_struct
*struct_decl
;
2717 ret
= visit_struct_decl(ctx
, node
->u
._struct
.name
, &node
->u
._struct
.declaration_list
,
2718 node
->u
._struct
.has_body
, &node
->u
._struct
.min_align
, &struct_decl
);
2720 BT_ASSERT(!struct_decl
);
2724 *decl
= &struct_decl
->base
;
2727 case TYPESPEC_VARIANT
:
2729 ctf_field_class_variant
*variant_decl
;
2731 ret
= visit_variant_decl(ctx
, node
->u
.variant
.name
, node
->u
.variant
.choice
,
2732 &node
->u
.variant
.declaration_list
, node
->u
.variant
.has_body
,
2735 BT_ASSERT(!variant_decl
);
2739 *decl
= &variant_decl
->base
;
2744 ctf_field_class_enum
*enum_decl
;
2746 ret
= visit_enum_decl(ctx
, node
->u
._enum
.enum_id
, node
->u
._enum
.container_field_class
,
2747 &node
->u
._enum
.enumerator_list
, node
->u
._enum
.has_body
, &enum_decl
);
2749 BT_ASSERT(!enum_decl
);
2753 *decl
= &enum_decl
->base
.base
.base
;
2758 case TYPESPEC_SHORT
:
2761 case TYPESPEC_FLOAT
:
2762 case TYPESPEC_DOUBLE
:
2763 case TYPESPEC_SIGNED
:
2764 case TYPESPEC_UNSIGNED
:
2766 case TYPESPEC_COMPLEX
:
2767 case TYPESPEC_IMAGINARY
:
2768 case TYPESPEC_CONST
:
2769 case TYPESPEC_ID_TYPE
:
2770 ret
= visit_field_class_specifier(ctx
, ts_list
, decl
);
2772 _BT_CPPLOGE_APPEND_CAUSE_NODE(first
, "Cannot visit field class specifier: ret={}", ret
);
2778 _BT_CPPLOGE_APPEND_CAUSE_NODE(first
, "Unexpected field class specifier type: node-type={}",
2779 first
->u
.field_class_specifier
.type
);
2788 ctf_field_class_destroy(*decl
);
2793 static int visit_event_decl_entry(struct ctf_visitor_generate_ir
*ctx
, struct ctf_node
*node
,
2794 struct ctf_event_class
*event_class
, uint64_t *stream_id
,
2800 switch (node
->type
) {
2802 ret
= visit_field_class_def(ctx
, node
->u
.field_class_def
.field_class_specifier_list
,
2803 &node
->u
.field_class_def
.field_class_declarators
);
2805 _BT_CPPLOGE_APPEND_CAUSE_NODE(node
, "Cannot add field class found in event class.");
2809 case NODE_TYPEALIAS
:
2810 ret
= visit_field_class_alias(ctx
, node
->u
.field_class_alias
.target
,
2811 node
->u
.field_class_alias
.alias
);
2813 _BT_CPPLOGE_APPEND_CAUSE_NODE(node
,
2814 "Cannot add field class alias found in event class.");
2818 case NODE_CTF_EXPRESSION
:
2820 left
= ctf_ast_concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
2822 _BT_CPPLOGE_APPEND_CAUSE_NODE(node
, "Cannot concatenate unary strings.");
2827 if (strcmp(left
, "name") == 0) {
2828 /* This is already known at this stage */
2829 if (_IS_SET(set
, _EVENT_NAME_SET
)) {
2830 _BT_CPPLOGE_APPEND_CAUSE_DUP_ATTR(node
, "name", "event class");
2835 _SET(set
, _EVENT_NAME_SET
);
2836 } else if (strcmp(left
, "id") == 0) {
2839 if (_IS_SET(set
, _EVENT_ID_SET
)) {
2840 _BT_CPPLOGE_APPEND_CAUSE_DUP_ATTR(node
, "id", "event class");
2845 ret
= get_unary_unsigned(ctx
, &node
->u
.ctf_expression
.right
, (uint64_t *) &id
);
2846 /* Only read "id" if get_unary_unsigned() succeeded. */
2847 if (ret
|| (!ret
&& id
< 0)) {
2848 _BT_CPPLOGE_APPEND_CAUSE_NODE(
2849 node
, "Unexpected unary expression for event class's `id` attribute.");
2854 event_class
->id
= id
;
2855 _SET(set
, _EVENT_ID_SET
);
2856 } else if (strcmp(left
, "stream_id") == 0) {
2857 if (_IS_SET(set
, _EVENT_STREAM_ID_SET
)) {
2858 _BT_CPPLOGE_APPEND_CAUSE_DUP_ATTR(node
, "stream_id", "event class");
2863 ret
= get_unary_unsigned(ctx
, &node
->u
.ctf_expression
.right
, stream_id
);
2866 * Only read "stream_id" if get_unary_unsigned()
2870 _BT_CPPLOGE_APPEND_CAUSE_NODE(
2871 node
, "Unexpected unary expression for event class's `stream_id` attribute.");
2876 _SET(set
, _EVENT_STREAM_ID_SET
);
2877 } else if (strcmp(left
, "context") == 0) {
2878 if (_IS_SET(set
, _EVENT_CONTEXT_SET
)) {
2879 _BT_CPPLOGE_APPEND_CAUSE_NODE(node
, "Duplicate `context` entry in event class.");
2884 ret
= visit_field_class_specifier_list(
2885 ctx
, _BT_LIST_FIRST_ENTRY(&node
->u
.ctf_expression
.right
, struct ctf_node
, siblings
),
2886 &event_class
->spec_context_fc
);
2888 _BT_CPPLOGE_APPEND_CAUSE_NODE(node
,
2889 "Cannot create event class's context field class.");
2893 BT_ASSERT(event_class
->spec_context_fc
);
2894 _SET(set
, _EVENT_CONTEXT_SET
);
2895 } else if (strcmp(left
, "fields") == 0) {
2896 if (_IS_SET(set
, _EVENT_FIELDS_SET
)) {
2897 _BT_CPPLOGE_APPEND_CAUSE_NODE(node
, "Duplicate `fields` entry in event class.");
2902 ret
= visit_field_class_specifier_list(
2903 ctx
, _BT_LIST_FIRST_ENTRY(&node
->u
.ctf_expression
.right
, struct ctf_node
, siblings
),
2904 &event_class
->payload_fc
);
2906 _BT_CPPLOGE_APPEND_CAUSE_NODE(node
,
2907 "Cannot create event class's payload field class.");
2911 BT_ASSERT(event_class
->payload_fc
);
2912 _SET(set
, _EVENT_FIELDS_SET
);
2913 } else if (strcmp(left
, "loglevel") == 0) {
2914 uint64_t loglevel_value
;
2915 bool is_log_level_known
= true;
2916 bt_event_class_log_level log_level
;
2918 if (_IS_SET(set
, _EVENT_LOG_LEVEL_SET
)) {
2919 _BT_CPPLOGE_APPEND_CAUSE_DUP_ATTR(node
, "loglevel", "event class");
2924 ret
= get_unary_unsigned(ctx
, &node
->u
.ctf_expression
.right
, &loglevel_value
);
2926 _BT_CPPLOGE_APPEND_CAUSE_NODE(
2927 node
, "Unexpected unary expression for event class's `loglevel` attribute.");
2932 switch (loglevel_value
) {
2934 log_level
= BT_EVENT_CLASS_LOG_LEVEL_EMERGENCY
;
2937 log_level
= BT_EVENT_CLASS_LOG_LEVEL_ALERT
;
2940 log_level
= BT_EVENT_CLASS_LOG_LEVEL_CRITICAL
;
2943 log_level
= BT_EVENT_CLASS_LOG_LEVEL_ERROR
;
2946 log_level
= BT_EVENT_CLASS_LOG_LEVEL_WARNING
;
2949 log_level
= BT_EVENT_CLASS_LOG_LEVEL_NOTICE
;
2952 log_level
= BT_EVENT_CLASS_LOG_LEVEL_INFO
;
2955 log_level
= BT_EVENT_CLASS_LOG_LEVEL_DEBUG_SYSTEM
;
2958 log_level
= BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROGRAM
;
2961 log_level
= BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROCESS
;
2964 log_level
= BT_EVENT_CLASS_LOG_LEVEL_DEBUG_MODULE
;
2967 log_level
= BT_EVENT_CLASS_LOG_LEVEL_DEBUG_UNIT
;
2970 log_level
= BT_EVENT_CLASS_LOG_LEVEL_DEBUG_FUNCTION
;
2973 log_level
= BT_EVENT_CLASS_LOG_LEVEL_DEBUG_LINE
;
2976 log_level
= BT_EVENT_CLASS_LOG_LEVEL_DEBUG
;
2979 is_log_level_known
= false;
2982 "Not setting event class's log level because its value is unknown: "
2987 if (is_log_level_known
) {
2988 ctf_event_class_set_log_level(event_class
, log_level
);
2991 _SET(set
, _EVENT_LOG_LEVEL_SET
);
2992 } else if (strcmp(left
, "model.emf.uri") == 0) {
2995 if (_IS_SET(set
, _EVENT_MODEL_EMF_URI_SET
)) {
2996 _BT_CPPLOGE_APPEND_CAUSE_DUP_ATTR(node
, "model.emf.uri", "event class");
3001 right
= ctf_ast_concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
3003 _BT_CPPLOGE_APPEND_CAUSE_NODE(
3005 "Unexpected unary expression for event class's `model.emf.uri` attribute.");
3010 if (strlen(right
) == 0) {
3011 _BT_CPPLOGW_NODE(node
, "Not setting event class's EMF URI because it's empty.");
3013 g_string_assign(event_class
->emf_uri
, right
);
3017 _SET(set
, _EVENT_MODEL_EMF_URI_SET
);
3019 _BT_CPPLOGW_NODE(node
,
3020 "Unknown attribute in event class: "
3043 static char *get_event_decl_name(struct ctf_visitor_generate_ir
*ctx
, struct ctf_node
*node
)
3047 struct ctf_node
*iter
;
3048 struct bt_list_head
*decl_list
= &node
->u
.event
.declaration_list
;
3050 bt_list_for_each_entry (iter
, decl_list
, siblings
) {
3051 if (iter
->type
!= NODE_CTF_EXPRESSION
) {
3055 left
= ctf_ast_concatenate_unary_strings(&iter
->u
.ctf_expression
.left
);
3057 _BT_CPPLOGE_APPEND_CAUSE_NODE(iter
, "Cannot concatenate unary strings.");
3061 if (strcmp(left
, "name") == 0) {
3062 name
= ctf_ast_concatenate_unary_strings(&iter
->u
.ctf_expression
.right
);
3064 _BT_CPPLOGE_APPEND_CAUSE_NODE(
3065 iter
, "Unexpected unary expression for event class's `name` attribute.");
3085 static int visit_event_decl(struct ctf_visitor_generate_ir
*ctx
, struct ctf_node
*node
)
3089 struct ctf_node
*iter
;
3090 uint64_t stream_id
= 0;
3091 char *event_name
= NULL
;
3092 struct ctf_event_class
*event_class
= NULL
;
3093 struct ctf_stream_class
*stream_class
= NULL
;
3094 struct bt_list_head
*decl_list
= &node
->u
.event
.declaration_list
;
3095 bool pop_scope
= false;
3097 if (node
->visited
) {
3101 node
->visited
= TRUE
;
3102 event_name
= get_event_decl_name(ctx
, node
);
3104 _BT_CPPLOGE_APPEND_CAUSE_NODE(node
, "Missing `name` attribute in event class.");
3109 event_class
= ctf_event_class_create();
3110 BT_ASSERT(event_class
);
3111 g_string_assign(event_class
->name
, event_name
);
3112 _TRY_PUSH_SCOPE_OR_GOTO_ERROR();
3115 bt_list_for_each_entry (iter
, decl_list
, siblings
) {
3116 ret
= visit_event_decl_entry(ctx
, iter
, event_class
, &stream_id
, &set
);
3118 _BT_CPPLOGE_APPEND_CAUSE_NODE(iter
,
3119 "Cannot visit event class's entry: "
3126 if (!_IS_SET(&set
, _EVENT_STREAM_ID_SET
)) {
3128 * Allow missing stream_id if there is only a single
3131 switch (ctx
->ctf_tc
->stream_classes
->len
) {
3133 /* Create implicit stream class if there's none */
3135 stream_class
= ctf_stream_class_create();
3136 BT_ASSERT(stream_class
);
3137 stream_class
->id
= stream_id
;
3138 g_ptr_array_add(ctx
->ctf_tc
->stream_classes
, stream_class
);
3141 /* Single stream class: get its ID */
3142 stream_class
= (ctf_stream_class
*) ctx
->ctf_tc
->stream_classes
->pdata
[0];
3143 stream_id
= stream_class
->id
;
3146 _BT_CPPLOGE_APPEND_CAUSE_NODE(node
, "Missing `stream_id` attribute in event class.");
3152 /* We have the stream ID now; get the stream class if found */
3153 if (!stream_class
) {
3154 stream_class
= ctf_trace_class_borrow_stream_class_by_id(ctx
->ctf_tc
, stream_id
);
3155 if (!stream_class
) {
3156 _BT_CPPLOGE_APPEND_CAUSE_NODE(node
,
3157 "Cannot find stream class at this point: "
3165 BT_ASSERT(stream_class
);
3167 if (!_IS_SET(&set
, _EVENT_ID_SET
)) {
3168 /* Allow only one event without ID per stream */
3169 if (stream_class
->event_classes
->len
!= 0) {
3170 _BT_CPPLOGE_APPEND_CAUSE_NODE(node
, "Missing `id` attribute in event class.");
3176 event_class
->id
= 0;
3179 if (ctf_stream_class_borrow_event_class_by_id(stream_class
, event_class
->id
)) {
3180 _BT_CPPLOGE_APPEND_CAUSE_NODE(node
,
3181 "Duplicate event class (same ID) in the same stream class: "
3188 ctf_stream_class_append_event_class(stream_class
, event_class
);
3193 ctf_event_class_destroy(event_class
);
3210 static int auto_map_field_to_trace_clock_class(struct ctf_visitor_generate_ir
*ctx
,
3211 struct ctf_field_class
*fc
)
3213 struct ctf_clock_class
*clock_class_to_map_to
= NULL
;
3214 uint64_t clock_class_count
;
3220 if (fc
->type
!= CTF_FIELD_CLASS_TYPE_INT
&& fc
->type
!= CTF_FIELD_CLASS_TYPE_ENUM
) {
3224 ctf_field_class_int
*int_fc
= ctf_field_class_as_int(fc
);
3226 if (int_fc
->mapped_clock_class
) {
3227 /* Already mapped */
3231 clock_class_count
= ctx
->ctf_tc
->clock_classes
->len
;
3233 switch (clock_class_count
) {
3236 * No clock class exists in the trace at this point. Create an
3237 * implicit one at 1 GHz, named `default`, and use this clock
3240 clock_class_to_map_to
= ctf_clock_class_create();
3241 BT_ASSERT(clock_class_to_map_to
);
3242 clock_class_to_map_to
->frequency
= UINT64_C(1000000000);
3243 g_string_assign(clock_class_to_map_to
->name
, "default");
3244 g_ptr_array_add(ctx
->ctf_tc
->clock_classes
, clock_class_to_map_to
);
3248 * Only one clock class exists in the trace at this point: use
3251 clock_class_to_map_to
= (ctf_clock_class
*) ctx
->ctf_tc
->clock_classes
->pdata
[0];
3255 * Timestamp field not mapped to a clock class and there's more
3256 * than one clock class in the trace: this is an error.
3258 BT_CPPLOGE_APPEND_CAUSE_SPEC(
3259 ctx
->logger
, "Timestamp field found with no mapped clock class, "
3260 "but there's more than one clock class in the trace at this point.");
3264 BT_ASSERT(clock_class_to_map_to
);
3265 int_fc
->mapped_clock_class
= clock_class_to_map_to
;
3270 static int auto_map_fields_to_trace_clock_class(struct ctf_visitor_generate_ir
*ctx
,
3271 struct ctf_field_class
*root_fc
,
3272 const char *field_name
)
3276 struct ctf_field_class_struct
*struct_fc
= (ctf_field_class_struct
*) root_fc
;
3277 struct ctf_field_class_variant
*var_fc
= (ctf_field_class_variant
*) root_fc
;
3283 if (root_fc
->type
!= CTF_FIELD_CLASS_TYPE_STRUCT
&&
3284 root_fc
->type
!= CTF_FIELD_CLASS_TYPE_VARIANT
) {
3288 if (root_fc
->type
== CTF_FIELD_CLASS_TYPE_STRUCT
) {
3289 count
= struct_fc
->members
->len
;
3291 count
= var_fc
->options
->len
;
3294 for (i
= 0; i
< count
; i
++) {
3295 struct ctf_named_field_class
*named_fc
= NULL
;
3297 if (root_fc
->type
== CTF_FIELD_CLASS_TYPE_STRUCT
) {
3298 named_fc
= ctf_field_class_struct_borrow_member_by_index(struct_fc
, i
);
3299 } else if (root_fc
->type
== CTF_FIELD_CLASS_TYPE_VARIANT
) {
3300 named_fc
= ctf_field_class_variant_borrow_option_by_index(var_fc
, i
);
3305 if (strcmp(named_fc
->name
->str
, field_name
) == 0) {
3306 ret
= auto_map_field_to_trace_clock_class(ctx
, named_fc
->fc
);
3308 BT_CPPLOGE_APPEND_CAUSE_SPEC(
3310 "Cannot automatically map field to trace's clock class: "
3311 "field-name=\"{}\"",
3317 ret
= auto_map_fields_to_trace_clock_class(ctx
, named_fc
->fc
, field_name
);
3319 BT_CPPLOGE_APPEND_CAUSE_SPEC(
3321 "Cannot automatically map structure or variant field class's fields to trace's clock class: "
3322 "field-name=\"{}\", root-field-name=\"{}\"",
3323 field_name
, named_fc
->name
->str
);
3332 static int visit_stream_decl_entry(struct ctf_visitor_generate_ir
*ctx
, struct ctf_node
*node
,
3333 struct ctf_stream_class
*stream_class
, int *set
)
3338 switch (node
->type
) {
3340 ret
= visit_field_class_def(ctx
, node
->u
.field_class_def
.field_class_specifier_list
,
3341 &node
->u
.field_class_def
.field_class_declarators
);
3343 _BT_CPPLOGE_APPEND_CAUSE_NODE(node
, "Cannot add field class found in stream class.");
3347 case NODE_TYPEALIAS
:
3348 ret
= visit_field_class_alias(ctx
, node
->u
.field_class_alias
.target
,
3349 node
->u
.field_class_alias
.alias
);
3351 _BT_CPPLOGE_APPEND_CAUSE_NODE(node
,
3352 "Cannot add field class alias found in stream class.");
3356 case NODE_CTF_EXPRESSION
:
3358 left
= ctf_ast_concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
3360 _BT_CPPLOGE_APPEND_CAUSE_NODE(node
, "Cannot concatenate unary strings.");
3365 if (strcmp(left
, "id") == 0) {
3368 if (_IS_SET(set
, _STREAM_ID_SET
)) {
3369 _BT_CPPLOGE_APPEND_CAUSE_DUP_ATTR(node
, "id", "stream declaration");
3374 ret
= get_unary_unsigned(ctx
, &node
->u
.ctf_expression
.right
, (uint64_t *) &id
);
3376 /* Only read "id" if get_unary_unsigned() succeeded. */
3377 if (ret
|| (!ret
&& id
< 0)) {
3378 _BT_CPPLOGE_APPEND_CAUSE_NODE(
3379 node
, "Unexpected unary expression for stream class's `id` attribute.");
3384 if (ctf_trace_class_borrow_stream_class_by_id(ctx
->ctf_tc
, id
)) {
3385 _BT_CPPLOGE_APPEND_CAUSE_NODE(node
, "Duplicate stream class (same ID): id={}", id
);
3390 stream_class
->id
= id
;
3391 _SET(set
, _STREAM_ID_SET
);
3392 } else if (strcmp(left
, "event.header") == 0) {
3393 if (_IS_SET(set
, _STREAM_EVENT_HEADER_SET
)) {
3394 _BT_CPPLOGE_APPEND_CAUSE_NODE(node
,
3395 "Duplicate `event.header` entry in stream class.");
3400 ret
= visit_field_class_specifier_list(
3401 ctx
, _BT_LIST_FIRST_ENTRY(&node
->u
.ctf_expression
.right
, struct ctf_node
, siblings
),
3402 &stream_class
->event_header_fc
);
3404 _BT_CPPLOGE_APPEND_CAUSE_NODE(
3405 node
, "Cannot create stream class's event header field class.");
3409 BT_ASSERT(stream_class
->event_header_fc
);
3410 ret
= auto_map_fields_to_trace_clock_class(ctx
, stream_class
->event_header_fc
,
3413 _BT_CPPLOGE_APPEND_CAUSE_NODE(
3415 "Cannot automatically map specific event header field class fields named `timestamp` to trace's clock class.");
3419 _SET(set
, _STREAM_EVENT_HEADER_SET
);
3420 } else if (strcmp(left
, "event.context") == 0) {
3421 if (_IS_SET(set
, _STREAM_EVENT_CONTEXT_SET
)) {
3422 _BT_CPPLOGE_APPEND_CAUSE_NODE(node
,
3423 "Duplicate `event.context` entry in stream class.");
3428 ret
= visit_field_class_specifier_list(
3429 ctx
, _BT_LIST_FIRST_ENTRY(&node
->u
.ctf_expression
.right
, struct ctf_node
, siblings
),
3430 &stream_class
->event_common_context_fc
);
3432 _BT_CPPLOGE_APPEND_CAUSE_NODE(
3433 node
, "Cannot create stream class's event context field class.");
3437 BT_ASSERT(stream_class
->event_common_context_fc
);
3438 _SET(set
, _STREAM_EVENT_CONTEXT_SET
);
3439 } else if (strcmp(left
, "packet.context") == 0) {
3440 if (_IS_SET(set
, _STREAM_PACKET_CONTEXT_SET
)) {
3441 _BT_CPPLOGE_APPEND_CAUSE_NODE(node
,
3442 "Duplicate `packet.context` entry in stream class.");
3447 ret
= visit_field_class_specifier_list(
3448 ctx
, _BT_LIST_FIRST_ENTRY(&node
->u
.ctf_expression
.right
, struct ctf_node
, siblings
),
3449 &stream_class
->packet_context_fc
);
3451 _BT_CPPLOGE_APPEND_CAUSE_NODE(
3452 node
, "Cannot create stream class's packet context field class.");
3456 BT_ASSERT(stream_class
->packet_context_fc
);
3457 ret
= auto_map_fields_to_trace_clock_class(ctx
, stream_class
->packet_context_fc
,
3460 _BT_CPPLOGE_APPEND_CAUSE_NODE(
3462 "Cannot automatically map specific packet context field class fields named `timestamp_begin` to trace's clock class.");
3466 ret
= auto_map_fields_to_trace_clock_class(ctx
, stream_class
->packet_context_fc
,
3469 _BT_CPPLOGE_APPEND_CAUSE_NODE(
3471 "Cannot automatically map specific packet context field class fields named `timestamp_end` to trace's clock class.");
3475 _SET(set
, _STREAM_PACKET_CONTEXT_SET
);
3477 _BT_CPPLOGW_NODE(node
,
3478 "Unknown attribute in stream class: "
3500 static int visit_stream_decl(struct ctf_visitor_generate_ir
*ctx
, struct ctf_node
*node
)
3504 struct ctf_node
*iter
;
3505 struct ctf_stream_class
*stream_class
= NULL
;
3506 struct bt_list_head
*decl_list
= &node
->u
.stream
.declaration_list
;
3508 if (node
->visited
) {
3512 node
->visited
= TRUE
;
3513 stream_class
= ctf_stream_class_create();
3514 BT_ASSERT(stream_class
);
3515 _TRY_PUSH_SCOPE_OR_GOTO_ERROR();
3517 bt_list_for_each_entry (iter
, decl_list
, siblings
) {
3518 ret
= visit_stream_decl_entry(ctx
, iter
, stream_class
, &set
);
3520 _BT_CPPLOGE_APPEND_CAUSE_NODE(iter
,
3521 "Cannot visit stream class's entry: "
3531 if (_IS_SET(&set
, _STREAM_ID_SET
)) {
3532 /* Check that packet header has `stream_id` field */
3533 struct ctf_named_field_class
*named_fc
= NULL
;
3535 if (!ctx
->ctf_tc
->packet_header_fc
) {
3536 _BT_CPPLOGE_APPEND_CAUSE_NODE(node
, "Stream class has a `id` attribute, "
3537 "but trace has no packet header field class.");
3542 named_fc
= ctf_field_class_struct_borrow_member_by_name(
3543 ctf_field_class_as_struct(ctx
->ctf_tc
->packet_header_fc
), "stream_id");
3545 _BT_CPPLOGE_APPEND_CAUSE_NODE(
3546 node
, "Stream class has a `id` attribute, "
3547 "but trace's packet header field class has no `stream_id` field.");
3552 if (named_fc
->fc
->type
!= CTF_FIELD_CLASS_TYPE_INT
&&
3553 named_fc
->fc
->type
!= CTF_FIELD_CLASS_TYPE_ENUM
) {
3554 _BT_CPPLOGE_APPEND_CAUSE_NODE(
3556 "Stream class has a `id` attribute, "
3557 "but trace's packet header field class's `stream_id` field is not an integer field class.");
3562 /* Allow only _one_ ID-less stream */
3563 if (ctx
->ctf_tc
->stream_classes
->len
!= 0) {
3564 _BT_CPPLOGE_APPEND_CAUSE_NODE(
3566 "Missing `id` attribute in stream class as there's more than one stream class in the trace.");
3571 /* Automatic ID: 0 */
3572 stream_class
->id
= 0;
3576 * Make sure that this stream class's ID is currently unique in
3579 if (ctf_trace_class_borrow_stream_class_by_id(ctx
->ctf_tc
, stream_class
->id
)) {
3580 _BT_CPPLOGE_APPEND_CAUSE_NODE(node
, "Duplicate stream class (same ID): id={}",
3586 g_ptr_array_add(ctx
->ctf_tc
->stream_classes
, stream_class
);
3587 stream_class
= NULL
;
3591 ctf_stream_class_destroy(stream_class
);
3592 stream_class
= NULL
;
3598 static int visit_trace_decl_entry(struct ctf_visitor_generate_ir
*ctx
, struct ctf_node
*node
,
3605 switch (node
->type
) {
3607 ret
= visit_field_class_def(ctx
, node
->u
.field_class_def
.field_class_specifier_list
,
3608 &node
->u
.field_class_def
.field_class_declarators
);
3610 _BT_CPPLOGE_APPEND_CAUSE_NODE(node
,
3611 "Cannot add field class found in trace (`trace` block).");
3615 case NODE_TYPEALIAS
:
3616 ret
= visit_field_class_alias(ctx
, node
->u
.field_class_alias
.target
,
3617 node
->u
.field_class_alias
.alias
);
3619 _BT_CPPLOGE_APPEND_CAUSE_NODE(
3620 node
, "Cannot add field class alias found in trace (`trace` block).");
3624 case NODE_CTF_EXPRESSION
:
3626 left
= ctf_ast_concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
3628 _BT_CPPLOGE_APPEND_CAUSE_NODE(node
, "Cannot concatenate unary strings.");
3633 if (strcmp(left
, "major") == 0) {
3634 if (_IS_SET(set
, _TRACE_MAJOR_SET
)) {
3635 _BT_CPPLOGE_APPEND_CAUSE_DUP_ATTR(node
, "major", "trace");
3640 ret
= get_unary_unsigned(ctx
, &node
->u
.ctf_expression
.right
, &val
);
3642 _BT_CPPLOGE_APPEND_CAUSE_NODE(
3643 node
, "Unexpected unary expression for trace's `major` attribute.");
3649 _BT_CPPLOGE_APPEND_CAUSE_NODE(node
,
3650 "Invalid trace's `minor` attribute: expecting 1.");
3655 ctx
->ctf_tc
->major
= val
;
3656 _SET(set
, _TRACE_MAJOR_SET
);
3657 } else if (strcmp(left
, "minor") == 0) {
3658 if (_IS_SET(set
, _TRACE_MINOR_SET
)) {
3659 _BT_CPPLOGE_APPEND_CAUSE_DUP_ATTR(node
, "minor", "trace");
3664 ret
= get_unary_unsigned(ctx
, &node
->u
.ctf_expression
.right
, &val
);
3666 _BT_CPPLOGE_APPEND_CAUSE_NODE(
3667 node
, "Unexpected unary expression for trace's `minor` attribute.");
3673 _BT_CPPLOGE_APPEND_CAUSE_NODE(node
,
3674 "Invalid trace's `minor` attribute: expecting 8.");
3679 ctx
->ctf_tc
->minor
= val
;
3680 _SET(set
, _TRACE_MINOR_SET
);
3681 } else if (strcmp(left
, "uuid") == 0) {
3682 if (_IS_SET(set
, _TRACE_UUID_SET
)) {
3683 _BT_CPPLOGE_APPEND_CAUSE_DUP_ATTR(node
, "uuid", "trace");
3688 ret
= get_unary_uuid(ctx
, &node
->u
.ctf_expression
.right
, ctx
->ctf_tc
->uuid
);
3690 _BT_CPPLOGE_APPEND_CAUSE_NODE(node
, "Invalid trace's `uuid` attribute.");
3694 ctx
->ctf_tc
->is_uuid_set
= true;
3695 _SET(set
, _TRACE_UUID_SET
);
3696 } else if (strcmp(left
, "byte_order") == 0) {
3697 /* Default byte order is already known at this stage */
3698 if (_IS_SET(set
, _TRACE_BYTE_ORDER_SET
)) {
3699 _BT_CPPLOGE_APPEND_CAUSE_DUP_ATTR(node
, "byte_order", "trace");
3704 BT_ASSERT(ctx
->ctf_tc
->default_byte_order
!= CTF_BYTE_ORDER_UNKNOWN
);
3705 _SET(set
, _TRACE_BYTE_ORDER_SET
);
3706 } else if (strcmp(left
, "packet.header") == 0) {
3707 if (_IS_SET(set
, _TRACE_PACKET_HEADER_SET
)) {
3708 _BT_CPPLOGE_APPEND_CAUSE_NODE(node
, "Duplicate `packet.header` entry in trace.");
3713 ret
= visit_field_class_specifier_list(
3714 ctx
, _BT_LIST_FIRST_ENTRY(&node
->u
.ctf_expression
.right
, struct ctf_node
, siblings
),
3715 &ctx
->ctf_tc
->packet_header_fc
);
3717 _BT_CPPLOGE_APPEND_CAUSE_NODE(node
,
3718 "Cannot create trace's packet header field class.");
3722 BT_ASSERT(ctx
->ctf_tc
->packet_header_fc
);
3723 _SET(set
, _TRACE_PACKET_HEADER_SET
);
3725 _BT_CPPLOGW_NODE(node
,
3726 "Unknown attribute in stream class: "
3736 _BT_CPPLOGE_APPEND_CAUSE_NODE(node
, "Unknown expression in trace.");
3748 static int visit_trace_decl(struct ctf_visitor_generate_ir
*ctx
, struct ctf_node
*node
)
3752 struct ctf_node
*iter
;
3753 struct bt_list_head
*decl_list
= &node
->u
.trace
.declaration_list
;
3755 if (node
->visited
) {
3759 node
->visited
= TRUE
;
3761 if (ctx
->is_trace_visited
) {
3762 _BT_CPPLOGE_APPEND_CAUSE_NODE(node
, "Duplicate trace (`trace` block).");
3767 _TRY_PUSH_SCOPE_OR_GOTO_ERROR();
3769 bt_list_for_each_entry (iter
, decl_list
, siblings
) {
3770 ret
= visit_trace_decl_entry(ctx
, iter
, &set
);
3772 _BT_CPPLOGE_APPEND_CAUSE_NODE(iter
,
3773 "Cannot visit trace's entry (`trace` block): "
3783 if (!_IS_SET(&set
, _TRACE_MAJOR_SET
)) {
3784 _BT_CPPLOGE_APPEND_CAUSE_NODE(node
, "Missing `major` attribute in trace (`trace` block).");
3789 if (!_IS_SET(&set
, _TRACE_MINOR_SET
)) {
3790 _BT_CPPLOGE_APPEND_CAUSE_NODE(node
, "Missing `minor` attribute in trace (`trace` block).");
3795 if (!_IS_SET(&set
, _TRACE_BYTE_ORDER_SET
)) {
3796 _BT_CPPLOGE_APPEND_CAUSE_NODE(node
,
3797 "Missing `byte_order` attribute in trace (`trace` block).");
3802 ctx
->is_trace_visited
= true;
3811 static int visit_env(struct ctf_visitor_generate_ir
*ctx
, struct ctf_node
*node
)
3815 struct ctf_node
*entry_node
;
3816 struct bt_list_head
*decl_list
= &node
->u
.env
.declaration_list
;
3818 if (node
->visited
) {
3822 node
->visited
= TRUE
;
3824 bt_list_for_each_entry (entry_node
, decl_list
, siblings
) {
3825 struct bt_list_head
*right_head
= &entry_node
->u
.ctf_expression
.right
;
3827 if (entry_node
->type
!= NODE_CTF_EXPRESSION
) {
3828 _BT_CPPLOGE_APPEND_CAUSE_NODE(entry_node
,
3829 "Wrong expression in environment entry: "
3836 left
= ctf_ast_concatenate_unary_strings(&entry_node
->u
.ctf_expression
.left
);
3838 _BT_CPPLOGE_APPEND_CAUSE_NODE(entry_node
, "Cannot get environment entry's name.");
3843 if (is_unary_string(right_head
)) {
3844 char *right
= ctf_ast_concatenate_unary_strings(right_head
);
3847 _BT_CPPLOGE_APPEND_CAUSE_NODE(
3849 "Unexpected unary expression for environment entry's value: "
3856 if (strcmp(left
, "tracer_name") == 0) {
3857 if (strncmp(right
, "lttng", 5) == 0) {
3858 BT_CPPLOGI_SPEC(ctx
->logger
,
3859 "Detected LTTng trace from `{}` environment value: "
3860 "tracer-name=\"{}\"",
3862 ctx
->is_lttng
= true;
3866 ctf_trace_class_append_env_entry(ctx
->ctf_tc
, left
, CTF_TRACE_CLASS_ENV_ENTRY_TYPE_STR
,
3869 } else if (is_unary_unsigned(right_head
) || is_unary_signed(right_head
)) {
3872 if (is_unary_unsigned(right_head
)) {
3873 ret
= get_unary_unsigned(ctx
, right_head
, (uint64_t *) &v
);
3875 ret
= get_unary_signed(right_head
, &v
);
3878 _BT_CPPLOGE_APPEND_CAUSE_NODE(
3880 "Unexpected unary expression for environment entry's value: "
3887 ctf_trace_class_append_env_entry(ctx
->ctf_tc
, left
, CTF_TRACE_CLASS_ENV_ENTRY_TYPE_INT
,
3890 _BT_CPPLOGW_NODE(entry_node
,
3891 "Environment entry has unknown type: "
3908 static int set_trace_byte_order(struct ctf_visitor_generate_ir
*ctx
, struct ctf_node
*trace_node
)
3913 struct ctf_node
*node
;
3914 struct bt_list_head
*decl_list
= &trace_node
->u
.trace
.declaration_list
;
3916 bt_list_for_each_entry (node
, decl_list
, siblings
) {
3917 if (node
->type
== NODE_CTF_EXPRESSION
) {
3918 struct ctf_node
*right_node
;
3920 left
= ctf_ast_concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
3922 _BT_CPPLOGE_APPEND_CAUSE_NODE(node
, "Cannot concatenate unary strings.");
3927 if (strcmp(left
, "byte_order") == 0) {
3928 enum ctf_byte_order bo
;
3930 if (_IS_SET(&set
, _TRACE_BYTE_ORDER_SET
)) {
3931 _BT_CPPLOGE_APPEND_CAUSE_DUP_ATTR(node
, "byte_order", "trace");
3936 _SET(&set
, _TRACE_BYTE_ORDER_SET
);
3938 _BT_LIST_FIRST_ENTRY(&node
->u
.ctf_expression
.right
, struct ctf_node
, siblings
);
3939 bo
= byte_order_from_unary_expr(ctx
, right_node
);
3940 if (bo
== CTF_BYTE_ORDER_UNKNOWN
) {
3941 _BT_CPPLOGE_APPEND_CAUSE_NODE(
3942 node
, "Invalid `byte_order` attribute in trace (`trace` block): "
3943 "expecting `le`, `be`, or `network`.");
3946 } else if (bo
== CTF_BYTE_ORDER_DEFAULT
) {
3947 _BT_CPPLOGE_APPEND_CAUSE_NODE(
3948 node
, "Invalid `byte_order` attribute in trace (`trace` block): "
3949 "cannot be set to `native` here.");
3954 ctx
->ctf_tc
->default_byte_order
= bo
;
3962 if (!_IS_SET(&set
, _TRACE_BYTE_ORDER_SET
)) {
3963 _BT_CPPLOGE_APPEND_CAUSE_NODE(trace_node
,
3964 "Missing `byte_order` attribute in trace (`trace` block).");
3976 static int visit_clock_decl_entry(struct ctf_visitor_generate_ir
*ctx
, struct ctf_node
*entry_node
,
3977 struct ctf_clock_class
*clock
, int *set
, int64_t *offset_seconds
,
3978 uint64_t *offset_cycles
)
3983 if (entry_node
->type
!= NODE_CTF_EXPRESSION
) {
3984 _BT_CPPLOGE_APPEND_CAUSE_NODE(entry_node
, "Unexpected node type: node-type={}",
3990 left
= ctf_ast_concatenate_unary_strings(&entry_node
->u
.ctf_expression
.left
);
3992 _BT_CPPLOGE_APPEND_CAUSE_NODE(entry_node
, "Cannot concatenate unary strings.");
3997 if (strcmp(left
, "name") == 0) {
4000 if (_IS_SET(set
, _CLOCK_NAME_SET
)) {
4001 _BT_CPPLOGE_APPEND_CAUSE_DUP_ATTR(entry_node
, "name", "clock class");
4006 right
= ctf_ast_concatenate_unary_strings(&entry_node
->u
.ctf_expression
.right
);
4008 _BT_CPPLOGE_APPEND_CAUSE_NODE(
4009 entry_node
, "Unexpected unary expression for clock class's `name` attribute.");
4014 g_string_assign(clock
->name
, right
);
4016 _SET(set
, _CLOCK_NAME_SET
);
4017 } else if (strcmp(left
, "uuid") == 0) {
4020 if (_IS_SET(set
, _CLOCK_UUID_SET
)) {
4021 _BT_CPPLOGE_APPEND_CAUSE_DUP_ATTR(entry_node
, "uuid", "clock class");
4026 ret
= get_unary_uuid(ctx
, &entry_node
->u
.ctf_expression
.right
, uuid
);
4028 _BT_CPPLOGE_APPEND_CAUSE_NODE(entry_node
, "Invalid clock class's `uuid` attribute.");
4032 clock
->has_uuid
= true;
4033 bt_uuid_copy(clock
->uuid
, uuid
);
4034 _SET(set
, _CLOCK_UUID_SET
);
4035 } else if (strcmp(left
, "description") == 0) {
4038 if (_IS_SET(set
, _CLOCK_DESCRIPTION_SET
)) {
4039 _BT_CPPLOGE_APPEND_CAUSE_DUP_ATTR(entry_node
, "description", "clock class");
4044 right
= ctf_ast_concatenate_unary_strings(&entry_node
->u
.ctf_expression
.right
);
4046 _BT_CPPLOGE_APPEND_CAUSE_NODE(
4048 "Unexpected unary expression for clock class's `description` attribute.");
4053 g_string_assign(clock
->description
, right
);
4055 _SET(set
, _CLOCK_DESCRIPTION_SET
);
4056 } else if (strcmp(left
, "freq") == 0) {
4057 uint64_t freq
= UINT64_C(-1);
4059 if (_IS_SET(set
, _CLOCK_FREQ_SET
)) {
4060 _BT_CPPLOGE_APPEND_CAUSE_DUP_ATTR(entry_node
, "freq", "clock class");
4065 ret
= get_unary_unsigned(ctx
, &entry_node
->u
.ctf_expression
.right
, &freq
);
4067 _BT_CPPLOGE_APPEND_CAUSE_NODE(
4068 entry_node
, "Unexpected unary expression for clock class's `freq` attribute.");
4073 if (freq
== UINT64_C(-1) || freq
== 0) {
4074 _BT_CPPLOGE_APPEND_CAUSE_NODE(entry_node
, "Invalid clock class frequency: freq={}",
4080 clock
->frequency
= freq
;
4081 _SET(set
, _CLOCK_FREQ_SET
);
4082 } else if (strcmp(left
, "precision") == 0) {
4085 if (_IS_SET(set
, _CLOCK_PRECISION_SET
)) {
4086 _BT_CPPLOGE_APPEND_CAUSE_DUP_ATTR(entry_node
, "precision", "clock class");
4091 ret
= get_unary_unsigned(ctx
, &entry_node
->u
.ctf_expression
.right
, &precision
);
4093 _BT_CPPLOGE_APPEND_CAUSE_NODE(
4094 entry_node
, "Unexpected unary expression for clock class's `precision` attribute.");
4099 clock
->precision
= precision
;
4100 _SET(set
, _CLOCK_PRECISION_SET
);
4101 } else if (strcmp(left
, "offset_s") == 0) {
4102 if (_IS_SET(set
, _CLOCK_OFFSET_S_SET
)) {
4103 _BT_CPPLOGE_APPEND_CAUSE_DUP_ATTR(entry_node
, "offset_s", "clock class");
4108 ret
= get_unary_signed(&entry_node
->u
.ctf_expression
.right
, offset_seconds
);
4110 _BT_CPPLOGE_APPEND_CAUSE_NODE(
4111 entry_node
, "Unexpected unary expression for clock class's `offset_s` attribute.");
4116 _SET(set
, _CLOCK_OFFSET_S_SET
);
4117 } else if (strcmp(left
, "offset") == 0) {
4118 if (_IS_SET(set
, _CLOCK_OFFSET_SET
)) {
4119 _BT_CPPLOGE_APPEND_CAUSE_DUP_ATTR(entry_node
, "offset", "clock class");
4124 ret
= get_unary_unsigned(ctx
, &entry_node
->u
.ctf_expression
.right
, offset_cycles
);
4126 _BT_CPPLOGE_APPEND_CAUSE_NODE(
4127 entry_node
, "Unexpected unary expression for clock class's `offset` attribute.");
4132 _SET(set
, _CLOCK_OFFSET_SET
);
4133 } else if (strcmp(left
, "absolute") == 0) {
4134 struct ctf_node
*right
;
4136 if (_IS_SET(set
, _CLOCK_ABSOLUTE_SET
)) {
4137 _BT_CPPLOGE_APPEND_CAUSE_DUP_ATTR(entry_node
, "absolute", "clock class");
4143 _BT_LIST_FIRST_ENTRY(&entry_node
->u
.ctf_expression
.right
, struct ctf_node
, siblings
);
4144 ret
= get_boolean(ctx
, right
);
4146 _BT_CPPLOGE_APPEND_CAUSE_NODE(
4147 entry_node
, "Unexpected unary expression for clock class's `absolute` attribute.");
4152 clock
->is_absolute
= ret
;
4153 _SET(set
, _CLOCK_ABSOLUTE_SET
);
4155 _BT_CPPLOGW_NODE(entry_node
, "Unknown attribute in clock class: attr-name=\"{}\"", left
);
4167 static inline uint64_t cycles_from_ns(uint64_t frequency
, uint64_t ns
)
4172 if (frequency
== UINT64_C(1000000000)) {
4175 cycles
= (uint64_t) (((double) ns
* (double) frequency
) / 1e9
);
4181 static void calibrate_clock_class_offsets(int64_t *offset_seconds
, uint64_t *offset_cycles
,
4184 if (*offset_cycles
>= freq
) {
4185 const uint64_t s_in_offset_cycles
= *offset_cycles
/ freq
;
4187 *offset_seconds
+= (int64_t) s_in_offset_cycles
;
4188 *offset_cycles
-= (s_in_offset_cycles
* freq
);
4192 static void apply_clock_class_is_absolute(struct ctf_visitor_generate_ir
*ctx
,
4193 struct ctf_clock_class
*clock
)
4195 if (ctx
->decoder_config
.clkClsCfg
.forceOriginIsUnixEpoch
) {
4196 clock
->is_absolute
= true;
4202 static void apply_clock_class_offset(struct ctf_visitor_generate_ir
*ctx
,
4203 struct ctf_clock_class
*clock
)
4206 int64_t offset_s_to_apply
= ctx
->decoder_config
.clkClsCfg
.offsetSec
;
4207 uint64_t offset_ns_to_apply
;
4208 int64_t cur_offset_s
;
4209 uint64_t cur_offset_cycles
;
4211 if (ctx
->decoder_config
.clkClsCfg
.offsetSec
== 0 &&
4212 ctx
->decoder_config
.clkClsCfg
.offsetNanoSec
== 0) {
4216 /* Transfer nanoseconds to seconds as much as possible */
4217 if (ctx
->decoder_config
.clkClsCfg
.offsetNanoSec
< 0) {
4218 const int64_t abs_ns
= -ctx
->decoder_config
.clkClsCfg
.offsetNanoSec
;
4219 const int64_t abs_extra_s
= abs_ns
/ INT64_C(1000000000) + 1;
4220 const int64_t extra_s
= -abs_extra_s
;
4221 const int64_t offset_ns
=
4222 ctx
->decoder_config
.clkClsCfg
.offsetNanoSec
- (extra_s
* INT64_C(1000000000));
4224 BT_ASSERT(offset_ns
> 0);
4225 offset_ns_to_apply
= (uint64_t) offset_ns
;
4226 offset_s_to_apply
+= extra_s
;
4228 const int64_t extra_s
= ctx
->decoder_config
.clkClsCfg
.offsetNanoSec
/ INT64_C(1000000000);
4229 const int64_t offset_ns
=
4230 ctx
->decoder_config
.clkClsCfg
.offsetNanoSec
- (extra_s
* INT64_C(1000000000));
4232 BT_ASSERT(offset_ns
>= 0);
4233 offset_ns_to_apply
= (uint64_t) offset_ns
;
4234 offset_s_to_apply
+= extra_s
;
4237 freq
= clock
->frequency
;
4238 cur_offset_s
= clock
->offset_seconds
;
4239 cur_offset_cycles
= clock
->offset_cycles
;
4242 cur_offset_s
+= offset_s_to_apply
;
4243 cur_offset_cycles
+= cycles_from_ns(freq
, offset_ns_to_apply
);
4246 * Recalibrate offsets because the part in cycles can be greater
4247 * than the frequency at this point.
4249 calibrate_clock_class_offsets(&cur_offset_s
, &cur_offset_cycles
, freq
);
4251 /* Set final offsets */
4252 clock
->offset_seconds
= cur_offset_s
;
4253 clock
->offset_cycles
= cur_offset_cycles
;
4259 static int visit_clock_decl(struct ctf_visitor_generate_ir
*ctx
, struct ctf_node
*clock_node
)
4263 struct ctf_clock_class
*clock
;
4264 struct ctf_node
*entry_node
;
4265 struct bt_list_head
*decl_list
= &clock_node
->u
.clock
.declaration_list
;
4266 const char *clock_class_name
;
4267 int64_t offset_seconds
= 0;
4268 uint64_t offset_cycles
= 0;
4271 if (clock_node
->visited
) {
4275 clock_node
->visited
= TRUE
;
4277 /* CTF 1.8's default frequency for a clock class is 1 GHz */
4278 clock
= ctf_clock_class_create();
4280 _BT_CPPLOGE_APPEND_CAUSE_NODE(clock_node
, "Cannot create default clock class.");
4285 bt_list_for_each_entry (entry_node
, decl_list
, siblings
) {
4286 ret
= visit_clock_decl_entry(ctx
, entry_node
, clock
, &set
, &offset_seconds
, &offset_cycles
);
4288 _BT_CPPLOGE_APPEND_CAUSE_NODE(entry_node
, "Cannot visit clock class's entry: ret={}",
4294 if (!_IS_SET(&set
, _CLOCK_NAME_SET
)) {
4295 _BT_CPPLOGE_APPEND_CAUSE_NODE(clock_node
, "Missing `name` attribute in clock class.");
4300 clock_class_name
= clock
->name
->str
;
4301 BT_ASSERT(clock_class_name
);
4302 if (ctx
->is_lttng
&& strcmp(clock_class_name
, "monotonic") == 0) {
4304 * Old versions of LTTng forgot to set its clock class
4305 * as absolute, even if it is. This is important because
4306 * it's a condition to be able to sort messages
4307 * from different sources.
4309 clock
->is_absolute
= true;
4313 * Adjust offsets so that the part in cycles is less than the
4314 * frequency (move to the part in seconds).
4316 freq
= clock
->frequency
;
4317 calibrate_clock_class_offsets(&offset_seconds
, &offset_cycles
, freq
);
4318 BT_ASSERT(offset_cycles
< clock
->frequency
);
4319 clock
->offset_seconds
= offset_seconds
;
4320 clock
->offset_cycles
= offset_cycles
;
4321 apply_clock_class_offset(ctx
, clock
);
4322 apply_clock_class_is_absolute(ctx
, clock
);
4323 g_ptr_array_add(ctx
->ctf_tc
->clock_classes
, clock
);
4328 ctf_clock_class_destroy(clock
);
4334 static int visit_root_decl(struct ctf_visitor_generate_ir
*ctx
, struct ctf_node
*root_decl_node
)
4338 if (root_decl_node
->visited
) {
4342 root_decl_node
->visited
= TRUE
;
4344 switch (root_decl_node
->type
) {
4347 visit_field_class_def(ctx
, root_decl_node
->u
.field_class_def
.field_class_specifier_list
,
4348 &root_decl_node
->u
.field_class_def
.field_class_declarators
);
4350 _BT_CPPLOGE_APPEND_CAUSE_NODE(root_decl_node
,
4351 "Cannot add field class found in root scope.");
4355 case NODE_TYPEALIAS
:
4356 ret
= visit_field_class_alias(ctx
, root_decl_node
->u
.field_class_alias
.target
,
4357 root_decl_node
->u
.field_class_alias
.alias
);
4359 _BT_CPPLOGE_APPEND_CAUSE_NODE(root_decl_node
,
4360 "Cannot add field class alias found in root scope.");
4364 case NODE_TYPE_SPECIFIER_LIST
:
4366 struct ctf_field_class
*decl
= NULL
;
4369 * Just add the field class specifier to the root
4370 * declaration scope. Put local reference.
4372 ret
= visit_field_class_specifier_list(ctx
, root_decl_node
, &decl
);
4374 _BT_CPPLOGE_APPEND_CAUSE_NODE(root_decl_node
,
4375 "Cannot visit root scope's field class: "
4382 ctf_field_class_destroy(decl
);
4387 _BT_CPPLOGE_APPEND_CAUSE_NODE(root_decl_node
, "Unexpected node type: node-type={}",
4388 root_decl_node
->type
);
4397 ctf_visitor_generate_ir::UP
4398 ctf_visitor_generate_ir_create(const struct ctf_metadata_decoder_config
*decoder_config
)
4400 bt2c::Logger logger
{decoder_config
->logger
, "PLUGIN/CTF/META/IR-VISITOR"};
4402 /* Create visitor's context */
4403 ctf_visitor_generate_ir::UP ctx
= ctx_create(decoder_config
, logger
);
4406 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger
, "Cannot create visitor's context.");
4419 bt2::TraceClass::Shared
4420 ctf_visitor_generate_ir_get_ir_trace_class(struct ctf_visitor_generate_ir
*ctx
)
4424 return ctx
->trace_class
;
4427 struct ctf_trace_class
*
4428 ctf_visitor_generate_ir_borrow_ctf_trace_class(struct ctf_visitor_generate_ir
*ctx
)
4431 BT_ASSERT_DBG(ctx
->ctf_tc
);
4435 int ctf_visitor_generate_ir_visit_node(struct ctf_visitor_generate_ir
*ctx
, struct ctf_node
*node
)
4439 BT_CPPLOGI_STR_SPEC(ctx
->logger
, "Visiting metadata's AST to generate CTF IR objects.");
4441 switch (node
->type
) {
4444 struct ctf_node
*iter
;
4445 bool got_trace_decl
= false;
4448 * The first thing we need is the native byte order of
4449 * the trace block, because early class aliases can have
4450 * a `byte_order` attribute set to `native`. If we don't
4451 * have the native byte order yet, and we don't have any
4452 * trace block yet, then fail with EINCOMPLETE.
4454 if (ctx
->ctf_tc
->default_byte_order
== CTF_BYTE_ORDER_UNKNOWN
) {
4455 bt_list_for_each_entry (iter
, &node
->u
.root
.trace
, siblings
) {
4456 if (got_trace_decl
) {
4457 _BT_CPPLOGE_APPEND_CAUSE_NODE(node
, "Duplicate trace (`trace` block).");
4462 ret
= set_trace_byte_order(ctx
, iter
);
4464 _BT_CPPLOGE_APPEND_CAUSE_NODE(node
,
4465 "Cannot set trace's native byte order: "
4471 got_trace_decl
= true;
4474 if (!got_trace_decl
) {
4475 BT_CPPLOGD_STR_SPEC(ctx
->logger
, "Incomplete AST: need trace (`trace` block).");
4481 BT_ASSERT(ctx
->ctf_tc
->default_byte_order
== CTF_BYTE_ORDER_LITTLE
||
4482 ctx
->ctf_tc
->default_byte_order
== CTF_BYTE_ORDER_BIG
);
4483 BT_ASSERT(ctx
->current_scope
&& !ctx
->current_scope
->parent_scope
);
4486 bt_list_for_each_entry (iter
, &node
->u
.root
.env
, siblings
) {
4487 ret
= visit_env(ctx
, iter
);
4489 _BT_CPPLOGE_APPEND_CAUSE_NODE(
4491 "Cannot visit trace's environment (`env` block) entry: "
4498 BT_ASSERT(ctx
->current_scope
&& !ctx
->current_scope
->parent_scope
);
4501 * Visit clock blocks.
4503 bt_list_for_each_entry (iter
, &node
->u
.root
.clock
, siblings
) {
4504 ret
= visit_clock_decl(ctx
, iter
);
4506 _BT_CPPLOGE_APPEND_CAUSE_NODE(iter
, "Cannot visit clock class: ret={}", ret
);
4511 BT_ASSERT(ctx
->current_scope
&& !ctx
->current_scope
->parent_scope
);
4514 * Visit root declarations next, as they can be used by any
4517 bt_list_for_each_entry (iter
, &node
->u
.root
.declaration_list
, siblings
) {
4518 ret
= visit_root_decl(ctx
, iter
);
4520 _BT_CPPLOGE_APPEND_CAUSE_NODE(iter
, "Cannot visit root entry: ret={}", ret
);
4525 BT_ASSERT(ctx
->current_scope
&& !ctx
->current_scope
->parent_scope
);
4527 /* Callsite blocks are not supported */
4528 bt_list_for_each_entry (iter
, &node
->u
.root
.callsite
, siblings
) {
4529 _BT_CPPLOGW_NODE(iter
, "\"callsite\" blocks are not supported as of this version.");
4532 BT_ASSERT(ctx
->current_scope
&& !ctx
->current_scope
->parent_scope
);
4535 bt_list_for_each_entry (iter
, &node
->u
.root
.trace
, siblings
) {
4536 ret
= visit_trace_decl(ctx
, iter
);
4538 _BT_CPPLOGE_APPEND_CAUSE_NODE(iter
,
4539 "Cannot visit trace (`trace` block): "
4546 BT_ASSERT(ctx
->current_scope
&& !ctx
->current_scope
->parent_scope
);
4549 bt_list_for_each_entry (iter
, &node
->u
.root
.stream
, siblings
) {
4550 ret
= visit_stream_decl(ctx
, iter
);
4552 _BT_CPPLOGE_APPEND_CAUSE_NODE(iter
, "Cannot visit stream class: ret={}", ret
);
4557 BT_ASSERT(ctx
->current_scope
&& !ctx
->current_scope
->parent_scope
);
4560 bt_list_for_each_entry (iter
, &node
->u
.root
.event
, siblings
) {
4561 ret
= visit_event_decl(ctx
, iter
);
4563 _BT_CPPLOGE_APPEND_CAUSE_NODE(iter
, "Cannot visit event class: ret={}", ret
);
4568 BT_ASSERT(ctx
->current_scope
&& !ctx
->current_scope
->parent_scope
);
4572 _BT_CPPLOGE_APPEND_CAUSE_NODE(node
, "Unexpected node type: node-type={}", node
->type
);
4577 /* Update default clock classes */
4578 ret
= ctf_trace_class_update_default_clock_classes(ctx
->ctf_tc
, ctx
->logger
);
4584 /* Update trace class meanings */
4585 ret
= ctf_trace_class_update_meanings(ctx
->ctf_tc
);
4591 /* Update stream class configuration */
4592 ret
= ctf_trace_class_update_stream_class_config(ctx
->ctf_tc
);
4598 /* Update text arrays and sequences */
4599 ret
= ctf_trace_class_update_text_array_sequence(ctx
->ctf_tc
);
4605 /* Update structure/array/sequence alignments */
4606 ret
= ctf_trace_class_update_alignments(ctx
->ctf_tc
);
4612 /* Resolve sequence lengths and variant tags */
4613 ret
= ctf_trace_class_resolve_field_classes(ctx
->ctf_tc
, ctx
->logger
);
4619 if (ctx
->trace_class
) {
4621 * Update "in IR" for field classes.
4623 * If we have no IR trace class, then we'll have no way
4624 * to create IR fields anyway, so we leave all the
4625 * `in_ir` members false.
4627 ret
= ctf_trace_class_update_in_ir(ctx
->ctf_tc
);
4634 /* Update saved value indexes */
4635 ret
= ctf_trace_class_update_value_storing_indexes(ctx
->ctf_tc
);
4641 /* Validate what we have so far */
4642 ret
= ctf_trace_class_validate(ctx
->ctf_tc
, ctx
->logger
);
4649 * If there are fields which are not related to the CTF format
4650 * itself in the packet header and in event header field
4651 * classes, warn about it because they are never translated.
4653 ctf_trace_class_warn_meaningless_header_fields(ctx
->ctf_tc
, ctx
->logger
);
4655 if (ctx
->trace_class
) {
4656 /* Copy new CTF metadata -> new IR metadata */
4657 ret
= ctf_trace_class_translate(ctx
->decoder_config
.self_comp
,
4658 ctx
->trace_class
->libObjPtr(), ctx
->ctf_tc
);