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-internal.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_signed(struct cds_list_head
*head
, int64_t *value
)
110 struct ctf_node
*node
;
113 cds_list_for_each_entry(node
, head
, siblings
) {
114 assert(node
->type
== NODE_UNARY_EXPRESSION
);
115 assert(node
->u
.unary_expression
.type
== UNARY_UNSIGNED_CONSTANT
);
116 assert(node
->u
.unary_expression
.link
== UNARY_LINK_UNKNOWN
);
118 *value
= node
->u
.unary_expression
.u
.signed_constant
;
125 int get_unary_uuid(struct cds_list_head
*head
, uuid_t
*uuid
)
127 struct ctf_node
*node
;
131 cds_list_for_each_entry(node
, head
, siblings
) {
132 const char *src_string
;
134 assert(node
->type
== NODE_UNARY_EXPRESSION
);
135 assert(node
->u
.unary_expression
.type
== UNARY_STRING
);
136 assert(node
->u
.unary_expression
.link
== UNARY_LINK_UNKNOWN
);
138 src_string
= node
->u
.unary_expression
.u
.string
;
139 ret
= uuid_parse(src_string
, *uuid
);
145 struct ctf_stream_class
*trace_stream_lookup(struct ctf_trace
*trace
, uint64_t stream_id
)
147 if (trace
->streams
->len
<= stream_id
)
149 return g_ptr_array_index(trace
->streams
, stream_id
);
153 int visit_type_specifier(FILE *fd
, struct ctf_node
*type_specifier
, GString
*str
)
155 assert(type_specifier
->type
== NODE_TYPE_SPECIFIER
);
157 switch (type_specifier
->u
.type_specifier
.type
) {
159 g_string_append(str
, "void");
162 g_string_append(str
, "char");
165 g_string_append(str
, "short");
168 g_string_append(str
, "int");
171 g_string_append(str
, "long");
174 g_string_append(str
, "float");
176 case TYPESPEC_DOUBLE
:
177 g_string_append(str
, "double");
179 case TYPESPEC_SIGNED
:
180 g_string_append(str
, "signed");
182 case TYPESPEC_UNSIGNED
:
183 g_string_append(str
, "unsigned");
186 g_string_append(str
, "bool");
188 case TYPESPEC_COMPLEX
:
189 g_string_append(str
, "_Complex");
191 case TYPESPEC_IMAGINARY
:
192 g_string_append(str
, "_Imaginary");
195 g_string_append(str
, "const");
197 case TYPESPEC_ID_TYPE
:
198 if (type_specifier
->u
.type_specifier
.id_type
)
199 g_string_append(str
, type_specifier
->u
.type_specifier
.id_type
);
201 case TYPESPEC_STRUCT
:
203 struct ctf_node
*node
= type_specifier
->u
.type_specifier
.node
;
205 if (!node
->u
._struct
.name
) {
206 fprintf(fd
, "[error] %s: unexpected empty variant name\n", __func__
);
209 g_string_append(str
, "struct ");
210 g_string_append(str
, node
->u
._struct
.name
);
213 case TYPESPEC_VARIANT
:
215 struct ctf_node
*node
= type_specifier
->u
.type_specifier
.node
;
217 if (!node
->u
.variant
.name
) {
218 fprintf(fd
, "[error] %s: unexpected empty variant name\n", __func__
);
221 g_string_append(str
, "variant ");
222 g_string_append(str
, node
->u
.variant
.name
);
227 struct ctf_node
*node
= type_specifier
->u
.type_specifier
.node
;
229 if (!node
->u
._enum
.enum_id
) {
230 fprintf(fd
, "[error] %s: unexpected empty enum ID\n", __func__
);
233 g_string_append(str
, "enum ");
234 g_string_append(str
, node
->u
._enum
.enum_id
);
237 case TYPESPEC_FLOATING_POINT
:
238 case TYPESPEC_INTEGER
:
239 case TYPESPEC_STRING
:
241 fprintf(fd
, "[error] %s: unknown specifier\n", __func__
);
248 int visit_type_specifier_list(FILE *fd
, struct ctf_node
*type_specifier_list
, GString
*str
)
250 struct ctf_node
*iter
;
251 int alias_item_nr
= 0;
254 cds_list_for_each_entry(iter
, &type_specifier_list
->u
.type_specifier_list
.head
, siblings
) {
255 if (alias_item_nr
!= 0)
256 g_string_append(str
, " ");
258 ret
= visit_type_specifier(fd
, iter
, str
);
266 GQuark
create_typealias_identifier(FILE *fd
, int depth
,
267 struct ctf_node
*type_specifier_list
,
268 struct ctf_node
*node_type_declarator
)
270 struct ctf_node
*iter
;
276 str
= g_string_new("");
277 ret
= visit_type_specifier_list(fd
, type_specifier_list
, str
);
279 g_string_free(str
, TRUE
);
282 cds_list_for_each_entry(iter
, &node_type_declarator
->u
.type_declarator
.pointers
, siblings
) {
283 g_string_append(str
, " *");
284 if (iter
->u
.pointer
.const_qualifier
)
285 g_string_append(str
, " const");
287 str_c
= g_string_free(str
, FALSE
);
288 alias_q
= g_quark_from_string(str_c
);
294 struct declaration
*ctf_type_declarator_visit(FILE *fd
, int depth
,
295 struct ctf_node
*type_specifier_list
,
297 struct ctf_node
*node_type_declarator
,
298 struct declaration_scope
*declaration_scope
,
299 struct declaration
*nested_declaration
,
300 struct ctf_trace
*trace
)
303 * Visit type declarator by first taking care of sequence/array
304 * (recursively). Then, when we get to the identifier, take care
308 if (node_type_declarator
) {
309 assert(node_type_declarator
->u
.type_declarator
.type
!= TYPEDEC_UNKNOWN
);
311 /* TODO: gcc bitfields not supported yet. */
312 if (node_type_declarator
->u
.type_declarator
.bitfield_len
!= NULL
) {
313 fprintf(fd
, "[error] %s: gcc bitfields are not supported yet.\n", __func__
);
318 if (!nested_declaration
) {
319 if (node_type_declarator
&& !cds_list_empty(&node_type_declarator
->u
.type_declarator
.pointers
)) {
323 * If we have a pointer declarator, it _has_ to be present in
324 * the typealiases (else fail).
326 alias_q
= create_typealias_identifier(fd
, depth
,
327 type_specifier_list
, node_type_declarator
);
328 nested_declaration
= lookup_declaration(alias_q
, declaration_scope
);
329 if (!nested_declaration
) {
330 fprintf(fd
, "[error] %s: cannot find typealias \"%s\".\n", __func__
, g_quark_to_string(alias_q
));
333 if (nested_declaration
->id
== CTF_TYPE_INTEGER
) {
334 struct declaration_integer
*integer_declaration
=
335 container_of(nested_declaration
, struct declaration_integer
, p
);
336 /* For base to 16 for pointers (expected pretty-print) */
337 if (!integer_declaration
->base
) {
339 * We need to do a copy of the
340 * integer declaration to modify it. There could be other references to
343 integer_declaration
= integer_declaration_new(integer_declaration
->len
,
344 integer_declaration
->byte_order
, integer_declaration
->signedness
,
345 integer_declaration
->p
.alignment
, 16, integer_declaration
->encoding
);
346 nested_declaration
= &integer_declaration
->p
;
350 nested_declaration
= ctf_type_specifier_list_visit(fd
, depth
,
351 type_specifier_list
, declaration_scope
, trace
);
355 if (!node_type_declarator
)
356 return nested_declaration
;
358 if (node_type_declarator
->u
.type_declarator
.type
== TYPEDEC_ID
) {
359 if (node_type_declarator
->u
.type_declarator
.u
.id
)
360 *field_name
= g_quark_from_string(node_type_declarator
->u
.type_declarator
.u
.id
);
363 return nested_declaration
;
365 struct declaration
*declaration
;
366 struct ctf_node
*first
;
370 if (!nested_declaration
) {
371 fprintf(fd
, "[error] %s: nested type is unknown.\n", __func__
);
375 /* create array/sequence, pass nested_declaration as child. */
376 if (cds_list_empty(&node_type_declarator
->u
.type_declarator
.u
.nested
.length
)) {
377 fprintf(fd
, "[error] %s: expecting length field reference or value.\n", __func__
);
380 first
= _cds_list_first_entry(&node_type_declarator
->u
.type_declarator
.u
.nested
.length
,
381 struct ctf_node
, siblings
);
382 assert(first
->type
== NODE_UNARY_EXPRESSION
);
384 switch (first
->u
.unary_expression
.type
) {
385 case UNARY_UNSIGNED_CONSTANT
:
387 struct declaration_array
*array_declaration
;
390 len
= first
->u
.unary_expression
.u
.unsigned_constant
;
391 array_declaration
= array_declaration_new(len
, nested_declaration
,
394 if (!array_declaration
) {
395 fprintf(fd
, "[error] %s: cannot create array declaration.\n", __func__
);
398 declaration
= &array_declaration
->p
;
403 /* Lookup unsigned integer definition, create sequence */
404 char *length_name
= concatenate_unary_strings(&node_type_declarator
->u
.type_declarator
.u
.nested
.length
);
405 struct declaration_sequence
*sequence_declaration
;
407 sequence_declaration
= sequence_declaration_new(length_name
, nested_declaration
, declaration_scope
);
408 if (!sequence_declaration
) {
409 fprintf(fd
, "[error] %s: cannot create sequence declaration.\n", __func__
);
412 declaration
= &sequence_declaration
->p
;
419 /* Pass it as content of outer container */
420 declaration
= ctf_type_declarator_visit(fd
, depth
,
421 type_specifier_list
, field_name
,
422 node_type_declarator
->u
.type_declarator
.u
.nested
.type_declarator
,
423 declaration_scope
, declaration
, trace
);
429 int ctf_struct_type_declarators_visit(FILE *fd
, int depth
,
430 struct declaration_struct
*struct_declaration
,
431 struct ctf_node
*type_specifier_list
,
432 struct cds_list_head
*type_declarators
,
433 struct declaration_scope
*declaration_scope
,
434 struct ctf_trace
*trace
)
436 struct ctf_node
*iter
;
439 cds_list_for_each_entry(iter
, type_declarators
, siblings
) {
440 struct declaration
*field_declaration
;
442 field_declaration
= ctf_type_declarator_visit(fd
, depth
,
445 struct_declaration
->scope
,
447 if (!field_declaration
) {
448 fprintf(fd
, "[error] %s: unable to find struct field declaration type\n", __func__
);
452 /* Check if field with same name already exists */
453 if (struct_declaration_lookup_field_index(struct_declaration
, field_name
) >= 0) {
454 fprintf(fd
, "[error] %s: duplicate field %s in struct\n", __func__
, g_quark_to_string(field_name
));
458 struct_declaration_add_field(struct_declaration
,
459 g_quark_to_string(field_name
),
466 int ctf_variant_type_declarators_visit(FILE *fd
, int depth
,
467 struct declaration_untagged_variant
*untagged_variant_declaration
,
468 struct ctf_node
*type_specifier_list
,
469 struct cds_list_head
*type_declarators
,
470 struct declaration_scope
*declaration_scope
,
471 struct ctf_trace
*trace
)
473 struct ctf_node
*iter
;
476 cds_list_for_each_entry(iter
, type_declarators
, siblings
) {
477 struct declaration
*field_declaration
;
479 field_declaration
= ctf_type_declarator_visit(fd
, depth
,
482 untagged_variant_declaration
->scope
,
484 if (!field_declaration
) {
485 fprintf(fd
, "[error] %s: unable to find variant field declaration type\n", __func__
);
489 if (untagged_variant_declaration_get_field_from_tag(untagged_variant_declaration
, field_name
) != NULL
) {
490 fprintf(fd
, "[error] %s: duplicate field %s in variant\n", __func__
, g_quark_to_string(field_name
));
495 untagged_variant_declaration_add_field(untagged_variant_declaration
,
496 g_quark_to_string(field_name
),
503 int ctf_typedef_visit(FILE *fd
, int depth
, struct declaration_scope
*scope
,
504 struct ctf_node
*type_specifier_list
,
505 struct cds_list_head
*type_declarators
,
506 struct ctf_trace
*trace
)
508 struct ctf_node
*iter
;
511 cds_list_for_each_entry(iter
, type_declarators
, siblings
) {
512 struct declaration
*type_declaration
;
515 type_declaration
= ctf_type_declarator_visit(fd
, depth
,
519 if (!type_declaration
) {
520 fprintf(fd
, "[error] %s: problem creating type declaration\n", __func__
);
524 * Don't allow typedef and typealias of untagged
527 if (type_declaration
->id
== CTF_TYPE_UNTAGGED_VARIANT
) {
528 fprintf(fd
, "[error] %s: typedef of untagged variant is not permitted.\n", __func__
);
529 declaration_unref(type_declaration
);
532 ret
= register_declaration(identifier
, type_declaration
, scope
);
534 type_declaration
->declaration_free(type_declaration
);
542 int ctf_typealias_visit(FILE *fd
, int depth
, struct declaration_scope
*scope
,
543 struct ctf_node
*target
, struct ctf_node
*alias
,
544 struct ctf_trace
*trace
)
546 struct declaration
*type_declaration
;
547 struct ctf_node
*node
;
552 /* See ctf_visitor_type_declarator() in the semantic validator. */
555 * Create target type declaration.
558 if (cds_list_empty(&target
->u
.typealias_target
.type_declarators
))
561 node
= _cds_list_first_entry(&target
->u
.typealias_target
.type_declarators
,
562 struct ctf_node
, siblings
);
563 type_declaration
= ctf_type_declarator_visit(fd
, depth
,
564 target
->u
.typealias_target
.type_specifier_list
,
567 if (!type_declaration
) {
568 fprintf(fd
, "[error] %s: problem creating type declaration\n", __func__
);
573 * Don't allow typedef and typealias of untagged
576 if (type_declaration
->id
== CTF_TYPE_UNTAGGED_VARIANT
) {
577 fprintf(fd
, "[error] %s: typedef of untagged variant is not permitted.\n", __func__
);
578 declaration_unref(type_declaration
);
582 * The semantic validator does not check whether the target is
583 * abstract or not (if it has an identifier). Check it here.
586 fprintf(fd
, "[error] %s: expecting empty identifier\n", __func__
);
591 * Create alias identifier.
594 node
= _cds_list_first_entry(&alias
->u
.typealias_alias
.type_declarators
,
595 struct ctf_node
, siblings
);
596 alias_q
= create_typealias_identifier(fd
, depth
,
597 alias
->u
.typealias_alias
.type_specifier_list
, node
);
598 err
= register_declaration(alias_q
, type_declaration
, scope
);
604 if (type_declaration
) {
605 type_declaration
->declaration_free(type_declaration
);
611 int ctf_struct_declaration_list_visit(FILE *fd
, int depth
,
612 struct ctf_node
*iter
, struct declaration_struct
*struct_declaration
,
613 struct ctf_trace
*trace
)
617 switch (iter
->type
) {
619 /* For each declarator, declare type and add type to struct declaration scope */
620 ret
= ctf_typedef_visit(fd
, depth
,
621 struct_declaration
->scope
,
622 iter
->u
._typedef
.type_specifier_list
,
623 &iter
->u
._typedef
.type_declarators
, trace
);
628 /* Declare type with declarator and add type to struct declaration scope */
629 ret
= ctf_typealias_visit(fd
, depth
,
630 struct_declaration
->scope
,
631 iter
->u
.typealias
.target
,
632 iter
->u
.typealias
.alias
, trace
);
636 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
637 /* Add field to structure declaration */
638 ret
= ctf_struct_type_declarators_visit(fd
, depth
,
640 iter
->u
.struct_or_variant_declaration
.type_specifier_list
,
641 &iter
->u
.struct_or_variant_declaration
.type_declarators
,
642 struct_declaration
->scope
, trace
);
647 fprintf(fd
, "[error] %s: unexpected node type %d\n", __func__
, (int) iter
->type
);
654 int ctf_variant_declaration_list_visit(FILE *fd
, int depth
,
655 struct ctf_node
*iter
,
656 struct declaration_untagged_variant
*untagged_variant_declaration
,
657 struct ctf_trace
*trace
)
661 switch (iter
->type
) {
663 /* For each declarator, declare type and add type to variant declaration scope */
664 ret
= ctf_typedef_visit(fd
, depth
,
665 untagged_variant_declaration
->scope
,
666 iter
->u
._typedef
.type_specifier_list
,
667 &iter
->u
._typedef
.type_declarators
, trace
);
672 /* Declare type with declarator and add type to variant declaration scope */
673 ret
= ctf_typealias_visit(fd
, depth
,
674 untagged_variant_declaration
->scope
,
675 iter
->u
.typealias
.target
,
676 iter
->u
.typealias
.alias
, trace
);
680 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
681 /* Add field to structure declaration */
682 ret
= ctf_variant_type_declarators_visit(fd
, depth
,
683 untagged_variant_declaration
,
684 iter
->u
.struct_or_variant_declaration
.type_specifier_list
,
685 &iter
->u
.struct_or_variant_declaration
.type_declarators
,
686 untagged_variant_declaration
->scope
, trace
);
691 fprintf(fd
, "[error] %s: unexpected node type %d\n", __func__
, (int) iter
->type
);
698 struct declaration
*ctf_declaration_struct_visit(FILE *fd
,
699 int depth
, const char *name
, struct cds_list_head
*declaration_list
,
700 int has_body
, struct cds_list_head
*min_align
,
701 struct declaration_scope
*declaration_scope
,
702 struct ctf_trace
*trace
)
704 struct declaration_struct
*struct_declaration
;
705 struct ctf_node
*iter
;
709 * For named struct (without body), lookup in
710 * declaration scope. Don't take reference on struct
711 * declaration: ref is only taken upon definition.
716 lookup_struct_declaration(g_quark_from_string(name
),
718 return &struct_declaration
->p
;
720 uint64_t min_align_value
= 0;
722 /* For unnamed struct, create type */
723 /* For named struct (with body), create type and add to declaration scope */
725 if (lookup_struct_declaration(g_quark_from_string(name
),
726 declaration_scope
)) {
728 fprintf(fd
, "[error] %s: struct %s already declared in scope\n", __func__
, name
);
732 if (!cds_list_empty(min_align
)) {
733 ret
= get_unary_unsigned(min_align
, &min_align_value
);
735 fprintf(fd
, "[error] %s: unexpected unary expression for structure \"align\" attribute\n", __func__
);
740 struct_declaration
= struct_declaration_new(declaration_scope
,
742 cds_list_for_each_entry(iter
, declaration_list
, siblings
) {
743 ret
= ctf_struct_declaration_list_visit(fd
, depth
+ 1, iter
,
744 struct_declaration
, trace
);
746 goto error_free_declaration
;
749 ret
= register_struct_declaration(g_quark_from_string(name
),
754 return &struct_declaration
->p
;
756 error_free_declaration
:
757 struct_declaration
->p
.declaration_free(&struct_declaration
->p
);
763 struct declaration
*ctf_declaration_variant_visit(FILE *fd
,
764 int depth
, const char *name
, const char *choice
,
765 struct cds_list_head
*declaration_list
,
766 int has_body
, struct declaration_scope
*declaration_scope
,
767 struct ctf_trace
*trace
)
769 struct declaration_untagged_variant
*untagged_variant_declaration
;
770 struct declaration_variant
*variant_declaration
;
771 struct ctf_node
*iter
;
775 * For named variant (without body), lookup in
776 * declaration scope. Don't take reference on variant
777 * declaration: ref is only taken upon definition.
781 untagged_variant_declaration
=
782 lookup_variant_declaration(g_quark_from_string(name
),
785 /* For unnamed variant, create type */
786 /* For named variant (with body), create type and add to declaration scope */
788 if (lookup_variant_declaration(g_quark_from_string(name
),
789 declaration_scope
)) {
791 fprintf(fd
, "[error] %s: variant %s already declared in scope\n", __func__
, name
);
795 untagged_variant_declaration
= untagged_variant_declaration_new(declaration_scope
);
796 cds_list_for_each_entry(iter
, declaration_list
, siblings
) {
797 ret
= ctf_variant_declaration_list_visit(fd
, depth
+ 1, iter
,
798 untagged_variant_declaration
, trace
);
803 ret
= register_variant_declaration(g_quark_from_string(name
),
804 untagged_variant_declaration
,
810 * if tagged, create tagged variant and return. else return
814 return &untagged_variant_declaration
->p
;
816 variant_declaration
= variant_declaration_new(untagged_variant_declaration
, choice
);
817 if (!variant_declaration
)
819 declaration_unref(&untagged_variant_declaration
->p
);
820 return &variant_declaration
->p
;
823 untagged_variant_declaration
->p
.declaration_free(&untagged_variant_declaration
->p
);
828 int ctf_enumerator_list_visit(FILE *fd
, int depth
,
829 struct ctf_node
*enumerator
,
830 struct declaration_enum
*enum_declaration
)
833 struct ctf_node
*iter
;
835 q
= g_quark_from_string(enumerator
->u
.enumerator
.id
);
836 if (enum_declaration
->integer_declaration
->signedness
) {
840 cds_list_for_each_entry(iter
, &enumerator
->u
.enumerator
.values
, siblings
) {
843 assert(iter
->type
== NODE_UNARY_EXPRESSION
);
849 switch (iter
->u
.unary_expression
.type
) {
850 case UNARY_SIGNED_CONSTANT
:
851 *target
= iter
->u
.unary_expression
.u
.signed_constant
;
853 case UNARY_UNSIGNED_CONSTANT
:
854 *target
= iter
->u
.unary_expression
.u
.unsigned_constant
;
857 fprintf(fd
, "[error] %s: invalid enumerator\n", __func__
);
861 fprintf(fd
, "[error] %s: invalid enumerator\n", __func__
);
868 enum_signed_insert(enum_declaration
, start
, end
, q
);
873 cds_list_for_each_entry(iter
, &enumerator
->u
.enumerator
.values
, siblings
) {
876 assert(iter
->type
== NODE_UNARY_EXPRESSION
);
882 switch (iter
->u
.unary_expression
.type
) {
883 case UNARY_UNSIGNED_CONSTANT
:
884 *target
= iter
->u
.unary_expression
.u
.unsigned_constant
;
886 case UNARY_SIGNED_CONSTANT
:
888 * We don't accept signed constants for enums with unsigned
891 fprintf(fd
, "[error] %s: invalid enumerator (signed constant encountered, but enum container type is unsigned)\n", __func__
);
894 fprintf(fd
, "[error] %s: invalid enumerator\n", __func__
);
898 fprintf(fd
, "[error] %s: invalid enumerator\n", __func__
);
905 enum_unsigned_insert(enum_declaration
, start
, end
, q
);
911 struct declaration
*ctf_declaration_enum_visit(FILE *fd
, int depth
,
913 struct ctf_node
*container_type
,
914 struct cds_list_head
*enumerator_list
,
916 struct declaration_scope
*declaration_scope
,
917 struct ctf_trace
*trace
)
919 struct declaration
*declaration
;
920 struct declaration_enum
*enum_declaration
;
921 struct declaration_integer
*integer_declaration
;
922 struct ctf_node
*iter
;
927 * For named enum (without body), lookup in
928 * declaration scope. Don't take reference on enum
929 * declaration: ref is only taken upon definition.
934 lookup_enum_declaration(g_quark_from_string(name
),
936 return &enum_declaration
->p
;
938 /* For unnamed enum, create type */
939 /* For named enum (with body), create type and add to declaration scope */
941 if (lookup_enum_declaration(g_quark_from_string(name
),
942 declaration_scope
)) {
944 fprintf(fd
, "[error] %s: enum %s already declared in scope\n", __func__
, name
);
948 if (!container_type
) {
949 declaration
= lookup_declaration(g_quark_from_static_string("int"),
952 fprintf(fd
, "[error] %s: \"int\" type declaration missing for enumeration\n", __func__
);
956 declaration
= ctf_type_declarator_visit(fd
, depth
,
963 fprintf(fd
, "[error] %s: unable to create container type for enumeration\n", __func__
);
966 if (declaration
->id
!= CTF_TYPE_INTEGER
) {
967 fprintf(fd
, "[error] %s: container type for enumeration is not integer\n", __func__
);
970 integer_declaration
= container_of(declaration
, struct declaration_integer
, p
);
971 enum_declaration
= enum_declaration_new(integer_declaration
);
972 declaration_unref(&integer_declaration
->p
); /* leave ref to enum */
973 cds_list_for_each_entry(iter
, enumerator_list
, siblings
) {
974 ret
= ctf_enumerator_list_visit(fd
, depth
+ 1, iter
, enum_declaration
);
979 ret
= register_enum_declaration(g_quark_from_string(name
),
984 return &enum_declaration
->p
;
987 enum_declaration
->p
.declaration_free(&enum_declaration
->p
);
992 struct declaration
*ctf_declaration_type_specifier_visit(FILE *fd
, int depth
,
993 struct ctf_node
*type_specifier_list
,
994 struct declaration_scope
*declaration_scope
)
997 struct declaration
*declaration
;
1002 str
= g_string_new("");
1003 ret
= visit_type_specifier_list(fd
, type_specifier_list
, str
);
1006 str_c
= g_string_free(str
, FALSE
);
1007 id_q
= g_quark_from_string(str_c
);
1009 declaration
= lookup_declaration(id_q
, declaration_scope
);
1014 * Returns 0/1 boolean, or < 0 on error.
1017 int get_boolean(FILE *fd
, int depth
, struct ctf_node
*unary_expression
)
1019 if (unary_expression
->type
!= NODE_UNARY_EXPRESSION
) {
1020 fprintf(fd
, "[error] %s: expecting unary expression\n",
1024 switch (unary_expression
->u
.unary_expression
.type
) {
1025 case UNARY_UNSIGNED_CONSTANT
:
1026 if (unary_expression
->u
.unary_expression
.u
.unsigned_constant
== 0)
1030 case UNARY_SIGNED_CONSTANT
:
1031 if (unary_expression
->u
.unary_expression
.u
.signed_constant
== 0)
1036 if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "true"))
1038 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "TRUE"))
1040 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "false"))
1042 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "FALSE"))
1045 fprintf(fd
, "[error] %s: unexpected string \"%s\"\n",
1046 __func__
, unary_expression
->u
.unary_expression
.u
.string
);
1051 fprintf(fd
, "[error] %s: unexpected unary expression type\n",
1059 int get_trace_byte_order(FILE *fd
, int depth
, struct ctf_node
*unary_expression
)
1063 if (unary_expression
->u
.unary_expression
.type
!= UNARY_STRING
) {
1064 fprintf(fd
, "[error] %s: byte_order: expecting string\n",
1068 if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "be"))
1069 byte_order
= BIG_ENDIAN
;
1070 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "le"))
1071 byte_order
= LITTLE_ENDIAN
;
1073 fprintf(fd
, "[error] %s: unexpected string \"%s\". Should be \"native\", \"network\", \"be\" or \"le\".\n",
1074 __func__
, unary_expression
->u
.unary_expression
.u
.string
);
1081 int get_byte_order(FILE *fd
, int depth
, struct ctf_node
*unary_expression
,
1082 struct ctf_trace
*trace
)
1086 if (unary_expression
->u
.unary_expression
.type
!= UNARY_STRING
) {
1087 fprintf(fd
, "[error] %s: byte_order: expecting string\n",
1091 if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "native"))
1092 byte_order
= trace
->byte_order
;
1093 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "network"))
1094 byte_order
= BIG_ENDIAN
;
1095 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "be"))
1096 byte_order
= BIG_ENDIAN
;
1097 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "le"))
1098 byte_order
= LITTLE_ENDIAN
;
1100 fprintf(fd
, "[error] %s: unexpected string \"%s\". Should be \"native\", \"network\", \"be\" or \"le\".\n",
1101 __func__
, unary_expression
->u
.unary_expression
.u
.string
);
1108 struct declaration
*ctf_declaration_integer_visit(FILE *fd
, int depth
,
1109 struct cds_list_head
*expressions
,
1110 struct ctf_trace
*trace
)
1112 struct ctf_node
*expression
;
1113 uint64_t alignment
= 1, size
= 0;
1114 int byte_order
= trace
->byte_order
;
1116 int has_alignment
= 0, has_size
= 0;
1118 enum ctf_string_encoding encoding
= CTF_STRING_NONE
;
1119 struct declaration_integer
*integer_declaration
;
1121 cds_list_for_each_entry(expression
, expressions
, siblings
) {
1122 struct ctf_node
*left
, *right
;
1124 left
= _cds_list_first_entry(&expression
->u
.ctf_expression
.left
, struct ctf_node
, siblings
);
1125 right
= _cds_list_first_entry(&expression
->u
.ctf_expression
.right
, struct ctf_node
, siblings
);
1126 assert(left
->u
.unary_expression
.type
== UNARY_STRING
);
1127 if (!strcmp(left
->u
.unary_expression
.u
.string
, "signed")) {
1128 signedness
= get_boolean(fd
, depth
, right
);
1131 } else 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
, "size")) {
1136 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
1137 fprintf(fd
, "[error] %s: size: expecting unsigned constant\n",
1141 size
= right
->u
.unary_expression
.u
.unsigned_constant
;
1143 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "align")) {
1144 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
1145 fprintf(fd
, "[error] %s: align: expecting unsigned constant\n",
1149 alignment
= right
->u
.unary_expression
.u
.unsigned_constant
;
1150 /* Make sure alignment is a power of two */
1151 if (alignment
== 0 || (alignment
& (alignment
- 1)) != 0) {
1152 fprintf(fd
, "[error] %s: align: expecting power of two\n",
1157 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "base")) {
1158 switch (right
->u
.unary_expression
.type
) {
1159 case UNARY_UNSIGNED_CONSTANT
:
1160 switch (right
->u
.unary_expression
.u
.unsigned_constant
) {
1165 base
= right
->u
.unary_expression
.u
.unsigned_constant
;
1168 fprintf(fd
, "[error] %s: base not supported (%" PRIu64
")\n",
1169 __func__
, right
->u
.unary_expression
.u
.unsigned_constant
);
1175 char *s_right
= concatenate_unary_strings(&expression
->u
.ctf_expression
.right
);
1177 fprintf(fd
, "[error] %s: unexpected unary expression for integer base\n", __func__
);
1181 if (!strcmp(s_right
, "decimal") || !strcmp(s_right
, "dec") || !strcmp(s_right
, "d")
1182 || !strcmp(s_right
, "i") || !strcmp(s_right
, "u")) {
1184 } else if (!strcmp(s_right
, "hexadecimal") || !strcmp(s_right
, "hex")
1185 || !strcmp(s_right
, "x") || !strcmp(s_right
, "X")
1186 || !strcmp(s_right
, "p")) {
1188 } else if (!strcmp(s_right
, "octal") || !strcmp(s_right
, "oct")
1189 || !strcmp(s_right
, "o")) {
1191 } else if (!strcmp(s_right
, "binary") || !strcmp(s_right
, "b")) {
1194 fprintf(fd
, "[error] %s: unexpected expression for integer base (%s)\n", __func__
, s_right
);
1203 fprintf(fd
, "[error] %s: base: expecting unsigned constant or unary string\n",
1207 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "encoding")) {
1210 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
1211 fprintf(fd
, "[error] %s: encoding: expecting unary string\n",
1215 s_right
= concatenate_unary_strings(&expression
->u
.ctf_expression
.right
);
1217 fprintf(fd
, "[error] %s: unexpected unary expression for integer base\n", __func__
);
1221 if (!strcmp(s_right
, "UTF8")
1222 || !strcmp(s_right
, "utf8")
1223 || !strcmp(s_right
, "utf-8")
1224 || !strcmp(s_right
, "UTF-8"))
1225 encoding
= CTF_STRING_UTF8
;
1226 else if (!strcmp(s_right
, "ASCII")
1227 || !strcmp(s_right
, "ascii"))
1228 encoding
= CTF_STRING_ASCII
;
1229 else if (!strcmp(s_right
, "none"))
1230 encoding
= CTF_STRING_NONE
;
1232 fprintf(fd
, "[error] %s: unknown string encoding \"%s\"\n", __func__
, s_right
);
1237 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "map")) {
1240 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
1241 fprintf(fd
, "[error] %s: map: expecting identifier\n",
1245 s_right
= concatenate_unary_strings(&expression
->u
.ctf_expression
.right
);
1247 fprintf(fd
, "[error] %s: unexpected unary expression for integer map\n", __func__
);
1254 fprintf(fd
, "[warning] %s: unknown attribute name %s\n",
1255 __func__
, left
->u
.unary_expression
.u
.string
);
1256 /* Fall-through after warning */
1260 fprintf(fd
, "[error] %s: missing size attribute\n", __func__
);
1263 if (!has_alignment
) {
1264 if (size
% CHAR_BIT
) {
1265 /* bit-packed alignment */
1268 /* byte-packed alignment */
1269 alignment
= CHAR_BIT
;
1272 integer_declaration
= integer_declaration_new(size
,
1273 byte_order
, signedness
, alignment
,
1275 return &integer_declaration
->p
;
1279 struct declaration
*ctf_declaration_floating_point_visit(FILE *fd
, int depth
,
1280 struct cds_list_head
*expressions
,
1281 struct ctf_trace
*trace
)
1283 struct ctf_node
*expression
;
1284 uint64_t alignment
= 1, exp_dig
= 0, mant_dig
= 0,
1285 byte_order
= trace
->byte_order
;
1286 int has_alignment
= 0, has_exp_dig
= 0, has_mant_dig
= 0;
1287 struct declaration_float
*float_declaration
;
1289 cds_list_for_each_entry(expression
, expressions
, siblings
) {
1290 struct ctf_node
*left
, *right
;
1292 left
= _cds_list_first_entry(&expression
->u
.ctf_expression
.left
, struct ctf_node
, siblings
);
1293 right
= _cds_list_first_entry(&expression
->u
.ctf_expression
.right
, struct ctf_node
, siblings
);
1294 assert(left
->u
.unary_expression
.type
== UNARY_STRING
);
1295 if (!strcmp(left
->u
.unary_expression
.u
.string
, "byte_order")) {
1296 byte_order
= get_byte_order(fd
, depth
, right
, trace
);
1299 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "exp_dig")) {
1300 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
1301 fprintf(fd
, "[error] %s: exp_dig: expecting unsigned constant\n",
1305 exp_dig
= right
->u
.unary_expression
.u
.unsigned_constant
;
1307 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "mant_dig")) {
1308 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
1309 fprintf(fd
, "[error] %s: mant_dig: expecting unsigned constant\n",
1313 mant_dig
= right
->u
.unary_expression
.u
.unsigned_constant
;
1315 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "align")) {
1316 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
1317 fprintf(fd
, "[error] %s: align: expecting unsigned constant\n",
1321 alignment
= right
->u
.unary_expression
.u
.unsigned_constant
;
1322 /* Make sure alignment is a power of two */
1323 if (alignment
== 0 || (alignment
& (alignment
- 1)) != 0) {
1324 fprintf(fd
, "[error] %s: align: expecting power of two\n",
1330 fprintf(fd
, "[warning] %s: unknown attribute name %s\n",
1331 __func__
, left
->u
.unary_expression
.u
.string
);
1332 /* Fall-through after warning */
1335 if (!has_mant_dig
) {
1336 fprintf(fd
, "[error] %s: missing mant_dig attribute\n", __func__
);
1340 fprintf(fd
, "[error] %s: missing exp_dig attribute\n", __func__
);
1343 if (!has_alignment
) {
1344 if ((mant_dig
+ exp_dig
) % CHAR_BIT
) {
1345 /* bit-packed alignment */
1348 /* byte-packed alignment */
1349 alignment
= CHAR_BIT
;
1352 float_declaration
= float_declaration_new(mant_dig
, exp_dig
,
1353 byte_order
, alignment
);
1354 return &float_declaration
->p
;
1358 struct declaration
*ctf_declaration_string_visit(FILE *fd
, int depth
,
1359 struct cds_list_head
*expressions
,
1360 struct ctf_trace
*trace
)
1362 struct ctf_node
*expression
;
1363 const char *encoding_c
= NULL
;
1364 enum ctf_string_encoding encoding
= CTF_STRING_UTF8
;
1365 struct declaration_string
*string_declaration
;
1367 cds_list_for_each_entry(expression
, expressions
, siblings
) {
1368 struct ctf_node
*left
, *right
;
1370 left
= _cds_list_first_entry(&expression
->u
.ctf_expression
.left
, struct ctf_node
, siblings
);
1371 right
= _cds_list_first_entry(&expression
->u
.ctf_expression
.right
, struct ctf_node
, siblings
);
1372 assert(left
->u
.unary_expression
.type
== UNARY_STRING
);
1373 if (!strcmp(left
->u
.unary_expression
.u
.string
, "encoding")) {
1374 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
1375 fprintf(fd
, "[error] %s: encoding: expecting string\n",
1379 encoding_c
= right
->u
.unary_expression
.u
.string
;
1381 fprintf(fd
, "[warning] %s: unknown attribute name %s\n",
1382 __func__
, left
->u
.unary_expression
.u
.string
);
1383 /* Fall-through after warning */
1386 if (encoding_c
&& !strcmp(encoding_c
, "ASCII"))
1387 encoding
= CTF_STRING_ASCII
;
1388 string_declaration
= string_declaration_new(encoding
);
1389 return &string_declaration
->p
;
1394 struct declaration
*ctf_type_specifier_list_visit(FILE *fd
,
1395 int depth
, struct ctf_node
*type_specifier_list
,
1396 struct declaration_scope
*declaration_scope
,
1397 struct ctf_trace
*trace
)
1399 struct ctf_node
*first
;
1400 struct ctf_node
*node
;
1402 assert(type_specifier_list
->type
== NODE_TYPE_SPECIFIER_LIST
);
1404 first
= _cds_list_first_entry(&type_specifier_list
->u
.type_specifier_list
.head
, struct ctf_node
, siblings
);
1406 assert(first
->type
== NODE_TYPE_SPECIFIER
);
1408 node
= first
->u
.type_specifier
.node
;
1410 switch (first
->u
.type_specifier
.type
) {
1411 case TYPESPEC_FLOATING_POINT
:
1412 return ctf_declaration_floating_point_visit(fd
, depth
,
1413 &node
->u
.floating_point
.expressions
, trace
);
1414 case TYPESPEC_INTEGER
:
1415 return ctf_declaration_integer_visit(fd
, depth
,
1416 &node
->u
.integer
.expressions
, trace
);
1417 case TYPESPEC_STRING
:
1418 return ctf_declaration_string_visit(fd
, depth
,
1419 &node
->u
.string
.expressions
, trace
);
1420 case TYPESPEC_STRUCT
:
1421 return ctf_declaration_struct_visit(fd
, depth
,
1422 node
->u
._struct
.name
,
1423 &node
->u
._struct
.declaration_list
,
1424 node
->u
._struct
.has_body
,
1425 &node
->u
._struct
.min_align
,
1428 case TYPESPEC_VARIANT
:
1429 return ctf_declaration_variant_visit(fd
, depth
,
1430 node
->u
.variant
.name
,
1431 node
->u
.variant
.choice
,
1432 &node
->u
.variant
.declaration_list
,
1433 node
->u
.variant
.has_body
,
1437 return ctf_declaration_enum_visit(fd
, depth
,
1438 node
->u
._enum
.enum_id
,
1439 node
->u
._enum
.container_type
,
1440 &node
->u
._enum
.enumerator_list
,
1441 node
->u
._enum
.has_body
,
1447 case TYPESPEC_SHORT
:
1450 case TYPESPEC_FLOAT
:
1451 case TYPESPEC_DOUBLE
:
1452 case TYPESPEC_SIGNED
:
1453 case TYPESPEC_UNSIGNED
:
1455 case TYPESPEC_COMPLEX
:
1456 case TYPESPEC_IMAGINARY
:
1457 case TYPESPEC_CONST
:
1458 case TYPESPEC_ID_TYPE
:
1459 return ctf_declaration_type_specifier_visit(fd
, depth
,
1460 type_specifier_list
, declaration_scope
);
1462 fprintf(fd
, "[error] %s: unexpected node type %d\n", __func__
, (int) first
->u
.type_specifier
.type
);
1468 int ctf_event_declaration_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_event
*event
, struct ctf_trace
*trace
)
1472 switch (node
->type
) {
1474 ret
= ctf_typedef_visit(fd
, depth
+ 1,
1475 event
->declaration_scope
,
1476 node
->u
._typedef
.type_specifier_list
,
1477 &node
->u
._typedef
.type_declarators
,
1482 case NODE_TYPEALIAS
:
1483 ret
= ctf_typealias_visit(fd
, depth
+ 1,
1484 event
->declaration_scope
,
1485 node
->u
.typealias
.target
, node
->u
.typealias
.alias
,
1490 case NODE_CTF_EXPRESSION
:
1494 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
1495 if (!strcmp(left
, "name")) {
1498 if (CTF_EVENT_FIELD_IS_SET(event
, name
)) {
1499 fprintf(fd
, "[error] %s: name already declared in event declaration\n", __func__
);
1503 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
1505 fprintf(fd
, "[error] %s: unexpected unary expression for event name\n", __func__
);
1509 event
->name
= g_quark_from_string(right
);
1511 CTF_EVENT_SET_FIELD(event
, name
);
1512 } else if (!strcmp(left
, "id")) {
1513 if (CTF_EVENT_FIELD_IS_SET(event
, id
)) {
1514 fprintf(fd
, "[error] %s: id already declared in event declaration\n", __func__
);
1518 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &event
->id
);
1520 fprintf(fd
, "[error] %s: unexpected unary expression for event id\n", __func__
);
1524 CTF_EVENT_SET_FIELD(event
, id
);
1525 } else if (!strcmp(left
, "stream_id")) {
1526 if (CTF_EVENT_FIELD_IS_SET(event
, stream_id
)) {
1527 fprintf(fd
, "[error] %s: stream_id already declared in event declaration\n", __func__
);
1531 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &event
->stream_id
);
1533 fprintf(fd
, "[error] %s: unexpected unary expression for event stream_id\n", __func__
);
1537 event
->stream
= trace_stream_lookup(trace
, event
->stream_id
);
1538 if (!event
->stream
) {
1539 fprintf(fd
, "[error] %s: stream id %" PRIu64
" cannot be found\n", __func__
, event
->stream_id
);
1543 CTF_EVENT_SET_FIELD(event
, stream_id
);
1544 } else if (!strcmp(left
, "context")) {
1545 struct declaration
*declaration
;
1547 if (event
->context_decl
) {
1548 fprintf(fd
, "[error] %s: context already declared in event declaration\n", __func__
);
1552 declaration
= ctf_type_specifier_list_visit(fd
, depth
,
1553 _cds_list_first_entry(&node
->u
.ctf_expression
.right
,
1554 struct ctf_node
, siblings
),
1555 event
->declaration_scope
, trace
);
1560 if (declaration
->id
!= CTF_TYPE_STRUCT
) {
1564 event
->context_decl
= container_of(declaration
, struct declaration_struct
, p
);
1565 } else if (!strcmp(left
, "fields")) {
1566 struct declaration
*declaration
;
1568 if (event
->fields_decl
) {
1569 fprintf(fd
, "[error] %s: fields already declared in event declaration\n", __func__
);
1573 declaration
= ctf_type_specifier_list_visit(fd
, depth
,
1574 _cds_list_first_entry(&node
->u
.ctf_expression
.right
,
1575 struct ctf_node
, siblings
),
1576 event
->declaration_scope
, trace
);
1581 if (declaration
->id
!= CTF_TYPE_STRUCT
) {
1585 event
->fields_decl
= container_of(declaration
, struct declaration_struct
, p
);
1586 } else if (!strcmp(left
, "loglevel.identifier")) {
1589 if (CTF_EVENT_FIELD_IS_SET(event
, loglevel_identifier
)) {
1590 fprintf(fd
, "[error] %s: identifier already declared in event declaration\n", __func__
);
1594 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
1596 fprintf(fd
, "[error] %s: unexpected unary expression for event identifier\n", __func__
);
1600 event
->loglevel_identifier
= g_quark_from_string(right
);
1602 CTF_EVENT_SET_FIELD(event
, loglevel_identifier
);
1603 } else if (!strcmp(left
, "loglevel.value")) {
1604 if (CTF_EVENT_FIELD_IS_SET(event
, loglevel_value
)) {
1605 fprintf(fd
, "[error] %s: loglevel value already declared in event declaration\n", __func__
);
1609 ret
= get_unary_signed(&node
->u
.ctf_expression
.right
, &event
->loglevel_value
);
1611 fprintf(fd
, "[error] %s: unexpected unary expression for event loglevel value\n", __func__
);
1615 CTF_EVENT_SET_FIELD(event
, loglevel_value
);
1617 fprintf(fd
, "[warning] %s: attribute \"%s\" is unknown in event declaration.\n", __func__
, left
);
1618 /* Fall-through after warning */
1626 /* TODO: declaration specifier should be added. */
1633 int ctf_event_visit(FILE *fd
, int depth
, struct ctf_node
*node
,
1634 struct declaration_scope
*parent_declaration_scope
, struct ctf_trace
*trace
)
1637 struct ctf_node
*iter
;
1638 struct ctf_event
*event
;
1640 event
= g_new0(struct ctf_event
, 1);
1641 event
->declaration_scope
= new_declaration_scope(parent_declaration_scope
);
1642 cds_list_for_each_entry(iter
, &node
->u
.event
.declaration_list
, siblings
) {
1643 ret
= ctf_event_declaration_visit(fd
, depth
+ 1, iter
, event
, trace
);
1647 if (!CTF_EVENT_FIELD_IS_SET(event
, name
)) {
1649 fprintf(fd
, "[error] %s: missing name field in event declaration\n", __func__
);
1652 if (!CTF_EVENT_FIELD_IS_SET(event
, stream_id
)) {
1653 /* Allow missing stream_id if there is only a single stream */
1654 switch (trace
->streams
->len
) {
1655 case 0: /* Create stream if there was none. */
1656 ret
= ctf_stream_visit(fd
, depth
, NULL
, trace
->root_declaration_scope
, trace
);
1661 event
->stream_id
= 0;
1662 event
->stream
= trace_stream_lookup(trace
, event
->stream_id
);
1666 fprintf(fd
, "[error] %s: missing stream_id field in event declaration\n", __func__
);
1670 /* Allow only one event without id per stream */
1671 if (!CTF_EVENT_FIELD_IS_SET(event
, id
)
1672 && event
->stream
->events_by_id
->len
!= 0) {
1674 fprintf(fd
, "[error] %s: missing id field in event declaration\n", __func__
);
1677 if (event
->stream
->events_by_id
->len
<= event
->id
)
1678 g_ptr_array_set_size(event
->stream
->events_by_id
, event
->id
+ 1);
1679 g_ptr_array_index(event
->stream
->events_by_id
, event
->id
) = event
;
1680 g_hash_table_insert(event
->stream
->event_quark_to_id
,
1681 (gpointer
)(unsigned long) event
->name
,
1686 if (event
->fields_decl
)
1687 declaration_unref(&event
->fields_decl
->p
);
1688 if (event
->context_decl
)
1689 declaration_unref(&event
->context_decl
->p
);
1690 free_declaration_scope(event
->declaration_scope
);
1697 int ctf_stream_declaration_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_stream_class
*stream
, struct ctf_trace
*trace
)
1701 switch (node
->type
) {
1703 ret
= ctf_typedef_visit(fd
, depth
+ 1,
1704 stream
->declaration_scope
,
1705 node
->u
._typedef
.type_specifier_list
,
1706 &node
->u
._typedef
.type_declarators
,
1711 case NODE_TYPEALIAS
:
1712 ret
= ctf_typealias_visit(fd
, depth
+ 1,
1713 stream
->declaration_scope
,
1714 node
->u
.typealias
.target
, node
->u
.typealias
.alias
,
1719 case NODE_CTF_EXPRESSION
:
1723 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
1724 if (!strcmp(left
, "id")) {
1725 if (CTF_STREAM_FIELD_IS_SET(stream
, stream_id
)) {
1726 fprintf(fd
, "[error] %s: id already declared in stream declaration\n", __func__
);
1730 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &stream
->stream_id
);
1732 fprintf(fd
, "[error] %s: unexpected unary expression for stream id\n", __func__
);
1736 CTF_STREAM_SET_FIELD(stream
, stream_id
);
1737 } else if (!strcmp(left
, "event.header")) {
1738 struct declaration
*declaration
;
1740 if (stream
->event_header_decl
) {
1741 fprintf(fd
, "[error] %s: event.header already declared in stream declaration\n", __func__
);
1745 declaration
= ctf_type_specifier_list_visit(fd
, depth
,
1746 _cds_list_first_entry(&node
->u
.ctf_expression
.right
,
1747 struct ctf_node
, siblings
),
1748 stream
->declaration_scope
, trace
);
1753 if (declaration
->id
!= CTF_TYPE_STRUCT
) {
1757 stream
->event_header_decl
= container_of(declaration
, struct declaration_struct
, p
);
1758 } else if (!strcmp(left
, "event.context")) {
1759 struct declaration
*declaration
;
1761 if (stream
->event_context_decl
) {
1762 fprintf(fd
, "[error] %s: event.context already declared in stream declaration\n", __func__
);
1766 declaration
= ctf_type_specifier_list_visit(fd
, depth
,
1767 _cds_list_first_entry(&node
->u
.ctf_expression
.right
,
1768 struct ctf_node
, siblings
),
1769 stream
->declaration_scope
, trace
);
1774 if (declaration
->id
!= CTF_TYPE_STRUCT
) {
1778 stream
->event_context_decl
= container_of(declaration
, struct declaration_struct
, p
);
1779 } else if (!strcmp(left
, "packet.context")) {
1780 struct declaration
*declaration
;
1782 if (stream
->packet_context_decl
) {
1783 fprintf(fd
, "[error] %s: packet.context already declared in stream declaration\n", __func__
);
1787 declaration
= ctf_type_specifier_list_visit(fd
, depth
,
1788 _cds_list_first_entry(&node
->u
.ctf_expression
.right
,
1789 struct ctf_node
, siblings
),
1790 stream
->declaration_scope
, trace
);
1795 if (declaration
->id
!= CTF_TYPE_STRUCT
) {
1799 stream
->packet_context_decl
= container_of(declaration
, struct declaration_struct
, p
);
1801 fprintf(fd
, "[warning] %s: attribute \"%s\" is unknown in stream declaration.\n", __func__
, left
);
1802 /* Fall-through after warning */
1811 /* TODO: declaration specifier should be added. */
1818 int ctf_stream_visit(FILE *fd
, int depth
, struct ctf_node
*node
,
1819 struct declaration_scope
*parent_declaration_scope
, struct ctf_trace
*trace
)
1822 struct ctf_node
*iter
;
1823 struct ctf_stream_class
*stream
;
1825 stream
= g_new0(struct ctf_stream_class
, 1);
1826 stream
->declaration_scope
= new_declaration_scope(parent_declaration_scope
);
1827 stream
->events_by_id
= g_ptr_array_new();
1828 stream
->event_quark_to_id
= g_hash_table_new(g_direct_hash
, g_direct_equal
);
1829 stream
->streams
= g_ptr_array_new();
1831 cds_list_for_each_entry(iter
, &node
->u
.stream
.declaration_list
, siblings
) {
1832 ret
= ctf_stream_declaration_visit(fd
, depth
+ 1, iter
, stream
, trace
);
1837 if (CTF_STREAM_FIELD_IS_SET(stream
, stream_id
)) {
1838 /* check that packet header has stream_id field. */
1839 if (!trace
->packet_header_decl
1840 || struct_declaration_lookup_field_index(trace
->packet_header_decl
, g_quark_from_static_string("stream_id")) < 0) {
1842 fprintf(fd
, "[error] %s: missing stream_id field in packet header declaration, but stream_id attribute is declared for stream.\n", __func__
);
1846 /* Allow only one id-less stream */
1847 if (trace
->streams
->len
!= 0) {
1849 fprintf(fd
, "[error] %s: missing id field in stream declaration\n", __func__
);
1852 stream
->stream_id
= 0;
1854 if (trace
->streams
->len
<= stream
->stream_id
)
1855 g_ptr_array_set_size(trace
->streams
, stream
->stream_id
+ 1);
1856 g_ptr_array_index(trace
->streams
, stream
->stream_id
) = stream
;
1857 stream
->trace
= trace
;
1862 if (stream
->event_header_decl
)
1863 declaration_unref(&stream
->event_header_decl
->p
);
1864 if (stream
->event_context_decl
)
1865 declaration_unref(&stream
->event_context_decl
->p
);
1866 if (stream
->packet_context_decl
)
1867 declaration_unref(&stream
->packet_context_decl
->p
);
1868 g_ptr_array_free(stream
->streams
, TRUE
);
1869 g_ptr_array_free(stream
->events_by_id
, TRUE
);
1870 g_hash_table_destroy(stream
->event_quark_to_id
);
1871 free_declaration_scope(stream
->declaration_scope
);
1877 int ctf_trace_declaration_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_trace
*trace
)
1881 switch (node
->type
) {
1883 ret
= ctf_typedef_visit(fd
, depth
+ 1,
1884 trace
->declaration_scope
,
1885 node
->u
._typedef
.type_specifier_list
,
1886 &node
->u
._typedef
.type_declarators
,
1891 case NODE_TYPEALIAS
:
1892 ret
= ctf_typealias_visit(fd
, depth
+ 1,
1893 trace
->declaration_scope
,
1894 node
->u
.typealias
.target
, node
->u
.typealias
.alias
,
1899 case NODE_CTF_EXPRESSION
:
1903 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
1904 if (!strcmp(left
, "major")) {
1905 if (CTF_TRACE_FIELD_IS_SET(trace
, major
)) {
1906 fprintf(fd
, "[error] %s: major already declared in trace declaration\n", __func__
);
1910 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &trace
->major
);
1912 fprintf(fd
, "[error] %s: unexpected unary expression for trace major number\n", __func__
);
1916 CTF_TRACE_SET_FIELD(trace
, major
);
1917 } else if (!strcmp(left
, "minor")) {
1918 if (CTF_TRACE_FIELD_IS_SET(trace
, minor
)) {
1919 fprintf(fd
, "[error] %s: minor already declared in trace declaration\n", __func__
);
1923 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &trace
->minor
);
1925 fprintf(fd
, "[error] %s: unexpected unary expression for trace minor number\n", __func__
);
1929 CTF_TRACE_SET_FIELD(trace
, minor
);
1930 } else if (!strcmp(left
, "uuid")) {
1933 ret
= get_unary_uuid(&node
->u
.ctf_expression
.right
, &uuid
);
1935 fprintf(fd
, "[error] %s: unexpected unary expression for trace uuid\n", __func__
);
1939 if (CTF_TRACE_FIELD_IS_SET(trace
, uuid
)
1940 && uuid_compare(uuid
, trace
->uuid
)) {
1941 fprintf(fd
, "[error] %s: uuid mismatch\n", __func__
);
1945 memcpy(trace
->uuid
, uuid
, sizeof(uuid
));
1947 CTF_TRACE_SET_FIELD(trace
, uuid
);
1948 } else if (!strcmp(left
, "byte_order")) {
1949 struct ctf_node
*right
;
1952 right
= _cds_list_first_entry(&node
->u
.ctf_expression
.right
, struct ctf_node
, siblings
);
1953 byte_order
= get_trace_byte_order(fd
, depth
, right
);
1957 if (CTF_TRACE_FIELD_IS_SET(trace
, byte_order
)
1958 && byte_order
!= trace
->byte_order
) {
1959 fprintf(fd
, "[error] %s: endianness mismatch\n", __func__
);
1963 if (byte_order
!= trace
->byte_order
) {
1964 trace
->byte_order
= byte_order
;
1966 * We need to restart
1967 * construction of the
1968 * intermediate representation.
1970 trace
->field_mask
= 0;
1971 CTF_TRACE_SET_FIELD(trace
, byte_order
);
1976 CTF_TRACE_SET_FIELD(trace
, byte_order
);
1977 } else if (!strcmp(left
, "packet.header")) {
1978 struct declaration
*declaration
;
1980 if (trace
->packet_header_decl
) {
1981 fprintf(fd
, "[error] %s: packet.header already declared in trace declaration\n", __func__
);
1985 declaration
= ctf_type_specifier_list_visit(fd
, depth
,
1986 _cds_list_first_entry(&node
->u
.ctf_expression
.right
,
1987 struct ctf_node
, siblings
),
1988 trace
->declaration_scope
, trace
);
1993 if (declaration
->id
!= CTF_TYPE_STRUCT
) {
1997 trace
->packet_header_decl
= container_of(declaration
, struct declaration_struct
, p
);
1999 fprintf(fd
, "[warning] %s: attribute \"%s\" is unknown in trace declaration.\n", __func__
, left
);
2008 /* TODO: declaration specifier should be added. */
2015 int ctf_trace_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_trace
*trace
)
2018 struct ctf_node
*iter
;
2020 if (trace
->declaration_scope
)
2022 trace
->declaration_scope
= new_declaration_scope(trace
->root_declaration_scope
);
2023 trace
->streams
= g_ptr_array_new();
2024 cds_list_for_each_entry(iter
, &node
->u
.trace
.declaration_list
, siblings
) {
2025 ret
= ctf_trace_declaration_visit(fd
, depth
+ 1, iter
, trace
);
2029 if (!CTF_TRACE_FIELD_IS_SET(trace
, major
)) {
2031 fprintf(fd
, "[error] %s: missing major field in trace declaration\n", __func__
);
2034 if (!CTF_TRACE_FIELD_IS_SET(trace
, minor
)) {
2036 fprintf(fd
, "[error] %s: missing minor field in trace declaration\n", __func__
);
2039 if (!CTF_TRACE_FIELD_IS_SET(trace
, uuid
)) {
2041 fprintf(fd
, "[error] %s: missing uuid field in trace declaration\n", __func__
);
2044 if (!CTF_TRACE_FIELD_IS_SET(trace
, byte_order
)) {
2046 fprintf(fd
, "[error] %s: missing byte_order field in trace declaration\n", __func__
);
2050 if (!CTF_TRACE_FIELD_IS_SET(trace
, byte_order
)) {
2051 /* check that the packet header contains a "magic" field */
2052 if (!trace
->packet_header_decl
2053 || struct_declaration_lookup_field_index(trace
->packet_header_decl
, g_quark_from_static_string("magic")) < 0) {
2055 fprintf(fd
, "[error] %s: missing both byte_order and packet header magic number in trace declaration\n", __func__
);
2062 if (trace
->packet_header_decl
) {
2063 declaration_unref(&trace
->packet_header_decl
->p
);
2064 trace
->packet_header_decl
= NULL
;
2066 g_ptr_array_free(trace
->streams
, TRUE
);
2067 free_declaration_scope(trace
->declaration_scope
);
2068 trace
->declaration_scope
= NULL
;
2073 int ctf_root_declaration_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_trace
*trace
)
2077 switch (node
->type
) {
2079 ret
= ctf_typedef_visit(fd
, depth
+ 1,
2080 trace
->root_declaration_scope
,
2081 node
->u
._typedef
.type_specifier_list
,
2082 &node
->u
._typedef
.type_declarators
,
2087 case NODE_TYPEALIAS
:
2088 ret
= ctf_typealias_visit(fd
, depth
+ 1,
2089 trace
->root_declaration_scope
,
2090 node
->u
.typealias
.target
, node
->u
.typealias
.alias
,
2095 case NODE_TYPE_SPECIFIER_LIST
:
2097 struct declaration
*declaration
;
2100 * Just add the type specifier to the root scope
2101 * declaration scope. Release local reference.
2103 declaration
= ctf_type_specifier_list_visit(fd
, depth
+ 1,
2104 node
, trace
->root_declaration_scope
, trace
);
2107 declaration_unref(declaration
);
2117 int ctf_visitor_construct_metadata(FILE *fd
, int depth
, struct ctf_node
*node
,
2118 struct ctf_trace
*trace
, int byte_order
)
2121 struct ctf_node
*iter
;
2123 printf_verbose("CTF visitor: metadata construction... ");
2124 trace
->byte_order
= byte_order
;
2127 trace
->root_declaration_scope
= new_declaration_scope(NULL
);
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
);
2141 if (ret
== -EINTR
) {
2142 free_declaration_scope(trace
->root_declaration_scope
);
2144 * Need to restart creation of type
2145 * definitions, aliases and
2146 * trace header declarations.
2151 fprintf(fd
, "[error] %s: trace declaration error\n", __func__
);
2155 if (!trace
->streams
) {
2156 fprintf(fd
, "[error] %s: missing trace declaration\n", __func__
);
2160 cds_list_for_each_entry(iter
, &node
->u
.root
.stream
, siblings
) {
2161 ret
= ctf_stream_visit(fd
, depth
+ 1, iter
,
2162 trace
->root_declaration_scope
, trace
);
2164 fprintf(fd
, "[error] %s: stream declaration error\n", __func__
);
2168 cds_list_for_each_entry(iter
, &node
->u
.root
.event
, siblings
) {
2169 ret
= ctf_event_visit(fd
, depth
+ 1, iter
,
2170 trace
->root_declaration_scope
, trace
);
2172 fprintf(fd
, "[error] %s: event declaration error\n", __func__
);
2179 fprintf(fd
, "[error] %s: unknown node type %d\n", __func__
,
2184 printf_verbose("done.\n");
2188 free_declaration_scope(trace
->root_declaration_scope
);