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
;
828 * For named struct (without body), lookup in
829 * declaration scope. Don't take reference on struct
830 * declaration: ref is only taken upon definition.
835 lookup_struct_declaration(g_quark_from_string(name
),
837 declaration_ref(&struct_declaration
->p
);
838 return &struct_declaration
->p
;
840 uint64_t min_align_value
= 0;
842 /* For unnamed struct, create type */
843 /* For named struct (with body), create type and add to declaration scope */
845 if (lookup_struct_declaration(g_quark_from_string(name
),
846 declaration_scope
)) {
848 fprintf(fd
, "[error] %s: struct %s already declared in scope\n", __func__
, name
);
852 if (!bt_list_empty(min_align
)) {
855 ret
= get_unary_unsigned(min_align
, &min_align_value
);
857 fprintf(fd
, "[error] %s: unexpected unary expression for structure \"align\" attribute\n", __func__
);
862 struct_declaration
= struct_declaration_new(declaration_scope
,
864 bt_list_for_each_entry(iter
, declaration_list
, siblings
) {
867 ret
= ctf_struct_declaration_list_visit(fd
, depth
+ 1, iter
,
868 struct_declaration
, trace
);
870 goto error_free_declaration
;
875 ret
= register_struct_declaration(g_quark_from_string(name
),
880 return &struct_declaration
->p
;
882 error_free_declaration
:
883 struct_declaration
->p
.declaration_free(&struct_declaration
->p
);
889 struct declaration
*ctf_declaration_variant_visit(FILE *fd
,
890 int depth
, const char *name
, const char *choice
,
891 struct bt_list_head
*declaration_list
,
892 int has_body
, struct declaration_scope
*declaration_scope
,
893 struct ctf_trace
*trace
)
895 struct declaration_untagged_variant
*untagged_variant_declaration
;
896 struct declaration_variant
*variant_declaration
;
897 struct ctf_node
*iter
;
900 * For named variant (without body), lookup in
901 * declaration scope. Don't take reference on variant
902 * declaration: ref is only taken upon definition.
906 untagged_variant_declaration
=
907 lookup_variant_declaration(g_quark_from_string(name
),
909 declaration_ref(&untagged_variant_declaration
->p
);
911 /* For unnamed variant, create type */
912 /* For named variant (with body), create type and add to declaration scope */
914 if (lookup_variant_declaration(g_quark_from_string(name
),
915 declaration_scope
)) {
917 fprintf(fd
, "[error] %s: variant %s already declared in scope\n", __func__
, name
);
921 untagged_variant_declaration
= untagged_variant_declaration_new(declaration_scope
);
922 bt_list_for_each_entry(iter
, declaration_list
, siblings
) {
925 ret
= ctf_variant_declaration_list_visit(fd
, depth
+ 1, iter
,
926 untagged_variant_declaration
, trace
);
933 ret
= register_variant_declaration(g_quark_from_string(name
),
934 untagged_variant_declaration
,
940 * if tagged, create tagged variant and return. else return
944 return &untagged_variant_declaration
->p
;
946 variant_declaration
= variant_declaration_new(untagged_variant_declaration
, choice
);
947 if (!variant_declaration
)
949 declaration_unref(&untagged_variant_declaration
->p
);
950 return &variant_declaration
->p
;
953 untagged_variant_declaration
->p
.declaration_free(&untagged_variant_declaration
->p
);
958 int ctf_enumerator_list_visit(FILE *fd
, int depth
,
959 struct ctf_node
*enumerator
,
960 struct declaration_enum
*enum_declaration
,
961 struct last_enum_value
*last
)
964 struct ctf_node
*iter
;
966 q
= g_quark_from_string(enumerator
->u
.enumerator
.id
);
967 if (enum_declaration
->integer_declaration
->signedness
) {
971 bt_list_for_each_entry(iter
, &enumerator
->u
.enumerator
.values
, siblings
) {
974 assert(iter
->type
== NODE_UNARY_EXPRESSION
);
980 switch (iter
->u
.unary_expression
.type
) {
981 case UNARY_SIGNED_CONSTANT
:
982 *target
= iter
->u
.unary_expression
.u
.signed_constant
;
984 case UNARY_UNSIGNED_CONSTANT
:
985 *target
= iter
->u
.unary_expression
.u
.unsigned_constant
;
988 fprintf(fd
, "[error] %s: invalid enumerator\n", __func__
);
992 fprintf(fd
, "[error] %s: invalid enumerator\n", __func__
);
1001 last
->u
.s
= end
+ 1;
1002 enum_signed_insert(enum_declaration
, start
, end
, q
);
1004 uint64_t start
, end
;
1007 bt_list_for_each_entry(iter
, &enumerator
->u
.enumerator
.values
, siblings
) {
1010 assert(iter
->type
== NODE_UNARY_EXPRESSION
);
1016 switch (iter
->u
.unary_expression
.type
) {
1017 case UNARY_UNSIGNED_CONSTANT
:
1018 *target
= iter
->u
.unary_expression
.u
.unsigned_constant
;
1020 case UNARY_SIGNED_CONSTANT
:
1022 * We don't accept signed constants for enums with unsigned
1025 fprintf(fd
, "[error] %s: invalid enumerator (signed constant encountered, but enum container type is unsigned)\n", __func__
);
1028 fprintf(fd
, "[error] %s: invalid enumerator\n", __func__
);
1032 fprintf(fd
, "[error] %s: invalid enumerator\n", __func__
);
1041 last
->u
.u
= end
+ 1;
1042 enum_unsigned_insert(enum_declaration
, start
, end
, q
);
1048 struct declaration
*ctf_declaration_enum_visit(FILE *fd
, int depth
,
1050 struct ctf_node
*container_type
,
1051 struct bt_list_head
*enumerator_list
,
1053 struct declaration_scope
*declaration_scope
,
1054 struct ctf_trace
*trace
)
1056 struct declaration
*declaration
;
1057 struct declaration_enum
*enum_declaration
;
1058 struct declaration_integer
*integer_declaration
;
1059 struct last_enum_value last_value
;
1060 struct ctf_node
*iter
;
1064 * For named enum (without body), lookup in
1065 * declaration scope. Don't take reference on enum
1066 * declaration: ref is only taken upon definition.
1071 lookup_enum_declaration(g_quark_from_string(name
),
1073 declaration_ref(&enum_declaration
->p
);
1074 return &enum_declaration
->p
;
1076 /* For unnamed enum, create type */
1077 /* For named enum (with body), create type and add to declaration scope */
1079 if (lookup_enum_declaration(g_quark_from_string(name
),
1080 declaration_scope
)) {
1082 fprintf(fd
, "[error] %s: enum %s already declared in scope\n", __func__
, name
);
1086 if (!container_type
) {
1087 declaration
= lookup_declaration(g_quark_from_static_string("int"),
1090 fprintf(fd
, "[error] %s: \"int\" type declaration missing for enumeration\n", __func__
);
1094 declaration
= ctf_type_declarator_visit(fd
, depth
,
1101 fprintf(fd
, "[error] %s: unable to create container type for enumeration\n", __func__
);
1104 if (declaration
->id
!= CTF_TYPE_INTEGER
) {
1105 fprintf(fd
, "[error] %s: container type for enumeration is not integer\n", __func__
);
1108 integer_declaration
= container_of(declaration
, struct declaration_integer
, p
);
1109 enum_declaration
= enum_declaration_new(integer_declaration
);
1110 declaration_unref(&integer_declaration
->p
); /* leave ref to enum */
1111 if (enum_declaration
->integer_declaration
->signedness
) {
1116 bt_list_for_each_entry(iter
, enumerator_list
, siblings
) {
1119 ret
= ctf_enumerator_list_visit(fd
, depth
+ 1, iter
, enum_declaration
,
1127 ret
= register_enum_declaration(g_quark_from_string(name
),
1131 declaration_unref(&enum_declaration
->p
);
1133 return &enum_declaration
->p
;
1136 enum_declaration
->p
.declaration_free(&enum_declaration
->p
);
1141 struct declaration
*ctf_declaration_type_specifier_visit(FILE *fd
, int depth
,
1142 struct ctf_node
*type_specifier_list
,
1143 struct declaration_scope
*declaration_scope
)
1146 struct declaration
*declaration
;
1151 str
= g_string_new("");
1152 ret
= visit_type_specifier_list(fd
, type_specifier_list
, str
);
1155 str_c
= g_string_free(str
, FALSE
);
1156 id_q
= g_quark_from_string(str_c
);
1158 declaration
= lookup_declaration(id_q
, declaration_scope
);
1159 declaration_ref(declaration
);
1164 * Returns 0/1 boolean, or < 0 on error.
1167 int get_boolean(FILE *fd
, int depth
, struct ctf_node
*unary_expression
)
1169 if (unary_expression
->type
!= NODE_UNARY_EXPRESSION
) {
1170 fprintf(fd
, "[error] %s: expecting unary expression\n",
1174 switch (unary_expression
->u
.unary_expression
.type
) {
1175 case UNARY_UNSIGNED_CONSTANT
:
1176 if (unary_expression
->u
.unary_expression
.u
.unsigned_constant
== 0)
1180 case UNARY_SIGNED_CONSTANT
:
1181 if (unary_expression
->u
.unary_expression
.u
.signed_constant
== 0)
1186 if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "true"))
1188 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "TRUE"))
1190 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "false"))
1192 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "FALSE"))
1195 fprintf(fd
, "[error] %s: unexpected string \"%s\"\n",
1196 __func__
, unary_expression
->u
.unary_expression
.u
.string
);
1201 fprintf(fd
, "[error] %s: unexpected unary expression type\n",
1209 int get_trace_byte_order(FILE *fd
, int depth
, struct ctf_node
*unary_expression
)
1213 if (unary_expression
->u
.unary_expression
.type
!= UNARY_STRING
) {
1214 fprintf(fd
, "[error] %s: byte_order: expecting string\n",
1218 if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "be"))
1219 byte_order
= BIG_ENDIAN
;
1220 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "le"))
1221 byte_order
= LITTLE_ENDIAN
;
1223 fprintf(fd
, "[error] %s: unexpected string \"%s\". Should be \"native\", \"network\", \"be\" or \"le\".\n",
1224 __func__
, unary_expression
->u
.unary_expression
.u
.string
);
1231 int get_byte_order(FILE *fd
, int depth
, struct ctf_node
*unary_expression
,
1232 struct ctf_trace
*trace
)
1236 if (unary_expression
->u
.unary_expression
.type
!= UNARY_STRING
) {
1237 fprintf(fd
, "[error] %s: byte_order: expecting string\n",
1241 if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "native"))
1242 byte_order
= trace
->byte_order
;
1243 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "network"))
1244 byte_order
= BIG_ENDIAN
;
1245 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "be"))
1246 byte_order
= BIG_ENDIAN
;
1247 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "le"))
1248 byte_order
= LITTLE_ENDIAN
;
1250 fprintf(fd
, "[error] %s: unexpected string \"%s\". Should be \"native\", \"network\", \"be\" or \"le\".\n",
1251 __func__
, unary_expression
->u
.unary_expression
.u
.string
);
1258 struct declaration
*ctf_declaration_integer_visit(FILE *fd
, int depth
,
1259 struct bt_list_head
*expressions
,
1260 struct ctf_trace
*trace
)
1262 struct ctf_node
*expression
;
1263 uint64_t alignment
= 1, size
= 0;
1264 int byte_order
= trace
->byte_order
;
1266 int has_alignment
= 0, has_size
= 0;
1268 enum ctf_string_encoding encoding
= CTF_STRING_NONE
;
1269 struct ctf_clock
*clock
= NULL
;
1270 struct declaration_integer
*integer_declaration
;
1272 bt_list_for_each_entry(expression
, expressions
, siblings
) {
1273 struct ctf_node
*left
, *right
;
1275 left
= _bt_list_first_entry(&expression
->u
.ctf_expression
.left
, struct ctf_node
, siblings
);
1276 right
= _bt_list_first_entry(&expression
->u
.ctf_expression
.right
, struct ctf_node
, siblings
);
1277 assert(left
->u
.unary_expression
.type
== UNARY_STRING
);
1278 if (!strcmp(left
->u
.unary_expression
.u
.string
, "signed")) {
1279 signedness
= get_boolean(fd
, depth
, right
);
1282 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "byte_order")) {
1283 byte_order
= get_byte_order(fd
, depth
, right
, trace
);
1286 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "size")) {
1287 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
1288 fprintf(fd
, "[error] %s: size: expecting unsigned constant\n",
1292 size
= right
->u
.unary_expression
.u
.unsigned_constant
;
1294 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "align")) {
1295 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
1296 fprintf(fd
, "[error] %s: align: expecting unsigned constant\n",
1300 alignment
= right
->u
.unary_expression
.u
.unsigned_constant
;
1301 /* Make sure alignment is a power of two */
1302 if (alignment
== 0 || (alignment
& (alignment
- 1)) != 0) {
1303 fprintf(fd
, "[error] %s: align: expecting power of two\n",
1308 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "base")) {
1309 switch (right
->u
.unary_expression
.type
) {
1310 case UNARY_UNSIGNED_CONSTANT
:
1311 switch (right
->u
.unary_expression
.u
.unsigned_constant
) {
1316 base
= right
->u
.unary_expression
.u
.unsigned_constant
;
1319 fprintf(fd
, "[error] %s: base not supported (%" PRIu64
")\n",
1320 __func__
, right
->u
.unary_expression
.u
.unsigned_constant
);
1326 char *s_right
= concatenate_unary_strings(&expression
->u
.ctf_expression
.right
);
1328 fprintf(fd
, "[error] %s: unexpected unary expression for integer base\n", __func__
);
1332 if (!strcmp(s_right
, "decimal") || !strcmp(s_right
, "dec") || !strcmp(s_right
, "d")
1333 || !strcmp(s_right
, "i") || !strcmp(s_right
, "u")) {
1335 } else if (!strcmp(s_right
, "hexadecimal") || !strcmp(s_right
, "hex")
1336 || !strcmp(s_right
, "x") || !strcmp(s_right
, "X")
1337 || !strcmp(s_right
, "p")) {
1339 } else if (!strcmp(s_right
, "octal") || !strcmp(s_right
, "oct")
1340 || !strcmp(s_right
, "o")) {
1342 } else if (!strcmp(s_right
, "binary") || !strcmp(s_right
, "b")) {
1345 fprintf(fd
, "[error] %s: unexpected expression for integer base (%s)\n", __func__
, s_right
);
1354 fprintf(fd
, "[error] %s: base: expecting unsigned constant or unary string\n",
1358 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "encoding")) {
1361 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
1362 fprintf(fd
, "[error] %s: encoding: expecting unary string\n",
1366 s_right
= concatenate_unary_strings(&expression
->u
.ctf_expression
.right
);
1368 fprintf(fd
, "[error] %s: unexpected unary expression for integer base\n", __func__
);
1372 if (!strcmp(s_right
, "UTF8")
1373 || !strcmp(s_right
, "utf8")
1374 || !strcmp(s_right
, "utf-8")
1375 || !strcmp(s_right
, "UTF-8"))
1376 encoding
= CTF_STRING_UTF8
;
1377 else if (!strcmp(s_right
, "ASCII")
1378 || !strcmp(s_right
, "ascii"))
1379 encoding
= CTF_STRING_ASCII
;
1380 else if (!strcmp(s_right
, "none"))
1381 encoding
= CTF_STRING_NONE
;
1383 fprintf(fd
, "[error] %s: unknown string encoding \"%s\"\n", __func__
, s_right
);
1388 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "map")) {
1391 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
1392 fprintf(fd
, "[error] %s: map: expecting identifier\n",
1396 /* currently only support clock.name.value */
1397 clock_name
= get_map_clock_name_value(&expression
->u
.ctf_expression
.right
);
1401 s_right
= concatenate_unary_strings(&expression
->u
.ctf_expression
.right
);
1403 fprintf(fd
, "[error] %s: unexpected unary expression for integer map\n", __func__
);
1407 fprintf(fd
, "[warning] %s: unknown map %s in integer declaration\n", __func__
,
1412 clock
= trace_clock_lookup(trace
, clock_name
);
1414 fprintf(fd
, "[error] %s: map: unable to find clock %s declaration\n",
1415 __func__
, g_quark_to_string(clock_name
));
1419 fprintf(fd
, "[warning] %s: unknown attribute name %s\n",
1420 __func__
, left
->u
.unary_expression
.u
.string
);
1421 /* Fall-through after warning */
1425 fprintf(fd
, "[error] %s: missing size attribute\n", __func__
);
1428 if (!has_alignment
) {
1429 if (size
% CHAR_BIT
) {
1430 /* bit-packed alignment */
1433 /* byte-packed alignment */
1434 alignment
= CHAR_BIT
;
1437 integer_declaration
= integer_declaration_new(size
,
1438 byte_order
, signedness
, alignment
,
1439 base
, encoding
, clock
);
1440 return &integer_declaration
->p
;
1444 struct declaration
*ctf_declaration_floating_point_visit(FILE *fd
, int depth
,
1445 struct bt_list_head
*expressions
,
1446 struct ctf_trace
*trace
)
1448 struct ctf_node
*expression
;
1449 uint64_t alignment
= 1, exp_dig
= 0, mant_dig
= 0,
1450 byte_order
= trace
->byte_order
;
1451 int has_alignment
= 0, has_exp_dig
= 0, has_mant_dig
= 0;
1452 struct declaration_float
*float_declaration
;
1454 bt_list_for_each_entry(expression
, expressions
, siblings
) {
1455 struct ctf_node
*left
, *right
;
1457 left
= _bt_list_first_entry(&expression
->u
.ctf_expression
.left
, struct ctf_node
, siblings
);
1458 right
= _bt_list_first_entry(&expression
->u
.ctf_expression
.right
, struct ctf_node
, siblings
);
1459 assert(left
->u
.unary_expression
.type
== UNARY_STRING
);
1460 if (!strcmp(left
->u
.unary_expression
.u
.string
, "byte_order")) {
1461 byte_order
= get_byte_order(fd
, depth
, right
, trace
);
1464 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "exp_dig")) {
1465 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
1466 fprintf(fd
, "[error] %s: exp_dig: expecting unsigned constant\n",
1470 exp_dig
= right
->u
.unary_expression
.u
.unsigned_constant
;
1472 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "mant_dig")) {
1473 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
1474 fprintf(fd
, "[error] %s: mant_dig: expecting unsigned constant\n",
1478 mant_dig
= right
->u
.unary_expression
.u
.unsigned_constant
;
1480 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "align")) {
1481 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
1482 fprintf(fd
, "[error] %s: align: expecting unsigned constant\n",
1486 alignment
= right
->u
.unary_expression
.u
.unsigned_constant
;
1487 /* Make sure alignment is a power of two */
1488 if (alignment
== 0 || (alignment
& (alignment
- 1)) != 0) {
1489 fprintf(fd
, "[error] %s: align: expecting power of two\n",
1495 fprintf(fd
, "[warning] %s: unknown attribute name %s\n",
1496 __func__
, left
->u
.unary_expression
.u
.string
);
1497 /* Fall-through after warning */
1500 if (!has_mant_dig
) {
1501 fprintf(fd
, "[error] %s: missing mant_dig attribute\n", __func__
);
1505 fprintf(fd
, "[error] %s: missing exp_dig attribute\n", __func__
);
1508 if (!has_alignment
) {
1509 if ((mant_dig
+ exp_dig
) % CHAR_BIT
) {
1510 /* bit-packed alignment */
1513 /* byte-packed alignment */
1514 alignment
= CHAR_BIT
;
1517 float_declaration
= float_declaration_new(mant_dig
, exp_dig
,
1518 byte_order
, alignment
);
1519 return &float_declaration
->p
;
1523 struct declaration
*ctf_declaration_string_visit(FILE *fd
, int depth
,
1524 struct bt_list_head
*expressions
,
1525 struct ctf_trace
*trace
)
1527 struct ctf_node
*expression
;
1528 const char *encoding_c
= NULL
;
1529 enum ctf_string_encoding encoding
= CTF_STRING_UTF8
;
1530 struct declaration_string
*string_declaration
;
1532 bt_list_for_each_entry(expression
, expressions
, siblings
) {
1533 struct ctf_node
*left
, *right
;
1535 left
= _bt_list_first_entry(&expression
->u
.ctf_expression
.left
, struct ctf_node
, siblings
);
1536 right
= _bt_list_first_entry(&expression
->u
.ctf_expression
.right
, struct ctf_node
, siblings
);
1537 assert(left
->u
.unary_expression
.type
== UNARY_STRING
);
1538 if (!strcmp(left
->u
.unary_expression
.u
.string
, "encoding")) {
1539 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
1540 fprintf(fd
, "[error] %s: encoding: expecting string\n",
1544 encoding_c
= right
->u
.unary_expression
.u
.string
;
1546 fprintf(fd
, "[warning] %s: unknown attribute name %s\n",
1547 __func__
, left
->u
.unary_expression
.u
.string
);
1548 /* Fall-through after warning */
1551 if (encoding_c
&& !strcmp(encoding_c
, "ASCII"))
1552 encoding
= CTF_STRING_ASCII
;
1553 string_declaration
= string_declaration_new(encoding
);
1554 return &string_declaration
->p
;
1559 struct declaration
*ctf_type_specifier_list_visit(FILE *fd
,
1560 int depth
, struct ctf_node
*type_specifier_list
,
1561 struct declaration_scope
*declaration_scope
,
1562 struct ctf_trace
*trace
)
1564 struct ctf_node
*first
;
1565 struct ctf_node
*node
;
1567 assert(type_specifier_list
->type
== NODE_TYPE_SPECIFIER_LIST
);
1569 first
= _bt_list_first_entry(&type_specifier_list
->u
.type_specifier_list
.head
, struct ctf_node
, siblings
);
1571 assert(first
->type
== NODE_TYPE_SPECIFIER
);
1573 node
= first
->u
.type_specifier
.node
;
1575 switch (first
->u
.type_specifier
.type
) {
1576 case TYPESPEC_FLOATING_POINT
:
1577 return ctf_declaration_floating_point_visit(fd
, depth
,
1578 &node
->u
.floating_point
.expressions
, trace
);
1579 case TYPESPEC_INTEGER
:
1580 return ctf_declaration_integer_visit(fd
, depth
,
1581 &node
->u
.integer
.expressions
, trace
);
1582 case TYPESPEC_STRING
:
1583 return ctf_declaration_string_visit(fd
, depth
,
1584 &node
->u
.string
.expressions
, trace
);
1585 case TYPESPEC_STRUCT
:
1586 return ctf_declaration_struct_visit(fd
, depth
,
1587 node
->u
._struct
.name
,
1588 &node
->u
._struct
.declaration_list
,
1589 node
->u
._struct
.has_body
,
1590 &node
->u
._struct
.min_align
,
1593 case TYPESPEC_VARIANT
:
1594 return ctf_declaration_variant_visit(fd
, depth
,
1595 node
->u
.variant
.name
,
1596 node
->u
.variant
.choice
,
1597 &node
->u
.variant
.declaration_list
,
1598 node
->u
.variant
.has_body
,
1602 return ctf_declaration_enum_visit(fd
, depth
,
1603 node
->u
._enum
.enum_id
,
1604 node
->u
._enum
.container_type
,
1605 &node
->u
._enum
.enumerator_list
,
1606 node
->u
._enum
.has_body
,
1612 case TYPESPEC_SHORT
:
1615 case TYPESPEC_FLOAT
:
1616 case TYPESPEC_DOUBLE
:
1617 case TYPESPEC_SIGNED
:
1618 case TYPESPEC_UNSIGNED
:
1620 case TYPESPEC_COMPLEX
:
1621 case TYPESPEC_IMAGINARY
:
1622 case TYPESPEC_CONST
:
1623 case TYPESPEC_ID_TYPE
:
1624 return ctf_declaration_type_specifier_visit(fd
, depth
,
1625 type_specifier_list
, declaration_scope
);
1627 fprintf(fd
, "[error] %s: unexpected node type %d\n", __func__
, (int) first
->u
.type_specifier
.type
);
1633 int ctf_event_declaration_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_event_declaration
*event
, struct ctf_trace
*trace
)
1637 switch (node
->type
) {
1639 ret
= ctf_typedef_visit(fd
, depth
+ 1,
1640 event
->declaration_scope
,
1641 node
->u
._typedef
.type_specifier_list
,
1642 &node
->u
._typedef
.type_declarators
,
1647 case NODE_TYPEALIAS
:
1648 ret
= ctf_typealias_visit(fd
, depth
+ 1,
1649 event
->declaration_scope
,
1650 node
->u
.typealias
.target
, node
->u
.typealias
.alias
,
1655 case NODE_CTF_EXPRESSION
:
1659 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
1660 if (!strcmp(left
, "name")) {
1663 if (CTF_EVENT_FIELD_IS_SET(event
, name
)) {
1664 fprintf(fd
, "[error] %s: name already declared in event declaration\n", __func__
);
1668 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
1670 fprintf(fd
, "[error] %s: unexpected unary expression for event name\n", __func__
);
1674 event
->name
= g_quark_from_string(right
);
1676 CTF_EVENT_SET_FIELD(event
, name
);
1677 } else if (!strcmp(left
, "id")) {
1678 if (CTF_EVENT_FIELD_IS_SET(event
, id
)) {
1679 fprintf(fd
, "[error] %s: id already declared in event declaration\n", __func__
);
1683 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &event
->id
);
1685 fprintf(fd
, "[error] %s: unexpected unary expression for event id\n", __func__
);
1689 CTF_EVENT_SET_FIELD(event
, id
);
1690 } else if (!strcmp(left
, "stream_id")) {
1691 if (CTF_EVENT_FIELD_IS_SET(event
, stream_id
)) {
1692 fprintf(fd
, "[error] %s: stream_id already declared in event declaration\n", __func__
);
1696 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &event
->stream_id
);
1698 fprintf(fd
, "[error] %s: unexpected unary expression for event stream_id\n", __func__
);
1702 event
->stream
= trace_stream_lookup(trace
, event
->stream_id
);
1703 if (!event
->stream
) {
1704 fprintf(fd
, "[error] %s: stream id %" PRIu64
" cannot be found\n", __func__
, event
->stream_id
);
1708 CTF_EVENT_SET_FIELD(event
, stream_id
);
1709 } else if (!strcmp(left
, "context")) {
1710 struct declaration
*declaration
;
1712 if (event
->context_decl
) {
1713 fprintf(fd
, "[error] %s: context already declared in event declaration\n", __func__
);
1717 declaration
= ctf_type_specifier_list_visit(fd
, depth
,
1718 _bt_list_first_entry(&node
->u
.ctf_expression
.right
,
1719 struct ctf_node
, siblings
),
1720 event
->declaration_scope
, trace
);
1725 if (declaration
->id
!= CTF_TYPE_STRUCT
) {
1729 event
->context_decl
= container_of(declaration
, struct declaration_struct
, p
);
1730 } else if (!strcmp(left
, "fields")) {
1731 struct declaration
*declaration
;
1733 if (event
->fields_decl
) {
1734 fprintf(fd
, "[error] %s: fields already declared in event declaration\n", __func__
);
1738 declaration
= ctf_type_specifier_list_visit(fd
, depth
,
1739 _bt_list_first_entry(&node
->u
.ctf_expression
.right
,
1740 struct ctf_node
, siblings
),
1741 event
->declaration_scope
, trace
);
1746 if (declaration
->id
!= CTF_TYPE_STRUCT
) {
1750 event
->fields_decl
= container_of(declaration
, struct declaration_struct
, p
);
1751 } else if (!strcmp(left
, "loglevel")) {
1752 int64_t loglevel
= -1;
1754 if (CTF_EVENT_FIELD_IS_SET(event
, loglevel
)) {
1755 fprintf(fd
, "[error] %s: loglevel already declared in event declaration\n", __func__
);
1759 ret
= get_unary_signed(&node
->u
.ctf_expression
.right
, &loglevel
);
1761 fprintf(fd
, "[error] %s: unexpected unary expression for event loglevel\n", __func__
);
1765 event
->loglevel
= (int) loglevel
;
1766 CTF_EVENT_SET_FIELD(event
, loglevel
);
1767 } else if (!strcmp(left
, "model.emf.uri")) {
1770 if (CTF_EVENT_FIELD_IS_SET(event
, model_emf_uri
)) {
1771 fprintf(fd
, "[error] %s: model.emf.uri already declared in event declaration\n", __func__
);
1775 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
1777 fprintf(fd
, "[error] %s: unexpected unary expression for event model.emf.uri\n", __func__
);
1781 event
->model_emf_uri
= g_quark_from_string(right
);
1783 CTF_EVENT_SET_FIELD(event
, model_emf_uri
);
1785 fprintf(fd
, "[warning] %s: attribute \"%s\" is unknown in event declaration.\n", __func__
, left
);
1786 /* Fall-through after warning */
1794 /* TODO: declaration specifier should be added. */
1801 int ctf_event_visit(FILE *fd
, int depth
, struct ctf_node
*node
,
1802 struct declaration_scope
*parent_declaration_scope
, struct ctf_trace
*trace
)
1805 struct ctf_node
*iter
;
1806 struct ctf_event_declaration
*event
;
1807 struct bt_ctf_event_decl
*event_decl
;
1809 event_decl
= g_new0(struct bt_ctf_event_decl
, 1);
1810 event
= &event_decl
->parent
;
1811 event
->declaration_scope
= new_declaration_scope(parent_declaration_scope
);
1812 event
->loglevel
= -1;
1813 bt_list_for_each_entry(iter
, &node
->u
.event
.declaration_list
, siblings
) {
1814 ret
= ctf_event_declaration_visit(fd
, depth
+ 1, iter
, event
, trace
);
1818 if (!CTF_EVENT_FIELD_IS_SET(event
, name
)) {
1820 fprintf(fd
, "[error] %s: missing name field in event declaration\n", __func__
);
1823 if (!CTF_EVENT_FIELD_IS_SET(event
, stream_id
)) {
1824 /* Allow missing stream_id if there is only a single stream */
1825 switch (trace
->streams
->len
) {
1826 case 0: /* Create stream if there was none. */
1827 ret
= ctf_stream_visit(fd
, depth
, NULL
, trace
->root_declaration_scope
, trace
);
1832 event
->stream_id
= 0;
1833 event
->stream
= trace_stream_lookup(trace
, event
->stream_id
);
1837 fprintf(fd
, "[error] %s: missing stream_id field in event declaration\n", __func__
);
1841 /* Allow only one event without id per stream */
1842 if (!CTF_EVENT_FIELD_IS_SET(event
, id
)
1843 && event
->stream
->events_by_id
->len
!= 0) {
1845 fprintf(fd
, "[error] %s: missing id field in event declaration\n", __func__
);
1848 if (event
->stream
->events_by_id
->len
<= event
->id
)
1849 g_ptr_array_set_size(event
->stream
->events_by_id
, event
->id
+ 1);
1850 g_ptr_array_index(event
->stream
->events_by_id
, event
->id
) = event
;
1851 g_hash_table_insert(event
->stream
->event_quark_to_id
,
1852 (gpointer
) (unsigned long) event
->name
,
1854 g_ptr_array_add(trace
->event_declarations
, event_decl
);
1858 if (event
->fields_decl
)
1859 declaration_unref(&event
->fields_decl
->p
);
1860 if (event
->context_decl
)
1861 declaration_unref(&event
->context_decl
->p
);
1862 free_declaration_scope(event
->declaration_scope
);
1869 int ctf_stream_declaration_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_stream_declaration
*stream
, struct ctf_trace
*trace
)
1873 switch (node
->type
) {
1875 ret
= ctf_typedef_visit(fd
, depth
+ 1,
1876 stream
->declaration_scope
,
1877 node
->u
._typedef
.type_specifier_list
,
1878 &node
->u
._typedef
.type_declarators
,
1883 case NODE_TYPEALIAS
:
1884 ret
= ctf_typealias_visit(fd
, depth
+ 1,
1885 stream
->declaration_scope
,
1886 node
->u
.typealias
.target
, node
->u
.typealias
.alias
,
1891 case NODE_CTF_EXPRESSION
:
1895 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
1896 if (!strcmp(left
, "id")) {
1897 if (CTF_STREAM_FIELD_IS_SET(stream
, stream_id
)) {
1898 fprintf(fd
, "[error] %s: id already declared in stream declaration\n", __func__
);
1902 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &stream
->stream_id
);
1904 fprintf(fd
, "[error] %s: unexpected unary expression for stream id\n", __func__
);
1908 CTF_STREAM_SET_FIELD(stream
, stream_id
);
1909 } else if (!strcmp(left
, "event.header")) {
1910 struct declaration
*declaration
;
1912 if (stream
->event_header_decl
) {
1913 fprintf(fd
, "[error] %s: event.header already declared in stream declaration\n", __func__
);
1917 declaration
= ctf_type_specifier_list_visit(fd
, depth
,
1918 _bt_list_first_entry(&node
->u
.ctf_expression
.right
,
1919 struct ctf_node
, siblings
),
1920 stream
->declaration_scope
, trace
);
1925 if (declaration
->id
!= CTF_TYPE_STRUCT
) {
1929 stream
->event_header_decl
= container_of(declaration
, struct declaration_struct
, p
);
1930 } else if (!strcmp(left
, "event.context")) {
1931 struct declaration
*declaration
;
1933 if (stream
->event_context_decl
) {
1934 fprintf(fd
, "[error] %s: event.context already declared in stream declaration\n", __func__
);
1938 declaration
= ctf_type_specifier_list_visit(fd
, depth
,
1939 _bt_list_first_entry(&node
->u
.ctf_expression
.right
,
1940 struct ctf_node
, siblings
),
1941 stream
->declaration_scope
, trace
);
1946 if (declaration
->id
!= CTF_TYPE_STRUCT
) {
1950 stream
->event_context_decl
= container_of(declaration
, struct declaration_struct
, p
);
1951 } else if (!strcmp(left
, "packet.context")) {
1952 struct declaration
*declaration
;
1954 if (stream
->packet_context_decl
) {
1955 fprintf(fd
, "[error] %s: packet.context already declared in stream declaration\n", __func__
);
1959 declaration
= ctf_type_specifier_list_visit(fd
, depth
,
1960 _bt_list_first_entry(&node
->u
.ctf_expression
.right
,
1961 struct ctf_node
, siblings
),
1962 stream
->declaration_scope
, trace
);
1967 if (declaration
->id
!= CTF_TYPE_STRUCT
) {
1971 stream
->packet_context_decl
= container_of(declaration
, struct declaration_struct
, p
);
1973 fprintf(fd
, "[warning] %s: attribute \"%s\" is unknown in stream declaration.\n", __func__
, left
);
1974 /* Fall-through after warning */
1983 /* TODO: declaration specifier should be added. */
1990 int ctf_stream_visit(FILE *fd
, int depth
, struct ctf_node
*node
,
1991 struct declaration_scope
*parent_declaration_scope
, struct ctf_trace
*trace
)
1994 struct ctf_node
*iter
;
1995 struct ctf_stream_declaration
*stream
;
1997 stream
= g_new0(struct ctf_stream_declaration
, 1);
1998 stream
->declaration_scope
= new_declaration_scope(parent_declaration_scope
);
1999 stream
->events_by_id
= g_ptr_array_new();
2000 stream
->event_quark_to_id
= g_hash_table_new(g_direct_hash
, g_direct_equal
);
2001 stream
->streams
= g_ptr_array_new();
2003 bt_list_for_each_entry(iter
, &node
->u
.stream
.declaration_list
, siblings
) {
2004 ret
= ctf_stream_declaration_visit(fd
, depth
+ 1, iter
, stream
, trace
);
2009 if (CTF_STREAM_FIELD_IS_SET(stream
, stream_id
)) {
2010 /* check that packet header has stream_id field. */
2011 if (!trace
->packet_header_decl
2012 || struct_declaration_lookup_field_index(trace
->packet_header_decl
, g_quark_from_static_string("stream_id")) < 0) {
2014 fprintf(fd
, "[error] %s: missing stream_id field in packet header declaration, but stream_id attribute is declared for stream.\n", __func__
);
2018 /* Allow only one id-less stream */
2019 if (trace
->streams
->len
!= 0) {
2021 fprintf(fd
, "[error] %s: missing id field in stream declaration\n", __func__
);
2024 stream
->stream_id
= 0;
2026 if (trace
->streams
->len
<= stream
->stream_id
)
2027 g_ptr_array_set_size(trace
->streams
, stream
->stream_id
+ 1);
2028 g_ptr_array_index(trace
->streams
, stream
->stream_id
) = stream
;
2029 stream
->trace
= trace
;
2034 if (stream
->event_header_decl
)
2035 declaration_unref(&stream
->event_header_decl
->p
);
2036 if (stream
->event_context_decl
)
2037 declaration_unref(&stream
->event_context_decl
->p
);
2038 if (stream
->packet_context_decl
)
2039 declaration_unref(&stream
->packet_context_decl
->p
);
2040 g_ptr_array_free(stream
->streams
, TRUE
);
2041 g_ptr_array_free(stream
->events_by_id
, TRUE
);
2042 g_hash_table_destroy(stream
->event_quark_to_id
);
2043 free_declaration_scope(stream
->declaration_scope
);
2049 int ctf_trace_declaration_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_trace
*trace
)
2053 switch (node
->type
) {
2055 ret
= ctf_typedef_visit(fd
, depth
+ 1,
2056 trace
->declaration_scope
,
2057 node
->u
._typedef
.type_specifier_list
,
2058 &node
->u
._typedef
.type_declarators
,
2063 case NODE_TYPEALIAS
:
2064 ret
= ctf_typealias_visit(fd
, depth
+ 1,
2065 trace
->declaration_scope
,
2066 node
->u
.typealias
.target
, node
->u
.typealias
.alias
,
2071 case NODE_CTF_EXPRESSION
:
2075 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
2076 if (!strcmp(left
, "major")) {
2077 if (CTF_TRACE_FIELD_IS_SET(trace
, major
)) {
2078 fprintf(fd
, "[error] %s: major already declared in trace declaration\n", __func__
);
2082 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &trace
->major
);
2084 fprintf(fd
, "[error] %s: unexpected unary expression for trace major number\n", __func__
);
2088 CTF_TRACE_SET_FIELD(trace
, major
);
2089 } else if (!strcmp(left
, "minor")) {
2090 if (CTF_TRACE_FIELD_IS_SET(trace
, minor
)) {
2091 fprintf(fd
, "[error] %s: minor already declared in trace declaration\n", __func__
);
2095 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &trace
->minor
);
2097 fprintf(fd
, "[error] %s: unexpected unary expression for trace minor number\n", __func__
);
2101 CTF_TRACE_SET_FIELD(trace
, minor
);
2102 } else if (!strcmp(left
, "uuid")) {
2103 unsigned char uuid
[BABELTRACE_UUID_LEN
];
2105 ret
= get_unary_uuid(&node
->u
.ctf_expression
.right
, uuid
);
2107 fprintf(fd
, "[error] %s: unexpected unary expression for trace uuid\n", __func__
);
2111 if (CTF_TRACE_FIELD_IS_SET(trace
, uuid
)
2112 && babeltrace_uuid_compare(uuid
, trace
->uuid
)) {
2113 fprintf(fd
, "[error] %s: uuid mismatch\n", __func__
);
2117 memcpy(trace
->uuid
, uuid
, sizeof(uuid
));
2119 CTF_TRACE_SET_FIELD(trace
, uuid
);
2120 } else if (!strcmp(left
, "byte_order")) {
2121 struct ctf_node
*right
;
2124 right
= _bt_list_first_entry(&node
->u
.ctf_expression
.right
, struct ctf_node
, siblings
);
2125 byte_order
= get_trace_byte_order(fd
, depth
, right
);
2129 if (CTF_TRACE_FIELD_IS_SET(trace
, byte_order
)
2130 && byte_order
!= trace
->byte_order
) {
2131 fprintf(fd
, "[error] %s: endianness mismatch\n", __func__
);
2135 if (byte_order
!= trace
->byte_order
) {
2136 trace
->byte_order
= byte_order
;
2138 * We need to restart
2139 * construction of the
2140 * intermediate representation.
2142 trace
->field_mask
= 0;
2143 CTF_TRACE_SET_FIELD(trace
, byte_order
);
2148 CTF_TRACE_SET_FIELD(trace
, byte_order
);
2149 } else if (!strcmp(left
, "packet.header")) {
2150 struct declaration
*declaration
;
2152 if (trace
->packet_header_decl
) {
2153 fprintf(fd
, "[error] %s: packet.header already declared in trace declaration\n", __func__
);
2157 declaration
= ctf_type_specifier_list_visit(fd
, depth
,
2158 _bt_list_first_entry(&node
->u
.ctf_expression
.right
,
2159 struct ctf_node
, siblings
),
2160 trace
->declaration_scope
, trace
);
2165 if (declaration
->id
!= CTF_TYPE_STRUCT
) {
2169 trace
->packet_header_decl
= container_of(declaration
, struct declaration_struct
, p
);
2171 fprintf(fd
, "[warning] %s: attribute \"%s\" is unknown in trace declaration.\n", __func__
, left
);
2180 /* TODO: declaration specifier should be added. */
2187 int ctf_trace_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_trace
*trace
)
2190 struct ctf_node
*iter
;
2192 if (trace
->declaration_scope
)
2194 trace
->declaration_scope
= new_declaration_scope(trace
->root_declaration_scope
);
2195 trace
->streams
= g_ptr_array_new();
2196 trace
->event_declarations
= g_ptr_array_new();
2197 bt_list_for_each_entry(iter
, &node
->u
.trace
.declaration_list
, siblings
) {
2198 ret
= ctf_trace_declaration_visit(fd
, depth
+ 1, iter
, trace
);
2202 if (!CTF_TRACE_FIELD_IS_SET(trace
, major
)) {
2204 fprintf(fd
, "[error] %s: missing major field in trace declaration\n", __func__
);
2207 if (!CTF_TRACE_FIELD_IS_SET(trace
, minor
)) {
2209 fprintf(fd
, "[error] %s: missing minor field in trace declaration\n", __func__
);
2212 if (!CTF_TRACE_FIELD_IS_SET(trace
, byte_order
)) {
2214 fprintf(fd
, "[error] %s: missing byte_order field in trace declaration\n", __func__
);
2218 if (!CTF_TRACE_FIELD_IS_SET(trace
, byte_order
)) {
2219 /* check that the packet header contains a "magic" field */
2220 if (!trace
->packet_header_decl
2221 || struct_declaration_lookup_field_index(trace
->packet_header_decl
, g_quark_from_static_string("magic")) < 0) {
2223 fprintf(fd
, "[error] %s: missing both byte_order and packet header magic number in trace declaration\n", __func__
);
2230 if (trace
->packet_header_decl
) {
2231 declaration_unref(&trace
->packet_header_decl
->p
);
2232 trace
->packet_header_decl
= NULL
;
2234 g_ptr_array_free(trace
->streams
, TRUE
);
2235 g_ptr_array_free(trace
->event_declarations
, TRUE
);
2236 free_declaration_scope(trace
->declaration_scope
);
2237 trace
->declaration_scope
= NULL
;
2242 int ctf_clock_declaration_visit(FILE *fd
, int depth
, struct ctf_node
*node
,
2243 struct ctf_clock
*clock
, struct ctf_trace
*trace
)
2247 switch (node
->type
) {
2248 case NODE_CTF_EXPRESSION
:
2252 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
2253 if (!strcmp(left
, "name")) {
2256 if (CTF_CLOCK_FIELD_IS_SET(clock
, name
)) {
2257 fprintf(fd
, "[error] %s: name already declared in clock declaration\n", __func__
);
2261 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
2263 fprintf(fd
, "[error] %s: unexpected unary expression for clock name\n", __func__
);
2267 clock
->name
= g_quark_from_string(right
);
2269 CTF_CLOCK_SET_FIELD(clock
, name
);
2270 } else if (!strcmp(left
, "uuid")) {
2274 fprintf(fd
, "[error] %s: uuid already declared in clock declaration\n", __func__
);
2278 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
2280 fprintf(fd
, "[error] %s: unexpected unary expression for clock uuid\n", __func__
);
2284 clock
->uuid
= g_quark_from_string(right
);
2286 } else if (!strcmp(left
, "description")) {
2289 if (clock
->description
) {
2290 fprintf(fd
, "[warning] %s: duplicated clock description\n", __func__
);
2291 goto error
; /* ret is 0, so not an actual error, just warn. */
2293 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
2295 fprintf(fd
, "[warning] %s: unexpected unary expression for clock description\n", __func__
);
2296 goto error
; /* ret is 0, so not an actual error, just warn. */
2298 clock
->description
= right
;
2299 } else if (!strcmp(left
, "freq")) {
2300 if (CTF_CLOCK_FIELD_IS_SET(clock
, freq
)) {
2301 fprintf(fd
, "[error] %s: freq already declared in clock declaration\n", __func__
);
2305 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &clock
->freq
);
2307 fprintf(fd
, "[error] %s: unexpected unary expression for clock freq\n", __func__
);
2311 CTF_CLOCK_SET_FIELD(clock
, freq
);
2312 } else if (!strcmp(left
, "precision")) {
2313 if (clock
->precision
) {
2314 fprintf(fd
, "[error] %s: precision already declared in clock declaration\n", __func__
);
2318 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &clock
->precision
);
2320 fprintf(fd
, "[error] %s: unexpected unary expression for clock precision\n", __func__
);
2324 } else if (!strcmp(left
, "offset_s")) {
2325 if (clock
->offset_s
) {
2326 fprintf(fd
, "[error] %s: offset_s already declared in clock declaration\n", __func__
);
2330 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &clock
->offset_s
);
2332 fprintf(fd
, "[error] %s: unexpected unary expression for clock offset_s\n", __func__
);
2336 } else if (!strcmp(left
, "offset")) {
2337 if (clock
->offset
) {
2338 fprintf(fd
, "[error] %s: offset already declared in clock declaration\n", __func__
);
2342 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &clock
->offset
);
2344 fprintf(fd
, "[error] %s: unexpected unary expression for clock offset\n", __func__
);
2348 } else if (!strcmp(left
, "absolute")) {
2349 struct ctf_node
*right
;
2351 right
= _bt_list_first_entry(&node
->u
.ctf_expression
.right
, struct ctf_node
, siblings
);
2352 ret
= get_boolean(fd
, depth
, right
);
2354 fprintf(fd
, "[error] %s: unexpected \"absolute\" right member\n", __func__
);
2358 clock
->absolute
= ret
;
2360 fprintf(fd
, "[warning] %s: attribute \"%s\" is unknown in clock declaration.\n", __func__
, left
);
2369 /* TODO: declaration specifier should be added. */
2376 int ctf_clock_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_trace
*trace
)
2379 struct ctf_node
*iter
;
2380 struct ctf_clock
*clock
;
2382 clock
= g_new0(struct ctf_clock
, 1);
2383 /* Default clock frequency is set to 1000000000 */
2384 clock
->freq
= 1000000000ULL;
2385 bt_list_for_each_entry(iter
, &node
->u
.clock
.declaration_list
, siblings
) {
2386 ret
= ctf_clock_declaration_visit(fd
, depth
+ 1, iter
, clock
, trace
);
2390 if (opt_clock_force_correlate
) {
2392 * User requested to forcibly correlate the clock
2393 * sources, even if we have no correlatation
2396 if (!clock
->absolute
) {
2397 fprintf(fd
, "[warning] Forcibly correlating trace clock sources (--clock-force-correlate).\n");
2399 clock
->absolute
= 1;
2401 if (!CTF_CLOCK_FIELD_IS_SET(clock
, name
)) {
2403 fprintf(fd
, "[error] %s: missing name field in clock declaration\n", __func__
);
2406 if (g_hash_table_size(trace
->clocks
) > 0) {
2407 fprintf(fd
, "[error] Only CTF traces with a single clock description are supported by this babeltrace version.\n");
2411 trace
->single_clock
= clock
;
2412 g_hash_table_insert(trace
->clocks
, (gpointer
) (unsigned long) clock
->name
, clock
);
2416 g_free(clock
->description
);
2422 void ctf_clock_default(FILE *fd
, int depth
, struct ctf_trace
*trace
)
2424 struct ctf_clock
*clock
;
2426 clock
= g_new0(struct ctf_clock
, 1);
2427 clock
->name
= g_quark_from_string("monotonic");
2429 clock
->description
= g_strdup("Default clock");
2430 /* Default clock frequency is set to 1000000000 */
2431 clock
->freq
= 1000000000ULL;
2432 if (opt_clock_force_correlate
) {
2434 * User requested to forcibly correlate the clock
2435 * sources, even if we have no correlatation
2438 if (!clock
->absolute
) {
2439 fprintf(fd
, "[warning] Forcibly correlating trace clock sources (--clock-force-correlate).\n");
2441 clock
->absolute
= 1;
2443 clock
->absolute
= 0; /* Not an absolute reference across traces */
2446 trace
->single_clock
= clock
;
2447 g_hash_table_insert(trace
->clocks
, (gpointer
) (unsigned long) clock
->name
, clock
);
2451 void clock_free(gpointer data
)
2453 struct ctf_clock
*clock
= data
;
2455 g_free(clock
->description
);
2460 int ctf_callsite_declaration_visit(FILE *fd
, int depth
, struct ctf_node
*node
,
2461 struct ctf_callsite
*callsite
, struct ctf_trace
*trace
)
2465 switch (node
->type
) {
2466 case NODE_CTF_EXPRESSION
:
2470 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
2471 if (!strcmp(left
, "name")) {
2474 if (CTF_CALLSITE_FIELD_IS_SET(callsite
, name
)) {
2475 fprintf(fd
, "[error] %s: name already declared in callsite declaration\n", __func__
);
2479 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
2481 fprintf(fd
, "[error] %s: unexpected unary expression for callsite name\n", __func__
);
2485 callsite
->name
= g_quark_from_string(right
);
2487 CTF_CALLSITE_SET_FIELD(callsite
, name
);
2488 } else if (!strcmp(left
, "func")) {
2491 if (CTF_CALLSITE_FIELD_IS_SET(callsite
, func
)) {
2492 fprintf(fd
, "[error] %s: func already declared in callsite declaration\n", __func__
);
2496 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
2498 fprintf(fd
, "[error] %s: unexpected unary expression for callsite func\n", __func__
);
2502 callsite
->func
= right
;
2503 CTF_CALLSITE_SET_FIELD(callsite
, func
);
2504 } else if (!strcmp(left
, "file")) {
2507 if (CTF_CALLSITE_FIELD_IS_SET(callsite
, file
)) {
2508 fprintf(fd
, "[error] %s: file already declared in callsite declaration\n", __func__
);
2512 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
2514 fprintf(fd
, "[error] %s: unexpected unary expression for callsite file\n", __func__
);
2518 callsite
->file
= right
;
2519 CTF_CALLSITE_SET_FIELD(callsite
, file
);
2520 } else if (!strcmp(left
, "line")) {
2521 if (CTF_CALLSITE_FIELD_IS_SET(callsite
, line
)) {
2522 fprintf(fd
, "[error] %s: line already declared in callsite declaration\n", __func__
);
2526 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &callsite
->line
);
2528 fprintf(fd
, "[error] %s: unexpected unary expression for callsite line\n", __func__
);
2532 CTF_CALLSITE_SET_FIELD(callsite
, line
);
2533 } else if (!strcmp(left
, "ip")) {
2534 if (CTF_CALLSITE_FIELD_IS_SET(callsite
, ip
)) {
2535 fprintf(fd
, "[error] %s: ip already declared in callsite declaration\n", __func__
);
2539 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &callsite
->ip
);
2541 fprintf(fd
, "[error] %s: unexpected unary expression for callsite ip\n", __func__
);
2545 CTF_CALLSITE_SET_FIELD(callsite
, ip
);
2547 fprintf(fd
, "[warning] %s: attribute \"%s\" is unknown in callsite declaration.\n", __func__
, left
);
2556 /* TODO: declaration specifier should be added. */
2563 int ctf_callsite_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_trace
*trace
)
2566 struct ctf_node
*iter
;
2567 struct ctf_callsite
*callsite
;
2568 struct ctf_callsite_dups
*cs_dups
;
2570 callsite
= g_new0(struct ctf_callsite
, 1);
2571 bt_list_for_each_entry(iter
, &node
->u
.callsite
.declaration_list
, siblings
) {
2572 ret
= ctf_callsite_declaration_visit(fd
, depth
+ 1, iter
, callsite
, trace
);
2576 if (!CTF_CALLSITE_FIELD_IS_SET(callsite
, name
)) {
2578 fprintf(fd
, "[error] %s: missing name field in callsite declaration\n", __func__
);
2581 if (!CTF_CALLSITE_FIELD_IS_SET(callsite
, func
)) {
2583 fprintf(fd
, "[error] %s: missing func field in callsite declaration\n", __func__
);
2586 if (!CTF_CALLSITE_FIELD_IS_SET(callsite
, file
)) {
2588 fprintf(fd
, "[error] %s: missing file field in callsite declaration\n", __func__
);
2591 if (!CTF_CALLSITE_FIELD_IS_SET(callsite
, line
)) {
2593 fprintf(fd
, "[error] %s: missing line field in callsite declaration\n", __func__
);
2597 cs_dups
= g_hash_table_lookup(trace
->callsites
,
2598 (gpointer
) (unsigned long) callsite
->name
);
2600 cs_dups
= g_new0(struct ctf_callsite_dups
, 1);
2601 BT_INIT_LIST_HEAD(&cs_dups
->head
);
2602 g_hash_table_insert(trace
->callsites
,
2603 (gpointer
) (unsigned long) callsite
->name
, cs_dups
);
2605 bt_list_add_tail(&callsite
->node
, &cs_dups
->head
);
2609 g_free(callsite
->func
);
2610 g_free(callsite
->file
);
2616 void callsite_free(gpointer data
)
2618 struct ctf_callsite_dups
*cs_dups
= data
;
2619 struct ctf_callsite
*callsite
, *cs_n
;
2621 bt_list_for_each_entry_safe(callsite
, cs_n
, &cs_dups
->head
, node
) {
2622 g_free(callsite
->func
);
2623 g_free(callsite
->file
);
2630 int ctf_env_declaration_visit(FILE *fd
, int depth
, struct ctf_node
*node
,
2631 struct ctf_trace
*trace
)
2634 struct ctf_tracer_env
*env
= &trace
->env
;
2636 switch (node
->type
) {
2637 case NODE_CTF_EXPRESSION
:
2641 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
2642 if (!strcmp(left
, "vpid")) {
2645 if (env
->vpid
!= -1) {
2646 fprintf(fd
, "[error] %s: vpid already declared in env declaration\n", __func__
);
2647 goto error
; /* ret is 0, so not an actual error, just warn. */
2649 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &v
);
2651 fprintf(fd
, "[error] %s: unexpected unary expression for env vpid\n", __func__
);
2652 goto error
; /* ret is 0, so not an actual error, just warn. */
2654 env
->vpid
= (int) v
;
2655 printf_verbose("env.vpid = %d\n", env
->vpid
);
2656 } else if (!strcmp(left
, "procname")) {
2659 if (env
->procname
[0]) {
2660 fprintf(fd
, "[warning] %s: duplicated env procname\n", __func__
);
2661 goto error
; /* ret is 0, so not an actual error, just warn. */
2663 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
2665 fprintf(fd
, "[warning] %s: unexpected unary expression for env procname\n", __func__
);
2666 goto error
; /* ret is 0, so not an actual error, just warn. */
2668 strncpy(env
->procname
, right
, TRACER_ENV_LEN
);
2669 env
->procname
[TRACER_ENV_LEN
- 1] = '\0';
2670 printf_verbose("env.procname = \"%s\"\n", env
->procname
);
2672 } else if (!strcmp(left
, "hostname")) {
2675 if (env
->hostname
[0]) {
2676 fprintf(fd
, "[warning] %s: duplicated env hostname\n", __func__
);
2677 goto error
; /* ret is 0, so not an actual error, just warn. */
2679 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
2681 fprintf(fd
, "[warning] %s: unexpected unary expression for env hostname\n", __func__
);
2682 goto error
; /* ret is 0, so not an actual error, just warn. */
2684 strncpy(env
->hostname
, right
, TRACER_ENV_LEN
);
2685 env
->hostname
[TRACER_ENV_LEN
- 1] = '\0';
2686 printf_verbose("env.hostname = \"%s\"\n", env
->hostname
);
2688 } else if (!strcmp(left
, "domain")) {
2691 if (env
->domain
[0]) {
2692 fprintf(fd
, "[warning] %s: duplicated env domain\n", __func__
);
2693 goto error
; /* ret is 0, so not an actual error, just warn. */
2695 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
2697 fprintf(fd
, "[warning] %s: unexpected unary expression for env domain\n", __func__
);
2698 goto error
; /* ret is 0, so not an actual error, just warn. */
2700 strncpy(env
->domain
, right
, TRACER_ENV_LEN
);
2701 env
->domain
[TRACER_ENV_LEN
- 1] = '\0';
2702 printf_verbose("env.domain = \"%s\"\n", env
->domain
);
2704 } else if (!strcmp(left
, "sysname")) {
2707 if (env
->sysname
[0]) {
2708 fprintf(fd
, "[warning] %s: duplicated env sysname\n", __func__
);
2709 goto error
; /* ret is 0, so not an actual error, just warn. */
2711 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
2713 fprintf(fd
, "[warning] %s: unexpected unary expression for env sysname\n", __func__
);
2714 goto error
; /* ret is 0, so not an actual error, just warn. */
2716 strncpy(env
->sysname
, right
, TRACER_ENV_LEN
);
2717 env
->sysname
[TRACER_ENV_LEN
- 1] = '\0';
2718 printf_verbose("env.sysname = \"%s\"\n", env
->sysname
);
2720 } else if (!strcmp(left
, "kernel_release")) {
2723 if (env
->release
[0]) {
2724 fprintf(fd
, "[warning] %s: duplicated env release\n", __func__
);
2725 goto error
; /* ret is 0, so not an actual error, just warn. */
2727 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
2729 fprintf(fd
, "[warning] %s: unexpected unary expression for env release\n", __func__
);
2730 goto error
; /* ret is 0, so not an actual error, just warn. */
2732 strncpy(env
->release
, right
, TRACER_ENV_LEN
);
2733 env
->release
[TRACER_ENV_LEN
- 1] = '\0';
2734 printf_verbose("env.release = \"%s\"\n", env
->release
);
2736 } else if (!strcmp(left
, "kernel_version")) {
2739 if (env
->version
[0]) {
2740 fprintf(fd
, "[warning] %s: duplicated env version\n", __func__
);
2741 goto error
; /* ret is 0, so not an actual error, just warn. */
2743 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
2745 fprintf(fd
, "[warning] %s: unexpected unary expression for env version\n", __func__
);
2746 goto error
; /* ret is 0, so not an actual error, just warn. */
2748 strncpy(env
->version
, right
, TRACER_ENV_LEN
);
2749 env
->version
[TRACER_ENV_LEN
- 1] = '\0';
2750 printf_verbose("env.version = \"%s\"\n", env
->version
);
2753 if (is_unary_string(&node
->u
.ctf_expression
.right
)) {
2756 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
2757 printf_verbose("env.%s = \"%s\"\n", left
, right
);
2759 } else if (is_unary_unsigned(&node
->u
.ctf_expression
.right
)) {
2763 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &v
);
2765 printf_verbose("env.%s = %" PRIu64
"\n", left
, v
);
2766 } else if (is_unary_signed(&node
->u
.ctf_expression
.right
)) {
2770 ret
= get_unary_signed(&node
->u
.ctf_expression
.right
, &v
);
2772 printf_verbose("env.%s = %" PRId64
"\n", left
, v
);
2774 printf_verbose("%s: attribute \"%s\" has unknown type.\n", __func__
, left
);
2790 int ctf_env_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_trace
*trace
)
2793 struct ctf_node
*iter
;
2795 trace
->env
.vpid
= -1;
2796 trace
->env
.procname
[0] = '\0';
2797 trace
->env
.hostname
[0] = '\0';
2798 trace
->env
.domain
[0] = '\0';
2799 trace
->env
.sysname
[0] = '\0';
2800 trace
->env
.release
[0] = '\0';
2801 trace
->env
.version
[0] = '\0';
2802 bt_list_for_each_entry(iter
, &node
->u
.env
.declaration_list
, siblings
) {
2803 ret
= ctf_env_declaration_visit(fd
, depth
+ 1, iter
, trace
);
2812 int ctf_root_declaration_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_trace
*trace
)
2816 switch (node
->type
) {
2818 ret
= ctf_typedef_visit(fd
, depth
+ 1,
2819 trace
->root_declaration_scope
,
2820 node
->u
._typedef
.type_specifier_list
,
2821 &node
->u
._typedef
.type_declarators
,
2826 case NODE_TYPEALIAS
:
2827 ret
= ctf_typealias_visit(fd
, depth
+ 1,
2828 trace
->root_declaration_scope
,
2829 node
->u
.typealias
.target
, node
->u
.typealias
.alias
,
2834 case NODE_TYPE_SPECIFIER_LIST
:
2836 struct declaration
*declaration
;
2839 * Just add the type specifier to the root scope
2840 * declaration scope. Release local reference.
2842 declaration
= ctf_type_specifier_list_visit(fd
, depth
+ 1,
2843 node
, trace
->root_declaration_scope
, trace
);
2846 declaration_unref(declaration
);
2856 int ctf_visitor_construct_metadata(FILE *fd
, int depth
, struct ctf_node
*node
,
2857 struct ctf_trace
*trace
, int byte_order
)
2860 struct ctf_node
*iter
;
2861 int env_clock_done
= 0;
2863 printf_verbose("CTF visitor: metadata construction... ");
2864 trace
->byte_order
= byte_order
;
2865 trace
->clocks
= g_hash_table_new_full(g_direct_hash
, g_direct_equal
,
2867 trace
->callsites
= g_hash_table_new_full(g_direct_hash
, g_direct_equal
,
2868 NULL
, callsite_free
);
2871 trace
->root_declaration_scope
= new_declaration_scope(NULL
);
2873 switch (node
->type
) {
2875 if (!env_clock_done
) {
2877 * declarations need to query clock hash table,
2878 * so clock need to be treated first.
2880 if (bt_list_empty(&node
->u
.root
.clock
)) {
2881 ctf_clock_default(fd
, depth
+ 1, trace
);
2883 bt_list_for_each_entry(iter
, &node
->u
.root
.clock
, siblings
) {
2884 ret
= ctf_clock_visit(fd
, depth
+ 1, iter
,
2887 fprintf(fd
, "[error] %s: clock declaration error\n", __func__
);
2894 bt_list_for_each_entry(iter
, &node
->u
.root
.declaration_list
,
2896 ret
= ctf_root_declaration_visit(fd
, depth
+ 1, iter
, trace
);
2898 fprintf(fd
, "[error] %s: root declaration error\n", __func__
);
2902 bt_list_for_each_entry(iter
, &node
->u
.root
.trace
, siblings
) {
2903 ret
= ctf_trace_visit(fd
, depth
+ 1, iter
, trace
);
2904 if (ret
== -EINTR
) {
2905 free_declaration_scope(trace
->root_declaration_scope
);
2907 * Need to restart creation of type
2908 * definitions, aliases and
2909 * trace header declarations.
2914 fprintf(fd
, "[error] %s: trace declaration error\n", __func__
);
2918 bt_list_for_each_entry(iter
, &node
->u
.root
.callsite
, siblings
) {
2919 ret
= ctf_callsite_visit(fd
, depth
+ 1, iter
,
2922 fprintf(fd
, "[error] %s: callsite declaration error\n", __func__
);
2926 if (!trace
->streams
) {
2927 fprintf(fd
, "[error] %s: missing trace declaration\n", __func__
);
2931 bt_list_for_each_entry(iter
, &node
->u
.root
.env
, siblings
) {
2932 ret
= ctf_env_visit(fd
, depth
+ 1, iter
, trace
);
2934 fprintf(fd
, "[error] %s: env declaration error\n", __func__
);
2938 bt_list_for_each_entry(iter
, &node
->u
.root
.stream
, siblings
) {
2939 ret
= ctf_stream_visit(fd
, depth
+ 1, iter
,
2940 trace
->root_declaration_scope
, trace
);
2942 fprintf(fd
, "[error] %s: stream declaration error\n", __func__
);
2946 bt_list_for_each_entry(iter
, &node
->u
.root
.event
, siblings
) {
2947 ret
= ctf_event_visit(fd
, depth
+ 1, iter
,
2948 trace
->root_declaration_scope
, trace
);
2950 fprintf(fd
, "[error] %s: event declaration error\n", __func__
);
2957 fprintf(fd
, "[error] %s: unknown node type %d\n", __func__
,
2962 printf_verbose("done.\n");
2966 free_declaration_scope(trace
->root_declaration_scope
);
2967 g_hash_table_destroy(trace
->callsites
);
2968 g_hash_table_destroy(trace
->clocks
);
2972 int ctf_destroy_metadata(struct ctf_trace
*trace
)
2975 struct ctf_file_stream
*metadata_stream
;
2977 if (trace
->streams
) {
2978 for (i
= 0; i
< trace
->streams
->len
; i
++) {
2979 struct ctf_stream_declaration
*stream
;
2982 stream
= g_ptr_array_index(trace
->streams
, i
);
2985 for (j
= 0; j
< stream
->streams
->len
; j
++) {
2986 struct ctf_stream_definition
*stream_def
;
2989 stream_def
= g_ptr_array_index(stream
->streams
, j
);
2992 for (k
= 0; k
< stream_def
->events_by_id
->len
; k
++) {
2993 struct ctf_event_definition
*event
;
2995 event
= g_ptr_array_index(stream_def
->events_by_id
, k
);
2998 if (&event
->event_fields
->p
)
2999 definition_unref(&event
->event_fields
->p
);
3000 if (&event
->event_context
->p
)
3001 definition_unref(&event
->event_context
->p
);
3004 if (&stream_def
->trace_packet_header
->p
)
3005 definition_unref(&stream_def
->trace_packet_header
->p
);
3006 if (&stream_def
->stream_event_header
->p
)
3007 definition_unref(&stream_def
->stream_event_header
->p
);
3008 if (&stream_def
->stream_packet_context
->p
)
3009 definition_unref(&stream_def
->stream_packet_context
->p
);
3010 if (&stream_def
->stream_event_context
->p
)
3011 definition_unref(&stream_def
->stream_event_context
->p
);
3012 g_ptr_array_free(stream_def
->events_by_id
, TRUE
);
3015 if (stream
->event_header_decl
)
3016 declaration_unref(&stream
->event_header_decl
->p
);
3017 if (stream
->event_context_decl
)
3018 declaration_unref(&stream
->event_context_decl
->p
);
3019 if (stream
->packet_context_decl
)
3020 declaration_unref(&stream
->packet_context_decl
->p
);
3021 g_ptr_array_free(stream
->streams
, TRUE
);
3022 g_ptr_array_free(stream
->events_by_id
, TRUE
);
3023 g_hash_table_destroy(stream
->event_quark_to_id
);
3024 free_declaration_scope(stream
->declaration_scope
);
3027 g_ptr_array_free(trace
->streams
, TRUE
);
3030 if (trace
->event_declarations
) {
3031 for (i
= 0; i
< trace
->event_declarations
->len
; i
++) {
3032 struct bt_ctf_event_decl
*event_decl
;
3033 struct ctf_event_declaration
*event
;
3035 event_decl
= g_ptr_array_index(trace
->event_declarations
, i
);
3036 if (event_decl
->context_decl
)
3037 g_ptr_array_free(event_decl
->context_decl
, TRUE
);
3038 if (event_decl
->fields_decl
)
3039 g_ptr_array_free(event_decl
->fields_decl
, TRUE
);
3040 if (event_decl
->packet_header_decl
)
3041 g_ptr_array_free(event_decl
->packet_header_decl
, TRUE
);
3042 if (event_decl
->event_context_decl
)
3043 g_ptr_array_free(event_decl
->event_context_decl
, TRUE
);
3044 if (event_decl
->event_header_decl
)
3045 g_ptr_array_free(event_decl
->event_header_decl
, TRUE
);
3046 if (event_decl
->packet_context_decl
)
3047 g_ptr_array_free(event_decl
->packet_context_decl
, TRUE
);
3049 event
= &event_decl
->parent
;
3050 if (event
->fields_decl
)
3051 declaration_unref(&event
->fields_decl
->p
);
3052 if (event
->context_decl
)
3053 declaration_unref(&event
->context_decl
->p
);
3054 free_declaration_scope(event
->declaration_scope
);
3058 g_ptr_array_free(trace
->event_declarations
, TRUE
);
3060 if (trace
->packet_header_decl
)
3061 declaration_unref(&trace
->packet_header_decl
->p
);
3063 free_declaration_scope(trace
->root_declaration_scope
);
3064 free_declaration_scope(trace
->declaration_scope
);
3066 g_hash_table_destroy(trace
->callsites
);
3067 g_hash_table_destroy(trace
->clocks
);
3069 metadata_stream
= container_of(trace
->metadata
, struct ctf_file_stream
, parent
);
3070 g_free(metadata_stream
);