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