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(src_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, integer_declaration
->encoding
);
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 if (!nested_declaration
) {
354 fprintf(fd
, "[error] %s: nested type is unknown.\n", __func__
);
358 /* create array/sequence, pass nested_declaration as child. */
359 if (cds_list_empty(&node_type_declarator
->u
.type_declarator
.u
.nested
.length
)) {
360 fprintf(fd
, "[error] %s: expecting length field reference or value.\n", __func__
);
363 first
= _cds_list_first_entry(&node_type_declarator
->u
.type_declarator
.u
.nested
.length
,
364 struct ctf_node
, siblings
);
365 assert(first
->type
== NODE_UNARY_EXPRESSION
);
367 switch (first
->u
.unary_expression
.type
) {
368 case UNARY_UNSIGNED_CONSTANT
:
370 struct declaration_array
*array_declaration
;
373 len
= first
->u
.unary_expression
.u
.unsigned_constant
;
374 array_declaration
= array_declaration_new(len
, nested_declaration
,
377 if (!array_declaration
) {
378 fprintf(fd
, "[error] %s: cannot create array declaration.\n", __func__
);
381 declaration
= &array_declaration
->p
;
386 /* Lookup unsigned integer definition, create sequence */
387 char *length_name
= concatenate_unary_strings(&node_type_declarator
->u
.type_declarator
.u
.nested
.length
);
388 struct declaration_sequence
*sequence_declaration
;
390 sequence_declaration
= sequence_declaration_new(length_name
, nested_declaration
, declaration_scope
);
391 if (!sequence_declaration
) {
392 fprintf(fd
, "[error] %s: cannot create sequence declaration.\n", __func__
);
395 declaration
= &sequence_declaration
->p
;
402 /* Pass it as content of outer container */
403 declaration
= ctf_type_declarator_visit(fd
, depth
,
404 type_specifier_list
, field_name
,
405 node_type_declarator
->u
.type_declarator
.u
.nested
.type_declarator
,
406 declaration_scope
, declaration
, trace
);
412 int ctf_struct_type_declarators_visit(FILE *fd
, int depth
,
413 struct declaration_struct
*struct_declaration
,
414 struct ctf_node
*type_specifier_list
,
415 struct cds_list_head
*type_declarators
,
416 struct declaration_scope
*declaration_scope
,
417 struct ctf_trace
*trace
)
419 struct ctf_node
*iter
;
422 cds_list_for_each_entry(iter
, type_declarators
, siblings
) {
423 struct declaration
*field_declaration
;
425 field_declaration
= ctf_type_declarator_visit(fd
, depth
,
428 struct_declaration
->scope
,
430 if (!field_declaration
) {
431 fprintf(fd
, "[error] %s: unable to find struct field declaration type\n", __func__
);
435 /* Check if field with same name already exists */
436 if (struct_declaration_lookup_field_index(struct_declaration
, field_name
) >= 0) {
437 fprintf(fd
, "[error] %s: duplicate field %s in struct\n", __func__
, g_quark_to_string(field_name
));
441 struct_declaration_add_field(struct_declaration
,
442 g_quark_to_string(field_name
),
449 int ctf_variant_type_declarators_visit(FILE *fd
, int depth
,
450 struct declaration_untagged_variant
*untagged_variant_declaration
,
451 struct ctf_node
*type_specifier_list
,
452 struct cds_list_head
*type_declarators
,
453 struct declaration_scope
*declaration_scope
,
454 struct ctf_trace
*trace
)
456 struct ctf_node
*iter
;
459 cds_list_for_each_entry(iter
, type_declarators
, siblings
) {
460 struct declaration
*field_declaration
;
462 field_declaration
= ctf_type_declarator_visit(fd
, depth
,
465 untagged_variant_declaration
->scope
,
467 if (!field_declaration
) {
468 fprintf(fd
, "[error] %s: unable to find variant field declaration type\n", __func__
);
472 if (untagged_variant_declaration_get_field_from_tag(untagged_variant_declaration
, field_name
) != NULL
) {
473 fprintf(fd
, "[error] %s: duplicate field %s in variant\n", __func__
, g_quark_to_string(field_name
));
478 untagged_variant_declaration_add_field(untagged_variant_declaration
,
479 g_quark_to_string(field_name
),
486 int ctf_typedef_visit(FILE *fd
, int depth
, struct declaration_scope
*scope
,
487 struct ctf_node
*type_specifier_list
,
488 struct cds_list_head
*type_declarators
,
489 struct ctf_trace
*trace
)
491 struct ctf_node
*iter
;
494 cds_list_for_each_entry(iter
, type_declarators
, siblings
) {
495 struct declaration
*type_declaration
;
498 type_declaration
= ctf_type_declarator_visit(fd
, depth
,
502 if (!type_declaration
) {
503 fprintf(fd
, "[error] %s: problem creating type declaration\n", __func__
);
507 * Don't allow typedef and typealias of untagged
510 if (type_declaration
->id
== CTF_TYPE_UNTAGGED_VARIANT
) {
511 fprintf(fd
, "[error] %s: typedef of untagged variant is not permitted.\n", __func__
);
512 declaration_unref(type_declaration
);
515 ret
= register_declaration(identifier
, type_declaration
, scope
);
517 type_declaration
->declaration_free(type_declaration
);
525 int ctf_typealias_visit(FILE *fd
, int depth
, struct declaration_scope
*scope
,
526 struct ctf_node
*target
, struct ctf_node
*alias
,
527 struct ctf_trace
*trace
)
529 struct declaration
*type_declaration
;
530 struct ctf_node
*node
;
535 /* See ctf_visitor_type_declarator() in the semantic validator. */
538 * Create target type declaration.
541 if (cds_list_empty(&target
->u
.typealias_target
.type_declarators
))
544 node
= _cds_list_first_entry(&target
->u
.typealias_target
.type_declarators
,
545 struct ctf_node
, siblings
);
546 type_declaration
= ctf_type_declarator_visit(fd
, depth
,
547 target
->u
.typealias_target
.type_specifier_list
,
550 if (!type_declaration
) {
551 fprintf(fd
, "[error] %s: problem creating type declaration\n", __func__
);
556 * Don't allow typedef and typealias of untagged
559 if (type_declaration
->id
== CTF_TYPE_UNTAGGED_VARIANT
) {
560 fprintf(fd
, "[error] %s: typedef of untagged variant is not permitted.\n", __func__
);
561 declaration_unref(type_declaration
);
565 * The semantic validator does not check whether the target is
566 * abstract or not (if it has an identifier). Check it here.
569 fprintf(fd
, "[error] %s: expecting empty identifier\n", __func__
);
574 * Create alias identifier.
577 node
= _cds_list_first_entry(&alias
->u
.typealias_alias
.type_declarators
,
578 struct ctf_node
, siblings
);
579 alias_q
= create_typealias_identifier(fd
, depth
,
580 alias
->u
.typealias_alias
.type_specifier_list
, node
);
581 err
= register_declaration(alias_q
, type_declaration
, scope
);
587 if (type_declaration
) {
588 type_declaration
->declaration_free(type_declaration
);
594 int ctf_struct_declaration_list_visit(FILE *fd
, int depth
,
595 struct ctf_node
*iter
, struct declaration_struct
*struct_declaration
,
596 struct ctf_trace
*trace
)
600 switch (iter
->type
) {
602 /* For each declarator, declare type and add type to struct declaration scope */
603 ret
= ctf_typedef_visit(fd
, depth
,
604 struct_declaration
->scope
,
605 iter
->u
._typedef
.type_specifier_list
,
606 &iter
->u
._typedef
.type_declarators
, trace
);
611 /* Declare type with declarator and add type to struct declaration scope */
612 ret
= ctf_typealias_visit(fd
, depth
,
613 struct_declaration
->scope
,
614 iter
->u
.typealias
.target
,
615 iter
->u
.typealias
.alias
, trace
);
619 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
620 /* Add field to structure declaration */
621 ret
= ctf_struct_type_declarators_visit(fd
, depth
,
623 iter
->u
.struct_or_variant_declaration
.type_specifier_list
,
624 &iter
->u
.struct_or_variant_declaration
.type_declarators
,
625 struct_declaration
->scope
, trace
);
630 fprintf(fd
, "[error] %s: unexpected node type %d\n", __func__
, (int) iter
->type
);
637 int ctf_variant_declaration_list_visit(FILE *fd
, int depth
,
638 struct ctf_node
*iter
,
639 struct declaration_untagged_variant
*untagged_variant_declaration
,
640 struct ctf_trace
*trace
)
644 switch (iter
->type
) {
646 /* For each declarator, declare type and add type to variant declaration scope */
647 ret
= ctf_typedef_visit(fd
, depth
,
648 untagged_variant_declaration
->scope
,
649 iter
->u
._typedef
.type_specifier_list
,
650 &iter
->u
._typedef
.type_declarators
, trace
);
655 /* Declare type with declarator and add type to variant declaration scope */
656 ret
= ctf_typealias_visit(fd
, depth
,
657 untagged_variant_declaration
->scope
,
658 iter
->u
.typealias
.target
,
659 iter
->u
.typealias
.alias
, trace
);
663 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
664 /* Add field to structure declaration */
665 ret
= ctf_variant_type_declarators_visit(fd
, depth
,
666 untagged_variant_declaration
,
667 iter
->u
.struct_or_variant_declaration
.type_specifier_list
,
668 &iter
->u
.struct_or_variant_declaration
.type_declarators
,
669 untagged_variant_declaration
->scope
, trace
);
674 fprintf(fd
, "[error] %s: unexpected node type %d\n", __func__
, (int) iter
->type
);
681 struct declaration
*ctf_declaration_struct_visit(FILE *fd
,
682 int depth
, const char *name
, struct cds_list_head
*declaration_list
,
683 int has_body
, struct cds_list_head
*min_align
,
684 struct declaration_scope
*declaration_scope
,
685 struct ctf_trace
*trace
)
687 struct declaration_struct
*struct_declaration
;
688 struct ctf_node
*iter
;
692 * For named struct (without body), lookup in
693 * declaration scope. Don't take reference on struct
694 * declaration: ref is only taken upon definition.
699 lookup_struct_declaration(g_quark_from_string(name
),
701 return &struct_declaration
->p
;
703 uint64_t min_align_value
= 0;
705 /* For unnamed struct, create type */
706 /* For named struct (with body), create type and add to declaration scope */
708 if (lookup_struct_declaration(g_quark_from_string(name
),
709 declaration_scope
)) {
711 fprintf(fd
, "[error] %s: struct %s already declared in scope\n", __func__
, name
);
715 if (!cds_list_empty(min_align
)) {
716 ret
= get_unary_unsigned(min_align
, &min_align_value
);
718 fprintf(fd
, "[error] %s: unexpected unary expression for structure \"align\" attribute\n", __func__
);
723 struct_declaration
= struct_declaration_new(declaration_scope
,
725 cds_list_for_each_entry(iter
, declaration_list
, siblings
) {
726 ret
= ctf_struct_declaration_list_visit(fd
, depth
+ 1, iter
,
727 struct_declaration
, trace
);
729 goto error_free_declaration
;
732 ret
= register_struct_declaration(g_quark_from_string(name
),
737 return &struct_declaration
->p
;
739 error_free_declaration
:
740 struct_declaration
->p
.declaration_free(&struct_declaration
->p
);
746 struct declaration
*ctf_declaration_variant_visit(FILE *fd
,
747 int depth
, const char *name
, const char *choice
,
748 struct cds_list_head
*declaration_list
,
749 int has_body
, struct declaration_scope
*declaration_scope
,
750 struct ctf_trace
*trace
)
752 struct declaration_untagged_variant
*untagged_variant_declaration
;
753 struct declaration_variant
*variant_declaration
;
754 struct ctf_node
*iter
;
758 * For named variant (without body), lookup in
759 * declaration scope. Don't take reference on variant
760 * declaration: ref is only taken upon definition.
764 untagged_variant_declaration
=
765 lookup_variant_declaration(g_quark_from_string(name
),
768 /* For unnamed variant, create type */
769 /* For named variant (with body), create type and add to declaration scope */
771 if (lookup_variant_declaration(g_quark_from_string(name
),
772 declaration_scope
)) {
774 fprintf(fd
, "[error] %s: variant %s already declared in scope\n", __func__
, name
);
778 untagged_variant_declaration
= untagged_variant_declaration_new(declaration_scope
);
779 cds_list_for_each_entry(iter
, declaration_list
, siblings
) {
780 ret
= ctf_variant_declaration_list_visit(fd
, depth
+ 1, iter
,
781 untagged_variant_declaration
, trace
);
786 ret
= register_variant_declaration(g_quark_from_string(name
),
787 untagged_variant_declaration
,
793 * if tagged, create tagged variant and return. else return
797 return &untagged_variant_declaration
->p
;
799 variant_declaration
= variant_declaration_new(untagged_variant_declaration
, choice
);
800 if (!variant_declaration
)
802 declaration_unref(&untagged_variant_declaration
->p
);
803 return &variant_declaration
->p
;
806 untagged_variant_declaration
->p
.declaration_free(&untagged_variant_declaration
->p
);
811 int ctf_enumerator_list_visit(FILE *fd
, int depth
,
812 struct ctf_node
*enumerator
,
813 struct declaration_enum
*enum_declaration
)
816 struct ctf_node
*iter
;
818 q
= g_quark_from_string(enumerator
->u
.enumerator
.id
);
819 if (enum_declaration
->integer_declaration
->signedness
) {
823 cds_list_for_each_entry(iter
, &enumerator
->u
.enumerator
.values
, siblings
) {
826 assert(iter
->type
== NODE_UNARY_EXPRESSION
);
832 switch (iter
->u
.unary_expression
.type
) {
833 case UNARY_SIGNED_CONSTANT
:
834 *target
= iter
->u
.unary_expression
.u
.signed_constant
;
836 case UNARY_UNSIGNED_CONSTANT
:
837 *target
= iter
->u
.unary_expression
.u
.unsigned_constant
;
840 fprintf(fd
, "[error] %s: invalid enumerator\n", __func__
);
844 fprintf(fd
, "[error] %s: invalid enumerator\n", __func__
);
851 enum_signed_insert(enum_declaration
, start
, end
, q
);
856 cds_list_for_each_entry(iter
, &enumerator
->u
.enumerator
.values
, siblings
) {
859 assert(iter
->type
== NODE_UNARY_EXPRESSION
);
865 switch (iter
->u
.unary_expression
.type
) {
866 case UNARY_UNSIGNED_CONSTANT
:
867 *target
= iter
->u
.unary_expression
.u
.unsigned_constant
;
869 case UNARY_SIGNED_CONSTANT
:
871 * We don't accept signed constants for enums with unsigned
874 fprintf(fd
, "[error] %s: invalid enumerator (signed constant encountered, but enum container type is unsigned)\n", __func__
);
877 fprintf(fd
, "[error] %s: invalid enumerator\n", __func__
);
881 fprintf(fd
, "[error] %s: invalid enumerator\n", __func__
);
888 enum_unsigned_insert(enum_declaration
, start
, end
, q
);
894 struct declaration
*ctf_declaration_enum_visit(FILE *fd
, int depth
,
896 struct ctf_node
*container_type
,
897 struct cds_list_head
*enumerator_list
,
899 struct declaration_scope
*declaration_scope
,
900 struct ctf_trace
*trace
)
902 struct declaration
*declaration
;
903 struct declaration_enum
*enum_declaration
;
904 struct declaration_integer
*integer_declaration
;
905 struct ctf_node
*iter
;
910 * For named enum (without body), lookup in
911 * declaration scope. Don't take reference on enum
912 * declaration: ref is only taken upon definition.
917 lookup_enum_declaration(g_quark_from_string(name
),
919 return &enum_declaration
->p
;
921 /* For unnamed enum, create type */
922 /* For named enum (with body), create type and add to declaration scope */
924 if (lookup_enum_declaration(g_quark_from_string(name
),
925 declaration_scope
)) {
927 fprintf(fd
, "[error] %s: enum %s already declared in scope\n", __func__
, name
);
931 if (!container_type
) {
932 declaration
= lookup_declaration(g_quark_from_static_string("int"),
935 fprintf(fd
, "[error] %s: \"int\" type declaration missing for enumeration\n", __func__
);
939 declaration
= ctf_type_declarator_visit(fd
, depth
,
946 fprintf(fd
, "[error] %s: unable to create container type for enumeration\n", __func__
);
949 if (declaration
->id
!= CTF_TYPE_INTEGER
) {
950 fprintf(fd
, "[error] %s: container type for enumeration is not integer\n", __func__
);
953 integer_declaration
= container_of(declaration
, struct declaration_integer
, p
);
954 enum_declaration
= enum_declaration_new(integer_declaration
);
955 declaration_unref(&integer_declaration
->p
); /* leave ref to enum */
956 cds_list_for_each_entry(iter
, enumerator_list
, siblings
) {
957 ret
= ctf_enumerator_list_visit(fd
, depth
+ 1, iter
, enum_declaration
);
962 ret
= register_enum_declaration(g_quark_from_string(name
),
967 return &enum_declaration
->p
;
970 enum_declaration
->p
.declaration_free(&enum_declaration
->p
);
975 struct declaration
*ctf_declaration_type_specifier_visit(FILE *fd
, int depth
,
976 struct ctf_node
*type_specifier_list
,
977 struct declaration_scope
*declaration_scope
)
980 struct declaration
*declaration
;
985 str
= g_string_new("");
986 ret
= visit_type_specifier_list(fd
, type_specifier_list
, str
);
989 str_c
= g_string_free(str
, FALSE
);
990 id_q
= g_quark_from_string(str_c
);
992 declaration
= lookup_declaration(id_q
, declaration_scope
);
997 * Returns 0/1 boolean, or < 0 on error.
1000 int get_boolean(FILE *fd
, int depth
, struct ctf_node
*unary_expression
)
1002 if (unary_expression
->type
!= NODE_UNARY_EXPRESSION
) {
1003 fprintf(fd
, "[error] %s: expecting unary expression\n",
1007 switch (unary_expression
->u
.unary_expression
.type
) {
1008 case UNARY_UNSIGNED_CONSTANT
:
1009 if (unary_expression
->u
.unary_expression
.u
.unsigned_constant
== 0)
1013 case UNARY_SIGNED_CONSTANT
:
1014 if (unary_expression
->u
.unary_expression
.u
.signed_constant
== 0)
1019 if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "true"))
1021 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "TRUE"))
1023 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "false"))
1025 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "FALSE"))
1028 fprintf(fd
, "[error] %s: unexpected string \"%s\"\n",
1029 __func__
, unary_expression
->u
.unary_expression
.u
.string
);
1034 fprintf(fd
, "[error] %s: unexpected unary expression type\n",
1042 int get_trace_byte_order(FILE *fd
, int depth
, struct ctf_node
*unary_expression
)
1046 if (unary_expression
->u
.unary_expression
.type
!= UNARY_STRING
) {
1047 fprintf(fd
, "[error] %s: byte_order: expecting string\n",
1051 if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "be"))
1052 byte_order
= BIG_ENDIAN
;
1053 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "le"))
1054 byte_order
= LITTLE_ENDIAN
;
1056 fprintf(fd
, "[error] %s: unexpected string \"%s\". Should be \"native\", \"network\", \"be\" or \"le\".\n",
1057 __func__
, unary_expression
->u
.unary_expression
.u
.string
);
1064 int get_byte_order(FILE *fd
, int depth
, struct ctf_node
*unary_expression
,
1065 struct ctf_trace
*trace
)
1069 if (unary_expression
->u
.unary_expression
.type
!= UNARY_STRING
) {
1070 fprintf(fd
, "[error] %s: byte_order: expecting string\n",
1074 if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "native"))
1075 byte_order
= trace
->byte_order
;
1076 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "network"))
1077 byte_order
= BIG_ENDIAN
;
1078 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "be"))
1079 byte_order
= BIG_ENDIAN
;
1080 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "le"))
1081 byte_order
= LITTLE_ENDIAN
;
1083 fprintf(fd
, "[error] %s: unexpected string \"%s\". Should be \"native\", \"network\", \"be\" or \"le\".\n",
1084 __func__
, unary_expression
->u
.unary_expression
.u
.string
);
1091 struct declaration
*ctf_declaration_integer_visit(FILE *fd
, int depth
,
1092 struct cds_list_head
*expressions
,
1093 struct ctf_trace
*trace
)
1095 struct ctf_node
*expression
;
1096 uint64_t alignment
= 1, size
;
1097 int byte_order
= trace
->byte_order
;
1099 int has_alignment
= 0, has_size
= 0;
1101 enum ctf_string_encoding encoding
= CTF_STRING_NONE
;
1102 struct declaration_integer
*integer_declaration
;
1104 cds_list_for_each_entry(expression
, expressions
, siblings
) {
1105 struct ctf_node
*left
, *right
;
1107 left
= _cds_list_first_entry(&expression
->u
.ctf_expression
.left
, struct ctf_node
, siblings
);
1108 right
= _cds_list_first_entry(&expression
->u
.ctf_expression
.right
, struct ctf_node
, siblings
);
1109 assert(left
->u
.unary_expression
.type
== UNARY_STRING
);
1110 if (!strcmp(left
->u
.unary_expression
.u
.string
, "signed")) {
1111 signedness
= get_boolean(fd
, depth
, right
);
1114 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "byte_order")) {
1115 byte_order
= get_byte_order(fd
, depth
, right
, trace
);
1118 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "size")) {
1119 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
1120 fprintf(fd
, "[error] %s: size: expecting unsigned constant\n",
1124 size
= right
->u
.unary_expression
.u
.unsigned_constant
;
1126 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "align")) {
1127 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
1128 fprintf(fd
, "[error] %s: align: expecting unsigned constant\n",
1132 alignment
= right
->u
.unary_expression
.u
.unsigned_constant
;
1133 /* Make sure alignment is a power of two */
1134 if (alignment
== 0 || (alignment
& (alignment
- 1)) != 0) {
1135 fprintf(fd
, "[error] %s: align: expecting power of two\n",
1140 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "base")) {
1141 switch (right
->u
.unary_expression
.type
) {
1142 case UNARY_UNSIGNED_CONSTANT
:
1143 switch (right
->u
.unary_expression
.u
.unsigned_constant
) {
1148 base
= right
->u
.unary_expression
.u
.unsigned_constant
;
1151 fprintf(fd
, "[error] %s: base not supported (%" PRIu64
")\n",
1152 __func__
, right
->u
.unary_expression
.u
.unsigned_constant
);
1158 char *s_right
= concatenate_unary_strings(&expression
->u
.ctf_expression
.right
);
1160 fprintf(fd
, "[error] %s: unexpected unary expression for integer base\n", __func__
);
1164 if (!strcmp(s_right
, "decimal") || !strcmp(s_right
, "dec") || !strcmp(s_right
, "d")
1165 || !strcmp(s_right
, "i") || !strcmp(s_right
, "u")) {
1167 } else if (!strcmp(s_right
, "hexadecimal") || !strcmp(s_right
, "hex")
1168 || !strcmp(s_right
, "x") || !strcmp(s_right
, "X")
1169 || !strcmp(s_right
, "p")) {
1171 } else if (!strcmp(s_right
, "octal") || !strcmp(s_right
, "oct")
1172 || !strcmp(s_right
, "o")) {
1174 } else if (!strcmp(s_right
, "binary") || !strcmp(s_right
, "b")) {
1177 fprintf(fd
, "[error] %s: unexpected expression for integer base (%s)\n", __func__
, s_right
);
1186 fprintf(fd
, "[error] %s: base: expecting unsigned constant or unary string\n",
1190 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "encoding")) {
1193 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
1194 fprintf(fd
, "[error] %s: encoding: expecting unary string\n",
1198 s_right
= concatenate_unary_strings(&expression
->u
.ctf_expression
.right
);
1200 fprintf(fd
, "[error] %s: unexpected unary expression for integer base\n", __func__
);
1204 if (!strcmp(s_right
, "UTF8")
1205 || !strcmp(s_right
, "utf8")
1206 || !strcmp(s_right
, "utf-8")
1207 || !strcmp(s_right
, "UTF-8"))
1208 encoding
= CTF_STRING_UTF8
;
1209 else if (!strcmp(s_right
, "ASCII")
1210 || !strcmp(s_right
, "ascii"))
1211 encoding
= CTF_STRING_ASCII
;
1212 else if (!strcmp(s_right
, "none"))
1213 encoding
= CTF_STRING_NONE
;
1215 fprintf(fd
, "[error] %s: unknown string encoding \"%s\"\n", __func__
, s_right
);
1221 fprintf(fd
, "[error] %s: unknown attribute name %s\n",
1222 __func__
, left
->u
.unary_expression
.u
.string
);
1227 fprintf(fd
, "[error] %s: missing size attribute\n", __func__
);
1230 if (!has_alignment
) {
1231 if (size
% CHAR_BIT
) {
1232 /* bit-packed alignment */
1235 /* byte-packed alignment */
1236 alignment
= CHAR_BIT
;
1239 integer_declaration
= integer_declaration_new(size
,
1240 byte_order
, signedness
, alignment
,
1242 return &integer_declaration
->p
;
1246 struct declaration
*ctf_declaration_floating_point_visit(FILE *fd
, int depth
,
1247 struct cds_list_head
*expressions
,
1248 struct ctf_trace
*trace
)
1250 struct ctf_node
*expression
;
1251 uint64_t alignment
= 1, exp_dig
= 0, mant_dig
= 0,
1252 byte_order
= trace
->byte_order
;
1253 int has_alignment
= 0, has_exp_dig
= 0, has_mant_dig
= 0;
1254 struct declaration_float
*float_declaration
;
1256 cds_list_for_each_entry(expression
, expressions
, siblings
) {
1257 struct ctf_node
*left
, *right
;
1259 left
= _cds_list_first_entry(&expression
->u
.ctf_expression
.left
, struct ctf_node
, siblings
);
1260 right
= _cds_list_first_entry(&expression
->u
.ctf_expression
.right
, struct ctf_node
, siblings
);
1261 assert(left
->u
.unary_expression
.type
== UNARY_STRING
);
1262 if (!strcmp(left
->u
.unary_expression
.u
.string
, "byte_order")) {
1263 byte_order
= get_byte_order(fd
, depth
, right
, trace
);
1266 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "exp_dig")) {
1267 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
1268 fprintf(fd
, "[error] %s: exp_dig: expecting unsigned constant\n",
1272 exp_dig
= right
->u
.unary_expression
.u
.unsigned_constant
;
1274 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "mant_dig")) {
1275 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
1276 fprintf(fd
, "[error] %s: mant_dig: expecting unsigned constant\n",
1280 mant_dig
= right
->u
.unary_expression
.u
.unsigned_constant
;
1282 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "align")) {
1283 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
1284 fprintf(fd
, "[error] %s: align: expecting unsigned constant\n",
1288 alignment
= right
->u
.unary_expression
.u
.unsigned_constant
;
1289 /* Make sure alignment is a power of two */
1290 if (alignment
== 0 || (alignment
& (alignment
- 1)) != 0) {
1291 fprintf(fd
, "[error] %s: align: expecting power of two\n",
1297 fprintf(fd
, "[error] %s: unknown attribute name %s\n",
1298 __func__
, left
->u
.unary_expression
.u
.string
);
1302 if (!has_mant_dig
) {
1303 fprintf(fd
, "[error] %s: missing mant_dig attribute\n", __func__
);
1307 fprintf(fd
, "[error] %s: missing exp_dig attribute\n", __func__
);
1310 if (!has_alignment
) {
1311 if ((mant_dig
+ exp_dig
) % CHAR_BIT
) {
1312 /* bit-packed alignment */
1315 /* byte-packed alignment */
1316 alignment
= CHAR_BIT
;
1319 float_declaration
= float_declaration_new(mant_dig
, exp_dig
,
1320 byte_order
, alignment
);
1321 return &float_declaration
->p
;
1325 struct declaration
*ctf_declaration_string_visit(FILE *fd
, int depth
,
1326 struct cds_list_head
*expressions
,
1327 struct ctf_trace
*trace
)
1329 struct ctf_node
*expression
;
1330 const char *encoding_c
= NULL
;
1331 enum ctf_string_encoding encoding
= CTF_STRING_UTF8
;
1332 struct declaration_string
*string_declaration
;
1334 cds_list_for_each_entry(expression
, expressions
, siblings
) {
1335 struct ctf_node
*left
, *right
;
1337 left
= _cds_list_first_entry(&expression
->u
.ctf_expression
.left
, struct ctf_node
, siblings
);
1338 right
= _cds_list_first_entry(&expression
->u
.ctf_expression
.right
, struct ctf_node
, siblings
);
1339 assert(left
->u
.unary_expression
.type
== UNARY_STRING
);
1340 if (!strcmp(left
->u
.unary_expression
.u
.string
, "encoding")) {
1341 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
1342 fprintf(fd
, "[error] %s: encoding: expecting string\n",
1346 encoding_c
= right
->u
.unary_expression
.u
.string
;
1348 fprintf(fd
, "[error] %s: unknown attribute name %s\n",
1349 __func__
, left
->u
.unary_expression
.u
.string
);
1353 if (encoding_c
&& !strcmp(encoding_c
, "ASCII"))
1354 encoding
= CTF_STRING_ASCII
;
1355 string_declaration
= string_declaration_new(encoding
);
1356 return &string_declaration
->p
;
1361 struct declaration
*ctf_type_specifier_list_visit(FILE *fd
,
1362 int depth
, struct ctf_node
*type_specifier_list
,
1363 struct declaration_scope
*declaration_scope
,
1364 struct ctf_trace
*trace
)
1366 struct ctf_node
*first
;
1367 struct ctf_node
*node
;
1369 assert(type_specifier_list
->type
== NODE_TYPE_SPECIFIER_LIST
);
1371 first
= _cds_list_first_entry(&type_specifier_list
->u
.type_specifier_list
.head
, struct ctf_node
, siblings
);
1373 assert(first
->type
== NODE_TYPE_SPECIFIER
);
1375 node
= first
->u
.type_specifier
.node
;
1377 switch (first
->u
.type_specifier
.type
) {
1378 case TYPESPEC_FLOATING_POINT
:
1379 return ctf_declaration_floating_point_visit(fd
, depth
,
1380 &node
->u
.floating_point
.expressions
, trace
);
1381 case TYPESPEC_INTEGER
:
1382 return ctf_declaration_integer_visit(fd
, depth
,
1383 &node
->u
.integer
.expressions
, trace
);
1384 case TYPESPEC_STRING
:
1385 return ctf_declaration_string_visit(fd
, depth
,
1386 &node
->u
.string
.expressions
, trace
);
1387 case TYPESPEC_STRUCT
:
1388 return ctf_declaration_struct_visit(fd
, depth
,
1389 node
->u
._struct
.name
,
1390 &node
->u
._struct
.declaration_list
,
1391 node
->u
._struct
.has_body
,
1392 &node
->u
._struct
.min_align
,
1395 case TYPESPEC_VARIANT
:
1396 return ctf_declaration_variant_visit(fd
, depth
,
1397 node
->u
.variant
.name
,
1398 node
->u
.variant
.choice
,
1399 &node
->u
.variant
.declaration_list
,
1400 node
->u
.variant
.has_body
,
1404 return ctf_declaration_enum_visit(fd
, depth
,
1405 node
->u
._enum
.enum_id
,
1406 node
->u
._enum
.container_type
,
1407 &node
->u
._enum
.enumerator_list
,
1408 node
->u
._enum
.has_body
,
1414 case TYPESPEC_SHORT
:
1417 case TYPESPEC_FLOAT
:
1418 case TYPESPEC_DOUBLE
:
1419 case TYPESPEC_SIGNED
:
1420 case TYPESPEC_UNSIGNED
:
1422 case TYPESPEC_COMPLEX
:
1423 case TYPESPEC_IMAGINARY
:
1424 case TYPESPEC_CONST
:
1425 case TYPESPEC_ID_TYPE
:
1426 return ctf_declaration_type_specifier_visit(fd
, depth
,
1427 type_specifier_list
, declaration_scope
);
1429 fprintf(fd
, "[error] %s: unexpected node type %d\n", __func__
, (int) first
->u
.type_specifier
.type
);
1435 int ctf_event_declaration_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_event
*event
, struct ctf_trace
*trace
)
1439 switch (node
->type
) {
1441 ret
= ctf_typedef_visit(fd
, depth
+ 1,
1442 event
->declaration_scope
,
1443 node
->u
._typedef
.type_specifier_list
,
1444 &node
->u
._typedef
.type_declarators
,
1449 case NODE_TYPEALIAS
:
1450 ret
= ctf_typealias_visit(fd
, depth
+ 1,
1451 event
->declaration_scope
,
1452 node
->u
.typealias
.target
, node
->u
.typealias
.alias
,
1457 case NODE_CTF_EXPRESSION
:
1461 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
1462 if (!strcmp(left
, "name")) {
1465 if (CTF_EVENT_FIELD_IS_SET(event
, name
)) {
1466 fprintf(fd
, "[error] %s: name already declared in event declaration\n", __func__
);
1470 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
1472 fprintf(fd
, "[error] %s: unexpected unary expression for event name\n", __func__
);
1476 event
->name
= g_quark_from_string(right
);
1478 CTF_EVENT_SET_FIELD(event
, name
);
1479 } else if (!strcmp(left
, "id")) {
1480 if (CTF_EVENT_FIELD_IS_SET(event
, id
)) {
1481 fprintf(fd
, "[error] %s: id already declared in event declaration\n", __func__
);
1485 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &event
->id
);
1487 fprintf(fd
, "[error] %s: unexpected unary expression for event id\n", __func__
);
1491 CTF_EVENT_SET_FIELD(event
, id
);
1492 } else if (!strcmp(left
, "stream_id")) {
1493 if (CTF_EVENT_FIELD_IS_SET(event
, stream_id
)) {
1494 fprintf(fd
, "[error] %s: stream_id already declared in event declaration\n", __func__
);
1498 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &event
->stream_id
);
1500 fprintf(fd
, "[error] %s: unexpected unary expression for event stream_id\n", __func__
);
1504 event
->stream
= trace_stream_lookup(trace
, event
->stream_id
);
1505 if (!event
->stream
) {
1506 fprintf(fd
, "[error] %s: stream id %" PRIu64
" cannot be found\n", __func__
, event
->stream_id
);
1510 CTF_EVENT_SET_FIELD(event
, stream_id
);
1511 } else if (!strcmp(left
, "context")) {
1512 struct declaration
*declaration
;
1514 if (event
->context_decl
) {
1515 fprintf(fd
, "[error] %s: context already declared in event declaration\n", __func__
);
1519 declaration
= ctf_type_specifier_list_visit(fd
, depth
,
1520 _cds_list_first_entry(&node
->u
.ctf_expression
.right
,
1521 struct ctf_node
, siblings
),
1522 event
->declaration_scope
, trace
);
1527 if (declaration
->id
!= CTF_TYPE_STRUCT
) {
1531 event
->context_decl
= container_of(declaration
, struct declaration_struct
, p
);
1532 } else if (!strcmp(left
, "fields")) {
1533 struct declaration
*declaration
;
1535 if (event
->fields_decl
) {
1536 fprintf(fd
, "[error] %s: fields already declared in event declaration\n", __func__
);
1540 declaration
= ctf_type_specifier_list_visit(fd
, depth
,
1541 _cds_list_first_entry(&node
->u
.ctf_expression
.right
,
1542 struct ctf_node
, siblings
),
1543 event
->declaration_scope
, trace
);
1548 if (declaration
->id
!= CTF_TYPE_STRUCT
) {
1552 event
->fields_decl
= container_of(declaration
, struct declaration_struct
, p
);
1554 fprintf(fd
, "[error] %s: attribute \"%s\" is unknown in event declaration.\n", __func__
, left
);
1564 /* TODO: declaration specifier should be added. */
1571 int ctf_event_visit(FILE *fd
, int depth
, struct ctf_node
*node
,
1572 struct declaration_scope
*parent_declaration_scope
, struct ctf_trace
*trace
)
1575 struct ctf_node
*iter
;
1576 struct ctf_event
*event
;
1578 event
= g_new0(struct ctf_event
, 1);
1579 event
->declaration_scope
= new_declaration_scope(parent_declaration_scope
);
1580 cds_list_for_each_entry(iter
, &node
->u
.event
.declaration_list
, siblings
) {
1581 ret
= ctf_event_declaration_visit(fd
, depth
+ 1, iter
, event
, trace
);
1585 if (!CTF_EVENT_FIELD_IS_SET(event
, name
)) {
1587 fprintf(fd
, "[error] %s: missing name field in event declaration\n", __func__
);
1590 if (!CTF_EVENT_FIELD_IS_SET(event
, stream_id
)) {
1591 /* Allow missing stream_id if there is only a single stream */
1592 switch (trace
->streams
->len
) {
1593 case 0: /* Create stream if there was none. */
1594 ret
= ctf_stream_visit(fd
, depth
, NULL
, trace
->root_declaration_scope
, trace
);
1599 event
->stream_id
= 0;
1600 event
->stream
= trace_stream_lookup(trace
, event
->stream_id
);
1604 fprintf(fd
, "[error] %s: missing stream_id field in event declaration\n", __func__
);
1608 /* Allow only one event without id per stream */
1609 if (!CTF_EVENT_FIELD_IS_SET(event
, id
)
1610 && event
->stream
->events_by_id
->len
!= 0) {
1612 fprintf(fd
, "[error] %s: missing id field in event declaration\n", __func__
);
1615 if (event
->stream
->events_by_id
->len
<= event
->id
)
1616 g_ptr_array_set_size(event
->stream
->events_by_id
, event
->id
+ 1);
1617 g_ptr_array_index(event
->stream
->events_by_id
, event
->id
) = event
;
1618 g_hash_table_insert(event
->stream
->event_quark_to_id
,
1619 (gpointer
)(unsigned long) event
->name
,
1624 if (event
->fields_decl
)
1625 declaration_unref(&event
->fields_decl
->p
);
1626 if (event
->context_decl
)
1627 declaration_unref(&event
->context_decl
->p
);
1628 free_declaration_scope(event
->declaration_scope
);
1635 int ctf_stream_declaration_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_stream_class
*stream
, struct ctf_trace
*trace
)
1639 switch (node
->type
) {
1641 ret
= ctf_typedef_visit(fd
, depth
+ 1,
1642 stream
->declaration_scope
,
1643 node
->u
._typedef
.type_specifier_list
,
1644 &node
->u
._typedef
.type_declarators
,
1649 case NODE_TYPEALIAS
:
1650 ret
= ctf_typealias_visit(fd
, depth
+ 1,
1651 stream
->declaration_scope
,
1652 node
->u
.typealias
.target
, node
->u
.typealias
.alias
,
1657 case NODE_CTF_EXPRESSION
:
1661 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
1662 if (!strcmp(left
, "id")) {
1663 if (CTF_STREAM_FIELD_IS_SET(stream
, stream_id
)) {
1664 fprintf(fd
, "[error] %s: id already declared in stream declaration\n", __func__
);
1668 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &stream
->stream_id
);
1670 fprintf(fd
, "[error] %s: unexpected unary expression for stream id\n", __func__
);
1674 CTF_STREAM_SET_FIELD(stream
, stream_id
);
1675 } else if (!strcmp(left
, "event.header")) {
1676 struct declaration
*declaration
;
1678 if (stream
->event_header_decl
) {
1679 fprintf(fd
, "[error] %s: event.header already declared in stream declaration\n", __func__
);
1683 declaration
= ctf_type_specifier_list_visit(fd
, depth
,
1684 _cds_list_first_entry(&node
->u
.ctf_expression
.right
,
1685 struct ctf_node
, siblings
),
1686 stream
->declaration_scope
, trace
);
1691 if (declaration
->id
!= CTF_TYPE_STRUCT
) {
1695 stream
->event_header_decl
= container_of(declaration
, struct declaration_struct
, p
);
1696 } else if (!strcmp(left
, "event.context")) {
1697 struct declaration
*declaration
;
1699 if (stream
->event_context_decl
) {
1700 fprintf(fd
, "[error] %s: event.context already declared in stream declaration\n", __func__
);
1704 declaration
= ctf_type_specifier_list_visit(fd
, depth
,
1705 _cds_list_first_entry(&node
->u
.ctf_expression
.right
,
1706 struct ctf_node
, siblings
),
1707 stream
->declaration_scope
, trace
);
1712 if (declaration
->id
!= CTF_TYPE_STRUCT
) {
1716 stream
->event_context_decl
= container_of(declaration
, struct declaration_struct
, p
);
1717 } else if (!strcmp(left
, "packet.context")) {
1718 struct declaration
*declaration
;
1720 if (stream
->packet_context_decl
) {
1721 fprintf(fd
, "[error] %s: packet.context already declared in stream declaration\n", __func__
);
1725 declaration
= ctf_type_specifier_list_visit(fd
, depth
,
1726 _cds_list_first_entry(&node
->u
.ctf_expression
.right
,
1727 struct ctf_node
, siblings
),
1728 stream
->declaration_scope
, trace
);
1733 if (declaration
->id
!= CTF_TYPE_STRUCT
) {
1737 stream
->packet_context_decl
= container_of(declaration
, struct declaration_struct
, p
);
1739 fprintf(fd
, "[error] %s: attribute \"%s\" is unknown in stream declaration.\n", __func__
, left
);
1750 /* TODO: declaration specifier should be added. */
1757 int ctf_stream_visit(FILE *fd
, int depth
, struct ctf_node
*node
,
1758 struct declaration_scope
*parent_declaration_scope
, struct ctf_trace
*trace
)
1761 struct ctf_node
*iter
;
1762 struct ctf_stream_class
*stream
;
1764 stream
= g_new0(struct ctf_stream_class
, 1);
1765 stream
->declaration_scope
= new_declaration_scope(parent_declaration_scope
);
1766 stream
->events_by_id
= g_ptr_array_new();
1767 stream
->event_quark_to_id
= g_hash_table_new(g_direct_hash
, g_direct_equal
);
1768 stream
->streams
= g_ptr_array_new();
1770 cds_list_for_each_entry(iter
, &node
->u
.stream
.declaration_list
, siblings
) {
1771 ret
= ctf_stream_declaration_visit(fd
, depth
+ 1, iter
, stream
, trace
);
1776 if (CTF_STREAM_FIELD_IS_SET(stream
, stream_id
)) {
1777 /* check that packet header has stream_id field. */
1778 if (!trace
->packet_header_decl
1779 || struct_declaration_lookup_field_index(trace
->packet_header_decl
, g_quark_from_static_string("stream_id")) < 0) {
1781 fprintf(fd
, "[error] %s: missing stream_id field in packet header declaration, but stream_id attribute is declared for stream.\n", __func__
);
1785 /* Allow only one id-less stream */
1786 if (trace
->streams
->len
!= 0) {
1788 fprintf(fd
, "[error] %s: missing id field in stream declaration\n", __func__
);
1791 stream
->stream_id
= 0;
1793 if (trace
->streams
->len
<= stream
->stream_id
)
1794 g_ptr_array_set_size(trace
->streams
, stream
->stream_id
+ 1);
1795 g_ptr_array_index(trace
->streams
, stream
->stream_id
) = stream
;
1800 if (stream
->event_header_decl
)
1801 declaration_unref(&stream
->event_header_decl
->p
);
1802 if (stream
->event_context_decl
)
1803 declaration_unref(&stream
->event_context_decl
->p
);
1804 if (stream
->packet_context_decl
)
1805 declaration_unref(&stream
->packet_context_decl
->p
);
1806 g_ptr_array_free(stream
->streams
, TRUE
);
1807 g_ptr_array_free(stream
->events_by_id
, TRUE
);
1808 g_hash_table_destroy(stream
->event_quark_to_id
);
1809 free_declaration_scope(stream
->declaration_scope
);
1815 int ctf_trace_declaration_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_trace
*trace
)
1819 switch (node
->type
) {
1821 ret
= ctf_typedef_visit(fd
, depth
+ 1,
1822 trace
->declaration_scope
,
1823 node
->u
._typedef
.type_specifier_list
,
1824 &node
->u
._typedef
.type_declarators
,
1829 case NODE_TYPEALIAS
:
1830 ret
= ctf_typealias_visit(fd
, depth
+ 1,
1831 trace
->declaration_scope
,
1832 node
->u
.typealias
.target
, node
->u
.typealias
.alias
,
1837 case NODE_CTF_EXPRESSION
:
1841 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
1842 if (!strcmp(left
, "major")) {
1843 if (CTF_TRACE_FIELD_IS_SET(trace
, major
)) {
1844 fprintf(fd
, "[error] %s: major already declared in trace declaration\n", __func__
);
1848 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &trace
->major
);
1850 fprintf(fd
, "[error] %s: unexpected unary expression for trace major number\n", __func__
);
1854 CTF_TRACE_SET_FIELD(trace
, major
);
1855 } else if (!strcmp(left
, "minor")) {
1856 if (CTF_TRACE_FIELD_IS_SET(trace
, minor
)) {
1857 fprintf(fd
, "[error] %s: minor already declared in trace declaration\n", __func__
);
1861 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &trace
->minor
);
1863 fprintf(fd
, "[error] %s: unexpected unary expression for trace minor number\n", __func__
);
1867 CTF_TRACE_SET_FIELD(trace
, minor
);
1868 } else if (!strcmp(left
, "uuid")) {
1871 ret
= get_unary_uuid(&node
->u
.ctf_expression
.right
, &uuid
);
1873 fprintf(fd
, "[error] %s: unexpected unary expression for trace uuid\n", __func__
);
1877 if (CTF_TRACE_FIELD_IS_SET(trace
, uuid
)
1878 && uuid_compare(uuid
, trace
->uuid
)) {
1879 fprintf(fd
, "[error] %s: uuid mismatch\n", __func__
);
1883 memcpy(trace
->uuid
, uuid
, sizeof(uuid
));
1885 CTF_TRACE_SET_FIELD(trace
, uuid
);
1886 } else if (!strcmp(left
, "byte_order")) {
1887 struct ctf_node
*right
;
1890 right
= _cds_list_first_entry(&node
->u
.ctf_expression
.right
, struct ctf_node
, siblings
);
1891 byte_order
= get_trace_byte_order(fd
, depth
, right
);
1895 if (CTF_TRACE_FIELD_IS_SET(trace
, byte_order
)
1896 && byte_order
!= trace
->byte_order
) {
1897 fprintf(fd
, "[error] %s: endianness mismatch\n", __func__
);
1901 if (byte_order
!= trace
->byte_order
) {
1902 trace
->byte_order
= byte_order
;
1904 * We need to restart
1905 * construction of the
1906 * intermediate representation.
1911 CTF_TRACE_SET_FIELD(trace
, byte_order
);
1912 } else if (!strcmp(left
, "packet.header")) {
1913 struct declaration
*declaration
;
1915 if (trace
->packet_header_decl
) {
1916 fprintf(fd
, "[error] %s: packet.header already declared in trace declaration\n", __func__
);
1920 declaration
= ctf_type_specifier_list_visit(fd
, depth
,
1921 _cds_list_first_entry(&node
->u
.ctf_expression
.right
,
1922 struct ctf_node
, siblings
),
1923 trace
->declaration_scope
, trace
);
1928 if (declaration
->id
!= CTF_TYPE_STRUCT
) {
1932 trace
->packet_header_decl
= container_of(declaration
, struct declaration_struct
, p
);
1934 fprintf(fd
, "[error] %s: attribute \"%s\" is unknown in trace declaration.\n", __func__
, left
);
1945 /* TODO: declaration specifier should be added. */
1952 int ctf_trace_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_trace
*trace
)
1955 struct ctf_node
*iter
;
1958 if (trace
->declaration_scope
)
1960 trace
->declaration_scope
= new_declaration_scope(trace
->root_declaration_scope
);
1961 trace
->streams
= g_ptr_array_new();
1962 cds_list_for_each_entry(iter
, &node
->u
.trace
.declaration_list
, siblings
) {
1963 ret
= ctf_trace_declaration_visit(fd
, depth
+ 1, iter
, trace
);
1967 if (!CTF_TRACE_FIELD_IS_SET(trace
, major
)) {
1969 fprintf(fd
, "[error] %s: missing major field in trace declaration\n", __func__
);
1972 if (!CTF_TRACE_FIELD_IS_SET(trace
, minor
)) {
1974 fprintf(fd
, "[error] %s: missing minor field in trace declaration\n", __func__
);
1977 if (!CTF_TRACE_FIELD_IS_SET(trace
, uuid
)) {
1979 fprintf(fd
, "[error] %s: missing uuid field in trace declaration\n", __func__
);
1982 if (!CTF_TRACE_FIELD_IS_SET(trace
, byte_order
)) {
1984 fprintf(fd
, "[error] %s: missing byte_order field in trace declaration\n", __func__
);
1988 if (!CTF_TRACE_FIELD_IS_SET(trace
, byte_order
)) {
1989 /* check that the packet header contains a "magic" field */
1990 if (!trace
->packet_header_decl
1991 || struct_declaration_lookup_field_index(trace
->packet_header_decl
, g_quark_from_static_string("magic")) < 0) {
1993 fprintf(fd
, "[error] %s: missing both byte_order and packet header magic number in trace declaration\n", __func__
);
2000 if (trace
->packet_header_decl
)
2001 declaration_unref(&trace
->packet_header_decl
->p
);
2002 g_ptr_array_free(trace
->streams
, TRUE
);
2003 free_declaration_scope(trace
->declaration_scope
);
2004 trace
->declaration_scope
= NULL
;
2005 /* byte order changed while creating types, retry. */
2006 if (ret
== -EINTR
) {
2007 trace
->field_mask
= 0;
2014 int ctf_root_declaration_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_trace
*trace
)
2018 switch (node
->type
) {
2020 ret
= ctf_typedef_visit(fd
, depth
+ 1,
2021 trace
->root_declaration_scope
,
2022 node
->u
._typedef
.type_specifier_list
,
2023 &node
->u
._typedef
.type_declarators
,
2028 case NODE_TYPEALIAS
:
2029 ret
= ctf_typealias_visit(fd
, depth
+ 1,
2030 trace
->root_declaration_scope
,
2031 node
->u
.typealias
.target
, node
->u
.typealias
.alias
,
2036 case NODE_TYPE_SPECIFIER_LIST
:
2038 struct declaration
*declaration
;
2041 * Just add the type specifier to the root scope
2042 * declaration scope. Release local reference.
2044 declaration
= ctf_type_specifier_list_visit(fd
, depth
+ 1,
2045 node
, trace
->root_declaration_scope
, trace
);
2048 declaration_unref(declaration
);
2058 int ctf_visitor_construct_metadata(FILE *fd
, int depth
, struct ctf_node
*node
,
2059 struct ctf_trace
*trace
, int byte_order
)
2062 struct ctf_node
*iter
;
2064 printf_verbose("CTF visitor: metadata construction... ");
2065 trace
->root_declaration_scope
= new_declaration_scope(NULL
);
2066 trace
->byte_order
= byte_order
;
2068 switch (node
->type
) {
2070 cds_list_for_each_entry(iter
, &node
->u
.root
.declaration_list
,
2072 ret
= ctf_root_declaration_visit(fd
, depth
+ 1, iter
, trace
);
2074 fprintf(fd
, "[error] %s: root declaration error\n", __func__
);
2078 cds_list_for_each_entry(iter
, &node
->u
.root
.trace
, siblings
) {
2079 ret
= ctf_trace_visit(fd
, depth
+ 1, iter
, trace
);
2081 fprintf(fd
, "[error] %s: trace declaration error\n", __func__
);
2085 if (!trace
->streams
) {
2086 fprintf(fd
, "[error] %s: missing trace declaration\n", __func__
);
2090 cds_list_for_each_entry(iter
, &node
->u
.root
.stream
, siblings
) {
2091 ret
= ctf_stream_visit(fd
, depth
+ 1, iter
,
2092 trace
->root_declaration_scope
, trace
);
2094 fprintf(fd
, "[error] %s: stream declaration error\n", __func__
);
2098 cds_list_for_each_entry(iter
, &node
->u
.root
.event
, siblings
) {
2099 ret
= ctf_event_visit(fd
, depth
+ 1, iter
,
2100 trace
->root_declaration_scope
, trace
);
2102 fprintf(fd
, "[error] %s: event declaration error\n", __func__
);
2109 fprintf(fd
, "[error] %s: unknown node type %d\n", __func__
,
2114 printf_verbose("done.\n");
2118 free_declaration_scope(trace
->root_declaration_scope
);