2 * ctf-visitor-generate-io-struct.c
4 * Common Trace Format Metadata Visitor (generate I/O structures).
6 * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
28 #include <babeltrace/babeltrace.h>
29 #include <babeltrace/list.h>
30 #include <babeltrace/types.h>
31 #include <babeltrace/ctf/metadata.h>
32 #include <uuid/uuid.h>
33 #include "ctf-scanner.h"
34 #include "ctf-parser.h"
37 #define fprintf_dbg(fd, fmt, args...) fprintf(fd, "%s: " fmt, __func__, ## args)
39 #define _cds_list_first_entry(ptr, type, member) \
40 cds_list_entry((ptr)->next, type, member)
43 struct declaration
*ctf_type_specifier_list_visit(FILE *fd
,
44 int depth
, struct ctf_node
*type_specifier_list
,
45 struct declaration_scope
*declaration_scope
,
46 struct ctf_trace
*trace
);
49 int ctf_stream_visit(FILE *fd
, int depth
, struct ctf_node
*node
,
50 struct declaration_scope
*parent_declaration_scope
, struct ctf_trace
*trace
);
53 * String returned must be freed by the caller using g_free.
56 char *concatenate_unary_strings(struct cds_list_head
*head
)
58 struct ctf_node
*node
;
62 str
= g_string_new("");
63 cds_list_for_each_entry(node
, head
, siblings
) {
66 assert(node
->type
== NODE_UNARY_EXPRESSION
);
67 assert(node
->u
.unary_expression
.type
== UNARY_STRING
);
68 assert((node
->u
.unary_expression
.link
== UNARY_LINK_UNKNOWN
)
70 switch (node
->u
.unary_expression
.link
) {
72 g_string_append(str
, ".");
75 g_string_append(str
, "->");
78 g_string_append(str
, "...");
83 src_string
= node
->u
.unary_expression
.u
.string
;
84 g_string_append(str
, src_string
);
87 return g_string_free(str
, FALSE
);
91 int get_unary_unsigned(struct cds_list_head
*head
, uint64_t *value
)
93 struct ctf_node
*node
;
96 cds_list_for_each_entry(node
, head
, siblings
) {
97 assert(node
->type
== NODE_UNARY_EXPRESSION
);
98 assert(node
->u
.unary_expression
.type
== UNARY_UNSIGNED_CONSTANT
);
99 assert(node
->u
.unary_expression
.link
== UNARY_LINK_UNKNOWN
);
101 *value
= node
->u
.unary_expression
.u
.unsigned_constant
;
108 int get_unary_uuid(struct cds_list_head
*head
, uuid_t
*uuid
)
110 struct ctf_node
*node
;
114 cds_list_for_each_entry(node
, head
, siblings
) {
115 const char *src_string
;
117 assert(node
->type
== NODE_UNARY_EXPRESSION
);
118 assert(node
->u
.unary_expression
.type
== UNARY_STRING
);
119 assert(node
->u
.unary_expression
.link
== UNARY_LINK_UNKNOWN
);
121 src_string
= node
->u
.unary_expression
.u
.string
;
122 ret
= uuid_parse(node
->u
.unary_expression
.u
.string
, *uuid
);
128 struct ctf_stream_class
*trace_stream_lookup(struct ctf_trace
*trace
, uint64_t stream_id
)
130 if (trace
->streams
->len
<= stream_id
)
132 return g_ptr_array_index(trace
->streams
, stream_id
);
136 int visit_type_specifier(FILE *fd
, struct ctf_node
*type_specifier
, GString
*str
)
138 assert(type_specifier
->type
== NODE_TYPE_SPECIFIER
);
140 switch (type_specifier
->u
.type_specifier
.type
) {
142 g_string_append(str
, "void");
145 g_string_append(str
, "char");
148 g_string_append(str
, "short");
151 g_string_append(str
, "int");
154 g_string_append(str
, "long");
157 g_string_append(str
, "float");
159 case TYPESPEC_DOUBLE
:
160 g_string_append(str
, "double");
162 case TYPESPEC_SIGNED
:
163 g_string_append(str
, "signed");
165 case TYPESPEC_UNSIGNED
:
166 g_string_append(str
, "unsigned");
169 g_string_append(str
, "bool");
171 case TYPESPEC_COMPLEX
:
172 g_string_append(str
, "_Complex");
174 case TYPESPEC_IMAGINARY
:
175 g_string_append(str
, "_Imaginary");
178 g_string_append(str
, "const");
180 case TYPESPEC_ID_TYPE
:
181 if (type_specifier
->u
.type_specifier
.id_type
)
182 g_string_append(str
, type_specifier
->u
.type_specifier
.id_type
);
184 case TYPESPEC_STRUCT
:
186 struct ctf_node
*node
= type_specifier
->u
.type_specifier
.node
;
188 if (!node
->u
._struct
.name
) {
189 fprintf(fd
, "[error] %s: unexpected empty variant name\n", __func__
);
192 g_string_append(str
, "struct ");
193 g_string_append(str
, node
->u
._struct
.name
);
196 case TYPESPEC_VARIANT
:
198 struct ctf_node
*node
= type_specifier
->u
.type_specifier
.node
;
200 if (!node
->u
.variant
.name
) {
201 fprintf(fd
, "[error] %s: unexpected empty variant name\n", __func__
);
204 g_string_append(str
, "variant ");
205 g_string_append(str
, node
->u
.variant
.name
);
210 struct ctf_node
*node
= type_specifier
->u
.type_specifier
.node
;
212 if (!node
->u
._enum
.enum_id
) {
213 fprintf(fd
, "[error] %s: unexpected empty enum ID\n", __func__
);
216 g_string_append(str
, "enum ");
217 g_string_append(str
, node
->u
._enum
.enum_id
);
220 case TYPESPEC_FLOATING_POINT
:
221 case TYPESPEC_INTEGER
:
222 case TYPESPEC_STRING
:
224 fprintf(fd
, "[error] %s: unknown specifier\n", __func__
);
231 int visit_type_specifier_list(FILE *fd
, struct ctf_node
*type_specifier_list
, GString
*str
)
233 struct ctf_node
*iter
;
234 int alias_item_nr
= 0;
237 cds_list_for_each_entry(iter
, &type_specifier_list
->u
.type_specifier_list
.head
, siblings
) {
238 if (alias_item_nr
!= 0)
239 g_string_append(str
, " ");
241 ret
= visit_type_specifier(fd
, iter
, str
);
249 GQuark
create_typealias_identifier(FILE *fd
, int depth
,
250 struct ctf_node
*type_specifier_list
,
251 struct ctf_node
*node_type_declarator
)
253 struct ctf_node
*iter
;
259 str
= g_string_new("");
260 ret
= visit_type_specifier_list(fd
, type_specifier_list
, str
);
262 g_string_free(str
, TRUE
);
265 cds_list_for_each_entry(iter
, &node_type_declarator
->u
.type_declarator
.pointers
, siblings
) {
266 g_string_append(str
, " *");
267 if (iter
->u
.pointer
.const_qualifier
)
268 g_string_append(str
, " const");
270 str_c
= g_string_free(str
, FALSE
);
271 alias_q
= g_quark_from_string(str_c
);
277 struct declaration
*ctf_type_declarator_visit(FILE *fd
, int depth
,
278 struct ctf_node
*type_specifier_list
,
280 struct ctf_node
*node_type_declarator
,
281 struct declaration_scope
*declaration_scope
,
282 struct declaration
*nested_declaration
,
283 struct ctf_trace
*trace
)
286 * Visit type declarator by first taking care of sequence/array
287 * (recursively). Then, when we get to the identifier, take care
291 if (node_type_declarator
) {
292 assert(node_type_declarator
->u
.type_declarator
.type
!= TYPEDEC_UNKNOWN
);
294 /* TODO: gcc bitfields not supported yet. */
295 if (node_type_declarator
->u
.type_declarator
.bitfield_len
!= NULL
) {
296 fprintf(fd
, "[error] %s: gcc bitfields are not supported yet.\n", __func__
);
301 if (!nested_declaration
) {
302 if (node_type_declarator
&& !cds_list_empty(&node_type_declarator
->u
.type_declarator
.pointers
)) {
306 * If we have a pointer declarator, it _has_ to be present in
307 * the typealiases (else fail).
309 alias_q
= create_typealias_identifier(fd
, depth
,
310 type_specifier_list
, node_type_declarator
);
311 nested_declaration
= lookup_declaration(alias_q
, declaration_scope
);
312 if (!nested_declaration
) {
313 fprintf(fd
, "[error] %s: cannot find typealias \"%s\".\n", __func__
, g_quark_to_string(alias_q
));
316 if (nested_declaration
->id
== CTF_TYPE_INTEGER
) {
317 struct declaration_integer
*integer_declaration
=
318 container_of(nested_declaration
, struct declaration_integer
, p
);
319 /* For base to 16 for pointers (expected pretty-print) */
320 if (!integer_declaration
->base
) {
322 * We need to do a copy of the
323 * integer declaration to modify it. There could be other references to
326 integer_declaration
= integer_declaration_new(integer_declaration
->len
,
327 integer_declaration
->byte_order
, integer_declaration
->signedness
,
328 integer_declaration
->p
.alignment
, 16, 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__
);
434 struct_declaration_add_field(struct_declaration
,
435 g_quark_to_string(field_name
),
442 int ctf_variant_type_declarators_visit(FILE *fd
, int depth
,
443 struct declaration_untagged_variant
*untagged_variant_declaration
,
444 struct ctf_node
*type_specifier_list
,
445 struct cds_list_head
*type_declarators
,
446 struct declaration_scope
*declaration_scope
,
447 struct ctf_trace
*trace
)
449 struct ctf_node
*iter
;
452 cds_list_for_each_entry(iter
, type_declarators
, siblings
) {
453 struct declaration
*field_declaration
;
455 field_declaration
= ctf_type_declarator_visit(fd
, depth
,
458 untagged_variant_declaration
->scope
,
460 if (!field_declaration
) {
461 fprintf(fd
, "[error] %s: unable to find variant field declaration type\n", __func__
);
464 untagged_variant_declaration_add_field(untagged_variant_declaration
,
465 g_quark_to_string(field_name
),
472 int ctf_typedef_visit(FILE *fd
, int depth
, struct declaration_scope
*scope
,
473 struct ctf_node
*type_specifier_list
,
474 struct cds_list_head
*type_declarators
,
475 struct ctf_trace
*trace
)
477 struct ctf_node
*iter
;
480 cds_list_for_each_entry(iter
, type_declarators
, siblings
) {
481 struct declaration
*type_declaration
;
484 type_declaration
= ctf_type_declarator_visit(fd
, depth
,
488 if (!type_declaration
) {
489 fprintf(fd
, "[error] %s: problem creating type declaration\n", __func__
);
493 * Don't allow typedef and typealias of untagged
496 if (type_declaration
->id
== CTF_TYPE_UNTAGGED_VARIANT
) {
497 fprintf(fd
, "[error] %s: typedef of untagged variant is not permitted.\n", __func__
);
498 declaration_unref(type_declaration
);
501 ret
= register_declaration(identifier
, type_declaration
, scope
);
503 type_declaration
->declaration_free(type_declaration
);
511 int ctf_typealias_visit(FILE *fd
, int depth
, struct declaration_scope
*scope
,
512 struct ctf_node
*target
, struct ctf_node
*alias
,
513 struct ctf_trace
*trace
)
515 struct declaration
*type_declaration
;
516 struct ctf_node
*node
;
521 /* See ctf_visitor_type_declarator() in the semantic validator. */
524 * Create target type declaration.
527 if (cds_list_empty(&target
->u
.typealias_target
.type_declarators
))
530 node
= _cds_list_first_entry(&target
->u
.typealias_target
.type_declarators
,
531 struct ctf_node
, siblings
);
532 type_declaration
= ctf_type_declarator_visit(fd
, depth
,
533 target
->u
.typealias_target
.type_specifier_list
,
536 if (!type_declaration
) {
537 fprintf(fd
, "[error] %s: problem creating type declaration\n", __func__
);
542 * Don't allow typedef and typealias of untagged
545 if (type_declaration
->id
== CTF_TYPE_UNTAGGED_VARIANT
) {
546 fprintf(fd
, "[error] %s: typedef of untagged variant is not permitted.\n", __func__
);
547 declaration_unref(type_declaration
);
551 * The semantic validator does not check whether the target is
552 * abstract or not (if it has an identifier). Check it here.
555 fprintf(fd
, "[error] %s: expecting empty identifier\n", __func__
);
560 * Create alias identifier.
563 node
= _cds_list_first_entry(&alias
->u
.typealias_alias
.type_declarators
,
564 struct ctf_node
, siblings
);
565 alias_q
= create_typealias_identifier(fd
, depth
,
566 alias
->u
.typealias_alias
.type_specifier_list
, node
);
567 err
= register_declaration(alias_q
, type_declaration
, scope
);
573 type_declaration
->declaration_free(type_declaration
);
578 int ctf_struct_declaration_list_visit(FILE *fd
, int depth
,
579 struct ctf_node
*iter
, struct declaration_struct
*struct_declaration
,
580 struct ctf_trace
*trace
)
584 switch (iter
->type
) {
586 /* For each declarator, declare type and add type to struct declaration scope */
587 ret
= ctf_typedef_visit(fd
, depth
,
588 struct_declaration
->scope
,
589 iter
->u
._typedef
.type_specifier_list
,
590 &iter
->u
._typedef
.type_declarators
, trace
);
595 /* Declare type with declarator and add type to struct declaration scope */
596 ret
= ctf_typealias_visit(fd
, depth
,
597 struct_declaration
->scope
,
598 iter
->u
.typealias
.target
,
599 iter
->u
.typealias
.alias
, trace
);
603 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
604 /* Add field to structure declaration */
605 ret
= ctf_struct_type_declarators_visit(fd
, depth
,
607 iter
->u
.struct_or_variant_declaration
.type_specifier_list
,
608 &iter
->u
.struct_or_variant_declaration
.type_declarators
,
609 struct_declaration
->scope
, trace
);
614 fprintf(fd
, "[error] %s: unexpected node type %d\n", __func__
, (int) iter
->type
);
621 int ctf_variant_declaration_list_visit(FILE *fd
, int depth
,
622 struct ctf_node
*iter
,
623 struct declaration_untagged_variant
*untagged_variant_declaration
,
624 struct ctf_trace
*trace
)
628 switch (iter
->type
) {
630 /* For each declarator, declare type and add type to variant declaration scope */
631 ret
= ctf_typedef_visit(fd
, depth
,
632 untagged_variant_declaration
->scope
,
633 iter
->u
._typedef
.type_specifier_list
,
634 &iter
->u
._typedef
.type_declarators
, trace
);
639 /* Declare type with declarator and add type to variant declaration scope */
640 ret
= ctf_typealias_visit(fd
, depth
,
641 untagged_variant_declaration
->scope
,
642 iter
->u
.typealias
.target
,
643 iter
->u
.typealias
.alias
, trace
);
647 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
648 /* Add field to structure declaration */
649 ret
= ctf_variant_type_declarators_visit(fd
, depth
,
650 untagged_variant_declaration
,
651 iter
->u
.struct_or_variant_declaration
.type_specifier_list
,
652 &iter
->u
.struct_or_variant_declaration
.type_declarators
,
653 untagged_variant_declaration
->scope
, trace
);
658 fprintf(fd
, "[error] %s: unexpected node type %d\n", __func__
, (int) iter
->type
);
665 struct declaration
*ctf_declaration_struct_visit(FILE *fd
,
666 int depth
, const char *name
, struct cds_list_head
*declaration_list
,
667 int has_body
, struct cds_list_head
*min_align
,
668 struct declaration_scope
*declaration_scope
,
669 struct ctf_trace
*trace
)
671 struct declaration_struct
*struct_declaration
;
672 struct ctf_node
*iter
;
676 * For named struct (without body), lookup in
677 * declaration scope. Don't take reference on struct
678 * declaration: ref is only taken upon definition.
683 lookup_struct_declaration(g_quark_from_string(name
),
685 return &struct_declaration
->p
;
687 uint64_t min_align_value
= 0;
689 /* For unnamed struct, create type */
690 /* For named struct (with body), create type and add to declaration scope */
692 if (lookup_struct_declaration(g_quark_from_string(name
),
693 declaration_scope
)) {
695 fprintf(fd
, "[error] %s: struct %s already declared in scope\n", __func__
, name
);
699 if (!cds_list_empty(min_align
)) {
700 ret
= get_unary_unsigned(min_align
, &min_align_value
);
702 fprintf(fd
, "[error] %s: unexpected unary expression for structure \"align\" attribute\n", __func__
);
707 struct_declaration
= struct_declaration_new(declaration_scope
,
709 cds_list_for_each_entry(iter
, declaration_list
, siblings
) {
710 ret
= ctf_struct_declaration_list_visit(fd
, depth
+ 1, iter
,
711 struct_declaration
, trace
);
716 ret
= register_struct_declaration(g_quark_from_string(name
),
721 return &struct_declaration
->p
;
724 struct_declaration
->p
.declaration_free(&struct_declaration
->p
);
729 struct declaration
*ctf_declaration_variant_visit(FILE *fd
,
730 int depth
, const char *name
, const char *choice
,
731 struct cds_list_head
*declaration_list
,
732 int has_body
, struct declaration_scope
*declaration_scope
,
733 struct ctf_trace
*trace
)
735 struct declaration_untagged_variant
*untagged_variant_declaration
;
736 struct declaration_variant
*variant_declaration
;
737 struct ctf_node
*iter
;
741 * For named variant (without body), lookup in
742 * declaration scope. Don't take reference on variant
743 * declaration: ref is only taken upon definition.
747 untagged_variant_declaration
=
748 lookup_variant_declaration(g_quark_from_string(name
),
751 /* For unnamed variant, create type */
752 /* For named variant (with body), create type and add to declaration scope */
754 if (lookup_variant_declaration(g_quark_from_string(name
),
755 declaration_scope
)) {
757 fprintf(fd
, "[error] %s: variant %s already declared in scope\n", __func__
, name
);
761 untagged_variant_declaration
= untagged_variant_declaration_new(declaration_scope
);
762 cds_list_for_each_entry(iter
, declaration_list
, siblings
) {
763 ret
= ctf_variant_declaration_list_visit(fd
, depth
+ 1, iter
,
764 untagged_variant_declaration
, trace
);
769 ret
= register_variant_declaration(g_quark_from_string(name
),
770 untagged_variant_declaration
,
776 * if tagged, create tagged variant and return. else return
780 return &untagged_variant_declaration
->p
;
782 variant_declaration
= variant_declaration_new(untagged_variant_declaration
, choice
);
783 if (!variant_declaration
)
785 declaration_unref(&untagged_variant_declaration
->p
);
786 return &variant_declaration
->p
;
789 untagged_variant_declaration
->p
.declaration_free(&untagged_variant_declaration
->p
);
794 int ctf_enumerator_list_visit(FILE *fd
, int depth
,
795 struct ctf_node
*enumerator
,
796 struct declaration_enum
*enum_declaration
)
799 struct ctf_node
*iter
;
801 q
= g_quark_from_string(enumerator
->u
.enumerator
.id
);
802 if (enum_declaration
->integer_declaration
->signedness
) {
806 cds_list_for_each_entry(iter
, &enumerator
->u
.enumerator
.values
, siblings
) {
809 assert(iter
->type
== NODE_UNARY_EXPRESSION
);
815 switch (iter
->u
.unary_expression
.type
) {
816 case UNARY_SIGNED_CONSTANT
:
817 *target
= iter
->u
.unary_expression
.u
.signed_constant
;
819 case UNARY_UNSIGNED_CONSTANT
:
820 *target
= iter
->u
.unary_expression
.u
.unsigned_constant
;
823 fprintf(fd
, "[error] %s: invalid enumerator\n", __func__
);
827 fprintf(fd
, "[error] %s: invalid enumerator\n", __func__
);
834 enum_signed_insert(enum_declaration
, start
, end
, q
);
839 cds_list_for_each_entry(iter
, &enumerator
->u
.enumerator
.values
, siblings
) {
842 assert(iter
->type
== NODE_UNARY_EXPRESSION
);
848 switch (iter
->u
.unary_expression
.type
) {
849 case UNARY_UNSIGNED_CONSTANT
:
850 *target
= iter
->u
.unary_expression
.u
.unsigned_constant
;
852 case UNARY_SIGNED_CONSTANT
:
854 * We don't accept signed constants for enums with unsigned
857 fprintf(fd
, "[error] %s: invalid enumerator (signed constant encountered, but enum container type is unsigned)\n", __func__
);
860 fprintf(fd
, "[error] %s: invalid enumerator\n", __func__
);
864 fprintf(fd
, "[error] %s: invalid enumerator\n", __func__
);
871 enum_unsigned_insert(enum_declaration
, start
, end
, q
);
877 struct declaration
*ctf_declaration_enum_visit(FILE *fd
, int depth
,
879 struct ctf_node
*container_type
,
880 struct cds_list_head
*enumerator_list
,
882 struct declaration_scope
*declaration_scope
,
883 struct ctf_trace
*trace
)
885 struct declaration
*declaration
;
886 struct declaration_enum
*enum_declaration
;
887 struct declaration_integer
*integer_declaration
;
888 struct ctf_node
*iter
;
893 * For named enum (without body), lookup in
894 * declaration scope. Don't take reference on enum
895 * declaration: ref is only taken upon definition.
900 lookup_enum_declaration(g_quark_from_string(name
),
902 return &enum_declaration
->p
;
904 /* For unnamed enum, create type */
905 /* For named enum (with body), create type and add to declaration scope */
907 if (lookup_enum_declaration(g_quark_from_string(name
),
908 declaration_scope
)) {
910 fprintf(fd
, "[error] %s: enum %s already declared in scope\n", __func__
, name
);
914 if (!container_type
) {
915 declaration
= lookup_declaration(g_quark_from_static_string("int"),
918 fprintf(fd
, "[error] %s: \"int\" type declaration missing for enumeration\n", __func__
);
922 declaration
= ctf_type_declarator_visit(fd
, depth
,
929 fprintf(fd
, "[error] %s: unable to create container type for enumeration\n", __func__
);
932 if (declaration
->id
!= CTF_TYPE_INTEGER
) {
933 fprintf(fd
, "[error] %s: container type for enumeration is not integer\n", __func__
);
936 integer_declaration
= container_of(declaration
, struct declaration_integer
, p
);
937 enum_declaration
= enum_declaration_new(integer_declaration
);
938 declaration_unref(&integer_declaration
->p
); /* leave ref to enum */
939 cds_list_for_each_entry(iter
, enumerator_list
, siblings
) {
940 ret
= ctf_enumerator_list_visit(fd
, depth
+ 1, iter
, enum_declaration
);
945 ret
= register_enum_declaration(g_quark_from_string(name
),
950 return &enum_declaration
->p
;
953 enum_declaration
->p
.declaration_free(&enum_declaration
->p
);
958 struct declaration
*ctf_declaration_type_specifier_visit(FILE *fd
, int depth
,
959 struct ctf_node
*type_specifier_list
,
960 struct declaration_scope
*declaration_scope
)
963 struct declaration
*declaration
;
968 str
= g_string_new("");
969 ret
= visit_type_specifier_list(fd
, type_specifier_list
, str
);
972 str_c
= g_string_free(str
, FALSE
);
973 id_q
= g_quark_from_string(str_c
);
975 declaration
= lookup_declaration(id_q
, declaration_scope
);
980 * Returns 0/1 boolean, or < 0 on error.
983 int get_boolean(FILE *fd
, int depth
, struct ctf_node
*unary_expression
)
985 if (unary_expression
->type
!= NODE_UNARY_EXPRESSION
) {
986 fprintf(fd
, "[error] %s: expecting unary expression\n",
990 switch (unary_expression
->u
.unary_expression
.type
) {
991 case UNARY_UNSIGNED_CONSTANT
:
992 if (unary_expression
->u
.unary_expression
.u
.unsigned_constant
== 0)
996 case UNARY_SIGNED_CONSTANT
:
997 if (unary_expression
->u
.unary_expression
.u
.signed_constant
== 0)
1002 if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "true"))
1004 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "TRUE"))
1006 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "false"))
1008 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "FALSE"))
1011 fprintf(fd
, "[error] %s: unexpected string \"%s\"\n",
1012 __func__
, unary_expression
->u
.unary_expression
.u
.string
);
1017 fprintf(fd
, "[error] %s: unexpected unary expression type\n",
1025 int get_trace_byte_order(FILE *fd
, int depth
, struct ctf_node
*unary_expression
)
1029 if (unary_expression
->u
.unary_expression
.type
!= UNARY_STRING
) {
1030 fprintf(fd
, "[error] %s: byte_order: expecting string\n",
1034 if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "be"))
1035 byte_order
= BIG_ENDIAN
;
1036 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "le"))
1037 byte_order
= LITTLE_ENDIAN
;
1039 fprintf(fd
, "[error] %s: unexpected string \"%s\". Should be \"native\", \"network\", \"be\" or \"le\".\n",
1040 __func__
, unary_expression
->u
.unary_expression
.u
.string
);
1047 int get_byte_order(FILE *fd
, int depth
, struct ctf_node
*unary_expression
,
1048 struct ctf_trace
*trace
)
1052 if (unary_expression
->u
.unary_expression
.type
!= UNARY_STRING
) {
1053 fprintf(fd
, "[error] %s: byte_order: expecting string\n",
1057 if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "native"))
1058 byte_order
= trace
->byte_order
;
1059 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "network"))
1060 byte_order
= BIG_ENDIAN
;
1061 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "be"))
1062 byte_order
= BIG_ENDIAN
;
1063 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "le"))
1064 byte_order
= LITTLE_ENDIAN
;
1066 fprintf(fd
, "[error] %s: unexpected string \"%s\". Should be \"native\", \"network\", \"be\" or \"le\".\n",
1067 __func__
, unary_expression
->u
.unary_expression
.u
.string
);
1074 struct declaration
*ctf_declaration_integer_visit(FILE *fd
, int depth
,
1075 struct cds_list_head
*expressions
,
1076 struct ctf_trace
*trace
)
1078 struct ctf_node
*expression
;
1079 uint64_t alignment
, size
;
1080 int byte_order
= trace
->byte_order
;
1082 int has_alignment
= 0, has_size
= 0;
1084 enum ctf_string_encoding encoding
= CTF_STRING_NONE
;
1085 struct declaration_integer
*integer_declaration
;
1087 cds_list_for_each_entry(expression
, expressions
, siblings
) {
1088 struct ctf_node
*left
, *right
;
1090 left
= _cds_list_first_entry(&expression
->u
.ctf_expression
.left
, struct ctf_node
, siblings
);
1091 right
= _cds_list_first_entry(&expression
->u
.ctf_expression
.right
, struct ctf_node
, siblings
);
1092 assert(left
->u
.unary_expression
.type
== UNARY_STRING
);
1093 if (!strcmp(left
->u
.unary_expression
.u
.string
, "signed")) {
1094 signedness
= get_boolean(fd
, depth
, right
);
1097 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "byte_order")) {
1098 byte_order
= get_byte_order(fd
, depth
, right
, trace
);
1101 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "size")) {
1102 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
1103 fprintf(fd
, "[error] %s: size: expecting unsigned constant\n",
1107 size
= right
->u
.unary_expression
.u
.unsigned_constant
;
1109 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "align")) {
1110 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
1111 fprintf(fd
, "[error] %s: align: expecting unsigned constant\n",
1115 alignment
= right
->u
.unary_expression
.u
.unsigned_constant
;
1116 /* Make sure alignment is a power of two */
1117 if (alignment
== 0 || (alignment
& (alignment
- 1)) != 0) {
1118 fprintf(fd
, "[error] %s: align: expecting power of two\n",
1123 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "base")) {
1124 switch (right
->u
.unary_expression
.type
) {
1125 case UNARY_UNSIGNED_CONSTANT
:
1126 switch (right
->u
.unary_expression
.u
.unsigned_constant
) {
1131 base
= right
->u
.unary_expression
.u
.unsigned_constant
;
1134 fprintf(fd
, "[error] %s: base not supported (%" PRIu64
")\n",
1135 __func__
, right
->u
.unary_expression
.u
.unsigned_constant
);
1141 char *s_right
= concatenate_unary_strings(&expression
->u
.ctf_expression
.right
);
1143 fprintf(fd
, "[error] %s: unexpected unary expression for integer base\n", __func__
);
1147 if (!strcmp(s_right
, "decimal") || !strcmp(s_right
, "dec") || !strcmp(s_right
, "d")
1148 || !strcmp(s_right
, "i") || !strcmp(s_right
, "u")) {
1150 } else if (!strcmp(s_right
, "hexadecimal") || !strcmp(s_right
, "hex")
1151 || !strcmp(s_right
, "x") || !strcmp(s_right
, "X")
1152 || !strcmp(s_right
, "p")) {
1154 } else if (!strcmp(s_right
, "octal") || !strcmp(s_right
, "oct")
1155 || !strcmp(s_right
, "o")) {
1157 } else if (!strcmp(s_right
, "binary") || !strcmp(s_right
, "b")) {
1160 fprintf(fd
, "[error] %s: unexpected expression for integer base (%s)\n", __func__
, s_right
);
1169 fprintf(fd
, "[error] %s: base: expecting unsigned constant or unary string\n",
1173 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "encoding")) {
1176 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
1177 fprintf(fd
, "[error] %s: encoding: expecting unary string\n",
1181 s_right
= concatenate_unary_strings(&expression
->u
.ctf_expression
.right
);
1183 fprintf(fd
, "[error] %s: unexpected unary expression for integer base\n", __func__
);
1187 if (!strcmp(s_right
, "UTF8")
1188 || !strcmp(s_right
, "utf8")
1189 || !strcmp(s_right
, "utf-8")
1190 || !strcmp(s_right
, "UTF-8"))
1191 encoding
= CTF_STRING_UTF8
;
1192 else if (!strcmp(s_right
, "ASCII")
1193 || !strcmp(s_right
, "ascii"))
1194 encoding
= CTF_STRING_ASCII
;
1195 else if (!strcmp(s_right
, "none"))
1196 encoding
= CTF_STRING_NONE
;
1198 fprintf(fd
, "[error] %s: unknown string encoding \"%s\"\n", __func__
, s_right
);
1204 fprintf(fd
, "[error] %s: unknown attribute name %s\n",
1205 __func__
, left
->u
.unary_expression
.u
.string
);
1210 fprintf(fd
, "[error] %s: missing size attribute\n", __func__
);
1213 if (!has_alignment
) {
1214 if (size
% CHAR_BIT
) {
1215 /* bit-packed alignment */
1218 /* byte-packed alignment */
1219 alignment
= CHAR_BIT
;
1222 integer_declaration
= integer_declaration_new(size
,
1223 byte_order
, signedness
, alignment
,
1225 return &integer_declaration
->p
;
1229 struct declaration
*ctf_declaration_floating_point_visit(FILE *fd
, int depth
,
1230 struct cds_list_head
*expressions
,
1231 struct ctf_trace
*trace
)
1233 struct ctf_node
*expression
;
1234 uint64_t alignment
, exp_dig
, mant_dig
, byte_order
= trace
->byte_order
;
1235 int has_alignment
= 0, has_exp_dig
= 0, has_mant_dig
= 0;
1236 struct declaration_float
*float_declaration
;
1238 cds_list_for_each_entry(expression
, expressions
, siblings
) {
1239 struct ctf_node
*left
, *right
;
1241 left
= _cds_list_first_entry(&expression
->u
.ctf_expression
.left
, struct ctf_node
, siblings
);
1242 right
= _cds_list_first_entry(&expression
->u
.ctf_expression
.right
, struct ctf_node
, siblings
);
1243 assert(left
->u
.unary_expression
.type
== UNARY_STRING
);
1244 if (!strcmp(left
->u
.unary_expression
.u
.string
, "byte_order")) {
1245 byte_order
= get_byte_order(fd
, depth
, right
, trace
);
1248 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "exp_dig")) {
1249 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
1250 fprintf(fd
, "[error] %s: exp_dig: expecting unsigned constant\n",
1254 exp_dig
= right
->u
.unary_expression
.u
.unsigned_constant
;
1256 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "mant_dig")) {
1257 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
1258 fprintf(fd
, "[error] %s: mant_dig: expecting unsigned constant\n",
1262 mant_dig
= right
->u
.unary_expression
.u
.unsigned_constant
;
1264 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "align")) {
1265 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
1266 fprintf(fd
, "[error] %s: align: expecting unsigned constant\n",
1270 alignment
= right
->u
.unary_expression
.u
.unsigned_constant
;
1271 /* Make sure alignment is a power of two */
1272 if (alignment
== 0 || (alignment
& (alignment
- 1)) != 0) {
1273 fprintf(fd
, "[error] %s: align: expecting power of two\n",
1279 fprintf(fd
, "[error] %s: unknown attribute name %s\n",
1280 __func__
, left
->u
.unary_expression
.u
.string
);
1284 if (!has_mant_dig
) {
1285 fprintf(fd
, "[error] %s: missing mant_dig attribute\n", __func__
);
1289 fprintf(fd
, "[error] %s: missing exp_dig attribute\n", __func__
);
1292 if (!has_alignment
) {
1293 if ((mant_dig
+ exp_dig
) % CHAR_BIT
) {
1294 /* bit-packed alignment */
1297 /* byte-packed alignment */
1298 alignment
= CHAR_BIT
;
1301 float_declaration
= float_declaration_new(mant_dig
, exp_dig
,
1302 byte_order
, alignment
);
1303 return &float_declaration
->p
;
1307 struct declaration
*ctf_declaration_string_visit(FILE *fd
, int depth
,
1308 struct cds_list_head
*expressions
,
1309 struct ctf_trace
*trace
)
1311 struct ctf_node
*expression
;
1312 const char *encoding_c
= NULL
;
1313 enum ctf_string_encoding encoding
= CTF_STRING_UTF8
;
1314 struct declaration_string
*string_declaration
;
1316 cds_list_for_each_entry(expression
, expressions
, siblings
) {
1317 struct ctf_node
*left
, *right
;
1319 left
= _cds_list_first_entry(&expression
->u
.ctf_expression
.left
, struct ctf_node
, siblings
);
1320 right
= _cds_list_first_entry(&expression
->u
.ctf_expression
.right
, struct ctf_node
, siblings
);
1321 assert(left
->u
.unary_expression
.type
== UNARY_STRING
);
1322 if (!strcmp(left
->u
.unary_expression
.u
.string
, "encoding")) {
1323 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
1324 fprintf(fd
, "[error] %s: encoding: expecting string\n",
1328 encoding_c
= right
->u
.unary_expression
.u
.string
;
1330 fprintf(fd
, "[error] %s: unknown attribute name %s\n",
1331 __func__
, left
->u
.unary_expression
.u
.string
);
1335 if (encoding_c
&& !strcmp(encoding_c
, "ASCII"))
1336 encoding
= CTF_STRING_ASCII
;
1337 string_declaration
= string_declaration_new(encoding
);
1338 return &string_declaration
->p
;
1343 struct declaration
*ctf_type_specifier_list_visit(FILE *fd
,
1344 int depth
, struct ctf_node
*type_specifier_list
,
1345 struct declaration_scope
*declaration_scope
,
1346 struct ctf_trace
*trace
)
1348 struct ctf_node
*first
;
1349 struct ctf_node
*node
;
1351 assert(type_specifier_list
->type
== NODE_TYPE_SPECIFIER_LIST
);
1353 first
= _cds_list_first_entry(&type_specifier_list
->u
.type_specifier_list
.head
, struct ctf_node
, siblings
);
1355 assert(first
->type
== NODE_TYPE_SPECIFIER
);
1357 node
= first
->u
.type_specifier
.node
;
1359 switch (first
->u
.type_specifier
.type
) {
1360 case TYPESPEC_FLOATING_POINT
:
1361 return ctf_declaration_floating_point_visit(fd
, depth
,
1362 &node
->u
.floating_point
.expressions
, trace
);
1363 case TYPESPEC_INTEGER
:
1364 return ctf_declaration_integer_visit(fd
, depth
,
1365 &node
->u
.integer
.expressions
, trace
);
1366 case TYPESPEC_STRING
:
1367 return ctf_declaration_string_visit(fd
, depth
,
1368 &node
->u
.string
.expressions
, trace
);
1369 case TYPESPEC_STRUCT
:
1370 return ctf_declaration_struct_visit(fd
, depth
,
1371 node
->u
._struct
.name
,
1372 &node
->u
._struct
.declaration_list
,
1373 node
->u
._struct
.has_body
,
1374 &node
->u
._struct
.min_align
,
1377 case TYPESPEC_VARIANT
:
1378 return ctf_declaration_variant_visit(fd
, depth
,
1379 node
->u
.variant
.name
,
1380 node
->u
.variant
.choice
,
1381 &node
->u
.variant
.declaration_list
,
1382 node
->u
.variant
.has_body
,
1386 return ctf_declaration_enum_visit(fd
, depth
,
1387 node
->u
._enum
.enum_id
,
1388 node
->u
._enum
.container_type
,
1389 &node
->u
._enum
.enumerator_list
,
1390 node
->u
._enum
.has_body
,
1396 case TYPESPEC_SHORT
:
1399 case TYPESPEC_FLOAT
:
1400 case TYPESPEC_DOUBLE
:
1401 case TYPESPEC_SIGNED
:
1402 case TYPESPEC_UNSIGNED
:
1404 case TYPESPEC_COMPLEX
:
1405 case TYPESPEC_IMAGINARY
:
1406 case TYPESPEC_CONST
:
1407 case TYPESPEC_ID_TYPE
:
1408 return ctf_declaration_type_specifier_visit(fd
, depth
,
1409 type_specifier_list
, declaration_scope
);
1411 fprintf(fd
, "[error] %s: unexpected node type %d\n", __func__
, (int) first
->u
.type_specifier
.type
);
1417 int ctf_event_declaration_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_event
*event
, struct ctf_trace
*trace
)
1421 switch (node
->type
) {
1423 ret
= ctf_typedef_visit(fd
, depth
+ 1,
1424 event
->declaration_scope
,
1425 node
->u
._typedef
.type_specifier_list
,
1426 &node
->u
._typedef
.type_declarators
,
1431 case NODE_TYPEALIAS
:
1432 ret
= ctf_typealias_visit(fd
, depth
+ 1,
1433 event
->declaration_scope
,
1434 node
->u
.typealias
.target
, node
->u
.typealias
.alias
,
1439 case NODE_CTF_EXPRESSION
:
1443 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
1444 if (!strcmp(left
, "name")) {
1447 if (CTF_EVENT_FIELD_IS_SET(event
, name
)) {
1448 fprintf(fd
, "[error] %s: name already declared in event declaration\n", __func__
);
1452 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
1454 fprintf(fd
, "[error] %s: unexpected unary expression for event name\n", __func__
);
1458 event
->name
= g_quark_from_string(right
);
1460 CTF_EVENT_SET_FIELD(event
, name
);
1461 } else if (!strcmp(left
, "id")) {
1462 if (CTF_EVENT_FIELD_IS_SET(event
, id
)) {
1463 fprintf(fd
, "[error] %s: id already declared in event declaration\n", __func__
);
1467 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &event
->id
);
1469 fprintf(fd
, "[error] %s: unexpected unary expression for event id\n", __func__
);
1473 CTF_EVENT_SET_FIELD(event
, id
);
1474 } else if (!strcmp(left
, "stream_id")) {
1475 if (CTF_EVENT_FIELD_IS_SET(event
, stream_id
)) {
1476 fprintf(fd
, "[error] %s: stream_id already declared in event declaration\n", __func__
);
1480 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &event
->stream_id
);
1482 fprintf(fd
, "[error] %s: unexpected unary expression for event stream_id\n", __func__
);
1486 event
->stream
= trace_stream_lookup(trace
, event
->stream_id
);
1487 if (!event
->stream
) {
1488 fprintf(fd
, "[error] %s: stream id %" PRIu64
" cannot be found\n", __func__
, event
->stream_id
);
1492 CTF_EVENT_SET_FIELD(event
, stream_id
);
1493 } else if (!strcmp(left
, "context")) {
1494 struct declaration
*declaration
;
1496 if (event
->context_decl
) {
1497 fprintf(fd
, "[error] %s: context already declared in event declaration\n", __func__
);
1501 declaration
= ctf_type_specifier_list_visit(fd
, depth
,
1502 _cds_list_first_entry(&node
->u
.ctf_expression
.right
,
1503 struct ctf_node
, siblings
),
1504 event
->declaration_scope
, trace
);
1509 if (declaration
->id
!= CTF_TYPE_STRUCT
) {
1513 event
->context_decl
= container_of(declaration
, struct declaration_struct
, p
);
1514 } else if (!strcmp(left
, "fields")) {
1515 struct declaration
*declaration
;
1517 if (event
->fields_decl
) {
1518 fprintf(fd
, "[error] %s: fields already declared in event declaration\n", __func__
);
1522 declaration
= ctf_type_specifier_list_visit(fd
, depth
,
1523 _cds_list_first_entry(&node
->u
.ctf_expression
.right
,
1524 struct ctf_node
, siblings
),
1525 event
->declaration_scope
, trace
);
1530 if (declaration
->id
!= CTF_TYPE_STRUCT
) {
1534 event
->fields_decl
= container_of(declaration
, struct declaration_struct
, p
);
1536 fprintf(fd
, "[error] %s: attribute \"%s\" is unknown in event declaration.\n", __func__
, left
);
1546 /* TODO: declaration specifier should be added. */
1553 int ctf_event_visit(FILE *fd
, int depth
, struct ctf_node
*node
,
1554 struct declaration_scope
*parent_declaration_scope
, struct ctf_trace
*trace
)
1557 struct ctf_node
*iter
;
1558 struct ctf_event
*event
;
1559 struct definition_scope
*parent_def_scope
;
1561 event
= g_new0(struct ctf_event
, 1);
1562 event
->declaration_scope
= new_declaration_scope(parent_declaration_scope
);
1563 cds_list_for_each_entry(iter
, &node
->u
.event
.declaration_list
, siblings
) {
1564 ret
= ctf_event_declaration_visit(fd
, depth
+ 1, iter
, event
, trace
);
1568 if (!CTF_EVENT_FIELD_IS_SET(event
, name
)) {
1570 fprintf(fd
, "[error] %s: missing name field in event declaration\n", __func__
);
1573 if (!CTF_EVENT_FIELD_IS_SET(event
, stream_id
)) {
1574 /* Allow missing stream_id if there is only a single stream */
1575 switch (trace
->streams
->len
) {
1576 case 0: /* Create stream if there was none. */
1577 ret
= ctf_stream_visit(fd
, depth
, NULL
, trace
->root_declaration_scope
, trace
);
1582 event
->stream_id
= 0;
1583 event
->stream
= trace_stream_lookup(trace
, event
->stream_id
);
1587 fprintf(fd
, "[error] %s: missing stream_id field in event declaration\n", __func__
);
1591 /* Allow only one event without id per stream */
1592 if (!CTF_EVENT_FIELD_IS_SET(event
, id
)
1593 && event
->stream
->events_by_id
->len
!= 0) {
1595 fprintf(fd
, "[error] %s: missing id field in event declaration\n", __func__
);
1598 if (event
->stream
->events_by_id
->len
<= event
->id
)
1599 g_ptr_array_set_size(event
->stream
->events_by_id
, event
->id
+ 1);
1600 g_ptr_array_index(event
->stream
->events_by_id
, event
->id
) = event
;
1601 g_hash_table_insert(event
->stream
->event_quark_to_id
,
1602 (gpointer
)(unsigned long) event
->name
,
1604 parent_def_scope
= event
->stream
->definition_scope
;
1605 if (event
->context_decl
) {
1606 struct definition
*definition
=
1607 event
->context_decl
->p
.definition_new(&event
->context_decl
->p
,
1608 parent_def_scope
, 0, 0, "event.context");
1613 event
->context
= container_of(definition
,
1614 struct definition_struct
, p
);
1615 parent_def_scope
= event
->context
->p
.scope
;
1617 if (event
->fields_decl
) {
1618 struct definition
*definition
=
1619 event
->fields_decl
->p
.definition_new(&event
->fields_decl
->p
,
1620 parent_def_scope
, 0, 0, "event.fields");
1625 event
->fields
= container_of(definition
,
1626 struct definition_struct
, p
);
1627 parent_def_scope
= event
->fields
->p
.scope
;
1633 definition_unref(&event
->context
->p
);
1635 definition_unref(&event
->fields
->p
);
1636 if (event
->fields_decl
)
1637 declaration_unref(&event
->fields_decl
->p
);
1638 if (event
->context_decl
)
1639 declaration_unref(&event
->context_decl
->p
);
1640 free_declaration_scope(event
->declaration_scope
);
1647 int ctf_stream_declaration_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_stream_class
*stream
, struct ctf_trace
*trace
)
1651 switch (node
->type
) {
1653 ret
= ctf_typedef_visit(fd
, depth
+ 1,
1654 stream
->declaration_scope
,
1655 node
->u
._typedef
.type_specifier_list
,
1656 &node
->u
._typedef
.type_declarators
,
1661 case NODE_TYPEALIAS
:
1662 ret
= ctf_typealias_visit(fd
, depth
+ 1,
1663 stream
->declaration_scope
,
1664 node
->u
.typealias
.target
, node
->u
.typealias
.alias
,
1669 case NODE_CTF_EXPRESSION
:
1673 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
1674 if (!strcmp(left
, "id")) {
1675 if (CTF_STREAM_FIELD_IS_SET(stream
, stream_id
)) {
1676 fprintf(fd
, "[error] %s: id already declared in stream declaration\n", __func__
);
1680 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &stream
->stream_id
);
1682 fprintf(fd
, "[error] %s: unexpected unary expression for stream id\n", __func__
);
1686 CTF_STREAM_SET_FIELD(stream
, stream_id
);
1687 } else if (!strcmp(left
, "event.header")) {
1688 struct declaration
*declaration
;
1690 if (stream
->event_header_decl
) {
1691 fprintf(fd
, "[error] %s: event.header already declared in stream declaration\n", __func__
);
1695 declaration
= ctf_type_specifier_list_visit(fd
, depth
,
1696 _cds_list_first_entry(&node
->u
.ctf_expression
.right
,
1697 struct ctf_node
, siblings
),
1698 stream
->declaration_scope
, trace
);
1703 if (declaration
->id
!= CTF_TYPE_STRUCT
) {
1707 stream
->event_header_decl
= container_of(declaration
, struct declaration_struct
, p
);
1708 } else if (!strcmp(left
, "event.context")) {
1709 struct declaration
*declaration
;
1711 if (stream
->event_context_decl
) {
1712 fprintf(fd
, "[error] %s: event.context already declared in stream declaration\n", __func__
);
1716 declaration
= ctf_type_specifier_list_visit(fd
, depth
,
1717 _cds_list_first_entry(&node
->u
.ctf_expression
.right
,
1718 struct ctf_node
, siblings
),
1719 stream
->declaration_scope
, trace
);
1724 if (declaration
->id
!= CTF_TYPE_STRUCT
) {
1728 stream
->event_context_decl
= container_of(declaration
, struct declaration_struct
, p
);
1729 } else if (!strcmp(left
, "packet.context")) {
1730 struct declaration
*declaration
;
1732 if (stream
->packet_context_decl
) {
1733 fprintf(fd
, "[error] %s: packet.context already declared in stream declaration\n", __func__
);
1737 declaration
= ctf_type_specifier_list_visit(fd
, depth
,
1738 _cds_list_first_entry(&node
->u
.ctf_expression
.right
,
1739 struct ctf_node
, siblings
),
1740 stream
->declaration_scope
, trace
);
1745 if (declaration
->id
!= CTF_TYPE_STRUCT
) {
1749 stream
->packet_context_decl
= container_of(declaration
, struct declaration_struct
, p
);
1751 fprintf(fd
, "[error] %s: attribute \"%s\" is unknown in stream declaration.\n", __func__
, left
);
1762 /* TODO: declaration specifier should be added. */
1769 int ctf_stream_visit(FILE *fd
, int depth
, struct ctf_node
*node
,
1770 struct declaration_scope
*parent_declaration_scope
, struct ctf_trace
*trace
)
1773 struct ctf_node
*iter
;
1774 struct ctf_stream_class
*stream
;
1775 struct definition_scope
*parent_def_scope
;
1777 stream
= g_new0(struct ctf_stream_class
, 1);
1778 stream
->declaration_scope
= new_declaration_scope(parent_declaration_scope
);
1779 stream
->events_by_id
= g_ptr_array_new();
1780 stream
->event_quark_to_id
= g_hash_table_new(g_direct_hash
, g_direct_equal
);
1781 stream
->files
= g_ptr_array_new();
1783 cds_list_for_each_entry(iter
, &node
->u
.stream
.declaration_list
, siblings
) {
1784 ret
= ctf_stream_declaration_visit(fd
, depth
+ 1, iter
, stream
, trace
);
1789 if (CTF_STREAM_FIELD_IS_SET(stream
, stream_id
)) {
1790 /* check that packet header has stream_id field. */
1791 if (!trace
->packet_header_decl
1792 || struct_declaration_lookup_field_index(trace
->packet_header_decl
, g_quark_from_static_string("stream_id")) < 0) {
1794 fprintf(fd
, "[error] %s: missing stream_id field in packet header declaration, but stream_id attribute is declared for stream.\n", __func__
);
1798 /* Allow only one id-less stream */
1799 if (trace
->streams
->len
!= 0) {
1801 fprintf(fd
, "[error] %s: missing id field in stream declaration\n", __func__
);
1804 stream
->stream_id
= 0;
1806 if (trace
->streams
->len
<= stream
->stream_id
)
1807 g_ptr_array_set_size(trace
->streams
, stream
->stream_id
+ 1);
1808 g_ptr_array_index(trace
->streams
, stream
->stream_id
) = stream
;
1810 parent_def_scope
= trace
->definition_scope
;
1811 if (stream
->packet_context_decl
) {
1812 struct definition
*definition
=
1813 stream
->packet_context_decl
->p
.definition_new(&stream
->packet_context_decl
->p
,
1814 parent_def_scope
, 0, 0, "stream.packet.context");
1819 stream
->packet_context
= container_of(definition
,
1820 struct definition_struct
, p
);
1821 parent_def_scope
= stream
->packet_context
->p
.scope
;
1823 if (stream
->event_header_decl
) {
1824 struct definition
*definition
=
1825 stream
->event_header_decl
->p
.definition_new(&stream
->event_header_decl
->p
,
1826 parent_def_scope
, 0, 0, "stream.event.header");
1831 stream
->event_header
=
1832 container_of(definition
, struct definition_struct
, p
);
1833 parent_def_scope
= stream
->event_header
->p
.scope
;
1835 if (stream
->event_context_decl
) {
1836 struct definition
*definition
=
1837 stream
->event_context_decl
->p
.definition_new(&stream
->event_context_decl
->p
,
1838 parent_def_scope
, 0, 0, "stream.event.context");
1843 stream
->event_context
=
1844 container_of(definition
, struct definition_struct
, p
);
1845 parent_def_scope
= stream
->event_context
->p
.scope
;
1847 stream
->definition_scope
= parent_def_scope
;
1852 if (stream
->event_context
)
1853 definition_unref(&stream
->event_context
->p
);
1854 if (stream
->event_header
)
1855 definition_unref(&stream
->event_header
->p
);
1856 if (stream
->packet_context
)
1857 definition_unref(&stream
->packet_context
->p
);
1858 if (stream
->event_header_decl
)
1859 declaration_unref(&stream
->event_header_decl
->p
);
1860 if (stream
->event_context_decl
)
1861 declaration_unref(&stream
->event_context_decl
->p
);
1862 if (stream
->packet_context_decl
)
1863 declaration_unref(&stream
->packet_context_decl
->p
);
1864 g_ptr_array_free(stream
->files
, TRUE
);
1865 g_ptr_array_free(stream
->events_by_id
, TRUE
);
1866 g_hash_table_destroy(stream
->event_quark_to_id
);
1867 free_declaration_scope(stream
->declaration_scope
);
1873 int ctf_trace_declaration_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_trace
*trace
)
1877 switch (node
->type
) {
1879 ret
= ctf_typedef_visit(fd
, depth
+ 1,
1880 trace
->declaration_scope
,
1881 node
->u
._typedef
.type_specifier_list
,
1882 &node
->u
._typedef
.type_declarators
,
1887 case NODE_TYPEALIAS
:
1888 ret
= ctf_typealias_visit(fd
, depth
+ 1,
1889 trace
->declaration_scope
,
1890 node
->u
.typealias
.target
, node
->u
.typealias
.alias
,
1895 case NODE_CTF_EXPRESSION
:
1899 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
1900 if (!strcmp(left
, "major")) {
1901 if (CTF_TRACE_FIELD_IS_SET(trace
, major
)) {
1902 fprintf(fd
, "[error] %s: major already declared in trace declaration\n", __func__
);
1906 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &trace
->major
);
1908 fprintf(fd
, "[error] %s: unexpected unary expression for trace major number\n", __func__
);
1912 CTF_TRACE_SET_FIELD(trace
, major
);
1913 } else if (!strcmp(left
, "minor")) {
1914 if (CTF_TRACE_FIELD_IS_SET(trace
, minor
)) {
1915 fprintf(fd
, "[error] %s: minor already declared in trace declaration\n", __func__
);
1919 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &trace
->minor
);
1921 fprintf(fd
, "[error] %s: unexpected unary expression for trace minor number\n", __func__
);
1925 CTF_TRACE_SET_FIELD(trace
, minor
);
1926 } else if (!strcmp(left
, "uuid")) {
1929 ret
= get_unary_uuid(&node
->u
.ctf_expression
.right
, &uuid
);
1931 fprintf(fd
, "[error] %s: unexpected unary expression for trace uuid\n", __func__
);
1935 if (CTF_TRACE_FIELD_IS_SET(trace
, uuid
)
1936 && uuid_compare(uuid
, trace
->uuid
)) {
1937 fprintf(fd
, "[error] %s: uuid mismatch\n", __func__
);
1941 memcpy(trace
->uuid
, uuid
, sizeof(uuid
));
1943 CTF_TRACE_SET_FIELD(trace
, uuid
);
1944 } else if (!strcmp(left
, "byte_order")) {
1945 struct ctf_node
*right
;
1948 right
= _cds_list_first_entry(&node
->u
.ctf_expression
.right
, struct ctf_node
, siblings
);
1949 byte_order
= get_trace_byte_order(fd
, depth
, right
);
1953 if (CTF_TRACE_FIELD_IS_SET(trace
, byte_order
)
1954 && byte_order
!= trace
->byte_order
) {
1955 fprintf(fd
, "[error] %s: endianness mismatch\n", __func__
);
1959 trace
->byte_order
= byte_order
;
1961 CTF_TRACE_SET_FIELD(trace
, byte_order
);
1962 } else if (!strcmp(left
, "packet.header")) {
1963 struct declaration
*declaration
;
1965 if (trace
->packet_header_decl
) {
1966 fprintf(fd
, "[error] %s: packet.header already declared in trace declaration\n", __func__
);
1970 declaration
= ctf_type_specifier_list_visit(fd
, depth
,
1971 _cds_list_first_entry(&node
->u
.ctf_expression
.right
,
1972 struct ctf_node
, siblings
),
1973 trace
->declaration_scope
, trace
);
1978 if (declaration
->id
!= CTF_TYPE_STRUCT
) {
1982 trace
->packet_header_decl
= container_of(declaration
, struct declaration_struct
, p
);
1984 fprintf(fd
, "[error] %s: attribute \"%s\" is unknown in trace declaration.\n", __func__
, left
);
1995 /* TODO: declaration specifier should be added. */
2002 int ctf_trace_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_trace
*trace
)
2004 struct definition_scope
*parent_def_scope
;
2006 struct ctf_node
*iter
;
2008 if (trace
->declaration_scope
)
2010 trace
->declaration_scope
= new_declaration_scope(trace
->root_declaration_scope
);
2011 trace
->streams
= g_ptr_array_new();
2012 cds_list_for_each_entry(iter
, &node
->u
.trace
.declaration_list
, siblings
) {
2013 ret
= ctf_trace_declaration_visit(fd
, depth
+ 1, iter
, trace
);
2017 if (!CTF_TRACE_FIELD_IS_SET(trace
, major
)) {
2019 fprintf(fd
, "[error] %s: missing major field in trace declaration\n", __func__
);
2022 if (!CTF_TRACE_FIELD_IS_SET(trace
, minor
)) {
2024 fprintf(fd
, "[error] %s: missing minor field in trace declaration\n", __func__
);
2027 if (!CTF_TRACE_FIELD_IS_SET(trace
, uuid
)) {
2029 fprintf(fd
, "[error] %s: missing uuid field in trace declaration\n", __func__
);
2032 if (!CTF_TRACE_FIELD_IS_SET(trace
, byte_order
)) {
2034 fprintf(fd
, "[error] %s: missing byte_order field in trace declaration\n", __func__
);
2038 parent_def_scope
= NULL
;
2039 if (trace
->packet_header_decl
) {
2040 struct definition
*definition
=
2041 trace
->packet_header_decl
->p
.definition_new(&trace
->packet_header_decl
->p
,
2042 parent_def_scope
, 0, 0, "trace.packet.header");
2047 trace
->packet_header
=
2048 container_of(definition
, struct definition_struct
, p
);
2049 parent_def_scope
= trace
->packet_header
->p
.scope
;
2051 trace
->definition_scope
= parent_def_scope
;
2053 if (!CTF_TRACE_FIELD_IS_SET(trace
, byte_order
)) {
2054 /* check that the packet header contains a "magic" field */
2055 if (!trace
->packet_header
2056 || struct_declaration_lookup_field_index(trace
->packet_header_decl
, g_quark_from_static_string("magic")) < 0) {
2058 fprintf(fd
, "[error] %s: missing both byte_order and packet header magic number in trace declaration\n", __func__
);
2065 if (trace
->packet_header
)
2066 definition_unref(&trace
->packet_header
->p
);
2067 if (trace
->packet_header_decl
)
2068 declaration_unref(&trace
->packet_header_decl
->p
);
2069 g_ptr_array_free(trace
->streams
, TRUE
);
2070 free_declaration_scope(trace
->declaration_scope
);
2075 int ctf_root_declaration_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_trace
*trace
)
2079 switch (node
->type
) {
2081 ret
= ctf_typedef_visit(fd
, depth
+ 1,
2082 trace
->root_declaration_scope
,
2083 node
->u
._typedef
.type_specifier_list
,
2084 &node
->u
._typedef
.type_declarators
,
2089 case NODE_TYPEALIAS
:
2090 ret
= ctf_typealias_visit(fd
, depth
+ 1,
2091 trace
->root_declaration_scope
,
2092 node
->u
.typealias
.target
, node
->u
.typealias
.alias
,
2097 case NODE_TYPE_SPECIFIER_LIST
:
2099 struct declaration
*declaration
;
2102 * Just add the type specifier to the root scope
2103 * declaration scope. Release local reference.
2105 declaration
= ctf_type_specifier_list_visit(fd
, depth
+ 1,
2106 node
, trace
->root_declaration_scope
, trace
);
2109 declaration_unref(declaration
);
2119 int ctf_visitor_construct_metadata(FILE *fd
, int depth
, struct ctf_node
*node
,
2120 struct ctf_trace
*trace
, int byte_order
)
2123 struct ctf_node
*iter
;
2125 printf_verbose("CTF visitor: metadata construction... ");
2126 trace
->root_declaration_scope
= new_declaration_scope(NULL
);
2127 trace
->byte_order
= byte_order
;
2129 switch (node
->type
) {
2131 cds_list_for_each_entry(iter
, &node
->u
.root
.declaration_list
,
2133 ret
= ctf_root_declaration_visit(fd
, depth
+ 1, iter
, trace
);
2135 fprintf(fd
, "[error] %s: root declaration error\n", __func__
);
2139 cds_list_for_each_entry(iter
, &node
->u
.root
.trace
, siblings
) {
2140 ret
= ctf_trace_visit(fd
, depth
+ 1, iter
, trace
);
2142 fprintf(fd
, "[error] %s: trace declaration error\n", __func__
);
2146 if (!trace
->streams
) {
2147 fprintf(fd
, "[error] %s: missing trace declaration\n", __func__
);
2151 cds_list_for_each_entry(iter
, &node
->u
.root
.stream
, siblings
) {
2152 ret
= ctf_stream_visit(fd
, depth
+ 1, iter
,
2153 trace
->root_declaration_scope
, trace
);
2155 fprintf(fd
, "[error] %s: stream declaration error\n", __func__
);
2159 cds_list_for_each_entry(iter
, &node
->u
.root
.event
, siblings
) {
2160 ret
= ctf_event_visit(fd
, depth
+ 1, iter
,
2161 trace
->root_declaration_scope
, trace
);
2163 fprintf(fd
, "[error] %s: event declaration error\n", __func__
);
2170 fprintf(fd
, "[error] %s: unknown node type %d\n", __func__
,
2175 printf_verbose("done.\n");
2179 free_declaration_scope(trace
->root_declaration_scope
);