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.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
35 #include <babeltrace/babeltrace-internal.h>
36 #include <babeltrace/list.h>
37 #include <babeltrace/types.h>
38 #include <babeltrace/ctf/metadata.h>
39 #include <babeltrace/uuid.h>
40 #include <babeltrace/endian.h>
41 #include <babeltrace/ctf/events-internal.h>
42 #include "ctf-scanner.h"
43 #include "ctf-parser.h"
46 #define fprintf_dbg(fd, fmt, args...) fprintf(fd, "%s: " fmt, __func__, ## args)
48 #define _bt_list_first_entry(ptr, type, member) \
49 bt_list_entry((ptr)->next, type, member)
51 struct last_enum_value
{
58 int opt_clock_force_correlate
;
61 struct declaration
*ctf_type_specifier_list_visit(FILE *fd
,
62 int depth
, struct ctf_node
*type_specifier_list
,
63 struct declaration_scope
*declaration_scope
,
64 struct ctf_trace
*trace
);
67 int ctf_stream_visit(FILE *fd
, int depth
, struct ctf_node
*node
,
68 struct declaration_scope
*parent_declaration_scope
, struct ctf_trace
*trace
);
71 int is_unary_string(struct bt_list_head
*head
)
73 struct ctf_node
*node
;
75 bt_list_for_each_entry(node
, head
, siblings
) {
76 if (node
->type
!= NODE_UNARY_EXPRESSION
)
78 if (node
->u
.unary_expression
.type
!= UNARY_STRING
)
85 * String returned must be freed by the caller using g_free.
88 char *concatenate_unary_strings(struct bt_list_head
*head
)
90 struct ctf_node
*node
;
94 str
= g_string_new("");
95 bt_list_for_each_entry(node
, head
, siblings
) {
98 assert(node
->type
== NODE_UNARY_EXPRESSION
);
99 assert(node
->u
.unary_expression
.type
== UNARY_STRING
);
100 assert((node
->u
.unary_expression
.link
== UNARY_LINK_UNKNOWN
)
102 switch (node
->u
.unary_expression
.link
) {
104 g_string_append(str
, ".");
106 case UNARY_ARROWLINK
:
107 g_string_append(str
, "->");
109 case UNARY_DOTDOTDOT
:
110 g_string_append(str
, "...");
115 src_string
= node
->u
.unary_expression
.u
.string
;
116 g_string_append(str
, src_string
);
119 return g_string_free(str
, FALSE
);
123 GQuark
get_map_clock_name_value(struct bt_list_head
*head
)
125 struct ctf_node
*node
;
126 const char *name
= NULL
;
129 bt_list_for_each_entry(node
, head
, siblings
) {
132 assert(node
->type
== NODE_UNARY_EXPRESSION
);
133 assert(node
->u
.unary_expression
.type
== UNARY_STRING
);
134 assert((node
->u
.unary_expression
.link
== UNARY_LINK_UNKNOWN
)
136 /* needs to be chained with . */
137 switch (node
->u
.unary_expression
.link
) {
140 case UNARY_ARROWLINK
:
141 case UNARY_DOTDOTDOT
:
146 src_string
= node
->u
.unary_expression
.u
.string
;
148 case 0: if (strcmp("clock", src_string
) != 0) {
152 case 1: name
= src_string
;
154 case 2: if (strcmp("value", src_string
) != 0) {
159 return 0; /* extra identifier, unknown */
163 return g_quark_from_string(name
);
167 int is_unary_unsigned(struct bt_list_head
*head
)
169 struct ctf_node
*node
;
171 bt_list_for_each_entry(node
, head
, siblings
) {
172 if (node
->type
!= NODE_UNARY_EXPRESSION
)
174 if (node
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
)
181 int get_unary_unsigned(struct bt_list_head
*head
, uint64_t *value
)
183 struct ctf_node
*node
;
186 bt_list_for_each_entry(node
, head
, siblings
) {
187 assert(node
->type
== NODE_UNARY_EXPRESSION
);
188 assert(node
->u
.unary_expression
.type
== UNARY_UNSIGNED_CONSTANT
);
189 assert(node
->u
.unary_expression
.link
== UNARY_LINK_UNKNOWN
);
191 *value
= node
->u
.unary_expression
.u
.unsigned_constant
;
198 int is_unary_signed(struct bt_list_head
*head
)
200 struct ctf_node
*node
;
202 bt_list_for_each_entry(node
, head
, siblings
) {
203 if (node
->type
!= NODE_UNARY_EXPRESSION
)
205 if (node
->u
.unary_expression
.type
!= UNARY_SIGNED_CONSTANT
)
212 int get_unary_signed(struct bt_list_head
*head
, int64_t *value
)
214 struct ctf_node
*node
;
217 bt_list_for_each_entry(node
, head
, siblings
) {
218 assert(node
->type
== NODE_UNARY_EXPRESSION
);
219 assert(node
->u
.unary_expression
.type
== UNARY_UNSIGNED_CONSTANT
220 || node
->u
.unary_expression
.type
== UNARY_SIGNED_CONSTANT
);
221 assert(node
->u
.unary_expression
.link
== UNARY_LINK_UNKNOWN
);
223 switch (node
->u
.unary_expression
.type
) {
224 case UNARY_UNSIGNED_CONSTANT
:
225 *value
= (int64_t) node
->u
.unary_expression
.u
.unsigned_constant
;
227 case UNARY_SIGNED_CONSTANT
:
228 *value
= node
->u
.unary_expression
.u
.signed_constant
;
239 int get_unary_uuid(struct bt_list_head
*head
, unsigned char *uuid
)
241 struct ctf_node
*node
;
245 bt_list_for_each_entry(node
, head
, siblings
) {
246 const char *src_string
;
248 assert(node
->type
== NODE_UNARY_EXPRESSION
);
249 assert(node
->u
.unary_expression
.type
== UNARY_STRING
);
250 assert(node
->u
.unary_expression
.link
== UNARY_LINK_UNKNOWN
);
252 src_string
= node
->u
.unary_expression
.u
.string
;
253 ret
= babeltrace_uuid_parse(src_string
, uuid
);
259 struct ctf_stream_declaration
*trace_stream_lookup(struct ctf_trace
*trace
, uint64_t stream_id
)
261 if (trace
->streams
->len
<= stream_id
)
263 return g_ptr_array_index(trace
->streams
, stream_id
);
267 struct ctf_clock
*trace_clock_lookup(struct ctf_trace
*trace
, GQuark clock_name
)
269 return g_hash_table_lookup(trace
->clocks
, (gpointer
) (unsigned long) clock_name
);
273 int visit_type_specifier(FILE *fd
, struct ctf_node
*type_specifier
, GString
*str
)
275 assert(type_specifier
->type
== NODE_TYPE_SPECIFIER
);
277 switch (type_specifier
->u
.type_specifier
.type
) {
279 g_string_append(str
, "void");
282 g_string_append(str
, "char");
285 g_string_append(str
, "short");
288 g_string_append(str
, "int");
291 g_string_append(str
, "long");
294 g_string_append(str
, "float");
296 case TYPESPEC_DOUBLE
:
297 g_string_append(str
, "double");
299 case TYPESPEC_SIGNED
:
300 g_string_append(str
, "signed");
302 case TYPESPEC_UNSIGNED
:
303 g_string_append(str
, "unsigned");
306 g_string_append(str
, "bool");
308 case TYPESPEC_COMPLEX
:
309 g_string_append(str
, "_Complex");
311 case TYPESPEC_IMAGINARY
:
312 g_string_append(str
, "_Imaginary");
315 g_string_append(str
, "const");
317 case TYPESPEC_ID_TYPE
:
318 if (type_specifier
->u
.type_specifier
.id_type
)
319 g_string_append(str
, type_specifier
->u
.type_specifier
.id_type
);
321 case TYPESPEC_STRUCT
:
323 struct ctf_node
*node
= type_specifier
->u
.type_specifier
.node
;
325 if (!node
->u
._struct
.name
) {
326 fprintf(fd
, "[error] %s: unexpected empty variant name\n", __func__
);
329 g_string_append(str
, "struct ");
330 g_string_append(str
, node
->u
._struct
.name
);
333 case TYPESPEC_VARIANT
:
335 struct ctf_node
*node
= type_specifier
->u
.type_specifier
.node
;
337 if (!node
->u
.variant
.name
) {
338 fprintf(fd
, "[error] %s: unexpected empty variant name\n", __func__
);
341 g_string_append(str
, "variant ");
342 g_string_append(str
, node
->u
.variant
.name
);
347 struct ctf_node
*node
= type_specifier
->u
.type_specifier
.node
;
349 if (!node
->u
._enum
.enum_id
) {
350 fprintf(fd
, "[error] %s: unexpected empty enum ID\n", __func__
);
353 g_string_append(str
, "enum ");
354 g_string_append(str
, node
->u
._enum
.enum_id
);
357 case TYPESPEC_FLOATING_POINT
:
358 case TYPESPEC_INTEGER
:
359 case TYPESPEC_STRING
:
361 fprintf(fd
, "[error] %s: unknown specifier\n", __func__
);
368 int visit_type_specifier_list(FILE *fd
, struct ctf_node
*type_specifier_list
, GString
*str
)
370 struct ctf_node
*iter
;
371 int alias_item_nr
= 0;
374 bt_list_for_each_entry(iter
, &type_specifier_list
->u
.type_specifier_list
.head
, siblings
) {
375 if (alias_item_nr
!= 0)
376 g_string_append(str
, " ");
378 ret
= visit_type_specifier(fd
, iter
, str
);
386 GQuark
create_typealias_identifier(FILE *fd
, int depth
,
387 struct ctf_node
*type_specifier_list
,
388 struct ctf_node
*node_type_declarator
)
390 struct ctf_node
*iter
;
396 str
= g_string_new("");
397 ret
= visit_type_specifier_list(fd
, type_specifier_list
, str
);
399 g_string_free(str
, TRUE
);
402 bt_list_for_each_entry(iter
, &node_type_declarator
->u
.type_declarator
.pointers
, siblings
) {
403 g_string_append(str
, " *");
404 if (iter
->u
.pointer
.const_qualifier
)
405 g_string_append(str
, " const");
407 str_c
= g_string_free(str
, FALSE
);
408 alias_q
= g_quark_from_string(str_c
);
414 struct declaration
*ctf_type_declarator_visit(FILE *fd
, int depth
,
415 struct ctf_node
*type_specifier_list
,
417 struct ctf_node
*node_type_declarator
,
418 struct declaration_scope
*declaration_scope
,
419 struct declaration
*nested_declaration
,
420 struct ctf_trace
*trace
)
423 * Visit type declarator by first taking care of sequence/array
424 * (recursively). Then, when we get to the identifier, take care
428 if (node_type_declarator
) {
429 assert(node_type_declarator
->u
.type_declarator
.type
!= TYPEDEC_UNKNOWN
);
431 /* TODO: gcc bitfields not supported yet. */
432 if (node_type_declarator
->u
.type_declarator
.bitfield_len
!= NULL
) {
433 fprintf(fd
, "[error] %s: gcc bitfields are not supported yet.\n", __func__
);
438 if (!nested_declaration
) {
439 if (node_type_declarator
&& !bt_list_empty(&node_type_declarator
->u
.type_declarator
.pointers
)) {
443 * If we have a pointer declarator, it _has_ to be present in
444 * the typealiases (else fail).
446 alias_q
= create_typealias_identifier(fd
, depth
,
447 type_specifier_list
, node_type_declarator
);
448 nested_declaration
= lookup_declaration(alias_q
, declaration_scope
);
449 if (!nested_declaration
) {
450 fprintf(fd
, "[error] %s: cannot find typealias \"%s\".\n", __func__
, g_quark_to_string(alias_q
));
453 if (nested_declaration
->id
== CTF_TYPE_INTEGER
) {
454 struct declaration_integer
*integer_declaration
=
455 container_of(nested_declaration
, struct declaration_integer
, p
);
456 /* For base to 16 for pointers (expected pretty-print) */
457 if (!integer_declaration
->base
) {
459 * We need to do a copy of the
460 * integer declaration to modify it. There could be other references to
463 integer_declaration
= integer_declaration_new(integer_declaration
->len
,
464 integer_declaration
->byte_order
, integer_declaration
->signedness
,
465 integer_declaration
->p
.alignment
, 16, integer_declaration
->encoding
,
466 integer_declaration
->clock
);
467 nested_declaration
= &integer_declaration
->p
;
471 nested_declaration
= ctf_type_specifier_list_visit(fd
, depth
,
472 type_specifier_list
, declaration_scope
, trace
);
476 if (!node_type_declarator
)
477 return nested_declaration
;
479 if (node_type_declarator
->u
.type_declarator
.type
== TYPEDEC_ID
) {
480 if (node_type_declarator
->u
.type_declarator
.u
.id
)
481 *field_name
= g_quark_from_string(node_type_declarator
->u
.type_declarator
.u
.id
);
484 return nested_declaration
;
486 struct declaration
*declaration
;
487 struct ctf_node
*first
;
491 if (!nested_declaration
) {
492 fprintf(fd
, "[error] %s: nested type is unknown.\n", __func__
);
496 /* create array/sequence, pass nested_declaration as child. */
497 if (bt_list_empty(&node_type_declarator
->u
.type_declarator
.u
.nested
.length
)) {
498 fprintf(fd
, "[error] %s: expecting length field reference or value.\n", __func__
);
501 first
= _bt_list_first_entry(&node_type_declarator
->u
.type_declarator
.u
.nested
.length
,
502 struct ctf_node
, siblings
);
503 assert(first
->type
== NODE_UNARY_EXPRESSION
);
505 switch (first
->u
.unary_expression
.type
) {
506 case UNARY_UNSIGNED_CONSTANT
:
508 struct declaration_array
*array_declaration
;
511 len
= first
->u
.unary_expression
.u
.unsigned_constant
;
512 array_declaration
= bt_array_declaration_new(len
, nested_declaration
,
515 if (!array_declaration
) {
516 fprintf(fd
, "[error] %s: cannot create array declaration.\n", __func__
);
519 bt_declaration_unref(nested_declaration
);
520 declaration
= &array_declaration
->p
;
525 /* Lookup unsigned integer definition, create sequence */
526 char *length_name
= concatenate_unary_strings(&node_type_declarator
->u
.type_declarator
.u
.nested
.length
);
527 struct declaration_sequence
*sequence_declaration
;
529 sequence_declaration
= sequence_declaration_new(length_name
, nested_declaration
, declaration_scope
);
530 if (!sequence_declaration
) {
531 fprintf(fd
, "[error] %s: cannot create sequence declaration.\n", __func__
);
535 bt_declaration_unref(nested_declaration
);
536 declaration
= &sequence_declaration
->p
;
544 /* Pass it as content of outer container */
545 declaration
= ctf_type_declarator_visit(fd
, depth
,
546 type_specifier_list
, field_name
,
547 node_type_declarator
->u
.type_declarator
.u
.nested
.type_declarator
,
548 declaration_scope
, declaration
, trace
);
554 int ctf_struct_type_declarators_visit(FILE *fd
, int depth
,
555 struct declaration_struct
*struct_declaration
,
556 struct ctf_node
*type_specifier_list
,
557 struct bt_list_head
*type_declarators
,
558 struct declaration_scope
*declaration_scope
,
559 struct ctf_trace
*trace
)
561 struct ctf_node
*iter
;
564 bt_list_for_each_entry(iter
, type_declarators
, siblings
) {
565 struct declaration
*field_declaration
;
567 field_declaration
= ctf_type_declarator_visit(fd
, depth
,
570 struct_declaration
->scope
,
572 if (!field_declaration
) {
573 fprintf(fd
, "[error] %s: unable to find struct field declaration type\n", __func__
);
577 /* Check if field with same name already exists */
578 if (struct_declaration_lookup_field_index(struct_declaration
, field_name
) >= 0) {
579 fprintf(fd
, "[error] %s: duplicate field %s in struct\n", __func__
, g_quark_to_string(field_name
));
583 struct_declaration_add_field(struct_declaration
,
584 g_quark_to_string(field_name
),
586 bt_declaration_unref(field_declaration
);
592 int ctf_variant_type_declarators_visit(FILE *fd
, int depth
,
593 struct declaration_untagged_variant
*untagged_variant_declaration
,
594 struct ctf_node
*type_specifier_list
,
595 struct bt_list_head
*type_declarators
,
596 struct declaration_scope
*declaration_scope
,
597 struct ctf_trace
*trace
)
599 struct ctf_node
*iter
;
602 bt_list_for_each_entry(iter
, type_declarators
, siblings
) {
603 struct declaration
*field_declaration
;
605 field_declaration
= ctf_type_declarator_visit(fd
, depth
,
608 untagged_variant_declaration
->scope
,
610 if (!field_declaration
) {
611 fprintf(fd
, "[error] %s: unable to find variant field declaration type\n", __func__
);
615 if (untagged_variant_declaration_get_field_from_tag(untagged_variant_declaration
, field_name
) != NULL
) {
616 fprintf(fd
, "[error] %s: duplicate field %s in variant\n", __func__
, g_quark_to_string(field_name
));
620 untagged_variant_declaration_add_field(untagged_variant_declaration
,
621 g_quark_to_string(field_name
),
623 bt_declaration_unref(field_declaration
);
629 int ctf_typedef_visit(FILE *fd
, int depth
, struct declaration_scope
*scope
,
630 struct ctf_node
*type_specifier_list
,
631 struct bt_list_head
*type_declarators
,
632 struct ctf_trace
*trace
)
634 struct ctf_node
*iter
;
637 bt_list_for_each_entry(iter
, type_declarators
, siblings
) {
638 struct declaration
*type_declaration
;
641 type_declaration
= ctf_type_declarator_visit(fd
, depth
,
645 if (!type_declaration
) {
646 fprintf(fd
, "[error] %s: problem creating type declaration\n", __func__
);
650 * Don't allow typedef and typealias of untagged
653 if (type_declaration
->id
== CTF_TYPE_UNTAGGED_VARIANT
) {
654 fprintf(fd
, "[error] %s: typedef of untagged variant is not permitted.\n", __func__
);
655 bt_declaration_unref(type_declaration
);
658 ret
= register_declaration(identifier
, type_declaration
, scope
);
660 type_declaration
->declaration_free(type_declaration
);
663 bt_declaration_unref(type_declaration
);
669 int ctf_typealias_visit(FILE *fd
, int depth
, struct declaration_scope
*scope
,
670 struct ctf_node
*target
, struct ctf_node
*alias
,
671 struct ctf_trace
*trace
)
673 struct declaration
*type_declaration
;
674 struct ctf_node
*node
;
679 /* See ctf_visitor_type_declarator() in the semantic validator. */
682 * Create target type declaration.
685 if (bt_list_empty(&target
->u
.typealias_target
.type_declarators
))
688 node
= _bt_list_first_entry(&target
->u
.typealias_target
.type_declarators
,
689 struct ctf_node
, siblings
);
690 type_declaration
= ctf_type_declarator_visit(fd
, depth
,
691 target
->u
.typealias_target
.type_specifier_list
,
694 if (!type_declaration
) {
695 fprintf(fd
, "[error] %s: problem creating type declaration\n", __func__
);
700 * Don't allow typedef and typealias of untagged
703 if (type_declaration
->id
== CTF_TYPE_UNTAGGED_VARIANT
) {
704 fprintf(fd
, "[error] %s: typedef of untagged variant is not permitted.\n", __func__
);
705 bt_declaration_unref(type_declaration
);
709 * The semantic validator does not check whether the target is
710 * abstract or not (if it has an identifier). Check it here.
713 fprintf(fd
, "[error] %s: expecting empty identifier\n", __func__
);
718 * Create alias identifier.
721 node
= _bt_list_first_entry(&alias
->u
.typealias_alias
.type_declarators
,
722 struct ctf_node
, siblings
);
723 alias_q
= create_typealias_identifier(fd
, depth
,
724 alias
->u
.typealias_alias
.type_specifier_list
, node
);
725 err
= register_declaration(alias_q
, type_declaration
, scope
);
728 bt_declaration_unref(type_declaration
);
732 if (type_declaration
) {
733 type_declaration
->declaration_free(type_declaration
);
739 int ctf_struct_declaration_list_visit(FILE *fd
, int depth
,
740 struct ctf_node
*iter
, struct declaration_struct
*struct_declaration
,
741 struct ctf_trace
*trace
)
745 switch (iter
->type
) {
747 /* For each declarator, declare type and add type to struct declaration scope */
748 ret
= ctf_typedef_visit(fd
, depth
,
749 struct_declaration
->scope
,
750 iter
->u
._typedef
.type_specifier_list
,
751 &iter
->u
._typedef
.type_declarators
, trace
);
756 /* Declare type with declarator and add type to struct declaration scope */
757 ret
= ctf_typealias_visit(fd
, depth
,
758 struct_declaration
->scope
,
759 iter
->u
.typealias
.target
,
760 iter
->u
.typealias
.alias
, trace
);
764 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
765 /* Add field to structure declaration */
766 ret
= ctf_struct_type_declarators_visit(fd
, depth
,
768 iter
->u
.struct_or_variant_declaration
.type_specifier_list
,
769 &iter
->u
.struct_or_variant_declaration
.type_declarators
,
770 struct_declaration
->scope
, trace
);
775 fprintf(fd
, "[error] %s: unexpected node type %d\n", __func__
, (int) iter
->type
);
782 int ctf_variant_declaration_list_visit(FILE *fd
, int depth
,
783 struct ctf_node
*iter
,
784 struct declaration_untagged_variant
*untagged_variant_declaration
,
785 struct ctf_trace
*trace
)
789 switch (iter
->type
) {
791 /* For each declarator, declare type and add type to variant declaration scope */
792 ret
= ctf_typedef_visit(fd
, depth
,
793 untagged_variant_declaration
->scope
,
794 iter
->u
._typedef
.type_specifier_list
,
795 &iter
->u
._typedef
.type_declarators
, trace
);
800 /* Declare type with declarator and add type to variant declaration scope */
801 ret
= ctf_typealias_visit(fd
, depth
,
802 untagged_variant_declaration
->scope
,
803 iter
->u
.typealias
.target
,
804 iter
->u
.typealias
.alias
, trace
);
808 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
809 /* Add field to structure declaration */
810 ret
= ctf_variant_type_declarators_visit(fd
, depth
,
811 untagged_variant_declaration
,
812 iter
->u
.struct_or_variant_declaration
.type_specifier_list
,
813 &iter
->u
.struct_or_variant_declaration
.type_declarators
,
814 untagged_variant_declaration
->scope
, trace
);
819 fprintf(fd
, "[error] %s: unexpected node type %d\n", __func__
, (int) iter
->type
);
826 struct declaration
*ctf_declaration_struct_visit(FILE *fd
,
827 int depth
, const char *name
, struct bt_list_head
*declaration_list
,
828 int has_body
, struct bt_list_head
*min_align
,
829 struct declaration_scope
*declaration_scope
,
830 struct ctf_trace
*trace
)
832 struct declaration_struct
*struct_declaration
;
833 struct ctf_node
*iter
;
836 * For named struct (without body), lookup in
837 * declaration scope. Don't take reference on struct
838 * declaration: ref is only taken upon definition.
843 lookup_struct_declaration(g_quark_from_string(name
),
845 bt_declaration_ref(&struct_declaration
->p
);
846 return &struct_declaration
->p
;
848 uint64_t min_align_value
= 0;
850 /* For unnamed struct, create type */
851 /* For named struct (with body), create type and add to declaration scope */
853 if (lookup_struct_declaration(g_quark_from_string(name
),
854 declaration_scope
)) {
856 fprintf(fd
, "[error] %s: struct %s already declared in scope\n", __func__
, name
);
860 if (!bt_list_empty(min_align
)) {
863 ret
= get_unary_unsigned(min_align
, &min_align_value
);
865 fprintf(fd
, "[error] %s: unexpected unary expression for structure \"align\" attribute\n", __func__
);
870 struct_declaration
= struct_declaration_new(declaration_scope
,
872 bt_list_for_each_entry(iter
, declaration_list
, siblings
) {
875 ret
= ctf_struct_declaration_list_visit(fd
, depth
+ 1, iter
,
876 struct_declaration
, trace
);
878 goto error_free_declaration
;
883 ret
= register_struct_declaration(g_quark_from_string(name
),
888 return &struct_declaration
->p
;
890 error_free_declaration
:
891 struct_declaration
->p
.declaration_free(&struct_declaration
->p
);
897 struct declaration
*ctf_declaration_variant_visit(FILE *fd
,
898 int depth
, const char *name
, const char *choice
,
899 struct bt_list_head
*declaration_list
,
900 int has_body
, struct declaration_scope
*declaration_scope
,
901 struct ctf_trace
*trace
)
903 struct declaration_untagged_variant
*untagged_variant_declaration
;
904 struct declaration_variant
*variant_declaration
;
905 struct ctf_node
*iter
;
908 * For named variant (without body), lookup in
909 * declaration scope. Don't take reference on variant
910 * declaration: ref is only taken upon definition.
914 untagged_variant_declaration
=
915 lookup_variant_declaration(g_quark_from_string(name
),
917 bt_declaration_ref(&untagged_variant_declaration
->p
);
919 /* For unnamed variant, create type */
920 /* For named variant (with body), create type and add to declaration scope */
922 if (lookup_variant_declaration(g_quark_from_string(name
),
923 declaration_scope
)) {
925 fprintf(fd
, "[error] %s: variant %s already declared in scope\n", __func__
, name
);
929 untagged_variant_declaration
= untagged_variant_declaration_new(declaration_scope
);
930 bt_list_for_each_entry(iter
, declaration_list
, siblings
) {
933 ret
= ctf_variant_declaration_list_visit(fd
, depth
+ 1, iter
,
934 untagged_variant_declaration
, trace
);
941 ret
= register_variant_declaration(g_quark_from_string(name
),
942 untagged_variant_declaration
,
948 * if tagged, create tagged variant and return. else return
952 return &untagged_variant_declaration
->p
;
954 variant_declaration
= variant_declaration_new(untagged_variant_declaration
, choice
);
955 if (!variant_declaration
)
957 bt_declaration_unref(&untagged_variant_declaration
->p
);
958 return &variant_declaration
->p
;
961 untagged_variant_declaration
->p
.declaration_free(&untagged_variant_declaration
->p
);
966 int ctf_enumerator_list_visit(FILE *fd
, int depth
,
967 struct ctf_node
*enumerator
,
968 struct declaration_enum
*enum_declaration
,
969 struct last_enum_value
*last
)
972 struct ctf_node
*iter
;
974 q
= g_quark_from_string(enumerator
->u
.enumerator
.id
);
975 if (enum_declaration
->integer_declaration
->signedness
) {
979 bt_list_for_each_entry(iter
, &enumerator
->u
.enumerator
.values
, siblings
) {
982 assert(iter
->type
== NODE_UNARY_EXPRESSION
);
988 switch (iter
->u
.unary_expression
.type
) {
989 case UNARY_SIGNED_CONSTANT
:
990 *target
= iter
->u
.unary_expression
.u
.signed_constant
;
992 case UNARY_UNSIGNED_CONSTANT
:
993 *target
= iter
->u
.unary_expression
.u
.unsigned_constant
;
996 fprintf(fd
, "[error] %s: invalid enumerator\n", __func__
);
1000 fprintf(fd
, "[error] %s: invalid enumerator\n", __func__
);
1009 last
->u
.s
= end
+ 1;
1010 enum_signed_insert(enum_declaration
, start
, end
, q
);
1012 uint64_t start
, end
;
1015 bt_list_for_each_entry(iter
, &enumerator
->u
.enumerator
.values
, siblings
) {
1018 assert(iter
->type
== NODE_UNARY_EXPRESSION
);
1024 switch (iter
->u
.unary_expression
.type
) {
1025 case UNARY_UNSIGNED_CONSTANT
:
1026 *target
= iter
->u
.unary_expression
.u
.unsigned_constant
;
1028 case UNARY_SIGNED_CONSTANT
:
1030 * We don't accept signed constants for enums with unsigned
1033 fprintf(fd
, "[error] %s: invalid enumerator (signed constant encountered, but enum container type is unsigned)\n", __func__
);
1036 fprintf(fd
, "[error] %s: invalid enumerator\n", __func__
);
1040 fprintf(fd
, "[error] %s: invalid enumerator\n", __func__
);
1049 last
->u
.u
= end
+ 1;
1050 enum_unsigned_insert(enum_declaration
, start
, end
, q
);
1056 struct declaration
*ctf_declaration_enum_visit(FILE *fd
, int depth
,
1058 struct ctf_node
*container_type
,
1059 struct bt_list_head
*enumerator_list
,
1061 struct declaration_scope
*declaration_scope
,
1062 struct ctf_trace
*trace
)
1064 struct declaration
*declaration
;
1065 struct declaration_enum
*enum_declaration
;
1066 struct declaration_integer
*integer_declaration
;
1067 struct last_enum_value last_value
;
1068 struct ctf_node
*iter
;
1072 * For named enum (without body), lookup in
1073 * declaration scope. Don't take reference on enum
1074 * declaration: ref is only taken upon definition.
1079 lookup_enum_declaration(g_quark_from_string(name
),
1081 bt_declaration_ref(&enum_declaration
->p
);
1082 return &enum_declaration
->p
;
1084 /* For unnamed enum, create type */
1085 /* For named enum (with body), create type and add to declaration scope */
1087 if (lookup_enum_declaration(g_quark_from_string(name
),
1088 declaration_scope
)) {
1090 fprintf(fd
, "[error] %s: enum %s already declared in scope\n", __func__
, name
);
1094 if (!container_type
) {
1095 declaration
= lookup_declaration(g_quark_from_static_string("int"),
1098 fprintf(fd
, "[error] %s: \"int\" type declaration missing for enumeration\n", __func__
);
1102 declaration
= ctf_type_declarator_visit(fd
, depth
,
1109 fprintf(fd
, "[error] %s: unable to create container type for enumeration\n", __func__
);
1112 if (declaration
->id
!= CTF_TYPE_INTEGER
) {
1113 fprintf(fd
, "[error] %s: container type for enumeration is not integer\n", __func__
);
1116 integer_declaration
= container_of(declaration
, struct declaration_integer
, p
);
1117 enum_declaration
= enum_declaration_new(integer_declaration
);
1118 bt_declaration_unref(&integer_declaration
->p
); /* leave ref to enum */
1119 if (enum_declaration
->integer_declaration
->signedness
) {
1124 bt_list_for_each_entry(iter
, enumerator_list
, siblings
) {
1127 ret
= ctf_enumerator_list_visit(fd
, depth
+ 1, iter
, enum_declaration
,
1135 ret
= register_enum_declaration(g_quark_from_string(name
),
1139 bt_declaration_unref(&enum_declaration
->p
);
1141 return &enum_declaration
->p
;
1144 enum_declaration
->p
.declaration_free(&enum_declaration
->p
);
1149 struct declaration
*ctf_declaration_type_specifier_visit(FILE *fd
, int depth
,
1150 struct ctf_node
*type_specifier_list
,
1151 struct declaration_scope
*declaration_scope
)
1154 struct declaration
*declaration
;
1159 str
= g_string_new("");
1160 ret
= visit_type_specifier_list(fd
, type_specifier_list
, str
);
1163 str_c
= g_string_free(str
, FALSE
);
1164 id_q
= g_quark_from_string(str_c
);
1166 declaration
= lookup_declaration(id_q
, declaration_scope
);
1167 bt_declaration_ref(declaration
);
1172 * Returns 0/1 boolean, or < 0 on error.
1175 int get_boolean(FILE *fd
, int depth
, struct ctf_node
*unary_expression
)
1177 if (unary_expression
->type
!= NODE_UNARY_EXPRESSION
) {
1178 fprintf(fd
, "[error] %s: expecting unary expression\n",
1182 switch (unary_expression
->u
.unary_expression
.type
) {
1183 case UNARY_UNSIGNED_CONSTANT
:
1184 if (unary_expression
->u
.unary_expression
.u
.unsigned_constant
== 0)
1188 case UNARY_SIGNED_CONSTANT
:
1189 if (unary_expression
->u
.unary_expression
.u
.signed_constant
== 0)
1194 if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "true"))
1196 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "TRUE"))
1198 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "false"))
1200 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "FALSE"))
1203 fprintf(fd
, "[error] %s: unexpected string \"%s\"\n",
1204 __func__
, unary_expression
->u
.unary_expression
.u
.string
);
1209 fprintf(fd
, "[error] %s: unexpected unary expression type\n",
1217 int get_trace_byte_order(FILE *fd
, int depth
, struct ctf_node
*unary_expression
)
1221 if (unary_expression
->u
.unary_expression
.type
!= UNARY_STRING
) {
1222 fprintf(fd
, "[error] %s: byte_order: expecting string\n",
1226 if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "be"))
1227 byte_order
= BIG_ENDIAN
;
1228 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "le"))
1229 byte_order
= LITTLE_ENDIAN
;
1231 fprintf(fd
, "[error] %s: unexpected string \"%s\". Should be \"native\", \"network\", \"be\" or \"le\".\n",
1232 __func__
, unary_expression
->u
.unary_expression
.u
.string
);
1239 int get_byte_order(FILE *fd
, int depth
, struct ctf_node
*unary_expression
,
1240 struct ctf_trace
*trace
)
1244 if (unary_expression
->u
.unary_expression
.type
!= UNARY_STRING
) {
1245 fprintf(fd
, "[error] %s: byte_order: expecting string\n",
1249 if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "native"))
1250 byte_order
= trace
->byte_order
;
1251 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "network"))
1252 byte_order
= BIG_ENDIAN
;
1253 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "be"))
1254 byte_order
= BIG_ENDIAN
;
1255 else if (!strcmp(unary_expression
->u
.unary_expression
.u
.string
, "le"))
1256 byte_order
= LITTLE_ENDIAN
;
1258 fprintf(fd
, "[error] %s: unexpected string \"%s\". Should be \"native\", \"network\", \"be\" or \"le\".\n",
1259 __func__
, unary_expression
->u
.unary_expression
.u
.string
);
1266 struct declaration
*ctf_declaration_integer_visit(FILE *fd
, int depth
,
1267 struct bt_list_head
*expressions
,
1268 struct ctf_trace
*trace
)
1270 struct ctf_node
*expression
;
1271 uint64_t alignment
= 1, size
= 0;
1272 int byte_order
= trace
->byte_order
;
1274 int has_alignment
= 0, has_size
= 0;
1276 enum ctf_string_encoding encoding
= CTF_STRING_NONE
;
1277 struct ctf_clock
*clock
= NULL
;
1278 struct declaration_integer
*integer_declaration
;
1280 bt_list_for_each_entry(expression
, expressions
, siblings
) {
1281 struct ctf_node
*left
, *right
;
1283 left
= _bt_list_first_entry(&expression
->u
.ctf_expression
.left
, struct ctf_node
, siblings
);
1284 right
= _bt_list_first_entry(&expression
->u
.ctf_expression
.right
, struct ctf_node
, siblings
);
1285 assert(left
->u
.unary_expression
.type
== UNARY_STRING
);
1286 if (!strcmp(left
->u
.unary_expression
.u
.string
, "signed")) {
1287 signedness
= get_boolean(fd
, depth
, right
);
1290 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "byte_order")) {
1291 byte_order
= get_byte_order(fd
, depth
, right
, trace
);
1294 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "size")) {
1295 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
1296 fprintf(fd
, "[error] %s: size: expecting unsigned constant\n",
1300 size
= right
->u
.unary_expression
.u
.unsigned_constant
;
1302 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "align")) {
1303 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
1304 fprintf(fd
, "[error] %s: align: expecting unsigned constant\n",
1308 alignment
= right
->u
.unary_expression
.u
.unsigned_constant
;
1309 /* Make sure alignment is a power of two */
1310 if (alignment
== 0 || (alignment
& (alignment
- 1)) != 0) {
1311 fprintf(fd
, "[error] %s: align: expecting power of two\n",
1316 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "base")) {
1317 switch (right
->u
.unary_expression
.type
) {
1318 case UNARY_UNSIGNED_CONSTANT
:
1319 switch (right
->u
.unary_expression
.u
.unsigned_constant
) {
1324 base
= right
->u
.unary_expression
.u
.unsigned_constant
;
1327 fprintf(fd
, "[error] %s: base not supported (%" PRIu64
")\n",
1328 __func__
, right
->u
.unary_expression
.u
.unsigned_constant
);
1334 char *s_right
= concatenate_unary_strings(&expression
->u
.ctf_expression
.right
);
1336 fprintf(fd
, "[error] %s: unexpected unary expression for integer base\n", __func__
);
1340 if (!strcmp(s_right
, "decimal") || !strcmp(s_right
, "dec") || !strcmp(s_right
, "d")
1341 || !strcmp(s_right
, "i") || !strcmp(s_right
, "u")) {
1343 } else if (!strcmp(s_right
, "hexadecimal") || !strcmp(s_right
, "hex")
1344 || !strcmp(s_right
, "x") || !strcmp(s_right
, "X")
1345 || !strcmp(s_right
, "p")) {
1347 } else if (!strcmp(s_right
, "octal") || !strcmp(s_right
, "oct")
1348 || !strcmp(s_right
, "o")) {
1350 } else if (!strcmp(s_right
, "binary") || !strcmp(s_right
, "b")) {
1353 fprintf(fd
, "[error] %s: unexpected expression for integer base (%s)\n", __func__
, s_right
);
1362 fprintf(fd
, "[error] %s: base: expecting unsigned constant or unary string\n",
1366 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "encoding")) {
1369 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
1370 fprintf(fd
, "[error] %s: encoding: expecting unary string\n",
1374 s_right
= concatenate_unary_strings(&expression
->u
.ctf_expression
.right
);
1376 fprintf(fd
, "[error] %s: unexpected unary expression for integer base\n", __func__
);
1380 if (!strcmp(s_right
, "UTF8")
1381 || !strcmp(s_right
, "utf8")
1382 || !strcmp(s_right
, "utf-8")
1383 || !strcmp(s_right
, "UTF-8"))
1384 encoding
= CTF_STRING_UTF8
;
1385 else if (!strcmp(s_right
, "ASCII")
1386 || !strcmp(s_right
, "ascii"))
1387 encoding
= CTF_STRING_ASCII
;
1388 else if (!strcmp(s_right
, "none"))
1389 encoding
= CTF_STRING_NONE
;
1391 fprintf(fd
, "[error] %s: unknown string encoding \"%s\"\n", __func__
, s_right
);
1396 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "map")) {
1399 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
1400 fprintf(fd
, "[error] %s: map: expecting identifier\n",
1404 /* currently only support clock.name.value */
1405 clock_name
= get_map_clock_name_value(&expression
->u
.ctf_expression
.right
);
1409 s_right
= concatenate_unary_strings(&expression
->u
.ctf_expression
.right
);
1411 fprintf(fd
, "[error] %s: unexpected unary expression for integer map\n", __func__
);
1415 fprintf(fd
, "[warning] %s: unknown map %s in integer declaration\n", __func__
,
1420 clock
= trace_clock_lookup(trace
, clock_name
);
1422 fprintf(fd
, "[error] %s: map: unable to find clock %s declaration\n",
1423 __func__
, g_quark_to_string(clock_name
));
1427 fprintf(fd
, "[warning] %s: unknown attribute name %s\n",
1428 __func__
, left
->u
.unary_expression
.u
.string
);
1429 /* Fall-through after warning */
1433 fprintf(fd
, "[error] %s: missing size attribute\n", __func__
);
1436 if (!has_alignment
) {
1437 if (size
% CHAR_BIT
) {
1438 /* bit-packed alignment */
1441 /* byte-packed alignment */
1442 alignment
= CHAR_BIT
;
1445 integer_declaration
= integer_declaration_new(size
,
1446 byte_order
, signedness
, alignment
,
1447 base
, encoding
, clock
);
1448 return &integer_declaration
->p
;
1452 struct declaration
*ctf_declaration_floating_point_visit(FILE *fd
, int depth
,
1453 struct bt_list_head
*expressions
,
1454 struct ctf_trace
*trace
)
1456 struct ctf_node
*expression
;
1457 uint64_t alignment
= 1, exp_dig
= 0, mant_dig
= 0,
1458 byte_order
= trace
->byte_order
;
1459 int has_alignment
= 0, has_exp_dig
= 0, has_mant_dig
= 0;
1460 struct declaration_float
*float_declaration
;
1462 bt_list_for_each_entry(expression
, expressions
, siblings
) {
1463 struct ctf_node
*left
, *right
;
1465 left
= _bt_list_first_entry(&expression
->u
.ctf_expression
.left
, struct ctf_node
, siblings
);
1466 right
= _bt_list_first_entry(&expression
->u
.ctf_expression
.right
, struct ctf_node
, siblings
);
1467 assert(left
->u
.unary_expression
.type
== UNARY_STRING
);
1468 if (!strcmp(left
->u
.unary_expression
.u
.string
, "byte_order")) {
1469 byte_order
= get_byte_order(fd
, depth
, right
, trace
);
1472 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "exp_dig")) {
1473 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
1474 fprintf(fd
, "[error] %s: exp_dig: expecting unsigned constant\n",
1478 exp_dig
= right
->u
.unary_expression
.u
.unsigned_constant
;
1480 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "mant_dig")) {
1481 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
1482 fprintf(fd
, "[error] %s: mant_dig: expecting unsigned constant\n",
1486 mant_dig
= right
->u
.unary_expression
.u
.unsigned_constant
;
1488 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "align")) {
1489 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
1490 fprintf(fd
, "[error] %s: align: expecting unsigned constant\n",
1494 alignment
= right
->u
.unary_expression
.u
.unsigned_constant
;
1495 /* Make sure alignment is a power of two */
1496 if (alignment
== 0 || (alignment
& (alignment
- 1)) != 0) {
1497 fprintf(fd
, "[error] %s: align: expecting power of two\n",
1503 fprintf(fd
, "[warning] %s: unknown attribute name %s\n",
1504 __func__
, left
->u
.unary_expression
.u
.string
);
1505 /* Fall-through after warning */
1508 if (!has_mant_dig
) {
1509 fprintf(fd
, "[error] %s: missing mant_dig attribute\n", __func__
);
1513 fprintf(fd
, "[error] %s: missing exp_dig attribute\n", __func__
);
1516 if (!has_alignment
) {
1517 if ((mant_dig
+ exp_dig
) % CHAR_BIT
) {
1518 /* bit-packed alignment */
1521 /* byte-packed alignment */
1522 alignment
= CHAR_BIT
;
1525 float_declaration
= float_declaration_new(mant_dig
, exp_dig
,
1526 byte_order
, alignment
);
1527 return &float_declaration
->p
;
1531 struct declaration
*ctf_declaration_string_visit(FILE *fd
, int depth
,
1532 struct bt_list_head
*expressions
,
1533 struct ctf_trace
*trace
)
1535 struct ctf_node
*expression
;
1536 const char *encoding_c
= NULL
;
1537 enum ctf_string_encoding encoding
= CTF_STRING_UTF8
;
1538 struct declaration_string
*string_declaration
;
1540 bt_list_for_each_entry(expression
, expressions
, siblings
) {
1541 struct ctf_node
*left
, *right
;
1543 left
= _bt_list_first_entry(&expression
->u
.ctf_expression
.left
, struct ctf_node
, siblings
);
1544 right
= _bt_list_first_entry(&expression
->u
.ctf_expression
.right
, struct ctf_node
, siblings
);
1545 assert(left
->u
.unary_expression
.type
== UNARY_STRING
);
1546 if (!strcmp(left
->u
.unary_expression
.u
.string
, "encoding")) {
1547 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
1548 fprintf(fd
, "[error] %s: encoding: expecting string\n",
1552 encoding_c
= right
->u
.unary_expression
.u
.string
;
1554 fprintf(fd
, "[warning] %s: unknown attribute name %s\n",
1555 __func__
, left
->u
.unary_expression
.u
.string
);
1556 /* Fall-through after warning */
1559 if (encoding_c
&& !strcmp(encoding_c
, "ASCII"))
1560 encoding
= CTF_STRING_ASCII
;
1561 string_declaration
= string_declaration_new(encoding
);
1562 return &string_declaration
->p
;
1567 struct declaration
*ctf_type_specifier_list_visit(FILE *fd
,
1568 int depth
, struct ctf_node
*type_specifier_list
,
1569 struct declaration_scope
*declaration_scope
,
1570 struct ctf_trace
*trace
)
1572 struct ctf_node
*first
;
1573 struct ctf_node
*node
;
1575 assert(type_specifier_list
->type
== NODE_TYPE_SPECIFIER_LIST
);
1577 first
= _bt_list_first_entry(&type_specifier_list
->u
.type_specifier_list
.head
, struct ctf_node
, siblings
);
1579 assert(first
->type
== NODE_TYPE_SPECIFIER
);
1581 node
= first
->u
.type_specifier
.node
;
1583 switch (first
->u
.type_specifier
.type
) {
1584 case TYPESPEC_FLOATING_POINT
:
1585 return ctf_declaration_floating_point_visit(fd
, depth
,
1586 &node
->u
.floating_point
.expressions
, trace
);
1587 case TYPESPEC_INTEGER
:
1588 return ctf_declaration_integer_visit(fd
, depth
,
1589 &node
->u
.integer
.expressions
, trace
);
1590 case TYPESPEC_STRING
:
1591 return ctf_declaration_string_visit(fd
, depth
,
1592 &node
->u
.string
.expressions
, trace
);
1593 case TYPESPEC_STRUCT
:
1594 return ctf_declaration_struct_visit(fd
, depth
,
1595 node
->u
._struct
.name
,
1596 &node
->u
._struct
.declaration_list
,
1597 node
->u
._struct
.has_body
,
1598 &node
->u
._struct
.min_align
,
1601 case TYPESPEC_VARIANT
:
1602 return ctf_declaration_variant_visit(fd
, depth
,
1603 node
->u
.variant
.name
,
1604 node
->u
.variant
.choice
,
1605 &node
->u
.variant
.declaration_list
,
1606 node
->u
.variant
.has_body
,
1610 return ctf_declaration_enum_visit(fd
, depth
,
1611 node
->u
._enum
.enum_id
,
1612 node
->u
._enum
.container_type
,
1613 &node
->u
._enum
.enumerator_list
,
1614 node
->u
._enum
.has_body
,
1620 case TYPESPEC_SHORT
:
1623 case TYPESPEC_FLOAT
:
1624 case TYPESPEC_DOUBLE
:
1625 case TYPESPEC_SIGNED
:
1626 case TYPESPEC_UNSIGNED
:
1628 case TYPESPEC_COMPLEX
:
1629 case TYPESPEC_IMAGINARY
:
1630 case TYPESPEC_CONST
:
1631 case TYPESPEC_ID_TYPE
:
1632 return ctf_declaration_type_specifier_visit(fd
, depth
,
1633 type_specifier_list
, declaration_scope
);
1635 fprintf(fd
, "[error] %s: unexpected node type %d\n", __func__
, (int) first
->u
.type_specifier
.type
);
1641 int ctf_event_declaration_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_event_declaration
*event
, struct ctf_trace
*trace
)
1645 switch (node
->type
) {
1647 ret
= ctf_typedef_visit(fd
, depth
+ 1,
1648 event
->declaration_scope
,
1649 node
->u
._typedef
.type_specifier_list
,
1650 &node
->u
._typedef
.type_declarators
,
1655 case NODE_TYPEALIAS
:
1656 ret
= ctf_typealias_visit(fd
, depth
+ 1,
1657 event
->declaration_scope
,
1658 node
->u
.typealias
.target
, node
->u
.typealias
.alias
,
1663 case NODE_CTF_EXPRESSION
:
1667 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
1668 if (!strcmp(left
, "name")) {
1671 if (CTF_EVENT_FIELD_IS_SET(event
, name
)) {
1672 fprintf(fd
, "[error] %s: name already declared in event declaration\n", __func__
);
1676 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
1678 fprintf(fd
, "[error] %s: unexpected unary expression for event name\n", __func__
);
1682 event
->name
= g_quark_from_string(right
);
1684 CTF_EVENT_SET_FIELD(event
, name
);
1685 } else if (!strcmp(left
, "id")) {
1686 if (CTF_EVENT_FIELD_IS_SET(event
, id
)) {
1687 fprintf(fd
, "[error] %s: id already declared in event declaration\n", __func__
);
1691 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &event
->id
);
1693 fprintf(fd
, "[error] %s: unexpected unary expression for event id\n", __func__
);
1697 CTF_EVENT_SET_FIELD(event
, id
);
1698 } else if (!strcmp(left
, "stream_id")) {
1699 if (CTF_EVENT_FIELD_IS_SET(event
, stream_id
)) {
1700 fprintf(fd
, "[error] %s: stream_id already declared in event declaration\n", __func__
);
1704 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &event
->stream_id
);
1706 fprintf(fd
, "[error] %s: unexpected unary expression for event stream_id\n", __func__
);
1710 event
->stream
= trace_stream_lookup(trace
, event
->stream_id
);
1711 if (!event
->stream
) {
1712 fprintf(fd
, "[error] %s: stream id %" PRIu64
" cannot be found\n", __func__
, event
->stream_id
);
1716 CTF_EVENT_SET_FIELD(event
, stream_id
);
1717 } else if (!strcmp(left
, "context")) {
1718 struct declaration
*declaration
;
1720 if (event
->context_decl
) {
1721 fprintf(fd
, "[error] %s: context already declared in event declaration\n", __func__
);
1725 declaration
= ctf_type_specifier_list_visit(fd
, depth
,
1726 _bt_list_first_entry(&node
->u
.ctf_expression
.right
,
1727 struct ctf_node
, siblings
),
1728 event
->declaration_scope
, trace
);
1733 if (declaration
->id
!= CTF_TYPE_STRUCT
) {
1737 event
->context_decl
= container_of(declaration
, struct declaration_struct
, p
);
1738 } else if (!strcmp(left
, "fields")) {
1739 struct declaration
*declaration
;
1741 if (event
->fields_decl
) {
1742 fprintf(fd
, "[error] %s: fields already declared in event declaration\n", __func__
);
1746 declaration
= ctf_type_specifier_list_visit(fd
, depth
,
1747 _bt_list_first_entry(&node
->u
.ctf_expression
.right
,
1748 struct ctf_node
, siblings
),
1749 event
->declaration_scope
, trace
);
1754 if (declaration
->id
!= CTF_TYPE_STRUCT
) {
1758 event
->fields_decl
= container_of(declaration
, struct declaration_struct
, p
);
1759 } else if (!strcmp(left
, "loglevel")) {
1760 int64_t loglevel
= -1;
1762 if (CTF_EVENT_FIELD_IS_SET(event
, loglevel
)) {
1763 fprintf(fd
, "[error] %s: loglevel already declared in event declaration\n", __func__
);
1767 ret
= get_unary_signed(&node
->u
.ctf_expression
.right
, &loglevel
);
1769 fprintf(fd
, "[error] %s: unexpected unary expression for event loglevel\n", __func__
);
1773 event
->loglevel
= (int) loglevel
;
1774 CTF_EVENT_SET_FIELD(event
, loglevel
);
1775 } else if (!strcmp(left
, "model.emf.uri")) {
1778 if (CTF_EVENT_FIELD_IS_SET(event
, model_emf_uri
)) {
1779 fprintf(fd
, "[error] %s: model.emf.uri already declared in event declaration\n", __func__
);
1783 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
1785 fprintf(fd
, "[error] %s: unexpected unary expression for event model.emf.uri\n", __func__
);
1789 event
->model_emf_uri
= g_quark_from_string(right
);
1791 CTF_EVENT_SET_FIELD(event
, model_emf_uri
);
1793 fprintf(fd
, "[warning] %s: attribute \"%s\" is unknown in event declaration.\n", __func__
, left
);
1794 /* Fall-through after warning */
1802 /* TODO: declaration specifier should be added. */
1809 int ctf_event_visit(FILE *fd
, int depth
, struct ctf_node
*node
,
1810 struct declaration_scope
*parent_declaration_scope
, struct ctf_trace
*trace
)
1813 struct ctf_node
*iter
;
1814 struct ctf_event_declaration
*event
;
1815 struct bt_ctf_event_decl
*event_decl
;
1817 event_decl
= g_new0(struct bt_ctf_event_decl
, 1);
1818 event
= &event_decl
->parent
;
1819 event
->declaration_scope
= new_declaration_scope(parent_declaration_scope
);
1820 event
->loglevel
= -1;
1821 bt_list_for_each_entry(iter
, &node
->u
.event
.declaration_list
, siblings
) {
1822 ret
= ctf_event_declaration_visit(fd
, depth
+ 1, iter
, event
, trace
);
1826 if (!CTF_EVENT_FIELD_IS_SET(event
, name
)) {
1828 fprintf(fd
, "[error] %s: missing name field in event declaration\n", __func__
);
1831 if (!CTF_EVENT_FIELD_IS_SET(event
, stream_id
)) {
1832 /* Allow missing stream_id if there is only a single stream */
1833 switch (trace
->streams
->len
) {
1834 case 0: /* Create stream if there was none. */
1835 ret
= ctf_stream_visit(fd
, depth
, NULL
, trace
->root_declaration_scope
, trace
);
1840 event
->stream_id
= 0;
1841 event
->stream
= trace_stream_lookup(trace
, event
->stream_id
);
1845 fprintf(fd
, "[error] %s: missing stream_id field in event declaration\n", __func__
);
1849 /* Allow only one event without id per stream */
1850 if (!CTF_EVENT_FIELD_IS_SET(event
, id
)
1851 && event
->stream
->events_by_id
->len
!= 0) {
1853 fprintf(fd
, "[error] %s: missing id field in event declaration\n", __func__
);
1856 if (event
->stream
->events_by_id
->len
<= event
->id
)
1857 g_ptr_array_set_size(event
->stream
->events_by_id
, event
->id
+ 1);
1858 g_ptr_array_index(event
->stream
->events_by_id
, event
->id
) = event
;
1859 g_hash_table_insert(event
->stream
->event_quark_to_id
,
1860 (gpointer
) (unsigned long) event
->name
,
1862 g_ptr_array_add(trace
->event_declarations
, event_decl
);
1866 if (event
->fields_decl
)
1867 bt_declaration_unref(&event
->fields_decl
->p
);
1868 if (event
->context_decl
)
1869 bt_declaration_unref(&event
->context_decl
->p
);
1870 free_declaration_scope(event
->declaration_scope
);
1877 int ctf_stream_declaration_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_stream_declaration
*stream
, struct ctf_trace
*trace
)
1881 switch (node
->type
) {
1883 ret
= ctf_typedef_visit(fd
, depth
+ 1,
1884 stream
->declaration_scope
,
1885 node
->u
._typedef
.type_specifier_list
,
1886 &node
->u
._typedef
.type_declarators
,
1891 case NODE_TYPEALIAS
:
1892 ret
= ctf_typealias_visit(fd
, depth
+ 1,
1893 stream
->declaration_scope
,
1894 node
->u
.typealias
.target
, node
->u
.typealias
.alias
,
1899 case NODE_CTF_EXPRESSION
:
1903 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
1904 if (!strcmp(left
, "id")) {
1905 if (CTF_STREAM_FIELD_IS_SET(stream
, stream_id
)) {
1906 fprintf(fd
, "[error] %s: id already declared in stream declaration\n", __func__
);
1910 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &stream
->stream_id
);
1912 fprintf(fd
, "[error] %s: unexpected unary expression for stream id\n", __func__
);
1916 CTF_STREAM_SET_FIELD(stream
, stream_id
);
1917 } else if (!strcmp(left
, "event.header")) {
1918 struct declaration
*declaration
;
1920 if (stream
->event_header_decl
) {
1921 fprintf(fd
, "[error] %s: event.header already declared in stream declaration\n", __func__
);
1925 declaration
= ctf_type_specifier_list_visit(fd
, depth
,
1926 _bt_list_first_entry(&node
->u
.ctf_expression
.right
,
1927 struct ctf_node
, siblings
),
1928 stream
->declaration_scope
, trace
);
1933 if (declaration
->id
!= CTF_TYPE_STRUCT
) {
1937 stream
->event_header_decl
= container_of(declaration
, struct declaration_struct
, p
);
1938 } else if (!strcmp(left
, "event.context")) {
1939 struct declaration
*declaration
;
1941 if (stream
->event_context_decl
) {
1942 fprintf(fd
, "[error] %s: event.context already declared in stream declaration\n", __func__
);
1946 declaration
= ctf_type_specifier_list_visit(fd
, depth
,
1947 _bt_list_first_entry(&node
->u
.ctf_expression
.right
,
1948 struct ctf_node
, siblings
),
1949 stream
->declaration_scope
, trace
);
1954 if (declaration
->id
!= CTF_TYPE_STRUCT
) {
1958 stream
->event_context_decl
= container_of(declaration
, struct declaration_struct
, p
);
1959 } else if (!strcmp(left
, "packet.context")) {
1960 struct declaration
*declaration
;
1962 if (stream
->packet_context_decl
) {
1963 fprintf(fd
, "[error] %s: packet.context already declared in stream declaration\n", __func__
);
1967 declaration
= ctf_type_specifier_list_visit(fd
, depth
,
1968 _bt_list_first_entry(&node
->u
.ctf_expression
.right
,
1969 struct ctf_node
, siblings
),
1970 stream
->declaration_scope
, trace
);
1975 if (declaration
->id
!= CTF_TYPE_STRUCT
) {
1979 stream
->packet_context_decl
= container_of(declaration
, struct declaration_struct
, p
);
1981 fprintf(fd
, "[warning] %s: attribute \"%s\" is unknown in stream declaration.\n", __func__
, left
);
1982 /* Fall-through after warning */
1991 /* TODO: declaration specifier should be added. */
1998 int ctf_stream_visit(FILE *fd
, int depth
, struct ctf_node
*node
,
1999 struct declaration_scope
*parent_declaration_scope
, struct ctf_trace
*trace
)
2002 struct ctf_node
*iter
;
2003 struct ctf_stream_declaration
*stream
;
2005 stream
= g_new0(struct ctf_stream_declaration
, 1);
2006 stream
->declaration_scope
= new_declaration_scope(parent_declaration_scope
);
2007 stream
->events_by_id
= g_ptr_array_new();
2008 stream
->event_quark_to_id
= g_hash_table_new(g_direct_hash
, g_direct_equal
);
2009 stream
->streams
= g_ptr_array_new();
2011 bt_list_for_each_entry(iter
, &node
->u
.stream
.declaration_list
, siblings
) {
2012 ret
= ctf_stream_declaration_visit(fd
, depth
+ 1, iter
, stream
, trace
);
2017 if (CTF_STREAM_FIELD_IS_SET(stream
, stream_id
)) {
2018 /* check that packet header has stream_id field. */
2019 if (!trace
->packet_header_decl
2020 || struct_declaration_lookup_field_index(trace
->packet_header_decl
, g_quark_from_static_string("stream_id")) < 0) {
2022 fprintf(fd
, "[error] %s: missing stream_id field in packet header declaration, but stream_id attribute is declared for stream.\n", __func__
);
2026 /* Allow only one id-less stream */
2027 if (trace
->streams
->len
!= 0) {
2029 fprintf(fd
, "[error] %s: missing id field in stream declaration\n", __func__
);
2032 stream
->stream_id
= 0;
2034 if (trace
->streams
->len
<= stream
->stream_id
)
2035 g_ptr_array_set_size(trace
->streams
, stream
->stream_id
+ 1);
2036 g_ptr_array_index(trace
->streams
, stream
->stream_id
) = stream
;
2037 stream
->trace
= trace
;
2042 if (stream
->event_header_decl
)
2043 bt_declaration_unref(&stream
->event_header_decl
->p
);
2044 if (stream
->event_context_decl
)
2045 bt_declaration_unref(&stream
->event_context_decl
->p
);
2046 if (stream
->packet_context_decl
)
2047 bt_declaration_unref(&stream
->packet_context_decl
->p
);
2048 g_ptr_array_free(stream
->streams
, TRUE
);
2049 g_ptr_array_free(stream
->events_by_id
, TRUE
);
2050 g_hash_table_destroy(stream
->event_quark_to_id
);
2051 free_declaration_scope(stream
->declaration_scope
);
2057 int ctf_trace_declaration_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_trace
*trace
)
2061 switch (node
->type
) {
2063 ret
= ctf_typedef_visit(fd
, depth
+ 1,
2064 trace
->declaration_scope
,
2065 node
->u
._typedef
.type_specifier_list
,
2066 &node
->u
._typedef
.type_declarators
,
2071 case NODE_TYPEALIAS
:
2072 ret
= ctf_typealias_visit(fd
, depth
+ 1,
2073 trace
->declaration_scope
,
2074 node
->u
.typealias
.target
, node
->u
.typealias
.alias
,
2079 case NODE_CTF_EXPRESSION
:
2083 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
2084 if (!strcmp(left
, "major")) {
2085 if (CTF_TRACE_FIELD_IS_SET(trace
, major
)) {
2086 fprintf(fd
, "[error] %s: major already declared in trace declaration\n", __func__
);
2090 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &trace
->major
);
2092 fprintf(fd
, "[error] %s: unexpected unary expression for trace major number\n", __func__
);
2096 CTF_TRACE_SET_FIELD(trace
, major
);
2097 } else if (!strcmp(left
, "minor")) {
2098 if (CTF_TRACE_FIELD_IS_SET(trace
, minor
)) {
2099 fprintf(fd
, "[error] %s: minor already declared in trace declaration\n", __func__
);
2103 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &trace
->minor
);
2105 fprintf(fd
, "[error] %s: unexpected unary expression for trace minor number\n", __func__
);
2109 CTF_TRACE_SET_FIELD(trace
, minor
);
2110 } else if (!strcmp(left
, "uuid")) {
2111 unsigned char uuid
[BABELTRACE_UUID_LEN
];
2113 ret
= get_unary_uuid(&node
->u
.ctf_expression
.right
, uuid
);
2115 fprintf(fd
, "[error] %s: unexpected unary expression for trace uuid\n", __func__
);
2119 if (CTF_TRACE_FIELD_IS_SET(trace
, uuid
)
2120 && babeltrace_uuid_compare(uuid
, trace
->uuid
)) {
2121 fprintf(fd
, "[error] %s: uuid mismatch\n", __func__
);
2125 memcpy(trace
->uuid
, uuid
, sizeof(uuid
));
2127 CTF_TRACE_SET_FIELD(trace
, uuid
);
2128 } else if (!strcmp(left
, "byte_order")) {
2129 struct ctf_node
*right
;
2132 right
= _bt_list_first_entry(&node
->u
.ctf_expression
.right
, struct ctf_node
, siblings
);
2133 byte_order
= get_trace_byte_order(fd
, depth
, right
);
2137 if (CTF_TRACE_FIELD_IS_SET(trace
, byte_order
)
2138 && byte_order
!= trace
->byte_order
) {
2139 fprintf(fd
, "[error] %s: endianness mismatch\n", __func__
);
2143 if (byte_order
!= trace
->byte_order
) {
2144 trace
->byte_order
= byte_order
;
2146 * We need to restart
2147 * construction of the
2148 * intermediate representation.
2150 trace
->field_mask
= 0;
2151 CTF_TRACE_SET_FIELD(trace
, byte_order
);
2156 CTF_TRACE_SET_FIELD(trace
, byte_order
);
2157 } else if (!strcmp(left
, "packet.header")) {
2158 struct declaration
*declaration
;
2160 if (trace
->packet_header_decl
) {
2161 fprintf(fd
, "[error] %s: packet.header already declared in trace declaration\n", __func__
);
2165 declaration
= ctf_type_specifier_list_visit(fd
, depth
,
2166 _bt_list_first_entry(&node
->u
.ctf_expression
.right
,
2167 struct ctf_node
, siblings
),
2168 trace
->declaration_scope
, trace
);
2173 if (declaration
->id
!= CTF_TYPE_STRUCT
) {
2177 trace
->packet_header_decl
= container_of(declaration
, struct declaration_struct
, p
);
2179 fprintf(fd
, "[warning] %s: attribute \"%s\" is unknown in trace declaration.\n", __func__
, left
);
2188 /* TODO: declaration specifier should be added. */
2195 int ctf_trace_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_trace
*trace
)
2198 struct ctf_node
*iter
;
2200 if (trace
->declaration_scope
)
2202 trace
->declaration_scope
= new_declaration_scope(trace
->root_declaration_scope
);
2203 trace
->streams
= g_ptr_array_new();
2204 trace
->event_declarations
= g_ptr_array_new();
2205 bt_list_for_each_entry(iter
, &node
->u
.trace
.declaration_list
, siblings
) {
2206 ret
= ctf_trace_declaration_visit(fd
, depth
+ 1, iter
, trace
);
2210 if (!CTF_TRACE_FIELD_IS_SET(trace
, major
)) {
2212 fprintf(fd
, "[error] %s: missing major field in trace declaration\n", __func__
);
2215 if (!CTF_TRACE_FIELD_IS_SET(trace
, minor
)) {
2217 fprintf(fd
, "[error] %s: missing minor field in trace declaration\n", __func__
);
2220 if (!CTF_TRACE_FIELD_IS_SET(trace
, byte_order
)) {
2222 fprintf(fd
, "[error] %s: missing byte_order field in trace declaration\n", __func__
);
2226 if (!CTF_TRACE_FIELD_IS_SET(trace
, byte_order
)) {
2227 /* check that the packet header contains a "magic" field */
2228 if (!trace
->packet_header_decl
2229 || struct_declaration_lookup_field_index(trace
->packet_header_decl
, g_quark_from_static_string("magic")) < 0) {
2231 fprintf(fd
, "[error] %s: missing both byte_order and packet header magic number in trace declaration\n", __func__
);
2238 if (trace
->packet_header_decl
) {
2239 bt_declaration_unref(&trace
->packet_header_decl
->p
);
2240 trace
->packet_header_decl
= NULL
;
2242 g_ptr_array_free(trace
->streams
, TRUE
);
2243 g_ptr_array_free(trace
->event_declarations
, TRUE
);
2244 free_declaration_scope(trace
->declaration_scope
);
2245 trace
->declaration_scope
= NULL
;
2250 int ctf_clock_declaration_visit(FILE *fd
, int depth
, struct ctf_node
*node
,
2251 struct ctf_clock
*clock
, struct ctf_trace
*trace
)
2255 switch (node
->type
) {
2256 case NODE_CTF_EXPRESSION
:
2260 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
2261 if (!strcmp(left
, "name")) {
2264 if (CTF_CLOCK_FIELD_IS_SET(clock
, name
)) {
2265 fprintf(fd
, "[error] %s: name already declared in clock declaration\n", __func__
);
2269 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
2271 fprintf(fd
, "[error] %s: unexpected unary expression for clock name\n", __func__
);
2275 clock
->name
= g_quark_from_string(right
);
2277 CTF_CLOCK_SET_FIELD(clock
, name
);
2278 } else if (!strcmp(left
, "uuid")) {
2282 fprintf(fd
, "[error] %s: uuid already declared in clock declaration\n", __func__
);
2286 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
2288 fprintf(fd
, "[error] %s: unexpected unary expression for clock uuid\n", __func__
);
2292 clock
->uuid
= g_quark_from_string(right
);
2294 } else if (!strcmp(left
, "description")) {
2297 if (clock
->description
) {
2298 fprintf(fd
, "[warning] %s: duplicated clock description\n", __func__
);
2299 goto error
; /* ret is 0, so not an actual error, just warn. */
2301 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
2303 fprintf(fd
, "[warning] %s: unexpected unary expression for clock description\n", __func__
);
2304 goto error
; /* ret is 0, so not an actual error, just warn. */
2306 clock
->description
= right
;
2307 } else if (!strcmp(left
, "freq")) {
2308 if (CTF_CLOCK_FIELD_IS_SET(clock
, freq
)) {
2309 fprintf(fd
, "[error] %s: freq already declared in clock declaration\n", __func__
);
2313 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &clock
->freq
);
2315 fprintf(fd
, "[error] %s: unexpected unary expression for clock freq\n", __func__
);
2319 CTF_CLOCK_SET_FIELD(clock
, freq
);
2320 } else if (!strcmp(left
, "precision")) {
2321 if (clock
->precision
) {
2322 fprintf(fd
, "[error] %s: precision already declared in clock declaration\n", __func__
);
2326 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &clock
->precision
);
2328 fprintf(fd
, "[error] %s: unexpected unary expression for clock precision\n", __func__
);
2332 } else if (!strcmp(left
, "offset_s")) {
2333 if (clock
->offset_s
) {
2334 fprintf(fd
, "[error] %s: offset_s already declared in clock declaration\n", __func__
);
2338 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &clock
->offset_s
);
2340 fprintf(fd
, "[error] %s: unexpected unary expression for clock offset_s\n", __func__
);
2344 } else if (!strcmp(left
, "offset")) {
2345 if (clock
->offset
) {
2346 fprintf(fd
, "[error] %s: offset already declared in clock declaration\n", __func__
);
2350 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &clock
->offset
);
2352 fprintf(fd
, "[error] %s: unexpected unary expression for clock offset\n", __func__
);
2356 } else if (!strcmp(left
, "absolute")) {
2357 struct ctf_node
*right
;
2359 right
= _bt_list_first_entry(&node
->u
.ctf_expression
.right
, struct ctf_node
, siblings
);
2360 ret
= get_boolean(fd
, depth
, right
);
2362 fprintf(fd
, "[error] %s: unexpected \"absolute\" right member\n", __func__
);
2366 clock
->absolute
= ret
;
2368 fprintf(fd
, "[warning] %s: attribute \"%s\" is unknown in clock declaration.\n", __func__
, left
);
2377 /* TODO: declaration specifier should be added. */
2384 int ctf_clock_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_trace
*trace
)
2387 struct ctf_node
*iter
;
2388 struct ctf_clock
*clock
;
2390 clock
= g_new0(struct ctf_clock
, 1);
2391 /* Default clock frequency is set to 1000000000 */
2392 clock
->freq
= 1000000000ULL;
2393 bt_list_for_each_entry(iter
, &node
->u
.clock
.declaration_list
, siblings
) {
2394 ret
= ctf_clock_declaration_visit(fd
, depth
+ 1, iter
, clock
, trace
);
2398 if (opt_clock_force_correlate
) {
2400 * User requested to forcibly correlate the clock
2401 * sources, even if we have no correlatation
2404 if (!clock
->absolute
) {
2405 fprintf(fd
, "[warning] Forcibly correlating trace clock sources (--clock-force-correlate).\n");
2407 clock
->absolute
= 1;
2409 if (!CTF_CLOCK_FIELD_IS_SET(clock
, name
)) {
2411 fprintf(fd
, "[error] %s: missing name field in clock declaration\n", __func__
);
2414 if (g_hash_table_size(trace
->clocks
) > 0) {
2415 fprintf(fd
, "[error] Only CTF traces with a single clock description are supported by this babeltrace version.\n");
2419 trace
->single_clock
= clock
;
2420 g_hash_table_insert(trace
->clocks
, (gpointer
) (unsigned long) clock
->name
, clock
);
2424 g_free(clock
->description
);
2430 void ctf_clock_default(FILE *fd
, int depth
, struct ctf_trace
*trace
)
2432 struct ctf_clock
*clock
;
2434 clock
= g_new0(struct ctf_clock
, 1);
2435 clock
->name
= g_quark_from_string("monotonic");
2437 clock
->description
= g_strdup("Default clock");
2438 /* Default clock frequency is set to 1000000000 */
2439 clock
->freq
= 1000000000ULL;
2440 if (opt_clock_force_correlate
) {
2442 * User requested to forcibly correlate the clock
2443 * sources, even if we have no correlatation
2446 if (!clock
->absolute
) {
2447 fprintf(fd
, "[warning] Forcibly correlating trace clock sources (--clock-force-correlate).\n");
2449 clock
->absolute
= 1;
2451 clock
->absolute
= 0; /* Not an absolute reference across traces */
2454 trace
->single_clock
= clock
;
2455 g_hash_table_insert(trace
->clocks
, (gpointer
) (unsigned long) clock
->name
, clock
);
2459 void clock_free(gpointer data
)
2461 struct ctf_clock
*clock
= data
;
2463 g_free(clock
->description
);
2468 int ctf_callsite_declaration_visit(FILE *fd
, int depth
, struct ctf_node
*node
,
2469 struct ctf_callsite
*callsite
, struct ctf_trace
*trace
)
2473 switch (node
->type
) {
2474 case NODE_CTF_EXPRESSION
:
2478 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
2479 if (!strcmp(left
, "name")) {
2482 if (CTF_CALLSITE_FIELD_IS_SET(callsite
, name
)) {
2483 fprintf(fd
, "[error] %s: name already declared in callsite declaration\n", __func__
);
2487 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
2489 fprintf(fd
, "[error] %s: unexpected unary expression for callsite name\n", __func__
);
2493 callsite
->name
= g_quark_from_string(right
);
2495 CTF_CALLSITE_SET_FIELD(callsite
, name
);
2496 } else if (!strcmp(left
, "func")) {
2499 if (CTF_CALLSITE_FIELD_IS_SET(callsite
, func
)) {
2500 fprintf(fd
, "[error] %s: func already declared in callsite declaration\n", __func__
);
2504 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
2506 fprintf(fd
, "[error] %s: unexpected unary expression for callsite func\n", __func__
);
2510 callsite
->func
= right
;
2511 CTF_CALLSITE_SET_FIELD(callsite
, func
);
2512 } else if (!strcmp(left
, "file")) {
2515 if (CTF_CALLSITE_FIELD_IS_SET(callsite
, file
)) {
2516 fprintf(fd
, "[error] %s: file already declared in callsite declaration\n", __func__
);
2520 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
2522 fprintf(fd
, "[error] %s: unexpected unary expression for callsite file\n", __func__
);
2526 callsite
->file
= right
;
2527 CTF_CALLSITE_SET_FIELD(callsite
, file
);
2528 } else if (!strcmp(left
, "line")) {
2529 if (CTF_CALLSITE_FIELD_IS_SET(callsite
, line
)) {
2530 fprintf(fd
, "[error] %s: line already declared in callsite declaration\n", __func__
);
2534 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &callsite
->line
);
2536 fprintf(fd
, "[error] %s: unexpected unary expression for callsite line\n", __func__
);
2540 CTF_CALLSITE_SET_FIELD(callsite
, line
);
2541 } else if (!strcmp(left
, "ip")) {
2542 if (CTF_CALLSITE_FIELD_IS_SET(callsite
, ip
)) {
2543 fprintf(fd
, "[error] %s: ip already declared in callsite declaration\n", __func__
);
2547 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &callsite
->ip
);
2549 fprintf(fd
, "[error] %s: unexpected unary expression for callsite ip\n", __func__
);
2553 CTF_CALLSITE_SET_FIELD(callsite
, ip
);
2555 fprintf(fd
, "[warning] %s: attribute \"%s\" is unknown in callsite declaration.\n", __func__
, left
);
2564 /* TODO: declaration specifier should be added. */
2571 int ctf_callsite_visit(FILE *fd
, int depth
, struct ctf_node
*node
, struct ctf_trace
*trace
)
2574 struct ctf_node
*iter
;
2575 struct ctf_callsite
*callsite
;
2576 struct ctf_callsite_dups
*cs_dups
;
2578 callsite
= g_new0(struct ctf_callsite
, 1);
2579 bt_list_for_each_entry(iter
, &node
->u
.callsite
.declaration_list
, siblings
) {
2580 ret
= ctf_callsite_declaration_visit(fd
, depth
+ 1, iter
, callsite
, trace
);
2584 if (!CTF_CALLSITE_FIELD_IS_SET(callsite
, name
)) {
2586 fprintf(fd
, "[error] %s: missing name field in callsite declaration\n", __func__
);
2589 if (!CTF_CALLSITE_FIELD_IS_SET(callsite
, func
)) {
2591 fprintf(fd
, "[error] %s: missing func field in callsite declaration\n", __func__
);
2594 if (!CTF_CALLSITE_FIELD_IS_SET(callsite
, file
)) {
2596 fprintf(fd
, "[error] %s: missing file field in callsite declaration\n", __func__
);
2599 if (!CTF_CALLSITE_FIELD_IS_SET(callsite
, line
)) {
2601 fprintf(fd
, "[error] %s: missing line field in callsite declaration\n", __func__
);
2605 cs_dups
= g_hash_table_lookup(trace
->callsites
,
2606 (gpointer
) (unsigned long) callsite
->name
);
2608 cs_dups
= g_new0(struct ctf_callsite_dups
, 1);
2609 BT_INIT_LIST_HEAD(&cs_dups
->head
);
2610 g_hash_table_insert(trace
->callsites
,
2611 (gpointer
) (unsigned long) callsite
->name
, cs_dups
);
2613 bt_list_add_tail(&callsite
->node
, &cs_dups
->head
);
2617 g_free(callsite
->func
);
2618 g_free(callsite
->file
);
2624 void callsite_free(gpointer data
)
2626 struct ctf_callsite_dups
*cs_dups
= data
;
2627 struct ctf_callsite
*callsite
, *cs_n
;
2629 bt_list_for_each_entry_safe(callsite
, cs_n
, &cs_dups
->head
, node
) {
2630 g_free(callsite
->func
);
2631 g_free(callsite
->file
);
2638 int ctf_env_declaration_visit(FILE *fd
, int depth
, struct ctf_node
*node
,
2639 struct ctf_trace
*trace
)
2642 struct ctf_tracer_env
*env
= &trace
->env
;
2644 switch (node
->type
) {
2645 case NODE_CTF_EXPRESSION
:
2649 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
2650 if (!strcmp(left
, "vpid")) {
2653 if (env
->vpid
!= -1) {
2654 fprintf(fd
, "[error] %s: vpid already declared in env declaration\n", __func__
);
2655 goto error
; /* ret is 0, so not an actual error, just warn. */
2657 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
, &v
);
2659 fprintf(fd
, "[error] %s: unexpected unary expression for env vpid\n", __func__
);
2660 goto error
; /* ret is 0, so not an actual error, just warn. */
2662 env
->vpid
= (int) v
;
2663 printf_verbose("env.vpid = %d\n", env
->vpid
);
2664 } else if (!strcmp(left
, "procname")) {
2667 if (env
->procname
[0]) {
2668 fprintf(fd
, "[warning] %s: duplicated env procname\n", __func__
);
2669 goto error
; /* ret is 0, so not an actual error, just warn. */
2671 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
2673 fprintf(fd
, "[warning] %s: unexpected unary expression for env procname\n", __func__
);
2674 goto error
; /* ret is 0, so not an actual error, just warn. */
2676 strncpy(env
->procname
, right
, TRACER_ENV_LEN
);
2677 env
->procname
[TRACER_ENV_LEN
- 1] = '\0';
2678 printf_verbose("env.procname = \"%s\"\n", env
->procname
);
2680 } else if (!strcmp(left
, "hostname")) {
2683 if (env
->hostname
[0]) {
2684 fprintf(fd
, "[warning] %s: duplicated env hostname\n", __func__
);
2685 goto error
; /* ret is 0, so not an actual error, just warn. */
2687 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
2689 fprintf(fd
, "[warning] %s: unexpected unary expression for env hostname\n", __func__
);
2690 goto error
; /* ret is 0, so not an actual error, just warn. */
2692 strncpy(env
->hostname
, right
, TRACER_ENV_LEN
);
2693 env
->hostname
[TRACER_ENV_LEN
- 1] = '\0';
2694 printf_verbose("env.hostname = \"%s\"\n", env
->hostname
);
2696 } else if (!strcmp(left
, "domain")) {
2699 if (env
->domain
[0]) {
2700 fprintf(fd
, "[warning] %s: duplicated env domain\n", __func__
);
2701 goto error
; /* ret is 0, so not an actual error, just warn. */
2703 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
2705 fprintf(fd
, "[warning] %s: unexpected unary expression for env domain\n", __func__
);
2706 goto error
; /* ret is 0, so not an actual error, just warn. */
2708 strncpy(env
->domain
, right
, TRACER_ENV_LEN
);
2709 env
->domain
[TRACER_ENV_LEN
- 1] = '\0';
2710 printf_verbose("env.domain = \"%s\"\n", env
->domain
);
2712 } else if (!strcmp(left
, "sysname")) {
2715 if (env
->sysname
[0]) {
2716 fprintf(fd
, "[warning] %s: duplicated env sysname\n", __func__
);
2717 goto error
; /* ret is 0, so not an actual error, just warn. */
2719 right
= concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
2721 fprintf(fd
, "[warning] %s: unexpected unary expression for env sysname\n", __func__
);
2722 goto error
; /* ret is 0, so not an actual error, just warn. */
2724 strncpy(env
->sysname
, right
, TRACER_ENV_LEN
);
2725 env
->sysname
[TRACER_ENV_LEN
- 1] = '\0';
2726 printf_verbose("env.sysname = \"%s\"\n", env
->sysname
);
2728 } else if (!strcmp(left
, "kernel_release")) {
2731 if (env
->release
[0]) {
2732 fprintf(fd
, "[warning] %s: duplicated env release<