CTF: fix packet indexing
[babeltrace.git] / formats / ctf / metadata / ctf-visitor-generate-io-struct.c
1 /*
2 * ctf-visitor-generate-io-struct.c
3 *
4 * Common Trace Format Metadata Visitor (generate I/O structures).
5 *
6 * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 *
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:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 */
18
19 #include <stdio.h>
20 #include <unistd.h>
21 #include <string.h>
22 #include <stdlib.h>
23 #include <assert.h>
24 #include <glib.h>
25 #include <inttypes.h>
26 #include <endian.h>
27 #include <errno.h>
28 #include <babeltrace/babeltrace.h>
29 #include <babeltrace/list.h>
30 #include <babeltrace/types.h>
31 #include <babeltrace/ctf/metadata.h>
32 #include <uuid/uuid.h>
33 #include "ctf-scanner.h"
34 #include "ctf-parser.h"
35 #include "ctf-ast.h"
36
37 #define fprintf_dbg(fd, fmt, args...) fprintf(fd, "%s: " fmt, __func__, ## args)
38
39 #define _cds_list_first_entry(ptr, type, member) \
40 cds_list_entry((ptr)->next, type, member)
41
42 static
43 struct declaration *ctf_type_specifier_list_visit(FILE *fd,
44 int depth, struct ctf_node *type_specifier_list,
45 struct declaration_scope *declaration_scope,
46 struct ctf_trace *trace);
47
48 static
49 int ctf_stream_visit(FILE *fd, int depth, struct ctf_node *node,
50 struct declaration_scope *parent_declaration_scope, struct ctf_trace *trace);
51
52 /*
53 * String returned must be freed by the caller using g_free.
54 */
55 static
56 char *concatenate_unary_strings(struct cds_list_head *head)
57 {
58 struct ctf_node *node;
59 GString *str;
60 int i = 0;
61
62 str = g_string_new("");
63 cds_list_for_each_entry(node, head, siblings) {
64 char *src_string;
65
66 assert(node->type == NODE_UNARY_EXPRESSION);
67 assert(node->u.unary_expression.type == UNARY_STRING);
68 assert((node->u.unary_expression.link == UNARY_LINK_UNKNOWN)
69 ^ (i != 0));
70 switch (node->u.unary_expression.link) {
71 case UNARY_DOTLINK:
72 g_string_append(str, ".");
73 break;
74 case UNARY_ARROWLINK:
75 g_string_append(str, "->");
76 break;
77 case UNARY_DOTDOTDOT:
78 g_string_append(str, "...");
79 break;
80 default:
81 break;
82 }
83 src_string = node->u.unary_expression.u.string;
84 g_string_append(str, src_string);
85 i++;
86 }
87 return g_string_free(str, FALSE);
88 }
89
90 static
91 int get_unary_unsigned(struct cds_list_head *head, uint64_t *value)
92 {
93 struct ctf_node *node;
94 int i = 0;
95
96 cds_list_for_each_entry(node, head, siblings) {
97 assert(node->type == NODE_UNARY_EXPRESSION);
98 assert(node->u.unary_expression.type == UNARY_UNSIGNED_CONSTANT);
99 assert(node->u.unary_expression.link == UNARY_LINK_UNKNOWN);
100 assert(i == 0);
101 *value = node->u.unary_expression.u.unsigned_constant;
102 i++;
103 }
104 return 0;
105 }
106
107 static
108 int get_unary_uuid(struct cds_list_head *head, uuid_t *uuid)
109 {
110 struct ctf_node *node;
111 int i = 0;
112 int ret = -1;
113
114 cds_list_for_each_entry(node, head, siblings) {
115 const char *src_string;
116
117 assert(node->type == NODE_UNARY_EXPRESSION);
118 assert(node->u.unary_expression.type == UNARY_STRING);
119 assert(node->u.unary_expression.link == UNARY_LINK_UNKNOWN);
120 assert(i == 0);
121 src_string = node->u.unary_expression.u.string;
122 ret = uuid_parse(node->u.unary_expression.u.string, *uuid);
123 }
124 return ret;
125 }
126
127 static
128 struct ctf_stream_class *trace_stream_lookup(struct ctf_trace *trace, uint64_t stream_id)
129 {
130 if (trace->streams->len <= stream_id)
131 return NULL;
132 return g_ptr_array_index(trace->streams, stream_id);
133 }
134
135 static
136 int visit_type_specifier(FILE *fd, struct ctf_node *type_specifier, GString *str)
137 {
138 assert(type_specifier->type == NODE_TYPE_SPECIFIER);
139
140 switch (type_specifier->u.type_specifier.type) {
141 case TYPESPEC_VOID:
142 g_string_append(str, "void");
143 break;
144 case TYPESPEC_CHAR:
145 g_string_append(str, "char");
146 break;
147 case TYPESPEC_SHORT:
148 g_string_append(str, "short");
149 break;
150 case TYPESPEC_INT:
151 g_string_append(str, "int");
152 break;
153 case TYPESPEC_LONG:
154 g_string_append(str, "long");
155 break;
156 case TYPESPEC_FLOAT:
157 g_string_append(str, "float");
158 break;
159 case TYPESPEC_DOUBLE:
160 g_string_append(str, "double");
161 break;
162 case TYPESPEC_SIGNED:
163 g_string_append(str, "signed");
164 break;
165 case TYPESPEC_UNSIGNED:
166 g_string_append(str, "unsigned");
167 break;
168 case TYPESPEC_BOOL:
169 g_string_append(str, "bool");
170 break;
171 case TYPESPEC_COMPLEX:
172 g_string_append(str, "_Complex");
173 break;
174 case TYPESPEC_IMAGINARY:
175 g_string_append(str, "_Imaginary");
176 break;
177 case TYPESPEC_CONST:
178 g_string_append(str, "const");
179 break;
180 case TYPESPEC_ID_TYPE:
181 if (type_specifier->u.type_specifier.id_type)
182 g_string_append(str, type_specifier->u.type_specifier.id_type);
183 break;
184 case TYPESPEC_STRUCT:
185 {
186 struct ctf_node *node = type_specifier->u.type_specifier.node;
187
188 if (!node->u._struct.name) {
189 fprintf(fd, "[error] %s: unexpected empty variant name\n", __func__);
190 return -EINVAL;
191 }
192 g_string_append(str, "struct ");
193 g_string_append(str, node->u._struct.name);
194 break;
195 }
196 case TYPESPEC_VARIANT:
197 {
198 struct ctf_node *node = type_specifier->u.type_specifier.node;
199
200 if (!node->u.variant.name) {
201 fprintf(fd, "[error] %s: unexpected empty variant name\n", __func__);
202 return -EINVAL;
203 }
204 g_string_append(str, "variant ");
205 g_string_append(str, node->u.variant.name);
206 break;
207 }
208 case TYPESPEC_ENUM:
209 {
210 struct ctf_node *node = type_specifier->u.type_specifier.node;
211
212 if (!node->u._enum.enum_id) {
213 fprintf(fd, "[error] %s: unexpected empty enum ID\n", __func__);
214 return -EINVAL;
215 }
216 g_string_append(str, "enum ");
217 g_string_append(str, node->u._enum.enum_id);
218 break;
219 }
220 case TYPESPEC_FLOATING_POINT:
221 case TYPESPEC_INTEGER:
222 case TYPESPEC_STRING:
223 default:
224 fprintf(fd, "[error] %s: unknown specifier\n", __func__);
225 return -EINVAL;
226 }
227 return 0;
228 }
229
230 static
231 int visit_type_specifier_list(FILE *fd, struct ctf_node *type_specifier_list, GString *str)
232 {
233 struct ctf_node *iter;
234 int alias_item_nr = 0;
235 int ret;
236
237 cds_list_for_each_entry(iter, &type_specifier_list->u.type_specifier_list.head, siblings) {
238 if (alias_item_nr != 0)
239 g_string_append(str, " ");
240 alias_item_nr++;
241 ret = visit_type_specifier(fd, iter, str);
242 if (ret)
243 return ret;
244 }
245 return 0;
246 }
247
248 static
249 GQuark create_typealias_identifier(FILE *fd, int depth,
250 struct ctf_node *type_specifier_list,
251 struct ctf_node *node_type_declarator)
252 {
253 struct ctf_node *iter;
254 GString *str;
255 char *str_c;
256 GQuark alias_q;
257 int ret;
258
259 str = g_string_new("");
260 ret = visit_type_specifier_list(fd, type_specifier_list, str);
261 if (ret) {
262 g_string_free(str, TRUE);
263 return 0;
264 }
265 cds_list_for_each_entry(iter, &node_type_declarator->u.type_declarator.pointers, siblings) {
266 g_string_append(str, " *");
267 if (iter->u.pointer.const_qualifier)
268 g_string_append(str, " const");
269 }
270 str_c = g_string_free(str, FALSE);
271 alias_q = g_quark_from_string(str_c);
272 g_free(str_c);
273 return alias_q;
274 }
275
276 static
277 struct declaration *ctf_type_declarator_visit(FILE *fd, int depth,
278 struct ctf_node *type_specifier_list,
279 GQuark *field_name,
280 struct ctf_node *node_type_declarator,
281 struct declaration_scope *declaration_scope,
282 struct declaration *nested_declaration,
283 struct ctf_trace *trace)
284 {
285 /*
286 * Visit type declarator by first taking care of sequence/array
287 * (recursively). Then, when we get to the identifier, take care
288 * of pointers.
289 */
290
291 if (node_type_declarator) {
292 assert(node_type_declarator->u.type_declarator.type != TYPEDEC_UNKNOWN);
293
294 /* TODO: gcc bitfields not supported yet. */
295 if (node_type_declarator->u.type_declarator.bitfield_len != NULL) {
296 fprintf(fd, "[error] %s: gcc bitfields are not supported yet.\n", __func__);
297 return NULL;
298 }
299 }
300
301 if (!nested_declaration) {
302 if (node_type_declarator && !cds_list_empty(&node_type_declarator->u.type_declarator.pointers)) {
303 GQuark alias_q;
304
305 /*
306 * If we have a pointer declarator, it _has_ to be present in
307 * the typealiases (else fail).
308 */
309 alias_q = create_typealias_identifier(fd, depth,
310 type_specifier_list, node_type_declarator);
311 nested_declaration = lookup_declaration(alias_q, declaration_scope);
312 if (!nested_declaration) {
313 fprintf(fd, "[error] %s: cannot find typealias \"%s\".\n", __func__, g_quark_to_string(alias_q));
314 return NULL;
315 }
316 if (nested_declaration->id == CTF_TYPE_INTEGER) {
317 struct declaration_integer *integer_declaration =
318 container_of(nested_declaration, struct declaration_integer, p);
319 /* For base to 16 for pointers (expected pretty-print) */
320 if (!integer_declaration->base) {
321 /*
322 * We need to do a copy of the
323 * integer declaration to modify it. There could be other references to
324 * it.
325 */
326 integer_declaration = integer_declaration_new(integer_declaration->len,
327 integer_declaration->byte_order, integer_declaration->signedness,
328 integer_declaration->p.alignment, 16, integer_declaration->encoding);
329 nested_declaration = &integer_declaration->p;
330 }
331 }
332 } else {
333 nested_declaration = ctf_type_specifier_list_visit(fd, depth,
334 type_specifier_list, declaration_scope, trace);
335 }
336 }
337
338 if (!node_type_declarator)
339 return nested_declaration;
340
341 if (node_type_declarator->u.type_declarator.type == TYPEDEC_ID) {
342 if (node_type_declarator->u.type_declarator.u.id)
343 *field_name = g_quark_from_string(node_type_declarator->u.type_declarator.u.id);
344 else
345 *field_name = 0;
346 return nested_declaration;
347 } else {
348 struct declaration *declaration;
349 struct ctf_node *first;
350
351 /* TYPEDEC_NESTED */
352
353 if (!nested_declaration) {
354 fprintf(fd, "[error] %s: nested type is unknown.\n", __func__);
355 return NULL;
356 }
357
358 /* create array/sequence, pass nested_declaration as child. */
359 if (cds_list_empty(&node_type_declarator->u.type_declarator.u.nested.length)) {
360 fprintf(fd, "[error] %s: expecting length field reference or value.\n", __func__);
361 return NULL;
362 }
363 first = _cds_list_first_entry(&node_type_declarator->u.type_declarator.u.nested.length,
364 struct ctf_node, siblings);
365 assert(first->type == NODE_UNARY_EXPRESSION);
366
367 switch (first->u.unary_expression.type) {
368 case UNARY_UNSIGNED_CONSTANT:
369 {
370 struct declaration_array *array_declaration;
371 size_t len;
372
373 len = first->u.unary_expression.u.unsigned_constant;
374 array_declaration = array_declaration_new(len, nested_declaration,
375 declaration_scope);
376
377 if (!array_declaration) {
378 fprintf(fd, "[error] %s: cannot create array declaration.\n", __func__);
379 return NULL;
380 }
381 declaration = &array_declaration->p;
382 break;
383 }
384 case UNARY_STRING:
385 {
386 /* Lookup unsigned integer definition, create sequence */
387 char *length_name = concatenate_unary_strings(&node_type_declarator->u.type_declarator.u.nested.length);
388 struct declaration_sequence *sequence_declaration;
389
390 sequence_declaration = sequence_declaration_new(length_name, nested_declaration, declaration_scope);
391 if (!sequence_declaration) {
392 fprintf(fd, "[error] %s: cannot create sequence declaration.\n", __func__);
393 return NULL;
394 }
395 declaration = &sequence_declaration->p;
396 break;
397 }
398 default:
399 assert(0);
400 }
401
402 /* Pass it as content of outer container */
403 declaration = ctf_type_declarator_visit(fd, depth,
404 type_specifier_list, field_name,
405 node_type_declarator->u.type_declarator.u.nested.type_declarator,
406 declaration_scope, declaration, trace);
407 return declaration;
408 }
409 }
410
411 static
412 int ctf_struct_type_declarators_visit(FILE *fd, int depth,
413 struct declaration_struct *struct_declaration,
414 struct ctf_node *type_specifier_list,
415 struct cds_list_head *type_declarators,
416 struct declaration_scope *declaration_scope,
417 struct ctf_trace *trace)
418 {
419 struct ctf_node *iter;
420 GQuark field_name;
421
422 cds_list_for_each_entry(iter, type_declarators, siblings) {
423 struct declaration *field_declaration;
424
425 field_declaration = ctf_type_declarator_visit(fd, depth,
426 type_specifier_list,
427 &field_name, iter,
428 struct_declaration->scope,
429 NULL, trace);
430 if (!field_declaration) {
431 fprintf(fd, "[error] %s: unable to find struct field declaration type\n", __func__);
432 return -EINVAL;
433 }
434 struct_declaration_add_field(struct_declaration,
435 g_quark_to_string(field_name),
436 field_declaration);
437 }
438 return 0;
439 }
440
441 static
442 int ctf_variant_type_declarators_visit(FILE *fd, int depth,
443 struct declaration_untagged_variant *untagged_variant_declaration,
444 struct ctf_node *type_specifier_list,
445 struct cds_list_head *type_declarators,
446 struct declaration_scope *declaration_scope,
447 struct ctf_trace *trace)
448 {
449 struct ctf_node *iter;
450 GQuark field_name;
451
452 cds_list_for_each_entry(iter, type_declarators, siblings) {
453 struct declaration *field_declaration;
454
455 field_declaration = ctf_type_declarator_visit(fd, depth,
456 type_specifier_list,
457 &field_name, iter,
458 untagged_variant_declaration->scope,
459 NULL, trace);
460 if (!field_declaration) {
461 fprintf(fd, "[error] %s: unable to find variant field declaration type\n", __func__);
462 return -EINVAL;
463 }
464 untagged_variant_declaration_add_field(untagged_variant_declaration,
465 g_quark_to_string(field_name),
466 field_declaration);
467 }
468 return 0;
469 }
470
471 static
472 int ctf_typedef_visit(FILE *fd, int depth, struct declaration_scope *scope,
473 struct ctf_node *type_specifier_list,
474 struct cds_list_head *type_declarators,
475 struct ctf_trace *trace)
476 {
477 struct ctf_node *iter;
478 GQuark identifier;
479
480 cds_list_for_each_entry(iter, type_declarators, siblings) {
481 struct declaration *type_declaration;
482 int ret;
483
484 type_declaration = ctf_type_declarator_visit(fd, depth,
485 type_specifier_list,
486 &identifier, iter,
487 scope, NULL, trace);
488 if (!type_declaration) {
489 fprintf(fd, "[error] %s: problem creating type declaration\n", __func__);
490 return -EINVAL;
491 }
492 /*
493 * Don't allow typedef and typealias of untagged
494 * variants.
495 */
496 if (type_declaration->id == CTF_TYPE_UNTAGGED_VARIANT) {
497 fprintf(fd, "[error] %s: typedef of untagged variant is not permitted.\n", __func__);
498 declaration_unref(type_declaration);
499 return -EPERM;
500 }
501 ret = register_declaration(identifier, type_declaration, scope);
502 if (ret) {
503 type_declaration->declaration_free(type_declaration);
504 return ret;
505 }
506 }
507 return 0;
508 }
509
510 static
511 int ctf_typealias_visit(FILE *fd, int depth, struct declaration_scope *scope,
512 struct ctf_node *target, struct ctf_node *alias,
513 struct ctf_trace *trace)
514 {
515 struct declaration *type_declaration;
516 struct ctf_node *node;
517 GQuark dummy_id;
518 GQuark alias_q;
519 int err;
520
521 /* See ctf_visitor_type_declarator() in the semantic validator. */
522
523 /*
524 * Create target type declaration.
525 */
526
527 if (cds_list_empty(&target->u.typealias_target.type_declarators))
528 node = NULL;
529 else
530 node = _cds_list_first_entry(&target->u.typealias_target.type_declarators,
531 struct ctf_node, siblings);
532 type_declaration = ctf_type_declarator_visit(fd, depth,
533 target->u.typealias_target.type_specifier_list,
534 &dummy_id, node,
535 scope, NULL, trace);
536 if (!type_declaration) {
537 fprintf(fd, "[error] %s: problem creating type declaration\n", __func__);
538 err = -EINVAL;
539 goto error;
540 }
541 /*
542 * Don't allow typedef and typealias of untagged
543 * variants.
544 */
545 if (type_declaration->id == CTF_TYPE_UNTAGGED_VARIANT) {
546 fprintf(fd, "[error] %s: typedef of untagged variant is not permitted.\n", __func__);
547 declaration_unref(type_declaration);
548 return -EPERM;
549 }
550 /*
551 * The semantic validator does not check whether the target is
552 * abstract or not (if it has an identifier). Check it here.
553 */
554 if (dummy_id != 0) {
555 fprintf(fd, "[error] %s: expecting empty identifier\n", __func__);
556 err = -EINVAL;
557 goto error;
558 }
559 /*
560 * Create alias identifier.
561 */
562
563 node = _cds_list_first_entry(&alias->u.typealias_alias.type_declarators,
564 struct ctf_node, siblings);
565 alias_q = create_typealias_identifier(fd, depth,
566 alias->u.typealias_alias.type_specifier_list, node);
567 err = register_declaration(alias_q, type_declaration, scope);
568 if (err)
569 goto error;
570 return 0;
571
572 error:
573 type_declaration->declaration_free(type_declaration);
574 return err;
575 }
576
577 static
578 int ctf_struct_declaration_list_visit(FILE *fd, int depth,
579 struct ctf_node *iter, struct declaration_struct *struct_declaration,
580 struct ctf_trace *trace)
581 {
582 int ret;
583
584 switch (iter->type) {
585 case NODE_TYPEDEF:
586 /* For each declarator, declare type and add type to struct declaration scope */
587 ret = ctf_typedef_visit(fd, depth,
588 struct_declaration->scope,
589 iter->u._typedef.type_specifier_list,
590 &iter->u._typedef.type_declarators, trace);
591 if (ret)
592 return ret;
593 break;
594 case NODE_TYPEALIAS:
595 /* Declare type with declarator and add type to struct declaration scope */
596 ret = ctf_typealias_visit(fd, depth,
597 struct_declaration->scope,
598 iter->u.typealias.target,
599 iter->u.typealias.alias, trace);
600 if (ret)
601 return ret;
602 break;
603 case NODE_STRUCT_OR_VARIANT_DECLARATION:
604 /* Add field to structure declaration */
605 ret = ctf_struct_type_declarators_visit(fd, depth,
606 struct_declaration,
607 iter->u.struct_or_variant_declaration.type_specifier_list,
608 &iter->u.struct_or_variant_declaration.type_declarators,
609 struct_declaration->scope, trace);
610 if (ret)
611 return ret;
612 break;
613 default:
614 fprintf(fd, "[error] %s: unexpected node type %d\n", __func__, (int) iter->type);
615 assert(0);
616 }
617 return 0;
618 }
619
620 static
621 int ctf_variant_declaration_list_visit(FILE *fd, int depth,
622 struct ctf_node *iter,
623 struct declaration_untagged_variant *untagged_variant_declaration,
624 struct ctf_trace *trace)
625 {
626 int ret;
627
628 switch (iter->type) {
629 case NODE_TYPEDEF:
630 /* For each declarator, declare type and add type to variant declaration scope */
631 ret = ctf_typedef_visit(fd, depth,
632 untagged_variant_declaration->scope,
633 iter->u._typedef.type_specifier_list,
634 &iter->u._typedef.type_declarators, trace);
635 if (ret)
636 return ret;
637 break;
638 case NODE_TYPEALIAS:
639 /* Declare type with declarator and add type to variant declaration scope */
640 ret = ctf_typealias_visit(fd, depth,
641 untagged_variant_declaration->scope,
642 iter->u.typealias.target,
643 iter->u.typealias.alias, trace);
644 if (ret)
645 return ret;
646 break;
647 case NODE_STRUCT_OR_VARIANT_DECLARATION:
648 /* Add field to structure declaration */
649 ret = ctf_variant_type_declarators_visit(fd, depth,
650 untagged_variant_declaration,
651 iter->u.struct_or_variant_declaration.type_specifier_list,
652 &iter->u.struct_or_variant_declaration.type_declarators,
653 untagged_variant_declaration->scope, trace);
654 if (ret)
655 return ret;
656 break;
657 default:
658 fprintf(fd, "[error] %s: unexpected node type %d\n", __func__, (int) iter->type);
659 assert(0);
660 }
661 return 0;
662 }
663
664 static
665 struct declaration *ctf_declaration_struct_visit(FILE *fd,
666 int depth, const char *name, struct cds_list_head *declaration_list,
667 int has_body, struct cds_list_head *min_align,
668 struct declaration_scope *declaration_scope,
669 struct ctf_trace *trace)
670 {
671 struct declaration_struct *struct_declaration;
672 struct ctf_node *iter;
673 int ret;
674
675 /*
676 * For named struct (without body), lookup in
677 * declaration scope. Don't take reference on struct
678 * declaration: ref is only taken upon definition.
679 */
680 if (!has_body) {
681 assert(name);
682 struct_declaration =
683 lookup_struct_declaration(g_quark_from_string(name),
684 declaration_scope);
685 return &struct_declaration->p;
686 } else {
687 uint64_t min_align_value = 0;
688
689 /* For unnamed struct, create type */
690 /* For named struct (with body), create type and add to declaration scope */
691 if (name) {
692 if (lookup_struct_declaration(g_quark_from_string(name),
693 declaration_scope)) {
694
695 fprintf(fd, "[error] %s: struct %s already declared in scope\n", __func__, name);
696 return NULL;
697 }
698 }
699 if (!cds_list_empty(min_align)) {
700 ret = get_unary_unsigned(min_align, &min_align_value);
701 if (ret) {
702 fprintf(fd, "[error] %s: unexpected unary expression for structure \"align\" attribute\n", __func__);
703 ret = -EINVAL;
704 goto error;
705 }
706 }
707 struct_declaration = struct_declaration_new(declaration_scope,
708 min_align_value);
709 cds_list_for_each_entry(iter, declaration_list, siblings) {
710 ret = ctf_struct_declaration_list_visit(fd, depth + 1, iter,
711 struct_declaration, trace);
712 if (ret)
713 goto error;
714 }
715 if (name) {
716 ret = register_struct_declaration(g_quark_from_string(name),
717 struct_declaration,
718 declaration_scope);
719 assert(!ret);
720 }
721 return &struct_declaration->p;
722 }
723 error:
724 struct_declaration->p.declaration_free(&struct_declaration->p);
725 return NULL;
726 }
727
728 static
729 struct declaration *ctf_declaration_variant_visit(FILE *fd,
730 int depth, const char *name, const char *choice,
731 struct cds_list_head *declaration_list,
732 int has_body, struct declaration_scope *declaration_scope,
733 struct ctf_trace *trace)
734 {
735 struct declaration_untagged_variant *untagged_variant_declaration;
736 struct declaration_variant *variant_declaration;
737 struct ctf_node *iter;
738 int ret;
739
740 /*
741 * For named variant (without body), lookup in
742 * declaration scope. Don't take reference on variant
743 * declaration: ref is only taken upon definition.
744 */
745 if (!has_body) {
746 assert(name);
747 untagged_variant_declaration =
748 lookup_variant_declaration(g_quark_from_string(name),
749 declaration_scope);
750 } else {
751 /* For unnamed variant, create type */
752 /* For named variant (with body), create type and add to declaration scope */
753 if (name) {
754 if (lookup_variant_declaration(g_quark_from_string(name),
755 declaration_scope)) {
756
757 fprintf(fd, "[error] %s: variant %s already declared in scope\n", __func__, name);
758 return NULL;
759 }
760 }
761 untagged_variant_declaration = untagged_variant_declaration_new(declaration_scope);
762 cds_list_for_each_entry(iter, declaration_list, siblings) {
763 ret = ctf_variant_declaration_list_visit(fd, depth + 1, iter,
764 untagged_variant_declaration, trace);
765 if (ret)
766 goto error;
767 }
768 if (name) {
769 ret = register_variant_declaration(g_quark_from_string(name),
770 untagged_variant_declaration,
771 declaration_scope);
772 assert(!ret);
773 }
774 }
775 /*
776 * if tagged, create tagged variant and return. else return
777 * untagged variant.
778 */
779 if (!choice) {
780 return &untagged_variant_declaration->p;
781 } else {
782 variant_declaration = variant_declaration_new(untagged_variant_declaration, choice);
783 if (!variant_declaration)
784 goto error;
785 declaration_unref(&untagged_variant_declaration->p);
786 return &variant_declaration->p;
787 }
788 error:
789 untagged_variant_declaration->p.declaration_free(&untagged_variant_declaration->p);
790 return NULL;
791 }
792
793 static
794 int ctf_enumerator_list_visit(FILE *fd, int depth,
795 struct ctf_node *enumerator,
796 struct declaration_enum *enum_declaration)
797 {
798 GQuark q;
799 struct ctf_node *iter;
800
801 q = g_quark_from_string(enumerator->u.enumerator.id);
802 if (enum_declaration->integer_declaration->signedness) {
803 int64_t start, end;
804 int nr_vals = 0;
805
806 cds_list_for_each_entry(iter, &enumerator->u.enumerator.values, siblings) {
807 int64_t *target;
808
809 assert(iter->type == NODE_UNARY_EXPRESSION);
810 if (nr_vals == 0)
811 target = &start;
812 else
813 target = &end;
814
815 switch (iter->u.unary_expression.type) {
816 case UNARY_SIGNED_CONSTANT:
817 *target = iter->u.unary_expression.u.signed_constant;
818 break;
819 case UNARY_UNSIGNED_CONSTANT:
820 *target = iter->u.unary_expression.u.unsigned_constant;
821 break;
822 default:
823 fprintf(fd, "[error] %s: invalid enumerator\n", __func__);
824 return -EINVAL;
825 }
826 if (nr_vals > 1) {
827 fprintf(fd, "[error] %s: invalid enumerator\n", __func__);
828 return -EINVAL;
829 }
830 nr_vals++;
831 }
832 if (nr_vals == 1)
833 end = start;
834 enum_signed_insert(enum_declaration, start, end, q);
835 } else {
836 uint64_t start, end;
837 int nr_vals = 0;
838
839 cds_list_for_each_entry(iter, &enumerator->u.enumerator.values, siblings) {
840 uint64_t *target;
841
842 assert(iter->type == NODE_UNARY_EXPRESSION);
843 if (nr_vals == 0)
844 target = &start;
845 else
846 target = &end;
847
848 switch (iter->u.unary_expression.type) {
849 case UNARY_UNSIGNED_CONSTANT:
850 *target = iter->u.unary_expression.u.unsigned_constant;
851 break;
852 case UNARY_SIGNED_CONSTANT:
853 /*
854 * We don't accept signed constants for enums with unsigned
855 * container type.
856 */
857 fprintf(fd, "[error] %s: invalid enumerator (signed constant encountered, but enum container type is unsigned)\n", __func__);
858 return -EINVAL;
859 default:
860 fprintf(fd, "[error] %s: invalid enumerator\n", __func__);
861 return -EINVAL;
862 }
863 if (nr_vals > 1) {
864 fprintf(fd, "[error] %s: invalid enumerator\n", __func__);
865 return -EINVAL;
866 }
867 nr_vals++;
868 }
869 if (nr_vals == 1)
870 end = start;
871 enum_unsigned_insert(enum_declaration, start, end, q);
872 }
873 return 0;
874 }
875
876 static
877 struct declaration *ctf_declaration_enum_visit(FILE *fd, int depth,
878 const char *name,
879 struct ctf_node *container_type,
880 struct cds_list_head *enumerator_list,
881 int has_body,
882 struct declaration_scope *declaration_scope,
883 struct ctf_trace *trace)
884 {
885 struct declaration *declaration;
886 struct declaration_enum *enum_declaration;
887 struct declaration_integer *integer_declaration;
888 struct ctf_node *iter;
889 GQuark dummy_id;
890 int ret;
891
892 /*
893 * For named enum (without body), lookup in
894 * declaration scope. Don't take reference on enum
895 * declaration: ref is only taken upon definition.
896 */
897 if (!has_body) {
898 assert(name);
899 enum_declaration =
900 lookup_enum_declaration(g_quark_from_string(name),
901 declaration_scope);
902 return &enum_declaration->p;
903 } else {
904 /* For unnamed enum, create type */
905 /* For named enum (with body), create type and add to declaration scope */
906 if (name) {
907 if (lookup_enum_declaration(g_quark_from_string(name),
908 declaration_scope)) {
909
910 fprintf(fd, "[error] %s: enum %s already declared in scope\n", __func__, name);
911 return NULL;
912 }
913 }
914 if (!container_type) {
915 declaration = lookup_declaration(g_quark_from_static_string("int"),
916 declaration_scope);
917 if (!declaration) {
918 fprintf(fd, "[error] %s: \"int\" type declaration missing for enumeration\n", __func__);
919 return NULL;
920 }
921 } else {
922 declaration = ctf_type_declarator_visit(fd, depth,
923 container_type,
924 &dummy_id, NULL,
925 declaration_scope,
926 NULL, trace);
927 }
928 if (!declaration) {
929 fprintf(fd, "[error] %s: unable to create container type for enumeration\n", __func__);
930 return NULL;
931 }
932 if (declaration->id != CTF_TYPE_INTEGER) {
933 fprintf(fd, "[error] %s: container type for enumeration is not integer\n", __func__);
934 return NULL;
935 }
936 integer_declaration = container_of(declaration, struct declaration_integer, p);
937 enum_declaration = enum_declaration_new(integer_declaration);
938 declaration_unref(&integer_declaration->p); /* leave ref to enum */
939 cds_list_for_each_entry(iter, enumerator_list, siblings) {
940 ret = ctf_enumerator_list_visit(fd, depth + 1, iter, enum_declaration);
941 if (ret)
942 goto error;
943 }
944 if (name) {
945 ret = register_enum_declaration(g_quark_from_string(name),
946 enum_declaration,
947 declaration_scope);
948 assert(!ret);
949 }
950 return &enum_declaration->p;
951 }
952 error:
953 enum_declaration->p.declaration_free(&enum_declaration->p);
954 return NULL;
955 }
956
957 static
958 struct declaration *ctf_declaration_type_specifier_visit(FILE *fd, int depth,
959 struct ctf_node *type_specifier_list,
960 struct declaration_scope *declaration_scope)
961 {
962 GString *str;
963 struct declaration *declaration;
964 char *str_c;
965 int ret;
966 GQuark id_q;
967
968 str = g_string_new("");
969 ret = visit_type_specifier_list(fd, type_specifier_list, str);
970 if (ret)
971 return NULL;
972 str_c = g_string_free(str, FALSE);
973 id_q = g_quark_from_string(str_c);
974 g_free(str_c);
975 declaration = lookup_declaration(id_q, declaration_scope);
976 return declaration;
977 }
978
979 /*
980 * Returns 0/1 boolean, or < 0 on error.
981 */
982 static
983 int get_boolean(FILE *fd, int depth, struct ctf_node *unary_expression)
984 {
985 if (unary_expression->type != NODE_UNARY_EXPRESSION) {
986 fprintf(fd, "[error] %s: expecting unary expression\n",
987 __func__);
988 return -EINVAL;
989 }
990 switch (unary_expression->u.unary_expression.type) {
991 case UNARY_UNSIGNED_CONSTANT:
992 if (unary_expression->u.unary_expression.u.unsigned_constant == 0)
993 return 0;
994 else
995 return 1;
996 case UNARY_SIGNED_CONSTANT:
997 if (unary_expression->u.unary_expression.u.signed_constant == 0)
998 return 0;
999 else
1000 return 1;
1001 case UNARY_STRING:
1002 if (!strcmp(unary_expression->u.unary_expression.u.string, "true"))
1003 return 1;
1004 else if (!strcmp(unary_expression->u.unary_expression.u.string, "TRUE"))
1005 return 1;
1006 else if (!strcmp(unary_expression->u.unary_expression.u.string, "false"))
1007 return 0;
1008 else if (!strcmp(unary_expression->u.unary_expression.u.string, "FALSE"))
1009 return 0;
1010 else {
1011 fprintf(fd, "[error] %s: unexpected string \"%s\"\n",
1012 __func__, unary_expression->u.unary_expression.u.string);
1013 return -EINVAL;
1014 }
1015 break;
1016 default:
1017 fprintf(fd, "[error] %s: unexpected unary expression type\n",
1018 __func__);
1019 return -EINVAL;
1020 }
1021
1022 }
1023
1024 static
1025 int get_trace_byte_order(FILE *fd, int depth, struct ctf_node *unary_expression)
1026 {
1027 int byte_order;
1028
1029 if (unary_expression->u.unary_expression.type != UNARY_STRING) {
1030 fprintf(fd, "[error] %s: byte_order: expecting string\n",
1031 __func__);
1032 return -EINVAL;
1033 }
1034 if (!strcmp(unary_expression->u.unary_expression.u.string, "be"))
1035 byte_order = BIG_ENDIAN;
1036 else if (!strcmp(unary_expression->u.unary_expression.u.string, "le"))
1037 byte_order = LITTLE_ENDIAN;
1038 else {
1039 fprintf(fd, "[error] %s: unexpected string \"%s\". Should be \"native\", \"network\", \"be\" or \"le\".\n",
1040 __func__, unary_expression->u.unary_expression.u.string);
1041 return -EINVAL;
1042 }
1043 return byte_order;
1044 }
1045
1046 static
1047 int get_byte_order(FILE *fd, int depth, struct ctf_node *unary_expression,
1048 struct ctf_trace *trace)
1049 {
1050 int byte_order;
1051
1052 if (unary_expression->u.unary_expression.type != UNARY_STRING) {
1053 fprintf(fd, "[error] %s: byte_order: expecting string\n",
1054 __func__);
1055 return -EINVAL;
1056 }
1057 if (!strcmp(unary_expression->u.unary_expression.u.string, "native"))
1058 byte_order = trace->byte_order;
1059 else if (!strcmp(unary_expression->u.unary_expression.u.string, "network"))
1060 byte_order = BIG_ENDIAN;
1061 else if (!strcmp(unary_expression->u.unary_expression.u.string, "be"))
1062 byte_order = BIG_ENDIAN;
1063 else if (!strcmp(unary_expression->u.unary_expression.u.string, "le"))
1064 byte_order = LITTLE_ENDIAN;
1065 else {
1066 fprintf(fd, "[error] %s: unexpected string \"%s\". Should be \"native\", \"network\", \"be\" or \"le\".\n",
1067 __func__, unary_expression->u.unary_expression.u.string);
1068 return -EINVAL;
1069 }
1070 return byte_order;
1071 }
1072
1073 static
1074 struct declaration *ctf_declaration_integer_visit(FILE *fd, int depth,
1075 struct cds_list_head *expressions,
1076 struct ctf_trace *trace)
1077 {
1078 struct ctf_node *expression;
1079 uint64_t alignment, size;
1080 int byte_order = trace->byte_order;
1081 int signedness = 0;
1082 int has_alignment = 0, has_size = 0;
1083 int base = 0;
1084 enum ctf_string_encoding encoding = CTF_STRING_NONE;
1085 struct declaration_integer *integer_declaration;
1086
1087 cds_list_for_each_entry(expression, expressions, siblings) {
1088 struct ctf_node *left, *right;
1089
1090 left = _cds_list_first_entry(&expression->u.ctf_expression.left, struct ctf_node, siblings);
1091 right = _cds_list_first_entry(&expression->u.ctf_expression.right, struct ctf_node, siblings);
1092 assert(left->u.unary_expression.type == UNARY_STRING);
1093 if (!strcmp(left->u.unary_expression.u.string, "signed")) {
1094 signedness = get_boolean(fd, depth, right);
1095 if (signedness < 0)
1096 return NULL;
1097 } else if (!strcmp(left->u.unary_expression.u.string, "byte_order")) {
1098 byte_order = get_byte_order(fd, depth, right, trace);
1099 if (byte_order < 0)
1100 return NULL;
1101 } else if (!strcmp(left->u.unary_expression.u.string, "size")) {
1102 if (right->u.unary_expression.type != UNARY_UNSIGNED_CONSTANT) {
1103 fprintf(fd, "[error] %s: size: expecting unsigned constant\n",
1104 __func__);
1105 return NULL;
1106 }
1107 size = right->u.unary_expression.u.unsigned_constant;
1108 has_size = 1;
1109 } else if (!strcmp(left->u.unary_expression.u.string, "align")) {
1110 if (right->u.unary_expression.type != UNARY_UNSIGNED_CONSTANT) {
1111 fprintf(fd, "[error] %s: align: expecting unsigned constant\n",
1112 __func__);
1113 return NULL;
1114 }
1115 alignment = right->u.unary_expression.u.unsigned_constant;
1116 /* Make sure alignment is a power of two */
1117 if (alignment == 0 || (alignment & (alignment - 1)) != 0) {
1118 fprintf(fd, "[error] %s: align: expecting power of two\n",
1119 __func__);
1120 return NULL;
1121 }
1122 has_alignment = 1;
1123 } else if (!strcmp(left->u.unary_expression.u.string, "base")) {
1124 switch (right->u.unary_expression.type) {
1125 case UNARY_UNSIGNED_CONSTANT:
1126 switch (right->u.unary_expression.u.unsigned_constant) {
1127 case 2:
1128 case 8:
1129 case 10:
1130 case 16:
1131 base = right->u.unary_expression.u.unsigned_constant;
1132 break;
1133 default:
1134 fprintf(fd, "[error] %s: base not supported (%" PRIu64 ")\n",
1135 __func__, right->u.unary_expression.u.unsigned_constant);
1136 return NULL;
1137 }
1138 break;
1139 case UNARY_STRING:
1140 {
1141 char *s_right = concatenate_unary_strings(&expression->u.ctf_expression.right);
1142 if (!s_right) {
1143 fprintf(fd, "[error] %s: unexpected unary expression for integer base\n", __func__);
1144 g_free(s_right);
1145 return NULL;
1146 }
1147 if (!strcmp(s_right, "decimal") || !strcmp(s_right, "dec") || !strcmp(s_right, "d")
1148 || !strcmp(s_right, "i") || !strcmp(s_right, "u")) {
1149 base = 10;
1150 } else if (!strcmp(s_right, "hexadecimal") || !strcmp(s_right, "hex")
1151 || !strcmp(s_right, "x") || !strcmp(s_right, "X")
1152 || !strcmp(s_right, "p")) {
1153 base = 16;
1154 } else if (!strcmp(s_right, "octal") || !strcmp(s_right, "oct")
1155 || !strcmp(s_right, "o")) {
1156 base = 8;
1157 } else if (!strcmp(s_right, "binary") || !strcmp(s_right, "b")) {
1158 base = 2;
1159 } else {
1160 fprintf(fd, "[error] %s: unexpected expression for integer base (%s)\n", __func__, s_right);
1161 g_free(s_right);
1162 return NULL;
1163 }
1164
1165 g_free(s_right);
1166 break;
1167 }
1168 default:
1169 fprintf(fd, "[error] %s: base: expecting unsigned constant or unary string\n",
1170 __func__);
1171 return NULL;
1172 }
1173 } else if (!strcmp(left->u.unary_expression.u.string, "encoding")) {
1174 char *s_right;
1175
1176 if (right->u.unary_expression.type != UNARY_STRING) {
1177 fprintf(fd, "[error] %s: encoding: expecting unary string\n",
1178 __func__);
1179 return NULL;
1180 }
1181 s_right = concatenate_unary_strings(&expression->u.ctf_expression.right);
1182 if (!s_right) {
1183 fprintf(fd, "[error] %s: unexpected unary expression for integer base\n", __func__);
1184 g_free(s_right);
1185 return NULL;
1186 }
1187 if (!strcmp(s_right, "UTF8")
1188 || !strcmp(s_right, "utf8")
1189 || !strcmp(s_right, "utf-8")
1190 || !strcmp(s_right, "UTF-8"))
1191 encoding = CTF_STRING_UTF8;
1192 else if (!strcmp(s_right, "ASCII")
1193 || !strcmp(s_right, "ascii"))
1194 encoding = CTF_STRING_ASCII;
1195 else if (!strcmp(s_right, "none"))
1196 encoding = CTF_STRING_NONE;
1197 else {
1198 fprintf(fd, "[error] %s: unknown string encoding \"%s\"\n", __func__, s_right);
1199 g_free(s_right);
1200 return NULL;
1201 }
1202 g_free(s_right);
1203 } else {
1204 fprintf(fd, "[error] %s: unknown attribute name %s\n",
1205 __func__, left->u.unary_expression.u.string);
1206 return NULL;
1207 }
1208 }
1209 if (!has_size) {
1210 fprintf(fd, "[error] %s: missing size attribute\n", __func__);
1211 return NULL;
1212 }
1213 if (!has_alignment) {
1214 if (size % CHAR_BIT) {
1215 /* bit-packed alignment */
1216 alignment = 1;
1217 } else {
1218 /* byte-packed alignment */
1219 alignment = CHAR_BIT;
1220 }
1221 }
1222 integer_declaration = integer_declaration_new(size,
1223 byte_order, signedness, alignment,
1224 base, encoding);
1225 return &integer_declaration->p;
1226 }
1227
1228 static
1229 struct declaration *ctf_declaration_floating_point_visit(FILE *fd, int depth,
1230 struct cds_list_head *expressions,
1231 struct ctf_trace *trace)
1232 {
1233 struct ctf_node *expression;
1234 uint64_t alignment, exp_dig, mant_dig, byte_order = trace->byte_order;
1235 int has_alignment = 0, has_exp_dig = 0, has_mant_dig = 0;
1236 struct declaration_float *float_declaration;
1237
1238 cds_list_for_each_entry(expression, expressions, siblings) {
1239 struct ctf_node *left, *right;
1240
1241 left = _cds_list_first_entry(&expression->u.ctf_expression.left, struct ctf_node, siblings);
1242 right = _cds_list_first_entry(&expression->u.ctf_expression.right, struct ctf_node, siblings);
1243 assert(left->u.unary_expression.type == UNARY_STRING);
1244 if (!strcmp(left->u.unary_expression.u.string, "byte_order")) {
1245 byte_order = get_byte_order(fd, depth, right, trace);
1246 if (byte_order < 0)
1247 return NULL;
1248 } else if (!strcmp(left->u.unary_expression.u.string, "exp_dig")) {
1249 if (right->u.unary_expression.type != UNARY_UNSIGNED_CONSTANT) {
1250 fprintf(fd, "[error] %s: exp_dig: expecting unsigned constant\n",
1251 __func__);
1252 return NULL;
1253 }
1254 exp_dig = right->u.unary_expression.u.unsigned_constant;
1255 has_exp_dig = 1;
1256 } else if (!strcmp(left->u.unary_expression.u.string, "mant_dig")) {
1257 if (right->u.unary_expression.type != UNARY_UNSIGNED_CONSTANT) {
1258 fprintf(fd, "[error] %s: mant_dig: expecting unsigned constant\n",
1259 __func__);
1260 return NULL;
1261 }
1262 mant_dig = right->u.unary_expression.u.unsigned_constant;
1263 has_mant_dig = 1;
1264 } else if (!strcmp(left->u.unary_expression.u.string, "align")) {
1265 if (right->u.unary_expression.type != UNARY_UNSIGNED_CONSTANT) {
1266 fprintf(fd, "[error] %s: align: expecting unsigned constant\n",
1267 __func__);
1268 return NULL;
1269 }
1270 alignment = right->u.unary_expression.u.unsigned_constant;
1271 /* Make sure alignment is a power of two */
1272 if (alignment == 0 || (alignment & (alignment - 1)) != 0) {
1273 fprintf(fd, "[error] %s: align: expecting power of two\n",
1274 __func__);
1275 return NULL;
1276 }
1277 has_alignment = 1;
1278 } else {
1279 fprintf(fd, "[error] %s: unknown attribute name %s\n",
1280 __func__, left->u.unary_expression.u.string);
1281 return NULL;
1282 }
1283 }
1284 if (!has_mant_dig) {
1285 fprintf(fd, "[error] %s: missing mant_dig attribute\n", __func__);
1286 return NULL;
1287 }
1288 if (!has_exp_dig) {
1289 fprintf(fd, "[error] %s: missing exp_dig attribute\n", __func__);
1290 return NULL;
1291 }
1292 if (!has_alignment) {
1293 if ((mant_dig + exp_dig) % CHAR_BIT) {
1294 /* bit-packed alignment */
1295 alignment = 1;
1296 } else {
1297 /* byte-packed alignment */
1298 alignment = CHAR_BIT;
1299 }
1300 }
1301 float_declaration = float_declaration_new(mant_dig, exp_dig,
1302 byte_order, alignment);
1303 return &float_declaration->p;
1304 }
1305
1306 static
1307 struct declaration *ctf_declaration_string_visit(FILE *fd, int depth,
1308 struct cds_list_head *expressions,
1309 struct ctf_trace *trace)
1310 {
1311 struct ctf_node *expression;
1312 const char *encoding_c = NULL;
1313 enum ctf_string_encoding encoding = CTF_STRING_UTF8;
1314 struct declaration_string *string_declaration;
1315
1316 cds_list_for_each_entry(expression, expressions, siblings) {
1317 struct ctf_node *left, *right;
1318
1319 left = _cds_list_first_entry(&expression->u.ctf_expression.left, struct ctf_node, siblings);
1320 right = _cds_list_first_entry(&expression->u.ctf_expression.right, struct ctf_node, siblings);
1321 assert(left->u.unary_expression.type == UNARY_STRING);
1322 if (!strcmp(left->u.unary_expression.u.string, "encoding")) {
1323 if (right->u.unary_expression.type != UNARY_STRING) {
1324 fprintf(fd, "[error] %s: encoding: expecting string\n",
1325 __func__);
1326 return NULL;
1327 }
1328 encoding_c = right->u.unary_expression.u.string;
1329 } else {
1330 fprintf(fd, "[error] %s: unknown attribute name %s\n",
1331 __func__, left->u.unary_expression.u.string);
1332 return NULL;
1333 }
1334 }
1335 if (encoding_c && !strcmp(encoding_c, "ASCII"))
1336 encoding = CTF_STRING_ASCII;
1337 string_declaration = string_declaration_new(encoding);
1338 return &string_declaration->p;
1339 }
1340
1341
1342 static
1343 struct declaration *ctf_type_specifier_list_visit(FILE *fd,
1344 int depth, struct ctf_node *type_specifier_list,
1345 struct declaration_scope *declaration_scope,
1346 struct ctf_trace *trace)
1347 {
1348 struct ctf_node *first;
1349 struct ctf_node *node;
1350
1351 assert(type_specifier_list->type == NODE_TYPE_SPECIFIER_LIST);
1352
1353 first = _cds_list_first_entry(&type_specifier_list->u.type_specifier_list.head, struct ctf_node, siblings);
1354
1355 assert(first->type == NODE_TYPE_SPECIFIER);
1356
1357 node = first->u.type_specifier.node;
1358
1359 switch (first->u.type_specifier.type) {
1360 case TYPESPEC_FLOATING_POINT:
1361 return ctf_declaration_floating_point_visit(fd, depth,
1362 &node->u.floating_point.expressions, trace);
1363 case TYPESPEC_INTEGER:
1364 return ctf_declaration_integer_visit(fd, depth,
1365 &node->u.integer.expressions, trace);
1366 case TYPESPEC_STRING:
1367 return ctf_declaration_string_visit(fd, depth,
1368 &node->u.string.expressions, trace);
1369 case TYPESPEC_STRUCT:
1370 return ctf_declaration_struct_visit(fd, depth,
1371 node->u._struct.name,
1372 &node->u._struct.declaration_list,
1373 node->u._struct.has_body,
1374 &node->u._struct.min_align,
1375 declaration_scope,
1376 trace);
1377 case TYPESPEC_VARIANT:
1378 return ctf_declaration_variant_visit(fd, depth,
1379 node->u.variant.name,
1380 node->u.variant.choice,
1381 &node->u.variant.declaration_list,
1382 node->u.variant.has_body,
1383 declaration_scope,
1384 trace);
1385 case TYPESPEC_ENUM:
1386 return ctf_declaration_enum_visit(fd, depth,
1387 node->u._enum.enum_id,
1388 node->u._enum.container_type,
1389 &node->u._enum.enumerator_list,
1390 node->u._enum.has_body,
1391 declaration_scope,
1392 trace);
1393
1394 case TYPESPEC_VOID:
1395 case TYPESPEC_CHAR:
1396 case TYPESPEC_SHORT:
1397 case TYPESPEC_INT:
1398 case TYPESPEC_LONG:
1399 case TYPESPEC_FLOAT:
1400 case TYPESPEC_DOUBLE:
1401 case TYPESPEC_SIGNED:
1402 case TYPESPEC_UNSIGNED:
1403 case TYPESPEC_BOOL:
1404 case TYPESPEC_COMPLEX:
1405 case TYPESPEC_IMAGINARY:
1406 case TYPESPEC_CONST:
1407 case TYPESPEC_ID_TYPE:
1408 return ctf_declaration_type_specifier_visit(fd, depth,
1409 type_specifier_list, declaration_scope);
1410 default:
1411 fprintf(fd, "[error] %s: unexpected node type %d\n", __func__, (int) first->u.type_specifier.type);
1412 return NULL;
1413 }
1414 }
1415
1416 static
1417 int ctf_event_declaration_visit(FILE *fd, int depth, struct ctf_node *node, struct ctf_event *event, struct ctf_trace *trace)
1418 {
1419 int ret = 0;
1420
1421 switch (node->type) {
1422 case NODE_TYPEDEF:
1423 ret = ctf_typedef_visit(fd, depth + 1,
1424 event->declaration_scope,
1425 node->u._typedef.type_specifier_list,
1426 &node->u._typedef.type_declarators,
1427 trace);
1428 if (ret)
1429 return ret;
1430 break;
1431 case NODE_TYPEALIAS:
1432 ret = ctf_typealias_visit(fd, depth + 1,
1433 event->declaration_scope,
1434 node->u.typealias.target, node->u.typealias.alias,
1435 trace);
1436 if (ret)
1437 return ret;
1438 break;
1439 case NODE_CTF_EXPRESSION:
1440 {
1441 char *left;
1442
1443 left = concatenate_unary_strings(&node->u.ctf_expression.left);
1444 if (!strcmp(left, "name")) {
1445 char *right;
1446
1447 if (CTF_EVENT_FIELD_IS_SET(event, name)) {
1448 fprintf(fd, "[error] %s: name already declared in event declaration\n", __func__);
1449 ret = -EPERM;
1450 goto error;
1451 }
1452 right = concatenate_unary_strings(&node->u.ctf_expression.right);
1453 if (!right) {
1454 fprintf(fd, "[error] %s: unexpected unary expression for event name\n", __func__);
1455 ret = -EINVAL;
1456 goto error;
1457 }
1458 event->name = g_quark_from_string(right);
1459 g_free(right);
1460 CTF_EVENT_SET_FIELD(event, name);
1461 } else if (!strcmp(left, "id")) {
1462 if (CTF_EVENT_FIELD_IS_SET(event, id)) {
1463 fprintf(fd, "[error] %s: id already declared in event declaration\n", __func__);
1464 ret = -EPERM;
1465 goto error;
1466 }
1467 ret = get_unary_unsigned(&node->u.ctf_expression.right, &event->id);
1468 if (ret) {
1469 fprintf(fd, "[error] %s: unexpected unary expression for event id\n", __func__);
1470 ret = -EINVAL;
1471 goto error;
1472 }
1473 CTF_EVENT_SET_FIELD(event, id);
1474 } else if (!strcmp(left, "stream_id")) {
1475 if (CTF_EVENT_FIELD_IS_SET(event, stream_id)) {
1476 fprintf(fd, "[error] %s: stream_id already declared in event declaration\n", __func__);
1477 ret = -EPERM;
1478 goto error;
1479 }
1480 ret = get_unary_unsigned(&node->u.ctf_expression.right, &event->stream_id);
1481 if (ret) {
1482 fprintf(fd, "[error] %s: unexpected unary expression for event stream_id\n", __func__);
1483 ret = -EINVAL;
1484 goto error;
1485 }
1486 event->stream = trace_stream_lookup(trace, event->stream_id);
1487 if (!event->stream) {
1488 fprintf(fd, "[error] %s: stream id %" PRIu64 " cannot be found\n", __func__, event->stream_id);
1489 ret = -EINVAL;
1490 goto error;
1491 }
1492 CTF_EVENT_SET_FIELD(event, stream_id);
1493 } else if (!strcmp(left, "context")) {
1494 struct declaration *declaration;
1495
1496 if (event->context_decl) {
1497 fprintf(fd, "[error] %s: context already declared in event declaration\n", __func__);
1498 ret = -EINVAL;
1499 goto error;
1500 }
1501 declaration = ctf_type_specifier_list_visit(fd, depth,
1502 _cds_list_first_entry(&node->u.ctf_expression.right,
1503 struct ctf_node, siblings),
1504 event->declaration_scope, trace);
1505 if (!declaration) {
1506 ret = -EPERM;
1507 goto error;
1508 }
1509 if (declaration->id != CTF_TYPE_STRUCT) {
1510 ret = -EPERM;
1511 goto error;
1512 }
1513 event->context_decl = container_of(declaration, struct declaration_struct, p);
1514 } else if (!strcmp(left, "fields")) {
1515 struct declaration *declaration;
1516
1517 if (event->fields_decl) {
1518 fprintf(fd, "[error] %s: fields already declared in event declaration\n", __func__);
1519 ret = -EINVAL;
1520 goto error;
1521 }
1522 declaration = ctf_type_specifier_list_visit(fd, depth,
1523 _cds_list_first_entry(&node->u.ctf_expression.right,
1524 struct ctf_node, siblings),
1525 event->declaration_scope, trace);
1526 if (!declaration) {
1527 ret = -EPERM;
1528 goto error;
1529 }
1530 if (declaration->id != CTF_TYPE_STRUCT) {
1531 ret = -EPERM;
1532 goto error;
1533 }
1534 event->fields_decl = container_of(declaration, struct declaration_struct, p);
1535 } else {
1536 fprintf(fd, "[error] %s: attribute \"%s\" is unknown in event declaration.\n", __func__, left);
1537 ret = -EINVAL;
1538 goto error;
1539 }
1540 error:
1541 g_free(left);
1542 break;
1543 }
1544 default:
1545 return -EPERM;
1546 /* TODO: declaration specifier should be added. */
1547 }
1548
1549 return ret;
1550 }
1551
1552 static
1553 int ctf_event_visit(FILE *fd, int depth, struct ctf_node *node,
1554 struct declaration_scope *parent_declaration_scope, struct ctf_trace *trace)
1555 {
1556 int ret = 0;
1557 struct ctf_node *iter;
1558 struct ctf_event *event;
1559
1560 event = g_new0(struct ctf_event, 1);
1561 event->declaration_scope = new_declaration_scope(parent_declaration_scope);
1562 cds_list_for_each_entry(iter, &node->u.event.declaration_list, siblings) {
1563 ret = ctf_event_declaration_visit(fd, depth + 1, iter, event, trace);
1564 if (ret)
1565 goto error;
1566 }
1567 if (!CTF_EVENT_FIELD_IS_SET(event, name)) {
1568 ret = -EPERM;
1569 fprintf(fd, "[error] %s: missing name field in event declaration\n", __func__);
1570 goto error;
1571 }
1572 if (!CTF_EVENT_FIELD_IS_SET(event, stream_id)) {
1573 /* Allow missing stream_id if there is only a single stream */
1574 switch (trace->streams->len) {
1575 case 0: /* Create stream if there was none. */
1576 ret = ctf_stream_visit(fd, depth, NULL, trace->root_declaration_scope, trace);
1577 if (ret)
1578 goto error;
1579 /* Fall-through */
1580 case 1:
1581 event->stream_id = 0;
1582 event->stream = trace_stream_lookup(trace, event->stream_id);
1583 break;
1584 default:
1585 ret = -EPERM;
1586 fprintf(fd, "[error] %s: missing stream_id field in event declaration\n", __func__);
1587 goto error;
1588 }
1589 }
1590 /* Allow only one event without id per stream */
1591 if (!CTF_EVENT_FIELD_IS_SET(event, id)
1592 && event->stream->events_by_id->len != 0) {
1593 ret = -EPERM;
1594 fprintf(fd, "[error] %s: missing id field in event declaration\n", __func__);
1595 goto error;
1596 }
1597 if (event->stream->events_by_id->len <= event->id)
1598 g_ptr_array_set_size(event->stream->events_by_id, event->id + 1);
1599 g_ptr_array_index(event->stream->events_by_id, event->id) = event;
1600 g_hash_table_insert(event->stream->event_quark_to_id,
1601 (gpointer)(unsigned long) event->name,
1602 &event->id);
1603 return 0;
1604
1605 error:
1606 if (event->fields_decl)
1607 declaration_unref(&event->fields_decl->p);
1608 if (event->context_decl)
1609 declaration_unref(&event->context_decl->p);
1610 free_declaration_scope(event->declaration_scope);
1611 g_free(event);
1612 return ret;
1613 }
1614
1615
1616 static
1617 int ctf_stream_declaration_visit(FILE *fd, int depth, struct ctf_node *node, struct ctf_stream_class *stream, struct ctf_trace *trace)
1618 {
1619 int ret = 0;
1620
1621 switch (node->type) {
1622 case NODE_TYPEDEF:
1623 ret = ctf_typedef_visit(fd, depth + 1,
1624 stream->declaration_scope,
1625 node->u._typedef.type_specifier_list,
1626 &node->u._typedef.type_declarators,
1627 trace);
1628 if (ret)
1629 return ret;
1630 break;
1631 case NODE_TYPEALIAS:
1632 ret = ctf_typealias_visit(fd, depth + 1,
1633 stream->declaration_scope,
1634 node->u.typealias.target, node->u.typealias.alias,
1635 trace);
1636 if (ret)
1637 return ret;
1638 break;
1639 case NODE_CTF_EXPRESSION:
1640 {
1641 char *left;
1642
1643 left = concatenate_unary_strings(&node->u.ctf_expression.left);
1644 if (!strcmp(left, "id")) {
1645 if (CTF_STREAM_FIELD_IS_SET(stream, stream_id)) {
1646 fprintf(fd, "[error] %s: id already declared in stream declaration\n", __func__);
1647 ret = -EPERM;
1648 goto error;
1649 }
1650 ret = get_unary_unsigned(&node->u.ctf_expression.right, &stream->stream_id);
1651 if (ret) {
1652 fprintf(fd, "[error] %s: unexpected unary expression for stream id\n", __func__);
1653 ret = -EINVAL;
1654 goto error;
1655 }
1656 CTF_STREAM_SET_FIELD(stream, stream_id);
1657 } else if (!strcmp(left, "event.header")) {
1658 struct declaration *declaration;
1659
1660 if (stream->event_header_decl) {
1661 fprintf(fd, "[error] %s: event.header already declared in stream declaration\n", __func__);
1662 ret = -EINVAL;
1663 goto error;
1664 }
1665 declaration = ctf_type_specifier_list_visit(fd, depth,
1666 _cds_list_first_entry(&node->u.ctf_expression.right,
1667 struct ctf_node, siblings),
1668 stream->declaration_scope, trace);
1669 if (!declaration) {
1670 ret = -EPERM;
1671 goto error;
1672 }
1673 if (declaration->id != CTF_TYPE_STRUCT) {
1674 ret = -EPERM;
1675 goto error;
1676 }
1677 stream->event_header_decl = container_of(declaration, struct declaration_struct, p);
1678 } else if (!strcmp(left, "event.context")) {
1679 struct declaration *declaration;
1680
1681 if (stream->event_context_decl) {
1682 fprintf(fd, "[error] %s: event.context already declared in stream declaration\n", __func__);
1683 ret = -EINVAL;
1684 goto error;
1685 }
1686 declaration = ctf_type_specifier_list_visit(fd, depth,
1687 _cds_list_first_entry(&node->u.ctf_expression.right,
1688 struct ctf_node, siblings),
1689 stream->declaration_scope, trace);
1690 if (!declaration) {
1691 ret = -EPERM;
1692 goto error;
1693 }
1694 if (declaration->id != CTF_TYPE_STRUCT) {
1695 ret = -EPERM;
1696 goto error;
1697 }
1698 stream->event_context_decl = container_of(declaration, struct declaration_struct, p);
1699 } else if (!strcmp(left, "packet.context")) {
1700 struct declaration *declaration;
1701
1702 if (stream->packet_context_decl) {
1703 fprintf(fd, "[error] %s: packet.context already declared in stream declaration\n", __func__);
1704 ret = -EINVAL;
1705 goto error;
1706 }
1707 declaration = ctf_type_specifier_list_visit(fd, depth,
1708 _cds_list_first_entry(&node->u.ctf_expression.right,
1709 struct ctf_node, siblings),
1710 stream->declaration_scope, trace);
1711 if (!declaration) {
1712 ret = -EPERM;
1713 goto error;
1714 }
1715 if (declaration->id != CTF_TYPE_STRUCT) {
1716 ret = -EPERM;
1717 goto error;
1718 }
1719 stream->packet_context_decl = container_of(declaration, struct declaration_struct, p);
1720 } else {
1721 fprintf(fd, "[error] %s: attribute \"%s\" is unknown in stream declaration.\n", __func__, left);
1722 ret = -EINVAL;
1723 goto error;
1724 }
1725
1726 error:
1727 g_free(left);
1728 break;
1729 }
1730 default:
1731 return -EPERM;
1732 /* TODO: declaration specifier should be added. */
1733 }
1734
1735 return ret;
1736 }
1737
1738 static
1739 int ctf_stream_visit(FILE *fd, int depth, struct ctf_node *node,
1740 struct declaration_scope *parent_declaration_scope, struct ctf_trace *trace)
1741 {
1742 int ret = 0;
1743 struct ctf_node *iter;
1744 struct ctf_stream_class *stream;
1745
1746 stream = g_new0(struct ctf_stream_class, 1);
1747 stream->declaration_scope = new_declaration_scope(parent_declaration_scope);
1748 stream->events_by_id = g_ptr_array_new();
1749 stream->event_quark_to_id = g_hash_table_new(g_direct_hash, g_direct_equal);
1750 stream->files = g_ptr_array_new();
1751 if (node) {
1752 cds_list_for_each_entry(iter, &node->u.stream.declaration_list, siblings) {
1753 ret = ctf_stream_declaration_visit(fd, depth + 1, iter, stream, trace);
1754 if (ret)
1755 goto error;
1756 }
1757 }
1758 if (CTF_STREAM_FIELD_IS_SET(stream, stream_id)) {
1759 /* check that packet header has stream_id field. */
1760 if (!trace->packet_header_decl
1761 || struct_declaration_lookup_field_index(trace->packet_header_decl, g_quark_from_static_string("stream_id")) < 0) {
1762 ret = -EPERM;
1763 fprintf(fd, "[error] %s: missing stream_id field in packet header declaration, but stream_id attribute is declared for stream.\n", __func__);
1764 goto error;
1765 }
1766 } else {
1767 /* Allow only one id-less stream */
1768 if (trace->streams->len != 0) {
1769 ret = -EPERM;
1770 fprintf(fd, "[error] %s: missing id field in stream declaration\n", __func__);
1771 goto error;
1772 }
1773 stream->stream_id = 0;
1774 }
1775 if (trace->streams->len <= stream->stream_id)
1776 g_ptr_array_set_size(trace->streams, stream->stream_id + 1);
1777 g_ptr_array_index(trace->streams, stream->stream_id) = stream;
1778
1779 return 0;
1780
1781 error:
1782 if (stream->event_header_decl)
1783 declaration_unref(&stream->event_header_decl->p);
1784 if (stream->event_context_decl)
1785 declaration_unref(&stream->event_context_decl->p);
1786 if (stream->packet_context_decl)
1787 declaration_unref(&stream->packet_context_decl->p);
1788 g_ptr_array_free(stream->files, TRUE);
1789 g_ptr_array_free(stream->events_by_id, TRUE);
1790 g_hash_table_destroy(stream->event_quark_to_id);
1791 free_declaration_scope(stream->declaration_scope);
1792 g_free(stream);
1793 return ret;
1794 }
1795
1796 static
1797 int ctf_trace_declaration_visit(FILE *fd, int depth, struct ctf_node *node, struct ctf_trace *trace)
1798 {
1799 int ret = 0;
1800
1801 switch (node->type) {
1802 case NODE_TYPEDEF:
1803 ret = ctf_typedef_visit(fd, depth + 1,
1804 trace->declaration_scope,
1805 node->u._typedef.type_specifier_list,
1806 &node->u._typedef.type_declarators,
1807 trace);
1808 if (ret)
1809 return ret;
1810 break;
1811 case NODE_TYPEALIAS:
1812 ret = ctf_typealias_visit(fd, depth + 1,
1813 trace->declaration_scope,
1814 node->u.typealias.target, node->u.typealias.alias,
1815 trace);
1816 if (ret)
1817 return ret;
1818 break;
1819 case NODE_CTF_EXPRESSION:
1820 {
1821 char *left;
1822
1823 left = concatenate_unary_strings(&node->u.ctf_expression.left);
1824 if (!strcmp(left, "major")) {
1825 if (CTF_TRACE_FIELD_IS_SET(trace, major)) {
1826 fprintf(fd, "[error] %s: major already declared in trace declaration\n", __func__);
1827 ret = -EPERM;
1828 goto error;
1829 }
1830 ret = get_unary_unsigned(&node->u.ctf_expression.right, &trace->major);
1831 if (ret) {
1832 fprintf(fd, "[error] %s: unexpected unary expression for trace major number\n", __func__);
1833 ret = -EINVAL;
1834 goto error;
1835 }
1836 CTF_TRACE_SET_FIELD(trace, major);
1837 } else if (!strcmp(left, "minor")) {
1838 if (CTF_TRACE_FIELD_IS_SET(trace, minor)) {
1839 fprintf(fd, "[error] %s: minor already declared in trace declaration\n", __func__);
1840 ret = -EPERM;
1841 goto error;
1842 }
1843 ret = get_unary_unsigned(&node->u.ctf_expression.right, &trace->minor);
1844 if (ret) {
1845 fprintf(fd, "[error] %s: unexpected unary expression for trace minor number\n", __func__);
1846 ret = -EINVAL;
1847 goto error;
1848 }
1849 CTF_TRACE_SET_FIELD(trace, minor);
1850 } else if (!strcmp(left, "uuid")) {
1851 uuid_t uuid;
1852
1853 ret = get_unary_uuid(&node->u.ctf_expression.right, &uuid);
1854 if (ret) {
1855 fprintf(fd, "[error] %s: unexpected unary expression for trace uuid\n", __func__);
1856 ret = -EINVAL;
1857 goto error;
1858 }
1859 if (CTF_TRACE_FIELD_IS_SET(trace, uuid)
1860 && uuid_compare(uuid, trace->uuid)) {
1861 fprintf(fd, "[error] %s: uuid mismatch\n", __func__);
1862 ret = -EPERM;
1863 goto error;
1864 } else {
1865 memcpy(trace->uuid, uuid, sizeof(uuid));
1866 }
1867 CTF_TRACE_SET_FIELD(trace, uuid);
1868 } else if (!strcmp(left, "byte_order")) {
1869 struct ctf_node *right;
1870 int byte_order;
1871
1872 right = _cds_list_first_entry(&node->u.ctf_expression.right, struct ctf_node, siblings);
1873 byte_order = get_trace_byte_order(fd, depth, right);
1874 if (byte_order < 0)
1875 return -EINVAL;
1876
1877 if (CTF_TRACE_FIELD_IS_SET(trace, byte_order)
1878 && byte_order != trace->byte_order) {
1879 fprintf(fd, "[error] %s: endianness mismatch\n", __func__);
1880 ret = -EPERM;
1881 goto error;
1882 } else {
1883 trace->byte_order = byte_order;
1884 }
1885 CTF_TRACE_SET_FIELD(trace, byte_order);
1886 } else if (!strcmp(left, "packet.header")) {
1887 struct declaration *declaration;
1888
1889 if (trace->packet_header_decl) {
1890 fprintf(fd, "[error] %s: packet.header already declared in trace declaration\n", __func__);
1891 ret = -EINVAL;
1892 goto error;
1893 }
1894 declaration = ctf_type_specifier_list_visit(fd, depth,
1895 _cds_list_first_entry(&node->u.ctf_expression.right,
1896 struct ctf_node, siblings),
1897 trace->declaration_scope, trace);
1898 if (!declaration) {
1899 ret = -EPERM;
1900 goto error;
1901 }
1902 if (declaration->id != CTF_TYPE_STRUCT) {
1903 ret = -EPERM;
1904 goto error;
1905 }
1906 trace->packet_header_decl = container_of(declaration, struct declaration_struct, p);
1907 } else {
1908 fprintf(fd, "[error] %s: attribute \"%s\" is unknown in trace declaration.\n", __func__, left);
1909 ret = -EINVAL;
1910 goto error;
1911 }
1912
1913 error:
1914 g_free(left);
1915 break;
1916 }
1917 default:
1918 return -EPERM;
1919 /* TODO: declaration specifier should be added. */
1920 }
1921
1922 return ret;
1923 }
1924
1925 static
1926 int ctf_trace_visit(FILE *fd, int depth, struct ctf_node *node, struct ctf_trace *trace)
1927 {
1928 int ret = 0;
1929 struct ctf_node *iter;
1930
1931 if (trace->declaration_scope)
1932 return -EEXIST;
1933 trace->declaration_scope = new_declaration_scope(trace->root_declaration_scope);
1934 trace->streams = g_ptr_array_new();
1935 cds_list_for_each_entry(iter, &node->u.trace.declaration_list, siblings) {
1936 ret = ctf_trace_declaration_visit(fd, depth + 1, iter, trace);
1937 if (ret)
1938 goto error;
1939 }
1940 if (!CTF_TRACE_FIELD_IS_SET(trace, major)) {
1941 ret = -EPERM;
1942 fprintf(fd, "[error] %s: missing major field in trace declaration\n", __func__);
1943 goto error;
1944 }
1945 if (!CTF_TRACE_FIELD_IS_SET(trace, minor)) {
1946 ret = -EPERM;
1947 fprintf(fd, "[error] %s: missing minor field in trace declaration\n", __func__);
1948 goto error;
1949 }
1950 if (!CTF_TRACE_FIELD_IS_SET(trace, uuid)) {
1951 ret = -EPERM;
1952 fprintf(fd, "[error] %s: missing uuid field in trace declaration\n", __func__);
1953 goto error;
1954 }
1955 if (!CTF_TRACE_FIELD_IS_SET(trace, byte_order)) {
1956 ret = -EPERM;
1957 fprintf(fd, "[error] %s: missing byte_order field in trace declaration\n", __func__);
1958 goto error;
1959 }
1960
1961 if (!CTF_TRACE_FIELD_IS_SET(trace, byte_order)) {
1962 /* check that the packet header contains a "magic" field */
1963 if (!trace->packet_header_decl
1964 || struct_declaration_lookup_field_index(trace->packet_header_decl, g_quark_from_static_string("magic")) < 0) {
1965 ret = -EPERM;
1966 fprintf(fd, "[error] %s: missing both byte_order and packet header magic number in trace declaration\n", __func__);
1967 goto error;
1968 }
1969 }
1970 return 0;
1971
1972 error:
1973 if (trace->packet_header_decl)
1974 declaration_unref(&trace->packet_header_decl->p);
1975 g_ptr_array_free(trace->streams, TRUE);
1976 free_declaration_scope(trace->declaration_scope);
1977 return ret;
1978 }
1979
1980 static
1981 int ctf_root_declaration_visit(FILE *fd, int depth, struct ctf_node *node, struct ctf_trace *trace)
1982 {
1983 int ret = 0;
1984
1985 switch (node->type) {
1986 case NODE_TYPEDEF:
1987 ret = ctf_typedef_visit(fd, depth + 1,
1988 trace->root_declaration_scope,
1989 node->u._typedef.type_specifier_list,
1990 &node->u._typedef.type_declarators,
1991 trace);
1992 if (ret)
1993 return ret;
1994 break;
1995 case NODE_TYPEALIAS:
1996 ret = ctf_typealias_visit(fd, depth + 1,
1997 trace->root_declaration_scope,
1998 node->u.typealias.target, node->u.typealias.alias,
1999 trace);
2000 if (ret)
2001 return ret;
2002 break;
2003 case NODE_TYPE_SPECIFIER_LIST:
2004 {
2005 struct declaration *declaration;
2006
2007 /*
2008 * Just add the type specifier to the root scope
2009 * declaration scope. Release local reference.
2010 */
2011 declaration = ctf_type_specifier_list_visit(fd, depth + 1,
2012 node, trace->root_declaration_scope, trace);
2013 if (!declaration)
2014 return -ENOMEM;
2015 declaration_unref(declaration);
2016 break;
2017 }
2018 default:
2019 return -EPERM;
2020 }
2021
2022 return 0;
2023 }
2024
2025 int ctf_visitor_construct_metadata(FILE *fd, int depth, struct ctf_node *node,
2026 struct ctf_trace *trace, int byte_order)
2027 {
2028 int ret = 0;
2029 struct ctf_node *iter;
2030
2031 printf_verbose("CTF visitor: metadata construction... ");
2032 trace->root_declaration_scope = new_declaration_scope(NULL);
2033 trace->byte_order = byte_order;
2034
2035 switch (node->type) {
2036 case NODE_ROOT:
2037 cds_list_for_each_entry(iter, &node->u.root.declaration_list,
2038 siblings) {
2039 ret = ctf_root_declaration_visit(fd, depth + 1, iter, trace);
2040 if (ret) {
2041 fprintf(fd, "[error] %s: root declaration error\n", __func__);
2042 goto error;
2043 }
2044 }
2045 cds_list_for_each_entry(iter, &node->u.root.trace, siblings) {
2046 ret = ctf_trace_visit(fd, depth + 1, iter, trace);
2047 if (ret) {
2048 fprintf(fd, "[error] %s: trace declaration error\n", __func__);
2049 goto error;
2050 }
2051 }
2052 if (!trace->streams) {
2053 fprintf(fd, "[error] %s: missing trace declaration\n", __func__);
2054 ret = -EINVAL;
2055 goto error;
2056 }
2057 cds_list_for_each_entry(iter, &node->u.root.stream, siblings) {
2058 ret = ctf_stream_visit(fd, depth + 1, iter,
2059 trace->root_declaration_scope, trace);
2060 if (ret) {
2061 fprintf(fd, "[error] %s: stream declaration error\n", __func__);
2062 goto error;
2063 }
2064 }
2065 cds_list_for_each_entry(iter, &node->u.root.event, siblings) {
2066 ret = ctf_event_visit(fd, depth + 1, iter,
2067 trace->root_declaration_scope, trace);
2068 if (ret) {
2069 fprintf(fd, "[error] %s: event declaration error\n", __func__);
2070 goto error;
2071 }
2072 }
2073 break;
2074 case NODE_UNKNOWN:
2075 default:
2076 fprintf(fd, "[error] %s: unknown node type %d\n", __func__,
2077 (int) node->type);
2078 ret = -EINVAL;
2079 goto error;
2080 }
2081 printf_verbose("done.\n");
2082 return ret;
2083
2084 error:
2085 free_declaration_scope(trace->root_declaration_scope);
2086 return ret;
2087 }
This page took 0.113644 seconds and 4 git commands to generate.