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
- 1ULL));
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
)) {
1653 _BT_LOGE_NODE(target
,
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) {
1665 _BT_LOGE_NODE(target
,
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 */
3638 new_stream_class
= create_reset_stream_class(ctx
);
3639 if (!new_stream_class
) {
3641 "Cannot create empty stream class.");
3646 ret
= bt_ctf_stream_class_set_id(new_stream_class
,
3650 "Cannot set stream class's ID: "
3651 "id=0, ret=%d", ret
);
3652 BT_PUT(new_stream_class
);
3656 new_stream_id
= g_new0(int64_t, 1);
3657 if (!new_stream_id
) {
3658 BT_LOGE_STR("Failed to allocate a int64_t.");
3663 *new_stream_id
= stream_id
;
3665 /* Move reference to visitor's context */
3666 g_hash_table_insert(ctx
->stream_classes
,
3667 new_stream_id
, new_stream_class
);
3668 new_stream_id
= NULL
;
3669 new_stream_class
= NULL
;
3672 /* Single stream class: get its ID */
3673 if (g_hash_table_size(ctx
->stream_classes
) == 1) {
3674 keys
= g_hash_table_get_keys(ctx
->stream_classes
);
3675 stream_id
= *((int64_t *) keys
->data
);
3678 assert(bt_ctf_trace_get_stream_class_count(
3681 bt_ctf_trace_get_stream_class_by_index(
3683 assert(stream_class
);
3684 stream_id
= bt_ctf_stream_class_get_id(
3686 BT_PUT(stream_class
);
3691 "Missing `stream_id` attribute in event class.");
3697 assert(stream_id
>= 0);
3699 /* We have the stream ID now; get the stream class if found */
3700 stream_class
= g_hash_table_lookup(ctx
->stream_classes
, &stream_id
);
3701 bt_get(stream_class
);
3702 if (!stream_class
) {
3703 stream_class
= bt_ctf_trace_get_stream_class_by_id(ctx
->trace
,
3705 if (!stream_class
) {
3707 "Cannot find stream class at this point: "
3708 "id=%" PRId64
, stream_id
);
3714 assert(stream_class
);
3716 if (!_IS_SET(&set
, _EVENT_ID_SET
)) {
3717 /* Allow only one event without ID per stream */
3718 if (bt_ctf_stream_class_get_event_class_count(stream_class
) !=
3721 "Missing `id` attribute in event class.");
3727 ret
= bt_ctf_event_class_set_id(event_class
, 0);
3730 "Cannot set event class's ID: id=0, ret=%d",
3736 event_id
= bt_ctf_event_class_get_id(event_class
);
3738 _BT_LOGE_NODE(node
, "Cannot get event class's ID.");
3743 eevent_class
= bt_ctf_stream_class_get_event_class_by_id(stream_class
,
3746 BT_PUT(eevent_class
);
3748 "Duplicate event class (same ID) in the same stream class: "
3749 "id=%" PRId64
, event_id
);
3754 ret
= bt_ctf_stream_class_add_event_class(stream_class
, event_class
);
3755 BT_PUT(event_class
);
3758 "Cannot add event class to stream class: ret=%d", ret
);
3765 bt_put(event_class
);
3773 bt_put(stream_class
);
3778 int auto_map_field_to_trace_clock_class(struct ctx
*ctx
,
3779 struct bt_ctf_field_type
*ft
)
3781 struct bt_ctf_clock_class
*clock_class_to_map_to
= NULL
;
3782 struct bt_ctf_clock_class
*mapped_clock_class
= NULL
;
3784 int64_t clock_class_count
;
3786 if (!ft
|| !bt_ctf_field_type_is_integer(ft
)) {
3790 mapped_clock_class
=
3791 bt_ctf_field_type_integer_get_mapped_clock_class(ft
);
3792 if (mapped_clock_class
) {
3796 clock_class_count
= bt_ctf_trace_get_clock_class_count(ctx
->trace
);
3797 assert(clock_class_count
>= 0);
3799 switch (clock_class_count
) {
3802 * No clock class exists in the trace at this
3803 * point. Create an implicit one at 1 GHz,
3804 * named `default`, and use this clock class.
3806 clock_class_to_map_to
= bt_ctf_clock_class_create("default",
3808 if (!clock_class_to_map_to
) {
3809 BT_LOGE_STR("Cannot create a clock class.");
3814 ret
= bt_ctf_trace_add_clock_class(ctx
->trace
,
3815 clock_class_to_map_to
);
3817 BT_LOGE_STR("Cannot add clock class to trace.");
3823 * Only one clock class exists in the trace at
3824 * this point: use this one.
3826 clock_class_to_map_to
=
3827 bt_ctf_trace_get_clock_class_by_index(ctx
->trace
, 0);
3828 assert(clock_class_to_map_to
);
3832 * Timestamp field not mapped to a clock class
3833 * and there's more than one clock class in the
3834 * trace: this is an error.
3836 BT_LOGE_STR("Timestamp field found with no mapped clock class, "
3837 "but there's more than one clock class in the trace at this point.");
3842 assert(clock_class_to_map_to
);
3843 ret
= bt_ctf_field_type_integer_set_mapped_clock_class(ft
,
3844 clock_class_to_map_to
);
3846 BT_LOGE("Cannot map field type's field to trace's clock class: "
3847 "clock-class-name=\"%s\", ret=%d",
3848 bt_ctf_clock_class_get_name(clock_class_to_map_to
),
3854 bt_put(clock_class_to_map_to
);
3855 bt_put(mapped_clock_class
);
3860 int auto_map_fields_to_trace_clock_class(struct ctx
*ctx
,
3861 struct bt_ctf_field_type
*root_ft
, const char *field_name
)
3870 if (!bt_ctf_field_type_is_structure(root_ft
) &&
3871 !bt_ctf_field_type_is_variant(root_ft
)) {
3875 if (bt_ctf_field_type_is_structure(root_ft
)) {
3876 count
= bt_ctf_field_type_structure_get_field_count(root_ft
);
3878 count
= bt_ctf_field_type_variant_get_field_count(root_ft
);
3883 for (i
= 0; i
< count
; i
++) {
3884 _BT_CTF_FIELD_TYPE_INIT(ft
);
3887 if (bt_ctf_field_type_is_structure(root_ft
)) {
3888 ret
= bt_ctf_field_type_structure_get_field_by_index(
3889 root_ft
, &name
, &ft
, i
);
3890 } else if (bt_ctf_field_type_is_variant(root_ft
)) {
3891 ret
= bt_ctf_field_type_variant_get_field_by_index(
3892 root_ft
, &name
, &ft
, i
);
3897 if (strcmp(name
, field_name
) == 0) {
3898 ret
= auto_map_field_to_trace_clock_class(ctx
, ft
);
3900 BT_LOGE("Cannot automatically map field to trace's clock class: "
3901 "field-name=\"%s\"", field_name
);
3907 ret
= auto_map_fields_to_trace_clock_class(ctx
, ft
, field_name
);
3910 BT_LOGE("Cannot automatically map structure or variant field type's fields to trace's clock class: "
3911 "field-name=\"%s\", root-field-name=\"%s\"",
3922 int visit_stream_decl_entry(struct ctx
*ctx
, struct ctf_node
*node
,
3923 struct bt_ctf_stream_class
*stream_class
, int *set
)
3927 _BT_CTF_FIELD_TYPE_INIT(decl
);
3929 switch (node
->type
) {
3931 ret
= visit_typedef(ctx
, node
->u
._typedef
.type_specifier_list
,
3932 &node
->u
._typedef
.type_declarators
);
3935 "Cannot add type definition found in stream class.");
3939 case NODE_TYPEALIAS
:
3940 ret
= visit_typealias(ctx
, node
->u
.typealias
.target
,
3941 node
->u
.typealias
.alias
);
3944 "Cannot add type alias found in stream class.");
3948 case NODE_CTF_EXPRESSION
:
3950 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
3952 _BT_LOGE_NODE(node
, "Cannot concatenate unary strings.");
3957 if (!strcmp(left
, "id")) {
3961 if (_IS_SET(set
, _STREAM_ID_SET
)) {
3962 _BT_LOGE_DUP_ATTR(node
, "id",
3963 "stream declaration");
3968 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
3970 /* Only read "id" if get_unary_unsigned() succeeded. */
3971 if (ret
|| (!ret
&& id
< 0)) {
3973 "Unexpected unary expression for stream class's `id` attribute.");
3978 ptr
= g_hash_table_lookup(ctx
->stream_classes
, &id
);
3981 "Duplicate stream class (same ID): id=%" PRId64
,
3987 ret
= bt_ctf_stream_class_set_id(stream_class
, id
);
3990 "Cannot set stream class's ID: "
3991 "id=%" PRId64
", ret=%d", id
, ret
);
3995 _SET(set
, _STREAM_ID_SET
);
3996 } else if (!strcmp(left
, "event.header")) {
3997 if (_IS_SET(set
, _STREAM_EVENT_HEADER_SET
)) {
3999 "Duplicate `event.header` entry in stream class.");
4004 ret
= visit_type_specifier_list(ctx
,
4005 _BT_LIST_FIRST_ENTRY(
4006 &node
->u
.ctf_expression
.right
,
4007 struct ctf_node
, siblings
),
4011 "Cannot create stream class's event header field type.");
4016 ret
= auto_map_fields_to_trace_clock_class(ctx
,
4020 "Cannot automatically map specific event header field type fields named `timestamp` to trace's clock class.");
4024 ret
= bt_ctf_stream_class_set_event_header_type(
4025 stream_class
, decl
);
4029 "Cannot set stream class's event header field type.");
4033 _SET(set
, _STREAM_EVENT_HEADER_SET
);
4034 } else if (!strcmp(left
, "event.context")) {
4035 if (_IS_SET(set
, _STREAM_EVENT_CONTEXT_SET
)) {
4037 "Duplicate `event.context` entry in stream class.");
4042 ret
= visit_type_specifier_list(ctx
,
4043 _BT_LIST_FIRST_ENTRY(
4044 &node
->u
.ctf_expression
.right
,
4045 struct ctf_node
, siblings
),
4049 "Cannot create stream class's event context field type.");
4055 ret
= bt_ctf_stream_class_set_event_context_type(
4056 stream_class
, decl
);
4060 "Cannot set stream class's event context field type.");
4064 _SET(set
, _STREAM_EVENT_CONTEXT_SET
);
4065 } else if (!strcmp(left
, "packet.context")) {
4066 if (_IS_SET(set
, _STREAM_PACKET_CONTEXT_SET
)) {
4068 "Duplicate `packet.context` entry in stream class.");
4073 ret
= visit_type_specifier_list(ctx
,
4074 _BT_LIST_FIRST_ENTRY(
4075 &node
->u
.ctf_expression
.right
,
4076 struct ctf_node
, siblings
),
4080 "Cannot create stream class's packet context field type.");
4085 ret
= auto_map_fields_to_trace_clock_class(ctx
,
4086 decl
, "timestamp_begin");
4089 "Cannot automatically map specific packet context field type fields named `timestamp_begin` to trace's clock class.");
4093 ret
= auto_map_fields_to_trace_clock_class(ctx
,
4094 decl
, "timestamp_end");
4097 "Cannot automatically map specific packet context field type fields named `timestamp_end` to trace's clock class.");
4101 ret
= bt_ctf_stream_class_set_packet_context_type(
4102 stream_class
, decl
);
4106 "Cannot set stream class's packet context field type.");
4110 _SET(set
, _STREAM_PACKET_CONTEXT_SET
);
4113 "Unknown attribute in stream class: "
4114 "attr-name=\"%s\"", left
);
4137 int visit_stream_decl(struct ctx
*ctx
, struct ctf_node
*node
)
4143 struct ctf_node
*iter
;
4144 struct bt_ctf_stream_class
*stream_class
= NULL
;
4145 struct bt_ctf_stream_class
*existing_stream_class
= NULL
;
4146 struct bt_list_head
*decl_list
= &node
->u
.stream
.declaration_list
;
4148 if (node
->visited
) {
4152 node
->visited
= TRUE
;
4153 stream_class
= create_reset_stream_class(ctx
);
4154 if (!stream_class
) {
4155 _BT_LOGE_NODE(node
, "Cannot create empty stream class.");
4160 ret
= ctx_push_scope(ctx
);
4162 BT_LOGE_STR("Cannot push scope.");
4166 bt_list_for_each_entry(iter
, decl_list
, siblings
) {
4167 ret
= visit_stream_decl_entry(ctx
, iter
, stream_class
, &set
);
4170 "Cannot visit stream class's entry: "
4179 if (_IS_SET(&set
, _STREAM_ID_SET
)) {
4180 /* Check that packet header has stream_id field */
4181 _BT_CTF_FIELD_TYPE_INIT(stream_id_decl
);
4182 _BT_CTF_FIELD_TYPE_INIT(packet_header_decl
);
4184 packet_header_decl
=
4185 bt_ctf_trace_get_packet_header_type(ctx
->trace
);
4186 if (!packet_header_decl
) {
4188 "Stream class has a `id` attribute, "
4189 "but trace has no packet header field type.");
4194 bt_ctf_field_type_structure_get_field_type_by_name(
4195 packet_header_decl
, "stream_id");
4196 BT_PUT(packet_header_decl
);
4197 if (!stream_id_decl
) {
4199 "Stream class has a `id` attribute, "
4200 "but trace's packet header field type has no `stream_id` field.");
4204 if (!bt_ctf_field_type_is_integer(stream_id_decl
)) {
4205 BT_PUT(stream_id_decl
);
4207 "Stream class has a `id` attribute, "
4208 "but trace's packet header field type's `stream_id` field is not an integer field type.");
4212 BT_PUT(stream_id_decl
);
4214 /* Allow only _one_ ID-less stream */
4215 if (g_hash_table_size(ctx
->stream_classes
) != 0) {
4217 "Missing `id` attribute in stream class as there's more than one stream class in the trace.");
4222 /* Automatic ID: 0 */
4223 ret
= bt_ctf_stream_class_set_id(stream_class
, 0);
4227 id
= bt_ctf_stream_class_get_id(stream_class
);
4230 "Cannot get stream class's ID.");
4236 * Make sure that this stream class's ID is currently unique in
4239 existing_stream_class
= bt_ctf_trace_get_stream_class_by_id(ctx
->trace
,
4241 if (g_hash_table_lookup(ctx
->stream_classes
, &id
) ||
4242 existing_stream_class
) {
4244 "Duplicate stream class (same ID): id=%" PRId64
,
4250 new_id
= g_new0(int64_t, 1);
4252 BT_LOGE_STR("Failed to allocate a int64_t.");
4258 /* Move reference to visitor's context */
4259 g_hash_table_insert(ctx
->stream_classes
, new_id
,
4261 stream_class
= NULL
;
4265 bt_put(stream_class
);
4268 bt_put(existing_stream_class
);
4273 int visit_trace_decl_entry(struct ctx
*ctx
, struct ctf_node
*node
, int *set
)
4277 _BT_CTF_FIELD_TYPE_INIT(packet_header_decl
);
4279 switch (node
->type
) {
4281 ret
= visit_typedef(ctx
, node
->u
._typedef
.type_specifier_list
,
4282 &node
->u
._typedef
.type_declarators
);
4285 "Cannot add type definition found in trace (`trace` block).");
4289 case NODE_TYPEALIAS
:
4290 ret
= visit_typealias(ctx
, node
->u
.typealias
.target
,
4291 node
->u
.typealias
.alias
);
4294 "Cannot add type alias found in trace (`trace` block).");
4298 case NODE_CTF_EXPRESSION
:
4300 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
4302 _BT_LOGE_NODE(node
, "Cannot concatenate unary strings.");
4307 if (!strcmp(left
, "major")) {
4308 if (_IS_SET(set
, _TRACE_MAJOR_SET
)) {
4309 _BT_LOGE_DUP_ATTR(node
, "major", "trace");
4314 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
4318 "Unexpected unary expression for trace's `major` attribute.");
4323 _SET(set
, _TRACE_MAJOR_SET
);
4324 } else if (!strcmp(left
, "minor")) {
4325 if (_IS_SET(set
, _TRACE_MINOR_SET
)) {
4326 _BT_LOGE_DUP_ATTR(node
, "minor", "trace");
4331 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
4335 "Unexpected unary expression for trace's `minor` attribute.");
4340 _SET(set
, _TRACE_MINOR_SET
);
4341 } else if (!strcmp(left
, "uuid")) {
4342 if (_IS_SET(set
, _TRACE_UUID_SET
)) {
4343 _BT_LOGE_DUP_ATTR(node
, "uuid", "trace");
4348 ret
= get_unary_uuid(&node
->u
.ctf_expression
.right
,
4352 "Invalid trace's `uuid` attribute.");
4356 ret
= bt_ctf_trace_set_uuid(ctx
->trace
, ctx
->trace_uuid
);
4358 _BT_LOGE_NODE(node
, "Cannot set trace's UUID.");
4362 _SET(set
, _TRACE_UUID_SET
);
4363 } else if (!strcmp(left
, "byte_order")) {
4364 /* Native byte order is already known at this stage */
4365 if (_IS_SET(set
, _TRACE_BYTE_ORDER_SET
)) {
4366 _BT_LOGE_DUP_ATTR(node
, "byte_order",
4372 _SET(set
, _TRACE_BYTE_ORDER_SET
);
4373 } else if (!strcmp(left
, "packet.header")) {
4374 if (_IS_SET(set
, _TRACE_PACKET_HEADER_SET
)) {
4376 "Duplicate `packet.header` entry in trace.");
4381 ret
= visit_type_specifier_list(ctx
,
4382 _BT_LIST_FIRST_ENTRY(
4383 &node
->u
.ctf_expression
.right
,
4384 struct ctf_node
, siblings
),
4385 &packet_header_decl
);
4388 "Cannot create trace's packet header field type.");
4392 assert(packet_header_decl
);
4393 ret
= bt_ctf_trace_set_packet_header_type(ctx
->trace
,
4394 packet_header_decl
);
4395 BT_PUT(packet_header_decl
);
4398 "Cannot set trace's packet header field type.");
4402 _SET(set
, _TRACE_PACKET_HEADER_SET
);
4405 "Unknown attribute in stream class: "
4406 "attr-name=\"%s\"", left
);
4414 _BT_LOGE_NODE(node
, "Unknown expression in trace.");
4423 BT_PUT(packet_header_decl
);
4429 int visit_trace_decl(struct ctx
*ctx
, struct ctf_node
*node
)
4433 struct ctf_node
*iter
;
4434 struct bt_list_head
*decl_list
= &node
->u
.trace
.declaration_list
;
4436 if (node
->visited
) {
4440 node
->visited
= TRUE
;
4442 if (ctx
->is_trace_visited
) {
4443 _BT_LOGE_NODE(node
, "Duplicate trace (`trace` block).");
4448 ret
= ctx_push_scope(ctx
);
4450 BT_LOGE_STR("Cannot push scope.");
4454 bt_list_for_each_entry(iter
, decl_list
, siblings
) {
4455 ret
= visit_trace_decl_entry(ctx
, iter
, &set
);
4457 _BT_LOGE_NODE(iter
, "Cannot visit trace's entry (`trace` block): "
4466 if (!_IS_SET(&set
, _TRACE_MAJOR_SET
)) {
4468 "Missing `major` attribute in trace (`trace` block).");
4473 if (!_IS_SET(&set
, _TRACE_MINOR_SET
)) {
4475 "Missing `minor` attribute in trace (`trace` block).");
4480 if (!_IS_SET(&set
, _TRACE_BYTE_ORDER_SET
)) {
4482 "Missing `byte_order` attribute in trace (`trace` block).");
4487 ctx
->is_trace_visited
= TRUE
;
4497 int visit_env(struct ctx
*ctx
, struct ctf_node
*node
)
4501 struct ctf_node
*entry_node
;
4502 struct bt_list_head
*decl_list
= &node
->u
.env
.declaration_list
;
4504 if (node
->visited
) {
4508 node
->visited
= TRUE
;
4510 bt_list_for_each_entry(entry_node
, decl_list
, siblings
) {
4511 struct bt_list_head
*right_head
=
4512 &entry_node
->u
.ctf_expression
.right
;
4514 if (entry_node
->type
!= NODE_CTF_EXPRESSION
) {
4515 _BT_LOGE_NODE(entry_node
,
4516 "Wrong expression in environment entry: "
4517 "node-type=%d", entry_node
->type
);
4522 left
= concatenate_unary_strings(
4523 &entry_node
->u
.ctf_expression
.left
);
4525 _BT_LOGE_NODE(entry_node
,
4526 "Cannot get environment entry's name.");
4531 if (is_unary_string(right_head
)) {
4532 char *right
= concatenate_unary_strings(right_head
);
4535 _BT_LOGE_NODE(entry_node
,
4536 "Unexpected unary expression for environment entry's value: "
4537 "name=\"%s\"", left
);
4542 if (strcmp(left
, "tracer_name") == 0) {
4543 if (strncmp(right
, "lttng", 5) == 0) {
4544 BT_LOGI("Detected LTTng trace from `%s` environment value: "
4545 "tracer-name=\"%s\"",
4551 ret
= bt_ctf_trace_set_environment_field_string(
4552 ctx
->trace
, left
, right
);
4556 _BT_LOGE_NODE(entry_node
,
4557 "Cannot add string environment entry to trace: "
4558 "name=\"%s\", ret=%d", left
, ret
);
4561 } else if (is_unary_unsigned(right_head
) ||
4562 is_unary_signed(right_head
)) {
4565 if (is_unary_unsigned(right_head
)) {
4566 ret
= get_unary_unsigned(right_head
,
4569 ret
= get_unary_signed(right_head
, &v
);
4572 _BT_LOGE_NODE(entry_node
,
4573 "Unexpected unary expression for environment entry's value: "
4574 "name=\"%s\"", left
);
4579 ret
= bt_ctf_trace_set_environment_field_integer(
4580 ctx
->trace
, left
, v
);
4582 _BT_LOGE_NODE(entry_node
,
4583 "Cannot add integer environment entry to trace: "
4584 "name=\"%s\", ret=%d", left
, ret
);
4588 _BT_LOGW_NODE(entry_node
,
4589 "Environment entry has unknown type: "
4590 "name=\"%s\"", left
);
4607 int set_trace_byte_order(struct ctx
*ctx
, struct ctf_node
*trace_node
)
4612 struct ctf_node
*node
;
4613 struct bt_list_head
*decl_list
= &trace_node
->u
.trace
.declaration_list
;
4615 bt_list_for_each_entry(node
, decl_list
, siblings
) {
4616 if (node
->type
== NODE_CTF_EXPRESSION
) {
4617 struct ctf_node
*right_node
;
4619 left
= concatenate_unary_strings(
4620 &node
->u
.ctf_expression
.left
);
4623 "Cannot concatenate unary strings.");
4628 if (!strcmp(left
, "byte_order")) {
4629 enum bt_ctf_byte_order bo
;
4631 if (_IS_SET(&set
, _TRACE_BYTE_ORDER_SET
)) {
4632 _BT_LOGE_DUP_ATTR(node
, "byte_order",
4638 _SET(&set
, _TRACE_BYTE_ORDER_SET
);
4639 right_node
= _BT_LIST_FIRST_ENTRY(
4640 &node
->u
.ctf_expression
.right
,
4641 struct ctf_node
, siblings
);
4642 bo
= byte_order_from_unary_expr(right_node
);
4643 if (bo
== BT_CTF_BYTE_ORDER_UNKNOWN
) {
4645 "Invalid `byte_order` attribute in trace (`trace` block): "
4646 "expecting `le`, `be`, or `network`.");
4649 } else if (bo
== BT_CTF_BYTE_ORDER_NATIVE
) {
4651 "Invalid `byte_order` attribute in trace (`trace` block): "
4652 "cannot be set to `native` here.");
4658 ret
= bt_ctf_trace_set_native_byte_order(
4662 "Cannot set trace's byte order: "
4673 if (!_IS_SET(&set
, _TRACE_BYTE_ORDER_SET
)) {
4674 _BT_LOGE_NODE(trace_node
,
4675 "Missing `byte_order` attribute in trace (`trace` block).");
4689 int visit_clock_decl_entry(struct ctx
*ctx
, struct ctf_node
*entry_node
,
4690 struct bt_ctf_clock_class
*clock
, int *set
)
4695 if (entry_node
->type
!= NODE_CTF_EXPRESSION
) {
4696 _BT_LOGE_NODE(entry_node
,
4697 "Unexpected node type: node-type=%d",
4703 left
= concatenate_unary_strings(&entry_node
->u
.ctf_expression
.left
);
4705 _BT_LOGE_NODE(entry_node
, "Cannot concatenate unary strings.");
4710 if (!strcmp(left
, "name")) {
4713 if (_IS_SET(set
, _CLOCK_NAME_SET
)) {
4714 _BT_LOGE_DUP_ATTR(entry_node
, "name", "clock class");
4719 right
= concatenate_unary_strings(
4720 &entry_node
->u
.ctf_expression
.right
);
4722 _BT_LOGE_NODE(entry_node
,
4723 "Unexpected unary expression for clock class's `name` attribute.");
4728 ret
= bt_ctf_clock_class_set_name(clock
, right
);
4730 _BT_LOGE_NODE(entry_node
,
4731 "cannot set clock class's name");
4737 _SET(set
, _CLOCK_NAME_SET
);
4738 } else if (!strcmp(left
, "uuid")) {
4739 unsigned char uuid
[BABELTRACE_UUID_LEN
];
4741 if (_IS_SET(set
, _CLOCK_UUID_SET
)) {
4742 _BT_LOGE_DUP_ATTR(entry_node
, "uuid", "clock class");
4747 ret
= get_unary_uuid(&entry_node
->u
.ctf_expression
.right
, uuid
);
4749 _BT_LOGE_NODE(entry_node
,
4750 "Invalid clock class's `uuid` attribute.");
4754 ret
= bt_ctf_clock_class_set_uuid(clock
, uuid
);
4756 _BT_LOGE_NODE(entry_node
,
4757 "Cannot set clock class's UUID.");
4761 _SET(set
, _CLOCK_UUID_SET
);
4762 } else if (!strcmp(left
, "description")) {
4765 if (_IS_SET(set
, _CLOCK_DESCRIPTION_SET
)) {
4766 _BT_LOGE_DUP_ATTR(entry_node
, "description",
4772 right
= concatenate_unary_strings(
4773 &entry_node
->u
.ctf_expression
.right
);
4775 _BT_LOGE_NODE(entry_node
,
4776 "Unexpected unary expression for clock class's `description` attribute.");
4781 ret
= bt_ctf_clock_class_set_description(clock
, right
);
4783 _BT_LOGE_NODE(entry_node
,
4784 "Cannot set clock class's description.");
4790 _SET(set
, _CLOCK_DESCRIPTION_SET
);
4791 } else if (!strcmp(left
, "freq")) {
4794 if (_IS_SET(set
, _CLOCK_FREQ_SET
)) {
4795 _BT_LOGE_DUP_ATTR(entry_node
, "freq", "clock class");
4800 ret
= get_unary_unsigned(
4801 &entry_node
->u
.ctf_expression
.right
, &freq
);
4803 _BT_LOGE_NODE(entry_node
,
4804 "Unexpected unary expression for clock class's `freq` attribute.");
4809 if (freq
== -1ULL || freq
== 0) {
4810 _BT_LOGE_NODE(entry_node
,
4811 "Invalid clock class frequency: freq=%" PRIu64
,
4817 ret
= bt_ctf_clock_class_set_frequency(clock
, freq
);
4819 _BT_LOGE_NODE(entry_node
,
4820 "Cannot set clock class's frequency.");
4824 _SET(set
, _CLOCK_FREQ_SET
);
4825 } else if (!strcmp(left
, "precision")) {
4828 if (_IS_SET(set
, _CLOCK_PRECISION_SET
)) {
4829 _BT_LOGE_DUP_ATTR(entry_node
, "precision",
4835 ret
= get_unary_unsigned(
4836 &entry_node
->u
.ctf_expression
.right
, &precision
);
4838 _BT_LOGE_NODE(entry_node
,
4839 "Unexpected unary expression for clock class's `precision` attribute.");
4844 ret
= bt_ctf_clock_class_set_precision(clock
, precision
);
4846 _BT_LOGE_NODE(entry_node
,
4847 "Cannot set clock class's precision.");
4851 _SET(set
, _CLOCK_PRECISION_SET
);
4852 } else if (!strcmp(left
, "offset_s")) {
4855 if (_IS_SET(set
, _CLOCK_OFFSET_S_SET
)) {
4856 _BT_LOGE_DUP_ATTR(entry_node
, "offset_s",
4862 ret
= get_unary_signed(
4863 &entry_node
->u
.ctf_expression
.right
, &offset_s
);
4865 _BT_LOGE_NODE(entry_node
,
4866 "Unexpected unary expression for clock class's `offset_s` attribute.");
4871 ret
= bt_ctf_clock_class_set_offset_s(clock
, offset_s
);
4873 _BT_LOGE_NODE(entry_node
,
4874 "Cannot set clock class's offset in seconds.");
4878 _SET(set
, _CLOCK_OFFSET_S_SET
);
4879 } else if (!strcmp(left
, "offset")) {
4882 if (_IS_SET(set
, _CLOCK_OFFSET_SET
)) {
4883 _BT_LOGE_DUP_ATTR(entry_node
, "offset", "clock class");
4888 ret
= get_unary_signed(
4889 &entry_node
->u
.ctf_expression
.right
, &offset
);
4891 _BT_LOGE_NODE(entry_node
,
4892 "Unexpected unary expression for clock class's `offset` attribute.");
4897 ret
= bt_ctf_clock_class_set_offset_cycles(clock
, offset
);
4899 _BT_LOGE_NODE(entry_node
,
4900 "Cannot set clock class's offset in cycles.");
4904 _SET(set
, _CLOCK_OFFSET_SET
);
4905 } else if (!strcmp(left
, "absolute")) {
4906 struct ctf_node
*right
;
4908 if (_IS_SET(set
, _CLOCK_ABSOLUTE_SET
)) {
4909 _BT_LOGE_DUP_ATTR(entry_node
, "absolute",
4915 right
= _BT_LIST_FIRST_ENTRY(
4916 &entry_node
->u
.ctf_expression
.right
,
4917 struct ctf_node
, siblings
);
4918 ret
= get_boolean(right
);
4920 _BT_LOGE_NODE(entry_node
,
4921 "Unexpected unary expression for clock class's `absolute` attribute.");
4926 ret
= bt_ctf_clock_class_set_is_absolute(clock
, ret
);
4928 _BT_LOGE_NODE(entry_node
,
4929 "Cannot set clock class's absolute flag.");
4933 _SET(set
, _CLOCK_ABSOLUTE_SET
);
4935 _BT_LOGW_NODE(entry_node
,
4936 "Unknown attribute in clock class: attr-name=\"%s\"",
4952 int64_t cycles_from_ns(uint64_t frequency
, int64_t ns
)
4957 if (frequency
== 1000000000ULL) {
4960 cycles
= (uint64_t) (((double) ns
* (double) frequency
) / 1e9
);
4967 int apply_clock_class_offset(struct ctx
*ctx
, struct bt_ctf_clock_class
*clock
)
4971 int64_t offset_cycles
;
4972 int64_t offset_to_apply
;
4974 freq
= bt_ctf_clock_class_get_frequency(clock
);
4975 if (freq
== -1ULL) {
4976 BT_LOGE_STR("Cannot get clock class's frequency.");
4981 ret
= bt_ctf_clock_class_get_offset_cycles(clock
, &offset_cycles
);
4983 BT_LOGE_STR("Cannot get clock class's offset in cycles.");
4989 ctx
->decoder_config
.clock_class_offset_s
* 1000000000LL +
4990 ctx
->decoder_config
.clock_class_offset_ns
;
4991 offset_cycles
+= cycles_from_ns(freq
, offset_to_apply
);
4992 ret
= bt_ctf_clock_class_set_offset_cycles(clock
, offset_cycles
);
4999 int visit_clock_decl(struct ctx
*ctx
, struct ctf_node
*clock_node
)
5003 struct bt_ctf_clock_class
*clock
;
5004 struct ctf_node
*entry_node
;
5005 struct bt_list_head
*decl_list
= &clock_node
->u
.clock
.declaration_list
;
5006 const char *clock_class_name
;
5008 if (clock_node
->visited
) {
5012 clock_node
->visited
= TRUE
;
5014 /* CTF 1.8's default frequency for a clock class is 1 GHz */
5015 clock
= bt_ctf_clock_class_create(NULL
, 1000000000);
5017 _BT_LOGE_NODE(clock_node
,
5018 "Cannot create default clock class.");
5023 bt_list_for_each_entry(entry_node
, decl_list
, siblings
) {
5024 ret
= visit_clock_decl_entry(ctx
, entry_node
, clock
, &set
);
5026 _BT_LOGE_NODE(entry_node
,
5027 "Cannot visit clock class's entry: ret=%d",
5033 if (!_IS_SET(&set
, _CLOCK_NAME_SET
)) {
5034 _BT_LOGE_NODE(clock_node
,
5035 "Missing `name` attribute in clock class.");
5040 clock_class_name
= bt_ctf_clock_class_get_name(clock
);
5041 assert(clock_class_name
);
5042 if (ctx
->is_lttng
&& strcmp(clock_class_name
, "monotonic") == 0) {
5044 * Old versions of LTTng forgot to set its clock class
5045 * as absolute, even if it is. This is important because
5046 * it's a condition to be able to sort notifications
5047 * from different sources.
5049 ret
= bt_ctf_clock_class_set_is_absolute(clock
, 1);
5051 _BT_LOGE_NODE(clock_node
,
5052 "Cannot set clock class's absolute flag.");
5057 ret
= apply_clock_class_offset(ctx
, clock
);
5059 _BT_LOGE_NODE(clock_node
,
5060 "Cannot apply clock class's custom offset.");
5064 ret
= bt_ctf_trace_add_clock_class(ctx
->trace
, clock
);
5066 _BT_LOGE_NODE(clock_node
,
5067 "Cannot add clock class to trace.");
5078 int visit_root_decl(struct ctx
*ctx
, struct ctf_node
*root_decl_node
)
5082 if (root_decl_node
->visited
) {
5086 root_decl_node
->visited
= TRUE
;
5088 switch (root_decl_node
->type
) {
5090 ret
= visit_typedef(ctx
,
5091 root_decl_node
->u
._typedef
.type_specifier_list
,
5092 &root_decl_node
->u
._typedef
.type_declarators
);
5094 _BT_LOGE_NODE(root_decl_node
,
5095 "Cannot add type definition found in root scope.");
5099 case NODE_TYPEALIAS
:
5100 ret
= visit_typealias(ctx
, root_decl_node
->u
.typealias
.target
,
5101 root_decl_node
->u
.typealias
.alias
);
5103 _BT_LOGE_NODE(root_decl_node
,
5104 "Cannot add type alias found in root scope.");
5108 case NODE_TYPE_SPECIFIER_LIST
:
5110 _BT_CTF_FIELD_TYPE_INIT(decl
);
5113 * Just add the type specifier to the root
5114 * declaration scope. Put local reference.
5116 ret
= visit_type_specifier_list(ctx
, root_decl_node
, &decl
);
5118 _BT_LOGE_NODE(root_decl_node
,
5119 "Cannot visit root scope's field type: "
5129 _BT_LOGE_NODE(root_decl_node
,
5130 "Unexpected node type: node-type=%d",
5131 root_decl_node
->type
);
5141 int set_trace_name(struct ctx
*ctx
)
5145 struct bt_value
*value
= NULL
;
5147 assert(bt_ctf_trace_get_stream_class_count(ctx
->trace
) == 0);
5148 name
= g_string_new(NULL
);
5150 BT_LOGE_STR("Failed to allocate a GString.");
5156 * Check if we have a trace environment string value named `hostname`.
5157 * If so, use it as the trace name's prefix.
5159 value
= bt_ctf_trace_get_environment_field_value_by_name(ctx
->trace
,
5161 if (bt_value_is_string(value
)) {
5162 const char *hostname
;
5164 ret
= bt_value_string_get(value
, &hostname
);
5166 g_string_append(name
, hostname
);
5168 if (ctx
->trace_name_suffix
) {
5169 g_string_append_c(name
, G_DIR_SEPARATOR
);
5173 if (ctx
->trace_name_suffix
) {
5174 g_string_append(name
, ctx
->trace_name_suffix
);
5177 ret
= bt_ctf_trace_set_name(ctx
->trace
, name
->str
);
5179 BT_LOGE("Cannot set trace's name: name=\"%s\"", name
->str
);
5192 g_string_free(name
, TRUE
);
5199 int move_ctx_stream_classes_to_trace(struct ctx
*ctx
)
5202 GHashTableIter iter
;
5203 gpointer key
, stream_class
;
5205 if (g_hash_table_size(ctx
->stream_classes
) > 0 &&
5206 bt_ctf_trace_get_stream_class_count(ctx
->trace
) == 0) {
5208 * We're about to add the first stream class to the
5209 * trace. This will freeze the trace, and after this
5210 * we cannot set the name anymore. At this point,
5211 * set the trace name.
5213 ret
= set_trace_name(ctx
);
5215 BT_LOGE_STR("Cannot set trace's name.");
5220 g_hash_table_iter_init(&iter
, ctx
->stream_classes
);
5222 while (g_hash_table_iter_next(&iter
, &key
, &stream_class
)) {
5223 ret
= bt_ctf_trace_add_stream_class(ctx
->trace
,
5226 int64_t id
= bt_ctf_stream_class_get_id(stream_class
);
5227 BT_LOGE("Cannot add stream class to trace: id=%" PRId64
,
5233 g_hash_table_remove_all(ctx
->stream_classes
);
5240 struct ctf_visitor_generate_ir
*ctf_visitor_generate_ir_create(
5241 const struct ctf_metadata_decoder_config
*decoder_config
,
5245 struct ctx
*ctx
= NULL
;
5246 struct bt_ctf_trace
*trace
;
5248 trace
= bt_ctf_trace_create();
5250 BT_LOGE_STR("Cannot create empty trace.");
5254 /* Set packet header to NULL to override the default one */
5255 ret
= bt_ctf_trace_set_packet_header_type(trace
, NULL
);
5257 BT_LOGE_STR("Cannot reset initial trace's packet header field type.");
5261 /* Create visitor's context */
5262 ctx
= ctx_create(trace
, decoder_config
, name
);
5264 BT_LOGE_STR("Cannot create visitor's context.");
5277 return (void *) ctx
;
5281 void ctf_visitor_generate_ir_destroy(struct ctf_visitor_generate_ir
*visitor
)
5283 ctx_destroy((void *) visitor
);
5287 struct bt_ctf_trace
*ctf_visitor_generate_ir_get_trace(
5288 struct ctf_visitor_generate_ir
*visitor
)
5290 struct ctx
*ctx
= (void *) visitor
;
5294 return bt_get(ctx
->trace
);
5298 int ctf_visitor_generate_ir_visit_node(struct ctf_visitor_generate_ir
*visitor
,
5299 struct ctf_node
*node
)
5302 struct ctx
*ctx
= (void *) visitor
;
5304 BT_LOGI_STR("Visiting metadata's AST to generate CTF IR objects.");
5306 switch (node
->type
) {
5309 struct ctf_node
*iter
;
5310 int got_trace_decl
= FALSE
;
5313 * The first thing we need is the native byte order of
5314 * the trace block, because early type aliases can have
5315 * a `byte_order` attribute set to `native`. If we don't
5316 * have the native byte order yet, and we don't have any
5317 * trace block yet, then fail with EINCOMPLETE.
5319 if (ctx
->trace_bo
== BT_CTF_BYTE_ORDER_NATIVE
) {
5320 bt_list_for_each_entry(iter
, &node
->u
.root
.trace
, siblings
) {
5321 if (got_trace_decl
) {
5323 "Duplicate trace (`trace` block).");
5328 ret
= set_trace_byte_order(ctx
, iter
);
5331 "Cannot set trace's native byte order: "
5336 got_trace_decl
= TRUE
;
5339 if (!got_trace_decl
) {
5340 BT_LOGD_STR("Incomplete AST: need trace (`trace` block).");
5346 assert(ctx
->trace_bo
== BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
||
5347 ctx
->trace_bo
== BT_CTF_BYTE_ORDER_BIG_ENDIAN
);
5348 assert(ctx
->current_scope
&&
5349 ctx
->current_scope
->parent_scope
== NULL
);
5352 bt_list_for_each_entry(iter
, &node
->u
.root
.env
, siblings
) {
5353 ret
= visit_env(ctx
, iter
);
5356 "Cannot visit trace's environment (`env` block) entry: "
5362 assert(ctx
->current_scope
&&
5363 ctx
->current_scope
->parent_scope
== NULL
);
5366 * Visit clock blocks.
5368 bt_list_for_each_entry(iter
, &node
->u
.root
.clock
, siblings
) {
5369 ret
= visit_clock_decl(ctx
, iter
);
5372 "Cannot visit clock class: ret=%d",
5378 assert(ctx
->current_scope
&&
5379 ctx
->current_scope
->parent_scope
== NULL
);
5382 * Visit root declarations next, as they can be used by any
5385 bt_list_for_each_entry(iter
, &node
->u
.root
.declaration_list
,
5387 ret
= visit_root_decl(ctx
, iter
);
5390 "Cannot visit root entry: ret=%d",
5396 assert(ctx
->current_scope
&&
5397 ctx
->current_scope
->parent_scope
== NULL
);
5399 /* Callsite blocks are not supported */
5400 bt_list_for_each_entry(iter
, &node
->u
.root
.callsite
, siblings
) {
5402 "\"callsite\" blocks are not supported as of this version.");
5405 assert(ctx
->current_scope
&&
5406 ctx
->current_scope
->parent_scope
== NULL
);
5409 bt_list_for_each_entry(iter
, &node
->u
.root
.trace
, siblings
) {
5410 ret
= visit_trace_decl(ctx
, iter
);
5413 "Cannot visit trace (`trace` block): "
5419 assert(ctx
->current_scope
&&
5420 ctx
->current_scope
->parent_scope
== NULL
);
5423 bt_list_for_each_entry(iter
, &node
->u
.root
.stream
, siblings
) {
5424 ret
= visit_stream_decl(ctx
, iter
);
5427 "Cannot visit stream class: ret=%d",
5433 assert(ctx
->current_scope
&&
5434 ctx
->current_scope
->parent_scope
== NULL
);
5437 bt_list_for_each_entry(iter
, &node
->u
.root
.event
, siblings
) {
5438 ret
= visit_event_decl(ctx
, iter
);
5441 "Cannot visit event class: ret=%d",
5447 assert(ctx
->current_scope
&&
5448 ctx
->current_scope
->parent_scope
== NULL
);
5453 "Unexpected node type: node-type=%d",
5459 /* Move decoded stream classes to trace, if any */
5460 ret
= move_ctx_stream_classes_to_trace(ctx
);
5462 BT_LOGE("Cannot move stream classes to trace: ret=%d", ret
);