clock: show as time of day
[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.identifier")) {
1653 char *right;
1654
1655 if (CTF_EVENT_FIELD_IS_SET(event, loglevel_identifier)) {
1656 fprintf(fd, "[error] %s: identifier already declared in event declaration\n", __func__);
1657 ret = -EPERM;
1658 goto error;
1659 }
1660 right = concatenate_unary_strings(&node->u.ctf_expression.right);
1661 if (!right) {
1662 fprintf(fd, "[error] %s: unexpected unary expression for event identifier\n", __func__);
1663 ret = -EINVAL;
1664 goto error;
1665 }
1666 event->loglevel_identifier = g_quark_from_string(right);
1667 g_free(right);
1668 CTF_EVENT_SET_FIELD(event, loglevel_identifier);
1669 } else if (!strcmp(left, "loglevel.value")) {
1670 if (CTF_EVENT_FIELD_IS_SET(event, loglevel_value)) {
1671 fprintf(fd, "[error] %s: loglevel value already declared in event declaration\n", __func__);
1672 ret = -EPERM;
1673 goto error;
1674 }
1675 ret = get_unary_signed(&node->u.ctf_expression.right, &event->loglevel_value);
1676 if (ret) {
1677 fprintf(fd, "[error] %s: unexpected unary expression for event loglevel value\n", __func__);
1678 ret = -EINVAL;
1679 goto error;
1680 }
1681 CTF_EVENT_SET_FIELD(event, loglevel_value);
1682 } else {
1683 fprintf(fd, "[warning] %s: attribute \"%s\" is unknown in event declaration.\n", __func__, left);
1684 /* Fall-through after warning */
1685 }
1686 error:
1687 g_free(left);
1688 break;
1689 }
1690 default:
1691 return -EPERM;
1692 /* TODO: declaration specifier should be added. */
1693 }
1694
1695 return ret;
1696 }
1697
1698 static
1699 int ctf_event_visit(FILE *fd, int depth, struct ctf_node *node,
1700 struct declaration_scope *parent_declaration_scope, struct ctf_trace *trace)
1701 {
1702 int ret = 0;
1703 struct ctf_node *iter;
1704 struct ctf_event *event;
1705
1706 event = g_new0(struct ctf_event, 1);
1707 event->declaration_scope = new_declaration_scope(parent_declaration_scope);
1708 cds_list_for_each_entry(iter, &node->u.event.declaration_list, siblings) {
1709 ret = ctf_event_declaration_visit(fd, depth + 1, iter, event, trace);
1710 if (ret)
1711 goto error;
1712 }
1713 if (!CTF_EVENT_FIELD_IS_SET(event, name)) {
1714 ret = -EPERM;
1715 fprintf(fd, "[error] %s: missing name field in event declaration\n", __func__);
1716 goto error;
1717 }
1718 if (!CTF_EVENT_FIELD_IS_SET(event, stream_id)) {
1719 /* Allow missing stream_id if there is only a single stream */
1720 switch (trace->streams->len) {
1721 case 0: /* Create stream if there was none. */
1722 ret = ctf_stream_visit(fd, depth, NULL, trace->root_declaration_scope, trace);
1723 if (ret)
1724 goto error;
1725 /* Fall-through */
1726 case 1:
1727 event->stream_id = 0;
1728 event->stream = trace_stream_lookup(trace, event->stream_id);
1729 break;
1730 default:
1731 ret = -EPERM;
1732 fprintf(fd, "[error] %s: missing stream_id field in event declaration\n", __func__);
1733 goto error;
1734 }
1735 }
1736 /* Allow only one event without id per stream */
1737 if (!CTF_EVENT_FIELD_IS_SET(event, id)
1738 && event->stream->events_by_id->len != 0) {
1739 ret = -EPERM;
1740 fprintf(fd, "[error] %s: missing id field in event declaration\n", __func__);
1741 goto error;
1742 }
1743 if (event->stream->events_by_id->len <= event->id)
1744 g_ptr_array_set_size(event->stream->events_by_id, event->id + 1);
1745 g_ptr_array_index(event->stream->events_by_id, event->id) = event;
1746 g_hash_table_insert(event->stream->event_quark_to_id,
1747 (gpointer) (unsigned long) event->name,
1748 &event->id);
1749 return 0;
1750
1751 error:
1752 if (event->fields_decl)
1753 declaration_unref(&event->fields_decl->p);
1754 if (event->context_decl)
1755 declaration_unref(&event->context_decl->p);
1756 free_declaration_scope(event->declaration_scope);
1757 g_free(event);
1758 return ret;
1759 }
1760
1761
1762 static
1763 int ctf_stream_declaration_visit(FILE *fd, int depth, struct ctf_node *node, struct ctf_stream_class *stream, struct ctf_trace *trace)
1764 {
1765 int ret = 0;
1766
1767 switch (node->type) {
1768 case NODE_TYPEDEF:
1769 ret = ctf_typedef_visit(fd, depth + 1,
1770 stream->declaration_scope,
1771 node->u._typedef.type_specifier_list,
1772 &node->u._typedef.type_declarators,
1773 trace);
1774 if (ret)
1775 return ret;
1776 break;
1777 case NODE_TYPEALIAS:
1778 ret = ctf_typealias_visit(fd, depth + 1,
1779 stream->declaration_scope,
1780 node->u.typealias.target, node->u.typealias.alias,
1781 trace);
1782 if (ret)
1783 return ret;
1784 break;
1785 case NODE_CTF_EXPRESSION:
1786 {
1787 char *left;
1788
1789 left = concatenate_unary_strings(&node->u.ctf_expression.left);
1790 if (!strcmp(left, "id")) {
1791 if (CTF_STREAM_FIELD_IS_SET(stream, stream_id)) {
1792 fprintf(fd, "[error] %s: id already declared in stream declaration\n", __func__);
1793 ret = -EPERM;
1794 goto error;
1795 }
1796 ret = get_unary_unsigned(&node->u.ctf_expression.right, &stream->stream_id);
1797 if (ret) {
1798 fprintf(fd, "[error] %s: unexpected unary expression for stream id\n", __func__);
1799 ret = -EINVAL;
1800 goto error;
1801 }
1802 CTF_STREAM_SET_FIELD(stream, stream_id);
1803 } else if (!strcmp(left, "event.header")) {
1804 struct declaration *declaration;
1805
1806 if (stream->event_header_decl) {
1807 fprintf(fd, "[error] %s: event.header already declared in stream declaration\n", __func__);
1808 ret = -EINVAL;
1809 goto error;
1810 }
1811 declaration = ctf_type_specifier_list_visit(fd, depth,
1812 _cds_list_first_entry(&node->u.ctf_expression.right,
1813 struct ctf_node, siblings),
1814 stream->declaration_scope, trace);
1815 if (!declaration) {
1816 ret = -EPERM;
1817 goto error;
1818 }
1819 if (declaration->id != CTF_TYPE_STRUCT) {
1820 ret = -EPERM;
1821 goto error;
1822 }
1823 stream->event_header_decl = container_of(declaration, struct declaration_struct, p);
1824 } else if (!strcmp(left, "event.context")) {
1825 struct declaration *declaration;
1826
1827 if (stream->event_context_decl) {
1828 fprintf(fd, "[error] %s: event.context already declared in stream declaration\n", __func__);
1829 ret = -EINVAL;
1830 goto error;
1831 }
1832 declaration = ctf_type_specifier_list_visit(fd, depth,
1833 _cds_list_first_entry(&node->u.ctf_expression.right,
1834 struct ctf_node, siblings),
1835 stream->declaration_scope, trace);
1836 if (!declaration) {
1837 ret = -EPERM;
1838 goto error;
1839 }
1840 if (declaration->id != CTF_TYPE_STRUCT) {
1841 ret = -EPERM;
1842 goto error;
1843 }
1844 stream->event_context_decl = container_of(declaration, struct declaration_struct, p);
1845 } else if (!strcmp(left, "packet.context")) {
1846 struct declaration *declaration;
1847
1848 if (stream->packet_context_decl) {
1849 fprintf(fd, "[error] %s: packet.context already declared in stream declaration\n", __func__);
1850 ret = -EINVAL;
1851 goto error;
1852 }
1853 declaration = ctf_type_specifier_list_visit(fd, depth,
1854 _cds_list_first_entry(&node->u.ctf_expression.right,
1855 struct ctf_node, siblings),
1856 stream->declaration_scope, trace);
1857 if (!declaration) {
1858 ret = -EPERM;
1859 goto error;
1860 }
1861 if (declaration->id != CTF_TYPE_STRUCT) {
1862 ret = -EPERM;
1863 goto error;
1864 }
1865 stream->packet_context_decl = container_of(declaration, struct declaration_struct, p);
1866 } else {
1867 fprintf(fd, "[warning] %s: attribute \"%s\" is unknown in stream declaration.\n", __func__, left);
1868 /* Fall-through after warning */
1869 }
1870
1871 error:
1872 g_free(left);
1873 break;
1874 }
1875 default:
1876 return -EPERM;
1877 /* TODO: declaration specifier should be added. */
1878 }
1879
1880 return ret;
1881 }
1882
1883 static
1884 int ctf_stream_visit(FILE *fd, int depth, struct ctf_node *node,
1885 struct declaration_scope *parent_declaration_scope, struct ctf_trace *trace)
1886 {
1887 int ret = 0;
1888 struct ctf_node *iter;
1889 struct ctf_stream_class *stream;
1890
1891 stream = g_new0(struct ctf_stream_class, 1);
1892 stream->declaration_scope = new_declaration_scope(parent_declaration_scope);
1893 stream->events_by_id = g_ptr_array_new();
1894 stream->event_quark_to_id = g_hash_table_new(g_direct_hash, g_direct_equal);
1895 stream->streams = g_ptr_array_new();
1896 if (node) {
1897 cds_list_for_each_entry(iter, &node->u.stream.declaration_list, siblings) {
1898 ret = ctf_stream_declaration_visit(fd, depth + 1, iter, stream, trace);
1899 if (ret)
1900 goto error;
1901 }
1902 }
1903 if (CTF_STREAM_FIELD_IS_SET(stream, stream_id)) {
1904 /* check that packet header has stream_id field. */
1905 if (!trace->packet_header_decl
1906 || struct_declaration_lookup_field_index(trace->packet_header_decl, g_quark_from_static_string("stream_id")) < 0) {
1907 ret = -EPERM;
1908 fprintf(fd, "[error] %s: missing stream_id field in packet header declaration, but stream_id attribute is declared for stream.\n", __func__);
1909 goto error;
1910 }
1911 } else {
1912 /* Allow only one id-less stream */
1913 if (trace->streams->len != 0) {
1914 ret = -EPERM;
1915 fprintf(fd, "[error] %s: missing id field in stream declaration\n", __func__);
1916 goto error;
1917 }
1918 stream->stream_id = 0;
1919 }
1920 if (trace->streams->len <= stream->stream_id)
1921 g_ptr_array_set_size(trace->streams, stream->stream_id + 1);
1922 g_ptr_array_index(trace->streams, stream->stream_id) = stream;
1923 stream->trace = trace;
1924
1925 return 0;
1926
1927 error:
1928 if (stream->event_header_decl)
1929 declaration_unref(&stream->event_header_decl->p);
1930 if (stream->event_context_decl)
1931 declaration_unref(&stream->event_context_decl->p);
1932 if (stream->packet_context_decl)
1933 declaration_unref(&stream->packet_context_decl->p);
1934 g_ptr_array_free(stream->streams, TRUE);
1935 g_ptr_array_free(stream->events_by_id, TRUE);
1936 g_hash_table_destroy(stream->event_quark_to_id);
1937 free_declaration_scope(stream->declaration_scope);
1938 g_free(stream);
1939 return ret;
1940 }
1941
1942 static
1943 int ctf_trace_declaration_visit(FILE *fd, int depth, struct ctf_node *node, struct ctf_trace *trace)
1944 {
1945 int ret = 0;
1946
1947 switch (node->type) {
1948 case NODE_TYPEDEF:
1949 ret = ctf_typedef_visit(fd, depth + 1,
1950 trace->declaration_scope,
1951 node->u._typedef.type_specifier_list,
1952 &node->u._typedef.type_declarators,
1953 trace);
1954 if (ret)
1955 return ret;
1956 break;
1957 case NODE_TYPEALIAS:
1958 ret = ctf_typealias_visit(fd, depth + 1,
1959 trace->declaration_scope,
1960 node->u.typealias.target, node->u.typealias.alias,
1961 trace);
1962 if (ret)
1963 return ret;
1964 break;
1965 case NODE_CTF_EXPRESSION:
1966 {
1967 char *left;
1968
1969 left = concatenate_unary_strings(&node->u.ctf_expression.left);
1970 if (!strcmp(left, "major")) {
1971 if (CTF_TRACE_FIELD_IS_SET(trace, major)) {
1972 fprintf(fd, "[error] %s: major 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->major);
1977 if (ret) {
1978 fprintf(fd, "[error] %s: unexpected unary expression for trace major number\n", __func__);
1979 ret = -EINVAL;
1980 goto error;
1981 }
1982 CTF_TRACE_SET_FIELD(trace, major);
1983 } else if (!strcmp(left, "minor")) {
1984 if (CTF_TRACE_FIELD_IS_SET(trace, minor)) {
1985 fprintf(fd, "[error] %s: minor already declared in trace declaration\n", __func__);
1986 ret = -EPERM;
1987 goto error;
1988 }
1989 ret = get_unary_unsigned(&node->u.ctf_expression.right, &trace->minor);
1990 if (ret) {
1991 fprintf(fd, "[error] %s: unexpected unary expression for trace minor number\n", __func__);
1992 ret = -EINVAL;
1993 goto error;
1994 }
1995 CTF_TRACE_SET_FIELD(trace, minor);
1996 } else if (!strcmp(left, "uuid")) {
1997 uuid_t uuid;
1998
1999 ret = get_unary_uuid(&node->u.ctf_expression.right, &uuid);
2000 if (ret) {
2001 fprintf(fd, "[error] %s: unexpected unary expression for trace uuid\n", __func__);
2002 ret = -EINVAL;
2003 goto error;
2004 }
2005 if (CTF_TRACE_FIELD_IS_SET(trace, uuid)
2006 && uuid_compare(uuid, trace->uuid)) {
2007 fprintf(fd, "[error] %s: uuid mismatch\n", __func__);
2008 ret = -EPERM;
2009 goto error;
2010 } else {
2011 memcpy(trace->uuid, uuid, sizeof(uuid));
2012 }
2013 CTF_TRACE_SET_FIELD(trace, uuid);
2014 } else if (!strcmp(left, "byte_order")) {
2015 struct ctf_node *right;
2016 int byte_order;
2017
2018 right = _cds_list_first_entry(&node->u.ctf_expression.right, struct ctf_node, siblings);
2019 byte_order = get_trace_byte_order(fd, depth, right);
2020 if (byte_order < 0)
2021 return -EINVAL;
2022
2023 if (CTF_TRACE_FIELD_IS_SET(trace, byte_order)
2024 && byte_order != trace->byte_order) {
2025 fprintf(fd, "[error] %s: endianness mismatch\n", __func__);
2026 ret = -EPERM;
2027 goto error;
2028 } else {
2029 if (byte_order != trace->byte_order) {
2030 trace->byte_order = byte_order;
2031 /*
2032 * We need to restart
2033 * construction of the
2034 * intermediate representation.
2035 */
2036 trace->field_mask = 0;
2037 CTF_TRACE_SET_FIELD(trace, byte_order);
2038 ret = -EINTR;
2039 goto error;
2040 }
2041 }
2042 CTF_TRACE_SET_FIELD(trace, byte_order);
2043 } else if (!strcmp(left, "packet.header")) {
2044 struct declaration *declaration;
2045
2046 if (trace->packet_header_decl) {
2047 fprintf(fd, "[error] %s: packet.header already declared in trace declaration\n", __func__);
2048 ret = -EINVAL;
2049 goto error;
2050 }
2051 declaration = ctf_type_specifier_list_visit(fd, depth,
2052 _cds_list_first_entry(&node->u.ctf_expression.right,
2053 struct ctf_node, siblings),
2054 trace->declaration_scope, trace);
2055 if (!declaration) {
2056 ret = -EPERM;
2057 goto error;
2058 }
2059 if (declaration->id != CTF_TYPE_STRUCT) {
2060 ret = -EPERM;
2061 goto error;
2062 }
2063 trace->packet_header_decl = container_of(declaration, struct declaration_struct, p);
2064 } else {
2065 fprintf(fd, "[warning] %s: attribute \"%s\" is unknown in trace declaration.\n", __func__, left);
2066 }
2067
2068 error:
2069 g_free(left);
2070 break;
2071 }
2072 default:
2073 return -EPERM;
2074 /* TODO: declaration specifier should be added. */
2075 }
2076
2077 return ret;
2078 }
2079
2080 static
2081 int ctf_trace_visit(FILE *fd, int depth, struct ctf_node *node, struct ctf_trace *trace)
2082 {
2083 int ret = 0;
2084 struct ctf_node *iter;
2085
2086 if (trace->declaration_scope)
2087 return -EEXIST;
2088 trace->declaration_scope = new_declaration_scope(trace->root_declaration_scope);
2089 trace->streams = g_ptr_array_new();
2090 cds_list_for_each_entry(iter, &node->u.trace.declaration_list, siblings) {
2091 ret = ctf_trace_declaration_visit(fd, depth + 1, iter, trace);
2092 if (ret)
2093 goto error;
2094 }
2095 if (!CTF_TRACE_FIELD_IS_SET(trace, major)) {
2096 ret = -EPERM;
2097 fprintf(fd, "[error] %s: missing major field in trace declaration\n", __func__);
2098 goto error;
2099 }
2100 if (!CTF_TRACE_FIELD_IS_SET(trace, minor)) {
2101 ret = -EPERM;
2102 fprintf(fd, "[error] %s: missing minor field in trace declaration\n", __func__);
2103 goto error;
2104 }
2105 if (!CTF_TRACE_FIELD_IS_SET(trace, uuid)) {
2106 ret = -EPERM;
2107 fprintf(fd, "[error] %s: missing uuid field in trace declaration\n", __func__);
2108 goto error;
2109 }
2110 if (!CTF_TRACE_FIELD_IS_SET(trace, byte_order)) {
2111 ret = -EPERM;
2112 fprintf(fd, "[error] %s: missing byte_order field in trace declaration\n", __func__);
2113 goto error;
2114 }
2115
2116 if (!CTF_TRACE_FIELD_IS_SET(trace, byte_order)) {
2117 /* check that the packet header contains a "magic" field */
2118 if (!trace->packet_header_decl
2119 || struct_declaration_lookup_field_index(trace->packet_header_decl, g_quark_from_static_string("magic")) < 0) {
2120 ret = -EPERM;
2121 fprintf(fd, "[error] %s: missing both byte_order and packet header magic number in trace declaration\n", __func__);
2122 goto error;
2123 }
2124 }
2125 return 0;
2126
2127 error:
2128 if (trace->packet_header_decl) {
2129 declaration_unref(&trace->packet_header_decl->p);
2130 trace->packet_header_decl = NULL;
2131 }
2132 g_ptr_array_free(trace->streams, TRUE);
2133 free_declaration_scope(trace->declaration_scope);
2134 trace->declaration_scope = NULL;
2135 return ret;
2136 }
2137
2138 static
2139 int ctf_clock_declaration_visit(FILE *fd, int depth, struct ctf_node *node,
2140 struct ctf_clock *clock, struct ctf_trace *trace)
2141 {
2142 int ret = 0;
2143
2144 switch (node->type) {
2145 case NODE_CTF_EXPRESSION:
2146 {
2147 char *left;
2148
2149 left = concatenate_unary_strings(&node->u.ctf_expression.left);
2150 if (!strcmp(left, "name")) {
2151 char *right;
2152
2153 if (CTF_CLOCK_FIELD_IS_SET(clock, name)) {
2154 fprintf(fd, "[error] %s: name already declared in clock declaration\n", __func__);
2155 ret = -EPERM;
2156 goto error;
2157 }
2158 right = concatenate_unary_strings(&node->u.ctf_expression.right);
2159 if (!right) {
2160 fprintf(fd, "[error] %s: unexpected unary expression for clock name\n", __func__);
2161 ret = -EINVAL;
2162 goto error;
2163 }
2164 clock->name = g_quark_from_string(right);
2165 g_free(right);
2166 CTF_EVENT_SET_FIELD(clock, name);
2167 } else if (!strcmp(left, "uuid")) {
2168 char *right;
2169
2170 if (clock->uuid) {
2171 fprintf(fd, "[error] %s: uuid already declared in clock declaration\n", __func__);
2172 ret = -EPERM;
2173 goto error;
2174 }
2175 right = concatenate_unary_strings(&node->u.ctf_expression.right);
2176 if (!right) {
2177 fprintf(fd, "[error] %s: unexpected unary expression for clock uuid\n", __func__);
2178 ret = -EINVAL;
2179 goto error;
2180 }
2181 clock->uuid = g_quark_from_string(right);
2182 g_free(right);
2183 } else if (!strcmp(left, "description")) {
2184 char *right;
2185
2186 if (clock->description) {
2187 fprintf(fd, "[warning] %s: duplicated clock description\n", __func__);
2188 goto error; /* ret is 0, so not an actual error, just warn. */
2189 }
2190 right = concatenate_unary_strings(&node->u.ctf_expression.right);
2191 if (!right) {
2192 fprintf(fd, "[warning] %s: unexpected unary expression for clock description\n", __func__);
2193 goto error; /* ret is 0, so not an actual error, just warn. */
2194 }
2195 clock->description = right;
2196 } else if (!strcmp(left, "freq")) {
2197 if (clock->freq) {
2198 fprintf(fd, "[error] %s: freq already declared in clock declaration\n", __func__);
2199 ret = -EPERM;
2200 goto error;
2201 }
2202 ret = get_unary_unsigned(&node->u.ctf_expression.right, &clock->freq);
2203 if (ret) {
2204 fprintf(fd, "[error] %s: unexpected unary expression for clock freq\n", __func__);
2205 ret = -EINVAL;
2206 goto error;
2207 }
2208 } else if (!strcmp(left, "precision")) {
2209 if (clock->precision) {
2210 fprintf(fd, "[error] %s: precision already declared in clock declaration\n", __func__);
2211 ret = -EPERM;
2212 goto error;
2213 }
2214 ret = get_unary_unsigned(&node->u.ctf_expression.right, &clock->precision);
2215 if (ret) {
2216 fprintf(fd, "[error] %s: unexpected unary expression for clock precision\n", __func__);
2217 ret = -EINVAL;
2218 goto error;
2219 }
2220 } else if (!strcmp(left, "offset_s")) {
2221 if (clock->offset_s) {
2222 fprintf(fd, "[error] %s: offset_s already declared in clock declaration\n", __func__);
2223 ret = -EPERM;
2224 goto error;
2225 }
2226 ret = get_unary_unsigned(&node->u.ctf_expression.right, &clock->offset_s);
2227 if (ret) {
2228 fprintf(fd, "[error] %s: unexpected unary expression for clock offset_s\n", __func__);
2229 ret = -EINVAL;
2230 goto error;
2231 }
2232 } else if (!strcmp(left, "offset")) {
2233 if (clock->offset) {
2234 fprintf(fd, "[error] %s: offset already declared in clock declaration\n", __func__);
2235 ret = -EPERM;
2236 goto error;
2237 }
2238 ret = get_unary_unsigned(&node->u.ctf_expression.right, &clock->offset);
2239 if (ret) {
2240 fprintf(fd, "[error] %s: unexpected unary expression for clock offset\n", __func__);
2241 ret = -EINVAL;
2242 goto error;
2243 }
2244 } else if (!strcmp(left, "absolute")) {
2245 struct ctf_node *right;
2246
2247 right = _cds_list_first_entry(&node->u.ctf_expression.right, struct ctf_node, siblings);
2248 ret = get_boolean(fd, depth, right);
2249 if (ret < 0) {
2250 fprintf(fd, "[error] %s: unexpected \"absolute\" right member\n", __func__);
2251 ret = -EINVAL;
2252 goto error;
2253 }
2254 clock->absolute = ret;
2255 } else {
2256 fprintf(fd, "[warning] %s: attribute \"%s\" is unknown in clock declaration.\n", __func__, left);
2257 }
2258
2259 error:
2260 g_free(left);
2261 break;
2262 }
2263 default:
2264 return -EPERM;
2265 /* TODO: declaration specifier should be added. */
2266 }
2267
2268 return ret;
2269 }
2270
2271 static
2272 int ctf_clock_visit(FILE *fd, int depth, struct ctf_node *node, struct ctf_trace *trace)
2273 {
2274 int ret = 0;
2275 struct ctf_node *iter;
2276 struct ctf_clock *clock;
2277
2278 clock = g_new0(struct ctf_clock, 1);
2279 cds_list_for_each_entry(iter, &node->u.clock.declaration_list, siblings) {
2280 ret = ctf_clock_declaration_visit(fd, depth + 1, iter, clock, trace);
2281 if (ret)
2282 goto error;
2283 }
2284 if (!CTF_CLOCK_FIELD_IS_SET(clock, name)) {
2285 ret = -EPERM;
2286 fprintf(fd, "[error] %s: missing namefield in clock declaration\n", __func__);
2287 goto error;
2288 }
2289 g_hash_table_insert(trace->clocks, (gpointer) (unsigned long) clock->name, clock);
2290 return 0;
2291
2292 error:
2293 g_free(clock->description);
2294 g_free(clock);
2295 return ret;
2296 }
2297
2298 static
2299 void clock_free(gpointer data)
2300 {
2301 struct ctf_clock *clock = data;
2302
2303 g_free(clock->description);
2304 g_free(clock);
2305 }
2306
2307 static
2308 int ctf_root_declaration_visit(FILE *fd, int depth, struct ctf_node *node, struct ctf_trace *trace)
2309 {
2310 int ret = 0;
2311
2312 switch (node->type) {
2313 case NODE_TYPEDEF:
2314 ret = ctf_typedef_visit(fd, depth + 1,
2315 trace->root_declaration_scope,
2316 node->u._typedef.type_specifier_list,
2317 &node->u._typedef.type_declarators,
2318 trace);
2319 if (ret)
2320 return ret;
2321 break;
2322 case NODE_TYPEALIAS:
2323 ret = ctf_typealias_visit(fd, depth + 1,
2324 trace->root_declaration_scope,
2325 node->u.typealias.target, node->u.typealias.alias,
2326 trace);
2327 if (ret)
2328 return ret;
2329 break;
2330 case NODE_TYPE_SPECIFIER_LIST:
2331 {
2332 struct declaration *declaration;
2333
2334 /*
2335 * Just add the type specifier to the root scope
2336 * declaration scope. Release local reference.
2337 */
2338 declaration = ctf_type_specifier_list_visit(fd, depth + 1,
2339 node, trace->root_declaration_scope, trace);
2340 if (!declaration)
2341 return -ENOMEM;
2342 declaration_unref(declaration);
2343 break;
2344 }
2345 default:
2346 return -EPERM;
2347 }
2348
2349 return 0;
2350 }
2351
2352 /* TODO: missing metadata "destroy" (memory leak) */
2353 int ctf_visitor_construct_metadata(FILE *fd, int depth, struct ctf_node *node,
2354 struct ctf_trace *trace, int byte_order)
2355 {
2356 int ret = 0;
2357 struct ctf_node *iter;
2358 int clock_done = 0;
2359
2360 printf_verbose("CTF visitor: metadata construction... ");
2361 trace->byte_order = byte_order;
2362 trace->clocks = g_hash_table_new_full(g_direct_hash, g_direct_equal,
2363 NULL, clock_free);
2364
2365 retry:
2366 trace->root_declaration_scope = new_declaration_scope(NULL);
2367
2368 switch (node->type) {
2369 case NODE_ROOT:
2370 if (!clock_done) {
2371 /*
2372 * declarations need to query clock hash table,
2373 * so clock need to be treated first.
2374 */
2375 cds_list_for_each_entry(iter, &node->u.root.clock, siblings) {
2376 ret = ctf_clock_visit(fd, depth + 1, iter,
2377 trace);
2378 if (ret) {
2379 fprintf(fd, "[error] %s: clock declaration error\n", __func__);
2380 goto error;
2381 }
2382 }
2383 clock_done = 1;
2384 }
2385 cds_list_for_each_entry(iter, &node->u.root.declaration_list,
2386 siblings) {
2387 ret = ctf_root_declaration_visit(fd, depth + 1, iter, trace);
2388 if (ret) {
2389 fprintf(fd, "[error] %s: root declaration error\n", __func__);
2390 goto error;
2391 }
2392 }
2393 cds_list_for_each_entry(iter, &node->u.root.trace, siblings) {
2394 ret = ctf_trace_visit(fd, depth + 1, iter, trace);
2395 if (ret == -EINTR) {
2396 free_declaration_scope(trace->root_declaration_scope);
2397 /*
2398 * Need to restart creation of type
2399 * definitions, aliases and
2400 * trace header declarations.
2401 */
2402 goto retry;
2403 }
2404 if (ret) {
2405 fprintf(fd, "[error] %s: trace declaration error\n", __func__);
2406 goto error;
2407 }
2408 }
2409 if (!trace->streams) {
2410 fprintf(fd, "[error] %s: missing trace declaration\n", __func__);
2411 ret = -EINVAL;
2412 goto error;
2413 }
2414 cds_list_for_each_entry(iter, &node->u.root.stream, siblings) {
2415 ret = ctf_stream_visit(fd, depth + 1, iter,
2416 trace->root_declaration_scope, trace);
2417 if (ret) {
2418 fprintf(fd, "[error] %s: stream declaration error\n", __func__);
2419 goto error;
2420 }
2421 }
2422 cds_list_for_each_entry(iter, &node->u.root.event, siblings) {
2423 ret = ctf_event_visit(fd, depth + 1, iter,
2424 trace->root_declaration_scope, trace);
2425 if (ret) {
2426 fprintf(fd, "[error] %s: event declaration error\n", __func__);
2427 goto error;
2428 }
2429 }
2430 break;
2431 case NODE_UNKNOWN:
2432 default:
2433 fprintf(fd, "[error] %s: unknown node type %d\n", __func__,
2434 (int) node->type);
2435 ret = -EINVAL;
2436 goto error;
2437 }
2438 printf_verbose("done.\n");
2439 return ret;
2440
2441 error:
2442 free_declaration_scope(trace->root_declaration_scope);
2443 g_hash_table_destroy(trace->clocks);
2444 return ret;
2445 }
This page took 0.161737 seconds and 4 git commands to generate.