2 * ctf-visitor-generate-io-struct.c
4 * Common Trace Format Metadata Visitor (generate I/O structures).
6 * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
28 #include <babeltrace/list.h>
29 #include <uuid/uuid.h>
30 #include "ctf-scanner.h"
31 #include "ctf-parser.h"
34 #define fprintf_dbg(fd, fmt, args...) fprintf(fd, "%s: " fmt, __func__, ## args)
36 #define _cds_list_first_entry(ptr, type, member) \
37 cds_list_entry((ptr)->next, type, member)
40 struct declaration
*ctf_declaration_specifier_visit(FILE *fd
,
41 int depth
, struct cds_list_head
*head
,
42 struct declaration_scope
*declaration_scope
,
43 struct ctf_trace
*trace
);
46 * String returned must be freed by the caller using g_free.
49 char *concatenate_unary_strings(struct cds_list_head
*head
)
51 struct ctf_node
*node
;
56 cds_list_for_each_entry(node
, head
, siblings
) {
59 assert(node
->type
== NODE_UNARY_EXPRESSION
);
60 assert(node
->u
.unary_expression
.type
== UNARY_STRING
);
61 assert((node
->u
.unary_expression
.link
== UNARY_LINK_UNKNOWN
)
63 switch (node
->u
.unary_expression
.link
) {
65 g_string_append(str
, ".")
68 g_string_append(str
, "->")
71 g_string_append(str
, "...")
74 src_string
= u
.unary_expression
.u
.string
;
75 g_string_append(str
, src_string
);
78 return g_string_free(str
, FALSE
);
82 int get_unary_unsigned(struct cds_list_head
*head
, uint64_t *value
)
84 struct ctf_node
*node
;
87 cds_list_for_each_entry(node
, head
, siblings
) {
88 assert(node
->type
== NODE_UNARY_EXPRESSION
);
89 assert(node
->u
.unary_expression
.type
== UNARY_UNSIGNED_CONSTANT
);
90 assert(node
->u
.unary_expression
.link
== UNARY_LINK_UNKNOWN
);
92 *value
= node
->u
.unary_expression
.unsigned_constant
99 int get_unary_uuid(struct cds_list_head
*head
, uuid_t
*uuid
)
101 struct ctf_node
*node
;
105 cds_list_for_each_entry(node
, head
, siblings
) {
106 const char *src_string
;
108 assert(node
->type
== NODE_UNARY_EXPRESSION
);
109 assert(node
->u
.unary_expression
.type
== UNARY_STRING
);
110 assert(node
->u
.unary_expression
.link
== UNARY_LINK_UNKNOWN
);
112 src_string
= u
.unary_expression
.u
.string
;
113 ret
= uuid_parse(u
.unary_expression
.u
.string
, *uuid
);
119 struct ctf_stream
*trace_stream_lookup(struct ctf_trace
*trace
, uint64_t stream_id
)
121 if (trace
->streams
->len
<= stream_id
)
123 return g_ptr_array_index(trace
->streams
, stream_id
);
127 void visit_declaration_specifier(struct cds_list_head
*declaration_specifier
, GString
*str
)
129 struct ctf_node
*iter
;
130 int alias_item_nr
= 0;
133 cds_list_for_each_entry(iter
, declaration_specifier
, siblings
) {
134 if (alias_item_nr
!= 0)
135 g_string_append(str
, " ");
138 switch (iter
->type
) {
139 case NODE_TYPE_SPECIFIER
:
140 switch (iter
->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 (iter
->u
.type_specifier
.id_type
)
182 g_string_append(str
, iter
->u
.type_specifier
.id_type
);
185 fprintf(stderr
, "[error] %s: unknown specifier\n", __func__
);
191 if (!iter
->u
._enum
.enum_id
) {
192 fprintf(stderr
, "[error] %s: unexpected empty enum ID\n", __func__
);
196 g_string_append(str
, "enum ");
197 g_string_append(str
, iter
->u
._enum
.enum_id
);
200 if (!iter
->u
.variant
.name
) {
201 fprintf(stderr
, "[error] %s: unexpected empty variant name\n", __func__
);
205 g_string_append(str
, "variant ");
206 g_string_append(str
, iter
->u
.variant
.name
);
209 if (!iter
->u
._struct
.name
) {
210 fprintf(stderr
, "[error] %s: unexpected empty variant name\n", __func__
);
214 g_string_append(str
, "struct ");
215 g_string_append(str
, iter
->u
._struct
.name
);
218 fprintf(stderr
, "[error] %s: unexpected node type %d\n", __func__
, (int) iter
->type
);
229 GQuark
create_typealias_identifier(int fd
, int depth
,
230 struct cds_list_head
*declaration_specifier
,
231 struct ctf_node
*node_type_declarator
)
238 str
= g_string_new();
239 ret
= visit_declaration_specifier(declaration_specifier
, str
);
241 g_string_free(str
, TRUE
);
244 cds_list_for_each_entry(iter
, &node_type_declarator
->u
.type_declarator
.pointers
, siblings
) {
245 g_string_append(str
, " *");
246 if (iter
->u
.pointer
.const_qualifier
)
247 g_string_append(str
, " const");
249 str_c
= g_string_free(str
, FALSE
);
250 alias_q
= g_quark_from_string(str_c
);
256 struct declaration
*ctf_type_declarator_visit(int fd
, int depth
,
257 struct cds_list_head
*declaration_specifier
,
259 struct ctf_node
*node_type_declarator
,
260 struct declaration_scope
*declaration_scope
,
261 struct declaration
*nested_declaration
)
264 * Visit type declarator by first taking care of sequence/array
265 * (recursively). Then, when we get to the identifier, take care
269 assert(node_type_declarator
->u
.type_declarator
.type
!= TYPEDEC_UNKNOWN
);
271 /* TODO: gcc bitfields not supported yet. */
272 if (node_type_declarator
->u
.type_declarator
.bitfield_len
!= NULL
) {
273 fprintf(stderr
, "[error] %s: gcc bitfields are not supported yet.\n", __func__
);
277 if (!nested_declaration
) {
278 if (!cds_list_empty(&node_type_declarator
->u
.type_declarator
.pointers
)) {
282 * If we have a pointer declarator, it _has_ to be present in
283 * the typealiases (else fail).
285 alias_q
= create_typealias_identifier(fd
, depth
,
286 declaration_specifier
, node_type_declarator
);
287 nested_declaration
= lookup_declaration(alias_q
, declaration_scope
);
288 if (!nested_declaration
) {
289 fprintf(stderr
, "[error] %s: cannot find typealias \"%s\".\n", __func__
, g_quark_to_string(alias_q
));
293 nested_declaration
= /* parse declaration_specifier */;
297 if (node_type_declarator
->u
.type_declarator
.type
== TYPEDEC_ID
) {
298 if (node_type_declarator
->u
.type_declarator
.u
.id
)
299 *field_name
= g_quark_from_string(node_type_declarator
->u
.type_declarator
.u
.id
);
302 return nested_declaration
;
304 struct declaration
*declaration
;
309 /* create array/sequence, pass nested_declaration as child. */
310 length
= node_type_declarator
->u
.type_declarator
.u
.nested
.length
;
312 switch (length
->type
) {
313 case NODE_UNARY_EXPRESSION
:
317 declaration
= /* create array */;
319 case NODE_TYPE_SPECIFIER
:
321 declaration
= /* create sequence */;
328 /* Pass it as content of outer container */
329 declaration
= ctf_type_declarator_visit(fd
, depth
,
330 declaration_specifier
, field_name
,
331 node_type_declarator
->u
.type_declarator
.u
.nested
.type_declarator
,
332 declaration_scope
, declaration
);
338 int ctf_struct_type_declarators_visit(int fd
, int depth
,
339 struct declaration_struct
*struct_declaration
,
340 struct cds_list_head
*declaration_specifier
,
341 struct cds_list_head
*type_declarators
,
342 struct declaration_scope
*declaration_scope
)
344 struct ctf_node
*iter
;
347 cds_list_for_each_entry(iter
, type_declarators
, siblings
) {
348 struct declaration
*field_declaration
;
350 field_declaration
= ctf_type_declarator_visit(fd
, depth
,
351 declaration_specifier
,
353 struct_declaration
->scope
,
355 struct_declaration_add_field(struct_declaration
,
356 g_quark_to_string(field_name
),
363 int ctf_variant_type_declarators_visit(int fd
, int depth
,
364 struct declaration_variant
*variant_declaration
,
365 struct cds_list_head
*declaration_specifier
,
366 struct cds_list_head
*type_declarators
,
367 struct declaration_scope
*declaration_scope
)
369 struct ctf_node
*iter
;
372 cds_list_for_each_entry(iter
, type_declarators
, siblings
) {
373 struct declaration
*field_declaration
;
375 field_declaration
= ctf_type_declarator_visit(fd
, depth
,
376 declaration_specifier
,
378 variant_declaration
->scope
,
380 variant_declaration_add_field(variant_declaration
,
381 g_quark_to_string(field_name
),
388 int ctf_typedef_visit(int fd
, int depth
, struct declaration_scope
*scope
,
389 struct cds_list_head
*declaration_specifier
,
390 struct cds_list_head
*type_declarators
)
392 struct ctf_node
*iter
;
395 cds_list_for_each_entry(iter
, type_declarators
, siblings
) {
396 struct declaration
*type_declaration
;
399 type_declaration
= ctf_type_declarator_visit(fd
, depth
,
400 declaration_specifier
,
403 ret
= register_declaration(identifier
, type_declaration
, scope
);
405 type_declaration
->declaration_free(type_declaration
);
413 int ctf_typealias_visit(int fd
, int depth
, struct declaration_scope
*scope
,
414 struct ctf_node
*target
, struct ctf_node
*alias
)
416 struct declaration
*type_declaration
;
417 struct ctf_node
*iter
, *node
;
421 /* See ctf_visitor_type_declarator() in the semantic validator. */
424 * Create target type declaration.
427 type_declaration
= ctf_type_declarator_visit(fd
, depth
,
428 &target
->u
.typealias_target
.declaration_specifier
,
429 &dummy_id
, &target
->u
.typealias_target
.type_declarators
,
431 if (!type_declaration
) {
432 fprintf(stderr
, "[error] %s: problem creating type declaration\n", __func__
);
437 * The semantic validator does not check whether the target is
438 * abstract or not (if it has an identifier). Check it here.
441 fprintf(stderr
, "[error] %s: expecting empty identifier\n", __func__
);
446 * Create alias identifier.
449 node
= _cds_list_first_entry(&alias
->u
.typealias_alias
.type_declarators
,
450 struct node
, siblings
);
451 alias_q
= create_typealias_identifier(fd
, depth
,
452 &alias
->u
.typealias_alias
.declaration_specifier
, node
);
453 ret
= register_declaration(alias_q
, type_declaration
, scope
);
459 type_declaration
->declaration_free(type_declaration
);
464 int ctf_struct_declaration_list_visit(int fd
, int depth
,
465 struct ctf_node
*iter
, struct declaration_struct
*struct_declaration
)
467 struct declaration
*declaration
;
470 switch (iter
->type
) {
472 /* For each declarator, declare type and add type to struct declaration scope */
473 ret
= ctf_typedef_visit(fd
, depth
,
474 struct_declaration
->scope
,
475 &iter
->u
._typedef
.declaration_specifier
,
476 &iter
->u
._typedef
.type_declarators
);
481 /* Declare type with declarator and add type to struct declaration scope */
482 ret
= ctf_typealias_visit(fd
, depth
,
483 struct_declaration
->scope
,
484 iter
->u
.typealias
.target
,
485 iter
->u
.typealias
.alias
);
489 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
490 /* Add field to structure declaration */
491 ret
= ctf_struct_type_declarators_visit(fd
, depth
,
493 &iter
->u
.struct_or_variant_declaration
.declaration_specifier
,
494 &iter
->u
.struct_or_variant_declaration
.type_declarators
);
499 fprintf(stderr
, "[error] %s: unexpected node type %d\n", __func__
, (int) iter
->type
);
506 int ctf_variant_declaration_list_visit(int fd
, int depth
,
507 struct ctf_node
*iter
, struct declaration_variant
*variant_declaration
)
509 struct declaration
*declaration
;
512 switch (iter
->type
) {
514 /* For each declarator, declare type and add type to variant declaration scope */
515 ret
= ctf_typedef_visit(fd
, depth
,
516 variant_declaration
->scope
,
517 &iter
->u
._typedef
.declaration_specifier
,
518 &iter
->u
._typedef
.type_declarators
);
523 /* Declare type with declarator and add type to variant declaration scope */
524 ret
= ctf_typealias_visit(fd
, depth
,
525 variant_declaration
->scope
,
526 iter
->u
.typealias
.target
,
527 iter
->u
.typealias
.alias
);
531 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
532 /* Add field to structure declaration */
533 ret
= ctf_variant_type_declarators_visit(fd
, depth
,
535 &iter
->u
.struct_or_variant_declaration
.declaration_specifier
,
536 &iter
->u
.struct_or_variant_declaration
.type_declarators
);
541 fprintf(stderr
, "[error] %s: unexpected node type %d\n", __func__
, (int) iter
->type
);
548 struct declaration_struct
*ctf_declaration_struct_visit(FILE *fd
,
549 int depth
, const char *name
, struct cds_list_head
*declaration_list
,
550 int has_body
, struct declaration_scope
*declaration_scope
)
552 struct declaration
*declaration
;
553 struct declaration_struct
*struct_declaration
;
554 struct ctf_node
*iter
;
557 * For named struct (without body), lookup in
558 * declaration scope. Don't take reference on struct
559 * declaration: ref is only taken upon definition.
564 lookup_struct_declaration(g_quark_from_string(name
),
566 return struct_declaration
;
568 /* For unnamed struct, create type */
569 /* For named struct (with body), create type and add to declaration scope */
571 if (lookup_struct_declaration(g_quark_from_string(name
),
572 declaration_scope
)) {
574 fprintf(stderr
, "[error] %s: struct %s already declared in scope\n", __func__
, name
);
578 struct_declaration
= struct_declaration_new(name
, declaration_scope
);
579 cds_list_for_each_entry(iter
, declaration_list
, siblings
) {
580 ret
= ctf_struct_declaration_list_visit(fd
, depth
+ 1, iter
, struct_declaration
);
585 ret
= register_struct_declaration(g_quark_from_string(name
),
590 return struct_declaration
;
593 struct_declaration
->p
.declaration_free(&struct_declaration
->p
);
598 struct declaration_variant
*ctf_declaration_variant_visit(FILE *fd
,
599 int depth
, const char *name
, struct cds_list_head
*declaration_list
,
600 int has_body
, struct declaration_scope
*declaration_scope
)
602 struct declaration
*declaration
;
603 struct declaration_variant
*variant_declaration
;
604 struct ctf_node
*iter
;
607 * For named variant (without body), lookup in
608 * declaration scope. Don't take reference on variant
609 * declaration: ref is only taken upon definition.
613 variant_declaration
=
614 lookup_variant_declaration(g_quark_from_string(name
),
616 return variant_declaration
;
618 /* For unnamed variant, create type */
619 /* For named variant (with body), create type and add to declaration scope */
621 if (lookup_variant_declaration(g_quark_from_string(name
),
622 declaration_scope
)) {
624 fprintf(stderr
, "[error] %s: variant %s already declared in scope\n", __func__
, name
);
628 variant_declaration
= variant_declaration_new(name
, declaration_scope
);
629 cds_list_for_each_entry(iter
, declaration_list
, siblings
) {
630 ret
= ctf_variant_declaration_list_visit(fd
, depth
+ 1, iter
, variant_declaration
);
635 ret
= register_variant_declaration(g_quark_from_string(name
),
640 return variant_declaration
;
643 variant_declaration
->p
.declaration_free(&variant_declaration
->p
);
648 int ctf_enumerator_list_visit(int fd
, int depth
,
649 struct ctf_node
*enumerator
,
650 struct declaration_enum
*enum_declaration
)
658 struct declaration
*ctf_declaration_enum_visit(int fd
, int depth
,
660 struct ctf_node
*container_type
,
661 struct cds_list_head
*enumerator_list
,
664 struct declaration
*declaration
;
665 struct declaration_enum
*enum_declaration
;
666 struct declaration_integer
*integer_declaration
;
667 struct ctf_node
*iter
;
670 * For named enum (without body), lookup in
671 * declaration scope. Don't take reference on enum
672 * declaration: ref is only taken upon definition.
677 lookup_enum_declaration(g_quark_from_string(name
),
679 return enum_declaration
;
681 /* For unnamed enum, create type */
682 /* For named enum (with body), create type and add to declaration scope */
684 if (lookup_enum_declaration(g_quark_from_string(name
),
685 declaration_scope
)) {
687 fprintf(stderr
, "[error] %s: enum %s already declared in scope\n", __func__
, name
);
692 /* TODO CHECK Enumerations need to have their size/type specifier (< >). */
693 integer_declaration
= integer_declaration_new(); /* TODO ... */
695 enum_declaration
= enum_declaration_new(name
, integer_declaration
);
696 declaration_unref(&integer_declaration
->p
); /* leave ref to enum */
697 cds_list_for_each_entry(iter
, enumerator_list
, siblings
) {
698 ret
= ctf_enumerator_list_visit(fd
, depth
+ 1, iter
, enum_declaration
);
703 ret
= register_enum_declaration(g_quark_from_string(name
),
708 return enum_declaration
;
711 enum_declaration
->p
.declaration_free(&enum_declaration
->p
);
716 struct declaration
*ctf_declaration_type_specifier_visit(int fd
, int depth
,
717 struct cds_list_head
*declaration_specifier
,
718 struct declaration_scope
*declaration_scope
)
721 struct declaration
*declaration
;
724 str
= g_string_new();
725 ret
= visit_declaration_specifier(declaration_specifier
, str
);
728 str_c
= g_string_free(str
, FALSE
);
729 id_q
= g_quark_from_string(str_c
);
731 declaration
= lookup_declaration(id_q
, declaration_scope
);
736 * Returns 0/1 boolean, or < 0 on error.
739 int get_boolean(int fd
, int depth
, struct node
*unary_expression
)
741 if (unary_expression
->type
!= NODE_UNARY_EXPRESSION
) {
742 fprintf(stderr
, "[error] %s: expecting unary expression\n",
746 switch (unary_expression
->u
.unary_expression
.type
) {
747 case UNARY_UNSIGNED_CONSTANT
:
748 if (unary_expression
->u
.unary_expression
.u
.unsigned_constant
== 0)
752 case UNARY_SIGNED_CONSTANT
:
753 if (unary_expression
->u
.unary_expression
.u
.signed_constant
== 0)
758 if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "true"))
760 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "TRUE"))
762 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "false"))
764 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "FALSE"))
767 fprintf(stderr
, "[error] %s: unexpected string \"%s\"\n",
768 __func__
, unary_expression
->u
.unary_expression
.u
.string
);
773 fprintf(stderr
, "[error] %s: unexpected unary expression type\n",
781 int get_byte_order(int fd
, int depth
, struct node
*unary_expression
)
785 if (unary_expression
->u
.unary_expression
.type
!= UNARY_STRING
) {
786 fprintf(stderr
, "[error] %s: byte_order: expecting string\n",
790 if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "native"))
791 byte_order
= trace
->byte_order
;
792 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "network"))
793 byte_order
= BIG_ENDIAN
;
794 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "be"))
795 byte_order
= BIG_ENDIAN
;
796 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "le"))
797 byte_order
= LITTLE_ENDIAN
;
799 fprintf(stderr
, "[error] %s: unexpected string \"%s\". Should be \"native\", \"network\", \"be\" or \"le\".\n",
800 __func__
, right
->u
.unary_expression
.u
.string
);
807 struct declaration
*ctf_declaration_integer_visit(int fd
, int depth
,
808 struct cds_list_head
*expressions
,
809 struct ctf_trace
*trace
)
811 struct node
*expression
;
812 uint64_t alignment
, size
;
813 int byte_order
= trace
->byte_order
;
815 int has_alignment
= 0, has_size
= 0;
816 struct declaration_integer
*integer_declaration
;
818 cds_list_for_each_entry(expression
, expressions
, siblings
) {
819 struct node
*left
, *right
;
821 left
= expression
->u
.ctf_expression
.left
;
822 right
= expression
->u
.ctf_expression
.right
;
823 assert(left
->u
.unary_expression
.type
== UNARY_STRING
);
824 if (!strcmp(left
->u
.unary_expression
.u
.string
, "signed")) {
825 signedness
= get_boolean(fd
, depth
, right
);
828 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "byte_order")) {
829 byte_order
= get_byte_order(fd
, depth
, right
);
832 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "size")) {
833 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
834 fprintf(stderr
, "[error] %s: size: expecting unsigned constant\n",
838 size
= right
->u
.unary_expression
.u
.unsigned_constant
;
840 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "align")) {
841 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
842 fprintf(stderr
, "[error] %s: align: expecting unsigned constant\n",
846 alignment
= right
->u
.unary_expression
.u
.unsigned_constant
;
849 fprintf(stderr
, "[error] %s: unknown attribute name %s\n",
850 __func__
, left
->u
.unary_expression
.u
.string
);
855 fprintf(stderr
, "[error] %s: missing size attribute\n", __func__
);
858 if (!has_alignment
) {
859 if (size
% CHAR_BIT
) {
860 /* bit-packed alignment */
863 /* byte-packed alignment */
864 alignment
= CHAR_BIT
;
867 integer_declaration
= integer_declaration_new(size
,
868 byte_order
, signedness
, alignment
);
869 return &integer_declaration
->p
;
873 struct declaration
*ctf_declaration_floating_point_visit(int fd
, int depth
,
874 struct cds_list_head
*expressions
,
875 struct ctf_trace
*trace
)
877 struct node
*expression
;
878 uint64_t alignment
, exp_dig
, mant_dig
, byte_order
= trace
->byte_order
;
879 int has_alignment
= 0, has_exp_dig
= 0, has_mant_dig
= 0;
880 struct declaration_float
*float_declaration
;
882 cds_list_for_each_entry(expression
, expressions
, siblings
) {
883 struct node
*left
, *right
;
885 left
= expression
->u
.ctf_expression
.left
;
886 right
= expression
->u
.ctf_expression
.right
;
887 assert(left
->u
.unary_expression
.type
== UNARY_STRING
);
888 if (!strcmp(left
->u
.unary_expression
.u
.string
, "byte_order")) {
889 byte_order
= get_byte_order(fd
, depth
, right
);
892 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "exp_dig")) {
893 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
894 fprintf(stderr
, "[error] %s: exp_dig: expecting unsigned constant\n",
898 exp_dig
= right
->u
.unary_expression
.u
.unsigned_constant
;
900 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "mant_dig")) {
901 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
902 fprintf(stderr
, "[error] %s: mant_dig: expecting unsigned constant\n",
906 mant_dig
= right
->u
.unary_expression
.u
.unsigned_constant
;
908 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "align")) {
909 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
910 fprintf(stderr
, "[error] %s: align: expecting unsigned constant\n",
914 alignment
= right
->u
.unary_expression
.u
.unsigned_constant
;
917 fprintf(stderr
, "[error] %s: unknown attribute name %s\n",
918 __func__
, left
->u
.unary_expression
.u
.string
);
923 fprintf(stderr
, "[error] %s: missing mant_dig attribute\n", __func__
);
927 fprintf(stderr
, "[error] %s: missing exp_dig attribute\n", __func__
);
930 if (!has_alignment
) {
931 if ((mant_dig
+ exp_dig
) % CHAR_BIT
) {
932 /* bit-packed alignment */
935 /* byte-packed alignment */
936 alignment
= CHAR_BIT
;
939 float_declaration
= float_declaration_new(mant_dig
, exp_dig
,
940 byte_order
, alignment
);
941 return &float_declaration
->p
;
945 struct declaration
*ctf_declaration_string_visit(int fd
, int depth
,
946 struct cds_list_head
*expressions
,
947 struct ctf_trace
*trace
)
949 struct node
*expression
;
950 const char *encoding_c
= NULL
;
951 enum ctf_string_encoding encoding
= CTF_STRING_UTF8
;
952 struct declaration_string
*string_declaration
;
954 cds_list_for_each_entry(expression
, expressions
, siblings
) {
955 struct node
*left
, *right
;
957 left
= expression
->u
.ctf_expression
.left
;
958 right
= expression
->u
.ctf_expression
.right
;
959 assert(left
->u
.unary_expression
.type
== UNARY_STRING
);
960 if (!strcmp(left
->u
.unary_expression
.u
.string
, "encoding")) {
961 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_STRING
) {
962 fprintf(stderr
, "[error] %s: encoding: expecting string\n",
966 encoding_c
= right
->u
.unary_expression
.u
.string
;
968 fprintf(stderr
, "[error] %s: unknown attribute name %s\n",
969 __func__
, left
->u
.unary_expression
.u
.string
);
973 if (encoding_c
&& !strcmp(encoding_c
, "ASCII"))
974 encoding
= CTF_STRING_ASCII
;
975 string_declaration
= string_declaration_new(encoding
);
976 return &string_declaration
->p
;
981 * Also add named variant, struct or enum to the current declaration scope.
984 struct declaration
*ctf_declaration_specifier_visit(FILE *fd
,
985 int depth
, struct cds_list_head
*head
,
986 struct declaration_scope
*declaration_scope
,
987 struct ctf_trace
*trace
)
989 struct declaration
*declaration
;
992 first
= _cds_list_first_entry(head
, struct node
, siblings
);
994 switch (first
->type
) {
996 return ctf_declaration_struct_visit(fd
, depth
,
997 first
->u
._struct
.name
,
998 &first
->u
._struct
.declaration_list
,
999 first
->u
._struct
.has_body
,
1003 return ctf_declaration_variant_visit(fd
, depth
,
1004 first
->u
.variant
.name
,
1005 &first
->u
.variant
.declaration_list
,
1006 first
->u
.variant
.has_body
,
1010 return ctf_declaration_enum_visit(fd
, depth
,
1011 first
->u
._enum
.enum_id
,
1012 first
->u
._enum
.container_type
,
1013 &first
->u
._enum
.enumerator_list
,
1014 first
->u
._enum
.has_body
,
1017 return ctf_declaration_integer_visit(fd
, depth
,
1018 &first
->u
.integer
.expressions
, trace
);
1019 case NODE_FLOATING_POINT
:
1020 return ctf_declaration_floating_point_visit(fd
, depth
,
1021 &first
->u
.floating_point
.expressions
, trace
);
1023 return ctf_declaration_string_visit(fd
, depth
,
1024 &first
->u
.string
.expressions
, trace
);
1025 case NODE_TYPE_SPECIFIER
:
1026 return ctf_declaration_type_specifier_visit(fd
, depth
,
1027 head
, declaration_scope
, trace
);
1032 int ctf_event_declaration_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_event
*event
, struct ctf_trace
*trace
)
1036 switch (node
->type
) {
1038 ret
= ctf_typedef_visit(fd
, depth
+ 1,
1039 &node
->u
._typedef
.declaration_specifier
,
1040 &node
->u
._typedef
.type_declarators
,
1041 event
->declaration_scope
);
1045 case NODE_TYPEALIAS
:
1046 ret
= ctf_typealias_visit(fd
, depth
+ 1,
1047 &node
->u
.typealias
.target
, &node
->u
.typealias
.alias
1048 event
->declaration_scope
);
1052 case NODE_CTF_EXPRESSION
:
1056 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
1057 if (!strcmp(left
, "name")) {
1060 if (CTF_EVENT_FIELD_IS_SET(event
, name
))
1062 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
1064 fprintf(stderr
, "[error] %s: unexpected unary expression for event name\n", __func__
);
1067 event
->name
= g_quark_from_string(right
);
1069 CTF_EVENT_SET_FIELD(event
, name
);
1070 } else if (!strcmp(left
, "id")) {
1071 if (CTF_EVENT_FIELD_IS_SET(event
, id
))
1073 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &event
->id
);
1075 fprintf(stderr
, "[error] %s: unexpected unary expression for event id\n", __func__
);
1078 CTF_EVENT_SET_FIELD(event
, id
);
1079 } else if (!strcmp(left
, "stream_id")) {
1080 if (CTF_EVENT_FIELD_IS_SET(event
, stream_id
))
1082 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &event
->stream_id
);
1084 fprintf(stderr
, "[error] %s: unexpected unary expression for event stream_id\n", __func__
);
1087 event
->stream
= trace_stream_lookup(trace
, event
->stream_id
);
1088 if (!event
->stream
) {
1089 fprintf(stderr
, "[error] %s: stream id %" PRIu64
" cannot be found\n", __func__
, event
->stream_id
);
1092 CTF_EVENT_SET_FIELD(event
, stream_id
);
1093 } else if (!strcmp(left
, "context")) {
1094 struct declaration
*declaration
;
1096 if (!event
->definition_scope
)
1098 declaration
= ctf_declaration_specifier_visit(fd
, depth
,
1099 &node
->u
.ctf_expression
.right
,
1100 event
->declaration_scope
, trace
);
1103 if (declaration
->type
->id
!= CTF_TYPE_STRUCT
)
1105 event
->context_decl
= container_of(declaration
, struct declaration_struct
, p
);
1106 } else if (!strcmp(left
, "fields")) {
1107 struct declaration
*declaration
;
1109 if (!event
->definition_scope
)
1111 declaration
= ctf_declaration_specifier_visit(fd
, depth
,
1112 &node
->u
.ctf_expression
.right
,
1113 event
->declaration_scope
, trace
);
1116 if (declaration
->type
->id
!= CTF_TYPE_STRUCT
)
1118 event
->fields_decl
= container_of(declaration
, struct declaration_struct
, p
);
1125 /* TODO: declaration specifier should be added. */
1132 int ctf_event_visit(FILE *fd
, int depth
, struct ctf_node
*node
,
1133 struct declaration_scope
*parent_declaration_scope
, struct ctf_trace
*trace
)
1136 struct ctf_node
*iter
;
1137 struct ctf_event
*event
;
1138 struct definition_scope
*parent_def_scope
;
1140 event
= g_new0(struct ctf_event
, 1);
1141 event
->declaration_scope
= new_declaration_scope(parent_declaration_scope
);
1142 cds_list_for_each_entry(iter
, &node
->u
.event
.declaration_list
, siblings
) {
1143 ret
= ctf_event_declaration_visit(fd
, depth
+ 1, iter
, event
, trace
);
1147 if (!CTF_EVENT_FIELD_IS_SET(event
, name
)) {
1151 if (!CTF_EVENT_FIELD_IS_SET(event
, id
)) {
1155 if (!CTF_EVENT_FIELD_IS_SET(event
, stream_id
)) {
1159 if (event
->stream
->events_by_id
->len
<= event
->id
)
1160 g_ptr_array_set_size(event
->stream
->events_by_id
, event
->id
+ 1);
1161 g_ptr_array_index(event
->stream
->events_by_id
, event
->id
) = event
;
1162 g_hash_table_insert(event
->stream
->event_quark_to_id
,
1163 (gpointer
)(unsigned long) event
->name
,
1165 parent_def_scope
= stream
->definition_scope
;
1166 if (event
->context_decl
) {
1168 event
->context_decl
->definition_new(event
->context_decl
,
1169 parent_def_scope
, 0, 0);
1170 set_dynamic_definition_scope(&event
->context
->p
,
1171 event
->context
->scope
,
1173 parent_def_scope
= event
->context
->scope
;
1174 declaration_unref(event
->context_decl
);
1176 if (event
->fields_decl
) {
1178 event
->fields_decl
->definition_new(event
->fields_decl
,
1179 parent_def_scope
, 0, 0);
1180 set_dynamic_definition_scope(&event
->fields
->p
,
1181 event
->fields
->scope
,
1183 parent_def_scope
= event
->fields
->scope
;
1184 declaration_unref(event
->fields_decl
);
1189 declaration_unref(event
->fields_decl
);
1190 declaration_unref(event
->context_decl
);
1191 free_definition_scope(event
->definition_scope
);
1192 free_declaration_scope(event
->declaration_scope
);
1199 int ctf_stream_declaration_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_stream
*stream
, struct ctf_trace
*trace
)
1203 switch (node
->type
) {
1205 ret
= ctf_typedef_visit(fd
, depth
+ 1,
1206 &node
->u
._typedef
.declaration_specifier
,
1207 &node
->u
._typedef
.type_declarators
,
1208 stream
->declaration_scope
);
1212 case NODE_TYPEALIAS
:
1213 ret
= ctf_typealias_visit(fd
, depth
+ 1,
1214 &node
->u
.typealias
.target
, &node
->u
.typealias
.alias
1215 stream
->declaration_scope
);
1219 case NODE_CTF_EXPRESSION
:
1223 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
1224 if (!strcmp(left
, "stream_id")) {
1225 if (CTF_EVENT_FIELD_IS_SET(event
, stream_id
))
1227 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &event
->stream_id
);
1229 fprintf(stderr
, "[error] %s: unexpected unary expression for event stream_id\n", __func__
);
1232 CTF_EVENT_SET_FIELD(event
, stream_id
);
1233 } else if (!strcmp(left
, "event.header")) {
1234 struct declaration
*declaration
;
1236 declaration
= ctf_declaration_specifier_visit(fd
, depth
,
1237 &node
->u
.ctf_expression
.right
,
1238 stream
->declaration_scope
, stream
->definition_scope
, trace
);
1241 if (declaration
->type
->id
!= CTF_TYPE_STRUCT
)
1243 stream
->event_header_decl
= container_of(declaration
, struct declaration_struct
, p
);
1244 } else if (!strcmp(left
, "event.context")) {
1245 struct declaration
*declaration
;
1247 declaration
= ctf_declaration_specifier_visit(fd
, depth
,
1248 &node
->u
.ctf_expression
.right
,
1249 stream
->declaration_scope
, trace
);
1252 if (declaration
->type
->id
!= CTF_TYPE_STRUCT
)
1254 stream
->event_context_decl
= container_of(declaration
, struct declaration_struct
, p
);
1255 } else if (!strcmp(left
, "packet.context")) {
1256 struct declaration
*declaration
;
1258 declaration
= ctf_declaration_specifier_visit(fd
, depth
,
1259 &node
->u
.ctf_expression
.right
,
1260 stream
->declaration_scope
, trace
);
1263 if (declaration
->type
->id
!= CTF_TYPE_STRUCT
)
1265 stream
->packet_context_decl
= container_of(declaration
, struct declaration_struct
, p
);
1272 /* TODO: declaration specifier should be added. */
1279 int ctf_stream_visit(FILE *fd
, int depth
, struct ctf_node
*node
,
1280 struct declaration_scope
*parent_declaration_scope
, struct ctf_trace
*trace
)
1283 struct ctf_node
*iter
;
1284 struct ctf_stream
*stream
;
1285 struct definition_scope
*parent_def_scope
;
1287 stream
= g_new0(struct ctf_stream
, 1);
1288 stream
->declaration_scope
= new_declaration_scope(parent_declaration_scope
);
1289 stream
->events_by_id
= g_ptr_array_new();
1290 stream
->event_quark_to_id
= g_hash_table_new(g_int_hash
, g_int_equal
);
1291 cds_list_for_each_entry(iter
, &node
->u
.stream
.declaration_list
, siblings
) {
1292 ret
= ctf_stream_declaration_visit(fd
, depth
+ 1, iter
, stream
, trace
);
1296 if (!CTF_EVENT_FIELD_IS_SET(stream
, stream_id
)) {
1300 if (trace
->streams
->len
<= stream
->stream_id
)
1301 g_ptr_array_set_size(trace
->streams
, stream
->stream_id
+ 1);
1302 g_ptr_array_index(trace
->streams
, stream
->stream_id
) = stream
;
1304 parent_def_scope
= NULL
;
1305 if (stream
->packet_context_decl
) {
1306 stream
->packet_context
=
1307 stream
->packet_context_decl
->definition_new(stream
->packet_context_decl
,
1308 parent_def_scope
, 0, 0);
1309 set_dynamic_definition_scope(&stream
->packet_context
->p
,
1310 stream
->packet_context
->scope
,
1311 "stream.packet.context");
1312 parent_def_scope
= stream
->packet_context
->scope
;
1313 declaration_unref(stream
->packet_context_decl
);
1315 if (stream
->event_header_decl
) {
1316 stream
->event_header
=
1317 stream
->event_header_decl
->definition_new(stream
->event_header_decl
,
1318 parent_def_scope
, 0, 0);
1319 set_dynamic_definition_scope(&stream
->event_header
->p
,
1320 stream
->event_header
->scope
,
1321 "stream.event.header");
1322 parent_def_scope
= stream
->event_header
->scope
;
1323 declaration_unref(stream
->event_header_decl
);
1325 if (stream
->event_context_decl
) {
1326 stream
->event_context
=
1327 stream
->event_context_decl
->definition_new(stream
->event_context_decl
,
1328 parent_def_scope
, 0, 0);
1329 set_dynamic_definition_scope(&stream
->event_context
->p
,
1330 stream
->event_context
->scope
,
1331 "stream.event.context");
1332 parent_def_scope
= stream
->event_context
->scope
;
1333 declaration_unref(stream
->event_context_decl
);
1335 stream
->definition_scope
= parent_def_scope
;
1340 declaration_unref(stream
->event_header
);
1341 declaration_unref(stream
->event_context
);
1342 declaration_unref(stream
->packet_context
);
1343 g_ptr_array_free(stream
->events_by_id
, TRUE
);
1344 g_hash_table_free(stream
->event_quark_to_id
);
1345 free_definition_scope(stream
->definition_scope
);
1346 free_declaration_scope(stream
->declaration_scope
);
1352 int ctf_trace_declaration_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_trace
*trace
)
1356 switch (node
->type
) {
1358 ret
= ctf_typedef_visit(fd
, depth
+ 1,
1359 &node
->u
._typedef
.declaration_specifier
,
1360 &node
->u
._typedef
.type_declarators
,
1361 trace
->declaration_scope
);
1365 case NODE_TYPEALIAS
:
1366 ret
= ctf_typealias_visit(fd
, depth
+ 1,
1367 &node
->u
.typealias
.target
, &node
->u
.typealias
.alias
1368 trace
->declaration_scope
);
1372 case NODE_CTF_EXPRESSION
:
1376 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
1377 if (!strcmp(left
, "major")) {
1378 if (CTF_EVENT_FIELD_IS_SET(trace
, major
))
1380 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &trace
->major
);
1382 fprintf(stderr
, "[error] %s: unexpected unary expression for trace major number\n", __func__
);
1385 CTF_EVENT_SET_FIELD(trace
, major
);
1386 } else if (!strcmp(left
, "minor")) {
1387 if (CTF_EVENT_FIELD_IS_SET(trace
, minor
))
1389 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &trace
->minor
);
1391 fprintf(stderr
, "[error] %s: unexpected unary expression for trace minor number\n", __func__
);
1394 CTF_EVENT_SET_FIELD(trace
, minor
);
1395 } else if (!strcmp(left
, "word_size")) {
1396 if (CTF_EVENT_FIELD_IS_SET(trace
, word_size
))
1398 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &trace
->word_size
);
1400 fprintf(stderr
, "[error] %s: unexpected unary expression for trace word_size\n", __func__
);
1403 CTF_EVENT_SET_FIELD(trace
, word_size
);
1404 } else if (!strcmp(left
, "uuid")) {
1405 if (CTF_EVENT_FIELD_IS_SET(trace
, uuid
))
1407 ret
= get_unary_uuid(&node
->u
.ctf_expression
.right
, &trace
->uuid
);
1409 fprintf(stderr
, "[error] %s: unexpected unary expression for trace uuid\n", __func__
);
1412 CTF_EVENT_SET_FIELD(trace
, uuid
);
1419 /* TODO: declaration specifier should be added. */
1426 int ctf_trace_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_trace
*trace
)
1429 struct ctf_node
*iter
;
1431 if (trace
->declaration_scope
)
1433 trace
->declaration_scope
= new_declaration_scope(trace
->root_declaration_scope
);
1434 trace
->definition_scope
= new_dynamic_definition_scope(trace
->root_definition_scope
);
1435 trace
->streams
= g_ptr_array_new();
1436 cds_list_for_each_entry(iter
, &node
->u
.trace
.declaration_list
, siblings
) {
1437 ret
= ctf_trace_declaration_visit(fd
, depth
+ 1, iter
, trace
);
1441 if (!CTF_EVENT_FIELD_IS_SET(trace
, major
)) {
1445 if (!CTF_EVENT_FIELD_IS_SET(trace
, minor
)) {
1449 if (!CTF_EVENT_FIELD_IS_SET(trace
, uuid
)) {
1453 if (!CTF_EVENT_FIELD_IS_SET(trace
, word_size
)) {
1460 g_ptr_array_free(trace
->streams
, TRUE
);
1461 free_definition_scope(stream
->definition_scope
);
1462 free_declaration_scope(stream
->declaration_scope
);
1466 int ctf_visitor_construct_metadata(FILE *fd
, int depth
, struct ctf_node
*node
,
1467 struct ctf_trace
*trace
, int byte_order
)
1470 struct ctf_node
*iter
;
1472 trace
->byte_order
= byte_order
;
1474 switch (node
->type
) {
1476 cds_list_for_each_entry(iter
, &node
->u
.root
._typedef
,
1478 ret
= ctf_typedef_visit(fd
, depth
+ 1,
1479 &iter
->u
._typedef
.declaration_specifier
,
1480 &iter
->u
._typedef
.type_declarators
,
1481 trace
->root_declaration_scope
);
1485 cds_list_for_each_entry(iter
, &node
->u
.root
.typealias
,
1487 ret
= ctf_typealias_visit(fd
, depth
+ 1,
1488 &iter
->u
.typealias
.target
, &iter
->u
.typealias
.alias
1489 trace
->root_declaration_scope
);
1493 cds_list_for_each_entry(iter
, &node
->u
.root
.declaration_specifier
, siblings
) {
1494 ret
= ctf_declaration_specifier_visit(fd
, depth
, iter
,
1495 trace
->root_declaration_scope
, trace
);
1499 cds_list_for_each_entry(iter
, &node
->u
.root
.trace
, siblings
) {
1500 ret
= ctf_trace_visit(fd
, depth
+ 1, iter
, trace
);
1504 cds_list_for_each_entry(iter
, &node
->u
.root
.stream
, siblings
) {
1505 ret
= ctf_stream_visit(fd
, depth
+ 1, iter
,
1506 trace
->root_declaration_scope
, trace
);
1510 cds_list_for_each_entry(iter
, &node
->u
.root
.event
, siblings
) {
1511 ret
= ctf_event_visit(fd
, depth
+ 1, iter
,
1512 trace
->root_declaration_scope
, trace
);
1519 fprintf(stderr
, "[error] %s: unknown node type %d\n", __func__
,