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
)
321 integer_declaration
->base
= 16;
324 nested_declaration
= ctf_type_specifier_list_visit(fd
, depth
,
325 type_specifier_list
, declaration_scope
, trace
);
329 if (!node_type_declarator
)
330 return nested_declaration
;
332 if (node_type_declarator
->u
.type_declarator
.type
== TYPEDEC_ID
) {
333 if (node_type_declarator
->u
.type_declarator
.u
.id
)
334 *field_name
= g_quark_from_string(node_type_declarator
->u
.type_declarator
.u
.id
);
337 return nested_declaration
;
339 struct declaration
*declaration
;
340 struct ctf_node
*length
;
344 /* create array/sequence, pass nested_declaration as child. */
345 length
= node_type_declarator
->u
.type_declarator
.u
.nested
.length
;
347 fprintf(fd
, "[error] %s: expecting length type or value.\n", __func__
);
350 switch (length
->type
) {
351 case NODE_UNARY_EXPRESSION
:
353 struct declaration_array
*array_declaration
;
356 if (length
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
357 fprintf(fd
, "[error] %s: array: unexpected unary expression.\n", __func__
);
360 len
= length
->u
.unary_expression
.u
.unsigned_constant
;
361 array_declaration
= array_declaration_new(len
, nested_declaration
,
363 declaration
= &array_declaration
->p
;
366 case NODE_TYPE_SPECIFIER_LIST
:
368 struct declaration_sequence
*sequence_declaration
;
369 struct declaration_integer
*integer_declaration
;
371 declaration
= ctf_type_specifier_list_visit(fd
, depth
,
372 length
, declaration_scope
, trace
);
374 fprintf(fd
, "[error] %s: unable to find declaration type for sequence length\n", __func__
);
377 if (declaration
->id
!= CTF_TYPE_INTEGER
) {
378 fprintf(fd
, "[error] %s: length type for sequence is expected to be an integer (unsigned).\n", __func__
);
379 declaration_unref(declaration
);
382 integer_declaration
= container_of(declaration
, struct declaration_integer
, p
);
383 if (integer_declaration
->signedness
!= false) {
384 fprintf(fd
, "[error] %s: length type for sequence should always be an unsigned integer.\n", __func__
);
385 declaration_unref(declaration
);
389 sequence_declaration
= sequence_declaration_new(integer_declaration
,
390 nested_declaration
, declaration_scope
);
391 declaration
= &sequence_declaration
->p
;
398 /* Pass it as content of outer container */
399 declaration
= ctf_type_declarator_visit(fd
, depth
,
400 type_specifier_list
, field_name
,
401 node_type_declarator
->u
.type_declarator
.u
.nested
.type_declarator
,
402 declaration_scope
, declaration
, trace
);
408 int ctf_struct_type_declarators_visit(FILE *fd
, int depth
,
409 struct declaration_struct
*struct_declaration
,
410 struct ctf_node
*type_specifier_list
,
411 struct cds_list_head
*type_declarators
,
412 struct declaration_scope
*declaration_scope
,
413 struct ctf_trace
*trace
)
415 struct ctf_node
*iter
;
418 cds_list_for_each_entry(iter
, type_declarators
, siblings
) {
419 struct declaration
*field_declaration
;
421 field_declaration
= ctf_type_declarator_visit(fd
, depth
,
424 struct_declaration
->scope
,
426 if (!field_declaration
) {
427 fprintf(fd
, "[error] %s: unable to find struct field declaration type\n", __func__
);
430 struct_declaration_add_field(struct_declaration
,
431 g_quark_to_string(field_name
),
438 int ctf_variant_type_declarators_visit(FILE *fd
, int depth
,
439 struct declaration_untagged_variant
*untagged_variant_declaration
,
440 struct ctf_node
*type_specifier_list
,
441 struct cds_list_head
*type_declarators
,
442 struct declaration_scope
*declaration_scope
,
443 struct ctf_trace
*trace
)
445 struct ctf_node
*iter
;
448 cds_list_for_each_entry(iter
, type_declarators
, siblings
) {
449 struct declaration
*field_declaration
;
451 field_declaration
= ctf_type_declarator_visit(fd
, depth
,
454 untagged_variant_declaration
->scope
,
456 if (!field_declaration
) {
457 fprintf(fd
, "[error] %s: unable to find variant field declaration type\n", __func__
);
460 untagged_variant_declaration_add_field(untagged_variant_declaration
,
461 g_quark_to_string(field_name
),
468 int ctf_typedef_visit(FILE *fd
, int depth
, struct declaration_scope
*scope
,
469 struct ctf_node
*type_specifier_list
,
470 struct cds_list_head
*type_declarators
,
471 struct ctf_trace
*trace
)
473 struct ctf_node
*iter
;
476 cds_list_for_each_entry(iter
, type_declarators
, siblings
) {
477 struct declaration
*type_declaration
;
480 type_declaration
= ctf_type_declarator_visit(fd
, depth
,
484 if (!type_declaration
) {
485 fprintf(fd
, "[error] %s: problem creating type declaration\n", __func__
);
489 * Don't allow typedef and typealias of untagged
492 if (type_declaration
->id
== CTF_TYPE_UNTAGGED_VARIANT
) {
493 fprintf(fd
, "[error] %s: typedef of untagged variant is not permitted.\n", __func__
);
494 declaration_unref(type_declaration
);
497 ret
= register_declaration(identifier
, type_declaration
, scope
);
499 type_declaration
->declaration_free(type_declaration
);
507 int ctf_typealias_visit(FILE *fd
, int depth
, struct declaration_scope
*scope
,
508 struct ctf_node
*target
, struct ctf_node
*alias
,
509 struct ctf_trace
*trace
)
511 struct declaration
*type_declaration
;
512 struct ctf_node
*node
;
517 /* See ctf_visitor_type_declarator() in the semantic validator. */
520 * Create target type declaration.
523 if (cds_list_empty(&target
->u
.typealias_target
.type_declarators
))
526 node
= _cds_list_first_entry(&target
->u
.typealias_target
.type_declarators
,
527 struct ctf_node
, siblings
);
528 type_declaration
= ctf_type_declarator_visit(fd
, depth
,
529 target
->u
.typealias_target
.type_specifier_list
,
532 if (!type_declaration
) {
533 fprintf(fd
, "[error] %s: problem creating type declaration\n", __func__
);
538 * Don't allow typedef and typealias of untagged
541 if (type_declaration
->id
== CTF_TYPE_UNTAGGED_VARIANT
) {
542 fprintf(fd
, "[error] %s: typedef of untagged variant is not permitted.\n", __func__
);
543 declaration_unref(type_declaration
);
547 * The semantic validator does not check whether the target is
548 * abstract or not (if it has an identifier). Check it here.
551 fprintf(fd
, "[error] %s: expecting empty identifier\n", __func__
);
556 * Create alias identifier.
559 node
= _cds_list_first_entry(&alias
->u
.typealias_alias
.type_declarators
,
560 struct ctf_node
, siblings
);
561 alias_q
= create_typealias_identifier(fd
, depth
,
562 alias
->u
.typealias_alias
.type_specifier_list
, node
);
563 err
= register_declaration(alias_q
, type_declaration
, scope
);
569 type_declaration
->declaration_free(type_declaration
);
574 int ctf_struct_declaration_list_visit(FILE *fd
, int depth
,
575 struct ctf_node
*iter
, struct declaration_struct
*struct_declaration
,
576 struct ctf_trace
*trace
)
580 switch (iter
->type
) {
582 /* For each declarator, declare type and add type to struct declaration scope */
583 ret
= ctf_typedef_visit(fd
, depth
,
584 struct_declaration
->scope
,
585 iter
->u
._typedef
.type_specifier_list
,
586 &iter
->u
._typedef
.type_declarators
, trace
);
591 /* Declare type with declarator and add type to struct declaration scope */
592 ret
= ctf_typealias_visit(fd
, depth
,
593 struct_declaration
->scope
,
594 iter
->u
.typealias
.target
,
595 iter
->u
.typealias
.alias
, trace
);
599 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
600 /* Add field to structure declaration */
601 ret
= ctf_struct_type_declarators_visit(fd
, depth
,
603 iter
->u
.struct_or_variant_declaration
.type_specifier_list
,
604 &iter
->u
.struct_or_variant_declaration
.type_declarators
,
605 struct_declaration
->scope
, trace
);
610 fprintf(fd
, "[error] %s: unexpected node type %d\n", __func__
, (int) iter
->type
);
617 int ctf_variant_declaration_list_visit(FILE *fd
, int depth
,
618 struct ctf_node
*iter
,
619 struct declaration_untagged_variant
*untagged_variant_declaration
,
620 struct ctf_trace
*trace
)
624 switch (iter
->type
) {
626 /* For each declarator, declare type and add type to variant declaration scope */
627 ret
= ctf_typedef_visit(fd
, depth
,
628 untagged_variant_declaration
->scope
,
629 iter
->u
._typedef
.type_specifier_list
,
630 &iter
->u
._typedef
.type_declarators
, trace
);
635 /* Declare type with declarator and add type to variant declaration scope */
636 ret
= ctf_typealias_visit(fd
, depth
,
637 untagged_variant_declaration
->scope
,
638 iter
->u
.typealias
.target
,
639 iter
->u
.typealias
.alias
, trace
);
643 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
644 /* Add field to structure declaration */
645 ret
= ctf_variant_type_declarators_visit(fd
, depth
,
646 untagged_variant_declaration
,
647 iter
->u
.struct_or_variant_declaration
.type_specifier_list
,
648 &iter
->u
.struct_or_variant_declaration
.type_declarators
,
649 untagged_variant_declaration
->scope
, trace
);
654 fprintf(fd
, "[error] %s: unexpected node type %d\n", __func__
, (int) iter
->type
);
661 struct declaration
*ctf_declaration_struct_visit(FILE *fd
,
662 int depth
, const char *name
, struct cds_list_head
*declaration_list
,
663 int has_body
, struct cds_list_head
*min_align
,
664 struct declaration_scope
*declaration_scope
,
665 struct ctf_trace
*trace
)
667 struct declaration_struct
*struct_declaration
;
668 struct ctf_node
*iter
;
672 * For named struct (without body), lookup in
673 * declaration scope. Don't take reference on struct
674 * declaration: ref is only taken upon definition.
679 lookup_struct_declaration(g_quark_from_string(name
),
681 return &struct_declaration
->p
;
683 uint64_t min_align_value
= 0;
685 /* For unnamed struct, create type */
686 /* For named struct (with body), create type and add to declaration scope */
688 if (lookup_struct_declaration(g_quark_from_string(name
),
689 declaration_scope
)) {
691 fprintf(fd
, "[error] %s: struct %s already declared in scope\n", __func__
, name
);
695 if (!cds_list_empty(min_align
)) {
696 ret
= get_unary_unsigned(min_align
, &min_align_value
);
698 fprintf(fd
, "[error] %s: unexpected unary expression for structure \"align\" attribute\n", __func__
);
703 struct_declaration
= struct_declaration_new(declaration_scope
,
705 cds_list_for_each_entry(iter
, declaration_list
, siblings
) {
706 ret
= ctf_struct_declaration_list_visit(fd
, depth
+ 1, iter
,
707 struct_declaration
, trace
);
712 ret
= register_struct_declaration(g_quark_from_string(name
),
717 return &struct_declaration
->p
;
720 struct_declaration
->p
.declaration_free(&struct_declaration
->p
);
725 struct declaration
*ctf_declaration_variant_visit(FILE *fd
,
726 int depth
, const char *name
, const char *choice
,
727 struct cds_list_head
*declaration_list
,
728 int has_body
, struct declaration_scope
*declaration_scope
,
729 struct ctf_trace
*trace
)
731 struct declaration_untagged_variant
*untagged_variant_declaration
;
732 struct declaration_variant
*variant_declaration
;
733 struct ctf_node
*iter
;
737 * For named variant (without body), lookup in
738 * declaration scope. Don't take reference on variant
739 * declaration: ref is only taken upon definition.
743 untagged_variant_declaration
=
744 lookup_variant_declaration(g_quark_from_string(name
),
747 /* For unnamed variant, create type */
748 /* For named variant (with body), create type and add to declaration scope */
750 if (lookup_variant_declaration(g_quark_from_string(name
),
751 declaration_scope
)) {
753 fprintf(fd
, "[error] %s: variant %s already declared in scope\n", __func__
, name
);
757 untagged_variant_declaration
= untagged_variant_declaration_new(declaration_scope
);
758 cds_list_for_each_entry(iter
, declaration_list
, siblings
) {
759 ret
= ctf_variant_declaration_list_visit(fd
, depth
+ 1, iter
,
760 untagged_variant_declaration
, trace
);
765 ret
= register_variant_declaration(g_quark_from_string(name
),
766 untagged_variant_declaration
,
772 * if tagged, create tagged variant and return. else return
776 return &untagged_variant_declaration
->p
;
778 variant_declaration
= variant_declaration_new(untagged_variant_declaration
, choice
);
779 if (!variant_declaration
)
781 declaration_unref(&untagged_variant_declaration
->p
);
782 return &variant_declaration
->p
;
785 untagged_variant_declaration
->p
.declaration_free(&untagged_variant_declaration
->p
);
790 int ctf_enumerator_list_visit(FILE *fd
, int depth
,
791 struct ctf_node
*enumerator
,
792 struct declaration_enum
*enum_declaration
)
795 struct ctf_node
*iter
;
797 q
= g_quark_from_string(enumerator
->u
.enumerator
.id
);
798 if (enum_declaration
->integer_declaration
->signedness
) {
802 cds_list_for_each_entry(iter
, &enumerator
->u
.enumerator
.values
, siblings
) {
805 assert(iter
->type
== NODE_UNARY_EXPRESSION
);
811 switch (iter
->u
.unary_expression
.type
) {
812 case UNARY_SIGNED_CONSTANT
:
813 *target
= iter
->u
.unary_expression
.u
.signed_constant
;
815 case UNARY_UNSIGNED_CONSTANT
:
816 *target
= iter
->u
.unary_expression
.u
.unsigned_constant
;
819 fprintf(fd
, "[error] %s: invalid enumerator\n", __func__
);
823 fprintf(fd
, "[error] %s: invalid enumerator\n", __func__
);
830 enum_signed_insert(enum_declaration
, start
, end
, q
);
835 cds_list_for_each_entry(iter
, &enumerator
->u
.enumerator
.values
, siblings
) {
838 assert(iter
->type
== NODE_UNARY_EXPRESSION
);
844 switch (iter
->u
.unary_expression
.type
) {
845 case UNARY_UNSIGNED_CONSTANT
:
846 *target
= iter
->u
.unary_expression
.u
.unsigned_constant
;
848 case UNARY_SIGNED_CONSTANT
:
850 * We don't accept signed constants for enums with unsigned
853 fprintf(fd
, "[error] %s: invalid enumerator (signed constant encountered, but enum container type is unsigned)\n", __func__
);
856 fprintf(fd
, "[error] %s: invalid enumerator\n", __func__
);
860 fprintf(fd
, "[error] %s: invalid enumerator\n", __func__
);
867 enum_unsigned_insert(enum_declaration
, start
, end
, q
);
873 struct declaration
*ctf_declaration_enum_visit(FILE *fd
, int depth
,
875 struct ctf_node
*container_type
,
876 struct cds_list_head
*enumerator_list
,
878 struct declaration_scope
*declaration_scope
,
879 struct ctf_trace
*trace
)
881 struct declaration
*declaration
;
882 struct declaration_enum
*enum_declaration
;
883 struct declaration_integer
*integer_declaration
;
884 struct ctf_node
*iter
;
889 * For named enum (without body), lookup in
890 * declaration scope. Don't take reference on enum
891 * declaration: ref is only taken upon definition.
896 lookup_enum_declaration(g_quark_from_string(name
),
898 return &enum_declaration
->p
;
900 /* For unnamed enum, create type */
901 /* For named enum (with body), create type and add to declaration scope */
903 if (lookup_enum_declaration(g_quark_from_string(name
),
904 declaration_scope
)) {
906 fprintf(fd
, "[error] %s: enum %s already declared in scope\n", __func__
, name
);
910 if (!container_type
) {
911 declaration
= lookup_declaration(g_quark_from_static_string("int"),
914 fprintf(fd
, "[error] %s: \"int\" type declaration missing for enumeration\n", __func__
);
918 declaration
= ctf_type_declarator_visit(fd
, depth
,
925 fprintf(fd
, "[error] %s: unable to create container type for enumeration\n", __func__
);
928 if (declaration
->id
!= CTF_TYPE_INTEGER
) {
929 fprintf(fd
, "[error] %s: container type for enumeration is not integer\n", __func__
);
932 integer_declaration
= container_of(declaration
, struct declaration_integer
, p
);
933 enum_declaration
= enum_declaration_new(integer_declaration
);
934 declaration_unref(&integer_declaration
->p
); /* leave ref to enum */
935 cds_list_for_each_entry(iter
, enumerator_list
, siblings
) {
936 ret
= ctf_enumerator_list_visit(fd
, depth
+ 1, iter
, enum_declaration
);
941 ret
= register_enum_declaration(g_quark_from_string(name
),
946 return &enum_declaration
->p
;
949 enum_declaration
->p
.declaration_free(&enum_declaration
->p
);
954 struct declaration
*ctf_declaration_type_specifier_visit(FILE *fd
, int depth
,
955 struct ctf_node
*type_specifier_list
,
956 struct declaration_scope
*declaration_scope
)
959 struct declaration
*declaration
;
964 str
= g_string_new("");
965 ret
= visit_type_specifier_list(fd
, type_specifier_list
, str
);
968 str_c
= g_string_free(str
, FALSE
);
969 id_q
= g_quark_from_string(str_c
);
971 declaration
= lookup_declaration(id_q
, declaration_scope
);
976 * Returns 0/1 boolean, or < 0 on error.
979 int get_boolean(FILE *fd
, int depth
, struct ctf_node
*unary_expression
)
981 if (unary_expression
->type
!= NODE_UNARY_EXPRESSION
) {
982 fprintf(fd
, "[error] %s: expecting unary expression\n",
986 switch (unary_expression
->u
.unary_expression
.type
) {
987 case UNARY_UNSIGNED_CONSTANT
:
988 if (unary_expression
->u
.unary_expression
.u
.unsigned_constant
== 0)
992 case UNARY_SIGNED_CONSTANT
:
993 if (unary_expression
->u
.unary_expression
.u
.signed_constant
== 0)
998 if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "true"))
1000 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "TRUE"))
1002 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "false"))
1004 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "FALSE"))
1007 fprintf(fd
, "[error] %s: unexpected string \"%s\"\n",
1008 __func__
, unary_expression
->u
.unary_expression
.u
.string
);
1013 fprintf(fd
, "[error] %s: unexpected unary expression type\n",
1021 int get_trace_byte_order(FILE *fd
, int depth
, struct ctf_node
*unary_expression
)
1025 if (unary_expression
->u
.unary_expression
.type
!= UNARY_STRING
) {
1026 fprintf(fd
, "[error] %s: byte_order: expecting string\n",
1030 if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "be"))
1031 byte_order
= BIG_ENDIAN
;
1032 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "le"))
1033 byte_order
= LITTLE_ENDIAN
;
1035 fprintf(fd
, "[error] %s: unexpected string \"%s\". Should be \"native\", \"network\", \"be\" or \"le\".\n",
1036 __func__
, unary_expression
->u
.unary_expression
.u
.string
);
1043 int get_byte_order(FILE *fd
, int depth
, struct ctf_node
*unary_expression
,
1044 struct ctf_trace
*trace
)
1048 if (unary_expression
->u
.unary_expression
.type
!= UNARY_STRING
) {
1049 fprintf(fd
, "[error] %s: byte_order: expecting string\n",
1053 if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "native"))
1054 byte_order
= trace
->byte_order
;
1055 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "network"))
1056 byte_order
= BIG_ENDIAN
;
1057 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "be"))
1058 byte_order
= BIG_ENDIAN
;
1059 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "le"))
1060 byte_order
= LITTLE_ENDIAN
;
1062 fprintf(fd
, "[error] %s: unexpected string \"%s\". Should be \"native\", \"network\", \"be\" or \"le\".\n",
1063 __func__
, unary_expression
->u
.unary_expression
.u
.string
);
1070 struct declaration
*ctf_declaration_integer_visit(FILE *fd
, int depth
,
1071 struct cds_list_head
*expressions
,
1072 struct ctf_trace
*trace
)
1074 struct ctf_node
*expression
;
1075 uint64_t alignment
, size
;
1076 int byte_order
= trace
->byte_order
;
1078 int has_alignment
= 0, has_size
= 0;
1080 struct declaration_integer
*integer_declaration
;
1082 cds_list_for_each_entry(expression
, expressions
, siblings
) {
1083 struct ctf_node
*left
, *right
;
1085 left
= _cds_list_first_entry(&expression
->u
.ctf_expression
.left
, struct ctf_node
, siblings
);
1086 right
= _cds_list_first_entry(&expression
->u
.ctf_expression
.right
, struct ctf_node
, siblings
);
1087 assert(left
->u
.unary_expression
.type
== UNARY_STRING
);
1088 if (!strcmp(left
->u
.unary_expression
.u
.string
, "signed")) {
1089 signedness
= get_boolean(fd
, depth
, right
);
1092 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "byte_order")) {
1093 byte_order
= get_byte_order(fd
, depth
, right
, trace
);
1096 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "size")) {
1097 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
1098 fprintf(fd
, "[error] %s: size: expecting unsigned constant\n",
1102 size
= right
->u
.unary_expression
.u
.unsigned_constant
;
1104 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "align")) {
1105 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
1106 fprintf(fd
, "[error] %s: align: expecting unsigned constant\n",
1110 alignment
= right
->u
.unary_expression
.u
.unsigned_constant
;
1111 /* Make sure alignment is a power of two */
1112 if (alignment
== 0 || (alignment
& (alignment
- 1)) != 0) {
1113 fprintf(fd
, "[error] %s: align: expecting power of two\n",
1118 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "base")) {
1119 switch (right
->u
.unary_expression
.type
) {
1120 case UNARY_UNSIGNED_CONSTANT
:
1121 switch (right
->u
.unary_expression
.u
.unsigned_constant
) {
1126 base
= right
->u
.unary_expression
.u
.unsigned_constant
;
1129 fprintf(fd
, "[error] %s: base not supported (%" PRIu64
")\n",
1130 __func__
, right
->u
.unary_expression
.u
.unsigned_constant
);
1136 char *s_right
= concatenate_unary_strings(&expression
->u
.ctf_expression
.right
);
1138 fprintf(fd
, "[error] %s: unexpected unary expression for integer base\n", __func__
);
1142 if (!strcmp(s_right
, "decimal") || !strcmp(s_right
, "dec") || !strcmp(s_right
, "d")
1143 || !strcmp(s_right
, "i") || !strcmp(s_right
, "u")) {
1145 } else if (!strcmp(s_right
, "hexadecimal") || !strcmp(s_right
, "hex")
1146 || !strcmp(s_right
, "x") || !strcmp(s_right
, "X")
1147 || !strcmp(s_right
, "p")) {
1149 } else if (!strcmp(s_right
, "octal") || !strcmp(s_right
, "oct")
1150 || !strcmp(s_right
, "o")) {
1152 } else if (!strcmp(s_right
, "binary") || !strcmp(s_right
, "b")) {
1155 fprintf(fd
, "[error] %s: unexpected expression for integer base (%s)\n", __func__
, s_right
);
1164 fprintf(fd
, "[error] %s: base: expecting unsigned constant or unary string\n",
1169 fprintf(fd
, "[error] %s: unknown attribute name %s\n",
1170 __func__
, left
->u
.unary_expression
.u
.string
);
1175 fprintf(fd
, "[error] %s: missing size attribute\n", __func__
);
1178 if (!has_alignment
) {
1179 if (size
% CHAR_BIT
) {
1180 /* bit-packed alignment */
1183 /* byte-packed alignment */
1184 alignment
= CHAR_BIT
;
1187 integer_declaration
= integer_declaration_new(size
,
1188 byte_order
, signedness
, alignment
, base
);
1189 return &integer_declaration
->p
;
1193 struct declaration
*ctf_declaration_floating_point_visit(FILE *fd
, int depth
,
1194 struct cds_list_head
*expressions
,
1195 struct ctf_trace
*trace
)
1197 struct ctf_node
*expression
;
1198 uint64_t alignment
, exp_dig
, mant_dig
, byte_order
= trace
->byte_order
;
1199 int has_alignment
= 0, has_exp_dig
= 0, has_mant_dig
= 0;
1200 struct declaration_float
*float_declaration
;
1202 cds_list_for_each_entry(expression
, expressions
, siblings
) {
1203 struct ctf_node
*left
, *right
;
1205 left
= _cds_list_first_entry(&expression
->u
.ctf_expression
.left
, struct ctf_node
, siblings
);
1206 right
= _cds_list_first_entry(&expression
->u
.ctf_expression
.right
, struct ctf_node
, siblings
);
1207 assert(left
->u
.unary_expression
.type
== UNARY_STRING
);
1208 if (!strcmp(left
->u
.unary_expression
.u
.string
, "byte_order")) {
1209 byte_order
= get_byte_order(fd
, depth
, right
, trace
);
1212 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "exp_dig")) {
1213 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
1214 fprintf(fd
, "[error] %s: exp_dig: expecting unsigned constant\n",
1218 exp_dig
= right
->u
.unary_expression
.u
.unsigned_constant
;
1220 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "mant_dig")) {
1221 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
1222 fprintf(fd
, "[error] %s: mant_dig: expecting unsigned constant\n",
1226 mant_dig
= right
->u
.unary_expression
.u
.unsigned_constant
;
1228 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "align")) {
1229 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
1230 fprintf(fd
, "[error] %s: align: expecting unsigned constant\n",
1234 alignment
= right
->u
.unary_expression
.u
.unsigned_constant
;
1235 /* Make sure alignment is a power of two */
1236 if (alignment
== 0 || (alignment
& (alignment
- 1)) != 0) {
1237 fprintf(fd
, "[error] %s: align: expecting power of two\n",
1243 fprintf(fd
, "[error] %s: unknown attribute name %s\n",
1244 __func__
, left
->u
.unary_expression
.u
.string
);
1248 if (!has_mant_dig
) {
1249 fprintf(fd
, "[error] %s: missing mant_dig attribute\n", __func__
);
1253 fprintf(fd
, "[error] %s: missing exp_dig attribute\n", __func__
);
1256 if (!has_alignment
) {
1257 if ((mant_dig
+ exp_dig
) % CHAR_BIT
) {
1258 /* bit-packed alignment */
1261 /* byte-packed alignment */
1262 alignment
= CHAR_BIT
;
1265 float_declaration
= float_declaration_new(mant_dig
, exp_dig
,
1266 byte_order
, alignment
);
1267 return &float_declaration
->p
;
1271 struct declaration
*ctf_declaration_string_visit(FILE *fd
, int depth
,
1272 struct cds_list_head
*expressions
,
1273 struct ctf_trace
*trace
)
1275 struct ctf_node
*expression
;
1276 const char *encoding_c
= NULL
;
1277 enum ctf_string_encoding encoding
= CTF_STRING_UTF8
;
1278 struct declaration_string
*string_declaration
;
1280 cds_list_for_each_entry(expression
, expressions
, siblings
) {
1281 struct ctf_node
*left
, *right
;
1283 left
= _cds_list_first_entry(&expression
->u
.ctf_expression
.left
, struct ctf_node
, siblings
);
1284 right
= _cds_list_first_entry(&expression
->u
.ctf_expression
.right
, struct ctf_node
, siblings
);
1285 assert(left
->u
.unary_expression
.type
== UNARY_STRING
);
1286 if (!strcmp(left
->u
.unary_expression
.u
.string
, "encoding")) {
1287 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
1288 fprintf(fd
, "[error] %s: encoding: expecting string\n",
1292 encoding_c
= right
->u
.unary_expression
.u
.string
;
1294 fprintf(fd
, "[error] %s: unknown attribute name %s\n",
1295 __func__
, left
->u
.unary_expression
.u
.string
);
1299 if (encoding_c
&& !strcmp(encoding_c
, "ASCII"))
1300 encoding
= CTF_STRING_ASCII
;
1301 string_declaration
= string_declaration_new(encoding
);
1302 return &string_declaration
->p
;
1307 struct declaration
*ctf_type_specifier_list_visit(FILE *fd
,
1308 int depth
, struct ctf_node
*type_specifier_list
,
1309 struct declaration_scope
*declaration_scope
,
1310 struct ctf_trace
*trace
)
1312 struct ctf_node
*first
;
1313 struct ctf_node
*node
;
1315 assert(type_specifier_list
->type
== NODE_TYPE_SPECIFIER_LIST
);
1317 first
= _cds_list_first_entry(&type_specifier_list
->u
.type_specifier_list
.head
, struct ctf_node
, siblings
);
1319 assert(first
->type
== NODE_TYPE_SPECIFIER
);
1321 node
= first
->u
.type_specifier
.node
;
1323 switch (first
->u
.type_specifier
.type
) {
1324 case TYPESPEC_FLOATING_POINT
:
1325 return ctf_declaration_floating_point_visit(fd
, depth
,
1326 &node
->u
.floating_point
.expressions
, trace
);
1327 case TYPESPEC_INTEGER
:
1328 return ctf_declaration_integer_visit(fd
, depth
,
1329 &node
->u
.integer
.expressions
, trace
);
1330 case TYPESPEC_STRING
:
1331 return ctf_declaration_string_visit(fd
, depth
,
1332 &node
->u
.string
.expressions
, trace
);
1333 case TYPESPEC_STRUCT
:
1334 return ctf_declaration_struct_visit(fd
, depth
,
1335 node
->u
._struct
.name
,
1336 &node
->u
._struct
.declaration_list
,
1337 node
->u
._struct
.has_body
,
1338 &node
->u
._struct
.min_align
,
1341 case TYPESPEC_VARIANT
:
1342 return ctf_declaration_variant_visit(fd
, depth
,
1343 node
->u
.variant
.name
,
1344 node
->u
.variant
.choice
,
1345 &node
->u
.variant
.declaration_list
,
1346 node
->u
.variant
.has_body
,
1350 return ctf_declaration_enum_visit(fd
, depth
,
1351 node
->u
._enum
.enum_id
,
1352 node
->u
._enum
.container_type
,
1353 &node
->u
._enum
.enumerator_list
,
1354 node
->u
._enum
.has_body
,
1360 case TYPESPEC_SHORT
:
1363 case TYPESPEC_FLOAT
:
1364 case TYPESPEC_DOUBLE
:
1365 case TYPESPEC_SIGNED
:
1366 case TYPESPEC_UNSIGNED
:
1368 case TYPESPEC_COMPLEX
:
1369 case TYPESPEC_IMAGINARY
:
1370 case TYPESPEC_CONST
:
1371 case TYPESPEC_ID_TYPE
:
1372 return ctf_declaration_type_specifier_visit(fd
, depth
,
1373 type_specifier_list
, declaration_scope
);
1375 fprintf(fd
, "[error] %s: unexpected node type %d\n", __func__
, (int) first
->u
.type_specifier
.type
);
1381 int ctf_event_declaration_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_event
*event
, struct ctf_trace
*trace
)
1385 switch (node
->type
) {
1387 ret
= ctf_typedef_visit(fd
, depth
+ 1,
1388 event
->declaration_scope
,
1389 node
->u
._typedef
.type_specifier_list
,
1390 &node
->u
._typedef
.type_declarators
,
1395 case NODE_TYPEALIAS
:
1396 ret
= ctf_typealias_visit(fd
, depth
+ 1,
1397 event
->declaration_scope
,
1398 node
->u
.typealias
.target
, node
->u
.typealias
.alias
,
1403 case NODE_CTF_EXPRESSION
:
1407 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
1408 if (!strcmp(left
, "name")) {
1411 if (CTF_EVENT_FIELD_IS_SET(event
, name
)) {
1412 fprintf(fd
, "[error] %s: name already declared in event declaration\n", __func__
);
1416 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
1418 fprintf(fd
, "[error] %s: unexpected unary expression for event name\n", __func__
);
1422 event
->name
= g_quark_from_string(right
);
1424 CTF_EVENT_SET_FIELD(event
, name
);
1425 } else if (!strcmp(left
, "id")) {
1426 if (CTF_EVENT_FIELD_IS_SET(event
, id
)) {
1427 fprintf(fd
, "[error] %s: id already declared in event declaration\n", __func__
);
1431 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &event
->id
);
1433 fprintf(fd
, "[error] %s: unexpected unary expression for event id\n", __func__
);
1437 CTF_EVENT_SET_FIELD(event
, id
);
1438 } else if (!strcmp(left
, "stream_id")) {
1439 if (CTF_EVENT_FIELD_IS_SET(event
, stream_id
)) {
1440 fprintf(fd
, "[error] %s: stream_id already declared in event declaration\n", __func__
);
1444 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &event
->stream_id
);
1446 fprintf(fd
, "[error] %s: unexpected unary expression for event stream_id\n", __func__
);
1450 event
->stream
= trace_stream_lookup(trace
, event
->stream_id
);
1451 if (!event
->stream
) {
1452 fprintf(fd
, "[error] %s: stream id %" PRIu64
" cannot be found\n", __func__
, event
->stream_id
);
1456 CTF_EVENT_SET_FIELD(event
, stream_id
);
1457 } else if (!strcmp(left
, "context")) {
1458 struct declaration
*declaration
;
1460 if (event
->context_decl
) {
1461 fprintf(fd
, "[error] %s: context already declared in event declaration\n", __func__
);
1465 declaration
= ctf_type_specifier_list_visit(fd
, depth
,
1466 _cds_list_first_entry(&node
->u
.ctf_expression
.right
,
1467 struct ctf_node
, siblings
),
1468 event
->declaration_scope
, trace
);
1473 if (declaration
->id
!= CTF_TYPE_STRUCT
) {
1477 event
->context_decl
= container_of(declaration
, struct declaration_struct
, p
);
1478 } else if (!strcmp(left
, "fields")) {
1479 struct declaration
*declaration
;
1481 if (event
->fields_decl
) {
1482 fprintf(fd
, "[error] %s: fields already declared in event declaration\n", __func__
);
1486 declaration
= ctf_type_specifier_list_visit(fd
, depth
,
1487 _cds_list_first_entry(&node
->u
.ctf_expression
.right
,
1488 struct ctf_node
, siblings
),
1489 event
->declaration_scope
, trace
);
1494 if (declaration
->id
!= CTF_TYPE_STRUCT
) {
1498 event
->fields_decl
= container_of(declaration
, struct declaration_struct
, p
);
1506 /* TODO: declaration specifier should be added. */
1513 int ctf_event_visit(FILE *fd
, int depth
, struct ctf_node
*node
,
1514 struct declaration_scope
*parent_declaration_scope
, struct ctf_trace
*trace
)
1517 struct ctf_node
*iter
;
1518 struct ctf_event
*event
;
1519 struct definition_scope
*parent_def_scope
;
1521 event
= g_new0(struct ctf_event
, 1);
1522 event
->declaration_scope
= new_declaration_scope(parent_declaration_scope
);
1523 cds_list_for_each_entry(iter
, &node
->u
.event
.declaration_list
, siblings
) {
1524 ret
= ctf_event_declaration_visit(fd
, depth
+ 1, iter
, event
, trace
);
1528 if (!CTF_EVENT_FIELD_IS_SET(event
, name
)) {
1530 fprintf(fd
, "[error] %s: missing name field in event declaration\n", __func__
);
1533 if (!CTF_EVENT_FIELD_IS_SET(event
, stream_id
)) {
1534 /* Allow missing stream_id if there is only a single stream */
1535 switch (trace
->streams
->len
) {
1536 case 0: /* Create stream if there was none. */
1537 ret
= ctf_stream_visit(fd
, depth
, NULL
, trace
->root_declaration_scope
, trace
);
1542 event
->stream_id
= 0;
1543 event
->stream
= trace_stream_lookup(trace
, event
->stream_id
);
1547 fprintf(fd
, "[error] %s: missing stream_id field in event declaration\n", __func__
);
1551 /* Allow only one event without id per stream */
1552 if (!CTF_EVENT_FIELD_IS_SET(event
, id
)
1553 && event
->stream
->events_by_id
->len
!= 0) {
1555 fprintf(fd
, "[error] %s: missing id field in event declaration\n", __func__
);
1558 if (event
->stream
->events_by_id
->len
<= event
->id
)
1559 g_ptr_array_set_size(event
->stream
->events_by_id
, event
->id
+ 1);
1560 g_ptr_array_index(event
->stream
->events_by_id
, event
->id
) = event
;
1561 g_hash_table_insert(event
->stream
->event_quark_to_id
,
1562 (gpointer
)(unsigned long) event
->name
,
1564 parent_def_scope
= event
->stream
->definition_scope
;
1565 if (event
->context_decl
) {
1568 event
->context_decl
->p
.definition_new(&event
->context_decl
->p
,
1569 parent_def_scope
, 0, 0),
1570 struct definition_struct
, p
);
1571 set_dynamic_definition_scope(&event
->context
->p
,
1572 event
->context
->scope
,
1574 parent_def_scope
= event
->context
->scope
;
1575 declaration_unref(&event
->context_decl
->p
);
1577 if (event
->fields_decl
) {
1580 event
->fields_decl
->p
.definition_new(&event
->fields_decl
->p
,
1581 parent_def_scope
, 0, 0),
1582 struct definition_struct
, p
);
1583 set_dynamic_definition_scope(&event
->fields
->p
,
1584 event
->fields
->scope
,
1586 parent_def_scope
= event
->fields
->scope
;
1587 declaration_unref(&event
->fields_decl
->p
);
1592 declaration_unref(&event
->fields_decl
->p
);
1593 declaration_unref(&event
->context_decl
->p
);
1594 free_declaration_scope(event
->declaration_scope
);
1601 int ctf_stream_declaration_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_stream_class
*stream
, struct ctf_trace
*trace
)
1605 switch (node
->type
) {
1607 ret
= ctf_typedef_visit(fd
, depth
+ 1,
1608 stream
->declaration_scope
,
1609 node
->u
._typedef
.type_specifier_list
,
1610 &node
->u
._typedef
.type_declarators
,
1615 case NODE_TYPEALIAS
:
1616 ret
= ctf_typealias_visit(fd
, depth
+ 1,
1617 stream
->declaration_scope
,
1618 node
->u
.typealias
.target
, node
->u
.typealias
.alias
,
1623 case NODE_CTF_EXPRESSION
:
1627 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
1628 if (!strcmp(left
, "id")) {
1629 if (CTF_STREAM_FIELD_IS_SET(stream
, stream_id
)) {
1630 fprintf(fd
, "[error] %s: id already declared in stream declaration\n", __func__
);
1634 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &stream
->stream_id
);
1636 fprintf(fd
, "[error] %s: unexpected unary expression for stream id\n", __func__
);
1640 CTF_STREAM_SET_FIELD(stream
, stream_id
);
1641 } else if (!strcmp(left
, "event.header")) {
1642 struct declaration
*declaration
;
1644 if (stream
->event_header_decl
) {
1645 fprintf(fd
, "[error] %s: event.header already declared in stream declaration\n", __func__
);
1649 declaration
= ctf_type_specifier_list_visit(fd
, depth
,
1650 _cds_list_first_entry(&node
->u
.ctf_expression
.right
,
1651 struct ctf_node
, siblings
),
1652 stream
->declaration_scope
, trace
);
1657 if (declaration
->id
!= CTF_TYPE_STRUCT
) {
1661 stream
->event_header_decl
= container_of(declaration
, struct declaration_struct
, p
);
1662 } else if (!strcmp(left
, "event.context")) {
1663 struct declaration
*declaration
;
1665 if (stream
->event_context_decl
) {
1666 fprintf(fd
, "[error] %s: event.context already declared in stream declaration\n", __func__
);
1670 declaration
= ctf_type_specifier_list_visit(fd
, depth
,
1671 _cds_list_first_entry(&node
->u
.ctf_expression
.right
,
1672 struct ctf_node
, siblings
),
1673 stream
->declaration_scope
, trace
);
1678 if (declaration
->id
!= CTF_TYPE_STRUCT
) {
1682 stream
->event_context_decl
= container_of(declaration
, struct declaration_struct
, p
);
1683 } else if (!strcmp(left
, "packet.context")) {
1684 struct declaration
*declaration
;
1686 if (stream
->packet_context_decl
) {
1687 fprintf(fd
, "[error] %s: packet.context already declared in stream declaration\n", __func__
);
1691 declaration
= ctf_type_specifier_list_visit(fd
, depth
,
1692 _cds_list_first_entry(&node
->u
.ctf_expression
.right
,
1693 struct ctf_node
, siblings
),
1694 stream
->declaration_scope
, trace
);
1699 if (declaration
->id
!= CTF_TYPE_STRUCT
) {
1703 stream
->packet_context_decl
= container_of(declaration
, struct declaration_struct
, p
);
1711 /* TODO: declaration specifier should be added. */
1718 int ctf_stream_visit(FILE *fd
, int depth
, struct ctf_node
*node
,
1719 struct declaration_scope
*parent_declaration_scope
, struct ctf_trace
*trace
)
1722 struct ctf_node
*iter
;
1723 struct ctf_stream_class
*stream
;
1724 struct definition_scope
*parent_def_scope
;
1726 stream
= g_new0(struct ctf_stream_class
, 1);
1727 stream
->declaration_scope
= new_declaration_scope(parent_declaration_scope
);
1728 stream
->events_by_id
= g_ptr_array_new();
1729 stream
->event_quark_to_id
= g_hash_table_new(g_direct_hash
, g_direct_equal
);
1730 stream
->files
= g_ptr_array_new();
1732 cds_list_for_each_entry(iter
, &node
->u
.stream
.declaration_list
, siblings
) {
1733 ret
= ctf_stream_declaration_visit(fd
, depth
+ 1, iter
, stream
, trace
);
1738 if (CTF_STREAM_FIELD_IS_SET(stream
, stream_id
)) {
1739 /* check that packet header has stream_id field. */
1740 if (!trace
->packet_header_decl
1741 || struct_declaration_lookup_field_index(trace
->packet_header_decl
, g_quark_from_static_string("stream_id")) < 0) {
1743 fprintf(fd
, "[error] %s: missing stream_id field in packet header declaration, but stream_id attribute is declared for stream.\n", __func__
);
1747 /* Allow only one id-less stream */
1748 if (trace
->streams
->len
!= 0) {
1750 fprintf(fd
, "[error] %s: missing id field in stream declaration\n", __func__
);
1753 stream
->stream_id
= 0;
1755 if (trace
->streams
->len
<= stream
->stream_id
)
1756 g_ptr_array_set_size(trace
->streams
, stream
->stream_id
+ 1);
1757 g_ptr_array_index(trace
->streams
, stream
->stream_id
) = stream
;
1759 parent_def_scope
= trace
->definition_scope
;
1760 if (stream
->packet_context_decl
) {
1761 stream
->packet_context
=
1763 stream
->packet_context_decl
->p
.definition_new(&stream
->packet_context_decl
->p
,
1764 parent_def_scope
, 0, 0),
1765 struct definition_struct
, p
);
1766 set_dynamic_definition_scope(&stream
->packet_context
->p
,
1767 stream
->packet_context
->scope
,
1768 "stream.packet.context");
1769 parent_def_scope
= stream
->packet_context
->scope
;
1770 declaration_unref(&stream
->packet_context_decl
->p
);
1772 if (stream
->event_header_decl
) {
1773 stream
->event_header
=
1775 stream
->event_header_decl
->p
.definition_new(&stream
->event_header_decl
->p
,
1776 parent_def_scope
, 0, 0),
1777 struct definition_struct
, p
);
1778 set_dynamic_definition_scope(&stream
->event_header
->p
,
1779 stream
->event_header
->scope
,
1780 "stream.event.header");
1781 parent_def_scope
= stream
->event_header
->scope
;
1782 declaration_unref(&stream
->event_header_decl
->p
);
1784 if (stream
->event_context_decl
) {
1785 stream
->event_context
=
1787 stream
->event_context_decl
->p
.definition_new(&stream
->event_context_decl
->p
,
1788 parent_def_scope
, 0, 0),
1789 struct definition_struct
, p
);
1790 set_dynamic_definition_scope(&stream
->event_context
->p
,
1791 stream
->event_context
->scope
,
1792 "stream.event.context");
1793 parent_def_scope
= stream
->event_context
->scope
;
1794 declaration_unref(&stream
->event_context_decl
->p
);
1796 stream
->definition_scope
= parent_def_scope
;
1801 declaration_unref(&stream
->event_header_decl
->p
);
1802 declaration_unref(&stream
->event_context_decl
->p
);
1803 declaration_unref(&stream
->packet_context_decl
->p
);
1804 g_ptr_array_free(stream
->files
, TRUE
);
1805 g_ptr_array_free(stream
->events_by_id
, TRUE
);
1806 g_hash_table_destroy(stream
->event_quark_to_id
);
1807 free_declaration_scope(stream
->declaration_scope
);
1813 int ctf_trace_declaration_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_trace
*trace
)
1817 switch (node
->type
) {
1819 ret
= ctf_typedef_visit(fd
, depth
+ 1,
1820 trace
->declaration_scope
,
1821 node
->u
._typedef
.type_specifier_list
,
1822 &node
->u
._typedef
.type_declarators
,
1827 case NODE_TYPEALIAS
:
1828 ret
= ctf_typealias_visit(fd
, depth
+ 1,
1829 trace
->declaration_scope
,
1830 node
->u
.typealias
.target
, node
->u
.typealias
.alias
,
1835 case NODE_CTF_EXPRESSION
:
1839 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
1840 if (!strcmp(left
, "major")) {
1841 if (CTF_TRACE_FIELD_IS_SET(trace
, major
)) {
1842 fprintf(fd
, "[error] %s: major already declared in trace declaration\n", __func__
);
1846 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &trace
->major
);
1848 fprintf(fd
, "[error] %s: unexpected unary expression for trace major number\n", __func__
);
1852 CTF_TRACE_SET_FIELD(trace
, major
);
1853 } else if (!strcmp(left
, "minor")) {
1854 if (CTF_TRACE_FIELD_IS_SET(trace
, minor
)) {
1855 fprintf(fd
, "[error] %s: minor already declared in trace declaration\n", __func__
);
1859 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &trace
->minor
);
1861 fprintf(fd
, "[error] %s: unexpected unary expression for trace minor number\n", __func__
);
1865 CTF_TRACE_SET_FIELD(trace
, minor
);
1866 } else if (!strcmp(left
, "uuid")) {
1867 if (CTF_TRACE_FIELD_IS_SET(trace
, uuid
)) {
1868 fprintf(fd
, "[error] %s: uuid already declared in trace declaration\n", __func__
);
1872 ret
= get_unary_uuid(&node
->u
.ctf_expression
.right
, &trace
->uuid
);
1874 fprintf(fd
, "[error] %s: unexpected unary expression for trace uuid\n", __func__
);
1878 CTF_TRACE_SET_FIELD(trace
, uuid
);
1879 } else if (!strcmp(left
, "byte_order")) {
1880 struct ctf_node
*right
;
1883 if (CTF_TRACE_FIELD_IS_SET(trace
, byte_order
)) {
1884 fprintf(fd
, "[error] %s: endianness already declared in trace declaration\n", __func__
);
1888 right
= _cds_list_first_entry(&node
->u
.ctf_expression
.right
, struct ctf_node
, siblings
);
1889 byte_order
= get_trace_byte_order(fd
, depth
, right
);
1892 trace
->byte_order
= byte_order
;
1893 CTF_TRACE_SET_FIELD(trace
, byte_order
);
1894 } else if (!strcmp(left
, "packet.header")) {
1895 struct declaration
*declaration
;
1897 if (trace
->packet_header_decl
) {
1898 fprintf(fd
, "[error] %s: packet.header already declared in trace declaration\n", __func__
);
1902 declaration
= ctf_type_specifier_list_visit(fd
, depth
,
1903 _cds_list_first_entry(&node
->u
.ctf_expression
.right
,
1904 struct ctf_node
, siblings
),
1905 trace
->declaration_scope
, trace
);
1910 if (declaration
->id
!= CTF_TYPE_STRUCT
) {
1914 trace
->packet_header_decl
= container_of(declaration
, struct declaration_struct
, p
);
1922 /* TODO: declaration specifier should be added. */
1929 int ctf_trace_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_trace
*trace
)
1931 struct definition_scope
*parent_def_scope
;
1933 struct ctf_node
*iter
;
1935 if (trace
->declaration_scope
)
1937 trace
->declaration_scope
= new_declaration_scope(trace
->root_declaration_scope
);
1938 trace
->streams
= g_ptr_array_new();
1939 cds_list_for_each_entry(iter
, &node
->u
.trace
.declaration_list
, siblings
) {
1940 ret
= ctf_trace_declaration_visit(fd
, depth
+ 1, iter
, trace
);
1944 if (!CTF_TRACE_FIELD_IS_SET(trace
, major
)) {
1946 fprintf(fd
, "[error] %s: missing major field in trace declaration\n", __func__
);
1949 if (!CTF_TRACE_FIELD_IS_SET(trace
, minor
)) {
1951 fprintf(fd
, "[error] %s: missing minor field in trace declaration\n", __func__
);
1954 if (!CTF_TRACE_FIELD_IS_SET(trace
, uuid
)) {
1956 fprintf(fd
, "[error] %s: missing uuid field in trace declaration\n", __func__
);
1959 if (!CTF_TRACE_FIELD_IS_SET(trace
, byte_order
)) {
1961 fprintf(fd
, "[error] %s: missing byte_order field in trace declaration\n", __func__
);
1965 parent_def_scope
= NULL
;
1966 if (trace
->packet_header_decl
) {
1967 trace
->packet_header
=
1969 trace
->packet_header_decl
->p
.definition_new(&trace
->packet_header_decl
->p
,
1970 parent_def_scope
, 0, 0),
1971 struct definition_struct
, p
);
1972 set_dynamic_definition_scope(&trace
->packet_header
->p
,
1973 trace
->packet_header
->scope
,
1974 "trace.packet.header");
1975 parent_def_scope
= trace
->packet_header
->scope
;
1976 declaration_unref(&trace
->packet_header_decl
->p
);
1978 trace
->definition_scope
= parent_def_scope
;
1980 if (!CTF_TRACE_FIELD_IS_SET(trace
, byte_order
)) {
1981 /* check that the packet header contains a "magic" field */
1982 if (!trace
->packet_header
1983 || struct_declaration_lookup_field_index(trace
->packet_header_decl
, g_quark_from_static_string("magic")) < 0) {
1985 fprintf(fd
, "[error] %s: missing both byte_order and packet header magic number in trace declaration\n", __func__
);
1986 goto error_free_def
;
1992 definition_unref(&trace
->packet_header
->p
);
1994 g_ptr_array_free(trace
->streams
, TRUE
);
1995 free_declaration_scope(trace
->declaration_scope
);
2000 int ctf_root_declaration_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_trace
*trace
)
2004 switch (node
->type
) {
2006 ret
= ctf_typedef_visit(fd
, depth
+ 1,
2007 trace
->root_declaration_scope
,
2008 node
->u
._typedef
.type_specifier_list
,
2009 &node
->u
._typedef
.type_declarators
,
2014 case NODE_TYPEALIAS
:
2015 ret
= ctf_typealias_visit(fd
, depth
+ 1,
2016 trace
->root_declaration_scope
,
2017 node
->u
.typealias
.target
, node
->u
.typealias
.alias
,
2022 case NODE_TYPE_SPECIFIER_LIST
:
2024 struct declaration
*declaration
;
2027 * Just add the type specifier to the root scope
2028 * declaration scope. Release local reference.
2030 declaration
= ctf_type_specifier_list_visit(fd
, depth
+ 1,
2031 node
, trace
->root_declaration_scope
, trace
);
2034 declaration_unref(declaration
);
2044 int ctf_visitor_construct_metadata(FILE *fd
, int depth
, struct ctf_node
*node
,
2045 struct ctf_trace
*trace
, int byte_order
)
2048 struct ctf_node
*iter
;
2050 printf_verbose("CTF visitor: metadata construction... ");
2051 trace
->root_declaration_scope
= new_declaration_scope(NULL
);
2052 trace
->streams
= g_ptr_array_new();
2053 trace
->byte_order
= byte_order
;
2055 switch (node
->type
) {
2057 cds_list_for_each_entry(iter
, &node
->u
.root
.declaration_list
,
2059 ret
= ctf_root_declaration_visit(fd
, depth
+ 1, iter
, trace
);
2061 fprintf(fd
, "[error] %s: root declaration error\n", __func__
);
2065 cds_list_for_each_entry(iter
, &node
->u
.root
.trace
, siblings
) {
2066 ret
= ctf_trace_visit(fd
, depth
+ 1, iter
, trace
);
2068 fprintf(fd
, "[error] %s: trace declaration error\n", __func__
);
2072 if (!trace
->streams
) {
2073 fprintf(fd
, "[error] %s: missing trace declaration\n", __func__
);
2076 cds_list_for_each_entry(iter
, &node
->u
.root
.stream
, siblings
) {
2077 ret
= ctf_stream_visit(fd
, depth
+ 1, iter
,
2078 trace
->root_declaration_scope
, trace
);
2080 fprintf(fd
, "[error] %s: stream declaration error\n", __func__
);
2084 cds_list_for_each_entry(iter
, &node
->u
.root
.event
, siblings
) {
2085 ret
= ctf_event_visit(fd
, depth
+ 1, iter
,
2086 trace
->root_declaration_scope
, trace
);
2088 fprintf(fd
, "[error] %s: event declaration error\n", __func__
);
2095 fprintf(fd
, "[error] %s: unknown node type %d\n", __func__
,
2099 printf_verbose("done.\n");