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
)
653 struct ctf_node
*iter
;
655 q
= g_quark_from_string(enumerator
->u
.enumerator
.id
);
656 if (enum_declaration
->integer
->signedness
) {
660 cds_list_for_each_entry(iter
, enumerator
->u
.enumerator
.values
, siblings
) {
663 assert(iter
->type
== NODE_UNARY_EXPRESSION
);
669 switch (iter
->u
.unary_expression
.type
) {
670 case UNARY_SIGNED_CONSTANT
:
671 *target
= iter
->u
.unary_expression
.u
.signed_constant
;
673 case UNARY_UNSIGNED_CONSTANT
:
674 *target
= iter
->u
.unary_expression
.u
.unsigned_constant
;
677 fprintf(stderr
, "[error] %s: invalid enumerator\n", __func__
);
681 fprintf(stderr
, "[error] %s: invalid enumerator\n", __func__
);
688 enum_signed_insert(enum_declaration
, start
, end
, q
);
693 cds_list_for_each_entry(iter
, enumerator
->u
.enumerator
.values
, siblings
) {
696 assert(iter
->type
== NODE_UNARY_EXPRESSION
);
702 switch (iter
->u
.unary_expression
.type
) {
703 case UNARY_UNSIGNED_CONSTANT
:
704 *target
= iter
->u
.unary_expression
.u
.unsigned_constant
;
706 case UNARY_SIGNED_CONSTANT
:
708 * We don't accept signed constants for enums with unsigned
711 fprintf(stderr
, "[error] %s: invalid enumerator (signed constant encountered, but enum container type is unsigned)\n", __func__
);
714 fprintf(stderr
, "[error] %s: invalid enumerator\n", __func__
);
718 fprintf(stderr
, "[error] %s: invalid enumerator\n", __func__
);
725 enum_unsigned_insert(enum_declaration
, start
, end
, q
);
731 struct declaration
*ctf_declaration_enum_visit(int fd
, int depth
,
733 struct cds_list_head
*container_type
,
734 struct cds_list_head
*enumerator_list
,
736 struct declaration_scope
*declaration_scope
,
737 struct ctf_trace
*trace
)
739 struct declaration
*declaration
;
740 struct declaration_enum
*enum_declaration
;
741 struct declaration_integer
*integer_declaration
;
742 struct ctf_node
*iter
, *first
;
746 * For named enum (without body), lookup in
747 * declaration scope. Don't take reference on enum
748 * declaration: ref is only taken upon definition.
753 lookup_enum_declaration(g_quark_from_string(name
),
755 return enum_declaration
;
757 /* For unnamed enum, create type */
758 /* For named enum (with body), create type and add to declaration scope */
760 if (lookup_enum_declaration(g_quark_from_string(name
),
761 declaration_scope
)) {
763 fprintf(stderr
, "[error] %s: enum %s already declared in scope\n", __func__
, name
);
767 if (cds_list_empty(container_type
)) {
768 fprintf(stderr
, "[error] %s: missing container type for enumeration\n", __func__
, name
);
772 first
= _cds_list_first_entry(container_type
, struct node
, siblings
);
773 switch (first
->type
) {
775 case NODE_TYPE_SPECIFIER
:
776 declaration
= ctf_type_declarator_visit(fd
, depth
,
781 assert(declaration
->id
== CTF_TYPE_INTEGER
);
782 integer_declaration
= container_of(declaration
, struct declaration_integer
, p
);
788 enum_declaration
= enum_declaration_new(name
, integer_declaration
);
789 declaration_unref(&integer_declaration
->p
); /* leave ref to enum */
790 cds_list_for_each_entry(iter
, enumerator_list
, siblings
) {
791 ret
= ctf_enumerator_list_visit(fd
, depth
+ 1, iter
, enum_declaration
);
796 ret
= register_enum_declaration(g_quark_from_string(name
),
801 return enum_declaration
;
804 enum_declaration
->p
.declaration_free(&enum_declaration
->p
);
809 struct declaration
*ctf_declaration_type_specifier_visit(int fd
, int depth
,
810 struct cds_list_head
*declaration_specifier
,
811 struct declaration_scope
*declaration_scope
)
814 struct declaration
*declaration
;
817 str
= g_string_new();
818 ret
= visit_declaration_specifier(declaration_specifier
, str
);
821 str_c
= g_string_free(str
, FALSE
);
822 id_q
= g_quark_from_string(str_c
);
824 declaration
= lookup_declaration(id_q
, declaration_scope
);
829 * Returns 0/1 boolean, or < 0 on error.
832 int get_boolean(int fd
, int depth
, struct node
*unary_expression
)
834 if (unary_expression
->type
!= NODE_UNARY_EXPRESSION
) {
835 fprintf(stderr
, "[error] %s: expecting unary expression\n",
839 switch (unary_expression
->u
.unary_expression
.type
) {
840 case UNARY_UNSIGNED_CONSTANT
:
841 if (unary_expression
->u
.unary_expression
.u
.unsigned_constant
== 0)
845 case UNARY_SIGNED_CONSTANT
:
846 if (unary_expression
->u
.unary_expression
.u
.signed_constant
== 0)
851 if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "true"))
853 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "TRUE"))
855 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "false"))
857 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "FALSE"))
860 fprintf(stderr
, "[error] %s: unexpected string \"%s\"\n",
861 __func__
, unary_expression
->u
.unary_expression
.u
.string
);
866 fprintf(stderr
, "[error] %s: unexpected unary expression type\n",
874 int get_byte_order(int fd
, int depth
, struct node
*unary_expression
)
878 if (unary_expression
->u
.unary_expression
.type
!= UNARY_STRING
) {
879 fprintf(stderr
, "[error] %s: byte_order: expecting string\n",
883 if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "native"))
884 byte_order
= trace
->byte_order
;
885 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "network"))
886 byte_order
= BIG_ENDIAN
;
887 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "be"))
888 byte_order
= BIG_ENDIAN
;
889 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "le"))
890 byte_order
= LITTLE_ENDIAN
;
892 fprintf(stderr
, "[error] %s: unexpected string \"%s\". Should be \"native\", \"network\", \"be\" or \"le\".\n",
893 __func__
, right
->u
.unary_expression
.u
.string
);
900 struct declaration
*ctf_declaration_integer_visit(int fd
, int depth
,
901 struct cds_list_head
*expressions
,
902 struct ctf_trace
*trace
)
904 struct node
*expression
;
905 uint64_t alignment
, size
;
906 int byte_order
= trace
->byte_order
;
908 int has_alignment
= 0, has_size
= 0;
909 struct declaration_integer
*integer_declaration
;
911 cds_list_for_each_entry(expression
, expressions
, siblings
) {
912 struct node
*left
, *right
;
914 left
= expression
->u
.ctf_expression
.left
;
915 right
= expression
->u
.ctf_expression
.right
;
916 assert(left
->u
.unary_expression
.type
== UNARY_STRING
);
917 if (!strcmp(left
->u
.unary_expression
.u
.string
, "signed")) {
918 signedness
= get_boolean(fd
, depth
, right
);
921 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "byte_order")) {
922 byte_order
= get_byte_order(fd
, depth
, right
);
925 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "size")) {
926 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
927 fprintf(stderr
, "[error] %s: size: expecting unsigned constant\n",
931 size
= right
->u
.unary_expression
.u
.unsigned_constant
;
933 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "align")) {
934 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
935 fprintf(stderr
, "[error] %s: align: expecting unsigned constant\n",
939 alignment
= right
->u
.unary_expression
.u
.unsigned_constant
;
942 fprintf(stderr
, "[error] %s: unknown attribute name %s\n",
943 __func__
, left
->u
.unary_expression
.u
.string
);
948 fprintf(stderr
, "[error] %s: missing size attribute\n", __func__
);
951 if (!has_alignment
) {
952 if (size
% CHAR_BIT
) {
953 /* bit-packed alignment */
956 /* byte-packed alignment */
957 alignment
= CHAR_BIT
;
960 integer_declaration
= integer_declaration_new(size
,
961 byte_order
, signedness
, alignment
);
962 return &integer_declaration
->p
;
966 struct declaration
*ctf_declaration_floating_point_visit(int fd
, int depth
,
967 struct cds_list_head
*expressions
,
968 struct ctf_trace
*trace
)
970 struct node
*expression
;
971 uint64_t alignment
, exp_dig
, mant_dig
, byte_order
= trace
->byte_order
;
972 int has_alignment
= 0, has_exp_dig
= 0, has_mant_dig
= 0;
973 struct declaration_float
*float_declaration
;
975 cds_list_for_each_entry(expression
, expressions
, siblings
) {
976 struct node
*left
, *right
;
978 left
= expression
->u
.ctf_expression
.left
;
979 right
= expression
->u
.ctf_expression
.right
;
980 assert(left
->u
.unary_expression
.type
== UNARY_STRING
);
981 if (!strcmp(left
->u
.unary_expression
.u
.string
, "byte_order")) {
982 byte_order
= get_byte_order(fd
, depth
, right
);
985 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "exp_dig")) {
986 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
987 fprintf(stderr
, "[error] %s: exp_dig: expecting unsigned constant\n",
991 exp_dig
= right
->u
.unary_expression
.u
.unsigned_constant
;
993 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "mant_dig")) {
994 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
995 fprintf(stderr
, "[error] %s: mant_dig: expecting unsigned constant\n",
999 mant_dig
= right
->u
.unary_expression
.u
.unsigned_constant
;
1001 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "align")) {
1002 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
1003 fprintf(stderr
, "[error] %s: align: expecting unsigned constant\n",
1007 alignment
= right
->u
.unary_expression
.u
.unsigned_constant
;
1010 fprintf(stderr
, "[error] %s: unknown attribute name %s\n",
1011 __func__
, left
->u
.unary_expression
.u
.string
);
1015 if (!has_mant_dig
) {
1016 fprintf(stderr
, "[error] %s: missing mant_dig attribute\n", __func__
);
1020 fprintf(stderr
, "[error] %s: missing exp_dig attribute\n", __func__
);
1023 if (!has_alignment
) {
1024 if ((mant_dig
+ exp_dig
) % CHAR_BIT
) {
1025 /* bit-packed alignment */
1028 /* byte-packed alignment */
1029 alignment
= CHAR_BIT
;
1032 float_declaration
= float_declaration_new(mant_dig
, exp_dig
,
1033 byte_order
, alignment
);
1034 return &float_declaration
->p
;
1038 struct declaration
*ctf_declaration_string_visit(int fd
, int depth
,
1039 struct cds_list_head
*expressions
,
1040 struct ctf_trace
*trace
)
1042 struct node
*expression
;
1043 const char *encoding_c
= NULL
;
1044 enum ctf_string_encoding encoding
= CTF_STRING_UTF8
;
1045 struct declaration_string
*string_declaration
;
1047 cds_list_for_each_entry(expression
, expressions
, siblings
) {
1048 struct node
*left
, *right
;
1050 left
= expression
->u
.ctf_expression
.left
;
1051 right
= expression
->u
.ctf_expression
.right
;
1052 assert(left
->u
.unary_expression
.type
== UNARY_STRING
);
1053 if (!strcmp(left
->u
.unary_expression
.u
.string
, "encoding")) {
1054 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_STRING
) {
1055 fprintf(stderr
, "[error] %s: encoding: expecting string\n",
1059 encoding_c
= right
->u
.unary_expression
.u
.string
;
1061 fprintf(stderr
, "[error] %s: unknown attribute name %s\n",
1062 __func__
, left
->u
.unary_expression
.u
.string
);
1066 if (encoding_c
&& !strcmp(encoding_c
, "ASCII"))
1067 encoding
= CTF_STRING_ASCII
;
1068 string_declaration
= string_declaration_new(encoding
);
1069 return &string_declaration
->p
;
1074 * Also add named variant, struct or enum to the current declaration scope.
1077 struct declaration
*ctf_declaration_specifier_visit(FILE *fd
,
1078 int depth
, struct cds_list_head
*head
,
1079 struct declaration_scope
*declaration_scope
,
1080 struct ctf_trace
*trace
)
1082 struct declaration
*declaration
;
1085 first
= _cds_list_first_entry(head
, struct node
, siblings
);
1087 switch (first
->type
) {
1089 return ctf_declaration_struct_visit(fd
, depth
,
1090 first
->u
._struct
.name
,
1091 &first
->u
._struct
.declaration_list
,
1092 first
->u
._struct
.has_body
,
1096 return ctf_declaration_variant_visit(fd
, depth
,
1097 first
->u
.variant
.name
,
1098 &first
->u
.variant
.declaration_list
,
1099 first
->u
.variant
.has_body
,
1103 return ctf_declaration_enum_visit(fd
, depth
,
1104 first
->u
._enum
.enum_id
,
1105 &first
->u
._enum
.container_type
,
1106 &first
->u
._enum
.enumerator_list
,
1107 first
->u
._enum
.has_body
,
1111 return ctf_declaration_integer_visit(fd
, depth
,
1112 &first
->u
.integer
.expressions
, trace
);
1113 case NODE_FLOATING_POINT
:
1114 return ctf_declaration_floating_point_visit(fd
, depth
,
1115 &first
->u
.floating_point
.expressions
, trace
);
1117 return ctf_declaration_string_visit(fd
, depth
,
1118 &first
->u
.string
.expressions
, trace
);
1119 case NODE_TYPE_SPECIFIER
:
1120 return ctf_declaration_type_specifier_visit(fd
, depth
,
1121 head
, declaration_scope
, trace
);
1126 int ctf_event_declaration_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_event
*event
, struct ctf_trace
*trace
)
1130 switch (node
->type
) {
1132 ret
= ctf_typedef_visit(fd
, depth
+ 1,
1133 &node
->u
._typedef
.declaration_specifier
,
1134 &node
->u
._typedef
.type_declarators
,
1135 event
->declaration_scope
);
1139 case NODE_TYPEALIAS
:
1140 ret
= ctf_typealias_visit(fd
, depth
+ 1,
1141 &node
->u
.typealias
.target
, &node
->u
.typealias
.alias
1142 event
->declaration_scope
);
1146 case NODE_CTF_EXPRESSION
:
1150 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
1151 if (!strcmp(left
, "name")) {
1154 if (CTF_EVENT_FIELD_IS_SET(event
, name
))
1156 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
1158 fprintf(stderr
, "[error] %s: unexpected unary expression for event name\n", __func__
);
1161 event
->name
= g_quark_from_string(right
);
1163 CTF_EVENT_SET_FIELD(event
, name
);
1164 } else if (!strcmp(left
, "id")) {
1165 if (CTF_EVENT_FIELD_IS_SET(event
, id
))
1167 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &event
->id
);
1169 fprintf(stderr
, "[error] %s: unexpected unary expression for event id\n", __func__
);
1172 CTF_EVENT_SET_FIELD(event
, id
);
1173 } else if (!strcmp(left
, "stream_id")) {
1174 if (CTF_EVENT_FIELD_IS_SET(event
, stream_id
))
1176 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &event
->stream_id
);
1178 fprintf(stderr
, "[error] %s: unexpected unary expression for event stream_id\n", __func__
);
1181 event
->stream
= trace_stream_lookup(trace
, event
->stream_id
);
1182 if (!event
->stream
) {
1183 fprintf(stderr
, "[error] %s: stream id %" PRIu64
" cannot be found\n", __func__
, event
->stream_id
);
1186 CTF_EVENT_SET_FIELD(event
, stream_id
);
1187 } else if (!strcmp(left
, "context")) {
1188 struct declaration
*declaration
;
1190 if (!event
->definition_scope
)
1192 declaration
= ctf_declaration_specifier_visit(fd
, depth
,
1193 &node
->u
.ctf_expression
.right
,
1194 event
->declaration_scope
, trace
);
1197 if (declaration
->type
->id
!= CTF_TYPE_STRUCT
)
1199 event
->context_decl
= container_of(declaration
, struct declaration_struct
, p
);
1200 } else if (!strcmp(left
, "fields")) {
1201 struct declaration
*declaration
;
1203 if (!event
->definition_scope
)
1205 declaration
= ctf_declaration_specifier_visit(fd
, depth
,
1206 &node
->u
.ctf_expression
.right
,
1207 event
->declaration_scope
, trace
);
1210 if (declaration
->type
->id
!= CTF_TYPE_STRUCT
)
1212 event
->fields_decl
= container_of(declaration
, struct declaration_struct
, p
);
1219 /* TODO: declaration specifier should be added. */
1226 int ctf_event_visit(FILE *fd
, int depth
, struct ctf_node
*node
,
1227 struct declaration_scope
*parent_declaration_scope
, struct ctf_trace
*trace
)
1230 struct ctf_node
*iter
;
1231 struct ctf_event
*event
;
1232 struct definition_scope
*parent_def_scope
;
1234 event
= g_new0(struct ctf_event
, 1);
1235 event
->declaration_scope
= new_declaration_scope(parent_declaration_scope
);
1236 cds_list_for_each_entry(iter
, &node
->u
.event
.declaration_list
, siblings
) {
1237 ret
= ctf_event_declaration_visit(fd
, depth
+ 1, iter
, event
, trace
);
1241 if (!CTF_EVENT_FIELD_IS_SET(event
, name
)) {
1245 if (!CTF_EVENT_FIELD_IS_SET(event
, id
)) {
1249 if (!CTF_EVENT_FIELD_IS_SET(event
, stream_id
)) {
1253 if (event
->stream
->events_by_id
->len
<= event
->id
)
1254 g_ptr_array_set_size(event
->stream
->events_by_id
, event
->id
+ 1);
1255 g_ptr_array_index(event
->stream
->events_by_id
, event
->id
) = event
;
1256 g_hash_table_insert(event
->stream
->event_quark_to_id
,
1257 (gpointer
)(unsigned long) event
->name
,
1259 parent_def_scope
= stream
->definition_scope
;
1260 if (event
->context_decl
) {
1262 event
->context_decl
->definition_new(event
->context_decl
,
1263 parent_def_scope
, 0, 0);
1264 set_dynamic_definition_scope(&event
->context
->p
,
1265 event
->context
->scope
,
1267 parent_def_scope
= event
->context
->scope
;
1268 declaration_unref(event
->context_decl
);
1270 if (event
->fields_decl
) {
1272 event
->fields_decl
->definition_new(event
->fields_decl
,
1273 parent_def_scope
, 0, 0);
1274 set_dynamic_definition_scope(&event
->fields
->p
,
1275 event
->fields
->scope
,
1277 parent_def_scope
= event
->fields
->scope
;
1278 declaration_unref(event
->fields_decl
);
1283 declaration_unref(event
->fields_decl
);
1284 declaration_unref(event
->context_decl
);
1285 free_definition_scope(event
->definition_scope
);
1286 free_declaration_scope(event
->declaration_scope
);
1293 int ctf_stream_declaration_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_stream
*stream
, struct ctf_trace
*trace
)
1297 switch (node
->type
) {
1299 ret
= ctf_typedef_visit(fd
, depth
+ 1,
1300 &node
->u
._typedef
.declaration_specifier
,
1301 &node
->u
._typedef
.type_declarators
,
1302 stream
->declaration_scope
);
1306 case NODE_TYPEALIAS
:
1307 ret
= ctf_typealias_visit(fd
, depth
+ 1,
1308 &node
->u
.typealias
.target
, &node
->u
.typealias
.alias
1309 stream
->declaration_scope
);
1313 case NODE_CTF_EXPRESSION
:
1317 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
1318 if (!strcmp(left
, "stream_id")) {
1319 if (CTF_EVENT_FIELD_IS_SET(event
, stream_id
))
1321 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &event
->stream_id
);
1323 fprintf(stderr
, "[error] %s: unexpected unary expression for event stream_id\n", __func__
);
1326 CTF_EVENT_SET_FIELD(event
, stream_id
);
1327 } else if (!strcmp(left
, "event.header")) {
1328 struct declaration
*declaration
;
1330 declaration
= ctf_declaration_specifier_visit(fd
, depth
,
1331 &node
->u
.ctf_expression
.right
,
1332 stream
->declaration_scope
, stream
->definition_scope
, trace
);
1335 if (declaration
->type
->id
!= CTF_TYPE_STRUCT
)
1337 stream
->event_header_decl
= container_of(declaration
, struct declaration_struct
, p
);
1338 } else if (!strcmp(left
, "event.context")) {
1339 struct declaration
*declaration
;
1341 declaration
= ctf_declaration_specifier_visit(fd
, depth
,
1342 &node
->u
.ctf_expression
.right
,
1343 stream
->declaration_scope
, trace
);
1346 if (declaration
->type
->id
!= CTF_TYPE_STRUCT
)
1348 stream
->event_context_decl
= container_of(declaration
, struct declaration_struct
, p
);
1349 } else if (!strcmp(left
, "packet.context")) {
1350 struct declaration
*declaration
;
1352 declaration
= ctf_declaration_specifier_visit(fd
, depth
,
1353 &node
->u
.ctf_expression
.right
,
1354 stream
->declaration_scope
, trace
);
1357 if (declaration
->type
->id
!= CTF_TYPE_STRUCT
)
1359 stream
->packet_context_decl
= container_of(declaration
, struct declaration_struct
, p
);
1366 /* TODO: declaration specifier should be added. */
1373 int ctf_stream_visit(FILE *fd
, int depth
, struct ctf_node
*node
,
1374 struct declaration_scope
*parent_declaration_scope
, struct ctf_trace
*trace
)
1377 struct ctf_node
*iter
;
1378 struct ctf_stream
*stream
;
1379 struct definition_scope
*parent_def_scope
;
1381 stream
= g_new0(struct ctf_stream
, 1);
1382 stream
->declaration_scope
= new_declaration_scope(parent_declaration_scope
);
1383 stream
->events_by_id
= g_ptr_array_new();
1384 stream
->event_quark_to_id
= g_hash_table_new(g_int_hash
, g_int_equal
);
1385 cds_list_for_each_entry(iter
, &node
->u
.stream
.declaration_list
, siblings
) {
1386 ret
= ctf_stream_declaration_visit(fd
, depth
+ 1, iter
, stream
, trace
);
1390 if (!CTF_EVENT_FIELD_IS_SET(stream
, stream_id
)) {
1394 if (trace
->streams
->len
<= stream
->stream_id
)
1395 g_ptr_array_set_size(trace
->streams
, stream
->stream_id
+ 1);
1396 g_ptr_array_index(trace
->streams
, stream
->stream_id
) = stream
;
1398 parent_def_scope
= NULL
;
1399 if (stream
->packet_context_decl
) {
1400 stream
->packet_context
=
1401 stream
->packet_context_decl
->definition_new(stream
->packet_context_decl
,
1402 parent_def_scope
, 0, 0);
1403 set_dynamic_definition_scope(&stream
->packet_context
->p
,
1404 stream
->packet_context
->scope
,
1405 "stream.packet.context");
1406 parent_def_scope
= stream
->packet_context
->scope
;
1407 declaration_unref(stream
->packet_context_decl
);
1409 if (stream
->event_header_decl
) {
1410 stream
->event_header
=
1411 stream
->event_header_decl
->definition_new(stream
->event_header_decl
,
1412 parent_def_scope
, 0, 0);
1413 set_dynamic_definition_scope(&stream
->event_header
->p
,
1414 stream
->event_header
->scope
,
1415 "stream.event.header");
1416 parent_def_scope
= stream
->event_header
->scope
;
1417 declaration_unref(stream
->event_header_decl
);
1419 if (stream
->event_context_decl
) {
1420 stream
->event_context
=
1421 stream
->event_context_decl
->definition_new(stream
->event_context_decl
,
1422 parent_def_scope
, 0, 0);
1423 set_dynamic_definition_scope(&stream
->event_context
->p
,
1424 stream
->event_context
->scope
,
1425 "stream.event.context");
1426 parent_def_scope
= stream
->event_context
->scope
;
1427 declaration_unref(stream
->event_context_decl
);
1429 stream
->definition_scope
= parent_def_scope
;
1434 declaration_unref(stream
->event_header
);
1435 declaration_unref(stream
->event_context
);
1436 declaration_unref(stream
->packet_context
);
1437 g_ptr_array_free(stream
->events_by_id
, TRUE
);
1438 g_hash_table_free(stream
->event_quark_to_id
);
1439 free_definition_scope(stream
->definition_scope
);
1440 free_declaration_scope(stream
->declaration_scope
);
1446 int ctf_trace_declaration_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_trace
*trace
)
1450 switch (node
->type
) {
1452 ret
= ctf_typedef_visit(fd
, depth
+ 1,
1453 &node
->u
._typedef
.declaration_specifier
,
1454 &node
->u
._typedef
.type_declarators
,
1455 trace
->declaration_scope
);
1459 case NODE_TYPEALIAS
:
1460 ret
= ctf_typealias_visit(fd
, depth
+ 1,
1461 &node
->u
.typealias
.target
, &node
->u
.typealias
.alias
1462 trace
->declaration_scope
);
1466 case NODE_CTF_EXPRESSION
:
1470 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
1471 if (!strcmp(left
, "major")) {
1472 if (CTF_EVENT_FIELD_IS_SET(trace
, major
))
1474 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &trace
->major
);
1476 fprintf(stderr
, "[error] %s: unexpected unary expression for trace major number\n", __func__
);
1479 CTF_EVENT_SET_FIELD(trace
, major
);
1480 } else if (!strcmp(left
, "minor")) {
1481 if (CTF_EVENT_FIELD_IS_SET(trace
, minor
))
1483 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &trace
->minor
);
1485 fprintf(stderr
, "[error] %s: unexpected unary expression for trace minor number\n", __func__
);
1488 CTF_EVENT_SET_FIELD(trace
, minor
);
1489 } else if (!strcmp(left
, "word_size")) {
1490 if (CTF_EVENT_FIELD_IS_SET(trace
, word_size
))
1492 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &trace
->word_size
);
1494 fprintf(stderr
, "[error] %s: unexpected unary expression for trace word_size\n", __func__
);
1497 CTF_EVENT_SET_FIELD(trace
, word_size
);
1498 } else if (!strcmp(left
, "uuid")) {
1499 if (CTF_EVENT_FIELD_IS_SET(trace
, uuid
))
1501 ret
= get_unary_uuid(&node
->u
.ctf_expression
.right
, &trace
->uuid
);
1503 fprintf(stderr
, "[error] %s: unexpected unary expression for trace uuid\n", __func__
);
1506 CTF_EVENT_SET_FIELD(trace
, uuid
);
1513 /* TODO: declaration specifier should be added. */
1520 int ctf_trace_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_trace
*trace
)
1523 struct ctf_node
*iter
;
1525 if (trace
->declaration_scope
)
1527 trace
->declaration_scope
= new_declaration_scope(trace
->root_declaration_scope
);
1528 trace
->definition_scope
= new_dynamic_definition_scope(trace
->root_definition_scope
);
1529 trace
->streams
= g_ptr_array_new();
1530 cds_list_for_each_entry(iter
, &node
->u
.trace
.declaration_list
, siblings
) {
1531 ret
= ctf_trace_declaration_visit(fd
, depth
+ 1, iter
, trace
);
1535 if (!CTF_EVENT_FIELD_IS_SET(trace
, major
)) {
1539 if (!CTF_EVENT_FIELD_IS_SET(trace
, minor
)) {
1543 if (!CTF_EVENT_FIELD_IS_SET(trace
, uuid
)) {
1547 if (!CTF_EVENT_FIELD_IS_SET(trace
, word_size
)) {
1554 g_ptr_array_free(trace
->streams
, TRUE
);
1555 free_definition_scope(stream
->definition_scope
);
1556 free_declaration_scope(stream
->declaration_scope
);
1560 int ctf_visitor_construct_metadata(FILE *fd
, int depth
, struct ctf_node
*node
,
1561 struct ctf_trace
*trace
, int byte_order
)
1564 struct ctf_node
*iter
;
1566 trace
->byte_order
= byte_order
;
1568 switch (node
->type
) {
1570 cds_list_for_each_entry(iter
, &node
->u
.root
._typedef
,
1572 ret
= ctf_typedef_visit(fd
, depth
+ 1,
1573 &iter
->u
._typedef
.declaration_specifier
,
1574 &iter
->u
._typedef
.type_declarators
,
1575 trace
->root_declaration_scope
);
1579 cds_list_for_each_entry(iter
, &node
->u
.root
.typealias
,
1581 ret
= ctf_typealias_visit(fd
, depth
+ 1,
1582 &iter
->u
.typealias
.target
, &iter
->u
.typealias
.alias
1583 trace
->root_declaration_scope
);
1587 cds_list_for_each_entry(iter
, &node
->u
.root
.declaration_specifier
, siblings
) {
1588 ret
= ctf_declaration_specifier_visit(fd
, depth
, iter
,
1589 trace
->root_declaration_scope
, trace
);
1593 cds_list_for_each_entry(iter
, &node
->u
.root
.trace
, siblings
) {
1594 ret
= ctf_trace_visit(fd
, depth
+ 1, iter
, trace
);
1598 cds_list_for_each_entry(iter
, &node
->u
.root
.stream
, siblings
) {
1599 ret
= ctf_stream_visit(fd
, depth
+ 1, iter
,
1600 trace
->root_declaration_scope
, trace
);
1604 cds_list_for_each_entry(iter
, &node
->u
.root
.event
, siblings
) {
1605 ret
= ctf_event_visit(fd
, depth
+ 1, iter
,
1606 trace
->root_declaration_scope
, trace
);
1613 fprintf(stderr
, "[error] %s: unknown node type %d\n", __func__
,