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
);
45 * String returned must be freed by the caller using g_free.
48 char *concatenate_unary_strings(struct cds_list_head
*head
)
50 struct ctf_node
*node
;
55 cds_list_for_each_entry(node
, head
, siblings
) {
58 assert(node
->type
== NODE_UNARY_EXPRESSION
);
59 assert(node
->u
.unary_expression
.type
== UNARY_STRING
);
60 assert((node
->u
.unary_expression
.link
== UNARY_LINK_UNKNOWN
)
62 switch (node
->u
.unary_expression
.link
) {
64 g_string_append(str
, ".")
67 g_string_append(str
, "->")
70 g_string_append(str
, "...")
73 src_string
= u
.unary_expression
.u
.string
;
74 g_string_append(str
, src_string
);
77 return g_string_free(str
, FALSE
);
81 int get_unary_unsigned(struct cds_list_head
*head
, uint64_t *value
)
83 struct ctf_node
*node
;
86 cds_list_for_each_entry(node
, head
, siblings
) {
87 assert(node
->type
== NODE_UNARY_EXPRESSION
);
88 assert(node
->u
.unary_expression
.type
== UNARY_UNSIGNED_CONSTANT
);
89 assert(node
->u
.unary_expression
.link
== UNARY_LINK_UNKNOWN
);
91 *value
= node
->u
.unary_expression
.unsigned_constant
98 int get_unary_uuid(struct cds_list_head
*head
, uuid_t
*uuid
)
100 struct ctf_node
*node
;
104 cds_list_for_each_entry(node
, head
, siblings
) {
105 const char *src_string
;
107 assert(node
->type
== NODE_UNARY_EXPRESSION
);
108 assert(node
->u
.unary_expression
.type
== UNARY_STRING
);
109 assert(node
->u
.unary_expression
.link
== UNARY_LINK_UNKNOWN
);
111 src_string
= u
.unary_expression
.u
.string
;
112 ret
= uuid_parse(u
.unary_expression
.u
.string
, *uuid
);
118 struct ctf_stream
*trace_stream_lookup(struct ctf_trace
*trace
, uint64_t stream_id
)
120 if (trace
->streams
->len
<= stream_id
)
122 return g_ptr_array_index(trace
->streams
, stream_id
);
126 void visit_declaration_specifier(struct cds_list_head
*declaration_specifier
, GString
*str
)
128 struct ctf_node
*iter
;
129 int alias_item_nr
= 0;
132 cds_list_for_each_entry(iter
, declaration_specifier
, siblings
) {
133 if (alias_item_nr
!= 0)
134 g_string_append(str
, " ");
137 switch (iter
->type
) {
138 case NODE_TYPE_SPECIFIER
:
139 switch (iter
->u
.type_specifier
.type
) {
141 g_string_append(str
, "void");
144 g_string_append(str
, "char");
147 g_string_append(str
, "short");
150 g_string_append(str
, "int");
153 g_string_append(str
, "long");
156 g_string_append(str
, "float");
158 case TYPESPEC_DOUBLE
:
159 g_string_append(str
, "double");
161 case TYPESPEC_SIGNED
:
162 g_string_append(str
, "signed");
164 case TYPESPEC_UNSIGNED
:
165 g_string_append(str
, "unsigned");
168 g_string_append(str
, "bool");
170 case TYPESPEC_COMPLEX
:
171 g_string_append(str
, "_Complex");
173 case TYPESPEC_IMAGINARY
:
174 g_string_append(str
, "_Imaginary");
177 g_string_append(str
, "const");
179 case TYPESPEC_ID_TYPE
:
180 if (iter
->u
.type_specifier
.id_type
)
181 g_string_append(str
, iter
->u
.type_specifier
.id_type
);
184 fprintf(stderr
, "[error] %s: unknown specifier\n", __func__
);
190 if (!iter
->u
._enum
.enum_id
) {
191 fprintf(stderr
, "[error] %s: unexpected empty enum ID\n", __func__
);
195 g_string_append(str
, "enum ");
196 g_string_append(str
, iter
->u
._enum
.enum_id
);
199 if (!iter
->u
.variant
.name
) {
200 fprintf(stderr
, "[error] %s: unexpected empty variant name\n", __func__
);
204 g_string_append(str
, "variant ");
205 g_string_append(str
, iter
->u
.variant
.name
);
208 if (!iter
->u
._struct
.name
) {
209 fprintf(stderr
, "[error] %s: unexpected empty variant name\n", __func__
);
213 g_string_append(str
, "struct ");
214 g_string_append(str
, iter
->u
._struct
.name
);
217 fprintf(stderr
, "[error] %s: unexpected node type %d\n", __func__
, (int) iter
->type
);
228 GQuark
create_typealias_identifier(int fd
, int depth
,
229 struct cds_list_head
*declaration_specifier
,
230 struct ctf_node
*node_type_declarator
)
237 str
= g_string_new();
238 ret
= visit_declaration_specifier(declaration_specifier
, str
);
240 g_string_free(str
, TRUE
);
243 cds_list_for_each_entry(iter
, &node_type_declarator
->u
.type_declarator
.pointers
, siblings
) {
244 g_string_append(str
, " *");
245 if (iter
->u
.pointer
.const_qualifier
)
246 g_string_append(str
, " const");
248 str_c
= g_string_free(str
, FALSE
);
249 alias_q
= g_quark_from_string(str_c
);
255 struct declaration
*ctf_type_declarator_visit(int fd
, int depth
,
256 struct cds_list_head
*declaration_specifier
,
258 struct ctf_node
*node_type_declarator
,
259 struct declaration_scope
*declaration_scope
,
260 struct declaration
*nested_declaration
)
263 * Visit type declarator by first taking care of sequence/array
264 * (recursively). Then, when we get to the identifier, take care
268 assert(node_type_declarator
->u
.type_declarator
.type
!= TYPEDEC_UNKNOWN
);
270 /* TODO: gcc bitfields not supported yet. */
271 if (node_type_declarator
->u
.type_declarator
.bitfield_len
!= NULL
) {
272 fprintf(stderr
, "[error] %s: gcc bitfields are not supported yet.\n", __func__
);
276 if (!nested_declaration
) {
277 if (!cds_list_empty(&node_type_declarator
->u
.type_declarator
.pointers
)) {
281 * If we have a pointer declarator, it _has_ to be present in
282 * the typealiases (else fail).
284 alias_q
= create_typealias_identifier(fd
, depth
,
285 declaration_specifier
, node_type_declarator
);
286 nested_declaration
= lookup_declaration(alias_q
, declaration_scope
);
287 if (!nested_declaration
) {
288 fprintf(stderr
, "[error] %s: cannot find typealias \"%s\".\n", __func__
, g_quark_to_string(alias_q
));
292 nested_declaration
= /* parse declaration_specifier */;
296 if (node_type_declarator
->u
.type_declarator
.type
== TYPEDEC_ID
) {
297 if (node_type_declarator
->u
.type_declarator
.u
.id
)
298 *field_name
= g_quark_from_string(node_type_declarator
->u
.type_declarator
.u
.id
);
301 return nested_declaration
;
303 struct declaration
*declaration
;
308 /* create array/sequence, pass nested_declaration as child. */
309 length
= node_type_declarator
->u
.type_declarator
.u
.nested
.length
;
311 switch (length
->type
) {
312 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
),
362 int ctf_variant_type_declarators_visit(int fd
, int depth
,
363 struct declaration_variant
*variant_declaration
,
364 struct cds_list_head
*declaration_specifier
,
365 struct cds_list_head
*type_declarators
,
366 struct declaration_scope
*declaration_scope
)
368 struct ctf_node
*iter
;
371 cds_list_for_each_entry(iter
, type_declarators
, siblings
) {
372 struct declaration
*field_declaration
;
374 field_declaration
= ctf_type_declarator_visit(fd
, depth
,
375 declaration_specifier
,
377 variant_declaration
->scope
,
379 variant_declaration_add_field(variant_declaration
,
380 g_quark_to_string(field_name
),
387 int ctf_typedef_visit(int fd
, int depth
, struct declaration_scope
*scope
,
388 struct cds_list_head
*declaration_specifier
,
389 struct cds_list_head
*type_declarators
)
391 struct ctf_node
*iter
;
394 cds_list_for_each_entry(iter
, type_declarators
, siblings
) {
395 struct declaration
*type_declaration
;
398 type_declaration
= ctf_type_declarator_visit(fd
, depth
,
399 declaration_specifier
,
402 ret
= register_declaration(identifier
, type_declaration
, scope
);
404 type_declaration
->declaration_free(type_declaration
);
412 int ctf_typealias_visit(int fd
, int depth
, struct declaration_scope
*scope
,
413 struct ctf_node
*target
, struct ctf_node
*alias
)
415 struct declaration
*type_declaration
;
416 struct ctf_node
*iter
, *node
;
420 /* See ctf_visitor_type_declarator() in the semantic validator. */
423 * Create target type declaration.
426 type_declaration
= ctf_type_declarator_visit(fd
, depth
,
427 &target
->u
.typealias_target
.declaration_specifier
,
428 &dummy_id
, &target
->u
.typealias_target
.type_declarators
,
430 if (!type_declaration
) {
431 fprintf(stderr
, "[error] %s: problem creating type declaration\n", __func__
);
436 * The semantic validator does not check whether the target is
437 * abstract or not (if it has an identifier). Check it here.
440 fprintf(stderr
, "[error] %s: expecting empty identifier\n", __func__
);
445 * Create alias identifier.
448 node
= _cds_list_first_entry(&alias
->u
.typealias_alias
.type_declarators
,
449 struct node
, siblings
);
450 alias_q
= create_typealias_identifier(fd
, depth
,
451 &alias
->u
.typealias_alias
.declaration_specifier
, node
);
452 ret
= register_declaration(alias_q
, type_declaration
, scope
);
458 type_declaration
->declaration_free(type_declaration
);
463 int ctf_struct_declaration_list_visit(int fd
, int depth
,
464 struct ctf_node
*iter
, struct declaration_struct
*struct_declaration
)
466 struct declaration
*declaration
;
469 switch (iter
->type
) {
471 /* For each declarator, declare type and add type to struct declaration scope */
472 ret
= ctf_typedef_visit(fd
, depth
,
473 struct_declaration
->scope
,
474 &iter
->u
._typedef
.declaration_specifier
,
475 &iter
->u
._typedef
.type_declarators
);
480 /* Declare type with declarator and add type to struct declaration scope */
481 ret
= ctf_typealias_visit(fd
, depth
,
482 struct_declaration
->scope
,
483 iter
->u
.typealias
.target
,
484 iter
->u
.typealias
.alias
);
488 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
489 /* Add field to structure declaration */
490 ret
= ctf_struct_type_declarators_visit(fd
, depth
,
492 &iter
->u
.struct_or_variant_declaration
.declaration_specifier
,
493 &iter
->u
.struct_or_variant_declaration
.type_declarators
);
498 fprintf(stderr
, "[error] %s: unexpected node type %d\n", __func__
, (int) iter
->type
);
505 int ctf_variant_declaration_list_visit(int fd
, int depth
,
506 struct ctf_node
*iter
, struct declaration_variant
*variant_declaration
)
508 struct declaration
*declaration
;
511 switch (iter
->type
) {
513 /* For each declarator, declare type and add type to variant declaration scope */
514 ret
= ctf_typedef_visit(fd
, depth
,
515 variant_declaration
->scope
,
516 &iter
->u
._typedef
.declaration_specifier
,
517 &iter
->u
._typedef
.type_declarators
);
522 /* Declare type with declarator and add type to variant declaration scope */
523 ret
= ctf_typealias_visit(fd
, depth
,
524 variant_declaration
->scope
,
525 iter
->u
.typealias
.target
,
526 iter
->u
.typealias
.alias
);
530 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
531 /* Add field to structure declaration */
532 ret
= ctf_variant_type_declarators_visit(fd
, depth
,
534 &iter
->u
.struct_or_variant_declaration
.declaration_specifier
,
535 &iter
->u
.struct_or_variant_declaration
.type_declarators
);
540 fprintf(stderr
, "[error] %s: unexpected node type %d\n", __func__
, (int) iter
->type
);
547 struct declaration_struct
*ctf_declaration_struct_visit(FILE *fd
,
548 int depth
, const char *name
, struct cds_list_head
*declaration_list
,
549 int has_body
, struct declaration_scope
*declaration_scope
)
551 struct declaration
*declaration
;
552 struct declaration_struct
*struct_declaration
;
553 struct ctf_node
*iter
;
556 * For named struct (without body), lookup in
557 * declaration scope. Don't take reference on struct
558 * declaration: ref is only taken upon definition.
563 lookup_struct_declaration(g_quark_from_string(name
),
565 return struct_declaration
;
567 /* For unnamed struct, create type */
568 /* For named struct (with body), create type and add to declaration scope */
570 if (lookup_struct_declaration(g_quark_from_string(name
),
571 declaration_scope
)) {
573 fprintf(stderr
, "[error] %s: struct %s already declared in scope\n", __func__
, name
);
577 struct_declaration
= struct_declaration_new(name
, declaration_scope
);
578 cds_list_for_each_entry(iter
, declaration_list
, siblings
) {
579 ret
= ctf_struct_declaration_list_visit(fd
, depth
+ 1, iter
, struct_declaration
);
584 ret
= register_struct_declaration(g_quark_from_string(name
),
589 return struct_declaration
;
592 struct_declaration
->p
.declaration_free(&struct_declaration
->p
);
597 struct declaration_variant
*ctf_declaration_variant_visit(FILE *fd
,
598 int depth
, const char *name
, struct cds_list_head
*declaration_list
,
599 int has_body
, struct declaration_scope
*declaration_scope
)
601 struct declaration
*declaration
;
602 struct declaration_variant
*variant_declaration
;
603 struct ctf_node
*iter
;
606 * For named variant (without body), lookup in
607 * declaration scope. Don't take reference on variant
608 * declaration: ref is only taken upon definition.
612 variant_declaration
=
613 lookup_variant_declaration(g_quark_from_string(name
),
615 return variant_declaration
;
617 /* For unnamed variant, create type */
618 /* For named variant (with body), create type and add to declaration scope */
620 if (lookup_variant_declaration(g_quark_from_string(name
),
621 declaration_scope
)) {
623 fprintf(stderr
, "[error] %s: variant %s already declared in scope\n", __func__
, name
);
627 variant_declaration
= variant_declaration_new(name
, declaration_scope
);
628 cds_list_for_each_entry(iter
, declaration_list
, siblings
) {
629 ret
= ctf_variant_declaration_list_visit(fd
, depth
+ 1, iter
, variant_declaration
);
634 ret
= register_variant_declaration(g_quark_from_string(name
),
639 return variant_declaration
;
642 variant_declaration
->p
.declaration_free(&variant_declaration
->p
);
647 int ctf_enumerator_list_visit(int fd
, int depth
,
648 struct ctf_node
*enumerator
,
649 struct declaration_enum
*enum_declaration
)
657 struct declaration
*ctf_declaration_enum_visit(int fd
, int depth
,
659 struct ctf_node
*container_type
,
660 struct cds_list_head
*enumerator_list
,
663 struct declaration
*declaration
;
664 struct declaration_enum
*enum_declaration
;
665 struct declaration_integer
*integer_declaration
;
666 struct ctf_node
*iter
;
669 * For named enum (without body), lookup in
670 * declaration scope. Don't take reference on enum
671 * declaration: ref is only taken upon definition.
676 lookup_enum_declaration(g_quark_from_string(name
),
678 return enum_declaration
;
680 /* For unnamed enum, create type */
681 /* For named enum (with body), create type and add to declaration scope */
683 if (lookup_enum_declaration(g_quark_from_string(name
),
684 declaration_scope
)) {
686 fprintf(stderr
, "[error] %s: enum %s already declared in scope\n", __func__
, name
);
691 /* TODO CHECK Enumerations need to have their size/type specifier (< >). */
692 integer_declaration
= integer_declaration_new(); /* TODO ... */
694 enum_declaration
= enum_declaration_new(name
, integer_declaration
);
695 declaration_unref(&integer_declaration
->p
); /* leave ref to enum */
696 cds_list_for_each_entry(iter
, enumerator_list
, siblings
) {
697 ret
= ctf_enumerator_list_visit(fd
, depth
+ 1, iter
, enum_declaration
);
702 ret
= register_enum_declaration(g_quark_from_string(name
),
707 return enum_declaration
;
710 enum_declaration
->p
.declaration_free(&enum_declaration
->p
);
715 struct declaration
*ctf_declaration_type_specifier_visit(int fd
, int depth
,
716 struct cds_list_head
*declaration_specifier
,
717 struct declaration_scope
*declaration_scope
)
720 struct declaration
*declaration
;
723 str
= g_string_new();
724 ret
= visit_declaration_specifier(declaration_specifier
, str
);
727 str_c
= g_string_free(str
, FALSE
);
728 id_q
= g_quark_from_string(str_c
);
730 declaration
= lookup_declaration(id_q
, declaration_scope
);
735 struct declaration
*ctf_declaration_integer_visit(int fd
, int depth
,
736 struct cds_list_head
*expressions
)
738 struct node
*expression
;
739 uint64_t alignment
, size
, byte_order
= trace
->native_bo
, signedness
= 0;
740 int has_alignment
= 0, has_size
= 0;
741 struct declaration_integer
*integer_declaration
;
743 cds_list_for_each_entry(expression
, expressions
, siblings
) {
744 struct node
*left
, *right
;
746 left
= expression
->u
.ctf_expression
.left
;
747 right
= expression
->u
.ctf_expression
.right
;
748 assert(left
->u
.unary_expression
.type
== UNARY_STRING
);
749 if (!strcmp(left
->u
.unary_expression
.u
.string
, "signed")) {
752 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "byte_order")) {
754 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "size")) {
756 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "align")) {
759 fprintf(stderr
, "[error] %s: unknown attribute name %s\n",
760 __func__
, left
->u
.unary_expression
.u
.string
);
764 if (!has_alignment
) {
765 fprintf(stderr
, "[error] %s: missing alignment attribute\n", __func__
);
769 fprintf(stderr
, "[error] %s: missing size attribute\n", __func__
);
772 integer_declaration
= integer_declaration_new(size
,
773 byte_order
, signedness
, alignment
);
774 return &integer_declaration
->p
;
778 * Also add named variant, struct or enum to the current declaration scope.
781 struct declaration
*ctf_declaration_specifier_visit(FILE *fd
,
782 int depth
, struct cds_list_head
*head
,
783 struct declaration_scope
*declaration_scope
)
785 struct declaration
*declaration
;
788 first
= _cds_list_first_entry(head
, struct node
, siblings
);
790 switch (first
->type
) {
792 return ctf_declaration_struct_visit(fd
, depth
,
793 first
->u
._struct
.name
,
794 &first
->u
._struct
.declaration_list
,
795 first
->u
._struct
.has_body
,
798 return ctf_declaration_variant_visit(fd
, depth
,
799 first
->u
.variant
.name
,
800 &first
->u
.variant
.declaration_list
,
801 first
->u
.variant
.has_body
,
804 return ctf_declaration_enum_visit(fd
, depth
,
805 first
->u
._enum
.enum_id
,
806 first
->u
._enum
.container_type
,
807 &first
->u
._enum
.enumerator_list
,
808 first
->u
._enum
.has_body
);
810 return ctf_declaration_integer_visit(fd
, depth
,
811 &first
->u
.integer
.expressions
);
812 case NODE_FLOATING_POINT
: /* TODO */
813 return ctf_declaration_floating_point_visit(fd
, depth
,
814 &first
->u
.floating_point
.expressions
);
815 case NODE_STRING
: /* TODO */
816 return ctf_declaration_string_visit(fd
, depth
,
817 &first
->u
.string
.expressions
);
818 case NODE_TYPE_SPECIFIER
:
819 return ctf_declaration_type_specifier_visit(fd
, depth
,
820 head
, declaration_scope
);
825 int ctf_event_declaration_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_event
*event
, struct ctf_trace
*trace
)
829 switch (node
->type
) {
831 ret
= ctf_typedef_visit(fd
, depth
+ 1,
832 &node
->u
._typedef
.declaration_specifier
,
833 &node
->u
._typedef
.type_declarators
,
834 event
->declaration_scope
);
839 ret
= ctf_typealias_visit(fd
, depth
+ 1,
840 &node
->u
.typealias
.target
, &node
->u
.typealias
.alias
841 event
->declaration_scope
);
845 case NODE_CTF_EXPRESSION
:
849 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
850 if (!strcmp(left
, "name")) {
853 if (CTF_EVENT_FIELD_IS_SET(event
, name
))
855 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
857 fprintf(stderr
, "[error] %s: unexpected unary expression for event name\n", __func__
);
860 event
->name
= g_quark_from_string(right
);
862 CTF_EVENT_SET_FIELD(event
, name
);
863 } else if (!strcmp(left
, "id")) {
864 if (CTF_EVENT_FIELD_IS_SET(event
, id
))
866 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &event
->id
);
868 fprintf(stderr
, "[error] %s: unexpected unary expression for event id\n", __func__
);
871 CTF_EVENT_SET_FIELD(event
, id
);
872 } else if (!strcmp(left
, "stream_id")) {
873 if (CTF_EVENT_FIELD_IS_SET(event
, stream_id
))
875 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &event
->stream_id
);
877 fprintf(stderr
, "[error] %s: unexpected unary expression for event stream_id\n", __func__
);
880 event
->stream
= trace_stream_lookup(trace
, event
->stream_id
);
881 if (!event
->stream
) {
882 fprintf(stderr
, "[error] %s: stream id %" PRIu64
" cannot be found\n", __func__
, event
->stream_id
);
885 CTF_EVENT_SET_FIELD(event
, stream_id
);
886 } else if (!strcmp(left
, "context")) {
887 struct declaration
*declaration
;
889 if (!event
->definition_scope
)
891 declaration
= ctf_declaration_specifier_visit(fd
, depth
,
892 &node
->u
.ctf_expression
.right
,
893 event
->declaration_scope
);
896 if (declaration
->type
->id
!= CTF_TYPE_STRUCT
)
898 event
->context_decl
= container_of(declaration
, struct declaration_struct
, p
);
899 } else if (!strcmp(left
, "fields")) {
900 struct declaration
*declaration
;
902 if (!event
->definition_scope
)
904 declaration
= ctf_declaration_specifier_visit(fd
, depth
,
905 &node
->u
.ctf_expression
.right
,
906 event
->declaration_scope
);
909 if (declaration
->type
->id
!= CTF_TYPE_STRUCT
)
911 event
->fields_decl
= container_of(declaration
, struct declaration_struct
, p
);
918 /* TODO: declaration specifier should be added. */
925 int ctf_event_visit(FILE *fd
, int depth
, struct ctf_node
*node
,
926 struct declaration_scope
*parent_declaration_scope
, struct ctf_trace
*trace
)
929 struct ctf_node
*iter
;
930 struct ctf_event
*event
;
931 struct definition_scope
*parent_def_scope
;
933 event
= g_new0(struct ctf_event
, 1);
934 event
->declaration_scope
= new_declaration_scope(parent_declaration_scope
);
935 cds_list_for_each_entry(iter
, &node
->u
.event
.declaration_list
, siblings
) {
936 ret
= ctf_event_declaration_visit(fd
, depth
+ 1, iter
, event
, trace
);
940 if (!CTF_EVENT_FIELD_IS_SET(event
, name
)) {
944 if (!CTF_EVENT_FIELD_IS_SET(event
, id
)) {
948 if (!CTF_EVENT_FIELD_IS_SET(event
, stream_id
)) {
952 if (event
->stream
->events_by_id
->len
<= event
->id
)
953 g_ptr_array_set_size(event
->stream
->events_by_id
, event
->id
+ 1);
954 g_ptr_array_index(event
->stream
->events_by_id
, event
->id
) = event
;
955 g_hash_table_insert(event
->stream
->event_quark_to_id
,
956 (gpointer
)(unsigned long) event
->name
,
958 parent_def_scope
= stream
->definition_scope
;
959 if (event
->context_decl
) {
961 event
->context_decl
->definition_new(event
->context_decl
,
962 parent_def_scope
, 0, 0);
963 set_dynamic_definition_scope(&event
->context
->p
,
964 event
->context
->scope
,
966 parent_def_scope
= event
->context
->scope
;
967 declaration_unref(event
->context_decl
);
969 if (event
->fields_decl
) {
971 event
->fields_decl
->definition_new(event
->fields_decl
,
972 parent_def_scope
, 0, 0);
973 set_dynamic_definition_scope(&event
->fields
->p
,
974 event
->fields
->scope
,
976 parent_def_scope
= event
->fields
->scope
;
977 declaration_unref(event
->fields_decl
);
982 declaration_unref(event
->fields_decl
);
983 declaration_unref(event
->context_decl
);
984 free_definition_scope(event
->definition_scope
);
985 free_declaration_scope(event
->declaration_scope
);
992 int ctf_stream_declaration_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_stream
*stream
, struct ctf_trace
*trace
)
996 switch (node
->type
) {
998 ret
= ctf_typedef_visit(fd
, depth
+ 1,
999 &node
->u
._typedef
.declaration_specifier
,
1000 &node
->u
._typedef
.type_declarators
,
1001 stream
->declaration_scope
);
1005 case NODE_TYPEALIAS
:
1006 ret
= ctf_typealias_visit(fd
, depth
+ 1,
1007 &node
->u
.typealias
.target
, &node
->u
.typealias
.alias
1008 stream
->declaration_scope
);
1012 case NODE_CTF_EXPRESSION
:
1016 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
1017 if (!strcmp(left
, "stream_id")) {
1018 if (CTF_EVENT_FIELD_IS_SET(event
, stream_id
))
1020 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &event
->stream_id
);
1022 fprintf(stderr
, "[error] %s: unexpected unary expression for event stream_id\n", __func__
);
1025 CTF_EVENT_SET_FIELD(event
, stream_id
);
1026 } else if (!strcmp(left
, "event.header")) {
1027 struct declaration
*declaration
;
1029 declaration
= ctf_declaration_specifier_visit(fd
, depth
,
1030 &node
->u
.ctf_expression
.right
,
1031 stream
->declaration_scope
, stream
->definition_scope
);
1034 if (declaration
->type
->id
!= CTF_TYPE_STRUCT
)
1036 stream
->event_header_decl
= container_of(declaration
, struct declaration_struct
, p
);
1037 } else if (!strcmp(left
, "event.context")) {
1038 struct declaration
*declaration
;
1040 declaration
= ctf_declaration_specifier_visit(fd
, depth
,
1041 &node
->u
.ctf_expression
.right
,
1042 stream
->declaration_scope
);
1045 if (declaration
->type
->id
!= CTF_TYPE_STRUCT
)
1047 stream
->event_context_decl
= container_of(declaration
, struct declaration_struct
, p
);
1048 } else if (!strcmp(left
, "packet.context")) {
1049 struct declaration
*declaration
;
1051 declaration
= ctf_declaration_specifier_visit(fd
, depth
,
1052 &node
->u
.ctf_expression
.right
,
1053 stream
->declaration_scope
);
1056 if (declaration
->type
->id
!= CTF_TYPE_STRUCT
)
1058 stream
->packet_context_decl
= container_of(declaration
, struct declaration_struct
, p
);
1065 /* TODO: declaration specifier should be added. */
1072 int ctf_stream_visit(FILE *fd
, int depth
, struct ctf_node
*node
,
1073 struct declaration_scope
*parent_declaration_scope
, struct ctf_trace
*trace
)
1076 struct ctf_node
*iter
;
1077 struct ctf_stream
*stream
;
1078 struct definition_scope
*parent_def_scope
;
1080 stream
= g_new0(struct ctf_stream
, 1);
1081 stream
->declaration_scope
= new_declaration_scope(parent_declaration_scope
);
1082 stream
->events_by_id
= g_ptr_array_new();
1083 stream
->event_quark_to_id
= g_hash_table_new(g_int_hash
, g_int_equal
);
1084 cds_list_for_each_entry(iter
, &node
->u
.stream
.declaration_list
, siblings
) {
1085 ret
= ctf_stream_declaration_visit(fd
, depth
+ 1, iter
, stream
, trace
);
1089 if (!CTF_EVENT_FIELD_IS_SET(stream
, stream_id
)) {
1093 if (trace
->streams
->len
<= stream
->stream_id
)
1094 g_ptr_array_set_size(trace
->streams
, stream
->stream_id
+ 1);
1095 g_ptr_array_index(trace
->streams
, stream
->stream_id
) = stream
;
1097 parent_def_scope
= NULL
;
1098 if (stream
->packet_context_decl
) {
1099 stream
->packet_context
=
1100 stream
->packet_context_decl
->definition_new(stream
->packet_context_decl
,
1101 parent_def_scope
, 0, 0);
1102 set_dynamic_definition_scope(&stream
->packet_context
->p
,
1103 stream
->packet_context
->scope
,
1104 "stream.packet.context");
1105 parent_def_scope
= stream
->packet_context
->scope
;
1106 declaration_unref(stream
->packet_context_decl
);
1108 if (stream
->event_header_decl
) {
1109 stream
->event_header
=
1110 stream
->event_header_decl
->definition_new(stream
->event_header_decl
,
1111 parent_def_scope
, 0, 0);
1112 set_dynamic_definition_scope(&stream
->event_header
->p
,
1113 stream
->event_header
->scope
,
1114 "stream.event.header");
1115 parent_def_scope
= stream
->event_header
->scope
;
1116 declaration_unref(stream
->event_header_decl
);
1118 if (stream
->event_context_decl
) {
1119 stream
->event_context
=
1120 stream
->event_context_decl
->definition_new(stream
->event_context_decl
,
1121 parent_def_scope
, 0, 0);
1122 set_dynamic_definition_scope(&stream
->event_context
->p
,
1123 stream
->event_context
->scope
,
1124 "stream.event.context");
1125 parent_def_scope
= stream
->event_context
->scope
;
1126 declaration_unref(stream
->event_context_decl
);
1128 stream
->definition_scope
= parent_def_scope
;
1133 declaration_unref(stream
->event_header
);
1134 declaration_unref(stream
->event_context
);
1135 declaration_unref(stream
->packet_context
);
1136 g_ptr_array_free(stream
->events_by_id
, TRUE
);
1137 g_hash_table_free(stream
->event_quark_to_id
);
1138 free_definition_scope(stream
->definition_scope
);
1139 free_declaration_scope(stream
->declaration_scope
);
1145 int ctf_trace_declaration_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_trace
*trace
)
1149 switch (node
->type
) {
1151 ret
= ctf_typedef_visit(fd
, depth
+ 1,
1152 &node
->u
._typedef
.declaration_specifier
,
1153 &node
->u
._typedef
.type_declarators
,
1154 trace
->declaration_scope
);
1158 case NODE_TYPEALIAS
:
1159 ret
= ctf_typealias_visit(fd
, depth
+ 1,
1160 &node
->u
.typealias
.target
, &node
->u
.typealias
.alias
1161 trace
->declaration_scope
);
1165 case NODE_CTF_EXPRESSION
:
1169 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
1170 if (!strcmp(left
, "major")) {
1171 if (CTF_EVENT_FIELD_IS_SET(trace
, major
))
1173 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &trace
->major
);
1175 fprintf(stderr
, "[error] %s: unexpected unary expression for trace major number\n", __func__
);
1178 CTF_EVENT_SET_FIELD(trace
, major
);
1179 } else if (!strcmp(left
, "minor")) {
1180 if (CTF_EVENT_FIELD_IS_SET(trace
, minor
))
1182 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &trace
->minor
);
1184 fprintf(stderr
, "[error] %s: unexpected unary expression for trace minor number\n", __func__
);
1187 CTF_EVENT_SET_FIELD(trace
, minor
);
1188 } else if (!strcmp(left
, "word_size")) {
1189 if (CTF_EVENT_FIELD_IS_SET(trace
, word_size
))
1191 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &trace
->word_size
);
1193 fprintf(stderr
, "[error] %s: unexpected unary expression for trace word_size\n", __func__
);
1196 CTF_EVENT_SET_FIELD(trace
, word_size
);
1197 } else if (!strcmp(left
, "uuid")) {
1198 if (CTF_EVENT_FIELD_IS_SET(trace
, uuid
))
1200 ret
= get_unary_uuid(&node
->u
.ctf_expression
.right
, &trace
->uuid
);
1202 fprintf(stderr
, "[error] %s: unexpected unary expression for trace uuid\n", __func__
);
1205 CTF_EVENT_SET_FIELD(trace
, uuid
);
1212 /* TODO: declaration specifier should be added. */
1219 int ctf_trace_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_trace
*trace
)
1222 struct ctf_node
*iter
;
1224 if (trace
->declaration_scope
)
1226 trace
->declaration_scope
= new_declaration_scope(trace
->root_declaration_scope
);
1227 trace
->definition_scope
= new_dynamic_definition_scope(trace
->root_definition_scope
);
1228 trace
->streams
= g_ptr_array_new();
1229 cds_list_for_each_entry(iter
, &node
->u
.trace
.declaration_list
, siblings
) {
1230 ret
= ctf_trace_declaration_visit(fd
, depth
+ 1, iter
, trace
);
1234 if (!CTF_EVENT_FIELD_IS_SET(trace
, major
)) {
1238 if (!CTF_EVENT_FIELD_IS_SET(trace
, minor
)) {
1242 if (!CTF_EVENT_FIELD_IS_SET(trace
, uuid
)) {
1246 if (!CTF_EVENT_FIELD_IS_SET(trace
, word_size
)) {
1253 g_ptr_array_free(trace
->streams
, TRUE
);
1254 free_definition_scope(stream
->definition_scope
);
1255 free_declaration_scope(stream
->declaration_scope
);
1259 int _ctf_visitor(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_trace
*trace
)
1262 struct ctf_node
*iter
;
1264 switch (node
->type
) {
1266 cds_list_for_each_entry(iter
, &node
->u
.root
._typedef
,
1268 ret
= ctf_typedef_visit(fd
, depth
+ 1,
1269 &iter
->u
._typedef
.declaration_specifier
,
1270 &iter
->u
._typedef
.type_declarators
,
1271 trace
->root_declaration_scope
);
1275 cds_list_for_each_entry(iter
, &node
->u
.root
.typealias
,
1277 ret
= ctf_typealias_visit(fd
, depth
+ 1,
1278 &iter
->u
.typealias
.target
, &iter
->u
.typealias
.alias
1279 trace
->root_declaration_scope
);
1283 cds_list_for_each_entry(iter
, &node
->u
.root
.declaration_specifier
, siblings
) {
1284 ret
= ctf_declaration_specifier_visit(fd
, depth
, iter
,
1285 trace
->root_declaration_scope
);
1289 cds_list_for_each_entry(iter
, &node
->u
.root
.trace
, siblings
) {
1290 ret
= ctf_trace_visit(fd
, depth
+ 1, iter
, trace
);
1294 cds_list_for_each_entry(iter
, &node
->u
.root
.stream
, siblings
) {
1295 ret
= ctf_stream_visit(fd
, depth
+ 1, iter
,
1296 trace
->root_declaration_scope
, trace
);
1300 cds_list_for_each_entry(iter
, &node
->u
.root
.event
, siblings
) {
1301 ret
= ctf_event_visit(fd
, depth
+ 1, iter
,
1302 trace
->root_declaration_scope
, trace
);
1309 fprintf(stderr
, "[error] %s: unknown node type %d\n", __func__
,