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/babeltrace-internal.h>
28 #include <babeltrace/list.h>
29 #include <babeltrace/types.h>
30 #include <babeltrace/ctf/metadata.h>
31 #include <babeltrace/uuid.h>
32 #include <babeltrace/endian.h>
33 #include "ctf-scanner.h"
34 #include "ctf-parser.h"
37 #define fprintf_dbg(fd, fmt, args...) fprintf(fd, "%s: " fmt, __func__, ## args)
39 #define _bt_list_first_entry(ptr, type, member) \
40 bt_list_entry((ptr)->next, type, member)
42 int opt_clock_force_correlate
;
45 struct declaration
*ctf_type_specifier_list_visit(FILE *fd
,
46 int depth
, struct ctf_node
*type_specifier_list
,
47 struct declaration_scope
*declaration_scope
,
48 struct ctf_trace
*trace
);
51 int ctf_stream_visit(FILE *fd
, int depth
, struct ctf_node
*node
,
52 struct declaration_scope
*parent_declaration_scope
, struct ctf_trace
*trace
);
55 int is_unary_string(struct bt_list_head
*head
)
57 struct ctf_node
*node
;
59 bt_list_for_each_entry(node
, head
, siblings
) {
60 if (node
->type
!= NODE_UNARY_EXPRESSION
)
62 if (node
->u
.unary_expression
.type
!= UNARY_STRING
)
69 * String returned must be freed by the caller using g_free.
72 char *concatenate_unary_strings(struct bt_list_head
*head
)
74 struct ctf_node
*node
;
78 str
= g_string_new("");
79 bt_list_for_each_entry(node
, head
, siblings
) {
82 assert(node
->type
== NODE_UNARY_EXPRESSION
);
83 assert(node
->u
.unary_expression
.type
== UNARY_STRING
);
84 assert((node
->u
.unary_expression
.link
== UNARY_LINK_UNKNOWN
)
86 switch (node
->u
.unary_expression
.link
) {
88 g_string_append(str
, ".");
91 g_string_append(str
, "->");
94 g_string_append(str
, "...");
99 src_string
= node
->u
.unary_expression
.u
.string
;
100 g_string_append(str
, src_string
);
103 return g_string_free(str
, FALSE
);
107 GQuark
get_map_clock_name_value(struct bt_list_head
*head
)
109 struct ctf_node
*node
;
110 const char *name
= NULL
;
113 bt_list_for_each_entry(node
, head
, siblings
) {
116 assert(node
->type
== NODE_UNARY_EXPRESSION
);
117 assert(node
->u
.unary_expression
.type
== UNARY_STRING
);
118 assert((node
->u
.unary_expression
.link
== UNARY_LINK_UNKNOWN
)
120 /* needs to be chained with . */
121 switch (node
->u
.unary_expression
.link
) {
124 case UNARY_ARROWLINK
:
125 case UNARY_DOTDOTDOT
:
130 src_string
= node
->u
.unary_expression
.u
.string
;
132 case 0: if (strcmp("clock", src_string
) != 0) {
136 case 1: name
= src_string
;
138 case 2: if (strcmp("value", src_string
) != 0) {
143 return 0; /* extra identifier, unknown */
147 return g_quark_from_string(name
);
151 int is_unary_unsigned(struct bt_list_head
*head
)
153 struct ctf_node
*node
;
155 bt_list_for_each_entry(node
, head
, siblings
) {
156 if (node
->type
!= NODE_UNARY_EXPRESSION
)
158 if (node
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
)
165 int get_unary_unsigned(struct bt_list_head
*head
, uint64_t *value
)
167 struct ctf_node
*node
;
170 bt_list_for_each_entry(node
, head
, siblings
) {
171 assert(node
->type
== NODE_UNARY_EXPRESSION
);
172 assert(node
->u
.unary_expression
.type
== UNARY_UNSIGNED_CONSTANT
);
173 assert(node
->u
.unary_expression
.link
== UNARY_LINK_UNKNOWN
);
175 *value
= node
->u
.unary_expression
.u
.unsigned_constant
;
182 int is_unary_signed(struct bt_list_head
*head
)
184 struct ctf_node
*node
;
186 bt_list_for_each_entry(node
, head
, siblings
) {
187 if (node
->type
!= NODE_UNARY_EXPRESSION
)
189 if (node
->u
.unary_expression
.type
!= UNARY_SIGNED_CONSTANT
)
196 int get_unary_signed(struct bt_list_head
*head
, int64_t *value
)
198 struct ctf_node
*node
;
201 bt_list_for_each_entry(node
, head
, siblings
) {
202 assert(node
->type
== NODE_UNARY_EXPRESSION
);
203 assert(node
->u
.unary_expression
.type
== UNARY_UNSIGNED_CONSTANT
204 || node
->u
.unary_expression
.type
== UNARY_SIGNED_CONSTANT
);
205 assert(node
->u
.unary_expression
.link
== UNARY_LINK_UNKNOWN
);
207 switch (node
->u
.unary_expression
.type
) {
208 case UNARY_UNSIGNED_CONSTANT
:
209 *value
= (int64_t) node
->u
.unary_expression
.u
.unsigned_constant
;
211 case UNARY_SIGNED_CONSTANT
:
212 *value
= node
->u
.unary_expression
.u
.signed_constant
;
223 int get_unary_uuid(struct bt_list_head
*head
, unsigned char *uuid
)
225 struct ctf_node
*node
;
229 bt_list_for_each_entry(node
, head
, siblings
) {
230 const char *src_string
;
232 assert(node
->type
== NODE_UNARY_EXPRESSION
);
233 assert(node
->u
.unary_expression
.type
== UNARY_STRING
);
234 assert(node
->u
.unary_expression
.link
== UNARY_LINK_UNKNOWN
);
236 src_string
= node
->u
.unary_expression
.u
.string
;
237 ret
= babeltrace_uuid_parse(src_string
, uuid
);
243 struct ctf_stream_declaration
*trace_stream_lookup(struct ctf_trace
*trace
, uint64_t stream_id
)
245 if (trace
->streams
->len
<= stream_id
)
247 return g_ptr_array_index(trace
->streams
, stream_id
);
251 struct ctf_clock
*trace_clock_lookup(struct ctf_trace
*trace
, GQuark clock_name
)
253 return g_hash_table_lookup(trace
->clocks
, (gpointer
) (unsigned long) clock_name
);
257 int visit_type_specifier(FILE *fd
, struct ctf_node
*type_specifier
, GString
*str
)
259 assert(type_specifier
->type
== NODE_TYPE_SPECIFIER
);
261 switch (type_specifier
->u
.type_specifier
.type
) {
263 g_string_append(str
, "void");
266 g_string_append(str
, "char");
269 g_string_append(str
, "short");
272 g_string_append(str
, "int");
275 g_string_append(str
, "long");
278 g_string_append(str
, "float");
280 case TYPESPEC_DOUBLE
:
281 g_string_append(str
, "double");
283 case TYPESPEC_SIGNED
:
284 g_string_append(str
, "signed");
286 case TYPESPEC_UNSIGNED
:
287 g_string_append(str
, "unsigned");
290 g_string_append(str
, "bool");
292 case TYPESPEC_COMPLEX
:
293 g_string_append(str
, "_Complex");
295 case TYPESPEC_IMAGINARY
:
296 g_string_append(str
, "_Imaginary");
299 g_string_append(str
, "const");
301 case TYPESPEC_ID_TYPE
:
302 if (type_specifier
->u
.type_specifier
.id_type
)
303 g_string_append(str
, type_specifier
->u
.type_specifier
.id_type
);
305 case TYPESPEC_STRUCT
:
307 struct ctf_node
*node
= type_specifier
->u
.type_specifier
.node
;
309 if (!node
->u
._struct
.name
) {
310 fprintf(fd
, "[error] %s: unexpected empty variant name\n", __func__
);
313 g_string_append(str
, "struct ");
314 g_string_append(str
, node
->u
._struct
.name
);
317 case TYPESPEC_VARIANT
:
319 struct ctf_node
*node
= type_specifier
->u
.type_specifier
.node
;
321 if (!node
->u
.variant
.name
) {
322 fprintf(fd
, "[error] %s: unexpected empty variant name\n", __func__
);
325 g_string_append(str
, "variant ");
326 g_string_append(str
, node
->u
.variant
.name
);
331 struct ctf_node
*node
= type_specifier
->u
.type_specifier
.node
;
333 if (!node
->u
._enum
.enum_id
) {
334 fprintf(fd
, "[error] %s: unexpected empty enum ID\n", __func__
);
337 g_string_append(str
, "enum ");
338 g_string_append(str
, node
->u
._enum
.enum_id
);
341 case TYPESPEC_FLOATING_POINT
:
342 case TYPESPEC_INTEGER
:
343 case TYPESPEC_STRING
:
345 fprintf(fd
, "[error] %s: unknown specifier\n", __func__
);
352 int visit_type_specifier_list(FILE *fd
, struct ctf_node
*type_specifier_list
, GString
*str
)
354 struct ctf_node
*iter
;
355 int alias_item_nr
= 0;
358 bt_list_for_each_entry(iter
, &type_specifier_list
->u
.type_specifier_list
.head
, siblings
) {
359 if (alias_item_nr
!= 0)
360 g_string_append(str
, " ");
362 ret
= visit_type_specifier(fd
, iter
, str
);
370 GQuark
create_typealias_identifier(FILE *fd
, int depth
,
371 struct ctf_node
*type_specifier_list
,
372 struct ctf_node
*node_type_declarator
)
374 struct ctf_node
*iter
;
380 str
= g_string_new("");
381 ret
= visit_type_specifier_list(fd
, type_specifier_list
, str
);
383 g_string_free(str
, TRUE
);
386 bt_list_for_each_entry(iter
, &node_type_declarator
->u
.type_declarator
.pointers
, siblings
) {
387 g_string_append(str
, " *");
388 if (iter
->u
.pointer
.const_qualifier
)
389 g_string_append(str
, " const");
391 str_c
= g_string_free(str
, FALSE
);
392 alias_q
= g_quark_from_string(str_c
);
398 struct declaration
*ctf_type_declarator_visit(FILE *fd
, int depth
,
399 struct ctf_node
*type_specifier_list
,
401 struct ctf_node
*node_type_declarator
,
402 struct declaration_scope
*declaration_scope
,
403 struct declaration
*nested_declaration
,
404 struct ctf_trace
*trace
)
407 * Visit type declarator by first taking care of sequence/array
408 * (recursively). Then, when we get to the identifier, take care
412 if (node_type_declarator
) {
413 assert(node_type_declarator
->u
.type_declarator
.type
!= TYPEDEC_UNKNOWN
);
415 /* TODO: gcc bitfields not supported yet. */
416 if (node_type_declarator
->u
.type_declarator
.bitfield_len
!= NULL
) {
417 fprintf(fd
, "[error] %s: gcc bitfields are not supported yet.\n", __func__
);
422 if (!nested_declaration
) {
423 if (node_type_declarator
&& !bt_list_empty(&node_type_declarator
->u
.type_declarator
.pointers
)) {
427 * If we have a pointer declarator, it _has_ to be present in
428 * the typealiases (else fail).
430 alias_q
= create_typealias_identifier(fd
, depth
,
431 type_specifier_list
, node_type_declarator
);
432 nested_declaration
= lookup_declaration(alias_q
, declaration_scope
);
433 if (!nested_declaration
) {
434 fprintf(fd
, "[error] %s: cannot find typealias \"%s\".\n", __func__
, g_quark_to_string(alias_q
));
437 if (nested_declaration
->id
== CTF_TYPE_INTEGER
) {
438 struct declaration_integer
*integer_declaration
=
439 container_of(nested_declaration
, struct declaration_integer
, p
);
440 /* For base to 16 for pointers (expected pretty-print) */
441 if (!integer_declaration
->base
) {
443 * We need to do a copy of the
444 * integer declaration to modify it. There could be other references to
447 integer_declaration
= integer_declaration_new(integer_declaration
->len
,
448 integer_declaration
->byte_order
, integer_declaration
->signedness
,
449 integer_declaration
->p
.alignment
, 16, integer_declaration
->encoding
,
450 integer_declaration
->clock
);
451 nested_declaration
= &integer_declaration
->p
;
455 nested_declaration
= ctf_type_specifier_list_visit(fd
, depth
,
456 type_specifier_list
, declaration_scope
, trace
);
460 if (!node_type_declarator
)
461 return nested_declaration
;
463 if (node_type_declarator
->u
.type_declarator
.type
== TYPEDEC_ID
) {
464 if (node_type_declarator
->u
.type_declarator
.u
.id
)
465 *field_name
= g_quark_from_string(node_type_declarator
->u
.type_declarator
.u
.id
);
468 return nested_declaration
;
470 struct declaration
*declaration
;
471 struct ctf_node
*first
;
475 if (!nested_declaration
) {
476 fprintf(fd
, "[error] %s: nested type is unknown.\n", __func__
);
480 /* create array/sequence, pass nested_declaration as child. */
481 if (bt_list_empty(&node_type_declarator
->u
.type_declarator
.u
.nested
.length
)) {
482 fprintf(fd
, "[error] %s: expecting length field reference or value.\n", __func__
);
485 first
= _bt_list_first_entry(&node_type_declarator
->u
.type_declarator
.u
.nested
.length
,
486 struct ctf_node
, siblings
);
487 assert(first
->type
== NODE_UNARY_EXPRESSION
);
489 switch (first
->u
.unary_expression
.type
) {
490 case UNARY_UNSIGNED_CONSTANT
:
492 struct declaration_array
*array_declaration
;
495 len
= first
->u
.unary_expression
.u
.unsigned_constant
;
496 array_declaration
= array_declaration_new(len
, nested_declaration
,
499 if (!array_declaration
) {
500 fprintf(fd
, "[error] %s: cannot create array declaration.\n", __func__
);
503 declaration
= &array_declaration
->p
;
508 /* Lookup unsigned integer definition, create sequence */
509 char *length_name
= concatenate_unary_strings(&node_type_declarator
->u
.type_declarator
.u
.nested
.length
);
510 struct declaration_sequence
*sequence_declaration
;
512 sequence_declaration
= sequence_declaration_new(length_name
, nested_declaration
, declaration_scope
);
513 if (!sequence_declaration
) {
514 fprintf(fd
, "[error] %s: cannot create sequence declaration.\n", __func__
);
517 declaration
= &sequence_declaration
->p
;
524 /* Pass it as content of outer container */
525 declaration
= ctf_type_declarator_visit(fd
, depth
,
526 type_specifier_list
, field_name
,
527 node_type_declarator
->u
.type_declarator
.u
.nested
.type_declarator
,
528 declaration_scope
, declaration
, trace
);
534 int ctf_struct_type_declarators_visit(FILE *fd
, int depth
,
535 struct declaration_struct
*struct_declaration
,
536 struct ctf_node
*type_specifier_list
,
537 struct bt_list_head
*type_declarators
,
538 struct declaration_scope
*declaration_scope
,
539 struct ctf_trace
*trace
)
541 struct ctf_node
*iter
;
544 bt_list_for_each_entry(iter
, type_declarators
, siblings
) {
545 struct declaration
*field_declaration
;
547 field_declaration
= ctf_type_declarator_visit(fd
, depth
,
550 struct_declaration
->scope
,
552 if (!field_declaration
) {
553 fprintf(fd
, "[error] %s: unable to find struct field declaration type\n", __func__
);
557 /* Check if field with same name already exists */
558 if (struct_declaration_lookup_field_index(struct_declaration
, field_name
) >= 0) {
559 fprintf(fd
, "[error] %s: duplicate field %s in struct\n", __func__
, g_quark_to_string(field_name
));
563 struct_declaration_add_field(struct_declaration
,
564 g_quark_to_string(field_name
),
571 int ctf_variant_type_declarators_visit(FILE *fd
, int depth
,
572 struct declaration_untagged_variant
*untagged_variant_declaration
,
573 struct ctf_node
*type_specifier_list
,
574 struct bt_list_head
*type_declarators
,
575 struct declaration_scope
*declaration_scope
,
576 struct ctf_trace
*trace
)
578 struct ctf_node
*iter
;
581 bt_list_for_each_entry(iter
, type_declarators
, siblings
) {
582 struct declaration
*field_declaration
;
584 field_declaration
= ctf_type_declarator_visit(fd
, depth
,
587 untagged_variant_declaration
->scope
,
589 if (!field_declaration
) {
590 fprintf(fd
, "[error] %s: unable to find variant field declaration type\n", __func__
);
594 if (untagged_variant_declaration_get_field_from_tag(untagged_variant_declaration
, field_name
) != NULL
) {
595 fprintf(fd
, "[error] %s: duplicate field %s in variant\n", __func__
, g_quark_to_string(field_name
));
600 untagged_variant_declaration_add_field(untagged_variant_declaration
,
601 g_quark_to_string(field_name
),
608 int ctf_typedef_visit(FILE *fd
, int depth
, struct declaration_scope
*scope
,
609 struct ctf_node
*type_specifier_list
,
610 struct bt_list_head
*type_declarators
,
611 struct ctf_trace
*trace
)
613 struct ctf_node
*iter
;
616 bt_list_for_each_entry(iter
, type_declarators
, siblings
) {
617 struct declaration
*type_declaration
;
620 type_declaration
= ctf_type_declarator_visit(fd
, depth
,
624 if (!type_declaration
) {
625 fprintf(fd
, "[error] %s: problem creating type declaration\n", __func__
);
629 * Don't allow typedef and typealias of untagged
632 if (type_declaration
->id
== CTF_TYPE_UNTAGGED_VARIANT
) {
633 fprintf(fd
, "[error] %s: typedef of untagged variant is not permitted.\n", __func__
);
634 declaration_unref(type_declaration
);
637 ret
= register_declaration(identifier
, type_declaration
, scope
);
639 type_declaration
->declaration_free(type_declaration
);
647 int ctf_typealias_visit(FILE *fd
, int depth
, struct declaration_scope
*scope
,
648 struct ctf_node
*target
, struct ctf_node
*alias
,
649 struct ctf_trace
*trace
)
651 struct declaration
*type_declaration
;
652 struct ctf_node
*node
;
657 /* See ctf_visitor_type_declarator() in the semantic validator. */
660 * Create target type declaration.
663 if (bt_list_empty(&target
->u
.typealias_target
.type_declarators
))
666 node
= _bt_list_first_entry(&target
->u
.typealias_target
.type_declarators
,
667 struct ctf_node
, siblings
);
668 type_declaration
= ctf_type_declarator_visit(fd
, depth
,
669 target
->u
.typealias_target
.type_specifier_list
,
672 if (!type_declaration
) {
673 fprintf(fd
, "[error] %s: problem creating type declaration\n", __func__
);
678 * Don't allow typedef and typealias of untagged
681 if (type_declaration
->id
== CTF_TYPE_UNTAGGED_VARIANT
) {
682 fprintf(fd
, "[error] %s: typedef of untagged variant is not permitted.\n", __func__
);
683 declaration_unref(type_declaration
);
687 * The semantic validator does not check whether the target is
688 * abstract or not (if it has an identifier). Check it here.
691 fprintf(fd
, "[error] %s: expecting empty identifier\n", __func__
);
696 * Create alias identifier.
699 node
= _bt_list_first_entry(&alias
->u
.typealias_alias
.type_declarators
,
700 struct ctf_node
, siblings
);
701 alias_q
= create_typealias_identifier(fd
, depth
,
702 alias
->u
.typealias_alias
.type_specifier_list
, node
);
703 err
= register_declaration(alias_q
, type_declaration
, scope
);
709 if (type_declaration
) {
710 type_declaration
->declaration_free(type_declaration
);
716 int ctf_struct_declaration_list_visit(FILE *fd
, int depth
,
717 struct ctf_node
*iter
, struct declaration_struct
*struct_declaration
,
718 struct ctf_trace
*trace
)
722 switch (iter
->type
) {
724 /* For each declarator, declare type and add type to struct declaration scope */
725 ret
= ctf_typedef_visit(fd
, depth
,
726 struct_declaration
->scope
,
727 iter
->u
._typedef
.type_specifier_list
,
728 &iter
->u
._typedef
.type_declarators
, trace
);
733 /* Declare type with declarator and add type to struct declaration scope */
734 ret
= ctf_typealias_visit(fd
, depth
,
735 struct_declaration
->scope
,
736 iter
->u
.typealias
.target
,
737 iter
->u
.typealias
.alias
, trace
);
741 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
742 /* Add field to structure declaration */
743 ret
= ctf_struct_type_declarators_visit(fd
, depth
,
745 iter
->u
.struct_or_variant_declaration
.type_specifier_list
,
746 &iter
->u
.struct_or_variant_declaration
.type_declarators
,
747 struct_declaration
->scope
, trace
);
752 fprintf(fd
, "[error] %s: unexpected node type %d\n", __func__
, (int) iter
->type
);
759 int ctf_variant_declaration_list_visit(FILE *fd
, int depth
,
760 struct ctf_node
*iter
,
761 struct declaration_untagged_variant
*untagged_variant_declaration
,
762 struct ctf_trace
*trace
)
766 switch (iter
->type
) {
768 /* For each declarator, declare type and add type to variant declaration scope */
769 ret
= ctf_typedef_visit(fd
, depth
,
770 untagged_variant_declaration
->scope
,
771 iter
->u
._typedef
.type_specifier_list
,
772 &iter
->u
._typedef
.type_declarators
, trace
);
777 /* Declare type with declarator and add type to variant declaration scope */
778 ret
= ctf_typealias_visit(fd
, depth
,
779 untagged_variant_declaration
->scope
,
780 iter
->u
.typealias
.target
,
781 iter
->u
.typealias
.alias
, trace
);
785 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
786 /* Add field to structure declaration */
787 ret
= ctf_variant_type_declarators_visit(fd
, depth
,
788 untagged_variant_declaration
,
789 iter
->u
.struct_or_variant_declaration
.type_specifier_list
,
790 &iter
->u
.struct_or_variant_declaration
.type_declarators
,
791 untagged_variant_declaration
->scope
, trace
);
796 fprintf(fd
, "[error] %s: unexpected node type %d\n", __func__
, (int) iter
->type
);
803 struct declaration
*ctf_declaration_struct_visit(FILE *fd
,
804 int depth
, const char *name
, struct bt_list_head
*declaration_list
,
805 int has_body
, struct bt_list_head
*min_align
,
806 struct declaration_scope
*declaration_scope
,
807 struct ctf_trace
*trace
)
809 struct declaration_struct
*struct_declaration
;
810 struct ctf_node
*iter
;
814 * For named struct (without body), lookup in
815 * declaration scope. Don't take reference on struct
816 * declaration: ref is only taken upon definition.
821 lookup_struct_declaration(g_quark_from_string(name
),
823 return &struct_declaration
->p
;
825 uint64_t min_align_value
= 0;
827 /* For unnamed struct, create type */
828 /* For named struct (with body), create type and add to declaration scope */
830 if (lookup_struct_declaration(g_quark_from_string(name
),
831 declaration_scope
)) {
833 fprintf(fd
, "[error] %s: struct %s already declared in scope\n", __func__
, name
);
837 if (!bt_list_empty(min_align
)) {
838 ret
= get_unary_unsigned(min_align
, &min_align_value
);
840 fprintf(fd
, "[error] %s: unexpected unary expression for structure \"align\" attribute\n", __func__
);
845 struct_declaration
= struct_declaration_new(declaration_scope
,
847 bt_list_for_each_entry(iter
, declaration_list
, siblings
) {
848 ret
= ctf_struct_declaration_list_visit(fd
, depth
+ 1, iter
,
849 struct_declaration
, trace
);
851 goto error_free_declaration
;
854 ret
= register_struct_declaration(g_quark_from_string(name
),
859 return &struct_declaration
->p
;
861 error_free_declaration
:
862 struct_declaration
->p
.declaration_free(&struct_declaration
->p
);
868 struct declaration
*ctf_declaration_variant_visit(FILE *fd
,
869 int depth
, const char *name
, const char *choice
,
870 struct bt_list_head
*declaration_list
,
871 int has_body
, struct declaration_scope
*declaration_scope
,
872 struct ctf_trace
*trace
)
874 struct declaration_untagged_variant
*untagged_variant_declaration
;
875 struct declaration_variant
*variant_declaration
;
876 struct ctf_node
*iter
;
880 * For named variant (without body), lookup in
881 * declaration scope. Don't take reference on variant
882 * declaration: ref is only taken upon definition.
886 untagged_variant_declaration
=
887 lookup_variant_declaration(g_quark_from_string(name
),
890 /* For unnamed variant, create type */
891 /* For named variant (with body), create type and add to declaration scope */
893 if (lookup_variant_declaration(g_quark_from_string(name
),
894 declaration_scope
)) {
896 fprintf(fd
, "[error] %s: variant %s already declared in scope\n", __func__
, name
);
900 untagged_variant_declaration
= untagged_variant_declaration_new(declaration_scope
);
901 bt_list_for_each_entry(iter
, declaration_list
, siblings
) {
902 ret
= ctf_variant_declaration_list_visit(fd
, depth
+ 1, iter
,
903 untagged_variant_declaration
, trace
);
908 ret
= register_variant_declaration(g_quark_from_string(name
),
909 untagged_variant_declaration
,
915 * if tagged, create tagged variant and return. else return
919 return &untagged_variant_declaration
->p
;
921 variant_declaration
= variant_declaration_new(untagged_variant_declaration
, choice
);
922 if (!variant_declaration
)
924 declaration_unref(&untagged_variant_declaration
->p
);
925 return &variant_declaration
->p
;
928 untagged_variant_declaration
->p
.declaration_free(&untagged_variant_declaration
->p
);
933 int ctf_enumerator_list_visit(FILE *fd
, int depth
,
934 struct ctf_node
*enumerator
,
935 struct declaration_enum
*enum_declaration
)
938 struct ctf_node
*iter
;
940 q
= g_quark_from_string(enumerator
->u
.enumerator
.id
);
941 if (enum_declaration
->integer_declaration
->signedness
) {
945 bt_list_for_each_entry(iter
, &enumerator
->u
.enumerator
.values
, siblings
) {
948 assert(iter
->type
== NODE_UNARY_EXPRESSION
);
954 switch (iter
->u
.unary_expression
.type
) {
955 case UNARY_SIGNED_CONSTANT
:
956 *target
= iter
->u
.unary_expression
.u
.signed_constant
;
958 case UNARY_UNSIGNED_CONSTANT
:
959 *target
= iter
->u
.unary_expression
.u
.unsigned_constant
;
962 fprintf(fd
, "[error] %s: invalid enumerator\n", __func__
);
966 fprintf(fd
, "[error] %s: invalid enumerator\n", __func__
);
973 enum_signed_insert(enum_declaration
, start
, end
, q
);
978 bt_list_for_each_entry(iter
, &enumerator
->u
.enumerator
.values
, siblings
) {
981 assert(iter
->type
== NODE_UNARY_EXPRESSION
);
987 switch (iter
->u
.unary_expression
.type
) {
988 case UNARY_UNSIGNED_CONSTANT
:
989 *target
= iter
->u
.unary_expression
.u
.unsigned_constant
;
991 case UNARY_SIGNED_CONSTANT
:
993 * We don't accept signed constants for enums with unsigned
996 fprintf(fd
, "[error] %s: invalid enumerator (signed constant encountered, but enum container type is unsigned)\n", __func__
);
999 fprintf(fd
, "[error] %s: invalid enumerator\n", __func__
);
1003 fprintf(fd
, "[error] %s: invalid enumerator\n", __func__
);
1010 enum_unsigned_insert(enum_declaration
, start
, end
, q
);
1016 struct declaration
*ctf_declaration_enum_visit(FILE *fd
, int depth
,
1018 struct ctf_node
*container_type
,
1019 struct bt_list_head
*enumerator_list
,
1021 struct declaration_scope
*declaration_scope
,
1022 struct ctf_trace
*trace
)
1024 struct declaration
*declaration
;
1025 struct declaration_enum
*enum_declaration
;
1026 struct declaration_integer
*integer_declaration
;
1027 struct ctf_node
*iter
;
1032 * For named enum (without body), lookup in
1033 * declaration scope. Don't take reference on enum
1034 * declaration: ref is only taken upon definition.
1039 lookup_enum_declaration(g_quark_from_string(name
),
1041 return &enum_declaration
->p
;
1043 /* For unnamed enum, create type */
1044 /* For named enum (with body), create type and add to declaration scope */
1046 if (lookup_enum_declaration(g_quark_from_string(name
),
1047 declaration_scope
)) {
1049 fprintf(fd
, "[error] %s: enum %s already declared in scope\n", __func__
, name
);
1053 if (!container_type
) {
1054 declaration
= lookup_declaration(g_quark_from_static_string("int"),
1057 fprintf(fd
, "[error] %s: \"int\" type declaration missing for enumeration\n", __func__
);
1061 declaration
= ctf_type_declarator_visit(fd
, depth
,
1068 fprintf(fd
, "[error] %s: unable to create container type for enumeration\n", __func__
);
1071 if (declaration
->id
!= CTF_TYPE_INTEGER
) {
1072 fprintf(fd
, "[error] %s: container type for enumeration is not integer\n", __func__
);
1075 integer_declaration
= container_of(declaration
, struct declaration_integer
, p
);
1076 enum_declaration
= enum_declaration_new(integer_declaration
);
1077 declaration_unref(&integer_declaration
->p
); /* leave ref to enum */
1078 bt_list_for_each_entry(iter
, enumerator_list
, siblings
) {
1079 ret
= ctf_enumerator_list_visit(fd
, depth
+ 1, iter
, enum_declaration
);
1084 ret
= register_enum_declaration(g_quark_from_string(name
),
1089 return &enum_declaration
->p
;
1092 enum_declaration
->p
.declaration_free(&enum_declaration
->p
);
1097 struct declaration
*ctf_declaration_type_specifier_visit(FILE *fd
, int depth
,
1098 struct ctf_node
*type_specifier_list
,
1099 struct declaration_scope
*declaration_scope
)
1102 struct declaration
*declaration
;
1107 str
= g_string_new("");
1108 ret
= visit_type_specifier_list(fd
, type_specifier_list
, str
);
1111 str_c
= g_string_free(str
, FALSE
);
1112 id_q
= g_quark_from_string(str_c
);
1114 declaration
= lookup_declaration(id_q
, declaration_scope
);
1119 * Returns 0/1 boolean, or < 0 on error.
1122 int get_boolean(FILE *fd
, int depth
, struct ctf_node
*unary_expression
)
1124 if (unary_expression
->type
!= NODE_UNARY_EXPRESSION
) {
1125 fprintf(fd
, "[error] %s: expecting unary expression\n",
1129 switch (unary_expression
->u
.unary_expression
.type
) {
1130 case UNARY_UNSIGNED_CONSTANT
:
1131 if (unary_expression
->u
.unary_expression
.u
.unsigned_constant
== 0)
1135 case UNARY_SIGNED_CONSTANT
:
1136 if (unary_expression
->u
.unary_expression
.u
.signed_constant
== 0)
1141 if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "true"))
1143 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "TRUE"))
1145 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "false"))
1147 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "FALSE"))
1150 fprintf(fd
, "[error] %s: unexpected string \"%s\"\n",
1151 __func__
, unary_expression
->u
.unary_expression
.u
.string
);
1156 fprintf(fd
, "[error] %s: unexpected unary expression type\n",
1164 int get_trace_byte_order(FILE *fd
, int depth
, struct ctf_node
*unary_expression
)
1168 if (unary_expression
->u
.unary_expression
.type
!= UNARY_STRING
) {
1169 fprintf(fd
, "[error] %s: byte_order: expecting string\n",
1173 if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "be"))
1174 byte_order
= BIG_ENDIAN
;
1175 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "le"))
1176 byte_order
= LITTLE_ENDIAN
;
1178 fprintf(fd
, "[error] %s: unexpected string \"%s\". Should be \"native\", \"network\", \"be\" or \"le\".\n",
1179 __func__
, unary_expression
->u
.unary_expression
.u
.string
);
1186 int get_byte_order(FILE *fd
, int depth
, struct ctf_node
*unary_expression
,
1187 struct ctf_trace
*trace
)
1191 if (unary_expression
->u
.unary_expression
.type
!= UNARY_STRING
) {
1192 fprintf(fd
, "[error] %s: byte_order: expecting string\n",
1196 if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "native"))
1197 byte_order
= trace
->byte_order
;
1198 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "network"))
1199 byte_order
= BIG_ENDIAN
;
1200 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "be"))
1201 byte_order
= BIG_ENDIAN
;
1202 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "le"))
1203 byte_order
= LITTLE_ENDIAN
;
1205 fprintf(fd
, "[error] %s: unexpected string \"%s\". Should be \"native\", \"network\", \"be\" or \"le\".\n",
1206 __func__
, unary_expression
->u
.unary_expression
.u
.string
);
1213 struct declaration
*ctf_declaration_integer_visit(FILE *fd
, int depth
,
1214 struct bt_list_head
*expressions
,
1215 struct ctf_trace
*trace
)
1217 struct ctf_node
*expression
;
1218 uint64_t alignment
= 1, size
= 0;
1219 int byte_order
= trace
->byte_order
;
1221 int has_alignment
= 0, has_size
= 0;
1223 enum ctf_string_encoding encoding
= CTF_STRING_NONE
;
1224 struct ctf_clock
*clock
= NULL
;
1225 struct declaration_integer
*integer_declaration
;
1227 bt_list_for_each_entry(expression
, expressions
, siblings
) {
1228 struct ctf_node
*left
, *right
;
1230 left
= _bt_list_first_entry(&expression
->u
.ctf_expression
.left
, struct ctf_node
, siblings
);
1231 right
= _bt_list_first_entry(&expression
->u
.ctf_expression
.right
, struct ctf_node
, siblings
);
1232 assert(left
->u
.unary_expression
.type
== UNARY_STRING
);
1233 if (!strcmp(left
->u
.unary_expression
.u
.string
, "signed")) {
1234 signedness
= get_boolean(fd
, depth
, right
);
1237 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "byte_order")) {
1238 byte_order
= get_byte_order(fd
, depth
, right
, trace
);
1241 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "size")) {
1242 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
1243 fprintf(fd
, "[error] %s: size: expecting unsigned constant\n",
1247 size
= right
->u
.unary_expression
.u
.unsigned_constant
;
1249 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "align")) {
1250 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
1251 fprintf(fd
, "[error] %s: align: expecting unsigned constant\n",
1255 alignment
= right
->u
.unary_expression
.u
.unsigned_constant
;
1256 /* Make sure alignment is a power of two */
1257 if (alignment
== 0 || (alignment
& (alignment
- 1)) != 0) {
1258 fprintf(fd
, "[error] %s: align: expecting power of two\n",
1263 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "base")) {
1264 switch (right
->u
.unary_expression
.type
) {
1265 case UNARY_UNSIGNED_CONSTANT
:
1266 switch (right
->u
.unary_expression
.u
.unsigned_constant
) {
1271 base
= right
->u
.unary_expression
.u
.unsigned_constant
;
1274 fprintf(fd
, "[error] %s: base not supported (%" PRIu64
")\n",
1275 __func__
, right
->u
.unary_expression
.u
.unsigned_constant
);
1281 char *s_right
= concatenate_unary_strings(&expression
->u
.ctf_expression
.right
);
1283 fprintf(fd
, "[error] %s: unexpected unary expression for integer base\n", __func__
);
1287 if (!strcmp(s_right
, "decimal") || !strcmp(s_right
, "dec") || !strcmp(s_right
, "d")
1288 || !strcmp(s_right
, "i") || !strcmp(s_right
, "u")) {
1290 } else if (!strcmp(s_right
, "hexadecimal") || !strcmp(s_right
, "hex")
1291 || !strcmp(s_right
, "x") || !strcmp(s_right
, "X")
1292 || !strcmp(s_right
, "p")) {
1294 } else if (!strcmp(s_right
, "octal") || !strcmp(s_right
, "oct")
1295 || !strcmp(s_right
, "o")) {
1297 } else if (!strcmp(s_right
, "binary") || !strcmp(s_right
, "b")) {
1300 fprintf(fd
, "[error] %s: unexpected expression for integer base (%s)\n", __func__
, s_right
);
1309 fprintf(fd
, "[error] %s: base: expecting unsigned constant or unary string\n",
1313 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "encoding")) {
1316 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
1317 fprintf(fd
, "[error] %s: encoding: expecting unary string\n",
1321 s_right
= concatenate_unary_strings(&expression
->u
.ctf_expression
.right
);
1323 fprintf(fd
, "[error] %s: unexpected unary expression for integer base\n", __func__
);
1327 if (!strcmp(s_right
, "UTF8")
1328 || !strcmp(s_right
, "utf8")
1329 || !strcmp(s_right
, "utf-8")
1330 || !strcmp(s_right
, "UTF-8"))
1331 encoding
= CTF_STRING_UTF8
;
1332 else if (!strcmp(s_right
, "ASCII")
1333 || !strcmp(s_right
, "ascii"))
1334 encoding
= CTF_STRING_ASCII
;
1335 else if (!strcmp(s_right
, "none"))
1336 encoding
= CTF_STRING_NONE
;
1338 fprintf(fd
, "[error] %s: unknown string encoding \"%s\"\n", __func__
, s_right
);
1343 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "map")) {
1346 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
1347 fprintf(fd
, "[error] %s: map: expecting identifier\n",
1351 /* currently only support clock.name.value */
1352 clock_name
= get_map_clock_name_value(&expression
->u
.ctf_expression
.right
);
1356 s_right
= concatenate_unary_strings(&expression
->u
.ctf_expression
.right
);
1358 fprintf(fd
, "[error] %s: unexpected unary expression for integer map\n", __func__
);
1362 fprintf(fd
, "[warning] %s: unknown map %s in integer declaration\n", __func__
,
1367 clock
= trace_clock_lookup(trace
, clock_name
);
1369 fprintf(fd
, "[error] %s: map: unable to find clock %s declaration\n",
1370 __func__
, g_quark_to_string(clock_name
));
1374 fprintf(fd
, "[warning] %s: unknown attribute name %s\n",
1375 __func__
, left
->u
.unary_expression
.u
.string
);
1376 /* Fall-through after warning */
1380 fprintf(fd
, "[error] %s: missing size attribute\n", __func__
);
1383 if (!has_alignment
) {
1384 if (size
% CHAR_BIT
) {
1385 /* bit-packed alignment */
1388 /* byte-packed alignment */
1389 alignment
= CHAR_BIT
;
1392 integer_declaration
= integer_declaration_new(size
,
1393 byte_order
, signedness
, alignment
,
1394 base
, encoding
, clock
);
1395 return &integer_declaration
->p
;
1399 struct declaration
*ctf_declaration_floating_point_visit(FILE *fd
, int depth
,
1400 struct bt_list_head
*expressions
,
1401 struct ctf_trace
*trace
)
1403 struct ctf_node
*expression
;
1404 uint64_t alignment
= 1, exp_dig
= 0, mant_dig
= 0,
1405 byte_order
= trace
->byte_order
;
1406 int has_alignment
= 0, has_exp_dig
= 0, has_mant_dig
= 0;
1407 struct declaration_float
*float_declaration
;
1409 bt_list_for_each_entry(expression
, expressions
, siblings
) {
1410 struct ctf_node
*left
, *right
;
1412 left
= _bt_list_first_entry(&expression
->u
.ctf_expression
.left
, struct ctf_node
, siblings
);
1413 right
= _bt_list_first_entry(&expression
->u
.ctf_expression
.right
, struct ctf_node
, siblings
);
1414 assert(left
->u
.unary_expression
.type
== UNARY_STRING
);
1415 if (!strcmp(left
->u
.unary_expression
.u
.string
, "byte_order")) {
1416 byte_order
= get_byte_order(fd
, depth
, right
, trace
);
1419 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "exp_dig")) {
1420 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
1421 fprintf(fd
, "[error] %s: exp_dig: expecting unsigned constant\n",
1425 exp_dig
= right
->u
.unary_expression
.u
.unsigned_constant
;
1427 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "mant_dig")) {
1428 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
1429 fprintf(fd
, "[error] %s: mant_dig: expecting unsigned constant\n",
1433 mant_dig
= right
->u
.unary_expression
.u
.unsigned_constant
;
1435 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "align")) {
1436 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
1437 fprintf(fd
, "[error] %s: align: expecting unsigned constant\n",
1441 alignment
= right
->u
.unary_expression
.u
.unsigned_constant
;
1442 /* Make sure alignment is a power of two */
1443 if (alignment
== 0 || (alignment
& (alignment
- 1)) != 0) {
1444 fprintf(fd
, "[error] %s: align: expecting power of two\n",
1450 fprintf(fd
, "[warning] %s: unknown attribute name %s\n",
1451 __func__
, left
->u
.unary_expression
.u
.string
);
1452 /* Fall-through after warning */
1455 if (!has_mant_dig
) {
1456 fprintf(fd
, "[error] %s: missing mant_dig attribute\n", __func__
);
1460 fprintf(fd
, "[error] %s: missing exp_dig attribute\n", __func__
);
1463 if (!has_alignment
) {
1464 if ((mant_dig
+ exp_dig
) % CHAR_BIT
) {
1465 /* bit-packed alignment */
1468 /* byte-packed alignment */
1469 alignment
= CHAR_BIT
;
1472 float_declaration
= float_declaration_new(mant_dig
, exp_dig
,
1473 byte_order
, alignment
);
1474 return &float_declaration
->p
;
1478 struct declaration
*ctf_declaration_string_visit(FILE *fd
, int depth
,
1479 struct bt_list_head
*expressions
,
1480 struct ctf_trace
*trace
)
1482 struct ctf_node
*expression
;
1483 const char *encoding_c
= NULL
;
1484 enum ctf_string_encoding encoding
= CTF_STRING_UTF8
;
1485 struct declaration_string
*string_declaration
;
1487 bt_list_for_each_entry(expression
, expressions
, siblings
) {
1488 struct ctf_node
*left
, *right
;
1490 left
= _bt_list_first_entry(&expression
->u
.ctf_expression
.left
, struct ctf_node
, siblings
);
1491 right
= _bt_list_first_entry(&expression
->u
.ctf_expression
.right
, struct ctf_node
, siblings
);
1492 assert(left
->u
.unary_expression
.type
== UNARY_STRING
);
1493 if (!strcmp(left
->u
.unary_expression
.u
.string
, "encoding")) {
1494 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
1495 fprintf(fd
, "[error] %s: encoding: expecting string\n",
1499 encoding_c
= right
->u
.unary_expression
.u
.string
;
1501 fprintf(fd
, "[warning] %s: unknown attribute name %s\n",
1502 __func__
, left
->u
.unary_expression
.u
.string
);
1503 /* Fall-through after warning */
1506 if (encoding_c
&& !strcmp(encoding_c
, "ASCII"))
1507 encoding
= CTF_STRING_ASCII
;
1508 string_declaration
= string_declaration_new(encoding
);
1509 return &string_declaration
->p
;
1514 struct declaration
*ctf_type_specifier_list_visit(FILE *fd
,
1515 int depth
, struct ctf_node
*type_specifier_list
,
1516 struct declaration_scope
*declaration_scope
,
1517 struct ctf_trace
*trace
)
1519 struct ctf_node
*first
;
1520 struct ctf_node
*node
;
1522 assert(type_specifier_list
->type
== NODE_TYPE_SPECIFIER_LIST
);
1524 first
= _bt_list_first_entry(&type_specifier_list
->u
.type_specifier_list
.head
, struct ctf_node
, siblings
);
1526 assert(first
->type
== NODE_TYPE_SPECIFIER
);
1528 node
= first
->u
.type_specifier
.node
;
1530 switch (first
->u
.type_specifier
.type
) {
1531 case TYPESPEC_FLOATING_POINT
:
1532 return ctf_declaration_floating_point_visit(fd
, depth
,
1533 &node
->u
.floating_point
.expressions
, trace
);
1534 case TYPESPEC_INTEGER
:
1535 return ctf_declaration_integer_visit(fd
, depth
,
1536 &node
->u
.integer
.expressions
, trace
);
1537 case TYPESPEC_STRING
:
1538 return ctf_declaration_string_visit(fd
, depth
,
1539 &node
->u
.string
.expressions
, trace
);
1540 case TYPESPEC_STRUCT
:
1541 return ctf_declaration_struct_visit(fd
, depth
,
1542 node
->u
._struct
.name
,
1543 &node
->u
._struct
.declaration_list
,
1544 node
->u
._struct
.has_body
,
1545 &node
->u
._struct
.min_align
,
1548 case TYPESPEC_VARIANT
:
1549 return ctf_declaration_variant_visit(fd
, depth
,
1550 node
->u
.variant
.name
,
1551 node
->u
.variant
.choice
,
1552 &node
->u
.variant
.declaration_list
,
1553 node
->u
.variant
.has_body
,
1557 return ctf_declaration_enum_visit(fd
, depth
,
1558 node
->u
._enum
.enum_id
,
1559 node
->u
._enum
.container_type
,
1560 &node
->u
._enum
.enumerator_list
,
1561 node
->u
._enum
.has_body
,
1567 case TYPESPEC_SHORT
:
1570 case TYPESPEC_FLOAT
:
1571 case TYPESPEC_DOUBLE
:
1572 case TYPESPEC_SIGNED
:
1573 case TYPESPEC_UNSIGNED
:
1575 case TYPESPEC_COMPLEX
:
1576 case TYPESPEC_IMAGINARY
:
1577 case TYPESPEC_CONST
:
1578 case TYPESPEC_ID_TYPE
:
1579 return ctf_declaration_type_specifier_visit(fd
, depth
,
1580 type_specifier_list
, declaration_scope
);
1582 fprintf(fd
, "[error] %s: unexpected node type %d\n", __func__
, (int) first
->u
.type_specifier
.type
);
1588 int ctf_event_declaration_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_event_declaration
*event
, struct ctf_trace
*trace
)
1592 switch (node
->type
) {
1594 ret
= ctf_typedef_visit(fd
, depth
+ 1,
1595 event
->declaration_scope
,
1596 node
->u
._typedef
.type_specifier_list
,
1597 &node
->u
._typedef
.type_declarators
,
1602 case NODE_TYPEALIAS
:
1603 ret
= ctf_typealias_visit(fd
, depth
+ 1,
1604 event
->declaration_scope
,
1605 node
->u
.typealias
.target
, node
->u
.typealias
.alias
,
1610 case NODE_CTF_EXPRESSION
:
1614 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
1615 if (!strcmp(left
, "name")) {
1618 if (CTF_EVENT_FIELD_IS_SET(event
, name
)) {
1619 fprintf(fd
, "[error] %s: name already declared in event declaration\n", __func__
);
1623 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
1625 fprintf(fd
, "[error] %s: unexpected unary expression for event name\n", __func__
);
1629 event
->name
= g_quark_from_string(right
);
1631 CTF_EVENT_SET_FIELD(event
, name
);
1632 } else if (!strcmp(left
, "id")) {
1633 if (CTF_EVENT_FIELD_IS_SET(event
, id
)) {
1634 fprintf(fd
, "[error] %s: id already declared in event declaration\n", __func__
);
1638 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &event
->id
);
1640 fprintf(fd
, "[error] %s: unexpected unary expression for event id\n", __func__
);
1644 CTF_EVENT_SET_FIELD(event
, id
);
1645 } else if (!strcmp(left
, "stream_id")) {
1646 if (CTF_EVENT_FIELD_IS_SET(event
, stream_id
)) {
1647 fprintf(fd
, "[error] %s: stream_id already declared in event declaration\n", __func__
);
1651 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &event
->stream_id
);
1653 fprintf(fd
, "[error] %s: unexpected unary expression for event stream_id\n", __func__
);
1657 event
->stream
= trace_stream_lookup(trace
, event
->stream_id
);
1658 if (!event
->stream
) {
1659 fprintf(fd
, "[error] %s: stream id %" PRIu64
" cannot be found\n", __func__
, event
->stream_id
);
1663 CTF_EVENT_SET_FIELD(event
, stream_id
);
1664 } else if (!strcmp(left
, "context")) {
1665 struct declaration
*declaration
;
1667 if (event
->context_decl
) {
1668 fprintf(fd
, "[error] %s: context already declared in event declaration\n", __func__
);
1672 declaration
= ctf_type_specifier_list_visit(fd
, depth
,
1673 _bt_list_first_entry(&node
->u
.ctf_expression
.right
,
1674 struct ctf_node
, siblings
),
1675 event
->declaration_scope
, trace
);
1680 if (declaration
->id
!= CTF_TYPE_STRUCT
) {
1684 event
->context_decl
= container_of(declaration
, struct declaration_struct
, p
);
1685 } else if (!strcmp(left
, "fields")) {
1686 struct declaration
*declaration
;
1688 if (event
->fields_decl
) {
1689 fprintf(fd
, "[error] %s: fields already declared in event declaration\n", __func__
);
1693 declaration
= ctf_type_specifier_list_visit(fd
, depth
,
1694 _bt_list_first_entry(&node
->u
.ctf_expression
.right
,
1695 struct ctf_node
, siblings
),
1696 event
->declaration_scope
, trace
);
1701 if (declaration
->id
!= CTF_TYPE_STRUCT
) {
1705 event
->fields_decl
= container_of(declaration
, struct declaration_struct
, p
);
1706 } else if (!strcmp(left
, "loglevel")) {
1707 int64_t loglevel
= -1;
1709 if (CTF_EVENT_FIELD_IS_SET(event
, loglevel
)) {
1710 fprintf(fd
, "[error] %s: loglevel already declared in event declaration\n", __func__
);
1714 ret
= get_unary_signed(&node
->u
.ctf_expression
.right
, &loglevel
);
1716 fprintf(fd
, "[error] %s: unexpected unary expression for event loglevel\n", __func__
);
1720 event
->loglevel
= (int) loglevel
;
1721 CTF_EVENT_SET_FIELD(event
, loglevel
);
1723 fprintf(fd
, "[warning] %s: attribute \"%s\" is unknown in event declaration.\n", __func__
, left
);
1724 /* Fall-through after warning */
1732 /* TODO: declaration specifier should be added. */
1739 int ctf_event_visit(FILE *fd
, int depth
, struct ctf_node
*node
,
1740 struct declaration_scope
*parent_declaration_scope
, struct ctf_trace
*trace
)
1743 struct ctf_node
*iter
;
1744 struct ctf_event_declaration
*event
;
1746 event
= g_new0(struct ctf_event_declaration
, 1);
1747 event
->declaration_scope
= new_declaration_scope(parent_declaration_scope
);
1748 event
->loglevel
= -1;
1749 bt_list_for_each_entry(iter
, &node
->u
.event
.declaration_list
, siblings
) {
1750 ret
= ctf_event_declaration_visit(fd
, depth
+ 1, iter
, event
, trace
);
1754 if (!CTF_EVENT_FIELD_IS_SET(event
, name
)) {
1756 fprintf(fd
, "[error] %s: missing name field in event declaration\n", __func__
);
1759 if (!CTF_EVENT_FIELD_IS_SET(event
, stream_id
)) {
1760 /* Allow missing stream_id if there is only a single stream */
1761 switch (trace
->streams
->len
) {
1762 case 0: /* Create stream if there was none. */
1763 ret
= ctf_stream_visit(fd
, depth
, NULL
, trace
->root_declaration_scope
, trace
);
1768 event
->stream_id
= 0;
1769 event
->stream
= trace_stream_lookup(trace
, event
->stream_id
);
1773 fprintf(fd
, "[error] %s: missing stream_id field in event declaration\n", __func__
);
1777 /* Allow only one event without id per stream */
1778 if (!CTF_EVENT_FIELD_IS_SET(event
, id
)
1779 && event
->stream
->events_by_id
->len
!= 0) {
1781 fprintf(fd
, "[error] %s: missing id field in event declaration\n", __func__
);
1784 if (event
->stream
->events_by_id
->len
<= event
->id
)
1785 g_ptr_array_set_size(event
->stream
->events_by_id
, event
->id
+ 1);
1786 g_ptr_array_index(event
->stream
->events_by_id
, event
->id
) = event
;
1787 g_hash_table_insert(event
->stream
->event_quark_to_id
,
1788 (gpointer
) (unsigned long) event
->name
,
1793 if (event
->fields_decl
)
1794 declaration_unref(&event
->fields_decl
->p
);
1795 if (event
->context_decl
)
1796 declaration_unref(&event
->context_decl
->p
);
1797 free_declaration_scope(event
->declaration_scope
);
1804 int ctf_stream_declaration_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_stream_declaration
*stream
, struct ctf_trace
*trace
)
1808 switch (node
->type
) {
1810 ret
= ctf_typedef_visit(fd
, depth
+ 1,
1811 stream
->declaration_scope
,
1812 node
->u
._typedef
.type_specifier_list
,
1813 &node
->u
._typedef
.type_declarators
,
1818 case NODE_TYPEALIAS
:
1819 ret
= ctf_typealias_visit(fd
, depth
+ 1,
1820 stream
->declaration_scope
,
1821 node
->u
.typealias
.target
, node
->u
.typealias
.alias
,
1826 case NODE_CTF_EXPRESSION
:
1830 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
1831 if (!strcmp(left
, "id")) {
1832 if (CTF_STREAM_FIELD_IS_SET(stream
, stream_id
)) {
1833 fprintf(fd
, "[error] %s: id already declared in stream declaration\n", __func__
);
1837 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &stream
->stream_id
);
1839 fprintf(fd
, "[error] %s: unexpected unary expression for stream id\n", __func__
);
1843 CTF_STREAM_SET_FIELD(stream
, stream_id
);
1844 } else if (!strcmp(left
, "event.header")) {
1845 struct declaration
*declaration
;
1847 if (stream
->event_header_decl
) {
1848 fprintf(fd
, "[error] %s: event.header already declared in stream declaration\n", __func__
);
1852 declaration
= ctf_type_specifier_list_visit(fd
, depth
,
1853 _bt_list_first_entry(&node
->u
.ctf_expression
.right
,
1854 struct ctf_node
, siblings
),
1855 stream
->declaration_scope
, trace
);
1860 if (declaration
->id
!= CTF_TYPE_STRUCT
) {
1864 stream
->event_header_decl
= container_of(declaration
, struct declaration_struct
, p
);
1865 } else if (!strcmp(left
, "event.context")) {
1866 struct declaration
*declaration
;
1868 if (stream
->event_context_decl
) {
1869 fprintf(fd
, "[error] %s: event.context already declared in stream declaration\n", __func__
);
1873 declaration
= ctf_type_specifier_list_visit(fd
, depth
,
1874 _bt_list_first_entry(&node
->u
.ctf_expression
.right
,
1875 struct ctf_node
, siblings
),
1876 stream
->declaration_scope
, trace
);
1881 if (declaration
->id
!= CTF_TYPE_STRUCT
) {
1885 stream
->event_context_decl
= container_of(declaration
, struct declaration_struct
, p
);
1886 } else if (!strcmp(left
, "packet.context")) {
1887 struct declaration
*declaration
;
1889 if (stream
->packet_context_decl
) {
1890 fprintf(fd
, "[error] %s: packet.context already declared in stream declaration\n", __func__
);
1894 declaration
= ctf_type_specifier_list_visit(fd
, depth
,
1895 _bt_list_first_entry(&node
->u
.ctf_expression
.right
,
1896 struct ctf_node
, siblings
),
1897 stream
->declaration_scope
, trace
);
1902 if (declaration
->id
!= CTF_TYPE_STRUCT
) {
1906 stream
->packet_context_decl
= container_of(declaration
, struct declaration_struct
, p
);
1908 fprintf(fd
, "[warning] %s: attribute \"%s\" is unknown in stream declaration.\n", __func__
, left
);
1909 /* Fall-through after warning */
1918 /* TODO: declaration specifier should be added. */
1925 int ctf_stream_visit(FILE *fd
, int depth
, struct ctf_node
*node
,
1926 struct declaration_scope
*parent_declaration_scope
, struct ctf_trace
*trace
)
1929 struct ctf_node
*iter
;
1930 struct ctf_stream_declaration
*stream
;
1932 stream
= g_new0(struct ctf_stream_declaration
, 1);
1933 stream
->declaration_scope
= new_declaration_scope(parent_declaration_scope
);
1934 stream
->events_by_id
= g_ptr_array_new();
1935 stream
->event_quark_to_id
= g_hash_table_new(g_direct_hash
, g_direct_equal
);
1936 stream
->streams
= g_ptr_array_new();
1938 bt_list_for_each_entry(iter
, &node
->u
.stream
.declaration_list
, siblings
) {
1939 ret
= ctf_stream_declaration_visit(fd
, depth
+ 1, iter
, stream
, trace
);
1944 if (CTF_STREAM_FIELD_IS_SET(stream
, stream_id
)) {
1945 /* check that packet header has stream_id field. */
1946 if (!trace
->packet_header_decl
1947 || struct_declaration_lookup_field_index(trace
->packet_header_decl
, g_quark_from_static_string("stream_id")) < 0) {
1949 fprintf(fd
, "[error] %s: missing stream_id field in packet header declaration, but stream_id attribute is declared for stream.\n", __func__
);
1953 /* Allow only one id-less stream */
1954 if (trace
->streams
->len
!= 0) {
1956 fprintf(fd
, "[error] %s: missing id field in stream declaration\n", __func__
);
1959 stream
->stream_id
= 0;
1961 if (trace
->streams
->len
<= stream
->stream_id
)
1962 g_ptr_array_set_size(trace
->streams
, stream
->stream_id
+ 1);
1963 g_ptr_array_index(trace
->streams
, stream
->stream_id
) = stream
;
1964 stream
->trace
= trace
;
1969 if (stream
->event_header_decl
)
1970 declaration_unref(&stream
->event_header_decl
->p
);
1971 if (stream
->event_context_decl
)
1972 declaration_unref(&stream
->event_context_decl
->p
);
1973 if (stream
->packet_context_decl
)
1974 declaration_unref(&stream
->packet_context_decl
->p
);
1975 g_ptr_array_free(stream
->streams
, TRUE
);
1976 g_ptr_array_free(stream
->events_by_id
, TRUE
);
1977 g_hash_table_destroy(stream
->event_quark_to_id
);
1978 free_declaration_scope(stream
->declaration_scope
);
1984 int ctf_trace_declaration_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_trace
*trace
)
1988 switch (node
->type
) {
1990 ret
= ctf_typedef_visit(fd
, depth
+ 1,
1991 trace
->declaration_scope
,
1992 node
->u
._typedef
.type_specifier_list
,
1993 &node
->u
._typedef
.type_declarators
,
1998 case NODE_TYPEALIAS
:
1999 ret
= ctf_typealias_visit(fd
, depth
+ 1,
2000 trace
->declaration_scope
,
2001 node
->u
.typealias
.target
, node
->u
.typealias
.alias
,
2006 case NODE_CTF_EXPRESSION
:
2010 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
2011 if (!strcmp(left
, "major")) {
2012 if (CTF_TRACE_FIELD_IS_SET(trace
, major
)) {
2013 fprintf(fd
, "[error] %s: major already declared in trace declaration\n", __func__
);
2017 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &trace
->major
);
2019 fprintf(fd
, "[error] %s: unexpected unary expression for trace major number\n", __func__
);
2023 CTF_TRACE_SET_FIELD(trace
, major
);
2024 } else if (!strcmp(left
, "minor")) {
2025 if (CTF_TRACE_FIELD_IS_SET(trace
, minor
)) {
2026 fprintf(fd
, "[error] %s: minor already declared in trace declaration\n", __func__
);
2030 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &trace
->minor
);
2032 fprintf(fd
, "[error] %s: unexpected unary expression for trace minor number\n", __func__
);
2036 CTF_TRACE_SET_FIELD(trace
, minor
);
2037 } else if (!strcmp(left
, "uuid")) {
2038 unsigned char uuid
[BABELTRACE_UUID_LEN
];
2040 ret
= get_unary_uuid(&node
->u
.ctf_expression
.right
, uuid
);
2042 fprintf(fd
, "[error] %s: unexpected unary expression for trace uuid\n", __func__
);
2046 if (CTF_TRACE_FIELD_IS_SET(trace
, uuid
)
2047 && babeltrace_uuid_compare(uuid
, trace
->uuid
)) {
2048 fprintf(fd
, "[error] %s: uuid mismatch\n", __func__
);
2052 memcpy(trace
->uuid
, uuid
, sizeof(uuid
));
2054 CTF_TRACE_SET_FIELD(trace
, uuid
);
2055 } else if (!strcmp(left
, "byte_order")) {
2056 struct ctf_node
*right
;
2059 right
= _bt_list_first_entry(&node
->u
.ctf_expression
.right
, struct ctf_node
, siblings
);
2060 byte_order
= get_trace_byte_order(fd
, depth
, right
);
2064 if (CTF_TRACE_FIELD_IS_SET(trace
, byte_order
)
2065 && byte_order
!= trace
->byte_order
) {
2066 fprintf(fd
, "[error] %s: endianness mismatch\n", __func__
);
2070 if (byte_order
!= trace
->byte_order
) {
2071 trace
->byte_order
= byte_order
;
2073 * We need to restart
2074 * construction of the
2075 * intermediate representation.
2077 trace
->field_mask
= 0;
2078 CTF_TRACE_SET_FIELD(trace
, byte_order
);
2083 CTF_TRACE_SET_FIELD(trace
, byte_order
);
2084 } else if (!strcmp(left
, "packet.header")) {
2085 struct declaration
*declaration
;
2087 if (trace
->packet_header_decl
) {
2088 fprintf(fd
, "[error] %s: packet.header already declared in trace declaration\n", __func__
);
2092 declaration
= ctf_type_specifier_list_visit(fd
, depth
,
2093 _bt_list_first_entry(&node
->u
.ctf_expression
.right
,
2094 struct ctf_node
, siblings
),
2095 trace
->declaration_scope
, trace
);
2100 if (declaration
->id
!= CTF_TYPE_STRUCT
) {
2104 trace
->packet_header_decl
= container_of(declaration
, struct declaration_struct
, p
);
2106 fprintf(fd
, "[warning] %s: attribute \"%s\" is unknown in trace declaration.\n", __func__
, left
);
2115 /* TODO: declaration specifier should be added. */
2122 int ctf_trace_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_trace
*trace
)
2125 struct ctf_node
*iter
;
2127 if (trace
->declaration_scope
)
2129 trace
->declaration_scope
= new_declaration_scope(trace
->root_declaration_scope
);
2130 trace
->streams
= g_ptr_array_new();
2131 bt_list_for_each_entry(iter
, &node
->u
.trace
.declaration_list
, siblings
) {
2132 ret
= ctf_trace_declaration_visit(fd
, depth
+ 1, iter
, trace
);
2136 if (!CTF_TRACE_FIELD_IS_SET(trace
, major
)) {
2138 fprintf(fd
, "[error] %s: missing major field in trace declaration\n", __func__
);
2141 if (!CTF_TRACE_FIELD_IS_SET(trace
, minor
)) {
2143 fprintf(fd
, "[error] %s: missing minor field in trace declaration\n", __func__
);
2146 if (!CTF_TRACE_FIELD_IS_SET(trace
, byte_order
)) {
2148 fprintf(fd
, "[error] %s: missing byte_order field in trace declaration\n", __func__
);
2152 if (!CTF_TRACE_FIELD_IS_SET(trace
, byte_order
)) {
2153 /* check that the packet header contains a "magic" field */
2154 if (!trace
->packet_header_decl
2155 || struct_declaration_lookup_field_index(trace
->packet_header_decl
, g_quark_from_static_string("magic")) < 0) {
2157 fprintf(fd
, "[error] %s: missing both byte_order and packet header magic number in trace declaration\n", __func__
);
2164 if (trace
->packet_header_decl
) {
2165 declaration_unref(&trace
->packet_header_decl
->p
);
2166 trace
->packet_header_decl
= NULL
;
2168 g_ptr_array_free(trace
->streams
, TRUE
);
2169 free_declaration_scope(trace
->declaration_scope
);
2170 trace
->declaration_scope
= NULL
;
2175 int ctf_clock_declaration_visit(FILE *fd
, int depth
, struct ctf_node
*node
,
2176 struct ctf_clock
*clock
, struct ctf_trace
*trace
)
2180 switch (node
->type
) {
2181 case NODE_CTF_EXPRESSION
:
2185 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
2186 if (!strcmp(left
, "name")) {
2189 if (CTF_CLOCK_FIELD_IS_SET(clock
, name
)) {
2190 fprintf(fd
, "[error] %s: name already declared in clock declaration\n", __func__
);
2194 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
2196 fprintf(fd
, "[error] %s: unexpected unary expression for clock name\n", __func__
);
2200 clock
->name
= g_quark_from_string(right
);
2202 CTF_CLOCK_SET_FIELD(clock
, name
);
2203 } else if (!strcmp(left
, "uuid")) {
2207 fprintf(fd
, "[error] %s: uuid already declared in clock declaration\n", __func__
);
2211 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
2213 fprintf(fd
, "[error] %s: unexpected unary expression for clock uuid\n", __func__
);
2217 clock
->uuid
= g_quark_from_string(right
);
2219 } else if (!strcmp(left
, "description")) {
2222 if (clock
->description
) {
2223 fprintf(fd
, "[warning] %s: duplicated clock description\n", __func__
);
2224 goto error
; /* ret is 0, so not an actual error, just warn. */
2226 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
2228 fprintf(fd
, "[warning] %s: unexpected unary expression for clock description\n", __func__
);
2229 goto error
; /* ret is 0, so not an actual error, just warn. */
2231 clock
->description
= right
;
2232 } else if (!strcmp(left
, "freq")) {
2233 if (CTF_CLOCK_FIELD_IS_SET(clock
, freq
)) {
2234 fprintf(fd
, "[error] %s: freq already declared in clock declaration\n", __func__
);
2238 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &clock
->freq
);
2240 fprintf(fd
, "[error] %s: unexpected unary expression for clock freq\n", __func__
);
2244 CTF_CLOCK_SET_FIELD(clock
, freq
);
2245 } else if (!strcmp(left
, "precision")) {
2246 if (clock
->precision
) {
2247 fprintf(fd
, "[error] %s: precision already declared in clock declaration\n", __func__
);
2251 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &clock
->precision
);
2253 fprintf(fd
, "[error] %s: unexpected unary expression for clock precision\n", __func__
);
2257 } else if (!strcmp(left
, "offset_s")) {
2258 if (clock
->offset_s
) {
2259 fprintf(fd
, "[error] %s: offset_s already declared in clock declaration\n", __func__
);
2263 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &clock
->offset_s
);
2265 fprintf(fd
, "[error] %s: unexpected unary expression for clock offset_s\n", __func__
);
2269 } else if (!strcmp(left
, "offset")) {
2270 if (clock
->offset
) {
2271 fprintf(fd
, "[error] %s: offset already declared in clock declaration\n", __func__
);
2275 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &clock
->offset
);
2277 fprintf(fd
, "[error] %s: unexpected unary expression for clock offset\n", __func__
);
2281 } else if (!strcmp(left
, "absolute")) {
2282 struct ctf_node
*right
;
2284 right
= _bt_list_first_entry(&node
->u
.ctf_expression
.right
, struct ctf_node
, siblings
);
2285 ret
= get_boolean(fd
, depth
, right
);
2287 fprintf(fd
, "[error] %s: unexpected \"absolute\" right member\n", __func__
);
2291 clock
->absolute
= ret
;
2293 fprintf(fd
, "[warning] %s: attribute \"%s\" is unknown in clock declaration.\n", __func__
, left
);
2302 /* TODO: declaration specifier should be added. */
2309 int ctf_clock_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_trace
*trace
)
2312 struct ctf_node
*iter
;
2313 struct ctf_clock
*clock
;
2315 clock
= g_new0(struct ctf_clock
, 1);
2316 /* Default clock frequency is set to 1000000000 */
2317 clock
->freq
= 1000000000ULL;
2318 bt_list_for_each_entry(iter
, &node
->u
.clock
.declaration_list
, siblings
) {
2319 ret
= ctf_clock_declaration_visit(fd
, depth
+ 1, iter
, clock
, trace
);
2323 if (opt_clock_force_correlate
) {
2325 * User requested to forcibly correlate the clock
2326 * sources, even if we have no correlatation
2329 if (!clock
->absolute
) {
2330 fprintf(fd
, "[warning] Forcibly correlating trace clock sources (--clock-force-correlate).\n");
2332 clock
->absolute
= 1;
2334 if (!CTF_CLOCK_FIELD_IS_SET(clock
, name
)) {
2336 fprintf(fd
, "[error] %s: missing namefield in clock declaration\n", __func__
);
2339 if (g_hash_table_size(trace
->clocks
) > 0) {
2340 fprintf(fd
, "[error] Only CTF traces with a single clock description are supported by this babeltrace version.\n");
2344 trace
->single_clock
= clock
;
2345 g_hash_table_insert(trace
->clocks
, (gpointer
) (unsigned long) clock
->name
, clock
);
2349 g_free(clock
->description
);
2355 void ctf_clock_default(FILE *fd
, int depth
, struct ctf_trace
*trace
)
2357 struct ctf_clock
*clock
;
2359 clock
= g_new0(struct ctf_clock
, 1);
2360 clock
->name
= g_quark_from_string("monotonic");
2362 clock
->description
= g_strdup("Default clock");
2363 /* Default clock frequency is set to 1000000000 */
2364 clock
->freq
= 1000000000ULL;
2365 if (opt_clock_force_correlate
) {
2367 * User requested to forcibly correlate the clock
2368 * sources, even if we have no correlatation
2371 if (!clock
->absolute
) {
2372 fprintf(fd
, "[warning] Forcibly correlating trace clock sources (--clock-force-correlate).\n");
2374 clock
->absolute
= 1;
2376 clock
->absolute
= 0; /* Not an absolute reference across traces */
2379 trace
->single_clock
= clock
;
2380 g_hash_table_insert(trace
->clocks
, (gpointer
) (unsigned long) clock
->name
, clock
);
2384 void clock_free(gpointer data
)
2386 struct ctf_clock
*clock
= data
;
2388 g_free(clock
->description
);
2393 int ctf_env_declaration_visit(FILE *fd
, int depth
, struct ctf_node
*node
,
2394 struct ctf_trace
*trace
)
2397 struct ctf_tracer_env
*env
= &trace
->env
;
2399 switch (node
->type
) {
2400 case NODE_CTF_EXPRESSION
:
2404 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
2405 if (!strcmp(left
, "vpid")) {
2408 if (env
->vpid
!= -1) {
2409 fprintf(fd
, "[error] %s: vpid already declared in env declaration\n", __func__
);
2410 goto error
; /* ret is 0, so not an actual error, just warn. */
2412 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &v
);
2414 fprintf(fd
, "[error] %s: unexpected unary expression for env vpid\n", __func__
);
2415 goto error
; /* ret is 0, so not an actual error, just warn. */
2417 env
->vpid
= (int) v
;
2418 printf_verbose("env.vpid = %d\n", env
->vpid
);
2419 } else if (!strcmp(left
, "procname")) {
2422 if (env
->procname
[0]) {
2423 fprintf(fd
, "[warning] %s: duplicated env procname\n", __func__
);
2424 goto error
; /* ret is 0, so not an actual error, just warn. */
2426 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
2428 fprintf(fd
, "[warning] %s: unexpected unary expression for env procname\n", __func__
);
2429 goto error
; /* ret is 0, so not an actual error, just warn. */
2431 strncpy(env
->procname
, right
, TRACER_ENV_LEN
);
2432 env
->procname
[TRACER_ENV_LEN
- 1] = '\0';
2433 printf_verbose("env.procname = \"%s\"\n", env
->procname
);
2434 } else if (!strcmp(left
, "domain")) {
2437 if (env
->domain
[0]) {
2438 fprintf(fd
, "[warning] %s: duplicated env domain\n", __func__
);
2439 goto error
; /* ret is 0, so not an actual error, just warn. */
2441 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
2443 fprintf(fd
, "[warning] %s: unexpected unary expression for env domain\n", __func__
);
2444 goto error
; /* ret is 0, so not an actual error, just warn. */
2446 strncpy(env
->domain
, right
, TRACER_ENV_LEN
);
2447 env
->domain
[TRACER_ENV_LEN
- 1] = '\0';
2448 printf_verbose("env.domain = \"%s\"\n", env
->domain
);
2449 } else if (!strcmp(left
, "sysname")) {
2452 if (env
->sysname
[0]) {
2453 fprintf(fd
, "[warning] %s: duplicated env sysname\n", __func__
);
2454 goto error
; /* ret is 0, so not an actual error, just warn. */
2456 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
2458 fprintf(fd
, "[warning] %s: unexpected unary expression for env sysname\n", __func__
);
2459 goto error
; /* ret is 0, so not an actual error, just warn. */
2461 strncpy(env
->sysname
, right
, TRACER_ENV_LEN
);
2462 env
->sysname
[TRACER_ENV_LEN
- 1] = '\0';
2463 printf_verbose("env.sysname = \"%s\"\n", env
->sysname
);
2464 } else if (!strcmp(left
, "kernel_release")) {
2467 if (env
->release
[0]) {
2468 fprintf(fd
, "[warning] %s: duplicated env release\n", __func__
);
2469 goto error
; /* ret is 0, so not an actual error, just warn. */
2471 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
2473 fprintf(fd
, "[warning] %s: unexpected unary expression for env release\n", __func__
);
2474 goto error
; /* ret is 0, so not an actual error, just warn. */
2476 strncpy(env
->release
, right
, TRACER_ENV_LEN
);
2477 env
->release
[TRACER_ENV_LEN
- 1] = '\0';
2478 printf_verbose("env.release = \"%s\"\n", env
->release
);
2479 } else if (!strcmp(left
, "kernel_version")) {
2482 if (env
->version
[0]) {
2483 fprintf(fd
, "[warning] %s: duplicated env version\n", __func__
);
2484 goto error
; /* ret is 0, so not an actual error, just warn. */
2486 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
2488 fprintf(fd
, "[warning] %s: unexpected unary expression for env version\n", __func__
);
2489 goto error
; /* ret is 0, so not an actual error, just warn. */
2491 strncpy(env
->version
, right
, TRACER_ENV_LEN
);
2492 env
->version
[TRACER_ENV_LEN
- 1] = '\0';
2493 printf_verbose("env.version = \"%s\"\n", env
->version
);
2495 if (is_unary_string(&node
->u
.ctf_expression
.right
)) {
2498 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
2499 printf_verbose("env.%s = \"%s\"\n", left
, right
);
2500 } else if (is_unary_unsigned(&node
->u
.ctf_expression
.right
)) {
2504 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &v
);
2506 printf_verbose("env.%s = %" PRIu64
"\n", left
, v
);
2507 } else if (is_unary_signed(&node
->u
.ctf_expression
.right
)) {
2511 ret
= get_unary_signed(&node
->u
.ctf_expression
.right
, &v
);
2513 printf_verbose("env.%s = %" PRId64
"\n", left
, v
);
2515 printf_verbose("%s: attribute \"%s\" has unknown type.\n", __func__
, left
);
2531 int ctf_env_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_trace
*trace
)
2534 struct ctf_node
*iter
;
2536 trace
->env
.vpid
= -1;
2537 trace
->env
.procname
[0] = '\0';
2538 trace
->env
.domain
[0] = '\0';
2539 trace
->env
.sysname
[0] = '\0';
2540 trace
->env
.release
[0] = '\0';
2541 trace
->env
.version
[0] = '\0';
2542 bt_list_for_each_entry(iter
, &node
->u
.env
.declaration_list
, siblings
) {
2543 ret
= ctf_env_declaration_visit(fd
, depth
+ 1, iter
, trace
);
2552 int ctf_root_declaration_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_trace
*trace
)
2556 switch (node
->type
) {
2558 ret
= ctf_typedef_visit(fd
, depth
+ 1,
2559 trace
->root_declaration_scope
,
2560 node
->u
._typedef
.type_specifier_list
,
2561 &node
->u
._typedef
.type_declarators
,
2566 case NODE_TYPEALIAS
:
2567 ret
= ctf_typealias_visit(fd
, depth
+ 1,
2568 trace
->root_declaration_scope
,
2569 node
->u
.typealias
.target
, node
->u
.typealias
.alias
,
2574 case NODE_TYPE_SPECIFIER_LIST
:
2576 struct declaration
*declaration
;
2579 * Just add the type specifier to the root scope
2580 * declaration scope. Release local reference.
2582 declaration
= ctf_type_specifier_list_visit(fd
, depth
+ 1,
2583 node
, trace
->root_declaration_scope
, trace
);
2586 declaration_unref(declaration
);
2596 /* TODO: missing metadata "destroy" (memory leak) */
2597 int ctf_visitor_construct_metadata(FILE *fd
, int depth
, struct ctf_node
*node
,
2598 struct ctf_trace
*trace
, int byte_order
)
2601 struct ctf_node
*iter
;
2602 int env_clock_done
= 0;
2604 printf_verbose("CTF visitor: metadata construction... ");
2605 trace
->byte_order
= byte_order
;
2606 trace
->clocks
= g_hash_table_new_full(g_direct_hash
, g_direct_equal
,
2610 trace
->root_declaration_scope
= new_declaration_scope(NULL
);
2612 switch (node
->type
) {
2614 if (!env_clock_done
) {
2616 * declarations need to query clock hash table,
2617 * so clock need to be treated first.
2619 if (bt_list_empty(&node
->u
.root
.clock
)) {
2620 ctf_clock_default(fd
, depth
+ 1, trace
);
2622 bt_list_for_each_entry(iter
, &node
->u
.root
.clock
, siblings
) {
2623 ret
= ctf_clock_visit(fd
, depth
+ 1, iter
,
2626 fprintf(fd
, "[error] %s: clock declaration error\n", __func__
);
2633 bt_list_for_each_entry(iter
, &node
->u
.root
.declaration_list
,
2635 ret
= ctf_root_declaration_visit(fd
, depth
+ 1, iter
, trace
);
2637 fprintf(fd
, "[error] %s: root declaration error\n", __func__
);
2641 bt_list_for_each_entry(iter
, &node
->u
.root
.trace
, siblings
) {
2642 ret
= ctf_trace_visit(fd
, depth
+ 1, iter
, trace
);
2643 if (ret
== -EINTR
) {
2644 free_declaration_scope(trace
->root_declaration_scope
);
2646 * Need to restart creation of type
2647 * definitions, aliases and
2648 * trace header declarations.
2653 fprintf(fd
, "[error] %s: trace declaration error\n", __func__
);
2657 if (!trace
->streams
) {
2658 fprintf(fd
, "[error] %s: missing trace declaration\n", __func__
);
2662 bt_list_for_each_entry(iter
, &node
->u
.root
.env
, siblings
) {
2663 ret
= ctf_env_visit(fd
, depth
+ 1, iter
, trace
);
2665 fprintf(fd
, "[error] %s: env declaration error\n", __func__
);
2669 bt_list_for_each_entry(iter
, &node
->u
.root
.stream
, siblings
) {
2670 ret
= ctf_stream_visit(fd
, depth
+ 1, iter
,
2671 trace
->root_declaration_scope
, trace
);
2673 fprintf(fd
, "[error] %s: stream declaration error\n", __func__
);
2677 bt_list_for_each_entry(iter
, &node
->u
.root
.event
, siblings
) {
2678 ret
= ctf_event_visit(fd
, depth
+ 1, iter
,
2679 trace
->root_declaration_scope
, trace
);
2681 fprintf(fd
, "[error] %s: event declaration error\n", __func__
);
2688 fprintf(fd
, "[error] %s: unknown node type %d\n", __func__
,
2693 printf_verbose("done.\n");
2697 free_declaration_scope(trace
->root_declaration_scope
);
2698 g_hash_table_destroy(trace
->clocks
);