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 * Creates a new declaration scope.
232 * @param par_scope Parent scope (NULL if creating a root scope)
233 * @returns New declaration scope, or NULL on error
236 struct ctx_decl_scope
*ctx_decl_scope_create(struct ctx_decl_scope
*par_scope
)
238 struct ctx_decl_scope
*scope
;
240 scope
= g_new(struct ctx_decl_scope
, 1);
242 BT_LOGE_STR("Failed to allocate one declaration scope.");
246 scope
->decl_map
= g_hash_table_new_full(g_direct_hash
, g_direct_equal
,
247 NULL
, (GDestroyNotify
) bt_ctf_field_type_put
);
248 scope
->parent_scope
= par_scope
;
255 * Destroys a declaration scope.
257 * This function does not destroy the parent scope.
259 * @param scope Scope to destroy
262 void ctx_decl_scope_destroy(struct ctx_decl_scope
*scope
)
268 g_hash_table_destroy(scope
->decl_map
);
276 * Returns the GQuark of a prefixed alias.
278 * @param prefix Prefix character
280 * @returns Associated GQuark, or 0 on error
283 GQuark
get_prefixed_named_quark(char prefix
, const char *name
)
289 /* Prefix character + original string + '\0' */
290 char *prname
= g_new(char, strlen(name
) + 2);
292 BT_LOGE_STR("Failed to allocate a string.");
296 sprintf(prname
, "%c%s", prefix
, name
);
297 qname
= g_quark_from_string(prname
);
305 * Looks up a prefixed type alias within a declaration scope.
307 * @param scope Declaration scope
308 * @param prefix Prefix character
309 * @param name Alias name
310 * @param level Number of levels to dig (-1 means infinite)
311 * @returns Declaration, or NULL if not found
314 struct bt_ctf_field_type
*ctx_decl_scope_lookup_prefix_alias(
315 struct ctx_decl_scope
*scope
, char prefix
,
316 const char *name
, int levels
)
320 _BT_CTF_FIELD_TYPE_INIT(decl
);
321 struct ctx_decl_scope
*cur_scope
= scope
;
325 qname
= get_prefixed_named_quark(prefix
, name
);
334 while (cur_scope
&& cur_levels
< levels
) {
335 decl
= g_hash_table_lookup(cur_scope
->decl_map
,
336 (gconstpointer
) GUINT_TO_POINTER(qname
));
338 /* Caller's reference */
343 cur_scope
= cur_scope
->parent_scope
;
354 * Looks up a type alias within a declaration scope.
356 * @param scope Declaration scope
357 * @param name Alias name
358 * @param level Number of levels to dig (-1 means infinite)
359 * @returns Declaration, or NULL if not found
362 struct bt_ctf_field_type
*ctx_decl_scope_lookup_alias(
363 struct ctx_decl_scope
*scope
, const char *name
, int levels
)
365 return ctx_decl_scope_lookup_prefix_alias(scope
, _PREFIX_ALIAS
,
370 * Looks up an enumeration within a declaration scope.
372 * @param scope Declaration scope
373 * @param name Enumeration name
374 * @param level Number of levels to dig (-1 means infinite)
375 * @returns Declaration, or NULL if not found
378 struct bt_ctf_field_type
*ctx_decl_scope_lookup_enum(
379 struct ctx_decl_scope
*scope
, const char *name
, int levels
)
381 return ctx_decl_scope_lookup_prefix_alias(scope
, _PREFIX_ENUM
,
386 * Looks up a structure within a declaration scope.
388 * @param scope Declaration scope
389 * @param name Structure name
390 * @param level Number of levels to dig (-1 means infinite)
391 * @returns Declaration, or NULL if not found
394 struct bt_ctf_field_type
*ctx_decl_scope_lookup_struct(
395 struct ctx_decl_scope
*scope
, const char *name
, int levels
)
397 return ctx_decl_scope_lookup_prefix_alias(scope
, _PREFIX_STRUCT
,
402 * Looks up a variant within a declaration scope.
404 * @param scope Declaration scope
405 * @param name Variant name
406 * @param level Number of levels to dig (-1 means infinite)
407 * @returns Declaration, or NULL if not found
410 struct bt_ctf_field_type
*ctx_decl_scope_lookup_variant(
411 struct ctx_decl_scope
*scope
, const char *name
, int levels
)
413 return ctx_decl_scope_lookup_prefix_alias(scope
, _PREFIX_VARIANT
,
418 * Registers a prefixed type alias within a declaration scope.
420 * @param scope Declaration scope
421 * @param prefix Prefix character
422 * @param name Alias name (non-NULL)
423 * @param decl Declaration to register
424 * @returns 0 if registration went okay, negative value otherwise
427 int ctx_decl_scope_register_prefix_alias(struct ctx_decl_scope
*scope
,
428 char prefix
, const char *name
, struct bt_ctf_field_type
*decl
)
432 _BT_CTF_FIELD_TYPE_INIT(edecl
);
437 qname
= get_prefixed_named_quark(prefix
, name
);
443 /* Make sure alias does not exist in local scope */
444 edecl
= ctx_decl_scope_lookup_prefix_alias(scope
, prefix
, name
, 1);
451 g_hash_table_insert(scope
->decl_map
,
452 GUINT_TO_POINTER(qname
), decl
);
454 /* Hash table's reference */
464 * Registers a type alias within a declaration scope.
466 * @param scope Declaration scope
467 * @param name Alias name (non-NULL)
468 * @param decl Declaration to register
469 * @returns 0 if registration went okay, negative value otherwise
472 int ctx_decl_scope_register_alias(struct ctx_decl_scope
*scope
,
473 const char *name
, struct bt_ctf_field_type
*decl
)
475 return ctx_decl_scope_register_prefix_alias(scope
, _PREFIX_ALIAS
,
480 * Registers an enumeration declaration within a declaration scope.
482 * @param scope Declaration scope
483 * @param name Enumeration name (non-NULL)
484 * @param decl Enumeration declaration to register
485 * @returns 0 if registration went okay, negative value otherwise
488 int ctx_decl_scope_register_enum(struct ctx_decl_scope
*scope
,
489 const char *name
, struct bt_ctf_field_type
*decl
)
491 return ctx_decl_scope_register_prefix_alias(scope
, _PREFIX_ENUM
,
496 * Registers a structure declaration within a declaration scope.
498 * @param scope Declaration scope
499 * @param name Structure name (non-NULL)
500 * @param decl Structure declaration to register
501 * @returns 0 if registration went okay, negative value otherwise
504 int ctx_decl_scope_register_struct(struct ctx_decl_scope
*scope
,
505 const char *name
, struct bt_ctf_field_type
*decl
)
507 return ctx_decl_scope_register_prefix_alias(scope
, _PREFIX_STRUCT
,
512 * Registers a variant declaration within a declaration scope.
514 * @param scope Declaration scope
515 * @param name Variant name (non-NULL)
516 * @param decl Variant declaration to register
517 * @returns 0 if registration went okay, negative value otherwise
520 int ctx_decl_scope_register_variant(struct ctx_decl_scope
*scope
,
521 const char *name
, struct bt_ctf_field_type
*decl
)
523 return ctx_decl_scope_register_prefix_alias(scope
, _PREFIX_VARIANT
,
528 * Destroys a visitor context.
530 * @param ctx Visitor context to destroy
533 void ctx_destroy(struct ctx
*ctx
)
535 struct ctx_decl_scope
*scope
;
537 * Destroy all scopes, from current one to the root scope.
544 scope
= ctx
->current_scope
;
547 struct ctx_decl_scope
*parent_scope
= scope
->parent_scope
;
549 ctx_decl_scope_destroy(scope
);
550 scope
= parent_scope
;
555 if (ctx
->stream_classes
) {
556 g_hash_table_destroy(ctx
->stream_classes
);
559 free(ctx
->trace_name_suffix
);
567 * Creates a new visitor context.
569 * @param trace Associated trace
570 * @returns New visitor context, or NULL on error
573 struct ctx
*ctx_create(struct bt_ctf_trace
*trace
,
574 const struct ctf_metadata_decoder_config
*decoder_config
,
575 const char *trace_name_suffix
)
577 struct ctx
*ctx
= NULL
;
578 struct ctx_decl_scope
*scope
= NULL
;
580 assert(decoder_config
);
582 ctx
= g_new0(struct ctx
, 1);
584 BT_LOGE_STR("Failed to allocate one visitor context.");
588 /* Root declaration scope */
589 scope
= ctx_decl_scope_create(NULL
);
591 BT_LOGE_STR("Cannot create declaration scope.");
595 ctx
->stream_classes
= g_hash_table_new_full(g_direct_hash
,
596 g_direct_equal
, NULL
, (GDestroyNotify
) bt_put
);
597 if (!ctx
->stream_classes
) {
598 BT_LOGE_STR("Failed to allocate a GHashTable.");
602 if (trace_name_suffix
) {
603 ctx
->trace_name_suffix
= strdup(trace_name_suffix
);
604 if (!ctx
->trace_name_suffix
) {
605 BT_LOGE_STR("Failed to copy string.");
611 ctx
->current_scope
= scope
;
613 ctx
->trace_bo
= BT_CTF_BYTE_ORDER_NATIVE
;
614 ctx
->decoder_config
= *decoder_config
;
619 ctx_decl_scope_destroy(scope
);
624 * Pushes a new declaration scope on top of a visitor context's
625 * declaration scope stack.
627 * @param ctx Visitor context
628 * @returns 0 on success, or a negative value on error
631 int ctx_push_scope(struct ctx
*ctx
)
634 struct ctx_decl_scope
*new_scope
;
637 new_scope
= ctx_decl_scope_create(ctx
->current_scope
);
639 BT_LOGE_STR("Cannot create declaration scope.");
644 ctx
->current_scope
= new_scope
;
651 void ctx_pop_scope(struct ctx
*ctx
)
653 struct ctx_decl_scope
*parent_scope
= NULL
;
657 if (!ctx
->current_scope
) {
661 parent_scope
= ctx
->current_scope
->parent_scope
;
662 ctx_decl_scope_destroy(ctx
->current_scope
);
663 ctx
->current_scope
= parent_scope
;
670 int visit_type_specifier_list(struct ctx
*ctx
, struct ctf_node
*ts_list
,
671 struct bt_ctf_field_type
**decl
);
674 int is_unary_string(struct bt_list_head
*head
)
677 struct ctf_node
*node
;
679 bt_list_for_each_entry(node
, head
, siblings
) {
680 if (node
->type
!= NODE_UNARY_EXPRESSION
) {
684 if (node
->u
.unary_expression
.type
!= UNARY_STRING
) {
693 char *concatenate_unary_strings(struct bt_list_head
*head
)
697 struct ctf_node
*node
;
699 str
= g_string_new(NULL
);
702 bt_list_for_each_entry(node
, head
, siblings
) {
706 node
->type
!= NODE_UNARY_EXPRESSION
||
707 node
->u
.unary_expression
.type
!= UNARY_STRING
||
710 node
->u
.unary_expression
.link
!=
718 switch (node
->u
.unary_expression
.link
) {
720 g_string_append(str
, ".");
722 case UNARY_ARROWLINK
:
723 g_string_append(str
, "->");
725 case UNARY_DOTDOTDOT
:
726 g_string_append(str
, "...");
732 src_string
= node
->u
.unary_expression
.u
.string
;
733 g_string_append(str
, src_string
);
737 /* Destroys the container, returns the underlying string */
738 return g_string_free(str
, FALSE
);
741 /* This always returns NULL */
742 return g_string_free(str
, TRUE
);
746 const char *get_map_clock_name_value(struct bt_list_head
*head
)
749 struct ctf_node
*node
;
750 const char *name
= NULL
;
752 bt_list_for_each_entry(node
, head
, siblings
) {
754 int uexpr_type
= node
->u
.unary_expression
.type
;
755 int uexpr_link
= node
->u
.unary_expression
.link
;
756 int cond
= node
->type
!= NODE_UNARY_EXPRESSION
||
757 uexpr_type
!= UNARY_STRING
||
758 !((uexpr_link
!= UNARY_LINK_UNKNOWN
) ^ (i
== 0));
763 /* Needs to be chained with . */
764 switch (node
->u
.unary_expression
.link
) {
767 case UNARY_ARROWLINK
:
768 case UNARY_DOTDOTDOT
:
774 src_string
= node
->u
.unary_expression
.u
.string
;
778 if (strcmp("clock", src_string
)) {
786 if (strcmp("value", src_string
)) {
791 /* Extra identifier, unknown */
805 int is_unary_unsigned(struct bt_list_head
*head
)
808 struct ctf_node
*node
;
810 bt_list_for_each_entry(node
, head
, siblings
) {
811 if (node
->type
!= NODE_UNARY_EXPRESSION
) {
815 if (node
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
824 int get_unary_unsigned(struct bt_list_head
*head
, uint64_t *value
)
828 struct ctf_node
*node
;
830 if (bt_list_empty(head
)) {
835 bt_list_for_each_entry(node
, head
, siblings
) {
836 int uexpr_type
= node
->u
.unary_expression
.type
;
837 int uexpr_link
= node
->u
.unary_expression
.link
;
838 int cond
= node
->type
!= NODE_UNARY_EXPRESSION
||
839 uexpr_type
!= UNARY_UNSIGNED_CONSTANT
||
840 uexpr_link
!= UNARY_LINK_UNKNOWN
|| i
!= 0;
842 _BT_LOGE_NODE(node
, "Invalid constant unsigned integer.");
847 *value
= node
->u
.unary_expression
.u
.unsigned_constant
;
856 int is_unary_signed(struct bt_list_head
*head
)
859 struct ctf_node
*node
;
861 bt_list_for_each_entry(node
, head
, siblings
) {
862 if (node
->type
!= NODE_UNARY_EXPRESSION
) {
866 if (node
->u
.unary_expression
.type
!= UNARY_SIGNED_CONSTANT
) {
875 int get_unary_signed(struct bt_list_head
*head
, int64_t *value
)
879 struct ctf_node
*node
;
881 bt_list_for_each_entry(node
, head
, siblings
) {
882 int uexpr_type
= node
->u
.unary_expression
.type
;
883 int uexpr_link
= node
->u
.unary_expression
.link
;
884 int cond
= node
->type
!= NODE_UNARY_EXPRESSION
||
885 (uexpr_type
!= UNARY_UNSIGNED_CONSTANT
) ||
886 (uexpr_type
!= UNARY_UNSIGNED_CONSTANT
&&
887 uexpr_type
!= UNARY_SIGNED_CONSTANT
) ||
888 uexpr_link
!= UNARY_LINK_UNKNOWN
|| i
!= 0;
894 switch (node
->u
.unary_expression
.type
) {
895 case UNARY_UNSIGNED_CONSTANT
:
897 node
->u
.unary_expression
.u
.unsigned_constant
;
899 case UNARY_SIGNED_CONSTANT
:
900 *value
= node
->u
.unary_expression
.u
.signed_constant
;
915 int get_unary_uuid(struct bt_list_head
*head
, unsigned char *uuid
)
919 struct ctf_node
*node
;
921 bt_list_for_each_entry(node
, head
, siblings
) {
922 int uexpr_type
= node
->u
.unary_expression
.type
;
923 int uexpr_link
= node
->u
.unary_expression
.link
;
924 const char *src_string
;
926 if (node
->type
!= NODE_UNARY_EXPRESSION
||
927 uexpr_type
!= UNARY_STRING
||
928 uexpr_link
!= UNARY_LINK_UNKNOWN
||
934 src_string
= node
->u
.unary_expression
.u
.string
;
935 ret
= bt_uuid_parse(src_string
, uuid
);
938 "Cannot parse UUID: uuid=\"%s\"", src_string
);
948 int get_boolean(struct ctf_node
*unary_expr
)
952 if (unary_expr
->type
!= NODE_UNARY_EXPRESSION
) {
953 _BT_LOGE_NODE(unary_expr
,
954 "Expecting unary expression: node-type=%d",
960 switch (unary_expr
->u
.unary_expression
.type
) {
961 case UNARY_UNSIGNED_CONSTANT
:
962 ret
= (unary_expr
->u
.unary_expression
.u
.unsigned_constant
!= 0);
964 case UNARY_SIGNED_CONSTANT
:
965 ret
= (unary_expr
->u
.unary_expression
.u
.signed_constant
!= 0);
969 const char *str
= unary_expr
->u
.unary_expression
.u
.string
;
971 if (!strcmp(str
, "true") || !strcmp(str
, "TRUE")) {
973 } else if (!strcmp(str
, "false") || !strcmp(str
, "FALSE")) {
976 _BT_LOGE_NODE(unary_expr
,
977 "Unexpected boolean value: value=\"%s\"", str
);
984 _BT_LOGE_NODE(unary_expr
,
985 "Unexpected unary expression type: node-type=%d",
986 unary_expr
->u
.unary_expression
.type
);
996 enum bt_ctf_byte_order
byte_order_from_unary_expr(struct ctf_node
*unary_expr
)
999 enum bt_ctf_byte_order bo
= BT_CTF_BYTE_ORDER_UNKNOWN
;
1001 if (unary_expr
->u
.unary_expression
.type
!= UNARY_STRING
) {
1002 _BT_LOGE_NODE(unary_expr
,
1003 "\"byte_order\" attribute: expecting `be`, `le`, `network`, or `native`.");
1007 str
= unary_expr
->u
.unary_expression
.u
.string
;
1009 if (!strcmp(str
, "be") || !strcmp(str
, "network")) {
1010 bo
= BT_CTF_BYTE_ORDER_BIG_ENDIAN
;
1011 } else if (!strcmp(str
, "le")) {
1012 bo
= BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
;
1013 } else if (!strcmp(str
, "native")) {
1014 bo
= BT_CTF_BYTE_ORDER_NATIVE
;
1016 _BT_LOGE_NODE(unary_expr
,
1017 "Unexpected \"byte_order\" attribute value: "
1018 "expecting `be`, `le`, `network`, or `native`: value=\"%s\"",
1028 enum bt_ctf_byte_order
get_real_byte_order(struct ctx
*ctx
,
1029 struct ctf_node
*uexpr
)
1031 enum bt_ctf_byte_order bo
= byte_order_from_unary_expr(uexpr
);
1033 if (bo
== BT_CTF_BYTE_ORDER_NATIVE
) {
1034 bo
= bt_ctf_trace_get_native_byte_order(ctx
->trace
);
1041 int is_align_valid(uint64_t align
)
1043 return (align
!= 0) && !(align
& (align
- 1));
1047 int get_type_specifier_name(struct ctx
*ctx
, struct ctf_node
*type_specifier
,
1052 if (type_specifier
->type
!= NODE_TYPE_SPECIFIER
) {
1053 _BT_LOGE_NODE(type_specifier
,
1054 "Unexpected node type: node-type=%d",
1055 type_specifier
->type
);
1060 switch (type_specifier
->u
.type_specifier
.type
) {
1062 g_string_append(str
, "void");
1065 g_string_append(str
, "char");
1067 case TYPESPEC_SHORT
:
1068 g_string_append(str
, "short");
1071 g_string_append(str
, "int");
1074 g_string_append(str
, "long");
1076 case TYPESPEC_FLOAT
:
1077 g_string_append(str
, "float");
1079 case TYPESPEC_DOUBLE
:
1080 g_string_append(str
, "double");
1082 case TYPESPEC_SIGNED
:
1083 g_string_append(str
, "signed");
1085 case TYPESPEC_UNSIGNED
:
1086 g_string_append(str
, "unsigned");
1089 g_string_append(str
, "bool");
1091 case TYPESPEC_COMPLEX
:
1092 g_string_append(str
, "_Complex");
1094 case TYPESPEC_IMAGINARY
:
1095 g_string_append(str
, "_Imaginary");
1097 case TYPESPEC_CONST
:
1098 g_string_append(str
, "const");
1100 case TYPESPEC_ID_TYPE
:
1101 if (type_specifier
->u
.type_specifier
.id_type
) {
1102 g_string_append(str
,
1103 type_specifier
->u
.type_specifier
.id_type
);
1106 case TYPESPEC_STRUCT
:
1108 struct ctf_node
*node
= type_specifier
->u
.type_specifier
.node
;
1110 if (!node
->u
._struct
.name
) {
1111 _BT_LOGE_NODE(node
, "Unexpected empty structure field type name.");
1116 g_string_append(str
, "struct ");
1117 g_string_append(str
, node
->u
._struct
.name
);
1120 case TYPESPEC_VARIANT
:
1122 struct ctf_node
*node
= type_specifier
->u
.type_specifier
.node
;
1124 if (!node
->u
.variant
.name
) {
1125 _BT_LOGE_NODE(node
, "Unexpected empty variant field type name.");
1130 g_string_append(str
, "variant ");
1131 g_string_append(str
, node
->u
.variant
.name
);
1136 struct ctf_node
*node
= type_specifier
->u
.type_specifier
.node
;
1138 if (!node
->u
._enum
.enum_id
) {
1140 "Unexpected empty enumeration field type (`enum`) name.");
1145 g_string_append(str
, "enum ");
1146 g_string_append(str
, node
->u
._enum
.enum_id
);
1149 case TYPESPEC_FLOATING_POINT
:
1150 case TYPESPEC_INTEGER
:
1151 case TYPESPEC_STRING
:
1153 _BT_LOGE_NODE(type_specifier
->u
.type_specifier
.node
,
1154 "Unexpected type specifier type: %d",
1155 type_specifier
->u
.type_specifier
.type
);
1165 int get_type_specifier_list_name(struct ctx
*ctx
,
1166 struct ctf_node
*type_specifier_list
, GString
*str
)
1169 struct ctf_node
*iter
;
1170 int alias_item_nr
= 0;
1171 struct bt_list_head
*head
=
1172 &type_specifier_list
->u
.type_specifier_list
.head
;
1174 bt_list_for_each_entry(iter
, head
, siblings
) {
1175 if (alias_item_nr
!= 0) {
1176 g_string_append(str
, " ");
1180 ret
= get_type_specifier_name(ctx
, iter
, str
);
1191 GQuark
create_typealias_identifier(struct ctx
*ctx
,
1192 struct ctf_node
*type_specifier_list
,
1193 struct ctf_node
*node_type_declarator
)
1199 struct ctf_node
*iter
;
1200 struct bt_list_head
*pointers
=
1201 &node_type_declarator
->u
.type_declarator
.pointers
;
1203 str
= g_string_new("");
1204 ret
= get_type_specifier_list_name(ctx
, type_specifier_list
, str
);
1206 g_string_free(str
, TRUE
);
1210 bt_list_for_each_entry(iter
, pointers
, siblings
) {
1211 g_string_append(str
, " *");
1213 if (iter
->u
.pointer
.const_qualifier
) {
1214 g_string_append(str
, " const");
1218 str_c
= g_string_free(str
, FALSE
);
1219 qalias
= g_quark_from_string(str_c
);
1227 int visit_type_declarator(struct ctx
*ctx
, struct ctf_node
*type_specifier_list
,
1228 GQuark
*field_name
, struct ctf_node
*node_type_declarator
,
1229 struct bt_ctf_field_type
**field_decl
,
1230 struct bt_ctf_field_type
*nested_decl
)
1233 * During this whole function, nested_decl is always OURS,
1234 * whereas field_decl is an output which we create, but
1235 * belongs to the caller (it is moved).
1241 /* Validate type declarator node */
1242 if (node_type_declarator
) {
1243 if (node_type_declarator
->u
.type_declarator
.type
==
1245 _BT_LOGE_NODE(node_type_declarator
,
1246 "Unexpected type declarator type: type=%d",
1247 node_type_declarator
->u
.type_declarator
.type
);
1252 /* TODO: GCC bitfields not supported yet */
1253 if (node_type_declarator
->u
.type_declarator
.bitfield_len
!=
1255 _BT_LOGE_NODE(node_type_declarator
,
1256 "GCC bitfields are not supported as of this version.");
1262 /* Find the right nested declaration if not provided */
1264 struct bt_list_head
*pointers
=
1265 &node_type_declarator
->u
.type_declarator
.pointers
;
1267 if (node_type_declarator
&& !bt_list_empty(pointers
)) {
1269 _BT_CTF_FIELD_TYPE_INIT(nested_decl_copy
);
1272 * If we have a pointer declarator, it HAS to
1273 * be present in the typealiases (else fail).
1275 qalias
= create_typealias_identifier(ctx
,
1276 type_specifier_list
, node_type_declarator
);
1278 ctx_decl_scope_lookup_alias(ctx
->current_scope
,
1279 g_quark_to_string(qalias
), -1);
1281 _BT_LOGE_NODE(node_type_declarator
,
1282 "Cannot find type alias: name=\"%s\"",
1283 g_quark_to_string(qalias
));
1288 /* Make a copy of it */
1289 nested_decl_copy
= bt_ctf_field_type_copy(nested_decl
);
1290 BT_PUT(nested_decl
);
1291 if (!nested_decl_copy
) {
1292 _BT_LOGE_NODE(node_type_declarator
,
1293 "Cannot copy nested field type.");
1298 BT_MOVE(nested_decl
, nested_decl_copy
);
1300 /* Force integer's base to 16 since it's a pointer */
1301 if (bt_ctf_field_type_is_integer(nested_decl
)) {
1302 ret
= bt_ctf_field_type_integer_set_base(
1304 BT_CTF_INTEGER_BASE_HEXADECIMAL
);
1308 ret
= visit_type_specifier_list(ctx
,
1309 type_specifier_list
, &nested_decl
);
1311 assert(!nested_decl
);
1317 assert(nested_decl
);
1319 if (!node_type_declarator
) {
1320 BT_MOVE(*field_decl
, nested_decl
);
1324 if (node_type_declarator
->u
.type_declarator
.type
== TYPEDEC_ID
) {
1325 if (node_type_declarator
->u
.type_declarator
.u
.id
) {
1327 node_type_declarator
->u
.type_declarator
.u
.id
;
1329 *field_name
= g_quark_from_string(id
);
1334 BT_MOVE(*field_decl
, nested_decl
);
1337 struct ctf_node
*first
;
1338 _BT_CTF_FIELD_TYPE_INIT(decl
);
1339 _BT_CTF_FIELD_TYPE_INIT(outer_field_decl
);
1340 struct bt_list_head
*length
=
1341 &node_type_declarator
->
1342 u
.type_declarator
.u
.nested
.length
;
1344 /* Create array/sequence, pass nested_decl as child */
1345 if (bt_list_empty(length
)) {
1346 _BT_LOGE_NODE(node_type_declarator
,
1347 "Expecting length field reference or value.");
1352 first
= _BT_LIST_FIRST_ENTRY(length
, struct ctf_node
, siblings
);
1353 if (first
->type
!= NODE_UNARY_EXPRESSION
) {
1354 _BT_LOGE_NODE(first
,
1355 "Unexpected node type: node-type=%d",
1361 switch (first
->u
.unary_expression
.type
) {
1362 case UNARY_UNSIGNED_CONSTANT
:
1365 _BT_CTF_FIELD_TYPE_INIT(array_decl
);
1367 len
= first
->u
.unary_expression
.u
.unsigned_constant
;
1368 array_decl
= bt_ctf_field_type_array_create(nested_decl
,
1370 BT_PUT(nested_decl
);
1372 _BT_LOGE_NODE(first
,
1373 "Cannot create array field type.");
1378 BT_MOVE(decl
, array_decl
);
1383 /* Lookup unsigned integer definition, create seq. */
1384 _BT_CTF_FIELD_TYPE_INIT(seq_decl
);
1385 char *length_name
= concatenate_unary_strings(length
);
1388 _BT_LOGE_NODE(node_type_declarator
,
1389 "Cannot concatenate unary strings.");
1394 seq_decl
= bt_ctf_field_type_sequence_create(
1395 nested_decl
, length_name
);
1396 g_free(length_name
);
1397 BT_PUT(nested_decl
);
1399 _BT_LOGE_NODE(node_type_declarator
,
1400 "Cannot create sequence field type.");
1405 BT_MOVE(decl
, seq_decl
);
1413 assert(!nested_decl
);
1415 assert(!*field_decl
);
1418 * At this point, we found the next nested declaration.
1419 * We currently own this (and lost the ownership of
1420 * nested_decl in the meantime). Pass this next
1421 * nested declaration as the content of the outer
1422 * container, MOVING its ownership.
1424 ret
= visit_type_declarator(ctx
, type_specifier_list
,
1426 node_type_declarator
->
1427 u
.type_declarator
.u
.nested
.type_declarator
,
1428 &outer_field_decl
, decl
);
1431 assert(!outer_field_decl
);
1436 assert(outer_field_decl
);
1437 BT_MOVE(*field_decl
, outer_field_decl
);
1441 BT_PUT(nested_decl
);
1442 assert(*field_decl
);
1447 BT_PUT(nested_decl
);
1448 BT_PUT(*field_decl
);
1454 int visit_struct_decl_field(struct ctx
*ctx
,
1455 struct bt_ctf_field_type
*struct_decl
,
1456 struct ctf_node
*type_specifier_list
,
1457 struct bt_list_head
*type_declarators
)
1460 struct ctf_node
*iter
;
1461 _BT_CTF_FIELD_TYPE_INIT(field_decl
);
1463 bt_list_for_each_entry(iter
, type_declarators
, siblings
) {
1466 const char *field_name
;
1467 _BT_CTF_FIELD_TYPE_INIT(efield_decl
);
1469 ret
= visit_type_declarator(ctx
, type_specifier_list
,
1470 &qfield_name
, iter
, &field_decl
, NULL
);
1472 assert(!field_decl
);
1473 _BT_LOGE_NODE(type_specifier_list
,
1474 "Cannot visit type declarator: ret=%d", ret
);
1479 field_name
= g_quark_to_string(qfield_name
);
1481 /* Check if field with same name already exists */
1483 bt_ctf_field_type_structure_get_field_type_by_name(
1484 struct_decl
, field_name
);
1486 BT_PUT(efield_decl
);
1487 _BT_LOGE_NODE(type_specifier_list
,
1488 "Duplicate field in structure field type: "
1489 "field-name=\"%s\"", field_name
);
1494 /* Add field to structure */
1495 ret
= bt_ctf_field_type_structure_add_field(struct_decl
,
1496 field_decl
, field_name
);
1499 _BT_LOGE_NODE(type_specifier_list
,
1500 "Cannot add field to structure field type: "
1501 "field-name=\"%s\", ret=%d",
1502 g_quark_to_string(qfield_name
), ret
);
1516 int visit_variant_decl_field(struct ctx
*ctx
,
1517 struct bt_ctf_field_type
*variant_decl
,
1518 struct ctf_node
*type_specifier_list
,
1519 struct bt_list_head
*type_declarators
)
1522 struct ctf_node
*iter
;
1523 _BT_CTF_FIELD_TYPE_INIT(field_decl
);
1525 bt_list_for_each_entry(iter
, type_declarators
, siblings
) {
1528 const char *field_name
;
1529 _BT_CTF_FIELD_TYPE_INIT(efield_decl
);
1531 ret
= visit_type_declarator(ctx
, type_specifier_list
,
1532 &qfield_name
, iter
, &field_decl
, NULL
);
1534 assert(!field_decl
);
1535 _BT_LOGE_NODE(type_specifier_list
,
1536 "Cannot visit type declarator: ret=%d", ret
);
1541 field_name
= g_quark_to_string(qfield_name
);
1543 /* Check if field with same name already exists */
1545 bt_ctf_field_type_variant_get_field_type_by_name(
1546 variant_decl
, field_name
);
1548 BT_PUT(efield_decl
);
1549 _BT_LOGE_NODE(type_specifier_list
,
1550 "Duplicate field in variant field type: "
1551 "field-name=\"%s\"", field_name
);
1556 /* Add field to structure */
1557 ret
= bt_ctf_field_type_variant_add_field(variant_decl
,
1558 field_decl
, field_name
);
1561 _BT_LOGE_NODE(type_specifier_list
,
1562 "Cannot add field to variant field type: "
1563 "field-name=\"%s\", ret=%d",
1564 g_quark_to_string(qfield_name
), ret
);
1578 int visit_typedef(struct ctx
*ctx
, struct ctf_node
*type_specifier_list
,
1579 struct bt_list_head
*type_declarators
)
1583 struct ctf_node
*iter
;
1584 _BT_CTF_FIELD_TYPE_INIT(type_decl
);
1586 bt_list_for_each_entry(iter
, type_declarators
, siblings
) {
1587 ret
= visit_type_declarator(ctx
, type_specifier_list
,
1588 &qidentifier
, iter
, &type_decl
, NULL
);
1591 "Cannot visit type declarator: ret=%d", ret
);
1596 /* Do not allow typedef and typealias of untagged variants */
1597 if (bt_ctf_field_type_is_variant(type_decl
)) {
1598 if (bt_ctf_field_type_variant_get_tag_name(type_decl
)) {
1600 "Type definition of untagged variant field type is not allowed.");
1606 ret
= ctx_decl_scope_register_alias(ctx
->current_scope
,
1607 g_quark_to_string(qidentifier
), type_decl
);
1610 "Cannot register type definition: name=\"%s\"",
1611 g_quark_to_string(qidentifier
));
1623 int visit_typealias(struct ctx
*ctx
, struct ctf_node
*target
,
1624 struct ctf_node
*alias
)
1628 struct ctf_node
*node
;
1629 GQuark qdummy_field_name
;
1630 _BT_CTF_FIELD_TYPE_INIT(type_decl
);
1632 /* Create target type declaration */
1633 if (bt_list_empty(&target
->u
.typealias_target
.type_declarators
)) {
1636 node
= _BT_LIST_FIRST_ENTRY(
1637 &target
->u
.typealias_target
.type_declarators
,
1638 struct ctf_node
, siblings
);
1641 ret
= visit_type_declarator(ctx
,
1642 target
->u
.typealias_target
.type_specifier_list
,
1643 &qdummy_field_name
, node
, &type_decl
, NULL
);
1647 "Cannot visit type declarator: ret=%d", ret
);
1651 /* Do not allow typedef and typealias of untagged variants */
1652 if (bt_ctf_field_type_is_variant(type_decl
)) {
1653 if (bt_ctf_field_type_variant_get_tag_name(type_decl
)) {
1655 "Type definition of untagged variant field type is not allowed.");
1662 * The semantic validator does not check whether the target is
1663 * abstract or not (if it has an identifier). Check it here.
1665 if (qdummy_field_name
!= 0) {
1667 "Expecting empty identifier: id=\"%s\"",
1668 g_quark_to_string(qdummy_field_name
));
1673 /* Create alias identifier */
1674 node
= _BT_LIST_FIRST_ENTRY(&alias
->u
.typealias_alias
.type_declarators
,
1675 struct ctf_node
, siblings
);
1676 qalias
= create_typealias_identifier(ctx
,
1677 alias
->u
.typealias_alias
.type_specifier_list
, node
);
1678 ret
= ctx_decl_scope_register_alias(ctx
->current_scope
,
1679 g_quark_to_string(qalias
), type_decl
);
1682 "Cannot register type alias: name=\"%s\"",
1683 g_quark_to_string(qalias
));
1694 int visit_struct_decl_entry(struct ctx
*ctx
, struct ctf_node
*entry_node
,
1695 struct bt_ctf_field_type
*struct_decl
)
1699 switch (entry_node
->type
) {
1701 ret
= visit_typedef(ctx
,
1702 entry_node
->u
._typedef
.type_specifier_list
,
1703 &entry_node
->u
._typedef
.type_declarators
);
1705 _BT_LOGE_NODE(entry_node
,
1706 "Cannot add type definition found in structure field type: ret=%d",
1711 case NODE_TYPEALIAS
:
1712 ret
= visit_typealias(ctx
, entry_node
->u
.typealias
.target
,
1713 entry_node
->u
.typealias
.alias
);
1715 _BT_LOGE_NODE(entry_node
,
1716 "Cannot add type alias found in structure field type: ret=%d",
1721 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
1723 ret
= visit_struct_decl_field(ctx
, struct_decl
,
1724 entry_node
->u
.struct_or_variant_declaration
.
1725 type_specifier_list
,
1726 &entry_node
->u
.struct_or_variant_declaration
.
1733 _BT_LOGE_NODE(entry_node
,
1734 "Unexpected node type: node-type=%d", entry_node
->type
);
1744 int visit_variant_decl_entry(struct ctx
*ctx
, struct ctf_node
*entry_node
,
1745 struct bt_ctf_field_type
*variant_decl
)
1749 switch (entry_node
->type
) {
1751 ret
= visit_typedef(ctx
,
1752 entry_node
->u
._typedef
.type_specifier_list
,
1753 &entry_node
->u
._typedef
.type_declarators
);
1755 _BT_LOGE_NODE(entry_node
,
1756 "Cannot add type definition found in variant field type: ret=%d",
1761 case NODE_TYPEALIAS
:
1762 ret
= visit_typealias(ctx
, entry_node
->u
.typealias
.target
,
1763 entry_node
->u
.typealias
.alias
);
1765 _BT_LOGE_NODE(entry_node
,
1766 "Cannot add type alias found in variant field type: ret=%d",
1771 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
1773 ret
= visit_variant_decl_field(ctx
, variant_decl
,
1774 entry_node
->u
.struct_or_variant_declaration
.
1775 type_specifier_list
,
1776 &entry_node
->u
.struct_or_variant_declaration
.
1783 _BT_LOGE_NODE(entry_node
,
1784 "Unexpected node type: node-type=%d",
1795 int visit_struct_decl(struct ctx
*ctx
, const char *name
,
1796 struct bt_list_head
*decl_list
, int has_body
,
1797 struct bt_list_head
*min_align
,
1798 struct bt_ctf_field_type
**struct_decl
)
1802 *struct_decl
= NULL
;
1804 /* For named struct (without body), lookup in declaration scope */
1806 _BT_CTF_FIELD_TYPE_INIT(struct_decl_copy
);
1809 BT_LOGE_STR("Bodyless structure field type: missing name.");
1814 *struct_decl
= ctx_decl_scope_lookup_struct(ctx
->current_scope
,
1816 if (!*struct_decl
) {
1817 BT_LOGE("Cannot find structure field type: name=\"struct %s\"",
1823 /* Make a copy of it */
1824 struct_decl_copy
= bt_ctf_field_type_copy(*struct_decl
);
1825 if (!struct_decl_copy
) {
1826 BT_LOGE_STR("Cannot create copy of structure field type.");
1831 BT_MOVE(*struct_decl
, struct_decl_copy
);
1833 struct ctf_node
*entry_node
;
1834 uint64_t min_align_value
= 0;
1837 _BT_CTF_FIELD_TYPE_INIT(estruct_decl
);
1839 estruct_decl
= ctx_decl_scope_lookup_struct(
1840 ctx
->current_scope
, name
, 1);
1842 BT_PUT(estruct_decl
);
1843 BT_LOGE("Structure field type already declared in local scope: "
1844 "name=\"struct %s\"", name
);
1850 if (!bt_list_empty(min_align
)) {
1851 ret
= get_unary_unsigned(min_align
, &min_align_value
);
1853 BT_LOGE("Unexpected unary expression for structure field type's `align` attribute: "
1859 *struct_decl
= bt_ctf_field_type_structure_create();
1860 if (!*struct_decl
) {
1861 BT_LOGE_STR("Cannot create empty structure field type.");
1866 if (min_align_value
!= 0) {
1867 ret
= bt_ctf_field_type_set_alignment(*struct_decl
,
1870 BT_LOGE("Cannot set structure field type's alignment: "
1876 ret
= ctx_push_scope(ctx
);
1878 BT_LOGE_STR("Cannot push scope.");
1882 bt_list_for_each_entry(entry_node
, decl_list
, siblings
) {
1883 ret
= visit_struct_decl_entry(ctx
, entry_node
,
1886 _BT_LOGE_NODE(entry_node
,
1887 "Cannot visit structure field type entry: "
1897 ret
= ctx_decl_scope_register_struct(ctx
->current_scope
,
1898 name
, *struct_decl
);
1900 BT_LOGE("Cannot register structure field type in declaration scope: "
1901 "name=\"struct %s\", ret=%d", name
, ret
);
1910 BT_PUT(*struct_decl
);
1916 int visit_variant_decl(struct ctx
*ctx
, const char *name
,
1917 const char *tag
, struct bt_list_head
*decl_list
,
1918 int has_body
, struct bt_ctf_field_type
**variant_decl
)
1921 _BT_CTF_FIELD_TYPE_INIT(untagged_variant_decl
);
1923 *variant_decl
= NULL
;
1925 /* For named variant (without body), lookup in declaration scope */
1927 _BT_CTF_FIELD_TYPE_INIT(variant_decl_copy
);
1930 BT_LOGE_STR("Bodyless variant field type: missing name.");
1935 untagged_variant_decl
=
1936 ctx_decl_scope_lookup_variant(ctx
->current_scope
,
1938 if (!untagged_variant_decl
) {
1939 BT_LOGE("Cannot find variant field type: name=\"variant %s\"",
1945 /* Make a copy of it */
1946 variant_decl_copy
= bt_ctf_field_type_copy(
1947 untagged_variant_decl
);
1948 if (!variant_decl_copy
) {
1949 BT_LOGE_STR("Cannot create copy of variant field type.");
1954 BT_MOVE(untagged_variant_decl
, variant_decl_copy
);
1956 struct ctf_node
*entry_node
;
1959 struct bt_ctf_field_type
*evariant_decl
=
1960 ctx_decl_scope_lookup_struct(ctx
->current_scope
,
1963 if (evariant_decl
) {
1964 BT_PUT(evariant_decl
);
1965 BT_LOGE("Variant field type already declared in local scope: "
1966 "name=\"variant %s\"", name
);
1972 untagged_variant_decl
= bt_ctf_field_type_variant_create(NULL
,
1974 if (!untagged_variant_decl
) {
1975 BT_LOGE_STR("Cannot create empty variant field type.");
1980 ret
= ctx_push_scope(ctx
);
1982 BT_LOGE_STR("Cannot push scope.");
1986 bt_list_for_each_entry(entry_node
, decl_list
, siblings
) {
1987 ret
= visit_variant_decl_entry(ctx
, entry_node
,
1988 untagged_variant_decl
);
1990 _BT_LOGE_NODE(entry_node
,
1991 "Cannot visit variant field type entry: "
2001 ret
= ctx_decl_scope_register_variant(
2002 ctx
->current_scope
, name
,
2003 untagged_variant_decl
);
2005 BT_LOGE("Cannot register variant field type in declaration scope: "
2006 "name=\"variant %s\", ret=%d", name
, ret
);
2013 * If tagged, create tagged variant and return; otherwise
2014 * return untagged variant.
2017 BT_MOVE(*variant_decl
, untagged_variant_decl
);
2020 * At this point, we have a fresh untagged variant; nobody
2021 * else owns it. Set its tag now.
2023 ret
= bt_ctf_field_type_variant_set_tag_name(
2024 untagged_variant_decl
, tag
);
2026 BT_LOGE("Cannot set variant field type's tag name: "
2027 "tag-name=\"%s\"", tag
);
2031 BT_MOVE(*variant_decl
, untagged_variant_decl
);
2034 assert(!untagged_variant_decl
);
2035 assert(*variant_decl
);
2040 BT_PUT(untagged_variant_decl
);
2041 BT_PUT(*variant_decl
);
2047 int visit_enum_decl_entry(struct ctx
*ctx
, struct ctf_node
*enumerator
,
2048 struct bt_ctf_field_type
*enum_decl
, int64_t *last
, int is_signed
)
2052 struct ctf_node
*iter
;
2053 int64_t start
= 0, end
= 0;
2054 const char *label
= enumerator
->u
.enumerator
.id
;
2055 struct bt_list_head
*values
= &enumerator
->u
.enumerator
.values
;
2057 bt_list_for_each_entry(iter
, values
, siblings
) {
2060 if (iter
->type
!= NODE_UNARY_EXPRESSION
) {
2062 "Wrong expression for enumeration field type label: "
2063 "node-type=%d, label=\"%s\"", iter
->type
,
2075 switch (iter
->u
.unary_expression
.type
) {
2076 case UNARY_SIGNED_CONSTANT
:
2077 *target
= iter
->u
.unary_expression
.u
.signed_constant
;
2079 case UNARY_UNSIGNED_CONSTANT
:
2081 iter
->u
.unary_expression
.u
.unsigned_constant
;
2085 "Invalid enumeration field type entry: "
2086 "expecting constant signed or unsigned integer: "
2087 "node-type=%d, label=\"%s\"",
2088 iter
->u
.unary_expression
.type
, label
);
2095 "Invalid enumeration field type entry: label=\"%s\"",
2115 ret
= bt_ctf_field_type_enumeration_add_mapping(enum_decl
, label
,
2118 ret
= bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_decl
,
2119 label
, (uint64_t) start
, (uint64_t) end
);
2122 _BT_LOGE_NODE(enumerator
,
2123 "Cannot add mapping to enumeration field type: "
2124 "label=\"%s\", ret=%d, "
2125 "start-value-unsigned=%" PRIu64
", "
2126 "end-value-unsigned=%" PRIu64
, label
, ret
,
2127 (uint64_t) start
, (uint64_t) end
);
2138 int visit_enum_decl(struct ctx
*ctx
, const char *name
,
2139 struct ctf_node
*container_type
,
2140 struct bt_list_head
*enumerator_list
,
2142 struct bt_ctf_field_type
**enum_decl
)
2146 _BT_CTF_FIELD_TYPE_INIT(integer_decl
);
2150 /* For named enum (without body), lookup in declaration scope */
2152 _BT_CTF_FIELD_TYPE_INIT(enum_decl_copy
);
2155 BT_LOGE_STR("Bodyless enumeration field type: missing name.");
2160 *enum_decl
= ctx_decl_scope_lookup_enum(ctx
->current_scope
,
2163 BT_LOGE("Cannot find enumeration field type: "
2164 "name=\"enum %s\"", name
);
2169 /* Make a copy of it */
2170 enum_decl_copy
= bt_ctf_field_type_copy(*enum_decl
);
2171 if (!enum_decl_copy
) {
2172 BT_LOGE_STR("Cannot create copy of enumeration field type.");
2178 BT_MOVE(*enum_decl
, enum_decl_copy
);
2180 struct ctf_node
*iter
;
2181 int64_t last_value
= 0;
2184 _BT_CTF_FIELD_TYPE_INIT(eenum_decl
);
2186 eenum_decl
= ctx_decl_scope_lookup_enum(
2187 ctx
->current_scope
, name
, 1);
2190 BT_LOGE("Enumeration field type already declared in local scope: "
2191 "name=\"enum %s\"", name
);
2197 if (!container_type
) {
2198 integer_decl
= ctx_decl_scope_lookup_alias(
2199 ctx
->current_scope
, "int", -1);
2200 if (!integer_decl
) {
2201 BT_LOGE_STR("Cannot find implicit `int` field type alias for enumeration field type.");
2206 ret
= visit_type_declarator(ctx
, container_type
,
2207 &qdummy_id
, NULL
, &integer_decl
, NULL
);
2209 assert(!integer_decl
);
2215 assert(integer_decl
);
2217 if (!bt_ctf_field_type_is_integer(integer_decl
)) {
2218 BT_LOGE("Container field type for enumeration field type is not an integer field type: "
2220 bt_ctf_field_type_id_string(
2221 bt_ctf_field_type_get_type_id(integer_decl
)));
2226 *enum_decl
= bt_ctf_field_type_enumeration_create(integer_decl
);
2228 BT_LOGE_STR("Cannot create enumeration field type.");
2233 bt_list_for_each_entry(iter
, enumerator_list
, siblings
) {
2234 ret
= visit_enum_decl_entry(ctx
, iter
, *enum_decl
,
2236 bt_ctf_field_type_integer_get_signed(integer_decl
));
2239 "Cannot visit enumeration field type entry: "
2246 ret
= ctx_decl_scope_register_enum(ctx
->current_scope
,
2249 BT_LOGE("Cannot register enumeration field type in declaration scope: "
2256 BT_PUT(integer_decl
);
2261 BT_PUT(integer_decl
);
2268 int visit_type_specifier(struct ctx
*ctx
,
2269 struct ctf_node
*type_specifier_list
,
2270 struct bt_ctf_field_type
**decl
)
2273 GString
*str
= NULL
;
2274 _BT_CTF_FIELD_TYPE_INIT(decl_copy
);
2277 str
= g_string_new("");
2278 ret
= get_type_specifier_list_name(ctx
, type_specifier_list
, str
);
2280 _BT_LOGE_NODE(type_specifier_list
,
2281 "Cannot get type specifier list's name: ret=%d", ret
);
2285 *decl
= ctx_decl_scope_lookup_alias(ctx
->current_scope
, str
->str
, -1);
2287 _BT_LOGE_NODE(type_specifier_list
,
2288 "Cannot find type alias: name=\"%s\"", str
->str
);
2293 /* Make a copy of the type declaration */
2294 decl_copy
= bt_ctf_field_type_copy(*decl
);
2296 _BT_LOGE_NODE(type_specifier_list
,
2297 "Cannot create field type copy.");
2302 BT_MOVE(*decl
, decl_copy
);
2303 (void) g_string_free(str
, TRUE
);
2310 (void) g_string_free(str
, TRUE
);
2319 int visit_integer_decl(struct ctx
*ctx
,
2320 struct bt_list_head
*expressions
,
2321 struct bt_ctf_field_type
**integer_decl
)
2326 struct ctf_node
*expression
;
2327 uint64_t alignment
= 0, size
= 0;
2328 struct bt_ctf_clock_class
*mapped_clock
= NULL
;
2329 enum bt_ctf_string_encoding encoding
= BT_CTF_STRING_ENCODING_NONE
;
2330 enum bt_ctf_integer_base base
= BT_CTF_INTEGER_BASE_DECIMAL
;
2331 enum bt_ctf_byte_order byte_order
=
2332 bt_ctf_trace_get_native_byte_order(ctx
->trace
);
2334 *integer_decl
= NULL
;
2336 bt_list_for_each_entry(expression
, expressions
, siblings
) {
2337 struct ctf_node
*left
, *right
;
2339 left
= _BT_LIST_FIRST_ENTRY(&expression
->u
.ctf_expression
.left
,
2340 struct ctf_node
, siblings
);
2341 right
= _BT_LIST_FIRST_ENTRY(
2342 &expression
->u
.ctf_expression
.right
, struct ctf_node
,
2345 if (left
->u
.unary_expression
.type
!= UNARY_STRING
) {
2347 "Unexpected unary expression type: type=%d",
2348 left
->u
.unary_expression
.type
);
2353 if (!strcmp(left
->u
.unary_expression
.u
.string
, "signed")) {
2354 if (_IS_SET(&set
, _INTEGER_SIGNED_SET
)) {
2355 _BT_LOGE_DUP_ATTR(left
, "signed",
2356 "integer field type");
2361 signedness
= get_boolean(right
);
2362 if (signedness
< 0) {
2363 _BT_LOGE_NODE(right
,
2364 "Invalid boolean value for integer field type's `signed` attribute: "
2370 _SET(&set
, _INTEGER_SIGNED_SET
);
2371 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2373 if (_IS_SET(&set
, _INTEGER_BYTE_ORDER_SET
)) {
2374 _BT_LOGE_DUP_ATTR(left
, "byte_order",
2375 "integer field type");
2380 byte_order
= get_real_byte_order(ctx
, right
);
2381 if (byte_order
== BT_CTF_BYTE_ORDER_UNKNOWN
) {
2382 _BT_LOGE_NODE(right
,
2383 "Invalid `byte_order` attribute in integer field type: "
2389 _SET(&set
, _INTEGER_BYTE_ORDER_SET
);
2390 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "size")) {
2391 if (_IS_SET(&set
, _INTEGER_SIZE_SET
)) {
2392 _BT_LOGE_DUP_ATTR(left
, "size",
2393 "integer field type");
2398 if (right
->u
.unary_expression
.type
!=
2399 UNARY_UNSIGNED_CONSTANT
) {
2400 _BT_LOGE_NODE(right
,
2401 "Invalid `size` attribute in integer field type: "
2402 "expecting unsigned constant integer: "
2404 right
->u
.unary_expression
.type
);
2409 size
= right
->u
.unary_expression
.u
.unsigned_constant
;
2411 _BT_LOGE_NODE(right
,
2412 "Invalid `size` attribute in integer field type: "
2413 "expecting positive constant integer: "
2414 "size=%" PRIu64
, size
);
2417 } else if (size
> 64) {
2418 _BT_LOGE_NODE(right
,
2419 "Invalid `size` attribute in integer field type: "
2420 "integer fields over 64 bits are not supported as of this version: "
2421 "size=%" PRIu64
, size
);
2426 _SET(&set
, _INTEGER_SIZE_SET
);
2427 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2429 if (_IS_SET(&set
, _INTEGER_ALIGN_SET
)) {
2430 _BT_LOGE_DUP_ATTR(left
, "align",
2431 "integer field type");
2436 if (right
->u
.unary_expression
.type
!=
2437 UNARY_UNSIGNED_CONSTANT
) {
2438 _BT_LOGE_NODE(right
,
2439 "Invalid `align` attribute in integer field type: "
2440 "expecting unsigned constant integer: "
2442 right
->u
.unary_expression
.type
);
2448 right
->u
.unary_expression
.u
.unsigned_constant
;
2449 if (!is_align_valid(alignment
)) {
2450 _BT_LOGE_NODE(right
,
2451 "Invalid `align` attribute in integer field type: "
2452 "expecting power of two: "
2453 "align=%" PRIu64
, alignment
);
2458 _SET(&set
, _INTEGER_ALIGN_SET
);
2459 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "base")) {
2460 if (_IS_SET(&set
, _INTEGER_BASE_SET
)) {
2461 _BT_LOGE_DUP_ATTR(left
, "base",
2462 "integer field type");
2467 switch (right
->u
.unary_expression
.type
) {
2468 case UNARY_UNSIGNED_CONSTANT
:
2470 uint64_t constant
= right
->u
.unary_expression
.
2471 u
.unsigned_constant
;
2475 base
= BT_CTF_INTEGER_BASE_BINARY
;
2478 base
= BT_CTF_INTEGER_BASE_OCTAL
;
2481 base
= BT_CTF_INTEGER_BASE_DECIMAL
;
2484 base
= BT_CTF_INTEGER_BASE_HEXADECIMAL
;
2487 _BT_LOGE_NODE(right
,
2488 "Invalid `base` attribute in integer field type: "
2490 right
->u
.unary_expression
.u
.unsigned_constant
);
2498 char *s_right
= concatenate_unary_strings(
2499 &expression
->u
.ctf_expression
.right
);
2501 _BT_LOGE_NODE(right
,
2502 "Unexpected unary expression for integer field type's `base` attribute.");
2507 if (!strcmp(s_right
, "decimal") ||
2508 !strcmp(s_right
, "dec") ||
2509 !strcmp(s_right
, "d") ||
2510 !strcmp(s_right
, "i") ||
2511 !strcmp(s_right
, "u")) {
2512 base
= BT_CTF_INTEGER_BASE_DECIMAL
;
2513 } else if (!strcmp(s_right
, "hexadecimal") ||
2514 !strcmp(s_right
, "hex") ||
2515 !strcmp(s_right
, "x") ||
2516 !strcmp(s_right
, "X") ||
2517 !strcmp(s_right
, "p")) {
2518 base
= BT_CTF_INTEGER_BASE_HEXADECIMAL
;
2519 } else if (!strcmp(s_right
, "octal") ||
2520 !strcmp(s_right
, "oct") ||
2521 !strcmp(s_right
, "o")) {
2522 base
= BT_CTF_INTEGER_BASE_OCTAL
;
2523 } else if (!strcmp(s_right
, "binary") ||
2524 !strcmp(s_right
, "b")) {
2525 base
= BT_CTF_INTEGER_BASE_BINARY
;
2527 _BT_LOGE_NODE(right
,
2528 "Unexpected unary expression for integer field type's `base` attribute: "
2529 "base=\"%s\"", s_right
);
2539 _BT_LOGE_NODE(right
,
2540 "Invalid `base` attribute in integer field type: "
2541 "expecting unsigned constant integer or unary string.");
2546 _SET(&set
, _INTEGER_BASE_SET
);
2547 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2551 if (_IS_SET(&set
, _INTEGER_ENCODING_SET
)) {
2552 _BT_LOGE_DUP_ATTR(left
, "encoding",
2553 "integer field type");
2558 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
2559 _BT_LOGE_NODE(right
,
2560 "Invalid `encoding` attribute in integer field type: "
2561 "expecting unary string.");
2566 s_right
= concatenate_unary_strings(
2567 &expression
->u
.ctf_expression
.right
);
2569 _BT_LOGE_NODE(right
,
2570 "Unexpected unary expression for integer field type's `encoding` attribute.");
2575 if (!strcmp(s_right
, "UTF8") ||
2576 !strcmp(s_right
, "utf8") ||
2577 !strcmp(s_right
, "utf-8") ||
2578 !strcmp(s_right
, "UTF-8")) {
2579 encoding
= BT_CTF_STRING_ENCODING_UTF8
;
2580 } else if (!strcmp(s_right
, "ASCII") ||
2581 !strcmp(s_right
, "ascii")) {
2582 encoding
= BT_CTF_STRING_ENCODING_ASCII
;
2583 } else if (!strcmp(s_right
, "none")) {
2584 encoding
= BT_CTF_STRING_ENCODING_NONE
;
2586 _BT_LOGE_NODE(right
,
2587 "Invalid `encoding` attribute in integer field type: "
2588 "unknown encoding: encoding=\"%s\"",
2596 _SET(&set
, _INTEGER_ENCODING_SET
);
2597 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "map")) {
2598 const char *clock_name
;
2600 if (_IS_SET(&set
, _INTEGER_MAP_SET
)) {
2601 _BT_LOGE_DUP_ATTR(left
, "map",
2602 "integer field type");
2607 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
2608 _BT_LOGE_NODE(right
,
2609 "Invalid `map` attribute in integer field type: "
2610 "expecting unary string.");
2616 get_map_clock_name_value(
2617 &expression
->u
.ctf_expression
.right
);
2619 char *s_right
= concatenate_unary_strings(
2620 &expression
->u
.ctf_expression
.right
);
2623 _BT_LOGE_NODE(right
,
2624 "Unexpected unary expression for integer field type's `map` attribute.");
2629 _BT_LOGE_NODE(right
,
2630 "Invalid `map` attribute in integer field type: "
2631 "cannot find clock class at this point: name=\"%s\"",
2633 _SET(&set
, _INTEGER_MAP_SET
);
2638 mapped_clock
= bt_ctf_trace_get_clock_class_by_name(
2639 ctx
->trace
, clock_name
);
2640 if (!mapped_clock
) {
2641 _BT_LOGE_NODE(right
,
2642 "Invalid `map` attribute in integer field type: "
2643 "cannot find clock class at this point: name=\"%s\"",
2649 _SET(&set
, _INTEGER_MAP_SET
);
2652 "Unknown attribute in integer field type: "
2654 left
->u
.unary_expression
.u
.string
);
2658 if (!_IS_SET(&set
, _INTEGER_SIZE_SET
)) {
2659 BT_LOGE_STR("Missing `size` attribute in integer field type.");
2664 if (!_IS_SET(&set
, _INTEGER_ALIGN_SET
)) {
2665 if (size
% CHAR_BIT
) {
2666 /* Bit-packed alignment */
2669 /* Byte-packed alignment */
2670 alignment
= CHAR_BIT
;
2674 *integer_decl
= bt_ctf_field_type_integer_create((unsigned int) size
);
2675 if (!*integer_decl
) {
2676 BT_LOGE_STR("Cannot create integer field type.");
2681 ret
= bt_ctf_field_type_integer_set_signed(*integer_decl
, signedness
);
2682 ret
|= bt_ctf_field_type_integer_set_base(*integer_decl
, base
);
2683 ret
|= bt_ctf_field_type_integer_set_encoding(*integer_decl
, encoding
);
2684 ret
|= bt_ctf_field_type_set_alignment(*integer_decl
,
2685 (unsigned int) alignment
);
2686 ret
|= bt_ctf_field_type_set_byte_order(*integer_decl
, byte_order
);
2690 ret
|= bt_ctf_field_type_integer_set_mapped_clock_class(
2691 *integer_decl
, mapped_clock
);
2692 bt_put(mapped_clock
);
2693 mapped_clock
= NULL
;
2697 BT_LOGE_STR("Cannot configure integer field type.");
2706 bt_put(mapped_clock
);
2709 BT_PUT(*integer_decl
);
2715 int visit_floating_point_number_decl(struct ctx
*ctx
,
2716 struct bt_list_head
*expressions
,
2717 struct bt_ctf_field_type
**float_decl
)
2721 struct ctf_node
*expression
;
2722 uint64_t alignment
= 1, exp_dig
= 0, mant_dig
= 0;
2723 enum bt_ctf_byte_order byte_order
=
2724 bt_ctf_trace_get_native_byte_order(ctx
->trace
);
2728 bt_list_for_each_entry(expression
, expressions
, siblings
) {
2729 struct ctf_node
*left
, *right
;
2731 left
= _BT_LIST_FIRST_ENTRY(&expression
->u
.ctf_expression
.left
,
2732 struct ctf_node
, siblings
);
2733 right
= _BT_LIST_FIRST_ENTRY(
2734 &expression
->u
.ctf_expression
.right
, struct ctf_node
,
2737 if (left
->u
.unary_expression
.type
!= UNARY_STRING
) {
2739 "Unexpected unary expression type: type=%d",
2740 left
->u
.unary_expression
.type
);
2745 if (!strcmp(left
->u
.unary_expression
.u
.string
, "byte_order")) {
2746 if (_IS_SET(&set
, _FLOAT_BYTE_ORDER_SET
)) {
2747 _BT_LOGE_DUP_ATTR(left
, "byte_order",
2748 "floating point number field type");
2753 byte_order
= get_real_byte_order(ctx
, right
);
2754 if (byte_order
== BT_CTF_BYTE_ORDER_UNKNOWN
) {
2755 _BT_LOGE_NODE(right
,
2756 "Invalid `byte_order` attribute in floating point number field type: "
2762 _SET(&set
, _FLOAT_BYTE_ORDER_SET
);
2763 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2765 if (_IS_SET(&set
, _FLOAT_EXP_DIG_SET
)) {
2766 _BT_LOGE_DUP_ATTR(left
, "exp_dig",
2767 "floating point number field type");
2772 if (right
->u
.unary_expression
.type
!=
2773 UNARY_UNSIGNED_CONSTANT
) {
2774 _BT_LOGE_NODE(right
,
2775 "Invalid `exp_dig` attribute in floating point number field type: "
2776 "expecting unsigned constant integer: "
2778 right
->u
.unary_expression
.type
);
2783 exp_dig
= right
->u
.unary_expression
.u
.unsigned_constant
;
2784 _SET(&set
, _FLOAT_EXP_DIG_SET
);
2785 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2787 if (_IS_SET(&set
, _FLOAT_MANT_DIG_SET
)) {
2788 _BT_LOGE_DUP_ATTR(left
, "mant_dig",
2789 "floating point number field type");
2794 if (right
->u
.unary_expression
.type
!=
2795 UNARY_UNSIGNED_CONSTANT
) {
2796 _BT_LOGE_NODE(right
,
2797 "Invalid `mant_dig` attribute in floating point number field type: "
2798 "expecting unsigned constant integer: "
2800 right
->u
.unary_expression
.type
);
2805 mant_dig
= right
->u
.unary_expression
.u
.
2807 _SET(&set
, _FLOAT_MANT_DIG_SET
);
2808 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2810 if (_IS_SET(&set
, _FLOAT_ALIGN_SET
)) {
2811 _BT_LOGE_DUP_ATTR(left
, "align",
2812 "floating point number field type");
2817 if (right
->u
.unary_expression
.type
!=
2818 UNARY_UNSIGNED_CONSTANT
) {
2819 _BT_LOGE_NODE(right
,
2820 "Invalid `align` attribute in floating point number field type: "
2821 "expecting unsigned constant integer: "
2823 right
->u
.unary_expression
.type
);
2828 alignment
= right
->u
.unary_expression
.u
.
2831 if (!is_align_valid(alignment
)) {
2832 _BT_LOGE_NODE(right
,
2833 "Invalid `align` attribute in floating point number field type: "
2834 "expecting power of two: "
2835 "align=%" PRIu64
, alignment
);
2840 _SET(&set
, _FLOAT_ALIGN_SET
);
2843 "Unknown attribute in floating point number field type: "
2845 left
->u
.unary_expression
.u
.string
);
2849 if (!_IS_SET(&set
, _FLOAT_MANT_DIG_SET
)) {
2850 BT_LOGE_STR("Missing `mant_dig` attribute in floating point number field type.");
2855 if (!_IS_SET(&set
, _FLOAT_EXP_DIG_SET
)) {
2856 BT_LOGE_STR("Missing `exp_dig` attribute in floating point number field type.");
2861 if (!_IS_SET(&set
, _INTEGER_ALIGN_SET
)) {
2862 if ((mant_dig
+ exp_dig
) % CHAR_BIT
) {
2863 /* Bit-packed alignment */
2866 /* Byte-packed alignment */
2867 alignment
= CHAR_BIT
;
2871 *float_decl
= bt_ctf_field_type_floating_point_create();
2873 BT_LOGE_STR("Cannot create floating point number field type.");
2878 ret
= bt_ctf_field_type_floating_point_set_exponent_digits(
2879 *float_decl
, exp_dig
);
2880 ret
|= bt_ctf_field_type_floating_point_set_mantissa_digits(
2881 *float_decl
, mant_dig
);
2882 ret
|= bt_ctf_field_type_set_byte_order(*float_decl
, byte_order
);
2883 ret
|= bt_ctf_field_type_set_alignment(*float_decl
, alignment
);
2885 BT_LOGE_STR("Cannot configure floating point number field type.");
2893 BT_PUT(*float_decl
);
2899 int visit_string_decl(struct ctx
*ctx
,
2900 struct bt_list_head
*expressions
,
2901 struct bt_ctf_field_type
**string_decl
)
2905 struct ctf_node
*expression
;
2906 enum bt_ctf_string_encoding encoding
= BT_CTF_STRING_ENCODING_UTF8
;
2908 *string_decl
= NULL
;
2910 bt_list_for_each_entry(expression
, expressions
, siblings
) {
2911 struct ctf_node
*left
, *right
;
2913 left
= _BT_LIST_FIRST_ENTRY(&expression
->u
.ctf_expression
.left
,
2914 struct ctf_node
, siblings
);
2915 right
= _BT_LIST_FIRST_ENTRY(
2916 &expression
->u
.ctf_expression
.right
, struct ctf_node
,
2919 if (left
->u
.unary_expression
.type
!= UNARY_STRING
) {
2921 "Unexpected unary expression type: type=%d",
2922 left
->u
.unary_expression
.type
);
2927 if (!strcmp(left
->u
.unary_expression
.u
.string
, "encoding")) {
2930 if (_IS_SET(&set
, _STRING_ENCODING_SET
)) {
2931 _BT_LOGE_DUP_ATTR(left
, "encoding",
2932 "string field type");
2937 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
2938 _BT_LOGE_NODE(right
,
2939 "Invalid `encoding` attribute in string field type: "
2940 "expecting unary string.");
2945 s_right
= concatenate_unary_strings(
2946 &expression
->u
.ctf_expression
.right
);
2948 _BT_LOGE_NODE(right
,
2949 "Unexpected unary expression for string field type's `encoding` attribute.");
2954 if (!strcmp(s_right
, "UTF8") ||
2955 !strcmp(s_right
, "utf8") ||
2956 !strcmp(s_right
, "utf-8") ||
2957 !strcmp(s_right
, "UTF-8")) {
2958 encoding
= BT_CTF_STRING_ENCODING_UTF8
;
2959 } else if (!strcmp(s_right
, "ASCII") ||
2960 !strcmp(s_right
, "ascii")) {
2961 encoding
= BT_CTF_STRING_ENCODING_ASCII
;
2962 } else if (!strcmp(s_right
, "none")) {
2963 encoding
= BT_CTF_STRING_ENCODING_NONE
;
2965 _BT_LOGE_NODE(right
,
2966 "Invalid `encoding` attribute in string field type: "
2967 "unknown encoding: encoding=\"%s\"",
2975 _SET(&set
, _STRING_ENCODING_SET
);
2978 "Unknown attribute in string field type: "
2980 left
->u
.unary_expression
.u
.string
);
2984 *string_decl
= bt_ctf_field_type_string_create();
2985 if (!*string_decl
) {
2986 BT_LOGE_STR("Cannot create string field type.");
2991 ret
= bt_ctf_field_type_string_set_encoding(*string_decl
, encoding
);
2993 BT_LOGE_STR("Cannot configure string field type.");
3001 BT_PUT(*string_decl
);
3007 int visit_type_specifier_list(struct ctx
*ctx
,
3008 struct ctf_node
*ts_list
,
3009 struct bt_ctf_field_type
**decl
)
3012 struct ctf_node
*first
, *node
;
3016 if (ts_list
->type
!= NODE_TYPE_SPECIFIER_LIST
) {
3017 _BT_LOGE_NODE(ts_list
,
3018 "Unexpected node type: node-type=%d", ts_list
->type
);
3023 first
= _BT_LIST_FIRST_ENTRY(&ts_list
->u
.type_specifier_list
.head
,
3024 struct ctf_node
, siblings
);
3025 if (first
->type
!= NODE_TYPE_SPECIFIER
) {
3026 _BT_LOGE_NODE(first
,
3027 "Unexpected node type: node-type=%d", first
->type
);
3032 node
= first
->u
.type_specifier
.node
;
3034 switch (first
->u
.type_specifier
.type
) {
3035 case TYPESPEC_INTEGER
:
3036 ret
= visit_integer_decl(ctx
, &node
->u
.integer
.expressions
,
3043 case TYPESPEC_FLOATING_POINT
:
3044 ret
= visit_floating_point_number_decl(ctx
,
3045 &node
->u
.floating_point
.expressions
, decl
);
3051 case TYPESPEC_STRING
:
3052 ret
= visit_string_decl(ctx
,
3053 &node
->u
.string
.expressions
, decl
);
3059 case TYPESPEC_STRUCT
:
3060 ret
= visit_struct_decl(ctx
, node
->u
._struct
.name
,
3061 &node
->u
._struct
.declaration_list
,
3062 node
->u
._struct
.has_body
,
3063 &node
->u
._struct
.min_align
, decl
);
3069 case TYPESPEC_VARIANT
:
3070 ret
= visit_variant_decl(ctx
, node
->u
.variant
.name
,
3071 node
->u
.variant
.choice
,
3072 &node
->u
.variant
.declaration_list
,
3073 node
->u
.variant
.has_body
, decl
);
3080 ret
= visit_enum_decl(ctx
, node
->u
._enum
.enum_id
,
3081 node
->u
._enum
.container_type
,
3082 &node
->u
._enum
.enumerator_list
,
3083 node
->u
._enum
.has_body
, decl
);
3091 case TYPESPEC_SHORT
:
3094 case TYPESPEC_FLOAT
:
3095 case TYPESPEC_DOUBLE
:
3096 case TYPESPEC_SIGNED
:
3097 case TYPESPEC_UNSIGNED
:
3099 case TYPESPEC_COMPLEX
:
3100 case TYPESPEC_IMAGINARY
:
3101 case TYPESPEC_CONST
:
3102 case TYPESPEC_ID_TYPE
:
3103 ret
= visit_type_specifier(ctx
, ts_list
, decl
);
3105 _BT_LOGE_NODE(first
,
3106 "Cannot visit type specifier: ret=%d",
3113 _BT_LOGE_NODE(first
,
3114 "Unexpected type specifier type: node-type=%d",
3115 first
->u
.type_specifier
.type
);
3131 int visit_event_decl_entry(struct ctx
*ctx
, struct ctf_node
*node
,
3132 struct bt_ctf_event_class
*event_class
, int64_t *stream_id
,
3137 _BT_CTF_FIELD_TYPE_INIT(decl
);
3139 switch (node
->type
) {
3141 ret
= visit_typedef(ctx
, node
->u
._typedef
.type_specifier_list
,
3142 &node
->u
._typedef
.type_declarators
);
3145 "Cannot add type definition found in event class.");
3149 case NODE_TYPEALIAS
:
3150 ret
= visit_typealias(ctx
, node
->u
.typealias
.target
,
3151 node
->u
.typealias
.alias
);
3154 "Cannot add type alias found in event class.");
3158 case NODE_CTF_EXPRESSION
:
3160 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
3162 _BT_LOGE_NODE(node
, "Cannot concatenate unary strings.");
3167 if (!strcmp(left
, "name")) {
3168 /* This is already known at this stage */
3169 if (_IS_SET(set
, _EVENT_NAME_SET
)) {
3170 _BT_LOGE_DUP_ATTR(node
, "name",
3176 _SET(set
, _EVENT_NAME_SET
);
3177 } else if (!strcmp(left
, "id")) {
3180 if (_IS_SET(set
, _EVENT_ID_SET
)) {
3181 _BT_LOGE_DUP_ATTR(node
, "id",
3187 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
3189 /* Only read "id" if get_unary_unsigned() succeeded. */
3190 if (ret
|| (!ret
&& id
< 0)) {
3192 "Unexpected unary expression for event class's `id` attribute.");
3197 ret
= bt_ctf_event_class_set_id(event_class
, id
);
3200 "Cannot set event class's ID: "
3205 _SET(set
, _EVENT_ID_SET
);
3206 } else if (!strcmp(left
, "stream_id")) {
3207 if (_IS_SET(set
, _EVENT_STREAM_ID_SET
)) {
3208 _BT_LOGE_DUP_ATTR(node
, "stream_id",
3214 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
3215 (uint64_t *) stream_id
);
3217 * Only read "stream_id" if get_unary_unsigned()
3220 if (ret
|| (!ret
&& *stream_id
< 0)) {
3222 "Unexpected unary expression for event class's `stream_id` attribute.");
3227 _SET(set
, _EVENT_STREAM_ID_SET
);
3228 } else if (!strcmp(left
, "context")) {
3229 if (_IS_SET(set
, _EVENT_CONTEXT_SET
)) {
3231 "Duplicate `context` entry in event class.");
3236 ret
= visit_type_specifier_list(ctx
,
3237 _BT_LIST_FIRST_ENTRY(
3238 &node
->u
.ctf_expression
.right
,
3239 struct ctf_node
, siblings
),
3243 "Cannot create event class's context field type.");
3248 ret
= bt_ctf_event_class_set_context_type(
3253 "Cannot set event class's context field type.");
3257 _SET(set
, _EVENT_CONTEXT_SET
);
3258 } else if (!strcmp(left
, "fields")) {
3259 if (_IS_SET(set
, _EVENT_FIELDS_SET
)) {
3261 "Duplicate `fields` entry in event class.");
3266 ret
= visit_type_specifier_list(ctx
,
3267 _BT_LIST_FIRST_ENTRY(
3268 &node
->u
.ctf_expression
.right
,
3269 struct ctf_node
, siblings
),
3273 "Cannot create event class's payload field type.");
3278 ret
= bt_ctf_event_class_set_payload_type(
3283 "Cannot set event class's payload field type.");
3287 _SET(set
, _EVENT_FIELDS_SET
);
3288 } else if (!strcmp(left
, "loglevel")) {
3289 uint64_t loglevel_value
;
3290 enum bt_ctf_event_class_log_level log_level
=
3291 BT_CTF_EVENT_CLASS_LOG_LEVEL_UNSPECIFIED
;
3293 if (_IS_SET(set
, _EVENT_LOGLEVEL_SET
)) {
3294 _BT_LOGE_DUP_ATTR(node
, "loglevel",
3300 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
3304 "Unexpected unary expression for event class's `loglevel` attribute.");
3309 switch (loglevel_value
) {
3311 log_level
= BT_CTF_EVENT_CLASS_LOG_LEVEL_EMERGENCY
;
3314 log_level
= BT_CTF_EVENT_CLASS_LOG_LEVEL_ALERT
;
3317 log_level
= BT_CTF_EVENT_CLASS_LOG_LEVEL_CRITICAL
;
3320 log_level
= BT_CTF_EVENT_CLASS_LOG_LEVEL_ERROR
;
3323 log_level
= BT_CTF_EVENT_CLASS_LOG_LEVEL_WARNING
;
3326 log_level
= BT_CTF_EVENT_CLASS_LOG_LEVEL_NOTICE
;
3329 log_level
= BT_CTF_EVENT_CLASS_LOG_LEVEL_INFO
;
3332 log_level
= BT_CTF_EVENT_CLASS_LOG_LEVEL_DEBUG_SYSTEM
;
3335 log_level
= BT_CTF_EVENT_CLASS_LOG_LEVEL_DEBUG_PROGRAM
;
3338 log_level
= BT_CTF_EVENT_CLASS_LOG_LEVEL_DEBUG_PROCESS
;
3341 log_level
= BT_CTF_EVENT_CLASS_LOG_LEVEL_DEBUG_MODULE
;
3344 log_level
= BT_CTF_EVENT_CLASS_LOG_LEVEL_DEBUG_UNIT
;
3347 log_level
= BT_CTF_EVENT_CLASS_LOG_LEVEL_DEBUG_FUNCTION
;
3350 log_level
= BT_CTF_EVENT_CLASS_LOG_LEVEL_DEBUG_LINE
;
3353 log_level
= BT_CTF_EVENT_CLASS_LOG_LEVEL_DEBUG
;
3356 _BT_LOGW_NODE(node
, "Not setting event class's log level because its value is unknown: "
3357 "log-level=%" PRIu64
, loglevel_value
);
3360 if (log_level
!= BT_CTF_EVENT_CLASS_LOG_LEVEL_UNSPECIFIED
) {
3361 ret
= bt_ctf_event_class_set_log_level(
3362 event_class
, log_level
);
3365 "Cannot set event class's log level.");
3370 _SET(set
, _EVENT_LOGLEVEL_SET
);
3371 } else if (!strcmp(left
, "model.emf.uri")) {
3374 if (_IS_SET(set
, _EVENT_MODEL_EMF_URI_SET
)) {
3375 _BT_LOGE_DUP_ATTR(node
, "model.emf.uri",
3381 right
= concatenate_unary_strings(
3382 &node
->u
.ctf_expression
.right
);
3385 "Unexpected unary expression for event class's `model.emf.uri` attribute.");
3390 if (strlen(right
) == 0) {
3392 "Not setting event class's EMF URI because it's empty.");
3394 ret
= bt_ctf_event_class_set_emf_uri(
3395 event_class
, right
);
3398 "Cannot set event class's EMF URI.");
3404 _SET(set
, _EVENT_MODEL_EMF_URI_SET
);
3407 "Unknown attribute in event class: "
3408 "attr-name=\"%s\"", left
);
3433 char *get_event_decl_name(struct ctx
*ctx
, struct ctf_node
*node
)
3437 struct ctf_node
*iter
;
3438 struct bt_list_head
*decl_list
= &node
->u
.event
.declaration_list
;
3440 bt_list_for_each_entry(iter
, decl_list
, siblings
) {
3441 if (iter
->type
!= NODE_CTF_EXPRESSION
) {
3445 left
= concatenate_unary_strings(&iter
->u
.ctf_expression
.left
);
3448 "Cannot concatenate unary strings.");
3452 if (!strcmp(left
, "name")) {
3453 name
= concatenate_unary_strings(
3454 &iter
->u
.ctf_expression
.right
);
3457 "Unexpected unary expression for event class's `name` attribute.");
3479 int reset_event_decl_types(struct ctx
*ctx
,
3480 struct bt_ctf_event_class
*event_class
)
3485 ret
= bt_ctf_event_class_set_context_type(event_class
, NULL
);
3487 BT_LOGE("Cannot reset initial event class's context field type: "
3488 "event-name=\"%s\"",
3489 bt_ctf_event_class_get_name(event_class
));
3493 /* Event payload. */
3494 ret
= bt_ctf_event_class_set_payload_type(event_class
, NULL
);
3496 BT_LOGE("Cannot reset initial event class's payload field type: "
3497 "event-name=\"%s\"",
3498 bt_ctf_event_class_get_name(event_class
));
3506 int reset_stream_decl_types(struct ctx
*ctx
,
3507 struct bt_ctf_stream_class
*stream_class
)
3511 /* Packet context. */
3512 ret
= bt_ctf_stream_class_set_packet_context_type(stream_class
, NULL
);
3514 BT_LOGE_STR("Cannot reset initial stream class's packet context field type.");
3519 ret
= bt_ctf_stream_class_set_event_header_type(stream_class
, NULL
);
3521 BT_LOGE_STR("Cannot reset initial stream class's event header field type.");
3525 /* Event context. */
3526 ret
= bt_ctf_stream_class_set_event_context_type(stream_class
, NULL
);
3528 BT_LOGE_STR("Cannot reset initial stream class's event context field type.");
3536 struct bt_ctf_stream_class
*create_reset_stream_class(struct ctx
*ctx
)
3539 struct bt_ctf_stream_class
*stream_class
;
3541 stream_class
= bt_ctf_stream_class_create_empty(NULL
);
3542 if (!stream_class
) {
3543 BT_LOGE_STR("Cannot create empty stream class.");
3548 * Set packet context, event header, and event context to NULL to
3549 * override the default ones.
3551 ret
= reset_stream_decl_types(ctx
, stream_class
);
3556 return stream_class
;
3559 BT_PUT(stream_class
);
3565 int visit_event_decl(struct ctx
*ctx
, struct ctf_node
*node
)
3570 struct ctf_node
*iter
;
3571 int64_t stream_id
= -1;
3572 char *event_name
= NULL
;
3573 struct bt_ctf_event_class
*event_class
= NULL
;
3574 struct bt_ctf_event_class
*eevent_class
;
3575 struct bt_ctf_stream_class
*stream_class
= NULL
;
3576 struct bt_list_head
*decl_list
= &node
->u
.event
.declaration_list
;
3577 bool pop_scope
= false;
3579 if (node
->visited
) {
3583 node
->visited
= TRUE
;
3584 event_name
= get_event_decl_name(ctx
, node
);
3587 "Missing `name` attribute in event class.");
3592 event_class
= bt_ctf_event_class_create(event_name
);
3595 * Unset context and fields to override the default ones.
3597 ret
= reset_event_decl_types(ctx
, event_class
);
3600 "Cannot reset event class's field types: "
3605 ret
= ctx_push_scope(ctx
);
3607 BT_LOGE_STR("Cannot push scope.");
3613 bt_list_for_each_entry(iter
, decl_list
, siblings
) {
3614 ret
= visit_event_decl_entry(ctx
, iter
, event_class
,
3617 _BT_LOGE_NODE(iter
, "Cannot visit event class's entry: "
3623 if (!_IS_SET(&set
, _EVENT_STREAM_ID_SET
)) {
3625 struct bt_ctf_stream_class
*new_stream_class
;
3626 size_t stream_class_count
=
3627 g_hash_table_size(ctx
->stream_classes
) +
3628 bt_ctf_trace_get_stream_class_count(ctx
->trace
);
3631 * Allow missing stream_id if there is only a single
3634 switch (stream_class_count
) {
3636 /* Create implicit stream class if there's none */
3637 new_stream_class
= create_reset_stream_class(ctx
);
3638 if (!new_stream_class
) {
3640 "Cannot create empty stream class.");
3645 ret
= bt_ctf_stream_class_set_id(new_stream_class
, 0);
3648 "Cannot set stream class's ID: "
3649 "id=0, ret=%d", ret
);
3650 BT_PUT(new_stream_class
);
3656 /* Move reference to visitor's context */
3657 g_hash_table_insert(ctx
->stream_classes
,
3658 (gpointer
) stream_id
, new_stream_class
);
3659 new_stream_class
= NULL
;
3662 /* Single stream class: get its ID */
3663 if (g_hash_table_size(ctx
->stream_classes
) == 1) {
3664 keys
= g_hash_table_get_keys(ctx
->stream_classes
);
3665 stream_id
= (int64_t) keys
->data
;
3668 assert(bt_ctf_trace_get_stream_class_count(
3671 bt_ctf_trace_get_stream_class_by_index(
3673 assert(stream_class
);
3674 stream_id
= bt_ctf_stream_class_get_id(
3676 BT_PUT(stream_class
);
3681 "Missing `stream_id` attribute in event class.");
3687 assert(stream_id
>= 0);
3689 /* We have the stream ID now; get the stream class if found */
3690 stream_class
= g_hash_table_lookup(ctx
->stream_classes
,
3691 (gpointer
) stream_id
);
3692 bt_get(stream_class
);
3693 if (!stream_class
) {
3694 stream_class
= bt_ctf_trace_get_stream_class_by_id(ctx
->trace
,
3696 if (!stream_class
) {
3698 "Cannot find stream class at this point: "
3699 "id=%" PRId64
, stream_id
);
3705 assert(stream_class
);
3707 if (!_IS_SET(&set
, _EVENT_ID_SET
)) {
3708 /* Allow only one event without ID per stream */
3709 if (bt_ctf_stream_class_get_event_class_count(stream_class
) !=
3712 "Missing `id` attribute in event class.");
3718 ret
= bt_ctf_event_class_set_id(event_class
, 0);
3721 "Cannot set event class's ID: id=0, ret=%d",
3727 event_id
= bt_ctf_event_class_get_id(event_class
);
3729 _BT_LOGE_NODE(node
, "Cannot get event class's ID.");
3734 eevent_class
= bt_ctf_stream_class_get_event_class_by_id(stream_class
,
3737 BT_PUT(eevent_class
);
3739 "Duplicate event class (same ID) in the same stream class: "
3740 "id=%" PRId64
, event_id
);
3745 ret
= bt_ctf_stream_class_add_event_class(stream_class
, event_class
);
3746 BT_PUT(event_class
);
3749 "Cannot add event class to stream class: ret=%d", ret
);
3756 bt_put(event_class
);
3764 bt_put(stream_class
);
3769 int auto_map_field_to_trace_clock_class(struct ctx
*ctx
,
3770 struct bt_ctf_field_type
*ft
)
3772 struct bt_ctf_clock_class
*clock_class_to_map_to
= NULL
;
3773 struct bt_ctf_clock_class
*mapped_clock_class
= NULL
;
3775 int64_t clock_class_count
;
3777 if (!ft
|| !bt_ctf_field_type_is_integer(ft
)) {
3781 mapped_clock_class
=
3782 bt_ctf_field_type_integer_get_mapped_clock_class(ft
);
3783 if (mapped_clock_class
) {
3787 clock_class_count
= bt_ctf_trace_get_clock_class_count(ctx
->trace
);
3788 assert(clock_class_count
>= 0);
3790 switch (clock_class_count
) {
3793 * No clock class exists in the trace at this
3794 * point. Create an implicit one at 1 GHz,
3795 * named `default`, and use this clock class.
3797 clock_class_to_map_to
= bt_ctf_clock_class_create("default");
3798 if (!clock_class_to_map_to
) {
3799 BT_LOGE_STR("Cannot create a clock class.");
3804 ret
= bt_ctf_clock_class_set_frequency(clock_class_to_map_to
,
3808 ret
= bt_ctf_trace_add_clock_class(ctx
->trace
,
3809 clock_class_to_map_to
);
3811 BT_LOGE_STR("Cannot add clock class to trace.");
3817 * Only one clock class exists in the trace at
3818 * this point: use this one.
3820 clock_class_to_map_to
=
3821 bt_ctf_trace_get_clock_class_by_index(ctx
->trace
, 0);
3822 assert(clock_class_to_map_to
);
3826 * Timestamp field not mapped to a clock class
3827 * and there's more than one clock class in the
3828 * trace: this is an error.
3830 BT_LOGE_STR("Timestamp field found with no mapped clock class, "
3831 "but there's more than one clock class in the trace at this point.");
3836 assert(clock_class_to_map_to
);
3837 ret
= bt_ctf_field_type_integer_set_mapped_clock_class(ft
,
3838 clock_class_to_map_to
);
3840 BT_LOGE("Cannot map field type's field to trace's clock class: "
3841 "clock-class-name=\"%s\", ret=%d",
3842 bt_ctf_clock_class_get_name(clock_class_to_map_to
),
3848 bt_put(clock_class_to_map_to
);
3849 bt_put(mapped_clock_class
);
3854 int auto_map_fields_to_trace_clock_class(struct ctx
*ctx
,
3855 struct bt_ctf_field_type
*root_ft
, const char *field_name
)
3864 if (!bt_ctf_field_type_is_structure(root_ft
) &&
3865 !bt_ctf_field_type_is_variant(root_ft
)) {
3869 if (bt_ctf_field_type_is_structure(root_ft
)) {
3870 count
= bt_ctf_field_type_structure_get_field_count(root_ft
);
3872 count
= bt_ctf_field_type_variant_get_field_count(root_ft
);
3877 for (i
= 0; i
< count
; i
++) {
3878 _BT_CTF_FIELD_TYPE_INIT(ft
);
3881 if (bt_ctf_field_type_is_structure(root_ft
)) {
3882 ret
= bt_ctf_field_type_structure_get_field_by_index(
3883 root_ft
, &name
, &ft
, i
);
3884 } else if (bt_ctf_field_type_is_variant(root_ft
)) {
3885 ret
= bt_ctf_field_type_variant_get_field_by_index(
3886 root_ft
, &name
, &ft
, i
);
3891 if (strcmp(name
, field_name
) == 0) {
3892 ret
= auto_map_field_to_trace_clock_class(ctx
, ft
);
3894 BT_LOGE("Cannot automatically map field to trace's clock class: "
3895 "field-name=\"%s\"", field_name
);
3901 ret
= auto_map_fields_to_trace_clock_class(ctx
, ft
, field_name
);
3904 BT_LOGE("Cannot automatically map structure or variant field type's fields to trace's clock class: "
3905 "field-name=\"%s\", root-field-name=\"%s\"",
3916 int visit_stream_decl_entry(struct ctx
*ctx
, struct ctf_node
*node
,
3917 struct bt_ctf_stream_class
*stream_class
, int *set
)
3921 _BT_CTF_FIELD_TYPE_INIT(decl
);
3923 switch (node
->type
) {
3925 ret
= visit_typedef(ctx
, node
->u
._typedef
.type_specifier_list
,
3926 &node
->u
._typedef
.type_declarators
);
3929 "Cannot add type definition found in stream class.");
3933 case NODE_TYPEALIAS
:
3934 ret
= visit_typealias(ctx
, node
->u
.typealias
.target
,
3935 node
->u
.typealias
.alias
);
3938 "Cannot add type alias found in stream class.");
3942 case NODE_CTF_EXPRESSION
:
3944 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
3946 _BT_LOGE_NODE(node
, "Cannot concatenate unary strings.");
3951 if (!strcmp(left
, "id")) {
3955 if (_IS_SET(set
, _STREAM_ID_SET
)) {
3956 _BT_LOGE_DUP_ATTR(node
, "id",
3957 "stream declaration");
3962 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
3964 /* Only read "id" if get_unary_unsigned() succeeded. */
3965 if (ret
|| (!ret
&& id
< 0)) {
3967 "Unexpected unary expression for stream class's `id` attribute.");
3972 ptr
= g_hash_table_lookup(ctx
->stream_classes
,
3976 "Duplicate stream class (same ID): id=%" PRId64
,
3982 ret
= bt_ctf_stream_class_set_id(stream_class
, id
);
3985 "Cannot set stream class's ID: "
3986 "id=%" PRId64
", ret=%d", id
, ret
);
3990 _SET(set
, _STREAM_ID_SET
);
3991 } else if (!strcmp(left
, "event.header")) {
3992 if (_IS_SET(set
, _STREAM_EVENT_HEADER_SET
)) {
3994 "Duplicate `event.header` entry in stream class.");
3999 ret
= visit_type_specifier_list(ctx
,
4000 _BT_LIST_FIRST_ENTRY(
4001 &node
->u
.ctf_expression
.right
,
4002 struct ctf_node
, siblings
),
4006 "Cannot create stream class's event header field type.");
4011 ret
= auto_map_fields_to_trace_clock_class(ctx
,
4015 "Cannot automatically map specific event header field type fields named `timestamp` to trace's clock class.");
4019 ret
= bt_ctf_stream_class_set_event_header_type(
4020 stream_class
, decl
);
4024 "Cannot set stream class's event header field type.");
4028 _SET(set
, _STREAM_EVENT_HEADER_SET
);
4029 } else if (!strcmp(left
, "event.context")) {
4030 if (_IS_SET(set
, _STREAM_EVENT_CONTEXT_SET
)) {
4032 "Duplicate `event.context` entry in stream class.");
4037 ret
= visit_type_specifier_list(ctx
,
4038 _BT_LIST_FIRST_ENTRY(
4039 &node
->u
.ctf_expression
.right
,
4040 struct ctf_node
, siblings
),
4044 "Cannot create stream class's event context field type.");
4050 ret
= bt_ctf_stream_class_set_event_context_type(
4051 stream_class
, decl
);
4055 "Cannot set stream class's event context field type.");
4059 _SET(set
, _STREAM_EVENT_CONTEXT_SET
);
4060 } else if (!strcmp(left
, "packet.context")) {
4061 if (_IS_SET(set
, _STREAM_PACKET_CONTEXT_SET
)) {
4063 "Duplicate `packet.context` entry in stream class.");
4068 ret
= visit_type_specifier_list(ctx
,
4069 _BT_LIST_FIRST_ENTRY(
4070 &node
->u
.ctf_expression
.right
,
4071 struct ctf_node
, siblings
),
4075 "Cannot create stream class's packet context field type.");
4080 ret
= auto_map_fields_to_trace_clock_class(ctx
,
4081 decl
, "timestamp_begin");
4084 "Cannot automatically map specific packet context field type fields named `timestamp_begin` to trace's clock class.");
4088 ret
= auto_map_fields_to_trace_clock_class(ctx
,
4089 decl
, "timestamp_end");
4092 "Cannot automatically map specific packet context field type fields named `timestamp_end` to trace's clock class.");
4096 ret
= bt_ctf_stream_class_set_packet_context_type(
4097 stream_class
, decl
);
4101 "Cannot set stream class's packet context field type.");
4105 _SET(set
, _STREAM_PACKET_CONTEXT_SET
);
4108 "Unknown attribute in stream class: "
4109 "attr-name=\"%s\"", left
);
4132 int visit_stream_decl(struct ctx
*ctx
, struct ctf_node
*node
)
4137 struct ctf_node
*iter
;
4138 struct bt_ctf_stream_class
*stream_class
= NULL
;
4139 struct bt_ctf_stream_class
*existing_stream_class
= NULL
;
4140 struct bt_list_head
*decl_list
= &node
->u
.stream
.declaration_list
;
4142 if (node
->visited
) {
4146 node
->visited
= TRUE
;
4147 stream_class
= create_reset_stream_class(ctx
);
4148 if (!stream_class
) {
4149 _BT_LOGE_NODE(node
, "Cannot create empty stream class.");
4154 ret
= ctx_push_scope(ctx
);
4156 BT_LOGE_STR("Cannot push scope.");
4160 bt_list_for_each_entry(iter
, decl_list
, siblings
) {
4161 ret
= visit_stream_decl_entry(ctx
, iter
, stream_class
, &set
);
4164 "Cannot visit stream class's entry: "
4173 if (_IS_SET(&set
, _STREAM_ID_SET
)) {
4174 /* Check that packet header has stream_id field */
4175 _BT_CTF_FIELD_TYPE_INIT(stream_id_decl
);
4176 _BT_CTF_FIELD_TYPE_INIT(packet_header_decl
);
4178 packet_header_decl
=
4179 bt_ctf_trace_get_packet_header_type(ctx
->trace
);
4180 if (!packet_header_decl
) {
4182 "Stream class has a `id` attribute, "
4183 "but trace has no packet header field type.");
4188 bt_ctf_field_type_structure_get_field_type_by_name(
4189 packet_header_decl
, "stream_id");
4190 BT_PUT(packet_header_decl
);
4191 if (!stream_id_decl
) {
4193 "Stream class has a `id` attribute, "
4194 "but trace's packet header field type has no `stream_id` field.");
4198 if (!bt_ctf_field_type_is_integer(stream_id_decl
)) {
4199 BT_PUT(stream_id_decl
);
4201 "Stream class has a `id` attribute, "
4202 "but trace's packet header field type's `stream_id` field is not an integer field type.");
4206 BT_PUT(stream_id_decl
);
4208 /* Allow only _one_ ID-less stream */
4209 if (g_hash_table_size(ctx
->stream_classes
) != 0) {
4211 "Missing `id` attribute in stream class as there's more than one stream class in the trace.");
4216 /* Automatic ID: 0 */
4217 ret
= bt_ctf_stream_class_set_id(stream_class
, 0);
4221 id
= bt_ctf_stream_class_get_id(stream_class
);
4224 "Cannot get stream class's ID.");
4230 * Make sure that this stream class's ID is currently unique in
4233 existing_stream_class
= bt_ctf_trace_get_stream_class_by_id(ctx
->trace
,
4235 if (g_hash_table_lookup(ctx
->stream_classes
, (gpointer
) id
) ||
4236 existing_stream_class
) {
4238 "Duplicate stream class (same ID): id=%" PRId64
,
4244 /* Move reference to visitor's context */
4245 g_hash_table_insert(ctx
->stream_classes
, (gpointer
) (int64_t) id
,
4247 stream_class
= NULL
;
4251 bt_put(stream_class
);
4254 bt_put(existing_stream_class
);
4259 int visit_trace_decl_entry(struct ctx
*ctx
, struct ctf_node
*node
, int *set
)
4263 _BT_CTF_FIELD_TYPE_INIT(packet_header_decl
);
4265 switch (node
->type
) {
4267 ret
= visit_typedef(ctx
, node
->u
._typedef
.type_specifier_list
,
4268 &node
->u
._typedef
.type_declarators
);
4271 "Cannot add type definition found in trace (`trace` block).");
4275 case NODE_TYPEALIAS
:
4276 ret
= visit_typealias(ctx
, node
->u
.typealias
.target
,
4277 node
->u
.typealias
.alias
);
4280 "Cannot add type alias found in trace (`trace` block).");
4284 case NODE_CTF_EXPRESSION
:
4286 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
4288 _BT_LOGE_NODE(node
, "Cannot concatenate unary strings.");
4293 if (!strcmp(left
, "major")) {
4294 if (_IS_SET(set
, _TRACE_MAJOR_SET
)) {
4295 _BT_LOGE_DUP_ATTR(node
, "major", "trace");
4300 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
4304 "Unexpected unary expression for trace's `major` attribute.");
4309 _SET(set
, _TRACE_MAJOR_SET
);
4310 } else if (!strcmp(left
, "minor")) {
4311 if (_IS_SET(set
, _TRACE_MINOR_SET
)) {
4312 _BT_LOGE_DUP_ATTR(node
, "minor", "trace");
4317 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
4321 "Unexpected unary expression for trace's `minor` attribute.");
4326 _SET(set
, _TRACE_MINOR_SET
);
4327 } else if (!strcmp(left
, "uuid")) {
4328 if (_IS_SET(set
, _TRACE_UUID_SET
)) {
4329 _BT_LOGE_DUP_ATTR(node
, "uuid", "trace");
4334 ret
= get_unary_uuid(&node
->u
.ctf_expression
.right
,
4338 "Invalid trace's `uuid` attribute.");
4342 ret
= bt_ctf_trace_set_uuid(ctx
->trace
, ctx
->trace_uuid
);
4344 _BT_LOGE_NODE(node
, "Cannot set trace's UUID.");
4348 _SET(set
, _TRACE_UUID_SET
);
4349 } else if (!strcmp(left
, "byte_order")) {
4350 /* Native byte order is already known at this stage */
4351 if (_IS_SET(set
, _TRACE_BYTE_ORDER_SET
)) {
4352 _BT_LOGE_DUP_ATTR(node
, "byte_order",
4358 _SET(set
, _TRACE_BYTE_ORDER_SET
);
4359 } else if (!strcmp(left
, "packet.header")) {
4360 if (_IS_SET(set
, _TRACE_PACKET_HEADER_SET
)) {
4362 "Duplicate `packet.header` entry in trace.");
4367 ret
= visit_type_specifier_list(ctx
,
4368 _BT_LIST_FIRST_ENTRY(
4369 &node
->u
.ctf_expression
.right
,
4370 struct ctf_node
, siblings
),
4371 &packet_header_decl
);
4374 "Cannot create trace's packet header field type.");
4378 assert(packet_header_decl
);
4379 ret
= bt_ctf_trace_set_packet_header_type(ctx
->trace
,
4380 packet_header_decl
);
4381 BT_PUT(packet_header_decl
);
4384 "Cannot set trace's packet header field type.");
4388 _SET(set
, _TRACE_PACKET_HEADER_SET
);
4391 "Unknown attribute in stream class: "
4392 "attr-name=\"%s\"", left
);
4400 _BT_LOGE_NODE(node
, "Unknown expression in trace.");
4409 BT_PUT(packet_header_decl
);
4415 int visit_trace_decl(struct ctx
*ctx
, struct ctf_node
*node
)
4419 struct ctf_node
*iter
;
4420 struct bt_list_head
*decl_list
= &node
->u
.trace
.declaration_list
;
4422 if (node
->visited
) {
4426 node
->visited
= TRUE
;
4428 if (ctx
->is_trace_visited
) {
4429 _BT_LOGE_NODE(node
, "Duplicate trace (`trace` block).");
4434 ret
= ctx_push_scope(ctx
);
4436 BT_LOGE_STR("Cannot push scope.");
4440 bt_list_for_each_entry(iter
, decl_list
, siblings
) {
4441 ret
= visit_trace_decl_entry(ctx
, iter
, &set
);
4443 _BT_LOGE_NODE(iter
, "Cannot visit trace's entry (`trace` block): "
4452 if (!_IS_SET(&set
, _TRACE_MAJOR_SET
)) {
4454 "Missing `major` attribute in trace (`trace` block).");
4459 if (!_IS_SET(&set
, _TRACE_MINOR_SET
)) {
4461 "Missing `minor` attribute in trace (`trace` block).");
4466 if (!_IS_SET(&set
, _TRACE_BYTE_ORDER_SET
)) {
4468 "Missing `byte_order` attribute in trace (`trace` block).");
4473 ctx
->is_trace_visited
= TRUE
;
4483 int visit_env(struct ctx
*ctx
, struct ctf_node
*node
)
4487 struct ctf_node
*entry_node
;
4488 struct bt_list_head
*decl_list
= &node
->u
.env
.declaration_list
;
4490 if (node
->visited
) {
4494 node
->visited
= TRUE
;
4496 bt_list_for_each_entry(entry_node
, decl_list
, siblings
) {
4497 struct bt_list_head
*right_head
=
4498 &entry_node
->u
.ctf_expression
.right
;
4500 if (entry_node
->type
!= NODE_CTF_EXPRESSION
) {
4501 _BT_LOGE_NODE(entry_node
,
4502 "Wrong expression in environment entry: "
4503 "node-type=%d", entry_node
->type
);
4508 left
= concatenate_unary_strings(
4509 &entry_node
->u
.ctf_expression
.left
);
4511 _BT_LOGE_NODE(entry_node
,
4512 "Cannot get environment entry's name.");
4517 if (is_unary_string(right_head
)) {
4518 char *right
= concatenate_unary_strings(right_head
);
4521 _BT_LOGE_NODE(entry_node
,
4522 "Unexpected unary expression for environment entry's value: "
4523 "name=\"%s\"", left
);
4528 if (strcmp(left
, "tracer_name") == 0) {
4529 if (strncmp(right
, "lttng", 5) == 0) {
4530 BT_LOGI("Detected LTTng trace from `%s` environment value: "
4531 "tracer-name=\"%s\"",
4537 ret
= bt_ctf_trace_set_environment_field_string(
4538 ctx
->trace
, left
, right
);
4542 _BT_LOGE_NODE(entry_node
,
4543 "Cannot add string environment entry to trace: "
4544 "name=\"%s\", ret=%d", left
, ret
);
4547 } else if (is_unary_unsigned(right_head
) ||
4548 is_unary_signed(right_head
)) {
4551 if (is_unary_unsigned(right_head
)) {
4552 ret
= get_unary_unsigned(right_head
,
4555 ret
= get_unary_signed(right_head
, &v
);
4558 _BT_LOGE_NODE(entry_node
,
4559 "Unexpected unary expression for environment entry's value: "
4560 "name=\"%s\"", left
);
4565 ret
= bt_ctf_trace_set_environment_field_integer(
4566 ctx
->trace
, left
, v
);
4568 _BT_LOGE_NODE(entry_node
,
4569 "Cannot add integer environment entry to trace: "
4570 "name=\"%s\", ret=%d", left
, ret
);
4574 _BT_LOGW_NODE(entry_node
,
4575 "Environment entry has unknown type: "
4576 "name=\"%s\"", left
);
4593 int set_trace_byte_order(struct ctx
*ctx
, struct ctf_node
*trace_node
)
4598 struct ctf_node
*node
;
4599 struct bt_list_head
*decl_list
= &trace_node
->u
.trace
.declaration_list
;
4601 bt_list_for_each_entry(node
, decl_list
, siblings
) {
4602 if (node
->type
== NODE_CTF_EXPRESSION
) {
4603 struct ctf_node
*right_node
;
4605 left
= concatenate_unary_strings(
4606 &node
->u
.ctf_expression
.left
);
4609 "Cannot concatenate unary strings.");
4614 if (!strcmp(left
, "byte_order")) {
4615 enum bt_ctf_byte_order bo
;
4617 if (_IS_SET(&set
, _TRACE_BYTE_ORDER_SET
)) {
4618 _BT_LOGE_DUP_ATTR(node
, "byte_order",
4624 _SET(&set
, _TRACE_BYTE_ORDER_SET
);
4625 right_node
= _BT_LIST_FIRST_ENTRY(
4626 &node
->u
.ctf_expression
.right
,
4627 struct ctf_node
, siblings
);
4628 bo
= byte_order_from_unary_expr(right_node
);
4629 if (bo
== BT_CTF_BYTE_ORDER_UNKNOWN
) {
4631 "Invalid `byte_order` attribute in trace (`trace` block): "
4632 "expecting `le`, `be`, or `network`.");
4635 } else if (bo
== BT_CTF_BYTE_ORDER_NATIVE
) {
4637 "Invalid `byte_order` attribute in trace (`trace` block): "
4638 "cannot be set to `native` here.");
4644 ret
= bt_ctf_trace_set_native_byte_order(
4648 "Cannot set trace's byte order: "
4659 if (!_IS_SET(&set
, _TRACE_BYTE_ORDER_SET
)) {
4660 _BT_LOGE_NODE(trace_node
,
4661 "Missing `byte_order` attribute in trace (`trace` block).");
4675 int visit_clock_decl_entry(struct ctx
*ctx
, struct ctf_node
*entry_node
,
4676 struct bt_ctf_clock_class
*clock
, int *set
)
4681 if (entry_node
->type
!= NODE_CTF_EXPRESSION
) {
4682 _BT_LOGE_NODE(entry_node
,
4683 "Unexpected node type: node-type=%d",
4689 left
= concatenate_unary_strings(&entry_node
->u
.ctf_expression
.left
);
4691 _BT_LOGE_NODE(entry_node
, "Cannot concatenate unary strings.");
4696 if (!strcmp(left
, "name")) {
4699 if (_IS_SET(set
, _CLOCK_NAME_SET
)) {
4700 _BT_LOGE_DUP_ATTR(entry_node
, "name", "clock class");
4705 right
= concatenate_unary_strings(
4706 &entry_node
->u
.ctf_expression
.right
);
4708 _BT_LOGE_NODE(entry_node
,
4709 "Unexpected unary expression for clock class's `name` attribute.");
4714 ret
= bt_ctf_clock_class_set_name(clock
, right
);
4716 _BT_LOGE_NODE(entry_node
,
4717 "cannot set clock class's name");
4723 _SET(set
, _CLOCK_NAME_SET
);
4724 } else if (!strcmp(left
, "uuid")) {
4725 unsigned char uuid
[BABELTRACE_UUID_LEN
];
4727 if (_IS_SET(set
, _CLOCK_UUID_SET
)) {
4728 _BT_LOGE_DUP_ATTR(entry_node
, "uuid", "clock class");
4733 ret
= get_unary_uuid(&entry_node
->u
.ctf_expression
.right
, uuid
);
4735 _BT_LOGE_NODE(entry_node
,
4736 "Invalid clock class's `uuid` attribute.");
4740 ret
= bt_ctf_clock_class_set_uuid(clock
, uuid
);
4742 _BT_LOGE_NODE(entry_node
,
4743 "Cannot set clock class's UUID.");
4747 _SET(set
, _CLOCK_UUID_SET
);
4748 } else if (!strcmp(left
, "description")) {
4751 if (_IS_SET(set
, _CLOCK_DESCRIPTION_SET
)) {
4752 _BT_LOGE_DUP_ATTR(entry_node
, "description",
4758 right
= concatenate_unary_strings(
4759 &entry_node
->u
.ctf_expression
.right
);
4761 _BT_LOGE_NODE(entry_node
,
4762 "Unexpected unary expression for clock class's `description` attribute.");
4767 ret
= bt_ctf_clock_class_set_description(clock
, right
);
4769 _BT_LOGE_NODE(entry_node
,
4770 "Cannot set clock class's description.");
4776 _SET(set
, _CLOCK_DESCRIPTION_SET
);
4777 } else if (!strcmp(left
, "freq")) {
4780 if (_IS_SET(set
, _CLOCK_FREQ_SET
)) {
4781 _BT_LOGE_DUP_ATTR(entry_node
, "freq", "clock class");
4786 ret
= get_unary_unsigned(
4787 &entry_node
->u
.ctf_expression
.right
, &freq
);
4789 _BT_LOGE_NODE(entry_node
,
4790 "Unexpected unary expression for clock class's `freq` attribute.");
4795 ret
= bt_ctf_clock_class_set_frequency(clock
, freq
);
4797 _BT_LOGE_NODE(entry_node
,
4798 "Cannot set clock class's frequency.");
4802 _SET(set
, _CLOCK_FREQ_SET
);
4803 } else if (!strcmp(left
, "precision")) {
4806 if (_IS_SET(set
, _CLOCK_PRECISION_SET
)) {
4807 _BT_LOGE_DUP_ATTR(entry_node
, "precision",
4813 ret
= get_unary_unsigned(
4814 &entry_node
->u
.ctf_expression
.right
, &precision
);
4816 _BT_LOGE_NODE(entry_node
,
4817 "Unexpected unary expression for clock class's `precision` attribute.");
4822 ret
= bt_ctf_clock_class_set_precision(clock
, precision
);
4824 _BT_LOGE_NODE(entry_node
,
4825 "Cannot set clock class's precision.");
4829 _SET(set
, _CLOCK_PRECISION_SET
);
4830 } else if (!strcmp(left
, "offset_s")) {
4833 if (_IS_SET(set
, _CLOCK_OFFSET_S_SET
)) {
4834 _BT_LOGE_DUP_ATTR(entry_node
, "offset_s",
4840 ret
= get_unary_unsigned(
4841 &entry_node
->u
.ctf_expression
.right
, &offset_s
);
4843 _BT_LOGE_NODE(entry_node
,
4844 "Unexpected unary expression for clock class's `offset_s` attribute.");
4849 ret
= bt_ctf_clock_class_set_offset_s(clock
, offset_s
);
4851 _BT_LOGE_NODE(entry_node
,
4852 "Cannot set clock class's offset in seconds.");
4856 _SET(set
, _CLOCK_OFFSET_S_SET
);
4857 } else if (!strcmp(left
, "offset")) {
4860 if (_IS_SET(set
, _CLOCK_OFFSET_SET
)) {
4861 _BT_LOGE_DUP_ATTR(entry_node
, "offset", "clock class");
4866 ret
= get_unary_unsigned(
4867 &entry_node
->u
.ctf_expression
.right
, &offset
);
4869 _BT_LOGE_NODE(entry_node
,
4870 "Unexpected unary expression for clock class's `offset` attribute.");
4875 ret
= bt_ctf_clock_class_set_offset_cycles(clock
, offset
);
4877 _BT_LOGE_NODE(entry_node
,
4878 "Cannot set clock class's offset in cycles.");
4882 _SET(set
, _CLOCK_OFFSET_SET
);
4883 } else if (!strcmp(left
, "absolute")) {
4884 struct ctf_node
*right
;
4886 if (_IS_SET(set
, _CLOCK_ABSOLUTE_SET
)) {
4887 _BT_LOGE_DUP_ATTR(entry_node
, "absolute",
4893 right
= _BT_LIST_FIRST_ENTRY(
4894 &entry_node
->u
.ctf_expression
.right
,
4895 struct ctf_node
, siblings
);
4896 ret
= get_boolean(right
);
4898 _BT_LOGE_NODE(entry_node
,
4899 "Unexpected unary expression for clock class's `absolute` attribute.");
4904 ret
= bt_ctf_clock_class_set_is_absolute(clock
, ret
);
4906 _BT_LOGE_NODE(entry_node
,
4907 "Cannot set clock class's absolute flag.");
4911 _SET(set
, _CLOCK_ABSOLUTE_SET
);
4913 _BT_LOGW_NODE(entry_node
,
4914 "Unknown attribute in clock class: attr-name=\"%s\"",
4930 int64_t cycles_from_ns(uint64_t frequency
, int64_t ns
)
4935 if (frequency
== 1000000000ULL) {
4938 cycles
= (uint64_t) (((double) ns
* (double) frequency
) / 1e9
);
4945 int apply_clock_class_offset(struct ctx
*ctx
, struct bt_ctf_clock_class
*clock
)
4949 int64_t offset_cycles
;
4950 int64_t offset_to_apply
;
4952 freq
= bt_ctf_clock_class_get_frequency(clock
);
4953 if (freq
== -1ULL) {
4954 BT_LOGE_STR("Cannot get clock class's frequency.");
4959 ret
= bt_ctf_clock_class_get_offset_cycles(clock
, &offset_cycles
);
4961 BT_LOGE_STR("Cannot get clock class's offset in cycles.");
4967 ctx
->decoder_config
.clock_class_offset_s
* 1000000000LL +
4968 ctx
->decoder_config
.clock_class_offset_ns
;
4969 offset_cycles
+= cycles_from_ns(freq
, offset_to_apply
);
4970 ret
= bt_ctf_clock_class_set_offset_cycles(clock
, offset_cycles
);
4977 int visit_clock_decl(struct ctx
*ctx
, struct ctf_node
*clock_node
)
4981 struct bt_ctf_clock_class
*clock
;
4982 struct ctf_node
*entry_node
;
4983 struct bt_list_head
*decl_list
= &clock_node
->u
.clock
.declaration_list
;
4984 const char *clock_class_name
;
4986 if (clock_node
->visited
) {
4990 clock_node
->visited
= TRUE
;
4991 clock
= bt_ctf_clock_class_create(NULL
);
4993 _BT_LOGE_NODE(clock_node
,
4994 "Cannot create default clock class.");
4999 bt_list_for_each_entry(entry_node
, decl_list
, siblings
) {
5000 ret
= visit_clock_decl_entry(ctx
, entry_node
, clock
, &set
);
5002 _BT_LOGE_NODE(entry_node
,
5003 "Cannot visit clock class's entry: ret=%d",
5009 if (!_IS_SET(&set
, _CLOCK_NAME_SET
)) {
5010 _BT_LOGE_NODE(clock_node
,
5011 "Missing `name` attribute in clock class.");
5016 clock_class_name
= bt_ctf_clock_class_get_name(clock
);
5017 assert(clock_class_name
);
5018 if (ctx
->is_lttng
&& strcmp(clock_class_name
, "monotonic") == 0) {
5020 * Old versions of LTTng forgot to set its clock class
5021 * as absolute, even if it is. This is important because
5022 * it's a condition to be able to sort notifications
5023 * from different sources.
5025 ret
= bt_ctf_clock_class_set_is_absolute(clock
, 1);
5027 _BT_LOGE_NODE(clock_node
,
5028 "Cannot set clock class's absolute flag.");
5033 ret
= apply_clock_class_offset(ctx
, clock
);
5035 _BT_LOGE_NODE(clock_node
,
5036 "Cannot apply clock class's custom offset.");
5040 ret
= bt_ctf_trace_add_clock_class(ctx
->trace
, clock
);
5042 _BT_LOGE_NODE(clock_node
,
5043 "Cannot add clock class to trace.");
5054 int visit_root_decl(struct ctx
*ctx
, struct ctf_node
*root_decl_node
)
5058 if (root_decl_node
->visited
) {
5062 root_decl_node
->visited
= TRUE
;
5064 switch (root_decl_node
->type
) {
5066 ret
= visit_typedef(ctx
,
5067 root_decl_node
->u
._typedef
.type_specifier_list
,
5068 &root_decl_node
->u
._typedef
.type_declarators
);
5070 _BT_LOGE_NODE(root_decl_node
,
5071 "Cannot add type definition found in root scope.");
5075 case NODE_TYPEALIAS
:
5076 ret
= visit_typealias(ctx
, root_decl_node
->u
.typealias
.target
,
5077 root_decl_node
->u
.typealias
.alias
);
5079 _BT_LOGE_NODE(root_decl_node
,
5080 "Cannot add type alias found in root scope.");
5084 case NODE_TYPE_SPECIFIER_LIST
:
5086 _BT_CTF_FIELD_TYPE_INIT(decl
);
5089 * Just add the type specifier to the root
5090 * declaration scope. Put local reference.
5092 ret
= visit_type_specifier_list(ctx
, root_decl_node
, &decl
);
5094 _BT_LOGE_NODE(root_decl_node
,
5095 "Cannot visit root scope's field type: "
5105 _BT_LOGE_NODE(root_decl_node
,
5106 "Unexpected node type: node-type=%d",
5107 root_decl_node
->type
);
5117 int set_trace_name(struct ctx
*ctx
)
5121 struct bt_value
*value
= NULL
;
5123 assert(bt_ctf_trace_get_stream_class_count(ctx
->trace
) == 0);
5124 name
= g_string_new(NULL
);
5126 BT_LOGE_STR("Failed to allocate a GString.");
5132 * Check if we have a trace environment string value named `hostname`.
5133 * If so, use it as the trace name's prefix.
5135 value
= bt_ctf_trace_get_environment_field_value_by_name(ctx
->trace
,
5137 if (bt_value_is_string(value
)) {
5138 const char *hostname
;
5140 ret
= bt_value_string_get(value
, &hostname
);
5142 g_string_append(name
, hostname
);
5144 if (ctx
->trace_name_suffix
) {
5145 g_string_append_c(name
, G_DIR_SEPARATOR
);
5149 if (ctx
->trace_name_suffix
) {
5150 g_string_append(name
, ctx
->trace_name_suffix
);
5153 ret
= bt_ctf_trace_set_name(ctx
->trace
, name
->str
);
5155 BT_LOGE("Cannot set trace's name: name=\"%s\"", name
->str
);
5168 g_string_free(name
, TRUE
);
5175 int move_ctx_stream_classes_to_trace(struct ctx
*ctx
)
5178 GHashTableIter iter
;
5179 gpointer key
, stream_class
;
5181 if (g_hash_table_size(ctx
->stream_classes
) > 0 &&
5182 bt_ctf_trace_get_stream_class_count(ctx
->trace
) == 0) {
5184 * We're about to add the first stream class to the
5185 * trace. This will freeze the trace, and after this
5186 * we cannot set the name anymore. At this point,
5187 * set the trace name.
5189 ret
= set_trace_name(ctx
);
5191 BT_LOGE_STR("Cannot set trace's name.");
5196 g_hash_table_iter_init(&iter
, ctx
->stream_classes
);
5198 while (g_hash_table_iter_next(&iter
, &key
, &stream_class
)) {
5199 ret
= bt_ctf_trace_add_stream_class(ctx
->trace
,
5202 int64_t id
= bt_ctf_stream_class_get_id(stream_class
);
5203 BT_LOGE("Cannot add stream class to trace: id=%" PRId64
,
5209 g_hash_table_remove_all(ctx
->stream_classes
);
5216 struct ctf_visitor_generate_ir
*ctf_visitor_generate_ir_create(
5217 const struct ctf_metadata_decoder_config
*decoder_config
,
5221 struct ctx
*ctx
= NULL
;
5222 struct bt_ctf_trace
*trace
;
5224 trace
= bt_ctf_trace_create();
5226 BT_LOGE_STR("Cannot create empty trace.");
5230 /* Set packet header to NULL to override the default one */
5231 ret
= bt_ctf_trace_set_packet_header_type(trace
, NULL
);
5233 BT_LOGE_STR("Cannot reset initial trace's packet header field type.");
5237 /* Create visitor's context */
5238 ctx
= ctx_create(trace
, decoder_config
, name
);
5240 BT_LOGE_STR("Cannot create visitor's context.");
5253 return (void *) ctx
;
5257 void ctf_visitor_generate_ir_destroy(struct ctf_visitor_generate_ir
*visitor
)
5259 ctx_destroy((void *) visitor
);
5263 struct bt_ctf_trace
*ctf_visitor_generate_ir_get_trace(
5264 struct ctf_visitor_generate_ir
*visitor
)
5266 struct ctx
*ctx
= (void *) visitor
;
5270 return bt_get(ctx
->trace
);
5274 int ctf_visitor_generate_ir_visit_node(struct ctf_visitor_generate_ir
*visitor
,
5275 struct ctf_node
*node
)
5278 struct ctx
*ctx
= (void *) visitor
;
5280 BT_LOGI_STR("Visiting metadata's AST to generate CTF IR objects.");
5282 switch (node
->type
) {
5285 struct ctf_node
*iter
;
5286 int got_trace_decl
= FALSE
;
5289 * The first thing we need is the native byte order of
5290 * the trace block, because early type aliases can have
5291 * a `byte_order` attribute set to `native`. If we don't
5292 * have the native byte order yet, and we don't have any
5293 * trace block yet, then fail with EINCOMPLETE.
5295 if (ctx
->trace_bo
== BT_CTF_BYTE_ORDER_NATIVE
) {
5296 bt_list_for_each_entry(iter
, &node
->u
.root
.trace
, siblings
) {
5297 if (got_trace_decl
) {
5299 "Duplicate trace (`trace` block).");
5304 ret
= set_trace_byte_order(ctx
, iter
);
5307 "Cannot set trace's native byte order: "
5312 got_trace_decl
= TRUE
;
5315 if (!got_trace_decl
) {
5316 BT_LOGD_STR("Incomplete AST: need trace (`trace` block).");
5322 assert(ctx
->trace_bo
== BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
||
5323 ctx
->trace_bo
== BT_CTF_BYTE_ORDER_BIG_ENDIAN
);
5324 assert(ctx
->current_scope
&&
5325 ctx
->current_scope
->parent_scope
== NULL
);
5328 bt_list_for_each_entry(iter
, &node
->u
.root
.env
, siblings
) {
5329 ret
= visit_env(ctx
, iter
);
5332 "Cannot visit trace's environment (`env` block) entry: "
5338 assert(ctx
->current_scope
&&
5339 ctx
->current_scope
->parent_scope
== NULL
);
5342 * Visit clock blocks.
5344 bt_list_for_each_entry(iter
, &node
->u
.root
.clock
, siblings
) {
5345 ret
= visit_clock_decl(ctx
, iter
);
5348 "Cannot visit clock class: ret=%d",
5354 assert(ctx
->current_scope
&&
5355 ctx
->current_scope
->parent_scope
== NULL
);
5358 * Visit root declarations next, as they can be used by any
5361 bt_list_for_each_entry(iter
, &node
->u
.root
.declaration_list
,
5363 ret
= visit_root_decl(ctx
, iter
);
5366 "Cannot visit root entry: ret=%d",
5372 assert(ctx
->current_scope
&&
5373 ctx
->current_scope
->parent_scope
== NULL
);
5375 /* Callsite blocks are not supported */
5376 bt_list_for_each_entry(iter
, &node
->u
.root
.callsite
, siblings
) {
5378 "\"callsite\" blocks are not supported as of this version.");
5381 assert(ctx
->current_scope
&&
5382 ctx
->current_scope
->parent_scope
== NULL
);
5385 bt_list_for_each_entry(iter
, &node
->u
.root
.trace
, siblings
) {
5386 ret
= visit_trace_decl(ctx
, iter
);
5389 "Cannot visit trace (`trace` block): "
5395 assert(ctx
->current_scope
&&
5396 ctx
->current_scope
->parent_scope
== NULL
);
5399 bt_list_for_each_entry(iter
, &node
->u
.root
.stream
, siblings
) {
5400 ret
= visit_stream_decl(ctx
, iter
);
5403 "Cannot visit stream class: ret=%d",
5409 assert(ctx
->current_scope
&&
5410 ctx
->current_scope
->parent_scope
== NULL
);
5413 bt_list_for_each_entry(iter
, &node
->u
.root
.event
, siblings
) {
5414 ret
= visit_event_decl(ctx
, iter
);
5417 "Cannot visit event class: ret=%d",
5423 assert(ctx
->current_scope
&&
5424 ctx
->current_scope
->parent_scope
== NULL
);
5429 "Unexpected node type: node-type=%d",
5435 /* Move decoded stream classes to trace, if any */
5436 ret
= move_ctx_stream_classes_to_trace(ctx
);
5438 BT_LOGE("Cannot move stream classes to trace: ret=%d", ret
);