2 * ctf-visitor-generate-io-struct.c
4 * Common Trace Format Metadata Visitor (generate I/O structures).
6 * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
28 #include <babeltrace/babeltrace-internal.h>
29 #include <babeltrace/list.h>
30 #include <babeltrace/types.h>
31 #include <babeltrace/ctf/metadata.h>
32 #include <uuid/uuid.h>
33 #include "ctf-scanner.h"
34 #include "ctf-parser.h"
37 #define fprintf_dbg(fd, fmt, args...) fprintf(fd, "%s: " fmt, __func__, ## args)
39 #define _bt_list_first_entry(ptr, type, member) \
40 bt_list_entry((ptr)->next, type, member)
42 int opt_clock_force_correlate
;
45 struct declaration
*ctf_type_specifier_list_visit(FILE *fd
,
46 int depth
, struct ctf_node
*type_specifier_list
,
47 struct declaration_scope
*declaration_scope
,
48 struct ctf_trace
*trace
);
51 int ctf_stream_visit(FILE *fd
, int depth
, struct ctf_node
*node
,
52 struct declaration_scope
*parent_declaration_scope
, struct ctf_trace
*trace
);
55 * String returned must be freed by the caller using g_free.
58 char *concatenate_unary_strings(struct bt_list_head
*head
)
60 struct ctf_node
*node
;
64 str
= g_string_new("");
65 bt_list_for_each_entry(node
, head
, siblings
) {
68 assert(node
->type
== NODE_UNARY_EXPRESSION
);
69 assert(node
->u
.unary_expression
.type
== UNARY_STRING
);
70 assert((node
->u
.unary_expression
.link
== UNARY_LINK_UNKNOWN
)
72 switch (node
->u
.unary_expression
.link
) {
74 g_string_append(str
, ".");
77 g_string_append(str
, "->");
80 g_string_append(str
, "...");
85 src_string
= node
->u
.unary_expression
.u
.string
;
86 g_string_append(str
, src_string
);
89 return g_string_free(str
, FALSE
);
93 GQuark
get_map_clock_name_value(struct bt_list_head
*head
)
95 struct ctf_node
*node
;
96 const char *name
= NULL
;
99 bt_list_for_each_entry(node
, head
, siblings
) {
102 assert(node
->type
== NODE_UNARY_EXPRESSION
);
103 assert(node
->u
.unary_expression
.type
== UNARY_STRING
);
104 assert((node
->u
.unary_expression
.link
== UNARY_LINK_UNKNOWN
)
106 /* needs to be chained with . */
107 switch (node
->u
.unary_expression
.link
) {
110 case UNARY_ARROWLINK
:
111 case UNARY_DOTDOTDOT
:
116 src_string
= node
->u
.unary_expression
.u
.string
;
118 case 0: if (strcmp("clock", src_string
) != 0) {
122 case 1: name
= src_string
;
124 case 2: if (strcmp("value", src_string
) != 0) {
129 return 0; /* extra identifier, unknown */
133 return g_quark_from_string(name
);
137 int get_unary_unsigned(struct bt_list_head
*head
, uint64_t *value
)
139 struct ctf_node
*node
;
142 bt_list_for_each_entry(node
, head
, siblings
) {
143 assert(node
->type
== NODE_UNARY_EXPRESSION
);
144 assert(node
->u
.unary_expression
.type
== UNARY_UNSIGNED_CONSTANT
);
145 assert(node
->u
.unary_expression
.link
== UNARY_LINK_UNKNOWN
);
147 *value
= node
->u
.unary_expression
.u
.unsigned_constant
;
154 int get_unary_signed(struct bt_list_head
*head
, int64_t *value
)
156 struct ctf_node
*node
;
159 bt_list_for_each_entry(node
, head
, siblings
) {
160 assert(node
->type
== NODE_UNARY_EXPRESSION
);
161 assert(node
->u
.unary_expression
.type
== UNARY_UNSIGNED_CONSTANT
162 || node
->u
.unary_expression
.type
== UNARY_SIGNED_CONSTANT
);
163 assert(node
->u
.unary_expression
.link
== UNARY_LINK_UNKNOWN
);
165 switch (node
->u
.unary_expression
.type
) {
166 case UNARY_UNSIGNED_CONSTANT
:
167 *value
= (int64_t) node
->u
.unary_expression
.u
.unsigned_constant
;
169 case UNARY_SIGNED_CONSTANT
:
170 *value
= node
->u
.unary_expression
.u
.signed_constant
;
181 int get_unary_uuid(struct bt_list_head
*head
, uuid_t
*uuid
)
183 struct ctf_node
*node
;
187 bt_list_for_each_entry(node
, head
, siblings
) {
188 const char *src_string
;
190 assert(node
->type
== NODE_UNARY_EXPRESSION
);
191 assert(node
->u
.unary_expression
.type
== UNARY_STRING
);
192 assert(node
->u
.unary_expression
.link
== UNARY_LINK_UNKNOWN
);
194 src_string
= node
->u
.unary_expression
.u
.string
;
195 ret
= uuid_parse(src_string
, *uuid
);
201 struct ctf_stream_class
*trace_stream_lookup(struct ctf_trace
*trace
, uint64_t stream_id
)
203 if (trace
->streams
->len
<= stream_id
)
205 return g_ptr_array_index(trace
->streams
, stream_id
);
209 struct ctf_clock
*trace_clock_lookup(struct ctf_trace
*trace
, GQuark clock_name
)
211 return g_hash_table_lookup(trace
->clocks
, (gpointer
) (unsigned long) clock_name
);
215 int visit_type_specifier(FILE *fd
, struct ctf_node
*type_specifier
, GString
*str
)
217 assert(type_specifier
->type
== NODE_TYPE_SPECIFIER
);
219 switch (type_specifier
->u
.type_specifier
.type
) {
221 g_string_append(str
, "void");
224 g_string_append(str
, "char");
227 g_string_append(str
, "short");
230 g_string_append(str
, "int");
233 g_string_append(str
, "long");
236 g_string_append(str
, "float");
238 case TYPESPEC_DOUBLE
:
239 g_string_append(str
, "double");
241 case TYPESPEC_SIGNED
:
242 g_string_append(str
, "signed");
244 case TYPESPEC_UNSIGNED
:
245 g_string_append(str
, "unsigned");
248 g_string_append(str
, "bool");
250 case TYPESPEC_COMPLEX
:
251 g_string_append(str
, "_Complex");
253 case TYPESPEC_IMAGINARY
:
254 g_string_append(str
, "_Imaginary");
257 g_string_append(str
, "const");
259 case TYPESPEC_ID_TYPE
:
260 if (type_specifier
->u
.type_specifier
.id_type
)
261 g_string_append(str
, type_specifier
->u
.type_specifier
.id_type
);
263 case TYPESPEC_STRUCT
:
265 struct ctf_node
*node
= type_specifier
->u
.type_specifier
.node
;
267 if (!node
->u
._struct
.name
) {
268 fprintf(fd
, "[error] %s: unexpected empty variant name\n", __func__
);
271 g_string_append(str
, "struct ");
272 g_string_append(str
, node
->u
._struct
.name
);
275 case TYPESPEC_VARIANT
:
277 struct ctf_node
*node
= type_specifier
->u
.type_specifier
.node
;
279 if (!node
->u
.variant
.name
) {
280 fprintf(fd
, "[error] %s: unexpected empty variant name\n", __func__
);
283 g_string_append(str
, "variant ");
284 g_string_append(str
, node
->u
.variant
.name
);
289 struct ctf_node
*node
= type_specifier
->u
.type_specifier
.node
;
291 if (!node
->u
._enum
.enum_id
) {
292 fprintf(fd
, "[error] %s: unexpected empty enum ID\n", __func__
);
295 g_string_append(str
, "enum ");
296 g_string_append(str
, node
->u
._enum
.enum_id
);
299 case TYPESPEC_FLOATING_POINT
:
300 case TYPESPEC_INTEGER
:
301 case TYPESPEC_STRING
:
303 fprintf(fd
, "[error] %s: unknown specifier\n", __func__
);
310 int visit_type_specifier_list(FILE *fd
, struct ctf_node
*type_specifier_list
, GString
*str
)
312 struct ctf_node
*iter
;
313 int alias_item_nr
= 0;
316 bt_list_for_each_entry(iter
, &type_specifier_list
->u
.type_specifier_list
.head
, siblings
) {
317 if (alias_item_nr
!= 0)
318 g_string_append(str
, " ");
320 ret
= visit_type_specifier(fd
, iter
, str
);
328 GQuark
create_typealias_identifier(FILE *fd
, int depth
,
329 struct ctf_node
*type_specifier_list
,
330 struct ctf_node
*node_type_declarator
)
332 struct ctf_node
*iter
;
338 str
= g_string_new("");
339 ret
= visit_type_specifier_list(fd
, type_specifier_list
, str
);
341 g_string_free(str
, TRUE
);
344 bt_list_for_each_entry(iter
, &node_type_declarator
->u
.type_declarator
.pointers
, siblings
) {
345 g_string_append(str
, " *");
346 if (iter
->u
.pointer
.const_qualifier
)
347 g_string_append(str
, " const");
349 str_c
= g_string_free(str
, FALSE
);
350 alias_q
= g_quark_from_string(str_c
);
356 struct declaration
*ctf_type_declarator_visit(FILE *fd
, int depth
,
357 struct ctf_node
*type_specifier_list
,
359 struct ctf_node
*node_type_declarator
,
360 struct declaration_scope
*declaration_scope
,
361 struct declaration
*nested_declaration
,
362 struct ctf_trace
*trace
)
365 * Visit type declarator by first taking care of sequence/array
366 * (recursively). Then, when we get to the identifier, take care
370 if (node_type_declarator
) {
371 assert(node_type_declarator
->u
.type_declarator
.type
!= TYPEDEC_UNKNOWN
);
373 /* TODO: gcc bitfields not supported yet. */
374 if (node_type_declarator
->u
.type_declarator
.bitfield_len
!= NULL
) {
375 fprintf(fd
, "[error] %s: gcc bitfields are not supported yet.\n", __func__
);
380 if (!nested_declaration
) {
381 if (node_type_declarator
&& !bt_list_empty(&node_type_declarator
->u
.type_declarator
.pointers
)) {
385 * If we have a pointer declarator, it _has_ to be present in
386 * the typealiases (else fail).
388 alias_q
= create_typealias_identifier(fd
, depth
,
389 type_specifier_list
, node_type_declarator
);
390 nested_declaration
= lookup_declaration(alias_q
, declaration_scope
);
391 if (!nested_declaration
) {
392 fprintf(fd
, "[error] %s: cannot find typealias \"%s\".\n", __func__
, g_quark_to_string(alias_q
));
395 if (nested_declaration
->id
== CTF_TYPE_INTEGER
) {
396 struct declaration_integer
*integer_declaration
=
397 container_of(nested_declaration
, struct declaration_integer
, p
);
398 /* For base to 16 for pointers (expected pretty-print) */
399 if (!integer_declaration
->base
) {
401 * We need to do a copy of the
402 * integer declaration to modify it. There could be other references to
405 integer_declaration
= integer_declaration_new(integer_declaration
->len
,
406 integer_declaration
->byte_order
, integer_declaration
->signedness
,
407 integer_declaration
->p
.alignment
, 16, integer_declaration
->encoding
,
408 integer_declaration
->clock
);
409 nested_declaration
= &integer_declaration
->p
;
413 nested_declaration
= ctf_type_specifier_list_visit(fd
, depth
,
414 type_specifier_list
, declaration_scope
, trace
);
418 if (!node_type_declarator
)
419 return nested_declaration
;
421 if (node_type_declarator
->u
.type_declarator
.type
== TYPEDEC_ID
) {
422 if (node_type_declarator
->u
.type_declarator
.u
.id
)
423 *field_name
= g_quark_from_string(node_type_declarator
->u
.type_declarator
.u
.id
);
426 return nested_declaration
;
428 struct declaration
*declaration
;
429 struct ctf_node
*first
;
433 if (!nested_declaration
) {
434 fprintf(fd
, "[error] %s: nested type is unknown.\n", __func__
);
438 /* create array/sequence, pass nested_declaration as child. */
439 if (bt_list_empty(&node_type_declarator
->u
.type_declarator
.u
.nested
.length
)) {
440 fprintf(fd
, "[error] %s: expecting length field reference or value.\n", __func__
);
443 first
= _bt_list_first_entry(&node_type_declarator
->u
.type_declarator
.u
.nested
.length
,
444 struct ctf_node
, siblings
);
445 assert(first
->type
== NODE_UNARY_EXPRESSION
);
447 switch (first
->u
.unary_expression
.type
) {
448 case UNARY_UNSIGNED_CONSTANT
:
450 struct declaration_array
*array_declaration
;
453 len
= first
->u
.unary_expression
.u
.unsigned_constant
;
454 array_declaration
= array_declaration_new(len
, nested_declaration
,
457 if (!array_declaration
) {
458 fprintf(fd
, "[error] %s: cannot create array declaration.\n", __func__
);
461 declaration
= &array_declaration
->p
;
466 /* Lookup unsigned integer definition, create sequence */
467 char *length_name
= concatenate_unary_strings(&node_type_declarator
->u
.type_declarator
.u
.nested
.length
);
468 struct declaration_sequence
*sequence_declaration
;
470 sequence_declaration
= sequence_declaration_new(length_name
, nested_declaration
, declaration_scope
);
471 if (!sequence_declaration
) {
472 fprintf(fd
, "[error] %s: cannot create sequence declaration.\n", __func__
);
475 declaration
= &sequence_declaration
->p
;
482 /* Pass it as content of outer container */
483 declaration
= ctf_type_declarator_visit(fd
, depth
,
484 type_specifier_list
, field_name
,
485 node_type_declarator
->u
.type_declarator
.u
.nested
.type_declarator
,
486 declaration_scope
, declaration
, trace
);
492 int ctf_struct_type_declarators_visit(FILE *fd
, int depth
,
493 struct declaration_struct
*struct_declaration
,
494 struct ctf_node
*type_specifier_list
,
495 struct bt_list_head
*type_declarators
,
496 struct declaration_scope
*declaration_scope
,
497 struct ctf_trace
*trace
)
499 struct ctf_node
*iter
;
502 bt_list_for_each_entry(iter
, type_declarators
, siblings
) {
503 struct declaration
*field_declaration
;
505 field_declaration
= ctf_type_declarator_visit(fd
, depth
,
508 struct_declaration
->scope
,
510 if (!field_declaration
) {
511 fprintf(fd
, "[error] %s: unable to find struct field declaration type\n", __func__
);
515 /* Check if field with same name already exists */
516 if (struct_declaration_lookup_field_index(struct_declaration
, field_name
) >= 0) {
517 fprintf(fd
, "[error] %s: duplicate field %s in struct\n", __func__
, g_quark_to_string(field_name
));
521 struct_declaration_add_field(struct_declaration
,
522 g_quark_to_string(field_name
),
529 int ctf_variant_type_declarators_visit(FILE *fd
, int depth
,
530 struct declaration_untagged_variant
*untagged_variant_declaration
,
531 struct ctf_node
*type_specifier_list
,
532 struct bt_list_head
*type_declarators
,
533 struct declaration_scope
*declaration_scope
,
534 struct ctf_trace
*trace
)
536 struct ctf_node
*iter
;
539 bt_list_for_each_entry(iter
, type_declarators
, siblings
) {
540 struct declaration
*field_declaration
;
542 field_declaration
= ctf_type_declarator_visit(fd
, depth
,
545 untagged_variant_declaration
->scope
,
547 if (!field_declaration
) {
548 fprintf(fd
, "[error] %s: unable to find variant field declaration type\n", __func__
);
552 if (untagged_variant_declaration_get_field_from_tag(untagged_variant_declaration
, field_name
) != NULL
) {
553 fprintf(fd
, "[error] %s: duplicate field %s in variant\n", __func__
, g_quark_to_string(field_name
));
558 untagged_variant_declaration_add_field(untagged_variant_declaration
,
559 g_quark_to_string(field_name
),
566 int ctf_typedef_visit(FILE *fd
, int depth
, struct declaration_scope
*scope
,
567 struct ctf_node
*type_specifier_list
,
568 struct bt_list_head
*type_declarators
,
569 struct ctf_trace
*trace
)
571 struct ctf_node
*iter
;
574 bt_list_for_each_entry(iter
, type_declarators
, siblings
) {
575 struct declaration
*type_declaration
;
578 type_declaration
= ctf_type_declarator_visit(fd
, depth
,
582 if (!type_declaration
) {
583 fprintf(fd
, "[error] %s: problem creating type declaration\n", __func__
);
587 * Don't allow typedef and typealias of untagged
590 if (type_declaration
->id
== CTF_TYPE_UNTAGGED_VARIANT
) {
591 fprintf(fd
, "[error] %s: typedef of untagged variant is not permitted.\n", __func__
);
592 declaration_unref(type_declaration
);
595 ret
= register_declaration(identifier
, type_declaration
, scope
);
597 type_declaration
->declaration_free(type_declaration
);
605 int ctf_typealias_visit(FILE *fd
, int depth
, struct declaration_scope
*scope
,
606 struct ctf_node
*target
, struct ctf_node
*alias
,
607 struct ctf_trace
*trace
)
609 struct declaration
*type_declaration
;
610 struct ctf_node
*node
;
615 /* See ctf_visitor_type_declarator() in the semantic validator. */
618 * Create target type declaration.
621 if (bt_list_empty(&target
->u
.typealias_target
.type_declarators
))
624 node
= _bt_list_first_entry(&target
->u
.typealias_target
.type_declarators
,
625 struct ctf_node
, siblings
);
626 type_declaration
= ctf_type_declarator_visit(fd
, depth
,
627 target
->u
.typealias_target
.type_specifier_list
,
630 if (!type_declaration
) {
631 fprintf(fd
, "[error] %s: problem creating type declaration\n", __func__
);
636 * Don't allow typedef and typealias of untagged
639 if (type_declaration
->id
== CTF_TYPE_UNTAGGED_VARIANT
) {
640 fprintf(fd
, "[error] %s: typedef of untagged variant is not permitted.\n", __func__
);
641 declaration_unref(type_declaration
);
645 * The semantic validator does not check whether the target is
646 * abstract or not (if it has an identifier). Check it here.
649 fprintf(fd
, "[error] %s: expecting empty identifier\n", __func__
);
654 * Create alias identifier.
657 node
= _bt_list_first_entry(&alias
->u
.typealias_alias
.type_declarators
,
658 struct ctf_node
, siblings
);
659 alias_q
= create_typealias_identifier(fd
, depth
,
660 alias
->u
.typealias_alias
.type_specifier_list
, node
);
661 err
= register_declaration(alias_q
, type_declaration
, scope
);
667 if (type_declaration
) {
668 type_declaration
->declaration_free(type_declaration
);
674 int ctf_struct_declaration_list_visit(FILE *fd
, int depth
,
675 struct ctf_node
*iter
, struct declaration_struct
*struct_declaration
,
676 struct ctf_trace
*trace
)
680 switch (iter
->type
) {
682 /* For each declarator, declare type and add type to struct declaration scope */
683 ret
= ctf_typedef_visit(fd
, depth
,
684 struct_declaration
->scope
,
685 iter
->u
._typedef
.type_specifier_list
,
686 &iter
->u
._typedef
.type_declarators
, trace
);
691 /* Declare type with declarator and add type to struct declaration scope */
692 ret
= ctf_typealias_visit(fd
, depth
,
693 struct_declaration
->scope
,
694 iter
->u
.typealias
.target
,
695 iter
->u
.typealias
.alias
, trace
);
699 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
700 /* Add field to structure declaration */
701 ret
= ctf_struct_type_declarators_visit(fd
, depth
,
703 iter
->u
.struct_or_variant_declaration
.type_specifier_list
,
704 &iter
->u
.struct_or_variant_declaration
.type_declarators
,
705 struct_declaration
->scope
, trace
);
710 fprintf(fd
, "[error] %s: unexpected node type %d\n", __func__
, (int) iter
->type
);
717 int ctf_variant_declaration_list_visit(FILE *fd
, int depth
,
718 struct ctf_node
*iter
,
719 struct declaration_untagged_variant
*untagged_variant_declaration
,
720 struct ctf_trace
*trace
)
724 switch (iter
->type
) {
726 /* For each declarator, declare type and add type to variant declaration scope */
727 ret
= ctf_typedef_visit(fd
, depth
,
728 untagged_variant_declaration
->scope
,
729 iter
->u
._typedef
.type_specifier_list
,
730 &iter
->u
._typedef
.type_declarators
, trace
);
735 /* Declare type with declarator and add type to variant declaration scope */
736 ret
= ctf_typealias_visit(fd
, depth
,
737 untagged_variant_declaration
->scope
,
738 iter
->u
.typealias
.target
,
739 iter
->u
.typealias
.alias
, trace
);
743 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
744 /* Add field to structure declaration */
745 ret
= ctf_variant_type_declarators_visit(fd
, depth
,
746 untagged_variant_declaration
,
747 iter
->u
.struct_or_variant_declaration
.type_specifier_list
,
748 &iter
->u
.struct_or_variant_declaration
.type_declarators
,
749 untagged_variant_declaration
->scope
, trace
);
754 fprintf(fd
, "[error] %s: unexpected node type %d\n", __func__
, (int) iter
->type
);
761 struct declaration
*ctf_declaration_struct_visit(FILE *fd
,
762 int depth
, const char *name
, struct bt_list_head
*declaration_list
,
763 int has_body
, struct bt_list_head
*min_align
,
764 struct declaration_scope
*declaration_scope
,
765 struct ctf_trace
*trace
)
767 struct declaration_struct
*struct_declaration
;
768 struct ctf_node
*iter
;
772 * For named struct (without body), lookup in
773 * declaration scope. Don't take reference on struct
774 * declaration: ref is only taken upon definition.
779 lookup_struct_declaration(g_quark_from_string(name
),
781 return &struct_declaration
->p
;
783 uint64_t min_align_value
= 0;
785 /* For unnamed struct, create type */
786 /* For named struct (with body), create type and add to declaration scope */
788 if (lookup_struct_declaration(g_quark_from_string(name
),
789 declaration_scope
)) {
791 fprintf(fd
, "[error] %s: struct %s already declared in scope\n", __func__
, name
);
795 if (!bt_list_empty(min_align
)) {
796 ret
= get_unary_unsigned(min_align
, &min_align_value
);
798 fprintf(fd
, "[error] %s: unexpected unary expression for structure \"align\" attribute\n", __func__
);
803 struct_declaration
= struct_declaration_new(declaration_scope
,
805 bt_list_for_each_entry(iter
, declaration_list
, siblings
) {
806 ret
= ctf_struct_declaration_list_visit(fd
, depth
+ 1, iter
,
807 struct_declaration
, trace
);
809 goto error_free_declaration
;
812 ret
= register_struct_declaration(g_quark_from_string(name
),
817 return &struct_declaration
->p
;
819 error_free_declaration
:
820 struct_declaration
->p
.declaration_free(&struct_declaration
->p
);
826 struct declaration
*ctf_declaration_variant_visit(FILE *fd
,
827 int depth
, const char *name
, const char *choice
,
828 struct bt_list_head
*declaration_list
,
829 int has_body
, struct declaration_scope
*declaration_scope
,
830 struct ctf_trace
*trace
)
832 struct declaration_untagged_variant
*untagged_variant_declaration
;
833 struct declaration_variant
*variant_declaration
;
834 struct ctf_node
*iter
;
838 * For named variant (without body), lookup in
839 * declaration scope. Don't take reference on variant
840 * declaration: ref is only taken upon definition.
844 untagged_variant_declaration
=
845 lookup_variant_declaration(g_quark_from_string(name
),
848 /* For unnamed variant, create type */
849 /* For named variant (with body), create type and add to declaration scope */
851 if (lookup_variant_declaration(g_quark_from_string(name
),
852 declaration_scope
)) {
854 fprintf(fd
, "[error] %s: variant %s already declared in scope\n", __func__
, name
);
858 untagged_variant_declaration
= untagged_variant_declaration_new(declaration_scope
);
859 bt_list_for_each_entry(iter
, declaration_list
, siblings
) {
860 ret
= ctf_variant_declaration_list_visit(fd
, depth
+ 1, iter
,
861 untagged_variant_declaration
, trace
);
866 ret
= register_variant_declaration(g_quark_from_string(name
),
867 untagged_variant_declaration
,
873 * if tagged, create tagged variant and return. else return
877 return &untagged_variant_declaration
->p
;
879 variant_declaration
= variant_declaration_new(untagged_variant_declaration
, choice
);
880 if (!variant_declaration
)
882 declaration_unref(&untagged_variant_declaration
->p
);
883 return &variant_declaration
->p
;
886 untagged_variant_declaration
->p
.declaration_free(&untagged_variant_declaration
->p
);
891 int ctf_enumerator_list_visit(FILE *fd
, int depth
,
892 struct ctf_node
*enumerator
,
893 struct declaration_enum
*enum_declaration
)
896 struct ctf_node
*iter
;
898 q
= g_quark_from_string(enumerator
->u
.enumerator
.id
);
899 if (enum_declaration
->integer_declaration
->signedness
) {
903 bt_list_for_each_entry(iter
, &enumerator
->u
.enumerator
.values
, siblings
) {
906 assert(iter
->type
== NODE_UNARY_EXPRESSION
);
912 switch (iter
->u
.unary_expression
.type
) {
913 case UNARY_SIGNED_CONSTANT
:
914 *target
= iter
->u
.unary_expression
.u
.signed_constant
;
916 case UNARY_UNSIGNED_CONSTANT
:
917 *target
= iter
->u
.unary_expression
.u
.unsigned_constant
;
920 fprintf(fd
, "[error] %s: invalid enumerator\n", __func__
);
924 fprintf(fd
, "[error] %s: invalid enumerator\n", __func__
);
931 enum_signed_insert(enum_declaration
, start
, end
, q
);
936 bt_list_for_each_entry(iter
, &enumerator
->u
.enumerator
.values
, siblings
) {
939 assert(iter
->type
== NODE_UNARY_EXPRESSION
);
945 switch (iter
->u
.unary_expression
.type
) {
946 case UNARY_UNSIGNED_CONSTANT
:
947 *target
= iter
->u
.unary_expression
.u
.unsigned_constant
;
949 case UNARY_SIGNED_CONSTANT
:
951 * We don't accept signed constants for enums with unsigned
954 fprintf(fd
, "[error] %s: invalid enumerator (signed constant encountered, but enum container type is unsigned)\n", __func__
);
957 fprintf(fd
, "[error] %s: invalid enumerator\n", __func__
);
961 fprintf(fd
, "[error] %s: invalid enumerator\n", __func__
);
968 enum_unsigned_insert(enum_declaration
, start
, end
, q
);
974 struct declaration
*ctf_declaration_enum_visit(FILE *fd
, int depth
,
976 struct ctf_node
*container_type
,
977 struct bt_list_head
*enumerator_list
,
979 struct declaration_scope
*declaration_scope
,
980 struct ctf_trace
*trace
)
982 struct declaration
*declaration
;
983 struct declaration_enum
*enum_declaration
;
984 struct declaration_integer
*integer_declaration
;
985 struct ctf_node
*iter
;
990 * For named enum (without body), lookup in
991 * declaration scope. Don't take reference on enum
992 * declaration: ref is only taken upon definition.
997 lookup_enum_declaration(g_quark_from_string(name
),
999 return &enum_declaration
->p
;
1001 /* For unnamed enum, create type */
1002 /* For named enum (with body), create type and add to declaration scope */
1004 if (lookup_enum_declaration(g_quark_from_string(name
),
1005 declaration_scope
)) {
1007 fprintf(fd
, "[error] %s: enum %s already declared in scope\n", __func__
, name
);
1011 if (!container_type
) {
1012 declaration
= lookup_declaration(g_quark_from_static_string("int"),
1015 fprintf(fd
, "[error] %s: \"int\" type declaration missing for enumeration\n", __func__
);
1019 declaration
= ctf_type_declarator_visit(fd
, depth
,
1026 fprintf(fd
, "[error] %s: unable to create container type for enumeration\n", __func__
);
1029 if (declaration
->id
!= CTF_TYPE_INTEGER
) {
1030 fprintf(fd
, "[error] %s: container type for enumeration is not integer\n", __func__
);
1033 integer_declaration
= container_of(declaration
, struct declaration_integer
, p
);
1034 enum_declaration
= enum_declaration_new(integer_declaration
);
1035 declaration_unref(&integer_declaration
->p
); /* leave ref to enum */
1036 bt_list_for_each_entry(iter
, enumerator_list
, siblings
) {
1037 ret
= ctf_enumerator_list_visit(fd
, depth
+ 1, iter
, enum_declaration
);
1042 ret
= register_enum_declaration(g_quark_from_string(name
),
1047 return &enum_declaration
->p
;
1050 enum_declaration
->p
.declaration_free(&enum_declaration
->p
);
1055 struct declaration
*ctf_declaration_type_specifier_visit(FILE *fd
, int depth
,
1056 struct ctf_node
*type_specifier_list
,
1057 struct declaration_scope
*declaration_scope
)
1060 struct declaration
*declaration
;
1065 str
= g_string_new("");
1066 ret
= visit_type_specifier_list(fd
, type_specifier_list
, str
);
1069 str_c
= g_string_free(str
, FALSE
);
1070 id_q
= g_quark_from_string(str_c
);
1072 declaration
= lookup_declaration(id_q
, declaration_scope
);
1077 * Returns 0/1 boolean, or < 0 on error.
1080 int get_boolean(FILE *fd
, int depth
, struct ctf_node
*unary_expression
)
1082 if (unary_expression
->type
!= NODE_UNARY_EXPRESSION
) {
1083 fprintf(fd
, "[error] %s: expecting unary expression\n",
1087 switch (unary_expression
->u
.unary_expression
.type
) {
1088 case UNARY_UNSIGNED_CONSTANT
:
1089 if (unary_expression
->u
.unary_expression
.u
.unsigned_constant
== 0)
1093 case UNARY_SIGNED_CONSTANT
:
1094 if (unary_expression
->u
.unary_expression
.u
.signed_constant
== 0)
1099 if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "true"))
1101 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "TRUE"))
1103 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "false"))
1105 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "FALSE"))
1108 fprintf(fd
, "[error] %s: unexpected string \"%s\"\n",
1109 __func__
, unary_expression
->u
.unary_expression
.u
.string
);
1114 fprintf(fd
, "[error] %s: unexpected unary expression type\n",
1122 int get_trace_byte_order(FILE *fd
, int depth
, struct ctf_node
*unary_expression
)
1126 if (unary_expression
->u
.unary_expression
.type
!= UNARY_STRING
) {
1127 fprintf(fd
, "[error] %s: byte_order: expecting string\n",
1131 if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "be"))
1132 byte_order
= BIG_ENDIAN
;
1133 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "le"))
1134 byte_order
= LITTLE_ENDIAN
;
1136 fprintf(fd
, "[error] %s: unexpected string \"%s\". Should be \"native\", \"network\", \"be\" or \"le\".\n",
1137 __func__
, unary_expression
->u
.unary_expression
.u
.string
);
1144 int get_byte_order(FILE *fd
, int depth
, struct ctf_node
*unary_expression
,
1145 struct ctf_trace
*trace
)
1149 if (unary_expression
->u
.unary_expression
.type
!= UNARY_STRING
) {
1150 fprintf(fd
, "[error] %s: byte_order: expecting string\n",
1154 if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "native"))
1155 byte_order
= trace
->byte_order
;
1156 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "network"))
1157 byte_order
= BIG_ENDIAN
;
1158 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "be"))
1159 byte_order
= BIG_ENDIAN
;
1160 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "le"))
1161 byte_order
= LITTLE_ENDIAN
;
1163 fprintf(fd
, "[error] %s: unexpected string \"%s\". Should be \"native\", \"network\", \"be\" or \"le\".\n",
1164 __func__
, unary_expression
->u
.unary_expression
.u
.string
);
1171 struct declaration
*ctf_declaration_integer_visit(FILE *fd
, int depth
,
1172 struct bt_list_head
*expressions
,
1173 struct ctf_trace
*trace
)
1175 struct ctf_node
*expression
;
1176 uint64_t alignment
= 1, size
= 0;
1177 int byte_order
= trace
->byte_order
;
1179 int has_alignment
= 0, has_size
= 0;
1181 enum ctf_string_encoding encoding
= CTF_STRING_NONE
;
1182 struct ctf_clock
*clock
= NULL
;
1183 struct declaration_integer
*integer_declaration
;
1185 bt_list_for_each_entry(expression
, expressions
, siblings
) {
1186 struct ctf_node
*left
, *right
;
1188 left
= _bt_list_first_entry(&expression
->u
.ctf_expression
.left
, struct ctf_node
, siblings
);
1189 right
= _bt_list_first_entry(&expression
->u
.ctf_expression
.right
, struct ctf_node
, siblings
);
1190 assert(left
->u
.unary_expression
.type
== UNARY_STRING
);
1191 if (!strcmp(left
->u
.unary_expression
.u
.string
, "signed")) {
1192 signedness
= get_boolean(fd
, depth
, right
);
1195 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "byte_order")) {
1196 byte_order
= get_byte_order(fd
, depth
, right
, trace
);
1199 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "size")) {
1200 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
1201 fprintf(fd
, "[error] %s: size: expecting unsigned constant\n",
1205 size
= right
->u
.unary_expression
.u
.unsigned_constant
;
1207 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "align")) {
1208 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
1209 fprintf(fd
, "[error] %s: align: expecting unsigned constant\n",
1213 alignment
= right
->u
.unary_expression
.u
.unsigned_constant
;
1214 /* Make sure alignment is a power of two */
1215 if (alignment
== 0 || (alignment
& (alignment
- 1)) != 0) {
1216 fprintf(fd
, "[error] %s: align: expecting power of two\n",
1221 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "base")) {
1222 switch (right
->u
.unary_expression
.type
) {
1223 case UNARY_UNSIGNED_CONSTANT
:
1224 switch (right
->u
.unary_expression
.u
.unsigned_constant
) {
1229 base
= right
->u
.unary_expression
.u
.unsigned_constant
;
1232 fprintf(fd
, "[error] %s: base not supported (%" PRIu64
")\n",
1233 __func__
, right
->u
.unary_expression
.u
.unsigned_constant
);
1239 char *s_right
= concatenate_unary_strings(&expression
->u
.ctf_expression
.right
);
1241 fprintf(fd
, "[error] %s: unexpected unary expression for integer base\n", __func__
);
1245 if (!strcmp(s_right
, "decimal") || !strcmp(s_right
, "dec") || !strcmp(s_right
, "d")
1246 || !strcmp(s_right
, "i") || !strcmp(s_right
, "u")) {
1248 } else if (!strcmp(s_right
, "hexadecimal") || !strcmp(s_right
, "hex")
1249 || !strcmp(s_right
, "x") || !strcmp(s_right
, "X")
1250 || !strcmp(s_right
, "p")) {
1252 } else if (!strcmp(s_right
, "octal") || !strcmp(s_right
, "oct")
1253 || !strcmp(s_right
, "o")) {
1255 } else if (!strcmp(s_right
, "binary") || !strcmp(s_right
, "b")) {
1258 fprintf(fd
, "[error] %s: unexpected expression for integer base (%s)\n", __func__
, s_right
);
1267 fprintf(fd
, "[error] %s: base: expecting unsigned constant or unary string\n",
1271 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "encoding")) {
1274 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
1275 fprintf(fd
, "[error] %s: encoding: expecting unary string\n",
1279 s_right
= concatenate_unary_strings(&expression
->u
.ctf_expression
.right
);
1281 fprintf(fd
, "[error] %s: unexpected unary expression for integer base\n", __func__
);
1285 if (!strcmp(s_right
, "UTF8")
1286 || !strcmp(s_right
, "utf8")
1287 || !strcmp(s_right
, "utf-8")
1288 || !strcmp(s_right
, "UTF-8"))
1289 encoding
= CTF_STRING_UTF8
;
1290 else if (!strcmp(s_right
, "ASCII")
1291 || !strcmp(s_right
, "ascii"))
1292 encoding
= CTF_STRING_ASCII
;
1293 else if (!strcmp(s_right
, "none"))
1294 encoding
= CTF_STRING_NONE
;
1296 fprintf(fd
, "[error] %s: unknown string encoding \"%s\"\n", __func__
, s_right
);
1301 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "map")) {
1304 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
1305 fprintf(fd
, "[error] %s: map: expecting identifier\n",
1309 /* currently only support clock.name.value */
1310 clock_name
= get_map_clock_name_value(&expression
->u
.ctf_expression
.right
);
1314 s_right
= concatenate_unary_strings(&expression
->u
.ctf_expression
.right
);
1316 fprintf(fd
, "[error] %s: unexpected unary expression for integer map\n", __func__
);
1320 fprintf(fd
, "[warning] %s: unknown map %s in integer declaration\n", __func__
,
1325 clock
= trace_clock_lookup(trace
, clock_name
);
1327 fprintf(fd
, "[error] %s: map: unable to find clock %s declaration\n",
1328 __func__
, g_quark_to_string(clock_name
));
1332 fprintf(fd
, "[warning] %s: unknown attribute name %s\n",
1333 __func__
, left
->u
.unary_expression
.u
.string
);
1334 /* Fall-through after warning */
1338 fprintf(fd
, "[error] %s: missing size attribute\n", __func__
);
1341 if (!has_alignment
) {
1342 if (size
% CHAR_BIT
) {
1343 /* bit-packed alignment */
1346 /* byte-packed alignment */
1347 alignment
= CHAR_BIT
;
1350 integer_declaration
= integer_declaration_new(size
,
1351 byte_order
, signedness
, alignment
,
1352 base
, encoding
, clock
);
1353 return &integer_declaration
->p
;
1357 struct declaration
*ctf_declaration_floating_point_visit(FILE *fd
, int depth
,
1358 struct bt_list_head
*expressions
,
1359 struct ctf_trace
*trace
)
1361 struct ctf_node
*expression
;
1362 uint64_t alignment
= 1, exp_dig
= 0, mant_dig
= 0,
1363 byte_order
= trace
->byte_order
;
1364 int has_alignment
= 0, has_exp_dig
= 0, has_mant_dig
= 0;
1365 struct declaration_float
*float_declaration
;
1367 bt_list_for_each_entry(expression
, expressions
, siblings
) {
1368 struct ctf_node
*left
, *right
;
1370 left
= _bt_list_first_entry(&expression
->u
.ctf_expression
.left
, struct ctf_node
, siblings
);
1371 right
= _bt_list_first_entry(&expression
->u
.ctf_expression
.right
, struct ctf_node
, siblings
);
1372 assert(left
->u
.unary_expression
.type
== UNARY_STRING
);
1373 if (!strcmp(left
->u
.unary_expression
.u
.string
, "byte_order")) {
1374 byte_order
= get_byte_order(fd
, depth
, right
, trace
);
1377 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "exp_dig")) {
1378 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
1379 fprintf(fd
, "[error] %s: exp_dig: expecting unsigned constant\n",
1383 exp_dig
= right
->u
.unary_expression
.u
.unsigned_constant
;
1385 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "mant_dig")) {
1386 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
1387 fprintf(fd
, "[error] %s: mant_dig: expecting unsigned constant\n",
1391 mant_dig
= right
->u
.unary_expression
.u
.unsigned_constant
;
1393 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "align")) {
1394 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
1395 fprintf(fd
, "[error] %s: align: expecting unsigned constant\n",
1399 alignment
= right
->u
.unary_expression
.u
.unsigned_constant
;
1400 /* Make sure alignment is a power of two */
1401 if (alignment
== 0 || (alignment
& (alignment
- 1)) != 0) {
1402 fprintf(fd
, "[error] %s: align: expecting power of two\n",
1408 fprintf(fd
, "[warning] %s: unknown attribute name %s\n",
1409 __func__
, left
->u
.unary_expression
.u
.string
);
1410 /* Fall-through after warning */
1413 if (!has_mant_dig
) {
1414 fprintf(fd
, "[error] %s: missing mant_dig attribute\n", __func__
);
1418 fprintf(fd
, "[error] %s: missing exp_dig attribute\n", __func__
);
1421 if (!has_alignment
) {
1422 if ((mant_dig
+ exp_dig
) % CHAR_BIT
) {
1423 /* bit-packed alignment */
1426 /* byte-packed alignment */
1427 alignment
= CHAR_BIT
;
1430 float_declaration
= float_declaration_new(mant_dig
, exp_dig
,
1431 byte_order
, alignment
);
1432 return &float_declaration
->p
;
1436 struct declaration
*ctf_declaration_string_visit(FILE *fd
, int depth
,
1437 struct bt_list_head
*expressions
,
1438 struct ctf_trace
*trace
)
1440 struct ctf_node
*expression
;
1441 const char *encoding_c
= NULL
;
1442 enum ctf_string_encoding encoding
= CTF_STRING_UTF8
;
1443 struct declaration_string
*string_declaration
;
1445 bt_list_for_each_entry(expression
, expressions
, siblings
) {
1446 struct ctf_node
*left
, *right
;
1448 left
= _bt_list_first_entry(&expression
->u
.ctf_expression
.left
, struct ctf_node
, siblings
);
1449 right
= _bt_list_first_entry(&expression
->u
.ctf_expression
.right
, struct ctf_node
, siblings
);
1450 assert(left
->u
.unary_expression
.type
== UNARY_STRING
);
1451 if (!strcmp(left
->u
.unary_expression
.u
.string
, "encoding")) {
1452 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
1453 fprintf(fd
, "[error] %s: encoding: expecting string\n",
1457 encoding_c
= right
->u
.unary_expression
.u
.string
;
1459 fprintf(fd
, "[warning] %s: unknown attribute name %s\n",
1460 __func__
, left
->u
.unary_expression
.u
.string
);
1461 /* Fall-through after warning */
1464 if (encoding_c
&& !strcmp(encoding_c
, "ASCII"))
1465 encoding
= CTF_STRING_ASCII
;
1466 string_declaration
= string_declaration_new(encoding
);
1467 return &string_declaration
->p
;
1472 struct declaration
*ctf_type_specifier_list_visit(FILE *fd
,
1473 int depth
, struct ctf_node
*type_specifier_list
,
1474 struct declaration_scope
*declaration_scope
,
1475 struct ctf_trace
*trace
)
1477 struct ctf_node
*first
;
1478 struct ctf_node
*node
;
1480 assert(type_specifier_list
->type
== NODE_TYPE_SPECIFIER_LIST
);
1482 first
= _bt_list_first_entry(&type_specifier_list
->u
.type_specifier_list
.head
, struct ctf_node
, siblings
);
1484 assert(first
->type
== NODE_TYPE_SPECIFIER
);
1486 node
= first
->u
.type_specifier
.node
;
1488 switch (first
->u
.type_specifier
.type
) {
1489 case TYPESPEC_FLOATING_POINT
:
1490 return ctf_declaration_floating_point_visit(fd
, depth
,
1491 &node
->u
.floating_point
.expressions
, trace
);
1492 case TYPESPEC_INTEGER
:
1493 return ctf_declaration_integer_visit(fd
, depth
,
1494 &node
->u
.integer
.expressions
, trace
);
1495 case TYPESPEC_STRING
:
1496 return ctf_declaration_string_visit(fd
, depth
,
1497 &node
->u
.string
.expressions
, trace
);
1498 case TYPESPEC_STRUCT
:
1499 return ctf_declaration_struct_visit(fd
, depth
,
1500 node
->u
._struct
.name
,
1501 &node
->u
._struct
.declaration_list
,
1502 node
->u
._struct
.has_body
,
1503 &node
->u
._struct
.min_align
,
1506 case TYPESPEC_VARIANT
:
1507 return ctf_declaration_variant_visit(fd
, depth
,
1508 node
->u
.variant
.name
,
1509 node
->u
.variant
.choice
,
1510 &node
->u
.variant
.declaration_list
,
1511 node
->u
.variant
.has_body
,
1515 return ctf_declaration_enum_visit(fd
, depth
,
1516 node
->u
._enum
.enum_id
,
1517 node
->u
._enum
.container_type
,
1518 &node
->u
._enum
.enumerator_list
,
1519 node
->u
._enum
.has_body
,
1525 case TYPESPEC_SHORT
:
1528 case TYPESPEC_FLOAT
:
1529 case TYPESPEC_DOUBLE
:
1530 case TYPESPEC_SIGNED
:
1531 case TYPESPEC_UNSIGNED
:
1533 case TYPESPEC_COMPLEX
:
1534 case TYPESPEC_IMAGINARY
:
1535 case TYPESPEC_CONST
:
1536 case TYPESPEC_ID_TYPE
:
1537 return ctf_declaration_type_specifier_visit(fd
, depth
,
1538 type_specifier_list
, declaration_scope
);
1540 fprintf(fd
, "[error] %s: unexpected node type %d\n", __func__
, (int) first
->u
.type_specifier
.type
);
1546 int ctf_event_declaration_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_event
*event
, struct ctf_trace
*trace
)
1550 switch (node
->type
) {
1552 ret
= ctf_typedef_visit(fd
, depth
+ 1,
1553 event
->declaration_scope
,
1554 node
->u
._typedef
.type_specifier_list
,
1555 &node
->u
._typedef
.type_declarators
,
1560 case NODE_TYPEALIAS
:
1561 ret
= ctf_typealias_visit(fd
, depth
+ 1,
1562 event
->declaration_scope
,
1563 node
->u
.typealias
.target
, node
->u
.typealias
.alias
,
1568 case NODE_CTF_EXPRESSION
:
1572 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
1573 if (!strcmp(left
, "name")) {
1576 if (CTF_EVENT_FIELD_IS_SET(event
, name
)) {
1577 fprintf(fd
, "[error] %s: name already declared in event declaration\n", __func__
);
1581 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
1583 fprintf(fd
, "[error] %s: unexpected unary expression for event name\n", __func__
);
1587 event
->name
= g_quark_from_string(right
);
1589 CTF_EVENT_SET_FIELD(event
, name
);
1590 } else if (!strcmp(left
, "id")) {
1591 if (CTF_EVENT_FIELD_IS_SET(event
, id
)) {
1592 fprintf(fd
, "[error] %s: id already declared in event declaration\n", __func__
);
1596 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &event
->id
);
1598 fprintf(fd
, "[error] %s: unexpected unary expression for event id\n", __func__
);
1602 CTF_EVENT_SET_FIELD(event
, id
);
1603 } else if (!strcmp(left
, "stream_id")) {
1604 if (CTF_EVENT_FIELD_IS_SET(event
, stream_id
)) {
1605 fprintf(fd
, "[error] %s: stream_id already declared in event declaration\n", __func__
);
1609 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &event
->stream_id
);
1611 fprintf(fd
, "[error] %s: unexpected unary expression for event stream_id\n", __func__
);
1615 event
->stream
= trace_stream_lookup(trace
, event
->stream_id
);
1616 if (!event
->stream
) {
1617 fprintf(fd
, "[error] %s: stream id %" PRIu64
" cannot be found\n", __func__
, event
->stream_id
);
1621 CTF_EVENT_SET_FIELD(event
, stream_id
);
1622 } else if (!strcmp(left
, "context")) {
1623 struct declaration
*declaration
;
1625 if (event
->context_decl
) {
1626 fprintf(fd
, "[error] %s: context already declared in event declaration\n", __func__
);
1630 declaration
= ctf_type_specifier_list_visit(fd
, depth
,
1631 _bt_list_first_entry(&node
->u
.ctf_expression
.right
,
1632 struct ctf_node
, siblings
),
1633 event
->declaration_scope
, trace
);
1638 if (declaration
->id
!= CTF_TYPE_STRUCT
) {
1642 event
->context_decl
= container_of(declaration
, struct declaration_struct
, p
);
1643 } else if (!strcmp(left
, "fields")) {
1644 struct declaration
*declaration
;
1646 if (event
->fields_decl
) {
1647 fprintf(fd
, "[error] %s: fields already declared in event declaration\n", __func__
);
1651 declaration
= ctf_type_specifier_list_visit(fd
, depth
,
1652 _bt_list_first_entry(&node
->u
.ctf_expression
.right
,
1653 struct ctf_node
, siblings
),
1654 event
->declaration_scope
, trace
);
1659 if (declaration
->id
!= CTF_TYPE_STRUCT
) {
1663 event
->fields_decl
= container_of(declaration
, struct declaration_struct
, p
);
1664 } else if (!strcmp(left
, "loglevel")) {
1665 int64_t loglevel
= -1;
1667 if (CTF_EVENT_FIELD_IS_SET(event
, loglevel
)) {
1668 fprintf(fd
, "[error] %s: loglevel already declared in event declaration\n", __func__
);
1672 ret
= get_unary_signed(&node
->u
.ctf_expression
.right
, &loglevel
);
1674 fprintf(fd
, "[error] %s: unexpected unary expression for event loglevel\n", __func__
);
1678 event
->loglevel
= (int) loglevel
;
1679 CTF_EVENT_SET_FIELD(event
, loglevel
);
1681 fprintf(fd
, "[warning] %s: attribute \"%s\" is unknown in event declaration.\n", __func__
, left
);
1682 /* Fall-through after warning */
1690 /* TODO: declaration specifier should be added. */
1697 int ctf_event_visit(FILE *fd
, int depth
, struct ctf_node
*node
,
1698 struct declaration_scope
*parent_declaration_scope
, struct ctf_trace
*trace
)
1701 struct ctf_node
*iter
;
1702 struct ctf_event
*event
;
1704 event
= g_new0(struct ctf_event
, 1);
1705 event
->declaration_scope
= new_declaration_scope(parent_declaration_scope
);
1706 event
->loglevel
= -1;
1707 bt_list_for_each_entry(iter
, &node
->u
.event
.declaration_list
, siblings
) {
1708 ret
= ctf_event_declaration_visit(fd
, depth
+ 1, iter
, event
, trace
);
1712 if (!CTF_EVENT_FIELD_IS_SET(event
, name
)) {
1714 fprintf(fd
, "[error] %s: missing name field in event declaration\n", __func__
);
1717 if (!CTF_EVENT_FIELD_IS_SET(event
, stream_id
)) {
1718 /* Allow missing stream_id if there is only a single stream */
1719 switch (trace
->streams
->len
) {
1720 case 0: /* Create stream if there was none. */
1721 ret
= ctf_stream_visit(fd
, depth
, NULL
, trace
->root_declaration_scope
, trace
);
1726 event
->stream_id
= 0;
1727 event
->stream
= trace_stream_lookup(trace
, event
->stream_id
);
1731 fprintf(fd
, "[error] %s: missing stream_id field in event declaration\n", __func__
);
1735 /* Allow only one event without id per stream */
1736 if (!CTF_EVENT_FIELD_IS_SET(event
, id
)
1737 && event
->stream
->events_by_id
->len
!= 0) {
1739 fprintf(fd
, "[error] %s: missing id field in event declaration\n", __func__
);
1742 if (event
->stream
->events_by_id
->len
<= event
->id
)
1743 g_ptr_array_set_size(event
->stream
->events_by_id
, event
->id
+ 1);
1744 g_ptr_array_index(event
->stream
->events_by_id
, event
->id
) = event
;
1745 g_hash_table_insert(event
->stream
->event_quark_to_id
,
1746 (gpointer
) (unsigned long) event
->name
,
1751 if (event
->fields_decl
)
1752 declaration_unref(&event
->fields_decl
->p
);
1753 if (event
->context_decl
)
1754 declaration_unref(&event
->context_decl
->p
);
1755 free_declaration_scope(event
->declaration_scope
);
1762 int ctf_stream_declaration_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_stream_class
*stream
, struct ctf_trace
*trace
)
1766 switch (node
->type
) {
1768 ret
= ctf_typedef_visit(fd
, depth
+ 1,
1769 stream
->declaration_scope
,
1770 node
->u
._typedef
.type_specifier_list
,
1771 &node
->u
._typedef
.type_declarators
,
1776 case NODE_TYPEALIAS
:
1777 ret
= ctf_typealias_visit(fd
, depth
+ 1,
1778 stream
->declaration_scope
,
1779 node
->u
.typealias
.target
, node
->u
.typealias
.alias
,
1784 case NODE_CTF_EXPRESSION
:
1788 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
1789 if (!strcmp(left
, "id")) {
1790 if (CTF_STREAM_FIELD_IS_SET(stream
, stream_id
)) {
1791 fprintf(fd
, "[error] %s: id already declared in stream declaration\n", __func__
);
1795 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &stream
->stream_id
);
1797 fprintf(fd
, "[error] %s: unexpected unary expression for stream id\n", __func__
);
1801 CTF_STREAM_SET_FIELD(stream
, stream_id
);
1802 } else if (!strcmp(left
, "event.header")) {
1803 struct declaration
*declaration
;
1805 if (stream
->event_header_decl
) {
1806 fprintf(fd
, "[error] %s: event.header already declared in stream declaration\n", __func__
);
1810 declaration
= ctf_type_specifier_list_visit(fd
, depth
,
1811 _bt_list_first_entry(&node
->u
.ctf_expression
.right
,
1812 struct ctf_node
, siblings
),
1813 stream
->declaration_scope
, trace
);
1818 if (declaration
->id
!= CTF_TYPE_STRUCT
) {
1822 stream
->event_header_decl
= container_of(declaration
, struct declaration_struct
, p
);
1823 } else if (!strcmp(left
, "event.context")) {
1824 struct declaration
*declaration
;
1826 if (stream
->event_context_decl
) {
1827 fprintf(fd
, "[error] %s: event.context already declared in stream declaration\n", __func__
);
1831 declaration
= ctf_type_specifier_list_visit(fd
, depth
,
1832 _bt_list_first_entry(&node
->u
.ctf_expression
.right
,
1833 struct ctf_node
, siblings
),
1834 stream
->declaration_scope
, trace
);
1839 if (declaration
->id
!= CTF_TYPE_STRUCT
) {
1843 stream
->event_context_decl
= container_of(declaration
, struct declaration_struct
, p
);
1844 } else if (!strcmp(left
, "packet.context")) {
1845 struct declaration
*declaration
;
1847 if (stream
->packet_context_decl
) {
1848 fprintf(fd
, "[error] %s: packet.context already declared in stream declaration\n", __func__
);
1852 declaration
= ctf_type_specifier_list_visit(fd
, depth
,
1853 _bt_list_first_entry(&node
->u
.ctf_expression
.right
,
1854 struct ctf_node
, siblings
),
1855 stream
->declaration_scope
, trace
);
1860 if (declaration
->id
!= CTF_TYPE_STRUCT
) {
1864 stream
->packet_context_decl
= container_of(declaration
, struct declaration_struct
, p
);
1866 fprintf(fd
, "[warning] %s: attribute \"%s\" is unknown in stream declaration.\n", __func__
, left
);
1867 /* Fall-through after warning */
1876 /* TODO: declaration specifier should be added. */
1883 int ctf_stream_visit(FILE *fd
, int depth
, struct ctf_node
*node
,
1884 struct declaration_scope
*parent_declaration_scope
, struct ctf_trace
*trace
)
1887 struct ctf_node
*iter
;
1888 struct ctf_stream_class
*stream
;
1890 stream
= g_new0(struct ctf_stream_class
, 1);
1891 stream
->declaration_scope
= new_declaration_scope(parent_declaration_scope
);
1892 stream
->events_by_id
= g_ptr_array_new();
1893 stream
->event_quark_to_id
= g_hash_table_new(g_direct_hash
, g_direct_equal
);
1894 stream
->streams
= g_ptr_array_new();
1896 bt_list_for_each_entry(iter
, &node
->u
.stream
.declaration_list
, siblings
) {
1897 ret
= ctf_stream_declaration_visit(fd
, depth
+ 1, iter
, stream
, trace
);
1902 if (CTF_STREAM_FIELD_IS_SET(stream
, stream_id
)) {
1903 /* check that packet header has stream_id field. */
1904 if (!trace
->packet_header_decl
1905 || struct_declaration_lookup_field_index(trace
->packet_header_decl
, g_quark_from_static_string("stream_id")) < 0) {
1907 fprintf(fd
, "[error] %s: missing stream_id field in packet header declaration, but stream_id attribute is declared for stream.\n", __func__
);
1911 /* Allow only one id-less stream */
1912 if (trace
->streams
->len
!= 0) {
1914 fprintf(fd
, "[error] %s: missing id field in stream declaration\n", __func__
);
1917 stream
->stream_id
= 0;
1919 if (trace
->streams
->len
<= stream
->stream_id
)
1920 g_ptr_array_set_size(trace
->streams
, stream
->stream_id
+ 1);
1921 g_ptr_array_index(trace
->streams
, stream
->stream_id
) = stream
;
1922 stream
->trace
= trace
;
1927 if (stream
->event_header_decl
)
1928 declaration_unref(&stream
->event_header_decl
->p
);
1929 if (stream
->event_context_decl
)
1930 declaration_unref(&stream
->event_context_decl
->p
);
1931 if (stream
->packet_context_decl
)
1932 declaration_unref(&stream
->packet_context_decl
->p
);
1933 g_ptr_array_free(stream
->streams
, TRUE
);
1934 g_ptr_array_free(stream
->events_by_id
, TRUE
);
1935 g_hash_table_destroy(stream
->event_quark_to_id
);
1936 free_declaration_scope(stream
->declaration_scope
);
1942 int ctf_trace_declaration_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_trace
*trace
)
1946 switch (node
->type
) {
1948 ret
= ctf_typedef_visit(fd
, depth
+ 1,
1949 trace
->declaration_scope
,
1950 node
->u
._typedef
.type_specifier_list
,
1951 &node
->u
._typedef
.type_declarators
,
1956 case NODE_TYPEALIAS
:
1957 ret
= ctf_typealias_visit(fd
, depth
+ 1,
1958 trace
->declaration_scope
,
1959 node
->u
.typealias
.target
, node
->u
.typealias
.alias
,
1964 case NODE_CTF_EXPRESSION
:
1968 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
1969 if (!strcmp(left
, "major")) {
1970 if (CTF_TRACE_FIELD_IS_SET(trace
, major
)) {
1971 fprintf(fd
, "[error] %s: major already declared in trace declaration\n", __func__
);
1975 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &trace
->major
);
1977 fprintf(fd
, "[error] %s: unexpected unary expression for trace major number\n", __func__
);
1981 CTF_TRACE_SET_FIELD(trace
, major
);
1982 } else if (!strcmp(left
, "minor")) {
1983 if (CTF_TRACE_FIELD_IS_SET(trace
, minor
)) {
1984 fprintf(fd
, "[error] %s: minor already declared in trace declaration\n", __func__
);
1988 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &trace
->minor
);
1990 fprintf(fd
, "[error] %s: unexpected unary expression for trace minor number\n", __func__
);
1994 CTF_TRACE_SET_FIELD(trace
, minor
);
1995 } else if (!strcmp(left
, "uuid")) {
1998 ret
= get_unary_uuid(&node
->u
.ctf_expression
.right
, &uuid
);
2000 fprintf(fd
, "[error] %s: unexpected unary expression for trace uuid\n", __func__
);
2004 if (CTF_TRACE_FIELD_IS_SET(trace
, uuid
)
2005 && uuid_compare(uuid
, trace
->uuid
)) {
2006 fprintf(fd
, "[error] %s: uuid mismatch\n", __func__
);
2010 memcpy(trace
->uuid
, uuid
, sizeof(uuid
));
2012 CTF_TRACE_SET_FIELD(trace
, uuid
);
2013 } else if (!strcmp(left
, "byte_order")) {
2014 struct ctf_node
*right
;
2017 right
= _bt_list_first_entry(&node
->u
.ctf_expression
.right
, struct ctf_node
, siblings
);
2018 byte_order
= get_trace_byte_order(fd
, depth
, right
);
2022 if (CTF_TRACE_FIELD_IS_SET(trace
, byte_order
)
2023 && byte_order
!= trace
->byte_order
) {
2024 fprintf(fd
, "[error] %s: endianness mismatch\n", __func__
);
2028 if (byte_order
!= trace
->byte_order
) {
2029 trace
->byte_order
= byte_order
;
2031 * We need to restart
2032 * construction of the
2033 * intermediate representation.
2035 trace
->field_mask
= 0;
2036 CTF_TRACE_SET_FIELD(trace
, byte_order
);
2041 CTF_TRACE_SET_FIELD(trace
, byte_order
);
2042 } else if (!strcmp(left
, "packet.header")) {
2043 struct declaration
*declaration
;
2045 if (trace
->packet_header_decl
) {
2046 fprintf(fd
, "[error] %s: packet.header already declared in trace declaration\n", __func__
);
2050 declaration
= ctf_type_specifier_list_visit(fd
, depth
,
2051 _bt_list_first_entry(&node
->u
.ctf_expression
.right
,
2052 struct ctf_node
, siblings
),
2053 trace
->declaration_scope
, trace
);
2058 if (declaration
->id
!= CTF_TYPE_STRUCT
) {
2062 trace
->packet_header_decl
= container_of(declaration
, struct declaration_struct
, p
);
2064 fprintf(fd
, "[warning] %s: attribute \"%s\" is unknown in trace declaration.\n", __func__
, left
);
2073 /* TODO: declaration specifier should be added. */
2080 int ctf_trace_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_trace
*trace
)
2083 struct ctf_node
*iter
;
2085 if (trace
->declaration_scope
)
2087 trace
->declaration_scope
= new_declaration_scope(trace
->root_declaration_scope
);
2088 trace
->streams
= g_ptr_array_new();
2089 bt_list_for_each_entry(iter
, &node
->u
.trace
.declaration_list
, siblings
) {
2090 ret
= ctf_trace_declaration_visit(fd
, depth
+ 1, iter
, trace
);
2094 if (!CTF_TRACE_FIELD_IS_SET(trace
, major
)) {
2096 fprintf(fd
, "[error] %s: missing major field in trace declaration\n", __func__
);
2099 if (!CTF_TRACE_FIELD_IS_SET(trace
, minor
)) {
2101 fprintf(fd
, "[error] %s: missing minor field in trace declaration\n", __func__
);
2104 if (!CTF_TRACE_FIELD_IS_SET(trace
, uuid
)) {
2106 fprintf(fd
, "[error] %s: missing uuid field in trace declaration\n", __func__
);
2109 if (!CTF_TRACE_FIELD_IS_SET(trace
, byte_order
)) {
2111 fprintf(fd
, "[error] %s: missing byte_order field in trace declaration\n", __func__
);
2115 if (!CTF_TRACE_FIELD_IS_SET(trace
, byte_order
)) {
2116 /* check that the packet header contains a "magic" field */
2117 if (!trace
->packet_header_decl
2118 || struct_declaration_lookup_field_index(trace
->packet_header_decl
, g_quark_from_static_string("magic")) < 0) {
2120 fprintf(fd
, "[error] %s: missing both byte_order and packet header magic number in trace declaration\n", __func__
);
2127 if (trace
->packet_header_decl
) {
2128 declaration_unref(&trace
->packet_header_decl
->p
);
2129 trace
->packet_header_decl
= NULL
;
2131 g_ptr_array_free(trace
->streams
, TRUE
);
2132 free_declaration_scope(trace
->declaration_scope
);
2133 trace
->declaration_scope
= NULL
;
2138 int ctf_clock_declaration_visit(FILE *fd
, int depth
, struct ctf_node
*node
,
2139 struct ctf_clock
*clock
, struct ctf_trace
*trace
)
2143 switch (node
->type
) {
2144 case NODE_CTF_EXPRESSION
:
2148 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
2149 if (!strcmp(left
, "name")) {
2152 if (CTF_CLOCK_FIELD_IS_SET(clock
, name
)) {
2153 fprintf(fd
, "[error] %s: name already declared in clock declaration\n", __func__
);
2157 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
2159 fprintf(fd
, "[error] %s: unexpected unary expression for clock name\n", __func__
);
2163 clock
->name
= g_quark_from_string(right
);
2165 CTF_CLOCK_SET_FIELD(clock
, name
);
2166 } else if (!strcmp(left
, "uuid")) {
2170 fprintf(fd
, "[error] %s: uuid already declared in clock declaration\n", __func__
);
2174 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
2176 fprintf(fd
, "[error] %s: unexpected unary expression for clock uuid\n", __func__
);
2180 clock
->uuid
= g_quark_from_string(right
);
2182 } else if (!strcmp(left
, "description")) {
2185 if (clock
->description
) {
2186 fprintf(fd
, "[warning] %s: duplicated clock description\n", __func__
);
2187 goto error
; /* ret is 0, so not an actual error, just warn. */
2189 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
2191 fprintf(fd
, "[warning] %s: unexpected unary expression for clock description\n", __func__
);
2192 goto error
; /* ret is 0, so not an actual error, just warn. */
2194 clock
->description
= right
;
2195 } else if (!strcmp(left
, "freq")) {
2196 if (CTF_CLOCK_FIELD_IS_SET(clock
, freq
)) {
2197 fprintf(fd
, "[error] %s: freq already declared in clock declaration\n", __func__
);
2201 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &clock
->freq
);
2203 fprintf(fd
, "[error] %s: unexpected unary expression for clock freq\n", __func__
);
2207 CTF_CLOCK_SET_FIELD(clock
, freq
);
2208 } else if (!strcmp(left
, "precision")) {
2209 if (clock
->precision
) {
2210 fprintf(fd
, "[error] %s: precision already declared in clock declaration\n", __func__
);
2214 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &clock
->precision
);
2216 fprintf(fd
, "[error] %s: unexpected unary expression for clock precision\n", __func__
);
2220 } else if (!strcmp(left
, "offset_s")) {
2221 if (clock
->offset_s
) {
2222 fprintf(fd
, "[error] %s: offset_s already declared in clock declaration\n", __func__
);
2226 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &clock
->offset_s
);
2228 fprintf(fd
, "[error] %s: unexpected unary expression for clock offset_s\n", __func__
);
2232 } else if (!strcmp(left
, "offset")) {
2233 if (clock
->offset
) {
2234 fprintf(fd
, "[error] %s: offset already declared in clock declaration\n", __func__
);
2238 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &clock
->offset
);
2240 fprintf(fd
, "[error] %s: unexpected unary expression for clock offset\n", __func__
);
2244 } else if (!strcmp(left
, "absolute")) {
2245 struct ctf_node
*right
;
2247 right
= _bt_list_first_entry(&node
->u
.ctf_expression
.right
, struct ctf_node
, siblings
);
2248 ret
= get_boolean(fd
, depth
, right
);
2250 fprintf(fd
, "[error] %s: unexpected \"absolute\" right member\n", __func__
);
2254 clock
->absolute
= ret
;
2256 fprintf(fd
, "[warning] %s: attribute \"%s\" is unknown in clock declaration.\n", __func__
, left
);
2265 /* TODO: declaration specifier should be added. */
2272 int ctf_clock_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_trace
*trace
)
2275 struct ctf_node
*iter
;
2276 struct ctf_clock
*clock
;
2278 clock
= g_new0(struct ctf_clock
, 1);
2279 /* Default clock frequency is set to 1000000000 */
2280 clock
->freq
= 1000000000ULL;
2281 bt_list_for_each_entry(iter
, &node
->u
.clock
.declaration_list
, siblings
) {
2282 ret
= ctf_clock_declaration_visit(fd
, depth
+ 1, iter
, clock
, trace
);
2286 if (opt_clock_force_correlate
) {
2288 * User requested to forcibly correlate the clock
2289 * sources, even if we have no correlatation
2292 if (!clock
->absolute
) {
2293 fprintf(fd
, "[warning] Forcibly correlating trace clock sources (--clock-force-correlate).\n");
2295 clock
->absolute
= 1;
2297 if (!CTF_CLOCK_FIELD_IS_SET(clock
, name
)) {
2299 fprintf(fd
, "[error] %s: missing namefield in clock declaration\n", __func__
);
2302 if (g_hash_table_size(trace
->clocks
) > 0) {
2303 fprintf(fd
, "[error] Only CTF traces with a single clock description are supported by this babeltrace version.\n");
2307 trace
->single_clock
= clock
;
2308 g_hash_table_insert(trace
->clocks
, (gpointer
) (unsigned long) clock
->name
, clock
);
2312 g_free(clock
->description
);
2318 void ctf_clock_default(FILE *fd
, int depth
, struct ctf_trace
*trace
)
2320 struct ctf_clock
*clock
;
2322 clock
= g_new0(struct ctf_clock
, 1);
2323 clock
->name
= g_quark_from_string("monotonic");
2325 clock
->description
= g_strdup("Default clock");
2326 /* Default clock frequency is set to 1000000000 */
2327 clock
->freq
= 1000000000ULL;
2328 if (opt_clock_force_correlate
) {
2330 * User requested to forcibly correlate the clock
2331 * sources, even if we have no correlatation
2334 if (!clock
->absolute
) {
2335 fprintf(fd
, "[warning] Forcibly correlating trace clock sources (--clock-force-correlate).\n");
2337 clock
->absolute
= 1;
2339 clock
->absolute
= 0; /* Not an absolute reference across traces */
2342 trace
->single_clock
= clock
;
2343 g_hash_table_insert(trace
->clocks
, (gpointer
) (unsigned long) clock
->name
, clock
);
2347 void clock_free(gpointer data
)
2349 struct ctf_clock
*clock
= data
;
2351 g_free(clock
->description
);
2356 int ctf_env_declaration_visit(FILE *fd
, int depth
, struct ctf_node
*node
,
2357 struct ctf_trace
*trace
)
2360 struct ctf_tracer_env
*env
= &trace
->env
;
2362 switch (node
->type
) {
2363 case NODE_CTF_EXPRESSION
:
2367 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
2368 if (!strcmp(left
, "vpid")) {
2371 if (env
->vpid
!= -1) {
2372 fprintf(fd
, "[error] %s: vpid already declared in env declaration\n", __func__
);
2373 goto error
; /* ret is 0, so not an actual error, just warn. */
2375 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &v
);
2377 fprintf(fd
, "[error] %s: unexpected unary expression for env vpid\n", __func__
);
2378 goto error
; /* ret is 0, so not an actual error, just warn. */
2380 env
->vpid
= (int) v
;
2381 printf_verbose("env.vpid = %d\n", env
->vpid
);
2382 } else if (!strcmp(left
, "procname")) {
2385 if (env
->procname
[0]) {
2386 fprintf(fd
, "[warning] %s: duplicated env procname\n", __func__
);
2387 goto error
; /* ret is 0, so not an actual error, just warn. */
2389 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
2391 fprintf(fd
, "[warning] %s: unexpected unary expression for env procname\n", __func__
);
2392 goto error
; /* ret is 0, so not an actual error, just warn. */
2394 strncpy(env
->procname
, right
, TRACER_ENV_LEN
);
2395 env
->procname
[TRACER_ENV_LEN
- 1] = '\0';
2396 printf_verbose("env.procname = %s\n", env
->procname
);
2397 } else if (!strcmp(left
, "domain")) {
2400 if (env
->domain
[0]) {
2401 fprintf(fd
, "[warning] %s: duplicated env domain\n", __func__
);
2402 goto error
; /* ret is 0, so not an actual error, just warn. */
2404 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
2406 fprintf(fd
, "[warning] %s: unexpected unary expression for env domain\n", __func__
);
2407 goto error
; /* ret is 0, so not an actual error, just warn. */
2409 strncpy(env
->domain
, right
, TRACER_ENV_LEN
);
2410 env
->domain
[TRACER_ENV_LEN
- 1] = '\0';
2411 printf_verbose("env.domain = %s\n", env
->domain
);
2412 } else if (!strcmp(left
, "sysname")) {
2415 if (env
->sysname
[0]) {
2416 fprintf(fd
, "[warning] %s: duplicated env sysname\n", __func__
);
2417 goto error
; /* ret is 0, so not an actual error, just warn. */
2419 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
2421 fprintf(fd
, "[warning] %s: unexpected unary expression for env sysname\n", __func__
);
2422 goto error
; /* ret is 0, so not an actual error, just warn. */
2424 strncpy(env
->sysname
, right
, TRACER_ENV_LEN
);
2425 env
->sysname
[TRACER_ENV_LEN
- 1] = '\0';
2426 printf_verbose("env.sysname = %s\n", env
->sysname
);
2427 } else if (!strcmp(left
, "release")) {
2430 if (env
->release
[0]) {
2431 fprintf(fd
, "[warning] %s: duplicated env release\n", __func__
);
2432 goto error
; /* ret is 0, so not an actual error, just warn. */
2434 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
2436 fprintf(fd
, "[warning] %s: unexpected unary expression for env release\n", __func__
);
2437 goto error
; /* ret is 0, so not an actual error, just warn. */
2439 strncpy(env
->release
, right
, TRACER_ENV_LEN
);
2440 env
->release
[TRACER_ENV_LEN
- 1] = '\0';
2441 printf_verbose("env.release = %s\n", env
->release
);
2442 } else if (!strcmp(left
, "version")) {
2445 if (env
->version
[0]) {
2446 fprintf(fd
, "[warning] %s: duplicated env version\n", __func__
);
2447 goto error
; /* ret is 0, so not an actual error, just warn. */
2449 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
2451 fprintf(fd
, "[warning] %s: unexpected unary expression for env version\n", __func__
);
2452 goto error
; /* ret is 0, so not an actual error, just warn. */
2454 strncpy(env
->version
, right
, TRACER_ENV_LEN
);
2455 env
->version
[TRACER_ENV_LEN
- 1] = '\0';
2456 printf_verbose("env.version = %s\n", env
->version
);
2458 printf_verbose("%s: attribute \"%s\" is unknown in environment declaration.\n", __func__
, left
);
2473 int ctf_env_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_trace
*trace
)
2476 struct ctf_node
*iter
;
2478 trace
->env
.vpid
= -1;
2479 trace
->env
.procname
[0] = '\0';
2480 trace
->env
.domain
[0] = '\0';
2481 trace
->env
.sysname
[0] = '\0';
2482 trace
->env
.release
[0] = '\0';
2483 trace
->env
.version
[0] = '\0';
2484 bt_list_for_each_entry(iter
, &node
->u
.env
.declaration_list
, siblings
) {
2485 ret
= ctf_env_declaration_visit(fd
, depth
+ 1, iter
, trace
);
2494 int ctf_root_declaration_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_trace
*trace
)
2498 switch (node
->type
) {
2500 ret
= ctf_typedef_visit(fd
, depth
+ 1,
2501 trace
->root_declaration_scope
,
2502 node
->u
._typedef
.type_specifier_list
,
2503 &node
->u
._typedef
.type_declarators
,
2508 case NODE_TYPEALIAS
:
2509 ret
= ctf_typealias_visit(fd
, depth
+ 1,
2510 trace
->root_declaration_scope
,
2511 node
->u
.typealias
.target
, node
->u
.typealias
.alias
,
2516 case NODE_TYPE_SPECIFIER_LIST
:
2518 struct declaration
*declaration
;
2521 * Just add the type specifier to the root scope
2522 * declaration scope. Release local reference.
2524 declaration
= ctf_type_specifier_list_visit(fd
, depth
+ 1,
2525 node
, trace
->root_declaration_scope
, trace
);
2528 declaration_unref(declaration
);
2538 /* TODO: missing metadata "destroy" (memory leak) */
2539 int ctf_visitor_construct_metadata(FILE *fd
, int depth
, struct ctf_node
*node
,
2540 struct ctf_trace
*trace
, int byte_order
)
2543 struct ctf_node
*iter
;
2544 int env_clock_done
= 0;
2546 printf_verbose("CTF visitor: metadata construction... ");
2547 trace
->byte_order
= byte_order
;
2548 trace
->clocks
= g_hash_table_new_full(g_direct_hash
, g_direct_equal
,
2552 trace
->root_declaration_scope
= new_declaration_scope(NULL
);
2554 switch (node
->type
) {
2556 if (!env_clock_done
) {
2558 * declarations need to query clock hash table,
2559 * so clock need to be treated first.
2561 if (bt_list_empty(&node
->u
.root
.clock
)) {
2562 ctf_clock_default(fd
, depth
+ 1, trace
);
2564 bt_list_for_each_entry(iter
, &node
->u
.root
.clock
, siblings
) {
2565 ret
= ctf_clock_visit(fd
, depth
+ 1, iter
,
2568 fprintf(fd
, "[error] %s: clock declaration error\n", __func__
);
2575 bt_list_for_each_entry(iter
, &node
->u
.root
.declaration_list
,
2577 ret
= ctf_root_declaration_visit(fd
, depth
+ 1, iter
, trace
);
2579 fprintf(fd
, "[error] %s: root declaration error\n", __func__
);
2583 bt_list_for_each_entry(iter
, &node
->u
.root
.trace
, siblings
) {
2584 ret
= ctf_trace_visit(fd
, depth
+ 1, iter
, trace
);
2585 if (ret
== -EINTR
) {
2586 free_declaration_scope(trace
->root_declaration_scope
);
2588 * Need to restart creation of type
2589 * definitions, aliases and
2590 * trace header declarations.
2595 fprintf(fd
, "[error] %s: trace declaration error\n", __func__
);
2599 if (!trace
->streams
) {
2600 fprintf(fd
, "[error] %s: missing trace declaration\n", __func__
);
2604 bt_list_for_each_entry(iter
, &node
->u
.root
.env
, siblings
) {
2605 ret
= ctf_env_visit(fd
, depth
+ 1, iter
, trace
);
2607 fprintf(fd
, "[error] %s: env declaration error\n", __func__
);
2611 bt_list_for_each_entry(iter
, &node
->u
.root
.stream
, siblings
) {
2612 ret
= ctf_stream_visit(fd
, depth
+ 1, iter
,
2613 trace
->root_declaration_scope
, trace
);
2615 fprintf(fd
, "[error] %s: stream declaration error\n", __func__
);
2619 bt_list_for_each_entry(iter
, &node
->u
.root
.event
, siblings
) {
2620 ret
= ctf_event_visit(fd
, depth
+ 1, iter
,
2621 trace
->root_declaration_scope
, trace
);
2623 fprintf(fd
, "[error] %s: event declaration error\n", __func__
);
2630 fprintf(fd
, "[error] %s: unknown node type %d\n", __func__
,
2635 printf_verbose("done.\n");
2639 free_declaration_scope(trace
->root_declaration_scope
);
2640 g_hash_table_destroy(trace
->clocks
);