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
));
316 if (nested_declaration
->id
== CTF_TYPE_INTEGER
) {
317 struct declaration_integer
*integer_declaration
=
318 container_of(nested_declaration
, struct declaration_integer
, p
);
319 /* For base to 16 for pointers (expected pretty-print) */
320 if (!integer_declaration
->base
) {
322 * We need to do a copy of the
323 * integer declaration to modify it. There could be other references to
326 integer_declaration
= integer_declaration_new(integer_declaration
->len
,
327 integer_declaration
->byte_order
, integer_declaration
->signedness
,
328 integer_declaration
->p
.alignment
, 16);
329 nested_declaration
= &integer_declaration
->p
;
333 nested_declaration
= ctf_type_specifier_list_visit(fd
, depth
,
334 type_specifier_list
, declaration_scope
, trace
);
338 if (!node_type_declarator
)
339 return nested_declaration
;
341 if (node_type_declarator
->u
.type_declarator
.type
== TYPEDEC_ID
) {
342 if (node_type_declarator
->u
.type_declarator
.u
.id
)
343 *field_name
= g_quark_from_string(node_type_declarator
->u
.type_declarator
.u
.id
);
346 return nested_declaration
;
348 struct declaration
*declaration
;
349 struct ctf_node
*length
;
353 /* create array/sequence, pass nested_declaration as child. */
354 length
= node_type_declarator
->u
.type_declarator
.u
.nested
.length
;
356 fprintf(fd
, "[error] %s: expecting length type or value.\n", __func__
);
359 switch (length
->type
) {
360 case NODE_UNARY_EXPRESSION
:
362 struct declaration_array
*array_declaration
;
365 if (length
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
366 fprintf(fd
, "[error] %s: array: unexpected unary expression.\n", __func__
);
369 len
= length
->u
.unary_expression
.u
.unsigned_constant
;
370 array_declaration
= array_declaration_new(len
, nested_declaration
,
372 declaration
= &array_declaration
->p
;
375 case NODE_TYPE_SPECIFIER_LIST
:
377 struct declaration_sequence
*sequence_declaration
;
378 struct declaration_integer
*integer_declaration
;
380 declaration
= ctf_type_specifier_list_visit(fd
, depth
,
381 length
, declaration_scope
, trace
);
383 fprintf(fd
, "[error] %s: unable to find declaration type for sequence length\n", __func__
);
386 if (declaration
->id
!= CTF_TYPE_INTEGER
) {
387 fprintf(fd
, "[error] %s: length type for sequence is expected to be an integer (unsigned).\n", __func__
);
388 declaration_unref(declaration
);
391 integer_declaration
= container_of(declaration
, struct declaration_integer
, p
);
392 if (integer_declaration
->signedness
!= false) {
393 fprintf(fd
, "[error] %s: length type for sequence should always be an unsigned integer.\n", __func__
);
394 declaration_unref(declaration
);
398 sequence_declaration
= sequence_declaration_new(integer_declaration
,
399 nested_declaration
, declaration_scope
);
400 declaration
= &sequence_declaration
->p
;
407 /* Pass it as content of outer container */
408 declaration
= ctf_type_declarator_visit(fd
, depth
,
409 type_specifier_list
, field_name
,
410 node_type_declarator
->u
.type_declarator
.u
.nested
.type_declarator
,
411 declaration_scope
, declaration
, trace
);
417 int ctf_struct_type_declarators_visit(FILE *fd
, int depth
,
418 struct declaration_struct
*struct_declaration
,
419 struct ctf_node
*type_specifier_list
,
420 struct cds_list_head
*type_declarators
,
421 struct declaration_scope
*declaration_scope
,
422 struct ctf_trace
*trace
)
424 struct ctf_node
*iter
;
427 cds_list_for_each_entry(iter
, type_declarators
, siblings
) {
428 struct declaration
*field_declaration
;
430 field_declaration
= ctf_type_declarator_visit(fd
, depth
,
433 struct_declaration
->scope
,
435 if (!field_declaration
) {
436 fprintf(fd
, "[error] %s: unable to find struct field declaration type\n", __func__
);
439 struct_declaration_add_field(struct_declaration
,
440 g_quark_to_string(field_name
),
447 int ctf_variant_type_declarators_visit(FILE *fd
, int depth
,
448 struct declaration_untagged_variant
*untagged_variant_declaration
,
449 struct ctf_node
*type_specifier_list
,
450 struct cds_list_head
*type_declarators
,
451 struct declaration_scope
*declaration_scope
,
452 struct ctf_trace
*trace
)
454 struct ctf_node
*iter
;
457 cds_list_for_each_entry(iter
, type_declarators
, siblings
) {
458 struct declaration
*field_declaration
;
460 field_declaration
= ctf_type_declarator_visit(fd
, depth
,
463 untagged_variant_declaration
->scope
,
465 if (!field_declaration
) {
466 fprintf(fd
, "[error] %s: unable to find variant field declaration type\n", __func__
);
469 untagged_variant_declaration_add_field(untagged_variant_declaration
,
470 g_quark_to_string(field_name
),
477 int ctf_typedef_visit(FILE *fd
, int depth
, struct declaration_scope
*scope
,
478 struct ctf_node
*type_specifier_list
,
479 struct cds_list_head
*type_declarators
,
480 struct ctf_trace
*trace
)
482 struct ctf_node
*iter
;
485 cds_list_for_each_entry(iter
, type_declarators
, siblings
) {
486 struct declaration
*type_declaration
;
489 type_declaration
= ctf_type_declarator_visit(fd
, depth
,
493 if (!type_declaration
) {
494 fprintf(fd
, "[error] %s: problem creating type declaration\n", __func__
);
498 * Don't allow typedef and typealias of untagged
501 if (type_declaration
->id
== CTF_TYPE_UNTAGGED_VARIANT
) {
502 fprintf(fd
, "[error] %s: typedef of untagged variant is not permitted.\n", __func__
);
503 declaration_unref(type_declaration
);
506 ret
= register_declaration(identifier
, type_declaration
, scope
);
508 type_declaration
->declaration_free(type_declaration
);
516 int ctf_typealias_visit(FILE *fd
, int depth
, struct declaration_scope
*scope
,
517 struct ctf_node
*target
, struct ctf_node
*alias
,
518 struct ctf_trace
*trace
)
520 struct declaration
*type_declaration
;
521 struct ctf_node
*node
;
526 /* See ctf_visitor_type_declarator() in the semantic validator. */
529 * Create target type declaration.
532 if (cds_list_empty(&target
->u
.typealias_target
.type_declarators
))
535 node
= _cds_list_first_entry(&target
->u
.typealias_target
.type_declarators
,
536 struct ctf_node
, siblings
);
537 type_declaration
= ctf_type_declarator_visit(fd
, depth
,
538 target
->u
.typealias_target
.type_specifier_list
,
541 if (!type_declaration
) {
542 fprintf(fd
, "[error] %s: problem creating type declaration\n", __func__
);
547 * Don't allow typedef and typealias of untagged
550 if (type_declaration
->id
== CTF_TYPE_UNTAGGED_VARIANT
) {
551 fprintf(fd
, "[error] %s: typedef of untagged variant is not permitted.\n", __func__
);
552 declaration_unref(type_declaration
);
556 * The semantic validator does not check whether the target is
557 * abstract or not (if it has an identifier). Check it here.
560 fprintf(fd
, "[error] %s: expecting empty identifier\n", __func__
);
565 * Create alias identifier.
568 node
= _cds_list_first_entry(&alias
->u
.typealias_alias
.type_declarators
,
569 struct ctf_node
, siblings
);
570 alias_q
= create_typealias_identifier(fd
, depth
,
571 alias
->u
.typealias_alias
.type_specifier_list
, node
);
572 err
= register_declaration(alias_q
, type_declaration
, scope
);
578 type_declaration
->declaration_free(type_declaration
);
583 int ctf_struct_declaration_list_visit(FILE *fd
, int depth
,
584 struct ctf_node
*iter
, struct declaration_struct
*struct_declaration
,
585 struct ctf_trace
*trace
)
589 switch (iter
->type
) {
591 /* For each declarator, declare type and add type to struct declaration scope */
592 ret
= ctf_typedef_visit(fd
, depth
,
593 struct_declaration
->scope
,
594 iter
->u
._typedef
.type_specifier_list
,
595 &iter
->u
._typedef
.type_declarators
, trace
);
600 /* Declare type with declarator and add type to struct declaration scope */
601 ret
= ctf_typealias_visit(fd
, depth
,
602 struct_declaration
->scope
,
603 iter
->u
.typealias
.target
,
604 iter
->u
.typealias
.alias
, trace
);
608 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
609 /* Add field to structure declaration */
610 ret
= ctf_struct_type_declarators_visit(fd
, depth
,
612 iter
->u
.struct_or_variant_declaration
.type_specifier_list
,
613 &iter
->u
.struct_or_variant_declaration
.type_declarators
,
614 struct_declaration
->scope
, trace
);
619 fprintf(fd
, "[error] %s: unexpected node type %d\n", __func__
, (int) iter
->type
);
626 int ctf_variant_declaration_list_visit(FILE *fd
, int depth
,
627 struct ctf_node
*iter
,
628 struct declaration_untagged_variant
*untagged_variant_declaration
,
629 struct ctf_trace
*trace
)
633 switch (iter
->type
) {
635 /* For each declarator, declare type and add type to variant declaration scope */
636 ret
= ctf_typedef_visit(fd
, depth
,
637 untagged_variant_declaration
->scope
,
638 iter
->u
._typedef
.type_specifier_list
,
639 &iter
->u
._typedef
.type_declarators
, trace
);
644 /* Declare type with declarator and add type to variant declaration scope */
645 ret
= ctf_typealias_visit(fd
, depth
,
646 untagged_variant_declaration
->scope
,
647 iter
->u
.typealias
.target
,
648 iter
->u
.typealias
.alias
, trace
);
652 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
653 /* Add field to structure declaration */
654 ret
= ctf_variant_type_declarators_visit(fd
, depth
,
655 untagged_variant_declaration
,
656 iter
->u
.struct_or_variant_declaration
.type_specifier_list
,
657 &iter
->u
.struct_or_variant_declaration
.type_declarators
,
658 untagged_variant_declaration
->scope
, trace
);
663 fprintf(fd
, "[error] %s: unexpected node type %d\n", __func__
, (int) iter
->type
);
670 struct declaration
*ctf_declaration_struct_visit(FILE *fd
,
671 int depth
, const char *name
, struct cds_list_head
*declaration_list
,
672 int has_body
, struct cds_list_head
*min_align
,
673 struct declaration_scope
*declaration_scope
,
674 struct ctf_trace
*trace
)
676 struct declaration_struct
*struct_declaration
;
677 struct ctf_node
*iter
;
681 * For named struct (without body), lookup in
682 * declaration scope. Don't take reference on struct
683 * declaration: ref is only taken upon definition.
688 lookup_struct_declaration(g_quark_from_string(name
),
690 return &struct_declaration
->p
;
692 uint64_t min_align_value
= 0;
694 /* For unnamed struct, create type */
695 /* For named struct (with body), create type and add to declaration scope */
697 if (lookup_struct_declaration(g_quark_from_string(name
),
698 declaration_scope
)) {
700 fprintf(fd
, "[error] %s: struct %s already declared in scope\n", __func__
, name
);
704 if (!cds_list_empty(min_align
)) {
705 ret
= get_unary_unsigned(min_align
, &min_align_value
);
707 fprintf(fd
, "[error] %s: unexpected unary expression for structure \"align\" attribute\n", __func__
);
712 struct_declaration
= struct_declaration_new(declaration_scope
,
714 cds_list_for_each_entry(iter
, declaration_list
, siblings
) {
715 ret
= ctf_struct_declaration_list_visit(fd
, depth
+ 1, iter
,
716 struct_declaration
, trace
);
721 ret
= register_struct_declaration(g_quark_from_string(name
),
726 return &struct_declaration
->p
;
729 struct_declaration
->p
.declaration_free(&struct_declaration
->p
);
734 struct declaration
*ctf_declaration_variant_visit(FILE *fd
,
735 int depth
, const char *name
, const char *choice
,
736 struct cds_list_head
*declaration_list
,
737 int has_body
, struct declaration_scope
*declaration_scope
,
738 struct ctf_trace
*trace
)
740 struct declaration_untagged_variant
*untagged_variant_declaration
;
741 struct declaration_variant
*variant_declaration
;
742 struct ctf_node
*iter
;
746 * For named variant (without body), lookup in
747 * declaration scope. Don't take reference on variant
748 * declaration: ref is only taken upon definition.
752 untagged_variant_declaration
=
753 lookup_variant_declaration(g_quark_from_string(name
),
756 /* For unnamed variant, create type */
757 /* For named variant (with body), create type and add to declaration scope */
759 if (lookup_variant_declaration(g_quark_from_string(name
),
760 declaration_scope
)) {
762 fprintf(fd
, "[error] %s: variant %s already declared in scope\n", __func__
, name
);
766 untagged_variant_declaration
= untagged_variant_declaration_new(declaration_scope
);
767 cds_list_for_each_entry(iter
, declaration_list
, siblings
) {
768 ret
= ctf_variant_declaration_list_visit(fd
, depth
+ 1, iter
,
769 untagged_variant_declaration
, trace
);
774 ret
= register_variant_declaration(g_quark_from_string(name
),
775 untagged_variant_declaration
,
781 * if tagged, create tagged variant and return. else return
785 return &untagged_variant_declaration
->p
;
787 variant_declaration
= variant_declaration_new(untagged_variant_declaration
, choice
);
788 if (!variant_declaration
)
790 declaration_unref(&untagged_variant_declaration
->p
);
791 return &variant_declaration
->p
;
794 untagged_variant_declaration
->p
.declaration_free(&untagged_variant_declaration
->p
);
799 int ctf_enumerator_list_visit(FILE *fd
, int depth
,
800 struct ctf_node
*enumerator
,
801 struct declaration_enum
*enum_declaration
)
804 struct ctf_node
*iter
;
806 q
= g_quark_from_string(enumerator
->u
.enumerator
.id
);
807 if (enum_declaration
->integer_declaration
->signedness
) {
811 cds_list_for_each_entry(iter
, &enumerator
->u
.enumerator
.values
, siblings
) {
814 assert(iter
->type
== NODE_UNARY_EXPRESSION
);
820 switch (iter
->u
.unary_expression
.type
) {
821 case UNARY_SIGNED_CONSTANT
:
822 *target
= iter
->u
.unary_expression
.u
.signed_constant
;
824 case UNARY_UNSIGNED_CONSTANT
:
825 *target
= iter
->u
.unary_expression
.u
.unsigned_constant
;
828 fprintf(fd
, "[error] %s: invalid enumerator\n", __func__
);
832 fprintf(fd
, "[error] %s: invalid enumerator\n", __func__
);
839 enum_signed_insert(enum_declaration
, start
, end
, q
);
844 cds_list_for_each_entry(iter
, &enumerator
->u
.enumerator
.values
, siblings
) {
847 assert(iter
->type
== NODE_UNARY_EXPRESSION
);
853 switch (iter
->u
.unary_expression
.type
) {
854 case UNARY_UNSIGNED_CONSTANT
:
855 *target
= iter
->u
.unary_expression
.u
.unsigned_constant
;
857 case UNARY_SIGNED_CONSTANT
:
859 * We don't accept signed constants for enums with unsigned
862 fprintf(fd
, "[error] %s: invalid enumerator (signed constant encountered, but enum container type is unsigned)\n", __func__
);
865 fprintf(fd
, "[error] %s: invalid enumerator\n", __func__
);
869 fprintf(fd
, "[error] %s: invalid enumerator\n", __func__
);
876 enum_unsigned_insert(enum_declaration
, start
, end
, q
);
882 struct declaration
*ctf_declaration_enum_visit(FILE *fd
, int depth
,
884 struct ctf_node
*container_type
,
885 struct cds_list_head
*enumerator_list
,
887 struct declaration_scope
*declaration_scope
,
888 struct ctf_trace
*trace
)
890 struct declaration
*declaration
;
891 struct declaration_enum
*enum_declaration
;
892 struct declaration_integer
*integer_declaration
;
893 struct ctf_node
*iter
;
898 * For named enum (without body), lookup in
899 * declaration scope. Don't take reference on enum
900 * declaration: ref is only taken upon definition.
905 lookup_enum_declaration(g_quark_from_string(name
),
907 return &enum_declaration
->p
;
909 /* For unnamed enum, create type */
910 /* For named enum (with body), create type and add to declaration scope */
912 if (lookup_enum_declaration(g_quark_from_string(name
),
913 declaration_scope
)) {
915 fprintf(fd
, "[error] %s: enum %s already declared in scope\n", __func__
, name
);
919 if (!container_type
) {
920 declaration
= lookup_declaration(g_quark_from_static_string("int"),
923 fprintf(fd
, "[error] %s: \"int\" type declaration missing for enumeration\n", __func__
);
927 declaration
= ctf_type_declarator_visit(fd
, depth
,
934 fprintf(fd
, "[error] %s: unable to create container type for enumeration\n", __func__
);
937 if (declaration
->id
!= CTF_TYPE_INTEGER
) {
938 fprintf(fd
, "[error] %s: container type for enumeration is not integer\n", __func__
);
941 integer_declaration
= container_of(declaration
, struct declaration_integer
, p
);
942 enum_declaration
= enum_declaration_new(integer_declaration
);
943 declaration_unref(&integer_declaration
->p
); /* leave ref to enum */
944 cds_list_for_each_entry(iter
, enumerator_list
, siblings
) {
945 ret
= ctf_enumerator_list_visit(fd
, depth
+ 1, iter
, enum_declaration
);
950 ret
= register_enum_declaration(g_quark_from_string(name
),
955 return &enum_declaration
->p
;
958 enum_declaration
->p
.declaration_free(&enum_declaration
->p
);
963 struct declaration
*ctf_declaration_type_specifier_visit(FILE *fd
, int depth
,
964 struct ctf_node
*type_specifier_list
,
965 struct declaration_scope
*declaration_scope
)
968 struct declaration
*declaration
;
973 str
= g_string_new("");
974 ret
= visit_type_specifier_list(fd
, type_specifier_list
, str
);
977 str_c
= g_string_free(str
, FALSE
);
978 id_q
= g_quark_from_string(str_c
);
980 declaration
= lookup_declaration(id_q
, declaration_scope
);
985 * Returns 0/1 boolean, or < 0 on error.
988 int get_boolean(FILE *fd
, int depth
, struct ctf_node
*unary_expression
)
990 if (unary_expression
->type
!= NODE_UNARY_EXPRESSION
) {
991 fprintf(fd
, "[error] %s: expecting unary expression\n",
995 switch (unary_expression
->u
.unary_expression
.type
) {
996 case UNARY_UNSIGNED_CONSTANT
:
997 if (unary_expression
->u
.unary_expression
.u
.unsigned_constant
== 0)
1001 case UNARY_SIGNED_CONSTANT
:
1002 if (unary_expression
->u
.unary_expression
.u
.signed_constant
== 0)
1007 if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "true"))
1009 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "TRUE"))
1011 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "false"))
1013 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "FALSE"))
1016 fprintf(fd
, "[error] %s: unexpected string \"%s\"\n",
1017 __func__
, unary_expression
->u
.unary_expression
.u
.string
);
1022 fprintf(fd
, "[error] %s: unexpected unary expression type\n",
1030 int get_trace_byte_order(FILE *fd
, int depth
, struct ctf_node
*unary_expression
)
1034 if (unary_expression
->u
.unary_expression
.type
!= UNARY_STRING
) {
1035 fprintf(fd
, "[error] %s: byte_order: expecting string\n",
1039 if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "be"))
1040 byte_order
= BIG_ENDIAN
;
1041 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "le"))
1042 byte_order
= LITTLE_ENDIAN
;
1044 fprintf(fd
, "[error] %s: unexpected string \"%s\". Should be \"native\", \"network\", \"be\" or \"le\".\n",
1045 __func__
, unary_expression
->u
.unary_expression
.u
.string
);
1052 int get_byte_order(FILE *fd
, int depth
, struct ctf_node
*unary_expression
,
1053 struct ctf_trace
*trace
)
1057 if (unary_expression
->u
.unary_expression
.type
!= UNARY_STRING
) {
1058 fprintf(fd
, "[error] %s: byte_order: expecting string\n",
1062 if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "native"))
1063 byte_order
= trace
->byte_order
;
1064 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "network"))
1065 byte_order
= BIG_ENDIAN
;
1066 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "be"))
1067 byte_order
= BIG_ENDIAN
;
1068 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "le"))
1069 byte_order
= LITTLE_ENDIAN
;
1071 fprintf(fd
, "[error] %s: unexpected string \"%s\". Should be \"native\", \"network\", \"be\" or \"le\".\n",
1072 __func__
, unary_expression
->u
.unary_expression
.u
.string
);
1079 struct declaration
*ctf_declaration_integer_visit(FILE *fd
, int depth
,
1080 struct cds_list_head
*expressions
,
1081 struct ctf_trace
*trace
)
1083 struct ctf_node
*expression
;
1084 uint64_t alignment
, size
;
1085 int byte_order
= trace
->byte_order
;
1087 int has_alignment
= 0, has_size
= 0;
1089 struct declaration_integer
*integer_declaration
;
1091 cds_list_for_each_entry(expression
, expressions
, siblings
) {
1092 struct ctf_node
*left
, *right
;
1094 left
= _cds_list_first_entry(&expression
->u
.ctf_expression
.left
, struct ctf_node
, siblings
);
1095 right
= _cds_list_first_entry(&expression
->u
.ctf_expression
.right
, struct ctf_node
, siblings
);
1096 assert(left
->u
.unary_expression
.type
== UNARY_STRING
);
1097 if (!strcmp(left
->u
.unary_expression
.u
.string
, "signed")) {
1098 signedness
= get_boolean(fd
, depth
, right
);
1101 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "byte_order")) {
1102 byte_order
= get_byte_order(fd
, depth
, right
, trace
);
1105 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "size")) {
1106 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
1107 fprintf(fd
, "[error] %s: size: expecting unsigned constant\n",
1111 size
= right
->u
.unary_expression
.u
.unsigned_constant
;
1113 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "align")) {
1114 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
1115 fprintf(fd
, "[error] %s: align: expecting unsigned constant\n",
1119 alignment
= right
->u
.unary_expression
.u
.unsigned_constant
;
1120 /* Make sure alignment is a power of two */
1121 if (alignment
== 0 || (alignment
& (alignment
- 1)) != 0) {
1122 fprintf(fd
, "[error] %s: align: expecting power of two\n",
1127 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "base")) {
1128 switch (right
->u
.unary_expression
.type
) {
1129 case UNARY_UNSIGNED_CONSTANT
:
1130 switch (right
->u
.unary_expression
.u
.unsigned_constant
) {
1135 base
= right
->u
.unary_expression
.u
.unsigned_constant
;
1138 fprintf(fd
, "[error] %s: base not supported (%" PRIu64
")\n",
1139 __func__
, right
->u
.unary_expression
.u
.unsigned_constant
);
1145 char *s_right
= concatenate_unary_strings(&expression
->u
.ctf_expression
.right
);
1147 fprintf(fd
, "[error] %s: unexpected unary expression for integer base\n", __func__
);
1151 if (!strcmp(s_right
, "decimal") || !strcmp(s_right
, "dec") || !strcmp(s_right
, "d")
1152 || !strcmp(s_right
, "i") || !strcmp(s_right
, "u")) {
1154 } else if (!strcmp(s_right
, "hexadecimal") || !strcmp(s_right
, "hex")
1155 || !strcmp(s_right
, "x") || !strcmp(s_right
, "X")
1156 || !strcmp(s_right
, "p")) {
1158 } else if (!strcmp(s_right
, "octal") || !strcmp(s_right
, "oct")
1159 || !strcmp(s_right
, "o")) {
1161 } else if (!strcmp(s_right
, "binary") || !strcmp(s_right
, "b")) {
1164 fprintf(fd
, "[error] %s: unexpected expression for integer base (%s)\n", __func__
, s_right
);
1173 fprintf(fd
, "[error] %s: base: expecting unsigned constant or unary string\n",
1178 fprintf(fd
, "[error] %s: unknown attribute name %s\n",
1179 __func__
, left
->u
.unary_expression
.u
.string
);
1184 fprintf(fd
, "[error] %s: missing size attribute\n", __func__
);
1187 if (!has_alignment
) {
1188 if (size
% CHAR_BIT
) {
1189 /* bit-packed alignment */
1192 /* byte-packed alignment */
1193 alignment
= CHAR_BIT
;
1196 integer_declaration
= integer_declaration_new(size
,
1197 byte_order
, signedness
, alignment
, base
);
1198 return &integer_declaration
->p
;
1202 struct declaration
*ctf_declaration_floating_point_visit(FILE *fd
, int depth
,
1203 struct cds_list_head
*expressions
,
1204 struct ctf_trace
*trace
)
1206 struct ctf_node
*expression
;
1207 uint64_t alignment
, exp_dig
, mant_dig
, byte_order
= trace
->byte_order
;
1208 int has_alignment
= 0, has_exp_dig
= 0, has_mant_dig
= 0;
1209 struct declaration_float
*float_declaration
;
1211 cds_list_for_each_entry(expression
, expressions
, siblings
) {
1212 struct ctf_node
*left
, *right
;
1214 left
= _cds_list_first_entry(&expression
->u
.ctf_expression
.left
, struct ctf_node
, siblings
);
1215 right
= _cds_list_first_entry(&expression
->u
.ctf_expression
.right
, struct ctf_node
, siblings
);
1216 assert(left
->u
.unary_expression
.type
== UNARY_STRING
);
1217 if (!strcmp(left
->u
.unary_expression
.u
.string
, "byte_order")) {
1218 byte_order
= get_byte_order(fd
, depth
, right
, trace
);
1221 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "exp_dig")) {
1222 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
1223 fprintf(fd
, "[error] %s: exp_dig: expecting unsigned constant\n",
1227 exp_dig
= right
->u
.unary_expression
.u
.unsigned_constant
;
1229 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "mant_dig")) {
1230 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
1231 fprintf(fd
, "[error] %s: mant_dig: expecting unsigned constant\n",
1235 mant_dig
= right
->u
.unary_expression
.u
.unsigned_constant
;
1237 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "align")) {
1238 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
1239 fprintf(fd
, "[error] %s: align: expecting unsigned constant\n",
1243 alignment
= right
->u
.unary_expression
.u
.unsigned_constant
;
1244 /* Make sure alignment is a power of two */
1245 if (alignment
== 0 || (alignment
& (alignment
- 1)) != 0) {
1246 fprintf(fd
, "[error] %s: align: expecting power of two\n",
1252 fprintf(fd
, "[error] %s: unknown attribute name %s\n",
1253 __func__
, left
->u
.unary_expression
.u
.string
);
1257 if (!has_mant_dig
) {
1258 fprintf(fd
, "[error] %s: missing mant_dig attribute\n", __func__
);
1262 fprintf(fd
, "[error] %s: missing exp_dig attribute\n", __func__
);
1265 if (!has_alignment
) {
1266 if ((mant_dig
+ exp_dig
) % CHAR_BIT
) {
1267 /* bit-packed alignment */
1270 /* byte-packed alignment */
1271 alignment
= CHAR_BIT
;
1274 float_declaration
= float_declaration_new(mant_dig
, exp_dig
,
1275 byte_order
, alignment
);
1276 return &float_declaration
->p
;
1280 struct declaration
*ctf_declaration_string_visit(FILE *fd
, int depth
,
1281 struct cds_list_head
*expressions
,
1282 struct ctf_trace
*trace
)
1284 struct ctf_node
*expression
;
1285 const char *encoding_c
= NULL
;
1286 enum ctf_string_encoding encoding
= CTF_STRING_UTF8
;
1287 struct declaration_string
*string_declaration
;
1289 cds_list_for_each_entry(expression
, expressions
, siblings
) {
1290 struct ctf_node
*left
, *right
;
1292 left
= _cds_list_first_entry(&expression
->u
.ctf_expression
.left
, struct ctf_node
, siblings
);
1293 right
= _cds_list_first_entry(&expression
->u
.ctf_expression
.right
, struct ctf_node
, siblings
);
1294 assert(left
->u
.unary_expression
.type
== UNARY_STRING
);
1295 if (!strcmp(left
->u
.unary_expression
.u
.string
, "encoding")) {
1296 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
1297 fprintf(fd
, "[error] %s: encoding: expecting string\n",
1301 encoding_c
= right
->u
.unary_expression
.u
.string
;
1303 fprintf(fd
, "[error] %s: unknown attribute name %s\n",
1304 __func__
, left
->u
.unary_expression
.u
.string
);
1308 if (encoding_c
&& !strcmp(encoding_c
, "ASCII"))
1309 encoding
= CTF_STRING_ASCII
;
1310 string_declaration
= string_declaration_new(encoding
);
1311 return &string_declaration
->p
;
1316 struct declaration
*ctf_type_specifier_list_visit(FILE *fd
,
1317 int depth
, struct ctf_node
*type_specifier_list
,
1318 struct declaration_scope
*declaration_scope
,
1319 struct ctf_trace
*trace
)
1321 struct ctf_node
*first
;
1322 struct ctf_node
*node
;
1324 assert(type_specifier_list
->type
== NODE_TYPE_SPECIFIER_LIST
);
1326 first
= _cds_list_first_entry(&type_specifier_list
->u
.type_specifier_list
.head
, struct ctf_node
, siblings
);
1328 assert(first
->type
== NODE_TYPE_SPECIFIER
);
1330 node
= first
->u
.type_specifier
.node
;
1332 switch (first
->u
.type_specifier
.type
) {
1333 case TYPESPEC_FLOATING_POINT
:
1334 return ctf_declaration_floating_point_visit(fd
, depth
,
1335 &node
->u
.floating_point
.expressions
, trace
);
1336 case TYPESPEC_INTEGER
:
1337 return ctf_declaration_integer_visit(fd
, depth
,
1338 &node
->u
.integer
.expressions
, trace
);
1339 case TYPESPEC_STRING
:
1340 return ctf_declaration_string_visit(fd
, depth
,
1341 &node
->u
.string
.expressions
, trace
);
1342 case TYPESPEC_STRUCT
:
1343 return ctf_declaration_struct_visit(fd
, depth
,
1344 node
->u
._struct
.name
,
1345 &node
->u
._struct
.declaration_list
,
1346 node
->u
._struct
.has_body
,
1347 &node
->u
._struct
.min_align
,
1350 case TYPESPEC_VARIANT
:
1351 return ctf_declaration_variant_visit(fd
, depth
,
1352 node
->u
.variant
.name
,
1353 node
->u
.variant
.choice
,
1354 &node
->u
.variant
.declaration_list
,
1355 node
->u
.variant
.has_body
,
1359 return ctf_declaration_enum_visit(fd
, depth
,
1360 node
->u
._enum
.enum_id
,
1361 node
->u
._enum
.container_type
,
1362 &node
->u
._enum
.enumerator_list
,
1363 node
->u
._enum
.has_body
,
1369 case TYPESPEC_SHORT
:
1372 case TYPESPEC_FLOAT
:
1373 case TYPESPEC_DOUBLE
:
1374 case TYPESPEC_SIGNED
:
1375 case TYPESPEC_UNSIGNED
:
1377 case TYPESPEC_COMPLEX
:
1378 case TYPESPEC_IMAGINARY
:
1379 case TYPESPEC_CONST
:
1380 case TYPESPEC_ID_TYPE
:
1381 return ctf_declaration_type_specifier_visit(fd
, depth
,
1382 type_specifier_list
, declaration_scope
);
1384 fprintf(fd
, "[error] %s: unexpected node type %d\n", __func__
, (int) first
->u
.type_specifier
.type
);
1390 int ctf_event_declaration_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_event
*event
, struct ctf_trace
*trace
)
1394 switch (node
->type
) {
1396 ret
= ctf_typedef_visit(fd
, depth
+ 1,
1397 event
->declaration_scope
,
1398 node
->u
._typedef
.type_specifier_list
,
1399 &node
->u
._typedef
.type_declarators
,
1404 case NODE_TYPEALIAS
:
1405 ret
= ctf_typealias_visit(fd
, depth
+ 1,
1406 event
->declaration_scope
,
1407 node
->u
.typealias
.target
, node
->u
.typealias
.alias
,
1412 case NODE_CTF_EXPRESSION
:
1416 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
1417 if (!strcmp(left
, "name")) {
1420 if (CTF_EVENT_FIELD_IS_SET(event
, name
)) {
1421 fprintf(fd
, "[error] %s: name already declared in event declaration\n", __func__
);
1425 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
1427 fprintf(fd
, "[error] %s: unexpected unary expression for event name\n", __func__
);
1431 event
->name
= g_quark_from_string(right
);
1433 CTF_EVENT_SET_FIELD(event
, name
);
1434 } else if (!strcmp(left
, "id")) {
1435 if (CTF_EVENT_FIELD_IS_SET(event
, id
)) {
1436 fprintf(fd
, "[error] %s: id already declared in event declaration\n", __func__
);
1440 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &event
->id
);
1442 fprintf(fd
, "[error] %s: unexpected unary expression for event id\n", __func__
);
1446 CTF_EVENT_SET_FIELD(event
, id
);
1447 } else if (!strcmp(left
, "stream_id")) {
1448 if (CTF_EVENT_FIELD_IS_SET(event
, stream_id
)) {
1449 fprintf(fd
, "[error] %s: stream_id already declared in event declaration\n", __func__
);
1453 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &event
->stream_id
);
1455 fprintf(fd
, "[error] %s: unexpected unary expression for event stream_id\n", __func__
);
1459 event
->stream
= trace_stream_lookup(trace
, event
->stream_id
);
1460 if (!event
->stream
) {
1461 fprintf(fd
, "[error] %s: stream id %" PRIu64
" cannot be found\n", __func__
, event
->stream_id
);
1465 CTF_EVENT_SET_FIELD(event
, stream_id
);
1466 } else if (!strcmp(left
, "context")) {
1467 struct declaration
*declaration
;
1469 if (event
->context_decl
) {
1470 fprintf(fd
, "[error] %s: context already declared in event declaration\n", __func__
);
1474 declaration
= ctf_type_specifier_list_visit(fd
, depth
,
1475 _cds_list_first_entry(&node
->u
.ctf_expression
.right
,
1476 struct ctf_node
, siblings
),
1477 event
->declaration_scope
, trace
);
1482 if (declaration
->id
!= CTF_TYPE_STRUCT
) {
1486 event
->context_decl
= container_of(declaration
, struct declaration_struct
, p
);
1487 } else if (!strcmp(left
, "fields")) {
1488 struct declaration
*declaration
;
1490 if (event
->fields_decl
) {
1491 fprintf(fd
, "[error] %s: fields already declared in event declaration\n", __func__
);
1495 declaration
= ctf_type_specifier_list_visit(fd
, depth
,
1496 _cds_list_first_entry(&node
->u
.ctf_expression
.right
,
1497 struct ctf_node
, siblings
),
1498 event
->declaration_scope
, trace
);
1503 if (declaration
->id
!= CTF_TYPE_STRUCT
) {
1507 event
->fields_decl
= container_of(declaration
, struct declaration_struct
, p
);
1515 /* TODO: declaration specifier should be added. */
1522 int ctf_event_visit(FILE *fd
, int depth
, struct ctf_node
*node
,
1523 struct declaration_scope
*parent_declaration_scope
, struct ctf_trace
*trace
)
1526 struct ctf_node
*iter
;
1527 struct ctf_event
*event
;
1528 struct definition_scope
*parent_def_scope
;
1530 event
= g_new0(struct ctf_event
, 1);
1531 event
->declaration_scope
= new_declaration_scope(parent_declaration_scope
);
1532 cds_list_for_each_entry(iter
, &node
->u
.event
.declaration_list
, siblings
) {
1533 ret
= ctf_event_declaration_visit(fd
, depth
+ 1, iter
, event
, trace
);
1537 if (!CTF_EVENT_FIELD_IS_SET(event
, name
)) {
1539 fprintf(fd
, "[error] %s: missing name field in event declaration\n", __func__
);
1542 if (!CTF_EVENT_FIELD_IS_SET(event
, stream_id
)) {
1543 /* Allow missing stream_id if there is only a single stream */
1544 switch (trace
->streams
->len
) {
1545 case 0: /* Create stream if there was none. */
1546 ret
= ctf_stream_visit(fd
, depth
, NULL
, trace
->root_declaration_scope
, trace
);
1551 event
->stream_id
= 0;
1552 event
->stream
= trace_stream_lookup(trace
, event
->stream_id
);
1556 fprintf(fd
, "[error] %s: missing stream_id field in event declaration\n", __func__
);
1560 /* Allow only one event without id per stream */
1561 if (!CTF_EVENT_FIELD_IS_SET(event
, id
)
1562 && event
->stream
->events_by_id
->len
!= 0) {
1564 fprintf(fd
, "[error] %s: missing id field in event declaration\n", __func__
);
1567 if (event
->stream
->events_by_id
->len
<= event
->id
)
1568 g_ptr_array_set_size(event
->stream
->events_by_id
, event
->id
+ 1);
1569 g_ptr_array_index(event
->stream
->events_by_id
, event
->id
) = event
;
1570 g_hash_table_insert(event
->stream
->event_quark_to_id
,
1571 (gpointer
)(unsigned long) event
->name
,
1573 parent_def_scope
= event
->stream
->definition_scope
;
1574 if (event
->context_decl
) {
1577 event
->context_decl
->p
.definition_new(&event
->context_decl
->p
,
1578 parent_def_scope
, 0, 0),
1579 struct definition_struct
, p
);
1580 set_dynamic_definition_scope(&event
->context
->p
,
1581 event
->context
->scope
,
1583 parent_def_scope
= event
->context
->scope
;
1584 declaration_unref(&event
->context_decl
->p
);
1586 if (event
->fields_decl
) {
1589 event
->fields_decl
->p
.definition_new(&event
->fields_decl
->p
,
1590 parent_def_scope
, 0, 0),
1591 struct definition_struct
, p
);
1592 set_dynamic_definition_scope(&event
->fields
->p
,
1593 event
->fields
->scope
,
1595 parent_def_scope
= event
->fields
->scope
;
1596 declaration_unref(&event
->fields_decl
->p
);
1601 declaration_unref(&event
->fields_decl
->p
);
1602 declaration_unref(&event
->context_decl
->p
);
1603 free_declaration_scope(event
->declaration_scope
);
1610 int ctf_stream_declaration_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_stream_class
*stream
, struct ctf_trace
*trace
)
1614 switch (node
->type
) {
1616 ret
= ctf_typedef_visit(fd
, depth
+ 1,
1617 stream
->declaration_scope
,
1618 node
->u
._typedef
.type_specifier_list
,
1619 &node
->u
._typedef
.type_declarators
,
1624 case NODE_TYPEALIAS
:
1625 ret
= ctf_typealias_visit(fd
, depth
+ 1,
1626 stream
->declaration_scope
,
1627 node
->u
.typealias
.target
, node
->u
.typealias
.alias
,
1632 case NODE_CTF_EXPRESSION
:
1636 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
1637 if (!strcmp(left
, "id")) {
1638 if (CTF_STREAM_FIELD_IS_SET(stream
, stream_id
)) {
1639 fprintf(fd
, "[error] %s: id already declared in stream declaration\n", __func__
);
1643 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &stream
->stream_id
);
1645 fprintf(fd
, "[error] %s: unexpected unary expression for stream id\n", __func__
);
1649 CTF_STREAM_SET_FIELD(stream
, stream_id
);
1650 } else if (!strcmp(left
, "event.header")) {
1651 struct declaration
*declaration
;
1653 if (stream
->event_header_decl
) {
1654 fprintf(fd
, "[error] %s: event.header already declared in stream declaration\n", __func__
);
1658 declaration
= ctf_type_specifier_list_visit(fd
, depth
,
1659 _cds_list_first_entry(&node
->u
.ctf_expression
.right
,
1660 struct ctf_node
, siblings
),
1661 stream
->declaration_scope
, trace
);
1666 if (declaration
->id
!= CTF_TYPE_STRUCT
) {
1670 stream
->event_header_decl
= container_of(declaration
, struct declaration_struct
, p
);
1671 } else if (!strcmp(left
, "event.context")) {
1672 struct declaration
*declaration
;
1674 if (stream
->event_context_decl
) {
1675 fprintf(fd
, "[error] %s: event.context already declared in stream declaration\n", __func__
);
1679 declaration
= ctf_type_specifier_list_visit(fd
, depth
,
1680 _cds_list_first_entry(&node
->u
.ctf_expression
.right
,
1681 struct ctf_node
, siblings
),
1682 stream
->declaration_scope
, trace
);
1687 if (declaration
->id
!= CTF_TYPE_STRUCT
) {
1691 stream
->event_context_decl
= container_of(declaration
, struct declaration_struct
, p
);
1692 } else if (!strcmp(left
, "packet.context")) {
1693 struct declaration
*declaration
;
1695 if (stream
->packet_context_decl
) {
1696 fprintf(fd
, "[error] %s: packet.context already declared in stream declaration\n", __func__
);
1700 declaration
= ctf_type_specifier_list_visit(fd
, depth
,
1701 _cds_list_first_entry(&node
->u
.ctf_expression
.right
,
1702 struct ctf_node
, siblings
),
1703 stream
->declaration_scope
, trace
);
1708 if (declaration
->id
!= CTF_TYPE_STRUCT
) {
1712 stream
->packet_context_decl
= container_of(declaration
, struct declaration_struct
, p
);
1720 /* TODO: declaration specifier should be added. */
1727 int ctf_stream_visit(FILE *fd
, int depth
, struct ctf_node
*node
,
1728 struct declaration_scope
*parent_declaration_scope
, struct ctf_trace
*trace
)
1731 struct ctf_node
*iter
;
1732 struct ctf_stream_class
*stream
;
1733 struct definition_scope
*parent_def_scope
;
1735 stream
= g_new0(struct ctf_stream_class
, 1);
1736 stream
->declaration_scope
= new_declaration_scope(parent_declaration_scope
);
1737 stream
->events_by_id
= g_ptr_array_new();
1738 stream
->event_quark_to_id
= g_hash_table_new(g_direct_hash
, g_direct_equal
);
1739 stream
->files
= g_ptr_array_new();
1741 cds_list_for_each_entry(iter
, &node
->u
.stream
.declaration_list
, siblings
) {
1742 ret
= ctf_stream_declaration_visit(fd
, depth
+ 1, iter
, stream
, trace
);
1747 if (CTF_STREAM_FIELD_IS_SET(stream
, stream_id
)) {
1748 /* check that packet header has stream_id field. */
1749 if (!trace
->packet_header_decl
1750 || struct_declaration_lookup_field_index(trace
->packet_header_decl
, g_quark_from_static_string("stream_id")) < 0) {
1752 fprintf(fd
, "[error] %s: missing stream_id field in packet header declaration, but stream_id attribute is declared for stream.\n", __func__
);
1756 /* Allow only one id-less stream */
1757 if (trace
->streams
->len
!= 0) {
1759 fprintf(fd
, "[error] %s: missing id field in stream declaration\n", __func__
);
1762 stream
->stream_id
= 0;
1764 if (trace
->streams
->len
<= stream
->stream_id
)
1765 g_ptr_array_set_size(trace
->streams
, stream
->stream_id
+ 1);
1766 g_ptr_array_index(trace
->streams
, stream
->stream_id
) = stream
;
1768 parent_def_scope
= trace
->definition_scope
;
1769 if (stream
->packet_context_decl
) {
1770 stream
->packet_context
=
1772 stream
->packet_context_decl
->p
.definition_new(&stream
->packet_context_decl
->p
,
1773 parent_def_scope
, 0, 0),
1774 struct definition_struct
, p
);
1775 set_dynamic_definition_scope(&stream
->packet_context
->p
,
1776 stream
->packet_context
->scope
,
1777 "stream.packet.context");
1778 parent_def_scope
= stream
->packet_context
->scope
;
1779 declaration_unref(&stream
->packet_context_decl
->p
);
1781 if (stream
->event_header_decl
) {
1782 stream
->event_header
=
1784 stream
->event_header_decl
->p
.definition_new(&stream
->event_header_decl
->p
,
1785 parent_def_scope
, 0, 0),
1786 struct definition_struct
, p
);
1787 set_dynamic_definition_scope(&stream
->event_header
->p
,
1788 stream
->event_header
->scope
,
1789 "stream.event.header");
1790 parent_def_scope
= stream
->event_header
->scope
;
1791 declaration_unref(&stream
->event_header_decl
->p
);
1793 if (stream
->event_context_decl
) {
1794 stream
->event_context
=
1796 stream
->event_context_decl
->p
.definition_new(&stream
->event_context_decl
->p
,
1797 parent_def_scope
, 0, 0),
1798 struct definition_struct
, p
);
1799 set_dynamic_definition_scope(&stream
->event_context
->p
,
1800 stream
->event_context
->scope
,
1801 "stream.event.context");
1802 parent_def_scope
= stream
->event_context
->scope
;
1803 declaration_unref(&stream
->event_context_decl
->p
);
1805 stream
->definition_scope
= parent_def_scope
;
1810 declaration_unref(&stream
->event_header_decl
->p
);
1811 declaration_unref(&stream
->event_context_decl
->p
);
1812 declaration_unref(&stream
->packet_context_decl
->p
);
1813 g_ptr_array_free(stream
->files
, TRUE
);
1814 g_ptr_array_free(stream
->events_by_id
, TRUE
);
1815 g_hash_table_destroy(stream
->event_quark_to_id
);
1816 free_declaration_scope(stream
->declaration_scope
);
1822 int ctf_trace_declaration_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_trace
*trace
)
1826 switch (node
->type
) {
1828 ret
= ctf_typedef_visit(fd
, depth
+ 1,
1829 trace
->declaration_scope
,
1830 node
->u
._typedef
.type_specifier_list
,
1831 &node
->u
._typedef
.type_declarators
,
1836 case NODE_TYPEALIAS
:
1837 ret
= ctf_typealias_visit(fd
, depth
+ 1,
1838 trace
->declaration_scope
,
1839 node
->u
.typealias
.target
, node
->u
.typealias
.alias
,
1844 case NODE_CTF_EXPRESSION
:
1848 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
1849 if (!strcmp(left
, "major")) {
1850 if (CTF_TRACE_FIELD_IS_SET(trace
, major
)) {
1851 fprintf(fd
, "[error] %s: major already declared in trace declaration\n", __func__
);
1855 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &trace
->major
);
1857 fprintf(fd
, "[error] %s: unexpected unary expression for trace major number\n", __func__
);
1861 CTF_TRACE_SET_FIELD(trace
, major
);
1862 } else if (!strcmp(left
, "minor")) {
1863 if (CTF_TRACE_FIELD_IS_SET(trace
, minor
)) {
1864 fprintf(fd
, "[error] %s: minor already declared in trace declaration\n", __func__
);
1868 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &trace
->minor
);
1870 fprintf(fd
, "[error] %s: unexpected unary expression for trace minor number\n", __func__
);
1874 CTF_TRACE_SET_FIELD(trace
, minor
);
1875 } else if (!strcmp(left
, "uuid")) {
1876 if (CTF_TRACE_FIELD_IS_SET(trace
, uuid
)) {
1877 fprintf(fd
, "[error] %s: uuid already declared in trace declaration\n", __func__
);
1881 ret
= get_unary_uuid(&node
->u
.ctf_expression
.right
, &trace
->uuid
);
1883 fprintf(fd
, "[error] %s: unexpected unary expression for trace uuid\n", __func__
);
1887 CTF_TRACE_SET_FIELD(trace
, uuid
);
1888 } else if (!strcmp(left
, "byte_order")) {
1889 struct ctf_node
*right
;
1892 if (CTF_TRACE_FIELD_IS_SET(trace
, byte_order
)) {
1893 fprintf(fd
, "[error] %s: endianness already declared in trace declaration\n", __func__
);
1897 right
= _cds_list_first_entry(&node
->u
.ctf_expression
.right
, struct ctf_node
, siblings
);
1898 byte_order
= get_trace_byte_order(fd
, depth
, right
);
1901 trace
->byte_order
= byte_order
;
1902 CTF_TRACE_SET_FIELD(trace
, byte_order
);
1903 } else if (!strcmp(left
, "packet.header")) {
1904 struct declaration
*declaration
;
1906 if (trace
->packet_header_decl
) {
1907 fprintf(fd
, "[error] %s: packet.header already declared in trace declaration\n", __func__
);
1911 declaration
= ctf_type_specifier_list_visit(fd
, depth
,
1912 _cds_list_first_entry(&node
->u
.ctf_expression
.right
,
1913 struct ctf_node
, siblings
),
1914 trace
->declaration_scope
, trace
);
1919 if (declaration
->id
!= CTF_TYPE_STRUCT
) {
1923 trace
->packet_header_decl
= container_of(declaration
, struct declaration_struct
, p
);
1931 /* TODO: declaration specifier should be added. */
1938 int ctf_trace_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_trace
*trace
)
1940 struct definition_scope
*parent_def_scope
;
1942 struct ctf_node
*iter
;
1944 if (trace
->declaration_scope
)
1946 trace
->declaration_scope
= new_declaration_scope(trace
->root_declaration_scope
);
1947 trace
->streams
= g_ptr_array_new();
1948 cds_list_for_each_entry(iter
, &node
->u
.trace
.declaration_list
, siblings
) {
1949 ret
= ctf_trace_declaration_visit(fd
, depth
+ 1, iter
, trace
);
1953 if (!CTF_TRACE_FIELD_IS_SET(trace
, major
)) {
1955 fprintf(fd
, "[error] %s: missing major field in trace declaration\n", __func__
);
1958 if (!CTF_TRACE_FIELD_IS_SET(trace
, minor
)) {
1960 fprintf(fd
, "[error] %s: missing minor field in trace declaration\n", __func__
);
1963 if (!CTF_TRACE_FIELD_IS_SET(trace
, uuid
)) {
1965 fprintf(fd
, "[error] %s: missing uuid field in trace declaration\n", __func__
);
1968 if (!CTF_TRACE_FIELD_IS_SET(trace
, byte_order
)) {
1970 fprintf(fd
, "[error] %s: missing byte_order field in trace declaration\n", __func__
);
1974 parent_def_scope
= NULL
;
1975 if (trace
->packet_header_decl
) {
1976 trace
->packet_header
=
1978 trace
->packet_header_decl
->p
.definition_new(&trace
->packet_header_decl
->p
,
1979 parent_def_scope
, 0, 0),
1980 struct definition_struct
, p
);
1981 set_dynamic_definition_scope(&trace
->packet_header
->p
,
1982 trace
->packet_header
->scope
,
1983 "trace.packet.header");
1984 parent_def_scope
= trace
->packet_header
->scope
;
1985 declaration_unref(&trace
->packet_header_decl
->p
);
1987 trace
->definition_scope
= parent_def_scope
;
1989 if (!CTF_TRACE_FIELD_IS_SET(trace
, byte_order
)) {
1990 /* check that the packet header contains a "magic" field */
1991 if (!trace
->packet_header
1992 || struct_declaration_lookup_field_index(trace
->packet_header_decl
, g_quark_from_static_string("magic")) < 0) {
1994 fprintf(fd
, "[error] %s: missing both byte_order and packet header magic number in trace declaration\n", __func__
);
1995 goto error_free_def
;
2001 definition_unref(&trace
->packet_header
->p
);
2003 g_ptr_array_free(trace
->streams
, TRUE
);
2004 free_declaration_scope(trace
->declaration_scope
);
2009 int ctf_root_declaration_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_trace
*trace
)
2013 switch (node
->type
) {
2015 ret
= ctf_typedef_visit(fd
, depth
+ 1,
2016 trace
->root_declaration_scope
,
2017 node
->u
._typedef
.type_specifier_list
,
2018 &node
->u
._typedef
.type_declarators
,
2023 case NODE_TYPEALIAS
:
2024 ret
= ctf_typealias_visit(fd
, depth
+ 1,
2025 trace
->root_declaration_scope
,
2026 node
->u
.typealias
.target
, node
->u
.typealias
.alias
,
2031 case NODE_TYPE_SPECIFIER_LIST
:
2033 struct declaration
*declaration
;
2036 * Just add the type specifier to the root scope
2037 * declaration scope. Release local reference.
2039 declaration
= ctf_type_specifier_list_visit(fd
, depth
+ 1,
2040 node
, trace
->root_declaration_scope
, trace
);
2043 declaration_unref(declaration
);
2053 int ctf_visitor_construct_metadata(FILE *fd
, int depth
, struct ctf_node
*node
,
2054 struct ctf_trace
*trace
, int byte_order
)
2057 struct ctf_node
*iter
;
2059 printf_verbose("CTF visitor: metadata construction... ");
2060 trace
->root_declaration_scope
= new_declaration_scope(NULL
);
2061 trace
->streams
= g_ptr_array_new();
2062 trace
->byte_order
= byte_order
;
2064 switch (node
->type
) {
2066 cds_list_for_each_entry(iter
, &node
->u
.root
.declaration_list
,
2068 ret
= ctf_root_declaration_visit(fd
, depth
+ 1, iter
, trace
);
2070 fprintf(fd
, "[error] %s: root declaration error\n", __func__
);
2074 cds_list_for_each_entry(iter
, &node
->u
.root
.trace
, siblings
) {
2075 ret
= ctf_trace_visit(fd
, depth
+ 1, iter
, trace
);
2077 fprintf(fd
, "[error] %s: trace declaration error\n", __func__
);
2081 if (!trace
->streams
) {
2082 fprintf(fd
, "[error] %s: missing trace declaration\n", __func__
);
2085 cds_list_for_each_entry(iter
, &node
->u
.root
.stream
, siblings
) {
2086 ret
= ctf_stream_visit(fd
, depth
+ 1, iter
,
2087 trace
->root_declaration_scope
, trace
);
2089 fprintf(fd
, "[error] %s: stream declaration error\n", __func__
);
2093 cds_list_for_each_entry(iter
, &node
->u
.root
.event
, siblings
) {
2094 ret
= ctf_event_visit(fd
, depth
+ 1, iter
,
2095 trace
->root_declaration_scope
, trace
);
2097 fprintf(fd
, "[error] %s: event declaration error\n", __func__
);
2104 fprintf(fd
, "[error] %s: unknown node type %d\n", __func__
,
2108 printf_verbose("done.\n");