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.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 _cds_list_first_entry(ptr, type, member) \
40 cds_list_entry((ptr)->next, type, member)
43 struct declaration
*ctf_type_specifier_list_visit(FILE *fd
,
44 int depth
, struct ctf_node
*type_specifier_list
,
45 struct declaration_scope
*declaration_scope
,
46 struct ctf_trace
*trace
);
49 int ctf_stream_visit(FILE *fd
, int depth
, struct ctf_node
*node
,
50 struct declaration_scope
*parent_declaration_scope
, struct ctf_trace
*trace
);
53 * String returned must be freed by the caller using g_free.
56 char *concatenate_unary_strings(struct cds_list_head
*head
)
58 struct ctf_node
*node
;
62 str
= g_string_new("");
63 cds_list_for_each_entry(node
, head
, siblings
) {
66 assert(node
->type
== NODE_UNARY_EXPRESSION
);
67 assert(node
->u
.unary_expression
.type
== UNARY_STRING
);
68 assert((node
->u
.unary_expression
.link
== UNARY_LINK_UNKNOWN
)
70 switch (node
->u
.unary_expression
.link
) {
72 g_string_append(str
, ".");
75 g_string_append(str
, "->");
78 g_string_append(str
, "...");
83 src_string
= node
->u
.unary_expression
.u
.string
;
84 g_string_append(str
, src_string
);
87 return g_string_free(str
, FALSE
);
91 int get_unary_unsigned(struct cds_list_head
*head
, uint64_t *value
)
93 struct ctf_node
*node
;
96 cds_list_for_each_entry(node
, head
, siblings
) {
97 assert(node
->type
== NODE_UNARY_EXPRESSION
);
98 assert(node
->u
.unary_expression
.type
== UNARY_UNSIGNED_CONSTANT
);
99 assert(node
->u
.unary_expression
.link
== UNARY_LINK_UNKNOWN
);
101 *value
= node
->u
.unary_expression
.u
.unsigned_constant
;
108 int get_unary_uuid(struct cds_list_head
*head
, uuid_t
*uuid
)
110 struct ctf_node
*node
;
114 cds_list_for_each_entry(node
, head
, siblings
) {
115 const char *src_string
;
117 assert(node
->type
== NODE_UNARY_EXPRESSION
);
118 assert(node
->u
.unary_expression
.type
== UNARY_STRING
);
119 assert(node
->u
.unary_expression
.link
== UNARY_LINK_UNKNOWN
);
121 src_string
= node
->u
.unary_expression
.u
.string
;
122 ret
= uuid_parse(node
->u
.unary_expression
.u
.string
, *uuid
);
128 struct ctf_stream_class
*trace_stream_lookup(struct ctf_trace
*trace
, uint64_t stream_id
)
130 if (trace
->streams
->len
<= stream_id
)
132 return g_ptr_array_index(trace
->streams
, stream_id
);
136 int visit_type_specifier(FILE *fd
, struct ctf_node
*type_specifier
, GString
*str
)
138 assert(type_specifier
->type
== NODE_TYPE_SPECIFIER
);
140 switch (type_specifier
->u
.type_specifier
.type
) {
142 g_string_append(str
, "void");
145 g_string_append(str
, "char");
148 g_string_append(str
, "short");
151 g_string_append(str
, "int");
154 g_string_append(str
, "long");
157 g_string_append(str
, "float");
159 case TYPESPEC_DOUBLE
:
160 g_string_append(str
, "double");
162 case TYPESPEC_SIGNED
:
163 g_string_append(str
, "signed");
165 case TYPESPEC_UNSIGNED
:
166 g_string_append(str
, "unsigned");
169 g_string_append(str
, "bool");
171 case TYPESPEC_COMPLEX
:
172 g_string_append(str
, "_Complex");
174 case TYPESPEC_IMAGINARY
:
175 g_string_append(str
, "_Imaginary");
178 g_string_append(str
, "const");
180 case TYPESPEC_ID_TYPE
:
181 if (type_specifier
->u
.type_specifier
.id_type
)
182 g_string_append(str
, type_specifier
->u
.type_specifier
.id_type
);
184 case TYPESPEC_STRUCT
:
186 struct ctf_node
*node
= type_specifier
->u
.type_specifier
.node
;
188 if (!node
->u
._struct
.name
) {
189 fprintf(fd
, "[error] %s: unexpected empty variant name\n", __func__
);
192 g_string_append(str
, "struct ");
193 g_string_append(str
, node
->u
._struct
.name
);
196 case TYPESPEC_VARIANT
:
198 struct ctf_node
*node
= type_specifier
->u
.type_specifier
.node
;
200 if (!node
->u
.variant
.name
) {
201 fprintf(fd
, "[error] %s: unexpected empty variant name\n", __func__
);
204 g_string_append(str
, "variant ");
205 g_string_append(str
, node
->u
.variant
.name
);
210 struct ctf_node
*node
= type_specifier
->u
.type_specifier
.node
;
212 if (!node
->u
._enum
.enum_id
) {
213 fprintf(fd
, "[error] %s: unexpected empty enum ID\n", __func__
);
216 g_string_append(str
, "enum ");
217 g_string_append(str
, node
->u
._enum
.enum_id
);
220 case TYPESPEC_FLOATING_POINT
:
221 case TYPESPEC_INTEGER
:
222 case TYPESPEC_STRING
:
224 fprintf(fd
, "[error] %s: unknown specifier\n", __func__
);
231 int visit_type_specifier_list(FILE *fd
, struct ctf_node
*type_specifier_list
, GString
*str
)
233 struct ctf_node
*iter
;
234 int alias_item_nr
= 0;
237 cds_list_for_each_entry(iter
, &type_specifier_list
->u
.type_specifier_list
.head
, siblings
) {
238 if (alias_item_nr
!= 0)
239 g_string_append(str
, " ");
241 ret
= visit_type_specifier(fd
, iter
, str
);
249 GQuark
create_typealias_identifier(FILE *fd
, int depth
,
250 struct ctf_node
*type_specifier_list
,
251 struct ctf_node
*node_type_declarator
)
253 struct ctf_node
*iter
;
259 str
= g_string_new("");
260 ret
= visit_type_specifier_list(fd
, type_specifier_list
, str
);
262 g_string_free(str
, TRUE
);
265 cds_list_for_each_entry(iter
, &node_type_declarator
->u
.type_declarator
.pointers
, siblings
) {
266 g_string_append(str
, " *");
267 if (iter
->u
.pointer
.const_qualifier
)
268 g_string_append(str
, " const");
270 str_c
= g_string_free(str
, FALSE
);
271 alias_q
= g_quark_from_string(str_c
);
277 struct declaration
*ctf_type_declarator_visit(FILE *fd
, int depth
,
278 struct ctf_node
*type_specifier_list
,
280 struct ctf_node
*node_type_declarator
,
281 struct declaration_scope
*declaration_scope
,
282 struct declaration
*nested_declaration
,
283 struct ctf_trace
*trace
)
286 * Visit type declarator by first taking care of sequence/array
287 * (recursively). Then, when we get to the identifier, take care
291 if (node_type_declarator
) {
292 assert(node_type_declarator
->u
.type_declarator
.type
!= TYPEDEC_UNKNOWN
);
294 /* TODO: gcc bitfields not supported yet. */
295 if (node_type_declarator
->u
.type_declarator
.bitfield_len
!= NULL
) {
296 fprintf(fd
, "[error] %s: gcc bitfields are not supported yet.\n", __func__
);
301 if (!nested_declaration
) {
302 if (node_type_declarator
&& !cds_list_empty(&node_type_declarator
->u
.type_declarator
.pointers
)) {
306 * If we have a pointer declarator, it _has_ to be present in
307 * the typealiases (else fail).
309 alias_q
= create_typealias_identifier(fd
, depth
,
310 type_specifier_list
, node_type_declarator
);
311 nested_declaration
= lookup_declaration(alias_q
, declaration_scope
);
312 if (!nested_declaration
) {
313 fprintf(fd
, "[error] %s: cannot find typealias \"%s\".\n", __func__
, g_quark_to_string(alias_q
));
317 nested_declaration
= ctf_type_specifier_list_visit(fd
, depth
,
318 type_specifier_list
, declaration_scope
, trace
);
322 if (!node_type_declarator
)
323 return nested_declaration
;
325 if (node_type_declarator
->u
.type_declarator
.type
== TYPEDEC_ID
) {
326 if (node_type_declarator
->u
.type_declarator
.u
.id
)
327 *field_name
= g_quark_from_string(node_type_declarator
->u
.type_declarator
.u
.id
);
330 return nested_declaration
;
332 struct declaration
*declaration
;
333 struct ctf_node
*length
;
337 /* create array/sequence, pass nested_declaration as child. */
338 length
= node_type_declarator
->u
.type_declarator
.u
.nested
.length
;
340 fprintf(fd
, "[error] %s: expecting length type or value.\n", __func__
);
343 switch (length
->type
) {
344 case NODE_UNARY_EXPRESSION
:
346 struct declaration_array
*array_declaration
;
349 if (length
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
350 fprintf(fd
, "[error] %s: array: unexpected unary expression.\n", __func__
);
353 len
= length
->u
.unary_expression
.u
.unsigned_constant
;
354 array_declaration
= array_declaration_new(len
, nested_declaration
,
356 declaration
= &array_declaration
->p
;
359 case NODE_TYPE_SPECIFIER_LIST
:
361 struct declaration_sequence
*sequence_declaration
;
362 struct declaration_integer
*integer_declaration
;
364 declaration
= ctf_type_specifier_list_visit(fd
, depth
,
365 length
, declaration_scope
, trace
);
367 fprintf(fd
, "[error] %s: unable to find declaration type for sequence length\n", __func__
);
370 if (declaration
->id
!= CTF_TYPE_INTEGER
) {
371 fprintf(fd
, "[error] %s: length type for sequence is expected to be an integer (unsigned).\n", __func__
);
372 declaration_unref(declaration
);
375 integer_declaration
= container_of(declaration
, struct declaration_integer
, p
);
376 if (integer_declaration
->signedness
!= false) {
377 fprintf(fd
, "[error] %s: length type for sequence should always be an unsigned integer.\n", __func__
);
378 declaration_unref(declaration
);
382 sequence_declaration
= sequence_declaration_new(integer_declaration
,
383 nested_declaration
, declaration_scope
);
384 declaration
= &sequence_declaration
->p
;
391 /* Pass it as content of outer container */
392 declaration
= ctf_type_declarator_visit(fd
, depth
,
393 type_specifier_list
, field_name
,
394 node_type_declarator
->u
.type_declarator
.u
.nested
.type_declarator
,
395 declaration_scope
, declaration
, trace
);
401 int ctf_struct_type_declarators_visit(FILE *fd
, int depth
,
402 struct declaration_struct
*struct_declaration
,
403 struct ctf_node
*type_specifier_list
,
404 struct cds_list_head
*type_declarators
,
405 struct declaration_scope
*declaration_scope
,
406 struct ctf_trace
*trace
)
408 struct ctf_node
*iter
;
411 cds_list_for_each_entry(iter
, type_declarators
, siblings
) {
412 struct declaration
*field_declaration
;
414 field_declaration
= ctf_type_declarator_visit(fd
, depth
,
417 struct_declaration
->scope
,
419 if (!field_declaration
) {
420 fprintf(fd
, "[error] %s: unable to find struct field declaration type\n", __func__
);
423 struct_declaration_add_field(struct_declaration
,
424 g_quark_to_string(field_name
),
431 int ctf_variant_type_declarators_visit(FILE *fd
, int depth
,
432 struct declaration_untagged_variant
*untagged_variant_declaration
,
433 struct ctf_node
*type_specifier_list
,
434 struct cds_list_head
*type_declarators
,
435 struct declaration_scope
*declaration_scope
,
436 struct ctf_trace
*trace
)
438 struct ctf_node
*iter
;
441 cds_list_for_each_entry(iter
, type_declarators
, siblings
) {
442 struct declaration
*field_declaration
;
444 field_declaration
= ctf_type_declarator_visit(fd
, depth
,
447 untagged_variant_declaration
->scope
,
449 if (!field_declaration
) {
450 fprintf(fd
, "[error] %s: unable to find variant field declaration type\n", __func__
);
453 untagged_variant_declaration_add_field(untagged_variant_declaration
,
454 g_quark_to_string(field_name
),
461 int ctf_typedef_visit(FILE *fd
, int depth
, struct declaration_scope
*scope
,
462 struct ctf_node
*type_specifier_list
,
463 struct cds_list_head
*type_declarators
,
464 struct ctf_trace
*trace
)
466 struct ctf_node
*iter
;
469 cds_list_for_each_entry(iter
, type_declarators
, siblings
) {
470 struct declaration
*type_declaration
;
473 type_declaration
= ctf_type_declarator_visit(fd
, depth
,
477 if (!type_declaration
) {
478 fprintf(fd
, "[error] %s: problem creating type declaration\n", __func__
);
482 * Don't allow typedef and typealias of untagged
485 if (type_declaration
->id
== CTF_TYPE_UNTAGGED_VARIANT
) {
486 fprintf(fd
, "[error] %s: typedef of untagged variant is not permitted.\n", __func__
);
487 declaration_unref(type_declaration
);
490 ret
= register_declaration(identifier
, type_declaration
, scope
);
492 type_declaration
->declaration_free(type_declaration
);
500 int ctf_typealias_visit(FILE *fd
, int depth
, struct declaration_scope
*scope
,
501 struct ctf_node
*target
, struct ctf_node
*alias
,
502 struct ctf_trace
*trace
)
504 struct declaration
*type_declaration
;
505 struct ctf_node
*node
;
510 /* See ctf_visitor_type_declarator() in the semantic validator. */
513 * Create target type declaration.
516 if (cds_list_empty(&target
->u
.typealias_target
.type_declarators
))
519 node
= _cds_list_first_entry(&target
->u
.typealias_target
.type_declarators
,
520 struct ctf_node
, siblings
);
521 type_declaration
= ctf_type_declarator_visit(fd
, depth
,
522 target
->u
.typealias_target
.type_specifier_list
,
525 if (!type_declaration
) {
526 fprintf(fd
, "[error] %s: problem creating type declaration\n", __func__
);
531 * Don't allow typedef and typealias of untagged
534 if (type_declaration
->id
== CTF_TYPE_UNTAGGED_VARIANT
) {
535 fprintf(fd
, "[error] %s: typedef of untagged variant is not permitted.\n", __func__
);
536 declaration_unref(type_declaration
);
540 * The semantic validator does not check whether the target is
541 * abstract or not (if it has an identifier). Check it here.
544 fprintf(fd
, "[error] %s: expecting empty identifier\n", __func__
);
549 * Create alias identifier.
552 node
= _cds_list_first_entry(&alias
->u
.typealias_alias
.type_declarators
,
553 struct ctf_node
, siblings
);
554 alias_q
= create_typealias_identifier(fd
, depth
,
555 alias
->u
.typealias_alias
.type_specifier_list
, node
);
556 err
= register_declaration(alias_q
, type_declaration
, scope
);
562 type_declaration
->declaration_free(type_declaration
);
567 int ctf_struct_declaration_list_visit(FILE *fd
, int depth
,
568 struct ctf_node
*iter
, struct declaration_struct
*struct_declaration
,
569 struct ctf_trace
*trace
)
573 switch (iter
->type
) {
575 /* For each declarator, declare type and add type to struct declaration scope */
576 ret
= ctf_typedef_visit(fd
, depth
,
577 struct_declaration
->scope
,
578 iter
->u
._typedef
.type_specifier_list
,
579 &iter
->u
._typedef
.type_declarators
, trace
);
584 /* Declare type with declarator and add type to struct declaration scope */
585 ret
= ctf_typealias_visit(fd
, depth
,
586 struct_declaration
->scope
,
587 iter
->u
.typealias
.target
,
588 iter
->u
.typealias
.alias
, trace
);
592 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
593 /* Add field to structure declaration */
594 ret
= ctf_struct_type_declarators_visit(fd
, depth
,
596 iter
->u
.struct_or_variant_declaration
.type_specifier_list
,
597 &iter
->u
.struct_or_variant_declaration
.type_declarators
,
598 struct_declaration
->scope
, trace
);
603 fprintf(fd
, "[error] %s: unexpected node type %d\n", __func__
, (int) iter
->type
);
610 int ctf_variant_declaration_list_visit(FILE *fd
, int depth
,
611 struct ctf_node
*iter
,
612 struct declaration_untagged_variant
*untagged_variant_declaration
,
613 struct ctf_trace
*trace
)
617 switch (iter
->type
) {
619 /* For each declarator, declare type and add type to variant declaration scope */
620 ret
= ctf_typedef_visit(fd
, depth
,
621 untagged_variant_declaration
->scope
,
622 iter
->u
._typedef
.type_specifier_list
,
623 &iter
->u
._typedef
.type_declarators
, trace
);
628 /* Declare type with declarator and add type to variant declaration scope */
629 ret
= ctf_typealias_visit(fd
, depth
,
630 untagged_variant_declaration
->scope
,
631 iter
->u
.typealias
.target
,
632 iter
->u
.typealias
.alias
, trace
);
636 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
637 /* Add field to structure declaration */
638 ret
= ctf_variant_type_declarators_visit(fd
, depth
,
639 untagged_variant_declaration
,
640 iter
->u
.struct_or_variant_declaration
.type_specifier_list
,
641 &iter
->u
.struct_or_variant_declaration
.type_declarators
,
642 untagged_variant_declaration
->scope
, trace
);
647 fprintf(fd
, "[error] %s: unexpected node type %d\n", __func__
, (int) iter
->type
);
654 struct declaration
*ctf_declaration_struct_visit(FILE *fd
,
655 int depth
, const char *name
, struct cds_list_head
*declaration_list
,
656 int has_body
, struct declaration_scope
*declaration_scope
,
657 struct ctf_trace
*trace
)
659 struct declaration_struct
*struct_declaration
;
660 struct ctf_node
*iter
;
664 * For named struct (without body), lookup in
665 * declaration scope. Don't take reference on struct
666 * declaration: ref is only taken upon definition.
671 lookup_struct_declaration(g_quark_from_string(name
),
673 return &struct_declaration
->p
;
675 /* For unnamed struct, create type */
676 /* For named struct (with body), create type and add to declaration scope */
678 if (lookup_struct_declaration(g_quark_from_string(name
),
679 declaration_scope
)) {
681 fprintf(fd
, "[error] %s: struct %s already declared in scope\n", __func__
, name
);
685 struct_declaration
= struct_declaration_new(declaration_scope
);
686 cds_list_for_each_entry(iter
, declaration_list
, siblings
) {
687 ret
= ctf_struct_declaration_list_visit(fd
, depth
+ 1, iter
,
688 struct_declaration
, trace
);
693 ret
= register_struct_declaration(g_quark_from_string(name
),
698 return &struct_declaration
->p
;
701 struct_declaration
->p
.declaration_free(&struct_declaration
->p
);
706 struct declaration
*ctf_declaration_variant_visit(FILE *fd
,
707 int depth
, const char *name
, const char *choice
,
708 struct cds_list_head
*declaration_list
,
709 int has_body
, struct declaration_scope
*declaration_scope
,
710 struct ctf_trace
*trace
)
712 struct declaration_untagged_variant
*untagged_variant_declaration
;
713 struct declaration_variant
*variant_declaration
;
714 struct ctf_node
*iter
;
718 * For named variant (without body), lookup in
719 * declaration scope. Don't take reference on variant
720 * declaration: ref is only taken upon definition.
724 untagged_variant_declaration
=
725 lookup_variant_declaration(g_quark_from_string(name
),
728 /* For unnamed variant, create type */
729 /* For named variant (with body), create type and add to declaration scope */
731 if (lookup_variant_declaration(g_quark_from_string(name
),
732 declaration_scope
)) {
734 fprintf(fd
, "[error] %s: variant %s already declared in scope\n", __func__
, name
);
738 untagged_variant_declaration
= untagged_variant_declaration_new(declaration_scope
);
739 cds_list_for_each_entry(iter
, declaration_list
, siblings
) {
740 ret
= ctf_variant_declaration_list_visit(fd
, depth
+ 1, iter
,
741 untagged_variant_declaration
, trace
);
746 ret
= register_variant_declaration(g_quark_from_string(name
),
747 untagged_variant_declaration
,
753 * if tagged, create tagged variant and return. else return
757 return &untagged_variant_declaration
->p
;
759 variant_declaration
= variant_declaration_new(untagged_variant_declaration
, choice
);
760 if (!variant_declaration
)
762 declaration_unref(&untagged_variant_declaration
->p
);
763 return &variant_declaration
->p
;
766 untagged_variant_declaration
->p
.declaration_free(&untagged_variant_declaration
->p
);
771 int ctf_enumerator_list_visit(FILE *fd
, int depth
,
772 struct ctf_node
*enumerator
,
773 struct declaration_enum
*enum_declaration
)
776 struct ctf_node
*iter
;
778 q
= g_quark_from_string(enumerator
->u
.enumerator
.id
);
779 if (enum_declaration
->integer_declaration
->signedness
) {
783 cds_list_for_each_entry(iter
, &enumerator
->u
.enumerator
.values
, siblings
) {
786 assert(iter
->type
== NODE_UNARY_EXPRESSION
);
792 switch (iter
->u
.unary_expression
.type
) {
793 case UNARY_SIGNED_CONSTANT
:
794 *target
= iter
->u
.unary_expression
.u
.signed_constant
;
796 case UNARY_UNSIGNED_CONSTANT
:
797 *target
= iter
->u
.unary_expression
.u
.unsigned_constant
;
800 fprintf(fd
, "[error] %s: invalid enumerator\n", __func__
);
804 fprintf(fd
, "[error] %s: invalid enumerator\n", __func__
);
811 enum_signed_insert(enum_declaration
, start
, end
, q
);
816 cds_list_for_each_entry(iter
, &enumerator
->u
.enumerator
.values
, siblings
) {
819 assert(iter
->type
== NODE_UNARY_EXPRESSION
);
825 switch (iter
->u
.unary_expression
.type
) {
826 case UNARY_UNSIGNED_CONSTANT
:
827 *target
= iter
->u
.unary_expression
.u
.unsigned_constant
;
829 case UNARY_SIGNED_CONSTANT
:
831 * We don't accept signed constants for enums with unsigned
834 fprintf(fd
, "[error] %s: invalid enumerator (signed constant encountered, but enum container type is unsigned)\n", __func__
);
837 fprintf(fd
, "[error] %s: invalid enumerator\n", __func__
);
841 fprintf(fd
, "[error] %s: invalid enumerator\n", __func__
);
848 enum_unsigned_insert(enum_declaration
, start
, end
, q
);
854 struct declaration
*ctf_declaration_enum_visit(FILE *fd
, int depth
,
856 struct ctf_node
*container_type
,
857 struct cds_list_head
*enumerator_list
,
859 struct declaration_scope
*declaration_scope
,
860 struct ctf_trace
*trace
)
862 struct declaration
*declaration
;
863 struct declaration_enum
*enum_declaration
;
864 struct declaration_integer
*integer_declaration
;
865 struct ctf_node
*iter
;
870 * For named enum (without body), lookup in
871 * declaration scope. Don't take reference on enum
872 * declaration: ref is only taken upon definition.
877 lookup_enum_declaration(g_quark_from_string(name
),
879 return &enum_declaration
->p
;
881 /* For unnamed enum, create type */
882 /* For named enum (with body), create type and add to declaration scope */
884 if (lookup_enum_declaration(g_quark_from_string(name
),
885 declaration_scope
)) {
887 fprintf(fd
, "[error] %s: enum %s already declared in scope\n", __func__
, name
);
891 if (!container_type
) {
892 declaration
= lookup_declaration(g_quark_from_static_string("int"),
895 fprintf(fd
, "[error] %s: \"int\" type declaration missing for enumeration\n", __func__
);
899 declaration
= ctf_type_declarator_visit(fd
, depth
,
906 fprintf(fd
, "[error] %s: unable to create container type for enumeration\n", __func__
);
909 if (declaration
->id
!= CTF_TYPE_INTEGER
) {
910 fprintf(fd
, "[error] %s: container type for enumeration is not integer\n", __func__
);
913 integer_declaration
= container_of(declaration
, struct declaration_integer
, p
);
914 enum_declaration
= enum_declaration_new(integer_declaration
);
915 declaration_unref(&integer_declaration
->p
); /* leave ref to enum */
916 cds_list_for_each_entry(iter
, enumerator_list
, siblings
) {
917 ret
= ctf_enumerator_list_visit(fd
, depth
+ 1, iter
, enum_declaration
);
922 ret
= register_enum_declaration(g_quark_from_string(name
),
927 return &enum_declaration
->p
;
930 enum_declaration
->p
.declaration_free(&enum_declaration
->p
);
935 struct declaration
*ctf_declaration_type_specifier_visit(FILE *fd
, int depth
,
936 struct ctf_node
*type_specifier_list
,
937 struct declaration_scope
*declaration_scope
)
940 struct declaration
*declaration
;
945 str
= g_string_new("");
946 ret
= visit_type_specifier_list(fd
, type_specifier_list
, str
);
949 str_c
= g_string_free(str
, FALSE
);
950 id_q
= g_quark_from_string(str_c
);
952 declaration
= lookup_declaration(id_q
, declaration_scope
);
957 * Returns 0/1 boolean, or < 0 on error.
960 int get_boolean(FILE *fd
, int depth
, struct ctf_node
*unary_expression
)
962 if (unary_expression
->type
!= NODE_UNARY_EXPRESSION
) {
963 fprintf(fd
, "[error] %s: expecting unary expression\n",
967 switch (unary_expression
->u
.unary_expression
.type
) {
968 case UNARY_UNSIGNED_CONSTANT
:
969 if (unary_expression
->u
.unary_expression
.u
.unsigned_constant
== 0)
973 case UNARY_SIGNED_CONSTANT
:
974 if (unary_expression
->u
.unary_expression
.u
.signed_constant
== 0)
979 if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "true"))
981 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "TRUE"))
983 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "false"))
985 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "FALSE"))
988 fprintf(fd
, "[error] %s: unexpected string \"%s\"\n",
989 __func__
, unary_expression
->u
.unary_expression
.u
.string
);
994 fprintf(fd
, "[error] %s: unexpected unary expression type\n",
1002 int get_trace_byte_order(FILE *fd
, int depth
, struct ctf_node
*unary_expression
)
1006 if (unary_expression
->u
.unary_expression
.type
!= UNARY_STRING
) {
1007 fprintf(fd
, "[error] %s: byte_order: expecting string\n",
1011 if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "be"))
1012 byte_order
= BIG_ENDIAN
;
1013 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "le"))
1014 byte_order
= LITTLE_ENDIAN
;
1016 fprintf(fd
, "[error] %s: unexpected string \"%s\". Should be \"native\", \"network\", \"be\" or \"le\".\n",
1017 __func__
, unary_expression
->u
.unary_expression
.u
.string
);
1024 int get_byte_order(FILE *fd
, int depth
, struct ctf_node
*unary_expression
,
1025 struct ctf_trace
*trace
)
1029 if (unary_expression
->u
.unary_expression
.type
!= UNARY_STRING
) {
1030 fprintf(fd
, "[error] %s: byte_order: expecting string\n",
1034 if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "native"))
1035 byte_order
= trace
->byte_order
;
1036 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "network"))
1037 byte_order
= BIG_ENDIAN
;
1038 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "be"))
1039 byte_order
= BIG_ENDIAN
;
1040 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "le"))
1041 byte_order
= LITTLE_ENDIAN
;
1043 fprintf(fd
, "[error] %s: unexpected string \"%s\". Should be \"native\", \"network\", \"be\" or \"le\".\n",
1044 __func__
, unary_expression
->u
.unary_expression
.u
.string
);
1051 struct declaration
*ctf_declaration_integer_visit(FILE *fd
, int depth
,
1052 struct cds_list_head
*expressions
,
1053 struct ctf_trace
*trace
)
1055 struct ctf_node
*expression
;
1056 uint64_t alignment
, size
;
1057 int byte_order
= trace
->byte_order
;
1059 int has_alignment
= 0, has_size
= 0;
1060 struct declaration_integer
*integer_declaration
;
1062 cds_list_for_each_entry(expression
, expressions
, siblings
) {
1063 struct ctf_node
*left
, *right
;
1065 left
= _cds_list_first_entry(&expression
->u
.ctf_expression
.left
, struct ctf_node
, siblings
);
1066 right
= _cds_list_first_entry(&expression
->u
.ctf_expression
.right
, struct ctf_node
, siblings
);
1067 assert(left
->u
.unary_expression
.type
== UNARY_STRING
);
1068 if (!strcmp(left
->u
.unary_expression
.u
.string
, "signed")) {
1069 signedness
= get_boolean(fd
, depth
, right
);
1072 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "byte_order")) {
1073 byte_order
= get_byte_order(fd
, depth
, right
, trace
);
1076 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "size")) {
1077 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
1078 fprintf(fd
, "[error] %s: size: expecting unsigned constant\n",
1082 size
= right
->u
.unary_expression
.u
.unsigned_constant
;
1084 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "align")) {
1085 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
1086 fprintf(fd
, "[error] %s: align: expecting unsigned constant\n",
1090 alignment
= right
->u
.unary_expression
.u
.unsigned_constant
;
1091 /* Make sure alignment is a power of two */
1092 if (alignment
== 0 || (alignment
& (alignment
- 1)) != 0) {
1093 fprintf(fd
, "[error] %s: align: expecting power of two\n",
1099 fprintf(fd
, "[error] %s: unknown attribute name %s\n",
1100 __func__
, left
->u
.unary_expression
.u
.string
);
1105 fprintf(fd
, "[error] %s: missing size attribute\n", __func__
);
1108 if (!has_alignment
) {
1109 if (size
% CHAR_BIT
) {
1110 /* bit-packed alignment */
1113 /* byte-packed alignment */
1114 alignment
= CHAR_BIT
;
1117 integer_declaration
= integer_declaration_new(size
,
1118 byte_order
, signedness
, alignment
);
1119 return &integer_declaration
->p
;
1123 struct declaration
*ctf_declaration_floating_point_visit(FILE *fd
, int depth
,
1124 struct cds_list_head
*expressions
,
1125 struct ctf_trace
*trace
)
1127 struct ctf_node
*expression
;
1128 uint64_t alignment
, exp_dig
, mant_dig
, byte_order
= trace
->byte_order
;
1129 int has_alignment
= 0, has_exp_dig
= 0, has_mant_dig
= 0;
1130 struct declaration_float
*float_declaration
;
1132 cds_list_for_each_entry(expression
, expressions
, siblings
) {
1133 struct ctf_node
*left
, *right
;
1135 left
= _cds_list_first_entry(&expression
->u
.ctf_expression
.left
, struct ctf_node
, siblings
);
1136 right
= _cds_list_first_entry(&expression
->u
.ctf_expression
.right
, struct ctf_node
, siblings
);
1137 assert(left
->u
.unary_expression
.type
== UNARY_STRING
);
1138 if (!strcmp(left
->u
.unary_expression
.u
.string
, "byte_order")) {
1139 byte_order
= get_byte_order(fd
, depth
, right
, trace
);
1142 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "exp_dig")) {
1143 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
1144 fprintf(fd
, "[error] %s: exp_dig: expecting unsigned constant\n",
1148 exp_dig
= right
->u
.unary_expression
.u
.unsigned_constant
;
1150 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "mant_dig")) {
1151 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
1152 fprintf(fd
, "[error] %s: mant_dig: expecting unsigned constant\n",
1156 mant_dig
= right
->u
.unary_expression
.u
.unsigned_constant
;
1158 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "align")) {
1159 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
1160 fprintf(fd
, "[error] %s: align: expecting unsigned constant\n",
1164 alignment
= right
->u
.unary_expression
.u
.unsigned_constant
;
1165 /* Make sure alignment is a power of two */
1166 if (alignment
== 0 || (alignment
& (alignment
- 1)) != 0) {
1167 fprintf(fd
, "[error] %s: align: expecting power of two\n",
1173 fprintf(fd
, "[error] %s: unknown attribute name %s\n",
1174 __func__
, left
->u
.unary_expression
.u
.string
);
1178 if (!has_mant_dig
) {
1179 fprintf(fd
, "[error] %s: missing mant_dig attribute\n", __func__
);
1183 fprintf(fd
, "[error] %s: missing exp_dig attribute\n", __func__
);
1186 if (!has_alignment
) {
1187 if ((mant_dig
+ exp_dig
) % CHAR_BIT
) {
1188 /* bit-packed alignment */
1191 /* byte-packed alignment */
1192 alignment
= CHAR_BIT
;
1195 float_declaration
= float_declaration_new(mant_dig
, exp_dig
,
1196 byte_order
, alignment
);
1197 return &float_declaration
->p
;
1201 struct declaration
*ctf_declaration_string_visit(FILE *fd
, int depth
,
1202 struct cds_list_head
*expressions
,
1203 struct ctf_trace
*trace
)
1205 struct ctf_node
*expression
;
1206 const char *encoding_c
= NULL
;
1207 enum ctf_string_encoding encoding
= CTF_STRING_UTF8
;
1208 struct declaration_string
*string_declaration
;
1210 cds_list_for_each_entry(expression
, expressions
, siblings
) {
1211 struct ctf_node
*left
, *right
;
1213 left
= _cds_list_first_entry(&expression
->u
.ctf_expression
.left
, struct ctf_node
, siblings
);
1214 right
= _cds_list_first_entry(&expression
->u
.ctf_expression
.right
, struct ctf_node
, siblings
);
1215 assert(left
->u
.unary_expression
.type
== UNARY_STRING
);
1216 if (!strcmp(left
->u
.unary_expression
.u
.string
, "encoding")) {
1217 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
1218 fprintf(fd
, "[error] %s: encoding: expecting string\n",
1222 encoding_c
= right
->u
.unary_expression
.u
.string
;
1224 fprintf(fd
, "[error] %s: unknown attribute name %s\n",
1225 __func__
, left
->u
.unary_expression
.u
.string
);
1229 if (encoding_c
&& !strcmp(encoding_c
, "ASCII"))
1230 encoding
= CTF_STRING_ASCII
;
1231 string_declaration
= string_declaration_new(encoding
);
1232 return &string_declaration
->p
;
1237 struct declaration
*ctf_type_specifier_list_visit(FILE *fd
,
1238 int depth
, struct ctf_node
*type_specifier_list
,
1239 struct declaration_scope
*declaration_scope
,
1240 struct ctf_trace
*trace
)
1242 struct ctf_node
*first
;
1243 struct ctf_node
*node
;
1245 assert(type_specifier_list
->type
== NODE_TYPE_SPECIFIER_LIST
);
1247 first
= _cds_list_first_entry(&type_specifier_list
->u
.type_specifier_list
.head
, struct ctf_node
, siblings
);
1249 assert(first
->type
== NODE_TYPE_SPECIFIER
);
1251 node
= first
->u
.type_specifier
.node
;
1253 switch (first
->u
.type_specifier
.type
) {
1254 case TYPESPEC_FLOATING_POINT
:
1255 return ctf_declaration_floating_point_visit(fd
, depth
,
1256 &node
->u
.floating_point
.expressions
, trace
);
1257 case TYPESPEC_INTEGER
:
1258 return ctf_declaration_integer_visit(fd
, depth
,
1259 &node
->u
.integer
.expressions
, trace
);
1260 case TYPESPEC_STRING
:
1261 return ctf_declaration_string_visit(fd
, depth
,
1262 &node
->u
.string
.expressions
, trace
);
1263 case TYPESPEC_STRUCT
:
1264 return ctf_declaration_struct_visit(fd
, depth
,
1265 node
->u
._struct
.name
,
1266 &node
->u
._struct
.declaration_list
,
1267 node
->u
._struct
.has_body
,
1270 case TYPESPEC_VARIANT
:
1271 return ctf_declaration_variant_visit(fd
, depth
,
1272 node
->u
.variant
.name
,
1273 node
->u
.variant
.choice
,
1274 &node
->u
.variant
.declaration_list
,
1275 node
->u
.variant
.has_body
,
1279 return ctf_declaration_enum_visit(fd
, depth
,
1280 node
->u
._enum
.enum_id
,
1281 node
->u
._enum
.container_type
,
1282 &node
->u
._enum
.enumerator_list
,
1283 node
->u
._enum
.has_body
,
1289 case TYPESPEC_SHORT
:
1292 case TYPESPEC_FLOAT
:
1293 case TYPESPEC_DOUBLE
:
1294 case TYPESPEC_SIGNED
:
1295 case TYPESPEC_UNSIGNED
:
1297 case TYPESPEC_COMPLEX
:
1298 case TYPESPEC_IMAGINARY
:
1299 case TYPESPEC_CONST
:
1300 case TYPESPEC_ID_TYPE
:
1301 return ctf_declaration_type_specifier_visit(fd
, depth
,
1302 type_specifier_list
, declaration_scope
);
1304 fprintf(fd
, "[error] %s: unexpected node type %d\n", __func__
, (int) first
->u
.type_specifier
.type
);
1310 int ctf_event_declaration_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_event
*event
, struct ctf_trace
*trace
)
1314 switch (node
->type
) {
1316 ret
= ctf_typedef_visit(fd
, depth
+ 1,
1317 event
->declaration_scope
,
1318 node
->u
._typedef
.type_specifier_list
,
1319 &node
->u
._typedef
.type_declarators
,
1324 case NODE_TYPEALIAS
:
1325 ret
= ctf_typealias_visit(fd
, depth
+ 1,
1326 event
->declaration_scope
,
1327 node
->u
.typealias
.target
, node
->u
.typealias
.alias
,
1332 case NODE_CTF_EXPRESSION
:
1336 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
1337 if (!strcmp(left
, "name")) {
1340 if (CTF_EVENT_FIELD_IS_SET(event
, name
)) {
1341 fprintf(fd
, "[error] %s: name already declared in event declaration\n", __func__
);
1345 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
1347 fprintf(fd
, "[error] %s: unexpected unary expression for event name\n", __func__
);
1351 event
->name
= g_quark_from_string(right
);
1353 CTF_EVENT_SET_FIELD(event
, name
);
1354 } else if (!strcmp(left
, "id")) {
1355 if (CTF_EVENT_FIELD_IS_SET(event
, id
)) {
1356 fprintf(fd
, "[error] %s: id already declared in event declaration\n", __func__
);
1360 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &event
->id
);
1362 fprintf(fd
, "[error] %s: unexpected unary expression for event id\n", __func__
);
1366 CTF_EVENT_SET_FIELD(event
, id
);
1367 } else if (!strcmp(left
, "stream_id")) {
1368 if (CTF_EVENT_FIELD_IS_SET(event
, stream_id
)) {
1369 fprintf(fd
, "[error] %s: stream_id already declared in event declaration\n", __func__
);
1373 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &event
->stream_id
);
1375 fprintf(fd
, "[error] %s: unexpected unary expression for event stream_id\n", __func__
);
1379 event
->stream
= trace_stream_lookup(trace
, event
->stream_id
);
1380 if (!event
->stream
) {
1381 fprintf(fd
, "[error] %s: stream id %" PRIu64
" cannot be found\n", __func__
, event
->stream_id
);
1385 CTF_EVENT_SET_FIELD(event
, stream_id
);
1386 } else if (!strcmp(left
, "context")) {
1387 struct declaration
*declaration
;
1389 if (event
->context_decl
) {
1390 fprintf(fd
, "[error] %s: context already declared in event declaration\n", __func__
);
1394 declaration
= ctf_type_specifier_list_visit(fd
, depth
,
1395 _cds_list_first_entry(&node
->u
.ctf_expression
.right
,
1396 struct ctf_node
, siblings
),
1397 event
->declaration_scope
, trace
);
1402 if (declaration
->id
!= CTF_TYPE_STRUCT
) {
1406 event
->context_decl
= container_of(declaration
, struct declaration_struct
, p
);
1407 } else if (!strcmp(left
, "fields")) {
1408 struct declaration
*declaration
;
1410 if (event
->fields_decl
) {
1411 fprintf(fd
, "[error] %s: fields already declared in event declaration\n", __func__
);
1415 declaration
= ctf_type_specifier_list_visit(fd
, depth
,
1416 _cds_list_first_entry(&node
->u
.ctf_expression
.right
,
1417 struct ctf_node
, siblings
),
1418 event
->declaration_scope
, trace
);
1423 if (declaration
->id
!= CTF_TYPE_STRUCT
) {
1427 event
->fields_decl
= container_of(declaration
, struct declaration_struct
, p
);
1435 /* TODO: declaration specifier should be added. */
1442 int ctf_event_visit(FILE *fd
, int depth
, struct ctf_node
*node
,
1443 struct declaration_scope
*parent_declaration_scope
, struct ctf_trace
*trace
)
1446 struct ctf_node
*iter
;
1447 struct ctf_event
*event
;
1448 struct definition_scope
*parent_def_scope
;
1450 event
= g_new0(struct ctf_event
, 1);
1451 event
->declaration_scope
= new_declaration_scope(parent_declaration_scope
);
1452 cds_list_for_each_entry(iter
, &node
->u
.event
.declaration_list
, siblings
) {
1453 ret
= ctf_event_declaration_visit(fd
, depth
+ 1, iter
, event
, trace
);
1457 if (!CTF_EVENT_FIELD_IS_SET(event
, name
)) {
1459 fprintf(fd
, "[error] %s: missing name field in event declaration\n", __func__
);
1462 if (!CTF_EVENT_FIELD_IS_SET(event
, stream_id
)) {
1463 /* Allow missing stream_id if there is only a single stream */
1464 switch (trace
->streams
->len
) {
1465 case 0: /* Create stream if there was none. */
1466 ret
= ctf_stream_visit(fd
, depth
, NULL
, trace
->root_declaration_scope
, trace
);
1471 event
->stream_id
= 0;
1472 event
->stream
= trace_stream_lookup(trace
, event
->stream_id
);
1476 fprintf(fd
, "[error] %s: missing stream_id field in event declaration\n", __func__
);
1480 /* Allow only one event without id per stream */
1481 if (!CTF_EVENT_FIELD_IS_SET(event
, id
)
1482 && event
->stream
->events_by_id
->len
!= 0) {
1484 fprintf(fd
, "[error] %s: missing id field in event declaration\n", __func__
);
1487 if (event
->stream
->events_by_id
->len
<= event
->id
)
1488 g_ptr_array_set_size(event
->stream
->events_by_id
, event
->id
+ 1);
1489 g_ptr_array_index(event
->stream
->events_by_id
, event
->id
) = event
;
1490 g_hash_table_insert(event
->stream
->event_quark_to_id
,
1491 (gpointer
)(unsigned long) event
->name
,
1493 parent_def_scope
= event
->stream
->definition_scope
;
1494 if (event
->context_decl
) {
1497 event
->context_decl
->p
.definition_new(&event
->context_decl
->p
,
1498 parent_def_scope
, 0, 0),
1499 struct definition_struct
, p
);
1500 set_dynamic_definition_scope(&event
->context
->p
,
1501 event
->context
->scope
,
1503 parent_def_scope
= event
->context
->scope
;
1504 declaration_unref(&event
->context_decl
->p
);
1506 if (event
->fields_decl
) {
1509 event
->fields_decl
->p
.definition_new(&event
->fields_decl
->p
,
1510 parent_def_scope
, 0, 0),
1511 struct definition_struct
, p
);
1512 set_dynamic_definition_scope(&event
->fields
->p
,
1513 event
->fields
->scope
,
1515 parent_def_scope
= event
->fields
->scope
;
1516 declaration_unref(&event
->fields_decl
->p
);
1521 declaration_unref(&event
->fields_decl
->p
);
1522 declaration_unref(&event
->context_decl
->p
);
1523 free_declaration_scope(event
->declaration_scope
);
1530 int ctf_stream_declaration_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_stream_class
*stream
, struct ctf_trace
*trace
)
1534 switch (node
->type
) {
1536 ret
= ctf_typedef_visit(fd
, depth
+ 1,
1537 stream
->declaration_scope
,
1538 node
->u
._typedef
.type_specifier_list
,
1539 &node
->u
._typedef
.type_declarators
,
1544 case NODE_TYPEALIAS
:
1545 ret
= ctf_typealias_visit(fd
, depth
+ 1,
1546 stream
->declaration_scope
,
1547 node
->u
.typealias
.target
, node
->u
.typealias
.alias
,
1552 case NODE_CTF_EXPRESSION
:
1556 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
1557 if (!strcmp(left
, "id")) {
1558 if (CTF_STREAM_FIELD_IS_SET(stream
, stream_id
)) {
1559 fprintf(fd
, "[error] %s: id already declared in stream declaration\n", __func__
);
1563 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &stream
->stream_id
);
1565 fprintf(fd
, "[error] %s: unexpected unary expression for stream id\n", __func__
);
1569 CTF_STREAM_SET_FIELD(stream
, stream_id
);
1570 } else if (!strcmp(left
, "event.header")) {
1571 struct declaration
*declaration
;
1573 if (stream
->event_header_decl
) {
1574 fprintf(fd
, "[error] %s: event.header already declared in stream declaration\n", __func__
);
1578 declaration
= ctf_type_specifier_list_visit(fd
, depth
,
1579 _cds_list_first_entry(&node
->u
.ctf_expression
.right
,
1580 struct ctf_node
, siblings
),
1581 stream
->declaration_scope
, trace
);
1586 if (declaration
->id
!= CTF_TYPE_STRUCT
) {
1590 stream
->event_header_decl
= container_of(declaration
, struct declaration_struct
, p
);
1591 } else if (!strcmp(left
, "event.context")) {
1592 struct declaration
*declaration
;
1594 if (stream
->event_context_decl
) {
1595 fprintf(fd
, "[error] %s: event.context already declared in stream declaration\n", __func__
);
1599 declaration
= ctf_type_specifier_list_visit(fd
, depth
,
1600 _cds_list_first_entry(&node
->u
.ctf_expression
.right
,
1601 struct ctf_node
, siblings
),
1602 stream
->declaration_scope
, trace
);
1607 if (declaration
->id
!= CTF_TYPE_STRUCT
) {
1611 stream
->event_context_decl
= container_of(declaration
, struct declaration_struct
, p
);
1612 } else if (!strcmp(left
, "packet.context")) {
1613 struct declaration
*declaration
;
1615 if (stream
->packet_context_decl
) {
1616 fprintf(fd
, "[error] %s: packet.context already declared in stream declaration\n", __func__
);
1620 declaration
= ctf_type_specifier_list_visit(fd
, depth
,
1621 _cds_list_first_entry(&node
->u
.ctf_expression
.right
,
1622 struct ctf_node
, siblings
),
1623 stream
->declaration_scope
, trace
);
1628 if (declaration
->id
!= CTF_TYPE_STRUCT
) {
1632 stream
->packet_context_decl
= container_of(declaration
, struct declaration_struct
, p
);
1640 /* TODO: declaration specifier should be added. */
1647 int ctf_stream_visit(FILE *fd
, int depth
, struct ctf_node
*node
,
1648 struct declaration_scope
*parent_declaration_scope
, struct ctf_trace
*trace
)
1651 struct ctf_node
*iter
;
1652 struct ctf_stream_class
*stream
;
1653 struct definition_scope
*parent_def_scope
;
1655 stream
= g_new0(struct ctf_stream_class
, 1);
1656 stream
->declaration_scope
= new_declaration_scope(parent_declaration_scope
);
1657 stream
->events_by_id
= g_ptr_array_new();
1658 stream
->event_quark_to_id
= g_hash_table_new(g_direct_hash
, g_direct_equal
);
1659 stream
->files
= g_ptr_array_new();
1661 cds_list_for_each_entry(iter
, &node
->u
.stream
.declaration_list
, siblings
) {
1662 ret
= ctf_stream_declaration_visit(fd
, depth
+ 1, iter
, stream
, trace
);
1667 if (CTF_STREAM_FIELD_IS_SET(stream
, stream_id
)) {
1668 /* check that packet header has stream_id field. */
1669 if (!trace
->packet_header_decl
1670 || struct_declaration_lookup_field_index(trace
->packet_header_decl
, g_quark_from_static_string("stream_id")) < 0) {
1672 fprintf(fd
, "[error] %s: missing stream_id field in packet header declaration, but stream_id attribute is declared for stream.\n", __func__
);
1676 /* Allow only one id-less stream */
1677 if (trace
->streams
->len
!= 0) {
1679 fprintf(fd
, "[error] %s: missing id field in stream declaration\n", __func__
);
1682 stream
->stream_id
= 0;
1684 if (trace
->streams
->len
<= stream
->stream_id
)
1685 g_ptr_array_set_size(trace
->streams
, stream
->stream_id
+ 1);
1686 g_ptr_array_index(trace
->streams
, stream
->stream_id
) = stream
;
1688 parent_def_scope
= trace
->definition_scope
;
1689 if (stream
->packet_context_decl
) {
1690 stream
->packet_context
=
1692 stream
->packet_context_decl
->p
.definition_new(&stream
->packet_context_decl
->p
,
1693 parent_def_scope
, 0, 0),
1694 struct definition_struct
, p
);
1695 set_dynamic_definition_scope(&stream
->packet_context
->p
,
1696 stream
->packet_context
->scope
,
1697 "stream.packet.context");
1698 parent_def_scope
= stream
->packet_context
->scope
;
1699 declaration_unref(&stream
->packet_context_decl
->p
);
1701 if (stream
->event_header_decl
) {
1702 stream
->event_header
=
1704 stream
->event_header_decl
->p
.definition_new(&stream
->event_header_decl
->p
,
1705 parent_def_scope
, 0, 0),
1706 struct definition_struct
, p
);
1707 set_dynamic_definition_scope(&stream
->event_header
->p
,
1708 stream
->event_header
->scope
,
1709 "stream.event.header");
1710 parent_def_scope
= stream
->event_header
->scope
;
1711 declaration_unref(&stream
->event_header_decl
->p
);
1713 if (stream
->event_context_decl
) {
1714 stream
->event_context
=
1716 stream
->event_context_decl
->p
.definition_new(&stream
->event_context_decl
->p
,
1717 parent_def_scope
, 0, 0),
1718 struct definition_struct
, p
);
1719 set_dynamic_definition_scope(&stream
->event_context
->p
,
1720 stream
->event_context
->scope
,
1721 "stream.event.context");
1722 parent_def_scope
= stream
->event_context
->scope
;
1723 declaration_unref(&stream
->event_context_decl
->p
);
1725 stream
->definition_scope
= parent_def_scope
;
1730 declaration_unref(&stream
->event_header_decl
->p
);
1731 declaration_unref(&stream
->event_context_decl
->p
);
1732 declaration_unref(&stream
->packet_context_decl
->p
);
1733 g_ptr_array_free(stream
->files
, TRUE
);
1734 g_ptr_array_free(stream
->events_by_id
, TRUE
);
1735 g_hash_table_destroy(stream
->event_quark_to_id
);
1736 free_declaration_scope(stream
->declaration_scope
);
1742 int ctf_trace_declaration_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_trace
*trace
)
1746 switch (node
->type
) {
1748 ret
= ctf_typedef_visit(fd
, depth
+ 1,
1749 trace
->declaration_scope
,
1750 node
->u
._typedef
.type_specifier_list
,
1751 &node
->u
._typedef
.type_declarators
,
1756 case NODE_TYPEALIAS
:
1757 ret
= ctf_typealias_visit(fd
, depth
+ 1,
1758 trace
->declaration_scope
,
1759 node
->u
.typealias
.target
, node
->u
.typealias
.alias
,
1764 case NODE_CTF_EXPRESSION
:
1768 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
1769 if (!strcmp(left
, "major")) {
1770 if (CTF_TRACE_FIELD_IS_SET(trace
, major
)) {
1771 fprintf(fd
, "[error] %s: major already declared in trace declaration\n", __func__
);
1775 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &trace
->major
);
1777 fprintf(fd
, "[error] %s: unexpected unary expression for trace major number\n", __func__
);
1781 CTF_TRACE_SET_FIELD(trace
, major
);
1782 } else if (!strcmp(left
, "minor")) {
1783 if (CTF_TRACE_FIELD_IS_SET(trace
, minor
)) {
1784 fprintf(fd
, "[error] %s: minor already declared in trace declaration\n", __func__
);
1788 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &trace
->minor
);
1790 fprintf(fd
, "[error] %s: unexpected unary expression for trace minor number\n", __func__
);
1794 CTF_TRACE_SET_FIELD(trace
, minor
);
1795 } else if (!strcmp(left
, "uuid")) {
1796 if (CTF_TRACE_FIELD_IS_SET(trace
, uuid
)) {
1797 fprintf(fd
, "[error] %s: uuid already declared in trace declaration\n", __func__
);
1801 ret
= get_unary_uuid(&node
->u
.ctf_expression
.right
, &trace
->uuid
);
1803 fprintf(fd
, "[error] %s: unexpected unary expression for trace uuid\n", __func__
);
1807 CTF_TRACE_SET_FIELD(trace
, uuid
);
1808 } else if (!strcmp(left
, "byte_order")) {
1809 struct ctf_node
*right
;
1812 if (CTF_TRACE_FIELD_IS_SET(trace
, byte_order
)) {
1813 fprintf(fd
, "[error] %s: endianness already declared in trace declaration\n", __func__
);
1817 right
= _cds_list_first_entry(&node
->u
.ctf_expression
.right
, struct ctf_node
, siblings
);
1818 byte_order
= get_trace_byte_order(fd
, depth
, right
);
1821 trace
->byte_order
= byte_order
;
1822 CTF_TRACE_SET_FIELD(trace
, byte_order
);
1823 } else if (!strcmp(left
, "packet.header")) {
1824 struct declaration
*declaration
;
1826 if (trace
->packet_header_decl
) {
1827 fprintf(fd
, "[error] %s: packet.header already declared in trace declaration\n", __func__
);
1831 declaration
= ctf_type_specifier_list_visit(fd
, depth
,
1832 _cds_list_first_entry(&node
->u
.ctf_expression
.right
,
1833 struct ctf_node
, siblings
),
1834 trace
->declaration_scope
, trace
);
1839 if (declaration
->id
!= CTF_TYPE_STRUCT
) {
1843 trace
->packet_header_decl
= container_of(declaration
, struct declaration_struct
, p
);
1851 /* TODO: declaration specifier should be added. */
1858 int ctf_trace_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_trace
*trace
)
1860 struct definition_scope
*parent_def_scope
;
1862 struct ctf_node
*iter
;
1864 if (trace
->declaration_scope
)
1866 trace
->declaration_scope
= new_declaration_scope(trace
->root_declaration_scope
);
1867 trace
->streams
= g_ptr_array_new();
1868 cds_list_for_each_entry(iter
, &node
->u
.trace
.declaration_list
, siblings
) {
1869 ret
= ctf_trace_declaration_visit(fd
, depth
+ 1, iter
, trace
);
1873 if (!CTF_TRACE_FIELD_IS_SET(trace
, major
)) {
1875 fprintf(fd
, "[error] %s: missing major field in trace declaration\n", __func__
);
1878 if (!CTF_TRACE_FIELD_IS_SET(trace
, minor
)) {
1880 fprintf(fd
, "[error] %s: missing minor field in trace declaration\n", __func__
);
1883 if (!CTF_TRACE_FIELD_IS_SET(trace
, uuid
)) {
1885 fprintf(fd
, "[error] %s: missing uuid field in trace declaration\n", __func__
);
1888 if (!CTF_TRACE_FIELD_IS_SET(trace
, byte_order
)) {
1890 fprintf(fd
, "[error] %s: missing byte_order field in trace declaration\n", __func__
);
1894 parent_def_scope
= NULL
;
1895 if (trace
->packet_header_decl
) {
1896 trace
->packet_header
=
1898 trace
->packet_header_decl
->p
.definition_new(&trace
->packet_header_decl
->p
,
1899 parent_def_scope
, 0, 0),
1900 struct definition_struct
, p
);
1901 set_dynamic_definition_scope(&trace
->packet_header
->p
,
1902 trace
->packet_header
->scope
,
1903 "trace.packet.header");
1904 parent_def_scope
= trace
->packet_header
->scope
;
1905 declaration_unref(&trace
->packet_header_decl
->p
);
1907 trace
->definition_scope
= parent_def_scope
;
1909 if (!CTF_TRACE_FIELD_IS_SET(trace
, byte_order
)) {
1910 /* check that the packet header contains a "magic" field */
1911 if (!trace
->packet_header
1912 || struct_declaration_lookup_field_index(trace
->packet_header_decl
, g_quark_from_static_string("magic")) < 0) {
1914 fprintf(fd
, "[error] %s: missing both byte_order and packet header magic number in trace declaration\n", __func__
);
1915 goto error_free_def
;
1921 definition_unref(&trace
->packet_header
->p
);
1923 g_ptr_array_free(trace
->streams
, TRUE
);
1924 free_declaration_scope(trace
->declaration_scope
);
1929 int ctf_root_declaration_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_trace
*trace
)
1933 switch (node
->type
) {
1935 ret
= ctf_typedef_visit(fd
, depth
+ 1,
1936 trace
->root_declaration_scope
,
1937 node
->u
._typedef
.type_specifier_list
,
1938 &node
->u
._typedef
.type_declarators
,
1943 case NODE_TYPEALIAS
:
1944 ret
= ctf_typealias_visit(fd
, depth
+ 1,
1945 trace
->root_declaration_scope
,
1946 node
->u
.typealias
.target
, node
->u
.typealias
.alias
,
1951 case NODE_TYPE_SPECIFIER_LIST
:
1953 struct declaration
*declaration
;
1956 * Just add the type specifier to the root scope
1957 * declaration scope. Release local reference.
1959 declaration
= ctf_type_specifier_list_visit(fd
, depth
+ 1,
1960 node
, trace
->root_declaration_scope
, trace
);
1963 declaration_unref(declaration
);
1973 int ctf_visitor_construct_metadata(FILE *fd
, int depth
, struct ctf_node
*node
,
1974 struct ctf_trace
*trace
, int byte_order
)
1977 struct ctf_node
*iter
;
1979 printf_verbose("CTF visitor: metadata construction... ");
1980 trace
->root_declaration_scope
= new_declaration_scope(NULL
);
1981 trace
->byte_order
= byte_order
;
1983 switch (node
->type
) {
1985 cds_list_for_each_entry(iter
, &node
->u
.root
.declaration_list
,
1987 ret
= ctf_root_declaration_visit(fd
, depth
+ 1, iter
, trace
);
1989 fprintf(fd
, "[error] %s: root declaration error\n", __func__
);
1993 cds_list_for_each_entry(iter
, &node
->u
.root
.trace
, siblings
) {
1994 ret
= ctf_trace_visit(fd
, depth
+ 1, iter
, trace
);
1996 fprintf(fd
, "[error] %s: trace declaration error\n", __func__
);
2000 if (!trace
->streams
) {
2001 fprintf(fd
, "[error] %s: missing trace declaration\n", __func__
);
2004 cds_list_for_each_entry(iter
, &node
->u
.root
.stream
, siblings
) {
2005 ret
= ctf_stream_visit(fd
, depth
+ 1, iter
,
2006 trace
->root_declaration_scope
, trace
);
2008 fprintf(fd
, "[error] %s: stream declaration error\n", __func__
);
2012 cds_list_for_each_entry(iter
, &node
->u
.root
.event
, siblings
) {
2013 ret
= ctf_event_visit(fd
, depth
+ 1, iter
,
2014 trace
->root_declaration_scope
, trace
);
2016 fprintf(fd
, "[error] %s: event declaration error\n", __func__
);
2023 fprintf(fd
, "[error] %s: unknown node type %d\n", __func__
,
2027 printf_verbose("done.\n");