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
;
896 * For named variant (without body), lookup in
897 * declaration scope. Don't take reference on variant
898 * declaration: ref is only taken upon definition.
902 untagged_variant_declaration
=
903 lookup_variant_declaration(g_quark_from_string(name
),
905 declaration_ref(&untagged_variant_declaration
->p
);
907 /* For unnamed variant, create type */
908 /* For named variant (with body), create type and add to declaration scope */
910 if (lookup_variant_declaration(g_quark_from_string(name
),
911 declaration_scope
)) {
913 fprintf(fd
, "[error] %s: variant %s already declared in scope\n", __func__
, name
);
917 untagged_variant_declaration
= untagged_variant_declaration_new(declaration_scope
);
918 bt_list_for_each_entry(iter
, declaration_list
, siblings
) {
919 ret
= ctf_variant_declaration_list_visit(fd
, depth
+ 1, iter
,
920 untagged_variant_declaration
, trace
);
925 ret
= register_variant_declaration(g_quark_from_string(name
),
926 untagged_variant_declaration
,
932 * if tagged, create tagged variant and return. else return
936 return &untagged_variant_declaration
->p
;
938 variant_declaration
= variant_declaration_new(untagged_variant_declaration
, choice
);
939 if (!variant_declaration
)
941 declaration_unref(&untagged_variant_declaration
->p
);
942 return &variant_declaration
->p
;
945 untagged_variant_declaration
->p
.declaration_free(&untagged_variant_declaration
->p
);
950 int ctf_enumerator_list_visit(FILE *fd
, int depth
,
951 struct ctf_node
*enumerator
,
952 struct declaration_enum
*enum_declaration
,
953 struct last_enum_value
*last
)
956 struct ctf_node
*iter
;
958 q
= g_quark_from_string(enumerator
->u
.enumerator
.id
);
959 if (enum_declaration
->integer_declaration
->signedness
) {
963 bt_list_for_each_entry(iter
, &enumerator
->u
.enumerator
.values
, siblings
) {
966 assert(iter
->type
== NODE_UNARY_EXPRESSION
);
972 switch (iter
->u
.unary_expression
.type
) {
973 case UNARY_SIGNED_CONSTANT
:
974 *target
= iter
->u
.unary_expression
.u
.signed_constant
;
976 case UNARY_UNSIGNED_CONSTANT
:
977 *target
= iter
->u
.unary_expression
.u
.unsigned_constant
;
980 fprintf(fd
, "[error] %s: invalid enumerator\n", __func__
);
984 fprintf(fd
, "[error] %s: invalid enumerator\n", __func__
);
994 enum_signed_insert(enum_declaration
, start
, end
, q
);
999 bt_list_for_each_entry(iter
, &enumerator
->u
.enumerator
.values
, siblings
) {
1002 assert(iter
->type
== NODE_UNARY_EXPRESSION
);
1008 switch (iter
->u
.unary_expression
.type
) {
1009 case UNARY_UNSIGNED_CONSTANT
:
1010 *target
= iter
->u
.unary_expression
.u
.unsigned_constant
;
1012 case UNARY_SIGNED_CONSTANT
:
1014 * We don't accept signed constants for enums with unsigned
1017 fprintf(fd
, "[error] %s: invalid enumerator (signed constant encountered, but enum container type is unsigned)\n", __func__
);
1020 fprintf(fd
, "[error] %s: invalid enumerator\n", __func__
);
1024 fprintf(fd
, "[error] %s: invalid enumerator\n", __func__
);
1033 last
->u
.u
= end
+ 1;
1034 enum_unsigned_insert(enum_declaration
, start
, end
, q
);
1040 struct declaration
*ctf_declaration_enum_visit(FILE *fd
, int depth
,
1042 struct ctf_node
*container_type
,
1043 struct bt_list_head
*enumerator_list
,
1045 struct declaration_scope
*declaration_scope
,
1046 struct ctf_trace
*trace
)
1048 struct declaration
*declaration
;
1049 struct declaration_enum
*enum_declaration
;
1050 struct declaration_integer
*integer_declaration
;
1051 struct last_enum_value last_value
;
1052 struct ctf_node
*iter
;
1057 * For named enum (without body), lookup in
1058 * declaration scope. Don't take reference on enum
1059 * declaration: ref is only taken upon definition.
1064 lookup_enum_declaration(g_quark_from_string(name
),
1066 declaration_ref(&enum_declaration
->p
);
1067 return &enum_declaration
->p
;
1069 /* For unnamed enum, create type */
1070 /* For named enum (with body), create type and add to declaration scope */
1072 if (lookup_enum_declaration(g_quark_from_string(name
),
1073 declaration_scope
)) {
1075 fprintf(fd
, "[error] %s: enum %s already declared in scope\n", __func__
, name
);
1079 if (!container_type
) {
1080 declaration
= lookup_declaration(g_quark_from_static_string("int"),
1083 fprintf(fd
, "[error] %s: \"int\" type declaration missing for enumeration\n", __func__
);
1087 declaration
= ctf_type_declarator_visit(fd
, depth
,
1094 fprintf(fd
, "[error] %s: unable to create container type for enumeration\n", __func__
);
1097 if (declaration
->id
!= CTF_TYPE_INTEGER
) {
1098 fprintf(fd
, "[error] %s: container type for enumeration is not integer\n", __func__
);
1101 integer_declaration
= container_of(declaration
, struct declaration_integer
, p
);
1102 enum_declaration
= enum_declaration_new(integer_declaration
);
1103 declaration_unref(&integer_declaration
->p
); /* leave ref to enum */
1104 if (enum_declaration
->integer_declaration
->signedness
) {
1109 bt_list_for_each_entry(iter
, enumerator_list
, siblings
) {
1110 ret
= ctf_enumerator_list_visit(fd
, depth
+ 1, iter
, enum_declaration
,
1116 ret
= register_enum_declaration(g_quark_from_string(name
),
1120 declaration_unref(&enum_declaration
->p
);
1122 return &enum_declaration
->p
;
1125 enum_declaration
->p
.declaration_free(&enum_declaration
->p
);
1130 struct declaration
*ctf_declaration_type_specifier_visit(FILE *fd
, int depth
,
1131 struct ctf_node
*type_specifier_list
,
1132 struct declaration_scope
*declaration_scope
)
1135 struct declaration
*declaration
;
1140 str
= g_string_new("");
1141 ret
= visit_type_specifier_list(fd
, type_specifier_list
, str
);
1144 str_c
= g_string_free(str
, FALSE
);
1145 id_q
= g_quark_from_string(str_c
);
1147 declaration
= lookup_declaration(id_q
, declaration_scope
);
1148 declaration_ref(declaration
);
1153 * Returns 0/1 boolean, or < 0 on error.
1156 int get_boolean(FILE *fd
, int depth
, struct ctf_node
*unary_expression
)
1158 if (unary_expression
->type
!= NODE_UNARY_EXPRESSION
) {
1159 fprintf(fd
, "[error] %s: expecting unary expression\n",
1163 switch (unary_expression
->u
.unary_expression
.type
) {
1164 case UNARY_UNSIGNED_CONSTANT
:
1165 if (unary_expression
->u
.unary_expression
.u
.unsigned_constant
== 0)
1169 case UNARY_SIGNED_CONSTANT
:
1170 if (unary_expression
->u
.unary_expression
.u
.signed_constant
== 0)
1175 if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "true"))
1177 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "TRUE"))
1179 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "false"))
1181 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "FALSE"))
1184 fprintf(fd
, "[error] %s: unexpected string \"%s\"\n",
1185 __func__
, unary_expression
->u
.unary_expression
.u
.string
);
1190 fprintf(fd
, "[error] %s: unexpected unary expression type\n",
1198 int get_trace_byte_order(FILE *fd
, int depth
, struct ctf_node
*unary_expression
)
1202 if (unary_expression
->u
.unary_expression
.type
!= UNARY_STRING
) {
1203 fprintf(fd
, "[error] %s: byte_order: expecting string\n",
1207 if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "be"))
1208 byte_order
= BIG_ENDIAN
;
1209 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "le"))
1210 byte_order
= LITTLE_ENDIAN
;
1212 fprintf(fd
, "[error] %s: unexpected string \"%s\". Should be \"native\", \"network\", \"be\" or \"le\".\n",
1213 __func__
, unary_expression
->u
.unary_expression
.u
.string
);
1220 int get_byte_order(FILE *fd
, int depth
, struct ctf_node
*unary_expression
,
1221 struct ctf_trace
*trace
)
1225 if (unary_expression
->u
.unary_expression
.type
!= UNARY_STRING
) {
1226 fprintf(fd
, "[error] %s: byte_order: expecting string\n",
1230 if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "native"))
1231 byte_order
= trace
->byte_order
;
1232 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "network"))
1233 byte_order
= BIG_ENDIAN
;
1234 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "be"))
1235 byte_order
= BIG_ENDIAN
;
1236 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "le"))
1237 byte_order
= LITTLE_ENDIAN
;
1239 fprintf(fd
, "[error] %s: unexpected string \"%s\". Should be \"native\", \"network\", \"be\" or \"le\".\n",
1240 __func__
, unary_expression
->u
.unary_expression
.u
.string
);
1247 struct declaration
*ctf_declaration_integer_visit(FILE *fd
, int depth
,
1248 struct bt_list_head
*expressions
,
1249 struct ctf_trace
*trace
)
1251 struct ctf_node
*expression
;
1252 uint64_t alignment
= 1, size
= 0;
1253 int byte_order
= trace
->byte_order
;
1255 int has_alignment
= 0, has_size
= 0;
1257 enum ctf_string_encoding encoding
= CTF_STRING_NONE
;
1258 struct ctf_clock
*clock
= NULL
;
1259 struct declaration_integer
*integer_declaration
;
1261 bt_list_for_each_entry(expression
, expressions
, siblings
) {
1262 struct ctf_node
*left
, *right
;
1264 left
= _bt_list_first_entry(&expression
->u
.ctf_expression
.left
, struct ctf_node
, siblings
);
1265 right
= _bt_list_first_entry(&expression
->u
.ctf_expression
.right
, struct ctf_node
, siblings
);
1266 assert(left
->u
.unary_expression
.type
== UNARY_STRING
);
1267 if (!strcmp(left
->u
.unary_expression
.u
.string
, "signed")) {
1268 signedness
= get_boolean(fd
, depth
, right
);
1271 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "byte_order")) {
1272 byte_order
= get_byte_order(fd
, depth
, right
, trace
);
1275 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "size")) {
1276 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
1277 fprintf(fd
, "[error] %s: size: expecting unsigned constant\n",
1281 size
= right
->u
.unary_expression
.u
.unsigned_constant
;
1283 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "align")) {
1284 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
1285 fprintf(fd
, "[error] %s: align: expecting unsigned constant\n",
1289 alignment
= right
->u
.unary_expression
.u
.unsigned_constant
;
1290 /* Make sure alignment is a power of two */
1291 if (alignment
== 0 || (alignment
& (alignment
- 1)) != 0) {
1292 fprintf(fd
, "[error] %s: align: expecting power of two\n",
1297 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "base")) {
1298 switch (right
->u
.unary_expression
.type
) {
1299 case UNARY_UNSIGNED_CONSTANT
:
1300 switch (right
->u
.unary_expression
.u
.unsigned_constant
) {
1305 base
= right
->u
.unary_expression
.u
.unsigned_constant
;
1308 fprintf(fd
, "[error] %s: base not supported (%" PRIu64
")\n",
1309 __func__
, right
->u
.unary_expression
.u
.unsigned_constant
);
1315 char *s_right
= concatenate_unary_strings(&expression
->u
.ctf_expression
.right
);
1317 fprintf(fd
, "[error] %s: unexpected unary expression for integer base\n", __func__
);
1321 if (!strcmp(s_right
, "decimal") || !strcmp(s_right
, "dec") || !strcmp(s_right
, "d")
1322 || !strcmp(s_right
, "i") || !strcmp(s_right
, "u")) {
1324 } else if (!strcmp(s_right
, "hexadecimal") || !strcmp(s_right
, "hex")
1325 || !strcmp(s_right
, "x") || !strcmp(s_right
, "X")
1326 || !strcmp(s_right
, "p")) {
1328 } else if (!strcmp(s_right
, "octal") || !strcmp(s_right
, "oct")
1329 || !strcmp(s_right
, "o")) {
1331 } else if (!strcmp(s_right
, "binary") || !strcmp(s_right
, "b")) {
1334 fprintf(fd
, "[error] %s: unexpected expression for integer base (%s)\n", __func__
, s_right
);
1343 fprintf(fd
, "[error] %s: base: expecting unsigned constant or unary string\n",
1347 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "encoding")) {
1350 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
1351 fprintf(fd
, "[error] %s: encoding: expecting unary string\n",
1355 s_right
= concatenate_unary_strings(&expression
->u
.ctf_expression
.right
);
1357 fprintf(fd
, "[error] %s: unexpected unary expression for integer base\n", __func__
);
1361 if (!strcmp(s_right
, "UTF8")
1362 || !strcmp(s_right
, "utf8")
1363 || !strcmp(s_right
, "utf-8")
1364 || !strcmp(s_right
, "UTF-8"))
1365 encoding
= CTF_STRING_UTF8
;
1366 else if (!strcmp(s_right
, "ASCII")
1367 || !strcmp(s_right
, "ascii"))
1368 encoding
= CTF_STRING_ASCII
;
1369 else if (!strcmp(s_right
, "none"))
1370 encoding
= CTF_STRING_NONE
;
1372 fprintf(fd
, "[error] %s: unknown string encoding \"%s\"\n", __func__
, s_right
);
1377 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "map")) {
1380 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
1381 fprintf(fd
, "[error] %s: map: expecting identifier\n",
1385 /* currently only support clock.name.value */
1386 clock_name
= get_map_clock_name_value(&expression
->u
.ctf_expression
.right
);
1390 s_right
= concatenate_unary_strings(&expression
->u
.ctf_expression
.right
);
1392 fprintf(fd
, "[error] %s: unexpected unary expression for integer map\n", __func__
);
1396 fprintf(fd
, "[warning] %s: unknown map %s in integer declaration\n", __func__
,
1401 clock
= trace_clock_lookup(trace
, clock_name
);
1403 fprintf(fd
, "[error] %s: map: unable to find clock %s declaration\n",
1404 __func__
, g_quark_to_string(clock_name
));
1408 fprintf(fd
, "[warning] %s: unknown attribute name %s\n",
1409 __func__
, left
->u
.unary_expression
.u
.string
);
1410 /* Fall-through after warning */
1414 fprintf(fd
, "[error] %s: missing size attribute\n", __func__
);
1417 if (!has_alignment
) {
1418 if (size
% CHAR_BIT
) {
1419 /* bit-packed alignment */
1422 /* byte-packed alignment */
1423 alignment
= CHAR_BIT
;
1426 integer_declaration
= integer_declaration_new(size
,
1427 byte_order
, signedness
, alignment
,
1428 base
, encoding
, clock
);
1429 return &integer_declaration
->p
;
1433 struct declaration
*ctf_declaration_floating_point_visit(FILE *fd
, int depth
,
1434 struct bt_list_head
*expressions
,
1435 struct ctf_trace
*trace
)
1437 struct ctf_node
*expression
;
1438 uint64_t alignment
= 1, exp_dig
= 0, mant_dig
= 0,
1439 byte_order
= trace
->byte_order
;
1440 int has_alignment
= 0, has_exp_dig
= 0, has_mant_dig
= 0;
1441 struct declaration_float
*float_declaration
;
1443 bt_list_for_each_entry(expression
, expressions
, siblings
) {
1444 struct ctf_node
*left
, *right
;
1446 left
= _bt_list_first_entry(&expression
->u
.ctf_expression
.left
, struct ctf_node
, siblings
);
1447 right
= _bt_list_first_entry(&expression
->u
.ctf_expression
.right
, struct ctf_node
, siblings
);
1448 assert(left
->u
.unary_expression
.type
== UNARY_STRING
);
1449 if (!strcmp(left
->u
.unary_expression
.u
.string
, "byte_order")) {
1450 byte_order
= get_byte_order(fd
, depth
, right
, trace
);
1453 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "exp_dig")) {
1454 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
1455 fprintf(fd
, "[error] %s: exp_dig: expecting unsigned constant\n",
1459 exp_dig
= right
->u
.unary_expression
.u
.unsigned_constant
;
1461 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "mant_dig")) {
1462 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
1463 fprintf(fd
, "[error] %s: mant_dig: expecting unsigned constant\n",
1467 mant_dig
= right
->u
.unary_expression
.u
.unsigned_constant
;
1469 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "align")) {
1470 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
1471 fprintf(fd
, "[error] %s: align: expecting unsigned constant\n",
1475 alignment
= right
->u
.unary_expression
.u
.unsigned_constant
;
1476 /* Make sure alignment is a power of two */
1477 if (alignment
== 0 || (alignment
& (alignment
- 1)) != 0) {
1478 fprintf(fd
, "[error] %s: align: expecting power of two\n",
1484 fprintf(fd
, "[warning] %s: unknown attribute name %s\n",
1485 __func__
, left
->u
.unary_expression
.u
.string
);
1486 /* Fall-through after warning */
1489 if (!has_mant_dig
) {
1490 fprintf(fd
, "[error] %s: missing mant_dig attribute\n", __func__
);
1494 fprintf(fd
, "[error] %s: missing exp_dig attribute\n", __func__
);
1497 if (!has_alignment
) {
1498 if ((mant_dig
+ exp_dig
) % CHAR_BIT
) {
1499 /* bit-packed alignment */
1502 /* byte-packed alignment */
1503 alignment
= CHAR_BIT
;
1506 float_declaration
= float_declaration_new(mant_dig
, exp_dig
,
1507 byte_order
, alignment
);
1508 return &float_declaration
->p
;
1512 struct declaration
*ctf_declaration_string_visit(FILE *fd
, int depth
,
1513 struct bt_list_head
*expressions
,
1514 struct ctf_trace
*trace
)
1516 struct ctf_node
*expression
;
1517 const char *encoding_c
= NULL
;
1518 enum ctf_string_encoding encoding
= CTF_STRING_UTF8
;
1519 struct declaration_string
*string_declaration
;
1521 bt_list_for_each_entry(expression
, expressions
, siblings
) {
1522 struct ctf_node
*left
, *right
;
1524 left
= _bt_list_first_entry(&expression
->u
.ctf_expression
.left
, struct ctf_node
, siblings
);
1525 right
= _bt_list_first_entry(&expression
->u
.ctf_expression
.right
, struct ctf_node
, siblings
);
1526 assert(left
->u
.unary_expression
.type
== UNARY_STRING
);
1527 if (!strcmp(left
->u
.unary_expression
.u
.string
, "encoding")) {
1528 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
1529 fprintf(fd
, "[error] %s: encoding: expecting string\n",
1533 encoding_c
= right
->u
.unary_expression
.u
.string
;
1535 fprintf(fd
, "[warning] %s: unknown attribute name %s\n",
1536 __func__
, left
->u
.unary_expression
.u
.string
);
1537 /* Fall-through after warning */
1540 if (encoding_c
&& !strcmp(encoding_c
, "ASCII"))
1541 encoding
= CTF_STRING_ASCII
;
1542 string_declaration
= string_declaration_new(encoding
);
1543 return &string_declaration
->p
;
1548 struct declaration
*ctf_type_specifier_list_visit(FILE *fd
,
1549 int depth
, struct ctf_node
*type_specifier_list
,
1550 struct declaration_scope
*declaration_scope
,
1551 struct ctf_trace
*trace
)
1553 struct ctf_node
*first
;
1554 struct ctf_node
*node
;
1556 assert(type_specifier_list
->type
== NODE_TYPE_SPECIFIER_LIST
);
1558 first
= _bt_list_first_entry(&type_specifier_list
->u
.type_specifier_list
.head
, struct ctf_node
, siblings
);
1560 assert(first
->type
== NODE_TYPE_SPECIFIER
);
1562 node
= first
->u
.type_specifier
.node
;
1564 switch (first
->u
.type_specifier
.type
) {
1565 case TYPESPEC_FLOATING_POINT
:
1566 return ctf_declaration_floating_point_visit(fd
, depth
,
1567 &node
->u
.floating_point
.expressions
, trace
);
1568 case TYPESPEC_INTEGER
:
1569 return ctf_declaration_integer_visit(fd
, depth
,
1570 &node
->u
.integer
.expressions
, trace
);
1571 case TYPESPEC_STRING
:
1572 return ctf_declaration_string_visit(fd
, depth
,
1573 &node
->u
.string
.expressions
, trace
);
1574 case TYPESPEC_STRUCT
:
1575 return ctf_declaration_struct_visit(fd
, depth
,
1576 node
->u
._struct
.name
,
1577 &node
->u
._struct
.declaration_list
,
1578 node
->u
._struct
.has_body
,
1579 &node
->u
._struct
.min_align
,
1582 case TYPESPEC_VARIANT
:
1583 return ctf_declaration_variant_visit(fd
, depth
,
1584 node
->u
.variant
.name
,
1585 node
->u
.variant
.choice
,
1586 &node
->u
.variant
.declaration_list
,
1587 node
->u
.variant
.has_body
,
1591 return ctf_declaration_enum_visit(fd
, depth
,
1592 node
->u
._enum
.enum_id
,
1593 node
->u
._enum
.container_type
,
1594 &node
->u
._enum
.enumerator_list
,
1595 node
->u
._enum
.has_body
,
1601 case TYPESPEC_SHORT
:
1604 case TYPESPEC_FLOAT
:
1605 case TYPESPEC_DOUBLE
:
1606 case TYPESPEC_SIGNED
:
1607 case TYPESPEC_UNSIGNED
:
1609 case TYPESPEC_COMPLEX
:
1610 case TYPESPEC_IMAGINARY
:
1611 case TYPESPEC_CONST
:
1612 case TYPESPEC_ID_TYPE
:
1613 return ctf_declaration_type_specifier_visit(fd
, depth
,
1614 type_specifier_list
, declaration_scope
);
1616 fprintf(fd
, "[error] %s: unexpected node type %d\n", __func__
, (int) first
->u
.type_specifier
.type
);
1622 int ctf_event_declaration_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_event_declaration
*event
, struct ctf_trace
*trace
)
1626 switch (node
->type
) {
1628 ret
= ctf_typedef_visit(fd
, depth
+ 1,
1629 event
->declaration_scope
,
1630 node
->u
._typedef
.type_specifier_list
,
1631 &node
->u
._typedef
.type_declarators
,
1636 case NODE_TYPEALIAS
:
1637 ret
= ctf_typealias_visit(fd
, depth
+ 1,
1638 event
->declaration_scope
,
1639 node
->u
.typealias
.target
, node
->u
.typealias
.alias
,
1644 case NODE_CTF_EXPRESSION
:
1648 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
1649 if (!strcmp(left
, "name")) {
1652 if (CTF_EVENT_FIELD_IS_SET(event
, name
)) {
1653 fprintf(fd
, "[error] %s: name already declared in event declaration\n", __func__
);
1657 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
1659 fprintf(fd
, "[error] %s: unexpected unary expression for event name\n", __func__
);
1663 event
->name
= g_quark_from_string(right
);
1665 CTF_EVENT_SET_FIELD(event
, name
);
1666 } else if (!strcmp(left
, "id")) {
1667 if (CTF_EVENT_FIELD_IS_SET(event
, id
)) {
1668 fprintf(fd
, "[error] %s: id already declared in event declaration\n", __func__
);
1672 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &event
->id
);
1674 fprintf(fd
, "[error] %s: unexpected unary expression for event id\n", __func__
);
1678 CTF_EVENT_SET_FIELD(event
, id
);
1679 } else if (!strcmp(left
, "stream_id")) {
1680 if (CTF_EVENT_FIELD_IS_SET(event
, stream_id
)) {
1681 fprintf(fd
, "[error] %s: stream_id already declared in event declaration\n", __func__
);
1685 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &event
->stream_id
);
1687 fprintf(fd
, "[error] %s: unexpected unary expression for event stream_id\n", __func__
);
1691 event
->stream
= trace_stream_lookup(trace
, event
->stream_id
);
1692 if (!event
->stream
) {
1693 fprintf(fd
, "[error] %s: stream id %" PRIu64
" cannot be found\n", __func__
, event
->stream_id
);
1697 CTF_EVENT_SET_FIELD(event
, stream_id
);
1698 } else if (!strcmp(left
, "context")) {
1699 struct declaration
*declaration
;
1701 if (event
->context_decl
) {
1702 fprintf(fd
, "[error] %s: context already declared in event declaration\n", __func__
);
1706 declaration
= ctf_type_specifier_list_visit(fd
, depth
,
1707 _bt_list_first_entry(&node
->u
.ctf_expression
.right
,
1708 struct ctf_node
, siblings
),
1709 event
->declaration_scope
, trace
);
1714 if (declaration
->id
!= CTF_TYPE_STRUCT
) {
1718 event
->context_decl
= container_of(declaration
, struct declaration_struct
, p
);
1719 } else if (!strcmp(left
, "fields")) {
1720 struct declaration
*declaration
;
1722 if (event
->fields_decl
) {
1723 fprintf(fd
, "[error] %s: fields already declared in event declaration\n", __func__
);
1727 declaration
= ctf_type_specifier_list_visit(fd
, depth
,
1728 _bt_list_first_entry(&node
->u
.ctf_expression
.right
,
1729 struct ctf_node
, siblings
),
1730 event
->declaration_scope
, trace
);
1735 if (declaration
->id
!= CTF_TYPE_STRUCT
) {
1739 event
->fields_decl
= container_of(declaration
, struct declaration_struct
, p
);
1740 } else if (!strcmp(left
, "loglevel")) {
1741 int64_t loglevel
= -1;
1743 if (CTF_EVENT_FIELD_IS_SET(event
, loglevel
)) {
1744 fprintf(fd
, "[error] %s: loglevel already declared in event declaration\n", __func__
);
1748 ret
= get_unary_signed(&node
->u
.ctf_expression
.right
, &loglevel
);
1750 fprintf(fd
, "[error] %s: unexpected unary expression for event loglevel\n", __func__
);
1754 event
->loglevel
= (int) loglevel
;
1755 CTF_EVENT_SET_FIELD(event
, loglevel
);
1756 } else if (!strcmp(left
, "model.emf.uri")) {
1759 if (CTF_EVENT_FIELD_IS_SET(event
, model_emf_uri
)) {
1760 fprintf(fd
, "[error] %s: model.emf.uri already declared in event declaration\n", __func__
);
1764 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
1766 fprintf(fd
, "[error] %s: unexpected unary expression for event model.emf.uri\n", __func__
);
1770 event
->model_emf_uri
= g_quark_from_string(right
);
1772 CTF_EVENT_SET_FIELD(event
, model_emf_uri
);
1774 fprintf(fd
, "[warning] %s: attribute \"%s\" is unknown in event declaration.\n", __func__
, left
);
1775 /* Fall-through after warning */
1783 /* TODO: declaration specifier should be added. */
1790 int ctf_event_visit(FILE *fd
, int depth
, struct ctf_node
*node
,
1791 struct declaration_scope
*parent_declaration_scope
, struct ctf_trace
*trace
)
1794 struct ctf_node
*iter
;
1795 struct ctf_event_declaration
*event
;
1796 struct bt_ctf_event_decl
*event_decl
;
1798 event_decl
= g_new0(struct bt_ctf_event_decl
, 1);
1799 event
= &event_decl
->parent
;
1800 event
->declaration_scope
= new_declaration_scope(parent_declaration_scope
);
1801 event
->loglevel
= -1;
1802 bt_list_for_each_entry(iter
, &node
->u
.event
.declaration_list
, siblings
) {
1803 ret
= ctf_event_declaration_visit(fd
, depth
+ 1, iter
, event
, trace
);
1807 if (!CTF_EVENT_FIELD_IS_SET(event
, name
)) {
1809 fprintf(fd
, "[error] %s: missing name field in event declaration\n", __func__
);
1812 if (!CTF_EVENT_FIELD_IS_SET(event
, stream_id
)) {
1813 /* Allow missing stream_id if there is only a single stream */
1814 switch (trace
->streams
->len
) {
1815 case 0: /* Create stream if there was none. */
1816 ret
= ctf_stream_visit(fd
, depth
, NULL
, trace
->root_declaration_scope
, trace
);
1821 event
->stream_id
= 0;
1822 event
->stream
= trace_stream_lookup(trace
, event
->stream_id
);
1826 fprintf(fd
, "[error] %s: missing stream_id field in event declaration\n", __func__
);
1830 /* Allow only one event without id per stream */
1831 if (!CTF_EVENT_FIELD_IS_SET(event
, id
)
1832 && event
->stream
->events_by_id
->len
!= 0) {
1834 fprintf(fd
, "[error] %s: missing id field in event declaration\n", __func__
);
1837 if (event
->stream
->events_by_id
->len
<= event
->id
)
1838 g_ptr_array_set_size(event
->stream
->events_by_id
, event
->id
+ 1);
1839 g_ptr_array_index(event
->stream
->events_by_id
, event
->id
) = event
;
1840 g_hash_table_insert(event
->stream
->event_quark_to_id
,
1841 (gpointer
) (unsigned long) event
->name
,
1843 g_ptr_array_add(trace
->event_declarations
, event_decl
);
1847 if (event
->fields_decl
)
1848 declaration_unref(&event
->fields_decl
->p
);
1849 if (event
->context_decl
)
1850 declaration_unref(&event
->context_decl
->p
);
1851 free_declaration_scope(event
->declaration_scope
);
1858 int ctf_stream_declaration_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_stream_declaration
*stream
, struct ctf_trace
*trace
)
1862 switch (node
->type
) {
1864 ret
= ctf_typedef_visit(fd
, depth
+ 1,
1865 stream
->declaration_scope
,
1866 node
->u
._typedef
.type_specifier_list
,
1867 &node
->u
._typedef
.type_declarators
,
1872 case NODE_TYPEALIAS
:
1873 ret
= ctf_typealias_visit(fd
, depth
+ 1,
1874 stream
->declaration_scope
,
1875 node
->u
.typealias
.target
, node
->u
.typealias
.alias
,
1880 case NODE_CTF_EXPRESSION
:
1884 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
1885 if (!strcmp(left
, "id")) {
1886 if (CTF_STREAM_FIELD_IS_SET(stream
, stream_id
)) {
1887 fprintf(fd
, "[error] %s: id already declared in stream declaration\n", __func__
);
1891 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &stream
->stream_id
);
1893 fprintf(fd
, "[error] %s: unexpected unary expression for stream id\n", __func__
);
1897 CTF_STREAM_SET_FIELD(stream
, stream_id
);
1898 } else if (!strcmp(left
, "event.header")) {
1899 struct declaration
*declaration
;
1901 if (stream
->event_header_decl
) {
1902 fprintf(fd
, "[error] %s: event.header already declared in stream declaration\n", __func__
);
1906 declaration
= ctf_type_specifier_list_visit(fd
, depth
,
1907 _bt_list_first_entry(&node
->u
.ctf_expression
.right
,
1908 struct ctf_node
, siblings
),
1909 stream
->declaration_scope
, trace
);
1914 if (declaration
->id
!= CTF_TYPE_STRUCT
) {
1918 stream
->event_header_decl
= container_of(declaration
, struct declaration_struct
, p
);
1919 } else if (!strcmp(left
, "event.context")) {
1920 struct declaration
*declaration
;
1922 if (stream
->event_context_decl
) {
1923 fprintf(fd
, "[error] %s: event.context already declared in stream declaration\n", __func__
);
1927 declaration
= ctf_type_specifier_list_visit(fd
, depth
,
1928 _bt_list_first_entry(&node
->u
.ctf_expression
.right
,
1929 struct ctf_node
, siblings
),
1930 stream
->declaration_scope
, trace
);
1935 if (declaration
->id
!= CTF_TYPE_STRUCT
) {
1939 stream
->event_context_decl
= container_of(declaration
, struct declaration_struct
, p
);
1940 } else if (!strcmp(left
, "packet.context")) {
1941 struct declaration
*declaration
;
1943 if (stream
->packet_context_decl
) {
1944 fprintf(fd
, "[error] %s: packet.context already declared in stream declaration\n", __func__
);
1948 declaration
= ctf_type_specifier_list_visit(fd
, depth
,
1949 _bt_list_first_entry(&node
->u
.ctf_expression
.right
,
1950 struct ctf_node
, siblings
),
1951 stream
->declaration_scope
, trace
);
1956 if (declaration
->id
!= CTF_TYPE_STRUCT
) {
1960 stream
->packet_context_decl
= container_of(declaration
, struct declaration_struct
, p
);
1962 fprintf(fd
, "[warning] %s: attribute \"%s\" is unknown in stream declaration.\n", __func__
, left
);
1963 /* Fall-through after warning */
1972 /* TODO: declaration specifier should be added. */
1979 int ctf_stream_visit(FILE *fd
, int depth
, struct ctf_node
*node
,
1980 struct declaration_scope
*parent_declaration_scope
, struct ctf_trace
*trace
)
1983 struct ctf_node
*iter
;
1984 struct ctf_stream_declaration
*stream
;
1986 stream
= g_new0(struct ctf_stream_declaration
, 1);
1987 stream
->declaration_scope
= new_declaration_scope(parent_declaration_scope
);
1988 stream
->events_by_id
= g_ptr_array_new();
1989 stream
->event_quark_to_id
= g_hash_table_new(g_direct_hash
, g_direct_equal
);
1990 stream
->streams
= g_ptr_array_new();
1992 bt_list_for_each_entry(iter
, &node
->u
.stream
.declaration_list
, siblings
) {
1993 ret
= ctf_stream_declaration_visit(fd
, depth
+ 1, iter
, stream
, trace
);
1998 if (CTF_STREAM_FIELD_IS_SET(stream
, stream_id
)) {
1999 /* check that packet header has stream_id field. */
2000 if (!trace
->packet_header_decl
2001 || struct_declaration_lookup_field_index(trace
->packet_header_decl
, g_quark_from_static_string("stream_id")) < 0) {
2003 fprintf(fd
, "[error] %s: missing stream_id field in packet header declaration, but stream_id attribute is declared for stream.\n", __func__
);
2007 /* Allow only one id-less stream */
2008 if (trace
->streams
->len
!= 0) {
2010 fprintf(fd
, "[error] %s: missing id field in stream declaration\n", __func__
);
2013 stream
->stream_id
= 0;
2015 if (trace
->streams
->len
<= stream
->stream_id
)
2016 g_ptr_array_set_size(trace
->streams
, stream
->stream_id
+ 1);
2017 g_ptr_array_index(trace
->streams
, stream
->stream_id
) = stream
;
2018 stream
->trace
= trace
;
2023 if (stream
->event_header_decl
)
2024 declaration_unref(&stream
->event_header_decl
->p
);
2025 if (stream
->event_context_decl
)
2026 declaration_unref(&stream
->event_context_decl
->p
);
2027 if (stream
->packet_context_decl
)
2028 declaration_unref(&stream
->packet_context_decl
->p
);
2029 g_ptr_array_free(stream
->streams
, TRUE
);
2030 g_ptr_array_free(stream
->events_by_id
, TRUE
);
2031 g_hash_table_destroy(stream
->event_quark_to_id
);
2032 free_declaration_scope(stream
->declaration_scope
);
2038 int ctf_trace_declaration_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_trace
*trace
)
2042 switch (node
->type
) {
2044 ret
= ctf_typedef_visit(fd
, depth
+ 1,
2045 trace
->declaration_scope
,
2046 node
->u
._typedef
.type_specifier_list
,
2047 &node
->u
._typedef
.type_declarators
,
2052 case NODE_TYPEALIAS
:
2053 ret
= ctf_typealias_visit(fd
, depth
+ 1,
2054 trace
->declaration_scope
,
2055 node
->u
.typealias
.target
, node
->u
.typealias
.alias
,
2060 case NODE_CTF_EXPRESSION
:
2064 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
2065 if (!strcmp(left
, "major")) {
2066 if (CTF_TRACE_FIELD_IS_SET(trace
, major
)) {
2067 fprintf(fd
, "[error] %s: major already declared in trace declaration\n", __func__
);
2071 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &trace
->major
);
2073 fprintf(fd
, "[error] %s: unexpected unary expression for trace major number\n", __func__
);
2077 CTF_TRACE_SET_FIELD(trace
, major
);
2078 } else if (!strcmp(left
, "minor")) {
2079 if (CTF_TRACE_FIELD_IS_SET(trace
, minor
)) {
2080 fprintf(fd
, "[error] %s: minor already declared in trace declaration\n", __func__
);
2084 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &trace
->minor
);
2086 fprintf(fd
, "[error] %s: unexpected unary expression for trace minor number\n", __func__
);
2090 CTF_TRACE_SET_FIELD(trace
, minor
);
2091 } else if (!strcmp(left
, "uuid")) {
2092 unsigned char uuid
[BABELTRACE_UUID_LEN
];
2094 ret
= get_unary_uuid(&node
->u
.ctf_expression
.right
, uuid
);
2096 fprintf(fd
, "[error] %s: unexpected unary expression for trace uuid\n", __func__
);
2100 if (CTF_TRACE_FIELD_IS_SET(trace
, uuid
)
2101 && babeltrace_uuid_compare(uuid
, trace
->uuid
)) {
2102 fprintf(fd
, "[error] %s: uuid mismatch\n", __func__
);
2106 memcpy(trace
->uuid
, uuid
, sizeof(uuid
));
2108 CTF_TRACE_SET_FIELD(trace
, uuid
);
2109 } else if (!strcmp(left
, "byte_order")) {
2110 struct ctf_node
*right
;
2113 right
= _bt_list_first_entry(&node
->u
.ctf_expression
.right
, struct ctf_node
, siblings
);
2114 byte_order
= get_trace_byte_order(fd
, depth
, right
);
2118 if (CTF_TRACE_FIELD_IS_SET(trace
, byte_order
)
2119 && byte_order
!= trace
->byte_order
) {
2120 fprintf(fd
, "[error] %s: endianness mismatch\n", __func__
);
2124 if (byte_order
!= trace
->byte_order
) {
2125 trace
->byte_order
= byte_order
;
2127 * We need to restart
2128 * construction of the
2129 * intermediate representation.
2131 trace
->field_mask
= 0;
2132 CTF_TRACE_SET_FIELD(trace
, byte_order
);
2137 CTF_TRACE_SET_FIELD(trace
, byte_order
);
2138 } else if (!strcmp(left
, "packet.header")) {
2139 struct declaration
*declaration
;
2141 if (trace
->packet_header_decl
) {
2142 fprintf(fd
, "[error] %s: packet.header already declared in trace declaration\n", __func__
);
2146 declaration
= ctf_type_specifier_list_visit(fd
, depth
,
2147 _bt_list_first_entry(&node
->u
.ctf_expression
.right
,
2148 struct ctf_node
, siblings
),
2149 trace
->declaration_scope
, trace
);
2154 if (declaration
->id
!= CTF_TYPE_STRUCT
) {
2158 trace
->packet_header_decl
= container_of(declaration
, struct declaration_struct
, p
);
2160 fprintf(fd
, "[warning] %s: attribute \"%s\" is unknown in trace declaration.\n", __func__
, left
);
2169 /* TODO: declaration specifier should be added. */
2176 int ctf_trace_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_trace
*trace
)
2179 struct ctf_node
*iter
;
2181 if (trace
->declaration_scope
)
2183 trace
->declaration_scope
= new_declaration_scope(trace
->root_declaration_scope
);
2184 trace
->streams
= g_ptr_array_new();
2185 trace
->event_declarations
= g_ptr_array_new();
2186 bt_list_for_each_entry(iter
, &node
->u
.trace
.declaration_list
, siblings
) {
2187 ret
= ctf_trace_declaration_visit(fd
, depth
+ 1, iter
, trace
);
2191 if (!CTF_TRACE_FIELD_IS_SET(trace
, major
)) {
2193 fprintf(fd
, "[error] %s: missing major field in trace declaration\n", __func__
);
2196 if (!CTF_TRACE_FIELD_IS_SET(trace
, minor
)) {
2198 fprintf(fd
, "[error] %s: missing minor field in trace declaration\n", __func__
);
2201 if (!CTF_TRACE_FIELD_IS_SET(trace
, byte_order
)) {
2203 fprintf(fd
, "[error] %s: missing byte_order field in trace declaration\n", __func__
);
2207 if (!CTF_TRACE_FIELD_IS_SET(trace
, byte_order
)) {
2208 /* check that the packet header contains a "magic" field */
2209 if (!trace
->packet_header_decl
2210 || struct_declaration_lookup_field_index(trace
->packet_header_decl
, g_quark_from_static_string("magic")) < 0) {
2212 fprintf(fd
, "[error] %s: missing both byte_order and packet header magic number in trace declaration\n", __func__
);
2219 if (trace
->packet_header_decl
) {
2220 declaration_unref(&trace
->packet_header_decl
->p
);
2221 trace
->packet_header_decl
= NULL
;
2223 g_ptr_array_free(trace
->streams
, TRUE
);
2224 g_ptr_array_free(trace
->event_declarations
, TRUE
);
2225 free_declaration_scope(trace
->declaration_scope
);
2226 trace
->declaration_scope
= NULL
;
2231 int ctf_clock_declaration_visit(FILE *fd
, int depth
, struct ctf_node
*node
,
2232 struct ctf_clock
*clock
, struct ctf_trace
*trace
)
2236 switch (node
->type
) {
2237 case NODE_CTF_EXPRESSION
:
2241 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
2242 if (!strcmp(left
, "name")) {
2245 if (CTF_CLOCK_FIELD_IS_SET(clock
, name
)) {
2246 fprintf(fd
, "[error] %s: name already declared in clock declaration\n", __func__
);
2250 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
2252 fprintf(fd
, "[error] %s: unexpected unary expression for clock name\n", __func__
);
2256 clock
->name
= g_quark_from_string(right
);
2258 CTF_CLOCK_SET_FIELD(clock
, name
);
2259 } else if (!strcmp(left
, "uuid")) {
2263 fprintf(fd
, "[error] %s: uuid already declared in clock declaration\n", __func__
);
2267 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
2269 fprintf(fd
, "[error] %s: unexpected unary expression for clock uuid\n", __func__
);
2273 clock
->uuid
= g_quark_from_string(right
);
2275 } else if (!strcmp(left
, "description")) {
2278 if (clock
->description
) {
2279 fprintf(fd
, "[warning] %s: duplicated clock description\n", __func__
);
2280 goto error
; /* ret is 0, so not an actual error, just warn. */
2282 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
2284 fprintf(fd
, "[warning] %s: unexpected unary expression for clock description\n", __func__
);
2285 goto error
; /* ret is 0, so not an actual error, just warn. */
2287 clock
->description
= right
;
2288 } else if (!strcmp(left
, "freq")) {
2289 if (CTF_CLOCK_FIELD_IS_SET(clock
, freq
)) {
2290 fprintf(fd
, "[error] %s: freq already declared in clock declaration\n", __func__
);
2294 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &clock
->freq
);
2296 fprintf(fd
, "[error] %s: unexpected unary expression for clock freq\n", __func__
);
2300 CTF_CLOCK_SET_FIELD(clock
, freq
);
2301 } else if (!strcmp(left
, "precision")) {
2302 if (clock
->precision
) {
2303 fprintf(fd
, "[error] %s: precision already declared in clock declaration\n", __func__
);
2307 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &clock
->precision
);
2309 fprintf(fd
, "[error] %s: unexpected unary expression for clock precision\n", __func__
);
2313 } else if (!strcmp(left
, "offset_s")) {
2314 if (clock
->offset_s
) {
2315 fprintf(fd
, "[error] %s: offset_s already declared in clock declaration\n", __func__
);
2319 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &clock
->offset_s
);
2321 fprintf(fd
, "[error] %s: unexpected unary expression for clock offset_s\n", __func__
);
2325 } else if (!strcmp(left
, "offset")) {
2326 if (clock
->offset
) {
2327 fprintf(fd
, "[error] %s: offset already declared in clock declaration\n", __func__
);
2331 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &clock
->offset
);
2333 fprintf(fd
, "[error] %s: unexpected unary expression for clock offset\n", __func__
);
2337 } else if (!strcmp(left
, "absolute")) {
2338 struct ctf_node
*right
;
2340 right
= _bt_list_first_entry(&node
->u
.ctf_expression
.right
, struct ctf_node
, siblings
);
2341 ret
= get_boolean(fd
, depth
, right
);
2343 fprintf(fd
, "[error] %s: unexpected \"absolute\" right member\n", __func__
);
2347 clock
->absolute
= ret
;
2349 fprintf(fd
, "[warning] %s: attribute \"%s\" is unknown in clock declaration.\n", __func__
, left
);
2358 /* TODO: declaration specifier should be added. */
2365 int ctf_clock_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_trace
*trace
)
2368 struct ctf_node
*iter
;
2369 struct ctf_clock
*clock
;
2371 clock
= g_new0(struct ctf_clock
, 1);
2372 /* Default clock frequency is set to 1000000000 */
2373 clock
->freq
= 1000000000ULL;
2374 bt_list_for_each_entry(iter
, &node
->u
.clock
.declaration_list
, siblings
) {
2375 ret
= ctf_clock_declaration_visit(fd
, depth
+ 1, iter
, clock
, trace
);
2379 if (opt_clock_force_correlate
) {
2381 * User requested to forcibly correlate the clock
2382 * sources, even if we have no correlatation
2385 if (!clock
->absolute
) {
2386 fprintf(fd
, "[warning] Forcibly correlating trace clock sources (--clock-force-correlate).\n");
2388 clock
->absolute
= 1;
2390 if (!CTF_CLOCK_FIELD_IS_SET(clock
, name
)) {
2392 fprintf(fd
, "[error] %s: missing name field in clock declaration\n", __func__
);
2395 if (g_hash_table_size(trace
->clocks
) > 0) {
2396 fprintf(fd
, "[error] Only CTF traces with a single clock description are supported by this babeltrace version.\n");
2400 trace
->single_clock
= clock
;
2401 g_hash_table_insert(trace
->clocks
, (gpointer
) (unsigned long) clock
->name
, clock
);
2405 g_free(clock
->description
);
2411 void ctf_clock_default(FILE *fd
, int depth
, struct ctf_trace
*trace
)
2413 struct ctf_clock
*clock
;
2415 clock
= g_new0(struct ctf_clock
, 1);
2416 clock
->name
= g_quark_from_string("monotonic");
2418 clock
->description
= g_strdup("Default clock");
2419 /* Default clock frequency is set to 1000000000 */
2420 clock
->freq
= 1000000000ULL;
2421 if (opt_clock_force_correlate
) {
2423 * User requested to forcibly correlate the clock
2424 * sources, even if we have no correlatation
2427 if (!clock
->absolute
) {
2428 fprintf(fd
, "[warning] Forcibly correlating trace clock sources (--clock-force-correlate).\n");
2430 clock
->absolute
= 1;
2432 clock
->absolute
= 0; /* Not an absolute reference across traces */
2435 trace
->single_clock
= clock
;
2436 g_hash_table_insert(trace
->clocks
, (gpointer
) (unsigned long) clock
->name
, clock
);
2440 void clock_free(gpointer data
)
2442 struct ctf_clock
*clock
= data
;
2444 g_free(clock
->description
);
2449 int ctf_callsite_declaration_visit(FILE *fd
, int depth
, struct ctf_node
*node
,
2450 struct ctf_callsite
*callsite
, struct ctf_trace
*trace
)
2454 switch (node
->type
) {
2455 case NODE_CTF_EXPRESSION
:
2459 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
2460 if (!strcmp(left
, "name")) {
2463 if (CTF_CALLSITE_FIELD_IS_SET(callsite
, name
)) {
2464 fprintf(fd
, "[error] %s: name already declared in callsite declaration\n", __func__
);
2468 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
2470 fprintf(fd
, "[error] %s: unexpected unary expression for callsite name\n", __func__
);
2474 callsite
->name
= g_quark_from_string(right
);
2476 CTF_CALLSITE_SET_FIELD(callsite
, name
);
2477 } else if (!strcmp(left
, "func")) {
2480 if (CTF_CALLSITE_FIELD_IS_SET(callsite
, func
)) {
2481 fprintf(fd
, "[error] %s: func already declared in callsite declaration\n", __func__
);
2485 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
2487 fprintf(fd
, "[error] %s: unexpected unary expression for callsite func\n", __func__
);
2491 callsite
->func
= right
;
2492 CTF_CALLSITE_SET_FIELD(callsite
, func
);
2493 } else if (!strcmp(left
, "file")) {
2496 if (CTF_CALLSITE_FIELD_IS_SET(callsite
, file
)) {
2497 fprintf(fd
, "[error] %s: file already declared in callsite declaration\n", __func__
);
2501 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
2503 fprintf(fd
, "[error] %s: unexpected unary expression for callsite file\n", __func__
);
2507 callsite
->file
= right
;
2508 CTF_CALLSITE_SET_FIELD(callsite
, file
);
2509 } else if (!strcmp(left
, "line")) {
2510 if (CTF_CALLSITE_FIELD_IS_SET(callsite
, line
)) {
2511 fprintf(fd
, "[error] %s: line already declared in callsite declaration\n", __func__
);
2515 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &callsite
->line
);
2517 fprintf(fd
, "[error] %s: unexpected unary expression for callsite line\n", __func__
);
2521 CTF_CALLSITE_SET_FIELD(callsite
, line
);
2522 } else if (!strcmp(left
, "ip")) {
2523 if (CTF_CALLSITE_FIELD_IS_SET(callsite
, ip
)) {
2524 fprintf(fd
, "[error] %s: ip already declared in callsite declaration\n", __func__
);
2528 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &callsite
->ip
);
2530 fprintf(fd
, "[error] %s: unexpected unary expression for callsite ip\n", __func__
);
2534 CTF_CALLSITE_SET_FIELD(callsite
, ip
);
2536 fprintf(fd
, "[warning] %s: attribute \"%s\" is unknown in callsite declaration.\n", __func__
, left
);
2545 /* TODO: declaration specifier should be added. */
2552 int ctf_callsite_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_trace
*trace
)
2555 struct ctf_node
*iter
;
2556 struct ctf_callsite
*callsite
;
2557 struct ctf_callsite_dups
*cs_dups
;
2559 callsite
= g_new0(struct ctf_callsite
, 1);
2560 bt_list_for_each_entry(iter
, &node
->u
.callsite
.declaration_list
, siblings
) {
2561 ret
= ctf_callsite_declaration_visit(fd
, depth
+ 1, iter
, callsite
, trace
);
2565 if (!CTF_CALLSITE_FIELD_IS_SET(callsite
, name
)) {
2567 fprintf(fd
, "[error] %s: missing name field in callsite declaration\n", __func__
);
2570 if (!CTF_CALLSITE_FIELD_IS_SET(callsite
, func
)) {
2572 fprintf(fd
, "[error] %s: missing func field in callsite declaration\n", __func__
);
2575 if (!CTF_CALLSITE_FIELD_IS_SET(callsite
, file
)) {
2577 fprintf(fd
, "[error] %s: missing file field in callsite declaration\n", __func__
);
2580 if (!CTF_CALLSITE_FIELD_IS_SET(callsite
, line
)) {
2582 fprintf(fd
, "[error] %s: missing line field in callsite declaration\n", __func__
);
2586 cs_dups
= g_hash_table_lookup(trace
->callsites
,
2587 (gpointer
) (unsigned long) callsite
->name
);
2589 cs_dups
= g_new0(struct ctf_callsite_dups
, 1);
2590 BT_INIT_LIST_HEAD(&cs_dups
->head
);
2591 g_hash_table_insert(trace
->callsites
,
2592 (gpointer
) (unsigned long) callsite
->name
, cs_dups
);
2594 bt_list_add_tail(&callsite
->node
, &cs_dups
->head
);
2598 g_free(callsite
->func
);
2599 g_free(callsite
->file
);
2605 void callsite_free(gpointer data
)
2607 struct ctf_callsite_dups
*cs_dups
= data
;
2608 struct ctf_callsite
*callsite
, *cs_n
;
2610 bt_list_for_each_entry_safe(callsite
, cs_n
, &cs_dups
->head
, node
) {
2611 g_free(callsite
->func
);
2612 g_free(callsite
->file
);
2618 int ctf_env_declaration_visit(FILE *fd
, int depth
, struct ctf_node
*node
,
2619 struct ctf_trace
*trace
)
2622 struct ctf_tracer_env
*env
= &trace
->env
;
2624 switch (node
->type
) {
2625 case NODE_CTF_EXPRESSION
:
2629 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
2630 if (!strcmp(left
, "vpid")) {
2633 if (env
->vpid
!= -1) {
2634 fprintf(fd
, "[error] %s: vpid already declared in env declaration\n", __func__
);
2635 goto error
; /* ret is 0, so not an actual error, just warn. */
2637 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &v
);
2639 fprintf(fd
, "[error] %s: unexpected unary expression for env vpid\n", __func__
);
2640 goto error
; /* ret is 0, so not an actual error, just warn. */
2642 env
->vpid
= (int) v
;
2643 printf_verbose("env.vpid = %d\n", env
->vpid
);
2644 } else if (!strcmp(left
, "procname")) {
2647 if (env
->procname
[0]) {
2648 fprintf(fd
, "[warning] %s: duplicated env procname\n", __func__
);
2649 goto error
; /* ret is 0, so not an actual error, just warn. */
2651 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
2653 fprintf(fd
, "[warning] %s: unexpected unary expression for env procname\n", __func__
);
2654 goto error
; /* ret is 0, so not an actual error, just warn. */
2656 strncpy(env
->procname
, right
, TRACER_ENV_LEN
);
2657 env
->procname
[TRACER_ENV_LEN
- 1] = '\0';
2658 printf_verbose("env.procname = \"%s\"\n", env
->procname
);
2660 } else if (!strcmp(left
, "hostname")) {
2663 if (env
->hostname
[0]) {
2664 fprintf(fd
, "[warning] %s: duplicated env hostname\n", __func__
);
2665 goto error
; /* ret is 0, so not an actual error, just warn. */
2667 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
2669 fprintf(fd
, "[warning] %s: unexpected unary expression for env hostname\n", __func__
);
2670 goto error
; /* ret is 0, so not an actual error, just warn. */
2672 strncpy(env
->hostname
, right
, TRACER_ENV_LEN
);
2673 env
->hostname
[TRACER_ENV_LEN
- 1] = '\0';
2674 printf_verbose("env.hostname = \"%s\"\n", env
->hostname
);
2676 } else if (!strcmp(left
, "domain")) {
2679 if (env
->domain
[0]) {
2680 fprintf(fd
, "[warning] %s: duplicated env domain\n", __func__
);
2681 goto error
; /* ret is 0, so not an actual error, just warn. */
2683 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
2685 fprintf(fd
, "[warning] %s: unexpected unary expression for env domain\n", __func__
);
2686 goto error
; /* ret is 0, so not an actual error, just warn. */
2688 strncpy(env
->domain
, right
, TRACER_ENV_LEN
);
2689 env
->domain
[TRACER_ENV_LEN
- 1] = '\0';
2690 printf_verbose("env.domain = \"%s\"\n", env
->domain
);
2692 } else if (!strcmp(left
, "sysname")) {
2695 if (env
->sysname
[0]) {
2696 fprintf(fd
, "[warning] %s: duplicated env sysname\n", __func__
);
2697 goto error
; /* ret is 0, so not an actual error, just warn. */
2699 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
2701 fprintf(fd
, "[warning] %s: unexpected unary expression for env sysname\n", __func__
);
2702 goto error
; /* ret is 0, so not an actual error, just warn. */
2704 strncpy(env
->sysname
, right
, TRACER_ENV_LEN
);
2705 env
->sysname
[TRACER_ENV_LEN
- 1] = '\0';
2706 printf_verbose("env.sysname = \"%s\"\n", env
->sysname
);
2708 } else if (!strcmp(left
, "kernel_release")) {
2711 if (env
->release
[0]) {
2712 fprintf(fd
, "[warning] %s: duplicated env release\n", __func__
);
2713 goto error
; /* ret is 0, so not an actual error, just warn. */
2715 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
2717 fprintf(fd
, "[warning] %s: unexpected unary expression for env release\n", __func__
);
2718 goto error
; /* ret is 0, so not an actual error, just warn. */
2720 strncpy(env
->release
, right
, TRACER_ENV_LEN
);
2721 env
->release
[TRACER_ENV_LEN
- 1] = '\0';
2722 printf_verbose("env.release = \"%s\"\n", env
->release
);
2724 } else if (!strcmp(left
, "kernel_version")) {
2727 if (env
->version
[0]) {
2728 fprintf(fd
, "[warning] %s: duplicated env version\n", __func__
);
2729 goto error
; /* ret is 0, so not an actual error, just warn. */
2731 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
2733 fprintf(fd
, "[warning] %s: unexpected unary expression for env version\n", __func__
);
2734 goto error
; /* ret is 0, so not an actual error, just warn. */
2736 strncpy(env
->version
, right
, TRACER_ENV_LEN
);
2737 env
->version
[TRACER_ENV_LEN
- 1] = '\0';
2738 printf_verbose("env.version = \"%s\"\n", env
->version
);
2741 if (is_unary_string(&node
->u
.ctf_expression
.right
)) {
2744 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
2745 printf_verbose("env.%s = \"%s\"\n", left
, right
);
2747 } else if (is_unary_unsigned(&node
->u
.ctf_expression
.right
)) {
2751 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &v
);
2753 printf_verbose("env.%s = %" PRIu64
"\n", left
, v
);
2754 } else if (is_unary_signed(&node
->u
.ctf_expression
.right
)) {
2758 ret
= get_unary_signed(&node
->u
.ctf_expression
.right
, &v
);
2760 printf_verbose("env.%s = %" PRId64
"\n", left
, v
);
2762 printf_verbose("%s: attribute \"%s\" has unknown type.\n", __func__
, left
);
2778 int ctf_env_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_trace
*trace
)
2781 struct ctf_node
*iter
;
2783 trace
->env
.vpid
= -1;
2784 trace
->env
.procname
[0] = '\0';
2785 trace
->env
.hostname
[0] = '\0';
2786 trace
->env
.domain
[0] = '\0';
2787 trace
->env
.sysname
[0] = '\0';
2788 trace
->env
.release
[0] = '\0';
2789 trace
->env
.version
[0] = '\0';
2790 bt_list_for_each_entry(iter
, &node
->u
.env
.declaration_list
, siblings
) {
2791 ret
= ctf_env_declaration_visit(fd
, depth
+ 1, iter
, trace
);
2800 int ctf_root_declaration_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_trace
*trace
)
2804 switch (node
->type
) {
2806 ret
= ctf_typedef_visit(fd
, depth
+ 1,
2807 trace
->root_declaration_scope
,
2808 node
->u
._typedef
.type_specifier_list
,
2809 &node
->u
._typedef
.type_declarators
,
2814 case NODE_TYPEALIAS
:
2815 ret
= ctf_typealias_visit(fd
, depth
+ 1,
2816 trace
->root_declaration_scope
,
2817 node
->u
.typealias
.target
, node
->u
.typealias
.alias
,
2822 case NODE_TYPE_SPECIFIER_LIST
:
2824 struct declaration
*declaration
;
2827 * Just add the type specifier to the root scope
2828 * declaration scope. Release local reference.
2830 declaration
= ctf_type_specifier_list_visit(fd
, depth
+ 1,
2831 node
, trace
->root_declaration_scope
, trace
);
2834 declaration_unref(declaration
);
2844 int ctf_visitor_construct_metadata(FILE *fd
, int depth
, struct ctf_node
*node
,
2845 struct ctf_trace
*trace
, int byte_order
)
2848 struct ctf_node
*iter
;
2849 int env_clock_done
= 0;
2851 printf_verbose("CTF visitor: metadata construction... ");
2852 trace
->byte_order
= byte_order
;
2853 trace
->clocks
= g_hash_table_new_full(g_direct_hash
, g_direct_equal
,
2855 trace
->callsites
= g_hash_table_new_full(g_direct_hash
, g_direct_equal
,
2856 NULL
, callsite_free
);
2859 trace
->root_declaration_scope
= new_declaration_scope(NULL
);
2861 switch (node
->type
) {
2863 if (!env_clock_done
) {
2865 * declarations need to query clock hash table,
2866 * so clock need to be treated first.
2868 if (bt_list_empty(&node
->u
.root
.clock
)) {
2869 ctf_clock_default(fd
, depth
+ 1, trace
);
2871 bt_list_for_each_entry(iter
, &node
->u
.root
.clock
, siblings
) {
2872 ret
= ctf_clock_visit(fd
, depth
+ 1, iter
,
2875 fprintf(fd
, "[error] %s: clock declaration error\n", __func__
);
2882 bt_list_for_each_entry(iter
, &node
->u
.root
.declaration_list
,
2884 ret
= ctf_root_declaration_visit(fd
, depth
+ 1, iter
, trace
);
2886 fprintf(fd
, "[error] %s: root declaration error\n", __func__
);
2890 bt_list_for_each_entry(iter
, &node
->u
.root
.trace
, siblings
) {
2891 ret
= ctf_trace_visit(fd
, depth
+ 1, iter
, trace
);
2892 if (ret
== -EINTR
) {
2893 free_declaration_scope(trace
->root_declaration_scope
);
2895 * Need to restart creation of type
2896 * definitions, aliases and
2897 * trace header declarations.
2902 fprintf(fd
, "[error] %s: trace declaration error\n", __func__
);
2906 bt_list_for_each_entry(iter
, &node
->u
.root
.callsite
, siblings
) {
2907 ret
= ctf_callsite_visit(fd
, depth
+ 1, iter
,
2910 fprintf(fd
, "[error] %s: callsite declaration error\n", __func__
);
2914 if (!trace
->streams
) {
2915 fprintf(fd
, "[error] %s: missing trace declaration\n", __func__
);
2919 bt_list_for_each_entry(iter
, &node
->u
.root
.env
, siblings
) {
2920 ret
= ctf_env_visit(fd
, depth
+ 1, iter
, trace
);
2922 fprintf(fd
, "[error] %s: env declaration error\n", __func__
);
2926 bt_list_for_each_entry(iter
, &node
->u
.root
.stream
, siblings
) {
2927 ret
= ctf_stream_visit(fd
, depth
+ 1, iter
,
2928 trace
->root_declaration_scope
, trace
);
2930 fprintf(fd
, "[error] %s: stream declaration error\n", __func__
);
2934 bt_list_for_each_entry(iter
, &node
->u
.root
.event
, siblings
) {
2935 ret
= ctf_event_visit(fd
, depth
+ 1, iter
,
2936 trace
->root_declaration_scope
, trace
);
2938 fprintf(fd
, "[error] %s: event declaration error\n", __func__
);
2945 fprintf(fd
, "[error] %s: unknown node type %d\n", __func__
,
2950 printf_verbose("done.\n");
2954 free_declaration_scope(trace
->root_declaration_scope
);
2955 g_hash_table_destroy(trace
->callsites
);
2956 g_hash_table_destroy(trace
->clocks
);
2960 int ctf_destroy_metadata(struct ctf_trace
*trace
)
2963 struct ctf_file_stream
*metadata_stream
;
2965 if (trace
->streams
) {
2966 for (i
= 0; i
< trace
->streams
->len
; i
++) {
2967 struct ctf_stream_declaration
*stream
;
2969 stream
= g_ptr_array_index(trace
->streams
, i
);
2972 for (j
= 0; j
< stream
->streams
->len
; j
++) {
2973 struct ctf_stream_definition
*stream_def
;
2975 stream_def
= g_ptr_array_index(stream
->streams
, j
);
2978 for (k
= 0; k
< stream_def
->events_by_id
->len
; k
++) {
2979 struct ctf_event_definition
*event
;
2981 event
= g_ptr_array_index(stream_def
->events_by_id
, k
);
2984 if (&event
->event_fields
->p
)
2985 definition_unref(&event
->event_fields
->p
);
2986 if (&event
->event_context
->p
)
2987 definition_unref(&event
->event_context
->p
);
2990 if (&stream_def
->trace_packet_header
->p
)
2991 definition_unref(&stream_def
->trace_packet_header
->p
);
2992 if (&stream_def
->stream_event_header
->p
)
2993 definition_unref(&stream_def
->stream_event_header
->p
);
2994 if (&stream_def
->stream_packet_context
->p
)
2995 definition_unref(&stream_def
->stream_packet_context
->p
);
2996 if (&stream_def
->stream_event_context
->p
)
2997 definition_unref(&stream_def
->stream_event_context
->p
);
2998 g_ptr_array_free(stream_def
->events_by_id
, TRUE
);
3001 if (stream
->event_header_decl
)
3002 declaration_unref(&stream
->event_header_decl
->p
);
3003 if (stream
->event_context_decl
)
3004 declaration_unref(&stream
->event_context_decl
->p
);
3005 if (stream
->packet_context_decl
)
3006 declaration_unref(&stream
->packet_context_decl
->p
);
3007 g_ptr_array_free(stream
->streams
, TRUE
);
3008 g_ptr_array_free(stream
->events_by_id
, TRUE
);
3009 g_hash_table_destroy(stream
->event_quark_to_id
);
3010 free_declaration_scope(stream
->declaration_scope
);
3013 g_ptr_array_free(trace
->streams
, TRUE
);
3016 if (trace
->event_declarations
) {
3017 for (i
= 0; i
< trace
->event_declarations
->len
; i
++) {
3018 struct bt_ctf_event_decl
*event_decl
;
3019 struct ctf_event_declaration
*event
;
3021 event_decl
= g_ptr_array_index(trace
->event_declarations
, i
);
3022 if (event_decl
->context_decl
)
3023 g_ptr_array_free(event_decl
->context_decl
, TRUE
);
3024 if (event_decl
->fields_decl
)
3025 g_ptr_array_free(event_decl
->fields_decl
, TRUE
);
3026 if (event_decl
->packet_header_decl
)
3027 g_ptr_array_free(event_decl
->packet_header_decl
, TRUE
);
3028 if (event_decl
->event_context_decl
)
3029 g_ptr_array_free(event_decl
->event_context_decl
, TRUE
);
3030 if (event_decl
->event_header_decl
)
3031 g_ptr_array_free(event_decl
->event_header_decl
, TRUE
);
3032 if (event_decl
->packet_context_decl
)
3033 g_ptr_array_free(event_decl
->packet_context_decl
, TRUE
);
3035 event
= &event_decl
->parent
;
3036 if (event
->fields_decl
)
3037 declaration_unref(&event
->fields_decl
->p
);
3038 if (event
->context_decl
)
3039 declaration_unref(&event
->context_decl
->p
);
3040 free_declaration_scope(event
->declaration_scope
);
3044 g_ptr_array_free(trace
->event_declarations
, TRUE
);
3046 if (trace
->packet_header_decl
)
3047 declaration_unref(&trace
->packet_header_decl
->p
);
3049 free_declaration_scope(trace
->root_declaration_scope
);
3050 free_declaration_scope(trace
->declaration_scope
);
3052 g_hash_table_destroy(trace
->callsites
);
3053 g_hash_table_destroy(trace
->clocks
);
3055 metadata_stream
= container_of(trace
->metadata
, struct ctf_file_stream
, parent
);
3056 g_free(metadata_stream
);