2 * ctf-visitor-generate-ir.c
4 * Common Trace Format metadata visitor (generates CTF IR objects).
6 * Based on older ctf-visitor-generate-io-struct.c.
8 * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
9 * Copyright 2015-2016 - Philippe Proulx <philippe.proulx@efficios.com>
11 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 #define BT_LOG_TAG "PLUGIN-CTF-METADATA-IR-VISITOR"
43 #include <babeltrace/compat/uuid-internal.h>
44 #include <babeltrace/endian-internal.h>
45 #include <babeltrace/babeltrace.h>
46 #include <babeltrace/ctf-ir/field-types-internal.h>
53 /* Bit value (left shift) */
54 #define _BV(_val) (1 << (_val))
56 /* Bit is set in a set of bits */
57 #define _IS_SET(_set, _mask) (*(_set) & (_mask))
59 /* Set bit in a set of bits */
60 #define _SET(_set, _mask) (*(_set) |= (_mask))
62 /* Bits for verifying existing attributes in various declarations */
64 _CLOCK_NAME_SET
= _BV(0),
65 _CLOCK_UUID_SET
= _BV(1),
66 _CLOCK_FREQ_SET
= _BV(2),
67 _CLOCK_PRECISION_SET
= _BV(3),
68 _CLOCK_OFFSET_S_SET
= _BV(4),
69 _CLOCK_OFFSET_SET
= _BV(5),
70 _CLOCK_ABSOLUTE_SET
= _BV(6),
71 _CLOCK_DESCRIPTION_SET
= _BV(7),
75 _INTEGER_ALIGN_SET
= _BV(0),
76 _INTEGER_SIZE_SET
= _BV(1),
77 _INTEGER_BASE_SET
= _BV(2),
78 _INTEGER_ENCODING_SET
= _BV(3),
79 _INTEGER_BYTE_ORDER_SET
= _BV(4),
80 _INTEGER_SIGNED_SET
= _BV(5),
81 _INTEGER_MAP_SET
= _BV(6),
85 _FLOAT_ALIGN_SET
= _BV(0),
86 _FLOAT_MANT_DIG_SET
= _BV(1),
87 _FLOAT_EXP_DIG_SET
= _BV(2),
88 _FLOAT_BYTE_ORDER_SET
= _BV(3),
92 _STRING_ENCODING_SET
= _BV(0),
96 _TRACE_MINOR_SET
= _BV(0),
97 _TRACE_MAJOR_SET
= _BV(1),
98 _TRACE_BYTE_ORDER_SET
= _BV(2),
99 _TRACE_UUID_SET
= _BV(3),
100 _TRACE_PACKET_HEADER_SET
= _BV(4),
104 _STREAM_ID_SET
= _BV(0),
105 _STREAM_PACKET_CONTEXT_SET
= _BV(1),
106 _STREAM_EVENT_HEADER_SET
= _BV(2),
107 _STREAM_EVENT_CONTEXT_SET
= _BV(3),
111 _EVENT_NAME_SET
= _BV(0),
112 _EVENT_ID_SET
= _BV(1),
113 _EVENT_MODEL_EMF_URI_SET
= _BV(2),
114 _EVENT_STREAM_ID_SET
= _BV(3),
115 _EVENT_LOGLEVEL_SET
= _BV(4),
116 _EVENT_CONTEXT_SET
= _BV(5),
117 _EVENT_FIELDS_SET
= _BV(6),
125 LOGLEVEL_WARNING
= 4,
128 LOGLEVEL_DEBUG_SYSTEM
= 7,
129 LOGLEVEL_DEBUG_PROGRAM
= 8,
130 LOGLEVEL_DEBUG_PROCESS
= 9,
131 LOGLEVEL_DEBUG_MODULE
= 10,
132 LOGLEVEL_DEBUG_UNIT
= 11,
133 LOGLEVEL_DEBUG_FUNCTION
= 12,
134 LOGLEVEL_DEBUG_LINE
= 13,
139 /* Prefixes of type aliases */
140 #define _PREFIX_ALIAS 'a'
141 #define _PREFIX_ENUM 'e'
142 #define _PREFIX_STRUCT 's'
143 #define _PREFIX_VARIANT 'v'
145 /* First entry in a BT list */
146 #define _BT_LIST_FIRST_ENTRY(_ptr, _type, _member) \
147 bt_list_entry((_ptr)->next, _type, _member)
149 #define _BT_CTF_FIELD_TYPE_INIT(_name) struct bt_ctf_field_type *_name = NULL;
151 #define _BT_LOGE_DUP_ATTR(_node, _attr, _entity) \
152 _BT_LOGE_LINENO((_node)->lineno, \
153 "Duplicate attribute in %s: attr-name=\"%s\"", \
156 #define _BT_LOGE_NODE(_node, _msg, args...) \
157 _BT_LOGE_LINENO((_node)->lineno, _msg, ## args)
159 #define _BT_LOGW_NODE(_node, _msg, args...) \
160 _BT_LOGW_LINENO((_node)->lineno, _msg, ## args)
162 #define _BT_LOGV_NODE(_node, _msg, args...) \
163 _BT_LOGV_LINENO((_node)->lineno, _msg, ## args)
166 * Declaration scope of a visitor context. This represents a TSDL
167 * lexical scope, so that aliases and named structures, variants,
168 * and enumerations may be registered and looked up hierarchically.
170 struct ctx_decl_scope
{
172 * Alias name to field type.
174 * GQuark -> struct bt_ctf_field_type *
176 GHashTable
*decl_map
;
178 /* Parent scope; NULL if this is the root declaration scope */
179 struct ctx_decl_scope
*parent_scope
;
183 * Visitor context (private).
186 /* Trace being filled (owned by this) */
187 struct bt_ctf_trace
*trace
;
189 /* Current declaration scope (top of the stack) */
190 struct ctx_decl_scope
*current_scope
;
192 /* 1 if trace declaration is visited */
193 int is_trace_visited
;
195 /* 1 if this is an LTTng trace */
198 /* Eventual name suffix of the trace to set */
199 char *trace_name_suffix
;
201 /* Trace attributes */
202 enum bt_ctf_byte_order trace_bo
;
203 uint64_t trace_major
;
204 uint64_t trace_minor
;
205 unsigned char trace_uuid
[BABELTRACE_UUID_LEN
];
208 * Stream IDs to stream classes.
210 * int64_t -> struct bt_ctf_stream_class *
212 GHashTable
*stream_classes
;
214 /* Config passed by the user */
215 struct ctf_metadata_decoder_config decoder_config
;
221 struct ctf_visitor_generate_ir
{ };
224 * Creates a new declaration scope.
226 * @param par_scope Parent scope (NULL if creating a root scope)
227 * @returns New declaration scope, or NULL on error
230 struct ctx_decl_scope
*ctx_decl_scope_create(struct ctx_decl_scope
*par_scope
)
232 struct ctx_decl_scope
*scope
;
234 scope
= g_new(struct ctx_decl_scope
, 1);
236 BT_LOGE_STR("Failed to allocate one declaration scope.");
240 scope
->decl_map
= g_hash_table_new_full(g_direct_hash
, g_direct_equal
,
241 NULL
, (GDestroyNotify
) bt_ctf_field_type_put
);
242 scope
->parent_scope
= par_scope
;
249 * Destroys a declaration scope.
251 * This function does not destroy the parent scope.
253 * @param scope Scope to destroy
256 void ctx_decl_scope_destroy(struct ctx_decl_scope
*scope
)
262 g_hash_table_destroy(scope
->decl_map
);
270 * Returns the GQuark of a prefixed alias.
272 * @param prefix Prefix character
274 * @returns Associated GQuark, or 0 on error
277 GQuark
get_prefixed_named_quark(char prefix
, const char *name
)
283 /* Prefix character + original string + '\0' */
284 char *prname
= g_new(char, strlen(name
) + 2);
286 BT_LOGE_STR("Failed to allocate a string.");
290 sprintf(prname
, "%c%s", prefix
, name
);
291 qname
= g_quark_from_string(prname
);
299 * Looks up a prefixed type alias within a declaration scope.
301 * @param scope Declaration scope
302 * @param prefix Prefix character
303 * @param name Alias name
304 * @param level Number of levels to dig (-1 means infinite)
305 * @returns Declaration, or NULL if not found
308 struct bt_ctf_field_type
*ctx_decl_scope_lookup_prefix_alias(
309 struct ctx_decl_scope
*scope
, char prefix
,
310 const char *name
, int levels
)
314 _BT_CTF_FIELD_TYPE_INIT(decl
);
315 struct ctx_decl_scope
*cur_scope
= scope
;
319 qname
= get_prefixed_named_quark(prefix
, name
);
328 while (cur_scope
&& cur_levels
< levels
) {
329 decl
= g_hash_table_lookup(cur_scope
->decl_map
,
330 (gconstpointer
) GUINT_TO_POINTER(qname
));
332 /* Caller's reference */
337 cur_scope
= cur_scope
->parent_scope
;
348 * Looks up a type alias within a declaration scope.
350 * @param scope Declaration scope
351 * @param name Alias name
352 * @param level Number of levels to dig (-1 means infinite)
353 * @returns Declaration, or NULL if not found
356 struct bt_ctf_field_type
*ctx_decl_scope_lookup_alias(
357 struct ctx_decl_scope
*scope
, const char *name
, int levels
)
359 return ctx_decl_scope_lookup_prefix_alias(scope
, _PREFIX_ALIAS
,
364 * Looks up an enumeration within a declaration scope.
366 * @param scope Declaration scope
367 * @param name Enumeration name
368 * @param level Number of levels to dig (-1 means infinite)
369 * @returns Declaration, or NULL if not found
372 struct bt_ctf_field_type
*ctx_decl_scope_lookup_enum(
373 struct ctx_decl_scope
*scope
, const char *name
, int levels
)
375 return ctx_decl_scope_lookup_prefix_alias(scope
, _PREFIX_ENUM
,
380 * Looks up a structure within a declaration scope.
382 * @param scope Declaration scope
383 * @param name Structure name
384 * @param level Number of levels to dig (-1 means infinite)
385 * @returns Declaration, or NULL if not found
388 struct bt_ctf_field_type
*ctx_decl_scope_lookup_struct(
389 struct ctx_decl_scope
*scope
, const char *name
, int levels
)
391 return ctx_decl_scope_lookup_prefix_alias(scope
, _PREFIX_STRUCT
,
396 * Looks up a variant within a declaration scope.
398 * @param scope Declaration scope
399 * @param name Variant name
400 * @param level Number of levels to dig (-1 means infinite)
401 * @returns Declaration, or NULL if not found
404 struct bt_ctf_field_type
*ctx_decl_scope_lookup_variant(
405 struct ctx_decl_scope
*scope
, const char *name
, int levels
)
407 return ctx_decl_scope_lookup_prefix_alias(scope
, _PREFIX_VARIANT
,
412 * Registers a prefixed type alias within a declaration scope.
414 * @param scope Declaration scope
415 * @param prefix Prefix character
416 * @param name Alias name (non-NULL)
417 * @param decl Declaration to register
418 * @returns 0 if registration went okay, negative value otherwise
421 int ctx_decl_scope_register_prefix_alias(struct ctx_decl_scope
*scope
,
422 char prefix
, const char *name
, struct bt_ctf_field_type
*decl
)
426 _BT_CTF_FIELD_TYPE_INIT(edecl
);
431 qname
= get_prefixed_named_quark(prefix
, name
);
437 /* Make sure alias does not exist in local scope */
438 edecl
= ctx_decl_scope_lookup_prefix_alias(scope
, prefix
, name
, 1);
445 g_hash_table_insert(scope
->decl_map
,
446 GUINT_TO_POINTER(qname
), decl
);
448 /* Hash table's reference */
458 * Registers a type alias within a declaration scope.
460 * @param scope Declaration scope
461 * @param name Alias name (non-NULL)
462 * @param decl Declaration to register
463 * @returns 0 if registration went okay, negative value otherwise
466 int ctx_decl_scope_register_alias(struct ctx_decl_scope
*scope
,
467 const char *name
, struct bt_ctf_field_type
*decl
)
469 return ctx_decl_scope_register_prefix_alias(scope
, _PREFIX_ALIAS
,
474 * Registers an enumeration declaration within a declaration scope.
476 * @param scope Declaration scope
477 * @param name Enumeration name (non-NULL)
478 * @param decl Enumeration declaration to register
479 * @returns 0 if registration went okay, negative value otherwise
482 int ctx_decl_scope_register_enum(struct ctx_decl_scope
*scope
,
483 const char *name
, struct bt_ctf_field_type
*decl
)
485 return ctx_decl_scope_register_prefix_alias(scope
, _PREFIX_ENUM
,
490 * Registers a structure declaration within a declaration scope.
492 * @param scope Declaration scope
493 * @param name Structure name (non-NULL)
494 * @param decl Structure declaration to register
495 * @returns 0 if registration went okay, negative value otherwise
498 int ctx_decl_scope_register_struct(struct ctx_decl_scope
*scope
,
499 const char *name
, struct bt_ctf_field_type
*decl
)
501 return ctx_decl_scope_register_prefix_alias(scope
, _PREFIX_STRUCT
,
506 * Registers a variant declaration within a declaration scope.
508 * @param scope Declaration scope
509 * @param name Variant name (non-NULL)
510 * @param decl Variant declaration to register
511 * @returns 0 if registration went okay, negative value otherwise
514 int ctx_decl_scope_register_variant(struct ctx_decl_scope
*scope
,
515 const char *name
, struct bt_ctf_field_type
*decl
)
517 return ctx_decl_scope_register_prefix_alias(scope
, _PREFIX_VARIANT
,
522 * Destroys a visitor context.
524 * @param ctx Visitor context to destroy
527 void ctx_destroy(struct ctx
*ctx
)
529 struct ctx_decl_scope
*scope
;
531 * Destroy all scopes, from current one to the root scope.
538 scope
= ctx
->current_scope
;
541 struct ctx_decl_scope
*parent_scope
= scope
->parent_scope
;
543 ctx_decl_scope_destroy(scope
);
544 scope
= parent_scope
;
549 if (ctx
->stream_classes
) {
550 g_hash_table_destroy(ctx
->stream_classes
);
553 free(ctx
->trace_name_suffix
);
561 * Creates a new visitor context.
563 * @param trace Associated trace
564 * @returns New visitor context, or NULL on error
567 struct ctx
*ctx_create(struct bt_ctf_trace
*trace
,
568 const struct ctf_metadata_decoder_config
*decoder_config
,
569 const char *trace_name_suffix
)
571 struct ctx
*ctx
= NULL
;
572 struct ctx_decl_scope
*scope
= NULL
;
574 assert(decoder_config
);
576 ctx
= g_new0(struct ctx
, 1);
578 BT_LOGE_STR("Failed to allocate one visitor context.");
582 /* Root declaration scope */
583 scope
= ctx_decl_scope_create(NULL
);
585 BT_LOGE_STR("Cannot create declaration scope.");
589 ctx
->stream_classes
= g_hash_table_new_full(g_int64_hash
,
590 g_int64_equal
, g_free
, (GDestroyNotify
) bt_put
);
591 if (!ctx
->stream_classes
) {
592 BT_LOGE_STR("Failed to allocate a GHashTable.");
596 if (trace_name_suffix
) {
597 ctx
->trace_name_suffix
= strdup(trace_name_suffix
);
598 if (!ctx
->trace_name_suffix
) {
599 BT_LOGE_STR("Failed to copy string.");
605 ctx
->current_scope
= scope
;
607 ctx
->trace_bo
= BT_CTF_BYTE_ORDER_NATIVE
;
608 ctx
->decoder_config
= *decoder_config
;
613 ctx_decl_scope_destroy(scope
);
618 * Pushes a new declaration scope on top of a visitor context's
619 * declaration scope stack.
621 * @param ctx Visitor context
622 * @returns 0 on success, or a negative value on error
625 int ctx_push_scope(struct ctx
*ctx
)
628 struct ctx_decl_scope
*new_scope
;
631 new_scope
= ctx_decl_scope_create(ctx
->current_scope
);
633 BT_LOGE_STR("Cannot create declaration scope.");
638 ctx
->current_scope
= new_scope
;
645 void ctx_pop_scope(struct ctx
*ctx
)
647 struct ctx_decl_scope
*parent_scope
= NULL
;
651 if (!ctx
->current_scope
) {
655 parent_scope
= ctx
->current_scope
->parent_scope
;
656 ctx_decl_scope_destroy(ctx
->current_scope
);
657 ctx
->current_scope
= parent_scope
;
664 int visit_type_specifier_list(struct ctx
*ctx
, struct ctf_node
*ts_list
,
665 struct bt_ctf_field_type
**decl
);
668 int is_unary_string(struct bt_list_head
*head
)
671 struct ctf_node
*node
;
673 bt_list_for_each_entry(node
, head
, siblings
) {
674 if (node
->type
!= NODE_UNARY_EXPRESSION
) {
678 if (node
->u
.unary_expression
.type
!= UNARY_STRING
) {
687 char *concatenate_unary_strings(struct bt_list_head
*head
)
691 struct ctf_node
*node
;
693 str
= g_string_new(NULL
);
696 bt_list_for_each_entry(node
, head
, siblings
) {
700 node
->type
!= NODE_UNARY_EXPRESSION
||
701 node
->u
.unary_expression
.type
!= UNARY_STRING
||
704 node
->u
.unary_expression
.link
!=
712 switch (node
->u
.unary_expression
.link
) {
714 g_string_append(str
, ".");
716 case UNARY_ARROWLINK
:
717 g_string_append(str
, "->");
719 case UNARY_DOTDOTDOT
:
720 g_string_append(str
, "...");
726 src_string
= node
->u
.unary_expression
.u
.string
;
727 g_string_append(str
, src_string
);
731 /* Destroys the container, returns the underlying string */
732 return g_string_free(str
, FALSE
);
735 /* This always returns NULL */
736 return g_string_free(str
, TRUE
);
740 const char *get_map_clock_name_value(struct bt_list_head
*head
)
743 struct ctf_node
*node
;
744 const char *name
= NULL
;
746 bt_list_for_each_entry(node
, head
, siblings
) {
748 int uexpr_type
= node
->u
.unary_expression
.type
;
749 int uexpr_link
= node
->u
.unary_expression
.link
;
750 int cond
= node
->type
!= NODE_UNARY_EXPRESSION
||
751 uexpr_type
!= UNARY_STRING
||
752 !((uexpr_link
!= UNARY_LINK_UNKNOWN
) ^ (i
== 0));
757 /* Needs to be chained with . */
758 switch (node
->u
.unary_expression
.link
) {
761 case UNARY_ARROWLINK
:
762 case UNARY_DOTDOTDOT
:
768 src_string
= node
->u
.unary_expression
.u
.string
;
772 if (strcmp("clock", src_string
)) {
780 if (strcmp("value", src_string
)) {
785 /* Extra identifier, unknown */
799 int is_unary_unsigned(struct bt_list_head
*head
)
802 struct ctf_node
*node
;
804 bt_list_for_each_entry(node
, head
, siblings
) {
805 if (node
->type
!= NODE_UNARY_EXPRESSION
) {
809 if (node
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
818 int get_unary_unsigned(struct bt_list_head
*head
, uint64_t *value
)
822 struct ctf_node
*node
;
824 if (bt_list_empty(head
)) {
829 bt_list_for_each_entry(node
, head
, siblings
) {
830 int uexpr_type
= node
->u
.unary_expression
.type
;
831 int uexpr_link
= node
->u
.unary_expression
.link
;
832 int cond
= node
->type
!= NODE_UNARY_EXPRESSION
||
833 uexpr_type
!= UNARY_UNSIGNED_CONSTANT
||
834 uexpr_link
!= UNARY_LINK_UNKNOWN
|| i
!= 0;
836 _BT_LOGE_NODE(node
, "Invalid constant unsigned integer.");
841 *value
= node
->u
.unary_expression
.u
.unsigned_constant
;
850 int is_unary_signed(struct bt_list_head
*head
)
853 struct ctf_node
*node
;
855 bt_list_for_each_entry(node
, head
, siblings
) {
856 if (node
->type
!= NODE_UNARY_EXPRESSION
) {
860 if (node
->u
.unary_expression
.type
!= UNARY_SIGNED_CONSTANT
) {
869 int get_unary_signed(struct bt_list_head
*head
, int64_t *value
)
873 struct ctf_node
*node
;
875 bt_list_for_each_entry(node
, head
, siblings
) {
876 int uexpr_type
= node
->u
.unary_expression
.type
;
877 int uexpr_link
= node
->u
.unary_expression
.link
;
878 int cond
= node
->type
!= NODE_UNARY_EXPRESSION
||
879 (uexpr_type
!= UNARY_UNSIGNED_CONSTANT
&&
880 uexpr_type
!= UNARY_SIGNED_CONSTANT
) ||
881 uexpr_link
!= UNARY_LINK_UNKNOWN
|| i
!= 0;
887 switch (uexpr_type
) {
888 case UNARY_UNSIGNED_CONSTANT
:
890 node
->u
.unary_expression
.u
.unsigned_constant
;
892 case UNARY_SIGNED_CONSTANT
:
893 *value
= node
->u
.unary_expression
.u
.signed_constant
;
908 int get_unary_uuid(struct bt_list_head
*head
, unsigned char *uuid
)
912 struct ctf_node
*node
;
914 bt_list_for_each_entry(node
, head
, siblings
) {
915 int uexpr_type
= node
->u
.unary_expression
.type
;
916 int uexpr_link
= node
->u
.unary_expression
.link
;
917 const char *src_string
;
919 if (node
->type
!= NODE_UNARY_EXPRESSION
||
920 uexpr_type
!= UNARY_STRING
||
921 uexpr_link
!= UNARY_LINK_UNKNOWN
||
927 src_string
= node
->u
.unary_expression
.u
.string
;
928 ret
= bt_uuid_parse(src_string
, uuid
);
931 "Cannot parse UUID: uuid=\"%s\"", src_string
);
941 int get_boolean(struct ctf_node
*unary_expr
)
945 if (unary_expr
->type
!= NODE_UNARY_EXPRESSION
) {
946 _BT_LOGE_NODE(unary_expr
,
947 "Expecting unary expression: node-type=%d",
953 switch (unary_expr
->u
.unary_expression
.type
) {
954 case UNARY_UNSIGNED_CONSTANT
:
955 ret
= (unary_expr
->u
.unary_expression
.u
.unsigned_constant
!= 0);
957 case UNARY_SIGNED_CONSTANT
:
958 ret
= (unary_expr
->u
.unary_expression
.u
.signed_constant
!= 0);
962 const char *str
= unary_expr
->u
.unary_expression
.u
.string
;
964 if (!strcmp(str
, "true") || !strcmp(str
, "TRUE")) {
966 } else if (!strcmp(str
, "false") || !strcmp(str
, "FALSE")) {
969 _BT_LOGE_NODE(unary_expr
,
970 "Unexpected boolean value: value=\"%s\"", str
);
977 _BT_LOGE_NODE(unary_expr
,
978 "Unexpected unary expression type: node-type=%d",
979 unary_expr
->u
.unary_expression
.type
);
989 enum bt_ctf_byte_order
byte_order_from_unary_expr(struct ctf_node
*unary_expr
)
992 enum bt_ctf_byte_order bo
= BT_CTF_BYTE_ORDER_UNKNOWN
;
994 if (unary_expr
->u
.unary_expression
.type
!= UNARY_STRING
) {
995 _BT_LOGE_NODE(unary_expr
,
996 "\"byte_order\" attribute: expecting `be`, `le`, `network`, or `native`.");
1000 str
= unary_expr
->u
.unary_expression
.u
.string
;
1002 if (!strcmp(str
, "be") || !strcmp(str
, "network")) {
1003 bo
= BT_CTF_BYTE_ORDER_BIG_ENDIAN
;
1004 } else if (!strcmp(str
, "le")) {
1005 bo
= BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
;
1006 } else if (!strcmp(str
, "native")) {
1007 bo
= BT_CTF_BYTE_ORDER_NATIVE
;
1009 _BT_LOGE_NODE(unary_expr
,
1010 "Unexpected \"byte_order\" attribute value: "
1011 "expecting `be`, `le`, `network`, or `native`: value=\"%s\"",
1021 enum bt_ctf_byte_order
get_real_byte_order(struct ctx
*ctx
,
1022 struct ctf_node
*uexpr
)
1024 enum bt_ctf_byte_order bo
= byte_order_from_unary_expr(uexpr
);
1026 if (bo
== BT_CTF_BYTE_ORDER_NATIVE
) {
1027 bo
= bt_ctf_trace_get_native_byte_order(ctx
->trace
);
1034 int is_align_valid(uint64_t align
)
1036 return (align
!= 0) && !(align
& (align
- 1ULL));
1040 int get_type_specifier_name(struct ctx
*ctx
, struct ctf_node
*type_specifier
,
1045 if (type_specifier
->type
!= NODE_TYPE_SPECIFIER
) {
1046 _BT_LOGE_NODE(type_specifier
,
1047 "Unexpected node type: node-type=%d",
1048 type_specifier
->type
);
1053 switch (type_specifier
->u
.type_specifier
.type
) {
1055 g_string_append(str
, "void");
1058 g_string_append(str
, "char");
1060 case TYPESPEC_SHORT
:
1061 g_string_append(str
, "short");
1064 g_string_append(str
, "int");
1067 g_string_append(str
, "long");
1069 case TYPESPEC_FLOAT
:
1070 g_string_append(str
, "float");
1072 case TYPESPEC_DOUBLE
:
1073 g_string_append(str
, "double");
1075 case TYPESPEC_SIGNED
:
1076 g_string_append(str
, "signed");
1078 case TYPESPEC_UNSIGNED
:
1079 g_string_append(str
, "unsigned");
1082 g_string_append(str
, "bool");
1084 case TYPESPEC_COMPLEX
:
1085 g_string_append(str
, "_Complex");
1087 case TYPESPEC_IMAGINARY
:
1088 g_string_append(str
, "_Imaginary");
1090 case TYPESPEC_CONST
:
1091 g_string_append(str
, "const");
1093 case TYPESPEC_ID_TYPE
:
1094 if (type_specifier
->u
.type_specifier
.id_type
) {
1095 g_string_append(str
,
1096 type_specifier
->u
.type_specifier
.id_type
);
1099 case TYPESPEC_STRUCT
:
1101 struct ctf_node
*node
= type_specifier
->u
.type_specifier
.node
;
1103 if (!node
->u
._struct
.name
) {
1104 _BT_LOGE_NODE(node
, "Unexpected empty structure field type name.");
1109 g_string_append(str
, "struct ");
1110 g_string_append(str
, node
->u
._struct
.name
);
1113 case TYPESPEC_VARIANT
:
1115 struct ctf_node
*node
= type_specifier
->u
.type_specifier
.node
;
1117 if (!node
->u
.variant
.name
) {
1118 _BT_LOGE_NODE(node
, "Unexpected empty variant field type name.");
1123 g_string_append(str
, "variant ");
1124 g_string_append(str
, node
->u
.variant
.name
);
1129 struct ctf_node
*node
= type_specifier
->u
.type_specifier
.node
;
1131 if (!node
->u
._enum
.enum_id
) {
1133 "Unexpected empty enumeration field type (`enum`) name.");
1138 g_string_append(str
, "enum ");
1139 g_string_append(str
, node
->u
._enum
.enum_id
);
1142 case TYPESPEC_FLOATING_POINT
:
1143 case TYPESPEC_INTEGER
:
1144 case TYPESPEC_STRING
:
1146 _BT_LOGE_NODE(type_specifier
->u
.type_specifier
.node
,
1147 "Unexpected type specifier type: %d",
1148 type_specifier
->u
.type_specifier
.type
);
1158 int get_type_specifier_list_name(struct ctx
*ctx
,
1159 struct ctf_node
*type_specifier_list
, GString
*str
)
1162 struct ctf_node
*iter
;
1163 int alias_item_nr
= 0;
1164 struct bt_list_head
*head
=
1165 &type_specifier_list
->u
.type_specifier_list
.head
;
1167 bt_list_for_each_entry(iter
, head
, siblings
) {
1168 if (alias_item_nr
!= 0) {
1169 g_string_append(str
, " ");
1173 ret
= get_type_specifier_name(ctx
, iter
, str
);
1184 GQuark
create_typealias_identifier(struct ctx
*ctx
,
1185 struct ctf_node
*type_specifier_list
,
1186 struct ctf_node
*node_type_declarator
)
1192 struct ctf_node
*iter
;
1193 struct bt_list_head
*pointers
=
1194 &node_type_declarator
->u
.type_declarator
.pointers
;
1196 str
= g_string_new("");
1197 ret
= get_type_specifier_list_name(ctx
, type_specifier_list
, str
);
1199 g_string_free(str
, TRUE
);
1203 bt_list_for_each_entry(iter
, pointers
, siblings
) {
1204 g_string_append(str
, " *");
1206 if (iter
->u
.pointer
.const_qualifier
) {
1207 g_string_append(str
, " const");
1211 str_c
= g_string_free(str
, FALSE
);
1212 qalias
= g_quark_from_string(str_c
);
1220 int visit_type_declarator(struct ctx
*ctx
, struct ctf_node
*type_specifier_list
,
1221 GQuark
*field_name
, struct ctf_node
*node_type_declarator
,
1222 struct bt_ctf_field_type
**field_decl
,
1223 struct bt_ctf_field_type
*nested_decl
)
1226 * During this whole function, nested_decl is always OURS,
1227 * whereas field_decl is an output which we create, but
1228 * belongs to the caller (it is moved).
1234 /* Validate type declarator node */
1235 if (node_type_declarator
) {
1236 if (node_type_declarator
->u
.type_declarator
.type
==
1238 _BT_LOGE_NODE(node_type_declarator
,
1239 "Unexpected type declarator type: type=%d",
1240 node_type_declarator
->u
.type_declarator
.type
);
1245 /* TODO: GCC bitfields not supported yet */
1246 if (node_type_declarator
->u
.type_declarator
.bitfield_len
!=
1248 _BT_LOGE_NODE(node_type_declarator
,
1249 "GCC bitfields are not supported as of this version.");
1255 /* Find the right nested declaration if not provided */
1257 struct bt_list_head
*pointers
=
1258 &node_type_declarator
->u
.type_declarator
.pointers
;
1260 if (node_type_declarator
&& !bt_list_empty(pointers
)) {
1262 _BT_CTF_FIELD_TYPE_INIT(nested_decl_copy
);
1265 * If we have a pointer declarator, it HAS to
1266 * be present in the typealiases (else fail).
1268 qalias
= create_typealias_identifier(ctx
,
1269 type_specifier_list
, node_type_declarator
);
1271 ctx_decl_scope_lookup_alias(ctx
->current_scope
,
1272 g_quark_to_string(qalias
), -1);
1274 _BT_LOGE_NODE(node_type_declarator
,
1275 "Cannot find type alias: name=\"%s\"",
1276 g_quark_to_string(qalias
));
1281 /* Make a copy of it */
1282 nested_decl_copy
= bt_ctf_field_type_copy(nested_decl
);
1283 BT_PUT(nested_decl
);
1284 if (!nested_decl_copy
) {
1285 _BT_LOGE_NODE(node_type_declarator
,
1286 "Cannot copy nested field type.");
1291 BT_MOVE(nested_decl
, nested_decl_copy
);
1293 /* Force integer's base to 16 since it's a pointer */
1294 if (bt_ctf_field_type_is_integer(nested_decl
)) {
1295 ret
= bt_ctf_field_type_integer_set_base(
1297 BT_CTF_INTEGER_BASE_HEXADECIMAL
);
1301 ret
= visit_type_specifier_list(ctx
,
1302 type_specifier_list
, &nested_decl
);
1304 assert(!nested_decl
);
1310 assert(nested_decl
);
1312 if (!node_type_declarator
) {
1313 BT_MOVE(*field_decl
, nested_decl
);
1317 if (node_type_declarator
->u
.type_declarator
.type
== TYPEDEC_ID
) {
1318 if (node_type_declarator
->u
.type_declarator
.u
.id
) {
1320 node_type_declarator
->u
.type_declarator
.u
.id
;
1322 *field_name
= g_quark_from_string(id
);
1327 BT_MOVE(*field_decl
, nested_decl
);
1330 struct ctf_node
*first
;
1331 _BT_CTF_FIELD_TYPE_INIT(decl
);
1332 _BT_CTF_FIELD_TYPE_INIT(outer_field_decl
);
1333 struct bt_list_head
*length
=
1334 &node_type_declarator
->
1335 u
.type_declarator
.u
.nested
.length
;
1337 /* Create array/sequence, pass nested_decl as child */
1338 if (bt_list_empty(length
)) {
1339 _BT_LOGE_NODE(node_type_declarator
,
1340 "Expecting length field reference or value.");
1345 first
= _BT_LIST_FIRST_ENTRY(length
, struct ctf_node
, siblings
);
1346 if (first
->type
!= NODE_UNARY_EXPRESSION
) {
1347 _BT_LOGE_NODE(first
,
1348 "Unexpected node type: node-type=%d",
1354 switch (first
->u
.unary_expression
.type
) {
1355 case UNARY_UNSIGNED_CONSTANT
:
1358 _BT_CTF_FIELD_TYPE_INIT(array_decl
);
1360 len
= first
->u
.unary_expression
.u
.unsigned_constant
;
1361 array_decl
= bt_ctf_field_type_array_create(nested_decl
,
1363 BT_PUT(nested_decl
);
1365 _BT_LOGE_NODE(first
,
1366 "Cannot create array field type.");
1371 BT_MOVE(decl
, array_decl
);
1376 /* Lookup unsigned integer definition, create seq. */
1377 _BT_CTF_FIELD_TYPE_INIT(seq_decl
);
1378 char *length_name
= concatenate_unary_strings(length
);
1381 _BT_LOGE_NODE(node_type_declarator
,
1382 "Cannot concatenate unary strings.");
1387 seq_decl
= bt_ctf_field_type_sequence_create(
1388 nested_decl
, length_name
);
1389 g_free(length_name
);
1390 BT_PUT(nested_decl
);
1392 _BT_LOGE_NODE(node_type_declarator
,
1393 "Cannot create sequence field type.");
1398 BT_MOVE(decl
, seq_decl
);
1406 assert(!nested_decl
);
1408 assert(!*field_decl
);
1411 * At this point, we found the next nested declaration.
1412 * We currently own this (and lost the ownership of
1413 * nested_decl in the meantime). Pass this next
1414 * nested declaration as the content of the outer
1415 * container, MOVING its ownership.
1417 ret
= visit_type_declarator(ctx
, type_specifier_list
,
1419 node_type_declarator
->
1420 u
.type_declarator
.u
.nested
.type_declarator
,
1421 &outer_field_decl
, decl
);
1424 assert(!outer_field_decl
);
1429 assert(outer_field_decl
);
1430 BT_MOVE(*field_decl
, outer_field_decl
);
1434 BT_PUT(nested_decl
);
1435 assert(*field_decl
);
1440 BT_PUT(nested_decl
);
1441 BT_PUT(*field_decl
);
1447 int visit_struct_decl_field(struct ctx
*ctx
,
1448 struct bt_ctf_field_type
*struct_decl
,
1449 struct ctf_node
*type_specifier_list
,
1450 struct bt_list_head
*type_declarators
)
1453 struct ctf_node
*iter
;
1454 _BT_CTF_FIELD_TYPE_INIT(field_decl
);
1456 bt_list_for_each_entry(iter
, type_declarators
, siblings
) {
1459 const char *field_name
;
1460 _BT_CTF_FIELD_TYPE_INIT(efield_decl
);
1462 ret
= visit_type_declarator(ctx
, type_specifier_list
,
1463 &qfield_name
, iter
, &field_decl
, NULL
);
1465 assert(!field_decl
);
1466 _BT_LOGE_NODE(type_specifier_list
,
1467 "Cannot visit type declarator: ret=%d", ret
);
1472 field_name
= g_quark_to_string(qfield_name
);
1474 /* Check if field with same name already exists */
1476 bt_ctf_field_type_structure_get_field_type_by_name(
1477 struct_decl
, field_name
);
1479 BT_PUT(efield_decl
);
1480 _BT_LOGE_NODE(type_specifier_list
,
1481 "Duplicate field in structure field type: "
1482 "field-name=\"%s\"", field_name
);
1487 /* Add field to structure */
1488 ret
= bt_ctf_field_type_structure_add_field(struct_decl
,
1489 field_decl
, field_name
);
1492 _BT_LOGE_NODE(type_specifier_list
,
1493 "Cannot add field to structure field type: "
1494 "field-name=\"%s\", ret=%d",
1495 g_quark_to_string(qfield_name
), ret
);
1509 int visit_variant_decl_field(struct ctx
*ctx
,
1510 struct bt_ctf_field_type
*variant_decl
,
1511 struct ctf_node
*type_specifier_list
,
1512 struct bt_list_head
*type_declarators
)
1515 struct ctf_node
*iter
;
1516 _BT_CTF_FIELD_TYPE_INIT(field_decl
);
1518 bt_list_for_each_entry(iter
, type_declarators
, siblings
) {
1521 const char *field_name
;
1522 _BT_CTF_FIELD_TYPE_INIT(efield_decl
);
1524 ret
= visit_type_declarator(ctx
, type_specifier_list
,
1525 &qfield_name
, iter
, &field_decl
, NULL
);
1527 assert(!field_decl
);
1528 _BT_LOGE_NODE(type_specifier_list
,
1529 "Cannot visit type declarator: ret=%d", ret
);
1534 field_name
= g_quark_to_string(qfield_name
);
1536 /* Check if field with same name already exists */
1538 bt_ctf_field_type_variant_get_field_type_by_name(
1539 variant_decl
, field_name
);
1541 BT_PUT(efield_decl
);
1542 _BT_LOGE_NODE(type_specifier_list
,
1543 "Duplicate field in variant field type: "
1544 "field-name=\"%s\"", field_name
);
1549 /* Add field to structure */
1550 ret
= bt_ctf_field_type_variant_add_field(variant_decl
,
1551 field_decl
, field_name
);
1554 _BT_LOGE_NODE(type_specifier_list
,
1555 "Cannot add field to variant field type: "
1556 "field-name=\"%s\", ret=%d",
1557 g_quark_to_string(qfield_name
), ret
);
1571 int visit_typedef(struct ctx
*ctx
, struct ctf_node
*type_specifier_list
,
1572 struct bt_list_head
*type_declarators
)
1576 struct ctf_node
*iter
;
1577 _BT_CTF_FIELD_TYPE_INIT(type_decl
);
1579 bt_list_for_each_entry(iter
, type_declarators
, siblings
) {
1580 ret
= visit_type_declarator(ctx
, type_specifier_list
,
1581 &qidentifier
, iter
, &type_decl
, NULL
);
1584 "Cannot visit type declarator: ret=%d", ret
);
1589 /* Do not allow typedef and typealias of untagged variants */
1590 if (bt_ctf_field_type_is_variant(type_decl
)) {
1591 if (bt_ctf_field_type_variant_get_tag_name(type_decl
)) {
1593 "Type definition of untagged variant field type is not allowed.");
1599 ret
= ctx_decl_scope_register_alias(ctx
->current_scope
,
1600 g_quark_to_string(qidentifier
), type_decl
);
1603 "Cannot register type definition: name=\"%s\"",
1604 g_quark_to_string(qidentifier
));
1616 int visit_typealias(struct ctx
*ctx
, struct ctf_node
*target
,
1617 struct ctf_node
*alias
)
1621 struct ctf_node
*node
;
1622 GQuark qdummy_field_name
;
1623 _BT_CTF_FIELD_TYPE_INIT(type_decl
);
1625 /* Create target type declaration */
1626 if (bt_list_empty(&target
->u
.typealias_target
.type_declarators
)) {
1629 node
= _BT_LIST_FIRST_ENTRY(
1630 &target
->u
.typealias_target
.type_declarators
,
1631 struct ctf_node
, siblings
);
1634 ret
= visit_type_declarator(ctx
,
1635 target
->u
.typealias_target
.type_specifier_list
,
1636 &qdummy_field_name
, node
, &type_decl
, NULL
);
1640 "Cannot visit type declarator: ret=%d", ret
);
1644 /* Do not allow typedef and typealias of untagged variants */
1645 if (bt_ctf_field_type_is_variant(type_decl
)) {
1646 if (bt_ctf_field_type_variant_get_tag_name(type_decl
)) {
1647 _BT_LOGE_NODE(target
,
1648 "Type definition of untagged variant field type is not allowed.");
1655 * The semantic validator does not check whether the target is
1656 * abstract or not (if it has an identifier). Check it here.
1658 if (qdummy_field_name
!= 0) {
1659 _BT_LOGE_NODE(target
,
1660 "Expecting empty identifier: id=\"%s\"",
1661 g_quark_to_string(qdummy_field_name
));
1666 /* Create alias identifier */
1667 node
= _BT_LIST_FIRST_ENTRY(&alias
->u
.typealias_alias
.type_declarators
,
1668 struct ctf_node
, siblings
);
1669 qalias
= create_typealias_identifier(ctx
,
1670 alias
->u
.typealias_alias
.type_specifier_list
, node
);
1671 ret
= ctx_decl_scope_register_alias(ctx
->current_scope
,
1672 g_quark_to_string(qalias
), type_decl
);
1675 "Cannot register type alias: name=\"%s\"",
1676 g_quark_to_string(qalias
));
1687 int visit_struct_decl_entry(struct ctx
*ctx
, struct ctf_node
*entry_node
,
1688 struct bt_ctf_field_type
*struct_decl
)
1692 switch (entry_node
->type
) {
1694 ret
= visit_typedef(ctx
,
1695 entry_node
->u
._typedef
.type_specifier_list
,
1696 &entry_node
->u
._typedef
.type_declarators
);
1698 _BT_LOGE_NODE(entry_node
,
1699 "Cannot add type definition found in structure field type: ret=%d",
1704 case NODE_TYPEALIAS
:
1705 ret
= visit_typealias(ctx
, entry_node
->u
.typealias
.target
,
1706 entry_node
->u
.typealias
.alias
);
1708 _BT_LOGE_NODE(entry_node
,
1709 "Cannot add type alias found in structure field type: ret=%d",
1714 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
1716 ret
= visit_struct_decl_field(ctx
, struct_decl
,
1717 entry_node
->u
.struct_or_variant_declaration
.
1718 type_specifier_list
,
1719 &entry_node
->u
.struct_or_variant_declaration
.
1726 _BT_LOGE_NODE(entry_node
,
1727 "Unexpected node type: node-type=%d", entry_node
->type
);
1737 int visit_variant_decl_entry(struct ctx
*ctx
, struct ctf_node
*entry_node
,
1738 struct bt_ctf_field_type
*variant_decl
)
1742 switch (entry_node
->type
) {
1744 ret
= visit_typedef(ctx
,
1745 entry_node
->u
._typedef
.type_specifier_list
,
1746 &entry_node
->u
._typedef
.type_declarators
);
1748 _BT_LOGE_NODE(entry_node
,
1749 "Cannot add type definition found in variant field type: ret=%d",
1754 case NODE_TYPEALIAS
:
1755 ret
= visit_typealias(ctx
, entry_node
->u
.typealias
.target
,
1756 entry_node
->u
.typealias
.alias
);
1758 _BT_LOGE_NODE(entry_node
,
1759 "Cannot add type alias found in variant field type: ret=%d",
1764 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
1766 ret
= visit_variant_decl_field(ctx
, variant_decl
,
1767 entry_node
->u
.struct_or_variant_declaration
.
1768 type_specifier_list
,
1769 &entry_node
->u
.struct_or_variant_declaration
.
1776 _BT_LOGE_NODE(entry_node
,
1777 "Unexpected node type: node-type=%d",
1788 int visit_struct_decl(struct ctx
*ctx
, const char *name
,
1789 struct bt_list_head
*decl_list
, int has_body
,
1790 struct bt_list_head
*min_align
,
1791 struct bt_ctf_field_type
**struct_decl
)
1795 *struct_decl
= NULL
;
1797 /* For named struct (without body), lookup in declaration scope */
1799 _BT_CTF_FIELD_TYPE_INIT(struct_decl_copy
);
1802 BT_LOGE_STR("Bodyless structure field type: missing name.");
1807 *struct_decl
= ctx_decl_scope_lookup_struct(ctx
->current_scope
,
1809 if (!*struct_decl
) {
1810 BT_LOGE("Cannot find structure field type: name=\"struct %s\"",
1816 /* Make a copy of it */
1817 struct_decl_copy
= bt_ctf_field_type_copy(*struct_decl
);
1818 if (!struct_decl_copy
) {
1819 BT_LOGE_STR("Cannot create copy of structure field type.");
1824 BT_MOVE(*struct_decl
, struct_decl_copy
);
1826 struct ctf_node
*entry_node
;
1827 uint64_t min_align_value
= 0;
1830 _BT_CTF_FIELD_TYPE_INIT(estruct_decl
);
1832 estruct_decl
= ctx_decl_scope_lookup_struct(
1833 ctx
->current_scope
, name
, 1);
1835 BT_PUT(estruct_decl
);
1836 BT_LOGE("Structure field type already declared in local scope: "
1837 "name=\"struct %s\"", name
);
1843 if (!bt_list_empty(min_align
)) {
1844 ret
= get_unary_unsigned(min_align
, &min_align_value
);
1846 BT_LOGE("Unexpected unary expression for structure field type's `align` attribute: "
1852 *struct_decl
= bt_ctf_field_type_structure_create();
1853 if (!*struct_decl
) {
1854 BT_LOGE_STR("Cannot create empty structure field type.");
1859 if (min_align_value
!= 0) {
1860 ret
= bt_ctf_field_type_set_alignment(*struct_decl
,
1863 BT_LOGE("Cannot set structure field type's alignment: "
1869 ret
= ctx_push_scope(ctx
);
1871 BT_LOGE_STR("Cannot push scope.");
1875 bt_list_for_each_entry(entry_node
, decl_list
, siblings
) {
1876 ret
= visit_struct_decl_entry(ctx
, entry_node
,
1879 _BT_LOGE_NODE(entry_node
,
1880 "Cannot visit structure field type entry: "
1890 ret
= ctx_decl_scope_register_struct(ctx
->current_scope
,
1891 name
, *struct_decl
);
1893 BT_LOGE("Cannot register structure field type in declaration scope: "
1894 "name=\"struct %s\", ret=%d", name
, ret
);
1903 BT_PUT(*struct_decl
);
1909 int visit_variant_decl(struct ctx
*ctx
, const char *name
,
1910 const char *tag
, struct bt_list_head
*decl_list
,
1911 int has_body
, struct bt_ctf_field_type
**variant_decl
)
1914 _BT_CTF_FIELD_TYPE_INIT(untagged_variant_decl
);
1916 *variant_decl
= NULL
;
1918 /* For named variant (without body), lookup in declaration scope */
1920 _BT_CTF_FIELD_TYPE_INIT(variant_decl_copy
);
1923 BT_LOGE_STR("Bodyless variant field type: missing name.");
1928 untagged_variant_decl
=
1929 ctx_decl_scope_lookup_variant(ctx
->current_scope
,
1931 if (!untagged_variant_decl
) {
1932 BT_LOGE("Cannot find variant field type: name=\"variant %s\"",
1938 /* Make a copy of it */
1939 variant_decl_copy
= bt_ctf_field_type_copy(
1940 untagged_variant_decl
);
1941 if (!variant_decl_copy
) {
1942 BT_LOGE_STR("Cannot create copy of variant field type.");
1947 BT_MOVE(untagged_variant_decl
, variant_decl_copy
);
1949 struct ctf_node
*entry_node
;
1952 struct bt_ctf_field_type
*evariant_decl
=
1953 ctx_decl_scope_lookup_struct(ctx
->current_scope
,
1956 if (evariant_decl
) {
1957 BT_PUT(evariant_decl
);
1958 BT_LOGE("Variant field type already declared in local scope: "
1959 "name=\"variant %s\"", name
);
1965 untagged_variant_decl
= bt_ctf_field_type_variant_create(NULL
,
1967 if (!untagged_variant_decl
) {
1968 BT_LOGE_STR("Cannot create empty variant field type.");
1973 ret
= ctx_push_scope(ctx
);
1975 BT_LOGE_STR("Cannot push scope.");
1979 bt_list_for_each_entry(entry_node
, decl_list
, siblings
) {
1980 ret
= visit_variant_decl_entry(ctx
, entry_node
,
1981 untagged_variant_decl
);
1983 _BT_LOGE_NODE(entry_node
,
1984 "Cannot visit variant field type entry: "
1994 ret
= ctx_decl_scope_register_variant(
1995 ctx
->current_scope
, name
,
1996 untagged_variant_decl
);
1998 BT_LOGE("Cannot register variant field type in declaration scope: "
1999 "name=\"variant %s\", ret=%d", name
, ret
);
2006 * If tagged, create tagged variant and return; otherwise
2007 * return untagged variant.
2010 BT_MOVE(*variant_decl
, untagged_variant_decl
);
2013 * At this point, we have a fresh untagged variant; nobody
2014 * else owns it. Set its tag now.
2016 ret
= bt_ctf_field_type_variant_set_tag_name(
2017 untagged_variant_decl
, tag
);
2019 BT_LOGE("Cannot set variant field type's tag name: "
2020 "tag-name=\"%s\"", tag
);
2024 BT_MOVE(*variant_decl
, untagged_variant_decl
);
2027 assert(!untagged_variant_decl
);
2028 assert(*variant_decl
);
2033 BT_PUT(untagged_variant_decl
);
2034 BT_PUT(*variant_decl
);
2040 int visit_enum_decl_entry(struct ctx
*ctx
, struct ctf_node
*enumerator
,
2041 struct bt_ctf_field_type
*enum_decl
, int64_t *last
, int is_signed
)
2045 struct ctf_node
*iter
;
2046 int64_t start
= 0, end
= 0;
2047 const char *label
= enumerator
->u
.enumerator
.id
;
2048 struct bt_list_head
*values
= &enumerator
->u
.enumerator
.values
;
2050 bt_list_for_each_entry(iter
, values
, siblings
) {
2053 if (iter
->type
!= NODE_UNARY_EXPRESSION
) {
2055 "Wrong expression for enumeration field type label: "
2056 "node-type=%d, label=\"%s\"", iter
->type
,
2068 switch (iter
->u
.unary_expression
.type
) {
2069 case UNARY_SIGNED_CONSTANT
:
2070 *target
= iter
->u
.unary_expression
.u
.signed_constant
;
2072 case UNARY_UNSIGNED_CONSTANT
:
2074 iter
->u
.unary_expression
.u
.unsigned_constant
;
2078 "Invalid enumeration field type entry: "
2079 "expecting constant signed or unsigned integer: "
2080 "node-type=%d, label=\"%s\"",
2081 iter
->u
.unary_expression
.type
, label
);
2088 "Invalid enumeration field type entry: label=\"%s\"",
2108 ret
= bt_ctf_field_type_enumeration_add_mapping(enum_decl
, label
,
2111 ret
= bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_decl
,
2112 label
, (uint64_t) start
, (uint64_t) end
);
2115 _BT_LOGE_NODE(enumerator
,
2116 "Cannot add mapping to enumeration field type: "
2117 "label=\"%s\", ret=%d, "
2118 "start-value-unsigned=%" PRIu64
", "
2119 "end-value-unsigned=%" PRIu64
, label
, ret
,
2120 (uint64_t) start
, (uint64_t) end
);
2131 int visit_enum_decl(struct ctx
*ctx
, const char *name
,
2132 struct ctf_node
*container_type
,
2133 struct bt_list_head
*enumerator_list
,
2135 struct bt_ctf_field_type
**enum_decl
)
2139 _BT_CTF_FIELD_TYPE_INIT(integer_decl
);
2143 /* For named enum (without body), lookup in declaration scope */
2145 _BT_CTF_FIELD_TYPE_INIT(enum_decl_copy
);
2148 BT_LOGE_STR("Bodyless enumeration field type: missing name.");
2153 *enum_decl
= ctx_decl_scope_lookup_enum(ctx
->current_scope
,
2156 BT_LOGE("Cannot find enumeration field type: "
2157 "name=\"enum %s\"", name
);
2162 /* Make a copy of it */
2163 enum_decl_copy
= bt_ctf_field_type_copy(*enum_decl
);
2164 if (!enum_decl_copy
) {
2165 BT_LOGE_STR("Cannot create copy of enumeration field type.");
2171 BT_MOVE(*enum_decl
, enum_decl_copy
);
2173 struct ctf_node
*iter
;
2174 int64_t last_value
= 0;
2177 _BT_CTF_FIELD_TYPE_INIT(eenum_decl
);
2179 eenum_decl
= ctx_decl_scope_lookup_enum(
2180 ctx
->current_scope
, name
, 1);
2183 BT_LOGE("Enumeration field type already declared in local scope: "
2184 "name=\"enum %s\"", name
);
2190 if (!container_type
) {
2191 integer_decl
= ctx_decl_scope_lookup_alias(
2192 ctx
->current_scope
, "int", -1);
2193 if (!integer_decl
) {
2194 BT_LOGE_STR("Cannot find implicit `int` field type alias for enumeration field type.");
2199 ret
= visit_type_declarator(ctx
, container_type
,
2200 &qdummy_id
, NULL
, &integer_decl
, NULL
);
2202 assert(!integer_decl
);
2208 assert(integer_decl
);
2210 if (!bt_ctf_field_type_is_integer(integer_decl
)) {
2211 BT_LOGE("Container field type for enumeration field type is not an integer field type: "
2213 bt_ctf_field_type_id_string(
2214 bt_ctf_field_type_get_type_id(integer_decl
)));
2219 *enum_decl
= bt_ctf_field_type_enumeration_create(integer_decl
);
2221 BT_LOGE_STR("Cannot create enumeration field type.");
2226 bt_list_for_each_entry(iter
, enumerator_list
, siblings
) {
2227 ret
= visit_enum_decl_entry(ctx
, iter
, *enum_decl
,
2229 bt_ctf_field_type_integer_get_signed(integer_decl
));
2232 "Cannot visit enumeration field type entry: "
2239 ret
= ctx_decl_scope_register_enum(ctx
->current_scope
,
2242 BT_LOGE("Cannot register enumeration field type in declaration scope: "
2249 BT_PUT(integer_decl
);
2254 BT_PUT(integer_decl
);
2261 int visit_type_specifier(struct ctx
*ctx
,
2262 struct ctf_node
*type_specifier_list
,
2263 struct bt_ctf_field_type
**decl
)
2266 GString
*str
= NULL
;
2267 _BT_CTF_FIELD_TYPE_INIT(decl_copy
);
2270 str
= g_string_new("");
2271 ret
= get_type_specifier_list_name(ctx
, type_specifier_list
, str
);
2273 _BT_LOGE_NODE(type_specifier_list
,
2274 "Cannot get type specifier list's name: ret=%d", ret
);
2278 *decl
= ctx_decl_scope_lookup_alias(ctx
->current_scope
, str
->str
, -1);
2280 _BT_LOGE_NODE(type_specifier_list
,
2281 "Cannot find type alias: name=\"%s\"", str
->str
);
2286 /* Make a copy of the type declaration */
2287 decl_copy
= bt_ctf_field_type_copy(*decl
);
2289 _BT_LOGE_NODE(type_specifier_list
,
2290 "Cannot create field type copy.");
2295 BT_MOVE(*decl
, decl_copy
);
2296 (void) g_string_free(str
, TRUE
);
2303 (void) g_string_free(str
, TRUE
);
2312 int visit_integer_decl(struct ctx
*ctx
,
2313 struct bt_list_head
*expressions
,
2314 struct bt_ctf_field_type
**integer_decl
)
2319 struct ctf_node
*expression
;
2320 uint64_t alignment
= 0, size
= 0;
2321 struct bt_ctf_clock_class
*mapped_clock
= NULL
;
2322 enum bt_ctf_string_encoding encoding
= BT_CTF_STRING_ENCODING_NONE
;
2323 enum bt_ctf_integer_base base
= BT_CTF_INTEGER_BASE_DECIMAL
;
2324 enum bt_ctf_byte_order byte_order
=
2325 bt_ctf_trace_get_native_byte_order(ctx
->trace
);
2327 *integer_decl
= NULL
;
2329 bt_list_for_each_entry(expression
, expressions
, siblings
) {
2330 struct ctf_node
*left
, *right
;
2332 left
= _BT_LIST_FIRST_ENTRY(&expression
->u
.ctf_expression
.left
,
2333 struct ctf_node
, siblings
);
2334 right
= _BT_LIST_FIRST_ENTRY(
2335 &expression
->u
.ctf_expression
.right
, struct ctf_node
,
2338 if (left
->u
.unary_expression
.type
!= UNARY_STRING
) {
2340 "Unexpected unary expression type: type=%d",
2341 left
->u
.unary_expression
.type
);
2346 if (!strcmp(left
->u
.unary_expression
.u
.string
, "signed")) {
2347 if (_IS_SET(&set
, _INTEGER_SIGNED_SET
)) {
2348 _BT_LOGE_DUP_ATTR(left
, "signed",
2349 "integer field type");
2354 signedness
= get_boolean(right
);
2355 if (signedness
< 0) {
2356 _BT_LOGE_NODE(right
,
2357 "Invalid boolean value for integer field type's `signed` attribute: "
2363 _SET(&set
, _INTEGER_SIGNED_SET
);
2364 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2366 if (_IS_SET(&set
, _INTEGER_BYTE_ORDER_SET
)) {
2367 _BT_LOGE_DUP_ATTR(left
, "byte_order",
2368 "integer field type");
2373 byte_order
= get_real_byte_order(ctx
, right
);
2374 if (byte_order
== BT_CTF_BYTE_ORDER_UNKNOWN
) {
2375 _BT_LOGE_NODE(right
,
2376 "Invalid `byte_order` attribute in integer field type: "
2382 _SET(&set
, _INTEGER_BYTE_ORDER_SET
);
2383 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "size")) {
2384 if (_IS_SET(&set
, _INTEGER_SIZE_SET
)) {
2385 _BT_LOGE_DUP_ATTR(left
, "size",
2386 "integer field type");
2391 if (right
->u
.unary_expression
.type
!=
2392 UNARY_UNSIGNED_CONSTANT
) {
2393 _BT_LOGE_NODE(right
,
2394 "Invalid `size` attribute in integer field type: "
2395 "expecting unsigned constant integer: "
2397 right
->u
.unary_expression
.type
);
2402 size
= right
->u
.unary_expression
.u
.unsigned_constant
;
2404 _BT_LOGE_NODE(right
,
2405 "Invalid `size` attribute in integer field type: "
2406 "expecting positive constant integer: "
2407 "size=%" PRIu64
, size
);
2410 } else if (size
> 64) {
2411 _BT_LOGE_NODE(right
,
2412 "Invalid `size` attribute in integer field type: "
2413 "integer fields over 64 bits are not supported as of this version: "
2414 "size=%" PRIu64
, size
);
2419 _SET(&set
, _INTEGER_SIZE_SET
);
2420 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2422 if (_IS_SET(&set
, _INTEGER_ALIGN_SET
)) {
2423 _BT_LOGE_DUP_ATTR(left
, "align",
2424 "integer field type");
2429 if (right
->u
.unary_expression
.type
!=
2430 UNARY_UNSIGNED_CONSTANT
) {
2431 _BT_LOGE_NODE(right
,
2432 "Invalid `align` attribute in integer field type: "
2433 "expecting unsigned constant integer: "
2435 right
->u
.unary_expression
.type
);
2441 right
->u
.unary_expression
.u
.unsigned_constant
;
2442 if (!is_align_valid(alignment
)) {
2443 _BT_LOGE_NODE(right
,
2444 "Invalid `align` attribute in integer field type: "
2445 "expecting power of two: "
2446 "align=%" PRIu64
, alignment
);
2451 _SET(&set
, _INTEGER_ALIGN_SET
);
2452 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "base")) {
2453 if (_IS_SET(&set
, _INTEGER_BASE_SET
)) {
2454 _BT_LOGE_DUP_ATTR(left
, "base",
2455 "integer field type");
2460 switch (right
->u
.unary_expression
.type
) {
2461 case UNARY_UNSIGNED_CONSTANT
:
2463 uint64_t constant
= right
->u
.unary_expression
.
2464 u
.unsigned_constant
;
2468 base
= BT_CTF_INTEGER_BASE_BINARY
;
2471 base
= BT_CTF_INTEGER_BASE_OCTAL
;
2474 base
= BT_CTF_INTEGER_BASE_DECIMAL
;
2477 base
= BT_CTF_INTEGER_BASE_HEXADECIMAL
;
2480 _BT_LOGE_NODE(right
,
2481 "Invalid `base` attribute in integer field type: "
2483 right
->u
.unary_expression
.u
.unsigned_constant
);
2491 char *s_right
= concatenate_unary_strings(
2492 &expression
->u
.ctf_expression
.right
);
2494 _BT_LOGE_NODE(right
,
2495 "Unexpected unary expression for integer field type's `base` attribute.");
2500 if (!strcmp(s_right
, "decimal") ||
2501 !strcmp(s_right
, "dec") ||
2502 !strcmp(s_right
, "d") ||
2503 !strcmp(s_right
, "i") ||
2504 !strcmp(s_right
, "u")) {
2505 base
= BT_CTF_INTEGER_BASE_DECIMAL
;
2506 } else if (!strcmp(s_right
, "hexadecimal") ||
2507 !strcmp(s_right
, "hex") ||
2508 !strcmp(s_right
, "x") ||
2509 !strcmp(s_right
, "X") ||
2510 !strcmp(s_right
, "p")) {
2511 base
= BT_CTF_INTEGER_BASE_HEXADECIMAL
;
2512 } else if (!strcmp(s_right
, "octal") ||
2513 !strcmp(s_right
, "oct") ||
2514 !strcmp(s_right
, "o")) {
2515 base
= BT_CTF_INTEGER_BASE_OCTAL
;
2516 } else if (!strcmp(s_right
, "binary") ||
2517 !strcmp(s_right
, "b")) {
2518 base
= BT_CTF_INTEGER_BASE_BINARY
;
2520 _BT_LOGE_NODE(right
,
2521 "Unexpected unary expression for integer field type's `base` attribute: "
2522 "base=\"%s\"", s_right
);
2532 _BT_LOGE_NODE(right
,
2533 "Invalid `base` attribute in integer field type: "
2534 "expecting unsigned constant integer or unary string.");
2539 _SET(&set
, _INTEGER_BASE_SET
);
2540 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2544 if (_IS_SET(&set
, _INTEGER_ENCODING_SET
)) {
2545 _BT_LOGE_DUP_ATTR(left
, "encoding",
2546 "integer field type");
2551 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
2552 _BT_LOGE_NODE(right
,
2553 "Invalid `encoding` attribute in integer field type: "
2554 "expecting unary string.");
2559 s_right
= concatenate_unary_strings(
2560 &expression
->u
.ctf_expression
.right
);
2562 _BT_LOGE_NODE(right
,
2563 "Unexpected unary expression for integer field type's `encoding` attribute.");
2568 if (!strcmp(s_right
, "UTF8") ||
2569 !strcmp(s_right
, "utf8") ||
2570 !strcmp(s_right
, "utf-8") ||
2571 !strcmp(s_right
, "UTF-8")) {
2572 encoding
= BT_CTF_STRING_ENCODING_UTF8
;
2573 } else if (!strcmp(s_right
, "ASCII") ||
2574 !strcmp(s_right
, "ascii")) {
2575 encoding
= BT_CTF_STRING_ENCODING_ASCII
;
2576 } else if (!strcmp(s_right
, "none")) {
2577 encoding
= BT_CTF_STRING_ENCODING_NONE
;
2579 _BT_LOGE_NODE(right
,
2580 "Invalid `encoding` attribute in integer field type: "
2581 "unknown encoding: encoding=\"%s\"",
2589 _SET(&set
, _INTEGER_ENCODING_SET
);
2590 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "map")) {
2591 const char *clock_name
;
2593 if (_IS_SET(&set
, _INTEGER_MAP_SET
)) {
2594 _BT_LOGE_DUP_ATTR(left
, "map",
2595 "integer field type");
2600 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
2601 _BT_LOGE_NODE(right
,
2602 "Invalid `map` attribute in integer field type: "
2603 "expecting unary string.");
2609 get_map_clock_name_value(
2610 &expression
->u
.ctf_expression
.right
);
2612 char *s_right
= concatenate_unary_strings(
2613 &expression
->u
.ctf_expression
.right
);
2616 _BT_LOGE_NODE(right
,
2617 "Unexpected unary expression for integer field type's `map` attribute.");
2622 _BT_LOGE_NODE(right
,
2623 "Invalid `map` attribute in integer field type: "
2624 "cannot find clock class at this point: name=\"%s\"",
2626 _SET(&set
, _INTEGER_MAP_SET
);
2631 mapped_clock
= bt_ctf_trace_get_clock_class_by_name(
2632 ctx
->trace
, clock_name
);
2633 if (!mapped_clock
) {
2634 _BT_LOGE_NODE(right
,
2635 "Invalid `map` attribute in integer field type: "
2636 "cannot find clock class at this point: name=\"%s\"",
2642 _SET(&set
, _INTEGER_MAP_SET
);
2645 "Unknown attribute in integer field type: "
2647 left
->u
.unary_expression
.u
.string
);
2651 if (!_IS_SET(&set
, _INTEGER_SIZE_SET
)) {
2652 BT_LOGE_STR("Missing `size` attribute in integer field type.");
2657 if (!_IS_SET(&set
, _INTEGER_ALIGN_SET
)) {
2658 if (size
% CHAR_BIT
) {
2659 /* Bit-packed alignment */
2662 /* Byte-packed alignment */
2663 alignment
= CHAR_BIT
;
2667 *integer_decl
= bt_ctf_field_type_integer_create((unsigned int) size
);
2668 if (!*integer_decl
) {
2669 BT_LOGE_STR("Cannot create integer field type.");
2674 ret
= bt_ctf_field_type_integer_set_signed(*integer_decl
, signedness
);
2675 ret
|= bt_ctf_field_type_integer_set_base(*integer_decl
, base
);
2676 ret
|= bt_ctf_field_type_integer_set_encoding(*integer_decl
, encoding
);
2677 ret
|= bt_ctf_field_type_set_alignment(*integer_decl
,
2678 (unsigned int) alignment
);
2679 ret
|= bt_ctf_field_type_set_byte_order(*integer_decl
, byte_order
);
2683 ret
|= bt_ctf_field_type_integer_set_mapped_clock_class(
2684 *integer_decl
, mapped_clock
);
2685 bt_put(mapped_clock
);
2686 mapped_clock
= NULL
;
2690 BT_LOGE_STR("Cannot configure integer field type.");
2699 bt_put(mapped_clock
);
2702 BT_PUT(*integer_decl
);
2708 int visit_floating_point_number_decl(struct ctx
*ctx
,
2709 struct bt_list_head
*expressions
,
2710 struct bt_ctf_field_type
**float_decl
)
2714 struct ctf_node
*expression
;
2715 uint64_t alignment
= 1, exp_dig
= 0, mant_dig
= 0;
2716 enum bt_ctf_byte_order byte_order
=
2717 bt_ctf_trace_get_native_byte_order(ctx
->trace
);
2721 bt_list_for_each_entry(expression
, expressions
, siblings
) {
2722 struct ctf_node
*left
, *right
;
2724 left
= _BT_LIST_FIRST_ENTRY(&expression
->u
.ctf_expression
.left
,
2725 struct ctf_node
, siblings
);
2726 right
= _BT_LIST_FIRST_ENTRY(
2727 &expression
->u
.ctf_expression
.right
, struct ctf_node
,
2730 if (left
->u
.unary_expression
.type
!= UNARY_STRING
) {
2732 "Unexpected unary expression type: type=%d",
2733 left
->u
.unary_expression
.type
);
2738 if (!strcmp(left
->u
.unary_expression
.u
.string
, "byte_order")) {
2739 if (_IS_SET(&set
, _FLOAT_BYTE_ORDER_SET
)) {
2740 _BT_LOGE_DUP_ATTR(left
, "byte_order",
2741 "floating point number field type");
2746 byte_order
= get_real_byte_order(ctx
, right
);
2747 if (byte_order
== BT_CTF_BYTE_ORDER_UNKNOWN
) {
2748 _BT_LOGE_NODE(right
,
2749 "Invalid `byte_order` attribute in floating point number field type: "
2755 _SET(&set
, _FLOAT_BYTE_ORDER_SET
);
2756 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2758 if (_IS_SET(&set
, _FLOAT_EXP_DIG_SET
)) {
2759 _BT_LOGE_DUP_ATTR(left
, "exp_dig",
2760 "floating point number field type");
2765 if (right
->u
.unary_expression
.type
!=
2766 UNARY_UNSIGNED_CONSTANT
) {
2767 _BT_LOGE_NODE(right
,
2768 "Invalid `exp_dig` attribute in floating point number field type: "
2769 "expecting unsigned constant integer: "
2771 right
->u
.unary_expression
.type
);
2776 exp_dig
= right
->u
.unary_expression
.u
.unsigned_constant
;
2777 _SET(&set
, _FLOAT_EXP_DIG_SET
);
2778 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2780 if (_IS_SET(&set
, _FLOAT_MANT_DIG_SET
)) {
2781 _BT_LOGE_DUP_ATTR(left
, "mant_dig",
2782 "floating point number field type");
2787 if (right
->u
.unary_expression
.type
!=
2788 UNARY_UNSIGNED_CONSTANT
) {
2789 _BT_LOGE_NODE(right
,
2790 "Invalid `mant_dig` attribute in floating point number field type: "
2791 "expecting unsigned constant integer: "
2793 right
->u
.unary_expression
.type
);
2798 mant_dig
= right
->u
.unary_expression
.u
.
2800 _SET(&set
, _FLOAT_MANT_DIG_SET
);
2801 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2803 if (_IS_SET(&set
, _FLOAT_ALIGN_SET
)) {
2804 _BT_LOGE_DUP_ATTR(left
, "align",
2805 "floating point number field type");
2810 if (right
->u
.unary_expression
.type
!=
2811 UNARY_UNSIGNED_CONSTANT
) {
2812 _BT_LOGE_NODE(right
,
2813 "Invalid `align` attribute in floating point number field type: "
2814 "expecting unsigned constant integer: "
2816 right
->u
.unary_expression
.type
);
2821 alignment
= right
->u
.unary_expression
.u
.
2824 if (!is_align_valid(alignment
)) {
2825 _BT_LOGE_NODE(right
,
2826 "Invalid `align` attribute in floating point number field type: "
2827 "expecting power of two: "
2828 "align=%" PRIu64
, alignment
);
2833 _SET(&set
, _FLOAT_ALIGN_SET
);
2836 "Unknown attribute in floating point number field type: "
2838 left
->u
.unary_expression
.u
.string
);
2842 if (!_IS_SET(&set
, _FLOAT_MANT_DIG_SET
)) {
2843 BT_LOGE_STR("Missing `mant_dig` attribute in floating point number field type.");
2848 if (!_IS_SET(&set
, _FLOAT_EXP_DIG_SET
)) {
2849 BT_LOGE_STR("Missing `exp_dig` attribute in floating point number field type.");
2854 if (!_IS_SET(&set
, _INTEGER_ALIGN_SET
)) {
2855 if ((mant_dig
+ exp_dig
) % CHAR_BIT
) {
2856 /* Bit-packed alignment */
2859 /* Byte-packed alignment */
2860 alignment
= CHAR_BIT
;
2864 *float_decl
= bt_ctf_field_type_floating_point_create();
2866 BT_LOGE_STR("Cannot create floating point number field type.");
2871 ret
= bt_ctf_field_type_floating_point_set_exponent_digits(
2872 *float_decl
, exp_dig
);
2873 ret
|= bt_ctf_field_type_floating_point_set_mantissa_digits(
2874 *float_decl
, mant_dig
);
2875 ret
|= bt_ctf_field_type_set_byte_order(*float_decl
, byte_order
);
2876 ret
|= bt_ctf_field_type_set_alignment(*float_decl
, alignment
);
2878 BT_LOGE_STR("Cannot configure floating point number field type.");
2886 BT_PUT(*float_decl
);
2892 int visit_string_decl(struct ctx
*ctx
,
2893 struct bt_list_head
*expressions
,
2894 struct bt_ctf_field_type
**string_decl
)
2898 struct ctf_node
*expression
;
2899 enum bt_ctf_string_encoding encoding
= BT_CTF_STRING_ENCODING_UTF8
;
2901 *string_decl
= NULL
;
2903 bt_list_for_each_entry(expression
, expressions
, siblings
) {
2904 struct ctf_node
*left
, *right
;
2906 left
= _BT_LIST_FIRST_ENTRY(&expression
->u
.ctf_expression
.left
,
2907 struct ctf_node
, siblings
);
2908 right
= _BT_LIST_FIRST_ENTRY(
2909 &expression
->u
.ctf_expression
.right
, struct ctf_node
,
2912 if (left
->u
.unary_expression
.type
!= UNARY_STRING
) {
2914 "Unexpected unary expression type: type=%d",
2915 left
->u
.unary_expression
.type
);
2920 if (!strcmp(left
->u
.unary_expression
.u
.string
, "encoding")) {
2923 if (_IS_SET(&set
, _STRING_ENCODING_SET
)) {
2924 _BT_LOGE_DUP_ATTR(left
, "encoding",
2925 "string field type");
2930 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
2931 _BT_LOGE_NODE(right
,
2932 "Invalid `encoding` attribute in string field type: "
2933 "expecting unary string.");
2938 s_right
= concatenate_unary_strings(
2939 &expression
->u
.ctf_expression
.right
);
2941 _BT_LOGE_NODE(right
,
2942 "Unexpected unary expression for string field type's `encoding` attribute.");
2947 if (!strcmp(s_right
, "UTF8") ||
2948 !strcmp(s_right
, "utf8") ||
2949 !strcmp(s_right
, "utf-8") ||
2950 !strcmp(s_right
, "UTF-8")) {
2951 encoding
= BT_CTF_STRING_ENCODING_UTF8
;
2952 } else if (!strcmp(s_right
, "ASCII") ||
2953 !strcmp(s_right
, "ascii")) {
2954 encoding
= BT_CTF_STRING_ENCODING_ASCII
;
2955 } else if (!strcmp(s_right
, "none")) {
2956 encoding
= BT_CTF_STRING_ENCODING_NONE
;
2958 _BT_LOGE_NODE(right
,
2959 "Invalid `encoding` attribute in string field type: "
2960 "unknown encoding: encoding=\"%s\"",
2968 _SET(&set
, _STRING_ENCODING_SET
);
2971 "Unknown attribute in string field type: "
2973 left
->u
.unary_expression
.u
.string
);
2977 *string_decl
= bt_ctf_field_type_string_create();
2978 if (!*string_decl
) {
2979 BT_LOGE_STR("Cannot create string field type.");
2984 ret
= bt_ctf_field_type_string_set_encoding(*string_decl
, encoding
);
2986 BT_LOGE_STR("Cannot configure string field type.");
2994 BT_PUT(*string_decl
);
3000 int visit_type_specifier_list(struct ctx
*ctx
,
3001 struct ctf_node
*ts_list
,
3002 struct bt_ctf_field_type
**decl
)
3005 struct ctf_node
*first
, *node
;
3009 if (ts_list
->type
!= NODE_TYPE_SPECIFIER_LIST
) {
3010 _BT_LOGE_NODE(ts_list
,
3011 "Unexpected node type: node-type=%d", ts_list
->type
);
3016 first
= _BT_LIST_FIRST_ENTRY(&ts_list
->u
.type_specifier_list
.head
,
3017 struct ctf_node
, siblings
);
3018 if (first
->type
!= NODE_TYPE_SPECIFIER
) {
3019 _BT_LOGE_NODE(first
,
3020 "Unexpected node type: node-type=%d", first
->type
);
3025 node
= first
->u
.type_specifier
.node
;
3027 switch (first
->u
.type_specifier
.type
) {
3028 case TYPESPEC_INTEGER
:
3029 ret
= visit_integer_decl(ctx
, &node
->u
.integer
.expressions
,
3036 case TYPESPEC_FLOATING_POINT
:
3037 ret
= visit_floating_point_number_decl(ctx
,
3038 &node
->u
.floating_point
.expressions
, decl
);
3044 case TYPESPEC_STRING
:
3045 ret
= visit_string_decl(ctx
,
3046 &node
->u
.string
.expressions
, decl
);
3052 case TYPESPEC_STRUCT
:
3053 ret
= visit_struct_decl(ctx
, node
->u
._struct
.name
,
3054 &node
->u
._struct
.declaration_list
,
3055 node
->u
._struct
.has_body
,
3056 &node
->u
._struct
.min_align
, decl
);
3062 case TYPESPEC_VARIANT
:
3063 ret
= visit_variant_decl(ctx
, node
->u
.variant
.name
,
3064 node
->u
.variant
.choice
,
3065 &node
->u
.variant
.declaration_list
,
3066 node
->u
.variant
.has_body
, decl
);
3073 ret
= visit_enum_decl(ctx
, node
->u
._enum
.enum_id
,
3074 node
->u
._enum
.container_type
,
3075 &node
->u
._enum
.enumerator_list
,
3076 node
->u
._enum
.has_body
, decl
);
3084 case TYPESPEC_SHORT
:
3087 case TYPESPEC_FLOAT
:
3088 case TYPESPEC_DOUBLE
:
3089 case TYPESPEC_SIGNED
:
3090 case TYPESPEC_UNSIGNED
:
3092 case TYPESPEC_COMPLEX
:
3093 case TYPESPEC_IMAGINARY
:
3094 case TYPESPEC_CONST
:
3095 case TYPESPEC_ID_TYPE
:
3096 ret
= visit_type_specifier(ctx
, ts_list
, decl
);
3098 _BT_LOGE_NODE(first
,
3099 "Cannot visit type specifier: ret=%d",
3106 _BT_LOGE_NODE(first
,
3107 "Unexpected type specifier type: node-type=%d",
3108 first
->u
.type_specifier
.type
);
3124 int visit_event_decl_entry(struct ctx
*ctx
, struct ctf_node
*node
,
3125 struct bt_ctf_event_class
*event_class
, int64_t *stream_id
,
3130 _BT_CTF_FIELD_TYPE_INIT(decl
);
3132 switch (node
->type
) {
3134 ret
= visit_typedef(ctx
, node
->u
._typedef
.type_specifier_list
,
3135 &node
->u
._typedef
.type_declarators
);
3138 "Cannot add type definition found in event class.");
3142 case NODE_TYPEALIAS
:
3143 ret
= visit_typealias(ctx
, node
->u
.typealias
.target
,
3144 node
->u
.typealias
.alias
);
3147 "Cannot add type alias found in event class.");
3151 case NODE_CTF_EXPRESSION
:
3153 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
3155 _BT_LOGE_NODE(node
, "Cannot concatenate unary strings.");
3160 if (!strcmp(left
, "name")) {
3161 /* This is already known at this stage */
3162 if (_IS_SET(set
, _EVENT_NAME_SET
)) {
3163 _BT_LOGE_DUP_ATTR(node
, "name",
3169 _SET(set
, _EVENT_NAME_SET
);
3170 } else if (!strcmp(left
, "id")) {
3173 if (_IS_SET(set
, _EVENT_ID_SET
)) {
3174 _BT_LOGE_DUP_ATTR(node
, "id",
3180 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
3182 /* Only read "id" if get_unary_unsigned() succeeded. */
3183 if (ret
|| (!ret
&& id
< 0)) {
3185 "Unexpected unary expression for event class's `id` attribute.");
3190 ret
= bt_ctf_event_class_set_id(event_class
, id
);
3193 "Cannot set event class's ID: "
3198 _SET(set
, _EVENT_ID_SET
);
3199 } else if (!strcmp(left
, "stream_id")) {
3200 if (_IS_SET(set
, _EVENT_STREAM_ID_SET
)) {
3201 _BT_LOGE_DUP_ATTR(node
, "stream_id",
3207 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
3208 (uint64_t *) stream_id
);
3210 * Only read "stream_id" if get_unary_unsigned()
3213 if (ret
|| (!ret
&& *stream_id
< 0)) {
3215 "Unexpected unary expression for event class's `stream_id` attribute.");
3220 _SET(set
, _EVENT_STREAM_ID_SET
);
3221 } else if (!strcmp(left
, "context")) {
3222 if (_IS_SET(set
, _EVENT_CONTEXT_SET
)) {
3224 "Duplicate `context` entry in event class.");
3229 ret
= visit_type_specifier_list(ctx
,
3230 _BT_LIST_FIRST_ENTRY(
3231 &node
->u
.ctf_expression
.right
,
3232 struct ctf_node
, siblings
),
3236 "Cannot create event class's context field type.");
3241 ret
= bt_ctf_event_class_set_context_type(
3246 "Cannot set event class's context field type.");
3250 _SET(set
, _EVENT_CONTEXT_SET
);
3251 } else if (!strcmp(left
, "fields")) {
3252 if (_IS_SET(set
, _EVENT_FIELDS_SET
)) {
3254 "Duplicate `fields` entry in event class.");
3259 ret
= visit_type_specifier_list(ctx
,
3260 _BT_LIST_FIRST_ENTRY(
3261 &node
->u
.ctf_expression
.right
,
3262 struct ctf_node
, siblings
),
3266 "Cannot create event class's payload field type.");
3271 ret
= bt_ctf_event_class_set_payload_type(
3276 "Cannot set event class's payload field type.");
3280 _SET(set
, _EVENT_FIELDS_SET
);
3281 } else if (!strcmp(left
, "loglevel")) {
3282 uint64_t loglevel_value
;
3283 enum bt_ctf_event_class_log_level log_level
=
3284 BT_CTF_EVENT_CLASS_LOG_LEVEL_UNSPECIFIED
;
3286 if (_IS_SET(set
, _EVENT_LOGLEVEL_SET
)) {
3287 _BT_LOGE_DUP_ATTR(node
, "loglevel",
3293 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
3297 "Unexpected unary expression for event class's `loglevel` attribute.");
3302 switch (loglevel_value
) {
3304 log_level
= BT_CTF_EVENT_CLASS_LOG_LEVEL_EMERGENCY
;
3307 log_level
= BT_CTF_EVENT_CLASS_LOG_LEVEL_ALERT
;
3310 log_level
= BT_CTF_EVENT_CLASS_LOG_LEVEL_CRITICAL
;
3313 log_level
= BT_CTF_EVENT_CLASS_LOG_LEVEL_ERROR
;
3316 log_level
= BT_CTF_EVENT_CLASS_LOG_LEVEL_WARNING
;
3319 log_level
= BT_CTF_EVENT_CLASS_LOG_LEVEL_NOTICE
;
3322 log_level
= BT_CTF_EVENT_CLASS_LOG_LEVEL_INFO
;
3325 log_level
= BT_CTF_EVENT_CLASS_LOG_LEVEL_DEBUG_SYSTEM
;
3328 log_level
= BT_CTF_EVENT_CLASS_LOG_LEVEL_DEBUG_PROGRAM
;
3331 log_level
= BT_CTF_EVENT_CLASS_LOG_LEVEL_DEBUG_PROCESS
;
3334 log_level
= BT_CTF_EVENT_CLASS_LOG_LEVEL_DEBUG_MODULE
;
3337 log_level
= BT_CTF_EVENT_CLASS_LOG_LEVEL_DEBUG_UNIT
;
3340 log_level
= BT_CTF_EVENT_CLASS_LOG_LEVEL_DEBUG_FUNCTION
;
3343 log_level
= BT_CTF_EVENT_CLASS_LOG_LEVEL_DEBUG_LINE
;
3346 log_level
= BT_CTF_EVENT_CLASS_LOG_LEVEL_DEBUG
;
3349 _BT_LOGW_NODE(node
, "Not setting event class's log level because its value is unknown: "
3350 "log-level=%" PRIu64
, loglevel_value
);
3353 if (log_level
!= BT_CTF_EVENT_CLASS_LOG_LEVEL_UNSPECIFIED
) {
3354 ret
= bt_ctf_event_class_set_log_level(
3355 event_class
, log_level
);
3358 "Cannot set event class's log level.");
3363 _SET(set
, _EVENT_LOGLEVEL_SET
);
3364 } else if (!strcmp(left
, "model.emf.uri")) {
3367 if (_IS_SET(set
, _EVENT_MODEL_EMF_URI_SET
)) {
3368 _BT_LOGE_DUP_ATTR(node
, "model.emf.uri",
3374 right
= concatenate_unary_strings(
3375 &node
->u
.ctf_expression
.right
);
3378 "Unexpected unary expression for event class's `model.emf.uri` attribute.");
3383 if (strlen(right
) == 0) {
3385 "Not setting event class's EMF URI because it's empty.");
3387 ret
= bt_ctf_event_class_set_emf_uri(
3388 event_class
, right
);
3391 "Cannot set event class's EMF URI.");
3397 _SET(set
, _EVENT_MODEL_EMF_URI_SET
);
3400 "Unknown attribute in event class: "
3401 "attr-name=\"%s\"", left
);
3426 char *get_event_decl_name(struct ctx
*ctx
, struct ctf_node
*node
)
3430 struct ctf_node
*iter
;
3431 struct bt_list_head
*decl_list
= &node
->u
.event
.declaration_list
;
3433 bt_list_for_each_entry(iter
, decl_list
, siblings
) {
3434 if (iter
->type
!= NODE_CTF_EXPRESSION
) {
3438 left
= concatenate_unary_strings(&iter
->u
.ctf_expression
.left
);
3441 "Cannot concatenate unary strings.");
3445 if (!strcmp(left
, "name")) {
3446 name
= concatenate_unary_strings(
3447 &iter
->u
.ctf_expression
.right
);
3450 "Unexpected unary expression for event class's `name` attribute.");
3472 int reset_event_decl_types(struct ctx
*ctx
,
3473 struct bt_ctf_event_class
*event_class
)
3478 ret
= bt_ctf_event_class_set_context_type(event_class
, NULL
);
3480 BT_LOGE("Cannot reset initial event class's context field type: "
3481 "event-name=\"%s\"",
3482 bt_ctf_event_class_get_name(event_class
));
3486 /* Event payload. */
3487 ret
= bt_ctf_event_class_set_payload_type(event_class
, NULL
);
3489 BT_LOGE("Cannot reset initial event class's payload field type: "
3490 "event-name=\"%s\"",
3491 bt_ctf_event_class_get_name(event_class
));
3499 int reset_stream_decl_types(struct ctx
*ctx
,
3500 struct bt_ctf_stream_class
*stream_class
)
3504 /* Packet context. */
3505 ret
= bt_ctf_stream_class_set_packet_context_type(stream_class
, NULL
);
3507 BT_LOGE_STR("Cannot reset initial stream class's packet context field type.");
3512 ret
= bt_ctf_stream_class_set_event_header_type(stream_class
, NULL
);
3514 BT_LOGE_STR("Cannot reset initial stream class's event header field type.");
3518 /* Event context. */
3519 ret
= bt_ctf_stream_class_set_event_context_type(stream_class
, NULL
);
3521 BT_LOGE_STR("Cannot reset initial stream class's event context field type.");
3529 struct bt_ctf_stream_class
*create_reset_stream_class(struct ctx
*ctx
)
3532 struct bt_ctf_stream_class
*stream_class
;
3534 stream_class
= bt_ctf_stream_class_create_empty(NULL
);
3535 if (!stream_class
) {
3536 BT_LOGE_STR("Cannot create empty stream class.");
3541 * Set packet context, event header, and event context to NULL to
3542 * override the default ones.
3544 ret
= reset_stream_decl_types(ctx
, stream_class
);
3549 return stream_class
;
3552 BT_PUT(stream_class
);
3558 int visit_event_decl(struct ctx
*ctx
, struct ctf_node
*node
)
3563 struct ctf_node
*iter
;
3564 int64_t stream_id
= -1;
3565 char *event_name
= NULL
;
3566 struct bt_ctf_event_class
*event_class
= NULL
;
3567 struct bt_ctf_event_class
*eevent_class
;
3568 struct bt_ctf_stream_class
*stream_class
= NULL
;
3569 struct bt_list_head
*decl_list
= &node
->u
.event
.declaration_list
;
3570 bool pop_scope
= false;
3572 if (node
->visited
) {
3576 node
->visited
= TRUE
;
3577 event_name
= get_event_decl_name(ctx
, node
);
3580 "Missing `name` attribute in event class.");
3585 event_class
= bt_ctf_event_class_create(event_name
);
3588 * Unset context and fields to override the default ones.
3590 ret
= reset_event_decl_types(ctx
, event_class
);
3593 "Cannot reset event class's field types: "
3598 ret
= ctx_push_scope(ctx
);
3600 BT_LOGE_STR("Cannot push scope.");
3606 bt_list_for_each_entry(iter
, decl_list
, siblings
) {
3607 ret
= visit_event_decl_entry(ctx
, iter
, event_class
,
3610 _BT_LOGE_NODE(iter
, "Cannot visit event class's entry: "
3616 if (!_IS_SET(&set
, _EVENT_STREAM_ID_SET
)) {
3618 int64_t *new_stream_id
;
3619 struct bt_ctf_stream_class
*new_stream_class
;
3620 size_t stream_class_count
=
3621 g_hash_table_size(ctx
->stream_classes
) +
3622 bt_ctf_trace_get_stream_class_count(ctx
->trace
);
3625 * Allow missing stream_id if there is only a single
3628 switch (stream_class_count
) {
3630 /* Create implicit stream class if there's none */
3632 new_stream_class
= create_reset_stream_class(ctx
);
3633 if (!new_stream_class
) {
3635 "Cannot create empty stream class.");
3640 ret
= bt_ctf_stream_class_set_id(new_stream_class
,
3644 "Cannot set stream class's ID: "
3645 "id=0, ret=%d", ret
);
3646 BT_PUT(new_stream_class
);
3650 new_stream_id
= g_new0(int64_t, 1);
3651 if (!new_stream_id
) {
3652 BT_LOGE_STR("Failed to allocate a int64_t.");
3657 *new_stream_id
= stream_id
;
3659 /* Move reference to visitor's context */
3660 g_hash_table_insert(ctx
->stream_classes
,
3661 new_stream_id
, new_stream_class
);
3662 new_stream_id
= NULL
;
3663 new_stream_class
= NULL
;
3666 /* Single stream class: get its ID */
3667 if (g_hash_table_size(ctx
->stream_classes
) == 1) {
3668 keys
= g_hash_table_get_keys(ctx
->stream_classes
);
3669 stream_id
= *((int64_t *) keys
->data
);
3672 assert(bt_ctf_trace_get_stream_class_count(
3675 bt_ctf_trace_get_stream_class_by_index(
3677 assert(stream_class
);
3678 stream_id
= bt_ctf_stream_class_get_id(
3680 BT_PUT(stream_class
);
3685 "Missing `stream_id` attribute in event class.");
3691 assert(stream_id
>= 0);
3693 /* We have the stream ID now; get the stream class if found */
3694 stream_class
= g_hash_table_lookup(ctx
->stream_classes
, &stream_id
);
3695 bt_get(stream_class
);
3696 if (!stream_class
) {
3697 stream_class
= bt_ctf_trace_get_stream_class_by_id(ctx
->trace
,
3699 if (!stream_class
) {
3701 "Cannot find stream class at this point: "
3702 "id=%" PRId64
, stream_id
);
3708 assert(stream_class
);
3710 if (!_IS_SET(&set
, _EVENT_ID_SET
)) {
3711 /* Allow only one event without ID per stream */
3712 if (bt_ctf_stream_class_get_event_class_count(stream_class
) !=
3715 "Missing `id` attribute in event class.");
3721 ret
= bt_ctf_event_class_set_id(event_class
, 0);
3724 "Cannot set event class's ID: id=0, ret=%d",
3730 event_id
= bt_ctf_event_class_get_id(event_class
);
3732 _BT_LOGE_NODE(node
, "Cannot get event class's ID.");
3737 eevent_class
= bt_ctf_stream_class_get_event_class_by_id(stream_class
,
3740 BT_PUT(eevent_class
);
3742 "Duplicate event class (same ID) in the same stream class: "
3743 "id=%" PRId64
, event_id
);
3748 ret
= bt_ctf_stream_class_add_event_class(stream_class
, event_class
);
3749 BT_PUT(event_class
);
3752 "Cannot add event class to stream class: ret=%d", ret
);
3759 bt_put(event_class
);
3767 bt_put(stream_class
);
3772 int auto_map_field_to_trace_clock_class(struct ctx
*ctx
,
3773 struct bt_ctf_field_type
*ft
)
3775 struct bt_ctf_clock_class
*clock_class_to_map_to
= NULL
;
3776 struct bt_ctf_clock_class
*mapped_clock_class
= NULL
;
3778 int64_t clock_class_count
;
3780 if (!ft
|| !bt_ctf_field_type_is_integer(ft
)) {
3784 mapped_clock_class
=
3785 bt_ctf_field_type_integer_get_mapped_clock_class(ft
);
3786 if (mapped_clock_class
) {
3790 clock_class_count
= bt_ctf_trace_get_clock_class_count(ctx
->trace
);
3791 assert(clock_class_count
>= 0);
3793 switch (clock_class_count
) {
3796 * No clock class exists in the trace at this
3797 * point. Create an implicit one at 1 GHz,
3798 * named `default`, and use this clock class.
3800 clock_class_to_map_to
= bt_ctf_clock_class_create("default",
3802 if (!clock_class_to_map_to
) {
3803 BT_LOGE_STR("Cannot create a clock class.");
3808 ret
= bt_ctf_trace_add_clock_class(ctx
->trace
,
3809 clock_class_to_map_to
);
3811 BT_LOGE_STR("Cannot add clock class to trace.");
3817 * Only one clock class exists in the trace at
3818 * this point: use this one.
3820 clock_class_to_map_to
=
3821 bt_ctf_trace_get_clock_class_by_index(ctx
->trace
, 0);
3822 assert(clock_class_to_map_to
);
3826 * Timestamp field not mapped to a clock class
3827 * and there's more than one clock class in the
3828 * trace: this is an error.
3830 BT_LOGE_STR("Timestamp field found with no mapped clock class, "
3831 "but there's more than one clock class in the trace at this point.");
3836 assert(clock_class_to_map_to
);
3837 ret
= bt_ctf_field_type_integer_set_mapped_clock_class(ft
,
3838 clock_class_to_map_to
);
3840 BT_LOGE("Cannot map field type's field to trace's clock class: "
3841 "clock-class-name=\"%s\", ret=%d",
3842 bt_ctf_clock_class_get_name(clock_class_to_map_to
),
3848 bt_put(clock_class_to_map_to
);
3849 bt_put(mapped_clock_class
);
3854 int auto_map_fields_to_trace_clock_class(struct ctx
*ctx
,
3855 struct bt_ctf_field_type
*root_ft
, const char *field_name
)
3864 if (!bt_ctf_field_type_is_structure(root_ft
) &&
3865 !bt_ctf_field_type_is_variant(root_ft
)) {
3869 if (bt_ctf_field_type_is_structure(root_ft
)) {
3870 count
= bt_ctf_field_type_structure_get_field_count(root_ft
);
3872 count
= bt_ctf_field_type_variant_get_field_count(root_ft
);
3877 for (i
= 0; i
< count
; i
++) {
3878 _BT_CTF_FIELD_TYPE_INIT(ft
);
3881 if (bt_ctf_field_type_is_structure(root_ft
)) {
3882 ret
= bt_ctf_field_type_structure_get_field_by_index(
3883 root_ft
, &name
, &ft
, i
);
3884 } else if (bt_ctf_field_type_is_variant(root_ft
)) {
3885 ret
= bt_ctf_field_type_variant_get_field_by_index(
3886 root_ft
, &name
, &ft
, i
);
3891 if (strcmp(name
, field_name
) == 0) {
3892 ret
= auto_map_field_to_trace_clock_class(ctx
, ft
);
3894 BT_LOGE("Cannot automatically map field to trace's clock class: "
3895 "field-name=\"%s\"", field_name
);
3901 ret
= auto_map_fields_to_trace_clock_class(ctx
, ft
, field_name
);
3904 BT_LOGE("Cannot automatically map structure or variant field type's fields to trace's clock class: "
3905 "field-name=\"%s\", root-field-name=\"%s\"",
3916 int visit_stream_decl_entry(struct ctx
*ctx
, struct ctf_node
*node
,
3917 struct bt_ctf_stream_class
*stream_class
, int *set
)
3921 _BT_CTF_FIELD_TYPE_INIT(decl
);
3923 switch (node
->type
) {
3925 ret
= visit_typedef(ctx
, node
->u
._typedef
.type_specifier_list
,
3926 &node
->u
._typedef
.type_declarators
);
3929 "Cannot add type definition found in stream class.");
3933 case NODE_TYPEALIAS
:
3934 ret
= visit_typealias(ctx
, node
->u
.typealias
.target
,
3935 node
->u
.typealias
.alias
);
3938 "Cannot add type alias found in stream class.");
3942 case NODE_CTF_EXPRESSION
:
3944 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
3946 _BT_LOGE_NODE(node
, "Cannot concatenate unary strings.");
3951 if (!strcmp(left
, "id")) {
3955 if (_IS_SET(set
, _STREAM_ID_SET
)) {
3956 _BT_LOGE_DUP_ATTR(node
, "id",
3957 "stream declaration");
3962 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
3964 /* Only read "id" if get_unary_unsigned() succeeded. */
3965 if (ret
|| (!ret
&& id
< 0)) {
3967 "Unexpected unary expression for stream class's `id` attribute.");
3972 ptr
= g_hash_table_lookup(ctx
->stream_classes
, &id
);
3975 "Duplicate stream class (same ID): id=%" PRId64
,
3981 ret
= bt_ctf_stream_class_set_id(stream_class
, id
);
3984 "Cannot set stream class's ID: "
3985 "id=%" PRId64
", ret=%d", id
, ret
);
3989 _SET(set
, _STREAM_ID_SET
);
3990 } else if (!strcmp(left
, "event.header")) {
3991 if (_IS_SET(set
, _STREAM_EVENT_HEADER_SET
)) {
3993 "Duplicate `event.header` entry in stream class.");
3998 ret
= visit_type_specifier_list(ctx
,
3999 _BT_LIST_FIRST_ENTRY(
4000 &node
->u
.ctf_expression
.right
,
4001 struct ctf_node
, siblings
),
4005 "Cannot create stream class's event header field type.");
4010 ret
= auto_map_fields_to_trace_clock_class(ctx
,
4014 "Cannot automatically map specific event header field type fields named `timestamp` to trace's clock class.");
4018 ret
= bt_ctf_stream_class_set_event_header_type(
4019 stream_class
, decl
);
4023 "Cannot set stream class's event header field type.");
4027 _SET(set
, _STREAM_EVENT_HEADER_SET
);
4028 } else if (!strcmp(left
, "event.context")) {
4029 if (_IS_SET(set
, _STREAM_EVENT_CONTEXT_SET
)) {
4031 "Duplicate `event.context` entry in stream class.");
4036 ret
= visit_type_specifier_list(ctx
,
4037 _BT_LIST_FIRST_ENTRY(
4038 &node
->u
.ctf_expression
.right
,
4039 struct ctf_node
, siblings
),
4043 "Cannot create stream class's event context field type.");
4049 ret
= bt_ctf_stream_class_set_event_context_type(
4050 stream_class
, decl
);
4054 "Cannot set stream class's event context field type.");
4058 _SET(set
, _STREAM_EVENT_CONTEXT_SET
);
4059 } else if (!strcmp(left
, "packet.context")) {
4060 if (_IS_SET(set
, _STREAM_PACKET_CONTEXT_SET
)) {
4062 "Duplicate `packet.context` entry in stream class.");
4067 ret
= visit_type_specifier_list(ctx
,
4068 _BT_LIST_FIRST_ENTRY(
4069 &node
->u
.ctf_expression
.right
,
4070 struct ctf_node
, siblings
),
4074 "Cannot create stream class's packet context field type.");
4079 ret
= auto_map_fields_to_trace_clock_class(ctx
,
4080 decl
, "timestamp_begin");
4083 "Cannot automatically map specific packet context field type fields named `timestamp_begin` to trace's clock class.");
4087 ret
= auto_map_fields_to_trace_clock_class(ctx
,
4088 decl
, "timestamp_end");
4091 "Cannot automatically map specific packet context field type fields named `timestamp_end` to trace's clock class.");
4095 ret
= bt_ctf_stream_class_set_packet_context_type(
4096 stream_class
, decl
);
4100 "Cannot set stream class's packet context field type.");
4104 _SET(set
, _STREAM_PACKET_CONTEXT_SET
);
4107 "Unknown attribute in stream class: "
4108 "attr-name=\"%s\"", left
);
4131 int visit_stream_decl(struct ctx
*ctx
, struct ctf_node
*node
)
4137 struct ctf_node
*iter
;
4138 struct bt_ctf_stream_class
*stream_class
= NULL
;
4139 struct bt_ctf_stream_class
*existing_stream_class
= NULL
;
4140 struct bt_list_head
*decl_list
= &node
->u
.stream
.declaration_list
;
4142 if (node
->visited
) {
4146 node
->visited
= TRUE
;
4147 stream_class
= create_reset_stream_class(ctx
);
4148 if (!stream_class
) {
4149 _BT_LOGE_NODE(node
, "Cannot create empty stream class.");
4154 ret
= ctx_push_scope(ctx
);
4156 BT_LOGE_STR("Cannot push scope.");
4160 bt_list_for_each_entry(iter
, decl_list
, siblings
) {
4161 ret
= visit_stream_decl_entry(ctx
, iter
, stream_class
, &set
);
4164 "Cannot visit stream class's entry: "
4173 if (_IS_SET(&set
, _STREAM_ID_SET
)) {
4174 /* Check that packet header has stream_id field */
4175 _BT_CTF_FIELD_TYPE_INIT(stream_id_decl
);
4176 _BT_CTF_FIELD_TYPE_INIT(packet_header_decl
);
4178 packet_header_decl
=
4179 bt_ctf_trace_get_packet_header_type(ctx
->trace
);
4180 if (!packet_header_decl
) {
4182 "Stream class has a `id` attribute, "
4183 "but trace has no packet header field type.");
4188 bt_ctf_field_type_structure_get_field_type_by_name(
4189 packet_header_decl
, "stream_id");
4190 BT_PUT(packet_header_decl
);
4191 if (!stream_id_decl
) {
4193 "Stream class has a `id` attribute, "
4194 "but trace's packet header field type has no `stream_id` field.");
4198 if (!bt_ctf_field_type_is_integer(stream_id_decl
)) {
4199 BT_PUT(stream_id_decl
);
4201 "Stream class has a `id` attribute, "
4202 "but trace's packet header field type's `stream_id` field is not an integer field type.");
4206 BT_PUT(stream_id_decl
);
4208 /* Allow only _one_ ID-less stream */
4209 if (g_hash_table_size(ctx
->stream_classes
) != 0) {
4211 "Missing `id` attribute in stream class as there's more than one stream class in the trace.");
4216 /* Automatic ID: 0 */
4217 ret
= bt_ctf_stream_class_set_id(stream_class
, 0);
4221 id
= bt_ctf_stream_class_get_id(stream_class
);
4224 "Cannot get stream class's ID.");
4230 * Make sure that this stream class's ID is currently unique in
4233 existing_stream_class
= bt_ctf_trace_get_stream_class_by_id(ctx
->trace
,
4235 if (g_hash_table_lookup(ctx
->stream_classes
, &id
) ||
4236 existing_stream_class
) {
4238 "Duplicate stream class (same ID): id=%" PRId64
,
4244 new_id
= g_new0(int64_t, 1);
4246 BT_LOGE_STR("Failed to allocate a int64_t.");
4252 /* Move reference to visitor's context */
4253 g_hash_table_insert(ctx
->stream_classes
, new_id
,
4255 stream_class
= NULL
;
4259 bt_put(stream_class
);
4262 bt_put(existing_stream_class
);
4267 int visit_trace_decl_entry(struct ctx
*ctx
, struct ctf_node
*node
, int *set
)
4271 _BT_CTF_FIELD_TYPE_INIT(packet_header_decl
);
4273 switch (node
->type
) {
4275 ret
= visit_typedef(ctx
, node
->u
._typedef
.type_specifier_list
,
4276 &node
->u
._typedef
.type_declarators
);
4279 "Cannot add type definition found in trace (`trace` block).");
4283 case NODE_TYPEALIAS
:
4284 ret
= visit_typealias(ctx
, node
->u
.typealias
.target
,
4285 node
->u
.typealias
.alias
);
4288 "Cannot add type alias found in trace (`trace` block).");
4292 case NODE_CTF_EXPRESSION
:
4294 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
4296 _BT_LOGE_NODE(node
, "Cannot concatenate unary strings.");
4301 if (!strcmp(left
, "major")) {
4302 if (_IS_SET(set
, _TRACE_MAJOR_SET
)) {
4303 _BT_LOGE_DUP_ATTR(node
, "major", "trace");
4308 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
4312 "Unexpected unary expression for trace's `major` attribute.");
4317 _SET(set
, _TRACE_MAJOR_SET
);
4318 } else if (!strcmp(left
, "minor")) {
4319 if (_IS_SET(set
, _TRACE_MINOR_SET
)) {
4320 _BT_LOGE_DUP_ATTR(node
, "minor", "trace");
4325 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
4329 "Unexpected unary expression for trace's `minor` attribute.");
4334 _SET(set
, _TRACE_MINOR_SET
);
4335 } else if (!strcmp(left
, "uuid")) {
4336 if (_IS_SET(set
, _TRACE_UUID_SET
)) {
4337 _BT_LOGE_DUP_ATTR(node
, "uuid", "trace");
4342 ret
= get_unary_uuid(&node
->u
.ctf_expression
.right
,
4346 "Invalid trace's `uuid` attribute.");
4350 ret
= bt_ctf_trace_set_uuid(ctx
->trace
, ctx
->trace_uuid
);
4352 _BT_LOGE_NODE(node
, "Cannot set trace's UUID.");
4356 _SET(set
, _TRACE_UUID_SET
);
4357 } else if (!strcmp(left
, "byte_order")) {
4358 /* Native byte order is already known at this stage */
4359 if (_IS_SET(set
, _TRACE_BYTE_ORDER_SET
)) {
4360 _BT_LOGE_DUP_ATTR(node
, "byte_order",
4366 _SET(set
, _TRACE_BYTE_ORDER_SET
);
4367 } else if (!strcmp(left
, "packet.header")) {
4368 if (_IS_SET(set
, _TRACE_PACKET_HEADER_SET
)) {
4370 "Duplicate `packet.header` entry in trace.");
4375 ret
= visit_type_specifier_list(ctx
,
4376 _BT_LIST_FIRST_ENTRY(
4377 &node
->u
.ctf_expression
.right
,
4378 struct ctf_node
, siblings
),
4379 &packet_header_decl
);
4382 "Cannot create trace's packet header field type.");
4386 assert(packet_header_decl
);
4387 ret
= bt_ctf_trace_set_packet_header_type(ctx
->trace
,
4388 packet_header_decl
);
4389 BT_PUT(packet_header_decl
);
4392 "Cannot set trace's packet header field type.");
4396 _SET(set
, _TRACE_PACKET_HEADER_SET
);
4399 "Unknown attribute in stream class: "
4400 "attr-name=\"%s\"", left
);
4408 _BT_LOGE_NODE(node
, "Unknown expression in trace.");
4417 BT_PUT(packet_header_decl
);
4423 int visit_trace_decl(struct ctx
*ctx
, struct ctf_node
*node
)
4427 struct ctf_node
*iter
;
4428 struct bt_list_head
*decl_list
= &node
->u
.trace
.declaration_list
;
4430 if (node
->visited
) {
4434 node
->visited
= TRUE
;
4436 if (ctx
->is_trace_visited
) {
4437 _BT_LOGE_NODE(node
, "Duplicate trace (`trace` block).");
4442 ret
= ctx_push_scope(ctx
);
4444 BT_LOGE_STR("Cannot push scope.");
4448 bt_list_for_each_entry(iter
, decl_list
, siblings
) {
4449 ret
= visit_trace_decl_entry(ctx
, iter
, &set
);
4451 _BT_LOGE_NODE(iter
, "Cannot visit trace's entry (`trace` block): "
4460 if (!_IS_SET(&set
, _TRACE_MAJOR_SET
)) {
4462 "Missing `major` attribute in trace (`trace` block).");
4467 if (!_IS_SET(&set
, _TRACE_MINOR_SET
)) {
4469 "Missing `minor` attribute in trace (`trace` block).");
4474 if (!_IS_SET(&set
, _TRACE_BYTE_ORDER_SET
)) {
4476 "Missing `byte_order` attribute in trace (`trace` block).");
4481 ctx
->is_trace_visited
= TRUE
;
4491 int visit_env(struct ctx
*ctx
, struct ctf_node
*node
)
4495 struct ctf_node
*entry_node
;
4496 struct bt_list_head
*decl_list
= &node
->u
.env
.declaration_list
;
4498 if (node
->visited
) {
4502 node
->visited
= TRUE
;
4504 bt_list_for_each_entry(entry_node
, decl_list
, siblings
) {
4505 struct bt_list_head
*right_head
=
4506 &entry_node
->u
.ctf_expression
.right
;
4508 if (entry_node
->type
!= NODE_CTF_EXPRESSION
) {
4509 _BT_LOGE_NODE(entry_node
,
4510 "Wrong expression in environment entry: "
4511 "node-type=%d", entry_node
->type
);
4516 left
= concatenate_unary_strings(
4517 &entry_node
->u
.ctf_expression
.left
);
4519 _BT_LOGE_NODE(entry_node
,
4520 "Cannot get environment entry's name.");
4525 if (is_unary_string(right_head
)) {
4526 char *right
= concatenate_unary_strings(right_head
);
4529 _BT_LOGE_NODE(entry_node
,
4530 "Unexpected unary expression for environment entry's value: "
4531 "name=\"%s\"", left
);
4536 if (strcmp(left
, "tracer_name") == 0) {
4537 if (strncmp(right
, "lttng", 5) == 0) {
4538 BT_LOGI("Detected LTTng trace from `%s` environment value: "
4539 "tracer-name=\"%s\"",
4545 ret
= bt_ctf_trace_set_environment_field_string(
4546 ctx
->trace
, left
, right
);
4550 _BT_LOGE_NODE(entry_node
,
4551 "Cannot add string environment entry to trace: "
4552 "name=\"%s\", ret=%d", left
, ret
);
4555 } else if (is_unary_unsigned(right_head
) ||
4556 is_unary_signed(right_head
)) {
4559 if (is_unary_unsigned(right_head
)) {
4560 ret
= get_unary_unsigned(right_head
,
4563 ret
= get_unary_signed(right_head
, &v
);
4566 _BT_LOGE_NODE(entry_node
,
4567 "Unexpected unary expression for environment entry's value: "
4568 "name=\"%s\"", left
);
4573 ret
= bt_ctf_trace_set_environment_field_integer(
4574 ctx
->trace
, left
, v
);
4576 _BT_LOGE_NODE(entry_node
,
4577 "Cannot add integer environment entry to trace: "
4578 "name=\"%s\", ret=%d", left
, ret
);
4582 _BT_LOGW_NODE(entry_node
,
4583 "Environment entry has unknown type: "
4584 "name=\"%s\"", left
);
4601 int set_trace_byte_order(struct ctx
*ctx
, struct ctf_node
*trace_node
)
4606 struct ctf_node
*node
;
4607 struct bt_list_head
*decl_list
= &trace_node
->u
.trace
.declaration_list
;
4609 bt_list_for_each_entry(node
, decl_list
, siblings
) {
4610 if (node
->type
== NODE_CTF_EXPRESSION
) {
4611 struct ctf_node
*right_node
;
4613 left
= concatenate_unary_strings(
4614 &node
->u
.ctf_expression
.left
);
4617 "Cannot concatenate unary strings.");
4622 if (!strcmp(left
, "byte_order")) {
4623 enum bt_ctf_byte_order bo
;
4625 if (_IS_SET(&set
, _TRACE_BYTE_ORDER_SET
)) {
4626 _BT_LOGE_DUP_ATTR(node
, "byte_order",
4632 _SET(&set
, _TRACE_BYTE_ORDER_SET
);
4633 right_node
= _BT_LIST_FIRST_ENTRY(
4634 &node
->u
.ctf_expression
.right
,
4635 struct ctf_node
, siblings
);
4636 bo
= byte_order_from_unary_expr(right_node
);
4637 if (bo
== BT_CTF_BYTE_ORDER_UNKNOWN
) {
4639 "Invalid `byte_order` attribute in trace (`trace` block): "
4640 "expecting `le`, `be`, or `network`.");
4643 } else if (bo
== BT_CTF_BYTE_ORDER_NATIVE
) {
4645 "Invalid `byte_order` attribute in trace (`trace` block): "
4646 "cannot be set to `native` here.");
4652 ret
= bt_ctf_trace_set_native_byte_order(
4656 "Cannot set trace's byte order: "
4667 if (!_IS_SET(&set
, _TRACE_BYTE_ORDER_SET
)) {
4668 _BT_LOGE_NODE(trace_node
,
4669 "Missing `byte_order` attribute in trace (`trace` block).");
4683 int visit_clock_decl_entry(struct ctx
*ctx
, struct ctf_node
*entry_node
,
4684 struct bt_ctf_clock_class
*clock
, int *set
)
4689 if (entry_node
->type
!= NODE_CTF_EXPRESSION
) {
4690 _BT_LOGE_NODE(entry_node
,
4691 "Unexpected node type: node-type=%d",
4697 left
= concatenate_unary_strings(&entry_node
->u
.ctf_expression
.left
);
4699 _BT_LOGE_NODE(entry_node
, "Cannot concatenate unary strings.");
4704 if (!strcmp(left
, "name")) {
4707 if (_IS_SET(set
, _CLOCK_NAME_SET
)) {
4708 _BT_LOGE_DUP_ATTR(entry_node
, "name", "clock class");
4713 right
= concatenate_unary_strings(
4714 &entry_node
->u
.ctf_expression
.right
);
4716 _BT_LOGE_NODE(entry_node
,
4717 "Unexpected unary expression for clock class's `name` attribute.");
4722 ret
= bt_ctf_clock_class_set_name(clock
, right
);
4724 _BT_LOGE_NODE(entry_node
,
4725 "cannot set clock class's name");
4731 _SET(set
, _CLOCK_NAME_SET
);
4732 } else if (!strcmp(left
, "uuid")) {
4733 unsigned char uuid
[BABELTRACE_UUID_LEN
];
4735 if (_IS_SET(set
, _CLOCK_UUID_SET
)) {
4736 _BT_LOGE_DUP_ATTR(entry_node
, "uuid", "clock class");
4741 ret
= get_unary_uuid(&entry_node
->u
.ctf_expression
.right
, uuid
);
4743 _BT_LOGE_NODE(entry_node
,
4744 "Invalid clock class's `uuid` attribute.");
4748 ret
= bt_ctf_clock_class_set_uuid(clock
, uuid
);
4750 _BT_LOGE_NODE(entry_node
,
4751 "Cannot set clock class's UUID.");
4755 _SET(set
, _CLOCK_UUID_SET
);
4756 } else if (!strcmp(left
, "description")) {
4759 if (_IS_SET(set
, _CLOCK_DESCRIPTION_SET
)) {
4760 _BT_LOGE_DUP_ATTR(entry_node
, "description",
4766 right
= concatenate_unary_strings(
4767 &entry_node
->u
.ctf_expression
.right
);
4769 _BT_LOGE_NODE(entry_node
,
4770 "Unexpected unary expression for clock class's `description` attribute.");
4775 ret
= bt_ctf_clock_class_set_description(clock
, right
);
4777 _BT_LOGE_NODE(entry_node
,
4778 "Cannot set clock class's description.");
4784 _SET(set
, _CLOCK_DESCRIPTION_SET
);
4785 } else if (!strcmp(left
, "freq")) {
4788 if (_IS_SET(set
, _CLOCK_FREQ_SET
)) {
4789 _BT_LOGE_DUP_ATTR(entry_node
, "freq", "clock class");
4794 ret
= get_unary_unsigned(
4795 &entry_node
->u
.ctf_expression
.right
, &freq
);
4797 _BT_LOGE_NODE(entry_node
,
4798 "Unexpected unary expression for clock class's `freq` attribute.");
4803 if (freq
== -1ULL || freq
== 0) {
4804 _BT_LOGE_NODE(entry_node
,
4805 "Invalid clock class frequency: freq=%" PRIu64
,
4811 ret
= bt_ctf_clock_class_set_frequency(clock
, freq
);
4813 _BT_LOGE_NODE(entry_node
,
4814 "Cannot set clock class's frequency.");
4818 _SET(set
, _CLOCK_FREQ_SET
);
4819 } else if (!strcmp(left
, "precision")) {
4822 if (_IS_SET(set
, _CLOCK_PRECISION_SET
)) {
4823 _BT_LOGE_DUP_ATTR(entry_node
, "precision",
4829 ret
= get_unary_unsigned(
4830 &entry_node
->u
.ctf_expression
.right
, &precision
);
4832 _BT_LOGE_NODE(entry_node
,
4833 "Unexpected unary expression for clock class's `precision` attribute.");
4838 ret
= bt_ctf_clock_class_set_precision(clock
, precision
);
4840 _BT_LOGE_NODE(entry_node
,
4841 "Cannot set clock class's precision.");
4845 _SET(set
, _CLOCK_PRECISION_SET
);
4846 } else if (!strcmp(left
, "offset_s")) {
4849 if (_IS_SET(set
, _CLOCK_OFFSET_S_SET
)) {
4850 _BT_LOGE_DUP_ATTR(entry_node
, "offset_s",
4856 ret
= get_unary_signed(
4857 &entry_node
->u
.ctf_expression
.right
, &offset_s
);
4859 _BT_LOGE_NODE(entry_node
,
4860 "Unexpected unary expression for clock class's `offset_s` attribute.");
4865 ret
= bt_ctf_clock_class_set_offset_s(clock
, offset_s
);
4867 _BT_LOGE_NODE(entry_node
,
4868 "Cannot set clock class's offset in seconds.");
4872 _SET(set
, _CLOCK_OFFSET_S_SET
);
4873 } else if (!strcmp(left
, "offset")) {
4876 if (_IS_SET(set
, _CLOCK_OFFSET_SET
)) {
4877 _BT_LOGE_DUP_ATTR(entry_node
, "offset", "clock class");
4882 ret
= get_unary_signed(
4883 &entry_node
->u
.ctf_expression
.right
, &offset
);
4885 _BT_LOGE_NODE(entry_node
,
4886 "Unexpected unary expression for clock class's `offset` attribute.");
4891 ret
= bt_ctf_clock_class_set_offset_cycles(clock
, offset
);
4893 _BT_LOGE_NODE(entry_node
,
4894 "Cannot set clock class's offset in cycles.");
4898 _SET(set
, _CLOCK_OFFSET_SET
);
4899 } else if (!strcmp(left
, "absolute")) {
4900 struct ctf_node
*right
;
4902 if (_IS_SET(set
, _CLOCK_ABSOLUTE_SET
)) {
4903 _BT_LOGE_DUP_ATTR(entry_node
, "absolute",
4909 right
= _BT_LIST_FIRST_ENTRY(
4910 &entry_node
->u
.ctf_expression
.right
,
4911 struct ctf_node
, siblings
);
4912 ret
= get_boolean(right
);
4914 _BT_LOGE_NODE(entry_node
,
4915 "Unexpected unary expression for clock class's `absolute` attribute.");
4920 ret
= bt_ctf_clock_class_set_is_absolute(clock
, ret
);
4922 _BT_LOGE_NODE(entry_node
,
4923 "Cannot set clock class's absolute flag.");
4927 _SET(set
, _CLOCK_ABSOLUTE_SET
);
4929 _BT_LOGW_NODE(entry_node
,
4930 "Unknown attribute in clock class: attr-name=\"%s\"",
4946 int64_t cycles_from_ns(uint64_t frequency
, int64_t ns
)
4951 if (frequency
== 1000000000ULL) {
4954 cycles
= (uint64_t) (((double) ns
* (double) frequency
) / 1e9
);
4961 int apply_clock_class_offset(struct ctx
*ctx
, struct bt_ctf_clock_class
*clock
)
4965 int64_t offset_cycles
;
4966 int64_t offset_to_apply
;
4968 freq
= bt_ctf_clock_class_get_frequency(clock
);
4969 if (freq
== -1ULL) {
4970 BT_LOGE_STR("Cannot get clock class's frequency.");
4975 ret
= bt_ctf_clock_class_get_offset_cycles(clock
, &offset_cycles
);
4977 BT_LOGE_STR("Cannot get clock class's offset in cycles.");
4983 ctx
->decoder_config
.clock_class_offset_s
* 1000000000LL +
4984 ctx
->decoder_config
.clock_class_offset_ns
;
4985 offset_cycles
+= cycles_from_ns(freq
, offset_to_apply
);
4986 ret
= bt_ctf_clock_class_set_offset_cycles(clock
, offset_cycles
);
4993 int visit_clock_decl(struct ctx
*ctx
, struct ctf_node
*clock_node
)
4997 struct bt_ctf_clock_class
*clock
;
4998 struct ctf_node
*entry_node
;
4999 struct bt_list_head
*decl_list
= &clock_node
->u
.clock
.declaration_list
;
5000 const char *clock_class_name
;
5002 if (clock_node
->visited
) {
5006 clock_node
->visited
= TRUE
;
5008 /* CTF 1.8's default frequency for a clock class is 1 GHz */
5009 clock
= bt_ctf_clock_class_create(NULL
, 1000000000);
5011 _BT_LOGE_NODE(clock_node
,
5012 "Cannot create default clock class.");
5017 bt_list_for_each_entry(entry_node
, decl_list
, siblings
) {
5018 ret
= visit_clock_decl_entry(ctx
, entry_node
, clock
, &set
);
5020 _BT_LOGE_NODE(entry_node
,
5021 "Cannot visit clock class's entry: ret=%d",
5027 if (!_IS_SET(&set
, _CLOCK_NAME_SET
)) {
5028 _BT_LOGE_NODE(clock_node
,
5029 "Missing `name` attribute in clock class.");
5034 clock_class_name
= bt_ctf_clock_class_get_name(clock
);
5035 assert(clock_class_name
);
5036 if (ctx
->is_lttng
&& strcmp(clock_class_name
, "monotonic") == 0) {
5038 * Old versions of LTTng forgot to set its clock class
5039 * as absolute, even if it is. This is important because
5040 * it's a condition to be able to sort notifications
5041 * from different sources.
5043 ret
= bt_ctf_clock_class_set_is_absolute(clock
, 1);
5045 _BT_LOGE_NODE(clock_node
,
5046 "Cannot set clock class's absolute flag.");
5051 ret
= apply_clock_class_offset(ctx
, clock
);
5053 _BT_LOGE_NODE(clock_node
,
5054 "Cannot apply clock class's custom offset.");
5058 ret
= bt_ctf_trace_add_clock_class(ctx
->trace
, clock
);
5060 _BT_LOGE_NODE(clock_node
,
5061 "Cannot add clock class to trace.");
5072 int visit_root_decl(struct ctx
*ctx
, struct ctf_node
*root_decl_node
)
5076 if (root_decl_node
->visited
) {
5080 root_decl_node
->visited
= TRUE
;
5082 switch (root_decl_node
->type
) {
5084 ret
= visit_typedef(ctx
,
5085 root_decl_node
->u
._typedef
.type_specifier_list
,
5086 &root_decl_node
->u
._typedef
.type_declarators
);
5088 _BT_LOGE_NODE(root_decl_node
,
5089 "Cannot add type definition found in root scope.");
5093 case NODE_TYPEALIAS
:
5094 ret
= visit_typealias(ctx
, root_decl_node
->u
.typealias
.target
,
5095 root_decl_node
->u
.typealias
.alias
);
5097 _BT_LOGE_NODE(root_decl_node
,
5098 "Cannot add type alias found in root scope.");
5102 case NODE_TYPE_SPECIFIER_LIST
:
5104 _BT_CTF_FIELD_TYPE_INIT(decl
);
5107 * Just add the type specifier to the root
5108 * declaration scope. Put local reference.
5110 ret
= visit_type_specifier_list(ctx
, root_decl_node
, &decl
);
5112 _BT_LOGE_NODE(root_decl_node
,
5113 "Cannot visit root scope's field type: "
5123 _BT_LOGE_NODE(root_decl_node
,
5124 "Unexpected node type: node-type=%d",
5125 root_decl_node
->type
);
5135 int set_trace_name(struct ctx
*ctx
)
5139 struct bt_value
*value
= NULL
;
5141 assert(bt_ctf_trace_get_stream_class_count(ctx
->trace
) == 0);
5142 name
= g_string_new(NULL
);
5144 BT_LOGE_STR("Failed to allocate a GString.");
5150 * Check if we have a trace environment string value named `hostname`.
5151 * If so, use it as the trace name's prefix.
5153 value
= bt_ctf_trace_get_environment_field_value_by_name(ctx
->trace
,
5155 if (bt_value_is_string(value
)) {
5156 const char *hostname
;
5158 ret
= bt_value_string_get(value
, &hostname
);
5160 g_string_append(name
, hostname
);
5162 if (ctx
->trace_name_suffix
) {
5163 g_string_append_c(name
, G_DIR_SEPARATOR
);
5167 if (ctx
->trace_name_suffix
) {
5168 g_string_append(name
, ctx
->trace_name_suffix
);
5171 ret
= bt_ctf_trace_set_name(ctx
->trace
, name
->str
);
5173 BT_LOGE("Cannot set trace's name: name=\"%s\"", name
->str
);
5186 g_string_free(name
, TRUE
);
5193 int move_ctx_stream_classes_to_trace(struct ctx
*ctx
)
5196 GHashTableIter iter
;
5197 gpointer key
, stream_class
;
5199 if (g_hash_table_size(ctx
->stream_classes
) > 0 &&
5200 bt_ctf_trace_get_stream_class_count(ctx
->trace
) == 0) {
5202 * We're about to add the first stream class to the
5203 * trace. This will freeze the trace, and after this
5204 * we cannot set the name anymore. At this point,
5205 * set the trace name.
5207 ret
= set_trace_name(ctx
);
5209 BT_LOGE_STR("Cannot set trace's name.");
5214 g_hash_table_iter_init(&iter
, ctx
->stream_classes
);
5216 while (g_hash_table_iter_next(&iter
, &key
, &stream_class
)) {
5217 ret
= bt_ctf_trace_add_stream_class(ctx
->trace
,
5220 int64_t id
= bt_ctf_stream_class_get_id(stream_class
);
5221 BT_LOGE("Cannot add stream class to trace: id=%" PRId64
,
5227 g_hash_table_remove_all(ctx
->stream_classes
);
5234 struct ctf_visitor_generate_ir
*ctf_visitor_generate_ir_create(
5235 const struct ctf_metadata_decoder_config
*decoder_config
,
5239 struct ctx
*ctx
= NULL
;
5240 struct bt_ctf_trace
*trace
;
5242 trace
= bt_ctf_trace_create();
5244 BT_LOGE_STR("Cannot create empty trace.");
5248 /* Set packet header to NULL to override the default one */
5249 ret
= bt_ctf_trace_set_packet_header_type(trace
, NULL
);
5251 BT_LOGE_STR("Cannot reset initial trace's packet header field type.");
5255 /* Create visitor's context */
5256 ctx
= ctx_create(trace
, decoder_config
, name
);
5258 BT_LOGE_STR("Cannot create visitor's context.");
5271 return (void *) ctx
;
5275 void ctf_visitor_generate_ir_destroy(struct ctf_visitor_generate_ir
*visitor
)
5277 ctx_destroy((void *) visitor
);
5281 struct bt_ctf_trace
*ctf_visitor_generate_ir_get_trace(
5282 struct ctf_visitor_generate_ir
*visitor
)
5284 struct ctx
*ctx
= (void *) visitor
;
5288 return bt_get(ctx
->trace
);
5292 int ctf_visitor_generate_ir_visit_node(struct ctf_visitor_generate_ir
*visitor
,
5293 struct ctf_node
*node
)
5296 struct ctx
*ctx
= (void *) visitor
;
5298 BT_LOGI_STR("Visiting metadata's AST to generate CTF IR objects.");
5300 switch (node
->type
) {
5303 struct ctf_node
*iter
;
5304 int got_trace_decl
= FALSE
;
5307 * The first thing we need is the native byte order of
5308 * the trace block, because early type aliases can have
5309 * a `byte_order` attribute set to `native`. If we don't
5310 * have the native byte order yet, and we don't have any
5311 * trace block yet, then fail with EINCOMPLETE.
5313 if (ctx
->trace_bo
== BT_CTF_BYTE_ORDER_NATIVE
) {
5314 bt_list_for_each_entry(iter
, &node
->u
.root
.trace
, siblings
) {
5315 if (got_trace_decl
) {
5317 "Duplicate trace (`trace` block).");
5322 ret
= set_trace_byte_order(ctx
, iter
);
5325 "Cannot set trace's native byte order: "
5330 got_trace_decl
= TRUE
;
5333 if (!got_trace_decl
) {
5334 BT_LOGD_STR("Incomplete AST: need trace (`trace` block).");
5340 assert(ctx
->trace_bo
== BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
||
5341 ctx
->trace_bo
== BT_CTF_BYTE_ORDER_BIG_ENDIAN
);
5342 assert(ctx
->current_scope
&&
5343 ctx
->current_scope
->parent_scope
== NULL
);
5346 bt_list_for_each_entry(iter
, &node
->u
.root
.env
, siblings
) {
5347 ret
= visit_env(ctx
, iter
);
5350 "Cannot visit trace's environment (`env` block) entry: "
5356 assert(ctx
->current_scope
&&
5357 ctx
->current_scope
->parent_scope
== NULL
);
5360 * Visit clock blocks.
5362 bt_list_for_each_entry(iter
, &node
->u
.root
.clock
, siblings
) {
5363 ret
= visit_clock_decl(ctx
, iter
);
5366 "Cannot visit clock class: ret=%d",
5372 assert(ctx
->current_scope
&&
5373 ctx
->current_scope
->parent_scope
== NULL
);
5376 * Visit root declarations next, as they can be used by any
5379 bt_list_for_each_entry(iter
, &node
->u
.root
.declaration_list
,
5381 ret
= visit_root_decl(ctx
, iter
);
5384 "Cannot visit root entry: ret=%d",
5390 assert(ctx
->current_scope
&&
5391 ctx
->current_scope
->parent_scope
== NULL
);
5393 /* Callsite blocks are not supported */
5394 bt_list_for_each_entry(iter
, &node
->u
.root
.callsite
, siblings
) {
5396 "\"callsite\" blocks are not supported as of this version.");
5399 assert(ctx
->current_scope
&&
5400 ctx
->current_scope
->parent_scope
== NULL
);
5403 bt_list_for_each_entry(iter
, &node
->u
.root
.trace
, siblings
) {
5404 ret
= visit_trace_decl(ctx
, iter
);
5407 "Cannot visit trace (`trace` block): "
5413 assert(ctx
->current_scope
&&
5414 ctx
->current_scope
->parent_scope
== NULL
);
5417 bt_list_for_each_entry(iter
, &node
->u
.root
.stream
, siblings
) {
5418 ret
= visit_stream_decl(ctx
, iter
);
5421 "Cannot visit stream class: ret=%d",
5427 assert(ctx
->current_scope
&&
5428 ctx
->current_scope
->parent_scope
== NULL
);
5431 bt_list_for_each_entry(iter
, &node
->u
.root
.event
, siblings
) {
5432 ret
= visit_event_decl(ctx
, iter
);
5435 "Cannot visit event class: ret=%d",
5441 assert(ctx
->current_scope
&&
5442 ctx
->current_scope
->parent_scope
== NULL
);
5447 "Unexpected node type: node-type=%d",
5453 /* Move decoded stream classes to trace, if any */
5454 ret
= move_ctx_stream_classes_to_trace(ctx
);
5456 BT_LOGE("Cannot move stream classes to trace: ret=%d", ret
);