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
*first
;
353 /* create array/sequence, pass nested_declaration as child. */
354 if (cds_list_empty(&node_type_declarator
->u
.type_declarator
.u
.nested
.length
)) {
355 fprintf(fd
, "[error] %s: expecting length field reference or value.\n", __func__
);
358 first
= _cds_list_first_entry(&node_type_declarator
->u
.type_declarator
.u
.nested
.length
,
359 struct ctf_node
, siblings
);
360 assert(first
->type
== NODE_UNARY_EXPRESSION
);
362 switch (first
->u
.unary_expression
.type
) {
363 case UNARY_UNSIGNED_CONSTANT
:
365 struct declaration_array
*array_declaration
;
368 len
= first
->u
.unary_expression
.u
.unsigned_constant
;
369 array_declaration
= array_declaration_new(len
, nested_declaration
,
372 if (!array_declaration
) {
373 fprintf(fd
, "[error] %s: cannot create array declaration.\n", __func__
);
376 declaration
= &array_declaration
->p
;
381 /* Lookup unsigned integer definition, create sequence */
382 char *length_name
= concatenate_unary_strings(&node_type_declarator
->u
.type_declarator
.u
.nested
.length
);
383 struct declaration_sequence
*sequence_declaration
;
385 sequence_declaration
= sequence_declaration_new(length_name
, nested_declaration
, declaration_scope
);
386 if (!sequence_declaration
) {
387 fprintf(fd
, "[error] %s: cannot create sequence declaration.\n", __func__
);
390 declaration
= &sequence_declaration
->p
;
397 /* Pass it as content of outer container */
398 declaration
= ctf_type_declarator_visit(fd
, depth
,
399 type_specifier_list
, field_name
,
400 node_type_declarator
->u
.type_declarator
.u
.nested
.type_declarator
,
401 declaration_scope
, declaration
, trace
);
407 int ctf_struct_type_declarators_visit(FILE *fd
, int depth
,
408 struct declaration_struct
*struct_declaration
,
409 struct ctf_node
*type_specifier_list
,
410 struct cds_list_head
*type_declarators
,
411 struct declaration_scope
*declaration_scope
,
412 struct ctf_trace
*trace
)
414 struct ctf_node
*iter
;
417 cds_list_for_each_entry(iter
, type_declarators
, siblings
) {
418 struct declaration
*field_declaration
;
420 field_declaration
= ctf_type_declarator_visit(fd
, depth
,
423 struct_declaration
->scope
,
425 if (!field_declaration
) {
426 fprintf(fd
, "[error] %s: unable to find struct field declaration type\n", __func__
);
429 struct_declaration_add_field(struct_declaration
,
430 g_quark_to_string(field_name
),
437 int ctf_variant_type_declarators_visit(FILE *fd
, int depth
,
438 struct declaration_untagged_variant
*untagged_variant_declaration
,
439 struct ctf_node
*type_specifier_list
,
440 struct cds_list_head
*type_declarators
,
441 struct declaration_scope
*declaration_scope
,
442 struct ctf_trace
*trace
)
444 struct ctf_node
*iter
;
447 cds_list_for_each_entry(iter
, type_declarators
, siblings
) {
448 struct declaration
*field_declaration
;
450 field_declaration
= ctf_type_declarator_visit(fd
, depth
,
453 untagged_variant_declaration
->scope
,
455 if (!field_declaration
) {
456 fprintf(fd
, "[error] %s: unable to find variant field declaration type\n", __func__
);
459 untagged_variant_declaration_add_field(untagged_variant_declaration
,
460 g_quark_to_string(field_name
),
467 int ctf_typedef_visit(FILE *fd
, int depth
, struct declaration_scope
*scope
,
468 struct ctf_node
*type_specifier_list
,
469 struct cds_list_head
*type_declarators
,
470 struct ctf_trace
*trace
)
472 struct ctf_node
*iter
;
475 cds_list_for_each_entry(iter
, type_declarators
, siblings
) {
476 struct declaration
*type_declaration
;
479 type_declaration
= ctf_type_declarator_visit(fd
, depth
,
483 if (!type_declaration
) {
484 fprintf(fd
, "[error] %s: problem creating type declaration\n", __func__
);
488 * Don't allow typedef and typealias of untagged
491 if (type_declaration
->id
== CTF_TYPE_UNTAGGED_VARIANT
) {
492 fprintf(fd
, "[error] %s: typedef of untagged variant is not permitted.\n", __func__
);
493 declaration_unref(type_declaration
);
496 ret
= register_declaration(identifier
, type_declaration
, scope
);
498 type_declaration
->declaration_free(type_declaration
);
506 int ctf_typealias_visit(FILE *fd
, int depth
, struct declaration_scope
*scope
,
507 struct ctf_node
*target
, struct ctf_node
*alias
,
508 struct ctf_trace
*trace
)
510 struct declaration
*type_declaration
;
511 struct ctf_node
*node
;
516 /* See ctf_visitor_type_declarator() in the semantic validator. */
519 * Create target type declaration.
522 if (cds_list_empty(&target
->u
.typealias_target
.type_declarators
))
525 node
= _cds_list_first_entry(&target
->u
.typealias_target
.type_declarators
,
526 struct ctf_node
, siblings
);
527 type_declaration
= ctf_type_declarator_visit(fd
, depth
,
528 target
->u
.typealias_target
.type_specifier_list
,
531 if (!type_declaration
) {
532 fprintf(fd
, "[error] %s: problem creating type declaration\n", __func__
);
537 * Don't allow typedef and typealias of untagged
540 if (type_declaration
->id
== CTF_TYPE_UNTAGGED_VARIANT
) {
541 fprintf(fd
, "[error] %s: typedef of untagged variant is not permitted.\n", __func__
);
542 declaration_unref(type_declaration
);
546 * The semantic validator does not check whether the target is
547 * abstract or not (if it has an identifier). Check it here.
550 fprintf(fd
, "[error] %s: expecting empty identifier\n", __func__
);
555 * Create alias identifier.
558 node
= _cds_list_first_entry(&alias
->u
.typealias_alias
.type_declarators
,
559 struct ctf_node
, siblings
);
560 alias_q
= create_typealias_identifier(fd
, depth
,
561 alias
->u
.typealias_alias
.type_specifier_list
, node
);
562 err
= register_declaration(alias_q
, type_declaration
, scope
);
568 type_declaration
->declaration_free(type_declaration
);
573 int ctf_struct_declaration_list_visit(FILE *fd
, int depth
,
574 struct ctf_node
*iter
, struct declaration_struct
*struct_declaration
,
575 struct ctf_trace
*trace
)
579 switch (iter
->type
) {
581 /* For each declarator, declare type and add type to struct declaration scope */
582 ret
= ctf_typedef_visit(fd
, depth
,
583 struct_declaration
->scope
,
584 iter
->u
._typedef
.type_specifier_list
,
585 &iter
->u
._typedef
.type_declarators
, trace
);
590 /* Declare type with declarator and add type to struct declaration scope */
591 ret
= ctf_typealias_visit(fd
, depth
,
592 struct_declaration
->scope
,
593 iter
->u
.typealias
.target
,
594 iter
->u
.typealias
.alias
, trace
);
598 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
599 /* Add field to structure declaration */
600 ret
= ctf_struct_type_declarators_visit(fd
, depth
,
602 iter
->u
.struct_or_variant_declaration
.type_specifier_list
,
603 &iter
->u
.struct_or_variant_declaration
.type_declarators
,
604 struct_declaration
->scope
, trace
);
609 fprintf(fd
, "[error] %s: unexpected node type %d\n", __func__
, (int) iter
->type
);
616 int ctf_variant_declaration_list_visit(FILE *fd
, int depth
,
617 struct ctf_node
*iter
,
618 struct declaration_untagged_variant
*untagged_variant_declaration
,
619 struct ctf_trace
*trace
)
623 switch (iter
->type
) {
625 /* For each declarator, declare type and add type to variant declaration scope */
626 ret
= ctf_typedef_visit(fd
, depth
,
627 untagged_variant_declaration
->scope
,
628 iter
->u
._typedef
.type_specifier_list
,
629 &iter
->u
._typedef
.type_declarators
, trace
);
634 /* Declare type with declarator and add type to variant declaration scope */
635 ret
= ctf_typealias_visit(fd
, depth
,
636 untagged_variant_declaration
->scope
,
637 iter
->u
.typealias
.target
,
638 iter
->u
.typealias
.alias
, trace
);
642 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
643 /* Add field to structure declaration */
644 ret
= ctf_variant_type_declarators_visit(fd
, depth
,
645 untagged_variant_declaration
,
646 iter
->u
.struct_or_variant_declaration
.type_specifier_list
,
647 &iter
->u
.struct_or_variant_declaration
.type_declarators
,
648 untagged_variant_declaration
->scope
, trace
);
653 fprintf(fd
, "[error] %s: unexpected node type %d\n", __func__
, (int) iter
->type
);
660 struct declaration
*ctf_declaration_struct_visit(FILE *fd
,
661 int depth
, const char *name
, struct cds_list_head
*declaration_list
,
662 int has_body
, struct cds_list_head
*min_align
,
663 struct declaration_scope
*declaration_scope
,
664 struct ctf_trace
*trace
)
666 struct declaration_struct
*struct_declaration
;
667 struct ctf_node
*iter
;
671 * For named struct (without body), lookup in
672 * declaration scope. Don't take reference on struct
673 * declaration: ref is only taken upon definition.
678 lookup_struct_declaration(g_quark_from_string(name
),
680 return &struct_declaration
->p
;
682 uint64_t min_align_value
= 0;
684 /* For unnamed struct, create type */
685 /* For named struct (with body), create type and add to declaration scope */
687 if (lookup_struct_declaration(g_quark_from_string(name
),
688 declaration_scope
)) {
690 fprintf(fd
, "[error] %s: struct %s already declared in scope\n", __func__
, name
);
694 if (!cds_list_empty(min_align
)) {
695 ret
= get_unary_unsigned(min_align
, &min_align_value
);
697 fprintf(fd
, "[error] %s: unexpected unary expression for structure \"align\" attribute\n", __func__
);
702 struct_declaration
= struct_declaration_new(declaration_scope
,
704 cds_list_for_each_entry(iter
, declaration_list
, siblings
) {
705 ret
= ctf_struct_declaration_list_visit(fd
, depth
+ 1, iter
,
706 struct_declaration
, trace
);
711 ret
= register_struct_declaration(g_quark_from_string(name
),
716 return &struct_declaration
->p
;
719 struct_declaration
->p
.declaration_free(&struct_declaration
->p
);
724 struct declaration
*ctf_declaration_variant_visit(FILE *fd
,
725 int depth
, const char *name
, const char *choice
,
726 struct cds_list_head
*declaration_list
,
727 int has_body
, struct declaration_scope
*declaration_scope
,
728 struct ctf_trace
*trace
)
730 struct declaration_untagged_variant
*untagged_variant_declaration
;
731 struct declaration_variant
*variant_declaration
;
732 struct ctf_node
*iter
;
736 * For named variant (without body), lookup in
737 * declaration scope. Don't take reference on variant
738 * declaration: ref is only taken upon definition.
742 untagged_variant_declaration
=
743 lookup_variant_declaration(g_quark_from_string(name
),
746 /* For unnamed variant, create type */
747 /* For named variant (with body), create type and add to declaration scope */
749 if (lookup_variant_declaration(g_quark_from_string(name
),
750 declaration_scope
)) {
752 fprintf(fd
, "[error] %s: variant %s already declared in scope\n", __func__
, name
);
756 untagged_variant_declaration
= untagged_variant_declaration_new(declaration_scope
);
757 cds_list_for_each_entry(iter
, declaration_list
, siblings
) {
758 ret
= ctf_variant_declaration_list_visit(fd
, depth
+ 1, iter
,
759 untagged_variant_declaration
, trace
);
764 ret
= register_variant_declaration(g_quark_from_string(name
),
765 untagged_variant_declaration
,
771 * if tagged, create tagged variant and return. else return
775 return &untagged_variant_declaration
->p
;
777 variant_declaration
= variant_declaration_new(untagged_variant_declaration
, choice
);
778 if (!variant_declaration
)
780 declaration_unref(&untagged_variant_declaration
->p
);
781 return &variant_declaration
->p
;
784 untagged_variant_declaration
->p
.declaration_free(&untagged_variant_declaration
->p
);
789 int ctf_enumerator_list_visit(FILE *fd
, int depth
,
790 struct ctf_node
*enumerator
,
791 struct declaration_enum
*enum_declaration
)
794 struct ctf_node
*iter
;
796 q
= g_quark_from_string(enumerator
->u
.enumerator
.id
);
797 if (enum_declaration
->integer_declaration
->signedness
) {
801 cds_list_for_each_entry(iter
, &enumerator
->u
.enumerator
.values
, siblings
) {
804 assert(iter
->type
== NODE_UNARY_EXPRESSION
);
810 switch (iter
->u
.unary_expression
.type
) {
811 case UNARY_SIGNED_CONSTANT
:
812 *target
= iter
->u
.unary_expression
.u
.signed_constant
;
814 case UNARY_UNSIGNED_CONSTANT
:
815 *target
= iter
->u
.unary_expression
.u
.unsigned_constant
;
818 fprintf(fd
, "[error] %s: invalid enumerator\n", __func__
);
822 fprintf(fd
, "[error] %s: invalid enumerator\n", __func__
);
829 enum_signed_insert(enum_declaration
, start
, end
, q
);
834 cds_list_for_each_entry(iter
, &enumerator
->u
.enumerator
.values
, siblings
) {
837 assert(iter
->type
== NODE_UNARY_EXPRESSION
);
843 switch (iter
->u
.unary_expression
.type
) {
844 case UNARY_UNSIGNED_CONSTANT
:
845 *target
= iter
->u
.unary_expression
.u
.unsigned_constant
;
847 case UNARY_SIGNED_CONSTANT
:
849 * We don't accept signed constants for enums with unsigned
852 fprintf(fd
, "[error] %s: invalid enumerator (signed constant encountered, but enum container type is unsigned)\n", __func__
);
855 fprintf(fd
, "[error] %s: invalid enumerator\n", __func__
);
859 fprintf(fd
, "[error] %s: invalid enumerator\n", __func__
);
866 enum_unsigned_insert(enum_declaration
, start
, end
, q
);
872 struct declaration
*ctf_declaration_enum_visit(FILE *fd
, int depth
,
874 struct ctf_node
*container_type
,
875 struct cds_list_head
*enumerator_list
,
877 struct declaration_scope
*declaration_scope
,
878 struct ctf_trace
*trace
)
880 struct declaration
*declaration
;
881 struct declaration_enum
*enum_declaration
;
882 struct declaration_integer
*integer_declaration
;
883 struct ctf_node
*iter
;
888 * For named enum (without body), lookup in
889 * declaration scope. Don't take reference on enum
890 * declaration: ref is only taken upon definition.
895 lookup_enum_declaration(g_quark_from_string(name
),
897 return &enum_declaration
->p
;
899 /* For unnamed enum, create type */
900 /* For named enum (with body), create type and add to declaration scope */
902 if (lookup_enum_declaration(g_quark_from_string(name
),
903 declaration_scope
)) {
905 fprintf(fd
, "[error] %s: enum %s already declared in scope\n", __func__
, name
);
909 if (!container_type
) {
910 declaration
= lookup_declaration(g_quark_from_static_string("int"),
913 fprintf(fd
, "[error] %s: \"int\" type declaration missing for enumeration\n", __func__
);
917 declaration
= ctf_type_declarator_visit(fd
, depth
,
924 fprintf(fd
, "[error] %s: unable to create container type for enumeration\n", __func__
);
927 if (declaration
->id
!= CTF_TYPE_INTEGER
) {
928 fprintf(fd
, "[error] %s: container type for enumeration is not integer\n", __func__
);
931 integer_declaration
= container_of(declaration
, struct declaration_integer
, p
);
932 enum_declaration
= enum_declaration_new(integer_declaration
);
933 declaration_unref(&integer_declaration
->p
); /* leave ref to enum */
934 cds_list_for_each_entry(iter
, enumerator_list
, siblings
) {
935 ret
= ctf_enumerator_list_visit(fd
, depth
+ 1, iter
, enum_declaration
);
940 ret
= register_enum_declaration(g_quark_from_string(name
),
945 return &enum_declaration
->p
;
948 enum_declaration
->p
.declaration_free(&enum_declaration
->p
);
953 struct declaration
*ctf_declaration_type_specifier_visit(FILE *fd
, int depth
,
954 struct ctf_node
*type_specifier_list
,
955 struct declaration_scope
*declaration_scope
)
958 struct declaration
*declaration
;
963 str
= g_string_new("");
964 ret
= visit_type_specifier_list(fd
, type_specifier_list
, str
);
967 str_c
= g_string_free(str
, FALSE
);
968 id_q
= g_quark_from_string(str_c
);
970 declaration
= lookup_declaration(id_q
, declaration_scope
);
975 * Returns 0/1 boolean, or < 0 on error.
978 int get_boolean(FILE *fd
, int depth
, struct ctf_node
*unary_expression
)
980 if (unary_expression
->type
!= NODE_UNARY_EXPRESSION
) {
981 fprintf(fd
, "[error] %s: expecting unary expression\n",
985 switch (unary_expression
->u
.unary_expression
.type
) {
986 case UNARY_UNSIGNED_CONSTANT
:
987 if (unary_expression
->u
.unary_expression
.u
.unsigned_constant
== 0)
991 case UNARY_SIGNED_CONSTANT
:
992 if (unary_expression
->u
.unary_expression
.u
.signed_constant
== 0)
997 if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "true"))
999 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "TRUE"))
1001 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "false"))
1003 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "FALSE"))
1006 fprintf(fd
, "[error] %s: unexpected string \"%s\"\n",
1007 __func__
, unary_expression
->u
.unary_expression
.u
.string
);
1012 fprintf(fd
, "[error] %s: unexpected unary expression type\n",
1020 int get_trace_byte_order(FILE *fd
, int depth
, struct ctf_node
*unary_expression
)
1024 if (unary_expression
->u
.unary_expression
.type
!= UNARY_STRING
) {
1025 fprintf(fd
, "[error] %s: byte_order: expecting string\n",
1029 if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "be"))
1030 byte_order
= BIG_ENDIAN
;
1031 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "le"))
1032 byte_order
= LITTLE_ENDIAN
;
1034 fprintf(fd
, "[error] %s: unexpected string \"%s\". Should be \"native\", \"network\", \"be\" or \"le\".\n",
1035 __func__
, unary_expression
->u
.unary_expression
.u
.string
);
1042 int get_byte_order(FILE *fd
, int depth
, struct ctf_node
*unary_expression
,
1043 struct ctf_trace
*trace
)
1047 if (unary_expression
->u
.unary_expression
.type
!= UNARY_STRING
) {
1048 fprintf(fd
, "[error] %s: byte_order: expecting string\n",
1052 if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "native"))
1053 byte_order
= trace
->byte_order
;
1054 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "network"))
1055 byte_order
= BIG_ENDIAN
;
1056 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "be"))
1057 byte_order
= BIG_ENDIAN
;
1058 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "le"))
1059 byte_order
= LITTLE_ENDIAN
;
1061 fprintf(fd
, "[error] %s: unexpected string \"%s\". Should be \"native\", \"network\", \"be\" or \"le\".\n",
1062 __func__
, unary_expression
->u
.unary_expression
.u
.string
);
1069 struct declaration
*ctf_declaration_integer_visit(FILE *fd
, int depth
,
1070 struct cds_list_head
*expressions
,
1071 struct ctf_trace
*trace
)
1073 struct ctf_node
*expression
;
1074 uint64_t alignment
, size
;
1075 int byte_order
= trace
->byte_order
;
1077 int has_alignment
= 0, has_size
= 0;
1079 struct declaration_integer
*integer_declaration
;
1081 cds_list_for_each_entry(expression
, expressions
, siblings
) {
1082 struct ctf_node
*left
, *right
;
1084 left
= _cds_list_first_entry(&expression
->u
.ctf_expression
.left
, struct ctf_node
, siblings
);
1085 right
= _cds_list_first_entry(&expression
->u
.ctf_expression
.right
, struct ctf_node
, siblings
);
1086 assert(left
->u
.unary_expression
.type
== UNARY_STRING
);
1087 if (!strcmp(left
->u
.unary_expression
.u
.string
, "signed")) {
1088 signedness
= get_boolean(fd
, depth
, right
);
1091 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "byte_order")) {
1092 byte_order
= get_byte_order(fd
, depth
, right
, trace
);
1095 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "size")) {
1096 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
1097 fprintf(fd
, "[error] %s: size: expecting unsigned constant\n",
1101 size
= right
->u
.unary_expression
.u
.unsigned_constant
;
1103 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "align")) {
1104 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
1105 fprintf(fd
, "[error] %s: align: expecting unsigned constant\n",
1109 alignment
= right
->u
.unary_expression
.u
.unsigned_constant
;
1110 /* Make sure alignment is a power of two */
1111 if (alignment
== 0 || (alignment
& (alignment
- 1)) != 0) {
1112 fprintf(fd
, "[error] %s: align: expecting power of two\n",
1117 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "base")) {
1118 switch (right
->u
.unary_expression
.type
) {
1119 case UNARY_UNSIGNED_CONSTANT
:
1120 switch (right
->u
.unary_expression
.u
.unsigned_constant
) {
1125 base
= right
->u
.unary_expression
.u
.unsigned_constant
;
1128 fprintf(fd
, "[error] %s: base not supported (%" PRIu64
")\n",
1129 __func__
, right
->u
.unary_expression
.u
.unsigned_constant
);
1135 char *s_right
= concatenate_unary_strings(&expression
->u
.ctf_expression
.right
);
1137 fprintf(fd
, "[error] %s: unexpected unary expression for integer base\n", __func__
);
1141 if (!strcmp(s_right
, "decimal") || !strcmp(s_right
, "dec") || !strcmp(s_right
, "d")
1142 || !strcmp(s_right
, "i") || !strcmp(s_right
, "u")) {
1144 } else if (!strcmp(s_right
, "hexadecimal") || !strcmp(s_right
, "hex")
1145 || !strcmp(s_right
, "x") || !strcmp(s_right
, "X")
1146 || !strcmp(s_right
, "p")) {
1148 } else if (!strcmp(s_right
, "octal") || !strcmp(s_right
, "oct")
1149 || !strcmp(s_right
, "o")) {
1151 } else if (!strcmp(s_right
, "binary") || !strcmp(s_right
, "b")) {
1154 fprintf(fd
, "[error] %s: unexpected expression for integer base (%s)\n", __func__
, s_right
);
1163 fprintf(fd
, "[error] %s: base: expecting unsigned constant or unary string\n",
1168 fprintf(fd
, "[error] %s: unknown attribute name %s\n",
1169 __func__
, left
->u
.unary_expression
.u
.string
);
1174 fprintf(fd
, "[error] %s: missing size attribute\n", __func__
);
1177 if (!has_alignment
) {
1178 if (size
% CHAR_BIT
) {
1179 /* bit-packed alignment */
1182 /* byte-packed alignment */
1183 alignment
= CHAR_BIT
;
1186 integer_declaration
= integer_declaration_new(size
,
1187 byte_order
, signedness
, alignment
, base
);
1188 return &integer_declaration
->p
;
1192 struct declaration
*ctf_declaration_floating_point_visit(FILE *fd
, int depth
,
1193 struct cds_list_head
*expressions
,
1194 struct ctf_trace
*trace
)
1196 struct ctf_node
*expression
;
1197 uint64_t alignment
, exp_dig
, mant_dig
, byte_order
= trace
->byte_order
;
1198 int has_alignment
= 0, has_exp_dig
= 0, has_mant_dig
= 0;
1199 struct declaration_float
*float_declaration
;
1201 cds_list_for_each_entry(expression
, expressions
, siblings
) {
1202 struct ctf_node
*left
, *right
;
1204 left
= _cds_list_first_entry(&expression
->u
.ctf_expression
.left
, struct ctf_node
, siblings
);
1205 right
= _cds_list_first_entry(&expression
->u
.ctf_expression
.right
, struct ctf_node
, siblings
);
1206 assert(left
->u
.unary_expression
.type
== UNARY_STRING
);
1207 if (!strcmp(left
->u
.unary_expression
.u
.string
, "byte_order")) {
1208 byte_order
= get_byte_order(fd
, depth
, right
, trace
);
1211 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "exp_dig")) {
1212 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
1213 fprintf(fd
, "[error] %s: exp_dig: expecting unsigned constant\n",
1217 exp_dig
= right
->u
.unary_expression
.u
.unsigned_constant
;
1219 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "mant_dig")) {
1220 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
1221 fprintf(fd
, "[error] %s: mant_dig: expecting unsigned constant\n",
1225 mant_dig
= right
->u
.unary_expression
.u
.unsigned_constant
;
1227 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "align")) {
1228 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
1229 fprintf(fd
, "[error] %s: align: expecting unsigned constant\n",
1233 alignment
= right
->u
.unary_expression
.u
.unsigned_constant
;
1234 /* Make sure alignment is a power of two */
1235 if (alignment
== 0 || (alignment
& (alignment
- 1)) != 0) {
1236 fprintf(fd
, "[error] %s: align: expecting power of two\n",
1242 fprintf(fd
, "[error] %s: unknown attribute name %s\n",
1243 __func__
, left
->u
.unary_expression
.u
.string
);
1247 if (!has_mant_dig
) {
1248 fprintf(fd
, "[error] %s: missing mant_dig attribute\n", __func__
);
1252 fprintf(fd
, "[error] %s: missing exp_dig attribute\n", __func__
);
1255 if (!has_alignment
) {
1256 if ((mant_dig
+ exp_dig
) % CHAR_BIT
) {
1257 /* bit-packed alignment */
1260 /* byte-packed alignment */
1261 alignment
= CHAR_BIT
;
1264 float_declaration
= float_declaration_new(mant_dig
, exp_dig
,
1265 byte_order
, alignment
);
1266 return &float_declaration
->p
;
1270 struct declaration
*ctf_declaration_string_visit(FILE *fd
, int depth
,
1271 struct cds_list_head
*expressions
,
1272 struct ctf_trace
*trace
)
1274 struct ctf_node
*expression
;
1275 const char *encoding_c
= NULL
;
1276 enum ctf_string_encoding encoding
= CTF_STRING_UTF8
;
1277 struct declaration_string
*string_declaration
;
1279 cds_list_for_each_entry(expression
, expressions
, siblings
) {
1280 struct ctf_node
*left
, *right
;
1282 left
= _cds_list_first_entry(&expression
->u
.ctf_expression
.left
, struct ctf_node
, siblings
);
1283 right
= _cds_list_first_entry(&expression
->u
.ctf_expression
.right
, struct ctf_node
, siblings
);
1284 assert(left
->u
.unary_expression
.type
== UNARY_STRING
);
1285 if (!strcmp(left
->u
.unary_expression
.u
.string
, "encoding")) {
1286 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
1287 fprintf(fd
, "[error] %s: encoding: expecting string\n",
1291 encoding_c
= right
->u
.unary_expression
.u
.string
;
1293 fprintf(fd
, "[error] %s: unknown attribute name %s\n",
1294 __func__
, left
->u
.unary_expression
.u
.string
);
1298 if (encoding_c
&& !strcmp(encoding_c
, "ASCII"))
1299 encoding
= CTF_STRING_ASCII
;
1300 string_declaration
= string_declaration_new(encoding
);
1301 return &string_declaration
->p
;
1306 struct declaration
*ctf_type_specifier_list_visit(FILE *fd
,
1307 int depth
, struct ctf_node
*type_specifier_list
,
1308 struct declaration_scope
*declaration_scope
,
1309 struct ctf_trace
*trace
)
1311 struct ctf_node
*first
;
1312 struct ctf_node
*node
;
1314 assert(type_specifier_list
->type
== NODE_TYPE_SPECIFIER_LIST
);
1316 first
= _cds_list_first_entry(&type_specifier_list
->u
.type_specifier_list
.head
, struct ctf_node
, siblings
);
1318 assert(first
->type
== NODE_TYPE_SPECIFIER
);
1320 node
= first
->u
.type_specifier
.node
;
1322 switch (first
->u
.type_specifier
.type
) {
1323 case TYPESPEC_FLOATING_POINT
:
1324 return ctf_declaration_floating_point_visit(fd
, depth
,
1325 &node
->u
.floating_point
.expressions
, trace
);
1326 case TYPESPEC_INTEGER
:
1327 return ctf_declaration_integer_visit(fd
, depth
,
1328 &node
->u
.integer
.expressions
, trace
);
1329 case TYPESPEC_STRING
:
1330 return ctf_declaration_string_visit(fd
, depth
,
1331 &node
->u
.string
.expressions
, trace
);
1332 case TYPESPEC_STRUCT
:
1333 return ctf_declaration_struct_visit(fd
, depth
,
1334 node
->u
._struct
.name
,
1335 &node
->u
._struct
.declaration_list
,
1336 node
->u
._struct
.has_body
,
1337 &node
->u
._struct
.min_align
,
1340 case TYPESPEC_VARIANT
:
1341 return ctf_declaration_variant_visit(fd
, depth
,
1342 node
->u
.variant
.name
,
1343 node
->u
.variant
.choice
,
1344 &node
->u
.variant
.declaration_list
,
1345 node
->u
.variant
.has_body
,
1349 return ctf_declaration_enum_visit(fd
, depth
,
1350 node
->u
._enum
.enum_id
,
1351 node
->u
._enum
.container_type
,
1352 &node
->u
._enum
.enumerator_list
,
1353 node
->u
._enum
.has_body
,
1359 case TYPESPEC_SHORT
:
1362 case TYPESPEC_FLOAT
:
1363 case TYPESPEC_DOUBLE
:
1364 case TYPESPEC_SIGNED
:
1365 case TYPESPEC_UNSIGNED
:
1367 case TYPESPEC_COMPLEX
:
1368 case TYPESPEC_IMAGINARY
:
1369 case TYPESPEC_CONST
:
1370 case TYPESPEC_ID_TYPE
:
1371 return ctf_declaration_type_specifier_visit(fd
, depth
,
1372 type_specifier_list
, declaration_scope
);
1374 fprintf(fd
, "[error] %s: unexpected node type %d\n", __func__
, (int) first
->u
.type_specifier
.type
);
1380 int ctf_event_declaration_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_event
*event
, struct ctf_trace
*trace
)
1384 switch (node
->type
) {
1386 ret
= ctf_typedef_visit(fd
, depth
+ 1,
1387 event
->declaration_scope
,
1388 node
->u
._typedef
.type_specifier_list
,
1389 &node
->u
._typedef
.type_declarators
,
1394 case NODE_TYPEALIAS
:
1395 ret
= ctf_typealias_visit(fd
, depth
+ 1,
1396 event
->declaration_scope
,
1397 node
->u
.typealias
.target
, node
->u
.typealias
.alias
,
1402 case NODE_CTF_EXPRESSION
:
1406 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
1407 if (!strcmp(left
, "name")) {
1410 if (CTF_EVENT_FIELD_IS_SET(event
, name
)) {
1411 fprintf(fd
, "[error] %s: name already declared in event declaration\n", __func__
);
1415 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
1417 fprintf(fd
, "[error] %s: unexpected unary expression for event name\n", __func__
);
1421 event
->name
= g_quark_from_string(right
);
1423 CTF_EVENT_SET_FIELD(event
, name
);
1424 } else if (!strcmp(left
, "id")) {
1425 if (CTF_EVENT_FIELD_IS_SET(event
, id
)) {
1426 fprintf(fd
, "[error] %s: id already declared in event declaration\n", __func__
);
1430 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &event
->id
);
1432 fprintf(fd
, "[error] %s: unexpected unary expression for event id\n", __func__
);
1436 CTF_EVENT_SET_FIELD(event
, id
);
1437 } else if (!strcmp(left
, "stream_id")) {
1438 if (CTF_EVENT_FIELD_IS_SET(event
, stream_id
)) {
1439 fprintf(fd
, "[error] %s: stream_id already declared in event declaration\n", __func__
);
1443 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &event
->stream_id
);
1445 fprintf(fd
, "[error] %s: unexpected unary expression for event stream_id\n", __func__
);
1449 event
->stream
= trace_stream_lookup(trace
, event
->stream_id
);
1450 if (!event
->stream
) {
1451 fprintf(fd
, "[error] %s: stream id %" PRIu64
" cannot be found\n", __func__
, event
->stream_id
);
1455 CTF_EVENT_SET_FIELD(event
, stream_id
);
1456 } else if (!strcmp(left
, "context")) {
1457 struct declaration
*declaration
;
1459 if (event
->context_decl
) {
1460 fprintf(fd
, "[error] %s: context already declared in event declaration\n", __func__
);
1464 declaration
= ctf_type_specifier_list_visit(fd
, depth
,
1465 _cds_list_first_entry(&node
->u
.ctf_expression
.right
,
1466 struct ctf_node
, siblings
),
1467 event
->declaration_scope
, trace
);
1472 if (declaration
->id
!= CTF_TYPE_STRUCT
) {
1476 event
->context_decl
= container_of(declaration
, struct declaration_struct
, p
);
1477 } else if (!strcmp(left
, "fields")) {
1478 struct declaration
*declaration
;
1480 if (event
->fields_decl
) {
1481 fprintf(fd
, "[error] %s: fields already declared in event declaration\n", __func__
);
1485 declaration
= ctf_type_specifier_list_visit(fd
, depth
,
1486 _cds_list_first_entry(&node
->u
.ctf_expression
.right
,
1487 struct ctf_node
, siblings
),
1488 event
->declaration_scope
, trace
);
1493 if (declaration
->id
!= CTF_TYPE_STRUCT
) {
1497 event
->fields_decl
= container_of(declaration
, struct declaration_struct
, p
);
1499 fprintf(fd
, "[error] %s: attribute \"%s\" is unknown in event declaration.\n", __func__
, left
);
1509 /* TODO: declaration specifier should be added. */
1516 int ctf_event_visit(FILE *fd
, int depth
, struct ctf_node
*node
,
1517 struct declaration_scope
*parent_declaration_scope
, struct ctf_trace
*trace
)
1520 struct ctf_node
*iter
;
1521 struct ctf_event
*event
;
1522 struct definition_scope
*parent_def_scope
;
1524 event
= g_new0(struct ctf_event
, 1);
1525 event
->declaration_scope
= new_declaration_scope(parent_declaration_scope
);
1526 cds_list_for_each_entry(iter
, &node
->u
.event
.declaration_list
, siblings
) {
1527 ret
= ctf_event_declaration_visit(fd
, depth
+ 1, iter
, event
, trace
);
1531 if (!CTF_EVENT_FIELD_IS_SET(event
, name
)) {
1533 fprintf(fd
, "[error] %s: missing name field in event declaration\n", __func__
);
1536 if (!CTF_EVENT_FIELD_IS_SET(event
, stream_id
)) {
1537 /* Allow missing stream_id if there is only a single stream */
1538 switch (trace
->streams
->len
) {
1539 case 0: /* Create stream if there was none. */
1540 ret
= ctf_stream_visit(fd
, depth
, NULL
, trace
->root_declaration_scope
, trace
);
1545 event
->stream_id
= 0;
1546 event
->stream
= trace_stream_lookup(trace
, event
->stream_id
);
1550 fprintf(fd
, "[error] %s: missing stream_id field in event declaration\n", __func__
);
1554 /* Allow only one event without id per stream */
1555 if (!CTF_EVENT_FIELD_IS_SET(event
, id
)
1556 && event
->stream
->events_by_id
->len
!= 0) {
1558 fprintf(fd
, "[error] %s: missing id field in event declaration\n", __func__
);
1561 if (event
->stream
->events_by_id
->len
<= event
->id
)
1562 g_ptr_array_set_size(event
->stream
->events_by_id
, event
->id
+ 1);
1563 g_ptr_array_index(event
->stream
->events_by_id
, event
->id
) = event
;
1564 g_hash_table_insert(event
->stream
->event_quark_to_id
,
1565 (gpointer
)(unsigned long) event
->name
,
1567 parent_def_scope
= event
->stream
->definition_scope
;
1568 if (event
->context_decl
) {
1569 struct definition
*definition
=
1570 event
->context_decl
->p
.definition_new(&event
->context_decl
->p
,
1571 parent_def_scope
, 0, 0, "event.context");
1576 event
->context
= container_of(definition
,
1577 struct definition_struct
, p
);
1578 parent_def_scope
= event
->context
->scope
;
1580 if (event
->fields_decl
) {
1581 struct definition
*definition
=
1582 event
->fields_decl
->p
.definition_new(&event
->fields_decl
->p
,
1583 parent_def_scope
, 0, 0, "event.fields");
1588 event
->fields
= container_of(definition
,
1589 struct definition_struct
, p
);
1590 parent_def_scope
= event
->fields
->scope
;
1596 definition_unref(&event
->context
->p
);
1598 definition_unref(&event
->fields
->p
);
1599 if (event
->fields_decl
)
1600 declaration_unref(&event
->fields_decl
->p
);
1601 if (event
->context_decl
)
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
);
1714 fprintf(fd
, "[error] %s: attribute \"%s\" is unknown in stream declaration.\n", __func__
, left
);
1725 /* TODO: declaration specifier should be added. */
1732 int ctf_stream_visit(FILE *fd
, int depth
, struct ctf_node
*node
,
1733 struct declaration_scope
*parent_declaration_scope
, struct ctf_trace
*trace
)
1736 struct ctf_node
*iter
;
1737 struct ctf_stream_class
*stream
;
1738 struct definition_scope
*parent_def_scope
;
1740 stream
= g_new0(struct ctf_stream_class
, 1);
1741 stream
->declaration_scope
= new_declaration_scope(parent_declaration_scope
);
1742 stream
->events_by_id
= g_ptr_array_new();
1743 stream
->event_quark_to_id
= g_hash_table_new(g_direct_hash
, g_direct_equal
);
1744 stream
->files
= g_ptr_array_new();
1746 cds_list_for_each_entry(iter
, &node
->u
.stream
.declaration_list
, siblings
) {
1747 ret
= ctf_stream_declaration_visit(fd
, depth
+ 1, iter
, stream
, trace
);
1752 if (CTF_STREAM_FIELD_IS_SET(stream
, stream_id
)) {
1753 /* check that packet header has stream_id field. */
1754 if (!trace
->packet_header_decl
1755 || struct_declaration_lookup_field_index(trace
->packet_header_decl
, g_quark_from_static_string("stream_id")) < 0) {
1757 fprintf(fd
, "[error] %s: missing stream_id field in packet header declaration, but stream_id attribute is declared for stream.\n", __func__
);
1761 /* Allow only one id-less stream */
1762 if (trace
->streams
->len
!= 0) {
1764 fprintf(fd
, "[error] %s: missing id field in stream declaration\n", __func__
);
1767 stream
->stream_id
= 0;
1769 if (trace
->streams
->len
<= stream
->stream_id
)
1770 g_ptr_array_set_size(trace
->streams
, stream
->stream_id
+ 1);
1771 g_ptr_array_index(trace
->streams
, stream
->stream_id
) = stream
;
1773 parent_def_scope
= trace
->definition_scope
;
1774 if (stream
->packet_context_decl
) {
1775 struct definition
*definition
=
1776 stream
->packet_context_decl
->p
.definition_new(&stream
->packet_context_decl
->p
,
1777 parent_def_scope
, 0, 0, "stream.packet.context");
1782 stream
->packet_context
= container_of(definition
,
1783 struct definition_struct
, p
);
1784 parent_def_scope
= stream
->packet_context
->scope
;
1786 if (stream
->event_header_decl
) {
1787 struct definition
*definition
=
1788 stream
->event_header_decl
->p
.definition_new(&stream
->event_header_decl
->p
,
1789 parent_def_scope
, 0, 0, "stream.event.header");
1794 stream
->event_header
=
1795 container_of(definition
, struct definition_struct
, p
);
1796 parent_def_scope
= stream
->event_header
->scope
;
1798 if (stream
->event_context_decl
) {
1799 struct definition
*definition
=
1800 stream
->event_context_decl
->p
.definition_new(&stream
->event_context_decl
->p
,
1801 parent_def_scope
, 0, 0, "stream.event.context");
1806 stream
->event_context
=
1807 container_of(definition
, struct definition_struct
, p
);
1808 parent_def_scope
= stream
->event_context
->scope
;
1810 stream
->definition_scope
= parent_def_scope
;
1815 if (stream
->event_context
)
1816 definition_unref(&stream
->event_context
->p
);
1817 if (stream
->event_header
)
1818 definition_unref(&stream
->event_header
->p
);
1819 if (stream
->packet_context
)
1820 definition_unref(&stream
->packet_context
->p
);
1821 if (stream
->event_header_decl
)
1822 declaration_unref(&stream
->event_header_decl
->p
);
1823 if (stream
->event_context_decl
)
1824 declaration_unref(&stream
->event_context_decl
->p
);
1825 if (stream
->packet_context_decl
)
1826 declaration_unref(&stream
->packet_context_decl
->p
);
1827 g_ptr_array_free(stream
->files
, TRUE
);
1828 g_ptr_array_free(stream
->events_by_id
, TRUE
);
1829 g_hash_table_destroy(stream
->event_quark_to_id
);
1830 free_declaration_scope(stream
->declaration_scope
);
1836 int ctf_trace_declaration_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_trace
*trace
)
1840 switch (node
->type
) {
1842 ret
= ctf_typedef_visit(fd
, depth
+ 1,
1843 trace
->declaration_scope
,
1844 node
->u
._typedef
.type_specifier_list
,
1845 &node
->u
._typedef
.type_declarators
,
1850 case NODE_TYPEALIAS
:
1851 ret
= ctf_typealias_visit(fd
, depth
+ 1,
1852 trace
->declaration_scope
,
1853 node
->u
.typealias
.target
, node
->u
.typealias
.alias
,
1858 case NODE_CTF_EXPRESSION
:
1862 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
1863 if (!strcmp(left
, "major")) {
1864 if (CTF_TRACE_FIELD_IS_SET(trace
, major
)) {
1865 fprintf(fd
, "[error] %s: major already declared in trace declaration\n", __func__
);
1869 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &trace
->major
);
1871 fprintf(fd
, "[error] %s: unexpected unary expression for trace major number\n", __func__
);
1875 CTF_TRACE_SET_FIELD(trace
, major
);
1876 } else if (!strcmp(left
, "minor")) {
1877 if (CTF_TRACE_FIELD_IS_SET(trace
, minor
)) {
1878 fprintf(fd
, "[error] %s: minor already declared in trace declaration\n", __func__
);
1882 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &trace
->minor
);
1884 fprintf(fd
, "[error] %s: unexpected unary expression for trace minor number\n", __func__
);
1888 CTF_TRACE_SET_FIELD(trace
, minor
);
1889 } else if (!strcmp(left
, "uuid")) {
1890 if (CTF_TRACE_FIELD_IS_SET(trace
, uuid
)) {
1891 fprintf(fd
, "[error] %s: uuid already declared in trace declaration\n", __func__
);
1895 ret
= get_unary_uuid(&node
->u
.ctf_expression
.right
, &trace
->uuid
);
1897 fprintf(fd
, "[error] %s: unexpected unary expression for trace uuid\n", __func__
);
1901 CTF_TRACE_SET_FIELD(trace
, uuid
);
1902 } else if (!strcmp(left
, "byte_order")) {
1903 struct ctf_node
*right
;
1906 if (CTF_TRACE_FIELD_IS_SET(trace
, byte_order
)) {
1907 fprintf(fd
, "[error] %s: endianness already declared in trace declaration\n", __func__
);
1911 right
= _cds_list_first_entry(&node
->u
.ctf_expression
.right
, struct ctf_node
, siblings
);
1912 byte_order
= get_trace_byte_order(fd
, depth
, right
);
1915 trace
->byte_order
= byte_order
;
1916 CTF_TRACE_SET_FIELD(trace
, byte_order
);
1917 } else if (!strcmp(left
, "packet.header")) {
1918 struct declaration
*declaration
;
1920 if (trace
->packet_header_decl
) {
1921 fprintf(fd
, "[error] %s: packet.header already declared in trace declaration\n", __func__
);
1925 declaration
= ctf_type_specifier_list_visit(fd
, depth
,
1926 _cds_list_first_entry(&node
->u
.ctf_expression
.right
,
1927 struct ctf_node
, siblings
),
1928 trace
->declaration_scope
, trace
);
1933 if (declaration
->id
!= CTF_TYPE_STRUCT
) {
1937 trace
->packet_header_decl
= container_of(declaration
, struct declaration_struct
, p
);
1939 fprintf(fd
, "[error] %s: attribute \"%s\" is unknown in trace declaration.\n", __func__
, left
);
1950 /* TODO: declaration specifier should be added. */
1957 int ctf_trace_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_trace
*trace
)
1959 struct definition_scope
*parent_def_scope
;
1961 struct ctf_node
*iter
;
1963 if (trace
->declaration_scope
)
1965 trace
->declaration_scope
= new_declaration_scope(trace
->root_declaration_scope
);
1966 trace
->streams
= g_ptr_array_new();
1967 cds_list_for_each_entry(iter
, &node
->u
.trace
.declaration_list
, siblings
) {
1968 ret
= ctf_trace_declaration_visit(fd
, depth
+ 1, iter
, trace
);
1972 if (!CTF_TRACE_FIELD_IS_SET(trace
, major
)) {
1974 fprintf(fd
, "[error] %s: missing major field in trace declaration\n", __func__
);
1977 if (!CTF_TRACE_FIELD_IS_SET(trace
, minor
)) {
1979 fprintf(fd
, "[error] %s: missing minor field in trace declaration\n", __func__
);
1982 if (!CTF_TRACE_FIELD_IS_SET(trace
, uuid
)) {
1984 fprintf(fd
, "[error] %s: missing uuid field in trace declaration\n", __func__
);
1987 if (!CTF_TRACE_FIELD_IS_SET(trace
, byte_order
)) {
1989 fprintf(fd
, "[error] %s: missing byte_order field in trace declaration\n", __func__
);
1993 parent_def_scope
= NULL
;
1994 if (trace
->packet_header_decl
) {
1995 struct definition
*definition
=
1996 trace
->packet_header_decl
->p
.definition_new(&trace
->packet_header_decl
->p
,
1997 parent_def_scope
, 0, 0, "trace.packet.header");
2002 trace
->packet_header
=
2003 container_of(definition
, struct definition_struct
, p
);
2004 parent_def_scope
= trace
->packet_header
->scope
;
2006 trace
->definition_scope
= parent_def_scope
;
2008 if (!CTF_TRACE_FIELD_IS_SET(trace
, byte_order
)) {
2009 /* check that the packet header contains a "magic" field */
2010 if (!trace
->packet_header
2011 || struct_declaration_lookup_field_index(trace
->packet_header_decl
, g_quark_from_static_string("magic")) < 0) {
2013 fprintf(fd
, "[error] %s: missing both byte_order and packet header magic number in trace declaration\n", __func__
);
2020 if (trace
->packet_header
)
2021 definition_unref(&trace
->packet_header
->p
);
2022 if (trace
->packet_header_decl
)
2023 declaration_unref(&trace
->packet_header_decl
->p
);
2024 g_ptr_array_free(trace
->streams
, TRUE
);
2025 free_declaration_scope(trace
->declaration_scope
);
2030 int ctf_root_declaration_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_trace
*trace
)
2034 switch (node
->type
) {
2036 ret
= ctf_typedef_visit(fd
, depth
+ 1,
2037 trace
->root_declaration_scope
,
2038 node
->u
._typedef
.type_specifier_list
,
2039 &node
->u
._typedef
.type_declarators
,
2044 case NODE_TYPEALIAS
:
2045 ret
= ctf_typealias_visit(fd
, depth
+ 1,
2046 trace
->root_declaration_scope
,
2047 node
->u
.typealias
.target
, node
->u
.typealias
.alias
,
2052 case NODE_TYPE_SPECIFIER_LIST
:
2054 struct declaration
*declaration
;
2057 * Just add the type specifier to the root scope
2058 * declaration scope. Release local reference.
2060 declaration
= ctf_type_specifier_list_visit(fd
, depth
+ 1,
2061 node
, trace
->root_declaration_scope
, trace
);
2064 declaration_unref(declaration
);
2074 int ctf_visitor_construct_metadata(FILE *fd
, int depth
, struct ctf_node
*node
,
2075 struct ctf_trace
*trace
, int byte_order
)
2078 struct ctf_node
*iter
;
2080 printf_verbose("CTF visitor: metadata construction... ");
2081 trace
->root_declaration_scope
= new_declaration_scope(NULL
);
2082 trace
->streams
= g_ptr_array_new();
2083 trace
->byte_order
= byte_order
;
2085 switch (node
->type
) {
2087 cds_list_for_each_entry(iter
, &node
->u
.root
.declaration_list
,
2089 ret
= ctf_root_declaration_visit(fd
, depth
+ 1, iter
, trace
);
2091 fprintf(fd
, "[error] %s: root declaration error\n", __func__
);
2095 cds_list_for_each_entry(iter
, &node
->u
.root
.trace
, siblings
) {
2096 ret
= ctf_trace_visit(fd
, depth
+ 1, iter
, trace
);
2098 fprintf(fd
, "[error] %s: trace declaration error\n", __func__
);
2102 if (!trace
->streams
) {
2103 fprintf(fd
, "[error] %s: missing trace declaration\n", __func__
);
2106 cds_list_for_each_entry(iter
, &node
->u
.root
.stream
, siblings
) {
2107 ret
= ctf_stream_visit(fd
, depth
+ 1, iter
,
2108 trace
->root_declaration_scope
, trace
);
2110 fprintf(fd
, "[error] %s: stream declaration error\n", __func__
);
2114 cds_list_for_each_entry(iter
, &node
->u
.root
.event
, siblings
) {
2115 ret
= ctf_event_visit(fd
, depth
+ 1, iter
,
2116 trace
->root_declaration_scope
, trace
);
2118 fprintf(fd
, "[error] %s: event declaration error\n", __func__
);
2125 fprintf(fd
, "[error] %s: unknown node type %d\n", __func__
,
2129 printf_verbose("done.\n");