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 <babeltrace/ctf/events-internal.h>
34 #include "ctf-scanner.h"
35 #include "ctf-parser.h"
38 #define fprintf_dbg(fd, fmt, args...) fprintf(fd, "%s: " fmt, __func__, ## args)
40 #define _bt_list_first_entry(ptr, type, member) \
41 bt_list_entry((ptr)->next, type, member)
43 int opt_clock_force_correlate
;
46 struct declaration
*ctf_type_specifier_list_visit(FILE *fd
,
47 int depth
, struct ctf_node
*type_specifier_list
,
48 struct declaration_scope
*declaration_scope
,
49 struct ctf_trace
*trace
);
52 int ctf_stream_visit(FILE *fd
, int depth
, struct ctf_node
*node
,
53 struct declaration_scope
*parent_declaration_scope
, struct ctf_trace
*trace
);
56 int is_unary_string(struct bt_list_head
*head
)
58 struct ctf_node
*node
;
60 bt_list_for_each_entry(node
, head
, siblings
) {
61 if (node
->type
!= NODE_UNARY_EXPRESSION
)
63 if (node
->u
.unary_expression
.type
!= UNARY_STRING
)
70 * String returned must be freed by the caller using g_free.
73 char *concatenate_unary_strings(struct bt_list_head
*head
)
75 struct ctf_node
*node
;
79 str
= g_string_new("");
80 bt_list_for_each_entry(node
, head
, siblings
) {
83 assert(node
->type
== NODE_UNARY_EXPRESSION
);
84 assert(node
->u
.unary_expression
.type
== UNARY_STRING
);
85 assert((node
->u
.unary_expression
.link
== UNARY_LINK_UNKNOWN
)
87 switch (node
->u
.unary_expression
.link
) {
89 g_string_append(str
, ".");
92 g_string_append(str
, "->");
95 g_string_append(str
, "...");
100 src_string
= node
->u
.unary_expression
.u
.string
;
101 g_string_append(str
, src_string
);
104 return g_string_free(str
, FALSE
);
108 GQuark
get_map_clock_name_value(struct bt_list_head
*head
)
110 struct ctf_node
*node
;
111 const char *name
= NULL
;
114 bt_list_for_each_entry(node
, head
, siblings
) {
117 assert(node
->type
== NODE_UNARY_EXPRESSION
);
118 assert(node
->u
.unary_expression
.type
== UNARY_STRING
);
119 assert((node
->u
.unary_expression
.link
== UNARY_LINK_UNKNOWN
)
121 /* needs to be chained with . */
122 switch (node
->u
.unary_expression
.link
) {
125 case UNARY_ARROWLINK
:
126 case UNARY_DOTDOTDOT
:
131 src_string
= node
->u
.unary_expression
.u
.string
;
133 case 0: if (strcmp("clock", src_string
) != 0) {
137 case 1: name
= src_string
;
139 case 2: if (strcmp("value", src_string
) != 0) {
144 return 0; /* extra identifier, unknown */
148 return g_quark_from_string(name
);
152 int is_unary_unsigned(struct bt_list_head
*head
)
154 struct ctf_node
*node
;
156 bt_list_for_each_entry(node
, head
, siblings
) {
157 if (node
->type
!= NODE_UNARY_EXPRESSION
)
159 if (node
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
)
166 int get_unary_unsigned(struct bt_list_head
*head
, uint64_t *value
)
168 struct ctf_node
*node
;
171 bt_list_for_each_entry(node
, head
, siblings
) {
172 assert(node
->type
== NODE_UNARY_EXPRESSION
);
173 assert(node
->u
.unary_expression
.type
== UNARY_UNSIGNED_CONSTANT
);
174 assert(node
->u
.unary_expression
.link
== UNARY_LINK_UNKNOWN
);
176 *value
= node
->u
.unary_expression
.u
.unsigned_constant
;
183 int is_unary_signed(struct bt_list_head
*head
)
185 struct ctf_node
*node
;
187 bt_list_for_each_entry(node
, head
, siblings
) {
188 if (node
->type
!= NODE_UNARY_EXPRESSION
)
190 if (node
->u
.unary_expression
.type
!= UNARY_SIGNED_CONSTANT
)
197 int get_unary_signed(struct bt_list_head
*head
, int64_t *value
)
199 struct ctf_node
*node
;
202 bt_list_for_each_entry(node
, head
, siblings
) {
203 assert(node
->type
== NODE_UNARY_EXPRESSION
);
204 assert(node
->u
.unary_expression
.type
== UNARY_UNSIGNED_CONSTANT
205 || node
->u
.unary_expression
.type
== UNARY_SIGNED_CONSTANT
);
206 assert(node
->u
.unary_expression
.link
== UNARY_LINK_UNKNOWN
);
208 switch (node
->u
.unary_expression
.type
) {
209 case UNARY_UNSIGNED_CONSTANT
:
210 *value
= (int64_t) node
->u
.unary_expression
.u
.unsigned_constant
;
212 case UNARY_SIGNED_CONSTANT
:
213 *value
= node
->u
.unary_expression
.u
.signed_constant
;
224 int get_unary_uuid(struct bt_list_head
*head
, unsigned char *uuid
)
226 struct ctf_node
*node
;
230 bt_list_for_each_entry(node
, head
, siblings
) {
231 const char *src_string
;
233 assert(node
->type
== NODE_UNARY_EXPRESSION
);
234 assert(node
->u
.unary_expression
.type
== UNARY_STRING
);
235 assert(node
->u
.unary_expression
.link
== UNARY_LINK_UNKNOWN
);
237 src_string
= node
->u
.unary_expression
.u
.string
;
238 ret
= babeltrace_uuid_parse(src_string
, uuid
);
244 struct ctf_stream_declaration
*trace_stream_lookup(struct ctf_trace
*trace
, uint64_t stream_id
)
246 if (trace
->streams
->len
<= stream_id
)
248 return g_ptr_array_index(trace
->streams
, stream_id
);
252 struct ctf_clock
*trace_clock_lookup(struct ctf_trace
*trace
, GQuark clock_name
)
254 return g_hash_table_lookup(trace
->clocks
, (gpointer
) (unsigned long) clock_name
);
258 int visit_type_specifier(FILE *fd
, struct ctf_node
*type_specifier
, GString
*str
)
260 assert(type_specifier
->type
== NODE_TYPE_SPECIFIER
);
262 switch (type_specifier
->u
.type_specifier
.type
) {
264 g_string_append(str
, "void");
267 g_string_append(str
, "char");
270 g_string_append(str
, "short");
273 g_string_append(str
, "int");
276 g_string_append(str
, "long");
279 g_string_append(str
, "float");
281 case TYPESPEC_DOUBLE
:
282 g_string_append(str
, "double");
284 case TYPESPEC_SIGNED
:
285 g_string_append(str
, "signed");
287 case TYPESPEC_UNSIGNED
:
288 g_string_append(str
, "unsigned");
291 g_string_append(str
, "bool");
293 case TYPESPEC_COMPLEX
:
294 g_string_append(str
, "_Complex");
296 case TYPESPEC_IMAGINARY
:
297 g_string_append(str
, "_Imaginary");
300 g_string_append(str
, "const");
302 case TYPESPEC_ID_TYPE
:
303 if (type_specifier
->u
.type_specifier
.id_type
)
304 g_string_append(str
, type_specifier
->u
.type_specifier
.id_type
);
306 case TYPESPEC_STRUCT
:
308 struct ctf_node
*node
= type_specifier
->u
.type_specifier
.node
;
310 if (!node
->u
._struct
.name
) {
311 fprintf(fd
, "[error] %s: unexpected empty variant name\n", __func__
);
314 g_string_append(str
, "struct ");
315 g_string_append(str
, node
->u
._struct
.name
);
318 case TYPESPEC_VARIANT
:
320 struct ctf_node
*node
= type_specifier
->u
.type_specifier
.node
;
322 if (!node
->u
.variant
.name
) {
323 fprintf(fd
, "[error] %s: unexpected empty variant name\n", __func__
);
326 g_string_append(str
, "variant ");
327 g_string_append(str
, node
->u
.variant
.name
);
332 struct ctf_node
*node
= type_specifier
->u
.type_specifier
.node
;
334 if (!node
->u
._enum
.enum_id
) {
335 fprintf(fd
, "[error] %s: unexpected empty enum ID\n", __func__
);
338 g_string_append(str
, "enum ");
339 g_string_append(str
, node
->u
._enum
.enum_id
);
342 case TYPESPEC_FLOATING_POINT
:
343 case TYPESPEC_INTEGER
:
344 case TYPESPEC_STRING
:
346 fprintf(fd
, "[error] %s: unknown specifier\n", __func__
);
353 int visit_type_specifier_list(FILE *fd
, struct ctf_node
*type_specifier_list
, GString
*str
)
355 struct ctf_node
*iter
;
356 int alias_item_nr
= 0;
359 bt_list_for_each_entry(iter
, &type_specifier_list
->u
.type_specifier_list
.head
, siblings
) {
360 if (alias_item_nr
!= 0)
361 g_string_append(str
, " ");
363 ret
= visit_type_specifier(fd
, iter
, str
);
371 GQuark
create_typealias_identifier(FILE *fd
, int depth
,
372 struct ctf_node
*type_specifier_list
,
373 struct ctf_node
*node_type_declarator
)
375 struct ctf_node
*iter
;
381 str
= g_string_new("");
382 ret
= visit_type_specifier_list(fd
, type_specifier_list
, str
);
384 g_string_free(str
, TRUE
);
387 bt_list_for_each_entry(iter
, &node_type_declarator
->u
.type_declarator
.pointers
, siblings
) {
388 g_string_append(str
, " *");
389 if (iter
->u
.pointer
.const_qualifier
)
390 g_string_append(str
, " const");
392 str_c
= g_string_free(str
, FALSE
);
393 alias_q
= g_quark_from_string(str_c
);
399 struct declaration
*ctf_type_declarator_visit(FILE *fd
, int depth
,
400 struct ctf_node
*type_specifier_list
,
402 struct ctf_node
*node_type_declarator
,
403 struct declaration_scope
*declaration_scope
,
404 struct declaration
*nested_declaration
,
405 struct ctf_trace
*trace
)
408 * Visit type declarator by first taking care of sequence/array
409 * (recursively). Then, when we get to the identifier, take care
413 if (node_type_declarator
) {
414 assert(node_type_declarator
->u
.type_declarator
.type
!= TYPEDEC_UNKNOWN
);
416 /* TODO: gcc bitfields not supported yet. */
417 if (node_type_declarator
->u
.type_declarator
.bitfield_len
!= NULL
) {
418 fprintf(fd
, "[error] %s: gcc bitfields are not supported yet.\n", __func__
);
423 if (!nested_declaration
) {
424 if (node_type_declarator
&& !bt_list_empty(&node_type_declarator
->u
.type_declarator
.pointers
)) {
428 * If we have a pointer declarator, it _has_ to be present in
429 * the typealiases (else fail).
431 alias_q
= create_typealias_identifier(fd
, depth
,
432 type_specifier_list
, node_type_declarator
);
433 nested_declaration
= lookup_declaration(alias_q
, declaration_scope
);
434 if (!nested_declaration
) {
435 fprintf(fd
, "[error] %s: cannot find typealias \"%s\".\n", __func__
, g_quark_to_string(alias_q
));
438 if (nested_declaration
->id
== CTF_TYPE_INTEGER
) {
439 struct declaration_integer
*integer_declaration
=
440 container_of(nested_declaration
, struct declaration_integer
, p
);
441 /* For base to 16 for pointers (expected pretty-print) */
442 if (!integer_declaration
->base
) {
444 * We need to do a copy of the
445 * integer declaration to modify it. There could be other references to
448 integer_declaration
= integer_declaration_new(integer_declaration
->len
,
449 integer_declaration
->byte_order
, integer_declaration
->signedness
,
450 integer_declaration
->p
.alignment
, 16, integer_declaration
->encoding
,
451 integer_declaration
->clock
);
452 nested_declaration
= &integer_declaration
->p
;
456 nested_declaration
= ctf_type_specifier_list_visit(fd
, depth
,
457 type_specifier_list
, declaration_scope
, trace
);
461 if (!node_type_declarator
)
462 return nested_declaration
;
464 if (node_type_declarator
->u
.type_declarator
.type
== TYPEDEC_ID
) {
465 if (node_type_declarator
->u
.type_declarator
.u
.id
)
466 *field_name
= g_quark_from_string(node_type_declarator
->u
.type_declarator
.u
.id
);
469 return nested_declaration
;
471 struct declaration
*declaration
;
472 struct ctf_node
*first
;
476 if (!nested_declaration
) {
477 fprintf(fd
, "[error] %s: nested type is unknown.\n", __func__
);
481 /* create array/sequence, pass nested_declaration as child. */
482 if (bt_list_empty(&node_type_declarator
->u
.type_declarator
.u
.nested
.length
)) {
483 fprintf(fd
, "[error] %s: expecting length field reference or value.\n", __func__
);
486 first
= _bt_list_first_entry(&node_type_declarator
->u
.type_declarator
.u
.nested
.length
,
487 struct ctf_node
, siblings
);
488 assert(first
->type
== NODE_UNARY_EXPRESSION
);
490 switch (first
->u
.unary_expression
.type
) {
491 case UNARY_UNSIGNED_CONSTANT
:
493 struct declaration_array
*array_declaration
;
496 len
= first
->u
.unary_expression
.u
.unsigned_constant
;
497 array_declaration
= array_declaration_new(len
, nested_declaration
,
500 if (!array_declaration
) {
501 fprintf(fd
, "[error] %s: cannot create array declaration.\n", __func__
);
504 declaration
= &array_declaration
->p
;
509 /* Lookup unsigned integer definition, create sequence */
510 char *length_name
= concatenate_unary_strings(&node_type_declarator
->u
.type_declarator
.u
.nested
.length
);
511 struct declaration_sequence
*sequence_declaration
;
513 sequence_declaration
= sequence_declaration_new(length_name
, nested_declaration
, declaration_scope
);
514 if (!sequence_declaration
) {
515 fprintf(fd
, "[error] %s: cannot create sequence declaration.\n", __func__
);
518 declaration
= &sequence_declaration
->p
;
525 /* Pass it as content of outer container */
526 declaration
= ctf_type_declarator_visit(fd
, depth
,
527 type_specifier_list
, field_name
,
528 node_type_declarator
->u
.type_declarator
.u
.nested
.type_declarator
,
529 declaration_scope
, declaration
, trace
);
535 int ctf_struct_type_declarators_visit(FILE *fd
, int depth
,
536 struct declaration_struct
*struct_declaration
,
537 struct ctf_node
*type_specifier_list
,
538 struct bt_list_head
*type_declarators
,
539 struct declaration_scope
*declaration_scope
,
540 struct ctf_trace
*trace
)
542 struct ctf_node
*iter
;
545 bt_list_for_each_entry(iter
, type_declarators
, siblings
) {
546 struct declaration
*field_declaration
;
548 field_declaration
= ctf_type_declarator_visit(fd
, depth
,
551 struct_declaration
->scope
,
553 if (!field_declaration
) {
554 fprintf(fd
, "[error] %s: unable to find struct field declaration type\n", __func__
);
558 /* Check if field with same name already exists */
559 if (struct_declaration_lookup_field_index(struct_declaration
, field_name
) >= 0) {
560 fprintf(fd
, "[error] %s: duplicate field %s in struct\n", __func__
, g_quark_to_string(field_name
));
564 struct_declaration_add_field(struct_declaration
,
565 g_quark_to_string(field_name
),
572 int ctf_variant_type_declarators_visit(FILE *fd
, int depth
,
573 struct declaration_untagged_variant
*untagged_variant_declaration
,
574 struct ctf_node
*type_specifier_list
,
575 struct bt_list_head
*type_declarators
,
576 struct declaration_scope
*declaration_scope
,
577 struct ctf_trace
*trace
)
579 struct ctf_node
*iter
;
582 bt_list_for_each_entry(iter
, type_declarators
, siblings
) {
583 struct declaration
*field_declaration
;
585 field_declaration
= ctf_type_declarator_visit(fd
, depth
,
588 untagged_variant_declaration
->scope
,
590 if (!field_declaration
) {
591 fprintf(fd
, "[error] %s: unable to find variant field declaration type\n", __func__
);
595 if (untagged_variant_declaration_get_field_from_tag(untagged_variant_declaration
, field_name
) != NULL
) {
596 fprintf(fd
, "[error] %s: duplicate field %s in variant\n", __func__
, g_quark_to_string(field_name
));
601 untagged_variant_declaration_add_field(untagged_variant_declaration
,
602 g_quark_to_string(field_name
),
609 int ctf_typedef_visit(FILE *fd
, int depth
, struct declaration_scope
*scope
,
610 struct ctf_node
*type_specifier_list
,
611 struct bt_list_head
*type_declarators
,
612 struct ctf_trace
*trace
)
614 struct ctf_node
*iter
;
617 bt_list_for_each_entry(iter
, type_declarators
, siblings
) {
618 struct declaration
*type_declaration
;
621 type_declaration
= ctf_type_declarator_visit(fd
, depth
,
625 if (!type_declaration
) {
626 fprintf(fd
, "[error] %s: problem creating type declaration\n", __func__
);
630 * Don't allow typedef and typealias of untagged
633 if (type_declaration
->id
== CTF_TYPE_UNTAGGED_VARIANT
) {
634 fprintf(fd
, "[error] %s: typedef of untagged variant is not permitted.\n", __func__
);
635 declaration_unref(type_declaration
);
638 ret
= register_declaration(identifier
, type_declaration
, scope
);
640 type_declaration
->declaration_free(type_declaration
);
648 int ctf_typealias_visit(FILE *fd
, int depth
, struct declaration_scope
*scope
,
649 struct ctf_node
*target
, struct ctf_node
*alias
,
650 struct ctf_trace
*trace
)
652 struct declaration
*type_declaration
;
653 struct ctf_node
*node
;
658 /* See ctf_visitor_type_declarator() in the semantic validator. */
661 * Create target type declaration.
664 if (bt_list_empty(&target
->u
.typealias_target
.type_declarators
))
667 node
= _bt_list_first_entry(&target
->u
.typealias_target
.type_declarators
,
668 struct ctf_node
, siblings
);
669 type_declaration
= ctf_type_declarator_visit(fd
, depth
,
670 target
->u
.typealias_target
.type_specifier_list
,
673 if (!type_declaration
) {
674 fprintf(fd
, "[error] %s: problem creating type declaration\n", __func__
);
679 * Don't allow typedef and typealias of untagged
682 if (type_declaration
->id
== CTF_TYPE_UNTAGGED_VARIANT
) {
683 fprintf(fd
, "[error] %s: typedef of untagged variant is not permitted.\n", __func__
);
684 declaration_unref(type_declaration
);
688 * The semantic validator does not check whether the target is
689 * abstract or not (if it has an identifier). Check it here.
692 fprintf(fd
, "[error] %s: expecting empty identifier\n", __func__
);
697 * Create alias identifier.
700 node
= _bt_list_first_entry(&alias
->u
.typealias_alias
.type_declarators
,
701 struct ctf_node
, siblings
);
702 alias_q
= create_typealias_identifier(fd
, depth
,
703 alias
->u
.typealias_alias
.type_specifier_list
, node
);
704 err
= register_declaration(alias_q
, type_declaration
, scope
);
710 if (type_declaration
) {
711 type_declaration
->declaration_free(type_declaration
);
717 int ctf_struct_declaration_list_visit(FILE *fd
, int depth
,
718 struct ctf_node
*iter
, struct declaration_struct
*struct_declaration
,
719 struct ctf_trace
*trace
)
723 switch (iter
->type
) {
725 /* For each declarator, declare type and add type to struct declaration scope */
726 ret
= ctf_typedef_visit(fd
, depth
,
727 struct_declaration
->scope
,
728 iter
->u
._typedef
.type_specifier_list
,
729 &iter
->u
._typedef
.type_declarators
, trace
);
734 /* Declare type with declarator and add type to struct declaration scope */
735 ret
= ctf_typealias_visit(fd
, depth
,
736 struct_declaration
->scope
,
737 iter
->u
.typealias
.target
,
738 iter
->u
.typealias
.alias
, trace
);
742 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
743 /* Add field to structure declaration */
744 ret
= ctf_struct_type_declarators_visit(fd
, depth
,
746 iter
->u
.struct_or_variant_declaration
.type_specifier_list
,
747 &iter
->u
.struct_or_variant_declaration
.type_declarators
,
748 struct_declaration
->scope
, trace
);
753 fprintf(fd
, "[error] %s: unexpected node type %d\n", __func__
, (int) iter
->type
);
760 int ctf_variant_declaration_list_visit(FILE *fd
, int depth
,
761 struct ctf_node
*iter
,
762 struct declaration_untagged_variant
*untagged_variant_declaration
,
763 struct ctf_trace
*trace
)
767 switch (iter
->type
) {
769 /* For each declarator, declare type and add type to variant declaration scope */
770 ret
= ctf_typedef_visit(fd
, depth
,
771 untagged_variant_declaration
->scope
,
772 iter
->u
._typedef
.type_specifier_list
,
773 &iter
->u
._typedef
.type_declarators
, trace
);
778 /* Declare type with declarator and add type to variant declaration scope */
779 ret
= ctf_typealias_visit(fd
, depth
,
780 untagged_variant_declaration
->scope
,
781 iter
->u
.typealias
.target
,
782 iter
->u
.typealias
.alias
, trace
);
786 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
787 /* Add field to structure declaration */
788 ret
= ctf_variant_type_declarators_visit(fd
, depth
,
789 untagged_variant_declaration
,
790 iter
->u
.struct_or_variant_declaration
.type_specifier_list
,
791 &iter
->u
.struct_or_variant_declaration
.type_declarators
,
792 untagged_variant_declaration
->scope
, trace
);
797 fprintf(fd
, "[error] %s: unexpected node type %d\n", __func__
, (int) iter
->type
);
804 struct declaration
*ctf_declaration_struct_visit(FILE *fd
,
805 int depth
, const char *name
, struct bt_list_head
*declaration_list
,
806 int has_body
, struct bt_list_head
*min_align
,
807 struct declaration_scope
*declaration_scope
,
808 struct ctf_trace
*trace
)
810 struct declaration_struct
*struct_declaration
;
811 struct ctf_node
*iter
;
815 * For named struct (without body), lookup in
816 * declaration scope. Don't take reference on struct
817 * declaration: ref is only taken upon definition.
822 lookup_struct_declaration(g_quark_from_string(name
),
824 return &struct_declaration
->p
;
826 uint64_t min_align_value
= 0;
828 /* For unnamed struct, create type */
829 /* For named struct (with body), create type and add to declaration scope */
831 if (lookup_struct_declaration(g_quark_from_string(name
),
832 declaration_scope
)) {
834 fprintf(fd
, "[error] %s: struct %s already declared in scope\n", __func__
, name
);
838 if (!bt_list_empty(min_align
)) {
839 ret
= get_unary_unsigned(min_align
, &min_align_value
);
841 fprintf(fd
, "[error] %s: unexpected unary expression for structure \"align\" attribute\n", __func__
);
846 struct_declaration
= struct_declaration_new(declaration_scope
,
848 bt_list_for_each_entry(iter
, declaration_list
, siblings
) {
849 ret
= ctf_struct_declaration_list_visit(fd
, depth
+ 1, iter
,
850 struct_declaration
, trace
);
852 goto error_free_declaration
;
855 ret
= register_struct_declaration(g_quark_from_string(name
),
860 return &struct_declaration
->p
;
862 error_free_declaration
:
863 struct_declaration
->p
.declaration_free(&struct_declaration
->p
);
869 struct declaration
*ctf_declaration_variant_visit(FILE *fd
,
870 int depth
, const char *name
, const char *choice
,
871 struct bt_list_head
*declaration_list
,
872 int has_body
, struct declaration_scope
*declaration_scope
,
873 struct ctf_trace
*trace
)
875 struct declaration_untagged_variant
*untagged_variant_declaration
;
876 struct declaration_variant
*variant_declaration
;
877 struct ctf_node
*iter
;
881 * For named variant (without body), lookup in
882 * declaration scope. Don't take reference on variant
883 * declaration: ref is only taken upon definition.
887 untagged_variant_declaration
=
888 lookup_variant_declaration(g_quark_from_string(name
),
891 /* For unnamed variant, create type */
892 /* For named variant (with body), create type and add to declaration scope */
894 if (lookup_variant_declaration(g_quark_from_string(name
),
895 declaration_scope
)) {
897 fprintf(fd
, "[error] %s: variant %s already declared in scope\n", __func__
, name
);
901 untagged_variant_declaration
= untagged_variant_declaration_new(declaration_scope
);
902 bt_list_for_each_entry(iter
, declaration_list
, siblings
) {
903 ret
= ctf_variant_declaration_list_visit(fd
, depth
+ 1, iter
,
904 untagged_variant_declaration
, trace
);
909 ret
= register_variant_declaration(g_quark_from_string(name
),
910 untagged_variant_declaration
,
916 * if tagged, create tagged variant and return. else return
920 return &untagged_variant_declaration
->p
;
922 variant_declaration
= variant_declaration_new(untagged_variant_declaration
, choice
);
923 if (!variant_declaration
)
925 declaration_unref(&untagged_variant_declaration
->p
);
926 return &variant_declaration
->p
;
929 untagged_variant_declaration
->p
.declaration_free(&untagged_variant_declaration
->p
);
934 int ctf_enumerator_list_visit(FILE *fd
, int depth
,
935 struct ctf_node
*enumerator
,
936 struct declaration_enum
*enum_declaration
)
939 struct ctf_node
*iter
;
941 q
= g_quark_from_string(enumerator
->u
.enumerator
.id
);
942 if (enum_declaration
->integer_declaration
->signedness
) {
946 bt_list_for_each_entry(iter
, &enumerator
->u
.enumerator
.values
, siblings
) {
949 assert(iter
->type
== NODE_UNARY_EXPRESSION
);
955 switch (iter
->u
.unary_expression
.type
) {
956 case UNARY_SIGNED_CONSTANT
:
957 *target
= iter
->u
.unary_expression
.u
.signed_constant
;
959 case UNARY_UNSIGNED_CONSTANT
:
960 *target
= iter
->u
.unary_expression
.u
.unsigned_constant
;
963 fprintf(fd
, "[error] %s: invalid enumerator\n", __func__
);
967 fprintf(fd
, "[error] %s: invalid enumerator\n", __func__
);
974 enum_signed_insert(enum_declaration
, start
, end
, q
);
979 bt_list_for_each_entry(iter
, &enumerator
->u
.enumerator
.values
, siblings
) {
982 assert(iter
->type
== NODE_UNARY_EXPRESSION
);
988 switch (iter
->u
.unary_expression
.type
) {
989 case UNARY_UNSIGNED_CONSTANT
:
990 *target
= iter
->u
.unary_expression
.u
.unsigned_constant
;
992 case UNARY_SIGNED_CONSTANT
:
994 * We don't accept signed constants for enums with unsigned
997 fprintf(fd
, "[error] %s: invalid enumerator (signed constant encountered, but enum container type is unsigned)\n", __func__
);
1000 fprintf(fd
, "[error] %s: invalid enumerator\n", __func__
);
1004 fprintf(fd
, "[error] %s: invalid enumerator\n", __func__
);
1011 enum_unsigned_insert(enum_declaration
, start
, end
, q
);
1017 struct declaration
*ctf_declaration_enum_visit(FILE *fd
, int depth
,
1019 struct ctf_node
*container_type
,
1020 struct bt_list_head
*enumerator_list
,
1022 struct declaration_scope
*declaration_scope
,
1023 struct ctf_trace
*trace
)
1025 struct declaration
*declaration
;
1026 struct declaration_enum
*enum_declaration
;
1027 struct declaration_integer
*integer_declaration
;
1028 struct ctf_node
*iter
;
1033 * For named enum (without body), lookup in
1034 * declaration scope. Don't take reference on enum
1035 * declaration: ref is only taken upon definition.
1040 lookup_enum_declaration(g_quark_from_string(name
),
1042 return &enum_declaration
->p
;
1044 /* For unnamed enum, create type */
1045 /* For named enum (with body), create type and add to declaration scope */
1047 if (lookup_enum_declaration(g_quark_from_string(name
),
1048 declaration_scope
)) {
1050 fprintf(fd
, "[error] %s: enum %s already declared in scope\n", __func__
, name
);
1054 if (!container_type
) {
1055 declaration
= lookup_declaration(g_quark_from_static_string("int"),
1058 fprintf(fd
, "[error] %s: \"int\" type declaration missing for enumeration\n", __func__
);
1062 declaration
= ctf_type_declarator_visit(fd
, depth
,
1069 fprintf(fd
, "[error] %s: unable to create container type for enumeration\n", __func__
);
1072 if (declaration
->id
!= CTF_TYPE_INTEGER
) {
1073 fprintf(fd
, "[error] %s: container type for enumeration is not integer\n", __func__
);
1076 integer_declaration
= container_of(declaration
, struct declaration_integer
, p
);
1077 enum_declaration
= enum_declaration_new(integer_declaration
);
1078 declaration_unref(&integer_declaration
->p
); /* leave ref to enum */
1079 bt_list_for_each_entry(iter
, enumerator_list
, siblings
) {
1080 ret
= ctf_enumerator_list_visit(fd
, depth
+ 1, iter
, enum_declaration
);
1085 ret
= register_enum_declaration(g_quark_from_string(name
),
1090 return &enum_declaration
->p
;
1093 enum_declaration
->p
.declaration_free(&enum_declaration
->p
);
1098 struct declaration
*ctf_declaration_type_specifier_visit(FILE *fd
, int depth
,
1099 struct ctf_node
*type_specifier_list
,
1100 struct declaration_scope
*declaration_scope
)
1103 struct declaration
*declaration
;
1108 str
= g_string_new("");
1109 ret
= visit_type_specifier_list(fd
, type_specifier_list
, str
);
1112 str_c
= g_string_free(str
, FALSE
);
1113 id_q
= g_quark_from_string(str_c
);
1115 declaration
= lookup_declaration(id_q
, declaration_scope
);
1120 * Returns 0/1 boolean, or < 0 on error.
1123 int get_boolean(FILE *fd
, int depth
, struct ctf_node
*unary_expression
)
1125 if (unary_expression
->type
!= NODE_UNARY_EXPRESSION
) {
1126 fprintf(fd
, "[error] %s: expecting unary expression\n",
1130 switch (unary_expression
->u
.unary_expression
.type
) {
1131 case UNARY_UNSIGNED_CONSTANT
:
1132 if (unary_expression
->u
.unary_expression
.u
.unsigned_constant
== 0)
1136 case UNARY_SIGNED_CONSTANT
:
1137 if (unary_expression
->u
.unary_expression
.u
.signed_constant
== 0)
1142 if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "true"))
1144 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "TRUE"))
1146 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "false"))
1148 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "FALSE"))
1151 fprintf(fd
, "[error] %s: unexpected string \"%s\"\n",
1152 __func__
, unary_expression
->u
.unary_expression
.u
.string
);
1157 fprintf(fd
, "[error] %s: unexpected unary expression type\n",
1165 int get_trace_byte_order(FILE *fd
, int depth
, struct ctf_node
*unary_expression
)
1169 if (unary_expression
->u
.unary_expression
.type
!= UNARY_STRING
) {
1170 fprintf(fd
, "[error] %s: byte_order: expecting string\n",
1174 if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "be"))
1175 byte_order
= BIG_ENDIAN
;
1176 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "le"))
1177 byte_order
= LITTLE_ENDIAN
;
1179 fprintf(fd
, "[error] %s: unexpected string \"%s\". Should be \"native\", \"network\", \"be\" or \"le\".\n",
1180 __func__
, unary_expression
->u
.unary_expression
.u
.string
);
1187 int get_byte_order(FILE *fd
, int depth
, struct ctf_node
*unary_expression
,
1188 struct ctf_trace
*trace
)
1192 if (unary_expression
->u
.unary_expression
.type
!= UNARY_STRING
) {
1193 fprintf(fd
, "[error] %s: byte_order: expecting string\n",
1197 if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "native"))
1198 byte_order
= trace
->byte_order
;
1199 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "network"))
1200 byte_order
= BIG_ENDIAN
;
1201 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "be"))
1202 byte_order
= BIG_ENDIAN
;
1203 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "le"))
1204 byte_order
= LITTLE_ENDIAN
;
1206 fprintf(fd
, "[error] %s: unexpected string \"%s\". Should be \"native\", \"network\", \"be\" or \"le\".\n",
1207 __func__
, unary_expression
->u
.unary_expression
.u
.string
);
1214 struct declaration
*ctf_declaration_integer_visit(FILE *fd
, int depth
,
1215 struct bt_list_head
*expressions
,
1216 struct ctf_trace
*trace
)
1218 struct ctf_node
*expression
;
1219 uint64_t alignment
= 1, size
= 0;
1220 int byte_order
= trace
->byte_order
;
1222 int has_alignment
= 0, has_size
= 0;
1224 enum ctf_string_encoding encoding
= CTF_STRING_NONE
;
1225 struct ctf_clock
*clock
= NULL
;
1226 struct declaration_integer
*integer_declaration
;
1228 bt_list_for_each_entry(expression
, expressions
, siblings
) {
1229 struct ctf_node
*left
, *right
;
1231 left
= _bt_list_first_entry(&expression
->u
.ctf_expression
.left
, struct ctf_node
, siblings
);
1232 right
= _bt_list_first_entry(&expression
->u
.ctf_expression
.right
, struct ctf_node
, siblings
);
1233 assert(left
->u
.unary_expression
.type
== UNARY_STRING
);
1234 if (!strcmp(left
->u
.unary_expression
.u
.string
, "signed")) {
1235 signedness
= get_boolean(fd
, depth
, right
);
1238 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "byte_order")) {
1239 byte_order
= get_byte_order(fd
, depth
, right
, trace
);
1242 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "size")) {
1243 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
1244 fprintf(fd
, "[error] %s: size: expecting unsigned constant\n",
1248 size
= right
->u
.unary_expression
.u
.unsigned_constant
;
1250 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "align")) {
1251 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
1252 fprintf(fd
, "[error] %s: align: expecting unsigned constant\n",
1256 alignment
= right
->u
.unary_expression
.u
.unsigned_constant
;
1257 /* Make sure alignment is a power of two */
1258 if (alignment
== 0 || (alignment
& (alignment
- 1)) != 0) {
1259 fprintf(fd
, "[error] %s: align: expecting power of two\n",
1264 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "base")) {
1265 switch (right
->u
.unary_expression
.type
) {
1266 case UNARY_UNSIGNED_CONSTANT
:
1267 switch (right
->u
.unary_expression
.u
.unsigned_constant
) {
1272 base
= right
->u
.unary_expression
.u
.unsigned_constant
;
1275 fprintf(fd
, "[error] %s: base not supported (%" PRIu64
")\n",
1276 __func__
, right
->u
.unary_expression
.u
.unsigned_constant
);
1282 char *s_right
= concatenate_unary_strings(&expression
->u
.ctf_expression
.right
);
1284 fprintf(fd
, "[error] %s: unexpected unary expression for integer base\n", __func__
);
1288 if (!strcmp(s_right
, "decimal") || !strcmp(s_right
, "dec") || !strcmp(s_right
, "d")
1289 || !strcmp(s_right
, "i") || !strcmp(s_right
, "u")) {
1291 } else if (!strcmp(s_right
, "hexadecimal") || !strcmp(s_right
, "hex")
1292 || !strcmp(s_right
, "x") || !strcmp(s_right
, "X")
1293 || !strcmp(s_right
, "p")) {
1295 } else if (!strcmp(s_right
, "octal") || !strcmp(s_right
, "oct")
1296 || !strcmp(s_right
, "o")) {
1298 } else if (!strcmp(s_right
, "binary") || !strcmp(s_right
, "b")) {
1301 fprintf(fd
, "[error] %s: unexpected expression for integer base (%s)\n", __func__
, s_right
);
1310 fprintf(fd
, "[error] %s: base: expecting unsigned constant or unary string\n",
1314 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "encoding")) {
1317 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
1318 fprintf(fd
, "[error] %s: encoding: expecting unary string\n",
1322 s_right
= concatenate_unary_strings(&expression
->u
.ctf_expression
.right
);
1324 fprintf(fd
, "[error] %s: unexpected unary expression for integer base\n", __func__
);
1328 if (!strcmp(s_right
, "UTF8")
1329 || !strcmp(s_right
, "utf8")
1330 || !strcmp(s_right
, "utf-8")
1331 || !strcmp(s_right
, "UTF-8"))
1332 encoding
= CTF_STRING_UTF8
;
1333 else if (!strcmp(s_right
, "ASCII")
1334 || !strcmp(s_right
, "ascii"))
1335 encoding
= CTF_STRING_ASCII
;
1336 else if (!strcmp(s_right
, "none"))
1337 encoding
= CTF_STRING_NONE
;
1339 fprintf(fd
, "[error] %s: unknown string encoding \"%s\"\n", __func__
, s_right
);
1344 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "map")) {
1347 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
1348 fprintf(fd
, "[error] %s: map: expecting identifier\n",
1352 /* currently only support clock.name.value */
1353 clock_name
= get_map_clock_name_value(&expression
->u
.ctf_expression
.right
);
1357 s_right
= concatenate_unary_strings(&expression
->u
.ctf_expression
.right
);
1359 fprintf(fd
, "[error] %s: unexpected unary expression for integer map\n", __func__
);
1363 fprintf(fd
, "[warning] %s: unknown map %s in integer declaration\n", __func__
,
1368 clock
= trace_clock_lookup(trace
, clock_name
);
1370 fprintf(fd
, "[error] %s: map: unable to find clock %s declaration\n",
1371 __func__
, g_quark_to_string(clock_name
));
1375 fprintf(fd
, "[warning] %s: unknown attribute name %s\n",
1376 __func__
, left
->u
.unary_expression
.u
.string
);
1377 /* Fall-through after warning */
1381 fprintf(fd
, "[error] %s: missing size attribute\n", __func__
);
1384 if (!has_alignment
) {
1385 if (size
% CHAR_BIT
) {
1386 /* bit-packed alignment */
1389 /* byte-packed alignment */
1390 alignment
= CHAR_BIT
;
1393 integer_declaration
= integer_declaration_new(size
,
1394 byte_order
, signedness
, alignment
,
1395 base
, encoding
, clock
);
1396 return &integer_declaration
->p
;
1400 struct declaration
*ctf_declaration_floating_point_visit(FILE *fd
, int depth
,
1401 struct bt_list_head
*expressions
,
1402 struct ctf_trace
*trace
)
1404 struct ctf_node
*expression
;
1405 uint64_t alignment
= 1, exp_dig
= 0, mant_dig
= 0,
1406 byte_order
= trace
->byte_order
;
1407 int has_alignment
= 0, has_exp_dig
= 0, has_mant_dig
= 0;
1408 struct declaration_float
*float_declaration
;
1410 bt_list_for_each_entry(expression
, expressions
, siblings
) {
1411 struct ctf_node
*left
, *right
;
1413 left
= _bt_list_first_entry(&expression
->u
.ctf_expression
.left
, struct ctf_node
, siblings
);
1414 right
= _bt_list_first_entry(&expression
->u
.ctf_expression
.right
, struct ctf_node
, siblings
);
1415 assert(left
->u
.unary_expression
.type
== UNARY_STRING
);
1416 if (!strcmp(left
->u
.unary_expression
.u
.string
, "byte_order")) {
1417 byte_order
= get_byte_order(fd
, depth
, right
, trace
);
1420 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "exp_dig")) {
1421 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
1422 fprintf(fd
, "[error] %s: exp_dig: expecting unsigned constant\n",
1426 exp_dig
= right
->u
.unary_expression
.u
.unsigned_constant
;
1428 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "mant_dig")) {
1429 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
1430 fprintf(fd
, "[error] %s: mant_dig: expecting unsigned constant\n",
1434 mant_dig
= right
->u
.unary_expression
.u
.unsigned_constant
;
1436 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "align")) {
1437 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
1438 fprintf(fd
, "[error] %s: align: expecting unsigned constant\n",
1442 alignment
= right
->u
.unary_expression
.u
.unsigned_constant
;
1443 /* Make sure alignment is a power of two */
1444 if (alignment
== 0 || (alignment
& (alignment
- 1)) != 0) {
1445 fprintf(fd
, "[error] %s: align: expecting power of two\n",
1451 fprintf(fd
, "[warning] %s: unknown attribute name %s\n",
1452 __func__
, left
->u
.unary_expression
.u
.string
);
1453 /* Fall-through after warning */
1456 if (!has_mant_dig
) {
1457 fprintf(fd
, "[error] %s: missing mant_dig attribute\n", __func__
);
1461 fprintf(fd
, "[error] %s: missing exp_dig attribute\n", __func__
);
1464 if (!has_alignment
) {
1465 if ((mant_dig
+ exp_dig
) % CHAR_BIT
) {
1466 /* bit-packed alignment */
1469 /* byte-packed alignment */
1470 alignment
= CHAR_BIT
;
1473 float_declaration
= float_declaration_new(mant_dig
, exp_dig
,
1474 byte_order
, alignment
);
1475 return &float_declaration
->p
;
1479 struct declaration
*ctf_declaration_string_visit(FILE *fd
, int depth
,
1480 struct bt_list_head
*expressions
,
1481 struct ctf_trace
*trace
)
1483 struct ctf_node
*expression
;
1484 const char *encoding_c
= NULL
;
1485 enum ctf_string_encoding encoding
= CTF_STRING_UTF8
;
1486 struct declaration_string
*string_declaration
;
1488 bt_list_for_each_entry(expression
, expressions
, siblings
) {
1489 struct ctf_node
*left
, *right
;
1491 left
= _bt_list_first_entry(&expression
->u
.ctf_expression
.left
, struct ctf_node
, siblings
);
1492 right
= _bt_list_first_entry(&expression
->u
.ctf_expression
.right
, struct ctf_node
, siblings
);
1493 assert(left
->u
.unary_expression
.type
== UNARY_STRING
);
1494 if (!strcmp(left
->u
.unary_expression
.u
.string
, "encoding")) {
1495 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
1496 fprintf(fd
, "[error] %s: encoding: expecting string\n",
1500 encoding_c
= right
->u
.unary_expression
.u
.string
;
1502 fprintf(fd
, "[warning] %s: unknown attribute name %s\n",
1503 __func__
, left
->u
.unary_expression
.u
.string
);
1504 /* Fall-through after warning */
1507 if (encoding_c
&& !strcmp(encoding_c
, "ASCII"))
1508 encoding
= CTF_STRING_ASCII
;
1509 string_declaration
= string_declaration_new(encoding
);
1510 return &string_declaration
->p
;
1515 struct declaration
*ctf_type_specifier_list_visit(FILE *fd
,
1516 int depth
, struct ctf_node
*type_specifier_list
,
1517 struct declaration_scope
*declaration_scope
,
1518 struct ctf_trace
*trace
)
1520 struct ctf_node
*first
;
1521 struct ctf_node
*node
;
1523 assert(type_specifier_list
->type
== NODE_TYPE_SPECIFIER_LIST
);
1525 first
= _bt_list_first_entry(&type_specifier_list
->u
.type_specifier_list
.head
, struct ctf_node
, siblings
);
1527 assert(first
->type
== NODE_TYPE_SPECIFIER
);
1529 node
= first
->u
.type_specifier
.node
;
1531 switch (first
->u
.type_specifier
.type
) {
1532 case TYPESPEC_FLOATING_POINT
:
1533 return ctf_declaration_floating_point_visit(fd
, depth
,
1534 &node
->u
.floating_point
.expressions
, trace
);
1535 case TYPESPEC_INTEGER
:
1536 return ctf_declaration_integer_visit(fd
, depth
,
1537 &node
->u
.integer
.expressions
, trace
);
1538 case TYPESPEC_STRING
:
1539 return ctf_declaration_string_visit(fd
, depth
,
1540 &node
->u
.string
.expressions
, trace
);
1541 case TYPESPEC_STRUCT
:
1542 return ctf_declaration_struct_visit(fd
, depth
,
1543 node
->u
._struct
.name
,
1544 &node
->u
._struct
.declaration_list
,
1545 node
->u
._struct
.has_body
,
1546 &node
->u
._struct
.min_align
,
1549 case TYPESPEC_VARIANT
:
1550 return ctf_declaration_variant_visit(fd
, depth
,
1551 node
->u
.variant
.name
,
1552 node
->u
.variant
.choice
,
1553 &node
->u
.variant
.declaration_list
,
1554 node
->u
.variant
.has_body
,
1558 return ctf_declaration_enum_visit(fd
, depth
,
1559 node
->u
._enum
.enum_id
,
1560 node
->u
._enum
.container_type
,
1561 &node
->u
._enum
.enumerator_list
,
1562 node
->u
._enum
.has_body
,
1568 case TYPESPEC_SHORT
:
1571 case TYPESPEC_FLOAT
:
1572 case TYPESPEC_DOUBLE
:
1573 case TYPESPEC_SIGNED
:
1574 case TYPESPEC_UNSIGNED
:
1576 case TYPESPEC_COMPLEX
:
1577 case TYPESPEC_IMAGINARY
:
1578 case TYPESPEC_CONST
:
1579 case TYPESPEC_ID_TYPE
:
1580 return ctf_declaration_type_specifier_visit(fd
, depth
,
1581 type_specifier_list
, declaration_scope
);
1583 fprintf(fd
, "[error] %s: unexpected node type %d\n", __func__
, (int) first
->u
.type_specifier
.type
);
1589 int ctf_event_declaration_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_event_declaration
*event
, struct ctf_trace
*trace
)
1593 switch (node
->type
) {
1595 ret
= ctf_typedef_visit(fd
, depth
+ 1,
1596 event
->declaration_scope
,
1597 node
->u
._typedef
.type_specifier_list
,
1598 &node
->u
._typedef
.type_declarators
,
1603 case NODE_TYPEALIAS
:
1604 ret
= ctf_typealias_visit(fd
, depth
+ 1,
1605 event
->declaration_scope
,
1606 node
->u
.typealias
.target
, node
->u
.typealias
.alias
,
1611 case NODE_CTF_EXPRESSION
:
1615 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
1616 if (!strcmp(left
, "name")) {
1619 if (CTF_EVENT_FIELD_IS_SET(event
, name
)) {
1620 fprintf(fd
, "[error] %s: name already declared in event declaration\n", __func__
);
1624 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
1626 fprintf(fd
, "[error] %s: unexpected unary expression for event name\n", __func__
);
1630 event
->name
= g_quark_from_string(right
);
1632 CTF_EVENT_SET_FIELD(event
, name
);
1633 } else if (!strcmp(left
, "id")) {
1634 if (CTF_EVENT_FIELD_IS_SET(event
, id
)) {
1635 fprintf(fd
, "[error] %s: id already declared in event declaration\n", __func__
);
1639 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &event
->id
);
1641 fprintf(fd
, "[error] %s: unexpected unary expression for event id\n", __func__
);
1645 CTF_EVENT_SET_FIELD(event
, id
);
1646 } else if (!strcmp(left
, "stream_id")) {
1647 if (CTF_EVENT_FIELD_IS_SET(event
, stream_id
)) {
1648 fprintf(fd
, "[error] %s: stream_id already declared in event declaration\n", __func__
);
1652 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &event
->stream_id
);
1654 fprintf(fd
, "[error] %s: unexpected unary expression for event stream_id\n", __func__
);
1658 event
->stream
= trace_stream_lookup(trace
, event
->stream_id
);
1659 if (!event
->stream
) {
1660 fprintf(fd
, "[error] %s: stream id %" PRIu64
" cannot be found\n", __func__
, event
->stream_id
);
1664 CTF_EVENT_SET_FIELD(event
, stream_id
);
1665 } else if (!strcmp(left
, "context")) {
1666 struct declaration
*declaration
;
1668 if (event
->context_decl
) {
1669 fprintf(fd
, "[error] %s: context already declared in event declaration\n", __func__
);
1673 declaration
= ctf_type_specifier_list_visit(fd
, depth
,
1674 _bt_list_first_entry(&node
->u
.ctf_expression
.right
,
1675 struct ctf_node
, siblings
),
1676 event
->declaration_scope
, trace
);
1681 if (declaration
->id
!= CTF_TYPE_STRUCT
) {
1685 event
->context_decl
= container_of(declaration
, struct declaration_struct
, p
);
1686 } else if (!strcmp(left
, "fields")) {
1687 struct declaration
*declaration
;
1689 if (event
->fields_decl
) {
1690 fprintf(fd
, "[error] %s: fields already declared in event declaration\n", __func__
);
1694 declaration
= ctf_type_specifier_list_visit(fd
, depth
,
1695 _bt_list_first_entry(&node
->u
.ctf_expression
.right
,
1696 struct ctf_node
, siblings
),
1697 event
->declaration_scope
, trace
);
1702 if (declaration
->id
!= CTF_TYPE_STRUCT
) {
1706 event
->fields_decl
= container_of(declaration
, struct declaration_struct
, p
);
1707 } else if (!strcmp(left
, "loglevel")) {
1708 int64_t loglevel
= -1;
1710 if (CTF_EVENT_FIELD_IS_SET(event
, loglevel
)) {
1711 fprintf(fd
, "[error] %s: loglevel already declared in event declaration\n", __func__
);
1715 ret
= get_unary_signed(&node
->u
.ctf_expression
.right
, &loglevel
);
1717 fprintf(fd
, "[error] %s: unexpected unary expression for event loglevel\n", __func__
);
1721 event
->loglevel
= (int) loglevel
;
1722 CTF_EVENT_SET_FIELD(event
, loglevel
);
1724 fprintf(fd
, "[warning] %s: attribute \"%s\" is unknown in event declaration.\n", __func__
, left
);
1725 /* Fall-through after warning */
1733 /* TODO: declaration specifier should be added. */
1740 int ctf_event_visit(FILE *fd
, int depth
, struct ctf_node
*node
,
1741 struct declaration_scope
*parent_declaration_scope
, struct ctf_trace
*trace
)
1744 struct ctf_node
*iter
;
1745 struct ctf_event_declaration
*event
;
1746 struct bt_ctf_event_decl
*event_decl
;
1748 event_decl
= g_new0(struct bt_ctf_event_decl
, 1);
1749 event
= &event_decl
->parent
;
1750 event
->declaration_scope
= new_declaration_scope(parent_declaration_scope
);
1751 event
->loglevel
= -1;
1752 bt_list_for_each_entry(iter
, &node
->u
.event
.declaration_list
, siblings
) {
1753 ret
= ctf_event_declaration_visit(fd
, depth
+ 1, iter
, event
, trace
);
1757 if (!CTF_EVENT_FIELD_IS_SET(event
, name
)) {
1759 fprintf(fd
, "[error] %s: missing name field in event declaration\n", __func__
);
1762 if (!CTF_EVENT_FIELD_IS_SET(event
, stream_id
)) {
1763 /* Allow missing stream_id if there is only a single stream */
1764 switch (trace
->streams
->len
) {
1765 case 0: /* Create stream if there was none. */
1766 ret
= ctf_stream_visit(fd
, depth
, NULL
, trace
->root_declaration_scope
, trace
);
1771 event
->stream_id
= 0;
1772 event
->stream
= trace_stream_lookup(trace
, event
->stream_id
);
1776 fprintf(fd
, "[error] %s: missing stream_id field in event declaration\n", __func__
);
1780 /* Allow only one event without id per stream */
1781 if (!CTF_EVENT_FIELD_IS_SET(event
, id
)
1782 && event
->stream
->events_by_id
->len
!= 0) {
1784 fprintf(fd
, "[error] %s: missing id field in event declaration\n", __func__
);
1787 if (event
->stream
->events_by_id
->len
<= event
->id
)
1788 g_ptr_array_set_size(event
->stream
->events_by_id
, event
->id
+ 1);
1789 g_ptr_array_index(event
->stream
->events_by_id
, event
->id
) = event
;
1790 g_hash_table_insert(event
->stream
->event_quark_to_id
,
1791 (gpointer
) (unsigned long) event
->name
,
1793 g_ptr_array_add(trace
->event_declarations
, event_decl
);
1797 if (event
->fields_decl
)
1798 declaration_unref(&event
->fields_decl
->p
);
1799 if (event
->context_decl
)
1800 declaration_unref(&event
->context_decl
->p
);
1801 free_declaration_scope(event
->declaration_scope
);
1808 int ctf_stream_declaration_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_stream_declaration
*stream
, struct ctf_trace
*trace
)
1812 switch (node
->type
) {
1814 ret
= ctf_typedef_visit(fd
, depth
+ 1,
1815 stream
->declaration_scope
,
1816 node
->u
._typedef
.type_specifier_list
,
1817 &node
->u
._typedef
.type_declarators
,
1822 case NODE_TYPEALIAS
:
1823 ret
= ctf_typealias_visit(fd
, depth
+ 1,
1824 stream
->declaration_scope
,
1825 node
->u
.typealias
.target
, node
->u
.typealias
.alias
,
1830 case NODE_CTF_EXPRESSION
:
1834 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
1835 if (!strcmp(left
, "id")) {
1836 if (CTF_STREAM_FIELD_IS_SET(stream
, stream_id
)) {
1837 fprintf(fd
, "[error] %s: id already declared in stream declaration\n", __func__
);
1841 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &stream
->stream_id
);
1843 fprintf(fd
, "[error] %s: unexpected unary expression for stream id\n", __func__
);
1847 CTF_STREAM_SET_FIELD(stream
, stream_id
);
1848 } else if (!strcmp(left
, "event.header")) {
1849 struct declaration
*declaration
;
1851 if (stream
->event_header_decl
) {
1852 fprintf(fd
, "[error] %s: event.header already declared in stream declaration\n", __func__
);
1856 declaration
= ctf_type_specifier_list_visit(fd
, depth
,
1857 _bt_list_first_entry(&node
->u
.ctf_expression
.right
,
1858 struct ctf_node
, siblings
),
1859 stream
->declaration_scope
, trace
);
1864 if (declaration
->id
!= CTF_TYPE_STRUCT
) {
1868 stream
->event_header_decl
= container_of(declaration
, struct declaration_struct
, p
);
1869 } else if (!strcmp(left
, "event.context")) {
1870 struct declaration
*declaration
;
1872 if (stream
->event_context_decl
) {
1873 fprintf(fd
, "[error] %s: event.context already declared in stream declaration\n", __func__
);
1877 declaration
= ctf_type_specifier_list_visit(fd
, depth
,
1878 _bt_list_first_entry(&node
->u
.ctf_expression
.right
,
1879 struct ctf_node
, siblings
),
1880 stream
->declaration_scope
, trace
);
1885 if (declaration
->id
!= CTF_TYPE_STRUCT
) {
1889 stream
->event_context_decl
= container_of(declaration
, struct declaration_struct
, p
);
1890 } else if (!strcmp(left
, "packet.context")) {
1891 struct declaration
*declaration
;
1893 if (stream
->packet_context_decl
) {
1894 fprintf(fd
, "[error] %s: packet.context already declared in stream declaration\n", __func__
);
1898 declaration
= ctf_type_specifier_list_visit(fd
, depth
,
1899 _bt_list_first_entry(&node
->u
.ctf_expression
.right
,
1900 struct ctf_node
, siblings
),
1901 stream
->declaration_scope
, trace
);
1906 if (declaration
->id
!= CTF_TYPE_STRUCT
) {
1910 stream
->packet_context_decl
= container_of(declaration
, struct declaration_struct
, p
);
1912 fprintf(fd
, "[warning] %s: attribute \"%s\" is unknown in stream declaration.\n", __func__
, left
);
1913 /* Fall-through after warning */
1922 /* TODO: declaration specifier should be added. */
1929 int ctf_stream_visit(FILE *fd
, int depth
, struct ctf_node
*node
,
1930 struct declaration_scope
*parent_declaration_scope
, struct ctf_trace
*trace
)
1933 struct ctf_node
*iter
;
1934 struct ctf_stream_declaration
*stream
;
1936 stream
= g_new0(struct ctf_stream_declaration
, 1);
1937 stream
->declaration_scope
= new_declaration_scope(parent_declaration_scope
);
1938 stream
->events_by_id
= g_ptr_array_new();
1939 stream
->event_quark_to_id
= g_hash_table_new(g_direct_hash
, g_direct_equal
);
1940 stream
->streams
= g_ptr_array_new();
1942 bt_list_for_each_entry(iter
, &node
->u
.stream
.declaration_list
, siblings
) {
1943 ret
= ctf_stream_declaration_visit(fd
, depth
+ 1, iter
, stream
, trace
);
1948 if (CTF_STREAM_FIELD_IS_SET(stream
, stream_id
)) {
1949 /* check that packet header has stream_id field. */
1950 if (!trace
->packet_header_decl
1951 || struct_declaration_lookup_field_index(trace
->packet_header_decl
, g_quark_from_static_string("stream_id")) < 0) {
1953 fprintf(fd
, "[error] %s: missing stream_id field in packet header declaration, but stream_id attribute is declared for stream.\n", __func__
);
1957 /* Allow only one id-less stream */
1958 if (trace
->streams
->len
!= 0) {
1960 fprintf(fd
, "[error] %s: missing id field in stream declaration\n", __func__
);
1963 stream
->stream_id
= 0;
1965 if (trace
->streams
->len
<= stream
->stream_id
)
1966 g_ptr_array_set_size(trace
->streams
, stream
->stream_id
+ 1);
1967 g_ptr_array_index(trace
->streams
, stream
->stream_id
) = stream
;
1968 stream
->trace
= trace
;
1973 if (stream
->event_header_decl
)
1974 declaration_unref(&stream
->event_header_decl
->p
);
1975 if (stream
->event_context_decl
)
1976 declaration_unref(&stream
->event_context_decl
->p
);
1977 if (stream
->packet_context_decl
)
1978 declaration_unref(&stream
->packet_context_decl
->p
);
1979 g_ptr_array_free(stream
->streams
, TRUE
);
1980 g_ptr_array_free(stream
->events_by_id
, TRUE
);
1981 g_hash_table_destroy(stream
->event_quark_to_id
);
1982 free_declaration_scope(stream
->declaration_scope
);
1988 int ctf_trace_declaration_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_trace
*trace
)
1992 switch (node
->type
) {
1994 ret
= ctf_typedef_visit(fd
, depth
+ 1,
1995 trace
->declaration_scope
,
1996 node
->u
._typedef
.type_specifier_list
,
1997 &node
->u
._typedef
.type_declarators
,
2002 case NODE_TYPEALIAS
:
2003 ret
= ctf_typealias_visit(fd
, depth
+ 1,
2004 trace
->declaration_scope
,
2005 node
->u
.typealias
.target
, node
->u
.typealias
.alias
,
2010 case NODE_CTF_EXPRESSION
:
2014 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
2015 if (!strcmp(left
, "major")) {
2016 if (CTF_TRACE_FIELD_IS_SET(trace
, major
)) {
2017 fprintf(fd
, "[error] %s: major already declared in trace declaration\n", __func__
);
2021 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &trace
->major
);
2023 fprintf(fd
, "[error] %s: unexpected unary expression for trace major number\n", __func__
);
2027 CTF_TRACE_SET_FIELD(trace
, major
);
2028 } else if (!strcmp(left
, "minor")) {
2029 if (CTF_TRACE_FIELD_IS_SET(trace
, minor
)) {
2030 fprintf(fd
, "[error] %s: minor already declared in trace declaration\n", __func__
);
2034 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &trace
->minor
);
2036 fprintf(fd
, "[error] %s: unexpected unary expression for trace minor number\n", __func__
);
2040 CTF_TRACE_SET_FIELD(trace
, minor
);
2041 } else if (!strcmp(left
, "uuid")) {
2042 unsigned char uuid
[BABELTRACE_UUID_LEN
];
2044 ret
= get_unary_uuid(&node
->u
.ctf_expression
.right
, uuid
);
2046 fprintf(fd
, "[error] %s: unexpected unary expression for trace uuid\n", __func__
);
2050 if (CTF_TRACE_FIELD_IS_SET(trace
, uuid
)
2051 && babeltrace_uuid_compare(uuid
, trace
->uuid
)) {
2052 fprintf(fd
, "[error] %s: uuid mismatch\n", __func__
);
2056 memcpy(trace
->uuid
, uuid
, sizeof(uuid
));
2058 CTF_TRACE_SET_FIELD(trace
, uuid
);
2059 } else if (!strcmp(left
, "byte_order")) {
2060 struct ctf_node
*right
;
2063 right
= _bt_list_first_entry(&node
->u
.ctf_expression
.right
, struct ctf_node
, siblings
);
2064 byte_order
= get_trace_byte_order(fd
, depth
, right
);
2068 if (CTF_TRACE_FIELD_IS_SET(trace
, byte_order
)
2069 && byte_order
!= trace
->byte_order
) {
2070 fprintf(fd
, "[error] %s: endianness mismatch\n", __func__
);
2074 if (byte_order
!= trace
->byte_order
) {
2075 trace
->byte_order
= byte_order
;
2077 * We need to restart
2078 * construction of the
2079 * intermediate representation.
2081 trace
->field_mask
= 0;
2082 CTF_TRACE_SET_FIELD(trace
, byte_order
);
2087 CTF_TRACE_SET_FIELD(trace
, byte_order
);
2088 } else if (!strcmp(left
, "packet.header")) {
2089 struct declaration
*declaration
;
2091 if (trace
->packet_header_decl
) {
2092 fprintf(fd
, "[error] %s: packet.header already declared in trace declaration\n", __func__
);
2096 declaration
= ctf_type_specifier_list_visit(fd
, depth
,
2097 _bt_list_first_entry(&node
->u
.ctf_expression
.right
,
2098 struct ctf_node
, siblings
),
2099 trace
->declaration_scope
, trace
);
2104 if (declaration
->id
!= CTF_TYPE_STRUCT
) {
2108 trace
->packet_header_decl
= container_of(declaration
, struct declaration_struct
, p
);
2110 fprintf(fd
, "[warning] %s: attribute \"%s\" is unknown in trace declaration.\n", __func__
, left
);
2119 /* TODO: declaration specifier should be added. */
2126 int ctf_trace_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_trace
*trace
)
2129 struct ctf_node
*iter
;
2131 if (trace
->declaration_scope
)
2133 trace
->declaration_scope
= new_declaration_scope(trace
->root_declaration_scope
);
2134 trace
->streams
= g_ptr_array_new();
2135 trace
->event_declarations
= g_ptr_array_new();
2136 bt_list_for_each_entry(iter
, &node
->u
.trace
.declaration_list
, siblings
) {
2137 ret
= ctf_trace_declaration_visit(fd
, depth
+ 1, iter
, trace
);
2141 if (!CTF_TRACE_FIELD_IS_SET(trace
, major
)) {
2143 fprintf(fd
, "[error] %s: missing major field in trace declaration\n", __func__
);
2146 if (!CTF_TRACE_FIELD_IS_SET(trace
, minor
)) {
2148 fprintf(fd
, "[error] %s: missing minor field in trace declaration\n", __func__
);
2151 if (!CTF_TRACE_FIELD_IS_SET(trace
, byte_order
)) {
2153 fprintf(fd
, "[error] %s: missing byte_order field in trace declaration\n", __func__
);
2157 if (!CTF_TRACE_FIELD_IS_SET(trace
, byte_order
)) {
2158 /* check that the packet header contains a "magic" field */
2159 if (!trace
->packet_header_decl
2160 || struct_declaration_lookup_field_index(trace
->packet_header_decl
, g_quark_from_static_string("magic")) < 0) {
2162 fprintf(fd
, "[error] %s: missing both byte_order and packet header magic number in trace declaration\n", __func__
);
2169 if (trace
->packet_header_decl
) {
2170 declaration_unref(&trace
->packet_header_decl
->p
);
2171 trace
->packet_header_decl
= NULL
;
2173 g_ptr_array_free(trace
->streams
, TRUE
);
2174 g_ptr_array_free(trace
->event_declarations
, TRUE
);
2175 free_declaration_scope(trace
->declaration_scope
);
2176 trace
->declaration_scope
= NULL
;
2181 int ctf_clock_declaration_visit(FILE *fd
, int depth
, struct ctf_node
*node
,
2182 struct ctf_clock
*clock
, struct ctf_trace
*trace
)
2186 switch (node
->type
) {
2187 case NODE_CTF_EXPRESSION
:
2191 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
2192 if (!strcmp(left
, "name")) {
2195 if (CTF_CLOCK_FIELD_IS_SET(clock
, name
)) {
2196 fprintf(fd
, "[error] %s: name already declared in clock declaration\n", __func__
);
2200 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
2202 fprintf(fd
, "[error] %s: unexpected unary expression for clock name\n", __func__
);
2206 clock
->name
= g_quark_from_string(right
);
2208 CTF_CLOCK_SET_FIELD(clock
, name
);
2209 } else if (!strcmp(left
, "uuid")) {
2213 fprintf(fd
, "[error] %s: uuid already declared in clock declaration\n", __func__
);
2217 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
2219 fprintf(fd
, "[error] %s: unexpected unary expression for clock uuid\n", __func__
);
2223 clock
->uuid
= g_quark_from_string(right
);
2225 } else if (!strcmp(left
, "description")) {
2228 if (clock
->description
) {
2229 fprintf(fd
, "[warning] %s: duplicated clock description\n", __func__
);
2230 goto error
; /* ret is 0, so not an actual error, just warn. */
2232 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
2234 fprintf(fd
, "[warning] %s: unexpected unary expression for clock description\n", __func__
);
2235 goto error
; /* ret is 0, so not an actual error, just warn. */
2237 clock
->description
= right
;
2238 } else if (!strcmp(left
, "freq")) {
2239 if (CTF_CLOCK_FIELD_IS_SET(clock
, freq
)) {
2240 fprintf(fd
, "[error] %s: freq already declared in clock declaration\n", __func__
);
2244 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &clock
->freq
);
2246 fprintf(fd
, "[error] %s: unexpected unary expression for clock freq\n", __func__
);
2250 CTF_CLOCK_SET_FIELD(clock
, freq
);
2251 } else if (!strcmp(left
, "precision")) {
2252 if (clock
->precision
) {
2253 fprintf(fd
, "[error] %s: precision already declared in clock declaration\n", __func__
);
2257 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &clock
->precision
);
2259 fprintf(fd
, "[error] %s: unexpected unary expression for clock precision\n", __func__
);
2263 } else if (!strcmp(left
, "offset_s")) {
2264 if (clock
->offset_s
) {
2265 fprintf(fd
, "[error] %s: offset_s already declared in clock declaration\n", __func__
);
2269 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &clock
->offset_s
);
2271 fprintf(fd
, "[error] %s: unexpected unary expression for clock offset_s\n", __func__
);
2275 } else if (!strcmp(left
, "offset")) {
2276 if (clock
->offset
) {
2277 fprintf(fd
, "[error] %s: offset already declared in clock declaration\n", __func__
);
2281 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &clock
->offset
);
2283 fprintf(fd
, "[error] %s: unexpected unary expression for clock offset\n", __func__
);
2287 } else if (!strcmp(left
, "absolute")) {
2288 struct ctf_node
*right
;
2290 right
= _bt_list_first_entry(&node
->u
.ctf_expression
.right
, struct ctf_node
, siblings
);
2291 ret
= get_boolean(fd
, depth
, right
);
2293 fprintf(fd
, "[error] %s: unexpected \"absolute\" right member\n", __func__
);
2297 clock
->absolute
= ret
;
2299 fprintf(fd
, "[warning] %s: attribute \"%s\" is unknown in clock declaration.\n", __func__
, left
);
2308 /* TODO: declaration specifier should be added. */
2315 int ctf_clock_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_trace
*trace
)
2318 struct ctf_node
*iter
;
2319 struct ctf_clock
*clock
;
2321 clock
= g_new0(struct ctf_clock
, 1);
2322 /* Default clock frequency is set to 1000000000 */
2323 clock
->freq
= 1000000000ULL;
2324 bt_list_for_each_entry(iter
, &node
->u
.clock
.declaration_list
, siblings
) {
2325 ret
= ctf_clock_declaration_visit(fd
, depth
+ 1, iter
, clock
, trace
);
2329 if (opt_clock_force_correlate
) {
2331 * User requested to forcibly correlate the clock
2332 * sources, even if we have no correlatation
2335 if (!clock
->absolute
) {
2336 fprintf(fd
, "[warning] Forcibly correlating trace clock sources (--clock-force-correlate).\n");
2338 clock
->absolute
= 1;
2340 if (!CTF_CLOCK_FIELD_IS_SET(clock
, name
)) {
2342 fprintf(fd
, "[error] %s: missing namefield in clock declaration\n", __func__
);
2345 if (g_hash_table_size(trace
->clocks
) > 0) {
2346 fprintf(fd
, "[error] Only CTF traces with a single clock description are supported by this babeltrace version.\n");
2350 trace
->single_clock
= clock
;
2351 g_hash_table_insert(trace
->clocks
, (gpointer
) (unsigned long) clock
->name
, clock
);
2355 g_free(clock
->description
);
2361 void ctf_clock_default(FILE *fd
, int depth
, struct ctf_trace
*trace
)
2363 struct ctf_clock
*clock
;
2365 clock
= g_new0(struct ctf_clock
, 1);
2366 clock
->name
= g_quark_from_string("monotonic");
2368 clock
->description
= g_strdup("Default clock");
2369 /* Default clock frequency is set to 1000000000 */
2370 clock
->freq
= 1000000000ULL;
2371 if (opt_clock_force_correlate
) {
2373 * User requested to forcibly correlate the clock
2374 * sources, even if we have no correlatation
2377 if (!clock
->absolute
) {
2378 fprintf(fd
, "[warning] Forcibly correlating trace clock sources (--clock-force-correlate).\n");
2380 clock
->absolute
= 1;
2382 clock
->absolute
= 0; /* Not an absolute reference across traces */
2385 trace
->single_clock
= clock
;
2386 g_hash_table_insert(trace
->clocks
, (gpointer
) (unsigned long) clock
->name
, clock
);
2390 void clock_free(gpointer data
)
2392 struct ctf_clock
*clock
= data
;
2394 g_free(clock
->description
);
2399 int ctf_env_declaration_visit(FILE *fd
, int depth
, struct ctf_node
*node
,
2400 struct ctf_trace
*trace
)
2403 struct ctf_tracer_env
*env
= &trace
->env
;
2405 switch (node
->type
) {
2406 case NODE_CTF_EXPRESSION
:
2410 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
2411 if (!strcmp(left
, "vpid")) {
2414 if (env
->vpid
!= -1) {
2415 fprintf(fd
, "[error] %s: vpid already declared in env declaration\n", __func__
);
2416 goto error
; /* ret is 0, so not an actual error, just warn. */
2418 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &v
);
2420 fprintf(fd
, "[error] %s: unexpected unary expression for env vpid\n", __func__
);
2421 goto error
; /* ret is 0, so not an actual error, just warn. */
2423 env
->vpid
= (int) v
;
2424 printf_verbose("env.vpid = %d\n", env
->vpid
);
2425 } else if (!strcmp(left
, "procname")) {
2428 if (env
->procname
[0]) {
2429 fprintf(fd
, "[warning] %s: duplicated env procname\n", __func__
);
2430 goto error
; /* ret is 0, so not an actual error, just warn. */
2432 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
2434 fprintf(fd
, "[warning] %s: unexpected unary expression for env procname\n", __func__
);
2435 goto error
; /* ret is 0, so not an actual error, just warn. */
2437 strncpy(env
->procname
, right
, TRACER_ENV_LEN
);
2438 env
->procname
[TRACER_ENV_LEN
- 1] = '\0';
2439 printf_verbose("env.procname = \"%s\"\n", env
->procname
);
2440 } else if (!strcmp(left
, "domain")) {
2443 if (env
->domain
[0]) {
2444 fprintf(fd
, "[warning] %s: duplicated env domain\n", __func__
);
2445 goto error
; /* ret is 0, so not an actual error, just warn. */
2447 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
2449 fprintf(fd
, "[warning] %s: unexpected unary expression for env domain\n", __func__
);
2450 goto error
; /* ret is 0, so not an actual error, just warn. */
2452 strncpy(env
->domain
, right
, TRACER_ENV_LEN
);
2453 env
->domain
[TRACER_ENV_LEN
- 1] = '\0';
2454 printf_verbose("env.domain = \"%s\"\n", env
->domain
);
2455 } else if (!strcmp(left
, "sysname")) {
2458 if (env
->sysname
[0]) {
2459 fprintf(fd
, "[warning] %s: duplicated env sysname\n", __func__
);
2460 goto error
; /* ret is 0, so not an actual error, just warn. */
2462 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
2464 fprintf(fd
, "[warning] %s: unexpected unary expression for env sysname\n", __func__
);
2465 goto error
; /* ret is 0, so not an actual error, just warn. */
2467 strncpy(env
->sysname
, right
, TRACER_ENV_LEN
);
2468 env
->sysname
[TRACER_ENV_LEN
- 1] = '\0';
2469 printf_verbose("env.sysname = \"%s\"\n", env
->sysname
);
2470 } else if (!strcmp(left
, "kernel_release")) {
2473 if (env
->release
[0]) {
2474 fprintf(fd
, "[warning] %s: duplicated env release\n", __func__
);
2475 goto error
; /* ret is 0, so not an actual error, just warn. */
2477 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
2479 fprintf(fd
, "[warning] %s: unexpected unary expression for env release\n", __func__
);
2480 goto error
; /* ret is 0, so not an actual error, just warn. */
2482 strncpy(env
->release
, right
, TRACER_ENV_LEN
);
2483 env
->release
[TRACER_ENV_LEN
- 1] = '\0';
2484 printf_verbose("env.release = \"%s\"\n", env
->release
);
2485 } else if (!strcmp(left
, "kernel_version")) {
2488 if (env
->version
[0]) {
2489 fprintf(fd
, "[warning] %s: duplicated env version\n", __func__
);
2490 goto error
; /* ret is 0, so not an actual error, just warn. */
2492 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
2494 fprintf(fd
, "[warning] %s: unexpected unary expression for env version\n", __func__
);
2495 goto error
; /* ret is 0, so not an actual error, just warn. */
2497 strncpy(env
->version
, right
, TRACER_ENV_LEN
);
2498 env
->version
[TRACER_ENV_LEN
- 1] = '\0';
2499 printf_verbose("env.version = \"%s\"\n", env
->version
);
2501 if (is_unary_string(&node
->u
.ctf_expression
.right
)) {
2504 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
2505 printf_verbose("env.%s = \"%s\"\n", left
, right
);
2506 } else if (is_unary_unsigned(&node
->u
.ctf_expression
.right
)) {
2510 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &v
);
2512 printf_verbose("env.%s = %" PRIu64
"\n", left
, v
);
2513 } else if (is_unary_signed(&node
->u
.ctf_expression
.right
)) {
2517 ret
= get_unary_signed(&node
->u
.ctf_expression
.right
, &v
);
2519 printf_verbose("env.%s = %" PRId64
"\n", left
, v
);
2521 printf_verbose("%s: attribute \"%s\" has unknown type.\n", __func__
, left
);
2537 int ctf_env_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_trace
*trace
)
2540 struct ctf_node
*iter
;
2542 trace
->env
.vpid
= -1;
2543 trace
->env
.procname
[0] = '\0';
2544 trace
->env
.domain
[0] = '\0';
2545 trace
->env
.sysname
[0] = '\0';
2546 trace
->env
.release
[0] = '\0';
2547 trace
->env
.version
[0] = '\0';
2548 bt_list_for_each_entry(iter
, &node
->u
.env
.declaration_list
, siblings
) {
2549 ret
= ctf_env_declaration_visit(fd
, depth
+ 1, iter
, trace
);
2558 int ctf_root_declaration_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_trace
*trace
)
2562 switch (node
->type
) {
2564 ret
= ctf_typedef_visit(fd
, depth
+ 1,
2565 trace
->root_declaration_scope
,
2566 node
->u
._typedef
.type_specifier_list
,
2567 &node
->u
._typedef
.type_declarators
,
2572 case NODE_TYPEALIAS
:
2573 ret
= ctf_typealias_visit(fd
, depth
+ 1,
2574 trace
->root_declaration_scope
,
2575 node
->u
.typealias
.target
, node
->u
.typealias
.alias
,
2580 case NODE_TYPE_SPECIFIER_LIST
:
2582 struct declaration
*declaration
;
2585 * Just add the type specifier to the root scope
2586 * declaration scope. Release local reference.
2588 declaration
= ctf_type_specifier_list_visit(fd
, depth
+ 1,
2589 node
, trace
->root_declaration_scope
, trace
);
2592 declaration_unref(declaration
);
2602 /* TODO: missing metadata "destroy" (memory leak) */
2603 int ctf_visitor_construct_metadata(FILE *fd
, int depth
, struct ctf_node
*node
,
2604 struct ctf_trace
*trace
, int byte_order
)
2607 struct ctf_node
*iter
;
2608 int env_clock_done
= 0;
2610 printf_verbose("CTF visitor: metadata construction... ");
2611 trace
->byte_order
= byte_order
;
2612 trace
->clocks
= g_hash_table_new_full(g_direct_hash
, g_direct_equal
,
2616 trace
->root_declaration_scope
= new_declaration_scope(NULL
);
2618 switch (node
->type
) {
2620 if (!env_clock_done
) {
2622 * declarations need to query clock hash table,
2623 * so clock need to be treated first.
2625 if (bt_list_empty(&node
->u
.root
.clock
)) {
2626 ctf_clock_default(fd
, depth
+ 1, trace
);
2628 bt_list_for_each_entry(iter
, &node
->u
.root
.clock
, siblings
) {
2629 ret
= ctf_clock_visit(fd
, depth
+ 1, iter
,
2632 fprintf(fd
, "[error] %s: clock declaration error\n", __func__
);
2639 bt_list_for_each_entry(iter
, &node
->u
.root
.declaration_list
,
2641 ret
= ctf_root_declaration_visit(fd
, depth
+ 1, iter
, trace
);
2643 fprintf(fd
, "[error] %s: root declaration error\n", __func__
);
2647 bt_list_for_each_entry(iter
, &node
->u
.root
.trace
, siblings
) {
2648 ret
= ctf_trace_visit(fd
, depth
+ 1, iter
, trace
);
2649 if (ret
== -EINTR
) {
2650 free_declaration_scope(trace
->root_declaration_scope
);
2652 * Need to restart creation of type
2653 * definitions, aliases and
2654 * trace header declarations.
2659 fprintf(fd
, "[error] %s: trace declaration error\n", __func__
);
2663 if (!trace
->streams
) {
2664 fprintf(fd
, "[error] %s: missing trace declaration\n", __func__
);
2668 bt_list_for_each_entry(iter
, &node
->u
.root
.env
, siblings
) {
2669 ret
= ctf_env_visit(fd
, depth
+ 1, iter
, trace
);
2671 fprintf(fd
, "[error] %s: env declaration error\n", __func__
);
2675 bt_list_for_each_entry(iter
, &node
->u
.root
.stream
, siblings
) {
2676 ret
= ctf_stream_visit(fd
, depth
+ 1, iter
,
2677 trace
->root_declaration_scope
, trace
);
2679 fprintf(fd
, "[error] %s: stream declaration error\n", __func__
);
2683 bt_list_for_each_entry(iter
, &node
->u
.root
.event
, siblings
) {
2684 ret
= ctf_event_visit(fd
, depth
+ 1, iter
,
2685 trace
->root_declaration_scope
, trace
);
2687 fprintf(fd
, "[error] %s: event declaration error\n", __func__
);
2694 fprintf(fd
, "[error] %s: unknown node type %d\n", __func__
,
2699 printf_verbose("done.\n");
2703 free_declaration_scope(trace
->root_declaration_scope
);
2704 g_hash_table_destroy(trace
->clocks
);