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