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