Move clock to bt_trace_descriptor
[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 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * SOFTWARE.
25 */
26
27 #include <stdio.h>
28 #include <unistd.h>
29 #include <string.h>
30 #include <stdlib.h>
31 #include <assert.h>
32 #include <glib.h>
33 #include <inttypes.h>
34 #include <errno.h>
35 #include <babeltrace/babeltrace-internal.h>
36 #include <babeltrace/list.h>
37 #include <babeltrace/types.h>
38 #include <babeltrace/ctf/metadata.h>
39 #include <babeltrace/uuid.h>
40 #include <babeltrace/endian.h>
41 #include <babeltrace/ctf/events-internal.h>
42 #include "ctf-scanner.h"
43 #include "ctf-parser.h"
44 #include "ctf-ast.h"
45
46 #define fprintf_dbg(fd, fmt, args...) fprintf(fd, "%s: " fmt, __func__, ## args)
47
48 #define _bt_list_first_entry(ptr, type, member) \
49 bt_list_entry((ptr)->next, type, member)
50
51 struct last_enum_value {
52 union {
53 int64_t s;
54 uint64_t u;
55 } u;
56 };
57
58 int opt_clock_force_correlate;
59
60 static
61 struct bt_declaration *ctf_type_specifier_list_visit(FILE *fd,
62 int depth, struct ctf_node *type_specifier_list,
63 struct declaration_scope *declaration_scope,
64 struct ctf_trace *trace);
65
66 static
67 int ctf_stream_visit(FILE *fd, int depth, struct ctf_node *node,
68 struct declaration_scope *parent_declaration_scope, struct ctf_trace *trace);
69
70 static
71 int is_unary_string(struct bt_list_head *head)
72 {
73 struct ctf_node *node;
74
75 bt_list_for_each_entry(node, head, siblings) {
76 if (node->type != NODE_UNARY_EXPRESSION)
77 return 0;
78 if (node->u.unary_expression.type != UNARY_STRING)
79 return 0;
80 }
81 return 1;
82 }
83
84 /*
85 * String returned must be freed by the caller using g_free.
86 */
87 static
88 char *concatenate_unary_strings(struct bt_list_head *head)
89 {
90 struct ctf_node *node;
91 GString *str;
92 int i = 0;
93
94 str = g_string_new("");
95 bt_list_for_each_entry(node, head, siblings) {
96 char *src_string;
97
98 if (node->type != NODE_UNARY_EXPRESSION
99 || node->u.unary_expression.type != UNARY_STRING
100 || !((node->u.unary_expression.link != UNARY_LINK_UNKNOWN)
101 ^ (i == 0)))
102 return NULL;
103 switch (node->u.unary_expression.link) {
104 case UNARY_DOTLINK:
105 g_string_append(str, ".");
106 break;
107 case UNARY_ARROWLINK:
108 g_string_append(str, "->");
109 break;
110 case UNARY_DOTDOTDOT:
111 g_string_append(str, "...");
112 break;
113 default:
114 break;
115 }
116 src_string = node->u.unary_expression.u.string;
117 g_string_append(str, src_string);
118 i++;
119 }
120 return g_string_free(str, FALSE);
121 }
122
123 static
124 GQuark get_map_clock_name_value(struct bt_list_head *head)
125 {
126 struct ctf_node *node;
127 const char *name = NULL;
128 int i = 0;
129
130 bt_list_for_each_entry(node, head, siblings) {
131 char *src_string;
132
133 if (node->type != NODE_UNARY_EXPRESSION
134 || node->u.unary_expression.type != UNARY_STRING
135 || !((node->u.unary_expression.link != UNARY_LINK_UNKNOWN)
136 ^ (i == 0)))
137 return 0;
138 /* needs to be chained with . */
139 switch (node->u.unary_expression.link) {
140 case UNARY_DOTLINK:
141 break;
142 case UNARY_ARROWLINK:
143 case UNARY_DOTDOTDOT:
144 return 0;
145 default:
146 break;
147 }
148 src_string = node->u.unary_expression.u.string;
149 switch (i) {
150 case 0: if (strcmp("clock", src_string) != 0) {
151 return 0;
152 }
153 break;
154 case 1: name = src_string;
155 break;
156 case 2: if (strcmp("value", src_string) != 0) {
157 return 0;
158 }
159 break;
160 default:
161 return 0; /* extra identifier, unknown */
162 }
163 i++;
164 }
165 return g_quark_from_string(name);
166 }
167
168 static
169 int is_unary_unsigned(struct bt_list_head *head)
170 {
171 struct ctf_node *node;
172
173 bt_list_for_each_entry(node, head, siblings) {
174 if (node->type != NODE_UNARY_EXPRESSION)
175 return 0;
176 if (node->u.unary_expression.type != UNARY_UNSIGNED_CONSTANT)
177 return 0;
178 }
179 return 1;
180 }
181
182 static
183 int get_unary_unsigned(struct bt_list_head *head, uint64_t *value)
184 {
185 struct ctf_node *node;
186 int i = 0;
187
188 bt_list_for_each_entry(node, head, siblings) {
189 if (node->type != NODE_UNARY_EXPRESSION
190 || node->u.unary_expression.type != UNARY_UNSIGNED_CONSTANT
191 || node->u.unary_expression.link != UNARY_LINK_UNKNOWN
192 || i != 0)
193 return -EINVAL;
194 *value = node->u.unary_expression.u.unsigned_constant;
195 i++;
196 }
197 return 0;
198 }
199
200 static
201 int is_unary_signed(struct bt_list_head *head)
202 {
203 struct ctf_node *node;
204
205 bt_list_for_each_entry(node, head, siblings) {
206 if (node->type != NODE_UNARY_EXPRESSION)
207 return 0;
208 if (node->u.unary_expression.type != UNARY_SIGNED_CONSTANT)
209 return 0;
210 }
211 return 1;
212 }
213
214 static
215 int get_unary_signed(struct bt_list_head *head, int64_t *value)
216 {
217 struct ctf_node *node;
218 int i = 0;
219
220 bt_list_for_each_entry(node, head, siblings) {
221 if (node->type != NODE_UNARY_EXPRESSION
222 || node->u.unary_expression.type != UNARY_UNSIGNED_CONSTANT
223 || (node->u.unary_expression.type != UNARY_UNSIGNED_CONSTANT && node->u.unary_expression.type != UNARY_SIGNED_CONSTANT)
224 || node->u.unary_expression.link != UNARY_LINK_UNKNOWN
225 || i != 0)
226 return -EINVAL;
227 switch (node->u.unary_expression.type) {
228 case UNARY_UNSIGNED_CONSTANT:
229 *value = (int64_t) node->u.unary_expression.u.unsigned_constant;
230 break;
231 case UNARY_SIGNED_CONSTANT:
232 *value = node->u.unary_expression.u.signed_constant;
233 break;
234 default:
235 return -EINVAL;
236 }
237 i++;
238 }
239 return 0;
240 }
241
242 static
243 int get_unary_uuid(struct bt_list_head *head, unsigned char *uuid)
244 {
245 struct ctf_node *node;
246 int i = 0;
247 int ret = -1;
248
249 bt_list_for_each_entry(node, head, siblings) {
250 const char *src_string;
251
252 if (node->type != NODE_UNARY_EXPRESSION
253 || node->u.unary_expression.type != UNARY_STRING
254 || node->u.unary_expression.link != UNARY_LINK_UNKNOWN
255 || i != 0)
256 return -EINVAL;
257 src_string = node->u.unary_expression.u.string;
258 ret = babeltrace_uuid_parse(src_string, uuid);
259 }
260 return ret;
261 }
262
263 static
264 struct ctf_stream_declaration *trace_stream_lookup(struct ctf_trace *trace, uint64_t stream_id)
265 {
266 if (trace->streams->len <= stream_id)
267 return NULL;
268 return g_ptr_array_index(trace->streams, stream_id);
269 }
270
271 static
272 struct ctf_clock *trace_clock_lookup(struct ctf_trace *trace, GQuark clock_name)
273 {
274 return g_hash_table_lookup(trace->parent.clocks, (gpointer) (unsigned long) clock_name);
275 }
276
277 static
278 int visit_type_specifier(FILE *fd, struct ctf_node *type_specifier, GString *str)
279 {
280 if (type_specifier->type != NODE_TYPE_SPECIFIER)
281 return -EINVAL;
282
283 switch (type_specifier->u.type_specifier.type) {
284 case TYPESPEC_VOID:
285 g_string_append(str, "void");
286 break;
287 case TYPESPEC_CHAR:
288 g_string_append(str, "char");
289 break;
290 case TYPESPEC_SHORT:
291 g_string_append(str, "short");
292 break;
293 case TYPESPEC_INT:
294 g_string_append(str, "int");
295 break;
296 case TYPESPEC_LONG:
297 g_string_append(str, "long");
298 break;
299 case TYPESPEC_FLOAT:
300 g_string_append(str, "float");
301 break;
302 case TYPESPEC_DOUBLE:
303 g_string_append(str, "double");
304 break;
305 case TYPESPEC_SIGNED:
306 g_string_append(str, "signed");
307 break;
308 case TYPESPEC_UNSIGNED:
309 g_string_append(str, "unsigned");
310 break;
311 case TYPESPEC_BOOL:
312 g_string_append(str, "bool");
313 break;
314 case TYPESPEC_COMPLEX:
315 g_string_append(str, "_Complex");
316 break;
317 case TYPESPEC_IMAGINARY:
318 g_string_append(str, "_Imaginary");
319 break;
320 case TYPESPEC_CONST:
321 g_string_append(str, "const");
322 break;
323 case TYPESPEC_ID_TYPE:
324 if (type_specifier->u.type_specifier.id_type)
325 g_string_append(str, type_specifier->u.type_specifier.id_type);
326 break;
327 case TYPESPEC_STRUCT:
328 {
329 struct ctf_node *node = type_specifier->u.type_specifier.node;
330
331 if (!node->u._struct.name) {
332 fprintf(fd, "[error] %s: unexpected empty variant name\n", __func__);
333 return -EINVAL;
334 }
335 g_string_append(str, "struct ");
336 g_string_append(str, node->u._struct.name);
337 break;
338 }
339 case TYPESPEC_VARIANT:
340 {
341 struct ctf_node *node = type_specifier->u.type_specifier.node;
342
343 if (!node->u.variant.name) {
344 fprintf(fd, "[error] %s: unexpected empty variant name\n", __func__);
345 return -EINVAL;
346 }
347 g_string_append(str, "variant ");
348 g_string_append(str, node->u.variant.name);
349 break;
350 }
351 case TYPESPEC_ENUM:
352 {
353 struct ctf_node *node = type_specifier->u.type_specifier.node;
354
355 if (!node->u._enum.enum_id) {
356 fprintf(fd, "[error] %s: unexpected empty enum ID\n", __func__);
357 return -EINVAL;
358 }
359 g_string_append(str, "enum ");
360 g_string_append(str, node->u._enum.enum_id);
361 break;
362 }
363 case TYPESPEC_FLOATING_POINT:
364 case TYPESPEC_INTEGER:
365 case TYPESPEC_STRING:
366 default:
367 fprintf(fd, "[error] %s: unknown specifier\n", __func__);
368 return -EINVAL;
369 }
370 return 0;
371 }
372
373 static
374 int visit_type_specifier_list(FILE *fd, struct ctf_node *type_specifier_list, GString *str)
375 {
376 struct ctf_node *iter;
377 int alias_item_nr = 0;
378 int ret;
379
380 bt_list_for_each_entry(iter, &type_specifier_list->u.type_specifier_list.head, siblings) {
381 if (alias_item_nr != 0)
382 g_string_append(str, " ");
383 alias_item_nr++;
384 ret = visit_type_specifier(fd, iter, str);
385 if (ret)
386 return ret;
387 }
388 return 0;
389 }
390
391 static
392 GQuark create_typealias_identifier(FILE *fd, int depth,
393 struct ctf_node *type_specifier_list,
394 struct ctf_node *node_type_declarator)
395 {
396 struct ctf_node *iter;
397 GString *str;
398 char *str_c;
399 GQuark alias_q;
400 int ret;
401
402 str = g_string_new("");
403 ret = visit_type_specifier_list(fd, type_specifier_list, str);
404 if (ret) {
405 g_string_free(str, TRUE);
406 return 0;
407 }
408 bt_list_for_each_entry(iter, &node_type_declarator->u.type_declarator.pointers, siblings) {
409 g_string_append(str, " *");
410 if (iter->u.pointer.const_qualifier)
411 g_string_append(str, " const");
412 }
413 str_c = g_string_free(str, FALSE);
414 alias_q = g_quark_from_string(str_c);
415 g_free(str_c);
416 return alias_q;
417 }
418
419 static
420 struct bt_declaration *ctf_type_declarator_visit(FILE *fd, int depth,
421 struct ctf_node *type_specifier_list,
422 GQuark *field_name,
423 struct ctf_node *node_type_declarator,
424 struct declaration_scope *declaration_scope,
425 struct bt_declaration *nested_declaration,
426 struct ctf_trace *trace)
427 {
428 /*
429 * Visit type declarator by first taking care of sequence/array
430 * (recursively). Then, when we get to the identifier, take care
431 * of pointers.
432 */
433
434 if (node_type_declarator) {
435 if (node_type_declarator->u.type_declarator.type == TYPEDEC_UNKNOWN) {
436 return NULL;
437 }
438
439 /* TODO: gcc bitfields not supported yet. */
440 if (node_type_declarator->u.type_declarator.bitfield_len != NULL) {
441 fprintf(fd, "[error] %s: gcc bitfields are not supported yet.\n", __func__);
442 return NULL;
443 }
444 }
445
446 if (!nested_declaration) {
447 if (node_type_declarator && !bt_list_empty(&node_type_declarator->u.type_declarator.pointers)) {
448 GQuark alias_q;
449
450 /*
451 * If we have a pointer declarator, it _has_ to be present in
452 * the typealiases (else fail).
453 */
454 alias_q = create_typealias_identifier(fd, depth,
455 type_specifier_list, node_type_declarator);
456 nested_declaration = bt_lookup_declaration(alias_q, declaration_scope);
457 if (!nested_declaration) {
458 fprintf(fd, "[error] %s: cannot find typealias \"%s\".\n", __func__, g_quark_to_string(alias_q));
459 return NULL;
460 }
461 if (nested_declaration->id == CTF_TYPE_INTEGER) {
462 struct declaration_integer *integer_declaration =
463 container_of(nested_declaration, struct declaration_integer, p);
464 /* For base to 16 for pointers (expected pretty-print) */
465 if (!integer_declaration->base) {
466 /*
467 * We need to do a copy of the
468 * integer declaration to modify it. There could be other references to
469 * it.
470 */
471 integer_declaration = bt_integer_declaration_new(integer_declaration->len,
472 integer_declaration->byte_order, integer_declaration->signedness,
473 integer_declaration->p.alignment, 16, integer_declaration->encoding,
474 integer_declaration->clock);
475 nested_declaration = &integer_declaration->p;
476 }
477 }
478 } else {
479 nested_declaration = ctf_type_specifier_list_visit(fd, depth,
480 type_specifier_list, declaration_scope, trace);
481 }
482 }
483
484 if (!node_type_declarator)
485 return nested_declaration;
486
487 if (node_type_declarator->u.type_declarator.type == TYPEDEC_ID) {
488 if (node_type_declarator->u.type_declarator.u.id)
489 *field_name = g_quark_from_string(node_type_declarator->u.type_declarator.u.id);
490 else
491 *field_name = 0;
492 return nested_declaration;
493 } else {
494 struct bt_declaration *declaration;
495 struct ctf_node *first;
496
497 /* TYPEDEC_NESTED */
498
499 if (!nested_declaration) {
500 fprintf(fd, "[error] %s: nested type is unknown.\n", __func__);
501 return NULL;
502 }
503
504 /* create array/sequence, pass nested_declaration as child. */
505 if (bt_list_empty(&node_type_declarator->u.type_declarator.u.nested.length)) {
506 fprintf(fd, "[error] %s: expecting length field reference or value.\n", __func__);
507 return NULL;
508 }
509 first = _bt_list_first_entry(&node_type_declarator->u.type_declarator.u.nested.length,
510 struct ctf_node, siblings);
511 if (first->type != NODE_UNARY_EXPRESSION) {
512 return NULL;
513 }
514
515 switch (first->u.unary_expression.type) {
516 case UNARY_UNSIGNED_CONSTANT:
517 {
518 struct declaration_array *array_declaration;
519 size_t len;
520
521 len = first->u.unary_expression.u.unsigned_constant;
522 array_declaration = bt_array_declaration_new(len, nested_declaration,
523 declaration_scope);
524
525 if (!array_declaration) {
526 fprintf(fd, "[error] %s: cannot create array declaration.\n", __func__);
527 return NULL;
528 }
529 bt_declaration_unref(nested_declaration);
530 declaration = &array_declaration->p;
531 break;
532 }
533 case UNARY_STRING:
534 {
535 /* Lookup unsigned integer definition, create sequence */
536 char *length_name = concatenate_unary_strings(&node_type_declarator->u.type_declarator.u.nested.length);
537 struct declaration_sequence *sequence_declaration;
538
539 if (!length_name)
540 return NULL;
541 sequence_declaration = bt_sequence_declaration_new(length_name, nested_declaration, declaration_scope);
542 if (!sequence_declaration) {
543 fprintf(fd, "[error] %s: cannot create sequence declaration.\n", __func__);
544 g_free(length_name);
545 return NULL;
546 }
547 bt_declaration_unref(nested_declaration);
548 declaration = &sequence_declaration->p;
549 g_free(length_name);
550 break;
551 }
552 default:
553 return NULL;
554 }
555
556 /* Pass it as content of outer container */
557 declaration = ctf_type_declarator_visit(fd, depth,
558 type_specifier_list, field_name,
559 node_type_declarator->u.type_declarator.u.nested.type_declarator,
560 declaration_scope, declaration, trace);
561 return declaration;
562 }
563 }
564
565 static
566 int ctf_struct_type_declarators_visit(FILE *fd, int depth,
567 struct declaration_struct *struct_declaration,
568 struct ctf_node *type_specifier_list,
569 struct bt_list_head *type_declarators,
570 struct declaration_scope *declaration_scope,
571 struct ctf_trace *trace)
572 {
573 struct ctf_node *iter;
574 GQuark field_name;
575
576 bt_list_for_each_entry(iter, type_declarators, siblings) {
577 struct bt_declaration *field_declaration;
578
579 field_declaration = ctf_type_declarator_visit(fd, depth,
580 type_specifier_list,
581 &field_name, iter,
582 struct_declaration->scope,
583 NULL, trace);
584 if (!field_declaration) {
585 fprintf(fd, "[error] %s: unable to find struct field declaration type\n", __func__);
586 return -EINVAL;
587 }
588
589 /* Check if field with same name already exists */
590 if (bt_struct_declaration_lookup_field_index(struct_declaration, field_name) >= 0) {
591 fprintf(fd, "[error] %s: duplicate field %s in struct\n", __func__, g_quark_to_string(field_name));
592 return -EINVAL;
593 }
594
595 bt_struct_declaration_add_field(struct_declaration,
596 g_quark_to_string(field_name),
597 field_declaration);
598 bt_declaration_unref(field_declaration);
599 }
600 return 0;
601 }
602
603 static
604 int ctf_variant_type_declarators_visit(FILE *fd, int depth,
605 struct declaration_untagged_variant *untagged_variant_declaration,
606 struct ctf_node *type_specifier_list,
607 struct bt_list_head *type_declarators,
608 struct declaration_scope *declaration_scope,
609 struct ctf_trace *trace)
610 {
611 struct ctf_node *iter;
612 GQuark field_name;
613
614 bt_list_for_each_entry(iter, type_declarators, siblings) {
615 struct bt_declaration *field_declaration;
616
617 field_declaration = ctf_type_declarator_visit(fd, depth,
618 type_specifier_list,
619 &field_name, iter,
620 untagged_variant_declaration->scope,
621 NULL, trace);
622 if (!field_declaration) {
623 fprintf(fd, "[error] %s: unable to find variant field declaration type\n", __func__);
624 return -EINVAL;
625 }
626
627 if (bt_untagged_variant_declaration_get_field_from_tag(untagged_variant_declaration, field_name) != NULL) {
628 fprintf(fd, "[error] %s: duplicate field %s in variant\n", __func__, g_quark_to_string(field_name));
629 return -EINVAL;
630 }
631
632 bt_untagged_variant_declaration_add_field(untagged_variant_declaration,
633 g_quark_to_string(field_name),
634 field_declaration);
635 bt_declaration_unref(field_declaration);
636 }
637 return 0;
638 }
639
640 static
641 int ctf_typedef_visit(FILE *fd, int depth, struct declaration_scope *scope,
642 struct ctf_node *type_specifier_list,
643 struct bt_list_head *type_declarators,
644 struct ctf_trace *trace)
645 {
646 struct ctf_node *iter;
647 GQuark identifier;
648
649 bt_list_for_each_entry(iter, type_declarators, siblings) {
650 struct bt_declaration *type_declaration;
651 int ret;
652
653 type_declaration = ctf_type_declarator_visit(fd, depth,
654 type_specifier_list,
655 &identifier, iter,
656 scope, NULL, trace);
657 if (!type_declaration) {
658 fprintf(fd, "[error] %s: problem creating type declaration\n", __func__);
659 return -EINVAL;
660 }
661 /*
662 * Don't allow typedef and typealias of untagged
663 * variants.
664 */
665 if (type_declaration->id == CTF_TYPE_UNTAGGED_VARIANT) {
666 fprintf(fd, "[error] %s: typedef of untagged variant is not permitted.\n", __func__);
667 bt_declaration_unref(type_declaration);
668 return -EPERM;
669 }
670 ret = bt_register_declaration(identifier, type_declaration, scope);
671 if (ret) {
672 type_declaration->declaration_free(type_declaration);
673 return ret;
674 }
675 bt_declaration_unref(type_declaration);
676 }
677 return 0;
678 }
679
680 static
681 int ctf_typealias_visit(FILE *fd, int depth, struct declaration_scope *scope,
682 struct ctf_node *target, struct ctf_node *alias,
683 struct ctf_trace *trace)
684 {
685 struct bt_declaration *type_declaration;
686 struct ctf_node *node;
687 GQuark dummy_id;
688 GQuark alias_q;
689 int err;
690
691 /* See ctf_visitor_type_declarator() in the semantic validator. */
692
693 /*
694 * Create target type declaration.
695 */
696
697 if (bt_list_empty(&target->u.typealias_target.type_declarators))
698 node = NULL;
699 else
700 node = _bt_list_first_entry(&target->u.typealias_target.type_declarators,
701 struct ctf_node, siblings);
702 type_declaration = ctf_type_declarator_visit(fd, depth,
703 target->u.typealias_target.type_specifier_list,
704 &dummy_id, node,
705 scope, NULL, trace);
706 if (!type_declaration) {
707 fprintf(fd, "[error] %s: problem creating type declaration\n", __func__);
708 err = -EINVAL;
709 goto error;
710 }
711 /*
712 * Don't allow typedef and typealias of untagged
713 * variants.
714 */
715 if (type_declaration->id == CTF_TYPE_UNTAGGED_VARIANT) {
716 fprintf(fd, "[error] %s: typedef of untagged variant is not permitted.\n", __func__);
717 bt_declaration_unref(type_declaration);
718 return -EPERM;
719 }
720 /*
721 * The semantic validator does not check whether the target is
722 * abstract or not (if it has an identifier). Check it here.
723 */
724 if (dummy_id != 0) {
725 fprintf(fd, "[error] %s: expecting empty identifier\n", __func__);
726 err = -EINVAL;
727 goto error;
728 }
729 /*
730 * Create alias identifier.
731 */
732
733 node = _bt_list_first_entry(&alias->u.typealias_alias.type_declarators,
734 struct ctf_node, siblings);
735 alias_q = create_typealias_identifier(fd, depth,
736 alias->u.typealias_alias.type_specifier_list, node);
737 err = bt_register_declaration(alias_q, type_declaration, scope);
738 if (err)
739 goto error;
740 bt_declaration_unref(type_declaration);
741 return 0;
742
743 error:
744 if (type_declaration) {
745 type_declaration->declaration_free(type_declaration);
746 }
747 return err;
748 }
749
750 static
751 int ctf_struct_declaration_list_visit(FILE *fd, int depth,
752 struct ctf_node *iter, struct declaration_struct *struct_declaration,
753 struct ctf_trace *trace)
754 {
755 int ret;
756
757 switch (iter->type) {
758 case NODE_TYPEDEF:
759 /* For each declarator, declare type and add type to struct bt_declaration scope */
760 ret = ctf_typedef_visit(fd, depth,
761 struct_declaration->scope,
762 iter->u._typedef.type_specifier_list,
763 &iter->u._typedef.type_declarators, trace);
764 if (ret)
765 return ret;
766 break;
767 case NODE_TYPEALIAS:
768 /* Declare type with declarator and add type to struct bt_declaration scope */
769 ret = ctf_typealias_visit(fd, depth,
770 struct_declaration->scope,
771 iter->u.typealias.target,
772 iter->u.typealias.alias, trace);
773 if (ret)
774 return ret;
775 break;
776 case NODE_STRUCT_OR_VARIANT_DECLARATION:
777 /* Add field to structure declaration */
778 ret = ctf_struct_type_declarators_visit(fd, depth,
779 struct_declaration,
780 iter->u.struct_or_variant_declaration.type_specifier_list,
781 &iter->u.struct_or_variant_declaration.type_declarators,
782 struct_declaration->scope, trace);
783 if (ret)
784 return ret;
785 break;
786 default:
787 fprintf(fd, "[error] %s: unexpected node type %d\n", __func__, (int) iter->type);
788 return -EINVAL;
789 }
790 return 0;
791 }
792
793 static
794 int ctf_variant_declaration_list_visit(FILE *fd, int depth,
795 struct ctf_node *iter,
796 struct declaration_untagged_variant *untagged_variant_declaration,
797 struct ctf_trace *trace)
798 {
799 int ret;
800
801 switch (iter->type) {
802 case NODE_TYPEDEF:
803 /* For each declarator, declare type and add type to variant declaration scope */
804 ret = ctf_typedef_visit(fd, depth,
805 untagged_variant_declaration->scope,
806 iter->u._typedef.type_specifier_list,
807 &iter->u._typedef.type_declarators, trace);
808 if (ret)
809 return ret;
810 break;
811 case NODE_TYPEALIAS:
812 /* Declare type with declarator and add type to variant declaration scope */
813 ret = ctf_typealias_visit(fd, depth,
814 untagged_variant_declaration->scope,
815 iter->u.typealias.target,
816 iter->u.typealias.alias, trace);
817 if (ret)
818 return ret;
819 break;
820 case NODE_STRUCT_OR_VARIANT_DECLARATION:
821 /* Add field to structure declaration */
822 ret = ctf_variant_type_declarators_visit(fd, depth,
823 untagged_variant_declaration,
824 iter->u.struct_or_variant_declaration.type_specifier_list,
825 &iter->u.struct_or_variant_declaration.type_declarators,
826 untagged_variant_declaration->scope, trace);
827 if (ret)
828 return ret;
829 break;
830 default:
831 fprintf(fd, "[error] %s: unexpected node type %d\n", __func__, (int) iter->type);
832 return -EINVAL;
833 }
834 return 0;
835 }
836
837 static
838 struct bt_declaration *ctf_declaration_struct_visit(FILE *fd,
839 int depth, const char *name, struct bt_list_head *declaration_list,
840 int has_body, struct bt_list_head *min_align,
841 struct declaration_scope *declaration_scope,
842 struct ctf_trace *trace)
843 {
844 struct declaration_struct *struct_declaration;
845 struct ctf_node *iter;
846
847 /*
848 * For named struct (without body), lookup in
849 * declaration scope. Don't take reference on struct
850 * declaration: ref is only taken upon definition.
851 */
852 if (!has_body) {
853 if (!name)
854 return NULL;
855 struct_declaration =
856 bt_lookup_struct_declaration(g_quark_from_string(name),
857 declaration_scope);
858 bt_declaration_ref(&struct_declaration->p);
859 return &struct_declaration->p;
860 } else {
861 uint64_t min_align_value = 0;
862
863 /* For unnamed struct, create type */
864 /* For named struct (with body), create type and add to declaration scope */
865 if (name) {
866 if (bt_lookup_struct_declaration(g_quark_from_string(name),
867 declaration_scope)) {
868
869 fprintf(fd, "[error] %s: struct %s already declared in scope\n", __func__, name);
870 return NULL;
871 }
872 }
873 if (!bt_list_empty(min_align)) {
874 int ret;
875
876 ret = get_unary_unsigned(min_align, &min_align_value);
877 if (ret) {
878 fprintf(fd, "[error] %s: unexpected unary expression for structure \"align\" attribute\n", __func__);
879 ret = -EINVAL;
880 goto error;
881 }
882 }
883 struct_declaration = bt_struct_declaration_new(declaration_scope,
884 min_align_value);
885 bt_list_for_each_entry(iter, declaration_list, siblings) {
886 int ret;
887
888 ret = ctf_struct_declaration_list_visit(fd, depth + 1, iter,
889 struct_declaration, trace);
890 if (ret)
891 goto error_free_declaration;
892 }
893 if (name) {
894 int ret;
895
896 ret = bt_register_struct_declaration(g_quark_from_string(name),
897 struct_declaration,
898 declaration_scope);
899 if (ret)
900 return NULL;
901 }
902 return &struct_declaration->p;
903 }
904 error_free_declaration:
905 struct_declaration->p.declaration_free(&struct_declaration->p);
906 error:
907 return NULL;
908 }
909
910 static
911 struct bt_declaration *ctf_declaration_variant_visit(FILE *fd,
912 int depth, const char *name, const char *choice,
913 struct bt_list_head *declaration_list,
914 int has_body, struct declaration_scope *declaration_scope,
915 struct ctf_trace *trace)
916 {
917 struct declaration_untagged_variant *untagged_variant_declaration;
918 struct declaration_variant *variant_declaration;
919 struct ctf_node *iter;
920
921 /*
922 * For named variant (without body), lookup in
923 * declaration scope. Don't take reference on variant
924 * declaration: ref is only taken upon definition.
925 */
926 if (!has_body) {
927 if (!name)
928 return NULL;
929 untagged_variant_declaration =
930 bt_lookup_variant_declaration(g_quark_from_string(name),
931 declaration_scope);
932 bt_declaration_ref(&untagged_variant_declaration->p);
933 } else {
934 /* For unnamed variant, create type */
935 /* For named variant (with body), create type and add to declaration scope */
936 if (name) {
937 if (bt_lookup_variant_declaration(g_quark_from_string(name),
938 declaration_scope)) {
939
940 fprintf(fd, "[error] %s: variant %s already declared in scope\n", __func__, name);
941 return NULL;
942 }
943 }
944 untagged_variant_declaration = bt_untagged_bt_variant_declaration_new(declaration_scope);
945 bt_list_for_each_entry(iter, declaration_list, siblings) {
946 int ret;
947
948 ret = ctf_variant_declaration_list_visit(fd, depth + 1, iter,
949 untagged_variant_declaration, trace);
950 if (ret)
951 goto error;
952 }
953 if (name) {
954 int ret;
955
956 ret = bt_register_variant_declaration(g_quark_from_string(name),
957 untagged_variant_declaration,
958 declaration_scope);
959 if (ret)
960 return NULL;
961 }
962 }
963 /*
964 * if tagged, create tagged variant and return. else return
965 * untagged variant.
966 */
967 if (!choice) {
968 return &untagged_variant_declaration->p;
969 } else {
970 variant_declaration = bt_variant_declaration_new(untagged_variant_declaration, choice);
971 if (!variant_declaration)
972 goto error;
973 bt_declaration_unref(&untagged_variant_declaration->p);
974 return &variant_declaration->p;
975 }
976 error:
977 untagged_variant_declaration->p.declaration_free(&untagged_variant_declaration->p);
978 return NULL;
979 }
980
981 static
982 int ctf_enumerator_list_visit(FILE *fd, int depth,
983 struct ctf_node *enumerator,
984 struct declaration_enum *enum_declaration,
985 struct last_enum_value *last)
986 {
987 GQuark q;
988 struct ctf_node *iter;
989
990 q = g_quark_from_string(enumerator->u.enumerator.id);
991 if (enum_declaration->integer_declaration->signedness) {
992 int64_t start, end;
993 int nr_vals = 0;
994
995 bt_list_for_each_entry(iter, &enumerator->u.enumerator.values, siblings) {
996 int64_t *target;
997
998 if (iter->type != NODE_UNARY_EXPRESSION)
999 return -EINVAL;
1000 if (nr_vals == 0)
1001 target = &start;
1002 else
1003 target = &end;
1004
1005 switch (iter->u.unary_expression.type) {
1006 case UNARY_SIGNED_CONSTANT:
1007 *target = iter->u.unary_expression.u.signed_constant;
1008 break;
1009 case UNARY_UNSIGNED_CONSTANT:
1010 *target = iter->u.unary_expression.u.unsigned_constant;
1011 break;
1012 default:
1013 fprintf(fd, "[error] %s: invalid enumerator\n", __func__);
1014 return -EINVAL;
1015 }
1016 if (nr_vals > 1) {
1017 fprintf(fd, "[error] %s: invalid enumerator\n", __func__);
1018 return -EINVAL;
1019 }
1020 nr_vals++;
1021 }
1022 if (nr_vals == 0)
1023 start = last->u.s;
1024 if (nr_vals <= 1)
1025 end = start;
1026 last->u.s = end + 1;
1027 bt_enum_signed_insert(enum_declaration, start, end, q);
1028 } else {
1029 uint64_t start, end;
1030 int nr_vals = 0;
1031
1032 bt_list_for_each_entry(iter, &enumerator->u.enumerator.values, siblings) {
1033 uint64_t *target;
1034
1035 if (iter->type != NODE_UNARY_EXPRESSION)
1036 return -EINVAL;
1037 if (nr_vals == 0)
1038 target = &start;
1039 else
1040 target = &end;
1041
1042 switch (iter->u.unary_expression.type) {
1043 case UNARY_UNSIGNED_CONSTANT:
1044 *target = iter->u.unary_expression.u.unsigned_constant;
1045 break;
1046 case UNARY_SIGNED_CONSTANT:
1047 /*
1048 * We don't accept signed constants for enums with unsigned
1049 * container type.
1050 */
1051 fprintf(fd, "[error] %s: invalid enumerator (signed constant encountered, but enum container type is unsigned)\n", __func__);
1052 return -EINVAL;
1053 default:
1054 fprintf(fd, "[error] %s: invalid enumerator\n", __func__);
1055 return -EINVAL;
1056 }
1057 if (nr_vals > 1) {
1058 fprintf(fd, "[error] %s: invalid enumerator\n", __func__);
1059 return -EINVAL;
1060 }
1061 nr_vals++;
1062 }
1063 if (nr_vals == 0)
1064 start = last->u.u;
1065 if (nr_vals <= 1)
1066 end = start;
1067 last->u.u = end + 1;
1068 bt_enum_unsigned_insert(enum_declaration, start, end, q);
1069 }
1070 return 0;
1071 }
1072
1073 static
1074 struct bt_declaration *ctf_declaration_enum_visit(FILE *fd, int depth,
1075 const char *name,
1076 struct ctf_node *container_type,
1077 struct bt_list_head *enumerator_list,
1078 int has_body,
1079 struct declaration_scope *declaration_scope,
1080 struct ctf_trace *trace)
1081 {
1082 struct bt_declaration *declaration;
1083 struct declaration_enum *enum_declaration;
1084 struct declaration_integer *integer_declaration;
1085 struct last_enum_value last_value;
1086 struct ctf_node *iter;
1087 GQuark dummy_id;
1088
1089 /*
1090 * For named enum (without body), lookup in
1091 * declaration scope. Don't take reference on enum
1092 * declaration: ref is only taken upon definition.
1093 */
1094 if (!has_body) {
1095 if (!name)
1096 return NULL;
1097 enum_declaration =
1098 bt_lookup_enum_declaration(g_quark_from_string(name),
1099 declaration_scope);
1100 bt_declaration_ref(&enum_declaration->p);
1101 return &enum_declaration->p;
1102 } else {
1103 /* For unnamed enum, create type */
1104 /* For named enum (with body), create type and add to declaration scope */
1105 if (name) {
1106 if (bt_lookup_enum_declaration(g_quark_from_string(name),
1107 declaration_scope)) {
1108
1109 fprintf(fd, "[error] %s: enum %s already declared in scope\n", __func__, name);
1110 return NULL;
1111 }
1112 }
1113 if (!container_type) {
1114 declaration = bt_lookup_declaration(g_quark_from_static_string("int"),
1115 declaration_scope);
1116 if (!declaration) {
1117 fprintf(fd, "[error] %s: \"int\" type declaration missing for enumeration\n", __func__);
1118 return NULL;
1119 }
1120 } else {
1121 declaration = ctf_type_declarator_visit(fd, depth,
1122 container_type,
1123 &dummy_id, NULL,
1124 declaration_scope,
1125 NULL, trace);
1126 }
1127 if (!declaration) {
1128 fprintf(fd, "[error] %s: unable to create container type for enumeration\n", __func__);
1129 return NULL;
1130 }
1131 if (declaration->id != CTF_TYPE_INTEGER) {
1132 fprintf(fd, "[error] %s: container type for enumeration is not integer\n", __func__);
1133 return NULL;
1134 }
1135 integer_declaration = container_of(declaration, struct declaration_integer, p);
1136 enum_declaration = bt_enum_declaration_new(integer_declaration);
1137 bt_declaration_unref(&integer_declaration->p); /* leave ref to enum */
1138 if (enum_declaration->integer_declaration->signedness) {
1139 last_value.u.s = 0;
1140 } else {
1141 last_value.u.u = 0;
1142 }
1143 bt_list_for_each_entry(iter, enumerator_list, siblings) {
1144 int ret;
1145
1146 ret = ctf_enumerator_list_visit(fd, depth + 1, iter, enum_declaration,
1147 &last_value);
1148 if (ret)
1149 goto error;
1150 }
1151 if (name) {
1152 int ret;
1153
1154 ret = bt_register_enum_declaration(g_quark_from_string(name),
1155 enum_declaration,
1156 declaration_scope);
1157 if (ret)
1158 return NULL;
1159 bt_declaration_unref(&enum_declaration->p);
1160 }
1161 return &enum_declaration->p;
1162 }
1163 error:
1164 enum_declaration->p.declaration_free(&enum_declaration->p);
1165 return NULL;
1166 }
1167
1168 static
1169 struct bt_declaration *ctf_declaration_type_specifier_visit(FILE *fd, int depth,
1170 struct ctf_node *type_specifier_list,
1171 struct declaration_scope *declaration_scope)
1172 {
1173 GString *str;
1174 struct bt_declaration *declaration;
1175 char *str_c;
1176 int ret;
1177 GQuark id_q;
1178
1179 str = g_string_new("");
1180 ret = visit_type_specifier_list(fd, type_specifier_list, str);
1181 if (ret) {
1182 (void) g_string_free(str, TRUE);
1183 return NULL;
1184 }
1185 str_c = g_string_free(str, FALSE);
1186 id_q = g_quark_from_string(str_c);
1187 g_free(str_c);
1188 declaration = bt_lookup_declaration(id_q, declaration_scope);
1189 if (!declaration)
1190 return NULL;
1191 bt_declaration_ref(declaration);
1192 return declaration;
1193 }
1194
1195 /*
1196 * Returns 0/1 boolean, or < 0 on error.
1197 */
1198 static
1199 int get_boolean(FILE *fd, int depth, struct ctf_node *unary_expression)
1200 {
1201 if (unary_expression->type != NODE_UNARY_EXPRESSION) {
1202 fprintf(fd, "[error] %s: expecting unary expression\n",
1203 __func__);
1204 return -EINVAL;
1205 }
1206 switch (unary_expression->u.unary_expression.type) {
1207 case UNARY_UNSIGNED_CONSTANT:
1208 if (unary_expression->u.unary_expression.u.unsigned_constant == 0)
1209 return 0;
1210 else
1211 return 1;
1212 case UNARY_SIGNED_CONSTANT:
1213 if (unary_expression->u.unary_expression.u.signed_constant == 0)
1214 return 0;
1215 else
1216 return 1;
1217 case UNARY_STRING:
1218 if (!strcmp(unary_expression->u.unary_expression.u.string, "true"))
1219 return 1;
1220 else if (!strcmp(unary_expression->u.unary_expression.u.string, "TRUE"))
1221 return 1;
1222 else if (!strcmp(unary_expression->u.unary_expression.u.string, "false"))
1223 return 0;
1224 else if (!strcmp(unary_expression->u.unary_expression.u.string, "FALSE"))
1225 return 0;
1226 else {
1227 fprintf(fd, "[error] %s: unexpected string \"%s\"\n",
1228 __func__, unary_expression->u.unary_expression.u.string);
1229 return -EINVAL;
1230 }
1231 break;
1232 default:
1233 fprintf(fd, "[error] %s: unexpected unary expression type\n",
1234 __func__);
1235 return -EINVAL;
1236 }
1237
1238 }
1239
1240 static
1241 int get_trace_byte_order(FILE *fd, int depth, struct ctf_node *unary_expression)
1242 {
1243 int byte_order;
1244
1245 if (unary_expression->u.unary_expression.type != UNARY_STRING) {
1246 fprintf(fd, "[error] %s: byte_order: expecting string\n",
1247 __func__);
1248 return -EINVAL;
1249 }
1250 if (!strcmp(unary_expression->u.unary_expression.u.string, "be"))
1251 byte_order = BIG_ENDIAN;
1252 else if (!strcmp(unary_expression->u.unary_expression.u.string, "le"))
1253 byte_order = LITTLE_ENDIAN;
1254 else {
1255 fprintf(fd, "[error] %s: unexpected string \"%s\". Should be \"be\" or \"le\".\n",
1256 __func__, unary_expression->u.unary_expression.u.string);
1257 return -EINVAL;
1258 }
1259 return byte_order;
1260 }
1261
1262 static
1263 int get_byte_order(FILE *fd, int depth, struct ctf_node *unary_expression,
1264 struct ctf_trace *trace)
1265 {
1266 int byte_order;
1267
1268 if (unary_expression->u.unary_expression.type != UNARY_STRING) {
1269 fprintf(fd, "[error] %s: byte_order: expecting string\n",
1270 __func__);
1271 return -EINVAL;
1272 }
1273 if (!strcmp(unary_expression->u.unary_expression.u.string, "native"))
1274 byte_order = trace->byte_order;
1275 else if (!strcmp(unary_expression->u.unary_expression.u.string, "network"))
1276 byte_order = BIG_ENDIAN;
1277 else if (!strcmp(unary_expression->u.unary_expression.u.string, "be"))
1278 byte_order = BIG_ENDIAN;
1279 else if (!strcmp(unary_expression->u.unary_expression.u.string, "le"))
1280 byte_order = LITTLE_ENDIAN;
1281 else {
1282 fprintf(fd, "[error] %s: unexpected string \"%s\". Should be \"native\", \"network\", \"be\" or \"le\".\n",
1283 __func__, unary_expression->u.unary_expression.u.string);
1284 return -EINVAL;
1285 }
1286 return byte_order;
1287 }
1288
1289 static
1290 struct bt_declaration *ctf_declaration_integer_visit(FILE *fd, int depth,
1291 struct bt_list_head *expressions,
1292 struct ctf_trace *trace)
1293 {
1294 struct ctf_node *expression;
1295 uint64_t alignment = 1, size = 0;
1296 int byte_order = trace->byte_order;
1297 int signedness = 0;
1298 int has_alignment = 0, has_size = 0;
1299 int base = 0;
1300 enum ctf_string_encoding encoding = CTF_STRING_NONE;
1301 struct ctf_clock *clock = NULL;
1302 struct declaration_integer *integer_declaration;
1303
1304 bt_list_for_each_entry(expression, expressions, siblings) {
1305 struct ctf_node *left, *right;
1306
1307 left = _bt_list_first_entry(&expression->u.ctf_expression.left, struct ctf_node, siblings);
1308 right = _bt_list_first_entry(&expression->u.ctf_expression.right, struct ctf_node, siblings);
1309 if (left->u.unary_expression.type != UNARY_STRING)
1310 return NULL;
1311 if (!strcmp(left->u.unary_expression.u.string, "signed")) {
1312 signedness = get_boolean(fd, depth, right);
1313 if (signedness < 0)
1314 return NULL;
1315 } else if (!strcmp(left->u.unary_expression.u.string, "byte_order")) {
1316 byte_order = get_byte_order(fd, depth, right, trace);
1317 if (byte_order < 0)
1318 return NULL;
1319 } else if (!strcmp(left->u.unary_expression.u.string, "size")) {
1320 if (right->u.unary_expression.type != UNARY_UNSIGNED_CONSTANT) {
1321 fprintf(fd, "[error] %s: size: expecting unsigned constant\n",
1322 __func__);
1323 return NULL;
1324 }
1325 size = right->u.unary_expression.u.unsigned_constant;
1326 has_size = 1;
1327 } else if (!strcmp(left->u.unary_expression.u.string, "align")) {
1328 if (right->u.unary_expression.type != UNARY_UNSIGNED_CONSTANT) {
1329 fprintf(fd, "[error] %s: align: expecting unsigned constant\n",
1330 __func__);
1331 return NULL;
1332 }
1333 alignment = right->u.unary_expression.u.unsigned_constant;
1334 /* Make sure alignment is a power of two */
1335 if (alignment == 0 || (alignment & (alignment - 1)) != 0) {
1336 fprintf(fd, "[error] %s: align: expecting power of two\n",
1337 __func__);
1338 return NULL;
1339 }
1340 has_alignment = 1;
1341 } else if (!strcmp(left->u.unary_expression.u.string, "base")) {
1342 switch (right->u.unary_expression.type) {
1343 case UNARY_UNSIGNED_CONSTANT:
1344 switch (right->u.unary_expression.u.unsigned_constant) {
1345 case 2:
1346 case 8:
1347 case 10:
1348 case 16:
1349 base = right->u.unary_expression.u.unsigned_constant;
1350 break;
1351 default:
1352 fprintf(fd, "[error] %s: base not supported (%" PRIu64 ")\n",
1353 __func__, right->u.unary_expression.u.unsigned_constant);
1354 return NULL;
1355 }
1356 break;
1357 case UNARY_STRING:
1358 {
1359 char *s_right = concatenate_unary_strings(&expression->u.ctf_expression.right);
1360 if (!s_right) {
1361 fprintf(fd, "[error] %s: unexpected unary expression for integer base\n", __func__);
1362 g_free(s_right);
1363 return NULL;
1364 }
1365 if (!strcmp(s_right, "decimal") || !strcmp(s_right, "dec") || !strcmp(s_right, "d")
1366 || !strcmp(s_right, "i") || !strcmp(s_right, "u")) {
1367 base = 10;
1368 } else if (!strcmp(s_right, "hexadecimal") || !strcmp(s_right, "hex")
1369 || !strcmp(s_right, "x") || !strcmp(s_right, "X")
1370 || !strcmp(s_right, "p")) {
1371 base = 16;
1372 } else if (!strcmp(s_right, "octal") || !strcmp(s_right, "oct")
1373 || !strcmp(s_right, "o")) {
1374 base = 8;
1375 } else if (!strcmp(s_right, "binary") || !strcmp(s_right, "b")) {
1376 base = 2;
1377 } else {
1378 fprintf(fd, "[error] %s: unexpected expression for integer base (%s)\n", __func__, s_right);
1379 g_free(s_right);
1380 return NULL;
1381 }
1382
1383 g_free(s_right);
1384 break;
1385 }
1386 default:
1387 fprintf(fd, "[error] %s: base: expecting unsigned constant or unary string\n",
1388 __func__);
1389 return NULL;
1390 }
1391 } else if (!strcmp(left->u.unary_expression.u.string, "encoding")) {
1392 char *s_right;
1393
1394 if (right->u.unary_expression.type != UNARY_STRING) {
1395 fprintf(fd, "[error] %s: encoding: expecting unary string\n",
1396 __func__);
1397 return NULL;
1398 }
1399 s_right = concatenate_unary_strings(&expression->u.ctf_expression.right);
1400 if (!s_right) {
1401 fprintf(fd, "[error] %s: unexpected unary expression for integer base\n", __func__);
1402 g_free(s_right);
1403 return NULL;
1404 }
1405 if (!strcmp(s_right, "UTF8")
1406 || !strcmp(s_right, "utf8")
1407 || !strcmp(s_right, "utf-8")
1408 || !strcmp(s_right, "UTF-8"))
1409 encoding = CTF_STRING_UTF8;
1410 else if (!strcmp(s_right, "ASCII")
1411 || !strcmp(s_right, "ascii"))
1412 encoding = CTF_STRING_ASCII;
1413 else if (!strcmp(s_right, "none"))
1414 encoding = CTF_STRING_NONE;
1415 else {
1416 fprintf(fd, "[error] %s: unknown string encoding \"%s\"\n", __func__, s_right);
1417 g_free(s_right);
1418 return NULL;
1419 }
1420 g_free(s_right);
1421 } else if (!strcmp(left->u.unary_expression.u.string, "map")) {
1422 GQuark clock_name;
1423
1424 if (right->u.unary_expression.type != UNARY_STRING) {
1425 fprintf(fd, "[error] %s: map: expecting identifier\n",
1426 __func__);
1427 return NULL;
1428 }
1429 /* currently only support clock.name.value */
1430 clock_name = get_map_clock_name_value(&expression->u.ctf_expression.right);
1431 if (!clock_name) {
1432 char *s_right;
1433
1434 s_right = concatenate_unary_strings(&expression->u.ctf_expression.right);
1435 if (!s_right) {
1436 fprintf(fd, "[error] %s: unexpected unary expression for integer map\n", __func__);
1437 g_free(s_right);
1438 return NULL;
1439 }
1440 fprintf(fd, "[warning] %s: unknown map %s in integer declaration\n", __func__,
1441 s_right);
1442 g_free(s_right);
1443 continue;
1444 }
1445 clock = trace_clock_lookup(trace, clock_name);
1446 if (!clock) {
1447 fprintf(fd, "[error] %s: map: unable to find clock %s declaration\n",
1448 __func__, g_quark_to_string(clock_name));
1449 return NULL;
1450 }
1451 } else {
1452 fprintf(fd, "[warning] %s: unknown attribute name %s\n",
1453 __func__, left->u.unary_expression.u.string);
1454 /* Fall-through after warning */
1455 }
1456 }
1457 if (!has_size) {
1458 fprintf(fd, "[error] %s: missing size attribute\n", __func__);
1459 return NULL;
1460 }
1461 if (!has_alignment) {
1462 if (size % CHAR_BIT) {
1463 /* bit-packed alignment */
1464 alignment = 1;
1465 } else {
1466 /* byte-packed alignment */
1467 alignment = CHAR_BIT;
1468 }
1469 }
1470 integer_declaration = bt_integer_declaration_new(size,
1471 byte_order, signedness, alignment,
1472 base, encoding, clock);
1473 return &integer_declaration->p;
1474 }
1475
1476 static
1477 struct bt_declaration *ctf_declaration_floating_point_visit(FILE *fd, int depth,
1478 struct bt_list_head *expressions,
1479 struct ctf_trace *trace)
1480 {
1481 struct ctf_node *expression;
1482 uint64_t alignment = 1, exp_dig = 0, mant_dig = 0,
1483 byte_order = trace->byte_order;
1484 int has_alignment = 0, has_exp_dig = 0, has_mant_dig = 0;
1485 struct declaration_float *float_declaration;
1486
1487 bt_list_for_each_entry(expression, expressions, siblings) {
1488 struct ctf_node *left, *right;
1489
1490 left = _bt_list_first_entry(&expression->u.ctf_expression.left, struct ctf_node, siblings);
1491 right = _bt_list_first_entry(&expression->u.ctf_expression.right, struct ctf_node, siblings);
1492 if (left->u.unary_expression.type != UNARY_STRING)
1493 return NULL;
1494 if (!strcmp(left->u.unary_expression.u.string, "byte_order")) {
1495 byte_order = get_byte_order(fd, depth, right, trace);
1496 if (byte_order < 0)
1497 return NULL;
1498 } else if (!strcmp(left->u.unary_expression.u.string, "exp_dig")) {
1499 if (right->u.unary_expression.type != UNARY_UNSIGNED_CONSTANT) {
1500 fprintf(fd, "[error] %s: exp_dig: expecting unsigned constant\n",
1501 __func__);
1502 return NULL;
1503 }
1504 exp_dig = right->u.unary_expression.u.unsigned_constant;
1505 has_exp_dig = 1;
1506 } else if (!strcmp(left->u.unary_expression.u.string, "mant_dig")) {
1507 if (right->u.unary_expression.type != UNARY_UNSIGNED_CONSTANT) {
1508 fprintf(fd, "[error] %s: mant_dig: expecting unsigned constant\n",
1509 __func__);
1510 return NULL;
1511 }
1512 mant_dig = right->u.unary_expression.u.unsigned_constant;
1513 has_mant_dig = 1;
1514 } else if (!strcmp(left->u.unary_expression.u.string, "align")) {
1515 if (right->u.unary_expression.type != UNARY_UNSIGNED_CONSTANT) {
1516 fprintf(fd, "[error] %s: align: expecting unsigned constant\n",
1517 __func__);
1518 return NULL;
1519 }
1520 alignment = right->u.unary_expression.u.unsigned_constant;
1521 /* Make sure alignment is a power of two */
1522 if (alignment == 0 || (alignment & (alignment - 1)) != 0) {
1523 fprintf(fd, "[error] %s: align: expecting power of two\n",
1524 __func__);
1525 return NULL;
1526 }
1527 has_alignment = 1;
1528 } else {
1529 fprintf(fd, "[warning] %s: unknown attribute name %s\n",
1530 __func__, left->u.unary_expression.u.string);
1531 /* Fall-through after warning */
1532 }
1533 }
1534 if (!has_mant_dig) {
1535 fprintf(fd, "[error] %s: missing mant_dig attribute\n", __func__);
1536 return NULL;
1537 }
1538 if (!has_exp_dig) {
1539 fprintf(fd, "[error] %s: missing exp_dig attribute\n", __func__);
1540 return NULL;
1541 }
1542 if (!has_alignment) {
1543 if ((mant_dig + exp_dig) % CHAR_BIT) {
1544 /* bit-packed alignment */
1545 alignment = 1;
1546 } else {
1547 /* byte-packed alignment */
1548 alignment = CHAR_BIT;
1549 }
1550 }
1551 float_declaration = bt_float_declaration_new(mant_dig, exp_dig,
1552 byte_order, alignment);
1553 return &float_declaration->p;
1554 }
1555
1556 static
1557 struct bt_declaration *ctf_declaration_string_visit(FILE *fd, int depth,
1558 struct bt_list_head *expressions,
1559 struct ctf_trace *trace)
1560 {
1561 struct ctf_node *expression;
1562 const char *encoding_c = NULL;
1563 enum ctf_string_encoding encoding = CTF_STRING_UTF8;
1564 struct declaration_string *string_declaration;
1565
1566 bt_list_for_each_entry(expression, expressions, siblings) {
1567 struct ctf_node *left, *right;
1568
1569 left = _bt_list_first_entry(&expression->u.ctf_expression.left, struct ctf_node, siblings);
1570 right = _bt_list_first_entry(&expression->u.ctf_expression.right, struct ctf_node, siblings);
1571 if (left->u.unary_expression.type != UNARY_STRING)
1572 return NULL;
1573 if (!strcmp(left->u.unary_expression.u.string, "encoding")) {
1574 if (right->u.unary_expression.type != UNARY_STRING) {
1575 fprintf(fd, "[error] %s: encoding: expecting string\n",
1576 __func__);
1577 return NULL;
1578 }
1579 encoding_c = right->u.unary_expression.u.string;
1580 } else {
1581 fprintf(fd, "[warning] %s: unknown attribute name %s\n",
1582 __func__, left->u.unary_expression.u.string);
1583 /* Fall-through after warning */
1584 }
1585 }
1586 if (encoding_c && !strcmp(encoding_c, "ASCII"))
1587 encoding = CTF_STRING_ASCII;
1588 string_declaration = bt_string_declaration_new(encoding);
1589 return &string_declaration->p;
1590 }
1591
1592
1593 static
1594 struct bt_declaration *ctf_type_specifier_list_visit(FILE *fd,
1595 int depth, struct ctf_node *type_specifier_list,
1596 struct declaration_scope *declaration_scope,
1597 struct ctf_trace *trace)
1598 {
1599 struct ctf_node *first;
1600 struct ctf_node *node;
1601
1602 if (type_specifier_list->type != NODE_TYPE_SPECIFIER_LIST)
1603 return NULL;
1604
1605 first = _bt_list_first_entry(&type_specifier_list->u.type_specifier_list.head, struct ctf_node, siblings);
1606
1607 if (first->type != NODE_TYPE_SPECIFIER)
1608 return NULL;
1609
1610 node = first->u.type_specifier.node;
1611
1612 switch (first->u.type_specifier.type) {
1613 case TYPESPEC_FLOATING_POINT:
1614 return ctf_declaration_floating_point_visit(fd, depth,
1615 &node->u.floating_point.expressions, trace);
1616 case TYPESPEC_INTEGER:
1617 return ctf_declaration_integer_visit(fd, depth,
1618 &node->u.integer.expressions, trace);
1619 case TYPESPEC_STRING:
1620 return ctf_declaration_string_visit(fd, depth,
1621 &node->u.string.expressions, trace);
1622 case TYPESPEC_STRUCT:
1623 return ctf_declaration_struct_visit(fd, depth,
1624 node->u._struct.name,
1625 &node->u._struct.declaration_list,
1626 node->u._struct.has_body,
1627 &node->u._struct.min_align,
1628 declaration_scope,
1629 trace);
1630 case TYPESPEC_VARIANT:
1631 return ctf_declaration_variant_visit(fd, depth,
1632 node->u.variant.name,
1633 node->u.variant.choice,
1634 &node->u.variant.declaration_list,
1635 node->u.variant.has_body,
1636 declaration_scope,
1637 trace);
1638 case TYPESPEC_ENUM:
1639 return ctf_declaration_enum_visit(fd, depth,
1640 node->u._enum.enum_id,
1641 node->u._enum.container_type,
1642 &node->u._enum.enumerator_list,
1643 node->u._enum.has_body,
1644 declaration_scope,
1645 trace);
1646
1647 case TYPESPEC_VOID:
1648 case TYPESPEC_CHAR:
1649 case TYPESPEC_SHORT:
1650 case TYPESPEC_INT:
1651 case TYPESPEC_LONG:
1652 case TYPESPEC_FLOAT:
1653 case TYPESPEC_DOUBLE:
1654 case TYPESPEC_SIGNED:
1655 case TYPESPEC_UNSIGNED:
1656 case TYPESPEC_BOOL:
1657 case TYPESPEC_COMPLEX:
1658 case TYPESPEC_IMAGINARY:
1659 case TYPESPEC_CONST:
1660 case TYPESPEC_ID_TYPE:
1661 return ctf_declaration_type_specifier_visit(fd, depth,
1662 type_specifier_list, declaration_scope);
1663 default:
1664 fprintf(fd, "[error] %s: unexpected node type %d\n", __func__, (int) first->u.type_specifier.type);
1665 return NULL;
1666 }
1667 }
1668
1669 static
1670 int ctf_event_declaration_visit(FILE *fd, int depth, struct ctf_node *node, struct ctf_event_declaration *event, struct ctf_trace *trace)
1671 {
1672 int ret = 0;
1673
1674 switch (node->type) {
1675 case NODE_TYPEDEF:
1676 ret = ctf_typedef_visit(fd, depth + 1,
1677 event->declaration_scope,
1678 node->u._typedef.type_specifier_list,
1679 &node->u._typedef.type_declarators,
1680 trace);
1681 if (ret)
1682 return ret;
1683 break;
1684 case NODE_TYPEALIAS:
1685 ret = ctf_typealias_visit(fd, depth + 1,
1686 event->declaration_scope,
1687 node->u.typealias.target, node->u.typealias.alias,
1688 trace);
1689 if (ret)
1690 return ret;
1691 break;
1692 case NODE_CTF_EXPRESSION:
1693 {
1694 char *left;
1695
1696 left = concatenate_unary_strings(&node->u.ctf_expression.left);
1697 if (!left)
1698 return -EINVAL;
1699 if (!strcmp(left, "name")) {
1700 char *right;
1701
1702 if (CTF_EVENT_FIELD_IS_SET(event, name)) {
1703 fprintf(fd, "[error] %s: name already declared in event declaration\n", __func__);
1704 ret = -EPERM;
1705 goto error;
1706 }
1707 right = concatenate_unary_strings(&node->u.ctf_expression.right);
1708 if (!right) {
1709 fprintf(fd, "[error] %s: unexpected unary expression for event name\n", __func__);
1710 ret = -EINVAL;
1711 goto error;
1712 }
1713 event->name = g_quark_from_string(right);
1714 g_free(right);
1715 CTF_EVENT_SET_FIELD(event, name);
1716 } else if (!strcmp(left, "id")) {
1717 if (CTF_EVENT_FIELD_IS_SET(event, id)) {
1718 fprintf(fd, "[error] %s: id already declared in event declaration\n", __func__);
1719 ret = -EPERM;
1720 goto error;
1721 }
1722 ret = get_unary_unsigned(&node->u.ctf_expression.right, &event->id);
1723 if (ret) {
1724 fprintf(fd, "[error] %s: unexpected unary expression for event id\n", __func__);
1725 ret = -EINVAL;
1726 goto error;
1727 }
1728 CTF_EVENT_SET_FIELD(event, id);
1729 } else if (!strcmp(left, "stream_id")) {
1730 if (CTF_EVENT_FIELD_IS_SET(event, stream_id)) {
1731 fprintf(fd, "[error] %s: stream_id already declared in event declaration\n", __func__);
1732 ret = -EPERM;
1733 goto error;
1734 }
1735 ret = get_unary_unsigned(&node->u.ctf_expression.right, &event->stream_id);
1736 if (ret) {
1737 fprintf(fd, "[error] %s: unexpected unary expression for event stream_id\n", __func__);
1738 ret = -EINVAL;
1739 goto error;
1740 }
1741 event->stream = trace_stream_lookup(trace, event->stream_id);
1742 if (!event->stream) {
1743 fprintf(fd, "[error] %s: stream id %" PRIu64 " cannot be found\n", __func__, event->stream_id);
1744 ret = -EINVAL;
1745 goto error;
1746 }
1747 CTF_EVENT_SET_FIELD(event, stream_id);
1748 } else if (!strcmp(left, "context")) {
1749 struct bt_declaration *declaration;
1750
1751 if (event->context_decl) {
1752 fprintf(fd, "[error] %s: context already declared in event declaration\n", __func__);
1753 ret = -EINVAL;
1754 goto error;
1755 }
1756 declaration = ctf_type_specifier_list_visit(fd, depth,
1757 _bt_list_first_entry(&node->u.ctf_expression.right,
1758 struct ctf_node, siblings),
1759 event->declaration_scope, trace);
1760 if (!declaration) {
1761 ret = -EPERM;
1762 goto error;
1763 }
1764 if (declaration->id != CTF_TYPE_STRUCT) {
1765 ret = -EPERM;
1766 goto error;
1767 }
1768 event->context_decl = container_of(declaration, struct declaration_struct, p);
1769 } else if (!strcmp(left, "fields")) {
1770 struct bt_declaration *declaration;
1771
1772 if (event->fields_decl) {
1773 fprintf(fd, "[error] %s: fields already declared in event declaration\n", __func__);
1774 ret = -EINVAL;
1775 goto error;
1776 }
1777 declaration = ctf_type_specifier_list_visit(fd, depth,
1778 _bt_list_first_entry(&node->u.ctf_expression.right,
1779 struct ctf_node, siblings),
1780 event->declaration_scope, trace);
1781 if (!declaration) {
1782 ret = -EPERM;
1783 goto error;
1784 }
1785 if (declaration->id != CTF_TYPE_STRUCT) {
1786 ret = -EPERM;
1787 goto error;
1788 }
1789 event->fields_decl = container_of(declaration, struct declaration_struct, p);
1790 } else if (!strcmp(left, "loglevel")) {
1791 int64_t loglevel = -1;
1792
1793 if (CTF_EVENT_FIELD_IS_SET(event, loglevel)) {
1794 fprintf(fd, "[error] %s: loglevel already declared in event declaration\n", __func__);
1795 ret = -EPERM;
1796 goto error;
1797 }
1798 ret = get_unary_signed(&node->u.ctf_expression.right, &loglevel);
1799 if (ret) {
1800 fprintf(fd, "[error] %s: unexpected unary expression for event loglevel\n", __func__);
1801 ret = -EINVAL;
1802 goto error;
1803 }
1804 event->loglevel = (int) loglevel;
1805 CTF_EVENT_SET_FIELD(event, loglevel);
1806 } else if (!strcmp(left, "model.emf.uri")) {
1807 char *right;
1808
1809 if (CTF_EVENT_FIELD_IS_SET(event, model_emf_uri)) {
1810 fprintf(fd, "[error] %s: model.emf.uri already declared in event declaration\n", __func__);
1811 ret = -EPERM;
1812 goto error;
1813 }
1814 right = concatenate_unary_strings(&node->u.ctf_expression.right);
1815 if (!right) {
1816 fprintf(fd, "[error] %s: unexpected unary expression for event model.emf.uri\n", __func__);
1817 ret = -EINVAL;
1818 goto error;
1819 }
1820 event->model_emf_uri = g_quark_from_string(right);
1821 g_free(right);
1822 CTF_EVENT_SET_FIELD(event, model_emf_uri);
1823 } else {
1824 fprintf(fd, "[warning] %s: attribute \"%s\" is unknown in event declaration.\n", __func__, left);
1825 /* Fall-through after warning */
1826 }
1827 error:
1828 g_free(left);
1829 break;
1830 }
1831 default:
1832 return -EPERM;
1833 /* TODO: declaration specifier should be added. */
1834 }
1835
1836 return ret;
1837 }
1838
1839 static
1840 int ctf_event_visit(FILE *fd, int depth, struct ctf_node *node,
1841 struct declaration_scope *parent_declaration_scope, struct ctf_trace *trace)
1842 {
1843 int ret = 0;
1844 struct ctf_node *iter;
1845 struct ctf_event_declaration *event;
1846 struct bt_ctf_event_decl *event_decl;
1847
1848 event_decl = g_new0(struct bt_ctf_event_decl, 1);
1849 event = &event_decl->parent;
1850 event->declaration_scope = bt_new_declaration_scope(parent_declaration_scope);
1851 event->loglevel = -1;
1852 bt_list_for_each_entry(iter, &node->u.event.declaration_list, siblings) {
1853 ret = ctf_event_declaration_visit(fd, depth + 1, iter, event, trace);
1854 if (ret)
1855 goto error;
1856 }
1857 if (!CTF_EVENT_FIELD_IS_SET(event, name)) {
1858 ret = -EPERM;
1859 fprintf(fd, "[error] %s: missing name field in event declaration\n", __func__);
1860 goto error;
1861 }
1862 if (!CTF_EVENT_FIELD_IS_SET(event, stream_id)) {
1863 /* Allow missing stream_id if there is only a single stream */
1864 switch (trace->streams->len) {
1865 case 0: /* Create stream if there was none. */
1866 ret = ctf_stream_visit(fd, depth, NULL, trace->root_declaration_scope, trace);
1867 if (ret)
1868 goto error;
1869 /* Fall-through */
1870 case 1:
1871 event->stream_id = 0;
1872 event->stream = trace_stream_lookup(trace, event->stream_id);
1873 break;
1874 default:
1875 ret = -EPERM;
1876 fprintf(fd, "[error] %s: missing stream_id field in event declaration\n", __func__);
1877 goto error;
1878 }
1879 }
1880 /* Allow only one event without id per stream */
1881 if (!CTF_EVENT_FIELD_IS_SET(event, id)
1882 && event->stream->events_by_id->len != 0) {
1883 ret = -EPERM;
1884 fprintf(fd, "[error] %s: missing id field in event declaration\n", __func__);
1885 goto error;
1886 }
1887 if (event->stream->events_by_id->len <= event->id)
1888 g_ptr_array_set_size(event->stream->events_by_id, event->id + 1);
1889 g_ptr_array_index(event->stream->events_by_id, event->id) = event;
1890 g_hash_table_insert(event->stream->event_quark_to_id,
1891 (gpointer) (unsigned long) event->name,
1892 &event->id);
1893 g_ptr_array_add(trace->event_declarations, event_decl);
1894 return 0;
1895
1896 error:
1897 if (event->fields_decl)
1898 bt_declaration_unref(&event->fields_decl->p);
1899 if (event->context_decl)
1900 bt_declaration_unref(&event->context_decl->p);
1901 bt_free_declaration_scope(event->declaration_scope);
1902 g_free(event_decl);
1903 return ret;
1904 }
1905
1906
1907 static
1908 int ctf_stream_declaration_visit(FILE *fd, int depth, struct ctf_node *node, struct ctf_stream_declaration *stream, struct ctf_trace *trace)
1909 {
1910 int ret = 0;
1911
1912 switch (node->type) {
1913 case NODE_TYPEDEF:
1914 ret = ctf_typedef_visit(fd, depth + 1,
1915 stream->declaration_scope,
1916 node->u._typedef.type_specifier_list,
1917 &node->u._typedef.type_declarators,
1918 trace);
1919 if (ret)
1920 return ret;
1921 break;
1922 case NODE_TYPEALIAS:
1923 ret = ctf_typealias_visit(fd, depth + 1,
1924 stream->declaration_scope,
1925 node->u.typealias.target, node->u.typealias.alias,
1926 trace);
1927 if (ret)
1928 return ret;
1929 break;
1930 case NODE_CTF_EXPRESSION:
1931 {
1932 char *left;
1933
1934 left = concatenate_unary_strings(&node->u.ctf_expression.left);
1935 if (!left)
1936 return -EINVAL;
1937 if (!strcmp(left, "id")) {
1938 if (CTF_STREAM_FIELD_IS_SET(stream, stream_id)) {
1939 fprintf(fd, "[error] %s: id already declared in stream declaration\n", __func__);
1940 ret = -EPERM;
1941 goto error;
1942 }
1943 ret = get_unary_unsigned(&node->u.ctf_expression.right, &stream->stream_id);
1944 if (ret) {
1945 fprintf(fd, "[error] %s: unexpected unary expression for stream id\n", __func__);
1946 ret = -EINVAL;
1947 goto error;
1948 }
1949 CTF_STREAM_SET_FIELD(stream, stream_id);
1950 } else if (!strcmp(left, "event.header")) {
1951 struct bt_declaration *declaration;
1952
1953 if (stream->event_header_decl) {
1954 fprintf(fd, "[error] %s: event.header already declared in stream declaration\n", __func__);
1955 ret = -EINVAL;
1956 goto error;
1957 }
1958 declaration = ctf_type_specifier_list_visit(fd, depth,
1959 _bt_list_first_entry(&node->u.ctf_expression.right,
1960 struct ctf_node, siblings),
1961 stream->declaration_scope, trace);
1962 if (!declaration) {
1963 ret = -EPERM;
1964 goto error;
1965 }
1966 if (declaration->id != CTF_TYPE_STRUCT) {
1967 ret = -EPERM;
1968 goto error;
1969 }
1970 stream->event_header_decl = container_of(declaration, struct declaration_struct, p);
1971 } else if (!strcmp(left, "event.context")) {
1972 struct bt_declaration *declaration;
1973
1974 if (stream->event_context_decl) {
1975 fprintf(fd, "[error] %s: event.context already declared in stream declaration\n", __func__);
1976 ret = -EINVAL;
1977 goto error;
1978 }
1979 declaration = ctf_type_specifier_list_visit(fd, depth,
1980 _bt_list_first_entry(&node->u.ctf_expression.right,
1981 struct ctf_node, siblings),
1982 stream->declaration_scope, trace);
1983 if (!declaration) {
1984 ret = -EPERM;
1985 goto error;
1986 }
1987 if (declaration->id != CTF_TYPE_STRUCT) {
1988 ret = -EPERM;
1989 goto error;
1990 }
1991 stream->event_context_decl = container_of(declaration, struct declaration_struct, p);
1992 } else if (!strcmp(left, "packet.context")) {
1993 struct bt_declaration *declaration;
1994
1995 if (stream->packet_context_decl) {
1996 fprintf(fd, "[error] %s: packet.context already declared in stream declaration\n", __func__);
1997 ret = -EINVAL;
1998 goto error;
1999 }
2000 declaration = ctf_type_specifier_list_visit(fd, depth,
2001 _bt_list_first_entry(&node->u.ctf_expression.right,
2002 struct ctf_node, siblings),
2003 stream->declaration_scope, trace);
2004 if (!declaration) {
2005 ret = -EPERM;
2006 goto error;
2007 }
2008 if (declaration->id != CTF_TYPE_STRUCT) {
2009 ret = -EPERM;
2010 goto error;
2011 }
2012 stream->packet_context_decl = container_of(declaration, struct declaration_struct, p);
2013 } else {
2014 fprintf(fd, "[warning] %s: attribute \"%s\" is unknown in stream declaration.\n", __func__, left);
2015 /* Fall-through after warning */
2016 }
2017
2018 error:
2019 g_free(left);
2020 break;
2021 }
2022 default:
2023 return -EPERM;
2024 /* TODO: declaration specifier should be added. */
2025 }
2026
2027 return ret;
2028 }
2029
2030 static
2031 int ctf_stream_visit(FILE *fd, int depth, struct ctf_node *node,
2032 struct declaration_scope *parent_declaration_scope, struct ctf_trace *trace)
2033 {
2034 int ret = 0;
2035 struct ctf_node *iter;
2036 struct ctf_stream_declaration *stream;
2037
2038 stream = g_new0(struct ctf_stream_declaration, 1);
2039 stream->declaration_scope = bt_new_declaration_scope(parent_declaration_scope);
2040 stream->events_by_id = g_ptr_array_new();
2041 stream->event_quark_to_id = g_hash_table_new(g_direct_hash, g_direct_equal);
2042 stream->streams = g_ptr_array_new();
2043 if (node) {
2044 bt_list_for_each_entry(iter, &node->u.stream.declaration_list, siblings) {
2045 ret = ctf_stream_declaration_visit(fd, depth + 1, iter, stream, trace);
2046 if (ret)
2047 goto error;
2048 }
2049 }
2050 if (CTF_STREAM_FIELD_IS_SET(stream, stream_id)) {
2051 /* check that packet header has stream_id field. */
2052 if (!trace->packet_header_decl
2053 || bt_struct_declaration_lookup_field_index(trace->packet_header_decl, g_quark_from_static_string("stream_id")) < 0) {
2054 ret = -EPERM;
2055 fprintf(fd, "[error] %s: missing stream_id field in packet header declaration, but stream_id attribute is declared for stream.\n", __func__);
2056 goto error;
2057 }
2058 } else {
2059 /* Allow only one id-less stream */
2060 if (trace->streams->len != 0) {
2061 ret = -EPERM;
2062 fprintf(fd, "[error] %s: missing id field in stream declaration\n", __func__);
2063 goto error;
2064 }
2065 stream->stream_id = 0;
2066 }
2067 if (trace->streams->len <= stream->stream_id)
2068 g_ptr_array_set_size(trace->streams, stream->stream_id + 1);
2069 g_ptr_array_index(trace->streams, stream->stream_id) = stream;
2070 stream->trace = trace;
2071
2072 return 0;
2073
2074 error:
2075 if (stream->event_header_decl)
2076 bt_declaration_unref(&stream->event_header_decl->p);
2077 if (stream->event_context_decl)
2078 bt_declaration_unref(&stream->event_context_decl->p);
2079 if (stream->packet_context_decl)
2080 bt_declaration_unref(&stream->packet_context_decl->p);
2081 g_ptr_array_free(stream->streams, TRUE);
2082 g_ptr_array_free(stream->events_by_id, TRUE);
2083 g_hash_table_destroy(stream->event_quark_to_id);
2084 bt_free_declaration_scope(stream->declaration_scope);
2085 g_free(stream);
2086 return ret;
2087 }
2088
2089 static
2090 int ctf_trace_declaration_visit(FILE *fd, int depth, struct ctf_node *node, struct ctf_trace *trace)
2091 {
2092 int ret = 0;
2093
2094 switch (node->type) {
2095 case NODE_TYPEDEF:
2096 ret = ctf_typedef_visit(fd, depth + 1,
2097 trace->declaration_scope,
2098 node->u._typedef.type_specifier_list,
2099 &node->u._typedef.type_declarators,
2100 trace);
2101 if (ret)
2102 return ret;
2103 break;
2104 case NODE_TYPEALIAS:
2105 ret = ctf_typealias_visit(fd, depth + 1,
2106 trace->declaration_scope,
2107 node->u.typealias.target, node->u.typealias.alias,
2108 trace);
2109 if (ret)
2110 return ret;
2111 break;
2112 case NODE_CTF_EXPRESSION:
2113 {
2114 char *left;
2115
2116 left = concatenate_unary_strings(&node->u.ctf_expression.left);
2117 if (!left)
2118 return -EINVAL;
2119 if (!strcmp(left, "major")) {
2120 if (CTF_TRACE_FIELD_IS_SET(trace, major)) {
2121 fprintf(fd, "[error] %s: major already declared in trace declaration\n", __func__);
2122 ret = -EPERM;
2123 goto error;
2124 }
2125 ret = get_unary_unsigned(&node->u.ctf_expression.right, &trace->major);
2126 if (ret) {
2127 fprintf(fd, "[error] %s: unexpected unary expression for trace major number\n", __func__);
2128 ret = -EINVAL;
2129 goto error;
2130 }
2131 CTF_TRACE_SET_FIELD(trace, major);
2132 } else if (!strcmp(left, "minor")) {
2133 if (CTF_TRACE_FIELD_IS_SET(trace, minor)) {
2134 fprintf(fd, "[error] %s: minor already declared in trace declaration\n", __func__);
2135 ret = -EPERM;
2136 goto error;
2137 }
2138 ret = get_unary_unsigned(&node->u.ctf_expression.right, &trace->minor);
2139 if (ret) {
2140 fprintf(fd, "[error] %s: unexpected unary expression for trace minor number\n", __func__);
2141 ret = -EINVAL;
2142 goto error;
2143 }
2144 CTF_TRACE_SET_FIELD(trace, minor);
2145 } else if (!strcmp(left, "uuid")) {
2146 unsigned char uuid[BABELTRACE_UUID_LEN];
2147
2148 ret = get_unary_uuid(&node->u.ctf_expression.right, uuid);
2149 if (ret) {
2150 fprintf(fd, "[error] %s: unexpected unary expression for trace uuid\n", __func__);
2151 ret = -EINVAL;
2152 goto error;
2153 }
2154 if (CTF_TRACE_FIELD_IS_SET(trace, uuid)
2155 && babeltrace_uuid_compare(uuid, trace->uuid)) {
2156 fprintf(fd, "[error] %s: uuid mismatch\n", __func__);
2157 ret = -EPERM;
2158 goto error;
2159 } else {
2160 memcpy(trace->uuid, uuid, sizeof(uuid));
2161 }
2162 CTF_TRACE_SET_FIELD(trace, uuid);
2163 } else if (!strcmp(left, "byte_order")) {
2164 struct ctf_node *right;
2165 int byte_order;
2166
2167 right = _bt_list_first_entry(&node->u.ctf_expression.right, struct ctf_node, siblings);
2168 byte_order = get_trace_byte_order(fd, depth, right);
2169 if (byte_order < 0) {
2170 ret = -EINVAL;
2171 goto error;
2172 }
2173
2174 if (CTF_TRACE_FIELD_IS_SET(trace, byte_order)
2175 && byte_order != trace->byte_order) {
2176 fprintf(fd, "[error] %s: endianness mismatch\n", __func__);
2177 ret = -EPERM;
2178 goto error;
2179 } else {
2180 if (byte_order != trace->byte_order) {
2181 trace->byte_order = byte_order;
2182 /*
2183 * We need to restart
2184 * construction of the
2185 * intermediate representation.
2186 */
2187 trace->field_mask = 0;
2188 CTF_TRACE_SET_FIELD(trace, byte_order);
2189 ret = -EINTR;
2190 goto error;
2191 }
2192 }
2193 CTF_TRACE_SET_FIELD(trace, byte_order);
2194 } else if (!strcmp(left, "packet.header")) {
2195 struct bt_declaration *declaration;
2196
2197 if (trace->packet_header_decl) {
2198 fprintf(fd, "[error] %s: packet.header already declared in trace declaration\n", __func__);
2199 ret = -EINVAL;
2200 goto error;
2201 }
2202 declaration = ctf_type_specifier_list_visit(fd, depth,
2203 _bt_list_first_entry(&node->u.ctf_expression.right,
2204 struct ctf_node, siblings),
2205 trace->declaration_scope, trace);
2206 if (!declaration) {
2207 ret = -EPERM;
2208 goto error;
2209 }
2210 if (declaration->id != CTF_TYPE_STRUCT) {
2211 ret = -EPERM;
2212 goto error;
2213 }
2214 trace->packet_header_decl = container_of(declaration, struct declaration_struct, p);
2215 } else {
2216 fprintf(fd, "[warning] %s: attribute \"%s\" is unknown in trace declaration.\n", __func__, left);
2217 }
2218
2219 error:
2220 g_free(left);
2221 break;
2222 }
2223 default:
2224 return -EPERM;
2225 /* TODO: declaration specifier should be added. */
2226 }
2227
2228 return ret;
2229 }
2230
2231 static
2232 int ctf_trace_visit(FILE *fd, int depth, struct ctf_node *node, struct ctf_trace *trace)
2233 {
2234 int ret = 0;
2235 struct ctf_node *iter;
2236
2237 if (trace->declaration_scope)
2238 return -EEXIST;
2239 trace->declaration_scope = bt_new_declaration_scope(trace->root_declaration_scope);
2240 trace->streams = g_ptr_array_new();
2241 trace->event_declarations = g_ptr_array_new();
2242 bt_list_for_each_entry(iter, &node->u.trace.declaration_list, siblings) {
2243 ret = ctf_trace_declaration_visit(fd, depth + 1, iter, trace);
2244 if (ret)
2245 goto error;
2246 }
2247 if (!CTF_TRACE_FIELD_IS_SET(trace, major)) {
2248 ret = -EPERM;
2249 fprintf(fd, "[error] %s: missing major field in trace declaration\n", __func__);
2250 goto error;
2251 }
2252 if (!CTF_TRACE_FIELD_IS_SET(trace, minor)) {
2253 ret = -EPERM;
2254 fprintf(fd, "[error] %s: missing minor field in trace declaration\n", __func__);
2255 goto error;
2256 }
2257 if (!CTF_TRACE_FIELD_IS_SET(trace, byte_order)) {
2258 ret = -EPERM;
2259 fprintf(fd, "[error] %s: missing byte_order field in trace declaration\n", __func__);
2260 goto error;
2261 }
2262
2263 if (!CTF_TRACE_FIELD_IS_SET(trace, byte_order)) {
2264 /* check that the packet header contains a "magic" field */
2265 if (!trace->packet_header_decl
2266 || bt_struct_declaration_lookup_field_index(trace->packet_header_decl, g_quark_from_static_string("magic")) < 0) {
2267 ret = -EPERM;
2268 fprintf(fd, "[error] %s: missing both byte_order and packet header magic number in trace declaration\n", __func__);
2269 goto error;
2270 }
2271 }
2272 return 0;
2273
2274 error:
2275 if (trace->packet_header_decl) {
2276 bt_declaration_unref(&trace->packet_header_decl->p);
2277 trace->packet_header_decl = NULL;
2278 }
2279 g_ptr_array_free(trace->streams, TRUE);
2280 g_ptr_array_free(trace->event_declarations, TRUE);
2281 bt_free_declaration_scope(trace->declaration_scope);
2282 trace->declaration_scope = NULL;
2283 return ret;
2284 }
2285
2286 static
2287 int ctf_clock_declaration_visit(FILE *fd, int depth, struct ctf_node *node,
2288 struct ctf_clock *clock, struct ctf_trace *trace)
2289 {
2290 int ret = 0;
2291
2292 switch (node->type) {
2293 case NODE_CTF_EXPRESSION:
2294 {
2295 char *left;
2296
2297 left = concatenate_unary_strings(&node->u.ctf_expression.left);
2298 if (!left)
2299 return -EINVAL;
2300 if (!strcmp(left, "name")) {
2301 char *right;
2302
2303 if (CTF_CLOCK_FIELD_IS_SET(clock, name)) {
2304 fprintf(fd, "[error] %s: name already declared in clock declaration\n", __func__);
2305 ret = -EPERM;
2306 goto error;
2307 }
2308 right = concatenate_unary_strings(&node->u.ctf_expression.right);
2309 if (!right) {
2310 fprintf(fd, "[error] %s: unexpected unary expression for clock name\n", __func__);
2311 ret = -EINVAL;
2312 goto error;
2313 }
2314 clock->name = g_quark_from_string(right);
2315 g_free(right);
2316 CTF_CLOCK_SET_FIELD(clock, name);
2317 } else if (!strcmp(left, "uuid")) {
2318 char *right;
2319
2320 if (clock->uuid) {
2321 fprintf(fd, "[error] %s: uuid already declared in clock declaration\n", __func__);
2322 ret = -EPERM;
2323 goto error;
2324 }
2325 right = concatenate_unary_strings(&node->u.ctf_expression.right);
2326 if (!right) {
2327 fprintf(fd, "[error] %s: unexpected unary expression for clock uuid\n", __func__);
2328 ret = -EINVAL;
2329 goto error;
2330 }
2331 clock->uuid = g_quark_from_string(right);
2332 g_free(right);
2333 } else if (!strcmp(left, "description")) {
2334 char *right;
2335
2336 if (clock->description) {
2337 fprintf(fd, "[warning] %s: duplicated clock description\n", __func__);
2338 goto error; /* ret is 0, so not an actual error, just warn. */
2339 }
2340 right = concatenate_unary_strings(&node->u.ctf_expression.right);
2341 if (!right) {
2342 fprintf(fd, "[warning] %s: unexpected unary expression for clock description\n", __func__);
2343 goto error; /* ret is 0, so not an actual error, just warn. */
2344 }
2345 clock->description = right;
2346 } else if (!strcmp(left, "freq")) {
2347 if (CTF_CLOCK_FIELD_IS_SET(clock, freq)) {
2348 fprintf(fd, "[error] %s: freq already declared in clock declaration\n", __func__);
2349 ret = -EPERM;
2350 goto error;
2351 }
2352 ret = get_unary_unsigned(&node->u.ctf_expression.right, &clock->freq);
2353 if (ret) {
2354 fprintf(fd, "[error] %s: unexpected unary expression for clock freq\n", __func__);
2355 ret = -EINVAL;
2356 goto error;
2357 }
2358 CTF_CLOCK_SET_FIELD(clock, freq);
2359 } else if (!strcmp(left, "precision")) {
2360 if (clock->precision) {
2361 fprintf(fd, "[error] %s: precision already declared in clock declaration\n", __func__);
2362 ret = -EPERM;
2363 goto error;
2364 }
2365 ret = get_unary_unsigned(&node->u.ctf_expression.right, &clock->precision);
2366 if (ret) {
2367 fprintf(fd, "[error] %s: unexpected unary expression for clock precision\n", __func__);
2368 ret = -EINVAL;
2369 goto error;
2370 }
2371 } else if (!strcmp(left, "offset_s")) {
2372 if (clock->offset_s) {
2373 fprintf(fd, "[error] %s: offset_s already declared in clock declaration\n", __func__);
2374 ret = -EPERM;
2375 goto error;
2376 }
2377 ret = get_unary_unsigned(&node->u.ctf_expression.right, &clock->offset_s);
2378 if (ret) {
2379 fprintf(fd, "[error] %s: unexpected unary expression for clock offset_s\n", __func__);
2380 ret = -EINVAL;
2381 goto error;
2382 }
2383 } else if (!strcmp(left, "offset")) {
2384 if (clock->offset) {
2385 fprintf(fd, "[error] %s: offset already declared in clock declaration\n", __func__);
2386 ret = -EPERM;
2387 goto error;
2388 }
2389 ret = get_unary_unsigned(&node->u.ctf_expression.right, &clock->offset);
2390 if (ret) {
2391 fprintf(fd, "[error] %s: unexpected unary expression for clock offset\n", __func__);
2392 ret = -EINVAL;
2393 goto error;
2394 }
2395 } else if (!strcmp(left, "absolute")) {
2396 struct ctf_node *right;
2397
2398 right = _bt_list_first_entry(&node->u.ctf_expression.right, struct ctf_node, siblings);
2399 ret = get_boolean(fd, depth, right);
2400 if (ret < 0) {
2401 fprintf(fd, "[error] %s: unexpected \"absolute\" right member\n", __func__);
2402 ret = -EINVAL;
2403 goto error;
2404 }
2405 clock->absolute = ret;
2406 } else {
2407 fprintf(fd, "[warning] %s: attribute \"%s\" is unknown in clock declaration.\n", __func__, left);
2408 }
2409
2410 error:
2411 g_free(left);
2412 break;
2413 }
2414 default:
2415 return -EPERM;
2416 /* TODO: declaration specifier should be added. */
2417 }
2418
2419 return ret;
2420 }
2421
2422 static
2423 int ctf_clock_visit(FILE *fd, int depth, struct ctf_node *node, struct ctf_trace *trace)
2424 {
2425 int ret = 0;
2426 struct ctf_node *iter;
2427 struct ctf_clock *clock;
2428
2429 clock = g_new0(struct ctf_clock, 1);
2430 /* Default clock frequency is set to 1000000000 */
2431 clock->freq = 1000000000ULL;
2432 bt_list_for_each_entry(iter, &node->u.clock.declaration_list, siblings) {
2433 ret = ctf_clock_declaration_visit(fd, depth + 1, iter, clock, trace);
2434 if (ret)
2435 goto error;
2436 }
2437 if (opt_clock_force_correlate) {
2438 /*
2439 * User requested to forcibly correlate the clock
2440 * sources, even if we have no correlation
2441 * information.
2442 */
2443 if (!clock->absolute) {
2444 fprintf(fd, "[warning] Forcibly correlating trace clock sources (--clock-force-correlate).\n");
2445 }
2446 clock->absolute = 1;
2447 }
2448 if (!CTF_CLOCK_FIELD_IS_SET(clock, name)) {
2449 ret = -EPERM;
2450 fprintf(fd, "[error] %s: missing name field in clock declaration\n", __func__);
2451 goto error;
2452 }
2453 if (g_hash_table_size(trace->parent.clocks) > 0) {
2454 fprintf(fd, "[error] Only CTF traces with a single clock description are supported by this babeltrace version.\n");
2455 ret = -EINVAL;
2456 goto error;
2457 }
2458 trace->parent.single_clock = clock;
2459 g_hash_table_insert(trace->parent.clocks, (gpointer) (unsigned long) clock->name, clock);
2460 return 0;
2461
2462 error:
2463 g_free(clock->description);
2464 g_free(clock);
2465 return ret;
2466 }
2467
2468 static
2469 void ctf_clock_default(FILE *fd, int depth, struct ctf_trace *trace)
2470 {
2471 struct ctf_clock *clock;
2472
2473 clock = g_new0(struct ctf_clock, 1);
2474 clock->name = g_quark_from_string("monotonic");
2475 clock->uuid = 0;
2476 clock->description = g_strdup("Default clock");
2477 /* Default clock frequency is set to 1000000000 */
2478 clock->freq = 1000000000ULL;
2479 if (opt_clock_force_correlate) {
2480 /*
2481 * User requested to forcibly correlate the clock
2482 * sources, even if we have no correlatation
2483 * information.
2484 */
2485 if (!clock->absolute) {
2486 fprintf(fd, "[warning] Forcibly correlating trace clock sources (--clock-force-correlate).\n");
2487 }
2488 clock->absolute = 1;
2489 } else {
2490 clock->absolute = 0; /* Not an absolute reference across traces */
2491 }
2492
2493 trace->parent.single_clock = clock;
2494 g_hash_table_insert(trace->parent.clocks, (gpointer) (unsigned long) clock->name, clock);
2495 }
2496
2497 static
2498 void clock_free(gpointer data)
2499 {
2500 struct ctf_clock *clock = data;
2501
2502 g_free(clock->description);
2503 g_free(clock);
2504 }
2505
2506 static
2507 int ctf_callsite_declaration_visit(FILE *fd, int depth, struct ctf_node *node,
2508 struct ctf_callsite *callsite, struct ctf_trace *trace)
2509 {
2510 int ret = 0;
2511
2512 switch (node->type) {
2513 case NODE_CTF_EXPRESSION:
2514 {
2515 char *left;
2516
2517 left = concatenate_unary_strings(&node->u.ctf_expression.left);
2518 if (!left)
2519 return -EINVAL;
2520 if (!strcmp(left, "name")) {
2521 char *right;
2522
2523 if (CTF_CALLSITE_FIELD_IS_SET(callsite, name)) {
2524 fprintf(fd, "[error] %s: name already declared in callsite declaration\n", __func__);
2525 ret = -EPERM;
2526 goto error;
2527 }
2528 right = concatenate_unary_strings(&node->u.ctf_expression.right);
2529 if (!right) {
2530 fprintf(fd, "[error] %s: unexpected unary expression for callsite name\n", __func__);
2531 ret = -EINVAL;
2532 goto error;
2533 }
2534 callsite->name = g_quark_from_string(right);
2535 g_free(right);
2536 CTF_CALLSITE_SET_FIELD(callsite, name);
2537 } else if (!strcmp(left, "func")) {
2538 char *right;
2539
2540 if (CTF_CALLSITE_FIELD_IS_SET(callsite, func)) {
2541 fprintf(fd, "[error] %s: func already declared in callsite declaration\n", __func__);
2542 ret = -EPERM;
2543 goto error;
2544 }
2545 right = concatenate_unary_strings(&node->u.ctf_expression.right);
2546 if (!right) {
2547 fprintf(fd, "[error] %s: unexpected unary expression for callsite func\n", __func__);
2548 ret = -EINVAL;
2549 goto error;
2550 }
2551 callsite->func = right;
2552 CTF_CALLSITE_SET_FIELD(callsite, func);
2553 } else if (!strcmp(left, "file")) {
2554 char *right;
2555
2556 if (CTF_CALLSITE_FIELD_IS_SET(callsite, file)) {
2557 fprintf(fd, "[error] %s: file already declared in callsite declaration\n", __func__);
2558 ret = -EPERM;
2559 goto error;
2560 }
2561 right = concatenate_unary_strings(&node->u.ctf_expression.right);
2562 if (!right) {
2563 fprintf(fd, "[error] %s: unexpected unary expression for callsite file\n", __func__);
2564 ret = -EINVAL;
2565 goto error;
2566 }
2567 callsite->file = right;
2568 CTF_CALLSITE_SET_FIELD(callsite, file);
2569 } else if (!strcmp(left, "line")) {
2570 if (CTF_CALLSITE_FIELD_IS_SET(callsite, line)) {
2571 fprintf(fd, "[error] %s: line already declared in callsite declaration\n", __func__);
2572 ret = -EPERM;
2573 goto error;
2574 }
2575 ret = get_unary_unsigned(&node->u.ctf_expression.right, &callsite->line);
2576 if (ret) {
2577 fprintf(fd, "[error] %s: unexpected unary expression for callsite line\n", __func__);
2578 ret = -EINVAL;
2579 goto error;
2580 }
2581 CTF_CALLSITE_SET_FIELD(callsite, line);
2582 } else if (!strcmp(left, "ip")) {
2583 if (CTF_CALLSITE_FIELD_IS_SET(callsite, ip)) {
2584 fprintf(fd, "[error] %s: ip already declared in callsite declaration\n", __func__);
2585 ret = -EPERM;
2586 goto error;
2587 }
2588 ret = get_unary_unsigned(&node->u.ctf_expression.right, &callsite->ip);
2589 if (ret) {
2590 fprintf(fd, "[error] %s: unexpected unary expression for callsite ip\n", __func__);
2591 ret = -EINVAL;
2592 goto error;
2593 }
2594 CTF_CALLSITE_SET_FIELD(callsite, ip);
2595 } else {
2596 fprintf(fd, "[warning] %s: attribute \"%s\" is unknown in callsite declaration.\n", __func__, left);
2597 }
2598
2599 error:
2600 g_free(left);
2601 break;
2602 }
2603 default:
2604 return -EPERM;
2605 /* TODO: declaration specifier should be added. */
2606 }
2607
2608 return ret;
2609 }
2610
2611 static
2612 int ctf_callsite_visit(FILE *fd, int depth, struct ctf_node *node, struct ctf_trace *trace)
2613 {
2614 int ret = 0;
2615 struct ctf_node *iter;
2616 struct ctf_callsite *callsite;
2617 struct ctf_callsite_dups *cs_dups;
2618
2619 callsite = g_new0(struct ctf_callsite, 1);
2620 bt_list_for_each_entry(iter, &node->u.callsite.declaration_list, siblings) {
2621 ret = ctf_callsite_declaration_visit(fd, depth + 1, iter, callsite, trace);
2622 if (ret)
2623 goto error;
2624 }
2625 if (!CTF_CALLSITE_FIELD_IS_SET(callsite, name)) {
2626 ret = -EPERM;
2627 fprintf(fd, "[error] %s: missing name field in callsite declaration\n", __func__);
2628 goto error;
2629 }
2630 if (!CTF_CALLSITE_FIELD_IS_SET(callsite, func)) {
2631 ret = -EPERM;
2632 fprintf(fd, "[error] %s: missing func field in callsite declaration\n", __func__);
2633 goto error;
2634 }
2635 if (!CTF_CALLSITE_FIELD_IS_SET(callsite, file)) {
2636 ret = -EPERM;
2637 fprintf(fd, "[error] %s: missing file field in callsite declaration\n", __func__);
2638 goto error;
2639 }
2640 if (!CTF_CALLSITE_FIELD_IS_SET(callsite, line)) {
2641 ret = -EPERM;
2642 fprintf(fd, "[error] %s: missing line field in callsite declaration\n", __func__);
2643 goto error;
2644 }
2645
2646 cs_dups = g_hash_table_lookup(trace->callsites,
2647 (gpointer) (unsigned long) callsite->name);
2648 if (!cs_dups) {
2649 cs_dups = g_new0(struct ctf_callsite_dups, 1);
2650 BT_INIT_LIST_HEAD(&cs_dups->head);
2651 g_hash_table_insert(trace->callsites,
2652 (gpointer) (unsigned long) callsite->name, cs_dups);
2653 }
2654 bt_list_add_tail(&callsite->node, &cs_dups->head);
2655 return 0;
2656
2657 error:
2658 g_free(callsite->func);
2659 g_free(callsite->file);
2660 g_free(callsite);
2661 return ret;
2662 }
2663
2664 static
2665 void callsite_free(gpointer data)
2666 {
2667 struct ctf_callsite_dups *cs_dups = data;
2668 struct ctf_callsite *callsite, *cs_n;
2669
2670 bt_list_for_each_entry_safe(callsite, cs_n, &cs_dups->head, node) {
2671 g_free(callsite->func);
2672 g_free(callsite->file);
2673 g_free(callsite);
2674 }
2675 g_free(cs_dups);
2676 }
2677
2678 static
2679 int ctf_env_declaration_visit(FILE *fd, int depth, struct ctf_node *node,
2680 struct ctf_trace *trace)
2681 {
2682 int ret = 0;
2683 struct ctf_tracer_env *env = &trace->env;
2684
2685 switch (node->type) {
2686 case NODE_CTF_EXPRESSION:
2687 {
2688 char *left;
2689
2690 left = concatenate_unary_strings(&node->u.ctf_expression.left);
2691 if (!left)
2692 return -EINVAL;
2693 if (!strcmp(left, "vpid")) {
2694 uint64_t v;
2695
2696 if (env->vpid != -1) {
2697 fprintf(fd, "[error] %s: vpid already declared in env declaration\n", __func__);
2698 goto error; /* ret is 0, so not an actual error, just warn. */
2699 }
2700 ret = get_unary_unsigned(&node->u.ctf_expression.right, &v);
2701 if (ret) {
2702 fprintf(fd, "[error] %s: unexpected unary expression for env vpid\n", __func__);
2703 goto error; /* ret is 0, so not an actual error, just warn. */
2704 }
2705 env->vpid = (int) v;
2706 printf_verbose("env.vpid = %d\n", env->vpid);
2707 } else if (!strcmp(left, "procname")) {
2708 char *right;
2709
2710 if (env->procname[0]) {
2711 fprintf(fd, "[warning] %s: duplicated env procname\n", __func__);
2712 goto error; /* ret is 0, so not an actual error, just warn. */
2713 }
2714 right = concatenate_unary_strings(&node->u.ctf_expression.right);
2715 if (!right) {
2716 fprintf(fd, "[warning] %s: unexpected unary expression for env procname\n", __func__);
2717 goto error; /* ret is 0, so not an actual error, just warn. */
2718 }
2719 strncpy(env->procname, right, TRACER_ENV_LEN);
2720 env->procname[TRACER_ENV_LEN - 1] = '\0';
2721 printf_verbose("env.procname = \"%s\"\n", env->procname);
2722 g_free(right);
2723 } else if (!strcmp(left, "hostname")) {
2724 char *right;
2725
2726 if (env->hostname[0]) {
2727 fprintf(fd, "[warning] %s: duplicated env hostname\n", __func__);
2728 goto error; /* ret is 0, so not an actual error, just warn. */
2729 }
2730 right = concatenate_unary_strings(&node->u.ctf_expression.right);
2731 if (!right) {
2732 fprintf(fd, "[warning] %s: unexpected unary expression for env hostname\n", __func__);
2733 goto error; /* ret is 0, so not an actual error, just warn. */
2734 }
2735 strncpy(env->hostname, right, TRACER_ENV_LEN);
2736 env->hostname[TRACER_ENV_LEN - 1] = '\0';
2737 printf_verbose("env.hostname = \"%s\"\n", env->hostname);
2738 g_free(right);
2739 } else if (!strcmp(left, "domain")) {
2740 char *right;
2741
2742 if (env->domain[0]) {
2743 fprintf(fd, "[warning] %s: duplicated env domain\n", __func__);
2744 goto error; /* ret is 0, so not an actual error, just warn. */
2745 }
2746 right = concatenate_unary_strings(&node->u.ctf_expression.right);
2747 if (!right) {
2748 fprintf(fd, "[warning] %s: unexpected unary expression for env domain\n", __func__);
2749 goto error; /* ret is 0, so not an actual error, just warn. */
2750 }
2751 strncpy(env->domain, right, TRACER_ENV_LEN);
2752 env->domain[TRACER_ENV_LEN - 1] = '\0';
2753 printf_verbose("env.domain = \"%s\"\n", env->domain);
2754 g_free(right);
2755 } else if (!strcmp(left, "sysname")) {
2756 char *right;
2757
2758 if (env->sysname[0]) {
2759 fprintf(fd, "[warning] %s: duplicated env sysname\n", __func__);
2760 goto error; /* ret is 0, so not an actual error, just warn. */
2761 }
2762 right = concatenate_unary_strings(&node->u.ctf_expression.right);
2763 if (!right) {
2764 fprintf(fd, "[warning] %s: unexpected unary expression for env sysname\n", __func__);
2765 goto error; /* ret is 0, so not an actual error, just warn. */
2766 }
2767 strncpy(env->sysname, right, TRACER_ENV_LEN);
2768 env->sysname[TRACER_ENV_LEN - 1] = '\0';
2769 printf_verbose("env.sysname = \"%s\"\n", env->sysname);
2770 g_free(right);
2771 } else if (!strcmp(left, "kernel_release")) {
2772 char *right;
2773
2774 if (env->release[0]) {
2775 fprintf(fd, "[warning] %s: duplicated env release\n", __func__);
2776 goto error; /* ret is 0, so not an actual error, just warn. */
2777 }
2778 right = concatenate_unary_strings(&node->u.ctf_expression.right);
2779 if (!right) {
2780 fprintf(fd, "[warning] %s: unexpected unary expression for env release\n", __func__);
2781 goto error; /* ret is 0, so not an actual error, just warn. */
2782 }
2783 strncpy(env->release, right, TRACER_ENV_LEN);
2784 env->release[TRACER_ENV_LEN - 1] = '\0';
2785 printf_verbose("env.release = \"%s\"\n", env->release);
2786 g_free(right);
2787 } else if (!strcmp(left, "kernel_version")) {
2788 char *right;
2789
2790 if (env->version[0]) {
2791 fprintf(fd, "[warning] %s: duplicated env version\n", __func__);
2792 goto error; /* ret is 0, so not an actual error, just warn. */
2793 }
2794 right = concatenate_unary_strings(&node->u.ctf_expression.right);
2795 if (!right) {
2796 fprintf(fd, "[warning] %s: unexpected unary expression for env version\n", __func__);
2797 goto error; /* ret is 0, so not an actual error, just warn. */
2798 }
2799 strncpy(env->version, right, TRACER_ENV_LEN);
2800 env->version[TRACER_ENV_LEN - 1] = '\0';
2801 printf_verbose("env.version = \"%s\"\n", env->version);
2802 g_free(right);
2803 } else {
2804 if (is_unary_string(&node->u.ctf_expression.right)) {
2805 char *right;
2806
2807 right = concatenate_unary_strings(&node->u.ctf_expression.right);
2808 if (!right) {
2809 fprintf(fd, "[warning] %s: unexpected unary expression for env\n", __func__);
2810 ret = -EINVAL;
2811 goto error;
2812 }
2813 printf_verbose("env.%s = \"%s\"\n", left, right);
2814 g_free(right);
2815 } else if (is_unary_unsigned(&node->u.ctf_expression.right)) {
2816 uint64_t v;
2817 int ret;
2818
2819 ret = get_unary_unsigned(&node->u.ctf_expression.right, &v);
2820 if (ret)
2821 goto error;
2822 printf_verbose("env.%s = %" PRIu64 "\n", left, v);
2823 } else if (is_unary_signed(&node->u.ctf_expression.right)) {
2824 int64_t v;
2825 int ret;
2826
2827 ret = get_unary_signed(&node->u.ctf_expression.right, &v);
2828 if (ret)
2829 goto error;
2830 printf_verbose("env.%s = %" PRId64 "\n", left, v);
2831 } else {
2832 printf_verbose("%s: attribute \"%s\" has unknown type.\n", __func__, left);
2833 }
2834 }
2835
2836 error:
2837 g_free(left);
2838 break;
2839 }
2840 default:
2841 return -EPERM;
2842 }
2843
2844 return ret;
2845 }
2846
2847 static
2848 int ctf_env_visit(FILE *fd, int depth, struct ctf_node *node, struct ctf_trace *trace)
2849 {
2850 int ret = 0;
2851 struct ctf_node *iter;
2852
2853 trace->env.vpid = -1;
2854 trace->env.procname[0] = '\0';
2855 trace->env.hostname[0] = '\0';
2856 trace->env.domain[0] = '\0';
2857 trace->env.sysname[0] = '\0';
2858 trace->env.release[0] = '\0';
2859 trace->env.version[0] = '\0';
2860 bt_list_for_each_entry(iter, &node->u.env.declaration_list, siblings) {
2861 ret = ctf_env_declaration_visit(fd, depth + 1, iter, trace);
2862 if (ret)
2863 goto error;
2864 }
2865 error:
2866 return 0;
2867 }
2868
2869 static
2870 int ctf_root_declaration_visit(FILE *fd, int depth, struct ctf_node *node, struct ctf_trace *trace)
2871 {
2872 int ret = 0;
2873
2874 switch (node->type) {
2875 case NODE_TYPEDEF:
2876 ret = ctf_typedef_visit(fd, depth + 1,
2877 trace->root_declaration_scope,
2878 node->u._typedef.type_specifier_list,
2879 &node->u._typedef.type_declarators,
2880 trace);
2881 if (ret)
2882 return ret;
2883 break;
2884 case NODE_TYPEALIAS:
2885 ret = ctf_typealias_visit(fd, depth + 1,
2886 trace->root_declaration_scope,
2887 node->u.typealias.target, node->u.typealias.alias,
2888 trace);
2889 if (ret)
2890 return ret;
2891 break;
2892 case NODE_TYPE_SPECIFIER_LIST:
2893 {
2894 struct bt_declaration *declaration;
2895
2896 /*
2897 * Just add the type specifier to the root scope
2898 * declaration scope. Release local reference.
2899 */
2900 declaration = ctf_type_specifier_list_visit(fd, depth + 1,
2901 node, trace->root_declaration_scope, trace);
2902 if (!declaration)
2903 return -ENOMEM;
2904 bt_declaration_unref(declaration);
2905 break;
2906 }
2907 default:
2908 return -EPERM;
2909 }
2910
2911 return 0;
2912 }
2913
2914 int ctf_visitor_construct_metadata(FILE *fd, int depth, struct ctf_node *node,
2915 struct ctf_trace *trace, int byte_order)
2916 {
2917 int ret = 0;
2918 struct ctf_node *iter;
2919 int env_clock_done = 0;
2920
2921 printf_verbose("CTF visitor: metadata construction...\n");
2922 trace->byte_order = byte_order;
2923 trace->parent.clocks = g_hash_table_new_full(g_direct_hash,
2924 g_direct_equal, NULL, clock_free);
2925 trace->callsites = g_hash_table_new_full(g_direct_hash, g_direct_equal,
2926 NULL, callsite_free);
2927
2928 retry:
2929 trace->root_declaration_scope = bt_new_declaration_scope(NULL);
2930
2931 switch (node->type) {
2932 case NODE_ROOT:
2933 if (!env_clock_done) {
2934 /*
2935 * declarations need to query clock hash table,
2936 * so clock need to be treated first.
2937 */
2938 if (bt_list_empty(&node->u.root.clock)) {
2939 ctf_clock_default(fd, depth + 1, trace);
2940 } else {
2941 bt_list_for_each_entry(iter, &node->u.root.clock, siblings) {
2942 ret = ctf_clock_visit(fd, depth + 1, iter,
2943 trace);
2944 if (ret) {
2945 fprintf(fd, "[error] %s: clock declaration error\n", __func__);
2946 goto error;
2947 }
2948 }
2949 }
2950 env_clock_done = 1;
2951 }
2952 bt_list_for_each_entry(iter, &node->u.root.declaration_list,
2953 siblings) {
2954 ret = ctf_root_declaration_visit(fd, depth + 1, iter, trace);
2955 if (ret) {
2956 fprintf(fd, "[error] %s: root declaration error\n", __func__);
2957 goto error;
2958 }
2959 }
2960 bt_list_for_each_entry(iter, &node->u.root.trace, siblings) {
2961 ret = ctf_trace_visit(fd, depth + 1, iter, trace);
2962 if (ret == -EINTR) {
2963 bt_free_declaration_scope(trace->root_declaration_scope);
2964 /*
2965 * Need to restart creation of type
2966 * definitions, aliases and
2967 * trace header declarations.
2968 */
2969 goto retry;
2970 }
2971 if (ret) {
2972 fprintf(fd, "[error] %s: trace declaration error\n", __func__);
2973 goto error;
2974 }
2975 }
2976 bt_list_for_each_entry(iter, &node->u.root.callsite, siblings) {
2977 ret = ctf_callsite_visit(fd, depth + 1, iter,
2978 trace);
2979 if (ret) {
2980 fprintf(fd, "[error] %s: callsite declaration error\n", __func__);
2981 goto error;
2982 }
2983 }
2984 if (!trace->streams) {
2985 fprintf(fd, "[error] %s: missing trace declaration\n", __func__);
2986 ret = -EINVAL;
2987 goto error;
2988 }
2989 bt_list_for_each_entry(iter, &node->u.root.env, siblings) {
2990 ret = ctf_env_visit(fd, depth + 1, iter, trace);
2991 if (ret) {
2992 fprintf(fd, "[error] %s: env declaration error\n", __func__);
2993 goto error;
2994 }
2995 }
2996 bt_list_for_each_entry(iter, &node->u.root.stream, siblings) {
2997 ret = ctf_stream_visit(fd, depth + 1, iter,
2998 trace->root_declaration_scope, trace);
2999 if (ret) {
3000 fprintf(fd, "[error] %s: stream declaration error\n", __func__);
3001 goto error;
3002 }
3003 }
3004 bt_list_for_each_entry(iter, &node->u.root.event, siblings) {
3005 ret = ctf_event_visit(fd, depth + 1, iter,
3006 trace->root_declaration_scope, trace);
3007 if (ret) {
3008 fprintf(fd, "[error] %s: event declaration error\n", __func__);
3009 goto error;
3010 }
3011 }
3012 break;
3013 case NODE_UNKNOWN:
3014 default:
3015 fprintf(fd, "[error] %s: unknown node type %d\n", __func__,
3016 (int) node->type);
3017 ret = -EINVAL;
3018 goto error;
3019 }
3020 printf_verbose("done.\n");
3021 return ret;
3022
3023 error:
3024 bt_free_declaration_scope(trace->root_declaration_scope);
3025 g_hash_table_destroy(trace->callsites);
3026 g_hash_table_destroy(trace->parent.clocks);
3027 return ret;
3028 }
3029
3030 int ctf_destroy_metadata(struct ctf_trace *trace)
3031 {
3032 int i;
3033 struct ctf_file_stream *metadata_stream;
3034
3035 if (trace->streams) {
3036 for (i = 0; i < trace->streams->len; i++) {
3037 struct ctf_stream_declaration *stream;
3038 int j;
3039
3040 stream = g_ptr_array_index(trace->streams, i);
3041 if (!stream)
3042 continue;
3043 for (j = 0; j < stream->streams->len; j++) {
3044 struct ctf_stream_definition *stream_def;
3045 int k;
3046
3047 stream_def = g_ptr_array_index(stream->streams, j);
3048 if (!stream_def)
3049 continue;
3050 for (k = 0; k < stream_def->events_by_id->len; k++) {
3051 struct ctf_event_definition *event;
3052
3053 event = g_ptr_array_index(stream_def->events_by_id, k);
3054 if (!event)
3055 continue;
3056 if (&event->event_fields->p)
3057 bt_definition_unref(&event->event_fields->p);
3058 if (&event->event_context->p)
3059 bt_definition_unref(&event->event_context->p);
3060 g_free(event);
3061 }
3062 if (&stream_def->trace_packet_header->p)
3063 bt_definition_unref(&stream_def->trace_packet_header->p);
3064 if (&stream_def->stream_event_header->p)
3065 bt_definition_unref(&stream_def->stream_event_header->p);
3066 if (&stream_def->stream_packet_context->p)
3067 bt_definition_unref(&stream_def->stream_packet_context->p);
3068 if (&stream_def->stream_event_context->p)
3069 bt_definition_unref(&stream_def->stream_event_context->p);
3070 g_ptr_array_free(stream_def->events_by_id, TRUE);
3071 g_free(stream_def);
3072 }
3073 if (stream->event_header_decl)
3074 bt_declaration_unref(&stream->event_header_decl->p);
3075 if (stream->event_context_decl)
3076 bt_declaration_unref(&stream->event_context_decl->p);
3077 if (stream->packet_context_decl)
3078 bt_declaration_unref(&stream->packet_context_decl->p);
3079 g_ptr_array_free(stream->streams, TRUE);
3080 g_ptr_array_free(stream->events_by_id, TRUE);
3081 g_hash_table_destroy(stream->event_quark_to_id);
3082 bt_free_declaration_scope(stream->declaration_scope);
3083 g_free(stream);
3084 }
3085 g_ptr_array_free(trace->streams, TRUE);
3086 }
3087
3088 if (trace->event_declarations) {
3089 for (i = 0; i < trace->event_declarations->len; i++) {
3090 struct bt_ctf_event_decl *event_decl;
3091 struct ctf_event_declaration *event;
3092
3093 event_decl = g_ptr_array_index(trace->event_declarations, i);
3094 if (event_decl->context_decl)
3095 g_ptr_array_free(event_decl->context_decl, TRUE);
3096 if (event_decl->fields_decl)
3097 g_ptr_array_free(event_decl->fields_decl, TRUE);
3098 if (event_decl->packet_header_decl)
3099 g_ptr_array_free(event_decl->packet_header_decl, TRUE);
3100 if (event_decl->event_context_decl)
3101 g_ptr_array_free(event_decl->event_context_decl, TRUE);
3102 if (event_decl->event_header_decl)
3103 g_ptr_array_free(event_decl->event_header_decl, TRUE);
3104 if (event_decl->packet_context_decl)
3105 g_ptr_array_free(event_decl->packet_context_decl, TRUE);
3106
3107 event = &event_decl->parent;
3108 if (event->fields_decl)
3109 bt_declaration_unref(&event->fields_decl->p);
3110 if (event->context_decl)
3111 bt_declaration_unref(&event->context_decl->p);
3112 bt_free_declaration_scope(event->declaration_scope);
3113
3114 g_free(event);
3115 }
3116 g_ptr_array_free(trace->event_declarations, TRUE);
3117 }
3118 if (trace->packet_header_decl)
3119 bt_declaration_unref(&trace->packet_header_decl->p);
3120
3121 bt_free_declaration_scope(trace->root_declaration_scope);
3122 bt_free_declaration_scope(trace->declaration_scope);
3123
3124 g_hash_table_destroy(trace->callsites);
3125 g_hash_table_destroy(trace->parent.clocks);
3126
3127 metadata_stream = container_of(trace->metadata, struct ctf_file_stream, parent);
3128 g_free(metadata_stream);
3129
3130 return 0;
3131 }
This page took 0.23058 seconds and 4 git commands to generate.