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/list.h>
29 #include <babeltrace/types.h>
30 #include <babeltrace/ctf/metadata.h>
31 #include <uuid/uuid.h>
32 #include "ctf-scanner.h"
33 #include "ctf-parser.h"
36 #define fprintf_dbg(fd, fmt, args...) fprintf(fd, "%s: " fmt, __func__, ## args)
38 #define _cds_list_first_entry(ptr, type, member) \
39 cds_list_entry((ptr)->next, type, member)
42 struct declaration
*ctf_type_specifier_list_visit(FILE *fd
,
43 int depth
, struct ctf_node
*type_specifier_list
,
44 struct declaration_scope
*declaration_scope
,
45 struct ctf_trace
*trace
);
48 int ctf_stream_visit(FILE *fd
, int depth
, struct ctf_node
*node
,
49 struct declaration_scope
*parent_declaration_scope
, struct ctf_trace
*trace
);
52 * String returned must be freed by the caller using g_free.
55 char *concatenate_unary_strings(struct cds_list_head
*head
)
57 struct ctf_node
*node
;
61 str
= g_string_new("");
62 cds_list_for_each_entry(node
, head
, siblings
) {
65 assert(node
->type
== NODE_UNARY_EXPRESSION
);
66 assert(node
->u
.unary_expression
.type
== UNARY_STRING
);
67 assert((node
->u
.unary_expression
.link
== UNARY_LINK_UNKNOWN
)
69 switch (node
->u
.unary_expression
.link
) {
71 g_string_append(str
, ".");
74 g_string_append(str
, "->");
77 g_string_append(str
, "...");
82 src_string
= node
->u
.unary_expression
.u
.string
;
83 g_string_append(str
, src_string
);
86 return g_string_free(str
, FALSE
);
90 int get_unary_unsigned(struct cds_list_head
*head
, uint64_t *value
)
92 struct ctf_node
*node
;
95 cds_list_for_each_entry(node
, head
, siblings
) {
96 assert(node
->type
== NODE_UNARY_EXPRESSION
);
97 assert(node
->u
.unary_expression
.type
== UNARY_UNSIGNED_CONSTANT
);
98 assert(node
->u
.unary_expression
.link
== UNARY_LINK_UNKNOWN
);
100 *value
= node
->u
.unary_expression
.u
.unsigned_constant
;
107 int get_unary_uuid(struct cds_list_head
*head
, uuid_t
*uuid
)
109 struct ctf_node
*node
;
113 cds_list_for_each_entry(node
, head
, siblings
) {
114 const char *src_string
;
116 assert(node
->type
== NODE_UNARY_EXPRESSION
);
117 assert(node
->u
.unary_expression
.type
== UNARY_STRING
);
118 assert(node
->u
.unary_expression
.link
== UNARY_LINK_UNKNOWN
);
120 src_string
= node
->u
.unary_expression
.u
.string
;
121 ret
= uuid_parse(node
->u
.unary_expression
.u
.string
, *uuid
);
127 struct ctf_stream
*trace_stream_lookup(struct ctf_trace
*trace
, uint64_t stream_id
)
129 if (trace
->streams
->len
<= stream_id
)
131 return g_ptr_array_index(trace
->streams
, stream_id
);
135 int visit_type_specifier(FILE *fd
, struct ctf_node
*type_specifier
, GString
*str
)
137 assert(type_specifier
->type
== NODE_TYPE_SPECIFIER
);
139 switch (type_specifier
->u
.type_specifier
.type
) {
141 g_string_append(str
, "void");
144 g_string_append(str
, "char");
147 g_string_append(str
, "short");
150 g_string_append(str
, "int");
153 g_string_append(str
, "long");
156 g_string_append(str
, "float");
158 case TYPESPEC_DOUBLE
:
159 g_string_append(str
, "double");
161 case TYPESPEC_SIGNED
:
162 g_string_append(str
, "signed");
164 case TYPESPEC_UNSIGNED
:
165 g_string_append(str
, "unsigned");
168 g_string_append(str
, "bool");
170 case TYPESPEC_COMPLEX
:
171 g_string_append(str
, "_Complex");
173 case TYPESPEC_IMAGINARY
:
174 g_string_append(str
, "_Imaginary");
177 g_string_append(str
, "const");
179 case TYPESPEC_ID_TYPE
:
180 if (type_specifier
->u
.type_specifier
.id_type
)
181 g_string_append(str
, type_specifier
->u
.type_specifier
.id_type
);
183 case TYPESPEC_STRUCT
:
185 struct ctf_node
*node
= type_specifier
->u
.type_specifier
.node
;
187 if (!node
->u
._struct
.name
) {
188 fprintf(fd
, "[error] %s: unexpected empty variant name\n", __func__
);
191 g_string_append(str
, "struct ");
192 g_string_append(str
, node
->u
._struct
.name
);
195 case TYPESPEC_VARIANT
:
197 struct ctf_node
*node
= type_specifier
->u
.type_specifier
.node
;
199 if (!node
->u
.variant
.name
) {
200 fprintf(fd
, "[error] %s: unexpected empty variant name\n", __func__
);
203 g_string_append(str
, "variant ");
204 g_string_append(str
, node
->u
.variant
.name
);
209 struct ctf_node
*node
= type_specifier
->u
.type_specifier
.node
;
211 if (!node
->u
._enum
.enum_id
) {
212 fprintf(fd
, "[error] %s: unexpected empty enum ID\n", __func__
);
215 g_string_append(str
, "enum ");
216 g_string_append(str
, node
->u
._enum
.enum_id
);
219 case TYPESPEC_FLOATING_POINT
:
220 case TYPESPEC_INTEGER
:
221 case TYPESPEC_STRING
:
223 fprintf(fd
, "[error] %s: unknown specifier\n", __func__
);
230 int visit_type_specifier_list(FILE *fd
, struct ctf_node
*type_specifier_list
, GString
*str
)
232 struct ctf_node
*iter
;
233 int alias_item_nr
= 0;
236 cds_list_for_each_entry(iter
, &type_specifier_list
->u
.type_specifier_list
.head
, siblings
) {
237 if (alias_item_nr
!= 0)
238 g_string_append(str
, " ");
240 ret
= visit_type_specifier(fd
, iter
, str
);
248 GQuark
create_typealias_identifier(FILE *fd
, int depth
,
249 struct ctf_node
*type_specifier_list
,
250 struct ctf_node
*node_type_declarator
)
252 struct ctf_node
*iter
;
258 str
= g_string_new("");
259 ret
= visit_type_specifier_list(fd
, type_specifier_list
, str
);
261 g_string_free(str
, TRUE
);
264 cds_list_for_each_entry(iter
, &node_type_declarator
->u
.type_declarator
.pointers
, siblings
) {
265 g_string_append(str
, " *");
266 if (iter
->u
.pointer
.const_qualifier
)
267 g_string_append(str
, " const");
269 str_c
= g_string_free(str
, FALSE
);
270 alias_q
= g_quark_from_string(str_c
);
276 struct declaration
*ctf_type_declarator_visit(FILE *fd
, int depth
,
277 struct ctf_node
*type_specifier_list
,
279 struct ctf_node
*node_type_declarator
,
280 struct declaration_scope
*declaration_scope
,
281 struct declaration
*nested_declaration
,
282 struct ctf_trace
*trace
)
285 * Visit type declarator by first taking care of sequence/array
286 * (recursively). Then, when we get to the identifier, take care
290 if (node_type_declarator
) {
291 assert(node_type_declarator
->u
.type_declarator
.type
!= TYPEDEC_UNKNOWN
);
293 /* TODO: gcc bitfields not supported yet. */
294 if (node_type_declarator
->u
.type_declarator
.bitfield_len
!= NULL
) {
295 fprintf(fd
, "[error] %s: gcc bitfields are not supported yet.\n", __func__
);
300 if (!nested_declaration
) {
301 if (node_type_declarator
&& !cds_list_empty(&node_type_declarator
->u
.type_declarator
.pointers
)) {
305 * If we have a pointer declarator, it _has_ to be present in
306 * the typealiases (else fail).
308 alias_q
= create_typealias_identifier(fd
, depth
,
309 type_specifier_list
, node_type_declarator
);
310 nested_declaration
= lookup_declaration(alias_q
, declaration_scope
);
311 if (!nested_declaration
) {
312 fprintf(fd
, "[error] %s: cannot find typealias \"%s\".\n", __func__
, g_quark_to_string(alias_q
));
316 nested_declaration
= ctf_type_specifier_list_visit(fd
, depth
,
317 type_specifier_list
, declaration_scope
, trace
);
321 if (!node_type_declarator
)
322 return nested_declaration
;
324 if (node_type_declarator
->u
.type_declarator
.type
== TYPEDEC_ID
) {
325 if (node_type_declarator
->u
.type_declarator
.u
.id
)
326 *field_name
= g_quark_from_string(node_type_declarator
->u
.type_declarator
.u
.id
);
329 return nested_declaration
;
331 struct declaration
*declaration
;
332 struct ctf_node
*length
;
336 /* create array/sequence, pass nested_declaration as child. */
337 length
= node_type_declarator
->u
.type_declarator
.u
.nested
.length
;
339 fprintf(fd
, "[error] %s: expecting length type or value.\n", __func__
);
342 switch (length
->type
) {
343 case NODE_UNARY_EXPRESSION
:
345 struct declaration_array
*array_declaration
;
348 if (length
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
349 fprintf(fd
, "[error] %s: array: unexpected unary expression.\n", __func__
);
352 len
= length
->u
.unary_expression
.u
.unsigned_constant
;
353 array_declaration
= array_declaration_new(len
, nested_declaration
,
355 declaration
= &array_declaration
->p
;
358 case NODE_TYPE_SPECIFIER_LIST
:
360 struct declaration_sequence
*sequence_declaration
;
361 struct declaration_integer
*integer_declaration
;
363 declaration
= ctf_type_specifier_list_visit(fd
, depth
,
364 length
, declaration_scope
, trace
);
366 fprintf(fd
, "[error] %s: unable to find declaration type for sequence length\n", __func__
);
369 if (declaration
->id
!= CTF_TYPE_INTEGER
) {
370 fprintf(fd
, "[error] %s: length type for sequence is expected to be an integer (unsigned).\n", __func__
);
371 declaration_unref(declaration
);
374 integer_declaration
= container_of(declaration
, struct declaration_integer
, p
);
375 if (integer_declaration
->signedness
!= false) {
376 fprintf(fd
, "[error] %s: length type for sequence should always be an unsigned integer.\n", __func__
);
377 declaration_unref(declaration
);
381 sequence_declaration
= sequence_declaration_new(integer_declaration
,
382 nested_declaration
, declaration_scope
);
383 declaration
= &sequence_declaration
->p
;
390 /* Pass it as content of outer container */
391 declaration
= ctf_type_declarator_visit(fd
, depth
,
392 type_specifier_list
, field_name
,
393 node_type_declarator
->u
.type_declarator
.u
.nested
.type_declarator
,
394 declaration_scope
, declaration
, trace
);
400 int ctf_struct_type_declarators_visit(FILE *fd
, int depth
,
401 struct declaration_struct
*struct_declaration
,
402 struct ctf_node
*type_specifier_list
,
403 struct cds_list_head
*type_declarators
,
404 struct declaration_scope
*declaration_scope
,
405 struct ctf_trace
*trace
)
407 struct ctf_node
*iter
;
410 cds_list_for_each_entry(iter
, type_declarators
, siblings
) {
411 struct declaration
*field_declaration
;
413 field_declaration
= ctf_type_declarator_visit(fd
, depth
,
416 struct_declaration
->scope
,
418 if (!field_declaration
) {
419 fprintf(fd
, "[error] %s: unable to find struct field declaration type\n", __func__
);
422 struct_declaration_add_field(struct_declaration
,
423 g_quark_to_string(field_name
),
430 int ctf_variant_type_declarators_visit(FILE *fd
, int depth
,
431 struct declaration_untagged_variant
*untagged_variant_declaration
,
432 struct ctf_node
*type_specifier_list
,
433 struct cds_list_head
*type_declarators
,
434 struct declaration_scope
*declaration_scope
,
435 struct ctf_trace
*trace
)
437 struct ctf_node
*iter
;
440 cds_list_for_each_entry(iter
, type_declarators
, siblings
) {
441 struct declaration
*field_declaration
;
443 field_declaration
= ctf_type_declarator_visit(fd
, depth
,
446 untagged_variant_declaration
->scope
,
448 if (!field_declaration
) {
449 fprintf(fd
, "[error] %s: unable to find variant field declaration type\n", __func__
);
452 untagged_variant_declaration_add_field(untagged_variant_declaration
,
453 g_quark_to_string(field_name
),
460 int ctf_typedef_visit(FILE *fd
, int depth
, struct declaration_scope
*scope
,
461 struct ctf_node
*type_specifier_list
,
462 struct cds_list_head
*type_declarators
,
463 struct ctf_trace
*trace
)
465 struct ctf_node
*iter
;
468 cds_list_for_each_entry(iter
, type_declarators
, siblings
) {
469 struct declaration
*type_declaration
;
472 type_declaration
= ctf_type_declarator_visit(fd
, depth
,
476 if (!type_declaration
) {
477 fprintf(fd
, "[error] %s: problem creating type declaration\n", __func__
);
481 * Don't allow typedef and typealias of untagged
484 if (type_declaration
->id
== CTF_TYPE_UNTAGGED_VARIANT
) {
485 fprintf(fd
, "[error] %s: typedef of untagged variant is not permitted.\n", __func__
);
486 declaration_unref(type_declaration
);
489 ret
= register_declaration(identifier
, type_declaration
, scope
);
491 type_declaration
->declaration_free(type_declaration
);
499 int ctf_typealias_visit(FILE *fd
, int depth
, struct declaration_scope
*scope
,
500 struct ctf_node
*target
, struct ctf_node
*alias
,
501 struct ctf_trace
*trace
)
503 struct declaration
*type_declaration
;
504 struct ctf_node
*node
;
509 /* See ctf_visitor_type_declarator() in the semantic validator. */
512 * Create target type declaration.
515 if (cds_list_empty(&target
->u
.typealias_target
.type_declarators
))
518 node
= _cds_list_first_entry(&target
->u
.typealias_target
.type_declarators
,
519 struct ctf_node
, siblings
);
520 type_declaration
= ctf_type_declarator_visit(fd
, depth
,
521 target
->u
.typealias_target
.type_specifier_list
,
524 if (!type_declaration
) {
525 fprintf(fd
, "[error] %s: problem creating type declaration\n", __func__
);
530 * Don't allow typedef and typealias of untagged
533 if (type_declaration
->id
== CTF_TYPE_UNTAGGED_VARIANT
) {
534 fprintf(fd
, "[error] %s: typedef of untagged variant is not permitted.\n", __func__
);
535 declaration_unref(type_declaration
);
539 * The semantic validator does not check whether the target is
540 * abstract or not (if it has an identifier). Check it here.
543 fprintf(fd
, "[error] %s: expecting empty identifier\n", __func__
);
548 * Create alias identifier.
551 node
= _cds_list_first_entry(&alias
->u
.typealias_alias
.type_declarators
,
552 struct ctf_node
, siblings
);
553 alias_q
= create_typealias_identifier(fd
, depth
,
554 alias
->u
.typealias_alias
.type_specifier_list
, node
);
555 err
= register_declaration(alias_q
, type_declaration
, scope
);
561 type_declaration
->declaration_free(type_declaration
);
566 int ctf_struct_declaration_list_visit(FILE *fd
, int depth
,
567 struct ctf_node
*iter
, struct declaration_struct
*struct_declaration
,
568 struct ctf_trace
*trace
)
572 switch (iter
->type
) {
574 /* For each declarator, declare type and add type to struct declaration scope */
575 ret
= ctf_typedef_visit(fd
, depth
,
576 struct_declaration
->scope
,
577 iter
->u
._typedef
.type_specifier_list
,
578 &iter
->u
._typedef
.type_declarators
, trace
);
583 /* Declare type with declarator and add type to struct declaration scope */
584 ret
= ctf_typealias_visit(fd
, depth
,
585 struct_declaration
->scope
,
586 iter
->u
.typealias
.target
,
587 iter
->u
.typealias
.alias
, trace
);
591 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
592 /* Add field to structure declaration */
593 ret
= ctf_struct_type_declarators_visit(fd
, depth
,
595 iter
->u
.struct_or_variant_declaration
.type_specifier_list
,
596 &iter
->u
.struct_or_variant_declaration
.type_declarators
,
597 struct_declaration
->scope
, trace
);
602 fprintf(fd
, "[error] %s: unexpected node type %d\n", __func__
, (int) iter
->type
);
609 int ctf_variant_declaration_list_visit(FILE *fd
, int depth
,
610 struct ctf_node
*iter
,
611 struct declaration_untagged_variant
*untagged_variant_declaration
,
612 struct ctf_trace
*trace
)
616 switch (iter
->type
) {
618 /* For each declarator, declare type and add type to variant declaration scope */
619 ret
= ctf_typedef_visit(fd
, depth
,
620 untagged_variant_declaration
->scope
,
621 iter
->u
._typedef
.type_specifier_list
,
622 &iter
->u
._typedef
.type_declarators
, trace
);
627 /* Declare type with declarator and add type to variant declaration scope */
628 ret
= ctf_typealias_visit(fd
, depth
,
629 untagged_variant_declaration
->scope
,
630 iter
->u
.typealias
.target
,
631 iter
->u
.typealias
.alias
, trace
);
635 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
636 /* Add field to structure declaration */
637 ret
= ctf_variant_type_declarators_visit(fd
, depth
,
638 untagged_variant_declaration
,
639 iter
->u
.struct_or_variant_declaration
.type_specifier_list
,
640 &iter
->u
.struct_or_variant_declaration
.type_declarators
,
641 untagged_variant_declaration
->scope
, trace
);
646 fprintf(fd
, "[error] %s: unexpected node type %d\n", __func__
, (int) iter
->type
);
653 struct declaration
*ctf_declaration_struct_visit(FILE *fd
,
654 int depth
, const char *name
, struct cds_list_head
*declaration_list
,
655 int has_body
, struct declaration_scope
*declaration_scope
,
656 struct ctf_trace
*trace
)
658 struct declaration_struct
*struct_declaration
;
659 struct ctf_node
*iter
;
663 * For named struct (without body), lookup in
664 * declaration scope. Don't take reference on struct
665 * declaration: ref is only taken upon definition.
670 lookup_struct_declaration(g_quark_from_string(name
),
672 return &struct_declaration
->p
;
674 /* For unnamed struct, create type */
675 /* For named struct (with body), create type and add to declaration scope */
677 if (lookup_struct_declaration(g_quark_from_string(name
),
678 declaration_scope
)) {
680 fprintf(fd
, "[error] %s: struct %s already declared in scope\n", __func__
, name
);
684 struct_declaration
= struct_declaration_new(declaration_scope
);
685 cds_list_for_each_entry(iter
, declaration_list
, siblings
) {
686 ret
= ctf_struct_declaration_list_visit(fd
, depth
+ 1, iter
,
687 struct_declaration
, trace
);
692 ret
= register_struct_declaration(g_quark_from_string(name
),
697 return &struct_declaration
->p
;
700 struct_declaration
->p
.declaration_free(&struct_declaration
->p
);
705 struct declaration
*ctf_declaration_variant_visit(FILE *fd
,
706 int depth
, const char *name
, const char *choice
,
707 struct cds_list_head
*declaration_list
,
708 int has_body
, struct declaration_scope
*declaration_scope
,
709 struct ctf_trace
*trace
)
711 struct declaration_untagged_variant
*untagged_variant_declaration
;
712 struct declaration_variant
*variant_declaration
;
713 struct ctf_node
*iter
;
717 * For named variant (without body), lookup in
718 * declaration scope. Don't take reference on variant
719 * declaration: ref is only taken upon definition.
723 untagged_variant_declaration
=
724 lookup_variant_declaration(g_quark_from_string(name
),
727 /* For unnamed variant, create type */
728 /* For named variant (with body), create type and add to declaration scope */
730 if (lookup_variant_declaration(g_quark_from_string(name
),
731 declaration_scope
)) {
733 fprintf(fd
, "[error] %s: variant %s already declared in scope\n", __func__
, name
);
737 untagged_variant_declaration
= untagged_variant_declaration_new(declaration_scope
);
738 cds_list_for_each_entry(iter
, declaration_list
, siblings
) {
739 ret
= ctf_variant_declaration_list_visit(fd
, depth
+ 1, iter
,
740 untagged_variant_declaration
, trace
);
745 ret
= register_variant_declaration(g_quark_from_string(name
),
746 untagged_variant_declaration
,
752 * if tagged, create tagged variant and return. else return
756 return &untagged_variant_declaration
->p
;
758 variant_declaration
= variant_declaration_new(untagged_variant_declaration
, choice
);
759 if (!variant_declaration
)
761 declaration_unref(&untagged_variant_declaration
->p
);
762 return &variant_declaration
->p
;
765 untagged_variant_declaration
->p
.declaration_free(&untagged_variant_declaration
->p
);
770 int ctf_enumerator_list_visit(FILE *fd
, int depth
,
771 struct ctf_node
*enumerator
,
772 struct declaration_enum
*enum_declaration
)
775 struct ctf_node
*iter
;
777 q
= g_quark_from_string(enumerator
->u
.enumerator
.id
);
778 if (enum_declaration
->integer_declaration
->signedness
) {
782 cds_list_for_each_entry(iter
, &enumerator
->u
.enumerator
.values
, siblings
) {
785 assert(iter
->type
== NODE_UNARY_EXPRESSION
);
791 switch (iter
->u
.unary_expression
.type
) {
792 case UNARY_SIGNED_CONSTANT
:
793 *target
= iter
->u
.unary_expression
.u
.signed_constant
;
795 case UNARY_UNSIGNED_CONSTANT
:
796 *target
= iter
->u
.unary_expression
.u
.unsigned_constant
;
799 fprintf(fd
, "[error] %s: invalid enumerator\n", __func__
);
803 fprintf(fd
, "[error] %s: invalid enumerator\n", __func__
);
810 enum_signed_insert(enum_declaration
, start
, end
, q
);
815 cds_list_for_each_entry(iter
, &enumerator
->u
.enumerator
.values
, siblings
) {
818 assert(iter
->type
== NODE_UNARY_EXPRESSION
);
824 switch (iter
->u
.unary_expression
.type
) {
825 case UNARY_UNSIGNED_CONSTANT
:
826 *target
= iter
->u
.unary_expression
.u
.unsigned_constant
;
828 case UNARY_SIGNED_CONSTANT
:
830 * We don't accept signed constants for enums with unsigned
833 fprintf(fd
, "[error] %s: invalid enumerator (signed constant encountered, but enum container type is unsigned)\n", __func__
);
836 fprintf(fd
, "[error] %s: invalid enumerator\n", __func__
);
840 fprintf(fd
, "[error] %s: invalid enumerator\n", __func__
);
847 enum_unsigned_insert(enum_declaration
, start
, end
, q
);
853 struct declaration
*ctf_declaration_enum_visit(FILE *fd
, int depth
,
855 struct ctf_node
*container_type
,
856 struct cds_list_head
*enumerator_list
,
858 struct declaration_scope
*declaration_scope
,
859 struct ctf_trace
*trace
)
861 struct declaration
*declaration
;
862 struct declaration_enum
*enum_declaration
;
863 struct declaration_integer
*integer_declaration
;
864 struct ctf_node
*iter
;
869 * For named enum (without body), lookup in
870 * declaration scope. Don't take reference on enum
871 * declaration: ref is only taken upon definition.
876 lookup_enum_declaration(g_quark_from_string(name
),
878 return &enum_declaration
->p
;
880 /* For unnamed enum, create type */
881 /* For named enum (with body), create type and add to declaration scope */
883 if (lookup_enum_declaration(g_quark_from_string(name
),
884 declaration_scope
)) {
886 fprintf(fd
, "[error] %s: enum %s already declared in scope\n", __func__
, name
);
890 if (!container_type
) {
891 declaration
= lookup_declaration(g_quark_from_static_string("int"),
894 fprintf(fd
, "[error] %s: \"int\" type declaration missing for enumeration\n", __func__
);
898 declaration
= ctf_type_declarator_visit(fd
, depth
,
905 fprintf(fd
, "[error] %s: unable to create container type for enumeration\n", __func__
);
908 if (declaration
->id
!= CTF_TYPE_INTEGER
) {
909 fprintf(fd
, "[error] %s: container type for enumeration is not integer\n", __func__
);
912 integer_declaration
= container_of(declaration
, struct declaration_integer
, p
);
913 enum_declaration
= enum_declaration_new(integer_declaration
);
914 declaration_unref(&integer_declaration
->p
); /* leave ref to enum */
915 cds_list_for_each_entry(iter
, enumerator_list
, siblings
) {
916 ret
= ctf_enumerator_list_visit(fd
, depth
+ 1, iter
, enum_declaration
);
921 ret
= register_enum_declaration(g_quark_from_string(name
),
926 return &enum_declaration
->p
;
929 enum_declaration
->p
.declaration_free(&enum_declaration
->p
);
934 struct declaration
*ctf_declaration_type_specifier_visit(FILE *fd
, int depth
,
935 struct ctf_node
*type_specifier_list
,
936 struct declaration_scope
*declaration_scope
)
939 struct declaration
*declaration
;
944 str
= g_string_new("");
945 ret
= visit_type_specifier_list(fd
, type_specifier_list
, str
);
948 str_c
= g_string_free(str
, FALSE
);
949 id_q
= g_quark_from_string(str_c
);
951 declaration
= lookup_declaration(id_q
, declaration_scope
);
956 * Returns 0/1 boolean, or < 0 on error.
959 int get_boolean(FILE *fd
, int depth
, struct ctf_node
*unary_expression
)
961 if (unary_expression
->type
!= NODE_UNARY_EXPRESSION
) {
962 fprintf(fd
, "[error] %s: expecting unary expression\n",
966 switch (unary_expression
->u
.unary_expression
.type
) {
967 case UNARY_UNSIGNED_CONSTANT
:
968 if (unary_expression
->u
.unary_expression
.u
.unsigned_constant
== 0)
972 case UNARY_SIGNED_CONSTANT
:
973 if (unary_expression
->u
.unary_expression
.u
.signed_constant
== 0)
978 if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "true"))
980 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "TRUE"))
982 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "false"))
984 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "FALSE"))
987 fprintf(fd
, "[error] %s: unexpected string \"%s\"\n",
988 __func__
, unary_expression
->u
.unary_expression
.u
.string
);
993 fprintf(fd
, "[error] %s: unexpected unary expression type\n",
1001 int get_trace_byte_order(FILE *fd
, int depth
, struct ctf_node
*unary_expression
)
1005 if (unary_expression
->u
.unary_expression
.type
!= UNARY_STRING
) {
1006 fprintf(fd
, "[error] %s: byte_order: expecting string\n",
1010 if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "be"))
1011 byte_order
= BIG_ENDIAN
;
1012 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "le"))
1013 byte_order
= LITTLE_ENDIAN
;
1015 fprintf(fd
, "[error] %s: unexpected string \"%s\". Should be \"native\", \"network\", \"be\" or \"le\".\n",
1016 __func__
, unary_expression
->u
.unary_expression
.u
.string
);
1023 int get_byte_order(FILE *fd
, int depth
, struct ctf_node
*unary_expression
,
1024 struct ctf_trace
*trace
)
1028 if (unary_expression
->u
.unary_expression
.type
!= UNARY_STRING
) {
1029 fprintf(fd
, "[error] %s: byte_order: expecting string\n",
1033 if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "native"))
1034 byte_order
= trace
->byte_order
;
1035 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "network"))
1036 byte_order
= BIG_ENDIAN
;
1037 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "be"))
1038 byte_order
= BIG_ENDIAN
;
1039 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "le"))
1040 byte_order
= LITTLE_ENDIAN
;
1042 fprintf(fd
, "[error] %s: unexpected string \"%s\". Should be \"native\", \"network\", \"be\" or \"le\".\n",
1043 __func__
, unary_expression
->u
.unary_expression
.u
.string
);
1050 struct declaration
*ctf_declaration_integer_visit(FILE *fd
, int depth
,
1051 struct cds_list_head
*expressions
,
1052 struct ctf_trace
*trace
)
1054 struct ctf_node
*expression
;
1055 uint64_t alignment
, size
;
1056 int byte_order
= trace
->byte_order
;
1058 int has_alignment
= 0, has_size
= 0;
1059 struct declaration_integer
*integer_declaration
;
1061 cds_list_for_each_entry(expression
, expressions
, siblings
) {
1062 struct ctf_node
*left
, *right
;
1064 left
= _cds_list_first_entry(&expression
->u
.ctf_expression
.left
, struct ctf_node
, siblings
);
1065 right
= _cds_list_first_entry(&expression
->u
.ctf_expression
.right
, struct ctf_node
, siblings
);
1066 assert(left
->u
.unary_expression
.type
== UNARY_STRING
);
1067 if (!strcmp(left
->u
.unary_expression
.u
.string
, "signed")) {
1068 signedness
= get_boolean(fd
, depth
, right
);
1071 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "byte_order")) {
1072 byte_order
= get_byte_order(fd
, depth
, right
, trace
);
1075 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "size")) {
1076 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
1077 fprintf(fd
, "[error] %s: size: expecting unsigned constant\n",
1081 size
= right
->u
.unary_expression
.u
.unsigned_constant
;
1083 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "align")) {
1084 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
1085 fprintf(fd
, "[error] %s: align: expecting unsigned constant\n",
1089 alignment
= right
->u
.unary_expression
.u
.unsigned_constant
;
1092 fprintf(fd
, "[error] %s: unknown attribute name %s\n",
1093 __func__
, left
->u
.unary_expression
.u
.string
);
1098 fprintf(fd
, "[error] %s: missing size attribute\n", __func__
);
1101 if (!has_alignment
) {
1102 if (size
% CHAR_BIT
) {
1103 /* bit-packed alignment */
1106 /* byte-packed alignment */
1107 alignment
= CHAR_BIT
;
1110 integer_declaration
= integer_declaration_new(size
,
1111 byte_order
, signedness
, alignment
);
1112 return &integer_declaration
->p
;
1116 struct declaration
*ctf_declaration_floating_point_visit(FILE *fd
, int depth
,
1117 struct cds_list_head
*expressions
,
1118 struct ctf_trace
*trace
)
1120 struct ctf_node
*expression
;
1121 uint64_t alignment
, exp_dig
, mant_dig
, byte_order
= trace
->byte_order
;
1122 int has_alignment
= 0, has_exp_dig
= 0, has_mant_dig
= 0;
1123 struct declaration_float
*float_declaration
;
1125 cds_list_for_each_entry(expression
, expressions
, siblings
) {
1126 struct ctf_node
*left
, *right
;
1128 left
= _cds_list_first_entry(&expression
->u
.ctf_expression
.left
, struct ctf_node
, siblings
);
1129 right
= _cds_list_first_entry(&expression
->u
.ctf_expression
.right
, struct ctf_node
, siblings
);
1130 assert(left
->u
.unary_expression
.type
== UNARY_STRING
);
1131 if (!strcmp(left
->u
.unary_expression
.u
.string
, "byte_order")) {
1132 byte_order
= get_byte_order(fd
, depth
, right
, trace
);
1135 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "exp_dig")) {
1136 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
1137 fprintf(fd
, "[error] %s: exp_dig: expecting unsigned constant\n",
1141 exp_dig
= right
->u
.unary_expression
.u
.unsigned_constant
;
1143 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "mant_dig")) {
1144 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
1145 fprintf(fd
, "[error] %s: mant_dig: expecting unsigned constant\n",
1149 mant_dig
= right
->u
.unary_expression
.u
.unsigned_constant
;
1151 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "align")) {
1152 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
1153 fprintf(fd
, "[error] %s: align: expecting unsigned constant\n",
1157 alignment
= right
->u
.unary_expression
.u
.unsigned_constant
;
1160 fprintf(fd
, "[error] %s: unknown attribute name %s\n",
1161 __func__
, left
->u
.unary_expression
.u
.string
);
1165 if (!has_mant_dig
) {
1166 fprintf(fd
, "[error] %s: missing mant_dig attribute\n", __func__
);
1170 fprintf(fd
, "[error] %s: missing exp_dig attribute\n", __func__
);
1173 if (!has_alignment
) {
1174 if ((mant_dig
+ exp_dig
) % CHAR_BIT
) {
1175 /* bit-packed alignment */
1178 /* byte-packed alignment */
1179 alignment
= CHAR_BIT
;
1182 float_declaration
= float_declaration_new(mant_dig
, exp_dig
,
1183 byte_order
, alignment
);
1184 return &float_declaration
->p
;
1188 struct declaration
*ctf_declaration_string_visit(FILE *fd
, int depth
,
1189 struct cds_list_head
*expressions
,
1190 struct ctf_trace
*trace
)
1192 struct ctf_node
*expression
;
1193 const char *encoding_c
= NULL
;
1194 enum ctf_string_encoding encoding
= CTF_STRING_UTF8
;
1195 struct declaration_string
*string_declaration
;
1197 cds_list_for_each_entry(expression
, expressions
, siblings
) {
1198 struct ctf_node
*left
, *right
;
1200 left
= _cds_list_first_entry(&expression
->u
.ctf_expression
.left
, struct ctf_node
, siblings
);
1201 right
= _cds_list_first_entry(&expression
->u
.ctf_expression
.right
, struct ctf_node
, siblings
);
1202 assert(left
->u
.unary_expression
.type
== UNARY_STRING
);
1203 if (!strcmp(left
->u
.unary_expression
.u
.string
, "encoding")) {
1204 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
1205 fprintf(fd
, "[error] %s: encoding: expecting string\n",
1209 encoding_c
= right
->u
.unary_expression
.u
.string
;
1211 fprintf(fd
, "[error] %s: unknown attribute name %s\n",
1212 __func__
, left
->u
.unary_expression
.u
.string
);
1216 if (encoding_c
&& !strcmp(encoding_c
, "ASCII"))
1217 encoding
= CTF_STRING_ASCII
;
1218 string_declaration
= string_declaration_new(encoding
);
1219 return &string_declaration
->p
;
1224 struct declaration
*ctf_type_specifier_list_visit(FILE *fd
,
1225 int depth
, struct ctf_node
*type_specifier_list
,
1226 struct declaration_scope
*declaration_scope
,
1227 struct ctf_trace
*trace
)
1229 struct ctf_node
*first
;
1230 struct ctf_node
*node
;
1232 assert(type_specifier_list
->type
== NODE_TYPE_SPECIFIER_LIST
);
1234 first
= _cds_list_first_entry(&type_specifier_list
->u
.type_specifier_list
.head
, struct ctf_node
, siblings
);
1236 assert(first
->type
== NODE_TYPE_SPECIFIER
);
1238 node
= first
->u
.type_specifier
.node
;
1240 switch (first
->u
.type_specifier
.type
) {
1241 case TYPESPEC_FLOATING_POINT
:
1242 return ctf_declaration_floating_point_visit(fd
, depth
,
1243 &node
->u
.floating_point
.expressions
, trace
);
1244 case TYPESPEC_INTEGER
:
1245 return ctf_declaration_integer_visit(fd
, depth
,
1246 &node
->u
.integer
.expressions
, trace
);
1247 case TYPESPEC_STRING
:
1248 return ctf_declaration_string_visit(fd
, depth
,
1249 &node
->u
.string
.expressions
, trace
);
1250 case TYPESPEC_STRUCT
:
1251 return ctf_declaration_struct_visit(fd
, depth
,
1252 node
->u
._struct
.name
,
1253 &node
->u
._struct
.declaration_list
,
1254 node
->u
._struct
.has_body
,
1257 case TYPESPEC_VARIANT
:
1258 return ctf_declaration_variant_visit(fd
, depth
,
1259 node
->u
.variant
.name
,
1260 node
->u
.variant
.choice
,
1261 &node
->u
.variant
.declaration_list
,
1262 node
->u
.variant
.has_body
,
1266 return ctf_declaration_enum_visit(fd
, depth
,
1267 node
->u
._enum
.enum_id
,
1268 node
->u
._enum
.container_type
,
1269 &node
->u
._enum
.enumerator_list
,
1270 node
->u
._enum
.has_body
,
1276 case TYPESPEC_SHORT
:
1279 case TYPESPEC_FLOAT
:
1280 case TYPESPEC_DOUBLE
:
1281 case TYPESPEC_SIGNED
:
1282 case TYPESPEC_UNSIGNED
:
1284 case TYPESPEC_COMPLEX
:
1285 case TYPESPEC_IMAGINARY
:
1286 case TYPESPEC_CONST
:
1287 case TYPESPEC_ID_TYPE
:
1288 return ctf_declaration_type_specifier_visit(fd
, depth
,
1289 type_specifier_list
, declaration_scope
);
1291 fprintf(fd
, "[error] %s: unexpected node type %d\n", __func__
, (int) first
->u
.type_specifier
.type
);
1297 int ctf_event_declaration_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_event
*event
, struct ctf_trace
*trace
)
1301 switch (node
->type
) {
1303 ret
= ctf_typedef_visit(fd
, depth
+ 1,
1304 event
->declaration_scope
,
1305 node
->u
._typedef
.type_specifier_list
,
1306 &node
->u
._typedef
.type_declarators
,
1311 case NODE_TYPEALIAS
:
1312 ret
= ctf_typealias_visit(fd
, depth
+ 1,
1313 event
->declaration_scope
,
1314 node
->u
.typealias
.target
, node
->u
.typealias
.alias
,
1319 case NODE_CTF_EXPRESSION
:
1323 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
1324 if (!strcmp(left
, "name")) {
1327 if (CTF_EVENT_FIELD_IS_SET(event
, name
)) {
1328 fprintf(fd
, "[error] %s: name already declared in event declaration\n", __func__
);
1332 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
1334 fprintf(fd
, "[error] %s: unexpected unary expression for event name\n", __func__
);
1338 event
->name
= g_quark_from_string(right
);
1340 CTF_EVENT_SET_FIELD(event
, name
);
1341 } else if (!strcmp(left
, "id")) {
1342 if (CTF_EVENT_FIELD_IS_SET(event
, id
)) {
1343 fprintf(fd
, "[error] %s: id already declared in event declaration\n", __func__
);
1347 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &event
->id
);
1349 fprintf(fd
, "[error] %s: unexpected unary expression for event id\n", __func__
);
1353 CTF_EVENT_SET_FIELD(event
, id
);
1354 } else if (!strcmp(left
, "stream_id")) {
1355 if (CTF_EVENT_FIELD_IS_SET(event
, stream_id
)) {
1356 fprintf(fd
, "[error] %s: stream_id already declared in event declaration\n", __func__
);
1360 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &event
->stream_id
);
1362 fprintf(fd
, "[error] %s: unexpected unary expression for event stream_id\n", __func__
);
1366 event
->stream
= trace_stream_lookup(trace
, event
->stream_id
);
1367 if (!event
->stream
) {
1368 fprintf(fd
, "[error] %s: stream id %" PRIu64
" cannot be found\n", __func__
, event
->stream_id
);
1372 CTF_EVENT_SET_FIELD(event
, stream_id
);
1373 } else if (!strcmp(left
, "context")) {
1374 struct declaration
*declaration
;
1376 if (event
->context_decl
) {
1377 fprintf(fd
, "[error] %s: context already declared in event declaration\n", __func__
);
1381 declaration
= ctf_type_specifier_list_visit(fd
, depth
,
1382 _cds_list_first_entry(&node
->u
.ctf_expression
.right
,
1383 struct ctf_node
, siblings
),
1384 event
->declaration_scope
, trace
);
1389 if (declaration
->id
!= CTF_TYPE_STRUCT
) {
1393 event
->context_decl
= container_of(declaration
, struct declaration_struct
, p
);
1394 } else if (!strcmp(left
, "fields")) {
1395 struct declaration
*declaration
;
1397 if (event
->fields_decl
) {
1398 fprintf(fd
, "[error] %s: fields already declared in event declaration\n", __func__
);
1402 declaration
= ctf_type_specifier_list_visit(fd
, depth
,
1403 _cds_list_first_entry(&node
->u
.ctf_expression
.right
,
1404 struct ctf_node
, siblings
),
1405 event
->declaration_scope
, trace
);
1410 if (declaration
->id
!= CTF_TYPE_STRUCT
) {
1414 event
->fields_decl
= container_of(declaration
, struct declaration_struct
, p
);
1422 /* TODO: declaration specifier should be added. */
1429 int ctf_event_visit(FILE *fd
, int depth
, struct ctf_node
*node
,
1430 struct declaration_scope
*parent_declaration_scope
, struct ctf_trace
*trace
)
1433 struct ctf_node
*iter
;
1434 struct ctf_event
*event
;
1435 struct definition_scope
*parent_def_scope
;
1437 event
= g_new0(struct ctf_event
, 1);
1438 event
->declaration_scope
= new_declaration_scope(parent_declaration_scope
);
1439 cds_list_for_each_entry(iter
, &node
->u
.event
.declaration_list
, siblings
) {
1440 ret
= ctf_event_declaration_visit(fd
, depth
+ 1, iter
, event
, trace
);
1444 if (!CTF_EVENT_FIELD_IS_SET(event
, name
)) {
1446 fprintf(fd
, "[error] %s: missing name field in event declaration\n", __func__
);
1449 if (!CTF_EVENT_FIELD_IS_SET(event
, stream_id
)) {
1450 /* Allow missing stream_id if there is only a single stream */
1451 switch (trace
->streams
->len
) {
1452 case 0: /* Create stream if there was none. */
1453 ret
= ctf_stream_visit(fd
, depth
, NULL
, trace
->root_declaration_scope
, trace
);
1458 event
->stream_id
= 0;
1459 event
->stream
= trace_stream_lookup(trace
, event
->stream_id
);
1463 fprintf(fd
, "[error] %s: missing stream_id field in event declaration\n", __func__
);
1467 /* Allow only one event without id per stream */
1468 if (!CTF_EVENT_FIELD_IS_SET(event
, id
)
1469 && event
->stream
->events_by_id
->len
!= 0) {
1471 fprintf(fd
, "[error] %s: missing id field in event declaration\n", __func__
);
1474 if (event
->stream
->events_by_id
->len
<= event
->id
)
1475 g_ptr_array_set_size(event
->stream
->events_by_id
, event
->id
+ 1);
1476 g_ptr_array_index(event
->stream
->events_by_id
, event
->id
) = event
;
1477 g_hash_table_insert(event
->stream
->event_quark_to_id
,
1478 (gpointer
)(unsigned long) event
->name
,
1480 parent_def_scope
= event
->stream
->definition_scope
;
1481 if (event
->context_decl
) {
1484 event
->context_decl
->p
.definition_new(&event
->context_decl
->p
,
1485 parent_def_scope
, 0, 0),
1486 struct definition_struct
, p
);
1487 set_dynamic_definition_scope(&event
->context
->p
,
1488 event
->context
->scope
,
1490 parent_def_scope
= event
->context
->scope
;
1491 declaration_unref(&event
->context_decl
->p
);
1493 if (event
->fields_decl
) {
1496 event
->fields_decl
->p
.definition_new(&event
->fields_decl
->p
,
1497 parent_def_scope
, 0, 0),
1498 struct definition_struct
, p
);
1499 set_dynamic_definition_scope(&event
->fields
->p
,
1500 event
->fields
->scope
,
1502 parent_def_scope
= event
->fields
->scope
;
1503 declaration_unref(&event
->fields_decl
->p
);
1508 declaration_unref(&event
->fields_decl
->p
);
1509 declaration_unref(&event
->context_decl
->p
);
1510 free_declaration_scope(event
->declaration_scope
);
1517 int ctf_stream_declaration_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_stream
*stream
, struct ctf_trace
*trace
)
1521 switch (node
->type
) {
1523 ret
= ctf_typedef_visit(fd
, depth
+ 1,
1524 stream
->declaration_scope
,
1525 node
->u
._typedef
.type_specifier_list
,
1526 &node
->u
._typedef
.type_declarators
,
1531 case NODE_TYPEALIAS
:
1532 ret
= ctf_typealias_visit(fd
, depth
+ 1,
1533 stream
->declaration_scope
,
1534 node
->u
.typealias
.target
, node
->u
.typealias
.alias
,
1539 case NODE_CTF_EXPRESSION
:
1543 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
1544 if (!strcmp(left
, "id")) {
1545 if (CTF_STREAM_FIELD_IS_SET(stream
, stream_id
)) {
1546 fprintf(fd
, "[error] %s: id already declared in stream declaration\n", __func__
);
1550 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &stream
->stream_id
);
1552 fprintf(fd
, "[error] %s: unexpected unary expression for stream id\n", __func__
);
1556 CTF_STREAM_SET_FIELD(stream
, stream_id
);
1557 } else if (!strcmp(left
, "event.header")) {
1558 struct declaration
*declaration
;
1560 if (stream
->event_header_decl
) {
1561 fprintf(fd
, "[error] %s: event.header already declared in stream declaration\n", __func__
);
1565 declaration
= ctf_type_specifier_list_visit(fd
, depth
,
1566 _cds_list_first_entry(&node
->u
.ctf_expression
.right
,
1567 struct ctf_node
, siblings
),
1568 stream
->declaration_scope
, trace
);
1573 if (declaration
->id
!= CTF_TYPE_STRUCT
) {
1577 stream
->event_header_decl
= container_of(declaration
, struct declaration_struct
, p
);
1578 } else if (!strcmp(left
, "event.context")) {
1579 struct declaration
*declaration
;
1581 if (stream
->event_context_decl
) {
1582 fprintf(fd
, "[error] %s: event.context already declared in stream declaration\n", __func__
);
1586 declaration
= ctf_type_specifier_list_visit(fd
, depth
,
1587 _cds_list_first_entry(&node
->u
.ctf_expression
.right
,
1588 struct ctf_node
, siblings
),
1589 stream
->declaration_scope
, trace
);
1594 if (declaration
->id
!= CTF_TYPE_STRUCT
) {
1598 stream
->event_context_decl
= container_of(declaration
, struct declaration_struct
, p
);
1599 } else if (!strcmp(left
, "packet.context")) {
1600 struct declaration
*declaration
;
1602 if (stream
->packet_context_decl
) {
1603 fprintf(fd
, "[error] %s: packet.context already declared in stream declaration\n", __func__
);
1607 declaration
= ctf_type_specifier_list_visit(fd
, depth
,
1608 _cds_list_first_entry(&node
->u
.ctf_expression
.right
,
1609 struct ctf_node
, siblings
),
1610 stream
->declaration_scope
, trace
);
1615 if (declaration
->id
!= CTF_TYPE_STRUCT
) {
1619 stream
->packet_context_decl
= container_of(declaration
, struct declaration_struct
, p
);
1627 /* TODO: declaration specifier should be added. */
1634 int ctf_stream_visit(FILE *fd
, int depth
, struct ctf_node
*node
,
1635 struct declaration_scope
*parent_declaration_scope
, struct ctf_trace
*trace
)
1638 struct ctf_node
*iter
;
1639 struct ctf_stream
*stream
;
1640 struct definition_scope
*parent_def_scope
;
1642 stream
= g_new0(struct ctf_stream
, 1);
1643 stream
->declaration_scope
= new_declaration_scope(parent_declaration_scope
);
1644 stream
->events_by_id
= g_ptr_array_new();
1645 stream
->event_quark_to_id
= g_hash_table_new(g_direct_hash
, g_direct_equal
);
1646 stream
->files
= g_ptr_array_new();
1648 cds_list_for_each_entry(iter
, &node
->u
.stream
.declaration_list
, siblings
) {
1649 ret
= ctf_stream_declaration_visit(fd
, depth
+ 1, iter
, stream
, trace
);
1654 if (CTF_STREAM_FIELD_IS_SET(stream
, stream_id
)) {
1655 /* check that packet header has stream_id field. */
1656 if (!trace
->packet_header_decl
1657 || struct_declaration_lookup_field_index(trace
->packet_header_decl
, g_quark_from_static_string("stream_id")) < 0) {
1659 fprintf(fd
, "[error] %s: missing stream_id field in packet header declaration, but stream_id attribute is declared for stream.\n", __func__
);
1663 /* Allow only one id-less stream */
1664 if (trace
->streams
->len
!= 0) {
1666 fprintf(fd
, "[error] %s: missing id field in stream declaration\n", __func__
);
1669 stream
->stream_id
= 0;
1671 if (trace
->streams
->len
<= stream
->stream_id
)
1672 g_ptr_array_set_size(trace
->streams
, stream
->stream_id
+ 1);
1673 g_ptr_array_index(trace
->streams
, stream
->stream_id
) = stream
;
1675 parent_def_scope
= trace
->definition_scope
;
1676 if (stream
->packet_context_decl
) {
1677 stream
->packet_context
=
1679 stream
->packet_context_decl
->p
.definition_new(&stream
->packet_context_decl
->p
,
1680 parent_def_scope
, 0, 0),
1681 struct definition_struct
, p
);
1682 set_dynamic_definition_scope(&stream
->packet_context
->p
,
1683 stream
->packet_context
->scope
,
1684 "stream.packet.context");
1685 parent_def_scope
= stream
->packet_context
->scope
;
1686 declaration_unref(&stream
->packet_context_decl
->p
);
1688 if (stream
->event_header_decl
) {
1689 stream
->event_header
=
1691 stream
->event_header_decl
->p
.definition_new(&stream
->event_header_decl
->p
,
1692 parent_def_scope
, 0, 0),
1693 struct definition_struct
, p
);
1694 set_dynamic_definition_scope(&stream
->event_header
->p
,
1695 stream
->event_header
->scope
,
1696 "stream.event.header");
1697 parent_def_scope
= stream
->event_header
->scope
;
1698 declaration_unref(&stream
->event_header_decl
->p
);
1700 if (stream
->event_context_decl
) {
1701 stream
->event_context
=
1703 stream
->event_context_decl
->p
.definition_new(&stream
->event_context_decl
->p
,
1704 parent_def_scope
, 0, 0),
1705 struct definition_struct
, p
);
1706 set_dynamic_definition_scope(&stream
->event_context
->p
,
1707 stream
->event_context
->scope
,
1708 "stream.event.context");
1709 parent_def_scope
= stream
->event_context
->scope
;
1710 declaration_unref(&stream
->event_context_decl
->p
);
1712 stream
->definition_scope
= parent_def_scope
;
1717 declaration_unref(&stream
->event_header_decl
->p
);
1718 declaration_unref(&stream
->event_context_decl
->p
);
1719 declaration_unref(&stream
->packet_context_decl
->p
);
1720 g_ptr_array_free(stream
->files
, TRUE
);
1721 g_ptr_array_free(stream
->events_by_id
, TRUE
);
1722 g_hash_table_destroy(stream
->event_quark_to_id
);
1723 free_declaration_scope(stream
->declaration_scope
);
1729 int ctf_trace_declaration_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_trace
*trace
)
1733 switch (node
->type
) {
1735 ret
= ctf_typedef_visit(fd
, depth
+ 1,
1736 trace
->declaration_scope
,
1737 node
->u
._typedef
.type_specifier_list
,
1738 &node
->u
._typedef
.type_declarators
,
1743 case NODE_TYPEALIAS
:
1744 ret
= ctf_typealias_visit(fd
, depth
+ 1,
1745 trace
->declaration_scope
,
1746 node
->u
.typealias
.target
, node
->u
.typealias
.alias
,
1751 case NODE_CTF_EXPRESSION
:
1755 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
1756 if (!strcmp(left
, "major")) {
1757 if (CTF_TRACE_FIELD_IS_SET(trace
, major
)) {
1758 fprintf(fd
, "[error] %s: major already declared in trace declaration\n", __func__
);
1762 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &trace
->major
);
1764 fprintf(fd
, "[error] %s: unexpected unary expression for trace major number\n", __func__
);
1768 CTF_TRACE_SET_FIELD(trace
, major
);
1769 } else if (!strcmp(left
, "minor")) {
1770 if (CTF_TRACE_FIELD_IS_SET(trace
, minor
)) {
1771 fprintf(fd
, "[error] %s: minor already declared in trace declaration\n", __func__
);
1775 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &trace
->minor
);
1777 fprintf(fd
, "[error] %s: unexpected unary expression for trace minor number\n", __func__
);
1781 CTF_TRACE_SET_FIELD(trace
, minor
);
1782 } else if (!strcmp(left
, "uuid")) {
1783 if (CTF_TRACE_FIELD_IS_SET(trace
, uuid
)) {
1784 fprintf(fd
, "[error] %s: uuid already declared in trace declaration\n", __func__
);
1788 ret
= get_unary_uuid(&node
->u
.ctf_expression
.right
, &trace
->uuid
);
1790 fprintf(fd
, "[error] %s: unexpected unary expression for trace uuid\n", __func__
);
1794 CTF_TRACE_SET_FIELD(trace
, uuid
);
1795 } else if (!strcmp(left
, "byte_order")) {
1796 struct ctf_node
*right
;
1799 if (CTF_TRACE_FIELD_IS_SET(trace
, byte_order
)) {
1800 fprintf(fd
, "[error] %s: endianness already declared in trace declaration\n", __func__
);
1804 right
= _cds_list_first_entry(&node
->u
.ctf_expression
.right
, struct ctf_node
, siblings
);
1805 byte_order
= get_trace_byte_order(fd
, depth
, right
);
1808 trace
->byte_order
= byte_order
;
1809 CTF_TRACE_SET_FIELD(trace
, byte_order
);
1810 } else if (!strcmp(left
, "packet.header")) {
1811 struct declaration
*declaration
;
1813 if (trace
->packet_header_decl
) {
1814 fprintf(fd
, "[error] %s: packet.header already declared in trace declaration\n", __func__
);
1818 declaration
= ctf_type_specifier_list_visit(fd
, depth
,
1819 _cds_list_first_entry(&node
->u
.ctf_expression
.right
,
1820 struct ctf_node
, siblings
),
1821 trace
->declaration_scope
, trace
);
1826 if (declaration
->id
!= CTF_TYPE_STRUCT
) {
1830 trace
->packet_header_decl
= container_of(declaration
, struct declaration_struct
, p
);
1838 /* TODO: declaration specifier should be added. */
1845 int ctf_trace_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_trace
*trace
)
1847 struct definition_scope
*parent_def_scope
;
1849 struct ctf_node
*iter
;
1851 if (trace
->declaration_scope
)
1853 trace
->declaration_scope
= new_declaration_scope(trace
->root_declaration_scope
);
1854 trace
->streams
= g_ptr_array_new();
1855 cds_list_for_each_entry(iter
, &node
->u
.trace
.declaration_list
, siblings
) {
1856 ret
= ctf_trace_declaration_visit(fd
, depth
+ 1, iter
, trace
);
1860 if (!CTF_TRACE_FIELD_IS_SET(trace
, major
)) {
1862 fprintf(fd
, "[error] %s: missing major field in trace declaration\n", __func__
);
1865 if (!CTF_TRACE_FIELD_IS_SET(trace
, minor
)) {
1867 fprintf(fd
, "[error] %s: missing minor field in trace declaration\n", __func__
);
1870 if (!CTF_TRACE_FIELD_IS_SET(trace
, uuid
)) {
1872 fprintf(fd
, "[error] %s: missing uuid field in trace declaration\n", __func__
);
1875 if (!CTF_TRACE_FIELD_IS_SET(trace
, byte_order
)) {
1877 fprintf(fd
, "[error] %s: missing byte_order field in trace declaration\n", __func__
);
1881 parent_def_scope
= NULL
;
1882 if (trace
->packet_header_decl
) {
1883 trace
->packet_header
=
1885 trace
->packet_header_decl
->p
.definition_new(&trace
->packet_header_decl
->p
,
1886 parent_def_scope
, 0, 0),
1887 struct definition_struct
, p
);
1888 set_dynamic_definition_scope(&trace
->packet_header
->p
,
1889 trace
->packet_header
->scope
,
1890 "trace.packet.header");
1891 parent_def_scope
= trace
->packet_header
->scope
;
1892 declaration_unref(&trace
->packet_header_decl
->p
);
1894 trace
->definition_scope
= parent_def_scope
;
1896 if (!CTF_TRACE_FIELD_IS_SET(trace
, byte_order
)) {
1897 /* check that the packet header contains a "magic" field */
1898 if (!trace
->packet_header
1899 || struct_declaration_lookup_field_index(trace
->packet_header_decl
, g_quark_from_static_string("magic")) < 0) {
1901 fprintf(fd
, "[error] %s: missing both byte_order and packet header magic number in trace declaration\n", __func__
);
1902 goto error_free_def
;
1908 definition_unref(&trace
->packet_header
->p
);
1910 g_ptr_array_free(trace
->streams
, TRUE
);
1911 free_declaration_scope(trace
->declaration_scope
);
1916 int ctf_root_declaration_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_trace
*trace
)
1920 switch (node
->type
) {
1922 ret
= ctf_typedef_visit(fd
, depth
+ 1,
1923 trace
->root_declaration_scope
,
1924 node
->u
._typedef
.type_specifier_list
,
1925 &node
->u
._typedef
.type_declarators
,
1930 case NODE_TYPEALIAS
:
1931 ret
= ctf_typealias_visit(fd
, depth
+ 1,
1932 trace
->root_declaration_scope
,
1933 node
->u
.typealias
.target
, node
->u
.typealias
.alias
,
1938 case NODE_TYPE_SPECIFIER_LIST
:
1940 struct declaration
*declaration
;
1943 * Just add the type specifier to the root scope
1944 * declaration scope. Release local reference.
1946 declaration
= ctf_type_specifier_list_visit(fd
, depth
+ 1,
1947 node
, trace
->root_declaration_scope
, trace
);
1950 declaration_unref(declaration
);
1960 int ctf_visitor_construct_metadata(FILE *fd
, int depth
, struct ctf_node
*node
,
1961 struct ctf_trace
*trace
, int byte_order
)
1964 struct ctf_node
*iter
;
1966 fprintf(fd
, "CTF visitor: metadata construction... ");
1967 trace
->root_declaration_scope
= new_declaration_scope(NULL
);
1968 trace
->byte_order
= byte_order
;
1970 switch (node
->type
) {
1972 cds_list_for_each_entry(iter
, &node
->u
.root
.declaration_list
,
1974 ret
= ctf_root_declaration_visit(fd
, depth
+ 1, iter
, trace
);
1976 fprintf(fd
, "[error] %s: root declaration error\n", __func__
);
1980 cds_list_for_each_entry(iter
, &node
->u
.root
.trace
, siblings
) {
1981 ret
= ctf_trace_visit(fd
, depth
+ 1, iter
, trace
);
1983 fprintf(fd
, "[error] %s: trace declaration error\n", __func__
);
1987 if (!trace
->streams
) {
1988 fprintf(fd
, "[error] %s: missing trace declaration\n", __func__
);
1991 cds_list_for_each_entry(iter
, &node
->u
.root
.stream
, siblings
) {
1992 ret
= ctf_stream_visit(fd
, depth
+ 1, iter
,
1993 trace
->root_declaration_scope
, trace
);
1995 fprintf(fd
, "[error] %s: stream declaration error\n", __func__
);
1999 cds_list_for_each_entry(iter
, &node
->u
.root
.event
, siblings
) {
2000 ret
= ctf_event_visit(fd
, depth
+ 1, iter
,
2001 trace
->root_declaration_scope
, trace
);
2003 fprintf(fd
, "[error] %s: event declaration error\n", __func__
);
2010 fprintf(fd
, "[error] %s: unknown node type %d\n", __func__
,
2014 fprintf(fd
, "done.\n");