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
40 #include <babeltrace/compat/uuid-internal.h>
41 #include <babeltrace/endian-internal.h>
42 #include <babeltrace/ref.h>
43 #include <babeltrace/ctf-ir/trace.h>
44 #include <babeltrace/ctf-ir/stream-class.h>
45 #include <babeltrace/ctf-ir/event.h>
46 #include <babeltrace/ctf-ir/event-class.h>
47 #include <babeltrace/ctf-ir/field-types.h>
48 #include <babeltrace/ctf-ir/field-types-internal.h>
49 #include <babeltrace/ctf-ir/clock-class.h>
55 #define BT_LOG_TAG "PLUGIN-CTF-METADATA-VISITOR-GENERATE-IR"
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(_attr, _entity) \
158 BT_LOGE("[error] %s: duplicate attribute \"" \
159 _attr "\" in " _entity "\n", __func__); \
163 * Declaration scope of a visitor context. This represents a TSDL
164 * lexical scope, so that aliases and named structures, variants,
165 * and enumerations may be registered and looked up hierarchically.
167 struct ctx_decl_scope
{
169 * Alias name to field type.
171 * GQuark -> struct bt_ctf_field_type *
173 GHashTable
*decl_map
;
175 /* Parent scope; NULL if this is the root declaration scope */
176 struct ctx_decl_scope
*parent_scope
;
180 * Visitor context (private).
183 /* Trace being filled (owned by this) */
184 struct bt_ctf_trace
*trace
;
186 /* Current declaration scope (top of the stack) */
187 struct ctx_decl_scope
*current_scope
;
189 /* 1 if trace declaration is visited */
190 int is_trace_visited
;
192 /* 1 if this is an LTTng trace */
195 /* Offset (ns) to apply to clock classes on creation */
196 int64_t clock_class_offset_ns
;
198 /* Eventual name suffix of the trace to set */
199 char *trace_name_suffix
;
201 /* Trace attributes */
202 enum bt_ctf_byte_order trace_bo
;
203 uint64_t trace_major
;
204 uint64_t trace_minor
;
205 unsigned char trace_uuid
[BABELTRACE_UUID_LEN
];
208 * Stream IDs to stream classes.
210 * int64_t -> struct bt_ctf_stream_class *
212 GHashTable
*stream_classes
;
218 struct ctf_visitor_generate_ir
{ };
221 const char *loglevel_str
[] = {
222 [ LOGLEVEL_EMERG
] = "TRACE_EMERG",
223 [ LOGLEVEL_ALERT
] = "TRACE_ALERT",
224 [ LOGLEVEL_CRIT
] = "TRACE_CRIT",
225 [ LOGLEVEL_ERR
] = "TRACE_ERR",
226 [ LOGLEVEL_WARNING
] = "TRACE_WARNING",
227 [ LOGLEVEL_NOTICE
] = "TRACE_NOTICE",
228 [ LOGLEVEL_INFO
] = "TRACE_INFO",
229 [ LOGLEVEL_DEBUG_SYSTEM
] = "TRACE_DEBUG_SYSTEM",
230 [ LOGLEVEL_DEBUG_PROGRAM
] = "TRACE_DEBUG_PROGRAM",
231 [ LOGLEVEL_DEBUG_PROCESS
] = "TRACE_DEBUG_PROCESS",
232 [ LOGLEVEL_DEBUG_MODULE
] = "TRACE_DEBUG_MODULE",
233 [ LOGLEVEL_DEBUG_UNIT
] = "TRACE_DEBUG_UNIT",
234 [ LOGLEVEL_DEBUG_FUNCTION
] = "TRACE_DEBUG_FUNCTION",
235 [ LOGLEVEL_DEBUG_LINE
] = "TRACE_DEBUG_LINE",
236 [ LOGLEVEL_DEBUG
] = "TRACE_DEBUG",
240 const char *print_loglevel(int64_t value
)
245 if (value
>= _NR_LOGLEVELS
) {
246 return "<<UNKNOWN>>";
248 return loglevel_str
[value
];
252 * Creates a new declaration scope.
254 * @param par_scope Parent scope (NULL if creating a root scope)
255 * @returns New declaration scope, or NULL on error
258 struct ctx_decl_scope
*ctx_decl_scope_create(struct ctx_decl_scope
*par_scope
)
260 struct ctx_decl_scope
*scope
;
262 scope
= g_new(struct ctx_decl_scope
, 1);
267 scope
->decl_map
= g_hash_table_new_full(g_direct_hash
, g_direct_equal
,
268 NULL
, (GDestroyNotify
) bt_ctf_field_type_put
);
269 scope
->parent_scope
= par_scope
;
276 * Destroys a declaration scope.
278 * This function does not destroy the parent scope.
280 * @param scope Scope to destroy
283 void ctx_decl_scope_destroy(struct ctx_decl_scope
*scope
)
289 g_hash_table_destroy(scope
->decl_map
);
297 * Returns the GQuark of a prefixed alias.
299 * @param prefix Prefix character
301 * @returns Associated GQuark, or 0 on error
304 GQuark
get_prefixed_named_quark(char prefix
, const char *name
)
310 /* Prefix character + original string + '\0' */
311 char *prname
= g_new(char, strlen(name
) + 2);
316 sprintf(prname
, "%c%s", prefix
, name
);
317 qname
= g_quark_from_string(prname
);
325 * Looks up a prefixed type alias within a declaration scope.
327 * @param scope Declaration scope
328 * @param prefix Prefix character
329 * @param name Alias name
330 * @param level Number of levels to dig (-1 means infinite)
331 * @returns Declaration, or NULL if not found
334 struct bt_ctf_field_type
*ctx_decl_scope_lookup_prefix_alias(
335 struct ctx_decl_scope
*scope
, char prefix
,
336 const char *name
, int levels
)
340 _BT_CTF_FIELD_TYPE_INIT(decl
);
341 struct ctx_decl_scope
*cur_scope
= scope
;
345 qname
= get_prefixed_named_quark(prefix
, name
);
354 while (cur_scope
&& cur_levels
< levels
) {
355 decl
= g_hash_table_lookup(cur_scope
->decl_map
,
356 (gconstpointer
) GUINT_TO_POINTER(qname
));
358 /* Caller's reference */
363 cur_scope
= cur_scope
->parent_scope
;
374 * Looks up a type alias within a declaration scope.
376 * @param scope Declaration scope
377 * @param name Alias name
378 * @param level Number of levels to dig (-1 means infinite)
379 * @returns Declaration, or NULL if not found
382 struct bt_ctf_field_type
*ctx_decl_scope_lookup_alias(
383 struct ctx_decl_scope
*scope
, const char *name
, int levels
)
385 return ctx_decl_scope_lookup_prefix_alias(scope
, _PREFIX_ALIAS
,
390 * Looks up an enumeration within a declaration scope.
392 * @param scope Declaration scope
393 * @param name Enumeration name
394 * @param level Number of levels to dig (-1 means infinite)
395 * @returns Declaration, or NULL if not found
398 struct bt_ctf_field_type
*ctx_decl_scope_lookup_enum(
399 struct ctx_decl_scope
*scope
, const char *name
, int levels
)
401 return ctx_decl_scope_lookup_prefix_alias(scope
, _PREFIX_ENUM
,
406 * Looks up a structure within a declaration scope.
408 * @param scope Declaration scope
409 * @param name Structure name
410 * @param level Number of levels to dig (-1 means infinite)
411 * @returns Declaration, or NULL if not found
414 struct bt_ctf_field_type
*ctx_decl_scope_lookup_struct(
415 struct ctx_decl_scope
*scope
, const char *name
, int levels
)
417 return ctx_decl_scope_lookup_prefix_alias(scope
, _PREFIX_STRUCT
,
422 * Looks up a variant within a declaration scope.
424 * @param scope Declaration scope
425 * @param name Variant name
426 * @param level Number of levels to dig (-1 means infinite)
427 * @returns Declaration, or NULL if not found
430 struct bt_ctf_field_type
*ctx_decl_scope_lookup_variant(
431 struct ctx_decl_scope
*scope
, const char *name
, int levels
)
433 return ctx_decl_scope_lookup_prefix_alias(scope
, _PREFIX_VARIANT
,
438 * Registers a prefixed type alias within a declaration scope.
440 * @param scope Declaration scope
441 * @param prefix Prefix character
442 * @param name Alias name (non-NULL)
443 * @param decl Declaration to register
444 * @returns 0 if registration went okay, negative value otherwise
447 int ctx_decl_scope_register_prefix_alias(struct ctx_decl_scope
*scope
,
448 char prefix
, const char *name
, struct bt_ctf_field_type
*decl
)
452 _BT_CTF_FIELD_TYPE_INIT(edecl
);
457 qname
= get_prefixed_named_quark(prefix
, name
);
463 /* Make sure alias does not exist in local scope */
464 edecl
= ctx_decl_scope_lookup_prefix_alias(scope
, prefix
, name
, 1);
471 g_hash_table_insert(scope
->decl_map
,
472 GUINT_TO_POINTER(qname
), decl
);
474 /* Hash table's reference */
484 * Registers a type alias within a declaration scope.
486 * @param scope Declaration scope
487 * @param name Alias name (non-NULL)
488 * @param decl Declaration to register
489 * @returns 0 if registration went okay, negative value otherwise
492 int ctx_decl_scope_register_alias(struct ctx_decl_scope
*scope
,
493 const char *name
, struct bt_ctf_field_type
*decl
)
495 return ctx_decl_scope_register_prefix_alias(scope
, _PREFIX_ALIAS
,
500 * Registers an enumeration declaration within a declaration scope.
502 * @param scope Declaration scope
503 * @param name Enumeration name (non-NULL)
504 * @param decl Enumeration declaration to register
505 * @returns 0 if registration went okay, negative value otherwise
508 int ctx_decl_scope_register_enum(struct ctx_decl_scope
*scope
,
509 const char *name
, struct bt_ctf_field_type
*decl
)
511 return ctx_decl_scope_register_prefix_alias(scope
, _PREFIX_ENUM
,
516 * Registers a structure declaration within a declaration scope.
518 * @param scope Declaration scope
519 * @param name Structure name (non-NULL)
520 * @param decl Structure declaration to register
521 * @returns 0 if registration went okay, negative value otherwise
524 int ctx_decl_scope_register_struct(struct ctx_decl_scope
*scope
,
525 const char *name
, struct bt_ctf_field_type
*decl
)
527 return ctx_decl_scope_register_prefix_alias(scope
, _PREFIX_STRUCT
,
532 * Registers a variant declaration within a declaration scope.
534 * @param scope Declaration scope
535 * @param name Variant name (non-NULL)
536 * @param decl Variant declaration to register
537 * @returns 0 if registration went okay, negative value otherwise
540 int ctx_decl_scope_register_variant(struct ctx_decl_scope
*scope
,
541 const char *name
, struct bt_ctf_field_type
*decl
)
543 return ctx_decl_scope_register_prefix_alias(scope
, _PREFIX_VARIANT
,
548 * Destroys a visitor context.
550 * @param ctx Visitor context to destroy
553 void ctx_destroy(struct ctx
*ctx
)
555 struct ctx_decl_scope
*scope
;
557 * Destroy all scopes, from current one to the root scope.
564 scope
= ctx
->current_scope
;
567 struct ctx_decl_scope
*parent_scope
= scope
->parent_scope
;
569 ctx_decl_scope_destroy(scope
);
570 scope
= parent_scope
;
575 if (ctx
->stream_classes
) {
576 g_hash_table_destroy(ctx
->stream_classes
);
579 free(ctx
->trace_name_suffix
);
587 * Creates a new visitor context.
589 * @param trace Associated trace
590 * @returns New visitor context, or NULL on error
593 struct ctx
*ctx_create(struct bt_ctf_trace
*trace
,
594 int64_t clock_class_offset_ns
, const char *trace_name_suffix
)
596 struct ctx
*ctx
= NULL
;
597 struct ctx_decl_scope
*scope
= NULL
;
599 ctx
= g_new0(struct ctx
, 1);
604 /* Root declaration scope */
605 scope
= ctx_decl_scope_create(NULL
);
610 ctx
->stream_classes
= g_hash_table_new_full(g_direct_hash
,
611 g_direct_equal
, NULL
, (GDestroyNotify
) bt_put
);
612 if (!ctx
->stream_classes
) {
616 if (trace_name_suffix
) {
617 ctx
->trace_name_suffix
= strdup(trace_name_suffix
);
618 if (!ctx
->trace_name_suffix
) {
624 ctx
->current_scope
= scope
;
626 ctx
->trace_bo
= BT_CTF_BYTE_ORDER_NATIVE
;
627 ctx
->clock_class_offset_ns
= clock_class_offset_ns
;
632 ctx_decl_scope_destroy(scope
);
637 * Pushes a new declaration scope on top of a visitor context's
638 * declaration scope stack.
640 * @param ctx Visitor context
641 * @returns 0 on success, or a negative value on error
644 int ctx_push_scope(struct ctx
*ctx
)
647 struct ctx_decl_scope
*new_scope
;
650 new_scope
= ctx_decl_scope_create(ctx
->current_scope
);
656 ctx
->current_scope
= new_scope
;
663 void ctx_pop_scope(struct ctx
*ctx
)
665 struct ctx_decl_scope
*parent_scope
= NULL
;
669 if (!ctx
->current_scope
) {
673 parent_scope
= ctx
->current_scope
->parent_scope
;
674 ctx_decl_scope_destroy(ctx
->current_scope
);
675 ctx
->current_scope
= parent_scope
;
682 int visit_type_specifier_list(struct ctx
*ctx
, struct ctf_node
*ts_list
,
683 struct bt_ctf_field_type
**decl
);
686 int is_unary_string(struct bt_list_head
*head
)
689 struct ctf_node
*node
;
691 bt_list_for_each_entry(node
, head
, siblings
) {
692 if (node
->type
!= NODE_UNARY_EXPRESSION
) {
696 if (node
->u
.unary_expression
.type
!= UNARY_STRING
) {
705 char *concatenate_unary_strings(struct bt_list_head
*head
)
709 struct ctf_node
*node
;
711 str
= g_string_new(NULL
);
713 bt_list_for_each_entry(node
, head
, siblings
) {
717 node
->type
!= NODE_UNARY_EXPRESSION
||
718 node
->u
.unary_expression
.type
!= UNARY_STRING
||
721 node
->u
.unary_expression
.link
!=
729 switch (node
->u
.unary_expression
.link
) {
731 g_string_append(str
, ".");
733 case UNARY_ARROWLINK
:
734 g_string_append(str
, "->");
736 case UNARY_DOTDOTDOT
:
737 g_string_append(str
, "...");
743 src_string
= node
->u
.unary_expression
.u
.string
;
744 g_string_append(str
, src_string
);
748 /* Destroys the container, returns the underlying string */
749 return g_string_free(str
, FALSE
);
752 /* This always returns NULL */
753 return g_string_free(str
, TRUE
);
757 const char *get_map_clock_name_value(struct bt_list_head
*head
)
760 struct ctf_node
*node
;
761 const char *name
= NULL
;
763 bt_list_for_each_entry(node
, head
, siblings
) {
765 int uexpr_type
= node
->u
.unary_expression
.type
;
766 int uexpr_link
= node
->u
.unary_expression
.link
;
767 int cond
= node
->type
!= NODE_UNARY_EXPRESSION
||
768 uexpr_type
!= UNARY_STRING
||
769 !((uexpr_link
!= UNARY_LINK_UNKNOWN
) ^ (i
== 0));
774 /* Needs to be chained with . */
775 switch (node
->u
.unary_expression
.link
) {
778 case UNARY_ARROWLINK
:
779 case UNARY_DOTDOTDOT
:
785 src_string
= node
->u
.unary_expression
.u
.string
;
789 if (strcmp("clock", src_string
)) {
797 if (strcmp("value", src_string
)) {
802 /* Extra identifier, unknown */
816 int is_unary_unsigned(struct bt_list_head
*head
)
819 struct ctf_node
*node
;
821 bt_list_for_each_entry(node
, head
, siblings
) {
822 if (node
->type
!= NODE_UNARY_EXPRESSION
) {
826 if (node
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
835 int get_unary_unsigned(struct bt_list_head
*head
, uint64_t *value
)
839 struct ctf_node
*node
;
841 if (bt_list_empty(head
)) {
846 bt_list_for_each_entry(node
, head
, siblings
) {
847 int uexpr_type
= node
->u
.unary_expression
.type
;
848 int uexpr_link
= node
->u
.unary_expression
.link
;
849 int cond
= node
->type
!= NODE_UNARY_EXPRESSION
||
850 uexpr_type
!= UNARY_UNSIGNED_CONSTANT
||
851 uexpr_link
!= UNARY_LINK_UNKNOWN
|| i
!= 0;
857 *value
= node
->u
.unary_expression
.u
.unsigned_constant
;
866 int is_unary_signed(struct bt_list_head
*head
)
869 struct ctf_node
*node
;
871 bt_list_for_each_entry(node
, head
, siblings
) {
872 if (node
->type
!= NODE_UNARY_EXPRESSION
) {
876 if (node
->u
.unary_expression
.type
!= UNARY_SIGNED_CONSTANT
) {
885 int get_unary_signed(struct bt_list_head
*head
, int64_t *value
)
889 struct ctf_node
*node
;
891 bt_list_for_each_entry(node
, head
, siblings
) {
892 int uexpr_type
= node
->u
.unary_expression
.type
;
893 int uexpr_link
= node
->u
.unary_expression
.link
;
894 int cond
= node
->type
!= NODE_UNARY_EXPRESSION
||
895 (uexpr_type
!= UNARY_UNSIGNED_CONSTANT
) ||
896 (uexpr_type
!= UNARY_UNSIGNED_CONSTANT
&&
897 uexpr_type
!= UNARY_SIGNED_CONSTANT
) ||
898 uexpr_link
!= UNARY_LINK_UNKNOWN
|| i
!= 0;
904 switch (node
->u
.unary_expression
.type
) {
905 case UNARY_UNSIGNED_CONSTANT
:
907 node
->u
.unary_expression
.u
.unsigned_constant
;
909 case UNARY_SIGNED_CONSTANT
:
910 *value
= node
->u
.unary_expression
.u
.signed_constant
;
925 int get_unary_uuid(struct bt_list_head
*head
, unsigned char *uuid
)
929 struct ctf_node
*node
;
931 bt_list_for_each_entry(node
, head
, siblings
) {
932 int uexpr_type
= node
->u
.unary_expression
.type
;
933 int uexpr_link
= node
->u
.unary_expression
.link
;
934 const char *src_string
;
936 if (node
->type
!= NODE_UNARY_EXPRESSION
||
937 uexpr_type
!= UNARY_STRING
||
938 uexpr_link
!= UNARY_LINK_UNKNOWN
||
944 src_string
= node
->u
.unary_expression
.u
.string
;
945 ret
= bt_uuid_parse(src_string
, uuid
);
956 int get_boolean(struct ctf_node
*unary_expr
)
960 if (unary_expr
->type
!= NODE_UNARY_EXPRESSION
) {
961 BT_LOGE("expecting unary expression");
966 switch (unary_expr
->u
.unary_expression
.type
) {
967 case UNARY_UNSIGNED_CONSTANT
:
968 ret
= (unary_expr
->u
.unary_expression
.u
.unsigned_constant
!= 0);
970 case UNARY_SIGNED_CONSTANT
:
971 ret
= (unary_expr
->u
.unary_expression
.u
.signed_constant
!= 0);
975 const char *str
= unary_expr
->u
.unary_expression
.u
.string
;
977 if (!strcmp(str
, "true") || !strcmp(str
, "TRUE")) {
979 } else if (!strcmp(str
, "false") || !strcmp(str
, "FALSE")) {
982 BT_LOGE("unexpected string \"%s\"", str
);
989 BT_LOGE("unexpected unary expression type");
999 enum bt_ctf_byte_order
byte_order_from_unary_expr(struct ctf_node
*unary_expr
)
1002 enum bt_ctf_byte_order bo
= BT_CTF_BYTE_ORDER_UNKNOWN
;
1004 if (unary_expr
->u
.unary_expression
.type
!= UNARY_STRING
) {
1005 BT_LOGE("\"byte_order\" attribute: expecting string");
1009 str
= unary_expr
->u
.unary_expression
.u
.string
;
1011 if (!strcmp(str
, "be") || !strcmp(str
, "network")) {
1012 bo
= BT_CTF_BYTE_ORDER_BIG_ENDIAN
;
1013 } else if (!strcmp(str
, "le")) {
1014 bo
= BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
;
1015 } else if (!strcmp(str
, "native")) {
1016 bo
= BT_CTF_BYTE_ORDER_NATIVE
;
1018 BT_LOGE("unexpected \"byte_order\" attribute value \"%s\"; should be \"be\", \"le\", \"network\", or \"native\"",
1028 enum bt_ctf_byte_order
get_real_byte_order(struct ctx
*ctx
,
1029 struct ctf_node
*uexpr
)
1031 enum bt_ctf_byte_order bo
= byte_order_from_unary_expr(uexpr
);
1033 if (bo
== BT_CTF_BYTE_ORDER_NATIVE
) {
1034 bo
= bt_ctf_trace_get_native_byte_order(ctx
->trace
);
1041 int is_align_valid(uint64_t align
)
1043 return (align
!= 0) && !(align
& (align
- 1));
1047 int get_type_specifier_name(struct ctx
*ctx
, struct ctf_node
*type_specifier
,
1052 if (type_specifier
->type
!= NODE_TYPE_SPECIFIER
) {
1057 switch (type_specifier
->u
.type_specifier
.type
) {
1059 g_string_append(str
, "void");
1062 g_string_append(str
, "char");
1064 case TYPESPEC_SHORT
:
1065 g_string_append(str
, "short");
1068 g_string_append(str
, "int");
1071 g_string_append(str
, "long");
1073 case TYPESPEC_FLOAT
:
1074 g_string_append(str
, "float");
1076 case TYPESPEC_DOUBLE
:
1077 g_string_append(str
, "double");
1079 case TYPESPEC_SIGNED
:
1080 g_string_append(str
, "signed");
1082 case TYPESPEC_UNSIGNED
:
1083 g_string_append(str
, "unsigned");
1086 g_string_append(str
, "bool");
1088 case TYPESPEC_COMPLEX
:
1089 g_string_append(str
, "_Complex");
1091 case TYPESPEC_IMAGINARY
:
1092 g_string_append(str
, "_Imaginary");
1094 case TYPESPEC_CONST
:
1095 g_string_append(str
, "const");
1097 case TYPESPEC_ID_TYPE
:
1098 if (type_specifier
->u
.type_specifier
.id_type
) {
1099 g_string_append(str
,
1100 type_specifier
->u
.type_specifier
.id_type
);
1103 case TYPESPEC_STRUCT
:
1105 struct ctf_node
*node
= type_specifier
->u
.type_specifier
.node
;
1107 if (!node
->u
._struct
.name
) {
1108 BT_LOGE("unexpected empty structure name");
1113 g_string_append(str
, "struct ");
1114 g_string_append(str
, node
->u
._struct
.name
);
1117 case TYPESPEC_VARIANT
:
1119 struct ctf_node
*node
= type_specifier
->u
.type_specifier
.node
;
1121 if (!node
->u
.variant
.name
) {
1122 BT_LOGE("unexpected empty variant name");
1127 g_string_append(str
, "variant ");
1128 g_string_append(str
, node
->u
.variant
.name
);
1133 struct ctf_node
*node
= type_specifier
->u
.type_specifier
.node
;
1135 if (!node
->u
._enum
.enum_id
) {
1136 BT_LOGE("unexpected empty enum name");
1141 g_string_append(str
, "enum ");
1142 g_string_append(str
, node
->u
._enum
.enum_id
);
1145 case TYPESPEC_FLOATING_POINT
:
1146 case TYPESPEC_INTEGER
:
1147 case TYPESPEC_STRING
:
1149 BT_LOGE("unknown specifier");
1159 int get_type_specifier_list_name(struct ctx
*ctx
,
1160 struct ctf_node
*type_specifier_list
, GString
*str
)
1163 struct ctf_node
*iter
;
1164 int alias_item_nr
= 0;
1165 struct bt_list_head
*head
=
1166 &type_specifier_list
->u
.type_specifier_list
.head
;
1168 bt_list_for_each_entry(iter
, head
, siblings
) {
1169 if (alias_item_nr
!= 0) {
1170 g_string_append(str
, " ");
1174 ret
= get_type_specifier_name(ctx
, iter
, str
);
1185 GQuark
create_typealias_identifier(struct ctx
*ctx
,
1186 struct ctf_node
*type_specifier_list
,
1187 struct ctf_node
*node_type_declarator
)
1193 struct ctf_node
*iter
;
1194 struct bt_list_head
*pointers
=
1195 &node_type_declarator
->u
.type_declarator
.pointers
;
1197 str
= g_string_new("");
1198 ret
= get_type_specifier_list_name(ctx
, type_specifier_list
, str
);
1200 g_string_free(str
, TRUE
);
1204 bt_list_for_each_entry(iter
, pointers
, siblings
) {
1205 g_string_append(str
, " *");
1207 if (iter
->u
.pointer
.const_qualifier
) {
1208 g_string_append(str
, " const");
1212 str_c
= g_string_free(str
, FALSE
);
1213 qalias
= g_quark_from_string(str_c
);
1221 int visit_type_declarator(struct ctx
*ctx
, struct ctf_node
*type_specifier_list
,
1222 GQuark
*field_name
, struct ctf_node
*node_type_declarator
,
1223 struct bt_ctf_field_type
**field_decl
,
1224 struct bt_ctf_field_type
*nested_decl
)
1227 * During this whole function, nested_decl is always OURS,
1228 * whereas field_decl is an output which we create, but
1229 * belongs to the caller (it is moved).
1235 /* Validate type declarator node */
1236 if (node_type_declarator
) {
1237 if (node_type_declarator
->u
.type_declarator
.type
==
1243 /* TODO: GCC bitfields not supported yet */
1244 if (node_type_declarator
->u
.type_declarator
.bitfield_len
!=
1246 BT_LOGE("GCC bitfields are not supported as of this version");
1252 /* Find the right nested declaration if not provided */
1254 struct bt_list_head
*pointers
=
1255 &node_type_declarator
->u
.type_declarator
.pointers
;
1257 if (node_type_declarator
&& !bt_list_empty(pointers
)) {
1259 _BT_CTF_FIELD_TYPE_INIT(nested_decl_copy
);
1262 * If we have a pointer declarator, it HAS to
1263 * be present in the typealiases (else fail).
1265 qalias
= create_typealias_identifier(ctx
,
1266 type_specifier_list
, node_type_declarator
);
1268 ctx_decl_scope_lookup_alias(ctx
->current_scope
,
1269 g_quark_to_string(qalias
), -1);
1271 BT_LOGE("cannot find typealias \"%s\"",
1272 g_quark_to_string(qalias
));
1277 /* Make a copy of it */
1278 nested_decl_copy
= bt_ctf_field_type_copy(nested_decl
);
1279 BT_PUT(nested_decl
);
1280 if (!nested_decl_copy
) {
1281 BT_LOGE("cannot copy nested field type");
1286 BT_MOVE(nested_decl
, nested_decl_copy
);
1288 /* Force integer's base to 16 since it's a pointer */
1289 if (bt_ctf_field_type_is_integer(nested_decl
)) {
1290 bt_ctf_field_type_integer_set_base(nested_decl
,
1291 BT_CTF_INTEGER_BASE_HEXADECIMAL
);
1294 ret
= visit_type_specifier_list(ctx
,
1295 type_specifier_list
, &nested_decl
);
1297 assert(!nested_decl
);
1303 assert(nested_decl
);
1305 if (!node_type_declarator
) {
1306 BT_MOVE(*field_decl
, nested_decl
);
1310 if (node_type_declarator
->u
.type_declarator
.type
== TYPEDEC_ID
) {
1311 if (node_type_declarator
->u
.type_declarator
.u
.id
) {
1313 node_type_declarator
->u
.type_declarator
.u
.id
;
1315 *field_name
= g_quark_from_string(id
);
1320 BT_MOVE(*field_decl
, nested_decl
);
1323 struct ctf_node
*first
;
1324 _BT_CTF_FIELD_TYPE_INIT(decl
);
1325 _BT_CTF_FIELD_TYPE_INIT(outer_field_decl
);
1326 struct bt_list_head
*length
=
1327 &node_type_declarator
->
1328 u
.type_declarator
.u
.nested
.length
;
1330 /* Create array/sequence, pass nested_decl as child */
1331 if (bt_list_empty(length
)) {
1332 BT_LOGE("expecting length field reference or value");
1337 first
= _BT_LIST_FIRST_ENTRY(length
, struct ctf_node
, siblings
);
1338 if (first
->type
!= NODE_UNARY_EXPRESSION
) {
1343 switch (first
->u
.unary_expression
.type
) {
1344 case UNARY_UNSIGNED_CONSTANT
:
1347 _BT_CTF_FIELD_TYPE_INIT(array_decl
);
1349 len
= first
->u
.unary_expression
.u
.unsigned_constant
;
1350 array_decl
= bt_ctf_field_type_array_create(nested_decl
,
1352 BT_PUT(nested_decl
);
1354 BT_LOGE("cannot create array field type");
1359 BT_MOVE(decl
, array_decl
);
1364 /* Lookup unsigned integer definition, create seq. */
1365 _BT_CTF_FIELD_TYPE_INIT(seq_decl
);
1366 char *length_name
= concatenate_unary_strings(length
);
1373 seq_decl
= bt_ctf_field_type_sequence_create(
1374 nested_decl
, length_name
);
1375 g_free(length_name
);
1376 BT_PUT(nested_decl
);
1378 BT_LOGE("cannot create sequence field type");
1383 BT_MOVE(decl
, seq_decl
);
1391 assert(!nested_decl
);
1393 assert(!*field_decl
);
1396 * At this point, we found the next nested declaration.
1397 * We currently own this (and lost the ownership of
1398 * nested_decl in the meantime). Pass this next
1399 * nested declaration as the content of the outer
1400 * container, MOVING its ownership.
1402 ret
= visit_type_declarator(ctx
, type_specifier_list
,
1404 node_type_declarator
->
1405 u
.type_declarator
.u
.nested
.type_declarator
,
1406 &outer_field_decl
, decl
);
1409 assert(!outer_field_decl
);
1414 assert(outer_field_decl
);
1415 BT_MOVE(*field_decl
, outer_field_decl
);
1419 BT_PUT(nested_decl
);
1420 assert(*field_decl
);
1425 BT_PUT(nested_decl
);
1426 BT_PUT(*field_decl
);
1432 int visit_struct_decl_field(struct ctx
*ctx
,
1433 struct bt_ctf_field_type
*struct_decl
,
1434 struct ctf_node
*type_specifier_list
,
1435 struct bt_list_head
*type_declarators
)
1438 struct ctf_node
*iter
;
1439 _BT_CTF_FIELD_TYPE_INIT(field_decl
);
1441 bt_list_for_each_entry(iter
, type_declarators
, siblings
) {
1444 const char *field_name
;
1445 _BT_CTF_FIELD_TYPE_INIT(efield_decl
);
1447 ret
= visit_type_declarator(ctx
, type_specifier_list
,
1448 &qfield_name
, iter
, &field_decl
, NULL
);
1450 assert(!field_decl
);
1451 BT_LOGE("cannot visit type declarator");
1456 field_name
= g_quark_to_string(qfield_name
);
1458 /* Check if field with same name already exists */
1460 bt_ctf_field_type_structure_get_field_type_by_name(
1461 struct_decl
, field_name
);
1463 BT_PUT(efield_decl
);
1464 BT_LOGE("duplicate field \"%s\" in structure",
1470 /* Add field to structure */
1471 ret
= bt_ctf_field_type_structure_add_field(struct_decl
,
1472 field_decl
, field_name
);
1475 BT_LOGE("cannot add field \"%s\" to structure",
1476 g_quark_to_string(qfield_name
));
1490 int visit_variant_decl_field(struct ctx
*ctx
,
1491 struct bt_ctf_field_type
*variant_decl
,
1492 struct ctf_node
*type_specifier_list
,
1493 struct bt_list_head
*type_declarators
)
1496 struct ctf_node
*iter
;
1497 _BT_CTF_FIELD_TYPE_INIT(field_decl
);
1499 bt_list_for_each_entry(iter
, type_declarators
, siblings
) {
1502 const char *field_name
;
1503 _BT_CTF_FIELD_TYPE_INIT(efield_decl
);
1505 ret
= visit_type_declarator(ctx
, type_specifier_list
,
1506 &qfield_name
, iter
, &field_decl
, NULL
);
1508 assert(!field_decl
);
1509 BT_LOGE("cannot visit type declarator");
1514 field_name
= g_quark_to_string(qfield_name
);
1516 /* Check if field with same name already exists */
1518 bt_ctf_field_type_variant_get_field_type_by_name(
1519 variant_decl
, field_name
);
1521 BT_PUT(efield_decl
);
1522 BT_LOGE("duplicate field \"%s\" in variant",
1528 /* Add field to structure */
1529 ret
= bt_ctf_field_type_variant_add_field(variant_decl
,
1530 field_decl
, field_name
);
1533 BT_LOGE("cannot add field \"%s\" to variant",
1534 g_quark_to_string(qfield_name
));
1548 int visit_typedef(struct ctx
*ctx
, struct ctf_node
*type_specifier_list
,
1549 struct bt_list_head
*type_declarators
)
1553 struct ctf_node
*iter
;
1554 _BT_CTF_FIELD_TYPE_INIT(type_decl
);
1556 bt_list_for_each_entry(iter
, type_declarators
, siblings
) {
1557 ret
= visit_type_declarator(ctx
, type_specifier_list
,
1558 &qidentifier
, iter
, &type_decl
, NULL
);
1560 BT_LOGE("cannot visit type declarator");
1565 /* Do not allow typedef and typealias of untagged variants */
1566 if (bt_ctf_field_type_is_variant(type_decl
)) {
1567 if (bt_ctf_field_type_variant_get_tag_name(type_decl
)) {
1568 BT_LOGE("typedef of untagged variant is not allowed");
1574 ret
= ctx_decl_scope_register_alias(ctx
->current_scope
,
1575 g_quark_to_string(qidentifier
), type_decl
);
1577 BT_LOGE("cannot register typedef \"%s\"",
1578 g_quark_to_string(qidentifier
));
1590 int visit_typealias(struct ctx
*ctx
, struct ctf_node
*target
,
1591 struct ctf_node
*alias
)
1595 struct ctf_node
*node
;
1596 GQuark qdummy_field_name
;
1597 _BT_CTF_FIELD_TYPE_INIT(type_decl
);
1599 /* Create target type declaration */
1600 if (bt_list_empty(&target
->u
.typealias_target
.type_declarators
)) {
1603 node
= _BT_LIST_FIRST_ENTRY(
1604 &target
->u
.typealias_target
.type_declarators
,
1605 struct ctf_node
, siblings
);
1608 ret
= visit_type_declarator(ctx
,
1609 target
->u
.typealias_target
.type_specifier_list
,
1610 &qdummy_field_name
, node
, &type_decl
, NULL
);
1613 BT_LOGE("cannot visit type declarator");
1617 /* Do not allow typedef and typealias of untagged variants */
1618 if (bt_ctf_field_type_is_variant(type_decl
)) {
1619 if (bt_ctf_field_type_variant_get_tag_name(type_decl
)) {
1620 BT_LOGE("typealias of untagged variant is not allowed");
1627 * The semantic validator does not check whether the target is
1628 * abstract or not (if it has an identifier). Check it here.
1630 if (qdummy_field_name
!= 0) {
1631 BT_LOGE("expecting empty identifier");
1636 /* Create alias identifier */
1637 node
= _BT_LIST_FIRST_ENTRY(&alias
->u
.typealias_alias
.type_declarators
,
1638 struct ctf_node
, siblings
);
1639 qalias
= create_typealias_identifier(ctx
,
1640 alias
->u
.typealias_alias
.type_specifier_list
, node
);
1641 ret
= ctx_decl_scope_register_alias(ctx
->current_scope
,
1642 g_quark_to_string(qalias
), type_decl
);
1644 BT_LOGE("cannot register typealias \"%s\"",
1645 g_quark_to_string(qalias
));
1656 int visit_struct_decl_entry(struct ctx
*ctx
, struct ctf_node
*entry_node
,
1657 struct bt_ctf_field_type
*struct_decl
)
1661 switch (entry_node
->type
) {
1663 ret
= visit_typedef(ctx
,
1664 entry_node
->u
._typedef
.type_specifier_list
,
1665 &entry_node
->u
._typedef
.type_declarators
);
1667 BT_LOGE("cannot add typedef in \"struct\" declaration");
1671 case NODE_TYPEALIAS
:
1672 ret
= visit_typealias(ctx
, entry_node
->u
.typealias
.target
,
1673 entry_node
->u
.typealias
.alias
);
1675 BT_LOGE("cannot add typealias in \"struct\" declaration");
1679 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
1681 ret
= visit_struct_decl_field(ctx
, struct_decl
,
1682 entry_node
->u
.struct_or_variant_declaration
.
1683 type_specifier_list
,
1684 &entry_node
->u
.struct_or_variant_declaration
.
1691 BT_LOGE("unexpected node type: %d", (int) entry_node
->type
);
1701 int visit_variant_decl_entry(struct ctx
*ctx
, struct ctf_node
*entry_node
,
1702 struct bt_ctf_field_type
*variant_decl
)
1706 switch (entry_node
->type
) {
1708 ret
= visit_typedef(ctx
,
1709 entry_node
->u
._typedef
.type_specifier_list
,
1710 &entry_node
->u
._typedef
.type_declarators
);
1713 "cannot add typedef in \"variant\" declaration");
1717 case NODE_TYPEALIAS
:
1718 ret
= visit_typealias(ctx
, entry_node
->u
.typealias
.target
,
1719 entry_node
->u
.typealias
.alias
);
1722 "cannot add typealias in \"variant\" declaration");
1726 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
1728 ret
= visit_variant_decl_field(ctx
, variant_decl
,
1729 entry_node
->u
.struct_or_variant_declaration
.
1730 type_specifier_list
,
1731 &entry_node
->u
.struct_or_variant_declaration
.
1738 BT_LOGE("unexpected node type: %d", (int) entry_node
->type
);
1748 int visit_struct_decl(struct ctx
*ctx
, const char *name
,
1749 struct bt_list_head
*decl_list
, int has_body
,
1750 struct bt_list_head
*min_align
,
1751 struct bt_ctf_field_type
**struct_decl
)
1755 *struct_decl
= NULL
;
1757 /* For named struct (without body), lookup in declaration scope */
1759 _BT_CTF_FIELD_TYPE_INIT(struct_decl_copy
);
1766 *struct_decl
= ctx_decl_scope_lookup_struct(ctx
->current_scope
,
1768 if (!*struct_decl
) {
1769 BT_LOGE("cannot find \"struct %s\"", name
);
1774 /* Make a copy of it */
1775 struct_decl_copy
= bt_ctf_field_type_copy(*struct_decl
);
1776 if (!struct_decl_copy
) {
1778 "cannot create copy of structure field type");
1783 BT_MOVE(*struct_decl
, struct_decl_copy
);
1785 struct ctf_node
*entry_node
;
1786 uint64_t min_align_value
= 0;
1789 _BT_CTF_FIELD_TYPE_INIT(estruct_decl
);
1791 estruct_decl
= ctx_decl_scope_lookup_struct(
1792 ctx
->current_scope
, name
, 1);
1794 BT_PUT(estruct_decl
);
1795 BT_LOGE("\"struct %s\" already declared in local scope",
1802 if (!bt_list_empty(min_align
)) {
1803 ret
= get_unary_unsigned(min_align
, &min_align_value
);
1805 BT_LOGE("unexpected unary expression for structure declaration's \"align\" attribute");
1810 *struct_decl
= bt_ctf_field_type_structure_create();
1811 if (!*struct_decl
) {
1812 BT_LOGE("cannot create structure field type");
1817 if (min_align_value
!= 0) {
1818 ret
= bt_ctf_field_type_set_alignment(*struct_decl
,
1821 BT_LOGE("failed to set structure's minimal alignment");
1826 ret
= ctx_push_scope(ctx
);
1828 BT_LOGE("cannot push scope");
1832 bt_list_for_each_entry(entry_node
, decl_list
, siblings
) {
1833 ret
= visit_struct_decl_entry(ctx
, entry_node
,
1844 ret
= ctx_decl_scope_register_struct(ctx
->current_scope
,
1845 name
, *struct_decl
);
1847 BT_LOGE("cannot register \"struct %s\" in declaration scope",
1857 BT_PUT(*struct_decl
);
1863 int visit_variant_decl(struct ctx
*ctx
, const char *name
,
1864 const char *tag
, struct bt_list_head
*decl_list
,
1865 int has_body
, struct bt_ctf_field_type
**variant_decl
)
1868 _BT_CTF_FIELD_TYPE_INIT(untagged_variant_decl
);
1870 *variant_decl
= NULL
;
1872 /* For named variant (without body), lookup in declaration scope */
1874 _BT_CTF_FIELD_TYPE_INIT(variant_decl_copy
);
1881 untagged_variant_decl
=
1882 ctx_decl_scope_lookup_variant(ctx
->current_scope
,
1884 if (!untagged_variant_decl
) {
1885 BT_LOGE("cannot find \"variant %s\"", name
);
1890 /* Make a copy of it */
1891 variant_decl_copy
= bt_ctf_field_type_copy(
1892 untagged_variant_decl
);
1893 if (!variant_decl_copy
) {
1895 "cannot create copy of variant field type");
1900 BT_MOVE(untagged_variant_decl
, variant_decl_copy
);
1902 struct ctf_node
*entry_node
;
1905 struct bt_ctf_field_type
*evariant_decl
=
1906 ctx_decl_scope_lookup_struct(ctx
->current_scope
,
1909 if (evariant_decl
) {
1910 BT_PUT(evariant_decl
);
1911 BT_LOGE("\"variant %s\" already declared in local scope",
1918 untagged_variant_decl
= bt_ctf_field_type_variant_create(NULL
,
1920 if (!untagged_variant_decl
) {
1921 BT_LOGE("cannot create variant field type");
1926 ret
= ctx_push_scope(ctx
);
1928 BT_LOGE("cannot push scope");
1932 bt_list_for_each_entry(entry_node
, decl_list
, siblings
) {
1933 ret
= visit_variant_decl_entry(ctx
, entry_node
,
1934 untagged_variant_decl
);
1944 ret
= ctx_decl_scope_register_variant(
1945 ctx
->current_scope
, name
,
1946 untagged_variant_decl
);
1948 BT_LOGE("cannot register \"variant %s\" in declaration scope",
1956 * If tagged, create tagged variant and return; otherwise
1957 * return untagged variant.
1960 BT_MOVE(*variant_decl
, untagged_variant_decl
);
1963 * At this point, we have a fresh untagged variant; nobody
1964 * else owns it. Set its tag now.
1966 ret
= bt_ctf_field_type_variant_set_tag_name(
1967 untagged_variant_decl
, tag
);
1972 BT_MOVE(*variant_decl
, untagged_variant_decl
);
1975 assert(!untagged_variant_decl
);
1976 assert(*variant_decl
);
1981 BT_PUT(untagged_variant_decl
);
1982 BT_PUT(*variant_decl
);
1988 int visit_enum_decl_entry(struct ctx
*ctx
, struct ctf_node
*enumerator
,
1989 struct bt_ctf_field_type
*enum_decl
, int64_t *last
, int is_signed
)
1993 struct ctf_node
*iter
;
1994 int64_t start
= 0, end
= 0;
1995 const char *label
= enumerator
->u
.enumerator
.id
;
1996 struct bt_list_head
*values
= &enumerator
->u
.enumerator
.values
;
1998 bt_list_for_each_entry(iter
, values
, siblings
) {
2001 if (iter
->type
!= NODE_UNARY_EXPRESSION
) {
2002 BT_LOGE("wrong unary expression for enumeration label \"%s\"",
2014 switch (iter
->u
.unary_expression
.type
) {
2015 case UNARY_SIGNED_CONSTANT
:
2016 *target
= iter
->u
.unary_expression
.u
.signed_constant
;
2018 case UNARY_UNSIGNED_CONSTANT
:
2020 iter
->u
.unary_expression
.u
.unsigned_constant
;
2023 BT_LOGE("invalid enumeration entry: \"%s\"",
2030 BT_LOGE("invalid enumeration entry: \"%s\"",
2050 ret
= bt_ctf_field_type_enumeration_add_mapping(enum_decl
, label
,
2053 ret
= bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_decl
,
2054 label
, (uint64_t) start
, (uint64_t) end
);
2057 BT_LOGE("cannot add mapping to enumeration for label \"%s\"",
2069 int visit_enum_decl(struct ctx
*ctx
, const char *name
,
2070 struct ctf_node
*container_type
,
2071 struct bt_list_head
*enumerator_list
,
2073 struct bt_ctf_field_type
**enum_decl
)
2077 _BT_CTF_FIELD_TYPE_INIT(integer_decl
);
2081 /* For named enum (without body), lookup in declaration scope */
2083 _BT_CTF_FIELD_TYPE_INIT(enum_decl_copy
);
2090 *enum_decl
= ctx_decl_scope_lookup_enum(ctx
->current_scope
,
2093 BT_LOGE("cannot find \"enum %s\"", name
);
2098 /* Make a copy of it */
2099 enum_decl_copy
= bt_ctf_field_type_copy(*enum_decl
);
2100 if (!enum_decl_copy
) {
2102 "cannot create copy of enumeration field type");
2108 BT_MOVE(*enum_decl
, enum_decl_copy
);
2110 struct ctf_node
*iter
;
2111 int64_t last_value
= 0;
2114 _BT_CTF_FIELD_TYPE_INIT(eenum_decl
);
2116 eenum_decl
= ctx_decl_scope_lookup_enum(
2117 ctx
->current_scope
, name
, 1);
2120 BT_LOGE("\"enum %s\" already declared in local scope",
2127 if (!container_type
) {
2128 integer_decl
= ctx_decl_scope_lookup_alias(
2129 ctx
->current_scope
, "int", -1);
2130 if (!integer_decl
) {
2131 BT_LOGE("cannot find \"int\" type for enumeration");
2136 ret
= visit_type_declarator(ctx
, container_type
,
2137 &qdummy_id
, NULL
, &integer_decl
, NULL
);
2139 assert(!integer_decl
);
2145 assert(integer_decl
);
2147 if (!bt_ctf_field_type_is_integer(integer_decl
)) {
2148 BT_LOGE("container type for enumeration is not an integer");
2153 *enum_decl
= bt_ctf_field_type_enumeration_create(integer_decl
);
2155 BT_LOGE("cannot create enumeration field type");
2160 bt_list_for_each_entry(iter
, enumerator_list
, siblings
) {
2161 ret
= visit_enum_decl_entry(ctx
, iter
, *enum_decl
,
2163 bt_ctf_field_type_integer_get_signed(integer_decl
));
2170 ret
= ctx_decl_scope_register_enum(ctx
->current_scope
,
2178 BT_PUT(integer_decl
);
2183 BT_PUT(integer_decl
);
2190 int visit_type_specifier(struct ctx
*ctx
,
2191 struct ctf_node
*type_specifier_list
,
2192 struct bt_ctf_field_type
**decl
)
2195 GString
*str
= NULL
;
2196 _BT_CTF_FIELD_TYPE_INIT(decl_copy
);
2199 str
= g_string_new("");
2200 ret
= get_type_specifier_list_name(ctx
, type_specifier_list
, str
);
2205 *decl
= ctx_decl_scope_lookup_alias(ctx
->current_scope
, str
->str
, -1);
2207 BT_LOGE("cannot find type alias \"%s\"", str
->str
);
2212 /* Make a copy of the type declaration */
2213 decl_copy
= bt_ctf_field_type_copy(*decl
);
2215 BT_LOGE("cannot create field type copy");
2220 BT_MOVE(*decl
, decl_copy
);
2221 (void) g_string_free(str
, TRUE
);
2228 (void) g_string_free(str
, TRUE
);
2237 int visit_integer_decl(struct ctx
*ctx
,
2238 struct bt_list_head
*expressions
,
2239 struct bt_ctf_field_type
**integer_decl
)
2244 struct ctf_node
*expression
;
2245 uint64_t alignment
= 0, size
= 0;
2246 struct bt_ctf_clock_class
*mapped_clock
= NULL
;
2247 enum bt_ctf_string_encoding encoding
= BT_CTF_STRING_ENCODING_NONE
;
2248 enum bt_ctf_integer_base base
= BT_CTF_INTEGER_BASE_DECIMAL
;
2249 enum bt_ctf_byte_order byte_order
=
2250 bt_ctf_trace_get_native_byte_order(ctx
->trace
);
2252 *integer_decl
= NULL
;
2254 bt_list_for_each_entry(expression
, expressions
, siblings
) {
2255 struct ctf_node
*left
, *right
;
2257 left
= _BT_LIST_FIRST_ENTRY(&expression
->u
.ctf_expression
.left
,
2258 struct ctf_node
, siblings
);
2259 right
= _BT_LIST_FIRST_ENTRY(
2260 &expression
->u
.ctf_expression
.right
, struct ctf_node
,
2263 if (left
->u
.unary_expression
.type
!= UNARY_STRING
) {
2268 if (!strcmp(left
->u
.unary_expression
.u
.string
, "signed")) {
2269 if (_IS_SET(&set
, _INTEGER_SIGNED_SET
)) {
2270 BT_LOGE_DUP_ATTR("signed",
2271 "integer declaration");
2276 signedness
= get_boolean(right
);
2277 if (signedness
< 0) {
2282 _SET(&set
, _INTEGER_SIGNED_SET
);
2283 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2285 if (_IS_SET(&set
, _INTEGER_BYTE_ORDER_SET
)) {
2286 BT_LOGE_DUP_ATTR("byte_order",
2287 "integer declaration");
2292 byte_order
= get_real_byte_order(ctx
, right
);
2293 if (byte_order
== BT_CTF_BYTE_ORDER_UNKNOWN
) {
2294 BT_LOGE("invalid \"byte_order\" attribute in integer declaration");
2299 _SET(&set
, _INTEGER_BYTE_ORDER_SET
);
2300 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "size")) {
2301 if (_IS_SET(&set
, _INTEGER_SIZE_SET
)) {
2302 BT_LOGE_DUP_ATTR("size",
2303 "integer declaration");
2308 if (right
->u
.unary_expression
.type
!=
2309 UNARY_UNSIGNED_CONSTANT
) {
2310 BT_LOGE("invalid \"size\" attribute in integer declaration: expecting unsigned constant");
2315 size
= right
->u
.unary_expression
.u
.unsigned_constant
;
2317 BT_LOGE("invalid \"size\" attribute in integer declaration: expecting positive constant");
2320 } else if (size
> 64) {
2321 BT_LOGE("invalid \"size\" attribute in integer declaration: integers over 64-bit are not supported as of this version");
2326 _SET(&set
, _INTEGER_SIZE_SET
);
2327 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2329 if (_IS_SET(&set
, _INTEGER_ALIGN_SET
)) {
2330 BT_LOGE_DUP_ATTR("align",
2331 "integer declaration");
2336 if (right
->u
.unary_expression
.type
!=
2337 UNARY_UNSIGNED_CONSTANT
) {
2338 BT_LOGE("invalid \"align\" attribute in integer declaration: expecting unsigned constant");
2344 right
->u
.unary_expression
.u
.unsigned_constant
;
2345 if (!is_align_valid(alignment
)) {
2346 BT_LOGE("invalid \"align\" attribute in integer declaration: expecting power of two");
2351 _SET(&set
, _INTEGER_ALIGN_SET
);
2352 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "base")) {
2353 if (_IS_SET(&set
, _INTEGER_BASE_SET
)) {
2354 BT_LOGE_DUP_ATTR("base", "integer declaration");
2359 switch (right
->u
.unary_expression
.type
) {
2360 case UNARY_UNSIGNED_CONSTANT
:
2362 uint64_t constant
= right
->u
.unary_expression
.
2363 u
.unsigned_constant
;
2367 base
= BT_CTF_INTEGER_BASE_BINARY
;
2370 base
= BT_CTF_INTEGER_BASE_OCTAL
;
2373 base
= BT_CTF_INTEGER_BASE_DECIMAL
;
2376 base
= BT_CTF_INTEGER_BASE_HEXADECIMAL
;
2379 BT_LOGE("invalid \"base\" attribute in integer declaration: %" PRIu64
,
2380 right
->u
.unary_expression
.u
.unsigned_constant
);
2388 char *s_right
= concatenate_unary_strings(
2389 &expression
->u
.ctf_expression
.right
);
2391 BT_LOGE("unexpected unary expression for integer declaration's \"base\" attribute");
2396 if (!strcmp(s_right
, "decimal") ||
2397 !strcmp(s_right
, "dec") ||
2398 !strcmp(s_right
, "d") ||
2399 !strcmp(s_right
, "i") ||
2400 !strcmp(s_right
, "u")) {
2401 base
= BT_CTF_INTEGER_BASE_DECIMAL
;
2402 } else if (!strcmp(s_right
, "hexadecimal") ||
2403 !strcmp(s_right
, "hex") ||
2404 !strcmp(s_right
, "x") ||
2405 !strcmp(s_right
, "X") ||
2406 !strcmp(s_right
, "p")) {
2407 base
= BT_CTF_INTEGER_BASE_HEXADECIMAL
;
2408 } else if (!strcmp(s_right
, "octal") ||
2409 !strcmp(s_right
, "oct") ||
2410 !strcmp(s_right
, "o")) {
2411 base
= BT_CTF_INTEGER_BASE_OCTAL
;
2412 } else if (!strcmp(s_right
, "binary") ||
2413 !strcmp(s_right
, "b")) {
2414 base
= BT_CTF_INTEGER_BASE_BINARY
;
2416 BT_LOGE("unexpected unary expression for integer declaration's \"base\" attribute: \"%s\"",
2427 BT_LOGE("invalid \"base\" attribute in integer declaration: expecting unsigned constant or unary string");
2432 _SET(&set
, _INTEGER_BASE_SET
);
2433 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2437 if (_IS_SET(&set
, _INTEGER_ENCODING_SET
)) {
2438 BT_LOGE_DUP_ATTR("encoding",
2439 "integer declaration");
2444 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
2445 BT_LOGE("invalid \"encoding\" attribute in integer declaration: expecting unary string");
2450 s_right
= concatenate_unary_strings(
2451 &expression
->u
.ctf_expression
.right
);
2453 BT_LOGE("unexpected unary expression for integer declaration's \"encoding\" attribute");
2458 if (!strcmp(s_right
, "UTF8") ||
2459 !strcmp(s_right
, "utf8") ||
2460 !strcmp(s_right
, "utf-8") ||
2461 !strcmp(s_right
, "UTF-8")) {
2462 encoding
= BT_CTF_STRING_ENCODING_UTF8
;
2463 } else if (!strcmp(s_right
, "ASCII") ||
2464 !strcmp(s_right
, "ascii")) {
2465 encoding
= BT_CTF_STRING_ENCODING_ASCII
;
2466 } else if (!strcmp(s_right
, "none")) {
2467 encoding
= BT_CTF_STRING_ENCODING_NONE
;
2469 BT_LOGE("invalid \"encoding\" attribute in integer declaration: unknown encoding \"%s\"",
2477 _SET(&set
, _INTEGER_ENCODING_SET
);
2478 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "map")) {
2479 const char *clock_name
;
2481 if (_IS_SET(&set
, _INTEGER_MAP_SET
)) {
2482 BT_LOGE_DUP_ATTR("map", "integer declaration");
2487 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
2488 BT_LOGE("invalid \"map\" attribute in integer declaration: expecting unary string");
2494 get_map_clock_name_value(
2495 &expression
->u
.ctf_expression
.right
);
2497 char *s_right
= concatenate_unary_strings(
2498 &expression
->u
.ctf_expression
.right
);
2501 BT_LOGE("unexpected unary expression for integer declaration's \"map\" attribute");
2506 BT_LOGW("invalid \"map\" attribute in integer declaration: unknown clock class: \"%s\"",
2508 _SET(&set
, _INTEGER_MAP_SET
);
2513 mapped_clock
= bt_ctf_trace_get_clock_class_by_name(
2514 ctx
->trace
, clock_name
);
2515 if (!mapped_clock
) {
2516 BT_LOGE("invalid \"map\" attribute in integer declaration: cannot find clock class \"%s\"",
2522 _SET(&set
, _INTEGER_MAP_SET
);
2524 BT_LOGW("unknown attribute \"%s\" in integer declaration",
2525 left
->u
.unary_expression
.u
.string
);
2529 if (!_IS_SET(&set
, _INTEGER_SIZE_SET
)) {
2531 "missing \"size\" attribute in integer declaration");
2536 if (!_IS_SET(&set
, _INTEGER_ALIGN_SET
)) {
2537 if (size
% CHAR_BIT
) {
2538 /* Bit-packed alignment */
2541 /* Byte-packed alignment */
2542 alignment
= CHAR_BIT
;
2546 *integer_decl
= bt_ctf_field_type_integer_create((unsigned int) size
);
2547 if (!*integer_decl
) {
2548 BT_LOGE("cannot create integer field type");
2553 ret
= bt_ctf_field_type_integer_set_signed(*integer_decl
, signedness
);
2554 ret
|= bt_ctf_field_type_integer_set_base(*integer_decl
, base
);
2555 ret
|= bt_ctf_field_type_integer_set_encoding(*integer_decl
, encoding
);
2556 ret
|= bt_ctf_field_type_set_alignment(*integer_decl
,
2557 (unsigned int) alignment
);
2558 ret
|= bt_ctf_field_type_set_byte_order(*integer_decl
, byte_order
);
2562 ret
|= bt_ctf_field_type_integer_set_mapped_clock_class(
2563 *integer_decl
, mapped_clock
);
2564 bt_put(mapped_clock
);
2565 mapped_clock
= NULL
;
2569 BT_LOGE("cannot configure integer field type");
2578 bt_put(mapped_clock
);
2581 BT_PUT(*integer_decl
);
2587 int visit_floating_point_number_decl(struct ctx
*ctx
,
2588 struct bt_list_head
*expressions
,
2589 struct bt_ctf_field_type
**float_decl
)
2593 struct ctf_node
*expression
;
2594 uint64_t alignment
= 1, exp_dig
= 0, mant_dig
= 0;
2595 enum bt_ctf_byte_order byte_order
=
2596 bt_ctf_trace_get_native_byte_order(ctx
->trace
);
2600 bt_list_for_each_entry(expression
, expressions
, siblings
) {
2601 struct ctf_node
*left
, *right
;
2603 left
= _BT_LIST_FIRST_ENTRY(&expression
->u
.ctf_expression
.left
,
2604 struct ctf_node
, siblings
);
2605 right
= _BT_LIST_FIRST_ENTRY(
2606 &expression
->u
.ctf_expression
.right
, struct ctf_node
,
2609 if (left
->u
.unary_expression
.type
!= UNARY_STRING
) {
2614 if (!strcmp(left
->u
.unary_expression
.u
.string
, "byte_order")) {
2615 if (_IS_SET(&set
, _FLOAT_BYTE_ORDER_SET
)) {
2616 BT_LOGE_DUP_ATTR("byte_order",
2617 "floating point number declaration");
2622 byte_order
= get_real_byte_order(ctx
, right
);
2623 if (byte_order
== BT_CTF_BYTE_ORDER_UNKNOWN
) {
2624 BT_LOGE("invalid \"byte_order\" attribute in floating point number declaration");
2629 _SET(&set
, _FLOAT_BYTE_ORDER_SET
);
2630 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2632 if (_IS_SET(&set
, _FLOAT_EXP_DIG_SET
)) {
2633 BT_LOGE_DUP_ATTR("exp_dig",
2634 "floating point number declaration");
2639 if (right
->u
.unary_expression
.type
!=
2640 UNARY_UNSIGNED_CONSTANT
) {
2641 BT_LOGE("invalid \"exp_dig\" attribute in floating point number declaration: expecting unsigned constant");
2646 exp_dig
= right
->u
.unary_expression
.u
.unsigned_constant
;
2647 _SET(&set
, _FLOAT_EXP_DIG_SET
);
2648 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2650 if (_IS_SET(&set
, _FLOAT_MANT_DIG_SET
)) {
2651 BT_LOGE_DUP_ATTR("mant_dig",
2652 "floating point number declaration");
2657 if (right
->u
.unary_expression
.type
!=
2658 UNARY_UNSIGNED_CONSTANT
) {
2659 BT_LOGE("invalid \"mant_dig\" attribute in floating point number declaration: expecting unsigned constant");
2664 mant_dig
= right
->u
.unary_expression
.u
.
2666 _SET(&set
, _FLOAT_MANT_DIG_SET
);
2667 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2669 if (_IS_SET(&set
, _FLOAT_ALIGN_SET
)) {
2670 BT_LOGE_DUP_ATTR("align",
2671 "floating point number declaration");
2676 if (right
->u
.unary_expression
.type
!=
2677 UNARY_UNSIGNED_CONSTANT
) {
2678 BT_LOGE("invalid \"align\" attribute in floating point number declaration: expecting unsigned constant");
2683 alignment
= right
->u
.unary_expression
.u
.
2686 if (!is_align_valid(alignment
)) {
2687 BT_LOGE("invalid \"align\" attribute in floating point number declaration: expecting power of two");
2692 _SET(&set
, _FLOAT_ALIGN_SET
);
2694 BT_LOGW("unknown attribute \"%s\" in floating point number declaration",
2695 left
->u
.unary_expression
.u
.string
);
2699 if (!_IS_SET(&set
, _FLOAT_MANT_DIG_SET
)) {
2700 BT_LOGE("missing \"mant_dig\" attribute in floating point number declaration");
2705 if (!_IS_SET(&set
, _FLOAT_EXP_DIG_SET
)) {
2706 BT_LOGE("missing \"exp_dig\" attribute in floating point number declaration");
2711 if (!_IS_SET(&set
, _INTEGER_ALIGN_SET
)) {
2712 if ((mant_dig
+ exp_dig
) % CHAR_BIT
) {
2713 /* Bit-packed alignment */
2716 /* Byte-packed alignment */
2717 alignment
= CHAR_BIT
;
2721 *float_decl
= bt_ctf_field_type_floating_point_create();
2724 "cannot create floating point number field type");
2729 ret
= bt_ctf_field_type_floating_point_set_exponent_digits(
2730 *float_decl
, exp_dig
);
2731 ret
|= bt_ctf_field_type_floating_point_set_mantissa_digits(
2732 *float_decl
, mant_dig
);
2733 ret
|= bt_ctf_field_type_set_byte_order(*float_decl
, byte_order
);
2734 ret
|= bt_ctf_field_type_set_alignment(*float_decl
, alignment
);
2737 "cannot configure floating point number field type");
2745 BT_PUT(*float_decl
);
2751 int visit_string_decl(struct ctx
*ctx
,
2752 struct bt_list_head
*expressions
,
2753 struct bt_ctf_field_type
**string_decl
)
2757 struct ctf_node
*expression
;
2758 enum bt_ctf_string_encoding encoding
= BT_CTF_STRING_ENCODING_UTF8
;
2760 *string_decl
= NULL
;
2762 bt_list_for_each_entry(expression
, expressions
, siblings
) {
2763 struct ctf_node
*left
, *right
;
2765 left
= _BT_LIST_FIRST_ENTRY(&expression
->u
.ctf_expression
.left
,
2766 struct ctf_node
, siblings
);
2767 right
= _BT_LIST_FIRST_ENTRY(
2768 &expression
->u
.ctf_expression
.right
, struct ctf_node
,
2771 if (left
->u
.unary_expression
.type
!= UNARY_STRING
) {
2776 if (!strcmp(left
->u
.unary_expression
.u
.string
, "encoding")) {
2779 if (_IS_SET(&set
, _STRING_ENCODING_SET
)) {
2780 BT_LOGE_DUP_ATTR("encoding",
2781 "string declaration");
2786 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
2787 BT_LOGE("invalid \"encoding\" attribute in string declaration: expecting unary string");
2792 s_right
= concatenate_unary_strings(
2793 &expression
->u
.ctf_expression
.right
);
2795 BT_LOGE("unexpected unary expression for string declaration's \"encoding\" attribute");
2800 if (!strcmp(s_right
, "UTF8") ||
2801 !strcmp(s_right
, "utf8") ||
2802 !strcmp(s_right
, "utf-8") ||
2803 !strcmp(s_right
, "UTF-8")) {
2804 encoding
= BT_CTF_STRING_ENCODING_UTF8
;
2805 } else if (!strcmp(s_right
, "ASCII") ||
2806 !strcmp(s_right
, "ascii")) {
2807 encoding
= BT_CTF_STRING_ENCODING_ASCII
;
2808 } else if (!strcmp(s_right
, "none")) {
2809 encoding
= BT_CTF_STRING_ENCODING_NONE
;
2811 BT_LOGE("invalid \"encoding\" attribute in string declaration: unknown encoding \"%s\"",
2819 _SET(&set
, _STRING_ENCODING_SET
);
2821 BT_LOGW("unknown attribute \"%s\" in string declaration",
2822 left
->u
.unary_expression
.u
.string
);
2826 *string_decl
= bt_ctf_field_type_string_create();
2827 if (!*string_decl
) {
2828 BT_LOGE("cannot create string field type");
2833 ret
= bt_ctf_field_type_string_set_encoding(*string_decl
, encoding
);
2835 BT_LOGE("cannot configure string field type");
2843 BT_PUT(*string_decl
);
2849 int visit_type_specifier_list(struct ctx
*ctx
,
2850 struct ctf_node
*ts_list
,
2851 struct bt_ctf_field_type
**decl
)
2854 struct ctf_node
*first
, *node
;
2858 if (ts_list
->type
!= NODE_TYPE_SPECIFIER_LIST
) {
2863 first
= _BT_LIST_FIRST_ENTRY(&ts_list
->u
.type_specifier_list
.head
,
2864 struct ctf_node
, siblings
);
2865 if (first
->type
!= NODE_TYPE_SPECIFIER
) {
2870 node
= first
->u
.type_specifier
.node
;
2872 switch (first
->u
.type_specifier
.type
) {
2873 case TYPESPEC_INTEGER
:
2874 ret
= visit_integer_decl(ctx
, &node
->u
.integer
.expressions
,
2881 case TYPESPEC_FLOATING_POINT
:
2882 ret
= visit_floating_point_number_decl(ctx
,
2883 &node
->u
.floating_point
.expressions
, decl
);
2889 case TYPESPEC_STRING
:
2890 ret
= visit_string_decl(ctx
,
2891 &node
->u
.string
.expressions
, decl
);
2897 case TYPESPEC_STRUCT
:
2898 ret
= visit_struct_decl(ctx
, node
->u
._struct
.name
,
2899 &node
->u
._struct
.declaration_list
,
2900 node
->u
._struct
.has_body
,
2901 &node
->u
._struct
.min_align
, decl
);
2907 case TYPESPEC_VARIANT
:
2908 ret
= visit_variant_decl(ctx
, node
->u
.variant
.name
,
2909 node
->u
.variant
.choice
,
2910 &node
->u
.variant
.declaration_list
,
2911 node
->u
.variant
.has_body
, decl
);
2918 ret
= visit_enum_decl(ctx
, node
->u
._enum
.enum_id
,
2919 node
->u
._enum
.container_type
,
2920 &node
->u
._enum
.enumerator_list
,
2921 node
->u
._enum
.has_body
, decl
);
2929 case TYPESPEC_SHORT
:
2932 case TYPESPEC_FLOAT
:
2933 case TYPESPEC_DOUBLE
:
2934 case TYPESPEC_SIGNED
:
2935 case TYPESPEC_UNSIGNED
:
2937 case TYPESPEC_COMPLEX
:
2938 case TYPESPEC_IMAGINARY
:
2939 case TYPESPEC_CONST
:
2940 case TYPESPEC_ID_TYPE
:
2941 ret
= visit_type_specifier(ctx
, ts_list
, decl
);
2948 BT_LOGE("unexpected node type: %d",
2949 (int) first
->u
.type_specifier
.type
);
2965 int visit_event_decl_entry(struct ctx
*ctx
, struct ctf_node
*node
,
2966 struct bt_ctf_event_class
*event_class
, int64_t *stream_id
,
2971 _BT_CTF_FIELD_TYPE_INIT(decl
);
2973 switch (node
->type
) {
2975 ret
= visit_typedef(ctx
, node
->u
._typedef
.type_specifier_list
,
2976 &node
->u
._typedef
.type_declarators
);
2979 "cannot add typedef in \"event\" declaration");
2983 case NODE_TYPEALIAS
:
2984 ret
= visit_typealias(ctx
, node
->u
.typealias
.target
,
2985 node
->u
.typealias
.alias
);
2987 BT_LOGE("cannot add typealias in \"event\" declaration");
2991 case NODE_CTF_EXPRESSION
:
2993 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
2999 if (!strcmp(left
, "name")) {
3000 /* This is already known at this stage */
3001 if (_IS_SET(set
, _EVENT_NAME_SET
)) {
3002 BT_LOGE_DUP_ATTR("name", "event declaration");
3007 _SET(set
, _EVENT_NAME_SET
);
3008 } else if (!strcmp(left
, "id")) {
3011 if (_IS_SET(set
, _EVENT_ID_SET
)) {
3012 BT_LOGE_DUP_ATTR("id", "event declaration");
3017 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
3019 /* Only read "id" if get_unary_unsigned() succeeded. */
3020 if (ret
|| (!ret
&& id
< 0)) {
3021 BT_LOGE("unexpected unary expression for event declaration's \"id\" attribute");
3026 ret
= bt_ctf_event_class_set_id(event_class
, id
);
3029 "cannot set event declaration's ID");
3033 _SET(set
, _EVENT_ID_SET
);
3034 } else if (!strcmp(left
, "stream_id")) {
3035 if (_IS_SET(set
, _EVENT_STREAM_ID_SET
)) {
3036 BT_LOGE_DUP_ATTR("stream_id",
3037 "event declaration");
3042 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
3043 (uint64_t *) stream_id
);
3045 * Only read "stream_id" if get_unary_unsigned()
3048 if (ret
|| (!ret
&& *stream_id
< 0)) {
3049 BT_LOGE("unexpected unary expression for event declaration's \"stream_id\" attribute");
3054 _SET(set
, _EVENT_STREAM_ID_SET
);
3055 } else if (!strcmp(left
, "context")) {
3056 if (_IS_SET(set
, _EVENT_CONTEXT_SET
)) {
3057 BT_LOGE("duplicate \"context\" entry in event declaration");
3062 ret
= visit_type_specifier_list(ctx
,
3063 _BT_LIST_FIRST_ENTRY(
3064 &node
->u
.ctf_expression
.right
,
3065 struct ctf_node
, siblings
),
3068 BT_LOGE("cannot create event context declaration");
3073 ret
= bt_ctf_event_class_set_context_type(
3077 BT_LOGE("cannot set event's context declaration");
3081 _SET(set
, _EVENT_CONTEXT_SET
);
3082 } else if (!strcmp(left
, "fields")) {
3083 if (_IS_SET(set
, _EVENT_FIELDS_SET
)) {
3084 BT_LOGE("duplicate \"fields\" entry in event declaration");
3089 ret
= visit_type_specifier_list(ctx
,
3090 _BT_LIST_FIRST_ENTRY(
3091 &node
->u
.ctf_expression
.right
,
3092 struct ctf_node
, siblings
),
3095 BT_LOGE("cannot create event payload field type");
3100 ret
= bt_ctf_event_class_set_payload_type(
3104 BT_LOGE("cannot set event's payload field type");
3108 _SET(set
, _EVENT_FIELDS_SET
);
3109 } else if (!strcmp(left
, "loglevel")) {
3110 uint64_t loglevel_value
;
3111 const char *loglevel_str
;
3112 struct bt_value
*value_obj
, *str_obj
;
3114 if (_IS_SET(set
, _EVENT_LOGLEVEL_SET
)) {
3115 BT_LOGE_DUP_ATTR("loglevel",
3116 "event declaration");
3121 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
3124 BT_LOGE("unexpected unary expression for event declaration's \"loglevel\" attribute");
3128 value_obj
= bt_value_integer_create_init(loglevel_value
);
3130 BT_LOGE("cannot allocate memory for loglevel value object");
3134 if (bt_ctf_event_class_set_attribute(event_class
,
3135 "loglevel", value_obj
) != BT_VALUE_STATUS_OK
) {
3136 BT_LOGE("cannot set loglevel value");
3141 loglevel_str
= print_loglevel(loglevel_value
);
3143 str_obj
= bt_value_string_create_init(loglevel_str
);
3144 if (bt_ctf_event_class_set_attribute(event_class
,
3145 "loglevel_string", str_obj
) != BT_VALUE_STATUS_OK
) {
3146 BT_LOGE("cannot set loglevel string");
3154 _SET(set
, _EVENT_LOGLEVEL_SET
);
3155 } else if (!strcmp(left
, "model.emf.uri")) {
3158 if (_IS_SET(set
, _EVENT_MODEL_EMF_URI_SET
)) {
3159 BT_LOGE_DUP_ATTR("model.emf.uri",
3160 "event declaration");
3165 right
= concatenate_unary_strings(
3166 &node
->u
.ctf_expression
.right
);
3168 BT_LOGE("unexpected unary expression for event declaration's \"model.emf.uri\" attribute");
3173 // TODO: FIXME: set model EMF URI here
3176 _SET(set
, _EVENT_MODEL_EMF_URI_SET
);
3178 BT_LOGW("unknown attribute \"%s\" in event declaration",
3204 char *get_event_decl_name(struct ctx
*ctx
, struct ctf_node
*node
)
3208 struct ctf_node
*iter
;
3209 struct bt_list_head
*decl_list
= &node
->u
.event
.declaration_list
;
3211 bt_list_for_each_entry(iter
, decl_list
, siblings
) {
3212 if (iter
->type
!= NODE_CTF_EXPRESSION
) {
3216 left
= concatenate_unary_strings(&iter
->u
.ctf_expression
.left
);
3221 if (!strcmp(left
, "name")) {
3222 name
= concatenate_unary_strings(
3223 &iter
->u
.ctf_expression
.right
);
3225 BT_LOGE("unexpected unary expression for event declaration's \"name\" attribute");
3247 int reset_event_decl_types(struct ctx
*ctx
,
3248 struct bt_ctf_event_class
*event_class
)
3253 ret
= bt_ctf_event_class_set_context_type(event_class
, NULL
);
3255 BT_LOGE("cannot set initial NULL event context");
3259 /* Event payload. */
3260 ret
= bt_ctf_event_class_set_payload_type(event_class
, NULL
);
3262 BT_LOGE("cannot set initial NULL event payload");
3270 int reset_stream_decl_types(struct ctx
*ctx
,
3271 struct bt_ctf_stream_class
*stream_class
)
3275 /* Packet context. */
3276 ret
= bt_ctf_stream_class_set_packet_context_type(stream_class
, NULL
);
3278 BT_LOGE("cannot set initial empty packet context");
3283 ret
= bt_ctf_stream_class_set_event_header_type(stream_class
, NULL
);
3285 BT_LOGE("cannot set initial empty event header");
3289 /* Event context. */
3290 ret
= bt_ctf_stream_class_set_event_context_type(stream_class
, NULL
);
3292 BT_LOGE("cannot set initial empty stream event context");
3300 struct bt_ctf_stream_class
*create_reset_stream_class(struct ctx
*ctx
)
3303 struct bt_ctf_stream_class
*stream_class
;
3305 stream_class
= bt_ctf_stream_class_create(NULL
);
3306 if (!stream_class
) {
3307 BT_LOGE("cannot create stream class");
3312 * Set packet context, event header, and event context to NULL to
3313 * override the default ones.
3315 ret
= reset_stream_decl_types(ctx
, stream_class
);
3320 return stream_class
;
3323 BT_PUT(stream_class
);
3329 int visit_event_decl(struct ctx
*ctx
, struct ctf_node
*node
)
3334 struct ctf_node
*iter
;
3335 int64_t stream_id
= -1;
3336 char *event_name
= NULL
;
3337 struct bt_ctf_event_class
*event_class
= NULL
;
3338 struct bt_ctf_event_class
*eevent_class
;
3339 struct bt_ctf_stream_class
*stream_class
= NULL
;
3340 struct bt_list_head
*decl_list
= &node
->u
.event
.declaration_list
;
3341 bool pop_scope
= false;
3343 if (node
->visited
) {
3347 node
->visited
= TRUE
;
3348 event_name
= get_event_decl_name(ctx
, node
);
3351 "missing \"name\" attribute in event declaration");
3356 event_class
= bt_ctf_event_class_create(event_name
);
3359 * Unset context and fields to override the default ones.
3361 ret
= reset_event_decl_types(ctx
, event_class
);
3366 ret
= ctx_push_scope(ctx
);
3368 BT_LOGE("cannot push scope");
3374 bt_list_for_each_entry(iter
, decl_list
, siblings
) {
3375 ret
= visit_event_decl_entry(ctx
, iter
, event_class
,
3382 if (!_IS_SET(&set
, _EVENT_STREAM_ID_SET
)) {
3384 struct bt_ctf_stream_class
*new_stream_class
;
3385 size_t stream_class_count
=
3386 g_hash_table_size(ctx
->stream_classes
) +
3387 bt_ctf_trace_get_stream_class_count(ctx
->trace
);
3390 * Allow missing stream_id if there is only a single
3393 switch (stream_class_count
) {
3395 /* Create implicit stream class if there's none */
3396 new_stream_class
= create_reset_stream_class(ctx
);
3397 if (!new_stream_class
) {
3402 ret
= bt_ctf_stream_class_set_id(new_stream_class
, 0);
3404 BT_LOGE("cannot set stream class's ID");
3405 BT_PUT(new_stream_class
);
3411 /* Move reference to visitor's context */
3412 g_hash_table_insert(ctx
->stream_classes
,
3413 (gpointer
) stream_id
, new_stream_class
);
3414 new_stream_class
= NULL
;
3417 /* Single stream class: get its ID */
3418 if (g_hash_table_size(ctx
->stream_classes
) == 1) {
3419 keys
= g_hash_table_get_keys(ctx
->stream_classes
);
3420 stream_id
= (int64_t) keys
->data
;
3423 assert(bt_ctf_trace_get_stream_class_count(
3426 bt_ctf_trace_get_stream_class_by_index(
3428 assert(stream_class
);
3429 stream_id
= bt_ctf_stream_class_get_id(
3431 BT_PUT(stream_class
);
3435 BT_LOGE("missing \"stream_id\" attribute in event declaration");
3441 assert(stream_id
>= 0);
3443 /* We have the stream ID now; get the stream class if found */
3444 stream_class
= g_hash_table_lookup(ctx
->stream_classes
,
3445 (gpointer
) stream_id
);
3446 bt_get(stream_class
);
3447 if (!stream_class
) {
3448 stream_class
= bt_ctf_trace_get_stream_class_by_id(ctx
->trace
,
3450 if (!stream_class
) {
3451 BT_LOGE("cannot find stream class with ID %" PRId64
,
3458 assert(stream_class
);
3460 if (!_IS_SET(&set
, _EVENT_ID_SET
)) {
3461 /* Allow only one event without ID per stream */
3462 if (bt_ctf_stream_class_get_event_class_count(stream_class
) !=
3465 "missing \"id\" field in event declaration");
3471 ret
= bt_ctf_event_class_set_id(event_class
, 0);
3473 BT_LOGE("cannot set event's ID");
3478 event_id
= bt_ctf_event_class_get_id(event_class
);
3480 BT_LOGE("cannot get event's ID");
3485 eevent_class
= bt_ctf_stream_class_get_event_class_by_id(stream_class
,
3488 BT_PUT(eevent_class
);
3489 BT_LOGE("duplicate event with ID %" PRId64
" in same stream", event_id
);
3494 ret
= bt_ctf_stream_class_add_event_class(stream_class
, event_class
);
3495 BT_PUT(event_class
);
3497 BT_LOGE("cannot add event class to stream class");
3504 bt_put(event_class
);
3512 bt_put(stream_class
);
3517 int visit_stream_decl_entry(struct ctx
*ctx
, struct ctf_node
*node
,
3518 struct bt_ctf_stream_class
*stream_class
, int *set
)
3522 _BT_CTF_FIELD_TYPE_INIT(decl
);
3524 switch (node
->type
) {
3526 ret
= visit_typedef(ctx
, node
->u
._typedef
.type_specifier_list
,
3527 &node
->u
._typedef
.type_declarators
);
3530 "cannot add typedef in \"stream\" declaration");
3534 case NODE_TYPEALIAS
:
3535 ret
= visit_typealias(ctx
, node
->u
.typealias
.target
,
3536 node
->u
.typealias
.alias
);
3538 BT_LOGE("cannot add typealias in \"stream\" declaration");
3542 case NODE_CTF_EXPRESSION
:
3544 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
3550 if (!strcmp(left
, "id")) {
3554 if (_IS_SET(set
, _STREAM_ID_SET
)) {
3555 BT_LOGE_DUP_ATTR("id", "stream declaration");
3560 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
3562 /* Only read "id" if get_unary_unsigned() succeeded. */
3563 if (ret
|| (!ret
&& id
< 0)) {
3564 BT_LOGE("unexpected unary expression for stream declaration's \"id\" attribute");
3569 ptr
= g_hash_table_lookup(ctx
->stream_classes
,
3572 BT_LOGE("duplicate stream with ID %" PRId64
,
3578 ret
= bt_ctf_stream_class_set_id(stream_class
, id
);
3581 "cannot set stream class's ID");
3585 _SET(set
, _STREAM_ID_SET
);
3586 } else if (!strcmp(left
, "event.header")) {
3587 if (_IS_SET(set
, _STREAM_EVENT_HEADER_SET
)) {
3588 BT_LOGE("duplicate \"event.header\" entry in stream declaration");
3593 ret
= visit_type_specifier_list(ctx
,
3594 _BT_LIST_FIRST_ENTRY(
3595 &node
->u
.ctf_expression
.right
,
3596 struct ctf_node
, siblings
),
3599 BT_LOGE("cannot create event header field type");
3605 ret
= bt_ctf_stream_class_set_event_header_type(
3606 stream_class
, decl
);
3609 BT_LOGE("cannot set stream's event header field type");
3613 _SET(set
, _STREAM_EVENT_HEADER_SET
);
3614 } else if (!strcmp(left
, "event.context")) {
3615 if (_IS_SET(set
, _STREAM_EVENT_CONTEXT_SET
)) {
3616 BT_LOGE("duplicate \"event.context\" entry in stream declaration");
3621 ret
= visit_type_specifier_list(ctx
,
3622 _BT_LIST_FIRST_ENTRY(
3623 &node
->u
.ctf_expression
.right
,
3624 struct ctf_node
, siblings
),
3627 BT_LOGE("cannot create stream event context field type");
3633 ret
= bt_ctf_stream_class_set_event_context_type(
3634 stream_class
, decl
);
3637 BT_LOGE("cannot set stream's event context field type");
3641 _SET(set
, _STREAM_EVENT_CONTEXT_SET
);
3642 } else if (!strcmp(left
, "packet.context")) {
3643 if (_IS_SET(set
, _STREAM_PACKET_CONTEXT_SET
)) {
3644 BT_LOGE("duplicate \"packet.context\" entry in stream declaration");
3649 ret
= visit_type_specifier_list(ctx
,
3650 _BT_LIST_FIRST_ENTRY(
3651 &node
->u
.ctf_expression
.right
,
3652 struct ctf_node
, siblings
),
3655 BT_LOGE("cannot create packet context field type");
3661 ret
= bt_ctf_stream_class_set_packet_context_type(
3662 stream_class
, decl
);
3665 BT_LOGE("cannot set stream's packet context field type");
3669 _SET(set
, _STREAM_PACKET_CONTEXT_SET
);
3671 BT_LOGW("unknown attribute \"%s\" in stream declaration",
3695 int visit_stream_decl(struct ctx
*ctx
, struct ctf_node
*node
)
3700 struct ctf_node
*iter
;
3701 struct bt_ctf_stream_class
*stream_class
= NULL
;
3702 struct bt_ctf_stream_class
*existing_stream_class
= NULL
;
3703 struct bt_list_head
*decl_list
= &node
->u
.stream
.declaration_list
;
3705 if (node
->visited
) {
3709 node
->visited
= TRUE
;
3710 stream_class
= create_reset_stream_class(ctx
);
3711 if (!stream_class
) {
3716 ret
= ctx_push_scope(ctx
);
3718 BT_LOGE("cannot push scope");
3722 bt_list_for_each_entry(iter
, decl_list
, siblings
) {
3723 ret
= visit_stream_decl_entry(ctx
, iter
, stream_class
, &set
);
3732 if (_IS_SET(&set
, _STREAM_ID_SET
)) {
3733 /* Check that packet header has stream_id field */
3734 _BT_CTF_FIELD_TYPE_INIT(stream_id_decl
);
3735 _BT_CTF_FIELD_TYPE_INIT(packet_header_decl
);
3737 packet_header_decl
=
3738 bt_ctf_trace_get_packet_header_type(ctx
->trace
);
3739 if (!packet_header_decl
) {
3741 "cannot get trace packet header field type");
3746 bt_ctf_field_type_structure_get_field_type_by_name(
3747 packet_header_decl
, "stream_id");
3748 BT_PUT(packet_header_decl
);
3749 if (!stream_id_decl
) {
3750 BT_LOGE("missing \"stream_id\" field in packet header declaration, but \"id\" attribute is declared for stream");
3754 if (!bt_ctf_field_type_is_integer(stream_id_decl
)) {
3755 BT_PUT(stream_id_decl
);
3756 BT_LOGE("\"stream_id\" field in packet header declaration is not an integer");
3760 BT_PUT(stream_id_decl
);
3762 /* Allow only _one_ ID-less stream */
3763 if (g_hash_table_size(ctx
->stream_classes
) != 0) {
3765 "missing \"id\" field in stream declaration");
3770 /* Automatic ID: 0 */
3771 ret
= bt_ctf_stream_class_set_id(stream_class
, 0);
3774 id
= bt_ctf_stream_class_get_id(stream_class
);
3776 BT_LOGE("wrong stream ID: %" PRId64
, id
);
3782 * Make sure that this stream class's ID is currently unique in
3785 if (g_hash_table_lookup(ctx
->stream_classes
, (gpointer
) id
)) {
3786 BT_LOGE("a stream class with ID: %" PRId64
" already exists in the trace", id
);
3791 existing_stream_class
= bt_ctf_trace_get_stream_class_by_id(ctx
->trace
,
3793 if (existing_stream_class
) {
3794 BT_LOGE("a stream class with ID: %" PRId64
" already exists in the trace", id
);
3799 /* Move reference to visitor's context */
3800 g_hash_table_insert(ctx
->stream_classes
, (gpointer
) (int64_t) id
,
3802 stream_class
= NULL
;
3806 bt_put(stream_class
);
3809 bt_put(existing_stream_class
);
3814 int visit_trace_decl_entry(struct ctx
*ctx
, struct ctf_node
*node
, int *set
)
3818 _BT_CTF_FIELD_TYPE_INIT(packet_header_decl
);
3820 switch (node
->type
) {
3822 ret
= visit_typedef(ctx
, node
->u
._typedef
.type_specifier_list
,
3823 &node
->u
._typedef
.type_declarators
);
3826 "cannot add typedef in \"trace\" declaration");
3830 case NODE_TYPEALIAS
:
3831 ret
= visit_typealias(ctx
, node
->u
.typealias
.target
,
3832 node
->u
.typealias
.alias
);
3835 "cannot add typealias in \"trace\" declaration");
3839 case NODE_CTF_EXPRESSION
:
3841 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
3847 if (!strcmp(left
, "major")) {
3848 if (_IS_SET(set
, _TRACE_MAJOR_SET
)) {
3849 BT_LOGE_DUP_ATTR("major", "trace declaration");
3854 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
3857 BT_LOGE("unexpected unary expression for trace's \"major\" attribute");
3862 _SET(set
, _TRACE_MAJOR_SET
);
3863 } else if (!strcmp(left
, "minor")) {
3864 if (_IS_SET(set
, _TRACE_MINOR_SET
)) {
3865 BT_LOGE_DUP_ATTR("minor", "trace declaration");
3870 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
3873 BT_LOGE("unexpected unary expression for trace's \"minor\" attribute");
3878 _SET(set
, _TRACE_MINOR_SET
);
3879 } else if (!strcmp(left
, "uuid")) {
3880 if (_IS_SET(set
, _TRACE_UUID_SET
)) {
3881 BT_LOGE_DUP_ATTR("uuid", "trace declaration");
3886 ret
= get_unary_uuid(&node
->u
.ctf_expression
.right
,
3890 "invalid trace declaration's UUID");
3894 ret
= bt_ctf_trace_set_uuid(ctx
->trace
, ctx
->trace_uuid
);
3897 "cannot set trace's UUID");
3901 _SET(set
, _TRACE_UUID_SET
);
3902 } else if (!strcmp(left
, "byte_order")) {
3903 /* Native byte order is already known at this stage */
3904 if (_IS_SET(set
, _TRACE_BYTE_ORDER_SET
)) {
3905 BT_LOGE_DUP_ATTR("byte_order",
3906 "trace declaration");
3911 _SET(set
, _TRACE_BYTE_ORDER_SET
);
3912 } else if (!strcmp(left
, "packet.header")) {
3913 if (_IS_SET(set
, _TRACE_PACKET_HEADER_SET
)) {
3914 BT_LOGE("duplicate \"packet.header\" entry in trace declaration");
3919 ret
= visit_type_specifier_list(ctx
,
3920 _BT_LIST_FIRST_ENTRY(
3921 &node
->u
.ctf_expression
.right
,
3922 struct ctf_node
, siblings
),
3923 &packet_header_decl
);
3925 BT_LOGE("cannot create packet header field type");
3929 assert(packet_header_decl
);
3930 ret
= bt_ctf_trace_set_packet_header_type(ctx
->trace
,
3931 packet_header_decl
);
3932 BT_PUT(packet_header_decl
);
3934 BT_LOGE("cannot set trace's packet header field type");
3938 _SET(set
, _TRACE_PACKET_HEADER_SET
);
3940 BT_LOGW("unknown attribute \"%s\" in trace declaration", left
);
3948 BT_LOGE("unknown expression in trace declaration");
3957 BT_PUT(packet_header_decl
);
3963 int visit_trace_decl(struct ctx
*ctx
, struct ctf_node
*node
)
3967 struct ctf_node
*iter
;
3968 struct bt_list_head
*decl_list
= &node
->u
.trace
.declaration_list
;
3970 if (node
->visited
) {
3974 node
->visited
= TRUE
;
3976 if (ctx
->is_trace_visited
) {
3977 BT_LOGE("duplicate \"trace\" block");
3982 ret
= ctx_push_scope(ctx
);
3984 BT_LOGE("cannot push scope");
3988 bt_list_for_each_entry(iter
, decl_list
, siblings
) {
3989 ret
= visit_trace_decl_entry(ctx
, iter
, &set
);
3998 if (!_IS_SET(&set
, _TRACE_MAJOR_SET
)) {
4000 "missing \"major\" attribute in trace declaration");
4005 if (!_IS_SET(&set
, _TRACE_MINOR_SET
)) {
4007 "missing \"minor\" attribute in trace declaration");
4012 if (!_IS_SET(&set
, _TRACE_BYTE_ORDER_SET
)) {
4013 BT_LOGE("missing \"byte_order\" attribute in trace declaration");
4018 ctx
->is_trace_visited
= TRUE
;
4028 int visit_env(struct ctx
*ctx
, struct ctf_node
*node
)
4032 struct ctf_node
*entry_node
;
4033 struct bt_list_head
*decl_list
= &node
->u
.env
.declaration_list
;
4035 if (node
->visited
) {
4039 node
->visited
= TRUE
;
4041 bt_list_for_each_entry(entry_node
, decl_list
, siblings
) {
4042 struct bt_list_head
*right_head
=
4043 &entry_node
->u
.ctf_expression
.right
;
4045 if (entry_node
->type
!= NODE_CTF_EXPRESSION
) {
4046 BT_LOGE("wrong expression in environment entry");
4051 left
= concatenate_unary_strings(
4052 &entry_node
->u
.ctf_expression
.left
);
4054 BT_LOGE("cannot get environment entry name");
4059 if (is_unary_string(right_head
)) {
4060 char *right
= concatenate_unary_strings(right_head
);
4063 BT_LOGE("unexpected unary expression for environment entry's value (\"%s\")",
4069 if (strcmp(left
, "tracer_name") == 0) {
4070 if (strncmp(right
, "lttng", 5) == 0) {
4075 printf_verbose("env.%s = \"%s\"\n", left
, right
);
4076 ret
= bt_ctf_trace_set_environment_field_string(
4077 ctx
->trace
, left
, right
);
4081 BT_LOGE("environment: cannot add entry \"%s\" to trace",
4085 } else if (is_unary_unsigned(right_head
) ||
4086 is_unary_signed(right_head
)) {
4089 if (is_unary_unsigned(right_head
)) {
4090 ret
= get_unary_unsigned(right_head
,
4093 ret
= get_unary_signed(right_head
, &v
);
4096 BT_LOGE("unexpected unary expression for environment entry's value (\"%s\")",
4102 printf_verbose("env.%s = %" PRId64
"\n", left
, v
);
4103 ret
= bt_ctf_trace_set_environment_field_integer(
4104 ctx
->trace
, left
, v
);
4106 BT_LOGE("environment: cannot add entry \"%s\" to trace",
4111 printf_verbose("%s: environment entry \"%s\" has unknown type\n",
4129 int set_trace_byte_order(struct ctx
*ctx
, struct ctf_node
*trace_node
)
4134 struct ctf_node
*node
;
4135 struct bt_list_head
*decl_list
= &trace_node
->u
.trace
.declaration_list
;
4137 bt_list_for_each_entry(node
, decl_list
, siblings
) {
4138 if (node
->type
== NODE_CTF_EXPRESSION
) {
4139 struct ctf_node
*right_node
;
4141 left
= concatenate_unary_strings(
4142 &node
->u
.ctf_expression
.left
);
4148 if (!strcmp(left
, "byte_order")) {
4149 enum bt_ctf_byte_order bo
;
4151 if (_IS_SET(&set
, _TRACE_BYTE_ORDER_SET
)) {
4152 BT_LOGE_DUP_ATTR("byte_order",
4153 "trace declaration");
4158 _SET(&set
, _TRACE_BYTE_ORDER_SET
);
4159 right_node
= _BT_LIST_FIRST_ENTRY(
4160 &node
->u
.ctf_expression
.right
,
4161 struct ctf_node
, siblings
);
4162 bo
= byte_order_from_unary_expr(right_node
);
4163 if (bo
== BT_CTF_BYTE_ORDER_UNKNOWN
) {
4164 BT_LOGE("unknown \"byte_order\" attribute in trace declaration");
4167 } else if (bo
== BT_CTF_BYTE_ORDER_NATIVE
) {
4168 BT_LOGE("\"byte_order\" attribute cannot be set to \"native\" in trace declaration");
4174 ret
= bt_ctf_trace_set_native_byte_order(
4177 BT_LOGE("cannot set trace's byte order (%d)",
4188 if (!_IS_SET(&set
, _TRACE_BYTE_ORDER_SET
)) {
4189 BT_LOGE("missing \"byte_order\" attribute in trace declaration");
4203 int visit_clock_decl_entry(struct ctx
*ctx
, struct ctf_node
*entry_node
,
4204 struct bt_ctf_clock_class
*clock
, int *set
)
4209 if (entry_node
->type
!= NODE_CTF_EXPRESSION
) {
4214 left
= concatenate_unary_strings(&entry_node
->u
.ctf_expression
.left
);
4220 if (!strcmp(left
, "name")) {
4223 if (_IS_SET(set
, _CLOCK_NAME_SET
)) {
4224 BT_LOGE_DUP_ATTR("name", "clock class declaration");
4229 right
= concatenate_unary_strings(
4230 &entry_node
->u
.ctf_expression
.right
);
4232 BT_LOGE("unexpected unary expression for clock class declaration's \"name\" attribute");
4237 ret
= bt_ctf_clock_class_set_name(clock
, right
);
4239 BT_LOGE("cannot set clock class's name");
4245 _SET(set
, _CLOCK_NAME_SET
);
4246 } else if (!strcmp(left
, "uuid")) {
4247 unsigned char uuid
[BABELTRACE_UUID_LEN
];
4249 if (_IS_SET(set
, _CLOCK_UUID_SET
)) {
4250 BT_LOGE_DUP_ATTR("uuid", "clock class declaration");
4255 ret
= get_unary_uuid(&entry_node
->u
.ctf_expression
.right
, uuid
);
4257 BT_LOGE("invalid clock class UUID");
4261 ret
= bt_ctf_clock_class_set_uuid(clock
, uuid
);
4263 BT_LOGE("cannot set clock class's UUID");
4267 _SET(set
, _CLOCK_UUID_SET
);
4268 } else if (!strcmp(left
, "description")) {
4271 if (_IS_SET(set
, _CLOCK_DESCRIPTION_SET
)) {
4272 BT_LOGE_DUP_ATTR("description", "clock class declaration");
4277 right
= concatenate_unary_strings(
4278 &entry_node
->u
.ctf_expression
.right
);
4280 BT_LOGE("unexpected unary expression for clock class's \"description\" attribute");
4285 ret
= bt_ctf_clock_class_set_description(clock
, right
);
4287 BT_LOGE("cannot set clock class's description");
4293 _SET(set
, _CLOCK_DESCRIPTION_SET
);
4294 } else if (!strcmp(left
, "freq")) {
4297 if (_IS_SET(set
, _CLOCK_FREQ_SET
)) {
4298 BT_LOGE_DUP_ATTR("freq", "clock class declaration");
4303 ret
= get_unary_unsigned(
4304 &entry_node
->u
.ctf_expression
.right
, &freq
);
4306 BT_LOGE("unexpected unary expression for clock class declaration's \"freq\" attribute");
4311 ret
= bt_ctf_clock_class_set_frequency(clock
, freq
);
4313 BT_LOGE("cannot set clock class's frequency");
4317 _SET(set
, _CLOCK_FREQ_SET
);
4318 } else if (!strcmp(left
, "precision")) {
4321 if (_IS_SET(set
, _CLOCK_PRECISION_SET
)) {
4322 BT_LOGE_DUP_ATTR("precision", "clock class declaration");
4327 ret
= get_unary_unsigned(
4328 &entry_node
->u
.ctf_expression
.right
, &precision
);
4330 BT_LOGE("unexpected unary expression for clock class declaration's \"precision\" attribute");
4335 ret
= bt_ctf_clock_class_set_precision(clock
, precision
);
4337 BT_LOGE("cannot set clock class's precision");
4341 _SET(set
, _CLOCK_PRECISION_SET
);
4342 } else if (!strcmp(left
, "offset_s")) {
4345 if (_IS_SET(set
, _CLOCK_OFFSET_S_SET
)) {
4346 BT_LOGE_DUP_ATTR("offset_s", "clock class declaration");
4351 ret
= get_unary_unsigned(
4352 &entry_node
->u
.ctf_expression
.right
, &offset_s
);
4354 BT_LOGE("unexpected unary expression for clock class declaration's \"offset_s\" attribute");
4359 ret
= bt_ctf_clock_class_set_offset_s(clock
, offset_s
);
4361 BT_LOGE("cannot set clock class's offset in seconds");
4365 _SET(set
, _CLOCK_OFFSET_S_SET
);
4366 } else if (!strcmp(left
, "offset")) {
4369 if (_IS_SET(set
, _CLOCK_OFFSET_SET
)) {
4370 BT_LOGE_DUP_ATTR("offset", "clock class declaration");
4375 ret
= get_unary_unsigned(
4376 &entry_node
->u
.ctf_expression
.right
, &offset
);
4378 BT_LOGE("unexpected unary expression for clock class declaration's \"offset\" attribute");
4383 ret
= bt_ctf_clock_class_set_offset_cycles(clock
, offset
);
4385 BT_LOGE("cannot set clock class's offset in cycles");
4389 _SET(set
, _CLOCK_OFFSET_SET
);
4390 } else if (!strcmp(left
, "absolute")) {
4391 struct ctf_node
*right
;
4393 if (_IS_SET(set
, _CLOCK_ABSOLUTE_SET
)) {
4394 BT_LOGE_DUP_ATTR("absolute", "clock class declaration");
4399 right
= _BT_LIST_FIRST_ENTRY(
4400 &entry_node
->u
.ctf_expression
.right
,
4401 struct ctf_node
, siblings
);
4402 ret
= get_boolean(right
);
4404 BT_LOGE("unexpected unary expression for clock class declaration's \"absolute\" attribute");
4409 ret
= bt_ctf_clock_class_set_is_absolute(clock
, ret
);
4411 BT_LOGE("cannot set clock class's absolute option");
4415 _SET(set
, _CLOCK_ABSOLUTE_SET
);
4417 BT_LOGW("unknown attribute \"%s\" in clock class declaration",
4433 int64_t cycles_from_ns(uint64_t frequency
, int64_t ns
)
4438 if (frequency
== 1000000000ULL) {
4441 cycles
= (uint64_t) (((double) ns
* (double) frequency
) / 1e9
);
4448 int apply_clock_class_offset(struct ctx
*ctx
, struct bt_ctf_clock_class
*clock
)
4452 int64_t offset_cycles
;
4454 freq
= bt_ctf_clock_class_get_frequency(clock
);
4455 if (freq
== -1ULL) {
4456 BT_LOGE("cannot get clock class frequency");
4461 ret
= bt_ctf_clock_class_get_offset_cycles(clock
, &offset_cycles
);
4463 BT_LOGE("cannot get offset in cycles");
4468 offset_cycles
+= cycles_from_ns(freq
, ctx
->clock_class_offset_ns
);
4469 ret
= bt_ctf_clock_class_set_offset_cycles(clock
, offset_cycles
);
4476 int visit_clock_decl(struct ctx
*ctx
, struct ctf_node
*clock_node
)
4480 struct bt_ctf_clock_class
*clock
;
4481 struct ctf_node
*entry_node
;
4482 struct bt_list_head
*decl_list
= &clock_node
->u
.clock
.declaration_list
;
4483 const char *clock_class_name
;
4485 if (clock_node
->visited
) {
4489 clock_node
->visited
= TRUE
;
4490 clock
= bt_ctf_clock_class_create(NULL
);
4492 BT_LOGE("cannot create clock");
4497 bt_list_for_each_entry(entry_node
, decl_list
, siblings
) {
4498 ret
= visit_clock_decl_entry(ctx
, entry_node
, clock
, &set
);
4504 if (!_IS_SET(&set
, _CLOCK_NAME_SET
)) {
4506 "missing \"name\" attribute in clock class declaration");
4511 clock_class_name
= bt_ctf_clock_class_get_name(clock
);
4512 assert(clock_class_name
);
4513 if (ctx
->is_lttng
&& strcmp(clock_class_name
, "monotonic") == 0) {
4515 * Old versions of LTTng forgot to set its clock class
4516 * as absolute, even if it is. This is important because
4517 * it's a condition to be able to sort notifications
4518 * from different sources.
4520 ret
= bt_ctf_clock_class_set_is_absolute(clock
, 1);
4522 BT_LOGE("cannot set clock class's absolute option");
4527 ret
= apply_clock_class_offset(ctx
, clock
);
4529 BT_LOGE("cannot apply clock class offset ");
4533 ret
= bt_ctf_trace_add_clock_class(ctx
->trace
, clock
);
4535 BT_LOGE("cannot add clock class to trace");
4546 int visit_root_decl(struct ctx
*ctx
, struct ctf_node
*root_decl_node
)
4550 if (root_decl_node
->visited
) {
4554 root_decl_node
->visited
= TRUE
;
4556 switch (root_decl_node
->type
) {
4558 ret
= visit_typedef(ctx
,
4559 root_decl_node
->u
._typedef
.type_specifier_list
,
4560 &root_decl_node
->u
._typedef
.type_declarators
);
4562 BT_LOGE("cannot add typedef in root scope");
4566 case NODE_TYPEALIAS
:
4567 ret
= visit_typealias(ctx
, root_decl_node
->u
.typealias
.target
,
4568 root_decl_node
->u
.typealias
.alias
);
4570 BT_LOGE("cannot add typealias in root scope");
4574 case NODE_TYPE_SPECIFIER_LIST
:
4576 _BT_CTF_FIELD_TYPE_INIT(decl
);
4579 * Just add the type specifier to the root
4580 * declaration scope. Put local reference.
4582 ret
= visit_type_specifier_list(ctx
, root_decl_node
, &decl
);
4601 int set_trace_name(struct ctx
*ctx
)
4605 struct bt_value
*value
= NULL
;
4607 assert(bt_ctf_trace_get_stream_class_count(ctx
->trace
) == 0);
4608 name
= g_string_new(NULL
);
4615 * Check if we have a trace environment string value named `hostname`.
4616 * If so, use it as the trace name's prefix.
4618 value
= bt_ctf_trace_get_environment_field_value_by_name(ctx
->trace
,
4620 if (bt_value_is_string(value
)) {
4621 const char *hostname
;
4623 ret
= bt_value_string_get(value
, &hostname
);
4625 g_string_append(name
, hostname
);
4627 if (ctx
->trace_name_suffix
) {
4628 g_string_append_c(name
, G_DIR_SEPARATOR
);
4632 if (ctx
->trace_name_suffix
) {
4633 g_string_append(name
, ctx
->trace_name_suffix
);
4636 ret
= bt_ctf_trace_set_name(ctx
->trace
, name
->str
);
4650 g_string_free(name
, TRUE
);
4657 int move_ctx_stream_classes_to_trace(struct ctx
*ctx
)
4660 GHashTableIter iter
;
4661 gpointer key
, stream_class
;
4663 if (g_hash_table_size(ctx
->stream_classes
) > 0 &&
4664 bt_ctf_trace_get_stream_class_count(ctx
->trace
) == 0) {
4666 * We're about to add the first stream class to the
4667 * trace. This will freeze the trace, and after this
4668 * we cannot set the name anymore. At this point,
4669 * set the trace name.
4671 ret
= set_trace_name(ctx
);
4677 g_hash_table_iter_init(&iter
, ctx
->stream_classes
);
4679 while (g_hash_table_iter_next(&iter
, &key
, &stream_class
)) {
4680 ret
= bt_ctf_trace_add_stream_class(ctx
->trace
,
4683 int64_t id
= bt_ctf_stream_class_get_id(stream_class
);
4684 BT_LOGE("cannot add stream class %" PRId64
" to trace",
4690 g_hash_table_remove_all(ctx
->stream_classes
);
4697 struct ctf_visitor_generate_ir
*ctf_visitor_generate_ir_create(
4698 int64_t clock_class_offset_ns
, const char *name
)
4701 struct ctx
*ctx
= NULL
;
4702 struct bt_ctf_trace
*trace
;
4704 trace
= bt_ctf_trace_create();
4706 BT_LOGE("cannot create trace");
4710 /* Set packet header to NULL to override the default one */
4711 ret
= bt_ctf_trace_set_packet_header_type(trace
, NULL
);
4713 BT_LOGE("cannot set initial, empty packet header structure");
4717 /* Create visitor's context */
4718 ctx
= ctx_create(trace
, clock_class_offset_ns
, name
);
4720 BT_LOGE("cannot create visitor context");
4733 return (void *) ctx
;
4737 void ctf_visitor_generate_ir_destroy(struct ctf_visitor_generate_ir
*visitor
)
4739 ctx_destroy((void *) visitor
);
4743 struct bt_ctf_trace
*ctf_visitor_generate_ir_get_trace(
4744 struct ctf_visitor_generate_ir
*visitor
)
4746 struct ctx
*ctx
= (void *) visitor
;
4750 return bt_get(ctx
->trace
);
4754 int ctf_visitor_generate_ir_visit_node(struct ctf_visitor_generate_ir
*visitor
,
4755 struct ctf_node
*node
)
4758 struct ctx
*ctx
= (void *) visitor
;
4760 printf_verbose("CTF visitor: AST -> CTF IR...\n");
4762 switch (node
->type
) {
4765 struct ctf_node
*iter
;
4766 int got_trace_decl
= FALSE
;
4767 int found_callsite
= FALSE
;
4770 * The first thing we need is the native byte order of
4771 * the trace block, because early type aliases can have
4772 * a `byte_order` attribute set to `native`. If we don't
4773 * have the native byte order yet, and we don't have any
4774 * trace block yet, then fail with EINCOMPLETE.
4776 if (ctx
->trace_bo
== BT_CTF_BYTE_ORDER_NATIVE
) {
4777 bt_list_for_each_entry(iter
, &node
->u
.root
.trace
, siblings
) {
4778 if (got_trace_decl
) {
4779 BT_LOGE("duplicate trace declaration");
4784 ret
= set_trace_byte_order(ctx
, iter
);
4786 BT_LOGE("cannot set trace's native byte order (%d)",
4791 got_trace_decl
= TRUE
;
4794 if (!got_trace_decl
) {
4800 assert(ctx
->trace_bo
== BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
||
4801 ctx
->trace_bo
== BT_CTF_BYTE_ORDER_BIG_ENDIAN
);
4802 assert(ctx
->current_scope
&&
4803 ctx
->current_scope
->parent_scope
== NULL
);
4806 bt_list_for_each_entry(iter
, &node
->u
.root
.env
, siblings
) {
4807 ret
= visit_env(ctx
, iter
);
4809 BT_LOGE("error while visiting environment block (%d)",
4815 assert(ctx
->current_scope
&&
4816 ctx
->current_scope
->parent_scope
== NULL
);
4819 * Visit clock blocks.
4821 bt_list_for_each_entry(iter
, &node
->u
.root
.clock
, siblings
) {
4822 ret
= visit_clock_decl(ctx
, iter
);
4824 BT_LOGE("error while visiting clock class declaration (%d)",
4830 assert(ctx
->current_scope
&&
4831 ctx
->current_scope
->parent_scope
== NULL
);
4834 * Visit root declarations next, as they can be used by any
4837 bt_list_for_each_entry(iter
, &node
->u
.root
.declaration_list
,
4839 ret
= visit_root_decl(ctx
, iter
);
4841 BT_LOGE("error while visiting root declaration (%d)",
4847 assert(ctx
->current_scope
&&
4848 ctx
->current_scope
->parent_scope
== NULL
);
4850 /* Callsite blocks are not supported */
4851 bt_list_for_each_entry(iter
, &node
->u
.root
.callsite
, siblings
) {
4852 found_callsite
= TRUE
;
4856 assert(ctx
->current_scope
&&
4857 ctx
->current_scope
->parent_scope
== NULL
);
4859 if (found_callsite
) {
4860 BT_LOGW("\"callsite\" blocks are not supported as of this version");
4864 bt_list_for_each_entry(iter
, &node
->u
.root
.trace
, siblings
) {
4865 ret
= visit_trace_decl(ctx
, iter
);
4867 BT_LOGE("error while visiting trace declaration");
4872 assert(ctx
->current_scope
&&
4873 ctx
->current_scope
->parent_scope
== NULL
);
4876 bt_list_for_each_entry(iter
, &node
->u
.root
.stream
, siblings
) {
4877 ret
= visit_stream_decl(ctx
, iter
);
4879 BT_LOGE("error while visiting stream declaration");
4884 assert(ctx
->current_scope
&&
4885 ctx
->current_scope
->parent_scope
== NULL
);
4888 bt_list_for_each_entry(iter
, &node
->u
.root
.event
, siblings
) {
4889 ret
= visit_event_decl(ctx
, iter
);
4891 BT_LOGE("error while visiting event declaration");
4896 assert(ctx
->current_scope
&&
4897 ctx
->current_scope
->parent_scope
== NULL
);
4901 BT_LOGE("cannot decode node type: %d", (int) node
->type
);
4906 /* Move decoded stream classes to trace, if any */
4907 ret
= move_ctx_stream_classes_to_trace(ctx
);
4909 BT_LOGE("cannot move stream classes to trace");