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/ref.h>
46 #include <babeltrace/ctf-ir/trace.h>
47 #include <babeltrace/ctf-ir/stream-class.h>
48 #include <babeltrace/ctf-ir/event.h>
49 #include <babeltrace/ctf-ir/event-class.h>
50 #include <babeltrace/ctf-ir/field-types.h>
51 #include <babeltrace/ctf-ir/field-types-internal.h>
52 #include <babeltrace/ctf-ir/clock-class.h>
59 /* Bit value (left shift) */
60 #define _BV(_val) (1 << (_val))
62 /* Bit is set in a set of bits */
63 #define _IS_SET(_set, _mask) (*(_set) & (_mask))
65 /* Set bit in a set of bits */
66 #define _SET(_set, _mask) (*(_set) |= (_mask))
68 /* Bits for verifying existing attributes in various declarations */
70 _CLOCK_NAME_SET
= _BV(0),
71 _CLOCK_UUID_SET
= _BV(1),
72 _CLOCK_FREQ_SET
= _BV(2),
73 _CLOCK_PRECISION_SET
= _BV(3),
74 _CLOCK_OFFSET_S_SET
= _BV(4),
75 _CLOCK_OFFSET_SET
= _BV(5),
76 _CLOCK_ABSOLUTE_SET
= _BV(6),
77 _CLOCK_DESCRIPTION_SET
= _BV(7),
81 _INTEGER_ALIGN_SET
= _BV(0),
82 _INTEGER_SIZE_SET
= _BV(1),
83 _INTEGER_BASE_SET
= _BV(2),
84 _INTEGER_ENCODING_SET
= _BV(3),
85 _INTEGER_BYTE_ORDER_SET
= _BV(4),
86 _INTEGER_SIGNED_SET
= _BV(5),
87 _INTEGER_MAP_SET
= _BV(6),
91 _FLOAT_ALIGN_SET
= _BV(0),
92 _FLOAT_MANT_DIG_SET
= _BV(1),
93 _FLOAT_EXP_DIG_SET
= _BV(2),
94 _FLOAT_BYTE_ORDER_SET
= _BV(3),
98 _STRING_ENCODING_SET
= _BV(0),
102 _TRACE_MINOR_SET
= _BV(0),
103 _TRACE_MAJOR_SET
= _BV(1),
104 _TRACE_BYTE_ORDER_SET
= _BV(2),
105 _TRACE_UUID_SET
= _BV(3),
106 _TRACE_PACKET_HEADER_SET
= _BV(4),
110 _STREAM_ID_SET
= _BV(0),
111 _STREAM_PACKET_CONTEXT_SET
= _BV(1),
112 _STREAM_EVENT_HEADER_SET
= _BV(2),
113 _STREAM_EVENT_CONTEXT_SET
= _BV(3),
117 _EVENT_NAME_SET
= _BV(0),
118 _EVENT_ID_SET
= _BV(1),
119 _EVENT_MODEL_EMF_URI_SET
= _BV(2),
120 _EVENT_STREAM_ID_SET
= _BV(3),
121 _EVENT_LOGLEVEL_SET
= _BV(4),
122 _EVENT_CONTEXT_SET
= _BV(5),
123 _EVENT_FIELDS_SET
= _BV(6),
131 LOGLEVEL_WARNING
= 4,
134 LOGLEVEL_DEBUG_SYSTEM
= 7,
135 LOGLEVEL_DEBUG_PROGRAM
= 8,
136 LOGLEVEL_DEBUG_PROCESS
= 9,
137 LOGLEVEL_DEBUG_MODULE
= 10,
138 LOGLEVEL_DEBUG_UNIT
= 11,
139 LOGLEVEL_DEBUG_FUNCTION
= 12,
140 LOGLEVEL_DEBUG_LINE
= 13,
145 /* Prefixes of type aliases */
146 #define _PREFIX_ALIAS 'a'
147 #define _PREFIX_ENUM 'e'
148 #define _PREFIX_STRUCT 's'
149 #define _PREFIX_VARIANT 'v'
151 /* First entry in a BT list */
152 #define _BT_LIST_FIRST_ENTRY(_ptr, _type, _member) \
153 bt_list_entry((_ptr)->next, _type, _member)
155 #define _BT_CTF_FIELD_TYPE_INIT(_name) struct bt_ctf_field_type *_name = NULL;
157 #define _BT_LOGE_DUP_ATTR(_node, _attr, _entity) \
158 _BT_LOGE_LINENO((_node)->lineno, \
159 "Duplicate attribute in %s: attr-name=\"%s\"", \
162 #define _BT_LOGE_NODE(_node, _msg, args...) \
163 _BT_LOGE_LINENO((_node)->lineno, _msg, ## args)
165 #define _BT_LOGW_NODE(_node, _msg, args...) \
166 _BT_LOGW_LINENO((_node)->lineno, _msg, ## args)
168 #define _BT_LOGV_NODE(_node, _msg, args...) \
169 _BT_LOGV_LINENO((_node)->lineno, _msg, ## args)
172 * Declaration scope of a visitor context. This represents a TSDL
173 * lexical scope, so that aliases and named structures, variants,
174 * and enumerations may be registered and looked up hierarchically.
176 struct ctx_decl_scope
{
178 * Alias name to field type.
180 * GQuark -> struct bt_ctf_field_type *
182 GHashTable
*decl_map
;
184 /* Parent scope; NULL if this is the root declaration scope */
185 struct ctx_decl_scope
*parent_scope
;
189 * Visitor context (private).
192 /* Trace being filled (owned by this) */
193 struct bt_ctf_trace
*trace
;
195 /* Current declaration scope (top of the stack) */
196 struct ctx_decl_scope
*current_scope
;
198 /* 1 if trace declaration is visited */
199 int is_trace_visited
;
201 /* 1 if this is an LTTng trace */
204 /* Eventual name suffix of the trace to set */
205 char *trace_name_suffix
;
207 /* Trace attributes */
208 enum bt_ctf_byte_order trace_bo
;
209 uint64_t trace_major
;
210 uint64_t trace_minor
;
211 unsigned char trace_uuid
[BABELTRACE_UUID_LEN
];
214 * Stream IDs to stream classes.
216 * int64_t -> struct bt_ctf_stream_class *
218 GHashTable
*stream_classes
;
220 /* Config passed by the user */
221 struct ctf_metadata_decoder_config decoder_config
;
227 struct ctf_visitor_generate_ir
{ };
230 const char *loglevel_str
[] = {
231 [ LOGLEVEL_EMERG
] = "TRACE_EMERG",
232 [ LOGLEVEL_ALERT
] = "TRACE_ALERT",
233 [ LOGLEVEL_CRIT
] = "TRACE_CRIT",
234 [ LOGLEVEL_ERR
] = "TRACE_ERR",
235 [ LOGLEVEL_WARNING
] = "TRACE_WARNING",
236 [ LOGLEVEL_NOTICE
] = "TRACE_NOTICE",
237 [ LOGLEVEL_INFO
] = "TRACE_INFO",
238 [ LOGLEVEL_DEBUG_SYSTEM
] = "TRACE_DEBUG_SYSTEM",
239 [ LOGLEVEL_DEBUG_PROGRAM
] = "TRACE_DEBUG_PROGRAM",
240 [ LOGLEVEL_DEBUG_PROCESS
] = "TRACE_DEBUG_PROCESS",
241 [ LOGLEVEL_DEBUG_MODULE
] = "TRACE_DEBUG_MODULE",
242 [ LOGLEVEL_DEBUG_UNIT
] = "TRACE_DEBUG_UNIT",
243 [ LOGLEVEL_DEBUG_FUNCTION
] = "TRACE_DEBUG_FUNCTION",
244 [ LOGLEVEL_DEBUG_LINE
] = "TRACE_DEBUG_LINE",
245 [ LOGLEVEL_DEBUG
] = "TRACE_DEBUG",
249 const char *print_loglevel(int64_t value
)
254 if (value
>= _NR_LOGLEVELS
) {
255 return "<<UNKNOWN>>";
257 return loglevel_str
[value
];
261 * Creates a new declaration scope.
263 * @param par_scope Parent scope (NULL if creating a root scope)
264 * @returns New declaration scope, or NULL on error
267 struct ctx_decl_scope
*ctx_decl_scope_create(struct ctx_decl_scope
*par_scope
)
269 struct ctx_decl_scope
*scope
;
271 scope
= g_new(struct ctx_decl_scope
, 1);
273 BT_LOGE_STR("Failed to allocate one declaration scope.");
277 scope
->decl_map
= g_hash_table_new_full(g_direct_hash
, g_direct_equal
,
278 NULL
, (GDestroyNotify
) bt_ctf_field_type_put
);
279 scope
->parent_scope
= par_scope
;
286 * Destroys a declaration scope.
288 * This function does not destroy the parent scope.
290 * @param scope Scope to destroy
293 void ctx_decl_scope_destroy(struct ctx_decl_scope
*scope
)
299 g_hash_table_destroy(scope
->decl_map
);
307 * Returns the GQuark of a prefixed alias.
309 * @param prefix Prefix character
311 * @returns Associated GQuark, or 0 on error
314 GQuark
get_prefixed_named_quark(char prefix
, const char *name
)
320 /* Prefix character + original string + '\0' */
321 char *prname
= g_new(char, strlen(name
) + 2);
323 BT_LOGE_STR("Failed to allocate a string.");
327 sprintf(prname
, "%c%s", prefix
, name
);
328 qname
= g_quark_from_string(prname
);
336 * Looks up a prefixed type alias within a declaration scope.
338 * @param scope Declaration scope
339 * @param prefix Prefix character
340 * @param name Alias name
341 * @param level Number of levels to dig (-1 means infinite)
342 * @returns Declaration, or NULL if not found
345 struct bt_ctf_field_type
*ctx_decl_scope_lookup_prefix_alias(
346 struct ctx_decl_scope
*scope
, char prefix
,
347 const char *name
, int levels
)
351 _BT_CTF_FIELD_TYPE_INIT(decl
);
352 struct ctx_decl_scope
*cur_scope
= scope
;
356 qname
= get_prefixed_named_quark(prefix
, name
);
365 while (cur_scope
&& cur_levels
< levels
) {
366 decl
= g_hash_table_lookup(cur_scope
->decl_map
,
367 (gconstpointer
) GUINT_TO_POINTER(qname
));
369 /* Caller's reference */
374 cur_scope
= cur_scope
->parent_scope
;
385 * Looks up a type alias within a declaration scope.
387 * @param scope Declaration scope
388 * @param name Alias name
389 * @param level Number of levels to dig (-1 means infinite)
390 * @returns Declaration, or NULL if not found
393 struct bt_ctf_field_type
*ctx_decl_scope_lookup_alias(
394 struct ctx_decl_scope
*scope
, const char *name
, int levels
)
396 return ctx_decl_scope_lookup_prefix_alias(scope
, _PREFIX_ALIAS
,
401 * Looks up an enumeration within a declaration scope.
403 * @param scope Declaration scope
404 * @param name Enumeration name
405 * @param level Number of levels to dig (-1 means infinite)
406 * @returns Declaration, or NULL if not found
409 struct bt_ctf_field_type
*ctx_decl_scope_lookup_enum(
410 struct ctx_decl_scope
*scope
, const char *name
, int levels
)
412 return ctx_decl_scope_lookup_prefix_alias(scope
, _PREFIX_ENUM
,
417 * Looks up a structure within a declaration scope.
419 * @param scope Declaration scope
420 * @param name Structure name
421 * @param level Number of levels to dig (-1 means infinite)
422 * @returns Declaration, or NULL if not found
425 struct bt_ctf_field_type
*ctx_decl_scope_lookup_struct(
426 struct ctx_decl_scope
*scope
, const char *name
, int levels
)
428 return ctx_decl_scope_lookup_prefix_alias(scope
, _PREFIX_STRUCT
,
433 * Looks up a variant within a declaration scope.
435 * @param scope Declaration scope
436 * @param name Variant name
437 * @param level Number of levels to dig (-1 means infinite)
438 * @returns Declaration, or NULL if not found
441 struct bt_ctf_field_type
*ctx_decl_scope_lookup_variant(
442 struct ctx_decl_scope
*scope
, const char *name
, int levels
)
444 return ctx_decl_scope_lookup_prefix_alias(scope
, _PREFIX_VARIANT
,
449 * Registers a prefixed type alias within a declaration scope.
451 * @param scope Declaration scope
452 * @param prefix Prefix character
453 * @param name Alias name (non-NULL)
454 * @param decl Declaration to register
455 * @returns 0 if registration went okay, negative value otherwise
458 int ctx_decl_scope_register_prefix_alias(struct ctx_decl_scope
*scope
,
459 char prefix
, const char *name
, struct bt_ctf_field_type
*decl
)
463 _BT_CTF_FIELD_TYPE_INIT(edecl
);
468 qname
= get_prefixed_named_quark(prefix
, name
);
474 /* Make sure alias does not exist in local scope */
475 edecl
= ctx_decl_scope_lookup_prefix_alias(scope
, prefix
, name
, 1);
482 g_hash_table_insert(scope
->decl_map
,
483 GUINT_TO_POINTER(qname
), decl
);
485 /* Hash table's reference */
495 * Registers a type alias within a declaration scope.
497 * @param scope Declaration scope
498 * @param name Alias name (non-NULL)
499 * @param decl Declaration to register
500 * @returns 0 if registration went okay, negative value otherwise
503 int ctx_decl_scope_register_alias(struct ctx_decl_scope
*scope
,
504 const char *name
, struct bt_ctf_field_type
*decl
)
506 return ctx_decl_scope_register_prefix_alias(scope
, _PREFIX_ALIAS
,
511 * Registers an enumeration declaration within a declaration scope.
513 * @param scope Declaration scope
514 * @param name Enumeration name (non-NULL)
515 * @param decl Enumeration declaration to register
516 * @returns 0 if registration went okay, negative value otherwise
519 int ctx_decl_scope_register_enum(struct ctx_decl_scope
*scope
,
520 const char *name
, struct bt_ctf_field_type
*decl
)
522 return ctx_decl_scope_register_prefix_alias(scope
, _PREFIX_ENUM
,
527 * Registers a structure declaration within a declaration scope.
529 * @param scope Declaration scope
530 * @param name Structure name (non-NULL)
531 * @param decl Structure declaration to register
532 * @returns 0 if registration went okay, negative value otherwise
535 int ctx_decl_scope_register_struct(struct ctx_decl_scope
*scope
,
536 const char *name
, struct bt_ctf_field_type
*decl
)
538 return ctx_decl_scope_register_prefix_alias(scope
, _PREFIX_STRUCT
,
543 * Registers a variant declaration within a declaration scope.
545 * @param scope Declaration scope
546 * @param name Variant name (non-NULL)
547 * @param decl Variant declaration to register
548 * @returns 0 if registration went okay, negative value otherwise
551 int ctx_decl_scope_register_variant(struct ctx_decl_scope
*scope
,
552 const char *name
, struct bt_ctf_field_type
*decl
)
554 return ctx_decl_scope_register_prefix_alias(scope
, _PREFIX_VARIANT
,
559 * Destroys a visitor context.
561 * @param ctx Visitor context to destroy
564 void ctx_destroy(struct ctx
*ctx
)
566 struct ctx_decl_scope
*scope
;
568 * Destroy all scopes, from current one to the root scope.
575 scope
= ctx
->current_scope
;
578 struct ctx_decl_scope
*parent_scope
= scope
->parent_scope
;
580 ctx_decl_scope_destroy(scope
);
581 scope
= parent_scope
;
586 if (ctx
->stream_classes
) {
587 g_hash_table_destroy(ctx
->stream_classes
);
590 free(ctx
->trace_name_suffix
);
598 * Creates a new visitor context.
600 * @param trace Associated trace
601 * @returns New visitor context, or NULL on error
604 struct ctx
*ctx_create(struct bt_ctf_trace
*trace
,
605 const struct ctf_metadata_decoder_config
*decoder_config
,
606 const char *trace_name_suffix
)
608 struct ctx
*ctx
= NULL
;
609 struct ctx_decl_scope
*scope
= NULL
;
611 assert(decoder_config
);
613 ctx
= g_new0(struct ctx
, 1);
615 BT_LOGE_STR("Failed to allocate one visitor context.");
619 /* Root declaration scope */
620 scope
= ctx_decl_scope_create(NULL
);
622 BT_LOGE_STR("Cannot create declaration scope.");
626 ctx
->stream_classes
= g_hash_table_new_full(g_direct_hash
,
627 g_direct_equal
, NULL
, (GDestroyNotify
) bt_put
);
628 if (!ctx
->stream_classes
) {
629 BT_LOGE_STR("Failed to allocate a GHashTable.");
633 if (trace_name_suffix
) {
634 ctx
->trace_name_suffix
= strdup(trace_name_suffix
);
635 if (!ctx
->trace_name_suffix
) {
636 BT_LOGE_STR("Failed to copy string.");
642 ctx
->current_scope
= scope
;
644 ctx
->trace_bo
= BT_CTF_BYTE_ORDER_NATIVE
;
645 ctx
->decoder_config
= *decoder_config
;
650 ctx_decl_scope_destroy(scope
);
655 * Pushes a new declaration scope on top of a visitor context's
656 * declaration scope stack.
658 * @param ctx Visitor context
659 * @returns 0 on success, or a negative value on error
662 int ctx_push_scope(struct ctx
*ctx
)
665 struct ctx_decl_scope
*new_scope
;
668 new_scope
= ctx_decl_scope_create(ctx
->current_scope
);
670 BT_LOGE_STR("Cannot create declaration scope.");
675 ctx
->current_scope
= new_scope
;
682 void ctx_pop_scope(struct ctx
*ctx
)
684 struct ctx_decl_scope
*parent_scope
= NULL
;
688 if (!ctx
->current_scope
) {
692 parent_scope
= ctx
->current_scope
->parent_scope
;
693 ctx_decl_scope_destroy(ctx
->current_scope
);
694 ctx
->current_scope
= parent_scope
;
701 int visit_type_specifier_list(struct ctx
*ctx
, struct ctf_node
*ts_list
,
702 struct bt_ctf_field_type
**decl
);
705 int is_unary_string(struct bt_list_head
*head
)
708 struct ctf_node
*node
;
710 bt_list_for_each_entry(node
, head
, siblings
) {
711 if (node
->type
!= NODE_UNARY_EXPRESSION
) {
715 if (node
->u
.unary_expression
.type
!= UNARY_STRING
) {
724 char *concatenate_unary_strings(struct bt_list_head
*head
)
728 struct ctf_node
*node
;
730 str
= g_string_new(NULL
);
733 bt_list_for_each_entry(node
, head
, siblings
) {
737 node
->type
!= NODE_UNARY_EXPRESSION
||
738 node
->u
.unary_expression
.type
!= UNARY_STRING
||
741 node
->u
.unary_expression
.link
!=
749 switch (node
->u
.unary_expression
.link
) {
751 g_string_append(str
, ".");
753 case UNARY_ARROWLINK
:
754 g_string_append(str
, "->");
756 case UNARY_DOTDOTDOT
:
757 g_string_append(str
, "...");
763 src_string
= node
->u
.unary_expression
.u
.string
;
764 g_string_append(str
, src_string
);
768 /* Destroys the container, returns the underlying string */
769 return g_string_free(str
, FALSE
);
772 /* This always returns NULL */
773 return g_string_free(str
, TRUE
);
777 const char *get_map_clock_name_value(struct bt_list_head
*head
)
780 struct ctf_node
*node
;
781 const char *name
= NULL
;
783 bt_list_for_each_entry(node
, head
, siblings
) {
785 int uexpr_type
= node
->u
.unary_expression
.type
;
786 int uexpr_link
= node
->u
.unary_expression
.link
;
787 int cond
= node
->type
!= NODE_UNARY_EXPRESSION
||
788 uexpr_type
!= UNARY_STRING
||
789 !((uexpr_link
!= UNARY_LINK_UNKNOWN
) ^ (i
== 0));
794 /* Needs to be chained with . */
795 switch (node
->u
.unary_expression
.link
) {
798 case UNARY_ARROWLINK
:
799 case UNARY_DOTDOTDOT
:
805 src_string
= node
->u
.unary_expression
.u
.string
;
809 if (strcmp("clock", src_string
)) {
817 if (strcmp("value", src_string
)) {
822 /* Extra identifier, unknown */
836 int is_unary_unsigned(struct bt_list_head
*head
)
839 struct ctf_node
*node
;
841 bt_list_for_each_entry(node
, head
, siblings
) {
842 if (node
->type
!= NODE_UNARY_EXPRESSION
) {
846 if (node
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
855 int get_unary_unsigned(struct bt_list_head
*head
, uint64_t *value
)
859 struct ctf_node
*node
;
861 if (bt_list_empty(head
)) {
866 bt_list_for_each_entry(node
, head
, siblings
) {
867 int uexpr_type
= node
->u
.unary_expression
.type
;
868 int uexpr_link
= node
->u
.unary_expression
.link
;
869 int cond
= node
->type
!= NODE_UNARY_EXPRESSION
||
870 uexpr_type
!= UNARY_UNSIGNED_CONSTANT
||
871 uexpr_link
!= UNARY_LINK_UNKNOWN
|| i
!= 0;
873 _BT_LOGE_NODE(node
, "Invalid constant unsigned integer.");
878 *value
= node
->u
.unary_expression
.u
.unsigned_constant
;
887 int is_unary_signed(struct bt_list_head
*head
)
890 struct ctf_node
*node
;
892 bt_list_for_each_entry(node
, head
, siblings
) {
893 if (node
->type
!= NODE_UNARY_EXPRESSION
) {
897 if (node
->u
.unary_expression
.type
!= UNARY_SIGNED_CONSTANT
) {
906 int get_unary_signed(struct bt_list_head
*head
, int64_t *value
)
910 struct ctf_node
*node
;
912 bt_list_for_each_entry(node
, head
, siblings
) {
913 int uexpr_type
= node
->u
.unary_expression
.type
;
914 int uexpr_link
= node
->u
.unary_expression
.link
;
915 int cond
= node
->type
!= NODE_UNARY_EXPRESSION
||
916 (uexpr_type
!= UNARY_UNSIGNED_CONSTANT
) ||
917 (uexpr_type
!= UNARY_UNSIGNED_CONSTANT
&&
918 uexpr_type
!= UNARY_SIGNED_CONSTANT
) ||
919 uexpr_link
!= UNARY_LINK_UNKNOWN
|| i
!= 0;
925 switch (node
->u
.unary_expression
.type
) {
926 case UNARY_UNSIGNED_CONSTANT
:
928 node
->u
.unary_expression
.u
.unsigned_constant
;
930 case UNARY_SIGNED_CONSTANT
:
931 *value
= node
->u
.unary_expression
.u
.signed_constant
;
946 int get_unary_uuid(struct bt_list_head
*head
, unsigned char *uuid
)
950 struct ctf_node
*node
;
952 bt_list_for_each_entry(node
, head
, siblings
) {
953 int uexpr_type
= node
->u
.unary_expression
.type
;
954 int uexpr_link
= node
->u
.unary_expression
.link
;
955 const char *src_string
;
957 if (node
->type
!= NODE_UNARY_EXPRESSION
||
958 uexpr_type
!= UNARY_STRING
||
959 uexpr_link
!= UNARY_LINK_UNKNOWN
||
965 src_string
= node
->u
.unary_expression
.u
.string
;
966 ret
= bt_uuid_parse(src_string
, uuid
);
969 "Cannot parse UUID: uuid=\"%s\"", src_string
);
979 int get_boolean(struct ctf_node
*unary_expr
)
983 if (unary_expr
->type
!= NODE_UNARY_EXPRESSION
) {
984 _BT_LOGE_NODE(unary_expr
,
985 "Expecting unary expression: node-type=%d",
991 switch (unary_expr
->u
.unary_expression
.type
) {
992 case UNARY_UNSIGNED_CONSTANT
:
993 ret
= (unary_expr
->u
.unary_expression
.u
.unsigned_constant
!= 0);
995 case UNARY_SIGNED_CONSTANT
:
996 ret
= (unary_expr
->u
.unary_expression
.u
.signed_constant
!= 0);
1000 const char *str
= unary_expr
->u
.unary_expression
.u
.string
;
1002 if (!strcmp(str
, "true") || !strcmp(str
, "TRUE")) {
1004 } else if (!strcmp(str
, "false") || !strcmp(str
, "FALSE")) {
1007 _BT_LOGE_NODE(unary_expr
,
1008 "Unexpected boolean value: value=\"%s\"", str
);
1015 _BT_LOGE_NODE(unary_expr
,
1016 "Unexpected unary expression type: node-type=%d",
1017 unary_expr
->u
.unary_expression
.type
);
1027 enum bt_ctf_byte_order
byte_order_from_unary_expr(struct ctf_node
*unary_expr
)
1030 enum bt_ctf_byte_order bo
= BT_CTF_BYTE_ORDER_UNKNOWN
;
1032 if (unary_expr
->u
.unary_expression
.type
!= UNARY_STRING
) {
1033 _BT_LOGE_NODE(unary_expr
,
1034 "\"byte_order\" attribute: expecting `be`, `le`, `network`, or `native`.");
1038 str
= unary_expr
->u
.unary_expression
.u
.string
;
1040 if (!strcmp(str
, "be") || !strcmp(str
, "network")) {
1041 bo
= BT_CTF_BYTE_ORDER_BIG_ENDIAN
;
1042 } else if (!strcmp(str
, "le")) {
1043 bo
= BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
;
1044 } else if (!strcmp(str
, "native")) {
1045 bo
= BT_CTF_BYTE_ORDER_NATIVE
;
1047 _BT_LOGE_NODE(unary_expr
,
1048 "Unexpected \"byte_order\" attribute value: "
1049 "expecting `be`, `le`, `network`, or `native`: value=\"%s\"",
1059 enum bt_ctf_byte_order
get_real_byte_order(struct ctx
*ctx
,
1060 struct ctf_node
*uexpr
)
1062 enum bt_ctf_byte_order bo
= byte_order_from_unary_expr(uexpr
);
1064 if (bo
== BT_CTF_BYTE_ORDER_NATIVE
) {
1065 bo
= bt_ctf_trace_get_native_byte_order(ctx
->trace
);
1072 int is_align_valid(uint64_t align
)
1074 return (align
!= 0) && !(align
& (align
- 1));
1078 int get_type_specifier_name(struct ctx
*ctx
, struct ctf_node
*type_specifier
,
1083 if (type_specifier
->type
!= NODE_TYPE_SPECIFIER
) {
1084 _BT_LOGE_NODE(type_specifier
,
1085 "Unexpected node type: node-type=%d",
1086 type_specifier
->type
);
1091 switch (type_specifier
->u
.type_specifier
.type
) {
1093 g_string_append(str
, "void");
1096 g_string_append(str
, "char");
1098 case TYPESPEC_SHORT
:
1099 g_string_append(str
, "short");
1102 g_string_append(str
, "int");
1105 g_string_append(str
, "long");
1107 case TYPESPEC_FLOAT
:
1108 g_string_append(str
, "float");
1110 case TYPESPEC_DOUBLE
:
1111 g_string_append(str
, "double");
1113 case TYPESPEC_SIGNED
:
1114 g_string_append(str
, "signed");
1116 case TYPESPEC_UNSIGNED
:
1117 g_string_append(str
, "unsigned");
1120 g_string_append(str
, "bool");
1122 case TYPESPEC_COMPLEX
:
1123 g_string_append(str
, "_Complex");
1125 case TYPESPEC_IMAGINARY
:
1126 g_string_append(str
, "_Imaginary");
1128 case TYPESPEC_CONST
:
1129 g_string_append(str
, "const");
1131 case TYPESPEC_ID_TYPE
:
1132 if (type_specifier
->u
.type_specifier
.id_type
) {
1133 g_string_append(str
,
1134 type_specifier
->u
.type_specifier
.id_type
);
1137 case TYPESPEC_STRUCT
:
1139 struct ctf_node
*node
= type_specifier
->u
.type_specifier
.node
;
1141 if (!node
->u
._struct
.name
) {
1142 _BT_LOGE_NODE(node
, "Unexpected empty structure field type name.");
1147 g_string_append(str
, "struct ");
1148 g_string_append(str
, node
->u
._struct
.name
);
1151 case TYPESPEC_VARIANT
:
1153 struct ctf_node
*node
= type_specifier
->u
.type_specifier
.node
;
1155 if (!node
->u
.variant
.name
) {
1156 _BT_LOGE_NODE(node
, "Unexpected empty variant field type name.");
1161 g_string_append(str
, "variant ");
1162 g_string_append(str
, node
->u
.variant
.name
);
1167 struct ctf_node
*node
= type_specifier
->u
.type_specifier
.node
;
1169 if (!node
->u
._enum
.enum_id
) {
1171 "Unexpected empty enumeration field type (`enum`) name.");
1176 g_string_append(str
, "enum ");
1177 g_string_append(str
, node
->u
._enum
.enum_id
);
1180 case TYPESPEC_FLOATING_POINT
:
1181 case TYPESPEC_INTEGER
:
1182 case TYPESPEC_STRING
:
1184 _BT_LOGE_NODE(type_specifier
->u
.type_specifier
.node
,
1185 "Unexpected type specifier type: %d",
1186 type_specifier
->u
.type_specifier
.type
);
1196 int get_type_specifier_list_name(struct ctx
*ctx
,
1197 struct ctf_node
*type_specifier_list
, GString
*str
)
1200 struct ctf_node
*iter
;
1201 int alias_item_nr
= 0;
1202 struct bt_list_head
*head
=
1203 &type_specifier_list
->u
.type_specifier_list
.head
;
1205 bt_list_for_each_entry(iter
, head
, siblings
) {
1206 if (alias_item_nr
!= 0) {
1207 g_string_append(str
, " ");
1211 ret
= get_type_specifier_name(ctx
, iter
, str
);
1222 GQuark
create_typealias_identifier(struct ctx
*ctx
,
1223 struct ctf_node
*type_specifier_list
,
1224 struct ctf_node
*node_type_declarator
)
1230 struct ctf_node
*iter
;
1231 struct bt_list_head
*pointers
=
1232 &node_type_declarator
->u
.type_declarator
.pointers
;
1234 str
= g_string_new("");
1235 ret
= get_type_specifier_list_name(ctx
, type_specifier_list
, str
);
1237 g_string_free(str
, TRUE
);
1241 bt_list_for_each_entry(iter
, pointers
, siblings
) {
1242 g_string_append(str
, " *");
1244 if (iter
->u
.pointer
.const_qualifier
) {
1245 g_string_append(str
, " const");
1249 str_c
= g_string_free(str
, FALSE
);
1250 qalias
= g_quark_from_string(str_c
);
1258 int visit_type_declarator(struct ctx
*ctx
, struct ctf_node
*type_specifier_list
,
1259 GQuark
*field_name
, struct ctf_node
*node_type_declarator
,
1260 struct bt_ctf_field_type
**field_decl
,
1261 struct bt_ctf_field_type
*nested_decl
)
1264 * During this whole function, nested_decl is always OURS,
1265 * whereas field_decl is an output which we create, but
1266 * belongs to the caller (it is moved).
1272 /* Validate type declarator node */
1273 if (node_type_declarator
) {
1274 if (node_type_declarator
->u
.type_declarator
.type
==
1276 _BT_LOGE_NODE(node_type_declarator
,
1277 "Unexpected type declarator type: type=%d",
1278 node_type_declarator
->u
.type_declarator
.type
);
1283 /* TODO: GCC bitfields not supported yet */
1284 if (node_type_declarator
->u
.type_declarator
.bitfield_len
!=
1286 _BT_LOGE_NODE(node_type_declarator
,
1287 "GCC bitfields are not supported as of this version.");
1293 /* Find the right nested declaration if not provided */
1295 struct bt_list_head
*pointers
=
1296 &node_type_declarator
->u
.type_declarator
.pointers
;
1298 if (node_type_declarator
&& !bt_list_empty(pointers
)) {
1300 _BT_CTF_FIELD_TYPE_INIT(nested_decl_copy
);
1303 * If we have a pointer declarator, it HAS to
1304 * be present in the typealiases (else fail).
1306 qalias
= create_typealias_identifier(ctx
,
1307 type_specifier_list
, node_type_declarator
);
1309 ctx_decl_scope_lookup_alias(ctx
->current_scope
,
1310 g_quark_to_string(qalias
), -1);
1312 _BT_LOGE_NODE(node_type_declarator
,
1313 "Cannot find type alias: name=\"%s\"",
1314 g_quark_to_string(qalias
));
1319 /* Make a copy of it */
1320 nested_decl_copy
= bt_ctf_field_type_copy(nested_decl
);
1321 BT_PUT(nested_decl
);
1322 if (!nested_decl_copy
) {
1323 _BT_LOGE_NODE(node_type_declarator
,
1324 "Cannot copy nested field type.");
1329 BT_MOVE(nested_decl
, nested_decl_copy
);
1331 /* Force integer's base to 16 since it's a pointer */
1332 if (bt_ctf_field_type_is_integer(nested_decl
)) {
1333 ret
= bt_ctf_field_type_integer_set_base(
1335 BT_CTF_INTEGER_BASE_HEXADECIMAL
);
1339 ret
= visit_type_specifier_list(ctx
,
1340 type_specifier_list
, &nested_decl
);
1342 assert(!nested_decl
);
1348 assert(nested_decl
);
1350 if (!node_type_declarator
) {
1351 BT_MOVE(*field_decl
, nested_decl
);
1355 if (node_type_declarator
->u
.type_declarator
.type
== TYPEDEC_ID
) {
1356 if (node_type_declarator
->u
.type_declarator
.u
.id
) {
1358 node_type_declarator
->u
.type_declarator
.u
.id
;
1360 *field_name
= g_quark_from_string(id
);
1365 BT_MOVE(*field_decl
, nested_decl
);
1368 struct ctf_node
*first
;
1369 _BT_CTF_FIELD_TYPE_INIT(decl
);
1370 _BT_CTF_FIELD_TYPE_INIT(outer_field_decl
);
1371 struct bt_list_head
*length
=
1372 &node_type_declarator
->
1373 u
.type_declarator
.u
.nested
.length
;
1375 /* Create array/sequence, pass nested_decl as child */
1376 if (bt_list_empty(length
)) {
1377 _BT_LOGE_NODE(node_type_declarator
,
1378 "Expecting length field reference or value.");
1383 first
= _BT_LIST_FIRST_ENTRY(length
, struct ctf_node
, siblings
);
1384 if (first
->type
!= NODE_UNARY_EXPRESSION
) {
1385 _BT_LOGE_NODE(first
,
1386 "Unexpected node type: node-type=%d",
1392 switch (first
->u
.unary_expression
.type
) {
1393 case UNARY_UNSIGNED_CONSTANT
:
1396 _BT_CTF_FIELD_TYPE_INIT(array_decl
);
1398 len
= first
->u
.unary_expression
.u
.unsigned_constant
;
1399 array_decl
= bt_ctf_field_type_array_create(nested_decl
,
1401 BT_PUT(nested_decl
);
1403 _BT_LOGE_NODE(first
,
1404 "Cannot create array field type.");
1409 BT_MOVE(decl
, array_decl
);
1414 /* Lookup unsigned integer definition, create seq. */
1415 _BT_CTF_FIELD_TYPE_INIT(seq_decl
);
1416 char *length_name
= concatenate_unary_strings(length
);
1419 _BT_LOGE_NODE(node_type_declarator
,
1420 "Cannot concatenate unary strings.");
1425 seq_decl
= bt_ctf_field_type_sequence_create(
1426 nested_decl
, length_name
);
1427 g_free(length_name
);
1428 BT_PUT(nested_decl
);
1430 _BT_LOGE_NODE(node_type_declarator
,
1431 "Cannot create sequence field type.");
1436 BT_MOVE(decl
, seq_decl
);
1444 assert(!nested_decl
);
1446 assert(!*field_decl
);
1449 * At this point, we found the next nested declaration.
1450 * We currently own this (and lost the ownership of
1451 * nested_decl in the meantime). Pass this next
1452 * nested declaration as the content of the outer
1453 * container, MOVING its ownership.
1455 ret
= visit_type_declarator(ctx
, type_specifier_list
,
1457 node_type_declarator
->
1458 u
.type_declarator
.u
.nested
.type_declarator
,
1459 &outer_field_decl
, decl
);
1462 assert(!outer_field_decl
);
1467 assert(outer_field_decl
);
1468 BT_MOVE(*field_decl
, outer_field_decl
);
1472 BT_PUT(nested_decl
);
1473 assert(*field_decl
);
1478 BT_PUT(nested_decl
);
1479 BT_PUT(*field_decl
);
1485 int visit_struct_decl_field(struct ctx
*ctx
,
1486 struct bt_ctf_field_type
*struct_decl
,
1487 struct ctf_node
*type_specifier_list
,
1488 struct bt_list_head
*type_declarators
)
1491 struct ctf_node
*iter
;
1492 _BT_CTF_FIELD_TYPE_INIT(field_decl
);
1494 bt_list_for_each_entry(iter
, type_declarators
, siblings
) {
1497 const char *field_name
;
1498 _BT_CTF_FIELD_TYPE_INIT(efield_decl
);
1500 ret
= visit_type_declarator(ctx
, type_specifier_list
,
1501 &qfield_name
, iter
, &field_decl
, NULL
);
1503 assert(!field_decl
);
1504 _BT_LOGE_NODE(type_specifier_list
,
1505 "Cannot visit type declarator: ret=%d", ret
);
1510 field_name
= g_quark_to_string(qfield_name
);
1512 /* Check if field with same name already exists */
1514 bt_ctf_field_type_structure_get_field_type_by_name(
1515 struct_decl
, field_name
);
1517 BT_PUT(efield_decl
);
1518 _BT_LOGE_NODE(type_specifier_list
,
1519 "Duplicate field in structure field type: "
1520 "field-name=\"%s\"", field_name
);
1525 /* Add field to structure */
1526 ret
= bt_ctf_field_type_structure_add_field(struct_decl
,
1527 field_decl
, field_name
);
1530 _BT_LOGE_NODE(type_specifier_list
,
1531 "Cannot add field to structure field type: "
1532 "field-name=\"%s\", ret=%d",
1533 g_quark_to_string(qfield_name
), ret
);
1547 int visit_variant_decl_field(struct ctx
*ctx
,
1548 struct bt_ctf_field_type
*variant_decl
,
1549 struct ctf_node
*type_specifier_list
,
1550 struct bt_list_head
*type_declarators
)
1553 struct ctf_node
*iter
;
1554 _BT_CTF_FIELD_TYPE_INIT(field_decl
);
1556 bt_list_for_each_entry(iter
, type_declarators
, siblings
) {
1559 const char *field_name
;
1560 _BT_CTF_FIELD_TYPE_INIT(efield_decl
);
1562 ret
= visit_type_declarator(ctx
, type_specifier_list
,
1563 &qfield_name
, iter
, &field_decl
, NULL
);
1565 assert(!field_decl
);
1566 _BT_LOGE_NODE(type_specifier_list
,
1567 "Cannot visit type declarator: ret=%d", ret
);
1572 field_name
= g_quark_to_string(qfield_name
);
1574 /* Check if field with same name already exists */
1576 bt_ctf_field_type_variant_get_field_type_by_name(
1577 variant_decl
, field_name
);
1579 BT_PUT(efield_decl
);
1580 _BT_LOGE_NODE(type_specifier_list
,
1581 "Duplicate field in variant field type: "
1582 "field-name=\"%s\"", field_name
);
1587 /* Add field to structure */
1588 ret
= bt_ctf_field_type_variant_add_field(variant_decl
,
1589 field_decl
, field_name
);
1592 _BT_LOGE_NODE(type_specifier_list
,
1593 "Cannot add field to variant field type: "
1594 "field-name=\"%s\", ret=%d",
1595 g_quark_to_string(qfield_name
), ret
);
1609 int visit_typedef(struct ctx
*ctx
, struct ctf_node
*type_specifier_list
,
1610 struct bt_list_head
*type_declarators
)
1614 struct ctf_node
*iter
;
1615 _BT_CTF_FIELD_TYPE_INIT(type_decl
);
1617 bt_list_for_each_entry(iter
, type_declarators
, siblings
) {
1618 ret
= visit_type_declarator(ctx
, type_specifier_list
,
1619 &qidentifier
, iter
, &type_decl
, NULL
);
1622 "Cannot visit type declarator: ret=%d", ret
);
1627 /* Do not allow typedef and typealias of untagged variants */
1628 if (bt_ctf_field_type_is_variant(type_decl
)) {
1629 if (bt_ctf_field_type_variant_get_tag_name(type_decl
)) {
1631 "Type definition of untagged variant field type is not allowed.");
1637 ret
= ctx_decl_scope_register_alias(ctx
->current_scope
,
1638 g_quark_to_string(qidentifier
), type_decl
);
1641 "Cannot register type definition: name=\"%s\"",
1642 g_quark_to_string(qidentifier
));
1654 int visit_typealias(struct ctx
*ctx
, struct ctf_node
*target
,
1655 struct ctf_node
*alias
)
1659 struct ctf_node
*node
;
1660 GQuark qdummy_field_name
;
1661 _BT_CTF_FIELD_TYPE_INIT(type_decl
);
1663 /* Create target type declaration */
1664 if (bt_list_empty(&target
->u
.typealias_target
.type_declarators
)) {
1667 node
= _BT_LIST_FIRST_ENTRY(
1668 &target
->u
.typealias_target
.type_declarators
,
1669 struct ctf_node
, siblings
);
1672 ret
= visit_type_declarator(ctx
,
1673 target
->u
.typealias_target
.type_specifier_list
,
1674 &qdummy_field_name
, node
, &type_decl
, NULL
);
1678 "Cannot visit type declarator: ret=%d", ret
);
1682 /* Do not allow typedef and typealias of untagged variants */
1683 if (bt_ctf_field_type_is_variant(type_decl
)) {
1684 if (bt_ctf_field_type_variant_get_tag_name(type_decl
)) {
1686 "Type definition of untagged variant field type is not allowed.");
1693 * The semantic validator does not check whether the target is
1694 * abstract or not (if it has an identifier). Check it here.
1696 if (qdummy_field_name
!= 0) {
1698 "Expecting empty identifier: id=\"%s\"",
1699 g_quark_to_string(qdummy_field_name
));
1704 /* Create alias identifier */
1705 node
= _BT_LIST_FIRST_ENTRY(&alias
->u
.typealias_alias
.type_declarators
,
1706 struct ctf_node
, siblings
);
1707 qalias
= create_typealias_identifier(ctx
,
1708 alias
->u
.typealias_alias
.type_specifier_list
, node
);
1709 ret
= ctx_decl_scope_register_alias(ctx
->current_scope
,
1710 g_quark_to_string(qalias
), type_decl
);
1713 "Cannot register type alias: name=\"%s\"",
1714 g_quark_to_string(qalias
));
1725 int visit_struct_decl_entry(struct ctx
*ctx
, struct ctf_node
*entry_node
,
1726 struct bt_ctf_field_type
*struct_decl
)
1730 switch (entry_node
->type
) {
1732 ret
= visit_typedef(ctx
,
1733 entry_node
->u
._typedef
.type_specifier_list
,
1734 &entry_node
->u
._typedef
.type_declarators
);
1736 _BT_LOGE_NODE(entry_node
,
1737 "Cannot add type definition found in structure field type: ret=%d",
1742 case NODE_TYPEALIAS
:
1743 ret
= visit_typealias(ctx
, entry_node
->u
.typealias
.target
,
1744 entry_node
->u
.typealias
.alias
);
1746 _BT_LOGE_NODE(entry_node
,
1747 "Cannot add type alias found in structure field type: ret=%d",
1752 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
1754 ret
= visit_struct_decl_field(ctx
, struct_decl
,
1755 entry_node
->u
.struct_or_variant_declaration
.
1756 type_specifier_list
,
1757 &entry_node
->u
.struct_or_variant_declaration
.
1764 _BT_LOGE_NODE(entry_node
,
1765 "Unexpected node type: node-type=%d", entry_node
->type
);
1775 int visit_variant_decl_entry(struct ctx
*ctx
, struct ctf_node
*entry_node
,
1776 struct bt_ctf_field_type
*variant_decl
)
1780 switch (entry_node
->type
) {
1782 ret
= visit_typedef(ctx
,
1783 entry_node
->u
._typedef
.type_specifier_list
,
1784 &entry_node
->u
._typedef
.type_declarators
);
1786 _BT_LOGE_NODE(entry_node
,
1787 "Cannot add type definition found in variant field type: ret=%d",
1792 case NODE_TYPEALIAS
:
1793 ret
= visit_typealias(ctx
, entry_node
->u
.typealias
.target
,
1794 entry_node
->u
.typealias
.alias
);
1796 _BT_LOGE_NODE(entry_node
,
1797 "Cannot add type alias found in variant field type: ret=%d",
1802 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
1804 ret
= visit_variant_decl_field(ctx
, variant_decl
,
1805 entry_node
->u
.struct_or_variant_declaration
.
1806 type_specifier_list
,
1807 &entry_node
->u
.struct_or_variant_declaration
.
1814 _BT_LOGE_NODE(entry_node
,
1815 "Unexpected node type: node-type=%d",
1826 int visit_struct_decl(struct ctx
*ctx
, const char *name
,
1827 struct bt_list_head
*decl_list
, int has_body
,
1828 struct bt_list_head
*min_align
,
1829 struct bt_ctf_field_type
**struct_decl
)
1833 *struct_decl
= NULL
;
1835 /* For named struct (without body), lookup in declaration scope */
1837 _BT_CTF_FIELD_TYPE_INIT(struct_decl_copy
);
1840 BT_LOGE_STR("Bodyless structure field type: missing name.");
1845 *struct_decl
= ctx_decl_scope_lookup_struct(ctx
->current_scope
,
1847 if (!*struct_decl
) {
1848 BT_LOGE("Cannot find structure field type: name=\"struct %s\"",
1854 /* Make a copy of it */
1855 struct_decl_copy
= bt_ctf_field_type_copy(*struct_decl
);
1856 if (!struct_decl_copy
) {
1857 BT_LOGE_STR("Cannot create copy of structure field type.");
1862 BT_MOVE(*struct_decl
, struct_decl_copy
);
1864 struct ctf_node
*entry_node
;
1865 uint64_t min_align_value
= 0;
1868 _BT_CTF_FIELD_TYPE_INIT(estruct_decl
);
1870 estruct_decl
= ctx_decl_scope_lookup_struct(
1871 ctx
->current_scope
, name
, 1);
1873 BT_PUT(estruct_decl
);
1874 BT_LOGE("Structure field type already declared in local scope: "
1875 "name=\"struct %s\"", name
);
1881 if (!bt_list_empty(min_align
)) {
1882 ret
= get_unary_unsigned(min_align
, &min_align_value
);
1884 BT_LOGE("Unexpected unary expression for structure field type's `align` attribute: "
1890 *struct_decl
= bt_ctf_field_type_structure_create();
1891 if (!*struct_decl
) {
1892 BT_LOGE_STR("Cannot create empty structure field type.");
1897 if (min_align_value
!= 0) {
1898 ret
= bt_ctf_field_type_set_alignment(*struct_decl
,
1901 BT_LOGE("Cannot set structure field type's alignment: "
1907 ret
= ctx_push_scope(ctx
);
1909 BT_LOGE_STR("Cannot push scope.");
1913 bt_list_for_each_entry(entry_node
, decl_list
, siblings
) {
1914 ret
= visit_struct_decl_entry(ctx
, entry_node
,
1917 _BT_LOGE_NODE(entry_node
,
1918 "Cannot visit structure field type entry: "
1928 ret
= ctx_decl_scope_register_struct(ctx
->current_scope
,
1929 name
, *struct_decl
);
1931 BT_LOGE("Cannot register structure field type in declaration scope: "
1932 "name=\"struct %s\", ret=%d", name
, ret
);
1941 BT_PUT(*struct_decl
);
1947 int visit_variant_decl(struct ctx
*ctx
, const char *name
,
1948 const char *tag
, struct bt_list_head
*decl_list
,
1949 int has_body
, struct bt_ctf_field_type
**variant_decl
)
1952 _BT_CTF_FIELD_TYPE_INIT(untagged_variant_decl
);
1954 *variant_decl
= NULL
;
1956 /* For named variant (without body), lookup in declaration scope */
1958 _BT_CTF_FIELD_TYPE_INIT(variant_decl_copy
);
1961 BT_LOGE_STR("Bodyless variant field type: missing name.");
1966 untagged_variant_decl
=
1967 ctx_decl_scope_lookup_variant(ctx
->current_scope
,
1969 if (!untagged_variant_decl
) {
1970 BT_LOGE("Cannot find variant field type: name=\"variant %s\"",
1976 /* Make a copy of it */
1977 variant_decl_copy
= bt_ctf_field_type_copy(
1978 untagged_variant_decl
);
1979 if (!variant_decl_copy
) {
1980 BT_LOGE_STR("Cannot create copy of variant field type.");
1985 BT_MOVE(untagged_variant_decl
, variant_decl_copy
);
1987 struct ctf_node
*entry_node
;
1990 struct bt_ctf_field_type
*evariant_decl
=
1991 ctx_decl_scope_lookup_struct(ctx
->current_scope
,
1994 if (evariant_decl
) {
1995 BT_PUT(evariant_decl
);
1996 BT_LOGE("Variant field type already declared in local scope: "
1997 "name=\"variant %s\"", name
);
2003 untagged_variant_decl
= bt_ctf_field_type_variant_create(NULL
,
2005 if (!untagged_variant_decl
) {
2006 BT_LOGE_STR("Cannot create empty variant field type.");
2011 ret
= ctx_push_scope(ctx
);
2013 BT_LOGE_STR("Cannot push scope.");
2017 bt_list_for_each_entry(entry_node
, decl_list
, siblings
) {
2018 ret
= visit_variant_decl_entry(ctx
, entry_node
,
2019 untagged_variant_decl
);
2021 _BT_LOGE_NODE(entry_node
,
2022 "Cannot visit variant field type entry: "
2032 ret
= ctx_decl_scope_register_variant(
2033 ctx
->current_scope
, name
,
2034 untagged_variant_decl
);
2036 BT_LOGE("Cannot register variant field type in declaration scope: "
2037 "name=\"variant %s\", ret=%d", name
, ret
);
2044 * If tagged, create tagged variant and return; otherwise
2045 * return untagged variant.
2048 BT_MOVE(*variant_decl
, untagged_variant_decl
);
2051 * At this point, we have a fresh untagged variant; nobody
2052 * else owns it. Set its tag now.
2054 ret
= bt_ctf_field_type_variant_set_tag_name(
2055 untagged_variant_decl
, tag
);
2057 BT_LOGE("Cannot set variant field type's tag name: "
2058 "tag-name=\"%s\"", tag
);
2062 BT_MOVE(*variant_decl
, untagged_variant_decl
);
2065 assert(!untagged_variant_decl
);
2066 assert(*variant_decl
);
2071 BT_PUT(untagged_variant_decl
);
2072 BT_PUT(*variant_decl
);
2078 int visit_enum_decl_entry(struct ctx
*ctx
, struct ctf_node
*enumerator
,
2079 struct bt_ctf_field_type
*enum_decl
, int64_t *last
, int is_signed
)
2083 struct ctf_node
*iter
;
2084 int64_t start
= 0, end
= 0;
2085 const char *label
= enumerator
->u
.enumerator
.id
;
2086 struct bt_list_head
*values
= &enumerator
->u
.enumerator
.values
;
2088 bt_list_for_each_entry(iter
, values
, siblings
) {
2091 if (iter
->type
!= NODE_UNARY_EXPRESSION
) {
2093 "Wrong expression for enumeration field type label: "
2094 "node-type=%d, label=\"%s\"", iter
->type
,
2106 switch (iter
->u
.unary_expression
.type
) {
2107 case UNARY_SIGNED_CONSTANT
:
2108 *target
= iter
->u
.unary_expression
.u
.signed_constant
;
2110 case UNARY_UNSIGNED_CONSTANT
:
2112 iter
->u
.unary_expression
.u
.unsigned_constant
;
2116 "Invalid enumeration field type entry: "
2117 "expecting constant signed or unsigned integer: "
2118 "node-type=%d, label=\"%s\"",
2119 iter
->u
.unary_expression
.type
, label
);
2126 "Invalid enumeration field type entry: label=\"%s\"",
2146 ret
= bt_ctf_field_type_enumeration_add_mapping(enum_decl
, label
,
2149 ret
= bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_decl
,
2150 label
, (uint64_t) start
, (uint64_t) end
);
2153 _BT_LOGE_NODE(enumerator
,
2154 "Cannot add mapping to enumeration field type: "
2155 "label=\"%s\", ret=%d, "
2156 "start-value-unsigned=%" PRIu64
", "
2157 "end-value-unsigned=%" PRIu64
, label
, ret
,
2158 (uint64_t) start
, (uint64_t) end
);
2169 int visit_enum_decl(struct ctx
*ctx
, const char *name
,
2170 struct ctf_node
*container_type
,
2171 struct bt_list_head
*enumerator_list
,
2173 struct bt_ctf_field_type
**enum_decl
)
2177 _BT_CTF_FIELD_TYPE_INIT(integer_decl
);
2181 /* For named enum (without body), lookup in declaration scope */
2183 _BT_CTF_FIELD_TYPE_INIT(enum_decl_copy
);
2186 BT_LOGE_STR("Bodyless enumeration field type: missing name.");
2191 *enum_decl
= ctx_decl_scope_lookup_enum(ctx
->current_scope
,
2194 BT_LOGE("Cannot find enumeration field type: "
2195 "name=\"enum %s\"", name
);
2200 /* Make a copy of it */
2201 enum_decl_copy
= bt_ctf_field_type_copy(*enum_decl
);
2202 if (!enum_decl_copy
) {
2203 BT_LOGE_STR("Cannot create copy of enumeration field type.");
2209 BT_MOVE(*enum_decl
, enum_decl_copy
);
2211 struct ctf_node
*iter
;
2212 int64_t last_value
= 0;
2215 _BT_CTF_FIELD_TYPE_INIT(eenum_decl
);
2217 eenum_decl
= ctx_decl_scope_lookup_enum(
2218 ctx
->current_scope
, name
, 1);
2221 BT_LOGE("Enumeration field type already declared in local scope: "
2222 "name=\"enum %s\"", name
);
2228 if (!container_type
) {
2229 integer_decl
= ctx_decl_scope_lookup_alias(
2230 ctx
->current_scope
, "int", -1);
2231 if (!integer_decl
) {
2232 BT_LOGE_STR("Cannot find implicit `int` field type alias for enumeration field type.");
2237 ret
= visit_type_declarator(ctx
, container_type
,
2238 &qdummy_id
, NULL
, &integer_decl
, NULL
);
2240 assert(!integer_decl
);
2246 assert(integer_decl
);
2248 if (!bt_ctf_field_type_is_integer(integer_decl
)) {
2249 BT_LOGE("Container field type for enumeration field type is not an integer field type: "
2251 bt_ctf_field_type_id_string(
2252 bt_ctf_field_type_get_type_id(integer_decl
)));
2257 *enum_decl
= bt_ctf_field_type_enumeration_create(integer_decl
);
2259 BT_LOGE_STR("Cannot create enumeration field type.");
2264 bt_list_for_each_entry(iter
, enumerator_list
, siblings
) {
2265 ret
= visit_enum_decl_entry(ctx
, iter
, *enum_decl
,
2267 bt_ctf_field_type_integer_get_signed(integer_decl
));
2270 "Cannot visit enumeration field type entry: "
2277 ret
= ctx_decl_scope_register_enum(ctx
->current_scope
,
2280 BT_LOGE("Cannot register enumeration field type in declaration scope: "
2287 BT_PUT(integer_decl
);
2292 BT_PUT(integer_decl
);
2299 int visit_type_specifier(struct ctx
*ctx
,
2300 struct ctf_node
*type_specifier_list
,
2301 struct bt_ctf_field_type
**decl
)
2304 GString
*str
= NULL
;
2305 _BT_CTF_FIELD_TYPE_INIT(decl_copy
);
2308 str
= g_string_new("");
2309 ret
= get_type_specifier_list_name(ctx
, type_specifier_list
, str
);
2311 _BT_LOGE_NODE(type_specifier_list
,
2312 "Cannot get type specifier list's name: ret=%d", ret
);
2316 *decl
= ctx_decl_scope_lookup_alias(ctx
->current_scope
, str
->str
, -1);
2318 _BT_LOGE_NODE(type_specifier_list
,
2319 "Cannot find type alias: name=\"%s\"", str
->str
);
2324 /* Make a copy of the type declaration */
2325 decl_copy
= bt_ctf_field_type_copy(*decl
);
2327 _BT_LOGE_NODE(type_specifier_list
,
2328 "Cannot create field type copy.");
2333 BT_MOVE(*decl
, decl_copy
);
2334 (void) g_string_free(str
, TRUE
);
2341 (void) g_string_free(str
, TRUE
);
2350 int visit_integer_decl(struct ctx
*ctx
,
2351 struct bt_list_head
*expressions
,
2352 struct bt_ctf_field_type
**integer_decl
)
2357 struct ctf_node
*expression
;
2358 uint64_t alignment
= 0, size
= 0;
2359 struct bt_ctf_clock_class
*mapped_clock
= NULL
;
2360 enum bt_ctf_string_encoding encoding
= BT_CTF_STRING_ENCODING_NONE
;
2361 enum bt_ctf_integer_base base
= BT_CTF_INTEGER_BASE_DECIMAL
;
2362 enum bt_ctf_byte_order byte_order
=
2363 bt_ctf_trace_get_native_byte_order(ctx
->trace
);
2365 *integer_decl
= NULL
;
2367 bt_list_for_each_entry(expression
, expressions
, siblings
) {
2368 struct ctf_node
*left
, *right
;
2370 left
= _BT_LIST_FIRST_ENTRY(&expression
->u
.ctf_expression
.left
,
2371 struct ctf_node
, siblings
);
2372 right
= _BT_LIST_FIRST_ENTRY(
2373 &expression
->u
.ctf_expression
.right
, struct ctf_node
,
2376 if (left
->u
.unary_expression
.type
!= UNARY_STRING
) {
2378 "Unexpected unary expression type: type=%d",
2379 left
->u
.unary_expression
.type
);
2384 if (!strcmp(left
->u
.unary_expression
.u
.string
, "signed")) {
2385 if (_IS_SET(&set
, _INTEGER_SIGNED_SET
)) {
2386 _BT_LOGE_DUP_ATTR(left
, "signed",
2387 "integer field type");
2392 signedness
= get_boolean(right
);
2393 if (signedness
< 0) {
2394 _BT_LOGE_NODE(right
,
2395 "Invalid boolean value for integer field type's `signed` attribute: "
2401 _SET(&set
, _INTEGER_SIGNED_SET
);
2402 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2404 if (_IS_SET(&set
, _INTEGER_BYTE_ORDER_SET
)) {
2405 _BT_LOGE_DUP_ATTR(left
, "byte_order",
2406 "integer field type");
2411 byte_order
= get_real_byte_order(ctx
, right
);
2412 if (byte_order
== BT_CTF_BYTE_ORDER_UNKNOWN
) {
2413 _BT_LOGE_NODE(right
,
2414 "Invalid `byte_order` attribute in integer field type: "
2420 _SET(&set
, _INTEGER_BYTE_ORDER_SET
);
2421 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "size")) {
2422 if (_IS_SET(&set
, _INTEGER_SIZE_SET
)) {
2423 _BT_LOGE_DUP_ATTR(left
, "size",
2424 "integer field type");
2429 if (right
->u
.unary_expression
.type
!=
2430 UNARY_UNSIGNED_CONSTANT
) {
2431 _BT_LOGE_NODE(right
,
2432 "Invalid `size` attribute in integer field type: "
2433 "expecting unsigned constant integer: "
2435 right
->u
.unary_expression
.type
);
2440 size
= right
->u
.unary_expression
.u
.unsigned_constant
;
2442 _BT_LOGE_NODE(right
,
2443 "Invalid `size` attribute in integer field type: "
2444 "expecting positive constant integer: "
2445 "size=%" PRIu64
, size
);
2448 } else if (size
> 64) {
2449 _BT_LOGE_NODE(right
,
2450 "Invalid `size` attribute in integer field type: "
2451 "integer fields over 64 bits are not supported as of this version: "
2452 "size=%" PRIu64
, size
);
2457 _SET(&set
, _INTEGER_SIZE_SET
);
2458 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2460 if (_IS_SET(&set
, _INTEGER_ALIGN_SET
)) {
2461 _BT_LOGE_DUP_ATTR(left
, "align",
2462 "integer field type");
2467 if (right
->u
.unary_expression
.type
!=
2468 UNARY_UNSIGNED_CONSTANT
) {
2469 _BT_LOGE_NODE(right
,
2470 "Invalid `align` attribute in integer field type: "
2471 "expecting unsigned constant integer: "
2473 right
->u
.unary_expression
.type
);
2479 right
->u
.unary_expression
.u
.unsigned_constant
;
2480 if (!is_align_valid(alignment
)) {
2481 _BT_LOGE_NODE(right
,
2482 "Invalid `align` attribute in integer field type: "
2483 "expecting power of two: "
2484 "align=%" PRIu64
, alignment
);
2489 _SET(&set
, _INTEGER_ALIGN_SET
);
2490 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "base")) {
2491 if (_IS_SET(&set
, _INTEGER_BASE_SET
)) {
2492 _BT_LOGE_DUP_ATTR(left
, "base",
2493 "integer field type");
2498 switch (right
->u
.unary_expression
.type
) {
2499 case UNARY_UNSIGNED_CONSTANT
:
2501 uint64_t constant
= right
->u
.unary_expression
.
2502 u
.unsigned_constant
;
2506 base
= BT_CTF_INTEGER_BASE_BINARY
;
2509 base
= BT_CTF_INTEGER_BASE_OCTAL
;
2512 base
= BT_CTF_INTEGER_BASE_DECIMAL
;
2515 base
= BT_CTF_INTEGER_BASE_HEXADECIMAL
;
2518 _BT_LOGE_NODE(right
,
2519 "Invalid `base` attribute in integer field type: "
2521 right
->u
.unary_expression
.u
.unsigned_constant
);
2529 char *s_right
= concatenate_unary_strings(
2530 &expression
->u
.ctf_expression
.right
);
2532 _BT_LOGE_NODE(right
,
2533 "Unexpected unary expression for integer field type's `base` attribute.");
2538 if (!strcmp(s_right
, "decimal") ||
2539 !strcmp(s_right
, "dec") ||
2540 !strcmp(s_right
, "d") ||
2541 !strcmp(s_right
, "i") ||
2542 !strcmp(s_right
, "u")) {
2543 base
= BT_CTF_INTEGER_BASE_DECIMAL
;
2544 } else if (!strcmp(s_right
, "hexadecimal") ||
2545 !strcmp(s_right
, "hex") ||
2546 !strcmp(s_right
, "x") ||
2547 !strcmp(s_right
, "X") ||
2548 !strcmp(s_right
, "p")) {
2549 base
= BT_CTF_INTEGER_BASE_HEXADECIMAL
;
2550 } else if (!strcmp(s_right
, "octal") ||
2551 !strcmp(s_right
, "oct") ||
2552 !strcmp(s_right
, "o")) {
2553 base
= BT_CTF_INTEGER_BASE_OCTAL
;
2554 } else if (!strcmp(s_right
, "binary") ||
2555 !strcmp(s_right
, "b")) {
2556 base
= BT_CTF_INTEGER_BASE_BINARY
;
2558 _BT_LOGE_NODE(right
,
2559 "Unexpected unary expression for integer field type's `base` attribute: "
2560 "base=\"%s\"", s_right
);
2570 _BT_LOGE_NODE(right
,
2571 "Invalid `base` attribute in integer field type: "
2572 "expecting unsigned constant integer or unary string.");
2577 _SET(&set
, _INTEGER_BASE_SET
);
2578 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2582 if (_IS_SET(&set
, _INTEGER_ENCODING_SET
)) {
2583 _BT_LOGE_DUP_ATTR(left
, "encoding",
2584 "integer field type");
2589 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
2590 _BT_LOGE_NODE(right
,
2591 "Invalid `encoding` attribute in integer field type: "
2592 "expecting unary string.");
2597 s_right
= concatenate_unary_strings(
2598 &expression
->u
.ctf_expression
.right
);
2600 _BT_LOGE_NODE(right
,
2601 "Unexpected unary expression for integer field type's `encoding` attribute.");
2606 if (!strcmp(s_right
, "UTF8") ||
2607 !strcmp(s_right
, "utf8") ||
2608 !strcmp(s_right
, "utf-8") ||
2609 !strcmp(s_right
, "UTF-8")) {
2610 encoding
= BT_CTF_STRING_ENCODING_UTF8
;
2611 } else if (!strcmp(s_right
, "ASCII") ||
2612 !strcmp(s_right
, "ascii")) {
2613 encoding
= BT_CTF_STRING_ENCODING_ASCII
;
2614 } else if (!strcmp(s_right
, "none")) {
2615 encoding
= BT_CTF_STRING_ENCODING_NONE
;
2617 _BT_LOGE_NODE(right
,
2618 "Invalid `encoding` attribute in integer field type: "
2619 "unknown encoding: encoding=\"%s\"",
2627 _SET(&set
, _INTEGER_ENCODING_SET
);
2628 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "map")) {
2629 const char *clock_name
;
2631 if (_IS_SET(&set
, _INTEGER_MAP_SET
)) {
2632 _BT_LOGE_DUP_ATTR(left
, "map",
2633 "integer field type");
2638 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
2639 _BT_LOGE_NODE(right
,
2640 "Invalid `map` attribute in integer field type: "
2641 "expecting unary string.");
2647 get_map_clock_name_value(
2648 &expression
->u
.ctf_expression
.right
);
2650 char *s_right
= concatenate_unary_strings(
2651 &expression
->u
.ctf_expression
.right
);
2654 _BT_LOGE_NODE(right
,
2655 "Unexpected unary expression for integer field type's `map` attribute.");
2660 _BT_LOGE_NODE(right
,
2661 "Invalid `map` attribute in integer field type: "
2662 "cannot find clock class at this point: name=\"%s\"",
2664 _SET(&set
, _INTEGER_MAP_SET
);
2669 mapped_clock
= bt_ctf_trace_get_clock_class_by_name(
2670 ctx
->trace
, clock_name
);
2671 if (!mapped_clock
) {
2672 _BT_LOGE_NODE(right
,
2673 "Invalid `map` attribute in integer field type: "
2674 "cannot find clock class at this point: name=\"%s\"",
2680 _SET(&set
, _INTEGER_MAP_SET
);
2683 "Unknown attribute in integer field type: "
2685 left
->u
.unary_expression
.u
.string
);
2689 if (!_IS_SET(&set
, _INTEGER_SIZE_SET
)) {
2690 BT_LOGE_STR("Missing `size` attribute in integer field type.");
2695 if (!_IS_SET(&set
, _INTEGER_ALIGN_SET
)) {
2696 if (size
% CHAR_BIT
) {
2697 /* Bit-packed alignment */
2700 /* Byte-packed alignment */
2701 alignment
= CHAR_BIT
;
2705 *integer_decl
= bt_ctf_field_type_integer_create((unsigned int) size
);
2706 if (!*integer_decl
) {
2707 BT_LOGE_STR("Cannot create integer field type.");
2712 ret
= bt_ctf_field_type_integer_set_signed(*integer_decl
, signedness
);
2713 ret
|= bt_ctf_field_type_integer_set_base(*integer_decl
, base
);
2714 ret
|= bt_ctf_field_type_integer_set_encoding(*integer_decl
, encoding
);
2715 ret
|= bt_ctf_field_type_set_alignment(*integer_decl
,
2716 (unsigned int) alignment
);
2717 ret
|= bt_ctf_field_type_set_byte_order(*integer_decl
, byte_order
);
2721 ret
|= bt_ctf_field_type_integer_set_mapped_clock_class(
2722 *integer_decl
, mapped_clock
);
2723 bt_put(mapped_clock
);
2724 mapped_clock
= NULL
;
2728 BT_LOGE_STR("Cannot configure integer field type.");
2737 bt_put(mapped_clock
);
2740 BT_PUT(*integer_decl
);
2746 int visit_floating_point_number_decl(struct ctx
*ctx
,
2747 struct bt_list_head
*expressions
,
2748 struct bt_ctf_field_type
**float_decl
)
2752 struct ctf_node
*expression
;
2753 uint64_t alignment
= 1, exp_dig
= 0, mant_dig
= 0;
2754 enum bt_ctf_byte_order byte_order
=
2755 bt_ctf_trace_get_native_byte_order(ctx
->trace
);
2759 bt_list_for_each_entry(expression
, expressions
, siblings
) {
2760 struct ctf_node
*left
, *right
;
2762 left
= _BT_LIST_FIRST_ENTRY(&expression
->u
.ctf_expression
.left
,
2763 struct ctf_node
, siblings
);
2764 right
= _BT_LIST_FIRST_ENTRY(
2765 &expression
->u
.ctf_expression
.right
, struct ctf_node
,
2768 if (left
->u
.unary_expression
.type
!= UNARY_STRING
) {
2770 "Unexpected unary expression type: type=%d",
2771 left
->u
.unary_expression
.type
);
2776 if (!strcmp(left
->u
.unary_expression
.u
.string
, "byte_order")) {
2777 if (_IS_SET(&set
, _FLOAT_BYTE_ORDER_SET
)) {
2778 _BT_LOGE_DUP_ATTR(left
, "byte_order",
2779 "floating point number field type");
2784 byte_order
= get_real_byte_order(ctx
, right
);
2785 if (byte_order
== BT_CTF_BYTE_ORDER_UNKNOWN
) {
2786 _BT_LOGE_NODE(right
,
2787 "Invalid `byte_order` attribute in floating point number field type: "
2793 _SET(&set
, _FLOAT_BYTE_ORDER_SET
);
2794 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2796 if (_IS_SET(&set
, _FLOAT_EXP_DIG_SET
)) {
2797 _BT_LOGE_DUP_ATTR(left
, "exp_dig",
2798 "floating point number field type");
2803 if (right
->u
.unary_expression
.type
!=
2804 UNARY_UNSIGNED_CONSTANT
) {
2805 _BT_LOGE_NODE(right
,
2806 "Invalid `exp_dig` attribute in floating point number field type: "
2807 "expecting unsigned constant integer: "
2809 right
->u
.unary_expression
.type
);
2814 exp_dig
= right
->u
.unary_expression
.u
.unsigned_constant
;
2815 _SET(&set
, _FLOAT_EXP_DIG_SET
);
2816 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2818 if (_IS_SET(&set
, _FLOAT_MANT_DIG_SET
)) {
2819 _BT_LOGE_DUP_ATTR(left
, "mant_dig",
2820 "floating point number field type");
2825 if (right
->u
.unary_expression
.type
!=
2826 UNARY_UNSIGNED_CONSTANT
) {
2827 _BT_LOGE_NODE(right
,
2828 "Invalid `mant_dig` attribute in floating point number field type: "
2829 "expecting unsigned constant integer: "
2831 right
->u
.unary_expression
.type
);
2836 mant_dig
= right
->u
.unary_expression
.u
.
2838 _SET(&set
, _FLOAT_MANT_DIG_SET
);
2839 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2841 if (_IS_SET(&set
, _FLOAT_ALIGN_SET
)) {
2842 _BT_LOGE_DUP_ATTR(left
, "align",
2843 "floating point number field type");
2848 if (right
->u
.unary_expression
.type
!=
2849 UNARY_UNSIGNED_CONSTANT
) {
2850 _BT_LOGE_NODE(right
,
2851 "Invalid `align` attribute in floating point number field type: "
2852 "expecting unsigned constant integer: "
2854 right
->u
.unary_expression
.type
);
2859 alignment
= right
->u
.unary_expression
.u
.
2862 if (!is_align_valid(alignment
)) {
2863 _BT_LOGE_NODE(right
,
2864 "Invalid `align` attribute in floating point number field type: "
2865 "expecting power of two: "
2866 "align=%" PRIu64
, alignment
);
2871 _SET(&set
, _FLOAT_ALIGN_SET
);
2874 "Unknown attribute in floating point number field type: "
2876 left
->u
.unary_expression
.u
.string
);
2880 if (!_IS_SET(&set
, _FLOAT_MANT_DIG_SET
)) {
2881 BT_LOGE_STR("Missing `mant_dig` attribute in floating point number field type.");
2886 if (!_IS_SET(&set
, _FLOAT_EXP_DIG_SET
)) {
2887 BT_LOGE_STR("Missing `exp_dig` attribute in floating point number field type.");
2892 if (!_IS_SET(&set
, _INTEGER_ALIGN_SET
)) {
2893 if ((mant_dig
+ exp_dig
) % CHAR_BIT
) {
2894 /* Bit-packed alignment */
2897 /* Byte-packed alignment */
2898 alignment
= CHAR_BIT
;
2902 *float_decl
= bt_ctf_field_type_floating_point_create();
2904 BT_LOGE_STR("Cannot create floating point number field type.");
2909 ret
= bt_ctf_field_type_floating_point_set_exponent_digits(
2910 *float_decl
, exp_dig
);
2911 ret
|= bt_ctf_field_type_floating_point_set_mantissa_digits(
2912 *float_decl
, mant_dig
);
2913 ret
|= bt_ctf_field_type_set_byte_order(*float_decl
, byte_order
);
2914 ret
|= bt_ctf_field_type_set_alignment(*float_decl
, alignment
);
2916 BT_LOGE_STR("Cannot configure floating point number field type.");
2924 BT_PUT(*float_decl
);
2930 int visit_string_decl(struct ctx
*ctx
,
2931 struct bt_list_head
*expressions
,
2932 struct bt_ctf_field_type
**string_decl
)
2936 struct ctf_node
*expression
;
2937 enum bt_ctf_string_encoding encoding
= BT_CTF_STRING_ENCODING_UTF8
;
2939 *string_decl
= NULL
;
2941 bt_list_for_each_entry(expression
, expressions
, siblings
) {
2942 struct ctf_node
*left
, *right
;
2944 left
= _BT_LIST_FIRST_ENTRY(&expression
->u
.ctf_expression
.left
,
2945 struct ctf_node
, siblings
);
2946 right
= _BT_LIST_FIRST_ENTRY(
2947 &expression
->u
.ctf_expression
.right
, struct ctf_node
,
2950 if (left
->u
.unary_expression
.type
!= UNARY_STRING
) {
2952 "Unexpected unary expression type: type=%d",
2953 left
->u
.unary_expression
.type
);
2958 if (!strcmp(left
->u
.unary_expression
.u
.string
, "encoding")) {
2961 if (_IS_SET(&set
, _STRING_ENCODING_SET
)) {
2962 _BT_LOGE_DUP_ATTR(left
, "encoding",
2963 "string field type");
2968 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
2969 _BT_LOGE_NODE(right
,
2970 "Invalid `encoding` attribute in string field type: "
2971 "expecting unary string.");
2976 s_right
= concatenate_unary_strings(
2977 &expression
->u
.ctf_expression
.right
);
2979 _BT_LOGE_NODE(right
,
2980 "Unexpected unary expression for string field type's `encoding` attribute.");
2985 if (!strcmp(s_right
, "UTF8") ||
2986 !strcmp(s_right
, "utf8") ||
2987 !strcmp(s_right
, "utf-8") ||
2988 !strcmp(s_right
, "UTF-8")) {
2989 encoding
= BT_CTF_STRING_ENCODING_UTF8
;
2990 } else if (!strcmp(s_right
, "ASCII") ||
2991 !strcmp(s_right
, "ascii")) {
2992 encoding
= BT_CTF_STRING_ENCODING_ASCII
;
2993 } else if (!strcmp(s_right
, "none")) {
2994 encoding
= BT_CTF_STRING_ENCODING_NONE
;
2996 _BT_LOGE_NODE(right
,
2997 "Invalid `encoding` attribute in string field type: "
2998 "unknown encoding: encoding=\"%s\"",
3006 _SET(&set
, _STRING_ENCODING_SET
);
3009 "Unknown attribute in string field type: "
3011 left
->u
.unary_expression
.u
.string
);
3015 *string_decl
= bt_ctf_field_type_string_create();
3016 if (!*string_decl
) {
3017 BT_LOGE_STR("Cannot create string field type.");
3022 ret
= bt_ctf_field_type_string_set_encoding(*string_decl
, encoding
);
3024 BT_LOGE_STR("Cannot configure string field type.");
3032 BT_PUT(*string_decl
);
3038 int visit_type_specifier_list(struct ctx
*ctx
,
3039 struct ctf_node
*ts_list
,
3040 struct bt_ctf_field_type
**decl
)
3043 struct ctf_node
*first
, *node
;
3047 if (ts_list
->type
!= NODE_TYPE_SPECIFIER_LIST
) {
3048 _BT_LOGE_NODE(ts_list
,
3049 "Unexpected node type: node-type=%d", ts_list
->type
);
3054 first
= _BT_LIST_FIRST_ENTRY(&ts_list
->u
.type_specifier_list
.head
,
3055 struct ctf_node
, siblings
);
3056 if (first
->type
!= NODE_TYPE_SPECIFIER
) {
3057 _BT_LOGE_NODE(first
,
3058 "Unexpected node type: node-type=%d", first
->type
);
3063 node
= first
->u
.type_specifier
.node
;
3065 switch (first
->u
.type_specifier
.type
) {
3066 case TYPESPEC_INTEGER
:
3067 ret
= visit_integer_decl(ctx
, &node
->u
.integer
.expressions
,
3074 case TYPESPEC_FLOATING_POINT
:
3075 ret
= visit_floating_point_number_decl(ctx
,
3076 &node
->u
.floating_point
.expressions
, decl
);
3082 case TYPESPEC_STRING
:
3083 ret
= visit_string_decl(ctx
,
3084 &node
->u
.string
.expressions
, decl
);
3090 case TYPESPEC_STRUCT
:
3091 ret
= visit_struct_decl(ctx
, node
->u
._struct
.name
,
3092 &node
->u
._struct
.declaration_list
,
3093 node
->u
._struct
.has_body
,
3094 &node
->u
._struct
.min_align
, decl
);
3100 case TYPESPEC_VARIANT
:
3101 ret
= visit_variant_decl(ctx
, node
->u
.variant
.name
,
3102 node
->u
.variant
.choice
,
3103 &node
->u
.variant
.declaration_list
,
3104 node
->u
.variant
.has_body
, decl
);
3111 ret
= visit_enum_decl(ctx
, node
->u
._enum
.enum_id
,
3112 node
->u
._enum
.container_type
,
3113 &node
->u
._enum
.enumerator_list
,
3114 node
->u
._enum
.has_body
, decl
);
3122 case TYPESPEC_SHORT
:
3125 case TYPESPEC_FLOAT
:
3126 case TYPESPEC_DOUBLE
:
3127 case TYPESPEC_SIGNED
:
3128 case TYPESPEC_UNSIGNED
:
3130 case TYPESPEC_COMPLEX
:
3131 case TYPESPEC_IMAGINARY
:
3132 case TYPESPEC_CONST
:
3133 case TYPESPEC_ID_TYPE
:
3134 ret
= visit_type_specifier(ctx
, ts_list
, decl
);
3136 _BT_LOGE_NODE(first
,
3137 "Cannot visit type specifier: ret=%d",
3144 _BT_LOGE_NODE(first
,
3145 "Unexpected type specifier type: node-type=%d",
3146 first
->u
.type_specifier
.type
);
3162 int visit_event_decl_entry(struct ctx
*ctx
, struct ctf_node
*node
,
3163 struct bt_ctf_event_class
*event_class
, int64_t *stream_id
,
3168 _BT_CTF_FIELD_TYPE_INIT(decl
);
3170 switch (node
->type
) {
3172 ret
= visit_typedef(ctx
, node
->u
._typedef
.type_specifier_list
,
3173 &node
->u
._typedef
.type_declarators
);
3176 "Cannot add type definition found in event class.");
3180 case NODE_TYPEALIAS
:
3181 ret
= visit_typealias(ctx
, node
->u
.typealias
.target
,
3182 node
->u
.typealias
.alias
);
3185 "Cannot add type alias found in event class.");
3189 case NODE_CTF_EXPRESSION
:
3191 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
3193 _BT_LOGE_NODE(node
, "Cannot concatenate unary strings.");
3198 if (!strcmp(left
, "name")) {
3199 /* This is already known at this stage */
3200 if (_IS_SET(set
, _EVENT_NAME_SET
)) {
3201 _BT_LOGE_DUP_ATTR(node
, "name",
3207 _SET(set
, _EVENT_NAME_SET
);
3208 } else if (!strcmp(left
, "id")) {
3211 if (_IS_SET(set
, _EVENT_ID_SET
)) {
3212 _BT_LOGE_DUP_ATTR(node
, "id",
3218 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
3220 /* Only read "id" if get_unary_unsigned() succeeded. */
3221 if (ret
|| (!ret
&& id
< 0)) {
3223 "Unexpected unary expression for event class's `id` attribute.");
3228 ret
= bt_ctf_event_class_set_id(event_class
, id
);
3231 "Cannot set event class's ID: "
3236 _SET(set
, _EVENT_ID_SET
);
3237 } else if (!strcmp(left
, "stream_id")) {
3238 if (_IS_SET(set
, _EVENT_STREAM_ID_SET
)) {
3239 _BT_LOGE_DUP_ATTR(node
, "stream_id",
3245 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
3246 (uint64_t *) stream_id
);
3248 * Only read "stream_id" if get_unary_unsigned()
3251 if (ret
|| (!ret
&& *stream_id
< 0)) {
3253 "Unexpected unary expression for event class's `stream_id` attribute.");
3258 _SET(set
, _EVENT_STREAM_ID_SET
);
3259 } else if (!strcmp(left
, "context")) {
3260 if (_IS_SET(set
, _EVENT_CONTEXT_SET
)) {
3262 "Duplicate `context` entry in event class.");
3267 ret
= visit_type_specifier_list(ctx
,
3268 _BT_LIST_FIRST_ENTRY(
3269 &node
->u
.ctf_expression
.right
,
3270 struct ctf_node
, siblings
),
3274 "Cannot create event class's context field type.");
3279 ret
= bt_ctf_event_class_set_context_type(
3284 "Cannot set event class's context field type.");
3288 _SET(set
, _EVENT_CONTEXT_SET
);
3289 } else if (!strcmp(left
, "fields")) {
3290 if (_IS_SET(set
, _EVENT_FIELDS_SET
)) {
3292 "Duplicate `fields` entry in event class.");
3297 ret
= visit_type_specifier_list(ctx
,
3298 _BT_LIST_FIRST_ENTRY(
3299 &node
->u
.ctf_expression
.right
,
3300 struct ctf_node
, siblings
),
3304 "Cannot create event class's payload field type.");
3309 ret
= bt_ctf_event_class_set_payload_type(
3314 "Cannot set event class's payload field type.");
3318 _SET(set
, _EVENT_FIELDS_SET
);
3319 } else if (!strcmp(left
, "loglevel")) {
3320 uint64_t loglevel_value
;
3321 const char *loglevel_str
;
3322 struct bt_value
*value_obj
, *str_obj
;
3324 if (_IS_SET(set
, _EVENT_LOGLEVEL_SET
)) {
3325 _BT_LOGE_DUP_ATTR(node
, "loglevel",
3331 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
3335 "Unexpected unary expression for event class's `loglevel` attribute.");
3339 value_obj
= bt_value_integer_create_init(loglevel_value
);
3342 "Cannot create integer value object.");
3346 if (bt_ctf_event_class_set_attribute(event_class
,
3347 "loglevel", value_obj
) != BT_VALUE_STATUS_OK
) {
3349 "Cannot set event class's `loglevel` attribute.");
3354 loglevel_str
= print_loglevel(loglevel_value
);
3356 str_obj
= bt_value_string_create_init(loglevel_str
);
3357 if (bt_ctf_event_class_set_attribute(event_class
,
3358 "loglevel_string", str_obj
) != BT_VALUE_STATUS_OK
) {
3360 "Cannot set event class's `loglevel_string` attribute.");
3368 _SET(set
, _EVENT_LOGLEVEL_SET
);
3369 } else if (!strcmp(left
, "model.emf.uri")) {
3371 struct bt_value
*str_obj
;
3373 if (_IS_SET(set
, _EVENT_MODEL_EMF_URI_SET
)) {
3374 _BT_LOGE_DUP_ATTR(node
, "model.emf.uri",
3380 right
= concatenate_unary_strings(
3381 &node
->u
.ctf_expression
.right
);
3384 "Unexpected unary expression for event class's `model.emf.uri` attribute.");
3389 str_obj
= bt_value_string_create_init(right
);
3390 if (bt_ctf_event_class_set_attribute(event_class
,
3391 "model.emf.uri", str_obj
) != BT_VALUE_STATUS_OK
) {
3393 "Cannot set event class's `model.emf.uri` attribute.");
3400 _SET(set
, _EVENT_MODEL_EMF_URI_SET
);
3403 "Unknown attribute in event class: "
3404 "attr-name=\"%s\"", left
);
3429 char *get_event_decl_name(struct ctx
*ctx
, struct ctf_node
*node
)
3433 struct ctf_node
*iter
;
3434 struct bt_list_head
*decl_list
= &node
->u
.event
.declaration_list
;
3436 bt_list_for_each_entry(iter
, decl_list
, siblings
) {
3437 if (iter
->type
!= NODE_CTF_EXPRESSION
) {
3441 left
= concatenate_unary_strings(&iter
->u
.ctf_expression
.left
);
3444 "Cannot concatenate unary strings.");
3448 if (!strcmp(left
, "name")) {
3449 name
= concatenate_unary_strings(
3450 &iter
->u
.ctf_expression
.right
);
3453 "Unexpected unary expression for event class's `name` attribute.");
3475 int reset_event_decl_types(struct ctx
*ctx
,
3476 struct bt_ctf_event_class
*event_class
)
3481 ret
= bt_ctf_event_class_set_context_type(event_class
, NULL
);
3483 BT_LOGE("Cannot reset initial event class's context field type: "
3484 "event-name=\"%s\"",
3485 bt_ctf_event_class_get_name(event_class
));
3489 /* Event payload. */
3490 ret
= bt_ctf_event_class_set_payload_type(event_class
, NULL
);
3492 BT_LOGE("Cannot reset initial event class's payload field type: "
3493 "event-name=\"%s\"",
3494 bt_ctf_event_class_get_name(event_class
));
3502 int reset_stream_decl_types(struct ctx
*ctx
,
3503 struct bt_ctf_stream_class
*stream_class
)
3507 /* Packet context. */
3508 ret
= bt_ctf_stream_class_set_packet_context_type(stream_class
, NULL
);
3510 BT_LOGE_STR("Cannot reset initial stream class's packet context field type.");
3515 ret
= bt_ctf_stream_class_set_event_header_type(stream_class
, NULL
);
3517 BT_LOGE_STR("Cannot reset initial stream class's event header field type.");
3521 /* Event context. */
3522 ret
= bt_ctf_stream_class_set_event_context_type(stream_class
, NULL
);
3524 BT_LOGE_STR("Cannot reset initial stream class's event context field type.");
3532 struct bt_ctf_stream_class
*create_reset_stream_class(struct ctx
*ctx
)
3535 struct bt_ctf_stream_class
*stream_class
;
3537 stream_class
= bt_ctf_stream_class_create_empty(NULL
);
3538 if (!stream_class
) {
3539 BT_LOGE_STR("Cannot create empty stream class.");
3544 * Set packet context, event header, and event context to NULL to
3545 * override the default ones.
3547 ret
= reset_stream_decl_types(ctx
, stream_class
);
3552 return stream_class
;
3555 BT_PUT(stream_class
);
3561 int visit_event_decl(struct ctx
*ctx
, struct ctf_node
*node
)
3566 struct ctf_node
*iter
;
3567 int64_t stream_id
= -1;
3568 char *event_name
= NULL
;
3569 struct bt_ctf_event_class
*event_class
= NULL
;
3570 struct bt_ctf_event_class
*eevent_class
;
3571 struct bt_ctf_stream_class
*stream_class
= NULL
;
3572 struct bt_list_head
*decl_list
= &node
->u
.event
.declaration_list
;
3573 bool pop_scope
= false;
3575 if (node
->visited
) {
3579 node
->visited
= TRUE
;
3580 event_name
= get_event_decl_name(ctx
, node
);
3583 "Missing `name` attribute in event class.");
3588 event_class
= bt_ctf_event_class_create(event_name
);
3591 * Unset context and fields to override the default ones.
3593 ret
= reset_event_decl_types(ctx
, event_class
);
3596 "Cannot reset event class's field types: "
3601 ret
= ctx_push_scope(ctx
);
3603 BT_LOGE_STR("Cannot push scope.");
3609 bt_list_for_each_entry(iter
, decl_list
, siblings
) {
3610 ret
= visit_event_decl_entry(ctx
, iter
, event_class
,
3613 _BT_LOGE_NODE(iter
, "Cannot visit event class's entry: "
3619 if (!_IS_SET(&set
, _EVENT_STREAM_ID_SET
)) {
3621 struct bt_ctf_stream_class
*new_stream_class
;
3622 size_t stream_class_count
=
3623 g_hash_table_size(ctx
->stream_classes
) +
3624 bt_ctf_trace_get_stream_class_count(ctx
->trace
);
3627 * Allow missing stream_id if there is only a single
3630 switch (stream_class_count
) {
3632 /* Create implicit stream class if there's none */
3633 new_stream_class
= create_reset_stream_class(ctx
);
3634 if (!new_stream_class
) {
3636 "Cannot create empty stream class.");
3641 ret
= bt_ctf_stream_class_set_id(new_stream_class
, 0);
3644 "Cannot set stream class's ID: "
3645 "id=0, ret=%d", ret
);
3646 BT_PUT(new_stream_class
);
3652 /* Move reference to visitor's context */
3653 g_hash_table_insert(ctx
->stream_classes
,
3654 (gpointer
) stream_id
, new_stream_class
);
3655 new_stream_class
= NULL
;
3658 /* Single stream class: get its ID */
3659 if (g_hash_table_size(ctx
->stream_classes
) == 1) {
3660 keys
= g_hash_table_get_keys(ctx
->stream_classes
);
3661 stream_id
= (int64_t) keys
->data
;
3664 assert(bt_ctf_trace_get_stream_class_count(
3667 bt_ctf_trace_get_stream_class_by_index(
3669 assert(stream_class
);
3670 stream_id
= bt_ctf_stream_class_get_id(
3672 BT_PUT(stream_class
);
3677 "Missing `stream_id` attribute in event class.");
3683 assert(stream_id
>= 0);
3685 /* We have the stream ID now; get the stream class if found */
3686 stream_class
= g_hash_table_lookup(ctx
->stream_classes
,
3687 (gpointer
) stream_id
);
3688 bt_get(stream_class
);
3689 if (!stream_class
) {
3690 stream_class
= bt_ctf_trace_get_stream_class_by_id(ctx
->trace
,
3692 if (!stream_class
) {
3694 "Cannot find stream class at this point: "
3695 "id=%" PRId64
, stream_id
);
3701 assert(stream_class
);
3703 if (!_IS_SET(&set
, _EVENT_ID_SET
)) {
3704 /* Allow only one event without ID per stream */
3705 if (bt_ctf_stream_class_get_event_class_count(stream_class
) !=
3708 "Missing `id` attribute in event class.");
3714 ret
= bt_ctf_event_class_set_id(event_class
, 0);
3717 "Cannot set event class's ID: id=0, ret=%d",
3723 event_id
= bt_ctf_event_class_get_id(event_class
);
3725 _BT_LOGE_NODE(node
, "Cannot get event class's ID.");
3730 eevent_class
= bt_ctf_stream_class_get_event_class_by_id(stream_class
,
3733 BT_PUT(eevent_class
);
3735 "Duplicate event class (same ID) in the same stream class: "
3736 "id=%" PRId64
, event_id
);
3741 ret
= bt_ctf_stream_class_add_event_class(stream_class
, event_class
);
3742 BT_PUT(event_class
);
3745 "Cannot add event class to stream class: ret=%d", ret
);
3752 bt_put(event_class
);
3760 bt_put(stream_class
);
3765 int visit_stream_decl_entry(struct ctx
*ctx
, struct ctf_node
*node
,
3766 struct bt_ctf_stream_class
*stream_class
, int *set
)
3770 _BT_CTF_FIELD_TYPE_INIT(decl
);
3772 switch (node
->type
) {
3774 ret
= visit_typedef(ctx
, node
->u
._typedef
.type_specifier_list
,
3775 &node
->u
._typedef
.type_declarators
);
3778 "Cannot add type definition found in stream class.");
3782 case NODE_TYPEALIAS
:
3783 ret
= visit_typealias(ctx
, node
->u
.typealias
.target
,
3784 node
->u
.typealias
.alias
);
3787 "Cannot add type alias found in stream class.");
3791 case NODE_CTF_EXPRESSION
:
3793 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
3795 _BT_LOGE_NODE(node
, "Cannot concatenate unary strings.");
3800 if (!strcmp(left
, "id")) {
3804 if (_IS_SET(set
, _STREAM_ID_SET
)) {
3805 _BT_LOGE_DUP_ATTR(node
, "id",
3806 "stream declaration");
3811 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
3813 /* Only read "id" if get_unary_unsigned() succeeded. */
3814 if (ret
|| (!ret
&& id
< 0)) {
3816 "Unexpected unary expression for stream class's `id` attribute.");
3821 ptr
= g_hash_table_lookup(ctx
->stream_classes
,
3825 "Duplicate stream class (same ID): id=%" PRId64
,
3831 ret
= bt_ctf_stream_class_set_id(stream_class
, id
);
3834 "Cannot set stream class's ID: "
3835 "id=%" PRId64
", ret=%d", id
, ret
);
3839 _SET(set
, _STREAM_ID_SET
);
3840 } else if (!strcmp(left
, "event.header")) {
3841 if (_IS_SET(set
, _STREAM_EVENT_HEADER_SET
)) {
3843 "Duplicate `event.header` entry in stream class.");
3848 ret
= visit_type_specifier_list(ctx
,
3849 _BT_LIST_FIRST_ENTRY(
3850 &node
->u
.ctf_expression
.right
,
3851 struct ctf_node
, siblings
),
3855 "Cannot create stream class's event header field type.");
3861 ret
= bt_ctf_stream_class_set_event_header_type(
3862 stream_class
, decl
);
3866 "Cannot set stream class's event header field type.");
3870 _SET(set
, _STREAM_EVENT_HEADER_SET
);
3871 } else if (!strcmp(left
, "event.context")) {
3872 if (_IS_SET(set
, _STREAM_EVENT_CONTEXT_SET
)) {
3874 "Duplicate `event.context` entry in stream class.");
3879 ret
= visit_type_specifier_list(ctx
,
3880 _BT_LIST_FIRST_ENTRY(
3881 &node
->u
.ctf_expression
.right
,
3882 struct ctf_node
, siblings
),
3886 "Cannot create stream class's event context field type.");
3892 ret
= bt_ctf_stream_class_set_event_context_type(
3893 stream_class
, decl
);
3897 "Cannot set stream class's event context field type.");
3901 _SET(set
, _STREAM_EVENT_CONTEXT_SET
);
3902 } else if (!strcmp(left
, "packet.context")) {
3903 if (_IS_SET(set
, _STREAM_PACKET_CONTEXT_SET
)) {
3905 "Duplicate `packet.context` entry in stream class.");
3910 ret
= visit_type_specifier_list(ctx
,
3911 _BT_LIST_FIRST_ENTRY(
3912 &node
->u
.ctf_expression
.right
,
3913 struct ctf_node
, siblings
),
3917 "Cannot create stream class's packet context field type.");
3923 ret
= bt_ctf_stream_class_set_packet_context_type(
3924 stream_class
, decl
);
3928 "Cannot set stream class's packet context field type.");
3932 _SET(set
, _STREAM_PACKET_CONTEXT_SET
);
3935 "Unknown attribute in stream class: "
3936 "attr-name=\"%s\"", left
);
3959 int visit_stream_decl(struct ctx
*ctx
, struct ctf_node
*node
)
3964 struct ctf_node
*iter
;
3965 struct bt_ctf_stream_class
*stream_class
= NULL
;
3966 struct bt_ctf_stream_class
*existing_stream_class
= NULL
;
3967 struct bt_list_head
*decl_list
= &node
->u
.stream
.declaration_list
;
3969 if (node
->visited
) {
3973 node
->visited
= TRUE
;
3974 stream_class
= create_reset_stream_class(ctx
);
3975 if (!stream_class
) {
3976 _BT_LOGE_NODE(node
, "Cannot create empty stream class.");
3981 ret
= ctx_push_scope(ctx
);
3983 BT_LOGE_STR("Cannot push scope.");
3987 bt_list_for_each_entry(iter
, decl_list
, siblings
) {
3988 ret
= visit_stream_decl_entry(ctx
, iter
, stream_class
, &set
);
3991 "Cannot visit stream class's entry: "
4000 if (_IS_SET(&set
, _STREAM_ID_SET
)) {
4001 /* Check that packet header has stream_id field */
4002 _BT_CTF_FIELD_TYPE_INIT(stream_id_decl
);
4003 _BT_CTF_FIELD_TYPE_INIT(packet_header_decl
);
4005 packet_header_decl
=
4006 bt_ctf_trace_get_packet_header_type(ctx
->trace
);
4007 if (!packet_header_decl
) {
4009 "Stream class has a `id` attribute, "
4010 "but trace has no packet header field type.");
4015 bt_ctf_field_type_structure_get_field_type_by_name(
4016 packet_header_decl
, "stream_id");
4017 BT_PUT(packet_header_decl
);
4018 if (!stream_id_decl
) {
4020 "Stream class has a `id` attribute, "
4021 "but trace's packet header field type has no `stream_id` field.");
4025 if (!bt_ctf_field_type_is_integer(stream_id_decl
)) {
4026 BT_PUT(stream_id_decl
);
4028 "Stream class has a `id` attribute, "
4029 "but trace's packet header field type's `stream_id` field is not an integer field type.");
4033 BT_PUT(stream_id_decl
);
4035 /* Allow only _one_ ID-less stream */
4036 if (g_hash_table_size(ctx
->stream_classes
) != 0) {
4038 "Missing `id` attribute in stream class as there's more than one stream class in the trace.");
4043 /* Automatic ID: 0 */
4044 ret
= bt_ctf_stream_class_set_id(stream_class
, 0);
4048 id
= bt_ctf_stream_class_get_id(stream_class
);
4051 "Cannot get stream class's ID.");
4057 * Make sure that this stream class's ID is currently unique in
4060 existing_stream_class
= bt_ctf_trace_get_stream_class_by_id(ctx
->trace
,
4062 if (g_hash_table_lookup(ctx
->stream_classes
, (gpointer
) id
) ||
4063 existing_stream_class
) {
4065 "Duplicate stream class (same ID): id=%" PRId64
,
4071 /* Move reference to visitor's context */
4072 g_hash_table_insert(ctx
->stream_classes
, (gpointer
) (int64_t) id
,
4074 stream_class
= NULL
;
4078 bt_put(stream_class
);
4081 bt_put(existing_stream_class
);
4086 int visit_trace_decl_entry(struct ctx
*ctx
, struct ctf_node
*node
, int *set
)
4090 _BT_CTF_FIELD_TYPE_INIT(packet_header_decl
);
4092 switch (node
->type
) {
4094 ret
= visit_typedef(ctx
, node
->u
._typedef
.type_specifier_list
,
4095 &node
->u
._typedef
.type_declarators
);
4098 "Cannot add type definition found in trace (`trace` block).");
4102 case NODE_TYPEALIAS
:
4103 ret
= visit_typealias(ctx
, node
->u
.typealias
.target
,
4104 node
->u
.typealias
.alias
);
4107 "Cannot add type alias found in trace (`trace` block).");
4111 case NODE_CTF_EXPRESSION
:
4113 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
4115 _BT_LOGE_NODE(node
, "Cannot concatenate unary strings.");
4120 if (!strcmp(left
, "major")) {
4121 if (_IS_SET(set
, _TRACE_MAJOR_SET
)) {
4122 _BT_LOGE_DUP_ATTR(node
, "major", "trace");
4127 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
4131 "Unexpected unary expression for trace's `major` attribute.");
4136 _SET(set
, _TRACE_MAJOR_SET
);
4137 } else if (!strcmp(left
, "minor")) {
4138 if (_IS_SET(set
, _TRACE_MINOR_SET
)) {
4139 _BT_LOGE_DUP_ATTR(node
, "minor", "trace");
4144 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
4148 "Unexpected unary expression for trace's `minor` attribute.");
4153 _SET(set
, _TRACE_MINOR_SET
);
4154 } else if (!strcmp(left
, "uuid")) {
4155 if (_IS_SET(set
, _TRACE_UUID_SET
)) {
4156 _BT_LOGE_DUP_ATTR(node
, "uuid", "trace");
4161 ret
= get_unary_uuid(&node
->u
.ctf_expression
.right
,
4165 "Invalid trace's `uuid` attribute.");
4169 ret
= bt_ctf_trace_set_uuid(ctx
->trace
, ctx
->trace_uuid
);
4171 _BT_LOGE_NODE(node
, "Cannot set trace's UUID.");
4175 _SET(set
, _TRACE_UUID_SET
);
4176 } else if (!strcmp(left
, "byte_order")) {
4177 /* Native byte order is already known at this stage */
4178 if (_IS_SET(set
, _TRACE_BYTE_ORDER_SET
)) {
4179 _BT_LOGE_DUP_ATTR(node
, "byte_order",
4185 _SET(set
, _TRACE_BYTE_ORDER_SET
);
4186 } else if (!strcmp(left
, "packet.header")) {
4187 if (_IS_SET(set
, _TRACE_PACKET_HEADER_SET
)) {
4189 "Duplicate `packet.header` entry in trace.");
4194 ret
= visit_type_specifier_list(ctx
,
4195 _BT_LIST_FIRST_ENTRY(
4196 &node
->u
.ctf_expression
.right
,
4197 struct ctf_node
, siblings
),
4198 &packet_header_decl
);
4201 "Cannot create trace's packet header field type.");
4205 assert(packet_header_decl
);
4206 ret
= bt_ctf_trace_set_packet_header_type(ctx
->trace
,
4207 packet_header_decl
);
4208 BT_PUT(packet_header_decl
);
4211 "Cannot set trace's packet header field type.");
4215 _SET(set
, _TRACE_PACKET_HEADER_SET
);
4218 "Unknown attribute in stream class: "
4219 "attr-name=\"%s\"", left
);
4227 _BT_LOGE_NODE(node
, "Unknown expression in trace.");
4236 BT_PUT(packet_header_decl
);
4242 int visit_trace_decl(struct ctx
*ctx
, struct ctf_node
*node
)
4246 struct ctf_node
*iter
;
4247 struct bt_list_head
*decl_list
= &node
->u
.trace
.declaration_list
;
4249 if (node
->visited
) {
4253 node
->visited
= TRUE
;
4255 if (ctx
->is_trace_visited
) {
4256 _BT_LOGE_NODE(node
, "Duplicate trace (`trace` block).");
4261 ret
= ctx_push_scope(ctx
);
4263 BT_LOGE_STR("Cannot push scope.");
4267 bt_list_for_each_entry(iter
, decl_list
, siblings
) {
4268 ret
= visit_trace_decl_entry(ctx
, iter
, &set
);
4270 _BT_LOGE_NODE(iter
, "Cannot visit trace's entry (`trace` block): "
4279 if (!_IS_SET(&set
, _TRACE_MAJOR_SET
)) {
4281 "Missing `major` attribute in trace (`trace` block).");
4286 if (!_IS_SET(&set
, _TRACE_MINOR_SET
)) {
4288 "Missing `minor` attribute in trace (`trace` block).");
4293 if (!_IS_SET(&set
, _TRACE_BYTE_ORDER_SET
)) {
4295 "Missing `byte_order` attribute in trace (`trace` block).");
4300 ctx
->is_trace_visited
= TRUE
;
4310 int visit_env(struct ctx
*ctx
, struct ctf_node
*node
)
4314 struct ctf_node
*entry_node
;
4315 struct bt_list_head
*decl_list
= &node
->u
.env
.declaration_list
;
4317 if (node
->visited
) {
4321 node
->visited
= TRUE
;
4323 bt_list_for_each_entry(entry_node
, decl_list
, siblings
) {
4324 struct bt_list_head
*right_head
=
4325 &entry_node
->u
.ctf_expression
.right
;
4327 if (entry_node
->type
!= NODE_CTF_EXPRESSION
) {
4328 _BT_LOGE_NODE(entry_node
,
4329 "Wrong expression in environment entry: "
4330 "node-type=%d", entry_node
->type
);
4335 left
= concatenate_unary_strings(
4336 &entry_node
->u
.ctf_expression
.left
);
4338 _BT_LOGE_NODE(entry_node
,
4339 "Cannot get environment entry's name.");
4344 if (is_unary_string(right_head
)) {
4345 char *right
= concatenate_unary_strings(right_head
);
4348 _BT_LOGE_NODE(entry_node
,
4349 "Unexpected unary expression for environment entry's value: "
4350 "name=\"%s\"", left
);
4355 if (!ctx
->decoder_config
.strict
) {
4356 if (strcmp(left
, "tracer_name") == 0) {
4357 if (strncmp(right
, "lttng", 5) == 0) {
4358 BT_LOGI("Non-strict mode: detected LTTng trace from `%s` environment value: "
4359 "tracer-name=\"%s\"",
4366 ret
= bt_ctf_trace_set_environment_field_string(
4367 ctx
->trace
, left
, right
);
4371 _BT_LOGE_NODE(entry_node
,
4372 "Cannot add string environment entry to trace: "
4373 "name=\"%s\", ret=%d", left
, ret
);
4376 } else if (is_unary_unsigned(right_head
) ||
4377 is_unary_signed(right_head
)) {
4380 if (is_unary_unsigned(right_head
)) {
4381 ret
= get_unary_unsigned(right_head
,
4384 ret
= get_unary_signed(right_head
, &v
);
4387 _BT_LOGE_NODE(entry_node
,
4388 "Unexpected unary expression for environment entry's value: "
4389 "name=\"%s\"", left
);
4394 ret
= bt_ctf_trace_set_environment_field_integer(
4395 ctx
->trace
, left
, v
);
4397 _BT_LOGE_NODE(entry_node
,
4398 "Cannot add integer environment entry to trace: "
4399 "name=\"%s\", ret=%d", left
, ret
);
4403 _BT_LOGW_NODE(entry_node
,
4404 "Environment entry has unknown type: "
4405 "name=\"%s\"", left
);
4422 int set_trace_byte_order(struct ctx
*ctx
, struct ctf_node
*trace_node
)
4427 struct ctf_node
*node
;
4428 struct bt_list_head
*decl_list
= &trace_node
->u
.trace
.declaration_list
;
4430 bt_list_for_each_entry(node
, decl_list
, siblings
) {
4431 if (node
->type
== NODE_CTF_EXPRESSION
) {
4432 struct ctf_node
*right_node
;
4434 left
= concatenate_unary_strings(
4435 &node
->u
.ctf_expression
.left
);
4438 "Cannot concatenate unary strings.");
4443 if (!strcmp(left
, "byte_order")) {
4444 enum bt_ctf_byte_order bo
;
4446 if (_IS_SET(&set
, _TRACE_BYTE_ORDER_SET
)) {
4447 _BT_LOGE_DUP_ATTR(node
, "byte_order",
4453 _SET(&set
, _TRACE_BYTE_ORDER_SET
);
4454 right_node
= _BT_LIST_FIRST_ENTRY(
4455 &node
->u
.ctf_expression
.right
,
4456 struct ctf_node
, siblings
);
4457 bo
= byte_order_from_unary_expr(right_node
);
4458 if (bo
== BT_CTF_BYTE_ORDER_UNKNOWN
) {
4460 "Invalid `byte_order` attribute in trace (`trace` block): "
4461 "expecting `le`, `be`, or `network`.");
4464 } else if (bo
== BT_CTF_BYTE_ORDER_NATIVE
) {
4466 "Invalid `byte_order` attribute in trace (`trace` block): "
4467 "cannot be set to `native` here.");
4473 ret
= bt_ctf_trace_set_native_byte_order(
4477 "Cannot set trace's byte order: "
4488 if (!_IS_SET(&set
, _TRACE_BYTE_ORDER_SET
)) {
4489 _BT_LOGE_NODE(trace_node
,
4490 "Missing `byte_order` attribute in trace (`trace` block).");
4504 int visit_clock_decl_entry(struct ctx
*ctx
, struct ctf_node
*entry_node
,
4505 struct bt_ctf_clock_class
*clock
, int *set
)
4510 if (entry_node
->type
!= NODE_CTF_EXPRESSION
) {
4511 _BT_LOGE_NODE(entry_node
,
4512 "Unexpected node type: node-type=%d",
4518 left
= concatenate_unary_strings(&entry_node
->u
.ctf_expression
.left
);
4520 _BT_LOGE_NODE(entry_node
, "Cannot concatenate unary strings.");
4525 if (!strcmp(left
, "name")) {
4528 if (_IS_SET(set
, _CLOCK_NAME_SET
)) {
4529 _BT_LOGE_DUP_ATTR(entry_node
, "name", "clock class");
4534 right
= concatenate_unary_strings(
4535 &entry_node
->u
.ctf_expression
.right
);
4537 _BT_LOGE_NODE(entry_node
,
4538 "Unexpected unary expression for clock class's `name` attribute.");
4543 ret
= bt_ctf_clock_class_set_name(clock
, right
);
4545 _BT_LOGE_NODE(entry_node
,
4546 "cannot set clock class's name");
4552 _SET(set
, _CLOCK_NAME_SET
);
4553 } else if (!strcmp(left
, "uuid")) {
4554 unsigned char uuid
[BABELTRACE_UUID_LEN
];
4556 if (_IS_SET(set
, _CLOCK_UUID_SET
)) {
4557 _BT_LOGE_DUP_ATTR(entry_node
, "uuid", "clock class");
4562 ret
= get_unary_uuid(&entry_node
->u
.ctf_expression
.right
, uuid
);
4564 _BT_LOGE_NODE(entry_node
,
4565 "Invalid clock class's `uuid` attribute.");
4569 ret
= bt_ctf_clock_class_set_uuid(clock
, uuid
);
4571 _BT_LOGE_NODE(entry_node
,
4572 "Cannot set clock class's UUID.");
4576 _SET(set
, _CLOCK_UUID_SET
);
4577 } else if (!strcmp(left
, "description")) {
4580 if (_IS_SET(set
, _CLOCK_DESCRIPTION_SET
)) {
4581 _BT_LOGE_DUP_ATTR(entry_node
, "description",
4587 right
= concatenate_unary_strings(
4588 &entry_node
->u
.ctf_expression
.right
);
4590 _BT_LOGE_NODE(entry_node
,
4591 "Unexpected unary expression for clock class's `description` attribute.");
4596 ret
= bt_ctf_clock_class_set_description(clock
, right
);
4598 _BT_LOGE_NODE(entry_node
,
4599 "Cannot set clock class's description.");
4605 _SET(set
, _CLOCK_DESCRIPTION_SET
);
4606 } else if (!strcmp(left
, "freq")) {
4609 if (_IS_SET(set
, _CLOCK_FREQ_SET
)) {
4610 _BT_LOGE_DUP_ATTR(entry_node
, "freq", "clock class");
4615 ret
= get_unary_unsigned(
4616 &entry_node
->u
.ctf_expression
.right
, &freq
);
4618 _BT_LOGE_NODE(entry_node
,
4619 "Unexpected unary expression for clock class's `freq` attribute.");
4624 ret
= bt_ctf_clock_class_set_frequency(clock
, freq
);
4626 _BT_LOGE_NODE(entry_node
,
4627 "Cannot set clock class's frequency.");
4631 _SET(set
, _CLOCK_FREQ_SET
);
4632 } else if (!strcmp(left
, "precision")) {
4635 if (_IS_SET(set
, _CLOCK_PRECISION_SET
)) {
4636 _BT_LOGE_DUP_ATTR(entry_node
, "precision",
4642 ret
= get_unary_unsigned(
4643 &entry_node
->u
.ctf_expression
.right
, &precision
);
4645 _BT_LOGE_NODE(entry_node
,
4646 "Unexpected unary expression for clock class's `precision` attribute.");
4651 ret
= bt_ctf_clock_class_set_precision(clock
, precision
);
4653 _BT_LOGE_NODE(entry_node
,
4654 "Cannot set clock class's precision.");
4658 _SET(set
, _CLOCK_PRECISION_SET
);
4659 } else if (!strcmp(left
, "offset_s")) {
4662 if (_IS_SET(set
, _CLOCK_OFFSET_S_SET
)) {
4663 _BT_LOGE_DUP_ATTR(entry_node
, "offset_s",
4669 ret
= get_unary_unsigned(
4670 &entry_node
->u
.ctf_expression
.right
, &offset_s
);
4672 _BT_LOGE_NODE(entry_node
,
4673 "Unexpected unary expression for clock class's `offset_s` attribute.");
4678 ret
= bt_ctf_clock_class_set_offset_s(clock
, offset_s
);
4680 _BT_LOGE_NODE(entry_node
,
4681 "Cannot set clock class's offset in seconds.");
4685 _SET(set
, _CLOCK_OFFSET_S_SET
);
4686 } else if (!strcmp(left
, "offset")) {
4689 if (_IS_SET(set
, _CLOCK_OFFSET_SET
)) {
4690 _BT_LOGE_DUP_ATTR(entry_node
, "offset", "clock class");
4695 ret
= get_unary_unsigned(
4696 &entry_node
->u
.ctf_expression
.right
, &offset
);
4698 _BT_LOGE_NODE(entry_node
,
4699 "Unexpected unary expression for clock class's `offset` attribute.");
4704 ret
= bt_ctf_clock_class_set_offset_cycles(clock
, offset
);
4706 _BT_LOGE_NODE(entry_node
,
4707 "Cannot set clock class's offset in cycles.");
4711 _SET(set
, _CLOCK_OFFSET_SET
);
4712 } else if (!strcmp(left
, "absolute")) {
4713 struct ctf_node
*right
;
4715 if (_IS_SET(set
, _CLOCK_ABSOLUTE_SET
)) {
4716 _BT_LOGE_DUP_ATTR(entry_node
, "absolute",
4722 right
= _BT_LIST_FIRST_ENTRY(
4723 &entry_node
->u
.ctf_expression
.right
,
4724 struct ctf_node
, siblings
);
4725 ret
= get_boolean(right
);
4727 _BT_LOGE_NODE(entry_node
,
4728 "Unexpected unary expression for clock class's `absolute` attribute.");
4733 ret
= bt_ctf_clock_class_set_is_absolute(clock
, ret
);
4735 _BT_LOGE_NODE(entry_node
,
4736 "Cannot set clock class's absolute flag.");
4740 _SET(set
, _CLOCK_ABSOLUTE_SET
);
4742 _BT_LOGW_NODE(entry_node
,
4743 "Unknown attribute in clock class: attr-name=\"%s\"",
4759 int64_t cycles_from_ns(uint64_t frequency
, int64_t ns
)
4764 if (frequency
== 1000000000ULL) {
4767 cycles
= (uint64_t) (((double) ns
* (double) frequency
) / 1e9
);
4774 int apply_clock_class_offset(struct ctx
*ctx
, struct bt_ctf_clock_class
*clock
)
4778 int64_t offset_cycles
;
4779 int64_t offset_to_apply
;
4781 freq
= bt_ctf_clock_class_get_frequency(clock
);
4782 if (freq
== -1ULL) {
4783 BT_LOGE_STR("Cannot get clock class's frequency.");
4788 ret
= bt_ctf_clock_class_get_offset_cycles(clock
, &offset_cycles
);
4790 BT_LOGE_STR("Cannot get clock class's offset in cycles.");
4796 ctx
->decoder_config
.clock_class_offset_s
* 1000000000LL +
4797 ctx
->decoder_config
.clock_class_offset_ns
;
4798 offset_cycles
+= cycles_from_ns(freq
, offset_to_apply
);
4799 ret
= bt_ctf_clock_class_set_offset_cycles(clock
, offset_cycles
);
4806 int visit_clock_decl(struct ctx
*ctx
, struct ctf_node
*clock_node
)
4810 struct bt_ctf_clock_class
*clock
;
4811 struct ctf_node
*entry_node
;
4812 struct bt_list_head
*decl_list
= &clock_node
->u
.clock
.declaration_list
;
4813 const char *clock_class_name
;
4815 if (clock_node
->visited
) {
4819 clock_node
->visited
= TRUE
;
4820 clock
= bt_ctf_clock_class_create(NULL
);
4822 _BT_LOGE_NODE(clock_node
,
4823 "Cannot create default clock class.");
4828 bt_list_for_each_entry(entry_node
, decl_list
, siblings
) {
4829 ret
= visit_clock_decl_entry(ctx
, entry_node
, clock
, &set
);
4831 _BT_LOGE_NODE(entry_node
,
4832 "Cannot visit clock class's entry: ret=%d",
4838 if (!_IS_SET(&set
, _CLOCK_NAME_SET
)) {
4839 _BT_LOGE_NODE(clock_node
,
4840 "Missing `name` attribute in clock class.");
4845 clock_class_name
= bt_ctf_clock_class_get_name(clock
);
4846 assert(clock_class_name
);
4847 if (ctx
->is_lttng
&& strcmp(clock_class_name
, "monotonic") == 0) {
4849 * Old versions of LTTng forgot to set its clock class
4850 * as absolute, even if it is. This is important because
4851 * it's a condition to be able to sort notifications
4852 * from different sources.
4854 ret
= bt_ctf_clock_class_set_is_absolute(clock
, 1);
4856 _BT_LOGE_NODE(clock_node
,
4857 "Cannot set clock class's absolute flag.");
4862 ret
= apply_clock_class_offset(ctx
, clock
);
4864 _BT_LOGE_NODE(clock_node
,
4865 "Cannot apply clock class's custom offset.");
4869 ret
= bt_ctf_trace_add_clock_class(ctx
->trace
, clock
);
4871 _BT_LOGE_NODE(clock_node
,
4872 "Cannot add clock class to trace.");
4883 int visit_root_decl(struct ctx
*ctx
, struct ctf_node
*root_decl_node
)
4887 if (root_decl_node
->visited
) {
4891 root_decl_node
->visited
= TRUE
;
4893 switch (root_decl_node
->type
) {
4895 ret
= visit_typedef(ctx
,
4896 root_decl_node
->u
._typedef
.type_specifier_list
,
4897 &root_decl_node
->u
._typedef
.type_declarators
);
4899 _BT_LOGE_NODE(root_decl_node
,
4900 "Cannot add type definition found in root scope.");
4904 case NODE_TYPEALIAS
:
4905 ret
= visit_typealias(ctx
, root_decl_node
->u
.typealias
.target
,
4906 root_decl_node
->u
.typealias
.alias
);
4908 _BT_LOGE_NODE(root_decl_node
,
4909 "Cannot add type alias found in root scope.");
4913 case NODE_TYPE_SPECIFIER_LIST
:
4915 _BT_CTF_FIELD_TYPE_INIT(decl
);
4918 * Just add the type specifier to the root
4919 * declaration scope. Put local reference.
4921 ret
= visit_type_specifier_list(ctx
, root_decl_node
, &decl
);
4923 _BT_LOGE_NODE(root_decl_node
,
4924 "Cannot visit root scope's field type: "
4934 _BT_LOGE_NODE(root_decl_node
,
4935 "Unexpected node type: node-type=%d",
4936 root_decl_node
->type
);
4946 int set_trace_name(struct ctx
*ctx
)
4950 struct bt_value
*value
= NULL
;
4952 assert(bt_ctf_trace_get_stream_class_count(ctx
->trace
) == 0);
4953 name
= g_string_new(NULL
);
4955 BT_LOGE_STR("Failed to allocate a GString.");
4961 * Check if we have a trace environment string value named `hostname`.
4962 * If so, use it as the trace name's prefix.
4964 value
= bt_ctf_trace_get_environment_field_value_by_name(ctx
->trace
,
4966 if (bt_value_is_string(value
)) {
4967 const char *hostname
;
4969 ret
= bt_value_string_get(value
, &hostname
);
4971 g_string_append(name
, hostname
);
4973 if (ctx
->trace_name_suffix
) {
4974 g_string_append_c(name
, G_DIR_SEPARATOR
);
4978 if (ctx
->trace_name_suffix
) {
4979 g_string_append(name
, ctx
->trace_name_suffix
);
4982 ret
= bt_ctf_trace_set_name(ctx
->trace
, name
->str
);
4984 BT_LOGE("Cannot set trace's name: name=\"%s\"", name
->str
);
4997 g_string_free(name
, TRUE
);
5004 int move_ctx_stream_classes_to_trace(struct ctx
*ctx
)
5007 GHashTableIter iter
;
5008 gpointer key
, stream_class
;
5010 if (g_hash_table_size(ctx
->stream_classes
) > 0 &&
5011 bt_ctf_trace_get_stream_class_count(ctx
->trace
) == 0) {
5013 * We're about to add the first stream class to the
5014 * trace. This will freeze the trace, and after this
5015 * we cannot set the name anymore. At this point,
5016 * set the trace name.
5018 ret
= set_trace_name(ctx
);
5020 BT_LOGE_STR("Cannot set trace's name.");
5025 g_hash_table_iter_init(&iter
, ctx
->stream_classes
);
5027 while (g_hash_table_iter_next(&iter
, &key
, &stream_class
)) {
5028 ret
= bt_ctf_trace_add_stream_class(ctx
->trace
,
5031 int64_t id
= bt_ctf_stream_class_get_id(stream_class
);
5032 BT_LOGE("Cannot add stream class to trace: id=%" PRId64
,
5038 g_hash_table_remove_all(ctx
->stream_classes
);
5045 struct ctf_visitor_generate_ir
*ctf_visitor_generate_ir_create(
5046 const struct ctf_metadata_decoder_config
*decoder_config
,
5050 struct ctx
*ctx
= NULL
;
5051 struct bt_ctf_trace
*trace
;
5053 trace
= bt_ctf_trace_create();
5055 BT_LOGE_STR("Cannot create empty trace.");
5059 /* Set packet header to NULL to override the default one */
5060 ret
= bt_ctf_trace_set_packet_header_type(trace
, NULL
);
5062 BT_LOGE_STR("Cannot reset initial trace's packet header field type.");
5066 /* Create visitor's context */
5067 ctx
= ctx_create(trace
, decoder_config
, name
);
5069 BT_LOGE_STR("Cannot create visitor's context.");
5082 return (void *) ctx
;
5086 void ctf_visitor_generate_ir_destroy(struct ctf_visitor_generate_ir
*visitor
)
5088 ctx_destroy((void *) visitor
);
5092 struct bt_ctf_trace
*ctf_visitor_generate_ir_get_trace(
5093 struct ctf_visitor_generate_ir
*visitor
)
5095 struct ctx
*ctx
= (void *) visitor
;
5099 return bt_get(ctx
->trace
);
5103 int ctf_visitor_generate_ir_visit_node(struct ctf_visitor_generate_ir
*visitor
,
5104 struct ctf_node
*node
)
5107 struct ctx
*ctx
= (void *) visitor
;
5109 BT_LOGI_STR("Visiting metadata's AST to generate CTF IR objects.");
5111 switch (node
->type
) {
5114 struct ctf_node
*iter
;
5115 int got_trace_decl
= FALSE
;
5118 * The first thing we need is the native byte order of
5119 * the trace block, because early type aliases can have
5120 * a `byte_order` attribute set to `native`. If we don't
5121 * have the native byte order yet, and we don't have any
5122 * trace block yet, then fail with EINCOMPLETE.
5124 if (ctx
->trace_bo
== BT_CTF_BYTE_ORDER_NATIVE
) {
5125 bt_list_for_each_entry(iter
, &node
->u
.root
.trace
, siblings
) {
5126 if (got_trace_decl
) {
5128 "Duplicate trace (`trace` block).");
5133 ret
= set_trace_byte_order(ctx
, iter
);
5136 "Cannot set trace's native byte order: "
5141 got_trace_decl
= TRUE
;
5144 if (!got_trace_decl
) {
5145 BT_LOGD_STR("Incomplete AST: need trace (`trace` block).");
5151 assert(ctx
->trace_bo
== BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
||
5152 ctx
->trace_bo
== BT_CTF_BYTE_ORDER_BIG_ENDIAN
);
5153 assert(ctx
->current_scope
&&
5154 ctx
->current_scope
->parent_scope
== NULL
);
5157 bt_list_for_each_entry(iter
, &node
->u
.root
.env
, siblings
) {
5158 ret
= visit_env(ctx
, iter
);
5161 "Cannot visit trace's environment (`env` block) entry: "
5167 assert(ctx
->current_scope
&&
5168 ctx
->current_scope
->parent_scope
== NULL
);
5171 * Visit clock blocks.
5173 bt_list_for_each_entry(iter
, &node
->u
.root
.clock
, siblings
) {
5174 ret
= visit_clock_decl(ctx
, iter
);
5177 "Cannot visit clock class: ret=%d",
5183 assert(ctx
->current_scope
&&
5184 ctx
->current_scope
->parent_scope
== NULL
);
5187 * Visit root declarations next, as they can be used by any
5190 bt_list_for_each_entry(iter
, &node
->u
.root
.declaration_list
,
5192 ret
= visit_root_decl(ctx
, iter
);
5195 "Cannot visit root entry: ret=%d",
5201 assert(ctx
->current_scope
&&
5202 ctx
->current_scope
->parent_scope
== NULL
);
5204 /* Callsite blocks are not supported */
5205 bt_list_for_each_entry(iter
, &node
->u
.root
.callsite
, siblings
) {
5207 "\"callsite\" blocks are not supported as of this version.");
5210 assert(ctx
->current_scope
&&
5211 ctx
->current_scope
->parent_scope
== NULL
);
5214 bt_list_for_each_entry(iter
, &node
->u
.root
.trace
, siblings
) {
5215 ret
= visit_trace_decl(ctx
, iter
);
5218 "Cannot visit trace (`trace` block): "
5224 assert(ctx
->current_scope
&&
5225 ctx
->current_scope
->parent_scope
== NULL
);
5228 bt_list_for_each_entry(iter
, &node
->u
.root
.stream
, siblings
) {
5229 ret
= visit_stream_decl(ctx
, iter
);
5232 "Cannot visit stream class: ret=%d",
5238 assert(ctx
->current_scope
&&
5239 ctx
->current_scope
->parent_scope
== NULL
);
5242 bt_list_for_each_entry(iter
, &node
->u
.root
.event
, siblings
) {
5243 ret
= visit_event_decl(ctx
, iter
);
5246 "Cannot visit event class: ret=%d",
5252 assert(ctx
->current_scope
&&
5253 ctx
->current_scope
->parent_scope
== NULL
);
5258 "Unexpected node type: node-type=%d",
5264 /* Move decoded stream classes to trace, if any */
5265 ret
= move_ctx_stream_classes_to_trace(ctx
);
5267 BT_LOGE("Cannot move stream classes to trace: ret=%d", ret
);