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.
27 #include <babeltrace/list.h>
28 #include <uuid/uuid.h>
29 #include "ctf-scanner.h"
30 #include "ctf-parser.h"
33 #define fprintf_dbg(fd, fmt, args...) fprintf(fd, "%s: " fmt, __func__, ## args)
35 #define _cds_list_first_entry(ptr, type, member) \
36 cds_list_entry((ptr)->next, type, member)
39 struct declaration
*ctf_declaration_specifier_visit(FILE *fd
,
40 int depth
, struct list_head
*head
,
41 struct declaration_scope
*declaration_scope
);
44 int ctf_visitor_print_type_specifier(FILE *fd
, int depth
, struct ctf_node
*node
)
46 print_tabs(fd
, depth
);
47 fprintf(fd
, "<type_specifier \"");
49 switch (node
->u
.type_specifier
.type
) {
69 fprintf(fd
, "double");
72 fprintf(fd
, "signed");
74 case TYPESPEC_UNSIGNED
:
75 fprintf(fd
, "unsigned");
80 case TYPESPEC_COMPLEX
:
81 fprintf(fd
, "_Complex");
83 case TYPESPEC_IMAGINARY
:
84 fprintf(fd
, "_Imaginary");
89 case TYPESPEC_ID_TYPE
:
90 fprintf(fd
, "%s", node
->u
.type_specifier
.id_type
);
93 case TYPESPEC_UNKNOWN
:
95 fprintf(stderr
, "[error] %s: unknown type specifier %d\n", __func__
,
96 (int) node
->u
.type_specifier
.type
);
99 fprintf(fd
, "\"/>\n");
104 int ctf_visitor_print_type_declarator(FILE *fd
, int depth
, struct ctf_node
*node
)
107 struct ctf_node
*iter
;
109 print_tabs(fd
, depth
);
110 fprintf(fd
, "<type_declarator>\n");
113 if (!cds_list_empty(&node
->u
.type_declarator
.pointers
)) {
114 print_tabs(fd
, depth
);
115 fprintf(fd
, "<pointers>\n");
116 cds_list_for_each_entry(iter
, &node
->u
.type_declarator
.pointers
,
118 ret
= ctf_visitor_print_xml(fd
, depth
+ 1, iter
);
122 print_tabs(fd
, depth
);
123 fprintf(fd
, "</pointers>\n");
126 switch (node
->u
.type_declarator
.type
) {
128 if (node
->u
.type_declarator
.u
.id
) {
129 print_tabs(fd
, depth
);
130 fprintf(fd
, "<id \"");
131 fprintf(fd
, "%s", node
->u
.type_declarator
.u
.id
);
132 fprintf(fd
, "\" />\n");
136 if (node
->u
.type_declarator
.u
.nested
.type_declarator
) {
137 print_tabs(fd
, depth
);
138 fprintf(fd
, "<type_declarator>\n");
139 ret
= ctf_visitor_print_xml(fd
, depth
+ 1,
140 node
->u
.type_declarator
.u
.nested
.type_declarator
);
143 print_tabs(fd
, depth
);
144 fprintf(fd
, "</type_declarator>\n");
146 if (node
->u
.type_declarator
.u
.nested
.length
) {
147 print_tabs(fd
, depth
);
148 fprintf(fd
, "<length>\n");
149 ret
= ctf_visitor_print_xml(fd
, depth
+ 1,
150 node
->u
.type_declarator
.u
.nested
.length
);
153 print_tabs(fd
, depth
);
154 fprintf(fd
, "</length>\n");
156 if (node
->u
.type_declarator
.u
.nested
.abstract_array
) {
157 print_tabs(fd
, depth
);
158 fprintf(fd
, "<length>\n");
159 print_tabs(fd
, depth
);
160 fprintf(fd
, "</length>\n");
162 if (node
->u
.type_declarator
.bitfield_len
) {
163 print_tabs(fd
, depth
);
164 fprintf(fd
, "<bitfield_len>\n");
165 ret
= ctf_visitor_print_xml(fd
, depth
+ 1,
166 node
->u
.type_declarator
.bitfield_len
);
169 print_tabs(fd
, depth
);
170 fprintf(fd
, "</bitfield_len>\n");
173 case TYPEDEC_UNKNOWN
:
175 fprintf(stderr
, "[error] %s: unknown type declarator %d\n", __func__
,
176 (int) node
->u
.type_declarator
.type
);
181 print_tabs(fd
, depth
);
182 fprintf(fd
, "</type_declarator>\n");
187 * String returned must be freed by the caller using g_free.
190 char *concatenate_unary_strings(struct list_head
*head
)
192 struct ctf_node
*node
;
196 str
= g_string_new();
197 cds_list_for_each_entry(node
, head
, siblings
) {
200 assert(node
->type
== NODE_UNARY_EXPRESSION
);
201 assert(node
->u
.unary_expression
.type
== UNARY_STRING
);
202 assert((node
->u
.unary_expression
.link
== UNARY_LINK_UNKNOWN
)
204 switch (node
->u
.unary_expression
.link
) {
206 g_string_append(str
, ".")
208 case UNARY_ARROWLINK
:
209 g_string_append(str
, "->")
211 case UNARY_DOTDOTDOT
:
212 g_string_append(str
, "...")
215 src_string
= u
.unary_expression
.u
.string
;
216 g_string_append(str
, src_string
);
219 return g_string_free(str
, FALSE
);
223 int get_unary_unsigned(struct list_head
*head
, uint64_t *value
)
225 struct ctf_node
*node
;
228 cds_list_for_each_entry(node
, head
, siblings
) {
229 assert(node
->type
== NODE_UNARY_EXPRESSION
);
230 assert(node
->u
.unary_expression
.type
== UNARY_UNSIGNED_CONSTANT
);
231 assert(node
->u
.unary_expression
.link
== UNARY_LINK_UNKNOWN
);
233 *value
= node
->u
.unary_expression
.unsigned_constant
240 int get_unary_uuid(struct list_head
*head
, uuid_t
*uuid
)
242 struct ctf_node
*node
;
246 cds_list_for_each_entry(node
, head
, siblings
) {
247 const char *src_string
;
249 assert(node
->type
== NODE_UNARY_EXPRESSION
);
250 assert(node
->u
.unary_expression
.type
== UNARY_STRING
);
251 assert(node
->u
.unary_expression
.link
== UNARY_LINK_UNKNOWN
);
253 src_string
= u
.unary_expression
.u
.string
;
254 ret
= uuid_parse(u
.unary_expression
.u
.string
, *uuid
);
260 struct ctf_stream
*trace_stream_lookup(struct ctf_trace
*trace
, uint64_t stream_id
)
262 if (trace
->streams
->len
<= stream_id
)
264 return g_ptr_array_index(trace
->streams
, stream_id
);
268 struct declaration
*ctf_type_declarator_visit(int fd
, int depth
,
269 struct cds_list_head
*declaration_specifier
,
271 struct ctf_node
*node_type_declarator
,
272 struct declaration_scope
*declaration_scope
273 struct declaration
*nested_declaration
)
276 * Visit type declarator by first taking care of sequence/array
277 * (recursively). Then, when we get to the identifier, take care
281 assert(node_type_declarator
->u
.type_declarator
.type
!= TYPEDEC_UNKNOWN
);
282 /* TODO: gcc bitfields not supported yet. */
283 assert(node_type_declarator
->u
.type_declarator
.bitfield_len
== NULL
);
285 if (!nested_declaration
) {
286 if (!cds_list_empty(&node_type_declarator
->u
.type_declarator
.pointers
)) {
288 * If we have a pointer declarator, it _has_ to be present in
289 * the typealiases (else fail).
291 nested_declaration
= ... ;
293 nested_declaration
= /* parse declaration_specifier */;
298 if (node_type_declarator
->u
.type_declarator
.type
== TYPEDEC_ID
) {
299 if (node_type_declarator
->u
.type_declarator
.u
.id
)
300 *field_name
= g_quark_from_string(node_type_declarator
->u
.type_declarator
.u
.id
);
303 return nested_declaration
;
305 struct declaration
*declaration
;
310 /* create array/sequence, pass nested_declaration as child. */
311 length
= node_type_declarator
->u
.type_declarator
.u
.nested
.length
;
313 switch (length
->type
) {
314 case NODE_UNARY_EXPRESSION
:
316 declaration
= /* create array */;
318 case NODE_TYPE_SPECIFIER
:
320 declaration
= /* create sequence */;
327 /* Pass it as content of outer container */
328 declaration
= ctf_type_declarator_visit(fd
, depth
,
329 declaration_specifier
, field_name
,
330 node_type_declarator
->u
.type_declarator
.u
.nested
.type_declarator
,
331 declaration_scope
, declaration
);
337 int ctf_struct_type_declarators_visit(int fd
, int depth
,
338 struct declaration_struct
*struct_declaration
,
339 struct cds_list_head
*declaration_specifier
,
340 struct cds_list_head
*type_declarators
,
341 struct declaration_scope
*declaration_scope
)
343 struct ctf_node
*iter
;
346 cds_list_for_each_entry(iter
, type_declarators
, siblings
) {
347 struct declaration
*field_declaration
;
349 field_declaration
= ctf_type_declarator_visit(fd
, depth
,
350 declaration_specifier
,
352 struct_declaration
->scope
,
354 struct_declaration_add_field(struct_declaration
,
355 g_quark_to_string(field_name
),
363 int ctf_typedef_visit(int fd
, int depth
, struct declaration_scope
*scope
,
364 struct cds_list_head
*declaration_specifier
,
365 struct cds_list_head
*type_declarators
)
367 struct ctf_node
*iter
;
370 cds_list_for_each_entry(iter
, type_declarators
, siblings
) {
371 struct declaration
*type_declaration
;
374 type_declaration
= ctf_type_declarator_visit(fd
, depth
,
375 declaration_specifier
,
378 ret
= register_declaration(identifier
, type_declaration
, scope
);
380 type_declaration
->declaration_free(type_declaration
);
388 int ctf_typealias_visit(int fd
, int depth
, struct declaration_scope
*scope
,
389 struct ctf_node
*target
, struct ctf_node
*alias
)
391 struct declaration
*type_declaration
;
392 struct ctf_node
*iter
, *node
;
393 GQuark identifier
, dummy_id
;
397 int alias_item_nr
= 0;
399 /* See ctf_visitor_type_declarator() in the semantic validator. */
402 * Create target type declaration.
405 type_declaration
= ctf_type_declarator_visit(fd
, depth
,
406 &target
->u
.typealias_target
.declaration_specifier
,
407 &dummy_id
, &target
->u
.typealias_target
.type_declarators
,
409 if (!type_declaration
) {
410 fprintf(stderr
, "[error] %s: problem creating type declaration\n", __func__
);
415 * The semantic validator does not check whether the target is
416 * abstract or not (if it has an identifier). Check it here.
419 fprintf(stderr
, "[error] %s: expecting empty identifier\n", __func__
);
424 * Create alias identifier.
426 str
= g_string_new();
427 cds_list_for_each_entry(iter
, &alias
->u
.typealias_alias
.declaration_specifier
, siblings
) {
428 if (alias_item_nr
!= 0)
429 g_string_append(str
, " ");
432 switch (iter
->type
) {
433 case NODE_TYPE_SPECIFIER
:
434 switch (iter
->u
.type_specifier
.type
) {
436 g_string_append(str
, "void");
439 g_string_append(str
, "char");
442 g_string_append(str
, "short");
445 g_string_append(str
, "int");
448 g_string_append(str
, "long");
451 g_string_append(str
, "float");
453 case TYPESPEC_DOUBLE
:
454 g_string_append(str
, "double");
456 case TYPESPEC_SIGNED
:
457 g_string_append(str
, "signed");
459 case TYPESPEC_UNSIGNED
:
460 g_string_append(str
, "unsigned");
463 g_string_append(str
, "bool");
465 case TYPESPEC_COMPLEX
:
466 g_string_append(str
, "_Complex");
468 case TYPESPEC_IMAGINARY
:
469 g_string_append(str
, "_Imaginary");
472 g_string_append(str
, "const");
474 case TYPESPEC_ID_TYPE
:
475 if (!iter
->u
.type_specifier
.id_type
) {
476 fprintf(stderr
, "[error] %s: unexpected empty ID\n", __func__
);
480 g_string_append(str
, iter
->u
.type_specifier
.id_type
);
483 fprintf(stderr
, "[error] %s: unknown specifier\n", __func__
);
489 if (!iter
->u
._enum
.enum_id
) {
490 fprintf(stderr
, "[error] %s: unexpected empty enum ID\n", __func__
);
494 g_string_append(str
, "enum ");
495 g_string_append(str
, iter
->u
._enum
.enum_id
);
498 if (!iter
->u
.variant
.name
) {
499 fprintf(stderr
, "[error] %s: unexpected empty variant name\n", __func__
);
503 g_string_append(str
, "variant ");
504 g_string_append(str
, iter
->u
.variant
.name
);
507 if (!iter
->u
._struct
.name
) {
508 fprintf(stderr
, "[error] %s: unexpected empty variant name\n", __func__
);
512 g_string_append(str
, "struct ");
513 g_string_append(str
, iter
->u
._struct
.name
);
516 fprintf(stderr
, "[error] %s: unexpected node type %d\n", __func__
, (int) iter
->type
);
522 node
= _cds_list_first_entry(&alias
->u
.typealias_alias
.type_declarators
,
523 struct node
, siblings
) {
524 cds_list_for_each_entry(iter
, &node
->u
.type_declarator
.pointers
, siblings
) {
525 g_string_append(str
, " *");
526 if (iter
->u
.pointer
.const_qualifier
)
527 g_string_append(str
, " const");
531 str_c
= g_string_free(str
, FALSE
);
532 alias_q
= g_quark_from_string(str_c
);
534 ret
= register_declaration(alias_q
, type_declaration
, scope
);
540 type_declaration
->declaration_free(type_declaration
);
545 int ctf_struct_declaration_list_visit(int fd
, int depth
,
546 struct ctf_node
*iter
, struct declaration_struct
*struct_declaration
)
548 struct declaration
*declaration
;
551 switch (iter
->type
) {
553 /* For each declarator, declare type and add type to struct declaration scope */
554 ret
= ctf_typedef_visit(fd
, depth
,
555 struct_declaration
->scope
,
556 &iter
->u
._typedef
.declaration_specifier
,
557 &iter
->u
._typedef
.type_declarators
);
562 /* Declare type with declarator and add type to struct declaration scope */
563 ret
= ctf_typealias_visit(fd
, depth
,
564 struct_declaration
->scope
,
565 iter
->u
.typealias
.target
,
566 iter
->u
.typealias
.alias
);
570 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
571 /* Add field to structure declaration */
572 ret
= ctf_struct_type_declarators_visit(fd
, depth
,
574 &iter
->u
.struct_or_variant_declaration
.declaration_specifier
,
575 &iter
->u
.struct_or_variant_declaration
.type_declarators
);
580 fprintf(stderr
, "[error] %s: unexpected node type %d\n", __func__
, (int) iter
->type
);
587 struct declaration_struct
*ctf_declaration_struct_visit(FILE *fd
,
588 int depth
, const char *name
, struct cds_list_head
*declaration_list
,
589 int has_body
, struct declaration_scope
*declaration_scope
)
591 struct declaration
*declaration
;
592 struct declaration_struct
*struct_declaration
;
593 struct ctf_node
*iter
;
596 * For named struct (without body), lookup in
597 * declaration scope. Don't take reference on struct
598 * declaration: ref is only taken upon definition.
603 lookup_struct_declaration(g_quark_from_string(name
),
605 return struct_declaration
;
607 /* For unnamed struct, create type */
608 /* For named struct (with body), create type and add to declaration scope */
610 if (lookup_struct_declaration(g_quark_from_string(name
),
611 declaration_scope
)) {
613 fprintf(stderr
, "[error] %s: struct %s already declared in scope\n", name
);
617 struct_declaration
= struct_declaration_new(name
, declaration_scope
);
618 cds_list_for_each_entry(iter
, declaration_list
, siblings
) {
619 ret
= ctf_struct_declaration_list_visit(fd
,
620 depth
+ 1, iter
, struct_declaration
);
625 ret
= register_struct_declaration(g_quark_from_string(name
),
630 return struct_declaration
;
633 struct_declaration
->p
.declaration_free(&struct_declaration
->p
);
638 * Also add named variant, struct or enum to the current declaration scope.
641 struct declaration
*ctf_declaration_specifier_visit(FILE *fd
,
642 int depth
, struct list_head
*head
,
643 struct declaration_scope
*declaration_scope
)
645 struct declaration
*declaration
;
648 first
= _cds_list_first_entry(head
, struct node
, siblings
);
650 switch (first
->type
) {
652 return ctf_declaration_struct_visit(fd
, depth
,
653 first
->u
._struct
.name
,
654 &first
->u
._struct
.declaration_list
,
655 first
->u
._struct
.has_body
,
660 * For named variant (without body), lookup in
661 * declaration scope and create declaration copy.
663 /* For named variant (with body), create type and add to declaration scope */
664 /* For unnamed variant, create type */
665 /* If variant has a tag field specifier, assign tag name. */
669 * For named enum (without body), lookup in declaration
670 * scope and create declaration copy.
672 /* For named enum (with body), create type and add to declaration scope */
673 /* For unnamed enum, create type */
674 /* Enumerations need to have their size/type specifier (< >). */
678 * Create an integer declaration.
681 case NODE_FLOATING_POINT
:
683 * Create a floating point declaration.
688 * Create a string declaration.
691 case NODE_TYPE_SPECIFIER
:
693 * Lookup named type in typedef declarations (in
694 * declaration scope). Create a copy of the declaration.
702 int ctf_typedef_declarator_visit(FILE *fd
, int depth
,
703 struct list_head
*declaration_specifier
,
704 struct node
*type_declarator
,
705 struct declaration_scope
*declaration_scope
)
708 * Build the type using declaration specifier (creating
709 * declaration from type_specifier), then apply type declarator,
710 * add the resulting type to the current declaration scope.
712 cds_list_for_each_entry(iter
, declaration_specifier
, siblings
) {
720 int ctf_typedef_visit(FILE *fd
, int depth
,
721 struct list_head
*declaration_specifier
,
722 struct list_head
*type_declarators
,
723 struct declaration_scope
*declaration_scope
)
725 struct ctf_node
*iter
;
727 cds_list_for_each_entry(iter
, type_declarators
, siblings
) {
728 ret
= ctf_typedef_declarator_visit(fd
, depth
+ 1,
729 &node
->u
._typedef
.declaration_specifier
, iter
,
738 int ctf_typealias_visit(FILE *fd
, int depth
, struct ctf_node
*target
,
739 struct ctf_node
*alias
,
740 struct declaration_scope
*declaration_scope
)
743 * Build target type, check that it is reachable in current
747 /* Only one type declarator is allowed */
749 /* Build alias type, add to current declaration scope. */
750 /* Only one type declarator is allowed */
754 int ctf_event_declaration_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_event
*event
, struct ctf_trace
*trace
)
758 switch (node
->type
) {
760 ret
= ctf_typedef_visit(fd
, depth
+ 1,
761 &node
->u
._typedef
.declaration_specifier
,
762 &node
->u
._typedef
.type_declarators
,
763 event
->declaration_scope
);
768 ret
= ctf_typealias_visit(fd
, depth
+ 1,
769 &node
->u
.typealias
.target
, &node
->u
.typealias
.alias
770 event
->declaration_scope
);
774 case NODE_CTF_EXPRESSION
:
778 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
779 if (!strcmp(left
, "name")) {
782 if (CTF_EVENT_FIELD_IS_SET(event
, name
))
784 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
786 fprintf(stderr
, "[error] %s: unexpected unary expression for event name\n", __func__
);
789 event
->name
= g_quark_from_string(right
);
791 CTF_EVENT_SET_FIELD(event
, name
);
792 } else if (!strcmp(left
, "id")) {
793 if (CTF_EVENT_FIELD_IS_SET(event
, id
))
795 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &event
->id
);
797 fprintf(stderr
, "[error] %s: unexpected unary expression for event id\n", __func__
);
800 CTF_EVENT_SET_FIELD(event
, id
);
801 } else if (!strcmp(left
, "stream_id")) {
802 if (CTF_EVENT_FIELD_IS_SET(event
, stream_id
))
804 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &event
->stream_id
);
806 fprintf(stderr
, "[error] %s: unexpected unary expression for event stream_id\n", __func__
);
809 event
->stream
= trace_stream_lookup(trace
, event
->stream_id
);
810 if (!event
->stream
) {
811 fprintf(stderr
, "[error] %s: stream id %" PRIu64
" cannot be found\n", __func__
, event
->stream_id
);
814 CTF_EVENT_SET_FIELD(event
, stream_id
);
815 } else if (!strcmp(left
, "context")) {
816 struct declaration
*declaration
;
818 if (!event
->definition_scope
)
820 declaration
= ctf_declaration_specifier_visit(fd
, depth
,
821 &node
->u
.ctf_expression
.right
,
822 event
->declaration_scope
);
825 if (declaration
->type
->id
!= CTF_TYPE_STRUCT
)
827 event
->context_decl
= container_of(declaration
, struct declaration_struct
, p
);
828 } else if (!strcmp(left
, "fields")) {
829 struct declaration
*declaration
;
831 if (!event
->definition_scope
)
833 declaration
= ctf_declaration_specifier_visit(fd
, depth
,
834 &node
->u
.ctf_expression
.right
,
835 event
->declaration_scope
);
838 if (declaration
->type
->id
!= CTF_TYPE_STRUCT
)
840 event
->fields_decl
= container_of(declaration
, struct declaration_struct
, p
);
847 /* TODO: declaration specifier should be added. */
854 int ctf_event_visit(FILE *fd
, int depth
, struct ctf_node
*node
,
855 struct declaration_scope
*parent_declaration_scope
, struct ctf_trace
*trace
)
858 struct ctf_node
*iter
;
859 struct ctf_event
*event
;
860 struct definition_scope
*parent_def_scope
;
862 event
= g_new0(struct ctf_event
, 1);
863 event
->declaration_scope
= new_declaration_scope(parent_declaration_scope
);
864 cds_list_for_each_entry(iter
, &node
->u
.event
.declaration_list
, siblings
) {
865 ret
= ctf_event_declaration_visit(fd
, depth
+ 1, iter
, event
, trace
);
869 if (!CTF_EVENT_FIELD_IS_SET(event
, name
)) {
873 if (!CTF_EVENT_FIELD_IS_SET(event
, id
)) {
877 if (!CTF_EVENT_FIELD_IS_SET(event
, stream_id
)) {
881 if (event
->stream
->events_by_id
->len
<= event
->id
)
882 g_ptr_array_set_size(event
->stream
->events_by_id
, event
->id
+ 1);
883 g_ptr_array_index(event
->stream
->events_by_id
, event
->id
) = event
;
884 g_hash_table_insert(event
->stream
->event_quark_to_id
,
885 (gpointer
)(unsigned long) event
->name
,
887 parent_def_scope
= stream
->definition_scope
;
888 if (event
->context_decl
) {
890 event
->context_decl
->definition_new(event
->context_decl
,
891 parent_def_scope
, 0, 0);
892 set_dynamic_definition_scope(&event
->context
->p
,
893 event
->context
->scope
,
895 parent_def_scope
= event
->context
->scope
;
896 declaration_unref(event
->context_decl
);
898 if (event
->fields_decl
) {
900 event
->fields_decl
->definition_new(event
->fields_decl
,
901 parent_def_scope
, 0, 0);
902 set_dynamic_definition_scope(&event
->fields
->p
,
903 event
->fields
->scope
,
905 parent_def_scope
= event
->fields
->scope
;
906 declaration_unref(event
->fields_decl
);
911 declaration_unref(event
->fields_decl
);
912 declaration_unref(event
->context_decl
);
913 free_definition_scope(event
->definition_scope
);
914 free_declaration_scope(event
->declaration_scope
);
921 int ctf_stream_declaration_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_stream
*stream
, struct ctf_trace
*trace
)
925 switch (node
->type
) {
927 ret
= ctf_typedef_visit(fd
, depth
+ 1,
928 &node
->u
._typedef
.declaration_specifier
,
929 &node
->u
._typedef
.type_declarators
,
930 stream
->declaration_scope
);
935 ret
= ctf_typealias_visit(fd
, depth
+ 1,
936 &node
->u
.typealias
.target
, &node
->u
.typealias
.alias
937 stream
->declaration_scope
);
941 case NODE_CTF_EXPRESSION
:
945 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
946 if (!strcmp(left
, "stream_id")) {
947 if (CTF_EVENT_FIELD_IS_SET(event
, stream_id
))
949 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &event
->stream_id
);
951 fprintf(stderr
, "[error] %s: unexpected unary expression for event stream_id\n", __func__
);
954 CTF_EVENT_SET_FIELD(event
, stream_id
);
955 } else if (!strcmp(left
, "event.header")) {
956 struct declaration
*declaration
;
958 declaration
= ctf_declaration_specifier_visit(fd
, depth
,
959 &node
->u
.ctf_expression
.right
,
960 stream
->declaration_scope
, stream
->definition_scope
);
963 if (declaration
->type
->id
!= CTF_TYPE_STRUCT
)
965 stream
->event_header_decl
= container_of(declaration
, struct declaration_struct
, p
);
966 } else if (!strcmp(left
, "event.context")) {
967 struct declaration
*declaration
;
969 declaration
= ctf_declaration_specifier_visit(fd
, depth
,
970 &node
->u
.ctf_expression
.right
,
971 stream
->declaration_scope
);
974 if (declaration
->type
->id
!= CTF_TYPE_STRUCT
)
976 stream
->event_context_decl
= container_of(declaration
, struct declaration_struct
, p
);
977 } else if (!strcmp(left
, "packet.context")) {
978 struct declaration
*declaration
;
980 declaration
= ctf_declaration_specifier_visit(fd
, depth
,
981 &node
->u
.ctf_expression
.right
,
982 stream
->declaration_scope
);
985 if (declaration
->type
->id
!= CTF_TYPE_STRUCT
)
987 stream
->packet_context_decl
= container_of(declaration
, struct declaration_struct
, p
);
994 /* TODO: declaration specifier should be added. */
1001 int ctf_stream_visit(FILE *fd
, int depth
, struct ctf_node
*node
,
1002 struct declaration_scope
*parent_declaration_scope
, struct ctf_trace
*trace
)
1005 struct ctf_node
*iter
;
1006 struct ctf_stream
*stream
;
1007 struct definition_scope
*parent_def_scope
;
1009 stream
= g_new0(struct ctf_stream
, 1);
1010 stream
->declaration_scope
= new_declaration_scope(parent_declaration_scope
);
1011 stream
->events_by_id
= g_ptr_array_new();
1012 stream
->event_quark_to_id
= g_hash_table_new(g_int_hash
, g_int_equal
);
1013 cds_list_for_each_entry(iter
, &node
->u
.stream
.declaration_list
, siblings
) {
1014 ret
= ctf_stream_declaration_visit(fd
, depth
+ 1, iter
, stream
, trace
);
1018 if (!CTF_EVENT_FIELD_IS_SET(stream
, stream_id
)) {
1022 if (trace
->streams
->len
<= stream
->stream_id
)
1023 g_ptr_array_set_size(trace
->streams
, stream
->stream_id
+ 1);
1024 g_ptr_array_index(trace
->streams
, stream
->stream_id
) = stream
;
1026 parent_def_scope
= NULL
;
1027 if (stream
->packet_context_decl
) {
1028 stream
->packet_context
=
1029 stream
->packet_context_decl
->definition_new(stream
->packet_context_decl
,
1030 parent_def_scope
, 0, 0);
1031 set_dynamic_definition_scope(&stream
->packet_context
->p
,
1032 stream
->packet_context
->scope
,
1033 "stream.packet.context");
1034 parent_def_scope
= stream
->packet_context
->scope
;
1035 declaration_unref(stream
->packet_context_decl
);
1037 if (stream
->event_header_decl
) {
1038 stream
->event_header
=
1039 stream
->event_header_decl
->definition_new(stream
->event_header_decl
,
1040 parent_def_scope
, 0, 0);
1041 set_dynamic_definition_scope(&stream
->event_header
->p
,
1042 stream
->event_header
->scope
,
1043 "stream.event.header");
1044 parent_def_scope
= stream
->event_header
->scope
;
1045 declaration_unref(stream
->event_header_decl
);
1047 if (stream
->event_context_decl
) {
1048 stream
->event_context
=
1049 stream
->event_context_decl
->definition_new(stream
->event_context_decl
,
1050 parent_def_scope
, 0, 0);
1051 set_dynamic_definition_scope(&stream
->event_context
->p
,
1052 stream
->event_context
->scope
,
1053 "stream.event.context");
1054 parent_def_scope
= stream
->event_context
->scope
;
1055 declaration_unref(stream
->event_context_decl
);
1057 stream
->definition_scope
= parent_def_scope
;
1062 declaration_unref(stream
->event_header
);
1063 declaration_unref(stream
->event_context
);
1064 declaration_unref(stream
->packet_context
);
1065 g_ptr_array_free(stream
->events_by_id
, TRUE
);
1066 g_hash_table_free(stream
->event_quark_to_id
);
1067 free_definition_scope(stream
->definition_scope
);
1068 free_declaration_scope(stream
->declaration_scope
);
1074 int ctf_trace_declaration_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_trace
*trace
)
1078 switch (node
->type
) {
1080 ret
= ctf_typedef_visit(fd
, depth
+ 1,
1081 &node
->u
._typedef
.declaration_specifier
,
1082 &node
->u
._typedef
.type_declarators
,
1083 trace
->declaration_scope
);
1087 case NODE_TYPEALIAS
:
1088 ret
= ctf_typealias_visit(fd
, depth
+ 1,
1089 &node
->u
.typealias
.target
, &node
->u
.typealias
.alias
1090 trace
->declaration_scope
);
1094 case NODE_CTF_EXPRESSION
:
1098 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
1099 if (!strcmp(left
, "major")) {
1100 if (CTF_EVENT_FIELD_IS_SET(trace
, major
))
1102 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &trace
->major
);
1104 fprintf(stderr
, "[error] %s: unexpected unary expression for trace major number\n", __func__
);
1107 CTF_EVENT_SET_FIELD(trace
, major
);
1108 } else if (!strcmp(left
, "minor")) {
1109 if (CTF_EVENT_FIELD_IS_SET(trace
, minor
))
1111 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &trace
->minor
);
1113 fprintf(stderr
, "[error] %s: unexpected unary expression for trace minor number\n", __func__
);
1116 CTF_EVENT_SET_FIELD(trace
, minor
);
1117 } else if (!strcmp(left
, "word_size")) {
1118 if (CTF_EVENT_FIELD_IS_SET(trace
, word_size
))
1120 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &trace
->word_size
);
1122 fprintf(stderr
, "[error] %s: unexpected unary expression for trace word_size\n", __func__
);
1125 CTF_EVENT_SET_FIELD(trace
, word_size
);
1126 } else if (!strcmp(left
, "uuid")) {
1127 if (CTF_EVENT_FIELD_IS_SET(trace
, uuid
))
1129 ret
= get_unary_uuid(&node
->u
.ctf_expression
.right
, &trace
->uuid
);
1131 fprintf(stderr
, "[error] %s: unexpected unary expression for trace uuid\n", __func__
);
1134 CTF_EVENT_SET_FIELD(trace
, uuid
);
1141 /* TODO: declaration specifier should be added. */
1149 int ctf_trace_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_trace
*trace
)
1152 struct ctf_node
*iter
;
1154 if (trace
->declaration_scope
)
1156 trace
->declaration_scope
= new_declaration_scope(trace
->root_declaration_scope
);
1157 trace
->definition_scope
= new_dynamic_definition_scope(trace
->root_definition_scope
);
1158 trace
->streams
= g_ptr_array_new();
1159 cds_list_for_each_entry(iter
, &node
->u
.trace
.declaration_list
, siblings
) {
1160 ret
= ctf_trace_declaration_visit(fd
, depth
+ 1, iter
, trace
);
1164 if (!CTF_EVENT_FIELD_IS_SET(trace
, major
)) {
1168 if (!CTF_EVENT_FIELD_IS_SET(trace
, minor
)) {
1172 if (!CTF_EVENT_FIELD_IS_SET(trace
, uuid
)) {
1176 if (!CTF_EVENT_FIELD_IS_SET(trace
, word_size
)) {
1183 g_ptr_array_free(trace
->streams
, TRUE
);
1184 free_definition_scope(stream
->definition_scope
);
1185 free_declaration_scope(stream
->declaration_scope
);
1189 int _ctf_visitor(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_trace
*trace
)
1192 struct ctf_node
*iter
;
1194 switch (node
->type
) {
1196 cds_list_for_each_entry(iter
, &node
->u
.root
._typedef
,
1198 ret
= ctf_typedef_visit(fd
, depth
+ 1,
1199 &iter
->u
._typedef
.declaration_specifier
,
1200 &iter
->u
._typedef
.type_declarators
,
1201 trace
->root_declaration_scope
);
1205 cds_list_for_each_entry(iter
, &node
->u
.root
.typealias
,
1207 ret
= ctf_typealias_visit(fd
, depth
+ 1,
1208 &iter
->u
.typealias
.target
, &iter
->u
.typealias
.alias
1209 trace
->root_declaration_scope
);
1213 cds_list_for_each_entry(iter
, &node
->u
.root
.declaration_specifier
, siblings
) {
1214 ret
= ctf_declaration_specifier_visit(fd
, depth
, iter
,
1215 trace
->root_declaration_scope
);
1219 cds_list_for_each_entry(iter
, &node
->u
.root
.trace
, siblings
) {
1220 ret
= ctf_trace_visit(fd
, depth
+ 1, iter
, trace
);
1224 cds_list_for_each_entry(iter
, &node
->u
.root
.stream
, siblings
) {
1225 ret
= ctf_stream_visit(fd
, depth
+ 1, iter
,
1226 trace
->root_declaration_scope
, trace
);
1230 cds_list_for_each_entry(iter
, &node
->u
.root
.event
, siblings
) {
1231 ret
= ctf_event_visit(fd
, depth
+ 1, iter
,
1232 trace
->root_declaration_scope
, trace
);
1238 case NODE_TYPEALIAS_TARGET
:
1239 print_tabs(fd
, depth
);
1240 fprintf(fd
, "<target>\n");
1243 print_tabs(fd
, depth
);
1244 fprintf(fd
, "<declaration_specifier>\n");
1245 cds_list_for_each_entry(iter
, &node
->u
.typealias_target
.declaration_specifier
, siblings
) {
1246 ret
= ctf_visitor_print_xml(fd
, depth
+ 1, iter
);
1250 print_tabs(fd
, depth
);
1251 fprintf(fd
, "</declaration_specifier>\n");
1253 print_tabs(fd
, depth
);
1254 fprintf(fd
, "<type_declarators>\n");
1255 cds_list_for_each_entry(iter
, &node
->u
.typealias_target
.type_declarators
, siblings
) {
1256 ret
= ctf_visitor_print_xml(fd
, depth
+ 1, iter
);
1260 print_tabs(fd
, depth
);
1261 fprintf(fd
, "</type_declarators>\n");
1264 print_tabs(fd
, depth
);
1265 fprintf(fd
, "</target>\n");
1267 case NODE_TYPEALIAS_ALIAS
:
1268 print_tabs(fd
, depth
);
1269 fprintf(fd
, "<alias>\n");
1272 print_tabs(fd
, depth
);
1273 fprintf(fd
, "<declaration_specifier>\n");
1274 cds_list_for_each_entry(iter
, &node
->u
.typealias_alias
.declaration_specifier
, siblings
) {
1275 ret
= ctf_visitor_print_xml(fd
, depth
+ 1, iter
);
1279 print_tabs(fd
, depth
);
1280 fprintf(fd
, "</declaration_specifier>\n");
1282 print_tabs(fd
, depth
);
1283 fprintf(fd
, "<type_declarators>\n");
1284 cds_list_for_each_entry(iter
, &node
->u
.typealias_alias
.type_declarators
, siblings
) {
1285 ret
= ctf_visitor_print_xml(fd
, depth
+ 1, iter
);
1289 print_tabs(fd
, depth
);
1290 fprintf(fd
, "</type_declarators>\n");
1293 print_tabs(fd
, depth
);
1294 fprintf(fd
, "</alias>\n");
1296 case NODE_TYPEALIAS
:
1297 print_tabs(fd
, depth
);
1298 fprintf(fd
, "<typealias>\n");
1299 ret
= ctf_visitor_print_xml(fd
, depth
+ 1, node
->u
.typealias
.target
);
1302 ret
= ctf_visitor_print_xml(fd
, depth
+ 1, node
->u
.typealias
.alias
);
1305 print_tabs(fd
, depth
);
1306 fprintf(fd
, "</typealias>\n");
1309 case NODE_TYPE_SPECIFIER
:
1310 ret
= ctf_visitor_print_type_specifier(fd
, depth
, node
);
1315 print_tabs(fd
, depth
);
1316 if (node
->u
.pointer
.const_qualifier
)
1317 fprintf(fd
, "<const_pointer />\n");
1319 fprintf(fd
, "<pointer />\n");
1321 case NODE_TYPE_DECLARATOR
:
1322 ret
= ctf_visitor_print_type_declarator(fd
, depth
, node
);
1327 case NODE_FLOATING_POINT
:
1328 print_tabs(fd
, depth
);
1329 fprintf(fd
, "<floating_point>\n");
1330 cds_list_for_each_entry(iter
, &node
->u
.floating_point
.expressions
, siblings
) {
1331 ret
= ctf_visitor_print_xml(fd
, depth
+ 1, iter
);
1335 print_tabs(fd
, depth
);
1336 fprintf(fd
, "</floating_point>\n");
1339 print_tabs(fd
, depth
);
1340 fprintf(fd
, "<integer>\n");
1341 cds_list_for_each_entry(iter
, &node
->u
.integer
.expressions
, siblings
) {
1342 ret
= ctf_visitor_print_xml(fd
, depth
+ 1, iter
);
1346 print_tabs(fd
, depth
);
1347 fprintf(fd
, "</integer>\n");
1350 print_tabs(fd
, depth
);
1351 fprintf(fd
, "<string>\n");
1352 cds_list_for_each_entry(iter
, &node
->u
.string
.expressions
, siblings
) {
1353 ret
= ctf_visitor_print_xml(fd
, depth
+ 1, iter
);
1357 print_tabs(fd
, depth
);
1358 fprintf(fd
, "</string>\n");
1360 case NODE_ENUMERATOR
:
1361 print_tabs(fd
, depth
);
1362 fprintf(fd
, "<enumerator");
1363 if (node
->u
.enumerator
.id
)
1364 fprintf(fd
, " id=\"%s\"", node
->u
.enumerator
.id
);
1366 cds_list_for_each_entry(iter
, &node
->u
.enumerator
.values
, siblings
) {
1367 ret
= ctf_visitor_print_xml(fd
, depth
+ 1, iter
);
1371 print_tabs(fd
, depth
);
1372 fprintf(fd
, "</enumerator>\n");
1375 print_tabs(fd
, depth
);
1376 if (node
->u
._struct
.name
)
1377 fprintf(fd
, "<enum name=\"%s\">\n",
1378 node
->u
._enum
.enum_id
);
1380 fprintf(fd
, "<enum >\n");
1383 if (node
->u
._enum
.container_type
) {
1384 print_tabs(fd
, depth
);
1385 fprintf(fd
, "<container_type>\n");
1386 ret
= ctf_visitor_print_xml(fd
, depth
+ 1, node
->u
._enum
.container_type
);
1389 print_tabs(fd
, depth
);
1390 fprintf(fd
, "</container_type>\n");
1393 print_tabs(fd
, depth
);
1394 fprintf(fd
, "<enumerator_list>\n");
1395 cds_list_for_each_entry(iter
, &node
->u
._enum
.enumerator_list
, siblings
) {
1396 ret
= ctf_visitor_print_xml(fd
, depth
+ 1, iter
);
1400 print_tabs(fd
, depth
);
1401 fprintf(fd
, "</enumerator_list>\n");
1404 print_tabs(fd
, depth
);
1405 fprintf(fd
, "</enum>\n");
1407 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
1408 print_tabs(fd
, depth
);
1409 fprintf(fd
, "<declaration_specifier>\n");
1410 cds_list_for_each_entry(iter
, &node
->u
.struct_or_variant_declaration
.declaration_specifier
, siblings
) {
1411 ret
= ctf_visitor_print_xml(fd
, depth
+ 1, iter
);
1415 print_tabs(fd
, depth
);
1416 fprintf(fd
, "</declaration_specifier>\n");
1418 print_tabs(fd
, depth
);
1419 fprintf(fd
, "<type_declarators>\n");
1420 cds_list_for_each_entry(iter
, &node
->u
.struct_or_variant_declaration
.type_declarators
, siblings
) {
1421 ret
= ctf_visitor_print_xml(fd
, depth
+ 1, iter
);
1425 print_tabs(fd
, depth
);
1426 fprintf(fd
, "</type_declarators>\n");
1429 print_tabs(fd
, depth
);
1430 fprintf(fd
, "<variant");
1431 if (node
->u
.variant
.name
)
1432 fprintf(fd
, " name=\"%s\"", node
->u
.variant
.name
);
1433 if (node
->u
.variant
.choice
)
1434 fprintf(fd
, " choice=\"%s\"", node
->u
.variant
.choice
);
1436 cds_list_for_each_entry(iter
, &node
->u
.variant
.declaration_list
, siblings
) {
1437 ret
= ctf_visitor_print_xml(fd
, depth
+ 1, iter
);
1441 print_tabs(fd
, depth
);
1442 fprintf(fd
, "</variant>\n");
1445 print_tabs(fd
, depth
);
1446 if (node
->u
._struct
.name
)
1447 fprintf(fd
, "<struct name=\"%s\">\n",
1448 node
->u
._struct
.name
);
1450 fprintf(fd
, "<struct>\n");
1451 cds_list_for_each_entry(iter
, &node
->u
._struct
.declaration_list
, siblings
) {
1452 ret
= ctf_visitor_print_xml(fd
, depth
+ 1, iter
);
1456 print_tabs(fd
, depth
);
1457 fprintf(fd
, "</struct>\n");
1462 fprintf(stderr
, "[error] %s: unknown node type %d\n", __func__
,