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_int64_hash
,
596 g_int64_equal
, g_free
, (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_SIGNED_CONSTANT
) ||
887 uexpr_link
!= UNARY_LINK_UNKNOWN
|| i
!= 0;
893 switch (uexpr_type
) {
894 case UNARY_UNSIGNED_CONSTANT
:
896 node
->u
.unary_expression
.u
.unsigned_constant
;
898 case UNARY_SIGNED_CONSTANT
:
899 *value
= node
->u
.unary_expression
.u
.signed_constant
;
914 int get_unary_uuid(struct bt_list_head
*head
, unsigned char *uuid
)
918 struct ctf_node
*node
;
920 bt_list_for_each_entry(node
, head
, siblings
) {
921 int uexpr_type
= node
->u
.unary_expression
.type
;
922 int uexpr_link
= node
->u
.unary_expression
.link
;
923 const char *src_string
;
925 if (node
->type
!= NODE_UNARY_EXPRESSION
||
926 uexpr_type
!= UNARY_STRING
||
927 uexpr_link
!= UNARY_LINK_UNKNOWN
||
933 src_string
= node
->u
.unary_expression
.u
.string
;
934 ret
= bt_uuid_parse(src_string
, uuid
);
937 "Cannot parse UUID: uuid=\"%s\"", src_string
);
947 int get_boolean(struct ctf_node
*unary_expr
)
951 if (unary_expr
->type
!= NODE_UNARY_EXPRESSION
) {
952 _BT_LOGE_NODE(unary_expr
,
953 "Expecting unary expression: node-type=%d",
959 switch (unary_expr
->u
.unary_expression
.type
) {
960 case UNARY_UNSIGNED_CONSTANT
:
961 ret
= (unary_expr
->u
.unary_expression
.u
.unsigned_constant
!= 0);
963 case UNARY_SIGNED_CONSTANT
:
964 ret
= (unary_expr
->u
.unary_expression
.u
.signed_constant
!= 0);
968 const char *str
= unary_expr
->u
.unary_expression
.u
.string
;
970 if (!strcmp(str
, "true") || !strcmp(str
, "TRUE")) {
972 } else if (!strcmp(str
, "false") || !strcmp(str
, "FALSE")) {
975 _BT_LOGE_NODE(unary_expr
,
976 "Unexpected boolean value: value=\"%s\"", str
);
983 _BT_LOGE_NODE(unary_expr
,
984 "Unexpected unary expression type: node-type=%d",
985 unary_expr
->u
.unary_expression
.type
);
995 enum bt_ctf_byte_order
byte_order_from_unary_expr(struct ctf_node
*unary_expr
)
998 enum bt_ctf_byte_order bo
= BT_CTF_BYTE_ORDER_UNKNOWN
;
1000 if (unary_expr
->u
.unary_expression
.type
!= UNARY_STRING
) {
1001 _BT_LOGE_NODE(unary_expr
,
1002 "\"byte_order\" attribute: expecting `be`, `le`, `network`, or `native`.");
1006 str
= unary_expr
->u
.unary_expression
.u
.string
;
1008 if (!strcmp(str
, "be") || !strcmp(str
, "network")) {
1009 bo
= BT_CTF_BYTE_ORDER_BIG_ENDIAN
;
1010 } else if (!strcmp(str
, "le")) {
1011 bo
= BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
;
1012 } else if (!strcmp(str
, "native")) {
1013 bo
= BT_CTF_BYTE_ORDER_NATIVE
;
1015 _BT_LOGE_NODE(unary_expr
,
1016 "Unexpected \"byte_order\" attribute value: "
1017 "expecting `be`, `le`, `network`, or `native`: value=\"%s\"",
1027 enum bt_ctf_byte_order
get_real_byte_order(struct ctx
*ctx
,
1028 struct ctf_node
*uexpr
)
1030 enum bt_ctf_byte_order bo
= byte_order_from_unary_expr(uexpr
);
1032 if (bo
== BT_CTF_BYTE_ORDER_NATIVE
) {
1033 bo
= bt_ctf_trace_get_native_byte_order(ctx
->trace
);
1040 int is_align_valid(uint64_t align
)
1042 return (align
!= 0) && !(align
& (align
- 1));
1046 int get_type_specifier_name(struct ctx
*ctx
, struct ctf_node
*type_specifier
,
1051 if (type_specifier
->type
!= NODE_TYPE_SPECIFIER
) {
1052 _BT_LOGE_NODE(type_specifier
,
1053 "Unexpected node type: node-type=%d",
1054 type_specifier
->type
);
1059 switch (type_specifier
->u
.type_specifier
.type
) {
1061 g_string_append(str
, "void");
1064 g_string_append(str
, "char");
1066 case TYPESPEC_SHORT
:
1067 g_string_append(str
, "short");
1070 g_string_append(str
, "int");
1073 g_string_append(str
, "long");
1075 case TYPESPEC_FLOAT
:
1076 g_string_append(str
, "float");
1078 case TYPESPEC_DOUBLE
:
1079 g_string_append(str
, "double");
1081 case TYPESPEC_SIGNED
:
1082 g_string_append(str
, "signed");
1084 case TYPESPEC_UNSIGNED
:
1085 g_string_append(str
, "unsigned");
1088 g_string_append(str
, "bool");
1090 case TYPESPEC_COMPLEX
:
1091 g_string_append(str
, "_Complex");
1093 case TYPESPEC_IMAGINARY
:
1094 g_string_append(str
, "_Imaginary");
1096 case TYPESPEC_CONST
:
1097 g_string_append(str
, "const");
1099 case TYPESPEC_ID_TYPE
:
1100 if (type_specifier
->u
.type_specifier
.id_type
) {
1101 g_string_append(str
,
1102 type_specifier
->u
.type_specifier
.id_type
);
1105 case TYPESPEC_STRUCT
:
1107 struct ctf_node
*node
= type_specifier
->u
.type_specifier
.node
;
1109 if (!node
->u
._struct
.name
) {
1110 _BT_LOGE_NODE(node
, "Unexpected empty structure field type name.");
1115 g_string_append(str
, "struct ");
1116 g_string_append(str
, node
->u
._struct
.name
);
1119 case TYPESPEC_VARIANT
:
1121 struct ctf_node
*node
= type_specifier
->u
.type_specifier
.node
;
1123 if (!node
->u
.variant
.name
) {
1124 _BT_LOGE_NODE(node
, "Unexpected empty variant field type name.");
1129 g_string_append(str
, "variant ");
1130 g_string_append(str
, node
->u
.variant
.name
);
1135 struct ctf_node
*node
= type_specifier
->u
.type_specifier
.node
;
1137 if (!node
->u
._enum
.enum_id
) {
1139 "Unexpected empty enumeration field type (`enum`) name.");
1144 g_string_append(str
, "enum ");
1145 g_string_append(str
, node
->u
._enum
.enum_id
);
1148 case TYPESPEC_FLOATING_POINT
:
1149 case TYPESPEC_INTEGER
:
1150 case TYPESPEC_STRING
:
1152 _BT_LOGE_NODE(type_specifier
->u
.type_specifier
.node
,
1153 "Unexpected type specifier type: %d",
1154 type_specifier
->u
.type_specifier
.type
);
1164 int get_type_specifier_list_name(struct ctx
*ctx
,
1165 struct ctf_node
*type_specifier_list
, GString
*str
)
1168 struct ctf_node
*iter
;
1169 int alias_item_nr
= 0;
1170 struct bt_list_head
*head
=
1171 &type_specifier_list
->u
.type_specifier_list
.head
;
1173 bt_list_for_each_entry(iter
, head
, siblings
) {
1174 if (alias_item_nr
!= 0) {
1175 g_string_append(str
, " ");
1179 ret
= get_type_specifier_name(ctx
, iter
, str
);
1190 GQuark
create_typealias_identifier(struct ctx
*ctx
,
1191 struct ctf_node
*type_specifier_list
,
1192 struct ctf_node
*node_type_declarator
)
1198 struct ctf_node
*iter
;
1199 struct bt_list_head
*pointers
=
1200 &node_type_declarator
->u
.type_declarator
.pointers
;
1202 str
= g_string_new("");
1203 ret
= get_type_specifier_list_name(ctx
, type_specifier_list
, str
);
1205 g_string_free(str
, TRUE
);
1209 bt_list_for_each_entry(iter
, pointers
, siblings
) {
1210 g_string_append(str
, " *");
1212 if (iter
->u
.pointer
.const_qualifier
) {
1213 g_string_append(str
, " const");
1217 str_c
= g_string_free(str
, FALSE
);
1218 qalias
= g_quark_from_string(str_c
);
1226 int visit_type_declarator(struct ctx
*ctx
, struct ctf_node
*type_specifier_list
,
1227 GQuark
*field_name
, struct ctf_node
*node_type_declarator
,
1228 struct bt_ctf_field_type
**field_decl
,
1229 struct bt_ctf_field_type
*nested_decl
)
1232 * During this whole function, nested_decl is always OURS,
1233 * whereas field_decl is an output which we create, but
1234 * belongs to the caller (it is moved).
1240 /* Validate type declarator node */
1241 if (node_type_declarator
) {
1242 if (node_type_declarator
->u
.type_declarator
.type
==
1244 _BT_LOGE_NODE(node_type_declarator
,
1245 "Unexpected type declarator type: type=%d",
1246 node_type_declarator
->u
.type_declarator
.type
);
1251 /* TODO: GCC bitfields not supported yet */
1252 if (node_type_declarator
->u
.type_declarator
.bitfield_len
!=
1254 _BT_LOGE_NODE(node_type_declarator
,
1255 "GCC bitfields are not supported as of this version.");
1261 /* Find the right nested declaration if not provided */
1263 struct bt_list_head
*pointers
=
1264 &node_type_declarator
->u
.type_declarator
.pointers
;
1266 if (node_type_declarator
&& !bt_list_empty(pointers
)) {
1268 _BT_CTF_FIELD_TYPE_INIT(nested_decl_copy
);
1271 * If we have a pointer declarator, it HAS to
1272 * be present in the typealiases (else fail).
1274 qalias
= create_typealias_identifier(ctx
,
1275 type_specifier_list
, node_type_declarator
);
1277 ctx_decl_scope_lookup_alias(ctx
->current_scope
,
1278 g_quark_to_string(qalias
), -1);
1280 _BT_LOGE_NODE(node_type_declarator
,
1281 "Cannot find type alias: name=\"%s\"",
1282 g_quark_to_string(qalias
));
1287 /* Make a copy of it */
1288 nested_decl_copy
= bt_ctf_field_type_copy(nested_decl
);
1289 BT_PUT(nested_decl
);
1290 if (!nested_decl_copy
) {
1291 _BT_LOGE_NODE(node_type_declarator
,
1292 "Cannot copy nested field type.");
1297 BT_MOVE(nested_decl
, nested_decl_copy
);
1299 /* Force integer's base to 16 since it's a pointer */
1300 if (bt_ctf_field_type_is_integer(nested_decl
)) {
1301 ret
= bt_ctf_field_type_integer_set_base(
1303 BT_CTF_INTEGER_BASE_HEXADECIMAL
);
1307 ret
= visit_type_specifier_list(ctx
,
1308 type_specifier_list
, &nested_decl
);
1310 assert(!nested_decl
);
1316 assert(nested_decl
);
1318 if (!node_type_declarator
) {
1319 BT_MOVE(*field_decl
, nested_decl
);
1323 if (node_type_declarator
->u
.type_declarator
.type
== TYPEDEC_ID
) {
1324 if (node_type_declarator
->u
.type_declarator
.u
.id
) {
1326 node_type_declarator
->u
.type_declarator
.u
.id
;
1328 *field_name
= g_quark_from_string(id
);
1333 BT_MOVE(*field_decl
, nested_decl
);
1336 struct ctf_node
*first
;
1337 _BT_CTF_FIELD_TYPE_INIT(decl
);
1338 _BT_CTF_FIELD_TYPE_INIT(outer_field_decl
);
1339 struct bt_list_head
*length
=
1340 &node_type_declarator
->
1341 u
.type_declarator
.u
.nested
.length
;
1343 /* Create array/sequence, pass nested_decl as child */
1344 if (bt_list_empty(length
)) {
1345 _BT_LOGE_NODE(node_type_declarator
,
1346 "Expecting length field reference or value.");
1351 first
= _BT_LIST_FIRST_ENTRY(length
, struct ctf_node
, siblings
);
1352 if (first
->type
!= NODE_UNARY_EXPRESSION
) {
1353 _BT_LOGE_NODE(first
,
1354 "Unexpected node type: node-type=%d",
1360 switch (first
->u
.unary_expression
.type
) {
1361 case UNARY_UNSIGNED_CONSTANT
:
1364 _BT_CTF_FIELD_TYPE_INIT(array_decl
);
1366 len
= first
->u
.unary_expression
.u
.unsigned_constant
;
1367 array_decl
= bt_ctf_field_type_array_create(nested_decl
,
1369 BT_PUT(nested_decl
);
1371 _BT_LOGE_NODE(first
,
1372 "Cannot create array field type.");
1377 BT_MOVE(decl
, array_decl
);
1382 /* Lookup unsigned integer definition, create seq. */
1383 _BT_CTF_FIELD_TYPE_INIT(seq_decl
);
1384 char *length_name
= concatenate_unary_strings(length
);
1387 _BT_LOGE_NODE(node_type_declarator
,
1388 "Cannot concatenate unary strings.");
1393 seq_decl
= bt_ctf_field_type_sequence_create(
1394 nested_decl
, length_name
);
1395 g_free(length_name
);
1396 BT_PUT(nested_decl
);
1398 _BT_LOGE_NODE(node_type_declarator
,
1399 "Cannot create sequence field type.");
1404 BT_MOVE(decl
, seq_decl
);
1412 assert(!nested_decl
);
1414 assert(!*field_decl
);
1417 * At this point, we found the next nested declaration.
1418 * We currently own this (and lost the ownership of
1419 * nested_decl in the meantime). Pass this next
1420 * nested declaration as the content of the outer
1421 * container, MOVING its ownership.
1423 ret
= visit_type_declarator(ctx
, type_specifier_list
,
1425 node_type_declarator
->
1426 u
.type_declarator
.u
.nested
.type_declarator
,
1427 &outer_field_decl
, decl
);
1430 assert(!outer_field_decl
);
1435 assert(outer_field_decl
);
1436 BT_MOVE(*field_decl
, outer_field_decl
);
1440 BT_PUT(nested_decl
);
1441 assert(*field_decl
);
1446 BT_PUT(nested_decl
);
1447 BT_PUT(*field_decl
);
1453 int visit_struct_decl_field(struct ctx
*ctx
,
1454 struct bt_ctf_field_type
*struct_decl
,
1455 struct ctf_node
*type_specifier_list
,
1456 struct bt_list_head
*type_declarators
)
1459 struct ctf_node
*iter
;
1460 _BT_CTF_FIELD_TYPE_INIT(field_decl
);
1462 bt_list_for_each_entry(iter
, type_declarators
, siblings
) {
1465 const char *field_name
;
1466 _BT_CTF_FIELD_TYPE_INIT(efield_decl
);
1468 ret
= visit_type_declarator(ctx
, type_specifier_list
,
1469 &qfield_name
, iter
, &field_decl
, NULL
);
1471 assert(!field_decl
);
1472 _BT_LOGE_NODE(type_specifier_list
,
1473 "Cannot visit type declarator: ret=%d", ret
);
1478 field_name
= g_quark_to_string(qfield_name
);
1480 /* Check if field with same name already exists */
1482 bt_ctf_field_type_structure_get_field_type_by_name(
1483 struct_decl
, field_name
);
1485 BT_PUT(efield_decl
);
1486 _BT_LOGE_NODE(type_specifier_list
,
1487 "Duplicate field in structure field type: "
1488 "field-name=\"%s\"", field_name
);
1493 /* Add field to structure */
1494 ret
= bt_ctf_field_type_structure_add_field(struct_decl
,
1495 field_decl
, field_name
);
1498 _BT_LOGE_NODE(type_specifier_list
,
1499 "Cannot add field to structure field type: "
1500 "field-name=\"%s\", ret=%d",
1501 g_quark_to_string(qfield_name
), ret
);
1515 int visit_variant_decl_field(struct ctx
*ctx
,
1516 struct bt_ctf_field_type
*variant_decl
,
1517 struct ctf_node
*type_specifier_list
,
1518 struct bt_list_head
*type_declarators
)
1521 struct ctf_node
*iter
;
1522 _BT_CTF_FIELD_TYPE_INIT(field_decl
);
1524 bt_list_for_each_entry(iter
, type_declarators
, siblings
) {
1527 const char *field_name
;
1528 _BT_CTF_FIELD_TYPE_INIT(efield_decl
);
1530 ret
= visit_type_declarator(ctx
, type_specifier_list
,
1531 &qfield_name
, iter
, &field_decl
, NULL
);
1533 assert(!field_decl
);
1534 _BT_LOGE_NODE(type_specifier_list
,
1535 "Cannot visit type declarator: ret=%d", ret
);
1540 field_name
= g_quark_to_string(qfield_name
);
1542 /* Check if field with same name already exists */
1544 bt_ctf_field_type_variant_get_field_type_by_name(
1545 variant_decl
, field_name
);
1547 BT_PUT(efield_decl
);
1548 _BT_LOGE_NODE(type_specifier_list
,
1549 "Duplicate field in variant field type: "
1550 "field-name=\"%s\"", field_name
);
1555 /* Add field to structure */
1556 ret
= bt_ctf_field_type_variant_add_field(variant_decl
,
1557 field_decl
, field_name
);
1560 _BT_LOGE_NODE(type_specifier_list
,
1561 "Cannot add field to variant field type: "
1562 "field-name=\"%s\", ret=%d",
1563 g_quark_to_string(qfield_name
), ret
);
1577 int visit_typedef(struct ctx
*ctx
, struct ctf_node
*type_specifier_list
,
1578 struct bt_list_head
*type_declarators
)
1582 struct ctf_node
*iter
;
1583 _BT_CTF_FIELD_TYPE_INIT(type_decl
);
1585 bt_list_for_each_entry(iter
, type_declarators
, siblings
) {
1586 ret
= visit_type_declarator(ctx
, type_specifier_list
,
1587 &qidentifier
, iter
, &type_decl
, NULL
);
1590 "Cannot visit type declarator: ret=%d", ret
);
1595 /* Do not allow typedef and typealias of untagged variants */
1596 if (bt_ctf_field_type_is_variant(type_decl
)) {
1597 if (bt_ctf_field_type_variant_get_tag_name(type_decl
)) {
1599 "Type definition of untagged variant field type is not allowed.");
1605 ret
= ctx_decl_scope_register_alias(ctx
->current_scope
,
1606 g_quark_to_string(qidentifier
), type_decl
);
1609 "Cannot register type definition: name=\"%s\"",
1610 g_quark_to_string(qidentifier
));
1622 int visit_typealias(struct ctx
*ctx
, struct ctf_node
*target
,
1623 struct ctf_node
*alias
)
1627 struct ctf_node
*node
;
1628 GQuark qdummy_field_name
;
1629 _BT_CTF_FIELD_TYPE_INIT(type_decl
);
1631 /* Create target type declaration */
1632 if (bt_list_empty(&target
->u
.typealias_target
.type_declarators
)) {
1635 node
= _BT_LIST_FIRST_ENTRY(
1636 &target
->u
.typealias_target
.type_declarators
,
1637 struct ctf_node
, siblings
);
1640 ret
= visit_type_declarator(ctx
,
1641 target
->u
.typealias_target
.type_specifier_list
,
1642 &qdummy_field_name
, node
, &type_decl
, NULL
);
1646 "Cannot visit type declarator: ret=%d", ret
);
1650 /* Do not allow typedef and typealias of untagged variants */
1651 if (bt_ctf_field_type_is_variant(type_decl
)) {
1652 if (bt_ctf_field_type_variant_get_tag_name(type_decl
)) {
1654 "Type definition of untagged variant field type is not allowed.");
1661 * The semantic validator does not check whether the target is
1662 * abstract or not (if it has an identifier). Check it here.
1664 if (qdummy_field_name
!= 0) {
1666 "Expecting empty identifier: id=\"%s\"",
1667 g_quark_to_string(qdummy_field_name
));
1672 /* Create alias identifier */
1673 node
= _BT_LIST_FIRST_ENTRY(&alias
->u
.typealias_alias
.type_declarators
,
1674 struct ctf_node
, siblings
);
1675 qalias
= create_typealias_identifier(ctx
,
1676 alias
->u
.typealias_alias
.type_specifier_list
, node
);
1677 ret
= ctx_decl_scope_register_alias(ctx
->current_scope
,
1678 g_quark_to_string(qalias
), type_decl
);
1681 "Cannot register type alias: name=\"%s\"",
1682 g_quark_to_string(qalias
));
1693 int visit_struct_decl_entry(struct ctx
*ctx
, struct ctf_node
*entry_node
,
1694 struct bt_ctf_field_type
*struct_decl
)
1698 switch (entry_node
->type
) {
1700 ret
= visit_typedef(ctx
,
1701 entry_node
->u
._typedef
.type_specifier_list
,
1702 &entry_node
->u
._typedef
.type_declarators
);
1704 _BT_LOGE_NODE(entry_node
,
1705 "Cannot add type definition found in structure field type: ret=%d",
1710 case NODE_TYPEALIAS
:
1711 ret
= visit_typealias(ctx
, entry_node
->u
.typealias
.target
,
1712 entry_node
->u
.typealias
.alias
);
1714 _BT_LOGE_NODE(entry_node
,
1715 "Cannot add type alias found in structure field type: ret=%d",
1720 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
1722 ret
= visit_struct_decl_field(ctx
, struct_decl
,
1723 entry_node
->u
.struct_or_variant_declaration
.
1724 type_specifier_list
,
1725 &entry_node
->u
.struct_or_variant_declaration
.
1732 _BT_LOGE_NODE(entry_node
,
1733 "Unexpected node type: node-type=%d", entry_node
->type
);
1743 int visit_variant_decl_entry(struct ctx
*ctx
, struct ctf_node
*entry_node
,
1744 struct bt_ctf_field_type
*variant_decl
)
1748 switch (entry_node
->type
) {
1750 ret
= visit_typedef(ctx
,
1751 entry_node
->u
._typedef
.type_specifier_list
,
1752 &entry_node
->u
._typedef
.type_declarators
);
1754 _BT_LOGE_NODE(entry_node
,
1755 "Cannot add type definition found in variant field type: ret=%d",
1760 case NODE_TYPEALIAS
:
1761 ret
= visit_typealias(ctx
, entry_node
->u
.typealias
.target
,
1762 entry_node
->u
.typealias
.alias
);
1764 _BT_LOGE_NODE(entry_node
,
1765 "Cannot add type alias found in variant field type: ret=%d",
1770 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
1772 ret
= visit_variant_decl_field(ctx
, variant_decl
,
1773 entry_node
->u
.struct_or_variant_declaration
.
1774 type_specifier_list
,
1775 &entry_node
->u
.struct_or_variant_declaration
.
1782 _BT_LOGE_NODE(entry_node
,
1783 "Unexpected node type: node-type=%d",
1794 int visit_struct_decl(struct ctx
*ctx
, const char *name
,
1795 struct bt_list_head
*decl_list
, int has_body
,
1796 struct bt_list_head
*min_align
,
1797 struct bt_ctf_field_type
**struct_decl
)
1801 *struct_decl
= NULL
;
1803 /* For named struct (without body), lookup in declaration scope */
1805 _BT_CTF_FIELD_TYPE_INIT(struct_decl_copy
);
1808 BT_LOGE_STR("Bodyless structure field type: missing name.");
1813 *struct_decl
= ctx_decl_scope_lookup_struct(ctx
->current_scope
,
1815 if (!*struct_decl
) {
1816 BT_LOGE("Cannot find structure field type: name=\"struct %s\"",
1822 /* Make a copy of it */
1823 struct_decl_copy
= bt_ctf_field_type_copy(*struct_decl
);
1824 if (!struct_decl_copy
) {
1825 BT_LOGE_STR("Cannot create copy of structure field type.");
1830 BT_MOVE(*struct_decl
, struct_decl_copy
);
1832 struct ctf_node
*entry_node
;
1833 uint64_t min_align_value
= 0;
1836 _BT_CTF_FIELD_TYPE_INIT(estruct_decl
);
1838 estruct_decl
= ctx_decl_scope_lookup_struct(
1839 ctx
->current_scope
, name
, 1);
1841 BT_PUT(estruct_decl
);
1842 BT_LOGE("Structure field type already declared in local scope: "
1843 "name=\"struct %s\"", name
);
1849 if (!bt_list_empty(min_align
)) {
1850 ret
= get_unary_unsigned(min_align
, &min_align_value
);
1852 BT_LOGE("Unexpected unary expression for structure field type's `align` attribute: "
1858 *struct_decl
= bt_ctf_field_type_structure_create();
1859 if (!*struct_decl
) {
1860 BT_LOGE_STR("Cannot create empty structure field type.");
1865 if (min_align_value
!= 0) {
1866 ret
= bt_ctf_field_type_set_alignment(*struct_decl
,
1869 BT_LOGE("Cannot set structure field type's alignment: "
1875 ret
= ctx_push_scope(ctx
);
1877 BT_LOGE_STR("Cannot push scope.");
1881 bt_list_for_each_entry(entry_node
, decl_list
, siblings
) {
1882 ret
= visit_struct_decl_entry(ctx
, entry_node
,
1885 _BT_LOGE_NODE(entry_node
,
1886 "Cannot visit structure field type entry: "
1896 ret
= ctx_decl_scope_register_struct(ctx
->current_scope
,
1897 name
, *struct_decl
);
1899 BT_LOGE("Cannot register structure field type in declaration scope: "
1900 "name=\"struct %s\", ret=%d", name
, ret
);
1909 BT_PUT(*struct_decl
);
1915 int visit_variant_decl(struct ctx
*ctx
, const char *name
,
1916 const char *tag
, struct bt_list_head
*decl_list
,
1917 int has_body
, struct bt_ctf_field_type
**variant_decl
)
1920 _BT_CTF_FIELD_TYPE_INIT(untagged_variant_decl
);
1922 *variant_decl
= NULL
;
1924 /* For named variant (without body), lookup in declaration scope */
1926 _BT_CTF_FIELD_TYPE_INIT(variant_decl_copy
);
1929 BT_LOGE_STR("Bodyless variant field type: missing name.");
1934 untagged_variant_decl
=
1935 ctx_decl_scope_lookup_variant(ctx
->current_scope
,
1937 if (!untagged_variant_decl
) {
1938 BT_LOGE("Cannot find variant field type: name=\"variant %s\"",
1944 /* Make a copy of it */
1945 variant_decl_copy
= bt_ctf_field_type_copy(
1946 untagged_variant_decl
);
1947 if (!variant_decl_copy
) {
1948 BT_LOGE_STR("Cannot create copy of variant field type.");
1953 BT_MOVE(untagged_variant_decl
, variant_decl_copy
);
1955 struct ctf_node
*entry_node
;
1958 struct bt_ctf_field_type
*evariant_decl
=
1959 ctx_decl_scope_lookup_struct(ctx
->current_scope
,
1962 if (evariant_decl
) {
1963 BT_PUT(evariant_decl
);
1964 BT_LOGE("Variant field type already declared in local scope: "
1965 "name=\"variant %s\"", name
);
1971 untagged_variant_decl
= bt_ctf_field_type_variant_create(NULL
,
1973 if (!untagged_variant_decl
) {
1974 BT_LOGE_STR("Cannot create empty variant field type.");
1979 ret
= ctx_push_scope(ctx
);
1981 BT_LOGE_STR("Cannot push scope.");
1985 bt_list_for_each_entry(entry_node
, decl_list
, siblings
) {
1986 ret
= visit_variant_decl_entry(ctx
, entry_node
,
1987 untagged_variant_decl
);
1989 _BT_LOGE_NODE(entry_node
,
1990 "Cannot visit variant field type entry: "
2000 ret
= ctx_decl_scope_register_variant(
2001 ctx
->current_scope
, name
,
2002 untagged_variant_decl
);
2004 BT_LOGE("Cannot register variant field type in declaration scope: "
2005 "name=\"variant %s\", ret=%d", name
, ret
);
2012 * If tagged, create tagged variant and return; otherwise
2013 * return untagged variant.
2016 BT_MOVE(*variant_decl
, untagged_variant_decl
);
2019 * At this point, we have a fresh untagged variant; nobody
2020 * else owns it. Set its tag now.
2022 ret
= bt_ctf_field_type_variant_set_tag_name(
2023 untagged_variant_decl
, tag
);
2025 BT_LOGE("Cannot set variant field type's tag name: "
2026 "tag-name=\"%s\"", tag
);
2030 BT_MOVE(*variant_decl
, untagged_variant_decl
);
2033 assert(!untagged_variant_decl
);
2034 assert(*variant_decl
);
2039 BT_PUT(untagged_variant_decl
);
2040 BT_PUT(*variant_decl
);
2046 int visit_enum_decl_entry(struct ctx
*ctx
, struct ctf_node
*enumerator
,
2047 struct bt_ctf_field_type
*enum_decl
, int64_t *last
, int is_signed
)
2051 struct ctf_node
*iter
;
2052 int64_t start
= 0, end
= 0;
2053 const char *label
= enumerator
->u
.enumerator
.id
;
2054 struct bt_list_head
*values
= &enumerator
->u
.enumerator
.values
;
2056 bt_list_for_each_entry(iter
, values
, siblings
) {
2059 if (iter
->type
!= NODE_UNARY_EXPRESSION
) {
2061 "Wrong expression for enumeration field type label: "
2062 "node-type=%d, label=\"%s\"", iter
->type
,
2074 switch (iter
->u
.unary_expression
.type
) {
2075 case UNARY_SIGNED_CONSTANT
:
2076 *target
= iter
->u
.unary_expression
.u
.signed_constant
;
2078 case UNARY_UNSIGNED_CONSTANT
:
2080 iter
->u
.unary_expression
.u
.unsigned_constant
;
2084 "Invalid enumeration field type entry: "
2085 "expecting constant signed or unsigned integer: "
2086 "node-type=%d, label=\"%s\"",
2087 iter
->u
.unary_expression
.type
, label
);
2094 "Invalid enumeration field type entry: label=\"%s\"",
2114 ret
= bt_ctf_field_type_enumeration_add_mapping(enum_decl
, label
,
2117 ret
= bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_decl
,
2118 label
, (uint64_t) start
, (uint64_t) end
);
2121 _BT_LOGE_NODE(enumerator
,
2122 "Cannot add mapping to enumeration field type: "
2123 "label=\"%s\", ret=%d, "
2124 "start-value-unsigned=%" PRIu64
", "
2125 "end-value-unsigned=%" PRIu64
, label
, ret
,
2126 (uint64_t) start
, (uint64_t) end
);
2137 int visit_enum_decl(struct ctx
*ctx
, const char *name
,
2138 struct ctf_node
*container_type
,
2139 struct bt_list_head
*enumerator_list
,
2141 struct bt_ctf_field_type
**enum_decl
)
2145 _BT_CTF_FIELD_TYPE_INIT(integer_decl
);
2149 /* For named enum (without body), lookup in declaration scope */
2151 _BT_CTF_FIELD_TYPE_INIT(enum_decl_copy
);
2154 BT_LOGE_STR("Bodyless enumeration field type: missing name.");
2159 *enum_decl
= ctx_decl_scope_lookup_enum(ctx
->current_scope
,
2162 BT_LOGE("Cannot find enumeration field type: "
2163 "name=\"enum %s\"", name
);
2168 /* Make a copy of it */
2169 enum_decl_copy
= bt_ctf_field_type_copy(*enum_decl
);
2170 if (!enum_decl_copy
) {
2171 BT_LOGE_STR("Cannot create copy of enumeration field type.");
2177 BT_MOVE(*enum_decl
, enum_decl_copy
);
2179 struct ctf_node
*iter
;
2180 int64_t last_value
= 0;
2183 _BT_CTF_FIELD_TYPE_INIT(eenum_decl
);
2185 eenum_decl
= ctx_decl_scope_lookup_enum(
2186 ctx
->current_scope
, name
, 1);
2189 BT_LOGE("Enumeration field type already declared in local scope: "
2190 "name=\"enum %s\"", name
);
2196 if (!container_type
) {
2197 integer_decl
= ctx_decl_scope_lookup_alias(
2198 ctx
->current_scope
, "int", -1);
2199 if (!integer_decl
) {
2200 BT_LOGE_STR("Cannot find implicit `int` field type alias for enumeration field type.");
2205 ret
= visit_type_declarator(ctx
, container_type
,
2206 &qdummy_id
, NULL
, &integer_decl
, NULL
);
2208 assert(!integer_decl
);
2214 assert(integer_decl
);
2216 if (!bt_ctf_field_type_is_integer(integer_decl
)) {
2217 BT_LOGE("Container field type for enumeration field type is not an integer field type: "
2219 bt_ctf_field_type_id_string(
2220 bt_ctf_field_type_get_type_id(integer_decl
)));
2225 *enum_decl
= bt_ctf_field_type_enumeration_create(integer_decl
);
2227 BT_LOGE_STR("Cannot create enumeration field type.");
2232 bt_list_for_each_entry(iter
, enumerator_list
, siblings
) {
2233 ret
= visit_enum_decl_entry(ctx
, iter
, *enum_decl
,
2235 bt_ctf_field_type_integer_get_signed(integer_decl
));
2238 "Cannot visit enumeration field type entry: "
2245 ret
= ctx_decl_scope_register_enum(ctx
->current_scope
,
2248 BT_LOGE("Cannot register enumeration field type in declaration scope: "
2255 BT_PUT(integer_decl
);
2260 BT_PUT(integer_decl
);
2267 int visit_type_specifier(struct ctx
*ctx
,
2268 struct ctf_node
*type_specifier_list
,
2269 struct bt_ctf_field_type
**decl
)
2272 GString
*str
= NULL
;
2273 _BT_CTF_FIELD_TYPE_INIT(decl_copy
);
2276 str
= g_string_new("");
2277 ret
= get_type_specifier_list_name(ctx
, type_specifier_list
, str
);
2279 _BT_LOGE_NODE(type_specifier_list
,
2280 "Cannot get type specifier list's name: ret=%d", ret
);
2284 *decl
= ctx_decl_scope_lookup_alias(ctx
->current_scope
, str
->str
, -1);
2286 _BT_LOGE_NODE(type_specifier_list
,
2287 "Cannot find type alias: name=\"%s\"", str
->str
);
2292 /* Make a copy of the type declaration */
2293 decl_copy
= bt_ctf_field_type_copy(*decl
);
2295 _BT_LOGE_NODE(type_specifier_list
,
2296 "Cannot create field type copy.");
2301 BT_MOVE(*decl
, decl_copy
);
2302 (void) g_string_free(str
, TRUE
);
2309 (void) g_string_free(str
, TRUE
);
2318 int visit_integer_decl(struct ctx
*ctx
,
2319 struct bt_list_head
*expressions
,
2320 struct bt_ctf_field_type
**integer_decl
)
2325 struct ctf_node
*expression
;
2326 uint64_t alignment
= 0, size
= 0;
2327 struct bt_ctf_clock_class
*mapped_clock
= NULL
;
2328 enum bt_ctf_string_encoding encoding
= BT_CTF_STRING_ENCODING_NONE
;
2329 enum bt_ctf_integer_base base
= BT_CTF_INTEGER_BASE_DECIMAL
;
2330 enum bt_ctf_byte_order byte_order
=
2331 bt_ctf_trace_get_native_byte_order(ctx
->trace
);
2333 *integer_decl
= NULL
;
2335 bt_list_for_each_entry(expression
, expressions
, siblings
) {
2336 struct ctf_node
*left
, *right
;
2338 left
= _BT_LIST_FIRST_ENTRY(&expression
->u
.ctf_expression
.left
,
2339 struct ctf_node
, siblings
);
2340 right
= _BT_LIST_FIRST_ENTRY(
2341 &expression
->u
.ctf_expression
.right
, struct ctf_node
,
2344 if (left
->u
.unary_expression
.type
!= UNARY_STRING
) {
2346 "Unexpected unary expression type: type=%d",
2347 left
->u
.unary_expression
.type
);
2352 if (!strcmp(left
->u
.unary_expression
.u
.string
, "signed")) {
2353 if (_IS_SET(&set
, _INTEGER_SIGNED_SET
)) {
2354 _BT_LOGE_DUP_ATTR(left
, "signed",
2355 "integer field type");
2360 signedness
= get_boolean(right
);
2361 if (signedness
< 0) {
2362 _BT_LOGE_NODE(right
,
2363 "Invalid boolean value for integer field type's `signed` attribute: "
2369 _SET(&set
, _INTEGER_SIGNED_SET
);
2370 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2372 if (_IS_SET(&set
, _INTEGER_BYTE_ORDER_SET
)) {
2373 _BT_LOGE_DUP_ATTR(left
, "byte_order",
2374 "integer field type");
2379 byte_order
= get_real_byte_order(ctx
, right
);
2380 if (byte_order
== BT_CTF_BYTE_ORDER_UNKNOWN
) {
2381 _BT_LOGE_NODE(right
,
2382 "Invalid `byte_order` attribute in integer field type: "
2388 _SET(&set
, _INTEGER_BYTE_ORDER_SET
);
2389 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "size")) {
2390 if (_IS_SET(&set
, _INTEGER_SIZE_SET
)) {
2391 _BT_LOGE_DUP_ATTR(left
, "size",
2392 "integer field type");
2397 if (right
->u
.unary_expression
.type
!=
2398 UNARY_UNSIGNED_CONSTANT
) {
2399 _BT_LOGE_NODE(right
,
2400 "Invalid `size` attribute in integer field type: "
2401 "expecting unsigned constant integer: "
2403 right
->u
.unary_expression
.type
);
2408 size
= right
->u
.unary_expression
.u
.unsigned_constant
;
2410 _BT_LOGE_NODE(right
,
2411 "Invalid `size` attribute in integer field type: "
2412 "expecting positive constant integer: "
2413 "size=%" PRIu64
, size
);
2416 } else if (size
> 64) {
2417 _BT_LOGE_NODE(right
,
2418 "Invalid `size` attribute in integer field type: "
2419 "integer fields over 64 bits are not supported as of this version: "
2420 "size=%" PRIu64
, size
);
2425 _SET(&set
, _INTEGER_SIZE_SET
);
2426 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2428 if (_IS_SET(&set
, _INTEGER_ALIGN_SET
)) {
2429 _BT_LOGE_DUP_ATTR(left
, "align",
2430 "integer field type");
2435 if (right
->u
.unary_expression
.type
!=
2436 UNARY_UNSIGNED_CONSTANT
) {
2437 _BT_LOGE_NODE(right
,
2438 "Invalid `align` attribute in integer field type: "
2439 "expecting unsigned constant integer: "
2441 right
->u
.unary_expression
.type
);
2447 right
->u
.unary_expression
.u
.unsigned_constant
;
2448 if (!is_align_valid(alignment
)) {
2449 _BT_LOGE_NODE(right
,
2450 "Invalid `align` attribute in integer field type: "
2451 "expecting power of two: "
2452 "align=%" PRIu64
, alignment
);
2457 _SET(&set
, _INTEGER_ALIGN_SET
);
2458 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "base")) {
2459 if (_IS_SET(&set
, _INTEGER_BASE_SET
)) {
2460 _BT_LOGE_DUP_ATTR(left
, "base",
2461 "integer field type");
2466 switch (right
->u
.unary_expression
.type
) {
2467 case UNARY_UNSIGNED_CONSTANT
:
2469 uint64_t constant
= right
->u
.unary_expression
.
2470 u
.unsigned_constant
;
2474 base
= BT_CTF_INTEGER_BASE_BINARY
;
2477 base
= BT_CTF_INTEGER_BASE_OCTAL
;
2480 base
= BT_CTF_INTEGER_BASE_DECIMAL
;
2483 base
= BT_CTF_INTEGER_BASE_HEXADECIMAL
;
2486 _BT_LOGE_NODE(right
,
2487 "Invalid `base` attribute in integer field type: "
2489 right
->u
.unary_expression
.u
.unsigned_constant
);
2497 char *s_right
= concatenate_unary_strings(
2498 &expression
->u
.ctf_expression
.right
);
2500 _BT_LOGE_NODE(right
,
2501 "Unexpected unary expression for integer field type's `base` attribute.");
2506 if (!strcmp(s_right
, "decimal") ||
2507 !strcmp(s_right
, "dec") ||
2508 !strcmp(s_right
, "d") ||
2509 !strcmp(s_right
, "i") ||
2510 !strcmp(s_right
, "u")) {
2511 base
= BT_CTF_INTEGER_BASE_DECIMAL
;
2512 } else if (!strcmp(s_right
, "hexadecimal") ||
2513 !strcmp(s_right
, "hex") ||
2514 !strcmp(s_right
, "x") ||
2515 !strcmp(s_right
, "X") ||
2516 !strcmp(s_right
, "p")) {
2517 base
= BT_CTF_INTEGER_BASE_HEXADECIMAL
;
2518 } else if (!strcmp(s_right
, "octal") ||
2519 !strcmp(s_right
, "oct") ||
2520 !strcmp(s_right
, "o")) {
2521 base
= BT_CTF_INTEGER_BASE_OCTAL
;
2522 } else if (!strcmp(s_right
, "binary") ||
2523 !strcmp(s_right
, "b")) {
2524 base
= BT_CTF_INTEGER_BASE_BINARY
;
2526 _BT_LOGE_NODE(right
,
2527 "Unexpected unary expression for integer field type's `base` attribute: "
2528 "base=\"%s\"", s_right
);
2538 _BT_LOGE_NODE(right
,
2539 "Invalid `base` attribute in integer field type: "
2540 "expecting unsigned constant integer or unary string.");
2545 _SET(&set
, _INTEGER_BASE_SET
);
2546 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2550 if (_IS_SET(&set
, _INTEGER_ENCODING_SET
)) {
2551 _BT_LOGE_DUP_ATTR(left
, "encoding",
2552 "integer field type");
2557 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
2558 _BT_LOGE_NODE(right
,
2559 "Invalid `encoding` attribute in integer field type: "
2560 "expecting unary string.");
2565 s_right
= concatenate_unary_strings(
2566 &expression
->u
.ctf_expression
.right
);
2568 _BT_LOGE_NODE(right
,
2569 "Unexpected unary expression for integer field type's `encoding` attribute.");
2574 if (!strcmp(s_right
, "UTF8") ||
2575 !strcmp(s_right
, "utf8") ||
2576 !strcmp(s_right
, "utf-8") ||
2577 !strcmp(s_right
, "UTF-8")) {
2578 encoding
= BT_CTF_STRING_ENCODING_UTF8
;
2579 } else if (!strcmp(s_right
, "ASCII") ||
2580 !strcmp(s_right
, "ascii")) {
2581 encoding
= BT_CTF_STRING_ENCODING_ASCII
;
2582 } else if (!strcmp(s_right
, "none")) {
2583 encoding
= BT_CTF_STRING_ENCODING_NONE
;
2585 _BT_LOGE_NODE(right
,
2586 "Invalid `encoding` attribute in integer field type: "
2587 "unknown encoding: encoding=\"%s\"",
2595 _SET(&set
, _INTEGER_ENCODING_SET
);
2596 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "map")) {
2597 const char *clock_name
;
2599 if (_IS_SET(&set
, _INTEGER_MAP_SET
)) {
2600 _BT_LOGE_DUP_ATTR(left
, "map",
2601 "integer field type");
2606 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
2607 _BT_LOGE_NODE(right
,
2608 "Invalid `map` attribute in integer field type: "
2609 "expecting unary string.");
2615 get_map_clock_name_value(
2616 &expression
->u
.ctf_expression
.right
);
2618 char *s_right
= concatenate_unary_strings(
2619 &expression
->u
.ctf_expression
.right
);
2622 _BT_LOGE_NODE(right
,
2623 "Unexpected unary expression for integer field type's `map` attribute.");
2628 _BT_LOGE_NODE(right
,
2629 "Invalid `map` attribute in integer field type: "
2630 "cannot find clock class at this point: name=\"%s\"",
2632 _SET(&set
, _INTEGER_MAP_SET
);
2637 mapped_clock
= bt_ctf_trace_get_clock_class_by_name(
2638 ctx
->trace
, clock_name
);
2639 if (!mapped_clock
) {
2640 _BT_LOGE_NODE(right
,
2641 "Invalid `map` attribute in integer field type: "
2642 "cannot find clock class at this point: name=\"%s\"",
2648 _SET(&set
, _INTEGER_MAP_SET
);
2651 "Unknown attribute in integer field type: "
2653 left
->u
.unary_expression
.u
.string
);
2657 if (!_IS_SET(&set
, _INTEGER_SIZE_SET
)) {
2658 BT_LOGE_STR("Missing `size` attribute in integer field type.");
2663 if (!_IS_SET(&set
, _INTEGER_ALIGN_SET
)) {
2664 if (size
% CHAR_BIT
) {
2665 /* Bit-packed alignment */
2668 /* Byte-packed alignment */
2669 alignment
= CHAR_BIT
;
2673 *integer_decl
= bt_ctf_field_type_integer_create((unsigned int) size
);
2674 if (!*integer_decl
) {
2675 BT_LOGE_STR("Cannot create integer field type.");
2680 ret
= bt_ctf_field_type_integer_set_signed(*integer_decl
, signedness
);
2681 ret
|= bt_ctf_field_type_integer_set_base(*integer_decl
, base
);
2682 ret
|= bt_ctf_field_type_integer_set_encoding(*integer_decl
, encoding
);
2683 ret
|= bt_ctf_field_type_set_alignment(*integer_decl
,
2684 (unsigned int) alignment
);
2685 ret
|= bt_ctf_field_type_set_byte_order(*integer_decl
, byte_order
);
2689 ret
|= bt_ctf_field_type_integer_set_mapped_clock_class(
2690 *integer_decl
, mapped_clock
);
2691 bt_put(mapped_clock
);
2692 mapped_clock
= NULL
;
2696 BT_LOGE_STR("Cannot configure integer field type.");
2705 bt_put(mapped_clock
);
2708 BT_PUT(*integer_decl
);
2714 int visit_floating_point_number_decl(struct ctx
*ctx
,
2715 struct bt_list_head
*expressions
,
2716 struct bt_ctf_field_type
**float_decl
)
2720 struct ctf_node
*expression
;
2721 uint64_t alignment
= 1, exp_dig
= 0, mant_dig
= 0;
2722 enum bt_ctf_byte_order byte_order
=
2723 bt_ctf_trace_get_native_byte_order(ctx
->trace
);
2727 bt_list_for_each_entry(expression
, expressions
, siblings
) {
2728 struct ctf_node
*left
, *right
;
2730 left
= _BT_LIST_FIRST_ENTRY(&expression
->u
.ctf_expression
.left
,
2731 struct ctf_node
, siblings
);
2732 right
= _BT_LIST_FIRST_ENTRY(
2733 &expression
->u
.ctf_expression
.right
, struct ctf_node
,
2736 if (left
->u
.unary_expression
.type
!= UNARY_STRING
) {
2738 "Unexpected unary expression type: type=%d",
2739 left
->u
.unary_expression
.type
);
2744 if (!strcmp(left
->u
.unary_expression
.u
.string
, "byte_order")) {
2745 if (_IS_SET(&set
, _FLOAT_BYTE_ORDER_SET
)) {
2746 _BT_LOGE_DUP_ATTR(left
, "byte_order",
2747 "floating point number field type");
2752 byte_order
= get_real_byte_order(ctx
, right
);
2753 if (byte_order
== BT_CTF_BYTE_ORDER_UNKNOWN
) {
2754 _BT_LOGE_NODE(right
,
2755 "Invalid `byte_order` attribute in floating point number field type: "
2761 _SET(&set
, _FLOAT_BYTE_ORDER_SET
);
2762 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2764 if (_IS_SET(&set
, _FLOAT_EXP_DIG_SET
)) {
2765 _BT_LOGE_DUP_ATTR(left
, "exp_dig",
2766 "floating point number field type");
2771 if (right
->u
.unary_expression
.type
!=
2772 UNARY_UNSIGNED_CONSTANT
) {
2773 _BT_LOGE_NODE(right
,
2774 "Invalid `exp_dig` attribute in floating point number field type: "
2775 "expecting unsigned constant integer: "
2777 right
->u
.unary_expression
.type
);
2782 exp_dig
= right
->u
.unary_expression
.u
.unsigned_constant
;
2783 _SET(&set
, _FLOAT_EXP_DIG_SET
);
2784 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2786 if (_IS_SET(&set
, _FLOAT_MANT_DIG_SET
)) {
2787 _BT_LOGE_DUP_ATTR(left
, "mant_dig",
2788 "floating point number field type");
2793 if (right
->u
.unary_expression
.type
!=
2794 UNARY_UNSIGNED_CONSTANT
) {
2795 _BT_LOGE_NODE(right
,
2796 "Invalid `mant_dig` attribute in floating point number field type: "
2797 "expecting unsigned constant integer: "
2799 right
->u
.unary_expression
.type
);
2804 mant_dig
= right
->u
.unary_expression
.u
.
2806 _SET(&set
, _FLOAT_MANT_DIG_SET
);
2807 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2809 if (_IS_SET(&set
, _FLOAT_ALIGN_SET
)) {
2810 _BT_LOGE_DUP_ATTR(left
, "align",
2811 "floating point number field type");
2816 if (right
->u
.unary_expression
.type
!=
2817 UNARY_UNSIGNED_CONSTANT
) {
2818 _BT_LOGE_NODE(right
,
2819 "Invalid `align` attribute in floating point number field type: "
2820 "expecting unsigned constant integer: "
2822 right
->u
.unary_expression
.type
);
2827 alignment
= right
->u
.unary_expression
.u
.
2830 if (!is_align_valid(alignment
)) {
2831 _BT_LOGE_NODE(right
,
2832 "Invalid `align` attribute in floating point number field type: "
2833 "expecting power of two: "
2834 "align=%" PRIu64
, alignment
);
2839 _SET(&set
, _FLOAT_ALIGN_SET
);
2842 "Unknown attribute in floating point number field type: "
2844 left
->u
.unary_expression
.u
.string
);
2848 if (!_IS_SET(&set
, _FLOAT_MANT_DIG_SET
)) {
2849 BT_LOGE_STR("Missing `mant_dig` attribute in floating point number field type.");
2854 if (!_IS_SET(&set
, _FLOAT_EXP_DIG_SET
)) {
2855 BT_LOGE_STR("Missing `exp_dig` attribute in floating point number field type.");
2860 if (!_IS_SET(&set
, _INTEGER_ALIGN_SET
)) {
2861 if ((mant_dig
+ exp_dig
) % CHAR_BIT
) {
2862 /* Bit-packed alignment */
2865 /* Byte-packed alignment */
2866 alignment
= CHAR_BIT
;
2870 *float_decl
= bt_ctf_field_type_floating_point_create();
2872 BT_LOGE_STR("Cannot create floating point number field type.");
2877 ret
= bt_ctf_field_type_floating_point_set_exponent_digits(
2878 *float_decl
, exp_dig
);
2879 ret
|= bt_ctf_field_type_floating_point_set_mantissa_digits(
2880 *float_decl
, mant_dig
);
2881 ret
|= bt_ctf_field_type_set_byte_order(*float_decl
, byte_order
);
2882 ret
|= bt_ctf_field_type_set_alignment(*float_decl
, alignment
);
2884 BT_LOGE_STR("Cannot configure floating point number field type.");
2892 BT_PUT(*float_decl
);
2898 int visit_string_decl(struct ctx
*ctx
,
2899 struct bt_list_head
*expressions
,
2900 struct bt_ctf_field_type
**string_decl
)
2904 struct ctf_node
*expression
;
2905 enum bt_ctf_string_encoding encoding
= BT_CTF_STRING_ENCODING_UTF8
;
2907 *string_decl
= NULL
;
2909 bt_list_for_each_entry(expression
, expressions
, siblings
) {
2910 struct ctf_node
*left
, *right
;
2912 left
= _BT_LIST_FIRST_ENTRY(&expression
->u
.ctf_expression
.left
,
2913 struct ctf_node
, siblings
);
2914 right
= _BT_LIST_FIRST_ENTRY(
2915 &expression
->u
.ctf_expression
.right
, struct ctf_node
,
2918 if (left
->u
.unary_expression
.type
!= UNARY_STRING
) {
2920 "Unexpected unary expression type: type=%d",
2921 left
->u
.unary_expression
.type
);
2926 if (!strcmp(left
->u
.unary_expression
.u
.string
, "encoding")) {
2929 if (_IS_SET(&set
, _STRING_ENCODING_SET
)) {
2930 _BT_LOGE_DUP_ATTR(left
, "encoding",
2931 "string field type");
2936 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
2937 _BT_LOGE_NODE(right
,
2938 "Invalid `encoding` attribute in string field type: "
2939 "expecting unary string.");
2944 s_right
= concatenate_unary_strings(
2945 &expression
->u
.ctf_expression
.right
);
2947 _BT_LOGE_NODE(right
,
2948 "Unexpected unary expression for string field type's `encoding` attribute.");
2953 if (!strcmp(s_right
, "UTF8") ||
2954 !strcmp(s_right
, "utf8") ||
2955 !strcmp(s_right
, "utf-8") ||
2956 !strcmp(s_right
, "UTF-8")) {
2957 encoding
= BT_CTF_STRING_ENCODING_UTF8
;
2958 } else if (!strcmp(s_right
, "ASCII") ||
2959 !strcmp(s_right
, "ascii")) {
2960 encoding
= BT_CTF_STRING_ENCODING_ASCII
;
2961 } else if (!strcmp(s_right
, "none")) {
2962 encoding
= BT_CTF_STRING_ENCODING_NONE
;
2964 _BT_LOGE_NODE(right
,
2965 "Invalid `encoding` attribute in string field type: "
2966 "unknown encoding: encoding=\"%s\"",
2974 _SET(&set
, _STRING_ENCODING_SET
);
2977 "Unknown attribute in string field type: "
2979 left
->u
.unary_expression
.u
.string
);
2983 *string_decl
= bt_ctf_field_type_string_create();
2984 if (!*string_decl
) {
2985 BT_LOGE_STR("Cannot create string field type.");
2990 ret
= bt_ctf_field_type_string_set_encoding(*string_decl
, encoding
);
2992 BT_LOGE_STR("Cannot configure string field type.");
3000 BT_PUT(*string_decl
);
3006 int visit_type_specifier_list(struct ctx
*ctx
,
3007 struct ctf_node
*ts_list
,
3008 struct bt_ctf_field_type
**decl
)
3011 struct ctf_node
*first
, *node
;
3015 if (ts_list
->type
!= NODE_TYPE_SPECIFIER_LIST
) {
3016 _BT_LOGE_NODE(ts_list
,
3017 "Unexpected node type: node-type=%d", ts_list
->type
);
3022 first
= _BT_LIST_FIRST_ENTRY(&ts_list
->u
.type_specifier_list
.head
,
3023 struct ctf_node
, siblings
);
3024 if (first
->type
!= NODE_TYPE_SPECIFIER
) {
3025 _BT_LOGE_NODE(first
,
3026 "Unexpected node type: node-type=%d", first
->type
);
3031 node
= first
->u
.type_specifier
.node
;
3033 switch (first
->u
.type_specifier
.type
) {
3034 case TYPESPEC_INTEGER
:
3035 ret
= visit_integer_decl(ctx
, &node
->u
.integer
.expressions
,
3042 case TYPESPEC_FLOATING_POINT
:
3043 ret
= visit_floating_point_number_decl(ctx
,
3044 &node
->u
.floating_point
.expressions
, decl
);
3050 case TYPESPEC_STRING
:
3051 ret
= visit_string_decl(ctx
,
3052 &node
->u
.string
.expressions
, decl
);
3058 case TYPESPEC_STRUCT
:
3059 ret
= visit_struct_decl(ctx
, node
->u
._struct
.name
,
3060 &node
->u
._struct
.declaration_list
,
3061 node
->u
._struct
.has_body
,
3062 &node
->u
._struct
.min_align
, decl
);
3068 case TYPESPEC_VARIANT
:
3069 ret
= visit_variant_decl(ctx
, node
->u
.variant
.name
,
3070 node
->u
.variant
.choice
,
3071 &node
->u
.variant
.declaration_list
,
3072 node
->u
.variant
.has_body
, decl
);
3079 ret
= visit_enum_decl(ctx
, node
->u
._enum
.enum_id
,
3080 node
->u
._enum
.container_type
,
3081 &node
->u
._enum
.enumerator_list
,
3082 node
->u
._enum
.has_body
, decl
);
3090 case TYPESPEC_SHORT
:
3093 case TYPESPEC_FLOAT
:
3094 case TYPESPEC_DOUBLE
:
3095 case TYPESPEC_SIGNED
:
3096 case TYPESPEC_UNSIGNED
:
3098 case TYPESPEC_COMPLEX
:
3099 case TYPESPEC_IMAGINARY
:
3100 case TYPESPEC_CONST
:
3101 case TYPESPEC_ID_TYPE
:
3102 ret
= visit_type_specifier(ctx
, ts_list
, decl
);
3104 _BT_LOGE_NODE(first
,
3105 "Cannot visit type specifier: ret=%d",
3112 _BT_LOGE_NODE(first
,
3113 "Unexpected type specifier type: node-type=%d",
3114 first
->u
.type_specifier
.type
);
3130 int visit_event_decl_entry(struct ctx
*ctx
, struct ctf_node
*node
,
3131 struct bt_ctf_event_class
*event_class
, int64_t *stream_id
,
3136 _BT_CTF_FIELD_TYPE_INIT(decl
);
3138 switch (node
->type
) {
3140 ret
= visit_typedef(ctx
, node
->u
._typedef
.type_specifier_list
,
3141 &node
->u
._typedef
.type_declarators
);
3144 "Cannot add type definition found in event class.");
3148 case NODE_TYPEALIAS
:
3149 ret
= visit_typealias(ctx
, node
->u
.typealias
.target
,
3150 node
->u
.typealias
.alias
);
3153 "Cannot add type alias found in event class.");
3157 case NODE_CTF_EXPRESSION
:
3159 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
3161 _BT_LOGE_NODE(node
, "Cannot concatenate unary strings.");
3166 if (!strcmp(left
, "name")) {
3167 /* This is already known at this stage */
3168 if (_IS_SET(set
, _EVENT_NAME_SET
)) {
3169 _BT_LOGE_DUP_ATTR(node
, "name",
3175 _SET(set
, _EVENT_NAME_SET
);
3176 } else if (!strcmp(left
, "id")) {
3179 if (_IS_SET(set
, _EVENT_ID_SET
)) {
3180 _BT_LOGE_DUP_ATTR(node
, "id",
3186 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
3188 /* Only read "id" if get_unary_unsigned() succeeded. */
3189 if (ret
|| (!ret
&& id
< 0)) {
3191 "Unexpected unary expression for event class's `id` attribute.");
3196 ret
= bt_ctf_event_class_set_id(event_class
, id
);
3199 "Cannot set event class's ID: "
3204 _SET(set
, _EVENT_ID_SET
);
3205 } else if (!strcmp(left
, "stream_id")) {
3206 if (_IS_SET(set
, _EVENT_STREAM_ID_SET
)) {
3207 _BT_LOGE_DUP_ATTR(node
, "stream_id",
3213 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
3214 (uint64_t *) stream_id
);
3216 * Only read "stream_id" if get_unary_unsigned()
3219 if (ret
|| (!ret
&& *stream_id
< 0)) {
3221 "Unexpected unary expression for event class's `stream_id` attribute.");
3226 _SET(set
, _EVENT_STREAM_ID_SET
);
3227 } else if (!strcmp(left
, "context")) {
3228 if (_IS_SET(set
, _EVENT_CONTEXT_SET
)) {
3230 "Duplicate `context` entry in event class.");
3235 ret
= visit_type_specifier_list(ctx
,
3236 _BT_LIST_FIRST_ENTRY(
3237 &node
->u
.ctf_expression
.right
,
3238 struct ctf_node
, siblings
),
3242 "Cannot create event class's context field type.");
3247 ret
= bt_ctf_event_class_set_context_type(
3252 "Cannot set event class's context field type.");
3256 _SET(set
, _EVENT_CONTEXT_SET
);
3257 } else if (!strcmp(left
, "fields")) {
3258 if (_IS_SET(set
, _EVENT_FIELDS_SET
)) {
3260 "Duplicate `fields` entry in event class.");
3265 ret
= visit_type_specifier_list(ctx
,
3266 _BT_LIST_FIRST_ENTRY(
3267 &node
->u
.ctf_expression
.right
,
3268 struct ctf_node
, siblings
),
3272 "Cannot create event class's payload field type.");
3277 ret
= bt_ctf_event_class_set_payload_type(
3282 "Cannot set event class's payload field type.");
3286 _SET(set
, _EVENT_FIELDS_SET
);
3287 } else if (!strcmp(left
, "loglevel")) {
3288 uint64_t loglevel_value
;
3289 enum bt_ctf_event_class_log_level log_level
=
3290 BT_CTF_EVENT_CLASS_LOG_LEVEL_UNSPECIFIED
;
3292 if (_IS_SET(set
, _EVENT_LOGLEVEL_SET
)) {
3293 _BT_LOGE_DUP_ATTR(node
, "loglevel",
3299 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
3303 "Unexpected unary expression for event class's `loglevel` attribute.");
3308 switch (loglevel_value
) {
3310 log_level
= BT_CTF_EVENT_CLASS_LOG_LEVEL_EMERGENCY
;
3313 log_level
= BT_CTF_EVENT_CLASS_LOG_LEVEL_ALERT
;
3316 log_level
= BT_CTF_EVENT_CLASS_LOG_LEVEL_CRITICAL
;
3319 log_level
= BT_CTF_EVENT_CLASS_LOG_LEVEL_ERROR
;
3322 log_level
= BT_CTF_EVENT_CLASS_LOG_LEVEL_WARNING
;
3325 log_level
= BT_CTF_EVENT_CLASS_LOG_LEVEL_NOTICE
;
3328 log_level
= BT_CTF_EVENT_CLASS_LOG_LEVEL_INFO
;
3331 log_level
= BT_CTF_EVENT_CLASS_LOG_LEVEL_DEBUG_SYSTEM
;
3334 log_level
= BT_CTF_EVENT_CLASS_LOG_LEVEL_DEBUG_PROGRAM
;
3337 log_level
= BT_CTF_EVENT_CLASS_LOG_LEVEL_DEBUG_PROCESS
;
3340 log_level
= BT_CTF_EVENT_CLASS_LOG_LEVEL_DEBUG_MODULE
;
3343 log_level
= BT_CTF_EVENT_CLASS_LOG_LEVEL_DEBUG_UNIT
;
3346 log_level
= BT_CTF_EVENT_CLASS_LOG_LEVEL_DEBUG_FUNCTION
;
3349 log_level
= BT_CTF_EVENT_CLASS_LOG_LEVEL_DEBUG_LINE
;
3352 log_level
= BT_CTF_EVENT_CLASS_LOG_LEVEL_DEBUG
;
3355 _BT_LOGW_NODE(node
, "Not setting event class's log level because its value is unknown: "
3356 "log-level=%" PRIu64
, loglevel_value
);
3359 if (log_level
!= BT_CTF_EVENT_CLASS_LOG_LEVEL_UNSPECIFIED
) {
3360 ret
= bt_ctf_event_class_set_log_level(
3361 event_class
, log_level
);
3364 "Cannot set event class's log level.");
3369 _SET(set
, _EVENT_LOGLEVEL_SET
);
3370 } else if (!strcmp(left
, "model.emf.uri")) {
3373 if (_IS_SET(set
, _EVENT_MODEL_EMF_URI_SET
)) {
3374 _BT_LOGE_DUP_ATTR(node
, "model.emf.uri",
3380 right
= concatenate_unary_strings(
3381 &node
->u
.ctf_expression
.right
);
3384 "Unexpected unary expression for event class's `model.emf.uri` attribute.");
3389 if (strlen(right
) == 0) {
3391 "Not setting event class's EMF URI because it's empty.");
3393 ret
= bt_ctf_event_class_set_emf_uri(
3394 event_class
, right
);
3397 "Cannot set event class's EMF URI.");
3403 _SET(set
, _EVENT_MODEL_EMF_URI_SET
);
3406 "Unknown attribute in event class: "
3407 "attr-name=\"%s\"", left
);
3432 char *get_event_decl_name(struct ctx
*ctx
, struct ctf_node
*node
)
3436 struct ctf_node
*iter
;
3437 struct bt_list_head
*decl_list
= &node
->u
.event
.declaration_list
;
3439 bt_list_for_each_entry(iter
, decl_list
, siblings
) {
3440 if (iter
->type
!= NODE_CTF_EXPRESSION
) {
3444 left
= concatenate_unary_strings(&iter
->u
.ctf_expression
.left
);
3447 "Cannot concatenate unary strings.");
3451 if (!strcmp(left
, "name")) {
3452 name
= concatenate_unary_strings(
3453 &iter
->u
.ctf_expression
.right
);
3456 "Unexpected unary expression for event class's `name` attribute.");
3478 int reset_event_decl_types(struct ctx
*ctx
,
3479 struct bt_ctf_event_class
*event_class
)
3484 ret
= bt_ctf_event_class_set_context_type(event_class
, NULL
);
3486 BT_LOGE("Cannot reset initial event class's context field type: "
3487 "event-name=\"%s\"",
3488 bt_ctf_event_class_get_name(event_class
));
3492 /* Event payload. */
3493 ret
= bt_ctf_event_class_set_payload_type(event_class
, NULL
);
3495 BT_LOGE("Cannot reset initial event class's payload field type: "
3496 "event-name=\"%s\"",
3497 bt_ctf_event_class_get_name(event_class
));
3505 int reset_stream_decl_types(struct ctx
*ctx
,
3506 struct bt_ctf_stream_class
*stream_class
)
3510 /* Packet context. */
3511 ret
= bt_ctf_stream_class_set_packet_context_type(stream_class
, NULL
);
3513 BT_LOGE_STR("Cannot reset initial stream class's packet context field type.");
3518 ret
= bt_ctf_stream_class_set_event_header_type(stream_class
, NULL
);
3520 BT_LOGE_STR("Cannot reset initial stream class's event header field type.");
3524 /* Event context. */
3525 ret
= bt_ctf_stream_class_set_event_context_type(stream_class
, NULL
);
3527 BT_LOGE_STR("Cannot reset initial stream class's event context field type.");
3535 struct bt_ctf_stream_class
*create_reset_stream_class(struct ctx
*ctx
)
3538 struct bt_ctf_stream_class
*stream_class
;
3540 stream_class
= bt_ctf_stream_class_create_empty(NULL
);
3541 if (!stream_class
) {
3542 BT_LOGE_STR("Cannot create empty stream class.");
3547 * Set packet context, event header, and event context to NULL to
3548 * override the default ones.
3550 ret
= reset_stream_decl_types(ctx
, stream_class
);
3555 return stream_class
;
3558 BT_PUT(stream_class
);
3564 int visit_event_decl(struct ctx
*ctx
, struct ctf_node
*node
)
3569 struct ctf_node
*iter
;
3570 int64_t stream_id
= -1;
3571 char *event_name
= NULL
;
3572 struct bt_ctf_event_class
*event_class
= NULL
;
3573 struct bt_ctf_event_class
*eevent_class
;
3574 struct bt_ctf_stream_class
*stream_class
= NULL
;
3575 struct bt_list_head
*decl_list
= &node
->u
.event
.declaration_list
;
3576 bool pop_scope
= false;
3578 if (node
->visited
) {
3582 node
->visited
= TRUE
;
3583 event_name
= get_event_decl_name(ctx
, node
);
3586 "Missing `name` attribute in event class.");
3591 event_class
= bt_ctf_event_class_create(event_name
);
3594 * Unset context and fields to override the default ones.
3596 ret
= reset_event_decl_types(ctx
, event_class
);
3599 "Cannot reset event class's field types: "
3604 ret
= ctx_push_scope(ctx
);
3606 BT_LOGE_STR("Cannot push scope.");
3612 bt_list_for_each_entry(iter
, decl_list
, siblings
) {
3613 ret
= visit_event_decl_entry(ctx
, iter
, event_class
,
3616 _BT_LOGE_NODE(iter
, "Cannot visit event class's entry: "
3622 if (!_IS_SET(&set
, _EVENT_STREAM_ID_SET
)) {
3624 int64_t *new_stream_id
;
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
);
3654 new_stream_id
= g_new0(int64_t, 1);
3655 if (!new_stream_id
) {
3656 BT_LOGE_STR("Failed to allocate a int64_t.");
3661 /* Move reference to visitor's context */
3662 g_hash_table_insert(ctx
->stream_classes
,
3663 new_stream_id
, new_stream_class
);
3664 new_stream_id
= NULL
;
3665 new_stream_class
= NULL
;
3668 /* Single stream class: get its ID */
3669 if (g_hash_table_size(ctx
->stream_classes
) == 1) {
3670 keys
= g_hash_table_get_keys(ctx
->stream_classes
);
3671 stream_id
= *((int64_t *) keys
->data
);
3674 assert(bt_ctf_trace_get_stream_class_count(
3677 bt_ctf_trace_get_stream_class_by_index(
3679 assert(stream_class
);
3680 stream_id
= bt_ctf_stream_class_get_id(
3682 BT_PUT(stream_class
);
3687 "Missing `stream_id` attribute in event class.");
3693 assert(stream_id
>= 0);
3695 /* We have the stream ID now; get the stream class if found */
3696 stream_class
= g_hash_table_lookup(ctx
->stream_classes
, &stream_id
);
3697 bt_get(stream_class
);
3698 if (!stream_class
) {
3699 stream_class
= bt_ctf_trace_get_stream_class_by_id(ctx
->trace
,
3701 if (!stream_class
) {
3703 "Cannot find stream class at this point: "
3704 "id=%" PRId64
, stream_id
);
3710 assert(stream_class
);
3712 if (!_IS_SET(&set
, _EVENT_ID_SET
)) {
3713 /* Allow only one event without ID per stream */
3714 if (bt_ctf_stream_class_get_event_class_count(stream_class
) !=
3717 "Missing `id` attribute in event class.");
3723 ret
= bt_ctf_event_class_set_id(event_class
, 0);
3726 "Cannot set event class's ID: id=0, ret=%d",
3732 event_id
= bt_ctf_event_class_get_id(event_class
);
3734 _BT_LOGE_NODE(node
, "Cannot get event class's ID.");
3739 eevent_class
= bt_ctf_stream_class_get_event_class_by_id(stream_class
,
3742 BT_PUT(eevent_class
);
3744 "Duplicate event class (same ID) in the same stream class: "
3745 "id=%" PRId64
, event_id
);
3750 ret
= bt_ctf_stream_class_add_event_class(stream_class
, event_class
);
3751 BT_PUT(event_class
);
3754 "Cannot add event class to stream class: ret=%d", ret
);
3761 bt_put(event_class
);
3769 bt_put(stream_class
);
3774 int auto_map_field_to_trace_clock_class(struct ctx
*ctx
,
3775 struct bt_ctf_field_type
*ft
)
3777 struct bt_ctf_clock_class
*clock_class_to_map_to
= NULL
;
3778 struct bt_ctf_clock_class
*mapped_clock_class
= NULL
;
3780 int64_t clock_class_count
;
3782 if (!ft
|| !bt_ctf_field_type_is_integer(ft
)) {
3786 mapped_clock_class
=
3787 bt_ctf_field_type_integer_get_mapped_clock_class(ft
);
3788 if (mapped_clock_class
) {
3792 clock_class_count
= bt_ctf_trace_get_clock_class_count(ctx
->trace
);
3793 assert(clock_class_count
>= 0);
3795 switch (clock_class_count
) {
3798 * No clock class exists in the trace at this
3799 * point. Create an implicit one at 1 GHz,
3800 * named `default`, and use this clock class.
3802 clock_class_to_map_to
= bt_ctf_clock_class_create("default",
3804 if (!clock_class_to_map_to
) {
3805 BT_LOGE_STR("Cannot create a clock class.");
3810 ret
= bt_ctf_trace_add_clock_class(ctx
->trace
,
3811 clock_class_to_map_to
);
3813 BT_LOGE_STR("Cannot add clock class to trace.");
3819 * Only one clock class exists in the trace at
3820 * this point: use this one.
3822 clock_class_to_map_to
=
3823 bt_ctf_trace_get_clock_class_by_index(ctx
->trace
, 0);
3824 assert(clock_class_to_map_to
);
3828 * Timestamp field not mapped to a clock class
3829 * and there's more than one clock class in the
3830 * trace: this is an error.
3832 BT_LOGE_STR("Timestamp field found with no mapped clock class, "
3833 "but there's more than one clock class in the trace at this point.");
3838 assert(clock_class_to_map_to
);
3839 ret
= bt_ctf_field_type_integer_set_mapped_clock_class(ft
,
3840 clock_class_to_map_to
);
3842 BT_LOGE("Cannot map field type's field to trace's clock class: "
3843 "clock-class-name=\"%s\", ret=%d",
3844 bt_ctf_clock_class_get_name(clock_class_to_map_to
),
3850 bt_put(clock_class_to_map_to
);
3851 bt_put(mapped_clock_class
);
3856 int auto_map_fields_to_trace_clock_class(struct ctx
*ctx
,
3857 struct bt_ctf_field_type
*root_ft
, const char *field_name
)
3866 if (!bt_ctf_field_type_is_structure(root_ft
) &&
3867 !bt_ctf_field_type_is_variant(root_ft
)) {
3871 if (bt_ctf_field_type_is_structure(root_ft
)) {
3872 count
= bt_ctf_field_type_structure_get_field_count(root_ft
);
3874 count
= bt_ctf_field_type_variant_get_field_count(root_ft
);
3879 for (i
= 0; i
< count
; i
++) {
3880 _BT_CTF_FIELD_TYPE_INIT(ft
);
3883 if (bt_ctf_field_type_is_structure(root_ft
)) {
3884 ret
= bt_ctf_field_type_structure_get_field_by_index(
3885 root_ft
, &name
, &ft
, i
);
3886 } else if (bt_ctf_field_type_is_variant(root_ft
)) {
3887 ret
= bt_ctf_field_type_variant_get_field_by_index(
3888 root_ft
, &name
, &ft
, i
);
3893 if (strcmp(name
, field_name
) == 0) {
3894 ret
= auto_map_field_to_trace_clock_class(ctx
, ft
);
3896 BT_LOGE("Cannot automatically map field to trace's clock class: "
3897 "field-name=\"%s\"", field_name
);
3903 ret
= auto_map_fields_to_trace_clock_class(ctx
, ft
, field_name
);
3906 BT_LOGE("Cannot automatically map structure or variant field type's fields to trace's clock class: "
3907 "field-name=\"%s\", root-field-name=\"%s\"",
3918 int visit_stream_decl_entry(struct ctx
*ctx
, struct ctf_node
*node
,
3919 struct bt_ctf_stream_class
*stream_class
, int *set
)
3923 _BT_CTF_FIELD_TYPE_INIT(decl
);
3925 switch (node
->type
) {
3927 ret
= visit_typedef(ctx
, node
->u
._typedef
.type_specifier_list
,
3928 &node
->u
._typedef
.type_declarators
);
3931 "Cannot add type definition found in stream class.");
3935 case NODE_TYPEALIAS
:
3936 ret
= visit_typealias(ctx
, node
->u
.typealias
.target
,
3937 node
->u
.typealias
.alias
);
3940 "Cannot add type alias found in stream class.");
3944 case NODE_CTF_EXPRESSION
:
3946 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
3948 _BT_LOGE_NODE(node
, "Cannot concatenate unary strings.");
3953 if (!strcmp(left
, "id")) {
3957 if (_IS_SET(set
, _STREAM_ID_SET
)) {
3958 _BT_LOGE_DUP_ATTR(node
, "id",
3959 "stream declaration");
3964 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
3966 /* Only read "id" if get_unary_unsigned() succeeded. */
3967 if (ret
|| (!ret
&& id
< 0)) {
3969 "Unexpected unary expression for stream class's `id` attribute.");
3974 ptr
= g_hash_table_lookup(ctx
->stream_classes
, &id
);
3977 "Duplicate stream class (same ID): id=%" PRId64
,
3983 ret
= bt_ctf_stream_class_set_id(stream_class
, id
);
3986 "Cannot set stream class's ID: "
3987 "id=%" PRId64
", ret=%d", id
, ret
);
3991 _SET(set
, _STREAM_ID_SET
);
3992 } else if (!strcmp(left
, "event.header")) {
3993 if (_IS_SET(set
, _STREAM_EVENT_HEADER_SET
)) {
3995 "Duplicate `event.header` entry in stream class.");
4000 ret
= visit_type_specifier_list(ctx
,
4001 _BT_LIST_FIRST_ENTRY(
4002 &node
->u
.ctf_expression
.right
,
4003 struct ctf_node
, siblings
),
4007 "Cannot create stream class's event header field type.");
4012 ret
= auto_map_fields_to_trace_clock_class(ctx
,
4016 "Cannot automatically map specific event header field type fields named `timestamp` to trace's clock class.");
4020 ret
= bt_ctf_stream_class_set_event_header_type(
4021 stream_class
, decl
);
4025 "Cannot set stream class's event header field type.");
4029 _SET(set
, _STREAM_EVENT_HEADER_SET
);
4030 } else if (!strcmp(left
, "event.context")) {
4031 if (_IS_SET(set
, _STREAM_EVENT_CONTEXT_SET
)) {
4033 "Duplicate `event.context` entry in stream class.");
4038 ret
= visit_type_specifier_list(ctx
,
4039 _BT_LIST_FIRST_ENTRY(
4040 &node
->u
.ctf_expression
.right
,
4041 struct ctf_node
, siblings
),
4045 "Cannot create stream class's event context field type.");
4051 ret
= bt_ctf_stream_class_set_event_context_type(
4052 stream_class
, decl
);
4056 "Cannot set stream class's event context field type.");
4060 _SET(set
, _STREAM_EVENT_CONTEXT_SET
);
4061 } else if (!strcmp(left
, "packet.context")) {
4062 if (_IS_SET(set
, _STREAM_PACKET_CONTEXT_SET
)) {
4064 "Duplicate `packet.context` entry in stream class.");
4069 ret
= visit_type_specifier_list(ctx
,
4070 _BT_LIST_FIRST_ENTRY(
4071 &node
->u
.ctf_expression
.right
,
4072 struct ctf_node
, siblings
),
4076 "Cannot create stream class's packet context field type.");
4081 ret
= auto_map_fields_to_trace_clock_class(ctx
,
4082 decl
, "timestamp_begin");
4085 "Cannot automatically map specific packet context field type fields named `timestamp_begin` to trace's clock class.");
4089 ret
= auto_map_fields_to_trace_clock_class(ctx
,
4090 decl
, "timestamp_end");
4093 "Cannot automatically map specific packet context field type fields named `timestamp_end` to trace's clock class.");
4097 ret
= bt_ctf_stream_class_set_packet_context_type(
4098 stream_class
, decl
);
4102 "Cannot set stream class's packet context field type.");
4106 _SET(set
, _STREAM_PACKET_CONTEXT_SET
);
4109 "Unknown attribute in stream class: "
4110 "attr-name=\"%s\"", left
);
4133 int visit_stream_decl(struct ctx
*ctx
, struct ctf_node
*node
)
4139 struct ctf_node
*iter
;
4140 struct bt_ctf_stream_class
*stream_class
= NULL
;
4141 struct bt_ctf_stream_class
*existing_stream_class
= NULL
;
4142 struct bt_list_head
*decl_list
= &node
->u
.stream
.declaration_list
;
4144 if (node
->visited
) {
4148 node
->visited
= TRUE
;
4149 stream_class
= create_reset_stream_class(ctx
);
4150 if (!stream_class
) {
4151 _BT_LOGE_NODE(node
, "Cannot create empty stream class.");
4156 ret
= ctx_push_scope(ctx
);
4158 BT_LOGE_STR("Cannot push scope.");
4162 bt_list_for_each_entry(iter
, decl_list
, siblings
) {
4163 ret
= visit_stream_decl_entry(ctx
, iter
, stream_class
, &set
);
4166 "Cannot visit stream class's entry: "
4175 if (_IS_SET(&set
, _STREAM_ID_SET
)) {
4176 /* Check that packet header has stream_id field */
4177 _BT_CTF_FIELD_TYPE_INIT(stream_id_decl
);
4178 _BT_CTF_FIELD_TYPE_INIT(packet_header_decl
);
4180 packet_header_decl
=
4181 bt_ctf_trace_get_packet_header_type(ctx
->trace
);
4182 if (!packet_header_decl
) {
4184 "Stream class has a `id` attribute, "
4185 "but trace has no packet header field type.");
4190 bt_ctf_field_type_structure_get_field_type_by_name(
4191 packet_header_decl
, "stream_id");
4192 BT_PUT(packet_header_decl
);
4193 if (!stream_id_decl
) {
4195 "Stream class has a `id` attribute, "
4196 "but trace's packet header field type has no `stream_id` field.");
4200 if (!bt_ctf_field_type_is_integer(stream_id_decl
)) {
4201 BT_PUT(stream_id_decl
);
4203 "Stream class has a `id` attribute, "
4204 "but trace's packet header field type's `stream_id` field is not an integer field type.");
4208 BT_PUT(stream_id_decl
);
4210 /* Allow only _one_ ID-less stream */
4211 if (g_hash_table_size(ctx
->stream_classes
) != 0) {
4213 "Missing `id` attribute in stream class as there's more than one stream class in the trace.");
4218 /* Automatic ID: 0 */
4219 ret
= bt_ctf_stream_class_set_id(stream_class
, 0);
4223 id
= bt_ctf_stream_class_get_id(stream_class
);
4226 "Cannot get stream class's ID.");
4232 * Make sure that this stream class's ID is currently unique in
4235 existing_stream_class
= bt_ctf_trace_get_stream_class_by_id(ctx
->trace
,
4237 if (g_hash_table_lookup(ctx
->stream_classes
, &id
) ||
4238 existing_stream_class
) {
4240 "Duplicate stream class (same ID): id=%" PRId64
,
4246 new_id
= g_new0(int64_t, 1);
4248 BT_LOGE_STR("Failed to allocate a int64_t.");
4254 /* Move reference to visitor's context */
4255 g_hash_table_insert(ctx
->stream_classes
, new_id
,
4257 stream_class
= NULL
;
4261 bt_put(stream_class
);
4264 bt_put(existing_stream_class
);
4269 int visit_trace_decl_entry(struct ctx
*ctx
, struct ctf_node
*node
, int *set
)
4273 _BT_CTF_FIELD_TYPE_INIT(packet_header_decl
);
4275 switch (node
->type
) {
4277 ret
= visit_typedef(ctx
, node
->u
._typedef
.type_specifier_list
,
4278 &node
->u
._typedef
.type_declarators
);
4281 "Cannot add type definition found in trace (`trace` block).");
4285 case NODE_TYPEALIAS
:
4286 ret
= visit_typealias(ctx
, node
->u
.typealias
.target
,
4287 node
->u
.typealias
.alias
);
4290 "Cannot add type alias found in trace (`trace` block).");
4294 case NODE_CTF_EXPRESSION
:
4296 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
4298 _BT_LOGE_NODE(node
, "Cannot concatenate unary strings.");
4303 if (!strcmp(left
, "major")) {
4304 if (_IS_SET(set
, _TRACE_MAJOR_SET
)) {
4305 _BT_LOGE_DUP_ATTR(node
, "major", "trace");
4310 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
4314 "Unexpected unary expression for trace's `major` attribute.");
4319 _SET(set
, _TRACE_MAJOR_SET
);
4320 } else if (!strcmp(left
, "minor")) {
4321 if (_IS_SET(set
, _TRACE_MINOR_SET
)) {
4322 _BT_LOGE_DUP_ATTR(node
, "minor", "trace");
4327 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
4331 "Unexpected unary expression for trace's `minor` attribute.");
4336 _SET(set
, _TRACE_MINOR_SET
);
4337 } else if (!strcmp(left
, "uuid")) {
4338 if (_IS_SET(set
, _TRACE_UUID_SET
)) {
4339 _BT_LOGE_DUP_ATTR(node
, "uuid", "trace");
4344 ret
= get_unary_uuid(&node
->u
.ctf_expression
.right
,
4348 "Invalid trace's `uuid` attribute.");
4352 ret
= bt_ctf_trace_set_uuid(ctx
->trace
, ctx
->trace_uuid
);
4354 _BT_LOGE_NODE(node
, "Cannot set trace's UUID.");
4358 _SET(set
, _TRACE_UUID_SET
);
4359 } else if (!strcmp(left
, "byte_order")) {
4360 /* Native byte order is already known at this stage */
4361 if (_IS_SET(set
, _TRACE_BYTE_ORDER_SET
)) {
4362 _BT_LOGE_DUP_ATTR(node
, "byte_order",
4368 _SET(set
, _TRACE_BYTE_ORDER_SET
);
4369 } else if (!strcmp(left
, "packet.header")) {
4370 if (_IS_SET(set
, _TRACE_PACKET_HEADER_SET
)) {
4372 "Duplicate `packet.header` entry in trace.");
4377 ret
= visit_type_specifier_list(ctx
,
4378 _BT_LIST_FIRST_ENTRY(
4379 &node
->u
.ctf_expression
.right
,
4380 struct ctf_node
, siblings
),
4381 &packet_header_decl
);
4384 "Cannot create trace's packet header field type.");
4388 assert(packet_header_decl
);
4389 ret
= bt_ctf_trace_set_packet_header_type(ctx
->trace
,
4390 packet_header_decl
);
4391 BT_PUT(packet_header_decl
);
4394 "Cannot set trace's packet header field type.");
4398 _SET(set
, _TRACE_PACKET_HEADER_SET
);
4401 "Unknown attribute in stream class: "
4402 "attr-name=\"%s\"", left
);
4410 _BT_LOGE_NODE(node
, "Unknown expression in trace.");
4419 BT_PUT(packet_header_decl
);
4425 int visit_trace_decl(struct ctx
*ctx
, struct ctf_node
*node
)
4429 struct ctf_node
*iter
;
4430 struct bt_list_head
*decl_list
= &node
->u
.trace
.declaration_list
;
4432 if (node
->visited
) {
4436 node
->visited
= TRUE
;
4438 if (ctx
->is_trace_visited
) {
4439 _BT_LOGE_NODE(node
, "Duplicate trace (`trace` block).");
4444 ret
= ctx_push_scope(ctx
);
4446 BT_LOGE_STR("Cannot push scope.");
4450 bt_list_for_each_entry(iter
, decl_list
, siblings
) {
4451 ret
= visit_trace_decl_entry(ctx
, iter
, &set
);
4453 _BT_LOGE_NODE(iter
, "Cannot visit trace's entry (`trace` block): "
4462 if (!_IS_SET(&set
, _TRACE_MAJOR_SET
)) {
4464 "Missing `major` attribute in trace (`trace` block).");
4469 if (!_IS_SET(&set
, _TRACE_MINOR_SET
)) {
4471 "Missing `minor` attribute in trace (`trace` block).");
4476 if (!_IS_SET(&set
, _TRACE_BYTE_ORDER_SET
)) {
4478 "Missing `byte_order` attribute in trace (`trace` block).");
4483 ctx
->is_trace_visited
= TRUE
;
4493 int visit_env(struct ctx
*ctx
, struct ctf_node
*node
)
4497 struct ctf_node
*entry_node
;
4498 struct bt_list_head
*decl_list
= &node
->u
.env
.declaration_list
;
4500 if (node
->visited
) {
4504 node
->visited
= TRUE
;
4506 bt_list_for_each_entry(entry_node
, decl_list
, siblings
) {
4507 struct bt_list_head
*right_head
=
4508 &entry_node
->u
.ctf_expression
.right
;
4510 if (entry_node
->type
!= NODE_CTF_EXPRESSION
) {
4511 _BT_LOGE_NODE(entry_node
,
4512 "Wrong expression in environment entry: "
4513 "node-type=%d", entry_node
->type
);
4518 left
= concatenate_unary_strings(
4519 &entry_node
->u
.ctf_expression
.left
);
4521 _BT_LOGE_NODE(entry_node
,
4522 "Cannot get environment entry's name.");
4527 if (is_unary_string(right_head
)) {
4528 char *right
= concatenate_unary_strings(right_head
);
4531 _BT_LOGE_NODE(entry_node
,
4532 "Unexpected unary expression for environment entry's value: "
4533 "name=\"%s\"", left
);
4538 if (strcmp(left
, "tracer_name") == 0) {
4539 if (strncmp(right
, "lttng", 5) == 0) {
4540 BT_LOGI("Detected LTTng trace from `%s` environment value: "
4541 "tracer-name=\"%s\"",
4547 ret
= bt_ctf_trace_set_environment_field_string(
4548 ctx
->trace
, left
, right
);
4552 _BT_LOGE_NODE(entry_node
,
4553 "Cannot add string environment entry to trace: "
4554 "name=\"%s\", ret=%d", left
, ret
);
4557 } else if (is_unary_unsigned(right_head
) ||
4558 is_unary_signed(right_head
)) {
4561 if (is_unary_unsigned(right_head
)) {
4562 ret
= get_unary_unsigned(right_head
,
4565 ret
= get_unary_signed(right_head
, &v
);
4568 _BT_LOGE_NODE(entry_node
,
4569 "Unexpected unary expression for environment entry's value: "
4570 "name=\"%s\"", left
);
4575 ret
= bt_ctf_trace_set_environment_field_integer(
4576 ctx
->trace
, left
, v
);
4578 _BT_LOGE_NODE(entry_node
,
4579 "Cannot add integer environment entry to trace: "
4580 "name=\"%s\", ret=%d", left
, ret
);
4584 _BT_LOGW_NODE(entry_node
,
4585 "Environment entry has unknown type: "
4586 "name=\"%s\"", left
);
4603 int set_trace_byte_order(struct ctx
*ctx
, struct ctf_node
*trace_node
)
4608 struct ctf_node
*node
;
4609 struct bt_list_head
*decl_list
= &trace_node
->u
.trace
.declaration_list
;
4611 bt_list_for_each_entry(node
, decl_list
, siblings
) {
4612 if (node
->type
== NODE_CTF_EXPRESSION
) {
4613 struct ctf_node
*right_node
;
4615 left
= concatenate_unary_strings(
4616 &node
->u
.ctf_expression
.left
);
4619 "Cannot concatenate unary strings.");
4624 if (!strcmp(left
, "byte_order")) {
4625 enum bt_ctf_byte_order bo
;
4627 if (_IS_SET(&set
, _TRACE_BYTE_ORDER_SET
)) {
4628 _BT_LOGE_DUP_ATTR(node
, "byte_order",
4634 _SET(&set
, _TRACE_BYTE_ORDER_SET
);
4635 right_node
= _BT_LIST_FIRST_ENTRY(
4636 &node
->u
.ctf_expression
.right
,
4637 struct ctf_node
, siblings
);
4638 bo
= byte_order_from_unary_expr(right_node
);
4639 if (bo
== BT_CTF_BYTE_ORDER_UNKNOWN
) {
4641 "Invalid `byte_order` attribute in trace (`trace` block): "
4642 "expecting `le`, `be`, or `network`.");
4645 } else if (bo
== BT_CTF_BYTE_ORDER_NATIVE
) {
4647 "Invalid `byte_order` attribute in trace (`trace` block): "
4648 "cannot be set to `native` here.");
4654 ret
= bt_ctf_trace_set_native_byte_order(
4658 "Cannot set trace's byte order: "
4669 if (!_IS_SET(&set
, _TRACE_BYTE_ORDER_SET
)) {
4670 _BT_LOGE_NODE(trace_node
,
4671 "Missing `byte_order` attribute in trace (`trace` block).");
4685 int visit_clock_decl_entry(struct ctx
*ctx
, struct ctf_node
*entry_node
,
4686 struct bt_ctf_clock_class
*clock
, int *set
)
4691 if (entry_node
->type
!= NODE_CTF_EXPRESSION
) {
4692 _BT_LOGE_NODE(entry_node
,
4693 "Unexpected node type: node-type=%d",
4699 left
= concatenate_unary_strings(&entry_node
->u
.ctf_expression
.left
);
4701 _BT_LOGE_NODE(entry_node
, "Cannot concatenate unary strings.");
4706 if (!strcmp(left
, "name")) {
4709 if (_IS_SET(set
, _CLOCK_NAME_SET
)) {
4710 _BT_LOGE_DUP_ATTR(entry_node
, "name", "clock class");
4715 right
= concatenate_unary_strings(
4716 &entry_node
->u
.ctf_expression
.right
);
4718 _BT_LOGE_NODE(entry_node
,
4719 "Unexpected unary expression for clock class's `name` attribute.");
4724 ret
= bt_ctf_clock_class_set_name(clock
, right
);
4726 _BT_LOGE_NODE(entry_node
,
4727 "cannot set clock class's name");
4733 _SET(set
, _CLOCK_NAME_SET
);
4734 } else if (!strcmp(left
, "uuid")) {
4735 unsigned char uuid
[BABELTRACE_UUID_LEN
];
4737 if (_IS_SET(set
, _CLOCK_UUID_SET
)) {
4738 _BT_LOGE_DUP_ATTR(entry_node
, "uuid", "clock class");
4743 ret
= get_unary_uuid(&entry_node
->u
.ctf_expression
.right
, uuid
);
4745 _BT_LOGE_NODE(entry_node
,
4746 "Invalid clock class's `uuid` attribute.");
4750 ret
= bt_ctf_clock_class_set_uuid(clock
, uuid
);
4752 _BT_LOGE_NODE(entry_node
,
4753 "Cannot set clock class's UUID.");
4757 _SET(set
, _CLOCK_UUID_SET
);
4758 } else if (!strcmp(left
, "description")) {
4761 if (_IS_SET(set
, _CLOCK_DESCRIPTION_SET
)) {
4762 _BT_LOGE_DUP_ATTR(entry_node
, "description",
4768 right
= concatenate_unary_strings(
4769 &entry_node
->u
.ctf_expression
.right
);
4771 _BT_LOGE_NODE(entry_node
,
4772 "Unexpected unary expression for clock class's `description` attribute.");
4777 ret
= bt_ctf_clock_class_set_description(clock
, right
);
4779 _BT_LOGE_NODE(entry_node
,
4780 "Cannot set clock class's description.");
4786 _SET(set
, _CLOCK_DESCRIPTION_SET
);
4787 } else if (!strcmp(left
, "freq")) {
4790 if (_IS_SET(set
, _CLOCK_FREQ_SET
)) {
4791 _BT_LOGE_DUP_ATTR(entry_node
, "freq", "clock class");
4796 ret
= get_unary_unsigned(
4797 &entry_node
->u
.ctf_expression
.right
, &freq
);
4799 _BT_LOGE_NODE(entry_node
,
4800 "Unexpected unary expression for clock class's `freq` attribute.");
4805 if (freq
== -1ULL || freq
== 0) {
4806 _BT_LOGE_NODE(entry_node
,
4807 "Invalid clock class frequency: freq=%" PRIu64
,
4813 ret
= bt_ctf_clock_class_set_frequency(clock
, freq
);
4815 _BT_LOGE_NODE(entry_node
,
4816 "Cannot set clock class's frequency.");
4820 _SET(set
, _CLOCK_FREQ_SET
);
4821 } else if (!strcmp(left
, "precision")) {
4824 if (_IS_SET(set
, _CLOCK_PRECISION_SET
)) {
4825 _BT_LOGE_DUP_ATTR(entry_node
, "precision",
4831 ret
= get_unary_unsigned(
4832 &entry_node
->u
.ctf_expression
.right
, &precision
);
4834 _BT_LOGE_NODE(entry_node
,
4835 "Unexpected unary expression for clock class's `precision` attribute.");
4840 ret
= bt_ctf_clock_class_set_precision(clock
, precision
);
4842 _BT_LOGE_NODE(entry_node
,
4843 "Cannot set clock class's precision.");
4847 _SET(set
, _CLOCK_PRECISION_SET
);
4848 } else if (!strcmp(left
, "offset_s")) {
4851 if (_IS_SET(set
, _CLOCK_OFFSET_S_SET
)) {
4852 _BT_LOGE_DUP_ATTR(entry_node
, "offset_s",
4858 ret
= get_unary_signed(
4859 &entry_node
->u
.ctf_expression
.right
, &offset_s
);
4861 _BT_LOGE_NODE(entry_node
,
4862 "Unexpected unary expression for clock class's `offset_s` attribute.");
4867 ret
= bt_ctf_clock_class_set_offset_s(clock
, offset_s
);
4869 _BT_LOGE_NODE(entry_node
,
4870 "Cannot set clock class's offset in seconds.");
4874 _SET(set
, _CLOCK_OFFSET_S_SET
);
4875 } else if (!strcmp(left
, "offset")) {
4878 if (_IS_SET(set
, _CLOCK_OFFSET_SET
)) {
4879 _BT_LOGE_DUP_ATTR(entry_node
, "offset", "clock class");
4884 ret
= get_unary_signed(
4885 &entry_node
->u
.ctf_expression
.right
, &offset
);
4887 _BT_LOGE_NODE(entry_node
,
4888 "Unexpected unary expression for clock class's `offset` attribute.");
4893 ret
= bt_ctf_clock_class_set_offset_cycles(clock
, offset
);
4895 _BT_LOGE_NODE(entry_node
,
4896 "Cannot set clock class's offset in cycles.");
4900 _SET(set
, _CLOCK_OFFSET_SET
);
4901 } else if (!strcmp(left
, "absolute")) {
4902 struct ctf_node
*right
;
4904 if (_IS_SET(set
, _CLOCK_ABSOLUTE_SET
)) {
4905 _BT_LOGE_DUP_ATTR(entry_node
, "absolute",
4911 right
= _BT_LIST_FIRST_ENTRY(
4912 &entry_node
->u
.ctf_expression
.right
,
4913 struct ctf_node
, siblings
);
4914 ret
= get_boolean(right
);
4916 _BT_LOGE_NODE(entry_node
,
4917 "Unexpected unary expression for clock class's `absolute` attribute.");
4922 ret
= bt_ctf_clock_class_set_is_absolute(clock
, ret
);
4924 _BT_LOGE_NODE(entry_node
,
4925 "Cannot set clock class's absolute flag.");
4929 _SET(set
, _CLOCK_ABSOLUTE_SET
);
4931 _BT_LOGW_NODE(entry_node
,
4932 "Unknown attribute in clock class: attr-name=\"%s\"",
4948 int64_t cycles_from_ns(uint64_t frequency
, int64_t ns
)
4953 if (frequency
== 1000000000ULL) {
4956 cycles
= (uint64_t) (((double) ns
* (double) frequency
) / 1e9
);
4963 int apply_clock_class_offset(struct ctx
*ctx
, struct bt_ctf_clock_class
*clock
)
4967 int64_t offset_cycles
;
4968 int64_t offset_to_apply
;
4970 freq
= bt_ctf_clock_class_get_frequency(clock
);
4971 if (freq
== -1ULL) {
4972 BT_LOGE_STR("Cannot get clock class's frequency.");
4977 ret
= bt_ctf_clock_class_get_offset_cycles(clock
, &offset_cycles
);
4979 BT_LOGE_STR("Cannot get clock class's offset in cycles.");
4985 ctx
->decoder_config
.clock_class_offset_s
* 1000000000LL +
4986 ctx
->decoder_config
.clock_class_offset_ns
;
4987 offset_cycles
+= cycles_from_ns(freq
, offset_to_apply
);
4988 ret
= bt_ctf_clock_class_set_offset_cycles(clock
, offset_cycles
);
4995 int visit_clock_decl(struct ctx
*ctx
, struct ctf_node
*clock_node
)
4999 struct bt_ctf_clock_class
*clock
;
5000 struct ctf_node
*entry_node
;
5001 struct bt_list_head
*decl_list
= &clock_node
->u
.clock
.declaration_list
;
5002 const char *clock_class_name
;
5004 if (clock_node
->visited
) {
5008 clock_node
->visited
= TRUE
;
5010 /* CTF 1.8's default frequency for a clock class is 1 GHz */
5011 clock
= bt_ctf_clock_class_create(NULL
, 1000000000);
5013 _BT_LOGE_NODE(clock_node
,
5014 "Cannot create default clock class.");
5019 bt_list_for_each_entry(entry_node
, decl_list
, siblings
) {
5020 ret
= visit_clock_decl_entry(ctx
, entry_node
, clock
, &set
);
5022 _BT_LOGE_NODE(entry_node
,
5023 "Cannot visit clock class's entry: ret=%d",
5029 if (!_IS_SET(&set
, _CLOCK_NAME_SET
)) {
5030 _BT_LOGE_NODE(clock_node
,
5031 "Missing `name` attribute in clock class.");
5036 clock_class_name
= bt_ctf_clock_class_get_name(clock
);
5037 assert(clock_class_name
);
5038 if (ctx
->is_lttng
&& strcmp(clock_class_name
, "monotonic") == 0) {
5040 * Old versions of LTTng forgot to set its clock class
5041 * as absolute, even if it is. This is important because
5042 * it's a condition to be able to sort notifications
5043 * from different sources.
5045 ret
= bt_ctf_clock_class_set_is_absolute(clock
, 1);
5047 _BT_LOGE_NODE(clock_node
,
5048 "Cannot set clock class's absolute flag.");
5053 ret
= apply_clock_class_offset(ctx
, clock
);
5055 _BT_LOGE_NODE(clock_node
,
5056 "Cannot apply clock class's custom offset.");
5060 ret
= bt_ctf_trace_add_clock_class(ctx
->trace
, clock
);
5062 _BT_LOGE_NODE(clock_node
,
5063 "Cannot add clock class to trace.");
5074 int visit_root_decl(struct ctx
*ctx
, struct ctf_node
*root_decl_node
)
5078 if (root_decl_node
->visited
) {
5082 root_decl_node
->visited
= TRUE
;
5084 switch (root_decl_node
->type
) {
5086 ret
= visit_typedef(ctx
,
5087 root_decl_node
->u
._typedef
.type_specifier_list
,
5088 &root_decl_node
->u
._typedef
.type_declarators
);
5090 _BT_LOGE_NODE(root_decl_node
,
5091 "Cannot add type definition found in root scope.");
5095 case NODE_TYPEALIAS
:
5096 ret
= visit_typealias(ctx
, root_decl_node
->u
.typealias
.target
,
5097 root_decl_node
->u
.typealias
.alias
);
5099 _BT_LOGE_NODE(root_decl_node
,
5100 "Cannot add type alias found in root scope.");
5104 case NODE_TYPE_SPECIFIER_LIST
:
5106 _BT_CTF_FIELD_TYPE_INIT(decl
);
5109 * Just add the type specifier to the root
5110 * declaration scope. Put local reference.
5112 ret
= visit_type_specifier_list(ctx
, root_decl_node
, &decl
);
5114 _BT_LOGE_NODE(root_decl_node
,
5115 "Cannot visit root scope's field type: "
5125 _BT_LOGE_NODE(root_decl_node
,
5126 "Unexpected node type: node-type=%d",
5127 root_decl_node
->type
);
5137 int set_trace_name(struct ctx
*ctx
)
5141 struct bt_value
*value
= NULL
;
5143 assert(bt_ctf_trace_get_stream_class_count(ctx
->trace
) == 0);
5144 name
= g_string_new(NULL
);
5146 BT_LOGE_STR("Failed to allocate a GString.");
5152 * Check if we have a trace environment string value named `hostname`.
5153 * If so, use it as the trace name's prefix.
5155 value
= bt_ctf_trace_get_environment_field_value_by_name(ctx
->trace
,
5157 if (bt_value_is_string(value
)) {
5158 const char *hostname
;
5160 ret
= bt_value_string_get(value
, &hostname
);
5162 g_string_append(name
, hostname
);
5164 if (ctx
->trace_name_suffix
) {
5165 g_string_append_c(name
, G_DIR_SEPARATOR
);
5169 if (ctx
->trace_name_suffix
) {
5170 g_string_append(name
, ctx
->trace_name_suffix
);
5173 ret
= bt_ctf_trace_set_name(ctx
->trace
, name
->str
);
5175 BT_LOGE("Cannot set trace's name: name=\"%s\"", name
->str
);
5188 g_string_free(name
, TRUE
);
5195 int move_ctx_stream_classes_to_trace(struct ctx
*ctx
)
5198 GHashTableIter iter
;
5199 gpointer key
, stream_class
;
5201 if (g_hash_table_size(ctx
->stream_classes
) > 0 &&
5202 bt_ctf_trace_get_stream_class_count(ctx
->trace
) == 0) {
5204 * We're about to add the first stream class to the
5205 * trace. This will freeze the trace, and after this
5206 * we cannot set the name anymore. At this point,
5207 * set the trace name.
5209 ret
= set_trace_name(ctx
);
5211 BT_LOGE_STR("Cannot set trace's name.");
5216 g_hash_table_iter_init(&iter
, ctx
->stream_classes
);
5218 while (g_hash_table_iter_next(&iter
, &key
, &stream_class
)) {
5219 ret
= bt_ctf_trace_add_stream_class(ctx
->trace
,
5222 int64_t id
= bt_ctf_stream_class_get_id(stream_class
);
5223 BT_LOGE("Cannot add stream class to trace: id=%" PRId64
,
5229 g_hash_table_remove_all(ctx
->stream_classes
);
5236 struct ctf_visitor_generate_ir
*ctf_visitor_generate_ir_create(
5237 const struct ctf_metadata_decoder_config
*decoder_config
,
5241 struct ctx
*ctx
= NULL
;
5242 struct bt_ctf_trace
*trace
;
5244 trace
= bt_ctf_trace_create();
5246 BT_LOGE_STR("Cannot create empty trace.");
5250 /* Set packet header to NULL to override the default one */
5251 ret
= bt_ctf_trace_set_packet_header_type(trace
, NULL
);
5253 BT_LOGE_STR("Cannot reset initial trace's packet header field type.");
5257 /* Create visitor's context */
5258 ctx
= ctx_create(trace
, decoder_config
, name
);
5260 BT_LOGE_STR("Cannot create visitor's context.");
5273 return (void *) ctx
;
5277 void ctf_visitor_generate_ir_destroy(struct ctf_visitor_generate_ir
*visitor
)
5279 ctx_destroy((void *) visitor
);
5283 struct bt_ctf_trace
*ctf_visitor_generate_ir_get_trace(
5284 struct ctf_visitor_generate_ir
*visitor
)
5286 struct ctx
*ctx
= (void *) visitor
;
5290 return bt_get(ctx
->trace
);
5294 int ctf_visitor_generate_ir_visit_node(struct ctf_visitor_generate_ir
*visitor
,
5295 struct ctf_node
*node
)
5298 struct ctx
*ctx
= (void *) visitor
;
5300 BT_LOGI_STR("Visiting metadata's AST to generate CTF IR objects.");
5302 switch (node
->type
) {
5305 struct ctf_node
*iter
;
5306 int got_trace_decl
= FALSE
;
5309 * The first thing we need is the native byte order of
5310 * the trace block, because early type aliases can have
5311 * a `byte_order` attribute set to `native`. If we don't
5312 * have the native byte order yet, and we don't have any
5313 * trace block yet, then fail with EINCOMPLETE.
5315 if (ctx
->trace_bo
== BT_CTF_BYTE_ORDER_NATIVE
) {
5316 bt_list_for_each_entry(iter
, &node
->u
.root
.trace
, siblings
) {
5317 if (got_trace_decl
) {
5319 "Duplicate trace (`trace` block).");
5324 ret
= set_trace_byte_order(ctx
, iter
);
5327 "Cannot set trace's native byte order: "
5332 got_trace_decl
= TRUE
;
5335 if (!got_trace_decl
) {
5336 BT_LOGD_STR("Incomplete AST: need trace (`trace` block).");
5342 assert(ctx
->trace_bo
== BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
||
5343 ctx
->trace_bo
== BT_CTF_BYTE_ORDER_BIG_ENDIAN
);
5344 assert(ctx
->current_scope
&&
5345 ctx
->current_scope
->parent_scope
== NULL
);
5348 bt_list_for_each_entry(iter
, &node
->u
.root
.env
, siblings
) {
5349 ret
= visit_env(ctx
, iter
);
5352 "Cannot visit trace's environment (`env` block) entry: "
5358 assert(ctx
->current_scope
&&
5359 ctx
->current_scope
->parent_scope
== NULL
);
5362 * Visit clock blocks.
5364 bt_list_for_each_entry(iter
, &node
->u
.root
.clock
, siblings
) {
5365 ret
= visit_clock_decl(ctx
, iter
);
5368 "Cannot visit clock class: ret=%d",
5374 assert(ctx
->current_scope
&&
5375 ctx
->current_scope
->parent_scope
== NULL
);
5378 * Visit root declarations next, as they can be used by any
5381 bt_list_for_each_entry(iter
, &node
->u
.root
.declaration_list
,
5383 ret
= visit_root_decl(ctx
, iter
);
5386 "Cannot visit root entry: ret=%d",
5392 assert(ctx
->current_scope
&&
5393 ctx
->current_scope
->parent_scope
== NULL
);
5395 /* Callsite blocks are not supported */
5396 bt_list_for_each_entry(iter
, &node
->u
.root
.callsite
, siblings
) {
5398 "\"callsite\" blocks are not supported as of this version.");
5401 assert(ctx
->current_scope
&&
5402 ctx
->current_scope
->parent_scope
== NULL
);
5405 bt_list_for_each_entry(iter
, &node
->u
.root
.trace
, siblings
) {
5406 ret
= visit_trace_decl(ctx
, iter
);
5409 "Cannot visit trace (`trace` block): "
5415 assert(ctx
->current_scope
&&
5416 ctx
->current_scope
->parent_scope
== NULL
);
5419 bt_list_for_each_entry(iter
, &node
->u
.root
.stream
, siblings
) {
5420 ret
= visit_stream_decl(ctx
, iter
);
5423 "Cannot visit stream class: ret=%d",
5429 assert(ctx
->current_scope
&&
5430 ctx
->current_scope
->parent_scope
== NULL
);
5433 bt_list_for_each_entry(iter
, &node
->u
.root
.event
, siblings
) {
5434 ret
= visit_event_decl(ctx
, iter
);
5437 "Cannot visit event class: ret=%d",
5443 assert(ctx
->current_scope
&&
5444 ctx
->current_scope
->parent_scope
== NULL
);
5449 "Unexpected node type: node-type=%d",
5455 /* Move decoded stream classes to trace, if any */
5456 ret
= move_ctx_stream_classes_to_trace(ctx
);
5458 BT_LOGE("Cannot move stream classes to trace: ret=%d", ret
);