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>
58 /* Bit value (left shift) */
59 #define _BV(_val) (1 << (_val))
61 /* Bit is set in a set of bits */
62 #define _IS_SET(_set, _mask) (*(_set) & (_mask))
64 /* Set bit in a set of bits */
65 #define _SET(_set, _mask) (*(_set) |= (_mask))
67 /* Bits for verifying existing attributes in various declarations */
69 _CLOCK_NAME_SET
= _BV(0),
70 _CLOCK_UUID_SET
= _BV(1),
71 _CLOCK_FREQ_SET
= _BV(2),
72 _CLOCK_PRECISION_SET
= _BV(3),
73 _CLOCK_OFFSET_S_SET
= _BV(4),
74 _CLOCK_OFFSET_SET
= _BV(5),
75 _CLOCK_ABSOLUTE_SET
= _BV(6),
76 _CLOCK_DESCRIPTION_SET
= _BV(7),
80 _INTEGER_ALIGN_SET
= _BV(0),
81 _INTEGER_SIZE_SET
= _BV(1),
82 _INTEGER_BASE_SET
= _BV(2),
83 _INTEGER_ENCODING_SET
= _BV(3),
84 _INTEGER_BYTE_ORDER_SET
= _BV(4),
85 _INTEGER_SIGNED_SET
= _BV(5),
86 _INTEGER_MAP_SET
= _BV(6),
90 _FLOAT_ALIGN_SET
= _BV(0),
91 _FLOAT_MANT_DIG_SET
= _BV(1),
92 _FLOAT_EXP_DIG_SET
= _BV(2),
93 _FLOAT_BYTE_ORDER_SET
= _BV(3),
97 _STRING_ENCODING_SET
= _BV(0),
101 _TRACE_MINOR_SET
= _BV(0),
102 _TRACE_MAJOR_SET
= _BV(1),
103 _TRACE_BYTE_ORDER_SET
= _BV(2),
104 _TRACE_UUID_SET
= _BV(3),
105 _TRACE_PACKET_HEADER_SET
= _BV(4),
109 _STREAM_ID_SET
= _BV(0),
110 _STREAM_PACKET_CONTEXT_SET
= _BV(1),
111 _STREAM_EVENT_HEADER_SET
= _BV(2),
112 _STREAM_EVENT_CONTEXT_SET
= _BV(3),
116 _EVENT_NAME_SET
= _BV(0),
117 _EVENT_ID_SET
= _BV(1),
118 _EVENT_MODEL_EMF_URI_SET
= _BV(2),
119 _EVENT_STREAM_ID_SET
= _BV(3),
120 _EVENT_LOGLEVEL_SET
= _BV(4),
121 _EVENT_CONTEXT_SET
= _BV(5),
122 _EVENT_FIELDS_SET
= _BV(6),
130 LOGLEVEL_WARNING
= 4,
133 LOGLEVEL_DEBUG_SYSTEM
= 7,
134 LOGLEVEL_DEBUG_PROGRAM
= 8,
135 LOGLEVEL_DEBUG_PROCESS
= 9,
136 LOGLEVEL_DEBUG_MODULE
= 10,
137 LOGLEVEL_DEBUG_UNIT
= 11,
138 LOGLEVEL_DEBUG_FUNCTION
= 12,
139 LOGLEVEL_DEBUG_LINE
= 13,
144 /* Prefixes of type aliases */
145 #define _PREFIX_ALIAS 'a'
146 #define _PREFIX_ENUM 'e'
147 #define _PREFIX_STRUCT 's'
148 #define _PREFIX_VARIANT 'v'
150 /* First entry in a BT list */
151 #define _BT_LIST_FIRST_ENTRY(_ptr, _type, _member) \
152 bt_list_entry((_ptr)->next, _type, _member)
154 #define _BT_CTF_FIELD_TYPE_INIT(_name) struct bt_ctf_field_type *_name = NULL;
156 #define _BT_LOGE_DUP_ATTR(_node, _attr, _entity) \
157 _BT_LOGE_LINENO((_node)->lineno, \
158 "Duplicate attribute in %s: attr-name=\"%s\"", \
161 #define _BT_LOGE_NODE(_node, _msg, args...) \
162 _BT_LOGE_LINENO((_node)->lineno, _msg, ## args)
164 #define _BT_LOGW_NODE(_node, _msg, args...) \
165 _BT_LOGW_LINENO((_node)->lineno, _msg, ## args)
167 #define _BT_LOGV_NODE(_node, _msg, args...) \
168 _BT_LOGV_LINENO((_node)->lineno, _msg, ## args)
171 * Declaration scope of a visitor context. This represents a TSDL
172 * lexical scope, so that aliases and named structures, variants,
173 * and enumerations may be registered and looked up hierarchically.
175 struct ctx_decl_scope
{
177 * Alias name to field type.
179 * GQuark -> struct bt_ctf_field_type *
181 GHashTable
*decl_map
;
183 /* Parent scope; NULL if this is the root declaration scope */
184 struct ctx_decl_scope
*parent_scope
;
188 * Visitor context (private).
191 /* Trace being filled (owned by this) */
192 struct bt_ctf_trace
*trace
;
194 /* Current declaration scope (top of the stack) */
195 struct ctx_decl_scope
*current_scope
;
197 /* 1 if trace declaration is visited */
198 int is_trace_visited
;
200 /* 1 if this is an LTTng trace */
203 /* Offset (ns) to apply to clock classes on creation */
204 int64_t clock_class_offset_ns
;
206 /* Eventual name suffix of the trace to set */
207 char *trace_name_suffix
;
209 /* Trace attributes */
210 enum bt_ctf_byte_order trace_bo
;
211 uint64_t trace_major
;
212 uint64_t trace_minor
;
213 unsigned char trace_uuid
[BABELTRACE_UUID_LEN
];
216 * Stream IDs to stream classes.
218 * int64_t -> struct bt_ctf_stream_class *
220 GHashTable
*stream_classes
;
226 struct ctf_visitor_generate_ir
{ };
229 const char *loglevel_str
[] = {
230 [ LOGLEVEL_EMERG
] = "TRACE_EMERG",
231 [ LOGLEVEL_ALERT
] = "TRACE_ALERT",
232 [ LOGLEVEL_CRIT
] = "TRACE_CRIT",
233 [ LOGLEVEL_ERR
] = "TRACE_ERR",
234 [ LOGLEVEL_WARNING
] = "TRACE_WARNING",
235 [ LOGLEVEL_NOTICE
] = "TRACE_NOTICE",
236 [ LOGLEVEL_INFO
] = "TRACE_INFO",
237 [ LOGLEVEL_DEBUG_SYSTEM
] = "TRACE_DEBUG_SYSTEM",
238 [ LOGLEVEL_DEBUG_PROGRAM
] = "TRACE_DEBUG_PROGRAM",
239 [ LOGLEVEL_DEBUG_PROCESS
] = "TRACE_DEBUG_PROCESS",
240 [ LOGLEVEL_DEBUG_MODULE
] = "TRACE_DEBUG_MODULE",
241 [ LOGLEVEL_DEBUG_UNIT
] = "TRACE_DEBUG_UNIT",
242 [ LOGLEVEL_DEBUG_FUNCTION
] = "TRACE_DEBUG_FUNCTION",
243 [ LOGLEVEL_DEBUG_LINE
] = "TRACE_DEBUG_LINE",
244 [ LOGLEVEL_DEBUG
] = "TRACE_DEBUG",
248 const char *print_loglevel(int64_t value
)
253 if (value
>= _NR_LOGLEVELS
) {
254 return "<<UNKNOWN>>";
256 return loglevel_str
[value
];
260 * Creates a new declaration scope.
262 * @param par_scope Parent scope (NULL if creating a root scope)
263 * @returns New declaration scope, or NULL on error
266 struct ctx_decl_scope
*ctx_decl_scope_create(struct ctx_decl_scope
*par_scope
)
268 struct ctx_decl_scope
*scope
;
270 scope
= g_new(struct ctx_decl_scope
, 1);
272 BT_LOGE_STR("Failed to allocate one declaration scope.");
276 scope
->decl_map
= g_hash_table_new_full(g_direct_hash
, g_direct_equal
,
277 NULL
, (GDestroyNotify
) bt_ctf_field_type_put
);
278 scope
->parent_scope
= par_scope
;
285 * Destroys a declaration scope.
287 * This function does not destroy the parent scope.
289 * @param scope Scope to destroy
292 void ctx_decl_scope_destroy(struct ctx_decl_scope
*scope
)
298 g_hash_table_destroy(scope
->decl_map
);
306 * Returns the GQuark of a prefixed alias.
308 * @param prefix Prefix character
310 * @returns Associated GQuark, or 0 on error
313 GQuark
get_prefixed_named_quark(char prefix
, const char *name
)
319 /* Prefix character + original string + '\0' */
320 char *prname
= g_new(char, strlen(name
) + 2);
322 BT_LOGE_STR("Failed to allocate a string.");
326 sprintf(prname
, "%c%s", prefix
, name
);
327 qname
= g_quark_from_string(prname
);
335 * Looks up a prefixed type alias within a declaration scope.
337 * @param scope Declaration scope
338 * @param prefix Prefix character
339 * @param name Alias name
340 * @param level Number of levels to dig (-1 means infinite)
341 * @returns Declaration, or NULL if not found
344 struct bt_ctf_field_type
*ctx_decl_scope_lookup_prefix_alias(
345 struct ctx_decl_scope
*scope
, char prefix
,
346 const char *name
, int levels
)
350 _BT_CTF_FIELD_TYPE_INIT(decl
);
351 struct ctx_decl_scope
*cur_scope
= scope
;
355 qname
= get_prefixed_named_quark(prefix
, name
);
364 while (cur_scope
&& cur_levels
< levels
) {
365 decl
= g_hash_table_lookup(cur_scope
->decl_map
,
366 (gconstpointer
) GUINT_TO_POINTER(qname
));
368 /* Caller's reference */
373 cur_scope
= cur_scope
->parent_scope
;
384 * Looks up a type alias within a declaration scope.
386 * @param scope Declaration scope
387 * @param name Alias name
388 * @param level Number of levels to dig (-1 means infinite)
389 * @returns Declaration, or NULL if not found
392 struct bt_ctf_field_type
*ctx_decl_scope_lookup_alias(
393 struct ctx_decl_scope
*scope
, const char *name
, int levels
)
395 return ctx_decl_scope_lookup_prefix_alias(scope
, _PREFIX_ALIAS
,
400 * Looks up an enumeration within a declaration scope.
402 * @param scope Declaration scope
403 * @param name Enumeration name
404 * @param level Number of levels to dig (-1 means infinite)
405 * @returns Declaration, or NULL if not found
408 struct bt_ctf_field_type
*ctx_decl_scope_lookup_enum(
409 struct ctx_decl_scope
*scope
, const char *name
, int levels
)
411 return ctx_decl_scope_lookup_prefix_alias(scope
, _PREFIX_ENUM
,
416 * Looks up a structure within a declaration scope.
418 * @param scope Declaration scope
419 * @param name Structure name
420 * @param level Number of levels to dig (-1 means infinite)
421 * @returns Declaration, or NULL if not found
424 struct bt_ctf_field_type
*ctx_decl_scope_lookup_struct(
425 struct ctx_decl_scope
*scope
, const char *name
, int levels
)
427 return ctx_decl_scope_lookup_prefix_alias(scope
, _PREFIX_STRUCT
,
432 * Looks up a variant within a declaration scope.
434 * @param scope Declaration scope
435 * @param name Variant name
436 * @param level Number of levels to dig (-1 means infinite)
437 * @returns Declaration, or NULL if not found
440 struct bt_ctf_field_type
*ctx_decl_scope_lookup_variant(
441 struct ctx_decl_scope
*scope
, const char *name
, int levels
)
443 return ctx_decl_scope_lookup_prefix_alias(scope
, _PREFIX_VARIANT
,
448 * Registers a prefixed type alias within a declaration scope.
450 * @param scope Declaration scope
451 * @param prefix Prefix character
452 * @param name Alias name (non-NULL)
453 * @param decl Declaration to register
454 * @returns 0 if registration went okay, negative value otherwise
457 int ctx_decl_scope_register_prefix_alias(struct ctx_decl_scope
*scope
,
458 char prefix
, const char *name
, struct bt_ctf_field_type
*decl
)
462 _BT_CTF_FIELD_TYPE_INIT(edecl
);
467 qname
= get_prefixed_named_quark(prefix
, name
);
473 /* Make sure alias does not exist in local scope */
474 edecl
= ctx_decl_scope_lookup_prefix_alias(scope
, prefix
, name
, 1);
481 g_hash_table_insert(scope
->decl_map
,
482 GUINT_TO_POINTER(qname
), decl
);
484 /* Hash table's reference */
494 * Registers a type alias within a declaration scope.
496 * @param scope Declaration scope
497 * @param name Alias name (non-NULL)
498 * @param decl Declaration to register
499 * @returns 0 if registration went okay, negative value otherwise
502 int ctx_decl_scope_register_alias(struct ctx_decl_scope
*scope
,
503 const char *name
, struct bt_ctf_field_type
*decl
)
505 return ctx_decl_scope_register_prefix_alias(scope
, _PREFIX_ALIAS
,
510 * Registers an enumeration declaration within a declaration scope.
512 * @param scope Declaration scope
513 * @param name Enumeration name (non-NULL)
514 * @param decl Enumeration declaration to register
515 * @returns 0 if registration went okay, negative value otherwise
518 int ctx_decl_scope_register_enum(struct ctx_decl_scope
*scope
,
519 const char *name
, struct bt_ctf_field_type
*decl
)
521 return ctx_decl_scope_register_prefix_alias(scope
, _PREFIX_ENUM
,
526 * Registers a structure declaration within a declaration scope.
528 * @param scope Declaration scope
529 * @param name Structure name (non-NULL)
530 * @param decl Structure declaration to register
531 * @returns 0 if registration went okay, negative value otherwise
534 int ctx_decl_scope_register_struct(struct ctx_decl_scope
*scope
,
535 const char *name
, struct bt_ctf_field_type
*decl
)
537 return ctx_decl_scope_register_prefix_alias(scope
, _PREFIX_STRUCT
,
542 * Registers a variant declaration within a declaration scope.
544 * @param scope Declaration scope
545 * @param name Variant name (non-NULL)
546 * @param decl Variant declaration to register
547 * @returns 0 if registration went okay, negative value otherwise
550 int ctx_decl_scope_register_variant(struct ctx_decl_scope
*scope
,
551 const char *name
, struct bt_ctf_field_type
*decl
)
553 return ctx_decl_scope_register_prefix_alias(scope
, _PREFIX_VARIANT
,
558 * Destroys a visitor context.
560 * @param ctx Visitor context to destroy
563 void ctx_destroy(struct ctx
*ctx
)
565 struct ctx_decl_scope
*scope
;
567 * Destroy all scopes, from current one to the root scope.
574 scope
= ctx
->current_scope
;
577 struct ctx_decl_scope
*parent_scope
= scope
->parent_scope
;
579 ctx_decl_scope_destroy(scope
);
580 scope
= parent_scope
;
585 if (ctx
->stream_classes
) {
586 g_hash_table_destroy(ctx
->stream_classes
);
589 free(ctx
->trace_name_suffix
);
597 * Creates a new visitor context.
599 * @param trace Associated trace
600 * @returns New visitor context, or NULL on error
603 struct ctx
*ctx_create(struct bt_ctf_trace
*trace
,
604 int64_t clock_class_offset_ns
, const char *trace_name_suffix
)
606 struct ctx
*ctx
= NULL
;
607 struct ctx_decl_scope
*scope
= NULL
;
609 ctx
= g_new0(struct ctx
, 1);
611 BT_LOGE_STR("Failed to allocate one visitor context.");
615 /* Root declaration scope */
616 scope
= ctx_decl_scope_create(NULL
);
618 BT_LOGE_STR("Cannot create declaration scope.");
622 ctx
->stream_classes
= g_hash_table_new_full(g_direct_hash
,
623 g_direct_equal
, NULL
, (GDestroyNotify
) bt_put
);
624 if (!ctx
->stream_classes
) {
625 BT_LOGE_STR("Failed to allocate a GHashTable.");
629 if (trace_name_suffix
) {
630 ctx
->trace_name_suffix
= strdup(trace_name_suffix
);
631 if (!ctx
->trace_name_suffix
) {
632 BT_LOGE_STR("Failed to copy string.");
638 ctx
->current_scope
= scope
;
640 ctx
->trace_bo
= BT_CTF_BYTE_ORDER_NATIVE
;
641 ctx
->clock_class_offset_ns
= clock_class_offset_ns
;
646 ctx_decl_scope_destroy(scope
);
651 * Pushes a new declaration scope on top of a visitor context's
652 * declaration scope stack.
654 * @param ctx Visitor context
655 * @returns 0 on success, or a negative value on error
658 int ctx_push_scope(struct ctx
*ctx
)
661 struct ctx_decl_scope
*new_scope
;
664 new_scope
= ctx_decl_scope_create(ctx
->current_scope
);
666 BT_LOGE_STR("Cannot create declaration scope.");
671 ctx
->current_scope
= new_scope
;
678 void ctx_pop_scope(struct ctx
*ctx
)
680 struct ctx_decl_scope
*parent_scope
= NULL
;
684 if (!ctx
->current_scope
) {
688 parent_scope
= ctx
->current_scope
->parent_scope
;
689 ctx_decl_scope_destroy(ctx
->current_scope
);
690 ctx
->current_scope
= parent_scope
;
697 int visit_type_specifier_list(struct ctx
*ctx
, struct ctf_node
*ts_list
,
698 struct bt_ctf_field_type
**decl
);
701 int is_unary_string(struct bt_list_head
*head
)
704 struct ctf_node
*node
;
706 bt_list_for_each_entry(node
, head
, siblings
) {
707 if (node
->type
!= NODE_UNARY_EXPRESSION
) {
711 if (node
->u
.unary_expression
.type
!= UNARY_STRING
) {
720 char *concatenate_unary_strings(struct bt_list_head
*head
)
724 struct ctf_node
*node
;
726 str
= g_string_new(NULL
);
729 bt_list_for_each_entry(node
, head
, siblings
) {
733 node
->type
!= NODE_UNARY_EXPRESSION
||
734 node
->u
.unary_expression
.type
!= UNARY_STRING
||
737 node
->u
.unary_expression
.link
!=
745 switch (node
->u
.unary_expression
.link
) {
747 g_string_append(str
, ".");
749 case UNARY_ARROWLINK
:
750 g_string_append(str
, "->");
752 case UNARY_DOTDOTDOT
:
753 g_string_append(str
, "...");
759 src_string
= node
->u
.unary_expression
.u
.string
;
760 g_string_append(str
, src_string
);
764 /* Destroys the container, returns the underlying string */
765 return g_string_free(str
, FALSE
);
768 /* This always returns NULL */
769 return g_string_free(str
, TRUE
);
773 const char *get_map_clock_name_value(struct bt_list_head
*head
)
776 struct ctf_node
*node
;
777 const char *name
= NULL
;
779 bt_list_for_each_entry(node
, head
, siblings
) {
781 int uexpr_type
= node
->u
.unary_expression
.type
;
782 int uexpr_link
= node
->u
.unary_expression
.link
;
783 int cond
= node
->type
!= NODE_UNARY_EXPRESSION
||
784 uexpr_type
!= UNARY_STRING
||
785 !((uexpr_link
!= UNARY_LINK_UNKNOWN
) ^ (i
== 0));
790 /* Needs to be chained with . */
791 switch (node
->u
.unary_expression
.link
) {
794 case UNARY_ARROWLINK
:
795 case UNARY_DOTDOTDOT
:
801 src_string
= node
->u
.unary_expression
.u
.string
;
805 if (strcmp("clock", src_string
)) {
813 if (strcmp("value", src_string
)) {
818 /* Extra identifier, unknown */
832 int is_unary_unsigned(struct bt_list_head
*head
)
835 struct ctf_node
*node
;
837 bt_list_for_each_entry(node
, head
, siblings
) {
838 if (node
->type
!= NODE_UNARY_EXPRESSION
) {
842 if (node
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
851 int get_unary_unsigned(struct bt_list_head
*head
, uint64_t *value
)
855 struct ctf_node
*node
;
857 if (bt_list_empty(head
)) {
862 bt_list_for_each_entry(node
, head
, siblings
) {
863 int uexpr_type
= node
->u
.unary_expression
.type
;
864 int uexpr_link
= node
->u
.unary_expression
.link
;
865 int cond
= node
->type
!= NODE_UNARY_EXPRESSION
||
866 uexpr_type
!= UNARY_UNSIGNED_CONSTANT
||
867 uexpr_link
!= UNARY_LINK_UNKNOWN
|| i
!= 0;
869 _BT_LOGE_NODE(node
, "Invalid constant unsigned integer.");
874 *value
= node
->u
.unary_expression
.u
.unsigned_constant
;
883 int is_unary_signed(struct bt_list_head
*head
)
886 struct ctf_node
*node
;
888 bt_list_for_each_entry(node
, head
, siblings
) {
889 if (node
->type
!= NODE_UNARY_EXPRESSION
) {
893 if (node
->u
.unary_expression
.type
!= UNARY_SIGNED_CONSTANT
) {
902 int get_unary_signed(struct bt_list_head
*head
, int64_t *value
)
906 struct ctf_node
*node
;
908 bt_list_for_each_entry(node
, head
, siblings
) {
909 int uexpr_type
= node
->u
.unary_expression
.type
;
910 int uexpr_link
= node
->u
.unary_expression
.link
;
911 int cond
= node
->type
!= NODE_UNARY_EXPRESSION
||
912 (uexpr_type
!= UNARY_UNSIGNED_CONSTANT
) ||
913 (uexpr_type
!= UNARY_UNSIGNED_CONSTANT
&&
914 uexpr_type
!= UNARY_SIGNED_CONSTANT
) ||
915 uexpr_link
!= UNARY_LINK_UNKNOWN
|| i
!= 0;
921 switch (node
->u
.unary_expression
.type
) {
922 case UNARY_UNSIGNED_CONSTANT
:
924 node
->u
.unary_expression
.u
.unsigned_constant
;
926 case UNARY_SIGNED_CONSTANT
:
927 *value
= node
->u
.unary_expression
.u
.signed_constant
;
942 int get_unary_uuid(struct bt_list_head
*head
, unsigned char *uuid
)
946 struct ctf_node
*node
;
948 bt_list_for_each_entry(node
, head
, siblings
) {
949 int uexpr_type
= node
->u
.unary_expression
.type
;
950 int uexpr_link
= node
->u
.unary_expression
.link
;
951 const char *src_string
;
953 if (node
->type
!= NODE_UNARY_EXPRESSION
||
954 uexpr_type
!= UNARY_STRING
||
955 uexpr_link
!= UNARY_LINK_UNKNOWN
||
961 src_string
= node
->u
.unary_expression
.u
.string
;
962 ret
= bt_uuid_parse(src_string
, uuid
);
965 "Cannot parse UUID: uuid=\"%s\"", src_string
);
975 int get_boolean(struct ctf_node
*unary_expr
)
979 if (unary_expr
->type
!= NODE_UNARY_EXPRESSION
) {
980 _BT_LOGE_NODE(unary_expr
,
981 "Expecting unary expression: node-type=%d",
987 switch (unary_expr
->u
.unary_expression
.type
) {
988 case UNARY_UNSIGNED_CONSTANT
:
989 ret
= (unary_expr
->u
.unary_expression
.u
.unsigned_constant
!= 0);
991 case UNARY_SIGNED_CONSTANT
:
992 ret
= (unary_expr
->u
.unary_expression
.u
.signed_constant
!= 0);
996 const char *str
= unary_expr
->u
.unary_expression
.u
.string
;
998 if (!strcmp(str
, "true") || !strcmp(str
, "TRUE")) {
1000 } else if (!strcmp(str
, "false") || !strcmp(str
, "FALSE")) {
1003 _BT_LOGE_NODE(unary_expr
,
1004 "Unexpected boolean value: value=\"%s\"", str
);
1011 _BT_LOGE_NODE(unary_expr
,
1012 "Unexpected unary expression type: node-type=%d",
1013 unary_expr
->u
.unary_expression
.type
);
1023 enum bt_ctf_byte_order
byte_order_from_unary_expr(struct ctf_node
*unary_expr
)
1026 enum bt_ctf_byte_order bo
= BT_CTF_BYTE_ORDER_UNKNOWN
;
1028 if (unary_expr
->u
.unary_expression
.type
!= UNARY_STRING
) {
1029 _BT_LOGE_NODE(unary_expr
,
1030 "\"byte_order\" attribute: expecting `be`, `le`, `network`, or `native`.");
1034 str
= unary_expr
->u
.unary_expression
.u
.string
;
1036 if (!strcmp(str
, "be") || !strcmp(str
, "network")) {
1037 bo
= BT_CTF_BYTE_ORDER_BIG_ENDIAN
;
1038 } else if (!strcmp(str
, "le")) {
1039 bo
= BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
;
1040 } else if (!strcmp(str
, "native")) {
1041 bo
= BT_CTF_BYTE_ORDER_NATIVE
;
1043 _BT_LOGE_NODE(unary_expr
,
1044 "Unexpected \"byte_order\" attribute value: "
1045 "expecting `be`, `le`, `network`, or `native`: value=\"%s\"",
1055 enum bt_ctf_byte_order
get_real_byte_order(struct ctx
*ctx
,
1056 struct ctf_node
*uexpr
)
1058 enum bt_ctf_byte_order bo
= byte_order_from_unary_expr(uexpr
);
1060 if (bo
== BT_CTF_BYTE_ORDER_NATIVE
) {
1061 bo
= bt_ctf_trace_get_native_byte_order(ctx
->trace
);
1068 int is_align_valid(uint64_t align
)
1070 return (align
!= 0) && !(align
& (align
- 1));
1074 int get_type_specifier_name(struct ctx
*ctx
, struct ctf_node
*type_specifier
,
1079 if (type_specifier
->type
!= NODE_TYPE_SPECIFIER
) {
1080 _BT_LOGE_NODE(type_specifier
,
1081 "Unexpected node type: node-type=%d",
1082 type_specifier
->type
);
1087 switch (type_specifier
->u
.type_specifier
.type
) {
1089 g_string_append(str
, "void");
1092 g_string_append(str
, "char");
1094 case TYPESPEC_SHORT
:
1095 g_string_append(str
, "short");
1098 g_string_append(str
, "int");
1101 g_string_append(str
, "long");
1103 case TYPESPEC_FLOAT
:
1104 g_string_append(str
, "float");
1106 case TYPESPEC_DOUBLE
:
1107 g_string_append(str
, "double");
1109 case TYPESPEC_SIGNED
:
1110 g_string_append(str
, "signed");
1112 case TYPESPEC_UNSIGNED
:
1113 g_string_append(str
, "unsigned");
1116 g_string_append(str
, "bool");
1118 case TYPESPEC_COMPLEX
:
1119 g_string_append(str
, "_Complex");
1121 case TYPESPEC_IMAGINARY
:
1122 g_string_append(str
, "_Imaginary");
1124 case TYPESPEC_CONST
:
1125 g_string_append(str
, "const");
1127 case TYPESPEC_ID_TYPE
:
1128 if (type_specifier
->u
.type_specifier
.id_type
) {
1129 g_string_append(str
,
1130 type_specifier
->u
.type_specifier
.id_type
);
1133 case TYPESPEC_STRUCT
:
1135 struct ctf_node
*node
= type_specifier
->u
.type_specifier
.node
;
1137 if (!node
->u
._struct
.name
) {
1138 _BT_LOGE_NODE(node
, "Unexpected empty structure field type name.");
1143 g_string_append(str
, "struct ");
1144 g_string_append(str
, node
->u
._struct
.name
);
1147 case TYPESPEC_VARIANT
:
1149 struct ctf_node
*node
= type_specifier
->u
.type_specifier
.node
;
1151 if (!node
->u
.variant
.name
) {
1152 _BT_LOGE_NODE(node
, "Unexpected empty variant field type name.");
1157 g_string_append(str
, "variant ");
1158 g_string_append(str
, node
->u
.variant
.name
);
1163 struct ctf_node
*node
= type_specifier
->u
.type_specifier
.node
;
1165 if (!node
->u
._enum
.enum_id
) {
1167 "Unexpected empty enumeration field type (`enum`) name.");
1172 g_string_append(str
, "enum ");
1173 g_string_append(str
, node
->u
._enum
.enum_id
);
1176 case TYPESPEC_FLOATING_POINT
:
1177 case TYPESPEC_INTEGER
:
1178 case TYPESPEC_STRING
:
1180 _BT_LOGE_NODE(type_specifier
->u
.type_specifier
.node
,
1181 "Unexpected type specifier type: %d",
1182 type_specifier
->u
.type_specifier
.type
);
1192 int get_type_specifier_list_name(struct ctx
*ctx
,
1193 struct ctf_node
*type_specifier_list
, GString
*str
)
1196 struct ctf_node
*iter
;
1197 int alias_item_nr
= 0;
1198 struct bt_list_head
*head
=
1199 &type_specifier_list
->u
.type_specifier_list
.head
;
1201 bt_list_for_each_entry(iter
, head
, siblings
) {
1202 if (alias_item_nr
!= 0) {
1203 g_string_append(str
, " ");
1207 ret
= get_type_specifier_name(ctx
, iter
, str
);
1218 GQuark
create_typealias_identifier(struct ctx
*ctx
,
1219 struct ctf_node
*type_specifier_list
,
1220 struct ctf_node
*node_type_declarator
)
1226 struct ctf_node
*iter
;
1227 struct bt_list_head
*pointers
=
1228 &node_type_declarator
->u
.type_declarator
.pointers
;
1230 str
= g_string_new("");
1231 ret
= get_type_specifier_list_name(ctx
, type_specifier_list
, str
);
1233 g_string_free(str
, TRUE
);
1237 bt_list_for_each_entry(iter
, pointers
, siblings
) {
1238 g_string_append(str
, " *");
1240 if (iter
->u
.pointer
.const_qualifier
) {
1241 g_string_append(str
, " const");
1245 str_c
= g_string_free(str
, FALSE
);
1246 qalias
= g_quark_from_string(str_c
);
1254 int visit_type_declarator(struct ctx
*ctx
, struct ctf_node
*type_specifier_list
,
1255 GQuark
*field_name
, struct ctf_node
*node_type_declarator
,
1256 struct bt_ctf_field_type
**field_decl
,
1257 struct bt_ctf_field_type
*nested_decl
)
1260 * During this whole function, nested_decl is always OURS,
1261 * whereas field_decl is an output which we create, but
1262 * belongs to the caller (it is moved).
1268 /* Validate type declarator node */
1269 if (node_type_declarator
) {
1270 if (node_type_declarator
->u
.type_declarator
.type
==
1272 _BT_LOGE_NODE(node_type_declarator
,
1273 "Unexpected type declarator type: type=%d",
1274 node_type_declarator
->u
.type_declarator
.type
);
1279 /* TODO: GCC bitfields not supported yet */
1280 if (node_type_declarator
->u
.type_declarator
.bitfield_len
!=
1282 _BT_LOGE_NODE(node_type_declarator
,
1283 "GCC bitfields are not supported as of this version.");
1289 /* Find the right nested declaration if not provided */
1291 struct bt_list_head
*pointers
=
1292 &node_type_declarator
->u
.type_declarator
.pointers
;
1294 if (node_type_declarator
&& !bt_list_empty(pointers
)) {
1296 _BT_CTF_FIELD_TYPE_INIT(nested_decl_copy
);
1299 * If we have a pointer declarator, it HAS to
1300 * be present in the typealiases (else fail).
1302 qalias
= create_typealias_identifier(ctx
,
1303 type_specifier_list
, node_type_declarator
);
1305 ctx_decl_scope_lookup_alias(ctx
->current_scope
,
1306 g_quark_to_string(qalias
), -1);
1308 _BT_LOGE_NODE(node_type_declarator
,
1309 "Cannot find type alias: name=\"%s\"",
1310 g_quark_to_string(qalias
));
1315 /* Make a copy of it */
1316 nested_decl_copy
= bt_ctf_field_type_copy(nested_decl
);
1317 BT_PUT(nested_decl
);
1318 if (!nested_decl_copy
) {
1319 _BT_LOGE_NODE(node_type_declarator
,
1320 "Cannot copy nested field type.");
1325 BT_MOVE(nested_decl
, nested_decl_copy
);
1327 /* Force integer's base to 16 since it's a pointer */
1328 if (bt_ctf_field_type_is_integer(nested_decl
)) {
1329 ret
= bt_ctf_field_type_integer_set_base(
1331 BT_CTF_INTEGER_BASE_HEXADECIMAL
);
1335 ret
= visit_type_specifier_list(ctx
,
1336 type_specifier_list
, &nested_decl
);
1338 assert(!nested_decl
);
1344 assert(nested_decl
);
1346 if (!node_type_declarator
) {
1347 BT_MOVE(*field_decl
, nested_decl
);
1351 if (node_type_declarator
->u
.type_declarator
.type
== TYPEDEC_ID
) {
1352 if (node_type_declarator
->u
.type_declarator
.u
.id
) {
1354 node_type_declarator
->u
.type_declarator
.u
.id
;
1356 *field_name
= g_quark_from_string(id
);
1361 BT_MOVE(*field_decl
, nested_decl
);
1364 struct ctf_node
*first
;
1365 _BT_CTF_FIELD_TYPE_INIT(decl
);
1366 _BT_CTF_FIELD_TYPE_INIT(outer_field_decl
);
1367 struct bt_list_head
*length
=
1368 &node_type_declarator
->
1369 u
.type_declarator
.u
.nested
.length
;
1371 /* Create array/sequence, pass nested_decl as child */
1372 if (bt_list_empty(length
)) {
1373 _BT_LOGE_NODE(node_type_declarator
,
1374 "Expecting length field reference or value.");
1379 first
= _BT_LIST_FIRST_ENTRY(length
, struct ctf_node
, siblings
);
1380 if (first
->type
!= NODE_UNARY_EXPRESSION
) {
1381 _BT_LOGE_NODE(first
,
1382 "Unexpected node type: node-type=%d",
1388 switch (first
->u
.unary_expression
.type
) {
1389 case UNARY_UNSIGNED_CONSTANT
:
1392 _BT_CTF_FIELD_TYPE_INIT(array_decl
);
1394 len
= first
->u
.unary_expression
.u
.unsigned_constant
;
1395 array_decl
= bt_ctf_field_type_array_create(nested_decl
,
1397 BT_PUT(nested_decl
);
1399 _BT_LOGE_NODE(first
,
1400 "Cannot create array field type.");
1405 BT_MOVE(decl
, array_decl
);
1410 /* Lookup unsigned integer definition, create seq. */
1411 _BT_CTF_FIELD_TYPE_INIT(seq_decl
);
1412 char *length_name
= concatenate_unary_strings(length
);
1415 _BT_LOGE_NODE(node_type_declarator
,
1416 "Cannot concatenate unary strings.");
1421 seq_decl
= bt_ctf_field_type_sequence_create(
1422 nested_decl
, length_name
);
1423 g_free(length_name
);
1424 BT_PUT(nested_decl
);
1426 _BT_LOGE_NODE(node_type_declarator
,
1427 "Cannot create sequence field type.");
1432 BT_MOVE(decl
, seq_decl
);
1440 assert(!nested_decl
);
1442 assert(!*field_decl
);
1445 * At this point, we found the next nested declaration.
1446 * We currently own this (and lost the ownership of
1447 * nested_decl in the meantime). Pass this next
1448 * nested declaration as the content of the outer
1449 * container, MOVING its ownership.
1451 ret
= visit_type_declarator(ctx
, type_specifier_list
,
1453 node_type_declarator
->
1454 u
.type_declarator
.u
.nested
.type_declarator
,
1455 &outer_field_decl
, decl
);
1458 assert(!outer_field_decl
);
1463 assert(outer_field_decl
);
1464 BT_MOVE(*field_decl
, outer_field_decl
);
1468 BT_PUT(nested_decl
);
1469 assert(*field_decl
);
1474 BT_PUT(nested_decl
);
1475 BT_PUT(*field_decl
);
1481 int visit_struct_decl_field(struct ctx
*ctx
,
1482 struct bt_ctf_field_type
*struct_decl
,
1483 struct ctf_node
*type_specifier_list
,
1484 struct bt_list_head
*type_declarators
)
1487 struct ctf_node
*iter
;
1488 _BT_CTF_FIELD_TYPE_INIT(field_decl
);
1490 bt_list_for_each_entry(iter
, type_declarators
, siblings
) {
1493 const char *field_name
;
1494 _BT_CTF_FIELD_TYPE_INIT(efield_decl
);
1496 ret
= visit_type_declarator(ctx
, type_specifier_list
,
1497 &qfield_name
, iter
, &field_decl
, NULL
);
1499 assert(!field_decl
);
1500 _BT_LOGE_NODE(type_specifier_list
,
1501 "Cannot visit type declarator: ret=%d", ret
);
1506 field_name
= g_quark_to_string(qfield_name
);
1508 /* Check if field with same name already exists */
1510 bt_ctf_field_type_structure_get_field_type_by_name(
1511 struct_decl
, field_name
);
1513 BT_PUT(efield_decl
);
1514 _BT_LOGE_NODE(type_specifier_list
,
1515 "Duplicate field in structure field type: "
1516 "field-name=\"%s\"", field_name
);
1521 /* Add field to structure */
1522 ret
= bt_ctf_field_type_structure_add_field(struct_decl
,
1523 field_decl
, field_name
);
1526 _BT_LOGE_NODE(type_specifier_list
,
1527 "Cannot add field to structure field type: "
1528 "field-name=\"%s\", ret=%d",
1529 g_quark_to_string(qfield_name
), ret
);
1543 int visit_variant_decl_field(struct ctx
*ctx
,
1544 struct bt_ctf_field_type
*variant_decl
,
1545 struct ctf_node
*type_specifier_list
,
1546 struct bt_list_head
*type_declarators
)
1549 struct ctf_node
*iter
;
1550 _BT_CTF_FIELD_TYPE_INIT(field_decl
);
1552 bt_list_for_each_entry(iter
, type_declarators
, siblings
) {
1555 const char *field_name
;
1556 _BT_CTF_FIELD_TYPE_INIT(efield_decl
);
1558 ret
= visit_type_declarator(ctx
, type_specifier_list
,
1559 &qfield_name
, iter
, &field_decl
, NULL
);
1561 assert(!field_decl
);
1562 _BT_LOGE_NODE(type_specifier_list
,
1563 "Cannot visit type declarator: ret=%d", ret
);
1568 field_name
= g_quark_to_string(qfield_name
);
1570 /* Check if field with same name already exists */
1572 bt_ctf_field_type_variant_get_field_type_by_name(
1573 variant_decl
, field_name
);
1575 BT_PUT(efield_decl
);
1576 _BT_LOGE_NODE(type_specifier_list
,
1577 "Duplicate field in variant field type: "
1578 "field-name=\"%s\"", field_name
);
1583 /* Add field to structure */
1584 ret
= bt_ctf_field_type_variant_add_field(variant_decl
,
1585 field_decl
, field_name
);
1588 _BT_LOGE_NODE(type_specifier_list
,
1589 "Cannot add field to variant field type: "
1590 "field-name=\"%s\", ret=%d",
1591 g_quark_to_string(qfield_name
), ret
);
1605 int visit_typedef(struct ctx
*ctx
, struct ctf_node
*type_specifier_list
,
1606 struct bt_list_head
*type_declarators
)
1610 struct ctf_node
*iter
;
1611 _BT_CTF_FIELD_TYPE_INIT(type_decl
);
1613 bt_list_for_each_entry(iter
, type_declarators
, siblings
) {
1614 ret
= visit_type_declarator(ctx
, type_specifier_list
,
1615 &qidentifier
, iter
, &type_decl
, NULL
);
1618 "Cannot visit type declarator: ret=%d", ret
);
1623 /* Do not allow typedef and typealias of untagged variants */
1624 if (bt_ctf_field_type_is_variant(type_decl
)) {
1625 if (bt_ctf_field_type_variant_get_tag_name(type_decl
)) {
1627 "Type definition of untagged variant field type is not allowed.");
1633 ret
= ctx_decl_scope_register_alias(ctx
->current_scope
,
1634 g_quark_to_string(qidentifier
), type_decl
);
1637 "Cannot register type definition: name=\"%s\"",
1638 g_quark_to_string(qidentifier
));
1650 int visit_typealias(struct ctx
*ctx
, struct ctf_node
*target
,
1651 struct ctf_node
*alias
)
1655 struct ctf_node
*node
;
1656 GQuark qdummy_field_name
;
1657 _BT_CTF_FIELD_TYPE_INIT(type_decl
);
1659 /* Create target type declaration */
1660 if (bt_list_empty(&target
->u
.typealias_target
.type_declarators
)) {
1663 node
= _BT_LIST_FIRST_ENTRY(
1664 &target
->u
.typealias_target
.type_declarators
,
1665 struct ctf_node
, siblings
);
1668 ret
= visit_type_declarator(ctx
,
1669 target
->u
.typealias_target
.type_specifier_list
,
1670 &qdummy_field_name
, node
, &type_decl
, NULL
);
1674 "Cannot visit type declarator: ret=%d", ret
);
1678 /* Do not allow typedef and typealias of untagged variants */
1679 if (bt_ctf_field_type_is_variant(type_decl
)) {
1680 if (bt_ctf_field_type_variant_get_tag_name(type_decl
)) {
1682 "Type definition of untagged variant field type is not allowed.");
1689 * The semantic validator does not check whether the target is
1690 * abstract or not (if it has an identifier). Check it here.
1692 if (qdummy_field_name
!= 0) {
1694 "Expecting empty identifier: id=\"%s\"",
1695 g_quark_to_string(qdummy_field_name
));
1700 /* Create alias identifier */
1701 node
= _BT_LIST_FIRST_ENTRY(&alias
->u
.typealias_alias
.type_declarators
,
1702 struct ctf_node
, siblings
);
1703 qalias
= create_typealias_identifier(ctx
,
1704 alias
->u
.typealias_alias
.type_specifier_list
, node
);
1705 ret
= ctx_decl_scope_register_alias(ctx
->current_scope
,
1706 g_quark_to_string(qalias
), type_decl
);
1709 "Cannot register type alias: name=\"%s\"",
1710 g_quark_to_string(qalias
));
1721 int visit_struct_decl_entry(struct ctx
*ctx
, struct ctf_node
*entry_node
,
1722 struct bt_ctf_field_type
*struct_decl
)
1726 switch (entry_node
->type
) {
1728 ret
= visit_typedef(ctx
,
1729 entry_node
->u
._typedef
.type_specifier_list
,
1730 &entry_node
->u
._typedef
.type_declarators
);
1732 _BT_LOGE_NODE(entry_node
,
1733 "Cannot add type definition found in structure field type: ret=%d",
1738 case NODE_TYPEALIAS
:
1739 ret
= visit_typealias(ctx
, entry_node
->u
.typealias
.target
,
1740 entry_node
->u
.typealias
.alias
);
1742 _BT_LOGE_NODE(entry_node
,
1743 "Cannot add type alias found in structure field type: ret=%d",
1748 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
1750 ret
= visit_struct_decl_field(ctx
, struct_decl
,
1751 entry_node
->u
.struct_or_variant_declaration
.
1752 type_specifier_list
,
1753 &entry_node
->u
.struct_or_variant_declaration
.
1760 _BT_LOGE_NODE(entry_node
,
1761 "Unexpected node type: node-type=%d", entry_node
->type
);
1771 int visit_variant_decl_entry(struct ctx
*ctx
, struct ctf_node
*entry_node
,
1772 struct bt_ctf_field_type
*variant_decl
)
1776 switch (entry_node
->type
) {
1778 ret
= visit_typedef(ctx
,
1779 entry_node
->u
._typedef
.type_specifier_list
,
1780 &entry_node
->u
._typedef
.type_declarators
);
1782 _BT_LOGE_NODE(entry_node
,
1783 "Cannot add type definition found in variant field type: ret=%d",
1788 case NODE_TYPEALIAS
:
1789 ret
= visit_typealias(ctx
, entry_node
->u
.typealias
.target
,
1790 entry_node
->u
.typealias
.alias
);
1792 _BT_LOGE_NODE(entry_node
,
1793 "Cannot add type alias found in variant field type: ret=%d",
1798 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
1800 ret
= visit_variant_decl_field(ctx
, variant_decl
,
1801 entry_node
->u
.struct_or_variant_declaration
.
1802 type_specifier_list
,
1803 &entry_node
->u
.struct_or_variant_declaration
.
1810 _BT_LOGE_NODE(entry_node
,
1811 "Unexpected node type: node-type=%d",
1822 int visit_struct_decl(struct ctx
*ctx
, const char *name
,
1823 struct bt_list_head
*decl_list
, int has_body
,
1824 struct bt_list_head
*min_align
,
1825 struct bt_ctf_field_type
**struct_decl
)
1829 *struct_decl
= NULL
;
1831 /* For named struct (without body), lookup in declaration scope */
1833 _BT_CTF_FIELD_TYPE_INIT(struct_decl_copy
);
1836 BT_LOGE_STR("Bodyless structure field type: missing name.");
1841 *struct_decl
= ctx_decl_scope_lookup_struct(ctx
->current_scope
,
1843 if (!*struct_decl
) {
1844 BT_LOGE("Cannot find structure field type: name=\"struct %s\"",
1850 /* Make a copy of it */
1851 struct_decl_copy
= bt_ctf_field_type_copy(*struct_decl
);
1852 if (!struct_decl_copy
) {
1853 BT_LOGE_STR("Cannot create copy of structure field type.");
1858 BT_MOVE(*struct_decl
, struct_decl_copy
);
1860 struct ctf_node
*entry_node
;
1861 uint64_t min_align_value
= 0;
1864 _BT_CTF_FIELD_TYPE_INIT(estruct_decl
);
1866 estruct_decl
= ctx_decl_scope_lookup_struct(
1867 ctx
->current_scope
, name
, 1);
1869 BT_PUT(estruct_decl
);
1870 BT_LOGE("Structure field type already declared in local scope: "
1871 "name=\"struct %s\"", name
);
1877 if (!bt_list_empty(min_align
)) {
1878 ret
= get_unary_unsigned(min_align
, &min_align_value
);
1880 BT_LOGE("Unexpected unary expression for structure field type's `align` attribute: "
1886 *struct_decl
= bt_ctf_field_type_structure_create();
1887 if (!*struct_decl
) {
1888 BT_LOGE_STR("Cannot create empty structure field type.");
1893 if (min_align_value
!= 0) {
1894 ret
= bt_ctf_field_type_set_alignment(*struct_decl
,
1897 BT_LOGE("Cannot set structure field type's alignment: "
1903 ret
= ctx_push_scope(ctx
);
1905 BT_LOGE_STR("Cannot push scope.");
1909 bt_list_for_each_entry(entry_node
, decl_list
, siblings
) {
1910 ret
= visit_struct_decl_entry(ctx
, entry_node
,
1913 _BT_LOGE_NODE(entry_node
,
1914 "Cannot visit structure field type entry: "
1924 ret
= ctx_decl_scope_register_struct(ctx
->current_scope
,
1925 name
, *struct_decl
);
1927 BT_LOGE("Cannot register structure field type in declaration scope: "
1928 "name=\"struct %s\", ret=%d", name
, ret
);
1937 BT_PUT(*struct_decl
);
1943 int visit_variant_decl(struct ctx
*ctx
, const char *name
,
1944 const char *tag
, struct bt_list_head
*decl_list
,
1945 int has_body
, struct bt_ctf_field_type
**variant_decl
)
1948 _BT_CTF_FIELD_TYPE_INIT(untagged_variant_decl
);
1950 *variant_decl
= NULL
;
1952 /* For named variant (without body), lookup in declaration scope */
1954 _BT_CTF_FIELD_TYPE_INIT(variant_decl_copy
);
1957 BT_LOGE_STR("Bodyless variant field type: missing name.");
1962 untagged_variant_decl
=
1963 ctx_decl_scope_lookup_variant(ctx
->current_scope
,
1965 if (!untagged_variant_decl
) {
1966 BT_LOGE("Cannot find variant field type: name=\"variant %s\"",
1972 /* Make a copy of it */
1973 variant_decl_copy
= bt_ctf_field_type_copy(
1974 untagged_variant_decl
);
1975 if (!variant_decl_copy
) {
1976 BT_LOGE_STR("Cannot create copy of variant field type.");
1981 BT_MOVE(untagged_variant_decl
, variant_decl_copy
);
1983 struct ctf_node
*entry_node
;
1986 struct bt_ctf_field_type
*evariant_decl
=
1987 ctx_decl_scope_lookup_struct(ctx
->current_scope
,
1990 if (evariant_decl
) {
1991 BT_PUT(evariant_decl
);
1992 BT_LOGE("Variant field type already declared in local scope: "
1993 "name=\"variant %s\"", name
);
1999 untagged_variant_decl
= bt_ctf_field_type_variant_create(NULL
,
2001 if (!untagged_variant_decl
) {
2002 BT_LOGE_STR("Cannot create empty variant field type.");
2007 ret
= ctx_push_scope(ctx
);
2009 BT_LOGE_STR("Cannot push scope.");
2013 bt_list_for_each_entry(entry_node
, decl_list
, siblings
) {
2014 ret
= visit_variant_decl_entry(ctx
, entry_node
,
2015 untagged_variant_decl
);
2017 _BT_LOGE_NODE(entry_node
,
2018 "Cannot visit variant field type entry: "
2028 ret
= ctx_decl_scope_register_variant(
2029 ctx
->current_scope
, name
,
2030 untagged_variant_decl
);
2032 BT_LOGE("Cannot register variant field type in declaration scope: "
2033 "name=\"variant %s\", ret=%d", name
, ret
);
2040 * If tagged, create tagged variant and return; otherwise
2041 * return untagged variant.
2044 BT_MOVE(*variant_decl
, untagged_variant_decl
);
2047 * At this point, we have a fresh untagged variant; nobody
2048 * else owns it. Set its tag now.
2050 ret
= bt_ctf_field_type_variant_set_tag_name(
2051 untagged_variant_decl
, tag
);
2053 BT_LOGE("Cannot set variant field type's tag name: "
2054 "tag-name=\"%s\"", tag
);
2058 BT_MOVE(*variant_decl
, untagged_variant_decl
);
2061 assert(!untagged_variant_decl
);
2062 assert(*variant_decl
);
2067 BT_PUT(untagged_variant_decl
);
2068 BT_PUT(*variant_decl
);
2074 int visit_enum_decl_entry(struct ctx
*ctx
, struct ctf_node
*enumerator
,
2075 struct bt_ctf_field_type
*enum_decl
, int64_t *last
, int is_signed
)
2079 struct ctf_node
*iter
;
2080 int64_t start
= 0, end
= 0;
2081 const char *label
= enumerator
->u
.enumerator
.id
;
2082 struct bt_list_head
*values
= &enumerator
->u
.enumerator
.values
;
2084 bt_list_for_each_entry(iter
, values
, siblings
) {
2087 if (iter
->type
!= NODE_UNARY_EXPRESSION
) {
2089 "Wrong expression for enumeration field type label: "
2090 "node-type=%d, label=\"%s\"", iter
->type
,
2102 switch (iter
->u
.unary_expression
.type
) {
2103 case UNARY_SIGNED_CONSTANT
:
2104 *target
= iter
->u
.unary_expression
.u
.signed_constant
;
2106 case UNARY_UNSIGNED_CONSTANT
:
2108 iter
->u
.unary_expression
.u
.unsigned_constant
;
2112 "Invalid enumeration field type entry: "
2113 "expecting constant signed or unsigned integer: "
2114 "node-type=%d, label=\"%s\"",
2115 iter
->u
.unary_expression
.type
, label
);
2122 "Invalid enumeration field type entry: label=\"%s\"",
2142 ret
= bt_ctf_field_type_enumeration_add_mapping(enum_decl
, label
,
2145 ret
= bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_decl
,
2146 label
, (uint64_t) start
, (uint64_t) end
);
2149 _BT_LOGE_NODE(enumerator
,
2150 "Cannot add mapping to enumeration field type: "
2151 "label=\"%s\", ret=%d, "
2152 "start-value-unsigned=%" PRIu64
", "
2153 "end-value-unsigned=%" PRIu64
, label
, ret
,
2154 (uint64_t) start
, (uint64_t) end
);
2165 int visit_enum_decl(struct ctx
*ctx
, const char *name
,
2166 struct ctf_node
*container_type
,
2167 struct bt_list_head
*enumerator_list
,
2169 struct bt_ctf_field_type
**enum_decl
)
2173 _BT_CTF_FIELD_TYPE_INIT(integer_decl
);
2177 /* For named enum (without body), lookup in declaration scope */
2179 _BT_CTF_FIELD_TYPE_INIT(enum_decl_copy
);
2182 BT_LOGE_STR("Bodyless enumeration field type: missing name.");
2187 *enum_decl
= ctx_decl_scope_lookup_enum(ctx
->current_scope
,
2190 BT_LOGE("Cannot find enumeration field type: "
2191 "name=\"enum %s\"", name
);
2196 /* Make a copy of it */
2197 enum_decl_copy
= bt_ctf_field_type_copy(*enum_decl
);
2198 if (!enum_decl_copy
) {
2199 BT_LOGE_STR("Cannot create copy of enumeration field type.");
2205 BT_MOVE(*enum_decl
, enum_decl_copy
);
2207 struct ctf_node
*iter
;
2208 int64_t last_value
= 0;
2211 _BT_CTF_FIELD_TYPE_INIT(eenum_decl
);
2213 eenum_decl
= ctx_decl_scope_lookup_enum(
2214 ctx
->current_scope
, name
, 1);
2217 BT_LOGE("Enumeration field type already declared in local scope: "
2218 "name=\"enum %s\"", name
);
2224 if (!container_type
) {
2225 integer_decl
= ctx_decl_scope_lookup_alias(
2226 ctx
->current_scope
, "int", -1);
2227 if (!integer_decl
) {
2228 BT_LOGE_STR("Cannot find implicit `int` field type alias for enumeration field type.");
2233 ret
= visit_type_declarator(ctx
, container_type
,
2234 &qdummy_id
, NULL
, &integer_decl
, NULL
);
2236 assert(!integer_decl
);
2242 assert(integer_decl
);
2244 if (!bt_ctf_field_type_is_integer(integer_decl
)) {
2245 BT_LOGE("Container field type for enumeration field type is not an integer field type: "
2247 bt_ctf_field_type_id_string(
2248 bt_ctf_field_type_get_type_id(integer_decl
)));
2253 *enum_decl
= bt_ctf_field_type_enumeration_create(integer_decl
);
2255 BT_LOGE_STR("Cannot create enumeration field type.");
2260 bt_list_for_each_entry(iter
, enumerator_list
, siblings
) {
2261 ret
= visit_enum_decl_entry(ctx
, iter
, *enum_decl
,
2263 bt_ctf_field_type_integer_get_signed(integer_decl
));
2266 "Cannot visit enumeration field type entry: "
2273 ret
= ctx_decl_scope_register_enum(ctx
->current_scope
,
2276 BT_LOGE("Cannot register enumeration field type in declaration scope: "
2283 BT_PUT(integer_decl
);
2288 BT_PUT(integer_decl
);
2295 int visit_type_specifier(struct ctx
*ctx
,
2296 struct ctf_node
*type_specifier_list
,
2297 struct bt_ctf_field_type
**decl
)
2300 GString
*str
= NULL
;
2301 _BT_CTF_FIELD_TYPE_INIT(decl_copy
);
2304 str
= g_string_new("");
2305 ret
= get_type_specifier_list_name(ctx
, type_specifier_list
, str
);
2307 _BT_LOGE_NODE(type_specifier_list
,
2308 "Cannot get type specifier list's name: ret=%d", ret
);
2312 *decl
= ctx_decl_scope_lookup_alias(ctx
->current_scope
, str
->str
, -1);
2314 _BT_LOGE_NODE(type_specifier_list
,
2315 "Cannot find type alias: name=\"%s\"", str
->str
);
2320 /* Make a copy of the type declaration */
2321 decl_copy
= bt_ctf_field_type_copy(*decl
);
2323 _BT_LOGE_NODE(type_specifier_list
,
2324 "Cannot create field type copy.");
2329 BT_MOVE(*decl
, decl_copy
);
2330 (void) g_string_free(str
, TRUE
);
2337 (void) g_string_free(str
, TRUE
);
2346 int visit_integer_decl(struct ctx
*ctx
,
2347 struct bt_list_head
*expressions
,
2348 struct bt_ctf_field_type
**integer_decl
)
2353 struct ctf_node
*expression
;
2354 uint64_t alignment
= 0, size
= 0;
2355 struct bt_ctf_clock_class
*mapped_clock
= NULL
;
2356 enum bt_ctf_string_encoding encoding
= BT_CTF_STRING_ENCODING_NONE
;
2357 enum bt_ctf_integer_base base
= BT_CTF_INTEGER_BASE_DECIMAL
;
2358 enum bt_ctf_byte_order byte_order
=
2359 bt_ctf_trace_get_native_byte_order(ctx
->trace
);
2361 *integer_decl
= NULL
;
2363 bt_list_for_each_entry(expression
, expressions
, siblings
) {
2364 struct ctf_node
*left
, *right
;
2366 left
= _BT_LIST_FIRST_ENTRY(&expression
->u
.ctf_expression
.left
,
2367 struct ctf_node
, siblings
);
2368 right
= _BT_LIST_FIRST_ENTRY(
2369 &expression
->u
.ctf_expression
.right
, struct ctf_node
,
2372 if (left
->u
.unary_expression
.type
!= UNARY_STRING
) {
2374 "Unexpected unary expression type: type=%d",
2375 left
->u
.unary_expression
.type
);
2380 if (!strcmp(left
->u
.unary_expression
.u
.string
, "signed")) {
2381 if (_IS_SET(&set
, _INTEGER_SIGNED_SET
)) {
2382 _BT_LOGE_DUP_ATTR(left
, "signed",
2383 "integer field type");
2388 signedness
= get_boolean(right
);
2389 if (signedness
< 0) {
2390 _BT_LOGE_NODE(right
,
2391 "Invalid boolean value for integer field type's `signed` attribute: "
2397 _SET(&set
, _INTEGER_SIGNED_SET
);
2398 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2400 if (_IS_SET(&set
, _INTEGER_BYTE_ORDER_SET
)) {
2401 _BT_LOGE_DUP_ATTR(left
, "byte_order",
2402 "integer field type");
2407 byte_order
= get_real_byte_order(ctx
, right
);
2408 if (byte_order
== BT_CTF_BYTE_ORDER_UNKNOWN
) {
2409 _BT_LOGE_NODE(right
,
2410 "Invalid `byte_order` attribute in integer field type: "
2416 _SET(&set
, _INTEGER_BYTE_ORDER_SET
);
2417 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "size")) {
2418 if (_IS_SET(&set
, _INTEGER_SIZE_SET
)) {
2419 _BT_LOGE_DUP_ATTR(left
, "size",
2420 "integer field type");
2425 if (right
->u
.unary_expression
.type
!=
2426 UNARY_UNSIGNED_CONSTANT
) {
2427 _BT_LOGE_NODE(right
,
2428 "Invalid `size` attribute in integer field type: "
2429 "expecting unsigned constant integer: "
2431 right
->u
.unary_expression
.type
);
2436 size
= right
->u
.unary_expression
.u
.unsigned_constant
;
2438 _BT_LOGE_NODE(right
,
2439 "Invalid `size` attribute in integer field type: "
2440 "expecting positive constant integer: "
2441 "size=%" PRIu64
, size
);
2444 } else if (size
> 64) {
2445 _BT_LOGE_NODE(right
,
2446 "Invalid `size` attribute in integer field type: "
2447 "integer fields over 64 bits are not supported as of this version: "
2448 "size=%" PRIu64
, size
);
2453 _SET(&set
, _INTEGER_SIZE_SET
);
2454 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2456 if (_IS_SET(&set
, _INTEGER_ALIGN_SET
)) {
2457 _BT_LOGE_DUP_ATTR(left
, "align",
2458 "integer field type");
2463 if (right
->u
.unary_expression
.type
!=
2464 UNARY_UNSIGNED_CONSTANT
) {
2465 _BT_LOGE_NODE(right
,
2466 "Invalid `align` attribute in integer field type: "
2467 "expecting unsigned constant integer: "
2469 right
->u
.unary_expression
.type
);
2475 right
->u
.unary_expression
.u
.unsigned_constant
;
2476 if (!is_align_valid(alignment
)) {
2477 _BT_LOGE_NODE(right
,
2478 "Invalid `align` attribute in integer field type: "
2479 "expecting power of two: "
2480 "align=%" PRIu64
, alignment
);
2485 _SET(&set
, _INTEGER_ALIGN_SET
);
2486 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "base")) {
2487 if (_IS_SET(&set
, _INTEGER_BASE_SET
)) {
2488 _BT_LOGE_DUP_ATTR(left
, "base",
2489 "integer field type");
2494 switch (right
->u
.unary_expression
.type
) {
2495 case UNARY_UNSIGNED_CONSTANT
:
2497 uint64_t constant
= right
->u
.unary_expression
.
2498 u
.unsigned_constant
;
2502 base
= BT_CTF_INTEGER_BASE_BINARY
;
2505 base
= BT_CTF_INTEGER_BASE_OCTAL
;
2508 base
= BT_CTF_INTEGER_BASE_DECIMAL
;
2511 base
= BT_CTF_INTEGER_BASE_HEXADECIMAL
;
2514 _BT_LOGE_NODE(right
,
2515 "Invalid `base` attribute in integer field type: "
2517 right
->u
.unary_expression
.u
.unsigned_constant
);
2525 char *s_right
= concatenate_unary_strings(
2526 &expression
->u
.ctf_expression
.right
);
2528 _BT_LOGE_NODE(right
,
2529 "Unexpected unary expression for integer field type's `base` attribute.");
2534 if (!strcmp(s_right
, "decimal") ||
2535 !strcmp(s_right
, "dec") ||
2536 !strcmp(s_right
, "d") ||
2537 !strcmp(s_right
, "i") ||
2538 !strcmp(s_right
, "u")) {
2539 base
= BT_CTF_INTEGER_BASE_DECIMAL
;
2540 } else if (!strcmp(s_right
, "hexadecimal") ||
2541 !strcmp(s_right
, "hex") ||
2542 !strcmp(s_right
, "x") ||
2543 !strcmp(s_right
, "X") ||
2544 !strcmp(s_right
, "p")) {
2545 base
= BT_CTF_INTEGER_BASE_HEXADECIMAL
;
2546 } else if (!strcmp(s_right
, "octal") ||
2547 !strcmp(s_right
, "oct") ||
2548 !strcmp(s_right
, "o")) {
2549 base
= BT_CTF_INTEGER_BASE_OCTAL
;
2550 } else if (!strcmp(s_right
, "binary") ||
2551 !strcmp(s_right
, "b")) {
2552 base
= BT_CTF_INTEGER_BASE_BINARY
;
2554 _BT_LOGE_NODE(right
,
2555 "Unexpected unary expression for integer field type's `base` attribute: "
2556 "base=\"%s\"", s_right
);
2566 _BT_LOGE_NODE(right
,
2567 "Invalid `base` attribute in integer field type: "
2568 "expecting unsigned constant integer or unary string.");
2573 _SET(&set
, _INTEGER_BASE_SET
);
2574 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2578 if (_IS_SET(&set
, _INTEGER_ENCODING_SET
)) {
2579 _BT_LOGE_DUP_ATTR(left
, "encoding",
2580 "integer field type");
2585 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
2586 _BT_LOGE_NODE(right
,
2587 "Invalid `encoding` attribute in integer field type: "
2588 "expecting unary string.");
2593 s_right
= concatenate_unary_strings(
2594 &expression
->u
.ctf_expression
.right
);
2596 _BT_LOGE_NODE(right
,
2597 "Unexpected unary expression for integer field type's `encoding` attribute.");
2602 if (!strcmp(s_right
, "UTF8") ||
2603 !strcmp(s_right
, "utf8") ||
2604 !strcmp(s_right
, "utf-8") ||
2605 !strcmp(s_right
, "UTF-8")) {
2606 encoding
= BT_CTF_STRING_ENCODING_UTF8
;
2607 } else if (!strcmp(s_right
, "ASCII") ||
2608 !strcmp(s_right
, "ascii")) {
2609 encoding
= BT_CTF_STRING_ENCODING_ASCII
;
2610 } else if (!strcmp(s_right
, "none")) {
2611 encoding
= BT_CTF_STRING_ENCODING_NONE
;
2613 _BT_LOGE_NODE(right
,
2614 "Invalid `encoding` attribute in integer field type: "
2615 "unknown encoding: encoding=\"%s\"",
2623 _SET(&set
, _INTEGER_ENCODING_SET
);
2624 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "map")) {
2625 const char *clock_name
;
2627 if (_IS_SET(&set
, _INTEGER_MAP_SET
)) {
2628 _BT_LOGE_DUP_ATTR(left
, "map",
2629 "integer field type");
2634 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
2635 _BT_LOGE_NODE(right
,
2636 "Invalid `map` attribute in integer field type: "
2637 "expecting unary string.");
2643 get_map_clock_name_value(
2644 &expression
->u
.ctf_expression
.right
);
2646 char *s_right
= concatenate_unary_strings(
2647 &expression
->u
.ctf_expression
.right
);
2650 _BT_LOGE_NODE(right
,
2651 "Unexpected unary expression for integer field type's `map` attribute.");
2656 _BT_LOGE_NODE(right
,
2657 "Invalid `map` attribute in integer field type: "
2658 "cannot find clock class at this point: name=\"%s\"",
2660 _SET(&set
, _INTEGER_MAP_SET
);
2665 mapped_clock
= bt_ctf_trace_get_clock_class_by_name(
2666 ctx
->trace
, clock_name
);
2667 if (!mapped_clock
) {
2668 _BT_LOGE_NODE(right
,
2669 "Invalid `map` attribute in integer field type: "
2670 "cannot find clock class at this point: name=\"%s\"",
2676 _SET(&set
, _INTEGER_MAP_SET
);
2679 "Unknown attribute in integer field type: "
2681 left
->u
.unary_expression
.u
.string
);
2685 if (!_IS_SET(&set
, _INTEGER_SIZE_SET
)) {
2686 BT_LOGE_STR("Missing `size` attribute in integer field type.");
2691 if (!_IS_SET(&set
, _INTEGER_ALIGN_SET
)) {
2692 if (size
% CHAR_BIT
) {
2693 /* Bit-packed alignment */
2696 /* Byte-packed alignment */
2697 alignment
= CHAR_BIT
;
2701 *integer_decl
= bt_ctf_field_type_integer_create((unsigned int) size
);
2702 if (!*integer_decl
) {
2703 BT_LOGE_STR("Cannot create integer field type.");
2708 ret
= bt_ctf_field_type_integer_set_signed(*integer_decl
, signedness
);
2709 ret
|= bt_ctf_field_type_integer_set_base(*integer_decl
, base
);
2710 ret
|= bt_ctf_field_type_integer_set_encoding(*integer_decl
, encoding
);
2711 ret
|= bt_ctf_field_type_set_alignment(*integer_decl
,
2712 (unsigned int) alignment
);
2713 ret
|= bt_ctf_field_type_set_byte_order(*integer_decl
, byte_order
);
2717 ret
|= bt_ctf_field_type_integer_set_mapped_clock_class(
2718 *integer_decl
, mapped_clock
);
2719 bt_put(mapped_clock
);
2720 mapped_clock
= NULL
;
2724 BT_LOGE_STR("Cannot configure integer field type.");
2733 bt_put(mapped_clock
);
2736 BT_PUT(*integer_decl
);
2742 int visit_floating_point_number_decl(struct ctx
*ctx
,
2743 struct bt_list_head
*expressions
,
2744 struct bt_ctf_field_type
**float_decl
)
2748 struct ctf_node
*expression
;
2749 uint64_t alignment
= 1, exp_dig
= 0, mant_dig
= 0;
2750 enum bt_ctf_byte_order byte_order
=
2751 bt_ctf_trace_get_native_byte_order(ctx
->trace
);
2755 bt_list_for_each_entry(expression
, expressions
, siblings
) {
2756 struct ctf_node
*left
, *right
;
2758 left
= _BT_LIST_FIRST_ENTRY(&expression
->u
.ctf_expression
.left
,
2759 struct ctf_node
, siblings
);
2760 right
= _BT_LIST_FIRST_ENTRY(
2761 &expression
->u
.ctf_expression
.right
, struct ctf_node
,
2764 if (left
->u
.unary_expression
.type
!= UNARY_STRING
) {
2766 "Unexpected unary expression type: type=%d",
2767 left
->u
.unary_expression
.type
);
2772 if (!strcmp(left
->u
.unary_expression
.u
.string
, "byte_order")) {
2773 if (_IS_SET(&set
, _FLOAT_BYTE_ORDER_SET
)) {
2774 _BT_LOGE_DUP_ATTR(left
, "byte_order",
2775 "floating point number field type");
2780 byte_order
= get_real_byte_order(ctx
, right
);
2781 if (byte_order
== BT_CTF_BYTE_ORDER_UNKNOWN
) {
2782 _BT_LOGE_NODE(right
,
2783 "Invalid `byte_order` attribute in floating point number field type: "
2789 _SET(&set
, _FLOAT_BYTE_ORDER_SET
);
2790 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2792 if (_IS_SET(&set
, _FLOAT_EXP_DIG_SET
)) {
2793 _BT_LOGE_DUP_ATTR(left
, "exp_dig",
2794 "floating point number field type");
2799 if (right
->u
.unary_expression
.type
!=
2800 UNARY_UNSIGNED_CONSTANT
) {
2801 _BT_LOGE_NODE(right
,
2802 "Invalid `exp_dig` attribute in floating point number field type: "
2803 "expecting unsigned constant integer: "
2805 right
->u
.unary_expression
.type
);
2810 exp_dig
= right
->u
.unary_expression
.u
.unsigned_constant
;
2811 _SET(&set
, _FLOAT_EXP_DIG_SET
);
2812 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2814 if (_IS_SET(&set
, _FLOAT_MANT_DIG_SET
)) {
2815 _BT_LOGE_DUP_ATTR(left
, "mant_dig",
2816 "floating point number field type");
2821 if (right
->u
.unary_expression
.type
!=
2822 UNARY_UNSIGNED_CONSTANT
) {
2823 _BT_LOGE_NODE(right
,
2824 "Invalid `mant_dig` attribute in floating point number field type: "
2825 "expecting unsigned constant integer: "
2827 right
->u
.unary_expression
.type
);
2832 mant_dig
= right
->u
.unary_expression
.u
.
2834 _SET(&set
, _FLOAT_MANT_DIG_SET
);
2835 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2837 if (_IS_SET(&set
, _FLOAT_ALIGN_SET
)) {
2838 _BT_LOGE_DUP_ATTR(left
, "align",
2839 "floating point number field type");
2844 if (right
->u
.unary_expression
.type
!=
2845 UNARY_UNSIGNED_CONSTANT
) {
2846 _BT_LOGE_NODE(right
,
2847 "Invalid `align` attribute in floating point number field type: "
2848 "expecting unsigned constant integer: "
2850 right
->u
.unary_expression
.type
);
2855 alignment
= right
->u
.unary_expression
.u
.
2858 if (!is_align_valid(alignment
)) {
2859 _BT_LOGE_NODE(right
,
2860 "Invalid `align` attribute in floating point number field type: "
2861 "expecting power of two: "
2862 "align=%" PRIu64
, alignment
);
2867 _SET(&set
, _FLOAT_ALIGN_SET
);
2870 "Unknown attribute in floating point number field type: "
2872 left
->u
.unary_expression
.u
.string
);
2876 if (!_IS_SET(&set
, _FLOAT_MANT_DIG_SET
)) {
2877 BT_LOGE_STR("Missing `mant_dig` attribute in floating point number field type.");
2882 if (!_IS_SET(&set
, _FLOAT_EXP_DIG_SET
)) {
2883 BT_LOGE_STR("Missing `exp_dig` attribute in floating point number field type.");
2888 if (!_IS_SET(&set
, _INTEGER_ALIGN_SET
)) {
2889 if ((mant_dig
+ exp_dig
) % CHAR_BIT
) {
2890 /* Bit-packed alignment */
2893 /* Byte-packed alignment */
2894 alignment
= CHAR_BIT
;
2898 *float_decl
= bt_ctf_field_type_floating_point_create();
2900 BT_LOGE_STR("Cannot create floating point number field type.");
2905 ret
= bt_ctf_field_type_floating_point_set_exponent_digits(
2906 *float_decl
, exp_dig
);
2907 ret
|= bt_ctf_field_type_floating_point_set_mantissa_digits(
2908 *float_decl
, mant_dig
);
2909 ret
|= bt_ctf_field_type_set_byte_order(*float_decl
, byte_order
);
2910 ret
|= bt_ctf_field_type_set_alignment(*float_decl
, alignment
);
2912 BT_LOGE_STR("Cannot configure floating point number field type.");
2920 BT_PUT(*float_decl
);
2926 int visit_string_decl(struct ctx
*ctx
,
2927 struct bt_list_head
*expressions
,
2928 struct bt_ctf_field_type
**string_decl
)
2932 struct ctf_node
*expression
;
2933 enum bt_ctf_string_encoding encoding
= BT_CTF_STRING_ENCODING_UTF8
;
2935 *string_decl
= NULL
;
2937 bt_list_for_each_entry(expression
, expressions
, siblings
) {
2938 struct ctf_node
*left
, *right
;
2940 left
= _BT_LIST_FIRST_ENTRY(&expression
->u
.ctf_expression
.left
,
2941 struct ctf_node
, siblings
);
2942 right
= _BT_LIST_FIRST_ENTRY(
2943 &expression
->u
.ctf_expression
.right
, struct ctf_node
,
2946 if (left
->u
.unary_expression
.type
!= UNARY_STRING
) {
2948 "Unexpected unary expression type: type=%d",
2949 left
->u
.unary_expression
.type
);
2954 if (!strcmp(left
->u
.unary_expression
.u
.string
, "encoding")) {
2957 if (_IS_SET(&set
, _STRING_ENCODING_SET
)) {
2958 _BT_LOGE_DUP_ATTR(left
, "encoding",
2959 "string field type");
2964 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
2965 _BT_LOGE_NODE(right
,
2966 "Invalid `encoding` attribute in string field type: "
2967 "expecting unary string.");
2972 s_right
= concatenate_unary_strings(
2973 &expression
->u
.ctf_expression
.right
);
2975 _BT_LOGE_NODE(right
,
2976 "Unexpected unary expression for string field type's `encoding` attribute.");
2981 if (!strcmp(s_right
, "UTF8") ||
2982 !strcmp(s_right
, "utf8") ||
2983 !strcmp(s_right
, "utf-8") ||
2984 !strcmp(s_right
, "UTF-8")) {
2985 encoding
= BT_CTF_STRING_ENCODING_UTF8
;
2986 } else if (!strcmp(s_right
, "ASCII") ||
2987 !strcmp(s_right
, "ascii")) {
2988 encoding
= BT_CTF_STRING_ENCODING_ASCII
;
2989 } else if (!strcmp(s_right
, "none")) {
2990 encoding
= BT_CTF_STRING_ENCODING_NONE
;
2992 _BT_LOGE_NODE(right
,
2993 "Invalid `encoding` attribute in string field type: "
2994 "unknown encoding: encoding=\"%s\"",
3002 _SET(&set
, _STRING_ENCODING_SET
);
3005 "Unknown attribute in string field type: "
3007 left
->u
.unary_expression
.u
.string
);
3011 *string_decl
= bt_ctf_field_type_string_create();
3012 if (!*string_decl
) {
3013 BT_LOGE_STR("Cannot create string field type.");
3018 ret
= bt_ctf_field_type_string_set_encoding(*string_decl
, encoding
);
3020 BT_LOGE_STR("Cannot configure string field type.");
3028 BT_PUT(*string_decl
);
3034 int visit_type_specifier_list(struct ctx
*ctx
,
3035 struct ctf_node
*ts_list
,
3036 struct bt_ctf_field_type
**decl
)
3039 struct ctf_node
*first
, *node
;
3043 if (ts_list
->type
!= NODE_TYPE_SPECIFIER_LIST
) {
3044 _BT_LOGE_NODE(ts_list
,
3045 "Unexpected node type: node-type=%d", ts_list
->type
);
3050 first
= _BT_LIST_FIRST_ENTRY(&ts_list
->u
.type_specifier_list
.head
,
3051 struct ctf_node
, siblings
);
3052 if (first
->type
!= NODE_TYPE_SPECIFIER
) {
3053 _BT_LOGE_NODE(first
,
3054 "Unexpected node type: node-type=%d", first
->type
);
3059 node
= first
->u
.type_specifier
.node
;
3061 switch (first
->u
.type_specifier
.type
) {
3062 case TYPESPEC_INTEGER
:
3063 ret
= visit_integer_decl(ctx
, &node
->u
.integer
.expressions
,
3070 case TYPESPEC_FLOATING_POINT
:
3071 ret
= visit_floating_point_number_decl(ctx
,
3072 &node
->u
.floating_point
.expressions
, decl
);
3078 case TYPESPEC_STRING
:
3079 ret
= visit_string_decl(ctx
,
3080 &node
->u
.string
.expressions
, decl
);
3086 case TYPESPEC_STRUCT
:
3087 ret
= visit_struct_decl(ctx
, node
->u
._struct
.name
,
3088 &node
->u
._struct
.declaration_list
,
3089 node
->u
._struct
.has_body
,
3090 &node
->u
._struct
.min_align
, decl
);
3096 case TYPESPEC_VARIANT
:
3097 ret
= visit_variant_decl(ctx
, node
->u
.variant
.name
,
3098 node
->u
.variant
.choice
,
3099 &node
->u
.variant
.declaration_list
,
3100 node
->u
.variant
.has_body
, decl
);
3107 ret
= visit_enum_decl(ctx
, node
->u
._enum
.enum_id
,
3108 node
->u
._enum
.container_type
,
3109 &node
->u
._enum
.enumerator_list
,
3110 node
->u
._enum
.has_body
, decl
);
3118 case TYPESPEC_SHORT
:
3121 case TYPESPEC_FLOAT
:
3122 case TYPESPEC_DOUBLE
:
3123 case TYPESPEC_SIGNED
:
3124 case TYPESPEC_UNSIGNED
:
3126 case TYPESPEC_COMPLEX
:
3127 case TYPESPEC_IMAGINARY
:
3128 case TYPESPEC_CONST
:
3129 case TYPESPEC_ID_TYPE
:
3130 ret
= visit_type_specifier(ctx
, ts_list
, decl
);
3132 _BT_LOGE_NODE(first
,
3133 "Cannot visit type specifier: ret=%d",
3140 _BT_LOGE_NODE(first
,
3141 "Unexpected type specifier type: node-type=%d",
3142 first
->u
.type_specifier
.type
);
3158 int visit_event_decl_entry(struct ctx
*ctx
, struct ctf_node
*node
,
3159 struct bt_ctf_event_class
*event_class
, int64_t *stream_id
,
3164 _BT_CTF_FIELD_TYPE_INIT(decl
);
3166 switch (node
->type
) {
3168 ret
= visit_typedef(ctx
, node
->u
._typedef
.type_specifier_list
,
3169 &node
->u
._typedef
.type_declarators
);
3172 "Cannot add type definition found in event class.");
3176 case NODE_TYPEALIAS
:
3177 ret
= visit_typealias(ctx
, node
->u
.typealias
.target
,
3178 node
->u
.typealias
.alias
);
3181 "Cannot add type alias found in event class.");
3185 case NODE_CTF_EXPRESSION
:
3187 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
3189 _BT_LOGE_NODE(node
, "Cannot concatenate unary strings.");
3194 if (!strcmp(left
, "name")) {
3195 /* This is already known at this stage */
3196 if (_IS_SET(set
, _EVENT_NAME_SET
)) {
3197 _BT_LOGE_DUP_ATTR(node
, "name",
3203 _SET(set
, _EVENT_NAME_SET
);
3204 } else if (!strcmp(left
, "id")) {
3207 if (_IS_SET(set
, _EVENT_ID_SET
)) {
3208 _BT_LOGE_DUP_ATTR(node
, "id",
3214 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
3216 /* Only read "id" if get_unary_unsigned() succeeded. */
3217 if (ret
|| (!ret
&& id
< 0)) {
3219 "Unexpected unary expression for event class's `id` attribute.");
3224 ret
= bt_ctf_event_class_set_id(event_class
, id
);
3227 "Cannot set event class's ID: "
3232 _SET(set
, _EVENT_ID_SET
);
3233 } else if (!strcmp(left
, "stream_id")) {
3234 if (_IS_SET(set
, _EVENT_STREAM_ID_SET
)) {
3235 _BT_LOGE_DUP_ATTR(node
, "stream_id",
3241 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
3242 (uint64_t *) stream_id
);
3244 * Only read "stream_id" if get_unary_unsigned()
3247 if (ret
|| (!ret
&& *stream_id
< 0)) {
3249 "Unexpected unary expression for event class's `stream_id` attribute.");
3254 _SET(set
, _EVENT_STREAM_ID_SET
);
3255 } else if (!strcmp(left
, "context")) {
3256 if (_IS_SET(set
, _EVENT_CONTEXT_SET
)) {
3258 "Duplicate `context` entry in event class.");
3263 ret
= visit_type_specifier_list(ctx
,
3264 _BT_LIST_FIRST_ENTRY(
3265 &node
->u
.ctf_expression
.right
,
3266 struct ctf_node
, siblings
),
3270 "Cannot create event class's context field type.");
3275 ret
= bt_ctf_event_class_set_context_type(
3280 "Cannot set event class's context field type.");
3284 _SET(set
, _EVENT_CONTEXT_SET
);
3285 } else if (!strcmp(left
, "fields")) {
3286 if (_IS_SET(set
, _EVENT_FIELDS_SET
)) {
3288 "Duplicate `fields` entry in event class.");
3293 ret
= visit_type_specifier_list(ctx
,
3294 _BT_LIST_FIRST_ENTRY(
3295 &node
->u
.ctf_expression
.right
,
3296 struct ctf_node
, siblings
),
3300 "Cannot create event class's payload field type.");
3305 ret
= bt_ctf_event_class_set_payload_type(
3310 "Cannot set event class's payload field type.");
3314 _SET(set
, _EVENT_FIELDS_SET
);
3315 } else if (!strcmp(left
, "loglevel")) {
3316 uint64_t loglevel_value
;
3317 const char *loglevel_str
;
3318 struct bt_value
*value_obj
, *str_obj
;
3320 if (_IS_SET(set
, _EVENT_LOGLEVEL_SET
)) {
3321 _BT_LOGE_DUP_ATTR(node
, "loglevel",
3327 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
3331 "Unexpected unary expression for event class's `loglevel` attribute.");
3335 value_obj
= bt_value_integer_create_init(loglevel_value
);
3338 "Cannot create integer value object.");
3342 if (bt_ctf_event_class_set_attribute(event_class
,
3343 "loglevel", value_obj
) != BT_VALUE_STATUS_OK
) {
3345 "Cannot set event class's `loglevel` attribute.");
3350 loglevel_str
= print_loglevel(loglevel_value
);
3352 str_obj
= bt_value_string_create_init(loglevel_str
);
3353 if (bt_ctf_event_class_set_attribute(event_class
,
3354 "loglevel_string", str_obj
) != BT_VALUE_STATUS_OK
) {
3356 "Cannot set event class's `loglevel_string` attribute.");
3364 _SET(set
, _EVENT_LOGLEVEL_SET
);
3365 } else if (!strcmp(left
, "model.emf.uri")) {
3367 struct bt_value
*str_obj
;
3369 if (_IS_SET(set
, _EVENT_MODEL_EMF_URI_SET
)) {
3370 _BT_LOGE_DUP_ATTR(node
, "model.emf.uri",
3376 right
= concatenate_unary_strings(
3377 &node
->u
.ctf_expression
.right
);
3380 "Unexpected unary expression for event class's `model.emf.uri` attribute.");
3385 str_obj
= bt_value_string_create_init(right
);
3386 if (bt_ctf_event_class_set_attribute(event_class
,
3387 "model.emf.uri", str_obj
) != BT_VALUE_STATUS_OK
) {
3389 "Cannot set event class's `model.emf.uri` attribute.");
3396 _SET(set
, _EVENT_MODEL_EMF_URI_SET
);
3399 "Unknown attribute in event class: "
3400 "attr-name=\"%s\"", left
);
3425 char *get_event_decl_name(struct ctx
*ctx
, struct ctf_node
*node
)
3429 struct ctf_node
*iter
;
3430 struct bt_list_head
*decl_list
= &node
->u
.event
.declaration_list
;
3432 bt_list_for_each_entry(iter
, decl_list
, siblings
) {
3433 if (iter
->type
!= NODE_CTF_EXPRESSION
) {
3437 left
= concatenate_unary_strings(&iter
->u
.ctf_expression
.left
);
3440 "Cannot concatenate unary strings.");
3444 if (!strcmp(left
, "name")) {
3445 name
= concatenate_unary_strings(
3446 &iter
->u
.ctf_expression
.right
);
3449 "Unexpected unary expression for event class's `name` attribute.");
3471 int reset_event_decl_types(struct ctx
*ctx
,
3472 struct bt_ctf_event_class
*event_class
)
3477 ret
= bt_ctf_event_class_set_context_type(event_class
, NULL
);
3479 BT_LOGE("Cannot reset initial event class's context field type: "
3480 "event-name=\"%s\"",
3481 bt_ctf_event_class_get_name(event_class
));
3485 /* Event payload. */
3486 ret
= bt_ctf_event_class_set_payload_type(event_class
, NULL
);
3488 BT_LOGE("Cannot reset initial event class's payload field type: "
3489 "event-name=\"%s\"",
3490 bt_ctf_event_class_get_name(event_class
));
3498 int reset_stream_decl_types(struct ctx
*ctx
,
3499 struct bt_ctf_stream_class
*stream_class
)
3503 /* Packet context. */
3504 ret
= bt_ctf_stream_class_set_packet_context_type(stream_class
, NULL
);
3506 BT_LOGE_STR("Cannot reset initial stream class's packet context field type.");
3511 ret
= bt_ctf_stream_class_set_event_header_type(stream_class
, NULL
);
3513 BT_LOGE_STR("Cannot reset initial stream class's event header field type.");
3517 /* Event context. */
3518 ret
= bt_ctf_stream_class_set_event_context_type(stream_class
, NULL
);
3520 BT_LOGE_STR("Cannot reset initial stream class's event context field type.");
3528 struct bt_ctf_stream_class
*create_reset_stream_class(struct ctx
*ctx
)
3531 struct bt_ctf_stream_class
*stream_class
;
3533 stream_class
= bt_ctf_stream_class_create_empty(NULL
);
3534 if (!stream_class
) {
3535 BT_LOGE_STR("Cannot create empty stream class.");
3540 * Set packet context, event header, and event context to NULL to
3541 * override the default ones.
3543 ret
= reset_stream_decl_types(ctx
, stream_class
);
3548 return stream_class
;
3551 BT_PUT(stream_class
);
3557 int visit_event_decl(struct ctx
*ctx
, struct ctf_node
*node
)
3562 struct ctf_node
*iter
;
3563 int64_t stream_id
= -1;
3564 char *event_name
= NULL
;
3565 struct bt_ctf_event_class
*event_class
= NULL
;
3566 struct bt_ctf_event_class
*eevent_class
;
3567 struct bt_ctf_stream_class
*stream_class
= NULL
;
3568 struct bt_list_head
*decl_list
= &node
->u
.event
.declaration_list
;
3569 bool pop_scope
= false;
3571 if (node
->visited
) {
3575 node
->visited
= TRUE
;
3576 event_name
= get_event_decl_name(ctx
, node
);
3579 "Missing `name` attribute in event class.");
3584 event_class
= bt_ctf_event_class_create(event_name
);
3587 * Unset context and fields to override the default ones.
3589 ret
= reset_event_decl_types(ctx
, event_class
);
3592 "Cannot reset event class's field types: "
3597 ret
= ctx_push_scope(ctx
);
3599 BT_LOGE_STR("Cannot push scope.");
3605 bt_list_for_each_entry(iter
, decl_list
, siblings
) {
3606 ret
= visit_event_decl_entry(ctx
, iter
, event_class
,
3609 _BT_LOGE_NODE(iter
, "Cannot visit event class's entry: "
3615 if (!_IS_SET(&set
, _EVENT_STREAM_ID_SET
)) {
3617 struct bt_ctf_stream_class
*new_stream_class
;
3618 size_t stream_class_count
=
3619 g_hash_table_size(ctx
->stream_classes
) +
3620 bt_ctf_trace_get_stream_class_count(ctx
->trace
);
3623 * Allow missing stream_id if there is only a single
3626 switch (stream_class_count
) {
3628 /* Create implicit stream class if there's none */
3629 new_stream_class
= create_reset_stream_class(ctx
);
3630 if (!new_stream_class
) {
3632 "Cannot create empty stream class.");
3637 ret
= bt_ctf_stream_class_set_id(new_stream_class
, 0);
3640 "Cannot set stream class's ID: "
3641 "id=0, ret=%d", ret
);
3642 BT_PUT(new_stream_class
);
3648 /* Move reference to visitor's context */
3649 g_hash_table_insert(ctx
->stream_classes
,
3650 (gpointer
) stream_id
, new_stream_class
);
3651 new_stream_class
= NULL
;
3654 /* Single stream class: get its ID */
3655 if (g_hash_table_size(ctx
->stream_classes
) == 1) {
3656 keys
= g_hash_table_get_keys(ctx
->stream_classes
);
3657 stream_id
= (int64_t) keys
->data
;
3660 assert(bt_ctf_trace_get_stream_class_count(
3663 bt_ctf_trace_get_stream_class_by_index(
3665 assert(stream_class
);
3666 stream_id
= bt_ctf_stream_class_get_id(
3668 BT_PUT(stream_class
);
3673 "Missing `stream_id` attribute in event class.");
3679 assert(stream_id
>= 0);
3681 /* We have the stream ID now; get the stream class if found */
3682 stream_class
= g_hash_table_lookup(ctx
->stream_classes
,
3683 (gpointer
) stream_id
);
3684 bt_get(stream_class
);
3685 if (!stream_class
) {
3686 stream_class
= bt_ctf_trace_get_stream_class_by_id(ctx
->trace
,
3688 if (!stream_class
) {
3690 "Cannot find stream class at this point: "
3691 "id=%" PRId64
, stream_id
);
3697 assert(stream_class
);
3699 if (!_IS_SET(&set
, _EVENT_ID_SET
)) {
3700 /* Allow only one event without ID per stream */
3701 if (bt_ctf_stream_class_get_event_class_count(stream_class
) !=
3704 "Missing `id` attribute in event class.");
3710 ret
= bt_ctf_event_class_set_id(event_class
, 0);
3713 "Cannot set event class's ID: id=0, ret=%d",
3719 event_id
= bt_ctf_event_class_get_id(event_class
);
3721 _BT_LOGE_NODE(node
, "Cannot get event class's ID.");
3726 eevent_class
= bt_ctf_stream_class_get_event_class_by_id(stream_class
,
3729 BT_PUT(eevent_class
);
3731 "Duplicate event class (same ID) in the same stream class: "
3732 "id=%" PRId64
, event_id
);
3737 ret
= bt_ctf_stream_class_add_event_class(stream_class
, event_class
);
3738 BT_PUT(event_class
);
3741 "Cannot add event class to stream class: ret=%d", ret
);
3748 bt_put(event_class
);
3756 bt_put(stream_class
);
3761 int visit_stream_decl_entry(struct ctx
*ctx
, struct ctf_node
*node
,
3762 struct bt_ctf_stream_class
*stream_class
, int *set
)
3766 _BT_CTF_FIELD_TYPE_INIT(decl
);
3768 switch (node
->type
) {
3770 ret
= visit_typedef(ctx
, node
->u
._typedef
.type_specifier_list
,
3771 &node
->u
._typedef
.type_declarators
);
3774 "Cannot add type definition found in stream class.");
3778 case NODE_TYPEALIAS
:
3779 ret
= visit_typealias(ctx
, node
->u
.typealias
.target
,
3780 node
->u
.typealias
.alias
);
3783 "Cannot add type alias found in stream class.");
3787 case NODE_CTF_EXPRESSION
:
3789 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
3791 _BT_LOGE_NODE(node
, "Cannot concatenate unary strings.");
3796 if (!strcmp(left
, "id")) {
3800 if (_IS_SET(set
, _STREAM_ID_SET
)) {
3801 _BT_LOGE_DUP_ATTR(node
, "id",
3802 "stream declaration");
3807 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
3809 /* Only read "id" if get_unary_unsigned() succeeded. */
3810 if (ret
|| (!ret
&& id
< 0)) {
3812 "Unexpected unary expression for stream class's `id` attribute.");
3817 ptr
= g_hash_table_lookup(ctx
->stream_classes
,
3821 "Duplicate stream class (same ID): id=%" PRId64
,
3827 ret
= bt_ctf_stream_class_set_id(stream_class
, id
);
3830 "Cannot set stream class's ID: "
3831 "id=%" PRId64
", ret=%d", id
, ret
);
3835 _SET(set
, _STREAM_ID_SET
);
3836 } else if (!strcmp(left
, "event.header")) {
3837 if (_IS_SET(set
, _STREAM_EVENT_HEADER_SET
)) {
3839 "Duplicate `event.header` entry in stream class.");
3844 ret
= visit_type_specifier_list(ctx
,
3845 _BT_LIST_FIRST_ENTRY(
3846 &node
->u
.ctf_expression
.right
,
3847 struct ctf_node
, siblings
),
3851 "Cannot create stream class's event header field type.");
3857 ret
= bt_ctf_stream_class_set_event_header_type(
3858 stream_class
, decl
);
3862 "Cannot set stream class's event header field type.");
3866 _SET(set
, _STREAM_EVENT_HEADER_SET
);
3867 } else if (!strcmp(left
, "event.context")) {
3868 if (_IS_SET(set
, _STREAM_EVENT_CONTEXT_SET
)) {
3870 "Duplicate `event.context` entry in stream class.");
3875 ret
= visit_type_specifier_list(ctx
,
3876 _BT_LIST_FIRST_ENTRY(
3877 &node
->u
.ctf_expression
.right
,
3878 struct ctf_node
, siblings
),
3882 "Cannot create stream class's event context field type.");
3888 ret
= bt_ctf_stream_class_set_event_context_type(
3889 stream_class
, decl
);
3893 "Cannot set stream class's event context field type.");
3897 _SET(set
, _STREAM_EVENT_CONTEXT_SET
);
3898 } else if (!strcmp(left
, "packet.context")) {
3899 if (_IS_SET(set
, _STREAM_PACKET_CONTEXT_SET
)) {
3901 "Duplicate `packet.context` entry in stream class.");
3906 ret
= visit_type_specifier_list(ctx
,
3907 _BT_LIST_FIRST_ENTRY(
3908 &node
->u
.ctf_expression
.right
,
3909 struct ctf_node
, siblings
),
3913 "Cannot create stream class's packet context field type.");
3919 ret
= bt_ctf_stream_class_set_packet_context_type(
3920 stream_class
, decl
);
3924 "Cannot set stream class's packet context field type.");
3928 _SET(set
, _STREAM_PACKET_CONTEXT_SET
);
3931 "Unknown attribute in stream class: "
3932 "attr-name=\"%s\"", left
);
3955 int visit_stream_decl(struct ctx
*ctx
, struct ctf_node
*node
)
3960 struct ctf_node
*iter
;
3961 struct bt_ctf_stream_class
*stream_class
= NULL
;
3962 struct bt_ctf_stream_class
*existing_stream_class
= NULL
;
3963 struct bt_list_head
*decl_list
= &node
->u
.stream
.declaration_list
;
3965 if (node
->visited
) {
3969 node
->visited
= TRUE
;
3970 stream_class
= create_reset_stream_class(ctx
);
3971 if (!stream_class
) {
3972 _BT_LOGE_NODE(node
, "Cannot create empty stream class.");
3977 ret
= ctx_push_scope(ctx
);
3979 BT_LOGE_STR("Cannot push scope.");
3983 bt_list_for_each_entry(iter
, decl_list
, siblings
) {
3984 ret
= visit_stream_decl_entry(ctx
, iter
, stream_class
, &set
);
3987 "Cannot visit stream class's entry: "
3996 if (_IS_SET(&set
, _STREAM_ID_SET
)) {
3997 /* Check that packet header has stream_id field */
3998 _BT_CTF_FIELD_TYPE_INIT(stream_id_decl
);
3999 _BT_CTF_FIELD_TYPE_INIT(packet_header_decl
);
4001 packet_header_decl
=
4002 bt_ctf_trace_get_packet_header_type(ctx
->trace
);
4003 if (!packet_header_decl
) {
4005 "Stream class has a `id` attribute, "
4006 "but trace has no packet header field type.");
4011 bt_ctf_field_type_structure_get_field_type_by_name(
4012 packet_header_decl
, "stream_id");
4013 BT_PUT(packet_header_decl
);
4014 if (!stream_id_decl
) {
4016 "Stream class has a `id` attribute, "
4017 "but trace's packet header field type has no `stream_id` field.");
4021 if (!bt_ctf_field_type_is_integer(stream_id_decl
)) {
4022 BT_PUT(stream_id_decl
);
4024 "Stream class has a `id` attribute, "
4025 "but trace's packet header field type's `stream_id` field is not an integer field type.");
4029 BT_PUT(stream_id_decl
);
4031 /* Allow only _one_ ID-less stream */
4032 if (g_hash_table_size(ctx
->stream_classes
) != 0) {
4034 "Missing `id` attribute in stream class as there's more than one stream class in the trace.");
4039 /* Automatic ID: 0 */
4040 ret
= bt_ctf_stream_class_set_id(stream_class
, 0);
4044 id
= bt_ctf_stream_class_get_id(stream_class
);
4047 "Cannot get stream class's ID.");
4053 * Make sure that this stream class's ID is currently unique in
4056 existing_stream_class
= bt_ctf_trace_get_stream_class_by_id(ctx
->trace
,
4058 if (g_hash_table_lookup(ctx
->stream_classes
, (gpointer
) id
) ||
4059 existing_stream_class
) {
4061 "Duplicate stream class (same ID): id=%" PRId64
,
4067 /* Move reference to visitor's context */
4068 g_hash_table_insert(ctx
->stream_classes
, (gpointer
) (int64_t) id
,
4070 stream_class
= NULL
;
4074 bt_put(stream_class
);
4077 bt_put(existing_stream_class
);
4082 int visit_trace_decl_entry(struct ctx
*ctx
, struct ctf_node
*node
, int *set
)
4086 _BT_CTF_FIELD_TYPE_INIT(packet_header_decl
);
4088 switch (node
->type
) {
4090 ret
= visit_typedef(ctx
, node
->u
._typedef
.type_specifier_list
,
4091 &node
->u
._typedef
.type_declarators
);
4094 "Cannot add type definition found in trace (`trace` block).");
4098 case NODE_TYPEALIAS
:
4099 ret
= visit_typealias(ctx
, node
->u
.typealias
.target
,
4100 node
->u
.typealias
.alias
);
4103 "Cannot add type alias found in trace (`trace` block).");
4107 case NODE_CTF_EXPRESSION
:
4109 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
4111 _BT_LOGE_NODE(node
, "Cannot concatenate unary strings.");
4116 if (!strcmp(left
, "major")) {
4117 if (_IS_SET(set
, _TRACE_MAJOR_SET
)) {
4118 _BT_LOGE_DUP_ATTR(node
, "major", "trace");
4123 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
4127 "Unexpected unary expression for trace's `major` attribute.");
4132 _SET(set
, _TRACE_MAJOR_SET
);
4133 } else if (!strcmp(left
, "minor")) {
4134 if (_IS_SET(set
, _TRACE_MINOR_SET
)) {
4135 _BT_LOGE_DUP_ATTR(node
, "minor", "trace");
4140 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
4144 "Unexpected unary expression for trace's `minor` attribute.");
4149 _SET(set
, _TRACE_MINOR_SET
);
4150 } else if (!strcmp(left
, "uuid")) {
4151 if (_IS_SET(set
, _TRACE_UUID_SET
)) {
4152 _BT_LOGE_DUP_ATTR(node
, "uuid", "trace");
4157 ret
= get_unary_uuid(&node
->u
.ctf_expression
.right
,
4161 "Invalid trace's `uuid` attribute.");
4165 ret
= bt_ctf_trace_set_uuid(ctx
->trace
, ctx
->trace_uuid
);
4167 _BT_LOGE_NODE(node
, "Cannot set trace's UUID.");
4171 _SET(set
, _TRACE_UUID_SET
);
4172 } else if (!strcmp(left
, "byte_order")) {
4173 /* Native byte order is already known at this stage */
4174 if (_IS_SET(set
, _TRACE_BYTE_ORDER_SET
)) {
4175 _BT_LOGE_DUP_ATTR(node
, "byte_order",
4181 _SET(set
, _TRACE_BYTE_ORDER_SET
);
4182 } else if (!strcmp(left
, "packet.header")) {
4183 if (_IS_SET(set
, _TRACE_PACKET_HEADER_SET
)) {
4185 "Duplicate `packet.header` entry in trace.");
4190 ret
= visit_type_specifier_list(ctx
,
4191 _BT_LIST_FIRST_ENTRY(
4192 &node
->u
.ctf_expression
.right
,
4193 struct ctf_node
, siblings
),
4194 &packet_header_decl
);
4197 "Cannot create trace's packet header field type.");
4201 assert(packet_header_decl
);
4202 ret
= bt_ctf_trace_set_packet_header_type(ctx
->trace
,
4203 packet_header_decl
);
4204 BT_PUT(packet_header_decl
);
4207 "Cannot set trace's packet header field type.");
4211 _SET(set
, _TRACE_PACKET_HEADER_SET
);
4214 "Unknown attribute in stream class: "
4215 "attr-name=\"%s\"", left
);
4223 _BT_LOGE_NODE(node
, "Unknown expression in trace.");
4232 BT_PUT(packet_header_decl
);
4238 int visit_trace_decl(struct ctx
*ctx
, struct ctf_node
*node
)
4242 struct ctf_node
*iter
;
4243 struct bt_list_head
*decl_list
= &node
->u
.trace
.declaration_list
;
4245 if (node
->visited
) {
4249 node
->visited
= TRUE
;
4251 if (ctx
->is_trace_visited
) {
4252 _BT_LOGE_NODE(node
, "Duplicate trace (`trace` block).");
4257 ret
= ctx_push_scope(ctx
);
4259 BT_LOGE_STR("Cannot push scope.");
4263 bt_list_for_each_entry(iter
, decl_list
, siblings
) {
4264 ret
= visit_trace_decl_entry(ctx
, iter
, &set
);
4266 _BT_LOGE_NODE(iter
, "Cannot visit trace's entry (`trace` block): "
4275 if (!_IS_SET(&set
, _TRACE_MAJOR_SET
)) {
4277 "Missing `major` attribute in trace (`trace` block).");
4282 if (!_IS_SET(&set
, _TRACE_MINOR_SET
)) {
4284 "Missing `minor` attribute in trace (`trace` block).");
4289 if (!_IS_SET(&set
, _TRACE_BYTE_ORDER_SET
)) {
4291 "Missing `byte_order` attribute in trace (`trace` block).");
4296 ctx
->is_trace_visited
= TRUE
;
4306 int visit_env(struct ctx
*ctx
, struct ctf_node
*node
)
4310 struct ctf_node
*entry_node
;
4311 struct bt_list_head
*decl_list
= &node
->u
.env
.declaration_list
;
4313 if (node
->visited
) {
4317 node
->visited
= TRUE
;
4319 bt_list_for_each_entry(entry_node
, decl_list
, siblings
) {
4320 struct bt_list_head
*right_head
=
4321 &entry_node
->u
.ctf_expression
.right
;
4323 if (entry_node
->type
!= NODE_CTF_EXPRESSION
) {
4324 _BT_LOGE_NODE(entry_node
,
4325 "Wrong expression in environment entry: "
4326 "node-type=%d", entry_node
->type
);
4331 left
= concatenate_unary_strings(
4332 &entry_node
->u
.ctf_expression
.left
);
4334 _BT_LOGE_NODE(entry_node
,
4335 "Cannot get environment entry's name.");
4340 if (is_unary_string(right_head
)) {
4341 char *right
= concatenate_unary_strings(right_head
);
4344 _BT_LOGE_NODE(entry_node
,
4345 "Unexpected unary expression for environment entry's value: "
4346 "name=\"%s\"", left
);
4351 if (strcmp(left
, "tracer_name") == 0) {
4352 if (strncmp(right
, "lttng", 5) == 0) {
4353 BT_LOGI("Detected LTTng trace from `%s` environment value: "
4354 "tracer-name=\"%s\"",
4360 ret
= bt_ctf_trace_set_environment_field_string(
4361 ctx
->trace
, left
, right
);
4365 _BT_LOGE_NODE(entry_node
,
4366 "Cannot add string environment entry to trace: "
4367 "name=\"%s\", ret=%d", left
, ret
);
4370 } else if (is_unary_unsigned(right_head
) ||
4371 is_unary_signed(right_head
)) {
4374 if (is_unary_unsigned(right_head
)) {
4375 ret
= get_unary_unsigned(right_head
,
4378 ret
= get_unary_signed(right_head
, &v
);
4381 _BT_LOGE_NODE(entry_node
,
4382 "Unexpected unary expression for environment entry's value: "
4383 "name=\"%s\"", left
);
4388 ret
= bt_ctf_trace_set_environment_field_integer(
4389 ctx
->trace
, left
, v
);
4391 _BT_LOGE_NODE(entry_node
,
4392 "Cannot add integer environment entry to trace: "
4393 "name=\"%s\", ret=%d", left
, ret
);
4397 _BT_LOGW_NODE(entry_node
,
4398 "Environment entry has unknown type: "
4399 "name=\"%s\"", left
);
4416 int set_trace_byte_order(struct ctx
*ctx
, struct ctf_node
*trace_node
)
4421 struct ctf_node
*node
;
4422 struct bt_list_head
*decl_list
= &trace_node
->u
.trace
.declaration_list
;
4424 bt_list_for_each_entry(node
, decl_list
, siblings
) {
4425 if (node
->type
== NODE_CTF_EXPRESSION
) {
4426 struct ctf_node
*right_node
;
4428 left
= concatenate_unary_strings(
4429 &node
->u
.ctf_expression
.left
);
4432 "Cannot concatenate unary strings.");
4437 if (!strcmp(left
, "byte_order")) {
4438 enum bt_ctf_byte_order bo
;
4440 if (_IS_SET(&set
, _TRACE_BYTE_ORDER_SET
)) {
4441 _BT_LOGE_DUP_ATTR(node
, "byte_order",
4447 _SET(&set
, _TRACE_BYTE_ORDER_SET
);
4448 right_node
= _BT_LIST_FIRST_ENTRY(
4449 &node
->u
.ctf_expression
.right
,
4450 struct ctf_node
, siblings
);
4451 bo
= byte_order_from_unary_expr(right_node
);
4452 if (bo
== BT_CTF_BYTE_ORDER_UNKNOWN
) {
4454 "Invalid `byte_order` attribute in trace (`trace` block): "
4455 "expecting `le`, `be`, or `network`.");
4458 } else if (bo
== BT_CTF_BYTE_ORDER_NATIVE
) {
4460 "Invalid `byte_order` attribute in trace (`trace` block): "
4461 "cannot be set to `native` here.");
4467 ret
= bt_ctf_trace_set_native_byte_order(
4471 "Cannot set trace's byte order: "
4482 if (!_IS_SET(&set
, _TRACE_BYTE_ORDER_SET
)) {
4483 _BT_LOGE_NODE(trace_node
,
4484 "Missing `byte_order` attribute in trace (`trace` block).");
4498 int visit_clock_decl_entry(struct ctx
*ctx
, struct ctf_node
*entry_node
,
4499 struct bt_ctf_clock_class
*clock
, int *set
)
4504 if (entry_node
->type
!= NODE_CTF_EXPRESSION
) {
4505 _BT_LOGE_NODE(entry_node
,
4506 "Unexpected node type: node-type=%d",
4512 left
= concatenate_unary_strings(&entry_node
->u
.ctf_expression
.left
);
4514 _BT_LOGE_NODE(entry_node
, "Cannot concatenate unary strings.");
4519 if (!strcmp(left
, "name")) {
4522 if (_IS_SET(set
, _CLOCK_NAME_SET
)) {
4523 _BT_LOGE_DUP_ATTR(entry_node
, "name", "clock class");
4528 right
= concatenate_unary_strings(
4529 &entry_node
->u
.ctf_expression
.right
);
4531 _BT_LOGE_NODE(entry_node
,
4532 "Unexpected unary expression for clock class's `name` attribute.");
4537 ret
= bt_ctf_clock_class_set_name(clock
, right
);
4539 _BT_LOGE_NODE(entry_node
,
4540 "cannot set clock class's name");
4546 _SET(set
, _CLOCK_NAME_SET
);
4547 } else if (!strcmp(left
, "uuid")) {
4548 unsigned char uuid
[BABELTRACE_UUID_LEN
];
4550 if (_IS_SET(set
, _CLOCK_UUID_SET
)) {
4551 _BT_LOGE_DUP_ATTR(entry_node
, "uuid", "clock class");
4556 ret
= get_unary_uuid(&entry_node
->u
.ctf_expression
.right
, uuid
);
4558 _BT_LOGE_NODE(entry_node
,
4559 "Invalid clock class's `uuid` attribute.");
4563 ret
= bt_ctf_clock_class_set_uuid(clock
, uuid
);
4565 _BT_LOGE_NODE(entry_node
,
4566 "Cannot set clock class's UUID.");
4570 _SET(set
, _CLOCK_UUID_SET
);
4571 } else if (!strcmp(left
, "description")) {
4574 if (_IS_SET(set
, _CLOCK_DESCRIPTION_SET
)) {
4575 _BT_LOGE_DUP_ATTR(entry_node
, "description",
4581 right
= concatenate_unary_strings(
4582 &entry_node
->u
.ctf_expression
.right
);
4584 _BT_LOGE_NODE(entry_node
,
4585 "Unexpected unary expression for clock class's `description` attribute.");
4590 ret
= bt_ctf_clock_class_set_description(clock
, right
);
4592 _BT_LOGE_NODE(entry_node
,
4593 "Cannot set clock class's description.");
4599 _SET(set
, _CLOCK_DESCRIPTION_SET
);
4600 } else if (!strcmp(left
, "freq")) {
4603 if (_IS_SET(set
, _CLOCK_FREQ_SET
)) {
4604 _BT_LOGE_DUP_ATTR(entry_node
, "freq", "clock class");
4609 ret
= get_unary_unsigned(
4610 &entry_node
->u
.ctf_expression
.right
, &freq
);
4612 _BT_LOGE_NODE(entry_node
,
4613 "Unexpected unary expression for clock class's `freq` attribute.");
4618 ret
= bt_ctf_clock_class_set_frequency(clock
, freq
);
4620 _BT_LOGE_NODE(entry_node
,
4621 "Cannot set clock class's frequency.");
4625 _SET(set
, _CLOCK_FREQ_SET
);
4626 } else if (!strcmp(left
, "precision")) {
4629 if (_IS_SET(set
, _CLOCK_PRECISION_SET
)) {
4630 _BT_LOGE_DUP_ATTR(entry_node
, "precision",
4636 ret
= get_unary_unsigned(
4637 &entry_node
->u
.ctf_expression
.right
, &precision
);
4639 _BT_LOGE_NODE(entry_node
,
4640 "Unexpected unary expression for clock class's `precision` attribute.");
4645 ret
= bt_ctf_clock_class_set_precision(clock
, precision
);
4647 _BT_LOGE_NODE(entry_node
,
4648 "Cannot set clock class's precision.");
4652 _SET(set
, _CLOCK_PRECISION_SET
);
4653 } else if (!strcmp(left
, "offset_s")) {
4656 if (_IS_SET(set
, _CLOCK_OFFSET_S_SET
)) {
4657 _BT_LOGE_DUP_ATTR(entry_node
, "offset_s",
4663 ret
= get_unary_unsigned(
4664 &entry_node
->u
.ctf_expression
.right
, &offset_s
);
4666 _BT_LOGE_NODE(entry_node
,
4667 "Unexpected unary expression for clock class's `offset_s` attribute.");
4672 ret
= bt_ctf_clock_class_set_offset_s(clock
, offset_s
);
4674 _BT_LOGE_NODE(entry_node
,
4675 "Cannot set clock class's offset in seconds.");
4679 _SET(set
, _CLOCK_OFFSET_S_SET
);
4680 } else if (!strcmp(left
, "offset")) {
4683 if (_IS_SET(set
, _CLOCK_OFFSET_SET
)) {
4684 _BT_LOGE_DUP_ATTR(entry_node
, "offset", "clock class");
4689 ret
= get_unary_unsigned(
4690 &entry_node
->u
.ctf_expression
.right
, &offset
);
4692 _BT_LOGE_NODE(entry_node
,
4693 "Unexpected unary expression for clock class's `offset` attribute.");
4698 ret
= bt_ctf_clock_class_set_offset_cycles(clock
, offset
);
4700 _BT_LOGE_NODE(entry_node
,
4701 "Cannot set clock class's offset in cycles.");
4705 _SET(set
, _CLOCK_OFFSET_SET
);
4706 } else if (!strcmp(left
, "absolute")) {
4707 struct ctf_node
*right
;
4709 if (_IS_SET(set
, _CLOCK_ABSOLUTE_SET
)) {
4710 _BT_LOGE_DUP_ATTR(entry_node
, "absolute",
4716 right
= _BT_LIST_FIRST_ENTRY(
4717 &entry_node
->u
.ctf_expression
.right
,
4718 struct ctf_node
, siblings
);
4719 ret
= get_boolean(right
);
4721 _BT_LOGE_NODE(entry_node
,
4722 "Unexpected unary expression for clock class's `absolute` attribute.");
4727 ret
= bt_ctf_clock_class_set_is_absolute(clock
, ret
);
4729 _BT_LOGE_NODE(entry_node
,
4730 "Cannot set clock class's absolute flag.");
4734 _SET(set
, _CLOCK_ABSOLUTE_SET
);
4736 _BT_LOGW_NODE(entry_node
,
4737 "Unknown attribute in clock class: attr-name=\"%s\"",
4753 int64_t cycles_from_ns(uint64_t frequency
, int64_t ns
)
4758 if (frequency
== 1000000000ULL) {
4761 cycles
= (uint64_t) (((double) ns
* (double) frequency
) / 1e9
);
4768 int apply_clock_class_offset(struct ctx
*ctx
, struct bt_ctf_clock_class
*clock
)
4772 int64_t offset_cycles
;
4774 freq
= bt_ctf_clock_class_get_frequency(clock
);
4775 if (freq
== -1ULL) {
4776 BT_LOGE_STR("Cannot get clock class's frequency.");
4781 ret
= bt_ctf_clock_class_get_offset_cycles(clock
, &offset_cycles
);
4783 BT_LOGE_STR("Cannot get clock class's offset in cycles.");
4788 offset_cycles
+= cycles_from_ns(freq
, ctx
->clock_class_offset_ns
);
4789 ret
= bt_ctf_clock_class_set_offset_cycles(clock
, offset_cycles
);
4796 int visit_clock_decl(struct ctx
*ctx
, struct ctf_node
*clock_node
)
4800 struct bt_ctf_clock_class
*clock
;
4801 struct ctf_node
*entry_node
;
4802 struct bt_list_head
*decl_list
= &clock_node
->u
.clock
.declaration_list
;
4803 const char *clock_class_name
;
4805 if (clock_node
->visited
) {
4809 clock_node
->visited
= TRUE
;
4810 clock
= bt_ctf_clock_class_create(NULL
);
4812 _BT_LOGE_NODE(clock_node
,
4813 "Cannot create default clock class.");
4818 bt_list_for_each_entry(entry_node
, decl_list
, siblings
) {
4819 ret
= visit_clock_decl_entry(ctx
, entry_node
, clock
, &set
);
4821 _BT_LOGE_NODE(entry_node
,
4822 "Cannot visit clock class's entry: ret=%d",
4828 if (!_IS_SET(&set
, _CLOCK_NAME_SET
)) {
4829 _BT_LOGE_NODE(clock_node
,
4830 "Missing `name` attribute in clock class.");
4835 clock_class_name
= bt_ctf_clock_class_get_name(clock
);
4836 assert(clock_class_name
);
4837 if (ctx
->is_lttng
&& strcmp(clock_class_name
, "monotonic") == 0) {
4839 * Old versions of LTTng forgot to set its clock class
4840 * as absolute, even if it is. This is important because
4841 * it's a condition to be able to sort notifications
4842 * from different sources.
4844 ret
= bt_ctf_clock_class_set_is_absolute(clock
, 1);
4846 _BT_LOGE_NODE(clock_node
,
4847 "Cannot set clock class's absolute flag.");
4852 ret
= apply_clock_class_offset(ctx
, clock
);
4854 _BT_LOGE_NODE(clock_node
,
4855 "Cannot apply clock class's custom offset.");
4859 ret
= bt_ctf_trace_add_clock_class(ctx
->trace
, clock
);
4861 _BT_LOGE_NODE(clock_node
,
4862 "Cannot add clock class to trace.");
4873 int visit_root_decl(struct ctx
*ctx
, struct ctf_node
*root_decl_node
)
4877 if (root_decl_node
->visited
) {
4881 root_decl_node
->visited
= TRUE
;
4883 switch (root_decl_node
->type
) {
4885 ret
= visit_typedef(ctx
,
4886 root_decl_node
->u
._typedef
.type_specifier_list
,
4887 &root_decl_node
->u
._typedef
.type_declarators
);
4889 _BT_LOGE_NODE(root_decl_node
,
4890 "Cannot add type definition found in root scope.");
4894 case NODE_TYPEALIAS
:
4895 ret
= visit_typealias(ctx
, root_decl_node
->u
.typealias
.target
,
4896 root_decl_node
->u
.typealias
.alias
);
4898 _BT_LOGE_NODE(root_decl_node
,
4899 "Cannot add type alias found in root scope.");
4903 case NODE_TYPE_SPECIFIER_LIST
:
4905 _BT_CTF_FIELD_TYPE_INIT(decl
);
4908 * Just add the type specifier to the root
4909 * declaration scope. Put local reference.
4911 ret
= visit_type_specifier_list(ctx
, root_decl_node
, &decl
);
4913 _BT_LOGE_NODE(root_decl_node
,
4914 "Cannot visit root scope's field type: "
4924 _BT_LOGE_NODE(root_decl_node
,
4925 "Unexpected node type: node-type=%d",
4926 root_decl_node
->type
);
4936 int set_trace_name(struct ctx
*ctx
)
4940 struct bt_value
*value
= NULL
;
4942 assert(bt_ctf_trace_get_stream_class_count(ctx
->trace
) == 0);
4943 name
= g_string_new(NULL
);
4945 BT_LOGE_STR("Failed to allocate a GString.");
4951 * Check if we have a trace environment string value named `hostname`.
4952 * If so, use it as the trace name's prefix.
4954 value
= bt_ctf_trace_get_environment_field_value_by_name(ctx
->trace
,
4956 if (bt_value_is_string(value
)) {
4957 const char *hostname
;
4959 ret
= bt_value_string_get(value
, &hostname
);
4961 g_string_append(name
, hostname
);
4963 if (ctx
->trace_name_suffix
) {
4964 g_string_append_c(name
, G_DIR_SEPARATOR
);
4968 if (ctx
->trace_name_suffix
) {
4969 g_string_append(name
, ctx
->trace_name_suffix
);
4972 ret
= bt_ctf_trace_set_name(ctx
->trace
, name
->str
);
4974 BT_LOGE("Cannot set trace's name: name=\"%s\"", name
->str
);
4987 g_string_free(name
, TRUE
);
4994 int move_ctx_stream_classes_to_trace(struct ctx
*ctx
)
4997 GHashTableIter iter
;
4998 gpointer key
, stream_class
;
5000 if (g_hash_table_size(ctx
->stream_classes
) > 0 &&
5001 bt_ctf_trace_get_stream_class_count(ctx
->trace
) == 0) {
5003 * We're about to add the first stream class to the
5004 * trace. This will freeze the trace, and after this
5005 * we cannot set the name anymore. At this point,
5006 * set the trace name.
5008 ret
= set_trace_name(ctx
);
5010 BT_LOGE_STR("Cannot set trace's name.");
5015 g_hash_table_iter_init(&iter
, ctx
->stream_classes
);
5017 while (g_hash_table_iter_next(&iter
, &key
, &stream_class
)) {
5018 ret
= bt_ctf_trace_add_stream_class(ctx
->trace
,
5021 int64_t id
= bt_ctf_stream_class_get_id(stream_class
);
5022 BT_LOGE("Cannot add stream class to trace: id=%" PRId64
,
5028 g_hash_table_remove_all(ctx
->stream_classes
);
5035 struct ctf_visitor_generate_ir
*ctf_visitor_generate_ir_create(
5036 int64_t clock_class_offset_ns
, const char *name
)
5039 struct ctx
*ctx
= NULL
;
5040 struct bt_ctf_trace
*trace
;
5042 trace
= bt_ctf_trace_create();
5044 BT_LOGE_STR("Cannot create empty trace.");
5048 /* Set packet header to NULL to override the default one */
5049 ret
= bt_ctf_trace_set_packet_header_type(trace
, NULL
);
5051 BT_LOGE_STR("Cannot reset initial trace's packet header field type.");
5055 /* Create visitor's context */
5056 ctx
= ctx_create(trace
, clock_class_offset_ns
, name
);
5058 BT_LOGE_STR("Cannot create visitor's context.");
5071 return (void *) ctx
;
5075 void ctf_visitor_generate_ir_destroy(struct ctf_visitor_generate_ir
*visitor
)
5077 ctx_destroy((void *) visitor
);
5081 struct bt_ctf_trace
*ctf_visitor_generate_ir_get_trace(
5082 struct ctf_visitor_generate_ir
*visitor
)
5084 struct ctx
*ctx
= (void *) visitor
;
5088 return bt_get(ctx
->trace
);
5092 int ctf_visitor_generate_ir_visit_node(struct ctf_visitor_generate_ir
*visitor
,
5093 struct ctf_node
*node
)
5096 struct ctx
*ctx
= (void *) visitor
;
5098 BT_LOGI_STR("Visiting metadata's AST to generate CTF IR objects.");
5100 switch (node
->type
) {
5103 struct ctf_node
*iter
;
5104 int got_trace_decl
= FALSE
;
5107 * The first thing we need is the native byte order of
5108 * the trace block, because early type aliases can have
5109 * a `byte_order` attribute set to `native`. If we don't
5110 * have the native byte order yet, and we don't have any
5111 * trace block yet, then fail with EINCOMPLETE.
5113 if (ctx
->trace_bo
== BT_CTF_BYTE_ORDER_NATIVE
) {
5114 bt_list_for_each_entry(iter
, &node
->u
.root
.trace
, siblings
) {
5115 if (got_trace_decl
) {
5117 "Duplicate trace (`trace` block).");
5122 ret
= set_trace_byte_order(ctx
, iter
);
5125 "Cannot set trace's native byte order: "
5130 got_trace_decl
= TRUE
;
5133 if (!got_trace_decl
) {
5134 BT_LOGD_STR("Incomplete AST: need trace (`trace` block).");
5140 assert(ctx
->trace_bo
== BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
||
5141 ctx
->trace_bo
== BT_CTF_BYTE_ORDER_BIG_ENDIAN
);
5142 assert(ctx
->current_scope
&&
5143 ctx
->current_scope
->parent_scope
== NULL
);
5146 bt_list_for_each_entry(iter
, &node
->u
.root
.env
, siblings
) {
5147 ret
= visit_env(ctx
, iter
);
5150 "Cannot visit trace's environment (`env` block) entry: "
5156 assert(ctx
->current_scope
&&
5157 ctx
->current_scope
->parent_scope
== NULL
);
5160 * Visit clock blocks.
5162 bt_list_for_each_entry(iter
, &node
->u
.root
.clock
, siblings
) {
5163 ret
= visit_clock_decl(ctx
, iter
);
5166 "Cannot visit clock class: ret=%d",
5172 assert(ctx
->current_scope
&&
5173 ctx
->current_scope
->parent_scope
== NULL
);
5176 * Visit root declarations next, as they can be used by any
5179 bt_list_for_each_entry(iter
, &node
->u
.root
.declaration_list
,
5181 ret
= visit_root_decl(ctx
, iter
);
5184 "Cannot visit root entry: ret=%d",
5190 assert(ctx
->current_scope
&&
5191 ctx
->current_scope
->parent_scope
== NULL
);
5193 /* Callsite blocks are not supported */
5194 bt_list_for_each_entry(iter
, &node
->u
.root
.callsite
, siblings
) {
5196 "\"callsite\" blocks are not supported as of this version.");
5199 assert(ctx
->current_scope
&&
5200 ctx
->current_scope
->parent_scope
== NULL
);
5203 bt_list_for_each_entry(iter
, &node
->u
.root
.trace
, siblings
) {
5204 ret
= visit_trace_decl(ctx
, iter
);
5207 "Cannot visit trace (`trace` block): "
5213 assert(ctx
->current_scope
&&
5214 ctx
->current_scope
->parent_scope
== NULL
);
5217 bt_list_for_each_entry(iter
, &node
->u
.root
.stream
, siblings
) {
5218 ret
= visit_stream_decl(ctx
, iter
);
5221 "Cannot visit stream class: ret=%d",
5227 assert(ctx
->current_scope
&&
5228 ctx
->current_scope
->parent_scope
== NULL
);
5231 bt_list_for_each_entry(iter
, &node
->u
.root
.event
, siblings
) {
5232 ret
= visit_event_decl(ctx
, iter
);
5235 "Cannot visit event class: ret=%d",
5241 assert(ctx
->current_scope
&&
5242 ctx
->current_scope
->parent_scope
== NULL
);
5247 "Unexpected node type: node-type=%d",
5253 /* Move decoded stream classes to trace, if any */
5254 ret
= move_ctx_stream_classes_to_trace(ctx
);
5256 BT_LOGE("Cannot move stream classes to trace: ret=%d", ret
);