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 struct last_enum_value
{
50 int opt_clock_force_correlate
;
53 struct declaration
*ctf_type_specifier_list_visit(FILE *fd
,
54 int depth
, struct ctf_node
*type_specifier_list
,
55 struct declaration_scope
*declaration_scope
,
56 struct ctf_trace
*trace
);
59 int ctf_stream_visit(FILE *fd
, int depth
, struct ctf_node
*node
,
60 struct declaration_scope
*parent_declaration_scope
, struct ctf_trace
*trace
);
63 int is_unary_string(struct bt_list_head
*head
)
65 struct ctf_node
*node
;
67 bt_list_for_each_entry(node
, head
, siblings
) {
68 if (node
->type
!= NODE_UNARY_EXPRESSION
)
70 if (node
->u
.unary_expression
.type
!= UNARY_STRING
)
77 * String returned must be freed by the caller using g_free.
80 char *concatenate_unary_strings(struct bt_list_head
*head
)
82 struct ctf_node
*node
;
86 str
= g_string_new("");
87 bt_list_for_each_entry(node
, head
, siblings
) {
90 assert(node
->type
== NODE_UNARY_EXPRESSION
);
91 assert(node
->u
.unary_expression
.type
== UNARY_STRING
);
92 assert((node
->u
.unary_expression
.link
== UNARY_LINK_UNKNOWN
)
94 switch (node
->u
.unary_expression
.link
) {
96 g_string_append(str
, ".");
99 g_string_append(str
, "->");
101 case UNARY_DOTDOTDOT
:
102 g_string_append(str
, "...");
107 src_string
= node
->u
.unary_expression
.u
.string
;
108 g_string_append(str
, src_string
);
111 return g_string_free(str
, FALSE
);
115 GQuark
get_map_clock_name_value(struct bt_list_head
*head
)
117 struct ctf_node
*node
;
118 const char *name
= NULL
;
121 bt_list_for_each_entry(node
, head
, siblings
) {
124 assert(node
->type
== NODE_UNARY_EXPRESSION
);
125 assert(node
->u
.unary_expression
.type
== UNARY_STRING
);
126 assert((node
->u
.unary_expression
.link
== UNARY_LINK_UNKNOWN
)
128 /* needs to be chained with . */
129 switch (node
->u
.unary_expression
.link
) {
132 case UNARY_ARROWLINK
:
133 case UNARY_DOTDOTDOT
:
138 src_string
= node
->u
.unary_expression
.u
.string
;
140 case 0: if (strcmp("clock", src_string
) != 0) {
144 case 1: name
= src_string
;
146 case 2: if (strcmp("value", src_string
) != 0) {
151 return 0; /* extra identifier, unknown */
155 return g_quark_from_string(name
);
159 int is_unary_unsigned(struct bt_list_head
*head
)
161 struct ctf_node
*node
;
163 bt_list_for_each_entry(node
, head
, siblings
) {
164 if (node
->type
!= NODE_UNARY_EXPRESSION
)
166 if (node
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
)
173 int get_unary_unsigned(struct bt_list_head
*head
, uint64_t *value
)
175 struct ctf_node
*node
;
178 bt_list_for_each_entry(node
, head
, siblings
) {
179 assert(node
->type
== NODE_UNARY_EXPRESSION
);
180 assert(node
->u
.unary_expression
.type
== UNARY_UNSIGNED_CONSTANT
);
181 assert(node
->u
.unary_expression
.link
== UNARY_LINK_UNKNOWN
);
183 *value
= node
->u
.unary_expression
.u
.unsigned_constant
;
190 int is_unary_signed(struct bt_list_head
*head
)
192 struct ctf_node
*node
;
194 bt_list_for_each_entry(node
, head
, siblings
) {
195 if (node
->type
!= NODE_UNARY_EXPRESSION
)
197 if (node
->u
.unary_expression
.type
!= UNARY_SIGNED_CONSTANT
)
204 int get_unary_signed(struct bt_list_head
*head
, int64_t *value
)
206 struct ctf_node
*node
;
209 bt_list_for_each_entry(node
, head
, siblings
) {
210 assert(node
->type
== NODE_UNARY_EXPRESSION
);
211 assert(node
->u
.unary_expression
.type
== UNARY_UNSIGNED_CONSTANT
212 || node
->u
.unary_expression
.type
== UNARY_SIGNED_CONSTANT
);
213 assert(node
->u
.unary_expression
.link
== UNARY_LINK_UNKNOWN
);
215 switch (node
->u
.unary_expression
.type
) {
216 case UNARY_UNSIGNED_CONSTANT
:
217 *value
= (int64_t) node
->u
.unary_expression
.u
.unsigned_constant
;
219 case UNARY_SIGNED_CONSTANT
:
220 *value
= node
->u
.unary_expression
.u
.signed_constant
;
231 int get_unary_uuid(struct bt_list_head
*head
, unsigned char *uuid
)
233 struct ctf_node
*node
;
237 bt_list_for_each_entry(node
, head
, siblings
) {
238 const char *src_string
;
240 assert(node
->type
== NODE_UNARY_EXPRESSION
);
241 assert(node
->u
.unary_expression
.type
== UNARY_STRING
);
242 assert(node
->u
.unary_expression
.link
== UNARY_LINK_UNKNOWN
);
244 src_string
= node
->u
.unary_expression
.u
.string
;
245 ret
= babeltrace_uuid_parse(src_string
, uuid
);
251 struct ctf_stream_declaration
*trace_stream_lookup(struct ctf_trace
*trace
, uint64_t stream_id
)
253 if (trace
->streams
->len
<= stream_id
)
255 return g_ptr_array_index(trace
->streams
, stream_id
);
259 struct ctf_clock
*trace_clock_lookup(struct ctf_trace
*trace
, GQuark clock_name
)
261 return g_hash_table_lookup(trace
->clocks
, (gpointer
) (unsigned long) clock_name
);
265 int visit_type_specifier(FILE *fd
, struct ctf_node
*type_specifier
, GString
*str
)
267 assert(type_specifier
->type
== NODE_TYPE_SPECIFIER
);
269 switch (type_specifier
->u
.type_specifier
.type
) {
271 g_string_append(str
, "void");
274 g_string_append(str
, "char");
277 g_string_append(str
, "short");
280 g_string_append(str
, "int");
283 g_string_append(str
, "long");
286 g_string_append(str
, "float");
288 case TYPESPEC_DOUBLE
:
289 g_string_append(str
, "double");
291 case TYPESPEC_SIGNED
:
292 g_string_append(str
, "signed");
294 case TYPESPEC_UNSIGNED
:
295 g_string_append(str
, "unsigned");
298 g_string_append(str
, "bool");
300 case TYPESPEC_COMPLEX
:
301 g_string_append(str
, "_Complex");
303 case TYPESPEC_IMAGINARY
:
304 g_string_append(str
, "_Imaginary");
307 g_string_append(str
, "const");
309 case TYPESPEC_ID_TYPE
:
310 if (type_specifier
->u
.type_specifier
.id_type
)
311 g_string_append(str
, type_specifier
->u
.type_specifier
.id_type
);
313 case TYPESPEC_STRUCT
:
315 struct ctf_node
*node
= type_specifier
->u
.type_specifier
.node
;
317 if (!node
->u
._struct
.name
) {
318 fprintf(fd
, "[error] %s: unexpected empty variant name\n", __func__
);
321 g_string_append(str
, "struct ");
322 g_string_append(str
, node
->u
._struct
.name
);
325 case TYPESPEC_VARIANT
:
327 struct ctf_node
*node
= type_specifier
->u
.type_specifier
.node
;
329 if (!node
->u
.variant
.name
) {
330 fprintf(fd
, "[error] %s: unexpected empty variant name\n", __func__
);
333 g_string_append(str
, "variant ");
334 g_string_append(str
, node
->u
.variant
.name
);
339 struct ctf_node
*node
= type_specifier
->u
.type_specifier
.node
;
341 if (!node
->u
._enum
.enum_id
) {
342 fprintf(fd
, "[error] %s: unexpected empty enum ID\n", __func__
);
345 g_string_append(str
, "enum ");
346 g_string_append(str
, node
->u
._enum
.enum_id
);
349 case TYPESPEC_FLOATING_POINT
:
350 case TYPESPEC_INTEGER
:
351 case TYPESPEC_STRING
:
353 fprintf(fd
, "[error] %s: unknown specifier\n", __func__
);
360 int visit_type_specifier_list(FILE *fd
, struct ctf_node
*type_specifier_list
, GString
*str
)
362 struct ctf_node
*iter
;
363 int alias_item_nr
= 0;
366 bt_list_for_each_entry(iter
, &type_specifier_list
->u
.type_specifier_list
.head
, siblings
) {
367 if (alias_item_nr
!= 0)
368 g_string_append(str
, " ");
370 ret
= visit_type_specifier(fd
, iter
, str
);
378 GQuark
create_typealias_identifier(FILE *fd
, int depth
,
379 struct ctf_node
*type_specifier_list
,
380 struct ctf_node
*node_type_declarator
)
382 struct ctf_node
*iter
;
388 str
= g_string_new("");
389 ret
= visit_type_specifier_list(fd
, type_specifier_list
, str
);
391 g_string_free(str
, TRUE
);
394 bt_list_for_each_entry(iter
, &node_type_declarator
->u
.type_declarator
.pointers
, siblings
) {
395 g_string_append(str
, " *");
396 if (iter
->u
.pointer
.const_qualifier
)
397 g_string_append(str
, " const");
399 str_c
= g_string_free(str
, FALSE
);
400 alias_q
= g_quark_from_string(str_c
);
406 struct declaration
*ctf_type_declarator_visit(FILE *fd
, int depth
,
407 struct ctf_node
*type_specifier_list
,
409 struct ctf_node
*node_type_declarator
,
410 struct declaration_scope
*declaration_scope
,
411 struct declaration
*nested_declaration
,
412 struct ctf_trace
*trace
)
415 * Visit type declarator by first taking care of sequence/array
416 * (recursively). Then, when we get to the identifier, take care
420 if (node_type_declarator
) {
421 assert(node_type_declarator
->u
.type_declarator
.type
!= TYPEDEC_UNKNOWN
);
423 /* TODO: gcc bitfields not supported yet. */
424 if (node_type_declarator
->u
.type_declarator
.bitfield_len
!= NULL
) {
425 fprintf(fd
, "[error] %s: gcc bitfields are not supported yet.\n", __func__
);
430 if (!nested_declaration
) {
431 if (node_type_declarator
&& !bt_list_empty(&node_type_declarator
->u
.type_declarator
.pointers
)) {
435 * If we have a pointer declarator, it _has_ to be present in
436 * the typealiases (else fail).
438 alias_q
= create_typealias_identifier(fd
, depth
,
439 type_specifier_list
, node_type_declarator
);
440 nested_declaration
= lookup_declaration(alias_q
, declaration_scope
);
441 if (!nested_declaration
) {
442 fprintf(fd
, "[error] %s: cannot find typealias \"%s\".\n", __func__
, g_quark_to_string(alias_q
));
445 if (nested_declaration
->id
== CTF_TYPE_INTEGER
) {
446 struct declaration_integer
*integer_declaration
=
447 container_of(nested_declaration
, struct declaration_integer
, p
);
448 /* For base to 16 for pointers (expected pretty-print) */
449 if (!integer_declaration
->base
) {
451 * We need to do a copy of the
452 * integer declaration to modify it. There could be other references to
455 integer_declaration
= integer_declaration_new(integer_declaration
->len
,
456 integer_declaration
->byte_order
, integer_declaration
->signedness
,
457 integer_declaration
->p
.alignment
, 16, integer_declaration
->encoding
,
458 integer_declaration
->clock
);
459 nested_declaration
= &integer_declaration
->p
;
463 nested_declaration
= ctf_type_specifier_list_visit(fd
, depth
,
464 type_specifier_list
, declaration_scope
, trace
);
468 if (!node_type_declarator
)
469 return nested_declaration
;
471 if (node_type_declarator
->u
.type_declarator
.type
== TYPEDEC_ID
) {
472 if (node_type_declarator
->u
.type_declarator
.u
.id
)
473 *field_name
= g_quark_from_string(node_type_declarator
->u
.type_declarator
.u
.id
);
476 return nested_declaration
;
478 struct declaration
*declaration
;
479 struct ctf_node
*first
;
483 if (!nested_declaration
) {
484 fprintf(fd
, "[error] %s: nested type is unknown.\n", __func__
);
488 /* create array/sequence, pass nested_declaration as child. */
489 if (bt_list_empty(&node_type_declarator
->u
.type_declarator
.u
.nested
.length
)) {
490 fprintf(fd
, "[error] %s: expecting length field reference or value.\n", __func__
);
493 first
= _bt_list_first_entry(&node_type_declarator
->u
.type_declarator
.u
.nested
.length
,
494 struct ctf_node
, siblings
);
495 assert(first
->type
== NODE_UNARY_EXPRESSION
);
497 switch (first
->u
.unary_expression
.type
) {
498 case UNARY_UNSIGNED_CONSTANT
:
500 struct declaration_array
*array_declaration
;
503 len
= first
->u
.unary_expression
.u
.unsigned_constant
;
504 array_declaration
= array_declaration_new(len
, nested_declaration
,
507 if (!array_declaration
) {
508 fprintf(fd
, "[error] %s: cannot create array declaration.\n", __func__
);
511 declaration_unref(nested_declaration
);
512 declaration
= &array_declaration
->p
;
517 /* Lookup unsigned integer definition, create sequence */
518 char *length_name
= concatenate_unary_strings(&node_type_declarator
->u
.type_declarator
.u
.nested
.length
);
519 struct declaration_sequence
*sequence_declaration
;
521 sequence_declaration
= sequence_declaration_new(length_name
, nested_declaration
, declaration_scope
);
522 if (!sequence_declaration
) {
523 fprintf(fd
, "[error] %s: cannot create sequence declaration.\n", __func__
);
527 declaration_unref(nested_declaration
);
528 declaration
= &sequence_declaration
->p
;
536 /* Pass it as content of outer container */
537 declaration
= ctf_type_declarator_visit(fd
, depth
,
538 type_specifier_list
, field_name
,
539 node_type_declarator
->u
.type_declarator
.u
.nested
.type_declarator
,
540 declaration_scope
, declaration
, trace
);
546 int ctf_struct_type_declarators_visit(FILE *fd
, int depth
,
547 struct declaration_struct
*struct_declaration
,
548 struct ctf_node
*type_specifier_list
,
549 struct bt_list_head
*type_declarators
,
550 struct declaration_scope
*declaration_scope
,
551 struct ctf_trace
*trace
)
553 struct ctf_node
*iter
;
556 bt_list_for_each_entry(iter
, type_declarators
, siblings
) {
557 struct declaration
*field_declaration
;
559 field_declaration
= ctf_type_declarator_visit(fd
, depth
,
562 struct_declaration
->scope
,
564 if (!field_declaration
) {
565 fprintf(fd
, "[error] %s: unable to find struct field declaration type\n", __func__
);
569 /* Check if field with same name already exists */
570 if (struct_declaration_lookup_field_index(struct_declaration
, field_name
) >= 0) {
571 fprintf(fd
, "[error] %s: duplicate field %s in struct\n", __func__
, g_quark_to_string(field_name
));
575 struct_declaration_add_field(struct_declaration
,
576 g_quark_to_string(field_name
),
578 declaration_unref(field_declaration
);
584 int ctf_variant_type_declarators_visit(FILE *fd
, int depth
,
585 struct declaration_untagged_variant
*untagged_variant_declaration
,
586 struct ctf_node
*type_specifier_list
,
587 struct bt_list_head
*type_declarators
,
588 struct declaration_scope
*declaration_scope
,
589 struct ctf_trace
*trace
)
591 struct ctf_node
*iter
;
594 bt_list_for_each_entry(iter
, type_declarators
, siblings
) {
595 struct declaration
*field_declaration
;
597 field_declaration
= ctf_type_declarator_visit(fd
, depth
,
600 untagged_variant_declaration
->scope
,
602 if (!field_declaration
) {
603 fprintf(fd
, "[error] %s: unable to find variant field declaration type\n", __func__
);
607 if (untagged_variant_declaration_get_field_from_tag(untagged_variant_declaration
, field_name
) != NULL
) {
608 fprintf(fd
, "[error] %s: duplicate field %s in variant\n", __func__
, g_quark_to_string(field_name
));
612 untagged_variant_declaration_add_field(untagged_variant_declaration
,
613 g_quark_to_string(field_name
),
615 declaration_unref(field_declaration
);
621 int ctf_typedef_visit(FILE *fd
, int depth
, struct declaration_scope
*scope
,
622 struct ctf_node
*type_specifier_list
,
623 struct bt_list_head
*type_declarators
,
624 struct ctf_trace
*trace
)
626 struct ctf_node
*iter
;
629 bt_list_for_each_entry(iter
, type_declarators
, siblings
) {
630 struct declaration
*type_declaration
;
633 type_declaration
= ctf_type_declarator_visit(fd
, depth
,
637 if (!type_declaration
) {
638 fprintf(fd
, "[error] %s: problem creating type declaration\n", __func__
);
642 * Don't allow typedef and typealias of untagged
645 if (type_declaration
->id
== CTF_TYPE_UNTAGGED_VARIANT
) {
646 fprintf(fd
, "[error] %s: typedef of untagged variant is not permitted.\n", __func__
);
647 declaration_unref(type_declaration
);
650 ret
= register_declaration(identifier
, type_declaration
, scope
);
652 type_declaration
->declaration_free(type_declaration
);
655 declaration_unref(type_declaration
);
661 int ctf_typealias_visit(FILE *fd
, int depth
, struct declaration_scope
*scope
,
662 struct ctf_node
*target
, struct ctf_node
*alias
,
663 struct ctf_trace
*trace
)
665 struct declaration
*type_declaration
;
666 struct ctf_node
*node
;
671 /* See ctf_visitor_type_declarator() in the semantic validator. */
674 * Create target type declaration.
677 if (bt_list_empty(&target
->u
.typealias_target
.type_declarators
))
680 node
= _bt_list_first_entry(&target
->u
.typealias_target
.type_declarators
,
681 struct ctf_node
, siblings
);
682 type_declaration
= ctf_type_declarator_visit(fd
, depth
,
683 target
->u
.typealias_target
.type_specifier_list
,
686 if (!type_declaration
) {
687 fprintf(fd
, "[error] %s: problem creating type declaration\n", __func__
);
692 * Don't allow typedef and typealias of untagged
695 if (type_declaration
->id
== CTF_TYPE_UNTAGGED_VARIANT
) {
696 fprintf(fd
, "[error] %s: typedef of untagged variant is not permitted.\n", __func__
);
697 declaration_unref(type_declaration
);
701 * The semantic validator does not check whether the target is
702 * abstract or not (if it has an identifier). Check it here.
705 fprintf(fd
, "[error] %s: expecting empty identifier\n", __func__
);
710 * Create alias identifier.
713 node
= _bt_list_first_entry(&alias
->u
.typealias_alias
.type_declarators
,
714 struct ctf_node
, siblings
);
715 alias_q
= create_typealias_identifier(fd
, depth
,
716 alias
->u
.typealias_alias
.type_specifier_list
, node
);
717 err
= register_declaration(alias_q
, type_declaration
, scope
);
720 declaration_unref(type_declaration
);
724 if (type_declaration
) {
725 type_declaration
->declaration_free(type_declaration
);
731 int ctf_struct_declaration_list_visit(FILE *fd
, int depth
,
732 struct ctf_node
*iter
, struct declaration_struct
*struct_declaration
,
733 struct ctf_trace
*trace
)
737 switch (iter
->type
) {
739 /* For each declarator, declare type and add type to struct declaration scope */
740 ret
= ctf_typedef_visit(fd
, depth
,
741 struct_declaration
->scope
,
742 iter
->u
._typedef
.type_specifier_list
,
743 &iter
->u
._typedef
.type_declarators
, trace
);
748 /* Declare type with declarator and add type to struct declaration scope */
749 ret
= ctf_typealias_visit(fd
, depth
,
750 struct_declaration
->scope
,
751 iter
->u
.typealias
.target
,
752 iter
->u
.typealias
.alias
, trace
);
756 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
757 /* Add field to structure declaration */
758 ret
= ctf_struct_type_declarators_visit(fd
, depth
,
760 iter
->u
.struct_or_variant_declaration
.type_specifier_list
,
761 &iter
->u
.struct_or_variant_declaration
.type_declarators
,
762 struct_declaration
->scope
, trace
);
767 fprintf(fd
, "[error] %s: unexpected node type %d\n", __func__
, (int) iter
->type
);
774 int ctf_variant_declaration_list_visit(FILE *fd
, int depth
,
775 struct ctf_node
*iter
,
776 struct declaration_untagged_variant
*untagged_variant_declaration
,
777 struct ctf_trace
*trace
)
781 switch (iter
->type
) {
783 /* For each declarator, declare type and add type to variant declaration scope */
784 ret
= ctf_typedef_visit(fd
, depth
,
785 untagged_variant_declaration
->scope
,
786 iter
->u
._typedef
.type_specifier_list
,
787 &iter
->u
._typedef
.type_declarators
, trace
);
792 /* Declare type with declarator and add type to variant declaration scope */
793 ret
= ctf_typealias_visit(fd
, depth
,
794 untagged_variant_declaration
->scope
,
795 iter
->u
.typealias
.target
,
796 iter
->u
.typealias
.alias
, trace
);
800 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
801 /* Add field to structure declaration */
802 ret
= ctf_variant_type_declarators_visit(fd
, depth
,
803 untagged_variant_declaration
,
804 iter
->u
.struct_or_variant_declaration
.type_specifier_list
,
805 &iter
->u
.struct_or_variant_declaration
.type_declarators
,
806 untagged_variant_declaration
->scope
, trace
);
811 fprintf(fd
, "[error] %s: unexpected node type %d\n", __func__
, (int) iter
->type
);
818 struct declaration
*ctf_declaration_struct_visit(FILE *fd
,
819 int depth
, const char *name
, struct bt_list_head
*declaration_list
,
820 int has_body
, struct bt_list_head
*min_align
,
821 struct declaration_scope
*declaration_scope
,
822 struct ctf_trace
*trace
)
824 struct declaration_struct
*struct_declaration
;
825 struct ctf_node
*iter
;
829 * For named struct (without body), lookup in
830 * declaration scope. Don't take reference on struct
831 * declaration: ref is only taken upon definition.
836 lookup_struct_declaration(g_quark_from_string(name
),
838 declaration_ref(&struct_declaration
->p
);
839 return &struct_declaration
->p
;
841 uint64_t min_align_value
= 0;
843 /* For unnamed struct, create type */
844 /* For named struct (with body), create type and add to declaration scope */
846 if (lookup_struct_declaration(g_quark_from_string(name
),
847 declaration_scope
)) {
849 fprintf(fd
, "[error] %s: struct %s already declared in scope\n", __func__
, name
);
853 if (!bt_list_empty(min_align
)) {
854 ret
= get_unary_unsigned(min_align
, &min_align_value
);
856 fprintf(fd
, "[error] %s: unexpected unary expression for structure \"align\" attribute\n", __func__
);
861 struct_declaration
= struct_declaration_new(declaration_scope
,
863 bt_list_for_each_entry(iter
, declaration_list
, siblings
) {
864 ret
= ctf_struct_declaration_list_visit(fd
, depth
+ 1, iter
,
865 struct_declaration
, trace
);
867 goto error_free_declaration
;
870 ret
= register_struct_declaration(g_quark_from_string(name
),
875 return &struct_declaration
->p
;
877 error_free_declaration
:
878 struct_declaration
->p
.declaration_free(&struct_declaration
->p
);
884 struct declaration
*ctf_declaration_variant_visit(FILE *fd
,
885 int depth
, const char *name
, const char *choice
,
886 struct bt_list_head
*declaration_list
,
887 int has_body
, struct declaration_scope
*declaration_scope
,
888 struct ctf_trace
*trace
)
890 struct declaration_untagged_variant
*untagged_variant_declaration
;
891 struct declaration_variant
*variant_declaration
;
892 struct ctf_node
*iter
;
895 * For named variant (without body), lookup in
896 * declaration scope. Don't take reference on variant
897 * declaration: ref is only taken upon definition.
901 untagged_variant_declaration
=
902 lookup_variant_declaration(g_quark_from_string(name
),
904 declaration_ref(&untagged_variant_declaration
->p
);
906 /* For unnamed variant, create type */
907 /* For named variant (with body), create type and add to declaration scope */
909 if (lookup_variant_declaration(g_quark_from_string(name
),
910 declaration_scope
)) {
912 fprintf(fd
, "[error] %s: variant %s already declared in scope\n", __func__
, name
);
916 untagged_variant_declaration
= untagged_variant_declaration_new(declaration_scope
);
917 bt_list_for_each_entry(iter
, declaration_list
, siblings
) {
920 ret
= ctf_variant_declaration_list_visit(fd
, depth
+ 1, iter
,
921 untagged_variant_declaration
, trace
);
928 ret
= register_variant_declaration(g_quark_from_string(name
),
929 untagged_variant_declaration
,
935 * if tagged, create tagged variant and return. else return
939 return &untagged_variant_declaration
->p
;
941 variant_declaration
= variant_declaration_new(untagged_variant_declaration
, choice
);
942 if (!variant_declaration
)
944 declaration_unref(&untagged_variant_declaration
->p
);
945 return &variant_declaration
->p
;
948 untagged_variant_declaration
->p
.declaration_free(&untagged_variant_declaration
->p
);
953 int ctf_enumerator_list_visit(FILE *fd
, int depth
,
954 struct ctf_node
*enumerator
,
955 struct declaration_enum
*enum_declaration
,
956 struct last_enum_value
*last
)
959 struct ctf_node
*iter
;
961 q
= g_quark_from_string(enumerator
->u
.enumerator
.id
);
962 if (enum_declaration
->integer_declaration
->signedness
) {
966 bt_list_for_each_entry(iter
, &enumerator
->u
.enumerator
.values
, siblings
) {
969 assert(iter
->type
== NODE_UNARY_EXPRESSION
);
975 switch (iter
->u
.unary_expression
.type
) {
976 case UNARY_SIGNED_CONSTANT
:
977 *target
= iter
->u
.unary_expression
.u
.signed_constant
;
979 case UNARY_UNSIGNED_CONSTANT
:
980 *target
= iter
->u
.unary_expression
.u
.unsigned_constant
;
983 fprintf(fd
, "[error] %s: invalid enumerator\n", __func__
);
987 fprintf(fd
, "[error] %s: invalid enumerator\n", __func__
);
997 enum_signed_insert(enum_declaration
, start
, end
, q
);
1002 bt_list_for_each_entry(iter
, &enumerator
->u
.enumerator
.values
, siblings
) {
1005 assert(iter
->type
== NODE_UNARY_EXPRESSION
);
1011 switch (iter
->u
.unary_expression
.type
) {
1012 case UNARY_UNSIGNED_CONSTANT
:
1013 *target
= iter
->u
.unary_expression
.u
.unsigned_constant
;
1015 case UNARY_SIGNED_CONSTANT
:
1017 * We don't accept signed constants for enums with unsigned
1020 fprintf(fd
, "[error] %s: invalid enumerator (signed constant encountered, but enum container type is unsigned)\n", __func__
);
1023 fprintf(fd
, "[error] %s: invalid enumerator\n", __func__
);
1027 fprintf(fd
, "[error] %s: invalid enumerator\n", __func__
);
1036 last
->u
.u
= end
+ 1;
1037 enum_unsigned_insert(enum_declaration
, start
, end
, q
);
1043 struct declaration
*ctf_declaration_enum_visit(FILE *fd
, int depth
,
1045 struct ctf_node
*container_type
,
1046 struct bt_list_head
*enumerator_list
,
1048 struct declaration_scope
*declaration_scope
,
1049 struct ctf_trace
*trace
)
1051 struct declaration
*declaration
;
1052 struct declaration_enum
*enum_declaration
;
1053 struct declaration_integer
*integer_declaration
;
1054 struct last_enum_value last_value
;
1055 struct ctf_node
*iter
;
1059 * For named enum (without body), lookup in
1060 * declaration scope. Don't take reference on enum
1061 * declaration: ref is only taken upon definition.
1066 lookup_enum_declaration(g_quark_from_string(name
),
1068 declaration_ref(&enum_declaration
->p
);
1069 return &enum_declaration
->p
;
1071 /* For unnamed enum, create type */
1072 /* For named enum (with body), create type and add to declaration scope */
1074 if (lookup_enum_declaration(g_quark_from_string(name
),
1075 declaration_scope
)) {
1077 fprintf(fd
, "[error] %s: enum %s already declared in scope\n", __func__
, name
);
1081 if (!container_type
) {
1082 declaration
= lookup_declaration(g_quark_from_static_string("int"),
1085 fprintf(fd
, "[error] %s: \"int\" type declaration missing for enumeration\n", __func__
);
1089 declaration
= ctf_type_declarator_visit(fd
, depth
,
1096 fprintf(fd
, "[error] %s: unable to create container type for enumeration\n", __func__
);
1099 if (declaration
->id
!= CTF_TYPE_INTEGER
) {
1100 fprintf(fd
, "[error] %s: container type for enumeration is not integer\n", __func__
);
1103 integer_declaration
= container_of(declaration
, struct declaration_integer
, p
);
1104 enum_declaration
= enum_declaration_new(integer_declaration
);
1105 declaration_unref(&integer_declaration
->p
); /* leave ref to enum */
1106 if (enum_declaration
->integer_declaration
->signedness
) {
1111 bt_list_for_each_entry(iter
, enumerator_list
, siblings
) {
1114 ret
= ctf_enumerator_list_visit(fd
, depth
+ 1, iter
, enum_declaration
,
1122 ret
= register_enum_declaration(g_quark_from_string(name
),
1126 declaration_unref(&enum_declaration
->p
);
1128 return &enum_declaration
->p
;
1131 enum_declaration
->p
.declaration_free(&enum_declaration
->p
);
1136 struct declaration
*ctf_declaration_type_specifier_visit(FILE *fd
, int depth
,
1137 struct ctf_node
*type_specifier_list
,
1138 struct declaration_scope
*declaration_scope
)
1141 struct declaration
*declaration
;
1146 str
= g_string_new("");
1147 ret
= visit_type_specifier_list(fd
, type_specifier_list
, str
);
1150 str_c
= g_string_free(str
, FALSE
);
1151 id_q
= g_quark_from_string(str_c
);
1153 declaration
= lookup_declaration(id_q
, declaration_scope
);
1154 declaration_ref(declaration
);
1159 * Returns 0/1 boolean, or < 0 on error.
1162 int get_boolean(FILE *fd
, int depth
, struct ctf_node
*unary_expression
)
1164 if (unary_expression
->type
!= NODE_UNARY_EXPRESSION
) {
1165 fprintf(fd
, "[error] %s: expecting unary expression\n",
1169 switch (unary_expression
->u
.unary_expression
.type
) {
1170 case UNARY_UNSIGNED_CONSTANT
:
1171 if (unary_expression
->u
.unary_expression
.u
.unsigned_constant
== 0)
1175 case UNARY_SIGNED_CONSTANT
:
1176 if (unary_expression
->u
.unary_expression
.u
.signed_constant
== 0)
1181 if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "true"))
1183 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "TRUE"))
1185 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "false"))
1187 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "FALSE"))
1190 fprintf(fd
, "[error] %s: unexpected string \"%s\"\n",
1191 __func__
, unary_expression
->u
.unary_expression
.u
.string
);
1196 fprintf(fd
, "[error] %s: unexpected unary expression type\n",
1204 int get_trace_byte_order(FILE *fd
, int depth
, struct ctf_node
*unary_expression
)
1208 if (unary_expression
->u
.unary_expression
.type
!= UNARY_STRING
) {
1209 fprintf(fd
, "[error] %s: byte_order: expecting string\n",
1213 if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "be"))
1214 byte_order
= BIG_ENDIAN
;
1215 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "le"))
1216 byte_order
= LITTLE_ENDIAN
;
1218 fprintf(fd
, "[error] %s: unexpected string \"%s\". Should be \"native\", \"network\", \"be\" or \"le\".\n",
1219 __func__
, unary_expression
->u
.unary_expression
.u
.string
);
1226 int get_byte_order(FILE *fd
, int depth
, struct ctf_node
*unary_expression
,
1227 struct ctf_trace
*trace
)
1231 if (unary_expression
->u
.unary_expression
.type
!= UNARY_STRING
) {
1232 fprintf(fd
, "[error] %s: byte_order: expecting string\n",
1236 if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "native"))
1237 byte_order
= trace
->byte_order
;
1238 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "network"))
1239 byte_order
= BIG_ENDIAN
;
1240 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "be"))
1241 byte_order
= BIG_ENDIAN
;
1242 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "le"))
1243 byte_order
= LITTLE_ENDIAN
;
1245 fprintf(fd
, "[error] %s: unexpected string \"%s\". Should be \"native\", \"network\", \"be\" or \"le\".\n",
1246 __func__
, unary_expression
->u
.unary_expression
.u
.string
);
1253 struct declaration
*ctf_declaration_integer_visit(FILE *fd
, int depth
,
1254 struct bt_list_head
*expressions
,
1255 struct ctf_trace
*trace
)
1257 struct ctf_node
*expression
;
1258 uint64_t alignment
= 1, size
= 0;
1259 int byte_order
= trace
->byte_order
;
1261 int has_alignment
= 0, has_size
= 0;
1263 enum ctf_string_encoding encoding
= CTF_STRING_NONE
;
1264 struct ctf_clock
*clock
= NULL
;
1265 struct declaration_integer
*integer_declaration
;
1267 bt_list_for_each_entry(expression
, expressions
, siblings
) {
1268 struct ctf_node
*left
, *right
;
1270 left
= _bt_list_first_entry(&expression
->u
.ctf_expression
.left
, struct ctf_node
, siblings
);
1271 right
= _bt_list_first_entry(&expression
->u
.ctf_expression
.right
, struct ctf_node
, siblings
);
1272 assert(left
->u
.unary_expression
.type
== UNARY_STRING
);
1273 if (!strcmp(left
->u
.unary_expression
.u
.string
, "signed")) {
1274 signedness
= get_boolean(fd
, depth
, right
);
1277 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "byte_order")) {
1278 byte_order
= get_byte_order(fd
, depth
, right
, trace
);
1281 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "size")) {
1282 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
1283 fprintf(fd
, "[error] %s: size: expecting unsigned constant\n",
1287 size
= right
->u
.unary_expression
.u
.unsigned_constant
;
1289 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "align")) {
1290 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
1291 fprintf(fd
, "[error] %s: align: expecting unsigned constant\n",
1295 alignment
= right
->u
.unary_expression
.u
.unsigned_constant
;
1296 /* Make sure alignment is a power of two */
1297 if (alignment
== 0 || (alignment
& (alignment
- 1)) != 0) {
1298 fprintf(fd
, "[error] %s: align: expecting power of two\n",
1303 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "base")) {
1304 switch (right
->u
.unary_expression
.type
) {
1305 case UNARY_UNSIGNED_CONSTANT
:
1306 switch (right
->u
.unary_expression
.u
.unsigned_constant
) {
1311 base
= right
->u
.unary_expression
.u
.unsigned_constant
;
1314 fprintf(fd
, "[error] %s: base not supported (%" PRIu64
")\n",
1315 __func__
, right
->u
.unary_expression
.u
.unsigned_constant
);
1321 char *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
, "decimal") || !strcmp(s_right
, "dec") || !strcmp(s_right
, "d")
1328 || !strcmp(s_right
, "i") || !strcmp(s_right
, "u")) {
1330 } else if (!strcmp(s_right
, "hexadecimal") || !strcmp(s_right
, "hex")
1331 || !strcmp(s_right
, "x") || !strcmp(s_right
, "X")
1332 || !strcmp(s_right
, "p")) {
1334 } else if (!strcmp(s_right
, "octal") || !strcmp(s_right
, "oct")
1335 || !strcmp(s_right
, "o")) {
1337 } else if (!strcmp(s_right
, "binary") || !strcmp(s_right
, "b")) {
1340 fprintf(fd
, "[error] %s: unexpected expression for integer base (%s)\n", __func__
, s_right
);
1349 fprintf(fd
, "[error] %s: base: expecting unsigned constant or unary string\n",
1353 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "encoding")) {
1356 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
1357 fprintf(fd
, "[error] %s: encoding: expecting unary string\n",
1361 s_right
= concatenate_unary_strings(&expression
->u
.ctf_expression
.right
);
1363 fprintf(fd
, "[error] %s: unexpected unary expression for integer base\n", __func__
);
1367 if (!strcmp(s_right
, "UTF8")
1368 || !strcmp(s_right
, "utf8")
1369 || !strcmp(s_right
, "utf-8")
1370 || !strcmp(s_right
, "UTF-8"))
1371 encoding
= CTF_STRING_UTF8
;
1372 else if (!strcmp(s_right
, "ASCII")
1373 || !strcmp(s_right
, "ascii"))
1374 encoding
= CTF_STRING_ASCII
;
1375 else if (!strcmp(s_right
, "none"))
1376 encoding
= CTF_STRING_NONE
;
1378 fprintf(fd
, "[error] %s: unknown string encoding \"%s\"\n", __func__
, s_right
);
1383 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "map")) {
1386 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
1387 fprintf(fd
, "[error] %s: map: expecting identifier\n",
1391 /* currently only support clock.name.value */
1392 clock_name
= get_map_clock_name_value(&expression
->u
.ctf_expression
.right
);
1396 s_right
= concatenate_unary_strings(&expression
->u
.ctf_expression
.right
);
1398 fprintf(fd
, "[error] %s: unexpected unary expression for integer map\n", __func__
);
1402 fprintf(fd
, "[warning] %s: unknown map %s in integer declaration\n", __func__
,
1407 clock
= trace_clock_lookup(trace
, clock_name
);
1409 fprintf(fd
, "[error] %s: map: unable to find clock %s declaration\n",
1410 __func__
, g_quark_to_string(clock_name
));
1414 fprintf(fd
, "[warning] %s: unknown attribute name %s\n",
1415 __func__
, left
->u
.unary_expression
.u
.string
);
1416 /* Fall-through after warning */
1420 fprintf(fd
, "[error] %s: missing size attribute\n", __func__
);
1423 if (!has_alignment
) {
1424 if (size
% CHAR_BIT
) {
1425 /* bit-packed alignment */
1428 /* byte-packed alignment */
1429 alignment
= CHAR_BIT
;
1432 integer_declaration
= integer_declaration_new(size
,
1433 byte_order
, signedness
, alignment
,
1434 base
, encoding
, clock
);
1435 return &integer_declaration
->p
;
1439 struct declaration
*ctf_declaration_floating_point_visit(FILE *fd
, int depth
,
1440 struct bt_list_head
*expressions
,
1441 struct ctf_trace
*trace
)
1443 struct ctf_node
*expression
;
1444 uint64_t alignment
= 1, exp_dig
= 0, mant_dig
= 0,
1445 byte_order
= trace
->byte_order
;
1446 int has_alignment
= 0, has_exp_dig
= 0, has_mant_dig
= 0;
1447 struct declaration_float
*float_declaration
;
1449 bt_list_for_each_entry(expression
, expressions
, siblings
) {
1450 struct ctf_node
*left
, *right
;
1452 left
= _bt_list_first_entry(&expression
->u
.ctf_expression
.left
, struct ctf_node
, siblings
);
1453 right
= _bt_list_first_entry(&expression
->u
.ctf_expression
.right
, struct ctf_node
, siblings
);
1454 assert(left
->u
.unary_expression
.type
== UNARY_STRING
);
1455 if (!strcmp(left
->u
.unary_expression
.u
.string
, "byte_order")) {
1456 byte_order
= get_byte_order(fd
, depth
, right
, trace
);
1459 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "exp_dig")) {
1460 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
1461 fprintf(fd
, "[error] %s: exp_dig: expecting unsigned constant\n",
1465 exp_dig
= right
->u
.unary_expression
.u
.unsigned_constant
;
1467 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "mant_dig")) {
1468 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
1469 fprintf(fd
, "[error] %s: mant_dig: expecting unsigned constant\n",
1473 mant_dig
= right
->u
.unary_expression
.u
.unsigned_constant
;
1475 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "align")) {
1476 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
1477 fprintf(fd
, "[error] %s: align: expecting unsigned constant\n",
1481 alignment
= right
->u
.unary_expression
.u
.unsigned_constant
;
1482 /* Make sure alignment is a power of two */
1483 if (alignment
== 0 || (alignment
& (alignment
- 1)) != 0) {
1484 fprintf(fd
, "[error] %s: align: expecting power of two\n",
1490 fprintf(fd
, "[warning] %s: unknown attribute name %s\n",
1491 __func__
, left
->u
.unary_expression
.u
.string
);
1492 /* Fall-through after warning */
1495 if (!has_mant_dig
) {
1496 fprintf(fd
, "[error] %s: missing mant_dig attribute\n", __func__
);
1500 fprintf(fd
, "[error] %s: missing exp_dig attribute\n", __func__
);
1503 if (!has_alignment
) {
1504 if ((mant_dig
+ exp_dig
) % CHAR_BIT
) {
1505 /* bit-packed alignment */
1508 /* byte-packed alignment */
1509 alignment
= CHAR_BIT
;
1512 float_declaration
= float_declaration_new(mant_dig
, exp_dig
,
1513 byte_order
, alignment
);
1514 return &float_declaration
->p
;
1518 struct declaration
*ctf_declaration_string_visit(FILE *fd
, int depth
,
1519 struct bt_list_head
*expressions
,
1520 struct ctf_trace
*trace
)
1522 struct ctf_node
*expression
;
1523 const char *encoding_c
= NULL
;
1524 enum ctf_string_encoding encoding
= CTF_STRING_UTF8
;
1525 struct declaration_string
*string_declaration
;
1527 bt_list_for_each_entry(expression
, expressions
, siblings
) {
1528 struct ctf_node
*left
, *right
;
1530 left
= _bt_list_first_entry(&expression
->u
.ctf_expression
.left
, struct ctf_node
, siblings
);
1531 right
= _bt_list_first_entry(&expression
->u
.ctf_expression
.right
, struct ctf_node
, siblings
);
1532 assert(left
->u
.unary_expression
.type
== UNARY_STRING
);
1533 if (!strcmp(left
->u
.unary_expression
.u
.string
, "encoding")) {
1534 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
1535 fprintf(fd
, "[error] %s: encoding: expecting string\n",
1539 encoding_c
= right
->u
.unary_expression
.u
.string
;
1541 fprintf(fd
, "[warning] %s: unknown attribute name %s\n",
1542 __func__
, left
->u
.unary_expression
.u
.string
);
1543 /* Fall-through after warning */
1546 if (encoding_c
&& !strcmp(encoding_c
, "ASCII"))
1547 encoding
= CTF_STRING_ASCII
;
1548 string_declaration
= string_declaration_new(encoding
);
1549 return &string_declaration
->p
;
1554 struct declaration
*ctf_type_specifier_list_visit(FILE *fd
,
1555 int depth
, struct ctf_node
*type_specifier_list
,
1556 struct declaration_scope
*declaration_scope
,
1557 struct ctf_trace
*trace
)
1559 struct ctf_node
*first
;
1560 struct ctf_node
*node
;
1562 assert(type_specifier_list
->type
== NODE_TYPE_SPECIFIER_LIST
);
1564 first
= _bt_list_first_entry(&type_specifier_list
->u
.type_specifier_list
.head
, struct ctf_node
, siblings
);
1566 assert(first
->type
== NODE_TYPE_SPECIFIER
);
1568 node
= first
->u
.type_specifier
.node
;
1570 switch (first
->u
.type_specifier
.type
) {
1571 case TYPESPEC_FLOATING_POINT
:
1572 return ctf_declaration_floating_point_visit(fd
, depth
,
1573 &node
->u
.floating_point
.expressions
, trace
);
1574 case TYPESPEC_INTEGER
:
1575 return ctf_declaration_integer_visit(fd
, depth
,
1576 &node
->u
.integer
.expressions
, trace
);
1577 case TYPESPEC_STRING
:
1578 return ctf_declaration_string_visit(fd
, depth
,
1579 &node
->u
.string
.expressions
, trace
);
1580 case TYPESPEC_STRUCT
:
1581 return ctf_declaration_struct_visit(fd
, depth
,
1582 node
->u
._struct
.name
,
1583 &node
->u
._struct
.declaration_list
,
1584 node
->u
._struct
.has_body
,
1585 &node
->u
._struct
.min_align
,
1588 case TYPESPEC_VARIANT
:
1589 return ctf_declaration_variant_visit(fd
, depth
,
1590 node
->u
.variant
.name
,
1591 node
->u
.variant
.choice
,
1592 &node
->u
.variant
.declaration_list
,
1593 node
->u
.variant
.has_body
,
1597 return ctf_declaration_enum_visit(fd
, depth
,
1598 node
->u
._enum
.enum_id
,
1599 node
->u
._enum
.container_type
,
1600 &node
->u
._enum
.enumerator_list
,
1601 node
->u
._enum
.has_body
,
1607 case TYPESPEC_SHORT
:
1610 case TYPESPEC_FLOAT
:
1611 case TYPESPEC_DOUBLE
:
1612 case TYPESPEC_SIGNED
:
1613 case TYPESPEC_UNSIGNED
:
1615 case TYPESPEC_COMPLEX
:
1616 case TYPESPEC_IMAGINARY
:
1617 case TYPESPEC_CONST
:
1618 case TYPESPEC_ID_TYPE
:
1619 return ctf_declaration_type_specifier_visit(fd
, depth
,
1620 type_specifier_list
, declaration_scope
);
1622 fprintf(fd
, "[error] %s: unexpected node type %d\n", __func__
, (int) first
->u
.type_specifier
.type
);
1628 int ctf_event_declaration_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_event_declaration
*event
, struct ctf_trace
*trace
)
1632 switch (node
->type
) {
1634 ret
= ctf_typedef_visit(fd
, depth
+ 1,
1635 event
->declaration_scope
,
1636 node
->u
._typedef
.type_specifier_list
,
1637 &node
->u
._typedef
.type_declarators
,
1642 case NODE_TYPEALIAS
:
1643 ret
= ctf_typealias_visit(fd
, depth
+ 1,
1644 event
->declaration_scope
,
1645 node
->u
.typealias
.target
, node
->u
.typealias
.alias
,
1650 case NODE_CTF_EXPRESSION
:
1654 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
1655 if (!strcmp(left
, "name")) {
1658 if (CTF_EVENT_FIELD_IS_SET(event
, name
)) {
1659 fprintf(fd
, "[error] %s: name already declared in event declaration\n", __func__
);
1663 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
1665 fprintf(fd
, "[error] %s: unexpected unary expression for event name\n", __func__
);
1669 event
->name
= g_quark_from_string(right
);
1671 CTF_EVENT_SET_FIELD(event
, name
);
1672 } else if (!strcmp(left
, "id")) {
1673 if (CTF_EVENT_FIELD_IS_SET(event
, id
)) {
1674 fprintf(fd
, "[error] %s: id already declared in event declaration\n", __func__
);
1678 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &event
->id
);
1680 fprintf(fd
, "[error] %s: unexpected unary expression for event id\n", __func__
);
1684 CTF_EVENT_SET_FIELD(event
, id
);
1685 } else if (!strcmp(left
, "stream_id")) {
1686 if (CTF_EVENT_FIELD_IS_SET(event
, stream_id
)) {
1687 fprintf(fd
, "[error] %s: stream_id already declared in event declaration\n", __func__
);
1691 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &event
->stream_id
);
1693 fprintf(fd
, "[error] %s: unexpected unary expression for event stream_id\n", __func__
);
1697 event
->stream
= trace_stream_lookup(trace
, event
->stream_id
);
1698 if (!event
->stream
) {
1699 fprintf(fd
, "[error] %s: stream id %" PRIu64
" cannot be found\n", __func__
, event
->stream_id
);
1703 CTF_EVENT_SET_FIELD(event
, stream_id
);
1704 } else if (!strcmp(left
, "context")) {
1705 struct declaration
*declaration
;
1707 if (event
->context_decl
) {
1708 fprintf(fd
, "[error] %s: context already declared in event declaration\n", __func__
);
1712 declaration
= ctf_type_specifier_list_visit(fd
, depth
,
1713 _bt_list_first_entry(&node
->u
.ctf_expression
.right
,
1714 struct ctf_node
, siblings
),
1715 event
->declaration_scope
, trace
);
1720 if (declaration
->id
!= CTF_TYPE_STRUCT
) {
1724 event
->context_decl
= container_of(declaration
, struct declaration_struct
, p
);
1725 } else if (!strcmp(left
, "fields")) {
1726 struct declaration
*declaration
;
1728 if (event
->fields_decl
) {
1729 fprintf(fd
, "[error] %s: fields already declared in event declaration\n", __func__
);
1733 declaration
= ctf_type_specifier_list_visit(fd
, depth
,
1734 _bt_list_first_entry(&node
->u
.ctf_expression
.right
,
1735 struct ctf_node
, siblings
),
1736 event
->declaration_scope
, trace
);
1741 if (declaration
->id
!= CTF_TYPE_STRUCT
) {
1745 event
->fields_decl
= container_of(declaration
, struct declaration_struct
, p
);
1746 } else if (!strcmp(left
, "loglevel")) {
1747 int64_t loglevel
= -1;
1749 if (CTF_EVENT_FIELD_IS_SET(event
, loglevel
)) {
1750 fprintf(fd
, "[error] %s: loglevel already declared in event declaration\n", __func__
);
1754 ret
= get_unary_signed(&node
->u
.ctf_expression
.right
, &loglevel
);
1756 fprintf(fd
, "[error] %s: unexpected unary expression for event loglevel\n", __func__
);
1760 event
->loglevel
= (int) loglevel
;
1761 CTF_EVENT_SET_FIELD(event
, loglevel
);
1762 } else if (!strcmp(left
, "model.emf.uri")) {
1765 if (CTF_EVENT_FIELD_IS_SET(event
, model_emf_uri
)) {
1766 fprintf(fd
, "[error] %s: model.emf.uri already declared in event declaration\n", __func__
);
1770 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
1772 fprintf(fd
, "[error] %s: unexpected unary expression for event model.emf.uri\n", __func__
);
1776 event
->model_emf_uri
= g_quark_from_string(right
);
1778 CTF_EVENT_SET_FIELD(event
, model_emf_uri
);
1780 fprintf(fd
, "[warning] %s: attribute \"%s\" is unknown in event declaration.\n", __func__
, left
);
1781 /* Fall-through after warning */
1789 /* TODO: declaration specifier should be added. */
1796 int ctf_event_visit(FILE *fd
, int depth
, struct ctf_node
*node
,
1797 struct declaration_scope
*parent_declaration_scope
, struct ctf_trace
*trace
)
1800 struct ctf_node
*iter
;
1801 struct ctf_event_declaration
*event
;
1802 struct bt_ctf_event_decl
*event_decl
;
1804 event_decl
= g_new0(struct bt_ctf_event_decl
, 1);
1805 event
= &event_decl
->parent
;
1806 event
->declaration_scope
= new_declaration_scope(parent_declaration_scope
);
1807 event
->loglevel
= -1;
1808 bt_list_for_each_entry(iter
, &node
->u
.event
.declaration_list
, siblings
) {
1809 ret
= ctf_event_declaration_visit(fd
, depth
+ 1, iter
, event
, trace
);
1813 if (!CTF_EVENT_FIELD_IS_SET(event
, name
)) {
1815 fprintf(fd
, "[error] %s: missing name field in event declaration\n", __func__
);
1818 if (!CTF_EVENT_FIELD_IS_SET(event
, stream_id
)) {
1819 /* Allow missing stream_id if there is only a single stream */
1820 switch (trace
->streams
->len
) {
1821 case 0: /* Create stream if there was none. */
1822 ret
= ctf_stream_visit(fd
, depth
, NULL
, trace
->root_declaration_scope
, trace
);
1827 event
->stream_id
= 0;
1828 event
->stream
= trace_stream_lookup(trace
, event
->stream_id
);
1832 fprintf(fd
, "[error] %s: missing stream_id field in event declaration\n", __func__
);
1836 /* Allow only one event without id per stream */
1837 if (!CTF_EVENT_FIELD_IS_SET(event
, id
)
1838 && event
->stream
->events_by_id
->len
!= 0) {
1840 fprintf(fd
, "[error] %s: missing id field in event declaration\n", __func__
);
1843 if (event
->stream
->events_by_id
->len
<= event
->id
)
1844 g_ptr_array_set_size(event
->stream
->events_by_id
, event
->id
+ 1);
1845 g_ptr_array_index(event
->stream
->events_by_id
, event
->id
) = event
;
1846 g_hash_table_insert(event
->stream
->event_quark_to_id
,
1847 (gpointer
) (unsigned long) event
->name
,
1849 g_ptr_array_add(trace
->event_declarations
, event_decl
);
1853 if (event
->fields_decl
)
1854 declaration_unref(&event
->fields_decl
->p
);
1855 if (event
->context_decl
)
1856 declaration_unref(&event
->context_decl
->p
);
1857 free_declaration_scope(event
->declaration_scope
);
1864 int ctf_stream_declaration_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_stream_declaration
*stream
, struct ctf_trace
*trace
)
1868 switch (node
->type
) {
1870 ret
= ctf_typedef_visit(fd
, depth
+ 1,
1871 stream
->declaration_scope
,
1872 node
->u
._typedef
.type_specifier_list
,
1873 &node
->u
._typedef
.type_declarators
,
1878 case NODE_TYPEALIAS
:
1879 ret
= ctf_typealias_visit(fd
, depth
+ 1,
1880 stream
->declaration_scope
,
1881 node
->u
.typealias
.target
, node
->u
.typealias
.alias
,
1886 case NODE_CTF_EXPRESSION
:
1890 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
1891 if (!strcmp(left
, "id")) {
1892 if (CTF_STREAM_FIELD_IS_SET(stream
, stream_id
)) {
1893 fprintf(fd
, "[error] %s: id already declared in stream declaration\n", __func__
);
1897 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &stream
->stream_id
);
1899 fprintf(fd
, "[error] %s: unexpected unary expression for stream id\n", __func__
);
1903 CTF_STREAM_SET_FIELD(stream
, stream_id
);
1904 } else if (!strcmp(left
, "event.header")) {
1905 struct declaration
*declaration
;
1907 if (stream
->event_header_decl
) {
1908 fprintf(fd
, "[error] %s: event.header already declared in stream declaration\n", __func__
);
1912 declaration
= ctf_type_specifier_list_visit(fd
, depth
,
1913 _bt_list_first_entry(&node
->u
.ctf_expression
.right
,
1914 struct ctf_node
, siblings
),
1915 stream
->declaration_scope
, trace
);
1920 if (declaration
->id
!= CTF_TYPE_STRUCT
) {
1924 stream
->event_header_decl
= container_of(declaration
, struct declaration_struct
, p
);
1925 } else if (!strcmp(left
, "event.context")) {
1926 struct declaration
*declaration
;
1928 if (stream
->event_context_decl
) {
1929 fprintf(fd
, "[error] %s: event.context already declared in stream declaration\n", __func__
);
1933 declaration
= ctf_type_specifier_list_visit(fd
, depth
,
1934 _bt_list_first_entry(&node
->u
.ctf_expression
.right
,
1935 struct ctf_node
, siblings
),
1936 stream
->declaration_scope
, trace
);
1941 if (declaration
->id
!= CTF_TYPE_STRUCT
) {
1945 stream
->event_context_decl
= container_of(declaration
, struct declaration_struct
, p
);
1946 } else if (!strcmp(left
, "packet.context")) {
1947 struct declaration
*declaration
;
1949 if (stream
->packet_context_decl
) {
1950 fprintf(fd
, "[error] %s: packet.context already declared in stream declaration\n", __func__
);
1954 declaration
= ctf_type_specifier_list_visit(fd
, depth
,
1955 _bt_list_first_entry(&node
->u
.ctf_expression
.right
,
1956 struct ctf_node
, siblings
),
1957 stream
->declaration_scope
, trace
);
1962 if (declaration
->id
!= CTF_TYPE_STRUCT
) {
1966 stream
->packet_context_decl
= container_of(declaration
, struct declaration_struct
, p
);
1968 fprintf(fd
, "[warning] %s: attribute \"%s\" is unknown in stream declaration.\n", __func__
, left
);
1969 /* Fall-through after warning */
1978 /* TODO: declaration specifier should be added. */
1985 int ctf_stream_visit(FILE *fd
, int depth
, struct ctf_node
*node
,
1986 struct declaration_scope
*parent_declaration_scope
, struct ctf_trace
*trace
)
1989 struct ctf_node
*iter
;
1990 struct ctf_stream_declaration
*stream
;
1992 stream
= g_new0(struct ctf_stream_declaration
, 1);
1993 stream
->declaration_scope
= new_declaration_scope(parent_declaration_scope
);
1994 stream
->events_by_id
= g_ptr_array_new();
1995 stream
->event_quark_to_id
= g_hash_table_new(g_direct_hash
, g_direct_equal
);
1996 stream
->streams
= g_ptr_array_new();
1998 bt_list_for_each_entry(iter
, &node
->u
.stream
.declaration_list
, siblings
) {
1999 ret
= ctf_stream_declaration_visit(fd
, depth
+ 1, iter
, stream
, trace
);
2004 if (CTF_STREAM_FIELD_IS_SET(stream
, stream_id
)) {
2005 /* check that packet header has stream_id field. */
2006 if (!trace
->packet_header_decl
2007 || struct_declaration_lookup_field_index(trace
->packet_header_decl
, g_quark_from_static_string("stream_id")) < 0) {
2009 fprintf(fd
, "[error] %s: missing stream_id field in packet header declaration, but stream_id attribute is declared for stream.\n", __func__
);
2013 /* Allow only one id-less stream */
2014 if (trace
->streams
->len
!= 0) {
2016 fprintf(fd
, "[error] %s: missing id field in stream declaration\n", __func__
);
2019 stream
->stream_id
= 0;
2021 if (trace
->streams
->len
<= stream
->stream_id
)
2022 g_ptr_array_set_size(trace
->streams
, stream
->stream_id
+ 1);
2023 g_ptr_array_index(trace
->streams
, stream
->stream_id
) = stream
;
2024 stream
->trace
= trace
;
2029 if (stream
->event_header_decl
)
2030 declaration_unref(&stream
->event_header_decl
->p
);
2031 if (stream
->event_context_decl
)
2032 declaration_unref(&stream
->event_context_decl
->p
);
2033 if (stream
->packet_context_decl
)
2034 declaration_unref(&stream
->packet_context_decl
->p
);
2035 g_ptr_array_free(stream
->streams
, TRUE
);
2036 g_ptr_array_free(stream
->events_by_id
, TRUE
);
2037 g_hash_table_destroy(stream
->event_quark_to_id
);
2038 free_declaration_scope(stream
->declaration_scope
);
2044 int ctf_trace_declaration_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_trace
*trace
)
2048 switch (node
->type
) {
2050 ret
= ctf_typedef_visit(fd
, depth
+ 1,
2051 trace
->declaration_scope
,
2052 node
->u
._typedef
.type_specifier_list
,
2053 &node
->u
._typedef
.type_declarators
,
2058 case NODE_TYPEALIAS
:
2059 ret
= ctf_typealias_visit(fd
, depth
+ 1,
2060 trace
->declaration_scope
,
2061 node
->u
.typealias
.target
, node
->u
.typealias
.alias
,
2066 case NODE_CTF_EXPRESSION
:
2070 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
2071 if (!strcmp(left
, "major")) {
2072 if (CTF_TRACE_FIELD_IS_SET(trace
, major
)) {
2073 fprintf(fd
, "[error] %s: major already declared in trace declaration\n", __func__
);
2077 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &trace
->major
);
2079 fprintf(fd
, "[error] %s: unexpected unary expression for trace major number\n", __func__
);
2083 CTF_TRACE_SET_FIELD(trace
, major
);
2084 } else if (!strcmp(left
, "minor")) {
2085 if (CTF_TRACE_FIELD_IS_SET(trace
, minor
)) {
2086 fprintf(fd
, "[error] %s: minor already declared in trace declaration\n", __func__
);
2090 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &trace
->minor
);
2092 fprintf(fd
, "[error] %s: unexpected unary expression for trace minor number\n", __func__
);
2096 CTF_TRACE_SET_FIELD(trace
, minor
);
2097 } else if (!strcmp(left
, "uuid")) {
2098 unsigned char uuid
[BABELTRACE_UUID_LEN
];
2100 ret
= get_unary_uuid(&node
->u
.ctf_expression
.right
, uuid
);
2102 fprintf(fd
, "[error] %s: unexpected unary expression for trace uuid\n", __func__
);
2106 if (CTF_TRACE_FIELD_IS_SET(trace
, uuid
)
2107 && babeltrace_uuid_compare(uuid
, trace
->uuid
)) {
2108 fprintf(fd
, "[error] %s: uuid mismatch\n", __func__
);
2112 memcpy(trace
->uuid
, uuid
, sizeof(uuid
));
2114 CTF_TRACE_SET_FIELD(trace
, uuid
);
2115 } else if (!strcmp(left
, "byte_order")) {
2116 struct ctf_node
*right
;
2119 right
= _bt_list_first_entry(&node
->u
.ctf_expression
.right
, struct ctf_node
, siblings
);
2120 byte_order
= get_trace_byte_order(fd
, depth
, right
);
2124 if (CTF_TRACE_FIELD_IS_SET(trace
, byte_order
)
2125 && byte_order
!= trace
->byte_order
) {
2126 fprintf(fd
, "[error] %s: endianness mismatch\n", __func__
);
2130 if (byte_order
!= trace
->byte_order
) {
2131 trace
->byte_order
= byte_order
;
2133 * We need to restart
2134 * construction of the
2135 * intermediate representation.
2137 trace
->field_mask
= 0;
2138 CTF_TRACE_SET_FIELD(trace
, byte_order
);
2143 CTF_TRACE_SET_FIELD(trace
, byte_order
);
2144 } else if (!strcmp(left
, "packet.header")) {
2145 struct declaration
*declaration
;
2147 if (trace
->packet_header_decl
) {
2148 fprintf(fd
, "[error] %s: packet.header already declared in trace declaration\n", __func__
);
2152 declaration
= ctf_type_specifier_list_visit(fd
, depth
,
2153 _bt_list_first_entry(&node
->u
.ctf_expression
.right
,
2154 struct ctf_node
, siblings
),
2155 trace
->declaration_scope
, trace
);
2160 if (declaration
->id
!= CTF_TYPE_STRUCT
) {
2164 trace
->packet_header_decl
= container_of(declaration
, struct declaration_struct
, p
);
2166 fprintf(fd
, "[warning] %s: attribute \"%s\" is unknown in trace declaration.\n", __func__
, left
);
2175 /* TODO: declaration specifier should be added. */
2182 int ctf_trace_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_trace
*trace
)
2185 struct ctf_node
*iter
;
2187 if (trace
->declaration_scope
)
2189 trace
->declaration_scope
= new_declaration_scope(trace
->root_declaration_scope
);
2190 trace
->streams
= g_ptr_array_new();
2191 trace
->event_declarations
= g_ptr_array_new();
2192 bt_list_for_each_entry(iter
, &node
->u
.trace
.declaration_list
, siblings
) {
2193 ret
= ctf_trace_declaration_visit(fd
, depth
+ 1, iter
, trace
);
2197 if (!CTF_TRACE_FIELD_IS_SET(trace
, major
)) {
2199 fprintf(fd
, "[error] %s: missing major field in trace declaration\n", __func__
);
2202 if (!CTF_TRACE_FIELD_IS_SET(trace
, minor
)) {
2204 fprintf(fd
, "[error] %s: missing minor field in trace declaration\n", __func__
);
2207 if (!CTF_TRACE_FIELD_IS_SET(trace
, byte_order
)) {
2209 fprintf(fd
, "[error] %s: missing byte_order field in trace declaration\n", __func__
);
2213 if (!CTF_TRACE_FIELD_IS_SET(trace
, byte_order
)) {
2214 /* check that the packet header contains a "magic" field */
2215 if (!trace
->packet_header_decl
2216 || struct_declaration_lookup_field_index(trace
->packet_header_decl
, g_quark_from_static_string("magic")) < 0) {
2218 fprintf(fd
, "[error] %s: missing both byte_order and packet header magic number in trace declaration\n", __func__
);
2225 if (trace
->packet_header_decl
) {
2226 declaration_unref(&trace
->packet_header_decl
->p
);
2227 trace
->packet_header_decl
= NULL
;
2229 g_ptr_array_free(trace
->streams
, TRUE
);
2230 g_ptr_array_free(trace
->event_declarations
, TRUE
);
2231 free_declaration_scope(trace
->declaration_scope
);
2232 trace
->declaration_scope
= NULL
;
2237 int ctf_clock_declaration_visit(FILE *fd
, int depth
, struct ctf_node
*node
,
2238 struct ctf_clock
*clock
, struct ctf_trace
*trace
)
2242 switch (node
->type
) {
2243 case NODE_CTF_EXPRESSION
:
2247 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
2248 if (!strcmp(left
, "name")) {
2251 if (CTF_CLOCK_FIELD_IS_SET(clock
, name
)) {
2252 fprintf(fd
, "[error] %s: name already declared in clock declaration\n", __func__
);
2256 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
2258 fprintf(fd
, "[error] %s: unexpected unary expression for clock name\n", __func__
);
2262 clock
->name
= g_quark_from_string(right
);
2264 CTF_CLOCK_SET_FIELD(clock
, name
);
2265 } else if (!strcmp(left
, "uuid")) {
2269 fprintf(fd
, "[error] %s: uuid already declared in clock declaration\n", __func__
);
2273 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
2275 fprintf(fd
, "[error] %s: unexpected unary expression for clock uuid\n", __func__
);
2279 clock
->uuid
= g_quark_from_string(right
);
2281 } else if (!strcmp(left
, "description")) {
2284 if (clock
->description
) {
2285 fprintf(fd
, "[warning] %s: duplicated clock description\n", __func__
);
2286 goto error
; /* ret is 0, so not an actual error, just warn. */
2288 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
2290 fprintf(fd
, "[warning] %s: unexpected unary expression for clock description\n", __func__
);
2291 goto error
; /* ret is 0, so not an actual error, just warn. */
2293 clock
->description
= right
;
2294 } else if (!strcmp(left
, "freq")) {
2295 if (CTF_CLOCK_FIELD_IS_SET(clock
, freq
)) {
2296 fprintf(fd
, "[error] %s: freq already declared in clock declaration\n", __func__
);
2300 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &clock
->freq
);
2302 fprintf(fd
, "[error] %s: unexpected unary expression for clock freq\n", __func__
);
2306 CTF_CLOCK_SET_FIELD(clock
, freq
);
2307 } else if (!strcmp(left
, "precision")) {
2308 if (clock
->precision
) {
2309 fprintf(fd
, "[error] %s: precision already declared in clock declaration\n", __func__
);
2313 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &clock
->precision
);
2315 fprintf(fd
, "[error] %s: unexpected unary expression for clock precision\n", __func__
);
2319 } else if (!strcmp(left
, "offset_s")) {
2320 if (clock
->offset_s
) {
2321 fprintf(fd
, "[error] %s: offset_s already declared in clock declaration\n", __func__
);
2325 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &clock
->offset_s
);
2327 fprintf(fd
, "[error] %s: unexpected unary expression for clock offset_s\n", __func__
);
2331 } else if (!strcmp(left
, "offset")) {
2332 if (clock
->offset
) {
2333 fprintf(fd
, "[error] %s: offset already declared in clock declaration\n", __func__
);
2337 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &clock
->offset
);
2339 fprintf(fd
, "[error] %s: unexpected unary expression for clock offset\n", __func__
);
2343 } else if (!strcmp(left
, "absolute")) {
2344 struct ctf_node
*right
;
2346 right
= _bt_list_first_entry(&node
->u
.ctf_expression
.right
, struct ctf_node
, siblings
);
2347 ret
= get_boolean(fd
, depth
, right
);
2349 fprintf(fd
, "[error] %s: unexpected \"absolute\" right member\n", __func__
);
2353 clock
->absolute
= ret
;
2355 fprintf(fd
, "[warning] %s: attribute \"%s\" is unknown in clock declaration.\n", __func__
, left
);
2364 /* TODO: declaration specifier should be added. */
2371 int ctf_clock_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_trace
*trace
)
2374 struct ctf_node
*iter
;
2375 struct ctf_clock
*clock
;
2377 clock
= g_new0(struct ctf_clock
, 1);
2378 /* Default clock frequency is set to 1000000000 */
2379 clock
->freq
= 1000000000ULL;
2380 bt_list_for_each_entry(iter
, &node
->u
.clock
.declaration_list
, siblings
) {
2381 ret
= ctf_clock_declaration_visit(fd
, depth
+ 1, iter
, clock
, trace
);
2385 if (opt_clock_force_correlate
) {
2387 * User requested to forcibly correlate the clock
2388 * sources, even if we have no correlatation
2391 if (!clock
->absolute
) {
2392 fprintf(fd
, "[warning] Forcibly correlating trace clock sources (--clock-force-correlate).\n");
2394 clock
->absolute
= 1;
2396 if (!CTF_CLOCK_FIELD_IS_SET(clock
, name
)) {
2398 fprintf(fd
, "[error] %s: missing name field in clock declaration\n", __func__
);
2401 if (g_hash_table_size(trace
->clocks
) > 0) {
2402 fprintf(fd
, "[error] Only CTF traces with a single clock description are supported by this babeltrace version.\n");
2406 trace
->single_clock
= clock
;
2407 g_hash_table_insert(trace
->clocks
, (gpointer
) (unsigned long) clock
->name
, clock
);
2411 g_free(clock
->description
);
2417 void ctf_clock_default(FILE *fd
, int depth
, struct ctf_trace
*trace
)
2419 struct ctf_clock
*clock
;
2421 clock
= g_new0(struct ctf_clock
, 1);
2422 clock
->name
= g_quark_from_string("monotonic");
2424 clock
->description
= g_strdup("Default clock");
2425 /* Default clock frequency is set to 1000000000 */
2426 clock
->freq
= 1000000000ULL;
2427 if (opt_clock_force_correlate
) {
2429 * User requested to forcibly correlate the clock
2430 * sources, even if we have no correlatation
2433 if (!clock
->absolute
) {
2434 fprintf(fd
, "[warning] Forcibly correlating trace clock sources (--clock-force-correlate).\n");
2436 clock
->absolute
= 1;
2438 clock
->absolute
= 0; /* Not an absolute reference across traces */
2441 trace
->single_clock
= clock
;
2442 g_hash_table_insert(trace
->clocks
, (gpointer
) (unsigned long) clock
->name
, clock
);
2446 void clock_free(gpointer data
)
2448 struct ctf_clock
*clock
= data
;
2450 g_free(clock
->description
);
2455 int ctf_callsite_declaration_visit(FILE *fd
, int depth
, struct ctf_node
*node
,
2456 struct ctf_callsite
*callsite
, struct ctf_trace
*trace
)
2460 switch (node
->type
) {
2461 case NODE_CTF_EXPRESSION
:
2465 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
2466 if (!strcmp(left
, "name")) {
2469 if (CTF_CALLSITE_FIELD_IS_SET(callsite
, name
)) {
2470 fprintf(fd
, "[error] %s: name already declared in callsite declaration\n", __func__
);
2474 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
2476 fprintf(fd
, "[error] %s: unexpected unary expression for callsite name\n", __func__
);
2480 callsite
->name
= g_quark_from_string(right
);
2482 CTF_CALLSITE_SET_FIELD(callsite
, name
);
2483 } else if (!strcmp(left
, "func")) {
2486 if (CTF_CALLSITE_FIELD_IS_SET(callsite
, func
)) {
2487 fprintf(fd
, "[error] %s: func already declared in callsite declaration\n", __func__
);
2491 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
2493 fprintf(fd
, "[error] %s: unexpected unary expression for callsite func\n", __func__
);
2497 callsite
->func
= right
;
2498 CTF_CALLSITE_SET_FIELD(callsite
, func
);
2499 } else if (!strcmp(left
, "file")) {
2502 if (CTF_CALLSITE_FIELD_IS_SET(callsite
, file
)) {
2503 fprintf(fd
, "[error] %s: file already declared in callsite declaration\n", __func__
);
2507 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
2509 fprintf(fd
, "[error] %s: unexpected unary expression for callsite file\n", __func__
);
2513 callsite
->file
= right
;
2514 CTF_CALLSITE_SET_FIELD(callsite
, file
);
2515 } else if (!strcmp(left
, "line")) {
2516 if (CTF_CALLSITE_FIELD_IS_SET(callsite
, line
)) {
2517 fprintf(fd
, "[error] %s: line already declared in callsite declaration\n", __func__
);
2521 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &callsite
->line
);
2523 fprintf(fd
, "[error] %s: unexpected unary expression for callsite line\n", __func__
);
2527 CTF_CALLSITE_SET_FIELD(callsite
, line
);
2528 } else if (!strcmp(left
, "ip")) {
2529 if (CTF_CALLSITE_FIELD_IS_SET(callsite
, ip
)) {
2530 fprintf(fd
, "[error] %s: ip already declared in callsite declaration\n", __func__
);
2534 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &callsite
->ip
);
2536 fprintf(fd
, "[error] %s: unexpected unary expression for callsite ip\n", __func__
);
2540 CTF_CALLSITE_SET_FIELD(callsite
, ip
);
2542 fprintf(fd
, "[warning] %s: attribute \"%s\" is unknown in callsite declaration.\n", __func__
, left
);
2551 /* TODO: declaration specifier should be added. */
2558 int ctf_callsite_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_trace
*trace
)
2561 struct ctf_node
*iter
;
2562 struct ctf_callsite
*callsite
;
2563 struct ctf_callsite_dups
*cs_dups
;
2565 callsite
= g_new0(struct ctf_callsite
, 1);
2566 bt_list_for_each_entry(iter
, &node
->u
.callsite
.declaration_list
, siblings
) {
2567 ret
= ctf_callsite_declaration_visit(fd
, depth
+ 1, iter
, callsite
, trace
);
2571 if (!CTF_CALLSITE_FIELD_IS_SET(callsite
, name
)) {
2573 fprintf(fd
, "[error] %s: missing name field in callsite declaration\n", __func__
);
2576 if (!CTF_CALLSITE_FIELD_IS_SET(callsite
, func
)) {
2578 fprintf(fd
, "[error] %s: missing func field in callsite declaration\n", __func__
);
2581 if (!CTF_CALLSITE_FIELD_IS_SET(callsite
, file
)) {
2583 fprintf(fd
, "[error] %s: missing file field in callsite declaration\n", __func__
);
2586 if (!CTF_CALLSITE_FIELD_IS_SET(callsite
, line
)) {
2588 fprintf(fd
, "[error] %s: missing line field in callsite declaration\n", __func__
);
2592 cs_dups
= g_hash_table_lookup(trace
->callsites
,
2593 (gpointer
) (unsigned long) callsite
->name
);
2595 cs_dups
= g_new0(struct ctf_callsite_dups
, 1);
2596 BT_INIT_LIST_HEAD(&cs_dups
->head
);
2597 g_hash_table_insert(trace
->callsites
,
2598 (gpointer
) (unsigned long) callsite
->name
, cs_dups
);
2600 bt_list_add_tail(&callsite
->node
, &cs_dups
->head
);
2604 g_free(callsite
->func
);
2605 g_free(callsite
->file
);
2611 void callsite_free(gpointer data
)
2613 struct ctf_callsite_dups
*cs_dups
= data
;
2614 struct ctf_callsite
*callsite
, *cs_n
;
2616 bt_list_for_each_entry_safe(callsite
, cs_n
, &cs_dups
->head
, node
) {
2617 g_free(callsite
->func
);
2618 g_free(callsite
->file
);
2625 int ctf_env_declaration_visit(FILE *fd
, int depth
, struct ctf_node
*node
,
2626 struct ctf_trace
*trace
)
2629 struct ctf_tracer_env
*env
= &trace
->env
;
2631 switch (node
->type
) {
2632 case NODE_CTF_EXPRESSION
:
2636 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
2637 if (!strcmp(left
, "vpid")) {
2640 if (env
->vpid
!= -1) {
2641 fprintf(fd
, "[error] %s: vpid already declared in env declaration\n", __func__
);
2642 goto error
; /* ret is 0, so not an actual error, just warn. */
2644 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &v
);
2646 fprintf(fd
, "[error] %s: unexpected unary expression for env vpid\n", __func__
);
2647 goto error
; /* ret is 0, so not an actual error, just warn. */
2649 env
->vpid
= (int) v
;
2650 printf_verbose("env.vpid = %d\n", env
->vpid
);
2651 } else if (!strcmp(left
, "procname")) {
2654 if (env
->procname
[0]) {
2655 fprintf(fd
, "[warning] %s: duplicated env procname\n", __func__
);
2656 goto error
; /* ret is 0, so not an actual error, just warn. */
2658 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
2660 fprintf(fd
, "[warning] %s: unexpected unary expression for env procname\n", __func__
);
2661 goto error
; /* ret is 0, so not an actual error, just warn. */
2663 strncpy(env
->procname
, right
, TRACER_ENV_LEN
);
2664 env
->procname
[TRACER_ENV_LEN
- 1] = '\0';
2665 printf_verbose("env.procname = \"%s\"\n", env
->procname
);
2667 } else if (!strcmp(left
, "hostname")) {
2670 if (env
->hostname
[0]) {
2671 fprintf(fd
, "[warning] %s: duplicated env hostname\n", __func__
);
2672 goto error
; /* ret is 0, so not an actual error, just warn. */
2674 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
2676 fprintf(fd
, "[warning] %s: unexpected unary expression for env hostname\n", __func__
);
2677 goto error
; /* ret is 0, so not an actual error, just warn. */
2679 strncpy(env
->hostname
, right
, TRACER_ENV_LEN
);
2680 env
->hostname
[TRACER_ENV_LEN
- 1] = '\0';
2681 printf_verbose("env.hostname = \"%s\"\n", env
->hostname
);
2683 } else if (!strcmp(left
, "domain")) {
2686 if (env
->domain
[0]) {
2687 fprintf(fd
, "[warning] %s: duplicated env domain\n", __func__
);
2688 goto error
; /* ret is 0, so not an actual error, just warn. */
2690 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
2692 fprintf(fd
, "[warning] %s: unexpected unary expression for env domain\n", __func__
);
2693 goto error
; /* ret is 0, so not an actual error, just warn. */
2695 strncpy(env
->domain
, right
, TRACER_ENV_LEN
);
2696 env
->domain
[TRACER_ENV_LEN
- 1] = '\0';
2697 printf_verbose("env.domain = \"%s\"\n", env
->domain
);
2699 } else if (!strcmp(left
, "sysname")) {
2702 if (env
->sysname
[0]) {
2703 fprintf(fd
, "[warning] %s: duplicated env sysname\n", __func__
);
2704 goto error
; /* ret is 0, so not an actual error, just warn. */
2706 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
2708 fprintf(fd
, "[warning] %s: unexpected unary expression for env sysname\n", __func__
);
2709 goto error
; /* ret is 0, so not an actual error, just warn. */
2711 strncpy(env
->sysname
, right
, TRACER_ENV_LEN
);
2712 env
->sysname
[TRACER_ENV_LEN
- 1] = '\0';
2713 printf_verbose("env.sysname = \"%s\"\n", env
->sysname
);
2715 } else if (!strcmp(left
, "kernel_release")) {
2718 if (env
->release
[0]) {
2719 fprintf(fd
, "[warning] %s: duplicated env release\n", __func__
);
2720 goto error
; /* ret is 0, so not an actual error, just warn. */
2722 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
2724 fprintf(fd
, "[warning] %s: unexpected unary expression for env release\n", __func__
);
2725 goto error
; /* ret is 0, so not an actual error, just warn. */
2727 strncpy(env
->release
, right
, TRACER_ENV_LEN
);
2728 env
->release
[TRACER_ENV_LEN
- 1] = '\0';
2729 printf_verbose("env.release = \"%s\"\n", env
->release
);
2731 } else if (!strcmp(left
, "kernel_version")) {
2734 if (env
->version
[0]) {
2735 fprintf(fd
, "[warning] %s: duplicated env version\n", __func__
);
2736 goto error
; /* ret is 0, so not an actual error, just warn. */
2738 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
2740 fprintf(fd
, "[warning] %s: unexpected unary expression for env version\n", __func__
);
2741 goto error
; /* ret is 0, so not an actual error, just warn. */
2743 strncpy(env
->version
, right
, TRACER_ENV_LEN
);
2744 env
->version
[TRACER_ENV_LEN
- 1] = '\0';
2745 printf_verbose("env.version = \"%s\"\n", env
->version
);
2748 if (is_unary_string(&node
->u
.ctf_expression
.right
)) {
2751 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
2752 printf_verbose("env.%s = \"%s\"\n", left
, right
);
2754 } else if (is_unary_unsigned(&node
->u
.ctf_expression
.right
)) {
2758 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &v
);
2760 printf_verbose("env.%s = %" PRIu64
"\n", left
, v
);
2761 } else if (is_unary_signed(&node
->u
.ctf_expression
.right
)) {
2765 ret
= get_unary_signed(&node
->u
.ctf_expression
.right
, &v
);
2767 printf_verbose("env.%s = %" PRId64
"\n", left
, v
);
2769 printf_verbose("%s: attribute \"%s\" has unknown type.\n", __func__
, left
);
2785 int ctf_env_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_trace
*trace
)
2788 struct ctf_node
*iter
;
2790 trace
->env
.vpid
= -1;
2791 trace
->env
.procname
[0] = '\0';
2792 trace
->env
.hostname
[0] = '\0';
2793 trace
->env
.domain
[0] = '\0';
2794 trace
->env
.sysname
[0] = '\0';
2795 trace
->env
.release
[0] = '\0';
2796 trace
->env
.version
[0] = '\0';
2797 bt_list_for_each_entry(iter
, &node
->u
.env
.declaration_list
, siblings
) {
2798 ret
= ctf_env_declaration_visit(fd
, depth
+ 1, iter
, trace
);
2807 int ctf_root_declaration_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_trace
*trace
)
2811 switch (node
->type
) {
2813 ret
= ctf_typedef_visit(fd
, depth
+ 1,
2814 trace
->root_declaration_scope
,
2815 node
->u
._typedef
.type_specifier_list
,
2816 &node
->u
._typedef
.type_declarators
,
2821 case NODE_TYPEALIAS
:
2822 ret
= ctf_typealias_visit(fd
, depth
+ 1,
2823 trace
->root_declaration_scope
,
2824 node
->u
.typealias
.target
, node
->u
.typealias
.alias
,
2829 case NODE_TYPE_SPECIFIER_LIST
:
2831 struct declaration
*declaration
;
2834 * Just add the type specifier to the root scope
2835 * declaration scope. Release local reference.
2837 declaration
= ctf_type_specifier_list_visit(fd
, depth
+ 1,
2838 node
, trace
->root_declaration_scope
, trace
);
2841 declaration_unref(declaration
);
2851 int ctf_visitor_construct_metadata(FILE *fd
, int depth
, struct ctf_node
*node
,
2852 struct ctf_trace
*trace
, int byte_order
)
2855 struct ctf_node
*iter
;
2856 int env_clock_done
= 0;
2858 printf_verbose("CTF visitor: metadata construction... ");
2859 trace
->byte_order
= byte_order
;
2860 trace
->clocks
= g_hash_table_new_full(g_direct_hash
, g_direct_equal
,
2862 trace
->callsites
= g_hash_table_new_full(g_direct_hash
, g_direct_equal
,
2863 NULL
, callsite_free
);
2866 trace
->root_declaration_scope
= new_declaration_scope(NULL
);
2868 switch (node
->type
) {
2870 if (!env_clock_done
) {
2872 * declarations need to query clock hash table,
2873 * so clock need to be treated first.
2875 if (bt_list_empty(&node
->u
.root
.clock
)) {
2876 ctf_clock_default(fd
, depth
+ 1, trace
);
2878 bt_list_for_each_entry(iter
, &node
->u
.root
.clock
, siblings
) {
2879 ret
= ctf_clock_visit(fd
, depth
+ 1, iter
,
2882 fprintf(fd
, "[error] %s: clock declaration error\n", __func__
);
2889 bt_list_for_each_entry(iter
, &node
->u
.root
.declaration_list
,
2891 ret
= ctf_root_declaration_visit(fd
, depth
+ 1, iter
, trace
);
2893 fprintf(fd
, "[error] %s: root declaration error\n", __func__
);
2897 bt_list_for_each_entry(iter
, &node
->u
.root
.trace
, siblings
) {
2898 ret
= ctf_trace_visit(fd
, depth
+ 1, iter
, trace
);
2899 if (ret
== -EINTR
) {
2900 free_declaration_scope(trace
->root_declaration_scope
);
2902 * Need to restart creation of type
2903 * definitions, aliases and
2904 * trace header declarations.
2909 fprintf(fd
, "[error] %s: trace declaration error\n", __func__
);
2913 bt_list_for_each_entry(iter
, &node
->u
.root
.callsite
, siblings
) {
2914 ret
= ctf_callsite_visit(fd
, depth
+ 1, iter
,
2917 fprintf(fd
, "[error] %s: callsite declaration error\n", __func__
);
2921 if (!trace
->streams
) {
2922 fprintf(fd
, "[error] %s: missing trace declaration\n", __func__
);
2926 bt_list_for_each_entry(iter
, &node
->u
.root
.env
, siblings
) {
2927 ret
= ctf_env_visit(fd
, depth
+ 1, iter
, trace
);
2929 fprintf(fd
, "[error] %s: env declaration error\n", __func__
);
2933 bt_list_for_each_entry(iter
, &node
->u
.root
.stream
, siblings
) {
2934 ret
= ctf_stream_visit(fd
, depth
+ 1, iter
,
2935 trace
->root_declaration_scope
, trace
);
2937 fprintf(fd
, "[error] %s: stream declaration error\n", __func__
);
2941 bt_list_for_each_entry(iter
, &node
->u
.root
.event
, siblings
) {
2942 ret
= ctf_event_visit(fd
, depth
+ 1, iter
,
2943 trace
->root_declaration_scope
, trace
);
2945 fprintf(fd
, "[error] %s: event declaration error\n", __func__
);
2952 fprintf(fd
, "[error] %s: unknown node type %d\n", __func__
,
2957 printf_verbose("done.\n");
2961 free_declaration_scope(trace
->root_declaration_scope
);
2962 g_hash_table_destroy(trace
->callsites
);
2963 g_hash_table_destroy(trace
->clocks
);
2967 int ctf_destroy_metadata(struct ctf_trace
*trace
)
2970 struct ctf_file_stream
*metadata_stream
;
2972 if (trace
->streams
) {
2973 for (i
= 0; i
< trace
->streams
->len
; i
++) {
2974 struct ctf_stream_declaration
*stream
;
2977 stream
= g_ptr_array_index(trace
->streams
, i
);
2980 for (j
= 0; j
< stream
->streams
->len
; j
++) {
2981 struct ctf_stream_definition
*stream_def
;
2984 stream_def
= g_ptr_array_index(stream
->streams
, j
);
2987 for (k
= 0; k
< stream_def
->events_by_id
->len
; k
++) {
2988 struct ctf_event_definition
*event
;
2990 event
= g_ptr_array_index(stream_def
->events_by_id
, k
);
2993 if (&event
->event_fields
->p
)
2994 definition_unref(&event
->event_fields
->p
);
2995 if (&event
->event_context
->p
)
2996 definition_unref(&event
->event_context
->p
);
2999 if (&stream_def
->trace_packet_header
->p
)
3000 definition_unref(&stream_def
->trace_packet_header
->p
);
3001 if (&stream_def
->stream_event_header
->p
)
3002 definition_unref(&stream_def
->stream_event_header
->p
);
3003 if (&stream_def
->stream_packet_context
->p
)
3004 definition_unref(&stream_def
->stream_packet_context
->p
);
3005 if (&stream_def
->stream_event_context
->p
)
3006 definition_unref(&stream_def
->stream_event_context
->p
);
3007 g_ptr_array_free(stream_def
->events_by_id
, TRUE
);
3010 if (stream
->event_header_decl
)
3011 declaration_unref(&stream
->event_header_decl
->p
);
3012 if (stream
->event_context_decl
)
3013 declaration_unref(&stream
->event_context_decl
->p
);
3014 if (stream
->packet_context_decl
)
3015 declaration_unref(&stream
->packet_context_decl
->p
);
3016 g_ptr_array_free(stream
->streams
, TRUE
);
3017 g_ptr_array_free(stream
->events_by_id
, TRUE
);
3018 g_hash_table_destroy(stream
->event_quark_to_id
);
3019 free_declaration_scope(stream
->declaration_scope
);
3022 g_ptr_array_free(trace
->streams
, TRUE
);
3025 if (trace
->event_declarations
) {
3026 for (i
= 0; i
< trace
->event_declarations
->len
; i
++) {
3027 struct bt_ctf_event_decl
*event_decl
;
3028 struct ctf_event_declaration
*event
;
3030 event_decl
= g_ptr_array_index(trace
->event_declarations
, i
);
3031 if (event_decl
->context_decl
)
3032 g_ptr_array_free(event_decl
->context_decl
, TRUE
);
3033 if (event_decl
->fields_decl
)
3034 g_ptr_array_free(event_decl
->fields_decl
, TRUE
);
3035 if (event_decl
->packet_header_decl
)
3036 g_ptr_array_free(event_decl
->packet_header_decl
, TRUE
);
3037 if (event_decl
->event_context_decl
)
3038 g_ptr_array_free(event_decl
->event_context_decl
, TRUE
);
3039 if (event_decl
->event_header_decl
)
3040 g_ptr_array_free(event_decl
->event_header_decl
, TRUE
);
3041 if (event_decl
->packet_context_decl
)
3042 g_ptr_array_free(event_decl
->packet_context_decl
, TRUE
);
3044 event
= &event_decl
->parent
;
3045 if (event
->fields_decl
)
3046 declaration_unref(&event
->fields_decl
->p
);
3047 if (event
->context_decl
)
3048 declaration_unref(&event
->context_decl
->p
);
3049 free_declaration_scope(event
->declaration_scope
);
3053 g_ptr_array_free(trace
->event_declarations
, TRUE
);
3055 if (trace
->packet_header_decl
)
3056 declaration_unref(&trace
->packet_header_decl
->p
);
3058 free_declaration_scope(trace
->root_declaration_scope
);
3059 free_declaration_scope(trace
->declaration_scope
);
3061 g_hash_table_destroy(trace
->callsites
);
3062 g_hash_table_destroy(trace
->clocks
);
3064 metadata_stream
= container_of(trace
->metadata
, struct ctf_file_stream
, parent
);
3065 g_free(metadata_stream
);