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).
18 #include <babeltrace2/babeltrace.h>
20 #define BT_COMP_LOG_SELF_COMP (ctx->log_cfg.self_comp)
21 #define BT_COMP_LOG_SELF_COMP_CLASS (ctx->log_cfg.self_comp_class)
22 #define BT_LOG_OUTPUT_LEVEL (ctx->log_cfg.log_level)
23 #define BT_LOG_TAG "PLUGIN/CTF/META/IR-VISITOR"
24 #include "logging.hpp"
25 #include "logging/comp-logging.h"
27 #include "common/assert.h"
28 #include "common/common.h"
29 #include "common/uuid.h"
30 #include "compat/endian.h" /* IWYU pragma: keep */
33 #include "ctf-meta-visitors.hpp"
34 #include "ctf-meta.hpp"
35 #include "decoder.hpp"
37 /* Bit value (left shift) */
38 #define _BV(_val) (1 << (_val))
40 /* Bit is set in a set of bits */
41 #define _IS_SET(_set, _mask) (*(_set) & (_mask))
43 /* Set bit in a set of bits */
44 #define _SET(_set, _mask) (*(_set) |= (_mask))
46 /* Try to push scope, or go to the `error` label */
47 #define _TRY_PUSH_SCOPE_OR_GOTO_ERROR() \
49 ret = ctx_push_scope(ctx); \
51 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Cannot push scope."); \
56 /* Bits for verifying existing attributes in various declarations */
59 _CLOCK_NAME_SET
= _BV(0),
60 _CLOCK_UUID_SET
= _BV(1),
61 _CLOCK_FREQ_SET
= _BV(2),
62 _CLOCK_PRECISION_SET
= _BV(3),
63 _CLOCK_OFFSET_S_SET
= _BV(4),
64 _CLOCK_OFFSET_SET
= _BV(5),
65 _CLOCK_ABSOLUTE_SET
= _BV(6),
66 _CLOCK_DESCRIPTION_SET
= _BV(7),
71 _INTEGER_ALIGN_SET
= _BV(0),
72 _INTEGER_SIZE_SET
= _BV(1),
73 _INTEGER_BASE_SET
= _BV(2),
74 _INTEGER_ENCODING_SET
= _BV(3),
75 _INTEGER_BYTE_ORDER_SET
= _BV(4),
76 _INTEGER_SIGNED_SET
= _BV(5),
77 _INTEGER_MAP_SET
= _BV(6),
82 _FLOAT_ALIGN_SET
= _BV(0),
83 _FLOAT_MANT_DIG_SET
= _BV(1),
84 _FLOAT_EXP_DIG_SET
= _BV(2),
85 _FLOAT_BYTE_ORDER_SET
= _BV(3),
90 _STRING_ENCODING_SET
= _BV(0),
95 _TRACE_MINOR_SET
= _BV(0),
96 _TRACE_MAJOR_SET
= _BV(1),
97 _TRACE_BYTE_ORDER_SET
= _BV(2),
98 _TRACE_UUID_SET
= _BV(3),
99 _TRACE_PACKET_HEADER_SET
= _BV(4),
104 _STREAM_ID_SET
= _BV(0),
105 _STREAM_PACKET_CONTEXT_SET
= _BV(1),
106 _STREAM_EVENT_HEADER_SET
= _BV(2),
107 _STREAM_EVENT_CONTEXT_SET
= _BV(3),
112 _EVENT_NAME_SET
= _BV(0),
113 _EVENT_ID_SET
= _BV(1),
114 _EVENT_MODEL_EMF_URI_SET
= _BV(2),
115 _EVENT_STREAM_ID_SET
= _BV(3),
116 _EVENT_LOG_LEVEL_SET
= _BV(4),
117 _EVENT_CONTEXT_SET
= _BV(5),
118 _EVENT_FIELDS_SET
= _BV(6),
127 LOG_LEVEL_WARNING
= 4,
128 LOG_LEVEL_NOTICE
= 5,
130 LOG_LEVEL_DEBUG_SYSTEM
= 7,
131 LOG_LEVEL_DEBUG_PROGRAM
= 8,
132 LOG_LEVEL_DEBUG_PROCESS
= 9,
133 LOG_LEVEL_DEBUG_MODULE
= 10,
134 LOG_LEVEL_DEBUG_UNIT
= 11,
135 LOG_LEVEL_DEBUG_FUNCTION
= 12,
136 LOG_LEVEL_DEBUG_LINE
= 13,
137 LOG_LEVEL_DEBUG
= 14,
141 /* Prefixes of class aliases */
142 #define _PREFIX_ALIAS 'a'
143 #define _PREFIX_ENUM 'e'
144 #define _PREFIX_STRUCT 's'
145 #define _PREFIX_VARIANT 'v'
147 /* First entry in a BT list */
148 #define _BT_LIST_FIRST_ENTRY(_ptr, _class, _member) bt_list_entry((_ptr)->next, _class, _member)
150 #define _BT_COMP_LOGE_APPEND_CAUSE_DUP_ATTR(_node, _attr, _entity) \
151 _BT_COMP_LOGE_APPEND_CAUSE_LINENO( \
152 (_node)->lineno, "Duplicate attribute in %s: attr-name=\"%s\"", _entity, _attr)
154 #define _BT_COMP_LOGE_NODE(_node, _msg, args...) _BT_COMP_LOGE_LINENO((_node)->lineno, _msg, ##args)
156 #define _BT_COMP_LOGE_APPEND_CAUSE_NODE(_node, _msg, args...) \
157 _BT_COMP_LOGE_APPEND_CAUSE_LINENO((_node)->lineno, _msg, ##args)
159 #define _BT_COMP_LOGW_NODE(_node, _msg, args...) _BT_COMP_LOGW_LINENO((_node)->lineno, _msg, ##args)
161 #define _BT_COMP_LOGT_NODE(_node, _msg, args...) _BT_COMP_LOGT_LINENO((_node)->lineno, _msg, ##args)
164 * Declaration scope of a visitor context. This represents a TSDL
165 * lexical scope, so that aliases and named structures, variants,
166 * and enumerations may be registered and looked up hierarchically.
168 struct ctx_decl_scope
171 * Alias name to field class.
173 * GQuark -> struct ctf_field_class * (owned by this)
175 GHashTable
*decl_map
;
177 /* Parent scope; NULL if this is the root declaration scope */
178 struct ctx_decl_scope
*parent_scope
;
182 * Visitor context (private).
184 struct ctf_visitor_generate_ir
186 struct meta_log_config log_cfg
;
188 /* Trace IR trace class being filled (owned by this) */
189 bt_trace_class
*trace_class
= nullptr;
191 /* CTF meta trace being filled (owned by this) */
192 struct ctf_trace_class
*ctf_tc
= nullptr;
194 /* Current declaration scope (top of the stack) (owned by this) */
195 struct ctx_decl_scope
*current_scope
= nullptr;
197 /* True if trace declaration is visited */
198 bool is_trace_visited
= false;
200 /* True if this is an LTTng trace */
201 bool is_lttng
= false;
203 /* Config passed by the user */
204 struct ctf_metadata_decoder_config decoder_config
;
210 struct ctf_visitor_generate_ir
;
213 * Creates a new declaration scope.
215 * @param par_scope Parent scope (NULL if creating a root scope)
216 * @returns New declaration scope, or NULL on error
218 static struct ctx_decl_scope
*ctx_decl_scope_create(struct ctf_visitor_generate_ir
*ctx
,
219 struct ctx_decl_scope
*par_scope
)
221 struct ctx_decl_scope
*scope
;
223 scope
= g_new(struct ctx_decl_scope
, 1);
225 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Failed to allocate one declaration scope.");
229 scope
->decl_map
= g_hash_table_new_full(g_direct_hash
, g_direct_equal
, NULL
,
230 (GDestroyNotify
) ctf_field_class_destroy
);
231 scope
->parent_scope
= par_scope
;
238 * Destroys a declaration scope.
240 * This function does not destroy the parent scope.
242 * @param scope Scope to destroy
244 static void ctx_decl_scope_destroy(struct ctx_decl_scope
*scope
)
250 g_hash_table_destroy(scope
->decl_map
);
258 * Returns the GQuark of a prefixed alias.
260 * @param prefix Prefix character
262 * @returns Associated GQuark, or 0 on error
264 static GQuark
get_prefixed_named_quark(char prefix
, const char *name
)
267 std::string prname
= std::string
{prefix
} + name
;
268 return g_quark_from_string(prname
.c_str());
272 * Looks up a prefixed class alias within a declaration scope.
274 * @param scope Declaration scope
275 * @param prefix Prefix character
276 * @param name Alias name
277 * @param levels Number of levels to dig into (-1 means infinite)
278 * @param copy True to return a copy
279 * @returns Declaration (owned by caller if \p copy is true),
280 * or NULL if not found
282 static struct ctf_field_class
*ctx_decl_scope_lookup_prefix_alias(struct ctx_decl_scope
*scope
,
283 char prefix
, const char *name
,
284 int levels
, bool copy
)
288 struct ctf_field_class
*decl
= NULL
;
289 struct ctx_decl_scope
*cur_scope
= scope
;
293 qname
= get_prefixed_named_quark(prefix
, name
);
302 while (cur_scope
&& cur_levels
< levels
) {
303 decl
= (ctf_field_class
*) g_hash_table_lookup(cur_scope
->decl_map
,
304 (gconstpointer
) GUINT_TO_POINTER(qname
));
306 /* Caller's reference */
308 decl
= ctf_field_class_copy(decl
);
315 cur_scope
= cur_scope
->parent_scope
;
324 * Looks up a class alias within a declaration scope.
326 * @param scope Declaration scope
327 * @param name Alias name
328 * @param levels Number of levels to dig into (-1 means infinite)
329 * @param copy True to return a copy
330 * @returns Declaration (owned by caller if \p copy is true),
331 * or NULL if not found
333 static struct ctf_field_class
*ctx_decl_scope_lookup_alias(struct ctx_decl_scope
*scope
,
334 const char *name
, int levels
, bool copy
)
336 return ctx_decl_scope_lookup_prefix_alias(scope
, _PREFIX_ALIAS
, name
, levels
, copy
);
340 * Looks up an enumeration within a declaration scope.
342 * @param scope Declaration scope
343 * @param name Enumeration name
344 * @param levels Number of levels to dig into (-1 means infinite)
345 * @param copy True to return a copy
346 * @returns Declaration (owned by caller if \p copy is true),
347 * or NULL if not found
349 static struct ctf_field_class_enum
*
350 ctx_decl_scope_lookup_enum(struct ctx_decl_scope
*scope
, const char *name
, int levels
, bool copy
)
352 return ctf_field_class_as_enum(
353 ctx_decl_scope_lookup_prefix_alias(scope
, _PREFIX_ENUM
, name
, levels
, copy
));
357 * Looks up a structure within a declaration scope.
359 * @param scope Declaration scope
360 * @param name Structure name
361 * @param levels Number of levels to dig into (-1 means infinite)
362 * @param copy True to return a copy
363 * @returns Declaration (owned by caller if \p copy is true),
364 * or NULL if not found
366 static struct ctf_field_class_struct
*
367 ctx_decl_scope_lookup_struct(struct ctx_decl_scope
*scope
, const char *name
, int levels
, bool copy
)
369 return ctf_field_class_as_struct(
370 ctx_decl_scope_lookup_prefix_alias(scope
, _PREFIX_STRUCT
, name
, levels
, copy
));
374 * Looks up a variant within a declaration scope.
376 * @param scope Declaration scope
377 * @param name Variant name
378 * @param levels Number of levels to dig into (-1 means infinite)
379 * @param copy True to return a copy
380 * @returns Declaration (owned by caller if \p copy is true),
381 * or NULL if not found
383 static struct ctf_field_class_variant
*
384 ctx_decl_scope_lookup_variant(struct ctx_decl_scope
*scope
, const char *name
, int levels
, bool copy
)
386 return ctf_field_class_as_variant(
387 ctx_decl_scope_lookup_prefix_alias(scope
, _PREFIX_VARIANT
, name
, levels
, copy
));
391 * Registers a prefixed class alias within a declaration scope.
393 * @param scope Declaration scope
394 * @param prefix Prefix character
395 * @param name Alias name (non-NULL)
396 * @param decl Field class to register (copied)
397 * @returns 0 if registration went okay, negative value otherwise
399 static int ctx_decl_scope_register_prefix_alias(struct ctx_decl_scope
*scope
, char prefix
,
400 const char *name
, struct ctf_field_class
*decl
)
408 qname
= get_prefixed_named_quark(prefix
, name
);
414 /* Make sure alias does not exist in local scope */
415 if (ctx_decl_scope_lookup_prefix_alias(scope
, prefix
, name
, 1, false)) {
420 decl
= ctf_field_class_copy(decl
);
422 g_hash_table_insert(scope
->decl_map
, GUINT_TO_POINTER(qname
), decl
);
429 * Registers a class alias within a declaration scope.
431 * @param scope Declaration scope
432 * @param name Alias name (non-NULL)
433 * @param decl Field class to register (copied)
434 * @returns 0 if registration went okay, negative value otherwise
436 static int ctx_decl_scope_register_alias(struct ctx_decl_scope
*scope
, const char *name
,
437 struct ctf_field_class
*decl
)
439 return ctx_decl_scope_register_prefix_alias(scope
, _PREFIX_ALIAS
, name
, decl
);
443 * Registers an enumeration declaration within a declaration scope.
445 * @param scope Declaration scope
446 * @param name Enumeration name (non-NULL)
447 * @param decl Enumeration field class to register (copied)
448 * @returns 0 if registration went okay, negative value otherwise
450 static int ctx_decl_scope_register_enum(struct ctx_decl_scope
*scope
, const char *name
,
451 struct ctf_field_class_enum
*decl
)
453 return ctx_decl_scope_register_prefix_alias(scope
, _PREFIX_ENUM
, name
, &decl
->base
.base
.base
);
457 * Registers a structure declaration within a declaration scope.
459 * @param scope Declaration scope
460 * @param name Structure name (non-NULL)
461 * @param decl Structure field class to register (copied)
462 * @returns 0 if registration went okay, negative value otherwise
464 static int ctx_decl_scope_register_struct(struct ctx_decl_scope
*scope
, const char *name
,
465 struct ctf_field_class_struct
*decl
)
467 return ctx_decl_scope_register_prefix_alias(scope
, _PREFIX_STRUCT
, name
, &decl
->base
);
471 * Registers a variant declaration within a declaration scope.
473 * @param scope Declaration scope
474 * @param name Variant name (non-NULL)
475 * @param decl Variant field class to register
476 * @returns 0 if registration went okay, negative value otherwise
478 static int ctx_decl_scope_register_variant(struct ctx_decl_scope
*scope
, const char *name
,
479 struct ctf_field_class_variant
*decl
)
481 return ctx_decl_scope_register_prefix_alias(scope
, _PREFIX_VARIANT
, name
, &decl
->base
);
485 * Destroys a visitor context.
487 * @param ctx Visitor context to destroy
489 static void ctx_destroy(struct ctf_visitor_generate_ir
*ctx
)
491 struct ctx_decl_scope
*scope
;
497 scope
= ctx
->current_scope
;
500 * Destroy all scopes, from current one to the root scope.
503 struct ctx_decl_scope
*parent_scope
= scope
->parent_scope
;
505 ctx_decl_scope_destroy(scope
);
506 scope
= parent_scope
;
509 bt_trace_class_put_ref(ctx
->trace_class
);
512 ctf_trace_class_destroy(ctx
->ctf_tc
);
522 * Creates a new visitor context.
524 * @param trace Associated trace
525 * @returns New visitor context, or NULL on error
527 static struct ctf_visitor_generate_ir
*
528 ctx_create(const struct ctf_metadata_decoder_config
*decoder_config
)
530 BT_ASSERT(decoder_config
);
532 ctf_visitor_generate_ir
*ctx
= new ctf_visitor_generate_ir
;
533 ctx
->log_cfg
.log_level
= decoder_config
->log_level
;
534 ctx
->log_cfg
.self_comp
= decoder_config
->self_comp
;
535 ctx
->log_cfg
.self_comp_class
= decoder_config
->self_comp_class
;
537 if (decoder_config
->self_comp
) {
538 ctx
->trace_class
= bt_trace_class_create(decoder_config
->self_comp
);
539 if (!ctx
->trace_class
) {
540 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Cannot create empty trace class.");
545 ctx
->ctf_tc
= ctf_trace_class_create();
547 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Cannot create CTF trace class.");
551 /* Root declaration scope */
552 ctx
->current_scope
= ctx_decl_scope_create(ctx
, NULL
);
553 if (!ctx
->current_scope
) {
554 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Cannot create declaration scope.");
558 ctx
->decoder_config
= *decoder_config
;
570 * Pushes a new declaration scope on top of a visitor context's
571 * declaration scope stack.
573 * @param ctx Visitor context
574 * @returns 0 on success, or a negative value on error
576 static int ctx_push_scope(struct ctf_visitor_generate_ir
*ctx
)
579 struct ctx_decl_scope
*new_scope
;
582 new_scope
= ctx_decl_scope_create(ctx
, ctx
->current_scope
);
584 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Cannot create declaration scope.");
589 ctx
->current_scope
= new_scope
;
595 static void ctx_pop_scope(struct ctf_visitor_generate_ir
*ctx
)
597 struct ctx_decl_scope
*parent_scope
= NULL
;
601 if (!ctx
->current_scope
) {
605 parent_scope
= ctx
->current_scope
->parent_scope
;
606 ctx_decl_scope_destroy(ctx
->current_scope
);
607 ctx
->current_scope
= parent_scope
;
613 static int visit_field_class_specifier_list(struct ctf_visitor_generate_ir
*ctx
,
614 struct ctf_node
*ts_list
,
615 struct ctf_field_class
**decl
);
617 static int is_unary_string(struct bt_list_head
*head
)
620 struct ctf_node
*node
;
622 bt_list_for_each_entry (node
, head
, siblings
) {
623 if (node
->type
!= NODE_UNARY_EXPRESSION
) {
627 if (node
->u
.unary_expression
.type
!= UNARY_STRING
) {
635 static const char *get_map_clock_name_value(struct bt_list_head
*head
)
638 struct ctf_node
*node
;
639 const char *name
= NULL
;
641 bt_list_for_each_entry (node
, head
, siblings
) {
643 int uexpr_type
= node
->u
.unary_expression
.type
;
644 int uexpr_link
= node
->u
.unary_expression
.link
;
645 int cond
= node
->type
!= NODE_UNARY_EXPRESSION
|| uexpr_type
!= UNARY_STRING
||
646 !((uexpr_link
!= UNARY_LINK_UNKNOWN
) ^ (i
== 0));
651 /* Needs to be chained with . */
652 switch (node
->u
.unary_expression
.link
) {
655 case UNARY_ARROWLINK
:
656 case UNARY_DOTDOTDOT
:
662 src_string
= node
->u
.unary_expression
.u
.string
;
666 if (strcmp("clock", src_string
)) {
674 if (strcmp("value", src_string
)) {
679 /* Extra identifier, unknown */
692 static int is_unary_unsigned(struct bt_list_head
*head
)
695 struct ctf_node
*node
;
697 bt_list_for_each_entry (node
, head
, siblings
) {
698 if (node
->type
!= NODE_UNARY_EXPRESSION
) {
702 if (node
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
710 static int get_unary_unsigned(struct ctf_visitor_generate_ir
*ctx
, struct bt_list_head
*head
,
715 struct ctf_node
*node
;
719 if (bt_list_empty(head
)) {
724 bt_list_for_each_entry (node
, head
, siblings
) {
725 int uexpr_type
= node
->u
.unary_expression
.type
;
726 int uexpr_link
= node
->u
.unary_expression
.link
;
727 int cond
= node
->type
!= NODE_UNARY_EXPRESSION
|| uexpr_type
!= UNARY_UNSIGNED_CONSTANT
||
728 uexpr_link
!= UNARY_LINK_UNKNOWN
|| i
!= 0;
730 _BT_COMP_LOGE_APPEND_CAUSE_NODE(node
, "Invalid constant unsigned integer.");
735 *value
= node
->u
.unary_expression
.u
.unsigned_constant
;
743 static int is_unary_signed(struct bt_list_head
*head
)
746 struct ctf_node
*node
;
748 bt_list_for_each_entry (node
, head
, siblings
) {
749 if (node
->type
!= NODE_UNARY_EXPRESSION
) {
753 if (node
->u
.unary_expression
.type
!= UNARY_SIGNED_CONSTANT
) {
761 static int get_unary_signed(struct bt_list_head
*head
, int64_t *value
)
765 struct ctf_node
*node
;
767 bt_list_for_each_entry (node
, head
, siblings
) {
768 int uexpr_type
= node
->u
.unary_expression
.type
;
769 int uexpr_link
= node
->u
.unary_expression
.link
;
770 int cond
= node
->type
!= NODE_UNARY_EXPRESSION
||
771 (uexpr_type
!= UNARY_UNSIGNED_CONSTANT
&& uexpr_type
!= UNARY_SIGNED_CONSTANT
) ||
772 uexpr_link
!= UNARY_LINK_UNKNOWN
|| i
!= 0;
778 switch (uexpr_type
) {
779 case UNARY_UNSIGNED_CONSTANT
:
780 *value
= (int64_t) node
->u
.unary_expression
.u
.unsigned_constant
;
782 case UNARY_SIGNED_CONSTANT
:
783 *value
= node
->u
.unary_expression
.u
.signed_constant
;
797 static int get_unary_uuid(struct ctf_visitor_generate_ir
*ctx
, struct bt_list_head
*head
,
800 return ctf_ast_get_unary_uuid(head
, uuid
, ctx
->log_cfg
.log_level
, ctx
->log_cfg
.self_comp
);
803 static int get_boolean(struct ctf_visitor_generate_ir
*ctx
, struct ctf_node
*unary_expr
)
807 if (unary_expr
->type
!= NODE_UNARY_EXPRESSION
) {
808 _BT_COMP_LOGE_APPEND_CAUSE_NODE(unary_expr
, "Expecting unary expression: node-type=%d",
814 switch (unary_expr
->u
.unary_expression
.type
) {
815 case UNARY_UNSIGNED_CONSTANT
:
816 ret
= (unary_expr
->u
.unary_expression
.u
.unsigned_constant
!= 0);
818 case UNARY_SIGNED_CONSTANT
:
819 ret
= (unary_expr
->u
.unary_expression
.u
.signed_constant
!= 0);
823 const char *str
= unary_expr
->u
.unary_expression
.u
.string
;
825 if (strcmp(str
, "true") == 0 || strcmp(str
, "TRUE") == 0) {
827 } else if (strcmp(str
, "false") == 0 || strcmp(str
, "FALSE") == 0) {
830 _BT_COMP_LOGE_APPEND_CAUSE_NODE(unary_expr
, "Unexpected boolean value: value=\"%s\"",
838 _BT_COMP_LOGE_APPEND_CAUSE_NODE(unary_expr
,
839 "Unexpected unary expression type: node-type=%d",
840 unary_expr
->u
.unary_expression
.type
);
849 static enum ctf_byte_order
byte_order_from_unary_expr(struct ctf_visitor_generate_ir
*ctx
,
850 struct ctf_node
*unary_expr
)
853 enum ctf_byte_order bo
= CTF_BYTE_ORDER_UNKNOWN
;
855 if (unary_expr
->u
.unary_expression
.type
!= UNARY_STRING
) {
856 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
857 unary_expr
, "\"byte_order\" attribute: expecting `be`, `le`, `network`, or `native`.");
861 str
= unary_expr
->u
.unary_expression
.u
.string
;
863 if (strcmp(str
, "be") == 0 || strcmp(str
, "network") == 0) {
864 bo
= CTF_BYTE_ORDER_BIG
;
865 } else if (strcmp(str
, "le") == 0) {
866 bo
= CTF_BYTE_ORDER_LITTLE
;
867 } else if (strcmp(str
, "native") == 0) {
868 bo
= CTF_BYTE_ORDER_DEFAULT
;
870 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
872 "Unexpected \"byte_order\" attribute value: "
873 "expecting `be`, `le`, `network`, or `native`: value=\"%s\"",
882 static enum ctf_byte_order
get_real_byte_order(struct ctf_visitor_generate_ir
*ctx
,
883 struct ctf_node
*uexpr
)
885 enum ctf_byte_order bo
= byte_order_from_unary_expr(ctx
, uexpr
);
887 if (bo
== CTF_BYTE_ORDER_DEFAULT
) {
888 bo
= ctx
->ctf_tc
->default_byte_order
;
894 static int is_align_valid(uint64_t align
)
896 return (align
!= 0) && !(align
& (align
- UINT64_C(1)));
899 static int get_class_specifier_name(struct ctf_visitor_generate_ir
*ctx
,
900 struct ctf_node
*cls_specifier
, GString
*str
)
904 if (cls_specifier
->type
!= NODE_TYPE_SPECIFIER
) {
905 _BT_COMP_LOGE_APPEND_CAUSE_NODE(cls_specifier
, "Unexpected node type: node-type=%d",
906 cls_specifier
->type
);
911 switch (cls_specifier
->u
.field_class_specifier
.type
) {
913 g_string_append(str
, "void");
916 g_string_append(str
, "char");
919 g_string_append(str
, "short");
922 g_string_append(str
, "int");
925 g_string_append(str
, "long");
928 g_string_append(str
, "float");
930 case TYPESPEC_DOUBLE
:
931 g_string_append(str
, "double");
933 case TYPESPEC_SIGNED
:
934 g_string_append(str
, "signed");
936 case TYPESPEC_UNSIGNED
:
937 g_string_append(str
, "unsigned");
940 g_string_append(str
, "bool");
942 case TYPESPEC_COMPLEX
:
943 g_string_append(str
, "_Complex");
945 case TYPESPEC_IMAGINARY
:
946 g_string_append(str
, "_Imaginary");
949 g_string_append(str
, "const");
951 case TYPESPEC_ID_TYPE
:
952 if (cls_specifier
->u
.field_class_specifier
.id_type
) {
953 g_string_append(str
, cls_specifier
->u
.field_class_specifier
.id_type
);
956 case TYPESPEC_STRUCT
:
958 struct ctf_node
*node
= cls_specifier
->u
.field_class_specifier
.node
;
960 if (!node
->u
._struct
.name
) {
961 _BT_COMP_LOGE_APPEND_CAUSE_NODE(node
, "Unexpected empty structure field class name.");
966 g_string_append(str
, "struct ");
967 g_string_append(str
, node
->u
._struct
.name
);
970 case TYPESPEC_VARIANT
:
972 struct ctf_node
*node
= cls_specifier
->u
.field_class_specifier
.node
;
974 if (!node
->u
.variant
.name
) {
975 _BT_COMP_LOGE_APPEND_CAUSE_NODE(node
, "Unexpected empty variant field class name.");
980 g_string_append(str
, "variant ");
981 g_string_append(str
, node
->u
.variant
.name
);
986 struct ctf_node
*node
= cls_specifier
->u
.field_class_specifier
.node
;
988 if (!node
->u
._enum
.enum_id
) {
989 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
990 node
, "Unexpected empty enumeration field class (`enum`) name.");
995 g_string_append(str
, "enum ");
996 g_string_append(str
, node
->u
._enum
.enum_id
);
999 case TYPESPEC_FLOATING_POINT
:
1000 case TYPESPEC_INTEGER
:
1001 case TYPESPEC_STRING
:
1003 _BT_COMP_LOGE_APPEND_CAUSE_NODE(cls_specifier
->u
.field_class_specifier
.node
,
1004 "Unexpected field class specifier type: %d",
1005 cls_specifier
->u
.field_class_specifier
.type
);
1014 static int get_class_specifier_list_name(struct ctf_visitor_generate_ir
*ctx
,
1015 struct ctf_node
*cls_specifier_list
, GString
*str
)
1018 struct ctf_node
*iter
;
1019 int alias_item_nr
= 0;
1020 struct bt_list_head
*head
= &cls_specifier_list
->u
.field_class_specifier_list
.head
;
1022 bt_list_for_each_entry (iter
, head
, siblings
) {
1023 if (alias_item_nr
!= 0) {
1024 g_string_append(str
, " ");
1028 ret
= get_class_specifier_name(ctx
, iter
, str
);
1038 static GQuark
create_class_alias_identifier(struct ctf_visitor_generate_ir
*ctx
,
1039 struct ctf_node
*cls_specifier_list
,
1040 struct ctf_node
*node_field_class_declarator
)
1046 struct ctf_node
*iter
;
1047 struct bt_list_head
*pointers
= &node_field_class_declarator
->u
.field_class_declarator
.pointers
;
1049 str
= g_string_new("");
1050 ret
= get_class_specifier_list_name(ctx
, cls_specifier_list
, str
);
1052 g_string_free(str
, TRUE
);
1056 bt_list_for_each_entry (iter
, pointers
, siblings
) {
1057 g_string_append(str
, " *");
1059 if (iter
->u
.pointer
.const_qualifier
) {
1060 g_string_append(str
, " const");
1064 str_c
= g_string_free(str
, FALSE
);
1065 qalias
= g_quark_from_string(str_c
);
1072 static int visit_field_class_declarator(struct ctf_visitor_generate_ir
*ctx
,
1073 struct ctf_node
*cls_specifier_list
, GQuark
*field_name
,
1074 struct ctf_node
*node_field_class_declarator
,
1075 struct ctf_field_class
**field_decl
,
1076 struct ctf_field_class
*nested_decl
)
1079 * During this whole function, nested_decl is always OURS,
1080 * whereas field_decl is an output which we create, but
1081 * belongs to the caller (it is moved).
1086 /* Validate field class declarator node */
1087 if (node_field_class_declarator
) {
1088 if (node_field_class_declarator
->u
.field_class_declarator
.type
== TYPEDEC_UNKNOWN
) {
1089 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
1090 node_field_class_declarator
, "Unexpected field class declarator type: type=%d",
1091 node_field_class_declarator
->u
.field_class_declarator
.type
);
1096 /* TODO: GCC bitfields not supported yet */
1097 if (node_field_class_declarator
->u
.field_class_declarator
.bitfield_len
!= NULL
) {
1098 _BT_COMP_LOGE_APPEND_CAUSE_NODE(node_field_class_declarator
,
1099 "GCC bitfields are not supported as of this version.");
1105 /* Find the right nested declaration if not provided */
1107 if (node_field_class_declarator
&&
1108 !bt_list_empty(&node_field_class_declarator
->u
.field_class_declarator
.pointers
)) {
1112 * If we have a pointer declarator, it HAS to
1113 * be present in the field class aliases (else
1117 create_class_alias_identifier(ctx
, cls_specifier_list
, node_field_class_declarator
);
1118 nested_decl
= ctx_decl_scope_lookup_alias(ctx
->current_scope
, g_quark_to_string(qalias
),
1121 _BT_COMP_LOGE_APPEND_CAUSE_NODE(node_field_class_declarator
,
1122 "Cannot find class alias: name=\"%s\"",
1123 g_quark_to_string(qalias
));
1128 if (nested_decl
->type
== CTF_FIELD_CLASS_TYPE_INT
) {
1129 /* Pointer: force integer's base to 16 */
1130 struct ctf_field_class_int
*int_fc
= ctf_field_class_as_int(nested_decl
);
1132 int_fc
->disp_base
= BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_HEXADECIMAL
;
1135 ret
= visit_field_class_specifier_list(ctx
, cls_specifier_list
, &nested_decl
);
1137 BT_ASSERT(!nested_decl
);
1143 BT_ASSERT(nested_decl
);
1145 if (!node_field_class_declarator
) {
1146 *field_decl
= nested_decl
;
1151 if (node_field_class_declarator
->u
.field_class_declarator
.type
== TYPEDEC_ID
) {
1152 if (node_field_class_declarator
->u
.field_class_declarator
.u
.id
) {
1153 const char *id
= node_field_class_declarator
->u
.field_class_declarator
.u
.id
;
1155 *field_name
= g_quark_from_string(id
);
1160 *field_decl
= nested_decl
;
1164 struct ctf_node
*first
;
1165 struct ctf_field_class
*decl
= NULL
;
1166 struct ctf_field_class
*outer_field_decl
= NULL
;
1167 struct bt_list_head
*length
=
1168 &node_field_class_declarator
->u
.field_class_declarator
.u
.nested
.length
;
1170 /* Create array/sequence, pass nested_decl as child */
1171 if (bt_list_empty(length
)) {
1172 _BT_COMP_LOGE_APPEND_CAUSE_NODE(node_field_class_declarator
,
1173 "Expecting length field reference or value.");
1178 first
= _BT_LIST_FIRST_ENTRY(length
, struct ctf_node
, siblings
);
1179 if (first
->type
!= NODE_UNARY_EXPRESSION
) {
1180 _BT_COMP_LOGE_APPEND_CAUSE_NODE(first
, "Unexpected node type: node-type=%d",
1186 switch (first
->u
.unary_expression
.type
) {
1187 case UNARY_UNSIGNED_CONSTANT
:
1189 struct ctf_field_class_array
*array_decl
= NULL
;
1191 array_decl
= ctf_field_class_array_create();
1192 BT_ASSERT(array_decl
);
1193 array_decl
->length
= first
->u
.unary_expression
.u
.unsigned_constant
;
1194 array_decl
->base
.elem_fc
= nested_decl
;
1196 decl
= &array_decl
->base
.base
;
1201 /* Lookup unsigned integer definition, create seq. */
1202 struct ctf_field_class_sequence
*seq_decl
= NULL
;
1203 char *length_name
= ctf_ast_concatenate_unary_strings(length
);
1206 _BT_COMP_LOGE_APPEND_CAUSE_NODE(node_field_class_declarator
,
1207 "Cannot concatenate unary strings.");
1212 if (strncmp(length_name
, "env.", 4) == 0) {
1213 /* This is, in fact, an array */
1214 const char *env_entry_name
= &length_name
[4];
1215 struct ctf_trace_class_env_entry
*env_entry
=
1216 ctf_trace_class_borrow_env_entry_by_name(ctx
->ctf_tc
, env_entry_name
);
1217 struct ctf_field_class_array
*array_decl
;
1220 _BT_COMP_LOGE_APPEND_CAUSE_NODE(node_field_class_declarator
,
1221 "Cannot find environment entry: "
1228 if (env_entry
->type
!= CTF_TRACE_CLASS_ENV_ENTRY_TYPE_INT
) {
1229 _BT_COMP_LOGE_APPEND_CAUSE_NODE(node_field_class_declarator
,
1230 "Wrong environment entry type "
1231 "(expecting integer): "
1238 if (env_entry
->value
.i
< 0) {
1239 _BT_COMP_LOGE_APPEND_CAUSE_NODE(node_field_class_declarator
,
1240 "Invalid, negative array length: "
1241 "env-entry-name=\"%s\", "
1243 env_entry_name
, env_entry
->value
.i
);
1248 array_decl
= ctf_field_class_array_create();
1249 BT_ASSERT(array_decl
);
1250 array_decl
->length
= (uint64_t) env_entry
->value
.i
;
1251 array_decl
->base
.elem_fc
= nested_decl
;
1253 decl
= &array_decl
->base
.base
;
1255 seq_decl
= ctf_field_class_sequence_create();
1256 BT_ASSERT(seq_decl
);
1257 seq_decl
->base
.elem_fc
= nested_decl
;
1259 g_string_assign(seq_decl
->length_ref
, length_name
);
1260 decl
= &seq_decl
->base
.base
;
1263 g_free(length_name
);
1271 BT_ASSERT(!nested_decl
);
1273 BT_ASSERT(!*field_decl
);
1276 * At this point, we found the next nested declaration.
1277 * We currently own this (and lost the ownership of
1278 * nested_decl in the meantime). Pass this next
1279 * nested declaration as the content of the outer
1280 * container, MOVING its ownership.
1282 ret
= visit_field_class_declarator(
1283 ctx
, cls_specifier_list
, field_name
,
1284 node_field_class_declarator
->u
.field_class_declarator
.u
.nested
.field_class_declarator
,
1285 &outer_field_decl
, decl
);
1288 BT_ASSERT(!outer_field_decl
);
1293 BT_ASSERT(outer_field_decl
);
1294 *field_decl
= outer_field_decl
;
1295 outer_field_decl
= NULL
;
1298 BT_ASSERT(*field_decl
);
1302 ctf_field_class_destroy(*field_decl
);
1310 ctf_field_class_destroy(nested_decl
);
1315 static int visit_struct_decl_field(struct ctf_visitor_generate_ir
*ctx
,
1316 struct ctf_field_class_struct
*struct_decl
,
1317 struct ctf_node
*cls_specifier_list
,
1318 struct bt_list_head
*field_class_declarators
)
1321 struct ctf_node
*iter
;
1322 struct ctf_field_class
*field_decl
= NULL
;
1324 bt_list_for_each_entry (iter
, field_class_declarators
, siblings
) {
1327 const char *field_name
;
1329 ret
= visit_field_class_declarator(ctx
, cls_specifier_list
, &qfield_name
, iter
, &field_decl
,
1332 BT_ASSERT(!field_decl
);
1333 _BT_COMP_LOGE_APPEND_CAUSE_NODE(cls_specifier_list
,
1334 "Cannot visit field class declarator: ret=%d", ret
);
1338 BT_ASSERT(field_decl
);
1339 field_name
= g_quark_to_string(qfield_name
);
1341 /* Check if field with same name already exists */
1342 if (ctf_field_class_struct_borrow_member_by_name(struct_decl
, field_name
)) {
1343 _BT_COMP_LOGE_APPEND_CAUSE_NODE(cls_specifier_list
,
1344 "Duplicate field in structure field class: "
1345 "field-name=\"%s\"",
1351 /* Add field to structure */
1352 ctf_field_class_struct_append_member(struct_decl
, field_name
, field_decl
);
1359 ctf_field_class_destroy(field_decl
);
1364 static int visit_variant_decl_field(struct ctf_visitor_generate_ir
*ctx
,
1365 struct ctf_field_class_variant
*variant_decl
,
1366 struct ctf_node
*cls_specifier_list
,
1367 struct bt_list_head
*field_class_declarators
)
1370 struct ctf_node
*iter
;
1371 struct ctf_field_class
*field_decl
= NULL
;
1373 bt_list_for_each_entry (iter
, field_class_declarators
, siblings
) {
1376 const char *field_name
;
1378 ret
= visit_field_class_declarator(ctx
, cls_specifier_list
, &qfield_name
, iter
, &field_decl
,
1381 BT_ASSERT(!field_decl
);
1382 _BT_COMP_LOGE_APPEND_CAUSE_NODE(cls_specifier_list
,
1383 "Cannot visit field class declarator: ret=%d", ret
);
1387 BT_ASSERT(field_decl
);
1388 field_name
= g_quark_to_string(qfield_name
);
1390 /* Check if field with same name already exists */
1391 if (ctf_field_class_variant_borrow_option_by_name(variant_decl
, field_name
)) {
1392 _BT_COMP_LOGE_APPEND_CAUSE_NODE(cls_specifier_list
,
1393 "Duplicate field in variant field class: "
1394 "field-name=\"%s\"",
1400 /* Add field to structure */
1401 ctf_field_class_variant_append_option(variant_decl
, field_name
, field_decl
);
1408 ctf_field_class_destroy(field_decl
);
1413 static int visit_field_class_def(struct ctf_visitor_generate_ir
*ctx
,
1414 struct ctf_node
*cls_specifier_list
,
1415 struct bt_list_head
*field_class_declarators
)
1419 struct ctf_node
*iter
;
1420 struct ctf_field_class
*class_decl
= NULL
;
1422 bt_list_for_each_entry (iter
, field_class_declarators
, siblings
) {
1423 ret
= visit_field_class_declarator(ctx
, cls_specifier_list
, &qidentifier
, iter
, &class_decl
,
1426 _BT_COMP_LOGE_APPEND_CAUSE_NODE(iter
, "Cannot visit field class declarator: ret=%d",
1432 /* Do not allow field class def and alias of untagged variants */
1433 if (class_decl
->type
== CTF_FIELD_CLASS_TYPE_VARIANT
) {
1434 struct ctf_field_class_variant
*var_fc
= ctf_field_class_as_variant(class_decl
);
1436 if (var_fc
->tag_path
.path
->len
== 0) {
1437 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
1438 iter
, "Type definition of untagged variant field class is not allowed.");
1444 ret
= ctx_decl_scope_register_alias(ctx
->current_scope
, g_quark_to_string(qidentifier
),
1447 _BT_COMP_LOGE_APPEND_CAUSE_NODE(iter
, "Cannot register field class alias: name=\"%s\"",
1448 g_quark_to_string(qidentifier
));
1454 ctf_field_class_destroy(class_decl
);
1459 static int visit_field_class_alias(struct ctf_visitor_generate_ir
*ctx
, struct ctf_node
*target
,
1460 struct ctf_node
*alias
)
1464 struct ctf_node
*node
;
1465 GQuark qdummy_field_name
;
1466 struct ctf_field_class
*class_decl
= NULL
;
1468 /* Create target field class */
1469 if (bt_list_empty(&target
->u
.field_class_alias_target
.field_class_declarators
)) {
1472 node
= _BT_LIST_FIRST_ENTRY(&target
->u
.field_class_alias_target
.field_class_declarators
,
1473 struct ctf_node
, siblings
);
1476 ret
= visit_field_class_declarator(
1477 ctx
, target
->u
.field_class_alias_target
.field_class_specifier_list
, &qdummy_field_name
,
1478 node
, &class_decl
, NULL
);
1480 BT_ASSERT(!class_decl
);
1481 _BT_COMP_LOGE_APPEND_CAUSE_NODE(node
, "Cannot visit field class declarator: ret=%d", ret
);
1485 /* Do not allow field class def and alias of untagged variants */
1486 if (class_decl
->type
== CTF_FIELD_CLASS_TYPE_VARIANT
) {
1487 struct ctf_field_class_variant
*var_fc
= ctf_field_class_as_variant(class_decl
);
1489 if (var_fc
->tag_path
.path
->len
== 0) {
1490 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
1491 target
, "Type definition of untagged variant field class is not allowed.");
1498 * The semantic validator does not check whether the target is
1499 * abstract or not (if it has an identifier). Check it here.
1501 if (qdummy_field_name
!= 0) {
1502 _BT_COMP_LOGE_APPEND_CAUSE_NODE(target
, "Expecting empty identifier: id=\"%s\"",
1503 g_quark_to_string(qdummy_field_name
));
1508 /* Create alias identifier */
1509 node
= _BT_LIST_FIRST_ENTRY(&alias
->u
.field_class_alias_name
.field_class_declarators
,
1510 struct ctf_node
, siblings
);
1511 qalias
= create_class_alias_identifier(
1512 ctx
, alias
->u
.field_class_alias_name
.field_class_specifier_list
, node
);
1513 ret
= ctx_decl_scope_register_alias(ctx
->current_scope
, g_quark_to_string(qalias
), class_decl
);
1515 _BT_COMP_LOGE_APPEND_CAUSE_NODE(node
, "Cannot register class alias: name=\"%s\"",
1516 g_quark_to_string(qalias
));
1521 ctf_field_class_destroy(class_decl
);
1526 static int visit_struct_decl_entry(struct ctf_visitor_generate_ir
*ctx
, struct ctf_node
*entry_node
,
1527 struct ctf_field_class_struct
*struct_decl
)
1531 switch (entry_node
->type
) {
1533 ret
= visit_field_class_def(ctx
, entry_node
->u
.field_class_def
.field_class_specifier_list
,
1534 &entry_node
->u
.field_class_def
.field_class_declarators
);
1536 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
1537 entry_node
, "Cannot add field class found in structure field class: ret=%d", ret
);
1541 case NODE_TYPEALIAS
:
1542 ret
= visit_field_class_alias(ctx
, entry_node
->u
.field_class_alias
.target
,
1543 entry_node
->u
.field_class_alias
.alias
);
1545 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
1546 entry_node
, "Cannot add field class alias found in structure field class: ret=%d",
1551 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
1553 ret
= visit_struct_decl_field(
1555 entry_node
->u
.struct_or_variant_declaration
.field_class_specifier_list
,
1556 &entry_node
->u
.struct_or_variant_declaration
.field_class_declarators
);
1562 _BT_COMP_LOGE_APPEND_CAUSE_NODE(entry_node
, "Unexpected node type: node-type=%d",
1572 static int visit_variant_decl_entry(struct ctf_visitor_generate_ir
*ctx
,
1573 struct ctf_node
*entry_node
,
1574 struct ctf_field_class_variant
*variant_decl
)
1578 switch (entry_node
->type
) {
1580 ret
= visit_field_class_def(ctx
, entry_node
->u
.field_class_def
.field_class_specifier_list
,
1581 &entry_node
->u
.field_class_def
.field_class_declarators
);
1583 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
1584 entry_node
, "Cannot add field class found in variant field class: ret=%d", ret
);
1588 case NODE_TYPEALIAS
:
1589 ret
= visit_field_class_alias(ctx
, entry_node
->u
.field_class_alias
.target
,
1590 entry_node
->u
.field_class_alias
.alias
);
1592 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
1593 entry_node
, "Cannot add field class alias found in variant field class: ret=%d",
1598 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
1600 ret
= visit_variant_decl_field(
1602 entry_node
->u
.struct_or_variant_declaration
.field_class_specifier_list
,
1603 &entry_node
->u
.struct_or_variant_declaration
.field_class_declarators
);
1609 _BT_COMP_LOGE_APPEND_CAUSE_NODE(entry_node
, "Unexpected node type: node-type=%d",
1619 static int visit_struct_decl(struct ctf_visitor_generate_ir
*ctx
, const char *name
,
1620 struct bt_list_head
*decl_list
, int has_body
,
1621 struct bt_list_head
*min_align
,
1622 struct ctf_field_class_struct
**struct_decl
)
1626 BT_ASSERT(struct_decl
);
1627 *struct_decl
= NULL
;
1629 /* For named struct (without body), lookup in declaration scope */
1632 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(
1633 "Bodyless structure field class: missing name.");
1638 *struct_decl
= ctx_decl_scope_lookup_struct(ctx
->current_scope
, name
, -1, true);
1639 if (!*struct_decl
) {
1640 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(
1641 "Cannot find structure field class: name=\"struct %s\"", name
);
1646 struct ctf_node
*entry_node
;
1647 uint64_t min_align_value
= 0;
1650 if (ctx_decl_scope_lookup_struct(ctx
->current_scope
, name
, 1, false)) {
1651 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(
1652 "Structure field class already declared in local scope: "
1653 "name=\"struct %s\"",
1660 if (!bt_list_empty(min_align
)) {
1661 ret
= get_unary_unsigned(ctx
, min_align
, &min_align_value
);
1663 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(
1664 "Unexpected unary expression for structure field class's `align` attribute: "
1671 *struct_decl
= ctf_field_class_struct_create();
1672 BT_ASSERT(*struct_decl
);
1674 if (min_align_value
!= 0) {
1675 (*struct_decl
)->base
.alignment
= min_align_value
;
1678 _TRY_PUSH_SCOPE_OR_GOTO_ERROR();
1680 bt_list_for_each_entry (entry_node
, decl_list
, siblings
) {
1681 ret
= visit_struct_decl_entry(ctx
, entry_node
, *struct_decl
);
1683 _BT_COMP_LOGE_APPEND_CAUSE_NODE(entry_node
,
1684 "Cannot visit structure field class entry: "
1695 ret
= ctx_decl_scope_register_struct(ctx
->current_scope
, name
, *struct_decl
);
1697 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(
1698 "Cannot register structure field class in declaration scope: "
1699 "name=\"struct %s\", ret=%d",
1709 ctf_field_class_destroy(&(*struct_decl
)->base
);
1710 *struct_decl
= NULL
;
1714 static int visit_variant_decl(struct ctf_visitor_generate_ir
*ctx
, const char *name
,
1715 const char *tag
, struct bt_list_head
*decl_list
, int has_body
,
1716 struct ctf_field_class_variant
**variant_decl
)
1719 struct ctf_field_class_variant
*untagged_variant_decl
= NULL
;
1721 BT_ASSERT(variant_decl
);
1722 *variant_decl
= NULL
;
1724 /* For named variant (without body), lookup in declaration scope */
1727 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Bodyless variant field class: missing name.");
1732 untagged_variant_decl
= ctx_decl_scope_lookup_variant(ctx
->current_scope
, name
, -1, true);
1733 if (!untagged_variant_decl
) {
1734 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(
1735 "Cannot find variant field class: name=\"variant %s\"", name
);
1740 struct ctf_node
*entry_node
;
1743 if (ctx_decl_scope_lookup_variant(ctx
->current_scope
, name
, 1, false)) {
1744 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(
1745 "Variant field class already declared in local scope: "
1746 "name=\"variant %s\"",
1753 untagged_variant_decl
= ctf_field_class_variant_create();
1754 BT_ASSERT(untagged_variant_decl
);
1755 _TRY_PUSH_SCOPE_OR_GOTO_ERROR();
1757 bt_list_for_each_entry (entry_node
, decl_list
, siblings
) {
1758 ret
= visit_variant_decl_entry(ctx
, entry_node
, untagged_variant_decl
);
1760 _BT_COMP_LOGE_APPEND_CAUSE_NODE(entry_node
,
1761 "Cannot visit variant field class entry: "
1772 ret
= ctx_decl_scope_register_variant(ctx
->current_scope
, name
, untagged_variant_decl
);
1774 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(
1775 "Cannot register variant field class in declaration scope: "
1776 "name=\"variant %s\", ret=%d",
1784 * If tagged, create tagged variant and return; otherwise
1785 * return untagged variant.
1788 *variant_decl
= untagged_variant_decl
;
1789 untagged_variant_decl
= NULL
;
1792 * At this point, we have a fresh untagged variant; nobody
1793 * else owns it. Set its tag now.
1795 g_string_assign(untagged_variant_decl
->tag_ref
, tag
);
1796 *variant_decl
= untagged_variant_decl
;
1797 untagged_variant_decl
= NULL
;
1800 BT_ASSERT(!untagged_variant_decl
);
1801 BT_ASSERT(*variant_decl
);
1805 ctf_field_class_destroy(&untagged_variant_decl
->base
);
1806 untagged_variant_decl
= NULL
;
1807 ctf_field_class_destroy(&(*variant_decl
)->base
);
1808 *variant_decl
= NULL
;
1822 static int visit_enum_decl_entry(struct ctf_visitor_generate_ir
*ctx
, struct ctf_node
*enumerator
,
1823 struct ctf_field_class_enum
*enum_decl
, struct uori
*last
)
1827 struct ctf_node
*iter
;
1828 struct uori start
= {
1842 const char *label
= enumerator
->u
.enumerator
.id
;
1843 struct bt_list_head
*values
= &enumerator
->u
.enumerator
.values
;
1845 bt_list_for_each_entry (iter
, values
, siblings
) {
1846 struct uori
*target
;
1848 if (iter
->type
!= NODE_UNARY_EXPRESSION
) {
1849 _BT_COMP_LOGE_APPEND_CAUSE_NODE(iter
,
1850 "Wrong expression for enumeration field class label: "
1851 "node-type=%d, label=\"%s\"",
1863 switch (iter
->u
.unary_expression
.type
) {
1864 case UNARY_SIGNED_CONSTANT
:
1865 target
->is_signed
= true;
1866 target
->value
.i
= iter
->u
.unary_expression
.u
.signed_constant
;
1868 case UNARY_UNSIGNED_CONSTANT
:
1869 target
->is_signed
= false;
1870 target
->value
.u
= iter
->u
.unary_expression
.u
.unsigned_constant
;
1873 _BT_COMP_LOGE_APPEND_CAUSE_NODE(iter
,
1874 "Invalid enumeration field class entry: "
1875 "expecting constant signed or unsigned integer: "
1876 "node-type=%d, label=\"%s\"",
1877 iter
->u
.unary_expression
.type
, label
);
1883 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
1884 iter
, "Invalid enumeration field class entry: label=\"%s\"", label
);
1900 if (end
.is_signed
) {
1901 last
->value
.i
= end
.value
.i
+ 1;
1903 last
->value
.u
= end
.value
.u
+ 1;
1906 ctf_field_class_enum_map_range(enum_decl
, label
, start
.value
.u
, end
.value
.u
);
1913 static int visit_enum_decl(struct ctf_visitor_generate_ir
*ctx
, const char *name
,
1914 struct ctf_node
*container_cls
, struct bt_list_head
*enumerator_list
,
1915 int has_body
, struct ctf_field_class_enum
**enum_decl
)
1919 struct ctf_field_class_int
*integer_decl
= NULL
;
1921 BT_ASSERT(enum_decl
);
1924 /* For named enum (without body), lookup in declaration scope */
1927 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(
1928 "Bodyless enumeration field class: missing name.");
1933 *enum_decl
= ctx_decl_scope_lookup_enum(ctx
->current_scope
, name
, -1, true);
1935 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Cannot find enumeration field class: "
1942 struct ctf_node
*iter
;
1943 struct uori last_value
= {
1952 if (ctx_decl_scope_lookup_enum(ctx
->current_scope
, name
, 1, false)) {
1953 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(
1954 "Enumeration field class already declared in local scope: "
1962 if (!container_cls
) {
1963 integer_decl
= ctf_field_class_as_int(
1964 ctx_decl_scope_lookup_alias(ctx
->current_scope
, "int", -1, true));
1965 if (!integer_decl
) {
1966 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(
1967 "Cannot find implicit `int` field class alias for enumeration field class.");
1972 ctf_field_class
*decl
;
1974 ret
= visit_field_class_declarator(ctx
, container_cls
, &qdummy_id
, NULL
, &decl
, NULL
);
1981 integer_decl
= ctf_field_class_as_int(decl
);
1984 BT_ASSERT(integer_decl
);
1986 if (integer_decl
->base
.base
.type
!= CTF_FIELD_CLASS_TYPE_INT
) {
1987 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(
1988 "Container field class for enumeration field class is not an integer field class: "
1990 integer_decl
->base
.base
.type
);
1995 *enum_decl
= ctf_field_class_enum_create();
1996 BT_ASSERT(*enum_decl
);
1997 (*enum_decl
)->base
.base
.base
.alignment
= integer_decl
->base
.base
.alignment
;
1998 ctf_field_class_int_copy_content(&(*enum_decl
)->base
, integer_decl
);
1999 last_value
.is_signed
= (*enum_decl
)->base
.is_signed
;
2001 bt_list_for_each_entry (iter
, enumerator_list
, siblings
) {
2002 ret
= visit_enum_decl_entry(ctx
, iter
, *enum_decl
, &last_value
);
2004 _BT_COMP_LOGE_APPEND_CAUSE_NODE(iter
,
2005 "Cannot visit enumeration field class entry: "
2013 ret
= ctx_decl_scope_register_enum(ctx
->current_scope
, name
, *enum_decl
);
2015 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(
2016 "Cannot register enumeration field class in declaration scope: "
2027 ctf_field_class_destroy(&(*enum_decl
)->base
.base
.base
);
2031 ctf_field_class_destroy(&integer_decl
->base
.base
);
2032 integer_decl
= NULL
;
2036 static int visit_field_class_specifier(struct ctf_visitor_generate_ir
*ctx
,
2037 struct ctf_node
*cls_specifier_list
,
2038 struct ctf_field_class
**decl
)
2041 GString
*str
= NULL
;
2044 str
= g_string_new("");
2045 ret
= get_class_specifier_list_name(ctx
, cls_specifier_list
, str
);
2047 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
2048 cls_specifier_list
, "Cannot get field class specifier list's name: ret=%d", ret
);
2052 *decl
= ctx_decl_scope_lookup_alias(ctx
->current_scope
, str
->str
, -1, true);
2054 _BT_COMP_LOGE_APPEND_CAUSE_NODE(cls_specifier_list
,
2055 "Cannot find field class alias: name=\"%s\"", str
->str
);
2063 ctf_field_class_destroy(*decl
);
2068 g_string_free(str
, TRUE
);
2074 static int visit_integer_decl(struct ctf_visitor_generate_ir
*ctx
, struct bt_list_head
*expressions
,
2075 struct ctf_field_class_int
**integer_decl
)
2080 struct ctf_node
*expression
;
2081 uint64_t alignment
= 0, size
= 0;
2082 struct ctf_clock_class
*mapped_clock_class
= NULL
;
2083 enum ctf_encoding encoding
= CTF_ENCODING_NONE
;
2084 bt_field_class_integer_preferred_display_base base
=
2085 BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL
;
2086 enum ctf_byte_order byte_order
= ctx
->ctf_tc
->default_byte_order
;
2088 *integer_decl
= NULL
;
2090 bt_list_for_each_entry (expression
, expressions
, siblings
) {
2091 struct ctf_node
*left
, *right
;
2093 left
= _BT_LIST_FIRST_ENTRY(&expression
->u
.ctf_expression
.left
, struct ctf_node
, siblings
);
2095 _BT_LIST_FIRST_ENTRY(&expression
->u
.ctf_expression
.right
, struct ctf_node
, siblings
);
2097 if (left
->u
.unary_expression
.type
!= UNARY_STRING
) {
2098 _BT_COMP_LOGE_APPEND_CAUSE_NODE(left
, "Unexpected unary expression type: type=%d",
2099 left
->u
.unary_expression
.type
);
2104 if (strcmp(left
->u
.unary_expression
.u
.string
, "signed") == 0) {
2105 if (_IS_SET(&set
, _INTEGER_SIGNED_SET
)) {
2106 _BT_COMP_LOGE_APPEND_CAUSE_DUP_ATTR(left
, "signed", "integer field class");
2111 signedness
= get_boolean(ctx
, right
);
2112 if (signedness
< 0) {
2113 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
2115 "Invalid boolean value for integer field class's `signed` attribute: "
2122 _SET(&set
, _INTEGER_SIGNED_SET
);
2123 } else if (strcmp(left
->u
.unary_expression
.u
.string
, "byte_order") == 0) {
2124 if (_IS_SET(&set
, _INTEGER_BYTE_ORDER_SET
)) {
2125 _BT_COMP_LOGE_APPEND_CAUSE_DUP_ATTR(left
, "byte_order", "integer field class");
2130 byte_order
= get_real_byte_order(ctx
, right
);
2131 if (byte_order
== CTF_BYTE_ORDER_UNKNOWN
) {
2132 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
2134 "Invalid `byte_order` attribute in integer field class: "
2141 _SET(&set
, _INTEGER_BYTE_ORDER_SET
);
2142 } else if (strcmp(left
->u
.unary_expression
.u
.string
, "size") == 0) {
2143 if (_IS_SET(&set
, _INTEGER_SIZE_SET
)) {
2144 _BT_COMP_LOGE_APPEND_CAUSE_DUP_ATTR(left
, "size", "integer field class");
2149 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
2150 _BT_COMP_LOGE_APPEND_CAUSE_NODE(right
,
2151 "Invalid `size` attribute in integer field class: "
2152 "expecting unsigned constant integer: "
2154 right
->u
.unary_expression
.type
);
2159 size
= right
->u
.unary_expression
.u
.unsigned_constant
;
2161 _BT_COMP_LOGE_APPEND_CAUSE_NODE(right
,
2162 "Invalid `size` attribute in integer field class: "
2163 "expecting positive constant integer: "
2168 } else if (size
> 64) {
2169 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
2171 "Invalid `size` attribute in integer field class: "
2172 "integer fields over 64 bits are not supported as of this version: "
2179 _SET(&set
, _INTEGER_SIZE_SET
);
2180 } else if (strcmp(left
->u
.unary_expression
.u
.string
, "align") == 0) {
2181 if (_IS_SET(&set
, _INTEGER_ALIGN_SET
)) {
2182 _BT_COMP_LOGE_APPEND_CAUSE_DUP_ATTR(left
, "align", "integer field class");
2187 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
2188 _BT_COMP_LOGE_APPEND_CAUSE_NODE(right
,
2189 "Invalid `align` attribute in integer field class: "
2190 "expecting unsigned constant integer: "
2192 right
->u
.unary_expression
.type
);
2197 alignment
= right
->u
.unary_expression
.u
.unsigned_constant
;
2198 if (!is_align_valid(alignment
)) {
2199 _BT_COMP_LOGE_APPEND_CAUSE_NODE(right
,
2200 "Invalid `align` attribute in integer field class: "
2201 "expecting power of two: "
2208 _SET(&set
, _INTEGER_ALIGN_SET
);
2209 } else if (strcmp(left
->u
.unary_expression
.u
.string
, "base") == 0) {
2210 if (_IS_SET(&set
, _INTEGER_BASE_SET
)) {
2211 _BT_COMP_LOGE_APPEND_CAUSE_DUP_ATTR(left
, "base", "integer field class");
2216 switch (right
->u
.unary_expression
.type
) {
2217 case UNARY_UNSIGNED_CONSTANT
:
2219 uint64_t constant
= right
->u
.unary_expression
.u
.unsigned_constant
;
2223 base
= BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_BINARY
;
2226 base
= BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_OCTAL
;
2229 base
= BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL
;
2232 base
= BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_HEXADECIMAL
;
2235 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
2237 "Invalid `base` attribute in integer field class: "
2239 right
->u
.unary_expression
.u
.unsigned_constant
);
2248 ctf_ast_concatenate_unary_strings(&expression
->u
.ctf_expression
.right
);
2250 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
2252 "Unexpected unary expression for integer field class's `base` attribute.");
2257 if (strcmp(s_right
, "decimal") == 0 || strcmp(s_right
, "dec") == 0 ||
2258 strcmp(s_right
, "d") == 0 || strcmp(s_right
, "i") == 0 ||
2259 strcmp(s_right
, "u") == 0) {
2260 base
= BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL
;
2261 } else if (strcmp(s_right
, "hexadecimal") == 0 || strcmp(s_right
, "hex") == 0 ||
2262 strcmp(s_right
, "x") == 0 || strcmp(s_right
, "X") == 0 ||
2263 strcmp(s_right
, "p") == 0) {
2264 base
= BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_HEXADECIMAL
;
2265 } else if (strcmp(s_right
, "octal") == 0 || strcmp(s_right
, "oct") == 0 ||
2266 strcmp(s_right
, "o") == 0) {
2267 base
= BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_OCTAL
;
2268 } else if (strcmp(s_right
, "binary") == 0 || strcmp(s_right
, "b") == 0) {
2269 base
= BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_BINARY
;
2271 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
2273 "Unexpected unary expression for integer field class's `base` attribute: "
2285 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
2286 right
, "Invalid `base` attribute in integer field class: "
2287 "expecting unsigned constant integer or unary string.");
2292 _SET(&set
, _INTEGER_BASE_SET
);
2293 } else if (strcmp(left
->u
.unary_expression
.u
.string
, "encoding") == 0) {
2296 if (_IS_SET(&set
, _INTEGER_ENCODING_SET
)) {
2297 _BT_COMP_LOGE_APPEND_CAUSE_DUP_ATTR(left
, "encoding", "integer field class");
2302 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
2303 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
2304 right
, "Invalid `encoding` attribute in integer field class: "
2305 "expecting unary string.");
2310 s_right
= ctf_ast_concatenate_unary_strings(&expression
->u
.ctf_expression
.right
);
2312 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
2314 "Unexpected unary expression for integer field class's `encoding` attribute.");
2319 if (strcmp(s_right
, "UTF8") == 0 || strcmp(s_right
, "utf8") == 0 ||
2320 strcmp(s_right
, "utf-8") == 0 || strcmp(s_right
, "UTF-8") == 0 ||
2321 strcmp(s_right
, "ASCII") == 0 || strcmp(s_right
, "ascii") == 0) {
2322 encoding
= CTF_ENCODING_UTF8
;
2323 } else if (strcmp(s_right
, "none") == 0) {
2324 encoding
= CTF_ENCODING_NONE
;
2326 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
2328 "Invalid `encoding` attribute in integer field class: "
2329 "unknown encoding: encoding=\"%s\"",
2337 _SET(&set
, _INTEGER_ENCODING_SET
);
2338 } else if (strcmp(left
->u
.unary_expression
.u
.string
, "map") == 0) {
2339 const char *clock_name
;
2341 if (_IS_SET(&set
, _INTEGER_MAP_SET
)) {
2342 _BT_COMP_LOGE_APPEND_CAUSE_DUP_ATTR(left
, "map", "integer field class");
2347 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
2348 _BT_COMP_LOGE_APPEND_CAUSE_NODE(right
,
2349 "Invalid `map` attribute in integer field class: "
2350 "expecting unary string.");
2355 clock_name
= get_map_clock_name_value(&expression
->u
.ctf_expression
.right
);
2358 ctf_ast_concatenate_unary_strings(&expression
->u
.ctf_expression
.right
);
2361 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
2363 "Unexpected unary expression for integer field class's `map` attribute.");
2368 _BT_COMP_LOGE_NODE(right
,
2369 "Invalid `map` attribute in integer field class: "
2370 "cannot find clock class at this point: name=\"%s\"",
2372 _SET(&set
, _INTEGER_MAP_SET
);
2377 mapped_clock_class
=
2378 ctf_trace_class_borrow_clock_class_by_name(ctx
->ctf_tc
, clock_name
);
2379 if (!mapped_clock_class
) {
2380 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
2382 "Invalid `map` attribute in integer field class: "
2383 "cannot find clock class at this point: name=\"%s\"",
2389 _SET(&set
, _INTEGER_MAP_SET
);
2391 _BT_COMP_LOGW_NODE(left
,
2392 "Unknown attribute in integer field class: "
2394 left
->u
.unary_expression
.u
.string
);
2398 if (!_IS_SET(&set
, _INTEGER_SIZE_SET
)) {
2399 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(
2400 "Missing `size` attribute in integer field class.");
2405 if (!_IS_SET(&set
, _INTEGER_ALIGN_SET
)) {
2406 if (size
% CHAR_BIT
) {
2407 /* Bit-packed alignment */
2410 /* Byte-packed alignment */
2411 alignment
= CHAR_BIT
;
2415 *integer_decl
= ctf_field_class_int_create();
2416 BT_ASSERT(*integer_decl
);
2417 (*integer_decl
)->base
.base
.alignment
= alignment
;
2418 (*integer_decl
)->base
.byte_order
= byte_order
;
2419 (*integer_decl
)->base
.size
= size
;
2420 (*integer_decl
)->is_signed
= (signedness
> 0);
2421 (*integer_decl
)->disp_base
= base
;
2422 (*integer_decl
)->encoding
= encoding
;
2423 (*integer_decl
)->mapped_clock_class
= mapped_clock_class
;
2427 ctf_field_class_destroy(&(*integer_decl
)->base
.base
);
2428 *integer_decl
= NULL
;
2432 static int visit_floating_point_number_decl(struct ctf_visitor_generate_ir
*ctx
,
2433 struct bt_list_head
*expressions
,
2434 struct ctf_field_class_float
**float_decl
)
2438 struct ctf_node
*expression
;
2439 uint64_t alignment
= 1, exp_dig
= 0, mant_dig
= 0;
2440 enum ctf_byte_order byte_order
= ctx
->ctf_tc
->default_byte_order
;
2444 bt_list_for_each_entry (expression
, expressions
, siblings
) {
2445 struct ctf_node
*left
, *right
;
2447 left
= _BT_LIST_FIRST_ENTRY(&expression
->u
.ctf_expression
.left
, struct ctf_node
, siblings
);
2449 _BT_LIST_FIRST_ENTRY(&expression
->u
.ctf_expression
.right
, struct ctf_node
, siblings
);
2451 if (left
->u
.unary_expression
.type
!= UNARY_STRING
) {
2452 _BT_COMP_LOGE_APPEND_CAUSE_NODE(left
, "Unexpected unary expression type: type=%d",
2453 left
->u
.unary_expression
.type
);
2458 if (strcmp(left
->u
.unary_expression
.u
.string
, "byte_order") == 0) {
2459 if (_IS_SET(&set
, _FLOAT_BYTE_ORDER_SET
)) {
2460 _BT_COMP_LOGE_APPEND_CAUSE_DUP_ATTR(left
, "byte_order",
2461 "floating point number field class");
2466 byte_order
= get_real_byte_order(ctx
, right
);
2467 if (byte_order
== CTF_BYTE_ORDER_UNKNOWN
) {
2468 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
2470 "Invalid `byte_order` attribute in floating point number field class: "
2477 _SET(&set
, _FLOAT_BYTE_ORDER_SET
);
2478 } else if (strcmp(left
->u
.unary_expression
.u
.string
, "exp_dig") == 0) {
2479 if (_IS_SET(&set
, _FLOAT_EXP_DIG_SET
)) {
2480 _BT_COMP_LOGE_APPEND_CAUSE_DUP_ATTR(left
, "exp_dig",
2481 "floating point number field class");
2486 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
2487 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
2489 "Invalid `exp_dig` attribute in floating point number field class: "
2490 "expecting unsigned constant integer: "
2492 right
->u
.unary_expression
.type
);
2497 exp_dig
= right
->u
.unary_expression
.u
.unsigned_constant
;
2498 _SET(&set
, _FLOAT_EXP_DIG_SET
);
2499 } else if (strcmp(left
->u
.unary_expression
.u
.string
, "mant_dig") == 0) {
2500 if (_IS_SET(&set
, _FLOAT_MANT_DIG_SET
)) {
2501 _BT_COMP_LOGE_APPEND_CAUSE_DUP_ATTR(left
, "mant_dig",
2502 "floating point number field class");
2507 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
2508 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
2510 "Invalid `mant_dig` attribute in floating point number field class: "
2511 "expecting unsigned constant integer: "
2513 right
->u
.unary_expression
.type
);
2518 mant_dig
= right
->u
.unary_expression
.u
.unsigned_constant
;
2519 _SET(&set
, _FLOAT_MANT_DIG_SET
);
2520 } else if (strcmp(left
->u
.unary_expression
.u
.string
, "align") == 0) {
2521 if (_IS_SET(&set
, _FLOAT_ALIGN_SET
)) {
2522 _BT_COMP_LOGE_APPEND_CAUSE_DUP_ATTR(left
, "align",
2523 "floating point number field class");
2528 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
2529 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
2531 "Invalid `align` attribute in floating point number field class: "
2532 "expecting unsigned constant integer: "
2534 right
->u
.unary_expression
.type
);
2539 alignment
= right
->u
.unary_expression
.u
.unsigned_constant
;
2541 if (!is_align_valid(alignment
)) {
2542 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
2544 "Invalid `align` attribute in floating point number field class: "
2545 "expecting power of two: "
2552 _SET(&set
, _FLOAT_ALIGN_SET
);
2554 _BT_COMP_LOGW_NODE(left
,
2555 "Unknown attribute in floating point number field class: "
2557 left
->u
.unary_expression
.u
.string
);
2561 if (!_IS_SET(&set
, _FLOAT_MANT_DIG_SET
)) {
2562 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(
2563 "Missing `mant_dig` attribute in floating point number field class.");
2568 if (!_IS_SET(&set
, _FLOAT_EXP_DIG_SET
)) {
2569 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(
2570 "Missing `exp_dig` attribute in floating point number field class.");
2575 if (mant_dig
!= 24 && mant_dig
!= 53) {
2576 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("`mant_dig` attribute: expecting 24 or 53.");
2581 if (mant_dig
== 24 && exp_dig
!= 8) {
2582 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(
2583 "`exp_dig` attribute: expecting 8 because `mant_dig` is 24.");
2588 if (mant_dig
== 53 && exp_dig
!= 11) {
2589 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(
2590 "`exp_dig` attribute: expecting 11 because `mant_dig` is 53.");
2595 if (!_IS_SET(&set
, _INTEGER_ALIGN_SET
)) {
2596 if ((mant_dig
+ exp_dig
) % CHAR_BIT
) {
2597 /* Bit-packed alignment */
2600 /* Byte-packed alignment */
2601 alignment
= CHAR_BIT
;
2605 *float_decl
= ctf_field_class_float_create();
2606 BT_ASSERT(*float_decl
);
2607 (*float_decl
)->base
.base
.alignment
= alignment
;
2608 (*float_decl
)->base
.byte_order
= byte_order
;
2609 (*float_decl
)->base
.size
= mant_dig
+ exp_dig
;
2613 ctf_field_class_destroy(&(*float_decl
)->base
.base
);
2618 static int visit_string_decl(struct ctf_visitor_generate_ir
*ctx
, struct bt_list_head
*expressions
,
2619 struct ctf_field_class_string
**string_decl
)
2623 struct ctf_node
*expression
;
2624 enum ctf_encoding encoding
= CTF_ENCODING_UTF8
;
2626 *string_decl
= NULL
;
2628 bt_list_for_each_entry (expression
, expressions
, siblings
) {
2629 struct ctf_node
*left
, *right
;
2631 left
= _BT_LIST_FIRST_ENTRY(&expression
->u
.ctf_expression
.left
, struct ctf_node
, siblings
);
2633 _BT_LIST_FIRST_ENTRY(&expression
->u
.ctf_expression
.right
, struct ctf_node
, siblings
);
2635 if (left
->u
.unary_expression
.type
!= UNARY_STRING
) {
2636 _BT_COMP_LOGE_APPEND_CAUSE_NODE(left
, "Unexpected unary expression type: type=%d",
2637 left
->u
.unary_expression
.type
);
2642 if (strcmp(left
->u
.unary_expression
.u
.string
, "encoding") == 0) {
2645 if (_IS_SET(&set
, _STRING_ENCODING_SET
)) {
2646 _BT_COMP_LOGE_APPEND_CAUSE_DUP_ATTR(left
, "encoding", "string field class");
2651 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
2652 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
2653 right
, "Invalid `encoding` attribute in string field class: "
2654 "expecting unary string.");
2659 s_right
= ctf_ast_concatenate_unary_strings(&expression
->u
.ctf_expression
.right
);
2661 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
2663 "Unexpected unary expression for string field class's `encoding` attribute.");
2668 if (strcmp(s_right
, "UTF8") == 0 || strcmp(s_right
, "utf8") == 0 ||
2669 strcmp(s_right
, "utf-8") == 0 || strcmp(s_right
, "UTF-8") == 0 ||
2670 strcmp(s_right
, "ASCII") == 0 || strcmp(s_right
, "ascii") == 0) {
2671 encoding
= CTF_ENCODING_UTF8
;
2672 } else if (strcmp(s_right
, "none") == 0) {
2673 encoding
= CTF_ENCODING_NONE
;
2675 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
2677 "Invalid `encoding` attribute in string field class: "
2678 "unknown encoding: encoding=\"%s\"",
2686 _SET(&set
, _STRING_ENCODING_SET
);
2688 _BT_COMP_LOGW_NODE(left
,
2689 "Unknown attribute in string field class: "
2691 left
->u
.unary_expression
.u
.string
);
2695 *string_decl
= ctf_field_class_string_create();
2696 BT_ASSERT(*string_decl
);
2697 (*string_decl
)->encoding
= encoding
;
2701 ctf_field_class_destroy(&(*string_decl
)->base
);
2702 *string_decl
= NULL
;
2706 static int visit_field_class_specifier_list(struct ctf_visitor_generate_ir
*ctx
,
2707 struct ctf_node
*ts_list
, struct ctf_field_class
**decl
)
2710 struct ctf_node
*first
, *node
;
2714 if (ts_list
->type
!= NODE_TYPE_SPECIFIER_LIST
) {
2715 _BT_COMP_LOGE_APPEND_CAUSE_NODE(ts_list
, "Unexpected node type: node-type=%d",
2721 first
= _BT_LIST_FIRST_ENTRY(&ts_list
->u
.field_class_specifier_list
.head
, struct ctf_node
,
2723 if (first
->type
!= NODE_TYPE_SPECIFIER
) {
2724 _BT_COMP_LOGE_APPEND_CAUSE_NODE(first
, "Unexpected node type: node-type=%d", first
->type
);
2729 node
= first
->u
.field_class_specifier
.node
;
2731 switch (first
->u
.field_class_specifier
.type
) {
2732 case TYPESPEC_INTEGER
:
2734 ctf_field_class_int
*int_decl
;
2736 ret
= visit_integer_decl(ctx
, &node
->u
.integer
.expressions
, &int_decl
);
2738 BT_ASSERT(!int_decl
);
2742 *decl
= &int_decl
->base
.base
;
2745 case TYPESPEC_FLOATING_POINT
:
2747 ctf_field_class_float
*float_decl
;
2750 visit_floating_point_number_decl(ctx
, &node
->u
.floating_point
.expressions
, &float_decl
);
2752 BT_ASSERT(!float_decl
);
2756 *decl
= &float_decl
->base
.base
;
2759 case TYPESPEC_STRING
:
2761 ctf_field_class_string
*string_decl
;
2763 ret
= visit_string_decl(ctx
, &node
->u
.string
.expressions
, &string_decl
);
2765 BT_ASSERT(!string_decl
);
2769 *decl
= &string_decl
->base
;
2772 case TYPESPEC_STRUCT
:
2774 ctf_field_class_struct
*struct_decl
;
2776 ret
= visit_struct_decl(ctx
, node
->u
._struct
.name
, &node
->u
._struct
.declaration_list
,
2777 node
->u
._struct
.has_body
, &node
->u
._struct
.min_align
, &struct_decl
);
2779 BT_ASSERT(!struct_decl
);
2783 *decl
= &struct_decl
->base
;
2786 case TYPESPEC_VARIANT
:
2788 ctf_field_class_variant
*variant_decl
;
2790 ret
= visit_variant_decl(ctx
, node
->u
.variant
.name
, node
->u
.variant
.choice
,
2791 &node
->u
.variant
.declaration_list
, node
->u
.variant
.has_body
,
2794 BT_ASSERT(!variant_decl
);
2798 *decl
= &variant_decl
->base
;
2803 ctf_field_class_enum
*enum_decl
;
2805 ret
= visit_enum_decl(ctx
, node
->u
._enum
.enum_id
, node
->u
._enum
.container_field_class
,
2806 &node
->u
._enum
.enumerator_list
, node
->u
._enum
.has_body
, &enum_decl
);
2808 BT_ASSERT(!enum_decl
);
2812 *decl
= &enum_decl
->base
.base
.base
;
2817 case TYPESPEC_SHORT
:
2820 case TYPESPEC_FLOAT
:
2821 case TYPESPEC_DOUBLE
:
2822 case TYPESPEC_SIGNED
:
2823 case TYPESPEC_UNSIGNED
:
2825 case TYPESPEC_COMPLEX
:
2826 case TYPESPEC_IMAGINARY
:
2827 case TYPESPEC_CONST
:
2828 case TYPESPEC_ID_TYPE
:
2829 ret
= visit_field_class_specifier(ctx
, ts_list
, decl
);
2831 _BT_COMP_LOGE_APPEND_CAUSE_NODE(first
, "Cannot visit field class specifier: ret=%d",
2838 _BT_COMP_LOGE_APPEND_CAUSE_NODE(first
,
2839 "Unexpected field class specifier type: node-type=%d",
2840 first
->u
.field_class_specifier
.type
);
2849 ctf_field_class_destroy(*decl
);
2854 static int visit_event_decl_entry(struct ctf_visitor_generate_ir
*ctx
, struct ctf_node
*node
,
2855 struct ctf_event_class
*event_class
, uint64_t *stream_id
,
2861 switch (node
->type
) {
2863 ret
= visit_field_class_def(ctx
, node
->u
.field_class_def
.field_class_specifier_list
,
2864 &node
->u
.field_class_def
.field_class_declarators
);
2866 _BT_COMP_LOGE_APPEND_CAUSE_NODE(node
, "Cannot add field class found in event class.");
2870 case NODE_TYPEALIAS
:
2871 ret
= visit_field_class_alias(ctx
, node
->u
.field_class_alias
.target
,
2872 node
->u
.field_class_alias
.alias
);
2874 _BT_COMP_LOGE_APPEND_CAUSE_NODE(node
,
2875 "Cannot add field class alias found in event class.");
2879 case NODE_CTF_EXPRESSION
:
2881 left
= ctf_ast_concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
2883 _BT_COMP_LOGE_APPEND_CAUSE_NODE(node
, "Cannot concatenate unary strings.");
2888 if (strcmp(left
, "name") == 0) {
2889 /* This is already known at this stage */
2890 if (_IS_SET(set
, _EVENT_NAME_SET
)) {
2891 _BT_COMP_LOGE_APPEND_CAUSE_DUP_ATTR(node
, "name", "event class");
2896 _SET(set
, _EVENT_NAME_SET
);
2897 } else if (strcmp(left
, "id") == 0) {
2900 if (_IS_SET(set
, _EVENT_ID_SET
)) {
2901 _BT_COMP_LOGE_APPEND_CAUSE_DUP_ATTR(node
, "id", "event class");
2906 ret
= get_unary_unsigned(ctx
, &node
->u
.ctf_expression
.right
, (uint64_t *) &id
);
2907 /* Only read "id" if get_unary_unsigned() succeeded. */
2908 if (ret
|| (!ret
&& id
< 0)) {
2909 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
2910 node
, "Unexpected unary expression for event class's `id` attribute.");
2915 event_class
->id
= id
;
2916 _SET(set
, _EVENT_ID_SET
);
2917 } else if (strcmp(left
, "stream_id") == 0) {
2918 if (_IS_SET(set
, _EVENT_STREAM_ID_SET
)) {
2919 _BT_COMP_LOGE_APPEND_CAUSE_DUP_ATTR(node
, "stream_id", "event class");
2924 ret
= get_unary_unsigned(ctx
, &node
->u
.ctf_expression
.right
, stream_id
);
2927 * Only read "stream_id" if get_unary_unsigned()
2931 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
2932 node
, "Unexpected unary expression for event class's `stream_id` attribute.");
2937 _SET(set
, _EVENT_STREAM_ID_SET
);
2938 } else if (strcmp(left
, "context") == 0) {
2939 if (_IS_SET(set
, _EVENT_CONTEXT_SET
)) {
2940 _BT_COMP_LOGE_APPEND_CAUSE_NODE(node
, "Duplicate `context` entry in event class.");
2945 ret
= visit_field_class_specifier_list(
2946 ctx
, _BT_LIST_FIRST_ENTRY(&node
->u
.ctf_expression
.right
, struct ctf_node
, siblings
),
2947 &event_class
->spec_context_fc
);
2949 _BT_COMP_LOGE_APPEND_CAUSE_NODE(node
,
2950 "Cannot create event class's context field class.");
2954 BT_ASSERT(event_class
->spec_context_fc
);
2955 _SET(set
, _EVENT_CONTEXT_SET
);
2956 } else if (strcmp(left
, "fields") == 0) {
2957 if (_IS_SET(set
, _EVENT_FIELDS_SET
)) {
2958 _BT_COMP_LOGE_APPEND_CAUSE_NODE(node
, "Duplicate `fields` entry in event class.");
2963 ret
= visit_field_class_specifier_list(
2964 ctx
, _BT_LIST_FIRST_ENTRY(&node
->u
.ctf_expression
.right
, struct ctf_node
, siblings
),
2965 &event_class
->payload_fc
);
2967 _BT_COMP_LOGE_APPEND_CAUSE_NODE(node
,
2968 "Cannot create event class's payload field class.");
2972 BT_ASSERT(event_class
->payload_fc
);
2973 _SET(set
, _EVENT_FIELDS_SET
);
2974 } else if (strcmp(left
, "loglevel") == 0) {
2975 uint64_t loglevel_value
;
2976 bool is_log_level_known
= true;
2977 bt_event_class_log_level log_level
;
2979 if (_IS_SET(set
, _EVENT_LOG_LEVEL_SET
)) {
2980 _BT_COMP_LOGE_APPEND_CAUSE_DUP_ATTR(node
, "loglevel", "event class");
2985 ret
= get_unary_unsigned(ctx
, &node
->u
.ctf_expression
.right
, &loglevel_value
);
2987 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
2988 node
, "Unexpected unary expression for event class's `loglevel` attribute.");
2993 switch (loglevel_value
) {
2995 log_level
= BT_EVENT_CLASS_LOG_LEVEL_EMERGENCY
;
2998 log_level
= BT_EVENT_CLASS_LOG_LEVEL_ALERT
;
3001 log_level
= BT_EVENT_CLASS_LOG_LEVEL_CRITICAL
;
3004 log_level
= BT_EVENT_CLASS_LOG_LEVEL_ERROR
;
3007 log_level
= BT_EVENT_CLASS_LOG_LEVEL_WARNING
;
3010 log_level
= BT_EVENT_CLASS_LOG_LEVEL_NOTICE
;
3013 log_level
= BT_EVENT_CLASS_LOG_LEVEL_INFO
;
3016 log_level
= BT_EVENT_CLASS_LOG_LEVEL_DEBUG_SYSTEM
;
3019 log_level
= BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROGRAM
;
3022 log_level
= BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROCESS
;
3025 log_level
= BT_EVENT_CLASS_LOG_LEVEL_DEBUG_MODULE
;
3028 log_level
= BT_EVENT_CLASS_LOG_LEVEL_DEBUG_UNIT
;
3031 log_level
= BT_EVENT_CLASS_LOG_LEVEL_DEBUG_FUNCTION
;
3034 log_level
= BT_EVENT_CLASS_LOG_LEVEL_DEBUG_LINE
;
3037 log_level
= BT_EVENT_CLASS_LOG_LEVEL_DEBUG
;
3040 is_log_level_known
= false;
3043 "Not setting event class's log level because its value is unknown: "
3044 "log-level=%" PRIu64
,
3048 if (is_log_level_known
) {
3049 ctf_event_class_set_log_level(event_class
, log_level
);
3052 _SET(set
, _EVENT_LOG_LEVEL_SET
);
3053 } else if (strcmp(left
, "model.emf.uri") == 0) {
3056 if (_IS_SET(set
, _EVENT_MODEL_EMF_URI_SET
)) {
3057 _BT_COMP_LOGE_APPEND_CAUSE_DUP_ATTR(node
, "model.emf.uri", "event class");
3062 right
= ctf_ast_concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
3064 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
3066 "Unexpected unary expression for event class's `model.emf.uri` attribute.");
3071 if (strlen(right
) == 0) {
3072 _BT_COMP_LOGW_NODE(node
, "Not setting event class's EMF URI because it's empty.");
3074 g_string_assign(event_class
->emf_uri
, right
);
3078 _SET(set
, _EVENT_MODEL_EMF_URI_SET
);
3080 _BT_COMP_LOGW_NODE(node
,
3081 "Unknown attribute in event class: "
3104 static char *get_event_decl_name(struct ctf_visitor_generate_ir
*ctx
, struct ctf_node
*node
)
3108 struct ctf_node
*iter
;
3109 struct bt_list_head
*decl_list
= &node
->u
.event
.declaration_list
;
3111 bt_list_for_each_entry (iter
, decl_list
, siblings
) {
3112 if (iter
->type
!= NODE_CTF_EXPRESSION
) {
3116 left
= ctf_ast_concatenate_unary_strings(&iter
->u
.ctf_expression
.left
);
3118 _BT_COMP_LOGE_APPEND_CAUSE_NODE(iter
, "Cannot concatenate unary strings.");
3122 if (strcmp(left
, "name") == 0) {
3123 name
= ctf_ast_concatenate_unary_strings(&iter
->u
.ctf_expression
.right
);
3125 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
3126 iter
, "Unexpected unary expression for event class's `name` attribute.");
3146 static int visit_event_decl(struct ctf_visitor_generate_ir
*ctx
, struct ctf_node
*node
)
3150 struct ctf_node
*iter
;
3151 uint64_t stream_id
= 0;
3152 char *event_name
= NULL
;
3153 struct ctf_event_class
*event_class
= NULL
;
3154 struct ctf_stream_class
*stream_class
= NULL
;
3155 struct bt_list_head
*decl_list
= &node
->u
.event
.declaration_list
;
3156 bool pop_scope
= false;
3158 if (node
->visited
) {
3162 node
->visited
= TRUE
;
3163 event_name
= get_event_decl_name(ctx
, node
);
3165 _BT_COMP_LOGE_APPEND_CAUSE_NODE(node
, "Missing `name` attribute in event class.");
3170 event_class
= ctf_event_class_create();
3171 BT_ASSERT(event_class
);
3172 g_string_assign(event_class
->name
, event_name
);
3173 _TRY_PUSH_SCOPE_OR_GOTO_ERROR();
3176 bt_list_for_each_entry (iter
, decl_list
, siblings
) {
3177 ret
= visit_event_decl_entry(ctx
, iter
, event_class
, &stream_id
, &set
);
3179 _BT_COMP_LOGE_APPEND_CAUSE_NODE(iter
,
3180 "Cannot visit event class's entry: "
3187 if (!_IS_SET(&set
, _EVENT_STREAM_ID_SET
)) {
3189 * Allow missing stream_id if there is only a single
3192 switch (ctx
->ctf_tc
->stream_classes
->len
) {
3194 /* Create implicit stream class if there's none */
3196 stream_class
= ctf_stream_class_create();
3197 BT_ASSERT(stream_class
);
3198 stream_class
->id
= stream_id
;
3199 g_ptr_array_add(ctx
->ctf_tc
->stream_classes
, stream_class
);
3202 /* Single stream class: get its ID */
3203 stream_class
= (ctf_stream_class
*) ctx
->ctf_tc
->stream_classes
->pdata
[0];
3204 stream_id
= stream_class
->id
;
3207 _BT_COMP_LOGE_APPEND_CAUSE_NODE(node
, "Missing `stream_id` attribute in event class.");
3213 /* We have the stream ID now; get the stream class if found */
3214 if (!stream_class
) {
3215 stream_class
= ctf_trace_class_borrow_stream_class_by_id(ctx
->ctf_tc
, stream_id
);
3216 if (!stream_class
) {
3217 _BT_COMP_LOGE_APPEND_CAUSE_NODE(node
,
3218 "Cannot find stream class at this point: "
3226 BT_ASSERT(stream_class
);
3228 if (!_IS_SET(&set
, _EVENT_ID_SET
)) {
3229 /* Allow only one event without ID per stream */
3230 if (stream_class
->event_classes
->len
!= 0) {
3231 _BT_COMP_LOGE_APPEND_CAUSE_NODE(node
, "Missing `id` attribute in event class.");
3237 event_class
->id
= 0;
3240 if (ctf_stream_class_borrow_event_class_by_id(stream_class
, event_class
->id
)) {
3241 _BT_COMP_LOGE_APPEND_CAUSE_NODE(node
,
3242 "Duplicate event class (same ID) in the same stream class: "
3249 ctf_stream_class_append_event_class(stream_class
, event_class
);
3254 ctf_event_class_destroy(event_class
);
3271 static int auto_map_field_to_trace_clock_class(struct ctf_visitor_generate_ir
*ctx
,
3272 struct ctf_field_class
*fc
)
3274 struct ctf_clock_class
*clock_class_to_map_to
= NULL
;
3275 uint64_t clock_class_count
;
3281 if (fc
->type
!= CTF_FIELD_CLASS_TYPE_INT
&& fc
->type
!= CTF_FIELD_CLASS_TYPE_ENUM
) {
3285 ctf_field_class_int
*int_fc
= ctf_field_class_as_int(fc
);
3287 if (int_fc
->mapped_clock_class
) {
3288 /* Already mapped */
3292 clock_class_count
= ctx
->ctf_tc
->clock_classes
->len
;
3294 switch (clock_class_count
) {
3297 * No clock class exists in the trace at this point. Create an
3298 * implicit one at 1 GHz, named `default`, and use this clock
3301 clock_class_to_map_to
= ctf_clock_class_create();
3302 BT_ASSERT(clock_class_to_map_to
);
3303 clock_class_to_map_to
->frequency
= UINT64_C(1000000000);
3304 g_string_assign(clock_class_to_map_to
->name
, "default");
3305 g_ptr_array_add(ctx
->ctf_tc
->clock_classes
, clock_class_to_map_to
);
3309 * Only one clock class exists in the trace at this point: use
3312 clock_class_to_map_to
= (ctf_clock_class
*) ctx
->ctf_tc
->clock_classes
->pdata
[0];
3316 * Timestamp field not mapped to a clock class and there's more
3317 * than one clock class in the trace: this is an error.
3319 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(
3320 "Timestamp field found with no mapped clock class, "
3321 "but there's more than one clock class in the trace at this point.");
3325 BT_ASSERT(clock_class_to_map_to
);
3326 int_fc
->mapped_clock_class
= clock_class_to_map_to
;
3331 static int auto_map_fields_to_trace_clock_class(struct ctf_visitor_generate_ir
*ctx
,
3332 struct ctf_field_class
*root_fc
,
3333 const char *field_name
)
3337 struct ctf_field_class_struct
*struct_fc
= (ctf_field_class_struct
*) root_fc
;
3338 struct ctf_field_class_variant
*var_fc
= (ctf_field_class_variant
*) root_fc
;
3344 if (root_fc
->type
!= CTF_FIELD_CLASS_TYPE_STRUCT
&&
3345 root_fc
->type
!= CTF_FIELD_CLASS_TYPE_VARIANT
) {
3349 if (root_fc
->type
== CTF_FIELD_CLASS_TYPE_STRUCT
) {
3350 count
= struct_fc
->members
->len
;
3352 count
= var_fc
->options
->len
;
3355 for (i
= 0; i
< count
; i
++) {
3356 struct ctf_named_field_class
*named_fc
= NULL
;
3358 if (root_fc
->type
== CTF_FIELD_CLASS_TYPE_STRUCT
) {
3359 named_fc
= ctf_field_class_struct_borrow_member_by_index(struct_fc
, i
);
3360 } else if (root_fc
->type
== CTF_FIELD_CLASS_TYPE_VARIANT
) {
3361 named_fc
= ctf_field_class_variant_borrow_option_by_index(var_fc
, i
);
3366 if (strcmp(named_fc
->name
->str
, field_name
) == 0) {
3367 ret
= auto_map_field_to_trace_clock_class(ctx
, named_fc
->fc
);
3369 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(
3370 "Cannot automatically map field to trace's clock class: "
3371 "field-name=\"%s\"",
3377 ret
= auto_map_fields_to_trace_clock_class(ctx
, named_fc
->fc
, field_name
);
3379 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(
3380 "Cannot automatically map structure or variant field class's fields to trace's clock class: "
3381 "field-name=\"%s\", root-field-name=\"%s\"",
3382 field_name
, named_fc
->name
->str
);
3391 static int visit_stream_decl_entry(struct ctf_visitor_generate_ir
*ctx
, struct ctf_node
*node
,
3392 struct ctf_stream_class
*stream_class
, int *set
)
3397 switch (node
->type
) {
3399 ret
= visit_field_class_def(ctx
, node
->u
.field_class_def
.field_class_specifier_list
,
3400 &node
->u
.field_class_def
.field_class_declarators
);
3402 _BT_COMP_LOGE_APPEND_CAUSE_NODE(node
, "Cannot add field class found in stream class.");
3406 case NODE_TYPEALIAS
:
3407 ret
= visit_field_class_alias(ctx
, node
->u
.field_class_alias
.target
,
3408 node
->u
.field_class_alias
.alias
);
3410 _BT_COMP_LOGE_APPEND_CAUSE_NODE(node
,
3411 "Cannot add field class alias found in stream class.");
3415 case NODE_CTF_EXPRESSION
:
3417 left
= ctf_ast_concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
3419 _BT_COMP_LOGE_APPEND_CAUSE_NODE(node
, "Cannot concatenate unary strings.");
3424 if (strcmp(left
, "id") == 0) {
3427 if (_IS_SET(set
, _STREAM_ID_SET
)) {
3428 _BT_COMP_LOGE_APPEND_CAUSE_DUP_ATTR(node
, "id", "stream declaration");
3433 ret
= get_unary_unsigned(ctx
, &node
->u
.ctf_expression
.right
, (uint64_t *) &id
);
3435 /* Only read "id" if get_unary_unsigned() succeeded. */
3436 if (ret
|| (!ret
&& id
< 0)) {
3437 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
3438 node
, "Unexpected unary expression for stream class's `id` attribute.");
3443 if (ctf_trace_class_borrow_stream_class_by_id(ctx
->ctf_tc
, id
)) {
3444 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
3445 node
, "Duplicate stream class (same ID): id=%" PRId64
, id
);
3450 stream_class
->id
= id
;
3451 _SET(set
, _STREAM_ID_SET
);
3452 } else if (strcmp(left
, "event.header") == 0) {
3453 if (_IS_SET(set
, _STREAM_EVENT_HEADER_SET
)) {
3454 _BT_COMP_LOGE_APPEND_CAUSE_NODE(node
,
3455 "Duplicate `event.header` entry in stream class.");
3460 ret
= visit_field_class_specifier_list(
3461 ctx
, _BT_LIST_FIRST_ENTRY(&node
->u
.ctf_expression
.right
, struct ctf_node
, siblings
),
3462 &stream_class
->event_header_fc
);
3464 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
3465 node
, "Cannot create stream class's event header field class.");
3469 BT_ASSERT(stream_class
->event_header_fc
);
3470 ret
= auto_map_fields_to_trace_clock_class(ctx
, stream_class
->event_header_fc
,
3473 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
3475 "Cannot automatically map specific event header field class fields named `timestamp` to trace's clock class.");
3479 _SET(set
, _STREAM_EVENT_HEADER_SET
);
3480 } else if (strcmp(left
, "event.context") == 0) {
3481 if (_IS_SET(set
, _STREAM_EVENT_CONTEXT_SET
)) {
3482 _BT_COMP_LOGE_APPEND_CAUSE_NODE(node
,
3483 "Duplicate `event.context` entry in stream class.");
3488 ret
= visit_field_class_specifier_list(
3489 ctx
, _BT_LIST_FIRST_ENTRY(&node
->u
.ctf_expression
.right
, struct ctf_node
, siblings
),
3490 &stream_class
->event_common_context_fc
);
3492 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
3493 node
, "Cannot create stream class's event context field class.");
3497 BT_ASSERT(stream_class
->event_common_context_fc
);
3498 _SET(set
, _STREAM_EVENT_CONTEXT_SET
);
3499 } else if (strcmp(left
, "packet.context") == 0) {
3500 if (_IS_SET(set
, _STREAM_PACKET_CONTEXT_SET
)) {
3501 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
3502 node
, "Duplicate `packet.context` entry in stream class.");
3507 ret
= visit_field_class_specifier_list(
3508 ctx
, _BT_LIST_FIRST_ENTRY(&node
->u
.ctf_expression
.right
, struct ctf_node
, siblings
),
3509 &stream_class
->packet_context_fc
);
3511 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
3512 node
, "Cannot create stream class's packet context field class.");
3516 BT_ASSERT(stream_class
->packet_context_fc
);
3517 ret
= auto_map_fields_to_trace_clock_class(ctx
, stream_class
->packet_context_fc
,
3520 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
3522 "Cannot automatically map specific packet context field class fields named `timestamp_begin` to trace's clock class.");
3526 ret
= auto_map_fields_to_trace_clock_class(ctx
, stream_class
->packet_context_fc
,
3529 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
3531 "Cannot automatically map specific packet context field class fields named `timestamp_end` to trace's clock class.");
3535 _SET(set
, _STREAM_PACKET_CONTEXT_SET
);
3537 _BT_COMP_LOGW_NODE(node
,
3538 "Unknown attribute in stream class: "
3560 static int visit_stream_decl(struct ctf_visitor_generate_ir
*ctx
, struct ctf_node
*node
)
3564 struct ctf_node
*iter
;
3565 struct ctf_stream_class
*stream_class
= NULL
;
3566 struct bt_list_head
*decl_list
= &node
->u
.stream
.declaration_list
;
3568 if (node
->visited
) {
3572 node
->visited
= TRUE
;
3573 stream_class
= ctf_stream_class_create();
3574 BT_ASSERT(stream_class
);
3575 _TRY_PUSH_SCOPE_OR_GOTO_ERROR();
3577 bt_list_for_each_entry (iter
, decl_list
, siblings
) {
3578 ret
= visit_stream_decl_entry(ctx
, iter
, stream_class
, &set
);
3580 _BT_COMP_LOGE_APPEND_CAUSE_NODE(iter
,
3581 "Cannot visit stream class's entry: "
3591 if (_IS_SET(&set
, _STREAM_ID_SET
)) {
3592 /* Check that packet header has `stream_id` field */
3593 struct ctf_named_field_class
*named_fc
= NULL
;
3595 if (!ctx
->ctf_tc
->packet_header_fc
) {
3596 _BT_COMP_LOGE_APPEND_CAUSE_NODE(node
, "Stream class has a `id` attribute, "
3597 "but trace has no packet header field class.");
3602 named_fc
= ctf_field_class_struct_borrow_member_by_name(
3603 ctf_field_class_as_struct(ctx
->ctf_tc
->packet_header_fc
), "stream_id");
3605 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
3606 node
, "Stream class has a `id` attribute, "
3607 "but trace's packet header field class has no `stream_id` field.");
3612 if (named_fc
->fc
->type
!= CTF_FIELD_CLASS_TYPE_INT
&&
3613 named_fc
->fc
->type
!= CTF_FIELD_CLASS_TYPE_ENUM
) {
3614 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
3616 "Stream class has a `id` attribute, "
3617 "but trace's packet header field class's `stream_id` field is not an integer field class.");
3622 /* Allow only _one_ ID-less stream */
3623 if (ctx
->ctf_tc
->stream_classes
->len
!= 0) {
3624 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
3626 "Missing `id` attribute in stream class as there's more than one stream class in the trace.");
3631 /* Automatic ID: 0 */
3632 stream_class
->id
= 0;
3636 * Make sure that this stream class's ID is currently unique in
3639 if (ctf_trace_class_borrow_stream_class_by_id(ctx
->ctf_tc
, stream_class
->id
)) {
3640 _BT_COMP_LOGE_APPEND_CAUSE_NODE(node
, "Duplicate stream class (same ID): id=%" PRId64
,
3646 g_ptr_array_add(ctx
->ctf_tc
->stream_classes
, stream_class
);
3647 stream_class
= NULL
;
3651 ctf_stream_class_destroy(stream_class
);
3652 stream_class
= NULL
;
3658 static int visit_trace_decl_entry(struct ctf_visitor_generate_ir
*ctx
, struct ctf_node
*node
,
3665 switch (node
->type
) {
3667 ret
= visit_field_class_def(ctx
, node
->u
.field_class_def
.field_class_specifier_list
,
3668 &node
->u
.field_class_def
.field_class_declarators
);
3670 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
3671 node
, "Cannot add field class found in trace (`trace` block).");
3675 case NODE_TYPEALIAS
:
3676 ret
= visit_field_class_alias(ctx
, node
->u
.field_class_alias
.target
,
3677 node
->u
.field_class_alias
.alias
);
3679 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
3680 node
, "Cannot add field class alias found in trace (`trace` block).");
3684 case NODE_CTF_EXPRESSION
:
3686 left
= ctf_ast_concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
3688 _BT_COMP_LOGE_APPEND_CAUSE_NODE(node
, "Cannot concatenate unary strings.");
3693 if (strcmp(left
, "major") == 0) {
3694 if (_IS_SET(set
, _TRACE_MAJOR_SET
)) {
3695 _BT_COMP_LOGE_APPEND_CAUSE_DUP_ATTR(node
, "major", "trace");
3700 ret
= get_unary_unsigned(ctx
, &node
->u
.ctf_expression
.right
, &val
);
3702 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
3703 node
, "Unexpected unary expression for trace's `major` attribute.");
3709 _BT_COMP_LOGE_APPEND_CAUSE_NODE(node
,
3710 "Invalid trace's `minor` attribute: expecting 1.");
3715 ctx
->ctf_tc
->major
= val
;
3716 _SET(set
, _TRACE_MAJOR_SET
);
3717 } else if (strcmp(left
, "minor") == 0) {
3718 if (_IS_SET(set
, _TRACE_MINOR_SET
)) {
3719 _BT_COMP_LOGE_APPEND_CAUSE_DUP_ATTR(node
, "minor", "trace");
3724 ret
= get_unary_unsigned(ctx
, &node
->u
.ctf_expression
.right
, &val
);
3726 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
3727 node
, "Unexpected unary expression for trace's `minor` attribute.");
3733 _BT_COMP_LOGE_APPEND_CAUSE_NODE(node
,
3734 "Invalid trace's `minor` attribute: expecting 8.");
3739 ctx
->ctf_tc
->minor
= val
;
3740 _SET(set
, _TRACE_MINOR_SET
);
3741 } else if (strcmp(left
, "uuid") == 0) {
3742 if (_IS_SET(set
, _TRACE_UUID_SET
)) {
3743 _BT_COMP_LOGE_APPEND_CAUSE_DUP_ATTR(node
, "uuid", "trace");
3748 ret
= get_unary_uuid(ctx
, &node
->u
.ctf_expression
.right
, ctx
->ctf_tc
->uuid
);
3750 _BT_COMP_LOGE_APPEND_CAUSE_NODE(node
, "Invalid trace's `uuid` attribute.");
3754 ctx
->ctf_tc
->is_uuid_set
= true;
3755 _SET(set
, _TRACE_UUID_SET
);
3756 } else if (strcmp(left
, "byte_order") == 0) {
3757 /* Default byte order is already known at this stage */
3758 if (_IS_SET(set
, _TRACE_BYTE_ORDER_SET
)) {
3759 _BT_COMP_LOGE_APPEND_CAUSE_DUP_ATTR(node
, "byte_order", "trace");
3764 BT_ASSERT(ctx
->ctf_tc
->default_byte_order
!= CTF_BYTE_ORDER_UNKNOWN
);
3765 _SET(set
, _TRACE_BYTE_ORDER_SET
);
3766 } else if (strcmp(left
, "packet.header") == 0) {
3767 if (_IS_SET(set
, _TRACE_PACKET_HEADER_SET
)) {
3768 _BT_COMP_LOGE_APPEND_CAUSE_NODE(node
, "Duplicate `packet.header` entry in trace.");
3773 ret
= visit_field_class_specifier_list(
3774 ctx
, _BT_LIST_FIRST_ENTRY(&node
->u
.ctf_expression
.right
, struct ctf_node
, siblings
),
3775 &ctx
->ctf_tc
->packet_header_fc
);
3777 _BT_COMP_LOGE_APPEND_CAUSE_NODE(node
,
3778 "Cannot create trace's packet header field class.");
3782 BT_ASSERT(ctx
->ctf_tc
->packet_header_fc
);
3783 _SET(set
, _TRACE_PACKET_HEADER_SET
);
3785 _BT_COMP_LOGW_NODE(node
,
3786 "Unknown attribute in stream class: "
3796 _BT_COMP_LOGE_APPEND_CAUSE_NODE(node
, "Unknown expression in trace.");
3808 static int visit_trace_decl(struct ctf_visitor_generate_ir
*ctx
, struct ctf_node
*node
)
3812 struct ctf_node
*iter
;
3813 struct bt_list_head
*decl_list
= &node
->u
.trace
.declaration_list
;
3815 if (node
->visited
) {
3819 node
->visited
= TRUE
;
3821 if (ctx
->is_trace_visited
) {
3822 _BT_COMP_LOGE_APPEND_CAUSE_NODE(node
, "Duplicate trace (`trace` block).");
3827 _TRY_PUSH_SCOPE_OR_GOTO_ERROR();
3829 bt_list_for_each_entry (iter
, decl_list
, siblings
) {
3830 ret
= visit_trace_decl_entry(ctx
, iter
, &set
);
3832 _BT_COMP_LOGE_APPEND_CAUSE_NODE(iter
,
3833 "Cannot visit trace's entry (`trace` block): "
3843 if (!_IS_SET(&set
, _TRACE_MAJOR_SET
)) {
3844 _BT_COMP_LOGE_APPEND_CAUSE_NODE(node
,
3845 "Missing `major` attribute in trace (`trace` block).");
3850 if (!_IS_SET(&set
, _TRACE_MINOR_SET
)) {
3851 _BT_COMP_LOGE_APPEND_CAUSE_NODE(node
,
3852 "Missing `minor` attribute in trace (`trace` block).");
3857 if (!_IS_SET(&set
, _TRACE_BYTE_ORDER_SET
)) {
3858 _BT_COMP_LOGE_APPEND_CAUSE_NODE(node
,
3859 "Missing `byte_order` attribute in trace (`trace` block).");
3864 ctx
->is_trace_visited
= true;
3873 static int visit_env(struct ctf_visitor_generate_ir
*ctx
, struct ctf_node
*node
)
3877 struct ctf_node
*entry_node
;
3878 struct bt_list_head
*decl_list
= &node
->u
.env
.declaration_list
;
3880 if (node
->visited
) {
3884 node
->visited
= TRUE
;
3886 bt_list_for_each_entry (entry_node
, decl_list
, siblings
) {
3887 struct bt_list_head
*right_head
= &entry_node
->u
.ctf_expression
.right
;
3889 if (entry_node
->type
!= NODE_CTF_EXPRESSION
) {
3890 _BT_COMP_LOGE_APPEND_CAUSE_NODE(entry_node
,
3891 "Wrong expression in environment entry: "
3898 left
= ctf_ast_concatenate_unary_strings(&entry_node
->u
.ctf_expression
.left
);
3900 _BT_COMP_LOGE_APPEND_CAUSE_NODE(entry_node
, "Cannot get environment entry's name.");
3905 if (is_unary_string(right_head
)) {
3906 char *right
= ctf_ast_concatenate_unary_strings(right_head
);
3909 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
3911 "Unexpected unary expression for environment entry's value: "
3918 if (strcmp(left
, "tracer_name") == 0) {
3919 if (strncmp(right
, "lttng", 5) == 0) {
3920 BT_COMP_LOGI("Detected LTTng trace from `%s` environment value: "
3921 "tracer-name=\"%s\"",
3923 ctx
->is_lttng
= true;
3927 ctf_trace_class_append_env_entry(ctx
->ctf_tc
, left
, CTF_TRACE_CLASS_ENV_ENTRY_TYPE_STR
,
3930 } else if (is_unary_unsigned(right_head
) || is_unary_signed(right_head
)) {
3933 if (is_unary_unsigned(right_head
)) {
3934 ret
= get_unary_unsigned(ctx
, right_head
, (uint64_t *) &v
);
3936 ret
= get_unary_signed(right_head
, &v
);
3939 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
3941 "Unexpected unary expression for environment entry's value: "
3948 ctf_trace_class_append_env_entry(ctx
->ctf_tc
, left
, CTF_TRACE_CLASS_ENV_ENTRY_TYPE_INT
,
3951 _BT_COMP_LOGW_NODE(entry_node
,
3952 "Environment entry has unknown type: "
3969 static int set_trace_byte_order(struct ctf_visitor_generate_ir
*ctx
, struct ctf_node
*trace_node
)
3974 struct ctf_node
*node
;
3975 struct bt_list_head
*decl_list
= &trace_node
->u
.trace
.declaration_list
;
3977 bt_list_for_each_entry (node
, decl_list
, siblings
) {
3978 if (node
->type
== NODE_CTF_EXPRESSION
) {
3979 struct ctf_node
*right_node
;
3981 left
= ctf_ast_concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
3983 _BT_COMP_LOGE_APPEND_CAUSE_NODE(node
, "Cannot concatenate unary strings.");
3988 if (strcmp(left
, "byte_order") == 0) {
3989 enum ctf_byte_order bo
;
3991 if (_IS_SET(&set
, _TRACE_BYTE_ORDER_SET
)) {
3992 _BT_COMP_LOGE_APPEND_CAUSE_DUP_ATTR(node
, "byte_order", "trace");
3997 _SET(&set
, _TRACE_BYTE_ORDER_SET
);
3999 _BT_LIST_FIRST_ENTRY(&node
->u
.ctf_expression
.right
, struct ctf_node
, siblings
);
4000 bo
= byte_order_from_unary_expr(ctx
, right_node
);
4001 if (bo
== CTF_BYTE_ORDER_UNKNOWN
) {
4002 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
4003 node
, "Invalid `byte_order` attribute in trace (`trace` block): "
4004 "expecting `le`, `be`, or `network`.");
4007 } else if (bo
== CTF_BYTE_ORDER_DEFAULT
) {
4008 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
4009 node
, "Invalid `byte_order` attribute in trace (`trace` block): "
4010 "cannot be set to `native` here.");
4015 ctx
->ctf_tc
->default_byte_order
= bo
;
4023 if (!_IS_SET(&set
, _TRACE_BYTE_ORDER_SET
)) {
4024 _BT_COMP_LOGE_APPEND_CAUSE_NODE(trace_node
,
4025 "Missing `byte_order` attribute in trace (`trace` block).");
4037 static int visit_clock_decl_entry(struct ctf_visitor_generate_ir
*ctx
, struct ctf_node
*entry_node
,
4038 struct ctf_clock_class
*clock
, int *set
, int64_t *offset_seconds
,
4039 uint64_t *offset_cycles
)
4044 if (entry_node
->type
!= NODE_CTF_EXPRESSION
) {
4045 _BT_COMP_LOGE_APPEND_CAUSE_NODE(entry_node
, "Unexpected node type: node-type=%d",
4051 left
= ctf_ast_concatenate_unary_strings(&entry_node
->u
.ctf_expression
.left
);
4053 _BT_COMP_LOGE_APPEND_CAUSE_NODE(entry_node
, "Cannot concatenate unary strings.");
4058 if (strcmp(left
, "name") == 0) {
4061 if (_IS_SET(set
, _CLOCK_NAME_SET
)) {
4062 _BT_COMP_LOGE_APPEND_CAUSE_DUP_ATTR(entry_node
, "name", "clock class");
4067 right
= ctf_ast_concatenate_unary_strings(&entry_node
->u
.ctf_expression
.right
);
4069 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
4070 entry_node
, "Unexpected unary expression for clock class's `name` attribute.");
4075 g_string_assign(clock
->name
, right
);
4077 _SET(set
, _CLOCK_NAME_SET
);
4078 } else if (strcmp(left
, "uuid") == 0) {
4081 if (_IS_SET(set
, _CLOCK_UUID_SET
)) {
4082 _BT_COMP_LOGE_APPEND_CAUSE_DUP_ATTR(entry_node
, "uuid", "clock class");
4087 ret
= get_unary_uuid(ctx
, &entry_node
->u
.ctf_expression
.right
, uuid
);
4089 _BT_COMP_LOGE_APPEND_CAUSE_NODE(entry_node
, "Invalid clock class's `uuid` attribute.");
4093 clock
->has_uuid
= true;
4094 bt_uuid_copy(clock
->uuid
, uuid
);
4095 _SET(set
, _CLOCK_UUID_SET
);
4096 } else if (strcmp(left
, "description") == 0) {
4099 if (_IS_SET(set
, _CLOCK_DESCRIPTION_SET
)) {
4100 _BT_COMP_LOGE_APPEND_CAUSE_DUP_ATTR(entry_node
, "description", "clock class");
4105 right
= ctf_ast_concatenate_unary_strings(&entry_node
->u
.ctf_expression
.right
);
4107 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
4109 "Unexpected unary expression for clock class's `description` attribute.");
4114 g_string_assign(clock
->description
, right
);
4116 _SET(set
, _CLOCK_DESCRIPTION_SET
);
4117 } else if (strcmp(left
, "freq") == 0) {
4118 uint64_t freq
= UINT64_C(-1);
4120 if (_IS_SET(set
, _CLOCK_FREQ_SET
)) {
4121 _BT_COMP_LOGE_APPEND_CAUSE_DUP_ATTR(entry_node
, "freq", "clock class");
4126 ret
= get_unary_unsigned(ctx
, &entry_node
->u
.ctf_expression
.right
, &freq
);
4128 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
4129 entry_node
, "Unexpected unary expression for clock class's `freq` attribute.");
4134 if (freq
== UINT64_C(-1) || freq
== 0) {
4135 _BT_COMP_LOGE_APPEND_CAUSE_NODE(entry_node
,
4136 "Invalid clock class frequency: freq=%" PRIu64
, freq
);
4141 clock
->frequency
= freq
;
4142 _SET(set
, _CLOCK_FREQ_SET
);
4143 } else if (strcmp(left
, "precision") == 0) {
4146 if (_IS_SET(set
, _CLOCK_PRECISION_SET
)) {
4147 _BT_COMP_LOGE_APPEND_CAUSE_DUP_ATTR(entry_node
, "precision", "clock class");
4152 ret
= get_unary_unsigned(ctx
, &entry_node
->u
.ctf_expression
.right
, &precision
);
4154 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
4155 entry_node
, "Unexpected unary expression for clock class's `precision` attribute.");
4160 clock
->precision
= precision
;
4161 _SET(set
, _CLOCK_PRECISION_SET
);
4162 } else if (strcmp(left
, "offset_s") == 0) {
4163 if (_IS_SET(set
, _CLOCK_OFFSET_S_SET
)) {
4164 _BT_COMP_LOGE_APPEND_CAUSE_DUP_ATTR(entry_node
, "offset_s", "clock class");
4169 ret
= get_unary_signed(&entry_node
->u
.ctf_expression
.right
, offset_seconds
);
4171 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
4172 entry_node
, "Unexpected unary expression for clock class's `offset_s` attribute.");
4177 _SET(set
, _CLOCK_OFFSET_S_SET
);
4178 } else if (strcmp(left
, "offset") == 0) {
4179 if (_IS_SET(set
, _CLOCK_OFFSET_SET
)) {
4180 _BT_COMP_LOGE_APPEND_CAUSE_DUP_ATTR(entry_node
, "offset", "clock class");
4185 ret
= get_unary_unsigned(ctx
, &entry_node
->u
.ctf_expression
.right
, offset_cycles
);
4187 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
4188 entry_node
, "Unexpected unary expression for clock class's `offset` attribute.");
4193 _SET(set
, _CLOCK_OFFSET_SET
);
4194 } else if (strcmp(left
, "absolute") == 0) {
4195 struct ctf_node
*right
;
4197 if (_IS_SET(set
, _CLOCK_ABSOLUTE_SET
)) {
4198 _BT_COMP_LOGE_APPEND_CAUSE_DUP_ATTR(entry_node
, "absolute", "clock class");
4204 _BT_LIST_FIRST_ENTRY(&entry_node
->u
.ctf_expression
.right
, struct ctf_node
, siblings
);
4205 ret
= get_boolean(ctx
, right
);
4207 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
4208 entry_node
, "Unexpected unary expression for clock class's `absolute` attribute.");
4213 clock
->is_absolute
= ret
;
4214 _SET(set
, _CLOCK_ABSOLUTE_SET
);
4216 _BT_COMP_LOGW_NODE(entry_node
, "Unknown attribute in clock class: attr-name=\"%s\"", left
);
4228 static inline uint64_t cycles_from_ns(uint64_t frequency
, uint64_t ns
)
4233 if (frequency
== UINT64_C(1000000000)) {
4236 cycles
= (uint64_t) (((double) ns
* (double) frequency
) / 1e9
);
4242 static void calibrate_clock_class_offsets(int64_t *offset_seconds
, uint64_t *offset_cycles
,
4245 if (*offset_cycles
>= freq
) {
4246 const uint64_t s_in_offset_cycles
= *offset_cycles
/ freq
;
4248 *offset_seconds
+= (int64_t) s_in_offset_cycles
;
4249 *offset_cycles
-= (s_in_offset_cycles
* freq
);
4253 static void apply_clock_class_is_absolute(struct ctf_visitor_generate_ir
*ctx
,
4254 struct ctf_clock_class
*clock
)
4256 if (ctx
->decoder_config
.force_clock_class_origin_unix_epoch
) {
4257 clock
->is_absolute
= true;
4263 static void apply_clock_class_offset(struct ctf_visitor_generate_ir
*ctx
,
4264 struct ctf_clock_class
*clock
)
4267 int64_t offset_s_to_apply
= ctx
->decoder_config
.clock_class_offset_s
;
4268 uint64_t offset_ns_to_apply
;
4269 int64_t cur_offset_s
;
4270 uint64_t cur_offset_cycles
;
4272 if (ctx
->decoder_config
.clock_class_offset_s
== 0 &&
4273 ctx
->decoder_config
.clock_class_offset_ns
== 0) {
4277 /* Transfer nanoseconds to seconds as much as possible */
4278 if (ctx
->decoder_config
.clock_class_offset_ns
< 0) {
4279 const int64_t abs_ns
= -ctx
->decoder_config
.clock_class_offset_ns
;
4280 const int64_t abs_extra_s
= abs_ns
/ INT64_C(1000000000) + 1;
4281 const int64_t extra_s
= -abs_extra_s
;
4282 const int64_t offset_ns
=
4283 ctx
->decoder_config
.clock_class_offset_ns
- (extra_s
* INT64_C(1000000000));
4285 BT_ASSERT(offset_ns
> 0);
4286 offset_ns_to_apply
= (uint64_t) offset_ns
;
4287 offset_s_to_apply
+= extra_s
;
4289 const int64_t extra_s
= ctx
->decoder_config
.clock_class_offset_ns
/ INT64_C(1000000000);
4290 const int64_t offset_ns
=
4291 ctx
->decoder_config
.clock_class_offset_ns
- (extra_s
* INT64_C(1000000000));
4293 BT_ASSERT(offset_ns
>= 0);
4294 offset_ns_to_apply
= (uint64_t) offset_ns
;
4295 offset_s_to_apply
+= extra_s
;
4298 freq
= clock
->frequency
;
4299 cur_offset_s
= clock
->offset_seconds
;
4300 cur_offset_cycles
= clock
->offset_cycles
;
4303 cur_offset_s
+= offset_s_to_apply
;
4304 cur_offset_cycles
+= cycles_from_ns(freq
, offset_ns_to_apply
);
4307 * Recalibrate offsets because the part in cycles can be greater
4308 * than the frequency at this point.
4310 calibrate_clock_class_offsets(&cur_offset_s
, &cur_offset_cycles
, freq
);
4312 /* Set final offsets */
4313 clock
->offset_seconds
= cur_offset_s
;
4314 clock
->offset_cycles
= cur_offset_cycles
;
4320 static int visit_clock_decl(struct ctf_visitor_generate_ir
*ctx
, struct ctf_node
*clock_node
)
4324 struct ctf_clock_class
*clock
;
4325 struct ctf_node
*entry_node
;
4326 struct bt_list_head
*decl_list
= &clock_node
->u
.clock
.declaration_list
;
4327 const char *clock_class_name
;
4328 int64_t offset_seconds
= 0;
4329 uint64_t offset_cycles
= 0;
4332 if (clock_node
->visited
) {
4336 clock_node
->visited
= TRUE
;
4338 /* CTF 1.8's default frequency for a clock class is 1 GHz */
4339 clock
= ctf_clock_class_create();
4341 _BT_COMP_LOGE_APPEND_CAUSE_NODE(clock_node
, "Cannot create default clock class.");
4346 bt_list_for_each_entry (entry_node
, decl_list
, siblings
) {
4347 ret
= visit_clock_decl_entry(ctx
, entry_node
, clock
, &set
, &offset_seconds
, &offset_cycles
);
4349 _BT_COMP_LOGE_APPEND_CAUSE_NODE(entry_node
, "Cannot visit clock class's entry: ret=%d",
4355 if (!_IS_SET(&set
, _CLOCK_NAME_SET
)) {
4356 _BT_COMP_LOGE_APPEND_CAUSE_NODE(clock_node
, "Missing `name` attribute in clock class.");
4361 clock_class_name
= clock
->name
->str
;
4362 BT_ASSERT(clock_class_name
);
4363 if (ctx
->is_lttng
&& strcmp(clock_class_name
, "monotonic") == 0) {
4365 * Old versions of LTTng forgot to set its clock class
4366 * as absolute, even if it is. This is important because
4367 * it's a condition to be able to sort messages
4368 * from different sources.
4370 clock
->is_absolute
= true;
4374 * Adjust offsets so that the part in cycles is less than the
4375 * frequency (move to the part in seconds).
4377 freq
= clock
->frequency
;
4378 calibrate_clock_class_offsets(&offset_seconds
, &offset_cycles
, freq
);
4379 BT_ASSERT(offset_cycles
< clock
->frequency
);
4380 clock
->offset_seconds
= offset_seconds
;
4381 clock
->offset_cycles
= offset_cycles
;
4382 apply_clock_class_offset(ctx
, clock
);
4383 apply_clock_class_is_absolute(ctx
, clock
);
4384 g_ptr_array_add(ctx
->ctf_tc
->clock_classes
, clock
);
4389 ctf_clock_class_destroy(clock
);
4395 static int visit_root_decl(struct ctf_visitor_generate_ir
*ctx
, struct ctf_node
*root_decl_node
)
4399 if (root_decl_node
->visited
) {
4403 root_decl_node
->visited
= TRUE
;
4405 switch (root_decl_node
->type
) {
4408 visit_field_class_def(ctx
, root_decl_node
->u
.field_class_def
.field_class_specifier_list
,
4409 &root_decl_node
->u
.field_class_def
.field_class_declarators
);
4411 _BT_COMP_LOGE_APPEND_CAUSE_NODE(root_decl_node
,
4412 "Cannot add field class found in root scope.");
4416 case NODE_TYPEALIAS
:
4417 ret
= visit_field_class_alias(ctx
, root_decl_node
->u
.field_class_alias
.target
,
4418 root_decl_node
->u
.field_class_alias
.alias
);
4420 _BT_COMP_LOGE_APPEND_CAUSE_NODE(root_decl_node
,
4421 "Cannot add field class alias found in root scope.");
4425 case NODE_TYPE_SPECIFIER_LIST
:
4427 struct ctf_field_class
*decl
= NULL
;
4430 * Just add the field class specifier to the root
4431 * declaration scope. Put local reference.
4433 ret
= visit_field_class_specifier_list(ctx
, root_decl_node
, &decl
);
4435 _BT_COMP_LOGE_APPEND_CAUSE_NODE(root_decl_node
,
4436 "Cannot visit root scope's field class: "
4443 ctf_field_class_destroy(decl
);
4448 _BT_COMP_LOGE_APPEND_CAUSE_NODE(root_decl_node
, "Unexpected node type: node-type=%d",
4449 root_decl_node
->type
);
4458 struct ctf_visitor_generate_ir
*
4459 ctf_visitor_generate_ir_create(const struct ctf_metadata_decoder_config
*decoder_config
)
4461 struct ctf_visitor_generate_ir
*ctx
= NULL
;
4463 /* Create visitor's context */
4464 ctx
= ctx_create(decoder_config
);
4466 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR
, decoder_config
->log_level
, decoder_config
->self_comp
,
4467 "Cannot create visitor's context.");
4481 void ctf_visitor_generate_ir_destroy(struct ctf_visitor_generate_ir
*visitor
)
4483 ctx_destroy(visitor
);
4486 bt_trace_class
*ctf_visitor_generate_ir_get_ir_trace_class(struct ctf_visitor_generate_ir
*ctx
)
4490 if (ctx
->trace_class
) {
4491 bt_trace_class_get_ref(ctx
->trace_class
);
4494 return ctx
->trace_class
;
4497 struct ctf_trace_class
*
4498 ctf_visitor_generate_ir_borrow_ctf_trace_class(struct ctf_visitor_generate_ir
*ctx
)
4501 BT_ASSERT_DBG(ctx
->ctf_tc
);
4505 int ctf_visitor_generate_ir_visit_node(struct ctf_visitor_generate_ir
*ctx
, struct ctf_node
*node
)
4509 BT_COMP_LOGI_STR("Visiting metadata's AST to generate CTF IR objects.");
4511 switch (node
->type
) {
4514 struct ctf_node
*iter
;
4515 bool got_trace_decl
= false;
4518 * The first thing we need is the native byte order of
4519 * the trace block, because early class aliases can have
4520 * a `byte_order` attribute set to `native`. If we don't
4521 * have the native byte order yet, and we don't have any
4522 * trace block yet, then fail with EINCOMPLETE.
4524 if (ctx
->ctf_tc
->default_byte_order
== CTF_BYTE_ORDER_UNKNOWN
) {
4525 bt_list_for_each_entry (iter
, &node
->u
.root
.trace
, siblings
) {
4526 if (got_trace_decl
) {
4527 _BT_COMP_LOGE_APPEND_CAUSE_NODE(node
, "Duplicate trace (`trace` block).");
4532 ret
= set_trace_byte_order(ctx
, iter
);
4534 _BT_COMP_LOGE_APPEND_CAUSE_NODE(node
,
4535 "Cannot set trace's native byte order: "
4541 got_trace_decl
= true;
4544 if (!got_trace_decl
) {
4545 BT_COMP_LOGD_STR("Incomplete AST: need trace (`trace` block).");
4551 BT_ASSERT(ctx
->ctf_tc
->default_byte_order
== CTF_BYTE_ORDER_LITTLE
||
4552 ctx
->ctf_tc
->default_byte_order
== CTF_BYTE_ORDER_BIG
);
4553 BT_ASSERT(ctx
->current_scope
&& !ctx
->current_scope
->parent_scope
);
4556 bt_list_for_each_entry (iter
, &node
->u
.root
.env
, siblings
) {
4557 ret
= visit_env(ctx
, iter
);
4559 _BT_COMP_LOGE_APPEND_CAUSE_NODE(
4561 "Cannot visit trace's environment (`env` block) entry: "
4568 BT_ASSERT(ctx
->current_scope
&& !ctx
->current_scope
->parent_scope
);
4571 * Visit clock blocks.
4573 bt_list_for_each_entry (iter
, &node
->u
.root
.clock
, siblings
) {
4574 ret
= visit_clock_decl(ctx
, iter
);
4576 _BT_COMP_LOGE_APPEND_CAUSE_NODE(iter
, "Cannot visit clock class: ret=%d", ret
);
4581 BT_ASSERT(ctx
->current_scope
&& !ctx
->current_scope
->parent_scope
);
4584 * Visit root declarations next, as they can be used by any
4587 bt_list_for_each_entry (iter
, &node
->u
.root
.declaration_list
, siblings
) {
4588 ret
= visit_root_decl(ctx
, iter
);
4590 _BT_COMP_LOGE_APPEND_CAUSE_NODE(iter
, "Cannot visit root entry: ret=%d", ret
);
4595 BT_ASSERT(ctx
->current_scope
&& !ctx
->current_scope
->parent_scope
);
4597 /* Callsite blocks are not supported */
4598 bt_list_for_each_entry (iter
, &node
->u
.root
.callsite
, siblings
) {
4599 _BT_COMP_LOGW_NODE(iter
, "\"callsite\" blocks are not supported as of this version.");
4602 BT_ASSERT(ctx
->current_scope
&& !ctx
->current_scope
->parent_scope
);
4605 bt_list_for_each_entry (iter
, &node
->u
.root
.trace
, siblings
) {
4606 ret
= visit_trace_decl(ctx
, iter
);
4608 _BT_COMP_LOGE_APPEND_CAUSE_NODE(iter
,
4609 "Cannot visit trace (`trace` block): "
4616 BT_ASSERT(ctx
->current_scope
&& !ctx
->current_scope
->parent_scope
);
4619 bt_list_for_each_entry (iter
, &node
->u
.root
.stream
, siblings
) {
4620 ret
= visit_stream_decl(ctx
, iter
);
4622 _BT_COMP_LOGE_APPEND_CAUSE_NODE(iter
, "Cannot visit stream class: ret=%d", ret
);
4627 BT_ASSERT(ctx
->current_scope
&& !ctx
->current_scope
->parent_scope
);
4630 bt_list_for_each_entry (iter
, &node
->u
.root
.event
, siblings
) {
4631 ret
= visit_event_decl(ctx
, iter
);
4633 _BT_COMP_LOGE_APPEND_CAUSE_NODE(iter
, "Cannot visit event class: ret=%d", ret
);
4638 BT_ASSERT(ctx
->current_scope
&& !ctx
->current_scope
->parent_scope
);
4642 _BT_COMP_LOGE_APPEND_CAUSE_NODE(node
, "Unexpected node type: node-type=%d", node
->type
);
4647 /* Update default clock classes */
4648 ret
= ctf_trace_class_update_default_clock_classes(ctx
->ctf_tc
, &ctx
->log_cfg
);
4654 /* Update trace class meanings */
4655 ret
= ctf_trace_class_update_meanings(ctx
->ctf_tc
);
4661 /* Update stream class configuration */
4662 ret
= ctf_trace_class_update_stream_class_config(ctx
->ctf_tc
);
4668 /* Update text arrays and sequences */
4669 ret
= ctf_trace_class_update_text_array_sequence(ctx
->ctf_tc
);
4675 /* Update structure/array/sequence alignments */
4676 ret
= ctf_trace_class_update_alignments(ctx
->ctf_tc
);
4682 /* Resolve sequence lengths and variant tags */
4683 ret
= ctf_trace_class_resolve_field_classes(ctx
->ctf_tc
, &ctx
->log_cfg
);
4689 if (ctx
->trace_class
) {
4691 * Update "in IR" for field classes.
4693 * If we have no IR trace class, then we'll have no way
4694 * to create IR fields anyway, so we leave all the
4695 * `in_ir` members false.
4697 ret
= ctf_trace_class_update_in_ir(ctx
->ctf_tc
);
4704 /* Update saved value indexes */
4705 ret
= ctf_trace_class_update_value_storing_indexes(ctx
->ctf_tc
);
4711 /* Validate what we have so far */
4712 ret
= ctf_trace_class_validate(ctx
->ctf_tc
, &ctx
->log_cfg
);
4719 * If there are fields which are not related to the CTF format
4720 * itself in the packet header and in event header field
4721 * classes, warn about it because they are never translated.
4723 ctf_trace_class_warn_meaningless_header_fields(ctx
->ctf_tc
, &ctx
->log_cfg
);
4725 if (ctx
->trace_class
) {
4726 /* Copy new CTF metadata -> new IR metadata */
4727 ret
= ctf_trace_class_translate(ctx
->log_cfg
.self_comp
, ctx
->trace_class
, ctx
->ctf_tc
);